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 it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; only version 2 of the License.
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 with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
31 #include <common/defaults.h>
32 #include <common/error.h>
34 #include "sessiond-comm.h"
37 * Human readable error message.
39 static const char *lttcomm_readable_code
[] = {
40 [ LTTCOMM_ERR_INDEX(LTTCOMM_OK
) ] = "Success",
41 [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR
) ] = "Unknown error",
42 [ LTTCOMM_ERR_INDEX(LTTCOMM_UND
) ] = "Undefined command",
43 [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED
) ] = "Not implemented",
44 [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN
) ] = "Unknown tracing domain",
45 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION
) ] = "No session found",
46 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL
) ] = "Unable to list traceable apps",
47 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS
) ] = "No traceable apps found",
48 [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND
) ] = "Session name not found",
49 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE
) ] = "No trace found",
50 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL
) ] = "Fatal error of the session daemon",
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_DIR_FAIL
) ] = "Create directory failed",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL
) ] = "Start trace failed",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL
) ] = "Stop trace failed",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE
) ] = "App is not traceable",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS
) ] = "A session MUST be selected",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS
) ] = "Session name already exist",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL
) ] = "Unable to connect to Unix socket",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND
) ] = "Application not found",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM
) ] = "Permission denied",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA
) ] = "Kernel tracer not available",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_VERSION
) ] = "Kernel tracer version is not compatible",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST
) ] = "Kernel event already exists",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL
) ] = "Kernel create session failed",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL
) ] = "Kernel create channel failed",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND
) ] = "Kernel channel not found",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL
) ] = "Disable kernel channel failed",
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL
) ] = "Enable kernel channel failed",
68 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL
) ] = "Add kernel context failed",
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL
) ] = "Enable kernel event failed",
70 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL
) ] = "Disable kernel event failed",
71 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL
) ] = "Opening metadata failed",
72 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL
) ] = "Starting kernel trace failed",
73 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL
) ] = "Stoping kernel trace failed",
74 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL
) ] = "Kernel consumer start failed",
75 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL
) ] = "Kernel create stream failed",
76 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL
) ] = "Kernel trace directory creation failed",
77 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST
) ] = "Kernel trace directory already exist",
78 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION
) ] = "No kernel session found",
79 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL
) ] = "Listing kernel events failed",
80 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CALIBRATE_FAIL
) ] = "UST calibration failed",
81 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_VERSION
) ] = "UST tracer version is not compatible",
82 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL
) ] = "UST create session failed",
83 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL
) ] = "UST create channel failed",
84 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST
) ] = "UST channel already exist",
85 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND
) ] = "UST channel not found",
86 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL
) ] = "Disable UST channel failed",
87 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL
) ] = "Enable UST channel failed",
88 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL
) ] = "Add UST context failed",
89 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL
) ] = "Enable UST event failed",
90 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL
) ] = "Disable UST event failed",
91 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL
) ] = "Opening metadata failed",
92 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL
) ] = "Starting UST trace failed",
93 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL
) ] = "Stoping UST trace failed",
94 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL
) ] = "64-bit UST consumer start failed",
95 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL
) ] = "32-bit UST consumer start failed",
96 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL
) ] = "UST create stream failed",
97 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_FAIL
) ] = "UST trace directory creation failed",
98 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_EXIST
) ] = "UST trace directory already exist",
99 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_NO_SESSION
) ] = "No UST session found",
100 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_LIST_FAIL
) ] = "Listing UST events failed",
101 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST
) ] = "UST event already exist",
102 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND
)] = "UST event not found",
103 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST
)] = "UST context already exist",
104 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_INVAL
)] = "UST invalid context",
105 [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY
) ] = "consumerd command socket ready",
106 [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD
) ] = "consumerd success on receiving fds",
107 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD
) ] = "consumerd error on receiving fds",
108 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD
) ] = "consumerd error on receiving command",
109 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR
) ] = "consumerd error in polling thread",
110 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL
) ] = "consumerd polling on closed fd",
111 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP
) ] = "consumerd all fd hung up",
112 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS
) ] = "consumerd exiting normally",
113 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE
) ] = "consumerd exiting on error",
114 [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR
) ] = "consumerd error opening the tracefile",
115 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF
) ] = "consumerd splice EBADF",
116 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL
) ] = "consumerd splice EINVAL",
117 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM
) ] = "consumerd splice ENOMEM",
118 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE
) ] = "consumerd splice ESPIPE",
119 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT
) ] = "Event not found",
123 * Return ptr to string representing a human readable error code from the
124 * lttcomm_return_code enum.
126 * These code MUST be negative in other to treat that as an error value.
128 const char *lttcomm_get_readable_code(enum lttcomm_return_code code
)
130 int tmp_code
= -code
;
132 if (tmp_code
>= LTTCOMM_OK
&& tmp_code
< LTTCOMM_NR
) {
133 return lttcomm_readable_code
[LTTCOMM_ERR_INDEX(tmp_code
)];
136 return "Unknown error code";
140 * Connect to unix socket using the path name.
142 int lttcomm_connect_unix_sock(const char *pathname
)
144 struct sockaddr_un sun
;
145 int fd
, ret
, closeret
;
147 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
154 memset(&sun
, 0, sizeof(sun
));
155 sun
.sun_family
= AF_UNIX
;
156 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
157 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
159 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
162 * Don't print message on connect error, because connect is used in
163 * normal execution to detect if sessiond is alive.
171 closeret
= close(fd
);
180 * Do an accept(2) on the sock and return the new file descriptor. The socket
181 * MUST be bind(2) before.
183 int lttcomm_accept_unix_sock(int sock
)
186 struct sockaddr_un sun
;
190 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
199 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
202 int lttcomm_create_unix_sock(const char *pathname
)
204 struct sockaddr_un sun
;
208 /* Create server socket */
209 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
214 memset(&sun
, 0, sizeof(sun
));
215 sun
.sun_family
= AF_UNIX
;
216 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
217 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
219 /* Unlink the old file if present */
220 (void) unlink(pathname
);
221 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
234 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
236 int lttcomm_listen_unix_sock(int sock
)
240 ret
= listen(sock
, LTTNG_SESSIOND_COMM_MAX_LISTEN
);
249 * Receive data of size len in put that data into the buf param. Using recvmsg
252 * Return the size of received data.
254 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
260 memset(&msg
, 0, sizeof(msg
));
262 iov
[0].iov_base
= buf
;
263 iov
[0].iov_len
= len
;
267 ret
= recvmsg(sock
, &msg
, MSG_WAITALL
);
276 * Send buf data of size len. Using sendmsg API.
278 * Return the size of sent data.
280 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
286 memset(&msg
, 0, sizeof(msg
));
288 iov
[0].iov_base
= buf
;
289 iov
[0].iov_len
= len
;
293 ret
= sendmsg(sock
, &msg
, 0);
302 * Shutdown cleanly a unix socket.
304 int lttcomm_close_unix_sock(int sock
)
308 /* Shutdown receptions and transmissions */
309 ret
= shutdown(sock
, SHUT_RDWR
);
314 closeret
= close(sock
);
323 * Send a message accompanied by fd(s) over a unix socket.
325 * Returns the size of data sent, or negative error value.
327 ssize_t
lttcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
330 struct cmsghdr
*cmptr
;
333 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
334 char tmp
[CMSG_SPACE(sizeof_fds
)];
337 memset(&msg
, 0, sizeof(msg
));
339 if (nb_fd
> LTTCOMM_MAX_SEND_FDS
)
342 msg
.msg_control
= (caddr_t
)tmp
;
343 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
345 cmptr
= CMSG_FIRSTHDR(&msg
);
346 cmptr
->cmsg_level
= SOL_SOCKET
;
347 cmptr
->cmsg_type
= SCM_RIGHTS
;
348 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
349 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
350 /* Sum of the length of all control messages in the buffer: */
351 msg
.msg_controllen
= cmptr
->cmsg_len
;
353 iov
[0].iov_base
= &dummy
;
358 ret
= sendmsg(sock
, &msg
, 0);
366 * Recv a message accompanied by fd(s) from a unix socket.
368 * Returns the size of received data, or negative error value.
370 * Expect at most "nb_fd" file descriptors. Returns the number of fd
371 * actually received in nb_fd.
373 ssize_t
lttcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
377 struct cmsghdr
*cmsg
;
378 size_t sizeof_fds
= nb_fd
* sizeof(int);
379 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
383 memset(&msg
, 0, sizeof(msg
));
385 /* Prepare to receive the structures */
386 iov
[0].iov_base
= &dummy
;
390 msg
.msg_control
= recv_fd
;
391 msg
.msg_controllen
= sizeof(recv_fd
);
393 ret
= recvmsg(sock
, &msg
, 0);
395 PERROR("recvmsg fds");
399 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
403 if (msg
.msg_flags
& MSG_CTRUNC
) {
404 fprintf(stderr
, "Error: Control message truncated.\n");
408 cmsg
= CMSG_FIRSTHDR(&msg
);
410 fprintf(stderr
, "Error: Invalid control message header\n");
414 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
415 fprintf(stderr
, "Didn't received any fd\n");
419 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
420 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
421 cmsg
->cmsg_len
, CMSG_LEN(sizeof_fds
));
425 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
432 * Send a message with credentials over a unix socket.
434 * Returns the size of data sent, or negative error value.
436 ssize_t
lttcomm_send_creds_unix_sock(int sock
, void *buf
, size_t len
)
439 struct cmsghdr
*cmptr
;
443 size_t sizeof_cred
= sizeof(struct ucred
);
444 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
446 memset(&msg
, 0, sizeof(msg
));
448 iov
[0].iov_base
= buf
;
449 iov
[0].iov_len
= len
;
453 msg
.msg_control
= (caddr_t
) anc_buf
;
454 msg
.msg_controllen
= CMSG_LEN(sizeof_cred
);
456 cmptr
= CMSG_FIRSTHDR(&msg
);
457 cmptr
->cmsg_level
= SOL_SOCKET
;
458 cmptr
->cmsg_type
= SCM_CREDENTIALS
;
459 cmptr
->cmsg_len
= CMSG_LEN(sizeof_cred
);
461 creds
= (struct ucred
*) CMSG_DATA(cmptr
);
463 creds
->uid
= geteuid();
464 creds
->gid
= getegid();
465 creds
->pid
= getpid();
467 ret
= sendmsg(sock
, &msg
, 0);
476 * Recv a message accompanied with credentials from a unix socket.
478 * Returns the size of received data, or negative error value.
480 ssize_t
lttcomm_recv_creds_unix_sock(int sock
, void *buf
, size_t len
,
484 struct cmsghdr
*cmptr
;
487 size_t sizeof_cred
= sizeof(struct ucred
);
488 char anc_buf
[CMSG_SPACE(sizeof_cred
)];
490 memset(&msg
, 0, sizeof(msg
));
498 /* Prepare to receive the structures */
499 iov
[0].iov_base
= buf
;
500 iov
[0].iov_len
= len
;
504 msg
.msg_control
= anc_buf
;
505 msg
.msg_controllen
= sizeof(anc_buf
);
507 ret
= recvmsg(sock
, &msg
, 0);
509 PERROR("recvmsg fds");
513 if (msg
.msg_flags
& MSG_CTRUNC
) {
514 fprintf(stderr
, "Error: Control message truncated.\n");
519 cmptr
= CMSG_FIRSTHDR(&msg
);
521 fprintf(stderr
, "Error: Invalid control message header\n");
526 if (cmptr
->cmsg_level
!= SOL_SOCKET
||
527 cmptr
->cmsg_type
!= SCM_CREDENTIALS
) {
528 fprintf(stderr
, "Didn't received any credentials\n");
533 if (cmptr
->cmsg_len
!= CMSG_LEN(sizeof_cred
)) {
534 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
535 cmptr
->cmsg_len
, CMSG_LEN(sizeof_cred
));
540 memcpy(creds
, CMSG_DATA(cmptr
), sizeof_cred
);
547 * 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");