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
);
385 closeret
= close(sock
);
394 * Send a message accompanied by fd(s) over a unix socket.
396 * Returns the size of data sent, or negative error value.
398 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
401 struct cmsghdr
*cmptr
;
404 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
405 char tmp
[CMSG_SPACE(sizeof_fds
)];
410 LTTNG_ASSERT(nb_fd
> 0);
412 memset(&msg
, 0, sizeof(msg
));
413 memset(tmp
, 0, sizeof(tmp
));
415 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
418 msg
.msg_control
= (caddr_t
)tmp
;
419 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
421 cmptr
= CMSG_FIRSTHDR(&msg
);
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
;
433 iov
[0].iov_base
= &dummy
;
439 ret
= sendmsg(sock
, &msg
, 0);
440 } while (ret
< 0 && errno
== EINTR
);
443 * Only warn about EPIPE when quiet mode is deactivated.
444 * We consider EPIPE as expected.
446 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
454 * Send the fd(s) of a payload view over a unix socket.
456 * Returns the size of data sent, or negative error value.
459 ssize_t
_lttcomm_send_payload_view_fds_unix_sock(int sock
,
460 struct lttng_payload_view
*view
,
465 struct lttng_dynamic_array raw_fds
;
466 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
468 lttng_dynamic_array_init(&raw_fds
, sizeof(int), NULL
);
471 ret
= -LTTNG_ERR_INVALID
;
476 * Prepare a contiguous array of file descriptors to send them.
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.
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(
491 fd_handle_put(handle
);
493 ret
= -LTTNG_ERR_NOMEM
;
499 ret
= lttcomm_send_fds_unix_sock(sock
,
500 (const int *) raw_fds
.buffer
.data
, fd_count
);
502 ret
= lttcomm_send_fds_unix_sock_non_block(sock
,
503 (const int *) raw_fds
.buffer
.data
, fd_count
);
507 lttng_dynamic_array_reset(&raw_fds
);
511 ssize_t
lttcomm_send_payload_view_fds_unix_sock(int sock
,
512 struct lttng_payload_view
*view
)
514 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, true);
517 ssize_t
lttcomm_send_payload_view_fds_unix_sock_non_block(int sock
,
518 struct lttng_payload_view
*view
)
520 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, false);
524 * Send a message accompanied by fd(s) over a unix socket.
525 * Only use for non blocking socket.
527 * Returns the size of data sent, or negative error value.
529 ssize_t
lttcomm_send_fds_unix_sock_non_block(int sock
, const int *fds
, size_t nb_fd
)
532 struct cmsghdr
*cmptr
;
535 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
536 char tmp
[CMSG_SPACE(sizeof_fds
)];
541 LTTNG_ASSERT(nb_fd
> 0);
543 memset(&msg
, 0, sizeof(msg
));
544 memset(tmp
, 0, sizeof(tmp
));
546 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
549 msg
.msg_control
= (caddr_t
)tmp
;
550 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
552 cmptr
= CMSG_FIRSTHDR(&msg
);
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
;
564 iov
[0].iov_base
= &dummy
;
570 ret
= sendmsg(sock
, &msg
, 0);
572 if (errno
== EINTR
) {
576 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
579 DIAGNOSTIC_IGNORE_LOGICAL_OP
580 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
583 * This can happen in non blocking mode.
590 if (errno
== EPIPE
) {
591 /* Expected error, pass error to caller */
592 DBG3("EPIPE on sendmsg");
597 /* Unexpected error */
609 * Recv a message accompanied by fd(s) from a unix socket.
611 * Returns the size of received data, or negative error value.
613 * Expect at most "nb_fd" file descriptors. Returns the number of fd
614 * actually received in nb_fd.
616 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
620 struct cmsghdr
*cmsg
;
621 size_t sizeof_fds
= nb_fd
* sizeof(int);
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))
627 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
628 #endif /* __linux__ */
630 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
636 LTTNG_ASSERT(nb_fd
> 0);
638 memset(&msg
, 0, sizeof(msg
));
640 /* Prepare to receive the structures */
641 iov
[0].iov_base
= &dummy
;
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
;
651 msg
.msg_control
= cmsg
;
652 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
656 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
658 if (errno
== EINTR
) {
661 /* We consider EPIPE and EAGAIN as expected. */
662 if (!lttng_opt_quiet
&&
663 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
671 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
676 if (msg
.msg_flags
& MSG_CTRUNC
) {
677 fprintf(stderr
, "Error: Control message truncated.\n");
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
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");
694 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
696 * We found the controle message for file descriptors,
697 * now copy the fds to the fds ptr and return success.
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
));
707 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
712 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
714 * Expect credentials to be sent when expecting fds even
715 * if no credential were include in the send(). The
716 * kernel adds them...
720 #endif /* __linux__ */
727 void close_raw_fd(void *ptr
)
729 const int raw_fd
= *((const int *) ptr
);
732 const int ret
= close(raw_fd
);
735 PERROR("Failed to close file descriptor %d", raw_fd
);
741 enum lttng_error_code
add_fds_to_payload(struct lttng_dynamic_array
*raw_fds
,
742 struct lttng_payload
*payload
)
745 enum lttng_error_code ret_code
= LTTNG_OK
;
746 const int fd_count
= lttng_dynamic_array_get_count(raw_fds
);
748 for (i
= 0; i
< fd_count
; i
++) {
750 struct fd_handle
*handle
;
751 int *raw_fd
= (int *) lttng_dynamic_array_get_element(
754 LTTNG_ASSERT(*raw_fd
!= -1);
756 handle
= fd_handle_create(*raw_fd
);
758 ret_code
= LTTNG_ERR_NOMEM
;
762 /* FD ownership transferred to the handle. */
765 ret
= lttng_payload_push_fd_handle(payload
, handle
);
766 fd_handle_put(handle
);
768 ret_code
= LTTNG_ERR_NOMEM
;
778 ssize_t
_lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
779 struct lttng_payload
*payload
, bool blocking
)
782 enum lttng_error_code add_ret
;
784 int default_value
= -1;
785 struct lttng_dynamic_array raw_fds
;
788 LTTNG_ASSERT(payload
);
789 LTTNG_ASSERT(nb_fd
> 0);
791 lttng_dynamic_array_init(&raw_fds
, sizeof(int), close_raw_fd
);
793 for (i
= 0; i
< nb_fd
; i
++) {
794 if (lttng_dynamic_array_add_element(&raw_fds
, &default_value
)) {
795 ret
= -LTTNG_ERR_NOMEM
;
801 ret
= lttcomm_recv_fds_unix_sock(
802 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
804 ret
= lttcomm_recv_fds_unix_sock_non_block(
805 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
812 add_ret
= add_fds_to_payload(&raw_fds
, payload
);
813 if (add_ret
!= LTTNG_OK
) {
814 ret
= - (int) add_ret
;
819 lttng_dynamic_array_reset(&raw_fds
);
823 ssize_t
lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
824 struct lttng_payload
*payload
)
826 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, true);
829 ssize_t
lttcomm_recv_payload_fds_unix_sock_non_block(int sock
, size_t nb_fd
,
830 struct lttng_payload
*payload
)
832 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, false);
836 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
837 * Only use with non-blocking sockets.
839 * Returns the size of received data, or negative error value.
841 * Expect at most "nb_fd" file descriptors.
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.
847 ssize_t
lttcomm_recv_fds_unix_sock_non_block(int sock
, int *fds
, size_t nb_fd
)
851 struct cmsghdr
*cmsg
;
852 size_t sizeof_fds
= nb_fd
* sizeof(int);
856 LTTNG_ASSERT(nb_fd
> 0);
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))
862 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
863 #endif /* __linux__ */
865 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
869 memset(&msg
, 0, sizeof(msg
));
871 /* Prepare to receive the structures */
872 iov
[0].iov_base
= &dummy
;
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
;
882 msg
.msg_control
= cmsg
;
883 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
887 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
889 if (errno
== EINTR
) {
893 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
896 DIAGNOSTIC_IGNORE_LOGICAL_OP
897 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
900 * This can happen in non blocking mode.
907 if (errno
== EPIPE
) {
908 /* Expected error, pass error to caller */
909 DBG3("EPIPE on recvmsg");
914 /* Unexpected error */
922 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
927 if (msg
.msg_flags
& MSG_CTRUNC
) {
928 fprintf(stderr
, "Error: Control message truncated.\n");
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
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");
945 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
947 * We found the controle message for file descriptors,
948 * now copy the fds to the fds ptr and return success.
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
));
958 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
963 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
965 * Expect credentials to be sent when expecting fds even
966 * if no credential were include in the send(). The
967 * kernel adds them...
971 #endif /* __linux__ */
978 * Send a message with credentials over a unix socket.
980 * Returns the size of data sent, or negative error value.
982 ssize_t
lttcomm_send_creds_unix_sock(int sock
, const void *buf
, size_t len
)
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
;
993 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
994 #endif /* __linux__, __CYGWIN__ */
996 memset(&msg
, 0, sizeof(msg
));
1000 LTTNG_ASSERT(len
> 0);
1002 iov
[0].iov_base
= (void *) buf
;
1003 iov
[0].iov_len
= len
;
1007 #if defined(__linux__) || defined(__CYGWIN__)
1008 msg
.msg_control
= (caddr_t
) anc_buf
;
1009 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
1011 cmptr
= CMSG_FIRSTHDR(&msg
);
1015 cmptr
->cmsg_level
= SOL_SOCKET
;
1016 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
1017 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
1019 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
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__ */
1027 ret
= sendmsg(sock
, &msg
, 0);
1028 } while (ret
< 0 && errno
== EINTR
);
1031 * Only warn about EPIPE when quiet mode is deactivated.
1032 * We consider EPIPE as expected.
1034 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
1042 * Recv a message accompanied with credentials from a unix socket.
1044 * Returns the size of received data, or negative error value.
1046 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
1047 lttng_sock_cred
*creds
)
1050 struct iovec iov
[1];
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__ */
1061 LTTNG_ASSERT(len
> 0);
1062 LTTNG_ASSERT(creds
);
1064 memset(&msg
, 0, sizeof(msg
));
1066 /* Prepare to receive the structures */
1067 iov
[0].iov_base
= buf
;
1068 iov
[0].iov_len
= len
;
1072 #if defined(__linux__) || defined(__CYGWIN__)
1073 msg
.msg_control
= anc_buf
;
1074 msg
.msg_controllen
= sizeof(anc_buf
);
1075 #endif /* __linux__, __CYGWIN__ */
1078 len_last
= iov
[0].iov_len
;
1079 ret
= recvmsg(sock
, &msg
, 0);
1081 iov
[0].iov_base
= (char *) iov
[0].iov_base
+ ret
;
1082 iov
[0].iov_len
-= ret
;
1083 LTTNG_ASSERT(ret
<= len_last
);
1085 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
1087 PERROR("recvmsg fds");
1089 } else if (ret
> 0) {
1092 /* Else ret = 0 meaning an orderly shutdown. */
1094 #if defined(__linux__) || defined(__CYGWIN__)
1095 if (msg
.msg_flags
& MSG_CTRUNC
) {
1096 fprintf(stderr
, "Error: Control message truncated.\n");
1101 cmptr
= CMSG_FIRSTHDR(&msg
);
1102 if (cmptr
== NULL
) {
1103 fprintf(stderr
, "Error: Invalid control message header\n");
1108 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
1109 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
1110 fprintf(stderr
, "Didn't received any credentials\n");
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
));
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");
1130 #error "Please implement credential support for your OS."
1131 #endif /* __linux__, __CYGWIN__ */
1138 * Set socket option to use credentials passing.
1140 #if defined(__linux__) || defined(__CYGWIN__)
1141 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1145 /* Set socket for credentials retrieval */
1146 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
1148 PERROR("setsockopt creds unix sock");
1152 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1153 int lttcomm_setsockopt_creds_unix_sock(int sock
__attribute__((unused
)))
1158 #error "Please implement credential support for your OS."
1159 #endif /* __linux__ */