-
Notifications
You must be signed in to change notification settings - Fork 76
/
unix.c
1170 lines (1018 loc) · 25.3 KB
/
unix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 EfficiOS Inc.
* Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
*
* SPDX-License-Identifier: LGPL-2.1-only
*
*/
#define _LGPL_SOURCE
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <common/common.h>
#include <common/compat/errno.h>
#include <common/sessiond-comm/sessiond-comm.h>
#include <common/fd-handle.h>
#include "unix.h"
/*
* Connect to unix socket using the path name.
*/
LTTNG_HIDDEN
int lttcomm_connect_unix_sock(const char *pathname)
{
struct sockaddr_un s_un;
int fd, ret, closeret;
if (strlen(pathname) >= sizeof(s_un.sun_path)) {
ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
pathname, strlen(pathname) + 1,
sizeof(s_un.sun_path));
ret = -ENAMETOOLONG;
goto error;
}
fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
PERROR("socket");
ret = fd;
goto error;
}
memset(&s_un, 0, sizeof(s_un));
s_un.sun_family = AF_UNIX;
strncpy(s_un.sun_path, pathname, sizeof(s_un.sun_path));
s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0';
ret = connect(fd, (struct sockaddr *) &s_un, sizeof(s_un));
if (ret < 0) {
/*
* Don't print message on connect error, because connect is used in
* normal execution to detect if sessiond is alive.
*/
goto error_connect;
}
return fd;
error_connect:
closeret = close(fd);
if (closeret) {
PERROR("close");
}
error:
return ret;
}
/*
* Do an accept(2) on the sock and return the new file descriptor. The socket
* MUST be bind(2) before.
*/
LTTNG_HIDDEN
int lttcomm_accept_unix_sock(int sock)
{
int new_fd;
struct sockaddr_un s_un;
socklen_t len = sizeof(s_un);
/* Blocking call */
new_fd = accept(sock, (struct sockaddr *) &s_un, &len);
if (new_fd < 0) {
PERROR("accept");
}
return new_fd;
}
LTTNG_HIDDEN
int lttcomm_create_anon_unix_socketpair(int *fds)
{
if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) < 0) {
PERROR("socketpair");
return -1;
}
return 0;
}
/*
* Creates a AF_UNIX local socket using pathname bind the socket upon creation
* and return the fd.
*/
LTTNG_HIDDEN
int lttcomm_create_unix_sock(const char *pathname)
{
struct sockaddr_un s_un;
int fd = -1;
int ret = -1;
if (strlen(pathname) >= sizeof(s_un.sun_path)) {
ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
pathname, strlen(pathname) + 1,
sizeof(s_un.sun_path));
ret = -ENAMETOOLONG;
goto error;
}
/* Create server socket */
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
PERROR("socket");
goto error;
}
memset(&s_un, 0, sizeof(s_un));
s_un.sun_family = AF_UNIX;
strncpy(s_un.sun_path, pathname, sizeof(s_un.sun_path));
s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0';
/* Unlink the old file if present */
(void) unlink(pathname);
ret = bind(fd, (struct sockaddr *) &s_un, sizeof(s_un));
if (ret < 0) {
PERROR("bind");
goto error;
}
return fd;
error:
if (fd >= 0) {
if (close(fd) < 0) {
PERROR("close create unix sock");
}
}
return ret;
}
/*
* Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
*/
LTTNG_HIDDEN
int lttcomm_listen_unix_sock(int sock)
{
int ret;
ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN);
if (ret < 0) {
PERROR("listen");
}
return ret;
}
/*
* Receive data of size len in put that data into the buf param. Using recvmsg
* API.
*
* Return the size of received data.
*/
LTTNG_HIDDEN
ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t ret = -1;
size_t len_last;
assert(sock);
assert(buf);
assert(len > 0);
memset(&msg, 0, sizeof(msg));
iov[0].iov_base = buf;
iov[0].iov_len = len;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
do {
len_last = iov[0].iov_len;
ret = lttng_recvmsg_nosigpipe(sock, &msg);
if (ret > 0) {
iov[0].iov_base += ret;
iov[0].iov_len -= ret;
assert(ret <= len_last);
}
} while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
if (ret < 0) {
PERROR("recvmsg");
} else if (ret > 0) {
ret = len;
}
/* Else ret = 0 meaning an orderly shutdown. */
return ret;
}
/*
* Receive data of size len in put that data into the buf param. Using recvmsg
* API. Only use with sockets set in non-blocking mode.
*
* NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
* poll set. The poll loop will handle the EPIPE original cause.
*
* Return the size of received data.
*/
LTTNG_HIDDEN
ssize_t lttcomm_recv_unix_sock_non_block(int sock, void *buf, size_t len)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t ret;
assert(sock);
assert(buf);
assert(len > 0);
memset(&msg, 0, sizeof(msg));
iov[0].iov_base = buf;
iov[0].iov_len = len;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
retry:
ret = lttng_recvmsg_nosigpipe(sock, &msg);
if (ret < 0) {
if (errno == EINTR) {
goto retry;
} else {
/*
* We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
*/
if (errno == EAGAIN || errno == EWOULDBLOCK ||
errno == EPIPE) {
/*
* Nothing was recv.
*/
ret = 0;
goto end;
}
/* Unexpected error */
PERROR("recvmsg");
ret = -1;
goto end;
}
}
end:
return ret;
}
/*
* Send buf data of size len. Using sendmsg API.
*
* Return the size of sent data.
*/
LTTNG_HIDDEN
ssize_t lttcomm_send_unix_sock(int sock, const void *buf, size_t len)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t ret;
assert(sock);
assert(buf);
assert(len > 0);
memset(&msg, 0, sizeof(msg));
iov[0].iov_base = (void *) buf;
iov[0].iov_len = len;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
while (iov[0].iov_len) {
ret = sendmsg(sock, &msg, 0);
if (ret < 0) {
if (errno == EINTR) {
continue;
} else {
/*
* Only warn about EPIPE when quiet mode is
* deactivated.
* We consider EPIPE as expected.
*/
if (errno != EPIPE || !lttng_opt_quiet) {
PERROR("sendmsg");
}
goto end;
}
}
iov[0].iov_len -= ret;
iov[0].iov_base += ret;
}
ret = len;
end:
return ret;
}
/*
* Send buf data of size len. Using sendmsg API.
* Only use with non-blocking sockets. The difference with the blocking version
* of the function is that this one does not retry to send on partial sends,
* except if the interruption was caused by a signal (EINTR).
*
* NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
* poll set. The poll loop will handle the EPIPE original cause.
*
* Return the size of sent data.
*/
LTTNG_HIDDEN
ssize_t lttcomm_send_unix_sock_non_block(int sock, const void *buf, size_t len)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t ret;
assert(sock);
assert(buf);
assert(len > 0);
memset(&msg, 0, sizeof(msg));
iov[0].iov_base = (void *) buf;
iov[0].iov_len = len;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
retry:
ret = sendmsg(sock, &msg, 0);
if (ret < 0) {
if (errno == EINTR) {
goto retry;
} else {
/*
* We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
*/
if (errno == EAGAIN || errno == EWOULDBLOCK ||
errno == EPIPE) {
/*
* This can happen in non blocking mode.
* Nothing was sent.
*/
ret = 0;
goto end;
}
/* Unexpected error */
PERROR("sendmsg");
ret = -1;
goto end;
}
}
end:
return ret;
}
/*
* Shutdown cleanly a unix socket.
*/
LTTNG_HIDDEN
int lttcomm_close_unix_sock(int sock)
{
int ret, closeret;
/* Shutdown receptions and transmissions */
ret = shutdown(sock, SHUT_RDWR);
if (ret < 0) {
PERROR("shutdown");
}
closeret = close(sock);
if (closeret) {
PERROR("close");
}
return ret;
}
/*
* Send a message accompanied by fd(s) over a unix socket.
*
* Returns the size of data sent, or negative error value.
*/
LTTNG_HIDDEN
ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd)
{
struct msghdr msg;
struct cmsghdr *cmptr;
struct iovec iov[1];
ssize_t ret = -1;
unsigned int sizeof_fds = nb_fd * sizeof(int);
char tmp[CMSG_SPACE(sizeof_fds)];
char dummy = 0;
assert(sock);
assert(fds);
assert(nb_fd > 0);
memset(&msg, 0, sizeof(msg));
memset(tmp, 0, sizeof(tmp));
if (nb_fd > LTTCOMM_MAX_SEND_FDS)
return -EINVAL;
msg.msg_control = (caddr_t)tmp;
msg.msg_controllen = CMSG_LEN(sizeof_fds);
cmptr = CMSG_FIRSTHDR(&msg);
if (!cmptr) {
return -1;
}
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
/* Sum of the length of all control messages in the buffer: */
msg.msg_controllen = cmptr->cmsg_len;
iov[0].iov_base = &dummy;
iov[0].iov_len = 1;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
do {
ret = sendmsg(sock, &msg, 0);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
/*
* Only warn about EPIPE when quiet mode is deactivated.
* We consider EPIPE as expected.
*/
if (errno != EPIPE || !lttng_opt_quiet) {
PERROR("sendmsg");
}
}
return ret;
}
/*
* Send the fd(s) of a payload view over a unix socket.
*
* Returns the size of data sent, or negative error value.
*/
static
ssize_t _lttcomm_send_payload_view_fds_unix_sock(int sock,
struct lttng_payload_view *view,
bool blocking)
{
int i;
ssize_t ret;
struct lttng_dynamic_array raw_fds;
const int fd_count = lttng_payload_view_get_fd_handle_count(view);
lttng_dynamic_array_init(&raw_fds, sizeof(int), NULL);
if (fd_count < 0) {
ret = -LTTNG_ERR_INVALID;
goto end;
}
/*
* Prepare a contiguous array of file descriptors to send them.
*
* Note that the reference to each fd is released during the iteration;
* we're just getting the numerical value of the fds to conform to the
* syscall's interface. We rely on the fact that "view" must remain
* valid for the duration of the call and that the underlying payload
* owns a reference to the fd_handles.
*/
for (i = 0; i < fd_count; i++) {
struct fd_handle *handle =
lttng_payload_view_pop_fd_handle(view);
const int raw_fd = fd_handle_get_fd(handle);
const int add_ret = lttng_dynamic_array_add_element(
&raw_fds, &raw_fd);
fd_handle_put(handle);
if (add_ret) {
ret = -LTTNG_ERR_NOMEM;
goto end;
}
}
if (blocking) {
ret = lttcomm_send_fds_unix_sock(sock,
(const int *) raw_fds.buffer.data, fd_count);
} else {
ret = lttcomm_send_fds_unix_sock_non_block(sock,
(const int *) raw_fds.buffer.data, fd_count);
}
end:
lttng_dynamic_array_reset(&raw_fds);
return ret;
}
LTTNG_HIDDEN
ssize_t lttcomm_send_payload_view_fds_unix_sock(int sock,
struct lttng_payload_view *view)
{
return _lttcomm_send_payload_view_fds_unix_sock(sock, view, true);
}
LTTNG_HIDDEN
ssize_t lttcomm_send_payload_view_fds_unix_sock_non_block(int sock,
struct lttng_payload_view *view)
{
return _lttcomm_send_payload_view_fds_unix_sock(sock, view, false);
}
/*
* Send a message accompanied by fd(s) over a unix socket.
* Only use for non blocking socket.
*
* Returns the size of data sent, or negative error value.
*/
LTTNG_HIDDEN
ssize_t lttcomm_send_fds_unix_sock_non_block(int sock, const int *fds, size_t nb_fd)
{
struct msghdr msg;
struct cmsghdr *cmptr;
struct iovec iov[1];
ssize_t ret = -1;
unsigned int sizeof_fds = nb_fd * sizeof(int);
char tmp[CMSG_SPACE(sizeof_fds)];
char dummy = 0;
assert(sock);
assert(fds);
assert(nb_fd > 0);
memset(&msg, 0, sizeof(msg));
memset(tmp, 0, sizeof(tmp));
if (nb_fd > LTTCOMM_MAX_SEND_FDS)
return -EINVAL;
msg.msg_control = (caddr_t)tmp;
msg.msg_controllen = CMSG_LEN(sizeof_fds);
cmptr = CMSG_FIRSTHDR(&msg);
if (!cmptr) {
return -1;
}
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
/* Sum of the length of all control messages in the buffer: */
msg.msg_controllen = cmptr->cmsg_len;
iov[0].iov_base = &dummy;
iov[0].iov_len = 1;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
retry:
ret = sendmsg(sock, &msg, 0);
if (ret < 0) {
if (errno == EINTR) {
goto retry;
} else {
/*
* We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
*/
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/*
* This can happen in non blocking mode.
* Nothing was sent.
*/
ret = 0;
goto end;
}
if (errno == EPIPE) {
/* Expected error, pass error to caller */
DBG3("EPIPE on sendmsg");
ret = -1;
goto end;
}
/* Unexpected error */
PERROR("sendmsg");
ret = -1;
goto end;
}
}
end:
return ret;
}
/*
* Recv a message accompanied by fd(s) from a unix socket.
*
* Returns the size of received data, or negative error value.
*
* Expect at most "nb_fd" file descriptors. Returns the number of fd
* actually received in nb_fd.
*/
LTTNG_HIDDEN
ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
{
struct iovec iov[1];
ssize_t ret = 0;
struct cmsghdr *cmsg;
size_t sizeof_fds = nb_fd * sizeof(int);
#ifdef __linux__
/* Account for the struct ucred cmsg in the buffer size */
#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
#else
#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
#endif /* __linux__ */
char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
struct msghdr msg;
char dummy;
assert(sock);
assert(fds);
assert(nb_fd > 0);
memset(&msg, 0, sizeof(msg));
/* Prepare to receive the structures */
iov[0].iov_base = &dummy;
iov[0].iov_len = 1;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
cmsg = (struct cmsghdr *) recv_buf;
cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
msg.msg_control = cmsg;
msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
msg.msg_flags = 0;
retry:
ret = lttng_recvmsg_nosigpipe(sock, &msg);
if (ret < 0) {
if (errno == EINTR) {
goto retry;
} else {
/* We consider EPIPE and EAGAIN as expected. */
if (!lttng_opt_quiet &&
(errno != EPIPE && errno != EAGAIN)) {
PERROR("recvmsg");
}
goto end;
}
}
if (ret != 1) {
fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
ret, 1);
goto end;
}
if (msg.msg_flags & MSG_CTRUNC) {
fprintf(stderr, "Error: Control message truncated.\n");
ret = -1;
goto end;
}
/*
* If the socket was configured with SO_PASSCRED, the kernel will add a
* control message (cmsg) to the ancillary data of the unix socket. We
* need to expect a cmsg of the SCM_CREDENTIALS as the first control
* message.
*/
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET) {
fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
ret = -1;
goto end;
}
if (cmsg->cmsg_type == SCM_RIGHTS) {
/*
* We found the controle message for file descriptors,
* now copy the fds to the fds ptr and return success.
*/
if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
fprintf(stderr, "Error: Received %zu bytes of"
"ancillary data for FDs, expected %zu\n",
(size_t) cmsg->cmsg_len,
(size_t) CMSG_LEN(sizeof_fds));
ret = -1;
goto end;
}
memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
ret = sizeof_fds;
goto end;
}
#ifdef __linux__
if (cmsg->cmsg_type == SCM_CREDENTIALS) {
/*
* Expect credentials to be sent when expecting fds even
* if no credential were include in the send(). The
* kernel adds them...
*/
ret = -1;
}
#endif /* __linux__ */
}
end:
return ret;
}
static
void close_raw_fd(void *ptr)
{
const int raw_fd = *((const int *) ptr);
if (raw_fd >= 0) {
const int ret = close(raw_fd);
if (ret) {
PERROR("Failed to close file descriptor %d", raw_fd);
}
}
}
static
enum lttng_error_code add_fds_to_payload(struct lttng_dynamic_array *raw_fds,
struct lttng_payload *payload)
{
int i;
enum lttng_error_code ret_code = LTTNG_OK;
const int fd_count = lttng_dynamic_array_get_count(raw_fds);
for (i = 0; i < fd_count; i++) {
int ret;
struct fd_handle *handle;
int *raw_fd = (int *) lttng_dynamic_array_get_element(
raw_fds, i);
assert(*raw_fd != -1);
handle = fd_handle_create(*raw_fd);
if (!handle) {
ret_code = LTTNG_ERR_NOMEM;
goto end;
}
/* FD ownership transferred to the handle. */
*raw_fd = -1;
ret = lttng_payload_push_fd_handle(payload, handle);
fd_handle_put(handle);
if (ret) {
ret_code = LTTNG_ERR_NOMEM;
goto end;
}
}
end:
return ret_code;
}
static
ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
struct lttng_payload *payload, bool blocking)
{
int i = 0;
enum lttng_error_code add_ret;
ssize_t ret;
int default_value = -1;
struct lttng_dynamic_array raw_fds;
assert(sock);
assert(payload);
assert(nb_fd > 0);
lttng_dynamic_array_init(&raw_fds, sizeof(int), close_raw_fd);
for (i = 0; i < nb_fd; i++) {
if (lttng_dynamic_array_add_element(&raw_fds, &default_value)) {
ret = -LTTNG_ERR_NOMEM;
goto end;
}
}
if (blocking) {
ret = lttcomm_recv_fds_unix_sock(
sock, (int *) raw_fds.buffer.data, nb_fd);
} else {
ret = lttcomm_recv_fds_unix_sock_non_block(
sock, (int *) raw_fds.buffer.data, nb_fd);
}
if (ret <= 0) {
goto end;
}
add_ret = add_fds_to_payload(&raw_fds, payload);
if (add_ret != LTTNG_OK) {
ret = - (int) add_ret;
goto end;
}
end:
lttng_dynamic_array_reset(&raw_fds);
return ret;
}
LTTNG_HIDDEN
ssize_t lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
struct lttng_payload *payload)
{
return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, true);
}
LTTNG_HIDDEN
ssize_t lttcomm_recv_payload_fds_unix_sock_non_block(int sock, size_t nb_fd,
struct lttng_payload *payload)
{
return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, false);
}
/*
* Recv a message accompanied by fd(s) from a non-blocking unix socket.
* Only use with non-blocking sockets.
*
* Returns the size of received data, or negative error value.
*
* Expect at most "nb_fd" file descriptors.
*
* Note that based on our comprehension, partial reception of fds is not
* possible since the FDs are actually in the control message. It is all or
* nothing, still the sender side can send the wrong number of fds.
*/
LTTNG_HIDDEN
ssize_t lttcomm_recv_fds_unix_sock_non_block(int sock, int *fds, size_t nb_fd)
{
struct iovec iov[1];
ssize_t ret = 0;
struct cmsghdr *cmsg;
size_t sizeof_fds = nb_fd * sizeof(int);
assert(sock);
assert(fds);
assert(nb_fd > 0);
#ifdef __linux__
/* Account for the struct ucred cmsg in the buffer size */
#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
#else
#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
#endif /* __linux__ */
char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
struct msghdr msg;
char dummy;
memset(&msg, 0, sizeof(msg));
/* Prepare to receive the structures */
iov[0].iov_base = &dummy;
iov[0].iov_len = 1;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
cmsg = (struct cmsghdr *) recv_buf;
cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
msg.msg_control = cmsg;
msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
msg.msg_flags = 0;
retry:
ret = lttng_recvmsg_nosigpipe(sock, &msg);
if (ret < 0) {
if (errno == EINTR) {
goto retry;
} else {
/*
* We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
*/
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/*
* This can happen in non blocking mode.
* Nothing was recv.
*/
ret = 0;
goto end;
}
if (errno == EPIPE) {
/* Expected error, pass error to caller */
DBG3("EPIPE on recvmsg");
ret = -1;
goto end;
}
/* Unexpected error */
PERROR("recvmsg");
ret = -1;
goto end;
}
}
if (ret != 1) {
fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
ret, 1);
goto end;
}
if (msg.msg_flags & MSG_CTRUNC) {
fprintf(stderr, "Error: Control message truncated.\n");
ret = -1;
goto end;
}
/*
* If the socket was configured with SO_PASSCRED, the kernel will add a
* control message (cmsg) to the ancillary data of the unix socket. We
* need to expect a cmsg of the SCM_CREDENTIALS as the first control
* message.
*/
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET) {
fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
ret = -1;
goto end;
}
if (cmsg->cmsg_type == SCM_RIGHTS) {
/*
* We found the controle message for file descriptors,
* now copy the fds to the fds ptr and return success.
*/
if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
fprintf(stderr, "Error: Received %zu bytes of"
"ancillary data for FDs, expected %zu\n",
(size_t) cmsg->cmsg_len,
(size_t) CMSG_LEN(sizeof_fds));
ret = -1;
goto end;
}
memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
ret = sizeof_fds;
goto end;
}
#ifdef __linux__
if (cmsg->cmsg_type == SCM_CREDENTIALS) {
/*
* Expect credentials to be sent when expecting fds even
* if no credential were include in the send(). The
* kernel adds them...
*/
ret = -1;
}
#endif /* __linux__ */
}
end:
return ret;
}
/*
* Send a message with credentials over a unix socket.
*
* Returns the size of data sent, or negative error value.
*/
LTTNG_HIDDEN
ssize_t lttcomm_send_creds_unix_sock(int sock, const void *buf, size_t len)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t ret = -1;
#if defined(__linux__) || defined(__CYGWIN__)
struct cmsghdr *cmptr;
size_t sizeof_cred = sizeof(lttng_sock_cred);
char anc_buf[CMSG_SPACE(sizeof_cred)];
lttng_sock_cred *creds;