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>
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.
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
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.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/resource.h>
39 #include <urcu/futex.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/rculist.h>
47 #include <lttng/lttng.h>
48 #include <common/common.h>
49 #include <common/compat/poll.h>
50 #include <common/compat/socket.h>
51 #include <common/compat/endian.h>
52 #include <common/compat/getenv.h>
53 #include <common/defaults.h>
54 #include <common/daemonize.h>
55 #include <common/futex.h>
56 #include <common/sessiond-comm/sessiond-comm.h>
57 #include <common/sessiond-comm/inet.h>
58 #include <common/sessiond-comm/relayd.h>
59 #include <common/uri.h>
60 #include <common/utils.h>
61 #include <common/align.h>
62 #include <common/config/session-config.h>
63 #include <common/dynamic-buffer.h>
64 #include <common/buffer-view.h>
65 #include <common/string-utils/format.h>
66 #include <common/fd-tracker/fd-tracker.h>
67 #include <common/fd-tracker/utils.h>
69 #include "backward-compatibility-group-by.h"
71 #include "connection.h"
72 #include "ctf-trace.h"
73 #include "health-relayd.h"
76 #include "lttng-relayd.h"
78 #include "sessiond-trace-chunks.h"
80 #include "tcp_keep_alive.h"
81 #include "testpoint.h"
82 #include "tracefile-array.h"
85 #include "viewer-stream.h"
87 static const char *help_msg
=
88 #ifdef LTTNG_EMBED_HELP
89 #include <lttng-relayd.8.h>
95 enum relay_connection_status
{
96 RELAY_CONNECTION_STATUS_OK
,
97 /* An error occurred while processing an event on the connection. */
98 RELAY_CONNECTION_STATUS_ERROR
,
99 /* Connection closed/shutdown cleanly. */
100 RELAY_CONNECTION_STATUS_CLOSED
,
103 /* command line options */
104 char *opt_output_path
, *opt_working_directory
;
105 static int opt_daemon
, opt_background
, opt_print_version
, opt_allow_clear
= 1;
106 enum relay_group_output_by opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
;
109 * We need to wait for listener and live listener threads, as well as
110 * health check thread, before being ready to signal readiness.
112 #define NR_LTTNG_RELAY_READY 3
113 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
115 /* Size of receive buffer. */
116 #define RECV_DATA_BUFFER_SIZE 65536
118 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
119 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
121 static struct lttng_uri
*control_uri
;
122 static struct lttng_uri
*data_uri
;
123 static struct lttng_uri
*live_uri
;
125 const char *progname
;
127 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
128 static int tracing_group_name_override
;
130 const char * const config_section_name
= "relayd";
133 * Quit pipe for all threads. This permits a single cancellation point
134 * for all threads when receiving an event on the pipe.
136 int thread_quit_pipe
[2] = { -1, -1 };
139 * This pipe is used to inform the worker thread that a command is queued and
140 * ready to be processed.
142 static int relay_conn_pipe
[2] = { -1, -1 };
144 /* Shared between threads */
145 static int dispatch_thread_exit
;
147 static pthread_t listener_thread
;
148 static pthread_t dispatcher_thread
;
149 static pthread_t worker_thread
;
150 static pthread_t health_thread
;
153 * last_relay_stream_id_lock protects last_relay_stream_id increment
154 * atomicity on 32-bit architectures.
156 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
157 static uint64_t last_relay_stream_id
;
160 * Relay command queue.
162 * The relay_thread_listener and relay_thread_dispatcher communicate with this
165 static struct relay_conn_queue relay_conn_queue
;
167 /* Cap of file desriptors to be in simultaneous use by the relay daemon. */
168 static unsigned int lttng_opt_fd_pool_size
= -1;
170 /* Global relay stream hash table. */
171 struct lttng_ht
*relay_streams_ht
;
173 /* Global relay viewer stream hash table. */
174 struct lttng_ht
*viewer_streams_ht
;
176 /* Global relay sessions hash table. */
177 struct lttng_ht
*sessions_ht
;
179 /* Relayd health monitoring */
180 struct health_app
*health_relayd
;
182 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
184 /* Global fd tracker. */
185 struct fd_tracker
*the_fd_tracker
;
187 static struct option long_options
[] = {
188 { "control-port", 1, 0, 'C', },
189 { "data-port", 1, 0, 'D', },
190 { "live-port", 1, 0, 'L', },
191 { "daemonize", 0, 0, 'd', },
192 { "background", 0, 0, 'b', },
193 { "group", 1, 0, 'g', },
194 { "fd-pool-size", 1, 0, '\0', },
195 { "help", 0, 0, 'h', },
196 { "output", 1, 0, 'o', },
197 { "verbose", 0, 0, 'v', },
198 { "config", 1, 0, 'f' },
199 { "version", 0, 0, 'V' },
200 { "working-directory", 1, 0, 'w', },
201 { "group-output-by-session", 0, 0, 's', },
202 { "group-output-by-host", 0, 0, 'p', },
203 { "disallow-clear", 0, 0, 'x' },
207 static const char *config_ignore_options
[] = { "help", "config", "version" };
209 static void print_version(void) {
210 fprintf(stdout
, "%s\n", VERSION
);
213 static void relayd_config_log(void)
215 DBG("LTTng-relayd " VERSION
" - " VERSION_NAME
"%s%s",
216 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
217 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
218 if (EXTRA_VERSION_DESCRIPTION
[0] != '\0') {
219 DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION
"\n");
221 if (EXTRA_VERSION_PATCHES
[0] != '\0') {
222 DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES
"\n");
227 * Take an option from the getopt output and set it in the right variable to be
230 * Return 0 on success else a negative value.
232 static int set_option(int opt
, const char *arg
, const char *optname
)
238 if (!strcmp(optname
, "fd-pool-size")) {
242 v
= strtoul(arg
, NULL
, 0);
243 if (errno
!= 0 || !isdigit(arg
[0])) {
244 ERR("Wrong value in --fd-pool-size parameter: %s", arg
);
249 ERR("File descriptor cap overflow in --fd-pool-size parameter: %s", arg
);
253 lttng_opt_fd_pool_size
= (unsigned int) v
;
255 fprintf(stderr
, "unknown option %s", optname
);
257 fprintf(stderr
, " with arg %s\n", arg
);
262 if (lttng_is_setuid_setgid()) {
263 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
264 "-C, --control-port");
266 ret
= uri_parse(arg
, &control_uri
);
268 ERR("Invalid control URI specified");
271 if (control_uri
->port
== 0) {
272 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
277 if (lttng_is_setuid_setgid()) {
278 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
281 ret
= uri_parse(arg
, &data_uri
);
283 ERR("Invalid data URI specified");
286 if (data_uri
->port
== 0) {
287 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
292 if (lttng_is_setuid_setgid()) {
293 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
296 ret
= uri_parse(arg
, &live_uri
);
298 ERR("Invalid live URI specified");
301 if (live_uri
->port
== 0) {
302 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
313 if (lttng_is_setuid_setgid()) {
314 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
317 tracing_group_name
= strdup(arg
);
318 if (tracing_group_name
== NULL
) {
323 tracing_group_name_override
= 1;
327 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
329 ERR("Cannot show --help for `lttng-relayd`");
334 opt_print_version
= 1;
337 if (lttng_is_setuid_setgid()) {
338 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
341 ret
= asprintf(&opt_output_path
, "%s", arg
);
344 PERROR("asprintf opt_output_path");
350 if (lttng_is_setuid_setgid()) {
351 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
352 "-w, --working-directory");
354 ret
= asprintf(&opt_working_directory
, "%s", arg
);
357 PERROR("asprintf opt_working_directory");
364 /* Verbose level can increase using multiple -v */
366 lttng_opt_verbose
= config_parse_value(arg
);
368 /* Only 3 level of verbosity (-vvv). */
369 if (lttng_opt_verbose
< 3) {
370 lttng_opt_verbose
+= 1;
375 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
376 ERR("Cannot set --group-output-by-session, another --group-output-by argument is present");
379 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_SESSION
;
382 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
383 ERR("Cannot set --group-output-by-host, another --group-output-by argument is present");
386 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
393 /* Unknown option or other error.
394 * Error is printed by getopt, just return */
407 * config_entry_handler_cb used to handle options read from a config file.
408 * See config_entry_handler_cb comment in common/config/session-config.h for the
409 * return value conventions.
411 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
415 if (!entry
|| !entry
->name
|| !entry
->value
) {
420 /* Check if the option is to be ignored */
421 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
422 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
427 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
428 /* Ignore if entry name is not fully matched. */
429 if (strcmp(entry
->name
, long_options
[i
].name
)) {
434 * If the option takes no argument on the command line,
435 * we have to check if the value is "true". We support
436 * non-zero numeric values, true, on and yes.
438 if (!long_options
[i
].has_arg
) {
439 ret
= config_parse_value(entry
->value
);
442 WARN("Invalid configuration value \"%s\" for option %s",
443 entry
->value
, entry
->name
);
445 /* False, skip boolean config option. */
450 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
454 WARN("Unrecognized option \"%s\" in daemon configuration file.",
461 static int parse_env_options(void)
466 value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV
);
468 opt_working_directory
= strdup(value
);
469 if (!opt_working_directory
) {
470 ERR("Failed to allocate working directory string (\"%s\")",
478 static int set_fd_pool_size(void)
481 struct rlimit rlimit
;
483 ret
= getrlimit(RLIMIT_NOFILE
, &rlimit
);
485 PERROR("Failed to get file descriptor limit");
490 DBG("File descriptor count limits are %" PRIu64
" (soft) and %" PRIu64
" (hard)",
491 (uint64_t) rlimit
.rlim_cur
,
492 (uint64_t) rlimit
.rlim_max
);
493 if (lttng_opt_fd_pool_size
== -1) {
494 /* Use default value (soft limit - reserve). */
495 if (rlimit
.rlim_cur
< DEFAULT_RELAYD_MIN_FD_POOL_SIZE
) {
496 ERR("The process' file number limit is too low (%" PRIu64
"). The process' file number limit must be set to at least %i.",
497 (uint64_t) rlimit
.rlim_cur
, DEFAULT_RELAYD_MIN_FD_POOL_SIZE
);
501 lttng_opt_fd_pool_size
= rlimit
.rlim_cur
-
502 DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE
;
506 if (lttng_opt_fd_pool_size
< DEFAULT_RELAYD_MIN_FD_POOL_SIZE
) {
507 ERR("File descriptor pool size must be set to at least %d",
508 DEFAULT_RELAYD_MIN_FD_POOL_SIZE
);
513 if (lttng_opt_fd_pool_size
> rlimit
.rlim_cur
) {
514 ERR("File descriptor pool size argument (%u) exceeds the process' soft limit (%" PRIu64
").",
515 lttng_opt_fd_pool_size
, (uint64_t) rlimit
.rlim_cur
);
520 DBG("File descriptor pool size argument (%u) adjusted to %u to accomodate transient fd uses",
521 lttng_opt_fd_pool_size
,
522 lttng_opt_fd_pool_size
- DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE
);
523 lttng_opt_fd_pool_size
-= DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE
;
528 static int set_options(int argc
, char **argv
)
530 int c
, ret
= 0, option_index
= 0, retval
= 0;
531 int orig_optopt
= optopt
, orig_optind
= optind
;
532 char *default_address
, *optstring
;
533 const char *config_path
= NULL
;
535 optstring
= utils_generate_optstring(long_options
,
536 sizeof(long_options
) / sizeof(struct option
));
542 /* Check for the --config option */
544 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
545 &option_index
)) != -1) {
549 } else if (c
!= 'f') {
553 if (lttng_is_setuid_setgid()) {
554 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
557 config_path
= utils_expand_path(optarg
);
559 ERR("Failed to resolve path: %s", optarg
);
564 ret
= config_get_section_entries(config_path
, config_section_name
,
565 config_entry_handler
, NULL
);
568 ERR("Invalid configuration option at line %i", ret
);
574 /* Reset getopt's global state */
575 optopt
= orig_optopt
;
576 optind
= orig_optind
;
578 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
583 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
590 /* assign default values */
591 if (control_uri
== NULL
) {
592 ret
= asprintf(&default_address
,
593 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
594 DEFAULT_NETWORK_CONTROL_PORT
);
596 PERROR("asprintf default data address");
601 ret
= uri_parse(default_address
, &control_uri
);
602 free(default_address
);
604 ERR("Invalid control URI specified");
609 if (data_uri
== NULL
) {
610 ret
= asprintf(&default_address
,
611 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
612 DEFAULT_NETWORK_DATA_PORT
);
614 PERROR("asprintf default data address");
619 ret
= uri_parse(default_address
, &data_uri
);
620 free(default_address
);
622 ERR("Invalid data URI specified");
627 if (live_uri
== NULL
) {
628 ret
= asprintf(&default_address
,
629 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
630 DEFAULT_NETWORK_VIEWER_PORT
);
632 PERROR("asprintf default viewer control address");
637 ret
= uri_parse(default_address
, &live_uri
);
638 free(default_address
);
640 ERR("Invalid viewer control URI specified");
645 ret
= set_fd_pool_size();
651 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
652 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
654 if (opt_allow_clear
) {
655 /* Check if env variable exists. */
656 const char *value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
658 ret
= config_parse_value(value
);
660 ERR("Invalid value for %s specified", DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
664 opt_allow_clear
= !ret
;
673 static void print_global_objects(void)
675 print_viewer_streams();
676 print_relay_streams();
680 static int noop_close(void *data
, int *fds
)
685 static void untrack_stdio(void)
687 int fds
[] = { fileno(stdout
), fileno(stderr
) };
690 * noop_close is used since we don't really want to close
691 * the stdio output fds; we merely want to stop tracking them.
693 (void) fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
694 fds
, 2, noop_close
, NULL
);
700 static void relayd_cleanup(void)
702 print_global_objects();
706 if (viewer_streams_ht
)
707 lttng_ht_destroy(viewer_streams_ht
);
708 if (relay_streams_ht
)
709 lttng_ht_destroy(relay_streams_ht
);
711 lttng_ht_destroy(sessions_ht
);
713 free(opt_output_path
);
714 free(opt_working_directory
);
717 health_app_destroy(health_relayd
);
719 /* Close thread quit pipes */
720 if (health_quit_pipe
[0] != -1) {
721 (void) fd_tracker_util_pipe_close(
722 the_fd_tracker
, health_quit_pipe
);
724 if (thread_quit_pipe
[0] != -1) {
725 (void) fd_tracker_util_pipe_close(
726 the_fd_tracker
, thread_quit_pipe
);
728 if (sessiond_trace_chunk_registry
) {
729 sessiond_trace_chunk_registry_destroy(
730 sessiond_trace_chunk_registry
);
732 if (the_fd_tracker
) {
735 * fd_tracker_destroy() will log the contents of the fd-tracker
736 * if a leak is detected.
738 fd_tracker_destroy(the_fd_tracker
);
741 uri_free(control_uri
);
743 /* Live URI is freed in the live thread. */
745 if (tracing_group_name_override
) {
746 free((void *) tracing_group_name
);
751 * Write to writable pipe used to notify a thread.
753 static int notify_thread_pipe(int wpipe
)
757 ret
= lttng_write(wpipe
, "!", 1);
759 PERROR("write poll pipe");
767 static int notify_health_quit_pipe(int *pipe
)
771 ret
= lttng_write(pipe
[1], "4", 1);
773 PERROR("write relay health quit");
782 * Stop all relayd and relayd-live threads.
784 int lttng_relay_stop_threads(void)
788 /* Stopping all threads */
789 DBG("Terminating all threads");
790 if (notify_thread_pipe(thread_quit_pipe
[1])) {
791 ERR("write error on thread quit pipe");
795 if (notify_health_quit_pipe(health_quit_pipe
)) {
796 ERR("write error on health quit pipe");
799 /* Dispatch thread */
800 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
801 futex_nto1_wake(&relay_conn_queue
.futex
);
803 if (relayd_live_stop()) {
804 ERR("Error stopping live threads");
811 * Signal handler for the daemon
813 * Simply stop all worker threads, leaving main() return gracefully after
814 * joining all threads and calling cleanup().
816 static void sighandler(int sig
)
820 DBG("SIGINT caught");
821 if (lttng_relay_stop_threads()) {
822 ERR("Error stopping threads");
826 DBG("SIGTERM caught");
827 if (lttng_relay_stop_threads()) {
828 ERR("Error stopping threads");
832 CMM_STORE_SHARED(recv_child_signal
, 1);
840 * Setup signal handler for :
841 * SIGINT, SIGTERM, SIGPIPE
843 static int set_signal_handler(void)
849 if ((ret
= sigemptyset(&sigset
)) < 0) {
850 PERROR("sigemptyset");
857 sa
.sa_handler
= sighandler
;
858 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
863 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
868 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
873 sa
.sa_handler
= SIG_IGN
;
874 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
879 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
884 void lttng_relay_notify_ready(void)
886 /* Notify the parent of the fork() process that we are ready. */
887 if (opt_daemon
|| opt_background
) {
888 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
889 kill(child_ppid
, SIGUSR1
);
895 * Init thread quit pipe.
897 * Return -1 on error or 0 if all pipes are created.
899 static int init_thread_quit_pipe(void)
901 return fd_tracker_util_pipe_open_cloexec(
902 the_fd_tracker
, "Quit pipe", thread_quit_pipe
);
906 * Init health quit pipe.
908 * Return -1 on error or 0 if all pipes are created.
910 static int init_health_quit_pipe(void)
912 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
913 "Health quit pipe", health_quit_pipe
);
917 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
919 static int create_named_thread_poll_set(struct lttng_poll_event
*events
,
920 int size
, const char *name
)
924 if (events
== NULL
|| size
== 0) {
929 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
930 name
, events
, 1, LTTNG_CLOEXEC
);
933 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
945 * Check if the thread quit pipe was triggered.
947 * Return 1 if it was triggered else 0;
949 static int check_thread_quit_pipe(int fd
, uint32_t events
)
951 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
958 static int create_sock(void *data
, int *out_fd
)
961 struct lttcomm_sock
*sock
= data
;
963 ret
= lttcomm_create_sock(sock
);
973 static int close_sock(void *data
, int *in_fd
)
975 struct lttcomm_sock
*sock
= data
;
977 return sock
->ops
->close(sock
);
981 * Create and init socket from uri.
983 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
,
987 struct lttcomm_sock
*sock
= NULL
;
988 char uri_str
[PATH_MAX
];
989 char *formated_name
= NULL
;
991 sock
= lttcomm_alloc_sock_from_uri(uri
);
993 ERR("Allocating socket");
998 * Don't fail to create the socket if the name can't be built as it is
999 * only used for debugging purposes.
1001 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
1002 uri_str
[sizeof(uri_str
) - 1] = '\0';
1004 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
1007 formated_name
= NULL
;
1011 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
1012 (const char **) (formated_name
? &formated_name
: NULL
),
1013 1, create_sock
, sock
);
1014 free(formated_name
);
1015 DBG("Listening on %s socket %d", name
, sock
->fd
);
1017 ret
= sock
->ops
->bind(sock
);
1019 PERROR("Failed to bind socket");
1023 ret
= sock
->ops
->listen(sock
, -1);
1033 lttcomm_destroy_sock(sock
);
1039 * This thread manages the listening for new connections on the network
1041 static void *relay_thread_listener(void *data
)
1043 int i
, ret
, pollfd
, err
= -1;
1044 uint32_t revents
, nb_fd
;
1045 struct lttng_poll_event events
;
1046 struct lttcomm_sock
*control_sock
, *data_sock
;
1048 DBG("[thread] Relay listener started");
1050 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
1052 health_code_update();
1054 control_sock
= relay_socket_create(control_uri
, "Control listener");
1055 if (!control_sock
) {
1056 goto error_sock_control
;
1059 data_sock
= relay_socket_create(data_uri
, "Data listener");
1061 goto error_sock_relay
;
1065 * Pass 3 as size here for the thread quit pipe, control and
1068 ret
= create_named_thread_poll_set(&events
, 3, "Listener thread epoll");
1070 goto error_create_poll
;
1073 /* Add the control socket */
1074 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
1076 goto error_poll_add
;
1079 /* Add the data socket */
1080 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
1082 goto error_poll_add
;
1085 lttng_relay_notify_ready();
1087 if (testpoint(relayd_thread_listener
)) {
1088 goto error_testpoint
;
1092 health_code_update();
1094 DBG("Listener accepting connections");
1097 health_poll_entry();
1098 ret
= lttng_poll_wait(&events
, -1);
1102 * Restart interrupted system call.
1104 if (errno
== EINTR
) {
1112 DBG("Relay new connection received");
1113 for (i
= 0; i
< nb_fd
; i
++) {
1114 health_code_update();
1116 /* Fetch once the poll data */
1117 revents
= LTTNG_POLL_GETEV(&events
, i
);
1118 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1120 /* Thread quit pipe has been closed. Killing thread. */
1121 ret
= check_thread_quit_pipe(pollfd
, revents
);
1127 if (revents
& LPOLLIN
) {
1129 * A new connection is requested, therefore a
1130 * sessiond/consumerd connection is allocated in
1131 * this thread, enqueued to a global queue and
1132 * dequeued (and freed) in the worker thread.
1135 struct relay_connection
*new_conn
;
1136 struct lttcomm_sock
*newsock
;
1137 enum connection_type type
;
1139 if (pollfd
== data_sock
->fd
) {
1141 newsock
= data_sock
->ops
->accept(data_sock
);
1142 DBG("Relay data connection accepted, socket %d",
1145 assert(pollfd
== control_sock
->fd
);
1146 type
= RELAY_CONTROL
;
1147 newsock
= control_sock
->ops
->accept(control_sock
);
1148 DBG("Relay control connection accepted, socket %d",
1152 PERROR("accepting sock");
1156 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
1159 PERROR("setsockopt inet");
1160 lttcomm_destroy_sock(newsock
);
1164 ret
= socket_apply_keep_alive_config(newsock
->fd
);
1166 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1168 lttcomm_destroy_sock(newsock
);
1172 new_conn
= connection_create(newsock
, type
);
1174 lttcomm_destroy_sock(newsock
);
1178 /* Enqueue request for the dispatcher thread. */
1179 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
1183 * Wake the dispatch queue futex.
1184 * Implicit memory barrier with the
1185 * exchange in cds_wfcq_enqueue.
1187 futex_nto1_wake(&relay_conn_queue
.futex
);
1188 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1189 ERR("socket poll error");
1192 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1202 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
1204 if (data_sock
->fd
>= 0) {
1205 int data_sock_fd
= data_sock
->fd
;
1207 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
1208 &data_sock_fd
, 1, close_sock
,
1211 PERROR("Failed to close the data listener socket file descriptor");
1215 lttcomm_destroy_sock(data_sock
);
1217 if (control_sock
->fd
>= 0) {
1218 int control_sock_fd
= control_sock
->fd
;
1220 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
1221 &control_sock_fd
, 1, close_sock
,
1224 PERROR("Failed to close the control listener socket file descriptor");
1226 control_sock
->fd
= -1;
1228 lttcomm_destroy_sock(control_sock
);
1232 ERR("Health error occurred in %s", __func__
);
1234 health_unregister(health_relayd
);
1235 DBG("Relay listener thread cleanup complete");
1236 lttng_relay_stop_threads();
1241 * This thread manages the dispatching of the requests to worker threads
1243 static void *relay_thread_dispatcher(void *data
)
1247 struct cds_wfcq_node
*node
;
1248 struct relay_connection
*new_conn
= NULL
;
1250 DBG("[thread] Relay dispatcher started");
1252 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1254 if (testpoint(relayd_thread_dispatcher
)) {
1255 goto error_testpoint
;
1258 health_code_update();
1261 health_code_update();
1263 /* Atomically prepare the queue futex */
1264 futex_nto1_prepare(&relay_conn_queue
.futex
);
1266 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1271 health_code_update();
1273 /* Dequeue commands */
1274 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1275 &relay_conn_queue
.tail
);
1277 DBG("Woken up but nothing in the relay command queue");
1278 /* Continue thread execution */
1281 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1283 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1286 * Inform worker thread of the new request. This
1287 * call is blocking so we can be assured that
1288 * the data will be read at some point in time
1289 * or wait to the end of the world :)
1291 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1293 PERROR("write connection pipe");
1294 connection_put(new_conn
);
1297 } while (node
!= NULL
);
1299 /* Futex wait on queue. Blocking call on futex() */
1300 health_poll_entry();
1301 futex_nto1_wait(&relay_conn_queue
.futex
);
1305 /* Normal exit, no error */
1312 ERR("Health error occurred in %s", __func__
);
1314 health_unregister(health_relayd
);
1315 DBG("Dispatch thread dying");
1316 lttng_relay_stop_threads();
1320 static bool session_streams_have_index(const struct relay_session
*session
)
1322 return session
->minor
>= 4 && !session
->snapshot
;
1326 * Handle the RELAYD_CREATE_SESSION command.
1328 * On success, send back the session id or else return a negative value.
1330 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1331 struct relay_connection
*conn
,
1332 const struct lttng_buffer_view
*payload
)
1336 struct relay_session
*session
= NULL
;
1337 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1338 char session_name
[LTTNG_NAME_MAX
] = {};
1339 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1340 uint32_t live_timer
= 0;
1341 bool snapshot
= false;
1342 bool session_name_contains_creation_timestamp
= false;
1343 /* Left nil for peers < 2.11. */
1344 char base_path
[LTTNG_PATH_MAX
] = {};
1345 lttng_uuid sessiond_uuid
= {};
1346 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1347 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1348 LTTNG_OPTIONAL(time_t) creation_time
= {};
1349 struct lttng_dynamic_buffer reply_payload
;
1351 lttng_dynamic_buffer_init(&reply_payload
);
1353 if (conn
->minor
< 4) {
1354 /* From 2.1 to 2.3 */
1356 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1357 /* From 2.4 to 2.10 */
1358 ret
= cmd_create_session_2_4(payload
, session_name
,
1359 hostname
, &live_timer
, &snapshot
);
1361 bool has_current_chunk
;
1362 uint64_t current_chunk_id_value
;
1363 time_t creation_time_value
;
1364 uint64_t id_sessiond_value
;
1366 /* From 2.11 to ... */
1367 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1368 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1369 sessiond_uuid
, &has_current_chunk
,
1370 ¤t_chunk_id_value
, &creation_time_value
,
1371 &session_name_contains_creation_timestamp
);
1372 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1373 /* The nil UUID is reserved for pre-2.11 clients. */
1374 ERR("Illegal nil UUID announced by peer in create session command");
1378 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1379 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1380 if (has_current_chunk
) {
1381 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1382 current_chunk_id_value
);
1390 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1391 snapshot
, sessiond_uuid
,
1392 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1393 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1394 creation_time
.is_set
? &creation_time
.value
: NULL
,
1395 conn
->major
, conn
->minor
,
1396 session_name_contains_creation_timestamp
);
1401 assert(!conn
->session
);
1402 conn
->session
= session
;
1403 DBG("Created session %" PRIu64
, session
->id
);
1405 reply
.generic
.session_id
= htobe64(session
->id
);
1409 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1411 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1414 if (conn
->minor
< 11) {
1415 /* From 2.1 to 2.10 */
1416 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1417 &reply
.generic
, sizeof(reply
.generic
));
1419 ERR("Failed to append \"create session\" command reply header to payload buffer");
1424 const uint32_t output_path_length
=
1425 session
? strlen(session
->output_path
) + 1 : 0;
1427 reply
.output_path_length
= htobe32(output_path_length
);
1428 ret
= lttng_dynamic_buffer_append(
1429 &reply_payload
, &reply
, sizeof(reply
));
1431 ERR("Failed to append \"create session\" command reply header to payload buffer");
1435 if (output_path_length
) {
1436 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1437 session
->output_path
,
1438 output_path_length
);
1440 ERR("Failed to append \"create session\" command reply path to payload buffer");
1446 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1447 reply_payload
.size
, 0);
1448 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1449 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1450 reply_payload
.size
, send_ret
);
1454 if (ret
< 0 && session
) {
1455 session_put(session
);
1457 lttng_dynamic_buffer_reset(&reply_payload
);
1462 * When we have received all the streams and the metadata for a channel,
1463 * we make them visible to the viewer threads.
1465 static void publish_connection_local_streams(struct relay_connection
*conn
)
1467 struct relay_stream
*stream
;
1468 struct relay_session
*session
= conn
->session
;
1471 * We publish all streams belonging to a session atomically wrt
1474 pthread_mutex_lock(&session
->lock
);
1476 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1478 stream_publish(stream
);
1483 * Inform the viewer that there are new streams in the session.
1485 if (session
->viewer_attached
) {
1486 uatomic_set(&session
->new_streams
, 1);
1488 pthread_mutex_unlock(&session
->lock
);
1491 static int conform_channel_path(char *channel_path
)
1495 if (strstr("../", channel_path
)) {
1496 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1502 if (*channel_path
== '/') {
1503 const size_t len
= strlen(channel_path
);
1506 * Channel paths from peers prior to 2.11 are expressed as an
1507 * absolute path that is, in reality, relative to the relay
1508 * daemon's output directory. Remove the leading slash so it
1509 * is correctly interpreted as a relative path later on.
1511 * len (and not len - 1) is used to copy the trailing NULL.
1513 bcopy(channel_path
+ 1, channel_path
, len
);
1520 * relay_add_stream: allocate a new stream for a session
1522 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1523 struct relay_connection
*conn
,
1524 const struct lttng_buffer_view
*payload
)
1528 struct relay_session
*session
= conn
->session
;
1529 struct relay_stream
*stream
= NULL
;
1530 struct lttcomm_relayd_status_stream reply
;
1531 struct ctf_trace
*trace
= NULL
;
1532 uint64_t stream_handle
= -1ULL;
1533 char *path_name
= NULL
, *channel_name
= NULL
;
1534 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1535 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1537 if (!session
|| !conn
->version_check_done
) {
1538 ERR("Trying to add a stream before version check");
1540 goto end_no_session
;
1543 if (session
->minor
== 1) {
1545 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1547 } else if (session
->minor
> 1 && session
->minor
< 11) {
1548 /* From 2.2 to 2.10 */
1549 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1550 &channel_name
, &tracefile_size
, &tracefile_count
);
1552 /* From 2.11 to ... */
1553 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1554 &channel_name
, &tracefile_size
, &tracefile_count
,
1555 &stream_chunk_id
.value
);
1556 stream_chunk_id
.is_set
= true;
1563 if (conform_channel_path(path_name
)) {
1568 * Backward compatibility for --group-output-by-session.
1569 * Prior to lttng 2.11, the complete path is passed by the stream.
1570 * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
1571 * >=2.11 the chunk is responsible for the output path. When dealing
1572 * with producer < 2.11 the chunk output_path is the root output path
1573 * and the stream carries the complete path (path_name).
1574 * To support --group-output-by-session with older producer (<2.11), we
1575 * need to craft the path based on the stream path.
1577 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_SESSION
) {
1578 if (conn
->minor
< 4) {
1580 * From 2.1 to 2.3, the session_name is not passed on
1581 * the RELAYD_CREATE_SESSION command. The session name
1582 * is necessary to detect the presence of a base_path
1583 * inside the stream path. Without it we cannot perform
1584 * a valid group-output-by-session transformation.
1586 WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
1587 " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
1588 session
->id
, path_name
);
1589 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1590 char *group_by_session_path_name
;
1592 assert(session
->session_name
[0] != '\0');
1594 group_by_session_path_name
=
1595 backward_compat_group_by_session(
1597 session
->session_name
);
1598 if (!group_by_session_path_name
) {
1599 ERR("Failed to apply group by session to stream of session %" PRIu64
,
1604 DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
1605 path_name
, group_by_session_path_name
);
1608 path_name
= group_by_session_path_name
;
1612 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1617 /* This stream here has one reference on the trace. */
1618 pthread_mutex_lock(&last_relay_stream_id_lock
);
1619 stream_handle
= ++last_relay_stream_id
;
1620 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1622 /* We pass ownership of path_name and channel_name. */
1623 stream
= stream_create(trace
, stream_handle
, path_name
,
1624 channel_name
, tracefile_size
, tracefile_count
);
1626 channel_name
= NULL
;
1629 * Streams are the owners of their trace. Reference to trace is
1630 * kept within stream_create().
1632 ctf_trace_put(trace
);
1635 memset(&reply
, 0, sizeof(reply
));
1636 reply
.handle
= htobe64(stream_handle
);
1638 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1640 reply
.ret_code
= htobe32(LTTNG_OK
);
1643 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1644 sizeof(struct lttcomm_relayd_status_stream
), 0);
1645 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1646 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1658 * relay_close_stream: close a specific stream
1660 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1661 struct relay_connection
*conn
,
1662 const struct lttng_buffer_view
*payload
)
1666 struct relay_session
*session
= conn
->session
;
1667 struct lttcomm_relayd_close_stream stream_info
;
1668 struct lttcomm_relayd_generic_reply reply
;
1669 struct relay_stream
*stream
;
1671 DBG("Close stream received");
1673 if (!session
|| !conn
->version_check_done
) {
1674 ERR("Trying to close a stream before version check");
1676 goto end_no_session
;
1679 if (payload
->size
< sizeof(stream_info
)) {
1680 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1681 sizeof(stream_info
), payload
->size
);
1683 goto end_no_session
;
1685 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1686 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1687 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1689 stream
= stream_get_by_id(stream_info
.stream_id
);
1696 * Set last_net_seq_num before the close flag. Required by data
1699 pthread_mutex_lock(&stream
->lock
);
1700 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1701 pthread_mutex_unlock(&stream
->lock
);
1704 * This is one of the conditions which may trigger a stream close
1705 * with the others being:
1706 * 1) A close command is received for a stream
1707 * 2) The control connection owning the stream is closed
1708 * 3) We have received all of the stream's data _after_ a close
1711 try_stream_close(stream
);
1712 if (stream
->is_metadata
) {
1713 struct relay_viewer_stream
*vstream
;
1715 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1717 if (stream
->no_new_metadata_notified
) {
1719 * Since all the metadata has been sent to the
1720 * viewer and that we have a request to close
1721 * its stream, we can safely teardown the
1722 * corresponding metadata viewer stream.
1724 viewer_stream_put(vstream
);
1726 /* Put local reference. */
1727 viewer_stream_put(vstream
);
1734 memset(&reply
, 0, sizeof(reply
));
1736 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1738 reply
.ret_code
= htobe32(LTTNG_OK
);
1740 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1741 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1742 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1743 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1753 * relay_reset_metadata: reset a metadata stream
1756 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1757 struct relay_connection
*conn
,
1758 const struct lttng_buffer_view
*payload
)
1762 struct relay_session
*session
= conn
->session
;
1763 struct lttcomm_relayd_reset_metadata stream_info
;
1764 struct lttcomm_relayd_generic_reply reply
;
1765 struct relay_stream
*stream
;
1767 DBG("Reset metadata received");
1769 if (!session
|| !conn
->version_check_done
) {
1770 ERR("Trying to reset a metadata stream before version check");
1772 goto end_no_session
;
1775 if (payload
->size
< sizeof(stream_info
)) {
1776 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1777 sizeof(stream_info
), payload
->size
);
1779 goto end_no_session
;
1781 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1782 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1783 stream_info
.version
= be64toh(stream_info
.version
);
1785 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1787 /* Unsupported for live sessions for now. */
1788 if (session
->live_timer
!= 0) {
1793 stream
= stream_get_by_id(stream_info
.stream_id
);
1798 pthread_mutex_lock(&stream
->lock
);
1799 if (!stream
->is_metadata
) {
1804 ret
= stream_reset_file(stream
);
1806 ERR("Failed to reset metadata stream %" PRIu64
1807 ": stream_path = %s, channel = %s",
1808 stream
->stream_handle
, stream
->path_name
,
1809 stream
->channel_name
);
1813 pthread_mutex_unlock(&stream
->lock
);
1817 memset(&reply
, 0, sizeof(reply
));
1819 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1821 reply
.ret_code
= htobe32(LTTNG_OK
);
1823 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1824 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1825 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1826 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1836 * relay_unknown_command: send -1 if received unknown command
1838 static void relay_unknown_command(struct relay_connection
*conn
)
1840 struct lttcomm_relayd_generic_reply reply
;
1843 memset(&reply
, 0, sizeof(reply
));
1844 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1845 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1846 if (send_ret
< sizeof(reply
)) {
1847 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1852 * relay_start: send an acknowledgment to the client to tell if we are
1853 * ready to receive data. We are ready if a session is established.
1855 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1856 struct relay_connection
*conn
,
1857 const struct lttng_buffer_view
*payload
)
1861 struct lttcomm_relayd_generic_reply reply
;
1862 struct relay_session
*session
= conn
->session
;
1865 DBG("Trying to start the streaming without a session established");
1866 ret
= htobe32(LTTNG_ERR_UNK
);
1869 memset(&reply
, 0, sizeof(reply
));
1870 reply
.ret_code
= htobe32(LTTNG_OK
);
1871 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1873 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1874 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1883 * relay_recv_metadata: receive the metadata for the session.
1885 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1886 struct relay_connection
*conn
,
1887 const struct lttng_buffer_view
*payload
)
1890 struct relay_session
*session
= conn
->session
;
1891 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1892 struct relay_stream
*metadata_stream
;
1893 uint64_t metadata_payload_size
;
1894 struct lttng_buffer_view packet_view
;
1897 ERR("Metadata sent before version check");
1902 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1903 ERR("Incorrect data size");
1907 metadata_payload_size
= recv_hdr
->data_size
-
1908 sizeof(struct lttcomm_relayd_metadata_payload
);
1910 memcpy(&metadata_payload_header
, payload
->data
,
1911 sizeof(metadata_payload_header
));
1912 metadata_payload_header
.stream_id
= be64toh(
1913 metadata_payload_header
.stream_id
);
1914 metadata_payload_header
.padding_size
= be32toh(
1915 metadata_payload_header
.padding_size
);
1917 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1918 if (!metadata_stream
) {
1923 packet_view
= lttng_buffer_view_from_view(payload
,
1924 sizeof(metadata_payload_header
), metadata_payload_size
);
1925 if (!packet_view
.data
) {
1926 ERR("Invalid metadata packet length announced by header");
1931 pthread_mutex_lock(&metadata_stream
->lock
);
1932 ret
= stream_write(metadata_stream
, &packet_view
,
1933 metadata_payload_header
.padding_size
);
1934 pthread_mutex_unlock(&metadata_stream
->lock
);
1940 stream_put(metadata_stream
);
1946 * relay_send_version: send relayd version number
1948 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1949 struct relay_connection
*conn
,
1950 const struct lttng_buffer_view
*payload
)
1954 struct lttcomm_relayd_version reply
, msg
;
1955 bool compatible
= true;
1957 conn
->version_check_done
= true;
1959 /* Get version from the other side. */
1960 if (payload
->size
< sizeof(msg
)) {
1961 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1962 sizeof(msg
), payload
->size
);
1967 memcpy(&msg
, payload
->data
, sizeof(msg
));
1968 msg
.major
= be32toh(msg
.major
);
1969 msg
.minor
= be32toh(msg
.minor
);
1971 memset(&reply
, 0, sizeof(reply
));
1972 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1973 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1975 /* Major versions must be the same */
1976 if (reply
.major
!= msg
.major
) {
1977 DBG("Incompatible major versions (%u vs %u), deleting session",
1978 reply
.major
, msg
.major
);
1982 conn
->major
= reply
.major
;
1983 /* We adapt to the lowest compatible version */
1984 if (reply
.minor
<= msg
.minor
) {
1985 conn
->minor
= reply
.minor
;
1987 conn
->minor
= msg
.minor
;
1990 reply
.major
= htobe32(reply
.major
);
1991 reply
.minor
= htobe32(reply
.minor
);
1992 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1994 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1995 ERR("Failed to send \"send version\" command reply (ret = %zd)",
2008 DBG("Version check done using protocol %u.%u", conn
->major
,
2016 * Check for data pending for a given stream id from the session daemon.
2018 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2019 struct relay_connection
*conn
,
2020 const struct lttng_buffer_view
*payload
)
2022 struct relay_session
*session
= conn
->session
;
2023 struct lttcomm_relayd_data_pending msg
;
2024 struct lttcomm_relayd_generic_reply reply
;
2025 struct relay_stream
*stream
;
2028 uint64_t stream_seq
;
2030 DBG("Data pending command received");
2032 if (!session
|| !conn
->version_check_done
) {
2033 ERR("Trying to check for data before version check");
2035 goto end_no_session
;
2038 if (payload
->size
< sizeof(msg
)) {
2039 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
2040 sizeof(msg
), payload
->size
);
2042 goto end_no_session
;
2044 memcpy(&msg
, payload
->data
, sizeof(msg
));
2045 msg
.stream_id
= be64toh(msg
.stream_id
);
2046 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
2048 stream
= stream_get_by_id(msg
.stream_id
);
2049 if (stream
== NULL
) {
2054 pthread_mutex_lock(&stream
->lock
);
2056 if (session_streams_have_index(session
)) {
2058 * Ensure that both the index and stream data have been
2059 * flushed up to the requested point.
2061 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2063 stream_seq
= stream
->prev_data_seq
;
2065 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
2066 ", prev_index_seq %" PRIu64
2067 ", and last_seq %" PRIu64
, msg
.stream_id
,
2068 stream
->prev_data_seq
, stream
->prev_index_seq
,
2069 msg
.last_net_seq_num
);
2071 /* Avoid wrapping issue */
2072 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
2073 /* Data has in fact been written and is NOT pending */
2076 /* Data still being streamed thus pending */
2080 stream
->data_pending_check_done
= true;
2081 pthread_mutex_unlock(&stream
->lock
);
2086 memset(&reply
, 0, sizeof(reply
));
2087 reply
.ret_code
= htobe32(ret
);
2088 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2089 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2090 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
2100 * Wait for the control socket to reach a quiescent state.
2102 * Note that for now, when receiving this command from the session
2103 * daemon, this means that every subsequent commands or data received on
2104 * the control socket has been handled. So, this is why we simply return
2107 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2108 struct relay_connection
*conn
,
2109 const struct lttng_buffer_view
*payload
)
2113 struct relay_stream
*stream
;
2114 struct lttcomm_relayd_quiescent_control msg
;
2115 struct lttcomm_relayd_generic_reply reply
;
2117 DBG("Checking quiescent state on control socket");
2119 if (!conn
->session
|| !conn
->version_check_done
) {
2120 ERR("Trying to check for data before version check");
2122 goto end_no_session
;
2125 if (payload
->size
< sizeof(msg
)) {
2126 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2127 sizeof(msg
), payload
->size
);
2129 goto end_no_session
;
2131 memcpy(&msg
, payload
->data
, sizeof(msg
));
2132 msg
.stream_id
= be64toh(msg
.stream_id
);
2134 stream
= stream_get_by_id(msg
.stream_id
);
2138 pthread_mutex_lock(&stream
->lock
);
2139 stream
->data_pending_check_done
= true;
2140 pthread_mutex_unlock(&stream
->lock
);
2142 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2145 memset(&reply
, 0, sizeof(reply
));
2146 reply
.ret_code
= htobe32(LTTNG_OK
);
2147 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2148 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2149 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2161 * Initialize a data pending command. This means that a consumer is about
2162 * to ask for data pending for each stream it holds. Simply iterate over
2163 * all streams of a session and set the data_pending_check_done flag.
2165 * This command returns to the client a LTTNG_OK code.
2167 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2168 struct relay_connection
*conn
,
2169 const struct lttng_buffer_view
*payload
)
2173 struct lttng_ht_iter iter
;
2174 struct lttcomm_relayd_begin_data_pending msg
;
2175 struct lttcomm_relayd_generic_reply reply
;
2176 struct relay_stream
*stream
;
2181 DBG("Init streams for data pending");
2183 if (!conn
->session
|| !conn
->version_check_done
) {
2184 ERR("Trying to check for data before version check");
2186 goto end_no_session
;
2189 if (payload
->size
< sizeof(msg
)) {
2190 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2191 sizeof(msg
), payload
->size
);
2193 goto end_no_session
;
2195 memcpy(&msg
, payload
->data
, sizeof(msg
));
2196 msg
.session_id
= be64toh(msg
.session_id
);
2199 * Iterate over all streams to set the begin data pending flag.
2200 * For now, the streams are indexed by stream handle so we have
2201 * to iterate over all streams to find the one associated with
2202 * the right session_id.
2205 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2207 if (!stream_get(stream
)) {
2210 if (stream
->trace
->session
->id
== msg
.session_id
) {
2211 pthread_mutex_lock(&stream
->lock
);
2212 stream
->data_pending_check_done
= false;
2213 pthread_mutex_unlock(&stream
->lock
);
2214 DBG("Set begin data pending flag to stream %" PRIu64
,
2215 stream
->stream_handle
);
2221 memset(&reply
, 0, sizeof(reply
));
2222 /* All good, send back reply. */
2223 reply
.ret_code
= htobe32(LTTNG_OK
);
2225 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2226 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2227 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2239 * End data pending command. This will check, for a given session id, if
2240 * each stream associated with it has its data_pending_check_done flag
2241 * set. If not, this means that the client lost track of the stream but
2242 * the data is still being streamed on our side. In this case, we inform
2243 * the client that data is in flight.
2245 * Return to the client if there is data in flight or not with a ret_code.
2247 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2248 struct relay_connection
*conn
,
2249 const struct lttng_buffer_view
*payload
)
2253 struct lttng_ht_iter iter
;
2254 struct lttcomm_relayd_end_data_pending msg
;
2255 struct lttcomm_relayd_generic_reply reply
;
2256 struct relay_stream
*stream
;
2257 uint32_t is_data_inflight
= 0;
2259 DBG("End data pending command");
2261 if (!conn
->session
|| !conn
->version_check_done
) {
2262 ERR("Trying to check for data before version check");
2264 goto end_no_session
;
2267 if (payload
->size
< sizeof(msg
)) {
2268 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2269 sizeof(msg
), payload
->size
);
2271 goto end_no_session
;
2273 memcpy(&msg
, payload
->data
, sizeof(msg
));
2274 msg
.session_id
= be64toh(msg
.session_id
);
2277 * Iterate over all streams to see if the begin data pending
2281 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2283 if (!stream_get(stream
)) {
2286 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2290 pthread_mutex_lock(&stream
->lock
);
2291 if (!stream
->data_pending_check_done
) {
2292 uint64_t stream_seq
;
2294 if (session_streams_have_index(conn
->session
)) {
2296 * Ensure that both the index and stream data have been
2297 * flushed up to the requested point.
2299 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2301 stream_seq
= stream
->prev_data_seq
;
2303 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2304 is_data_inflight
= 1;
2305 DBG("Data is still in flight for stream %" PRIu64
,
2306 stream
->stream_handle
);
2307 pthread_mutex_unlock(&stream
->lock
);
2312 pthread_mutex_unlock(&stream
->lock
);
2317 memset(&reply
, 0, sizeof(reply
));
2318 /* All good, send back reply. */
2319 reply
.ret_code
= htobe32(is_data_inflight
);
2321 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2322 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2323 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2335 * Receive an index for a specific stream.
2337 * Return 0 on success else a negative value.
2339 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2340 struct relay_connection
*conn
,
2341 const struct lttng_buffer_view
*payload
)
2345 struct relay_session
*session
= conn
->session
;
2346 struct lttcomm_relayd_index index_info
;
2347 struct lttcomm_relayd_generic_reply reply
;
2348 struct relay_stream
*stream
;
2353 DBG("Relay receiving index");
2355 if (!session
|| !conn
->version_check_done
) {
2356 ERR("Trying to close a stream before version check");
2358 goto end_no_session
;
2361 msg_len
= lttcomm_relayd_index_len(
2362 lttng_to_index_major(conn
->major
, conn
->minor
),
2363 lttng_to_index_minor(conn
->major
, conn
->minor
));
2364 if (payload
->size
< msg_len
) {
2365 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2366 msg_len
, payload
->size
);
2368 goto end_no_session
;
2370 memcpy(&index_info
, payload
->data
, msg_len
);
2371 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2372 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2373 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2374 index_info
.content_size
= be64toh(index_info
.content_size
);
2375 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2376 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2377 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2378 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2380 if (conn
->minor
>= 8) {
2381 index_info
.stream_instance_id
=
2382 be64toh(index_info
.stream_instance_id
);
2383 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2385 index_info
.stream_instance_id
= -1ULL;
2386 index_info
.packet_seq_num
= -1ULL;
2389 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2391 ERR("stream_get_by_id not found");
2396 pthread_mutex_lock(&stream
->lock
);
2397 ret
= stream_add_index(stream
, &index_info
);
2398 pthread_mutex_unlock(&stream
->lock
);
2400 goto end_stream_put
;
2406 memset(&reply
, 0, sizeof(reply
));
2408 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2410 reply
.ret_code
= htobe32(LTTNG_OK
);
2412 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2413 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2414 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2423 * Receive the streams_sent message.
2425 * Return 0 on success else a negative value.
2427 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2428 struct relay_connection
*conn
,
2429 const struct lttng_buffer_view
*payload
)
2433 struct lttcomm_relayd_generic_reply reply
;
2437 DBG("Relay receiving streams_sent");
2439 if (!conn
->session
|| !conn
->version_check_done
) {
2440 ERR("Trying to close a stream before version check");
2442 goto end_no_session
;
2446 * Publish every pending stream in the connection recv list which are
2447 * now ready to be used by the viewer.
2449 publish_connection_local_streams(conn
);
2451 memset(&reply
, 0, sizeof(reply
));
2452 reply
.ret_code
= htobe32(LTTNG_OK
);
2453 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2454 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2455 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2468 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2469 * session rotation feature (not the tracefile rotation feature).
2471 static int relay_rotate_session_streams(
2472 const struct lttcomm_relayd_hdr
*recv_hdr
,
2473 struct relay_connection
*conn
,
2474 const struct lttng_buffer_view
*payload
)
2479 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2480 struct relay_session
*session
= conn
->session
;
2481 struct lttcomm_relayd_rotate_streams rotate_streams
;
2482 struct lttcomm_relayd_generic_reply reply
= {};
2483 struct relay_stream
*stream
= NULL
;
2484 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2485 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2486 struct lttng_buffer_view stream_positions
;
2487 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2488 const char *chunk_id_str
= "none";
2490 if (!session
|| !conn
->version_check_done
) {
2491 ERR("Trying to rotate a stream before version check");
2496 if (session
->major
== 2 && session
->minor
< 11) {
2497 ERR("Unsupported feature before 2.11");
2502 if (payload
->size
< header_len
) {
2503 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2504 header_len
, payload
->size
);
2509 memcpy(&rotate_streams
, payload
->data
, header_len
);
2511 /* Convert header to host endianness. */
2512 rotate_streams
= (typeof(rotate_streams
)) {
2513 .stream_count
= be32toh(rotate_streams
.stream_count
),
2514 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2515 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2516 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2520 if (rotate_streams
.new_chunk_id
.is_set
) {
2522 * Retrieve the trace chunk the stream must transition to. As
2523 * per the protocol, this chunk should have been created
2524 * before this command is received.
2526 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2527 sessiond_trace_chunk_registry
,
2528 session
->sessiond_uuid
, session
->id
,
2529 rotate_streams
.new_chunk_id
.value
);
2530 if (!next_trace_chunk
) {
2531 char uuid_str
[LTTNG_UUID_STR_LEN
];
2533 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2534 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2535 ", trace_chunk_id = %" PRIu64
,
2536 uuid_str
, session
->id
,
2537 rotate_streams
.new_chunk_id
.value
);
2538 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2543 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2544 rotate_streams
.new_chunk_id
.value
);
2545 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2546 chunk_id_str
= "formatting error";
2548 chunk_id_str
= chunk_id_buf
;
2552 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2553 rotate_streams
.stream_count
, session
->session_name
,
2556 stream_positions
= lttng_buffer_view_from_view(payload
,
2557 sizeof(rotate_streams
), -1);
2558 if (!stream_positions
.data
||
2559 stream_positions
.size
<
2560 (rotate_streams
.stream_count
*
2561 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2562 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2567 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2568 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2569 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2570 const struct lttcomm_relayd_stream_rotation_position pos
= {
2571 .stream_id
= be64toh(position_comm
->stream_id
),
2572 .rotate_at_seq_num
= be64toh(
2573 position_comm
->rotate_at_seq_num
),
2576 stream
= stream_get_by_id(pos
.stream_id
);
2578 reply_code
= LTTNG_ERR_INVALID
;
2583 pthread_mutex_lock(&stream
->lock
);
2584 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2585 pos
.rotate_at_seq_num
);
2586 pthread_mutex_unlock(&stream
->lock
);
2588 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2596 reply_code
= LTTNG_OK
;
2603 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2604 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2605 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2606 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2607 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2612 lttng_trace_chunk_put(next_trace_chunk
);
2619 * relay_create_trace_chunk: create a new trace chunk
2621 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2622 struct relay_connection
*conn
,
2623 const struct lttng_buffer_view
*payload
)
2627 struct relay_session
*session
= conn
->session
;
2628 struct lttcomm_relayd_create_trace_chunk
*msg
;
2629 struct lttcomm_relayd_generic_reply reply
= {};
2630 struct lttng_buffer_view header_view
;
2631 struct lttng_buffer_view chunk_name_view
;
2632 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2633 enum lttng_error_code reply_code
= LTTNG_OK
;
2634 enum lttng_trace_chunk_status chunk_status
;
2635 struct lttng_directory_handle
*session_output
= NULL
;
2636 const char *new_path
;
2638 if (!session
|| !conn
->version_check_done
) {
2639 ERR("Trying to create a trace chunk before version check");
2644 if (session
->major
== 2 && session
->minor
< 11) {
2645 ERR("Chunk creation command is unsupported before 2.11");
2650 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2651 if (!header_view
.data
) {
2652 ERR("Failed to receive payload of chunk creation command");
2657 /* Convert to host endianness. */
2658 msg
= (typeof(msg
)) header_view
.data
;
2659 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2660 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2661 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2663 if (session
->current_trace_chunk
&&
2664 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2665 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2666 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
2667 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2668 ERR("Failed to rename old chunk");
2670 reply_code
= LTTNG_ERR_UNK
;
2674 session
->ongoing_rotation
= true;
2675 if (!session
->current_trace_chunk
) {
2676 if (!session
->has_rotated
) {
2682 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
2684 chunk
= lttng_trace_chunk_create(
2685 msg
->chunk_id
, msg
->creation_timestamp
, new_path
);
2687 ERR("Failed to create trace chunk in trace chunk creation command");
2689 reply_code
= LTTNG_ERR_NOMEM
;
2693 if (msg
->override_name_length
) {
2696 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2698 msg
->override_name_length
);
2699 name
= chunk_name_view
.data
;
2700 if (!name
|| name
[msg
->override_name_length
- 1]) {
2701 ERR("Failed to receive payload of chunk creation command");
2703 reply_code
= LTTNG_ERR_INVALID
;
2707 chunk_status
= lttng_trace_chunk_override_name(
2708 chunk
, chunk_name_view
.data
);
2709 switch (chunk_status
) {
2710 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2712 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2713 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2714 reply_code
= LTTNG_ERR_INVALID
;
2718 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2719 reply_code
= LTTNG_ERR_UNK
;
2725 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2726 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2727 reply_code
= LTTNG_ERR_UNK
;
2732 session_output
= session_create_output_directory_handle(
2734 if (!session_output
) {
2735 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2738 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, session_output
);
2739 lttng_directory_handle_put(session_output
);
2740 session_output
= NULL
;
2741 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2742 reply_code
= LTTNG_ERR_UNK
;
2747 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2748 sessiond_trace_chunk_registry
,
2749 conn
->session
->sessiond_uuid
,
2752 if (!published_chunk
) {
2753 char uuid_str
[LTTNG_UUID_STR_LEN
];
2755 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2756 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2761 reply_code
= LTTNG_ERR_NOMEM
;
2765 pthread_mutex_lock(&conn
->session
->lock
);
2766 if (conn
->session
->pending_closure_trace_chunk
) {
2768 * Invalid; this means a second create_trace_chunk command was
2769 * received before a close_trace_chunk.
2771 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2772 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2774 goto end_unlock_session
;
2776 conn
->session
->pending_closure_trace_chunk
=
2777 conn
->session
->current_trace_chunk
;
2778 conn
->session
->current_trace_chunk
= published_chunk
;
2779 published_chunk
= NULL
;
2780 if (!conn
->session
->pending_closure_trace_chunk
) {
2781 session
->ongoing_rotation
= false;
2784 pthread_mutex_unlock(&conn
->session
->lock
);
2786 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2787 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2789 sizeof(struct lttcomm_relayd_generic_reply
),
2791 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2792 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2797 lttng_trace_chunk_put(chunk
);
2798 lttng_trace_chunk_put(published_chunk
);
2799 lttng_directory_handle_put(session_output
);
2804 * relay_close_trace_chunk: close a trace chunk
2806 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2807 struct relay_connection
*conn
,
2808 const struct lttng_buffer_view
*payload
)
2810 int ret
= 0, buf_ret
;
2812 struct relay_session
*session
= conn
->session
;
2813 struct lttcomm_relayd_close_trace_chunk
*msg
;
2814 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2815 struct lttng_buffer_view header_view
;
2816 struct lttng_trace_chunk
*chunk
= NULL
;
2817 enum lttng_error_code reply_code
= LTTNG_OK
;
2818 enum lttng_trace_chunk_status chunk_status
;
2820 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2821 time_t close_timestamp
;
2822 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2823 size_t path_length
= 0;
2824 const char *chunk_name
= NULL
;
2825 struct lttng_dynamic_buffer reply_payload
;
2826 const char *new_path
;
2828 lttng_dynamic_buffer_init(&reply_payload
);
2830 if (!session
|| !conn
->version_check_done
) {
2831 ERR("Trying to close a trace chunk before version check");
2836 if (session
->major
== 2 && session
->minor
< 11) {
2837 ERR("Chunk close command is unsupported before 2.11");
2842 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2843 if (!header_view
.data
) {
2844 ERR("Failed to receive payload of chunk close command");
2849 /* Convert to host endianness. */
2850 msg
= (typeof(msg
)) header_view
.data
;
2851 chunk_id
= be64toh(msg
->chunk_id
);
2852 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2853 close_command
= (typeof(close_command
)){
2854 .value
= be32toh(msg
->close_command
.value
),
2855 .is_set
= msg
->close_command
.is_set
,
2858 chunk
= sessiond_trace_chunk_registry_get_chunk(
2859 sessiond_trace_chunk_registry
,
2860 conn
->session
->sessiond_uuid
,
2864 char uuid_str
[LTTNG_UUID_STR_LEN
];
2866 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2867 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2872 reply_code
= LTTNG_ERR_NOMEM
;
2876 pthread_mutex_lock(&session
->lock
);
2877 if (close_command
.is_set
&&
2878 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
) {
2880 * Clear command. It is a protocol error to ask for a
2881 * clear on a relay which does not allow it. Querying
2882 * the configuration allows figuring out whether
2883 * clearing is allowed before doing the clear.
2885 if (!opt_allow_clear
) {
2887 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2888 goto end_unlock_session
;
2891 if (session
->pending_closure_trace_chunk
&&
2892 session
->pending_closure_trace_chunk
!= chunk
) {
2893 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2894 session
->session_name
);
2895 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2897 goto end_unlock_session
;
2900 if (session
->current_trace_chunk
&& session
->current_trace_chunk
!= chunk
&&
2901 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2902 if (close_command
.is_set
&&
2903 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&&
2904 !session
->has_rotated
) {
2905 /* New chunk stays in session output directory. */
2908 /* Use chunk name for new chunk. */
2911 /* Rename new chunk path. */
2912 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2914 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2918 session
->ongoing_rotation
= false;
2920 if ((!close_command
.is_set
||
2921 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) &&
2922 !lttng_trace_chunk_get_name_overridden(chunk
)) {
2923 const char *old_path
;
2925 if (!session
->has_rotated
) {
2930 /* We need to move back the .tmp_old_chunk to its rightful place. */
2931 chunk_status
= lttng_trace_chunk_rename_path(chunk
, old_path
);
2932 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2937 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2938 chunk
, close_timestamp
);
2939 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2940 ERR("Failed to set trace chunk close timestamp");
2942 reply_code
= LTTNG_ERR_UNK
;
2943 goto end_unlock_session
;
2946 if (close_command
.is_set
) {
2947 chunk_status
= lttng_trace_chunk_set_close_command(
2948 chunk
, close_command
.value
);
2949 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2951 reply_code
= LTTNG_ERR_INVALID
;
2952 goto end_unlock_session
;
2955 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2956 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2957 ERR("Failed to get chunk name");
2959 reply_code
= LTTNG_ERR_UNK
;
2960 goto end_unlock_session
;
2962 if (!session
->has_rotated
&& !session
->snapshot
) {
2963 ret
= lttng_strncpy(closed_trace_chunk_path
,
2964 session
->output_path
,
2965 sizeof(closed_trace_chunk_path
));
2967 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2968 strlen(session
->output_path
),
2969 sizeof(closed_trace_chunk_path
));
2970 reply_code
= LTTNG_ERR_NOMEM
;
2972 goto end_unlock_session
;
2975 if (session
->snapshot
) {
2976 ret
= snprintf(closed_trace_chunk_path
,
2977 sizeof(closed_trace_chunk_path
),
2978 "%s/%s", session
->output_path
,
2981 ret
= snprintf(closed_trace_chunk_path
,
2982 sizeof(closed_trace_chunk_path
),
2983 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2985 session
->output_path
, chunk_name
);
2987 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2988 ERR("Failed to format closed trace chunk resulting path");
2989 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2991 goto end_unlock_session
;
2994 if (close_command
.is_set
&&
2995 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
2996 session
->has_rotated
= true;
2998 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2999 path_length
= strlen(closed_trace_chunk_path
) + 1;
3000 if (path_length
> UINT32_MAX
) {
3001 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
3003 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3004 goto end_unlock_session
;
3007 if (session
->current_trace_chunk
== chunk
) {
3009 * After a trace chunk close command, no new streams
3010 * referencing the chunk may be created. Hence, on the
3011 * event that no new trace chunk have been created for
3012 * the session, the reference to the current trace chunk
3013 * is released in order to allow it to be reclaimed when
3014 * the last stream releases its reference to it.
3016 lttng_trace_chunk_put(session
->current_trace_chunk
);
3017 session
->current_trace_chunk
= NULL
;
3019 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
3020 session
->pending_closure_trace_chunk
= NULL
;
3022 pthread_mutex_unlock(&session
->lock
);
3025 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
3026 reply
.path_length
= htobe32((uint32_t) path_length
);
3027 buf_ret
= lttng_dynamic_buffer_append(
3028 &reply_payload
, &reply
, sizeof(reply
));
3030 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
3034 if (reply_code
== LTTNG_OK
) {
3035 buf_ret
= lttng_dynamic_buffer_append(&reply_payload
,
3036 closed_trace_chunk_path
, path_length
);
3038 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
3043 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
3047 if (send_ret
< reply_payload
.size
) {
3048 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
3049 reply_payload
.size
, send_ret
);
3054 lttng_trace_chunk_put(chunk
);
3055 lttng_dynamic_buffer_reset(&reply_payload
);
3060 * relay_trace_chunk_exists: check if a trace chunk exists
3062 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
3063 struct relay_connection
*conn
,
3064 const struct lttng_buffer_view
*payload
)
3068 struct relay_session
*session
= conn
->session
;
3069 struct lttcomm_relayd_trace_chunk_exists
*msg
;
3070 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
3071 struct lttng_buffer_view header_view
;
3075 if (!session
|| !conn
->version_check_done
) {
3076 ERR("Trying to close a trace chunk before version check");
3081 if (session
->major
== 2 && session
->minor
< 11) {
3082 ERR("Chunk close command is unsupported before 2.11");
3087 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3088 if (!header_view
.data
) {
3089 ERR("Failed to receive payload of chunk close command");
3094 /* Convert to host endianness. */
3095 msg
= (typeof(msg
)) header_view
.data
;
3096 chunk_id
= be64toh(msg
->chunk_id
);
3098 ret
= sessiond_trace_chunk_registry_chunk_exists(
3099 sessiond_trace_chunk_registry
,
3100 conn
->session
->sessiond_uuid
,
3102 chunk_id
, &chunk_exists
);
3104 * If ret is not 0, send the reply and report the error to the caller.
3105 * It is a protocol (or internal) error and the session/connection
3106 * should be torn down.
3108 reply
= (typeof(reply
)){
3109 .generic
.ret_code
= htobe32((uint32_t)
3110 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3111 .trace_chunk_exists
= ret
== 0 ? chunk_exists
: 0,
3113 send_ret
= conn
->sock
->ops
->sendmsg(
3114 conn
->sock
, &reply
, sizeof(reply
), 0);
3115 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3116 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
3125 * relay_get_configuration: query whether feature is available
3127 static int relay_get_configuration(const struct lttcomm_relayd_hdr
*recv_hdr
,
3128 struct relay_connection
*conn
,
3129 const struct lttng_buffer_view
*payload
)
3133 struct lttcomm_relayd_get_configuration
*msg
;
3134 struct lttcomm_relayd_get_configuration_reply reply
= {};
3135 struct lttng_buffer_view header_view
;
3136 uint64_t query_flags
= 0;
3137 uint64_t result_flags
= 0;
3139 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3140 if (!header_view
.data
) {
3141 ERR("Failed to receive payload of chunk close command");
3146 /* Convert to host endianness. */
3147 msg
= (typeof(msg
)) header_view
.data
;
3148 query_flags
= be64toh(msg
->query_flags
);
3151 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
3154 if (opt_allow_clear
) {
3155 result_flags
|= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
;
3159 reply
= (typeof(reply
)){
3160 .generic
.ret_code
= htobe32((uint32_t)
3161 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3162 .relayd_configuration_flags
= htobe64(result_flags
),
3164 send_ret
= conn
->sock
->ops
->sendmsg(
3165 conn
->sock
, &reply
, sizeof(reply
), 0);
3166 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3167 ERR("Failed to send \"get configuration\" command reply (ret = %zd)",
3175 #define DBG_CMD(cmd_name, conn) \
3176 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3178 static int relay_process_control_command(struct relay_connection
*conn
,
3179 const struct lttcomm_relayd_hdr
*header
,
3180 const struct lttng_buffer_view
*payload
)
3184 switch (header
->cmd
) {
3185 case RELAYD_CREATE_SESSION
:
3186 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
3187 ret
= relay_create_session(header
, conn
, payload
);
3189 case RELAYD_ADD_STREAM
:
3190 DBG_CMD("RELAYD_ADD_STREAM", conn
);
3191 ret
= relay_add_stream(header
, conn
, payload
);
3193 case RELAYD_START_DATA
:
3194 DBG_CMD("RELAYD_START_DATA", conn
);
3195 ret
= relay_start(header
, conn
, payload
);
3197 case RELAYD_SEND_METADATA
:
3198 DBG_CMD("RELAYD_SEND_METADATA", conn
);
3199 ret
= relay_recv_metadata(header
, conn
, payload
);
3201 case RELAYD_VERSION
:
3202 DBG_CMD("RELAYD_VERSION", conn
);
3203 ret
= relay_send_version(header
, conn
, payload
);
3205 case RELAYD_CLOSE_STREAM
:
3206 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
3207 ret
= relay_close_stream(header
, conn
, payload
);
3209 case RELAYD_DATA_PENDING
:
3210 DBG_CMD("RELAYD_DATA_PENDING", conn
);
3211 ret
= relay_data_pending(header
, conn
, payload
);
3213 case RELAYD_QUIESCENT_CONTROL
:
3214 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
3215 ret
= relay_quiescent_control(header
, conn
, payload
);
3217 case RELAYD_BEGIN_DATA_PENDING
:
3218 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
3219 ret
= relay_begin_data_pending(header
, conn
, payload
);
3221 case RELAYD_END_DATA_PENDING
:
3222 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
3223 ret
= relay_end_data_pending(header
, conn
, payload
);
3225 case RELAYD_SEND_INDEX
:
3226 DBG_CMD("RELAYD_SEND_INDEX", conn
);
3227 ret
= relay_recv_index(header
, conn
, payload
);
3229 case RELAYD_STREAMS_SENT
:
3230 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
3231 ret
= relay_streams_sent(header
, conn
, payload
);
3233 case RELAYD_RESET_METADATA
:
3234 DBG_CMD("RELAYD_RESET_METADATA", conn
);
3235 ret
= relay_reset_metadata(header
, conn
, payload
);
3237 case RELAYD_ROTATE_STREAMS
:
3238 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
3239 ret
= relay_rotate_session_streams(header
, conn
, payload
);
3241 case RELAYD_CREATE_TRACE_CHUNK
:
3242 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
3243 ret
= relay_create_trace_chunk(header
, conn
, payload
);
3245 case RELAYD_CLOSE_TRACE_CHUNK
:
3246 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
3247 ret
= relay_close_trace_chunk(header
, conn
, payload
);
3249 case RELAYD_TRACE_CHUNK_EXISTS
:
3250 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
3251 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
3253 case RELAYD_GET_CONFIGURATION
:
3254 DBG_CMD("RELAYD_GET_CONFIGURATION", conn
);
3255 ret
= relay_get_configuration(header
, conn
, payload
);
3257 case RELAYD_UPDATE_SYNC_INFO
:
3259 ERR("Received unknown command (%u)", header
->cmd
);
3260 relay_unknown_command(conn
);
3269 static enum relay_connection_status
relay_process_control_receive_payload(
3270 struct relay_connection
*conn
)
3273 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3274 struct lttng_dynamic_buffer
*reception_buffer
=
3275 &conn
->protocol
.ctrl
.reception_buffer
;
3276 struct ctrl_connection_state_receive_payload
*state
=
3277 &conn
->protocol
.ctrl
.state
.receive_payload
;
3278 struct lttng_buffer_view payload_view
;
3280 if (state
->left_to_receive
== 0) {
3281 /* Short-circuit for payload-less commands. */
3282 goto reception_complete
;
3285 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3286 reception_buffer
->data
+ state
->received
,
3287 state
->left_to_receive
, MSG_DONTWAIT
);
3289 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3290 PERROR("Unable to receive command payload on sock %d",
3292 status
= RELAY_CONNECTION_STATUS_ERROR
;
3295 } else if (ret
== 0) {
3296 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3297 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3302 assert(ret
<= state
->left_to_receive
);
3304 state
->left_to_receive
-= ret
;
3305 state
->received
+= ret
;
3307 if (state
->left_to_receive
> 0) {
3309 * Can't transition to the protocol's next state, wait to
3310 * receive the rest of the header.
3312 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3313 state
->received
, state
->left_to_receive
,
3319 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3320 conn
->sock
->fd
, state
->received
);
3322 * The payload required to process the command has been received.
3323 * A view to the reception buffer is forwarded to the various
3324 * commands and the state of the control is reset on success.
3326 * Commands are responsible for sending their reply to the peer.
3328 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3330 ret
= relay_process_control_command(conn
,
3331 &state
->header
, &payload_view
);
3333 status
= RELAY_CONNECTION_STATUS_ERROR
;
3337 ret
= connection_reset_protocol_state(conn
);
3339 status
= RELAY_CONNECTION_STATUS_ERROR
;
3345 static enum relay_connection_status
relay_process_control_receive_header(
3346 struct relay_connection
*conn
)
3349 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3350 struct lttcomm_relayd_hdr header
;
3351 struct lttng_dynamic_buffer
*reception_buffer
=
3352 &conn
->protocol
.ctrl
.reception_buffer
;
3353 struct ctrl_connection_state_receive_header
*state
=
3354 &conn
->protocol
.ctrl
.state
.receive_header
;
3356 assert(state
->left_to_receive
!= 0);
3358 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3359 reception_buffer
->data
+ state
->received
,
3360 state
->left_to_receive
, MSG_DONTWAIT
);
3362 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3363 PERROR("Unable to receive control command header on sock %d",
3365 status
= RELAY_CONNECTION_STATUS_ERROR
;
3368 } else if (ret
== 0) {
3369 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3370 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3375 assert(ret
<= state
->left_to_receive
);
3377 state
->left_to_receive
-= ret
;
3378 state
->received
+= ret
;
3380 if (state
->left_to_receive
> 0) {
3382 * Can't transition to the protocol's next state, wait to
3383 * receive the rest of the header.
3385 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3386 state
->received
, state
->left_to_receive
,
3391 /* Transition to next state: receiving the command's payload. */
3392 conn
->protocol
.ctrl
.state_id
=
3393 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3394 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3395 header
.circuit_id
= be64toh(header
.circuit_id
);
3396 header
.data_size
= be64toh(header
.data_size
);
3397 header
.cmd
= be32toh(header
.cmd
);
3398 header
.cmd_version
= be32toh(header
.cmd_version
);
3399 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3400 &header
, sizeof(header
));
3402 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3403 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3406 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3407 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3409 status
= RELAY_CONNECTION_STATUS_ERROR
;
3413 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3415 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3416 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3419 status
= RELAY_CONNECTION_STATUS_ERROR
;
3423 if (header
.data_size
== 0) {
3425 * Manually invoke the next state as the poll loop
3426 * will not wake-up to allow us to proceed further.
3428 status
= relay_process_control_receive_payload(conn
);
3435 * Process the commands received on the control socket
3437 static enum relay_connection_status
relay_process_control(
3438 struct relay_connection
*conn
)
3440 enum relay_connection_status status
;
3442 switch (conn
->protocol
.ctrl
.state_id
) {
3443 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3444 status
= relay_process_control_receive_header(conn
);
3446 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3447 status
= relay_process_control_receive_payload(conn
);
3450 ERR("Unknown control connection protocol state encountered.");
3457 static enum relay_connection_status
relay_process_data_receive_header(
3458 struct relay_connection
*conn
)
3461 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3462 struct data_connection_state_receive_header
*state
=
3463 &conn
->protocol
.data
.state
.receive_header
;
3464 struct lttcomm_relayd_data_hdr header
;
3465 struct relay_stream
*stream
;
3467 assert(state
->left_to_receive
!= 0);
3469 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3470 state
->header_reception_buffer
+ state
->received
,
3471 state
->left_to_receive
, MSG_DONTWAIT
);
3473 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3474 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3475 status
= RELAY_CONNECTION_STATUS_ERROR
;
3478 } else if (ret
== 0) {
3479 /* Orderly shutdown. Not necessary to print an error. */
3480 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3481 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3486 assert(ret
<= state
->left_to_receive
);
3488 state
->left_to_receive
-= ret
;
3489 state
->received
+= ret
;
3491 if (state
->left_to_receive
> 0) {
3493 * Can't transition to the protocol's next state, wait to
3494 * receive the rest of the header.
3496 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3497 state
->received
, state
->left_to_receive
,
3502 /* Transition to next state: receiving the payload. */
3503 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3505 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3506 header
.circuit_id
= be64toh(header
.circuit_id
);
3507 header
.stream_id
= be64toh(header
.stream_id
);
3508 header
.data_size
= be32toh(header
.data_size
);
3509 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3510 header
.padding_size
= be32toh(header
.padding_size
);
3511 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3513 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3515 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3516 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3518 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3519 conn
->sock
->fd
, header
.circuit_id
,
3520 header
.stream_id
, header
.data_size
,
3521 header
.net_seq_num
, header
.padding_size
);
3523 stream
= stream_get_by_id(header
.stream_id
);
3525 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3527 /* Protocol error. */
3528 status
= RELAY_CONNECTION_STATUS_ERROR
;
3532 pthread_mutex_lock(&stream
->lock
);
3533 /* Prepare stream for the reception of a new packet. */
3534 ret
= stream_init_packet(stream
, header
.data_size
,
3535 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3536 pthread_mutex_unlock(&stream
->lock
);
3538 ERR("Failed to rotate stream output file");
3539 status
= RELAY_CONNECTION_STATUS_ERROR
;
3540 goto end_stream_unlock
;
3549 static enum relay_connection_status
relay_process_data_receive_payload(
3550 struct relay_connection
*conn
)
3553 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3554 struct relay_stream
*stream
;
3555 struct data_connection_state_receive_payload
*state
=
3556 &conn
->protocol
.data
.state
.receive_payload
;
3557 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3558 char data_buffer
[chunk_size
];
3559 bool partial_recv
= false;
3560 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3561 uint64_t left_to_receive
= state
->left_to_receive
;
3562 struct relay_session
*session
;
3564 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3565 state
->header
.stream_id
, state
->header
.net_seq_num
,
3566 state
->received
, left_to_receive
);
3568 stream
= stream_get_by_id(state
->header
.stream_id
);
3570 /* Protocol error. */
3571 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3572 state
->header
.stream_id
);
3573 status
= RELAY_CONNECTION_STATUS_ERROR
;
3577 pthread_mutex_lock(&stream
->lock
);
3578 session
= stream
->trace
->session
;
3579 if (!conn
->session
) {
3580 ret
= connection_set_session(conn
, session
);
3582 status
= RELAY_CONNECTION_STATUS_ERROR
;
3583 goto end_stream_unlock
;
3588 * The size of the "chunk" received on any iteration is bounded by:
3589 * - the data left to receive,
3590 * - the data immediately available on the socket,
3591 * - the on-stack data buffer
3593 while (left_to_receive
> 0 && !partial_recv
) {
3594 size_t recv_size
= min(left_to_receive
, chunk_size
);
3595 struct lttng_buffer_view packet_chunk
;
3597 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3598 recv_size
, MSG_DONTWAIT
);
3600 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3601 PERROR("Socket %d error", conn
->sock
->fd
);
3602 status
= RELAY_CONNECTION_STATUS_ERROR
;
3604 goto end_stream_unlock
;
3605 } else if (ret
== 0) {
3606 /* No more data ready to be consumed on socket. */
3607 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3608 state
->header
.stream_id
);
3609 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3611 } else if (ret
< (int) recv_size
) {
3613 * All the data available on the socket has been
3616 partial_recv
= true;
3620 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3622 assert(packet_chunk
.data
);
3624 ret
= stream_write(stream
, &packet_chunk
, 0);
3626 ERR("Relay error writing data to file");
3627 status
= RELAY_CONNECTION_STATUS_ERROR
;
3628 goto end_stream_unlock
;
3631 left_to_receive
-= recv_size
;
3632 state
->received
+= recv_size
;
3633 state
->left_to_receive
= left_to_receive
;
3636 if (state
->left_to_receive
> 0) {
3638 * Did not receive all the data expected, wait for more data to
3639 * become available on the socket.
3641 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3642 state
->header
.stream_id
, state
->received
,
3643 state
->left_to_receive
);
3644 goto end_stream_unlock
;
3647 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3649 status
= RELAY_CONNECTION_STATUS_ERROR
;
3650 goto end_stream_unlock
;
3653 if (session_streams_have_index(session
)) {
3654 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3655 state
->rotate_index
, &index_flushed
,
3656 state
->header
.data_size
+ state
->header
.padding_size
);
3658 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3659 stream
->stream_handle
,
3660 state
->header
.net_seq_num
, ret
);
3661 status
= RELAY_CONNECTION_STATUS_ERROR
;
3662 goto end_stream_unlock
;
3666 if (stream
->prev_data_seq
== -1ULL) {
3670 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3671 state
->header
.padding_size
, state
->header
.net_seq_num
,
3674 status
= RELAY_CONNECTION_STATUS_ERROR
;
3675 goto end_stream_unlock
;
3679 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3680 * contents of *state which are aliased (union) to the same location as
3681 * the new state. Don't use it beyond this point.
3683 connection_reset_protocol_state(conn
);
3687 close_requested
= stream
->close_requested
;
3688 pthread_mutex_unlock(&stream
->lock
);
3689 if (close_requested
&& left_to_receive
== 0) {
3690 try_stream_close(stream
);
3694 pthread_mutex_lock(&session
->lock
);
3695 uatomic_set(&session
->new_streams
, 1);
3696 pthread_mutex_unlock(&session
->lock
);
3705 * relay_process_data: Process the data received on the data socket
3707 static enum relay_connection_status
relay_process_data(
3708 struct relay_connection
*conn
)
3710 enum relay_connection_status status
;
3712 switch (conn
->protocol
.data
.state_id
) {
3713 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3714 status
= relay_process_data_receive_header(conn
);
3716 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3717 status
= relay_process_data_receive_payload(conn
);
3720 ERR("Unexpected data connection communication state.");
3727 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3731 (void) lttng_poll_del(events
, pollfd
);
3733 ret
= close(pollfd
);
3735 ERR("Closing pollfd %d", pollfd
);
3739 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3740 int pollfd
, struct relay_connection
*conn
)
3742 const char *type_str
;
3744 switch (conn
->type
) {
3749 type_str
= "Control";
3751 case RELAY_VIEWER_COMMAND
:
3752 type_str
= "Viewer Command";
3754 case RELAY_VIEWER_NOTIFICATION
:
3755 type_str
= "Viewer Notification";
3758 type_str
= "Unknown";
3760 cleanup_connection_pollfd(events
, pollfd
);
3761 connection_put(conn
);
3762 DBG("%s connection closed with %d", type_str
, pollfd
);
3766 * This thread does the actual work
3768 static void *relay_thread_worker(void *data
)
3770 int ret
, err
= -1, last_seen_data_fd
= -1;
3772 struct lttng_poll_event events
;
3773 struct lttng_ht
*relay_connections_ht
;
3774 struct lttng_ht_iter iter
;
3775 struct relay_connection
*destroy_conn
= NULL
;
3777 DBG("[thread] Relay worker started");
3779 rcu_register_thread();
3781 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3783 if (testpoint(relayd_thread_worker
)) {
3784 goto error_testpoint
;
3787 health_code_update();
3789 /* table of connections indexed on socket */
3790 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3791 if (!relay_connections_ht
) {
3792 goto relay_connections_ht_error
;
3795 ret
= create_named_thread_poll_set(&events
, 2, "Worker thread epoll");
3797 goto error_poll_create
;
3800 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3807 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3809 health_code_update();
3811 /* Infinite blocking call, waiting for transmission */
3812 DBG3("Relayd worker thread polling...");
3813 health_poll_entry();
3814 ret
= lttng_poll_wait(&events
, -1);
3818 * Restart interrupted system call.
3820 if (errno
== EINTR
) {
3829 * Process control. The control connection is
3830 * prioritized so we don't starve it with high
3831 * throughput tracing data on the data connection.
3833 for (i
= 0; i
< nb_fd
; i
++) {
3834 /* Fetch once the poll data */
3835 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3836 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3838 health_code_update();
3840 /* Thread quit pipe has been closed. Killing thread. */
3841 ret
= check_thread_quit_pipe(pollfd
, revents
);
3847 /* Inspect the relay conn pipe for new connection */
3848 if (pollfd
== relay_conn_pipe
[0]) {
3849 if (revents
& LPOLLIN
) {
3850 struct relay_connection
*conn
;
3852 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3856 ret
= lttng_poll_add(&events
,
3858 LPOLLIN
| LPOLLRDHUP
);
3860 ERR("Failed to add new connection file descriptor to poll set");
3863 connection_ht_add(relay_connections_ht
, conn
);
3864 DBG("Connection socket %d added", conn
->sock
->fd
);
3865 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3866 ERR("Relay connection pipe error");
3869 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3873 struct relay_connection
*ctrl_conn
;
3875 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3876 /* If not found, there is a synchronization issue. */
3879 if (ctrl_conn
->type
== RELAY_DATA
) {
3880 if (revents
& LPOLLIN
) {
3882 * Flag the last seen data fd not deleted. It will be
3883 * used as the last seen fd if any fd gets deleted in
3886 last_notdel_data_fd
= pollfd
;
3888 goto put_ctrl_connection
;
3890 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3892 if (revents
& LPOLLIN
) {
3893 enum relay_connection_status status
;
3895 status
= relay_process_control(ctrl_conn
);
3896 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3898 * On socket error flag the session as aborted to force
3899 * the cleanup of its stream otherwise it can leak
3900 * during the lifetime of the relayd.
3902 * This prevents situations in which streams can be
3903 * left opened because an index was received, the
3904 * control connection is closed, and the data
3905 * connection is closed (uncleanly) before the packet's
3908 * Since the control connection encountered an error,
3909 * it is okay to be conservative and close the
3910 * session right now as we can't rely on the protocol
3911 * being respected anymore.
3913 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3914 session_abort(ctrl_conn
->session
);
3917 /* Clear the connection on error or close. */
3918 relay_thread_close_connection(&events
,
3923 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3924 relay_thread_close_connection(&events
,
3926 if (last_seen_data_fd
== pollfd
) {
3927 last_seen_data_fd
= last_notdel_data_fd
;
3930 ERR("Unexpected poll events %u for control sock %d",
3932 connection_put(ctrl_conn
);
3935 put_ctrl_connection
:
3936 connection_put(ctrl_conn
);
3941 * The last loop handled a control request, go back to poll to make
3942 * sure we prioritise the control socket.
3948 if (last_seen_data_fd
>= 0) {
3949 for (i
= 0; i
< nb_fd
; i
++) {
3950 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3952 health_code_update();
3954 if (last_seen_data_fd
== pollfd
) {
3961 /* Process data connection. */
3962 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3963 /* Fetch the poll data. */
3964 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3965 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3966 struct relay_connection
*data_conn
;
3968 health_code_update();
3971 /* No activity for this FD (poll implementation). */
3975 /* Skip the command pipe. It's handled in the first loop. */
3976 if (pollfd
== relay_conn_pipe
[0]) {
3980 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3982 /* Skip it. Might be removed before. */
3985 if (data_conn
->type
== RELAY_CONTROL
) {
3986 goto put_data_connection
;
3988 assert(data_conn
->type
== RELAY_DATA
);
3990 if (revents
& LPOLLIN
) {
3991 enum relay_connection_status status
;
3993 status
= relay_process_data(data_conn
);
3994 /* Connection closed or error. */
3995 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3997 * On socket error flag the session as aborted to force
3998 * the cleanup of its stream otherwise it can leak
3999 * during the lifetime of the relayd.
4001 * This prevents situations in which streams can be
4002 * left opened because an index was received, the
4003 * control connection is closed, and the data
4004 * connection is closed (uncleanly) before the packet's
4007 * Since the data connection encountered an error,
4008 * it is okay to be conservative and close the
4009 * session right now as we can't rely on the protocol
4010 * being respected anymore.
4012 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
4013 session_abort(data_conn
->session
);
4015 relay_thread_close_connection(&events
, pollfd
,
4018 * Every goto restart call sets the last seen fd where
4019 * here we don't really care since we gracefully
4020 * continue the loop after the connection is deleted.
4023 /* Keep last seen port. */
4024 last_seen_data_fd
= pollfd
;
4025 connection_put(data_conn
);
4028 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4029 relay_thread_close_connection(&events
, pollfd
,
4032 ERR("Unknown poll events %u for data sock %d",
4035 put_data_connection
:
4036 connection_put(data_conn
);
4038 last_seen_data_fd
= -1;
4041 /* Normal exit, no error */
4046 /* Cleanup remaining connection object. */
4048 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
4051 health_code_update();
4053 session_abort(destroy_conn
->session
);
4056 * No need to grab another ref, because we own
4059 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
4064 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
4066 lttng_ht_destroy(relay_connections_ht
);
4067 relay_connections_ht_error
:
4068 /* Close relay conn pipes */
4069 (void) fd_tracker_util_pipe_close(the_fd_tracker
,
4072 DBG("Thread exited with error");
4074 DBG("Worker thread cleanup complete");
4078 ERR("Health error occurred in %s", __func__
);
4080 health_unregister(health_relayd
);
4081 rcu_unregister_thread();
4082 lttng_relay_stop_threads();
4087 * Create the relay command pipe to wake thread_manage_apps.
4088 * Closed in cleanup().
4090 static int create_relay_conn_pipe(void)
4092 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
4093 "Relayd connection pipe", relay_conn_pipe
);
4096 static int stdio_open(void *data
, int *fds
)
4098 fds
[0] = fileno(stdout
);
4099 fds
[1] = fileno(stderr
);
4103 static int track_stdio(void)
4106 const char *names
[] = { "stdout", "stderr" };
4108 return fd_tracker_open_unsuspendable_fd(the_fd_tracker
, fds
,
4109 names
, 2, stdio_open
, NULL
);
4115 int main(int argc
, char **argv
)
4117 bool thread_is_rcu_registered
= false;
4118 int ret
= 0, retval
= 0;
4121 /* Parse environment variables */
4122 ret
= parse_env_options();
4130 * Command line arguments overwrite environment.
4133 if (set_options(argc
, argv
)) {
4138 if (set_signal_handler()) {
4143 relayd_config_log();
4145 if (opt_print_version
) {
4151 ret
= fclose(stdin
);
4153 PERROR("Failed to close stdin");
4157 DBG("Clear command %s", opt_allow_clear
? "allowed" : "disallowed");
4159 /* Try to create directory if -o, --output is specified. */
4160 if (opt_output_path
) {
4161 if (*opt_output_path
!= '/') {
4162 ERR("Please specify an absolute path for -o, --output PATH");
4167 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
4170 ERR("Unable to create %s", opt_output_path
);
4177 if (opt_daemon
|| opt_background
) {
4178 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4186 if (opt_working_directory
) {
4187 ret
= utils_change_working_directory(opt_working_directory
);
4189 /* All errors are already logged. */
4194 sessiond_trace_chunk_registry
= sessiond_trace_chunk_registry_create();
4195 if (!sessiond_trace_chunk_registry
) {
4196 ERR("Failed to initialize session daemon trace chunk registry");
4202 * The RCU thread registration (and use, through the fd-tracker's
4203 * creation) is done after the daemonization to allow us to not
4204 * deal with liburcu's fork() management as the call RCU needs to
4207 rcu_register_thread();
4208 thread_is_rcu_registered
= true;
4210 the_fd_tracker
= fd_tracker_create(lttng_opt_fd_pool_size
);
4211 if (!the_fd_tracker
) {
4216 ret
= track_stdio();
4222 /* Initialize thread health monitoring */
4223 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
4224 if (!health_relayd
) {
4225 PERROR("health_app_create error");
4230 /* Create thread quit pipe */
4231 if (init_thread_quit_pipe()) {
4236 /* Setup the thread apps communication pipe. */
4237 if (create_relay_conn_pipe()) {
4242 /* Init relay command queue. */
4243 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
4245 /* Initialize communication library */
4247 lttcomm_inet_init();
4249 /* tables of sessions indexed by session ID */
4250 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4256 /* tables of streams indexed by stream ID */
4257 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4258 if (!relay_streams_ht
) {
4263 /* tables of streams indexed by stream ID */
4264 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4265 if (!viewer_streams_ht
) {
4270 ret
= init_health_quit_pipe();
4276 /* Create thread to manage the client socket */
4277 ret
= pthread_create(&health_thread
, default_pthread_attr(),
4278 thread_manage_health
, (void *) NULL
);
4281 PERROR("pthread_create health");
4286 /* Setup the dispatcher thread */
4287 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
4288 relay_thread_dispatcher
, (void *) NULL
);
4291 PERROR("pthread_create dispatcher");
4293 goto exit_dispatcher_thread
;
4296 /* Setup the worker thread */
4297 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
4298 relay_thread_worker
, NULL
);
4301 PERROR("pthread_create worker");
4303 goto exit_worker_thread
;
4306 /* Setup the listener thread */
4307 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
4308 relay_thread_listener
, (void *) NULL
);
4311 PERROR("pthread_create listener");
4313 goto exit_listener_thread
;
4316 ret
= relayd_live_create(live_uri
);
4318 ERR("Starting live viewer threads");
4324 * This is where we start awaiting program completion (e.g. through
4325 * signal that asks threads to teardown).
4328 ret
= relayd_live_join();
4334 ret
= pthread_join(listener_thread
, &status
);
4337 PERROR("pthread_join listener_thread");
4341 exit_listener_thread
:
4342 ret
= pthread_join(worker_thread
, &status
);
4345 PERROR("pthread_join worker_thread");
4350 ret
= pthread_join(dispatcher_thread
, &status
);
4353 PERROR("pthread_join dispatcher_thread");
4356 exit_dispatcher_thread
:
4358 ret
= pthread_join(health_thread
, &status
);
4361 PERROR("pthread_join health_thread");
4366 * Wait for all pending call_rcu work to complete before tearing
4367 * down data structures. call_rcu worker may be trying to
4368 * perform lookups in those structures.
4373 /* Ensure all prior call_rcu are done. */
4376 if (thread_is_rcu_registered
) {
4377 rcu_unregister_thread();