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
15 #include <sys/types.h>
18 #include <common/common.h>
19 #include <common/compat/errno.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/fd-handle.h>
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.
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.
267 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
275 LTTNG_ASSERT(len
> 0);
277 memset(&msg
, 0, sizeof(msg
));
279 iov
[0].iov_base
= (void *) buf
;
280 iov
[0].iov_len
= len
;
284 while (iov
[0].iov_len
) {
285 ret
= sendmsg(sock
, &msg
, 0);
287 if (errno
== EINTR
) {
291 * Only warn about EPIPE when quiet mode is
293 * We consider EPIPE as expected.
295 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
301 iov
[0].iov_len
-= ret
;
302 iov
[0].iov_base
= (char *) iov
[0].iov_base
+ ret
;
310 * Send buf data of size len. Using sendmsg API.
311 * Only use with non-blocking sockets. The difference with the blocking version
312 * of the function is that this one does not retry to send on partial sends,
313 * except if the interruption was caused by a signal (EINTR).
315 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
316 * poll set. The poll loop will handle the EPIPE original cause.
318 * Return the size of sent data.
320 ssize_t
lttcomm_send_unix_sock_non_block(int sock
, const void *buf
, size_t len
)
328 LTTNG_ASSERT(len
> 0);
330 memset(&msg
, 0, sizeof(msg
));
332 iov
[0].iov_base
= (void *) buf
;
333 iov
[0].iov_len
= len
;
338 ret
= sendmsg(sock
, &msg
, 0);
340 if (errno
== EINTR
) {
344 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
346 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
349 * This can happen in non blocking mode.
356 /* Unexpected error */
367 * Shutdown cleanly a unix socket.
369 int lttcomm_close_unix_sock(int sock
)
373 /* Shutdown receptions and transmissions */
374 ret
= shutdown(sock
, SHUT_RDWR
);
379 closeret
= close(sock
);
388 * Send a message accompanied by fd(s) over a unix socket.
390 * Returns the size of data sent, or negative error value.
392 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
395 struct cmsghdr
*cmptr
;
398 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
399 char tmp
[CMSG_SPACE(sizeof_fds
)];
404 LTTNG_ASSERT(nb_fd
> 0);
406 memset(&msg
, 0, sizeof(msg
));
407 memset(tmp
, 0, sizeof(tmp
));
409 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
412 msg
.msg_control
= (caddr_t
)tmp
;
413 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
415 cmptr
= CMSG_FIRSTHDR(&msg
);
420 cmptr
->cmsg_level
= SOL_SOCKET
;
421 cmptr
->cmsg_type
= SCM_RIGHTS
;
422 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
423 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
424 /* Sum of the length of all control messages in the buffer: */
425 msg
.msg_controllen
= cmptr
->cmsg_len
;
427 iov
[0].iov_base
= &dummy
;
433 ret
= sendmsg(sock
, &msg
, 0);
434 } while (ret
< 0 && errno
== EINTR
);
437 * Only warn about EPIPE when quiet mode is deactivated.
438 * We consider EPIPE as expected.
440 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
448 * Send the fd(s) of a payload view over a unix socket.
450 * Returns the size of data sent, or negative error value.
453 ssize_t
_lttcomm_send_payload_view_fds_unix_sock(int sock
,
454 struct lttng_payload_view
*view
,
459 struct lttng_dynamic_array raw_fds
;
460 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
462 lttng_dynamic_array_init(&raw_fds
, sizeof(int), NULL
);
465 ret
= -LTTNG_ERR_INVALID
;
470 * Prepare a contiguous array of file descriptors to send them.
472 * Note that the reference to each fd is released during the iteration;
473 * we're just getting the numerical value of the fds to conform to the
474 * syscall's interface. We rely on the fact that "view" must remain
475 * valid for the duration of the call and that the underlying payload
476 * owns a reference to the fd_handles.
478 for (i
= 0; i
< fd_count
; i
++) {
479 struct fd_handle
*handle
=
480 lttng_payload_view_pop_fd_handle(view
);
481 const int raw_fd
= fd_handle_get_fd(handle
);
482 const int add_ret
= lttng_dynamic_array_add_element(
485 fd_handle_put(handle
);
487 ret
= -LTTNG_ERR_NOMEM
;
493 ret
= lttcomm_send_fds_unix_sock(sock
,
494 (const int *) raw_fds
.buffer
.data
, fd_count
);
496 ret
= lttcomm_send_fds_unix_sock_non_block(sock
,
497 (const int *) raw_fds
.buffer
.data
, fd_count
);
501 lttng_dynamic_array_reset(&raw_fds
);
505 ssize_t
lttcomm_send_payload_view_fds_unix_sock(int sock
,
506 struct lttng_payload_view
*view
)
508 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, true);
511 ssize_t
lttcomm_send_payload_view_fds_unix_sock_non_block(int sock
,
512 struct lttng_payload_view
*view
)
514 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, false);
518 * Send a message accompanied by fd(s) over a unix socket.
519 * Only use for non blocking socket.
521 * Returns the size of data sent, or negative error value.
523 ssize_t
lttcomm_send_fds_unix_sock_non_block(int sock
, const int *fds
, size_t nb_fd
)
526 struct cmsghdr
*cmptr
;
529 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
530 char tmp
[CMSG_SPACE(sizeof_fds
)];
535 LTTNG_ASSERT(nb_fd
> 0);
537 memset(&msg
, 0, sizeof(msg
));
538 memset(tmp
, 0, sizeof(tmp
));
540 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
543 msg
.msg_control
= (caddr_t
)tmp
;
544 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
546 cmptr
= CMSG_FIRSTHDR(&msg
);
551 cmptr
->cmsg_level
= SOL_SOCKET
;
552 cmptr
->cmsg_type
= SCM_RIGHTS
;
553 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
554 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
555 /* Sum of the length of all control messages in the buffer: */
556 msg
.msg_controllen
= cmptr
->cmsg_len
;
558 iov
[0].iov_base
= &dummy
;
564 ret
= sendmsg(sock
, &msg
, 0);
566 if (errno
== EINTR
) {
570 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
572 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
574 * This can happen in non blocking mode.
581 if (errno
== EPIPE
) {
582 /* Expected error, pass error to caller */
583 DBG3("EPIPE on sendmsg");
588 /* Unexpected error */
600 * Recv a message accompanied by fd(s) from a unix socket.
602 * Returns the size of received data, or negative error value.
604 * Expect at most "nb_fd" file descriptors. Returns the number of fd
605 * actually received in nb_fd.
607 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
611 struct cmsghdr
*cmsg
;
612 size_t sizeof_fds
= nb_fd
* sizeof(int);
615 /* Account for the struct ucred cmsg in the buffer size */
616 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
618 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
619 #endif /* __linux__ */
621 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
627 LTTNG_ASSERT(nb_fd
> 0);
629 memset(&msg
, 0, sizeof(msg
));
631 /* Prepare to receive the structures */
632 iov
[0].iov_base
= &dummy
;
637 cmsg
= (struct cmsghdr
*) recv_buf
;
638 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
639 cmsg
->cmsg_level
= SOL_SOCKET
;
640 cmsg
->cmsg_type
= SCM_RIGHTS
;
642 msg
.msg_control
= cmsg
;
643 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
647 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
649 if (errno
== EINTR
) {
652 /* We consider EPIPE and EAGAIN as expected. */
653 if (!lttng_opt_quiet
&&
654 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
662 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
667 if (msg
.msg_flags
& MSG_CTRUNC
) {
668 fprintf(stderr
, "Error: Control message truncated.\n");
674 * If the socket was configured with SO_PASSCRED, the kernel will add a
675 * control message (cmsg) to the ancillary data of the unix socket. We
676 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
679 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
680 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
681 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
685 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
687 * We found the controle message for file descriptors,
688 * now copy the fds to the fds ptr and return success.
690 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
691 fprintf(stderr
, "Error: Received %zu bytes of"
692 "ancillary data for FDs, expected %zu\n",
693 (size_t) cmsg
->cmsg_len
,
694 (size_t) CMSG_LEN(sizeof_fds
));
698 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
703 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
705 * Expect credentials to be sent when expecting fds even
706 * if no credential were include in the send(). The
707 * kernel adds them...
711 #endif /* __linux__ */
718 void close_raw_fd(void *ptr
)
720 const int raw_fd
= *((const int *) ptr
);
723 const int ret
= close(raw_fd
);
726 PERROR("Failed to close file descriptor %d", raw_fd
);
732 enum lttng_error_code
add_fds_to_payload(struct lttng_dynamic_array
*raw_fds
,
733 struct lttng_payload
*payload
)
736 enum lttng_error_code ret_code
= LTTNG_OK
;
737 const int fd_count
= lttng_dynamic_array_get_count(raw_fds
);
739 for (i
= 0; i
< fd_count
; i
++) {
741 struct fd_handle
*handle
;
742 int *raw_fd
= (int *) lttng_dynamic_array_get_element(
745 LTTNG_ASSERT(*raw_fd
!= -1);
747 handle
= fd_handle_create(*raw_fd
);
749 ret_code
= LTTNG_ERR_NOMEM
;
753 /* FD ownership transferred to the handle. */
756 ret
= lttng_payload_push_fd_handle(payload
, handle
);
757 fd_handle_put(handle
);
759 ret_code
= LTTNG_ERR_NOMEM
;
769 ssize_t
_lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
770 struct lttng_payload
*payload
, bool blocking
)
773 enum lttng_error_code add_ret
;
775 int default_value
= -1;
776 struct lttng_dynamic_array raw_fds
;
779 LTTNG_ASSERT(payload
);
780 LTTNG_ASSERT(nb_fd
> 0);
782 lttng_dynamic_array_init(&raw_fds
, sizeof(int), close_raw_fd
);
784 for (i
= 0; i
< nb_fd
; i
++) {
785 if (lttng_dynamic_array_add_element(&raw_fds
, &default_value
)) {
786 ret
= -LTTNG_ERR_NOMEM
;
792 ret
= lttcomm_recv_fds_unix_sock(
793 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
795 ret
= lttcomm_recv_fds_unix_sock_non_block(
796 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
803 add_ret
= add_fds_to_payload(&raw_fds
, payload
);
804 if (add_ret
!= LTTNG_OK
) {
805 ret
= - (int) add_ret
;
810 lttng_dynamic_array_reset(&raw_fds
);
814 ssize_t
lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
815 struct lttng_payload
*payload
)
817 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, true);
820 ssize_t
lttcomm_recv_payload_fds_unix_sock_non_block(int sock
, size_t nb_fd
,
821 struct lttng_payload
*payload
)
823 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, false);
827 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
828 * Only use with non-blocking sockets.
830 * Returns the size of received data, or negative error value.
832 * Expect at most "nb_fd" file descriptors.
834 * Note that based on our comprehension, partial reception of fds is not
835 * possible since the FDs are actually in the control message. It is all or
836 * nothing, still the sender side can send the wrong number of fds.
838 ssize_t
lttcomm_recv_fds_unix_sock_non_block(int sock
, int *fds
, size_t nb_fd
)
842 struct cmsghdr
*cmsg
;
843 size_t sizeof_fds
= nb_fd
* sizeof(int);
847 LTTNG_ASSERT(nb_fd
> 0);
850 /* Account for the struct ucred cmsg in the buffer size */
851 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
853 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
854 #endif /* __linux__ */
856 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
860 memset(&msg
, 0, sizeof(msg
));
862 /* Prepare to receive the structures */
863 iov
[0].iov_base
= &dummy
;
868 cmsg
= (struct cmsghdr
*) recv_buf
;
869 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
870 cmsg
->cmsg_level
= SOL_SOCKET
;
871 cmsg
->cmsg_type
= SCM_RIGHTS
;
873 msg
.msg_control
= cmsg
;
874 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
878 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
880 if (errno
== EINTR
) {
884 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
886 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
888 * This can happen in non blocking mode.
895 if (errno
== EPIPE
) {
896 /* Expected error, pass error to caller */
897 DBG3("EPIPE on recvmsg");
902 /* Unexpected error */
910 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
915 if (msg
.msg_flags
& MSG_CTRUNC
) {
916 fprintf(stderr
, "Error: Control message truncated.\n");
922 * If the socket was configured with SO_PASSCRED, the kernel will add a
923 * control message (cmsg) to the ancillary data of the unix socket. We
924 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
927 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
928 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
929 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
933 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
935 * We found the controle message for file descriptors,
936 * now copy the fds to the fds ptr and return success.
938 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
939 fprintf(stderr
, "Error: Received %zu bytes of"
940 "ancillary data for FDs, expected %zu\n",
941 (size_t) cmsg
->cmsg_len
,
942 (size_t) CMSG_LEN(sizeof_fds
));
946 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
951 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
953 * Expect credentials to be sent when expecting fds even
954 * if no credential were include in the send(). The
955 * kernel adds them...
959 #endif /* __linux__ */
966 * Send a message with credentials over a unix socket.
968 * Returns the size of data sent, or negative error value.
970 ssize_t
lttcomm_send_creds_unix_sock(int sock
, const void *buf
, size_t len
)
975 #if defined(__linux__) || defined(__CYGWIN__)
976 struct cmsghdr
*cmptr
;
977 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
978 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
979 lttng_sock_cred
*creds
;
981 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
982 #endif /* __linux__, __CYGWIN__ */
984 memset(&msg
, 0, sizeof(msg
));
988 LTTNG_ASSERT(len
> 0);
990 iov
[0].iov_base
= (void *) buf
;
991 iov
[0].iov_len
= len
;
995 #if defined(__linux__) || defined(__CYGWIN__)
996 msg
.msg_control
= (caddr_t
) anc_buf
;
997 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
999 cmptr
= CMSG_FIRSTHDR(&msg
);
1003 cmptr
->cmsg_level
= SOL_SOCKET
;
1004 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
1005 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
1007 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
1009 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
1010 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
1011 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
1012 #endif /* __linux__, __CYGWIN__ */
1015 ret
= sendmsg(sock
, &msg
, 0);
1016 } while (ret
< 0 && errno
== EINTR
);
1019 * Only warn about EPIPE when quiet mode is deactivated.
1020 * We consider EPIPE as expected.
1022 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
1030 * Recv a message accompanied with credentials from a unix socket.
1032 * Returns the size of received data, or negative error value.
1034 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
1035 lttng_sock_cred
*creds
)
1038 struct iovec iov
[1];
1041 #if defined(__linux__) || defined(__CYGWIN__)
1042 struct cmsghdr
*cmptr
;
1043 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
1044 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
1045 #endif /* __linux__, __CYGWIN__ */
1049 LTTNG_ASSERT(len
> 0);
1050 LTTNG_ASSERT(creds
);
1052 memset(&msg
, 0, sizeof(msg
));
1054 /* Prepare to receive the structures */
1055 iov
[0].iov_base
= buf
;
1056 iov
[0].iov_len
= len
;
1060 #if defined(__linux__) || defined(__CYGWIN__)
1061 msg
.msg_control
= anc_buf
;
1062 msg
.msg_controllen
= sizeof(anc_buf
);
1063 #endif /* __linux__, __CYGWIN__ */
1066 len_last
= iov
[0].iov_len
;
1067 ret
= recvmsg(sock
, &msg
, 0);
1069 iov
[0].iov_base
= (char *) iov
[0].iov_base
+ ret
;
1070 iov
[0].iov_len
-= ret
;
1071 LTTNG_ASSERT(ret
<= len_last
);
1073 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
1075 PERROR("recvmsg fds");
1077 } else if (ret
> 0) {
1080 /* Else ret = 0 meaning an orderly shutdown. */
1082 #if defined(__linux__) || defined(__CYGWIN__)
1083 if (msg
.msg_flags
& MSG_CTRUNC
) {
1084 fprintf(stderr
, "Error: Control message truncated.\n");
1089 cmptr
= CMSG_FIRSTHDR(&msg
);
1090 if (cmptr
== NULL
) {
1091 fprintf(stderr
, "Error: Invalid control message header\n");
1096 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
1097 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
1098 fprintf(stderr
, "Didn't received any credentials\n");
1103 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
1104 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
1105 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
1110 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
1111 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1112 if (lttng_get_unix_socket_peer_creds(sock
, creds
)) {
1113 fprintf(stderr
, "ARG\n");
1118 #error "Please implement credential support for your OS."
1119 #endif /* __linux__, __CYGWIN__ */
1126 * Set socket option to use credentials passing.
1128 #if defined(__linux__) || defined(__CYGWIN__)
1129 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1133 /* Set socket for credentials retrieval */
1134 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
1136 PERROR("setsockopt creds unix sock");
1140 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1141 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1146 #error "Please implement credential support for your OS."
1147 #endif /* __linux__ */