2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
15 #include <sys/types.h>
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>
26 * Connect to unix socket using the path name.
28 int lttcomm_connect_unix_sock(const char *pathname
)
30 struct sockaddr_un s_un
;
31 int fd
, ret
, closeret
;
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
));
41 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
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';
53 ret
= connect(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
56 * Don't print message on connect error, because connect is used in
57 * normal execution to detect if sessiond is alive.
74 * Do an accept(2) on the sock and return the new file descriptor. The socket
75 * MUST be bind(2) before.
77 int lttcomm_accept_unix_sock(int sock
)
80 struct sockaddr_un s_un
;
81 socklen_t len
= sizeof(s_un
);
84 new_fd
= accept(sock
, (struct sockaddr
*) &s_un
, &len
);
92 int lttcomm_create_anon_unix_socketpair(int *fds
)
94 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
102 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
105 int lttcomm_create_unix_sock(const char *pathname
)
107 struct sockaddr_un s_un
;
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
));
119 /* Create server socket */
120 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
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';
130 /* Unlink the old file if present */
131 (void) unlink(pathname
);
132 ret
= bind(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
143 PERROR("close create unix sock");
150 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
152 int lttcomm_listen_unix_sock(int sock
)
156 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
165 * Receive data of size len in put that data into the buf param. Using recvmsg
168 * Return the size of received data.
170 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
179 LTTNG_ASSERT(len
> 0);
181 memset(&msg
, 0, sizeof(msg
));
183 iov
[0].iov_base
= buf
;
184 iov
[0].iov_len
= len
;
189 len_last
= iov
[0].iov_len
;
190 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
192 iov
[0].iov_base
= (char *) iov
[0].iov_base
+ ret
;
193 iov
[0].iov_len
-= ret
;
194 LTTNG_ASSERT(ret
<= len_last
);
196 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
199 } else if (ret
> 0) {
202 /* Else ret = 0 meaning an orderly shutdown. */
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.
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.
214 * Return the size of received data.
216 ssize_t
lttcomm_recv_unix_sock_non_block(int sock
, void *buf
, size_t len
)
224 LTTNG_ASSERT(len
> 0);
226 memset(&msg
, 0, sizeof(msg
));
228 iov
[0].iov_base
= buf
;
229 iov
[0].iov_len
= len
;
234 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
236 if (errno
== EINTR
) {
240 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
243 DIAGNOSTIC_IGNORE_LOGICAL_OP
244 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
254 /* Unexpected error */
266 * Send buf data of size len. Using sendmsg API.
268 * Return the size of sent data.
270 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
278 LTTNG_ASSERT(len
> 0);
280 memset(&msg
, 0, sizeof(msg
));
282 iov
[0].iov_base
= (void *) buf
;
283 iov
[0].iov_len
= len
;
287 while (iov
[0].iov_len
) {
288 ret
= sendmsg(sock
, &msg
, 0);
290 if (errno
== EINTR
) {
294 * Only warn about EPIPE when quiet mode is
296 * We consider EPIPE as expected.
298 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
304 iov
[0].iov_len
-= ret
;
305 iov
[0].iov_base
= (char *) iov
[0].iov_base
+ ret
;
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).
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.
321 * Return the size of sent data.
323 ssize_t
lttcomm_send_unix_sock_non_block(int sock
, const void *buf
, size_t len
)
331 LTTNG_ASSERT(len
> 0);
333 memset(&msg
, 0, sizeof(msg
));
335 iov
[0].iov_base
= (void *) buf
;
336 iov
[0].iov_len
= len
;
341 ret
= sendmsg(sock
, &msg
, 0);
343 if (errno
== EINTR
) {
347 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
350 DIAGNOSTIC_IGNORE_LOGICAL_OP
351 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
355 * This can happen in non blocking mode.
362 /* Unexpected error */
373 * Shutdown cleanly a unix socket.
375 int lttcomm_close_unix_sock(int sock
)
379 /* Shutdown receptions and transmissions */
380 ret
= shutdown(sock
, SHUT_RDWR
);
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
387 if (errno
== ENOTCONN
) {
394 closeret
= close(sock
);
403 * Send a message accompanied by fd(s) over a unix socket.
405 * Returns the size of data sent, or negative error value.
407 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
410 struct cmsghdr
*cmptr
;
413 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
414 char tmp
[CMSG_SPACE(sizeof_fds
)];
419 LTTNG_ASSERT(nb_fd
> 0);
421 memset(&msg
, 0, sizeof(msg
));
422 memset(tmp
, 0, sizeof(tmp
));
424 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
427 msg
.msg_control
= (caddr_t
)tmp
;
428 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
430 cmptr
= CMSG_FIRSTHDR(&msg
);
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
;
442 iov
[0].iov_base
= &dummy
;
448 ret
= sendmsg(sock
, &msg
, 0);
449 } while (ret
< 0 && errno
== EINTR
);
452 * Only warn about EPIPE when quiet mode is deactivated.
453 * We consider EPIPE as expected.
455 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
463 * Send the fd(s) of a payload view over a unix socket.
465 * Returns the size of data sent, or negative error value.
468 ssize_t
_lttcomm_send_payload_view_fds_unix_sock(int sock
,
469 struct lttng_payload_view
*view
,
474 struct lttng_dynamic_array raw_fds
;
475 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
477 lttng_dynamic_array_init(&raw_fds
, sizeof(int), NULL
);
480 ret
= -LTTNG_ERR_INVALID
;
485 * Prepare a contiguous array of file descriptors to send them.
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.
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(
500 fd_handle_put(handle
);
502 ret
= -LTTNG_ERR_NOMEM
;
508 ret
= lttcomm_send_fds_unix_sock(sock
,
509 (const int *) raw_fds
.buffer
.data
, fd_count
);
511 ret
= lttcomm_send_fds_unix_sock_non_block(sock
,
512 (const int *) raw_fds
.buffer
.data
, fd_count
);
516 lttng_dynamic_array_reset(&raw_fds
);
520 ssize_t
lttcomm_send_payload_view_fds_unix_sock(int sock
,
521 struct lttng_payload_view
*view
)
523 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, true);
526 ssize_t
lttcomm_send_payload_view_fds_unix_sock_non_block(int sock
,
527 struct lttng_payload_view
*view
)
529 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, false);
533 * Send a message accompanied by fd(s) over a unix socket.
534 * Only use for non blocking socket.
536 * Returns the size of data sent, or negative error value.
538 ssize_t
lttcomm_send_fds_unix_sock_non_block(int sock
, const int *fds
, size_t nb_fd
)
541 struct cmsghdr
*cmptr
;
544 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
545 char tmp
[CMSG_SPACE(sizeof_fds
)];
550 LTTNG_ASSERT(nb_fd
> 0);
552 memset(&msg
, 0, sizeof(msg
));
553 memset(tmp
, 0, sizeof(tmp
));
555 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
558 msg
.msg_control
= (caddr_t
)tmp
;
559 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
561 cmptr
= CMSG_FIRSTHDR(&msg
);
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
;
573 iov
[0].iov_base
= &dummy
;
579 ret
= sendmsg(sock
, &msg
, 0);
581 if (errno
== EINTR
) {
585 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
588 DIAGNOSTIC_IGNORE_LOGICAL_OP
589 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
592 * This can happen in non blocking mode.
599 if (errno
== EPIPE
) {
600 /* Expected error, pass error to caller */
601 DBG3("EPIPE on sendmsg");
606 /* Unexpected error */
618 * Recv a message accompanied by fd(s) from a unix socket.
620 * Returns the size of received data, or negative error value.
622 * Expect at most "nb_fd" file descriptors. Returns the number of fd
623 * actually received in nb_fd.
625 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
629 struct cmsghdr
*cmsg
;
630 size_t sizeof_fds
= nb_fd
* sizeof(int);
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))
636 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
637 #endif /* __linux__ */
639 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
645 LTTNG_ASSERT(nb_fd
> 0);
647 memset(&msg
, 0, sizeof(msg
));
649 /* Prepare to receive the structures */
650 iov
[0].iov_base
= &dummy
;
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
;
660 msg
.msg_control
= cmsg
;
661 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
665 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
667 if (errno
== EINTR
) {
670 /* We consider EPIPE and EAGAIN as expected. */
671 if (!lttng_opt_quiet
&&
672 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
680 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
685 if (msg
.msg_flags
& MSG_CTRUNC
) {
686 fprintf(stderr
, "Error: Control message truncated.\n");
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
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");
703 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
705 * We found the controle message for file descriptors,
706 * now copy the fds to the fds ptr and return success.
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
));
716 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
721 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
723 * Expect credentials to be sent when expecting fds even
724 * if no credential were include in the send(). The
725 * kernel adds them...
729 #endif /* __linux__ */
736 void close_raw_fd(void *ptr
)
738 const int raw_fd
= *((const int *) ptr
);
741 const int ret
= close(raw_fd
);
744 PERROR("Failed to close file descriptor %d", raw_fd
);
750 enum lttng_error_code
add_fds_to_payload(struct lttng_dynamic_array
*raw_fds
,
751 struct lttng_payload
*payload
)
754 enum lttng_error_code ret_code
= LTTNG_OK
;
755 const int fd_count
= lttng_dynamic_array_get_count(raw_fds
);
757 for (i
= 0; i
< fd_count
; i
++) {
759 struct fd_handle
*handle
;
760 int *raw_fd
= (int *) lttng_dynamic_array_get_element(
763 LTTNG_ASSERT(*raw_fd
!= -1);
765 handle
= fd_handle_create(*raw_fd
);
767 ret_code
= LTTNG_ERR_NOMEM
;
771 /* FD ownership transferred to the handle. */
774 ret
= lttng_payload_push_fd_handle(payload
, handle
);
775 fd_handle_put(handle
);
777 ret_code
= LTTNG_ERR_NOMEM
;
787 ssize_t
_lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
788 struct lttng_payload
*payload
, bool blocking
)
791 enum lttng_error_code add_ret
;
793 int default_value
= -1;
794 struct lttng_dynamic_array raw_fds
;
797 LTTNG_ASSERT(payload
);
798 LTTNG_ASSERT(nb_fd
> 0);
800 lttng_dynamic_array_init(&raw_fds
, sizeof(int), close_raw_fd
);
802 for (i
= 0; i
< nb_fd
; i
++) {
803 if (lttng_dynamic_array_add_element(&raw_fds
, &default_value
)) {
804 ret
= -LTTNG_ERR_NOMEM
;
810 ret
= lttcomm_recv_fds_unix_sock(
811 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
813 ret
= lttcomm_recv_fds_unix_sock_non_block(
814 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
821 add_ret
= add_fds_to_payload(&raw_fds
, payload
);
822 if (add_ret
!= LTTNG_OK
) {
823 ret
= - (int) add_ret
;
828 lttng_dynamic_array_reset(&raw_fds
);
832 ssize_t
lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
833 struct lttng_payload
*payload
)
835 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, true);
838 ssize_t
lttcomm_recv_payload_fds_unix_sock_non_block(int sock
, size_t nb_fd
,
839 struct lttng_payload
*payload
)
841 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, false);
845 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
846 * Only use with non-blocking sockets.
848 * Returns the size of received data, or negative error value.
850 * Expect at most "nb_fd" file descriptors.
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.
856 ssize_t
lttcomm_recv_fds_unix_sock_non_block(int sock
, int *fds
, size_t nb_fd
)
860 struct cmsghdr
*cmsg
;
861 size_t sizeof_fds
= nb_fd
* sizeof(int);
865 LTTNG_ASSERT(nb_fd
> 0);
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))
871 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
872 #endif /* __linux__ */
874 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
878 memset(&msg
, 0, sizeof(msg
));
880 /* Prepare to receive the structures */
881 iov
[0].iov_base
= &dummy
;
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
;
891 msg
.msg_control
= cmsg
;
892 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
896 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
898 if (errno
== EINTR
) {
902 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
905 DIAGNOSTIC_IGNORE_LOGICAL_OP
906 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
909 * This can happen in non blocking mode.
916 if (errno
== EPIPE
) {
917 /* Expected error, pass error to caller */
918 DBG3("EPIPE on recvmsg");
923 /* Unexpected error */
931 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
936 if (msg
.msg_flags
& MSG_CTRUNC
) {
937 fprintf(stderr
, "Error: Control message truncated.\n");
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
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");
954 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
956 * We found the controle message for file descriptors,
957 * now copy the fds to the fds ptr and return success.
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
));
967 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
972 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
974 * Expect credentials to be sent when expecting fds even
975 * if no credential were include in the send(). The
976 * kernel adds them...
980 #endif /* __linux__ */
987 * Send a message with credentials over a unix socket.
989 * Returns the size of data sent, or negative error value.
991 ssize_t
lttcomm_send_creds_unix_sock(int sock
, const void *buf
, size_t len
)
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
;
1002 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
1003 #endif /* __linux__, __CYGWIN__ */
1005 memset(&msg
, 0, sizeof(msg
));
1009 LTTNG_ASSERT(len
> 0);
1011 iov
[0].iov_base
= (void *) buf
;
1012 iov
[0].iov_len
= len
;
1016 #if defined(__linux__) || defined(__CYGWIN__)
1017 msg
.msg_control
= (caddr_t
) anc_buf
;
1018 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
1020 cmptr
= CMSG_FIRSTHDR(&msg
);
1024 cmptr
->cmsg_level
= SOL_SOCKET
;
1025 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
1026 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
1028 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
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__ */
1036 ret
= sendmsg(sock
, &msg
, 0);
1037 } while (ret
< 0 && errno
== EINTR
);
1040 * Only warn about EPIPE when quiet mode is deactivated.
1041 * We consider EPIPE as expected.
1043 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
1051 * Recv a message accompanied with credentials from a unix socket.
1053 * Returns the size of received data, or negative error value.
1055 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
1056 lttng_sock_cred
*creds
)
1059 struct iovec iov
[1];
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__ */
1070 LTTNG_ASSERT(len
> 0);
1071 LTTNG_ASSERT(creds
);
1073 memset(&msg
, 0, sizeof(msg
));
1075 /* Prepare to receive the structures */
1076 iov
[0].iov_base
= buf
;
1077 iov
[0].iov_len
= len
;
1081 #if defined(__linux__) || defined(__CYGWIN__)
1082 msg
.msg_control
= anc_buf
;
1083 msg
.msg_controllen
= sizeof(anc_buf
);
1084 #endif /* __linux__, __CYGWIN__ */
1087 len_last
= iov
[0].iov_len
;
1088 ret
= recvmsg(sock
, &msg
, 0);
1090 iov
[0].iov_base
= (char *) iov
[0].iov_base
+ ret
;
1091 iov
[0].iov_len
-= ret
;
1092 LTTNG_ASSERT(ret
<= len_last
);
1094 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
1096 PERROR("recvmsg fds");
1098 } else if (ret
> 0) {
1101 /* Else ret = 0 meaning an orderly shutdown. */
1103 #if defined(__linux__) || defined(__CYGWIN__)
1104 if (msg
.msg_flags
& MSG_CTRUNC
) {
1105 fprintf(stderr
, "Error: Control message truncated.\n");
1110 cmptr
= CMSG_FIRSTHDR(&msg
);
1111 if (cmptr
== NULL
) {
1112 fprintf(stderr
, "Error: Invalid control message header\n");
1117 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
1118 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
1119 fprintf(stderr
, "Didn't received any credentials\n");
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
));
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");
1139 #error "Please implement credential support for your OS."
1140 #endif /* __linux__, __CYGWIN__ */
1147 * Set socket option to use credentials passing.
1149 #if defined(__linux__) || defined(__CYGWIN__)
1150 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1154 /* Set socket for credentials retrieval */
1155 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
1157 PERROR("setsockopt creds unix sock");
1161 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1162 int lttcomm_setsockopt_creds_unix_sock(int sock
__attribute__((unused
)))
1167 #error "Please implement credential support for your OS."
1168 #endif /* __linux__ */