Introduce EXTRA_VERSION_PATCHES
[lttng-tools.git] / src / bin / lttng-relayd / main.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 _LGPL_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>
37#include <inttypes.h>
38#include <urcu/futex.h>
39#include <urcu/uatomic.h>
40#include <urcu/rculist.h>
41#include <unistd.h>
42#include <fcntl.h>
43#include <strings.h>
44
45#include <lttng/lttng.h>
46#include <common/common.h>
47#include <common/compat/poll.h>
48#include <common/compat/socket.h>
49#include <common/compat/endian.h>
50#include <common/compat/getenv.h>
51#include <common/defaults.h>
52#include <common/daemonize.h>
53#include <common/futex.h>
54#include <common/sessiond-comm/sessiond-comm.h>
55#include <common/sessiond-comm/inet.h>
56#include <common/sessiond-comm/relayd.h>
57#include <common/uri.h>
58#include <common/utils.h>
59#include <common/align.h>
60#include <common/config/session-config.h>
61#include <common/dynamic-buffer.h>
62#include <common/buffer-view.h>
63#include <common/string-utils/format.h>
64
65#include "version.h"
66#include "cmd.h"
67#include "ctf-trace.h"
68#include "index.h"
69#include "utils.h"
70#include "lttng-relayd.h"
71#include "live.h"
72#include "health-relayd.h"
73#include "testpoint.h"
74#include "viewer-stream.h"
75#include "session.h"
76#include "stream.h"
77#include "connection.h"
78#include "tracefile-array.h"
79#include "tcp_keep_alive.h"
80#include "sessiond-trace-chunks.h"
81
82static const char *help_msg =
83#ifdef LTTNG_EMBED_HELP
84#include <lttng-relayd.8.h>
85#else
86NULL
87#endif
88;
89
90enum relay_connection_status {
91 RELAY_CONNECTION_STATUS_OK,
92 /* An error occurred while processing an event on the connection. */
93 RELAY_CONNECTION_STATUS_ERROR,
94 /* Connection closed/shutdown cleanly. */
95 RELAY_CONNECTION_STATUS_CLOSED,
96};
97
98/* command line options */
99char *opt_output_path;
100static int opt_daemon, opt_background, opt_print_version;
101
102/*
103 * We need to wait for listener and live listener threads, as well as
104 * health check thread, before being ready to signal readiness.
105 */
106#define NR_LTTNG_RELAY_READY 3
107static int lttng_relay_ready = NR_LTTNG_RELAY_READY;
108
109/* Size of receive buffer. */
110#define RECV_DATA_BUFFER_SIZE 65536
111
112static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */
113static pid_t child_ppid; /* Internal parent PID use with daemonize. */
114
115static struct lttng_uri *control_uri;
116static struct lttng_uri *data_uri;
117static struct lttng_uri *live_uri;
118
119const char *progname;
120
121const char *tracing_group_name = DEFAULT_TRACING_GROUP;
122static int tracing_group_name_override;
123
124const char * const config_section_name = "relayd";
125
126/*
127 * Quit pipe for all threads. This permits a single cancellation point
128 * for all threads when receiving an event on the pipe.
129 */
130int thread_quit_pipe[2] = { -1, -1 };
131
132/*
133 * This pipe is used to inform the worker thread that a command is queued and
134 * ready to be processed.
135 */
136static int relay_conn_pipe[2] = { -1, -1 };
137
138/* Shared between threads */
139static int dispatch_thread_exit;
140
141static pthread_t listener_thread;
142static pthread_t dispatcher_thread;
143static pthread_t worker_thread;
144static pthread_t health_thread;
145
146/*
147 * last_relay_stream_id_lock protects last_relay_stream_id increment
148 * atomicity on 32-bit architectures.
149 */
150static pthread_mutex_t last_relay_stream_id_lock = PTHREAD_MUTEX_INITIALIZER;
151static uint64_t last_relay_stream_id;
152
153/*
154 * Relay command queue.
155 *
156 * The relay_thread_listener and relay_thread_dispatcher communicate with this
157 * queue.
158 */
159static struct relay_conn_queue relay_conn_queue;
160
161/* Global relay stream hash table. */
162struct lttng_ht *relay_streams_ht;
163
164/* Global relay viewer stream hash table. */
165struct lttng_ht *viewer_streams_ht;
166
167/* Global relay sessions hash table. */
168struct lttng_ht *sessions_ht;
169
170/* Relayd health monitoring */
171struct health_app *health_relayd;
172
173struct sessiond_trace_chunk_registry *sessiond_trace_chunk_registry;
174
175static struct option long_options[] = {
176 { "control-port", 1, 0, 'C', },
177 { "data-port", 1, 0, 'D', },
178 { "live-port", 1, 0, 'L', },
179 { "daemonize", 0, 0, 'd', },
180 { "background", 0, 0, 'b', },
181 { "group", 1, 0, 'g', },
182 { "help", 0, 0, 'h', },
183 { "output", 1, 0, 'o', },
184 { "verbose", 0, 0, 'v', },
185 { "config", 1, 0, 'f' },
186 { "version", 0, 0, 'V' },
187 { NULL, 0, 0, 0, },
188};
189
190static const char *config_ignore_options[] = { "help", "config", "version" };
191
192static void print_version(void) {
193 fprintf(stdout, "%s\n", VERSION);
194}
195
196static void relayd_config_log(void)
197{
198 DBG("LTTng-relayd " VERSION " - " VERSION_NAME "%s%s",
199 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION,
200 EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME);
201 if (EXTRA_VERSION_DESCRIPTION[0] != '\0') {
202 DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n");
203 }
204 if (EXTRA_VERSION_PATCHES[0] != '\0') {
205 DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES "\n");
206 }
207}
208
209/*
210 * Take an option from the getopt output and set it in the right variable to be
211 * used later.
212 *
213 * Return 0 on success else a negative value.
214 */
215static int set_option(int opt, const char *arg, const char *optname)
216{
217 int ret;
218
219 switch (opt) {
220 case 0:
221 fprintf(stderr, "option %s", optname);
222 if (arg) {
223 fprintf(stderr, " with arg %s\n", arg);
224 }
225 break;
226 case 'C':
227 if (lttng_is_setuid_setgid()) {
228 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
229 "-C, --control-port");
230 } else {
231 ret = uri_parse(arg, &control_uri);
232 if (ret < 0) {
233 ERR("Invalid control URI specified");
234 goto end;
235 }
236 if (control_uri->port == 0) {
237 control_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
238 }
239 }
240 break;
241 case 'D':
242 if (lttng_is_setuid_setgid()) {
243 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
244 "-D, -data-port");
245 } else {
246 ret = uri_parse(arg, &data_uri);
247 if (ret < 0) {
248 ERR("Invalid data URI specified");
249 goto end;
250 }
251 if (data_uri->port == 0) {
252 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
253 }
254 }
255 break;
256 case 'L':
257 if (lttng_is_setuid_setgid()) {
258 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
259 "-L, -live-port");
260 } else {
261 ret = uri_parse(arg, &live_uri);
262 if (ret < 0) {
263 ERR("Invalid live URI specified");
264 goto end;
265 }
266 if (live_uri->port == 0) {
267 live_uri->port = DEFAULT_NETWORK_VIEWER_PORT;
268 }
269 }
270 break;
271 case 'd':
272 opt_daemon = 1;
273 break;
274 case 'b':
275 opt_background = 1;
276 break;
277 case 'g':
278 if (lttng_is_setuid_setgid()) {
279 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
280 "-g, --group");
281 } else {
282 tracing_group_name = strdup(arg);
283 if (tracing_group_name == NULL) {
284 ret = -errno;
285 PERROR("strdup");
286 goto end;
287 }
288 tracing_group_name_override = 1;
289 }
290 break;
291 case 'h':
292 ret = utils_show_help(8, "lttng-relayd", help_msg);
293 if (ret) {
294 ERR("Cannot show --help for `lttng-relayd`");
295 perror("exec");
296 }
297 exit(EXIT_FAILURE);
298 case 'V':
299 opt_print_version = 1;
300 break;
301 case 'o':
302 if (lttng_is_setuid_setgid()) {
303 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
304 "-o, --output");
305 } else {
306 ret = asprintf(&opt_output_path, "%s", arg);
307 if (ret < 0) {
308 ret = -errno;
309 PERROR("asprintf opt_output_path");
310 goto end;
311 }
312 }
313 break;
314 case 'v':
315 /* Verbose level can increase using multiple -v */
316 if (arg) {
317 lttng_opt_verbose = config_parse_value(arg);
318 } else {
319 /* Only 3 level of verbosity (-vvv). */
320 if (lttng_opt_verbose < 3) {
321 lttng_opt_verbose += 1;
322 }
323 }
324 break;
325 default:
326 /* Unknown option or other error.
327 * Error is printed by getopt, just return */
328 ret = -1;
329 goto end;
330 }
331
332 /* All good. */
333 ret = 0;
334
335end:
336 return ret;
337}
338
339/*
340 * config_entry_handler_cb used to handle options read from a config file.
341 * See config_entry_handler_cb comment in common/config/session-config.h for the
342 * return value conventions.
343 */
344static int config_entry_handler(const struct config_entry *entry, void *unused)
345{
346 int ret = 0, i;
347
348 if (!entry || !entry->name || !entry->value) {
349 ret = -EINVAL;
350 goto end;
351 }
352
353 /* Check if the option is to be ignored */
354 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
355 if (!strcmp(entry->name, config_ignore_options[i])) {
356 goto end;
357 }
358 }
359
360 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) {
361 /* Ignore if entry name is not fully matched. */
362 if (strcmp(entry->name, long_options[i].name)) {
363 continue;
364 }
365
366 /*
367 * If the option takes no argument on the command line,
368 * we have to check if the value is "true". We support
369 * non-zero numeric values, true, on and yes.
370 */
371 if (!long_options[i].has_arg) {
372 ret = config_parse_value(entry->value);
373 if (ret <= 0) {
374 if (ret) {
375 WARN("Invalid configuration value \"%s\" for option %s",
376 entry->value, entry->name);
377 }
378 /* False, skip boolean config option. */
379 goto end;
380 }
381 }
382
383 ret = set_option(long_options[i].val, entry->value, entry->name);
384 goto end;
385 }
386
387 WARN("Unrecognized option \"%s\" in daemon configuration file.",
388 entry->name);
389
390end:
391 return ret;
392}
393
394static int set_options(int argc, char **argv)
395{
396 int c, ret = 0, option_index = 0, retval = 0;
397 int orig_optopt = optopt, orig_optind = optind;
398 char *default_address, *optstring;
399 const char *config_path = NULL;
400
401 optstring = utils_generate_optstring(long_options,
402 sizeof(long_options) / sizeof(struct option));
403 if (!optstring) {
404 retval = -ENOMEM;
405 goto exit;
406 }
407
408 /* Check for the --config option */
409
410 while ((c = getopt_long(argc, argv, optstring, long_options,
411 &option_index)) != -1) {
412 if (c == '?') {
413 retval = -EINVAL;
414 goto exit;
415 } else if (c != 'f') {
416 continue;
417 }
418
419 if (lttng_is_setuid_setgid()) {
420 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
421 "-f, --config");
422 } else {
423 config_path = utils_expand_path(optarg);
424 if (!config_path) {
425 ERR("Failed to resolve path: %s", optarg);
426 }
427 }
428 }
429
430 ret = config_get_section_entries(config_path, config_section_name,
431 config_entry_handler, NULL);
432 if (ret) {
433 if (ret > 0) {
434 ERR("Invalid configuration option at line %i", ret);
435 }
436 retval = -1;
437 goto exit;
438 }
439
440 /* Reset getopt's global state */
441 optopt = orig_optopt;
442 optind = orig_optind;
443 while (1) {
444 c = getopt_long(argc, argv, optstring, long_options, &option_index);
445 if (c == -1) {
446 break;
447 }
448
449 ret = set_option(c, optarg, long_options[option_index].name);
450 if (ret < 0) {
451 retval = -1;
452 goto exit;
453 }
454 }
455
456 /* assign default values */
457 if (control_uri == NULL) {
458 ret = asprintf(&default_address,
459 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d",
460 DEFAULT_NETWORK_CONTROL_PORT);
461 if (ret < 0) {
462 PERROR("asprintf default data address");
463 retval = -1;
464 goto exit;
465 }
466
467 ret = uri_parse(default_address, &control_uri);
468 free(default_address);
469 if (ret < 0) {
470 ERR("Invalid control URI specified");
471 retval = -1;
472 goto exit;
473 }
474 }
475 if (data_uri == NULL) {
476 ret = asprintf(&default_address,
477 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d",
478 DEFAULT_NETWORK_DATA_PORT);
479 if (ret < 0) {
480 PERROR("asprintf default data address");
481 retval = -1;
482 goto exit;
483 }
484
485 ret = uri_parse(default_address, &data_uri);
486 free(default_address);
487 if (ret < 0) {
488 ERR("Invalid data URI specified");
489 retval = -1;
490 goto exit;
491 }
492 }
493 if (live_uri == NULL) {
494 ret = asprintf(&default_address,
495 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d",
496 DEFAULT_NETWORK_VIEWER_PORT);
497 if (ret < 0) {
498 PERROR("asprintf default viewer control address");
499 retval = -1;
500 goto exit;
501 }
502
503 ret = uri_parse(default_address, &live_uri);
504 free(default_address);
505 if (ret < 0) {
506 ERR("Invalid viewer control URI specified");
507 retval = -1;
508 goto exit;
509 }
510 }
511
512exit:
513 free(optstring);
514 return retval;
515}
516
517static void print_global_objects(void)
518{
519 rcu_register_thread();
520
521 print_viewer_streams();
522 print_relay_streams();
523 print_sessions();
524
525 rcu_unregister_thread();
526}
527
528/*
529 * Cleanup the daemon
530 */
531static void relayd_cleanup(void)
532{
533 print_global_objects();
534
535 DBG("Cleaning up");
536
537 if (viewer_streams_ht)
538 lttng_ht_destroy(viewer_streams_ht);
539 if (relay_streams_ht)
540 lttng_ht_destroy(relay_streams_ht);
541 if (sessions_ht)
542 lttng_ht_destroy(sessions_ht);
543
544 /* free the dynamically allocated opt_output_path */
545 free(opt_output_path);
546
547 /* Close thread quit pipes */
548 utils_close_pipe(thread_quit_pipe);
549
550 uri_free(control_uri);
551 uri_free(data_uri);
552 /* Live URI is freed in the live thread. */
553
554 if (tracing_group_name_override) {
555 free((void *) tracing_group_name);
556 }
557}
558
559/*
560 * Write to writable pipe used to notify a thread.
561 */
562static int notify_thread_pipe(int wpipe)
563{
564 ssize_t ret;
565
566 ret = lttng_write(wpipe, "!", 1);
567 if (ret < 1) {
568 PERROR("write poll pipe");
569 goto end;
570 }
571 ret = 0;
572end:
573 return ret;
574}
575
576static int notify_health_quit_pipe(int *pipe)
577{
578 ssize_t ret;
579
580 ret = lttng_write(pipe[1], "4", 1);
581 if (ret < 1) {
582 PERROR("write relay health quit");
583 goto end;
584 }
585 ret = 0;
586end:
587 return ret;
588}
589
590/*
591 * Stop all relayd and relayd-live threads.
592 */
593int lttng_relay_stop_threads(void)
594{
595 int retval = 0;
596
597 /* Stopping all threads */
598 DBG("Terminating all threads");
599 if (notify_thread_pipe(thread_quit_pipe[1])) {
600 ERR("write error on thread quit pipe");
601 retval = -1;
602 }
603
604 if (notify_health_quit_pipe(health_quit_pipe)) {
605 ERR("write error on health quit pipe");
606 }
607
608 /* Dispatch thread */
609 CMM_STORE_SHARED(dispatch_thread_exit, 1);
610 futex_nto1_wake(&relay_conn_queue.futex);
611
612 if (relayd_live_stop()) {
613 ERR("Error stopping live threads");
614 retval = -1;
615 }
616 return retval;
617}
618
619/*
620 * Signal handler for the daemon
621 *
622 * Simply stop all worker threads, leaving main() return gracefully after
623 * joining all threads and calling cleanup().
624 */
625static void sighandler(int sig)
626{
627 switch (sig) {
628 case SIGINT:
629 DBG("SIGINT caught");
630 if (lttng_relay_stop_threads()) {
631 ERR("Error stopping threads");
632 }
633 break;
634 case SIGTERM:
635 DBG("SIGTERM caught");
636 if (lttng_relay_stop_threads()) {
637 ERR("Error stopping threads");
638 }
639 break;
640 case SIGUSR1:
641 CMM_STORE_SHARED(recv_child_signal, 1);
642 break;
643 default:
644 break;
645 }
646}
647
648/*
649 * Setup signal handler for :
650 * SIGINT, SIGTERM, SIGPIPE
651 */
652static int set_signal_handler(void)
653{
654 int ret = 0;
655 struct sigaction sa;
656 sigset_t sigset;
657
658 if ((ret = sigemptyset(&sigset)) < 0) {
659 PERROR("sigemptyset");
660 return ret;
661 }
662
663 sa.sa_mask = sigset;
664 sa.sa_flags = 0;
665
666 sa.sa_handler = sighandler;
667 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
668 PERROR("sigaction");
669 return ret;
670 }
671
672 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
673 PERROR("sigaction");
674 return ret;
675 }
676
677 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
678 PERROR("sigaction");
679 return ret;
680 }
681
682 sa.sa_handler = SIG_IGN;
683 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
684 PERROR("sigaction");
685 return ret;
686 }
687
688 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
689
690 return ret;
691}
692
693void lttng_relay_notify_ready(void)
694{
695 /* Notify the parent of the fork() process that we are ready. */
696 if (opt_daemon || opt_background) {
697 if (uatomic_sub_return(&lttng_relay_ready, 1) == 0) {
698 kill(child_ppid, SIGUSR1);
699 }
700 }
701}
702
703/*
704 * Init thread quit pipe.
705 *
706 * Return -1 on error or 0 if all pipes are created.
707 */
708static int init_thread_quit_pipe(void)
709{
710 int ret;
711
712 ret = utils_create_pipe_cloexec(thread_quit_pipe);
713
714 return ret;
715}
716
717/*
718 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
719 */
720static int create_thread_poll_set(struct lttng_poll_event *events, int size)
721{
722 int ret;
723
724 if (events == NULL || size == 0) {
725 ret = -1;
726 goto error;
727 }
728
729 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
730 if (ret < 0) {
731 goto error;
732 }
733
734 /* Add quit pipe */
735 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
736 if (ret < 0) {
737 goto error;
738 }
739
740 return 0;
741
742error:
743 return ret;
744}
745
746/*
747 * Check if the thread quit pipe was triggered.
748 *
749 * Return 1 if it was triggered else 0;
750 */
751static int check_thread_quit_pipe(int fd, uint32_t events)
752{
753 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
754 return 1;
755 }
756
757 return 0;
758}
759
760/*
761 * Create and init socket from uri.
762 */
763static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri)
764{
765 int ret;
766 struct lttcomm_sock *sock = NULL;
767
768 sock = lttcomm_alloc_sock_from_uri(uri);
769 if (sock == NULL) {
770 ERR("Allocating socket");
771 goto error;
772 }
773
774 ret = lttcomm_create_sock(sock);
775 if (ret < 0) {
776 goto error;
777 }
778 DBG("Listening on sock %d", sock->fd);
779
780 ret = sock->ops->bind(sock);
781 if (ret < 0) {
782 PERROR("Failed to bind socket");
783 goto error;
784 }
785
786 ret = sock->ops->listen(sock, -1);
787 if (ret < 0) {
788 goto error;
789
790 }
791
792 return sock;
793
794error:
795 if (sock) {
796 lttcomm_destroy_sock(sock);
797 }
798 return NULL;
799}
800
801/*
802 * This thread manages the listening for new connections on the network
803 */
804static void *relay_thread_listener(void *data)
805{
806 int i, ret, pollfd, err = -1;
807 uint32_t revents, nb_fd;
808 struct lttng_poll_event events;
809 struct lttcomm_sock *control_sock, *data_sock;
810
811 DBG("[thread] Relay listener started");
812
813 health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER);
814
815 health_code_update();
816
817 control_sock = relay_socket_create(control_uri);
818 if (!control_sock) {
819 goto error_sock_control;
820 }
821
822 data_sock = relay_socket_create(data_uri);
823 if (!data_sock) {
824 goto error_sock_relay;
825 }
826
827 /*
828 * Pass 3 as size here for the thread quit pipe, control and
829 * data socket.
830 */
831 ret = create_thread_poll_set(&events, 3);
832 if (ret < 0) {
833 goto error_create_poll;
834 }
835
836 /* Add the control socket */
837 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
838 if (ret < 0) {
839 goto error_poll_add;
840 }
841
842 /* Add the data socket */
843 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
844 if (ret < 0) {
845 goto error_poll_add;
846 }
847
848 lttng_relay_notify_ready();
849
850 if (testpoint(relayd_thread_listener)) {
851 goto error_testpoint;
852 }
853
854 while (1) {
855 health_code_update();
856
857 DBG("Listener accepting connections");
858
859restart:
860 health_poll_entry();
861 ret = lttng_poll_wait(&events, -1);
862 health_poll_exit();
863 if (ret < 0) {
864 /*
865 * Restart interrupted system call.
866 */
867 if (errno == EINTR) {
868 goto restart;
869 }
870 goto error;
871 }
872
873 nb_fd = ret;
874
875 DBG("Relay new connection received");
876 for (i = 0; i < nb_fd; i++) {
877 health_code_update();
878
879 /* Fetch once the poll data */
880 revents = LTTNG_POLL_GETEV(&events, i);
881 pollfd = LTTNG_POLL_GETFD(&events, i);
882
883 /* Thread quit pipe has been closed. Killing thread. */
884 ret = check_thread_quit_pipe(pollfd, revents);
885 if (ret) {
886 err = 0;
887 goto exit;
888 }
889
890 if (revents & LPOLLIN) {
891 /*
892 * A new connection is requested, therefore a
893 * sessiond/consumerd connection is allocated in
894 * this thread, enqueued to a global queue and
895 * dequeued (and freed) in the worker thread.
896 */
897 int val = 1;
898 struct relay_connection *new_conn;
899 struct lttcomm_sock *newsock;
900 enum connection_type type;
901
902 if (pollfd == data_sock->fd) {
903 type = RELAY_DATA;
904 newsock = data_sock->ops->accept(data_sock);
905 DBG("Relay data connection accepted, socket %d",
906 newsock->fd);
907 } else {
908 assert(pollfd == control_sock->fd);
909 type = RELAY_CONTROL;
910 newsock = control_sock->ops->accept(control_sock);
911 DBG("Relay control connection accepted, socket %d",
912 newsock->fd);
913 }
914 if (!newsock) {
915 PERROR("accepting sock");
916 goto error;
917 }
918
919 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
920 sizeof(val));
921 if (ret < 0) {
922 PERROR("setsockopt inet");
923 lttcomm_destroy_sock(newsock);
924 goto error;
925 }
926
927 ret = socket_apply_keep_alive_config(newsock->fd);
928 if (ret < 0) {
929 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
930 newsock->fd);
931 lttcomm_destroy_sock(newsock);
932 goto error;
933 }
934
935 new_conn = connection_create(newsock, type);
936 if (!new_conn) {
937 lttcomm_destroy_sock(newsock);
938 goto error;
939 }
940
941 /* Enqueue request for the dispatcher thread. */
942 cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail,
943 &new_conn->qnode);
944
945 /*
946 * Wake the dispatch queue futex.
947 * Implicit memory barrier with the
948 * exchange in cds_wfcq_enqueue.
949 */
950 futex_nto1_wake(&relay_conn_queue.futex);
951 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
952 ERR("socket poll error");
953 goto error;
954 } else {
955 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
956 goto error;
957 }
958 }
959 }
960
961exit:
962error:
963error_poll_add:
964error_testpoint:
965 lttng_poll_clean(&events);
966error_create_poll:
967 if (data_sock->fd >= 0) {
968 ret = data_sock->ops->close(data_sock);
969 if (ret) {
970 PERROR("close");
971 }
972 }
973 lttcomm_destroy_sock(data_sock);
974error_sock_relay:
975 if (control_sock->fd >= 0) {
976 ret = control_sock->ops->close(control_sock);
977 if (ret) {
978 PERROR("close");
979 }
980 }
981 lttcomm_destroy_sock(control_sock);
982error_sock_control:
983 if (err) {
984 health_error();
985 ERR("Health error occurred in %s", __func__);
986 }
987 health_unregister(health_relayd);
988 DBG("Relay listener thread cleanup complete");
989 lttng_relay_stop_threads();
990 return NULL;
991}
992
993/*
994 * This thread manages the dispatching of the requests to worker threads
995 */
996static void *relay_thread_dispatcher(void *data)
997{
998 int err = -1;
999 ssize_t ret;
1000 struct cds_wfcq_node *node;
1001 struct relay_connection *new_conn = NULL;
1002
1003 DBG("[thread] Relay dispatcher started");
1004
1005 health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER);
1006
1007 if (testpoint(relayd_thread_dispatcher)) {
1008 goto error_testpoint;
1009 }
1010
1011 health_code_update();
1012
1013 for (;;) {
1014 health_code_update();
1015
1016 /* Atomically prepare the queue futex */
1017 futex_nto1_prepare(&relay_conn_queue.futex);
1018
1019 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1020 break;
1021 }
1022
1023 do {
1024 health_code_update();
1025
1026 /* Dequeue commands */
1027 node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head,
1028 &relay_conn_queue.tail);
1029 if (node == NULL) {
1030 DBG("Woken up but nothing in the relay command queue");
1031 /* Continue thread execution */
1032 break;
1033 }
1034 new_conn = caa_container_of(node, struct relay_connection, qnode);
1035
1036 DBG("Dispatching request waiting on sock %d", new_conn->sock->fd);
1037
1038 /*
1039 * Inform worker thread of the new request. This
1040 * call is blocking so we can be assured that
1041 * the data will be read at some point in time
1042 * or wait to the end of the world :)
1043 */
1044 ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn));
1045 if (ret < 0) {
1046 PERROR("write connection pipe");
1047 connection_put(new_conn);
1048 goto error;
1049 }
1050 } while (node != NULL);
1051
1052 /* Futex wait on queue. Blocking call on futex() */
1053 health_poll_entry();
1054 futex_nto1_wait(&relay_conn_queue.futex);
1055 health_poll_exit();
1056 }
1057
1058 /* Normal exit, no error */
1059 err = 0;
1060
1061error:
1062error_testpoint:
1063 if (err) {
1064 health_error();
1065 ERR("Health error occurred in %s", __func__);
1066 }
1067 health_unregister(health_relayd);
1068 DBG("Dispatch thread dying");
1069 lttng_relay_stop_threads();
1070 return NULL;
1071}
1072
1073static bool session_streams_have_index(const struct relay_session *session)
1074{
1075 return session->minor >= 4 && !session->snapshot;
1076}
1077
1078/*
1079 * Handle the RELAYD_CREATE_SESSION command.
1080 *
1081 * On success, send back the session id or else return a negative value.
1082 */
1083static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
1084 struct relay_connection *conn,
1085 const struct lttng_buffer_view *payload)
1086{
1087 int ret = 0;
1088 ssize_t send_ret;
1089 struct relay_session *session = NULL;
1090 struct lttcomm_relayd_create_session_reply_2_11 reply = {};
1091 char session_name[LTTNG_NAME_MAX] = {};
1092 char hostname[LTTNG_HOST_NAME_MAX] = {};
1093 uint32_t live_timer = 0;
1094 bool snapshot = false;
1095 bool session_name_contains_creation_timestamp = false;
1096 /* Left nil for peers < 2.11. */
1097 char base_path[LTTNG_PATH_MAX] = {};
1098 lttng_uuid sessiond_uuid = {};
1099 LTTNG_OPTIONAL(uint64_t) id_sessiond = {};
1100 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
1101 LTTNG_OPTIONAL(time_t) creation_time = {};
1102 struct lttng_dynamic_buffer reply_payload;
1103
1104 lttng_dynamic_buffer_init(&reply_payload);
1105
1106 if (conn->minor < 4) {
1107 /* From 2.1 to 2.3 */
1108 ret = 0;
1109 } else if (conn->minor >= 4 && conn->minor < 11) {
1110 /* From 2.4 to 2.10 */
1111 ret = cmd_create_session_2_4(payload, session_name,
1112 hostname, &live_timer, &snapshot);
1113 } else {
1114 bool has_current_chunk;
1115 uint64_t current_chunk_id_value;
1116 time_t creation_time_value;
1117 uint64_t id_sessiond_value;
1118
1119 /* From 2.11 to ... */
1120 ret = cmd_create_session_2_11(payload, session_name, hostname,
1121 base_path, &live_timer, &snapshot, &id_sessiond_value,
1122 sessiond_uuid, &has_current_chunk,
1123 &current_chunk_id_value, &creation_time_value,
1124 &session_name_contains_creation_timestamp);
1125 if (lttng_uuid_is_nil(sessiond_uuid)) {
1126 /* The nil UUID is reserved for pre-2.11 clients. */
1127 ERR("Illegal nil UUID announced by peer in create session command");
1128 ret = -1;
1129 goto send_reply;
1130 }
1131 LTTNG_OPTIONAL_SET(&id_sessiond, id_sessiond_value);
1132 LTTNG_OPTIONAL_SET(&creation_time, creation_time_value);
1133 if (has_current_chunk) {
1134 LTTNG_OPTIONAL_SET(&current_chunk_id,
1135 current_chunk_id_value);
1136 }
1137 }
1138
1139 if (ret < 0) {
1140 goto send_reply;
1141 }
1142
1143 session = session_create(session_name, hostname, base_path, live_timer,
1144 snapshot, sessiond_uuid,
1145 id_sessiond.is_set ? &id_sessiond.value : NULL,
1146 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1147 creation_time.is_set ? &creation_time.value : NULL,
1148 conn->major, conn->minor,
1149 session_name_contains_creation_timestamp);
1150 if (!session) {
1151 ret = -1;
1152 goto send_reply;
1153 }
1154 assert(!conn->session);
1155 conn->session = session;
1156 DBG("Created session %" PRIu64, session->id);
1157
1158 reply.generic.session_id = htobe64(session->id);
1159
1160send_reply:
1161 if (ret < 0) {
1162 reply.generic.ret_code = htobe32(LTTNG_ERR_FATAL);
1163 } else {
1164 reply.generic.ret_code = htobe32(LTTNG_OK);
1165 }
1166
1167 if (conn->minor < 11) {
1168 /* From 2.1 to 2.10 */
1169 ret = lttng_dynamic_buffer_append(&reply_payload,
1170 &reply.generic, sizeof(reply.generic));
1171 if (ret) {
1172 ERR("Failed to append \"create session\" command reply header to payload buffer");
1173 ret = -1;
1174 goto end;
1175 }
1176 } else {
1177 const uint32_t output_path_length =
1178 session ? strlen(session->output_path) + 1 : 0;
1179
1180 reply.output_path_length = htobe32(output_path_length);
1181 ret = lttng_dynamic_buffer_append(
1182 &reply_payload, &reply, sizeof(reply));
1183 if (ret) {
1184 ERR("Failed to append \"create session\" command reply header to payload buffer");
1185 goto end;
1186 }
1187
1188 if (output_path_length) {
1189 ret = lttng_dynamic_buffer_append(&reply_payload,
1190 session->output_path,
1191 output_path_length);
1192 if (ret) {
1193 ERR("Failed to append \"create session\" command reply path to payload buffer");
1194 goto end;
1195 }
1196 }
1197 }
1198
1199 send_ret = conn->sock->ops->sendmsg(conn->sock, reply_payload.data,
1200 reply_payload.size, 0);
1201 if (send_ret < (ssize_t) reply_payload.size) {
1202 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1203 reply_payload.size, send_ret);
1204 ret = -1;
1205 }
1206end:
1207 if (ret < 0 && session) {
1208 session_put(session);
1209 }
1210 lttng_dynamic_buffer_reset(&reply_payload);
1211 return ret;
1212}
1213
1214/*
1215 * When we have received all the streams and the metadata for a channel,
1216 * we make them visible to the viewer threads.
1217 */
1218static void publish_connection_local_streams(struct relay_connection *conn)
1219{
1220 struct relay_stream *stream;
1221 struct relay_session *session = conn->session;
1222
1223 /*
1224 * We publish all streams belonging to a session atomically wrt
1225 * session lock.
1226 */
1227 pthread_mutex_lock(&session->lock);
1228 rcu_read_lock();
1229 cds_list_for_each_entry_rcu(stream, &session->recv_list,
1230 recv_node) {
1231 stream_publish(stream);
1232 }
1233 rcu_read_unlock();
1234
1235 /*
1236 * Inform the viewer that there are new streams in the session.
1237 */
1238 if (session->viewer_attached) {
1239 uatomic_set(&session->new_streams, 1);
1240 }
1241 pthread_mutex_unlock(&session->lock);
1242}
1243
1244static int conform_channel_path(char *channel_path)
1245{
1246 int ret = 0;
1247
1248 if (strstr("../", channel_path)) {
1249 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1250 channel_path);
1251 ret = -1;
1252 goto end;
1253 }
1254
1255 if (*channel_path == '/') {
1256 const size_t len = strlen(channel_path);
1257
1258 /*
1259 * Channel paths from peers prior to 2.11 are expressed as an
1260 * absolute path that is, in reality, relative to the relay
1261 * daemon's output directory. Remove the leading slash so it
1262 * is correctly interpreted as a relative path later on.
1263 *
1264 * len (and not len - 1) is used to copy the trailing NULL.
1265 */
1266 bcopy(channel_path + 1, channel_path, len);
1267 }
1268end:
1269 return ret;
1270}
1271
1272/*
1273 * relay_add_stream: allocate a new stream for a session
1274 */
1275static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr,
1276 struct relay_connection *conn,
1277 const struct lttng_buffer_view *payload)
1278{
1279 int ret;
1280 ssize_t send_ret;
1281 struct relay_session *session = conn->session;
1282 struct relay_stream *stream = NULL;
1283 struct lttcomm_relayd_status_stream reply;
1284 struct ctf_trace *trace = NULL;
1285 uint64_t stream_handle = -1ULL;
1286 char *path_name = NULL, *channel_name = NULL;
1287 uint64_t tracefile_size = 0, tracefile_count = 0;
1288 LTTNG_OPTIONAL(uint64_t) stream_chunk_id = {};
1289
1290 if (!session || !conn->version_check_done) {
1291 ERR("Trying to add a stream before version check");
1292 ret = -1;
1293 goto end_no_session;
1294 }
1295
1296 if (session->minor == 1) {
1297 /* For 2.1 */
1298 ret = cmd_recv_stream_2_1(payload, &path_name,
1299 &channel_name);
1300 } else if (session->minor > 1 && session->minor < 11) {
1301 /* From 2.2 to 2.10 */
1302 ret = cmd_recv_stream_2_2(payload, &path_name,
1303 &channel_name, &tracefile_size, &tracefile_count);
1304 } else {
1305 /* From 2.11 to ... */
1306 ret = cmd_recv_stream_2_11(payload, &path_name,
1307 &channel_name, &tracefile_size, &tracefile_count,
1308 &stream_chunk_id.value);
1309 stream_chunk_id.is_set = true;
1310 }
1311
1312 if (ret < 0) {
1313 goto send_reply;
1314 }
1315
1316 if (conform_channel_path(path_name)) {
1317 goto send_reply;
1318 }
1319
1320 trace = ctf_trace_get_by_path_or_create(session, path_name);
1321 if (!trace) {
1322 goto send_reply;
1323 }
1324 /* This stream here has one reference on the trace. */
1325
1326 pthread_mutex_lock(&last_relay_stream_id_lock);
1327 stream_handle = ++last_relay_stream_id;
1328 pthread_mutex_unlock(&last_relay_stream_id_lock);
1329
1330 /* We pass ownership of path_name and channel_name. */
1331 stream = stream_create(trace, stream_handle, path_name,
1332 channel_name, tracefile_size, tracefile_count);
1333 path_name = NULL;
1334 channel_name = NULL;
1335
1336 /*
1337 * Streams are the owners of their trace. Reference to trace is
1338 * kept within stream_create().
1339 */
1340 ctf_trace_put(trace);
1341
1342send_reply:
1343 memset(&reply, 0, sizeof(reply));
1344 reply.handle = htobe64(stream_handle);
1345 if (!stream) {
1346 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1347 } else {
1348 reply.ret_code = htobe32(LTTNG_OK);
1349 }
1350
1351 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1352 sizeof(struct lttcomm_relayd_status_stream), 0);
1353 if (send_ret < (ssize_t) sizeof(reply)) {
1354 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1355 send_ret);
1356 ret = -1;
1357 }
1358
1359end_no_session:
1360 free(path_name);
1361 free(channel_name);
1362 return ret;
1363}
1364
1365/*
1366 * relay_close_stream: close a specific stream
1367 */
1368static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr,
1369 struct relay_connection *conn,
1370 const struct lttng_buffer_view *payload)
1371{
1372 int ret;
1373 ssize_t send_ret;
1374 struct relay_session *session = conn->session;
1375 struct lttcomm_relayd_close_stream stream_info;
1376 struct lttcomm_relayd_generic_reply reply;
1377 struct relay_stream *stream;
1378
1379 DBG("Close stream received");
1380
1381 if (!session || !conn->version_check_done) {
1382 ERR("Trying to close a stream before version check");
1383 ret = -1;
1384 goto end_no_session;
1385 }
1386
1387 if (payload->size < sizeof(stream_info)) {
1388 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1389 sizeof(stream_info), payload->size);
1390 ret = -1;
1391 goto end_no_session;
1392 }
1393 memcpy(&stream_info, payload->data, sizeof(stream_info));
1394 stream_info.stream_id = be64toh(stream_info.stream_id);
1395 stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num);
1396
1397 stream = stream_get_by_id(stream_info.stream_id);
1398 if (!stream) {
1399 ret = -1;
1400 goto end;
1401 }
1402
1403 /*
1404 * Set last_net_seq_num before the close flag. Required by data
1405 * pending check.
1406 */
1407 pthread_mutex_lock(&stream->lock);
1408 stream->last_net_seq_num = stream_info.last_net_seq_num;
1409 pthread_mutex_unlock(&stream->lock);
1410
1411 /*
1412 * This is one of the conditions which may trigger a stream close
1413 * with the others being:
1414 * 1) A close command is received for a stream
1415 * 2) The control connection owning the stream is closed
1416 * 3) We have received all of the stream's data _after_ a close
1417 * request.
1418 */
1419 try_stream_close(stream);
1420 if (stream->is_metadata) {
1421 struct relay_viewer_stream *vstream;
1422
1423 vstream = viewer_stream_get_by_id(stream->stream_handle);
1424 if (vstream) {
1425 if (vstream->metadata_sent == stream->metadata_received) {
1426 /*
1427 * Since all the metadata has been sent to the
1428 * viewer and that we have a request to close
1429 * its stream, we can safely teardown the
1430 * corresponding metadata viewer stream.
1431 */
1432 viewer_stream_put(vstream);
1433 }
1434 /* Put local reference. */
1435 viewer_stream_put(vstream);
1436 }
1437 }
1438 stream_put(stream);
1439 ret = 0;
1440
1441end:
1442 memset(&reply, 0, sizeof(reply));
1443 if (ret < 0) {
1444 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1445 } else {
1446 reply.ret_code = htobe32(LTTNG_OK);
1447 }
1448 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1449 sizeof(struct lttcomm_relayd_generic_reply), 0);
1450 if (send_ret < (ssize_t) sizeof(reply)) {
1451 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1452 send_ret);
1453 ret = -1;
1454 }
1455
1456end_no_session:
1457 return ret;
1458}
1459
1460/*
1461 * relay_reset_metadata: reset a metadata stream
1462 */
1463static
1464int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr,
1465 struct relay_connection *conn,
1466 const struct lttng_buffer_view *payload)
1467{
1468 int ret;
1469 ssize_t send_ret;
1470 struct relay_session *session = conn->session;
1471 struct lttcomm_relayd_reset_metadata stream_info;
1472 struct lttcomm_relayd_generic_reply reply;
1473 struct relay_stream *stream;
1474
1475 DBG("Reset metadata received");
1476
1477 if (!session || !conn->version_check_done) {
1478 ERR("Trying to reset a metadata stream before version check");
1479 ret = -1;
1480 goto end_no_session;
1481 }
1482
1483 if (payload->size < sizeof(stream_info)) {
1484 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1485 sizeof(stream_info), payload->size);
1486 ret = -1;
1487 goto end_no_session;
1488 }
1489 memcpy(&stream_info, payload->data, sizeof(stream_info));
1490 stream_info.stream_id = be64toh(stream_info.stream_id);
1491 stream_info.version = be64toh(stream_info.version);
1492
1493 DBG("Update metadata to version %" PRIu64, stream_info.version);
1494
1495 /* Unsupported for live sessions for now. */
1496 if (session->live_timer != 0) {
1497 ret = -1;
1498 goto end;
1499 }
1500
1501 stream = stream_get_by_id(stream_info.stream_id);
1502 if (!stream) {
1503 ret = -1;
1504 goto end;
1505 }
1506 pthread_mutex_lock(&stream->lock);
1507 if (!stream->is_metadata) {
1508 ret = -1;
1509 goto end_unlock;
1510 }
1511
1512 ret = stream_reset_file(stream);
1513 if (ret < 0) {
1514 ERR("Failed to reset metadata stream %" PRIu64
1515 ": stream_path = %s, channel = %s",
1516 stream->stream_handle, stream->path_name,
1517 stream->channel_name);
1518 goto end_unlock;
1519 }
1520end_unlock:
1521 pthread_mutex_unlock(&stream->lock);
1522 stream_put(stream);
1523
1524end:
1525 memset(&reply, 0, sizeof(reply));
1526 if (ret < 0) {
1527 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1528 } else {
1529 reply.ret_code = htobe32(LTTNG_OK);
1530 }
1531 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1532 sizeof(struct lttcomm_relayd_generic_reply), 0);
1533 if (send_ret < (ssize_t) sizeof(reply)) {
1534 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1535 send_ret);
1536 ret = -1;
1537 }
1538
1539end_no_session:
1540 return ret;
1541}
1542
1543/*
1544 * relay_unknown_command: send -1 if received unknown command
1545 */
1546static void relay_unknown_command(struct relay_connection *conn)
1547{
1548 struct lttcomm_relayd_generic_reply reply;
1549 ssize_t send_ret;
1550
1551 memset(&reply, 0, sizeof(reply));
1552 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1553 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1554 if (send_ret < sizeof(reply)) {
1555 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret);
1556 }
1557}
1558
1559/*
1560 * relay_start: send an acknowledgment to the client to tell if we are
1561 * ready to receive data. We are ready if a session is established.
1562 */
1563static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr,
1564 struct relay_connection *conn,
1565 const struct lttng_buffer_view *payload)
1566{
1567 int ret = 0;
1568 ssize_t send_ret;
1569 struct lttcomm_relayd_generic_reply reply;
1570 struct relay_session *session = conn->session;
1571
1572 if (!session) {
1573 DBG("Trying to start the streaming without a session established");
1574 ret = htobe32(LTTNG_ERR_UNK);
1575 }
1576
1577 memset(&reply, 0, sizeof(reply));
1578 reply.ret_code = htobe32(LTTNG_OK);
1579 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1580 sizeof(reply), 0);
1581 if (send_ret < (ssize_t) sizeof(reply)) {
1582 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1583 send_ret);
1584 ret = -1;
1585 }
1586
1587 return ret;
1588}
1589
1590/*
1591 * relay_recv_metadata: receive the metadata for the session.
1592 */
1593static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr,
1594 struct relay_connection *conn,
1595 const struct lttng_buffer_view *payload)
1596{
1597 int ret = 0;
1598 struct relay_session *session = conn->session;
1599 struct lttcomm_relayd_metadata_payload metadata_payload_header;
1600 struct relay_stream *metadata_stream;
1601 uint64_t metadata_payload_size;
1602 struct lttng_buffer_view packet_view;
1603
1604 if (!session) {
1605 ERR("Metadata sent before version check");
1606 ret = -1;
1607 goto end;
1608 }
1609
1610 if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1611 ERR("Incorrect data size");
1612 ret = -1;
1613 goto end;
1614 }
1615 metadata_payload_size = recv_hdr->data_size -
1616 sizeof(struct lttcomm_relayd_metadata_payload);
1617
1618 memcpy(&metadata_payload_header, payload->data,
1619 sizeof(metadata_payload_header));
1620 metadata_payload_header.stream_id = be64toh(
1621 metadata_payload_header.stream_id);
1622 metadata_payload_header.padding_size = be32toh(
1623 metadata_payload_header.padding_size);
1624
1625 metadata_stream = stream_get_by_id(metadata_payload_header.stream_id);
1626 if (!metadata_stream) {
1627 ret = -1;
1628 goto end;
1629 }
1630
1631 packet_view = lttng_buffer_view_from_view(payload,
1632 sizeof(metadata_payload_header), metadata_payload_size);
1633 if (!packet_view.data) {
1634 ERR("Invalid metadata packet length announced by header");
1635 ret = -1;
1636 goto end_put;
1637 }
1638
1639 pthread_mutex_lock(&metadata_stream->lock);
1640 ret = stream_write(metadata_stream, &packet_view,
1641 metadata_payload_header.padding_size);
1642 pthread_mutex_unlock(&metadata_stream->lock);
1643 if (ret){
1644 ret = -1;
1645 goto end_put;
1646 }
1647end_put:
1648 stream_put(metadata_stream);
1649end:
1650 return ret;
1651}
1652
1653/*
1654 * relay_send_version: send relayd version number
1655 */
1656static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr,
1657 struct relay_connection *conn,
1658 const struct lttng_buffer_view *payload)
1659{
1660 int ret;
1661 ssize_t send_ret;
1662 struct lttcomm_relayd_version reply, msg;
1663 bool compatible = true;
1664
1665 conn->version_check_done = true;
1666
1667 /* Get version from the other side. */
1668 if (payload->size < sizeof(msg)) {
1669 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1670 sizeof(msg), payload->size);
1671 ret = -1;
1672 goto end;
1673 }
1674
1675 memcpy(&msg, payload->data, sizeof(msg));
1676 msg.major = be32toh(msg.major);
1677 msg.minor = be32toh(msg.minor);
1678
1679 memset(&reply, 0, sizeof(reply));
1680 reply.major = RELAYD_VERSION_COMM_MAJOR;
1681 reply.minor = RELAYD_VERSION_COMM_MINOR;
1682
1683 /* Major versions must be the same */
1684 if (reply.major != msg.major) {
1685 DBG("Incompatible major versions (%u vs %u), deleting session",
1686 reply.major, msg.major);
1687 compatible = false;
1688 }
1689
1690 conn->major = reply.major;
1691 /* We adapt to the lowest compatible version */
1692 if (reply.minor <= msg.minor) {
1693 conn->minor = reply.minor;
1694 } else {
1695 conn->minor = msg.minor;
1696 }
1697
1698 reply.major = htobe32(reply.major);
1699 reply.minor = htobe32(reply.minor);
1700 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1701 sizeof(reply), 0);
1702 if (send_ret < (ssize_t) sizeof(reply)) {
1703 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1704 send_ret);
1705 ret = -1;
1706 goto end;
1707 } else {
1708 ret = 0;
1709 }
1710
1711 if (!compatible) {
1712 ret = -1;
1713 goto end;
1714 }
1715
1716 DBG("Version check done using protocol %u.%u", conn->major,
1717 conn->minor);
1718
1719end:
1720 return ret;
1721}
1722
1723/*
1724 * Check for data pending for a given stream id from the session daemon.
1725 */
1726static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
1727 struct relay_connection *conn,
1728 const struct lttng_buffer_view *payload)
1729{
1730 struct relay_session *session = conn->session;
1731 struct lttcomm_relayd_data_pending msg;
1732 struct lttcomm_relayd_generic_reply reply;
1733 struct relay_stream *stream;
1734 ssize_t send_ret;
1735 int ret;
1736 uint64_t stream_seq;
1737
1738 DBG("Data pending command received");
1739
1740 if (!session || !conn->version_check_done) {
1741 ERR("Trying to check for data before version check");
1742 ret = -1;
1743 goto end_no_session;
1744 }
1745
1746 if (payload->size < sizeof(msg)) {
1747 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1748 sizeof(msg), payload->size);
1749 ret = -1;
1750 goto end_no_session;
1751 }
1752 memcpy(&msg, payload->data, sizeof(msg));
1753 msg.stream_id = be64toh(msg.stream_id);
1754 msg.last_net_seq_num = be64toh(msg.last_net_seq_num);
1755
1756 stream = stream_get_by_id(msg.stream_id);
1757 if (stream == NULL) {
1758 ret = -1;
1759 goto end;
1760 }
1761
1762 pthread_mutex_lock(&stream->lock);
1763
1764 if (session_streams_have_index(session)) {
1765 /*
1766 * Ensure that both the index and stream data have been
1767 * flushed up to the requested point.
1768 */
1769 stream_seq = min(stream->prev_data_seq, stream->prev_index_seq);
1770 } else {
1771 stream_seq = stream->prev_data_seq;
1772 }
1773 DBG("Data pending for stream id %" PRIu64 ": prev_data_seq %" PRIu64
1774 ", prev_index_seq %" PRIu64
1775 ", and last_seq %" PRIu64, msg.stream_id,
1776 stream->prev_data_seq, stream->prev_index_seq,
1777 msg.last_net_seq_num);
1778
1779 /* Avoid wrapping issue */
1780 if (((int64_t) (stream_seq - msg.last_net_seq_num)) >= 0) {
1781 /* Data has in fact been written and is NOT pending */
1782 ret = 0;
1783 } else {
1784 /* Data still being streamed thus pending */
1785 ret = 1;
1786 }
1787
1788 stream->data_pending_check_done = true;
1789 pthread_mutex_unlock(&stream->lock);
1790
1791 stream_put(stream);
1792end:
1793
1794 memset(&reply, 0, sizeof(reply));
1795 reply.ret_code = htobe32(ret);
1796 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1797 if (send_ret < (ssize_t) sizeof(reply)) {
1798 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1799 send_ret);
1800 ret = -1;
1801 }
1802
1803end_no_session:
1804 return ret;
1805}
1806
1807/*
1808 * Wait for the control socket to reach a quiescent state.
1809 *
1810 * Note that for now, when receiving this command from the session
1811 * daemon, this means that every subsequent commands or data received on
1812 * the control socket has been handled. So, this is why we simply return
1813 * OK here.
1814 */
1815static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr,
1816 struct relay_connection *conn,
1817 const struct lttng_buffer_view *payload)
1818{
1819 int ret;
1820 ssize_t send_ret;
1821 struct relay_stream *stream;
1822 struct lttcomm_relayd_quiescent_control msg;
1823 struct lttcomm_relayd_generic_reply reply;
1824
1825 DBG("Checking quiescent state on control socket");
1826
1827 if (!conn->session || !conn->version_check_done) {
1828 ERR("Trying to check for data before version check");
1829 ret = -1;
1830 goto end_no_session;
1831 }
1832
1833 if (payload->size < sizeof(msg)) {
1834 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
1835 sizeof(msg), payload->size);
1836 ret = -1;
1837 goto end_no_session;
1838 }
1839 memcpy(&msg, payload->data, sizeof(msg));
1840 msg.stream_id = be64toh(msg.stream_id);
1841
1842 stream = stream_get_by_id(msg.stream_id);
1843 if (!stream) {
1844 goto reply;
1845 }
1846 pthread_mutex_lock(&stream->lock);
1847 stream->data_pending_check_done = true;
1848 pthread_mutex_unlock(&stream->lock);
1849
1850 DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id);
1851 stream_put(stream);
1852reply:
1853 memset(&reply, 0, sizeof(reply));
1854 reply.ret_code = htobe32(LTTNG_OK);
1855 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1856 if (send_ret < (ssize_t) sizeof(reply)) {
1857 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
1858 send_ret);
1859 ret = -1;
1860 } else {
1861 ret = 0;
1862 }
1863
1864end_no_session:
1865 return ret;
1866}
1867
1868/*
1869 * Initialize a data pending command. This means that a consumer is about
1870 * to ask for data pending for each stream it holds. Simply iterate over
1871 * all streams of a session and set the data_pending_check_done flag.
1872 *
1873 * This command returns to the client a LTTNG_OK code.
1874 */
1875static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
1876 struct relay_connection *conn,
1877 const struct lttng_buffer_view *payload)
1878{
1879 int ret;
1880 ssize_t send_ret;
1881 struct lttng_ht_iter iter;
1882 struct lttcomm_relayd_begin_data_pending msg;
1883 struct lttcomm_relayd_generic_reply reply;
1884 struct relay_stream *stream;
1885
1886 assert(recv_hdr);
1887 assert(conn);
1888
1889 DBG("Init streams for data pending");
1890
1891 if (!conn->session || !conn->version_check_done) {
1892 ERR("Trying to check for data before version check");
1893 ret = -1;
1894 goto end_no_session;
1895 }
1896
1897 if (payload->size < sizeof(msg)) {
1898 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
1899 sizeof(msg), payload->size);
1900 ret = -1;
1901 goto end_no_session;
1902 }
1903 memcpy(&msg, payload->data, sizeof(msg));
1904 msg.session_id = be64toh(msg.session_id);
1905
1906 /*
1907 * Iterate over all streams to set the begin data pending flag.
1908 * For now, the streams are indexed by stream handle so we have
1909 * to iterate over all streams to find the one associated with
1910 * the right session_id.
1911 */
1912 rcu_read_lock();
1913 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1914 node.node) {
1915 if (!stream_get(stream)) {
1916 continue;
1917 }
1918 if (stream->trace->session->id == msg.session_id) {
1919 pthread_mutex_lock(&stream->lock);
1920 stream->data_pending_check_done = false;
1921 pthread_mutex_unlock(&stream->lock);
1922 DBG("Set begin data pending flag to stream %" PRIu64,
1923 stream->stream_handle);
1924 }
1925 stream_put(stream);
1926 }
1927 rcu_read_unlock();
1928
1929 memset(&reply, 0, sizeof(reply));
1930 /* All good, send back reply. */
1931 reply.ret_code = htobe32(LTTNG_OK);
1932
1933 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1934 if (send_ret < (ssize_t) sizeof(reply)) {
1935 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
1936 send_ret);
1937 ret = -1;
1938 } else {
1939 ret = 0;
1940 }
1941
1942end_no_session:
1943 return ret;
1944}
1945
1946/*
1947 * End data pending command. This will check, for a given session id, if
1948 * each stream associated with it has its data_pending_check_done flag
1949 * set. If not, this means that the client lost track of the stream but
1950 * the data is still being streamed on our side. In this case, we inform
1951 * the client that data is in flight.
1952 *
1953 * Return to the client if there is data in flight or not with a ret_code.
1954 */
1955static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
1956 struct relay_connection *conn,
1957 const struct lttng_buffer_view *payload)
1958{
1959 int ret;
1960 ssize_t send_ret;
1961 struct lttng_ht_iter iter;
1962 struct lttcomm_relayd_end_data_pending msg;
1963 struct lttcomm_relayd_generic_reply reply;
1964 struct relay_stream *stream;
1965 uint32_t is_data_inflight = 0;
1966
1967 DBG("End data pending command");
1968
1969 if (!conn->session || !conn->version_check_done) {
1970 ERR("Trying to check for data before version check");
1971 ret = -1;
1972 goto end_no_session;
1973 }
1974
1975 if (payload->size < sizeof(msg)) {
1976 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
1977 sizeof(msg), payload->size);
1978 ret = -1;
1979 goto end_no_session;
1980 }
1981 memcpy(&msg, payload->data, sizeof(msg));
1982 msg.session_id = be64toh(msg.session_id);
1983
1984 /*
1985 * Iterate over all streams to see if the begin data pending
1986 * flag is set.
1987 */
1988 rcu_read_lock();
1989 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1990 node.node) {
1991 if (!stream_get(stream)) {
1992 continue;
1993 }
1994 if (stream->trace->session->id != msg.session_id) {
1995 stream_put(stream);
1996 continue;
1997 }
1998 pthread_mutex_lock(&stream->lock);
1999 if (!stream->data_pending_check_done) {
2000 uint64_t stream_seq;
2001
2002 if (session_streams_have_index(conn->session)) {
2003 /*
2004 * Ensure that both the index and stream data have been
2005 * flushed up to the requested point.
2006 */
2007 stream_seq = min(stream->prev_data_seq, stream->prev_index_seq);
2008 } else {
2009 stream_seq = stream->prev_data_seq;
2010 }
2011 if (!stream->closed || !(((int64_t) (stream_seq - stream->last_net_seq_num)) >= 0)) {
2012 is_data_inflight = 1;
2013 DBG("Data is still in flight for stream %" PRIu64,
2014 stream->stream_handle);
2015 pthread_mutex_unlock(&stream->lock);
2016 stream_put(stream);
2017 break;
2018 }
2019 }
2020 pthread_mutex_unlock(&stream->lock);
2021 stream_put(stream);
2022 }
2023 rcu_read_unlock();
2024
2025 memset(&reply, 0, sizeof(reply));
2026 /* All good, send back reply. */
2027 reply.ret_code = htobe32(is_data_inflight);
2028
2029 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2030 if (send_ret < (ssize_t) sizeof(reply)) {
2031 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2032 send_ret);
2033 ret = -1;
2034 } else {
2035 ret = 0;
2036 }
2037
2038end_no_session:
2039 return ret;
2040}
2041
2042/*
2043 * Receive an index for a specific stream.
2044 *
2045 * Return 0 on success else a negative value.
2046 */
2047static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr,
2048 struct relay_connection *conn,
2049 const struct lttng_buffer_view *payload)
2050{
2051 int ret;
2052 ssize_t send_ret;
2053 struct relay_session *session = conn->session;
2054 struct lttcomm_relayd_index index_info;
2055 struct lttcomm_relayd_generic_reply reply;
2056 struct relay_stream *stream;
2057 size_t msg_len;
2058
2059 assert(conn);
2060
2061 DBG("Relay receiving index");
2062
2063 if (!session || !conn->version_check_done) {
2064 ERR("Trying to close a stream before version check");
2065 ret = -1;
2066 goto end_no_session;
2067 }
2068
2069 msg_len = lttcomm_relayd_index_len(
2070 lttng_to_index_major(conn->major, conn->minor),
2071 lttng_to_index_minor(conn->major, conn->minor));
2072 if (payload->size < msg_len) {
2073 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2074 msg_len, payload->size);
2075 ret = -1;
2076 goto end_no_session;
2077 }
2078 memcpy(&index_info, payload->data, msg_len);
2079 index_info.relay_stream_id = be64toh(index_info.relay_stream_id);
2080 index_info.net_seq_num = be64toh(index_info.net_seq_num);
2081 index_info.packet_size = be64toh(index_info.packet_size);
2082 index_info.content_size = be64toh(index_info.content_size);
2083 index_info.timestamp_begin = be64toh(index_info.timestamp_begin);
2084 index_info.timestamp_end = be64toh(index_info.timestamp_end);
2085 index_info.events_discarded = be64toh(index_info.events_discarded);
2086 index_info.stream_id = be64toh(index_info.stream_id);
2087
2088 if (conn->minor >= 8) {
2089 index_info.stream_instance_id =
2090 be64toh(index_info.stream_instance_id);
2091 index_info.packet_seq_num = be64toh(index_info.packet_seq_num);
2092 }
2093
2094 stream = stream_get_by_id(index_info.relay_stream_id);
2095 if (!stream) {
2096 ERR("stream_get_by_id not found");
2097 ret = -1;
2098 goto end;
2099 }
2100
2101 pthread_mutex_lock(&stream->lock);
2102 ret = stream_add_index(stream, &index_info);
2103 pthread_mutex_unlock(&stream->lock);
2104 if (ret) {
2105 goto end_stream_put;
2106 }
2107
2108end_stream_put:
2109 stream_put(stream);
2110end:
2111 memset(&reply, 0, sizeof(reply));
2112 if (ret < 0) {
2113 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2114 } else {
2115 reply.ret_code = htobe32(LTTNG_OK);
2116 }
2117 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2118 if (send_ret < (ssize_t) sizeof(reply)) {
2119 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret);
2120 ret = -1;
2121 }
2122
2123end_no_session:
2124 return ret;
2125}
2126
2127/*
2128 * Receive the streams_sent message.
2129 *
2130 * Return 0 on success else a negative value.
2131 */
2132static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr,
2133 struct relay_connection *conn,
2134 const struct lttng_buffer_view *payload)
2135{
2136 int ret;
2137 ssize_t send_ret;
2138 struct lttcomm_relayd_generic_reply reply;
2139
2140 assert(conn);
2141
2142 DBG("Relay receiving streams_sent");
2143
2144 if (!conn->session || !conn->version_check_done) {
2145 ERR("Trying to close a stream before version check");
2146 ret = -1;
2147 goto end_no_session;
2148 }
2149
2150 /*
2151 * Publish every pending stream in the connection recv list which are
2152 * now ready to be used by the viewer.
2153 */
2154 publish_connection_local_streams(conn);
2155
2156 memset(&reply, 0, sizeof(reply));
2157 reply.ret_code = htobe32(LTTNG_OK);
2158 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2159 if (send_ret < (ssize_t) sizeof(reply)) {
2160 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2161 send_ret);
2162 ret = -1;
2163 } else {
2164 /* Success. */
2165 ret = 0;
2166 }
2167
2168end_no_session:
2169 return ret;
2170}
2171
2172/*
2173 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2174 * session rotation feature (not the tracefile rotation feature).
2175 */
2176static int relay_rotate_session_streams(
2177 const struct lttcomm_relayd_hdr *recv_hdr,
2178 struct relay_connection *conn,
2179 const struct lttng_buffer_view *payload)
2180{
2181 int ret = 0;
2182 uint32_t i;
2183 ssize_t send_ret;
2184 enum lttng_error_code reply_code = LTTNG_ERR_UNK;
2185 struct relay_session *session = conn->session;
2186 struct lttcomm_relayd_rotate_streams rotate_streams;
2187 struct lttcomm_relayd_generic_reply reply = {};
2188 struct relay_stream *stream = NULL;
2189 const size_t header_len = sizeof(struct lttcomm_relayd_rotate_streams);
2190 struct lttng_trace_chunk *next_trace_chunk = NULL;
2191 struct lttng_buffer_view stream_positions;
2192 char chunk_id_buf[MAX_INT_DEC_LEN(uint64_t)];
2193 const char *chunk_id_str = "none";
2194
2195 if (!session || !conn->version_check_done) {
2196 ERR("Trying to rotate a stream before version check");
2197 ret = -1;
2198 goto end_no_reply;
2199 }
2200
2201 if (session->major == 2 && session->minor < 11) {
2202 ERR("Unsupported feature before 2.11");
2203 ret = -1;
2204 goto end_no_reply;
2205 }
2206
2207 if (payload->size < header_len) {
2208 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2209 header_len, payload->size);
2210 ret = -1;
2211 goto end_no_reply;
2212 }
2213
2214 memcpy(&rotate_streams, payload->data, header_len);
2215
2216 /* Convert header to host endianness. */
2217 rotate_streams = (typeof(rotate_streams)) {
2218 .stream_count = be32toh(rotate_streams.stream_count),
2219 .new_chunk_id = (typeof(rotate_streams.new_chunk_id)) {
2220 .is_set = !!rotate_streams.new_chunk_id.is_set,
2221 .value = be64toh(rotate_streams.new_chunk_id.value),
2222 }
2223 };
2224
2225 if (rotate_streams.new_chunk_id.is_set) {
2226 /*
2227 * Retrieve the trace chunk the stream must transition to. As
2228 * per the protocol, this chunk should have been created
2229 * before this command is received.
2230 */
2231 next_trace_chunk = sessiond_trace_chunk_registry_get_chunk(
2232 sessiond_trace_chunk_registry,
2233 session->sessiond_uuid, session->id,
2234 rotate_streams.new_chunk_id.value);
2235 if (!next_trace_chunk) {
2236 char uuid_str[UUID_STR_LEN];
2237
2238 lttng_uuid_to_str(session->sessiond_uuid, uuid_str);
2239 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2240 ", trace_chunk_id = %" PRIu64,
2241 uuid_str, session->id,
2242 rotate_streams.new_chunk_id.value);
2243 reply_code = LTTNG_ERR_INVALID_PROTOCOL;
2244 ret = -1;
2245 goto end;
2246 }
2247
2248 ret = snprintf(chunk_id_buf, sizeof(chunk_id_buf), "%" PRIu64,
2249 rotate_streams.new_chunk_id.value);
2250 if (ret < 0 || ret >= sizeof(chunk_id_buf)) {
2251 chunk_id_str = "formatting error";
2252 } else {
2253 chunk_id_str = chunk_id_buf;
2254 }
2255 session->has_rotated = true;
2256 }
2257
2258 DBG("Rotate %" PRIu32 " streams of session \"%s\" to chunk \"%s\"",
2259 rotate_streams.stream_count, session->session_name,
2260 chunk_id_str);
2261
2262 stream_positions = lttng_buffer_view_from_view(payload,
2263 sizeof(rotate_streams), -1);
2264 if (!stream_positions.data ||
2265 stream_positions.size <
2266 (rotate_streams.stream_count *
2267 sizeof(struct lttcomm_relayd_stream_rotation_position))) {
2268 reply_code = LTTNG_ERR_INVALID_PROTOCOL;
2269 ret = -1;
2270 goto end;
2271 }
2272
2273 for (i = 0; i < rotate_streams.stream_count; i++) {
2274 struct lttcomm_relayd_stream_rotation_position *position_comm =
2275 &((typeof(position_comm)) stream_positions.data)[i];
2276 const struct lttcomm_relayd_stream_rotation_position pos = {
2277 .stream_id = be64toh(position_comm->stream_id),
2278 .rotate_at_seq_num = be64toh(
2279 position_comm->rotate_at_seq_num),
2280 };
2281
2282 stream = stream_get_by_id(pos.stream_id);
2283 if (!stream) {
2284 reply_code = LTTNG_ERR_INVALID;
2285 ret = -1;
2286 goto end;
2287 }
2288
2289 pthread_mutex_lock(&stream->lock);
2290 ret = stream_set_pending_rotation(stream, next_trace_chunk,
2291 pos.rotate_at_seq_num);
2292 pthread_mutex_unlock(&stream->lock);
2293 if (ret) {
2294 reply_code = LTTNG_ERR_FILE_CREATION_ERROR;
2295 goto end;
2296 }
2297
2298 stream_put(stream);
2299 stream = NULL;
2300 }
2301
2302 reply_code = LTTNG_OK;
2303 ret = 0;
2304end:
2305 if (stream) {
2306 stream_put(stream);
2307 }
2308
2309 reply.ret_code = htobe32((uint32_t) reply_code);
2310 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2311 sizeof(struct lttcomm_relayd_generic_reply), 0);
2312 if (send_ret < (ssize_t) sizeof(reply)) {
2313 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2314 send_ret);
2315 ret = -1;
2316 }
2317end_no_reply:
2318 lttng_trace_chunk_put(next_trace_chunk);
2319 return ret;
2320}
2321
2322
2323
2324/*
2325 * relay_create_trace_chunk: create a new trace chunk
2326 */
2327static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
2328 struct relay_connection *conn,
2329 const struct lttng_buffer_view *payload)
2330{
2331 int ret = 0;
2332 ssize_t send_ret;
2333 struct relay_session *session = conn->session;
2334 struct lttcomm_relayd_create_trace_chunk *msg;
2335 struct lttcomm_relayd_generic_reply reply = {};
2336 struct lttng_buffer_view header_view;
2337 struct lttng_buffer_view chunk_name_view;
2338 struct lttng_trace_chunk *chunk = NULL, *published_chunk = NULL;
2339 enum lttng_error_code reply_code = LTTNG_OK;
2340 enum lttng_trace_chunk_status chunk_status;
2341 struct lttng_directory_handle session_output;
2342
2343 if (!session || !conn->version_check_done) {
2344 ERR("Trying to create a trace chunk before version check");
2345 ret = -1;
2346 goto end_no_reply;
2347 }
2348
2349 if (session->major == 2 && session->minor < 11) {
2350 ERR("Chunk creation command is unsupported before 2.11");
2351 ret = -1;
2352 goto end_no_reply;
2353 }
2354
2355 header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg));
2356 if (!header_view.data) {
2357 ERR("Failed to receive payload of chunk creation command");
2358 ret = -1;
2359 goto end_no_reply;
2360 }
2361
2362 /* Convert to host endianness. */
2363 msg = (typeof(msg)) header_view.data;
2364 msg->chunk_id = be64toh(msg->chunk_id);
2365 msg->creation_timestamp = be64toh(msg->creation_timestamp);
2366 msg->override_name_length = be32toh(msg->override_name_length);
2367
2368 chunk = lttng_trace_chunk_create(
2369 msg->chunk_id, msg->creation_timestamp);
2370 if (!chunk) {
2371 ERR("Failed to create trace chunk in trace chunk creation command");
2372 ret = -1;
2373 reply_code = LTTNG_ERR_NOMEM;
2374 goto end;
2375 }
2376
2377 if (msg->override_name_length) {
2378 const char *name;
2379
2380 chunk_name_view = lttng_buffer_view_from_view(payload,
2381 sizeof(*msg),
2382 msg->override_name_length);
2383 name = chunk_name_view.data;
2384 if (!name || name[msg->override_name_length - 1]) {
2385 ERR("Failed to receive payload of chunk creation command");
2386 ret = -1;
2387 reply_code = LTTNG_ERR_INVALID;
2388 goto end;
2389 }
2390
2391 chunk_status = lttng_trace_chunk_override_name(
2392 chunk, chunk_name_view.data);
2393 switch (chunk_status) {
2394 case LTTNG_TRACE_CHUNK_STATUS_OK:
2395 break;
2396 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT:
2397 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2398 reply_code = LTTNG_ERR_INVALID;
2399 ret = -1;
2400 goto end;
2401 default:
2402 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2403 reply_code = LTTNG_ERR_UNK;
2404 ret = -1;
2405 goto end;
2406 }
2407 }
2408
2409 chunk_status = lttng_trace_chunk_set_credentials_current_user(chunk);
2410 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
2411 reply_code = LTTNG_ERR_UNK;
2412 ret = -1;
2413 goto end;
2414 }
2415
2416 ret = session_init_output_directory_handle(
2417 conn->session, &session_output);
2418 if (ret) {
2419 reply_code = LTTNG_ERR_CREATE_DIR_FAIL;
2420 goto end;
2421 }
2422 chunk_status = lttng_trace_chunk_set_as_owner(chunk, &session_output);
2423 lttng_directory_handle_fini(&session_output);
2424 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
2425 reply_code = LTTNG_ERR_UNK;
2426 ret = -1;
2427 goto end;
2428 }
2429
2430 published_chunk = sessiond_trace_chunk_registry_publish_chunk(
2431 sessiond_trace_chunk_registry,
2432 conn->session->sessiond_uuid,
2433 conn->session->id,
2434 chunk);
2435 if (!published_chunk) {
2436 char uuid_str[UUID_STR_LEN];
2437
2438 lttng_uuid_to_str(conn->session->sessiond_uuid, uuid_str);
2439 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64 ", chunk_id = %" PRIu64,
2440 uuid_str,
2441 conn->session->id,
2442 msg->chunk_id);
2443 ret = -1;
2444 reply_code = LTTNG_ERR_NOMEM;
2445 goto end;
2446 }
2447
2448 pthread_mutex_lock(&conn->session->lock);
2449 if (conn->session->pending_closure_trace_chunk) {
2450 /*
2451 * Invalid; this means a second create_trace_chunk command was
2452 * received before a close_trace_chunk.
2453 */
2454 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2455 reply_code = LTTNG_ERR_INVALID_PROTOCOL;
2456 ret = -1;
2457 goto end_unlock_session;
2458 }
2459 conn->session->pending_closure_trace_chunk =
2460 conn->session->current_trace_chunk;
2461 conn->session->current_trace_chunk = published_chunk;
2462 published_chunk = NULL;
2463end_unlock_session:
2464 pthread_mutex_unlock(&conn->session->lock);
2465end:
2466 reply.ret_code = htobe32((uint32_t) reply_code);
2467 send_ret = conn->sock->ops->sendmsg(conn->sock,
2468 &reply,
2469 sizeof(struct lttcomm_relayd_generic_reply),
2470 0);
2471 if (send_ret < (ssize_t) sizeof(reply)) {
2472 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2473 send_ret);
2474 ret = -1;
2475 }
2476end_no_reply:
2477 lttng_trace_chunk_put(chunk);
2478 lttng_trace_chunk_put(published_chunk);
2479 return ret;
2480}
2481
2482/*
2483 * relay_close_trace_chunk: close a trace chunk
2484 */
2485static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
2486 struct relay_connection *conn,
2487 const struct lttng_buffer_view *payload)
2488{
2489 int ret = 0, buf_ret;
2490 ssize_t send_ret;
2491 struct relay_session *session = conn->session;
2492 struct lttcomm_relayd_close_trace_chunk *msg;
2493 struct lttcomm_relayd_close_trace_chunk_reply reply = {};
2494 struct lttng_buffer_view header_view;
2495 struct lttng_trace_chunk *chunk = NULL;
2496 enum lttng_error_code reply_code = LTTNG_OK;
2497 enum lttng_trace_chunk_status chunk_status;
2498 uint64_t chunk_id;
2499 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type) close_command = {};
2500 time_t close_timestamp;
2501 char closed_trace_chunk_path[LTTNG_PATH_MAX];
2502 size_t path_length = 0;
2503 const char *chunk_name = NULL;
2504 struct lttng_dynamic_buffer reply_payload;
2505
2506 lttng_dynamic_buffer_init(&reply_payload);
2507
2508 if (!session || !conn->version_check_done) {
2509 ERR("Trying to close a trace chunk before version check");
2510 ret = -1;
2511 goto end_no_reply;
2512 }
2513
2514 if (session->major == 2 && session->minor < 11) {
2515 ERR("Chunk close command is unsupported before 2.11");
2516 ret = -1;
2517 goto end_no_reply;
2518 }
2519
2520 header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg));
2521 if (!header_view.data) {
2522 ERR("Failed to receive payload of chunk close command");
2523 ret = -1;
2524 goto end_no_reply;
2525 }
2526
2527 /* Convert to host endianness. */
2528 msg = (typeof(msg)) header_view.data;
2529 chunk_id = be64toh(msg->chunk_id);
2530 close_timestamp = (time_t) be64toh(msg->close_timestamp);
2531 close_command = (typeof(close_command)){
2532 .value = be32toh(msg->close_command.value),
2533 .is_set = msg->close_command.is_set,
2534 };
2535
2536 chunk = sessiond_trace_chunk_registry_get_chunk(
2537 sessiond_trace_chunk_registry,
2538 conn->session->sessiond_uuid,
2539 conn->session->id,
2540 chunk_id);
2541 if (!chunk) {
2542 char uuid_str[UUID_STR_LEN];
2543
2544 lttng_uuid_to_str(conn->session->sessiond_uuid, uuid_str);
2545 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64 ", chunk_id = %" PRIu64,
2546 uuid_str,
2547 conn->session->id,
2548 msg->chunk_id);
2549 ret = -1;
2550 reply_code = LTTNG_ERR_NOMEM;
2551 goto end;
2552 }
2553
2554 pthread_mutex_lock(&session->lock);
2555 if (session->pending_closure_trace_chunk &&
2556 session->pending_closure_trace_chunk != chunk) {
2557 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2558 session->session_name);
2559 reply_code = LTTNG_ERR_INVALID_PROTOCOL;
2560 ret = -1;
2561 goto end_unlock_session;
2562 }
2563
2564 chunk_status = lttng_trace_chunk_set_close_timestamp(
2565 chunk, close_timestamp);
2566 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
2567 ERR("Failed to set trace chunk close timestamp");
2568 ret = -1;
2569 reply_code = LTTNG_ERR_UNK;
2570 goto end_unlock_session;
2571 }
2572
2573 if (close_command.is_set) {
2574 chunk_status = lttng_trace_chunk_set_close_command(
2575 chunk, close_command.value);
2576 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
2577 ret = -1;
2578 reply_code = LTTNG_ERR_INVALID;
2579 goto end_unlock_session;
2580 }
2581 }
2582 chunk_status = lttng_trace_chunk_get_name(chunk, &chunk_name, NULL);
2583 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
2584 ERR("Failed to get chunk name");
2585 ret = -1;
2586 reply_code = LTTNG_ERR_UNK;
2587 goto end_unlock_session;
2588 }
2589 if (!session->has_rotated && !session->snapshot) {
2590 ret = lttng_strncpy(closed_trace_chunk_path,
2591 session->output_path,
2592 sizeof(closed_trace_chunk_path));
2593 if (ret) {
2594 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2595 strlen(session->output_path),
2596 sizeof(closed_trace_chunk_path));
2597 reply_code = LTTNG_ERR_NOMEM;
2598 ret = -1;
2599 goto end_unlock_session;
2600 }
2601 } else {
2602 if (session->snapshot) {
2603 ret = snprintf(closed_trace_chunk_path,
2604 sizeof(closed_trace_chunk_path),
2605 "%s/%s", session->output_path,
2606 chunk_name);
2607 } else {
2608 ret = snprintf(closed_trace_chunk_path,
2609 sizeof(closed_trace_chunk_path),
2610 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2611 "/%s",
2612 session->output_path, chunk_name);
2613 }
2614 if (ret < 0 || ret == sizeof(closed_trace_chunk_path)) {
2615 ERR("Failed to format closed trace chunk resulting path");
2616 reply_code = ret < 0 ? LTTNG_ERR_UNK : LTTNG_ERR_NOMEM;
2617 ret = -1;
2618 goto end_unlock_session;
2619 }
2620 }
2621 DBG("Reply chunk path on close: %s", closed_trace_chunk_path);
2622 path_length = strlen(closed_trace_chunk_path) + 1;
2623 if (path_length > UINT32_MAX) {
2624 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2625 ret = -1;
2626 reply_code = LTTNG_ERR_INVALID_PROTOCOL;
2627 goto end_unlock_session;
2628 }
2629
2630 if (session->current_trace_chunk == chunk) {
2631 /*
2632 * After a trace chunk close command, no new streams
2633 * referencing the chunk may be created. Hence, on the
2634 * event that no new trace chunk have been created for
2635 * the session, the reference to the current trace chunk
2636 * is released in order to allow it to be reclaimed when
2637 * the last stream releases its reference to it.
2638 */
2639 lttng_trace_chunk_put(session->current_trace_chunk);
2640 session->current_trace_chunk = NULL;
2641 }
2642 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
2643 session->pending_closure_trace_chunk = NULL;
2644end_unlock_session:
2645 pthread_mutex_unlock(&session->lock);
2646
2647end:
2648 reply.generic.ret_code = htobe32((uint32_t) reply_code);
2649 reply.path_length = htobe32((uint32_t) path_length);
2650 buf_ret = lttng_dynamic_buffer_append(
2651 &reply_payload, &reply, sizeof(reply));
2652 if (buf_ret) {
2653 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2654 goto end_no_reply;
2655 }
2656
2657 if (reply_code == LTTNG_OK) {
2658 buf_ret = lttng_dynamic_buffer_append(&reply_payload,
2659 closed_trace_chunk_path, path_length);
2660 if (buf_ret) {
2661 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2662 goto end_no_reply;
2663 }
2664 }
2665
2666 send_ret = conn->sock->ops->sendmsg(conn->sock,
2667 reply_payload.data,
2668 reply_payload.size,
2669 0);
2670 if (send_ret < reply_payload.size) {
2671 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2672 reply_payload.size, send_ret);
2673 ret = -1;
2674 goto end_no_reply;
2675 }
2676end_no_reply:
2677 lttng_trace_chunk_put(chunk);
2678 lttng_dynamic_buffer_reset(&reply_payload);
2679 return ret;
2680}
2681
2682/*
2683 * relay_trace_chunk_exists: check if a trace chunk exists
2684 */
2685static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr *recv_hdr,
2686 struct relay_connection *conn,
2687 const struct lttng_buffer_view *payload)
2688{
2689 int ret = 0;
2690 ssize_t send_ret;
2691 struct relay_session *session = conn->session;
2692 struct lttcomm_relayd_trace_chunk_exists *msg;
2693 struct lttcomm_relayd_trace_chunk_exists_reply reply = {};
2694 struct lttng_buffer_view header_view;
2695 uint64_t chunk_id;
2696 bool chunk_exists;
2697
2698 if (!session || !conn->version_check_done) {
2699 ERR("Trying to close a trace chunk before version check");
2700 ret = -1;
2701 goto end_no_reply;
2702 }
2703
2704 if (session->major == 2 && session->minor < 11) {
2705 ERR("Chunk close command is unsupported before 2.11");
2706 ret = -1;
2707 goto end_no_reply;
2708 }
2709
2710 header_view = lttng_buffer_view_from_view(payload, 0, sizeof(*msg));
2711 if (!header_view.data) {
2712 ERR("Failed to receive payload of chunk close command");
2713 ret = -1;
2714 goto end_no_reply;
2715 }
2716
2717 /* Convert to host endianness. */
2718 msg = (typeof(msg)) header_view.data;
2719 chunk_id = be64toh(msg->chunk_id);
2720
2721 ret = sessiond_trace_chunk_registry_chunk_exists(
2722 sessiond_trace_chunk_registry,
2723 conn->session->sessiond_uuid,
2724 conn->session->id,
2725 chunk_id, &chunk_exists);
2726 /*
2727 * If ret is not 0, send the reply and report the error to the caller.
2728 * It is a protocol (or internal) error and the session/connection
2729 * should be torn down.
2730 */
2731 reply = (typeof(reply)){
2732 .generic.ret_code = htobe32((uint32_t)
2733 (ret == 0 ? LTTNG_OK : LTTNG_ERR_INVALID_PROTOCOL)),
2734 .trace_chunk_exists = ret == 0 ? chunk_exists : 0,
2735 };
2736 send_ret = conn->sock->ops->sendmsg(
2737 conn->sock, &reply, sizeof(reply), 0);
2738 if (send_ret < (ssize_t) sizeof(reply)) {
2739 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2740 send_ret);
2741 ret = -1;
2742 }
2743end_no_reply:
2744 return ret;
2745}
2746
2747#define DBG_CMD(cmd_name, conn) \
2748 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2749
2750static int relay_process_control_command(struct relay_connection *conn,
2751 const struct lttcomm_relayd_hdr *header,
2752 const struct lttng_buffer_view *payload)
2753{
2754 int ret = 0;
2755
2756 switch (header->cmd) {
2757 case RELAYD_CREATE_SESSION:
2758 DBG_CMD("RELAYD_CREATE_SESSION", conn);
2759 ret = relay_create_session(header, conn, payload);
2760 break;
2761 case RELAYD_ADD_STREAM:
2762 DBG_CMD("RELAYD_ADD_STREAM", conn);
2763 ret = relay_add_stream(header, conn, payload);
2764 break;
2765 case RELAYD_START_DATA:
2766 DBG_CMD("RELAYD_START_DATA", conn);
2767 ret = relay_start(header, conn, payload);
2768 break;
2769 case RELAYD_SEND_METADATA:
2770 DBG_CMD("RELAYD_SEND_METADATA", conn);
2771 ret = relay_recv_metadata(header, conn, payload);
2772 break;
2773 case RELAYD_VERSION:
2774 DBG_CMD("RELAYD_VERSION", conn);
2775 ret = relay_send_version(header, conn, payload);
2776 break;
2777 case RELAYD_CLOSE_STREAM:
2778 DBG_CMD("RELAYD_CLOSE_STREAM", conn);
2779 ret = relay_close_stream(header, conn, payload);
2780 break;
2781 case RELAYD_DATA_PENDING:
2782 DBG_CMD("RELAYD_DATA_PENDING", conn);
2783 ret = relay_data_pending(header, conn, payload);
2784 break;
2785 case RELAYD_QUIESCENT_CONTROL:
2786 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn);
2787 ret = relay_quiescent_control(header, conn, payload);
2788 break;
2789 case RELAYD_BEGIN_DATA_PENDING:
2790 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn);
2791 ret = relay_begin_data_pending(header, conn, payload);
2792 break;
2793 case RELAYD_END_DATA_PENDING:
2794 DBG_CMD("RELAYD_END_DATA_PENDING", conn);
2795 ret = relay_end_data_pending(header, conn, payload);
2796 break;
2797 case RELAYD_SEND_INDEX:
2798 DBG_CMD("RELAYD_SEND_INDEX", conn);
2799 ret = relay_recv_index(header, conn, payload);
2800 break;
2801 case RELAYD_STREAMS_SENT:
2802 DBG_CMD("RELAYD_STREAMS_SENT", conn);
2803 ret = relay_streams_sent(header, conn, payload);
2804 break;
2805 case RELAYD_RESET_METADATA:
2806 DBG_CMD("RELAYD_RESET_METADATA", conn);
2807 ret = relay_reset_metadata(header, conn, payload);
2808 break;
2809 case RELAYD_ROTATE_STREAMS:
2810 DBG_CMD("RELAYD_ROTATE_STREAMS", conn);
2811 ret = relay_rotate_session_streams(header, conn, payload);
2812 break;
2813 case RELAYD_CREATE_TRACE_CHUNK:
2814 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn);
2815 ret = relay_create_trace_chunk(header, conn, payload);
2816 break;
2817 case RELAYD_CLOSE_TRACE_CHUNK:
2818 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn);
2819 ret = relay_close_trace_chunk(header, conn, payload);
2820 break;
2821 case RELAYD_TRACE_CHUNK_EXISTS:
2822 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn);
2823 ret = relay_trace_chunk_exists(header, conn, payload);
2824 break;
2825 case RELAYD_UPDATE_SYNC_INFO:
2826 default:
2827 ERR("Received unknown command (%u)", header->cmd);
2828 relay_unknown_command(conn);
2829 ret = -1;
2830 goto end;
2831 }
2832
2833end:
2834 return ret;
2835}
2836
2837static enum relay_connection_status relay_process_control_receive_payload(
2838 struct relay_connection *conn)
2839{
2840 int ret = 0;
2841 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
2842 struct lttng_dynamic_buffer *reception_buffer =
2843 &conn->protocol.ctrl.reception_buffer;
2844 struct ctrl_connection_state_receive_payload *state =
2845 &conn->protocol.ctrl.state.receive_payload;
2846 struct lttng_buffer_view payload_view;
2847
2848 if (state->left_to_receive == 0) {
2849 /* Short-circuit for payload-less commands. */
2850 goto reception_complete;
2851 }
2852
2853 ret = conn->sock->ops->recvmsg(conn->sock,
2854 reception_buffer->data + state->received,
2855 state->left_to_receive, MSG_DONTWAIT);
2856 if (ret < 0) {
2857 if (errno != EAGAIN && errno != EWOULDBLOCK) {
2858 PERROR("Unable to receive command payload on sock %d",
2859 conn->sock->fd);
2860 status = RELAY_CONNECTION_STATUS_ERROR;
2861 }
2862 goto end;
2863 } else if (ret == 0) {
2864 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
2865 status = RELAY_CONNECTION_STATUS_CLOSED;
2866 goto end;
2867 }
2868
2869 assert(ret > 0);
2870 assert(ret <= state->left_to_receive);
2871
2872 state->left_to_receive -= ret;
2873 state->received += ret;
2874
2875 if (state->left_to_receive > 0) {
2876 /*
2877 * Can't transition to the protocol's next state, wait to
2878 * receive the rest of the header.
2879 */
2880 DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
2881 state->received, state->left_to_receive,
2882 conn->sock->fd);
2883 goto end;
2884 }
2885
2886reception_complete:
2887 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes",
2888 conn->sock->fd, state->received);
2889 /*
2890 * The payload required to process the command has been received.
2891 * A view to the reception buffer is forwarded to the various
2892 * commands and the state of the control is reset on success.
2893 *
2894 * Commands are responsible for sending their reply to the peer.
2895 */
2896 payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer,
2897 0, -1);
2898 ret = relay_process_control_command(conn,
2899 &state->header, &payload_view);
2900 if (ret < 0) {
2901 status = RELAY_CONNECTION_STATUS_ERROR;
2902 goto end;
2903 }
2904
2905 ret = connection_reset_protocol_state(conn);
2906 if (ret) {
2907 status = RELAY_CONNECTION_STATUS_ERROR;
2908 }
2909end:
2910 return status;
2911}
2912
2913static enum relay_connection_status relay_process_control_receive_header(
2914 struct relay_connection *conn)
2915{
2916 int ret = 0;
2917 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
2918 struct lttcomm_relayd_hdr header;
2919 struct lttng_dynamic_buffer *reception_buffer =
2920 &conn->protocol.ctrl.reception_buffer;
2921 struct ctrl_connection_state_receive_header *state =
2922 &conn->protocol.ctrl.state.receive_header;
2923
2924 assert(state->left_to_receive != 0);
2925
2926 ret = conn->sock->ops->recvmsg(conn->sock,
2927 reception_buffer->data + state->received,
2928 state->left_to_receive, MSG_DONTWAIT);
2929 if (ret < 0) {
2930 if (errno != EAGAIN && errno != EWOULDBLOCK) {
2931 PERROR("Unable to receive control command header on sock %d",
2932 conn->sock->fd);
2933 status = RELAY_CONNECTION_STATUS_ERROR;
2934 }
2935 goto end;
2936 } else if (ret == 0) {
2937 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
2938 status = RELAY_CONNECTION_STATUS_CLOSED;
2939 goto end;
2940 }
2941
2942 assert(ret > 0);
2943 assert(ret <= state->left_to_receive);
2944
2945 state->left_to_receive -= ret;
2946 state->received += ret;
2947
2948 if (state->left_to_receive > 0) {
2949 /*
2950 * Can't transition to the protocol's next state, wait to
2951 * receive the rest of the header.
2952 */
2953 DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
2954 state->received, state->left_to_receive,
2955 conn->sock->fd);
2956 goto end;
2957 }
2958
2959 /* Transition to next state: receiving the command's payload. */
2960 conn->protocol.ctrl.state_id =
2961 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD;
2962 memcpy(&header, reception_buffer->data, sizeof(header));
2963 header.circuit_id = be64toh(header.circuit_id);
2964 header.data_size = be64toh(header.data_size);
2965 header.cmd = be32toh(header.cmd);
2966 header.cmd_version = be32toh(header.cmd_version);
2967 memcpy(&conn->protocol.ctrl.state.receive_payload.header,
2968 &header, sizeof(header));
2969
2970 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes",
2971 conn->sock->fd, header.cmd, header.cmd_version,
2972 header.data_size);
2973
2974 if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) {
2975 ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.",
2976 header.data_size);
2977 status = RELAY_CONNECTION_STATUS_ERROR;
2978 goto end;
2979 }
2980
2981 conn->protocol.ctrl.state.receive_payload.left_to_receive =
2982 header.data_size;
2983 conn->protocol.ctrl.state.receive_payload.received = 0;
2984 ret = lttng_dynamic_buffer_set_size(reception_buffer,
2985 header.data_size);
2986 if (ret) {
2987 status = RELAY_CONNECTION_STATUS_ERROR;
2988 goto end;
2989 }
2990
2991 if (header.data_size == 0) {
2992 /*
2993 * Manually invoke the next state as the poll loop
2994 * will not wake-up to allow us to proceed further.
2995 */
2996 status = relay_process_control_receive_payload(conn);
2997 }
2998end:
2999 return status;
3000}
3001
3002/*
3003 * Process the commands received on the control socket
3004 */
3005static enum relay_connection_status relay_process_control(
3006 struct relay_connection *conn)
3007{
3008 enum relay_connection_status status;
3009
3010 switch (conn->protocol.ctrl.state_id) {
3011 case CTRL_CONNECTION_STATE_RECEIVE_HEADER:
3012 status = relay_process_control_receive_header(conn);
3013 break;
3014 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD:
3015 status = relay_process_control_receive_payload(conn);
3016 break;
3017 default:
3018 ERR("Unknown control connection protocol state encountered.");
3019 abort();
3020 }
3021
3022 return status;
3023}
3024
3025static enum relay_connection_status relay_process_data_receive_header(
3026 struct relay_connection *conn)
3027{
3028 int ret;
3029 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3030 struct data_connection_state_receive_header *state =
3031 &conn->protocol.data.state.receive_header;
3032 struct lttcomm_relayd_data_hdr header;
3033 struct relay_stream *stream;
3034
3035 assert(state->left_to_receive != 0);
3036
3037 ret = conn->sock->ops->recvmsg(conn->sock,
3038 state->header_reception_buffer + state->received,
3039 state->left_to_receive, MSG_DONTWAIT);
3040 if (ret < 0) {
3041 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3042 PERROR("Unable to receive data header on sock %d", conn->sock->fd);
3043 status = RELAY_CONNECTION_STATUS_ERROR;
3044 }
3045 goto end;
3046 } else if (ret == 0) {
3047 /* Orderly shutdown. Not necessary to print an error. */
3048 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
3049 status = RELAY_CONNECTION_STATUS_CLOSED;
3050 goto end;
3051 }
3052
3053 assert(ret > 0);
3054 assert(ret <= state->left_to_receive);
3055
3056 state->left_to_receive -= ret;
3057 state->received += ret;
3058
3059 if (state->left_to_receive > 0) {
3060 /*
3061 * Can't transition to the protocol's next state, wait to
3062 * receive the rest of the header.
3063 */
3064 DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
3065 state->received, state->left_to_receive,
3066 conn->sock->fd);
3067 goto end;
3068 }
3069
3070 /* Transition to next state: receiving the payload. */
3071 conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD;
3072
3073 memcpy(&header, state->header_reception_buffer, sizeof(header));
3074 header.circuit_id = be64toh(header.circuit_id);
3075 header.stream_id = be64toh(header.stream_id);
3076 header.data_size = be32toh(header.data_size);
3077 header.net_seq_num = be64toh(header.net_seq_num);
3078 header.padding_size = be32toh(header.padding_size);
3079 memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header));
3080
3081 conn->protocol.data.state.receive_payload.left_to_receive =
3082 header.data_size;
3083 conn->protocol.data.state.receive_payload.received = 0;
3084 conn->protocol.data.state.receive_payload.rotate_index = false;
3085
3086 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32,
3087 conn->sock->fd, header.circuit_id,
3088 header.stream_id, header.data_size,
3089 header.net_seq_num, header.padding_size);
3090
3091 stream = stream_get_by_id(header.stream_id);
3092 if (!stream) {
3093 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64,
3094 header.stream_id);
3095 /* Protocol error. */
3096 status = RELAY_CONNECTION_STATUS_ERROR;
3097 goto end;
3098 }
3099
3100 pthread_mutex_lock(&stream->lock);
3101 /* Prepare stream for the reception of a new packet. */
3102 ret = stream_init_packet(stream, header.data_size,
3103 &conn->protocol.data.state.receive_payload.rotate_index);
3104 pthread_mutex_unlock(&stream->lock);
3105 if (ret) {
3106 ERR("Failed to rotate stream output file");
3107 status = RELAY_CONNECTION_STATUS_ERROR;
3108 goto end_stream_unlock;
3109 }
3110
3111end_stream_unlock:
3112 stream_put(stream);
3113end:
3114 return status;
3115}
3116
3117static enum relay_connection_status relay_process_data_receive_payload(
3118 struct relay_connection *conn)
3119{
3120 int ret;
3121 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3122 struct relay_stream *stream;
3123 struct data_connection_state_receive_payload *state =
3124 &conn->protocol.data.state.receive_payload;
3125 const size_t chunk_size = RECV_DATA_BUFFER_SIZE;
3126 char data_buffer[chunk_size];
3127 bool partial_recv = false;
3128 bool new_stream = false, close_requested = false, index_flushed = false;
3129 uint64_t left_to_receive = state->left_to_receive;
3130 struct relay_session *session;
3131
3132 DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive",
3133 state->header.stream_id, state->header.net_seq_num,
3134 state->received, left_to_receive);
3135
3136 stream = stream_get_by_id(state->header.stream_id);
3137 if (!stream) {
3138 /* Protocol error. */
3139 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64,
3140 state->header.stream_id);
3141 status = RELAY_CONNECTION_STATUS_ERROR;
3142 goto end;
3143 }
3144
3145 pthread_mutex_lock(&stream->lock);
3146 session = stream->trace->session;
3147 if (!conn->session) {
3148 ret = connection_set_session(conn, session);
3149 if (ret) {
3150 status = RELAY_CONNECTION_STATUS_ERROR;
3151 goto end_stream_unlock;
3152 }
3153 }
3154
3155 /*
3156 * The size of the "chunk" received on any iteration is bounded by:
3157 * - the data left to receive,
3158 * - the data immediately available on the socket,
3159 * - the on-stack data buffer
3160 */
3161 while (left_to_receive > 0 && !partial_recv) {
3162 size_t recv_size = min(left_to_receive, chunk_size);
3163 struct lttng_buffer_view packet_chunk;
3164
3165 ret = conn->sock->ops->recvmsg(conn->sock, data_buffer,
3166 recv_size, MSG_DONTWAIT);
3167 if (ret < 0) {
3168 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3169 PERROR("Socket %d error", conn->sock->fd);
3170 status = RELAY_CONNECTION_STATUS_ERROR;
3171 }
3172 goto end_stream_unlock;
3173 } else if (ret == 0) {
3174 /* No more data ready to be consumed on socket. */
3175 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64,
3176 state->header.stream_id);
3177 status = RELAY_CONNECTION_STATUS_CLOSED;
3178 break;
3179 } else if (ret < (int) recv_size) {
3180 /*
3181 * All the data available on the socket has been
3182 * consumed.
3183 */
3184 partial_recv = true;
3185 recv_size = ret;
3186 }
3187
3188 packet_chunk = lttng_buffer_view_init(data_buffer,
3189 0, recv_size);
3190 assert(packet_chunk.data);
3191
3192 ret = stream_write(stream, &packet_chunk, 0);
3193 if (ret) {
3194 ERR("Relay error writing data to file");
3195 status = RELAY_CONNECTION_STATUS_ERROR;
3196 goto end_stream_unlock;
3197 }
3198
3199 left_to_receive -= recv_size;
3200 state->received += recv_size;
3201 state->left_to_receive = left_to_receive;
3202 }
3203
3204 if (state->left_to_receive > 0) {
3205 /*
3206 * Did not receive all the data expected, wait for more data to
3207 * become available on the socket.
3208 */
3209 DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive",
3210 state->header.stream_id, state->received,
3211 state->left_to_receive);
3212 goto end_stream_unlock;
3213 }
3214
3215 ret = stream_write(stream, NULL, state->header.padding_size);
3216 if (ret) {
3217 status = RELAY_CONNECTION_STATUS_ERROR;
3218 goto end_stream_unlock;
3219 }
3220
3221 if (session_streams_have_index(session)) {
3222 ret = stream_update_index(stream, state->header.net_seq_num,
3223 state->rotate_index, &index_flushed,
3224 state->header.data_size + state->header.padding_size);
3225 if (ret < 0) {
3226 ERR("Failed to update index: stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d",
3227 stream->stream_handle,
3228 state->header.net_seq_num, ret);
3229 status = RELAY_CONNECTION_STATUS_ERROR;
3230 goto end_stream_unlock;
3231 }
3232 }
3233
3234 if (stream->prev_data_seq == -1ULL) {
3235 new_stream = true;
3236 }
3237
3238 ret = stream_complete_packet(stream, state->header.data_size +
3239 state->header.padding_size, state->header.net_seq_num,
3240 index_flushed);
3241 if (ret) {
3242 status = RELAY_CONNECTION_STATUS_ERROR;
3243 goto end_stream_unlock;
3244 }
3245
3246 /*
3247 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3248 * contents of *state which are aliased (union) to the same location as
3249 * the new state. Don't use it beyond this point.
3250 */
3251 connection_reset_protocol_state(conn);
3252 state = NULL;
3253
3254end_stream_unlock:
3255 close_requested = stream->close_requested;
3256 pthread_mutex_unlock(&stream->lock);
3257 if (close_requested && left_to_receive == 0) {
3258 try_stream_close(stream);
3259 }
3260
3261 if (new_stream) {
3262 pthread_mutex_lock(&session->lock);
3263 uatomic_set(&session->new_streams, 1);
3264 pthread_mutex_unlock(&session->lock);
3265 }
3266
3267 stream_put(stream);
3268end:
3269 return status;
3270}
3271
3272/*
3273 * relay_process_data: Process the data received on the data socket
3274 */
3275static enum relay_connection_status relay_process_data(
3276 struct relay_connection *conn)
3277{
3278 enum relay_connection_status status;
3279
3280 switch (conn->protocol.data.state_id) {
3281 case DATA_CONNECTION_STATE_RECEIVE_HEADER:
3282 status = relay_process_data_receive_header(conn);
3283 break;
3284 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD:
3285 status = relay_process_data_receive_payload(conn);
3286 break;
3287 default:
3288 ERR("Unexpected data connection communication state.");
3289 abort();
3290 }
3291
3292 return status;
3293}
3294
3295static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
3296{
3297 int ret;
3298
3299 (void) lttng_poll_del(events, pollfd);
3300
3301 ret = close(pollfd);
3302 if (ret < 0) {
3303 ERR("Closing pollfd %d", pollfd);
3304 }
3305}
3306
3307static void relay_thread_close_connection(struct lttng_poll_event *events,
3308 int pollfd, struct relay_connection *conn)
3309{
3310 const char *type_str;
3311
3312 switch (conn->type) {
3313 case RELAY_DATA:
3314 type_str = "Data";
3315 break;
3316 case RELAY_CONTROL:
3317 type_str = "Control";
3318 break;
3319 case RELAY_VIEWER_COMMAND:
3320 type_str = "Viewer Command";
3321 break;
3322 case RELAY_VIEWER_NOTIFICATION:
3323 type_str = "Viewer Notification";
3324 break;
3325 default:
3326 type_str = "Unknown";
3327 }
3328 cleanup_connection_pollfd(events, pollfd);
3329 connection_put(conn);
3330 DBG("%s connection closed with %d", type_str, pollfd);
3331}
3332
3333/*
3334 * This thread does the actual work
3335 */
3336static void *relay_thread_worker(void *data)
3337{
3338 int ret, err = -1, last_seen_data_fd = -1;
3339 uint32_t nb_fd;
3340 struct lttng_poll_event events;
3341 struct lttng_ht *relay_connections_ht;
3342 struct lttng_ht_iter iter;
3343 struct relay_connection *destroy_conn = NULL;
3344
3345 DBG("[thread] Relay worker started");
3346
3347 rcu_register_thread();
3348
3349 health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER);
3350
3351 if (testpoint(relayd_thread_worker)) {
3352 goto error_testpoint;
3353 }
3354
3355 health_code_update();
3356
3357 /* table of connections indexed on socket */
3358 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3359 if (!relay_connections_ht) {
3360 goto relay_connections_ht_error;
3361 }
3362
3363 ret = create_thread_poll_set(&events, 2);
3364 if (ret < 0) {
3365 goto error_poll_create;
3366 }
3367
3368 ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
3369 if (ret < 0) {
3370 goto error;
3371 }
3372
3373restart:
3374 while (1) {
3375 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
3376
3377 health_code_update();
3378
3379 /* Infinite blocking call, waiting for transmission */
3380 DBG3("Relayd worker thread polling...");
3381 health_poll_entry();
3382 ret = lttng_poll_wait(&events, -1);
3383 health_poll_exit();
3384 if (ret < 0) {
3385 /*
3386 * Restart interrupted system call.
3387 */
3388 if (errno == EINTR) {
3389 goto restart;
3390 }
3391 goto error;
3392 }
3393
3394 nb_fd = ret;
3395
3396 /*
3397 * Process control. The control connection is
3398 * prioritized so we don't starve it with high
3399 * throughput tracing data on the data connection.
3400 */
3401 for (i = 0; i < nb_fd; i++) {
3402 /* Fetch once the poll data */
3403 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
3404 int pollfd = LTTNG_POLL_GETFD(&events, i);
3405
3406 health_code_update();
3407
3408 /* Thread quit pipe has been closed. Killing thread. */
3409 ret = check_thread_quit_pipe(pollfd, revents);
3410 if (ret) {
3411 err = 0;
3412 goto exit;
3413 }
3414
3415 /* Inspect the relay conn pipe for new connection */
3416 if (pollfd == relay_conn_pipe[0]) {
3417 if (revents & LPOLLIN) {
3418 struct relay_connection *conn;
3419
3420 ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn));
3421 if (ret < 0) {
3422 goto error;
3423 }
3424 ret = lttng_poll_add(&events,
3425 conn->sock->fd,
3426 LPOLLIN | LPOLLRDHUP);
3427 if (ret) {
3428 ERR("Failed to add new connection file descriptor to poll set");
3429 goto error;
3430 }
3431 connection_ht_add(relay_connections_ht, conn);
3432 DBG("Connection socket %d added", conn->sock->fd);
3433 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3434 ERR("Relay connection pipe error");
3435 goto error;
3436 } else {
3437 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3438 goto error;
3439 }
3440 } else {
3441 struct relay_connection *ctrl_conn;
3442
3443 ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd);
3444 /* If not found, there is a synchronization issue. */
3445 assert(ctrl_conn);
3446
3447 if (ctrl_conn->type == RELAY_DATA) {
3448 if (revents & LPOLLIN) {
3449 /*
3450 * Flag the last seen data fd not deleted. It will be
3451 * used as the last seen fd if any fd gets deleted in
3452 * this first loop.
3453 */
3454 last_notdel_data_fd = pollfd;
3455 }
3456 goto put_ctrl_connection;
3457 }
3458 assert(ctrl_conn->type == RELAY_CONTROL);
3459
3460 if (revents & LPOLLIN) {
3461 enum relay_connection_status status;
3462
3463 status = relay_process_control(ctrl_conn);
3464 if (status != RELAY_CONNECTION_STATUS_OK) {
3465 /*
3466 * On socket error flag the session as aborted to force
3467 * the cleanup of its stream otherwise it can leak
3468 * during the lifetime of the relayd.
3469 *
3470 * This prevents situations in which streams can be
3471 * left opened because an index was received, the
3472 * control connection is closed, and the data
3473 * connection is closed (uncleanly) before the packet's
3474 * data provided.
3475 *
3476 * Since the control connection encountered an error,
3477 * it is okay to be conservative and close the
3478 * session right now as we can't rely on the protocol
3479 * being respected anymore.
3480 */
3481 if (status == RELAY_CONNECTION_STATUS_ERROR) {
3482 session_abort(ctrl_conn->session);
3483 }
3484
3485 /* Clear the connection on error or close. */
3486 relay_thread_close_connection(&events,
3487 pollfd,
3488 ctrl_conn);
3489 }
3490 seen_control = 1;
3491 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3492 relay_thread_close_connection(&events,
3493 pollfd, ctrl_conn);
3494 if (last_seen_data_fd == pollfd) {
3495 last_seen_data_fd = last_notdel_data_fd;
3496 }
3497 } else {
3498 ERR("Unexpected poll events %u for control sock %d",
3499 revents, pollfd);
3500 connection_put(ctrl_conn);
3501 goto error;
3502 }
3503 put_ctrl_connection:
3504 connection_put(ctrl_conn);
3505 }
3506 }
3507
3508 /*
3509 * The last loop handled a control request, go back to poll to make
3510 * sure we prioritise the control socket.
3511 */
3512 if (seen_control) {
3513 continue;
3514 }
3515
3516 if (last_seen_data_fd >= 0) {
3517 for (i = 0; i < nb_fd; i++) {
3518 int pollfd = LTTNG_POLL_GETFD(&events, i);
3519
3520 health_code_update();
3521
3522 if (last_seen_data_fd == pollfd) {
3523 idx = i;
3524 break;
3525 }
3526 }
3527 }
3528
3529 /* Process data connection. */
3530 for (i = idx + 1; i < nb_fd; i++) {
3531 /* Fetch the poll data. */
3532 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
3533 int pollfd = LTTNG_POLL_GETFD(&events, i);
3534 struct relay_connection *data_conn;
3535
3536 health_code_update();
3537
3538 if (!revents) {
3539 /* No activity for this FD (poll implementation). */
3540 continue;
3541 }
3542
3543 /* Skip the command pipe. It's handled in the first loop. */
3544 if (pollfd == relay_conn_pipe[0]) {
3545 continue;
3546 }
3547
3548 data_conn = connection_get_by_sock(relay_connections_ht, pollfd);
3549 if (!data_conn) {
3550 /* Skip it. Might be removed before. */
3551 continue;
3552 }
3553 if (data_conn->type == RELAY_CONTROL) {
3554 goto put_data_connection;
3555 }
3556 assert(data_conn->type == RELAY_DATA);
3557
3558 if (revents & LPOLLIN) {
3559 enum relay_connection_status status;
3560
3561 status = relay_process_data(data_conn);
3562 /* Connection closed or error. */
3563 if (status != RELAY_CONNECTION_STATUS_OK) {
3564 /*
3565 * On socket error flag the session as aborted to force
3566 * the cleanup of its stream otherwise it can leak
3567 * during the lifetime of the relayd.
3568 *
3569 * This prevents situations in which streams can be
3570 * left opened because an index was received, the
3571 * control connection is closed, and the data
3572 * connection is closed (uncleanly) before the packet's
3573 * data provided.
3574 *
3575 * Since the data connection encountered an error,
3576 * it is okay to be conservative and close the
3577 * session right now as we can't rely on the protocol
3578 * being respected anymore.
3579 */
3580 if (status == RELAY_CONNECTION_STATUS_ERROR) {
3581 session_abort(data_conn->session);
3582 }
3583 relay_thread_close_connection(&events, pollfd,
3584 data_conn);
3585 /*
3586 * Every goto restart call sets the last seen fd where
3587 * here we don't really care since we gracefully
3588 * continue the loop after the connection is deleted.
3589 */
3590 } else {
3591 /* Keep last seen port. */
3592 last_seen_data_fd = pollfd;
3593 connection_put(data_conn);
3594 goto restart;
3595 }
3596 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3597 relay_thread_close_connection(&events, pollfd,
3598 data_conn);
3599 } else {
3600 ERR("Unknown poll events %u for data sock %d",
3601 revents, pollfd);
3602 }
3603 put_data_connection:
3604 connection_put(data_conn);
3605 }
3606 last_seen_data_fd = -1;
3607 }
3608
3609 /* Normal exit, no error */
3610 ret = 0;
3611
3612exit:
3613error:
3614 /* Cleanup remaining connection object. */
3615 rcu_read_lock();
3616 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter,
3617 destroy_conn,
3618 sock_n.node) {
3619 health_code_update();
3620
3621 session_abort(destroy_conn->session);
3622
3623 /*
3624 * No need to grab another ref, because we own
3625 * destroy_conn.
3626 */
3627 relay_thread_close_connection(&events, destroy_conn->sock->fd,
3628 destroy_conn);
3629 }
3630 rcu_read_unlock();
3631
3632 lttng_poll_clean(&events);
3633error_poll_create:
3634 lttng_ht_destroy(relay_connections_ht);
3635relay_connections_ht_error:
3636 /* Close relay conn pipes */
3637 utils_close_pipe(relay_conn_pipe);
3638 if (err) {
3639 DBG("Thread exited with error");
3640 }
3641 DBG("Worker thread cleanup complete");
3642error_testpoint:
3643 if (err) {
3644 health_error();
3645 ERR("Health error occurred in %s", __func__);
3646 }
3647 health_unregister(health_relayd);
3648 rcu_unregister_thread();
3649 lttng_relay_stop_threads();
3650 return NULL;
3651}
3652
3653/*
3654 * Create the relay command pipe to wake thread_manage_apps.
3655 * Closed in cleanup().
3656 */
3657static int create_relay_conn_pipe(void)
3658{
3659 int ret;
3660
3661 ret = utils_create_pipe_cloexec(relay_conn_pipe);
3662
3663 return ret;
3664}
3665
3666/*
3667 * main
3668 */
3669int main(int argc, char **argv)
3670{
3671 int ret = 0, retval = 0;
3672 void *status;
3673
3674 /* Parse arguments */
3675 progname = argv[0];
3676 if (set_options(argc, argv)) {
3677 retval = -1;
3678 goto exit_options;
3679 }
3680
3681 if (set_signal_handler()) {
3682 retval = -1;
3683 goto exit_options;
3684 }
3685
3686 relayd_config_log();
3687
3688 if (opt_print_version) {
3689 print_version();
3690 retval = 0;
3691 goto exit_options;
3692 }
3693
3694 /* Try to create directory if -o, --output is specified. */
3695 if (opt_output_path) {
3696 if (*opt_output_path != '/') {
3697 ERR("Please specify an absolute path for -o, --output PATH");
3698 retval = -1;
3699 goto exit_options;
3700 }
3701
3702 ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG,
3703 -1, -1);
3704 if (ret < 0) {
3705 ERR("Unable to create %s", opt_output_path);
3706 retval = -1;
3707 goto exit_options;
3708 }
3709 }
3710
3711 /* Daemonize */
3712 if (opt_daemon || opt_background) {
3713 int i;
3714
3715 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
3716 !opt_background);
3717 if (ret < 0) {
3718 retval = -1;
3719 goto exit_options;
3720 }
3721
3722 /*
3723 * We are in the child. Make sure all other file
3724 * descriptors are closed, in case we are called with
3725 * more opened file descriptors than the standard ones.
3726 */
3727 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
3728 (void) close(i);
3729 }
3730 }
3731
3732 sessiond_trace_chunk_registry = sessiond_trace_chunk_registry_create();
3733 if (!sessiond_trace_chunk_registry) {
3734 ERR("Failed to initialize session daemon trace chunk registry");
3735 retval = -1;
3736 goto exit_sessiond_trace_chunk_registry;
3737 }
3738
3739 /* Initialize thread health monitoring */
3740 health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
3741 if (!health_relayd) {
3742 PERROR("health_app_create error");
3743 retval = -1;
3744 goto exit_health_app_create;
3745 }
3746
3747 /* Create thread quit pipe */
3748 if (init_thread_quit_pipe()) {
3749 retval = -1;
3750 goto exit_init_data;
3751 }
3752
3753 /* Setup the thread apps communication pipe. */
3754 if (create_relay_conn_pipe()) {
3755 retval = -1;
3756 goto exit_init_data;
3757 }
3758
3759 /* Init relay command queue. */
3760 cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail);
3761
3762 /* Initialize communication library */
3763 lttcomm_init();
3764 lttcomm_inet_init();
3765
3766 /* tables of sessions indexed by session ID */
3767 sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3768 if (!sessions_ht) {
3769 retval = -1;
3770 goto exit_init_data;
3771 }
3772
3773 /* tables of streams indexed by stream ID */
3774 relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3775 if (!relay_streams_ht) {
3776 retval = -1;
3777 goto exit_init_data;
3778 }
3779
3780 /* tables of streams indexed by stream ID */
3781 viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3782 if (!viewer_streams_ht) {
3783 retval = -1;
3784 goto exit_init_data;
3785 }
3786
3787 ret = utils_create_pipe(health_quit_pipe);
3788 if (ret) {
3789 retval = -1;
3790 goto exit_health_quit_pipe;
3791 }
3792
3793 /* Create thread to manage the client socket */
3794 ret = pthread_create(&health_thread, default_pthread_attr(),
3795 thread_manage_health, (void *) NULL);
3796 if (ret) {
3797 errno = ret;
3798 PERROR("pthread_create health");
3799 retval = -1;
3800 goto exit_health_thread;
3801 }
3802
3803 /* Setup the dispatcher thread */
3804 ret = pthread_create(&dispatcher_thread, default_pthread_attr(),
3805 relay_thread_dispatcher, (void *) NULL);
3806 if (ret) {
3807 errno = ret;
3808 PERROR("pthread_create dispatcher");
3809 retval = -1;
3810 goto exit_dispatcher_thread;
3811 }
3812
3813 /* Setup the worker thread */
3814 ret = pthread_create(&worker_thread, default_pthread_attr(),
3815 relay_thread_worker, NULL);
3816 if (ret) {
3817 errno = ret;
3818 PERROR("pthread_create worker");
3819 retval = -1;
3820 goto exit_worker_thread;
3821 }
3822
3823 /* Setup the listener thread */
3824 ret = pthread_create(&listener_thread, default_pthread_attr(),
3825 relay_thread_listener, (void *) NULL);
3826 if (ret) {
3827 errno = ret;
3828 PERROR("pthread_create listener");
3829 retval = -1;
3830 goto exit_listener_thread;
3831 }
3832
3833 ret = relayd_live_create(live_uri);
3834 if (ret) {
3835 ERR("Starting live viewer threads");
3836 retval = -1;
3837 goto exit_live;
3838 }
3839
3840 /*
3841 * This is where we start awaiting program completion (e.g. through
3842 * signal that asks threads to teardown).
3843 */
3844
3845 ret = relayd_live_join();
3846 if (ret) {
3847 retval = -1;
3848 }
3849exit_live:
3850
3851 ret = pthread_join(listener_thread, &status);
3852 if (ret) {
3853 errno = ret;
3854 PERROR("pthread_join listener_thread");
3855 retval = -1;
3856 }
3857
3858exit_listener_thread:
3859 ret = pthread_join(worker_thread, &status);
3860 if (ret) {
3861 errno = ret;
3862 PERROR("pthread_join worker_thread");
3863 retval = -1;
3864 }
3865
3866exit_worker_thread:
3867 ret = pthread_join(dispatcher_thread, &status);
3868 if (ret) {
3869 errno = ret;
3870 PERROR("pthread_join dispatcher_thread");
3871 retval = -1;
3872 }
3873exit_dispatcher_thread:
3874
3875 ret = pthread_join(health_thread, &status);
3876 if (ret) {
3877 errno = ret;
3878 PERROR("pthread_join health_thread");
3879 retval = -1;
3880 }
3881exit_health_thread:
3882
3883 utils_close_pipe(health_quit_pipe);
3884exit_health_quit_pipe:
3885
3886exit_init_data:
3887 health_app_destroy(health_relayd);
3888 sessiond_trace_chunk_registry_destroy(sessiond_trace_chunk_registry);
3889exit_health_app_create:
3890exit_sessiond_trace_chunk_registry:
3891exit_options:
3892 /*
3893 * Wait for all pending call_rcu work to complete before tearing
3894 * down data structures. call_rcu worker may be trying to
3895 * perform lookups in those structures.
3896 */
3897 rcu_barrier();
3898 relayd_cleanup();
3899
3900 /* Ensure all prior call_rcu are done. */
3901 rcu_barrier();
3902
3903 if (!retval) {
3904 exit(EXIT_SUCCESS);
3905 } else {
3906 exit(EXIT_FAILURE);
3907 }
3908}
This page took 0.109712 seconds and 5 git commands to generate.