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