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 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
101 int lttcomm_create_unix_sock(const char *pathname
)
103 struct sockaddr_un sun
;
107 /* Create server socket */
108 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
113 memset(&sun
, 0, sizeof(sun
));
114 sun
.sun_family
= AF_UNIX
;
115 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
116 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
118 /* Unlink the old file if present */
119 (void) unlink(pathname
);
120 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
131 PERROR("close create unix sock");
138 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
141 int lttcomm_listen_unix_sock(int sock
)
145 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
154 * Receive data of size len in put that data into the buf param. Using recvmsg
157 * Return the size of received data.
160 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
167 memset(&msg
, 0, sizeof(msg
));
169 iov
[0].iov_base
= buf
;
170 iov
[0].iov_len
= len
;
175 len_last
= iov
[0].iov_len
;
176 ret
= recvmsg(sock
, &msg
, 0);
178 iov
[0].iov_base
+= ret
;
179 iov
[0].iov_len
-= ret
;
180 assert(ret
<= len_last
);
182 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
185 } else if (ret
> 0) {
188 /* Else ret = 0 meaning an orderly shutdown. */
194 * Send buf data of size len. Using sendmsg API.
196 * Return the size of sent data.
199 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
205 memset(&msg
, 0, sizeof(msg
));
207 iov
[0].iov_base
= buf
;
208 iov
[0].iov_len
= len
;
212 ret
= sendmsg(sock
, &msg
, 0);
215 * Only warn about EPIPE when quiet mode is deactivated.
216 * We consider EPIPE as expected.
218 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
227 * Shutdown cleanly a unix socket.
230 int lttcomm_close_unix_sock(int sock
)
234 /* Shutdown receptions and transmissions */
235 ret
= shutdown(sock
, SHUT_RDWR
);
240 closeret
= close(sock
);
249 * Send a message accompanied by fd(s) over a unix socket.
251 * Returns the size of data sent, or negative error value.
254 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
257 struct cmsghdr
*cmptr
;
260 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
261 char tmp
[CMSG_SPACE(sizeof_fds
)];
264 memset(&msg
, 0, sizeof(msg
));
265 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
267 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
270 msg
.msg_control
= (caddr_t
)tmp
;
271 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
273 cmptr
= CMSG_FIRSTHDR(&msg
);
274 cmptr
->cmsg_level
= SOL_SOCKET
;
275 cmptr
->cmsg_type
= SCM_RIGHTS
;
276 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
277 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
278 /* Sum of the length of all control messages in the buffer: */
279 msg
.msg_controllen
= cmptr
->cmsg_len
;
281 iov
[0].iov_base
= &dummy
;
287 ret
= sendmsg(sock
, &msg
, 0);
288 } while (ret
< 0 && errno
== EINTR
);
291 * Only warn about EPIPE when quiet mode is deactivated.
292 * We consider EPIPE as expected.
294 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
302 * Recv a message accompanied by fd(s) from a unix socket.
304 * Returns the size of received data, or negative error value.
306 * Expect at most "nb_fd" file descriptors. Returns the number of fd
307 * actually received in nb_fd.
310 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
314 struct cmsghdr
*cmsg
;
315 size_t sizeof_fds
= nb_fd
* sizeof(int);
316 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
320 memset(&msg
, 0, sizeof(msg
));
322 /* Prepare to receive the structures */
323 iov
[0].iov_base
= &dummy
;
327 msg
.msg_control
= recv_fd
;
328 msg
.msg_controllen
= sizeof(recv_fd
);
331 ret
= recvmsg(sock
, &msg
, 0);
332 } while (ret
< 0 && errno
== EINTR
);
334 PERROR("recvmsg fds");
338 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
342 if (msg
.msg_flags
& MSG_CTRUNC
) {
343 fprintf(stderr
, "Error: Control message truncated.\n");
347 cmsg
= CMSG_FIRSTHDR(&msg
);
349 fprintf(stderr
, "Error: Invalid control message header\n");
353 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
354 fprintf(stderr
, "Didn't received any fd\n");
358 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
359 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
360 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
364 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
371 * Send a message with credentials over a unix socket.
373 * Returns the size of data sent, or negative error value.
376 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
382 struct cmsghdr
*cmptr
;
383 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
384 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
385 lttng_sock_cred
*creds
;
386 #endif /* __linux__ */
388 memset(&msg
, 0, sizeof(msg
));
389 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
391 iov
[0].iov_base
= buf
;
392 iov
[0].iov_len
= len
;
397 msg
.msg_control
= (caddr_t
) anc_buf
;
398 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
400 cmptr
= CMSG_FIRSTHDR(&msg
);
401 cmptr
->cmsg_level
= SOL_SOCKET
;
402 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
403 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
405 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
407 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
408 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
409 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
410 #endif /* __linux__ */
413 ret
= sendmsg(sock
, &msg
, 0);
414 } while (ret
< 0 && errno
== EINTR
);
417 * Only warn about EPIPE when quiet mode is deactivated.
418 * We consider EPIPE as expected.
420 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
428 * Recv a message accompanied with credentials from a unix socket.
430 * Returns the size of received data, or negative error value.
433 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
434 lttng_sock_cred
*creds
)
441 struct cmsghdr
*cmptr
;
442 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
443 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
444 #endif /* __linux__ */
446 memset(&msg
, 0, sizeof(msg
));
454 /* Prepare to receive the structures */
455 iov
[0].iov_base
= buf
;
456 iov
[0].iov_len
= len
;
461 msg
.msg_control
= anc_buf
;
462 msg
.msg_controllen
= sizeof(anc_buf
);
463 #endif /* __linux__ */
466 len_last
= iov
[0].iov_len
;
467 ret
= recvmsg(sock
, &msg
, 0);
469 iov
[0].iov_base
+= ret
;
470 iov
[0].iov_len
-= ret
;
471 assert(ret
<= len_last
);
473 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
475 PERROR("recvmsg fds");
477 } else if (ret
> 0) {
480 /* Else ret = 0 meaning an orderly shutdown. */
483 if (msg
.msg_flags
& MSG_CTRUNC
) {
484 fprintf(stderr
, "Error: Control message truncated.\n");
489 cmptr
= CMSG_FIRSTHDR(&msg
);
491 fprintf(stderr
, "Error: Invalid control message header\n");
496 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
497 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
498 fprintf(stderr
, "Didn't received any credentials\n");
503 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
504 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
505 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
510 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
511 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
515 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
521 #error "Please implement credential support for your OS."
522 #endif /* __linux__ */
529 * Set socket option to use credentials passing.
533 int lttcomm_setsockopt_creds_unix_sock(int sock
)
537 /* Set socket for credentials retrieval */
538 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
540 PERROR("setsockopt creds unix sock");
544 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
546 int lttcomm_setsockopt_creds_unix_sock(int sock
)
551 #error "Please implement credential support for your OS."
552 #endif /* __linux__ */