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