Fix: use the poll wait ret value when iterating on fd(s)
[lttng-tools.git] / src / bin / lttng-relayd / main.c
CommitLineData
b8aa1682
JD
1/*
2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
20#include <getopt.h>
21#include <grp.h>
22#include <limits.h>
23#include <pthread.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/mman.h>
29#include <sys/mount.h>
30#include <sys/resource.h>
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
173af62f 35#include <inttypes.h>
b8aa1682
JD
36#include <urcu/futex.h>
37#include <urcu/uatomic.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <config.h>
41
42#include <lttng/lttng.h>
43#include <common/common.h>
44#include <common/compat/poll.h>
45#include <common/compat/socket.h>
46#include <common/defaults.h>
47#include <common/futex.h>
48#include <common/sessiond-comm/sessiond-comm.h>
49#include <common/sessiond-comm/inet.h>
50#include <common/hashtable/hashtable.h>
51#include <common/sessiond-comm/relayd.h>
52#include <common/uri.h>
a02de639 53#include <common/utils.h>
b8aa1682
JD
54
55#include "lttng-relayd.h"
56
57/* command line options */
58static int opt_daemon;
59static char *opt_output_path;
095a4ae5
MD
60static struct lttng_uri *control_uri;
61static struct lttng_uri *data_uri;
b8aa1682
JD
62
63const char *progname;
64static int is_root; /* Set to 1 if the daemon is running as root */
65
66/*
67 * Quit pipe for all threads. This permits a single cancellation point
68 * for all threads when receiving an event on the pipe.
69 */
70static int thread_quit_pipe[2] = { -1, -1 };
71
72/*
73 * This pipe is used to inform the worker thread that a command is queued and
74 * ready to be processed.
75 */
76static int relay_cmd_pipe[2] = { -1, -1 };
77
26c9d55e 78/* Shared between threads */
b8aa1682
JD
79static int dispatch_thread_exit;
80
81static pthread_t listener_thread;
82static pthread_t dispatcher_thread;
83static pthread_t worker_thread;
84
095a4ae5
MD
85static uint64_t last_relay_stream_id;
86static uint64_t last_relay_session_id;
b8aa1682
JD
87
88/*
89 * Relay command queue.
90 *
91 * The relay_thread_listener and relay_thread_dispatcher communicate with this
92 * queue.
93 */
94static struct relay_cmd_queue relay_cmd_queue;
95
96/* buffer allocated at startup, used to store the trace data */
095a4ae5
MD
97static char *data_buffer;
98static unsigned int data_buffer_size;
b8aa1682
JD
99
100/*
101 * usage function on stderr
102 */
103static
104void usage(void)
105{
106 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
107 fprintf(stderr, " -h, --help Display this usage.\n");
108 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
109 fprintf(stderr, " -C, --control-port Control port listening (URI)\n");
110 fprintf(stderr, " -D, --data-port Data port listening (URI)\n");
25672d02 111 fprintf(stderr, " -o, --output Output path for traces (PATH)\n");
b8aa1682
JD
112 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
113}
114
115static
116int parse_args(int argc, char **argv)
117{
118 int c;
119 int ret = 0;
120 char *default_address;
121
122 static struct option long_options[] = {
e3678fd8
MD
123 { "control-port", 1, 0, 'C', },
124 { "data-port", 1, 0, 'D', },
125 { "daemonize", 0, 0, 'd', },
126 { "help", 0, 0, 'h', },
127 { "output", 1, 0, 'o', },
128 { "verbose", 0, 0, 'v', },
095a4ae5 129 { NULL, 0, 0, 0, },
b8aa1682
JD
130 };
131
132 while (1) {
133 int option_index = 0;
134 c = getopt_long(argc, argv, "dhv" "C:D:o:",
135 long_options, &option_index);
136 if (c == -1) {
137 break;
138 }
139
140 switch (c) {
141 case 0:
142 fprintf(stderr, "option %s", long_options[option_index].name);
143 if (optarg) {
144 fprintf(stderr, " with arg %s\n", optarg);
145 }
146 break;
147 case 'C':
148 ret = uri_parse(optarg, &control_uri);
149 if (ret < 0) {
150 ERR("Invalid control URI specified");
151 goto exit;
152 }
153 if (control_uri->port == 0) {
154 control_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
155 }
156 break;
157 case 'D':
158 ret = uri_parse(optarg, &data_uri);
159 if (ret < 0) {
160 ERR("Invalid data URI specified");
161 goto exit;
162 }
163 if (data_uri->port == 0) {
164 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
165 }
166 break;
167 case 'd':
168 opt_daemon = 1;
169 break;
170 case 'h':
171 usage();
172 exit(EXIT_FAILURE);
173 case 'o':
174 ret = asprintf(&opt_output_path, "%s", optarg);
175 if (ret < 0) {
176 PERROR("asprintf opt_output_path");
177 goto exit;
178 }
179 break;
180 case 'v':
181 /* Verbose level can increase using multiple -v */
182 lttng_opt_verbose += 1;
183 break;
184 default:
185 /* Unknown option or other error.
186 * Error is printed by getopt, just return */
187 ret = -1;
188 goto exit;
189 }
190 }
191
192 /* assign default values */
193 if (control_uri == NULL) {
194 ret = asprintf(&default_address, "tcp://0.0.0.0:%d",
195 DEFAULT_NETWORK_CONTROL_PORT);
196 if (ret < 0) {
197 PERROR("asprintf default data address");
198 goto exit;
199 }
200
201 ret = uri_parse(default_address, &control_uri);
202 free(default_address);
203 if (ret < 0) {
204 ERR("Invalid control URI specified");
205 goto exit;
206 }
207 }
208 if (data_uri == NULL) {
209 ret = asprintf(&default_address, "tcp://0.0.0.0:%d",
210 DEFAULT_NETWORK_DATA_PORT);
211 if (ret < 0) {
212 PERROR("asprintf default data address");
213 goto exit;
214 }
215
216 ret = uri_parse(default_address, &data_uri);
217 free(default_address);
218 if (ret < 0) {
219 ERR("Invalid data URI specified");
220 goto exit;
221 }
222 }
223
224exit:
225 return ret;
226}
227
228/*
229 * Cleanup the daemon
230 */
231static
232void cleanup(void)
233{
b8aa1682
JD
234 DBG("Cleaning up");
235
095a4ae5
MD
236 /* free the dynamically allocated opt_output_path */
237 free(opt_output_path);
238
a02de639
CB
239 /* Close thread quit pipes */
240 utils_close_pipe(thread_quit_pipe);
241
710c1f73
DG
242 uri_free(control_uri);
243 uri_free(data_uri);
b8aa1682
JD
244}
245
246/*
247 * Write to writable pipe used to notify a thread.
248 */
249static
250int notify_thread_pipe(int wpipe)
251{
252 int ret;
253
6f94560a
MD
254 do {
255 ret = write(wpipe, "!", 1);
256 } while (ret < 0 && errno == EINTR);
b8aa1682
JD
257 if (ret < 0) {
258 PERROR("write poll pipe");
259 }
260
261 return ret;
262}
263
264/*
265 * Stop all threads by closing the thread quit pipe.
266 */
267static
268void stop_threads(void)
269{
270 int ret;
271
272 /* Stopping all threads */
273 DBG("Terminating all threads");
274 ret = notify_thread_pipe(thread_quit_pipe[1]);
275 if (ret < 0) {
276 ERR("write error on thread quit pipe");
277 }
278
279 /* Dispatch thread */
26c9d55e 280 CMM_STORE_SHARED(dispatch_thread_exit, 1);
b8aa1682
JD
281 futex_nto1_wake(&relay_cmd_queue.futex);
282}
283
284/*
285 * Signal handler for the daemon
286 *
287 * Simply stop all worker threads, leaving main() return gracefully after
288 * joining all threads and calling cleanup().
289 */
290static
291void sighandler(int sig)
292{
293 switch (sig) {
294 case SIGPIPE:
295 DBG("SIGPIPE caught");
296 return;
297 case SIGINT:
298 DBG("SIGINT caught");
299 stop_threads();
300 break;
301 case SIGTERM:
302 DBG("SIGTERM caught");
303 stop_threads();
304 break;
305 default:
306 break;
307 }
308}
309
310/*
311 * Setup signal handler for :
312 * SIGINT, SIGTERM, SIGPIPE
313 */
314static
315int set_signal_handler(void)
316{
317 int ret = 0;
318 struct sigaction sa;
319 sigset_t sigset;
320
321 if ((ret = sigemptyset(&sigset)) < 0) {
322 PERROR("sigemptyset");
323 return ret;
324 }
325
326 sa.sa_handler = sighandler;
327 sa.sa_mask = sigset;
328 sa.sa_flags = 0;
329 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
330 PERROR("sigaction");
331 return ret;
332 }
333
334 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
335 PERROR("sigaction");
336 return ret;
337 }
338
339 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
340 PERROR("sigaction");
341 return ret;
342 }
343
344 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
345
346 return ret;
347}
348
349/*
350 * Init thread quit pipe.
351 *
352 * Return -1 on error or 0 if all pipes are created.
353 */
354static
355int init_thread_quit_pipe(void)
356{
a02de639 357 int ret;
b8aa1682 358
a02de639 359 ret = utils_create_pipe_cloexec(thread_quit_pipe);
b8aa1682 360
b8aa1682
JD
361 return ret;
362}
363
364/*
365 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
366 */
367static
368int create_thread_poll_set(struct lttng_poll_event *events, int size)
369{
370 int ret;
371
372 if (events == NULL || size == 0) {
373 ret = -1;
374 goto error;
375 }
376
377 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
378 if (ret < 0) {
379 goto error;
380 }
381
382 /* Add quit pipe */
383 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
384 if (ret < 0) {
385 goto error;
386 }
387
388 return 0;
389
390error:
391 return ret;
392}
393
394/*
395 * Check if the thread quit pipe was triggered.
396 *
397 * Return 1 if it was triggered else 0;
398 */
399static
400int check_thread_quit_pipe(int fd, uint32_t events)
401{
402 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
403 return 1;
404 }
405
406 return 0;
407}
408
409/*
410 * Create and init socket from uri.
411 */
412static
413struct lttcomm_sock *relay_init_sock(struct lttng_uri *uri)
414{
415 int ret;
416 struct lttcomm_sock *sock = NULL;
417
418 sock = lttcomm_alloc_sock_from_uri(uri);
419 if (sock == NULL) {
420 ERR("Allocating socket");
421 goto error;
422 }
423
424 ret = lttcomm_create_sock(sock);
425 if (ret < 0) {
426 goto error;
427 }
428 DBG("Listening on sock %d", sock->fd);
429
430 ret = sock->ops->bind(sock);
431 if (ret < 0) {
432 goto error;
433 }
434
435 ret = sock->ops->listen(sock, -1);
436 if (ret < 0) {
437 goto error;
438
439 }
440
441 return sock;
442
443error:
444 if (sock) {
445 lttcomm_destroy_sock(sock);
446 }
447 return NULL;
448}
449
173af62f
DG
450/*
451 * Return nonzero if stream needs to be closed.
452 */
453static
454int close_stream_check(struct relay_stream *stream)
455{
456
457 if (stream->close_flag && stream->prev_seq == stream->last_net_seq_num) {
f7079f67
DG
458 /*
459 * We are about to close the stream so set the data pending flag to 1
460 * which will make the end data pending command skip the stream which
461 * is now closed and ready. Note that after proceeding to a file close,
462 * the written file is ready for reading.
463 */
464 stream->data_pending_check_done = 1;
173af62f
DG
465 return 1;
466 }
467 return 0;
468}
469
b8aa1682
JD
470/*
471 * This thread manages the listening for new connections on the network
472 */
473static
474void *relay_thread_listener(void *data)
475{
095a4ae5 476 int i, ret, pollfd, err = -1;
b8aa1682
JD
477 int val = 1;
478 uint32_t revents, nb_fd;
479 struct lttng_poll_event events;
480 struct lttcomm_sock *control_sock, *data_sock;
481
b8aa1682
JD
482 DBG("[thread] Relay listener started");
483
484 control_sock = relay_init_sock(control_uri);
485 if (!control_sock) {
095a4ae5 486 goto error_sock_control;
b8aa1682
JD
487 }
488
489 data_sock = relay_init_sock(data_uri);
490 if (!data_sock) {
095a4ae5 491 goto error_sock_relay;
b8aa1682
JD
492 }
493
494 /*
495 * Pass 3 as size here for the thread quit pipe, control and data socket.
496 */
497 ret = create_thread_poll_set(&events, 3);
498 if (ret < 0) {
499 goto error_create_poll;
500 }
501
502 /* Add the control socket */
503 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
504 if (ret < 0) {
505 goto error_poll_add;
506 }
507
508 /* Add the data socket */
509 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
510 if (ret < 0) {
511 goto error_poll_add;
512 }
513
514 while (1) {
515 DBG("Listener accepting connections");
516
b8aa1682
JD
517restart:
518 ret = lttng_poll_wait(&events, -1);
519 if (ret < 0) {
520 /*
521 * Restart interrupted system call.
522 */
523 if (errno == EINTR) {
524 goto restart;
525 }
526 goto error;
527 }
528
0d9c5d77
DG
529 nb_fd = ret;
530
b8aa1682
JD
531 DBG("Relay new connection received");
532 for (i = 0; i < nb_fd; i++) {
533 /* Fetch once the poll data */
534 revents = LTTNG_POLL_GETEV(&events, i);
535 pollfd = LTTNG_POLL_GETFD(&events, i);
536
537 /* Thread quit pipe has been closed. Killing thread. */
538 ret = check_thread_quit_pipe(pollfd, revents);
539 if (ret) {
095a4ae5
MD
540 err = 0;
541 goto exit;
b8aa1682
JD
542 }
543
544 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
545 ERR("socket poll error");
546 goto error;
547 } else if (revents & LPOLLIN) {
4b7f17b2
MD
548 /*
549 * Get allocated in this thread,
550 * enqueued to a global queue, dequeued
551 * and freed in the worker thread.
552 */
553 struct relay_command *relay_cmd;
554 struct lttcomm_sock *newsock;
b8aa1682
JD
555
556 relay_cmd = zmalloc(sizeof(struct relay_command));
557 if (relay_cmd == NULL) {
558 PERROR("relay command zmalloc");
559 goto error;
560 }
561
562 if (pollfd == data_sock->fd) {
563 newsock = data_sock->ops->accept(data_sock);
4b7f17b2 564 if (!newsock) {
b8aa1682 565 PERROR("accepting data sock");
4b7f17b2 566 free(relay_cmd);
b8aa1682
JD
567 goto error;
568 }
569 relay_cmd->type = RELAY_DATA;
570 DBG("Relay data connection accepted, socket %d", newsock->fd);
4b7f17b2
MD
571 } else {
572 assert(pollfd == control_sock->fd);
b8aa1682 573 newsock = control_sock->ops->accept(control_sock);
4b7f17b2 574 if (!newsock) {
b8aa1682 575 PERROR("accepting control sock");
4b7f17b2 576 free(relay_cmd);
b8aa1682
JD
577 goto error;
578 }
579 relay_cmd->type = RELAY_CONTROL;
580 DBG("Relay control connection accepted, socket %d", newsock->fd);
581 }
582 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR,
583 &val, sizeof(int));
584 if (ret < 0) {
585 PERROR("setsockopt inet");
4b7f17b2
MD
586 lttcomm_destroy_sock(newsock);
587 free(relay_cmd);
b8aa1682
JD
588 goto error;
589 }
590 relay_cmd->sock = newsock;
591 /*
592 * Lock free enqueue the request.
593 */
594 cds_wfq_enqueue(&relay_cmd_queue.queue, &relay_cmd->node);
595
596 /*
597 * Wake the dispatch queue futex. Implicit memory
598 * barrier with the exchange in cds_wfq_enqueue.
599 */
600 futex_nto1_wake(&relay_cmd_queue.futex);
601 }
602 }
603 }
604
095a4ae5 605exit:
b8aa1682
JD
606error:
607error_poll_add:
608 lttng_poll_clean(&events);
609error_create_poll:
095a4ae5
MD
610 if (data_sock->fd >= 0) {
611 ret = data_sock->ops->close(data_sock);
b8aa1682
JD
612 if (ret) {
613 PERROR("close");
614 }
b8aa1682 615 }
095a4ae5
MD
616 lttcomm_destroy_sock(data_sock);
617error_sock_relay:
618 if (control_sock->fd >= 0) {
619 ret = control_sock->ops->close(control_sock);
b8aa1682
JD
620 if (ret) {
621 PERROR("close");
622 }
b8aa1682 623 }
095a4ae5
MD
624 lttcomm_destroy_sock(control_sock);
625error_sock_control:
626 if (err) {
627 DBG("Thread exited with error");
628 }
b8aa1682
JD
629 DBG("Relay listener thread cleanup complete");
630 stop_threads();
b8aa1682
JD
631 return NULL;
632}
633
634/*
635 * This thread manages the dispatching of the requests to worker threads
636 */
637static
638void *relay_thread_dispatcher(void *data)
639{
640 int ret;
641 struct cds_wfq_node *node;
642 struct relay_command *relay_cmd = NULL;
643
644 DBG("[thread] Relay dispatcher started");
645
26c9d55e 646 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
b8aa1682
JD
647 /* Atomically prepare the queue futex */
648 futex_nto1_prepare(&relay_cmd_queue.futex);
649
650 do {
651 /* Dequeue commands */
652 node = cds_wfq_dequeue_blocking(&relay_cmd_queue.queue);
653 if (node == NULL) {
654 DBG("Woken up but nothing in the relay command queue");
655 /* Continue thread execution */
656 break;
657 }
658
659 relay_cmd = caa_container_of(node, struct relay_command, node);
660 DBG("Dispatching request waiting on sock %d", relay_cmd->sock->fd);
661
662 /*
663 * Inform worker thread of the new request. This
664 * call is blocking so we can be assured that the data will be read
665 * at some point in time or wait to the end of the world :)
666 */
6f94560a
MD
667 do {
668 ret = write(relay_cmd_pipe[1], relay_cmd,
669 sizeof(struct relay_command));
670 } while (ret < 0 && errno == EINTR);
b8aa1682
JD
671 free(relay_cmd);
672 if (ret < 0) {
673 PERROR("write cmd pipe");
674 goto error;
675 }
676 } while (node != NULL);
677
678 /* Futex wait on queue. Blocking call on futex() */
679 futex_nto1_wait(&relay_cmd_queue.futex);
680 }
681
682error:
683 DBG("Dispatch thread dying");
684 stop_threads();
685 return NULL;
686}
687
688/*
689 * Return the realpath(3) of the path even if the last directory token does not
690 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
691 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
692 * fails if the end point directory does not exist.
693 */
694static
695char *expand_full_path(const char *path)
696{
697 const char *end_path = path;
095a4ae5 698 char *next, *cut_path, *expanded_path, *respath;
b8aa1682
JD
699
700 /* Find last token delimited by '/' */
701 while ((next = strpbrk(end_path + 1, "/"))) {
702 end_path = next;
703 }
704
705 /* Cut last token from original path */
706 cut_path = strndup(path, end_path - path);
707
708 expanded_path = malloc(PATH_MAX);
709 if (expanded_path == NULL) {
095a4ae5
MD
710 respath = NULL;
711 goto end;
b8aa1682
JD
712 }
713
095a4ae5
MD
714 respath = realpath(cut_path, expanded_path);
715 if (respath == NULL) {
b8aa1682
JD
716 switch (errno) {
717 case ENOENT:
718 ERR("%s: No such file or directory", cut_path);
719 break;
720 default:
721 PERROR("realpath");
722 break;
723 }
095a4ae5
MD
724 free(expanded_path);
725 } else {
726 /* Add end part to expanded path */
727 strcat(respath, end_path);
b8aa1682 728 }
095a4ae5 729end:
b8aa1682 730 free(cut_path);
095a4ae5 731 return respath;
b8aa1682
JD
732}
733
734
735/*
736 * config_get_default_path
737 *
738 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
739 */
740static
741char *config_get_default_path(void)
742{
743 return getenv("HOME");
744}
745
746/*
747 * Create recursively directory using the FULL path.
748 */
749static
750int mkdir_recursive(char *path, mode_t mode)
751{
752 char *p, tmp[PATH_MAX];
753 struct stat statbuf;
754 size_t len;
755 int ret;
756
757 ret = snprintf(tmp, sizeof(tmp), "%s", path);
758 if (ret < 0) {
759 PERROR("snprintf mkdir");
760 goto error;
761 }
762
763 len = ret;
764 if (tmp[len - 1] == '/') {
765 tmp[len - 1] = 0;
766 }
767
768 for (p = tmp + 1; *p; p++) {
769 if (*p == '/') {
770 *p = 0;
771 if (tmp[strlen(tmp) - 1] == '.' &&
772 tmp[strlen(tmp) - 2] == '.' &&
773 tmp[strlen(tmp) - 3] == '/') {
774 ERR("Using '/../' is not permitted in the trace path (%s)",
775 tmp);
776 ret = -1;
777 goto error;
778 }
779 ret = stat(tmp, &statbuf);
780 if (ret < 0) {
781 ret = mkdir(tmp, mode);
782 if (ret < 0) {
095a4ae5 783 if (errno != EEXIST) {
b8aa1682
JD
784 PERROR("mkdir recursive");
785 ret = -errno;
786 goto error;
787 }
788 }
789 }
790 *p = '/';
791 }
792 }
793
794 ret = mkdir(tmp, mode);
795 if (ret < 0) {
095a4ae5 796 if (errno != EEXIST) {
b8aa1682
JD
797 PERROR("mkdir recursive last piece");
798 ret = -errno;
799 } else {
800 ret = 0;
801 }
802 }
803
804error:
805 return ret;
806}
807
b8aa1682 808static
095a4ae5 809char *create_output_path_auto(char *path_name)
b8aa1682 810{
095a4ae5 811 int ret;
b8aa1682 812 char *traces_path = NULL;
095a4ae5
MD
813 char *alloc_path = NULL;
814 char *default_path;
b8aa1682 815
095a4ae5
MD
816 default_path = config_get_default_path();
817 if (default_path == NULL) {
818 ERR("Home path not found.\n \
819 Please specify an output path using -o, --output PATH");
820 goto exit;
821 }
822 alloc_path = strdup(default_path);
823 if (alloc_path == NULL) {
824 PERROR("Path allocation");
825 goto exit;
826 }
827 ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME
828 "/%s", alloc_path, path_name);
829 if (ret < 0) {
830 PERROR("asprintf trace dir name");
831 goto exit;
b8aa1682 832 }
095a4ae5 833exit:
b8aa1682 834 free(alloc_path);
095a4ae5
MD
835 return traces_path;
836}
837
838static
839char *create_output_path_noauto(char *path_name)
840{
841 int ret;
842 char *traces_path = NULL;
843 char *full_path;
b8aa1682 844
095a4ae5
MD
845 full_path = expand_full_path(opt_output_path);
846 ret = asprintf(&traces_path, "%s/%s", full_path, path_name);
847 if (ret < 0) {
848 PERROR("asprintf trace dir name");
849 goto exit;
850 }
b8aa1682 851exit:
095a4ae5 852 free(full_path);
b8aa1682
JD
853 return traces_path;
854}
855
095a4ae5
MD
856/*
857 * create_output_path: create the output trace directory
858 */
859static
860char *create_output_path(char *path_name)
861{
862 if (opt_output_path == NULL) {
863 return create_output_path_auto(path_name);
864 } else {
865 return create_output_path_noauto(path_name);
866 }
867}
868
9d1bbf21
MD
869static
870void deferred_free_stream(struct rcu_head *head)
871{
872 struct relay_stream *stream =
873 caa_container_of(head, struct relay_stream, rcu_node);
874 free(stream);
875}
876
b8aa1682
JD
877/*
878 * relay_delete_session: Free all memory associated with a session and
879 * close all the FDs
880 */
881static
882void relay_delete_session(struct relay_command *cmd, struct lttng_ht *streams_ht)
883{
884 struct lttng_ht_iter iter;
885 struct lttng_ht_node_ulong *node;
886 struct relay_stream *stream;
887 int ret;
888
095a4ae5 889 if (!cmd->session) {
b8aa1682 890 return;
095a4ae5 891 }
b8aa1682 892
77c7c900 893 DBG("Relay deleting session %" PRIu64, cmd->session->id);
5b6d8097 894
9d1bbf21 895 rcu_read_lock();
b8aa1682
JD
896 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, node, node) {
897 node = lttng_ht_iter_get_node_ulong(&iter);
898 if (node) {
899 stream = caa_container_of(node,
900 struct relay_stream, stream_n);
901 if (stream->session == cmd->session) {
f66c074c
DG
902 ret = close(stream->fd);
903 if (ret < 0) {
904 PERROR("close stream fd on delete session");
905 }
b8aa1682
JD
906 ret = lttng_ht_del(streams_ht, &iter);
907 assert(!ret);
9d1bbf21
MD
908 call_rcu(&stream->rcu_node,
909 deferred_free_stream);
b8aa1682
JD
910 }
911 }
912 }
9d1bbf21 913 rcu_read_unlock();
5b6d8097
DG
914
915 free(cmd->session);
b8aa1682
JD
916}
917
c5b6f4f0
DG
918/*
919 * Handle the RELAYD_CREATE_SESSION command.
920 *
921 * On success, send back the session id or else return a negative value.
922 */
923static
924int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr,
925 struct relay_command *cmd)
926{
927 int ret = 0, send_ret;
928 struct relay_session *session;
929 struct lttcomm_relayd_status_session reply;
930
931 assert(recv_hdr);
932 assert(cmd);
933
934 memset(&reply, 0, sizeof(reply));
935
936 session = zmalloc(sizeof(struct relay_session));
937 if (session == NULL) {
938 PERROR("relay session zmalloc");
939 ret = -1;
940 goto error;
941 }
942
943 session->id = ++last_relay_session_id;
944 session->sock = cmd->sock;
945 cmd->session = session;
946
947 reply.session_id = htobe64(session->id);
948
949 DBG("Created session %" PRIu64, session->id);
950
951error:
952 if (ret < 0) {
953 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
954 } else {
955 reply.ret_code = htobe32(LTTNG_OK);
956 }
957
958 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
959 if (send_ret < 0) {
960 ERR("Relayd sending session id");
961 }
962
963 return ret;
964}
965
b8aa1682
JD
966/*
967 * relay_add_stream: allocate a new stream for a session
968 */
969static
970int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
971 struct relay_command *cmd, struct lttng_ht *streams_ht)
972{
973 struct relay_session *session = cmd->session;
974 struct lttcomm_relayd_add_stream stream_info;
975 struct relay_stream *stream = NULL;
976 struct lttcomm_relayd_status_stream reply;
977 char *path = NULL, *root_path = NULL;
978 int ret, send_ret;
979
c5b6f4f0 980 if (!session || cmd->version_check_done == 0) {
b8aa1682
JD
981 ERR("Trying to add a stream before version check");
982 ret = -1;
983 goto end_no_session;
984 }
985
986 /* FIXME : use data_size for something ? */
987 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
7c5aef62 988 sizeof(struct lttcomm_relayd_add_stream), 0);
b8aa1682
JD
989 if (ret < sizeof(struct lttcomm_relayd_add_stream)) {
990 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
991 ret = -1;
992 goto end_no_session;
993 }
994 stream = zmalloc(sizeof(struct relay_stream));
995 if (stream == NULL) {
996 PERROR("relay stream zmalloc");
997 ret = -1;
998 goto end_no_session;
999 }
1000
9d1bbf21 1001 rcu_read_lock();
b8aa1682 1002 stream->stream_handle = ++last_relay_stream_id;
173af62f 1003 stream->prev_seq = -1ULL;
b8aa1682
JD
1004 stream->session = session;
1005
1006 root_path = create_output_path(stream_info.pathname);
1007 if (!root_path) {
1008 ret = -1;
1009 goto end;
1010 }
1011 ret = mkdir_recursive(root_path, S_IRWXU | S_IRWXG);
1012 if (ret < 0) {
b8aa1682
JD
1013 ERR("relay creating output directory");
1014 goto end;
1015 }
1016
1017 ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name);
1018 if (ret < 0) {
1019 PERROR("asprintf stream path");
1020 goto end;
1021 }
1022
1023 ret = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
1024 if (ret < 0) {
1025 PERROR("Relay creating trace file");
1026 goto end;
1027 }
1028
1029 stream->fd = ret;
1030 DBG("Tracefile %s created", path);
1031
1032 lttng_ht_node_init_ulong(&stream->stream_n,
1033 (unsigned long) stream->stream_handle);
1034 lttng_ht_add_unique_ulong(streams_ht,
1035 &stream->stream_n);
1036
1037 DBG("Relay new stream added %s", stream_info.channel_name);
1038
1039end:
1040 free(path);
1041 free(root_path);
1042 /* send the session id to the client or a negative return code on error */
1043 if (ret < 0) {
f73fabfd 1044 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682 1045 } else {
f73fabfd 1046 reply.ret_code = htobe32(LTTNG_OK);
b8aa1682
JD
1047 }
1048 reply.handle = htobe64(stream->stream_handle);
1049 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1050 sizeof(struct lttcomm_relayd_status_stream), 0);
1051 if (send_ret < 0) {
1052 ERR("Relay sending stream id");
1053 }
9d1bbf21 1054 rcu_read_unlock();
b8aa1682
JD
1055
1056end_no_session:
1057 return ret;
1058}
1059
173af62f
DG
1060/*
1061 * relay_close_stream: close a specific stream
1062 */
1063static
1064int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
1065 struct relay_command *cmd, struct lttng_ht *streams_ht)
1066{
1067 struct relay_session *session = cmd->session;
1068 struct lttcomm_relayd_close_stream stream_info;
1069 struct lttcomm_relayd_generic_reply reply;
1070 struct relay_stream *stream;
1071 int ret, send_ret;
1072 struct lttng_ht_node_ulong *node;
1073 struct lttng_ht_iter iter;
1074
1075 DBG("Close stream received");
1076
c5b6f4f0 1077 if (!session || cmd->version_check_done == 0) {
173af62f
DG
1078 ERR("Trying to close a stream before version check");
1079 ret = -1;
1080 goto end_no_session;
1081 }
1082
1083 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
7c5aef62 1084 sizeof(struct lttcomm_relayd_close_stream), 0);
173af62f
DG
1085 if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
1086 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
1087 ret = -1;
1088 goto end_no_session;
1089 }
1090
1091 rcu_read_lock();
1092 lttng_ht_lookup(streams_ht,
1093 (void *)((unsigned long) be64toh(stream_info.stream_id)),
1094 &iter);
1095 node = lttng_ht_iter_get_node_ulong(&iter);
1096 if (node == NULL) {
77c7c900 1097 DBG("Relay stream %" PRIu64 " not found", be64toh(stream_info.stream_id));
173af62f
DG
1098 ret = -1;
1099 goto end_unlock;
1100 }
1101
1102 stream = caa_container_of(node, struct relay_stream, stream_n);
1103 if (!stream) {
1104 ret = -1;
1105 goto end_unlock;
1106 }
1107
8e2583a4 1108 stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num);
173af62f
DG
1109 stream->close_flag = 1;
1110
1111 if (close_stream_check(stream)) {
1112 int delret;
1113
f66c074c
DG
1114 delret = close(stream->fd);
1115 if (delret < 0) {
1116 PERROR("close stream");
1117 }
173af62f
DG
1118 delret = lttng_ht_del(streams_ht, &iter);
1119 assert(!delret);
1120 call_rcu(&stream->rcu_node,
1121 deferred_free_stream);
1122 DBG("Closed tracefile %d from close stream", stream->fd);
1123 }
1124
1125end_unlock:
1126 rcu_read_unlock();
1127
1128 if (ret < 0) {
f73fabfd 1129 reply.ret_code = htobe32(LTTNG_ERR_UNK);
173af62f 1130 } else {
f73fabfd 1131 reply.ret_code = htobe32(LTTNG_OK);
173af62f
DG
1132 }
1133 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1134 sizeof(struct lttcomm_relayd_generic_reply), 0);
1135 if (send_ret < 0) {
1136 ERR("Relay sending stream id");
1137 }
1138
1139end_no_session:
1140 return ret;
1141}
1142
b8aa1682
JD
1143/*
1144 * relay_unknown_command: send -1 if received unknown command
1145 */
1146static
1147void relay_unknown_command(struct relay_command *cmd)
1148{
1149 struct lttcomm_relayd_generic_reply reply;
1150 int ret;
1151
f73fabfd 1152 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1153 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1154 sizeof(struct lttcomm_relayd_generic_reply), 0);
1155 if (ret < 0) {
1156 ERR("Relay sending unknown command");
1157 }
1158}
1159
1160/*
1161 * relay_start: send an acknowledgment to the client to tell if we are
1162 * ready to receive data. We are ready if a session is established.
1163 */
1164static
1165int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
1166 struct relay_command *cmd)
1167{
f73fabfd 1168 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1169 struct lttcomm_relayd_generic_reply reply;
1170 struct relay_session *session = cmd->session;
1171
1172 if (!session) {
1173 DBG("Trying to start the streaming without a session established");
f73fabfd 1174 ret = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1175 }
1176
1177 reply.ret_code = ret;
1178 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1179 sizeof(struct lttcomm_relayd_generic_reply), 0);
1180 if (ret < 0) {
1181 ERR("Relay sending start ack");
1182 }
1183
1184 return ret;
1185}
1186
1187/*
1188 * Get stream from stream id.
9d1bbf21 1189 * Need to be called with RCU read-side lock held.
b8aa1682
JD
1190 */
1191static
1192struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id,
1193 struct lttng_ht *streams_ht)
1194{
1195 struct lttng_ht_node_ulong *node;
1196 struct lttng_ht_iter iter;
1197 struct relay_stream *ret;
1198
1199 lttng_ht_lookup(streams_ht,
1200 (void *)((unsigned long) stream_id),
1201 &iter);
1202 node = lttng_ht_iter_get_node_ulong(&iter);
1203 if (node == NULL) {
77c7c900 1204 DBG("Relay stream %" PRIu64 " not found", stream_id);
b8aa1682
JD
1205 ret = NULL;
1206 goto end;
1207 }
1208
1209 ret = caa_container_of(node, struct relay_stream, stream_n);
1210
1211end:
1212 return ret;
1213}
1214
1d4dfdef
DG
1215/*
1216 * Append padding to the file pointed by the file descriptor fd.
1217 */
1218static int write_padding_to_file(int fd, uint32_t size)
1219{
1220 int ret = 0;
1221 char *zeros;
1222
1223 if (size == 0) {
1224 goto end;
1225 }
1226
1227 zeros = zmalloc(size);
1228 if (zeros == NULL) {
1229 PERROR("zmalloc zeros for padding");
1230 ret = -1;
1231 goto end;
1232 }
1233
1234 do {
1235 ret = write(fd, zeros, size);
1236 } while (ret < 0 && errno == EINTR);
1237 if (ret < 0) {
1238 PERROR("write padding to file");
1239 }
1240
e986c7a1
DG
1241 free(zeros);
1242
1d4dfdef
DG
1243end:
1244 return ret;
1245}
1246
b8aa1682
JD
1247/*
1248 * relay_recv_metadata: receive the metada for the session.
1249 */
1250static
1251int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
1252 struct relay_command *cmd, struct lttng_ht *streams_ht)
1253{
f73fabfd 1254 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1255 struct relay_session *session = cmd->session;
1256 struct lttcomm_relayd_metadata_payload *metadata_struct;
1257 struct relay_stream *metadata_stream;
1258 uint64_t data_size, payload_size;
1259
1260 if (!session) {
1261 ERR("Metadata sent before version check");
1262 ret = -1;
1263 goto end;
1264 }
1265
f6416125
MD
1266 data_size = payload_size = be64toh(recv_hdr->data_size);
1267 if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1268 ERR("Incorrect data size");
1269 ret = -1;
1270 goto end;
1271 }
1272 payload_size -= sizeof(struct lttcomm_relayd_metadata_payload);
1273
b8aa1682 1274 if (data_buffer_size < data_size) {
d7b3776f
DG
1275 /* In case the realloc fails, we can free the memory */
1276 char *tmp_data_ptr = data_buffer;
b8aa1682
JD
1277 data_buffer = realloc(data_buffer, data_size);
1278 if (!data_buffer) {
1279 ERR("Allocating data buffer");
d7b3776f 1280 free(tmp_data_ptr);
b8aa1682
JD
1281 ret = -1;
1282 goto end;
1283 }
1284 data_buffer_size = data_size;
1285 }
1286 memset(data_buffer, 0, data_size);
77c7c900 1287 DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
7c5aef62 1288 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682
JD
1289 if (ret < 0 || ret != data_size) {
1290 ret = -1;
1291 ERR("Relay didn't receive the whole metadata");
1292 goto end;
1293 }
1294 metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
9d1bbf21
MD
1295
1296 rcu_read_lock();
b8aa1682
JD
1297 metadata_stream = relay_stream_from_stream_id(
1298 be64toh(metadata_struct->stream_id), streams_ht);
1299 if (!metadata_stream) {
1300 ret = -1;
9d1bbf21 1301 goto end_unlock;
b8aa1682
JD
1302 }
1303
6f94560a
MD
1304 do {
1305 ret = write(metadata_stream->fd, metadata_struct->payload,
1306 payload_size);
1307 } while (ret < 0 && errno == EINTR);
87c1611d 1308 if (ret < payload_size) {
b8aa1682
JD
1309 ERR("Relay error writing metadata on file");
1310 ret = -1;
9d1bbf21 1311 goto end_unlock;
b8aa1682 1312 }
1d4dfdef
DG
1313
1314 ret = write_padding_to_file(metadata_stream->fd,
1315 be32toh(metadata_struct->padding_size));
1316 if (ret < 0) {
1317 goto end_unlock;
1318 }
1319
b8aa1682
JD
1320 DBG2("Relay metadata written");
1321
9d1bbf21 1322end_unlock:
6e3c5836 1323 rcu_read_unlock();
b8aa1682
JD
1324end:
1325 return ret;
1326}
1327
1328/*
1329 * relay_send_version: send relayd version number
1330 */
1331static
1332int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
1333 struct relay_command *cmd)
1334{
7f51dcba 1335 int ret;
092b6259 1336 struct lttcomm_relayd_version reply, msg;
b8aa1682 1337
c5b6f4f0
DG
1338 assert(cmd);
1339
1340 cmd->version_check_done = 1;
b8aa1682 1341
092b6259 1342 /* Get version from the other side. */
7c5aef62 1343 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
092b6259
DG
1344 if (ret < 0 || ret != sizeof(msg)) {
1345 ret = -1;
1346 ERR("Relay failed to receive the version values.");
1347 goto end;
1348 }
1349
1350 /*
1351 * For now, we just ignore the received version but after 2.1 stable
1352 * release, a check must be done to see if we either adapt to the other
1353 * side version (which MUST be lower than us) or keep the latest data
1354 * structure considering that the other side will adapt.
1355 */
1356
7f51dcba
MD
1357 ret = sscanf(VERSION, "%u.%u", &reply.major, &reply.minor);
1358 if (ret < 2) {
1359 ERR("Error in scanning version");
1360 ret = -1;
1361 goto end;
1362 }
b8aa1682
JD
1363 reply.major = htobe32(reply.major);
1364 reply.minor = htobe32(reply.minor);
1365 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1366 sizeof(struct lttcomm_relayd_version), 0);
1367 if (ret < 0) {
1368 ERR("Relay sending version");
1369 }
0a6b5085
DG
1370 DBG("Version check done (%u.%u)", be32toh(reply.major),
1371 be32toh(reply.minor));
b8aa1682
JD
1372
1373end:
1374 return ret;
1375}
1376
c8f59ee5 1377/*
6d805429 1378 * Check for data pending for a given stream id from the session daemon.
c8f59ee5
DG
1379 */
1380static
6d805429 1381int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
c8f59ee5
DG
1382 struct relay_command *cmd, struct lttng_ht *streams_ht)
1383{
1384 struct relay_session *session = cmd->session;
6d805429 1385 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
1386 struct lttcomm_relayd_generic_reply reply;
1387 struct relay_stream *stream;
1388 int ret;
1389 struct lttng_ht_node_ulong *node;
1390 struct lttng_ht_iter iter;
1391 uint64_t last_net_seq_num, stream_id;
1392
6d805429 1393 DBG("Data pending command received");
c8f59ee5 1394
c5b6f4f0 1395 if (!session || cmd->version_check_done == 0) {
c8f59ee5
DG
1396 ERR("Trying to check for data before version check");
1397 ret = -1;
1398 goto end_no_session;
1399 }
1400
7c5aef62 1401 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
c8f59ee5 1402 if (ret < sizeof(msg)) {
6d805429 1403 ERR("Relay didn't receive valid data_pending struct size : %d", ret);
c8f59ee5
DG
1404 ret = -1;
1405 goto end_no_session;
1406 }
1407
1408 stream_id = be64toh(msg.stream_id);
1409 last_net_seq_num = be64toh(msg.last_net_seq_num);
1410
1411 rcu_read_lock();
1412 lttng_ht_lookup(streams_ht, (void *)((unsigned long) stream_id), &iter);
1413 node = lttng_ht_iter_get_node_ulong(&iter);
1414 if (node == NULL) {
1415 DBG("Relay stream %" PRIu64 " not found", stream_id);
1416 ret = -1;
1417 goto end_unlock;
1418 }
1419
1420 stream = caa_container_of(node, struct relay_stream, stream_n);
1421 assert(stream);
1422
6d805429 1423 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
c8f59ee5
DG
1424 " and last_seq %" PRIu64, stream_id, stream->prev_seq,
1425 last_net_seq_num);
1426
33832e64
DG
1427 /* Avoid wrapping issue */
1428 if (((int64_t) (stream->prev_seq - last_net_seq_num)) <= 0) {
6d805429 1429 /* Data has in fact been written and is NOT pending */
c8f59ee5 1430 ret = 0;
6d805429
DG
1431 } else {
1432 /* Data still being streamed thus pending */
1433 ret = 1;
c8f59ee5
DG
1434 }
1435
f7079f67
DG
1436 /* Pending check is now done. */
1437 stream->data_pending_check_done = 1;
1438
c8f59ee5
DG
1439end_unlock:
1440 rcu_read_unlock();
1441
1442 reply.ret_code = htobe32(ret);
1443 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1444 if (ret < 0) {
6d805429 1445 ERR("Relay data pending ret code failed");
c8f59ee5
DG
1446 }
1447
1448end_no_session:
1449 return ret;
1450}
1451
1452/*
1453 * Wait for the control socket to reach a quiescent state.
1454 *
1455 * Note that for now, when receiving this command from the session daemon, this
1456 * means that every subsequent commands or data received on the control socket
1457 * has been handled. So, this is why we simply return OK here.
1458 */
1459static
1460int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
1461 struct relay_command *cmd)
1462{
1463 int ret;
1464 struct lttcomm_relayd_generic_reply reply;
1465
1466 DBG("Checking quiescent state on control socket");
1467
1468 reply.ret_code = htobe32(LTTNG_OK);
1469 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1470 if (ret < 0) {
6d805429 1471 ERR("Relay data quiescent control ret code failed");
c8f59ee5
DG
1472 }
1473
1474 return ret;
1475}
1476
f7079f67
DG
1477/*
1478 * Initialize a data pending command. This means that a client is about to ask
1479 * for data pending for each stream he/she holds. Simply iterate over all
1480 * streams of a session and set the data_pending_check_done flag.
1481 *
1482 * This command returns to the client a LTTNG_OK code.
1483 */
1484static
1485int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
1486 struct relay_command *cmd, struct lttng_ht *streams_ht)
1487{
1488 int ret;
1489 struct lttng_ht_iter iter;
1490 struct lttcomm_relayd_begin_data_pending msg;
1491 struct lttcomm_relayd_generic_reply reply;
1492 struct relay_stream *stream;
1493 uint64_t session_id;
1494
1495 assert(recv_hdr);
1496 assert(cmd);
1497 assert(streams_ht);
1498
1499 DBG("Init streams for data pending");
1500
1501 if (!cmd->session || cmd->version_check_done == 0) {
1502 ERR("Trying to check for data before version check");
1503 ret = -1;
1504 goto end_no_session;
1505 }
1506
1507 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1508 if (ret < sizeof(msg)) {
1509 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1510 ret);
1511 ret = -1;
1512 goto end_no_session;
1513 }
1514
1515 session_id = be64toh(msg.session_id);
1516
1517 /*
1518 * Iterate over all streams to set the begin data pending flag. For now, the
1519 * streams are indexed by stream handle so we have to iterate over all
1520 * streams to find the one associated with the right session_id.
1521 */
1522 rcu_read_lock();
1523 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
1524 if (stream->session->id == session_id) {
1525 stream->data_pending_check_done = 0;
1526 DBG("Set begin data pending flag to stream %" PRIu64,
1527 stream->stream_handle);
1528 }
1529 }
1530 rcu_read_unlock();
1531
1532 /* All good, send back reply. */
1533 reply.ret_code = htobe32(LTTNG_OK);
1534
1535 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1536 if (ret < 0) {
1537 ERR("Relay begin data pending send reply failed");
1538 }
1539
1540end_no_session:
1541 return ret;
1542}
1543
1544/*
1545 * End data pending command. This will check, for a given session id, if each
1546 * stream associated with it has its data_pending_check_done flag set. If not,
1547 * this means that the client lost track of the stream but the data is still
1548 * being streamed on our side. In this case, we inform the client that data is
1549 * inflight.
1550 *
1551 * Return to the client if there is data in flight or not with a ret_code.
1552 */
1553static
1554int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
1555 struct relay_command *cmd, struct lttng_ht *streams_ht)
1556{
1557 int ret;
1558 struct lttng_ht_iter iter;
1559 struct lttcomm_relayd_end_data_pending msg;
1560 struct lttcomm_relayd_generic_reply reply;
1561 struct relay_stream *stream;
1562 uint64_t session_id;
1563 uint32_t is_data_inflight = 0;
1564
1565 assert(recv_hdr);
1566 assert(cmd);
1567 assert(streams_ht);
1568
1569 DBG("End data pending command");
1570
1571 if (!cmd->session || cmd->version_check_done == 0) {
1572 ERR("Trying to check for data before version check");
1573 ret = -1;
1574 goto end_no_session;
1575 }
1576
1577 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1578 if (ret < sizeof(msg)) {
1579 ERR("Relay didn't receive valid end data_pending struct size: %d",
1580 ret);
1581 ret = -1;
1582 goto end_no_session;
1583 }
1584
1585 session_id = be64toh(msg.session_id);
1586
1587 /* Iterate over all streams to see if the begin data pending flag is set. */
1588 rcu_read_lock();
1589 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
1590 if (stream->session->id == session_id &&
1591 !stream->data_pending_check_done) {
1592 is_data_inflight = 1;
1593 DBG("Data is still in flight for stream %" PRIu64,
1594 stream->stream_handle);
1595 break;
1596 }
1597 }
1598 rcu_read_unlock();
1599
1600 /* All good, send back reply. */
1601 reply.ret_code = htobe32(is_data_inflight);
1602
1603 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1604 if (ret < 0) {
1605 ERR("Relay end data pending send reply failed");
1606 }
1607
1608end_no_session:
1609 return ret;
1610}
1611
b8aa1682
JD
1612/*
1613 * relay_process_control: Process the commands received on the control socket
1614 */
1615static
1616int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
1617 struct relay_command *cmd, struct lttng_ht *streams_ht)
1618{
1619 int ret = 0;
1620
1621 switch (be32toh(recv_hdr->cmd)) {
b8aa1682
JD
1622 case RELAYD_CREATE_SESSION:
1623 ret = relay_create_session(recv_hdr, cmd);
1624 break;
b8aa1682
JD
1625 case RELAYD_ADD_STREAM:
1626 ret = relay_add_stream(recv_hdr, cmd, streams_ht);
1627 break;
1628 case RELAYD_START_DATA:
1629 ret = relay_start(recv_hdr, cmd);
1630 break;
1631 case RELAYD_SEND_METADATA:
1632 ret = relay_recv_metadata(recv_hdr, cmd, streams_ht);
1633 break;
1634 case RELAYD_VERSION:
1635 ret = relay_send_version(recv_hdr, cmd);
1636 break;
173af62f
DG
1637 case RELAYD_CLOSE_STREAM:
1638 ret = relay_close_stream(recv_hdr, cmd, streams_ht);
1639 break;
6d805429
DG
1640 case RELAYD_DATA_PENDING:
1641 ret = relay_data_pending(recv_hdr, cmd, streams_ht);
c8f59ee5
DG
1642 break;
1643 case RELAYD_QUIESCENT_CONTROL:
1644 ret = relay_quiescent_control(recv_hdr, cmd);
1645 break;
f7079f67
DG
1646 case RELAYD_BEGIN_DATA_PENDING:
1647 ret = relay_begin_data_pending(recv_hdr, cmd, streams_ht);
1648 break;
1649 case RELAYD_END_DATA_PENDING:
1650 ret = relay_end_data_pending(recv_hdr, cmd, streams_ht);
1651 break;
b8aa1682
JD
1652 case RELAYD_UPDATE_SYNC_INFO:
1653 default:
1654 ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
1655 relay_unknown_command(cmd);
1656 ret = -1;
1657 goto end;
1658 }
1659
1660end:
1661 return ret;
1662}
1663
1664/*
1665 * relay_process_data: Process the data received on the data socket
1666 */
1667static
1668int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
1669{
1670 int ret = 0;
1671 struct relay_stream *stream;
1672 struct lttcomm_relayd_data_hdr data_hdr;
1673 uint64_t stream_id;
173af62f 1674 uint64_t net_seq_num;
b8aa1682
JD
1675 uint32_t data_size;
1676
1677 ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr,
7c5aef62 1678 sizeof(struct lttcomm_relayd_data_hdr), 0);
b8aa1682
JD
1679 if (ret <= 0) {
1680 ERR("Connections seems to be closed");
1681 ret = -1;
1682 goto end;
1683 }
1684
1685 stream_id = be64toh(data_hdr.stream_id);
9d1bbf21
MD
1686
1687 rcu_read_lock();
b8aa1682
JD
1688 stream = relay_stream_from_stream_id(stream_id, streams_ht);
1689 if (!stream) {
1690 ret = -1;
9d1bbf21 1691 goto end_unlock;
b8aa1682
JD
1692 }
1693
1694 data_size = be32toh(data_hdr.data_size);
1695 if (data_buffer_size < data_size) {
d7b3776f 1696 char *tmp_data_ptr = data_buffer;
b8aa1682
JD
1697 data_buffer = realloc(data_buffer, data_size);
1698 if (!data_buffer) {
1699 ERR("Allocating data buffer");
d7b3776f 1700 free(tmp_data_ptr);
b8aa1682 1701 ret = -1;
9d1bbf21 1702 goto end_unlock;
b8aa1682
JD
1703 }
1704 data_buffer_size = data_size;
1705 }
1706 memset(data_buffer, 0, data_size);
1707
173af62f
DG
1708 net_seq_num = be64toh(data_hdr.net_seq_num);
1709
77c7c900 1710 DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64,
173af62f 1711 data_size, stream_id, net_seq_num);
7c5aef62 1712 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682
JD
1713 if (ret <= 0) {
1714 ret = -1;
9d1bbf21 1715 goto end_unlock;
b8aa1682
JD
1716 }
1717
6f94560a
MD
1718 do {
1719 ret = write(stream->fd, data_buffer, data_size);
1720 } while (ret < 0 && errno == EINTR);
b8aa1682
JD
1721 if (ret < data_size) {
1722 ERR("Relay error writing data to file");
1723 ret = -1;
9d1bbf21 1724 goto end_unlock;
b8aa1682 1725 }
1d4dfdef 1726
5ab7344e
JD
1727 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64,
1728 ret, stream->stream_handle);
1729
1d4dfdef
DG
1730 ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size));
1731 if (ret < 0) {
1732 goto end_unlock;
1733 }
1734
173af62f
DG
1735 stream->prev_seq = net_seq_num;
1736
1737 /* Check if we need to close the FD */
1738 if (close_stream_check(stream)) {
f66c074c 1739 int cret;
173af62f
DG
1740 struct lttng_ht_iter iter;
1741
f66c074c
DG
1742 cret = close(stream->fd);
1743 if (cret < 0) {
1744 PERROR("close stream process data");
1745 }
173af62f
DG
1746 iter.iter.node = &stream->stream_n.node;
1747 ret = lttng_ht_del(streams_ht, &iter);
1748 assert(!ret);
1749 call_rcu(&stream->rcu_node,
1750 deferred_free_stream);
1751 DBG("Closed tracefile %d after recv data", stream->fd);
1752 }
1753
9d1bbf21
MD
1754end_unlock:
1755 rcu_read_unlock();
b8aa1682
JD
1756end:
1757 return ret;
1758}
1759
1760static
9d1bbf21 1761void relay_cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
b8aa1682
JD
1762{
1763 int ret;
1764
b8aa1682
JD
1765 lttng_poll_del(events, pollfd);
1766
1767 ret = close(pollfd);
1768 if (ret < 0) {
1769 ERR("Closing pollfd %d", pollfd);
1770 }
1771}
1772
1773static
1774int relay_add_connection(int fd, struct lttng_poll_event *events,
1775 struct lttng_ht *relay_connections_ht)
1776{
b8aa1682 1777 struct relay_command *relay_connection;
9d1bbf21 1778 int ret;
b8aa1682
JD
1779
1780 relay_connection = zmalloc(sizeof(struct relay_command));
1781 if (relay_connection == NULL) {
1782 PERROR("Relay command zmalloc");
9d1bbf21 1783 goto error;
b8aa1682
JD
1784 }
1785 ret = read(fd, relay_connection, sizeof(struct relay_command));
cdf0f8fc 1786 if (ret < 0 || ret < sizeof(struct relay_command)) {
b8aa1682 1787 PERROR("read relay cmd pipe");
9d1bbf21 1788 goto error_read;
b8aa1682
JD
1789 }
1790
1791 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1792 (unsigned long) relay_connection->sock->fd);
9d1bbf21 1793 rcu_read_lock();
b8aa1682
JD
1794 lttng_ht_add_unique_ulong(relay_connections_ht,
1795 &relay_connection->sock_n);
9d1bbf21
MD
1796 rcu_read_unlock();
1797 return lttng_poll_add(events,
b8aa1682
JD
1798 relay_connection->sock->fd,
1799 LPOLLIN | LPOLLRDHUP);
1800
9d1bbf21
MD
1801error_read:
1802 free(relay_connection);
1803error:
1804 return -1;
1805}
1806
1807static
1808void deferred_free_connection(struct rcu_head *head)
1809{
1810 struct relay_command *relay_connection =
1811 caa_container_of(head, struct relay_command, rcu_node);
5b6d8097
DG
1812
1813 lttcomm_destroy_sock(relay_connection->sock);
9d1bbf21
MD
1814 free(relay_connection);
1815}
1816
1817static
1818void relay_del_connection(struct lttng_ht *relay_connections_ht,
1819 struct lttng_ht *streams_ht, struct lttng_ht_iter *iter,
1820 struct relay_command *relay_connection)
1821{
1822 int ret;
1823
1824 ret = lttng_ht_del(relay_connections_ht, iter);
1825 assert(!ret);
1826 if (relay_connection->type == RELAY_CONTROL) {
1827 relay_delete_session(relay_connection, streams_ht);
1828 }
5b6d8097 1829
9d1bbf21
MD
1830 call_rcu(&relay_connection->rcu_node,
1831 deferred_free_connection);
b8aa1682
JD
1832}
1833
1834/*
1835 * This thread does the actual work
1836 */
1837static
1838void *relay_thread_worker(void *data)
1839{
095a4ae5 1840 int i, ret, pollfd, err = -1;
b8aa1682
JD
1841 uint32_t revents, nb_fd;
1842 struct relay_command *relay_connection;
1843 struct lttng_poll_event events;
1844 struct lttng_ht *relay_connections_ht;
1845 struct lttng_ht_node_ulong *node;
1846 struct lttng_ht_iter iter;
1847 struct lttng_ht *streams_ht;
1848 struct lttcomm_relayd_hdr recv_hdr;
1849
1850 DBG("[thread] Relay worker started");
1851
9d1bbf21
MD
1852 rcu_register_thread();
1853
b8aa1682
JD
1854 /* table of connections indexed on socket */
1855 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
1856 if (!relay_connections_ht) {
1857 goto relay_connections_ht_error;
1858 }
b8aa1682
JD
1859
1860 /* tables of streams indexed by stream ID */
1861 streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
1862 if (!streams_ht) {
1863 goto streams_ht_error;
1864 }
b8aa1682
JD
1865
1866 ret = create_thread_poll_set(&events, 2);
1867 if (ret < 0) {
1868 goto error_poll_create;
1869 }
1870
1871 ret = lttng_poll_add(&events, relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1872 if (ret < 0) {
1873 goto error;
1874 }
1875
1876 while (1) {
b8aa1682
JD
1877 /* Infinite blocking call, waiting for transmission */
1878 restart:
87c1611d 1879 DBG3("Relayd worker thread polling...");
b8aa1682
JD
1880 ret = lttng_poll_wait(&events, -1);
1881 if (ret < 0) {
1882 /*
1883 * Restart interrupted system call.
1884 */
1885 if (errno == EINTR) {
1886 goto restart;
1887 }
1888 goto error;
1889 }
1890
0d9c5d77
DG
1891 nb_fd = ret;
1892
b8aa1682
JD
1893 for (i = 0; i < nb_fd; i++) {
1894 /* Fetch once the poll data */
1895 revents = LTTNG_POLL_GETEV(&events, i);
1896 pollfd = LTTNG_POLL_GETFD(&events, i);
1897
1898 /* Thread quit pipe has been closed. Killing thread. */
1899 ret = check_thread_quit_pipe(pollfd, revents);
1900 if (ret) {
095a4ae5
MD
1901 err = 0;
1902 goto exit;
b8aa1682
JD
1903 }
1904
1905 /* Inspect the relay cmd pipe for new connection */
1906 if (pollfd == relay_cmd_pipe[0]) {
1907 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1908 ERR("Relay pipe error");
1909 goto error;
1910 } else if (revents & LPOLLIN) {
1911 DBG("Relay command received");
1912 ret = relay_add_connection(relay_cmd_pipe[0],
1913 &events, relay_connections_ht);
1914 if (ret < 0) {
1915 goto error;
1916 }
1917 }
1918 } else if (revents > 0) {
9d1bbf21 1919 rcu_read_lock();
b8aa1682
JD
1920 lttng_ht_lookup(relay_connections_ht,
1921 (void *)((unsigned long) pollfd),
1922 &iter);
1923 node = lttng_ht_iter_get_node_ulong(&iter);
1924 if (node == NULL) {
1925 DBG2("Relay sock %d not found", pollfd);
9d1bbf21 1926 rcu_read_unlock();
b8aa1682
JD
1927 goto error;
1928 }
1929 relay_connection = caa_container_of(node,
1930 struct relay_command, sock_n);
1931
1932 if (revents & (LPOLLERR)) {
1933 ERR("POLL ERROR");
9d1bbf21
MD
1934 relay_cleanup_poll_connection(&events, pollfd);
1935 relay_del_connection(relay_connections_ht,
1936 streams_ht, &iter,
1937 relay_connection);
b8aa1682
JD
1938 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1939 DBG("Socket %d hung up", pollfd);
9d1bbf21
MD
1940 relay_cleanup_poll_connection(&events, pollfd);
1941 relay_del_connection(relay_connections_ht,
1942 streams_ht, &iter,
1943 relay_connection);
b8aa1682
JD
1944 } else if (revents & LPOLLIN) {
1945 /* control socket */
1946 if (relay_connection->type == RELAY_CONTROL) {
1947 ret = relay_connection->sock->ops->recvmsg(
1948 relay_connection->sock, &recv_hdr,
7c5aef62 1949 sizeof(struct lttcomm_relayd_hdr), 0);
b8aa1682
JD
1950 /* connection closed */
1951 if (ret <= 0) {
9d1bbf21
MD
1952 relay_cleanup_poll_connection(&events, pollfd);
1953 relay_del_connection(relay_connections_ht,
1954 streams_ht, &iter,
1955 relay_connection);
b8aa1682
JD
1956 DBG("Control connection closed with %d", pollfd);
1957 } else {
1958 if (relay_connection->session) {
77c7c900 1959 DBG2("Relay worker receiving data for session : %" PRIu64,
b8aa1682
JD
1960 relay_connection->session->id);
1961 }
1962 ret = relay_process_control(&recv_hdr,
1963 relay_connection,
1964 streams_ht);
1965 /*
1966 * there was an error in processing a control
1967 * command: clear the session
1968 * */
1969 if (ret < 0) {
9d1bbf21
MD
1970 relay_cleanup_poll_connection(&events, pollfd);
1971 relay_del_connection(relay_connections_ht,
1972 streams_ht, &iter,
1973 relay_connection);
b8aa1682
JD
1974 DBG("Connection closed with %d", pollfd);
1975 }
1976 }
1977 /* data socket */
1978 } else if (relay_connection->type == RELAY_DATA) {
1979 ret = relay_process_data(relay_connection, streams_ht);
1980 /* connection closed */
1981 if (ret < 0) {
9d1bbf21
MD
1982 relay_cleanup_poll_connection(&events, pollfd);
1983 relay_del_connection(relay_connections_ht,
1984 streams_ht, &iter,
1985 relay_connection);
b8aa1682
JD
1986 DBG("Data connection closed with %d", pollfd);
1987 }
1988 }
1989 }
9d1bbf21 1990 rcu_read_unlock();
b8aa1682
JD
1991 }
1992 }
1993 }
1994
095a4ae5 1995exit:
b8aa1682
JD
1996error:
1997 lttng_poll_clean(&events);
1998
1999 /* empty the hash table and free the memory */
9d1bbf21 2000 rcu_read_lock();
b8aa1682
JD
2001 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
2002 node = lttng_ht_iter_get_node_ulong(&iter);
2003 if (node) {
2004 relay_connection = caa_container_of(node,
2005 struct relay_command, sock_n);
9d1bbf21
MD
2006 relay_del_connection(relay_connections_ht,
2007 streams_ht, &iter,
2008 relay_connection);
b8aa1682 2009 }
b8aa1682 2010 }
9d1bbf21 2011 rcu_read_unlock();
b8aa1682 2012error_poll_create:
095a4ae5
MD
2013 lttng_ht_destroy(streams_ht);
2014streams_ht_error:
b8aa1682 2015 lttng_ht_destroy(relay_connections_ht);
095a4ae5 2016relay_connections_ht_error:
6620da75
DG
2017 /* Close relay cmd pipes */
2018 utils_close_pipe(relay_cmd_pipe);
095a4ae5
MD
2019 if (err) {
2020 DBG("Thread exited with error");
2021 }
b8aa1682 2022 DBG("Worker thread cleanup complete");
095a4ae5 2023 free(data_buffer);
b8aa1682 2024 stop_threads();
9d1bbf21 2025 rcu_unregister_thread();
b8aa1682
JD
2026 return NULL;
2027}
2028
2029/*
2030 * Create the relay command pipe to wake thread_manage_apps.
2031 * Closed in cleanup().
2032 */
2033static int create_relay_cmd_pipe(void)
2034{
a02de639 2035 int ret;
b8aa1682 2036
a02de639 2037 ret = utils_create_pipe_cloexec(relay_cmd_pipe);
b8aa1682 2038
b8aa1682
JD
2039 return ret;
2040}
2041
2042/*
2043 * main
2044 */
2045int main(int argc, char **argv)
2046{
2047 int ret = 0;
2048 void *status;
2049
2050 /* Create thread quit pipe */
2051 if ((ret = init_thread_quit_pipe()) < 0) {
2052 goto error;
2053 }
2054
2055 /* Parse arguments */
2056 progname = argv[0];
2057 if ((ret = parse_args(argc, argv) < 0)) {
a02de639 2058 goto exit;
b8aa1682
JD
2059 }
2060
2061 if ((ret = set_signal_handler()) < 0) {
2062 goto exit;
2063 }
2064
2065 /* Daemonize */
2066 if (opt_daemon) {
2067 ret = daemon(0, 0);
2068 if (ret < 0) {
2069 PERROR("daemon");
a02de639 2070 goto exit;
b8aa1682
JD
2071 }
2072 }
2073
2074 /* Check if daemon is UID = 0 */
2075 is_root = !getuid();
2076
2077 if (!is_root) {
2078 if (control_uri->port < 1024 || data_uri->port < 1024) {
2079 ERR("Need to be root to use ports < 1024");
2080 ret = -1;
a02de639 2081 goto exit;
b8aa1682
JD
2082 }
2083 }
2084
2085 /* Setup the thread apps communication pipe. */
2086 if ((ret = create_relay_cmd_pipe()) < 0) {
2087 goto exit;
2088 }
2089
2090 /* Init relay command queue. */
2091 cds_wfq_init(&relay_cmd_queue.queue);
2092
2093 /* Set up max poll set size */
2094 lttng_poll_set_max_size();
2095
2096 /* Setup the dispatcher thread */
2097 ret = pthread_create(&dispatcher_thread, NULL,
2098 relay_thread_dispatcher, (void *) NULL);
2099 if (ret != 0) {
2100 PERROR("pthread_create dispatcher");
2101 goto exit_dispatcher;
2102 }
2103
2104 /* Setup the worker thread */
2105 ret = pthread_create(&worker_thread, NULL,
2106 relay_thread_worker, (void *) NULL);
2107 if (ret != 0) {
2108 PERROR("pthread_create worker");
2109 goto exit_worker;
2110 }
2111
2112 /* Setup the listener thread */
2113 ret = pthread_create(&listener_thread, NULL,
2114 relay_thread_listener, (void *) NULL);
2115 if (ret != 0) {
2116 PERROR("pthread_create listener");
2117 goto exit_listener;
2118 }
2119
2120exit_listener:
2121 ret = pthread_join(listener_thread, &status);
2122 if (ret != 0) {
2123 PERROR("pthread_join");
2124 goto error; /* join error, exit without cleanup */
2125 }
2126
2127exit_worker:
2128 ret = pthread_join(worker_thread, &status);
2129 if (ret != 0) {
2130 PERROR("pthread_join");
2131 goto error; /* join error, exit without cleanup */
2132 }
2133
2134exit_dispatcher:
2135 ret = pthread_join(dispatcher_thread, &status);
2136 if (ret != 0) {
2137 PERROR("pthread_join");
2138 goto error; /* join error, exit without cleanup */
2139 }
2140
2141exit:
2142 cleanup();
2143 if (!ret) {
2144 exit(EXIT_SUCCESS);
2145 }
a02de639 2146
b8aa1682
JD
2147error:
2148 exit(EXIT_FAILURE);
2149}
This page took 0.121316 seconds and 4 git commands to generate.