Fix: sendmsg should retry on EINTR, and use MSG_NOSIGNAL
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #define _GNU_SOURCE
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <errno.h>
32
33 #include <ust-comm.h>
34
35 /*
36 * Human readable error message.
37 */
38 static const char *ustcomm_readable_code[] = {
39 [ USTCOMM_ERR_INDEX(USTCOMM_ERR) ] = "Unknown error",
40 [ USTCOMM_ERR_INDEX(USTCOMM_UND) ] = "Undefined command",
41 [ USTCOMM_ERR_INDEX(USTCOMM_NOT_IMPLEMENTED) ] = "Not implemented",
42 [ USTCOMM_ERR_INDEX(USTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain",
43 [ USTCOMM_ERR_INDEX(USTCOMM_NO_SESSION) ] = "No session found",
44 [ USTCOMM_ERR_INDEX(USTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
45 [ USTCOMM_ERR_INDEX(USTCOMM_NO_APPS) ] = "No traceable apps found",
46 [ USTCOMM_ERR_INDEX(USTCOMM_SESS_NOT_FOUND) ] = "Session name not found",
47 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACE) ] = "No trace found",
48 [ USTCOMM_ERR_INDEX(USTCOMM_FATAL) ] = "Fatal error of the session daemon",
49 [ USTCOMM_ERR_INDEX(USTCOMM_CREATE_FAIL) ] = "Create trace failed",
50 [ USTCOMM_ERR_INDEX(USTCOMM_START_FAIL) ] = "Start trace failed",
51 [ USTCOMM_ERR_INDEX(USTCOMM_STOP_FAIL) ] = "Stop trace failed",
52 [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACEABLE) ] = "App is not traceable",
53 [ USTCOMM_ERR_INDEX(USTCOMM_SELECT_SESS) ] = "A session MUST be selected",
54 [ USTCOMM_ERR_INDEX(USTCOMM_EXIST_SESS) ] = "Session name already exist",
55 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NA) ] = "UST tracer not available",
56 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_EVENT_EXIST) ] = "UST event already exists",
57 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_SESS_FAIL) ] = "UST create session failed",
58 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_FAIL) ] = "UST create channel failed",
59 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_NOT_FOUND) ] = "UST channel not found",
60 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed",
61 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed",
62 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONTEXT_FAIL) ] = "Add UST context failed",
63 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_ENABLE_FAIL) ] = "Enable UST event failed",
64 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DISABLE_FAIL) ] = "Disable UST event failed",
65 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_META_FAIL) ] = "Opening metadata failed",
66 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_START_FAIL) ] = "Starting UST trace failed",
67 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STOP_FAIL) ] = "Stoping UST trace failed",
68 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONSUMER_FAIL) ] = "UST consumer start failed",
69 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STREAM_FAIL) ] = "UST create stream failed",
70 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_FAIL) ] = "UST trace directory creation failed",
71 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_EXIST) ] = "UST trace directory already exist",
72 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NO_SESSION) ] = "No UST session found",
73 [ USTCOMM_ERR_INDEX(USTCOMM_KERN_LIST_FAIL) ] = "Listing UST events failed",
74 [ USTCOMM_ERR_INDEX(USTCONSUMER_COMMAND_SOCK_READY) ] = "UST consumer command socket ready",
75 [ USTCOMM_ERR_INDEX(USTCONSUMER_SUCCESS_RECV_FD) ] = "UST consumer success on receiving fds",
76 [ USTCOMM_ERR_INDEX(USTCONSUMER_ERROR_RECV_FD) ] = "UST consumer error on receiving fds",
77 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_ERROR) ] = "UST consumer error in polling thread",
78 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_NVAL) ] = "UST consumer polling on closed fd",
79 [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_HUP) ] = "UST consumer all fd hung up",
80 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_SUCCESS) ] = "UST consumer exiting normally",
81 [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_FAILURE) ] = "UST consumer exiting on error",
82 [ USTCOMM_ERR_INDEX(USTCONSUMER_OUTFD_ERROR) ] = "UST consumer error opening the tracefile",
83 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EBADF) ] = "UST consumer splice EBADF",
84 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EINVAL) ] = "UST consumer splice EINVAL",
85 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ENOMEM) ] = "UST consumer splice ENOMEM",
86 [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ESPIPE) ] = "UST consumer splice ESPIPE",
87 [ USTCOMM_ERR_INDEX(USTCOMM_NO_EVENT) ] = "Event not found",
88 };
89
90 /*
91 * lttcom_get_readable_code
92 *
93 * Return ptr to string representing a human readable
94 * error code from the ustcomm_return_code enum.
95 */
96 const char *ustcomm_get_readable_code(int code)
97 {
98 if (code == USTCOMM_OK) {
99 return "Success";
100 }
101 if (code >= USTCOMM_ERR && code < USTCOMM_NR) {
102 return ustcomm_readable_code[USTCOMM_ERR_INDEX(code)];
103 }
104 return strerror(code);
105 }
106
107 /*
108 * ustcomm_connect_unix_sock
109 *
110 * Connect to unix socket using the path name.
111 */
112 int ustcomm_connect_unix_sock(const char *pathname)
113 {
114 struct sockaddr_un sun;
115 int fd;
116 int ret;
117
118 /*
119 * libust threads require the close-on-exec flag for all
120 * resources so it does not leak file descriptors upon exec.
121 */
122 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
123 if (fd < 0) {
124 perror("socket");
125 ret = fd;
126 goto error;
127 }
128
129 memset(&sun, 0, sizeof(sun));
130 sun.sun_family = AF_UNIX;
131 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
132 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
133
134 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
135 if (ret < 0) {
136 /*
137 * Don't print message on connect error, because connect
138 * is used in normal execution to detect if sessiond is
139 * alive.
140 */
141 goto error_connect;
142 }
143
144 return fd;
145
146 error_connect:
147 close(fd);
148 error:
149 return ret;
150 }
151
152 /*
153 * ustcomm_accept_unix_sock
154 *
155 * Do an accept(2) on the sock and return the
156 * new file descriptor. The socket MUST be bind(2) before.
157 */
158 int ustcomm_accept_unix_sock(int sock)
159 {
160 int new_fd;
161 struct sockaddr_un sun;
162 socklen_t len = 0;
163
164 /* Blocking call */
165 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
166 if (new_fd < 0) {
167 perror("accept");
168 goto error;
169 }
170
171 return new_fd;
172
173 error:
174 return -1;
175 }
176
177 /*
178 * ustcomm_create_unix_sock
179 *
180 * Creates a AF_UNIX local socket using pathname
181 * bind the socket upon creation and return the fd.
182 */
183 int ustcomm_create_unix_sock(const char *pathname)
184 {
185 struct sockaddr_un sun;
186 int fd;
187 int ret = -1;
188
189 /* Create server socket */
190 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
191 perror("socket");
192 goto error;
193 }
194
195 memset(&sun, 0, sizeof(sun));
196 sun.sun_family = AF_UNIX;
197 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
198 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
199
200 /* Unlink the old file if present */
201 (void) unlink(pathname);
202 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
203 if (ret < 0) {
204 perror("bind");
205 goto error;
206 }
207
208 return fd;
209
210 error:
211 return ret;
212 }
213
214 /*
215 * ustcomm_listen_unix_sock
216 *
217 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
218 */
219 int ustcomm_listen_unix_sock(int sock)
220 {
221 int ret;
222
223 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
224 if (ret < 0) {
225 perror("listen");
226 }
227
228 return ret;
229 }
230
231 /*
232 * ustcomm_recv_unix_sock
233 *
234 * Receive data of size len in put that data into
235 * the buf param. Using recvmsg API.
236 * Return the size of received data.
237 */
238 ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
239 {
240 struct msghdr msg;
241 struct iovec iov[1];
242 ssize_t ret = -1;
243
244 memset(&msg, 0, sizeof(msg));
245
246 iov[0].iov_base = buf;
247 iov[0].iov_len = len;
248 msg.msg_iov = iov;
249 msg.msg_iovlen = 1;
250
251 do {
252 ret = recvmsg(sock, &msg, 0);
253 } while (ret < 0 && errno == EINTR);
254 if (ret < 0 && errno != EPIPE) {
255 perror("recvmsg");
256 }
257
258 return ret;
259 }
260
261 /*
262 * ustcomm_send_unix_sock
263 *
264 * Send buf data of size len. Using sendmsg API.
265 * Return the size of sent data.
266 */
267 ssize_t ustcomm_send_unix_sock(int sock, void *buf, size_t len)
268 {
269 struct msghdr msg;
270 struct iovec iov[1];
271 ssize_t ret = -1;
272
273 memset(&msg, 0, sizeof(msg));
274
275 iov[0].iov_base = buf;
276 iov[0].iov_len = len;
277 msg.msg_iov = iov;
278 msg.msg_iovlen = 1;
279
280 /*
281 * Using the MSG_NOSIGNAL when sending data from sessiond to
282 * libust, so libust does not receive an unhandled SIGPIPE or
283 * SIGURG. The sessiond receiver side can be made more resilient
284 * by ignoring SIGPIPE, but we don't have this luxury on the
285 * libust side.
286 */
287 do {
288 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
289 } while (ret < 0 && errno == EINTR);
290 if (ret < 0 && errno != EPIPE) {
291 perror("sendmsg");
292 }
293
294 return ret;
295 }
296
297 /*
298 * ustcomm_close_unix_sock
299 *
300 * Shutdown cleanly a unix socket.
301 */
302 int ustcomm_close_unix_sock(int sock)
303 {
304 int ret;
305
306 ret = close(sock);
307 if (ret < 0) {
308 perror("close");
309 }
310
311 return ret;
312 }
313
314 /*
315 * ustcomm_send_fds_unix_sock
316 *
317 * Send multiple fds on a unix socket.
318 */
319 ssize_t ustcomm_send_fds_unix_sock(int sock, void *buf, int *fds, size_t nb_fd, size_t len)
320 {
321 struct msghdr msg;
322 struct cmsghdr *cmptr;
323 struct iovec iov[1];
324 ssize_t ret = -1;
325 unsigned int sizeof_fds = nb_fd * sizeof(int);
326 char tmp[CMSG_SPACE(sizeof_fds)];
327
328 memset(&msg, 0, sizeof(msg));
329
330 /*
331 * Note: the consumerd receiver only supports receiving one FD per
332 * message.
333 */
334 assert(nb_fd == 1);
335
336 msg.msg_control = (caddr_t)tmp;
337 msg.msg_controllen = CMSG_LEN(sizeof_fds);
338
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;
346
347 iov[0].iov_base = buf;
348 iov[0].iov_len = len;
349 msg.msg_iov = iov;
350 msg.msg_iovlen = 1;
351
352 do {
353 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
354 } while (ret < 0 && errno == EINTR);
355 if (ret < 0 && errno != EPIPE) {
356 perror("sendmsg");
357 }
358
359 return ret;
360 }
361
362 int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
363 {
364 ssize_t len;
365
366 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
367 switch (len) {
368 case sizeof(*lum):
369 break;
370 case -1:
371 if (errno == ECONNRESET) {
372 fprintf(stderr, "remote end closed connection\n");
373 return 0;
374 }
375 return -1;
376 default:
377 fprintf(stderr, "incorrect message size: %zd\n", len);
378 return -1;
379 }
380 return 0;
381 }
382
383 int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
384 uint32_t expected_handle, uint32_t expected_cmd)
385 {
386 ssize_t len;
387
388 memset(lur, 0, sizeof(*lur));
389 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
390 switch (len) {
391 case 0: /* orderly shutdown */
392 return -EINVAL;
393 case sizeof(*lur):
394 if (lur->handle != expected_handle) {
395 fprintf(stderr, "Unexpected result message handle\n");
396 return -EINVAL;
397 }
398
399 if (lur->cmd != expected_cmd) {
400 fprintf(stderr, "Unexpected result message command\n");
401 return -EINVAL;
402 }
403 if (lur->ret_code != USTCOMM_OK) {
404 /*
405 * Some errors are normal.. we should put this
406 * in a debug level message...
407 * fprintf(stderr, "remote operation failed with code %d.\n",
408 * lur->ret_code);
409 */
410 return lur->ret_code;
411 }
412 return 0;
413 case -1:
414 if (errno == ECONNRESET) {
415 fprintf(stderr, "remote end closed connection\n");
416 return -EINVAL;
417 }
418 return -1;
419 default:
420 fprintf(stderr, "incorrect message size: %zd\n", len);
421 return len > 0 ? -1 : len;
422 }
423 }
424
425 int ustcomm_send_app_cmd(int sock,
426 struct ustcomm_ust_msg *lum,
427 struct ustcomm_ust_reply *lur)
428 {
429 int ret;
430
431 ret = ustcomm_send_app_msg(sock, lum);
432 if (ret)
433 return ret;
434 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
435 if (ret)
436 return ret;
437 return 0;
438 }
439
440
441 /*
442 * Receives a single fd from socket.
443 *
444 * Returns the size of received data
445 */
446 int ustcomm_recv_fd(int sock)
447 {
448 struct iovec iov[1];
449 int ret = 0;
450 int data_fd;
451 struct cmsghdr *cmsg;
452 char recv_fd[CMSG_SPACE(sizeof(int))];
453 struct msghdr msg;
454 union {
455 unsigned char vc[4];
456 int vi;
457 } tmp;
458 int i;
459
460 memset(&msg, 0, sizeof(msg));
461
462 /* Prepare to receive the structures */
463 iov[0].iov_base = &data_fd;
464 iov[0].iov_len = sizeof(data_fd);
465 msg.msg_iov = iov;
466 msg.msg_iovlen = 1;
467 msg.msg_control = recv_fd;
468 msg.msg_controllen = sizeof(recv_fd);
469
470 do {
471 ret = recvmsg(sock, &msg, 0);
472 } while (ret < 0 && errno == EINTR);
473 if (ret < 0) {
474 if (errno != EPIPE) {
475 perror("recvmsg");
476 }
477 goto end;
478 }
479 if (ret != sizeof(data_fd)) {
480 fprintf(stderr, "Received %d bytes, expected %zd", ret, sizeof(data_fd));
481 goto end;
482 }
483 cmsg = CMSG_FIRSTHDR(&msg);
484 if (!cmsg) {
485 fprintf(stderr, "Invalid control message header\n");
486 ret = -1;
487 goto end;
488 }
489 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
490 fprintf(stderr, "Didn't received any fd\n");
491 ret = -1;
492 goto end;
493 }
494 /* this is our fd */
495 for (i = 0; i < sizeof(int); i++)
496 tmp.vc[i] = CMSG_DATA(cmsg)[i];
497 ret = tmp.vi;
498 /*
499 * Useful for fd leak debug.
500 * fprintf(stderr, "received fd %d\n", ret);
501 */
502 end:
503 return ret;
504 }
This page took 0.039945 seconds and 4 git commands to generate.