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