vscode: Add configurations to run the executables under the debugger
[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 PERROR("shutdown");
383 }
384
385 closeret = close(sock);
386 if (closeret) {
387 PERROR("close");
388 }
389
390 return ret;
391 }
392
393 /*
394 * Send a message accompanied by fd(s) over a unix socket.
395 *
396 * Returns the size of data sent, or negative error value.
397 */
398 ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd)
399 {
400 struct msghdr msg;
401 struct cmsghdr *cmptr;
402 struct iovec iov[1];
403 ssize_t ret = -1;
404 unsigned int sizeof_fds = nb_fd * sizeof(int);
405 char tmp[CMSG_SPACE(sizeof_fds)];
406 char dummy = 0;
407
408 LTTNG_ASSERT(sock);
409 LTTNG_ASSERT(fds);
410 LTTNG_ASSERT(nb_fd > 0);
411
412 memset(&msg, 0, sizeof(msg));
413 memset(tmp, 0, sizeof(tmp));
414
415 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
416 return -EINVAL;
417
418 msg.msg_control = (caddr_t)tmp;
419 msg.msg_controllen = CMSG_LEN(sizeof_fds);
420
421 cmptr = CMSG_FIRSTHDR(&msg);
422 if (!cmptr) {
423 return -1;
424 }
425
426 cmptr->cmsg_level = SOL_SOCKET;
427 cmptr->cmsg_type = SCM_RIGHTS;
428 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
429 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
430 /* Sum of the length of all control messages in the buffer: */
431 msg.msg_controllen = cmptr->cmsg_len;
432
433 iov[0].iov_base = &dummy;
434 iov[0].iov_len = 1;
435 msg.msg_iov = iov;
436 msg.msg_iovlen = 1;
437
438 do {
439 ret = sendmsg(sock, &msg, 0);
440 } while (ret < 0 && errno == EINTR);
441 if (ret < 0) {
442 /*
443 * Only warn about EPIPE when quiet mode is deactivated.
444 * We consider EPIPE as expected.
445 */
446 if (errno != EPIPE || !lttng_opt_quiet) {
447 PERROR("sendmsg");
448 }
449 }
450 return ret;
451 }
452
453 /*
454 * Send the fd(s) of a payload view over a unix socket.
455 *
456 * Returns the size of data sent, or negative error value.
457 */
458 static
459 ssize_t _lttcomm_send_payload_view_fds_unix_sock(int sock,
460 struct lttng_payload_view *view,
461 bool blocking)
462 {
463 int i;
464 ssize_t ret;
465 struct lttng_dynamic_array raw_fds;
466 const int fd_count = lttng_payload_view_get_fd_handle_count(view);
467
468 lttng_dynamic_array_init(&raw_fds, sizeof(int), NULL);
469
470 if (fd_count < 0) {
471 ret = -LTTNG_ERR_INVALID;
472 goto end;
473 }
474
475 /*
476 * Prepare a contiguous array of file descriptors to send them.
477 *
478 * Note that the reference to each fd is released during the iteration;
479 * we're just getting the numerical value of the fds to conform to the
480 * syscall's interface. We rely on the fact that "view" must remain
481 * valid for the duration of the call and that the underlying payload
482 * owns a reference to the fd_handles.
483 */
484 for (i = 0; i < fd_count; i++) {
485 struct fd_handle *handle =
486 lttng_payload_view_pop_fd_handle(view);
487 const int raw_fd = fd_handle_get_fd(handle);
488 const int add_ret = lttng_dynamic_array_add_element(
489 &raw_fds, &raw_fd);
490
491 fd_handle_put(handle);
492 if (add_ret) {
493 ret = -LTTNG_ERR_NOMEM;
494 goto end;
495 }
496 }
497
498 if (blocking) {
499 ret = lttcomm_send_fds_unix_sock(sock,
500 (const int *) raw_fds.buffer.data, fd_count);
501 } else {
502 ret = lttcomm_send_fds_unix_sock_non_block(sock,
503 (const int *) raw_fds.buffer.data, fd_count);
504 }
505
506 end:
507 lttng_dynamic_array_reset(&raw_fds);
508 return ret;
509 }
510
511 ssize_t lttcomm_send_payload_view_fds_unix_sock(int sock,
512 struct lttng_payload_view *view)
513 {
514 return _lttcomm_send_payload_view_fds_unix_sock(sock, view, true);
515 }
516
517 ssize_t lttcomm_send_payload_view_fds_unix_sock_non_block(int sock,
518 struct lttng_payload_view *view)
519 {
520 return _lttcomm_send_payload_view_fds_unix_sock(sock, view, false);
521 }
522
523 /*
524 * Send a message accompanied by fd(s) over a unix socket.
525 * Only use for non blocking socket.
526 *
527 * Returns the size of data sent, or negative error value.
528 */
529 ssize_t lttcomm_send_fds_unix_sock_non_block(int sock, const int *fds, size_t nb_fd)
530 {
531 struct msghdr msg;
532 struct cmsghdr *cmptr;
533 struct iovec iov[1];
534 ssize_t ret = -1;
535 unsigned int sizeof_fds = nb_fd * sizeof(int);
536 char tmp[CMSG_SPACE(sizeof_fds)];
537 char dummy = 0;
538
539 LTTNG_ASSERT(sock);
540 LTTNG_ASSERT(fds);
541 LTTNG_ASSERT(nb_fd > 0);
542
543 memset(&msg, 0, sizeof(msg));
544 memset(tmp, 0, sizeof(tmp));
545
546 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
547 return -EINVAL;
548
549 msg.msg_control = (caddr_t)tmp;
550 msg.msg_controllen = CMSG_LEN(sizeof_fds);
551
552 cmptr = CMSG_FIRSTHDR(&msg);
553 if (!cmptr) {
554 return -1;
555 }
556
557 cmptr->cmsg_level = SOL_SOCKET;
558 cmptr->cmsg_type = SCM_RIGHTS;
559 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
560 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
561 /* Sum of the length of all control messages in the buffer: */
562 msg.msg_controllen = cmptr->cmsg_len;
563
564 iov[0].iov_base = &dummy;
565 iov[0].iov_len = 1;
566 msg.msg_iov = iov;
567 msg.msg_iovlen = 1;
568
569 retry:
570 ret = sendmsg(sock, &msg, 0);
571 if (ret < 0) {
572 if (errno == EINTR) {
573 goto retry;
574 } else {
575 /*
576 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
577 */
578 DIAGNOSTIC_PUSH
579 DIAGNOSTIC_IGNORE_LOGICAL_OP
580 if (errno == EAGAIN || errno == EWOULDBLOCK) {
581 DIAGNOSTIC_POP
582 /*
583 * This can happen in non blocking mode.
584 * Nothing was sent.
585 */
586 ret = 0;
587 goto end;
588 }
589
590 if (errno == EPIPE) {
591 /* Expected error, pass error to caller */
592 DBG3("EPIPE on sendmsg");
593 ret = -1;
594 goto end;
595 }
596
597 /* Unexpected error */
598 PERROR("sendmsg");
599 ret = -1;
600 goto end;
601 }
602 }
603
604 end:
605 return ret;
606 }
607
608 /*
609 * Recv a message accompanied by fd(s) from a unix socket.
610 *
611 * Returns the size of received data, or negative error value.
612 *
613 * Expect at most "nb_fd" file descriptors. Returns the number of fd
614 * actually received in nb_fd.
615 */
616 ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
617 {
618 struct iovec iov[1];
619 ssize_t ret = 0;
620 struct cmsghdr *cmsg;
621 size_t sizeof_fds = nb_fd * sizeof(int);
622
623 #ifdef __linux__
624 /* Account for the struct ucred cmsg in the buffer size */
625 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
626 #else
627 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
628 #endif /* __linux__ */
629
630 char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
631 struct msghdr msg;
632 char dummy;
633
634 LTTNG_ASSERT(sock);
635 LTTNG_ASSERT(fds);
636 LTTNG_ASSERT(nb_fd > 0);
637
638 memset(&msg, 0, sizeof(msg));
639
640 /* Prepare to receive the structures */
641 iov[0].iov_base = &dummy;
642 iov[0].iov_len = 1;
643 msg.msg_iov = iov;
644 msg.msg_iovlen = 1;
645
646 cmsg = (struct cmsghdr *) recv_buf;
647 cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
648 cmsg->cmsg_level = SOL_SOCKET;
649 cmsg->cmsg_type = SCM_RIGHTS;
650
651 msg.msg_control = cmsg;
652 msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
653 msg.msg_flags = 0;
654
655 retry:
656 ret = lttng_recvmsg_nosigpipe(sock, &msg);
657 if (ret < 0) {
658 if (errno == EINTR) {
659 goto retry;
660 } else {
661 /* We consider EPIPE and EAGAIN as expected. */
662 if (!lttng_opt_quiet &&
663 (errno != EPIPE && errno != EAGAIN)) {
664 PERROR("recvmsg");
665 }
666 goto end;
667 }
668 }
669
670 if (ret != 1) {
671 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
672 ret, 1);
673 goto end;
674 }
675
676 if (msg.msg_flags & MSG_CTRUNC) {
677 fprintf(stderr, "Error: Control message truncated.\n");
678 ret = -1;
679 goto end;
680 }
681
682 /*
683 * If the socket was configured with SO_PASSCRED, the kernel will add a
684 * control message (cmsg) to the ancillary data of the unix socket. We
685 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
686 * message.
687 */
688 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
689 if (cmsg->cmsg_level != SOL_SOCKET) {
690 fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
691 ret = -1;
692 goto end;
693 }
694 if (cmsg->cmsg_type == SCM_RIGHTS) {
695 /*
696 * We found the controle message for file descriptors,
697 * now copy the fds to the fds ptr and return success.
698 */
699 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
700 fprintf(stderr, "Error: Received %zu bytes of"
701 "ancillary data for FDs, expected %zu\n",
702 (size_t) cmsg->cmsg_len,
703 (size_t) CMSG_LEN(sizeof_fds));
704 ret = -1;
705 goto end;
706 }
707 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
708 ret = sizeof_fds;
709 goto end;
710 }
711 #ifdef __linux__
712 if (cmsg->cmsg_type == SCM_CREDENTIALS) {
713 /*
714 * Expect credentials to be sent when expecting fds even
715 * if no credential were include in the send(). The
716 * kernel adds them...
717 */
718 ret = -1;
719 }
720 #endif /* __linux__ */
721 }
722 end:
723 return ret;
724 }
725
726 static
727 void close_raw_fd(void *ptr)
728 {
729 const int raw_fd = *((const int *) ptr);
730
731 if (raw_fd >= 0) {
732 const int ret = close(raw_fd);
733
734 if (ret) {
735 PERROR("Failed to close file descriptor %d", raw_fd);
736 }
737 }
738 }
739
740 static
741 enum lttng_error_code add_fds_to_payload(struct lttng_dynamic_array *raw_fds,
742 struct lttng_payload *payload)
743 {
744 int i;
745 enum lttng_error_code ret_code = LTTNG_OK;
746 const int fd_count = lttng_dynamic_array_get_count(raw_fds);
747
748 for (i = 0; i < fd_count; i++) {
749 int ret;
750 struct fd_handle *handle;
751 int *raw_fd = (int *) lttng_dynamic_array_get_element(
752 raw_fds, i);
753
754 LTTNG_ASSERT(*raw_fd != -1);
755
756 handle = fd_handle_create(*raw_fd);
757 if (!handle) {
758 ret_code = LTTNG_ERR_NOMEM;
759 goto end;
760 }
761
762 /* FD ownership transferred to the handle. */
763 *raw_fd = -1;
764
765 ret = lttng_payload_push_fd_handle(payload, handle);
766 fd_handle_put(handle);
767 if (ret) {
768 ret_code = LTTNG_ERR_NOMEM;
769 goto end;
770 }
771 }
772
773 end:
774 return ret_code;
775 }
776
777 static
778 ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
779 struct lttng_payload *payload, bool blocking)
780 {
781 int i = 0;
782 enum lttng_error_code add_ret;
783 ssize_t ret;
784 int default_value = -1;
785 struct lttng_dynamic_array raw_fds;
786
787 LTTNG_ASSERT(sock);
788 LTTNG_ASSERT(payload);
789 LTTNG_ASSERT(nb_fd > 0);
790
791 lttng_dynamic_array_init(&raw_fds, sizeof(int), close_raw_fd);
792
793 for (i = 0; i < nb_fd; i++) {
794 if (lttng_dynamic_array_add_element(&raw_fds, &default_value)) {
795 ret = -LTTNG_ERR_NOMEM;
796 goto end;
797 }
798 }
799
800 if (blocking) {
801 ret = lttcomm_recv_fds_unix_sock(
802 sock, (int *) raw_fds.buffer.data, nb_fd);
803 } else {
804 ret = lttcomm_recv_fds_unix_sock_non_block(
805 sock, (int *) raw_fds.buffer.data, nb_fd);
806 }
807
808 if (ret <= 0) {
809 goto end;
810 }
811
812 add_ret = add_fds_to_payload(&raw_fds, payload);
813 if (add_ret != LTTNG_OK) {
814 ret = - (int) add_ret;
815 goto end;
816 }
817
818 end:
819 lttng_dynamic_array_reset(&raw_fds);
820 return ret;
821 }
822
823 ssize_t lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
824 struct lttng_payload *payload)
825 {
826 return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, true);
827 }
828
829 ssize_t lttcomm_recv_payload_fds_unix_sock_non_block(int sock, size_t nb_fd,
830 struct lttng_payload *payload)
831 {
832 return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, false);
833 }
834
835 /*
836 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
837 * Only use with non-blocking sockets.
838 *
839 * Returns the size of received data, or negative error value.
840 *
841 * Expect at most "nb_fd" file descriptors.
842 *
843 * Note that based on our comprehension, partial reception of fds is not
844 * possible since the FDs are actually in the control message. It is all or
845 * nothing, still the sender side can send the wrong number of fds.
846 */
847 ssize_t lttcomm_recv_fds_unix_sock_non_block(int sock, int *fds, size_t nb_fd)
848 {
849 struct iovec iov[1];
850 ssize_t ret = 0;
851 struct cmsghdr *cmsg;
852 size_t sizeof_fds = nb_fd * sizeof(int);
853
854 LTTNG_ASSERT(sock);
855 LTTNG_ASSERT(fds);
856 LTTNG_ASSERT(nb_fd > 0);
857
858 #ifdef __linux__
859 /* Account for the struct ucred cmsg in the buffer size */
860 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
861 #else
862 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
863 #endif /* __linux__ */
864
865 char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
866 struct msghdr msg;
867 char dummy;
868
869 memset(&msg, 0, sizeof(msg));
870
871 /* Prepare to receive the structures */
872 iov[0].iov_base = &dummy;
873 iov[0].iov_len = 1;
874 msg.msg_iov = iov;
875 msg.msg_iovlen = 1;
876
877 cmsg = (struct cmsghdr *) recv_buf;
878 cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
879 cmsg->cmsg_level = SOL_SOCKET;
880 cmsg->cmsg_type = SCM_RIGHTS;
881
882 msg.msg_control = cmsg;
883 msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
884 msg.msg_flags = 0;
885
886 retry:
887 ret = lttng_recvmsg_nosigpipe(sock, &msg);
888 if (ret < 0) {
889 if (errno == EINTR) {
890 goto retry;
891 } else {
892 /*
893 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
894 */
895 DIAGNOSTIC_PUSH
896 DIAGNOSTIC_IGNORE_LOGICAL_OP
897 if (errno == EAGAIN || errno == EWOULDBLOCK) {
898 DIAGNOSTIC_POP
899 /*
900 * This can happen in non blocking mode.
901 * Nothing was recv.
902 */
903 ret = 0;
904 goto end;
905 }
906
907 if (errno == EPIPE) {
908 /* Expected error, pass error to caller */
909 DBG3("EPIPE on recvmsg");
910 ret = -1;
911 goto end;
912 }
913
914 /* Unexpected error */
915 PERROR("recvmsg");
916 ret = -1;
917 goto end;
918 }
919 }
920
921 if (ret != 1) {
922 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
923 ret, 1);
924 goto end;
925 }
926
927 if (msg.msg_flags & MSG_CTRUNC) {
928 fprintf(stderr, "Error: Control message truncated.\n");
929 ret = -1;
930 goto end;
931 }
932
933 /*
934 * If the socket was configured with SO_PASSCRED, the kernel will add a
935 * control message (cmsg) to the ancillary data of the unix socket. We
936 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
937 * message.
938 */
939 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
940 if (cmsg->cmsg_level != SOL_SOCKET) {
941 fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
942 ret = -1;
943 goto end;
944 }
945 if (cmsg->cmsg_type == SCM_RIGHTS) {
946 /*
947 * We found the controle message for file descriptors,
948 * now copy the fds to the fds ptr and return success.
949 */
950 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
951 fprintf(stderr, "Error: Received %zu bytes of"
952 "ancillary data for FDs, expected %zu\n",
953 (size_t) cmsg->cmsg_len,
954 (size_t) CMSG_LEN(sizeof_fds));
955 ret = -1;
956 goto end;
957 }
958 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
959 ret = sizeof_fds;
960 goto end;
961 }
962 #ifdef __linux__
963 if (cmsg->cmsg_type == SCM_CREDENTIALS) {
964 /*
965 * Expect credentials to be sent when expecting fds even
966 * if no credential were include in the send(). The
967 * kernel adds them...
968 */
969 ret = -1;
970 }
971 #endif /* __linux__ */
972 }
973 end:
974 return ret;
975 }
976
977 /*
978 * Send a message with credentials over a unix socket.
979 *
980 * Returns the size of data sent, or negative error value.
981 */
982 ssize_t lttcomm_send_creds_unix_sock(int sock, const void *buf, size_t len)
983 {
984 struct msghdr msg;
985 struct iovec iov[1];
986 ssize_t ret = -1;
987 #if defined(__linux__) || defined(__CYGWIN__)
988 struct cmsghdr *cmptr;
989 size_t sizeof_cred = sizeof(lttng_sock_cred);
990 char anc_buf[CMSG_SPACE(sizeof_cred)];
991 lttng_sock_cred *creds;
992
993 memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char));
994 #endif /* __linux__, __CYGWIN__ */
995
996 memset(&msg, 0, sizeof(msg));
997
998 LTTNG_ASSERT(sock);
999 LTTNG_ASSERT(buf);
1000 LTTNG_ASSERT(len > 0);
1001
1002 iov[0].iov_base = (void *) buf;
1003 iov[0].iov_len = len;
1004 msg.msg_iov = iov;
1005 msg.msg_iovlen = 1;
1006
1007 #if defined(__linux__) || defined(__CYGWIN__)
1008 msg.msg_control = (caddr_t) anc_buf;
1009 msg.msg_controllen = CMSG_LEN(sizeof_cred);
1010
1011 cmptr = CMSG_FIRSTHDR(&msg);
1012 if (!cmptr) {
1013 return -1;
1014 }
1015 cmptr->cmsg_level = SOL_SOCKET;
1016 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
1017 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
1018
1019 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
1020
1021 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
1022 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
1023 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
1024 #endif /* __linux__, __CYGWIN__ */
1025
1026 do {
1027 ret = sendmsg(sock, &msg, 0);
1028 } while (ret < 0 && errno == EINTR);
1029 if (ret < 0) {
1030 /*
1031 * Only warn about EPIPE when quiet mode is deactivated.
1032 * We consider EPIPE as expected.
1033 */
1034 if (errno != EPIPE || !lttng_opt_quiet) {
1035 PERROR("sendmsg");
1036 }
1037 }
1038 return ret;
1039 }
1040
1041 /*
1042 * Recv a message accompanied with credentials from a unix socket.
1043 *
1044 * Returns the size of received data, or negative error value.
1045 */
1046 ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
1047 lttng_sock_cred *creds)
1048 {
1049 struct msghdr msg;
1050 struct iovec iov[1];
1051 ssize_t ret;
1052 size_t len_last;
1053 #if defined(__linux__) || defined(__CYGWIN__)
1054 struct cmsghdr *cmptr;
1055 size_t sizeof_cred = sizeof(lttng_sock_cred);
1056 char anc_buf[CMSG_SPACE(sizeof_cred)];
1057 #endif /* __linux__, __CYGWIN__ */
1058
1059 LTTNG_ASSERT(sock);
1060 LTTNG_ASSERT(buf);
1061 LTTNG_ASSERT(len > 0);
1062 LTTNG_ASSERT(creds);
1063
1064 memset(&msg, 0, sizeof(msg));
1065
1066 /* Prepare to receive the structures */
1067 iov[0].iov_base = buf;
1068 iov[0].iov_len = len;
1069 msg.msg_iov = iov;
1070 msg.msg_iovlen = 1;
1071
1072 #if defined(__linux__) || defined(__CYGWIN__)
1073 msg.msg_control = anc_buf;
1074 msg.msg_controllen = sizeof(anc_buf);
1075 #endif /* __linux__, __CYGWIN__ */
1076
1077 do {
1078 len_last = iov[0].iov_len;
1079 ret = recvmsg(sock, &msg, 0);
1080 if (ret > 0) {
1081 iov[0].iov_base = (char *) iov[0].iov_base + ret;
1082 iov[0].iov_len -= ret;
1083 LTTNG_ASSERT(ret <= len_last);
1084 }
1085 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
1086 if (ret < 0) {
1087 PERROR("recvmsg fds");
1088 goto end;
1089 } else if (ret > 0) {
1090 ret = len;
1091 }
1092 /* Else ret = 0 meaning an orderly shutdown. */
1093
1094 #if defined(__linux__) || defined(__CYGWIN__)
1095 if (msg.msg_flags & MSG_CTRUNC) {
1096 fprintf(stderr, "Error: Control message truncated.\n");
1097 ret = -1;
1098 goto end;
1099 }
1100
1101 cmptr = CMSG_FIRSTHDR(&msg);
1102 if (cmptr == NULL) {
1103 fprintf(stderr, "Error: Invalid control message header\n");
1104 ret = -1;
1105 goto end;
1106 }
1107
1108 if (cmptr->cmsg_level != SOL_SOCKET ||
1109 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
1110 fprintf(stderr, "Didn't received any credentials\n");
1111 ret = -1;
1112 goto end;
1113 }
1114
1115 if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
1116 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
1117 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
1118 ret = -1;
1119 goto end;
1120 }
1121
1122 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
1123 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1124 if (lttng_get_unix_socket_peer_creds(sock, creds)) {
1125 fprintf(stderr, "ARG\n");
1126 ret = -1;
1127 goto end;
1128 }
1129 #else
1130 #error "Please implement credential support for your OS."
1131 #endif /* __linux__, __CYGWIN__ */
1132
1133 end:
1134 return ret;
1135 }
1136
1137 /*
1138 * Set socket option to use credentials passing.
1139 */
1140 #if defined(__linux__) || defined(__CYGWIN__)
1141 int lttcomm_setsockopt_creds_unix_sock(int sock)
1142 {
1143 int ret, on = 1;
1144
1145 /* Set socket for credentials retrieval */
1146 ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
1147 if (ret < 0) {
1148 PERROR("setsockopt creds unix sock");
1149 }
1150 return ret;
1151 }
1152 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1153 int lttcomm_setsockopt_creds_unix_sock(int sock __attribute__((unused)))
1154 {
1155 return 0;
1156 }
1157 #else
1158 #error "Please implement credential support for your OS."
1159 #endif /* __linux__ */
This page took 0.050529 seconds and 4 git commands to generate.