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>
20 #include <common/common.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 * Prepare a contiguous array of file descriptors to send them.
459 * Note that the reference to each fd is released during the iteration;
460 * we're just getting the numerical value of the fds to conform to the
461 * syscall's interface. We rely on the fact that "view" must remain
462 * valid for the duration of the call and that the underlying payload
463 * owns a reference to the fd_handles.
465 for (i
= 0; i
< fd_count
; i
++) {
466 struct fd_handle
*handle
=
467 lttng_payload_view_pop_fd_handle(view
);
468 const int raw_fd
= fd_handle_get_fd(handle
);
469 const int add_ret
= lttng_dynamic_array_add_element(
472 fd_handle_put(handle
);
474 ret
= -LTTNG_ERR_NOMEM
;
480 ret
= lttcomm_send_fds_unix_sock(sock
,
481 (const int *) raw_fds
.buffer
.data
, fd_count
);
483 ret
= lttcomm_send_fds_unix_sock_non_block(sock
,
484 (const int *) raw_fds
.buffer
.data
, fd_count
);
488 lttng_dynamic_array_reset(&raw_fds
);
493 ssize_t
lttcomm_send_payload_view_fds_unix_sock(int sock
,
494 struct lttng_payload_view
*view
)
496 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, true);
500 ssize_t
lttcomm_send_payload_view_fds_unix_sock_non_block(int sock
,
501 struct lttng_payload_view
*view
)
503 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, false);
507 * Send a message accompanied by fd(s) over a unix socket.
508 * Only use for non blocking socket.
510 * Returns the size of data sent, or negative error value.
513 ssize_t
lttcomm_send_fds_unix_sock_non_block(int sock
, const int *fds
, size_t nb_fd
)
516 struct cmsghdr
*cmptr
;
519 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
520 char tmp
[CMSG_SPACE(sizeof_fds
)];
523 memset(&msg
, 0, sizeof(msg
));
524 memset(tmp
, 0, sizeof(tmp
));
526 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
529 msg
.msg_control
= (caddr_t
)tmp
;
530 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
532 cmptr
= CMSG_FIRSTHDR(&msg
);
537 cmptr
->cmsg_level
= SOL_SOCKET
;
538 cmptr
->cmsg_type
= SCM_RIGHTS
;
539 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
540 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
541 /* Sum of the length of all control messages in the buffer: */
542 msg
.msg_controllen
= cmptr
->cmsg_len
;
544 iov
[0].iov_base
= &dummy
;
550 ret
= sendmsg(sock
, &msg
, 0);
552 if (errno
== EINTR
) {
556 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
558 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
560 * This can happen in non blocking mode.
567 if (errno
== EPIPE
) {
568 /* Expected error, pass error to caller */
569 DBG3("EPIPE on sendmsg");
574 /* Unexpected error */
586 * Recv a message accompanied by fd(s) from a unix socket.
588 * Returns the size of received data, or negative error value.
590 * Expect at most "nb_fd" file descriptors. Returns the number of fd
591 * actually received in nb_fd.
594 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
598 struct cmsghdr
*cmsg
;
599 size_t sizeof_fds
= nb_fd
* sizeof(int);
602 /* Account for the struct ucred cmsg in the buffer size */
603 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
605 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
606 #endif /* __linux__ */
608 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
612 memset(&msg
, 0, sizeof(msg
));
614 /* Prepare to receive the structures */
615 iov
[0].iov_base
= &dummy
;
620 cmsg
= (struct cmsghdr
*) recv_buf
;
621 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
622 cmsg
->cmsg_level
= SOL_SOCKET
;
623 cmsg
->cmsg_type
= SCM_RIGHTS
;
625 msg
.msg_control
= cmsg
;
626 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
630 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
632 if (errno
== EINTR
) {
635 /* We consider EPIPE and EAGAIN as expected. */
636 if (!lttng_opt_quiet
&&
637 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
645 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
650 if (msg
.msg_flags
& MSG_CTRUNC
) {
651 fprintf(stderr
, "Error: Control message truncated.\n");
657 * If the socket was configured with SO_PASSCRED, the kernel will add a
658 * control message (cmsg) to the ancillary data of the unix socket. We
659 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
662 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
663 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
664 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
668 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
670 * We found the controle message for file descriptors,
671 * now copy the fds to the fds ptr and return success.
673 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
674 fprintf(stderr
, "Error: Received %zu bytes of"
675 "ancillary data for FDs, expected %zu\n",
676 (size_t) cmsg
->cmsg_len
,
677 (size_t) CMSG_LEN(sizeof_fds
));
681 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
686 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
688 * Expect credentials to be sent when expecting fds even
689 * if no credential were include in the send(). The
690 * kernel adds them...
694 #endif /* __linux__ */
701 void close_raw_fd(void *ptr
)
703 const int raw_fd
= *((const int *) ptr
);
706 const int ret
= close(raw_fd
);
709 PERROR("Failed to close file descriptor %d", raw_fd
);
715 enum lttng_error_code
add_fds_to_payload(struct lttng_dynamic_array
*raw_fds
,
716 struct lttng_payload
*payload
)
719 enum lttng_error_code ret_code
= LTTNG_OK
;
720 const int fd_count
= lttng_dynamic_array_get_count(raw_fds
);
722 for (i
= 0; i
< fd_count
; i
++) {
724 struct fd_handle
*handle
;
725 int *raw_fd
= (int *) lttng_dynamic_array_get_element(
728 handle
= fd_handle_create(*raw_fd
);
730 ret_code
= LTTNG_ERR_NOMEM
;
734 /* FD ownership transferred to the handle. */
737 ret
= lttng_payload_push_fd_handle(payload
, handle
);
738 fd_handle_put(handle
);
740 ret_code
= LTTNG_ERR_NOMEM
;
750 ssize_t
_lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
751 struct lttng_payload
*payload
, bool blocking
)
753 enum lttng_error_code add_ret
;
755 struct lttng_dynamic_array raw_fds
;
757 lttng_dynamic_array_init(&raw_fds
, sizeof(int), close_raw_fd
);
758 ret
= lttng_dynamic_array_set_count(&raw_fds
, nb_fd
);
760 ret
= -LTTNG_ERR_NOMEM
;
765 ret
= lttcomm_recv_fds_unix_sock(
766 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
768 ret
= lttcomm_recv_fds_unix_sock_non_block(
769 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
776 add_ret
= add_fds_to_payload(&raw_fds
, payload
);
777 if (add_ret
!= LTTNG_OK
) {
778 ret
= - (int) add_ret
;
783 lttng_dynamic_array_reset(&raw_fds
);
788 ssize_t
lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
789 struct lttng_payload
*payload
)
791 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, true);
795 ssize_t
lttcomm_recv_payload_fds_unix_sock_non_block(int sock
, size_t nb_fd
,
796 struct lttng_payload
*payload
)
798 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, false);
802 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
803 * Only use with non-blocking sockets.
805 * Returns the size of received data, or negative error value.
807 * Expect at most "nb_fd" file descriptors.
809 * Note that based on our comprehension, partial reception of fds is not
810 * possible since the FDs are actually in the control message. It is all or
811 * nothing, still the sender side can send the wrong number of fds.
814 ssize_t
lttcomm_recv_fds_unix_sock_non_block(int sock
, int *fds
, size_t nb_fd
)
818 struct cmsghdr
*cmsg
;
819 size_t sizeof_fds
= nb_fd
* sizeof(int);
822 /* Account for the struct ucred cmsg in the buffer size */
823 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
825 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
826 #endif /* __linux__ */
828 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
832 memset(&msg
, 0, sizeof(msg
));
834 /* Prepare to receive the structures */
835 iov
[0].iov_base
= &dummy
;
840 cmsg
= (struct cmsghdr
*) recv_buf
;
841 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
842 cmsg
->cmsg_level
= SOL_SOCKET
;
843 cmsg
->cmsg_type
= SCM_RIGHTS
;
845 msg
.msg_control
= cmsg
;
846 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
850 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
852 if (errno
== EINTR
) {
856 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
858 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
860 * This can happen in non blocking mode.
867 if (errno
== EPIPE
) {
868 /* Expected error, pass error to caller */
869 DBG3("EPIPE on recvmsg");
874 /* Unexpected error */
882 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
887 if (msg
.msg_flags
& MSG_CTRUNC
) {
888 fprintf(stderr
, "Error: Control message truncated.\n");
894 * If the socket was configured with SO_PASSCRED, the kernel will add a
895 * control message (cmsg) to the ancillary data of the unix socket. We
896 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
899 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
900 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
901 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
905 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
907 * We found the controle message for file descriptors,
908 * now copy the fds to the fds ptr and return success.
910 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
911 fprintf(stderr
, "Error: Received %zu bytes of"
912 "ancillary data for FDs, expected %zu\n",
913 (size_t) cmsg
->cmsg_len
,
914 (size_t) CMSG_LEN(sizeof_fds
));
918 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
923 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
925 * Expect credentials to be sent when expecting fds even
926 * if no credential were include in the send(). The
927 * kernel adds them...
931 #endif /* __linux__ */
938 * Send a message with credentials over a unix socket.
940 * Returns the size of data sent, or negative error value.
943 ssize_t
lttcomm_send_creds_unix_sock(int sock
, const void *buf
, size_t len
)
949 struct cmsghdr
*cmptr
;
950 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
951 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
952 lttng_sock_cred
*creds
;
954 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
955 #endif /* __linux__ */
957 memset(&msg
, 0, sizeof(msg
));
959 iov
[0].iov_base
= (void *) buf
;
960 iov
[0].iov_len
= len
;
965 msg
.msg_control
= (caddr_t
) anc_buf
;
966 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
968 cmptr
= CMSG_FIRSTHDR(&msg
);
972 cmptr
->cmsg_level
= SOL_SOCKET
;
973 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
974 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
976 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
978 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
979 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
980 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
981 #endif /* __linux__ */
984 ret
= sendmsg(sock
, &msg
, 0);
985 } while (ret
< 0 && errno
== EINTR
);
988 * Only warn about EPIPE when quiet mode is deactivated.
989 * We consider EPIPE as expected.
991 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
999 * Recv a message accompanied with credentials from a unix socket.
1001 * Returns the size of received data, or negative error value.
1004 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
1005 lttng_sock_cred
*creds
)
1008 struct iovec iov
[1];
1012 struct cmsghdr
*cmptr
;
1013 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
1014 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
1015 #endif /* __linux__ */
1017 memset(&msg
, 0, sizeof(msg
));
1020 if (creds
== NULL
) {
1025 /* Prepare to receive the structures */
1026 iov
[0].iov_base
= buf
;
1027 iov
[0].iov_len
= len
;
1032 msg
.msg_control
= anc_buf
;
1033 msg
.msg_controllen
= sizeof(anc_buf
);
1034 #endif /* __linux__ */
1037 len_last
= iov
[0].iov_len
;
1038 ret
= recvmsg(sock
, &msg
, 0);
1040 iov
[0].iov_base
+= ret
;
1041 iov
[0].iov_len
-= ret
;
1042 assert(ret
<= len_last
);
1044 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
1046 PERROR("recvmsg fds");
1048 } else if (ret
> 0) {
1051 /* Else ret = 0 meaning an orderly shutdown. */
1054 if (msg
.msg_flags
& MSG_CTRUNC
) {
1055 fprintf(stderr
, "Error: Control message truncated.\n");
1060 cmptr
= CMSG_FIRSTHDR(&msg
);
1061 if (cmptr
== NULL
) {
1062 fprintf(stderr
, "Error: Invalid control message header\n");
1067 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
1068 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
1069 fprintf(stderr
, "Didn't received any credentials\n");
1074 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
1075 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
1076 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
1081 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
1082 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
1086 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
1087 if (peer_ret
!= 0) {
1092 #error "Please implement credential support for your OS."
1093 #endif /* __linux__ */
1100 * Set socket option to use credentials passing.
1104 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1108 /* Set socket for credentials retrieval */
1109 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
1111 PERROR("setsockopt creds unix sock");
1115 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
1117 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1122 #error "Please implement credential support for your OS."
1123 #endif /* __linux__ */