2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/resource.h>
39 #include <urcu/futex.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/rculist.h>
47 #include <lttng/lttng.h>
48 #include <common/common.h>
49 #include <common/compat/poll.h>
50 #include <common/compat/socket.h>
51 #include <common/compat/endian.h>
52 #include <common/compat/getenv.h>
53 #include <common/defaults.h>
54 #include <common/daemonize.h>
55 #include <common/futex.h>
56 #include <common/sessiond-comm/sessiond-comm.h>
57 #include <common/sessiond-comm/inet.h>
58 #include <common/sessiond-comm/relayd.h>
59 #include <common/uri.h>
60 #include <common/utils.h>
61 #include <common/align.h>
62 #include <common/config/session-config.h>
63 #include <common/dynamic-buffer.h>
64 #include <common/buffer-view.h>
65 #include <common/string-utils/format.h>
66 #include <common/fd-tracker/fd-tracker.h>
67 #include <common/fd-tracker/utils.h>
69 #include "backward-compatibility-group-by.h"
71 #include "connection.h"
72 #include "ctf-trace.h"
73 #include "health-relayd.h"
76 #include "lttng-relayd.h"
78 #include "sessiond-trace-chunks.h"
80 #include "tcp_keep_alive.h"
81 #include "testpoint.h"
82 #include "tracefile-array.h"
85 #include "viewer-stream.h"
87 static const char *help_msg
=
88 #ifdef LTTNG_EMBED_HELP
89 #include <lttng-relayd.8.h>
95 enum relay_connection_status
{
96 RELAY_CONNECTION_STATUS_OK
,
97 /* An error occurred while processing an event on the connection. */
98 RELAY_CONNECTION_STATUS_ERROR
,
99 /* Connection closed/shutdown cleanly. */
100 RELAY_CONNECTION_STATUS_CLOSED
,
103 /* command line options */
104 char *opt_output_path
, *opt_working_directory
;
105 static int opt_daemon
, opt_background
, opt_print_version
, opt_allow_clear
= 1;
106 enum relay_group_output_by opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
;
109 * We need to wait for listener and live listener threads, as well as
110 * health check thread, before being ready to signal readiness.
112 #define NR_LTTNG_RELAY_READY 3
113 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
115 /* Size of receive buffer. */
116 #define RECV_DATA_BUFFER_SIZE 65536
118 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
119 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
121 static struct lttng_uri
*control_uri
;
122 static struct lttng_uri
*data_uri
;
123 static struct lttng_uri
*live_uri
;
125 const char *progname
;
127 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
128 static int tracing_group_name_override
;
130 const char * const config_section_name
= "relayd";
133 * Quit pipe for all threads. This permits a single cancellation point
134 * for all threads when receiving an event on the pipe.
136 int thread_quit_pipe
[2] = { -1, -1 };
139 * This pipe is used to inform the worker thread that a command is queued and
140 * ready to be processed.
142 static int relay_conn_pipe
[2] = { -1, -1 };
144 /* Shared between threads */
145 static int dispatch_thread_exit
;
147 static pthread_t listener_thread
;
148 static pthread_t dispatcher_thread
;
149 static pthread_t worker_thread
;
150 static pthread_t health_thread
;
153 * last_relay_stream_id_lock protects last_relay_stream_id increment
154 * atomicity on 32-bit architectures.
156 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
157 static uint64_t last_relay_stream_id
;
160 * Relay command queue.
162 * The relay_thread_listener and relay_thread_dispatcher communicate with this
165 static struct relay_conn_queue relay_conn_queue
;
167 /* Cap of file desriptors to be in simultaneous use by the relay daemon. */
168 static unsigned int lttng_opt_fd_pool_size
= -1;
170 /* Global relay stream hash table. */
171 struct lttng_ht
*relay_streams_ht
;
173 /* Global relay viewer stream hash table. */
174 struct lttng_ht
*viewer_streams_ht
;
176 /* Global relay sessions hash table. */
177 struct lttng_ht
*sessions_ht
;
179 /* Relayd health monitoring */
180 struct health_app
*health_relayd
;
182 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
184 /* Global fd tracker. */
185 struct fd_tracker
*the_fd_tracker
;
187 static struct option long_options
[] = {
188 { "control-port", 1, 0, 'C', },
189 { "data-port", 1, 0, 'D', },
190 { "live-port", 1, 0, 'L', },
191 { "daemonize", 0, 0, 'd', },
192 { "background", 0, 0, 'b', },
193 { "group", 1, 0, 'g', },
194 { "fd-pool-size", 1, 0, '\0', },
195 { "help", 0, 0, 'h', },
196 { "output", 1, 0, 'o', },
197 { "verbose", 0, 0, 'v', },
198 { "config", 1, 0, 'f' },
199 { "version", 0, 0, 'V' },
200 { "working-directory", 1, 0, 'w', },
201 { "group-output-by-session", 0, 0, 's', },
202 { "group-output-by-host", 0, 0, 'p', },
203 { "disallow-clear", 0, 0, 'x' },
207 static const char *config_ignore_options
[] = { "help", "config", "version" };
209 static void print_version(void) {
210 fprintf(stdout
, "%s\n", VERSION
);
213 static void relayd_config_log(void)
215 DBG("LTTng-relayd " VERSION
" - " VERSION_NAME
"%s%s",
216 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
217 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
218 if (EXTRA_VERSION_DESCRIPTION
[0] != '\0') {
219 DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION
"\n");
221 if (EXTRA_VERSION_PATCHES
[0] != '\0') {
222 DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES
"\n");
227 * Take an option from the getopt output and set it in the right variable to be
230 * Return 0 on success else a negative value.
232 static int set_option(int opt
, const char *arg
, const char *optname
)
238 if (!strcmp(optname
, "fd-pool-size")) {
242 v
= strtoul(arg
, NULL
, 0);
243 if (errno
!= 0 || !isdigit(arg
[0])) {
244 ERR("Wrong value in --fd-pool-size parameter: %s", arg
);
249 ERR("File descriptor cap overflow in --fd-pool-size parameter: %s", arg
);
253 lttng_opt_fd_pool_size
= (unsigned int) v
;
255 fprintf(stderr
, "unknown option %s", optname
);
257 fprintf(stderr
, " with arg %s\n", arg
);
262 if (lttng_is_setuid_setgid()) {
263 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
264 "-C, --control-port");
266 ret
= uri_parse(arg
, &control_uri
);
268 ERR("Invalid control URI specified");
271 if (control_uri
->port
== 0) {
272 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
277 if (lttng_is_setuid_setgid()) {
278 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
281 ret
= uri_parse(arg
, &data_uri
);
283 ERR("Invalid data URI specified");
286 if (data_uri
->port
== 0) {
287 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
292 if (lttng_is_setuid_setgid()) {
293 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
296 ret
= uri_parse(arg
, &live_uri
);
298 ERR("Invalid live URI specified");
301 if (live_uri
->port
== 0) {
302 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
313 if (lttng_is_setuid_setgid()) {
314 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
317 tracing_group_name
= strdup(arg
);
318 if (tracing_group_name
== NULL
) {
323 tracing_group_name_override
= 1;
327 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
329 ERR("Cannot show --help for `lttng-relayd`");
334 opt_print_version
= 1;
337 if (lttng_is_setuid_setgid()) {
338 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
341 ret
= asprintf(&opt_output_path
, "%s", arg
);
344 PERROR("asprintf opt_output_path");
350 if (lttng_is_setuid_setgid()) {
351 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
352 "-w, --working-directory");
354 ret
= asprintf(&opt_working_directory
, "%s", arg
);
357 PERROR("asprintf opt_working_directory");
364 /* Verbose level can increase using multiple -v */
366 lttng_opt_verbose
= config_parse_value(arg
);
368 /* Only 3 level of verbosity (-vvv). */
369 if (lttng_opt_verbose
< 3) {
370 lttng_opt_verbose
+= 1;
375 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
376 ERR("Cannot set --group-output-by-session, another --group-output-by argument is present");
379 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_SESSION
;
382 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
383 ERR("Cannot set --group-output-by-host, another --group-output-by argument is present");
386 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
393 /* Unknown option or other error.
394 * Error is printed by getopt, just return */
407 * config_entry_handler_cb used to handle options read from a config file.
408 * See config_entry_handler_cb comment in common/config/session-config.h for the
409 * return value conventions.
411 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
415 if (!entry
|| !entry
->name
|| !entry
->value
) {
420 /* Check if the option is to be ignored */
421 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
422 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
427 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
428 /* Ignore if entry name is not fully matched. */
429 if (strcmp(entry
->name
, long_options
[i
].name
)) {
434 * If the option takes no argument on the command line,
435 * we have to check if the value is "true". We support
436 * non-zero numeric values, true, on and yes.
438 if (!long_options
[i
].has_arg
) {
439 ret
= config_parse_value(entry
->value
);
442 WARN("Invalid configuration value \"%s\" for option %s",
443 entry
->value
, entry
->name
);
445 /* False, skip boolean config option. */
450 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
454 WARN("Unrecognized option \"%s\" in daemon configuration file.",
461 static int parse_env_options(void)
466 value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV
);
468 opt_working_directory
= strdup(value
);
469 if (!opt_working_directory
) {
470 ERR("Failed to allocate working directory string (\"%s\")",
478 static int set_fd_pool_size(void)
481 struct rlimit rlimit
;
483 ret
= getrlimit(RLIMIT_NOFILE
, &rlimit
);
485 PERROR("Failed to get file descriptor limit");
490 DBG("File descriptor count limits are %" PRIu64
" (soft) and %" PRIu64
" (hard)",
491 (uint64_t) rlimit
.rlim_cur
,
492 (uint64_t) rlimit
.rlim_max
);
493 if (lttng_opt_fd_pool_size
== -1) {
494 /* Use default value (soft limit - reserve). */
495 if (rlimit
.rlim_cur
< DEFAULT_RELAYD_MIN_FD_POOL_SIZE
) {
496 ERR("The process' file number limit is too low (%" PRIu64
"). The process' file number limit must be set to at least %i.",
497 (uint64_t) rlimit
.rlim_cur
, DEFAULT_RELAYD_MIN_FD_POOL_SIZE
);
501 lttng_opt_fd_pool_size
= rlimit
.rlim_cur
-
502 DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE
;
506 if (lttng_opt_fd_pool_size
< DEFAULT_RELAYD_MIN_FD_POOL_SIZE
) {
507 ERR("File descriptor pool size must be set to at least %d",
508 DEFAULT_RELAYD_MIN_FD_POOL_SIZE
);
513 if (lttng_opt_fd_pool_size
> rlimit
.rlim_cur
) {
514 ERR("File descriptor pool size argument (%u) exceeds the process' soft limit (%" PRIu64
").",
515 lttng_opt_fd_pool_size
, (uint64_t) rlimit
.rlim_cur
);
520 DBG("File descriptor pool size argument (%u) adjusted to %u to accomodate transient fd uses",
521 lttng_opt_fd_pool_size
,
522 lttng_opt_fd_pool_size
- DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE
);
523 lttng_opt_fd_pool_size
-= DEFAULT_RELAYD_FD_POOL_SIZE_RESERVE
;
528 static int set_options(int argc
, char **argv
)
530 int c
, ret
= 0, option_index
= 0, retval
= 0;
531 int orig_optopt
= optopt
, orig_optind
= optind
;
532 char *default_address
, *optstring
;
533 const char *config_path
= NULL
;
535 optstring
= utils_generate_optstring(long_options
,
536 sizeof(long_options
) / sizeof(struct option
));
542 /* Check for the --config option */
544 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
545 &option_index
)) != -1) {
549 } else if (c
!= 'f') {
553 if (lttng_is_setuid_setgid()) {
554 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
557 config_path
= utils_expand_path(optarg
);
559 ERR("Failed to resolve path: %s", optarg
);
564 ret
= config_get_section_entries(config_path
, config_section_name
,
565 config_entry_handler
, NULL
);
568 ERR("Invalid configuration option at line %i", ret
);
574 /* Reset getopt's global state */
575 optopt
= orig_optopt
;
576 optind
= orig_optind
;
578 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
583 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
590 /* assign default values */
591 if (control_uri
== NULL
) {
592 ret
= asprintf(&default_address
,
593 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
594 DEFAULT_NETWORK_CONTROL_PORT
);
596 PERROR("asprintf default data address");
601 ret
= uri_parse(default_address
, &control_uri
);
602 free(default_address
);
604 ERR("Invalid control URI specified");
609 if (data_uri
== NULL
) {
610 ret
= asprintf(&default_address
,
611 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
612 DEFAULT_NETWORK_DATA_PORT
);
614 PERROR("asprintf default data address");
619 ret
= uri_parse(default_address
, &data_uri
);
620 free(default_address
);
622 ERR("Invalid data URI specified");
627 if (live_uri
== NULL
) {
628 ret
= asprintf(&default_address
,
629 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
630 DEFAULT_NETWORK_VIEWER_PORT
);
632 PERROR("asprintf default viewer control address");
637 ret
= uri_parse(default_address
, &live_uri
);
638 free(default_address
);
640 ERR("Invalid viewer control URI specified");
645 ret
= set_fd_pool_size();
651 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
652 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
654 if (opt_allow_clear
) {
655 /* Check if env variable exists. */
656 const char *value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
658 ret
= config_parse_value(value
);
660 ERR("Invalid value for %s specified", DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
664 opt_allow_clear
= !ret
;
673 static void print_global_objects(void)
675 print_viewer_streams();
676 print_relay_streams();
680 static int noop_close(void *data
, int *fds
)
685 static void untrack_stdio(void)
687 int fds
[] = { fileno(stdout
), fileno(stderr
) };
690 * noop_close is used since we don't really want to close
691 * the stdio output fds; we merely want to stop tracking them.
693 (void) fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
694 fds
, 2, noop_close
, NULL
);
700 static void relayd_cleanup(void)
702 print_global_objects();
706 if (viewer_streams_ht
)
707 lttng_ht_destroy(viewer_streams_ht
);
708 if (relay_streams_ht
)
709 lttng_ht_destroy(relay_streams_ht
);
711 lttng_ht_destroy(sessions_ht
);
713 free(opt_output_path
);
714 free(opt_working_directory
);
717 health_app_destroy(health_relayd
);
719 /* Close thread quit pipes */
720 if (health_quit_pipe
[0] != -1) {
721 (void) fd_tracker_util_pipe_close(
722 the_fd_tracker
, health_quit_pipe
);
724 if (thread_quit_pipe
[0] != -1) {
725 (void) fd_tracker_util_pipe_close(
726 the_fd_tracker
, thread_quit_pipe
);
728 if (sessiond_trace_chunk_registry
) {
729 sessiond_trace_chunk_registry_destroy(
730 sessiond_trace_chunk_registry
);
732 if (the_fd_tracker
) {
735 * fd_tracker_destroy() will log the contents of the fd-tracker
736 * if a leak is detected.
738 fd_tracker_destroy(the_fd_tracker
);
741 uri_free(control_uri
);
743 /* Live URI is freed in the live thread. */
745 if (tracing_group_name_override
) {
746 free((void *) tracing_group_name
);
751 * Write to writable pipe used to notify a thread.
753 static int notify_thread_pipe(int wpipe
)
757 ret
= lttng_write(wpipe
, "!", 1);
759 PERROR("write poll pipe");
767 static int notify_health_quit_pipe(int *pipe
)
771 ret
= lttng_write(pipe
[1], "4", 1);
773 PERROR("write relay health quit");
782 * Stop all relayd and relayd-live threads.
784 int lttng_relay_stop_threads(void)
788 /* Stopping all threads */
789 DBG("Terminating all threads");
790 if (notify_thread_pipe(thread_quit_pipe
[1])) {
791 ERR("write error on thread quit pipe");
795 if (notify_health_quit_pipe(health_quit_pipe
)) {
796 ERR("write error on health quit pipe");
799 /* Dispatch thread */
800 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
801 futex_nto1_wake(&relay_conn_queue
.futex
);
803 if (relayd_live_stop()) {
804 ERR("Error stopping live threads");
811 * Signal handler for the daemon
813 * Simply stop all worker threads, leaving main() return gracefully after
814 * joining all threads and calling cleanup().
816 static void sighandler(int sig
)
820 DBG("SIGINT caught");
821 if (lttng_relay_stop_threads()) {
822 ERR("Error stopping threads");
826 DBG("SIGTERM caught");
827 if (lttng_relay_stop_threads()) {
828 ERR("Error stopping threads");
832 CMM_STORE_SHARED(recv_child_signal
, 1);
840 * Setup signal handler for :
841 * SIGINT, SIGTERM, SIGPIPE
843 static int set_signal_handler(void)
849 if ((ret
= sigemptyset(&sigset
)) < 0) {
850 PERROR("sigemptyset");
857 sa
.sa_handler
= sighandler
;
858 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
863 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
868 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
873 sa
.sa_handler
= SIG_IGN
;
874 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
879 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
884 void lttng_relay_notify_ready(void)
886 /* Notify the parent of the fork() process that we are ready. */
887 if (opt_daemon
|| opt_background
) {
888 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
889 kill(child_ppid
, SIGUSR1
);
895 * Init thread quit pipe.
897 * Return -1 on error or 0 if all pipes are created.
899 static int init_thread_quit_pipe(void)
901 return fd_tracker_util_pipe_open_cloexec(
902 the_fd_tracker
, "Quit pipe", thread_quit_pipe
);
906 * Init health quit pipe.
908 * Return -1 on error or 0 if all pipes are created.
910 static int init_health_quit_pipe(void)
912 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
913 "Health quit pipe", health_quit_pipe
);
917 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
919 static int create_named_thread_poll_set(struct lttng_poll_event
*events
,
920 int size
, const char *name
)
924 if (events
== NULL
|| size
== 0) {
929 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
930 name
, events
, 1, LTTNG_CLOEXEC
);
933 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
945 * Check if the thread quit pipe was triggered.
947 * Return 1 if it was triggered else 0;
949 static int check_thread_quit_pipe(int fd
, uint32_t events
)
951 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
958 static int create_sock(void *data
, int *out_fd
)
961 struct lttcomm_sock
*sock
= data
;
963 ret
= lttcomm_create_sock(sock
);
973 static int close_sock(void *data
, int *in_fd
)
975 struct lttcomm_sock
*sock
= data
;
977 return sock
->ops
->close(sock
);
980 static int accept_sock(void *data
, int *out_fd
)
983 /* Socks is an array of in_sock, out_sock. */
984 struct lttcomm_sock
**socks
= data
;
985 struct lttcomm_sock
*in_sock
= socks
[0];
987 socks
[1] = in_sock
->ops
->accept(in_sock
);
992 *out_fd
= socks
[1]->fd
;
998 * Create and init socket from uri.
1000 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
,
1004 struct lttcomm_sock
*sock
= NULL
;
1005 char uri_str
[PATH_MAX
];
1006 char *formated_name
= NULL
;
1008 sock
= lttcomm_alloc_sock_from_uri(uri
);
1010 ERR("Allocating socket");
1015 * Don't fail to create the socket if the name can't be built as it is
1016 * only used for debugging purposes.
1018 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
1019 uri_str
[sizeof(uri_str
) - 1] = '\0';
1021 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
1024 formated_name
= NULL
;
1028 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
1029 (const char **) (formated_name
? &formated_name
: NULL
),
1030 1, create_sock
, sock
);
1031 free(formated_name
);
1032 DBG("Listening on %s socket %d", name
, sock
->fd
);
1034 ret
= sock
->ops
->bind(sock
);
1036 PERROR("Failed to bind socket");
1040 ret
= sock
->ops
->listen(sock
, -1);
1050 lttcomm_destroy_sock(sock
);
1056 struct lttcomm_sock
*accept_relayd_sock(struct lttcomm_sock
*listening_sock
,
1060 struct lttcomm_sock
*socks
[2] = { listening_sock
, NULL
};
1061 struct lttcomm_sock
*new_sock
= NULL
;
1063 ret
= fd_tracker_open_unsuspendable_fd(
1064 the_fd_tracker
, &out_fd
,
1065 (const char **) &name
,
1066 1, accept_sock
, &socks
);
1070 new_sock
= socks
[1];
1071 DBG("%s accepted, socket %d", name
, new_sock
->fd
);
1077 * This thread manages the listening for new connections on the network
1079 static void *relay_thread_listener(void *data
)
1081 int i
, ret
, pollfd
, err
= -1;
1082 uint32_t revents
, nb_fd
;
1083 struct lttng_poll_event events
;
1084 struct lttcomm_sock
*control_sock
, *data_sock
;
1086 DBG("[thread] Relay listener started");
1088 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
1090 health_code_update();
1092 control_sock
= relay_socket_create(control_uri
, "Control listener");
1093 if (!control_sock
) {
1094 goto error_sock_control
;
1097 data_sock
= relay_socket_create(data_uri
, "Data listener");
1099 goto error_sock_relay
;
1103 * Pass 3 as size here for the thread quit pipe, control and
1106 ret
= create_named_thread_poll_set(&events
, 3, "Listener thread epoll");
1108 goto error_create_poll
;
1111 /* Add the control socket */
1112 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
1114 goto error_poll_add
;
1117 /* Add the data socket */
1118 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
1120 goto error_poll_add
;
1123 lttng_relay_notify_ready();
1125 if (testpoint(relayd_thread_listener
)) {
1126 goto error_testpoint
;
1130 health_code_update();
1132 DBG("Listener accepting connections");
1135 health_poll_entry();
1136 ret
= lttng_poll_wait(&events
, -1);
1140 * Restart interrupted system call.
1142 if (errno
== EINTR
) {
1150 DBG("Relay new connection received");
1151 for (i
= 0; i
< nb_fd
; i
++) {
1152 health_code_update();
1154 /* Fetch once the poll data */
1155 revents
= LTTNG_POLL_GETEV(&events
, i
);
1156 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1158 /* Thread quit pipe has been closed. Killing thread. */
1159 ret
= check_thread_quit_pipe(pollfd
, revents
);
1165 if (revents
& LPOLLIN
) {
1167 * A new connection is requested, therefore a
1168 * sessiond/consumerd connection is allocated in
1169 * this thread, enqueued to a global queue and
1170 * dequeued (and freed) in the worker thread.
1173 struct relay_connection
*new_conn
;
1174 struct lttcomm_sock
*newsock
= NULL
;
1175 enum connection_type type
;
1177 if (pollfd
== data_sock
->fd
) {
1179 newsock
= accept_relayd_sock(data_sock
,
1180 "Data socket to relayd");
1182 assert(pollfd
== control_sock
->fd
);
1183 type
= RELAY_CONTROL
;
1184 newsock
= control_sock
->ops
->accept(control_sock
);
1185 DBG("Relay control connection accepted, socket %d",
1189 PERROR("accepting sock");
1193 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
1196 PERROR("setsockopt inet");
1197 lttcomm_destroy_sock(newsock
);
1201 ret
= socket_apply_keep_alive_config(newsock
->fd
);
1203 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1205 lttcomm_destroy_sock(newsock
);
1209 new_conn
= connection_create(newsock
, type
);
1211 lttcomm_destroy_sock(newsock
);
1215 /* Enqueue request for the dispatcher thread. */
1216 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
1220 * Wake the dispatch queue futex.
1221 * Implicit memory barrier with the
1222 * exchange in cds_wfcq_enqueue.
1224 futex_nto1_wake(&relay_conn_queue
.futex
);
1225 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1226 ERR("socket poll error");
1229 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1239 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
1241 if (data_sock
->fd
>= 0) {
1242 int data_sock_fd
= data_sock
->fd
;
1244 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
1245 &data_sock_fd
, 1, close_sock
,
1248 PERROR("Failed to close the data listener socket file descriptor");
1252 lttcomm_destroy_sock(data_sock
);
1254 if (control_sock
->fd
>= 0) {
1255 int control_sock_fd
= control_sock
->fd
;
1257 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
1258 &control_sock_fd
, 1, close_sock
,
1261 PERROR("Failed to close the control listener socket file descriptor");
1263 control_sock
->fd
= -1;
1265 lttcomm_destroy_sock(control_sock
);
1269 ERR("Health error occurred in %s", __func__
);
1271 health_unregister(health_relayd
);
1272 DBG("Relay listener thread cleanup complete");
1273 lttng_relay_stop_threads();
1278 * This thread manages the dispatching of the requests to worker threads
1280 static void *relay_thread_dispatcher(void *data
)
1284 struct cds_wfcq_node
*node
;
1285 struct relay_connection
*new_conn
= NULL
;
1287 DBG("[thread] Relay dispatcher started");
1289 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1291 if (testpoint(relayd_thread_dispatcher
)) {
1292 goto error_testpoint
;
1295 health_code_update();
1298 health_code_update();
1300 /* Atomically prepare the queue futex */
1301 futex_nto1_prepare(&relay_conn_queue
.futex
);
1303 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1308 health_code_update();
1310 /* Dequeue commands */
1311 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1312 &relay_conn_queue
.tail
);
1314 DBG("Woken up but nothing in the relay command queue");
1315 /* Continue thread execution */
1318 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1320 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1323 * Inform worker thread of the new request. This
1324 * call is blocking so we can be assured that
1325 * the data will be read at some point in time
1326 * or wait to the end of the world :)
1328 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1330 PERROR("write connection pipe");
1331 connection_put(new_conn
);
1334 } while (node
!= NULL
);
1336 /* Futex wait on queue. Blocking call on futex() */
1337 health_poll_entry();
1338 futex_nto1_wait(&relay_conn_queue
.futex
);
1342 /* Normal exit, no error */
1349 ERR("Health error occurred in %s", __func__
);
1351 health_unregister(health_relayd
);
1352 DBG("Dispatch thread dying");
1353 lttng_relay_stop_threads();
1357 static bool session_streams_have_index(const struct relay_session
*session
)
1359 return session
->minor
>= 4 && !session
->snapshot
;
1363 * Handle the RELAYD_CREATE_SESSION command.
1365 * On success, send back the session id or else return a negative value.
1367 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1368 struct relay_connection
*conn
,
1369 const struct lttng_buffer_view
*payload
)
1373 struct relay_session
*session
= NULL
;
1374 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1375 char session_name
[LTTNG_NAME_MAX
] = {};
1376 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1377 uint32_t live_timer
= 0;
1378 bool snapshot
= false;
1379 bool session_name_contains_creation_timestamp
= false;
1380 /* Left nil for peers < 2.11. */
1381 char base_path
[LTTNG_PATH_MAX
] = {};
1382 lttng_uuid sessiond_uuid
= {};
1383 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1384 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1385 LTTNG_OPTIONAL(time_t) creation_time
= {};
1386 struct lttng_dynamic_buffer reply_payload
;
1388 lttng_dynamic_buffer_init(&reply_payload
);
1390 if (conn
->minor
< 4) {
1391 /* From 2.1 to 2.3 */
1393 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1394 /* From 2.4 to 2.10 */
1395 ret
= cmd_create_session_2_4(payload
, session_name
,
1396 hostname
, &live_timer
, &snapshot
);
1398 bool has_current_chunk
;
1399 uint64_t current_chunk_id_value
;
1400 time_t creation_time_value
;
1401 uint64_t id_sessiond_value
;
1403 /* From 2.11 to ... */
1404 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1405 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1406 sessiond_uuid
, &has_current_chunk
,
1407 ¤t_chunk_id_value
, &creation_time_value
,
1408 &session_name_contains_creation_timestamp
);
1409 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1410 /* The nil UUID is reserved for pre-2.11 clients. */
1411 ERR("Illegal nil UUID announced by peer in create session command");
1415 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1416 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1417 if (has_current_chunk
) {
1418 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1419 current_chunk_id_value
);
1427 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1428 snapshot
, sessiond_uuid
,
1429 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1430 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1431 creation_time
.is_set
? &creation_time
.value
: NULL
,
1432 conn
->major
, conn
->minor
,
1433 session_name_contains_creation_timestamp
);
1438 assert(!conn
->session
);
1439 conn
->session
= session
;
1440 DBG("Created session %" PRIu64
, session
->id
);
1442 reply
.generic
.session_id
= htobe64(session
->id
);
1446 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1448 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1451 if (conn
->minor
< 11) {
1452 /* From 2.1 to 2.10 */
1453 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1454 &reply
.generic
, sizeof(reply
.generic
));
1456 ERR("Failed to append \"create session\" command reply header to payload buffer");
1461 const uint32_t output_path_length
=
1462 session
? strlen(session
->output_path
) + 1 : 0;
1464 reply
.output_path_length
= htobe32(output_path_length
);
1465 ret
= lttng_dynamic_buffer_append(
1466 &reply_payload
, &reply
, sizeof(reply
));
1468 ERR("Failed to append \"create session\" command reply header to payload buffer");
1472 if (output_path_length
) {
1473 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1474 session
->output_path
,
1475 output_path_length
);
1477 ERR("Failed to append \"create session\" command reply path to payload buffer");
1483 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1484 reply_payload
.size
, 0);
1485 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1486 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1487 reply_payload
.size
, send_ret
);
1491 if (ret
< 0 && session
) {
1492 session_put(session
);
1494 lttng_dynamic_buffer_reset(&reply_payload
);
1499 * When we have received all the streams and the metadata for a channel,
1500 * we make them visible to the viewer threads.
1502 static void publish_connection_local_streams(struct relay_connection
*conn
)
1504 struct relay_stream
*stream
;
1505 struct relay_session
*session
= conn
->session
;
1508 * We publish all streams belonging to a session atomically wrt
1511 pthread_mutex_lock(&session
->lock
);
1513 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1515 stream_publish(stream
);
1520 * Inform the viewer that there are new streams in the session.
1522 if (session
->viewer_attached
) {
1523 uatomic_set(&session
->new_streams
, 1);
1525 pthread_mutex_unlock(&session
->lock
);
1528 static int conform_channel_path(char *channel_path
)
1532 if (strstr("../", channel_path
)) {
1533 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1539 if (*channel_path
== '/') {
1540 const size_t len
= strlen(channel_path
);
1543 * Channel paths from peers prior to 2.11 are expressed as an
1544 * absolute path that is, in reality, relative to the relay
1545 * daemon's output directory. Remove the leading slash so it
1546 * is correctly interpreted as a relative path later on.
1548 * len (and not len - 1) is used to copy the trailing NULL.
1550 bcopy(channel_path
+ 1, channel_path
, len
);
1557 * relay_add_stream: allocate a new stream for a session
1559 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1560 struct relay_connection
*conn
,
1561 const struct lttng_buffer_view
*payload
)
1565 struct relay_session
*session
= conn
->session
;
1566 struct relay_stream
*stream
= NULL
;
1567 struct lttcomm_relayd_status_stream reply
;
1568 struct ctf_trace
*trace
= NULL
;
1569 uint64_t stream_handle
= -1ULL;
1570 char *path_name
= NULL
, *channel_name
= NULL
;
1571 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1572 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1574 if (!session
|| !conn
->version_check_done
) {
1575 ERR("Trying to add a stream before version check");
1577 goto end_no_session
;
1580 if (session
->minor
== 1) {
1582 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1584 } else if (session
->minor
> 1 && session
->minor
< 11) {
1585 /* From 2.2 to 2.10 */
1586 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1587 &channel_name
, &tracefile_size
, &tracefile_count
);
1589 /* From 2.11 to ... */
1590 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1591 &channel_name
, &tracefile_size
, &tracefile_count
,
1592 &stream_chunk_id
.value
);
1593 stream_chunk_id
.is_set
= true;
1600 if (conform_channel_path(path_name
)) {
1605 * Backward compatibility for --group-output-by-session.
1606 * Prior to lttng 2.11, the complete path is passed by the stream.
1607 * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
1608 * >=2.11 the chunk is responsible for the output path. When dealing
1609 * with producer < 2.11 the chunk output_path is the root output path
1610 * and the stream carries the complete path (path_name).
1611 * To support --group-output-by-session with older producer (<2.11), we
1612 * need to craft the path based on the stream path.
1614 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_SESSION
) {
1615 if (conn
->minor
< 4) {
1617 * From 2.1 to 2.3, the session_name is not passed on
1618 * the RELAYD_CREATE_SESSION command. The session name
1619 * is necessary to detect the presence of a base_path
1620 * inside the stream path. Without it we cannot perform
1621 * a valid group-output-by-session transformation.
1623 WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
1624 " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
1625 session
->id
, path_name
);
1626 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1627 char *group_by_session_path_name
;
1629 assert(session
->session_name
[0] != '\0');
1631 group_by_session_path_name
=
1632 backward_compat_group_by_session(
1634 session
->session_name
);
1635 if (!group_by_session_path_name
) {
1636 ERR("Failed to apply group by session to stream of session %" PRIu64
,
1641 DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
1642 path_name
, group_by_session_path_name
);
1645 path_name
= group_by_session_path_name
;
1649 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1654 /* This stream here has one reference on the trace. */
1655 pthread_mutex_lock(&last_relay_stream_id_lock
);
1656 stream_handle
= ++last_relay_stream_id
;
1657 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1659 /* We pass ownership of path_name and channel_name. */
1660 stream
= stream_create(trace
, stream_handle
, path_name
,
1661 channel_name
, tracefile_size
, tracefile_count
);
1663 channel_name
= NULL
;
1666 * Streams are the owners of their trace. Reference to trace is
1667 * kept within stream_create().
1669 ctf_trace_put(trace
);
1672 memset(&reply
, 0, sizeof(reply
));
1673 reply
.handle
= htobe64(stream_handle
);
1675 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1677 reply
.ret_code
= htobe32(LTTNG_OK
);
1680 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1681 sizeof(struct lttcomm_relayd_status_stream
), 0);
1682 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1683 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1695 * relay_close_stream: close a specific stream
1697 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1698 struct relay_connection
*conn
,
1699 const struct lttng_buffer_view
*payload
)
1703 struct relay_session
*session
= conn
->session
;
1704 struct lttcomm_relayd_close_stream stream_info
;
1705 struct lttcomm_relayd_generic_reply reply
;
1706 struct relay_stream
*stream
;
1708 DBG("Close stream received");
1710 if (!session
|| !conn
->version_check_done
) {
1711 ERR("Trying to close a stream before version check");
1713 goto end_no_session
;
1716 if (payload
->size
< sizeof(stream_info
)) {
1717 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1718 sizeof(stream_info
), payload
->size
);
1720 goto end_no_session
;
1722 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1723 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1724 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1726 stream
= stream_get_by_id(stream_info
.stream_id
);
1733 * Set last_net_seq_num before the close flag. Required by data
1736 pthread_mutex_lock(&stream
->lock
);
1737 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1738 pthread_mutex_unlock(&stream
->lock
);
1741 * This is one of the conditions which may trigger a stream close
1742 * with the others being:
1743 * 1) A close command is received for a stream
1744 * 2) The control connection owning the stream is closed
1745 * 3) We have received all of the stream's data _after_ a close
1748 try_stream_close(stream
);
1749 if (stream
->is_metadata
) {
1750 struct relay_viewer_stream
*vstream
;
1752 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1754 if (stream
->no_new_metadata_notified
) {
1756 * Since all the metadata has been sent to the
1757 * viewer and that we have a request to close
1758 * its stream, we can safely teardown the
1759 * corresponding metadata viewer stream.
1761 viewer_stream_put(vstream
);
1763 /* Put local reference. */
1764 viewer_stream_put(vstream
);
1771 memset(&reply
, 0, sizeof(reply
));
1773 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1775 reply
.ret_code
= htobe32(LTTNG_OK
);
1777 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1778 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1779 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1780 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1790 * relay_reset_metadata: reset a metadata stream
1793 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1794 struct relay_connection
*conn
,
1795 const struct lttng_buffer_view
*payload
)
1799 struct relay_session
*session
= conn
->session
;
1800 struct lttcomm_relayd_reset_metadata stream_info
;
1801 struct lttcomm_relayd_generic_reply reply
;
1802 struct relay_stream
*stream
;
1804 DBG("Reset metadata received");
1806 if (!session
|| !conn
->version_check_done
) {
1807 ERR("Trying to reset a metadata stream before version check");
1809 goto end_no_session
;
1812 if (payload
->size
< sizeof(stream_info
)) {
1813 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1814 sizeof(stream_info
), payload
->size
);
1816 goto end_no_session
;
1818 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1819 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1820 stream_info
.version
= be64toh(stream_info
.version
);
1822 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1824 /* Unsupported for live sessions for now. */
1825 if (session
->live_timer
!= 0) {
1830 stream
= stream_get_by_id(stream_info
.stream_id
);
1835 pthread_mutex_lock(&stream
->lock
);
1836 if (!stream
->is_metadata
) {
1841 ret
= stream_reset_file(stream
);
1843 ERR("Failed to reset metadata stream %" PRIu64
1844 ": stream_path = %s, channel = %s",
1845 stream
->stream_handle
, stream
->path_name
,
1846 stream
->channel_name
);
1850 pthread_mutex_unlock(&stream
->lock
);
1854 memset(&reply
, 0, sizeof(reply
));
1856 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1858 reply
.ret_code
= htobe32(LTTNG_OK
);
1860 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1861 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1862 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1863 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1873 * relay_unknown_command: send -1 if received unknown command
1875 static void relay_unknown_command(struct relay_connection
*conn
)
1877 struct lttcomm_relayd_generic_reply reply
;
1880 memset(&reply
, 0, sizeof(reply
));
1881 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1882 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1883 if (send_ret
< sizeof(reply
)) {
1884 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1889 * relay_start: send an acknowledgment to the client to tell if we are
1890 * ready to receive data. We are ready if a session is established.
1892 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1893 struct relay_connection
*conn
,
1894 const struct lttng_buffer_view
*payload
)
1898 struct lttcomm_relayd_generic_reply reply
;
1899 struct relay_session
*session
= conn
->session
;
1902 DBG("Trying to start the streaming without a session established");
1903 ret
= htobe32(LTTNG_ERR_UNK
);
1906 memset(&reply
, 0, sizeof(reply
));
1907 reply
.ret_code
= htobe32(LTTNG_OK
);
1908 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1910 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1911 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1920 * relay_recv_metadata: receive the metadata for the session.
1922 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1923 struct relay_connection
*conn
,
1924 const struct lttng_buffer_view
*payload
)
1927 struct relay_session
*session
= conn
->session
;
1928 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1929 struct relay_stream
*metadata_stream
;
1930 uint64_t metadata_payload_size
;
1931 struct lttng_buffer_view packet_view
;
1934 ERR("Metadata sent before version check");
1939 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1940 ERR("Incorrect data size");
1944 metadata_payload_size
= recv_hdr
->data_size
-
1945 sizeof(struct lttcomm_relayd_metadata_payload
);
1947 memcpy(&metadata_payload_header
, payload
->data
,
1948 sizeof(metadata_payload_header
));
1949 metadata_payload_header
.stream_id
= be64toh(
1950 metadata_payload_header
.stream_id
);
1951 metadata_payload_header
.padding_size
= be32toh(
1952 metadata_payload_header
.padding_size
);
1954 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1955 if (!metadata_stream
) {
1960 packet_view
= lttng_buffer_view_from_view(payload
,
1961 sizeof(metadata_payload_header
), metadata_payload_size
);
1962 if (!packet_view
.data
) {
1963 ERR("Invalid metadata packet length announced by header");
1968 pthread_mutex_lock(&metadata_stream
->lock
);
1969 ret
= stream_write(metadata_stream
, &packet_view
,
1970 metadata_payload_header
.padding_size
);
1971 pthread_mutex_unlock(&metadata_stream
->lock
);
1977 stream_put(metadata_stream
);
1983 * relay_send_version: send relayd version number
1985 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1986 struct relay_connection
*conn
,
1987 const struct lttng_buffer_view
*payload
)
1991 struct lttcomm_relayd_version reply
, msg
;
1992 bool compatible
= true;
1994 conn
->version_check_done
= true;
1996 /* Get version from the other side. */
1997 if (payload
->size
< sizeof(msg
)) {
1998 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1999 sizeof(msg
), payload
->size
);
2004 memcpy(&msg
, payload
->data
, sizeof(msg
));
2005 msg
.major
= be32toh(msg
.major
);
2006 msg
.minor
= be32toh(msg
.minor
);
2008 memset(&reply
, 0, sizeof(reply
));
2009 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
2010 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
2012 /* Major versions must be the same */
2013 if (reply
.major
!= msg
.major
) {
2014 DBG("Incompatible major versions (%u vs %u), deleting session",
2015 reply
.major
, msg
.major
);
2019 conn
->major
= reply
.major
;
2020 /* We adapt to the lowest compatible version */
2021 if (reply
.minor
<= msg
.minor
) {
2022 conn
->minor
= reply
.minor
;
2024 conn
->minor
= msg
.minor
;
2027 reply
.major
= htobe32(reply
.major
);
2028 reply
.minor
= htobe32(reply
.minor
);
2029 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2031 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2032 ERR("Failed to send \"send version\" command reply (ret = %zd)",
2045 DBG("Version check done using protocol %u.%u", conn
->major
,
2053 * Check for data pending for a given stream id from the session daemon.
2055 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2056 struct relay_connection
*conn
,
2057 const struct lttng_buffer_view
*payload
)
2059 struct relay_session
*session
= conn
->session
;
2060 struct lttcomm_relayd_data_pending msg
;
2061 struct lttcomm_relayd_generic_reply reply
;
2062 struct relay_stream
*stream
;
2065 uint64_t stream_seq
;
2067 DBG("Data pending command received");
2069 if (!session
|| !conn
->version_check_done
) {
2070 ERR("Trying to check for data before version check");
2072 goto end_no_session
;
2075 if (payload
->size
< sizeof(msg
)) {
2076 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
2077 sizeof(msg
), payload
->size
);
2079 goto end_no_session
;
2081 memcpy(&msg
, payload
->data
, sizeof(msg
));
2082 msg
.stream_id
= be64toh(msg
.stream_id
);
2083 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
2085 stream
= stream_get_by_id(msg
.stream_id
);
2086 if (stream
== NULL
) {
2091 pthread_mutex_lock(&stream
->lock
);
2093 if (session_streams_have_index(session
)) {
2095 * Ensure that both the index and stream data have been
2096 * flushed up to the requested point.
2098 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2100 stream_seq
= stream
->prev_data_seq
;
2102 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
2103 ", prev_index_seq %" PRIu64
2104 ", and last_seq %" PRIu64
, msg
.stream_id
,
2105 stream
->prev_data_seq
, stream
->prev_index_seq
,
2106 msg
.last_net_seq_num
);
2108 /* Avoid wrapping issue */
2109 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
2110 /* Data has in fact been written and is NOT pending */
2113 /* Data still being streamed thus pending */
2117 stream
->data_pending_check_done
= true;
2118 pthread_mutex_unlock(&stream
->lock
);
2123 memset(&reply
, 0, sizeof(reply
));
2124 reply
.ret_code
= htobe32(ret
);
2125 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2126 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2127 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
2137 * Wait for the control socket to reach a quiescent state.
2139 * Note that for now, when receiving this command from the session
2140 * daemon, this means that every subsequent commands or data received on
2141 * the control socket has been handled. So, this is why we simply return
2144 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2145 struct relay_connection
*conn
,
2146 const struct lttng_buffer_view
*payload
)
2150 struct relay_stream
*stream
;
2151 struct lttcomm_relayd_quiescent_control msg
;
2152 struct lttcomm_relayd_generic_reply reply
;
2154 DBG("Checking quiescent state on control socket");
2156 if (!conn
->session
|| !conn
->version_check_done
) {
2157 ERR("Trying to check for data before version check");
2159 goto end_no_session
;
2162 if (payload
->size
< sizeof(msg
)) {
2163 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2164 sizeof(msg
), payload
->size
);
2166 goto end_no_session
;
2168 memcpy(&msg
, payload
->data
, sizeof(msg
));
2169 msg
.stream_id
= be64toh(msg
.stream_id
);
2171 stream
= stream_get_by_id(msg
.stream_id
);
2175 pthread_mutex_lock(&stream
->lock
);
2176 stream
->data_pending_check_done
= true;
2177 pthread_mutex_unlock(&stream
->lock
);
2179 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2182 memset(&reply
, 0, sizeof(reply
));
2183 reply
.ret_code
= htobe32(LTTNG_OK
);
2184 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2185 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2186 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2198 * Initialize a data pending command. This means that a consumer is about
2199 * to ask for data pending for each stream it holds. Simply iterate over
2200 * all streams of a session and set the data_pending_check_done flag.
2202 * This command returns to the client a LTTNG_OK code.
2204 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2205 struct relay_connection
*conn
,
2206 const struct lttng_buffer_view
*payload
)
2210 struct lttng_ht_iter iter
;
2211 struct lttcomm_relayd_begin_data_pending msg
;
2212 struct lttcomm_relayd_generic_reply reply
;
2213 struct relay_stream
*stream
;
2218 DBG("Init streams for data pending");
2220 if (!conn
->session
|| !conn
->version_check_done
) {
2221 ERR("Trying to check for data before version check");
2223 goto end_no_session
;
2226 if (payload
->size
< sizeof(msg
)) {
2227 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2228 sizeof(msg
), payload
->size
);
2230 goto end_no_session
;
2232 memcpy(&msg
, payload
->data
, sizeof(msg
));
2233 msg
.session_id
= be64toh(msg
.session_id
);
2236 * Iterate over all streams to set the begin data pending flag.
2237 * For now, the streams are indexed by stream handle so we have
2238 * to iterate over all streams to find the one associated with
2239 * the right session_id.
2242 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2244 if (!stream_get(stream
)) {
2247 if (stream
->trace
->session
->id
== msg
.session_id
) {
2248 pthread_mutex_lock(&stream
->lock
);
2249 stream
->data_pending_check_done
= false;
2250 pthread_mutex_unlock(&stream
->lock
);
2251 DBG("Set begin data pending flag to stream %" PRIu64
,
2252 stream
->stream_handle
);
2258 memset(&reply
, 0, sizeof(reply
));
2259 /* All good, send back reply. */
2260 reply
.ret_code
= htobe32(LTTNG_OK
);
2262 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2263 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2264 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2276 * End data pending command. This will check, for a given session id, if
2277 * each stream associated with it has its data_pending_check_done flag
2278 * set. If not, this means that the client lost track of the stream but
2279 * the data is still being streamed on our side. In this case, we inform
2280 * the client that data is in flight.
2282 * Return to the client if there is data in flight or not with a ret_code.
2284 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2285 struct relay_connection
*conn
,
2286 const struct lttng_buffer_view
*payload
)
2290 struct lttng_ht_iter iter
;
2291 struct lttcomm_relayd_end_data_pending msg
;
2292 struct lttcomm_relayd_generic_reply reply
;
2293 struct relay_stream
*stream
;
2294 uint32_t is_data_inflight
= 0;
2296 DBG("End data pending command");
2298 if (!conn
->session
|| !conn
->version_check_done
) {
2299 ERR("Trying to check for data before version check");
2301 goto end_no_session
;
2304 if (payload
->size
< sizeof(msg
)) {
2305 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2306 sizeof(msg
), payload
->size
);
2308 goto end_no_session
;
2310 memcpy(&msg
, payload
->data
, sizeof(msg
));
2311 msg
.session_id
= be64toh(msg
.session_id
);
2314 * Iterate over all streams to see if the begin data pending
2318 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2320 if (!stream_get(stream
)) {
2323 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2327 pthread_mutex_lock(&stream
->lock
);
2328 if (!stream
->data_pending_check_done
) {
2329 uint64_t stream_seq
;
2331 if (session_streams_have_index(conn
->session
)) {
2333 * Ensure that both the index and stream data have been
2334 * flushed up to the requested point.
2336 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2338 stream_seq
= stream
->prev_data_seq
;
2340 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2341 is_data_inflight
= 1;
2342 DBG("Data is still in flight for stream %" PRIu64
,
2343 stream
->stream_handle
);
2344 pthread_mutex_unlock(&stream
->lock
);
2349 pthread_mutex_unlock(&stream
->lock
);
2354 memset(&reply
, 0, sizeof(reply
));
2355 /* All good, send back reply. */
2356 reply
.ret_code
= htobe32(is_data_inflight
);
2358 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2359 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2360 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2372 * Receive an index for a specific stream.
2374 * Return 0 on success else a negative value.
2376 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2377 struct relay_connection
*conn
,
2378 const struct lttng_buffer_view
*payload
)
2382 struct relay_session
*session
= conn
->session
;
2383 struct lttcomm_relayd_index index_info
;
2384 struct lttcomm_relayd_generic_reply reply
;
2385 struct relay_stream
*stream
;
2390 DBG("Relay receiving index");
2392 if (!session
|| !conn
->version_check_done
) {
2393 ERR("Trying to close a stream before version check");
2395 goto end_no_session
;
2398 msg_len
= lttcomm_relayd_index_len(
2399 lttng_to_index_major(conn
->major
, conn
->minor
),
2400 lttng_to_index_minor(conn
->major
, conn
->minor
));
2401 if (payload
->size
< msg_len
) {
2402 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2403 msg_len
, payload
->size
);
2405 goto end_no_session
;
2407 memcpy(&index_info
, payload
->data
, msg_len
);
2408 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2409 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2410 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2411 index_info
.content_size
= be64toh(index_info
.content_size
);
2412 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2413 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2414 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2415 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2417 if (conn
->minor
>= 8) {
2418 index_info
.stream_instance_id
=
2419 be64toh(index_info
.stream_instance_id
);
2420 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2422 index_info
.stream_instance_id
= -1ULL;
2423 index_info
.packet_seq_num
= -1ULL;
2426 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2428 ERR("stream_get_by_id not found");
2433 pthread_mutex_lock(&stream
->lock
);
2434 ret
= stream_add_index(stream
, &index_info
);
2435 pthread_mutex_unlock(&stream
->lock
);
2437 goto end_stream_put
;
2443 memset(&reply
, 0, sizeof(reply
));
2445 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2447 reply
.ret_code
= htobe32(LTTNG_OK
);
2449 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2450 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2451 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2460 * Receive the streams_sent message.
2462 * Return 0 on success else a negative value.
2464 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2465 struct relay_connection
*conn
,
2466 const struct lttng_buffer_view
*payload
)
2470 struct lttcomm_relayd_generic_reply reply
;
2474 DBG("Relay receiving streams_sent");
2476 if (!conn
->session
|| !conn
->version_check_done
) {
2477 ERR("Trying to close a stream before version check");
2479 goto end_no_session
;
2483 * Publish every pending stream in the connection recv list which are
2484 * now ready to be used by the viewer.
2486 publish_connection_local_streams(conn
);
2488 memset(&reply
, 0, sizeof(reply
));
2489 reply
.ret_code
= htobe32(LTTNG_OK
);
2490 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2491 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2492 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2505 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2506 * session rotation feature (not the tracefile rotation feature).
2508 static int relay_rotate_session_streams(
2509 const struct lttcomm_relayd_hdr
*recv_hdr
,
2510 struct relay_connection
*conn
,
2511 const struct lttng_buffer_view
*payload
)
2516 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2517 struct relay_session
*session
= conn
->session
;
2518 struct lttcomm_relayd_rotate_streams rotate_streams
;
2519 struct lttcomm_relayd_generic_reply reply
= {};
2520 struct relay_stream
*stream
= NULL
;
2521 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2522 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2523 struct lttng_buffer_view stream_positions
;
2524 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2525 const char *chunk_id_str
= "none";
2527 if (!session
|| !conn
->version_check_done
) {
2528 ERR("Trying to rotate a stream before version check");
2533 if (session
->major
== 2 && session
->minor
< 11) {
2534 ERR("Unsupported feature before 2.11");
2539 if (payload
->size
< header_len
) {
2540 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2541 header_len
, payload
->size
);
2546 memcpy(&rotate_streams
, payload
->data
, header_len
);
2548 /* Convert header to host endianness. */
2549 rotate_streams
= (typeof(rotate_streams
)) {
2550 .stream_count
= be32toh(rotate_streams
.stream_count
),
2551 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2552 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2553 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2557 if (rotate_streams
.new_chunk_id
.is_set
) {
2559 * Retrieve the trace chunk the stream must transition to. As
2560 * per the protocol, this chunk should have been created
2561 * before this command is received.
2563 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2564 sessiond_trace_chunk_registry
,
2565 session
->sessiond_uuid
, session
->id
,
2566 rotate_streams
.new_chunk_id
.value
);
2567 if (!next_trace_chunk
) {
2568 char uuid_str
[LTTNG_UUID_STR_LEN
];
2570 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2571 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2572 ", trace_chunk_id = %" PRIu64
,
2573 uuid_str
, session
->id
,
2574 rotate_streams
.new_chunk_id
.value
);
2575 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2580 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2581 rotate_streams
.new_chunk_id
.value
);
2582 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2583 chunk_id_str
= "formatting error";
2585 chunk_id_str
= chunk_id_buf
;
2589 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2590 rotate_streams
.stream_count
, session
->session_name
,
2593 stream_positions
= lttng_buffer_view_from_view(payload
,
2594 sizeof(rotate_streams
), -1);
2595 if (!stream_positions
.data
||
2596 stream_positions
.size
<
2597 (rotate_streams
.stream_count
*
2598 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2599 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2604 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2605 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2606 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2607 const struct lttcomm_relayd_stream_rotation_position pos
= {
2608 .stream_id
= be64toh(position_comm
->stream_id
),
2609 .rotate_at_seq_num
= be64toh(
2610 position_comm
->rotate_at_seq_num
),
2613 stream
= stream_get_by_id(pos
.stream_id
);
2615 reply_code
= LTTNG_ERR_INVALID
;
2620 pthread_mutex_lock(&stream
->lock
);
2621 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2622 pos
.rotate_at_seq_num
);
2623 pthread_mutex_unlock(&stream
->lock
);
2625 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2633 reply_code
= LTTNG_OK
;
2640 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2641 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2642 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2643 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2644 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2649 lttng_trace_chunk_put(next_trace_chunk
);
2656 * relay_create_trace_chunk: create a new trace chunk
2658 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2659 struct relay_connection
*conn
,
2660 const struct lttng_buffer_view
*payload
)
2664 struct relay_session
*session
= conn
->session
;
2665 struct lttcomm_relayd_create_trace_chunk
*msg
;
2666 struct lttcomm_relayd_generic_reply reply
= {};
2667 struct lttng_buffer_view header_view
;
2668 struct lttng_buffer_view chunk_name_view
;
2669 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2670 enum lttng_error_code reply_code
= LTTNG_OK
;
2671 enum lttng_trace_chunk_status chunk_status
;
2672 const char *new_path
;
2674 if (!session
|| !conn
->version_check_done
) {
2675 ERR("Trying to create a trace chunk before version check");
2680 if (session
->major
== 2 && session
->minor
< 11) {
2681 ERR("Chunk creation command is unsupported before 2.11");
2686 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2687 if (!header_view
.data
) {
2688 ERR("Failed to receive payload of chunk creation command");
2693 /* Convert to host endianness. */
2694 msg
= (typeof(msg
)) header_view
.data
;
2695 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2696 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2697 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2699 if (session
->current_trace_chunk
&&
2700 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2701 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2702 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
2703 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2704 ERR("Failed to rename old chunk");
2706 reply_code
= LTTNG_ERR_UNK
;
2710 session
->ongoing_rotation
= true;
2711 if (!session
->current_trace_chunk
) {
2712 if (!session
->has_rotated
) {
2718 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
2720 chunk
= lttng_trace_chunk_create(
2721 msg
->chunk_id
, msg
->creation_timestamp
, new_path
);
2723 ERR("Failed to create trace chunk in trace chunk creation command");
2725 reply_code
= LTTNG_ERR_NOMEM
;
2729 if (msg
->override_name_length
) {
2732 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2734 msg
->override_name_length
);
2735 name
= chunk_name_view
.data
;
2736 if (!name
|| name
[msg
->override_name_length
- 1]) {
2737 ERR("Failed to receive payload of chunk creation command");
2739 reply_code
= LTTNG_ERR_INVALID
;
2743 chunk_status
= lttng_trace_chunk_override_name(
2744 chunk
, chunk_name_view
.data
);
2745 switch (chunk_status
) {
2746 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2748 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2749 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2750 reply_code
= LTTNG_ERR_INVALID
;
2754 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2755 reply_code
= LTTNG_ERR_UNK
;
2761 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2762 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2763 reply_code
= LTTNG_ERR_UNK
;
2768 assert(conn
->session
->output_directory
);
2769 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
,
2770 conn
->session
->output_directory
);
2771 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2772 reply_code
= LTTNG_ERR_UNK
;
2777 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2778 sessiond_trace_chunk_registry
,
2779 conn
->session
->sessiond_uuid
,
2782 if (!published_chunk
) {
2783 char uuid_str
[LTTNG_UUID_STR_LEN
];
2785 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2786 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2791 reply_code
= LTTNG_ERR_NOMEM
;
2795 pthread_mutex_lock(&conn
->session
->lock
);
2796 if (conn
->session
->pending_closure_trace_chunk
) {
2798 * Invalid; this means a second create_trace_chunk command was
2799 * received before a close_trace_chunk.
2801 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2802 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2804 goto end_unlock_session
;
2806 conn
->session
->pending_closure_trace_chunk
=
2807 conn
->session
->current_trace_chunk
;
2808 conn
->session
->current_trace_chunk
= published_chunk
;
2809 published_chunk
= NULL
;
2810 if (!conn
->session
->pending_closure_trace_chunk
) {
2811 session
->ongoing_rotation
= false;
2814 pthread_mutex_unlock(&conn
->session
->lock
);
2816 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2817 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2819 sizeof(struct lttcomm_relayd_generic_reply
),
2821 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2822 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2827 lttng_trace_chunk_put(chunk
);
2828 lttng_trace_chunk_put(published_chunk
);
2833 * relay_close_trace_chunk: close a trace chunk
2835 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2836 struct relay_connection
*conn
,
2837 const struct lttng_buffer_view
*payload
)
2839 int ret
= 0, buf_ret
;
2841 struct relay_session
*session
= conn
->session
;
2842 struct lttcomm_relayd_close_trace_chunk
*msg
;
2843 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2844 struct lttng_buffer_view header_view
;
2845 struct lttng_trace_chunk
*chunk
= NULL
;
2846 enum lttng_error_code reply_code
= LTTNG_OK
;
2847 enum lttng_trace_chunk_status chunk_status
;
2849 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2850 time_t close_timestamp
;
2851 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2852 size_t path_length
= 0;
2853 const char *chunk_name
= NULL
;
2854 struct lttng_dynamic_buffer reply_payload
;
2855 const char *new_path
;
2857 lttng_dynamic_buffer_init(&reply_payload
);
2859 if (!session
|| !conn
->version_check_done
) {
2860 ERR("Trying to close a trace chunk before version check");
2865 if (session
->major
== 2 && session
->minor
< 11) {
2866 ERR("Chunk close command is unsupported before 2.11");
2871 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2872 if (!header_view
.data
) {
2873 ERR("Failed to receive payload of chunk close command");
2878 /* Convert to host endianness. */
2879 msg
= (typeof(msg
)) header_view
.data
;
2880 chunk_id
= be64toh(msg
->chunk_id
);
2881 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2882 close_command
= (typeof(close_command
)){
2883 .value
= be32toh(msg
->close_command
.value
),
2884 .is_set
= msg
->close_command
.is_set
,
2887 chunk
= sessiond_trace_chunk_registry_get_chunk(
2888 sessiond_trace_chunk_registry
,
2889 conn
->session
->sessiond_uuid
,
2893 char uuid_str
[LTTNG_UUID_STR_LEN
];
2895 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2896 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2901 reply_code
= LTTNG_ERR_NOMEM
;
2905 pthread_mutex_lock(&session
->lock
);
2906 if (close_command
.is_set
&&
2907 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
) {
2909 * Clear command. It is a protocol error to ask for a
2910 * clear on a relay which does not allow it. Querying
2911 * the configuration allows figuring out whether
2912 * clearing is allowed before doing the clear.
2914 if (!opt_allow_clear
) {
2916 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2917 goto end_unlock_session
;
2920 if (session
->pending_closure_trace_chunk
&&
2921 session
->pending_closure_trace_chunk
!= chunk
) {
2922 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2923 session
->session_name
);
2924 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2926 goto end_unlock_session
;
2929 if (session
->current_trace_chunk
&& session
->current_trace_chunk
!= chunk
&&
2930 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2931 if (close_command
.is_set
&&
2932 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&&
2933 !session
->has_rotated
) {
2934 /* New chunk stays in session output directory. */
2937 /* Use chunk name for new chunk. */
2940 /* Rename new chunk path. */
2941 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2943 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2947 session
->ongoing_rotation
= false;
2949 if ((!close_command
.is_set
||
2950 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) &&
2951 !lttng_trace_chunk_get_name_overridden(chunk
)) {
2952 const char *old_path
;
2954 if (!session
->has_rotated
) {
2959 /* We need to move back the .tmp_old_chunk to its rightful place. */
2960 chunk_status
= lttng_trace_chunk_rename_path(chunk
, old_path
);
2961 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2966 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2967 chunk
, close_timestamp
);
2968 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2969 ERR("Failed to set trace chunk close timestamp");
2971 reply_code
= LTTNG_ERR_UNK
;
2972 goto end_unlock_session
;
2975 if (close_command
.is_set
) {
2976 chunk_status
= lttng_trace_chunk_set_close_command(
2977 chunk
, close_command
.value
);
2978 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2980 reply_code
= LTTNG_ERR_INVALID
;
2981 goto end_unlock_session
;
2984 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2985 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2986 ERR("Failed to get chunk name");
2988 reply_code
= LTTNG_ERR_UNK
;
2989 goto end_unlock_session
;
2991 if (!session
->has_rotated
&& !session
->snapshot
) {
2992 ret
= lttng_strncpy(closed_trace_chunk_path
,
2993 session
->output_path
,
2994 sizeof(closed_trace_chunk_path
));
2996 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2997 strlen(session
->output_path
),
2998 sizeof(closed_trace_chunk_path
));
2999 reply_code
= LTTNG_ERR_NOMEM
;
3001 goto end_unlock_session
;
3004 if (session
->snapshot
) {
3005 ret
= snprintf(closed_trace_chunk_path
,
3006 sizeof(closed_trace_chunk_path
),
3007 "%s/%s", session
->output_path
,
3010 ret
= snprintf(closed_trace_chunk_path
,
3011 sizeof(closed_trace_chunk_path
),
3012 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
3014 session
->output_path
, chunk_name
);
3016 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
3017 ERR("Failed to format closed trace chunk resulting path");
3018 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
3020 goto end_unlock_session
;
3023 if (close_command
.is_set
&&
3024 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
3025 session
->has_rotated
= true;
3027 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
3028 path_length
= strlen(closed_trace_chunk_path
) + 1;
3029 if (path_length
> UINT32_MAX
) {
3030 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
3032 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3033 goto end_unlock_session
;
3036 if (session
->current_trace_chunk
== chunk
) {
3038 * After a trace chunk close command, no new streams
3039 * referencing the chunk may be created. Hence, on the
3040 * event that no new trace chunk have been created for
3041 * the session, the reference to the current trace chunk
3042 * is released in order to allow it to be reclaimed when
3043 * the last stream releases its reference to it.
3045 lttng_trace_chunk_put(session
->current_trace_chunk
);
3046 session
->current_trace_chunk
= NULL
;
3048 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
3049 session
->pending_closure_trace_chunk
= NULL
;
3051 pthread_mutex_unlock(&session
->lock
);
3054 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
3055 reply
.path_length
= htobe32((uint32_t) path_length
);
3056 buf_ret
= lttng_dynamic_buffer_append(
3057 &reply_payload
, &reply
, sizeof(reply
));
3059 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
3063 if (reply_code
== LTTNG_OK
) {
3064 buf_ret
= lttng_dynamic_buffer_append(&reply_payload
,
3065 closed_trace_chunk_path
, path_length
);
3067 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
3072 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
3076 if (send_ret
< reply_payload
.size
) {
3077 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
3078 reply_payload
.size
, send_ret
);
3083 lttng_trace_chunk_put(chunk
);
3084 lttng_dynamic_buffer_reset(&reply_payload
);
3089 * relay_trace_chunk_exists: check if a trace chunk exists
3091 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
3092 struct relay_connection
*conn
,
3093 const struct lttng_buffer_view
*payload
)
3097 struct relay_session
*session
= conn
->session
;
3098 struct lttcomm_relayd_trace_chunk_exists
*msg
;
3099 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
3100 struct lttng_buffer_view header_view
;
3104 if (!session
|| !conn
->version_check_done
) {
3105 ERR("Trying to close a trace chunk before version check");
3110 if (session
->major
== 2 && session
->minor
< 11) {
3111 ERR("Chunk close command is unsupported before 2.11");
3116 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3117 if (!header_view
.data
) {
3118 ERR("Failed to receive payload of chunk close command");
3123 /* Convert to host endianness. */
3124 msg
= (typeof(msg
)) header_view
.data
;
3125 chunk_id
= be64toh(msg
->chunk_id
);
3127 ret
= sessiond_trace_chunk_registry_chunk_exists(
3128 sessiond_trace_chunk_registry
,
3129 conn
->session
->sessiond_uuid
,
3131 chunk_id
, &chunk_exists
);
3133 * If ret is not 0, send the reply and report the error to the caller.
3134 * It is a protocol (or internal) error and the session/connection
3135 * should be torn down.
3137 reply
= (typeof(reply
)){
3138 .generic
.ret_code
= htobe32((uint32_t)
3139 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3140 .trace_chunk_exists
= ret
== 0 ? chunk_exists
: 0,
3142 send_ret
= conn
->sock
->ops
->sendmsg(
3143 conn
->sock
, &reply
, sizeof(reply
), 0);
3144 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3145 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
3154 * relay_get_configuration: query whether feature is available
3156 static int relay_get_configuration(const struct lttcomm_relayd_hdr
*recv_hdr
,
3157 struct relay_connection
*conn
,
3158 const struct lttng_buffer_view
*payload
)
3162 struct lttcomm_relayd_get_configuration
*msg
;
3163 struct lttcomm_relayd_get_configuration_reply reply
= {};
3164 struct lttng_buffer_view header_view
;
3165 uint64_t query_flags
= 0;
3166 uint64_t result_flags
= 0;
3168 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3169 if (!header_view
.data
) {
3170 ERR("Failed to receive payload of chunk close command");
3175 /* Convert to host endianness. */
3176 msg
= (typeof(msg
)) header_view
.data
;
3177 query_flags
= be64toh(msg
->query_flags
);
3180 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
3183 if (opt_allow_clear
) {
3184 result_flags
|= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
;
3188 reply
= (typeof(reply
)){
3189 .generic
.ret_code
= htobe32((uint32_t)
3190 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3191 .relayd_configuration_flags
= htobe64(result_flags
),
3193 send_ret
= conn
->sock
->ops
->sendmsg(
3194 conn
->sock
, &reply
, sizeof(reply
), 0);
3195 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3196 ERR("Failed to send \"get configuration\" command reply (ret = %zd)",
3204 #define DBG_CMD(cmd_name, conn) \
3205 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3207 static int relay_process_control_command(struct relay_connection
*conn
,
3208 const struct lttcomm_relayd_hdr
*header
,
3209 const struct lttng_buffer_view
*payload
)
3213 switch (header
->cmd
) {
3214 case RELAYD_CREATE_SESSION
:
3215 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
3216 ret
= relay_create_session(header
, conn
, payload
);
3218 case RELAYD_ADD_STREAM
:
3219 DBG_CMD("RELAYD_ADD_STREAM", conn
);
3220 ret
= relay_add_stream(header
, conn
, payload
);
3222 case RELAYD_START_DATA
:
3223 DBG_CMD("RELAYD_START_DATA", conn
);
3224 ret
= relay_start(header
, conn
, payload
);
3226 case RELAYD_SEND_METADATA
:
3227 DBG_CMD("RELAYD_SEND_METADATA", conn
);
3228 ret
= relay_recv_metadata(header
, conn
, payload
);
3230 case RELAYD_VERSION
:
3231 DBG_CMD("RELAYD_VERSION", conn
);
3232 ret
= relay_send_version(header
, conn
, payload
);
3234 case RELAYD_CLOSE_STREAM
:
3235 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
3236 ret
= relay_close_stream(header
, conn
, payload
);
3238 case RELAYD_DATA_PENDING
:
3239 DBG_CMD("RELAYD_DATA_PENDING", conn
);
3240 ret
= relay_data_pending(header
, conn
, payload
);
3242 case RELAYD_QUIESCENT_CONTROL
:
3243 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
3244 ret
= relay_quiescent_control(header
, conn
, payload
);
3246 case RELAYD_BEGIN_DATA_PENDING
:
3247 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
3248 ret
= relay_begin_data_pending(header
, conn
, payload
);
3250 case RELAYD_END_DATA_PENDING
:
3251 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
3252 ret
= relay_end_data_pending(header
, conn
, payload
);
3254 case RELAYD_SEND_INDEX
:
3255 DBG_CMD("RELAYD_SEND_INDEX", conn
);
3256 ret
= relay_recv_index(header
, conn
, payload
);
3258 case RELAYD_STREAMS_SENT
:
3259 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
3260 ret
= relay_streams_sent(header
, conn
, payload
);
3262 case RELAYD_RESET_METADATA
:
3263 DBG_CMD("RELAYD_RESET_METADATA", conn
);
3264 ret
= relay_reset_metadata(header
, conn
, payload
);
3266 case RELAYD_ROTATE_STREAMS
:
3267 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
3268 ret
= relay_rotate_session_streams(header
, conn
, payload
);
3270 case RELAYD_CREATE_TRACE_CHUNK
:
3271 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
3272 ret
= relay_create_trace_chunk(header
, conn
, payload
);
3274 case RELAYD_CLOSE_TRACE_CHUNK
:
3275 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
3276 ret
= relay_close_trace_chunk(header
, conn
, payload
);
3278 case RELAYD_TRACE_CHUNK_EXISTS
:
3279 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
3280 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
3282 case RELAYD_GET_CONFIGURATION
:
3283 DBG_CMD("RELAYD_GET_CONFIGURATION", conn
);
3284 ret
= relay_get_configuration(header
, conn
, payload
);
3286 case RELAYD_UPDATE_SYNC_INFO
:
3288 ERR("Received unknown command (%u)", header
->cmd
);
3289 relay_unknown_command(conn
);
3298 static enum relay_connection_status
relay_process_control_receive_payload(
3299 struct relay_connection
*conn
)
3302 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3303 struct lttng_dynamic_buffer
*reception_buffer
=
3304 &conn
->protocol
.ctrl
.reception_buffer
;
3305 struct ctrl_connection_state_receive_payload
*state
=
3306 &conn
->protocol
.ctrl
.state
.receive_payload
;
3307 struct lttng_buffer_view payload_view
;
3309 if (state
->left_to_receive
== 0) {
3310 /* Short-circuit for payload-less commands. */
3311 goto reception_complete
;
3314 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3315 reception_buffer
->data
+ state
->received
,
3316 state
->left_to_receive
, MSG_DONTWAIT
);
3318 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3319 PERROR("Unable to receive command payload on sock %d",
3321 status
= RELAY_CONNECTION_STATUS_ERROR
;
3324 } else if (ret
== 0) {
3325 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3326 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3331 assert(ret
<= state
->left_to_receive
);
3333 state
->left_to_receive
-= ret
;
3334 state
->received
+= ret
;
3336 if (state
->left_to_receive
> 0) {
3338 * Can't transition to the protocol's next state, wait to
3339 * receive the rest of the header.
3341 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3342 state
->received
, state
->left_to_receive
,
3348 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3349 conn
->sock
->fd
, state
->received
);
3351 * The payload required to process the command has been received.
3352 * A view to the reception buffer is forwarded to the various
3353 * commands and the state of the control is reset on success.
3355 * Commands are responsible for sending their reply to the peer.
3357 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3359 ret
= relay_process_control_command(conn
,
3360 &state
->header
, &payload_view
);
3362 status
= RELAY_CONNECTION_STATUS_ERROR
;
3366 ret
= connection_reset_protocol_state(conn
);
3368 status
= RELAY_CONNECTION_STATUS_ERROR
;
3374 static enum relay_connection_status
relay_process_control_receive_header(
3375 struct relay_connection
*conn
)
3378 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3379 struct lttcomm_relayd_hdr header
;
3380 struct lttng_dynamic_buffer
*reception_buffer
=
3381 &conn
->protocol
.ctrl
.reception_buffer
;
3382 struct ctrl_connection_state_receive_header
*state
=
3383 &conn
->protocol
.ctrl
.state
.receive_header
;
3385 assert(state
->left_to_receive
!= 0);
3387 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3388 reception_buffer
->data
+ state
->received
,
3389 state
->left_to_receive
, MSG_DONTWAIT
);
3391 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3392 PERROR("Unable to receive control command header on sock %d",
3394 status
= RELAY_CONNECTION_STATUS_ERROR
;
3397 } else if (ret
== 0) {
3398 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3399 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3404 assert(ret
<= state
->left_to_receive
);
3406 state
->left_to_receive
-= ret
;
3407 state
->received
+= ret
;
3409 if (state
->left_to_receive
> 0) {
3411 * Can't transition to the protocol's next state, wait to
3412 * receive the rest of the header.
3414 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3415 state
->received
, state
->left_to_receive
,
3420 /* Transition to next state: receiving the command's payload. */
3421 conn
->protocol
.ctrl
.state_id
=
3422 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3423 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3424 header
.circuit_id
= be64toh(header
.circuit_id
);
3425 header
.data_size
= be64toh(header
.data_size
);
3426 header
.cmd
= be32toh(header
.cmd
);
3427 header
.cmd_version
= be32toh(header
.cmd_version
);
3428 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3429 &header
, sizeof(header
));
3431 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3432 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3435 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3436 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3438 status
= RELAY_CONNECTION_STATUS_ERROR
;
3442 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3444 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3445 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3448 status
= RELAY_CONNECTION_STATUS_ERROR
;
3452 if (header
.data_size
== 0) {
3454 * Manually invoke the next state as the poll loop
3455 * will not wake-up to allow us to proceed further.
3457 status
= relay_process_control_receive_payload(conn
);
3464 * Process the commands received on the control socket
3466 static enum relay_connection_status
relay_process_control(
3467 struct relay_connection
*conn
)
3469 enum relay_connection_status status
;
3471 switch (conn
->protocol
.ctrl
.state_id
) {
3472 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3473 status
= relay_process_control_receive_header(conn
);
3475 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3476 status
= relay_process_control_receive_payload(conn
);
3479 ERR("Unknown control connection protocol state encountered.");
3486 static enum relay_connection_status
relay_process_data_receive_header(
3487 struct relay_connection
*conn
)
3490 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3491 struct data_connection_state_receive_header
*state
=
3492 &conn
->protocol
.data
.state
.receive_header
;
3493 struct lttcomm_relayd_data_hdr header
;
3494 struct relay_stream
*stream
;
3496 assert(state
->left_to_receive
!= 0);
3498 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3499 state
->header_reception_buffer
+ state
->received
,
3500 state
->left_to_receive
, MSG_DONTWAIT
);
3502 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3503 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3504 status
= RELAY_CONNECTION_STATUS_ERROR
;
3507 } else if (ret
== 0) {
3508 /* Orderly shutdown. Not necessary to print an error. */
3509 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3510 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3515 assert(ret
<= state
->left_to_receive
);
3517 state
->left_to_receive
-= ret
;
3518 state
->received
+= ret
;
3520 if (state
->left_to_receive
> 0) {
3522 * Can't transition to the protocol's next state, wait to
3523 * receive the rest of the header.
3525 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3526 state
->received
, state
->left_to_receive
,
3531 /* Transition to next state: receiving the payload. */
3532 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3534 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3535 header
.circuit_id
= be64toh(header
.circuit_id
);
3536 header
.stream_id
= be64toh(header
.stream_id
);
3537 header
.data_size
= be32toh(header
.data_size
);
3538 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3539 header
.padding_size
= be32toh(header
.padding_size
);
3540 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3542 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3544 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3545 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3547 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3548 conn
->sock
->fd
, header
.circuit_id
,
3549 header
.stream_id
, header
.data_size
,
3550 header
.net_seq_num
, header
.padding_size
);
3552 stream
= stream_get_by_id(header
.stream_id
);
3554 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3556 /* Protocol error. */
3557 status
= RELAY_CONNECTION_STATUS_ERROR
;
3561 pthread_mutex_lock(&stream
->lock
);
3562 /* Prepare stream for the reception of a new packet. */
3563 ret
= stream_init_packet(stream
, header
.data_size
,
3564 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3565 pthread_mutex_unlock(&stream
->lock
);
3567 ERR("Failed to rotate stream output file");
3568 status
= RELAY_CONNECTION_STATUS_ERROR
;
3569 goto end_stream_unlock
;
3578 static enum relay_connection_status
relay_process_data_receive_payload(
3579 struct relay_connection
*conn
)
3582 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3583 struct relay_stream
*stream
;
3584 struct data_connection_state_receive_payload
*state
=
3585 &conn
->protocol
.data
.state
.receive_payload
;
3586 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3587 char data_buffer
[chunk_size
];
3588 bool partial_recv
= false;
3589 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3590 uint64_t left_to_receive
= state
->left_to_receive
;
3591 struct relay_session
*session
;
3593 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3594 state
->header
.stream_id
, state
->header
.net_seq_num
,
3595 state
->received
, left_to_receive
);
3597 stream
= stream_get_by_id(state
->header
.stream_id
);
3599 /* Protocol error. */
3600 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3601 state
->header
.stream_id
);
3602 status
= RELAY_CONNECTION_STATUS_ERROR
;
3606 pthread_mutex_lock(&stream
->lock
);
3607 session
= stream
->trace
->session
;
3608 if (!conn
->session
) {
3609 ret
= connection_set_session(conn
, session
);
3611 status
= RELAY_CONNECTION_STATUS_ERROR
;
3612 goto end_stream_unlock
;
3617 * The size of the "chunk" received on any iteration is bounded by:
3618 * - the data left to receive,
3619 * - the data immediately available on the socket,
3620 * - the on-stack data buffer
3622 while (left_to_receive
> 0 && !partial_recv
) {
3623 size_t recv_size
= min(left_to_receive
, chunk_size
);
3624 struct lttng_buffer_view packet_chunk
;
3626 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3627 recv_size
, MSG_DONTWAIT
);
3629 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3630 PERROR("Socket %d error", conn
->sock
->fd
);
3631 status
= RELAY_CONNECTION_STATUS_ERROR
;
3633 goto end_stream_unlock
;
3634 } else if (ret
== 0) {
3635 /* No more data ready to be consumed on socket. */
3636 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,