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 s_un
;
41 int fd
, ret
, closeret
;
43 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
50 memset(&s_un
, 0, sizeof(s_un
));
51 s_un
.sun_family
= AF_UNIX
;
52 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
53 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
55 ret
= connect(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
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 s_un
;
87 new_fd
= accept(sock
, (struct sockaddr
*) &s_un
, &len
);
96 int lttcomm_create_anon_unix_socketpair(int *fds
)
98 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
106 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
110 int lttcomm_create_unix_sock(const char *pathname
)
112 struct sockaddr_un s_un
;
116 /* Create server socket */
117 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
122 memset(&s_un
, 0, sizeof(s_un
));
123 s_un
.sun_family
= AF_UNIX
;
124 strncpy(s_un
.sun_path
, pathname
, sizeof(s_un
.sun_path
));
125 s_un
.sun_path
[sizeof(s_un
.sun_path
) - 1] = '\0';
127 /* Unlink the old file if present */
128 (void) unlink(pathname
);
129 ret
= bind(fd
, (struct sockaddr
*) &s_un
, sizeof(s_un
));
140 PERROR("close create unix sock");
147 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
150 int lttcomm_listen_unix_sock(int sock
)
154 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
163 * Receive data of size len in put that data into the buf param. Using recvmsg
166 * Return the size of received data.
169 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
176 memset(&msg
, 0, sizeof(msg
));
178 iov
[0].iov_base
= buf
;
179 iov
[0].iov_len
= len
;
184 len_last
= iov
[0].iov_len
;
185 ret
= lttng_recvmsg_nosigpipe(sock
, &msg
);
187 iov
[0].iov_base
+= ret
;
188 iov
[0].iov_len
-= ret
;
189 assert(ret
<= len_last
);
191 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
194 } else if (ret
> 0) {
197 /* Else ret = 0 meaning an orderly shutdown. */
203 * Send buf data of size len. Using sendmsg API.
205 * Return the size of sent data.
208 ssize_t
lttcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
214 memset(&msg
, 0, sizeof(msg
));
216 iov
[0].iov_base
= (void *) buf
;
217 iov
[0].iov_len
= len
;
221 ret
= sendmsg(sock
, &msg
, 0);
224 * Only warn about EPIPE when quiet mode is deactivated.
225 * We consider EPIPE as expected.
227 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
236 * Shutdown cleanly a unix socket.
239 int lttcomm_close_unix_sock(int sock
)
243 /* Shutdown receptions and transmissions */
244 ret
= shutdown(sock
, SHUT_RDWR
);
249 closeret
= close(sock
);
258 * Send a message accompanied by fd(s) over a unix socket.
260 * Returns the size of data sent, or negative error value.
263 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
266 struct cmsghdr
*cmptr
;
269 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
270 char tmp
[CMSG_SPACE(sizeof_fds
)];
273 memset(&msg
, 0, sizeof(msg
));
274 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
276 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
279 msg
.msg_control
= (caddr_t
)tmp
;
280 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
282 cmptr
= CMSG_FIRSTHDR(&msg
);
286 cmptr
->cmsg_level
= SOL_SOCKET
;
287 cmptr
->cmsg_type
= SCM_RIGHTS
;
288 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
289 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
290 /* Sum of the length of all control messages in the buffer: */
291 msg
.msg_controllen
= cmptr
->cmsg_len
;
293 iov
[0].iov_base
= &dummy
;
299 ret
= sendmsg(sock
, &msg
, 0);
300 } while (ret
< 0 && errno
== EINTR
);
303 * Only warn about EPIPE when quiet mode is deactivated.
304 * We consider EPIPE as expected.
306 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
314 * Recv a message accompanied by fd(s) from a unix socket.
316 * Returns the size of received data, or negative error value.
318 * Expect at most "nb_fd" file descriptors. Returns the number of fd
319 * actually received in nb_fd.
322 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
326 struct cmsghdr
*cmsg
;
327 size_t sizeof_fds
= nb_fd
* sizeof(int);
328 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
332 memset(&msg
, 0, sizeof(msg
));
334 /* Prepare to receive the structures */
335 iov
[0].iov_base
= &dummy
;
339 msg
.msg_control
= recv_fd
;
340 msg
.msg_controllen
= sizeof(recv_fd
);
343 ret
= recvmsg(sock
, &msg
, 0);
344 } while (ret
< 0 && errno
== EINTR
);
346 PERROR("recvmsg fds");
350 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
354 if (msg
.msg_flags
& MSG_CTRUNC
) {
355 fprintf(stderr
, "Error: Control message truncated.\n");
359 cmsg
= CMSG_FIRSTHDR(&msg
);
361 fprintf(stderr
, "Error: Invalid control message header\n");
365 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
366 fprintf(stderr
, "Didn't received any fd\n");
370 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
371 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
372 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
376 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
383 * Send a message with credentials over a unix socket.
385 * Returns the size of data sent, or negative error value.
388 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
394 struct cmsghdr
*cmptr
;
395 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
396 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
397 lttng_sock_cred
*creds
;
399 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
400 #endif /* __linux__ */
402 memset(&msg
, 0, sizeof(msg
));
404 iov
[0].iov_base
= buf
;
405 iov
[0].iov_len
= len
;
410 msg
.msg_control
= (caddr_t
) anc_buf
;
411 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
413 cmptr
= CMSG_FIRSTHDR(&msg
);
417 cmptr
->cmsg_level
= SOL_SOCKET
;
418 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
419 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
421 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
423 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
424 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
425 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
426 #endif /* __linux__ */
429 ret
= sendmsg(sock
, &msg
, 0);
430 } while (ret
< 0 && errno
== EINTR
);
433 * Only warn about EPIPE when quiet mode is deactivated.
434 * We consider EPIPE as expected.
436 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
444 * Recv a message accompanied with credentials from a unix socket.
446 * Returns the size of received data, or negative error value.
449 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
450 lttng_sock_cred
*creds
)
457 struct cmsghdr
*cmptr
;
458 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
459 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
460 #endif /* __linux__ */
462 memset(&msg
, 0, sizeof(msg
));
470 /* Prepare to receive the structures */
471 iov
[0].iov_base
= buf
;
472 iov
[0].iov_len
= len
;
477 msg
.msg_control
= anc_buf
;
478 msg
.msg_controllen
= sizeof(anc_buf
);
479 #endif /* __linux__ */
482 len_last
= iov
[0].iov_len
;
483 ret
= recvmsg(sock
, &msg
, 0);
485 iov
[0].iov_base
+= ret
;
486 iov
[0].iov_len
-= ret
;
487 assert(ret
<= len_last
);
489 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
491 PERROR("recvmsg fds");
493 } else if (ret
> 0) {
496 /* Else ret = 0 meaning an orderly shutdown. */
499 if (msg
.msg_flags
& MSG_CTRUNC
) {
500 fprintf(stderr
, "Error: Control message truncated.\n");
505 cmptr
= CMSG_FIRSTHDR(&msg
);
507 fprintf(stderr
, "Error: Invalid control message header\n");
512 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
513 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
514 fprintf(stderr
, "Didn't received any credentials\n");
519 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
520 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
521 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
526 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
527 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
531 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
537 #error "Please implement credential support for your OS."
538 #endif /* __linux__ */
545 * Set socket option to use credentials passing.
549 int lttcomm_setsockopt_creds_unix_sock(int sock
)
553 /* Set socket for credentials retrieval */
554 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
556 PERROR("setsockopt creds unix sock");
560 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
562 int lttcomm_setsockopt_creds_unix_sock(int sock
)
567 #error "Please implement credential support for your OS."
568 #endif /* __linux__ */