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