2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
19 #include <common/common.h>
20 #include <common/compat/errno.h>
21 #include <common/sessiond-comm/sessiond-comm.h>
22 #include <common/fd-handle.h>
27 * Connect to unix socket using the path name.
30 int lttcomm_connect_unix_sock(const char *pathname
)
32 struct sockaddr_un s_un
;
33 int fd
, ret
, closeret
;
35 if (strlen(pathname
) >= sizeof(s_un
.sun_path
)) {
36 ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
37 pathname
, strlen(pathname
) + 1,
38 sizeof(s_un
.sun_path
));
43 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
50 memset(&s_un
, 0, sizeof(s_un
));
51 s_un
.sun_family
= AF_UNIX
;
52 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
53 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
55 ret
= connect(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
58 * Don't print message on connect error, because connect is used in
59 * normal execution to detect if sessiond is alive.
76 * Do an accept(2) on the sock and return the new file descriptor. The socket
77 * MUST be bind(2) before.
80 int lttcomm_accept_unix_sock(int sock
)
83 struct sockaddr_un s_un
;
84 socklen_t len
= sizeof(s_un
);
87 new_fd
= accept(sock
, (struct sockaddr
*) &s_un
, &len
);
96 int lttcomm_create_anon_unix_socketpair(int *fds
)
98 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
106 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
110 int lttcomm_create_unix_sock(const char *pathname
)
112 struct sockaddr_un s_un
;
116 if (strlen(pathname
) >= sizeof(s_un
.sun_path
)) {
117 ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
118 pathname
, strlen(pathname
) + 1,
119 sizeof(s_un
.sun_path
));
124 /* Create server socket */
125 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
130 memset(&s_un
, 0, sizeof(s_un
));
131 s_un
.sun_family
= AF_UNIX
;
132 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
133 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
135 /* Unlink the old file if present */
136 (void) unlink(pathname
);
137 ret
= bind(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
148 PERROR("close create unix sock");
155 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
158 int lttcomm_listen_unix_sock(int sock
)
162 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
171 * Receive data of size len in put that data into the buf param. Using recvmsg
174 * Return the size of received data.
177 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
184 memset(&msg
, 0, sizeof(msg
));
186 iov
[0].iov_base
= buf
;
187 iov
[0].iov_len
= len
;
192 len_last
= iov
[0].iov_len
;
193 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
195 iov
[0].iov_base
+= ret
;
196 iov
[0].iov_len
-= ret
;
197 assert(ret
<= len_last
);
199 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
202 } else if (ret
> 0) {
205 /* Else ret = 0 meaning an orderly shutdown. */
211 * Receive data of size len in put that data into the buf param. Using recvmsg
212 * API. Only use with sockets set in non-blocking mode.
214 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
215 * poll set. The poll loop will handle the EPIPE original cause.
217 * Return the size of received data.
220 ssize_t
lttcomm_recv_unix_sock_non_block(int sock
, void *buf
, size_t len
)
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.
242 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
251 /* Unexpected error */
263 * Send buf data of size len. Using sendmsg API.
265 * Return the size of sent data.
268 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
274 memset(&msg
, 0, sizeof(msg
));
276 iov
[0].iov_base
= (void *) buf
;
277 iov
[0].iov_len
= len
;
281 while (iov
[0].iov_len
) {
282 ret
= sendmsg(sock
, &msg
, 0);
284 if (errno
== EINTR
) {
288 * Only warn about EPIPE when quiet mode is
290 * We consider EPIPE as expected.
292 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
298 iov
[0].iov_len
-= ret
;
299 iov
[0].iov_base
+= ret
;
307 * Send buf data of size len. Using sendmsg API.
308 * Only use with non-blocking sockets. The difference with the blocking version
309 * of the function is that this one does not retry to send on partial sends,
310 * except if the interruption was caused by a signal (EINTR).
312 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
313 * poll set. The poll loop will handle the EPIPE original cause.
315 * Return the size of sent data.
318 ssize_t
lttcomm_send_unix_sock_non_block(int sock
, const void *buf
, size_t len
)
324 memset(&msg
, 0, sizeof(msg
));
326 iov
[0].iov_base
= (void *) buf
;
327 iov
[0].iov_len
= len
;
332 ret
= sendmsg(sock
, &msg
, 0);
334 if (errno
== EINTR
) {
338 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
340 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
343 * This can happen in non blocking mode.
350 /* Unexpected error */
361 * Shutdown cleanly a unix socket.
364 int lttcomm_close_unix_sock(int sock
)
368 /* Shutdown receptions and transmissions */
369 ret
= shutdown(sock
, SHUT_RDWR
);
374 closeret
= close(sock
);
383 * Send a message accompanied by fd(s) over a unix socket.
385 * Returns the size of data sent, or negative error value.
388 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
391 struct cmsghdr
*cmptr
;
394 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
395 char tmp
[CMSG_SPACE(sizeof_fds
)];
398 memset(&msg
, 0, sizeof(msg
));
399 memset(tmp
, 0, sizeof(tmp
));
401 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
404 msg
.msg_control
= (caddr_t
)tmp
;
405 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
407 cmptr
= CMSG_FIRSTHDR(&msg
);
412 cmptr
->cmsg_level
= SOL_SOCKET
;
413 cmptr
->cmsg_type
= SCM_RIGHTS
;
414 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
415 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
416 /* Sum of the length of all control messages in the buffer: */
417 msg
.msg_controllen
= cmptr
->cmsg_len
;
419 iov
[0].iov_base
= &dummy
;
425 ret
= sendmsg(sock
, &msg
, 0);
426 } while (ret
< 0 && errno
== EINTR
);
429 * Only warn about EPIPE when quiet mode is deactivated.
430 * We consider EPIPE as expected.
432 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
440 * Send the fd(s) of a payload view over a unix socket.
442 * Returns the size of data sent, or negative error value.
445 ssize_t
_lttcomm_send_payload_view_fds_unix_sock(int sock
,
446 struct lttng_payload_view
*view
,
451 struct lttng_dynamic_array raw_fds
;
452 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
454 lttng_dynamic_array_init(&raw_fds
, sizeof(int), NULL
);
457 ret
= -LTTNG_ERR_INVALID
;
462 * Prepare a contiguous array of file descriptors to send them.
464 * Note that the reference to each fd is released during the iteration;
465 * we're just getting the numerical value of the fds to conform to the
466 * syscall's interface. We rely on the fact that "view" must remain
467 * valid for the duration of the call and that the underlying payload
468 * owns a reference to the fd_handles.
470 for (i
= 0; i
< fd_count
; i
++) {
471 struct fd_handle
*handle
=
472 lttng_payload_view_pop_fd_handle(view
);
473 const int raw_fd
= fd_handle_get_fd(handle
);
474 const int add_ret
= lttng_dynamic_array_add_element(
477 fd_handle_put(handle
);
479 ret
= -LTTNG_ERR_NOMEM
;
485 ret
= lttcomm_send_fds_unix_sock(sock
,
486 (const int *) raw_fds
.buffer
.data
, fd_count
);
488 ret
= lttcomm_send_fds_unix_sock_non_block(sock
,
489 (const int *) raw_fds
.buffer
.data
, fd_count
);
493 lttng_dynamic_array_reset(&raw_fds
);
498 ssize_t
lttcomm_send_payload_view_fds_unix_sock(int sock
,
499 struct lttng_payload_view
*view
)
501 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, true);
505 ssize_t
lttcomm_send_payload_view_fds_unix_sock_non_block(int sock
,
506 struct lttng_payload_view
*view
)
508 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, false);
512 * Send a message accompanied by fd(s) over a unix socket.
513 * Only use for non blocking socket.
515 * Returns the size of data sent, or negative error value.
518 ssize_t
lttcomm_send_fds_unix_sock_non_block(int sock
, const int *fds
, size_t nb_fd
)
521 struct cmsghdr
*cmptr
;
524 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
525 char tmp
[CMSG_SPACE(sizeof_fds
)];
528 memset(&msg
, 0, sizeof(msg
));
529 memset(tmp
, 0, sizeof(tmp
));
531 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
534 msg
.msg_control
= (caddr_t
)tmp
;
535 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
537 cmptr
= CMSG_FIRSTHDR(&msg
);
542 cmptr
->cmsg_level
= SOL_SOCKET
;
543 cmptr
->cmsg_type
= SCM_RIGHTS
;
544 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
545 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
546 /* Sum of the length of all control messages in the buffer: */
547 msg
.msg_controllen
= cmptr
->cmsg_len
;
549 iov
[0].iov_base
= &dummy
;
555 ret
= sendmsg(sock
, &msg
, 0);
557 if (errno
== EINTR
) {
561 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
563 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
565 * This can happen in non blocking mode.
572 if (errno
== EPIPE
) {
573 /* Expected error, pass error to caller */
574 DBG3("EPIPE on sendmsg");
579 /* Unexpected error */
591 * Recv a message accompanied by fd(s) from a unix socket.
593 * Returns the size of received data, or negative error value.
595 * Expect at most "nb_fd" file descriptors. Returns the number of fd
596 * actually received in nb_fd.
599 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
603 struct cmsghdr
*cmsg
;
604 size_t sizeof_fds
= nb_fd
* sizeof(int);
607 /* Account for the struct ucred cmsg in the buffer size */
608 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
610 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
611 #endif /* __linux__ */
613 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
617 memset(&msg
, 0, sizeof(msg
));
619 /* Prepare to receive the structures */
620 iov
[0].iov_base
= &dummy
;
625 cmsg
= (struct cmsghdr
*) recv_buf
;
626 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
627 cmsg
->cmsg_level
= SOL_SOCKET
;
628 cmsg
->cmsg_type
= SCM_RIGHTS
;
630 msg
.msg_control
= cmsg
;
631 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
635 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
637 if (errno
== EINTR
) {
640 /* We consider EPIPE and EAGAIN as expected. */
641 if (!lttng_opt_quiet
&&
642 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
650 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
655 if (msg
.msg_flags
& MSG_CTRUNC
) {
656 fprintf(stderr
, "Error: Control message truncated.\n");
662 * If the socket was configured with SO_PASSCRED, the kernel will add a
663 * control message (cmsg) to the ancillary data of the unix socket. We
664 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
667 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
668 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
669 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
673 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
675 * We found the controle message for file descriptors,
676 * now copy the fds to the fds ptr and return success.
678 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
679 fprintf(stderr
, "Error: Received %zu bytes of"
680 "ancillary data for FDs, expected %zu\n",
681 (size_t) cmsg
->cmsg_len
,
682 (size_t) CMSG_LEN(sizeof_fds
));
686 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
691 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
693 * Expect credentials to be sent when expecting fds even
694 * if no credential were include in the send(). The
695 * kernel adds them...
699 #endif /* __linux__ */
706 void close_raw_fd(void *ptr
)
708 const int raw_fd
= *((const int *) ptr
);
711 const int ret
= close(raw_fd
);
714 PERROR("Failed to close file descriptor %d", raw_fd
);
720 enum lttng_error_code
add_fds_to_payload(struct lttng_dynamic_array
*raw_fds
,
721 struct lttng_payload
*payload
)
724 enum lttng_error_code ret_code
= LTTNG_OK
;
725 const int fd_count
= lttng_dynamic_array_get_count(raw_fds
);
727 for (i
= 0; i
< fd_count
; i
++) {
729 struct fd_handle
*handle
;
730 int *raw_fd
= (int *) lttng_dynamic_array_get_element(
733 handle
= fd_handle_create(*raw_fd
);
735 ret_code
= LTTNG_ERR_NOMEM
;
739 /* FD ownership transferred to the handle. */
742 ret
= lttng_payload_push_fd_handle(payload
, handle
);
743 fd_handle_put(handle
);
745 ret_code
= LTTNG_ERR_NOMEM
;
755 ssize_t
_lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
756 struct lttng_payload
*payload
, bool blocking
)
758 enum lttng_error_code add_ret
;
760 struct lttng_dynamic_array raw_fds
;
762 lttng_dynamic_array_init(&raw_fds
, sizeof(int), close_raw_fd
);
763 ret
= lttng_dynamic_array_set_count(&raw_fds
, nb_fd
);
765 ret
= -LTTNG_ERR_NOMEM
;
770 ret
= lttcomm_recv_fds_unix_sock(
771 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
773 ret
= lttcomm_recv_fds_unix_sock_non_block(
774 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
781 add_ret
= add_fds_to_payload(&raw_fds
, payload
);
782 if (add_ret
!= LTTNG_OK
) {
783 ret
= - (int) add_ret
;
788 lttng_dynamic_array_reset(&raw_fds
);
793 ssize_t
lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
794 struct lttng_payload
*payload
)
796 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, true);
800 ssize_t
lttcomm_recv_payload_fds_unix_sock_non_block(int sock
, size_t nb_fd
,
801 struct lttng_payload
*payload
)
803 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, false);
807 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
808 * Only use with non-blocking sockets.
810 * Returns the size of received data, or negative error value.
812 * Expect at most "nb_fd" file descriptors.
814 * Note that based on our comprehension, partial reception of fds is not
815 * possible since the FDs are actually in the control message. It is all or
816 * nothing, still the sender side can send the wrong number of fds.
819 ssize_t
lttcomm_recv_fds_unix_sock_non_block(int sock
, int *fds
, size_t nb_fd
)
823 struct cmsghdr
*cmsg
;
824 size_t sizeof_fds
= nb_fd
* sizeof(int);
827 /* Account for the struct ucred cmsg in the buffer size */
828 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
830 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
831 #endif /* __linux__ */
833 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
837 memset(&msg
, 0, sizeof(msg
));
839 /* Prepare to receive the structures */
840 iov
[0].iov_base
= &dummy
;
845 cmsg
= (struct cmsghdr
*) recv_buf
;
846 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
847 cmsg
->cmsg_level
= SOL_SOCKET
;
848 cmsg
->cmsg_type
= SCM_RIGHTS
;
850 msg
.msg_control
= cmsg
;
851 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
855 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
857 if (errno
== EINTR
) {
861 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
863 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
865 * This can happen in non blocking mode.
872 if (errno
== EPIPE
) {
873 /* Expected error, pass error to caller */
874 DBG3("EPIPE on recvmsg");
879 /* Unexpected error */
887 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
892 if (msg
.msg_flags
& MSG_CTRUNC
) {
893 fprintf(stderr
, "Error: Control message truncated.\n");
899 * If the socket was configured with SO_PASSCRED, the kernel will add a
900 * control message (cmsg) to the ancillary data of the unix socket. We
901 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
904 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
905 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
906 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
910 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
912 * We found the controle message for file descriptors,
913 * now copy the fds to the fds ptr and return success.
915 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
916 fprintf(stderr
, "Error: Received %zu bytes of"
917 "ancillary data for FDs, expected %zu\n",
918 (size_t) cmsg
->cmsg_len
,
919 (size_t) CMSG_LEN(sizeof_fds
));
923 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
928 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
930 * Expect credentials to be sent when expecting fds even
931 * if no credential were include in the send(). The
932 * kernel adds them...
936 #endif /* __linux__ */
943 * Send a message with credentials over a unix socket.
945 * Returns the size of data sent, or negative error value.
948 ssize_t
lttcomm_send_creds_unix_sock(int sock
, const void *buf
, size_t len
)
954 struct cmsghdr
*cmptr
;
955 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
956 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
957 lttng_sock_cred
*creds
;
959 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
960 #endif /* __linux__ */
962 memset(&msg
, 0, sizeof(msg
));
964 iov
[0].iov_base
= (void *) buf
;
965 iov
[0].iov_len
= len
;
970 msg
.msg_control
= (caddr_t
) anc_buf
;
971 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
973 cmptr
= CMSG_FIRSTHDR(&msg
);
977 cmptr
->cmsg_level
= SOL_SOCKET
;
978 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
979 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
981 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
983 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
984 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
985 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
986 #endif /* __linux__ */
989 ret
= sendmsg(sock
, &msg
, 0);
990 } while (ret
< 0 && errno
== EINTR
);
993 * Only warn about EPIPE when quiet mode is deactivated.
994 * We consider EPIPE as expected.
996 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
1004 * Recv a message accompanied with credentials from a unix socket.
1006 * Returns the size of received data, or negative error value.
1009 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
1010 lttng_sock_cred
*creds
)
1013 struct iovec iov
[1];
1017 struct cmsghdr
*cmptr
;
1018 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
1019 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
1020 #endif /* __linux__ */
1022 memset(&msg
, 0, sizeof(msg
));
1025 if (creds
== NULL
) {
1030 /* Prepare to receive the structures */
1031 iov
[0].iov_base
= buf
;
1032 iov
[0].iov_len
= len
;
1037 msg
.msg_control
= anc_buf
;
1038 msg
.msg_controllen
= sizeof(anc_buf
);
1039 #endif /* __linux__ */
1042 len_last
= iov
[0].iov_len
;
1043 ret
= recvmsg(sock
, &msg
, 0);
1045 iov
[0].iov_base
+= ret
;
1046 iov
[0].iov_len
-= ret
;
1047 assert(ret
<= len_last
);
1049 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
1051 PERROR("recvmsg fds");
1053 } else if (ret
> 0) {
1056 /* Else ret = 0 meaning an orderly shutdown. */
1059 if (msg
.msg_flags
& MSG_CTRUNC
) {
1060 fprintf(stderr
, "Error: Control message truncated.\n");
1065 cmptr
= CMSG_FIRSTHDR(&msg
);
1066 if (cmptr
== NULL
) {
1067 fprintf(stderr
, "Error: Invalid control message header\n");
1072 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
1073 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
1074 fprintf(stderr
, "Didn't received any credentials\n");
1079 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
1080 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
1081 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
1086 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
1087 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
1091 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
1092 if (peer_ret
!= 0) {
1097 #error "Please implement credential support for your OS."
1098 #endif /* __linux__ */
1105 * Set socket option to use credentials passing.
1109 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1113 /* Set socket for credentials retrieval */
1114 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
1116 PERROR("setsockopt creds unix sock");
1120 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
1122 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1127 #error "Please implement credential support for your OS."
1128 #endif /* __linux__ */