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