Tests: add print bytecode to filter grammar test
[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
242 /* Close relay cmd pipes */
243 utils_close_pipe(relay_cmd_pipe);
b8aa1682
JD
244}
245
246/*
247 * Write to writable pipe used to notify a thread.
248 */
249static
250int notify_thread_pipe(int wpipe)
251{
252 int ret;
253
6f94560a
MD
254 do {
255 ret = write(wpipe, "!", 1);
256 } while (ret < 0 && errno == EINTR);
b8aa1682
JD
257 if (ret < 0) {
258 PERROR("write poll pipe");
259 }
260
261 return ret;
262}
263
264/*
265 * Stop all threads by closing the thread quit pipe.
266 */
267static
268void stop_threads(void)
269{
270 int ret;
271
272 /* Stopping all threads */
273 DBG("Terminating all threads");
274 ret = notify_thread_pipe(thread_quit_pipe[1]);
275 if (ret < 0) {
276 ERR("write error on thread quit pipe");
277 }
278
279 /* Dispatch thread */
26c9d55e 280 CMM_STORE_SHARED(dispatch_thread_exit, 1);
b8aa1682
JD
281 futex_nto1_wake(&relay_cmd_queue.futex);
282}
283
284/*
285 * Signal handler for the daemon
286 *
287 * Simply stop all worker threads, leaving main() return gracefully after
288 * joining all threads and calling cleanup().
289 */
290static
291void sighandler(int sig)
292{
293 switch (sig) {
294 case SIGPIPE:
295 DBG("SIGPIPE caught");
296 return;
297 case SIGINT:
298 DBG("SIGINT caught");
299 stop_threads();
300 break;
301 case SIGTERM:
302 DBG("SIGTERM caught");
303 stop_threads();
304 break;
305 default:
306 break;
307 }
308}
309
310/*
311 * Setup signal handler for :
312 * SIGINT, SIGTERM, SIGPIPE
313 */
314static
315int set_signal_handler(void)
316{
317 int ret = 0;
318 struct sigaction sa;
319 sigset_t sigset;
320
321 if ((ret = sigemptyset(&sigset)) < 0) {
322 PERROR("sigemptyset");
323 return ret;
324 }
325
326 sa.sa_handler = sighandler;
327 sa.sa_mask = sigset;
328 sa.sa_flags = 0;
329 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
330 PERROR("sigaction");
331 return ret;
332 }
333
334 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
335 PERROR("sigaction");
336 return ret;
337 }
338
339 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
340 PERROR("sigaction");
341 return ret;
342 }
343
344 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
345
346 return ret;
347}
348
349/*
350 * Init thread quit pipe.
351 *
352 * Return -1 on error or 0 if all pipes are created.
353 */
354static
355int init_thread_quit_pipe(void)
356{
a02de639 357 int ret;
b8aa1682 358
a02de639 359 ret = utils_create_pipe_cloexec(thread_quit_pipe);
b8aa1682 360
b8aa1682
JD
361 return ret;
362}
363
364/*
365 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
366 */
367static
368int create_thread_poll_set(struct lttng_poll_event *events, int size)
369{
370 int ret;
371
372 if (events == NULL || size == 0) {
373 ret = -1;
374 goto error;
375 }
376
377 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
378 if (ret < 0) {
379 goto error;
380 }
381
382 /* Add quit pipe */
383 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
384 if (ret < 0) {
385 goto error;
386 }
387
388 return 0;
389
390error:
391 return ret;
392}
393
394/*
395 * Check if the thread quit pipe was triggered.
396 *
397 * Return 1 if it was triggered else 0;
398 */
399static
400int check_thread_quit_pipe(int fd, uint32_t events)
401{
402 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
403 return 1;
404 }
405
406 return 0;
407}
408
409/*
410 * Create and init socket from uri.
411 */
412static
413struct lttcomm_sock *relay_init_sock(struct lttng_uri *uri)
414{
415 int ret;
416 struct lttcomm_sock *sock = NULL;
417
418 sock = lttcomm_alloc_sock_from_uri(uri);
419 if (sock == NULL) {
420 ERR("Allocating socket");
421 goto error;
422 }
423
424 ret = lttcomm_create_sock(sock);
425 if (ret < 0) {
426 goto error;
427 }
428 DBG("Listening on sock %d", sock->fd);
429
430 ret = sock->ops->bind(sock);
431 if (ret < 0) {
432 goto error;
433 }
434
435 ret = sock->ops->listen(sock, -1);
436 if (ret < 0) {
437 goto error;
438
439 }
440
441 return sock;
442
443error:
444 if (sock) {
445 lttcomm_destroy_sock(sock);
446 }
447 return NULL;
448}
449
173af62f
DG
450/*
451 * Return nonzero if stream needs to be closed.
452 */
453static
454int close_stream_check(struct relay_stream *stream)
455{
456
457 if (stream->close_flag && stream->prev_seq == stream->last_net_seq_num) {
458 return 1;
459 }
460 return 0;
461}
462
b8aa1682
JD
463/*
464 * This thread manages the listening for new connections on the network
465 */
466static
467void *relay_thread_listener(void *data)
468{
095a4ae5 469 int i, ret, pollfd, err = -1;
b8aa1682
JD
470 int val = 1;
471 uint32_t revents, nb_fd;
472 struct lttng_poll_event events;
473 struct lttcomm_sock *control_sock, *data_sock;
474
475 /*
476 * Get allocated in this thread, enqueued to a global queue, dequeued and
477 * freed in the worker thread.
478 */
479 struct relay_command *relay_cmd = NULL;
480
481 DBG("[thread] Relay listener started");
482
483 control_sock = relay_init_sock(control_uri);
484 if (!control_sock) {
095a4ae5 485 goto error_sock_control;
b8aa1682
JD
486 }
487
488 data_sock = relay_init_sock(data_uri);
489 if (!data_sock) {
095a4ae5 490 goto error_sock_relay;
b8aa1682
JD
491 }
492
493 /*
494 * Pass 3 as size here for the thread quit pipe, control and data socket.
495 */
496 ret = create_thread_poll_set(&events, 3);
497 if (ret < 0) {
498 goto error_create_poll;
499 }
500
501 /* Add the control socket */
502 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
503 if (ret < 0) {
504 goto error_poll_add;
505 }
506
507 /* Add the data socket */
508 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
509 if (ret < 0) {
510 goto error_poll_add;
511 }
512
513 while (1) {
514 DBG("Listener accepting connections");
515
516 nb_fd = LTTNG_POLL_GETNB(&events);
517
518restart:
519 ret = lttng_poll_wait(&events, -1);
520 if (ret < 0) {
521 /*
522 * Restart interrupted system call.
523 */
524 if (errno == EINTR) {
525 goto restart;
526 }
527 goto error;
528 }
529
530 DBG("Relay new connection received");
531 for (i = 0; i < nb_fd; i++) {
532 /* Fetch once the poll data */
533 revents = LTTNG_POLL_GETEV(&events, i);
534 pollfd = LTTNG_POLL_GETFD(&events, i);
535
536 /* Thread quit pipe has been closed. Killing thread. */
537 ret = check_thread_quit_pipe(pollfd, revents);
538 if (ret) {
095a4ae5
MD
539 err = 0;
540 goto exit;
b8aa1682
JD
541 }
542
543 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
544 ERR("socket poll error");
545 goto error;
546 } else if (revents & LPOLLIN) {
547 struct lttcomm_sock *newsock = NULL;
548
549 relay_cmd = zmalloc(sizeof(struct relay_command));
550 if (relay_cmd == NULL) {
551 PERROR("relay command zmalloc");
552 goto error;
553 }
554
555 if (pollfd == data_sock->fd) {
556 newsock = data_sock->ops->accept(data_sock);
557 if (newsock < 0) {
558 PERROR("accepting data sock");
559 goto error;
560 }
561 relay_cmd->type = RELAY_DATA;
562 DBG("Relay data connection accepted, socket %d", newsock->fd);
563 } else if (pollfd == control_sock->fd) {
564 newsock = control_sock->ops->accept(control_sock);
565 if (newsock < 0) {
566 PERROR("accepting control sock");
567 goto error;
568 }
569 relay_cmd->type = RELAY_CONTROL;
570 DBG("Relay control connection accepted, socket %d", newsock->fd);
571 }
572 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR,
573 &val, sizeof(int));
574 if (ret < 0) {
575 PERROR("setsockopt inet");
576 goto error;
577 }
578 relay_cmd->sock = newsock;
579 /*
580 * Lock free enqueue the request.
581 */
582 cds_wfq_enqueue(&relay_cmd_queue.queue, &relay_cmd->node);
583
584 /*
585 * Wake the dispatch queue futex. Implicit memory
586 * barrier with the exchange in cds_wfq_enqueue.
587 */
588 futex_nto1_wake(&relay_cmd_queue.futex);
589 }
590 }
591 }
592
095a4ae5 593exit:
b8aa1682
JD
594error:
595error_poll_add:
596 lttng_poll_clean(&events);
597error_create_poll:
095a4ae5
MD
598 if (data_sock->fd >= 0) {
599 ret = data_sock->ops->close(data_sock);
b8aa1682
JD
600 if (ret) {
601 PERROR("close");
602 }
b8aa1682 603 }
095a4ae5
MD
604 lttcomm_destroy_sock(data_sock);
605error_sock_relay:
606 if (control_sock->fd >= 0) {
607 ret = control_sock->ops->close(control_sock);
b8aa1682
JD
608 if (ret) {
609 PERROR("close");
610 }
b8aa1682 611 }
095a4ae5
MD
612 lttcomm_destroy_sock(control_sock);
613error_sock_control:
614 if (err) {
615 DBG("Thread exited with error");
616 }
b8aa1682
JD
617 DBG("Relay listener thread cleanup complete");
618 stop_threads();
b8aa1682
JD
619 return NULL;
620}
621
622/*
623 * This thread manages the dispatching of the requests to worker threads
624 */
625static
626void *relay_thread_dispatcher(void *data)
627{
628 int ret;
629 struct cds_wfq_node *node;
630 struct relay_command *relay_cmd = NULL;
631
632 DBG("[thread] Relay dispatcher started");
633
26c9d55e 634 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
b8aa1682
JD
635 /* Atomically prepare the queue futex */
636 futex_nto1_prepare(&relay_cmd_queue.futex);
637
638 do {
639 /* Dequeue commands */
640 node = cds_wfq_dequeue_blocking(&relay_cmd_queue.queue);
641 if (node == NULL) {
642 DBG("Woken up but nothing in the relay command queue");
643 /* Continue thread execution */
644 break;
645 }
646
647 relay_cmd = caa_container_of(node, struct relay_command, node);
648 DBG("Dispatching request waiting on sock %d", relay_cmd->sock->fd);
649
650 /*
651 * Inform worker thread of the new request. This
652 * call is blocking so we can be assured that the data will be read
653 * at some point in time or wait to the end of the world :)
654 */
6f94560a
MD
655 do {
656 ret = write(relay_cmd_pipe[1], relay_cmd,
657 sizeof(struct relay_command));
658 } while (ret < 0 && errno == EINTR);
b8aa1682
JD
659 free(relay_cmd);
660 if (ret < 0) {
661 PERROR("write cmd pipe");
662 goto error;
663 }
664 } while (node != NULL);
665
666 /* Futex wait on queue. Blocking call on futex() */
667 futex_nto1_wait(&relay_cmd_queue.futex);
668 }
669
670error:
671 DBG("Dispatch thread dying");
672 stop_threads();
673 return NULL;
674}
675
676/*
677 * Return the realpath(3) of the path even if the last directory token does not
678 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
679 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
680 * fails if the end point directory does not exist.
681 */
682static
683char *expand_full_path(const char *path)
684{
685 const char *end_path = path;
095a4ae5 686 char *next, *cut_path, *expanded_path, *respath;
b8aa1682
JD
687
688 /* Find last token delimited by '/' */
689 while ((next = strpbrk(end_path + 1, "/"))) {
690 end_path = next;
691 }
692
693 /* Cut last token from original path */
694 cut_path = strndup(path, end_path - path);
695
696 expanded_path = malloc(PATH_MAX);
697 if (expanded_path == NULL) {
095a4ae5
MD
698 respath = NULL;
699 goto end;
b8aa1682
JD
700 }
701
095a4ae5
MD
702 respath = realpath(cut_path, expanded_path);
703 if (respath == NULL) {
b8aa1682
JD
704 switch (errno) {
705 case ENOENT:
706 ERR("%s: No such file or directory", cut_path);
707 break;
708 default:
709 PERROR("realpath");
710 break;
711 }
095a4ae5
MD
712 free(expanded_path);
713 } else {
714 /* Add end part to expanded path */
715 strcat(respath, end_path);
b8aa1682 716 }
095a4ae5 717end:
b8aa1682 718 free(cut_path);
095a4ae5 719 return respath;
b8aa1682
JD
720}
721
722
723/*
724 * config_get_default_path
725 *
726 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
727 */
728static
729char *config_get_default_path(void)
730{
731 return getenv("HOME");
732}
733
734/*
735 * Create recursively directory using the FULL path.
736 */
737static
738int mkdir_recursive(char *path, mode_t mode)
739{
740 char *p, tmp[PATH_MAX];
741 struct stat statbuf;
742 size_t len;
743 int ret;
744
745 ret = snprintf(tmp, sizeof(tmp), "%s", path);
746 if (ret < 0) {
747 PERROR("snprintf mkdir");
748 goto error;
749 }
750
751 len = ret;
752 if (tmp[len - 1] == '/') {
753 tmp[len - 1] = 0;
754 }
755
756 for (p = tmp + 1; *p; p++) {
757 if (*p == '/') {
758 *p = 0;
759 if (tmp[strlen(tmp) - 1] == '.' &&
760 tmp[strlen(tmp) - 2] == '.' &&
761 tmp[strlen(tmp) - 3] == '/') {
762 ERR("Using '/../' is not permitted in the trace path (%s)",
763 tmp);
764 ret = -1;
765 goto error;
766 }
767 ret = stat(tmp, &statbuf);
768 if (ret < 0) {
769 ret = mkdir(tmp, mode);
770 if (ret < 0) {
095a4ae5 771 if (errno != EEXIST) {
b8aa1682
JD
772 PERROR("mkdir recursive");
773 ret = -errno;
774 goto error;
775 }
776 }
777 }
778 *p = '/';
779 }
780 }
781
782 ret = mkdir(tmp, mode);
783 if (ret < 0) {
095a4ae5 784 if (errno != EEXIST) {
b8aa1682
JD
785 PERROR("mkdir recursive last piece");
786 ret = -errno;
787 } else {
788 ret = 0;
789 }
790 }
791
792error:
793 return ret;
794}
795
b8aa1682 796static
095a4ae5 797char *create_output_path_auto(char *path_name)
b8aa1682 798{
095a4ae5 799 int ret;
b8aa1682 800 char *traces_path = NULL;
095a4ae5
MD
801 char *alloc_path = NULL;
802 char *default_path;
b8aa1682 803
095a4ae5
MD
804 default_path = config_get_default_path();
805 if (default_path == NULL) {
806 ERR("Home path not found.\n \
807 Please specify an output path using -o, --output PATH");
808 goto exit;
809 }
810 alloc_path = strdup(default_path);
811 if (alloc_path == NULL) {
812 PERROR("Path allocation");
813 goto exit;
814 }
815 ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME
816 "/%s", alloc_path, path_name);
817 if (ret < 0) {
818 PERROR("asprintf trace dir name");
819 goto exit;
b8aa1682 820 }
095a4ae5 821exit:
b8aa1682 822 free(alloc_path);
095a4ae5
MD
823 return traces_path;
824}
825
826static
827char *create_output_path_noauto(char *path_name)
828{
829 int ret;
830 char *traces_path = NULL;
831 char *full_path;
b8aa1682 832
095a4ae5
MD
833 full_path = expand_full_path(opt_output_path);
834 ret = asprintf(&traces_path, "%s/%s", full_path, path_name);
835 if (ret < 0) {
836 PERROR("asprintf trace dir name");
837 goto exit;
838 }
b8aa1682 839exit:
095a4ae5 840 free(full_path);
b8aa1682
JD
841 return traces_path;
842}
843
095a4ae5
MD
844/*
845 * create_output_path: create the output trace directory
846 */
847static
848char *create_output_path(char *path_name)
849{
850 if (opt_output_path == NULL) {
851 return create_output_path_auto(path_name);
852 } else {
853 return create_output_path_noauto(path_name);
854 }
855}
856
9d1bbf21
MD
857static
858void deferred_free_stream(struct rcu_head *head)
859{
860 struct relay_stream *stream =
861 caa_container_of(head, struct relay_stream, rcu_node);
862 free(stream);
863}
864
b8aa1682
JD
865/*
866 * relay_delete_session: Free all memory associated with a session and
867 * close all the FDs
868 */
869static
870void relay_delete_session(struct relay_command *cmd, struct lttng_ht *streams_ht)
871{
872 struct lttng_ht_iter iter;
873 struct lttng_ht_node_ulong *node;
874 struct relay_stream *stream;
875 int ret;
876
095a4ae5 877 if (!cmd->session) {
b8aa1682 878 return;
095a4ae5 879 }
b8aa1682 880
77c7c900 881 DBG("Relay deleting session %" PRIu64, cmd->session->id);
b8aa1682
JD
882 free(cmd->session->sock);
883
9d1bbf21 884 rcu_read_lock();
b8aa1682
JD
885 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, node, node) {
886 node = lttng_ht_iter_get_node_ulong(&iter);
887 if (node) {
888 stream = caa_container_of(node,
889 struct relay_stream, stream_n);
890 if (stream->session == cmd->session) {
891 close(stream->fd);
892 ret = lttng_ht_del(streams_ht, &iter);
893 assert(!ret);
9d1bbf21
MD
894 call_rcu(&stream->rcu_node,
895 deferred_free_stream);
b8aa1682
JD
896 }
897 }
898 }
9d1bbf21 899 rcu_read_unlock();
b8aa1682
JD
900}
901
902/*
903 * relay_add_stream: allocate a new stream for a session
904 */
905static
906int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
907 struct relay_command *cmd, struct lttng_ht *streams_ht)
908{
909 struct relay_session *session = cmd->session;
910 struct lttcomm_relayd_add_stream stream_info;
911 struct relay_stream *stream = NULL;
912 struct lttcomm_relayd_status_stream reply;
913 char *path = NULL, *root_path = NULL;
914 int ret, send_ret;
915
916 if (!session || session->version_check_done == 0) {
917 ERR("Trying to add a stream before version check");
918 ret = -1;
919 goto end_no_session;
920 }
921
922 /* FIXME : use data_size for something ? */
923 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
924 sizeof(struct lttcomm_relayd_add_stream), MSG_WAITALL);
925 if (ret < sizeof(struct lttcomm_relayd_add_stream)) {
926 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
927 ret = -1;
928 goto end_no_session;
929 }
930 stream = zmalloc(sizeof(struct relay_stream));
931 if (stream == NULL) {
932 PERROR("relay stream zmalloc");
933 ret = -1;
934 goto end_no_session;
935 }
936
9d1bbf21 937 rcu_read_lock();
b8aa1682 938 stream->stream_handle = ++last_relay_stream_id;
173af62f 939 stream->prev_seq = -1ULL;
b8aa1682
JD
940 stream->session = session;
941
942 root_path = create_output_path(stream_info.pathname);
943 if (!root_path) {
944 ret = -1;
945 goto end;
946 }
947 ret = mkdir_recursive(root_path, S_IRWXU | S_IRWXG);
948 if (ret < 0) {
b8aa1682
JD
949 ERR("relay creating output directory");
950 goto end;
951 }
952
953 ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name);
954 if (ret < 0) {
955 PERROR("asprintf stream path");
956 goto end;
957 }
958
959 ret = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
960 if (ret < 0) {
961 PERROR("Relay creating trace file");
962 goto end;
963 }
964
965 stream->fd = ret;
966 DBG("Tracefile %s created", path);
967
968 lttng_ht_node_init_ulong(&stream->stream_n,
969 (unsigned long) stream->stream_handle);
970 lttng_ht_add_unique_ulong(streams_ht,
971 &stream->stream_n);
972
973 DBG("Relay new stream added %s", stream_info.channel_name);
974
975end:
976 free(path);
977 free(root_path);
978 /* send the session id to the client or a negative return code on error */
979 if (ret < 0) {
f73fabfd 980 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682 981 } else {
f73fabfd 982 reply.ret_code = htobe32(LTTNG_OK);
b8aa1682
JD
983 }
984 reply.handle = htobe64(stream->stream_handle);
985 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
986 sizeof(struct lttcomm_relayd_status_stream), 0);
987 if (send_ret < 0) {
988 ERR("Relay sending stream id");
989 }
9d1bbf21 990 rcu_read_unlock();
b8aa1682
JD
991
992end_no_session:
993 return ret;
994}
995
173af62f
DG
996/*
997 * relay_close_stream: close a specific stream
998 */
999static
1000int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
1001 struct relay_command *cmd, struct lttng_ht *streams_ht)
1002{
1003 struct relay_session *session = cmd->session;
1004 struct lttcomm_relayd_close_stream stream_info;
1005 struct lttcomm_relayd_generic_reply reply;
1006 struct relay_stream *stream;
1007 int ret, send_ret;
1008 struct lttng_ht_node_ulong *node;
1009 struct lttng_ht_iter iter;
1010
1011 DBG("Close stream received");
1012
1013 if (!session || session->version_check_done == 0) {
1014 ERR("Trying to close a stream before version check");
1015 ret = -1;
1016 goto end_no_session;
1017 }
1018
1019 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
1020 sizeof(struct lttcomm_relayd_close_stream), MSG_WAITALL);
1021 if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
1022 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
1023 ret = -1;
1024 goto end_no_session;
1025 }
1026
1027 rcu_read_lock();
1028 lttng_ht_lookup(streams_ht,
1029 (void *)((unsigned long) be64toh(stream_info.stream_id)),
1030 &iter);
1031 node = lttng_ht_iter_get_node_ulong(&iter);
1032 if (node == NULL) {
77c7c900 1033 DBG("Relay stream %" PRIu64 " not found", be64toh(stream_info.stream_id));
173af62f
DG
1034 ret = -1;
1035 goto end_unlock;
1036 }
1037
1038 stream = caa_container_of(node, struct relay_stream, stream_n);
1039 if (!stream) {
1040 ret = -1;
1041 goto end_unlock;
1042 }
1043
1044 stream->close_flag = 1;
1045
1046 if (close_stream_check(stream)) {
1047 int delret;
1048
1049 close(stream->fd);
1050 delret = lttng_ht_del(streams_ht, &iter);
1051 assert(!delret);
1052 call_rcu(&stream->rcu_node,
1053 deferred_free_stream);
1054 DBG("Closed tracefile %d from close stream", stream->fd);
1055 }
1056
1057end_unlock:
1058 rcu_read_unlock();
1059
1060 if (ret < 0) {
f73fabfd 1061 reply.ret_code = htobe32(LTTNG_ERR_UNK);
173af62f 1062 } else {
f73fabfd 1063 reply.ret_code = htobe32(LTTNG_OK);
173af62f
DG
1064 }
1065 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1066 sizeof(struct lttcomm_relayd_generic_reply), 0);
1067 if (send_ret < 0) {
1068 ERR("Relay sending stream id");
1069 }
1070
1071end_no_session:
1072 return ret;
1073}
1074
b8aa1682
JD
1075/*
1076 * relay_unknown_command: send -1 if received unknown command
1077 */
1078static
1079void relay_unknown_command(struct relay_command *cmd)
1080{
1081 struct lttcomm_relayd_generic_reply reply;
1082 int ret;
1083
f73fabfd 1084 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1085 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1086 sizeof(struct lttcomm_relayd_generic_reply), 0);
1087 if (ret < 0) {
1088 ERR("Relay sending unknown command");
1089 }
1090}
1091
1092/*
1093 * relay_start: send an acknowledgment to the client to tell if we are
1094 * ready to receive data. We are ready if a session is established.
1095 */
1096static
1097int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
1098 struct relay_command *cmd)
1099{
f73fabfd 1100 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1101 struct lttcomm_relayd_generic_reply reply;
1102 struct relay_session *session = cmd->session;
1103
1104 if (!session) {
1105 DBG("Trying to start the streaming without a session established");
f73fabfd 1106 ret = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1107 }
1108
1109 reply.ret_code = ret;
1110 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1111 sizeof(struct lttcomm_relayd_generic_reply), 0);
1112 if (ret < 0) {
1113 ERR("Relay sending start ack");
1114 }
1115
1116 return ret;
1117}
1118
1119/*
1120 * Get stream from stream id.
9d1bbf21 1121 * Need to be called with RCU read-side lock held.
b8aa1682
JD
1122 */
1123static
1124struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id,
1125 struct lttng_ht *streams_ht)
1126{
1127 struct lttng_ht_node_ulong *node;
1128 struct lttng_ht_iter iter;
1129 struct relay_stream *ret;
1130
1131 lttng_ht_lookup(streams_ht,
1132 (void *)((unsigned long) stream_id),
1133 &iter);
1134 node = lttng_ht_iter_get_node_ulong(&iter);
1135 if (node == NULL) {
77c7c900 1136 DBG("Relay stream %" PRIu64 " not found", stream_id);
b8aa1682
JD
1137 ret = NULL;
1138 goto end;
1139 }
1140
1141 ret = caa_container_of(node, struct relay_stream, stream_n);
1142
1143end:
1144 return ret;
1145}
1146
1d4dfdef
DG
1147/*
1148 * Append padding to the file pointed by the file descriptor fd.
1149 */
1150static int write_padding_to_file(int fd, uint32_t size)
1151{
1152 int ret = 0;
1153 char *zeros;
1154
1155 if (size == 0) {
1156 goto end;
1157 }
1158
1159 zeros = zmalloc(size);
1160 if (zeros == NULL) {
1161 PERROR("zmalloc zeros for padding");
1162 ret = -1;
1163 goto end;
1164 }
1165
1166 do {
1167 ret = write(fd, zeros, size);
1168 } while (ret < 0 && errno == EINTR);
1169 if (ret < 0) {
1170 PERROR("write padding to file");
1171 }
1172
1173end:
1174 return ret;
1175}
1176
b8aa1682
JD
1177/*
1178 * relay_recv_metadata: receive the metada for the session.
1179 */
1180static
1181int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
1182 struct relay_command *cmd, struct lttng_ht *streams_ht)
1183{
f73fabfd 1184 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1185 struct relay_session *session = cmd->session;
1186 struct lttcomm_relayd_metadata_payload *metadata_struct;
1187 struct relay_stream *metadata_stream;
1188 uint64_t data_size, payload_size;
1189
1190 if (!session) {
1191 ERR("Metadata sent before version check");
1192 ret = -1;
1193 goto end;
1194 }
1195
f6416125
MD
1196 data_size = payload_size = be64toh(recv_hdr->data_size);
1197 if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1198 ERR("Incorrect data size");
1199 ret = -1;
1200 goto end;
1201 }
1202 payload_size -= sizeof(struct lttcomm_relayd_metadata_payload);
1203
b8aa1682
JD
1204 if (data_buffer_size < data_size) {
1205 data_buffer = realloc(data_buffer, data_size);
1206 if (!data_buffer) {
1207 ERR("Allocating data buffer");
1208 ret = -1;
1209 goto end;
1210 }
1211 data_buffer_size = data_size;
1212 }
1213 memset(data_buffer, 0, data_size);
77c7c900 1214 DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
87c1611d
DG
1215 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size,
1216 MSG_WAITALL);
b8aa1682
JD
1217 if (ret < 0 || ret != data_size) {
1218 ret = -1;
1219 ERR("Relay didn't receive the whole metadata");
1220 goto end;
1221 }
1222 metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
9d1bbf21
MD
1223
1224 rcu_read_lock();
b8aa1682
JD
1225 metadata_stream = relay_stream_from_stream_id(
1226 be64toh(metadata_struct->stream_id), streams_ht);
1227 if (!metadata_stream) {
1228 ret = -1;
9d1bbf21 1229 goto end_unlock;
b8aa1682
JD
1230 }
1231
6f94560a
MD
1232 do {
1233 ret = write(metadata_stream->fd, metadata_struct->payload,
1234 payload_size);
1235 } while (ret < 0 && errno == EINTR);
87c1611d 1236 if (ret < payload_size) {
b8aa1682
JD
1237 ERR("Relay error writing metadata on file");
1238 ret = -1;
9d1bbf21 1239 goto end_unlock;
b8aa1682 1240 }
1d4dfdef
DG
1241
1242 ret = write_padding_to_file(metadata_stream->fd,
1243 be32toh(metadata_struct->padding_size));
1244 if (ret < 0) {
1245 goto end_unlock;
1246 }
1247
b8aa1682
JD
1248 DBG2("Relay metadata written");
1249
9d1bbf21 1250end_unlock:
6e3c5836 1251 rcu_read_unlock();
b8aa1682
JD
1252end:
1253 return ret;
1254}
1255
1256/*
1257 * relay_send_version: send relayd version number
1258 */
1259static
1260int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
1261 struct relay_command *cmd)
1262{
f73fabfd 1263 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1264 struct lttcomm_relayd_version reply;
1265 struct relay_session *session = NULL;
1266
1267 if (cmd->session == NULL) {
1268 session = zmalloc(sizeof(struct relay_session));
1269 if (session == NULL) {
1270 PERROR("relay session zmalloc");
1271 ret = -1;
1272 goto end;
1273 }
1274 session->id = ++last_relay_session_id;
77c7c900 1275 DBG("Created session %" PRIu64, session->id);
b8aa1682
JD
1276 cmd->session = session;
1277 }
1278 session->version_check_done = 1;
1279
1280 sscanf(VERSION, "%u.%u", &reply.major, &reply.minor);
1281 reply.major = htobe32(reply.major);
1282 reply.minor = htobe32(reply.minor);
1283 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1284 sizeof(struct lttcomm_relayd_version), 0);
1285 if (ret < 0) {
1286 ERR("Relay sending version");
1287 }
0a6b5085
DG
1288 DBG("Version check done (%u.%u)", be32toh(reply.major),
1289 be32toh(reply.minor));
b8aa1682
JD
1290
1291end:
1292 return ret;
1293}
1294
1295/*
1296 * relay_process_control: Process the commands received on the control socket
1297 */
1298static
1299int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
1300 struct relay_command *cmd, struct lttng_ht *streams_ht)
1301{
1302 int ret = 0;
1303
1304 switch (be32toh(recv_hdr->cmd)) {
1305 /*
1306 case RELAYD_CREATE_SESSION:
1307 ret = relay_create_session(recv_hdr, cmd);
1308 break;
1309 */
1310 case RELAYD_ADD_STREAM:
1311 ret = relay_add_stream(recv_hdr, cmd, streams_ht);
1312 break;
1313 case RELAYD_START_DATA:
1314 ret = relay_start(recv_hdr, cmd);
1315 break;
1316 case RELAYD_SEND_METADATA:
1317 ret = relay_recv_metadata(recv_hdr, cmd, streams_ht);
1318 break;
1319 case RELAYD_VERSION:
1320 ret = relay_send_version(recv_hdr, cmd);
1321 break;
173af62f
DG
1322 case RELAYD_CLOSE_STREAM:
1323 ret = relay_close_stream(recv_hdr, cmd, streams_ht);
1324 break;
b8aa1682
JD
1325 case RELAYD_UPDATE_SYNC_INFO:
1326 default:
1327 ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
1328 relay_unknown_command(cmd);
1329 ret = -1;
1330 goto end;
1331 }
1332
1333end:
1334 return ret;
1335}
1336
1337/*
1338 * relay_process_data: Process the data received on the data socket
1339 */
1340static
1341int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
1342{
1343 int ret = 0;
1344 struct relay_stream *stream;
1345 struct lttcomm_relayd_data_hdr data_hdr;
1346 uint64_t stream_id;
173af62f 1347 uint64_t net_seq_num;
b8aa1682
JD
1348 uint32_t data_size;
1349
1350 ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr,
1351 sizeof(struct lttcomm_relayd_data_hdr), MSG_WAITALL);
1352 if (ret <= 0) {
1353 ERR("Connections seems to be closed");
1354 ret = -1;
1355 goto end;
1356 }
1357
1358 stream_id = be64toh(data_hdr.stream_id);
9d1bbf21
MD
1359
1360 rcu_read_lock();
b8aa1682
JD
1361 stream = relay_stream_from_stream_id(stream_id, streams_ht);
1362 if (!stream) {
1363 ret = -1;
9d1bbf21 1364 goto end_unlock;
b8aa1682
JD
1365 }
1366
1367 data_size = be32toh(data_hdr.data_size);
1368 if (data_buffer_size < data_size) {
1369 data_buffer = realloc(data_buffer, data_size);
1370 if (!data_buffer) {
1371 ERR("Allocating data buffer");
1372 ret = -1;
9d1bbf21 1373 goto end_unlock;
b8aa1682
JD
1374 }
1375 data_buffer_size = data_size;
1376 }
1377 memset(data_buffer, 0, data_size);
1378
173af62f
DG
1379 net_seq_num = be64toh(data_hdr.net_seq_num);
1380
77c7c900 1381 DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64,
173af62f 1382 data_size, stream_id, net_seq_num);
b8aa1682
JD
1383 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, MSG_WAITALL);
1384 if (ret <= 0) {
1385 ret = -1;
9d1bbf21 1386 goto end_unlock;
b8aa1682
JD
1387 }
1388
6f94560a
MD
1389 do {
1390 ret = write(stream->fd, data_buffer, data_size);
1391 } while (ret < 0 && errno == EINTR);
b8aa1682
JD
1392 if (ret < data_size) {
1393 ERR("Relay error writing data to file");
1394 ret = -1;
9d1bbf21 1395 goto end_unlock;
b8aa1682 1396 }
1d4dfdef
DG
1397
1398 ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size));
1399 if (ret < 0) {
1400 goto end_unlock;
1401 }
1402
77c7c900
MD
1403 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64,
1404 ret, stream->stream_handle);
b8aa1682 1405
173af62f
DG
1406 stream->prev_seq = net_seq_num;
1407
1408 /* Check if we need to close the FD */
1409 if (close_stream_check(stream)) {
1410 struct lttng_ht_iter iter;
1411
1412 close(stream->fd);
1413 iter.iter.node = &stream->stream_n.node;
1414 ret = lttng_ht_del(streams_ht, &iter);
1415 assert(!ret);
1416 call_rcu(&stream->rcu_node,
1417 deferred_free_stream);
1418 DBG("Closed tracefile %d after recv data", stream->fd);
1419 }
1420
9d1bbf21
MD
1421end_unlock:
1422 rcu_read_unlock();
b8aa1682
JD
1423end:
1424 return ret;
1425}
1426
1427static
9d1bbf21 1428void relay_cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
b8aa1682
JD
1429{
1430 int ret;
1431
b8aa1682
JD
1432 lttng_poll_del(events, pollfd);
1433
1434 ret = close(pollfd);
1435 if (ret < 0) {
1436 ERR("Closing pollfd %d", pollfd);
1437 }
1438}
1439
1440static
1441int relay_add_connection(int fd, struct lttng_poll_event *events,
1442 struct lttng_ht *relay_connections_ht)
1443{
b8aa1682 1444 struct relay_command *relay_connection;
9d1bbf21 1445 int ret;
b8aa1682
JD
1446
1447 relay_connection = zmalloc(sizeof(struct relay_command));
1448 if (relay_connection == NULL) {
1449 PERROR("Relay command zmalloc");
9d1bbf21 1450 goto error;
b8aa1682
JD
1451 }
1452 ret = read(fd, relay_connection, sizeof(struct relay_command));
cdf0f8fc 1453 if (ret < 0 || ret < sizeof(struct relay_command)) {
b8aa1682 1454 PERROR("read relay cmd pipe");
9d1bbf21 1455 goto error_read;
b8aa1682
JD
1456 }
1457
1458 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1459 (unsigned long) relay_connection->sock->fd);
9d1bbf21 1460 rcu_read_lock();
b8aa1682
JD
1461 lttng_ht_add_unique_ulong(relay_connections_ht,
1462 &relay_connection->sock_n);
9d1bbf21
MD
1463 rcu_read_unlock();
1464 return lttng_poll_add(events,
b8aa1682
JD
1465 relay_connection->sock->fd,
1466 LPOLLIN | LPOLLRDHUP);
1467
9d1bbf21
MD
1468error_read:
1469 free(relay_connection);
1470error:
1471 return -1;
1472}
1473
1474static
1475void deferred_free_connection(struct rcu_head *head)
1476{
1477 struct relay_command *relay_connection =
1478 caa_container_of(head, struct relay_command, rcu_node);
1479 free(relay_connection);
1480}
1481
1482static
1483void relay_del_connection(struct lttng_ht *relay_connections_ht,
1484 struct lttng_ht *streams_ht, struct lttng_ht_iter *iter,
1485 struct relay_command *relay_connection)
1486{
1487 int ret;
1488
1489 ret = lttng_ht_del(relay_connections_ht, iter);
1490 assert(!ret);
1491 if (relay_connection->type == RELAY_CONTROL) {
1492 relay_delete_session(relay_connection, streams_ht);
1493 }
1494 call_rcu(&relay_connection->rcu_node,
1495 deferred_free_connection);
b8aa1682
JD
1496}
1497
1498/*
1499 * This thread does the actual work
1500 */
1501static
1502void *relay_thread_worker(void *data)
1503{
095a4ae5 1504 int i, ret, pollfd, err = -1;
b8aa1682
JD
1505 uint32_t revents, nb_fd;
1506 struct relay_command *relay_connection;
1507 struct lttng_poll_event events;
1508 struct lttng_ht *relay_connections_ht;
1509 struct lttng_ht_node_ulong *node;
1510 struct lttng_ht_iter iter;
1511 struct lttng_ht *streams_ht;
1512 struct lttcomm_relayd_hdr recv_hdr;
1513
1514 DBG("[thread] Relay worker started");
1515
9d1bbf21
MD
1516 rcu_register_thread();
1517
b8aa1682
JD
1518 /* table of connections indexed on socket */
1519 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
1520 if (!relay_connections_ht) {
1521 goto relay_connections_ht_error;
1522 }
b8aa1682
JD
1523
1524 /* tables of streams indexed by stream ID */
1525 streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
1526 if (!streams_ht) {
1527 goto streams_ht_error;
1528 }
b8aa1682
JD
1529
1530 ret = create_thread_poll_set(&events, 2);
1531 if (ret < 0) {
1532 goto error_poll_create;
1533 }
1534
1535 ret = lttng_poll_add(&events, relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1536 if (ret < 0) {
1537 goto error;
1538 }
1539
1540 while (1) {
1541 /* Zeroed the events structure */
1542 lttng_poll_reset(&events);
1543
1544 nb_fd = LTTNG_POLL_GETNB(&events);
1545
1546 /* Infinite blocking call, waiting for transmission */
1547 restart:
87c1611d 1548 DBG3("Relayd worker thread polling...");
b8aa1682
JD
1549 ret = lttng_poll_wait(&events, -1);
1550 if (ret < 0) {
1551 /*
1552 * Restart interrupted system call.
1553 */
1554 if (errno == EINTR) {
1555 goto restart;
1556 }
1557 goto error;
1558 }
1559
1560 for (i = 0; i < nb_fd; i++) {
1561 /* Fetch once the poll data */
1562 revents = LTTNG_POLL_GETEV(&events, i);
1563 pollfd = LTTNG_POLL_GETFD(&events, i);
1564
1565 /* Thread quit pipe has been closed. Killing thread. */
1566 ret = check_thread_quit_pipe(pollfd, revents);
1567 if (ret) {
095a4ae5
MD
1568 err = 0;
1569 goto exit;
b8aa1682
JD
1570 }
1571
1572 /* Inspect the relay cmd pipe for new connection */
1573 if (pollfd == relay_cmd_pipe[0]) {
1574 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1575 ERR("Relay pipe error");
1576 goto error;
1577 } else if (revents & LPOLLIN) {
1578 DBG("Relay command received");
1579 ret = relay_add_connection(relay_cmd_pipe[0],
1580 &events, relay_connections_ht);
1581 if (ret < 0) {
1582 goto error;
1583 }
1584 }
1585 } else if (revents > 0) {
9d1bbf21 1586 rcu_read_lock();
b8aa1682
JD
1587 lttng_ht_lookup(relay_connections_ht,
1588 (void *)((unsigned long) pollfd),
1589 &iter);
1590 node = lttng_ht_iter_get_node_ulong(&iter);
1591 if (node == NULL) {
1592 DBG2("Relay sock %d not found", pollfd);
9d1bbf21 1593 rcu_read_unlock();
b8aa1682
JD
1594 goto error;
1595 }
1596 relay_connection = caa_container_of(node,
1597 struct relay_command, sock_n);
1598
1599 if (revents & (LPOLLERR)) {
1600 ERR("POLL ERROR");
9d1bbf21
MD
1601 relay_cleanup_poll_connection(&events, pollfd);
1602 relay_del_connection(relay_connections_ht,
1603 streams_ht, &iter,
1604 relay_connection);
b8aa1682
JD
1605 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1606 DBG("Socket %d hung up", pollfd);
9d1bbf21
MD
1607 relay_cleanup_poll_connection(&events, pollfd);
1608 relay_del_connection(relay_connections_ht,
1609 streams_ht, &iter,
1610 relay_connection);
b8aa1682
JD
1611 } else if (revents & LPOLLIN) {
1612 /* control socket */
1613 if (relay_connection->type == RELAY_CONTROL) {
1614 ret = relay_connection->sock->ops->recvmsg(
1615 relay_connection->sock, &recv_hdr,
1616 sizeof(struct lttcomm_relayd_hdr), MSG_WAITALL);
1617 /* connection closed */
1618 if (ret <= 0) {
9d1bbf21
MD
1619 relay_cleanup_poll_connection(&events, pollfd);
1620 relay_del_connection(relay_connections_ht,
1621 streams_ht, &iter,
1622 relay_connection);
b8aa1682
JD
1623 DBG("Control connection closed with %d", pollfd);
1624 } else {
1625 if (relay_connection->session) {
77c7c900 1626 DBG2("Relay worker receiving data for session : %" PRIu64,
b8aa1682
JD
1627 relay_connection->session->id);
1628 }
1629 ret = relay_process_control(&recv_hdr,
1630 relay_connection,
1631 streams_ht);
1632 /*
1633 * there was an error in processing a control
1634 * command: clear the session
1635 * */
1636 if (ret < 0) {
9d1bbf21
MD
1637 relay_cleanup_poll_connection(&events, pollfd);
1638 relay_del_connection(relay_connections_ht,
1639 streams_ht, &iter,
1640 relay_connection);
b8aa1682
JD
1641 DBG("Connection closed with %d", pollfd);
1642 }
1643 }
1644 /* data socket */
1645 } else if (relay_connection->type == RELAY_DATA) {
1646 ret = relay_process_data(relay_connection, streams_ht);
1647 /* connection closed */
1648 if (ret < 0) {
9d1bbf21
MD
1649 relay_cleanup_poll_connection(&events, pollfd);
1650 relay_del_connection(relay_connections_ht,
1651 streams_ht, &iter,
1652 relay_connection);
b8aa1682
JD
1653 DBG("Data connection closed with %d", pollfd);
1654 }
1655 }
1656 }
9d1bbf21 1657 rcu_read_unlock();
b8aa1682
JD
1658 }
1659 }
1660 }
1661
095a4ae5 1662exit:
b8aa1682
JD
1663error:
1664 lttng_poll_clean(&events);
1665
1666 /* empty the hash table and free the memory */
9d1bbf21 1667 rcu_read_lock();
b8aa1682
JD
1668 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
1669 node = lttng_ht_iter_get_node_ulong(&iter);
1670 if (node) {
1671 relay_connection = caa_container_of(node,
1672 struct relay_command, sock_n);
9d1bbf21
MD
1673 relay_del_connection(relay_connections_ht,
1674 streams_ht, &iter,
1675 relay_connection);
b8aa1682 1676 }
b8aa1682 1677 }
9d1bbf21 1678 rcu_read_unlock();
b8aa1682 1679error_poll_create:
095a4ae5
MD
1680 lttng_ht_destroy(streams_ht);
1681streams_ht_error:
b8aa1682 1682 lttng_ht_destroy(relay_connections_ht);
095a4ae5
MD
1683relay_connections_ht_error:
1684 if (err) {
1685 DBG("Thread exited with error");
1686 }
b8aa1682 1687 DBG("Worker thread cleanup complete");
095a4ae5 1688 free(data_buffer);
b8aa1682 1689 stop_threads();
9d1bbf21 1690 rcu_unregister_thread();
b8aa1682
JD
1691 return NULL;
1692}
1693
1694/*
1695 * Create the relay command pipe to wake thread_manage_apps.
1696 * Closed in cleanup().
1697 */
1698static int create_relay_cmd_pipe(void)
1699{
a02de639 1700 int ret;
b8aa1682 1701
a02de639 1702 ret = utils_create_pipe_cloexec(relay_cmd_pipe);
b8aa1682 1703
b8aa1682
JD
1704 return ret;
1705}
1706
1707/*
1708 * main
1709 */
1710int main(int argc, char **argv)
1711{
1712 int ret = 0;
1713 void *status;
1714
1715 /* Create thread quit pipe */
1716 if ((ret = init_thread_quit_pipe()) < 0) {
1717 goto error;
1718 }
1719
1720 /* Parse arguments */
1721 progname = argv[0];
1722 if ((ret = parse_args(argc, argv) < 0)) {
a02de639 1723 goto exit;
b8aa1682
JD
1724 }
1725
1726 if ((ret = set_signal_handler()) < 0) {
1727 goto exit;
1728 }
1729
1730 /* Daemonize */
1731 if (opt_daemon) {
1732 ret = daemon(0, 0);
1733 if (ret < 0) {
1734 PERROR("daemon");
a02de639 1735 goto exit;
b8aa1682
JD
1736 }
1737 }
1738
1739 /* Check if daemon is UID = 0 */
1740 is_root = !getuid();
1741
1742 if (!is_root) {
1743 if (control_uri->port < 1024 || data_uri->port < 1024) {
1744 ERR("Need to be root to use ports < 1024");
1745 ret = -1;
a02de639 1746 goto exit;
b8aa1682
JD
1747 }
1748 }
1749
1750 /* Setup the thread apps communication pipe. */
1751 if ((ret = create_relay_cmd_pipe()) < 0) {
1752 goto exit;
1753 }
1754
1755 /* Init relay command queue. */
1756 cds_wfq_init(&relay_cmd_queue.queue);
1757
1758 /* Set up max poll set size */
1759 lttng_poll_set_max_size();
1760
1761 /* Setup the dispatcher thread */
1762 ret = pthread_create(&dispatcher_thread, NULL,
1763 relay_thread_dispatcher, (void *) NULL);
1764 if (ret != 0) {
1765 PERROR("pthread_create dispatcher");
1766 goto exit_dispatcher;
1767 }
1768
1769 /* Setup the worker thread */
1770 ret = pthread_create(&worker_thread, NULL,
1771 relay_thread_worker, (void *) NULL);
1772 if (ret != 0) {
1773 PERROR("pthread_create worker");
1774 goto exit_worker;
1775 }
1776
1777 /* Setup the listener thread */
1778 ret = pthread_create(&listener_thread, NULL,
1779 relay_thread_listener, (void *) NULL);
1780 if (ret != 0) {
1781 PERROR("pthread_create listener");
1782 goto exit_listener;
1783 }
1784
1785exit_listener:
1786 ret = pthread_join(listener_thread, &status);
1787 if (ret != 0) {
1788 PERROR("pthread_join");
1789 goto error; /* join error, exit without cleanup */
1790 }
1791
1792exit_worker:
1793 ret = pthread_join(worker_thread, &status);
1794 if (ret != 0) {
1795 PERROR("pthread_join");
1796 goto error; /* join error, exit without cleanup */
1797 }
1798
1799exit_dispatcher:
1800 ret = pthread_join(dispatcher_thread, &status);
1801 if (ret != 0) {
1802 PERROR("pthread_join");
1803 goto error; /* join error, exit without cleanup */
1804 }
1805
1806exit:
1807 cleanup();
1808 if (!ret) {
1809 exit(EXIT_SUCCESS);
1810 }
a02de639 1811
b8aa1682
JD
1812error:
1813 exit(EXIT_FAILURE);
1814}
This page took 0.098275 seconds and 5 git commands to generate.