2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <sys/socket.h>
26 #include <sys/types.h>
32 #include <lttng/ust-comm.h>
35 * Human readable error message.
37 static const char *ustcomm_readable_code
[] = {
38 [ USTCOMM_ERR_INDEX(USTCOMM_ERR
) ] = "Unknown error",
39 [ USTCOMM_ERR_INDEX(USTCOMM_UND
) ] = "Undefined command",
40 [ USTCOMM_ERR_INDEX(USTCOMM_NOT_IMPLEMENTED
) ] = "Not implemented",
41 [ USTCOMM_ERR_INDEX(USTCOMM_UNKNOWN_DOMAIN
) ] = "Unknown tracing domain",
42 [ USTCOMM_ERR_INDEX(USTCOMM_NO_SESSION
) ] = "No session found",
43 [ USTCOMM_ERR_INDEX(USTCOMM_LIST_FAIL
) ] = "Unable to list traceable apps",
44 [ USTCOMM_ERR_INDEX(USTCOMM_NO_APPS
) ] = "No traceable apps found",
45 [ USTCOMM_ERR_INDEX(USTCOMM_SESS_NOT_FOUND
) ] = "Session name not found",
46 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACE
) ] = "No trace found",
47 [ USTCOMM_ERR_INDEX(USTCOMM_FATAL
) ] = "Fatal error of the session daemon",
48 [ USTCOMM_ERR_INDEX(USTCOMM_CREATE_FAIL
) ] = "Create trace failed",
49 [ USTCOMM_ERR_INDEX(USTCOMM_START_FAIL
) ] = "Start trace failed",
50 [ USTCOMM_ERR_INDEX(USTCOMM_STOP_FAIL
) ] = "Stop trace failed",
51 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACEABLE
) ] = "App is not traceable",
52 [ USTCOMM_ERR_INDEX(USTCOMM_SELECT_SESS
) ] = "A session MUST be selected",
53 [ USTCOMM_ERR_INDEX(USTCOMM_EXIST_SESS
) ] = "Session name already exist",
54 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NA
) ] = "UST tracer not available",
55 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_EVENT_EXIST
) ] = "UST event already exists",
56 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_SESS_FAIL
) ] = "UST create session failed",
57 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_FAIL
) ] = "UST create channel failed",
58 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_NOT_FOUND
) ] = "UST channel not found",
59 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_DISABLE_FAIL
) ] = "Disable UST channel failed",
60 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_ENABLE_FAIL
) ] = "Enable UST channel failed",
61 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONTEXT_FAIL
) ] = "Add UST context failed",
62 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_ENABLE_FAIL
) ] = "Enable UST event failed",
63 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DISABLE_FAIL
) ] = "Disable UST event failed",
64 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_META_FAIL
) ] = "Opening metadata failed",
65 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_START_FAIL
) ] = "Starting UST trace failed",
66 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STOP_FAIL
) ] = "Stoping UST trace failed",
67 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONSUMER_FAIL
) ] = "UST consumer start failed",
68 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STREAM_FAIL
) ] = "UST create stream failed",
69 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_FAIL
) ] = "UST trace directory creation failed",
70 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_EXIST
) ] = "UST trace directory already exist",
71 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NO_SESSION
) ] = "No UST session found",
72 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_LIST_FAIL
) ] = "Listing UST events failed",
73 [ USTCOMM_ERR_INDEX(USTCONSUMER_COMMAND_SOCK_READY
) ] = "UST consumer command socket ready",
74 [ USTCOMM_ERR_INDEX(USTCONSUMER_SUCCESS_RECV_FD
) ] = "UST consumer success on receiving fds",
75 [ USTCOMM_ERR_INDEX(USTCONSUMER_ERROR_RECV_FD
) ] = "UST consumer error on receiving fds",
76 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_ERROR
) ] = "UST consumer error in polling thread",
77 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_NVAL
) ] = "UST consumer polling on closed fd",
78 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_HUP
) ] = "UST consumer all fd hung up",
79 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_SUCCESS
) ] = "UST consumer exiting normally",
80 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_FAILURE
) ] = "UST consumer exiting on error",
81 [ USTCOMM_ERR_INDEX(USTCONSUMER_OUTFD_ERROR
) ] = "UST consumer error opening the tracefile",
82 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EBADF
) ] = "UST consumer splice EBADF",
83 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EINVAL
) ] = "UST consumer splice EINVAL",
84 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ENOMEM
) ] = "UST consumer splice ENOMEM",
85 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ESPIPE
) ] = "UST consumer splice ESPIPE",
86 [ USTCOMM_ERR_INDEX(USTCOMM_NO_EVENT
) ] = "Event not found",
90 * lttcom_get_readable_code
92 * Return ptr to string representing a human readable
93 * error code from the ustcomm_return_code enum.
95 const char *ustcomm_get_readable_code(int code
)
97 if (code
== USTCOMM_OK
) {
100 if (code
>= USTCOMM_ERR
&& code
< USTCOMM_NR
) {
101 return ustcomm_readable_code
[USTCOMM_ERR_INDEX(code
)];
103 return strerror(code
);
107 * ustcomm_connect_unix_sock
109 * Connect to unix socket using the path name.
111 int ustcomm_connect_unix_sock(const char *pathname
)
113 struct sockaddr_un sun
;
118 * libust threads require the close-on-exec flag for all
119 * resources so it does not leak file descriptors upon exec.
121 fd
= socket(PF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
128 memset(&sun
, 0, sizeof(sun
));
129 sun
.sun_family
= AF_UNIX
;
130 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
131 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
133 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
136 * Don't print message on connect error, because connect
137 * is used in normal execution to detect if sessiond is
152 * ustcomm_accept_unix_sock
154 * Do an accept(2) on the sock and return the
155 * new file descriptor. The socket MUST be bind(2) before.
157 int ustcomm_accept_unix_sock(int sock
)
160 struct sockaddr_un sun
;
164 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
177 * ustcomm_create_unix_sock
179 * Creates a AF_UNIX local socket using pathname
180 * bind the socket upon creation and return the fd.
182 int ustcomm_create_unix_sock(const char *pathname
)
184 struct sockaddr_un sun
;
188 /* Create server socket */
189 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
194 memset(&sun
, 0, sizeof(sun
));
195 sun
.sun_family
= AF_UNIX
;
196 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
197 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
199 /* Unlink the old file if present */
200 (void) unlink(pathname
);
201 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
214 * ustcomm_listen_unix_sock
216 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
218 int ustcomm_listen_unix_sock(int sock
)
222 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
231 * ustcomm_recv_unix_sock
233 * Receive data of size len in put that data into
234 * the buf param. Using recvmsg API.
235 * Return the size of received data.
237 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
239 struct msghdr msg
= { 0 };
243 iov
[0].iov_base
= buf
;
244 iov
[0].iov_len
= len
;
248 ret
= recvmsg(sock
, &msg
, 0);
257 * ustcomm_send_unix_sock
259 * Send buf data of size len. Using sendmsg API.
260 * Return the size of sent data.
262 ssize_t
ustcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
264 struct msghdr msg
= { 0 };
268 iov
[0].iov_base
= buf
;
269 iov
[0].iov_len
= len
;
274 * Using the MSG_NOSIGNAL when sending data from sessiond to
275 * libust, so libust does not receive an unhandled SIGPIPE or
276 * SIGURG. The sessiond receiver side can be made more resilient
277 * by ignoring SIGPIPE, but we don't have this luxury on the
280 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
289 * ustcomm_close_unix_sock
291 * Shutdown cleanly a unix socket.
293 int ustcomm_close_unix_sock(int sock
)
297 /* Shutdown receptions and transmissions */
298 ret
= shutdown(sock
, SHUT_RDWR
);
307 * ustcomm_send_fds_unix_sock
309 * Send multiple fds on a unix socket.
311 ssize_t
ustcomm_send_fds_unix_sock(int sock
, void *buf
, int *fds
, size_t nb_fd
, size_t len
)
313 struct msghdr msg
= { 0 };
314 struct cmsghdr
*cmptr
;
317 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
318 char tmp
[CMSG_SPACE(sizeof_fds
)];
321 * Note: the consumerd receiver only supports receiving one FD per
326 msg
.msg_control
= (caddr_t
)tmp
;
327 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
329 cmptr
= CMSG_FIRSTHDR(&msg
);
330 cmptr
->cmsg_level
= SOL_SOCKET
;
331 cmptr
->cmsg_type
= SCM_RIGHTS
;
332 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
333 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
334 /* Sum of the length of all control messages in the buffer: */
335 msg
.msg_controllen
= cmptr
->cmsg_len
;
337 iov
[0].iov_base
= buf
;
338 iov
[0].iov_len
= len
;
342 ret
= sendmsg(sock
, &msg
, 0);
350 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
354 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
357 printf("message successfully sent\n");
360 if (errno
== ECONNRESET
) {
361 printf("remote end closed connection\n");
366 printf("incorrect message size: %zd\n", len
);
372 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
373 uint32_t expected_handle
, uint32_t expected_cmd
)
377 memset(lur
, 0, sizeof(*lur
));
378 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
380 case 0: /* orderly shutdown */
381 printf("Application has performed an orderly shutdown\n");
384 printf("result message received\n");
385 if (lur
->handle
!= expected_handle
) {
386 printf("Unexpected result message handle\n");
390 if (lur
->cmd
!= expected_cmd
) {
391 printf("Unexpected result message command\n");
394 if (lur
->ret_code
!= USTCOMM_OK
) {
395 printf("remote operation failed with code %d.\n",
397 return lur
->ret_code
;
401 if (errno
== ECONNRESET
) {
402 printf("remote end closed connection\n");
407 printf("incorrect message size: %zd\n", len
);
408 return len
> 0 ? -1 : len
;
412 int ustcomm_send_app_cmd(int sock
,
413 struct ustcomm_ust_msg
*lum
,
414 struct ustcomm_ust_reply
*lur
)
418 ret
= ustcomm_send_app_msg(sock
, lum
);
421 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
429 * Receives a single fd from socket.
431 * Returns the size of received data
433 int ustcomm_recv_fd(int sock
)
438 struct cmsghdr
*cmsg
;
439 char recv_fd
[CMSG_SPACE(sizeof(int))];
440 struct msghdr msg
= { 0 };
447 /* Prepare to receive the structures */
448 iov
[0].iov_base
= &data_fd
;
449 iov
[0].iov_len
= sizeof(data_fd
);
452 msg
.msg_control
= recv_fd
;
453 msg
.msg_controllen
= sizeof(recv_fd
);
455 printf("Waiting to receive fd\n");
456 if ((ret
= recvmsg(sock
, &msg
, 0)) < 0) {
460 if (ret
!= sizeof(data_fd
)) {
461 printf("Received %d bytes, expected %ld", ret
, sizeof(data_fd
));
464 cmsg
= CMSG_FIRSTHDR(&msg
);
466 printf("Invalid control message header\n");
470 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
471 printf("Didn't received any fd\n");
476 for (i
= 0; i
< sizeof(int); i
++)
477 tmp
.vc
[i
] = CMSG_DATA(cmsg
)[i
];
479 printf("received fd %d\n", ret
);