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/common.h>
35 * Connect to unix socket using the path name.
38 int lttcomm_connect_unix_sock(const char *pathname
)
40 struct sockaddr_un sun
;
41 int fd
, ret
, closeret
;
43 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
50 memset(&sun
, 0, sizeof(sun
));
51 sun
.sun_family
= AF_UNIX
;
52 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
53 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
55 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
58 * Don't print message on connect error, because connect is used in
59 * normal execution to detect if sessiond is alive.
76 * Do an accept(2) on the sock and return the new file descriptor. The socket
77 * MUST be bind(2) before.
80 int lttcomm_accept_unix_sock(int sock
)
83 struct sockaddr_un sun
;
87 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
96 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
100 int lttcomm_create_unix_sock(const char *pathname
)
102 struct sockaddr_un sun
;
106 /* Create server socket */
107 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
112 memset(&sun
, 0, sizeof(sun
));
113 sun
.sun_family
= AF_UNIX
;
114 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
115 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
117 /* Unlink the old file if present */
118 (void) unlink(pathname
);
119 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
132 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
135 int lttcomm_listen_unix_sock(int sock
)
139 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
148 * Receive data of size len in put that data into the buf param. Using recvmsg
151 * Return the size of received data.
154 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 len_last
= iov
[0].iov_len
;
170 ret
= recvmsg(sock
, &msg
, 0);
172 iov
[0].iov_base
+= ret
;
173 iov
[0].iov_len
-= ret
;
174 assert(ret
<= len_last
);
176 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
179 } else if (ret
> 0) {
182 /* Else ret = 0 meaning an orderly shutdown. */
188 * Send buf data of size len. Using sendmsg API.
190 * Return the size of sent data.
193 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
199 memset(&msg
, 0, sizeof(msg
));
201 iov
[0].iov_base
= buf
;
202 iov
[0].iov_len
= len
;
206 ret
= sendmsg(sock
, &msg
, 0);
209 * Only warn about EPIPE when quiet mode is deactivated.
210 * We consider EPIPE as expected.
212 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
221 * Shutdown cleanly a unix socket.
224 int lttcomm_close_unix_sock(int sock
)
228 /* Shutdown receptions and transmissions */
229 ret
= shutdown(sock
, SHUT_RDWR
);
234 closeret
= close(sock
);
243 * Send a message accompanied by fd(s) over a unix socket.
245 * Returns the size of data sent, or negative error value.
248 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
251 struct cmsghdr
*cmptr
;
254 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
255 char tmp
[CMSG_SPACE(sizeof_fds
)];
258 memset(&msg
, 0, sizeof(msg
));
259 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
261 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
264 msg
.msg_control
= (caddr_t
)tmp
;
265 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
267 cmptr
= CMSG_FIRSTHDR(&msg
);
268 cmptr
->cmsg_level
= SOL_SOCKET
;
269 cmptr
->cmsg_type
= SCM_RIGHTS
;
270 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
271 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
272 /* Sum of the length of all control messages in the buffer: */
273 msg
.msg_controllen
= cmptr
->cmsg_len
;
275 iov
[0].iov_base
= &dummy
;
281 ret
= sendmsg(sock
, &msg
, 0);
282 } while (ret
< 0 && errno
== EINTR
);
285 * Only warn about EPIPE when quiet mode is deactivated.
286 * We consider EPIPE as expected.
288 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
296 * Recv a message accompanied by fd(s) from a unix socket.
298 * Returns the size of received data, or negative error value.
300 * Expect at most "nb_fd" file descriptors. Returns the number of fd
301 * actually received in nb_fd.
304 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
308 struct cmsghdr
*cmsg
;
309 size_t sizeof_fds
= nb_fd
* sizeof(int);
310 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
314 memset(&msg
, 0, sizeof(msg
));
316 /* Prepare to receive the structures */
317 iov
[0].iov_base
= &dummy
;
321 msg
.msg_control
= recv_fd
;
322 msg
.msg_controllen
= sizeof(recv_fd
);
325 ret
= recvmsg(sock
, &msg
, 0);
326 } while (ret
< 0 && errno
== EINTR
);
328 PERROR("recvmsg fds");
332 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
336 if (msg
.msg_flags
& MSG_CTRUNC
) {
337 fprintf(stderr
, "Error: Control message truncated.\n");
341 cmsg
= CMSG_FIRSTHDR(&msg
);
343 fprintf(stderr
, "Error: Invalid control message header\n");
347 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
348 fprintf(stderr
, "Didn't received any fd\n");
352 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
353 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
354 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
358 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
365 * Send a message with credentials over a unix socket.
367 * Returns the size of data sent, or negative error value.
370 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
376 struct cmsghdr
*cmptr
;
377 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
378 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
379 lttng_sock_cred
*creds
;
380 #endif /* __linux__ */
382 memset(&msg
, 0, sizeof(msg
));
383 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
385 iov
[0].iov_base
= buf
;
386 iov
[0].iov_len
= len
;
391 msg
.msg_control
= (caddr_t
) anc_buf
;
392 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
394 cmptr
= CMSG_FIRSTHDR(&msg
);
395 cmptr
->cmsg_level
= SOL_SOCKET
;
396 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
397 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
399 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
401 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
402 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
403 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
404 #endif /* __linux__ */
407 ret
= sendmsg(sock
, &msg
, 0);
408 } while (ret
< 0 && errno
== EINTR
);
411 * Only warn about EPIPE when quiet mode is deactivated.
412 * We consider EPIPE as expected.
414 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
422 * Recv a message accompanied with credentials from a unix socket.
424 * Returns the size of received data, or negative error value.
427 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
428 lttng_sock_cred
*creds
)
434 struct cmsghdr
*cmptr
;
435 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
436 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
437 #endif /* __linux__ */
439 memset(&msg
, 0, sizeof(msg
));
447 /* Prepare to receive the structures */
448 iov
[0].iov_base
= buf
;
449 iov
[0].iov_len
= len
;
454 msg
.msg_control
= anc_buf
;
455 msg
.msg_controllen
= sizeof(anc_buf
);
456 #endif /* __linux__ */
459 ret
= recvmsg(sock
, &msg
, 0);
460 } while (ret
< 0 && errno
== EINTR
);
462 PERROR("recvmsg fds");
467 if (msg
.msg_flags
& MSG_CTRUNC
) {
468 fprintf(stderr
, "Error: Control message truncated.\n");
473 cmptr
= CMSG_FIRSTHDR(&msg
);
475 fprintf(stderr
, "Error: Invalid control message header\n");
480 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
481 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
482 fprintf(stderr
, "Didn't received any credentials\n");
487 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
488 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
489 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
494 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
495 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
499 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
505 #error "Please implement credential support for your OS."
506 #endif /* __linux__ */
513 * Set socket option to use credentials passing.
517 int lttcomm_setsockopt_creds_unix_sock(int sock
)
521 /* Set socket for credentials retrieval */
522 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
524 PERROR("setsockopt creds unix sock");
528 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
530 int lttcomm_setsockopt_creds_unix_sock(int sock
)
535 #error "Please implement credential support for your OS."
536 #endif /* __linux__ */
539 * Set socket reciving timeout.
542 int lttcomm_setsockopt_rcv_timeout(int sock
, unsigned int sec
)
550 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
552 PERROR("setsockopt SO_RCVTIMEO");
560 * Set socket sending timeout.
563 int lttcomm_setsockopt_snd_timeout(int sock
, unsigned int sec
)
571 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
573 PERROR("setsockopt SO_SNDTIMEO");