Adjust the relayd protocol on version check
[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);
4cec016f 257 if (ret < 0 || ret != 1) {
b8aa1682
JD
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 671 free(relay_cmd);
4cec016f 672 if (ret < 0 || ret != sizeof(struct relay_command)) {
b8aa1682
JD
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
b8aa1682
JD
688/*
689 * config_get_default_path
690 *
691 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
692 */
693static
694char *config_get_default_path(void)
695{
696 return getenv("HOME");
697}
698
b8aa1682 699static
095a4ae5 700char *create_output_path_auto(char *path_name)
b8aa1682 701{
095a4ae5 702 int ret;
b8aa1682 703 char *traces_path = NULL;
095a4ae5
MD
704 char *alloc_path = NULL;
705 char *default_path;
b8aa1682 706
095a4ae5
MD
707 default_path = config_get_default_path();
708 if (default_path == NULL) {
709 ERR("Home path not found.\n \
710 Please specify an output path using -o, --output PATH");
711 goto exit;
712 }
713 alloc_path = strdup(default_path);
714 if (alloc_path == NULL) {
715 PERROR("Path allocation");
716 goto exit;
717 }
718 ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME
719 "/%s", alloc_path, path_name);
720 if (ret < 0) {
721 PERROR("asprintf trace dir name");
722 goto exit;
b8aa1682 723 }
095a4ae5 724exit:
b8aa1682 725 free(alloc_path);
095a4ae5
MD
726 return traces_path;
727}
728
729static
730char *create_output_path_noauto(char *path_name)
731{
732 int ret;
733 char *traces_path = NULL;
734 char *full_path;
b8aa1682 735
834f3ec7 736 full_path = utils_expand_path(opt_output_path);
d620da68
DG
737 if (!full_path) {
738 goto exit;
739 }
740
095a4ae5
MD
741 ret = asprintf(&traces_path, "%s/%s", full_path, path_name);
742 if (ret < 0) {
743 PERROR("asprintf trace dir name");
744 goto exit;
745 }
b8aa1682 746exit:
095a4ae5 747 free(full_path);
b8aa1682
JD
748 return traces_path;
749}
750
095a4ae5
MD
751/*
752 * create_output_path: create the output trace directory
753 */
754static
755char *create_output_path(char *path_name)
756{
757 if (opt_output_path == NULL) {
758 return create_output_path_auto(path_name);
759 } else {
760 return create_output_path_noauto(path_name);
761 }
762}
763
de91f48a
DG
764/*
765 * Get stream from stream id.
766 * Need to be called with RCU read-side lock held.
767 */
768static
769struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id,
770 struct lttng_ht *streams_ht)
771{
772 struct lttng_ht_node_ulong *node;
773 struct lttng_ht_iter iter;
774 struct relay_stream *ret;
775
776 lttng_ht_lookup(streams_ht,
777 (void *)((unsigned long) stream_id),
778 &iter);
779 node = lttng_ht_iter_get_node_ulong(&iter);
780 if (node == NULL) {
781 DBG("Relay stream %" PRIu64 " not found", stream_id);
782 ret = NULL;
783 goto end;
784 }
785
786 ret = caa_container_of(node, struct relay_stream, stream_n);
787
788end:
789 return ret;
790}
791
9d1bbf21
MD
792static
793void deferred_free_stream(struct rcu_head *head)
794{
795 struct relay_stream *stream =
796 caa_container_of(head, struct relay_stream, rcu_node);
797 free(stream);
798}
799
b8aa1682
JD
800/*
801 * relay_delete_session: Free all memory associated with a session and
802 * close all the FDs
803 */
804static
805void relay_delete_session(struct relay_command *cmd, struct lttng_ht *streams_ht)
806{
807 struct lttng_ht_iter iter;
808 struct lttng_ht_node_ulong *node;
809 struct relay_stream *stream;
810 int ret;
811
095a4ae5 812 if (!cmd->session) {
b8aa1682 813 return;
095a4ae5 814 }
b8aa1682 815
77c7c900 816 DBG("Relay deleting session %" PRIu64, cmd->session->id);
5b6d8097 817
9d1bbf21 818 rcu_read_lock();
b8aa1682
JD
819 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, node, node) {
820 node = lttng_ht_iter_get_node_ulong(&iter);
821 if (node) {
822 stream = caa_container_of(node,
823 struct relay_stream, stream_n);
824 if (stream->session == cmd->session) {
f66c074c
DG
825 ret = close(stream->fd);
826 if (ret < 0) {
827 PERROR("close stream fd on delete session");
828 }
b8aa1682
JD
829 ret = lttng_ht_del(streams_ht, &iter);
830 assert(!ret);
9d1bbf21
MD
831 call_rcu(&stream->rcu_node,
832 deferred_free_stream);
b8aa1682
JD
833 }
834 }
835 }
9d1bbf21 836 rcu_read_unlock();
5b6d8097
DG
837
838 free(cmd->session);
b8aa1682
JD
839}
840
c5b6f4f0
DG
841/*
842 * Handle the RELAYD_CREATE_SESSION command.
843 *
844 * On success, send back the session id or else return a negative value.
845 */
846static
847int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr,
848 struct relay_command *cmd)
849{
850 int ret = 0, send_ret;
851 struct relay_session *session;
852 struct lttcomm_relayd_status_session reply;
853
854 assert(recv_hdr);
855 assert(cmd);
856
857 memset(&reply, 0, sizeof(reply));
858
859 session = zmalloc(sizeof(struct relay_session));
860 if (session == NULL) {
861 PERROR("relay session zmalloc");
862 ret = -1;
863 goto error;
864 }
865
866 session->id = ++last_relay_session_id;
867 session->sock = cmd->sock;
868 cmd->session = session;
869
870 reply.session_id = htobe64(session->id);
871
872 DBG("Created session %" PRIu64, session->id);
873
874error:
875 if (ret < 0) {
876 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
877 } else {
878 reply.ret_code = htobe32(LTTNG_OK);
879 }
880
881 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
882 if (send_ret < 0) {
883 ERR("Relayd sending session id");
4169f5ad 884 ret = send_ret;
c5b6f4f0
DG
885 }
886
887 return ret;
888}
889
b8aa1682
JD
890/*
891 * relay_add_stream: allocate a new stream for a session
892 */
893static
894int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
895 struct relay_command *cmd, struct lttng_ht *streams_ht)
896{
897 struct relay_session *session = cmd->session;
898 struct lttcomm_relayd_add_stream stream_info;
899 struct relay_stream *stream = NULL;
900 struct lttcomm_relayd_status_stream reply;
901 char *path = NULL, *root_path = NULL;
902 int ret, send_ret;
903
c5b6f4f0 904 if (!session || cmd->version_check_done == 0) {
b8aa1682
JD
905 ERR("Trying to add a stream before version check");
906 ret = -1;
907 goto end_no_session;
908 }
909
b8aa1682 910 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
7c5aef62 911 sizeof(struct lttcomm_relayd_add_stream), 0);
b8aa1682 912 if (ret < sizeof(struct lttcomm_relayd_add_stream)) {
a6cd2b97
DG
913 if (ret == 0) {
914 /* Orderly shutdown. Not necessary to print an error. */
915 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
916 } else {
917 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
918 }
b8aa1682
JD
919 ret = -1;
920 goto end_no_session;
921 }
922 stream = zmalloc(sizeof(struct relay_stream));
923 if (stream == NULL) {
924 PERROR("relay stream zmalloc");
925 ret = -1;
926 goto end_no_session;
927 }
928
9d1bbf21 929 rcu_read_lock();
b8aa1682 930 stream->stream_handle = ++last_relay_stream_id;
173af62f 931 stream->prev_seq = -1ULL;
b8aa1682
JD
932 stream->session = session;
933
934 root_path = create_output_path(stream_info.pathname);
935 if (!root_path) {
936 ret = -1;
937 goto end;
938 }
834f3ec7 939 ret = utils_mkdir_recursive(root_path, S_IRWXU | S_IRWXG);
b8aa1682 940 if (ret < 0) {
b8aa1682
JD
941 ERR("relay creating output directory");
942 goto end;
943 }
944
945 ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name);
946 if (ret < 0) {
947 PERROR("asprintf stream path");
5af40280 948 path = NULL;
b8aa1682
JD
949 goto end;
950 }
951
952 ret = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
953 if (ret < 0) {
954 PERROR("Relay creating trace file");
955 goto end;
956 }
957
958 stream->fd = ret;
959 DBG("Tracefile %s created", path);
960
961 lttng_ht_node_init_ulong(&stream->stream_n,
962 (unsigned long) stream->stream_handle);
963 lttng_ht_add_unique_ulong(streams_ht,
964 &stream->stream_n);
965
966 DBG("Relay new stream added %s", stream_info.channel_name);
967
968end:
969 free(path);
970 free(root_path);
5af40280
CB
971
972 reply.handle = htobe64(stream->stream_handle);
b8aa1682
JD
973 /* send the session id to the client or a negative return code on error */
974 if (ret < 0) {
f73fabfd 975 reply.ret_code = htobe32(LTTNG_ERR_UNK);
5af40280
CB
976 /* stream was not properly added to the ht, so free it */
977 free(stream);
b8aa1682 978 } else {
f73fabfd 979 reply.ret_code = htobe32(LTTNG_OK);
b8aa1682 980 }
5af40280 981
b8aa1682
JD
982 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
983 sizeof(struct lttcomm_relayd_status_stream), 0);
984 if (send_ret < 0) {
985 ERR("Relay sending stream id");
4169f5ad 986 ret = send_ret;
b8aa1682 987 }
9d1bbf21 988 rcu_read_unlock();
b8aa1682
JD
989
990end_no_session:
991 return ret;
992}
993
173af62f
DG
994/*
995 * relay_close_stream: close a specific stream
996 */
997static
998int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
999 struct relay_command *cmd, struct lttng_ht *streams_ht)
1000{
1001 struct relay_session *session = cmd->session;
1002 struct lttcomm_relayd_close_stream stream_info;
1003 struct lttcomm_relayd_generic_reply reply;
1004 struct relay_stream *stream;
1005 int ret, send_ret;
173af62f
DG
1006 struct lttng_ht_iter iter;
1007
1008 DBG("Close stream received");
1009
c5b6f4f0 1010 if (!session || cmd->version_check_done == 0) {
173af62f
DG
1011 ERR("Trying to close a stream before version check");
1012 ret = -1;
1013 goto end_no_session;
1014 }
1015
1016 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
7c5aef62 1017 sizeof(struct lttcomm_relayd_close_stream), 0);
173af62f 1018 if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
a6cd2b97
DG
1019 if (ret == 0) {
1020 /* Orderly shutdown. Not necessary to print an error. */
1021 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1022 } else {
1023 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
1024 }
173af62f
DG
1025 ret = -1;
1026 goto end_no_session;
1027 }
1028
1029 rcu_read_lock();
de91f48a
DG
1030 stream = relay_stream_from_stream_id(be64toh(stream_info.stream_id),
1031 streams_ht);
173af62f
DG
1032 if (!stream) {
1033 ret = -1;
1034 goto end_unlock;
1035 }
1036
8e2583a4 1037 stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num);
173af62f
DG
1038 stream->close_flag = 1;
1039
1040 if (close_stream_check(stream)) {
1041 int delret;
1042
f66c074c
DG
1043 delret = close(stream->fd);
1044 if (delret < 0) {
1045 PERROR("close stream");
1046 }
de91f48a 1047 iter.iter.node = &stream->stream_n.node;
173af62f
DG
1048 delret = lttng_ht_del(streams_ht, &iter);
1049 assert(!delret);
1050 call_rcu(&stream->rcu_node,
1051 deferred_free_stream);
1052 DBG("Closed tracefile %d from close stream", stream->fd);
1053 }
1054
1055end_unlock:
1056 rcu_read_unlock();
1057
1058 if (ret < 0) {
f73fabfd 1059 reply.ret_code = htobe32(LTTNG_ERR_UNK);
173af62f 1060 } else {
f73fabfd 1061 reply.ret_code = htobe32(LTTNG_OK);
173af62f
DG
1062 }
1063 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1064 sizeof(struct lttcomm_relayd_generic_reply), 0);
1065 if (send_ret < 0) {
1066 ERR("Relay sending stream id");
4169f5ad 1067 ret = send_ret;
173af62f
DG
1068 }
1069
1070end_no_session:
1071 return ret;
1072}
1073
b8aa1682
JD
1074/*
1075 * relay_unknown_command: send -1 if received unknown command
1076 */
1077static
1078void relay_unknown_command(struct relay_command *cmd)
1079{
1080 struct lttcomm_relayd_generic_reply reply;
1081 int ret;
1082
f73fabfd 1083 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1084 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1085 sizeof(struct lttcomm_relayd_generic_reply), 0);
1086 if (ret < 0) {
1087 ERR("Relay sending unknown command");
1088 }
1089}
1090
1091/*
1092 * relay_start: send an acknowledgment to the client to tell if we are
1093 * ready to receive data. We are ready if a session is established.
1094 */
1095static
1096int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
1097 struct relay_command *cmd)
1098{
f73fabfd 1099 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1100 struct lttcomm_relayd_generic_reply reply;
1101 struct relay_session *session = cmd->session;
1102
1103 if (!session) {
1104 DBG("Trying to start the streaming without a session established");
f73fabfd 1105 ret = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1106 }
1107
1108 reply.ret_code = ret;
1109 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1110 sizeof(struct lttcomm_relayd_generic_reply), 0);
1111 if (ret < 0) {
1112 ERR("Relay sending start ack");
1113 }
1114
1115 return ret;
1116}
1117
1d4dfdef
DG
1118/*
1119 * Append padding to the file pointed by the file descriptor fd.
1120 */
1121static int write_padding_to_file(int fd, uint32_t size)
1122{
1123 int ret = 0;
1124 char *zeros;
1125
1126 if (size == 0) {
1127 goto end;
1128 }
1129
1130 zeros = zmalloc(size);
1131 if (zeros == NULL) {
1132 PERROR("zmalloc zeros for padding");
1133 ret = -1;
1134 goto end;
1135 }
1136
1137 do {
1138 ret = write(fd, zeros, size);
1139 } while (ret < 0 && errno == EINTR);
4cec016f 1140 if (ret < 0 || ret != size) {
1d4dfdef
DG
1141 PERROR("write padding to file");
1142 }
1143
e986c7a1
DG
1144 free(zeros);
1145
1d4dfdef
DG
1146end:
1147 return ret;
1148}
1149
b8aa1682
JD
1150/*
1151 * relay_recv_metadata: receive the metada for the session.
1152 */
1153static
1154int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
1155 struct relay_command *cmd, struct lttng_ht *streams_ht)
1156{
f73fabfd 1157 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1158 struct relay_session *session = cmd->session;
1159 struct lttcomm_relayd_metadata_payload *metadata_struct;
1160 struct relay_stream *metadata_stream;
1161 uint64_t data_size, payload_size;
1162
1163 if (!session) {
1164 ERR("Metadata sent before version check");
1165 ret = -1;
1166 goto end;
1167 }
1168
f6416125
MD
1169 data_size = payload_size = be64toh(recv_hdr->data_size);
1170 if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1171 ERR("Incorrect data size");
1172 ret = -1;
1173 goto end;
1174 }
1175 payload_size -= sizeof(struct lttcomm_relayd_metadata_payload);
1176
b8aa1682 1177 if (data_buffer_size < data_size) {
d7b3776f 1178 /* In case the realloc fails, we can free the memory */
c617c0c6
MD
1179 char *tmp_data_ptr;
1180
1181 tmp_data_ptr = realloc(data_buffer, data_size);
1182 if (!tmp_data_ptr) {
b8aa1682 1183 ERR("Allocating data buffer");
c617c0c6 1184 free(data_buffer);
b8aa1682
JD
1185 ret = -1;
1186 goto end;
1187 }
c617c0c6 1188 data_buffer = tmp_data_ptr;
b8aa1682
JD
1189 data_buffer_size = data_size;
1190 }
1191 memset(data_buffer, 0, data_size);
77c7c900 1192 DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
7c5aef62 1193 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682 1194 if (ret < 0 || ret != data_size) {
a6cd2b97
DG
1195 if (ret == 0) {
1196 /* Orderly shutdown. Not necessary to print an error. */
1197 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1198 } else {
1199 ERR("Relay didn't receive the whole metadata");
1200 }
b8aa1682 1201 ret = -1;
b8aa1682
JD
1202 goto end;
1203 }
1204 metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
9d1bbf21
MD
1205
1206 rcu_read_lock();
b8aa1682
JD
1207 metadata_stream = relay_stream_from_stream_id(
1208 be64toh(metadata_struct->stream_id), streams_ht);
1209 if (!metadata_stream) {
1210 ret = -1;
9d1bbf21 1211 goto end_unlock;
b8aa1682
JD
1212 }
1213
6f94560a
MD
1214 do {
1215 ret = write(metadata_stream->fd, metadata_struct->payload,
1216 payload_size);
1217 } while (ret < 0 && errno == EINTR);
4cec016f 1218 if (ret < 0 || ret != payload_size) {
b8aa1682
JD
1219 ERR("Relay error writing metadata on file");
1220 ret = -1;
9d1bbf21 1221 goto end_unlock;
b8aa1682 1222 }
1d4dfdef
DG
1223
1224 ret = write_padding_to_file(metadata_stream->fd,
1225 be32toh(metadata_struct->padding_size));
1226 if (ret < 0) {
1227 goto end_unlock;
1228 }
1229
b8aa1682
JD
1230 DBG2("Relay metadata written");
1231
9d1bbf21 1232end_unlock:
6e3c5836 1233 rcu_read_unlock();
b8aa1682
JD
1234end:
1235 return ret;
1236}
1237
1238/*
1239 * relay_send_version: send relayd version number
1240 */
1241static
1242int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
d4519fa3 1243 struct relay_command *cmd, struct lttng_ht *streams_ht)
b8aa1682 1244{
7f51dcba 1245 int ret;
092b6259 1246 struct lttcomm_relayd_version reply, msg;
b8aa1682 1247
c5b6f4f0
DG
1248 assert(cmd);
1249
1250 cmd->version_check_done = 1;
b8aa1682 1251
092b6259 1252 /* Get version from the other side. */
7c5aef62 1253 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
092b6259 1254 if (ret < 0 || ret != sizeof(msg)) {
a6cd2b97
DG
1255 if (ret == 0) {
1256 /* Orderly shutdown. Not necessary to print an error. */
1257 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1258 } else {
1259 ERR("Relay failed to receive the version values.");
1260 }
092b6259 1261 ret = -1;
092b6259
DG
1262 goto end;
1263 }
1264
c617c0c6 1265 ret = sscanf(VERSION, "%10u.%10u", &reply.major, &reply.minor);
7f51dcba
MD
1266 if (ret < 2) {
1267 ERR("Error in scanning version");
1268 ret = -1;
1269 goto end;
1270 }
b8aa1682
JD
1271 reply.major = htobe32(reply.major);
1272 reply.minor = htobe32(reply.minor);
1273 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1274 sizeof(struct lttcomm_relayd_version), 0);
1275 if (ret < 0) {
1276 ERR("Relay sending version");
1277 }
d4519fa3
JD
1278
1279 /* Major versions must be the same */
1280 if (reply.major != be32toh(msg.major)) {
1281 DBG("Incompatible major versions, deleting session");
1282 relay_delete_session(cmd, streams_ht);
1283 ret = 0;
1284 goto end;
1285 }
1286
1287 cmd->session->major = reply.major;
1288 /* We adapt to the lowest compatible version */
1289 if (reply.minor <= be32toh(msg.minor)) {
1290 cmd->session->minor = reply.minor;
1291 } else {
1292 cmd->session->minor = be32toh(msg.minor);
1293 }
1294
1295 DBG("Version check done using protocol %u.%u", cmd->session->major,
1296 cmd->session->minor);
b8aa1682
JD
1297
1298end:
1299 return ret;
1300}
1301
c8f59ee5 1302/*
6d805429 1303 * Check for data pending for a given stream id from the session daemon.
c8f59ee5
DG
1304 */
1305static
6d805429 1306int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
c8f59ee5
DG
1307 struct relay_command *cmd, struct lttng_ht *streams_ht)
1308{
1309 struct relay_session *session = cmd->session;
6d805429 1310 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
1311 struct lttcomm_relayd_generic_reply reply;
1312 struct relay_stream *stream;
1313 int ret;
c8f59ee5
DG
1314 uint64_t last_net_seq_num, stream_id;
1315
6d805429 1316 DBG("Data pending command received");
c8f59ee5 1317
c5b6f4f0 1318 if (!session || cmd->version_check_done == 0) {
c8f59ee5
DG
1319 ERR("Trying to check for data before version check");
1320 ret = -1;
1321 goto end_no_session;
1322 }
1323
7c5aef62 1324 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
c8f59ee5 1325 if (ret < sizeof(msg)) {
a6cd2b97
DG
1326 if (ret == 0) {
1327 /* Orderly shutdown. Not necessary to print an error. */
1328 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1329 } else {
1330 ERR("Relay didn't receive valid data_pending struct size : %d",
1331 ret);
1332 }
c8f59ee5
DG
1333 ret = -1;
1334 goto end_no_session;
1335 }
1336
1337 stream_id = be64toh(msg.stream_id);
1338 last_net_seq_num = be64toh(msg.last_net_seq_num);
1339
1340 rcu_read_lock();
de91f48a
DG
1341 stream = relay_stream_from_stream_id(stream_id, streams_ht);
1342 if (stream == NULL) {
c8f59ee5
DG
1343 ret = -1;
1344 goto end_unlock;
1345 }
1346
6d805429 1347 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
c8f59ee5
DG
1348 " and last_seq %" PRIu64, stream_id, stream->prev_seq,
1349 last_net_seq_num);
1350
33832e64 1351 /* Avoid wrapping issue */
39df6d9f 1352 if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) {
6d805429 1353 /* Data has in fact been written and is NOT pending */
c8f59ee5 1354 ret = 0;
6d805429
DG
1355 } else {
1356 /* Data still being streamed thus pending */
1357 ret = 1;
c8f59ee5
DG
1358 }
1359
f7079f67
DG
1360 /* Pending check is now done. */
1361 stream->data_pending_check_done = 1;
1362
c8f59ee5
DG
1363end_unlock:
1364 rcu_read_unlock();
1365
1366 reply.ret_code = htobe32(ret);
1367 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1368 if (ret < 0) {
6d805429 1369 ERR("Relay data pending ret code failed");
c8f59ee5
DG
1370 }
1371
1372end_no_session:
1373 return ret;
1374}
1375
1376/*
1377 * Wait for the control socket to reach a quiescent state.
1378 *
1379 * Note that for now, when receiving this command from the session daemon, this
1380 * means that every subsequent commands or data received on the control socket
1381 * has been handled. So, this is why we simply return OK here.
1382 */
1383static
1384int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
ad7051c0 1385 struct relay_command *cmd, struct lttng_ht *streams_ht)
c8f59ee5
DG
1386{
1387 int ret;
ad7051c0
DG
1388 uint64_t stream_id;
1389 struct relay_stream *stream;
1390 struct lttng_ht_iter iter;
1391 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
1392 struct lttcomm_relayd_generic_reply reply;
1393
1394 DBG("Checking quiescent state on control socket");
1395
ad7051c0
DG
1396 if (!cmd->session || cmd->version_check_done == 0) {
1397 ERR("Trying to check for data before version check");
1398 ret = -1;
1399 goto end_no_session;
1400 }
1401
1402 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1403 if (ret < sizeof(msg)) {
a6cd2b97
DG
1404 if (ret == 0) {
1405 /* Orderly shutdown. Not necessary to print an error. */
1406 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1407 } else {
1408 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1409 ret);
1410 }
ad7051c0
DG
1411 ret = -1;
1412 goto end_no_session;
1413 }
1414
1415 stream_id = be64toh(msg.stream_id);
1416
1417 rcu_read_lock();
1418 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
1419 if (stream->stream_handle == stream_id) {
1420 stream->data_pending_check_done = 1;
1421 DBG("Relay quiescent control pending flag set to %" PRIu64,
1422 stream_id);
1423 break;
1424 }
1425 }
1426 rcu_read_unlock();
1427
c8f59ee5
DG
1428 reply.ret_code = htobe32(LTTNG_OK);
1429 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1430 if (ret < 0) {
6d805429 1431 ERR("Relay data quiescent control ret code failed");
c8f59ee5
DG
1432 }
1433
ad7051c0 1434end_no_session:
c8f59ee5
DG
1435 return ret;
1436}
1437
f7079f67
DG
1438/*
1439 * Initialize a data pending command. This means that a client is about to ask
1440 * for data pending for each stream he/she holds. Simply iterate over all
1441 * streams of a session and set the data_pending_check_done flag.
1442 *
1443 * This command returns to the client a LTTNG_OK code.
1444 */
1445static
1446int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
1447 struct relay_command *cmd, struct lttng_ht *streams_ht)
1448{
1449 int ret;
1450 struct lttng_ht_iter iter;
1451 struct lttcomm_relayd_begin_data_pending msg;
1452 struct lttcomm_relayd_generic_reply reply;
1453 struct relay_stream *stream;
1454 uint64_t session_id;
1455
1456 assert(recv_hdr);
1457 assert(cmd);
1458 assert(streams_ht);
1459
1460 DBG("Init streams for data pending");
1461
1462 if (!cmd->session || cmd->version_check_done == 0) {
1463 ERR("Trying to check for data before version check");
1464 ret = -1;
1465 goto end_no_session;
1466 }
1467
1468 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1469 if (ret < sizeof(msg)) {
a6cd2b97
DG
1470 if (ret == 0) {
1471 /* Orderly shutdown. Not necessary to print an error. */
1472 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1473 } else {
1474 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1475 ret);
1476 }
f7079f67
DG
1477 ret = -1;
1478 goto end_no_session;
1479 }
1480
1481 session_id = be64toh(msg.session_id);
1482
1483 /*
1484 * Iterate over all streams to set the begin data pending flag. For now, the
1485 * streams are indexed by stream handle so we have to iterate over all
1486 * streams to find the one associated with the right session_id.
1487 */
1488 rcu_read_lock();
1489 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
1490 if (stream->session->id == session_id) {
1491 stream->data_pending_check_done = 0;
1492 DBG("Set begin data pending flag to stream %" PRIu64,
1493 stream->stream_handle);
1494 }
1495 }
1496 rcu_read_unlock();
1497
1498 /* All good, send back reply. */
1499 reply.ret_code = htobe32(LTTNG_OK);
1500
1501 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1502 if (ret < 0) {
1503 ERR("Relay begin data pending send reply failed");
1504 }
1505
1506end_no_session:
1507 return ret;
1508}
1509
1510/*
1511 * End data pending command. This will check, for a given session id, if each
1512 * stream associated with it has its data_pending_check_done flag set. If not,
1513 * this means that the client lost track of the stream but the data is still
1514 * being streamed on our side. In this case, we inform the client that data is
1515 * inflight.
1516 *
1517 * Return to the client if there is data in flight or not with a ret_code.
1518 */
1519static
1520int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
1521 struct relay_command *cmd, struct lttng_ht *streams_ht)
1522{
1523 int ret;
1524 struct lttng_ht_iter iter;
1525 struct lttcomm_relayd_end_data_pending msg;
1526 struct lttcomm_relayd_generic_reply reply;
1527 struct relay_stream *stream;
1528 uint64_t session_id;
1529 uint32_t is_data_inflight = 0;
1530
1531 assert(recv_hdr);
1532 assert(cmd);
1533 assert(streams_ht);
1534
1535 DBG("End data pending command");
1536
1537 if (!cmd->session || cmd->version_check_done == 0) {
1538 ERR("Trying to check for data before version check");
1539 ret = -1;
1540 goto end_no_session;
1541 }
1542
1543 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1544 if (ret < sizeof(msg)) {
a6cd2b97
DG
1545 if (ret == 0) {
1546 /* Orderly shutdown. Not necessary to print an error. */
1547 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1548 } else {
1549 ERR("Relay didn't receive valid end data_pending struct size: %d",
1550 ret);
1551 }
f7079f67
DG
1552 ret = -1;
1553 goto end_no_session;
1554 }
1555
1556 session_id = be64toh(msg.session_id);
1557
1558 /* Iterate over all streams to see if the begin data pending flag is set. */
1559 rcu_read_lock();
1560 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
1561 if (stream->session->id == session_id &&
1562 !stream->data_pending_check_done) {
1563 is_data_inflight = 1;
1564 DBG("Data is still in flight for stream %" PRIu64,
1565 stream->stream_handle);
1566 break;
1567 }
1568 }
1569 rcu_read_unlock();
1570
1571 /* All good, send back reply. */
1572 reply.ret_code = htobe32(is_data_inflight);
1573
1574 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1575 if (ret < 0) {
1576 ERR("Relay end data pending send reply failed");
1577 }
1578
1579end_no_session:
1580 return ret;
1581}
1582
b8aa1682
JD
1583/*
1584 * relay_process_control: Process the commands received on the control socket
1585 */
1586static
1587int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
1588 struct relay_command *cmd, struct lttng_ht *streams_ht)
1589{
1590 int ret = 0;
1591
1592 switch (be32toh(recv_hdr->cmd)) {
b8aa1682
JD
1593 case RELAYD_CREATE_SESSION:
1594 ret = relay_create_session(recv_hdr, cmd);
1595 break;
b8aa1682
JD
1596 case RELAYD_ADD_STREAM:
1597 ret = relay_add_stream(recv_hdr, cmd, streams_ht);
1598 break;
1599 case RELAYD_START_DATA:
1600 ret = relay_start(recv_hdr, cmd);
1601 break;
1602 case RELAYD_SEND_METADATA:
1603 ret = relay_recv_metadata(recv_hdr, cmd, streams_ht);
1604 break;
1605 case RELAYD_VERSION:
d4519fa3 1606 ret = relay_send_version(recv_hdr, cmd, streams_ht);
b8aa1682 1607 break;
173af62f
DG
1608 case RELAYD_CLOSE_STREAM:
1609 ret = relay_close_stream(recv_hdr, cmd, streams_ht);
1610 break;
6d805429
DG
1611 case RELAYD_DATA_PENDING:
1612 ret = relay_data_pending(recv_hdr, cmd, streams_ht);
c8f59ee5
DG
1613 break;
1614 case RELAYD_QUIESCENT_CONTROL:
ad7051c0 1615 ret = relay_quiescent_control(recv_hdr, cmd, streams_ht);
c8f59ee5 1616 break;
f7079f67
DG
1617 case RELAYD_BEGIN_DATA_PENDING:
1618 ret = relay_begin_data_pending(recv_hdr, cmd, streams_ht);
1619 break;
1620 case RELAYD_END_DATA_PENDING:
1621 ret = relay_end_data_pending(recv_hdr, cmd, streams_ht);
1622 break;
b8aa1682
JD
1623 case RELAYD_UPDATE_SYNC_INFO:
1624 default:
1625 ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
1626 relay_unknown_command(cmd);
1627 ret = -1;
1628 goto end;
1629 }
1630
1631end:
1632 return ret;
1633}
1634
1635/*
1636 * relay_process_data: Process the data received on the data socket
1637 */
1638static
1639int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
1640{
1641 int ret = 0;
1642 struct relay_stream *stream;
1643 struct lttcomm_relayd_data_hdr data_hdr;
1644 uint64_t stream_id;
173af62f 1645 uint64_t net_seq_num;
b8aa1682
JD
1646 uint32_t data_size;
1647
1648 ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr,
7c5aef62 1649 sizeof(struct lttcomm_relayd_data_hdr), 0);
b8aa1682 1650 if (ret <= 0) {
a6cd2b97
DG
1651 if (ret == 0) {
1652 /* Orderly shutdown. Not necessary to print an error. */
1653 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1654 } else {
1655 ERR("Unable to receive data header on sock %d", cmd->sock->fd);
1656 }
b8aa1682
JD
1657 ret = -1;
1658 goto end;
1659 }
1660
1661 stream_id = be64toh(data_hdr.stream_id);
9d1bbf21
MD
1662
1663 rcu_read_lock();
b8aa1682
JD
1664 stream = relay_stream_from_stream_id(stream_id, streams_ht);
1665 if (!stream) {
1666 ret = -1;
9d1bbf21 1667 goto end_unlock;
b8aa1682
JD
1668 }
1669
1670 data_size = be32toh(data_hdr.data_size);
1671 if (data_buffer_size < data_size) {
c617c0c6
MD
1672 char *tmp_data_ptr;
1673
1674 tmp_data_ptr = realloc(data_buffer, data_size);
1675 if (!tmp_data_ptr) {
b8aa1682 1676 ERR("Allocating data buffer");
c617c0c6 1677 free(data_buffer);
b8aa1682 1678 ret = -1;
9d1bbf21 1679 goto end_unlock;
b8aa1682 1680 }
c617c0c6 1681 data_buffer = tmp_data_ptr;
b8aa1682
JD
1682 data_buffer_size = data_size;
1683 }
1684 memset(data_buffer, 0, data_size);
1685
173af62f
DG
1686 net_seq_num = be64toh(data_hdr.net_seq_num);
1687
77c7c900 1688 DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64,
173af62f 1689 data_size, stream_id, net_seq_num);
7c5aef62 1690 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682 1691 if (ret <= 0) {
a6cd2b97
DG
1692 if (ret == 0) {
1693 /* Orderly shutdown. Not necessary to print an error. */
1694 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1695 }
b8aa1682 1696 ret = -1;
9d1bbf21 1697 goto end_unlock;
b8aa1682
JD
1698 }
1699
6f94560a
MD
1700 do {
1701 ret = write(stream->fd, data_buffer, data_size);
1702 } while (ret < 0 && errno == EINTR);
4cec016f 1703 if (ret < 0 || ret != data_size) {
b8aa1682
JD
1704 ERR("Relay error writing data to file");
1705 ret = -1;
9d1bbf21 1706 goto end_unlock;
b8aa1682 1707 }
1d4dfdef 1708
5ab7344e
JD
1709 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64,
1710 ret, stream->stream_handle);
1711
1d4dfdef
DG
1712 ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size));
1713 if (ret < 0) {
1714 goto end_unlock;
1715 }
1716
173af62f
DG
1717 stream->prev_seq = net_seq_num;
1718
1719 /* Check if we need to close the FD */
1720 if (close_stream_check(stream)) {
f66c074c 1721 int cret;
173af62f
DG
1722 struct lttng_ht_iter iter;
1723
f66c074c
DG
1724 cret = close(stream->fd);
1725 if (cret < 0) {
1726 PERROR("close stream process data");
1727 }
173af62f
DG
1728 iter.iter.node = &stream->stream_n.node;
1729 ret = lttng_ht_del(streams_ht, &iter);
1730 assert(!ret);
1731 call_rcu(&stream->rcu_node,
1732 deferred_free_stream);
1733 DBG("Closed tracefile %d after recv data", stream->fd);
1734 }
1735
9d1bbf21
MD
1736end_unlock:
1737 rcu_read_unlock();
b8aa1682
JD
1738end:
1739 return ret;
1740}
1741
1742static
9d1bbf21 1743void relay_cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
b8aa1682
JD
1744{
1745 int ret;
1746
b8aa1682
JD
1747 lttng_poll_del(events, pollfd);
1748
1749 ret = close(pollfd);
1750 if (ret < 0) {
1751 ERR("Closing pollfd %d", pollfd);
1752 }
1753}
1754
1755static
1756int relay_add_connection(int fd, struct lttng_poll_event *events,
1757 struct lttng_ht *relay_connections_ht)
1758{
b8aa1682 1759 struct relay_command *relay_connection;
9d1bbf21 1760 int ret;
b8aa1682
JD
1761
1762 relay_connection = zmalloc(sizeof(struct relay_command));
1763 if (relay_connection == NULL) {
1764 PERROR("Relay command zmalloc");
9d1bbf21 1765 goto error;
b8aa1682 1766 }
f921c78f
DG
1767 do {
1768 ret = read(fd, relay_connection, sizeof(struct relay_command));
1769 } while (ret < 0 && errno == EINTR);
cdf0f8fc 1770 if (ret < 0 || ret < sizeof(struct relay_command)) {
b8aa1682 1771 PERROR("read relay cmd pipe");
9d1bbf21 1772 goto error_read;
b8aa1682
JD
1773 }
1774
1775 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1776 (unsigned long) relay_connection->sock->fd);
9d1bbf21 1777 rcu_read_lock();
b8aa1682
JD
1778 lttng_ht_add_unique_ulong(relay_connections_ht,
1779 &relay_connection->sock_n);
9d1bbf21
MD
1780 rcu_read_unlock();
1781 return lttng_poll_add(events,
b8aa1682
JD
1782 relay_connection->sock->fd,
1783 LPOLLIN | LPOLLRDHUP);
1784
9d1bbf21
MD
1785error_read:
1786 free(relay_connection);
1787error:
1788 return -1;
1789}
1790
1791static
1792void deferred_free_connection(struct rcu_head *head)
1793{
1794 struct relay_command *relay_connection =
1795 caa_container_of(head, struct relay_command, rcu_node);
5b6d8097
DG
1796
1797 lttcomm_destroy_sock(relay_connection->sock);
9d1bbf21
MD
1798 free(relay_connection);
1799}
1800
1801static
1802void relay_del_connection(struct lttng_ht *relay_connections_ht,
1803 struct lttng_ht *streams_ht, struct lttng_ht_iter *iter,
1804 struct relay_command *relay_connection)
1805{
1806 int ret;
1807
1808 ret = lttng_ht_del(relay_connections_ht, iter);
1809 assert(!ret);
1810 if (relay_connection->type == RELAY_CONTROL) {
1811 relay_delete_session(relay_connection, streams_ht);
1812 }
5b6d8097 1813
9d1bbf21
MD
1814 call_rcu(&relay_connection->rcu_node,
1815 deferred_free_connection);
b8aa1682
JD
1816}
1817
1818/*
1819 * This thread does the actual work
1820 */
1821static
1822void *relay_thread_worker(void *data)
1823{
beaad64c
DG
1824 int ret, err = -1, last_seen_data_fd = -1;
1825 uint32_t nb_fd;
b8aa1682
JD
1826 struct relay_command *relay_connection;
1827 struct lttng_poll_event events;
1828 struct lttng_ht *relay_connections_ht;
1829 struct lttng_ht_node_ulong *node;
1830 struct lttng_ht_iter iter;
1831 struct lttng_ht *streams_ht;
1832 struct lttcomm_relayd_hdr recv_hdr;
1833
1834 DBG("[thread] Relay worker started");
1835
9d1bbf21
MD
1836 rcu_register_thread();
1837
b8aa1682
JD
1838 /* table of connections indexed on socket */
1839 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
1840 if (!relay_connections_ht) {
1841 goto relay_connections_ht_error;
1842 }
b8aa1682
JD
1843
1844 /* tables of streams indexed by stream ID */
1845 streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
1846 if (!streams_ht) {
1847 goto streams_ht_error;
1848 }
b8aa1682
JD
1849
1850 ret = create_thread_poll_set(&events, 2);
1851 if (ret < 0) {
1852 goto error_poll_create;
1853 }
1854
1855 ret = lttng_poll_add(&events, relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1856 if (ret < 0) {
1857 goto error;
1858 }
1859
beaad64c 1860restart:
b8aa1682 1861 while (1) {
beaad64c
DG
1862 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
1863
b8aa1682 1864 /* Infinite blocking call, waiting for transmission */
87c1611d 1865 DBG3("Relayd worker thread polling...");
b8aa1682
JD
1866 ret = lttng_poll_wait(&events, -1);
1867 if (ret < 0) {
1868 /*
1869 * Restart interrupted system call.
1870 */
1871 if (errno == EINTR) {
1872 goto restart;
1873 }
1874 goto error;
1875 }
1876
0d9c5d77
DG
1877 nb_fd = ret;
1878
beaad64c
DG
1879 /*
1880 * Process control. The control connection is prioritised so we don't
1881 * starve it with high throughout put tracing data on the data
1882 * connection.
1883 */
b8aa1682
JD
1884 for (i = 0; i < nb_fd; i++) {
1885 /* Fetch once the poll data */
beaad64c
DG
1886 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1887 int pollfd = LTTNG_POLL_GETFD(&events, i);
b8aa1682
JD
1888
1889 /* Thread quit pipe has been closed. Killing thread. */
1890 ret = check_thread_quit_pipe(pollfd, revents);
1891 if (ret) {
095a4ae5
MD
1892 err = 0;
1893 goto exit;
b8aa1682
JD
1894 }
1895
1896 /* Inspect the relay cmd pipe for new connection */
1897 if (pollfd == relay_cmd_pipe[0]) {
1898 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1899 ERR("Relay pipe error");
1900 goto error;
1901 } else if (revents & LPOLLIN) {
1902 DBG("Relay command received");
1903 ret = relay_add_connection(relay_cmd_pipe[0],
1904 &events, relay_connections_ht);
1905 if (ret < 0) {
1906 goto error;
1907 }
1908 }
beaad64c 1909 } else if (revents) {
9d1bbf21 1910 rcu_read_lock();
b8aa1682
JD
1911 lttng_ht_lookup(relay_connections_ht,
1912 (void *)((unsigned long) pollfd),
1913 &iter);
1914 node = lttng_ht_iter_get_node_ulong(&iter);
1915 if (node == NULL) {
1916 DBG2("Relay sock %d not found", pollfd);
9d1bbf21 1917 rcu_read_unlock();
b8aa1682
JD
1918 goto error;
1919 }
1920 relay_connection = caa_container_of(node,
1921 struct relay_command, sock_n);
1922
1923 if (revents & (LPOLLERR)) {
1924 ERR("POLL ERROR");
9d1bbf21
MD
1925 relay_cleanup_poll_connection(&events, pollfd);
1926 relay_del_connection(relay_connections_ht,
1927 streams_ht, &iter,
1928 relay_connection);
beaad64c
DG
1929 if (last_seen_data_fd == pollfd) {
1930 last_seen_data_fd = last_notdel_data_fd;
1931 }
b8aa1682
JD
1932 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1933 DBG("Socket %d hung up", pollfd);
9d1bbf21
MD
1934 relay_cleanup_poll_connection(&events, pollfd);
1935 relay_del_connection(relay_connections_ht,
1936 streams_ht, &iter,
1937 relay_connection);
beaad64c
DG
1938 if (last_seen_data_fd == pollfd) {
1939 last_seen_data_fd = last_notdel_data_fd;
1940 }
b8aa1682
JD
1941 } else if (revents & LPOLLIN) {
1942 /* control socket */
1943 if (relay_connection->type == RELAY_CONTROL) {
1944 ret = relay_connection->sock->ops->recvmsg(
1945 relay_connection->sock, &recv_hdr,
7c5aef62 1946 sizeof(struct lttcomm_relayd_hdr), 0);
b8aa1682
JD
1947 /* connection closed */
1948 if (ret <= 0) {
9d1bbf21
MD
1949 relay_cleanup_poll_connection(&events, pollfd);
1950 relay_del_connection(relay_connections_ht,
1951 streams_ht, &iter,
1952 relay_connection);
b8aa1682
JD
1953 DBG("Control connection closed with %d", pollfd);
1954 } else {
1955 if (relay_connection->session) {
77c7c900 1956 DBG2("Relay worker receiving data for session : %" PRIu64,
b8aa1682
JD
1957 relay_connection->session->id);
1958 }
1959 ret = relay_process_control(&recv_hdr,
1960 relay_connection,
1961 streams_ht);
b8aa1682 1962 if (ret < 0) {
beaad64c 1963 /* Clear the session on error. */
9d1bbf21
MD
1964 relay_cleanup_poll_connection(&events, pollfd);
1965 relay_del_connection(relay_connections_ht,
1966 streams_ht, &iter,
1967 relay_connection);
b8aa1682
JD
1968 DBG("Connection closed with %d", pollfd);
1969 }
beaad64c 1970 seen_control = 1;
b8aa1682 1971 }
beaad64c
DG
1972 } else {
1973 /*
1974 * Flag the last seen data fd not deleted. It will be
1975 * used as the last seen fd if any fd gets deleted in
1976 * this first loop.
1977 */
1978 last_notdel_data_fd = pollfd;
1979 }
1980 }
1981 rcu_read_unlock();
1982 }
1983 }
1984
1985 /*
1986 * The last loop handled a control request, go back to poll to make
1987 * sure we prioritise the control socket.
1988 */
1989 if (seen_control) {
1990 continue;
1991 }
1992
1993 if (last_seen_data_fd >= 0) {
1994 for (i = 0; i < nb_fd; i++) {
1995 int pollfd = LTTNG_POLL_GETFD(&events, i);
1996 if (last_seen_data_fd == pollfd) {
1997 idx = i;
1998 break;
1999 }
2000 }
2001 }
2002
2003 /* Process data connection. */
2004 for (i = idx + 1; i < nb_fd; i++) {
2005 /* Fetch the poll data. */
2006 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2007 int pollfd = LTTNG_POLL_GETFD(&events, i);
2008
2009 /* Skip the command pipe. It's handled in the first loop. */
2010 if (pollfd == relay_cmd_pipe[0]) {
2011 continue;
2012 }
2013
2014 if (revents) {
2015 rcu_read_lock();
2016 lttng_ht_lookup(relay_connections_ht,
2017 (void *)((unsigned long) pollfd),
2018 &iter);
2019 node = lttng_ht_iter_get_node_ulong(&iter);
2020 if (node == NULL) {
2021 /* Skip it. Might be removed before. */
2022 rcu_read_unlock();
2023 continue;
2024 }
2025 relay_connection = caa_container_of(node,
2026 struct relay_command, sock_n);
2027
2028 if (revents & LPOLLIN) {
2029 if (relay_connection->type != RELAY_DATA) {
2030 continue;
2031 }
2032
2033 ret = relay_process_data(relay_connection, streams_ht);
2034 /* connection closed */
2035 if (ret < 0) {
2036 relay_cleanup_poll_connection(&events, pollfd);
2037 relay_del_connection(relay_connections_ht,
2038 streams_ht, &iter,
2039 relay_connection);
2040 DBG("Data connection closed with %d", pollfd);
2041 /*
2042 * Every goto restart call sets the last seen fd where
2043 * here we don't really care since we gracefully
2044 * continue the loop after the connection is deleted.
2045 */
2046 } else {
2047 /* Keep last seen port. */
2048 last_seen_data_fd = pollfd;
2049 rcu_read_unlock();
2050 goto restart;
b8aa1682
JD
2051 }
2052 }
9d1bbf21 2053 rcu_read_unlock();
b8aa1682
JD
2054 }
2055 }
beaad64c 2056 last_seen_data_fd = -1;
b8aa1682
JD
2057 }
2058
095a4ae5 2059exit:
b8aa1682
JD
2060error:
2061 lttng_poll_clean(&events);
2062
2063 /* empty the hash table and free the memory */
9d1bbf21 2064 rcu_read_lock();
b8aa1682
JD
2065 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
2066 node = lttng_ht_iter_get_node_ulong(&iter);
2067 if (node) {
2068 relay_connection = caa_container_of(node,
2069 struct relay_command, sock_n);
9d1bbf21
MD
2070 relay_del_connection(relay_connections_ht,
2071 streams_ht, &iter,
2072 relay_connection);
b8aa1682 2073 }
b8aa1682 2074 }
9d1bbf21 2075 rcu_read_unlock();
b8aa1682 2076error_poll_create:
095a4ae5
MD
2077 lttng_ht_destroy(streams_ht);
2078streams_ht_error:
b8aa1682 2079 lttng_ht_destroy(relay_connections_ht);
095a4ae5 2080relay_connections_ht_error:
6620da75
DG
2081 /* Close relay cmd pipes */
2082 utils_close_pipe(relay_cmd_pipe);
095a4ae5
MD
2083 if (err) {
2084 DBG("Thread exited with error");
2085 }
b8aa1682 2086 DBG("Worker thread cleanup complete");
095a4ae5 2087 free(data_buffer);
b8aa1682 2088 stop_threads();
9d1bbf21 2089 rcu_unregister_thread();
b8aa1682
JD
2090 return NULL;
2091}
2092
2093/*
2094 * Create the relay command pipe to wake thread_manage_apps.
2095 * Closed in cleanup().
2096 */
2097static int create_relay_cmd_pipe(void)
2098{
a02de639 2099 int ret;
b8aa1682 2100
a02de639 2101 ret = utils_create_pipe_cloexec(relay_cmd_pipe);
b8aa1682 2102
b8aa1682
JD
2103 return ret;
2104}
2105
2106/*
2107 * main
2108 */
2109int main(int argc, char **argv)
2110{
2111 int ret = 0;
2112 void *status;
2113
2114 /* Create thread quit pipe */
2115 if ((ret = init_thread_quit_pipe()) < 0) {
2116 goto error;
2117 }
2118
2119 /* Parse arguments */
2120 progname = argv[0];
c617c0c6 2121 if ((ret = parse_args(argc, argv)) < 0) {
a02de639 2122 goto exit;
b8aa1682
JD
2123 }
2124
2125 if ((ret = set_signal_handler()) < 0) {
2126 goto exit;
2127 }
2128
4d513a50
DG
2129 /* Try to create directory if -o, --output is specified. */
2130 if (opt_output_path) {
2131 ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG);
2132 if (ret < 0) {
2133 ERR("Unable to create %s", opt_output_path);
2134 goto exit;
2135 }
2136 }
2137
b8aa1682
JD
2138 /* Daemonize */
2139 if (opt_daemon) {
2140 ret = daemon(0, 0);
2141 if (ret < 0) {
2142 PERROR("daemon");
a02de639 2143 goto exit;
b8aa1682
JD
2144 }
2145 }
2146
2147 /* Check if daemon is UID = 0 */
2148 is_root = !getuid();
2149
2150 if (!is_root) {
2151 if (control_uri->port < 1024 || data_uri->port < 1024) {
2152 ERR("Need to be root to use ports < 1024");
2153 ret = -1;
a02de639 2154 goto exit;
b8aa1682
JD
2155 }
2156 }
2157
2158 /* Setup the thread apps communication pipe. */
2159 if ((ret = create_relay_cmd_pipe()) < 0) {
2160 goto exit;
2161 }
2162
2163 /* Init relay command queue. */
2164 cds_wfq_init(&relay_cmd_queue.queue);
2165
2166 /* Set up max poll set size */
2167 lttng_poll_set_max_size();
2168
2169 /* Setup the dispatcher thread */
2170 ret = pthread_create(&dispatcher_thread, NULL,
2171 relay_thread_dispatcher, (void *) NULL);
2172 if (ret != 0) {
2173 PERROR("pthread_create dispatcher");
2174 goto exit_dispatcher;
2175 }
2176
2177 /* Setup the worker thread */
2178 ret = pthread_create(&worker_thread, NULL,
2179 relay_thread_worker, (void *) NULL);
2180 if (ret != 0) {
2181 PERROR("pthread_create worker");
2182 goto exit_worker;
2183 }
2184
2185 /* Setup the listener thread */
2186 ret = pthread_create(&listener_thread, NULL,
2187 relay_thread_listener, (void *) NULL);
2188 if (ret != 0) {
2189 PERROR("pthread_create listener");
2190 goto exit_listener;
2191 }
2192
2193exit_listener:
2194 ret = pthread_join(listener_thread, &status);
2195 if (ret != 0) {
2196 PERROR("pthread_join");
2197 goto error; /* join error, exit without cleanup */
2198 }
2199
2200exit_worker:
2201 ret = pthread_join(worker_thread, &status);
2202 if (ret != 0) {
2203 PERROR("pthread_join");
2204 goto error; /* join error, exit without cleanup */
2205 }
2206
2207exit_dispatcher:
2208 ret = pthread_join(dispatcher_thread, &status);
2209 if (ret != 0) {
2210 PERROR("pthread_join");
2211 goto error; /* join error, exit without cleanup */
2212 }
2213
2214exit:
2215 cleanup();
2216 if (!ret) {
2217 exit(EXIT_SUCCESS);
2218 }
a02de639 2219
b8aa1682
JD
2220error:
2221 exit(EXIT_FAILURE);
2222}
This page took 0.12493 seconds and 4 git commands to generate.