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