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