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