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 * Send buf data of size len. Using sendmsg API.
222 * Return the size of sent data.
225 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
231 memset(&msg
, 0, sizeof(msg
));
233 iov
[0].iov_base
= (void *) buf
;
234 iov
[0].iov_len
= len
;
238 ret
= sendmsg(sock
, &msg
, 0);
241 * Only warn about EPIPE when quiet mode is deactivated.
242 * We consider EPIPE as expected.
244 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
253 * Shutdown cleanly a unix socket.
256 int lttcomm_close_unix_sock(int sock
)
260 /* Shutdown receptions and transmissions */
261 ret
= shutdown(sock
, SHUT_RDWR
);
266 closeret
= close(sock
);
275 * Send a message accompanied by fd(s) over a unix socket.
277 * Returns the size of data sent, or negative error value.
280 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
283 struct cmsghdr
*cmptr
;
286 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
287 char tmp
[CMSG_SPACE(sizeof_fds
)];
290 memset(&msg
, 0, sizeof(msg
));
291 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
293 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
296 msg
.msg_control
= (caddr_t
)tmp
;
297 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
299 cmptr
= CMSG_FIRSTHDR(&msg
);
303 cmptr
->cmsg_level
= SOL_SOCKET
;
304 cmptr
->cmsg_type
= SCM_RIGHTS
;
305 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
306 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
307 /* Sum of the length of all control messages in the buffer: */
308 msg
.msg_controllen
= cmptr
->cmsg_len
;
310 iov
[0].iov_base
= &dummy
;
316 ret
= sendmsg(sock
, &msg
, 0);
317 } while (ret
< 0 && errno
== EINTR
);
320 * Only warn about EPIPE when quiet mode is deactivated.
321 * We consider EPIPE as expected.
323 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
331 * Recv a message accompanied by fd(s) from a unix socket.
333 * Returns the size of received data, or negative error value.
335 * Expect at most "nb_fd" file descriptors. Returns the number of fd
336 * actually received in nb_fd.
339 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
343 struct cmsghdr
*cmsg
;
344 size_t sizeof_fds
= nb_fd
* sizeof(int);
345 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
349 memset(&msg
, 0, sizeof(msg
));
351 /* Prepare to receive the structures */
352 iov
[0].iov_base
= &dummy
;
356 msg
.msg_control
= recv_fd
;
357 msg
.msg_controllen
= sizeof(recv_fd
);
360 ret
= recvmsg(sock
, &msg
, 0);
361 } while (ret
< 0 && errno
== EINTR
);
363 PERROR("recvmsg fds");
367 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
371 if (msg
.msg_flags
& MSG_CTRUNC
) {
372 fprintf(stderr
, "Error: Control message truncated.\n");
376 cmsg
= CMSG_FIRSTHDR(&msg
);
378 fprintf(stderr
, "Error: Invalid control message header\n");
382 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
383 fprintf(stderr
, "Didn't received any fd\n");
387 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
388 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
389 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
393 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
400 * Send a message with credentials over a unix socket.
402 * Returns the size of data sent, or negative error value.
405 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
411 struct cmsghdr
*cmptr
;
412 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
413 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
414 lttng_sock_cred
*creds
;
416 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
417 #endif /* __linux__ */
419 memset(&msg
, 0, sizeof(msg
));
421 iov
[0].iov_base
= buf
;
422 iov
[0].iov_len
= len
;
427 msg
.msg_control
= (caddr_t
) anc_buf
;
428 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
430 cmptr
= CMSG_FIRSTHDR(&msg
);
434 cmptr
->cmsg_level
= SOL_SOCKET
;
435 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
436 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
438 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
440 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
441 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
442 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
443 #endif /* __linux__ */
446 ret
= sendmsg(sock
, &msg
, 0);
447 } while (ret
< 0 && errno
== EINTR
);
450 * Only warn about EPIPE when quiet mode is deactivated.
451 * We consider EPIPE as expected.
453 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
461 * Recv a message accompanied with credentials from a unix socket.
463 * Returns the size of received data, or negative error value.
466 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
467 lttng_sock_cred
*creds
)
474 struct cmsghdr
*cmptr
;
475 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
476 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
477 #endif /* __linux__ */
479 memset(&msg
, 0, sizeof(msg
));
487 /* Prepare to receive the structures */
488 iov
[0].iov_base
= buf
;
489 iov
[0].iov_len
= len
;
494 msg
.msg_control
= anc_buf
;
495 msg
.msg_controllen
= sizeof(anc_buf
);
496 #endif /* __linux__ */
499 len_last
= iov
[0].iov_len
;
500 ret
= recvmsg(sock
, &msg
, 0);
502 iov
[0].iov_base
+= ret
;
503 iov
[0].iov_len
-= ret
;
504 assert(ret
<= len_last
);
506 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
508 PERROR("recvmsg fds");
510 } else if (ret
> 0) {
513 /* Else ret = 0 meaning an orderly shutdown. */
516 if (msg
.msg_flags
& MSG_CTRUNC
) {
517 fprintf(stderr
, "Error: Control message truncated.\n");
522 cmptr
= CMSG_FIRSTHDR(&msg
);
524 fprintf(stderr
, "Error: Invalid control message header\n");
529 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
530 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
531 fprintf(stderr
, "Didn't received any credentials\n");
536 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
537 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
538 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
543 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
544 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
548 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
554 #error "Please implement credential support for your OS."
555 #endif /* __linux__ */
562 * Set socket option to use credentials passing.
566 int lttcomm_setsockopt_creds_unix_sock(int sock
)
570 /* Set socket for credentials retrieval */
571 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
573 PERROR("setsockopt creds unix sock");
577 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
579 int lttcomm_setsockopt_creds_unix_sock(int sock
)
584 #error "Please implement credential support for your OS."
585 #endif /* __linux__ */