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