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>
35 #include <lttng/ust-error.h>
37 #define USTCOMM_CODE_OFFSET(code) \
38 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
41 * Human readable error message.
43 static const char *ustcomm_readable_code
[] = {
44 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
45 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
46 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
47 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
48 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
49 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
50 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
56 * Receives positive error value.
57 * Return ptr to string representing a human readable
58 * error code from the ustcomm_return_code enum.
60 const char *lttng_ust_strerror(int code
)
62 if (code
== LTTNG_UST_OK
)
63 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
64 if (code
< LTTNG_UST_ERR
)
65 return strerror(code
);
66 if (code
>= LTTNG_UST_ERR_NR
)
68 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
73 * ustcomm_connect_unix_sock
75 * Connect to unix socket using the path name.
77 int ustcomm_connect_unix_sock(const char *pathname
)
79 struct sockaddr_un sun
;
83 * libust threads require the close-on-exec flag for all
84 * resources so it does not leak file descriptors upon exec.
86 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
92 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
99 memset(&sun
, 0, sizeof(sun
));
100 sun
.sun_family
= AF_UNIX
;
101 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
102 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
104 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
107 * Don't print message on connect error, because connect
108 * is used in normal execution to detect if sessiond is
122 closeret
= close(fd
);
131 * ustcomm_accept_unix_sock
133 * Do an accept(2) on the sock and return the
134 * new file descriptor. The socket MUST be bind(2) before.
136 int ustcomm_accept_unix_sock(int sock
)
139 struct sockaddr_un sun
;
143 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
152 * ustcomm_create_unix_sock
154 * Creates a AF_UNIX local socket using pathname
155 * bind the socket upon creation and return the fd.
157 int ustcomm_create_unix_sock(const char *pathname
)
159 struct sockaddr_un sun
;
162 /* Create server socket */
163 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
169 memset(&sun
, 0, sizeof(sun
));
170 sun
.sun_family
= AF_UNIX
;
171 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
172 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
174 /* Unlink the old file if present */
175 (void) unlink(pathname
);
176 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
189 closeret
= close(fd
);
199 * ustcomm_listen_unix_sock
201 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
203 int ustcomm_listen_unix_sock(int sock
)
207 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
217 * ustcomm_recv_unix_sock
219 * Receive data of size len in put that data into
220 * the buf param. Using recvmsg API.
221 * Return the size of received data.
223 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
229 memset(&msg
, 0, sizeof(msg
));
231 iov
[0].iov_base
= buf
;
232 iov
[0].iov_len
= len
;
237 ret
= recvmsg(sock
, &msg
, 0);
238 } while (ret
< 0 && errno
== EINTR
);
247 shutret
= shutdown(sock
, SHUT_RDWR
);
249 fprintf(stderr
, "Socket shutdown error");
256 * ustcomm_send_unix_sock
258 * Send buf data of size len. Using sendmsg API.
259 * Return the size of sent data.
261 ssize_t
ustcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
267 memset(&msg
, 0, sizeof(msg
));
269 iov
[0].iov_base
= buf
;
270 iov
[0].iov_len
= len
;
275 * Using the MSG_NOSIGNAL when sending data from sessiond to
276 * libust, so libust does not receive an unhandled SIGPIPE or
277 * SIGURG. The sessiond receiver side can be made more resilient
278 * by ignoring SIGPIPE, but we don't have this luxury on the
282 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
283 } while (ret
< 0 && errno
== EINTR
);
292 shutret
= shutdown(sock
, SHUT_RDWR
);
294 fprintf(stderr
, "Socket shutdown error");
301 * ustcomm_close_unix_sock
303 * Shutdown cleanly a unix socket.
305 int ustcomm_close_unix_sock(int sock
)
319 * ustcomm_send_fds_unix_sock
321 * Send multiple fds on a unix socket.
323 ssize_t
ustcomm_send_fds_unix_sock(int sock
, void *buf
, int *fds
, size_t nb_fd
, size_t len
)
326 struct cmsghdr
*cmptr
;
329 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
330 char tmp
[CMSG_SPACE(sizeof_fds
)];
332 memset(&msg
, 0, sizeof(msg
));
335 * Note: we currently only support sending a single FD per
340 msg
.msg_control
= (caddr_t
)tmp
;
341 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
343 cmptr
= CMSG_FIRSTHDR(&msg
);
344 cmptr
->cmsg_level
= SOL_SOCKET
;
345 cmptr
->cmsg_type
= SCM_RIGHTS
;
346 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
347 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
348 /* Sum of the length of all control messages in the buffer: */
349 msg
.msg_controllen
= cmptr
->cmsg_len
;
351 iov
[0].iov_base
= buf
;
352 iov
[0].iov_len
= len
;
357 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
358 } while (ret
< 0 && errno
== EINTR
);
367 shutret
= shutdown(sock
, SHUT_RDWR
);
369 fprintf(stderr
, "Socket shutdown error");
375 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
379 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
385 if (len
== -ECONNRESET
)
386 fprintf(stderr
, "remote end closed connection\n");
389 fprintf(stderr
, "incorrect message size: %zd\n", len
);
396 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
397 uint32_t expected_handle
, uint32_t expected_cmd
)
401 memset(lur
, 0, sizeof(*lur
));
402 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
404 case 0: /* orderly shutdown */
407 if (lur
->handle
!= expected_handle
) {
408 fprintf(stderr
, "Unexpected result message handle\n");
411 if (lur
->cmd
!= expected_cmd
) {
412 fprintf(stderr
, "Unexpected result message command\n");
415 return lur
->ret_code
;
418 /* Transport level error */
419 if (len
== -ECONNRESET
)
420 fprintf(stderr
, "remote end closed connection\n");
423 fprintf(stderr
, "incorrect message size: %zd\n", len
);
429 int ustcomm_send_app_cmd(int sock
,
430 struct ustcomm_ust_msg
*lum
,
431 struct ustcomm_ust_reply
*lur
)
435 ret
= ustcomm_send_app_msg(sock
, lum
);
438 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
445 * Receives a single fd from socket.
447 * Returns negative error value on error, or file descriptor number on
450 int ustcomm_recv_fd(int sock
)
455 struct cmsghdr
*cmsg
;
456 char recv_fd
[CMSG_SPACE(sizeof(int))];
464 memset(&msg
, 0, sizeof(msg
));
466 /* Prepare to receive the structures */
467 iov
[0].iov_base
= &data_fd
;
468 iov
[0].iov_len
= sizeof(data_fd
);
471 msg
.msg_control
= recv_fd
;
472 msg
.msg_controllen
= sizeof(recv_fd
);
475 ret
= recvmsg(sock
, &msg
, 0);
476 } while (ret
< 0 && errno
== EINTR
);
478 if (errno
!= EPIPE
) {
484 if (ret
!= sizeof(data_fd
)) {
485 fprintf(stderr
, "Received %d bytes, expected %zd", ret
, sizeof(data_fd
));
489 cmsg
= CMSG_FIRSTHDR(&msg
);
491 fprintf(stderr
, "Invalid control message header\n");
495 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
496 fprintf(stderr
, "Didn't received any fd\n");
501 for (i
= 0; i
< sizeof(int); i
++)
502 tmp
.vc
[i
] = CMSG_DATA(cmsg
)[i
];
505 * Useful for fd leak debug.
506 * fprintf(stderr, "received fd %d\n", ret);
512 shutret
= shutdown(sock
, SHUT_RDWR
);
514 fprintf(stderr
, "Socket shutdown error");