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
));
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.
303 __attribute__((visibility("hidden")))
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.
369 __attribute__((visibility("hidden")))
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
));
384 iov
[0].iov_base
= buf
;
385 iov
[0].iov_len
= len
;
390 msg
.msg_control
= (caddr_t
) anc_buf
;
391 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
393 cmptr
= CMSG_FIRSTHDR(&msg
);
394 cmptr
->cmsg_level
= SOL_SOCKET
;
395 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
396 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
398 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
400 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
401 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
402 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
403 #endif /* __linux__ */
406 ret
= sendmsg(sock
, &msg
, 0);
407 } while (ret
< 0 && errno
== EINTR
);
410 * Only warn about EPIPE when quiet mode is deactivated.
411 * We consider EPIPE as expected.
413 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
421 * Recv a message accompanied with credentials from a unix socket.
423 * Returns the size of received data, or negative error value.
425 __attribute__((visibility("hidden")))
426 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
427 lttng_sock_cred
*creds
)
433 struct cmsghdr
*cmptr
;
434 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
435 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
436 #endif /* __linux__ */
438 memset(&msg
, 0, sizeof(msg
));
446 /* Prepare to receive the structures */
447 iov
[0].iov_base
= buf
;
448 iov
[0].iov_len
= len
;
453 msg
.msg_control
= anc_buf
;
454 msg
.msg_controllen
= sizeof(anc_buf
);
455 #endif /* __linux__ */
458 ret
= recvmsg(sock
, &msg
, 0);
459 } while (ret
< 0 && errno
== EINTR
);
461 PERROR("recvmsg fds");
466 if (msg
.msg_flags
& MSG_CTRUNC
) {
467 fprintf(stderr
, "Error: Control message truncated.\n");
472 cmptr
= CMSG_FIRSTHDR(&msg
);
474 fprintf(stderr
, "Error: Invalid control message header\n");
479 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
480 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
481 fprintf(stderr
, "Didn't received any credentials\n");
486 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
487 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
488 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
493 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
494 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
498 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
504 #error "Please implement credential support for your OS."
505 #endif /* __linux__ */
512 * Set socket option to use credentials passing.
515 __attribute__((visibility("hidden")))
516 int lttcomm_setsockopt_creds_unix_sock(int sock
)
520 /* Set socket for credentials retrieval */
521 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
523 PERROR("setsockopt creds unix sock");
527 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
528 __attribute__((visibility("hidden")))
529 int lttcomm_setsockopt_creds_unix_sock(int sock
)
534 #error "Please implement credential support for your OS."
535 #endif /* __linux__ */
538 * Set socket reciving timeout.
540 __attribute__((visibility("hidden")))
541 int lttcomm_setsockopt_rcv_timeout(int sock
, unsigned int sec
)
549 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
551 PERROR("setsockopt SO_RCVTIMEO");
559 * Set socket sending timeout.
561 __attribute__((visibility("hidden")))
562 int lttcomm_setsockopt_snd_timeout(int sock
, unsigned int sec
)
570 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
572 PERROR("setsockopt SO_SNDTIMEO");