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
);
284 cmptr
->cmsg_level
= SOL_SOCKET
;
285 cmptr
->cmsg_type
= SCM_RIGHTS
;
286 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
287 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
288 /* Sum of the length of all control messages in the buffer: */
289 msg
.msg_controllen
= cmptr
->cmsg_len
;
291 iov
[0].iov_base
= &dummy
;
297 ret
= sendmsg(sock
, &msg
, 0);
298 } while (ret
< 0 && errno
== EINTR
);
301 * Only warn about EPIPE when quiet mode is deactivated.
302 * We consider EPIPE as expected.
304 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
312 * Recv a message accompanied by fd(s) from a unix socket.
314 * Returns the size of received data, or negative error value.
316 * Expect at most "nb_fd" file descriptors. Returns the number of fd
317 * actually received in nb_fd.
320 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
324 struct cmsghdr
*cmsg
;
325 size_t sizeof_fds
= nb_fd
* sizeof(int);
326 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
330 memset(&msg
, 0, sizeof(msg
));
332 /* Prepare to receive the structures */
333 iov
[0].iov_base
= &dummy
;
337 msg
.msg_control
= recv_fd
;
338 msg
.msg_controllen
= sizeof(recv_fd
);
341 ret
= recvmsg(sock
, &msg
, 0);
342 } while (ret
< 0 && errno
== EINTR
);
344 PERROR("recvmsg fds");
348 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
352 if (msg
.msg_flags
& MSG_CTRUNC
) {
353 fprintf(stderr
, "Error: Control message truncated.\n");
357 cmsg
= CMSG_FIRSTHDR(&msg
);
359 fprintf(stderr
, "Error: Invalid control message header\n");
363 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
364 fprintf(stderr
, "Didn't received any fd\n");
368 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
369 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
370 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
374 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
381 * Send a message with credentials over a unix socket.
383 * Returns the size of data sent, or negative error value.
386 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
392 struct cmsghdr
*cmptr
;
393 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
394 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
395 lttng_sock_cred
*creds
;
396 #endif /* __linux__ */
398 memset(&msg
, 0, sizeof(msg
));
399 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
401 iov
[0].iov_base
= buf
;
402 iov
[0].iov_len
= len
;
407 msg
.msg_control
= (caddr_t
) anc_buf
;
408 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
410 cmptr
= CMSG_FIRSTHDR(&msg
);
411 cmptr
->cmsg_level
= SOL_SOCKET
;
412 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
413 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
415 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
417 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
418 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
419 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
420 #endif /* __linux__ */
423 ret
= sendmsg(sock
, &msg
, 0);
424 } while (ret
< 0 && errno
== EINTR
);
427 * Only warn about EPIPE when quiet mode is deactivated.
428 * We consider EPIPE as expected.
430 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
438 * Recv a message accompanied with credentials from a unix socket.
440 * Returns the size of received data, or negative error value.
443 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
444 lttng_sock_cred
*creds
)
451 struct cmsghdr
*cmptr
;
452 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
453 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
454 #endif /* __linux__ */
456 memset(&msg
, 0, sizeof(msg
));
464 /* Prepare to receive the structures */
465 iov
[0].iov_base
= buf
;
466 iov
[0].iov_len
= len
;
471 msg
.msg_control
= anc_buf
;
472 msg
.msg_controllen
= sizeof(anc_buf
);
473 #endif /* __linux__ */
476 len_last
= iov
[0].iov_len
;
477 ret
= recvmsg(sock
, &msg
, 0);
479 iov
[0].iov_base
+= ret
;
480 iov
[0].iov_len
-= ret
;
481 assert(ret
<= len_last
);
483 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
485 PERROR("recvmsg fds");
487 } else if (ret
> 0) {
490 /* Else ret = 0 meaning an orderly shutdown. */
493 if (msg
.msg_flags
& MSG_CTRUNC
) {
494 fprintf(stderr
, "Error: Control message truncated.\n");
499 cmptr
= CMSG_FIRSTHDR(&msg
);
501 fprintf(stderr
, "Error: Invalid control message header\n");
506 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
507 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
508 fprintf(stderr
, "Didn't received any credentials\n");
513 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
514 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
515 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
520 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
521 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
525 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
531 #error "Please implement credential support for your OS."
532 #endif /* __linux__ */
539 * Set socket option to use credentials passing.
543 int lttcomm_setsockopt_creds_unix_sock(int sock
)
547 /* Set socket for credentials retrieval */
548 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
550 PERROR("setsockopt creds unix sock");
554 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
556 int lttcomm_setsockopt_creds_unix_sock(int sock
)
561 #error "Please implement credential support for your OS."
562 #endif /* __linux__ */