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