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