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 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.
79 int lttcomm_accept_unix_sock(int sock
)
82 struct sockaddr_un sun
;
86 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
95 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
98 int lttcomm_create_unix_sock(const char *pathname
)
100 struct sockaddr_un sun
;
104 /* Create server socket */
105 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
110 memset(&sun
, 0, sizeof(sun
));
111 sun
.sun_family
= AF_UNIX
;
112 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
113 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
115 /* Unlink the old file if present */
116 (void) unlink(pathname
);
117 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
130 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
132 int lttcomm_listen_unix_sock(int sock
)
136 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
145 * Receive data of size len in put that data into the buf param. Using recvmsg
148 * Return the size of received data.
150 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
156 memset(&msg
, 0, sizeof(msg
));
158 iov
[0].iov_base
= buf
;
159 iov
[0].iov_len
= len
;
164 ret
= recvmsg(sock
, &msg
, MSG_WAITALL
);
165 } while (ret
< 0 && errno
== EINTR
);
174 * Send buf data of size len. Using sendmsg API.
176 * Return the size of sent data.
178 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
184 memset(&msg
, 0, sizeof(msg
));
186 iov
[0].iov_base
= buf
;
187 iov
[0].iov_len
= len
;
191 ret
= sendmsg(sock
, &msg
, 0);
194 * Only warn about EPIPE when quiet mode is deactivated.
195 * We consider EPIPE as expected.
197 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
206 * Shutdown cleanly a unix socket.
208 int lttcomm_close_unix_sock(int sock
)
212 /* Shutdown receptions and transmissions */
213 ret
= shutdown(sock
, SHUT_RDWR
);
218 closeret
= close(sock
);
227 * Send a message accompanied by fd(s) over a unix socket.
229 * Returns the size of data sent, or negative error value.
231 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
234 struct cmsghdr
*cmptr
;
237 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
238 char tmp
[CMSG_SPACE(sizeof_fds
)];
241 memset(&msg
, 0, sizeof(msg
));
243 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
246 msg
.msg_control
= (caddr_t
)tmp
;
247 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
249 cmptr
= CMSG_FIRSTHDR(&msg
);
250 cmptr
->cmsg_level
= SOL_SOCKET
;
251 cmptr
->cmsg_type
= SCM_RIGHTS
;
252 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
253 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
254 /* Sum of the length of all control messages in the buffer: */
255 msg
.msg_controllen
= cmptr
->cmsg_len
;
257 iov
[0].iov_base
= &dummy
;
263 ret
= sendmsg(sock
, &msg
, 0);
264 } while (ret
< 0 && errno
== EINTR
);
267 * Only warn about EPIPE when quiet mode is deactivated.
268 * We consider EPIPE as expected.
270 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
278 * Recv a message accompanied by fd(s) from a unix socket.
280 * Returns the size of received data, or negative error value.
282 * Expect at most "nb_fd" file descriptors. Returns the number of fd
283 * actually received in nb_fd.
285 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
289 struct cmsghdr
*cmsg
;
290 size_t sizeof_fds
= nb_fd
* sizeof(int);
291 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
295 memset(&msg
, 0, sizeof(msg
));
297 /* Prepare to receive the structures */
298 iov
[0].iov_base
= &dummy
;
302 msg
.msg_control
= recv_fd
;
303 msg
.msg_controllen
= sizeof(recv_fd
);
306 ret
= recvmsg(sock
, &msg
, 0);
307 } while (ret
< 0 && errno
== EINTR
);
309 PERROR("recvmsg fds");
313 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
317 if (msg
.msg_flags
& MSG_CTRUNC
) {
318 fprintf(stderr
, "Error: Control message truncated.\n");
322 cmsg
= CMSG_FIRSTHDR(&msg
);
324 fprintf(stderr
, "Error: Invalid control message header\n");
328 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
329 fprintf(stderr
, "Didn't received any fd\n");
333 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
334 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
335 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
339 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
346 * Send a message with credentials over a unix socket.
348 * Returns the size of data sent, or negative error value.
350 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
356 struct cmsghdr
*cmptr
;
357 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
358 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
359 lttng_sock_cred
*creds
;
360 #endif /* __linux__ */
362 memset(&msg
, 0, sizeof(msg
));
364 iov
[0].iov_base
= buf
;
365 iov
[0].iov_len
= len
;
370 msg
.msg_control
= (caddr_t
) anc_buf
;
371 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
373 cmptr
= CMSG_FIRSTHDR(&msg
);
374 cmptr
->cmsg_level
= SOL_SOCKET
;
375 cmptr
->cmsg_type
= LTTNG_SOCK_CREDS
;
376 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
378 creds
= (lttng_sock_cred
*) CMSG_DATA(cmptr
);
380 LTTNG_SOCK_SET_UID_CRED(creds
, geteuid());
381 LTTNG_SOCK_SET_GID_CRED(creds
, getegid());
382 LTTNG_SOCK_SET_PID_CRED(creds
, getpid());
383 #endif /* __linux__ */
386 ret
= sendmsg(sock
, &msg
, 0);
387 } while (ret
< 0 && errno
== EINTR
);
390 * Only warn about EPIPE when quiet mode is deactivated.
391 * We consider EPIPE as expected.
393 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
401 * Recv a message accompanied with credentials from a unix socket.
403 * Returns the size of received data, or negative error value.
405 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
406 lttng_sock_cred
*creds
)
412 struct cmsghdr
*cmptr
;
413 size_t sizeof_cred
= sizeof(lttng_sock_cred
);
414 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
415 #endif /* __linux__ */
417 memset(&msg
, 0, sizeof(msg
));
425 /* Prepare to receive the structures */
426 iov
[0].iov_base
= buf
;
427 iov
[0].iov_len
= len
;
432 msg
.msg_control
= anc_buf
;
433 msg
.msg_controllen
= sizeof(anc_buf
);
434 #endif /* __linux__ */
437 ret
= recvmsg(sock
, &msg
, 0);
438 } while (ret
< 0 && errno
== EINTR
);
440 PERROR("recvmsg fds");
445 if (msg
.msg_flags
& MSG_CTRUNC
) {
446 fprintf(stderr
, "Error: Control message truncated.\n");
451 cmptr
= CMSG_FIRSTHDR(&msg
);
453 fprintf(stderr
, "Error: Invalid control message header\n");
458 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
459 cmptr
->cmsg_type
!= LTTNG_SOCK_CREDS
) {
460 fprintf(stderr
, "Didn't received any credentials\n");
465 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
466 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
467 (size_t) cmptr
->cmsg_len
, (size_t) CMSG_LEN(sizeof_cred
));
472 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
473 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
477 peer_ret
= getpeereid(sock
, &creds
->uid
, &creds
->gid
);
483 #error "Please implement credential support for your OS."
484 #endif /* __linux__ */
491 * Set socket option to use credentials passing.
494 int lttcomm_setsockopt_creds_unix_sock(int sock
)
498 /* Set socket for credentials retrieval */
499 ret
= setsockopt(sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
501 PERROR("setsockopt creds unix sock");
505 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
506 int lttcomm_setsockopt_creds_unix_sock(int sock
)
511 #error "Please implement credential support for your OS."
512 #endif /* __linux__ */