2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/resource.h>
39 #include <urcu/futex.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/rculist.h>
47 #include <lttng/lttng.h>
48 #include <common/common.h>
49 #include <common/compat/poll.h>
50 #include <common/compat/socket.h>
51 #include <common/compat/endian.h>
52 #include <common/compat/getenv.h>
53 #include <common/defaults.h>
54 #include <common/daemonize.h>
55 #include <common/futex.h>
56 #include <common/sessiond-comm/sessiond-comm.h>
57 #include <common/sessiond-comm/inet.h>
58 #include <common/sessiond-comm/relayd.h>
59 #include <common/uri.h>
60 #include <common/utils.h>
61 #include <common/align.h>
62 #include <common/config/session-config.h>
63 #include <common/dynamic-buffer.h>
64 #include <common/buffer-view.h>
65 #include <common/string-utils/format.h>
66 #include <common/fd-tracker/fd-tracker.h>
67 #include <common/fd-tracker/utils.h>
69 #include "backward-compatibility-group-by.h"
71 #include "connection.h"
72 #include "ctf-trace.h"
73 #include "health-relayd.h"
76 #include "lttng-relayd.h"
78 #include "sessiond-trace-chunks.h"
80 #include "tcp_keep_alive.h"
81 #include "testpoint.h"
82 #include "tracefile-array.h"
85 #include "viewer-stream.h"
87 static const char *help_msg
=
88 #ifdef LTTNG_EMBED_HELP
89 #include <lttng-relayd.8.h>
95 enum relay_connection_status
{
96 RELAY_CONNECTION_STATUS_OK
,
97 /* An error occurred while processing an event on the connection. */
98 RELAY_CONNECTION_STATUS_ERROR
,
99 /* Connection closed/shutdown cleanly. */
100 RELAY_CONNECTION_STATUS_CLOSED
,
103 /* command line options */
104 char *opt_output_path
, *opt_working_directory
;
105 static int opt_daemon
, opt_background
, opt_print_version
, opt_allow_clear
= 1;
106 enum relay_group_output_by opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
;
109 * We need to wait for listener and live listener threads, as well as
110 * health check thread, before being ready to signal readiness.
112 #define NR_LTTNG_RELAY_READY 3
113 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
115 /* Size of receive buffer. */
116 #define RECV_DATA_BUFFER_SIZE 65536
118 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
119 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
121 static struct lttng_uri
*control_uri
;
122 static struct lttng_uri
*data_uri
;
123 static struct lttng_uri
*live_uri
;
125 const char *progname
;
127 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
128 static int tracing_group_name_override
;
130 const char * const config_section_name
= "relayd";
133 * Quit pipe for all threads. This permits a single cancellation point
134 * for all threads when receiving an event on the pipe.
136 int thread_quit_pipe
[2] = { -1, -1 };
139 * This pipe is used to inform the worker thread that a command is queued and
140 * ready to be processed.
142 static int relay_conn_pipe
[2] = { -1, -1 };
144 /* Shared between threads */
145 static int dispatch_thread_exit
;
147 static pthread_t listener_thread
;
148 static pthread_t dispatcher_thread
;
149 static pthread_t worker_thread
;
150 static pthread_t health_thread
;
153 * last_relay_stream_id_lock protects last_relay_stream_id increment
154 * atomicity on 32-bit architectures.
156 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
157 static uint64_t last_relay_stream_id
;
160 * Relay command queue.
162 * The relay_thread_listener and relay_thread_dispatcher communicate with this
165 static struct relay_conn_queue relay_conn_queue
;
167 /* Cap of file desriptors to be in simultaneous use by the relay daemon. */
168 static unsigned int lttng_opt_fd_cap
;
170 /* Global relay stream hash table. */
171 struct lttng_ht
*relay_streams_ht
;
173 /* Global relay viewer stream hash table. */
174 struct lttng_ht
*viewer_streams_ht
;
176 /* Global relay sessions hash table. */
177 struct lttng_ht
*sessions_ht
;
179 /* Relayd health monitoring */
180 struct health_app
*health_relayd
;
182 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
184 /* Global fd tracker. */
185 struct fd_tracker
*the_fd_tracker
;
187 static struct option long_options
[] = {
188 { "control-port", 1, 0, 'C', },
189 { "data-port", 1, 0, 'D', },
190 { "live-port", 1, 0, 'L', },
191 { "daemonize", 0, 0, 'd', },
192 { "background", 0, 0, 'b', },
193 { "group", 1, 0, 'g', },
194 { "fd-cap", 1, 0, '\0', },
195 { "help", 0, 0, 'h', },
196 { "output", 1, 0, 'o', },
197 { "verbose", 0, 0, 'v', },
198 { "config", 1, 0, 'f' },
199 { "version", 0, 0, 'V' },
200 { "working-directory", 1, 0, 'w', },
201 { "group-output-by-session", 0, 0, 's', },
202 { "group-output-by-host", 0, 0, 'p', },
203 { "disallow-clear", 0, 0, 'x' },
207 static const char *config_ignore_options
[] = { "help", "config", "version" };
209 static void print_version(void) {
210 fprintf(stdout
, "%s\n", VERSION
);
213 static void relayd_config_log(void)
215 DBG("LTTng-relayd " VERSION
" - " VERSION_NAME
"%s%s",
216 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
217 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
218 if (EXTRA_VERSION_DESCRIPTION
[0] != '\0') {
219 DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION
"\n");
221 if (EXTRA_VERSION_PATCHES
[0] != '\0') {
222 DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES
"\n");
227 * Take an option from the getopt output and set it in the right variable to be
230 * Return 0 on success else a negative value.
232 static int set_option(int opt
, const char *arg
, const char *optname
)
238 if (!strcmp(optname
, "fd-cap")) {
242 v
= strtoul(arg
, NULL
, 0);
243 if (errno
!= 0 || !isdigit(arg
[0])) {
244 ERR("Wrong value in --fd-cap parameter: %s",
249 if (v
< DEFAULT_RELAYD_MINIMAL_FD_CAP
) {
250 ERR("File descriptor cap must be set to at least %d",
251 DEFAULT_RELAYD_MINIMAL_FD_CAP
);
254 ERR("File descriptor cap overflow in --fd-cap parameter: %s",
259 lttng_opt_fd_cap
= (unsigned int) v
;
260 DBG3("File descriptor cap set to %u", lttng_opt_fd_cap
);
262 fprintf(stderr
, "unknown option %s", optname
);
264 fprintf(stderr
, " with arg %s\n", arg
);
269 if (lttng_is_setuid_setgid()) {
270 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
271 "-C, --control-port");
273 ret
= uri_parse(arg
, &control_uri
);
275 ERR("Invalid control URI specified");
278 if (control_uri
->port
== 0) {
279 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
284 if (lttng_is_setuid_setgid()) {
285 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
288 ret
= uri_parse(arg
, &data_uri
);
290 ERR("Invalid data URI specified");
293 if (data_uri
->port
== 0) {
294 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
299 if (lttng_is_setuid_setgid()) {
300 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
303 ret
= uri_parse(arg
, &live_uri
);
305 ERR("Invalid live URI specified");
308 if (live_uri
->port
== 0) {
309 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
320 if (lttng_is_setuid_setgid()) {
321 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
324 tracing_group_name
= strdup(arg
);
325 if (tracing_group_name
== NULL
) {
330 tracing_group_name_override
= 1;
334 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
336 ERR("Cannot show --help for `lttng-relayd`");
341 opt_print_version
= 1;
344 if (lttng_is_setuid_setgid()) {
345 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
348 ret
= asprintf(&opt_output_path
, "%s", arg
);
351 PERROR("asprintf opt_output_path");
357 if (lttng_is_setuid_setgid()) {
358 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
359 "-w, --working-directory");
361 ret
= asprintf(&opt_working_directory
, "%s", arg
);
364 PERROR("asprintf opt_working_directory");
371 /* Verbose level can increase using multiple -v */
373 lttng_opt_verbose
= config_parse_value(arg
);
375 /* Only 3 level of verbosity (-vvv). */
376 if (lttng_opt_verbose
< 3) {
377 lttng_opt_verbose
+= 1;
382 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
383 ERR("Cannot set --group-output-by-session, another --group-output-by argument is present");
386 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_SESSION
;
389 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
390 ERR("Cannot set --group-output-by-host, another --group-output-by argument is present");
393 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
400 /* Unknown option or other error.
401 * Error is printed by getopt, just return */
414 * config_entry_handler_cb used to handle options read from a config file.
415 * See config_entry_handler_cb comment in common/config/session-config.h for the
416 * return value conventions.
418 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
422 if (!entry
|| !entry
->name
|| !entry
->value
) {
427 /* Check if the option is to be ignored */
428 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
429 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
434 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
435 /* Ignore if entry name is not fully matched. */
436 if (strcmp(entry
->name
, long_options
[i
].name
)) {
441 * If the option takes no argument on the command line,
442 * we have to check if the value is "true". We support
443 * non-zero numeric values, true, on and yes.
445 if (!long_options
[i
].has_arg
) {
446 ret
= config_parse_value(entry
->value
);
449 WARN("Invalid configuration value \"%s\" for option %s",
450 entry
->value
, entry
->name
);
452 /* False, skip boolean config option. */
457 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
461 WARN("Unrecognized option \"%s\" in daemon configuration file.",
468 static int parse_env_options(void)
473 value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV
);
475 opt_working_directory
= strdup(value
);
476 if (!opt_working_directory
) {
477 ERR("Failed to allocate working directory string (\"%s\")",
485 static int set_options(int argc
, char **argv
)
487 int c
, ret
= 0, option_index
= 0, retval
= 0;
488 int orig_optopt
= optopt
, orig_optind
= optind
;
489 char *default_address
, *optstring
;
490 const char *config_path
= NULL
;
492 optstring
= utils_generate_optstring(long_options
,
493 sizeof(long_options
) / sizeof(struct option
));
499 /* Check for the --config option */
501 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
502 &option_index
)) != -1) {
506 } else if (c
!= 'f') {
510 if (lttng_is_setuid_setgid()) {
511 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
514 config_path
= utils_expand_path(optarg
);
516 ERR("Failed to resolve path: %s", optarg
);
521 ret
= config_get_section_entries(config_path
, config_section_name
,
522 config_entry_handler
, NULL
);
525 ERR("Invalid configuration option at line %i", ret
);
531 /* Reset getopt's global state */
532 optopt
= orig_optopt
;
533 optind
= orig_optind
;
535 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
540 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
547 /* assign default values */
548 if (control_uri
== NULL
) {
549 ret
= asprintf(&default_address
,
550 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
551 DEFAULT_NETWORK_CONTROL_PORT
);
553 PERROR("asprintf default data address");
558 ret
= uri_parse(default_address
, &control_uri
);
559 free(default_address
);
561 ERR("Invalid control URI specified");
566 if (data_uri
== NULL
) {
567 ret
= asprintf(&default_address
,
568 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
569 DEFAULT_NETWORK_DATA_PORT
);
571 PERROR("asprintf default data address");
576 ret
= uri_parse(default_address
, &data_uri
);
577 free(default_address
);
579 ERR("Invalid data URI specified");
584 if (live_uri
== NULL
) {
585 ret
= asprintf(&default_address
,
586 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
587 DEFAULT_NETWORK_VIEWER_PORT
);
589 PERROR("asprintf default viewer control address");
594 ret
= uri_parse(default_address
, &live_uri
);
595 free(default_address
);
597 ERR("Invalid viewer control URI specified");
602 if (lttng_opt_fd_cap
== 0) {
604 struct rlimit rlimit
;
606 ret
= getrlimit(RLIMIT_NOFILE
, &rlimit
);
608 PERROR("Failed to get file descriptor limit");
612 lttng_opt_fd_cap
= rlimit
.rlim_cur
;
615 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
616 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
618 if (opt_allow_clear
) {
619 /* Check if env variable exists. */
620 const char *value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
622 ret
= config_parse_value(value
);
624 ERR("Invalid value for %s specified", DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
628 opt_allow_clear
= !ret
;
637 static void print_global_objects(void)
639 print_viewer_streams();
640 print_relay_streams();
647 static void relayd_cleanup(void)
649 print_global_objects();
653 if (viewer_streams_ht
)
654 lttng_ht_destroy(viewer_streams_ht
);
655 if (relay_streams_ht
)
656 lttng_ht_destroy(relay_streams_ht
);
658 lttng_ht_destroy(sessions_ht
);
660 free(opt_output_path
);
661 free(opt_working_directory
);
664 health_app_destroy(health_relayd
);
666 /* Close thread quit pipes */
667 if (health_quit_pipe
[0] != -1) {
668 (void) fd_tracker_util_pipe_close(
669 the_fd_tracker
, health_quit_pipe
);
671 if (thread_quit_pipe
[0] != -1) {
672 (void) fd_tracker_util_pipe_close(
673 the_fd_tracker
, thread_quit_pipe
);
675 if (sessiond_trace_chunk_registry
) {
676 sessiond_trace_chunk_registry_destroy(
677 sessiond_trace_chunk_registry
);
679 if (the_fd_tracker
) {
682 * fd_tracker_destroy() will log the contents of the fd-tracker
683 * if a leak is detected.
685 fd_tracker_destroy(the_fd_tracker
);
688 uri_free(control_uri
);
690 /* Live URI is freed in the live thread. */
692 if (tracing_group_name_override
) {
693 free((void *) tracing_group_name
);
698 * Write to writable pipe used to notify a thread.
700 static int notify_thread_pipe(int wpipe
)
704 ret
= lttng_write(wpipe
, "!", 1);
706 PERROR("write poll pipe");
714 static int notify_health_quit_pipe(int *pipe
)
718 ret
= lttng_write(pipe
[1], "4", 1);
720 PERROR("write relay health quit");
729 * Stop all relayd and relayd-live threads.
731 int lttng_relay_stop_threads(void)
735 /* Stopping all threads */
736 DBG("Terminating all threads");
737 if (notify_thread_pipe(thread_quit_pipe
[1])) {
738 ERR("write error on thread quit pipe");
742 if (notify_health_quit_pipe(health_quit_pipe
)) {
743 ERR("write error on health quit pipe");
746 /* Dispatch thread */
747 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
748 futex_nto1_wake(&relay_conn_queue
.futex
);
750 if (relayd_live_stop()) {
751 ERR("Error stopping live threads");
758 * Signal handler for the daemon
760 * Simply stop all worker threads, leaving main() return gracefully after
761 * joining all threads and calling cleanup().
763 static void sighandler(int sig
)
767 DBG("SIGINT caught");
768 if (lttng_relay_stop_threads()) {
769 ERR("Error stopping threads");
773 DBG("SIGTERM caught");
774 if (lttng_relay_stop_threads()) {
775 ERR("Error stopping threads");
779 CMM_STORE_SHARED(recv_child_signal
, 1);
787 * Setup signal handler for :
788 * SIGINT, SIGTERM, SIGPIPE
790 static int set_signal_handler(void)
796 if ((ret
= sigemptyset(&sigset
)) < 0) {
797 PERROR("sigemptyset");
804 sa
.sa_handler
= sighandler
;
805 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
810 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
815 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
820 sa
.sa_handler
= SIG_IGN
;
821 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
826 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
831 void lttng_relay_notify_ready(void)
833 /* Notify the parent of the fork() process that we are ready. */
834 if (opt_daemon
|| opt_background
) {
835 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
836 kill(child_ppid
, SIGUSR1
);
842 * Init thread quit pipe.
844 * Return -1 on error or 0 if all pipes are created.
846 static int init_thread_quit_pipe(void)
848 return fd_tracker_util_pipe_open_cloexec(
849 the_fd_tracker
, "Quit pipe", thread_quit_pipe
);
853 * Init health quit pipe.
855 * Return -1 on error or 0 if all pipes are created.
857 static int init_health_quit_pipe(void)
859 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
860 "Health quit pipe", health_quit_pipe
);
864 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
866 static int create_named_thread_poll_set(struct lttng_poll_event
*events
,
867 int size
, const char *name
)
871 if (events
== NULL
|| size
== 0) {
876 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
877 name
, events
, 1, LTTNG_CLOEXEC
);
880 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
892 * Check if the thread quit pipe was triggered.
894 * Return 1 if it was triggered else 0;
896 static int check_thread_quit_pipe(int fd
, uint32_t events
)
898 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
905 static int create_sock(void *data
, int *out_fd
)
908 struct lttcomm_sock
*sock
= data
;
910 ret
= lttcomm_create_sock(sock
);
920 static int close_sock(void *data
, int *in_fd
)
922 struct lttcomm_sock
*sock
= data
;
924 return sock
->ops
->close(sock
);
928 * Create and init socket from uri.
930 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
,
934 struct lttcomm_sock
*sock
= NULL
;
935 char uri_str
[PATH_MAX
];
936 char *formated_name
= NULL
;
938 sock
= lttcomm_alloc_sock_from_uri(uri
);
940 ERR("Allocating socket");
945 * Don't fail to create the socket if the name can't be built as it is
946 * only used for debugging purposes.
948 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
949 uri_str
[sizeof(uri_str
) - 1] = '\0';
951 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
954 formated_name
= NULL
;
958 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
959 (const char **) (formated_name
? &formated_name
: NULL
),
960 1, create_sock
, sock
);
962 DBG("Listening on %s socket %d", name
, sock
->fd
);
964 ret
= sock
->ops
->bind(sock
);
966 PERROR("Failed to bind socket");
970 ret
= sock
->ops
->listen(sock
, -1);
980 lttcomm_destroy_sock(sock
);
986 * This thread manages the listening for new connections on the network
988 static void *relay_thread_listener(void *data
)
990 int i
, ret
, pollfd
, err
= -1;
991 uint32_t revents
, nb_fd
;
992 struct lttng_poll_event events
;
993 struct lttcomm_sock
*control_sock
, *data_sock
;
995 DBG("[thread] Relay listener started");
997 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
999 health_code_update();
1001 control_sock
= relay_socket_create(control_uri
, "Control listener");
1002 if (!control_sock
) {
1003 goto error_sock_control
;
1006 data_sock
= relay_socket_create(data_uri
, "Data listener");
1008 goto error_sock_relay
;
1012 * Pass 3 as size here for the thread quit pipe, control and
1015 ret
= create_named_thread_poll_set(&events
, 3, "Listener thread epoll");
1017 goto error_create_poll
;
1020 /* Add the control socket */
1021 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
1023 goto error_poll_add
;
1026 /* Add the data socket */
1027 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
1029 goto error_poll_add
;
1032 lttng_relay_notify_ready();
1034 if (testpoint(relayd_thread_listener
)) {
1035 goto error_testpoint
;
1039 health_code_update();
1041 DBG("Listener accepting connections");
1044 health_poll_entry();
1045 ret
= lttng_poll_wait(&events
, -1);
1049 * Restart interrupted system call.
1051 if (errno
== EINTR
) {
1059 DBG("Relay new connection received");
1060 for (i
= 0; i
< nb_fd
; i
++) {
1061 health_code_update();
1063 /* Fetch once the poll data */
1064 revents
= LTTNG_POLL_GETEV(&events
, i
);
1065 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1067 /* Thread quit pipe has been closed. Killing thread. */
1068 ret
= check_thread_quit_pipe(pollfd
, revents
);
1074 if (revents
& LPOLLIN
) {
1076 * A new connection is requested, therefore a
1077 * sessiond/consumerd connection is allocated in
1078 * this thread, enqueued to a global queue and
1079 * dequeued (and freed) in the worker thread.
1082 struct relay_connection
*new_conn
;
1083 struct lttcomm_sock
*newsock
;
1084 enum connection_type type
;
1086 if (pollfd
== data_sock
->fd
) {
1088 newsock
= data_sock
->ops
->accept(data_sock
);
1089 DBG("Relay data connection accepted, socket %d",
1092 assert(pollfd
== control_sock
->fd
);
1093 type
= RELAY_CONTROL
;
1094 newsock
= control_sock
->ops
->accept(control_sock
);
1095 DBG("Relay control connection accepted, socket %d",
1099 PERROR("accepting sock");
1103 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
1106 PERROR("setsockopt inet");
1107 lttcomm_destroy_sock(newsock
);
1111 ret
= socket_apply_keep_alive_config(newsock
->fd
);
1113 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1115 lttcomm_destroy_sock(newsock
);
1119 new_conn
= connection_create(newsock
, type
);
1121 lttcomm_destroy_sock(newsock
);
1125 /* Enqueue request for the dispatcher thread. */
1126 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
1130 * Wake the dispatch queue futex.
1131 * Implicit memory barrier with the
1132 * exchange in cds_wfcq_enqueue.
1134 futex_nto1_wake(&relay_conn_queue
.futex
);
1135 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1136 ERR("socket poll error");
1139 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1149 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
1151 if (data_sock
->fd
>= 0) {
1152 int data_sock_fd
= data_sock
->fd
;
1154 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
1155 &data_sock_fd
, 1, close_sock
,
1158 PERROR("Failed to close the data listener socket file descriptor");
1162 lttcomm_destroy_sock(data_sock
);
1164 if (control_sock
->fd
>= 0) {
1165 int control_sock_fd
= control_sock
->fd
;
1167 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
1168 &control_sock_fd
, 1, close_sock
,
1171 PERROR("Failed to close the control listener socket file descriptor");
1173 control_sock
->fd
= -1;
1175 lttcomm_destroy_sock(control_sock
);
1179 ERR("Health error occurred in %s", __func__
);
1181 health_unregister(health_relayd
);
1182 DBG("Relay listener thread cleanup complete");
1183 lttng_relay_stop_threads();
1188 * This thread manages the dispatching of the requests to worker threads
1190 static void *relay_thread_dispatcher(void *data
)
1194 struct cds_wfcq_node
*node
;
1195 struct relay_connection
*new_conn
= NULL
;
1197 DBG("[thread] Relay dispatcher started");
1199 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1201 if (testpoint(relayd_thread_dispatcher
)) {
1202 goto error_testpoint
;
1205 health_code_update();
1208 health_code_update();
1210 /* Atomically prepare the queue futex */
1211 futex_nto1_prepare(&relay_conn_queue
.futex
);
1213 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1218 health_code_update();
1220 /* Dequeue commands */
1221 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1222 &relay_conn_queue
.tail
);
1224 DBG("Woken up but nothing in the relay command queue");
1225 /* Continue thread execution */
1228 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1230 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1233 * Inform worker thread of the new request. This
1234 * call is blocking so we can be assured that
1235 * the data will be read at some point in time
1236 * or wait to the end of the world :)
1238 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1240 PERROR("write connection pipe");
1241 connection_put(new_conn
);
1244 } while (node
!= NULL
);
1246 /* Futex wait on queue. Blocking call on futex() */
1247 health_poll_entry();
1248 futex_nto1_wait(&relay_conn_queue
.futex
);
1252 /* Normal exit, no error */
1259 ERR("Health error occurred in %s", __func__
);
1261 health_unregister(health_relayd
);
1262 DBG("Dispatch thread dying");
1263 lttng_relay_stop_threads();
1267 static bool session_streams_have_index(const struct relay_session
*session
)
1269 return session
->minor
>= 4 && !session
->snapshot
;
1273 * Handle the RELAYD_CREATE_SESSION command.
1275 * On success, send back the session id or else return a negative value.
1277 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1278 struct relay_connection
*conn
,
1279 const struct lttng_buffer_view
*payload
)
1283 struct relay_session
*session
= NULL
;
1284 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1285 char session_name
[LTTNG_NAME_MAX
] = {};
1286 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1287 uint32_t live_timer
= 0;
1288 bool snapshot
= false;
1289 bool session_name_contains_creation_timestamp
= false;
1290 /* Left nil for peers < 2.11. */
1291 char base_path
[LTTNG_PATH_MAX
] = {};
1292 lttng_uuid sessiond_uuid
= {};
1293 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1294 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1295 LTTNG_OPTIONAL(time_t) creation_time
= {};
1296 struct lttng_dynamic_buffer reply_payload
;
1298 lttng_dynamic_buffer_init(&reply_payload
);
1300 if (conn
->minor
< 4) {
1301 /* From 2.1 to 2.3 */
1303 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1304 /* From 2.4 to 2.10 */
1305 ret
= cmd_create_session_2_4(payload
, session_name
,
1306 hostname
, &live_timer
, &snapshot
);
1308 bool has_current_chunk
;
1309 uint64_t current_chunk_id_value
;
1310 time_t creation_time_value
;
1311 uint64_t id_sessiond_value
;
1313 /* From 2.11 to ... */
1314 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1315 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1316 sessiond_uuid
, &has_current_chunk
,
1317 ¤t_chunk_id_value
, &creation_time_value
,
1318 &session_name_contains_creation_timestamp
);
1319 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1320 /* The nil UUID is reserved for pre-2.11 clients. */
1321 ERR("Illegal nil UUID announced by peer in create session command");
1325 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1326 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1327 if (has_current_chunk
) {
1328 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1329 current_chunk_id_value
);
1337 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1338 snapshot
, sessiond_uuid
,
1339 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1340 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1341 creation_time
.is_set
? &creation_time
.value
: NULL
,
1342 conn
->major
, conn
->minor
,
1343 session_name_contains_creation_timestamp
);
1348 assert(!conn
->session
);
1349 conn
->session
= session
;
1350 DBG("Created session %" PRIu64
, session
->id
);
1352 reply
.generic
.session_id
= htobe64(session
->id
);
1356 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1358 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1361 if (conn
->minor
< 11) {
1362 /* From 2.1 to 2.10 */
1363 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1364 &reply
.generic
, sizeof(reply
.generic
));
1366 ERR("Failed to append \"create session\" command reply header to payload buffer");
1371 const uint32_t output_path_length
=
1372 session
? strlen(session
->output_path
) + 1 : 0;
1374 reply
.output_path_length
= htobe32(output_path_length
);
1375 ret
= lttng_dynamic_buffer_append(
1376 &reply_payload
, &reply
, sizeof(reply
));
1378 ERR("Failed to append \"create session\" command reply header to payload buffer");
1382 if (output_path_length
) {
1383 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1384 session
->output_path
,
1385 output_path_length
);
1387 ERR("Failed to append \"create session\" command reply path to payload buffer");
1393 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1394 reply_payload
.size
, 0);
1395 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1396 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1397 reply_payload
.size
, send_ret
);
1401 if (ret
< 0 && session
) {
1402 session_put(session
);
1404 lttng_dynamic_buffer_reset(&reply_payload
);
1409 * When we have received all the streams and the metadata for a channel,
1410 * we make them visible to the viewer threads.
1412 static void publish_connection_local_streams(struct relay_connection
*conn
)
1414 struct relay_stream
*stream
;
1415 struct relay_session
*session
= conn
->session
;
1418 * We publish all streams belonging to a session atomically wrt
1421 pthread_mutex_lock(&session
->lock
);
1423 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1425 stream_publish(stream
);
1430 * Inform the viewer that there are new streams in the session.
1432 if (session
->viewer_attached
) {
1433 uatomic_set(&session
->new_streams
, 1);
1435 pthread_mutex_unlock(&session
->lock
);
1438 static int conform_channel_path(char *channel_path
)
1442 if (strstr("../", channel_path
)) {
1443 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1449 if (*channel_path
== '/') {
1450 const size_t len
= strlen(channel_path
);
1453 * Channel paths from peers prior to 2.11 are expressed as an
1454 * absolute path that is, in reality, relative to the relay
1455 * daemon's output directory. Remove the leading slash so it
1456 * is correctly interpreted as a relative path later on.
1458 * len (and not len - 1) is used to copy the trailing NULL.
1460 bcopy(channel_path
+ 1, channel_path
, len
);
1467 * relay_add_stream: allocate a new stream for a session
1469 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1470 struct relay_connection
*conn
,
1471 const struct lttng_buffer_view
*payload
)
1475 struct relay_session
*session
= conn
->session
;
1476 struct relay_stream
*stream
= NULL
;
1477 struct lttcomm_relayd_status_stream reply
;
1478 struct ctf_trace
*trace
= NULL
;
1479 uint64_t stream_handle
= -1ULL;
1480 char *path_name
= NULL
, *channel_name
= NULL
;
1481 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1482 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1484 if (!session
|| !conn
->version_check_done
) {
1485 ERR("Trying to add a stream before version check");
1487 goto end_no_session
;
1490 if (session
->minor
== 1) {
1492 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1494 } else if (session
->minor
> 1 && session
->minor
< 11) {
1495 /* From 2.2 to 2.10 */
1496 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1497 &channel_name
, &tracefile_size
, &tracefile_count
);
1499 /* From 2.11 to ... */
1500 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1501 &channel_name
, &tracefile_size
, &tracefile_count
,
1502 &stream_chunk_id
.value
);
1503 stream_chunk_id
.is_set
= true;
1510 if (conform_channel_path(path_name
)) {
1515 * Backward compatibility for --group-output-by-session.
1516 * Prior to lttng 2.11, the complete path is passed by the stream.
1517 * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
1518 * >=2.11 the chunk is responsible for the output path. When dealing
1519 * with producer < 2.11 the chunk output_path is the root output path
1520 * and the stream carries the complete path (path_name).
1521 * To support --group-output-by-session with older producer (<2.11), we
1522 * need to craft the path based on the stream path.
1524 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_SESSION
) {
1525 if (conn
->minor
< 4) {
1527 * From 2.1 to 2.3, the session_name is not passed on
1528 * the RELAYD_CREATE_SESSION command. The session name
1529 * is necessary to detect the presence of a base_path
1530 * inside the stream path. Without it we cannot perform
1531 * a valid group-output-by-session transformation.
1533 WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
1534 " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
1535 session
->id
, path_name
);
1536 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1537 char *group_by_session_path_name
;
1539 assert(session
->session_name
[0] != '\0');
1541 group_by_session_path_name
=
1542 backward_compat_group_by_session(
1544 session
->session_name
);
1545 if (!group_by_session_path_name
) {
1546 ERR("Failed to apply group by session to stream of session %" PRIu64
,
1551 DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
1552 path_name
, group_by_session_path_name
);
1555 path_name
= group_by_session_path_name
;
1559 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1564 /* This stream here has one reference on the trace. */
1565 pthread_mutex_lock(&last_relay_stream_id_lock
);
1566 stream_handle
= ++last_relay_stream_id
;
1567 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1569 /* We pass ownership of path_name and channel_name. */
1570 stream
= stream_create(trace
, stream_handle
, path_name
,
1571 channel_name
, tracefile_size
, tracefile_count
);
1573 channel_name
= NULL
;
1576 * Streams are the owners of their trace. Reference to trace is
1577 * kept within stream_create().
1579 ctf_trace_put(trace
);
1582 memset(&reply
, 0, sizeof(reply
));
1583 reply
.handle
= htobe64(stream_handle
);
1585 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1587 reply
.ret_code
= htobe32(LTTNG_OK
);
1590 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1591 sizeof(struct lttcomm_relayd_status_stream
), 0);
1592 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1593 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1605 * relay_close_stream: close a specific stream
1607 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1608 struct relay_connection
*conn
,
1609 const struct lttng_buffer_view
*payload
)
1613 struct relay_session
*session
= conn
->session
;
1614 struct lttcomm_relayd_close_stream stream_info
;
1615 struct lttcomm_relayd_generic_reply reply
;
1616 struct relay_stream
*stream
;
1618 DBG("Close stream received");
1620 if (!session
|| !conn
->version_check_done
) {
1621 ERR("Trying to close a stream before version check");
1623 goto end_no_session
;
1626 if (payload
->size
< sizeof(stream_info
)) {
1627 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1628 sizeof(stream_info
), payload
->size
);
1630 goto end_no_session
;
1632 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1633 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1634 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1636 stream
= stream_get_by_id(stream_info
.stream_id
);
1643 * Set last_net_seq_num before the close flag. Required by data
1646 pthread_mutex_lock(&stream
->lock
);
1647 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1648 pthread_mutex_unlock(&stream
->lock
);
1651 * This is one of the conditions which may trigger a stream close
1652 * with the others being:
1653 * 1) A close command is received for a stream
1654 * 2) The control connection owning the stream is closed
1655 * 3) We have received all of the stream's data _after_ a close
1658 try_stream_close(stream
);
1659 if (stream
->is_metadata
) {
1660 struct relay_viewer_stream
*vstream
;
1662 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1664 if (stream
->no_new_metadata_notified
) {
1666 * Since all the metadata has been sent to the
1667 * viewer and that we have a request to close
1668 * its stream, we can safely teardown the
1669 * corresponding metadata viewer stream.
1671 viewer_stream_put(vstream
);
1673 /* Put local reference. */
1674 viewer_stream_put(vstream
);
1681 memset(&reply
, 0, sizeof(reply
));
1683 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1685 reply
.ret_code
= htobe32(LTTNG_OK
);
1687 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1688 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1689 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1690 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1700 * relay_reset_metadata: reset a metadata stream
1703 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1704 struct relay_connection
*conn
,
1705 const struct lttng_buffer_view
*payload
)
1709 struct relay_session
*session
= conn
->session
;
1710 struct lttcomm_relayd_reset_metadata stream_info
;
1711 struct lttcomm_relayd_generic_reply reply
;
1712 struct relay_stream
*stream
;
1714 DBG("Reset metadata received");
1716 if (!session
|| !conn
->version_check_done
) {
1717 ERR("Trying to reset a metadata stream before version check");
1719 goto end_no_session
;
1722 if (payload
->size
< sizeof(stream_info
)) {
1723 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1724 sizeof(stream_info
), payload
->size
);
1726 goto end_no_session
;
1728 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1729 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1730 stream_info
.version
= be64toh(stream_info
.version
);
1732 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1734 /* Unsupported for live sessions for now. */
1735 if (session
->live_timer
!= 0) {
1740 stream
= stream_get_by_id(stream_info
.stream_id
);
1745 pthread_mutex_lock(&stream
->lock
);
1746 if (!stream
->is_metadata
) {
1751 ret
= stream_reset_file(stream
);
1753 ERR("Failed to reset metadata stream %" PRIu64
1754 ": stream_path = %s, channel = %s",
1755 stream
->stream_handle
, stream
->path_name
,
1756 stream
->channel_name
);
1760 pthread_mutex_unlock(&stream
->lock
);
1764 memset(&reply
, 0, sizeof(reply
));
1766 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1768 reply
.ret_code
= htobe32(LTTNG_OK
);
1770 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1771 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1772 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1773 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1783 * relay_unknown_command: send -1 if received unknown command
1785 static void relay_unknown_command(struct relay_connection
*conn
)
1787 struct lttcomm_relayd_generic_reply reply
;
1790 memset(&reply
, 0, sizeof(reply
));
1791 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1792 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1793 if (send_ret
< sizeof(reply
)) {
1794 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1799 * relay_start: send an acknowledgment to the client to tell if we are
1800 * ready to receive data. We are ready if a session is established.
1802 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1803 struct relay_connection
*conn
,
1804 const struct lttng_buffer_view
*payload
)
1808 struct lttcomm_relayd_generic_reply reply
;
1809 struct relay_session
*session
= conn
->session
;
1812 DBG("Trying to start the streaming without a session established");
1813 ret
= htobe32(LTTNG_ERR_UNK
);
1816 memset(&reply
, 0, sizeof(reply
));
1817 reply
.ret_code
= htobe32(LTTNG_OK
);
1818 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1820 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1821 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1830 * relay_recv_metadata: receive the metadata for the session.
1832 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1833 struct relay_connection
*conn
,
1834 const struct lttng_buffer_view
*payload
)
1837 struct relay_session
*session
= conn
->session
;
1838 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1839 struct relay_stream
*metadata_stream
;
1840 uint64_t metadata_payload_size
;
1841 struct lttng_buffer_view packet_view
;
1844 ERR("Metadata sent before version check");
1849 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1850 ERR("Incorrect data size");
1854 metadata_payload_size
= recv_hdr
->data_size
-
1855 sizeof(struct lttcomm_relayd_metadata_payload
);
1857 memcpy(&metadata_payload_header
, payload
->data
,
1858 sizeof(metadata_payload_header
));
1859 metadata_payload_header
.stream_id
= be64toh(
1860 metadata_payload_header
.stream_id
);
1861 metadata_payload_header
.padding_size
= be32toh(
1862 metadata_payload_header
.padding_size
);
1864 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1865 if (!metadata_stream
) {
1870 packet_view
= lttng_buffer_view_from_view(payload
,
1871 sizeof(metadata_payload_header
), metadata_payload_size
);
1872 if (!packet_view
.data
) {
1873 ERR("Invalid metadata packet length announced by header");
1878 pthread_mutex_lock(&metadata_stream
->lock
);
1879 ret
= stream_write(metadata_stream
, &packet_view
,
1880 metadata_payload_header
.padding_size
);
1881 pthread_mutex_unlock(&metadata_stream
->lock
);
1887 stream_put(metadata_stream
);
1893 * relay_send_version: send relayd version number
1895 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1896 struct relay_connection
*conn
,
1897 const struct lttng_buffer_view
*payload
)
1901 struct lttcomm_relayd_version reply
, msg
;
1902 bool compatible
= true;
1904 conn
->version_check_done
= true;
1906 /* Get version from the other side. */
1907 if (payload
->size
< sizeof(msg
)) {
1908 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1909 sizeof(msg
), payload
->size
);
1914 memcpy(&msg
, payload
->data
, sizeof(msg
));
1915 msg
.major
= be32toh(msg
.major
);
1916 msg
.minor
= be32toh(msg
.minor
);
1918 memset(&reply
, 0, sizeof(reply
));
1919 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1920 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1922 /* Major versions must be the same */
1923 if (reply
.major
!= msg
.major
) {
1924 DBG("Incompatible major versions (%u vs %u), deleting session",
1925 reply
.major
, msg
.major
);
1929 conn
->major
= reply
.major
;
1930 /* We adapt to the lowest compatible version */
1931 if (reply
.minor
<= msg
.minor
) {
1932 conn
->minor
= reply
.minor
;
1934 conn
->minor
= msg
.minor
;
1937 reply
.major
= htobe32(reply
.major
);
1938 reply
.minor
= htobe32(reply
.minor
);
1939 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1941 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1942 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1955 DBG("Version check done using protocol %u.%u", conn
->major
,
1963 * Check for data pending for a given stream id from the session daemon.
1965 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1966 struct relay_connection
*conn
,
1967 const struct lttng_buffer_view
*payload
)
1969 struct relay_session
*session
= conn
->session
;
1970 struct lttcomm_relayd_data_pending msg
;
1971 struct lttcomm_relayd_generic_reply reply
;
1972 struct relay_stream
*stream
;
1975 uint64_t stream_seq
;
1977 DBG("Data pending command received");
1979 if (!session
|| !conn
->version_check_done
) {
1980 ERR("Trying to check for data before version check");
1982 goto end_no_session
;
1985 if (payload
->size
< sizeof(msg
)) {
1986 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1987 sizeof(msg
), payload
->size
);
1989 goto end_no_session
;
1991 memcpy(&msg
, payload
->data
, sizeof(msg
));
1992 msg
.stream_id
= be64toh(msg
.stream_id
);
1993 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1995 stream
= stream_get_by_id(msg
.stream_id
);
1996 if (stream
== NULL
) {
2001 pthread_mutex_lock(&stream
->lock
);
2003 if (session_streams_have_index(session
)) {
2005 * Ensure that both the index and stream data have been
2006 * flushed up to the requested point.
2008 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2010 stream_seq
= stream
->prev_data_seq
;
2012 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
2013 ", prev_index_seq %" PRIu64
2014 ", and last_seq %" PRIu64
, msg
.stream_id
,
2015 stream
->prev_data_seq
, stream
->prev_index_seq
,
2016 msg
.last_net_seq_num
);
2018 /* Avoid wrapping issue */
2019 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
2020 /* Data has in fact been written and is NOT pending */
2023 /* Data still being streamed thus pending */
2027 stream
->data_pending_check_done
= true;
2028 pthread_mutex_unlock(&stream
->lock
);
2033 memset(&reply
, 0, sizeof(reply
));
2034 reply
.ret_code
= htobe32(ret
);
2035 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2036 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2037 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
2047 * Wait for the control socket to reach a quiescent state.
2049 * Note that for now, when receiving this command from the session
2050 * daemon, this means that every subsequent commands or data received on
2051 * the control socket has been handled. So, this is why we simply return
2054 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2055 struct relay_connection
*conn
,
2056 const struct lttng_buffer_view
*payload
)
2060 struct relay_stream
*stream
;
2061 struct lttcomm_relayd_quiescent_control msg
;
2062 struct lttcomm_relayd_generic_reply reply
;
2064 DBG("Checking quiescent state on control socket");
2066 if (!conn
->session
|| !conn
->version_check_done
) {
2067 ERR("Trying to check for data before version check");
2069 goto end_no_session
;
2072 if (payload
->size
< sizeof(msg
)) {
2073 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2074 sizeof(msg
), payload
->size
);
2076 goto end_no_session
;
2078 memcpy(&msg
, payload
->data
, sizeof(msg
));
2079 msg
.stream_id
= be64toh(msg
.stream_id
);
2081 stream
= stream_get_by_id(msg
.stream_id
);
2085 pthread_mutex_lock(&stream
->lock
);
2086 stream
->data_pending_check_done
= true;
2087 pthread_mutex_unlock(&stream
->lock
);
2089 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2092 memset(&reply
, 0, sizeof(reply
));
2093 reply
.ret_code
= htobe32(LTTNG_OK
);
2094 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2095 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2096 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2108 * Initialize a data pending command. This means that a consumer is about
2109 * to ask for data pending for each stream it holds. Simply iterate over
2110 * all streams of a session and set the data_pending_check_done flag.
2112 * This command returns to the client a LTTNG_OK code.
2114 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2115 struct relay_connection
*conn
,
2116 const struct lttng_buffer_view
*payload
)
2120 struct lttng_ht_iter iter
;
2121 struct lttcomm_relayd_begin_data_pending msg
;
2122 struct lttcomm_relayd_generic_reply reply
;
2123 struct relay_stream
*stream
;
2128 DBG("Init streams for data pending");
2130 if (!conn
->session
|| !conn
->version_check_done
) {
2131 ERR("Trying to check for data before version check");
2133 goto end_no_session
;
2136 if (payload
->size
< sizeof(msg
)) {
2137 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2138 sizeof(msg
), payload
->size
);
2140 goto end_no_session
;
2142 memcpy(&msg
, payload
->data
, sizeof(msg
));
2143 msg
.session_id
= be64toh(msg
.session_id
);
2146 * Iterate over all streams to set the begin data pending flag.
2147 * For now, the streams are indexed by stream handle so we have
2148 * to iterate over all streams to find the one associated with
2149 * the right session_id.
2152 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2154 if (!stream_get(stream
)) {
2157 if (stream
->trace
->session
->id
== msg
.session_id
) {
2158 pthread_mutex_lock(&stream
->lock
);
2159 stream
->data_pending_check_done
= false;
2160 pthread_mutex_unlock(&stream
->lock
);
2161 DBG("Set begin data pending flag to stream %" PRIu64
,
2162 stream
->stream_handle
);
2168 memset(&reply
, 0, sizeof(reply
));
2169 /* All good, send back reply. */
2170 reply
.ret_code
= htobe32(LTTNG_OK
);
2172 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2173 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2174 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2186 * End data pending command. This will check, for a given session id, if
2187 * each stream associated with it has its data_pending_check_done flag
2188 * set. If not, this means that the client lost track of the stream but
2189 * the data is still being streamed on our side. In this case, we inform
2190 * the client that data is in flight.
2192 * Return to the client if there is data in flight or not with a ret_code.
2194 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2195 struct relay_connection
*conn
,
2196 const struct lttng_buffer_view
*payload
)
2200 struct lttng_ht_iter iter
;
2201 struct lttcomm_relayd_end_data_pending msg
;
2202 struct lttcomm_relayd_generic_reply reply
;
2203 struct relay_stream
*stream
;
2204 uint32_t is_data_inflight
= 0;
2206 DBG("End data pending command");
2208 if (!conn
->session
|| !conn
->version_check_done
) {
2209 ERR("Trying to check for data before version check");
2211 goto end_no_session
;
2214 if (payload
->size
< sizeof(msg
)) {
2215 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2216 sizeof(msg
), payload
->size
);
2218 goto end_no_session
;
2220 memcpy(&msg
, payload
->data
, sizeof(msg
));
2221 msg
.session_id
= be64toh(msg
.session_id
);
2224 * Iterate over all streams to see if the begin data pending
2228 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2230 if (!stream_get(stream
)) {
2233 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2237 pthread_mutex_lock(&stream
->lock
);
2238 if (!stream
->data_pending_check_done
) {
2239 uint64_t stream_seq
;
2241 if (session_streams_have_index(conn
->session
)) {
2243 * Ensure that both the index and stream data have been
2244 * flushed up to the requested point.
2246 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2248 stream_seq
= stream
->prev_data_seq
;
2250 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2251 is_data_inflight
= 1;
2252 DBG("Data is still in flight for stream %" PRIu64
,
2253 stream
->stream_handle
);
2254 pthread_mutex_unlock(&stream
->lock
);
2259 pthread_mutex_unlock(&stream
->lock
);
2264 memset(&reply
, 0, sizeof(reply
));
2265 /* All good, send back reply. */
2266 reply
.ret_code
= htobe32(is_data_inflight
);
2268 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2269 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2270 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2282 * Receive an index for a specific stream.
2284 * Return 0 on success else a negative value.
2286 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2287 struct relay_connection
*conn
,
2288 const struct lttng_buffer_view
*payload
)
2292 struct relay_session
*session
= conn
->session
;
2293 struct lttcomm_relayd_index index_info
;
2294 struct lttcomm_relayd_generic_reply reply
;
2295 struct relay_stream
*stream
;
2300 DBG("Relay receiving index");
2302 if (!session
|| !conn
->version_check_done
) {
2303 ERR("Trying to close a stream before version check");
2305 goto end_no_session
;
2308 msg_len
= lttcomm_relayd_index_len(
2309 lttng_to_index_major(conn
->major
, conn
->minor
),
2310 lttng_to_index_minor(conn
->major
, conn
->minor
));
2311 if (payload
->size
< msg_len
) {
2312 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2313 msg_len
, payload
->size
);
2315 goto end_no_session
;
2317 memcpy(&index_info
, payload
->data
, msg_len
);
2318 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2319 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2320 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2321 index_info
.content_size
= be64toh(index_info
.content_size
);
2322 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2323 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2324 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2325 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2327 if (conn
->minor
>= 8) {
2328 index_info
.stream_instance_id
=
2329 be64toh(index_info
.stream_instance_id
);
2330 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2332 index_info
.stream_instance_id
= -1ULL;
2333 index_info
.packet_seq_num
= -1ULL;
2336 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2338 ERR("stream_get_by_id not found");
2343 pthread_mutex_lock(&stream
->lock
);
2344 ret
= stream_add_index(stream
, &index_info
);
2345 pthread_mutex_unlock(&stream
->lock
);
2347 goto end_stream_put
;
2353 memset(&reply
, 0, sizeof(reply
));
2355 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2357 reply
.ret_code
= htobe32(LTTNG_OK
);
2359 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2360 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2361 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2370 * Receive the streams_sent message.
2372 * Return 0 on success else a negative value.
2374 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2375 struct relay_connection
*conn
,
2376 const struct lttng_buffer_view
*payload
)
2380 struct lttcomm_relayd_generic_reply reply
;
2384 DBG("Relay receiving streams_sent");
2386 if (!conn
->session
|| !conn
->version_check_done
) {
2387 ERR("Trying to close a stream before version check");
2389 goto end_no_session
;
2393 * Publish every pending stream in the connection recv list which are
2394 * now ready to be used by the viewer.
2396 publish_connection_local_streams(conn
);
2398 memset(&reply
, 0, sizeof(reply
));
2399 reply
.ret_code
= htobe32(LTTNG_OK
);
2400 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2401 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2402 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2415 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2416 * session rotation feature (not the tracefile rotation feature).
2418 static int relay_rotate_session_streams(
2419 const struct lttcomm_relayd_hdr
*recv_hdr
,
2420 struct relay_connection
*conn
,
2421 const struct lttng_buffer_view
*payload
)
2426 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2427 struct relay_session
*session
= conn
->session
;
2428 struct lttcomm_relayd_rotate_streams rotate_streams
;
2429 struct lttcomm_relayd_generic_reply reply
= {};
2430 struct relay_stream
*stream
= NULL
;
2431 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2432 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2433 struct lttng_buffer_view stream_positions
;
2434 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2435 const char *chunk_id_str
= "none";
2437 if (!session
|| !conn
->version_check_done
) {
2438 ERR("Trying to rotate a stream before version check");
2443 if (session
->major
== 2 && session
->minor
< 11) {
2444 ERR("Unsupported feature before 2.11");
2449 if (payload
->size
< header_len
) {
2450 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2451 header_len
, payload
->size
);
2456 memcpy(&rotate_streams
, payload
->data
, header_len
);
2458 /* Convert header to host endianness. */
2459 rotate_streams
= (typeof(rotate_streams
)) {
2460 .stream_count
= be32toh(rotate_streams
.stream_count
),
2461 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2462 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2463 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2467 if (rotate_streams
.new_chunk_id
.is_set
) {
2469 * Retrieve the trace chunk the stream must transition to. As
2470 * per the protocol, this chunk should have been created
2471 * before this command is received.
2473 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2474 sessiond_trace_chunk_registry
,
2475 session
->sessiond_uuid
, session
->id
,
2476 rotate_streams
.new_chunk_id
.value
);
2477 if (!next_trace_chunk
) {
2478 char uuid_str
[LTTNG_UUID_STR_LEN
];
2480 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2481 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2482 ", trace_chunk_id = %" PRIu64
,
2483 uuid_str
, session
->id
,
2484 rotate_streams
.new_chunk_id
.value
);
2485 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2490 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2491 rotate_streams
.new_chunk_id
.value
);
2492 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2493 chunk_id_str
= "formatting error";
2495 chunk_id_str
= chunk_id_buf
;
2499 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2500 rotate_streams
.stream_count
, session
->session_name
,
2503 stream_positions
= lttng_buffer_view_from_view(payload
,
2504 sizeof(rotate_streams
), -1);
2505 if (!stream_positions
.data
||
2506 stream_positions
.size
<
2507 (rotate_streams
.stream_count
*
2508 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2509 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2514 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2515 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2516 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2517 const struct lttcomm_relayd_stream_rotation_position pos
= {
2518 .stream_id
= be64toh(position_comm
->stream_id
),
2519 .rotate_at_seq_num
= be64toh(
2520 position_comm
->rotate_at_seq_num
),
2523 stream
= stream_get_by_id(pos
.stream_id
);
2525 reply_code
= LTTNG_ERR_INVALID
;
2530 pthread_mutex_lock(&stream
->lock
);
2531 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2532 pos
.rotate_at_seq_num
);
2533 pthread_mutex_unlock(&stream
->lock
);
2535 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2543 reply_code
= LTTNG_OK
;
2550 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2551 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2552 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2553 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2554 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2559 lttng_trace_chunk_put(next_trace_chunk
);
2566 * relay_create_trace_chunk: create a new trace chunk
2568 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2569 struct relay_connection
*conn
,
2570 const struct lttng_buffer_view
*payload
)
2574 struct relay_session
*session
= conn
->session
;
2575 struct lttcomm_relayd_create_trace_chunk
*msg
;
2576 struct lttcomm_relayd_generic_reply reply
= {};
2577 struct lttng_buffer_view header_view
;
2578 struct lttng_buffer_view chunk_name_view
;
2579 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2580 enum lttng_error_code reply_code
= LTTNG_OK
;
2581 enum lttng_trace_chunk_status chunk_status
;
2582 struct lttng_directory_handle
*session_output
= NULL
;
2583 const char *new_path
;
2585 if (!session
|| !conn
->version_check_done
) {
2586 ERR("Trying to create a trace chunk before version check");
2591 if (session
->major
== 2 && session
->minor
< 11) {
2592 ERR("Chunk creation command is unsupported before 2.11");
2597 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2598 if (!header_view
.data
) {
2599 ERR("Failed to receive payload of chunk creation command");
2604 /* Convert to host endianness. */
2605 msg
= (typeof(msg
)) header_view
.data
;
2606 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2607 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2608 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2610 if (session
->current_trace_chunk
&&
2611 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2612 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2613 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
2614 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2615 ERR("Failed to rename old chunk");
2617 reply_code
= LTTNG_ERR_UNK
;
2621 session
->ongoing_rotation
= true;
2622 if (!session
->current_trace_chunk
) {
2623 if (!session
->has_rotated
) {
2629 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
2631 chunk
= lttng_trace_chunk_create(
2632 msg
->chunk_id
, msg
->creation_timestamp
, new_path
);
2634 ERR("Failed to create trace chunk in trace chunk creation command");
2636 reply_code
= LTTNG_ERR_NOMEM
;
2640 if (msg
->override_name_length
) {
2643 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2645 msg
->override_name_length
);
2646 name
= chunk_name_view
.data
;
2647 if (!name
|| name
[msg
->override_name_length
- 1]) {
2648 ERR("Failed to receive payload of chunk creation command");
2650 reply_code
= LTTNG_ERR_INVALID
;
2654 chunk_status
= lttng_trace_chunk_override_name(
2655 chunk
, chunk_name_view
.data
);
2656 switch (chunk_status
) {
2657 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2659 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2660 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2661 reply_code
= LTTNG_ERR_INVALID
;
2665 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2666 reply_code
= LTTNG_ERR_UNK
;
2672 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2673 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2674 reply_code
= LTTNG_ERR_UNK
;
2679 session_output
= session_create_output_directory_handle(
2681 if (!session_output
) {
2682 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2685 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, session_output
);
2686 lttng_directory_handle_put(session_output
);
2687 session_output
= NULL
;
2688 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2689 reply_code
= LTTNG_ERR_UNK
;
2694 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2695 sessiond_trace_chunk_registry
,
2696 conn
->session
->sessiond_uuid
,
2699 if (!published_chunk
) {
2700 char uuid_str
[LTTNG_UUID_STR_LEN
];
2702 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2703 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2708 reply_code
= LTTNG_ERR_NOMEM
;
2712 pthread_mutex_lock(&conn
->session
->lock
);
2713 if (conn
->session
->pending_closure_trace_chunk
) {
2715 * Invalid; this means a second create_trace_chunk command was
2716 * received before a close_trace_chunk.
2718 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2719 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2721 goto end_unlock_session
;
2723 conn
->session
->pending_closure_trace_chunk
=
2724 conn
->session
->current_trace_chunk
;
2725 conn
->session
->current_trace_chunk
= published_chunk
;
2726 published_chunk
= NULL
;
2727 if (!conn
->session
->pending_closure_trace_chunk
) {
2728 session
->ongoing_rotation
= false;
2731 pthread_mutex_unlock(&conn
->session
->lock
);
2733 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2734 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2736 sizeof(struct lttcomm_relayd_generic_reply
),
2738 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2739 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2744 lttng_trace_chunk_put(chunk
);
2745 lttng_trace_chunk_put(published_chunk
);
2746 lttng_directory_handle_put(session_output
);
2751 * relay_close_trace_chunk: close a trace chunk
2753 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2754 struct relay_connection
*conn
,
2755 const struct lttng_buffer_view
*payload
)
2757 int ret
= 0, buf_ret
;
2759 struct relay_session
*session
= conn
->session
;
2760 struct lttcomm_relayd_close_trace_chunk
*msg
;
2761 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2762 struct lttng_buffer_view header_view
;
2763 struct lttng_trace_chunk
*chunk
= NULL
;
2764 enum lttng_error_code reply_code
= LTTNG_OK
;
2765 enum lttng_trace_chunk_status chunk_status
;
2767 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2768 time_t close_timestamp
;
2769 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2770 size_t path_length
= 0;
2771 const char *chunk_name
= NULL
;
2772 struct lttng_dynamic_buffer reply_payload
;
2773 const char *new_path
;
2775 lttng_dynamic_buffer_init(&reply_payload
);
2777 if (!session
|| !conn
->version_check_done
) {
2778 ERR("Trying to close a trace chunk before version check");
2783 if (session
->major
== 2 && session
->minor
< 11) {
2784 ERR("Chunk close command is unsupported before 2.11");
2789 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2790 if (!header_view
.data
) {
2791 ERR("Failed to receive payload of chunk close command");
2796 /* Convert to host endianness. */
2797 msg
= (typeof(msg
)) header_view
.data
;
2798 chunk_id
= be64toh(msg
->chunk_id
);
2799 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2800 close_command
= (typeof(close_command
)){
2801 .value
= be32toh(msg
->close_command
.value
),
2802 .is_set
= msg
->close_command
.is_set
,
2805 chunk
= sessiond_trace_chunk_registry_get_chunk(
2806 sessiond_trace_chunk_registry
,
2807 conn
->session
->sessiond_uuid
,
2811 char uuid_str
[LTTNG_UUID_STR_LEN
];
2813 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2814 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2819 reply_code
= LTTNG_ERR_NOMEM
;
2823 pthread_mutex_lock(&session
->lock
);
2824 if (close_command
.is_set
&&
2825 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
) {
2827 * Clear command. It is a protocol error to ask for a
2828 * clear on a relay which does not allow it. Querying
2829 * the configuration allows figuring out whether
2830 * clearing is allowed before doing the clear.
2832 if (!opt_allow_clear
) {
2834 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2835 goto end_unlock_session
;
2838 if (session
->pending_closure_trace_chunk
&&
2839 session
->pending_closure_trace_chunk
!= chunk
) {
2840 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2841 session
->session_name
);
2842 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2844 goto end_unlock_session
;
2847 if (session
->current_trace_chunk
&& session
->current_trace_chunk
!= chunk
&&
2848 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2849 if (close_command
.is_set
&&
2850 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&&
2851 !session
->has_rotated
) {
2852 /* New chunk stays in session output directory. */
2855 /* Use chunk name for new chunk. */
2858 /* Rename new chunk path. */
2859 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2861 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2865 session
->ongoing_rotation
= false;
2867 if ((!close_command
.is_set
||
2868 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) &&
2869 !lttng_trace_chunk_get_name_overridden(chunk
)) {
2870 const char *old_path
;
2872 if (!session
->has_rotated
) {
2877 /* We need to move back the .tmp_old_chunk to its rightful place. */
2878 chunk_status
= lttng_trace_chunk_rename_path(chunk
, old_path
);
2879 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2884 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2885 chunk
, close_timestamp
);
2886 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2887 ERR("Failed to set trace chunk close timestamp");
2889 reply_code
= LTTNG_ERR_UNK
;
2890 goto end_unlock_session
;
2893 if (close_command
.is_set
) {
2894 chunk_status
= lttng_trace_chunk_set_close_command(
2895 chunk
, close_command
.value
);
2896 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2898 reply_code
= LTTNG_ERR_INVALID
;
2899 goto end_unlock_session
;
2902 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2903 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2904 ERR("Failed to get chunk name");
2906 reply_code
= LTTNG_ERR_UNK
;
2907 goto end_unlock_session
;
2909 if (!session
->has_rotated
&& !session
->snapshot
) {
2910 ret
= lttng_strncpy(closed_trace_chunk_path
,
2911 session
->output_path
,
2912 sizeof(closed_trace_chunk_path
));
2914 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2915 strlen(session
->output_path
),
2916 sizeof(closed_trace_chunk_path
));
2917 reply_code
= LTTNG_ERR_NOMEM
;
2919 goto end_unlock_session
;
2922 if (session
->snapshot
) {
2923 ret
= snprintf(closed_trace_chunk_path
,
2924 sizeof(closed_trace_chunk_path
),
2925 "%s/%s", session
->output_path
,
2928 ret
= snprintf(closed_trace_chunk_path
,
2929 sizeof(closed_trace_chunk_path
),
2930 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2932 session
->output_path
, chunk_name
);
2934 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2935 ERR("Failed to format closed trace chunk resulting path");
2936 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2938 goto end_unlock_session
;
2941 if (close_command
.is_set
&&
2942 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
2943 session
->has_rotated
= true;
2945 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2946 path_length
= strlen(closed_trace_chunk_path
) + 1;
2947 if (path_length
> UINT32_MAX
) {
2948 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2950 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2951 goto end_unlock_session
;
2954 if (session
->current_trace_chunk
== chunk
) {
2956 * After a trace chunk close command, no new streams
2957 * referencing the chunk may be created. Hence, on the
2958 * event that no new trace chunk have been created for
2959 * the session, the reference to the current trace chunk
2960 * is released in order to allow it to be reclaimed when
2961 * the last stream releases its reference to it.
2963 lttng_trace_chunk_put(session
->current_trace_chunk
);
2964 session
->current_trace_chunk
= NULL
;
2966 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2967 session
->pending_closure_trace_chunk
= NULL
;
2969 pthread_mutex_unlock(&session
->lock
);
2972 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
2973 reply
.path_length
= htobe32((uint32_t) path_length
);
2974 buf_ret
= lttng_dynamic_buffer_append(
2975 &reply_payload
, &reply
, sizeof(reply
));
2977 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2981 if (reply_code
== LTTNG_OK
) {
2982 buf_ret
= lttng_dynamic_buffer_append(&reply_payload
,
2983 closed_trace_chunk_path
, path_length
);
2985 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2990 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2994 if (send_ret
< reply_payload
.size
) {
2995 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2996 reply_payload
.size
, send_ret
);
3001 lttng_trace_chunk_put(chunk
);
3002 lttng_dynamic_buffer_reset(&reply_payload
);
3007 * relay_trace_chunk_exists: check if a trace chunk exists
3009 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
3010 struct relay_connection
*conn
,
3011 const struct lttng_buffer_view
*payload
)
3015 struct relay_session
*session
= conn
->session
;
3016 struct lttcomm_relayd_trace_chunk_exists
*msg
;
3017 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
3018 struct lttng_buffer_view header_view
;
3022 if (!session
|| !conn
->version_check_done
) {
3023 ERR("Trying to close a trace chunk before version check");
3028 if (session
->major
== 2 && session
->minor
< 11) {
3029 ERR("Chunk close command is unsupported before 2.11");
3034 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3035 if (!header_view
.data
) {
3036 ERR("Failed to receive payload of chunk close command");
3041 /* Convert to host endianness. */
3042 msg
= (typeof(msg
)) header_view
.data
;
3043 chunk_id
= be64toh(msg
->chunk_id
);
3045 ret
= sessiond_trace_chunk_registry_chunk_exists(
3046 sessiond_trace_chunk_registry
,
3047 conn
->session
->sessiond_uuid
,
3049 chunk_id
, &chunk_exists
);
3051 * If ret is not 0, send the reply and report the error to the caller.
3052 * It is a protocol (or internal) error and the session/connection
3053 * should be torn down.
3055 reply
= (typeof(reply
)){
3056 .generic
.ret_code
= htobe32((uint32_t)
3057 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3058 .trace_chunk_exists
= ret
== 0 ? chunk_exists
: 0,
3060 send_ret
= conn
->sock
->ops
->sendmsg(
3061 conn
->sock
, &reply
, sizeof(reply
), 0);
3062 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3063 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
3072 * relay_get_configuration: query whether feature is available
3074 static int relay_get_configuration(const struct lttcomm_relayd_hdr
*recv_hdr
,
3075 struct relay_connection
*conn
,
3076 const struct lttng_buffer_view
*payload
)
3080 struct lttcomm_relayd_get_configuration
*msg
;
3081 struct lttcomm_relayd_get_configuration_reply reply
= {};
3082 struct lttng_buffer_view header_view
;
3083 uint64_t query_flags
= 0;
3084 uint64_t result_flags
= 0;
3086 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3087 if (!header_view
.data
) {
3088 ERR("Failed to receive payload of chunk close command");
3093 /* Convert to host endianness. */
3094 msg
= (typeof(msg
)) header_view
.data
;
3095 query_flags
= be64toh(msg
->query_flags
);
3098 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
3101 if (opt_allow_clear
) {
3102 result_flags
|= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
;
3106 reply
= (typeof(reply
)){
3107 .generic
.ret_code
= htobe32((uint32_t)
3108 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3109 .relayd_configuration_flags
= htobe64(result_flags
),
3111 send_ret
= conn
->sock
->ops
->sendmsg(
3112 conn
->sock
, &reply
, sizeof(reply
), 0);
3113 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3114 ERR("Failed to send \"get configuration\" command reply (ret = %zd)",
3122 #define DBG_CMD(cmd_name, conn) \
3123 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3125 static int relay_process_control_command(struct relay_connection
*conn
,
3126 const struct lttcomm_relayd_hdr
*header
,
3127 const struct lttng_buffer_view
*payload
)
3131 switch (header
->cmd
) {
3132 case RELAYD_CREATE_SESSION
:
3133 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
3134 ret
= relay_create_session(header
, conn
, payload
);
3136 case RELAYD_ADD_STREAM
:
3137 DBG_CMD("RELAYD_ADD_STREAM", conn
);
3138 ret
= relay_add_stream(header
, conn
, payload
);
3140 case RELAYD_START_DATA
:
3141 DBG_CMD("RELAYD_START_DATA", conn
);
3142 ret
= relay_start(header
, conn
, payload
);
3144 case RELAYD_SEND_METADATA
:
3145 DBG_CMD("RELAYD_SEND_METADATA", conn
);
3146 ret
= relay_recv_metadata(header
, conn
, payload
);
3148 case RELAYD_VERSION
:
3149 DBG_CMD("RELAYD_VERSION", conn
);
3150 ret
= relay_send_version(header
, conn
, payload
);
3152 case RELAYD_CLOSE_STREAM
:
3153 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
3154 ret
= relay_close_stream(header
, conn
, payload
);
3156 case RELAYD_DATA_PENDING
:
3157 DBG_CMD("RELAYD_DATA_PENDING", conn
);
3158 ret
= relay_data_pending(header
, conn
, payload
);
3160 case RELAYD_QUIESCENT_CONTROL
:
3161 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
3162 ret
= relay_quiescent_control(header
, conn
, payload
);
3164 case RELAYD_BEGIN_DATA_PENDING
:
3165 DBG_CMD("RELAYD_BEGIN_DATA_PENDING",