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