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