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