Use free running metadata channel key between sessiond and kernel consumer
[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>
f40ef1d5 57#include <common/config/session-config.h>
7591bab1 58#include <urcu/rculist.h>
b8aa1682 59
0f907de1 60#include "cmd.h"
d3e2ba59 61#include "ctf-trace.h"
1c20f0e2 62#include "index.h"
0f907de1 63#include "utils.h"
b8aa1682 64#include "lttng-relayd.h"
d3e2ba59 65#include "live.h"
55706a7d 66#include "health-relayd.h"
9b5e0863 67#include "testpoint.h"
2f8f53af 68#include "viewer-stream.h"
2a174661
DG
69#include "session.h"
70#include "stream.h"
58eb9381 71#include "connection.h"
a44ca2ca 72#include "tracefile-array.h"
f056029c 73#include "tcp_keep_alive.h"
b8aa1682 74
4fc83d94
PP
75static const char *help_msg =
76#ifdef LTTNG_EMBED_HELP
77#include <lttng-relayd.8.h>
78#else
79NULL
80#endif
81;
82
b8aa1682 83/* command line options */
0f907de1 84char *opt_output_path;
b5218ffb 85static int opt_daemon, opt_background;
3fd27398
MD
86
87/*
88 * We need to wait for listener and live listener threads, as well as
89 * health check thread, before being ready to signal readiness.
90 */
91#define NR_LTTNG_RELAY_READY 3
92static int lttng_relay_ready = NR_LTTNG_RELAY_READY;
0848dba7
MD
93
94/* Size of receive buffer. */
95#define RECV_DATA_BUFFER_SIZE 65536
96
3fd27398
MD
97static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */
98static pid_t child_ppid; /* Internal parent PID use with daemonize. */
99
095a4ae5
MD
100static struct lttng_uri *control_uri;
101static struct lttng_uri *data_uri;
d3e2ba59 102static struct lttng_uri *live_uri;
b8aa1682
JD
103
104const char *progname;
b8aa1682 105
65931c8b 106const char *tracing_group_name = DEFAULT_TRACING_GROUP;
cd60b05a
JG
107static int tracing_group_name_override;
108
109const char * const config_section_name = "relayd";
65931c8b 110
b8aa1682
JD
111/*
112 * Quit pipe for all threads. This permits a single cancellation point
113 * for all threads when receiving an event on the pipe.
114 */
0b242f62 115int thread_quit_pipe[2] = { -1, -1 };
b8aa1682
JD
116
117/*
118 * This pipe is used to inform the worker thread that a command is queued and
119 * ready to be processed.
120 */
58eb9381 121static int relay_conn_pipe[2] = { -1, -1 };
b8aa1682 122
26c9d55e 123/* Shared between threads */
b8aa1682
JD
124static int dispatch_thread_exit;
125
126static pthread_t listener_thread;
127static pthread_t dispatcher_thread;
128static pthread_t worker_thread;
65931c8b 129static pthread_t health_thread;
b8aa1682 130
7591bab1
MD
131/*
132 * last_relay_stream_id_lock protects last_relay_stream_id increment
133 * atomicity on 32-bit architectures.
134 */
135static pthread_mutex_t last_relay_stream_id_lock = PTHREAD_MUTEX_INITIALIZER;
095a4ae5 136static uint64_t last_relay_stream_id;
b8aa1682
JD
137
138/*
139 * Relay command queue.
140 *
141 * The relay_thread_listener and relay_thread_dispatcher communicate with this
142 * queue.
143 */
58eb9381 144static struct relay_conn_queue relay_conn_queue;
b8aa1682
JD
145
146/* buffer allocated at startup, used to store the trace data */
095a4ae5
MD
147static char *data_buffer;
148static unsigned int data_buffer_size;
b8aa1682 149
d3e2ba59
JD
150/* Global relay stream hash table. */
151struct lttng_ht *relay_streams_ht;
152
92c6ca54
DG
153/* Global relay viewer stream hash table. */
154struct lttng_ht *viewer_streams_ht;
155
7591bab1
MD
156/* Global relay sessions hash table. */
157struct lttng_ht *sessions_ht;
0a6518b0 158
55706a7d 159/* Relayd health monitoring */
eea7556c 160struct health_app *health_relayd;
55706a7d 161
cd60b05a
JG
162static struct option long_options[] = {
163 { "control-port", 1, 0, 'C', },
164 { "data-port", 1, 0, 'D', },
8d5c808e 165 { "live-port", 1, 0, 'L', },
cd60b05a 166 { "daemonize", 0, 0, 'd', },
b5218ffb 167 { "background", 0, 0, 'b', },
cd60b05a
JG
168 { "group", 1, 0, 'g', },
169 { "help", 0, 0, 'h', },
170 { "output", 1, 0, 'o', },
171 { "verbose", 0, 0, 'v', },
172 { "config", 1, 0, 'f' },
3a904098 173 { "version", 0, 0, 'V' },
cd60b05a
JG
174 { NULL, 0, 0, 0, },
175};
176
3a904098 177static const char *config_ignore_options[] = { "help", "config", "version" };
cd60b05a 178
cd60b05a
JG
179/*
180 * Take an option from the getopt output and set it in the right variable to be
181 * used later.
182 *
183 * Return 0 on success else a negative value.
184 */
7591bab1 185static int set_option(int opt, const char *arg, const char *optname)
b8aa1682 186{
cd60b05a
JG
187 int ret;
188
189 switch (opt) {
190 case 0:
191 fprintf(stderr, "option %s", optname);
192 if (arg) {
193 fprintf(stderr, " with arg %s\n", arg);
194 }
195 break;
196 case 'C':
e8fa9fb0
MD
197 if (lttng_is_setuid_setgid()) {
198 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
199 "-C, --control-port");
200 } else {
201 ret = uri_parse(arg, &control_uri);
202 if (ret < 0) {
203 ERR("Invalid control URI specified");
204 goto end;
205 }
206 if (control_uri->port == 0) {
207 control_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
208 }
cd60b05a
JG
209 }
210 break;
211 case 'D':
e8fa9fb0
MD
212 if (lttng_is_setuid_setgid()) {
213 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
214 "-D, -data-port");
215 } else {
216 ret = uri_parse(arg, &data_uri);
217 if (ret < 0) {
218 ERR("Invalid data URI specified");
219 goto end;
220 }
221 if (data_uri->port == 0) {
222 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
223 }
cd60b05a
JG
224 }
225 break;
8d5c808e 226 case 'L':
e8fa9fb0
MD
227 if (lttng_is_setuid_setgid()) {
228 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
229 "-L, -live-port");
230 } else {
231 ret = uri_parse(arg, &live_uri);
232 if (ret < 0) {
233 ERR("Invalid live URI specified");
234 goto end;
235 }
236 if (live_uri->port == 0) {
237 live_uri->port = DEFAULT_NETWORK_VIEWER_PORT;
238 }
8d5c808e
AM
239 }
240 break;
cd60b05a
JG
241 case 'd':
242 opt_daemon = 1;
243 break;
b5218ffb
MD
244 case 'b':
245 opt_background = 1;
246 break;
cd60b05a 247 case 'g':
e8fa9fb0
MD
248 if (lttng_is_setuid_setgid()) {
249 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
250 "-g, --group");
251 } else {
252 tracing_group_name = strdup(arg);
253 if (tracing_group_name == NULL) {
254 ret = -errno;
255 PERROR("strdup");
256 goto end;
257 }
258 tracing_group_name_override = 1;
330a40bb 259 }
cd60b05a
JG
260 break;
261 case 'h':
4fc83d94 262 ret = utils_show_help(8, "lttng-relayd", help_msg);
655b5cc1 263 if (ret) {
4fc83d94 264 ERR("Cannot show --help for `lttng-relayd`");
655b5cc1
PP
265 perror("exec");
266 }
cd60b05a 267 exit(EXIT_FAILURE);
3a904098
AW
268 case 'V':
269 fprintf(stdout, "%s\n", VERSION);
270 exit(EXIT_SUCCESS);
cd60b05a 271 case 'o':
e8fa9fb0
MD
272 if (lttng_is_setuid_setgid()) {
273 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
274 "-o, --output");
275 } else {
276 ret = asprintf(&opt_output_path, "%s", arg);
277 if (ret < 0) {
278 ret = -errno;
279 PERROR("asprintf opt_output_path");
280 goto end;
281 }
cd60b05a
JG
282 }
283 break;
284 case 'v':
285 /* Verbose level can increase using multiple -v */
286 if (arg) {
287 lttng_opt_verbose = config_parse_value(arg);
288 } else {
849e5b7b
DG
289 /* Only 3 level of verbosity (-vvv). */
290 if (lttng_opt_verbose < 3) {
291 lttng_opt_verbose += 1;
292 }
cd60b05a
JG
293 }
294 break;
295 default:
296 /* Unknown option or other error.
297 * Error is printed by getopt, just return */
298 ret = -1;
299 goto end;
300 }
301
302 /* All good. */
303 ret = 0;
304
305end:
306 return ret;
307}
308
309/*
310 * config_entry_handler_cb used to handle options read from a config file.
f40ef1d5 311 * See config_entry_handler_cb comment in common/config/session-config.h for the
cd60b05a
JG
312 * return value conventions.
313 */
7591bab1 314static int config_entry_handler(const struct config_entry *entry, void *unused)
cd60b05a
JG
315{
316 int ret = 0, i;
317
318 if (!entry || !entry->name || !entry->value) {
319 ret = -EINVAL;
320 goto end;
321 }
322
323 /* Check if the option is to be ignored */
324 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
325 if (!strcmp(entry->name, config_ignore_options[i])) {
326 goto end;
327 }
328 }
329
330 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) {
331 /* Ignore if entry name is not fully matched. */
332 if (strcmp(entry->name, long_options[i].name)) {
333 continue;
334 }
335
336 /*
7591bab1
MD
337 * If the option takes no argument on the command line,
338 * we have to check if the value is "true". We support
339 * non-zero numeric values, true, on and yes.
cd60b05a
JG
340 */
341 if (!long_options[i].has_arg) {
342 ret = config_parse_value(entry->value);
343 if (ret <= 0) {
344 if (ret) {
345 WARN("Invalid configuration value \"%s\" for option %s",
346 entry->value, entry->name);
347 }
348 /* False, skip boolean config option. */
349 goto end;
350 }
351 }
352
353 ret = set_option(long_options[i].val, entry->value, entry->name);
354 goto end;
355 }
356
357 WARN("Unrecognized option \"%s\" in daemon configuration file.",
358 entry->name);
359
360end:
361 return ret;
362}
363
7591bab1 364static int set_options(int argc, char **argv)
cd60b05a 365{
178a0557 366 int c, ret = 0, option_index = 0, retval = 0;
cd60b05a
JG
367 int orig_optopt = optopt, orig_optind = optind;
368 char *default_address, *optstring;
369 const char *config_path = NULL;
370
371 optstring = utils_generate_optstring(long_options,
372 sizeof(long_options) / sizeof(struct option));
373 if (!optstring) {
178a0557 374 retval = -ENOMEM;
cd60b05a
JG
375 goto exit;
376 }
377
378 /* Check for the --config option */
379
380 while ((c = getopt_long(argc, argv, optstring, long_options,
381 &option_index)) != -1) {
382 if (c == '?') {
178a0557 383 retval = -EINVAL;
cd60b05a
JG
384 goto exit;
385 } else if (c != 'f') {
386 continue;
387 }
388
e8fa9fb0
MD
389 if (lttng_is_setuid_setgid()) {
390 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
391 "-f, --config");
392 } else {
393 config_path = utils_expand_path(optarg);
394 if (!config_path) {
395 ERR("Failed to resolve path: %s", optarg);
396 }
cd60b05a
JG
397 }
398 }
399
400 ret = config_get_section_entries(config_path, config_section_name,
401 config_entry_handler, NULL);
402 if (ret) {
403 if (ret > 0) {
404 ERR("Invalid configuration option at line %i", ret);
cd60b05a 405 }
178a0557 406 retval = -1;
cd60b05a
JG
407 goto exit;
408 }
b8aa1682 409
cd60b05a
JG
410 /* Reset getopt's global state */
411 optopt = orig_optopt;
412 optind = orig_optind;
b8aa1682 413 while (1) {
cd60b05a 414 c = getopt_long(argc, argv, optstring, long_options, &option_index);
b8aa1682
JD
415 if (c == -1) {
416 break;
417 }
418
cd60b05a
JG
419 ret = set_option(c, optarg, long_options[option_index].name);
420 if (ret < 0) {
178a0557 421 retval = -1;
b8aa1682
JD
422 goto exit;
423 }
424 }
425
426 /* assign default values */
427 if (control_uri == NULL) {
fa91dc52
MD
428 ret = asprintf(&default_address,
429 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d",
430 DEFAULT_NETWORK_CONTROL_PORT);
b8aa1682
JD
431 if (ret < 0) {
432 PERROR("asprintf default data address");
178a0557 433 retval = -1;
b8aa1682
JD
434 goto exit;
435 }
436
437 ret = uri_parse(default_address, &control_uri);
438 free(default_address);
439 if (ret < 0) {
440 ERR("Invalid control URI specified");
178a0557 441 retval = -1;
b8aa1682
JD
442 goto exit;
443 }
444 }
445 if (data_uri == NULL) {
fa91dc52
MD
446 ret = asprintf(&default_address,
447 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d",
448 DEFAULT_NETWORK_DATA_PORT);
b8aa1682
JD
449 if (ret < 0) {
450 PERROR("asprintf default data address");
178a0557 451 retval = -1;
b8aa1682
JD
452 goto exit;
453 }
454
455 ret = uri_parse(default_address, &data_uri);
456 free(default_address);
457 if (ret < 0) {
458 ERR("Invalid data URI specified");
178a0557 459 retval = -1;
b8aa1682
JD
460 goto exit;
461 }
462 }
d3e2ba59 463 if (live_uri == NULL) {
fa91dc52
MD
464 ret = asprintf(&default_address,
465 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d",
466 DEFAULT_NETWORK_VIEWER_PORT);
d3e2ba59
JD
467 if (ret < 0) {
468 PERROR("asprintf default viewer control address");
178a0557 469 retval = -1;
d3e2ba59
JD
470 goto exit;
471 }
472
473 ret = uri_parse(default_address, &live_uri);
474 free(default_address);
475 if (ret < 0) {
476 ERR("Invalid viewer control URI specified");
178a0557 477 retval = -1;
d3e2ba59
JD
478 goto exit;
479 }
480 }
b8aa1682
JD
481
482exit:
cd60b05a 483 free(optstring);
178a0557 484 return retval;
b8aa1682
JD
485}
486
7591bab1
MD
487static void print_global_objects(void)
488{
489 rcu_register_thread();
490
491 print_viewer_streams();
492 print_relay_streams();
493 print_sessions();
494
495 rcu_unregister_thread();
496}
497
b8aa1682
JD
498/*
499 * Cleanup the daemon
500 */
7591bab1 501static void relayd_cleanup(void)
b8aa1682 502{
7591bab1
MD
503 print_global_objects();
504
b8aa1682
JD
505 DBG("Cleaning up");
506
178a0557
MD
507 if (viewer_streams_ht)
508 lttng_ht_destroy(viewer_streams_ht);
509 if (relay_streams_ht)
510 lttng_ht_destroy(relay_streams_ht);
7591bab1
MD
511 if (sessions_ht)
512 lttng_ht_destroy(sessions_ht);
178a0557 513
095a4ae5
MD
514 /* free the dynamically allocated opt_output_path */
515 free(opt_output_path);
516
a02de639
CB
517 /* Close thread quit pipes */
518 utils_close_pipe(thread_quit_pipe);
519
710c1f73
DG
520 uri_free(control_uri);
521 uri_free(data_uri);
8d5c808e 522 /* Live URI is freed in the live thread. */
cd60b05a
JG
523
524 if (tracing_group_name_override) {
525 free((void *) tracing_group_name);
526 }
b8aa1682
JD
527}
528
529/*
530 * Write to writable pipe used to notify a thread.
531 */
7591bab1 532static int notify_thread_pipe(int wpipe)
b8aa1682 533{
6cd525e8 534 ssize_t ret;
b8aa1682 535
6cd525e8
MD
536 ret = lttng_write(wpipe, "!", 1);
537 if (ret < 1) {
b8aa1682 538 PERROR("write poll pipe");
b4aacfdc 539 goto end;
b8aa1682 540 }
b4aacfdc
MD
541 ret = 0;
542end:
b8aa1682
JD
543 return ret;
544}
545
7591bab1 546static int notify_health_quit_pipe(int *pipe)
65931c8b 547{
6cd525e8 548 ssize_t ret;
65931c8b 549
6cd525e8
MD
550 ret = lttng_write(pipe[1], "4", 1);
551 if (ret < 1) {
65931c8b 552 PERROR("write relay health quit");
b4aacfdc 553 goto end;
65931c8b 554 }
b4aacfdc
MD
555 ret = 0;
556end:
557 return ret;
65931c8b
MD
558}
559
b8aa1682 560/*
b4aacfdc 561 * Stop all relayd and relayd-live threads.
b8aa1682 562 */
b4aacfdc 563int lttng_relay_stop_threads(void)
b8aa1682 564{
b4aacfdc 565 int retval = 0;
b8aa1682
JD
566
567 /* Stopping all threads */
568 DBG("Terminating all threads");
b4aacfdc 569 if (notify_thread_pipe(thread_quit_pipe[1])) {
b8aa1682 570 ERR("write error on thread quit pipe");
b4aacfdc 571 retval = -1;
b8aa1682
JD
572 }
573
b4aacfdc
MD
574 if (notify_health_quit_pipe(health_quit_pipe)) {
575 ERR("write error on health quit pipe");
576 }
65931c8b 577
b8aa1682 578 /* Dispatch thread */
26c9d55e 579 CMM_STORE_SHARED(dispatch_thread_exit, 1);
58eb9381 580 futex_nto1_wake(&relay_conn_queue.futex);
178a0557 581
b4aacfdc 582 if (relayd_live_stop()) {
178a0557 583 ERR("Error stopping live threads");
b4aacfdc 584 retval = -1;
178a0557 585 }
b4aacfdc 586 return retval;
b8aa1682
JD
587}
588
589/*
590 * Signal handler for the daemon
591 *
592 * Simply stop all worker threads, leaving main() return gracefully after
593 * joining all threads and calling cleanup().
594 */
7591bab1 595static void sighandler(int sig)
b8aa1682
JD
596{
597 switch (sig) {
b8aa1682
JD
598 case SIGINT:
599 DBG("SIGINT caught");
b4aacfdc
MD
600 if (lttng_relay_stop_threads()) {
601 ERR("Error stopping threads");
602 }
b8aa1682
JD
603 break;
604 case SIGTERM:
605 DBG("SIGTERM caught");
b4aacfdc
MD
606 if (lttng_relay_stop_threads()) {
607 ERR("Error stopping threads");
608 }
b8aa1682 609 break;
3fd27398
MD
610 case SIGUSR1:
611 CMM_STORE_SHARED(recv_child_signal, 1);
612 break;
b8aa1682
JD
613 default:
614 break;
615 }
616}
617
618/*
619 * Setup signal handler for :
620 * SIGINT, SIGTERM, SIGPIPE
621 */
7591bab1 622static int set_signal_handler(void)
b8aa1682
JD
623{
624 int ret = 0;
625 struct sigaction sa;
626 sigset_t sigset;
627
628 if ((ret = sigemptyset(&sigset)) < 0) {
629 PERROR("sigemptyset");
630 return ret;
631 }
632
b8aa1682
JD
633 sa.sa_mask = sigset;
634 sa.sa_flags = 0;
0072e5e2
MD
635
636 sa.sa_handler = sighandler;
b8aa1682
JD
637 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
638 PERROR("sigaction");
639 return ret;
640 }
641
642 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
643 PERROR("sigaction");
644 return ret;
645 }
646
0072e5e2 647 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
b8aa1682
JD
648 PERROR("sigaction");
649 return ret;
650 }
651
0072e5e2
MD
652 sa.sa_handler = SIG_IGN;
653 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3fd27398
MD
654 PERROR("sigaction");
655 return ret;
656 }
657
658 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
b8aa1682
JD
659
660 return ret;
661}
662
3fd27398
MD
663void lttng_relay_notify_ready(void)
664{
665 /* Notify the parent of the fork() process that we are ready. */
666 if (opt_daemon || opt_background) {
667 if (uatomic_sub_return(&lttng_relay_ready, 1) == 0) {
668 kill(child_ppid, SIGUSR1);
669 }
670 }
671}
672
b8aa1682
JD
673/*
674 * Init thread quit pipe.
675 *
676 * Return -1 on error or 0 if all pipes are created.
677 */
7591bab1 678static int init_thread_quit_pipe(void)
b8aa1682 679{
a02de639 680 int ret;
b8aa1682 681
a02de639 682 ret = utils_create_pipe_cloexec(thread_quit_pipe);
b8aa1682 683
b8aa1682
JD
684 return ret;
685}
686
687/*
688 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
689 */
7591bab1 690static int create_thread_poll_set(struct lttng_poll_event *events, int size)
b8aa1682
JD
691{
692 int ret;
693
694 if (events == NULL || size == 0) {
695 ret = -1;
696 goto error;
697 }
698
699 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
700 if (ret < 0) {
701 goto error;
702 }
703
704 /* Add quit pipe */
c7759e6a 705 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
b8aa1682
JD
706 if (ret < 0) {
707 goto error;
708 }
709
710 return 0;
711
712error:
713 return ret;
714}
715
716/*
717 * Check if the thread quit pipe was triggered.
718 *
719 * Return 1 if it was triggered else 0;
720 */
7591bab1 721static int check_thread_quit_pipe(int fd, uint32_t events)
b8aa1682
JD
722{
723 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
724 return 1;
725 }
726
727 return 0;
728}
729
730/*
731 * Create and init socket from uri.
732 */
7591bab1 733static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri)
b8aa1682
JD
734{
735 int ret;
736 struct lttcomm_sock *sock = NULL;
737
738 sock = lttcomm_alloc_sock_from_uri(uri);
739 if (sock == NULL) {
740 ERR("Allocating socket");
741 goto error;
742 }
743
744 ret = lttcomm_create_sock(sock);
745 if (ret < 0) {
746 goto error;
747 }
748 DBG("Listening on sock %d", sock->fd);
749
750 ret = sock->ops->bind(sock);
751 if (ret < 0) {
752 goto error;
753 }
754
755 ret = sock->ops->listen(sock, -1);
756 if (ret < 0) {
757 goto error;
758
759 }
760
761 return sock;
762
763error:
764 if (sock) {
765 lttcomm_destroy_sock(sock);
766 }
767 return NULL;
768}
769
770/*
771 * This thread manages the listening for new connections on the network
772 */
7591bab1 773static void *relay_thread_listener(void *data)
b8aa1682 774{
095a4ae5 775 int i, ret, pollfd, err = -1;
b8aa1682
JD
776 uint32_t revents, nb_fd;
777 struct lttng_poll_event events;
778 struct lttcomm_sock *control_sock, *data_sock;
779
b8aa1682
JD
780 DBG("[thread] Relay listener started");
781
55706a7d
MD
782 health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER);
783
f385ae0a
MD
784 health_code_update();
785
7591bab1 786 control_sock = relay_socket_create(control_uri);
b8aa1682 787 if (!control_sock) {
095a4ae5 788 goto error_sock_control;
b8aa1682
JD
789 }
790
7591bab1 791 data_sock = relay_socket_create(data_uri);
b8aa1682 792 if (!data_sock) {
095a4ae5 793 goto error_sock_relay;
b8aa1682
JD
794 }
795
796 /*
7591bab1
MD
797 * Pass 3 as size here for the thread quit pipe, control and
798 * data socket.
b8aa1682
JD
799 */
800 ret = create_thread_poll_set(&events, 3);
801 if (ret < 0) {
802 goto error_create_poll;
803 }
804
805 /* Add the control socket */
806 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
807 if (ret < 0) {
808 goto error_poll_add;
809 }
810
811 /* Add the data socket */
812 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
813 if (ret < 0) {
814 goto error_poll_add;
815 }
816
3fd27398
MD
817 lttng_relay_notify_ready();
818
9b5e0863
MD
819 if (testpoint(relayd_thread_listener)) {
820 goto error_testpoint;
821 }
822
b8aa1682 823 while (1) {
f385ae0a
MD
824 health_code_update();
825
b8aa1682
JD
826 DBG("Listener accepting connections");
827
b8aa1682 828restart:
f385ae0a 829 health_poll_entry();
b8aa1682 830 ret = lttng_poll_wait(&events, -1);
f385ae0a 831 health_poll_exit();
b8aa1682
JD
832 if (ret < 0) {
833 /*
834 * Restart interrupted system call.
835 */
836 if (errno == EINTR) {
837 goto restart;
838 }
839 goto error;
840 }
841
0d9c5d77
DG
842 nb_fd = ret;
843
b8aa1682
JD
844 DBG("Relay new connection received");
845 for (i = 0; i < nb_fd; i++) {
f385ae0a
MD
846 health_code_update();
847
b8aa1682
JD
848 /* Fetch once the poll data */
849 revents = LTTNG_POLL_GETEV(&events, i);
850 pollfd = LTTNG_POLL_GETFD(&events, i);
851
fd20dac9 852 if (!revents) {
7591bab1
MD
853 /*
854 * No activity for this FD (poll
855 * implementation).
856 */
fd20dac9
MD
857 continue;
858 }
859
b8aa1682
JD
860 /* Thread quit pipe has been closed. Killing thread. */
861 ret = check_thread_quit_pipe(pollfd, revents);
862 if (ret) {
095a4ae5
MD
863 err = 0;
864 goto exit;
b8aa1682
JD
865 }
866
03e43155 867 if (revents & LPOLLIN) {
4b7f17b2 868 /*
7591bab1
MD
869 * A new connection is requested, therefore a
870 * sessiond/consumerd connection is allocated in
871 * this thread, enqueued to a global queue and
872 * dequeued (and freed) in the worker thread.
4b7f17b2 873 */
58eb9381
DG
874 int val = 1;
875 struct relay_connection *new_conn;
4b7f17b2 876 struct lttcomm_sock *newsock;
7591bab1 877 enum connection_type type;
b8aa1682
JD
878
879 if (pollfd == data_sock->fd) {
7591bab1 880 type = RELAY_DATA;
b8aa1682 881 newsock = data_sock->ops->accept(data_sock);
58eb9381
DG
882 DBG("Relay data connection accepted, socket %d",
883 newsock->fd);
4b7f17b2
MD
884 } else {
885 assert(pollfd == control_sock->fd);
7591bab1 886 type = RELAY_CONTROL;
b8aa1682 887 newsock = control_sock->ops->accept(control_sock);
58eb9381
DG
888 DBG("Relay control connection accepted, socket %d",
889 newsock->fd);
b8aa1682 890 }
58eb9381
DG
891 if (!newsock) {
892 PERROR("accepting sock");
58eb9381
DG
893 goto error;
894 }
895
896 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
897 sizeof(val));
b8aa1682
JD
898 if (ret < 0) {
899 PERROR("setsockopt inet");
4b7f17b2 900 lttcomm_destroy_sock(newsock);
b8aa1682
JD
901 goto error;
902 }
f056029c
JR
903
904 ret = socket_apply_keep_alive_config(newsock->fd);
905 if (ret < 0) {
906 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
907 newsock->fd);
908 lttcomm_destroy_sock(newsock);
909 goto error;
910 }
911
7591bab1
MD
912 new_conn = connection_create(newsock, type);
913 if (!new_conn) {
914 lttcomm_destroy_sock(newsock);
915 goto error;
916 }
58eb9381
DG
917
918 /* Enqueue request for the dispatcher thread. */
8bdee6e2
SM
919 cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail,
920 &new_conn->qnode);
b8aa1682
JD
921
922 /*
7591bab1
MD
923 * Wake the dispatch queue futex.
924 * Implicit memory barrier with the
925 * exchange in cds_wfcq_enqueue.
b8aa1682 926 */
58eb9381 927 futex_nto1_wake(&relay_conn_queue.futex);
03e43155
MD
928 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
929 ERR("socket poll error");
930 goto error;
931 } else {
932 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
933 goto error;
b8aa1682
JD
934 }
935 }
936 }
937
095a4ae5 938exit:
b8aa1682
JD
939error:
940error_poll_add:
9b5e0863 941error_testpoint:
b8aa1682
JD
942 lttng_poll_clean(&events);
943error_create_poll:
095a4ae5
MD
944 if (data_sock->fd >= 0) {
945 ret = data_sock->ops->close(data_sock);
b8aa1682
JD
946 if (ret) {
947 PERROR("close");
948 }
b8aa1682 949 }
095a4ae5
MD
950 lttcomm_destroy_sock(data_sock);
951error_sock_relay:
952 if (control_sock->fd >= 0) {
953 ret = control_sock->ops->close(control_sock);
b8aa1682
JD
954 if (ret) {
955 PERROR("close");
956 }
b8aa1682 957 }
095a4ae5
MD
958 lttcomm_destroy_sock(control_sock);
959error_sock_control:
960 if (err) {
f385ae0a
MD
961 health_error();
962 ERR("Health error occurred in %s", __func__);
095a4ae5 963 }
55706a7d 964 health_unregister(health_relayd);
b8aa1682 965 DBG("Relay listener thread cleanup complete");
b4aacfdc 966 lttng_relay_stop_threads();
b8aa1682
JD
967 return NULL;
968}
969
970/*
971 * This thread manages the dispatching of the requests to worker threads
972 */
7591bab1 973static void *relay_thread_dispatcher(void *data)
b8aa1682 974{
6cd525e8
MD
975 int err = -1;
976 ssize_t ret;
8bdee6e2 977 struct cds_wfcq_node *node;
58eb9381 978 struct relay_connection *new_conn = NULL;
b8aa1682
JD
979
980 DBG("[thread] Relay dispatcher started");
981
55706a7d
MD
982 health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER);
983
9b5e0863
MD
984 if (testpoint(relayd_thread_dispatcher)) {
985 goto error_testpoint;
986 }
987
f385ae0a
MD
988 health_code_update();
989
0ed3b1a8 990 for (;;) {
f385ae0a
MD
991 health_code_update();
992
b8aa1682 993 /* Atomically prepare the queue futex */
58eb9381 994 futex_nto1_prepare(&relay_conn_queue.futex);
b8aa1682 995
0ed3b1a8
MD
996 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
997 break;
998 }
999
b8aa1682 1000 do {
f385ae0a
MD
1001 health_code_update();
1002
b8aa1682 1003 /* Dequeue commands */
8bdee6e2
SM
1004 node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head,
1005 &relay_conn_queue.tail);
b8aa1682
JD
1006 if (node == NULL) {
1007 DBG("Woken up but nothing in the relay command queue");
1008 /* Continue thread execution */
1009 break;
1010 }
58eb9381 1011 new_conn = caa_container_of(node, struct relay_connection, qnode);
b8aa1682 1012
58eb9381 1013 DBG("Dispatching request waiting on sock %d", new_conn->sock->fd);
b8aa1682
JD
1014
1015 /*
7591bab1
MD
1016 * Inform worker thread of the new request. This
1017 * call is blocking so we can be assured that
1018 * the data will be read at some point in time
1019 * or wait to the end of the world :)
b8aa1682 1020 */
58eb9381
DG
1021 ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn));
1022 if (ret < 0) {
1023 PERROR("write connection pipe");
7591bab1 1024 connection_put(new_conn);
b8aa1682
JD
1025 goto error;
1026 }
1027 } while (node != NULL);
1028
1029 /* Futex wait on queue. Blocking call on futex() */
f385ae0a 1030 health_poll_entry();
58eb9381 1031 futex_nto1_wait(&relay_conn_queue.futex);
f385ae0a 1032 health_poll_exit();
b8aa1682
JD
1033 }
1034
f385ae0a
MD
1035 /* Normal exit, no error */
1036 err = 0;
1037
b8aa1682 1038error:
9b5e0863 1039error_testpoint:
f385ae0a
MD
1040 if (err) {
1041 health_error();
1042 ERR("Health error occurred in %s", __func__);
1043 }
55706a7d 1044 health_unregister(health_relayd);
b8aa1682 1045 DBG("Dispatch thread dying");
b4aacfdc 1046 lttng_relay_stop_threads();
b8aa1682
JD
1047 return NULL;
1048}
1049
b8aa1682 1050/*
7591bab1 1051 * Set index data from the control port to a given index object.
b8aa1682 1052 */
7591bab1 1053static int set_index_control_data(struct relay_index *index,
234cd636
JD
1054 struct lttcomm_relayd_index *data,
1055 struct relay_connection *conn)
1c20f0e2 1056{
7591bab1 1057 struct ctf_packet_index index_data;
1c20f0e2
JD
1058
1059 /*
7591bab1
MD
1060 * The index on disk is encoded in big endian, so we don't need
1061 * to convert the data received on the network. The data_offset
1062 * value is NEVER modified here and is updated by the data
1063 * thread.
1c20f0e2 1064 */
7591bab1
MD
1065 index_data.packet_size = data->packet_size;
1066 index_data.content_size = data->content_size;
1067 index_data.timestamp_begin = data->timestamp_begin;
1068 index_data.timestamp_end = data->timestamp_end;
1069 index_data.events_discarded = data->events_discarded;
1070 index_data.stream_id = data->stream_id;
234cd636
JD
1071
1072 if (conn->minor >= 8) {
1073 index->index_data.stream_instance_id = data->stream_instance_id;
1074 index->index_data.packet_seq_num = data->packet_seq_num;
1075 }
1076
7591bab1 1077 return relay_index_set_data(index, &index_data);
1c20f0e2
JD
1078}
1079
c5b6f4f0
DG
1080/*
1081 * Handle the RELAYD_CREATE_SESSION command.
1082 *
1083 * On success, send back the session id or else return a negative value.
1084 */
7591bab1 1085static int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1086 struct relay_connection *conn)
c5b6f4f0
DG
1087{
1088 int ret = 0, send_ret;
1089 struct relay_session *session;
1090 struct lttcomm_relayd_status_session reply;
36d2e35d 1091 char session_name[LTTNG_NAME_MAX];
9cf3eb20 1092 char hostname[LTTNG_HOST_NAME_MAX];
7591bab1
MD
1093 uint32_t live_timer = 0;
1094 bool snapshot = false;
c5b6f4f0 1095
36d2e35d 1096 memset(session_name, 0, LTTNG_NAME_MAX);
9cf3eb20 1097 memset(hostname, 0, LTTNG_HOST_NAME_MAX);
c5b6f4f0
DG
1098
1099 memset(&reply, 0, sizeof(reply));
1100
58eb9381 1101 switch (conn->minor) {
2a174661
DG
1102 case 1:
1103 case 2:
1104 case 3:
1105 break;
1106 case 4: /* LTTng sessiond 2.4 */
1107 default:
7591bab1
MD
1108 ret = cmd_create_session_2_4(conn, session_name,
1109 hostname, &live_timer, &snapshot);
1110 }
1111 if (ret < 0) {
1112 goto send_reply;
d3e2ba59
JD
1113 }
1114
7591bab1
MD
1115 session = session_create(session_name, hostname, live_timer,
1116 snapshot, conn->major, conn->minor);
1117 if (!session) {
1118 ret = -1;
1119 goto send_reply;
1120 }
1121 assert(!conn->session);
1122 conn->session = session;
c5b6f4f0
DG
1123 DBG("Created session %" PRIu64, session->id);
1124
7591bab1
MD
1125 reply.session_id = htobe64(session->id);
1126
1127send_reply:
c5b6f4f0
DG
1128 if (ret < 0) {
1129 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
1130 } else {
1131 reply.ret_code = htobe32(LTTNG_OK);
1132 }
1133
58eb9381 1134 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
c5b6f4f0
DG
1135 if (send_ret < 0) {
1136 ERR("Relayd sending session id");
4169f5ad 1137 ret = send_ret;
c5b6f4f0
DG
1138 }
1139
1140 return ret;
1141}
1142
a4baae1b
JD
1143/*
1144 * When we have received all the streams and the metadata for a channel,
1145 * we make them visible to the viewer threads.
1146 */
7591bab1 1147static void publish_connection_local_streams(struct relay_connection *conn)
a4baae1b 1148{
7591bab1
MD
1149 struct relay_stream *stream;
1150 struct relay_session *session = conn->session;
a4baae1b 1151
7591bab1
MD
1152 /*
1153 * We publish all streams belonging to a session atomically wrt
1154 * session lock.
1155 */
1156 pthread_mutex_lock(&session->lock);
1157 rcu_read_lock();
1158 cds_list_for_each_entry_rcu(stream, &session->recv_list,
1159 recv_node) {
1160 stream_publish(stream);
a4baae1b 1161 }
7591bab1 1162 rcu_read_unlock();
a4baae1b 1163
7591bab1
MD
1164 /*
1165 * Inform the viewer that there are new streams in the session.
1166 */
1167 if (session->viewer_attached) {
1168 uatomic_set(&session->new_streams, 1);
1169 }
1170 pthread_mutex_unlock(&session->lock);
a4baae1b
JD
1171}
1172
b8aa1682
JD
1173/*
1174 * relay_add_stream: allocate a new stream for a session
1175 */
7591bab1 1176static int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1177 struct relay_connection *conn)
b8aa1682 1178{
7591bab1
MD
1179 int ret;
1180 ssize_t send_ret;
58eb9381 1181 struct relay_session *session = conn->session;
b8aa1682
JD
1182 struct relay_stream *stream = NULL;
1183 struct lttcomm_relayd_status_stream reply;
4030a636 1184 struct ctf_trace *trace = NULL;
7591bab1
MD
1185 uint64_t stream_handle = -1ULL;
1186 char *path_name = NULL, *channel_name = NULL;
1187 uint64_t tracefile_size = 0, tracefile_count = 0;
b8aa1682 1188
58eb9381 1189 if (!session || conn->version_check_done == 0) {
b8aa1682
JD
1190 ERR("Trying to add a stream before version check");
1191 ret = -1;
1192 goto end_no_session;
1193 }
1194
7591bab1
MD
1195 switch (session->minor) {
1196 case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */
1197 ret = cmd_recv_stream_2_1(conn, &path_name,
1198 &channel_name);
0f907de1 1199 break;
7591bab1 1200 case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
0f907de1 1201 default:
7591bab1
MD
1202 ret = cmd_recv_stream_2_2(conn, &path_name,
1203 &channel_name, &tracefile_size, &tracefile_count);
0f907de1
JD
1204 break;
1205 }
1206 if (ret < 0) {
7591bab1 1207 goto send_reply;
b8aa1682
JD
1208 }
1209
7591bab1 1210 trace = ctf_trace_get_by_path_or_create(session, path_name);
2a174661 1211 if (!trace) {
7591bab1 1212 goto send_reply;
2a174661 1213 }
7591bab1 1214 /* This stream here has one reference on the trace. */
2a174661 1215
7591bab1
MD
1216 pthread_mutex_lock(&last_relay_stream_id_lock);
1217 stream_handle = ++last_relay_stream_id;
1218 pthread_mutex_unlock(&last_relay_stream_id_lock);
d3e2ba59 1219
7591bab1
MD
1220 /* We pass ownership of path_name and channel_name. */
1221 stream = stream_create(trace, stream_handle, path_name,
1222 channel_name, tracefile_size, tracefile_count);
1223 path_name = NULL;
1224 channel_name = NULL;
a4baae1b 1225
2a174661 1226 /*
7591bab1
MD
1227 * Streams are the owners of their trace. Reference to trace is
1228 * kept within stream_create().
2a174661 1229 */
7591bab1 1230 ctf_trace_put(trace);
d3e2ba59 1231
7591bab1 1232send_reply:
53efb85a 1233 memset(&reply, 0, sizeof(reply));
7591bab1
MD
1234 reply.handle = htobe64(stream_handle);
1235 if (!stream) {
f73fabfd 1236 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682 1237 } else {
f73fabfd 1238 reply.ret_code = htobe32(LTTNG_OK);
b8aa1682 1239 }
5af40280 1240
58eb9381 1241 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
b8aa1682
JD
1242 sizeof(struct lttcomm_relayd_status_stream), 0);
1243 if (send_ret < 0) {
1244 ERR("Relay sending stream id");
7591bab1 1245 ret = (int) send_ret;
b8aa1682
JD
1246 }
1247
1248end_no_session:
7591bab1
MD
1249 free(path_name);
1250 free(channel_name);
0f907de1 1251 return ret;
b8aa1682
JD
1252}
1253
173af62f
DG
1254/*
1255 * relay_close_stream: close a specific stream
1256 */
7591bab1 1257static int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1258 struct relay_connection *conn)
173af62f 1259{
94d49140 1260 int ret, send_ret;
58eb9381 1261 struct relay_session *session = conn->session;
173af62f
DG
1262 struct lttcomm_relayd_close_stream stream_info;
1263 struct lttcomm_relayd_generic_reply reply;
1264 struct relay_stream *stream;
173af62f
DG
1265
1266 DBG("Close stream received");
1267
58eb9381 1268 if (!session || conn->version_check_done == 0) {
173af62f
DG
1269 ERR("Trying to close a stream before version check");
1270 ret = -1;
1271 goto end_no_session;
1272 }
1273
58eb9381 1274 ret = conn->sock->ops->recvmsg(conn->sock, &stream_info,
7c5aef62 1275 sizeof(struct lttcomm_relayd_close_stream), 0);
173af62f 1276 if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
a6cd2b97
DG
1277 if (ret == 0) {
1278 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1279 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97
DG
1280 } else {
1281 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
1282 }
173af62f
DG
1283 ret = -1;
1284 goto end_no_session;
1285 }
1286
7591bab1 1287 stream = stream_get_by_id(be64toh(stream_info.stream_id));
173af62f
DG
1288 if (!stream) {
1289 ret = -1;
7591bab1 1290 goto end;
173af62f 1291 }
77f7bd85
MD
1292
1293 /*
1294 * Set last_net_seq_num before the close flag. Required by data
1295 * pending check.
1296 */
7591bab1 1297 pthread_mutex_lock(&stream->lock);
8e2583a4 1298 stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num);
77f7bd85
MD
1299 pthread_mutex_unlock(&stream->lock);
1300
bda7c7b9
JG
1301 /*
1302 * This is one of the conditions which may trigger a stream close
1303 * with the others being:
1304 * 1) A close command is received for a stream
1305 * 2) The control connection owning the stream is closed
1306 * 3) We have received all of the stream's data _after_ a close
1307 * request.
1308 */
1309 try_stream_close(stream);
7591bab1
MD
1310 if (stream->is_metadata) {
1311 struct relay_viewer_stream *vstream;
173af62f 1312
7591bab1
MD
1313 vstream = viewer_stream_get_by_id(stream->stream_handle);
1314 if (vstream) {
1315 if (vstream->metadata_sent == stream->metadata_received) {
1316 /*
1317 * Since all the metadata has been sent to the
1318 * viewer and that we have a request to close
1319 * its stream, we can safely teardown the
1320 * corresponding metadata viewer stream.
1321 */
1322 viewer_stream_put(vstream);
1323 }
1324 /* Put local reference. */
1325 viewer_stream_put(vstream);
1326 }
1327 }
7591bab1 1328 stream_put(stream);
173af62f 1329
7591bab1 1330end:
53efb85a 1331 memset(&reply, 0, sizeof(reply));
173af62f 1332 if (ret < 0) {
f73fabfd 1333 reply.ret_code = htobe32(LTTNG_ERR_UNK);
173af62f 1334 } else {
f73fabfd 1335 reply.ret_code = htobe32(LTTNG_OK);
173af62f 1336 }
58eb9381 1337 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
173af62f
DG
1338 sizeof(struct lttcomm_relayd_generic_reply), 0);
1339 if (send_ret < 0) {
1340 ERR("Relay sending stream id");
4169f5ad 1341 ret = send_ret;
173af62f
DG
1342 }
1343
1344end_no_session:
1345 return ret;
1346}
1347
93ec662e
JD
1348/*
1349 * relay_reset_metadata: reset a metadata stream
1350 */
1351static
1352int relay_reset_metadata(struct lttcomm_relayd_hdr *recv_hdr,
1353 struct relay_connection *conn)
1354{
1355 int ret, send_ret;
1356 struct relay_session *session = conn->session;
1357 struct lttcomm_relayd_reset_metadata stream_info;
1358 struct lttcomm_relayd_generic_reply reply;
1359 struct relay_stream *stream;
1360
1361 DBG("Reset metadata received");
1362
1363 if (!session || conn->version_check_done == 0) {
1364 ERR("Trying to reset a metadata stream before version check");
1365 ret = -1;
1366 goto end_no_session;
1367 }
1368
1369 ret = conn->sock->ops->recvmsg(conn->sock, &stream_info,
1370 sizeof(struct lttcomm_relayd_reset_metadata), 0);
1371 if (ret < sizeof(struct lttcomm_relayd_reset_metadata)) {
1372 if (ret == 0) {
1373 /* Orderly shutdown. Not necessary to print an error. */
1374 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
1375 } else {
1376 ERR("Relay didn't receive valid reset_metadata struct "
1377 "size : %d", ret);
1378 }
1379 ret = -1;
1380 goto end_no_session;
1381 }
1382 DBG("Update metadata to version %" PRIu64, be64toh(stream_info.version));
1383
1384 /* Unsupported for live sessions for now. */
1385 if (session->live_timer != 0) {
1386 ret = -1;
1387 goto end;
1388 }
1389
1390 stream = stream_get_by_id(be64toh(stream_info.stream_id));
1391 if (!stream) {
1392 ret = -1;
1393 goto end;
1394 }
1395 pthread_mutex_lock(&stream->lock);
1396 if (!stream->is_metadata) {
1397 ret = -1;
1398 goto end_unlock;
1399 }
1400
1401 ret = utils_rotate_stream_file(stream->path_name, stream->channel_name,
1402 0, 0, -1, -1, stream->stream_fd->fd, NULL,
1403 &stream->stream_fd->fd);
1404 if (ret < 0) {
1405 ERR("Failed to rotate metadata file %s of channel %s",
1406 stream->path_name, stream->channel_name);
1407 goto end_unlock;
1408 }
1409
1410end_unlock:
1411 pthread_mutex_unlock(&stream->lock);
1412 stream_put(stream);
1413
1414end:
1415 memset(&reply, 0, sizeof(reply));
1416 if (ret < 0) {
1417 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1418 } else {
1419 reply.ret_code = htobe32(LTTNG_OK);
1420 }
1421 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1422 sizeof(struct lttcomm_relayd_generic_reply), 0);
1423 if (send_ret < 0) {
1424 ERR("Relay sending reset metadata reply");
1425 ret = send_ret;
1426 }
1427
1428end_no_session:
1429 return ret;
1430}
1431
b8aa1682
JD
1432/*
1433 * relay_unknown_command: send -1 if received unknown command
1434 */
7591bab1 1435static void relay_unknown_command(struct relay_connection *conn)
b8aa1682
JD
1436{
1437 struct lttcomm_relayd_generic_reply reply;
1438 int ret;
1439
53efb85a 1440 memset(&reply, 0, sizeof(reply));
f73fabfd 1441 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 1442 ret = conn->sock->ops->sendmsg(conn->sock, &reply,
b8aa1682
JD
1443 sizeof(struct lttcomm_relayd_generic_reply), 0);
1444 if (ret < 0) {
1445 ERR("Relay sending unknown command");
1446 }
1447}
1448
1449/*
1450 * relay_start: send an acknowledgment to the client to tell if we are
1451 * ready to receive data. We are ready if a session is established.
1452 */
7591bab1 1453static int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1454 struct relay_connection *conn)
b8aa1682 1455{
f73fabfd 1456 int ret = htobe32(LTTNG_OK);
b8aa1682 1457 struct lttcomm_relayd_generic_reply reply;
58eb9381 1458 struct relay_session *session = conn->session;
b8aa1682
JD
1459
1460 if (!session) {
1461 DBG("Trying to start the streaming without a session established");
f73fabfd 1462 ret = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1463 }
1464
53efb85a 1465 memset(&reply, 0, sizeof(reply));
b8aa1682 1466 reply.ret_code = ret;
58eb9381 1467 ret = conn->sock->ops->sendmsg(conn->sock, &reply,
b8aa1682
JD
1468 sizeof(struct lttcomm_relayd_generic_reply), 0);
1469 if (ret < 0) {
1470 ERR("Relay sending start ack");
1471 }
1472
1473 return ret;
1474}
1475
1d4dfdef
DG
1476/*
1477 * Append padding to the file pointed by the file descriptor fd.
1478 */
1479static int write_padding_to_file(int fd, uint32_t size)
1480{
6cd525e8 1481 ssize_t ret = 0;
1d4dfdef
DG
1482 char *zeros;
1483
1484 if (size == 0) {
1485 goto end;
1486 }
1487
1488 zeros = zmalloc(size);
1489 if (zeros == NULL) {
1490 PERROR("zmalloc zeros for padding");
1491 ret = -1;
1492 goto end;
1493 }
1494
6cd525e8
MD
1495 ret = lttng_write(fd, zeros, size);
1496 if (ret < size) {
1d4dfdef
DG
1497 PERROR("write padding to file");
1498 }
1499
e986c7a1
DG
1500 free(zeros);
1501
1d4dfdef
DG
1502end:
1503 return ret;
1504}
1505
b8aa1682 1506/*
7591bab1 1507 * relay_recv_metadata: receive the metadata for the session.
b8aa1682 1508 */
7591bab1 1509static int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1510 struct relay_connection *conn)
b8aa1682 1511{
32d1569c 1512 int ret = 0;
6cd525e8 1513 ssize_t size_ret;
58eb9381 1514 struct relay_session *session = conn->session;
b8aa1682
JD
1515 struct lttcomm_relayd_metadata_payload *metadata_struct;
1516 struct relay_stream *metadata_stream;
1517 uint64_t data_size, payload_size;
1518
1519 if (!session) {
1520 ERR("Metadata sent before version check");
1521 ret = -1;
1522 goto end;
1523 }
1524
f6416125
MD
1525 data_size = payload_size = be64toh(recv_hdr->data_size);
1526 if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1527 ERR("Incorrect data size");
1528 ret = -1;
1529 goto end;
1530 }
1531 payload_size -= sizeof(struct lttcomm_relayd_metadata_payload);
1532
b8aa1682 1533 if (data_buffer_size < data_size) {
d7b3776f 1534 /* In case the realloc fails, we can free the memory */
c617c0c6
MD
1535 char *tmp_data_ptr;
1536
1537 tmp_data_ptr = realloc(data_buffer, data_size);
1538 if (!tmp_data_ptr) {
b8aa1682 1539 ERR("Allocating data buffer");
c617c0c6 1540 free(data_buffer);
b8aa1682
JD
1541 ret = -1;
1542 goto end;
1543 }
c617c0c6 1544 data_buffer = tmp_data_ptr;
b8aa1682
JD
1545 data_buffer_size = data_size;
1546 }
1547 memset(data_buffer, 0, data_size);
77c7c900 1548 DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
32d1569c
JG
1549 size_ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, data_size, 0);
1550 if (size_ret < 0 || size_ret != data_size) {
1551 if (size_ret == 0) {
a6cd2b97 1552 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1553 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97
DG
1554 } else {
1555 ERR("Relay didn't receive the whole metadata");
1556 }
b8aa1682 1557 ret = -1;
b8aa1682
JD
1558 goto end;
1559 }
1560 metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
9d1bbf21 1561
7591bab1 1562 metadata_stream = stream_get_by_id(be64toh(metadata_struct->stream_id));
b8aa1682
JD
1563 if (!metadata_stream) {
1564 ret = -1;
7591bab1 1565 goto end;
b8aa1682
JD
1566 }
1567
7591bab1
MD
1568 pthread_mutex_lock(&metadata_stream->lock);
1569
1570 size_ret = lttng_write(metadata_stream->stream_fd->fd, metadata_struct->payload,
6cd525e8
MD
1571 payload_size);
1572 if (size_ret < payload_size) {
b8aa1682
JD
1573 ERR("Relay error writing metadata on file");
1574 ret = -1;
7591bab1 1575 goto end_put;
b8aa1682 1576 }
1d4dfdef 1577
32d1569c 1578 size_ret = write_padding_to_file(metadata_stream->stream_fd->fd,
1d4dfdef 1579 be32toh(metadata_struct->padding_size));
32d1569c 1580 if (size_ret < 0) {
7591bab1 1581 goto end_put;
1d4dfdef 1582 }
2a174661 1583
7591bab1 1584 metadata_stream->metadata_received +=
d3e2ba59 1585 payload_size + be32toh(metadata_struct->padding_size);
7591bab1
MD
1586 DBG2("Relay metadata written. Updated metadata_received %" PRIu64,
1587 metadata_stream->metadata_received);
1d4dfdef 1588
7591bab1
MD
1589end_put:
1590 pthread_mutex_unlock(&metadata_stream->lock);
1591 stream_put(metadata_stream);
b8aa1682
JD
1592end:
1593 return ret;
1594}
1595
1596/*
1597 * relay_send_version: send relayd version number
1598 */
7591bab1 1599static int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1600 struct relay_connection *conn)
b8aa1682 1601{
7f51dcba 1602 int ret;
092b6259 1603 struct lttcomm_relayd_version reply, msg;
87cb6359 1604 bool compatible = true;
b8aa1682 1605
58eb9381 1606 conn->version_check_done = 1;
b8aa1682 1607
092b6259 1608 /* Get version from the other side. */
58eb9381 1609 ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0);
092b6259 1610 if (ret < 0 || ret != sizeof(msg)) {
a6cd2b97
DG
1611 if (ret == 0) {
1612 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1613 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97
DG
1614 } else {
1615 ERR("Relay failed to receive the version values.");
1616 }
092b6259 1617 ret = -1;
092b6259
DG
1618 goto end;
1619 }
1620
53efb85a 1621 memset(&reply, 0, sizeof(reply));
d83a952c
MD
1622 reply.major = RELAYD_VERSION_COMM_MAJOR;
1623 reply.minor = RELAYD_VERSION_COMM_MINOR;
d4519fa3
JD
1624
1625 /* Major versions must be the same */
1626 if (reply.major != be32toh(msg.major)) {
6151a90f
JD
1627 DBG("Incompatible major versions (%u vs %u), deleting session",
1628 reply.major, be32toh(msg.major));
87cb6359 1629 compatible = false;
d4519fa3
JD
1630 }
1631
58eb9381 1632 conn->major = reply.major;
0f907de1
JD
1633 /* We adapt to the lowest compatible version */
1634 if (reply.minor <= be32toh(msg.minor)) {
58eb9381 1635 conn->minor = reply.minor;
0f907de1 1636 } else {
58eb9381 1637 conn->minor = be32toh(msg.minor);
0f907de1
JD
1638 }
1639
6151a90f
JD
1640 reply.major = htobe32(reply.major);
1641 reply.minor = htobe32(reply.minor);
58eb9381 1642 ret = conn->sock->ops->sendmsg(conn->sock, &reply,
6151a90f
JD
1643 sizeof(struct lttcomm_relayd_version), 0);
1644 if (ret < 0) {
1645 ERR("Relay sending version");
1646 }
1647
87cb6359
JD
1648 if (!compatible) {
1649 ret = -1;
1650 goto end;
1651 }
1652
58eb9381
DG
1653 DBG("Version check done using protocol %u.%u", conn->major,
1654 conn->minor);
b8aa1682
JD
1655
1656end:
1657 return ret;
1658}
1659
c8f59ee5 1660/*
6d805429 1661 * Check for data pending for a given stream id from the session daemon.
c8f59ee5 1662 */
7591bab1 1663static int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1664 struct relay_connection *conn)
c8f59ee5 1665{
58eb9381 1666 struct relay_session *session = conn->session;
6d805429 1667 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
1668 struct lttcomm_relayd_generic_reply reply;
1669 struct relay_stream *stream;
1670 int ret;
c8f59ee5
DG
1671 uint64_t last_net_seq_num, stream_id;
1672
6d805429 1673 DBG("Data pending command received");
c8f59ee5 1674
58eb9381 1675 if (!session || conn->version_check_done == 0) {
c8f59ee5
DG
1676 ERR("Trying to check for data before version check");
1677 ret = -1;
1678 goto end_no_session;
1679 }
1680
58eb9381 1681 ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0);
c8f59ee5 1682 if (ret < sizeof(msg)) {
a6cd2b97
DG
1683 if (ret == 0) {
1684 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1685 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97
DG
1686 } else {
1687 ERR("Relay didn't receive valid data_pending struct size : %d",
1688 ret);
1689 }
c8f59ee5
DG
1690 ret = -1;
1691 goto end_no_session;
1692 }
1693
1694 stream_id = be64toh(msg.stream_id);
1695 last_net_seq_num = be64toh(msg.last_net_seq_num);
1696
7591bab1 1697 stream = stream_get_by_id(stream_id);
de91f48a 1698 if (stream == NULL) {
c8f59ee5 1699 ret = -1;
7591bab1 1700 goto end;
c8f59ee5
DG
1701 }
1702
7591bab1
MD
1703 pthread_mutex_lock(&stream->lock);
1704
6d805429 1705 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
c8f59ee5
DG
1706 " and last_seq %" PRIu64, stream_id, stream->prev_seq,
1707 last_net_seq_num);
1708
33832e64 1709 /* Avoid wrapping issue */
39df6d9f 1710 if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) {
6d805429 1711 /* Data has in fact been written and is NOT pending */
c8f59ee5 1712 ret = 0;
6d805429
DG
1713 } else {
1714 /* Data still being streamed thus pending */
1715 ret = 1;
c8f59ee5
DG
1716 }
1717
7591bab1
MD
1718 stream->data_pending_check_done = true;
1719 pthread_mutex_unlock(&stream->lock);
f7079f67 1720
7591bab1
MD
1721 stream_put(stream);
1722end:
c8f59ee5 1723
53efb85a 1724 memset(&reply, 0, sizeof(reply));
c8f59ee5 1725 reply.ret_code = htobe32(ret);
58eb9381 1726 ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
c8f59ee5 1727 if (ret < 0) {
6d805429 1728 ERR("Relay data pending ret code failed");
c8f59ee5
DG
1729 }
1730
1731end_no_session:
1732 return ret;
1733}
1734
1735/*
1736 * Wait for the control socket to reach a quiescent state.
1737 *
7591bab1
MD
1738 * Note that for now, when receiving this command from the session
1739 * daemon, this means that every subsequent commands or data received on
1740 * the control socket has been handled. So, this is why we simply return
1741 * OK here.
c8f59ee5 1742 */
7591bab1 1743static int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1744 struct relay_connection *conn)
c8f59ee5
DG
1745{
1746 int ret;
ad7051c0
DG
1747 uint64_t stream_id;
1748 struct relay_stream *stream;
ad7051c0 1749 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
1750 struct lttcomm_relayd_generic_reply reply;
1751
1752 DBG("Checking quiescent state on control socket");
1753
58eb9381 1754 if (!conn->session || conn->version_check_done == 0) {
ad7051c0
DG
1755 ERR("Trying to check for data before version check");
1756 ret = -1;
1757 goto end_no_session;
1758 }
1759
58eb9381 1760 ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0);
ad7051c0 1761 if (ret < sizeof(msg)) {
a6cd2b97
DG
1762 if (ret == 0) {
1763 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1764 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97
DG
1765 } else {
1766 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1767 ret);
1768 }
ad7051c0
DG
1769 ret = -1;
1770 goto end_no_session;
1771 }
1772
1773 stream_id = be64toh(msg.stream_id);
7591bab1
MD
1774 stream = stream_get_by_id(stream_id);
1775 if (!stream) {
1776 goto reply;
1777 }
1778 pthread_mutex_lock(&stream->lock);
1779 stream->data_pending_check_done = true;
1780 pthread_mutex_unlock(&stream->lock);
1781 DBG("Relay quiescent control pending flag set to %" PRIu64, stream_id);
1782 stream_put(stream);
1783reply:
53efb85a 1784 memset(&reply, 0, sizeof(reply));
c8f59ee5 1785 reply.ret_code = htobe32(LTTNG_OK);
58eb9381 1786 ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
c8f59ee5 1787 if (ret < 0) {
6d805429 1788 ERR("Relay data quiescent control ret code failed");
c8f59ee5
DG
1789 }
1790
ad7051c0 1791end_no_session:
c8f59ee5
DG
1792 return ret;
1793}
1794
f7079f67 1795/*
7591bab1
MD
1796 * Initialize a data pending command. This means that a consumer is about
1797 * to ask for data pending for each stream it holds. Simply iterate over
1798 * all streams of a session and set the data_pending_check_done flag.
f7079f67
DG
1799 *
1800 * This command returns to the client a LTTNG_OK code.
1801 */
7591bab1 1802static int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1803 struct relay_connection *conn)
f7079f67
DG
1804{
1805 int ret;
1806 struct lttng_ht_iter iter;
1807 struct lttcomm_relayd_begin_data_pending msg;
1808 struct lttcomm_relayd_generic_reply reply;
1809 struct relay_stream *stream;
1810 uint64_t session_id;
1811
1812 assert(recv_hdr);
58eb9381 1813 assert(conn);
f7079f67
DG
1814
1815 DBG("Init streams for data pending");
1816
58eb9381 1817 if (!conn->session || conn->version_check_done == 0) {
f7079f67
DG
1818 ERR("Trying to check for data before version check");
1819 ret = -1;
1820 goto end_no_session;
1821 }
1822
58eb9381 1823 ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0);
f7079f67 1824 if (ret < sizeof(msg)) {
a6cd2b97
DG
1825 if (ret == 0) {
1826 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1827 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97
DG
1828 } else {
1829 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1830 ret);
1831 }
f7079f67
DG
1832 ret = -1;
1833 goto end_no_session;
1834 }
1835
1836 session_id = be64toh(msg.session_id);
1837
1838 /*
7591bab1
MD
1839 * Iterate over all streams to set the begin data pending flag.
1840 * For now, the streams are indexed by stream handle so we have
1841 * to iterate over all streams to find the one associated with
1842 * the right session_id.
f7079f67
DG
1843 */
1844 rcu_read_lock();
d3e2ba59 1845 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
2a174661 1846 node.node) {
7591bab1
MD
1847 if (!stream_get(stream)) {
1848 continue;
1849 }
1850 if (stream->trace->session->id == session_id) {
1851 pthread_mutex_lock(&stream->lock);
1852 stream->data_pending_check_done = false;
1853 pthread_mutex_unlock(&stream->lock);
f7079f67
DG
1854 DBG("Set begin data pending flag to stream %" PRIu64,
1855 stream->stream_handle);
1856 }
7591bab1 1857 stream_put(stream);
f7079f67
DG
1858 }
1859 rcu_read_unlock();
1860
53efb85a 1861 memset(&reply, 0, sizeof(reply));
f7079f67
DG
1862 /* All good, send back reply. */
1863 reply.ret_code = htobe32(LTTNG_OK);
1864
58eb9381 1865 ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
f7079f67
DG
1866 if (ret < 0) {
1867 ERR("Relay begin data pending send reply failed");
1868 }
1869
1870end_no_session:
1871 return ret;
1872}
1873
1874/*
7591bab1
MD
1875 * End data pending command. This will check, for a given session id, if
1876 * each stream associated with it has its data_pending_check_done flag
1877 * set. If not, this means that the client lost track of the stream but
1878 * the data is still being streamed on our side. In this case, we inform
1879 * the client that data is in flight.
f7079f67
DG
1880 *
1881 * Return to the client if there is data in flight or not with a ret_code.
1882 */
7591bab1 1883static int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1884 struct relay_connection *conn)
f7079f67
DG
1885{
1886 int ret;
1887 struct lttng_ht_iter iter;
1888 struct lttcomm_relayd_end_data_pending msg;
1889 struct lttcomm_relayd_generic_reply reply;
1890 struct relay_stream *stream;
1891 uint64_t session_id;
1892 uint32_t is_data_inflight = 0;
1893
f7079f67
DG
1894 DBG("End data pending command");
1895
58eb9381 1896 if (!conn->session || conn->version_check_done == 0) {
f7079f67
DG
1897 ERR("Trying to check for data before version check");
1898 ret = -1;
1899 goto end_no_session;
1900 }
1901
58eb9381 1902 ret = conn->sock->ops->recvmsg(conn->sock, &msg, sizeof(msg), 0);
f7079f67 1903 if (ret < sizeof(msg)) {
a6cd2b97
DG
1904 if (ret == 0) {
1905 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1906 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97
DG
1907 } else {
1908 ERR("Relay didn't receive valid end data_pending struct size: %d",
1909 ret);
1910 }
f7079f67
DG
1911 ret = -1;
1912 goto end_no_session;
1913 }
1914
1915 session_id = be64toh(msg.session_id);
1916
7591bab1
MD
1917 /*
1918 * Iterate over all streams to see if the begin data pending
1919 * flag is set.
1920 */
f7079f67 1921 rcu_read_lock();
d3e2ba59 1922 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
2a174661 1923 node.node) {
7591bab1
MD
1924 if (!stream_get(stream)) {
1925 continue;
1926 }
1927 if (stream->trace->session->id != session_id) {
1928 stream_put(stream);
1929 continue;
1930 }
1931 pthread_mutex_lock(&stream->lock);
1932 if (!stream->data_pending_check_done) {
1933 if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) {
1934 is_data_inflight = 1;
1935 DBG("Data is still in flight for stream %" PRIu64,
1936 stream->stream_handle);
1937 pthread_mutex_unlock(&stream->lock);
1938 stream_put(stream);
1939 break;
1940 }
f7079f67 1941 }
7591bab1
MD
1942 pthread_mutex_unlock(&stream->lock);
1943 stream_put(stream);
f7079f67
DG
1944 }
1945 rcu_read_unlock();
1946
53efb85a 1947 memset(&reply, 0, sizeof(reply));
f7079f67
DG
1948 /* All good, send back reply. */
1949 reply.ret_code = htobe32(is_data_inflight);
1950
58eb9381 1951 ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
f7079f67
DG
1952 if (ret < 0) {
1953 ERR("Relay end data pending send reply failed");
1954 }
1955
1956end_no_session:
1957 return ret;
1958}
1959
1c20f0e2
JD
1960/*
1961 * Receive an index for a specific stream.
1962 *
1963 * Return 0 on success else a negative value.
1964 */
7591bab1 1965static int relay_recv_index(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 1966 struct relay_connection *conn)
1c20f0e2 1967{
7591bab1 1968 int ret, send_ret;
58eb9381 1969 struct relay_session *session = conn->session;
1c20f0e2 1970 struct lttcomm_relayd_index index_info;
7591bab1 1971 struct relay_index *index;
1c20f0e2
JD
1972 struct lttcomm_relayd_generic_reply reply;
1973 struct relay_stream *stream;
1974 uint64_t net_seq_num;
f8f3885c 1975 size_t msg_len;
1c20f0e2 1976
58eb9381 1977 assert(conn);
1c20f0e2
JD
1978
1979 DBG("Relay receiving index");
1980
58eb9381 1981 if (!session || conn->version_check_done == 0) {
1c20f0e2
JD
1982 ERR("Trying to close a stream before version check");
1983 ret = -1;
1984 goto end_no_session;
1985 }
1986
f8f3885c
MD
1987 msg_len = lttcomm_relayd_index_len(
1988 lttng_to_index_major(conn->major, conn->minor),
1989 lttng_to_index_minor(conn->major, conn->minor));
58eb9381 1990 ret = conn->sock->ops->recvmsg(conn->sock, &index_info,
f8f3885c
MD
1991 msg_len, 0);
1992 if (ret < msg_len) {
1c20f0e2
JD
1993 if (ret == 0) {
1994 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 1995 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
1c20f0e2
JD
1996 } else {
1997 ERR("Relay didn't receive valid index struct size : %d", ret);
1998 }
1999 ret = -1;
2000 goto end_no_session;
2001 }
2002
2003 net_seq_num = be64toh(index_info.net_seq_num);
2004
7591bab1 2005 stream = stream_get_by_id(be64toh(index_info.relay_stream_id));
1c20f0e2 2006 if (!stream) {
7591bab1 2007 ERR("stream_get_by_id not found");
1c20f0e2 2008 ret = -1;
7591bab1 2009 goto end;
1c20f0e2 2010 }
7591bab1 2011 pthread_mutex_lock(&stream->lock);
1c20f0e2 2012
d3e2ba59
JD
2013 /* Live beacon handling */
2014 if (index_info.packet_size == 0) {
7591bab1
MD
2015 DBG("Received live beacon for stream %" PRIu64,
2016 stream->stream_handle);
d3e2ba59
JD
2017
2018 /*
7591bab1
MD
2019 * Only flag a stream inactive when it has already
2020 * received data and no indexes are in flight.
d3e2ba59 2021 */
a44ca2ca 2022 if (stream->index_received_seqcount > 0
7591bab1
MD
2023 && stream->indexes_in_flight == 0) {
2024 stream->beacon_ts_end =
2025 be64toh(index_info.timestamp_end);
d3e2ba59
JD
2026 }
2027 ret = 0;
7591bab1 2028 goto end_stream_put;
d3e2ba59
JD
2029 } else {
2030 stream->beacon_ts_end = -1ULL;
2031 }
2032
528f2ffa
JD
2033 if (stream->ctf_stream_id == -1ULL) {
2034 stream->ctf_stream_id = be64toh(index_info.stream_id);
2035 }
7591bab1
MD
2036 index = relay_index_get_by_id_or_create(stream, net_seq_num);
2037 if (!index) {
2038 ret = -1;
2039 ERR("relay_index_get_by_id_or_create index NULL");
2040 goto end_stream_put;
1c20f0e2 2041 }
234cd636 2042 if (set_index_control_data(index, &index_info, conn)) {
7591bab1
MD
2043 ERR("set_index_control_data error");
2044 relay_index_put(index);
2045 ret = -1;
2046 goto end_stream_put;
2047 }
2048 ret = relay_index_try_flush(index);
2049 if (ret == 0) {
a44ca2ca
MD
2050 tracefile_array_commit_seq(stream->tfa);
2051 stream->index_received_seqcount++;
7591bab1
MD
2052 } else if (ret > 0) {
2053 /* no flush. */
2054 ret = 0;
2055 } else {
2056 ERR("relay_index_try_flush error %d", ret);
2057 relay_index_put(index);
2058 ret = -1;
1c20f0e2
JD
2059 }
2060
7591bab1
MD
2061end_stream_put:
2062 pthread_mutex_unlock(&stream->lock);
2063 stream_put(stream);
2064
2065end:
1c20f0e2 2066
53efb85a 2067 memset(&reply, 0, sizeof(reply));
1c20f0e2
JD
2068 if (ret < 0) {
2069 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2070 } else {
2071 reply.ret_code = htobe32(LTTNG_OK);
2072 }
58eb9381 2073 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1c20f0e2
JD
2074 if (send_ret < 0) {
2075 ERR("Relay sending close index id reply");
2076 ret = send_ret;
2077 }
2078
2079end_no_session:
2080 return ret;
2081}
2082
a4baae1b
JD
2083/*
2084 * Receive the streams_sent message.
2085 *
2086 * Return 0 on success else a negative value.
2087 */
7591bab1 2088static int relay_streams_sent(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 2089 struct relay_connection *conn)
a4baae1b
JD
2090{
2091 int ret, send_ret;
2092 struct lttcomm_relayd_generic_reply reply;
2093
58eb9381 2094 assert(conn);
a4baae1b
JD
2095
2096 DBG("Relay receiving streams_sent");
2097
58eb9381 2098 if (!conn->session || conn->version_check_done == 0) {
a4baae1b
JD
2099 ERR("Trying to close a stream before version check");
2100 ret = -1;
2101 goto end_no_session;
2102 }
2103
2104 /*
7591bab1
MD
2105 * Publish every pending stream in the connection recv list which are
2106 * now ready to be used by the viewer.
4a9daf17 2107 */
7591bab1 2108 publish_connection_local_streams(conn);
4a9daf17 2109
53efb85a 2110 memset(&reply, 0, sizeof(reply));
a4baae1b 2111 reply.ret_code = htobe32(LTTNG_OK);
58eb9381 2112 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
a4baae1b
JD
2113 if (send_ret < 0) {
2114 ERR("Relay sending sent_stream reply");
2115 ret = send_ret;
2116 } else {
2117 /* Success. */
2118 ret = 0;
2119 }
2120
2121end_no_session:
2122 return ret;
2123}
2124
b8aa1682 2125/*
d3e2ba59 2126 * Process the commands received on the control socket
b8aa1682 2127 */
7591bab1 2128static int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
58eb9381 2129 struct relay_connection *conn)
b8aa1682
JD
2130{
2131 int ret = 0;
2132
2133 switch (be32toh(recv_hdr->cmd)) {
b8aa1682 2134 case RELAYD_CREATE_SESSION:
58eb9381 2135 ret = relay_create_session(recv_hdr, conn);
b8aa1682 2136 break;
b8aa1682 2137 case RELAYD_ADD_STREAM:
58eb9381 2138 ret = relay_add_stream(recv_hdr, conn);
b8aa1682
JD
2139 break;
2140 case RELAYD_START_DATA:
58eb9381 2141 ret = relay_start(recv_hdr, conn);
b8aa1682
JD
2142 break;
2143 case RELAYD_SEND_METADATA:
58eb9381 2144 ret = relay_recv_metadata(recv_hdr, conn);
b8aa1682
JD
2145 break;
2146 case RELAYD_VERSION:
58eb9381 2147 ret = relay_send_version(recv_hdr, conn);
b8aa1682 2148 break;
173af62f 2149 case RELAYD_CLOSE_STREAM:
58eb9381 2150 ret = relay_close_stream(recv_hdr, conn);
173af62f 2151 break;
6d805429 2152 case RELAYD_DATA_PENDING:
58eb9381 2153 ret = relay_data_pending(recv_hdr, conn);
c8f59ee5
DG
2154 break;
2155 case RELAYD_QUIESCENT_CONTROL:
58eb9381 2156 ret = relay_quiescent_control(recv_hdr, conn);
c8f59ee5 2157 break;
f7079f67 2158 case RELAYD_BEGIN_DATA_PENDING:
58eb9381 2159 ret = relay_begin_data_pending(recv_hdr, conn);
f7079f67
DG
2160 break;
2161 case RELAYD_END_DATA_PENDING:
58eb9381 2162 ret = relay_end_data_pending(recv_hdr, conn);
f7079f67 2163 break;
1c20f0e2 2164 case RELAYD_SEND_INDEX:
58eb9381 2165 ret = relay_recv_index(recv_hdr, conn);
1c20f0e2 2166 break;
a4baae1b 2167 case RELAYD_STREAMS_SENT:
58eb9381 2168 ret = relay_streams_sent(recv_hdr, conn);
a4baae1b 2169 break;
93ec662e
JD
2170 case RELAYD_RESET_METADATA:
2171 ret = relay_reset_metadata(recv_hdr, conn);
2172 break;
b8aa1682
JD
2173 case RELAYD_UPDATE_SYNC_INFO:
2174 default:
2175 ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
58eb9381 2176 relay_unknown_command(conn);
b8aa1682
JD
2177 ret = -1;
2178 goto end;
2179 }
2180
2181end:
2182 return ret;
2183}
2184
7d2f7452
DG
2185/*
2186 * Handle index for a data stream.
2187 *
7591bab1 2188 * Called with the stream lock held.
7d2f7452
DG
2189 *
2190 * Return 0 on success else a negative value.
2191 */
2192static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num,
2193 int rotate_index)
2194{
7591bab1
MD
2195 int ret = 0;
2196 uint64_t data_offset;
2197 struct relay_index *index;
7d2f7452 2198
7d2f7452
DG
2199 /* Get data offset because we are about to update the index. */
2200 data_offset = htobe64(stream->tracefile_size_current);
2201
65d66b19
MD
2202 DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64,
2203 stream->stream_handle, net_seq_num, stream->tracefile_size_current);
7591bab1 2204
7d2f7452 2205 /*
7591bab1
MD
2206 * Lookup for an existing index for that stream id/sequence
2207 * number. If it exists, the control thread has already received the
2208 * data for it, thus we need to write it to disk.
7d2f7452 2209 */
7591bab1 2210 index = relay_index_get_by_id_or_create(stream, net_seq_num);
7d2f7452 2211 if (!index) {
7591bab1
MD
2212 ret = -1;
2213 goto end;
7d2f7452
DG
2214 }
2215
f8f3885c
MD
2216 if (rotate_index || !stream->index_file) {
2217 uint32_t major, minor;
7591bab1 2218
f8f3885c
MD
2219 /* Put ref on previous index_file. */
2220 if (stream->index_file) {
2221 lttng_index_file_put(stream->index_file);
2222 stream->index_file = NULL;
7d2f7452 2223 }
f8f3885c
MD
2224 major = stream->trace->session->major;
2225 minor = stream->trace->session->minor;
2226 stream->index_file = lttng_index_file_create(stream->path_name,
2227 stream->channel_name,
7591bab1 2228 -1, -1, stream->tracefile_size,
f8f3885c
MD
2229 tracefile_array_get_file_index_head(stream->tfa),
2230 lttng_to_index_major(major, minor),
2231 lttng_to_index_minor(major, minor));
2232 if (!stream->index_file) {
7591bab1
MD
2233 ret = -1;
2234 /* Put self-ref for this index due to error. */
2235 relay_index_put(index);
f8f3885c 2236 index = NULL;
7591bab1 2237 goto end;
7d2f7452 2238 }
7d2f7452
DG
2239 }
2240
f8f3885c 2241 if (relay_index_set_file(index, stream->index_file, data_offset)) {
7591bab1
MD
2242 ret = -1;
2243 /* Put self-ref for this index due to error. */
2244 relay_index_put(index);
f8f3885c 2245 index = NULL;
7591bab1 2246 goto end;
7d2f7452
DG
2247 }
2248
7591bab1
MD
2249 ret = relay_index_try_flush(index);
2250 if (ret == 0) {
a44ca2ca
MD
2251 tracefile_array_commit_seq(stream->tfa);
2252 stream->index_received_seqcount++;
7591bab1
MD
2253 } else if (ret > 0) {
2254 /* No flush. */
2255 ret = 0;
2256 } else {
2257 /* Put self-ref for this index due to error. */
2258 relay_index_put(index);
f8f3885c 2259 index = NULL;
7591bab1
MD
2260 ret = -1;
2261 }
2262end:
7d2f7452
DG
2263 return ret;
2264}
2265
b8aa1682
JD
2266/*
2267 * relay_process_data: Process the data received on the data socket
2268 */
7591bab1 2269static int relay_process_data(struct relay_connection *conn)
b8aa1682 2270{
7d2f7452 2271 int ret = 0, rotate_index = 0;
6cd525e8 2272 ssize_t size_ret;
b8aa1682
JD
2273 struct relay_stream *stream;
2274 struct lttcomm_relayd_data_hdr data_hdr;
7d2f7452 2275 uint64_t stream_id;
173af62f 2276 uint64_t net_seq_num;
b8aa1682 2277 uint32_t data_size;
2a174661 2278 struct relay_session *session;
bda7c7b9 2279 bool new_stream = false, close_requested = false;
0848dba7
MD
2280 size_t chunk_size = RECV_DATA_BUFFER_SIZE;
2281 size_t recv_off = 0;
2282 char data_buffer[chunk_size];
b8aa1682 2283
58eb9381 2284 ret = conn->sock->ops->recvmsg(conn->sock, &data_hdr,
7c5aef62 2285 sizeof(struct lttcomm_relayd_data_hdr), 0);
b8aa1682 2286 if (ret <= 0) {
a6cd2b97
DG
2287 if (ret == 0) {
2288 /* Orderly shutdown. Not necessary to print an error. */
58eb9381 2289 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
a6cd2b97 2290 } else {
58eb9381 2291 ERR("Unable to receive data header on sock %d", conn->sock->fd);
a6cd2b97 2292 }
b8aa1682
JD
2293 ret = -1;
2294 goto end;
2295 }
2296
2297 stream_id = be64toh(data_hdr.stream_id);
7591bab1 2298 stream = stream_get_by_id(stream_id);
b8aa1682 2299 if (!stream) {
65d66b19 2300 ERR("relay_process_data: Cannot find stream %" PRIu64, stream_id);
b8aa1682 2301 ret = -1;
7591bab1 2302 goto end;
b8aa1682 2303 }
7591bab1 2304 session = stream->trace->session;
b8aa1682 2305 data_size = be32toh(data_hdr.data_size);
b8aa1682 2306
173af62f
DG
2307 net_seq_num = be64toh(data_hdr.net_seq_num);
2308
77c7c900 2309 DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64,
173af62f 2310 data_size, stream_id, net_seq_num);
b8aa1682 2311
7591bab1
MD
2312 pthread_mutex_lock(&stream->lock);
2313
1c20f0e2 2314 /* Check if a rotation is needed. */
0f907de1
JD
2315 if (stream->tracefile_size > 0 &&
2316 (stream->tracefile_size_current + data_size) >
2317 stream->tracefile_size) {
a44ca2ca
MD
2318 uint64_t old_id, new_id;
2319
2320 old_id = tracefile_array_get_file_index_head(stream->tfa);
2321 tracefile_array_file_rotate(stream->tfa);
2322
2323 /* new_id is updated by utils_rotate_stream_file. */
2324 new_id = old_id;
6b6b9a5a 2325
7591bab1
MD
2326 ret = utils_rotate_stream_file(stream->path_name,
2327 stream->channel_name, stream->tracefile_size,
2328 stream->tracefile_count, -1,
2329 -1, stream->stream_fd->fd,
a44ca2ca 2330 &new_id, &stream->stream_fd->fd);
0f907de1 2331 if (ret < 0) {
1c20f0e2 2332 ERR("Rotating stream output file");
7591bab1
MD
2333 goto end_stream_unlock;
2334 }
7591bab1
MD
2335 /*
2336 * Reset current size because we just performed a stream
2337 * rotation.
2338 */
a6976990 2339 stream->tracefile_size_current = 0;
1c20f0e2
JD
2340 rotate_index = 1;
2341 }
2342
1c20f0e2 2343 /*
7591bab1
MD
2344 * Index are handled in protocol version 2.4 and above. Also,
2345 * snapshot and index are NOT supported.
1c20f0e2 2346 */
2a174661 2347 if (session->minor >= 4 && !session->snapshot) {
7d2f7452 2348 ret = handle_index_data(stream, net_seq_num, rotate_index);
1c20f0e2 2349 if (ret < 0) {
65d66b19
MD
2350 ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d",
2351 stream->stream_handle, net_seq_num, ret);
7591bab1 2352 goto end_stream_unlock;
1c20f0e2 2353 }
1c20f0e2
JD
2354 }
2355
0848dba7
MD
2356 for (recv_off = 0; recv_off < data_size; recv_off += chunk_size) {
2357 size_t recv_size = min(data_size - recv_off, chunk_size);
1d4dfdef 2358
0848dba7
MD
2359 ret = conn->sock->ops->recvmsg(conn->sock, data_buffer, recv_size, 0);
2360 if (ret <= 0) {
2361 if (ret == 0) {
2362 /* Orderly shutdown. Not necessary to print an error. */
2363 DBG("Socket %d did an orderly shutdown", conn->sock->fd);
2364 } else {
2365 ERR("Socket %d error %d", conn->sock->fd, ret);
2366 }
2367 ret = -1;
2368 goto end_stream_unlock;
2369 }
2370
2371 /* Write data to stream output fd. */
2372 size_ret = lttng_write(stream->stream_fd->fd, data_buffer,
2373 recv_size);
2374 if (size_ret < recv_size) {
2375 ERR("Relay error writing data to file");
2376 ret = -1;
2377 goto end_stream_unlock;
2378 }
2379
2380 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64,
2381 size_ret, stream->stream_handle);
2382 }
5ab7344e 2383
7591bab1
MD
2384 ret = write_padding_to_file(stream->stream_fd->fd,
2385 be32toh(data_hdr.padding_size));
1d4dfdef 2386 if (ret < 0) {
65d66b19
MD
2387 ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d",
2388 stream->stream_handle, net_seq_num, ret);
7591bab1 2389 goto end_stream_unlock;
1d4dfdef 2390 }
7591bab1
MD
2391 stream->tracefile_size_current +=
2392 data_size + be32toh(data_hdr.padding_size);
c0bae11d
MD
2393 if (stream->prev_seq == -1ULL) {
2394 new_stream = true;
2395 }
2396
173af62f
DG
2397 stream->prev_seq = net_seq_num;
2398
7591bab1 2399end_stream_unlock:
bda7c7b9 2400 close_requested = stream->close_requested;
7591bab1 2401 pthread_mutex_unlock(&stream->lock);
bda7c7b9
JG
2402 if (close_requested) {
2403 try_stream_close(stream);
2404 }
2405
c0bae11d
MD
2406 if (new_stream) {
2407 pthread_mutex_lock(&session->lock);
2408 uatomic_set(&session->new_streams, 1);
2409 pthread_mutex_unlock(&session->lock);
2410 }
7591bab1 2411 stream_put(stream);
b8aa1682
JD
2412end:
2413 return ret;
2414}
2415
7591bab1 2416static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
b8aa1682
JD
2417{
2418 int ret;
2419
58eb9381 2420 (void) lttng_poll_del(events, pollfd);
b8aa1682
JD
2421
2422 ret = close(pollfd);
2423 if (ret < 0) {
2424 ERR("Closing pollfd %d", pollfd);
2425 }
2426}
2427
7591bab1
MD
2428static void relay_thread_close_connection(struct lttng_poll_event *events,
2429 int pollfd, struct relay_connection *conn)
9d1bbf21 2430{
7591bab1 2431 const char *type_str;
2a174661 2432
7591bab1
MD
2433 switch (conn->type) {
2434 case RELAY_DATA:
2435 type_str = "Data";
2436 break;
2437 case RELAY_CONTROL:
2438 type_str = "Control";
2439 break;
2440 case RELAY_VIEWER_COMMAND:
2441 type_str = "Viewer Command";
2442 break;
2443 case RELAY_VIEWER_NOTIFICATION:
2444 type_str = "Viewer Notification";
2445 break;
2446 default:
2447 type_str = "Unknown";
9d1bbf21 2448 }
7591bab1
MD
2449 cleanup_connection_pollfd(events, pollfd);
2450 connection_put(conn);
2451 DBG("%s connection closed with %d", type_str, pollfd);
b8aa1682
JD
2452}
2453
2454/*
2455 * This thread does the actual work
2456 */
7591bab1 2457static void *relay_thread_worker(void *data)
b8aa1682 2458{
beaad64c
DG
2459 int ret, err = -1, last_seen_data_fd = -1;
2460 uint32_t nb_fd;
b8aa1682
JD
2461 struct lttng_poll_event events;
2462 struct lttng_ht *relay_connections_ht;
b8aa1682 2463 struct lttng_ht_iter iter;
b8aa1682 2464 struct lttcomm_relayd_hdr recv_hdr;
90e7d72f 2465 struct relay_connection *destroy_conn = NULL;
b8aa1682
JD
2466
2467 DBG("[thread] Relay worker started");
2468
9d1bbf21
MD
2469 rcu_register_thread();
2470
55706a7d
MD
2471 health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER);
2472
9b5e0863
MD
2473 if (testpoint(relayd_thread_worker)) {
2474 goto error_testpoint;
2475 }
2476
f385ae0a
MD
2477 health_code_update();
2478
b8aa1682
JD
2479 /* table of connections indexed on socket */
2480 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
2481 if (!relay_connections_ht) {
2482 goto relay_connections_ht_error;
2483 }
b8aa1682 2484
b8aa1682
JD
2485 ret = create_thread_poll_set(&events, 2);
2486 if (ret < 0) {
2487 goto error_poll_create;
2488 }
2489
58eb9381 2490 ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
b8aa1682
JD
2491 if (ret < 0) {
2492 goto error;
2493 }
2494
beaad64c 2495restart:
b8aa1682 2496 while (1) {
beaad64c
DG
2497 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
2498
f385ae0a
MD
2499 health_code_update();
2500
b8aa1682 2501 /* Infinite blocking call, waiting for transmission */
87c1611d 2502 DBG3("Relayd worker thread polling...");
f385ae0a 2503 health_poll_entry();
b8aa1682 2504 ret = lttng_poll_wait(&events, -1);
f385ae0a 2505 health_poll_exit();
b8aa1682
JD
2506 if (ret < 0) {
2507 /*
2508 * Restart interrupted system call.
2509 */
2510 if (errno == EINTR) {
2511 goto restart;
2512 }
2513 goto error;
2514 }
2515
0d9c5d77
DG
2516 nb_fd = ret;
2517
beaad64c 2518 /*
7591bab1
MD
2519 * Process control. The control connection is
2520 * prioritized so we don't starve it with high
2521 * throughput tracing data on the data connection.
beaad64c 2522 */
b8aa1682
JD
2523 for (i = 0; i < nb_fd; i++) {
2524 /* Fetch once the poll data */
beaad64c
DG
2525 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2526 int pollfd = LTTNG_POLL_GETFD(&events, i);
b8aa1682 2527
f385ae0a
MD
2528 health_code_update();
2529
fd20dac9 2530 if (!revents) {
7591bab1
MD
2531 /*
2532 * No activity for this FD (poll
2533 * implementation).
2534 */
fd20dac9
MD
2535 continue;
2536 }
2537
b8aa1682
JD
2538 /* Thread quit pipe has been closed. Killing thread. */
2539 ret = check_thread_quit_pipe(pollfd, revents);
2540 if (ret) {
095a4ae5
MD
2541 err = 0;
2542 goto exit;
b8aa1682
JD
2543 }
2544
58eb9381
DG
2545 /* Inspect the relay conn pipe for new connection */
2546 if (pollfd == relay_conn_pipe[0]) {
03e43155 2547 if (revents & LPOLLIN) {
90e7d72f
JG
2548 struct relay_connection *conn;
2549
58eb9381 2550 ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn));
b8aa1682
JD
2551 if (ret < 0) {
2552 goto error;
2553 }
58eb9381
DG
2554 lttng_poll_add(&events, conn->sock->fd,
2555 LPOLLIN | LPOLLRDHUP);
7591bab1 2556 connection_ht_add(relay_connections_ht, conn);
58eb9381 2557 DBG("Connection socket %d added", conn->sock->fd);
03e43155
MD
2558 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2559 ERR("Relay connection pipe error");
2560 goto error;
2561 } else {
2562 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2563 goto error;
b8aa1682 2564 }
58eb9381 2565 } else {
90e7d72f
JG
2566 struct relay_connection *ctrl_conn;
2567
7591bab1 2568 ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd);
58eb9381 2569 /* If not found, there is a synchronization issue. */
90e7d72f 2570 assert(ctrl_conn);
58eb9381 2571
03e43155
MD
2572 if (ctrl_conn->type == RELAY_DATA) {
2573 if (revents & LPOLLIN) {
beaad64c
DG
2574 /*
2575 * Flag the last seen data fd not deleted. It will be
2576 * used as the last seen fd if any fd gets deleted in
2577 * this first loop.
2578 */
2579 last_notdel_data_fd = pollfd;
2580 }
03e43155
MD
2581 goto put_ctrl_connection;
2582 }
2583 assert(ctrl_conn->type == RELAY_CONTROL);
2584
2585 if (revents & LPOLLIN) {
2586 ret = ctrl_conn->sock->ops->recvmsg(ctrl_conn->sock,
2587 &recv_hdr, sizeof(recv_hdr), 0);
2588 if (ret <= 0) {
2589 /* Connection closed */
2590 relay_thread_close_connection(&events, pollfd,
2591 ctrl_conn);
2592 } else {
2593 ret = relay_process_control(&recv_hdr, ctrl_conn);
2594 if (ret < 0) {
2595 /* Clear the session on error. */
2596 relay_thread_close_connection(&events,
2597 pollfd, ctrl_conn);
2598 }
2599 seen_control = 1;
2600 }
2601 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2602 relay_thread_close_connection(&events,
2603 pollfd, ctrl_conn);
2604 if (last_seen_data_fd == pollfd) {
2605 last_seen_data_fd = last_notdel_data_fd;
2606 }
58eb9381 2607 } else {
03e43155
MD
2608 ERR("Unexpected poll events %u for control sock %d",
2609 revents, pollfd);
2610 connection_put(ctrl_conn);
2611 goto error;
beaad64c 2612 }
03e43155 2613 put_ctrl_connection:
7591bab1 2614 connection_put(ctrl_conn);
beaad64c
DG
2615 }
2616 }
2617
2618 /*
2619 * The last loop handled a control request, go back to poll to make
2620 * sure we prioritise the control socket.
2621 */
2622 if (seen_control) {
2623 continue;
2624 }
2625
2626 if (last_seen_data_fd >= 0) {
2627 for (i = 0; i < nb_fd; i++) {
2628 int pollfd = LTTNG_POLL_GETFD(&events, i);
f385ae0a
MD
2629
2630 health_code_update();
2631
beaad64c
DG
2632 if (last_seen_data_fd == pollfd) {
2633 idx = i;
2634 break;
2635 }
2636 }
2637 }
2638
2639 /* Process data connection. */
2640 for (i = idx + 1; i < nb_fd; i++) {
2641 /* Fetch the poll data. */
2642 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2643 int pollfd = LTTNG_POLL_GETFD(&events, i);
90e7d72f 2644 struct relay_connection *data_conn;
beaad64c 2645
f385ae0a
MD
2646 health_code_update();
2647
fd20dac9
MD
2648 if (!revents) {
2649 /* No activity for this FD (poll implementation). */
2650 continue;
2651 }
2652
beaad64c 2653 /* Skip the command pipe. It's handled in the first loop. */
58eb9381 2654 if (pollfd == relay_conn_pipe[0]) {
beaad64c
DG
2655 continue;
2656 }
2657
7591bab1 2658 data_conn = connection_get_by_sock(relay_connections_ht, pollfd);
90e7d72f 2659 if (!data_conn) {
fd20dac9 2660 /* Skip it. Might be removed before. */
fd20dac9
MD
2661 continue;
2662 }
03e43155
MD
2663 if (data_conn->type == RELAY_CONTROL) {
2664 goto put_data_connection;
2665 }
2666 assert(data_conn->type == RELAY_DATA);
fd20dac9
MD
2667
2668 if (revents & LPOLLIN) {
90e7d72f 2669 ret = relay_process_data(data_conn);
fd20dac9
MD
2670 /* Connection closed */
2671 if (ret < 0) {
7591bab1 2672 relay_thread_close_connection(&events, pollfd,
03e43155 2673 data_conn);
fd20dac9
MD
2674 /*
2675 * Every goto restart call sets the last seen fd where
2676 * here we don't really care since we gracefully
2677 * continue the loop after the connection is deleted.
2678 */
2679 } else {
2680 /* Keep last seen port. */
2681 last_seen_data_fd = pollfd;
7591bab1 2682 connection_put(data_conn);
fd20dac9 2683 goto restart;
b8aa1682 2684 }
03e43155
MD
2685 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2686 relay_thread_close_connection(&events, pollfd,
2687 data_conn);
2688 } else {
2689 ERR("Unknown poll events %u for data sock %d",
2690 revents, pollfd);
b8aa1682 2691 }
03e43155 2692 put_data_connection:
7591bab1 2693 connection_put(data_conn);
b8aa1682 2694 }
beaad64c 2695 last_seen_data_fd = -1;
b8aa1682
JD
2696 }
2697
f385ae0a
MD
2698 /* Normal exit, no error */
2699 ret = 0;
2700
095a4ae5 2701exit:
b8aa1682 2702error:
58eb9381 2703 /* Cleanup reamaining connection object. */
9d1bbf21 2704 rcu_read_lock();
90e7d72f
JG
2705 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter,
2706 destroy_conn,
58eb9381 2707 sock_n.node) {
f385ae0a 2708 health_code_update();
98ba050e
JR
2709
2710 if (session_abort(destroy_conn->session)) {
2711 assert(0);
2712 }
2713
7591bab1
MD
2714 /*
2715 * No need to grab another ref, because we own
2716 * destroy_conn.
2717 */
2718 relay_thread_close_connection(&events, destroy_conn->sock->fd,
2719 destroy_conn);
b8aa1682 2720 }
94d49140 2721 rcu_read_unlock();
7591bab1
MD
2722
2723 lttng_poll_clean(&events);
7d2f7452 2724error_poll_create:
b8aa1682 2725 lttng_ht_destroy(relay_connections_ht);
095a4ae5 2726relay_connections_ht_error:
58eb9381
DG
2727 /* Close relay conn pipes */
2728 utils_close_pipe(relay_conn_pipe);
095a4ae5
MD
2729 if (err) {
2730 DBG("Thread exited with error");
2731 }
b8aa1682 2732 DBG("Worker thread cleanup complete");
9b5e0863 2733error_testpoint:
f385ae0a
MD
2734 if (err) {
2735 health_error();
2736 ERR("Health error occurred in %s", __func__);
2737 }
2738 health_unregister(health_relayd);
9d1bbf21 2739 rcu_unregister_thread();
b4aacfdc 2740 lttng_relay_stop_threads();
b8aa1682
JD
2741 return NULL;
2742}
2743
2744/*
2745 * Create the relay command pipe to wake thread_manage_apps.
2746 * Closed in cleanup().
2747 */
58eb9381 2748static int create_relay_conn_pipe(void)
b8aa1682 2749{
a02de639 2750 int ret;
b8aa1682 2751
58eb9381 2752 ret = utils_create_pipe_cloexec(relay_conn_pipe);
b8aa1682 2753
b8aa1682
JD
2754 return ret;
2755}
2756
2757/*
2758 * main
2759 */
2760int main(int argc, char **argv)
2761{
178a0557 2762 int ret = 0, retval = 0;
b8aa1682
JD
2763 void *status;
2764
b8aa1682
JD
2765 /* Parse arguments */
2766 progname = argv[0];
178a0557
MD
2767 if (set_options(argc, argv)) {
2768 retval = -1;
2769 goto exit_options;
b8aa1682
JD
2770 }
2771
178a0557
MD
2772 if (set_signal_handler()) {
2773 retval = -1;
2774 goto exit_options;
b8aa1682
JD
2775 }
2776
4d513a50
DG
2777 /* Try to create directory if -o, --output is specified. */
2778 if (opt_output_path) {
994fa64f
DG
2779 if (*opt_output_path != '/') {
2780 ERR("Please specify an absolute path for -o, --output PATH");
178a0557
MD
2781 retval = -1;
2782 goto exit_options;
994fa64f
DG
2783 }
2784
d77dded2
JG
2785 ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG,
2786 -1, -1);
4d513a50
DG
2787 if (ret < 0) {
2788 ERR("Unable to create %s", opt_output_path);
178a0557
MD
2789 retval = -1;
2790 goto exit_options;
4d513a50
DG
2791 }
2792 }
2793
b8aa1682 2794 /* Daemonize */
b5218ffb 2795 if (opt_daemon || opt_background) {
3fd27398
MD
2796 int i;
2797
2798 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
2799 !opt_background);
b8aa1682 2800 if (ret < 0) {
178a0557
MD
2801 retval = -1;
2802 goto exit_options;
b8aa1682 2803 }
3fd27398
MD
2804
2805 /*
2806 * We are in the child. Make sure all other file
2807 * descriptors are closed, in case we are called with
2808 * more opened file descriptors than the standard ones.
2809 */
2810 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
2811 (void) close(i);
2812 }
2813 }
2814
178a0557
MD
2815 /* Initialize thread health monitoring */
2816 health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
2817 if (!health_relayd) {
2818 PERROR("health_app_create error");
2819 retval = -1;
2820 goto exit_health_app_create;
2821 }
2822
3fd27398 2823 /* Create thread quit pipe */
178a0557
MD
2824 if (init_thread_quit_pipe()) {
2825 retval = -1;
2826 goto exit_init_data;
b8aa1682
JD
2827 }
2828
b8aa1682 2829 /* Setup the thread apps communication pipe. */
178a0557
MD
2830 if (create_relay_conn_pipe()) {
2831 retval = -1;
2832 goto exit_init_data;
b8aa1682
JD
2833 }
2834
2835 /* Init relay command queue. */
8bdee6e2 2836 cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail);
b8aa1682 2837
554831e7
MD
2838 /* Initialize communication library */
2839 lttcomm_init();
87e45c13 2840 lttcomm_inet_init();
554831e7 2841
d3e2ba59 2842 /* tables of sessions indexed by session ID */
7591bab1
MD
2843 sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2844 if (!sessions_ht) {
178a0557
MD
2845 retval = -1;
2846 goto exit_init_data;
d3e2ba59
JD
2847 }
2848
2849 /* tables of streams indexed by stream ID */
2a174661 2850 relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d3e2ba59 2851 if (!relay_streams_ht) {
178a0557
MD
2852 retval = -1;
2853 goto exit_init_data;
d3e2ba59
JD
2854 }
2855
2856 /* tables of streams indexed by stream ID */
92c6ca54
DG
2857 viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2858 if (!viewer_streams_ht) {
178a0557
MD
2859 retval = -1;
2860 goto exit_init_data;
55706a7d
MD
2861 }
2862
65931c8b 2863 ret = utils_create_pipe(health_quit_pipe);
178a0557
MD
2864 if (ret) {
2865 retval = -1;
2866 goto exit_health_quit_pipe;
65931c8b
MD
2867 }
2868
2869 /* Create thread to manage the client socket */
1a1a34b4 2870 ret = pthread_create(&health_thread, default_pthread_attr(),
65931c8b 2871 thread_manage_health, (void *) NULL);
178a0557
MD
2872 if (ret) {
2873 errno = ret;
65931c8b 2874 PERROR("pthread_create health");
178a0557
MD
2875 retval = -1;
2876 goto exit_health_thread;
65931c8b
MD
2877 }
2878
b8aa1682 2879 /* Setup the dispatcher thread */
1a1a34b4 2880 ret = pthread_create(&dispatcher_thread, default_pthread_attr(),
b8aa1682 2881 relay_thread_dispatcher, (void *) NULL);
178a0557
MD
2882 if (ret) {
2883 errno = ret;
b8aa1682 2884 PERROR("pthread_create dispatcher");
178a0557
MD
2885 retval = -1;
2886 goto exit_dispatcher_thread;
b8aa1682
JD
2887 }
2888
2889 /* Setup the worker thread */
1a1a34b4 2890 ret = pthread_create(&worker_thread, default_pthread_attr(),
7591bab1 2891 relay_thread_worker, NULL);
178a0557
MD
2892 if (ret) {
2893 errno = ret;
b8aa1682 2894 PERROR("pthread_create worker");
178a0557
MD
2895 retval = -1;
2896 goto exit_worker_thread;
b8aa1682
JD
2897 }
2898
2899 /* Setup the listener thread */
1a1a34b4 2900 ret = pthread_create(&listener_thread, default_pthread_attr(),
b8aa1682 2901 relay_thread_listener, (void *) NULL);
178a0557
MD
2902 if (ret) {
2903 errno = ret;
b8aa1682 2904 PERROR("pthread_create listener");
178a0557
MD
2905 retval = -1;
2906 goto exit_listener_thread;
b8aa1682
JD
2907 }
2908
7591bab1 2909 ret = relayd_live_create(live_uri);
178a0557 2910 if (ret) {
d3e2ba59 2911 ERR("Starting live viewer threads");
178a0557 2912 retval = -1;
50138f51 2913 goto exit_live;
d3e2ba59
JD
2914 }
2915
178a0557
MD
2916 /*
2917 * This is where we start awaiting program completion (e.g. through
2918 * signal that asks threads to teardown).
2919 */
2920
2921 ret = relayd_live_join();
2922 if (ret) {
2923 retval = -1;
2924 }
50138f51 2925exit_live:
178a0557 2926
b8aa1682 2927 ret = pthread_join(listener_thread, &status);
178a0557
MD
2928 if (ret) {
2929 errno = ret;
2930 PERROR("pthread_join listener_thread");
2931 retval = -1;
b8aa1682
JD
2932 }
2933
178a0557 2934exit_listener_thread:
b8aa1682 2935 ret = pthread_join(worker_thread, &status);
178a0557
MD
2936 if (ret) {
2937 errno = ret;
2938 PERROR("pthread_join worker_thread");
2939 retval = -1;
b8aa1682
JD
2940 }
2941
178a0557 2942exit_worker_thread:
b8aa1682 2943 ret = pthread_join(dispatcher_thread, &status);
178a0557
MD
2944 if (ret) {
2945 errno = ret;
2946 PERROR("pthread_join dispatcher_thread");
2947 retval = -1;
b8aa1682 2948 }
178a0557 2949exit_dispatcher_thread:
42415026 2950
65931c8b 2951 ret = pthread_join(health_thread, &status);
178a0557
MD
2952 if (ret) {
2953 errno = ret;
2954 PERROR("pthread_join health_thread");
2955 retval = -1;
65931c8b 2956 }
178a0557 2957exit_health_thread:
65931c8b 2958
65931c8b 2959 utils_close_pipe(health_quit_pipe);
178a0557 2960exit_health_quit_pipe:
65931c8b 2961
178a0557 2962exit_init_data:
55706a7d 2963 health_app_destroy(health_relayd);
55706a7d 2964exit_health_app_create:
178a0557 2965exit_options:
4d62fbf8
MD
2966 /*
2967 * Wait for all pending call_rcu work to complete before tearing
2968 * down data structures. call_rcu worker may be trying to
2969 * perform lookups in those structures.
2970 */
2971 rcu_barrier();
7591bab1
MD
2972 relayd_cleanup();
2973
2974 /* Ensure all prior call_rcu are done. */
2975 rcu_barrier();
d3e2ba59 2976
178a0557 2977 if (!retval) {
b8aa1682 2978 exit(EXIT_SUCCESS);
178a0557
MD
2979 } else {
2980 exit(EXIT_FAILURE);
b8aa1682 2981 }
b8aa1682 2982}
This page took 0.216938 seconds and 4 git commands to generate.