2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011-2013 - 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 #include <lttng/ust-error.h>
38 #define USTCOMM_CODE_OFFSET(code) \
39 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
41 #define USTCOMM_MAX_SEND_FDS 4
44 * Human readable error message.
46 static const char *ustcomm_readable_code
[] = {
47 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
48 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
49 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
50 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
51 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
52 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
53 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
54 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
60 * Receives positive error value.
61 * Return ptr to string representing a human readable
62 * error code from the ustcomm_return_code enum.
64 const char *lttng_ust_strerror(int code
)
66 if (code
== LTTNG_UST_OK
)
67 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
68 if (code
< LTTNG_UST_ERR
)
69 return strerror(code
);
70 if (code
>= LTTNG_UST_ERR_NR
)
72 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
77 * ustcomm_connect_unix_sock
79 * Connect to unix socket using the path name.
81 int ustcomm_connect_unix_sock(const char *pathname
)
83 struct sockaddr_un sun
;
87 * libust threads require the close-on-exec flag for all
88 * resources so it does not leak file descriptors upon exec.
90 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
96 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
103 memset(&sun
, 0, sizeof(sun
));
104 sun
.sun_family
= AF_UNIX
;
105 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
106 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
108 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
111 * Don't print message on connect error, because connect
112 * is used in normal execution to detect if sessiond is
126 closeret
= close(fd
);
135 * ustcomm_accept_unix_sock
137 * Do an accept(2) on the sock and return the
138 * new file descriptor. The socket MUST be bind(2) before.
140 int ustcomm_accept_unix_sock(int sock
)
143 struct sockaddr_un sun
;
147 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
156 * ustcomm_create_unix_sock
158 * Creates a AF_UNIX local socket using pathname
159 * bind the socket upon creation and return the fd.
161 int ustcomm_create_unix_sock(const char *pathname
)
163 struct sockaddr_un sun
;
166 /* Create server socket */
167 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
173 memset(&sun
, 0, sizeof(sun
));
174 sun
.sun_family
= AF_UNIX
;
175 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
176 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
178 /* Unlink the old file if present */
179 (void) unlink(pathname
);
180 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
193 closeret
= close(fd
);
203 * ustcomm_listen_unix_sock
205 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
207 int ustcomm_listen_unix_sock(int sock
)
211 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
221 * ustcomm_close_unix_sock
223 * Shutdown cleanly a unix socket.
225 int ustcomm_close_unix_sock(int sock
)
239 * ustcomm_recv_unix_sock
241 * Receive data of size len in put that data into
242 * the buf param. Using recvmsg API.
243 * Return the size of received data.
244 * Return 0 on orderly shutdown.
246 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
252 memset(&msg
, 0, sizeof(msg
));
254 iov
[0].iov_base
= buf
;
255 iov
[0].iov_len
= len
;
260 ret
= recvmsg(sock
, &msg
, 0);
261 } while (ret
< 0 && errno
== EINTR
);
266 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
270 shutret
= shutdown(sock
, SHUT_RDWR
);
272 fprintf(stderr
, "Socket shutdown error");
279 * ustcomm_send_unix_sock
281 * Send buf data of size len. Using sendmsg API.
282 * Return the size of sent data.
284 ssize_t
ustcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
290 memset(&msg
, 0, sizeof(msg
));
292 iov
[0].iov_base
= buf
;
293 iov
[0].iov_len
= len
;
298 * Using the MSG_NOSIGNAL when sending data from sessiond to
299 * libust, so libust does not receive an unhandled SIGPIPE or
300 * SIGURG. The sessiond receiver side can be made more resilient
301 * by ignoring SIGPIPE, but we don't have this luxury on the
305 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
306 } while (ret
< 0 && errno
== EINTR
);
311 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
315 shutret
= shutdown(sock
, SHUT_RDWR
);
317 fprintf(stderr
, "Socket shutdown error");
324 * Send a message accompanied by fd(s) over a unix socket.
326 * Returns the size of data sent, or negative error value.
328 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
331 struct cmsghdr
*cmptr
;
334 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
335 char tmp
[CMSG_SPACE(sizeof_fds
)];
338 memset(&msg
, 0, sizeof(msg
));
339 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
341 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
344 msg
.msg_control
= (caddr_t
)tmp
;
345 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
347 cmptr
= CMSG_FIRSTHDR(&msg
);
348 cmptr
->cmsg_level
= SOL_SOCKET
;
349 cmptr
->cmsg_type
= SCM_RIGHTS
;
350 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
351 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
352 /* Sum of the length of all control messages in the buffer: */
353 msg
.msg_controllen
= cmptr
->cmsg_len
;
355 iov
[0].iov_base
= &dummy
;
361 ret
= sendmsg(sock
, &msg
, 0);
362 } while (ret
< 0 && errno
== EINTR
);
365 * We consider EPIPE and ECONNRESET as expected.
367 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
375 * Recv a message accompanied by fd(s) from a unix socket.
377 * Returns the size of received data, or negative error value.
379 * Expect at most "nb_fd" file descriptors. Returns the number of fd
380 * actually received in nb_fd.
381 * Returns -EPIPE on orderly shutdown.
383 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
387 struct cmsghdr
*cmsg
;
388 size_t sizeof_fds
= nb_fd
* sizeof(int);
389 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
393 memset(&msg
, 0, sizeof(msg
));
395 /* Prepare to receive the structures */
396 iov
[0].iov_base
= &dummy
;
400 msg
.msg_control
= recv_fd
;
401 msg
.msg_controllen
= sizeof(recv_fd
);
404 ret
= recvmsg(sock
, &msg
, 0);
405 } while (ret
< 0 && errno
== EINTR
);
407 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
408 perror("recvmsg fds");
410 if (errno
== EPIPE
|| errno
== ECONNRESET
)
415 /* orderly shutdown */
420 fprintf(stderr
, "Error: Received %zd bytes, expected %d\n",
424 if (msg
.msg_flags
& MSG_CTRUNC
) {
425 fprintf(stderr
, "Error: Control message truncated.\n");
429 cmsg
= CMSG_FIRSTHDR(&msg
);
431 fprintf(stderr
, "Error: Invalid control message header\n");
435 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
436 fprintf(stderr
, "Didn't received any fd\n");
440 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
441 fprintf(stderr
, "Error: Received %zu bytes of ancillary data, expected %zu\n",
442 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
446 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
452 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
456 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
464 fprintf(stderr
, "incorrect message size: %zd\n", len
);
471 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
472 uint32_t expected_handle
, uint32_t expected_cmd
)
476 memset(lur
, 0, sizeof(*lur
));
477 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
479 case 0: /* orderly shutdown */
482 if (lur
->handle
!= expected_handle
) {
483 fprintf(stderr
, "Unexpected result message handle: "
484 "expected: %u vs received: %u\n",
485 expected_handle
, lur
->handle
);
488 if (lur
->cmd
!= expected_cmd
) {
489 fprintf(stderr
, "Unexpected result message command "
490 "expected: %u vs received: %u\n",
491 expected_cmd
, lur
->cmd
);
494 return lur
->ret_code
;
497 /* Transport level error */
498 if (errno
== EPIPE
|| errno
== ECONNRESET
)
502 fprintf(stderr
, "incorrect message size: %zd\n", len
);
508 int ustcomm_send_app_cmd(int sock
,
509 struct ustcomm_ust_msg
*lum
,
510 struct ustcomm_ust_reply
*lur
)
514 ret
= ustcomm_send_app_msg(sock
, lum
);
517 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
524 * chan_data is allocated internally if this function returns the
527 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
528 void **_chan_data
, uint64_t var_len
)
533 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
537 /* Receive variable length data */
538 chan_data
= zmalloc(var_len
);
543 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
544 if (len
!= var_len
) {
547 *_chan_data
= chan_data
;
557 int ustcomm_recv_stream_from_sessiond(int sock
,
558 uint64_t *memory_map_size
,
559 int *shm_fd
, int *wakeup_fd
)
565 /* recv shm fd and wakeup fd */
566 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);