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
)
439 struct cmsghdr
*cmptr
;
440 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
441 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
442 #endif /* __linux__ */
444 memset(&msg
, 0, sizeof(msg
));
452 /* Prepare to receive the structures */
453 iov
[0].iov_base
= buf
;
454 iov
[0].iov_len
= len
;
459 msg
.msg_control
= anc_buf
;
460 msg
.msg_controllen
= sizeof(anc_buf
);
461 #endif /* __linux__ */
464 ret
= recvmsg(sock
, &msg
, 0);
465 } while (ret
< 0 && errno
== EINTR
);
467 PERROR("recvmsg fds");
472 if (msg
.msg_flags
& MSG_CTRUNC
) {
473 fprintf(stderr
, "Error: Control message truncated.\n");
478 cmptr
= CMSG_FIRSTHDR(&msg
);
480 fprintf(stderr
, "Error: Invalid control message header\n");
485 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
486 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
487 fprintf(stderr
, "Didn't received any credentials\n");
492 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
493 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
494 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
499 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
500 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
504 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
510 #error "Please implement credential support for your OS."
511 #endif /* __linux__ */
518 * Set socket option to use credentials passing.
522 int lttcomm_setsockopt_creds_unix_sock(int sock
)
526 /* Set socket for credentials retrieval */
527 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
529 PERROR("setsockopt creds unix sock");
533 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
535 int lttcomm_setsockopt_creds_unix_sock(int sock
)
540 #error "Please implement credential support for your OS."
541 #endif /* __linux__ */