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