Fix: missing reset when listing UST fields for multiple PIDs
[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>
cd60b05a 4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
b8aa1682
JD
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#define _GNU_SOURCE
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
24#include <pthread.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/mman.h>
30#include <sys/mount.h>
31#include <sys/resource.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/wait.h>
173af62f 36#include <inttypes.h>
b8aa1682
JD
37#include <urcu/futex.h>
38#include <urcu/uatomic.h>
39#include <unistd.h>
40#include <fcntl.h>
41#include <config.h>
42
43#include <lttng/lttng.h>
44#include <common/common.h>
45#include <common/compat/poll.h>
46#include <common/compat/socket.h>
47#include <common/defaults.h>
48#include <common/futex.h>
49#include <common/sessiond-comm/sessiond-comm.h>
50#include <common/sessiond-comm/inet.h>
b8aa1682
JD
51#include <common/sessiond-comm/relayd.h>
52#include <common/uri.h>
a02de639 53#include <common/utils.h>
cd60b05a 54#include <common/config/config.h>
b8aa1682 55
0f907de1 56#include "cmd.h"
d3e2ba59 57#include "ctf-trace.h"
1c20f0e2 58#include "index.h"
0f907de1 59#include "utils.h"
b8aa1682 60#include "lttng-relayd.h"
d3e2ba59 61#include "live.h"
55706a7d 62#include "health-relayd.h"
b8aa1682
JD
63
64/* command line options */
0f907de1 65char *opt_output_path;
b8aa1682 66static int opt_daemon;
095a4ae5
MD
67static struct lttng_uri *control_uri;
68static struct lttng_uri *data_uri;
d3e2ba59 69static struct lttng_uri *live_uri;
b8aa1682
JD
70
71const char *progname;
b8aa1682 72
65931c8b 73const char *tracing_group_name = DEFAULT_TRACING_GROUP;
cd60b05a
JG
74static int tracing_group_name_override;
75
76const char * const config_section_name = "relayd";
65931c8b 77
b8aa1682
JD
78/*
79 * Quit pipe for all threads. This permits a single cancellation point
80 * for all threads when receiving an event on the pipe.
81 */
82static int thread_quit_pipe[2] = { -1, -1 };
83
84/*
85 * This pipe is used to inform the worker thread that a command is queued and
86 * ready to be processed.
87 */
88static int relay_cmd_pipe[2] = { -1, -1 };
89
26c9d55e 90/* Shared between threads */
b8aa1682
JD
91static int dispatch_thread_exit;
92
93static pthread_t listener_thread;
94static pthread_t dispatcher_thread;
95static pthread_t worker_thread;
65931c8b 96static pthread_t health_thread;
b8aa1682 97
095a4ae5
MD
98static uint64_t last_relay_stream_id;
99static uint64_t last_relay_session_id;
b8aa1682
JD
100
101/*
102 * Relay command queue.
103 *
104 * The relay_thread_listener and relay_thread_dispatcher communicate with this
105 * queue.
106 */
107static struct relay_cmd_queue relay_cmd_queue;
108
109/* buffer allocated at startup, used to store the trace data */
095a4ae5
MD
110static char *data_buffer;
111static unsigned int data_buffer_size;
b8aa1682 112
1c20f0e2
JD
113/* We need those values for the file/dir creation. */
114static uid_t relayd_uid;
115static gid_t relayd_gid;
116
d3e2ba59
JD
117/* Global relay stream hash table. */
118struct lttng_ht *relay_streams_ht;
119
92c6ca54
DG
120/* Global relay viewer stream hash table. */
121struct lttng_ht *viewer_streams_ht;
122
0a6518b0
DG
123/* Global hash table that stores relay index object. */
124struct lttng_ht *indexes_ht;
125
55706a7d 126/* Relayd health monitoring */
eea7556c 127struct health_app *health_relayd;
55706a7d 128
cd60b05a
JG
129static struct option long_options[] = {
130 { "control-port", 1, 0, 'C', },
131 { "data-port", 1, 0, 'D', },
8d5c808e 132 { "live-port", 1, 0, 'L', },
cd60b05a
JG
133 { "daemonize", 0, 0, 'd', },
134 { "group", 1, 0, 'g', },
135 { "help", 0, 0, 'h', },
136 { "output", 1, 0, 'o', },
137 { "verbose", 0, 0, 'v', },
138 { "config", 1, 0, 'f' },
139 { NULL, 0, 0, 0, },
140};
141
142static const char *config_ignore_options[] = { "help", "config" };
143
b8aa1682
JD
144/*
145 * usage function on stderr
146 */
147static
148void usage(void)
149{
150 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
994fa64f
DG
151 fprintf(stderr, " -h, --help Display this usage.\n");
152 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
153 fprintf(stderr, " -C, --control-port URL Control port listening.\n");
154 fprintf(stderr, " -D, --data-port URL Data port listening.\n");
8d5c808e 155 fprintf(stderr, " -L, --live-port URL Live view port listening.\n");
994fa64f
DG
156 fprintf(stderr, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
157 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
65931c8b 158 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
cd60b05a 159 fprintf(stderr, " -f --config Load daemon configuration file\n");
b8aa1682
JD
160}
161
cd60b05a
JG
162/*
163 * Take an option from the getopt output and set it in the right variable to be
164 * used later.
165 *
166 * Return 0 on success else a negative value.
167 */
b8aa1682 168static
cd60b05a 169int set_option(int opt, const char *arg, const char *optname)
b8aa1682 170{
cd60b05a
JG
171 int ret;
172
173 switch (opt) {
174 case 0:
175 fprintf(stderr, "option %s", optname);
176 if (arg) {
177 fprintf(stderr, " with arg %s\n", arg);
178 }
179 break;
180 case 'C':
181 ret = uri_parse(arg, &control_uri);
182 if (ret < 0) {
183 ERR("Invalid control URI specified");
184 goto end;
185 }
186 if (control_uri->port == 0) {
187 control_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
188 }
189 break;
190 case 'D':
191 ret = uri_parse(arg, &data_uri);
192 if (ret < 0) {
193 ERR("Invalid data URI specified");
194 goto end;
195 }
196 if (data_uri->port == 0) {
197 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
198 }
199 break;
8d5c808e
AM
200 case 'L':
201 ret = uri_parse(arg, &live_uri);
202 if (ret < 0) {
203 ERR("Invalid live URI specified");
204 goto end;
205 }
206 if (live_uri->port == 0) {
207 live_uri->port = DEFAULT_NETWORK_VIEWER_PORT;
208 }
209 break;
cd60b05a
JG
210 case 'd':
211 opt_daemon = 1;
212 break;
213 case 'g':
214 tracing_group_name = strdup(arg);
215 tracing_group_name_override = 1;
216 break;
217 case 'h':
218 usage();
219 exit(EXIT_FAILURE);
220 case 'o':
221 ret = asprintf(&opt_output_path, "%s", arg);
222 if (ret < 0) {
223 ret = -errno;
224 PERROR("asprintf opt_output_path");
225 goto end;
226 }
227 break;
228 case 'v':
229 /* Verbose level can increase using multiple -v */
230 if (arg) {
231 lttng_opt_verbose = config_parse_value(arg);
232 } else {
233 lttng_opt_verbose += 1;
234 }
235 break;
236 default:
237 /* Unknown option or other error.
238 * Error is printed by getopt, just return */
239 ret = -1;
240 goto end;
241 }
242
243 /* All good. */
244 ret = 0;
245
246end:
247 return ret;
248}
249
250/*
251 * config_entry_handler_cb used to handle options read from a config file.
252 * See config_entry_handler_cb comment in common/config/config.h for the
253 * return value conventions.
254 */
255static
256int config_entry_handler(const struct config_entry *entry, void *unused)
257{
258 int ret = 0, i;
259
260 if (!entry || !entry->name || !entry->value) {
261 ret = -EINVAL;
262 goto end;
263 }
264
265 /* Check if the option is to be ignored */
266 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
267 if (!strcmp(entry->name, config_ignore_options[i])) {
268 goto end;
269 }
270 }
271
272 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) {
273 /* Ignore if entry name is not fully matched. */
274 if (strcmp(entry->name, long_options[i].name)) {
275 continue;
276 }
277
278 /*
279 * If the option takes no argument on the command line, we have to
280 * check if the value is "true". We support non-zero numeric values,
281 * true, on and yes.
282 */
283 if (!long_options[i].has_arg) {
284 ret = config_parse_value(entry->value);
285 if (ret <= 0) {
286 if (ret) {
287 WARN("Invalid configuration value \"%s\" for option %s",
288 entry->value, entry->name);
289 }
290 /* False, skip boolean config option. */
291 goto end;
292 }
293 }
294
295 ret = set_option(long_options[i].val, entry->value, entry->name);
296 goto end;
297 }
298
299 WARN("Unrecognized option \"%s\" in daemon configuration file.",
300 entry->name);
301
302end:
303 return ret;
304}
305
306static
307int set_options(int argc, char **argv)
308{
309 int c, ret = 0, option_index = 0;
310 int orig_optopt = optopt, orig_optind = optind;
311 char *default_address, *optstring;
312 const char *config_path = NULL;
313
314 optstring = utils_generate_optstring(long_options,
315 sizeof(long_options) / sizeof(struct option));
316 if (!optstring) {
317 ret = -ENOMEM;
318 goto exit;
319 }
320
321 /* Check for the --config option */
322
323 while ((c = getopt_long(argc, argv, optstring, long_options,
324 &option_index)) != -1) {
325 if (c == '?') {
326 ret = -EINVAL;
327 goto exit;
328 } else if (c != 'f') {
329 continue;
330 }
331
332 config_path = utils_expand_path(optarg);
333 if (!config_path) {
334 ERR("Failed to resolve path: %s", optarg);
335 }
336 }
337
338 ret = config_get_section_entries(config_path, config_section_name,
339 config_entry_handler, NULL);
340 if (ret) {
341 if (ret > 0) {
342 ERR("Invalid configuration option at line %i", ret);
343 ret = -1;
344 }
345 goto exit;
346 }
b8aa1682 347
cd60b05a
JG
348 /* Reset getopt's global state */
349 optopt = orig_optopt;
350 optind = orig_optind;
b8aa1682 351 while (1) {
cd60b05a 352 c = getopt_long(argc, argv, optstring, long_options, &option_index);
b8aa1682
JD
353 if (c == -1) {
354 break;
355 }
356
cd60b05a
JG
357 ret = set_option(c, optarg, long_options[option_index].name);
358 if (ret < 0) {
b8aa1682
JD
359 goto exit;
360 }
361 }
362
363 /* assign default values */
364 if (control_uri == NULL) {
365 ret = asprintf(&default_address, "tcp://0.0.0.0:%d",
366 DEFAULT_NETWORK_CONTROL_PORT);
367 if (ret < 0) {
368 PERROR("asprintf default data address");
369 goto exit;
370 }
371
372 ret = uri_parse(default_address, &control_uri);
373 free(default_address);
374 if (ret < 0) {
375 ERR("Invalid control URI specified");
376 goto exit;
377 }
378 }
379 if (data_uri == NULL) {
380 ret = asprintf(&default_address, "tcp://0.0.0.0:%d",
381 DEFAULT_NETWORK_DATA_PORT);
382 if (ret < 0) {
383 PERROR("asprintf default data address");
384 goto exit;
385 }
386
387 ret = uri_parse(default_address, &data_uri);
388 free(default_address);
389 if (ret < 0) {
390 ERR("Invalid data URI specified");
391 goto exit;
392 }
393 }
d3e2ba59
JD
394 if (live_uri == NULL) {
395 ret = asprintf(&default_address, "tcp://0.0.0.0:%d",
396 DEFAULT_NETWORK_VIEWER_PORT);
397 if (ret < 0) {
398 PERROR("asprintf default viewer control address");
399 goto exit;
400 }
401
402 ret = uri_parse(default_address, &live_uri);
403 free(default_address);
404 if (ret < 0) {
405 ERR("Invalid viewer control URI specified");
406 goto exit;
407 }
408 }
b8aa1682
JD
409
410exit:
cd60b05a 411 free(optstring);
b8aa1682
JD
412 return ret;
413}
414
415/*
416 * Cleanup the daemon
417 */
418static
419void cleanup(void)
420{
b8aa1682
JD
421 DBG("Cleaning up");
422
095a4ae5
MD
423 /* free the dynamically allocated opt_output_path */
424 free(opt_output_path);
425
a02de639
CB
426 /* Close thread quit pipes */
427 utils_close_pipe(thread_quit_pipe);
428
710c1f73
DG
429 uri_free(control_uri);
430 uri_free(data_uri);
8d5c808e 431 /* Live URI is freed in the live thread. */
cd60b05a
JG
432
433 if (tracing_group_name_override) {
434 free((void *) tracing_group_name);
435 }
b8aa1682
JD
436}
437
438/*
439 * Write to writable pipe used to notify a thread.
440 */
441static
442int notify_thread_pipe(int wpipe)
443{
6cd525e8 444 ssize_t ret;
b8aa1682 445
6cd525e8
MD
446 ret = lttng_write(wpipe, "!", 1);
447 if (ret < 1) {
b8aa1682
JD
448 PERROR("write poll pipe");
449 }
450
451 return ret;
452}
453
65931c8b
MD
454static void notify_health_quit_pipe(int *pipe)
455{
6cd525e8 456 ssize_t ret;
65931c8b 457
6cd525e8
MD
458 ret = lttng_write(pipe[1], "4", 1);
459 if (ret < 1) {
65931c8b
MD
460 PERROR("write relay health quit");
461 }
462}
463
b8aa1682
JD
464/*
465 * Stop all threads by closing the thread quit pipe.
466 */
467static
468void stop_threads(void)
469{
470 int ret;
471
472 /* Stopping all threads */
473 DBG("Terminating all threads");
474 ret = notify_thread_pipe(thread_quit_pipe[1]);
475 if (ret < 0) {
476 ERR("write error on thread quit pipe");
477 }
478
65931c8b
MD
479 notify_health_quit_pipe(health_quit_pipe);
480
b8aa1682 481 /* Dispatch thread */
26c9d55e 482 CMM_STORE_SHARED(dispatch_thread_exit, 1);
b8aa1682
JD
483 futex_nto1_wake(&relay_cmd_queue.futex);
484}
485
486/*
487 * Signal handler for the daemon
488 *
489 * Simply stop all worker threads, leaving main() return gracefully after
490 * joining all threads and calling cleanup().
491 */
492static
493void sighandler(int sig)
494{
495 switch (sig) {
496 case SIGPIPE:
497 DBG("SIGPIPE caught");
498 return;
499 case SIGINT:
500 DBG("SIGINT caught");
501 stop_threads();
502 break;
503 case SIGTERM:
504 DBG("SIGTERM caught");
505 stop_threads();
506 break;
507 default:
508 break;
509 }
510}
511
512/*
513 * Setup signal handler for :
514 * SIGINT, SIGTERM, SIGPIPE
515 */
516static
517int set_signal_handler(void)
518{
519 int ret = 0;
520 struct sigaction sa;
521 sigset_t sigset;
522
523 if ((ret = sigemptyset(&sigset)) < 0) {
524 PERROR("sigemptyset");
525 return ret;
526 }
527
528 sa.sa_handler = sighandler;
529 sa.sa_mask = sigset;
530 sa.sa_flags = 0;
531 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
532 PERROR("sigaction");
533 return ret;
534 }
535
536 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
537 PERROR("sigaction");
538 return ret;
539 }
540
541 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
542 PERROR("sigaction");
543 return ret;
544 }
545
546 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
547
548 return ret;
549}
550
551/*
552 * Init thread quit pipe.
553 *
554 * Return -1 on error or 0 if all pipes are created.
555 */
556static
557int init_thread_quit_pipe(void)
558{
a02de639 559 int ret;
b8aa1682 560
a02de639 561 ret = utils_create_pipe_cloexec(thread_quit_pipe);
b8aa1682 562
b8aa1682
JD
563 return ret;
564}
565
566/*
567 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
568 */
569static
570int create_thread_poll_set(struct lttng_poll_event *events, int size)
571{
572 int ret;
573
574 if (events == NULL || size == 0) {
575 ret = -1;
576 goto error;
577 }
578
579 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
580 if (ret < 0) {
581 goto error;
582 }
583
584 /* Add quit pipe */
585 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
586 if (ret < 0) {
587 goto error;
588 }
589
590 return 0;
591
592error:
593 return ret;
594}
595
596/*
597 * Check if the thread quit pipe was triggered.
598 *
599 * Return 1 if it was triggered else 0;
600 */
601static
602int check_thread_quit_pipe(int fd, uint32_t events)
603{
604 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
605 return 1;
606 }
607
608 return 0;
609}
610
611/*
612 * Create and init socket from uri.
613 */
614static
615struct lttcomm_sock *relay_init_sock(struct lttng_uri *uri)
616{
617 int ret;
618 struct lttcomm_sock *sock = NULL;
619
620 sock = lttcomm_alloc_sock_from_uri(uri);
621 if (sock == NULL) {
622 ERR("Allocating socket");
623 goto error;
624 }
625
626 ret = lttcomm_create_sock(sock);
627 if (ret < 0) {
628 goto error;
629 }
630 DBG("Listening on sock %d", sock->fd);
631
632 ret = sock->ops->bind(sock);
633 if (ret < 0) {
634 goto error;
635 }
636
637 ret = sock->ops->listen(sock, -1);
638 if (ret < 0) {
639 goto error;
640
641 }
642
643 return sock;
644
645error:
646 if (sock) {
647 lttcomm_destroy_sock(sock);
648 }
649 return NULL;
650}
651
173af62f
DG
652/*
653 * Return nonzero if stream needs to be closed.
654 */
655static
656int close_stream_check(struct relay_stream *stream)
657{
658
659 if (stream->close_flag && stream->prev_seq == stream->last_net_seq_num) {
f7079f67
DG
660 /*
661 * We are about to close the stream so set the data pending flag to 1
662 * which will make the end data pending command skip the stream which
663 * is now closed and ready. Note that after proceeding to a file close,
664 * the written file is ready for reading.
665 */
666 stream->data_pending_check_done = 1;
173af62f
DG
667 return 1;
668 }
669 return 0;
670}
671
b8aa1682
JD
672/*
673 * This thread manages the listening for new connections on the network
674 */
675static
676void *relay_thread_listener(void *data)
677{
095a4ae5 678 int i, ret, pollfd, err = -1;
b8aa1682
JD
679 int val = 1;
680 uint32_t revents, nb_fd;
681 struct lttng_poll_event events;
682 struct lttcomm_sock *control_sock, *data_sock;
683
b8aa1682
JD
684 DBG("[thread] Relay listener started");
685
55706a7d
MD
686 health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER);
687
f385ae0a
MD
688 health_code_update();
689
b8aa1682
JD
690 control_sock = relay_init_sock(control_uri);
691 if (!control_sock) {
095a4ae5 692 goto error_sock_control;
b8aa1682
JD
693 }
694
695 data_sock = relay_init_sock(data_uri);
696 if (!data_sock) {
095a4ae5 697 goto error_sock_relay;
b8aa1682
JD
698 }
699
700 /*
701 * Pass 3 as size here for the thread quit pipe, control and data socket.
702 */
703 ret = create_thread_poll_set(&events, 3);
704 if (ret < 0) {
705 goto error_create_poll;
706 }
707
708 /* Add the control socket */
709 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
710 if (ret < 0) {
711 goto error_poll_add;
712 }
713
714 /* Add the data socket */
715 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
716 if (ret < 0) {
717 goto error_poll_add;
718 }
719
720 while (1) {
f385ae0a
MD
721 health_code_update();
722
b8aa1682
JD
723 DBG("Listener accepting connections");
724
b8aa1682 725restart:
f385ae0a 726 health_poll_entry();
b8aa1682 727 ret = lttng_poll_wait(&events, -1);
f385ae0a 728 health_poll_exit();
b8aa1682
JD
729 if (ret < 0) {
730 /*
731 * Restart interrupted system call.
732 */
733 if (errno == EINTR) {
734 goto restart;
735 }
736 goto error;
737 }
738
0d9c5d77
DG
739 nb_fd = ret;
740
b8aa1682
JD
741 DBG("Relay new connection received");
742 for (i = 0; i < nb_fd; i++) {
f385ae0a
MD
743 health_code_update();
744
b8aa1682
JD
745 /* Fetch once the poll data */
746 revents = LTTNG_POLL_GETEV(&events, i);
747 pollfd = LTTNG_POLL_GETFD(&events, i);
748
749 /* Thread quit pipe has been closed. Killing thread. */
750 ret = check_thread_quit_pipe(pollfd, revents);
751 if (ret) {
095a4ae5
MD
752 err = 0;
753 goto exit;
b8aa1682
JD
754 }
755
756 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
757 ERR("socket poll error");
758 goto error;
759 } else if (revents & LPOLLIN) {
4b7f17b2
MD
760 /*
761 * Get allocated in this thread,
762 * enqueued to a global queue, dequeued
763 * and freed in the worker thread.
764 */
765 struct relay_command *relay_cmd;
766 struct lttcomm_sock *newsock;
b8aa1682
JD
767
768 relay_cmd = zmalloc(sizeof(struct relay_command));
769 if (relay_cmd == NULL) {
770 PERROR("relay command zmalloc");
771 goto error;
772 }
773
774 if (pollfd == data_sock->fd) {
775 newsock = data_sock->ops->accept(data_sock);
4b7f17b2 776 if (!newsock) {
b8aa1682 777 PERROR("accepting data sock");
4b7f17b2 778 free(relay_cmd);
b8aa1682
JD
779 goto error;
780 }
781 relay_cmd->type = RELAY_DATA;
782 DBG("Relay data connection accepted, socket %d", newsock->fd);
4b7f17b2
MD
783 } else {
784 assert(pollfd == control_sock->fd);
b8aa1682 785 newsock = control_sock->ops->accept(control_sock);
4b7f17b2 786 if (!newsock) {
b8aa1682 787 PERROR("accepting control sock");
4b7f17b2 788 free(relay_cmd);
b8aa1682
JD
789 goto error;
790 }
791 relay_cmd->type = RELAY_CONTROL;
792 DBG("Relay control connection accepted, socket %d", newsock->fd);
793 }
794 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR,
795 &val, sizeof(int));
796 if (ret < 0) {
797 PERROR("setsockopt inet");
4b7f17b2
MD
798 lttcomm_destroy_sock(newsock);
799 free(relay_cmd);
b8aa1682
JD
800 goto error;
801 }
802 relay_cmd->sock = newsock;
803 /*
804 * Lock free enqueue the request.
805 */
806 cds_wfq_enqueue(&relay_cmd_queue.queue, &relay_cmd->node);
807
808 /*
809 * Wake the dispatch queue futex. Implicit memory
810 * barrier with the exchange in cds_wfq_enqueue.
811 */
812 futex_nto1_wake(&relay_cmd_queue.futex);
813 }
814 }
815 }
816
095a4ae5 817exit:
b8aa1682
JD
818error:
819error_poll_add:
820 lttng_poll_clean(&events);
821error_create_poll:
095a4ae5
MD
822 if (data_sock->fd >= 0) {
823 ret = data_sock->ops->close(data_sock);
b8aa1682
JD
824 if (ret) {
825 PERROR("close");
826 }
b8aa1682 827 }
095a4ae5
MD
828 lttcomm_destroy_sock(data_sock);
829error_sock_relay:
830 if (control_sock->fd >= 0) {
831 ret = control_sock->ops->close(control_sock);
b8aa1682
JD
832 if (ret) {
833 PERROR("close");
834 }
b8aa1682 835 }
095a4ae5
MD
836 lttcomm_destroy_sock(control_sock);
837error_sock_control:
838 if (err) {
f385ae0a
MD
839 health_error();
840 ERR("Health error occurred in %s", __func__);
095a4ae5 841 }
55706a7d 842 health_unregister(health_relayd);
b8aa1682
JD
843 DBG("Relay listener thread cleanup complete");
844 stop_threads();
b8aa1682
JD
845 return NULL;
846}
847
848/*
849 * This thread manages the dispatching of the requests to worker threads
850 */
851static
852void *relay_thread_dispatcher(void *data)
853{
6cd525e8
MD
854 int err = -1;
855 ssize_t ret;
b8aa1682
JD
856 struct cds_wfq_node *node;
857 struct relay_command *relay_cmd = NULL;
858
859 DBG("[thread] Relay dispatcher started");
860
55706a7d
MD
861 health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER);
862
f385ae0a
MD
863 health_code_update();
864
26c9d55e 865 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
f385ae0a
MD
866 health_code_update();
867
b8aa1682
JD
868 /* Atomically prepare the queue futex */
869 futex_nto1_prepare(&relay_cmd_queue.futex);
870
871 do {
f385ae0a
MD
872 health_code_update();
873
b8aa1682
JD
874 /* Dequeue commands */
875 node = cds_wfq_dequeue_blocking(&relay_cmd_queue.queue);
876 if (node == NULL) {
877 DBG("Woken up but nothing in the relay command queue");
878 /* Continue thread execution */
879 break;
880 }
881
882 relay_cmd = caa_container_of(node, struct relay_command, node);
883 DBG("Dispatching request waiting on sock %d", relay_cmd->sock->fd);
884
885 /*
886 * Inform worker thread of the new request. This
887 * call is blocking so we can be assured that the data will be read
888 * at some point in time or wait to the end of the world :)
889 */
6cd525e8
MD
890 ret = lttng_write(relay_cmd_pipe[1], relay_cmd,
891 sizeof(struct relay_command));
b8aa1682 892 free(relay_cmd);
6cd525e8 893 if (ret < sizeof(struct relay_command)) {
b8aa1682
JD
894 PERROR("write cmd pipe");
895 goto error;
896 }
897 } while (node != NULL);
898
899 /* Futex wait on queue. Blocking call on futex() */
f385ae0a 900 health_poll_entry();
b8aa1682 901 futex_nto1_wait(&relay_cmd_queue.futex);
f385ae0a 902 health_poll_exit();
b8aa1682
JD
903 }
904
f385ae0a
MD
905 /* Normal exit, no error */
906 err = 0;
907
b8aa1682 908error:
f385ae0a
MD
909 if (err) {
910 health_error();
911 ERR("Health error occurred in %s", __func__);
912 }
55706a7d 913 health_unregister(health_relayd);
b8aa1682
JD
914 DBG("Dispatch thread dying");
915 stop_threads();
916 return NULL;
917}
918
de91f48a
DG
919/*
920 * Get stream from stream id.
921 * Need to be called with RCU read-side lock held.
922 */
d3e2ba59 923struct relay_stream *relay_stream_find_by_id(uint64_t stream_id)
de91f48a
DG
924{
925 struct lttng_ht_node_ulong *node;
926 struct lttng_ht_iter iter;
927 struct relay_stream *ret;
928
d3e2ba59 929 lttng_ht_lookup(relay_streams_ht,
de91f48a
DG
930 (void *)((unsigned long) stream_id),
931 &iter);
932 node = lttng_ht_iter_get_node_ulong(&iter);
933 if (node == NULL) {
934 DBG("Relay stream %" PRIu64 " not found", stream_id);
935 ret = NULL;
936 goto end;
937 }
938
939 ret = caa_container_of(node, struct relay_stream, stream_n);
940
941end:
942 return ret;
943}
944
9d1bbf21
MD
945static
946void deferred_free_stream(struct rcu_head *head)
947{
948 struct relay_stream *stream =
949 caa_container_of(head, struct relay_stream, rcu_node);
d3e2ba59 950
0f907de1
JD
951 free(stream->path_name);
952 free(stream->channel_name);
9d1bbf21
MD
953 free(stream);
954}
955
d3e2ba59
JD
956static
957void deferred_free_session(struct rcu_head *head)
958{
959 struct relay_session *session =
960 caa_container_of(head, struct relay_session, rcu_node);
961 free(session);
962}
963
4c80d9a2
DG
964/*
965 * Close a given stream. The stream is freed using a call RCU.
966 *
967 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
968 * BEFORE the stream lock MUST be acquired.
969 */
c07d8a78 970static void destroy_stream(struct relay_stream *stream)
94d49140
JD
971{
972 int delret;
973 struct relay_viewer_stream *vstream;
974 struct lttng_ht_iter iter;
975
976 assert(stream);
94d49140
JD
977
978 delret = close(stream->fd);
979 if (delret < 0) {
980 PERROR("close stream");
981 }
982
983 if (stream->index_fd >= 0) {
984 delret = close(stream->index_fd);
985 if (delret < 0) {
986 PERROR("close stream index_fd");
987 }
988 }
989
92c6ca54 990 vstream = live_find_viewer_stream_by_id(stream->stream_handle);
94d49140
JD
991 if (vstream) {
992 /*
993 * Set the last good value into the viewer stream. This is done
994 * right before the stream gets deleted from the hash table. The
995 * lookup failure on the live thread side of a stream indicates
996 * that the viewer stream index received value should be used.
997 */
cef0f7d5 998 pthread_mutex_lock(&stream->viewer_stream_rotation_lock);
94d49140 999 vstream->total_index_received = stream->total_index_received;
a020f610 1000 vstream->tracefile_count_last = stream->tracefile_count_current;
cef0f7d5
JD
1001 vstream->close_write_flag = 1;
1002 pthread_mutex_unlock(&stream->viewer_stream_rotation_lock);
94d49140
JD
1003 }
1004
efc8a87f
DG
1005 /* Cleanup index of that stream. */
1006 relay_index_destroy_by_stream_id(stream->stream_handle);
1007
94d49140
JD
1008 iter.iter.node = &stream->stream_n.node;
1009 delret = lttng_ht_del(relay_streams_ht, &iter);
1010 assert(!delret);
1011 iter.iter.node = &stream->ctf_trace_node.node;
c07d8a78 1012 delret = lttng_ht_del(stream->ctf_traces_ht, &iter);
94d49140 1013 assert(!delret);
157df586
JD
1014
1015 if (stream->ctf_trace) {
1016 ctf_trace_try_destroy(stream->ctf_trace);
1017 }
1018
94d49140
JD
1019 call_rcu(&stream->rcu_node, deferred_free_stream);
1020 DBG("Closed tracefile %d from close stream", stream->fd);
1021}
1022
b8aa1682
JD
1023/*
1024 * relay_delete_session: Free all memory associated with a session and
1025 * close all the FDs
1026 */
1027static
d3e2ba59
JD
1028void relay_delete_session(struct relay_command *cmd,
1029 struct lttng_ht *sessions_ht)
b8aa1682
JD
1030{
1031 struct lttng_ht_iter iter;
1032 struct lttng_ht_node_ulong *node;
1033 struct relay_stream *stream;
1034 int ret;
1035
095a4ae5 1036 if (!cmd->session) {
b8aa1682 1037 return;
095a4ae5 1038 }
b8aa1682 1039
77c7c900 1040 DBG("Relay deleting session %" PRIu64, cmd->session->id);
5b6d8097 1041
9d1bbf21 1042 rcu_read_lock();
d3e2ba59 1043 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, node, node) {
b8aa1682 1044 node = lttng_ht_iter_get_node_ulong(&iter);
4c80d9a2
DG
1045 if (!node) {
1046 continue;
b8aa1682 1047 }
4c80d9a2
DG
1048 stream = caa_container_of(node, struct relay_stream, stream_n);
1049 if (stream->session == cmd->session) {
c07d8a78 1050 destroy_stream(stream);
87b576ec
JD
1051 cmd->session->stream_count--;
1052 assert(cmd->session->stream_count >= 0);
4c80d9a2 1053 }
b8aa1682 1054 }
4c80d9a2
DG
1055
1056 /* Make this session not visible anymore. */
d3e2ba59
JD
1057 iter.iter.node = &cmd->session->session_n.node;
1058 ret = lttng_ht_del(sessions_ht, &iter);
1059 assert(!ret);
4c80d9a2 1060 call_rcu(&cmd->session->rcu_node, deferred_free_session);
9d1bbf21 1061 rcu_read_unlock();
b8aa1682
JD
1062}
1063
1c20f0e2
JD
1064/*
1065 * Copy index data from the control port to a given index object.
1066 */
1067static void copy_index_control_data(struct relay_index *index,
1068 struct lttcomm_relayd_index *data)
1069{
1070 assert(index);
1071 assert(data);
1072
1073 /*
1074 * The index on disk is encoded in big endian, so we don't need to convert
1075 * the data received on the network. The data_offset value is NEVER
1076 * modified here and is updated by the data thread.
1077 */
1078 index->index_data.packet_size = data->packet_size;
1079 index->index_data.content_size = data->content_size;
1080 index->index_data.timestamp_begin = data->timestamp_begin;
1081 index->index_data.timestamp_end = data->timestamp_end;
1082 index->index_data.events_discarded = data->events_discarded;
1083 index->index_data.stream_id = data->stream_id;
1084}
1085
c5b6f4f0
DG
1086/*
1087 * Handle the RELAYD_CREATE_SESSION command.
1088 *
1089 * On success, send back the session id or else return a negative value.
1090 */
1091static
1092int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59
JD
1093 struct relay_command *cmd,
1094 struct lttng_ht *sessions_ht)
c5b6f4f0
DG
1095{
1096 int ret = 0, send_ret;
1097 struct relay_session *session;
1098 struct lttcomm_relayd_status_session reply;
1099
1100 assert(recv_hdr);
1101 assert(cmd);
1102
1103 memset(&reply, 0, sizeof(reply));
1104
1105 session = zmalloc(sizeof(struct relay_session));
1106 if (session == NULL) {
1107 PERROR("relay session zmalloc");
1108 ret = -1;
1109 goto error;
1110 }
1111
1112 session->id = ++last_relay_session_id;
1113 session->sock = cmd->sock;
7d2f7452
DG
1114 session->minor = cmd->minor;
1115 session->major = cmd->major;
c5b6f4f0
DG
1116 cmd->session = session;
1117
1118 reply.session_id = htobe64(session->id);
1119
d3e2ba59
JD
1120 switch (cmd->minor) {
1121 case 4: /* LTTng sessiond 2.4 */
1122 default:
1123 ret = cmd_create_session_2_4(cmd, session);
1124 break;
1125 }
1126
1127 lttng_ht_node_init_ulong(&session->session_n,
1128 (unsigned long) session->id);
1129 lttng_ht_add_unique_ulong(sessions_ht,
1130 &session->session_n);
1131
c5b6f4f0
DG
1132 DBG("Created session %" PRIu64, session->id);
1133
1134error:
1135 if (ret < 0) {
1136 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
1137 } else {
1138 reply.ret_code = htobe32(LTTNG_OK);
1139 }
1140
1141 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1142 if (send_ret < 0) {
1143 ERR("Relayd sending session id");
4169f5ad 1144 ret = send_ret;
c5b6f4f0
DG
1145 }
1146
1147 return ret;
1148}
1149
b8aa1682
JD
1150/*
1151 * relay_add_stream: allocate a new stream for a session
1152 */
1153static
1154int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1155 struct relay_command *cmd, struct lttng_ht *sessions_ht)
b8aa1682
JD
1156{
1157 struct relay_session *session = cmd->session;
b8aa1682
JD
1158 struct relay_stream *stream = NULL;
1159 struct lttcomm_relayd_status_stream reply;
b8aa1682
JD
1160 int ret, send_ret;
1161
c5b6f4f0 1162 if (!session || cmd->version_check_done == 0) {
b8aa1682
JD
1163 ERR("Trying to add a stream before version check");
1164 ret = -1;
1165 goto end_no_session;
1166 }
1167
b8aa1682
JD
1168 stream = zmalloc(sizeof(struct relay_stream));
1169 if (stream == NULL) {
1170 PERROR("relay stream zmalloc");
1171 ret = -1;
1172 goto end_no_session;
1173 }
1174
0f907de1
JD
1175 switch (cmd->minor) {
1176 case 1: /* LTTng sessiond 2.1 */
1177 ret = cmd_recv_stream_2_1(cmd, stream);
1178 break;
1179 case 2: /* LTTng sessiond 2.2 */
1180 default:
1181 ret = cmd_recv_stream_2_2(cmd, stream);
1182 break;
1183 }
1184 if (ret < 0) {
1185 goto err_free_stream;
1186 }
1187
9d1bbf21 1188 rcu_read_lock();
b8aa1682 1189 stream->stream_handle = ++last_relay_stream_id;
173af62f 1190 stream->prev_seq = -1ULL;
b8aa1682 1191 stream->session = session;
1c20f0e2 1192 stream->index_fd = -1;
d3e2ba59
JD
1193 stream->read_index_fd = -1;
1194 stream->ctf_trace = NULL;
1195 pthread_mutex_init(&stream->lock, NULL);
b8aa1682 1196
0f907de1 1197 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG);
b8aa1682 1198 if (ret < 0) {
b8aa1682
JD
1199 ERR("relay creating output directory");
1200 goto end;
1201 }
1202
be96a7d1
DG
1203 /*
1204 * No need to use run_as API here because whatever we receives, the relayd
1205 * uses its own credentials for the stream files.
1206 */
0f907de1 1207 ret = utils_create_stream_file(stream->path_name, stream->channel_name,
1c20f0e2 1208 stream->tracefile_size, 0, relayd_uid, relayd_gid, NULL);
b8aa1682 1209 if (ret < 0) {
0f907de1 1210 ERR("Create output file");
b8aa1682
JD
1211 goto end;
1212 }
b8aa1682 1213 stream->fd = ret;
0f907de1
JD
1214 if (stream->tracefile_size) {
1215 DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name);
1216 } else {
1217 DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name);
1218 }
b8aa1682 1219
d3e2ba59
JD
1220 if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, NAME_MAX)) {
1221 stream->metadata_flag = 1;
1222 /*
1223 * When we receive a new metadata stream, we create a new
1224 * ctf_trace and we assign this ctf_trace to all streams with
1225 * the same path.
1226 *
1227 * If later on we receive a new stream for the same ctf_trace,
1228 * we copy the information from the first hit in the HT to the
1229 * new stream.
1230 */
1231 stream->ctf_trace = ctf_trace_create();
1232 if (!stream->ctf_trace) {
1233 ret = -1;
1234 goto end;
1235 }
1236 stream->ctf_trace->refcount++;
1237 stream->ctf_trace->metadata_stream = stream;
1238 }
1239 ctf_trace_assign(cmd->ctf_traces_ht, stream);
c07d8a78 1240 stream->ctf_traces_ht = cmd->ctf_traces_ht;
d3e2ba59 1241
b8aa1682
JD
1242 lttng_ht_node_init_ulong(&stream->stream_n,
1243 (unsigned long) stream->stream_handle);
d3e2ba59 1244 lttng_ht_add_unique_ulong(relay_streams_ht,
b8aa1682
JD
1245 &stream->stream_n);
1246
d3e2ba59
JD
1247 lttng_ht_node_init_str(&stream->ctf_trace_node, stream->path_name);
1248 lttng_ht_add_str(cmd->ctf_traces_ht, &stream->ctf_trace_node);
87b576ec 1249 session->stream_count++;
d3e2ba59 1250
1c20f0e2
JD
1251 DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name,
1252 stream->stream_handle);
b8aa1682
JD
1253
1254end:
5af40280 1255 reply.handle = htobe64(stream->stream_handle);
b8aa1682
JD
1256 /* send the session id to the client or a negative return code on error */
1257 if (ret < 0) {
f73fabfd 1258 reply.ret_code = htobe32(LTTNG_ERR_UNK);
5af40280
CB
1259 /* stream was not properly added to the ht, so free it */
1260 free(stream);
b8aa1682 1261 } else {
f73fabfd 1262 reply.ret_code = htobe32(LTTNG_OK);
b8aa1682 1263 }
5af40280 1264
b8aa1682
JD
1265 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1266 sizeof(struct lttcomm_relayd_status_stream), 0);
1267 if (send_ret < 0) {
1268 ERR("Relay sending stream id");
4169f5ad 1269 ret = send_ret;
b8aa1682 1270 }
9d1bbf21 1271 rcu_read_unlock();
b8aa1682
JD
1272
1273end_no_session:
1274 return ret;
0f907de1
JD
1275
1276err_free_stream:
1277 free(stream->path_name);
1278 free(stream->channel_name);
1279 free(stream);
1280 return ret;
b8aa1682
JD
1281}
1282
173af62f
DG
1283/*
1284 * relay_close_stream: close a specific stream
1285 */
1286static
1287int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
92c6ca54 1288 struct relay_command *cmd)
173af62f 1289{
94d49140 1290 int ret, send_ret;
173af62f
DG
1291 struct relay_session *session = cmd->session;
1292 struct lttcomm_relayd_close_stream stream_info;
1293 struct lttcomm_relayd_generic_reply reply;
1294 struct relay_stream *stream;
173af62f
DG
1295
1296 DBG("Close stream received");
1297
c5b6f4f0 1298 if (!session || cmd->version_check_done == 0) {
173af62f
DG
1299 ERR("Trying to close a stream before version check");
1300 ret = -1;
1301 goto end_no_session;
1302 }
1303
1304 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
7c5aef62 1305 sizeof(struct lttcomm_relayd_close_stream), 0);
173af62f 1306 if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
a6cd2b97
DG
1307 if (ret == 0) {
1308 /* Orderly shutdown. Not necessary to print an error. */
1309 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1310 } else {
1311 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
1312 }
173af62f
DG
1313 ret = -1;
1314 goto end_no_session;
1315 }
1316
1317 rcu_read_lock();
d3e2ba59 1318 stream = relay_stream_find_by_id(be64toh(stream_info.stream_id));
173af62f
DG
1319 if (!stream) {
1320 ret = -1;
1321 goto end_unlock;
1322 }
1323
8e2583a4 1324 stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num);
173af62f 1325 stream->close_flag = 1;
87b576ec
JD
1326 session->stream_count--;
1327 assert(session->stream_count >= 0);
173af62f
DG
1328
1329 if (close_stream_check(stream)) {
c07d8a78 1330 destroy_stream(stream);
173af62f
DG
1331 }
1332
1333end_unlock:
1334 rcu_read_unlock();
1335
1336 if (ret < 0) {
f73fabfd 1337 reply.ret_code = htobe32(LTTNG_ERR_UNK);
173af62f 1338 } else {
f73fabfd 1339 reply.ret_code = htobe32(LTTNG_OK);
173af62f
DG
1340 }
1341 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1342 sizeof(struct lttcomm_relayd_generic_reply), 0);
1343 if (send_ret < 0) {
1344 ERR("Relay sending stream id");
4169f5ad 1345 ret = send_ret;
173af62f
DG
1346 }
1347
1348end_no_session:
1349 return ret;
1350}
1351
b8aa1682
JD
1352/*
1353 * relay_unknown_command: send -1 if received unknown command
1354 */
1355static
1356void relay_unknown_command(struct relay_command *cmd)
1357{
1358 struct lttcomm_relayd_generic_reply reply;
1359 int ret;
1360
f73fabfd 1361 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1362 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1363 sizeof(struct lttcomm_relayd_generic_reply), 0);
1364 if (ret < 0) {
1365 ERR("Relay sending unknown command");
1366 }
1367}
1368
1369/*
1370 * relay_start: send an acknowledgment to the client to tell if we are
1371 * ready to receive data. We are ready if a session is established.
1372 */
1373static
1374int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
1375 struct relay_command *cmd)
1376{
f73fabfd 1377 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1378 struct lttcomm_relayd_generic_reply reply;
1379 struct relay_session *session = cmd->session;
1380
1381 if (!session) {
1382 DBG("Trying to start the streaming without a session established");
f73fabfd 1383 ret = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1384 }
1385
1386 reply.ret_code = ret;
1387 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1388 sizeof(struct lttcomm_relayd_generic_reply), 0);
1389 if (ret < 0) {
1390 ERR("Relay sending start ack");
1391 }
1392
1393 return ret;
1394}
1395
1d4dfdef
DG
1396/*
1397 * Append padding to the file pointed by the file descriptor fd.
1398 */
1399static int write_padding_to_file(int fd, uint32_t size)
1400{
6cd525e8 1401 ssize_t ret = 0;
1d4dfdef
DG
1402 char *zeros;
1403
1404 if (size == 0) {
1405 goto end;
1406 }
1407
1408 zeros = zmalloc(size);
1409 if (zeros == NULL) {
1410 PERROR("zmalloc zeros for padding");
1411 ret = -1;
1412 goto end;
1413 }
1414
6cd525e8
MD
1415 ret = lttng_write(fd, zeros, size);
1416 if (ret < size) {
1d4dfdef
DG
1417 PERROR("write padding to file");
1418 }
1419
e986c7a1
DG
1420 free(zeros);
1421
1d4dfdef
DG
1422end:
1423 return ret;
1424}
1425
b8aa1682
JD
1426/*
1427 * relay_recv_metadata: receive the metada for the session.
1428 */
1429static
1430int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1431 struct relay_command *cmd)
b8aa1682 1432{
f73fabfd 1433 int ret = htobe32(LTTNG_OK);
6cd525e8 1434 ssize_t size_ret;
b8aa1682
JD
1435 struct relay_session *session = cmd->session;
1436 struct lttcomm_relayd_metadata_payload *metadata_struct;
1437 struct relay_stream *metadata_stream;
1438 uint64_t data_size, payload_size;
1439
1440 if (!session) {
1441 ERR("Metadata sent before version check");
1442 ret = -1;
1443 goto end;
1444 }
1445
f6416125
MD
1446 data_size = payload_size = be64toh(recv_hdr->data_size);
1447 if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1448 ERR("Incorrect data size");
1449 ret = -1;
1450 goto end;
1451 }
1452 payload_size -= sizeof(struct lttcomm_relayd_metadata_payload);
1453
b8aa1682 1454 if (data_buffer_size < data_size) {
d7b3776f 1455 /* In case the realloc fails, we can free the memory */
c617c0c6
MD
1456 char *tmp_data_ptr;
1457
1458 tmp_data_ptr = realloc(data_buffer, data_size);
1459 if (!tmp_data_ptr) {
b8aa1682 1460 ERR("Allocating data buffer");
c617c0c6 1461 free(data_buffer);
b8aa1682
JD
1462 ret = -1;
1463 goto end;
1464 }
c617c0c6 1465 data_buffer = tmp_data_ptr;
b8aa1682
JD
1466 data_buffer_size = data_size;
1467 }
1468 memset(data_buffer, 0, data_size);
77c7c900 1469 DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
7c5aef62 1470 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682 1471 if (ret < 0 || ret != data_size) {
a6cd2b97
DG
1472 if (ret == 0) {
1473 /* Orderly shutdown. Not necessary to print an error. */
1474 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1475 } else {
1476 ERR("Relay didn't receive the whole metadata");
1477 }
b8aa1682 1478 ret = -1;
b8aa1682
JD
1479 goto end;
1480 }
1481 metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
9d1bbf21
MD
1482
1483 rcu_read_lock();
d3e2ba59
JD
1484 metadata_stream = relay_stream_find_by_id(
1485 be64toh(metadata_struct->stream_id));
b8aa1682
JD
1486 if (!metadata_stream) {
1487 ret = -1;
9d1bbf21 1488 goto end_unlock;
b8aa1682
JD
1489 }
1490
6cd525e8
MD
1491 size_ret = lttng_write(metadata_stream->fd, metadata_struct->payload,
1492 payload_size);
1493 if (size_ret < payload_size) {
b8aa1682
JD
1494 ERR("Relay error writing metadata on file");
1495 ret = -1;
9d1bbf21 1496 goto end_unlock;
b8aa1682 1497 }
1d4dfdef
DG
1498
1499 ret = write_padding_to_file(metadata_stream->fd,
1500 be32toh(metadata_struct->padding_size));
1501 if (ret < 0) {
1502 goto end_unlock;
1503 }
d3e2ba59
JD
1504 metadata_stream->ctf_trace->metadata_received +=
1505 payload_size + be32toh(metadata_struct->padding_size);
1d4dfdef 1506
b8aa1682
JD
1507 DBG2("Relay metadata written");
1508
9d1bbf21 1509end_unlock:
6e3c5836 1510 rcu_read_unlock();
b8aa1682
JD
1511end:
1512 return ret;
1513}
1514
1515/*
1516 * relay_send_version: send relayd version number
1517 */
1518static
1519int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1520 struct relay_command *cmd, struct lttng_ht *sessions_ht)
b8aa1682 1521{
7f51dcba 1522 int ret;
092b6259 1523 struct lttcomm_relayd_version reply, msg;
b8aa1682 1524
c5b6f4f0
DG
1525 assert(cmd);
1526
1527 cmd->version_check_done = 1;
b8aa1682 1528
092b6259 1529 /* Get version from the other side. */
7c5aef62 1530 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
092b6259 1531 if (ret < 0 || ret != sizeof(msg)) {
a6cd2b97
DG
1532 if (ret == 0) {
1533 /* Orderly shutdown. Not necessary to print an error. */
1534 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1535 } else {
1536 ERR("Relay failed to receive the version values.");
1537 }
092b6259 1538 ret = -1;
092b6259
DG
1539 goto end;
1540 }
1541
d83a952c
MD
1542 reply.major = RELAYD_VERSION_COMM_MAJOR;
1543 reply.minor = RELAYD_VERSION_COMM_MINOR;
d4519fa3
JD
1544
1545 /* Major versions must be the same */
1546 if (reply.major != be32toh(msg.major)) {
6151a90f
JD
1547 DBG("Incompatible major versions (%u vs %u), deleting session",
1548 reply.major, be32toh(msg.major));
d3e2ba59 1549 relay_delete_session(cmd, sessions_ht);
d4519fa3
JD
1550 ret = 0;
1551 goto end;
1552 }
1553
0f907de1
JD
1554 cmd->major = reply.major;
1555 /* We adapt to the lowest compatible version */
1556 if (reply.minor <= be32toh(msg.minor)) {
1557 cmd->minor = reply.minor;
1558 } else {
1559 cmd->minor = be32toh(msg.minor);
1560 }
1561
6151a90f
JD
1562 reply.major = htobe32(reply.major);
1563 reply.minor = htobe32(reply.minor);
1564 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1565 sizeof(struct lttcomm_relayd_version), 0);
1566 if (ret < 0) {
1567 ERR("Relay sending version");
1568 }
1569
0f907de1
JD
1570 DBG("Version check done using protocol %u.%u", cmd->major,
1571 cmd->minor);
b8aa1682
JD
1572
1573end:
1574 return ret;
1575}
1576
c8f59ee5 1577/*
6d805429 1578 * Check for data pending for a given stream id from the session daemon.
c8f59ee5
DG
1579 */
1580static
6d805429 1581int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1582 struct relay_command *cmd)
c8f59ee5
DG
1583{
1584 struct relay_session *session = cmd->session;
6d805429 1585 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
1586 struct lttcomm_relayd_generic_reply reply;
1587 struct relay_stream *stream;
1588 int ret;
c8f59ee5
DG
1589 uint64_t last_net_seq_num, stream_id;
1590
6d805429 1591 DBG("Data pending command received");
c8f59ee5 1592
c5b6f4f0 1593 if (!session || cmd->version_check_done == 0) {
c8f59ee5
DG
1594 ERR("Trying to check for data before version check");
1595 ret = -1;
1596 goto end_no_session;
1597 }
1598
7c5aef62 1599 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
c8f59ee5 1600 if (ret < sizeof(msg)) {
a6cd2b97
DG
1601 if (ret == 0) {
1602 /* Orderly shutdown. Not necessary to print an error. */
1603 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1604 } else {
1605 ERR("Relay didn't receive valid data_pending struct size : %d",
1606 ret);
1607 }
c8f59ee5
DG
1608 ret = -1;
1609 goto end_no_session;
1610 }
1611
1612 stream_id = be64toh(msg.stream_id);
1613 last_net_seq_num = be64toh(msg.last_net_seq_num);
1614
1615 rcu_read_lock();
d3e2ba59 1616 stream = relay_stream_find_by_id(stream_id);
de91f48a 1617 if (stream == NULL) {
c8f59ee5
DG
1618 ret = -1;
1619 goto end_unlock;
1620 }
1621
6d805429 1622 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
c8f59ee5
DG
1623 " and last_seq %" PRIu64, stream_id, stream->prev_seq,
1624 last_net_seq_num);
1625
33832e64 1626 /* Avoid wrapping issue */
39df6d9f 1627 if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) {
6d805429 1628 /* Data has in fact been written and is NOT pending */
c8f59ee5 1629 ret = 0;
6d805429
DG
1630 } else {
1631 /* Data still being streamed thus pending */
1632 ret = 1;
c8f59ee5
DG
1633 }
1634
f7079f67
DG
1635 /* Pending check is now done. */
1636 stream->data_pending_check_done = 1;
1637
c8f59ee5
DG
1638end_unlock:
1639 rcu_read_unlock();
1640
1641 reply.ret_code = htobe32(ret);
1642 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1643 if (ret < 0) {
6d805429 1644 ERR("Relay data pending ret code failed");
c8f59ee5
DG
1645 }
1646
1647end_no_session:
1648 return ret;
1649}
1650
1651/*
1652 * Wait for the control socket to reach a quiescent state.
1653 *
1654 * Note that for now, when receiving this command from the session daemon, this
1655 * means that every subsequent commands or data received on the control socket
1656 * has been handled. So, this is why we simply return OK here.
1657 */
1658static
1659int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1660 struct relay_command *cmd)
c8f59ee5
DG
1661{
1662 int ret;
ad7051c0
DG
1663 uint64_t stream_id;
1664 struct relay_stream *stream;
1665 struct lttng_ht_iter iter;
1666 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
1667 struct lttcomm_relayd_generic_reply reply;
1668
1669 DBG("Checking quiescent state on control socket");
1670
ad7051c0
DG
1671 if (!cmd->session || cmd->version_check_done == 0) {
1672 ERR("Trying to check for data before version check");
1673 ret = -1;
1674 goto end_no_session;
1675 }
1676
1677 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1678 if (ret < sizeof(msg)) {
a6cd2b97
DG
1679 if (ret == 0) {
1680 /* Orderly shutdown. Not necessary to print an error. */
1681 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1682 } else {
1683 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1684 ret);
1685 }
ad7051c0
DG
1686 ret = -1;
1687 goto end_no_session;
1688 }
1689
1690 stream_id = be64toh(msg.stream_id);
1691
1692 rcu_read_lock();
d3e2ba59
JD
1693 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1694 stream_n.node) {
ad7051c0
DG
1695 if (stream->stream_handle == stream_id) {
1696 stream->data_pending_check_done = 1;
1697 DBG("Relay quiescent control pending flag set to %" PRIu64,
1698 stream_id);
1699 break;
1700 }
1701 }
1702 rcu_read_unlock();
1703
c8f59ee5
DG
1704 reply.ret_code = htobe32(LTTNG_OK);
1705 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1706 if (ret < 0) {
6d805429 1707 ERR("Relay data quiescent control ret code failed");
c8f59ee5
DG
1708 }
1709
ad7051c0 1710end_no_session:
c8f59ee5
DG
1711 return ret;
1712}
1713
f7079f67
DG
1714/*
1715 * Initialize a data pending command. This means that a client is about to ask
1716 * for data pending for each stream he/she holds. Simply iterate over all
1717 * streams of a session and set the data_pending_check_done flag.
1718 *
1719 * This command returns to the client a LTTNG_OK code.
1720 */
1721static
1722int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1723 struct relay_command *cmd)
f7079f67
DG
1724{
1725 int ret;
1726 struct lttng_ht_iter iter;
1727 struct lttcomm_relayd_begin_data_pending msg;
1728 struct lttcomm_relayd_generic_reply reply;
1729 struct relay_stream *stream;
1730 uint64_t session_id;
1731
1732 assert(recv_hdr);
1733 assert(cmd);
f7079f67
DG
1734
1735 DBG("Init streams for data pending");
1736
1737 if (!cmd->session || cmd->version_check_done == 0) {
1738 ERR("Trying to check for data before version check");
1739 ret = -1;
1740 goto end_no_session;
1741 }
1742
1743 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1744 if (ret < sizeof(msg)) {
a6cd2b97
DG
1745 if (ret == 0) {
1746 /* Orderly shutdown. Not necessary to print an error. */
1747 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1748 } else {
1749 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1750 ret);
1751 }
f7079f67
DG
1752 ret = -1;
1753 goto end_no_session;
1754 }
1755
1756 session_id = be64toh(msg.session_id);
1757
1758 /*
1759 * Iterate over all streams to set the begin data pending flag. For now, the
1760 * streams are indexed by stream handle so we have to iterate over all
1761 * streams to find the one associated with the right session_id.
1762 */
1763 rcu_read_lock();
d3e2ba59
JD
1764 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1765 stream_n.node) {
f7079f67
DG
1766 if (stream->session->id == session_id) {
1767 stream->data_pending_check_done = 0;
1768 DBG("Set begin data pending flag to stream %" PRIu64,
1769 stream->stream_handle);
1770 }
1771 }
1772 rcu_read_unlock();
1773
1774 /* All good, send back reply. */
1775 reply.ret_code = htobe32(LTTNG_OK);
1776
1777 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1778 if (ret < 0) {
1779 ERR("Relay begin data pending send reply failed");
1780 }
1781
1782end_no_session:
1783 return ret;
1784}
1785
1786/*
1787 * End data pending command. This will check, for a given session id, if each
1788 * stream associated with it has its data_pending_check_done flag set. If not,
1789 * this means that the client lost track of the stream but the data is still
1790 * being streamed on our side. In this case, we inform the client that data is
1791 * inflight.
1792 *
1793 * Return to the client if there is data in flight or not with a ret_code.
1794 */
1795static
1796int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1797 struct relay_command *cmd)
f7079f67
DG
1798{
1799 int ret;
1800 struct lttng_ht_iter iter;
1801 struct lttcomm_relayd_end_data_pending msg;
1802 struct lttcomm_relayd_generic_reply reply;
1803 struct relay_stream *stream;
1804 uint64_t session_id;
1805 uint32_t is_data_inflight = 0;
1806
1807 assert(recv_hdr);
1808 assert(cmd);
f7079f67
DG
1809
1810 DBG("End data pending command");
1811
1812 if (!cmd->session || cmd->version_check_done == 0) {
1813 ERR("Trying to check for data before version check");
1814 ret = -1;
1815 goto end_no_session;
1816 }
1817
1818 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1819 if (ret < sizeof(msg)) {
a6cd2b97
DG
1820 if (ret == 0) {
1821 /* Orderly shutdown. Not necessary to print an error. */
1822 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1823 } else {
1824 ERR("Relay didn't receive valid end data_pending struct size: %d",
1825 ret);
1826 }
f7079f67
DG
1827 ret = -1;
1828 goto end_no_session;
1829 }
1830
1831 session_id = be64toh(msg.session_id);
1832
1833 /* Iterate over all streams to see if the begin data pending flag is set. */
1834 rcu_read_lock();
d3e2ba59
JD
1835 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1836 stream_n.node) {
f7079f67
DG
1837 if (stream->session->id == session_id &&
1838 !stream->data_pending_check_done) {
1839 is_data_inflight = 1;
1840 DBG("Data is still in flight for stream %" PRIu64,
1841 stream->stream_handle);
1842 break;
1843 }
1844 }
1845 rcu_read_unlock();
1846
1847 /* All good, send back reply. */
1848 reply.ret_code = htobe32(is_data_inflight);
1849
1850 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1851 if (ret < 0) {
1852 ERR("Relay end data pending send reply failed");
1853 }
1854
1855end_no_session:
1856 return ret;
1857}
1858
1c20f0e2
JD
1859/*
1860 * Receive an index for a specific stream.
1861 *
1862 * Return 0 on success else a negative value.
1863 */
1864static
1865int relay_recv_index(struct lttcomm_relayd_hdr *recv_hdr,
0a6518b0 1866 struct relay_command *cmd)
1c20f0e2
JD
1867{
1868 int ret, send_ret, index_created = 0;
1869 struct relay_session *session = cmd->session;
1870 struct lttcomm_relayd_index index_info;
1871 struct relay_index *index, *wr_index = NULL;
1872 struct lttcomm_relayd_generic_reply reply;
1873 struct relay_stream *stream;
1874 uint64_t net_seq_num;
1875
1876 assert(cmd);
1c20f0e2
JD
1877
1878 DBG("Relay receiving index");
1879
1880 if (!session || cmd->version_check_done == 0) {
1881 ERR("Trying to close a stream before version check");
1882 ret = -1;
1883 goto end_no_session;
1884 }
1885
1886 ret = cmd->sock->ops->recvmsg(cmd->sock, &index_info,
1887 sizeof(index_info), 0);
1888 if (ret < sizeof(index_info)) {
1889 if (ret == 0) {
1890 /* Orderly shutdown. Not necessary to print an error. */
1891 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1892 } else {
1893 ERR("Relay didn't receive valid index struct size : %d", ret);
1894 }
1895 ret = -1;
1896 goto end_no_session;
1897 }
1898
1899 net_seq_num = be64toh(index_info.net_seq_num);
1900
1901 rcu_read_lock();
d3e2ba59 1902 stream = relay_stream_find_by_id(be64toh(index_info.relay_stream_id));
1c20f0e2
JD
1903 if (!stream) {
1904 ret = -1;
1905 goto end_rcu_unlock;
1906 }
1907
d3e2ba59
JD
1908 /* Live beacon handling */
1909 if (index_info.packet_size == 0) {
1910 DBG("Received live beacon for stream %" PRIu64, stream->stream_handle);
1911
1912 /*
1913 * Only flag a stream inactive when it has already received data.
1914 */
1915 if (stream->total_index_received > 0) {
1916 stream->beacon_ts_end = be64toh(index_info.timestamp_end);
1917 }
1918 ret = 0;
1919 goto end_rcu_unlock;
1920 } else {
1921 stream->beacon_ts_end = -1ULL;
1922 }
1923
0a6518b0 1924 index = relay_index_find(stream->stream_handle, net_seq_num);
1c20f0e2
JD
1925 if (!index) {
1926 /* A successful creation will add the object to the HT. */
1927 index = relay_index_create(stream->stream_handle, net_seq_num);
1928 if (!index) {
1929 goto end_rcu_unlock;
1930 }
1931 index_created = 1;
1932 }
1933
1934 copy_index_control_data(index, &index_info);
1935
1936 if (index_created) {
1937 /*
1938 * Try to add the relay index object to the hash table. If an object
1939 * already exist, destroy back the index created, set the data in this
1940 * object and write it on disk.
1941 */
0a6518b0 1942 relay_index_add(index, &wr_index);
1c20f0e2
JD
1943 if (wr_index) {
1944 copy_index_control_data(wr_index, &index_info);
1945 free(index);
1946 }
1947 } else {
1948 /* The index already exists so write it on disk. */
1949 wr_index = index;
1950 }
1951
1952 /* Do we have a writable ready index to write on disk. */
1953 if (wr_index) {
1954 /* Starting at 2.4, create the index file if none available. */
1955 if (cmd->minor >= 4 && stream->index_fd < 0) {
1956 ret = index_create_file(stream->path_name, stream->channel_name,
1957 relayd_uid, relayd_gid, stream->tracefile_size,
1958 stream->tracefile_count_current);
1959 if (ret < 0) {
1960 goto end_rcu_unlock;
1961 }
1962 stream->index_fd = ret;
1963 }
1964
0a6518b0 1965 ret = relay_index_write(wr_index->fd, wr_index);
1c20f0e2
JD
1966 if (ret < 0) {
1967 goto end_rcu_unlock;
1968 }
d3e2ba59 1969 stream->total_index_received++;
1c20f0e2
JD
1970 }
1971
1972end_rcu_unlock:
1973 rcu_read_unlock();
1974
1975 if (ret < 0) {
1976 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1977 } else {
1978 reply.ret_code = htobe32(LTTNG_OK);
1979 }
1980 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1981 if (send_ret < 0) {
1982 ERR("Relay sending close index id reply");
1983 ret = send_ret;
1984 }
1985
1986end_no_session:
1987 return ret;
1988}
1989
b8aa1682 1990/*
d3e2ba59 1991 * Process the commands received on the control socket
b8aa1682
JD
1992 */
1993static
1994int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1995 struct relay_command *cmd, struct relay_local_data *ctx)
b8aa1682
JD
1996{
1997 int ret = 0;
1998
1999 switch (be32toh(recv_hdr->cmd)) {
b8aa1682 2000 case RELAYD_CREATE_SESSION:
d3e2ba59 2001 ret = relay_create_session(recv_hdr, cmd, ctx->sessions_ht);
b8aa1682 2002 break;
b8aa1682 2003 case RELAYD_ADD_STREAM:
d3e2ba59 2004 ret = relay_add_stream(recv_hdr, cmd, ctx->sessions_ht);
b8aa1682
JD
2005 break;
2006 case RELAYD_START_DATA:
2007 ret = relay_start(recv_hdr, cmd);
2008 break;
2009 case RELAYD_SEND_METADATA:
d3e2ba59 2010 ret = relay_recv_metadata(recv_hdr, cmd);
b8aa1682
JD
2011 break;
2012 case RELAYD_VERSION:
d3e2ba59 2013 ret = relay_send_version(recv_hdr, cmd, ctx->sessions_ht);
b8aa1682 2014 break;
173af62f 2015 case RELAYD_CLOSE_STREAM:
92c6ca54 2016 ret = relay_close_stream(recv_hdr, cmd);
173af62f 2017 break;
6d805429 2018 case RELAYD_DATA_PENDING:
d3e2ba59 2019 ret = relay_data_pending(recv_hdr, cmd);
c8f59ee5
DG
2020 break;
2021 case RELAYD_QUIESCENT_CONTROL:
d3e2ba59 2022 ret = relay_quiescent_control(recv_hdr, cmd);
c8f59ee5 2023 break;
f7079f67 2024 case RELAYD_BEGIN_DATA_PENDING:
d3e2ba59 2025 ret = relay_begin_data_pending(recv_hdr, cmd);
f7079f67
DG
2026 break;
2027 case RELAYD_END_DATA_PENDING:
d3e2ba59 2028 ret = relay_end_data_pending(recv_hdr, cmd);
f7079f67 2029 break;
1c20f0e2 2030 case RELAYD_SEND_INDEX:
0a6518b0 2031 ret = relay_recv_index(recv_hdr, cmd);
1c20f0e2 2032 break;
b8aa1682
JD
2033 case RELAYD_UPDATE_SYNC_INFO:
2034 default:
2035 ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
2036 relay_unknown_command(cmd);
2037 ret = -1;
2038 goto end;
2039 }
2040
2041end:
2042 return ret;
2043}
2044
7d2f7452
DG
2045/*
2046 * Handle index for a data stream.
2047 *
2048 * RCU read side lock MUST be acquired.
2049 *
2050 * Return 0 on success else a negative value.
2051 */
2052static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num,
2053 int rotate_index)
2054{
2055 int ret = 0, index_created = 0;
2056 uint64_t stream_id, data_offset;
2057 struct relay_index *index, *wr_index = NULL;
2058
2059 assert(stream);
2060
2061 stream_id = stream->stream_handle;
2062 /* Get data offset because we are about to update the index. */
2063 data_offset = htobe64(stream->tracefile_size_current);
2064
2065 /*
2066 * Lookup for an existing index for that stream id/sequence number. If on
2067 * exists, the control thread already received the data for it thus we need
2068 * to write it on disk.
2069 */
2070 index = relay_index_find(stream_id, net_seq_num);
2071 if (!index) {
2072 /* A successful creation will add the object to the HT. */
2073 index = relay_index_create(stream_id, net_seq_num);
2074 if (!index) {
2075 ret = -1;
2076 goto error;
2077 }
2078 index_created = 1;
2079 }
2080
2081 if (rotate_index || stream->index_fd < 0) {
2082 index->to_close_fd = stream->index_fd;
2083 ret = index_create_file(stream->path_name, stream->channel_name,
2084 relayd_uid, relayd_gid, stream->tracefile_size,
2085 stream->tracefile_count_current);
2086 if (ret < 0) {
2087 /* This will close the stream's index fd if one. */
2088 relay_index_free_safe(index);
2089 goto error;
2090 }
2091 stream->index_fd = ret;
2092 }
2093 index->fd = stream->index_fd;
2094 index->index_data.offset = data_offset;
2095
2096 if (index_created) {
2097 /*
2098 * Try to add the relay index object to the hash table. If an object
2099 * already exist, destroy back the index created and set the data.
2100 */
2101 relay_index_add(index, &wr_index);
2102 if (wr_index) {
2103 /* Copy back data from the created index. */
2104 wr_index->fd = index->fd;
2105 wr_index->to_close_fd = index->to_close_fd;
2106 wr_index->index_data.offset = data_offset;
2107 free(index);
2108 }
2109 } else {
2110 /* The index already exists so write it on disk. */
2111 wr_index = index;
2112 }
2113
2114 /* Do we have a writable ready index to write on disk. */
2115 if (wr_index) {
2116 ret = relay_index_write(wr_index->fd, wr_index);
2117 if (ret < 0) {
2118 goto error;
2119 }
2120 stream->total_index_received++;
2121 }
2122
2123error:
2124 return ret;
2125}
2126
b8aa1682
JD
2127/*
2128 * relay_process_data: Process the data received on the data socket
2129 */
2130static
0a6518b0 2131int relay_process_data(struct relay_command *cmd)
b8aa1682 2132{
7d2f7452 2133 int ret = 0, rotate_index = 0;
6cd525e8 2134 ssize_t size_ret;
b8aa1682
JD
2135 struct relay_stream *stream;
2136 struct lttcomm_relayd_data_hdr data_hdr;
7d2f7452 2137 uint64_t stream_id;
173af62f 2138 uint64_t net_seq_num;
b8aa1682
JD
2139 uint32_t data_size;
2140
2141 ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr,
7c5aef62 2142 sizeof(struct lttcomm_relayd_data_hdr), 0);
b8aa1682 2143 if (ret <= 0) {
a6cd2b97
DG
2144 if (ret == 0) {
2145 /* Orderly shutdown. Not necessary to print an error. */
2146 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
2147 } else {
2148 ERR("Unable to receive data header on sock %d", cmd->sock->fd);
2149 }
b8aa1682
JD
2150 ret = -1;
2151 goto end;
2152 }
2153
2154 stream_id = be64toh(data_hdr.stream_id);
9d1bbf21
MD
2155
2156 rcu_read_lock();
d3e2ba59 2157 stream = relay_stream_find_by_id(stream_id);
b8aa1682
JD
2158 if (!stream) {
2159 ret = -1;
1c20f0e2 2160 goto end_rcu_unlock;
b8aa1682
JD
2161 }
2162
2163 data_size = be32toh(data_hdr.data_size);
2164 if (data_buffer_size < data_size) {
c617c0c6
MD
2165 char *tmp_data_ptr;
2166
2167 tmp_data_ptr = realloc(data_buffer, data_size);
2168 if (!tmp_data_ptr) {
b8aa1682 2169 ERR("Allocating data buffer");
c617c0c6 2170 free(data_buffer);
b8aa1682 2171 ret = -1;
1c20f0e2 2172 goto end_rcu_unlock;
b8aa1682 2173 }
c617c0c6 2174 data_buffer = tmp_data_ptr;
b8aa1682
JD
2175 data_buffer_size = data_size;
2176 }
2177 memset(data_buffer, 0, data_size);
2178
173af62f
DG
2179 net_seq_num = be64toh(data_hdr.net_seq_num);
2180
77c7c900 2181 DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64,
173af62f 2182 data_size, stream_id, net_seq_num);
7c5aef62 2183 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682 2184 if (ret <= 0) {
a6cd2b97
DG
2185 if (ret == 0) {
2186 /* Orderly shutdown. Not necessary to print an error. */
2187 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
2188 }
b8aa1682 2189 ret = -1;
1c20f0e2 2190 goto end_rcu_unlock;
b8aa1682
JD
2191 }
2192
1c20f0e2 2193 /* Check if a rotation is needed. */
0f907de1
JD
2194 if (stream->tracefile_size > 0 &&
2195 (stream->tracefile_size_current + data_size) >
2196 stream->tracefile_size) {
6b6b9a5a
JD
2197 struct relay_viewer_stream *vstream;
2198 uint64_t new_id;
2199
2200 new_id = (stream->tracefile_count_current + 1) %
2201 stream->tracefile_count;
2202 /*
2203 * When we wrap-around back to 0, we start overwriting old
2204 * trace data.
2205 */
2206 if (!stream->tracefile_overwrite && new_id == 0) {
2207 stream->tracefile_overwrite = 1;
2208 }
2209 pthread_mutex_lock(&stream->viewer_stream_rotation_lock);
2210 if (stream->tracefile_overwrite) {
2211 stream->oldest_tracefile_id =
2212 (stream->oldest_tracefile_id + 1) %
2213 stream->tracefile_count;
2214 }
2215 vstream = live_find_viewer_stream_by_id(stream->stream_handle);
2216 if (vstream) {
2217 /*
2218 * The viewer is reading a file about to be
2219 * overwritten. Close the FDs it is
2220 * currently using and let it handle the fault.
2221 */
2222 if (vstream->tracefile_count_current == new_id) {
cef0f7d5 2223 pthread_mutex_lock(&vstream->overwrite_lock);
6b6b9a5a 2224 vstream->abort_flag = 1;
cef0f7d5 2225 pthread_mutex_unlock(&vstream->overwrite_lock);
6b6b9a5a
JD
2226 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2227 stream->channel_name, new_id);
2228 } else if (vstream->tracefile_count_current ==
2229 stream->tracefile_count_current) {
2230 /*
2231 * The reader and writer were in the
2232 * same trace file, inform the viewer
2233 * that no new index will ever be added
2234 * to this file.
2235 */
2236 vstream->close_write_flag = 1;
2237 }
2238 }
1c20f0e2
JD
2239 ret = utils_rotate_stream_file(stream->path_name, stream->channel_name,
2240 stream->tracefile_size, stream->tracefile_count,
2241 relayd_uid, relayd_gid, stream->fd,
2242 &(stream->tracefile_count_current), &stream->fd);
cef0f7d5 2243 stream->total_index_received = 0;
6b6b9a5a 2244 pthread_mutex_unlock(&stream->viewer_stream_rotation_lock);
0f907de1 2245 if (ret < 0) {
1c20f0e2
JD
2246 ERR("Rotating stream output file");
2247 goto end_rcu_unlock;
0f907de1 2248 }
a6976990
DG
2249 /* Reset current size because we just perform a stream rotation. */
2250 stream->tracefile_size_current = 0;
1c20f0e2
JD
2251 rotate_index = 1;
2252 }
2253
1c20f0e2 2254 /*
7d2f7452
DG
2255 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2256 * index are NOT supported.
1c20f0e2 2257 */
7d2f7452
DG
2258 if (stream->session->minor >= 4 && !stream->session->snapshot) {
2259 ret = handle_index_data(stream, net_seq_num, rotate_index);
1c20f0e2 2260 if (ret < 0) {
1c20f0e2
JD
2261 goto end_rcu_unlock;
2262 }
1c20f0e2
JD
2263 }
2264
7d2f7452 2265 /* Write data to stream output fd. */
6cd525e8
MD
2266 size_ret = lttng_write(stream->fd, data_buffer, data_size);
2267 if (size_ret < data_size) {
b8aa1682
JD
2268 ERR("Relay error writing data to file");
2269 ret = -1;
1c20f0e2 2270 goto end_rcu_unlock;
b8aa1682 2271 }
1d4dfdef 2272
5ab7344e
JD
2273 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64,
2274 ret, stream->stream_handle);
2275
1d4dfdef
DG
2276 ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size));
2277 if (ret < 0) {
1c20f0e2 2278 goto end_rcu_unlock;
1d4dfdef 2279 }
1c20f0e2 2280 stream->tracefile_size_current += data_size + be32toh(data_hdr.padding_size);
1d4dfdef 2281
173af62f
DG
2282 stream->prev_seq = net_seq_num;
2283
2284 /* Check if we need to close the FD */
2285 if (close_stream_check(stream)) {
c07d8a78 2286 destroy_stream(stream);
173af62f
DG
2287 }
2288
1c20f0e2 2289end_rcu_unlock:
9d1bbf21 2290 rcu_read_unlock();
b8aa1682
JD
2291end:
2292 return ret;
2293}
2294
2295static
9d1bbf21 2296void relay_cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
b8aa1682
JD
2297{
2298 int ret;
2299
b8aa1682
JD
2300 lttng_poll_del(events, pollfd);
2301
2302 ret = close(pollfd);
2303 if (ret < 0) {
2304 ERR("Closing pollfd %d", pollfd);
2305 }
2306}
2307
2308static
2309int relay_add_connection(int fd, struct lttng_poll_event *events,
2310 struct lttng_ht *relay_connections_ht)
2311{
b8aa1682 2312 struct relay_command *relay_connection;
6cd525e8 2313 ssize_t ret;
b8aa1682
JD
2314
2315 relay_connection = zmalloc(sizeof(struct relay_command));
2316 if (relay_connection == NULL) {
2317 PERROR("Relay command zmalloc");
9d1bbf21 2318 goto error;
b8aa1682 2319 }
6cd525e8
MD
2320 ret = lttng_read(fd, relay_connection, sizeof(struct relay_command));
2321 if (ret < sizeof(struct relay_command)) {
b8aa1682 2322 PERROR("read relay cmd pipe");
9d1bbf21 2323 goto error_read;
b8aa1682
JD
2324 }
2325
c07d8a78
DG
2326 /*
2327 * Only used by the control side and the reference is copied inside each
2328 * stream from that connection. Thus a destroy HT must be done after every
2329 * stream has been destroyed.
2330 */
2331 if (relay_connection->type == RELAY_CONTROL) {
2332 relay_connection->ctf_traces_ht = lttng_ht_new(0,
2333 LTTNG_HT_TYPE_STRING);
2334 if (!relay_connection->ctf_traces_ht) {
2335 goto error_read;
2336 }
d3e2ba59
JD
2337 }
2338
b8aa1682
JD
2339 lttng_ht_node_init_ulong(&relay_connection->sock_n,
2340 (unsigned long) relay_connection->sock->fd);
9d1bbf21 2341 rcu_read_lock();
b8aa1682
JD
2342 lttng_ht_add_unique_ulong(relay_connections_ht,
2343 &relay_connection->sock_n);
9d1bbf21
MD
2344 rcu_read_unlock();
2345 return lttng_poll_add(events,
b8aa1682
JD
2346 relay_connection->sock->fd,
2347 LPOLLIN | LPOLLRDHUP);
2348
9d1bbf21
MD
2349error_read:
2350 free(relay_connection);
2351error:
2352 return -1;
2353}
2354
2355static
2356void deferred_free_connection(struct rcu_head *head)
2357{
2358 struct relay_command *relay_connection =
2359 caa_container_of(head, struct relay_command, rcu_node);
5b6d8097
DG
2360
2361 lttcomm_destroy_sock(relay_connection->sock);
9d1bbf21
MD
2362 free(relay_connection);
2363}
2364
2365static
2366void relay_del_connection(struct lttng_ht *relay_connections_ht,
d3e2ba59
JD
2367 struct lttng_ht_iter *iter, struct relay_command *relay_connection,
2368 struct lttng_ht *sessions_ht)
9d1bbf21
MD
2369{
2370 int ret;
2371
2372 ret = lttng_ht_del(relay_connections_ht, iter);
2373 assert(!ret);
c07d8a78 2374
9d1bbf21 2375 if (relay_connection->type == RELAY_CONTROL) {
d3e2ba59 2376 relay_delete_session(relay_connection, sessions_ht);
c07d8a78 2377 lttng_ht_destroy(relay_connection->ctf_traces_ht);
9d1bbf21 2378 }
5b6d8097 2379
c07d8a78 2380 call_rcu(&relay_connection->rcu_node, deferred_free_connection);
b8aa1682
JD
2381}
2382
2383/*
2384 * This thread does the actual work
2385 */
2386static
2387void *relay_thread_worker(void *data)
2388{
beaad64c
DG
2389 int ret, err = -1, last_seen_data_fd = -1;
2390 uint32_t nb_fd;
b8aa1682
JD
2391 struct relay_command *relay_connection;
2392 struct lttng_poll_event events;
2393 struct lttng_ht *relay_connections_ht;
2394 struct lttng_ht_node_ulong *node;
2395 struct lttng_ht_iter iter;
b8aa1682 2396 struct lttcomm_relayd_hdr recv_hdr;
d3e2ba59
JD
2397 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
2398 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
b8aa1682
JD
2399
2400 DBG("[thread] Relay worker started");
2401
9d1bbf21
MD
2402 rcu_register_thread();
2403
55706a7d
MD
2404 health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER);
2405
f385ae0a
MD
2406 health_code_update();
2407
b8aa1682
JD
2408 /* table of connections indexed on socket */
2409 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
2410 if (!relay_connections_ht) {
2411 goto relay_connections_ht_error;
2412 }
b8aa1682 2413
1c20f0e2
JD
2414 /* Tables of received indexes indexed by index handle and net_seq_num. */
2415 indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64);
2416 if (!indexes_ht) {
2417 goto indexes_ht_error;
2418 }
2419
b8aa1682
JD
2420 ret = create_thread_poll_set(&events, 2);
2421 if (ret < 0) {
2422 goto error_poll_create;
2423 }
2424
2425 ret = lttng_poll_add(&events, relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
2426 if (ret < 0) {
2427 goto error;
2428 }
2429
beaad64c 2430restart:
b8aa1682 2431 while (1) {
beaad64c
DG
2432 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
2433
f385ae0a
MD
2434 health_code_update();
2435
b8aa1682 2436 /* Infinite blocking call, waiting for transmission */
87c1611d 2437 DBG3("Relayd worker thread polling...");
f385ae0a 2438 health_poll_entry();
b8aa1682 2439 ret = lttng_poll_wait(&events, -1);
f385ae0a 2440 health_poll_exit();
b8aa1682
JD
2441 if (ret < 0) {
2442 /*
2443 * Restart interrupted system call.
2444 */
2445 if (errno == EINTR) {
2446 goto restart;
2447 }
2448 goto error;
2449 }
2450
0d9c5d77
DG
2451 nb_fd = ret;
2452
beaad64c
DG
2453 /*
2454 * Process control. The control connection is prioritised so we don't
2455 * starve it with high throughout put tracing data on the data
2456 * connection.
2457 */
b8aa1682
JD
2458 for (i = 0; i < nb_fd; i++) {
2459 /* Fetch once the poll data */
beaad64c
DG
2460 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2461 int pollfd = LTTNG_POLL_GETFD(&events, i);
b8aa1682 2462
f385ae0a
MD
2463 health_code_update();
2464
b8aa1682
JD
2465 /* Thread quit pipe has been closed. Killing thread. */
2466 ret = check_thread_quit_pipe(pollfd, revents);
2467 if (ret) {
095a4ae5
MD
2468 err = 0;
2469 goto exit;
b8aa1682
JD
2470 }
2471
2472 /* Inspect the relay cmd pipe for new connection */
2473 if (pollfd == relay_cmd_pipe[0]) {
2474 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2475 ERR("Relay pipe error");
2476 goto error;
2477 } else if (revents & LPOLLIN) {
2478 DBG("Relay command received");
2479 ret = relay_add_connection(relay_cmd_pipe[0],
2480 &events, relay_connections_ht);
2481 if (ret < 0) {
2482 goto error;
2483 }
2484 }
beaad64c 2485 } else if (revents) {
9d1bbf21 2486 rcu_read_lock();
b8aa1682
JD
2487 lttng_ht_lookup(relay_connections_ht,
2488 (void *)((unsigned long) pollfd),
2489 &iter);
2490 node = lttng_ht_iter_get_node_ulong(&iter);
2491 if (node == NULL) {
2492 DBG2("Relay sock %d not found", pollfd);
9d1bbf21 2493 rcu_read_unlock();
b8aa1682
JD
2494 goto error;
2495 }
2496 relay_connection = caa_container_of(node,
2497 struct relay_command, sock_n);
2498
2499 if (revents & (LPOLLERR)) {
2500 ERR("POLL ERROR");
9d1bbf21
MD
2501 relay_cleanup_poll_connection(&events, pollfd);
2502 relay_del_connection(relay_connections_ht,
d3e2ba59 2503 &iter, relay_connection, sessions_ht);
beaad64c
DG
2504 if (last_seen_data_fd == pollfd) {
2505 last_seen_data_fd = last_notdel_data_fd;
2506 }
b8aa1682
JD
2507 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
2508 DBG("Socket %d hung up", pollfd);
9d1bbf21
MD
2509 relay_cleanup_poll_connection(&events, pollfd);
2510 relay_del_connection(relay_connections_ht,
d3e2ba59 2511 &iter, relay_connection, sessions_ht);
beaad64c
DG
2512 if (last_seen_data_fd == pollfd) {
2513 last_seen_data_fd = last_notdel_data_fd;
2514 }
b8aa1682
JD
2515 } else if (revents & LPOLLIN) {
2516 /* control socket */
2517 if (relay_connection->type == RELAY_CONTROL) {
2518 ret = relay_connection->sock->ops->recvmsg(
2519 relay_connection->sock, &recv_hdr,
7c5aef62 2520 sizeof(struct lttcomm_relayd_hdr), 0);
b8aa1682
JD
2521 /* connection closed */
2522 if (ret <= 0) {
9d1bbf21
MD
2523 relay_cleanup_poll_connection(&events, pollfd);
2524 relay_del_connection(relay_connections_ht,
d3e2ba59 2525 &iter, relay_connection, sessions_ht);
b8aa1682
JD
2526 DBG("Control connection closed with %d", pollfd);
2527 } else {
2528 if (relay_connection->session) {
77c7c900 2529 DBG2("Relay worker receiving data for session : %" PRIu64,
b8aa1682
JD
2530 relay_connection->session->id);
2531 }
2532 ret = relay_process_control(&recv_hdr,
d3e2ba59 2533 relay_connection, relay_ctx);
b8aa1682 2534 if (ret < 0) {
beaad64c 2535 /* Clear the session on error. */
9d1bbf21
MD
2536 relay_cleanup_poll_connection(&events, pollfd);
2537 relay_del_connection(relay_connections_ht,
d3e2ba59 2538 &iter, relay_connection, sessions_ht);
b8aa1682
JD
2539 DBG("Connection closed with %d", pollfd);
2540 }
beaad64c 2541 seen_control = 1;
b8aa1682 2542 }
beaad64c
DG
2543 } else {
2544 /*
2545 * Flag the last seen data fd not deleted. It will be
2546 * used as the last seen fd if any fd gets deleted in
2547 * this first loop.
2548 */
2549 last_notdel_data_fd = pollfd;
2550 }
2551 }
2552 rcu_read_unlock();
2553 }
2554 }
2555
2556 /*
2557 * The last loop handled a control request, go back to poll to make
2558 * sure we prioritise the control socket.
2559 */
2560 if (seen_control) {
2561 continue;
2562 }
2563
2564 if (last_seen_data_fd >= 0) {
2565 for (i = 0; i < nb_fd; i++) {
2566 int pollfd = LTTNG_POLL_GETFD(&events, i);
f385ae0a
MD
2567
2568 health_code_update();
2569
beaad64c
DG
2570 if (last_seen_data_fd == pollfd) {
2571 idx = i;
2572 break;
2573 }
2574 }
2575 }
2576
2577 /* Process data connection. */
2578 for (i = idx + 1; i < nb_fd; i++) {
2579 /* Fetch the poll data. */
2580 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2581 int pollfd = LTTNG_POLL_GETFD(&events, i);
2582
f385ae0a
MD
2583 health_code_update();
2584
beaad64c
DG
2585 /* Skip the command pipe. It's handled in the first loop. */
2586 if (pollfd == relay_cmd_pipe[0]) {
2587 continue;
2588 }
2589
2590 if (revents) {
2591 rcu_read_lock();
2592 lttng_ht_lookup(relay_connections_ht,
2593 (void *)((unsigned long) pollfd),
2594 &iter);
2595 node = lttng_ht_iter_get_node_ulong(&iter);
2596 if (node == NULL) {
2597 /* Skip it. Might be removed before. */
2598 rcu_read_unlock();
2599 continue;
2600 }
2601 relay_connection = caa_container_of(node,
2602 struct relay_command, sock_n);
2603
2604 if (revents & LPOLLIN) {
2605 if (relay_connection->type != RELAY_DATA) {
2606 continue;
2607 }
2608
0a6518b0 2609 ret = relay_process_data(relay_connection);
beaad64c
DG
2610 /* connection closed */
2611 if (ret < 0) {
2612 relay_cleanup_poll_connection(&events, pollfd);
2613 relay_del_connection(relay_connections_ht,
d3e2ba59 2614 &iter, relay_connection, sessions_ht);
beaad64c
DG
2615 DBG("Data connection closed with %d", pollfd);
2616 /*
2617 * Every goto restart call sets the last seen fd where
2618 * here we don't really care since we gracefully
2619 * continue the loop after the connection is deleted.
2620 */
2621 } else {
2622 /* Keep last seen port. */
2623 last_seen_data_fd = pollfd;
2624 rcu_read_unlock();
2625 goto restart;
b8aa1682
JD
2626 }
2627 }
9d1bbf21 2628 rcu_read_unlock();
b8aa1682
JD
2629 }
2630 }
beaad64c 2631 last_seen_data_fd = -1;
b8aa1682
JD
2632 }
2633
f385ae0a
MD
2634 /* Normal exit, no error */
2635 ret = 0;
2636
095a4ae5 2637exit:
b8aa1682
JD
2638error:
2639 lttng_poll_clean(&events);
2640
2641 /* empty the hash table and free the memory */
9d1bbf21 2642 rcu_read_lock();
b8aa1682 2643 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
f385ae0a
MD
2644 health_code_update();
2645
b8aa1682
JD
2646 node = lttng_ht_iter_get_node_ulong(&iter);
2647 if (node) {
2648 relay_connection = caa_container_of(node,
2649 struct relay_command, sock_n);
9d1bbf21 2650 relay_del_connection(relay_connections_ht,
d3e2ba59 2651 &iter, relay_connection, sessions_ht);
b8aa1682 2652 }
b8aa1682 2653 }
94d49140 2654 rcu_read_unlock();
7d2f7452
DG
2655error_poll_create:
2656 lttng_ht_destroy(indexes_ht);
1c20f0e2 2657indexes_ht_error:
b8aa1682 2658 lttng_ht_destroy(relay_connections_ht);
095a4ae5 2659relay_connections_ht_error:
6620da75
DG
2660 /* Close relay cmd pipes */
2661 utils_close_pipe(relay_cmd_pipe);
095a4ae5
MD
2662 if (err) {
2663 DBG("Thread exited with error");
2664 }
b8aa1682 2665 DBG("Worker thread cleanup complete");
095a4ae5 2666 free(data_buffer);
f385ae0a
MD
2667 if (err) {
2668 health_error();
2669 ERR("Health error occurred in %s", __func__);
2670 }
2671 health_unregister(health_relayd);
9d1bbf21 2672 rcu_unregister_thread();
f385ae0a 2673 stop_threads();
b8aa1682
JD
2674 return NULL;
2675}
2676
2677/*
2678 * Create the relay command pipe to wake thread_manage_apps.
2679 * Closed in cleanup().
2680 */
2681static int create_relay_cmd_pipe(void)
2682{
a02de639 2683 int ret;
b8aa1682 2684
a02de639 2685 ret = utils_create_pipe_cloexec(relay_cmd_pipe);
b8aa1682 2686
b8aa1682
JD
2687 return ret;
2688}
2689
2690/*
2691 * main
2692 */
2693int main(int argc, char **argv)
2694{
2695 int ret = 0;
2696 void *status;
d3e2ba59 2697 struct relay_local_data *relay_ctx;
b8aa1682
JD
2698
2699 /* Create thread quit pipe */
2700 if ((ret = init_thread_quit_pipe()) < 0) {
2701 goto error;
2702 }
2703
2704 /* Parse arguments */
2705 progname = argv[0];
cd60b05a 2706 if ((ret = set_options(argc, argv)) < 0) {
a02de639 2707 goto exit;
b8aa1682
JD
2708 }
2709
2710 if ((ret = set_signal_handler()) < 0) {
2711 goto exit;
2712 }
2713
4d513a50
DG
2714 /* Try to create directory if -o, --output is specified. */
2715 if (opt_output_path) {
994fa64f
DG
2716 if (*opt_output_path != '/') {
2717 ERR("Please specify an absolute path for -o, --output PATH");
2718 goto exit;
2719 }
2720
4d513a50
DG
2721 ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG);
2722 if (ret < 0) {
2723 ERR("Unable to create %s", opt_output_path);
2724 goto exit;
2725 }
2726 }
2727
b8aa1682
JD
2728 /* Daemonize */
2729 if (opt_daemon) {
2730 ret = daemon(0, 0);
2731 if (ret < 0) {
2732 PERROR("daemon");
a02de639 2733 goto exit;
b8aa1682
JD
2734 }
2735 }
2736
1c20f0e2
JD
2737 /* We need those values for the file/dir creation. */
2738 relayd_uid = getuid();
2739 relayd_gid = getgid();
b8aa1682 2740
1c20f0e2
JD
2741 /* Check if daemon is UID = 0 */
2742 if (relayd_uid == 0) {
8d5c808e 2743 if (control_uri->port < 1024 || data_uri->port < 1024 || live_uri->port < 1024) {
b8aa1682
JD
2744 ERR("Need to be root to use ports < 1024");
2745 ret = -1;
a02de639 2746 goto exit;
b8aa1682
JD
2747 }
2748 }
2749
2750 /* Setup the thread apps communication pipe. */
2751 if ((ret = create_relay_cmd_pipe()) < 0) {
2752 goto exit;
2753 }
2754
2755 /* Init relay command queue. */
2756 cds_wfq_init(&relay_cmd_queue.queue);
2757
2758 /* Set up max poll set size */
2759 lttng_poll_set_max_size();
2760
554831e7
MD
2761 /* Initialize communication library */
2762 lttcomm_init();
2763
d3e2ba59
JD
2764 relay_ctx = zmalloc(sizeof(struct relay_local_data));
2765 if (!relay_ctx) {
2766 PERROR("relay_ctx");
2767 goto exit;
2768 }
2769
2770 /* tables of sessions indexed by session ID */
2771 relay_ctx->sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2772 if (!relay_ctx->sessions_ht) {
2773 goto exit_relay_ctx_sessions;
2774 }
2775
2776 /* tables of streams indexed by stream ID */
2777 relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2778 if (!relay_streams_ht) {
2779 goto exit_relay_ctx_streams;
2780 }
2781
2782 /* tables of streams indexed by stream ID */
92c6ca54
DG
2783 viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2784 if (!viewer_streams_ht) {
d3e2ba59
JD
2785 goto exit_relay_ctx_viewer_streams;
2786 }
2787
55706a7d
MD
2788 /* Initialize thread health monitoring */
2789 health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
2790 if (!health_relayd) {
2791 PERROR("health_app_create error");
2792 goto exit_health_app_create;
2793 }
2794
65931c8b
MD
2795 ret = utils_create_pipe(health_quit_pipe);
2796 if (ret < 0) {
2797 goto error_health_pipe;
2798 }
2799
2800 /* Create thread to manage the client socket */
2801 ret = pthread_create(&health_thread, NULL,
2802 thread_manage_health, (void *) NULL);
2803 if (ret != 0) {
2804 PERROR("pthread_create health");
2805 goto health_error;
2806 }
2807
b8aa1682
JD
2808 /* Setup the dispatcher thread */
2809 ret = pthread_create(&dispatcher_thread, NULL,
2810 relay_thread_dispatcher, (void *) NULL);
2811 if (ret != 0) {
2812 PERROR("pthread_create dispatcher");
2813 goto exit_dispatcher;
2814 }
2815
2816 /* Setup the worker thread */
2817 ret = pthread_create(&worker_thread, NULL,
d3e2ba59 2818 relay_thread_worker, (void *) relay_ctx);
b8aa1682
JD
2819 if (ret != 0) {
2820 PERROR("pthread_create worker");
2821 goto exit_worker;
2822 }
2823
2824 /* Setup the listener thread */
2825 ret = pthread_create(&listener_thread, NULL,
2826 relay_thread_listener, (void *) NULL);
2827 if (ret != 0) {
2828 PERROR("pthread_create listener");
2829 goto exit_listener;
2830 }
2831
42415026 2832 ret = live_start_threads(live_uri, relay_ctx, thread_quit_pipe);
d3e2ba59
JD
2833 if (ret != 0) {
2834 ERR("Starting live viewer threads");
50138f51 2835 goto exit_live;
d3e2ba59
JD
2836 }
2837
50138f51 2838exit_live:
b8aa1682
JD
2839 ret = pthread_join(listener_thread, &status);
2840 if (ret != 0) {
2841 PERROR("pthread_join");
2842 goto error; /* join error, exit without cleanup */
2843 }
2844
50138f51 2845exit_listener:
b8aa1682
JD
2846 ret = pthread_join(worker_thread, &status);
2847 if (ret != 0) {
2848 PERROR("pthread_join");
2849 goto error; /* join error, exit without cleanup */
2850 }
2851
50138f51 2852exit_worker:
b8aa1682
JD
2853 ret = pthread_join(dispatcher_thread, &status);
2854 if (ret != 0) {
2855 PERROR("pthread_join");
2856 goto error; /* join error, exit without cleanup */
2857 }
42415026 2858
50138f51 2859exit_dispatcher:
65931c8b
MD
2860 ret = pthread_join(health_thread, &status);
2861 if (ret != 0) {
2862 PERROR("pthread_join health thread");
2863 goto error; /* join error, exit without cleanup */
2864 }
2865
bd11e201
MD
2866 /*
2867 * Stop live threads only after joining other threads.
2868 */
2869 live_stop_threads();
2870
65931c8b
MD
2871health_error:
2872 utils_close_pipe(health_quit_pipe);
2873
2874error_health_pipe:
55706a7d
MD
2875 health_app_destroy(health_relayd);
2876
2877exit_health_app_create:
92c6ca54 2878 lttng_ht_destroy(viewer_streams_ht);
d3e2ba59
JD
2879
2880exit_relay_ctx_viewer_streams:
2881 lttng_ht_destroy(relay_streams_ht);
2882
2883exit_relay_ctx_streams:
2884 lttng_ht_destroy(relay_ctx->sessions_ht);
2885
2886exit_relay_ctx_sessions:
2887 free(relay_ctx);
b8aa1682
JD
2888
2889exit:
2890 cleanup();
2891 if (!ret) {
2892 exit(EXIT_SUCCESS);
2893 }
a02de639 2894
b8aa1682
JD
2895error:
2896 exit(EXIT_FAILURE);
2897}
This page took 0.176632 seconds and 5 git commands to generate.