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
)
161 memset(&msg
, 0, sizeof(msg
));
163 iov
[0].iov_base
= buf
;
164 iov
[0].iov_len
= len
;
169 ret
= recvmsg(sock
, &msg
, MSG_WAITALL
);
170 } while (ret
< 0 && errno
== EINTR
);
179 * Send buf data of size len. Using sendmsg API.
181 * Return the size of sent data.
183 __attribute__((visibility("hidden")))
184 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
190 memset(&msg
, 0, sizeof(msg
));
192 iov
[0].iov_base
= buf
;
193 iov
[0].iov_len
= len
;
197 ret
= sendmsg(sock
, &msg
, 0);
200 * Only warn about EPIPE when quiet mode is deactivated.
201 * We consider EPIPE as expected.
203 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
212 * Shutdown cleanly a unix socket.
214 __attribute__((visibility("hidden")))
215 int lttcomm_close_unix_sock(int sock
)
219 /* Shutdown receptions and transmissions */
220 ret
= shutdown(sock
, SHUT_RDWR
);
225 closeret
= close(sock
);
234 * Send a message accompanied by fd(s) over a unix socket.
236 * Returns the size of data sent, or negative error value.
238 __attribute__((visibility("hidden")))
239 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
242 struct cmsghdr
*cmptr
;
245 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
246 char tmp
[CMSG_SPACE(sizeof_fds
)];
249 memset(&msg
, 0, sizeof(msg
));
251 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
254 msg
.msg_control
= (caddr_t
)tmp
;
255 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
257 cmptr
= CMSG_FIRSTHDR(&msg
);
258 cmptr
->cmsg_level
= SOL_SOCKET
;
259 cmptr
->cmsg_type
= SCM_RIGHTS
;
260 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
261 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
262 /* Sum of the length of all control messages in the buffer: */
263 msg
.msg_controllen
= cmptr
->cmsg_len
;
265 iov
[0].iov_base
= &dummy
;
271 ret
= sendmsg(sock
, &msg
, 0);
272 } while (ret
< 0 && errno
== EINTR
);
275 * Only warn about EPIPE when quiet mode is deactivated.
276 * We consider EPIPE as expected.
278 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
286 * Recv a message accompanied by fd(s) from a unix socket.
288 * Returns the size of received data, or negative error value.
290 * Expect at most "nb_fd" file descriptors. Returns the number of fd
291 * actually received in nb_fd.
293 __attribute__((visibility("hidden")))
294 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
298 struct cmsghdr
*cmsg
;
299 size_t sizeof_fds
= nb_fd
* sizeof(int);
300 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
304 memset(&msg
, 0, sizeof(msg
));
306 /* Prepare to receive the structures */
307 iov
[0].iov_base
= &dummy
;
311 msg
.msg_control
= recv_fd
;
312 msg
.msg_controllen
= sizeof(recv_fd
);
315 ret
= recvmsg(sock
, &msg
, 0);
316 } while (ret
< 0 && errno
== EINTR
);
318 PERROR("recvmsg fds");
322 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
326 if (msg
.msg_flags
& MSG_CTRUNC
) {
327 fprintf(stderr
, "Error: Control message truncated.\n");
331 cmsg
= CMSG_FIRSTHDR(&msg
);
333 fprintf(stderr
, "Error: Invalid control message header\n");
337 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
338 fprintf(stderr
, "Didn't received any fd\n");
342 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
343 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
344 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
348 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
355 * Send a message with credentials over a unix socket.
357 * Returns the size of data sent, or negative error value.
359 __attribute__((visibility("hidden")))
360 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
366 struct cmsghdr
*cmptr
;
367 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
368 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
369 lttng_sock_cred
*creds
;
370 #endif /* __linux__ */
372 memset(&msg
, 0, sizeof(msg
));
374 iov
[0].iov_base
= buf
;
375 iov
[0].iov_len
= len
;
380 msg
.msg_control
= (caddr_t
) anc_buf
;
381 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
383 cmptr
= CMSG_FIRSTHDR(&msg
);
384 cmptr
->cmsg_level
= SOL_SOCKET
;
385 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
386 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
388 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
390 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
391 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
392 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
393 #endif /* __linux__ */
396 ret
= sendmsg(sock
, &msg
, 0);
397 } while (ret
< 0 && errno
== EINTR
);
400 * Only warn about EPIPE when quiet mode is deactivated.
401 * We consider EPIPE as expected.
403 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
411 * Recv a message accompanied with credentials from a unix socket.
413 * Returns the size of received data, or negative error value.
415 __attribute__((visibility("hidden")))
416 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
417 lttng_sock_cred
*creds
)
423 struct cmsghdr
*cmptr
;
424 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
425 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
426 #endif /* __linux__ */
428 memset(&msg
, 0, sizeof(msg
));
436 /* Prepare to receive the structures */
437 iov
[0].iov_base
= buf
;
438 iov
[0].iov_len
= len
;
443 msg
.msg_control
= anc_buf
;
444 msg
.msg_controllen
= sizeof(anc_buf
);
445 #endif /* __linux__ */
448 ret
= recvmsg(sock
, &msg
, 0);
449 } while (ret
< 0 && errno
== EINTR
);
451 PERROR("recvmsg fds");
456 if (msg
.msg_flags
& MSG_CTRUNC
) {
457 fprintf(stderr
, "Error: Control message truncated.\n");
462 cmptr
= CMSG_FIRSTHDR(&msg
);
464 fprintf(stderr
, "Error: Invalid control message header\n");
469 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
470 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
471 fprintf(stderr
, "Didn't received any credentials\n");
476 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
477 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
478 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
483 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
484 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
488 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
494 #error "Please implement credential support for your OS."
495 #endif /* __linux__ */
502 * Set socket option to use credentials passing.
505 __attribute__((visibility("hidden")))
506 int lttcomm_setsockopt_creds_unix_sock(int sock
)
510 /* Set socket for credentials retrieval */
511 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
513 PERROR("setsockopt creds unix sock");
517 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
518 __attribute__((visibility("hidden")))
519 int lttcomm_setsockopt_creds_unix_sock(int sock
)
524 #error "Please implement credential support for your OS."
525 #endif /* __linux__ */