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_cap
;
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-cap", 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-cap")) {
242 v
= strtoul(arg
, NULL
, 0);
243 if (errno
!= 0 || !isdigit(arg
[0])) {
244 ERR("Wrong value in --fd-cap parameter: %s",
249 if (v
< DEFAULT_RELAYD_MINIMAL_FD_CAP
) {
250 ERR("File descriptor cap must be set to at least %d",
251 DEFAULT_RELAYD_MINIMAL_FD_CAP
);
254 ERR("File descriptor cap overflow in --fd-cap parameter: %s",
259 lttng_opt_fd_cap
= (unsigned int) v
;
260 DBG3("File descriptor cap set to %u", lttng_opt_fd_cap
);
262 fprintf(stderr
, "unknown option %s", optname
);
264 fprintf(stderr
, " with arg %s\n", arg
);
269 if (lttng_is_setuid_setgid()) {
270 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
271 "-C, --control-port");
273 ret
= uri_parse(arg
, &control_uri
);
275 ERR("Invalid control URI specified");
278 if (control_uri
->port
== 0) {
279 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
284 if (lttng_is_setuid_setgid()) {
285 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
288 ret
= uri_parse(arg
, &data_uri
);
290 ERR("Invalid data URI specified");
293 if (data_uri
->port
== 0) {
294 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
299 if (lttng_is_setuid_setgid()) {
300 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
303 ret
= uri_parse(arg
, &live_uri
);
305 ERR("Invalid live URI specified");
308 if (live_uri
->port
== 0) {
309 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
320 if (lttng_is_setuid_setgid()) {
321 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
324 tracing_group_name
= strdup(arg
);
325 if (tracing_group_name
== NULL
) {
330 tracing_group_name_override
= 1;
334 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
336 ERR("Cannot show --help for `lttng-relayd`");
341 opt_print_version
= 1;
344 if (lttng_is_setuid_setgid()) {
345 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
348 ret
= asprintf(&opt_output_path
, "%s", arg
);
351 PERROR("asprintf opt_output_path");
357 if (lttng_is_setuid_setgid()) {
358 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
359 "-w, --working-directory");
361 ret
= asprintf(&opt_working_directory
, "%s", arg
);
364 PERROR("asprintf opt_working_directory");
371 /* Verbose level can increase using multiple -v */
373 lttng_opt_verbose
= config_parse_value(arg
);
375 /* Only 3 level of verbosity (-vvv). */
376 if (lttng_opt_verbose
< 3) {
377 lttng_opt_verbose
+= 1;
382 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
383 ERR("Cannot set --group-output-by-session, another --group-output-by argument is present");
386 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_SESSION
;
389 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
390 ERR("Cannot set --group-output-by-host, another --group-output-by argument is present");
393 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
400 /* Unknown option or other error.
401 * Error is printed by getopt, just return */
414 * config_entry_handler_cb used to handle options read from a config file.
415 * See config_entry_handler_cb comment in common/config/session-config.h for the
416 * return value conventions.
418 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
422 if (!entry
|| !entry
->name
|| !entry
->value
) {
427 /* Check if the option is to be ignored */
428 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
429 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
434 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
435 /* Ignore if entry name is not fully matched. */
436 if (strcmp(entry
->name
, long_options
[i
].name
)) {
441 * If the option takes no argument on the command line,
442 * we have to check if the value is "true". We support
443 * non-zero numeric values, true, on and yes.
445 if (!long_options
[i
].has_arg
) {
446 ret
= config_parse_value(entry
->value
);
449 WARN("Invalid configuration value \"%s\" for option %s",
450 entry
->value
, entry
->name
);
452 /* False, skip boolean config option. */
457 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
461 WARN("Unrecognized option \"%s\" in daemon configuration file.",
468 static int parse_env_options(void)
473 value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV
);
475 opt_working_directory
= strdup(value
);
476 if (!opt_working_directory
) {
477 ERR("Failed to allocate working directory string (\"%s\")",
485 static int set_options(int argc
, char **argv
)
487 int c
, ret
= 0, option_index
= 0, retval
= 0;
488 int orig_optopt
= optopt
, orig_optind
= optind
;
489 char *default_address
, *optstring
;
490 const char *config_path
= NULL
;
492 optstring
= utils_generate_optstring(long_options
,
493 sizeof(long_options
) / sizeof(struct option
));
499 /* Check for the --config option */
501 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
502 &option_index
)) != -1) {
506 } else if (c
!= 'f') {
510 if (lttng_is_setuid_setgid()) {
511 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
514 config_path
= utils_expand_path(optarg
);
516 ERR("Failed to resolve path: %s", optarg
);
521 ret
= config_get_section_entries(config_path
, config_section_name
,
522 config_entry_handler
, NULL
);
525 ERR("Invalid configuration option at line %i", ret
);
531 /* Reset getopt's global state */
532 optopt
= orig_optopt
;
533 optind
= orig_optind
;
535 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
540 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
547 /* assign default values */
548 if (control_uri
== NULL
) {
549 ret
= asprintf(&default_address
,
550 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
551 DEFAULT_NETWORK_CONTROL_PORT
);
553 PERROR("asprintf default data address");
558 ret
= uri_parse(default_address
, &control_uri
);
559 free(default_address
);
561 ERR("Invalid control URI specified");
566 if (data_uri
== NULL
) {
567 ret
= asprintf(&default_address
,
568 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
569 DEFAULT_NETWORK_DATA_PORT
);
571 PERROR("asprintf default data address");
576 ret
= uri_parse(default_address
, &data_uri
);
577 free(default_address
);
579 ERR("Invalid data URI specified");
584 if (live_uri
== NULL
) {
585 ret
= asprintf(&default_address
,
586 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
587 DEFAULT_NETWORK_VIEWER_PORT
);
589 PERROR("asprintf default viewer control address");
594 ret
= uri_parse(default_address
, &live_uri
);
595 free(default_address
);
597 ERR("Invalid viewer control URI specified");
602 if (lttng_opt_fd_cap
== 0) {
604 struct rlimit rlimit
;
606 ret
= getrlimit(RLIMIT_NOFILE
, &rlimit
);
608 PERROR("Failed to get file descriptor limit");
612 lttng_opt_fd_cap
= rlimit
.rlim_cur
;
615 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
616 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
618 if (opt_allow_clear
) {
619 /* Check if env variable exists. */
620 const char *value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
622 ret
= config_parse_value(value
);
624 ERR("Invalid value for %s specified", DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
628 opt_allow_clear
= !ret
;
637 static void print_global_objects(void)
639 print_viewer_streams();
640 print_relay_streams();
647 static void relayd_cleanup(void)
649 print_global_objects();
653 if (viewer_streams_ht
)
654 lttng_ht_destroy(viewer_streams_ht
);
655 if (relay_streams_ht
)
656 lttng_ht_destroy(relay_streams_ht
);
658 lttng_ht_destroy(sessions_ht
);
660 free(opt_output_path
);
661 free(opt_working_directory
);
664 health_app_destroy(health_relayd
);
666 /* Close thread quit pipes */
667 if (health_quit_pipe
[0] != -1) {
668 (void) fd_tracker_util_pipe_close(
669 the_fd_tracker
, health_quit_pipe
);
671 if (thread_quit_pipe
[0] != -1) {
672 (void) fd_tracker_util_pipe_close(
673 the_fd_tracker
, thread_quit_pipe
);
675 if (sessiond_trace_chunk_registry
) {
676 sessiond_trace_chunk_registry_destroy(
677 sessiond_trace_chunk_registry
);
679 if (the_fd_tracker
) {
680 fd_tracker_destroy(the_fd_tracker
);
683 uri_free(control_uri
);
685 /* Live URI is freed in the live thread. */
687 if (tracing_group_name_override
) {
688 free((void *) tracing_group_name
);
690 fd_tracker_log(the_fd_tracker
);
694 * Write to writable pipe used to notify a thread.
696 static int notify_thread_pipe(int wpipe
)
700 ret
= lttng_write(wpipe
, "!", 1);
702 PERROR("write poll pipe");
710 static int notify_health_quit_pipe(int *pipe
)
714 ret
= lttng_write(pipe
[1], "4", 1);
716 PERROR("write relay health quit");
725 * Stop all relayd and relayd-live threads.
727 int lttng_relay_stop_threads(void)
731 /* Stopping all threads */
732 DBG("Terminating all threads");
733 if (notify_thread_pipe(thread_quit_pipe
[1])) {
734 ERR("write error on thread quit pipe");
738 if (notify_health_quit_pipe(health_quit_pipe
)) {
739 ERR("write error on health quit pipe");
742 /* Dispatch thread */
743 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
744 futex_nto1_wake(&relay_conn_queue
.futex
);
746 if (relayd_live_stop()) {
747 ERR("Error stopping live threads");
754 * Signal handler for the daemon
756 * Simply stop all worker threads, leaving main() return gracefully after
757 * joining all threads and calling cleanup().
759 static void sighandler(int sig
)
763 DBG("SIGINT caught");
764 if (lttng_relay_stop_threads()) {
765 ERR("Error stopping threads");
769 DBG("SIGTERM caught");
770 if (lttng_relay_stop_threads()) {
771 ERR("Error stopping threads");
775 CMM_STORE_SHARED(recv_child_signal
, 1);
783 * Setup signal handler for :
784 * SIGINT, SIGTERM, SIGPIPE
786 static int set_signal_handler(void)
792 if ((ret
= sigemptyset(&sigset
)) < 0) {
793 PERROR("sigemptyset");
800 sa
.sa_handler
= sighandler
;
801 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
806 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
811 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
816 sa
.sa_handler
= SIG_IGN
;
817 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
822 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
827 void lttng_relay_notify_ready(void)
829 /* Notify the parent of the fork() process that we are ready. */
830 if (opt_daemon
|| opt_background
) {
831 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
832 kill(child_ppid
, SIGUSR1
);
838 * Init thread quit pipe.
840 * Return -1 on error or 0 if all pipes are created.
842 static int init_thread_quit_pipe(void)
844 return fd_tracker_util_pipe_open_cloexec(
845 the_fd_tracker
, "Quit pipe", thread_quit_pipe
);
849 * Init health quit pipe.
851 * Return -1 on error or 0 if all pipes are created.
853 static int init_health_quit_pipe(void)
855 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
856 "Health quit pipe", health_quit_pipe
);
860 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
862 static int create_named_thread_poll_set(struct lttng_poll_event
*events
,
863 int size
, const char *name
)
867 if (events
== NULL
|| size
== 0) {
872 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
873 name
, events
, 1, LTTNG_CLOEXEC
);
876 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
888 * Check if the thread quit pipe was triggered.
890 * Return 1 if it was triggered else 0;
892 static int check_thread_quit_pipe(int fd
, uint32_t events
)
894 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
902 * Create and init socket from uri.
904 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
907 struct lttcomm_sock
*sock
= NULL
;
909 sock
= lttcomm_alloc_sock_from_uri(uri
);
911 ERR("Allocating socket");
915 ret
= lttcomm_create_sock(sock
);
919 DBG("Listening on sock %d", sock
->fd
);
921 ret
= sock
->ops
->bind(sock
);
923 PERROR("Failed to bind socket");
927 ret
= sock
->ops
->listen(sock
, -1);
937 lttcomm_destroy_sock(sock
);
943 * This thread manages the listening for new connections on the network
945 static void *relay_thread_listener(void *data
)
947 int i
, ret
, pollfd
, err
= -1;
948 uint32_t revents
, nb_fd
;
949 struct lttng_poll_event events
;
950 struct lttcomm_sock
*control_sock
, *data_sock
;
952 DBG("[thread] Relay listener started");
954 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
956 health_code_update();
958 control_sock
= relay_socket_create(control_uri
);
960 goto error_sock_control
;
963 data_sock
= relay_socket_create(data_uri
);
965 goto error_sock_relay
;
969 * Pass 3 as size here for the thread quit pipe, control and
972 ret
= create_named_thread_poll_set(&events
, 3, "Listener thread epoll");
974 goto error_create_poll
;
977 /* Add the control socket */
978 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
983 /* Add the data socket */
984 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
989 lttng_relay_notify_ready();
991 if (testpoint(relayd_thread_listener
)) {
992 goto error_testpoint
;
996 health_code_update();
998 DBG("Listener accepting connections");
1001 health_poll_entry();
1002 ret
= lttng_poll_wait(&events
, -1);
1006 * Restart interrupted system call.
1008 if (errno
== EINTR
) {
1016 DBG("Relay new connection received");
1017 for (i
= 0; i
< nb_fd
; i
++) {
1018 health_code_update();
1020 /* Fetch once the poll data */
1021 revents
= LTTNG_POLL_GETEV(&events
, i
);
1022 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1024 /* Thread quit pipe has been closed. Killing thread. */
1025 ret
= check_thread_quit_pipe(pollfd
, revents
);
1031 if (revents
& LPOLLIN
) {
1033 * A new connection is requested, therefore a
1034 * sessiond/consumerd connection is allocated in
1035 * this thread, enqueued to a global queue and
1036 * dequeued (and freed) in the worker thread.
1039 struct relay_connection
*new_conn
;
1040 struct lttcomm_sock
*newsock
;
1041 enum connection_type type
;
1043 if (pollfd
== data_sock
->fd
) {
1045 newsock
= data_sock
->ops
->accept(data_sock
);
1046 DBG("Relay data connection accepted, socket %d",
1049 assert(pollfd
== control_sock
->fd
);
1050 type
= RELAY_CONTROL
;
1051 newsock
= control_sock
->ops
->accept(control_sock
);
1052 DBG("Relay control connection accepted, socket %d",
1056 PERROR("accepting sock");
1060 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
1063 PERROR("setsockopt inet");
1064 lttcomm_destroy_sock(newsock
);
1068 ret
= socket_apply_keep_alive_config(newsock
->fd
);
1070 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1072 lttcomm_destroy_sock(newsock
);
1076 new_conn
= connection_create(newsock
, type
);
1078 lttcomm_destroy_sock(newsock
);
1082 /* Enqueue request for the dispatcher thread. */
1083 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
1087 * Wake the dispatch queue futex.
1088 * Implicit memory barrier with the
1089 * exchange in cds_wfcq_enqueue.
1091 futex_nto1_wake(&relay_conn_queue
.futex
);
1092 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1093 ERR("socket poll error");
1096 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1106 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
1108 if (data_sock
->fd
>= 0) {
1109 ret
= data_sock
->ops
->close(data_sock
);
1114 lttcomm_destroy_sock(data_sock
);
1116 if (control_sock
->fd
>= 0) {
1117 ret
= control_sock
->ops
->close(control_sock
);
1122 lttcomm_destroy_sock(control_sock
);
1126 ERR("Health error occurred in %s", __func__
);
1128 health_unregister(health_relayd
);
1129 DBG("Relay listener thread cleanup complete");
1130 lttng_relay_stop_threads();
1135 * This thread manages the dispatching of the requests to worker threads
1137 static void *relay_thread_dispatcher(void *data
)
1141 struct cds_wfcq_node
*node
;
1142 struct relay_connection
*new_conn
= NULL
;
1144 DBG("[thread] Relay dispatcher started");
1146 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1148 if (testpoint(relayd_thread_dispatcher
)) {
1149 goto error_testpoint
;
1152 health_code_update();
1155 health_code_update();
1157 /* Atomically prepare the queue futex */
1158 futex_nto1_prepare(&relay_conn_queue
.futex
);
1160 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1165 health_code_update();
1167 /* Dequeue commands */
1168 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1169 &relay_conn_queue
.tail
);
1171 DBG("Woken up but nothing in the relay command queue");
1172 /* Continue thread execution */
1175 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1177 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1180 * Inform worker thread of the new request. This
1181 * call is blocking so we can be assured that
1182 * the data will be read at some point in time
1183 * or wait to the end of the world :)
1185 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1187 PERROR("write connection pipe");
1188 connection_put(new_conn
);
1191 } while (node
!= NULL
);
1193 /* Futex wait on queue. Blocking call on futex() */
1194 health_poll_entry();
1195 futex_nto1_wait(&relay_conn_queue
.futex
);
1199 /* Normal exit, no error */
1206 ERR("Health error occurred in %s", __func__
);
1208 health_unregister(health_relayd
);
1209 DBG("Dispatch thread dying");
1210 lttng_relay_stop_threads();
1214 static bool session_streams_have_index(const struct relay_session
*session
)
1216 return session
->minor
>= 4 && !session
->snapshot
;
1220 * Handle the RELAYD_CREATE_SESSION command.
1222 * On success, send back the session id or else return a negative value.
1224 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1225 struct relay_connection
*conn
,
1226 const struct lttng_buffer_view
*payload
)
1230 struct relay_session
*session
= NULL
;
1231 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1232 char session_name
[LTTNG_NAME_MAX
] = {};
1233 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1234 uint32_t live_timer
= 0;
1235 bool snapshot
= false;
1236 bool session_name_contains_creation_timestamp
= false;
1237 /* Left nil for peers < 2.11. */
1238 char base_path
[LTTNG_PATH_MAX
] = {};
1239 lttng_uuid sessiond_uuid
= {};
1240 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1241 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1242 LTTNG_OPTIONAL(time_t) creation_time
= {};
1243 struct lttng_dynamic_buffer reply_payload
;
1245 lttng_dynamic_buffer_init(&reply_payload
);
1247 if (conn
->minor
< 4) {
1248 /* From 2.1 to 2.3 */
1250 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1251 /* From 2.4 to 2.10 */
1252 ret
= cmd_create_session_2_4(payload
, session_name
,
1253 hostname
, &live_timer
, &snapshot
);
1255 bool has_current_chunk
;
1256 uint64_t current_chunk_id_value
;
1257 time_t creation_time_value
;
1258 uint64_t id_sessiond_value
;
1260 /* From 2.11 to ... */
1261 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1262 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1263 sessiond_uuid
, &has_current_chunk
,
1264 ¤t_chunk_id_value
, &creation_time_value
,
1265 &session_name_contains_creation_timestamp
);
1266 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1267 /* The nil UUID is reserved for pre-2.11 clients. */
1268 ERR("Illegal nil UUID announced by peer in create session command");
1272 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1273 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1274 if (has_current_chunk
) {
1275 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1276 current_chunk_id_value
);
1284 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1285 snapshot
, sessiond_uuid
,
1286 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1287 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1288 creation_time
.is_set
? &creation_time
.value
: NULL
,
1289 conn
->major
, conn
->minor
,
1290 session_name_contains_creation_timestamp
);
1295 assert(!conn
->session
);
1296 conn
->session
= session
;
1297 DBG("Created session %" PRIu64
, session
->id
);
1299 reply
.generic
.session_id
= htobe64(session
->id
);
1303 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1305 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1308 if (conn
->minor
< 11) {
1309 /* From 2.1 to 2.10 */
1310 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1311 &reply
.generic
, sizeof(reply
.generic
));
1313 ERR("Failed to append \"create session\" command reply header to payload buffer");
1318 const uint32_t output_path_length
=
1319 session
? strlen(session
->output_path
) + 1 : 0;
1321 reply
.output_path_length
= htobe32(output_path_length
);
1322 ret
= lttng_dynamic_buffer_append(
1323 &reply_payload
, &reply
, sizeof(reply
));
1325 ERR("Failed to append \"create session\" command reply header to payload buffer");
1329 if (output_path_length
) {
1330 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1331 session
->output_path
,
1332 output_path_length
);
1334 ERR("Failed to append \"create session\" command reply path to payload buffer");
1340 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1341 reply_payload
.size
, 0);
1342 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1343 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1344 reply_payload
.size
, send_ret
);
1348 if (ret
< 0 && session
) {
1349 session_put(session
);
1351 lttng_dynamic_buffer_reset(&reply_payload
);
1356 * When we have received all the streams and the metadata for a channel,
1357 * we make them visible to the viewer threads.
1359 static void publish_connection_local_streams(struct relay_connection
*conn
)
1361 struct relay_stream
*stream
;
1362 struct relay_session
*session
= conn
->session
;
1365 * We publish all streams belonging to a session atomically wrt
1368 pthread_mutex_lock(&session
->lock
);
1370 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1372 stream_publish(stream
);
1377 * Inform the viewer that there are new streams in the session.
1379 if (session
->viewer_attached
) {
1380 uatomic_set(&session
->new_streams
, 1);
1382 pthread_mutex_unlock(&session
->lock
);
1385 static int conform_channel_path(char *channel_path
)
1389 if (strstr("../", channel_path
)) {
1390 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1396 if (*channel_path
== '/') {
1397 const size_t len
= strlen(channel_path
);
1400 * Channel paths from peers prior to 2.11 are expressed as an
1401 * absolute path that is, in reality, relative to the relay
1402 * daemon's output directory. Remove the leading slash so it
1403 * is correctly interpreted as a relative path later on.
1405 * len (and not len - 1) is used to copy the trailing NULL.
1407 bcopy(channel_path
+ 1, channel_path
, len
);
1414 * relay_add_stream: allocate a new stream for a session
1416 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1417 struct relay_connection
*conn
,
1418 const struct lttng_buffer_view
*payload
)
1422 struct relay_session
*session
= conn
->session
;
1423 struct relay_stream
*stream
= NULL
;
1424 struct lttcomm_relayd_status_stream reply
;
1425 struct ctf_trace
*trace
= NULL
;
1426 uint64_t stream_handle
= -1ULL;
1427 char *path_name
= NULL
, *channel_name
= NULL
;
1428 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1429 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1431 if (!session
|| !conn
->version_check_done
) {
1432 ERR("Trying to add a stream before version check");
1434 goto end_no_session
;
1437 if (session
->minor
== 1) {
1439 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1441 } else if (session
->minor
> 1 && session
->minor
< 11) {
1442 /* From 2.2 to 2.10 */
1443 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1444 &channel_name
, &tracefile_size
, &tracefile_count
);
1446 /* From 2.11 to ... */
1447 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1448 &channel_name
, &tracefile_size
, &tracefile_count
,
1449 &stream_chunk_id
.value
);
1450 stream_chunk_id
.is_set
= true;
1457 if (conform_channel_path(path_name
)) {
1462 * Backward compatibility for --group-output-by-session.
1463 * Prior to lttng 2.11, the complete path is passed by the stream.
1464 * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
1465 * >=2.11 the chunk is responsible for the output path. When dealing
1466 * with producer < 2.11 the chunk output_path is the root output path
1467 * and the stream carries the complete path (path_name).
1468 * To support --group-output-by-session with older producer (<2.11), we
1469 * need to craft the path based on the stream path.
1471 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_SESSION
) {
1472 if (conn
->minor
< 4) {
1474 * From 2.1 to 2.3, the session_name is not passed on
1475 * the RELAYD_CREATE_SESSION command. The session name
1476 * is necessary to detect the presence of a base_path
1477 * inside the stream path. Without it we cannot perform
1478 * a valid group-output-by-session transformation.
1480 WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
1481 " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
1482 session
->id
, path_name
);
1483 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1484 char *group_by_session_path_name
;
1486 assert(session
->session_name
[0] != '\0');
1488 group_by_session_path_name
=
1489 backward_compat_group_by_session(
1491 session
->session_name
);
1492 if (!group_by_session_path_name
) {
1493 ERR("Failed to apply group by session to stream of session %" PRIu64
,
1498 DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
1499 path_name
, group_by_session_path_name
);
1502 path_name
= group_by_session_path_name
;
1506 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1511 /* This stream here has one reference on the trace. */
1512 pthread_mutex_lock(&last_relay_stream_id_lock
);
1513 stream_handle
= ++last_relay_stream_id
;
1514 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1516 /* We pass ownership of path_name and channel_name. */
1517 stream
= stream_create(trace
, stream_handle
, path_name
,
1518 channel_name
, tracefile_size
, tracefile_count
);
1520 channel_name
= NULL
;
1523 * Streams are the owners of their trace. Reference to trace is
1524 * kept within stream_create().
1526 ctf_trace_put(trace
);
1529 memset(&reply
, 0, sizeof(reply
));
1530 reply
.handle
= htobe64(stream_handle
);
1532 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1534 reply
.ret_code
= htobe32(LTTNG_OK
);
1537 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1538 sizeof(struct lttcomm_relayd_status_stream
), 0);
1539 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1540 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1552 * relay_close_stream: close a specific stream
1554 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1555 struct relay_connection
*conn
,
1556 const struct lttng_buffer_view
*payload
)
1560 struct relay_session
*session
= conn
->session
;
1561 struct lttcomm_relayd_close_stream stream_info
;
1562 struct lttcomm_relayd_generic_reply reply
;
1563 struct relay_stream
*stream
;
1565 DBG("Close stream received");
1567 if (!session
|| !conn
->version_check_done
) {
1568 ERR("Trying to close a stream before version check");
1570 goto end_no_session
;
1573 if (payload
->size
< sizeof(stream_info
)) {
1574 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1575 sizeof(stream_info
), payload
->size
);
1577 goto end_no_session
;
1579 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1580 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1581 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1583 stream
= stream_get_by_id(stream_info
.stream_id
);
1590 * Set last_net_seq_num before the close flag. Required by data
1593 pthread_mutex_lock(&stream
->lock
);
1594 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1595 pthread_mutex_unlock(&stream
->lock
);
1598 * This is one of the conditions which may trigger a stream close
1599 * with the others being:
1600 * 1) A close command is received for a stream
1601 * 2) The control connection owning the stream is closed
1602 * 3) We have received all of the stream's data _after_ a close
1605 try_stream_close(stream
);
1606 if (stream
->is_metadata
) {
1607 struct relay_viewer_stream
*vstream
;
1609 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1611 if (stream
->no_new_metadata_notified
) {
1613 * Since all the metadata has been sent to the
1614 * viewer and that we have a request to close
1615 * its stream, we can safely teardown the
1616 * corresponding metadata viewer stream.
1618 viewer_stream_put(vstream
);
1620 /* Put local reference. */
1621 viewer_stream_put(vstream
);
1628 memset(&reply
, 0, sizeof(reply
));
1630 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1632 reply
.ret_code
= htobe32(LTTNG_OK
);
1634 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1635 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1636 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1637 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1647 * relay_reset_metadata: reset a metadata stream
1650 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1651 struct relay_connection
*conn
,
1652 const struct lttng_buffer_view
*payload
)
1656 struct relay_session
*session
= conn
->session
;
1657 struct lttcomm_relayd_reset_metadata stream_info
;
1658 struct lttcomm_relayd_generic_reply reply
;
1659 struct relay_stream
*stream
;
1661 DBG("Reset metadata received");
1663 if (!session
|| !conn
->version_check_done
) {
1664 ERR("Trying to reset a metadata stream before version check");
1666 goto end_no_session
;
1669 if (payload
->size
< sizeof(stream_info
)) {
1670 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1671 sizeof(stream_info
), payload
->size
);
1673 goto end_no_session
;
1675 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1676 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1677 stream_info
.version
= be64toh(stream_info
.version
);
1679 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1681 /* Unsupported for live sessions for now. */
1682 if (session
->live_timer
!= 0) {
1687 stream
= stream_get_by_id(stream_info
.stream_id
);
1692 pthread_mutex_lock(&stream
->lock
);
1693 if (!stream
->is_metadata
) {
1698 ret
= stream_reset_file(stream
);
1700 ERR("Failed to reset metadata stream %" PRIu64
1701 ": stream_path = %s, channel = %s",
1702 stream
->stream_handle
, stream
->path_name
,
1703 stream
->channel_name
);
1707 pthread_mutex_unlock(&stream
->lock
);
1711 memset(&reply
, 0, sizeof(reply
));
1713 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1715 reply
.ret_code
= htobe32(LTTNG_OK
);
1717 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1718 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1719 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1720 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1730 * relay_unknown_command: send -1 if received unknown command
1732 static void relay_unknown_command(struct relay_connection
*conn
)
1734 struct lttcomm_relayd_generic_reply reply
;
1737 memset(&reply
, 0, sizeof(reply
));
1738 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1739 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1740 if (send_ret
< sizeof(reply
)) {
1741 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1746 * relay_start: send an acknowledgment to the client to tell if we are
1747 * ready to receive data. We are ready if a session is established.
1749 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1750 struct relay_connection
*conn
,
1751 const struct lttng_buffer_view
*payload
)
1755 struct lttcomm_relayd_generic_reply reply
;
1756 struct relay_session
*session
= conn
->session
;
1759 DBG("Trying to start the streaming without a session established");
1760 ret
= htobe32(LTTNG_ERR_UNK
);
1763 memset(&reply
, 0, sizeof(reply
));
1764 reply
.ret_code
= htobe32(LTTNG_OK
);
1765 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1767 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1768 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1777 * relay_recv_metadata: receive the metadata for the session.
1779 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1780 struct relay_connection
*conn
,
1781 const struct lttng_buffer_view
*payload
)
1784 struct relay_session
*session
= conn
->session
;
1785 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1786 struct relay_stream
*metadata_stream
;
1787 uint64_t metadata_payload_size
;
1788 struct lttng_buffer_view packet_view
;
1791 ERR("Metadata sent before version check");
1796 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1797 ERR("Incorrect data size");
1801 metadata_payload_size
= recv_hdr
->data_size
-
1802 sizeof(struct lttcomm_relayd_metadata_payload
);
1804 memcpy(&metadata_payload_header
, payload
->data
,
1805 sizeof(metadata_payload_header
));
1806 metadata_payload_header
.stream_id
= be64toh(
1807 metadata_payload_header
.stream_id
);
1808 metadata_payload_header
.padding_size
= be32toh(
1809 metadata_payload_header
.padding_size
);
1811 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1812 if (!metadata_stream
) {
1817 packet_view
= lttng_buffer_view_from_view(payload
,
1818 sizeof(metadata_payload_header
), metadata_payload_size
);
1819 if (!packet_view
.data
) {
1820 ERR("Invalid metadata packet length announced by header");
1825 pthread_mutex_lock(&metadata_stream
->lock
);
1826 ret
= stream_write(metadata_stream
, &packet_view
,
1827 metadata_payload_header
.padding_size
);
1828 pthread_mutex_unlock(&metadata_stream
->lock
);
1834 stream_put(metadata_stream
);
1840 * relay_send_version: send relayd version number
1842 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1843 struct relay_connection
*conn
,
1844 const struct lttng_buffer_view
*payload
)
1848 struct lttcomm_relayd_version reply
, msg
;
1849 bool compatible
= true;
1851 conn
->version_check_done
= true;
1853 /* Get version from the other side. */
1854 if (payload
->size
< sizeof(msg
)) {
1855 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1856 sizeof(msg
), payload
->size
);
1861 memcpy(&msg
, payload
->data
, sizeof(msg
));
1862 msg
.major
= be32toh(msg
.major
);
1863 msg
.minor
= be32toh(msg
.minor
);
1865 memset(&reply
, 0, sizeof(reply
));
1866 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1867 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1869 /* Major versions must be the same */
1870 if (reply
.major
!= msg
.major
) {
1871 DBG("Incompatible major versions (%u vs %u), deleting session",
1872 reply
.major
, msg
.major
);
1876 conn
->major
= reply
.major
;
1877 /* We adapt to the lowest compatible version */
1878 if (reply
.minor
<= msg
.minor
) {
1879 conn
->minor
= reply
.minor
;
1881 conn
->minor
= msg
.minor
;
1884 reply
.major
= htobe32(reply
.major
);
1885 reply
.minor
= htobe32(reply
.minor
);
1886 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1888 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1889 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1902 DBG("Version check done using protocol %u.%u", conn
->major
,
1910 * Check for data pending for a given stream id from the session daemon.
1912 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1913 struct relay_connection
*conn
,
1914 const struct lttng_buffer_view
*payload
)
1916 struct relay_session
*session
= conn
->session
;
1917 struct lttcomm_relayd_data_pending msg
;
1918 struct lttcomm_relayd_generic_reply reply
;
1919 struct relay_stream
*stream
;
1922 uint64_t stream_seq
;
1924 DBG("Data pending command received");
1926 if (!session
|| !conn
->version_check_done
) {
1927 ERR("Trying to check for data before version check");
1929 goto end_no_session
;
1932 if (payload
->size
< sizeof(msg
)) {
1933 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1934 sizeof(msg
), payload
->size
);
1936 goto end_no_session
;
1938 memcpy(&msg
, payload
->data
, sizeof(msg
));
1939 msg
.stream_id
= be64toh(msg
.stream_id
);
1940 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1942 stream
= stream_get_by_id(msg
.stream_id
);
1943 if (stream
== NULL
) {
1948 pthread_mutex_lock(&stream
->lock
);
1950 if (session_streams_have_index(session
)) {
1952 * Ensure that both the index and stream data have been
1953 * flushed up to the requested point.
1955 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1957 stream_seq
= stream
->prev_data_seq
;
1959 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
1960 ", prev_index_seq %" PRIu64
1961 ", and last_seq %" PRIu64
, msg
.stream_id
,
1962 stream
->prev_data_seq
, stream
->prev_index_seq
,
1963 msg
.last_net_seq_num
);
1965 /* Avoid wrapping issue */
1966 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
1967 /* Data has in fact been written and is NOT pending */
1970 /* Data still being streamed thus pending */
1974 stream
->data_pending_check_done
= true;
1975 pthread_mutex_unlock(&stream
->lock
);
1980 memset(&reply
, 0, sizeof(reply
));
1981 reply
.ret_code
= htobe32(ret
);
1982 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1983 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1984 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1994 * Wait for the control socket to reach a quiescent state.
1996 * Note that for now, when receiving this command from the session
1997 * daemon, this means that every subsequent commands or data received on
1998 * the control socket has been handled. So, this is why we simply return
2001 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2002 struct relay_connection
*conn
,
2003 const struct lttng_buffer_view
*payload
)
2007 struct relay_stream
*stream
;
2008 struct lttcomm_relayd_quiescent_control msg
;
2009 struct lttcomm_relayd_generic_reply reply
;
2011 DBG("Checking quiescent state on control socket");
2013 if (!conn
->session
|| !conn
->version_check_done
) {
2014 ERR("Trying to check for data before version check");
2016 goto end_no_session
;
2019 if (payload
->size
< sizeof(msg
)) {
2020 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2021 sizeof(msg
), payload
->size
);
2023 goto end_no_session
;
2025 memcpy(&msg
, payload
->data
, sizeof(msg
));
2026 msg
.stream_id
= be64toh(msg
.stream_id
);
2028 stream
= stream_get_by_id(msg
.stream_id
);
2032 pthread_mutex_lock(&stream
->lock
);
2033 stream
->data_pending_check_done
= true;
2034 pthread_mutex_unlock(&stream
->lock
);
2036 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2039 memset(&reply
, 0, sizeof(reply
));
2040 reply
.ret_code
= htobe32(LTTNG_OK
);
2041 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2042 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2043 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2055 * Initialize a data pending command. This means that a consumer is about
2056 * to ask for data pending for each stream it holds. Simply iterate over
2057 * all streams of a session and set the data_pending_check_done flag.
2059 * This command returns to the client a LTTNG_OK code.
2061 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2062 struct relay_connection
*conn
,
2063 const struct lttng_buffer_view
*payload
)
2067 struct lttng_ht_iter iter
;
2068 struct lttcomm_relayd_begin_data_pending msg
;
2069 struct lttcomm_relayd_generic_reply reply
;
2070 struct relay_stream
*stream
;
2075 DBG("Init streams for data pending");
2077 if (!conn
->session
|| !conn
->version_check_done
) {
2078 ERR("Trying to check for data before version check");
2080 goto end_no_session
;
2083 if (payload
->size
< sizeof(msg
)) {
2084 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2085 sizeof(msg
), payload
->size
);
2087 goto end_no_session
;
2089 memcpy(&msg
, payload
->data
, sizeof(msg
));
2090 msg
.session_id
= be64toh(msg
.session_id
);
2093 * Iterate over all streams to set the begin data pending flag.
2094 * For now, the streams are indexed by stream handle so we have
2095 * to iterate over all streams to find the one associated with
2096 * the right session_id.
2099 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2101 if (!stream_get(stream
)) {
2104 if (stream
->trace
->session
->id
== msg
.session_id
) {
2105 pthread_mutex_lock(&stream
->lock
);
2106 stream
->data_pending_check_done
= false;
2107 pthread_mutex_unlock(&stream
->lock
);
2108 DBG("Set begin data pending flag to stream %" PRIu64
,
2109 stream
->stream_handle
);
2115 memset(&reply
, 0, sizeof(reply
));
2116 /* All good, send back reply. */
2117 reply
.ret_code
= htobe32(LTTNG_OK
);
2119 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2120 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2121 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2133 * End data pending command. This will check, for a given session id, if
2134 * each stream associated with it has its data_pending_check_done flag
2135 * set. If not, this means that the client lost track of the stream but
2136 * the data is still being streamed on our side. In this case, we inform
2137 * the client that data is in flight.
2139 * Return to the client if there is data in flight or not with a ret_code.
2141 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2142 struct relay_connection
*conn
,
2143 const struct lttng_buffer_view
*payload
)
2147 struct lttng_ht_iter iter
;
2148 struct lttcomm_relayd_end_data_pending msg
;
2149 struct lttcomm_relayd_generic_reply reply
;
2150 struct relay_stream
*stream
;
2151 uint32_t is_data_inflight
= 0;
2153 DBG("End data pending command");
2155 if (!conn
->session
|| !conn
->version_check_done
) {
2156 ERR("Trying to check for data before version check");
2158 goto end_no_session
;
2161 if (payload
->size
< sizeof(msg
)) {
2162 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2163 sizeof(msg
), payload
->size
);
2165 goto end_no_session
;
2167 memcpy(&msg
, payload
->data
, sizeof(msg
));
2168 msg
.session_id
= be64toh(msg
.session_id
);
2171 * Iterate over all streams to see if the begin data pending
2175 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2177 if (!stream_get(stream
)) {
2180 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2184 pthread_mutex_lock(&stream
->lock
);
2185 if (!stream
->data_pending_check_done
) {
2186 uint64_t stream_seq
;
2188 if (session_streams_have_index(conn
->session
)) {
2190 * Ensure that both the index and stream data have been
2191 * flushed up to the requested point.
2193 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2195 stream_seq
= stream
->prev_data_seq
;
2197 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2198 is_data_inflight
= 1;
2199 DBG("Data is still in flight for stream %" PRIu64
,
2200 stream
->stream_handle
);
2201 pthread_mutex_unlock(&stream
->lock
);
2206 pthread_mutex_unlock(&stream
->lock
);
2211 memset(&reply
, 0, sizeof(reply
));
2212 /* All good, send back reply. */
2213 reply
.ret_code
= htobe32(is_data_inflight
);
2215 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2216 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2217 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2229 * Receive an index for a specific stream.
2231 * Return 0 on success else a negative value.
2233 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2234 struct relay_connection
*conn
,
2235 const struct lttng_buffer_view
*payload
)
2239 struct relay_session
*session
= conn
->session
;
2240 struct lttcomm_relayd_index index_info
;
2241 struct lttcomm_relayd_generic_reply reply
;
2242 struct relay_stream
*stream
;
2247 DBG("Relay receiving index");
2249 if (!session
|| !conn
->version_check_done
) {
2250 ERR("Trying to close a stream before version check");
2252 goto end_no_session
;
2255 msg_len
= lttcomm_relayd_index_len(
2256 lttng_to_index_major(conn
->major
, conn
->minor
),
2257 lttng_to_index_minor(conn
->major
, conn
->minor
));
2258 if (payload
->size
< msg_len
) {
2259 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2260 msg_len
, payload
->size
);
2262 goto end_no_session
;
2264 memcpy(&index_info
, payload
->data
, msg_len
);
2265 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2266 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2267 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2268 index_info
.content_size
= be64toh(index_info
.content_size
);
2269 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2270 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2271 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2272 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2274 if (conn
->minor
>= 8) {
2275 index_info
.stream_instance_id
=
2276 be64toh(index_info
.stream_instance_id
);
2277 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2279 index_info
.stream_instance_id
= -1ULL;
2280 index_info
.packet_seq_num
= -1ULL;
2283 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2285 ERR("stream_get_by_id not found");
2290 pthread_mutex_lock(&stream
->lock
);
2291 ret
= stream_add_index(stream
, &index_info
);
2292 pthread_mutex_unlock(&stream
->lock
);
2294 goto end_stream_put
;
2300 memset(&reply
, 0, sizeof(reply
));
2302 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2304 reply
.ret_code
= htobe32(LTTNG_OK
);
2306 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2307 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2308 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2317 * Receive the streams_sent message.
2319 * Return 0 on success else a negative value.
2321 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2322 struct relay_connection
*conn
,
2323 const struct lttng_buffer_view
*payload
)
2327 struct lttcomm_relayd_generic_reply reply
;
2331 DBG("Relay receiving streams_sent");
2333 if (!conn
->session
|| !conn
->version_check_done
) {
2334 ERR("Trying to close a stream before version check");
2336 goto end_no_session
;
2340 * Publish every pending stream in the connection recv list which are
2341 * now ready to be used by the viewer.
2343 publish_connection_local_streams(conn
);
2345 memset(&reply
, 0, sizeof(reply
));
2346 reply
.ret_code
= htobe32(LTTNG_OK
);
2347 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2348 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2349 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2362 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2363 * session rotation feature (not the tracefile rotation feature).
2365 static int relay_rotate_session_streams(
2366 const struct lttcomm_relayd_hdr
*recv_hdr
,
2367 struct relay_connection
*conn
,
2368 const struct lttng_buffer_view
*payload
)
2373 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2374 struct relay_session
*session
= conn
->session
;
2375 struct lttcomm_relayd_rotate_streams rotate_streams
;
2376 struct lttcomm_relayd_generic_reply reply
= {};
2377 struct relay_stream
*stream
= NULL
;
2378 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2379 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2380 struct lttng_buffer_view stream_positions
;
2381 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2382 const char *chunk_id_str
= "none";
2384 if (!session
|| !conn
->version_check_done
) {
2385 ERR("Trying to rotate a stream before version check");
2390 if (session
->major
== 2 && session
->minor
< 11) {
2391 ERR("Unsupported feature before 2.11");
2396 if (payload
->size
< header_len
) {
2397 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2398 header_len
, payload
->size
);
2403 memcpy(&rotate_streams
, payload
->data
, header_len
);
2405 /* Convert header to host endianness. */
2406 rotate_streams
= (typeof(rotate_streams
)) {
2407 .stream_count
= be32toh(rotate_streams
.stream_count
),
2408 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2409 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2410 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2414 if (rotate_streams
.new_chunk_id
.is_set
) {
2416 * Retrieve the trace chunk the stream must transition to. As
2417 * per the protocol, this chunk should have been created
2418 * before this command is received.
2420 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2421 sessiond_trace_chunk_registry
,
2422 session
->sessiond_uuid
, session
->id
,
2423 rotate_streams
.new_chunk_id
.value
);
2424 if (!next_trace_chunk
) {
2425 char uuid_str
[LTTNG_UUID_STR_LEN
];
2427 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2428 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2429 ", trace_chunk_id = %" PRIu64
,
2430 uuid_str
, session
->id
,
2431 rotate_streams
.new_chunk_id
.value
);
2432 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2437 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2438 rotate_streams
.new_chunk_id
.value
);
2439 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2440 chunk_id_str
= "formatting error";
2442 chunk_id_str
= chunk_id_buf
;
2446 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2447 rotate_streams
.stream_count
, session
->session_name
,
2450 stream_positions
= lttng_buffer_view_from_view(payload
,
2451 sizeof(rotate_streams
), -1);
2452 if (!stream_positions
.data
||
2453 stream_positions
.size
<
2454 (rotate_streams
.stream_count
*
2455 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2456 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2461 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2462 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2463 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2464 const struct lttcomm_relayd_stream_rotation_position pos
= {
2465 .stream_id
= be64toh(position_comm
->stream_id
),
2466 .rotate_at_seq_num
= be64toh(
2467 position_comm
->rotate_at_seq_num
),
2470 stream
= stream_get_by_id(pos
.stream_id
);
2472 reply_code
= LTTNG_ERR_INVALID
;
2477 pthread_mutex_lock(&stream
->lock
);
2478 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2479 pos
.rotate_at_seq_num
);
2480 pthread_mutex_unlock(&stream
->lock
);
2482 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2490 reply_code
= LTTNG_OK
;
2497 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2498 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2499 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2500 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2501 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2506 lttng_trace_chunk_put(next_trace_chunk
);
2513 * relay_create_trace_chunk: create a new trace chunk
2515 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2516 struct relay_connection
*conn
,
2517 const struct lttng_buffer_view
*payload
)
2521 struct relay_session
*session
= conn
->session
;
2522 struct lttcomm_relayd_create_trace_chunk
*msg
;
2523 struct lttcomm_relayd_generic_reply reply
= {};
2524 struct lttng_buffer_view header_view
;
2525 struct lttng_buffer_view chunk_name_view
;
2526 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2527 enum lttng_error_code reply_code
= LTTNG_OK
;
2528 enum lttng_trace_chunk_status chunk_status
;
2529 struct lttng_directory_handle
*session_output
= NULL
;
2530 const char *new_path
;
2532 if (!session
|| !conn
->version_check_done
) {
2533 ERR("Trying to create a trace chunk before version check");
2538 if (session
->major
== 2 && session
->minor
< 11) {
2539 ERR("Chunk creation command is unsupported before 2.11");
2544 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2545 if (!header_view
.data
) {
2546 ERR("Failed to receive payload of chunk creation command");
2551 /* Convert to host endianness. */
2552 msg
= (typeof(msg
)) header_view
.data
;
2553 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2554 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2555 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2557 if (session
->current_trace_chunk
&&
2558 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2559 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2560 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
2561 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2562 ERR("Failed to rename old chunk");
2564 reply_code
= LTTNG_ERR_UNK
;
2568 session
->ongoing_rotation
= true;
2569 if (!session
->current_trace_chunk
) {
2570 if (!session
->has_rotated
) {
2576 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
2578 chunk
= lttng_trace_chunk_create(
2579 msg
->chunk_id
, msg
->creation_timestamp
, new_path
);
2581 ERR("Failed to create trace chunk in trace chunk creation command");
2583 reply_code
= LTTNG_ERR_NOMEM
;
2587 if (msg
->override_name_length
) {
2590 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2592 msg
->override_name_length
);
2593 name
= chunk_name_view
.data
;
2594 if (!name
|| name
[msg
->override_name_length
- 1]) {
2595 ERR("Failed to receive payload of chunk creation command");
2597 reply_code
= LTTNG_ERR_INVALID
;
2601 chunk_status
= lttng_trace_chunk_override_name(
2602 chunk
, chunk_name_view
.data
);
2603 switch (chunk_status
) {
2604 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2606 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2607 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2608 reply_code
= LTTNG_ERR_INVALID
;
2612 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2613 reply_code
= LTTNG_ERR_UNK
;
2619 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2620 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2621 reply_code
= LTTNG_ERR_UNK
;
2626 session_output
= session_create_output_directory_handle(
2628 if (!session_output
) {
2629 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2632 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, session_output
);
2633 lttng_directory_handle_put(session_output
);
2634 session_output
= NULL
;
2635 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2636 reply_code
= LTTNG_ERR_UNK
;
2641 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2642 sessiond_trace_chunk_registry
,
2643 conn
->session
->sessiond_uuid
,
2646 if (!published_chunk
) {
2647 char uuid_str
[LTTNG_UUID_STR_LEN
];
2649 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2650 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2655 reply_code
= LTTNG_ERR_NOMEM
;
2659 pthread_mutex_lock(&conn
->session
->lock
);
2660 if (conn
->session
->pending_closure_trace_chunk
) {
2662 * Invalid; this means a second create_trace_chunk command was
2663 * received before a close_trace_chunk.
2665 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2666 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2668 goto end_unlock_session
;
2670 conn
->session
->pending_closure_trace_chunk
=
2671 conn
->session
->current_trace_chunk
;
2672 conn
->session
->current_trace_chunk
= published_chunk
;
2673 published_chunk
= NULL
;
2674 if (!conn
->session
->pending_closure_trace_chunk
) {
2675 session
->ongoing_rotation
= false;
2678 pthread_mutex_unlock(&conn
->session
->lock
);
2680 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2681 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2683 sizeof(struct lttcomm_relayd_generic_reply
),
2685 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2686 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2691 lttng_trace_chunk_put(chunk
);
2692 lttng_trace_chunk_put(published_chunk
);
2693 lttng_directory_handle_put(session_output
);
2698 * relay_close_trace_chunk: close a trace chunk
2700 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2701 struct relay_connection
*conn
,
2702 const struct lttng_buffer_view
*payload
)
2704 int ret
= 0, buf_ret
;
2706 struct relay_session
*session
= conn
->session
;
2707 struct lttcomm_relayd_close_trace_chunk
*msg
;
2708 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2709 struct lttng_buffer_view header_view
;
2710 struct lttng_trace_chunk
*chunk
= NULL
;
2711 enum lttng_error_code reply_code
= LTTNG_OK
;
2712 enum lttng_trace_chunk_status chunk_status
;
2714 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2715 time_t close_timestamp
;
2716 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2717 size_t path_length
= 0;
2718 const char *chunk_name
= NULL
;
2719 struct lttng_dynamic_buffer reply_payload
;
2720 const char *new_path
;
2722 lttng_dynamic_buffer_init(&reply_payload
);
2724 if (!session
|| !conn
->version_check_done
) {
2725 ERR("Trying to close a trace chunk before version check");
2730 if (session
->major
== 2 && session
->minor
< 11) {
2731 ERR("Chunk close command is unsupported before 2.11");
2736 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2737 if (!header_view
.data
) {
2738 ERR("Failed to receive payload of chunk close command");
2743 /* Convert to host endianness. */
2744 msg
= (typeof(msg
)) header_view
.data
;
2745 chunk_id
= be64toh(msg
->chunk_id
);
2746 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2747 close_command
= (typeof(close_command
)){
2748 .value
= be32toh(msg
->close_command
.value
),
2749 .is_set
= msg
->close_command
.is_set
,
2752 chunk
= sessiond_trace_chunk_registry_get_chunk(
2753 sessiond_trace_chunk_registry
,
2754 conn
->session
->sessiond_uuid
,
2758 char uuid_str
[LTTNG_UUID_STR_LEN
];
2760 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2761 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2766 reply_code
= LTTNG_ERR_NOMEM
;
2770 pthread_mutex_lock(&session
->lock
);
2771 if (close_command
.is_set
&&
2772 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
) {
2774 * Clear command. It is a protocol error to ask for a
2775 * clear on a relay which does not allow it. Querying
2776 * the configuration allows figuring out whether
2777 * clearing is allowed before doing the clear.
2779 if (!opt_allow_clear
) {
2781 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2782 goto end_unlock_session
;
2785 if (session
->pending_closure_trace_chunk
&&
2786 session
->pending_closure_trace_chunk
!= chunk
) {
2787 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2788 session
->session_name
);
2789 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2791 goto end_unlock_session
;
2794 if (session
->current_trace_chunk
&& session
->current_trace_chunk
!= chunk
&&
2795 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2796 if (close_command
.is_set
&&
2797 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&&
2798 !session
->has_rotated
) {
2799 /* New chunk stays in session output directory. */
2802 /* Use chunk name for new chunk. */
2805 /* Rename new chunk path. */
2806 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2808 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2812 session
->ongoing_rotation
= false;
2814 if ((!close_command
.is_set
||
2815 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) &&
2816 !lttng_trace_chunk_get_name_overridden(chunk
)) {
2817 const char *old_path
;
2819 if (!session
->has_rotated
) {
2824 /* We need to move back the .tmp_old_chunk to its rightful place. */
2825 chunk_status
= lttng_trace_chunk_rename_path(chunk
, old_path
);
2826 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2831 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2832 chunk
, close_timestamp
);
2833 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2834 ERR("Failed to set trace chunk close timestamp");
2836 reply_code
= LTTNG_ERR_UNK
;
2837 goto end_unlock_session
;
2840 if (close_command
.is_set
) {
2841 chunk_status
= lttng_trace_chunk_set_close_command(
2842 chunk
, close_command
.value
);
2843 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2845 reply_code
= LTTNG_ERR_INVALID
;
2846 goto end_unlock_session
;
2849 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2850 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2851 ERR("Failed to get chunk name");
2853 reply_code
= LTTNG_ERR_UNK
;
2854 goto end_unlock_session
;
2856 if (!session
->has_rotated
&& !session
->snapshot
) {
2857 ret
= lttng_strncpy(closed_trace_chunk_path
,
2858 session
->output_path
,
2859 sizeof(closed_trace_chunk_path
));
2861 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2862 strlen(session
->output_path
),
2863 sizeof(closed_trace_chunk_path
));
2864 reply_code
= LTTNG_ERR_NOMEM
;
2866 goto end_unlock_session
;
2869 if (session
->snapshot
) {
2870 ret
= snprintf(closed_trace_chunk_path
,
2871 sizeof(closed_trace_chunk_path
),
2872 "%s/%s", session
->output_path
,
2875 ret
= snprintf(closed_trace_chunk_path
,
2876 sizeof(closed_trace_chunk_path
),
2877 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2879 session
->output_path
, chunk_name
);
2881 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2882 ERR("Failed to format closed trace chunk resulting path");
2883 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2885 goto end_unlock_session
;
2888 if (close_command
.is_set
&&
2889 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
2890 session
->has_rotated
= true;
2892 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2893 path_length
= strlen(closed_trace_chunk_path
) + 1;
2894 if (path_length
> UINT32_MAX
) {
2895 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2897 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2898 goto end_unlock_session
;
2901 if (session
->current_trace_chunk
== chunk
) {
2903 * After a trace chunk close command, no new streams
2904 * referencing the chunk may be created. Hence, on the
2905 * event that no new trace chunk have been created for
2906 * the session, the reference to the current trace chunk
2907 * is released in order to allow it to be reclaimed when
2908 * the last stream releases its reference to it.
2910 lttng_trace_chunk_put(session
->current_trace_chunk
);
2911 session
->current_trace_chunk
= NULL
;
2913 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2914 session
->pending_closure_trace_chunk
= NULL
;
2916 pthread_mutex_unlock(&session
->lock
);
2919 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
2920 reply
.path_length
= htobe32((uint32_t) path_length
);
2921 buf_ret
= lttng_dynamic_buffer_append(
2922 &reply_payload
, &reply
, sizeof(reply
));
2924 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2928 if (reply_code
== LTTNG_OK
) {
2929 buf_ret
= lttng_dynamic_buffer_append(&reply_payload
,
2930 closed_trace_chunk_path
, path_length
);
2932 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2937 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2941 if (send_ret
< reply_payload
.size
) {
2942 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2943 reply_payload
.size
, send_ret
);
2948 lttng_trace_chunk_put(chunk
);
2949 lttng_dynamic_buffer_reset(&reply_payload
);
2954 * relay_trace_chunk_exists: check if a trace chunk exists
2956 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2957 struct relay_connection
*conn
,
2958 const struct lttng_buffer_view
*payload
)
2962 struct relay_session
*session
= conn
->session
;
2963 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2964 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2965 struct lttng_buffer_view header_view
;
2969 if (!session
|| !conn
->version_check_done
) {
2970 ERR("Trying to close a trace chunk before version check");
2975 if (session
->major
== 2 && session
->minor
< 11) {
2976 ERR("Chunk close command is unsupported before 2.11");
2981 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2982 if (!header_view
.data
) {
2983 ERR("Failed to receive payload of chunk close command");
2988 /* Convert to host endianness. */
2989 msg
= (typeof(msg
)) header_view
.data
;
2990 chunk_id
= be64toh(msg
->chunk_id
);
2992 ret
= sessiond_trace_chunk_registry_chunk_exists(
2993 sessiond_trace_chunk_registry
,
2994 conn
->session
->sessiond_uuid
,
2996 chunk_id
, &chunk_exists
);
2998 * If ret is not 0, send the reply and report the error to the caller.
2999 * It is a protocol (or internal) error and the session/connection
3000 * should be torn down.
3002 reply
= (typeof(reply
)){
3003 .generic
.ret_code
= htobe32((uint32_t)
3004 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3005 .trace_chunk_exists
= ret
== 0 ? chunk_exists
: 0,
3007 send_ret
= conn
->sock
->ops
->sendmsg(
3008 conn
->sock
, &reply
, sizeof(reply
), 0);
3009 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3010 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
3019 * relay_get_configuration: query whether feature is available
3021 static int relay_get_configuration(const struct lttcomm_relayd_hdr
*recv_hdr
,
3022 struct relay_connection
*conn
,
3023 const struct lttng_buffer_view
*payload
)
3027 struct lttcomm_relayd_get_configuration
*msg
;
3028 struct lttcomm_relayd_get_configuration_reply reply
= {};
3029 struct lttng_buffer_view header_view
;
3030 uint64_t query_flags
= 0;
3031 uint64_t result_flags
= 0;
3033 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3034 if (!header_view
.data
) {
3035 ERR("Failed to receive payload of chunk close command");
3040 /* Convert to host endianness. */
3041 msg
= (typeof(msg
)) header_view
.data
;
3042 query_flags
= be64toh(msg
->query_flags
);
3045 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
3048 if (opt_allow_clear
) {
3049 result_flags
|= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
;
3053 reply
= (typeof(reply
)){
3054 .generic
.ret_code
= htobe32((uint32_t)
3055 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3056 .relayd_configuration_flags
= htobe64(result_flags
),
3058 send_ret
= conn
->sock
->ops
->sendmsg(
3059 conn
->sock
, &reply
, sizeof(reply
), 0);
3060 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3061 ERR("Failed to send \"get configuration\" command reply (ret = %zd)",
3069 #define DBG_CMD(cmd_name, conn) \
3070 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3072 static int relay_process_control_command(struct relay_connection
*conn
,
3073 const struct lttcomm_relayd_hdr
*header
,
3074 const struct lttng_buffer_view
*payload
)
3078 switch (header
->cmd
) {
3079 case RELAYD_CREATE_SESSION
:
3080 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
3081 ret
= relay_create_session(header
, conn
, payload
);
3083 case RELAYD_ADD_STREAM
:
3084 DBG_CMD("RELAYD_ADD_STREAM", conn
);
3085 ret
= relay_add_stream(header
, conn
, payload
);
3087 case RELAYD_START_DATA
:
3088 DBG_CMD("RELAYD_START_DATA", conn
);
3089 ret
= relay_start(header
, conn
, payload
);
3091 case RELAYD_SEND_METADATA
:
3092 DBG_CMD("RELAYD_SEND_METADATA", conn
);
3093 ret
= relay_recv_metadata(header
, conn
, payload
);
3095 case RELAYD_VERSION
:
3096 DBG_CMD("RELAYD_VERSION", conn
);
3097 ret
= relay_send_version(header
, conn
, payload
);
3099 case RELAYD_CLOSE_STREAM
:
3100 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
3101 ret
= relay_close_stream(header
, conn
, payload
);
3103 case RELAYD_DATA_PENDING
:
3104 DBG_CMD("RELAYD_DATA_PENDING", conn
);
3105 ret
= relay_data_pending(header
, conn
, payload
);
3107 case RELAYD_QUIESCENT_CONTROL
:
3108 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
3109 ret
= relay_quiescent_control(header
, conn
, payload
);
3111 case RELAYD_BEGIN_DATA_PENDING
:
3112 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
3113 ret
= relay_begin_data_pending(header
, conn
, payload
);
3115 case RELAYD_END_DATA_PENDING
:
3116 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
3117 ret
= relay_end_data_pending(header
, conn
, payload
);
3119 case RELAYD_SEND_INDEX
:
3120 DBG_CMD("RELAYD_SEND_INDEX", conn
);
3121 ret
= relay_recv_index(header
, conn
, payload
);
3123 case RELAYD_STREAMS_SENT
:
3124 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
3125 ret
= relay_streams_sent(header
, conn
, payload
);
3127 case RELAYD_RESET_METADATA
:
3128 DBG_CMD("RELAYD_RESET_METADATA", conn
);
3129 ret
= relay_reset_metadata(header
, conn
, payload
);
3131 case RELAYD_ROTATE_STREAMS
:
3132 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
3133 ret
= relay_rotate_session_streams(header
, conn
, payload
);
3135 case RELAYD_CREATE_TRACE_CHUNK
:
3136 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
3137 ret
= relay_create_trace_chunk(header
, conn
, payload
);
3139 case RELAYD_CLOSE_TRACE_CHUNK
:
3140 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
3141 ret
= relay_close_trace_chunk(header
, conn
, payload
);
3143 case RELAYD_TRACE_CHUNK_EXISTS
:
3144 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
3145 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
3147 case RELAYD_GET_CONFIGURATION
:
3148 DBG_CMD("RELAYD_GET_CONFIGURATION", conn
);
3149 ret
= relay_get_configuration(header
, conn
, payload
);
3151 case RELAYD_UPDATE_SYNC_INFO
:
3153 ERR("Received unknown command (%u)", header
->cmd
);
3154 relay_unknown_command(conn
);
3163 static enum relay_connection_status
relay_process_control_receive_payload(
3164 struct relay_connection
*conn
)
3167 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3168 struct lttng_dynamic_buffer
*reception_buffer
=
3169 &conn
->protocol
.ctrl
.reception_buffer
;
3170 struct ctrl_connection_state_receive_payload
*state
=
3171 &conn
->protocol
.ctrl
.state
.receive_payload
;
3172 struct lttng_buffer_view payload_view
;
3174 if (state
->left_to_receive
== 0) {
3175 /* Short-circuit for payload-less commands. */
3176 goto reception_complete
;
3179 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3180 reception_buffer
->data
+ state
->received
,
3181 state
->left_to_receive
, MSG_DONTWAIT
);
3183 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3184 PERROR("Unable to receive command payload on sock %d",
3186 status
= RELAY_CONNECTION_STATUS_ERROR
;
3189 } else if (ret
== 0) {
3190 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3191 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3196 assert(ret
<= state
->left_to_receive
);
3198 state
->left_to_receive
-= ret
;
3199 state
->received
+= ret
;
3201 if (state
->left_to_receive
> 0) {
3203 * Can't transition to the protocol's next state, wait to
3204 * receive the rest of the header.
3206 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3207 state
->received
, state
->left_to_receive
,
3213 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3214 conn
->sock
->fd
, state
->received
);
3216 * The payload required to process the command has been received.
3217 * A view to the reception buffer is forwarded to the various
3218 * commands and the state of the control is reset on success.
3220 * Commands are responsible for sending their reply to the peer.
3222 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3224 ret
= relay_process_control_command(conn
,
3225 &state
->header
, &payload_view
);
3227 status
= RELAY_CONNECTION_STATUS_ERROR
;
3231 ret
= connection_reset_protocol_state(conn
);
3233 status
= RELAY_CONNECTION_STATUS_ERROR
;
3239 static enum relay_connection_status
relay_process_control_receive_header(
3240 struct relay_connection
*conn
)
3243 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3244 struct lttcomm_relayd_hdr header
;
3245 struct lttng_dynamic_buffer
*reception_buffer
=
3246 &conn
->protocol
.ctrl
.reception_buffer
;
3247 struct ctrl_connection_state_receive_header
*state
=
3248 &conn
->protocol
.ctrl
.state
.receive_header
;
3250 assert(state
->left_to_receive
!= 0);
3252 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3253 reception_buffer
->data
+ state
->received
,
3254 state
->left_to_receive
, MSG_DONTWAIT
);
3256 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3257 PERROR("Unable to receive control command header on sock %d",
3259 status
= RELAY_CONNECTION_STATUS_ERROR
;
3262 } else if (ret
== 0) {
3263 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3264 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3269 assert(ret
<= state
->left_to_receive
);
3271 state
->left_to_receive
-= ret
;
3272 state
->received
+= ret
;
3274 if (state
->left_to_receive
> 0) {
3276 * Can't transition to the protocol's next state, wait to
3277 * receive the rest of the header.
3279 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3280 state
->received
, state
->left_to_receive
,
3285 /* Transition to next state: receiving the command's payload. */
3286 conn
->protocol
.ctrl
.state_id
=
3287 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3288 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3289 header
.circuit_id
= be64toh(header
.circuit_id
);
3290 header
.data_size
= be64toh(header
.data_size
);
3291 header
.cmd
= be32toh(header
.cmd
);
3292 header
.cmd_version
= be32toh(header
.cmd_version
);
3293 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3294 &header
, sizeof(header
));
3296 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3297 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3300 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3301 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3303 status
= RELAY_CONNECTION_STATUS_ERROR
;
3307 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3309 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3310 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3313 status
= RELAY_CONNECTION_STATUS_ERROR
;
3317 if (header
.data_size
== 0) {
3319 * Manually invoke the next state as the poll loop
3320 * will not wake-up to allow us to proceed further.
3322 status
= relay_process_control_receive_payload(conn
);
3329 * Process the commands received on the control socket
3331 static enum relay_connection_status
relay_process_control(
3332 struct relay_connection
*conn
)
3334 enum relay_connection_status status
;
3336 switch (conn
->protocol
.ctrl
.state_id
) {
3337 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3338 status
= relay_process_control_receive_header(conn
);
3340 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3341 status
= relay_process_control_receive_payload(conn
);
3344 ERR("Unknown control connection protocol state encountered.");
3351 static enum relay_connection_status
relay_process_data_receive_header(
3352 struct relay_connection
*conn
)
3355 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3356 struct data_connection_state_receive_header
*state
=
3357 &conn
->protocol
.data
.state
.receive_header
;
3358 struct lttcomm_relayd_data_hdr header
;
3359 struct relay_stream
*stream
;
3361 assert(state
->left_to_receive
!= 0);
3363 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3364 state
->header_reception_buffer
+ state
->received
,
3365 state
->left_to_receive
, MSG_DONTWAIT
);
3367 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3368 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3369 status
= RELAY_CONNECTION_STATUS_ERROR
;
3372 } else if (ret
== 0) {
3373 /* Orderly shutdown. Not necessary to print an error. */
3374 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3375 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3380 assert(ret
<= state
->left_to_receive
);
3382 state
->left_to_receive
-= ret
;
3383 state
->received
+= ret
;
3385 if (state
->left_to_receive
> 0) {
3387 * Can't transition to the protocol's next state, wait to
3388 * receive the rest of the header.
3390 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3391 state
->received
, state
->left_to_receive
,
3396 /* Transition to next state: receiving the payload. */
3397 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3399 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3400 header
.circuit_id
= be64toh(header
.circuit_id
);
3401 header
.stream_id
= be64toh(header
.stream_id
);
3402 header
.data_size
= be32toh(header
.data_size
);
3403 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3404 header
.padding_size
= be32toh(header
.padding_size
);
3405 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3407 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3409 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3410 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3412 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3413 conn
->sock
->fd
, header
.circuit_id
,
3414 header
.stream_id
, header
.data_size
,
3415 header
.net_seq_num
, header
.padding_size
);
3417 stream
= stream_get_by_id(header
.stream_id
);
3419 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3421 /* Protocol error. */
3422 status
= RELAY_CONNECTION_STATUS_ERROR
;
3426 pthread_mutex_lock(&stream
->lock
);
3427 /* Prepare stream for the reception of a new packet. */
3428 ret
= stream_init_packet(stream
, header
.data_size
,
3429 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3430 pthread_mutex_unlock(&stream
->lock
);
3432 ERR("Failed to rotate stream output file");
3433 status
= RELAY_CONNECTION_STATUS_ERROR
;
3434 goto end_stream_unlock
;
3443 static enum relay_connection_status
relay_process_data_receive_payload(
3444 struct relay_connection
*conn
)
3447 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3448 struct relay_stream
*stream
;
3449 struct data_connection_state_receive_payload
*state
=
3450 &conn
->protocol
.data
.state
.receive_payload
;
3451 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3452 char data_buffer
[chunk_size
];
3453 bool partial_recv
= false;
3454 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3455 uint64_t left_to_receive
= state
->left_to_receive
;
3456 struct relay_session
*session
;
3458 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3459 state
->header
.stream_id
, state
->header
.net_seq_num
,
3460 state
->received
, left_to_receive
);
3462 stream
= stream_get_by_id(state
->header
.stream_id
);
3464 /* Protocol error. */
3465 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3466 state
->header
.stream_id
);
3467 status
= RELAY_CONNECTION_STATUS_ERROR
;
3471 pthread_mutex_lock(&stream
->lock
);
3472 session
= stream
->trace
->session
;
3473 if (!conn
->session
) {
3474 ret
= connection_set_session(conn
, session
);
3476 status
= RELAY_CONNECTION_STATUS_ERROR
;
3477 goto end_stream_unlock
;
3482 * The size of the "chunk" received on any iteration is bounded by:
3483 * - the data left to receive,
3484 * - the data immediately available on the socket,
3485 * - the on-stack data buffer
3487 while (left_to_receive
> 0 && !partial_recv
) {
3488 size_t recv_size
= min(left_to_receive
, chunk_size
);
3489 struct lttng_buffer_view packet_chunk
;
3491 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3492 recv_size
, MSG_DONTWAIT
);
3494 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3495 PERROR("Socket %d error", conn
->sock
->fd
);
3496 status
= RELAY_CONNECTION_STATUS_ERROR
;
3498 goto end_stream_unlock
;
3499 } else if (ret
== 0) {
3500 /* No more data ready to be consumed on socket. */
3501 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3502 state
->header
.stream_id
);
3503 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3505 } else if (ret
< (int) recv_size
) {
3507 * All the data available on the socket has been
3510 partial_recv
= true;
3514 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3516 assert(packet_chunk
.data
);
3518 ret
= stream_write(stream
, &packet_chunk
, 0);
3520 ERR("Relay error writing data to file");
3521 status
= RELAY_CONNECTION_STATUS_ERROR
;
3522 goto end_stream_unlock
;
3525 left_to_receive
-= recv_size
;
3526 state
->received
+= recv_size
;
3527 state
->left_to_receive
= left_to_receive
;
3530 if (state
->left_to_receive
> 0) {
3532 * Did not receive all the data expected, wait for more data to
3533 * become available on the socket.
3535 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3536 state
->header
.stream_id
, state
->received
,
3537 state
->left_to_receive
);
3538 goto end_stream_unlock
;
3541 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3543 status
= RELAY_CONNECTION_STATUS_ERROR
;
3544 goto end_stream_unlock
;
3547 if (session_streams_have_index(session
)) {
3548 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3549 state
->rotate_index
, &index_flushed
,
3550 state
->header
.data_size
+ state
->header
.padding_size
);
3552 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3553 stream
->stream_handle
,
3554 state
->header
.net_seq_num
, ret
);
3555 status
= RELAY_CONNECTION_STATUS_ERROR
;
3556 goto end_stream_unlock
;
3560 if (stream
->prev_data_seq
== -1ULL) {
3564 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3565 state
->header
.padding_size
, state
->header
.net_seq_num
,
3568 status
= RELAY_CONNECTION_STATUS_ERROR
;
3569 goto end_stream_unlock
;
3573 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3574 * contents of *state which are aliased (union) to the same location as
3575 * the new state. Don't use it beyond this point.
3577 connection_reset_protocol_state(conn
);
3581 close_requested
= stream
->close_requested
;
3582 pthread_mutex_unlock(&stream
->lock
);
3583 if (close_requested
&& left_to_receive
== 0) {
3584 try_stream_close(stream
);
3588 pthread_mutex_lock(&session
->lock
);
3589 uatomic_set(&session
->new_streams
, 1);
3590 pthread_mutex_unlock(&session
->lock
);
3599 * relay_process_data: Process the data received on the data socket
3601 static enum relay_connection_status
relay_process_data(
3602 struct relay_connection
*conn
)
3604 enum relay_connection_status status
;
3606 switch (conn
->protocol
.data
.state_id
) {
3607 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3608 status
= relay_process_data_receive_header(conn
);
3610 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3611 status
= relay_process_data_receive_payload(conn
);
3614 ERR("Unexpected data connection communication state.");
3621 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3625 (void) lttng_poll_del(events
, pollfd
);
3627 ret
= close(pollfd
);
3629 ERR("Closing pollfd %d", pollfd
);
3633 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3634 int pollfd
, struct relay_connection
*conn
)
3636 const char *type_str
;
3638 switch (conn
->type
) {
3643 type_str
= "Control";
3645 case RELAY_VIEWER_COMMAND
:
3646 type_str
= "Viewer Command";
3648 case RELAY_VIEWER_NOTIFICATION
:
3649 type_str
= "Viewer Notification";
3652 type_str
= "Unknown";
3654 cleanup_connection_pollfd(events
, pollfd
);
3655 connection_put(conn
);
3656 DBG("%s connection closed with %d", type_str
, pollfd
);
3660 * This thread does the actual work
3662 static void *relay_thread_worker(void *data
)
3664 int ret
, err
= -1, last_seen_data_fd
= -1;
3666 struct lttng_poll_event events
;
3667 struct lttng_ht
*relay_connections_ht
;
3668 struct lttng_ht_iter iter
;
3669 struct relay_connection
*destroy_conn
= NULL
;
3671 DBG("[thread] Relay worker started");
3673 rcu_register_thread();
3675 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3677 if (testpoint(relayd_thread_worker
)) {
3678 goto error_testpoint
;
3681 health_code_update();
3683 /* table of connections indexed on socket */
3684 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3685 if (!relay_connections_ht
) {
3686 goto relay_connections_ht_error
;
3689 ret
= create_named_thread_poll_set(&events
, 2, "Worker thread epoll");
3691 goto error_poll_create
;
3694 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3701 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3703 health_code_update();
3705 /* Infinite blocking call, waiting for transmission */
3706 DBG3("Relayd worker thread polling...");
3707 health_poll_entry();
3708 ret
= lttng_poll_wait(&events
, -1);
3712 * Restart interrupted system call.
3714 if (errno
== EINTR
) {
3723 * Process control. The control connection is
3724 * prioritized so we don't starve it with high
3725 * throughput tracing data on the data connection.
3727 for (i
= 0; i
< nb_fd
; i
++) {
3728 /* Fetch once the poll data */
3729 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3730 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3732 health_code_update();
3734 /* Thread quit pipe has been closed. Killing thread. */
3735 ret
= check_thread_quit_pipe(pollfd
, revents
);
3741 /* Inspect the relay conn pipe for new connection */
3742 if (pollfd
== relay_conn_pipe
[0]) {
3743 if (revents
& LPOLLIN
) {
3744 struct relay_connection
*conn
;
3746 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3750 ret
= lttng_poll_add(&events
,
3752 LPOLLIN
| LPOLLRDHUP
);
3754 ERR("Failed to add new connection file descriptor to poll set");
3757 connection_ht_add(relay_connections_ht
, conn
);
3758 DBG("Connection socket %d added", conn
->sock
->fd
);
3759 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3760 ERR("Relay connection pipe error");
3763 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3767 struct relay_connection
*ctrl_conn
;
3769 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3770 /* If not found, there is a synchronization issue. */
3773 if (ctrl_conn
->type
== RELAY_DATA
) {
3774 if (revents
& LPOLLIN
) {
3776 * Flag the last seen data fd not deleted. It will be
3777 * used as the last seen fd if any fd gets deleted in
3780 last_notdel_data_fd
= pollfd
;
3782 goto put_ctrl_connection
;
3784 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3786 if (revents
& LPOLLIN
) {
3787 enum relay_connection_status status
;
3789 status
= relay_process_control(ctrl_conn
);
3790 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3792 * On socket error flag the session as aborted to force
3793 * the cleanup of its stream otherwise it can leak
3794 * during the lifetime of the relayd.
3796 * This prevents situations in which streams can be
3797 * left opened because an index was received, the
3798 * control connection is closed, and the data
3799 * connection is closed (uncleanly) before the packet's
3802 * Since the control connection encountered an error,
3803 * it is okay to be conservative and close the
3804 * session right now as we can't rely on the protocol
3805 * being respected anymore.
3807 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3808 session_abort(ctrl_conn
->session
);
3811 /* Clear the connection on error or close. */
3812 relay_thread_close_connection(&events
,
3817 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3818 relay_thread_close_connection(&events
,
3820 if (last_seen_data_fd
== pollfd
) {
3821 last_seen_data_fd
= last_notdel_data_fd
;
3824 ERR("Unexpected poll events %u for control sock %d",
3826 connection_put(ctrl_conn
);
3829 put_ctrl_connection
:
3830 connection_put(ctrl_conn
);
3835 * The last loop handled a control request, go back to poll to make
3836 * sure we prioritise the control socket.
3842 if (last_seen_data_fd
>= 0) {
3843 for (i
= 0; i
< nb_fd
; i
++) {
3844 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3846 health_code_update();
3848 if (last_seen_data_fd
== pollfd
) {
3855 /* Process data connection. */
3856 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3857 /* Fetch the poll data. */
3858 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3859 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3860 struct relay_connection
*data_conn
;
3862 health_code_update();
3865 /* No activity for this FD (poll implementation). */
3869 /* Skip the command pipe. It's handled in the first loop. */
3870 if (pollfd
== relay_conn_pipe
[0]) {
3874 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3876 /* Skip it. Might be removed before. */
3879 if (data_conn
->type
== RELAY_CONTROL
) {
3880 goto put_data_connection
;
3882 assert(data_conn
->type
== RELAY_DATA
);
3884 if (revents
& LPOLLIN
) {
3885 enum relay_connection_status status
;
3887 status
= relay_process_data(data_conn
);
3888 /* Connection closed or error. */
3889 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3891 * On socket error flag the session as aborted to force
3892 * the cleanup of its stream otherwise it can leak
3893 * during the lifetime of the relayd.
3895 * This prevents situations in which streams can be
3896 * left opened because an index was received, the
3897 * control connection is closed, and the data
3898 * connection is closed (uncleanly) before the packet's
3901 * Since the data connection encountered an error,
3902 * it is okay to be conservative and close the
3903 * session right now as we can't rely on the protocol
3904 * being respected anymore.
3906 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3907 session_abort(data_conn
->session
);
3909 relay_thread_close_connection(&events
, pollfd
,
3912 * Every goto restart call sets the last seen fd where
3913 * here we don't really care since we gracefully
3914 * continue the loop after the connection is deleted.
3917 /* Keep last seen port. */
3918 last_seen_data_fd
= pollfd
;
3919 connection_put(data_conn
);
3922 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3923 relay_thread_close_connection(&events
, pollfd
,
3926 ERR("Unknown poll events %u for data sock %d",
3929 put_data_connection
:
3930 connection_put(data_conn
);
3932 last_seen_data_fd
= -1;
3935 /* Normal exit, no error */
3940 /* Cleanup remaining connection object. */
3942 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3945 health_code_update();
3947 session_abort(destroy_conn
->session
);
3950 * No need to grab another ref, because we own
3953 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3958 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
3960 lttng_ht_destroy(relay_connections_ht
);
3961 relay_connections_ht_error
:
3962 /* Close relay conn pipes */
3963 (void) fd_tracker_util_pipe_close(the_fd_tracker
,
3966 DBG("Thread exited with error");
3968 DBG("Worker thread cleanup complete");
3972 ERR("Health error occurred in %s", __func__
);
3974 health_unregister(health_relayd
);
3975 rcu_unregister_thread();
3976 lttng_relay_stop_threads();
3981 * Create the relay command pipe to wake thread_manage_apps.
3982 * Closed in cleanup().
3984 static int create_relay_conn_pipe(void)
3986 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
3987 "Relayd connection pipe", relay_conn_pipe
);
3993 int main(int argc
, char **argv
)
3995 bool thread_is_rcu_registered
= false;
3996 int ret
= 0, retval
= 0;
3999 /* Parse environment variables */
4000 ret
= parse_env_options();
4008 * Command line arguments overwrite environment.
4011 if (set_options(argc
, argv
)) {
4016 if (set_signal_handler()) {
4021 relayd_config_log();
4023 if (opt_print_version
) {
4029 ret
= fclose(stdin
);
4031 PERROR("Failed to close stdin");
4035 DBG("Clear command %s", opt_allow_clear
? "allowed" : "disallowed");
4037 /* Try to create directory if -o, --output is specified. */
4038 if (opt_output_path
) {
4039 if (*opt_output_path
!= '/') {
4040 ERR("Please specify an absolute path for -o, --output PATH");
4045 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
4048 ERR("Unable to create %s", opt_output_path
);
4055 if (opt_daemon
|| opt_background
) {
4056 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4064 if (opt_working_directory
) {
4065 ret
= utils_change_working_directory(opt_working_directory
);
4067 /* All errors are already logged. */
4072 sessiond_trace_chunk_registry
= sessiond_trace_chunk_registry_create();
4073 if (!sessiond_trace_chunk_registry
) {
4074 ERR("Failed to initialize session daemon trace chunk registry");
4080 * The RCU thread registration (and use, through the fd-tracker's
4081 * creation) is done after the daemonization to allow us to not
4082 * deal with liburcu's fork() management as the call RCU needs to
4085 rcu_register_thread();
4086 thread_is_rcu_registered
= true;
4088 the_fd_tracker
= fd_tracker_create(lttng_opt_fd_cap
);
4089 if (!the_fd_tracker
) {
4094 /* Initialize thread health monitoring */
4095 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
4096 if (!health_relayd
) {
4097 PERROR("health_app_create error");
4102 /* Create thread quit pipe */
4103 if (init_thread_quit_pipe()) {
4108 /* Setup the thread apps communication pipe. */
4109 if (create_relay_conn_pipe()) {
4114 /* Init relay command queue. */
4115 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
4117 /* Initialize communication library */
4119 lttcomm_inet_init();
4121 /* tables of sessions indexed by session ID */
4122 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4128 /* tables of streams indexed by stream ID */
4129 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4130 if (!relay_streams_ht
) {
4135 /* tables of streams indexed by stream ID */
4136 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4137 if (!viewer_streams_ht
) {
4142 ret
= init_health_quit_pipe();
4148 /* Create thread to manage the client socket */
4149 ret
= pthread_create(&health_thread
, default_pthread_attr(),
4150 thread_manage_health
, (void *) NULL
);
4153 PERROR("pthread_create health");
4158 /* Setup the dispatcher thread */
4159 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
4160 relay_thread_dispatcher
, (void *) NULL
);
4163 PERROR("pthread_create dispatcher");
4165 goto exit_dispatcher_thread
;
4168 /* Setup the worker thread */
4169 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
4170 relay_thread_worker
, NULL
);
4173 PERROR("pthread_create worker");
4175 goto exit_worker_thread
;
4178 /* Setup the listener thread */
4179 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
4180 relay_thread_listener
, (void *) NULL
);
4183 PERROR("pthread_create listener");
4185 goto exit_listener_thread
;
4188 ret
= relayd_live_create(live_uri
);
4190 ERR("Starting live viewer threads");
4196 * This is where we start awaiting program completion (e.g. through
4197 * signal that asks threads to teardown).
4200 ret
= relayd_live_join();
4206 ret
= pthread_join(listener_thread
, &status
);
4209 PERROR("pthread_join listener_thread");
4213 exit_listener_thread
:
4214 ret
= pthread_join(worker_thread
, &status
);
4217 PERROR("pthread_join worker_thread");
4222 ret
= pthread_join(dispatcher_thread
, &status
);
4225 PERROR("pthread_join dispatcher_thread");
4228 exit_dispatcher_thread
:
4230 ret
= pthread_join(health_thread
, &status
);
4233 PERROR("pthread_join health_thread");
4238 * Wait for all pending call_rcu work to complete before tearing
4239 * down data structures. call_rcu worker may be trying to
4240 * perform lookups in those structures.
4245 /* Ensure all prior call_rcu are done. */
4248 if (thread_is_rcu_registered
) {
4249 rcu_unregister_thread();