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
));
130 PERROR("close create unix sock");
137 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
140 int lttcomm_listen_unix_sock(int sock
)
144 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
153 * Receive data of size len in put that data into the buf param. Using recvmsg
156 * Return the size of received data.
159 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
166 memset(&msg
, 0, sizeof(msg
));
168 iov
[0].iov_base
= buf
;
169 iov
[0].iov_len
= len
;
174 len_last
= iov
[0].iov_len
;
175 ret
= recvmsg(sock
, &msg
, 0);
177 iov
[0].iov_base
+= ret
;
178 iov
[0].iov_len
-= ret
;
179 assert(ret
<= len_last
);
181 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
184 } else if (ret
> 0) {
187 /* Else ret = 0 meaning an orderly shutdown. */
193 * Send buf data of size len. Using sendmsg API.
195 * Return the size of sent data.
198 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
204 memset(&msg
, 0, sizeof(msg
));
206 iov
[0].iov_base
= buf
;
207 iov
[0].iov_len
= len
;
211 ret
= sendmsg(sock
, &msg
, 0);
214 * Only warn about EPIPE when quiet mode is deactivated.
215 * We consider EPIPE as expected.
217 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
226 * Shutdown cleanly a unix socket.
229 int lttcomm_close_unix_sock(int sock
)
233 /* Shutdown receptions and transmissions */
234 ret
= shutdown(sock
, SHUT_RDWR
);
239 closeret
= close(sock
);
248 * Send a message accompanied by fd(s) over a unix socket.
250 * Returns the size of data sent, or negative error value.
253 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
256 struct cmsghdr
*cmptr
;
259 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
260 char tmp
[CMSG_SPACE(sizeof_fds
)];
263 memset(&msg
, 0, sizeof(msg
));
264 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
266 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
269 msg
.msg_control
= (caddr_t
)tmp
;
270 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
272 cmptr
= CMSG_FIRSTHDR(&msg
);
273 cmptr
->cmsg_level
= SOL_SOCKET
;
274 cmptr
->cmsg_type
= SCM_RIGHTS
;
275 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
276 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
277 /* Sum of the length of all control messages in the buffer: */
278 msg
.msg_controllen
= cmptr
->cmsg_len
;
280 iov
[0].iov_base
= &dummy
;
286 ret
= sendmsg(sock
, &msg
, 0);
287 } while (ret
< 0 && errno
== EINTR
);
290 * Only warn about EPIPE when quiet mode is deactivated.
291 * We consider EPIPE as expected.
293 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
301 * Recv a message accompanied by fd(s) from a unix socket.
303 * Returns the size of received data, or negative error value.
305 * Expect at most "nb_fd" file descriptors. Returns the number of fd
306 * actually received in nb_fd.
309 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
313 struct cmsghdr
*cmsg
;
314 size_t sizeof_fds
= nb_fd
* sizeof(int);
315 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
319 memset(&msg
, 0, sizeof(msg
));
321 /* Prepare to receive the structures */
322 iov
[0].iov_base
= &dummy
;
326 msg
.msg_control
= recv_fd
;
327 msg
.msg_controllen
= sizeof(recv_fd
);
330 ret
= recvmsg(sock
, &msg
, 0);
331 } while (ret
< 0 && errno
== EINTR
);
333 PERROR("recvmsg fds");
337 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
341 if (msg
.msg_flags
& MSG_CTRUNC
) {
342 fprintf(stderr
, "Error: Control message truncated.\n");
346 cmsg
= CMSG_FIRSTHDR(&msg
);
348 fprintf(stderr
, "Error: Invalid control message header\n");
352 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
353 fprintf(stderr
, "Didn't received any fd\n");
357 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
358 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
359 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
363 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
370 * Send a message with credentials over a unix socket.
372 * Returns the size of data sent, or negative error value.
375 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
381 struct cmsghdr
*cmptr
;
382 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
383 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
384 lttng_sock_cred
*creds
;
385 #endif /* __linux__ */
387 memset(&msg
, 0, sizeof(msg
));
388 memset(anc_buf
, 0, CMSG_SPACE(sizeof_cred
) * sizeof(char));
390 iov
[0].iov_base
= buf
;
391 iov
[0].iov_len
= len
;
396 msg
.msg_control
= (caddr_t
) anc_buf
;
397 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
399 cmptr
= CMSG_FIRSTHDR(&msg
);
400 cmptr
->cmsg_level
= SOL_SOCKET
;
401 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
402 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
404 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
406 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
407 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
408 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
409 #endif /* __linux__ */
412 ret
= sendmsg(sock
, &msg
, 0);
413 } while (ret
< 0 && errno
== EINTR
);
416 * Only warn about EPIPE when quiet mode is deactivated.
417 * We consider EPIPE as expected.
419 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
427 * Recv a message accompanied with credentials from a unix socket.
429 * Returns the size of received data, or negative error value.
432 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
433 lttng_sock_cred
*creds
)
440 struct cmsghdr
*cmptr
;
441 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
442 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
443 #endif /* __linux__ */
445 memset(&msg
, 0, sizeof(msg
));
453 /* Prepare to receive the structures */
454 iov
[0].iov_base
= buf
;
455 iov
[0].iov_len
= len
;
460 msg
.msg_control
= anc_buf
;
461 msg
.msg_controllen
= sizeof(anc_buf
);
462 #endif /* __linux__ */
465 len_last
= iov
[0].iov_len
;
466 ret
= recvmsg(sock
, &msg
, 0);
468 iov
[0].iov_base
+= ret
;
469 iov
[0].iov_len
-= ret
;
470 assert(ret
<= len_last
);
472 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
474 PERROR("recvmsg fds");
476 } else if (ret
> 0) {
479 /* Else ret = 0 meaning an orderly shutdown. */
482 if (msg
.msg_flags
& MSG_CTRUNC
) {
483 fprintf(stderr
, "Error: Control message truncated.\n");
488 cmptr
= CMSG_FIRSTHDR(&msg
);
490 fprintf(stderr
, "Error: Invalid control message header\n");
495 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
496 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
497 fprintf(stderr
, "Didn't received any credentials\n");
502 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
503 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
504 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
509 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
510 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
514 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
520 #error "Please implement credential support for your OS."
521 #endif /* __linux__ */
528 * Set socket option to use credentials passing.
532 int lttcomm_setsockopt_creds_unix_sock(int sock
)
536 /* Set socket for credentials retrieval */
537 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
539 PERROR("setsockopt creds unix sock");
543 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
545 int lttcomm_setsockopt_creds_unix_sock(int sock
)
550 #error "Please implement credential support for your OS."
551 #endif /* __linux__ */