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
)
188 memset(&msg
, 0, sizeof(msg
));
190 iov
[0].iov_base
= buf
;
191 iov
[0].iov_len
= len
;
196 len_last
= iov
[0].iov_len
;
197 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
199 iov
[0].iov_base
+= ret
;
200 iov
[0].iov_len
-= ret
;
201 assert(ret
<= len_last
);
203 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
206 } else if (ret
> 0) {
209 /* Else ret = 0 meaning an orderly shutdown. */
215 * Receive data of size len in put that data into the buf param. Using recvmsg
216 * API. Only use with sockets set in non-blocking mode.
218 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
219 * poll set. The poll loop will handle the EPIPE original cause.
221 * Return the size of received data.
224 ssize_t
lttcomm_recv_unix_sock_non_block(int sock
, void *buf
, size_t len
)
234 memset(&msg
, 0, sizeof(msg
));
236 iov
[0].iov_base
= buf
;
237 iov
[0].iov_len
= len
;
242 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
244 if (errno
== EINTR
) {
248 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
250 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
259 /* Unexpected error */
271 * Send buf data of size len. Using sendmsg API.
273 * Return the size of sent data.
276 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
286 memset(&msg
, 0, sizeof(msg
));
288 iov
[0].iov_base
= (void *) buf
;
289 iov
[0].iov_len
= len
;
293 while (iov
[0].iov_len
) {
294 ret
= sendmsg(sock
, &msg
, 0);
296 if (errno
== EINTR
) {
300 * Only warn about EPIPE when quiet mode is
302 * We consider EPIPE as expected.
304 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
310 iov
[0].iov_len
-= ret
;
311 iov
[0].iov_base
+= ret
;
319 * Send buf data of size len. Using sendmsg API.
320 * Only use with non-blocking sockets. The difference with the blocking version
321 * of the function is that this one does not retry to send on partial sends,
322 * except if the interruption was caused by a signal (EINTR).
324 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
325 * poll set. The poll loop will handle the EPIPE original cause.
327 * Return the size of sent data.
330 ssize_t
lttcomm_send_unix_sock_non_block(int sock
, const void *buf
, size_t len
)
340 memset(&msg
, 0, sizeof(msg
));
342 iov
[0].iov_base
= (void *) buf
;
343 iov
[0].iov_len
= len
;
348 ret
= sendmsg(sock
, &msg
, 0);
350 if (errno
== EINTR
) {
354 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
356 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
359 * This can happen in non blocking mode.
366 /* Unexpected error */
377 * Shutdown cleanly a unix socket.
380 int lttcomm_close_unix_sock(int sock
)
384 /* Shutdown receptions and transmissions */
385 ret
= shutdown(sock
, SHUT_RDWR
);
390 closeret
= close(sock
);
399 * Send a message accompanied by fd(s) over a unix socket.
401 * Returns the size of data sent, or negative error value.
404 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
407 struct cmsghdr
*cmptr
;
410 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
411 char tmp
[CMSG_SPACE(sizeof_fds
)];
418 memset(&msg
, 0, sizeof(msg
));
419 memset(tmp
, 0, sizeof(tmp
));
421 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
424 msg
.msg_control
= (caddr_t
)tmp
;
425 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
427 cmptr
= CMSG_FIRSTHDR(&msg
);
432 cmptr
->cmsg_level
= SOL_SOCKET
;
433 cmptr
->cmsg_type
= SCM_RIGHTS
;
434 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
435 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
436 /* Sum of the length of all control messages in the buffer: */
437 msg
.msg_controllen
= cmptr
->cmsg_len
;
439 iov
[0].iov_base
= &dummy
;
445 ret
= sendmsg(sock
, &msg
, 0);
446 } while (ret
< 0 && errno
== EINTR
);
449 * Only warn about EPIPE when quiet mode is deactivated.
450 * We consider EPIPE as expected.
452 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
460 * Send the fd(s) of a payload view over a unix socket.
462 * Returns the size of data sent, or negative error value.
465 ssize_t
_lttcomm_send_payload_view_fds_unix_sock(int sock
,
466 struct lttng_payload_view
*view
,
471 struct lttng_dynamic_array raw_fds
;
472 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
474 lttng_dynamic_array_init(&raw_fds
, sizeof(int), NULL
);
477 ret
= -LTTNG_ERR_INVALID
;
482 * Prepare a contiguous array of file descriptors to send them.
484 * Note that the reference to each fd is released during the iteration;
485 * we're just getting the numerical value of the fds to conform to the
486 * syscall's interface. We rely on the fact that "view" must remain
487 * valid for the duration of the call and that the underlying payload
488 * owns a reference to the fd_handles.
490 for (i
= 0; i
< fd_count
; i
++) {
491 struct fd_handle
*handle
=
492 lttng_payload_view_pop_fd_handle(view
);
493 const int raw_fd
= fd_handle_get_fd(handle
);
494 const int add_ret
= lttng_dynamic_array_add_element(
497 fd_handle_put(handle
);
499 ret
= -LTTNG_ERR_NOMEM
;
505 ret
= lttcomm_send_fds_unix_sock(sock
,
506 (const int *) raw_fds
.buffer
.data
, fd_count
);
508 ret
= lttcomm_send_fds_unix_sock_non_block(sock
,
509 (const int *) raw_fds
.buffer
.data
, fd_count
);
513 lttng_dynamic_array_reset(&raw_fds
);
518 ssize_t
lttcomm_send_payload_view_fds_unix_sock(int sock
,
519 struct lttng_payload_view
*view
)
521 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, true);
525 ssize_t
lttcomm_send_payload_view_fds_unix_sock_non_block(int sock
,
526 struct lttng_payload_view
*view
)
528 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, false);
532 * Send a message accompanied by fd(s) over a unix socket.
533 * Only use for non blocking socket.
535 * 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
)];
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.
587 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
589 * This can happen in non blocking mode.
596 if (errno
== EPIPE
) {
597 /* Expected error, pass error to caller */
598 DBG3("EPIPE on sendmsg");
603 /* Unexpected error */
615 * Recv a message accompanied by fd(s) from a unix socket.
617 * Returns the size of received data, or negative error value.
619 * Expect at most "nb_fd" file descriptors. Returns the number of fd
620 * actually received in nb_fd.
623 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
627 struct cmsghdr
*cmsg
;
628 size_t sizeof_fds
= nb_fd
* sizeof(int);
631 /* Account for the struct ucred cmsg in the buffer size */
632 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
634 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
635 #endif /* __linux__ */
637 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
645 memset(&msg
, 0, sizeof(msg
));
647 /* Prepare to receive the structures */
648 iov
[0].iov_base
= &dummy
;
653 cmsg
= (struct cmsghdr
*) recv_buf
;
654 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
655 cmsg
->cmsg_level
= SOL_SOCKET
;
656 cmsg
->cmsg_type
= SCM_RIGHTS
;
658 msg
.msg_control
= cmsg
;
659 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
663 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
665 if (errno
== EINTR
) {
668 /* We consider EPIPE and EAGAIN as expected. */
669 if (!lttng_opt_quiet
&&
670 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
678 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
683 if (msg
.msg_flags
& MSG_CTRUNC
) {
684 fprintf(stderr
, "Error: Control message truncated.\n");
690 * If the socket was configured with SO_PASSCRED, the kernel will add a
691 * control message (cmsg) to the ancillary data of the unix socket. We
692 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
695 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
696 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
697 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
701 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
703 * We found the controle message for file descriptors,
704 * now copy the fds to the fds ptr and return success.
706 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
707 fprintf(stderr
, "Error: Received %zu bytes of"
708 "ancillary data for FDs, expected %zu\n",
709 (size_t) cmsg
->cmsg_len
,
710 (size_t) CMSG_LEN(sizeof_fds
));
714 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
719 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
721 * Expect credentials to be sent when expecting fds even
722 * if no credential were include in the send(). The
723 * kernel adds them...
727 #endif /* __linux__ */
734 void close_raw_fd(void *ptr
)
736 const int raw_fd
= *((const int *) ptr
);
739 const int ret
= close(raw_fd
);
742 PERROR("Failed to close file descriptor %d", raw_fd
);
748 enum lttng_error_code
add_fds_to_payload(struct lttng_dynamic_array
*raw_fds
,
749 struct lttng_payload
*payload
)
752 enum lttng_error_code ret_code
= LTTNG_OK
;
753 const int fd_count
= lttng_dynamic_array_get_count(raw_fds
);
755 for (i
= 0; i
< fd_count
; i
++) {
757 struct fd_handle
*handle
;
758 int *raw_fd
= (int *) lttng_dynamic_array_get_element(
761 assert(*raw_fd
!= -1);
763 handle
= fd_handle_create(*raw_fd
);
765 ret_code
= LTTNG_ERR_NOMEM
;
769 /* FD ownership transferred to the handle. */
772 ret
= lttng_payload_push_fd_handle(payload
, handle
);
773 fd_handle_put(handle
);
775 ret_code
= LTTNG_ERR_NOMEM
;
785 ssize_t
_lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
786 struct lttng_payload
*payload
, bool blocking
)
789 enum lttng_error_code add_ret
;
791 int default_value
= -1;
792 struct lttng_dynamic_array raw_fds
;
798 lttng_dynamic_array_init(&raw_fds
, sizeof(int), close_raw_fd
);
800 for (i
= 0; i
< nb_fd
; i
++) {
801 if (lttng_dynamic_array_add_element(&raw_fds
, &default_value
)) {
802 ret
= -LTTNG_ERR_NOMEM
;
808 ret
= lttcomm_recv_fds_unix_sock(
809 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
811 ret
= lttcomm_recv_fds_unix_sock_non_block(
812 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
819 add_ret
= add_fds_to_payload(&raw_fds
, payload
);
820 if (add_ret
!= LTTNG_OK
) {
821 ret
= - (int) add_ret
;
826 lttng_dynamic_array_reset(&raw_fds
);
831 ssize_t
lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
832 struct lttng_payload
*payload
)
834 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.
857 ssize_t
lttcomm_recv_fds_unix_sock_non_block(int sock
, int *fds
, size_t nb_fd
)
861 struct cmsghdr
*cmsg
;
862 size_t sizeof_fds
= nb_fd
* sizeof(int);
869 /* Account for the struct ucred cmsg in the buffer size */
870 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
872 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
873 #endif /* __linux__ */
875 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
879 memset(&msg
, 0, sizeof(msg
));
881 /* Prepare to receive the structures */
882 iov
[0].iov_base
= &dummy
;
887 cmsg
= (struct cmsghdr
*) recv_buf
;
888 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
889 cmsg
->cmsg_level
= SOL_SOCKET
;
890 cmsg
->cmsg_type
= SCM_RIGHTS
;
892 msg
.msg_control
= cmsg
;
893 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
897 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
899 if (errno
== EINTR
) {
903 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
905 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
907 * This can happen in non blocking mode.
914 if (errno
== EPIPE
) {
915 /* Expected error, pass error to caller */
916 DBG3("EPIPE on recvmsg");
921 /* Unexpected error */
929 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
934 if (msg
.msg_flags
& MSG_CTRUNC
) {
935 fprintf(stderr
, "Error: Control message truncated.\n");
941 * If the socket was configured with SO_PASSCRED, the kernel will add a
942 * control message (cmsg) to the ancillary data of the unix socket. We
943 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
946 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
947 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
948 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
952 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
954 * We found the controle message for file descriptors,
955 * now copy the fds to the fds ptr and return success.
957 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
958 fprintf(stderr
, "Error: Received %zu bytes of"
959 "ancillary data for FDs, expected %zu\n",
960 (size_t) cmsg
->cmsg_len
,
961 (size_t) CMSG_LEN(sizeof_fds
));
965 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
970 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
972 * Expect credentials to be sent when expecting fds even
973 * if no credential were include in the send(). The
974 * kernel adds them...
978 #endif /* __linux__ */
985 * Send a message with credentials over a unix socket.
987 * Returns the size of data sent, or negative error value.
990 ssize_t
lttcomm_send_creds_unix_sock(int sock
, const void *buf
, size_t len
)
995 #if defined(__linux__) || defined(__CYGWIN__)
996 struct cmsghdr
*cmptr
;
997 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
998 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
999 lttng_sock_cred
*creds
;
1001 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
1002 #endif /* __linux__, __CYGWIN__ */
1004 memset(&msg
, 0, sizeof(msg
));
1010 iov
[0].iov_base
= (void *) buf
;
1011 iov
[0].iov_len
= len
;
1015 #if defined(__linux__) || defined(__CYGWIN__)
1016 msg
.msg_control
= (caddr_t
) anc_buf
;
1017 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
1019 cmptr
= CMSG_FIRSTHDR(&msg
);
1023 cmptr
->cmsg_level
= SOL_SOCKET
;
1024 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
1025 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
1027 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
1029 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
1030 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
1031 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
1032 #endif /* __linux__, __CYGWIN__ */
1035 ret
= sendmsg(sock
, &msg
, 0);
1036 } while (ret
< 0 && errno
== EINTR
);
1039 * Only warn about EPIPE when quiet mode is deactivated.
1040 * We consider EPIPE as expected.
1042 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
1050 * Recv a message accompanied with credentials from a unix socket.
1052 * 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__ */
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
+= ret
;
1091 iov
[0].iov_len
-= ret
;
1092 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__)
1151 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1155 /* Set socket for credentials retrieval */
1156 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
1158 PERROR("setsockopt creds unix sock");
1162 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1164 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1169 #error "Please implement credential support for your OS."
1170 #endif /* __linux__ */