Fix: rlim_cur and rlim_max printing causes a warning on some archs
[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 <sys/resource.h>
38 #include <inttypes.h>
39 #include <urcu/futex.h>
40 #include <urcu/uatomic.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <ctype.h>
44
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/compat/getenv.h>
51 #include <common/defaults.h>
52 #include <common/daemonize.h>
53 #include <common/futex.h>
54 #include <common/sessiond-comm/sessiond-comm.h>
55 #include <common/sessiond-comm/inet.h>
56 #include <common/sessiond-comm/relayd.h>
57 #include <common/uri.h>
58 #include <common/utils.h>
59 #include <common/align.h>
60 #include <common/config/session-config.h>
61 #include <common/dynamic-buffer.h>
62 #include <common/buffer-view.h>
63 #include <common/fd-tracker/utils.h>
64 #include <urcu/rculist.h>
65
66 #include "cmd.h"
67 #include "ctf-trace.h"
68 #include "index.h"
69 #include "utils.h"
70 #include "lttng-relayd.h"
71 #include "live.h"
72 #include "health-relayd.h"
73 #include "testpoint.h"
74 #include "viewer-stream.h"
75 #include "session.h"
76 #include "stream.h"
77 #include "connection.h"
78 #include "tracefile-array.h"
79 #include "tcp_keep_alive.h"
80
81 static const char *help_msg =
82 #ifdef LTTNG_EMBED_HELP
83 #include <lttng-relayd.8.h>
84 #else
85 NULL
86 #endif
87 ;
88
89 enum relay_connection_status {
90 RELAY_CONNECTION_STATUS_OK,
91 /* An error occured while processing an event on the connection. */
92 RELAY_CONNECTION_STATUS_ERROR,
93 /* Connection closed/shutdown cleanly. */
94 RELAY_CONNECTION_STATUS_CLOSED,
95 };
96
97 /* command line options */
98 char *opt_output_path;
99 static int opt_daemon, opt_background;
100
101 /*
102 * We need to wait for listener and live listener threads, as well as
103 * health check thread, before being ready to signal readiness.
104 */
105 #define NR_LTTNG_RELAY_READY 3
106 static int lttng_relay_ready = NR_LTTNG_RELAY_READY;
107
108 /* Size of receive buffer. */
109 #define RECV_DATA_BUFFER_SIZE 65536
110 #define FILE_COPY_BUFFER_SIZE 65536
111
112 static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */
113 static pid_t child_ppid; /* Internal parent PID use with daemonize. */
114
115 static struct lttng_uri *control_uri;
116 static struct lttng_uri *data_uri;
117 static struct lttng_uri *live_uri;
118
119 const char *progname;
120
121 const char *tracing_group_name = DEFAULT_TRACING_GROUP;
122 static int tracing_group_name_override;
123
124 const char * const config_section_name = "relayd";
125
126 /*
127 * Quit pipe for all threads. This permits a single cancellation point
128 * for all threads when receiving an event on the pipe.
129 */
130 int thread_quit_pipe[2] = { -1, -1 };
131
132 /*
133 * This pipe is used to inform the worker thread that a command is queued and
134 * ready to be processed.
135 */
136 static int relay_conn_pipe[2] = { -1, -1 };
137
138 /* Shared between threads */
139 static int dispatch_thread_exit;
140
141 static pthread_t listener_thread;
142 static pthread_t dispatcher_thread;
143 static pthread_t worker_thread;
144 static pthread_t health_thread;
145
146 /*
147 * last_relay_stream_id_lock protects last_relay_stream_id increment
148 * atomicity on 32-bit architectures.
149 */
150 static pthread_mutex_t last_relay_stream_id_lock = PTHREAD_MUTEX_INITIALIZER;
151 static uint64_t last_relay_stream_id;
152
153 /*
154 * Relay command queue.
155 *
156 * The relay_thread_listener and relay_thread_dispatcher communicate with this
157 * queue.
158 */
159 static struct relay_conn_queue relay_conn_queue;
160
161 /* Cap of file desriptors to be in simultaneous use by the relay daemon. */
162 static unsigned int lttng_opt_fd_pool_size = -1;
163
164 /* Global relay stream hash table. */
165 struct lttng_ht *relay_streams_ht;
166
167 /* Global relay viewer stream hash table. */
168 struct lttng_ht *viewer_streams_ht;
169
170 /* Global relay sessions hash table. */
171 struct lttng_ht *sessions_ht;
172
173 /* Relayd health monitoring */
174 struct health_app *health_relayd;
175
176 /* Global fd tracker. */
177 struct fd_tracker *the_fd_tracker;
178
179 static struct option long_options[] = {
180 { "control-port", 1, 0, 'C', },
181 { "data-port", 1, 0, 'D', },
182 { "live-port", 1, 0, 'L', },
183 { "daemonize", 0, 0, 'd', },
184 { "background", 0, 0, 'b', },
185 { "group", 1, 0, 'g', },
186 { "fd-pool-size", 1, 0, '\0', },
187 { "help", 0, 0, 'h', },
188 { "output", 1, 0, 'o', },
189 { "verbose", 0, 0, 'v', },
190 { "config", 1, 0, 'f' },
191 { "version", 0, 0, 'V' },
192 { NULL, 0, 0, 0, },
193 };
194
195 static const char *config_ignore_options[] = { "help", "config", "version" };
196
197 /*
198 * Take an option from the getopt output and set it in the right variable to be
199 * used later.
200 *
201 * Return 0 on success else a negative value.
202 */
203 static int set_option(int opt, const char *arg, const char *optname)
204 {
205 int ret;
206
207 switch (opt) {
208 case 0:
209 if (!strcmp(optname, "fd-pool-size")) {
210 unsigned long v;
211
212 errno = 0;
213 v = strtoul(arg, NULL, 0);
214 if (errno != 0 || !isdigit(arg[0])) {
215 ERR("Wrong value in --fd-pool-size parameter: %s", arg);
216 ret = -1;
217 goto end;
218 }
219 if (v >= UINT_MAX) {
220 ERR("File descriptor cap overflow in --fd-pool-size parameter: %s", arg);
221 ret = -1;
222 goto end;
223 }
224 lttng_opt_fd_pool_size = (unsigned int) v;
225 } else {
226 fprintf(stderr, "unknown option %s", optname);
227 if (arg) {
228 fprintf(stderr, " with arg %s\n", arg);
229 }
230 }
231 break;
232 case 'C':
233 if (lttng_is_setuid_setgid()) {
234 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
235 "-C, --control-port");
236 } else {
237 ret = uri_parse(arg, &control_uri);
238 if (ret < 0) {
239 ERR("Invalid control URI specified");
240 goto end;
241 }
242 if (control_uri->port == 0) {
243 control_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
244 }
245 }
246 break;
247 case 'D':
248 if (lttng_is_setuid_setgid()) {
249 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
250 "-D, -data-port");
251 } else {
252 ret = uri_parse(arg, &data_uri);
253 if (ret < 0) {
254 ERR("Invalid data URI specified");
255 goto end;
256 }
257 if (data_uri->port == 0) {
258 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
259 }
260 }
261 break;
262 case 'L':
263 if (lttng_is_setuid_setgid()) {
264 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
265 "-L, -live-port");
266 } else {
267 ret = uri_parse(arg, &live_uri);
268 if (ret < 0) {
269 ERR("Invalid live URI specified");
270 goto end;
271 }
272 if (live_uri->port == 0) {
273 live_uri->port = DEFAULT_NETWORK_VIEWER_PORT;
274 }
275 }
276 break;
277 case 'd':
278 opt_daemon = 1;
279 break;
280 case 'b':
281 opt_background = 1;
282 break;
283 case 'g':
284 if (lttng_is_setuid_setgid()) {
285 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
286 "-g, --group");
287 } else {
288 tracing_group_name = strdup(arg);
289 if (tracing_group_name == NULL) {
290 ret = -errno;
291 PERROR("strdup");
292 goto end;
293 }
294 tracing_group_name_override = 1;
295 }
296 break;
297 case 'h':
298 ret = utils_show_help(8, "lttng-relayd", help_msg);
299 if (ret) {
300 ERR("Cannot show --help for `lttng-relayd`");
301 perror("exec");
302 }
303 exit(EXIT_FAILURE);
304 case 'V':
305 fprintf(stdout, "%s\n", VERSION);
306 exit(EXIT_SUCCESS);
307 case 'o':
308 if (lttng_is_setuid_setgid()) {
309 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
310 "-o, --output");
311 } else {
312 ret = asprintf(&opt_output_path, "%s", arg);
313 if (ret < 0) {
314 ret = -errno;
315 PERROR("asprintf opt_output_path");
316 goto end;
317 }
318 }
319 break;
320 case 'v':
321 /* Verbose level can increase using multiple -v */
322 if (arg) {
323 lttng_opt_verbose = config_parse_value(arg);
324 } else {
325 /* Only 3 level of verbosity (-vvv). */
326 if (lttng_opt_verbose < 3) {
327 lttng_opt_verbose += 1;
328 }
329 }
330 break;
331 default:
332 /* Unknown option or other error.
333 * Error is printed by getopt, just return */
334 ret = -1;
335 goto end;
336 }
337
338 /* All good. */
339 ret = 0;
340
341 end:
342 return ret;
343 }
344
345 /*
346 * config_entry_handler_cb used to handle options read from a config file.
347 * See config_entry_handler_cb comment in common/config/session-config.h for the
348 * return value conventions.
349 */
350 static int config_entry_handler(const struct config_entry *entry, void *unused)
351 {
352 int ret = 0, i;
353
354 if (!entry || !entry->name || !entry->value) {
355 ret = -EINVAL;
356 goto end;
357 }
358
359 /* Check if the option is to be ignored */
360 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
361 if (!strcmp(entry->name, config_ignore_options[i])) {
362 goto end;
363 }
364 }
365
366 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) {
367 /* Ignore if entry name is not fully matched. */
368 if (strcmp(entry->name, long_options[i].name)) {
369 continue;
370 }
371
372 /*
373 * If the option takes no argument on the command line,
374 * we have to check if the value is "true". We support
375 * non-zero numeric values, true, on and yes.
376 */
377 if (!long_options[i].has_arg) {
378 ret = config_parse_value(entry->value);
379 if (ret <= 0) {
380 if (ret) {
381 WARN("Invalid configuration value \"%s\" for option %s",
382 entry->value, entry->name);
383 }
384 /* False, skip boolean config option. */
385 goto end;
386 }
387 }
388
389 ret = set_option(long_options[i].val, entry->value, entry->name);
390 goto end;
391 }
392
393 WARN("Unrecognized option \"%s\" in daemon configuration file.",
394 entry->name);
395
396 end:
397 return ret;
398 }
399
400 static int set_fd_pool_size(void)
401 {
402 int ret = 0;
403 struct rlimit rlimit;
404
405 ret = getrlimit(RLIMIT_NOFILE, &rlimit);
406 if (ret) {
407 PERROR("Failed to get file descriptor limit");
408 ret = -1;
409 goto end;
410 }
411
412 DBG("File descriptor count limits are %" PRIu64 " (soft) and %" PRIu64 " (hard)",
413 (uint64_t) rlimit.rlim_cur, (uint64_t) rlimit.rlim_max);
414 if (lttng_opt_fd_pool_size == -1) {
415 /* Use default value (soft limit - reserve). */
416 if (rlimit.rlim_cur < DEFAULT_RELAYD_MIN_FD_POOL_SIZE) {
417 ERR("The process' file number limit is too low (%" PRIu64 "). The process' file number limit must be set to at least %i.",
418 (uint64_t) rlimit.rlim_cur,
419 DEFAULT_RELAYD_MIN_FD_POOL_SIZE);
420 ret = -1;
421 goto end;
422 }
423 lttng_opt_fd_pool_size = rlimit.rlim_cur -
424 DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE;
425 goto end;
426 }
427
428 if (lttng_opt_fd_pool_size < DEFAULT_RELAYD_MIN_FD_POOL_SIZE) {
429 ERR("File descriptor pool size must be set to at least %d",
430 DEFAULT_RELAYD_MIN_FD_POOL_SIZE);
431 ret = -1;
432 goto end;
433 }
434
435 if (lttng_opt_fd_pool_size > rlimit.rlim_cur) {
436 ERR("File descriptor pool size argument (%u) exceeds the process' soft limit (%lu).",
437 lttng_opt_fd_pool_size, rlimit.rlim_cur);
438 ret = -1;
439 goto end;
440 }
441
442
443 DBG("File descriptor pool size argument (%u) adjusted to %u to accomodate transient fd uses",
444 lttng_opt_fd_pool_size,
445 lttng_opt_fd_pool_size - DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE);
446 lttng_opt_fd_pool_size -= DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE;
447 end:
448 return ret;
449 }
450
451 static int set_options(int argc, char **argv)
452 {
453 int c, ret = 0, option_index = 0, retval = 0;
454 int orig_optopt = optopt, orig_optind = optind;
455 char *default_address, *optstring;
456 const char *config_path = NULL;
457
458 optstring = utils_generate_optstring(long_options,
459 sizeof(long_options) / sizeof(struct option));
460 if (!optstring) {
461 retval = -ENOMEM;
462 goto exit;
463 }
464
465 /* Check for the --config option */
466
467 while ((c = getopt_long(argc, argv, optstring, long_options,
468 &option_index)) != -1) {
469 if (c == '?') {
470 retval = -EINVAL;
471 goto exit;
472 } else if (c != 'f') {
473 continue;
474 }
475
476 if (lttng_is_setuid_setgid()) {
477 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
478 "-f, --config");
479 } else {
480 config_path = utils_expand_path(optarg);
481 if (!config_path) {
482 ERR("Failed to resolve path: %s", optarg);
483 }
484 }
485 }
486
487 ret = config_get_section_entries(config_path, config_section_name,
488 config_entry_handler, NULL);
489 if (ret) {
490 if (ret > 0) {
491 ERR("Invalid configuration option at line %i", ret);
492 }
493 retval = -1;
494 goto exit;
495 }
496
497 /* Reset getopt's global state */
498 optopt = orig_optopt;
499 optind = orig_optind;
500 while (1) {
501 c = getopt_long(argc, argv, optstring, long_options, &option_index);
502 if (c == -1) {
503 break;
504 }
505
506 ret = set_option(c, optarg, long_options[option_index].name);
507 if (ret < 0) {
508 retval = -1;
509 goto exit;
510 }
511 }
512
513 /* assign default values */
514 if (control_uri == NULL) {
515 ret = asprintf(&default_address,
516 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d",
517 DEFAULT_NETWORK_CONTROL_PORT);
518 if (ret < 0) {
519 PERROR("asprintf default data address");
520 retval = -1;
521 goto exit;
522 }
523
524 ret = uri_parse(default_address, &control_uri);
525 free(default_address);
526 if (ret < 0) {
527 ERR("Invalid control URI specified");
528 retval = -1;
529 goto exit;
530 }
531 }
532 if (data_uri == NULL) {
533 ret = asprintf(&default_address,
534 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d",
535 DEFAULT_NETWORK_DATA_PORT);
536 if (ret < 0) {
537 PERROR("asprintf default data address");
538 retval = -1;
539 goto exit;
540 }
541
542 ret = uri_parse(default_address, &data_uri);
543 free(default_address);
544 if (ret < 0) {
545 ERR("Invalid data URI specified");
546 retval = -1;
547 goto exit;
548 }
549 }
550 if (live_uri == NULL) {
551 ret = asprintf(&default_address,
552 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d",
553 DEFAULT_NETWORK_VIEWER_PORT);
554 if (ret < 0) {
555 PERROR("asprintf default viewer control address");
556 retval = -1;
557 goto exit;
558 }
559
560 ret = uri_parse(default_address, &live_uri);
561 free(default_address);
562 if (ret < 0) {
563 ERR("Invalid viewer control URI specified");
564 retval = -1;
565 goto exit;
566 }
567 }
568 ret = set_fd_pool_size();
569 if (ret) {
570 retval = -1;
571 goto exit;
572 }
573
574 exit:
575 free(optstring);
576 return retval;
577 }
578
579 static void print_global_objects(void)
580 {
581 print_viewer_streams();
582 print_relay_streams();
583 print_sessions();
584 }
585
586 /*
587 * Cleanup the daemon
588 */
589 static void relayd_cleanup(void)
590 {
591 print_global_objects();
592
593 DBG("Cleaning up");
594
595 if (viewer_streams_ht)
596 lttng_ht_destroy(viewer_streams_ht);
597 if (relay_streams_ht)
598 lttng_ht_destroy(relay_streams_ht);
599 if (sessions_ht)
600 lttng_ht_destroy(sessions_ht);
601
602 /* free the dynamically allocated opt_output_path */
603 free(opt_output_path);
604
605 /* Close thread quit pipes */
606 (void) fd_tracker_util_pipe_close(the_fd_tracker, thread_quit_pipe);
607
608 uri_free(control_uri);
609 uri_free(data_uri);
610 /* Live URI is freed in the live thread. */
611
612 if (tracing_group_name_override) {
613 free((void *) tracing_group_name);
614 }
615 }
616
617 /*
618 * Write to writable pipe used to notify a thread.
619 */
620 static int notify_thread_pipe(int wpipe)
621 {
622 ssize_t ret;
623
624 ret = lttng_write(wpipe, "!", 1);
625 if (ret < 1) {
626 PERROR("write poll pipe");
627 goto end;
628 }
629 ret = 0;
630 end:
631 return ret;
632 }
633
634 static int notify_health_quit_pipe(int *pipe)
635 {
636 ssize_t ret;
637
638 ret = lttng_write(pipe[1], "4", 1);
639 if (ret < 1) {
640 PERROR("write relay health quit");
641 goto end;
642 }
643 ret = 0;
644 end:
645 return ret;
646 }
647
648 /*
649 * Stop all relayd and relayd-live threads.
650 */
651 int lttng_relay_stop_threads(void)
652 {
653 int retval = 0;
654
655 /* Stopping all threads */
656 DBG("Terminating all threads");
657 if (notify_thread_pipe(thread_quit_pipe[1])) {
658 ERR("write error on thread quit pipe");
659 retval = -1;
660 }
661
662 if (notify_health_quit_pipe(health_quit_pipe)) {
663 ERR("write error on health quit pipe");
664 }
665
666 /* Dispatch thread */
667 CMM_STORE_SHARED(dispatch_thread_exit, 1);
668 futex_nto1_wake(&relay_conn_queue.futex);
669
670 if (relayd_live_stop()) {
671 ERR("Error stopping live threads");
672 retval = -1;
673 }
674 return retval;
675 }
676
677 /*
678 * Signal handler for the daemon
679 *
680 * Simply stop all worker threads, leaving main() return gracefully after
681 * joining all threads and calling cleanup().
682 */
683 static void sighandler(int sig)
684 {
685 switch (sig) {
686 case SIGINT:
687 DBG("SIGINT caught");
688 if (lttng_relay_stop_threads()) {
689 ERR("Error stopping threads");
690 }
691 break;
692 case SIGTERM:
693 DBG("SIGTERM caught");
694 if (lttng_relay_stop_threads()) {
695 ERR("Error stopping threads");
696 }
697 break;
698 case SIGUSR1:
699 CMM_STORE_SHARED(recv_child_signal, 1);
700 break;
701 default:
702 break;
703 }
704 }
705
706 /*
707 * Setup signal handler for :
708 * SIGINT, SIGTERM, SIGPIPE
709 */
710 static int set_signal_handler(void)
711 {
712 int ret = 0;
713 struct sigaction sa;
714 sigset_t sigset;
715
716 if ((ret = sigemptyset(&sigset)) < 0) {
717 PERROR("sigemptyset");
718 return ret;
719 }
720
721 sa.sa_mask = sigset;
722 sa.sa_flags = 0;
723
724 sa.sa_handler = sighandler;
725 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
726 PERROR("sigaction");
727 return ret;
728 }
729
730 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
731 PERROR("sigaction");
732 return ret;
733 }
734
735 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
736 PERROR("sigaction");
737 return ret;
738 }
739
740 sa.sa_handler = SIG_IGN;
741 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
742 PERROR("sigaction");
743 return ret;
744 }
745
746 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
747
748 return ret;
749 }
750
751 void lttng_relay_notify_ready(void)
752 {
753 /* Notify the parent of the fork() process that we are ready. */
754 if (opt_daemon || opt_background) {
755 if (uatomic_sub_return(&lttng_relay_ready, 1) == 0) {
756 kill(child_ppid, SIGUSR1);
757 }
758 }
759 }
760
761 /*
762 * Init thread quit pipe.
763 *
764 * Return -1 on error or 0 if all pipes are created.
765 */
766 static int init_thread_quit_pipe(void)
767 {
768 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
769 "Quit pipe", thread_quit_pipe);
770 }
771
772 /*
773 * Init health quit pipe.
774 *
775 * Return -1 on error or 0 if all pipes are created.
776 */
777 static int init_health_quit_pipe(void)
778 {
779 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
780 "Health quit pipe", health_quit_pipe);
781 }
782
783 /*
784 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
785 */
786 static int create_named_thread_poll_set(struct lttng_poll_event *events,
787 int size, const char *name)
788 {
789 int ret;
790
791 if (events == NULL || size == 0) {
792 ret = -1;
793 goto error;
794 }
795
796 ret = fd_tracker_util_poll_create(the_fd_tracker,
797 name, events, 1, LTTNG_CLOEXEC);
798
799 /* Add quit pipe */
800 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
801 if (ret < 0) {
802 goto error;
803 }
804
805 return 0;
806
807 error:
808 return ret;
809 }
810
811 /*
812 * Check if the thread quit pipe was triggered.
813 *
814 * Return 1 if it was triggered else 0;
815 */
816 static int check_thread_quit_pipe(int fd, uint32_t events)
817 {
818 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
819 return 1;
820 }
821
822 return 0;
823 }
824
825 static int create_sock(void *data, int *out_fd)
826 {
827 int ret;
828 struct lttcomm_sock *sock = data;
829
830 ret = lttcomm_create_sock(sock);
831 if (ret < 0) {
832 goto end;
833 }
834
835 *out_fd = sock->fd;
836 end:
837 return ret;
838 }
839
840 static int close_sock(void *data, int *in_fd)
841 {
842 struct lttcomm_sock *sock = data;
843
844 return sock->ops->close(sock);
845 }
846
847 static int accept_sock(void *data, int *out_fd)
848 {
849 int ret = 0;
850 /* Socks is an array of in_sock, out_sock. */
851 struct lttcomm_sock **socks = data;
852 struct lttcomm_sock *in_sock = socks[0];
853
854 socks[1] = in_sock->ops->accept(in_sock);
855 if (!socks[1]) {
856 ret = -1;
857 goto end;
858 }
859 *out_fd = socks[1]->fd;
860 end:
861 return ret;
862 }
863
864 /*
865 * Create and init socket from uri.
866 */
867 static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri,
868 const char *name)
869 {
870 int ret, sock_fd;
871 struct lttcomm_sock *sock = NULL;
872 char uri_str[PATH_MAX];
873 char *formated_name = NULL;
874
875 sock = lttcomm_alloc_sock_from_uri(uri);
876 if (sock == NULL) {
877 ERR("Allocating socket");
878 goto error;
879 }
880
881 /*
882 * Don't fail to create the socket if the name can't be built as it is
883 * only used for debugging purposes.
884 */
885 ret = uri_to_str_url(uri, uri_str, sizeof(uri_str));
886 uri_str[sizeof(uri_str) - 1] = '\0';
887 if (ret >= 0) {
888 ret = asprintf(&formated_name, "%s socket @ %s", name,
889 uri_str);
890 if (ret < 0) {
891 formated_name = NULL;
892 }
893 }
894
895 ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock_fd,
896 (const char **) (formated_name ? &formated_name : NULL),
897 1, create_sock, sock);
898 free(formated_name);
899 DBG("Listening on %s socket %d", name, sock->fd);
900
901 ret = sock->ops->bind(sock);
902 if (ret < 0) {
903 PERROR("Failed to bind socket");
904 goto error;
905 }
906
907 ret = sock->ops->listen(sock, -1);
908 if (ret < 0) {
909 goto error;
910
911 }
912
913 return sock;
914
915 error:
916 if (sock) {
917 lttcomm_destroy_sock(sock);
918 }
919 return NULL;
920 }
921
922 static
923 struct lttcomm_sock *accept_relayd_sock(struct lttcomm_sock *listening_sock,
924 const char *name)
925 {
926 int out_fd, ret;
927 struct lttcomm_sock *socks[2] = { listening_sock, NULL };
928 struct lttcomm_sock *new_sock = NULL;
929
930 ret = fd_tracker_open_unsuspendable_fd(
931 the_fd_tracker, &out_fd,
932 (const char **) &name,
933 1, accept_sock, &socks);
934 if (ret) {
935 goto end;
936 }
937 new_sock = socks[1];
938 DBG("%s accepted, socket %d", name, new_sock->fd);
939 end:
940 return new_sock;
941 }
942
943 /*
944 * This thread manages the listening for new connections on the network
945 */
946 static void *relay_thread_listener(void *data)
947 {
948 int i, ret, pollfd, err = -1;
949 uint32_t revents, nb_fd;
950 struct lttng_poll_event events;
951 struct lttcomm_sock *control_sock, *data_sock;
952
953 DBG("[thread] Relay listener started");
954
955 health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER);
956
957 health_code_update();
958
959 control_sock = relay_socket_create(control_uri, "Control listener");
960 if (!control_sock) {
961 goto error_sock_control;
962 }
963
964 data_sock = relay_socket_create(data_uri, "Data listener");
965 if (!data_sock) {
966 goto error_sock_relay;
967 }
968
969 /*
970 * Pass 3 as size here for the thread quit pipe, control and
971 * data socket.
972 */
973 ret = create_named_thread_poll_set(&events, 3, "Listener thread epoll");
974 if (ret < 0) {
975 goto error_create_poll;
976 }
977
978 /* Add the control socket */
979 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
980 if (ret < 0) {
981 goto error_poll_add;
982 }
983
984 /* Add the data socket */
985 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
986 if (ret < 0) {
987 goto error_poll_add;
988 }
989
990 lttng_relay_notify_ready();
991
992 if (testpoint(relayd_thread_listener)) {
993 goto error_testpoint;
994 }
995
996 while (1) {
997 health_code_update();
998
999 DBG("Listener accepting connections");
1000
1001 restart:
1002 health_poll_entry();
1003 ret = lttng_poll_wait(&events, -1);
1004 health_poll_exit();
1005 if (ret < 0) {
1006 /*
1007 * Restart interrupted system call.
1008 */
1009 if (errno == EINTR) {
1010 goto restart;
1011 }
1012 goto error;
1013 }
1014
1015 nb_fd = ret;
1016
1017 DBG("Relay new connection received");
1018 for (i = 0; i < nb_fd; i++) {
1019 health_code_update();
1020
1021 /* Fetch once the poll data */
1022 revents = LTTNG_POLL_GETEV(&events, i);
1023 pollfd = LTTNG_POLL_GETFD(&events, i);
1024
1025 if (!revents) {
1026 /*
1027 * No activity for this FD (poll
1028 * implementation).
1029 */
1030 continue;
1031 }
1032
1033 /* Thread quit pipe has been closed. Killing thread. */
1034 ret = check_thread_quit_pipe(pollfd, revents);
1035 if (ret) {
1036 err = 0;
1037 goto exit;
1038 }
1039
1040 if (revents & LPOLLIN) {
1041 /*
1042 * A new connection is requested, therefore a
1043 * sessiond/consumerd connection is allocated in
1044 * this thread, enqueued to a global queue and
1045 * dequeued (and freed) in the worker thread.
1046 */
1047 int val = 1;
1048 struct relay_connection *new_conn;
1049 struct lttcomm_sock *newsock = NULL;
1050 enum connection_type type;
1051
1052 if (pollfd == data_sock->fd) {
1053 type = RELAY_DATA;
1054 newsock = accept_relayd_sock(data_sock,
1055 "Data socket to relayd");
1056 } else {
1057 assert(pollfd == control_sock->fd);
1058 type = RELAY_CONTROL;
1059 newsock = accept_relayd_sock(control_sock,
1060 "Control socket to relayd");
1061 }
1062 if (!newsock) {
1063 PERROR("accepting sock");
1064 goto error;
1065 }
1066
1067 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
1068 sizeof(val));
1069 if (ret < 0) {
1070 PERROR("setsockopt inet");
1071 lttcomm_destroy_sock(newsock);
1072 goto error;
1073 }
1074
1075 ret = socket_apply_keep_alive_config(newsock->fd);
1076 if (ret < 0) {
1077 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1078 newsock->fd);
1079 lttcomm_destroy_sock(newsock);
1080 goto error;
1081 }
1082
1083 new_conn = connection_create(newsock, type);
1084 if (!new_conn) {
1085 lttcomm_destroy_sock(newsock);
1086 goto error;
1087 }
1088
1089 /* Enqueue request for the dispatcher thread. */
1090 cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail,
1091 &new_conn->qnode);
1092
1093 /*
1094 * Wake the dispatch queue futex.
1095 * Implicit memory barrier with the
1096 * exchange in cds_wfcq_enqueue.
1097 */
1098 futex_nto1_wake(&relay_conn_queue.futex);
1099 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1100 ERR("socket poll error");
1101 goto error;
1102 } else {
1103 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1104 goto error;
1105 }
1106 }
1107 }
1108
1109 exit:
1110 error:
1111 error_poll_add:
1112 error_testpoint:
1113 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
1114 error_create_poll:
1115 if (data_sock->fd >= 0) {
1116 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker,
1117 &data_sock->fd, 1, close_sock,
1118 data_sock);
1119 if (ret) {
1120 PERROR("close");
1121 }
1122 }
1123 lttcomm_destroy_sock(data_sock);
1124 error_sock_relay:
1125 if (control_sock->fd >= 0) {
1126 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker,
1127 &control_sock->fd, 1, close_sock,
1128 control_sock);
1129 if (ret) {
1130 PERROR("close");
1131 }
1132 }
1133 lttcomm_destroy_sock(control_sock);
1134 error_sock_control:
1135 if (err) {
1136 health_error();
1137 ERR("Health error occurred in %s", __func__);
1138 }
1139 health_unregister(health_relayd);
1140 DBG("Relay listener thread cleanup complete");
1141 lttng_relay_stop_threads();
1142 return NULL;
1143 }
1144
1145 /*
1146 * This thread manages the dispatching of the requests to worker threads
1147 */
1148 static void *relay_thread_dispatcher(void *data)
1149 {
1150 int err = -1;
1151 ssize_t ret;
1152 struct cds_wfcq_node *node;
1153 struct relay_connection *new_conn = NULL;
1154
1155 DBG("[thread] Relay dispatcher started");
1156
1157 health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER);
1158
1159 if (testpoint(relayd_thread_dispatcher)) {
1160 goto error_testpoint;
1161 }
1162
1163 health_code_update();
1164
1165 for (;;) {
1166 health_code_update();
1167
1168 /* Atomically prepare the queue futex */
1169 futex_nto1_prepare(&relay_conn_queue.futex);
1170
1171 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1172 break;
1173 }
1174
1175 do {
1176 health_code_update();
1177
1178 /* Dequeue commands */
1179 node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head,
1180 &relay_conn_queue.tail);
1181 if (node == NULL) {
1182 DBG("Woken up but nothing in the relay command queue");
1183 /* Continue thread execution */
1184 break;
1185 }
1186 new_conn = caa_container_of(node, struct relay_connection, qnode);
1187
1188 DBG("Dispatching request waiting on sock %d", new_conn->sock->fd);
1189
1190 /*
1191 * Inform worker thread of the new request. This
1192 * call is blocking so we can be assured that
1193 * the data will be read at some point in time
1194 * or wait to the end of the world :)
1195 */
1196 ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn));
1197 if (ret < 0) {
1198 PERROR("write connection pipe");
1199 connection_put(new_conn);
1200 goto error;
1201 }
1202 } while (node != NULL);
1203
1204 /* Futex wait on queue. Blocking call on futex() */
1205 health_poll_entry();
1206 futex_nto1_wait(&relay_conn_queue.futex);
1207 health_poll_exit();
1208 }
1209
1210 /* Normal exit, no error */
1211 err = 0;
1212
1213 error:
1214 error_testpoint:
1215 if (err) {
1216 health_error();
1217 ERR("Health error occurred in %s", __func__);
1218 }
1219 health_unregister(health_relayd);
1220 DBG("Dispatch thread dying");
1221 lttng_relay_stop_threads();
1222 return NULL;
1223 }
1224
1225 /*
1226 * Set index data from the control port to a given index object.
1227 */
1228 static int set_index_control_data(struct relay_index *index,
1229 struct lttcomm_relayd_index *data,
1230 struct relay_connection *conn)
1231 {
1232 struct ctf_packet_index index_data;
1233
1234 /*
1235 * The index on disk is encoded in big endian.
1236 */
1237 index_data.packet_size = htobe64(data->packet_size);
1238 index_data.content_size = htobe64(data->content_size);
1239 index_data.timestamp_begin = htobe64(data->timestamp_begin);
1240 index_data.timestamp_end = htobe64(data->timestamp_end);
1241 index_data.events_discarded = htobe64(data->events_discarded);
1242 index_data.stream_id = htobe64(data->stream_id);
1243
1244 if (conn->minor >= 8) {
1245 index->index_data.stream_instance_id = htobe64(data->stream_instance_id);
1246 index->index_data.packet_seq_num = htobe64(data->packet_seq_num);
1247 }
1248
1249 return relay_index_set_data(index, &index_data);
1250 }
1251
1252 /*
1253 * Handle the RELAYD_CREATE_SESSION command.
1254 *
1255 * On success, send back the session id or else return a negative value.
1256 */
1257 static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
1258 struct relay_connection *conn,
1259 const struct lttng_buffer_view *payload)
1260 {
1261 int ret = 0;
1262 ssize_t send_ret;
1263 struct relay_session *session;
1264 struct lttcomm_relayd_status_session reply;
1265 char session_name[LTTNG_NAME_MAX];
1266 char hostname[LTTNG_HOST_NAME_MAX];
1267 uint32_t live_timer = 0;
1268 bool snapshot = false;
1269
1270 memset(session_name, 0, LTTNG_NAME_MAX);
1271 memset(hostname, 0, LTTNG_HOST_NAME_MAX);
1272
1273 memset(&reply, 0, sizeof(reply));
1274
1275 if (conn->minor < 4) {
1276 /* From 2.1 to 2.3 */
1277 ret = 0;
1278 } else if (conn->minor >= 4 && conn->minor < 11) {
1279 /* From 2.4 to 2.10 */
1280 ret = cmd_create_session_2_4(payload, session_name,
1281 hostname, &live_timer, &snapshot);
1282 } else {
1283 /* From 2.11 to ... */
1284 ret = cmd_create_session_2_11(payload, session_name,
1285 hostname, &live_timer, &snapshot);
1286 }
1287
1288 if (ret < 0) {
1289 goto send_reply;
1290 }
1291
1292 session = session_create(session_name, hostname, live_timer,
1293 snapshot, conn->major, conn->minor);
1294 if (!session) {
1295 ret = -1;
1296 goto send_reply;
1297 }
1298 assert(!conn->session);
1299 conn->session = session;
1300 DBG("Created session %" PRIu64, session->id);
1301
1302 reply.session_id = htobe64(session->id);
1303
1304 send_reply:
1305 if (ret < 0) {
1306 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
1307 } else {
1308 reply.ret_code = htobe32(LTTNG_OK);
1309 }
1310
1311 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1312 if (send_ret < (ssize_t) sizeof(reply)) {
1313 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1314 send_ret);
1315 ret = -1;
1316 }
1317
1318 return ret;
1319 }
1320
1321 /*
1322 * When we have received all the streams and the metadata for a channel,
1323 * we make them visible to the viewer threads.
1324 */
1325 static void publish_connection_local_streams(struct relay_connection *conn)
1326 {
1327 struct relay_stream *stream;
1328 struct relay_session *session = conn->session;
1329
1330 /*
1331 * We publish all streams belonging to a session atomically wrt
1332 * session lock.
1333 */
1334 pthread_mutex_lock(&session->lock);
1335 rcu_read_lock();
1336 cds_list_for_each_entry_rcu(stream, &session->recv_list,
1337 recv_node) {
1338 stream_publish(stream);
1339 }
1340 rcu_read_unlock();
1341
1342 /*
1343 * Inform the viewer that there are new streams in the session.
1344 */
1345 if (session->viewer_attached) {
1346 uatomic_set(&session->new_streams, 1);
1347 }
1348 pthread_mutex_unlock(&session->lock);
1349 }
1350
1351 /*
1352 * relay_add_stream: allocate a new stream for a session
1353 */
1354 static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr,
1355 struct relay_connection *conn,
1356 const struct lttng_buffer_view *payload)
1357 {
1358 int ret;
1359 ssize_t send_ret;
1360 struct relay_session *session = conn->session;
1361 struct relay_stream *stream = NULL;
1362 struct lttcomm_relayd_status_stream reply;
1363 struct ctf_trace *trace = NULL;
1364 uint64_t stream_handle = -1ULL;
1365 char *path_name = NULL, *channel_name = NULL;
1366 uint64_t tracefile_size = 0, tracefile_count = 0;
1367 struct relay_stream_chunk_id stream_chunk_id = { 0 };
1368
1369 if (!session || !conn->version_check_done) {
1370 ERR("Trying to add a stream before version check");
1371 ret = -1;
1372 goto end_no_session;
1373 }
1374
1375 if (session->minor == 1) {
1376 /* For 2.1 */
1377 ret = cmd_recv_stream_2_1(payload, &path_name,
1378 &channel_name);
1379 } else if (session->minor > 1 && session->minor < 11) {
1380 /* From 2.2 to 2.10 */
1381 ret = cmd_recv_stream_2_2(payload, &path_name,
1382 &channel_name, &tracefile_size, &tracefile_count);
1383 } else {
1384 /* From 2.11 to ... */
1385 ret = cmd_recv_stream_2_11(payload, &path_name,
1386 &channel_name, &tracefile_size, &tracefile_count,
1387 &stream_chunk_id.value);
1388 stream_chunk_id.is_set = true;
1389 }
1390
1391 if (ret < 0) {
1392 goto send_reply;
1393 }
1394
1395 trace = ctf_trace_get_by_path_or_create(session, path_name);
1396 if (!trace) {
1397 goto send_reply;
1398 }
1399 /* This stream here has one reference on the trace. */
1400
1401 pthread_mutex_lock(&last_relay_stream_id_lock);
1402 stream_handle = ++last_relay_stream_id;
1403 pthread_mutex_unlock(&last_relay_stream_id_lock);
1404
1405 /* We pass ownership of path_name and channel_name. */
1406 stream = stream_create(trace, stream_handle, path_name,
1407 channel_name, tracefile_size, tracefile_count,
1408 &stream_chunk_id);
1409 path_name = NULL;
1410 channel_name = NULL;
1411
1412 /*
1413 * Streams are the owners of their trace. Reference to trace is
1414 * kept within stream_create().
1415 */
1416 ctf_trace_put(trace);
1417
1418 send_reply:
1419 memset(&reply, 0, sizeof(reply));
1420 reply.handle = htobe64(stream_handle);
1421 if (!stream) {
1422 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1423 } else {
1424 reply.ret_code = htobe32(LTTNG_OK);
1425 }
1426
1427 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1428 sizeof(struct lttcomm_relayd_status_stream), 0);
1429 if (send_ret < (ssize_t) sizeof(reply)) {
1430 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1431 send_ret);
1432 ret = -1;
1433 }
1434
1435 end_no_session:
1436 free(path_name);
1437 free(channel_name);
1438 return ret;
1439 }
1440
1441 /*
1442 * relay_close_stream: close a specific stream
1443 */
1444 static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr,
1445 struct relay_connection *conn,
1446 const struct lttng_buffer_view *payload)
1447 {
1448 int ret;
1449 ssize_t send_ret;
1450 struct relay_session *session = conn->session;
1451 struct lttcomm_relayd_close_stream stream_info;
1452 struct lttcomm_relayd_generic_reply reply;
1453 struct relay_stream *stream;
1454
1455 DBG("Close stream received");
1456
1457 if (!session || !conn->version_check_done) {
1458 ERR("Trying to close a stream before version check");
1459 ret = -1;
1460 goto end_no_session;
1461 }
1462
1463 if (payload->size < sizeof(stream_info)) {
1464 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1465 sizeof(stream_info), payload->size);
1466 ret = -1;
1467 goto end_no_session;
1468 }
1469 memcpy(&stream_info, payload->data, sizeof(stream_info));
1470 stream_info.stream_id = be64toh(stream_info.stream_id);
1471 stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num);
1472
1473 stream = stream_get_by_id(stream_info.stream_id);
1474 if (!stream) {
1475 ret = -1;
1476 goto end;
1477 }
1478
1479 /*
1480 * Set last_net_seq_num before the close flag. Required by data
1481 * pending check.
1482 */
1483 pthread_mutex_lock(&stream->lock);
1484 stream->last_net_seq_num = stream_info.last_net_seq_num;
1485 pthread_mutex_unlock(&stream->lock);
1486
1487 /*
1488 * This is one of the conditions which may trigger a stream close
1489 * with the others being:
1490 * 1) A close command is received for a stream
1491 * 2) The control connection owning the stream is closed
1492 * 3) We have received all of the stream's data _after_ a close
1493 * request.
1494 */
1495 try_stream_close(stream);
1496 if (stream->is_metadata) {
1497 struct relay_viewer_stream *vstream;
1498
1499 vstream = viewer_stream_get_by_id(stream->stream_handle);
1500 if (vstream) {
1501 if (vstream->metadata_sent == stream->metadata_received) {
1502 /*
1503 * Since all the metadata has been sent to the
1504 * viewer and that we have a request to close
1505 * its stream, we can safely teardown the
1506 * corresponding metadata viewer stream.
1507 */
1508 viewer_stream_put(vstream);
1509 }
1510 /* Put local reference. */
1511 viewer_stream_put(vstream);
1512 }
1513 }
1514 stream_put(stream);
1515 ret = 0;
1516
1517 end:
1518 memset(&reply, 0, sizeof(reply));
1519 if (ret < 0) {
1520 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1521 } else {
1522 reply.ret_code = htobe32(LTTNG_OK);
1523 }
1524 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1525 sizeof(struct lttcomm_relayd_generic_reply), 0);
1526 if (send_ret < (ssize_t) sizeof(reply)) {
1527 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1528 send_ret);
1529 ret = -1;
1530 }
1531
1532 end_no_session:
1533 return ret;
1534 }
1535
1536 /*
1537 * relay_reset_metadata: reset a metadata stream
1538 */
1539 static
1540 int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr,
1541 struct relay_connection *conn,
1542 const struct lttng_buffer_view *payload)
1543 {
1544 int ret;
1545 ssize_t send_ret;
1546 struct relay_session *session = conn->session;
1547 struct lttcomm_relayd_reset_metadata stream_info;
1548 struct lttcomm_relayd_generic_reply reply;
1549 struct relay_stream *stream;
1550
1551 DBG("Reset metadata received");
1552
1553 if (!session || !conn->version_check_done) {
1554 ERR("Trying to reset a metadata stream before version check");
1555 ret = -1;
1556 goto end_no_session;
1557 }
1558
1559 if (payload->size < sizeof(stream_info)) {
1560 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1561 sizeof(stream_info), payload->size);
1562 ret = -1;
1563 goto end_no_session;
1564 }
1565 memcpy(&stream_info, payload->data, sizeof(stream_info));
1566 stream_info.stream_id = be64toh(stream_info.stream_id);
1567 stream_info.version = be64toh(stream_info.version);
1568
1569 DBG("Update metadata to version %" PRIu64, stream_info.version);
1570
1571 /* Unsupported for live sessions for now. */
1572 if (session->live_timer != 0) {
1573 ret = -1;
1574 goto end;
1575 }
1576
1577 stream = stream_get_by_id(stream_info.stream_id);
1578 if (!stream) {
1579 ret = -1;
1580 goto end;
1581 }
1582 pthread_mutex_lock(&stream->lock);
1583 if (!stream->is_metadata) {
1584 ret = -1;
1585 goto end_unlock;
1586 }
1587
1588 ret = stream_fd_rotate(stream->stream_fd,
1589 stream->path_name, stream->channel_name, 0, 0, NULL);
1590 if (ret < 0) {
1591 ERR("Failed to rotate metadata file %s of channel %s",
1592 stream->path_name, stream->channel_name);
1593 goto end_unlock;
1594 }
1595
1596 end_unlock:
1597 pthread_mutex_unlock(&stream->lock);
1598 stream_put(stream);
1599
1600 end:
1601 memset(&reply, 0, sizeof(reply));
1602 if (ret < 0) {
1603 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1604 } else {
1605 reply.ret_code = htobe32(LTTNG_OK);
1606 }
1607 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1608 sizeof(struct lttcomm_relayd_generic_reply), 0);
1609 if (send_ret < (ssize_t) sizeof(reply)) {
1610 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1611 send_ret);
1612 ret = -1;
1613 }
1614
1615 end_no_session:
1616 return ret;
1617 }
1618
1619 /*
1620 * relay_unknown_command: send -1 if received unknown command
1621 */
1622 static void relay_unknown_command(struct relay_connection *conn)
1623 {
1624 struct lttcomm_relayd_generic_reply reply;
1625 ssize_t send_ret;
1626
1627 memset(&reply, 0, sizeof(reply));
1628 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1629 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1630 if (send_ret < sizeof(reply)) {
1631 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret);
1632 }
1633 }
1634
1635 /*
1636 * relay_start: send an acknowledgment to the client to tell if we are
1637 * ready to receive data. We are ready if a session is established.
1638 */
1639 static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr,
1640 struct relay_connection *conn,
1641 const struct lttng_buffer_view *payload)
1642 {
1643 int ret = 0;
1644 ssize_t send_ret;
1645 struct lttcomm_relayd_generic_reply reply;
1646 struct relay_session *session = conn->session;
1647
1648 if (!session) {
1649 DBG("Trying to start the streaming without a session established");
1650 ret = htobe32(LTTNG_ERR_UNK);
1651 }
1652
1653 memset(&reply, 0, sizeof(reply));
1654 reply.ret_code = htobe32(LTTNG_OK);
1655 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1656 sizeof(reply), 0);
1657 if (send_ret < (ssize_t) sizeof(reply)) {
1658 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1659 send_ret);
1660 ret = -1;
1661 }
1662
1663 return ret;
1664 }
1665
1666 /*
1667 * Append padding to the file pointed by the file descriptor fd.
1668 */
1669 static int write_padding_to_file(int fd, uint32_t size)
1670 {
1671 ssize_t ret = 0;
1672 char *zeros;
1673
1674 if (size == 0) {
1675 goto end;
1676 }
1677
1678 zeros = zmalloc(size);
1679 if (zeros == NULL) {
1680 PERROR("zmalloc zeros for padding");
1681 ret = -1;
1682 goto end;
1683 }
1684
1685 ret = lttng_write(fd, zeros, size);
1686 if (ret < size) {
1687 PERROR("write padding to file");
1688 }
1689
1690 free(zeros);
1691
1692 end:
1693 return ret;
1694 }
1695
1696 /*
1697 * Close the current index file if it is open, and create a new one.
1698 *
1699 * Return 0 on success, -1 on error.
1700 */
1701 static
1702 int create_rotate_index_file(struct relay_stream *stream)
1703 {
1704 int ret;
1705 uint32_t major, minor;
1706
1707 /* Put ref on previous index_file. */
1708 if (stream->index_file) {
1709 relay_index_file_put(stream->index_file);
1710 stream->index_file = NULL;
1711 }
1712 major = stream->trace->session->major;
1713 minor = stream->trace->session->minor;
1714 stream->index_file = relay_index_file_create(stream->path_name,
1715 stream->channel_name,
1716 stream->tracefile_size,
1717 tracefile_array_get_file_index_head(stream->tfa),
1718 lttng_to_index_major(major, minor),
1719 lttng_to_index_minor(major, minor));
1720 if (!stream->index_file) {
1721 ret = -1;
1722 goto end;
1723 }
1724
1725 ret = 0;
1726
1727 end:
1728 return ret;
1729 }
1730
1731 static
1732 int do_rotate_stream(struct relay_stream *stream)
1733 {
1734 int ret;
1735
1736 /* Perform the stream rotation. */
1737 ret = stream_fd_rotate(stream->stream_fd, stream->path_name,
1738 stream->channel_name, stream->tracefile_size,
1739 stream->tracefile_count, NULL);
1740 if (ret < 0) {
1741 ERR("Rotating stream output file");
1742 goto end;
1743 }
1744 stream->tracefile_size_current = 0;
1745
1746 /* Rotate also the index if the stream is not a metadata stream. */
1747 if (!stream->is_metadata) {
1748 ret = create_rotate_index_file(stream);
1749 if (ret < 0) {
1750 ERR("Failed to rotate index file");
1751 goto end;
1752 }
1753 }
1754
1755 stream->rotate_at_seq_num = -1ULL;
1756 stream->pos_after_last_complete_data_index = 0;
1757
1758 end:
1759 return ret;
1760 }
1761
1762 /*
1763 * If too much data has been written in a tracefile before we received the
1764 * rotation command, we have to move the excess data to the new tracefile and
1765 * perform the rotation. This can happen because the control and data
1766 * connections are separate, the indexes as well as the commands arrive from
1767 * the control connection and we have no control over the order so we could be
1768 * in a situation where too much data has been received on the data connection
1769 * before the rotation command on the control connection arrives. We don't need
1770 * to update the index because its order is guaranteed with the rotation
1771 * command message.
1772 */
1773 static
1774 int rotate_truncate_stream(struct relay_stream *stream)
1775 {
1776 int ret, old_fd = -1, new_fd = -1;
1777 off_t lseek_ret;
1778 uint64_t diff, pos = 0;
1779 char buf[FILE_COPY_BUFFER_SIZE];
1780 struct stream_fd *new_stream_fd = NULL;
1781
1782 assert(!stream->is_metadata);
1783
1784 assert(stream->tracefile_size_current >
1785 stream->pos_after_last_complete_data_index);
1786 diff = stream->tracefile_size_current -
1787 stream->pos_after_last_complete_data_index;
1788
1789 /* Create the new tracefile. */
1790 new_stream_fd = stream_fd_create(stream->path_name, stream->channel_name,
1791 stream->tracefile_size, stream->tracefile_count, NULL);
1792 if (new_stream_fd < 0) {
1793 ERR("Failed to create new stream file at path %s for channel %s",
1794 stream->path_name, stream->channel_name);
1795 ret = -1;
1796 goto end;
1797 }
1798
1799 old_fd = stream_fd_get_fd(stream->stream_fd);
1800 if (old_fd < 0) {
1801 ret = -1;
1802 goto end;
1803 }
1804
1805 new_fd = stream_fd_get_fd(new_stream_fd);
1806 if (new_fd < 0) {
1807 ret = -1;
1808 goto end;
1809 }
1810
1811 /*
1812 * Rewind the current tracefile to the position at which the rotation
1813 * should have occured.
1814 */
1815 lseek_ret = lseek(old_fd, stream->pos_after_last_complete_data_index,
1816 SEEK_SET);
1817 if (lseek_ret < 0) {
1818 PERROR("seek truncate stream");
1819 ret = -1;
1820 goto end;
1821 }
1822
1823 /* Move data from the old file to the new file. */
1824 while (pos < diff) {
1825 uint64_t count, bytes_left;
1826 ssize_t io_ret;
1827
1828 bytes_left = diff - pos;
1829 count = bytes_left > sizeof(buf) ? sizeof(buf) : bytes_left;
1830 assert(count <= SIZE_MAX);
1831
1832 io_ret = lttng_read(old_fd, buf, count);
1833 if (io_ret < (ssize_t) count) {
1834 char error_string[256];
1835
1836 snprintf(error_string, sizeof(error_string),
1837 "Failed to read %" PRIu64 " bytes from in rotate_truncate_stream(), returned %zi",
1838 count, io_ret);
1839 if (io_ret == -1) {
1840 PERROR("%s", error_string);
1841 } else {
1842 ERR("%s", error_string);
1843 }
1844 ret = -1;
1845 goto end;
1846 }
1847
1848 io_ret = lttng_write(new_fd, buf, count);
1849 if (io_ret < (ssize_t) count) {
1850 char error_string[256];
1851
1852 snprintf(error_string, sizeof(error_string),
1853 "Failed to write %" PRIu64 " bytes from fd %i in rotate_truncate_stream(), returned %zi",
1854 count, new_fd, io_ret);
1855 if (io_ret == -1) {
1856 PERROR("%s", error_string);
1857 } else {
1858 ERR("%s", error_string);
1859 }
1860 ret = -1;
1861 goto end;
1862 }
1863
1864 pos += count;
1865 }
1866
1867 stream_fd_put_fd(new_stream_fd);
1868 new_fd = -1;
1869
1870 /* Truncate the file to get rid of the excess data. */
1871 ret = ftruncate(old_fd, stream->pos_after_last_complete_data_index);
1872 if (ret) {
1873 PERROR("ftruncate");
1874 goto end;
1875 }
1876
1877 stream_fd_put_fd(stream->stream_fd);
1878 old_fd = -1;
1879
1880 /*
1881 * Swap the old stream_fd with the new, releasing the stream's
1882 * reference.
1883 */
1884 stream_fd_put(stream->stream_fd);
1885 stream->stream_fd = new_stream_fd;
1886
1887 ret = create_rotate_index_file(stream);
1888 if (ret < 0) {
1889 ERR("Rotate stream index file");
1890 goto end;
1891 }
1892
1893 /*
1894 * Update the offset and FD of all the eventual indexes created by the
1895 * data connection before the rotation command arrived.
1896 */
1897 ret = relay_index_switch_all_files(stream);
1898 if (ret < 0) {
1899 ERR("Failed to rotate index file");
1900 goto end;
1901 }
1902
1903 stream->tracefile_size_current = diff;
1904 stream->pos_after_last_complete_data_index = 0;
1905 stream->rotate_at_seq_num = -1ULL;
1906
1907 ret = 0;
1908
1909 end:
1910 if (old_fd != -1) {
1911 stream_fd_put_fd(stream->stream_fd);
1912 }
1913 if (new_fd != -1) {
1914 stream_fd_put_fd(new_stream_fd);
1915 }
1916
1917 return ret;
1918 }
1919
1920 /*
1921 * Check if a stream should perform a rotation (for session rotation).
1922 * Must be called with the stream lock held.
1923 *
1924 * Return 0 on success, a negative value on error.
1925 */
1926 static
1927 int try_rotate_stream(struct relay_stream *stream)
1928 {
1929 int ret = 0;
1930
1931 /* No rotation expected. */
1932 if (stream->rotate_at_seq_num == -1ULL) {
1933 goto end;
1934 }
1935
1936 if (stream->prev_seq < stream->rotate_at_seq_num ||
1937 stream->prev_seq == -1ULL) {
1938 DBG("Stream %" PRIu64 " no yet ready for rotation",
1939 stream->stream_handle);
1940 goto end;
1941 } else if (stream->prev_seq > stream->rotate_at_seq_num) {
1942 DBG("Rotation after too much data has been written in tracefile "
1943 "for stream %" PRIu64 ", need to truncate before "
1944 "rotating", stream->stream_handle);
1945 ret = rotate_truncate_stream(stream);
1946 if (ret) {
1947 ERR("Failed to truncate stream");
1948 goto end;
1949 }
1950 } else {
1951 /* stream->prev_seq == stream->rotate_at_seq_num */
1952 DBG("Stream %" PRIu64 " ready for rotation",
1953 stream->stream_handle);
1954 ret = do_rotate_stream(stream);
1955 }
1956
1957 end:
1958 return ret;
1959 }
1960
1961 /*
1962 * relay_recv_metadata: receive the metadata for the session.
1963 */
1964 static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr,
1965 struct relay_connection *conn,
1966 const struct lttng_buffer_view *payload)
1967 {
1968 int ret = 0;
1969 ssize_t size_ret;
1970 struct relay_session *session = conn->session;
1971 struct lttcomm_relayd_metadata_payload metadata_payload_header;
1972 struct relay_stream *metadata_stream;
1973 uint64_t metadata_payload_size;
1974 int metadata_fd = -1;
1975
1976 if (!session) {
1977 ERR("Metadata sent before version check");
1978 ret = -1;
1979 goto end;
1980 }
1981
1982 if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1983 ERR("Incorrect data size");
1984 ret = -1;
1985 goto end;
1986 }
1987 metadata_payload_size = recv_hdr->data_size -
1988 sizeof(struct lttcomm_relayd_metadata_payload);
1989
1990 memcpy(&metadata_payload_header, payload->data,
1991 sizeof(metadata_payload_header));
1992 metadata_payload_header.stream_id = be64toh(
1993 metadata_payload_header.stream_id);
1994 metadata_payload_header.padding_size = be32toh(
1995 metadata_payload_header.padding_size);
1996
1997 metadata_stream = stream_get_by_id(metadata_payload_header.stream_id);
1998 if (!metadata_stream) {
1999 ret = -1;
2000 goto end;
2001 }
2002
2003 pthread_mutex_lock(&metadata_stream->lock);
2004
2005 metadata_fd = stream_fd_get_fd(metadata_stream->stream_fd);
2006 if (metadata_fd < 0) {
2007 goto end_put;
2008 }
2009 size_ret = lttng_write(metadata_fd,
2010 payload->data + sizeof(metadata_payload_header),
2011 metadata_payload_size);
2012 if (size_ret < metadata_payload_size) {
2013 ERR("Relay error writing metadata on file");
2014 ret = -1;
2015 goto end_put_fd;
2016 }
2017
2018 size_ret = write_padding_to_file(metadata_fd,
2019 metadata_payload_header.padding_size);
2020 if (size_ret < (int64_t) metadata_payload_header.padding_size) {
2021 ret = -1;
2022 goto end_put_fd;
2023 }
2024
2025 metadata_stream->metadata_received +=
2026 metadata_payload_size + metadata_payload_header.padding_size;
2027 DBG2("Relay metadata written. Updated metadata_received %" PRIu64,
2028 metadata_stream->metadata_received);
2029
2030 ret = try_rotate_stream(metadata_stream);
2031 if (ret < 0) {
2032 goto end_put_fd;
2033 }
2034
2035 end_put_fd:
2036 stream_fd_put_fd(metadata_stream->stream_fd);
2037 end_put:
2038 pthread_mutex_unlock(&metadata_stream->lock);
2039 stream_put(metadata_stream);
2040 end:
2041 return ret;
2042 }
2043
2044 /*
2045 * relay_send_version: send relayd version number
2046 */
2047 static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr,
2048 struct relay_connection *conn,
2049 const struct lttng_buffer_view *payload)
2050 {
2051 int ret;
2052 ssize_t send_ret;
2053 struct lttcomm_relayd_version reply, msg;
2054 bool compatible = true;
2055
2056 conn->version_check_done = true;
2057
2058 /* Get version from the other side. */
2059 if (payload->size < sizeof(msg)) {
2060 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
2061 sizeof(msg), payload->size);
2062 ret = -1;
2063 goto end;
2064 }
2065
2066 memcpy(&msg, payload->data, sizeof(msg));
2067 msg.major = be32toh(msg.major);
2068 msg.minor = be32toh(msg.minor);
2069
2070 memset(&reply, 0, sizeof(reply));
2071 reply.major = RELAYD_VERSION_COMM_MAJOR;
2072 reply.minor = RELAYD_VERSION_COMM_MINOR;
2073
2074 /* Major versions must be the same */
2075 if (reply.major != msg.major) {
2076 DBG("Incompatible major versions (%u vs %u), deleting session",
2077 reply.major, msg.major);
2078 compatible = false;
2079 }
2080
2081 conn->major = reply.major;
2082 /* We adapt to the lowest compatible version */
2083 if (reply.minor <= msg.minor) {
2084 conn->minor = reply.minor;
2085 } else {
2086 conn->minor = msg.minor;
2087 }
2088
2089 reply.major = htobe32(reply.major);
2090 reply.minor = htobe32(reply.minor);
2091 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2092 sizeof(reply), 0);
2093 if (send_ret < (ssize_t) sizeof(reply)) {
2094 ERR("Failed to send \"send version\" command reply (ret = %zd)",
2095 send_ret);
2096 ret = -1;
2097 goto end;
2098 } else {
2099 ret = 0;
2100 }
2101
2102 if (!compatible) {
2103 ret = -1;
2104 goto end;
2105 }
2106
2107 DBG("Version check done using protocol %u.%u", conn->major,
2108 conn->minor);
2109
2110 end:
2111 return ret;
2112 }
2113
2114 /*
2115 * Check for data pending for a given stream id from the session daemon.
2116 */
2117 static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2118 struct relay_connection *conn,
2119 const struct lttng_buffer_view *payload)
2120 {
2121 struct relay_session *session = conn->session;
2122 struct lttcomm_relayd_data_pending msg;
2123 struct lttcomm_relayd_generic_reply reply;
2124 struct relay_stream *stream;
2125 ssize_t send_ret;
2126 int ret;
2127
2128 DBG("Data pending command received");
2129
2130 if (!session || !conn->version_check_done) {
2131 ERR("Trying to check for data before version check");
2132 ret = -1;
2133 goto end_no_session;
2134 }
2135
2136 if (payload->size < sizeof(msg)) {
2137 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
2138 sizeof(msg), payload->size);
2139 ret = -1;
2140 goto end_no_session;
2141 }
2142 memcpy(&msg, payload->data, sizeof(msg));
2143 msg.stream_id = be64toh(msg.stream_id);
2144 msg.last_net_seq_num = be64toh(msg.last_net_seq_num);
2145
2146 stream = stream_get_by_id(msg.stream_id);
2147 if (stream == NULL) {
2148 ret = -1;
2149 goto end;
2150 }
2151
2152 pthread_mutex_lock(&stream->lock);
2153
2154 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
2155 " and last_seq %" PRIu64, msg.stream_id,
2156 stream->prev_seq, msg.last_net_seq_num);
2157
2158 /* Avoid wrapping issue */
2159 if (((int64_t) (stream->prev_seq - msg.last_net_seq_num)) >= 0) {
2160 /* Data has in fact been written and is NOT pending */
2161 ret = 0;
2162 } else {
2163 /* Data still being streamed thus pending */
2164 ret = 1;
2165 }
2166
2167 stream->data_pending_check_done = true;
2168 pthread_mutex_unlock(&stream->lock);
2169
2170 stream_put(stream);
2171 end:
2172
2173 memset(&reply, 0, sizeof(reply));
2174 reply.ret_code = htobe32(ret);
2175 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2176 if (send_ret < (ssize_t) sizeof(reply)) {
2177 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
2178 send_ret);
2179 ret = -1;
2180 }
2181
2182 end_no_session:
2183 return ret;
2184 }
2185
2186 /*
2187 * Wait for the control socket to reach a quiescent state.
2188 *
2189 * Note that for now, when receiving this command from the session
2190 * daemon, this means that every subsequent commands or data received on
2191 * the control socket has been handled. So, this is why we simply return
2192 * OK here.
2193 */
2194 static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr,
2195 struct relay_connection *conn,
2196 const struct lttng_buffer_view *payload)
2197 {
2198 int ret;
2199 ssize_t send_ret;
2200 struct relay_stream *stream;
2201 struct lttcomm_relayd_quiescent_control msg;
2202 struct lttcomm_relayd_generic_reply reply;
2203
2204 DBG("Checking quiescent state on control socket");
2205
2206 if (!conn->session || !conn->version_check_done) {
2207 ERR("Trying to check for data before version check");
2208 ret = -1;
2209 goto end_no_session;
2210 }
2211
2212 if (payload->size < sizeof(msg)) {
2213 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2214 sizeof(msg), payload->size);
2215 ret = -1;
2216 goto end_no_session;
2217 }
2218 memcpy(&msg, payload->data, sizeof(msg));
2219 msg.stream_id = be64toh(msg.stream_id);
2220
2221 stream = stream_get_by_id(msg.stream_id);
2222 if (!stream) {
2223 goto reply;
2224 }
2225 pthread_mutex_lock(&stream->lock);
2226 stream->data_pending_check_done = true;
2227 pthread_mutex_unlock(&stream->lock);
2228
2229 DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id);
2230 stream_put(stream);
2231 reply:
2232 memset(&reply, 0, sizeof(reply));
2233 reply.ret_code = htobe32(LTTNG_OK);
2234 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2235 if (send_ret < (ssize_t) sizeof(reply)) {
2236 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2237 send_ret);
2238 ret = -1;
2239 } else {
2240 ret = 0;
2241 }
2242
2243 end_no_session:
2244 return ret;
2245 }
2246
2247 /*
2248 * Initialize a data pending command. This means that a consumer is about
2249 * to ask for data pending for each stream it holds. Simply iterate over
2250 * all streams of a session and set the data_pending_check_done flag.
2251 *
2252 * This command returns to the client a LTTNG_OK code.
2253 */
2254 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2255 struct relay_connection *conn,
2256 const struct lttng_buffer_view *payload)
2257 {
2258 int ret;
2259 ssize_t send_ret;
2260 struct lttng_ht_iter iter;
2261 struct lttcomm_relayd_begin_data_pending msg;
2262 struct lttcomm_relayd_generic_reply reply;
2263 struct relay_stream *stream;
2264
2265 assert(recv_hdr);
2266 assert(conn);
2267
2268 DBG("Init streams for data pending");
2269
2270 if (!conn->session || !conn->version_check_done) {
2271 ERR("Trying to check for data before version check");
2272 ret = -1;
2273 goto end_no_session;
2274 }
2275
2276 if (payload->size < sizeof(msg)) {
2277 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2278 sizeof(msg), payload->size);
2279 ret = -1;
2280 goto end_no_session;
2281 }
2282 memcpy(&msg, payload->data, sizeof(msg));
2283 msg.session_id = be64toh(msg.session_id);
2284
2285 /*
2286 * Iterate over all streams to set the begin data pending flag.
2287 * For now, the streams are indexed by stream handle so we have
2288 * to iterate over all streams to find the one associated with
2289 * the right session_id.
2290 */
2291 rcu_read_lock();
2292 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
2293 node.node) {
2294 if (!stream_get(stream)) {
2295 continue;
2296 }
2297 if (stream->trace->session->id == msg.session_id) {
2298 pthread_mutex_lock(&stream->lock);
2299 stream->data_pending_check_done = false;
2300 pthread_mutex_unlock(&stream->lock);
2301 DBG("Set begin data pending flag to stream %" PRIu64,
2302 stream->stream_handle);
2303 }
2304 stream_put(stream);
2305 }
2306 rcu_read_unlock();
2307
2308 memset(&reply, 0, sizeof(reply));
2309 /* All good, send back reply. */
2310 reply.ret_code = htobe32(LTTNG_OK);
2311
2312 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2313 if (send_ret < (ssize_t) sizeof(reply)) {
2314 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2315 send_ret);
2316 ret = -1;
2317 } else {
2318 ret = 0;
2319 }
2320
2321 end_no_session:
2322 return ret;
2323 }
2324
2325 /*
2326 * End data pending command. This will check, for a given session id, if
2327 * each stream associated with it has its data_pending_check_done flag
2328 * set. If not, this means that the client lost track of the stream but
2329 * the data is still being streamed on our side. In this case, we inform
2330 * the client that data is in flight.
2331 *
2332 * Return to the client if there is data in flight or not with a ret_code.
2333 */
2334 static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2335 struct relay_connection *conn,
2336 const struct lttng_buffer_view *payload)
2337 {
2338 int ret;
2339 ssize_t send_ret;
2340 struct lttng_ht_iter iter;
2341 struct lttcomm_relayd_end_data_pending msg;
2342 struct lttcomm_relayd_generic_reply reply;
2343 struct relay_stream *stream;
2344 uint32_t is_data_inflight = 0;
2345
2346 DBG("End data pending command");
2347
2348 if (!conn->session || !conn->version_check_done) {
2349 ERR("Trying to check for data before version check");
2350 ret = -1;
2351 goto end_no_session;
2352 }
2353
2354 if (payload->size < sizeof(msg)) {
2355 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2356 sizeof(msg), payload->size);
2357 ret = -1;
2358 goto end_no_session;
2359 }
2360 memcpy(&msg, payload->data, sizeof(msg));
2361 msg.session_id = be64toh(msg.session_id);
2362
2363 /*
2364 * Iterate over all streams to see if the begin data pending
2365 * flag is set.
2366 */
2367 rcu_read_lock();
2368 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
2369 node.node) {
2370 if (!stream_get(stream)) {
2371 continue;
2372 }
2373 if (stream->trace->session->id != msg.session_id) {
2374 stream_put(stream);
2375 continue;
2376 }
2377 pthread_mutex_lock(&stream->lock);
2378 if (!stream->data_pending_check_done) {
2379 if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) {
2380 is_data_inflight = 1;
2381 DBG("Data is still in flight for stream %" PRIu64,
2382 stream->stream_handle);
2383 pthread_mutex_unlock(&stream->lock);
2384 stream_put(stream);
2385 break;
2386 }
2387 }
2388 pthread_mutex_unlock(&stream->lock);
2389 stream_put(stream);
2390 }
2391 rcu_read_unlock();
2392
2393 memset(&reply, 0, sizeof(reply));
2394 /* All good, send back reply. */
2395 reply.ret_code = htobe32(is_data_inflight);
2396
2397 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2398 if (send_ret < (ssize_t) sizeof(reply)) {
2399 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2400 send_ret);
2401 ret = -1;
2402 } else {
2403 ret = 0;
2404 }
2405
2406 end_no_session:
2407 return ret;
2408 }
2409
2410 /*
2411 * Receive an index for a specific stream.
2412 *
2413 * Return 0 on success else a negative value.
2414 */
2415 static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr,
2416 struct relay_connection *conn,
2417 const struct lttng_buffer_view *payload)
2418 {
2419 int ret;
2420 ssize_t send_ret;
2421 struct relay_session *session = conn->session;
2422 struct lttcomm_relayd_index index_info;
2423 struct relay_index *index;
2424 struct lttcomm_relayd_generic_reply reply;
2425 struct relay_stream *stream;
2426 size_t msg_len;
2427
2428 assert(conn);
2429
2430 DBG("Relay receiving index");
2431
2432 if (!session || !conn->version_check_done) {
2433 ERR("Trying to close a stream before version check");
2434 ret = -1;
2435 goto end_no_session;
2436 }
2437
2438 msg_len = lttcomm_relayd_index_len(
2439 lttng_to_index_major(conn->major, conn->minor),
2440 lttng_to_index_minor(conn->major, conn->minor));
2441 if (payload->size < msg_len) {
2442 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2443 msg_len, payload->size);
2444 ret = -1;
2445 goto end_no_session;
2446 }
2447 memcpy(&index_info, payload->data, msg_len);
2448 index_info.relay_stream_id = be64toh(index_info.relay_stream_id);
2449 index_info.net_seq_num = be64toh(index_info.net_seq_num);
2450 index_info.packet_size = be64toh(index_info.packet_size);
2451 index_info.content_size = be64toh(index_info.content_size);
2452 index_info.timestamp_begin = be64toh(index_info.timestamp_begin);
2453 index_info.timestamp_end = be64toh(index_info.timestamp_end);
2454 index_info.events_discarded = be64toh(index_info.events_discarded);
2455 index_info.stream_id = be64toh(index_info.stream_id);
2456
2457 if (conn->minor >= 8) {
2458 index_info.stream_instance_id =
2459 be64toh(index_info.stream_instance_id);
2460 index_info.packet_seq_num = be64toh(index_info.packet_seq_num);
2461 }
2462
2463 stream = stream_get_by_id(index_info.relay_stream_id);
2464 if (!stream) {
2465 ERR("stream_get_by_id not found");
2466 ret = -1;
2467 goto end;
2468 }
2469 pthread_mutex_lock(&stream->lock);
2470
2471 /* Live beacon handling */
2472 if (index_info.packet_size == 0) {
2473 DBG("Received live beacon for stream %" PRIu64,
2474 stream->stream_handle);
2475
2476 /*
2477 * Only flag a stream inactive when it has already
2478 * received data and no indexes are in flight.
2479 */
2480 if (stream->index_received_seqcount > 0
2481 && stream->indexes_in_flight == 0) {
2482 stream->beacon_ts_end = index_info.timestamp_end;
2483 }
2484 ret = 0;
2485 goto end_stream_put;
2486 } else {
2487 stream->beacon_ts_end = -1ULL;
2488 }
2489
2490 if (stream->ctf_stream_id == -1ULL) {
2491 stream->ctf_stream_id = index_info.stream_id;
2492 }
2493 index = relay_index_get_by_id_or_create(stream, index_info.net_seq_num);
2494 if (!index) {
2495 ret = -1;
2496 ERR("relay_index_get_by_id_or_create index NULL");
2497 goto end_stream_put;
2498 }
2499 if (set_index_control_data(index, &index_info, conn)) {
2500 ERR("set_index_control_data error");
2501 relay_index_put(index);
2502 ret = -1;
2503 goto end_stream_put;
2504 }
2505 ret = relay_index_try_flush(index);
2506 if (ret == 0) {
2507 tracefile_array_commit_seq(stream->tfa);
2508 stream->index_received_seqcount++;
2509 stream->pos_after_last_complete_data_index += index->total_size;
2510 } else if (ret > 0) {
2511 /* no flush. */
2512 ret = 0;
2513 } else {
2514 ERR("relay_index_try_flush error %d", ret);
2515 relay_index_put(index);
2516 ret = -1;
2517 }
2518
2519 end_stream_put:
2520 pthread_mutex_unlock(&stream->lock);
2521 stream_put(stream);
2522
2523 end:
2524
2525 memset(&reply, 0, sizeof(reply));
2526 if (ret < 0) {
2527 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2528 } else {
2529 reply.ret_code = htobe32(LTTNG_OK);
2530 }
2531 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2532 if (send_ret < (ssize_t) sizeof(reply)) {
2533 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret);
2534 ret = -1;
2535 }
2536
2537 end_no_session:
2538 return ret;
2539 }
2540
2541 /*
2542 * Receive the streams_sent message.
2543 *
2544 * Return 0 on success else a negative value.
2545 */
2546 static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr,
2547 struct relay_connection *conn,
2548 const struct lttng_buffer_view *payload)
2549 {
2550 int ret;
2551 ssize_t send_ret;
2552 struct lttcomm_relayd_generic_reply reply;
2553
2554 assert(conn);
2555
2556 DBG("Relay receiving streams_sent");
2557
2558 if (!conn->session || !conn->version_check_done) {
2559 ERR("Trying to close a stream before version check");
2560 ret = -1;
2561 goto end_no_session;
2562 }
2563
2564 /*
2565 * Publish every pending stream in the connection recv list which are
2566 * now ready to be used by the viewer.
2567 */
2568 publish_connection_local_streams(conn);
2569
2570 memset(&reply, 0, sizeof(reply));
2571 reply.ret_code = htobe32(LTTNG_OK);
2572 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2573 if (send_ret < (ssize_t) sizeof(reply)) {
2574 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2575 send_ret);
2576 ret = -1;
2577 } else {
2578 /* Success. */
2579 ret = 0;
2580 }
2581
2582 end_no_session:
2583 return ret;
2584 }
2585
2586 /*
2587 * relay_rotate_session_stream: rotate a stream to a new tracefile for the session
2588 * rotation feature (not the tracefile rotation feature).
2589 */
2590 static int relay_rotate_session_stream(const struct lttcomm_relayd_hdr *recv_hdr,
2591 struct relay_connection *conn,
2592 const struct lttng_buffer_view *payload)
2593 {
2594 int ret;
2595 ssize_t send_ret;
2596 struct relay_session *session = conn->session;
2597 struct lttcomm_relayd_rotate_stream stream_info;
2598 struct lttcomm_relayd_generic_reply reply;
2599 struct relay_stream *stream;
2600 size_t header_len;
2601 size_t path_len;
2602 struct lttng_buffer_view new_path_view;
2603
2604 DBG("Rotate stream received");
2605
2606 if (!session || !conn->version_check_done) {
2607 ERR("Trying to rotate a stream before version check");
2608 ret = -1;
2609 goto end_no_reply;
2610 }
2611
2612 if (session->major == 2 && session->minor < 11) {
2613 ERR("Unsupported feature before 2.11");
2614 ret = -1;
2615 goto end_no_reply;
2616 }
2617
2618 header_len = sizeof(struct lttcomm_relayd_rotate_stream);
2619
2620 if (payload->size < header_len) {
2621 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2622 header_len, payload->size);
2623 ret = -1;
2624 goto end_no_reply;
2625 }
2626
2627 memcpy(&stream_info, payload->data, header_len);
2628
2629 /* Convert to host */
2630 stream_info.pathname_length = be32toh(stream_info.pathname_length);
2631 stream_info.stream_id = be64toh(stream_info.stream_id);
2632 stream_info.new_chunk_id = be64toh(stream_info.new_chunk_id);
2633 stream_info.rotate_at_seq_num = be64toh(stream_info.rotate_at_seq_num);
2634
2635 path_len = stream_info.pathname_length;
2636 if (payload->size < header_len + path_len) {
2637 ERR("Unexpected payload size in \"relay_rotate_session_stream\" including path: expected >= %zu bytes, got %zu bytes",
2638 header_len + path_len, payload->size);
2639 ret = -1;
2640 goto end_no_reply;
2641 }
2642
2643 /* Ensure it fits in local filename length. */
2644 if (path_len >= LTTNG_PATH_MAX) {
2645 ret = -ENAMETOOLONG;
2646 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2647 path_len, LTTNG_PATH_MAX);
2648 goto end;
2649 }
2650
2651 new_path_view = lttng_buffer_view_from_view(payload, header_len,
2652 stream_info.pathname_length);
2653
2654 stream = stream_get_by_id(stream_info.stream_id);
2655 if (!stream) {
2656 ret = -1;
2657 goto end;
2658 }
2659
2660 pthread_mutex_lock(&stream->lock);
2661
2662 /*
2663 * Update the trace path (just the folder, the stream name does not
2664 * change).
2665 */
2666 free(stream->path_name);
2667 stream->path_name = create_output_path(new_path_view.data);
2668 if (!stream->path_name) {
2669 ERR("Failed to create a new output path");
2670 ret = -1;
2671 goto end_stream_unlock;
2672 }
2673 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG,
2674 -1, -1);
2675 if (ret < 0) {
2676 ERR("relay creating output directory");
2677 ret = -1;
2678 goto end_stream_unlock;
2679 }
2680
2681 assert(stream->current_chunk_id.is_set);
2682 stream->current_chunk_id.value = stream_info.new_chunk_id;
2683
2684 if (stream->is_metadata) {
2685 /*
2686 * The metadata stream is sent only over the control connection
2687 * so we know we have all the data to perform the stream
2688 * rotation.
2689 */
2690 ret = do_rotate_stream(stream);
2691 } else {
2692 stream->rotate_at_seq_num = stream_info.rotate_at_seq_num;
2693 ret = try_rotate_stream(stream);
2694 }
2695 if (ret < 0) {
2696 goto end_stream_unlock;
2697 }
2698
2699 end_stream_unlock:
2700 pthread_mutex_unlock(&stream->lock);
2701 stream_put(stream);
2702 end:
2703 memset(&reply, 0, sizeof(reply));
2704 if (ret < 0) {
2705 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2706 } else {
2707 reply.ret_code = htobe32(LTTNG_OK);
2708 }
2709 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2710 sizeof(struct lttcomm_relayd_generic_reply), 0);
2711 if (send_ret < (ssize_t) sizeof(reply)) {
2712 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2713 send_ret);
2714 ret = -1;
2715 }
2716
2717 end_no_reply:
2718 return ret;
2719 }
2720
2721 /*
2722 * relay_mkdir: Create a folder on the disk.
2723 */
2724 static int relay_mkdir(const struct lttcomm_relayd_hdr *recv_hdr,
2725 struct relay_connection *conn,
2726 const struct lttng_buffer_view *payload)
2727 {
2728 int ret;
2729 struct relay_session *session = conn->session;
2730 struct lttcomm_relayd_mkdir path_info_header;
2731 struct lttcomm_relayd_generic_reply reply;
2732 char *path = NULL;
2733 size_t header_len;
2734 ssize_t send_ret;
2735 struct lttng_buffer_view path_view;
2736
2737 if (!session || !conn->version_check_done) {
2738 ERR("Trying to create a directory before version check");
2739 ret = -1;
2740 goto end_no_session;
2741 }
2742
2743 if (session->major == 2 && session->minor < 11) {
2744 /*
2745 * This client is not supposed to use this command since
2746 * it predates its introduction.
2747 */
2748 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2749 ret = -1;
2750 goto end_no_session;
2751 }
2752
2753 header_len = sizeof(path_info_header);
2754 if (payload->size < header_len) {
2755 ERR("Unexpected payload size in \"relay_mkdir\": expected >= %zu bytes, got %zu bytes",
2756 header_len, payload->size);
2757 ret = -1;
2758 goto end_no_session;
2759 }
2760
2761 memcpy(&path_info_header, payload->data, header_len);
2762
2763 path_info_header.length = be32toh(path_info_header.length);
2764
2765 if (payload->size < header_len + path_info_header.length) {
2766 ERR("Unexpected payload size in \"relay_mkdir\" including path: expected >= %zu bytes, got %zu bytes",
2767 header_len + path_info_header.length, payload->size);
2768 ret = -1;
2769 goto end_no_session;
2770 }
2771
2772 /* Ensure that it fits in local path length. */
2773 if (path_info_header.length >= LTTNG_PATH_MAX) {
2774 ret = -ENAMETOOLONG;
2775 ERR("Path name argument of mkdir command (%" PRIu32 " bytes) exceeds the maximal length allowed (%d bytes)",
2776 path_info_header.length, LTTNG_PATH_MAX);
2777 goto end;
2778 }
2779
2780 path_view = lttng_buffer_view_from_view(payload, header_len,
2781 path_info_header.length);
2782
2783 path = create_output_path(path_view.data);
2784 if (!path) {
2785 ERR("Failed to create output path");
2786 ret = -1;
2787 goto end;
2788 }
2789
2790 ret = utils_mkdir_recursive(path, S_IRWXU | S_IRWXG, -1, -1);
2791 if (ret < 0) {
2792 ERR("relay creating output directory");
2793 goto end;
2794 }
2795
2796 ret = 0;
2797
2798 end:
2799 memset(&reply, 0, sizeof(reply));
2800 if (ret < 0) {
2801 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2802 } else {
2803 reply.ret_code = htobe32(LTTNG_OK);
2804 }
2805 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2806 if (send_ret < (ssize_t) sizeof(reply)) {
2807 ERR("Failed to send \"mkdir\" command reply (ret = %zd)", send_ret);
2808 ret = -1;
2809 }
2810
2811 end_no_session:
2812 free(path);
2813 return ret;
2814 }
2815
2816 static int validate_rotate_rename_path_length(const char *path_type,
2817 uint32_t path_length)
2818 {
2819 int ret = 0;
2820
2821 if (path_length > LTTNG_PATH_MAX) {
2822 ret = -ENAMETOOLONG;
2823 ERR("rotate rename \"%s\" path name length (%" PRIu32 " bytes) exceeds the allowed size of %i bytes",
2824 path_type, path_length, LTTNG_PATH_MAX);
2825 } else if (path_length == 0) {
2826 ret = -EINVAL;
2827 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type);
2828 }
2829 return ret;
2830 }
2831
2832 /*
2833 * relay_rotate_rename: rename the trace folder after a rotation is
2834 * completed. We are not closing any fd here, just moving the folder, so it
2835 * works even if data is still in-flight.
2836 */
2837 static int relay_rotate_rename(const struct lttcomm_relayd_hdr *recv_hdr,
2838 struct relay_connection *conn,
2839 const struct lttng_buffer_view *payload)
2840 {
2841 int ret;
2842 ssize_t send_ret;
2843 struct relay_session *session = conn->session;
2844 struct lttcomm_relayd_generic_reply reply;
2845 struct lttcomm_relayd_rotate_rename header;
2846 size_t header_len;
2847 size_t received_paths_size;
2848 char *complete_old_path = NULL, *complete_new_path = NULL;
2849 struct lttng_buffer_view old_path_view;
2850 struct lttng_buffer_view new_path_view;
2851
2852 if (!session || !conn->version_check_done) {
2853 ERR("Trying to rename a trace folder before version check");
2854 ret = -1;
2855 goto end_no_reply;
2856 }
2857
2858 if (session->major == 2 && session->minor < 11) {
2859 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2860 ret = -1;
2861 goto end_no_reply;
2862 }
2863
2864 header_len = sizeof(header);
2865 if (payload->size < header_len) {
2866 ERR("Unexpected payload size in \"relay_rotate_rename\": expected >= %zu bytes, got %zu bytes",
2867 header_len, payload->size);
2868 ret = -1;
2869 goto end_no_reply;
2870 }
2871
2872 memcpy(&header, payload->data, header_len);
2873
2874 header.old_path_length = be32toh(header.old_path_length);
2875 header.new_path_length = be32toh(header.new_path_length);
2876 received_paths_size = header.old_path_length + header.new_path_length;
2877
2878 if (payload->size < header_len + received_paths_size) {
2879 ERR("Unexpected payload size in \"relay_rotate_rename\" including paths: expected >= %zu bytes, got %zu bytes",
2880 header_len, payload->size);
2881 ret = -1;
2882 goto end_no_reply;
2883 }
2884
2885 /* Ensure the paths don't exceed their allowed size. */
2886 ret = validate_rotate_rename_path_length("old", header.old_path_length);
2887 if (ret) {
2888 goto end;
2889 }
2890 ret = validate_rotate_rename_path_length("new", header.new_path_length);
2891 if (ret) {
2892 goto end;
2893 }
2894
2895 old_path_view = lttng_buffer_view_from_view(payload, header_len,
2896 header.old_path_length);
2897 new_path_view = lttng_buffer_view_from_view(payload,
2898 header_len + header.old_path_length,
2899 header.new_path_length);
2900
2901 /* Validate that both paths received are NULL terminated. */
2902 if (old_path_view.data[old_path_view.size - 1] != '\0') {
2903 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2904 ret = -1;
2905 goto end;
2906 }
2907 if (new_path_view.data[new_path_view.size - 1] != '\0') {
2908 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2909 ret = -1;
2910 goto end;
2911 }
2912
2913 complete_old_path = create_output_path(old_path_view.data);
2914 if (!complete_old_path) {
2915 ERR("Failed to build old output path in rotate_rename command");
2916 ret = -1;
2917 goto end;
2918 }
2919
2920 complete_new_path = create_output_path(new_path_view.data);
2921 if (!complete_new_path) {
2922 ERR("Failed to build new output path in rotate_rename command");
2923 ret = -1;
2924 goto end;
2925 }
2926
2927 ret = utils_mkdir_recursive(complete_new_path, S_IRWXU | S_IRWXG,
2928 -1, -1);
2929 if (ret < 0) {
2930 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2931 complete_new_path);
2932 goto end;
2933 }
2934
2935 /*
2936 * If a domain has not yet created its channel, the domain-specific
2937 * folder might not exist, but this is not an error.
2938 */
2939 ret = rename(complete_old_path, complete_new_path);
2940 if (ret < 0 && errno != ENOENT) {
2941 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2942 complete_old_path, complete_new_path);
2943 goto end;
2944 }
2945 ret = 0;
2946
2947 end:
2948 memset(&reply, 0, sizeof(reply));
2949 if (ret < 0) {
2950 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2951 } else {
2952 reply.ret_code = htobe32(LTTNG_OK);
2953 }
2954 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2955 sizeof(reply), 0);
2956 if (send_ret < sizeof(reply)) {
2957 ERR("Failed to send \"rotate rename\" command reply (ret = %zd)",
2958 send_ret);
2959 ret = -1;
2960 }
2961
2962 end_no_reply:
2963 free(complete_old_path);
2964 free(complete_new_path);
2965 return ret;
2966 }
2967
2968 /*
2969 * Check if all the streams in the session have completed the last rotation.
2970 * The chunk_id value is used to distinguish the cases where a stream was
2971 * closed on the consumerd before the rotation started but it still active on
2972 * the relayd, and the case where a stream appeared on the consumerd/relayd
2973 * after the last rotation started (in that case, it is already writing in the
2974 * new chunk folder).
2975 */
2976 static
2977 int relay_rotate_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2978 struct relay_connection *conn,
2979 const struct lttng_buffer_view *payload)
2980 {
2981 struct relay_session *session = conn->session;
2982 struct lttcomm_relayd_rotate_pending msg;
2983 struct lttcomm_relayd_rotate_pending_reply reply;
2984 struct lttng_ht_iter iter;
2985 struct relay_stream *stream;
2986 int ret = 0;
2987 ssize_t send_ret;
2988 uint64_t chunk_id;
2989 bool rotate_pending = false;
2990
2991 DBG("Rotate pending command received");
2992
2993 if (!session || !conn->version_check_done) {
2994 ERR("Trying to check for data before version check");
2995 ret = -1;
2996 goto end_no_reply;
2997 }
2998
2999 if (session->major == 2 && session->minor < 11) {
3000 ERR("Unsupported feature before 2.11");
3001 ret = -1;
3002 goto end_no_reply;
3003 }
3004
3005 if (payload->size < sizeof(msg)) {
3006 ERR("Unexpected payload size in \"relay_rotate_pending\": expected >= %zu bytes, got %zu bytes",
3007 sizeof(msg), payload->size);
3008 ret = -1;
3009 goto end_no_reply;
3010 }
3011
3012 memcpy(&msg, payload->data, sizeof(msg));
3013
3014 chunk_id = be64toh(msg.chunk_id);
3015
3016 DBG("Evaluating rotate pending for session \"%s\" and chunk id %" PRIu64,
3017 session->session_name, chunk_id);
3018
3019 /*
3020 * Iterate over all the streams in the session and check if they are
3021 * still waiting for data to perform their rotation.
3022 */
3023 rcu_read_lock();
3024 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
3025 node.node) {
3026 if (!stream_get(stream)) {
3027 continue;
3028 }
3029 if (stream->trace->session != session) {
3030 stream_put(stream);
3031 continue;
3032 }
3033 pthread_mutex_lock(&stream->lock);
3034 if (stream->rotate_at_seq_num != -1ULL) {
3035 /* We have not yet performed the rotation. */
3036 rotate_pending = true;
3037 DBG("Stream %" PRIu64 " is still rotating",
3038 stream->stream_handle);
3039 } else if (stream->current_chunk_id.value < chunk_id) {
3040 /*
3041 * Stream closed on the consumer but still active on the
3042 * relay.
3043 */
3044 rotate_pending = true;
3045 DBG("Stream %" PRIu64 " did not exist on the consumer "
3046 "when the last rotation started, but is"
3047 "still waiting for data before getting"
3048 "closed",
3049 stream->stream_handle);
3050 }
3051 pthread_mutex_unlock(&stream->lock);
3052 stream_put(stream);
3053 if (rotate_pending) {
3054 goto send_reply;
3055 }
3056 }
3057
3058 send_reply:
3059 rcu_read_unlock();
3060 memset(&reply, 0, sizeof(reply));
3061 reply.generic.ret_code = htobe32((uint32_t) LTTNG_OK);
3062 reply.is_pending = (uint8_t) !!rotate_pending;
3063 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
3064 sizeof(reply), 0);
3065 if (send_ret < (ssize_t) sizeof(reply)) {
3066 ERR("Failed to send \"rotate pending\" command reply (ret = %zd)",
3067 send_ret);
3068 ret = -1;
3069 }
3070
3071 end_no_reply:
3072 return ret;
3073 }
3074
3075 #define DBG_CMD(cmd_name, conn) \
3076 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3077
3078 static int relay_process_control_command(struct relay_connection *conn,
3079 const struct lttcomm_relayd_hdr *header,
3080 const struct lttng_buffer_view *payload)
3081 {
3082 int ret = 0;
3083
3084 switch (header->cmd) {
3085 case RELAYD_CREATE_SESSION:
3086 DBG_CMD("RELAYD_CREATE_SESSION", conn);
3087 ret = relay_create_session(header, conn, payload);
3088 break;
3089 case RELAYD_ADD_STREAM:
3090 DBG_CMD("RELAYD_ADD_STREAM", conn);
3091 ret = relay_add_stream(header, conn, payload);
3092 break;
3093 case RELAYD_START_DATA:
3094 DBG_CMD("RELAYD_START_DATA", conn);
3095 ret = relay_start(header, conn, payload);
3096 break;
3097 case RELAYD_SEND_METADATA:
3098 DBG_CMD("RELAYD_SEND_METADATA", conn);
3099 ret = relay_recv_metadata(header, conn, payload);
3100 break;
3101 case RELAYD_VERSION:
3102 DBG_CMD("RELAYD_VERSION", conn);
3103 ret = relay_send_version(header, conn, payload);
3104 break;
3105 case RELAYD_CLOSE_STREAM:
3106 DBG_CMD("RELAYD_CLOSE_STREAM", conn);
3107 ret = relay_close_stream(header, conn, payload);
3108 break;
3109 case RELAYD_DATA_PENDING:
3110 DBG_CMD("RELAYD_DATA_PENDING", conn);
3111 ret = relay_data_pending(header, conn, payload);
3112 break;
3113 case RELAYD_QUIESCENT_CONTROL:
3114 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn);
3115 ret = relay_quiescent_control(header, conn, payload);
3116 break;
3117 case RELAYD_BEGIN_DATA_PENDING:
3118 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn);
3119 ret = relay_begin_data_pending(header, conn, payload);
3120 break;
3121 case RELAYD_END_DATA_PENDING:
3122 DBG_CMD("RELAYD_END_DATA_PENDING", conn);
3123 ret = relay_end_data_pending(header, conn, payload);
3124 break;
3125 case RELAYD_SEND_INDEX:
3126 DBG_CMD("RELAYD_SEND_INDEX", conn);
3127 ret = relay_recv_index(header, conn, payload);
3128 break;
3129 case RELAYD_STREAMS_SENT:
3130 DBG_CMD("RELAYD_STREAMS_SENT", conn);
3131 ret = relay_streams_sent(header, conn, payload);
3132 break;
3133 case RELAYD_RESET_METADATA:
3134 DBG_CMD("RELAYD_RESET_METADATA", conn);
3135 ret = relay_reset_metadata(header, conn, payload);
3136 break;
3137 case RELAYD_ROTATE_STREAM:
3138 DBG_CMD("RELAYD_ROTATE_STREAM", conn);
3139 ret = relay_rotate_session_stream(header, conn, payload);
3140 break;
3141 case RELAYD_ROTATE_RENAME:
3142 DBG_CMD("RELAYD_ROTATE_RENAME", conn);
3143 ret = relay_rotate_rename(header, conn, payload);
3144 break;
3145 case RELAYD_ROTATE_PENDING:
3146 DBG_CMD("RELAYD_ROTATE_PENDING", conn);
3147 ret = relay_rotate_pending(header, conn, payload);
3148 break;
3149 case RELAYD_MKDIR:
3150 DBG_CMD("RELAYD_MKDIR", conn);
3151 ret = relay_mkdir(header, conn, payload);
3152 break;
3153 case RELAYD_UPDATE_SYNC_INFO:
3154 default:
3155 ERR("Received unknown command (%u)", header->cmd);
3156 relay_unknown_command(conn);
3157 ret = -1;
3158 goto end;
3159 }
3160
3161 end:
3162 return ret;
3163 }
3164
3165 static enum relay_connection_status relay_process_control_receive_payload(
3166 struct relay_connection *conn)
3167 {
3168 int ret = 0;
3169 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3170 struct lttng_dynamic_buffer *reception_buffer =
3171 &conn->protocol.ctrl.reception_buffer;
3172 struct ctrl_connection_state_receive_payload *state =
3173 &conn->protocol.ctrl.state.receive_payload;
3174 struct lttng_buffer_view payload_view;
3175
3176 if (state->left_to_receive == 0) {
3177 /* Short-circuit for payload-less commands. */
3178 goto reception_complete;
3179 }
3180
3181 ret = conn->sock->ops->recvmsg(conn->sock,
3182 reception_buffer->data + state->received,
3183 state->left_to_receive, MSG_DONTWAIT);
3184 if (ret < 0) {
3185 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3186 PERROR("Unable to receive command payload on sock %d",
3187 conn->sock->fd);
3188 status = RELAY_CONNECTION_STATUS_ERROR;
3189 }
3190 goto end;
3191 } else if (ret == 0) {
3192 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
3193 status = RELAY_CONNECTION_STATUS_CLOSED;
3194 goto end;
3195 }
3196
3197 assert(ret > 0);
3198 assert(ret <= state->left_to_receive);
3199
3200 state->left_to_receive -= ret;
3201 state->received += ret;
3202
3203 if (state->left_to_receive > 0) {
3204 /*
3205 * Can't transition to the protocol's next state, wait to
3206 * receive the rest of the header.
3207 */
3208 DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
3209 state->received, state->left_to_receive,
3210 conn->sock->fd);
3211 goto end;
3212 }
3213
3214 reception_complete:
3215 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes",
3216 conn->sock->fd, state->received);
3217 /*
3218 * The payload required to process the command has been received.
3219 * A view to the reception buffer is forwarded to the various
3220 * commands and the state of the control is reset on success.
3221 *
3222 * Commands are responsible for sending their reply to the peer.
3223 */
3224 payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer,
3225 0, -1);
3226 ret = relay_process_control_command(conn,
3227 &state->header, &payload_view);
3228 if (ret < 0) {
3229 status = RELAY_CONNECTION_STATUS_ERROR;
3230 goto end;
3231 }
3232
3233 ret = connection_reset_protocol_state(conn);
3234 if (ret) {
3235 status = RELAY_CONNECTION_STATUS_ERROR;
3236 }
3237 end:
3238 return status;
3239 }
3240
3241 static enum relay_connection_status relay_process_control_receive_header(
3242 struct relay_connection *conn)
3243 {
3244 int ret = 0;
3245 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3246 struct lttcomm_relayd_hdr header;
3247 struct lttng_dynamic_buffer *reception_buffer =
3248 &conn->protocol.ctrl.reception_buffer;
3249 struct ctrl_connection_state_receive_header *state =
3250 &conn->protocol.ctrl.state.receive_header;
3251
3252 assert(state->left_to_receive != 0);
3253
3254 ret = conn->sock->ops->recvmsg(conn->sock,
3255 reception_buffer->data + state->received,
3256 state->left_to_receive, MSG_DONTWAIT);
3257 if (ret < 0) {
3258 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3259 PERROR("Unable to receive control command header on sock %d",
3260 conn->sock->fd);
3261 status = RELAY_CONNECTION_STATUS_ERROR;
3262 }
3263 goto end;
3264 } else if (ret == 0) {
3265 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
3266 status = RELAY_CONNECTION_STATUS_CLOSED;
3267 goto end;
3268 }
3269
3270 assert(ret > 0);
3271 assert(ret <= state->left_to_receive);
3272
3273 state->left_to_receive -= ret;
3274 state->received += ret;
3275
3276 if (state->left_to_receive > 0) {
3277 /*
3278 * Can't transition to the protocol's next state, wait to
3279 * receive the rest of the header.
3280 */
3281 DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
3282 state->received, state->left_to_receive,
3283 conn->sock->fd);
3284 goto end;
3285 }
3286
3287 /* Transition to next state: receiving the command's payload. */
3288 conn->protocol.ctrl.state_id =
3289 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD;
3290 memcpy(&header, reception_buffer->data, sizeof(header));
3291 header.circuit_id = be64toh(header.circuit_id);
3292 header.data_size = be64toh(header.data_size);
3293 header.cmd = be32toh(header.cmd);
3294 header.cmd_version = be32toh(header.cmd_version);
3295 memcpy(&conn->protocol.ctrl.state.receive_payload.header,
3296 &header, sizeof(header));
3297
3298 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes",
3299 conn->sock->fd, header.cmd, header.cmd_version,
3300 header.data_size);
3301
3302 if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) {
3303 ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.",
3304 header.data_size);
3305 status = RELAY_CONNECTION_STATUS_ERROR;
3306 goto end;
3307 }
3308
3309 conn->protocol.ctrl.state.receive_payload.left_to_receive =
3310 header.data_size;
3311 conn->protocol.ctrl.state.receive_payload.received = 0;
3312 ret = lttng_dynamic_buffer_set_size(reception_buffer,
3313 header.data_size);
3314 if (ret) {
3315 status = RELAY_CONNECTION_STATUS_ERROR;
3316 goto end;
3317 }
3318
3319 if (header.data_size == 0) {
3320 /*
3321 * Manually invoke the next state as the poll loop
3322 * will not wake-up to allow us to proceed further.
3323 */
3324 status = relay_process_control_receive_payload(conn);
3325 }
3326 end:
3327 return status;
3328 }
3329
3330 /*
3331 * Process the commands received on the control socket
3332 */
3333 static enum relay_connection_status relay_process_control(
3334 struct relay_connection *conn)
3335 {
3336 enum relay_connection_status status;
3337
3338 switch (conn->protocol.ctrl.state_id) {
3339 case CTRL_CONNECTION_STATE_RECEIVE_HEADER:
3340 status = relay_process_control_receive_header(conn);
3341 break;
3342 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD:
3343 status = relay_process_control_receive_payload(conn);
3344 break;
3345 default:
3346 ERR("Unknown control connection protocol state encountered.");
3347 abort();
3348 }
3349
3350 return status;
3351 }
3352
3353 /*
3354 * Handle index for a data stream.
3355 *
3356 * Called with the stream lock held.
3357 *
3358 * Return 0 on success else a negative value.
3359 */
3360 static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num,
3361 bool rotate_index, bool *flushed, uint64_t total_size)
3362 {
3363 int ret = 0;
3364 uint64_t data_offset;
3365 struct relay_index *index;
3366
3367 /* Get data offset because we are about to update the index. */
3368 data_offset = htobe64(stream->tracefile_size_current);
3369
3370 DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64,
3371 stream->stream_handle, net_seq_num, stream->tracefile_size_current);
3372
3373 /*
3374 * Lookup for an existing index for that stream id/sequence
3375 * number. If it exists, the control thread has already received the
3376 * data for it, thus we need to write it to disk.
3377 */
3378 index = relay_index_get_by_id_or_create(stream, net_seq_num);
3379 if (!index) {
3380 ret = -1;
3381 goto end;
3382 }
3383
3384 if (rotate_index || !stream->index_file) {
3385 ret = create_rotate_index_file(stream);
3386 if (ret < 0) {
3387 ERR("Failed to rotate index");
3388 /* Put self-ref for this index due to error. */
3389 relay_index_put(index);
3390 index = NULL;
3391 goto end;
3392 }
3393 }
3394
3395 if (relay_index_set_file(index, stream->index_file, data_offset)) {
3396 ret = -1;
3397 /* Put self-ref for this index due to error. */
3398 relay_index_put(index);
3399 index = NULL;
3400 goto end;
3401 }
3402
3403 ret = relay_index_try_flush(index);
3404 if (ret == 0) {
3405 tracefile_array_commit_seq(stream->tfa);
3406 stream->index_received_seqcount++;
3407 *flushed = true;
3408 } else if (ret > 0) {
3409 index->total_size = total_size;
3410 /* No flush. */
3411 ret = 0;
3412 } else {
3413 /* Put self-ref for this index due to error. */
3414 relay_index_put(index);
3415 index = NULL;
3416 ret = -1;
3417 }
3418 end:
3419 return ret;
3420 }
3421
3422 static enum relay_connection_status relay_process_data_receive_header(
3423 struct relay_connection *conn)
3424 {
3425 int ret;
3426 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3427 struct data_connection_state_receive_header *state =
3428 &conn->protocol.data.state.receive_header;
3429 struct lttcomm_relayd_data_hdr header;
3430 struct relay_stream *stream;
3431
3432 assert(state->left_to_receive != 0);
3433
3434 ret = conn->sock->ops->recvmsg(conn->sock,
3435 state->header_reception_buffer + state->received,
3436 state->left_to_receive, MSG_DONTWAIT);
3437 if (ret < 0) {
3438 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3439 PERROR("Unable to receive data header on sock %d", conn->sock->fd);
3440 status = RELAY_CONNECTION_STATUS_ERROR;
3441 }
3442 goto end;
3443 } else if (ret == 0) {
3444 /* Orderly shutdown. Not necessary to print an error. */
3445 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
3446 status = RELAY_CONNECTION_STATUS_CLOSED;
3447 goto end;
3448 }
3449
3450 assert(ret > 0);
3451 assert(ret <= state->left_to_receive);
3452
3453 state->left_to_receive -= ret;
3454 state->received += ret;
3455
3456 if (state->left_to_receive > 0) {
3457 /*
3458 * Can't transition to the protocol's next state, wait to
3459 * receive the rest of the header.
3460 */
3461 DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
3462 state->received, state->left_to_receive,
3463 conn->sock->fd);
3464 goto end;
3465 }
3466
3467 /* Transition to next state: receiving the payload. */
3468 conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD;
3469
3470 memcpy(&header, state->header_reception_buffer, sizeof(header));
3471 header.circuit_id = be64toh(header.circuit_id);
3472 header.stream_id = be64toh(header.stream_id);
3473 header.data_size = be32toh(header.data_size);
3474 header.net_seq_num = be64toh(header.net_seq_num);
3475 header.padding_size = be32toh(header.padding_size);
3476 memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header));
3477
3478 conn->protocol.data.state.receive_payload.left_to_receive =
3479 header.data_size;
3480 conn->protocol.data.state.receive_payload.received = 0;
3481 conn->protocol.data.state.receive_payload.rotate_index = false;
3482
3483 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32,
3484 conn->sock->fd, header.circuit_id,
3485 header.stream_id, header.data_size,
3486 header.net_seq_num, header.padding_size);
3487
3488 stream = stream_get_by_id(header.stream_id);
3489 if (!stream) {
3490 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64,
3491 header.stream_id);
3492 /* Protocol error. */
3493 status = RELAY_CONNECTION_STATUS_ERROR;
3494 goto end;
3495 }
3496
3497 pthread_mutex_lock(&stream->lock);
3498
3499 /* Check if a rotation is needed. */
3500 if (stream->tracefile_size > 0 &&
3501 (stream->tracefile_size_current + header.data_size) >
3502 stream->tracefile_size) {
3503 uint64_t old_id, new_id;
3504
3505 old_id = tracefile_array_get_file_index_head(stream->tfa);
3506 tracefile_array_file_rotate(stream->tfa);
3507
3508 /* new_id is updated by utils_rotate_stream_file. */
3509 new_id = old_id;
3510
3511 ret = stream_fd_rotate(stream->stream_fd, stream->path_name,
3512 stream->channel_name, stream->tracefile_size,
3513 stream->tracefile_count, &new_id);
3514 if (ret < 0) {
3515 ERR("Failed to rotate stream output file");
3516 status = RELAY_CONNECTION_STATUS_ERROR;
3517 goto end_stream_unlock;
3518 }
3519
3520 /*
3521 * Reset current size because we just performed a stream
3522 * rotation.
3523 */
3524 stream->tracefile_size_current = 0;
3525 conn->protocol.data.state.receive_payload.rotate_index = true;
3526 }
3527
3528 end_stream_unlock:
3529 pthread_mutex_unlock(&stream->lock);
3530 stream_put(stream);
3531 end:
3532 return status;
3533 }
3534
3535 static enum relay_connection_status relay_process_data_receive_payload(
3536 struct relay_connection *conn)
3537 {
3538 int ret;
3539 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3540 struct relay_stream *stream;
3541 struct data_connection_state_receive_payload *state =
3542 &conn->protocol.data.state.receive_payload;
3543 const size_t chunk_size = RECV_DATA_BUFFER_SIZE;
3544 char data_buffer[chunk_size];
3545 bool partial_recv = false;
3546 bool new_stream = false, close_requested = false, index_flushed = false;
3547 uint64_t left_to_receive = state->left_to_receive;
3548 struct relay_session *session;
3549 int stream_fd = -1;
3550
3551 DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive",
3552 state->header.stream_id, state->header.net_seq_num,
3553 state->received, left_to_receive);
3554
3555 stream = stream_get_by_id(state->header.stream_id);
3556 if (!stream) {
3557 /* Protocol error. */
3558 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64,
3559 state->header.stream_id);
3560 status = RELAY_CONNECTION_STATUS_ERROR;
3561 goto end;
3562 }
3563
3564 pthread_mutex_lock(&stream->lock);
3565 session = stream->trace->session;
3566 if (!conn->session) {
3567 ret = connection_set_session(conn, session);
3568 if (ret) {
3569 status = RELAY_CONNECTION_STATUS_ERROR;
3570 goto end_stream_unlock;
3571 }
3572 }
3573
3574 stream_fd = stream_fd_get_fd(stream->stream_fd);
3575 if (stream_fd < 0) {
3576 status = RELAY_CONNECTION_STATUS_ERROR;
3577 goto end_stream_unlock;
3578 }
3579
3580 /*
3581 * The size of the "chunk" received on any iteration is bounded by:
3582 * - the data left to receive,
3583 * - the data immediately available on the socket,
3584 * - the on-stack data buffer
3585 */
3586 while (left_to_receive > 0 && !partial_recv) {
3587 ssize_t write_ret;
3588 size_t recv_size = min(left_to_receive, chunk_size);
3589
3590 ret = conn->sock->ops->recvmsg(conn->sock, data_buffer,
3591 recv_size, MSG_DONTWAIT);
3592 if (ret < 0) {
3593 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3594 PERROR("Socket %d error", conn->sock->fd);
3595 status = RELAY_CONNECTION_STATUS_ERROR;
3596 }
3597 goto end_put_fd;
3598 } else if (ret == 0) {
3599 /* No more data ready to be consumed on socket. */
3600 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64,
3601 state->header.stream_id);
3602 status = RELAY_CONNECTION_STATUS_CLOSED;
3603 break;
3604 } else if (ret < (int) recv_size) {
3605 /*
3606 * All the data available on the socket has been
3607 * consumed.
3608 */
3609 partial_recv = true;
3610 }
3611
3612 recv_size = ret;
3613
3614 /* Write data to stream output fd. */
3615 write_ret = lttng_write(stream_fd, data_buffer,
3616 recv_size);
3617 if (write_ret < (ssize_t) recv_size) {
3618 ERR("Relay error writing data to file");
3619 status = RELAY_CONNECTION_STATUS_ERROR;
3620 goto end_put_fd;
3621 }
3622
3623 left_to_receive -= recv_size;
3624 state->received += recv_size;
3625 state->left_to_receive = left_to_receive;
3626
3627 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64,
3628 write_ret, stream->stream_handle);
3629 }
3630
3631 if (state->left_to_receive > 0) {
3632 /*
3633 * Did not receive all the data expected, wait for more data to
3634 * become available on the socket.
3635 */
3636 DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive",
3637 state->header.stream_id, state->received,
3638 state->left_to_receive);
3639 goto end_put_fd;
3640 }
3641
3642 ret = write_padding_to_file(stream_fd,
3643 state->header.padding_size);
3644 if ((int64_t) ret < (int64_t) state->header.padding_size) {
3645 ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d",
3646 stream->stream_handle,
3647 state->header.net_seq_num, ret);
3648 status = RELAY_CONNECTION_STATUS_ERROR;
3649 goto end_put_fd;
3650 }
3651
3652
3653 if (session->minor >= 4 && !session->snapshot) {
3654 ret = handle_index_data(stream, state->header.net_seq_num,
3655 state->rotate_index, &index_flushed, state->header.data_size + state->header.padding_size);
3656 if (ret < 0) {
3657 ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d",
3658 stream->stream_handle,
3659 state->header.net_seq_num, ret);
3660 status = RELAY_CONNECTION_STATUS_ERROR;
3661 goto end_put_fd;
3662 }
3663 }
3664
3665 stream->tracefile_size_current += state->header.data_size +
3666 state->header.padding_size;
3667
3668 if (stream->prev_seq == -1ULL) {
3669 new_stream = true;
3670 }
3671 if (index_flushed) {
3672 stream->pos_after_last_complete_data_index =
3673 stream->tracefile_size_current;
3674 }
3675
3676 stream->prev_seq = state->header.net_seq_num;
3677
3678 /*
3679 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3680 * contents of *state which are aliased (union) to the same location as
3681 * the new state. Don't use it beyond this point.
3682 */
3683 connection_reset_protocol_state(conn);
3684 state = NULL;
3685
3686 ret = try_rotate_stream(stream);
3687 if (ret < 0) {
3688 status = RELAY_CONNECTION_STATUS_ERROR;
3689 goto end_put_fd;
3690 }
3691 end_put_fd:
3692 (void) stream_fd_put_fd(stream->stream_fd);
3693 end_stream_unlock:
3694 close_requested = stream->close_requested;
3695 pthread_mutex_unlock(&stream->lock);
3696 if (close_requested && left_to_receive == 0) {
3697 try_stream_close(stream);
3698 }
3699
3700 if (new_stream) {
3701 pthread_mutex_lock(&session->lock);
3702 uatomic_set(&session->new_streams, 1);
3703 pthread_mutex_unlock(&session->lock);
3704 }
3705
3706 stream_put(stream);
3707 end:
3708 return status;
3709 }
3710
3711 /*
3712 * relay_process_data: Process the data received on the data socket
3713 */
3714 static enum relay_connection_status relay_process_data(
3715 struct relay_connection *conn)
3716 {
3717 enum relay_connection_status status;
3718
3719 switch (conn->protocol.data.state_id) {
3720 case DATA_CONNECTION_STATE_RECEIVE_HEADER:
3721 status = relay_process_data_receive_header(conn);
3722 break;
3723 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD:
3724 status = relay_process_data_receive_payload(conn);
3725 break;
3726 default:
3727 ERR("Unexpected data connection communication state.");
3728 abort();
3729 }
3730
3731 return status;
3732 }
3733
3734 static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
3735 {
3736 int ret;
3737
3738 (void) lttng_poll_del(events, pollfd);
3739
3740 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &pollfd, 1,
3741 fd_tracker_util_close_fd, NULL);
3742 if (ret < 0) {
3743 ERR("Closing pollfd %d", pollfd);
3744 }
3745 }
3746
3747 static void relay_thread_close_connection(struct lttng_poll_event *events,
3748 int pollfd, struct relay_connection *conn)
3749 {
3750 const char *type_str;
3751
3752 switch (conn->type) {
3753 case RELAY_DATA:
3754 type_str = "Data";
3755 break;
3756 case RELAY_CONTROL:
3757 type_str = "Control";
3758 break;
3759 case RELAY_VIEWER_COMMAND:
3760 type_str = "Viewer Command";
3761 break;
3762 case RELAY_VIEWER_NOTIFICATION:
3763 type_str = "Viewer Notification";
3764 break;
3765 default:
3766 type_str = "Unknown";
3767 }
3768 cleanup_connection_pollfd(events, pollfd);
3769 connection_put(conn);
3770 DBG("%s connection closed with %d", type_str, pollfd);
3771 }
3772
3773 /*
3774 * This thread does the actual work
3775 */
3776 static void *relay_thread_worker(void *data)
3777 {
3778 int ret, err = -1, last_seen_data_fd = -1;
3779 uint32_t nb_fd;
3780 struct lttng_poll_event events;
3781 struct lttng_ht *relay_connections_ht;
3782 struct lttng_ht_iter iter;
3783 struct relay_connection *destroy_conn = NULL;
3784
3785 DBG("[thread] Relay worker started");
3786
3787 rcu_register_thread();
3788
3789 health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER);
3790
3791 if (testpoint(relayd_thread_worker)) {
3792 goto error_testpoint;
3793 }
3794
3795 health_code_update();
3796
3797 /* table of connections indexed on socket */
3798 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3799 if (!relay_connections_ht) {
3800 goto relay_connections_ht_error;
3801 }
3802
3803 ret = create_named_thread_poll_set(&events, 2, "Worker thread epoll");
3804 if (ret < 0) {
3805 goto error_poll_create;
3806 }
3807
3808 ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
3809 if (ret < 0) {
3810 goto error;
3811 }
3812
3813 restart:
3814 while (1) {
3815 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
3816
3817 health_code_update();
3818
3819 /* Infinite blocking call, waiting for transmission */
3820 DBG3("Relayd worker thread polling...");
3821 health_poll_entry();
3822 ret = lttng_poll_wait(&events, -1);
3823 health_poll_exit();
3824 if (ret < 0) {
3825 /*
3826 * Restart interrupted system call.
3827 */
3828 if (errno == EINTR) {
3829 goto restart;
3830 }
3831 goto error;
3832 }
3833
3834 nb_fd = ret;
3835
3836 /*
3837 * Process control. The control connection is
3838 * prioritized so we don't starve it with high
3839 * throughput tracing data on the data connection.
3840 */
3841 for (i = 0; i < nb_fd; i++) {
3842 /* Fetch once the poll data */
3843 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
3844 int pollfd = LTTNG_POLL_GETFD(&events, i);
3845
3846 health_code_update();
3847
3848 if (!revents) {
3849 /*
3850 * No activity for this FD (poll
3851 * implementation).
3852 */
3853 continue;
3854 }
3855
3856 /* Thread quit pipe has been closed. Killing thread. */
3857 ret = check_thread_quit_pipe(pollfd, revents);
3858 if (ret) {
3859 err = 0;
3860 goto exit;
3861 }
3862
3863 /* Inspect the relay conn pipe for new connection */
3864 if (pollfd == relay_conn_pipe[0]) {
3865 if (revents & LPOLLIN) {
3866 struct relay_connection *conn;
3867
3868 ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn));
3869 if (ret < 0) {
3870 goto error;
3871 }
3872 lttng_poll_add(&events, conn->sock->fd,
3873 LPOLLIN | LPOLLRDHUP);
3874 connection_ht_add(relay_connections_ht, conn);
3875 DBG("Connection socket %d added", conn->sock->fd);
3876 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3877 ERR("Relay connection pipe error");
3878 goto error;
3879 } else {
3880 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3881 goto error;
3882 }
3883 } else {
3884 struct relay_connection *ctrl_conn;
3885
3886 ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd);
3887 /* If not found, there is a synchronization issue. */
3888 assert(ctrl_conn);
3889
3890 if (ctrl_conn->type == RELAY_DATA) {
3891 if (revents & LPOLLIN) {
3892 /*
3893 * Flag the last seen data fd not deleted. It will be
3894 * used as the last seen fd if any fd gets deleted in
3895 * this first loop.
3896 */
3897 last_notdel_data_fd = pollfd;
3898 }
3899 goto put_ctrl_connection;
3900 }
3901 assert(ctrl_conn->type == RELAY_CONTROL);
3902
3903 if (revents & LPOLLIN) {
3904 enum relay_connection_status status;
3905
3906 status = relay_process_control(ctrl_conn);
3907 if (status != RELAY_CONNECTION_STATUS_OK) {
3908 /*
3909 * On socket error flag the session as aborted to force
3910 * the cleanup of its stream otherwise it can leak
3911 * during the lifetime of the relayd.
3912 *
3913 * This prevents situations in which streams can be
3914 * left opened because an index was received, the
3915 * control connection is closed, and the data
3916 * connection is closed (uncleanly) before the packet's
3917 * data provided.
3918 *
3919 * Since the control connection encountered an error,
3920 * it is okay to be conservative and close the
3921 * session right now as we can't rely on the protocol
3922 * being respected anymore.
3923 */
3924 if (status == RELAY_CONNECTION_STATUS_ERROR) {
3925 session_abort(ctrl_conn->session);
3926 }
3927
3928 /* Clear the connection on error or close. */
3929 relay_thread_close_connection(&events,
3930 pollfd,
3931 ctrl_conn);
3932 }
3933 seen_control = 1;
3934 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3935 relay_thread_close_connection(&events,
3936 pollfd, ctrl_conn);
3937 if (last_seen_data_fd == pollfd) {
3938 last_seen_data_fd = last_notdel_data_fd;
3939 }
3940 } else {
3941 ERR("Unexpected poll events %u for control sock %d",
3942 revents, pollfd);
3943 connection_put(ctrl_conn);
3944 goto error;
3945 }
3946 put_ctrl_connection:
3947 connection_put(ctrl_conn);
3948 }
3949 }
3950
3951 /*
3952 * The last loop handled a control request, go back to poll to make
3953 * sure we prioritise the control socket.
3954 */
3955 if (seen_control) {
3956 continue;
3957 }
3958
3959 if (last_seen_data_fd >= 0) {
3960 for (i = 0; i < nb_fd; i++) {
3961 int pollfd = LTTNG_POLL_GETFD(&events, i);
3962
3963 health_code_update();
3964
3965 if (last_seen_data_fd == pollfd) {
3966 idx = i;
3967 break;
3968 }
3969 }
3970 }
3971
3972 /* Process data connection. */
3973 for (i = idx + 1; i < nb_fd; i++) {
3974 /* Fetch the poll data. */
3975 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
3976 int pollfd = LTTNG_POLL_GETFD(&events, i);
3977 struct relay_connection *data_conn;
3978
3979 health_code_update();
3980
3981 if (!revents) {
3982 /* No activity for this FD (poll implementation). */
3983 continue;
3984 }
3985
3986 /* Skip the command pipe. It's handled in the first loop. */
3987 if (pollfd == relay_conn_pipe[0]) {
3988 continue;
3989 }
3990
3991 data_conn = connection_get_by_sock(relay_connections_ht, pollfd);
3992 if (!data_conn) {
3993 /* Skip it. Might be removed before. */
3994 continue;
3995 }
3996 if (data_conn->type == RELAY_CONTROL) {
3997 goto put_data_connection;
3998 }
3999 assert(data_conn->type == RELAY_DATA);
4000
4001 if (revents & LPOLLIN) {
4002 enum relay_connection_status status;
4003
4004 status = relay_process_data(data_conn);
4005 /* Connection closed or error. */
4006 if (status != RELAY_CONNECTION_STATUS_OK) {
4007 /*
4008 * On socket error flag the session as aborted to force
4009 * the cleanup of its stream otherwise it can leak
4010 * during the lifetime of the relayd.
4011 *
4012 * This prevents situations in which streams can be
4013 * left opened because an index was received, the
4014 * control connection is closed, and the data
4015 * connection is closed (uncleanly) before the packet's
4016 * data provided.
4017 *
4018 * Since the data connection encountered an error,
4019 * it is okay to be conservative and close the
4020 * session right now as we can't rely on the protocol
4021 * being respected anymore.
4022 */
4023 if (status == RELAY_CONNECTION_STATUS_ERROR) {
4024 session_abort(data_conn->session);
4025 }
4026 relay_thread_close_connection(&events, pollfd,
4027 data_conn);
4028 /*
4029 * Every goto restart call sets the last seen fd where
4030 * here we don't really care since we gracefully
4031 * continue the loop after the connection is deleted.
4032 */
4033 } else {
4034 /* Keep last seen port. */
4035 last_seen_data_fd = pollfd;
4036 connection_put(data_conn);
4037 goto restart;
4038 }
4039 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4040 relay_thread_close_connection(&events, pollfd,
4041 data_conn);
4042 } else {
4043 ERR("Unknown poll events %u for data sock %d",
4044 revents, pollfd);
4045 }
4046 put_data_connection:
4047 connection_put(data_conn);
4048 }
4049 last_seen_data_fd = -1;
4050 }
4051
4052 /* Normal exit, no error */
4053 ret = 0;
4054
4055 exit:
4056 error:
4057 /* Cleanup remaining connection object. */
4058 rcu_read_lock();
4059 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter,
4060 destroy_conn,
4061 sock_n.node) {
4062 health_code_update();
4063
4064 session_abort(destroy_conn->session);
4065
4066 /*
4067 * No need to grab another ref, because we own
4068 * destroy_conn.
4069 */
4070 relay_thread_close_connection(&events, destroy_conn->sock->fd,
4071 destroy_conn);
4072 }
4073 rcu_read_unlock();
4074
4075 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
4076 error_poll_create:
4077 lttng_ht_destroy(relay_connections_ht);
4078 relay_connections_ht_error:
4079 /* Close relay conn pipes */
4080 (void) fd_tracker_util_pipe_close(the_fd_tracker,
4081 relay_conn_pipe);
4082 if (err) {
4083 DBG("Thread exited with error");
4084 }
4085 DBG("Worker thread cleanup complete");
4086 error_testpoint:
4087 if (err) {
4088 health_error();
4089 ERR("Health error occurred in %s", __func__);
4090 }
4091 health_unregister(health_relayd);
4092 rcu_unregister_thread();
4093 lttng_relay_stop_threads();
4094 return NULL;
4095 }
4096
4097 /*
4098 * Create the relay command pipe to wake thread_manage_apps.
4099 * Closed in cleanup().
4100 */
4101 static int create_relay_conn_pipe(void)
4102 {
4103 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
4104 "Relayd connection pipe", relay_conn_pipe);
4105 }
4106
4107 static
4108 int stdio_open(void *data, int *fds)
4109 {
4110 fds[0] = fileno(stdout);
4111 fds[1] = fileno(stderr);
4112 return 0;
4113 }
4114
4115 static
4116 int noop_close(void *data, int *fds)
4117 {
4118 return 0;
4119 }
4120
4121 static
4122 int track_stdio(void)
4123 {
4124 int fds[2];
4125 const char *names[] = { "stdout", "stderr" };
4126
4127 return fd_tracker_open_unsuspendable_fd(the_fd_tracker, fds,
4128 names, 2, stdio_open, NULL);
4129 }
4130
4131 static
4132 void untrack_stdio(void)
4133 {
4134 int fds[] = { fileno(stdout), fileno(stderr) };
4135
4136 /*
4137 * noop_close is used since we don't really want to close
4138 * the stdio output fds; we merely want to stop tracking them.
4139 */
4140 (void) fd_tracker_close_unsuspendable_fd(the_fd_tracker,
4141 fds, 2, noop_close, NULL);
4142 }
4143
4144 /*
4145 * main
4146 */
4147 int main(int argc, char **argv)
4148 {
4149 int ret = 0, retval = 0;
4150 void *status;
4151
4152 /* Parse arguments */
4153 progname = argv[0];
4154 if (set_options(argc, argv)) {
4155 retval = -1;
4156 goto exit_options;
4157 }
4158
4159 if (set_signal_handler()) {
4160 retval = -1;
4161 goto exit_options;
4162 }
4163
4164 ret = fclose(stdin);
4165 if (ret) {
4166 PERROR("Failed to close stdin");
4167 goto exit_options;
4168 }
4169 /* Try to create directory if -o, --output is specified. */
4170 if (opt_output_path) {
4171 if (*opt_output_path != '/') {
4172 ERR("Please specify an absolute path for -o, --output PATH");
4173 retval = -1;
4174 goto exit_options;
4175 }
4176
4177 ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG,
4178 -1, -1);
4179 if (ret < 0) {
4180 ERR("Unable to create %s", opt_output_path);
4181 retval = -1;
4182 goto exit_options;
4183 }
4184 }
4185
4186 /* Daemonize */
4187 if (opt_daemon || opt_background) {
4188 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
4189 !opt_background);
4190 if (ret < 0) {
4191 retval = -1;
4192 goto exit_options;
4193 }
4194 }
4195
4196 /*
4197 * The RCU thread registration (and use, through the fd-tracker's
4198 * creation) is done after the daemonization to allow us to not
4199 * deal with liburcu's fork() management as the call RCU needs to
4200 * be restored.
4201 */
4202 rcu_register_thread();
4203
4204 the_fd_tracker = fd_tracker_create(lttng_opt_fd_pool_size);
4205 if (!the_fd_tracker) {
4206 retval = -1;
4207 goto exit_options;
4208 }
4209
4210 ret = track_stdio();
4211 if (ret) {
4212 retval = -1;
4213 goto exit_tracker;
4214 }
4215
4216 /* Initialize thread health monitoring */
4217 health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
4218 if (!health_relayd) {
4219 PERROR("health_app_create error");
4220 retval = -1;
4221 goto exit_health_app_create;
4222 }
4223
4224 /* Create thread quit pipe */
4225 if (init_thread_quit_pipe()) {
4226 retval = -1;
4227 goto exit_init_data;
4228 }
4229
4230 /* Setup the thread apps communication pipe. */
4231 if (create_relay_conn_pipe()) {
4232 retval = -1;
4233 goto exit_init_data;
4234 }
4235
4236 /* Init relay command queue. */
4237 cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail);
4238
4239 /* Initialize communication library */
4240 lttcomm_init();
4241 lttcomm_inet_init();
4242
4243 /* tables of sessions indexed by session ID */
4244 sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4245 if (!sessions_ht) {
4246 retval = -1;
4247 goto exit_init_data;
4248 }
4249
4250 /* tables of streams indexed by stream ID */
4251 relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4252 if (!relay_streams_ht) {
4253 retval = -1;
4254 goto exit_init_data;
4255 }
4256
4257 /* tables of streams indexed by stream ID */
4258 viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4259 if (!viewer_streams_ht) {
4260 retval = -1;
4261 goto exit_init_data;
4262 }
4263
4264 ret = init_health_quit_pipe();
4265 if (ret) {
4266 retval = -1;
4267 goto exit_health_quit_pipe;
4268 }
4269
4270 /* Create thread to manage the client socket */
4271 ret = pthread_create(&health_thread, default_pthread_attr(),
4272 thread_manage_health, (void *) NULL);
4273 if (ret) {
4274 errno = ret;
4275 PERROR("pthread_create health");
4276 retval = -1;
4277 goto exit_health_thread;
4278 }
4279
4280 /* Setup the dispatcher thread */
4281 ret = pthread_create(&dispatcher_thread, default_pthread_attr(),
4282 relay_thread_dispatcher, (void *) NULL);
4283 if (ret) {
4284 errno = ret;
4285 PERROR("pthread_create dispatcher");
4286 retval = -1;
4287 goto exit_dispatcher_thread;
4288 }
4289
4290 /* Setup the worker thread */
4291 ret = pthread_create(&worker_thread, default_pthread_attr(),
4292 relay_thread_worker, NULL);
4293 if (ret) {
4294 errno = ret;
4295 PERROR("pthread_create worker");
4296 retval = -1;
4297 goto exit_worker_thread;
4298 }
4299
4300 /* Setup the listener thread */
4301 ret = pthread_create(&listener_thread, default_pthread_attr(),
4302 relay_thread_listener, (void *) NULL);
4303 if (ret) {
4304 errno = ret;
4305 PERROR("pthread_create listener");
4306 retval = -1;
4307 goto exit_listener_thread;
4308 }
4309
4310 ret = relayd_live_create(live_uri);
4311 if (ret) {
4312 ERR("Starting live viewer threads");
4313 retval = -1;
4314 goto exit_live;
4315 }
4316
4317 /*
4318 * This is where we start awaiting program completion (e.g. through
4319 * signal that asks threads to teardown).
4320 */
4321
4322 ret = relayd_live_join();
4323 if (ret) {
4324 retval = -1;
4325 }
4326 exit_live:
4327
4328 ret = pthread_join(listener_thread, &status);
4329 if (ret) {
4330 errno = ret;
4331 PERROR("pthread_join listener_thread");
4332 retval = -1;
4333 }
4334
4335 exit_listener_thread:
4336 ret = pthread_join(worker_thread, &status);
4337 if (ret) {
4338 errno = ret;
4339 PERROR("pthread_join worker_thread");
4340 retval = -1;
4341 }
4342
4343 exit_worker_thread:
4344 ret = pthread_join(dispatcher_thread, &status);
4345 if (ret) {
4346 errno = ret;
4347 PERROR("pthread_join dispatcher_thread");
4348 retval = -1;
4349 }
4350 exit_dispatcher_thread:
4351
4352 ret = pthread_join(health_thread, &status);
4353 if (ret) {
4354 errno = ret;
4355 PERROR("pthread_join health_thread");
4356 retval = -1;
4357 }
4358 exit_health_thread:
4359
4360 (void) fd_tracker_util_pipe_close(the_fd_tracker, health_quit_pipe);
4361 exit_health_quit_pipe:
4362
4363 exit_init_data:
4364 health_app_destroy(health_relayd);
4365 exit_health_app_create:
4366
4367 /*
4368 * Wait for all pending call_rcu work to complete before tearing
4369 * down data structures. call_rcu worker may be trying to
4370 * perform lookups in those structures.
4371 */
4372 rcu_barrier();
4373 relayd_cleanup();
4374
4375 /* Ensure all prior call_rcu are done. */
4376 rcu_barrier();
4377 exit_tracker:
4378 untrack_stdio();
4379 /*
4380 * fd_tracker_destroy() will log the contents of the fd-tracker
4381 * if a leak is detected.
4382 */
4383 fd_tracker_destroy(the_fd_tracker);
4384 rcu_unregister_thread();
4385 exit_options:
4386 if (!retval) {
4387 exit(EXIT_SUCCESS);
4388 } else {
4389 exit(EXIT_FAILURE);
4390 }
4391 }
This page took 0.119977 seconds and 5 git commands to generate.