2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/socket.h>
27 #include <sys/types.h>
36 * Human readable error message.
38 static const char *ustcomm_readable_code
[] = {
39 [ USTCOMM_ERR_INDEX(USTCOMM_ERR
) ] = "Unknown error",
40 [ USTCOMM_ERR_INDEX(USTCOMM_UND
) ] = "Undefined command",
41 [ USTCOMM_ERR_INDEX(USTCOMM_NOT_IMPLEMENTED
) ] = "Not implemented",
42 [ USTCOMM_ERR_INDEX(USTCOMM_UNKNOWN_DOMAIN
) ] = "Unknown tracing domain",
43 [ USTCOMM_ERR_INDEX(USTCOMM_NO_SESSION
) ] = "No session found",
44 [ USTCOMM_ERR_INDEX(USTCOMM_LIST_FAIL
) ] = "Unable to list traceable apps",
45 [ USTCOMM_ERR_INDEX(USTCOMM_NO_APPS
) ] = "No traceable apps found",
46 [ USTCOMM_ERR_INDEX(USTCOMM_SESS_NOT_FOUND
) ] = "Session name not found",
47 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACE
) ] = "No trace found",
48 [ USTCOMM_ERR_INDEX(USTCOMM_FATAL
) ] = "Fatal error of the session daemon",
49 [ USTCOMM_ERR_INDEX(USTCOMM_CREATE_FAIL
) ] = "Create trace failed",
50 [ USTCOMM_ERR_INDEX(USTCOMM_START_FAIL
) ] = "Start trace failed",
51 [ USTCOMM_ERR_INDEX(USTCOMM_STOP_FAIL
) ] = "Stop trace failed",
52 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACEABLE
) ] = "App is not traceable",
53 [ USTCOMM_ERR_INDEX(USTCOMM_SELECT_SESS
) ] = "A session MUST be selected",
54 [ USTCOMM_ERR_INDEX(USTCOMM_EXIST_SESS
) ] = "Session name already exist",
55 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NA
) ] = "UST tracer not available",
56 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_EVENT_EXIST
) ] = "UST event already exists",
57 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_SESS_FAIL
) ] = "UST create session failed",
58 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_FAIL
) ] = "UST create channel failed",
59 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_NOT_FOUND
) ] = "UST channel not found",
60 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_DISABLE_FAIL
) ] = "Disable UST channel failed",
61 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_ENABLE_FAIL
) ] = "Enable UST channel failed",
62 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONTEXT_FAIL
) ] = "Add UST context failed",
63 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_ENABLE_FAIL
) ] = "Enable UST event failed",
64 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DISABLE_FAIL
) ] = "Disable UST event failed",
65 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_META_FAIL
) ] = "Opening metadata failed",
66 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_START_FAIL
) ] = "Starting UST trace failed",
67 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STOP_FAIL
) ] = "Stoping UST trace failed",
68 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONSUMER_FAIL
) ] = "UST consumer start failed",
69 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STREAM_FAIL
) ] = "UST create stream failed",
70 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_FAIL
) ] = "UST trace directory creation failed",
71 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_EXIST
) ] = "UST trace directory already exist",
72 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NO_SESSION
) ] = "No UST session found",
73 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_LIST_FAIL
) ] = "Listing UST events failed",
74 [ USTCOMM_ERR_INDEX(USTCONSUMER_COMMAND_SOCK_READY
) ] = "UST consumer command socket ready",
75 [ USTCOMM_ERR_INDEX(USTCONSUMER_SUCCESS_RECV_FD
) ] = "UST consumer success on receiving fds",
76 [ USTCOMM_ERR_INDEX(USTCONSUMER_ERROR_RECV_FD
) ] = "UST consumer error on receiving fds",
77 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_ERROR
) ] = "UST consumer error in polling thread",
78 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_NVAL
) ] = "UST consumer polling on closed fd",
79 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_HUP
) ] = "UST consumer all fd hung up",
80 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_SUCCESS
) ] = "UST consumer exiting normally",
81 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_FAILURE
) ] = "UST consumer exiting on error",
82 [ USTCOMM_ERR_INDEX(USTCONSUMER_OUTFD_ERROR
) ] = "UST consumer error opening the tracefile",
83 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EBADF
) ] = "UST consumer splice EBADF",
84 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EINVAL
) ] = "UST consumer splice EINVAL",
85 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ENOMEM
) ] = "UST consumer splice ENOMEM",
86 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ESPIPE
) ] = "UST consumer splice ESPIPE",
87 [ USTCOMM_ERR_INDEX(USTCOMM_NO_EVENT
) ] = "Event not found",
91 * lttcom_get_readable_code
93 * Return ptr to string representing a human readable
94 * error code from the ustcomm_return_code enum.
96 const char *ustcomm_get_readable_code(int code
)
98 if (code
== USTCOMM_OK
) {
101 if (code
>= USTCOMM_ERR
&& code
< USTCOMM_NR
) {
102 return ustcomm_readable_code
[USTCOMM_ERR_INDEX(code
)];
104 return strerror(code
);
108 * ustcomm_connect_unix_sock
110 * Connect to unix socket using the path name.
112 int ustcomm_connect_unix_sock(const char *pathname
)
114 struct sockaddr_un sun
;
119 * libust threads require the close-on-exec flag for all
120 * resources so it does not leak file descriptors upon exec.
122 fd
= socket(PF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
129 memset(&sun
, 0, sizeof(sun
));
130 sun
.sun_family
= AF_UNIX
;
131 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
132 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
134 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
137 * Don't print message on connect error, because connect
138 * is used in normal execution to detect if sessiond is
153 * ustcomm_accept_unix_sock
155 * Do an accept(2) on the sock and return the
156 * new file descriptor. The socket MUST be bind(2) before.
158 int ustcomm_accept_unix_sock(int sock
)
161 struct sockaddr_un sun
;
165 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
178 * ustcomm_create_unix_sock
180 * Creates a AF_UNIX local socket using pathname
181 * bind the socket upon creation and return the fd.
183 int ustcomm_create_unix_sock(const char *pathname
)
185 struct sockaddr_un sun
;
189 /* Create server socket */
190 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
195 memset(&sun
, 0, sizeof(sun
));
196 sun
.sun_family
= AF_UNIX
;
197 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
198 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
200 /* Unlink the old file if present */
201 (void) unlink(pathname
);
202 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
215 * ustcomm_listen_unix_sock
217 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
219 int ustcomm_listen_unix_sock(int sock
)
223 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
232 * ustcomm_recv_unix_sock
234 * Receive data of size len in put that data into
235 * the buf param. Using recvmsg API.
236 * Return the size of received data.
238 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
244 memset(&msg
, 0, sizeof(msg
));
246 iov
[0].iov_base
= buf
;
247 iov
[0].iov_len
= len
;
251 ret
= recvmsg(sock
, &msg
, 0);
260 * ustcomm_send_unix_sock
262 * Send buf data of size len. Using sendmsg API.
263 * Return the size of sent data.
265 ssize_t
ustcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
271 memset(&msg
, 0, sizeof(msg
));
273 iov
[0].iov_base
= buf
;
274 iov
[0].iov_len
= len
;
279 * Using the MSG_NOSIGNAL when sending data from sessiond to
280 * libust, so libust does not receive an unhandled SIGPIPE or
281 * SIGURG. The sessiond receiver side can be made more resilient
282 * by ignoring SIGPIPE, but we don't have this luxury on the
285 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
294 * ustcomm_close_unix_sock
296 * Shutdown cleanly a unix socket.
298 int ustcomm_close_unix_sock(int sock
)
302 /* Shutdown receptions and transmissions */
303 ret
= shutdown(sock
, SHUT_RDWR
);
312 * ustcomm_send_fds_unix_sock
314 * Send multiple fds on a unix socket.
316 ssize_t
ustcomm_send_fds_unix_sock(int sock
, void *buf
, int *fds
, size_t nb_fd
, size_t len
)
319 struct cmsghdr
*cmptr
;
322 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
323 char tmp
[CMSG_SPACE(sizeof_fds
)];
325 memset(&msg
, 0, sizeof(msg
));
328 * Note: the consumerd receiver only supports receiving one FD per
333 msg
.msg_control
= (caddr_t
)tmp
;
334 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
336 cmptr
= CMSG_FIRSTHDR(&msg
);
337 cmptr
->cmsg_level
= SOL_SOCKET
;
338 cmptr
->cmsg_type
= SCM_RIGHTS
;
339 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
340 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
341 /* Sum of the length of all control messages in the buffer: */
342 msg
.msg_controllen
= cmptr
->cmsg_len
;
344 iov
[0].iov_base
= buf
;
345 iov
[0].iov_len
= len
;
349 ret
= sendmsg(sock
, &msg
, 0);
357 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
361 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
366 if (errno
== ECONNRESET
) {
367 fprintf(stderr
, "remote end closed connection\n");
372 fprintf(stderr
, "incorrect message size: %zd\n", len
);
378 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
379 uint32_t expected_handle
, uint32_t expected_cmd
)
383 memset(lur
, 0, sizeof(*lur
));
384 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
386 case 0: /* orderly shutdown */
389 if (lur
->handle
!= expected_handle
) {
390 fprintf(stderr
, "Unexpected result message handle\n");
394 if (lur
->cmd
!= expected_cmd
) {
395 fprintf(stderr
, "Unexpected result message command\n");
398 if (lur
->ret_code
!= USTCOMM_OK
) {
400 * Some errors are normal.. we should put this
401 * in a debug level message...
402 * fprintf(stderr, "remote operation failed with code %d.\n",
405 return lur
->ret_code
;
409 if (errno
== ECONNRESET
) {
410 fprintf(stderr
, "remote end closed connection\n");
415 fprintf(stderr
, "incorrect message size: %zd\n", len
);
416 return len
> 0 ? -1 : len
;
420 int ustcomm_send_app_cmd(int sock
,
421 struct ustcomm_ust_msg
*lum
,
422 struct ustcomm_ust_reply
*lur
)
426 ret
= ustcomm_send_app_msg(sock
, lum
);
429 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
437 * Receives a single fd from socket.
439 * Returns the size of received data
441 int ustcomm_recv_fd(int sock
)
446 struct cmsghdr
*cmsg
;
447 char recv_fd
[CMSG_SPACE(sizeof(int))];
455 memset(&msg
, 0, sizeof(msg
));
457 /* Prepare to receive the structures */
458 iov
[0].iov_base
= &data_fd
;
459 iov
[0].iov_len
= sizeof(data_fd
);
462 msg
.msg_control
= recv_fd
;
463 msg
.msg_controllen
= sizeof(recv_fd
);
465 if ((ret
= recvmsg(sock
, &msg
, 0)) < 0) {
469 if (ret
!= sizeof(data_fd
)) {
470 fprintf(stderr
, "Received %d bytes, expected %zd", ret
, sizeof(data_fd
));
473 cmsg
= CMSG_FIRSTHDR(&msg
);
475 fprintf(stderr
, "Invalid control message header\n");
479 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
480 fprintf(stderr
, "Didn't received any fd\n");
485 for (i
= 0; i
< sizeof(int); i
++)
486 tmp
.vc
[i
] = CMSG_DATA(cmsg
)[i
];
489 * Useful for fd leak debug.
490 * fprintf(stderr, "received fd %d\n", ret);