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",
52 * Receives positive error value.
53 * Return ptr to string representing a human readable
54 * error code from the ustcomm_return_code enum.
56 const char *lttng_ust_strerror(int code
)
58 if (code
== LTTNG_UST_OK
)
59 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
60 if (code
< LTTNG_UST_ERR
)
61 return strerror(code
);
62 if (code
>= LTTNG_UST_ERR_NR
)
64 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
69 * ustcomm_connect_unix_sock
71 * Connect to unix socket using the path name.
73 int ustcomm_connect_unix_sock(const char *pathname
)
75 struct sockaddr_un sun
;
79 * libust threads require the close-on-exec flag for all
80 * resources so it does not leak file descriptors upon exec.
82 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
88 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
95 memset(&sun
, 0, sizeof(sun
));
96 sun
.sun_family
= AF_UNIX
;
97 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
98 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
100 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
103 * Don't print message on connect error, because connect
104 * is used in normal execution to detect if sessiond is
118 closeret
= close(fd
);
127 * ustcomm_accept_unix_sock
129 * Do an accept(2) on the sock and return the
130 * new file descriptor. The socket MUST be bind(2) before.
132 int ustcomm_accept_unix_sock(int sock
)
135 struct sockaddr_un sun
;
139 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
148 * ustcomm_create_unix_sock
150 * Creates a AF_UNIX local socket using pathname
151 * bind the socket upon creation and return the fd.
153 int ustcomm_create_unix_sock(const char *pathname
)
155 struct sockaddr_un sun
;
158 /* Create server socket */
159 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
165 memset(&sun
, 0, sizeof(sun
));
166 sun
.sun_family
= AF_UNIX
;
167 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
168 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
170 /* Unlink the old file if present */
171 (void) unlink(pathname
);
172 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
185 closeret
= close(fd
);
195 * ustcomm_listen_unix_sock
197 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
199 int ustcomm_listen_unix_sock(int sock
)
203 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
213 * ustcomm_recv_unix_sock
215 * Receive data of size len in put that data into
216 * the buf param. Using recvmsg API.
217 * Return the size of received data.
219 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
225 memset(&msg
, 0, sizeof(msg
));
227 iov
[0].iov_base
= buf
;
228 iov
[0].iov_len
= len
;
233 ret
= recvmsg(sock
, &msg
, 0);
234 } while (ret
< 0 && errno
== EINTR
);
243 shutret
= shutdown(sock
, SHUT_RDWR
);
245 fprintf(stderr
, "Socket shutdown error");
252 * ustcomm_send_unix_sock
254 * Send buf data of size len. Using sendmsg API.
255 * Return the size of sent data.
257 ssize_t
ustcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
263 memset(&msg
, 0, sizeof(msg
));
265 iov
[0].iov_base
= buf
;
266 iov
[0].iov_len
= len
;
271 * Using the MSG_NOSIGNAL when sending data from sessiond to
272 * libust, so libust does not receive an unhandled SIGPIPE or
273 * SIGURG. The sessiond receiver side can be made more resilient
274 * by ignoring SIGPIPE, but we don't have this luxury on the
278 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
279 } while (ret
< 0 && errno
== EINTR
);
288 shutret
= shutdown(sock
, SHUT_RDWR
);
290 fprintf(stderr
, "Socket shutdown error");
297 * ustcomm_close_unix_sock
299 * Shutdown cleanly a unix socket.
301 int ustcomm_close_unix_sock(int sock
)
315 * ustcomm_send_fds_unix_sock
317 * Send multiple fds on a unix socket.
319 ssize_t
ustcomm_send_fds_unix_sock(int sock
, void *buf
, int *fds
, size_t nb_fd
, size_t len
)
322 struct cmsghdr
*cmptr
;
325 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
326 char tmp
[CMSG_SPACE(sizeof_fds
)];
328 memset(&msg
, 0, sizeof(msg
));
331 * Note: we currently only support sending a single FD per
336 msg
.msg_control
= (caddr_t
)tmp
;
337 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
339 cmptr
= CMSG_FIRSTHDR(&msg
);
340 cmptr
->cmsg_level
= SOL_SOCKET
;
341 cmptr
->cmsg_type
= SCM_RIGHTS
;
342 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
343 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
344 /* Sum of the length of all control messages in the buffer: */
345 msg
.msg_controllen
= cmptr
->cmsg_len
;
347 iov
[0].iov_base
= buf
;
348 iov
[0].iov_len
= len
;
353 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
354 } while (ret
< 0 && errno
== EINTR
);
363 shutret
= shutdown(sock
, SHUT_RDWR
);
365 fprintf(stderr
, "Socket shutdown error");
371 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
375 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
381 if (len
== -ECONNRESET
)
382 fprintf(stderr
, "remote end closed connection\n");
385 fprintf(stderr
, "incorrect message size: %zd\n", len
);
392 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
393 uint32_t expected_handle
, uint32_t expected_cmd
)
397 memset(lur
, 0, sizeof(*lur
));
398 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
400 case 0: /* orderly shutdown */
403 if (lur
->handle
!= expected_handle
) {
404 fprintf(stderr
, "Unexpected result message handle\n");
407 if (lur
->cmd
!= expected_cmd
) {
408 fprintf(stderr
, "Unexpected result message command\n");
411 return lur
->ret_code
;
414 /* Transport level error */
415 if (len
== -ECONNRESET
)
416 fprintf(stderr
, "remote end closed connection\n");
419 fprintf(stderr
, "incorrect message size: %zd\n", len
);
425 int ustcomm_send_app_cmd(int sock
,
426 struct ustcomm_ust_msg
*lum
,
427 struct ustcomm_ust_reply
*lur
)
431 ret
= ustcomm_send_app_msg(sock
, lum
);
434 return ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
438 * Receives a single fd from socket.
440 * Returns negative error value on error, or file descriptor number on
443 int ustcomm_recv_fd(int sock
)
448 struct cmsghdr
*cmsg
;
449 char recv_fd
[CMSG_SPACE(sizeof(int))];
457 memset(&msg
, 0, sizeof(msg
));
459 /* Prepare to receive the structures */
460 iov
[0].iov_base
= &data_fd
;
461 iov
[0].iov_len
= sizeof(data_fd
);
464 msg
.msg_control
= recv_fd
;
465 msg
.msg_controllen
= sizeof(recv_fd
);
468 ret
= recvmsg(sock
, &msg
, 0);
469 } while (ret
< 0 && errno
== EINTR
);
471 if (errno
!= EPIPE
) {
477 if (ret
!= sizeof(data_fd
)) {
478 fprintf(stderr
, "Received %d bytes, expected %zd", ret
, sizeof(data_fd
));
482 cmsg
= CMSG_FIRSTHDR(&msg
);
484 fprintf(stderr
, "Invalid control message header\n");
488 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
489 fprintf(stderr
, "Didn't received any fd\n");
494 for (i
= 0; i
< sizeof(int); i
++)
495 tmp
.vc
[i
] = CMSG_DATA(cmsg
)[i
];
498 * Useful for fd leak debug.
499 * fprintf(stderr, "received fd %d\n", ret);
505 shutret
= shutdown(sock
, SHUT_RDWR
);
507 fprintf(stderr
, "Socket shutdown error");