2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
36 * Connect to unix socket using the path name.
39 int lttcomm_connect_unix_sock(const char *pathname
)
41 struct sockaddr_un s_un
;
42 int fd
, ret
, closeret
;
44 if (strlen(pathname
) >= sizeof(s_un
.sun_path
)) {
45 ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
46 pathname
, strlen(pathname
) + 1,
47 sizeof(s_un
.sun_path
));
52 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
59 memset(&s_un
, 0, sizeof(s_un
));
60 s_un
.sun_family
= AF_UNIX
;
61 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
62 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
64 ret
= connect(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
67 * Don't print message on connect error, because connect is used in
68 * normal execution to detect if sessiond is alive.
85 * Do an accept(2) on the sock and return the new file descriptor. The socket
86 * MUST be bind(2) before.
89 int lttcomm_accept_unix_sock(int sock
)
92 struct sockaddr_un s_un
;
93 socklen_t len
= sizeof(s_un
);
96 new_fd
= accept(sock
, (struct sockaddr
*) &s_un
, &len
);
105 int lttcomm_create_anon_unix_socketpair(int *fds
)
107 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
108 PERROR("socketpair");
115 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
119 int lttcomm_create_unix_sock(const char *pathname
)
121 struct sockaddr_un s_un
;
125 if (strlen(pathname
) >= sizeof(s_un
.sun_path
)) {
126 ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
127 pathname
, strlen(pathname
) + 1,
128 sizeof(s_un
.sun_path
));
133 /* Create server socket */
134 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
139 memset(&s_un
, 0, sizeof(s_un
));
140 s_un
.sun_family
= AF_UNIX
;
141 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
142 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
144 /* Unlink the old file if present */
145 (void) unlink(pathname
);
146 ret
= bind(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
157 PERROR("close create unix sock");
164 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
167 int lttcomm_listen_unix_sock(int sock
)
171 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
180 * Receive data of size len in put that data into the buf param. Using recvmsg
183 * Return the size of received data.
186 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
193 memset(&msg
, 0, sizeof(msg
));
195 iov
[0].iov_base
= buf
;
196 iov
[0].iov_len
= len
;
201 len_last
= iov
[0].iov_len
;
202 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
204 iov
[0].iov_base
+= ret
;
205 iov
[0].iov_len
-= ret
;
206 assert(ret
<= len_last
);
208 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
211 } else if (ret
> 0) {
214 /* Else ret = 0 meaning an orderly shutdown. */
220 * Receive data of size len in put that data into the buf param. Using recvmsg
221 * API. Only use with sockets set in non-blocking mode.
223 * Return the size of received data.
226 ssize_t
lttcomm_recv_unix_sock_non_block(int sock
, void *buf
, size_t len
)
232 memset(&msg
, 0, sizeof(msg
));
234 iov
[0].iov_base
= buf
;
235 iov
[0].iov_len
= len
;
240 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
242 if (errno
== EINTR
) {
246 * Only warn about EPIPE when quiet mode is
248 * We consider EPIPE as expected.
250 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
262 * Send buf data of size len. Using sendmsg API.
264 * Return the size of sent data.
267 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
273 memset(&msg
, 0, sizeof(msg
));
275 iov
[0].iov_base
= (void *) buf
;
276 iov
[0].iov_len
= len
;
280 while (iov
[0].iov_len
) {
281 ret
= sendmsg(sock
, &msg
, 0);
283 if (errno
== EINTR
) {
287 * Only warn about EPIPE when quiet mode is
289 * We consider EPIPE as expected.
291 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
297 iov
[0].iov_len
-= ret
;
298 iov
[0].iov_base
+= ret
;
306 * Send buf data of size len. Using sendmsg API.
307 * Only use with non-blocking sockets. The difference with the blocking version
308 * of the function is that this one does not retry to send on partial sends,
309 * except if the interruption was caused by a signal (EINTR).
311 * Return the size of sent data.
314 ssize_t
lttcomm_send_unix_sock_non_block(int sock
, const void *buf
, size_t len
)
320 memset(&msg
, 0, sizeof(msg
));
322 iov
[0].iov_base
= (void *) buf
;
323 iov
[0].iov_len
= len
;
328 ret
= sendmsg(sock
, &msg
, 0);
330 if (errno
== EINTR
) {
334 * Only warn about EPIPE when quiet mode is
336 * We consider EPIPE as expected.
338 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
350 * Shutdown cleanly a unix socket.
353 int lttcomm_close_unix_sock(int sock
)
357 /* Shutdown receptions and transmissions */
358 ret
= shutdown(sock
, SHUT_RDWR
);
363 closeret
= close(sock
);
372 * Send a message accompanied by fd(s) over a unix socket.
374 * Returns the size of data sent, or negative error value.
377 ssize_t
lttcomm_send_fds_unix_sock(int sock
, const int *fds
, size_t nb_fd
)
380 struct cmsghdr
*cmptr
;
383 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
384 char tmp
[CMSG_SPACE(sizeof_fds
)];
387 memset(&msg
, 0, sizeof(msg
));
388 memset(tmp
, 0, sizeof(tmp
));
390 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
393 msg
.msg_control
= (caddr_t
)tmp
;
394 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
396 cmptr
= CMSG_FIRSTHDR(&msg
);
401 cmptr
->cmsg_level
= SOL_SOCKET
;
402 cmptr
->cmsg_type
= SCM_RIGHTS
;
403 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
404 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
405 /* Sum of the length of all control messages in the buffer: */
406 msg
.msg_controllen
= cmptr
->cmsg_len
;
408 iov
[0].iov_base
= &dummy
;
414 ret
= sendmsg(sock
, &msg
, 0);
415 } while (ret
< 0 && errno
== EINTR
);
418 * Only warn about EPIPE when quiet mode is deactivated.
419 * We consider EPIPE as expected.
421 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
429 * Recv a message accompanied by fd(s) from a unix socket.
431 * Returns the size of received data, or negative error value.
433 * Expect at most "nb_fd" file descriptors. Returns the number of fd
434 * actually received in nb_fd.
437 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
441 struct cmsghdr
*cmsg
;
442 size_t sizeof_fds
= nb_fd
* sizeof(int);
445 /* Account for the struct ucred cmsg in the buffer size */
446 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
448 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
449 #endif /* __linux__ */
451 char recv_buf
[LTTNG_SOCK_RECV_FDS_BUF_SIZE
];
455 memset(&msg
, 0, sizeof(msg
));
457 /* Prepare to receive the structures */
458 iov
[0].iov_base
= &dummy
;
463 cmsg
= (struct cmsghdr
*) recv_buf
;
464 cmsg
->cmsg_len
= CMSG_LEN(sizeof_fds
);
465 cmsg
->cmsg_level
= SOL_SOCKET
;
466 cmsg
->cmsg_type
= SCM_RIGHTS
;
468 msg
.msg_control
= cmsg
;
469 msg
.msg_controllen
= CMSG_LEN(sizeof(recv_buf
));
473 ret
= recvmsg(sock
, &msg
, 0);
474 } while (ret
< 0 && errno
== EINTR
);
476 PERROR("recvmsg fds");
481 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
486 if (msg
.msg_flags
& MSG_CTRUNC
) {
487 fprintf(stderr
, "Error: Control message truncated.\n");
493 * If the socket was configured with SO_PASSCRED, the kernel will add a
494 * control message (cmsg) to the ancillary data of the unix socket. We
495 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
498 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
499 if (cmsg
->cmsg_level
!= SOL_SOCKET
) {
500 fprintf(stderr
, "Error: The socket needs to be of type SOL_SOCKET\n");
504 if (cmsg
->cmsg_type
== SCM_RIGHTS
) {
506 * We found the controle message for file descriptors,
507 * now copy the fds to the fds ptr and return success.
509 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
510 fprintf(stderr
, "Error: Received %zu bytes of"
511 "ancillary data for FDs, expected %zu\n",
512 (size_t) cmsg
->cmsg_len
,
513 (size_t) CMSG_LEN(sizeof_fds
));
517 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
522 if (cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
524 * Expect credentials to be sent when expecting fds even
525 * if no credential were include in the send(). The
526 * kernel adds them...
530 #endif /* __linux__ */
537 * Send a message with credentials over a unix socket.
539 * Returns the size of data sent, or negative error value.
542 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
548 struct cmsghdr
*cmptr
;
549 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
550 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
551 lttng_sock_cred
*creds
;
553 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
554 #endif /* __linux__ */
556 memset(&msg
, 0, sizeof(msg
));
558 iov
[0].iov_base
= buf
;
559 iov
[0].iov_len
= len
;
564 msg
.msg_control
= (caddr_t
) anc_buf
;
565 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
567 cmptr
= CMSG_FIRSTHDR(&msg
);
571 cmptr
->cmsg_level
= SOL_SOCKET
;
572 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
573 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
575 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
577 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
578 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
579 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
580 #endif /* __linux__ */
583 ret
= sendmsg(sock
, &msg
, 0);
584 } while (ret
< 0 && errno
== EINTR
);
587 * Only warn about EPIPE when quiet mode is deactivated.
588 * We consider EPIPE as expected.
590 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
598 * Recv a message accompanied with credentials from a unix socket.
600 * Returns the size of received data, or negative error value.
603 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
604 lttng_sock_cred
*creds
)
611 struct cmsghdr
*cmptr
;
612 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
613 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
614 #endif /* __linux__ */
616 memset(&msg
, 0, sizeof(msg
));
624 /* Prepare to receive the structures */
625 iov
[0].iov_base
= buf
;
626 iov
[0].iov_len
= len
;
631 msg
.msg_control
= anc_buf
;
632 msg
.msg_controllen
= sizeof(anc_buf
);
633 #endif /* __linux__ */
636 len_last
= iov
[0].iov_len
;
637 ret
= recvmsg(sock
, &msg
, 0);
639 iov
[0].iov_base
+= ret
;
640 iov
[0].iov_len
-= ret
;
641 assert(ret
<= len_last
);
643 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
645 PERROR("recvmsg fds");
647 } else if (ret
> 0) {
650 /* Else ret = 0 meaning an orderly shutdown. */
653 if (msg
.msg_flags
& MSG_CTRUNC
) {
654 fprintf(stderr
, "Error: Control message truncated.\n");
659 cmptr
= CMSG_FIRSTHDR(&msg
);
661 fprintf(stderr
, "Error: Invalid control message header\n");
666 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
667 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
668 fprintf(stderr
, "Didn't received any credentials\n");
673 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
674 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
675 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
680 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
681 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
685 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
691 #error "Please implement credential support for your OS."
692 #endif /* __linux__ */
699 * Set socket option to use credentials passing.
703 int lttcomm_setsockopt_creds_unix_sock(int sock
)
707 /* Set socket for credentials retrieval */
708 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
710 PERROR("setsockopt creds unix sock");
714 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
716 int lttcomm_setsockopt_creds_unix_sock(int sock
)
721 #error "Please implement credential support for your OS."
722 #endif /* __linux__ */