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>
37 * Human readable error message.
39 static const char *ustcomm_readable_code
[] = {
40 [ USTCOMM_ERR_INDEX(USTCOMM_ERR
) ] = "Unknown error",
41 [ USTCOMM_ERR_INDEX(USTCOMM_UND
) ] = "Undefined command",
42 [ USTCOMM_ERR_INDEX(USTCOMM_NOT_IMPLEMENTED
) ] = "Not implemented",
43 [ USTCOMM_ERR_INDEX(USTCOMM_UNKNOWN_DOMAIN
) ] = "Unknown tracing domain",
44 [ USTCOMM_ERR_INDEX(USTCOMM_NO_SESSION
) ] = "No session found",
45 [ USTCOMM_ERR_INDEX(USTCOMM_LIST_FAIL
) ] = "Unable to list traceable apps",
46 [ USTCOMM_ERR_INDEX(USTCOMM_NO_APPS
) ] = "No traceable apps found",
47 [ USTCOMM_ERR_INDEX(USTCOMM_SESS_NOT_FOUND
) ] = "Session name not found",
48 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACE
) ] = "No trace found",
49 [ USTCOMM_ERR_INDEX(USTCOMM_FATAL
) ] = "Fatal error of the session daemon",
50 [ USTCOMM_ERR_INDEX(USTCOMM_CREATE_FAIL
) ] = "Create trace failed",
51 [ USTCOMM_ERR_INDEX(USTCOMM_START_FAIL
) ] = "Start trace failed",
52 [ USTCOMM_ERR_INDEX(USTCOMM_STOP_FAIL
) ] = "Stop trace failed",
53 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACEABLE
) ] = "App is not traceable",
54 [ USTCOMM_ERR_INDEX(USTCOMM_SELECT_SESS
) ] = "A session MUST be selected",
55 [ USTCOMM_ERR_INDEX(USTCOMM_EXIST_SESS
) ] = "Session name already exist",
56 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NA
) ] = "UST tracer not available",
57 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_EVENT_EXIST
) ] = "UST event already exists",
58 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_SESS_FAIL
) ] = "UST create session failed",
59 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_FAIL
) ] = "UST create channel failed",
60 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_NOT_FOUND
) ] = "UST channel not found",
61 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_DISABLE_FAIL
) ] = "Disable UST channel failed",
62 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_ENABLE_FAIL
) ] = "Enable UST channel failed",
63 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONTEXT_FAIL
) ] = "Add UST context failed",
64 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_ENABLE_FAIL
) ] = "Enable UST event failed",
65 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DISABLE_FAIL
) ] = "Disable UST event failed",
66 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_META_FAIL
) ] = "Opening metadata failed",
67 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_START_FAIL
) ] = "Starting UST trace failed",
68 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STOP_FAIL
) ] = "Stoping UST trace failed",
69 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONSUMER_FAIL
) ] = "UST consumer start failed",
70 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STREAM_FAIL
) ] = "UST create stream failed",
71 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_FAIL
) ] = "UST trace directory creation failed",
72 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_EXIST
) ] = "UST trace directory already exist",
73 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NO_SESSION
) ] = "No UST session found",
74 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_LIST_FAIL
) ] = "Listing UST events failed",
75 [ USTCOMM_ERR_INDEX(USTCONSUMER_COMMAND_SOCK_READY
) ] = "UST consumer command socket ready",
76 [ USTCOMM_ERR_INDEX(USTCONSUMER_SUCCESS_RECV_FD
) ] = "UST consumer success on receiving fds",
77 [ USTCOMM_ERR_INDEX(USTCONSUMER_ERROR_RECV_FD
) ] = "UST consumer error on receiving fds",
78 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_ERROR
) ] = "UST consumer error in polling thread",
79 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_NVAL
) ] = "UST consumer polling on closed fd",
80 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_HUP
) ] = "UST consumer all fd hung up",
81 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_SUCCESS
) ] = "UST consumer exiting normally",
82 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_FAILURE
) ] = "UST consumer exiting on error",
83 [ USTCOMM_ERR_INDEX(USTCONSUMER_OUTFD_ERROR
) ] = "UST consumer error opening the tracefile",
84 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EBADF
) ] = "UST consumer splice EBADF",
85 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EINVAL
) ] = "UST consumer splice EINVAL",
86 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ENOMEM
) ] = "UST consumer splice ENOMEM",
87 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ESPIPE
) ] = "UST consumer splice ESPIPE",
88 [ USTCOMM_ERR_INDEX(USTCOMM_NO_EVENT
) ] = "Event not found",
92 * lttcom_get_readable_code
94 * Return ptr to string representing a human readable
95 * error code from the ustcomm_return_code enum.
97 const char *ustcomm_get_readable_code(int code
)
99 if (code
== USTCOMM_OK
) {
102 if (code
>= USTCOMM_ERR
&& code
< USTCOMM_NR
) {
103 return ustcomm_readable_code
[USTCOMM_ERR_INDEX(code
)];
105 return strerror(code
);
109 * ustcomm_connect_unix_sock
111 * Connect to unix socket using the path name.
113 int ustcomm_connect_unix_sock(const char *pathname
)
115 struct sockaddr_un sun
;
120 * libust threads require the close-on-exec flag for all
121 * resources so it does not leak file descriptors upon exec.
123 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
129 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
135 memset(&sun
, 0, sizeof(sun
));
136 sun
.sun_family
= AF_UNIX
;
137 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
138 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
140 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
143 * Don't print message on connect error, because connect
144 * is used in normal execution to detect if sessiond is
160 * ustcomm_accept_unix_sock
162 * Do an accept(2) on the sock and return the
163 * new file descriptor. The socket MUST be bind(2) before.
165 int ustcomm_accept_unix_sock(int sock
)
168 struct sockaddr_un sun
;
172 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
181 * ustcomm_create_unix_sock
183 * Creates a AF_UNIX local socket using pathname
184 * bind the socket upon creation and return the fd.
186 int ustcomm_create_unix_sock(const char *pathname
)
188 struct sockaddr_un sun
;
192 /* Create server socket */
193 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
198 memset(&sun
, 0, sizeof(sun
));
199 sun
.sun_family
= AF_UNIX
;
200 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
201 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
203 /* Unlink the old file if present */
204 (void) unlink(pathname
);
205 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
218 * ustcomm_listen_unix_sock
220 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
222 int ustcomm_listen_unix_sock(int sock
)
226 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
235 * ustcomm_recv_unix_sock
237 * Receive data of size len in put that data into
238 * the buf param. Using recvmsg API.
239 * Return the size of received data.
241 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
247 memset(&msg
, 0, sizeof(msg
));
249 iov
[0].iov_base
= buf
;
250 iov
[0].iov_len
= len
;
255 ret
= recvmsg(sock
, &msg
, 0);
256 } while (ret
< 0 && errno
== EINTR
);
257 if (ret
< 0 && errno
!= EPIPE
) {
265 * ustcomm_send_unix_sock
267 * Send buf data of size len. Using sendmsg API.
268 * Return the size of sent data.
270 ssize_t
ustcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
276 memset(&msg
, 0, sizeof(msg
));
278 iov
[0].iov_base
= buf
;
279 iov
[0].iov_len
= len
;
284 * Using the MSG_NOSIGNAL when sending data from sessiond to
285 * libust, so libust does not receive an unhandled SIGPIPE or
286 * SIGURG. The sessiond receiver side can be made more resilient
287 * by ignoring SIGPIPE, but we don't have this luxury on the
291 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
292 } while (ret
< 0 && errno
== EINTR
);
293 if (ret
< 0 && errno
!= EPIPE
) {
301 * ustcomm_close_unix_sock
303 * Shutdown cleanly a unix socket.
305 int ustcomm_close_unix_sock(int sock
)
318 * ustcomm_send_fds_unix_sock
320 * Send multiple fds on a unix socket.
322 ssize_t
ustcomm_send_fds_unix_sock(int sock
, void *buf
, int *fds
, size_t nb_fd
, size_t len
)
325 struct cmsghdr
*cmptr
;
328 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
329 char tmp
[CMSG_SPACE(sizeof_fds
)];
331 memset(&msg
, 0, sizeof(msg
));
334 * Note: the consumerd receiver only supports receiving one FD per
339 msg
.msg_control
= (caddr_t
)tmp
;
340 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
342 cmptr
= CMSG_FIRSTHDR(&msg
);
343 cmptr
->cmsg_level
= SOL_SOCKET
;
344 cmptr
->cmsg_type
= SCM_RIGHTS
;
345 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
346 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
347 /* Sum of the length of all control messages in the buffer: */
348 msg
.msg_controllen
= cmptr
->cmsg_len
;
350 iov
[0].iov_base
= buf
;
351 iov
[0].iov_len
= len
;
356 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
357 } while (ret
< 0 && errno
== EINTR
);
358 if (ret
< 0 && errno
!= EPIPE
) {
365 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
369 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
374 if (errno
== ECONNRESET
) {
375 fprintf(stderr
, "remote end closed connection\n");
380 fprintf(stderr
, "incorrect message size: %zd\n", len
);
386 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
387 uint32_t expected_handle
, uint32_t expected_cmd
)
391 memset(lur
, 0, sizeof(*lur
));
392 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
394 case 0: /* orderly shutdown */
397 if (lur
->handle
!= expected_handle
) {
398 fprintf(stderr
, "Unexpected result message handle\n");
402 if (lur
->cmd
!= expected_cmd
) {
403 fprintf(stderr
, "Unexpected result message command\n");
406 if (lur
->ret_code
!= USTCOMM_OK
) {
408 * Some errors are normal.. we should put this
409 * in a debug level message...
410 * fprintf(stderr, "remote operation failed with code %d.\n",
413 return lur
->ret_code
;
417 if (errno
== ECONNRESET
) {
418 fprintf(stderr
, "remote end closed connection\n");
423 fprintf(stderr
, "incorrect message size: %zd\n", len
);
424 return len
> 0 ? -1 : len
;
428 int ustcomm_send_app_cmd(int sock
,
429 struct ustcomm_ust_msg
*lum
,
430 struct ustcomm_ust_reply
*lur
)
434 ret
= ustcomm_send_app_msg(sock
, lum
);
437 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
445 * Receives a single fd from socket.
447 * Returns the size of received data
449 int ustcomm_recv_fd(int sock
)
454 struct cmsghdr
*cmsg
;
455 char recv_fd
[CMSG_SPACE(sizeof(int))];
463 memset(&msg
, 0, sizeof(msg
));
465 /* Prepare to receive the structures */
466 iov
[0].iov_base
= &data_fd
;
467 iov
[0].iov_len
= sizeof(data_fd
);
470 msg
.msg_control
= recv_fd
;
471 msg
.msg_controllen
= sizeof(recv_fd
);
474 ret
= recvmsg(sock
, &msg
, 0);
475 } while (ret
< 0 && errno
== EINTR
);
477 if (errno
!= EPIPE
) {
482 if (ret
!= sizeof(data_fd
)) {
483 fprintf(stderr
, "Received %d bytes, expected %zd", ret
, sizeof(data_fd
));
486 cmsg
= CMSG_FIRSTHDR(&msg
);
488 fprintf(stderr
, "Invalid control message header\n");
492 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
493 fprintf(stderr
, "Didn't received any fd\n");
498 for (i
= 0; i
< sizeof(int); i
++)
499 tmp
.vc
[i
] = CMSG_DATA(cmsg
)[i
];
502 * Useful for fd leak debug.
503 * fprintf(stderr, "received fd %d\n", ret);