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.
27 #include <sys/types.h>
31 #include <common/common.h>
36 * Connect to unix socket using the path name.
39 int lttcomm_connect_unix_sock(const char *pathname
)
41 struct sockaddr_un sun
;
42 int fd
, ret
, closeret
;
44 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
51 memset(&sun
, 0, sizeof(sun
));
52 sun
.sun_family
= AF_UNIX
;
53 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
54 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
56 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
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 sun
;
88 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &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 sun
;
117 /* Create server socket */
118 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
123 memset(&sun
, 0, sizeof(sun
));
124 sun
.sun_family
= AF_UNIX
;
125 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
126 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
128 /* Unlink the old file if present */
129 (void) unlink(pathname
);
130 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
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
= recvmsg(sock
, &msg
, MSG_NOSIGNAL
);
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
, void *buf
, size_t len
)
215 memset(&msg
, 0, sizeof(msg
));
217 iov
[0].iov_base
= 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
;
399 #endif /* __linux__ */
401 memset(&msg
, 0, sizeof(msg
));
402 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
404 iov
[0].iov_base
= buf
;
405 iov
[0].iov_len
= len
;
410 msg
.msg_control
= (caddr_t
) anc_buf
;
411 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
413 cmptr
= CMSG_FIRSTHDR(&msg
);
417 cmptr
->cmsg_level
= SOL_SOCKET
;
418 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
419 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
421 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
423 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
424 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
425 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
426 #endif /* __linux__ */
429 ret
= sendmsg(sock
, &msg
, 0);
430 } while (ret
< 0 && errno
== EINTR
);
433 * Only warn about EPIPE when quiet mode is deactivated.
434 * We consider EPIPE as expected.
436 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
444 * Recv a message accompanied with credentials from a unix socket.
446 * Returns the size of received data, or negative error value.
449 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
450 lttng_sock_cred
*creds
)
457 struct cmsghdr
*cmptr
;
458 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
459 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
460 #endif /* __linux__ */
462 memset(&msg
, 0, sizeof(msg
));
470 /* Prepare to receive the structures */
471 iov
[0].iov_base
= buf
;
472 iov
[0].iov_len
= len
;
477 msg
.msg_control
= anc_buf
;
478 msg
.msg_controllen
= sizeof(anc_buf
);
479 #endif /* __linux__ */
482 len_last
= iov
[0].iov_len
;
483 ret
= recvmsg(sock
, &msg
, 0);
485 iov
[0].iov_base
+= ret
;
486 iov
[0].iov_len
-= ret
;
487 assert(ret
<= len_last
);
489 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
491 PERROR("recvmsg fds");
493 } else if (ret
> 0) {
496 /* Else ret = 0 meaning an orderly shutdown. */
499 if (msg
.msg_flags
& MSG_CTRUNC
) {
500 fprintf(stderr
, "Error: Control message truncated.\n");
505 cmptr
= CMSG_FIRSTHDR(&msg
);
507 fprintf(stderr
, "Error: Invalid control message header\n");
512 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
513 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
514 fprintf(stderr
, "Didn't received any credentials\n");
519 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
520 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
521 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
526 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
527 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
531 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
537 #error "Please implement credential support for your OS."
538 #endif /* __linux__ */
545 * Set socket option to use credentials passing.
549 int lttcomm_setsockopt_creds_unix_sock(int sock
)
553 /* Set socket for credentials retrieval */
554 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
556 PERROR("setsockopt creds unix sock");
560 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
562 int lttcomm_setsockopt_creds_unix_sock(int sock
)
567 #error "Please implement credential support for your OS."
568 #endif /* __linux__ */