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.
29 int lttcomm_connect_unix_sock(const char *pathname
)
31 struct sockaddr_un s_un
;
32 int fd
, ret
, closeret
;
34 if (strlen(pathname
) >= sizeof(s_un
.sun_path
)) {
35 ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
36 pathname
, strlen(pathname
) + 1,
37 sizeof(s_un
.sun_path
));
42 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
49 memset(&s_un
, 0, sizeof(s_un
));
50 s_un
.sun_family
= AF_UNIX
;
51 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
52 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
54 ret
= connect(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
57 * Don't print message on connect error, because connect is used in
58 * normal execution to detect if sessiond is alive.
75 * Do an accept(2) on the sock and return the new file descriptor. The socket
76 * MUST be bind(2) before.
79 int lttcomm_accept_unix_sock(int sock
)
82 struct sockaddr_un s_un
;
83 socklen_t len
= sizeof(s_un
);
86 new_fd
= accept(sock
, (struct sockaddr
*) &s_un
, &len
);
95 int lttcomm_create_anon_unix_socketpair(int *fds
)
97 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
105 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
109 int lttcomm_create_unix_sock(const char *pathname
)
111 struct sockaddr_un s_un
;
115 if (strlen(pathname
) >= sizeof(s_un
.sun_path
)) {
116 ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
117 pathname
, strlen(pathname
) + 1,
118 sizeof(s_un
.sun_path
));
123 /* Create server socket */
124 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
129 memset(&s_un
, 0, sizeof(s_un
));
130 s_un
.sun_family
= AF_UNIX
;
131 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
132 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
134 /* Unlink the old file if present */
135 (void) unlink(pathname
);
136 ret
= bind(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
147 PERROR("close create unix sock");
154 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
157 int lttcomm_listen_unix_sock(int sock
)
161 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
170 * Receive data of size len in put that data into the buf param. Using recvmsg
173 * Return the size of received data.
176 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
185 LTTNG_ASSERT(len
> 0);
187 memset(&msg
, 0, sizeof(msg
));
189 iov
[0].iov_base
= buf
;
190 iov
[0].iov_len
= len
;
195 len_last
= iov
[0].iov_len
;
196 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
198 iov
[0].iov_base
+= ret
;
199 iov
[0].iov_len
-= ret
;
200 LTTNG_ASSERT(ret
<= len_last
);
202 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
205 } else if (ret
> 0) {
208 /* Else ret = 0 meaning an orderly shutdown. */
214 * Receive data of size len in put that data into the buf param. Using recvmsg
215 * API. Only use with sockets set in non-blocking mode.
217 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
218 * poll set. The poll loop will handle the EPIPE original cause.
220 * Return the size of received data.
223 ssize_t
lttcomm_recv_unix_sock_non_block(int sock
, void *buf
, size_t len
)
231 LTTNG_ASSERT(len
> 0);
233 memset(&msg
, 0, sizeof(msg
));
235 iov
[0].iov_base
= buf
;
236 iov
[0].iov_len
= len
;
241 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
243 if (errno
== EINTR
) {
247 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
249 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
258 /* Unexpected error */
270 * Send buf data of size len. Using sendmsg API.
272 * Return the size of sent data.
275 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
283 LTTNG_ASSERT(len
> 0);
285 memset(&msg
, 0, sizeof(msg
));
287 iov
[0].iov_base
= (void *) buf
;
288 iov
[0].iov_len
= len
;
292 while (iov
[0].iov_len
) {
293 ret
= sendmsg(sock
, &msg
, 0);
295 if (errno
== EINTR
) {
299 * Only warn about EPIPE when quiet mode is
301 * We consider EPIPE as expected.
303 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
309 iov
[0].iov_len
-= ret
;
310 iov
[0].iov_base
+= ret
;
318 * Send buf data of size len. Using sendmsg API.
319 * Only use with non-blocking sockets. The difference with the blocking version
320 * of the function is that this one does not retry to send on partial sends,
321 * except if the interruption was caused by a signal (EINTR).
323 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
324 * poll set. The poll loop will handle the EPIPE original cause.
326 * Return the size of sent data.
329 ssize_t
lttcomm_send_unix_sock_non_block(int sock
, const void *buf
, size_t len
)
337 LTTNG_ASSERT(len
> 0);
339 memset(&msg
, 0, sizeof(msg
));
341 iov
[0].iov_base
= (void *) buf
;
342 iov
[0].iov_len
= len
;
347 ret
= sendmsg(sock
, &msg
, 0);
349 if (errno
== EINTR
) {
353 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
355 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
358 * This can happen in non blocking mode.
365 /* Unexpected error */
376 * Shutdown cleanly a unix socket.
379 int lttcomm_close_unix_sock(int sock
)
383 /* Shutdown receptions and transmissions */
384 ret
= shutdown(sock
, SHUT_RDWR
);
389 closeret
= close(sock
);
398 * Send a message accompanied by fd(s) over a unix socket.
400 * Returns the size of data sent, or negative error value.
403 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
406 struct cmsghdr
*cmptr
;
409 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
410 char tmp
[CMSG_SPACE(sizeof_fds
)];
415 LTTNG_ASSERT(nb_fd
> 0);
417 memset(&msg
, 0, sizeof(msg
));
418 memset(tmp
, 0, sizeof(tmp
));
420 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
423 msg
.msg_control
= (caddr_t
)tmp
;
424 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
426 cmptr
= CMSG_FIRSTHDR(&msg
);
431 cmptr
->cmsg_level
= SOL_SOCKET
;
432 cmptr
->cmsg_type
= SCM_RIGHTS
;
433 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
434 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
435 /* Sum of the length of all control messages in the buffer: */
436 msg
.msg_controllen
= cmptr
->cmsg_len
;
438 iov
[0].iov_base
= &dummy
;
444 ret
= sendmsg(sock
, &msg
, 0);
445 } while (ret
< 0 && errno
== EINTR
);
448 * Only warn about EPIPE when quiet mode is deactivated.
449 * We consider EPIPE as expected.
451 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
459 * Send the fd(s) of a payload view over a unix socket.
461 * Returns the size of data sent, or negative error value.
464 ssize_t
_lttcomm_send_payload_view_fds_unix_sock(int sock
,
465 struct lttng_payload_view
*view
,
470 struct lttng_dynamic_array raw_fds
;
471 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
473 lttng_dynamic_array_init(&raw_fds
, sizeof(int), NULL
);
476 ret
= -LTTNG_ERR_INVALID
;
481 * Prepare a contiguous array of file descriptors to send them.
483 * Note that the reference to each fd is released during the iteration;
484 * we're just getting the numerical value of the fds to conform to the
485 * syscall's interface. We rely on the fact that "view" must remain
486 * valid for the duration of the call and that the underlying payload
487 * owns a reference to the fd_handles.
489 for (i
= 0; i
< fd_count
; i
++) {
490 struct fd_handle
*handle
=
491 lttng_payload_view_pop_fd_handle(view
);
492 const int raw_fd
= fd_handle_get_fd(handle
);
493 const int add_ret
= lttng_dynamic_array_add_element(
496 fd_handle_put(handle
);
498 ret
= -LTTNG_ERR_NOMEM
;
504 ret
= lttcomm_send_fds_unix_sock(sock
,
505 (const int *) raw_fds
.buffer
.data
, fd_count
);
507 ret
= lttcomm_send_fds_unix_sock_non_block(sock
,
508 (const int *) raw_fds
.buffer
.data
, fd_count
);
512 lttng_dynamic_array_reset(&raw_fds
);
517 ssize_t
lttcomm_send_payload_view_fds_unix_sock(int sock
,
518 struct lttng_payload_view
*view
)
520 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, true);
524 ssize_t
lttcomm_send_payload_view_fds_unix_sock_non_block(int sock
,
525 struct lttng_payload_view
*view
)
527 return _lttcomm_send_payload_view_fds_unix_sock(sock
, view
, false);
531 * Send a message accompanied by fd(s) over a unix socket.
532 * Only use for non blocking socket.
534 * Returns the size of data sent, or negative error value.
537 ssize_t
lttcomm_send_fds_unix_sock_non_block(int sock
, const int *fds
, size_t nb_fd
)
540 struct cmsghdr
*cmptr
;
543 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
544 char tmp
[CMSG_SPACE(sizeof_fds
)];
549 LTTNG_ASSERT(nb_fd
> 0);
551 memset(&msg
, 0, sizeof(msg
));
552 memset(tmp
, 0, sizeof(tmp
));
554 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
557 msg
.msg_control
= (caddr_t
)tmp
;
558 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
560 cmptr
= CMSG_FIRSTHDR(&msg
);
565 cmptr
->cmsg_level
= SOL_SOCKET
;
566 cmptr
->cmsg_type
= SCM_RIGHTS
;
567 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
568 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
569 /* Sum of the length of all control messages in the buffer: */
570 msg
.msg_controllen
= cmptr
->cmsg_len
;
572 iov
[0].iov_base
= &dummy
;
578 ret
= sendmsg(sock
, &msg
, 0);
580 if (errno
== EINTR
) {
584 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
586 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
588 * This can happen in non blocking mode.
595 if (errno
== EPIPE
) {
596 /* Expected error, pass error to caller */
597 DBG3("EPIPE on sendmsg");
602 /* Unexpected error */
614 * Recv a message accompanied by fd(s) from a unix socket.
616 * Returns the size of received data, or negative error value.
618 * Expect at most "nb_fd" file descriptors. Returns the number of fd
619 * actually received in nb_fd.
622 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
626 struct cmsghdr
*cmsg
;
627 size_t sizeof_fds
= nb_fd
* sizeof(int);
630 /* Account for the struct ucred cmsg in the buffer size */
631 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
633 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
634 #endif /* __linux__ */
636 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
642 LTTNG_ASSERT(nb_fd
> 0);
644 memset(&msg
, 0, sizeof(msg
));
646 /* Prepare to receive the structures */
647 iov
[0].iov_base
= &dummy
;
652 cmsg
= (struct cmsghdr
*) recv_buf
;
653 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
654 cmsg
->cmsg_level
= SOL_SOCKET
;
655 cmsg
->cmsg_type
= SCM_RIGHTS
;
657 msg
.msg_control
= cmsg
;
658 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
662 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
664 if (errno
== EINTR
) {
667 /* We consider EPIPE and EAGAIN as expected. */
668 if (!lttng_opt_quiet
&&
669 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
677 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
682 if (msg
.msg_flags
& MSG_CTRUNC
) {
683 fprintf(stderr
, "Error: Control message truncated.\n");
689 * If the socket was configured with SO_PASSCRED, the kernel will add a
690 * control message (cmsg) to the ancillary data of the unix socket. We
691 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
694 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
695 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
696 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
700 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
702 * We found the controle message for file descriptors,
703 * now copy the fds to the fds ptr and return success.
705 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
706 fprintf(stderr
, "Error: Received %zu bytes of"
707 "ancillary data for FDs, expected %zu\n",
708 (size_t) cmsg
->cmsg_len
,
709 (size_t) CMSG_LEN(sizeof_fds
));
713 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
718 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
720 * Expect credentials to be sent when expecting fds even
721 * if no credential were include in the send(). The
722 * kernel adds them...
726 #endif /* __linux__ */
733 void close_raw_fd(void *ptr
)
735 const int raw_fd
= *((const int *) ptr
);
738 const int ret
= close(raw_fd
);
741 PERROR("Failed to close file descriptor %d", raw_fd
);
747 enum lttng_error_code
add_fds_to_payload(struct lttng_dynamic_array
*raw_fds
,
748 struct lttng_payload
*payload
)
751 enum lttng_error_code ret_code
= LTTNG_OK
;
752 const int fd_count
= lttng_dynamic_array_get_count(raw_fds
);
754 for (i
= 0; i
< fd_count
; i
++) {
756 struct fd_handle
*handle
;
757 int *raw_fd
= (int *) lttng_dynamic_array_get_element(
760 LTTNG_ASSERT(*raw_fd
!= -1);
762 handle
= fd_handle_create(*raw_fd
);
764 ret_code
= LTTNG_ERR_NOMEM
;
768 /* FD ownership transferred to the handle. */
771 ret
= lttng_payload_push_fd_handle(payload
, handle
);
772 fd_handle_put(handle
);
774 ret_code
= LTTNG_ERR_NOMEM
;
784 ssize_t
_lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
785 struct lttng_payload
*payload
, bool blocking
)
788 enum lttng_error_code add_ret
;
790 int default_value
= -1;
791 struct lttng_dynamic_array raw_fds
;
794 LTTNG_ASSERT(payload
);
795 LTTNG_ASSERT(nb_fd
> 0);
797 lttng_dynamic_array_init(&raw_fds
, sizeof(int), close_raw_fd
);
799 for (i
= 0; i
< nb_fd
; i
++) {
800 if (lttng_dynamic_array_add_element(&raw_fds
, &default_value
)) {
801 ret
= -LTTNG_ERR_NOMEM
;
807 ret
= lttcomm_recv_fds_unix_sock(
808 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
810 ret
= lttcomm_recv_fds_unix_sock_non_block(
811 sock
, (int *) raw_fds
.buffer
.data
, nb_fd
);
818 add_ret
= add_fds_to_payload(&raw_fds
, payload
);
819 if (add_ret
!= LTTNG_OK
) {
820 ret
= - (int) add_ret
;
825 lttng_dynamic_array_reset(&raw_fds
);
830 ssize_t
lttcomm_recv_payload_fds_unix_sock(int sock
, size_t nb_fd
,
831 struct lttng_payload
*payload
)
833 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, true);
837 ssize_t
lttcomm_recv_payload_fds_unix_sock_non_block(int sock
, size_t nb_fd
,
838 struct lttng_payload
*payload
)
840 return _lttcomm_recv_payload_fds_unix_sock(sock
, nb_fd
, payload
, false);
844 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
845 * Only use with non-blocking sockets.
847 * Returns the size of received data, or negative error value.
849 * Expect at most "nb_fd" file descriptors.
851 * Note that based on our comprehension, partial reception of fds is not
852 * possible since the FDs are actually in the control message. It is all or
853 * nothing, still the sender side can send the wrong number of fds.
856 ssize_t
lttcomm_recv_fds_unix_sock_non_block(int sock
, int *fds
, size_t nb_fd
)
860 struct cmsghdr
*cmsg
;
861 size_t sizeof_fds
= nb_fd
* sizeof(int);
865 LTTNG_ASSERT(nb_fd
> 0);
868 /* Account for the struct ucred cmsg in the buffer size */
869 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
871 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
872 #endif /* __linux__ */
874 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
878 memset(&msg
, 0, sizeof(msg
));
880 /* Prepare to receive the structures */
881 iov
[0].iov_base
= &dummy
;
886 cmsg
= (struct cmsghdr
*) recv_buf
;
887 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
888 cmsg
->cmsg_level
= SOL_SOCKET
;
889 cmsg
->cmsg_type
= SCM_RIGHTS
;
891 msg
.msg_control
= cmsg
;
892 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
896 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
898 if (errno
== EINTR
) {
902 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
904 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
906 * This can happen in non blocking mode.
913 if (errno
== EPIPE
) {
914 /* Expected error, pass error to caller */
915 DBG3("EPIPE on recvmsg");
920 /* Unexpected error */
928 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
933 if (msg
.msg_flags
& MSG_CTRUNC
) {
934 fprintf(stderr
, "Error: Control message truncated.\n");
940 * If the socket was configured with SO_PASSCRED, the kernel will add a
941 * control message (cmsg) to the ancillary data of the unix socket. We
942 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
945 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
946 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
947 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
951 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
953 * We found the controle message for file descriptors,
954 * now copy the fds to the fds ptr and return success.
956 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
957 fprintf(stderr
, "Error: Received %zu bytes of"
958 "ancillary data for FDs, expected %zu\n",
959 (size_t) cmsg
->cmsg_len
,
960 (size_t) CMSG_LEN(sizeof_fds
));
964 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
969 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
971 * Expect credentials to be sent when expecting fds even
972 * if no credential were include in the send(). The
973 * kernel adds them...
977 #endif /* __linux__ */
984 * Send a message with credentials over a unix socket.
986 * Returns the size of data sent, or negative error value.
989 ssize_t
lttcomm_send_creds_unix_sock(int sock
, const void *buf
, size_t len
)
994 #if defined(__linux__) || defined(__CYGWIN__)
995 struct cmsghdr
*cmptr
;
996 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
997 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
998 lttng_sock_cred
*creds
;
1000 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
1001 #endif /* __linux__, __CYGWIN__ */
1003 memset(&msg
, 0, sizeof(msg
));
1007 LTTNG_ASSERT(len
> 0);
1009 iov
[0].iov_base
= (void *) buf
;
1010 iov
[0].iov_len
= len
;
1014 #if defined(__linux__) || defined(__CYGWIN__)
1015 msg
.msg_control
= (caddr_t
) anc_buf
;
1016 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
1018 cmptr
= CMSG_FIRSTHDR(&msg
);
1022 cmptr
->cmsg_level
= SOL_SOCKET
;
1023 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
1024 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
1026 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
1028 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
1029 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
1030 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
1031 #endif /* __linux__, __CYGWIN__ */
1034 ret
= sendmsg(sock
, &msg
, 0);
1035 } while (ret
< 0 && errno
== EINTR
);
1038 * Only warn about EPIPE when quiet mode is deactivated.
1039 * We consider EPIPE as expected.
1041 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
1049 * Recv a message accompanied with credentials from a unix socket.
1051 * Returns the size of received data, or negative error value.
1054 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
1055 lttng_sock_cred
*creds
)
1058 struct iovec iov
[1];
1061 #if defined(__linux__) || defined(__CYGWIN__)
1062 struct cmsghdr
*cmptr
;
1063 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
1064 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
1065 #endif /* __linux__, __CYGWIN__ */
1069 LTTNG_ASSERT(len
> 0);
1070 LTTNG_ASSERT(creds
);
1072 memset(&msg
, 0, sizeof(msg
));
1074 /* Prepare to receive the structures */
1075 iov
[0].iov_base
= buf
;
1076 iov
[0].iov_len
= len
;
1080 #if defined(__linux__) || defined(__CYGWIN__)
1081 msg
.msg_control
= anc_buf
;
1082 msg
.msg_controllen
= sizeof(anc_buf
);
1083 #endif /* __linux__, __CYGWIN__ */
1086 len_last
= iov
[0].iov_len
;
1087 ret
= recvmsg(sock
, &msg
, 0);
1089 iov
[0].iov_base
+= ret
;
1090 iov
[0].iov_len
-= ret
;
1091 LTTNG_ASSERT(ret
<= len_last
);
1093 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
1095 PERROR("recvmsg fds");
1097 } else if (ret
> 0) {
1100 /* Else ret = 0 meaning an orderly shutdown. */
1102 #if defined(__linux__) || defined(__CYGWIN__)
1103 if (msg
.msg_flags
& MSG_CTRUNC
) {
1104 fprintf(stderr
, "Error: Control message truncated.\n");
1109 cmptr
= CMSG_FIRSTHDR(&msg
);
1110 if (cmptr
== NULL
) {
1111 fprintf(stderr
, "Error: Invalid control message header\n");
1116 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
1117 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
1118 fprintf(stderr
, "Didn't received any credentials\n");
1123 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
1124 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
1125 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
1130 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
1131 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1132 if (lttng_get_unix_socket_peer_creds(sock
, creds
)) {
1133 fprintf(stderr
, "ARG\n");
1138 #error "Please implement credential support for your OS."
1139 #endif /* __linux__, __CYGWIN__ */
1146 * Set socket option to use credentials passing.
1148 #if defined(__linux__) || defined(__CYGWIN__)
1150 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1154 /* Set socket for credentials retrieval */
1155 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
1157 PERROR("setsockopt creds unix sock");
1161 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
1163 int lttcomm_setsockopt_creds_unix_sock(int sock
)
1168 #error "Please implement credential support for your OS."
1169 #endif /* __linux__ */