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/defaults.h>
31 #include <common/error.h>
36 * Connect to unix socket using the path name.
38 __attribute__((visibility("hidden")))
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.
80 __attribute__((visibility("hidden")))
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
100 __attribute__((visibility("hidden")))
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
));
133 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
135 __attribute__((visibility("hidden")))
136 int lttcomm_listen_unix_sock(int sock
)
140 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
149 * Receive data of size len in put that data into the buf param. Using recvmsg
152 * Return the size of received data.
154 __attribute__((visibility("hidden")))
155 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
162 memset(&msg
, 0, sizeof(msg
));
164 iov
[0].iov_base
= buf
;
165 iov
[0].iov_len
= len
;
170 len_last
= iov
[0].iov_len
;
171 ret
= recvmsg(sock
, &msg
, 0);
173 iov
[0].iov_base
+= ret
;
174 iov
[0].iov_len
-= ret
;
175 assert(ret
<= len_last
);
177 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
180 } else if (ret
> 0) {
183 /* Else ret = 0 meaning an orderly shutdown. */
189 * Send buf data of size len. Using sendmsg API.
191 * Return the size of sent data.
193 __attribute__((visibility("hidden")))
194 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
200 memset(&msg
, 0, sizeof(msg
));
202 iov
[0].iov_base
= buf
;
203 iov
[0].iov_len
= len
;
207 ret
= sendmsg(sock
, &msg
, 0);
210 * Only warn about EPIPE when quiet mode is deactivated.
211 * We consider EPIPE as expected.
213 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
222 * Shutdown cleanly a unix socket.
224 __attribute__((visibility("hidden")))
225 int lttcomm_close_unix_sock(int sock
)
229 /* Shutdown receptions and transmissions */
230 ret
= shutdown(sock
, SHUT_RDWR
);
235 closeret
= close(sock
);
244 * Send a message accompanied by fd(s) over a unix socket.
246 * Returns the size of data sent, or negative error value.
248 __attribute__((visibility("hidden")))
249 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
252 struct cmsghdr
*cmptr
;
255 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
256 char tmp
[CMSG_SPACE(sizeof_fds
)];
259 memset(&msg
, 0, sizeof(msg
));
260 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
262 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
265 msg
.msg_control
= (caddr_t
)tmp
;
266 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
268 cmptr
= CMSG_FIRSTHDR(&msg
);
269 cmptr
->cmsg_level
= SOL_SOCKET
;
270 cmptr
->cmsg_type
= SCM_RIGHTS
;
271 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
272 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
273 /* Sum of the length of all control messages in the buffer: */
274 msg
.msg_controllen
= cmptr
->cmsg_len
;
276 iov
[0].iov_base
= &dummy
;
282 ret
= sendmsg(sock
, &msg
, 0);
283 } while (ret
< 0 && errno
== EINTR
);
286 * Only warn about EPIPE when quiet mode is deactivated.
287 * We consider EPIPE as expected.
289 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
297 * Recv a message accompanied by fd(s) from a unix socket.
299 * Returns the size of received data, or negative error value.
301 * Expect at most "nb_fd" file descriptors. Returns the number of fd
302 * actually received in nb_fd.
304 __attribute__((visibility("hidden")))
305 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
309 struct cmsghdr
*cmsg
;
310 size_t sizeof_fds
= nb_fd
* sizeof(int);
311 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
315 memset(&msg
, 0, sizeof(msg
));
317 /* Prepare to receive the structures */
318 iov
[0].iov_base
= &dummy
;
322 msg
.msg_control
= recv_fd
;
323 msg
.msg_controllen
= sizeof(recv_fd
);
326 ret
= recvmsg(sock
, &msg
, 0);
327 } while (ret
< 0 && errno
== EINTR
);
329 PERROR("recvmsg fds");
333 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
337 if (msg
.msg_flags
& MSG_CTRUNC
) {
338 fprintf(stderr
, "Error: Control message truncated.\n");
342 cmsg
= CMSG_FIRSTHDR(&msg
);
344 fprintf(stderr
, "Error: Invalid control message header\n");
348 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
349 fprintf(stderr
, "Didn't received any fd\n");
353 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
354 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
355 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
359 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
366 * Send a message with credentials over a unix socket.
368 * Returns the size of data sent, or negative error value.
370 __attribute__((visibility("hidden")))
371 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
377 struct cmsghdr
*cmptr
;
378 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
379 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
380 lttng_sock_cred
*creds
;
381 #endif /* __linux__ */
383 memset(&msg
, 0, sizeof(msg
));
384 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
386 iov
[0].iov_base
= buf
;
387 iov
[0].iov_len
= len
;
392 msg
.msg_control
= (caddr_t
) anc_buf
;
393 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
395 cmptr
= CMSG_FIRSTHDR(&msg
);
396 cmptr
->cmsg_level
= SOL_SOCKET
;
397 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
398 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
400 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
402 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
403 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
404 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
405 #endif /* __linux__ */
408 ret
= sendmsg(sock
, &msg
, 0);
409 } while (ret
< 0 && errno
== EINTR
);
412 * Only warn about EPIPE when quiet mode is deactivated.
413 * We consider EPIPE as expected.
415 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
423 * Recv a message accompanied with credentials from a unix socket.
425 * Returns the size of received data, or negative error value.
427 __attribute__((visibility("hidden")))
428 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
429 lttng_sock_cred
*creds
)
435 struct cmsghdr
*cmptr
;
436 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
437 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
438 #endif /* __linux__ */
440 memset(&msg
, 0, sizeof(msg
));
448 /* Prepare to receive the structures */
449 iov
[0].iov_base
= buf
;
450 iov
[0].iov_len
= len
;
455 msg
.msg_control
= anc_buf
;
456 msg
.msg_controllen
= sizeof(anc_buf
);
457 #endif /* __linux__ */
460 ret
= recvmsg(sock
, &msg
, 0);
461 } while (ret
< 0 && errno
== EINTR
);
463 PERROR("recvmsg fds");
468 if (msg
.msg_flags
& MSG_CTRUNC
) {
469 fprintf(stderr
, "Error: Control message truncated.\n");
474 cmptr
= CMSG_FIRSTHDR(&msg
);
476 fprintf(stderr
, "Error: Invalid control message header\n");
481 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
482 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
483 fprintf(stderr
, "Didn't received any credentials\n");
488 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
489 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
490 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
495 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
496 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
500 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
506 #error "Please implement credential support for your OS."
507 #endif /* __linux__ */
514 * Set socket option to use credentials passing.
517 __attribute__((visibility("hidden")))
518 int lttcomm_setsockopt_creds_unix_sock(int sock
)
522 /* Set socket for credentials retrieval */
523 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
525 PERROR("setsockopt creds unix sock");
529 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
530 __attribute__((visibility("hidden")))
531 int lttcomm_setsockopt_creds_unix_sock(int sock
)
536 #error "Please implement credential support for your OS."
537 #endif /* __linux__ */
540 * Set socket reciving timeout.
542 __attribute__((visibility("hidden")))
543 int lttcomm_setsockopt_rcv_timeout(int sock
, unsigned int sec
)
551 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
553 PERROR("setsockopt SO_RCVTIMEO");
561 * Set socket sending timeout.
563 __attribute__((visibility("hidden")))
564 int lttcomm_setsockopt_snd_timeout(int sock
, unsigned int sec
)
572 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
574 PERROR("setsockopt SO_SNDTIMEO");