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 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
51 memset(&s_un
, 0, sizeof(s_un
));
52 s_un
.sun_family
= AF_UNIX
;
53 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
54 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
56 ret
= connect(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
59 * Don't print message on connect error, because connect is used in
60 * normal execution to detect if sessiond is alive.
77 * Do an accept(2) on the sock and return the new file descriptor. The socket
78 * MUST be bind(2) before.
81 int lttcomm_accept_unix_sock(int sock
)
84 struct sockaddr_un s_un
;
88 new_fd
= accept(sock
, (struct sockaddr
*) &s_un
, &len
);
97 int lttcomm_create_anon_unix_socketpair(int *fds
)
99 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
100 PERROR("socketpair");
107 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
111 int lttcomm_create_unix_sock(const char *pathname
)
113 struct sockaddr_un s_un
;
117 /* Create server socket */
118 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
123 memset(&s_un
, 0, sizeof(s_un
));
124 s_un
.sun_family
= AF_UNIX
;
125 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
126 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
128 /* Unlink the old file if present */
129 (void) unlink(pathname
);
130 ret
= bind(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
141 PERROR("close create unix sock");
148 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
151 int lttcomm_listen_unix_sock(int sock
)
155 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
164 * Receive data of size len in put that data into the buf param. Using recvmsg
167 * Return the size of received data.
170 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
177 memset(&msg
, 0, sizeof(msg
));
179 iov
[0].iov_base
= buf
;
180 iov
[0].iov_len
= len
;
185 len_last
= iov
[0].iov_len
;
186 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
188 iov
[0].iov_base
+= ret
;
189 iov
[0].iov_len
-= ret
;
190 assert(ret
<= len_last
);
192 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
195 } else if (ret
> 0) {
198 /* Else ret = 0 meaning an orderly shutdown. */
204 * Send buf data of size len. Using sendmsg API.
206 * Return the size of sent data.
209 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
215 memset(&msg
, 0, sizeof(msg
));
217 iov
[0].iov_base
= (void *) buf
;
218 iov
[0].iov_len
= len
;
222 ret
= sendmsg(sock
, &msg
, 0);
225 * Only warn about EPIPE when quiet mode is deactivated.
226 * We consider EPIPE as expected.
228 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
237 * Shutdown cleanly a unix socket.
240 int lttcomm_close_unix_sock(int sock
)
244 /* Shutdown receptions and transmissions */
245 ret
= shutdown(sock
, SHUT_RDWR
);
250 closeret
= close(sock
);
259 * Send a message accompanied by fd(s) over a unix socket.
261 * Returns the size of data sent, or negative error value.
264 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
267 struct cmsghdr
*cmptr
;
270 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
271 char tmp
[CMSG_SPACE(sizeof_fds
)];
274 memset(&msg
, 0, sizeof(msg
));
275 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
277 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
280 msg
.msg_control
= (caddr_t
)tmp
;
281 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
283 cmptr
= CMSG_FIRSTHDR(&msg
);
287 cmptr
->cmsg_level
= SOL_SOCKET
;
288 cmptr
->cmsg_type
= SCM_RIGHTS
;
289 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
290 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
291 /* Sum of the length of all control messages in the buffer: */
292 msg
.msg_controllen
= cmptr
->cmsg_len
;
294 iov
[0].iov_base
= &dummy
;
300 ret
= sendmsg(sock
, &msg
, 0);
301 } while (ret
< 0 && errno
== EINTR
);
304 * Only warn about EPIPE when quiet mode is deactivated.
305 * We consider EPIPE as expected.
307 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
315 * Recv a message accompanied by fd(s) from a unix socket.
317 * Returns the size of received data, or negative error value.
319 * Expect at most "nb_fd" file descriptors. Returns the number of fd
320 * actually received in nb_fd.
323 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
327 struct cmsghdr
*cmsg
;
328 size_t sizeof_fds
= nb_fd
* sizeof(int);
329 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
333 memset(&msg
, 0, sizeof(msg
));
335 /* Prepare to receive the structures */
336 iov
[0].iov_base
= &dummy
;
340 msg
.msg_control
= recv_fd
;
341 msg
.msg_controllen
= sizeof(recv_fd
);
344 ret
= recvmsg(sock
, &msg
, 0);
345 } while (ret
< 0 && errno
== EINTR
);
347 PERROR("recvmsg fds");
351 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
355 if (msg
.msg_flags
& MSG_CTRUNC
) {
356 fprintf(stderr
, "Error: Control message truncated.\n");
360 cmsg
= CMSG_FIRSTHDR(&msg
);
362 fprintf(stderr
, "Error: Invalid control message header\n");
366 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
367 fprintf(stderr
, "Didn't received any fd\n");
371 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
372 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
373 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
377 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
384 * Send a message with credentials over a unix socket.
386 * Returns the size of data sent, or negative error value.
389 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
395 struct cmsghdr
*cmptr
;
396 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
397 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
398 lttng_sock_cred
*creds
;
400 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
401 #endif /* __linux__ */
403 memset(&msg
, 0, sizeof(msg
));
405 iov
[0].iov_base
= buf
;
406 iov
[0].iov_len
= len
;
411 msg
.msg_control
= (caddr_t
) anc_buf
;
412 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
414 cmptr
= CMSG_FIRSTHDR(&msg
);
418 cmptr
->cmsg_level
= SOL_SOCKET
;
419 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
420 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
422 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
424 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
425 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
426 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
427 #endif /* __linux__ */
430 ret
= sendmsg(sock
, &msg
, 0);
431 } while (ret
< 0 && errno
== EINTR
);
434 * Only warn about EPIPE when quiet mode is deactivated.
435 * We consider EPIPE as expected.
437 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
445 * Recv a message accompanied with credentials from a unix socket.
447 * Returns the size of received data, or negative error value.
450 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
451 lttng_sock_cred
*creds
)
458 struct cmsghdr
*cmptr
;
459 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
460 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
461 #endif /* __linux__ */
463 memset(&msg
, 0, sizeof(msg
));
471 /* Prepare to receive the structures */
472 iov
[0].iov_base
= buf
;
473 iov
[0].iov_len
= len
;
478 msg
.msg_control
= anc_buf
;
479 msg
.msg_controllen
= sizeof(anc_buf
);
480 #endif /* __linux__ */
483 len_last
= iov
[0].iov_len
;
484 ret
= recvmsg(sock
, &msg
, 0);
486 iov
[0].iov_base
+= ret
;
487 iov
[0].iov_len
-= ret
;
488 assert(ret
<= len_last
);
490 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
492 PERROR("recvmsg fds");
494 } else if (ret
> 0) {
497 /* Else ret = 0 meaning an orderly shutdown. */
500 if (msg
.msg_flags
& MSG_CTRUNC
) {
501 fprintf(stderr
, "Error: Control message truncated.\n");
506 cmptr
= CMSG_FIRSTHDR(&msg
);
508 fprintf(stderr
, "Error: Invalid control message header\n");
513 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
514 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
515 fprintf(stderr
, "Didn't received any credentials\n");
520 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
521 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
522 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
527 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
528 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
532 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
538 #error "Please implement credential support for your OS."
539 #endif /* __linux__ */
546 * Set socket option to use credentials passing.
550 int lttcomm_setsockopt_creds_unix_sock(int sock
)
554 /* Set socket for credentials retrieval */
555 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
557 PERROR("setsockopt creds unix sock");
561 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
563 int lttcomm_setsockopt_creds_unix_sock(int sock
)
568 #error "Please implement credential support for your OS."
569 #endif /* __linux__ */