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