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>
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
)
183 memset(&msg
, 0, sizeof(msg
));
185 iov
[0].iov_base
= buf
;
186 iov
[0].iov_len
= len
;
191 len_last
= iov
[0].iov_len
;
192 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
194 iov
[0].iov_base
+= ret
;
195 iov
[0].iov_len
-= ret
;
196 assert(ret
<= len_last
);
198 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
201 } else if (ret
> 0) {
204 /* Else ret = 0 meaning an orderly shutdown. */
210 * Receive data of size len in put that data into the buf param. Using recvmsg
211 * API. Only use with sockets set in non-blocking mode.
213 * Return the size of received data.
216 ssize_t
lttcomm_recv_unix_sock_non_block(int sock
, void *buf
, size_t len
)
222 memset(&msg
, 0, sizeof(msg
));
224 iov
[0].iov_base
= buf
;
225 iov
[0].iov_len
= len
;
230 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
232 if (errno
== EINTR
) {
235 /* We consider EPIPE and EAGAIN as expected. */
236 if (!lttng_opt_quiet
&&
237 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
249 * Send buf data of size len. Using sendmsg API.
251 * Return the size of sent data.
254 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
260 memset(&msg
, 0, sizeof(msg
));
262 iov
[0].iov_base
= (void *) buf
;
263 iov
[0].iov_len
= len
;
267 while (iov
[0].iov_len
) {
268 ret
= sendmsg(sock
, &msg
, 0);
270 if (errno
== EINTR
) {
274 * Only warn about EPIPE when quiet mode is
276 * We consider EPIPE as expected.
278 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
284 iov
[0].iov_len
-= ret
;
285 iov
[0].iov_base
+= ret
;
293 * Send buf data of size len. Using sendmsg API.
294 * Only use with non-blocking sockets. The difference with the blocking version
295 * of the function is that this one does not retry to send on partial sends,
296 * except if the interruption was caused by a signal (EINTR).
298 * Return the size of sent data.
301 ssize_t
lttcomm_send_unix_sock_non_block(int sock
, const void *buf
, size_t len
)
307 memset(&msg
, 0, sizeof(msg
));
309 iov
[0].iov_base
= (void *) buf
;
310 iov
[0].iov_len
= len
;
315 ret
= sendmsg(sock
, &msg
, 0);
317 if (errno
== EINTR
) {
320 /* We consider EPIPE and EAGAIN as expected. */
321 if (!lttng_opt_quiet
&&
322 (errno
!= EPIPE
&& errno
!= EAGAIN
)) {
334 * Shutdown cleanly a unix socket.
337 int lttcomm_close_unix_sock(int sock
)
341 /* Shutdown receptions and transmissions */
342 ret
= shutdown(sock
, SHUT_RDWR
);
347 closeret
= close(sock
);
356 * Send a message accompanied by fd(s) over a unix socket.
358 * Returns the size of data sent, or negative error value.
361 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
364 struct cmsghdr
*cmptr
;
367 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
368 char tmp
[CMSG_SPACE(sizeof_fds
)];
371 memset(&msg
, 0, sizeof(msg
));
372 memset(tmp
, 0, sizeof(tmp
));
374 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
377 msg
.msg_control
= (caddr_t
)tmp
;
378 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
380 cmptr
= CMSG_FIRSTHDR(&msg
);
385 cmptr
->cmsg_level
= SOL_SOCKET
;
386 cmptr
->cmsg_type
= SCM_RIGHTS
;
387 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
388 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
389 /* Sum of the length of all control messages in the buffer: */
390 msg
.msg_controllen
= cmptr
->cmsg_len
;
392 iov
[0].iov_base
= &dummy
;
398 ret
= sendmsg(sock
, &msg
, 0);
399 } while (ret
< 0 && errno
== EINTR
);
402 * Only warn about EPIPE when quiet mode is deactivated.
403 * We consider EPIPE as expected.
405 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
413 * Recv a message accompanied by fd(s) from a unix socket.
415 * Returns the size of received data, or negative error value.
417 * Expect at most "nb_fd" file descriptors. Returns the number of fd
418 * actually received in nb_fd.
421 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
425 struct cmsghdr
*cmsg
;
426 size_t sizeof_fds
= nb_fd
* sizeof(int);
429 /* Account for the struct ucred cmsg in the buffer size */
430 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
432 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
433 #endif /* __linux__ */
435 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
439 memset(&msg
, 0, sizeof(msg
));
441 /* Prepare to receive the structures */
442 iov
[0].iov_base
= &dummy
;
447 cmsg
= (struct cmsghdr
*) recv_buf
;
448 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
449 cmsg
->cmsg_level
= SOL_SOCKET
;
450 cmsg
->cmsg_type
= SCM_RIGHTS
;
452 msg
.msg_control
= cmsg
;
453 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
457 ret
= recvmsg(sock
, &msg
, 0);
458 } while (ret
< 0 && errno
== EINTR
);
460 PERROR("recvmsg fds");
465 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
470 if (msg
.msg_flags
& MSG_CTRUNC
) {
471 fprintf(stderr
, "Error: Control message truncated.\n");
477 * If the socket was configured with SO_PASSCRED, the kernel will add a
478 * control message (cmsg) to the ancillary data of the unix socket. We
479 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
482 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
483 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
484 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
488 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
490 * We found the controle message for file descriptors,
491 * now copy the fds to the fds ptr and return success.
493 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
494 fprintf(stderr
, "Error: Received %zu bytes of"
495 "ancillary data for FDs, expected %zu\n",
496 (size_t) cmsg
->cmsg_len
,
497 (size_t) CMSG_LEN(sizeof_fds
));
501 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
506 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
508 * Expect credentials to be sent when expecting fds even
509 * if no credential were include in the send(). The
510 * kernel adds them...
514 #endif /* __linux__ */
521 * Send a message with credentials over a unix socket.
523 * Returns the size of data sent, or negative error value.
526 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
532 struct cmsghdr
*cmptr
;
533 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
534 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
535 lttng_sock_cred
*creds
;
537 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
538 #endif /* __linux__ */
540 memset(&msg
, 0, sizeof(msg
));
542 iov
[0].iov_base
= buf
;
543 iov
[0].iov_len
= len
;
548 msg
.msg_control
= (caddr_t
) anc_buf
;
549 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
551 cmptr
= CMSG_FIRSTHDR(&msg
);
555 cmptr
->cmsg_level
= SOL_SOCKET
;
556 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
557 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
559 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
561 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
562 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
563 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
564 #endif /* __linux__ */
567 ret
= sendmsg(sock
, &msg
, 0);
568 } while (ret
< 0 && errno
== EINTR
);
571 * Only warn about EPIPE when quiet mode is deactivated.
572 * We consider EPIPE as expected.
574 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
582 * Recv a message accompanied with credentials from a unix socket.
584 * Returns the size of received data, or negative error value.
587 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
588 lttng_sock_cred
*creds
)
595 struct cmsghdr
*cmptr
;
596 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
597 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
598 #endif /* __linux__ */
600 memset(&msg
, 0, sizeof(msg
));
608 /* Prepare to receive the structures */
609 iov
[0].iov_base
= buf
;
610 iov
[0].iov_len
= len
;
615 msg
.msg_control
= anc_buf
;
616 msg
.msg_controllen
= sizeof(anc_buf
);
617 #endif /* __linux__ */
620 len_last
= iov
[0].iov_len
;
621 ret
= recvmsg(sock
, &msg
, 0);
623 iov
[0].iov_base
+= ret
;
624 iov
[0].iov_len
-= ret
;
625 assert(ret
<= len_last
);
627 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
629 PERROR("recvmsg fds");
631 } else if (ret
> 0) {
634 /* Else ret = 0 meaning an orderly shutdown. */
637 if (msg
.msg_flags
& MSG_CTRUNC
) {
638 fprintf(stderr
, "Error: Control message truncated.\n");
643 cmptr
= CMSG_FIRSTHDR(&msg
);
645 fprintf(stderr
, "Error: Invalid control message header\n");
650 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
651 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
652 fprintf(stderr
, "Didn't received any credentials\n");
657 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
658 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
659 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
664 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
665 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
669 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
675 #error "Please implement credential support for your OS."
676 #endif /* __linux__ */
683 * Set socket option to use credentials passing.
687 int lttcomm_setsockopt_creds_unix_sock(int sock
)
691 /* Set socket for credentials retrieval */
692 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
694 PERROR("setsockopt creds unix sock");
698 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
700 int lttcomm_setsockopt_creds_unix_sock(int sock
)
705 #error "Please implement credential support for your OS."
706 #endif /* __linux__ */