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>
38 #include <urcu/futex.h>
39 #include <urcu/uatomic.h>
40 #include <urcu/rculist.h>
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/compat/getenv.h>
51 #include <common/defaults.h>
52 #include <common/daemonize.h>
53 #include <common/futex.h>
54 #include <common/sessiond-comm/sessiond-comm.h>
55 #include <common/sessiond-comm/inet.h>
56 #include <common/sessiond-comm/relayd.h>
57 #include <common/uri.h>
58 #include <common/utils.h>
59 #include <common/align.h>
60 #include <common/config/session-config.h>
61 #include <common/dynamic-buffer.h>
62 #include <common/buffer-view.h>
63 #include <common/string-utils/format.h>
65 #include "backward-compatibility-group-by.h"
67 #include "connection.h"
68 #include "ctf-trace.h"
69 #include "health-relayd.h"
72 #include "lttng-relayd.h"
74 #include "sessiond-trace-chunks.h"
76 #include "tcp_keep_alive.h"
77 #include "testpoint.h"
78 #include "tracefile-array.h"
81 #include "viewer-stream.h"
83 static const char *help_msg
=
84 #ifdef LTTNG_EMBED_HELP
85 #include <lttng-relayd.8.h>
91 enum relay_connection_status
{
92 RELAY_CONNECTION_STATUS_OK
,
93 /* An error occurred while processing an event on the connection. */
94 RELAY_CONNECTION_STATUS_ERROR
,
95 /* Connection closed/shutdown cleanly. */
96 RELAY_CONNECTION_STATUS_CLOSED
,
99 /* command line options */
100 char *opt_output_path
, *opt_working_directory
;
101 static int opt_daemon
, opt_background
, opt_print_version
, opt_allow_clear
= 1;
102 enum relay_group_output_by opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
;
105 * We need to wait for listener and live listener threads, as well as
106 * health check thread, before being ready to signal readiness.
108 #define NR_LTTNG_RELAY_READY 3
109 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
111 /* Size of receive buffer. */
112 #define RECV_DATA_BUFFER_SIZE 65536
114 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
115 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
117 static struct lttng_uri
*control_uri
;
118 static struct lttng_uri
*data_uri
;
119 static struct lttng_uri
*live_uri
;
121 const char *progname
;
123 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
124 static int tracing_group_name_override
;
126 const char * const config_section_name
= "relayd";
129 * Quit pipe for all threads. This permits a single cancellation point
130 * for all threads when receiving an event on the pipe.
132 int thread_quit_pipe
[2] = { -1, -1 };
135 * This pipe is used to inform the worker thread that a command is queued and
136 * ready to be processed.
138 static int relay_conn_pipe
[2] = { -1, -1 };
140 /* Shared between threads */
141 static int dispatch_thread_exit
;
143 static pthread_t listener_thread
;
144 static pthread_t dispatcher_thread
;
145 static pthread_t worker_thread
;
146 static pthread_t health_thread
;
149 * last_relay_stream_id_lock protects last_relay_stream_id increment
150 * atomicity on 32-bit architectures.
152 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
153 static uint64_t last_relay_stream_id
;
156 * Relay command queue.
158 * The relay_thread_listener and relay_thread_dispatcher communicate with this
161 static struct relay_conn_queue relay_conn_queue
;
163 /* Global relay stream hash table. */
164 struct lttng_ht
*relay_streams_ht
;
166 /* Global relay viewer stream hash table. */
167 struct lttng_ht
*viewer_streams_ht
;
169 /* Global relay sessions hash table. */
170 struct lttng_ht
*sessions_ht
;
172 /* Relayd health monitoring */
173 struct health_app
*health_relayd
;
175 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
177 static struct option long_options
[] = {
178 { "control-port", 1, 0, 'C', },
179 { "data-port", 1, 0, 'D', },
180 { "live-port", 1, 0, 'L', },
181 { "daemonize", 0, 0, 'd', },
182 { "background", 0, 0, 'b', },
183 { "group", 1, 0, 'g', },
184 { "help", 0, 0, 'h', },
185 { "output", 1, 0, 'o', },
186 { "verbose", 0, 0, 'v', },
187 { "config", 1, 0, 'f' },
188 { "version", 0, 0, 'V' },
189 { "working-directory", 1, 0, 'w', },
190 { "group-output-by-session", 0, 0, 's', },
191 { "group-output-by-host", 0, 0, 'p', },
192 { "disallow-clear", 0, 0, 'x' },
196 static const char *config_ignore_options
[] = { "help", "config", "version" };
198 static void print_version(void) {
199 fprintf(stdout
, "%s\n", VERSION
);
202 static void relayd_config_log(void)
204 DBG("LTTng-relayd " VERSION
" - " VERSION_NAME
"%s%s",
205 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
206 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
207 if (EXTRA_VERSION_DESCRIPTION
[0] != '\0') {
208 DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION
"\n");
210 if (EXTRA_VERSION_PATCHES
[0] != '\0') {
211 DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES
"\n");
216 * Take an option from the getopt output and set it in the right variable to be
219 * Return 0 on success else a negative value.
221 static int set_option(int opt
, const char *arg
, const char *optname
)
227 fprintf(stderr
, "option %s", optname
);
229 fprintf(stderr
, " with arg %s\n", arg
);
233 if (lttng_is_setuid_setgid()) {
234 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
235 "-C, --control-port");
237 ret
= uri_parse(arg
, &control_uri
);
239 ERR("Invalid control URI specified");
242 if (control_uri
->port
== 0) {
243 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
248 if (lttng_is_setuid_setgid()) {
249 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
252 ret
= uri_parse(arg
, &data_uri
);
254 ERR("Invalid data URI specified");
257 if (data_uri
->port
== 0) {
258 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
263 if (lttng_is_setuid_setgid()) {
264 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
267 ret
= uri_parse(arg
, &live_uri
);
269 ERR("Invalid live URI specified");
272 if (live_uri
->port
== 0) {
273 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
284 if (lttng_is_setuid_setgid()) {
285 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
288 tracing_group_name
= strdup(arg
);
289 if (tracing_group_name
== NULL
) {
294 tracing_group_name_override
= 1;
298 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
300 ERR("Cannot show --help for `lttng-relayd`");
305 opt_print_version
= 1;
308 if (lttng_is_setuid_setgid()) {
309 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
312 ret
= asprintf(&opt_output_path
, "%s", arg
);
315 PERROR("asprintf opt_output_path");
321 if (lttng_is_setuid_setgid()) {
322 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
323 "-w, --working-directory");
325 ret
= asprintf(&opt_working_directory
, "%s", arg
);
328 PERROR("asprintf opt_working_directory");
335 /* Verbose level can increase using multiple -v */
337 lttng_opt_verbose
= config_parse_value(arg
);
339 /* Only 3 level of verbosity (-vvv). */
340 if (lttng_opt_verbose
< 3) {
341 lttng_opt_verbose
+= 1;
346 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
347 ERR("Cannot set --group-output-by-session, another --group-output-by argument is present");
350 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_SESSION
;
353 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
354 ERR("Cannot set --group-output-by-host, another --group-output-by argument is present");
357 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
364 /* Unknown option or other error.
365 * Error is printed by getopt, just return */
378 * config_entry_handler_cb used to handle options read from a config file.
379 * See config_entry_handler_cb comment in common/config/session-config.h for the
380 * return value conventions.
382 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
386 if (!entry
|| !entry
->name
|| !entry
->value
) {
391 /* Check if the option is to be ignored */
392 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
393 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
398 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
399 /* Ignore if entry name is not fully matched. */
400 if (strcmp(entry
->name
, long_options
[i
].name
)) {
405 * If the option takes no argument on the command line,
406 * we have to check if the value is "true". We support
407 * non-zero numeric values, true, on and yes.
409 if (!long_options
[i
].has_arg
) {
410 ret
= config_parse_value(entry
->value
);
413 WARN("Invalid configuration value \"%s\" for option %s",
414 entry
->value
, entry
->name
);
416 /* False, skip boolean config option. */
421 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
425 WARN("Unrecognized option \"%s\" in daemon configuration file.",
432 static int parse_env_options(void)
437 value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV
);
439 opt_working_directory
= strdup(value
);
440 if (!opt_working_directory
) {
441 ERR("Failed to allocate working directory string (\"%s\")",
449 static int set_options(int argc
, char **argv
)
451 int c
, ret
= 0, option_index
= 0, retval
= 0;
452 int orig_optopt
= optopt
, orig_optind
= optind
;
453 char *default_address
, *optstring
;
454 const char *config_path
= NULL
;
456 optstring
= utils_generate_optstring(long_options
,
457 sizeof(long_options
) / sizeof(struct option
));
463 /* Check for the --config option */
465 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
466 &option_index
)) != -1) {
470 } else if (c
!= 'f') {
474 if (lttng_is_setuid_setgid()) {
475 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
478 config_path
= utils_expand_path(optarg
);
480 ERR("Failed to resolve path: %s", optarg
);
485 ret
= config_get_section_entries(config_path
, config_section_name
,
486 config_entry_handler
, NULL
);
489 ERR("Invalid configuration option at line %i", ret
);
495 /* Reset getopt's global state */
496 optopt
= orig_optopt
;
497 optind
= orig_optind
;
499 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
504 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
511 /* assign default values */
512 if (control_uri
== NULL
) {
513 ret
= asprintf(&default_address
,
514 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
515 DEFAULT_NETWORK_CONTROL_PORT
);
517 PERROR("asprintf default data address");
522 ret
= uri_parse(default_address
, &control_uri
);
523 free(default_address
);
525 ERR("Invalid control URI specified");
530 if (data_uri
== NULL
) {
531 ret
= asprintf(&default_address
,
532 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
533 DEFAULT_NETWORK_DATA_PORT
);
535 PERROR("asprintf default data address");
540 ret
= uri_parse(default_address
, &data_uri
);
541 free(default_address
);
543 ERR("Invalid data URI specified");
548 if (live_uri
== NULL
) {
549 ret
= asprintf(&default_address
,
550 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
551 DEFAULT_NETWORK_VIEWER_PORT
);
553 PERROR("asprintf default viewer control address");
558 ret
= uri_parse(default_address
, &live_uri
);
559 free(default_address
);
561 ERR("Invalid viewer control URI specified");
567 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
568 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
570 if (opt_allow_clear
) {
571 /* Check if env variable exists. */
572 const char *value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
574 ret
= config_parse_value(value
);
576 ERR("Invalid value for %s specified", DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
580 opt_allow_clear
= !ret
;
589 static void print_global_objects(void)
591 rcu_register_thread();
593 print_viewer_streams();
594 print_relay_streams();
597 rcu_unregister_thread();
603 static void relayd_cleanup(void)
605 print_global_objects();
609 if (viewer_streams_ht
)
610 lttng_ht_destroy(viewer_streams_ht
);
611 if (relay_streams_ht
)
612 lttng_ht_destroy(relay_streams_ht
);
614 lttng_ht_destroy(sessions_ht
);
616 free(opt_output_path
);
617 free(opt_working_directory
);
619 /* Close thread quit pipes */
620 utils_close_pipe(thread_quit_pipe
);
622 uri_free(control_uri
);
624 /* Live URI is freed in the live thread. */
626 if (tracing_group_name_override
) {
627 free((void *) tracing_group_name
);
632 * Write to writable pipe used to notify a thread.
634 static int notify_thread_pipe(int wpipe
)
638 ret
= lttng_write(wpipe
, "!", 1);
640 PERROR("write poll pipe");
648 static int notify_health_quit_pipe(int *pipe
)
652 ret
= lttng_write(pipe
[1], "4", 1);
654 PERROR("write relay health quit");
663 * Stop all relayd and relayd-live threads.
665 int lttng_relay_stop_threads(void)
669 /* Stopping all threads */
670 DBG("Terminating all threads");
671 if (notify_thread_pipe(thread_quit_pipe
[1])) {
672 ERR("write error on thread quit pipe");
676 if (notify_health_quit_pipe(health_quit_pipe
)) {
677 ERR("write error on health quit pipe");
680 /* Dispatch thread */
681 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
682 futex_nto1_wake(&relay_conn_queue
.futex
);
684 if (relayd_live_stop()) {
685 ERR("Error stopping live threads");
692 * Signal handler for the daemon
694 * Simply stop all worker threads, leaving main() return gracefully after
695 * joining all threads and calling cleanup().
697 static void sighandler(int sig
)
701 DBG("SIGINT caught");
702 if (lttng_relay_stop_threads()) {
703 ERR("Error stopping threads");
707 DBG("SIGTERM caught");
708 if (lttng_relay_stop_threads()) {
709 ERR("Error stopping threads");
713 CMM_STORE_SHARED(recv_child_signal
, 1);
721 * Setup signal handler for :
722 * SIGINT, SIGTERM, SIGPIPE
724 static int set_signal_handler(void)
730 if ((ret
= sigemptyset(&sigset
)) < 0) {
731 PERROR("sigemptyset");
738 sa
.sa_handler
= sighandler
;
739 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
744 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
749 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
754 sa
.sa_handler
= SIG_IGN
;
755 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
760 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
765 void lttng_relay_notify_ready(void)
767 /* Notify the parent of the fork() process that we are ready. */
768 if (opt_daemon
|| opt_background
) {
769 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
770 kill(child_ppid
, SIGUSR1
);
776 * Init thread quit pipe.
778 * Return -1 on error or 0 if all pipes are created.
780 static int init_thread_quit_pipe(void)
784 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
790 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
792 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
796 if (events
== NULL
|| size
== 0) {
801 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
807 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
819 * Check if the thread quit pipe was triggered.
821 * Return 1 if it was triggered else 0;
823 static int check_thread_quit_pipe(int fd
, uint32_t events
)
825 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
833 * Create and init socket from uri.
835 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
838 struct lttcomm_sock
*sock
= NULL
;
840 sock
= lttcomm_alloc_sock_from_uri(uri
);
842 ERR("Allocating socket");
846 ret
= lttcomm_create_sock(sock
);
850 DBG("Listening on sock %d", sock
->fd
);
852 ret
= sock
->ops
->bind(sock
);
854 PERROR("Failed to bind socket");
858 ret
= sock
->ops
->listen(sock
, -1);
868 lttcomm_destroy_sock(sock
);
874 * This thread manages the listening for new connections on the network
876 static void *relay_thread_listener(void *data
)
878 int i
, ret
, pollfd
, err
= -1;
879 uint32_t revents
, nb_fd
;
880 struct lttng_poll_event events
;
881 struct lttcomm_sock
*control_sock
, *data_sock
;
883 DBG("[thread] Relay listener started");
885 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
887 health_code_update();
889 control_sock
= relay_socket_create(control_uri
);
891 goto error_sock_control
;
894 data_sock
= relay_socket_create(data_uri
);
896 goto error_sock_relay
;
900 * Pass 3 as size here for the thread quit pipe, control and
903 ret
= create_thread_poll_set(&events
, 3);
905 goto error_create_poll
;
908 /* Add the control socket */
909 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
914 /* Add the data socket */
915 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
920 lttng_relay_notify_ready();
922 if (testpoint(relayd_thread_listener
)) {
923 goto error_testpoint
;
927 health_code_update();
929 DBG("Listener accepting connections");
933 ret
= lttng_poll_wait(&events
, -1);
937 * Restart interrupted system call.
939 if (errno
== EINTR
) {
947 DBG("Relay new connection received");
948 for (i
= 0; i
< nb_fd
; i
++) {
949 health_code_update();
951 /* Fetch once the poll data */
952 revents
= LTTNG_POLL_GETEV(&events
, i
);
953 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
955 /* Thread quit pipe has been closed. Killing thread. */
956 ret
= check_thread_quit_pipe(pollfd
, revents
);
962 if (revents
& LPOLLIN
) {
964 * A new connection is requested, therefore a
965 * sessiond/consumerd connection is allocated in
966 * this thread, enqueued to a global queue and
967 * dequeued (and freed) in the worker thread.
970 struct relay_connection
*new_conn
;
971 struct lttcomm_sock
*newsock
;
972 enum connection_type type
;
974 if (pollfd
== data_sock
->fd
) {
976 newsock
= data_sock
->ops
->accept(data_sock
);
977 DBG("Relay data connection accepted, socket %d",
980 assert(pollfd
== control_sock
->fd
);
981 type
= RELAY_CONTROL
;
982 newsock
= control_sock
->ops
->accept(control_sock
);
983 DBG("Relay control connection accepted, socket %d",
987 PERROR("accepting sock");
991 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
994 PERROR("setsockopt inet");
995 lttcomm_destroy_sock(newsock
);
999 ret
= socket_apply_keep_alive_config(newsock
->fd
);
1001 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1003 lttcomm_destroy_sock(newsock
);
1007 new_conn
= connection_create(newsock
, type
);
1009 lttcomm_destroy_sock(newsock
);
1013 /* Enqueue request for the dispatcher thread. */
1014 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
1018 * Wake the dispatch queue futex.
1019 * Implicit memory barrier with the
1020 * exchange in cds_wfcq_enqueue.
1022 futex_nto1_wake(&relay_conn_queue
.futex
);
1023 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1024 ERR("socket poll error");
1027 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1037 lttng_poll_clean(&events
);
1039 if (data_sock
->fd
>= 0) {
1040 ret
= data_sock
->ops
->close(data_sock
);
1045 lttcomm_destroy_sock(data_sock
);
1047 if (control_sock
->fd
>= 0) {
1048 ret
= control_sock
->ops
->close(control_sock
);
1053 lttcomm_destroy_sock(control_sock
);
1057 ERR("Health error occurred in %s", __func__
);
1059 health_unregister(health_relayd
);
1060 DBG("Relay listener thread cleanup complete");
1061 lttng_relay_stop_threads();
1066 * This thread manages the dispatching of the requests to worker threads
1068 static void *relay_thread_dispatcher(void *data
)
1072 struct cds_wfcq_node
*node
;
1073 struct relay_connection
*new_conn
= NULL
;
1075 DBG("[thread] Relay dispatcher started");
1077 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1079 if (testpoint(relayd_thread_dispatcher
)) {
1080 goto error_testpoint
;
1083 health_code_update();
1086 health_code_update();
1088 /* Atomically prepare the queue futex */
1089 futex_nto1_prepare(&relay_conn_queue
.futex
);
1091 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1096 health_code_update();
1098 /* Dequeue commands */
1099 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1100 &relay_conn_queue
.tail
);
1102 DBG("Woken up but nothing in the relay command queue");
1103 /* Continue thread execution */
1106 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1108 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1111 * Inform worker thread of the new request. This
1112 * call is blocking so we can be assured that
1113 * the data will be read at some point in time
1114 * or wait to the end of the world :)
1116 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1118 PERROR("write connection pipe");
1119 connection_put(new_conn
);
1122 } while (node
!= NULL
);
1124 /* Futex wait on queue. Blocking call on futex() */
1125 health_poll_entry();
1126 futex_nto1_wait(&relay_conn_queue
.futex
);
1130 /* Normal exit, no error */
1137 ERR("Health error occurred in %s", __func__
);
1139 health_unregister(health_relayd
);
1140 DBG("Dispatch thread dying");
1141 lttng_relay_stop_threads();
1145 static bool session_streams_have_index(const struct relay_session
*session
)
1147 return session
->minor
>= 4 && !session
->snapshot
;
1151 * Handle the RELAYD_CREATE_SESSION command.
1153 * On success, send back the session id or else return a negative value.
1155 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1156 struct relay_connection
*conn
,
1157 const struct lttng_buffer_view
*payload
)
1161 struct relay_session
*session
= NULL
;
1162 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1163 char session_name
[LTTNG_NAME_MAX
] = {};
1164 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1165 uint32_t live_timer
= 0;
1166 bool snapshot
= false;
1167 bool session_name_contains_creation_timestamp
= false;
1168 /* Left nil for peers < 2.11. */
1169 char base_path
[LTTNG_PATH_MAX
] = {};
1170 lttng_uuid sessiond_uuid
= {};
1171 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1172 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1173 LTTNG_OPTIONAL(time_t) creation_time
= {};
1174 struct lttng_dynamic_buffer reply_payload
;
1176 lttng_dynamic_buffer_init(&reply_payload
);
1178 if (conn
->minor
< 4) {
1179 /* From 2.1 to 2.3 */
1181 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1182 /* From 2.4 to 2.10 */
1183 ret
= cmd_create_session_2_4(payload
, session_name
,
1184 hostname
, &live_timer
, &snapshot
);
1186 bool has_current_chunk
;
1187 uint64_t current_chunk_id_value
;
1188 time_t creation_time_value
;
1189 uint64_t id_sessiond_value
;
1191 /* From 2.11 to ... */
1192 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1193 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1194 sessiond_uuid
, &has_current_chunk
,
1195 ¤t_chunk_id_value
, &creation_time_value
,
1196 &session_name_contains_creation_timestamp
);
1197 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1198 /* The nil UUID is reserved for pre-2.11 clients. */
1199 ERR("Illegal nil UUID announced by peer in create session command");
1203 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1204 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1205 if (has_current_chunk
) {
1206 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1207 current_chunk_id_value
);
1215 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1216 snapshot
, sessiond_uuid
,
1217 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1218 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1219 creation_time
.is_set
? &creation_time
.value
: NULL
,
1220 conn
->major
, conn
->minor
,
1221 session_name_contains_creation_timestamp
);
1226 assert(!conn
->session
);
1227 conn
->session
= session
;
1228 DBG("Created session %" PRIu64
, session
->id
);
1230 reply
.generic
.session_id
= htobe64(session
->id
);
1234 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1236 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1239 if (conn
->minor
< 11) {
1240 /* From 2.1 to 2.10 */
1241 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1242 &reply
.generic
, sizeof(reply
.generic
));
1244 ERR("Failed to append \"create session\" command reply header to payload buffer");
1249 const uint32_t output_path_length
=
1250 session
? strlen(session
->output_path
) + 1 : 0;
1252 reply
.output_path_length
= htobe32(output_path_length
);
1253 ret
= lttng_dynamic_buffer_append(
1254 &reply_payload
, &reply
, sizeof(reply
));
1256 ERR("Failed to append \"create session\" command reply header to payload buffer");
1260 if (output_path_length
) {
1261 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1262 session
->output_path
,
1263 output_path_length
);
1265 ERR("Failed to append \"create session\" command reply path to payload buffer");
1271 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1272 reply_payload
.size
, 0);
1273 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1274 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1275 reply_payload
.size
, send_ret
);
1279 if (ret
< 0 && session
) {
1280 session_put(session
);
1282 lttng_dynamic_buffer_reset(&reply_payload
);
1287 * When we have received all the streams and the metadata for a channel,
1288 * we make them visible to the viewer threads.
1290 static void publish_connection_local_streams(struct relay_connection
*conn
)
1292 struct relay_stream
*stream
;
1293 struct relay_session
*session
= conn
->session
;
1296 * We publish all streams belonging to a session atomically wrt
1299 pthread_mutex_lock(&session
->lock
);
1301 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1303 stream_publish(stream
);
1308 * Inform the viewer that there are new streams in the session.
1310 if (session
->viewer_attached
) {
1311 uatomic_set(&session
->new_streams
, 1);
1313 pthread_mutex_unlock(&session
->lock
);
1316 static int conform_channel_path(char *channel_path
)
1320 if (strstr("../", channel_path
)) {
1321 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1327 if (*channel_path
== '/') {
1328 const size_t len
= strlen(channel_path
);
1331 * Channel paths from peers prior to 2.11 are expressed as an
1332 * absolute path that is, in reality, relative to the relay
1333 * daemon's output directory. Remove the leading slash so it
1334 * is correctly interpreted as a relative path later on.
1336 * len (and not len - 1) is used to copy the trailing NULL.
1338 bcopy(channel_path
+ 1, channel_path
, len
);
1345 * relay_add_stream: allocate a new stream for a session
1347 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1348 struct relay_connection
*conn
,
1349 const struct lttng_buffer_view
*payload
)
1353 struct relay_session
*session
= conn
->session
;
1354 struct relay_stream
*stream
= NULL
;
1355 struct lttcomm_relayd_status_stream reply
;
1356 struct ctf_trace
*trace
= NULL
;
1357 uint64_t stream_handle
= -1ULL;
1358 char *path_name
= NULL
, *channel_name
= NULL
;
1359 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1360 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1362 if (!session
|| !conn
->version_check_done
) {
1363 ERR("Trying to add a stream before version check");
1365 goto end_no_session
;
1368 if (session
->minor
== 1) {
1370 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1372 } else if (session
->minor
> 1 && session
->minor
< 11) {
1373 /* From 2.2 to 2.10 */
1374 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1375 &channel_name
, &tracefile_size
, &tracefile_count
);
1377 /* From 2.11 to ... */
1378 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1379 &channel_name
, &tracefile_size
, &tracefile_count
,
1380 &stream_chunk_id
.value
);
1381 stream_chunk_id
.is_set
= true;
1388 if (conform_channel_path(path_name
)) {
1393 * Backward compatibility for --group-output-by-session.
1394 * Prior to lttng 2.11, the complete path is passed by the stream.
1395 * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
1396 * >=2.11 the chunk is responsible for the output path. When dealing
1397 * with producer < 2.11 the chunk output_path is the root output path
1398 * and the stream carries the complete path (path_name).
1399 * To support --group-output-by-session with older producer (<2.11), we
1400 * need to craft the path based on the stream path.
1402 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_SESSION
) {
1403 if (conn
->minor
< 4) {
1405 * From 2.1 to 2.3, the session_name is not passed on
1406 * the RELAYD_CREATE_SESSION command. The session name
1407 * is necessary to detect the presence of a base_path
1408 * inside the stream path. Without it we cannot perform
1409 * a valid group-output-by-session transformation.
1411 WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
1412 " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
1413 session
->id
, path_name
);
1414 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1415 char *group_by_session_path_name
;
1417 assert(session
->session_name
[0] != '\0');
1419 group_by_session_path_name
=
1420 backward_compat_group_by_session(
1422 session
->session_name
);
1423 if (!group_by_session_path_name
) {
1424 ERR("Failed to apply group by session to stream of session %" PRIu64
,
1429 DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
1430 path_name
, group_by_session_path_name
);
1433 path_name
= group_by_session_path_name
;
1437 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1442 /* This stream here has one reference on the trace. */
1443 pthread_mutex_lock(&last_relay_stream_id_lock
);
1444 stream_handle
= ++last_relay_stream_id
;
1445 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1447 /* We pass ownership of path_name and channel_name. */
1448 stream
= stream_create(trace
, stream_handle
, path_name
,
1449 channel_name
, tracefile_size
, tracefile_count
);
1451 channel_name
= NULL
;
1454 * Streams are the owners of their trace. Reference to trace is
1455 * kept within stream_create().
1457 ctf_trace_put(trace
);
1460 memset(&reply
, 0, sizeof(reply
));
1461 reply
.handle
= htobe64(stream_handle
);
1463 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1465 reply
.ret_code
= htobe32(LTTNG_OK
);
1468 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1469 sizeof(struct lttcomm_relayd_status_stream
), 0);
1470 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1471 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1483 * relay_close_stream: close a specific stream
1485 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1486 struct relay_connection
*conn
,
1487 const struct lttng_buffer_view
*payload
)
1491 struct relay_session
*session
= conn
->session
;
1492 struct lttcomm_relayd_close_stream stream_info
;
1493 struct lttcomm_relayd_generic_reply reply
;
1494 struct relay_stream
*stream
;
1496 DBG("Close stream received");
1498 if (!session
|| !conn
->version_check_done
) {
1499 ERR("Trying to close a stream before version check");
1501 goto end_no_session
;
1504 if (payload
->size
< sizeof(stream_info
)) {
1505 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1506 sizeof(stream_info
), payload
->size
);
1508 goto end_no_session
;
1510 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1511 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1512 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1514 stream
= stream_get_by_id(stream_info
.stream_id
);
1521 * Set last_net_seq_num before the close flag. Required by data
1524 pthread_mutex_lock(&stream
->lock
);
1525 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1526 pthread_mutex_unlock(&stream
->lock
);
1529 * This is one of the conditions which may trigger a stream close
1530 * with the others being:
1531 * 1) A close command is received for a stream
1532 * 2) The control connection owning the stream is closed
1533 * 3) We have received all of the stream's data _after_ a close
1536 try_stream_close(stream
);
1537 if (stream
->is_metadata
) {
1538 struct relay_viewer_stream
*vstream
;
1540 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1542 if (stream
->no_new_metadata_notified
) {
1544 * Since all the metadata has been sent to the
1545 * viewer and that we have a request to close
1546 * its stream, we can safely teardown the
1547 * corresponding metadata viewer stream.
1549 viewer_stream_put(vstream
);
1551 /* Put local reference. */
1552 viewer_stream_put(vstream
);
1559 memset(&reply
, 0, sizeof(reply
));
1561 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1563 reply
.ret_code
= htobe32(LTTNG_OK
);
1565 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1566 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1567 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1568 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1578 * relay_reset_metadata: reset a metadata stream
1581 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1582 struct relay_connection
*conn
,
1583 const struct lttng_buffer_view
*payload
)
1587 struct relay_session
*session
= conn
->session
;
1588 struct lttcomm_relayd_reset_metadata stream_info
;
1589 struct lttcomm_relayd_generic_reply reply
;
1590 struct relay_stream
*stream
;
1592 DBG("Reset metadata received");
1594 if (!session
|| !conn
->version_check_done
) {
1595 ERR("Trying to reset a metadata stream before version check");
1597 goto end_no_session
;
1600 if (payload
->size
< sizeof(stream_info
)) {
1601 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1602 sizeof(stream_info
), payload
->size
);
1604 goto end_no_session
;
1606 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1607 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1608 stream_info
.version
= be64toh(stream_info
.version
);
1610 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1612 /* Unsupported for live sessions for now. */
1613 if (session
->live_timer
!= 0) {
1618 stream
= stream_get_by_id(stream_info
.stream_id
);
1623 pthread_mutex_lock(&stream
->lock
);
1624 if (!stream
->is_metadata
) {
1629 ret
= stream_reset_file(stream
);
1631 ERR("Failed to reset metadata stream %" PRIu64
1632 ": stream_path = %s, channel = %s",
1633 stream
->stream_handle
, stream
->path_name
,
1634 stream
->channel_name
);
1638 pthread_mutex_unlock(&stream
->lock
);
1642 memset(&reply
, 0, sizeof(reply
));
1644 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1646 reply
.ret_code
= htobe32(LTTNG_OK
);
1648 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1649 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1650 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1651 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1661 * relay_unknown_command: send -1 if received unknown command
1663 static void relay_unknown_command(struct relay_connection
*conn
)
1665 struct lttcomm_relayd_generic_reply reply
;
1668 memset(&reply
, 0, sizeof(reply
));
1669 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1670 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1671 if (send_ret
< sizeof(reply
)) {
1672 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1677 * relay_start: send an acknowledgment to the client to tell if we are
1678 * ready to receive data. We are ready if a session is established.
1680 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1681 struct relay_connection
*conn
,
1682 const struct lttng_buffer_view
*payload
)
1686 struct lttcomm_relayd_generic_reply reply
;
1687 struct relay_session
*session
= conn
->session
;
1690 DBG("Trying to start the streaming without a session established");
1691 ret
= htobe32(LTTNG_ERR_UNK
);
1694 memset(&reply
, 0, sizeof(reply
));
1695 reply
.ret_code
= htobe32(LTTNG_OK
);
1696 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1698 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1699 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1708 * relay_recv_metadata: receive the metadata for the session.
1710 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1711 struct relay_connection
*conn
,
1712 const struct lttng_buffer_view
*payload
)
1715 struct relay_session
*session
= conn
->session
;
1716 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1717 struct relay_stream
*metadata_stream
;
1718 uint64_t metadata_payload_size
;
1719 struct lttng_buffer_view packet_view
;
1722 ERR("Metadata sent before version check");
1727 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1728 ERR("Incorrect data size");
1732 metadata_payload_size
= recv_hdr
->data_size
-
1733 sizeof(struct lttcomm_relayd_metadata_payload
);
1735 memcpy(&metadata_payload_header
, payload
->data
,
1736 sizeof(metadata_payload_header
));
1737 metadata_payload_header
.stream_id
= be64toh(
1738 metadata_payload_header
.stream_id
);
1739 metadata_payload_header
.padding_size
= be32toh(
1740 metadata_payload_header
.padding_size
);
1742 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1743 if (!metadata_stream
) {
1748 packet_view
= lttng_buffer_view_from_view(payload
,
1749 sizeof(metadata_payload_header
), metadata_payload_size
);
1750 if (!packet_view
.data
) {
1751 ERR("Invalid metadata packet length announced by header");
1756 pthread_mutex_lock(&metadata_stream
->lock
);
1757 ret
= stream_write(metadata_stream
, &packet_view
,
1758 metadata_payload_header
.padding_size
);
1759 pthread_mutex_unlock(&metadata_stream
->lock
);
1765 stream_put(metadata_stream
);
1771 * relay_send_version: send relayd version number
1773 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1774 struct relay_connection
*conn
,
1775 const struct lttng_buffer_view
*payload
)
1779 struct lttcomm_relayd_version reply
, msg
;
1780 bool compatible
= true;
1782 conn
->version_check_done
= true;
1784 /* Get version from the other side. */
1785 if (payload
->size
< sizeof(msg
)) {
1786 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1787 sizeof(msg
), payload
->size
);
1792 memcpy(&msg
, payload
->data
, sizeof(msg
));
1793 msg
.major
= be32toh(msg
.major
);
1794 msg
.minor
= be32toh(msg
.minor
);
1796 memset(&reply
, 0, sizeof(reply
));
1797 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1798 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1800 /* Major versions must be the same */
1801 if (reply
.major
!= msg
.major
) {
1802 DBG("Incompatible major versions (%u vs %u), deleting session",
1803 reply
.major
, msg
.major
);
1807 conn
->major
= reply
.major
;
1808 /* We adapt to the lowest compatible version */
1809 if (reply
.minor
<= msg
.minor
) {
1810 conn
->minor
= reply
.minor
;
1812 conn
->minor
= msg
.minor
;
1815 reply
.major
= htobe32(reply
.major
);
1816 reply
.minor
= htobe32(reply
.minor
);
1817 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1819 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1820 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1833 DBG("Version check done using protocol %u.%u", conn
->major
,
1841 * Check for data pending for a given stream id from the session daemon.
1843 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1844 struct relay_connection
*conn
,
1845 const struct lttng_buffer_view
*payload
)
1847 struct relay_session
*session
= conn
->session
;
1848 struct lttcomm_relayd_data_pending msg
;
1849 struct lttcomm_relayd_generic_reply reply
;
1850 struct relay_stream
*stream
;
1853 uint64_t stream_seq
;
1855 DBG("Data pending command received");
1857 if (!session
|| !conn
->version_check_done
) {
1858 ERR("Trying to check for data before version check");
1860 goto end_no_session
;
1863 if (payload
->size
< sizeof(msg
)) {
1864 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1865 sizeof(msg
), payload
->size
);
1867 goto end_no_session
;
1869 memcpy(&msg
, payload
->data
, sizeof(msg
));
1870 msg
.stream_id
= be64toh(msg
.stream_id
);
1871 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1873 stream
= stream_get_by_id(msg
.stream_id
);
1874 if (stream
== NULL
) {
1879 pthread_mutex_lock(&stream
->lock
);
1881 if (session_streams_have_index(session
)) {
1883 * Ensure that both the index and stream data have been
1884 * flushed up to the requested point.
1886 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1888 stream_seq
= stream
->prev_data_seq
;
1890 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
1891 ", prev_index_seq %" PRIu64
1892 ", and last_seq %" PRIu64
, msg
.stream_id
,
1893 stream
->prev_data_seq
, stream
->prev_index_seq
,
1894 msg
.last_net_seq_num
);
1896 /* Avoid wrapping issue */
1897 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
1898 /* Data has in fact been written and is NOT pending */
1901 /* Data still being streamed thus pending */
1905 stream
->data_pending_check_done
= true;
1906 pthread_mutex_unlock(&stream
->lock
);
1911 memset(&reply
, 0, sizeof(reply
));
1912 reply
.ret_code
= htobe32(ret
);
1913 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1914 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1915 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1925 * Wait for the control socket to reach a quiescent state.
1927 * Note that for now, when receiving this command from the session
1928 * daemon, this means that every subsequent commands or data received on
1929 * the control socket has been handled. So, this is why we simply return
1932 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
1933 struct relay_connection
*conn
,
1934 const struct lttng_buffer_view
*payload
)
1938 struct relay_stream
*stream
;
1939 struct lttcomm_relayd_quiescent_control msg
;
1940 struct lttcomm_relayd_generic_reply reply
;
1942 DBG("Checking quiescent state on control socket");
1944 if (!conn
->session
|| !conn
->version_check_done
) {
1945 ERR("Trying to check for data before version check");
1947 goto end_no_session
;
1950 if (payload
->size
< sizeof(msg
)) {
1951 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
1952 sizeof(msg
), payload
->size
);
1954 goto end_no_session
;
1956 memcpy(&msg
, payload
->data
, sizeof(msg
));
1957 msg
.stream_id
= be64toh(msg
.stream_id
);
1959 stream
= stream_get_by_id(msg
.stream_id
);
1963 pthread_mutex_lock(&stream
->lock
);
1964 stream
->data_pending_check_done
= true;
1965 pthread_mutex_unlock(&stream
->lock
);
1967 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
1970 memset(&reply
, 0, sizeof(reply
));
1971 reply
.ret_code
= htobe32(LTTNG_OK
);
1972 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1973 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1974 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
1986 * Initialize a data pending command. This means that a consumer is about
1987 * to ask for data pending for each stream it holds. Simply iterate over
1988 * all streams of a session and set the data_pending_check_done flag.
1990 * This command returns to the client a LTTNG_OK code.
1992 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1993 struct relay_connection
*conn
,
1994 const struct lttng_buffer_view
*payload
)
1998 struct lttng_ht_iter iter
;
1999 struct lttcomm_relayd_begin_data_pending msg
;
2000 struct lttcomm_relayd_generic_reply reply
;
2001 struct relay_stream
*stream
;
2006 DBG("Init streams for data pending");
2008 if (!conn
->session
|| !conn
->version_check_done
) {
2009 ERR("Trying to check for data before version check");
2011 goto end_no_session
;
2014 if (payload
->size
< sizeof(msg
)) {
2015 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2016 sizeof(msg
), payload
->size
);
2018 goto end_no_session
;
2020 memcpy(&msg
, payload
->data
, sizeof(msg
));
2021 msg
.session_id
= be64toh(msg
.session_id
);
2024 * Iterate over all streams to set the begin data pending flag.
2025 * For now, the streams are indexed by stream handle so we have
2026 * to iterate over all streams to find the one associated with
2027 * the right session_id.
2030 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2032 if (!stream_get(stream
)) {
2035 if (stream
->trace
->session
->id
== msg
.session_id
) {
2036 pthread_mutex_lock(&stream
->lock
);
2037 stream
->data_pending_check_done
= false;
2038 pthread_mutex_unlock(&stream
->lock
);
2039 DBG("Set begin data pending flag to stream %" PRIu64
,
2040 stream
->stream_handle
);
2046 memset(&reply
, 0, sizeof(reply
));
2047 /* All good, send back reply. */
2048 reply
.ret_code
= htobe32(LTTNG_OK
);
2050 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2051 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2052 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2064 * End data pending command. This will check, for a given session id, if
2065 * each stream associated with it has its data_pending_check_done flag
2066 * set. If not, this means that the client lost track of the stream but
2067 * the data is still being streamed on our side. In this case, we inform
2068 * the client that data is in flight.
2070 * Return to the client if there is data in flight or not with a ret_code.
2072 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2073 struct relay_connection
*conn
,
2074 const struct lttng_buffer_view
*payload
)
2078 struct lttng_ht_iter iter
;
2079 struct lttcomm_relayd_end_data_pending msg
;
2080 struct lttcomm_relayd_generic_reply reply
;
2081 struct relay_stream
*stream
;
2082 uint32_t is_data_inflight
= 0;
2084 DBG("End data pending command");
2086 if (!conn
->session
|| !conn
->version_check_done
) {
2087 ERR("Trying to check for data before version check");
2089 goto end_no_session
;
2092 if (payload
->size
< sizeof(msg
)) {
2093 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2094 sizeof(msg
), payload
->size
);
2096 goto end_no_session
;
2098 memcpy(&msg
, payload
->data
, sizeof(msg
));
2099 msg
.session_id
= be64toh(msg
.session_id
);
2102 * Iterate over all streams to see if the begin data pending
2106 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2108 if (!stream_get(stream
)) {
2111 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2115 pthread_mutex_lock(&stream
->lock
);
2116 if (!stream
->data_pending_check_done
) {
2117 uint64_t stream_seq
;
2119 if (session_streams_have_index(conn
->session
)) {
2121 * Ensure that both the index and stream data have been
2122 * flushed up to the requested point.
2124 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2126 stream_seq
= stream
->prev_data_seq
;
2128 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2129 is_data_inflight
= 1;
2130 DBG("Data is still in flight for stream %" PRIu64
,
2131 stream
->stream_handle
);
2132 pthread_mutex_unlock(&stream
->lock
);
2137 pthread_mutex_unlock(&stream
->lock
);
2142 memset(&reply
, 0, sizeof(reply
));
2143 /* All good, send back reply. */
2144 reply
.ret_code
= htobe32(is_data_inflight
);
2146 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2147 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2148 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2160 * Receive an index for a specific stream.
2162 * Return 0 on success else a negative value.
2164 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2165 struct relay_connection
*conn
,
2166 const struct lttng_buffer_view
*payload
)
2170 struct relay_session
*session
= conn
->session
;
2171 struct lttcomm_relayd_index index_info
;
2172 struct lttcomm_relayd_generic_reply reply
;
2173 struct relay_stream
*stream
;
2178 DBG("Relay receiving index");
2180 if (!session
|| !conn
->version_check_done
) {
2181 ERR("Trying to close a stream before version check");
2183 goto end_no_session
;
2186 msg_len
= lttcomm_relayd_index_len(
2187 lttng_to_index_major(conn
->major
, conn
->minor
),
2188 lttng_to_index_minor(conn
->major
, conn
->minor
));
2189 if (payload
->size
< msg_len
) {
2190 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2191 msg_len
, payload
->size
);
2193 goto end_no_session
;
2195 memcpy(&index_info
, payload
->data
, msg_len
);
2196 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2197 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2198 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2199 index_info
.content_size
= be64toh(index_info
.content_size
);
2200 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2201 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2202 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2203 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2205 if (conn
->minor
>= 8) {
2206 index_info
.stream_instance_id
=
2207 be64toh(index_info
.stream_instance_id
);
2208 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2210 index_info
.stream_instance_id
= -1ULL;
2211 index_info
.packet_seq_num
= -1ULL;
2214 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2216 ERR("stream_get_by_id not found");
2221 pthread_mutex_lock(&stream
->lock
);
2222 ret
= stream_add_index(stream
, &index_info
);
2223 pthread_mutex_unlock(&stream
->lock
);
2225 goto end_stream_put
;
2231 memset(&reply
, 0, sizeof(reply
));
2233 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2235 reply
.ret_code
= htobe32(LTTNG_OK
);
2237 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2238 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2239 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2248 * Receive the streams_sent message.
2250 * Return 0 on success else a negative value.
2252 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2253 struct relay_connection
*conn
,
2254 const struct lttng_buffer_view
*payload
)
2258 struct lttcomm_relayd_generic_reply reply
;
2262 DBG("Relay receiving streams_sent");
2264 if (!conn
->session
|| !conn
->version_check_done
) {
2265 ERR("Trying to close a stream before version check");
2267 goto end_no_session
;
2271 * Publish every pending stream in the connection recv list which are
2272 * now ready to be used by the viewer.
2274 publish_connection_local_streams(conn
);
2276 memset(&reply
, 0, sizeof(reply
));
2277 reply
.ret_code
= htobe32(LTTNG_OK
);
2278 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2279 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2280 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2293 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2294 * session rotation feature (not the tracefile rotation feature).
2296 static int relay_rotate_session_streams(
2297 const struct lttcomm_relayd_hdr
*recv_hdr
,
2298 struct relay_connection
*conn
,
2299 const struct lttng_buffer_view
*payload
)
2304 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2305 struct relay_session
*session
= conn
->session
;
2306 struct lttcomm_relayd_rotate_streams rotate_streams
;
2307 struct lttcomm_relayd_generic_reply reply
= {};
2308 struct relay_stream
*stream
= NULL
;
2309 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2310 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2311 struct lttng_buffer_view stream_positions
;
2312 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2313 const char *chunk_id_str
= "none";
2315 if (!session
|| !conn
->version_check_done
) {
2316 ERR("Trying to rotate a stream before version check");
2321 if (session
->major
== 2 && session
->minor
< 11) {
2322 ERR("Unsupported feature before 2.11");
2327 if (payload
->size
< header_len
) {
2328 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2329 header_len
, payload
->size
);
2334 memcpy(&rotate_streams
, payload
->data
, header_len
);
2336 /* Convert header to host endianness. */
2337 rotate_streams
= (typeof(rotate_streams
)) {
2338 .stream_count
= be32toh(rotate_streams
.stream_count
),
2339 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2340 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2341 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2345 if (rotate_streams
.new_chunk_id
.is_set
) {
2347 * Retrieve the trace chunk the stream must transition to. As
2348 * per the protocol, this chunk should have been created
2349 * before this command is received.
2351 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2352 sessiond_trace_chunk_registry
,
2353 session
->sessiond_uuid
, session
->id
,
2354 rotate_streams
.new_chunk_id
.value
);
2355 if (!next_trace_chunk
) {
2356 char uuid_str
[LTTNG_UUID_STR_LEN
];
2358 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2359 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2360 ", trace_chunk_id = %" PRIu64
,
2361 uuid_str
, session
->id
,
2362 rotate_streams
.new_chunk_id
.value
);
2363 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2368 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2369 rotate_streams
.new_chunk_id
.value
);
2370 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2371 chunk_id_str
= "formatting error";
2373 chunk_id_str
= chunk_id_buf
;
2377 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2378 rotate_streams
.stream_count
, session
->session_name
,
2381 stream_positions
= lttng_buffer_view_from_view(payload
,
2382 sizeof(rotate_streams
), -1);
2383 if (!stream_positions
.data
||
2384 stream_positions
.size
<
2385 (rotate_streams
.stream_count
*
2386 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2387 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2392 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2393 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2394 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2395 const struct lttcomm_relayd_stream_rotation_position pos
= {
2396 .stream_id
= be64toh(position_comm
->stream_id
),
2397 .rotate_at_seq_num
= be64toh(
2398 position_comm
->rotate_at_seq_num
),
2401 stream
= stream_get_by_id(pos
.stream_id
);
2403 reply_code
= LTTNG_ERR_INVALID
;
2408 pthread_mutex_lock(&stream
->lock
);
2409 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2410 pos
.rotate_at_seq_num
);
2411 pthread_mutex_unlock(&stream
->lock
);
2413 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2421 reply_code
= LTTNG_OK
;
2428 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2429 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2430 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2431 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2432 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2437 lttng_trace_chunk_put(next_trace_chunk
);
2444 * relay_create_trace_chunk: create a new trace chunk
2446 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2447 struct relay_connection
*conn
,
2448 const struct lttng_buffer_view
*payload
)
2452 struct relay_session
*session
= conn
->session
;
2453 struct lttcomm_relayd_create_trace_chunk
*msg
;
2454 struct lttcomm_relayd_generic_reply reply
= {};
2455 struct lttng_buffer_view header_view
;
2456 struct lttng_buffer_view chunk_name_view
;
2457 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2458 enum lttng_error_code reply_code
= LTTNG_OK
;
2459 enum lttng_trace_chunk_status chunk_status
;
2460 struct lttng_directory_handle
*session_output
= NULL
;
2461 const char *new_path
;
2463 if (!session
|| !conn
->version_check_done
) {
2464 ERR("Trying to create a trace chunk before version check");
2469 if (session
->major
== 2 && session
->minor
< 11) {
2470 ERR("Chunk creation command is unsupported before 2.11");
2475 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2476 if (!header_view
.data
) {
2477 ERR("Failed to receive payload of chunk creation command");
2482 /* Convert to host endianness. */
2483 msg
= (typeof(msg
)) header_view
.data
;
2484 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2485 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2486 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2488 if (session
->current_trace_chunk
&&
2489 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2490 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2491 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
2492 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2493 ERR("Failed to rename old chunk");
2495 reply_code
= LTTNG_ERR_UNK
;
2499 session
->ongoing_rotation
= true;
2500 if (!session
->current_trace_chunk
) {
2501 if (!session
->has_rotated
) {
2507 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
2509 chunk
= lttng_trace_chunk_create(
2510 msg
->chunk_id
, msg
->creation_timestamp
, new_path
);
2512 ERR("Failed to create trace chunk in trace chunk creation command");
2514 reply_code
= LTTNG_ERR_NOMEM
;
2518 if (msg
->override_name_length
) {
2521 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2523 msg
->override_name_length
);
2524 name
= chunk_name_view
.data
;
2525 if (!name
|| name
[msg
->override_name_length
- 1]) {
2526 ERR("Failed to receive payload of chunk creation command");
2528 reply_code
= LTTNG_ERR_INVALID
;
2532 chunk_status
= lttng_trace_chunk_override_name(
2533 chunk
, chunk_name_view
.data
);
2534 switch (chunk_status
) {
2535 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2537 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2538 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2539 reply_code
= LTTNG_ERR_INVALID
;
2543 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2544 reply_code
= LTTNG_ERR_UNK
;
2550 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2551 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2552 reply_code
= LTTNG_ERR_UNK
;
2557 session_output
= session_create_output_directory_handle(
2559 if (!session_output
) {
2560 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2563 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, session_output
);
2564 lttng_directory_handle_put(session_output
);
2565 session_output
= NULL
;
2566 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2567 reply_code
= LTTNG_ERR_UNK
;
2572 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2573 sessiond_trace_chunk_registry
,
2574 conn
->session
->sessiond_uuid
,
2577 if (!published_chunk
) {
2578 char uuid_str
[LTTNG_UUID_STR_LEN
];
2580 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2581 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2586 reply_code
= LTTNG_ERR_NOMEM
;
2590 pthread_mutex_lock(&conn
->session
->lock
);
2591 if (conn
->session
->pending_closure_trace_chunk
) {
2593 * Invalid; this means a second create_trace_chunk command was
2594 * received before a close_trace_chunk.
2596 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2597 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2599 goto end_unlock_session
;
2601 conn
->session
->pending_closure_trace_chunk
=
2602 conn
->session
->current_trace_chunk
;
2603 conn
->session
->current_trace_chunk
= published_chunk
;
2604 published_chunk
= NULL
;
2605 if (!conn
->session
->pending_closure_trace_chunk
) {
2606 session
->ongoing_rotation
= false;
2609 pthread_mutex_unlock(&conn
->session
->lock
);
2611 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2612 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2614 sizeof(struct lttcomm_relayd_generic_reply
),
2616 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2617 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2622 lttng_trace_chunk_put(chunk
);
2623 lttng_trace_chunk_put(published_chunk
);
2624 lttng_directory_handle_put(session_output
);
2629 * relay_close_trace_chunk: close a trace chunk
2631 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2632 struct relay_connection
*conn
,
2633 const struct lttng_buffer_view
*payload
)
2635 int ret
= 0, buf_ret
;
2637 struct relay_session
*session
= conn
->session
;
2638 struct lttcomm_relayd_close_trace_chunk
*msg
;
2639 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2640 struct lttng_buffer_view header_view
;
2641 struct lttng_trace_chunk
*chunk
= NULL
;
2642 enum lttng_error_code reply_code
= LTTNG_OK
;
2643 enum lttng_trace_chunk_status chunk_status
;
2645 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2646 time_t close_timestamp
;
2647 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2648 size_t path_length
= 0;
2649 const char *chunk_name
= NULL
;
2650 struct lttng_dynamic_buffer reply_payload
;
2651 const char *new_path
;
2653 lttng_dynamic_buffer_init(&reply_payload
);
2655 if (!session
|| !conn
->version_check_done
) {
2656 ERR("Trying to close a trace chunk before version check");
2661 if (session
->major
== 2 && session
->minor
< 11) {
2662 ERR("Chunk close command is unsupported before 2.11");
2667 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2668 if (!header_view
.data
) {
2669 ERR("Failed to receive payload of chunk close command");
2674 /* Convert to host endianness. */
2675 msg
= (typeof(msg
)) header_view
.data
;
2676 chunk_id
= be64toh(msg
->chunk_id
);
2677 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2678 close_command
= (typeof(close_command
)){
2679 .value
= be32toh(msg
->close_command
.value
),
2680 .is_set
= msg
->close_command
.is_set
,
2683 chunk
= sessiond_trace_chunk_registry_get_chunk(
2684 sessiond_trace_chunk_registry
,
2685 conn
->session
->sessiond_uuid
,
2689 char uuid_str
[LTTNG_UUID_STR_LEN
];
2691 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2692 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2697 reply_code
= LTTNG_ERR_NOMEM
;
2701 pthread_mutex_lock(&session
->lock
);
2702 if (session
->pending_closure_trace_chunk
&&
2703 session
->pending_closure_trace_chunk
!= chunk
) {
2704 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2705 session
->session_name
);
2706 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2708 goto end_unlock_session
;
2711 if (session
->current_trace_chunk
&& session
->current_trace_chunk
!= chunk
&&
2712 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2713 if (close_command
.is_set
&&
2714 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&&
2715 !session
->has_rotated
) {
2716 /* New chunk stays in session output directory. */
2719 /* Use chunk name for new chunk. */
2722 /* Rename new chunk path. */
2723 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2725 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2729 session
->ongoing_rotation
= false;
2731 if ((!close_command
.is_set
||
2732 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) &&
2733 !lttng_trace_chunk_get_name_overridden(chunk
)) {
2734 const char *old_path
;
2736 if (!session
->has_rotated
) {
2741 /* We need to move back the .tmp_old_chunk to its rightful place. */
2742 chunk_status
= lttng_trace_chunk_rename_path(chunk
, old_path
);
2743 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2748 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2749 chunk
, close_timestamp
);
2750 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2751 ERR("Failed to set trace chunk close timestamp");
2753 reply_code
= LTTNG_ERR_UNK
;
2754 goto end_unlock_session
;
2757 if (close_command
.is_set
) {
2758 chunk_status
= lttng_trace_chunk_set_close_command(
2759 chunk
, close_command
.value
);
2760 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2762 reply_code
= LTTNG_ERR_INVALID
;
2763 goto end_unlock_session
;
2766 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2767 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2768 ERR("Failed to get chunk name");
2770 reply_code
= LTTNG_ERR_UNK
;
2771 goto end_unlock_session
;
2773 if (!session
->has_rotated
&& !session
->snapshot
) {
2774 ret
= lttng_strncpy(closed_trace_chunk_path
,
2775 session
->output_path
,
2776 sizeof(closed_trace_chunk_path
));
2778 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2779 strlen(session
->output_path
),
2780 sizeof(closed_trace_chunk_path
));
2781 reply_code
= LTTNG_ERR_NOMEM
;
2783 goto end_unlock_session
;
2786 if (session
->snapshot
) {
2787 ret
= snprintf(closed_trace_chunk_path
,
2788 sizeof(closed_trace_chunk_path
),
2789 "%s/%s", session
->output_path
,
2792 ret
= snprintf(closed_trace_chunk_path
,
2793 sizeof(closed_trace_chunk_path
),
2794 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2796 session
->output_path
, chunk_name
);
2798 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2799 ERR("Failed to format closed trace chunk resulting path");
2800 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2802 goto end_unlock_session
;
2805 if (close_command
.is_set
&&
2806 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
2807 session
->has_rotated
= true;
2809 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2810 path_length
= strlen(closed_trace_chunk_path
) + 1;
2811 if (path_length
> UINT32_MAX
) {
2812 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2814 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2815 goto end_unlock_session
;
2818 if (session
->current_trace_chunk
== chunk
) {
2820 * After a trace chunk close command, no new streams
2821 * referencing the chunk may be created. Hence, on the
2822 * event that no new trace chunk have been created for
2823 * the session, the reference to the current trace chunk
2824 * is released in order to allow it to be reclaimed when
2825 * the last stream releases its reference to it.
2827 lttng_trace_chunk_put(session
->current_trace_chunk
);
2828 session
->current_trace_chunk
= NULL
;
2830 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2831 session
->pending_closure_trace_chunk
= NULL
;
2833 pthread_mutex_unlock(&session
->lock
);
2836 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
2837 reply
.path_length
= htobe32((uint32_t) path_length
);
2838 buf_ret
= lttng_dynamic_buffer_append(
2839 &reply_payload
, &reply
, sizeof(reply
));
2841 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2845 if (reply_code
== LTTNG_OK
) {
2846 buf_ret
= lttng_dynamic_buffer_append(&reply_payload
,
2847 closed_trace_chunk_path
, path_length
);
2849 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2854 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2858 if (send_ret
< reply_payload
.size
) {
2859 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2860 reply_payload
.size
, send_ret
);
2865 lttng_trace_chunk_put(chunk
);
2866 lttng_dynamic_buffer_reset(&reply_payload
);
2871 * relay_trace_chunk_exists: check if a trace chunk exists
2873 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2874 struct relay_connection
*conn
,
2875 const struct lttng_buffer_view
*payload
)
2879 struct relay_session
*session
= conn
->session
;
2880 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2881 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2882 struct lttng_buffer_view header_view
;
2886 if (!session
|| !conn
->version_check_done
) {
2887 ERR("Trying to close a trace chunk before version check");
2892 if (session
->major
== 2 && session
->minor
< 11) {
2893 ERR("Chunk close command is unsupported before 2.11");
2898 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2899 if (!header_view
.data
) {
2900 ERR("Failed to receive payload of chunk close command");
2905 /* Convert to host endianness. */
2906 msg
= (typeof(msg
)) header_view
.data
;
2907 chunk_id
= be64toh(msg
->chunk_id
);
2909 ret
= sessiond_trace_chunk_registry_chunk_exists(
2910 sessiond_trace_chunk_registry
,
2911 conn
->session
->sessiond_uuid
,
2913 chunk_id
, &chunk_exists
);
2915 * If ret is not 0, send the reply and report the error to the caller.
2916 * It is a protocol (or internal) error and the session/connection
2917 * should be torn down.
2919 reply
= (typeof(reply
)){
2920 .generic
.ret_code
= htobe32((uint32_t)
2921 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
2922 .trace_chunk_exists
= ret
== 0 ? chunk_exists
: 0,
2924 send_ret
= conn
->sock
->ops
->sendmsg(
2925 conn
->sock
, &reply
, sizeof(reply
), 0);
2926 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2927 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2936 * relay_get_configuration: query whether feature is available
2938 static int relay_get_configuration(const struct lttcomm_relayd_hdr
*recv_hdr
,
2939 struct relay_connection
*conn
,
2940 const struct lttng_buffer_view
*payload
)
2944 struct lttcomm_relayd_get_configuration
*msg
;
2945 struct lttcomm_relayd_get_configuration_reply reply
= {};
2946 struct lttng_buffer_view header_view
;
2947 uint64_t query_flags
= 0;
2948 uint64_t result_flags
= 0;
2950 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2951 if (!header_view
.data
) {
2952 ERR("Failed to receive payload of chunk close command");
2957 /* Convert to host endianness. */
2958 msg
= (typeof(msg
)) header_view
.data
;
2959 query_flags
= be64toh(msg
->query_flags
);
2962 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
2965 if (opt_allow_clear
) {
2966 result_flags
|= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
;
2970 reply
= (typeof(reply
)){
2971 .generic
.ret_code
= htobe32((uint32_t)
2972 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
2973 .relayd_configuration_flags
= htobe64(result_flags
),
2975 send_ret
= conn
->sock
->ops
->sendmsg(
2976 conn
->sock
, &reply
, sizeof(reply
), 0);
2977 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2978 ERR("Failed to send \"get configuration\" command reply (ret = %zd)",
2986 #define DBG_CMD(cmd_name, conn) \
2987 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2989 static int relay_process_control_command(struct relay_connection
*conn
,
2990 const struct lttcomm_relayd_hdr
*header
,
2991 const struct lttng_buffer_view
*payload
)
2995 switch (header
->cmd
) {
2996 case RELAYD_CREATE_SESSION
:
2997 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2998 ret
= relay_create_session(header
, conn
, payload
);
3000 case RELAYD_ADD_STREAM
:
3001 DBG_CMD("RELAYD_ADD_STREAM", conn
);
3002 ret
= relay_add_stream(header
, conn
, payload
);
3004 case RELAYD_START_DATA
:
3005 DBG_CMD("RELAYD_START_DATA", conn
);
3006 ret
= relay_start(header
, conn
, payload
);
3008 case RELAYD_SEND_METADATA
:
3009 DBG_CMD("RELAYD_SEND_METADATA", conn
);
3010 ret
= relay_recv_metadata(header
, conn
, payload
);
3012 case RELAYD_VERSION
:
3013 DBG_CMD("RELAYD_VERSION", conn
);
3014 ret
= relay_send_version(header
, conn
, payload
);
3016 case RELAYD_CLOSE_STREAM
:
3017 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
3018 ret
= relay_close_stream(header
, conn
, payload
);
3020 case RELAYD_DATA_PENDING
:
3021 DBG_CMD("RELAYD_DATA_PENDING", conn
);
3022 ret
= relay_data_pending(header
, conn
, payload
);
3024 case RELAYD_QUIESCENT_CONTROL
:
3025 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
3026 ret
= relay_quiescent_control(header
, conn
, payload
);
3028 case RELAYD_BEGIN_DATA_PENDING
:
3029 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
3030 ret
= relay_begin_data_pending(header
, conn
, payload
);
3032 case RELAYD_END_DATA_PENDING
:
3033 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
3034 ret
= relay_end_data_pending(header
, conn
, payload
);
3036 case RELAYD_SEND_INDEX
:
3037 DBG_CMD("RELAYD_SEND_INDEX", conn
);
3038 ret
= relay_recv_index(header
, conn
, payload
);
3040 case RELAYD_STREAMS_SENT
:
3041 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
3042 ret
= relay_streams_sent(header
, conn
, payload
);
3044 case RELAYD_RESET_METADATA
:
3045 DBG_CMD("RELAYD_RESET_METADATA", conn
);
3046 ret
= relay_reset_metadata(header
, conn
, payload
);
3048 case RELAYD_ROTATE_STREAMS
:
3049 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
3050 ret
= relay_rotate_session_streams(header
, conn
, payload
);
3052 case RELAYD_CREATE_TRACE_CHUNK
:
3053 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
3054 ret
= relay_create_trace_chunk(header
, conn
, payload
);
3056 case RELAYD_CLOSE_TRACE_CHUNK
:
3057 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
3058 ret
= relay_close_trace_chunk(header
, conn
, payload
);
3060 case RELAYD_TRACE_CHUNK_EXISTS
:
3061 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
3062 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
3064 case RELAYD_GET_CONFIGURATION
:
3065 DBG_CMD("RELAYD_GET_CONFIGURATION", conn
);
3066 ret
= relay_get_configuration(header
, conn
, payload
);
3068 case RELAYD_UPDATE_SYNC_INFO
:
3070 ERR("Received unknown command (%u)", header
->cmd
);
3071 relay_unknown_command(conn
);
3080 static enum relay_connection_status
relay_process_control_receive_payload(
3081 struct relay_connection
*conn
)
3084 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3085 struct lttng_dynamic_buffer
*reception_buffer
=
3086 &conn
->protocol
.ctrl
.reception_buffer
;
3087 struct ctrl_connection_state_receive_payload
*state
=
3088 &conn
->protocol
.ctrl
.state
.receive_payload
;
3089 struct lttng_buffer_view payload_view
;
3091 if (state
->left_to_receive
== 0) {
3092 /* Short-circuit for payload-less commands. */
3093 goto reception_complete
;
3096 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3097 reception_buffer
->data
+ state
->received
,
3098 state
->left_to_receive
, MSG_DONTWAIT
);
3100 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3101 PERROR("Unable to receive command payload on sock %d",
3103 status
= RELAY_CONNECTION_STATUS_ERROR
;
3106 } else if (ret
== 0) {
3107 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3108 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3113 assert(ret
<= state
->left_to_receive
);
3115 state
->left_to_receive
-= ret
;
3116 state
->received
+= ret
;
3118 if (state
->left_to_receive
> 0) {
3120 * Can't transition to the protocol's next state, wait to
3121 * receive the rest of the header.
3123 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3124 state
->received
, state
->left_to_receive
,
3130 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3131 conn
->sock
->fd
, state
->received
);
3133 * The payload required to process the command has been received.
3134 * A view to the reception buffer is forwarded to the various
3135 * commands and the state of the control is reset on success.
3137 * Commands are responsible for sending their reply to the peer.
3139 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3141 ret
= relay_process_control_command(conn
,
3142 &state
->header
, &payload_view
);
3144 status
= RELAY_CONNECTION_STATUS_ERROR
;
3148 ret
= connection_reset_protocol_state(conn
);
3150 status
= RELAY_CONNECTION_STATUS_ERROR
;
3156 static enum relay_connection_status
relay_process_control_receive_header(
3157 struct relay_connection
*conn
)
3160 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3161 struct lttcomm_relayd_hdr header
;
3162 struct lttng_dynamic_buffer
*reception_buffer
=