2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/resource.h>
39 #include <urcu/futex.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/rculist.h>
47 #include <lttng/lttng.h>
48 #include <common/common.h>
49 #include <common/compat/poll.h>
50 #include <common/compat/socket.h>
51 #include <common/compat/endian.h>
52 #include <common/compat/getenv.h>
53 #include <common/defaults.h>
54 #include <common/daemonize.h>
55 #include <common/futex.h>
56 #include <common/sessiond-comm/sessiond-comm.h>
57 #include <common/sessiond-comm/inet.h>
58 #include <common/sessiond-comm/relayd.h>
59 #include <common/uri.h>
60 #include <common/utils.h>
61 #include <common/align.h>
62 #include <common/config/session-config.h>
63 #include <common/dynamic-buffer.h>
64 #include <common/buffer-view.h>
65 #include <common/string-utils/format.h>
66 #include <common/fd-tracker/fd-tracker.h>
67 #include <common/fd-tracker/utils.h>
69 #include "backward-compatibility-group-by.h"
71 #include "connection.h"
72 #include "ctf-trace.h"
73 #include "health-relayd.h"
76 #include "lttng-relayd.h"
78 #include "sessiond-trace-chunks.h"
80 #include "tcp_keep_alive.h"
81 #include "testpoint.h"
82 #include "tracefile-array.h"
85 #include "viewer-stream.h"
87 static const char *help_msg
=
88 #ifdef LTTNG_EMBED_HELP
89 #include <lttng-relayd.8.h>
95 enum relay_connection_status
{
96 RELAY_CONNECTION_STATUS_OK
,
97 /* An error occurred while processing an event on the connection. */
98 RELAY_CONNECTION_STATUS_ERROR
,
99 /* Connection closed/shutdown cleanly. */
100 RELAY_CONNECTION_STATUS_CLOSED
,
103 /* command line options */
104 char *opt_output_path
, *opt_working_directory
;
105 static int opt_daemon
, opt_background
, opt_print_version
, opt_allow_clear
= 1;
106 enum relay_group_output_by opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
;
109 * We need to wait for listener and live listener threads, as well as
110 * health check thread, before being ready to signal readiness.
112 #define NR_LTTNG_RELAY_READY 3
113 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
115 /* Size of receive buffer. */
116 #define RECV_DATA_BUFFER_SIZE 65536
118 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
119 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
121 static struct lttng_uri
*control_uri
;
122 static struct lttng_uri
*data_uri
;
123 static struct lttng_uri
*live_uri
;
125 const char *progname
;
127 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
128 static int tracing_group_name_override
;
130 const char * const config_section_name
= "relayd";
133 * Quit pipe for all threads. This permits a single cancellation point
134 * for all threads when receiving an event on the pipe.
136 int thread_quit_pipe
[2] = { -1, -1 };
139 * This pipe is used to inform the worker thread that a command is queued and
140 * ready to be processed.
142 static int relay_conn_pipe
[2] = { -1, -1 };
144 /* Shared between threads */
145 static int dispatch_thread_exit
;
147 static pthread_t listener_thread
;
148 static pthread_t dispatcher_thread
;
149 static pthread_t worker_thread
;
150 static pthread_t health_thread
;
153 * last_relay_stream_id_lock protects last_relay_stream_id increment
154 * atomicity on 32-bit architectures.
156 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
157 static uint64_t last_relay_stream_id
;
160 * Relay command queue.
162 * The relay_thread_listener and relay_thread_dispatcher communicate with this
165 static struct relay_conn_queue relay_conn_queue
;
167 /* Cap of file desriptors to be in simultaneous use by the relay daemon. */
168 static unsigned int lttng_opt_fd_cap
;
170 /* Global relay stream hash table. */
171 struct lttng_ht
*relay_streams_ht
;
173 /* Global relay viewer stream hash table. */
174 struct lttng_ht
*viewer_streams_ht
;
176 /* Global relay sessions hash table. */
177 struct lttng_ht
*sessions_ht
;
179 /* Relayd health monitoring */
180 struct health_app
*health_relayd
;
182 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
184 /* Global fd tracker. */
185 struct fd_tracker
*the_fd_tracker
;
187 static struct option long_options
[] = {
188 { "control-port", 1, 0, 'C', },
189 { "data-port", 1, 0, 'D', },
190 { "live-port", 1, 0, 'L', },
191 { "daemonize", 0, 0, 'd', },
192 { "background", 0, 0, 'b', },
193 { "group", 1, 0, 'g', },
194 { "fd-cap", 1, 0, '\0', },
195 { "help", 0, 0, 'h', },
196 { "output", 1, 0, 'o', },
197 { "verbose", 0, 0, 'v', },
198 { "config", 1, 0, 'f' },
199 { "version", 0, 0, 'V' },
200 { "working-directory", 1, 0, 'w', },
201 { "group-output-by-session", 0, 0, 's', },
202 { "group-output-by-host", 0, 0, 'p', },
203 { "disallow-clear", 0, 0, 'x' },
207 static const char *config_ignore_options
[] = { "help", "config", "version" };
209 static void print_version(void) {
210 fprintf(stdout
, "%s\n", VERSION
);
213 static void relayd_config_log(void)
215 DBG("LTTng-relayd " VERSION
" - " VERSION_NAME
"%s%s",
216 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
217 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
218 if (EXTRA_VERSION_DESCRIPTION
[0] != '\0') {
219 DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION
"\n");
221 if (EXTRA_VERSION_PATCHES
[0] != '\0') {
222 DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES
"\n");
227 * Take an option from the getopt output and set it in the right variable to be
230 * Return 0 on success else a negative value.
232 static int set_option(int opt
, const char *arg
, const char *optname
)
238 if (!strcmp(optname
, "fd-cap")) {
242 v
= strtoul(arg
, NULL
, 0);
243 if (errno
!= 0 || !isdigit(arg
[0])) {
244 ERR("Wrong value in --fd-cap parameter: %s",
249 if (v
< DEFAULT_RELAYD_MINIMAL_FD_CAP
) {
250 ERR("File descriptor cap must be set to at least %d",
251 DEFAULT_RELAYD_MINIMAL_FD_CAP
);
254 ERR("File descriptor cap overflow in --fd-cap parameter: %s",
259 lttng_opt_fd_cap
= (unsigned int) v
;
260 DBG3("File descriptor cap set to %u", lttng_opt_fd_cap
);
262 fprintf(stderr
, "unknown option %s", optname
);
264 fprintf(stderr
, " with arg %s\n", arg
);
269 if (lttng_is_setuid_setgid()) {
270 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
271 "-C, --control-port");
273 ret
= uri_parse(arg
, &control_uri
);
275 ERR("Invalid control URI specified");
278 if (control_uri
->port
== 0) {
279 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
284 if (lttng_is_setuid_setgid()) {
285 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
288 ret
= uri_parse(arg
, &data_uri
);
290 ERR("Invalid data URI specified");
293 if (data_uri
->port
== 0) {
294 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
299 if (lttng_is_setuid_setgid()) {
300 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
303 ret
= uri_parse(arg
, &live_uri
);
305 ERR("Invalid live URI specified");
308 if (live_uri
->port
== 0) {
309 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
320 if (lttng_is_setuid_setgid()) {
321 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
324 tracing_group_name
= strdup(arg
);
325 if (tracing_group_name
== NULL
) {
330 tracing_group_name_override
= 1;
334 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
336 ERR("Cannot show --help for `lttng-relayd`");
341 opt_print_version
= 1;
344 if (lttng_is_setuid_setgid()) {
345 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
348 ret
= asprintf(&opt_output_path
, "%s", arg
);
351 PERROR("asprintf opt_output_path");
357 if (lttng_is_setuid_setgid()) {
358 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
359 "-w, --working-directory");
361 ret
= asprintf(&opt_working_directory
, "%s", arg
);
364 PERROR("asprintf opt_working_directory");
371 /* Verbose level can increase using multiple -v */
373 lttng_opt_verbose
= config_parse_value(arg
);
375 /* Only 3 level of verbosity (-vvv). */
376 if (lttng_opt_verbose
< 3) {
377 lttng_opt_verbose
+= 1;
382 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
383 ERR("Cannot set --group-output-by-session, another --group-output-by argument is present");
386 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_SESSION
;
389 if (opt_group_output_by
!= RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
390 ERR("Cannot set --group-output-by-host, another --group-output-by argument is present");
393 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
400 /* Unknown option or other error.
401 * Error is printed by getopt, just return */
414 * config_entry_handler_cb used to handle options read from a config file.
415 * See config_entry_handler_cb comment in common/config/session-config.h for the
416 * return value conventions.
418 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
422 if (!entry
|| !entry
->name
|| !entry
->value
) {
427 /* Check if the option is to be ignored */
428 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
429 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
434 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
435 /* Ignore if entry name is not fully matched. */
436 if (strcmp(entry
->name
, long_options
[i
].name
)) {
441 * If the option takes no argument on the command line,
442 * we have to check if the value is "true". We support
443 * non-zero numeric values, true, on and yes.
445 if (!long_options
[i
].has_arg
) {
446 ret
= config_parse_value(entry
->value
);
449 WARN("Invalid configuration value \"%s\" for option %s",
450 entry
->value
, entry
->name
);
452 /* False, skip boolean config option. */
457 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
461 WARN("Unrecognized option \"%s\" in daemon configuration file.",
468 static int parse_env_options(void)
473 value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_WORKING_DIRECTORY_ENV
);
475 opt_working_directory
= strdup(value
);
476 if (!opt_working_directory
) {
477 ERR("Failed to allocate working directory string (\"%s\")",
485 static int set_options(int argc
, char **argv
)
487 int c
, ret
= 0, option_index
= 0, retval
= 0;
488 int orig_optopt
= optopt
, orig_optind
= optind
;
489 char *default_address
, *optstring
;
490 const char *config_path
= NULL
;
492 optstring
= utils_generate_optstring(long_options
,
493 sizeof(long_options
) / sizeof(struct option
));
499 /* Check for the --config option */
501 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
502 &option_index
)) != -1) {
506 } else if (c
!= 'f') {
510 if (lttng_is_setuid_setgid()) {
511 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
514 config_path
= utils_expand_path(optarg
);
516 ERR("Failed to resolve path: %s", optarg
);
521 ret
= config_get_section_entries(config_path
, config_section_name
,
522 config_entry_handler
, NULL
);
525 ERR("Invalid configuration option at line %i", ret
);
531 /* Reset getopt's global state */
532 optopt
= orig_optopt
;
533 optind
= orig_optind
;
535 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
540 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
547 /* assign default values */
548 if (control_uri
== NULL
) {
549 ret
= asprintf(&default_address
,
550 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
551 DEFAULT_NETWORK_CONTROL_PORT
);
553 PERROR("asprintf default data address");
558 ret
= uri_parse(default_address
, &control_uri
);
559 free(default_address
);
561 ERR("Invalid control URI specified");
566 if (data_uri
== NULL
) {
567 ret
= asprintf(&default_address
,
568 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
569 DEFAULT_NETWORK_DATA_PORT
);
571 PERROR("asprintf default data address");
576 ret
= uri_parse(default_address
, &data_uri
);
577 free(default_address
);
579 ERR("Invalid data URI specified");
584 if (live_uri
== NULL
) {
585 ret
= asprintf(&default_address
,
586 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
587 DEFAULT_NETWORK_VIEWER_PORT
);
589 PERROR("asprintf default viewer control address");
594 ret
= uri_parse(default_address
, &live_uri
);
595 free(default_address
);
597 ERR("Invalid viewer control URI specified");
602 if (lttng_opt_fd_cap
== 0) {
604 struct rlimit rlimit
;
606 ret
= getrlimit(RLIMIT_NOFILE
, &rlimit
);
608 PERROR("Failed to get file descriptor limit");
612 lttng_opt_fd_cap
= rlimit
.rlim_cur
;
615 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_UNKNOWN
) {
616 opt_group_output_by
= RELAYD_GROUP_OUTPUT_BY_HOST
;
618 if (opt_allow_clear
) {
619 /* Check if env variable exists. */
620 const char *value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
622 ret
= config_parse_value(value
);
624 ERR("Invalid value for %s specified", DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
628 opt_allow_clear
= !ret
;
637 static void print_global_objects(void)
639 print_viewer_streams();
640 print_relay_streams();
647 static void relayd_cleanup(void)
649 print_global_objects();
653 if (viewer_streams_ht
)
654 lttng_ht_destroy(viewer_streams_ht
);
655 if (relay_streams_ht
)
656 lttng_ht_destroy(relay_streams_ht
);
658 lttng_ht_destroy(sessions_ht
);
660 free(opt_output_path
);
661 free(opt_working_directory
);
664 health_app_destroy(health_relayd
);
666 /* Close thread quit pipes */
667 if (health_quit_pipe
[0] != -1) {
668 (void) fd_tracker_util_pipe_close(
669 the_fd_tracker
, health_quit_pipe
);
671 if (thread_quit_pipe
[0] != -1) {
672 (void) fd_tracker_util_pipe_close(
673 the_fd_tracker
, thread_quit_pipe
);
675 if (sessiond_trace_chunk_registry
) {
676 sessiond_trace_chunk_registry_destroy(
677 sessiond_trace_chunk_registry
);
679 if (the_fd_tracker
) {
680 fd_tracker_destroy(the_fd_tracker
);
683 uri_free(control_uri
);
685 /* Live URI is freed in the live thread. */
687 if (tracing_group_name_override
) {
688 free((void *) tracing_group_name
);
690 fd_tracker_log(the_fd_tracker
);
694 * Write to writable pipe used to notify a thread.
696 static int notify_thread_pipe(int wpipe
)
700 ret
= lttng_write(wpipe
, "!", 1);
702 PERROR("write poll pipe");
710 static int notify_health_quit_pipe(int *pipe
)
714 ret
= lttng_write(pipe
[1], "4", 1);
716 PERROR("write relay health quit");
725 * Stop all relayd and relayd-live threads.
727 int lttng_relay_stop_threads(void)
731 /* Stopping all threads */
732 DBG("Terminating all threads");
733 if (notify_thread_pipe(thread_quit_pipe
[1])) {
734 ERR("write error on thread quit pipe");
738 if (notify_health_quit_pipe(health_quit_pipe
)) {
739 ERR("write error on health quit pipe");
742 /* Dispatch thread */
743 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
744 futex_nto1_wake(&relay_conn_queue
.futex
);
746 if (relayd_live_stop()) {
747 ERR("Error stopping live threads");
754 * Signal handler for the daemon
756 * Simply stop all worker threads, leaving main() return gracefully after
757 * joining all threads and calling cleanup().
759 static void sighandler(int sig
)
763 DBG("SIGINT caught");
764 if (lttng_relay_stop_threads()) {
765 ERR("Error stopping threads");
769 DBG("SIGTERM caught");
770 if (lttng_relay_stop_threads()) {
771 ERR("Error stopping threads");
775 CMM_STORE_SHARED(recv_child_signal
, 1);
783 * Setup signal handler for :
784 * SIGINT, SIGTERM, SIGPIPE
786 static int set_signal_handler(void)
792 if ((ret
= sigemptyset(&sigset
)) < 0) {
793 PERROR("sigemptyset");
800 sa
.sa_handler
= sighandler
;
801 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
806 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
811 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
816 sa
.sa_handler
= SIG_IGN
;
817 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
822 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
827 void lttng_relay_notify_ready(void)
829 /* Notify the parent of the fork() process that we are ready. */
830 if (opt_daemon
|| opt_background
) {
831 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
832 kill(child_ppid
, SIGUSR1
);
838 * Init thread quit pipe.
840 * Return -1 on error or 0 if all pipes are created.
842 static int init_thread_quit_pipe(void)
844 return fd_tracker_util_pipe_open_cloexec(
845 the_fd_tracker
, "Quit pipe", thread_quit_pipe
);
849 * Init health quit pipe.
851 * Return -1 on error or 0 if all pipes are created.
853 static int init_health_quit_pipe(void)
855 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
856 "Health quit pipe", health_quit_pipe
);
860 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
862 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
866 if (events
== NULL
|| size
== 0) {
871 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
877 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
889 * Check if the thread quit pipe was triggered.
891 * Return 1 if it was triggered else 0;
893 static int check_thread_quit_pipe(int fd
, uint32_t events
)
895 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
903 * Create and init socket from uri.
905 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
908 struct lttcomm_sock
*sock
= NULL
;
910 sock
= lttcomm_alloc_sock_from_uri(uri
);
912 ERR("Allocating socket");
916 ret
= lttcomm_create_sock(sock
);
920 DBG("Listening on sock %d", sock
->fd
);
922 ret
= sock
->ops
->bind(sock
);
924 PERROR("Failed to bind socket");
928 ret
= sock
->ops
->listen(sock
, -1);
938 lttcomm_destroy_sock(sock
);
944 * This thread manages the listening for new connections on the network
946 static void *relay_thread_listener(void *data
)
948 int i
, ret
, pollfd
, err
= -1;
949 uint32_t revents
, nb_fd
;
950 struct lttng_poll_event events
;
951 struct lttcomm_sock
*control_sock
, *data_sock
;
953 DBG("[thread] Relay listener started");
955 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
957 health_code_update();
959 control_sock
= relay_socket_create(control_uri
);
961 goto error_sock_control
;
964 data_sock
= relay_socket_create(data_uri
);
966 goto error_sock_relay
;
970 * Pass 3 as size here for the thread quit pipe, control and
973 ret
= create_thread_poll_set(&events
, 3);
975 goto error_create_poll
;
978 /* Add the control socket */
979 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
984 /* Add the data socket */
985 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
990 lttng_relay_notify_ready();
992 if (testpoint(relayd_thread_listener
)) {
993 goto error_testpoint
;
997 health_code_update();
999 DBG("Listener accepting connections");
1002 health_poll_entry();
1003 ret
= lttng_poll_wait(&events
, -1);
1007 * Restart interrupted system call.
1009 if (errno
== EINTR
) {
1017 DBG("Relay new connection received");
1018 for (i
= 0; i
< nb_fd
; i
++) {
1019 health_code_update();
1021 /* Fetch once the poll data */
1022 revents
= LTTNG_POLL_GETEV(&events
, i
);
1023 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1025 /* Thread quit pipe has been closed. Killing thread. */
1026 ret
= check_thread_quit_pipe(pollfd
, revents
);
1032 if (revents
& LPOLLIN
) {
1034 * A new connection is requested, therefore a
1035 * sessiond/consumerd connection is allocated in
1036 * this thread, enqueued to a global queue and
1037 * dequeued (and freed) in the worker thread.
1040 struct relay_connection
*new_conn
;
1041 struct lttcomm_sock
*newsock
;
1042 enum connection_type type
;
1044 if (pollfd
== data_sock
->fd
) {
1046 newsock
= data_sock
->ops
->accept(data_sock
);
1047 DBG("Relay data connection accepted, socket %d",
1050 assert(pollfd
== control_sock
->fd
);
1051 type
= RELAY_CONTROL
;
1052 newsock
= control_sock
->ops
->accept(control_sock
);
1053 DBG("Relay control connection accepted, socket %d",
1057 PERROR("accepting sock");
1061 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
1064 PERROR("setsockopt inet");
1065 lttcomm_destroy_sock(newsock
);
1069 ret
= socket_apply_keep_alive_config(newsock
->fd
);
1071 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1073 lttcomm_destroy_sock(newsock
);
1077 new_conn
= connection_create(newsock
, type
);
1079 lttcomm_destroy_sock(newsock
);
1083 /* Enqueue request for the dispatcher thread. */
1084 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
1088 * Wake the dispatch queue futex.
1089 * Implicit memory barrier with the
1090 * exchange in cds_wfcq_enqueue.
1092 futex_nto1_wake(&relay_conn_queue
.futex
);
1093 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1094 ERR("socket poll error");
1097 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1107 lttng_poll_clean(&events
);
1109 if (data_sock
->fd
>= 0) {
1110 ret
= data_sock
->ops
->close(data_sock
);
1115 lttcomm_destroy_sock(data_sock
);
1117 if (control_sock
->fd
>= 0) {
1118 ret
= control_sock
->ops
->close(control_sock
);
1123 lttcomm_destroy_sock(control_sock
);
1127 ERR("Health error occurred in %s", __func__
);
1129 health_unregister(health_relayd
);
1130 DBG("Relay listener thread cleanup complete");
1131 lttng_relay_stop_threads();
1136 * This thread manages the dispatching of the requests to worker threads
1138 static void *relay_thread_dispatcher(void *data
)
1142 struct cds_wfcq_node
*node
;
1143 struct relay_connection
*new_conn
= NULL
;
1145 DBG("[thread] Relay dispatcher started");
1147 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1149 if (testpoint(relayd_thread_dispatcher
)) {
1150 goto error_testpoint
;
1153 health_code_update();
1156 health_code_update();
1158 /* Atomically prepare the queue futex */
1159 futex_nto1_prepare(&relay_conn_queue
.futex
);
1161 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1166 health_code_update();
1168 /* Dequeue commands */
1169 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1170 &relay_conn_queue
.tail
);
1172 DBG("Woken up but nothing in the relay command queue");
1173 /* Continue thread execution */
1176 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1178 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1181 * Inform worker thread of the new request. This
1182 * call is blocking so we can be assured that
1183 * the data will be read at some point in time
1184 * or wait to the end of the world :)
1186 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1188 PERROR("write connection pipe");
1189 connection_put(new_conn
);
1192 } while (node
!= NULL
);
1194 /* Futex wait on queue. Blocking call on futex() */
1195 health_poll_entry();
1196 futex_nto1_wait(&relay_conn_queue
.futex
);
1200 /* Normal exit, no error */
1207 ERR("Health error occurred in %s", __func__
);
1209 health_unregister(health_relayd
);
1210 DBG("Dispatch thread dying");
1211 lttng_relay_stop_threads();
1215 static bool session_streams_have_index(const struct relay_session
*session
)
1217 return session
->minor
>= 4 && !session
->snapshot
;
1221 * Handle the RELAYD_CREATE_SESSION command.
1223 * On success, send back the session id or else return a negative value.
1225 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1226 struct relay_connection
*conn
,
1227 const struct lttng_buffer_view
*payload
)
1231 struct relay_session
*session
= NULL
;
1232 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1233 char session_name
[LTTNG_NAME_MAX
] = {};
1234 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1235 uint32_t live_timer
= 0;
1236 bool snapshot
= false;
1237 bool session_name_contains_creation_timestamp
= false;
1238 /* Left nil for peers < 2.11. */
1239 char base_path
[LTTNG_PATH_MAX
] = {};
1240 lttng_uuid sessiond_uuid
= {};
1241 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1242 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1243 LTTNG_OPTIONAL(time_t) creation_time
= {};
1244 struct lttng_dynamic_buffer reply_payload
;
1246 lttng_dynamic_buffer_init(&reply_payload
);
1248 if (conn
->minor
< 4) {
1249 /* From 2.1 to 2.3 */
1251 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1252 /* From 2.4 to 2.10 */
1253 ret
= cmd_create_session_2_4(payload
, session_name
,
1254 hostname
, &live_timer
, &snapshot
);
1256 bool has_current_chunk
;
1257 uint64_t current_chunk_id_value
;
1258 time_t creation_time_value
;
1259 uint64_t id_sessiond_value
;
1261 /* From 2.11 to ... */
1262 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1263 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1264 sessiond_uuid
, &has_current_chunk
,
1265 ¤t_chunk_id_value
, &creation_time_value
,
1266 &session_name_contains_creation_timestamp
);
1267 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1268 /* The nil UUID is reserved for pre-2.11 clients. */
1269 ERR("Illegal nil UUID announced by peer in create session command");
1273 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1274 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1275 if (has_current_chunk
) {
1276 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1277 current_chunk_id_value
);
1285 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1286 snapshot
, sessiond_uuid
,
1287 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1288 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1289 creation_time
.is_set
? &creation_time
.value
: NULL
,
1290 conn
->major
, conn
->minor
,
1291 session_name_contains_creation_timestamp
);
1296 assert(!conn
->session
);
1297 conn
->session
= session
;
1298 DBG("Created session %" PRIu64
, session
->id
);
1300 reply
.generic
.session_id
= htobe64(session
->id
);
1304 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1306 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1309 if (conn
->minor
< 11) {
1310 /* From 2.1 to 2.10 */
1311 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1312 &reply
.generic
, sizeof(reply
.generic
));
1314 ERR("Failed to append \"create session\" command reply header to payload buffer");
1319 const uint32_t output_path_length
=
1320 session
? strlen(session
->output_path
) + 1 : 0;
1322 reply
.output_path_length
= htobe32(output_path_length
);
1323 ret
= lttng_dynamic_buffer_append(
1324 &reply_payload
, &reply
, sizeof(reply
));
1326 ERR("Failed to append \"create session\" command reply header to payload buffer");
1330 if (output_path_length
) {
1331 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1332 session
->output_path
,
1333 output_path_length
);
1335 ERR("Failed to append \"create session\" command reply path to payload buffer");
1341 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1342 reply_payload
.size
, 0);
1343 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1344 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1345 reply_payload
.size
, send_ret
);
1349 if (ret
< 0 && session
) {
1350 session_put(session
);
1352 lttng_dynamic_buffer_reset(&reply_payload
);
1357 * When we have received all the streams and the metadata for a channel,
1358 * we make them visible to the viewer threads.
1360 static void publish_connection_local_streams(struct relay_connection
*conn
)
1362 struct relay_stream
*stream
;
1363 struct relay_session
*session
= conn
->session
;
1366 * We publish all streams belonging to a session atomically wrt
1369 pthread_mutex_lock(&session
->lock
);
1371 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1373 stream_publish(stream
);
1378 * Inform the viewer that there are new streams in the session.
1380 if (session
->viewer_attached
) {
1381 uatomic_set(&session
->new_streams
, 1);
1383 pthread_mutex_unlock(&session
->lock
);
1386 static int conform_channel_path(char *channel_path
)
1390 if (strstr("../", channel_path
)) {
1391 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1397 if (*channel_path
== '/') {
1398 const size_t len
= strlen(channel_path
);
1401 * Channel paths from peers prior to 2.11 are expressed as an
1402 * absolute path that is, in reality, relative to the relay
1403 * daemon's output directory. Remove the leading slash so it
1404 * is correctly interpreted as a relative path later on.
1406 * len (and not len - 1) is used to copy the trailing NULL.
1408 bcopy(channel_path
+ 1, channel_path
, len
);
1415 * relay_add_stream: allocate a new stream for a session
1417 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1418 struct relay_connection
*conn
,
1419 const struct lttng_buffer_view
*payload
)
1423 struct relay_session
*session
= conn
->session
;
1424 struct relay_stream
*stream
= NULL
;
1425 struct lttcomm_relayd_status_stream reply
;
1426 struct ctf_trace
*trace
= NULL
;
1427 uint64_t stream_handle
= -1ULL;
1428 char *path_name
= NULL
, *channel_name
= NULL
;
1429 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1430 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1432 if (!session
|| !conn
->version_check_done
) {
1433 ERR("Trying to add a stream before version check");
1435 goto end_no_session
;
1438 if (session
->minor
== 1) {
1440 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1442 } else if (session
->minor
> 1 && session
->minor
< 11) {
1443 /* From 2.2 to 2.10 */
1444 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1445 &channel_name
, &tracefile_size
, &tracefile_count
);
1447 /* From 2.11 to ... */
1448 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1449 &channel_name
, &tracefile_size
, &tracefile_count
,
1450 &stream_chunk_id
.value
);
1451 stream_chunk_id
.is_set
= true;
1458 if (conform_channel_path(path_name
)) {
1463 * Backward compatibility for --group-output-by-session.
1464 * Prior to lttng 2.11, the complete path is passed by the stream.
1465 * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
1466 * >=2.11 the chunk is responsible for the output path. When dealing
1467 * with producer < 2.11 the chunk output_path is the root output path
1468 * and the stream carries the complete path (path_name).
1469 * To support --group-output-by-session with older producer (<2.11), we
1470 * need to craft the path based on the stream path.
1472 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_SESSION
) {
1473 if (conn
->minor
< 4) {
1475 * From 2.1 to 2.3, the session_name is not passed on
1476 * the RELAYD_CREATE_SESSION command. The session name
1477 * is necessary to detect the presence of a base_path
1478 * inside the stream path. Without it we cannot perform
1479 * a valid group-output-by-session transformation.
1481 WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
1482 " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
1483 session
->id
, path_name
);
1484 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1485 char *group_by_session_path_name
;
1487 assert(session
->session_name
[0] != '\0');
1489 group_by_session_path_name
=
1490 backward_compat_group_by_session(
1492 session
->session_name
);
1493 if (!group_by_session_path_name
) {
1494 ERR("Failed to apply group by session to stream of session %" PRIu64
,
1499 DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
1500 path_name
, group_by_session_path_name
);
1503 path_name
= group_by_session_path_name
;
1507 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1512 /* This stream here has one reference on the trace. */
1513 pthread_mutex_lock(&last_relay_stream_id_lock
);
1514 stream_handle
= ++last_relay_stream_id
;
1515 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1517 /* We pass ownership of path_name and channel_name. */
1518 stream
= stream_create(trace
, stream_handle
, path_name
,
1519 channel_name
, tracefile_size
, tracefile_count
);
1521 channel_name
= NULL
;
1524 * Streams are the owners of their trace. Reference to trace is
1525 * kept within stream_create().
1527 ctf_trace_put(trace
);
1530 memset(&reply
, 0, sizeof(reply
));
1531 reply
.handle
= htobe64(stream_handle
);
1533 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1535 reply
.ret_code
= htobe32(LTTNG_OK
);
1538 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1539 sizeof(struct lttcomm_relayd_status_stream
), 0);
1540 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1541 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1553 * relay_close_stream: close a specific stream
1555 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1556 struct relay_connection
*conn
,
1557 const struct lttng_buffer_view
*payload
)
1561 struct relay_session
*session
= conn
->session
;
1562 struct lttcomm_relayd_close_stream stream_info
;
1563 struct lttcomm_relayd_generic_reply reply
;
1564 struct relay_stream
*stream
;
1566 DBG("Close stream received");
1568 if (!session
|| !conn
->version_check_done
) {
1569 ERR("Trying to close a stream before version check");
1571 goto end_no_session
;
1574 if (payload
->size
< sizeof(stream_info
)) {
1575 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1576 sizeof(stream_info
), payload
->size
);
1578 goto end_no_session
;
1580 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1581 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1582 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1584 stream
= stream_get_by_id(stream_info
.stream_id
);
1591 * Set last_net_seq_num before the close flag. Required by data
1594 pthread_mutex_lock(&stream
->lock
);
1595 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1596 pthread_mutex_unlock(&stream
->lock
);
1599 * This is one of the conditions which may trigger a stream close
1600 * with the others being:
1601 * 1) A close command is received for a stream
1602 * 2) The control connection owning the stream is closed
1603 * 3) We have received all of the stream's data _after_ a close
1606 try_stream_close(stream
);
1607 if (stream
->is_metadata
) {
1608 struct relay_viewer_stream
*vstream
;
1610 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1612 if (stream
->no_new_metadata_notified
) {
1614 * Since all the metadata has been sent to the
1615 * viewer and that we have a request to close
1616 * its stream, we can safely teardown the
1617 * corresponding metadata viewer stream.
1619 viewer_stream_put(vstream
);
1621 /* Put local reference. */
1622 viewer_stream_put(vstream
);
1629 memset(&reply
, 0, sizeof(reply
));
1631 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1633 reply
.ret_code
= htobe32(LTTNG_OK
);
1635 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1636 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1637 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1638 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1648 * relay_reset_metadata: reset a metadata stream
1651 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1652 struct relay_connection
*conn
,
1653 const struct lttng_buffer_view
*payload
)
1657 struct relay_session
*session
= conn
->session
;
1658 struct lttcomm_relayd_reset_metadata stream_info
;
1659 struct lttcomm_relayd_generic_reply reply
;
1660 struct relay_stream
*stream
;
1662 DBG("Reset metadata received");
1664 if (!session
|| !conn
->version_check_done
) {
1665 ERR("Trying to reset a metadata stream before version check");
1667 goto end_no_session
;
1670 if (payload
->size
< sizeof(stream_info
)) {
1671 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1672 sizeof(stream_info
), payload
->size
);
1674 goto end_no_session
;
1676 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1677 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1678 stream_info
.version
= be64toh(stream_info
.version
);
1680 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1682 /* Unsupported for live sessions for now. */
1683 if (session
->live_timer
!= 0) {
1688 stream
= stream_get_by_id(stream_info
.stream_id
);
1693 pthread_mutex_lock(&stream
->lock
);
1694 if (!stream
->is_metadata
) {
1699 ret
= stream_reset_file(stream
);
1701 ERR("Failed to reset metadata stream %" PRIu64
1702 ": stream_path = %s, channel = %s",
1703 stream
->stream_handle
, stream
->path_name
,
1704 stream
->channel_name
);
1708 pthread_mutex_unlock(&stream
->lock
);
1712 memset(&reply
, 0, sizeof(reply
));
1714 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1716 reply
.ret_code
= htobe32(LTTNG_OK
);
1718 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1719 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1720 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1721 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1731 * relay_unknown_command: send -1 if received unknown command
1733 static void relay_unknown_command(struct relay_connection
*conn
)
1735 struct lttcomm_relayd_generic_reply reply
;
1738 memset(&reply
, 0, sizeof(reply
));
1739 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1740 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1741 if (send_ret
< sizeof(reply
)) {
1742 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1747 * relay_start: send an acknowledgment to the client to tell if we are
1748 * ready to receive data. We are ready if a session is established.
1750 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1751 struct relay_connection
*conn
,
1752 const struct lttng_buffer_view
*payload
)
1756 struct lttcomm_relayd_generic_reply reply
;
1757 struct relay_session
*session
= conn
->session
;
1760 DBG("Trying to start the streaming without a session established");
1761 ret
= htobe32(LTTNG_ERR_UNK
);
1764 memset(&reply
, 0, sizeof(reply
));
1765 reply
.ret_code
= htobe32(LTTNG_OK
);
1766 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1768 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1769 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1778 * relay_recv_metadata: receive the metadata for the session.
1780 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1781 struct relay_connection
*conn
,
1782 const struct lttng_buffer_view
*payload
)
1785 struct relay_session
*session
= conn
->session
;
1786 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1787 struct relay_stream
*metadata_stream
;
1788 uint64_t metadata_payload_size
;
1789 struct lttng_buffer_view packet_view
;
1792 ERR("Metadata sent before version check");
1797 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1798 ERR("Incorrect data size");
1802 metadata_payload_size
= recv_hdr
->data_size
-
1803 sizeof(struct lttcomm_relayd_metadata_payload
);
1805 memcpy(&metadata_payload_header
, payload
->data
,
1806 sizeof(metadata_payload_header
));
1807 metadata_payload_header
.stream_id
= be64toh(
1808 metadata_payload_header
.stream_id
);
1809 metadata_payload_header
.padding_size
= be32toh(
1810 metadata_payload_header
.padding_size
);
1812 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1813 if (!metadata_stream
) {
1818 packet_view
= lttng_buffer_view_from_view(payload
,
1819 sizeof(metadata_payload_header
), metadata_payload_size
);
1820 if (!packet_view
.data
) {
1821 ERR("Invalid metadata packet length announced by header");
1826 pthread_mutex_lock(&metadata_stream
->lock
);
1827 ret
= stream_write(metadata_stream
, &packet_view
,
1828 metadata_payload_header
.padding_size
);
1829 pthread_mutex_unlock(&metadata_stream
->lock
);
1835 stream_put(metadata_stream
);
1841 * relay_send_version: send relayd version number
1843 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1844 struct relay_connection
*conn
,
1845 const struct lttng_buffer_view
*payload
)
1849 struct lttcomm_relayd_version reply
, msg
;
1850 bool compatible
= true;
1852 conn
->version_check_done
= true;
1854 /* Get version from the other side. */
1855 if (payload
->size
< sizeof(msg
)) {
1856 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1857 sizeof(msg
), payload
->size
);
1862 memcpy(&msg
, payload
->data
, sizeof(msg
));
1863 msg
.major
= be32toh(msg
.major
);
1864 msg
.minor
= be32toh(msg
.minor
);
1866 memset(&reply
, 0, sizeof(reply
));
1867 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1868 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1870 /* Major versions must be the same */
1871 if (reply
.major
!= msg
.major
) {
1872 DBG("Incompatible major versions (%u vs %u), deleting session",
1873 reply
.major
, msg
.major
);
1877 conn
->major
= reply
.major
;
1878 /* We adapt to the lowest compatible version */
1879 if (reply
.minor
<= msg
.minor
) {
1880 conn
->minor
= reply
.minor
;
1882 conn
->minor
= msg
.minor
;
1885 reply
.major
= htobe32(reply
.major
);
1886 reply
.minor
= htobe32(reply
.minor
);
1887 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1889 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1890 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1903 DBG("Version check done using protocol %u.%u", conn
->major
,
1911 * Check for data pending for a given stream id from the session daemon.
1913 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1914 struct relay_connection
*conn
,
1915 const struct lttng_buffer_view
*payload
)
1917 struct relay_session
*session
= conn
->session
;
1918 struct lttcomm_relayd_data_pending msg
;
1919 struct lttcomm_relayd_generic_reply reply
;
1920 struct relay_stream
*stream
;
1923 uint64_t stream_seq
;
1925 DBG("Data pending command received");
1927 if (!session
|| !conn
->version_check_done
) {
1928 ERR("Trying to check for data before version check");
1930 goto end_no_session
;
1933 if (payload
->size
< sizeof(msg
)) {
1934 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1935 sizeof(msg
), payload
->size
);
1937 goto end_no_session
;
1939 memcpy(&msg
, payload
->data
, sizeof(msg
));
1940 msg
.stream_id
= be64toh(msg
.stream_id
);
1941 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1943 stream
= stream_get_by_id(msg
.stream_id
);
1944 if (stream
== NULL
) {
1949 pthread_mutex_lock(&stream
->lock
);
1951 if (session_streams_have_index(session
)) {
1953 * Ensure that both the index and stream data have been
1954 * flushed up to the requested point.
1956 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1958 stream_seq
= stream
->prev_data_seq
;
1960 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
1961 ", prev_index_seq %" PRIu64
1962 ", and last_seq %" PRIu64
, msg
.stream_id
,
1963 stream
->prev_data_seq
, stream
->prev_index_seq
,
1964 msg
.last_net_seq_num
);
1966 /* Avoid wrapping issue */
1967 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
1968 /* Data has in fact been written and is NOT pending */
1971 /* Data still being streamed thus pending */
1975 stream
->data_pending_check_done
= true;
1976 pthread_mutex_unlock(&stream
->lock
);
1981 memset(&reply
, 0, sizeof(reply
));
1982 reply
.ret_code
= htobe32(ret
);
1983 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1984 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1985 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1995 * Wait for the control socket to reach a quiescent state.
1997 * Note that for now, when receiving this command from the session
1998 * daemon, this means that every subsequent commands or data received on
1999 * the control socket has been handled. So, this is why we simply return
2002 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2003 struct relay_connection
*conn
,
2004 const struct lttng_buffer_view
*payload
)
2008 struct relay_stream
*stream
;
2009 struct lttcomm_relayd_quiescent_control msg
;
2010 struct lttcomm_relayd_generic_reply reply
;
2012 DBG("Checking quiescent state on control socket");
2014 if (!conn
->session
|| !conn
->version_check_done
) {
2015 ERR("Trying to check for data before version check");
2017 goto end_no_session
;
2020 if (payload
->size
< sizeof(msg
)) {
2021 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2022 sizeof(msg
), payload
->size
);
2024 goto end_no_session
;
2026 memcpy(&msg
, payload
->data
, sizeof(msg
));
2027 msg
.stream_id
= be64toh(msg
.stream_id
);
2029 stream
= stream_get_by_id(msg
.stream_id
);
2033 pthread_mutex_lock(&stream
->lock
);
2034 stream
->data_pending_check_done
= true;
2035 pthread_mutex_unlock(&stream
->lock
);
2037 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2040 memset(&reply
, 0, sizeof(reply
));
2041 reply
.ret_code
= htobe32(LTTNG_OK
);
2042 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2043 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2044 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2056 * Initialize a data pending command. This means that a consumer is about
2057 * to ask for data pending for each stream it holds. Simply iterate over
2058 * all streams of a session and set the data_pending_check_done flag.
2060 * This command returns to the client a LTTNG_OK code.
2062 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2063 struct relay_connection
*conn
,
2064 const struct lttng_buffer_view
*payload
)
2068 struct lttng_ht_iter iter
;
2069 struct lttcomm_relayd_begin_data_pending msg
;
2070 struct lttcomm_relayd_generic_reply reply
;
2071 struct relay_stream
*stream
;
2076 DBG("Init streams for data pending");
2078 if (!conn
->session
|| !conn
->version_check_done
) {
2079 ERR("Trying to check for data before version check");
2081 goto end_no_session
;
2084 if (payload
->size
< sizeof(msg
)) {
2085 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2086 sizeof(msg
), payload
->size
);
2088 goto end_no_session
;
2090 memcpy(&msg
, payload
->data
, sizeof(msg
));
2091 msg
.session_id
= be64toh(msg
.session_id
);
2094 * Iterate over all streams to set the begin data pending flag.
2095 * For now, the streams are indexed by stream handle so we have
2096 * to iterate over all streams to find the one associated with
2097 * the right session_id.
2100 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2102 if (!stream_get(stream
)) {
2105 if (stream
->trace
->session
->id
== msg
.session_id
) {
2106 pthread_mutex_lock(&stream
->lock
);
2107 stream
->data_pending_check_done
= false;
2108 pthread_mutex_unlock(&stream
->lock
);
2109 DBG("Set begin data pending flag to stream %" PRIu64
,
2110 stream
->stream_handle
);
2116 memset(&reply
, 0, sizeof(reply
));
2117 /* All good, send back reply. */
2118 reply
.ret_code
= htobe32(LTTNG_OK
);
2120 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2121 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2122 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2134 * End data pending command. This will check, for a given session id, if
2135 * each stream associated with it has its data_pending_check_done flag
2136 * set. If not, this means that the client lost track of the stream but
2137 * the data is still being streamed on our side. In this case, we inform
2138 * the client that data is in flight.
2140 * Return to the client if there is data in flight or not with a ret_code.
2142 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2143 struct relay_connection
*conn
,
2144 const struct lttng_buffer_view
*payload
)
2148 struct lttng_ht_iter iter
;
2149 struct lttcomm_relayd_end_data_pending msg
;
2150 struct lttcomm_relayd_generic_reply reply
;
2151 struct relay_stream
*stream
;
2152 uint32_t is_data_inflight
= 0;
2154 DBG("End data pending command");
2156 if (!conn
->session
|| !conn
->version_check_done
) {
2157 ERR("Trying to check for data before version check");
2159 goto end_no_session
;
2162 if (payload
->size
< sizeof(msg
)) {
2163 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2164 sizeof(msg
), payload
->size
);
2166 goto end_no_session
;
2168 memcpy(&msg
, payload
->data
, sizeof(msg
));
2169 msg
.session_id
= be64toh(msg
.session_id
);
2172 * Iterate over all streams to see if the begin data pending
2176 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2178 if (!stream_get(stream
)) {
2181 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2185 pthread_mutex_lock(&stream
->lock
);
2186 if (!stream
->data_pending_check_done
) {
2187 uint64_t stream_seq
;
2189 if (session_streams_have_index(conn
->session
)) {
2191 * Ensure that both the index and stream data have been
2192 * flushed up to the requested point.
2194 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2196 stream_seq
= stream
->prev_data_seq
;
2198 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2199 is_data_inflight
= 1;
2200 DBG("Data is still in flight for stream %" PRIu64
,
2201 stream
->stream_handle
);
2202 pthread_mutex_unlock(&stream
->lock
);
2207 pthread_mutex_unlock(&stream
->lock
);
2212 memset(&reply
, 0, sizeof(reply
));
2213 /* All good, send back reply. */
2214 reply
.ret_code
= htobe32(is_data_inflight
);
2216 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2217 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2218 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2230 * Receive an index for a specific stream.
2232 * Return 0 on success else a negative value.
2234 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2235 struct relay_connection
*conn
,
2236 const struct lttng_buffer_view
*payload
)
2240 struct relay_session
*session
= conn
->session
;
2241 struct lttcomm_relayd_index index_info
;
2242 struct lttcomm_relayd_generic_reply reply
;
2243 struct relay_stream
*stream
;
2248 DBG("Relay receiving index");
2250 if (!session
|| !conn
->version_check_done
) {
2251 ERR("Trying to close a stream before version check");
2253 goto end_no_session
;
2256 msg_len
= lttcomm_relayd_index_len(
2257 lttng_to_index_major(conn
->major
, conn
->minor
),
2258 lttng_to_index_minor(conn
->major
, conn
->minor
));
2259 if (payload
->size
< msg_len
) {
2260 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2261 msg_len
, payload
->size
);
2263 goto end_no_session
;
2265 memcpy(&index_info
, payload
->data
, msg_len
);
2266 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2267 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2268 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2269 index_info
.content_size
= be64toh(index_info
.content_size
);
2270 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2271 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2272 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2273 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2275 if (conn
->minor
>= 8) {
2276 index_info
.stream_instance_id
=
2277 be64toh(index_info
.stream_instance_id
);
2278 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2280 index_info
.stream_instance_id
= -1ULL;
2281 index_info
.packet_seq_num
= -1ULL;
2284 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2286 ERR("stream_get_by_id not found");
2291 pthread_mutex_lock(&stream
->lock
);
2292 ret
= stream_add_index(stream
, &index_info
);
2293 pthread_mutex_unlock(&stream
->lock
);
2295 goto end_stream_put
;
2301 memset(&reply
, 0, sizeof(reply
));
2303 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2305 reply
.ret_code
= htobe32(LTTNG_OK
);
2307 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2308 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2309 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2318 * Receive the streams_sent message.
2320 * Return 0 on success else a negative value.
2322 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2323 struct relay_connection
*conn
,
2324 const struct lttng_buffer_view
*payload
)
2328 struct lttcomm_relayd_generic_reply reply
;
2332 DBG("Relay receiving streams_sent");
2334 if (!conn
->session
|| !conn
->version_check_done
) {
2335 ERR("Trying to close a stream before version check");
2337 goto end_no_session
;
2341 * Publish every pending stream in the connection recv list which are
2342 * now ready to be used by the viewer.
2344 publish_connection_local_streams(conn
);
2346 memset(&reply
, 0, sizeof(reply
));
2347 reply
.ret_code
= htobe32(LTTNG_OK
);
2348 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2349 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2350 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2363 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2364 * session rotation feature (not the tracefile rotation feature).
2366 static int relay_rotate_session_streams(
2367 const struct lttcomm_relayd_hdr
*recv_hdr
,
2368 struct relay_connection
*conn
,
2369 const struct lttng_buffer_view
*payload
)
2374 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2375 struct relay_session
*session
= conn
->session
;
2376 struct lttcomm_relayd_rotate_streams rotate_streams
;
2377 struct lttcomm_relayd_generic_reply reply
= {};
2378 struct relay_stream
*stream
= NULL
;
2379 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2380 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2381 struct lttng_buffer_view stream_positions
;
2382 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2383 const char *chunk_id_str
= "none";
2385 if (!session
|| !conn
->version_check_done
) {
2386 ERR("Trying to rotate a stream before version check");
2391 if (session
->major
== 2 && session
->minor
< 11) {
2392 ERR("Unsupported feature before 2.11");
2397 if (payload
->size
< header_len
) {
2398 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2399 header_len
, payload
->size
);
2404 memcpy(&rotate_streams
, payload
->data
, header_len
);
2406 /* Convert header to host endianness. */
2407 rotate_streams
= (typeof(rotate_streams
)) {
2408 .stream_count
= be32toh(rotate_streams
.stream_count
),
2409 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2410 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2411 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2415 if (rotate_streams
.new_chunk_id
.is_set
) {
2417 * Retrieve the trace chunk the stream must transition to. As
2418 * per the protocol, this chunk should have been created
2419 * before this command is received.
2421 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2422 sessiond_trace_chunk_registry
,
2423 session
->sessiond_uuid
, session
->id
,
2424 rotate_streams
.new_chunk_id
.value
);
2425 if (!next_trace_chunk
) {
2426 char uuid_str
[LTTNG_UUID_STR_LEN
];
2428 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2429 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2430 ", trace_chunk_id = %" PRIu64
,
2431 uuid_str
, session
->id
,
2432 rotate_streams
.new_chunk_id
.value
);
2433 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2438 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2439 rotate_streams
.new_chunk_id
.value
);
2440 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2441 chunk_id_str
= "formatting error";
2443 chunk_id_str
= chunk_id_buf
;
2447 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2448 rotate_streams
.stream_count
, session
->session_name
,
2451 stream_positions
= lttng_buffer_view_from_view(payload
,
2452 sizeof(rotate_streams
), -1);
2453 if (!stream_positions
.data
||
2454 stream_positions
.size
<
2455 (rotate_streams
.stream_count
*
2456 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2457 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2462 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2463 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2464 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2465 const struct lttcomm_relayd_stream_rotation_position pos
= {
2466 .stream_id
= be64toh(position_comm
->stream_id
),
2467 .rotate_at_seq_num
= be64toh(
2468 position_comm
->rotate_at_seq_num
),
2471 stream
= stream_get_by_id(pos
.stream_id
);
2473 reply_code
= LTTNG_ERR_INVALID
;
2478 pthread_mutex_lock(&stream
->lock
);
2479 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2480 pos
.rotate_at_seq_num
);
2481 pthread_mutex_unlock(&stream
->lock
);
2483 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2491 reply_code
= LTTNG_OK
;
2498 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2499 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2500 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2501 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2502 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2507 lttng_trace_chunk_put(next_trace_chunk
);
2514 * relay_create_trace_chunk: create a new trace chunk
2516 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2517 struct relay_connection
*conn
,
2518 const struct lttng_buffer_view
*payload
)
2522 struct relay_session
*session
= conn
->session
;
2523 struct lttcomm_relayd_create_trace_chunk
*msg
;
2524 struct lttcomm_relayd_generic_reply reply
= {};
2525 struct lttng_buffer_view header_view
;
2526 struct lttng_buffer_view chunk_name_view
;
2527 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2528 enum lttng_error_code reply_code
= LTTNG_OK
;
2529 enum lttng_trace_chunk_status chunk_status
;
2530 struct lttng_directory_handle
*session_output
= NULL
;
2531 const char *new_path
;
2533 if (!session
|| !conn
->version_check_done
) {
2534 ERR("Trying to create a trace chunk before version check");
2539 if (session
->major
== 2 && session
->minor
< 11) {
2540 ERR("Chunk creation command is unsupported before 2.11");
2545 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2546 if (!header_view
.data
) {
2547 ERR("Failed to receive payload of chunk creation command");
2552 /* Convert to host endianness. */
2553 msg
= (typeof(msg
)) header_view
.data
;
2554 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2555 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2556 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2558 if (session
->current_trace_chunk
&&
2559 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2560 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2561 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
2562 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2563 ERR("Failed to rename old chunk");
2565 reply_code
= LTTNG_ERR_UNK
;
2569 session
->ongoing_rotation
= true;
2570 if (!session
->current_trace_chunk
) {
2571 if (!session
->has_rotated
) {
2577 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
2579 chunk
= lttng_trace_chunk_create(
2580 msg
->chunk_id
, msg
->creation_timestamp
, new_path
);
2582 ERR("Failed to create trace chunk in trace chunk creation command");
2584 reply_code
= LTTNG_ERR_NOMEM
;
2588 if (msg
->override_name_length
) {
2591 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2593 msg
->override_name_length
);
2594 name
= chunk_name_view
.data
;
2595 if (!name
|| name
[msg
->override_name_length
- 1]) {
2596 ERR("Failed to receive payload of chunk creation command");
2598 reply_code
= LTTNG_ERR_INVALID
;
2602 chunk_status
= lttng_trace_chunk_override_name(
2603 chunk
, chunk_name_view
.data
);
2604 switch (chunk_status
) {
2605 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2607 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2608 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2609 reply_code
= LTTNG_ERR_INVALID
;
2613 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2614 reply_code
= LTTNG_ERR_UNK
;
2620 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2621 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2622 reply_code
= LTTNG_ERR_UNK
;
2627 session_output
= session_create_output_directory_handle(
2629 if (!session_output
) {
2630 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2633 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, session_output
);
2634 lttng_directory_handle_put(session_output
);
2635 session_output
= NULL
;
2636 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2637 reply_code
= LTTNG_ERR_UNK
;
2642 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2643 sessiond_trace_chunk_registry
,
2644 conn
->session
->sessiond_uuid
,
2647 if (!published_chunk
) {
2648 char uuid_str
[LTTNG_UUID_STR_LEN
];
2650 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2651 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2656 reply_code
= LTTNG_ERR_NOMEM
;
2660 pthread_mutex_lock(&conn
->session
->lock
);
2661 if (conn
->session
->pending_closure_trace_chunk
) {
2663 * Invalid; this means a second create_trace_chunk command was
2664 * received before a close_trace_chunk.
2666 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2667 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2669 goto end_unlock_session
;
2671 conn
->session
->pending_closure_trace_chunk
=
2672 conn
->session
->current_trace_chunk
;
2673 conn
->session
->current_trace_chunk
= published_chunk
;
2674 published_chunk
= NULL
;
2675 if (!conn
->session
->pending_closure_trace_chunk
) {
2676 session
->ongoing_rotation
= false;
2679 pthread_mutex_unlock(&conn
->session
->lock
);
2681 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2682 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2684 sizeof(struct lttcomm_relayd_generic_reply
),
2686 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2687 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2692 lttng_trace_chunk_put(chunk
);
2693 lttng_trace_chunk_put(published_chunk
);
2694 lttng_directory_handle_put(session_output
);
2699 * relay_close_trace_chunk: close a trace chunk
2701 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2702 struct relay_connection
*conn
,
2703 const struct lttng_buffer_view
*payload
)
2705 int ret
= 0, buf_ret
;
2707 struct relay_session
*session
= conn
->session
;
2708 struct lttcomm_relayd_close_trace_chunk
*msg
;
2709 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2710 struct lttng_buffer_view header_view
;
2711 struct lttng_trace_chunk
*chunk
= NULL
;
2712 enum lttng_error_code reply_code
= LTTNG_OK
;
2713 enum lttng_trace_chunk_status chunk_status
;
2715 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2716 time_t close_timestamp
;
2717 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2718 size_t path_length
= 0;
2719 const char *chunk_name
= NULL
;
2720 struct lttng_dynamic_buffer reply_payload
;
2721 const char *new_path
;
2723 lttng_dynamic_buffer_init(&reply_payload
);
2725 if (!session
|| !conn
->version_check_done
) {
2726 ERR("Trying to close a trace chunk before version check");
2731 if (session
->major
== 2 && session
->minor
< 11) {
2732 ERR("Chunk close command is unsupported before 2.11");
2737 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2738 if (!header_view
.data
) {
2739 ERR("Failed to receive payload of chunk close command");
2744 /* Convert to host endianness. */
2745 msg
= (typeof(msg
)) header_view
.data
;
2746 chunk_id
= be64toh(msg
->chunk_id
);
2747 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2748 close_command
= (typeof(close_command
)){
2749 .value
= be32toh(msg
->close_command
.value
),
2750 .is_set
= msg
->close_command
.is_set
,
2753 chunk
= sessiond_trace_chunk_registry_get_chunk(
2754 sessiond_trace_chunk_registry
,
2755 conn
->session
->sessiond_uuid
,
2759 char uuid_str
[LTTNG_UUID_STR_LEN
];
2761 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2762 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2767 reply_code
= LTTNG_ERR_NOMEM
;
2771 pthread_mutex_lock(&session
->lock
);
2772 if (close_command
.is_set
&&
2773 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
) {
2775 * Clear command. It is a protocol error to ask for a
2776 * clear on a relay which does not allow it. Querying
2777 * the configuration allows figuring out whether
2778 * clearing is allowed before doing the clear.
2780 if (!opt_allow_clear
) {
2782 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2783 goto end_unlock_session
;
2786 if (session
->pending_closure_trace_chunk
&&
2787 session
->pending_closure_trace_chunk
!= chunk
) {
2788 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2789 session
->session_name
);
2790 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2792 goto end_unlock_session
;
2795 if (session
->current_trace_chunk
&& session
->current_trace_chunk
!= chunk
&&
2796 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2797 if (close_command
.is_set
&&
2798 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&&
2799 !session
->has_rotated
) {
2800 /* New chunk stays in session output directory. */
2803 /* Use chunk name for new chunk. */
2806 /* Rename new chunk path. */
2807 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2809 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2813 session
->ongoing_rotation
= false;
2815 if ((!close_command
.is_set
||
2816 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) &&
2817 !lttng_trace_chunk_get_name_overridden(chunk
)) {
2818 const char *old_path
;
2820 if (!session
->has_rotated
) {
2825 /* We need to move back the .tmp_old_chunk to its rightful place. */
2826 chunk_status
= lttng_trace_chunk_rename_path(chunk
, old_path
);
2827 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2832 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2833 chunk
, close_timestamp
);
2834 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2835 ERR("Failed to set trace chunk close timestamp");
2837 reply_code
= LTTNG_ERR_UNK
;
2838 goto end_unlock_session
;
2841 if (close_command
.is_set
) {
2842 chunk_status
= lttng_trace_chunk_set_close_command(
2843 chunk
, close_command
.value
);
2844 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2846 reply_code
= LTTNG_ERR_INVALID
;
2847 goto end_unlock_session
;
2850 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2851 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2852 ERR("Failed to get chunk name");
2854 reply_code
= LTTNG_ERR_UNK
;
2855 goto end_unlock_session
;
2857 if (!session
->has_rotated
&& !session
->snapshot
) {
2858 ret
= lttng_strncpy(closed_trace_chunk_path
,
2859 session
->output_path
,
2860 sizeof(closed_trace_chunk_path
));
2862 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2863 strlen(session
->output_path
),
2864 sizeof(closed_trace_chunk_path
));
2865 reply_code
= LTTNG_ERR_NOMEM
;
2867 goto end_unlock_session
;
2870 if (session
->snapshot
) {
2871 ret
= snprintf(closed_trace_chunk_path
,
2872 sizeof(closed_trace_chunk_path
),
2873 "%s/%s", session
->output_path
,
2876 ret
= snprintf(closed_trace_chunk_path
,
2877 sizeof(closed_trace_chunk_path
),
2878 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2880 session
->output_path
, chunk_name
);
2882 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2883 ERR("Failed to format closed trace chunk resulting path");
2884 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2886 goto end_unlock_session
;
2889 if (close_command
.is_set
&&
2890 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
2891 session
->has_rotated
= true;
2893 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2894 path_length
= strlen(closed_trace_chunk_path
) + 1;
2895 if (path_length
> UINT32_MAX
) {
2896 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2898 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2899 goto end_unlock_session
;
2902 if (session
->current_trace_chunk
== chunk
) {
2904 * After a trace chunk close command, no new streams
2905 * referencing the chunk may be created. Hence, on the
2906 * event that no new trace chunk have been created for
2907 * the session, the reference to the current trace chunk
2908 * is released in order to allow it to be reclaimed when
2909 * the last stream releases its reference to it.
2911 lttng_trace_chunk_put(session
->current_trace_chunk
);
2912 session
->current_trace_chunk
= NULL
;
2914 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2915 session
->pending_closure_trace_chunk
= NULL
;
2917 pthread_mutex_unlock(&session
->lock
);
2920 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
2921 reply
.path_length
= htobe32((uint32_t) path_length
);
2922 buf_ret
= lttng_dynamic_buffer_append(
2923 &reply_payload
, &reply
, sizeof(reply
));
2925 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2929 if (reply_code
== LTTNG_OK
) {
2930 buf_ret
= lttng_dynamic_buffer_append(&reply_payload
,
2931 closed_trace_chunk_path
, path_length
);
2933 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2938 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2942 if (send_ret
< reply_payload
.size
) {
2943 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2944 reply_payload
.size
, send_ret
);
2949 lttng_trace_chunk_put(chunk
);
2950 lttng_dynamic_buffer_reset(&reply_payload
);
2955 * relay_trace_chunk_exists: check if a trace chunk exists
2957 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2958 struct relay_connection
*conn
,
2959 const struct lttng_buffer_view
*payload
)
2963 struct relay_session
*session
= conn
->session
;
2964 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2965 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2966 struct lttng_buffer_view header_view
;
2970 if (!session
|| !conn
->version_check_done
) {
2971 ERR("Trying to close a trace chunk before version check");
2976 if (session
->major
== 2 && session
->minor
< 11) {
2977 ERR("Chunk close command is unsupported before 2.11");
2982 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2983 if (!header_view
.data
) {
2984 ERR("Failed to receive payload of chunk close command");
2989 /* Convert to host endianness. */
2990 msg
= (typeof(msg
)) header_view
.data
;
2991 chunk_id
= be64toh(msg
->chunk_id
);
2993 ret
= sessiond_trace_chunk_registry_chunk_exists(
2994 sessiond_trace_chunk_registry
,
2995 conn
->session
->sessiond_uuid
,
2997 chunk_id
, &chunk_exists
);
2999 * If ret is not 0, send the reply and report the error to the caller.
3000 * It is a protocol (or internal) error and the session/connection
3001 * should be torn down.
3003 reply
= (typeof(reply
)){
3004 .generic
.ret_code
= htobe32((uint32_t)
3005 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3006 .trace_chunk_exists
= ret
== 0 ? chunk_exists
: 0,
3008 send_ret
= conn
->sock
->ops
->sendmsg(
3009 conn
->sock
, &reply
, sizeof(reply
), 0);
3010 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3011 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
3020 * relay_get_configuration: query whether feature is available
3022 static int relay_get_configuration(const struct lttcomm_relayd_hdr
*recv_hdr
,
3023 struct relay_connection
*conn
,
3024 const struct lttng_buffer_view
*payload
)
3028 struct lttcomm_relayd_get_configuration
*msg
;
3029 struct lttcomm_relayd_get_configuration_reply reply
= {};
3030 struct lttng_buffer_view header_view
;
3031 uint64_t query_flags
= 0;
3032 uint64_t result_flags
= 0;
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 query_flags
= be64toh(msg
->query_flags
);
3046 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
3049 if (opt_allow_clear
) {
3050 result_flags
|= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
;
3054 reply
= (typeof(reply
)){
3055 .generic
.ret_code
= htobe32((uint32_t)
3056 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3057 .relayd_configuration_flags
= htobe64(result_flags
),
3059 send_ret
= conn
->sock
->ops
->sendmsg(
3060 conn
->sock
, &reply
, sizeof(reply
), 0);
3061 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3062 ERR("Failed to send \"get configuration\" command reply (ret = %zd)",
3070 #define DBG_CMD(cmd_name, conn) \
3071 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3073 static int relay_process_control_command(struct relay_connection
*conn
,
3074 const struct lttcomm_relayd_hdr
*header
,
3075 const struct lttng_buffer_view
*payload
)
3079 switch (header
->cmd
) {
3080 case RELAYD_CREATE_SESSION
:
3081 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
3082 ret
= relay_create_session(header
, conn
, payload
);
3084 case RELAYD_ADD_STREAM
:
3085 DBG_CMD("RELAYD_ADD_STREAM", conn
);
3086 ret
= relay_add_stream(header
, conn
, payload
);
3088 case RELAYD_START_DATA
:
3089 DBG_CMD("RELAYD_START_DATA", conn
);
3090 ret
= relay_start(header
, conn
, payload
);
3092 case RELAYD_SEND_METADATA
:
3093 DBG_CMD("RELAYD_SEND_METADATA", conn
);
3094 ret
= relay_recv_metadata(header
, conn
, payload
);
3096 case RELAYD_VERSION
:
3097 DBG_CMD("RELAYD_VERSION", conn
);
3098 ret
= relay_send_version(header
, conn
, payload
);
3100 case RELAYD_CLOSE_STREAM
:
3101 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
3102 ret
= relay_close_stream(header
, conn
, payload
);
3104 case RELAYD_DATA_PENDING
:
3105 DBG_CMD("RELAYD_DATA_PENDING", conn
);
3106 ret
= relay_data_pending(header
, conn
, payload
);
3108 case RELAYD_QUIESCENT_CONTROL
:
3109 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
3110 ret
= relay_quiescent_control(header
, conn
, payload
);
3112 case RELAYD_BEGIN_DATA_PENDING
:
3113 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
3114 ret
= relay_begin_data_pending(header
, conn
, payload
);
3116 case RELAYD_END_DATA_PENDING
:
3117 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
3118 ret
= relay_end_data_pending(header
, conn
, payload
);
3120 case RELAYD_SEND_INDEX
:
3121 DBG_CMD("RELAYD_SEND_INDEX", conn
);
3122 ret
= relay_recv_index(header
, conn
, payload
);
3124 case RELAYD_STREAMS_SENT
:
3125 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
3126 ret
= relay_streams_sent(header
, conn
, payload
);
3128 case RELAYD_RESET_METADATA
:
3129 DBG_CMD("RELAYD_RESET_METADATA", conn
);
3130 ret
= relay_reset_metadata(header
, conn
, payload
);
3132 case RELAYD_ROTATE_STREAMS
:
3133 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
3134 ret
= relay_rotate_session_streams(header
, conn
, payload
);
3136 case RELAYD_CREATE_TRACE_CHUNK
:
3137 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
3138 ret
= relay_create_trace_chunk(header
, conn
, payload
);
3140 case RELAYD_CLOSE_TRACE_CHUNK
:
3141 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
3142 ret
= relay_close_trace_chunk(header
, conn
, payload
);
3144 case RELAYD_TRACE_CHUNK_EXISTS
:
3145 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
3146 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
3148 case RELAYD_GET_CONFIGURATION
:
3149 DBG_CMD("RELAYD_GET_CONFIGURATION", conn
);
3150 ret
= relay_get_configuration(header
, conn
, payload
);
3152 case RELAYD_UPDATE_SYNC_INFO
:
3154 ERR("Received unknown command (%u)", header
->cmd
);
3155 relay_unknown_command(conn
);
3164 static enum relay_connection_status
relay_process_control_receive_payload(
3165 struct relay_connection
*conn
)
3168 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3169 struct lttng_dynamic_buffer
*reception_buffer
=
3170 &conn
->protocol
.ctrl
.reception_buffer
;
3171 struct ctrl_connection_state_receive_payload
*state
=
3172 &conn
->protocol
.ctrl
.state
.receive_payload
;
3173 struct lttng_buffer_view payload_view
;
3175 if (state
->left_to_receive
== 0) {
3176 /* Short-circuit for payload-less commands. */
3177 goto reception_complete
;
3180 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3181 reception_buffer
->data
+ state
->received
,
3182 state
->left_to_receive
, MSG_DONTWAIT
);
3184 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3185 PERROR("Unable to receive command payload on sock %d",
3187 status
= RELAY_CONNECTION_STATUS_ERROR
;
3190 } else if (ret
== 0) {
3191 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3192 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3197 assert(ret
<= state
->left_to_receive
);
3199 state
->left_to_receive
-= ret
;
3200 state
->received
+= ret
;
3202 if (state
->left_to_receive
> 0) {
3204 * Can't transition to the protocol's next state, wait to
3205 * receive the rest of the header.
3207 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3208 state
->received
, state
->left_to_receive
,
3214 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3215 conn
->sock
->fd
, state
->received
);
3217 * The payload required to process the command has been received.
3218 * A view to the reception buffer is forwarded to the various
3219 * commands and the state of the control is reset on success.
3221 * Commands are responsible for sending their reply to the peer.
3223 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3225 ret
= relay_process_control_command(conn
,
3226 &state
->header
, &payload_view
);
3228 status
= RELAY_CONNECTION_STATUS_ERROR
;
3232 ret
= connection_reset_protocol_state(conn
);
3234 status
= RELAY_CONNECTION_STATUS_ERROR
;
3240 static enum relay_connection_status
relay_process_control_receive_header(
3241 struct relay_connection
*conn
)
3244 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3245 struct lttcomm_relayd_hdr header
;
3246 struct lttng_dynamic_buffer
*reception_buffer
=
3247 &conn
->protocol
.ctrl
.reception_buffer
;
3248 struct ctrl_connection_state_receive_header
*state
=
3249 &conn
->protocol
.ctrl
.state
.receive_header
;
3251 assert(state
->left_to_receive
!= 0);
3253 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3254 reception_buffer
->data
+ state
->received
,
3255 state
->left_to_receive
, MSG_DONTWAIT
);
3257 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3258 PERROR("Unable to receive control command header on sock %d",
3260 status
= RELAY_CONNECTION_STATUS_ERROR
;
3263 } else if (ret
== 0) {
3264 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3265 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3270 assert(ret
<= state
->left_to_receive
);
3272 state
->left_to_receive
-= ret
;
3273 state
->received
+= ret
;
3275 if (state
->left_to_receive
> 0) {
3277 * Can't transition to the protocol's next state, wait to
3278 * receive the rest of the header.
3280 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3281 state
->received
, state
->left_to_receive
,
3286 /* Transition to next state: receiving the command's payload. */
3287 conn
->protocol
.ctrl
.state_id
=
3288 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3289 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3290 header
.circuit_id
= be64toh(header
.circuit_id
);
3291 header
.data_size
= be64toh(header
.data_size
);
3292 header
.cmd
= be32toh(header
.cmd
);
3293 header
.cmd_version
= be32toh(header
.cmd_version
);
3294 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3295 &header
, sizeof(header
));
3297 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3298 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3301 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3302 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3304 status
= RELAY_CONNECTION_STATUS_ERROR
;
3308 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3310 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3311 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3314 status
= RELAY_CONNECTION_STATUS_ERROR
;
3318 if (header
.data_size
== 0) {
3320 * Manually invoke the next state as the poll loop
3321 * will not wake-up to allow us to proceed further.
3323 status
= relay_process_control_receive_payload(conn
);
3330 * Process the commands received on the control socket
3332 static enum relay_connection_status
relay_process_control(
3333 struct relay_connection
*conn
)
3335 enum relay_connection_status status
;
3337 switch (conn
->protocol
.ctrl
.state_id
) {
3338 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3339 status
= relay_process_control_receive_header(conn
);
3341 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3342 status
= relay_process_control_receive_payload(conn
);
3345 ERR("Unknown control connection protocol state encountered.");
3352 static enum relay_connection_status
relay_process_data_receive_header(
3353 struct relay_connection
*conn
)
3356 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3357 struct data_connection_state_receive_header
*state
=
3358 &conn
->protocol
.data
.state
.receive_header
;
3359 struct lttcomm_relayd_data_hdr header
;
3360 struct relay_stream
*stream
;
3362 assert(state
->left_to_receive
!= 0);
3364 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3365 state
->header_reception_buffer
+ state
->received
,
3366 state
->left_to_receive
, MSG_DONTWAIT
);
3368 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3369 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3370 status
= RELAY_CONNECTION_STATUS_ERROR
;
3373 } else if (ret
== 0) {
3374 /* Orderly shutdown. Not necessary to print an error. */
3375 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3376 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3381 assert(ret
<= state
->left_to_receive
);
3383 state
->left_to_receive
-= ret
;
3384 state
->received
+= ret
;
3386 if (state
->left_to_receive
> 0) {
3388 * Can't transition to the protocol's next state, wait to
3389 * receive the rest of the header.
3391 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3392 state
->received
, state
->left_to_receive
,
3397 /* Transition to next state: receiving the payload. */
3398 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3400 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3401 header
.circuit_id
= be64toh(header
.circuit_id
);
3402 header
.stream_id
= be64toh(header
.stream_id
);
3403 header
.data_size
= be32toh(header
.data_size
);
3404 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3405 header
.padding_size
= be32toh(header
.padding_size
);
3406 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3408 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3410 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3411 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3413 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3414 conn
->sock
->fd
, header
.circuit_id
,
3415 header
.stream_id
, header
.data_size
,
3416 header
.net_seq_num
, header
.padding_size
);
3418 stream
= stream_get_by_id(header
.stream_id
);
3420 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3422 /* Protocol error. */
3423 status
= RELAY_CONNECTION_STATUS_ERROR
;
3427 pthread_mutex_lock(&stream
->lock
);
3428 /* Prepare stream for the reception of a new packet. */
3429 ret
= stream_init_packet(stream
, header
.data_size
,
3430 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3431 pthread_mutex_unlock(&stream
->lock
);
3433 ERR("Failed to rotate stream output file");
3434 status
= RELAY_CONNECTION_STATUS_ERROR
;
3435 goto end_stream_unlock
;
3444 static enum relay_connection_status
relay_process_data_receive_payload(
3445 struct relay_connection
*conn
)
3448 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3449 struct relay_stream
*stream
;
3450 struct data_connection_state_receive_payload
*state
=
3451 &conn
->protocol
.data
.state
.receive_payload
;
3452 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3453 char data_buffer
[chunk_size
];
3454 bool partial_recv
= false;
3455 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3456 uint64_t left_to_receive
= state
->left_to_receive
;
3457 struct relay_session
*session
;
3459 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3460 state
->header
.stream_id
, state
->header
.net_seq_num
,
3461 state
->received
, left_to_receive
);
3463 stream
= stream_get_by_id(state
->header
.stream_id
);
3465 /* Protocol error. */
3466 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3467 state
->header
.stream_id
);
3468 status
= RELAY_CONNECTION_STATUS_ERROR
;
3472 pthread_mutex_lock(&stream
->lock
);
3473 session
= stream
->trace
->session
;
3474 if (!conn
->session
) {
3475 ret
= connection_set_session(conn
, session
);
3477 status
= RELAY_CONNECTION_STATUS_ERROR
;
3478 goto end_stream_unlock
;
3483 * The size of the "chunk" received on any iteration is bounded by:
3484 * - the data left to receive,
3485 * - the data immediately available on the socket,
3486 * - the on-stack data buffer
3488 while (left_to_receive
> 0 && !partial_recv
) {
3489 size_t recv_size
= min(left_to_receive
, chunk_size
);
3490 struct lttng_buffer_view packet_chunk
;
3492 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3493 recv_size
, MSG_DONTWAIT
);
3495 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3496 PERROR("Socket %d error", conn
->sock
->fd
);
3497 status
= RELAY_CONNECTION_STATUS_ERROR
;
3499 goto end_stream_unlock
;
3500 } else if (ret
== 0) {
3501 /* No more data ready to be consumed on socket. */
3502 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3503 state
->header
.stream_id
);
3504 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3506 } else if (ret
< (int) recv_size
) {
3508 * All the data available on the socket has been
3511 partial_recv
= true;
3515 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3517 assert(packet_chunk
.data
);
3519 ret
= stream_write(stream
, &packet_chunk
, 0);
3521 ERR("Relay error writing data to file");
3522 status
= RELAY_CONNECTION_STATUS_ERROR
;
3523 goto end_stream_unlock
;
3526 left_to_receive
-= recv_size
;
3527 state
->received
+= recv_size
;
3528 state
->left_to_receive
= left_to_receive
;
3531 if (state
->left_to_receive
> 0) {
3533 * Did not receive all the data expected, wait for more data to
3534 * become available on the socket.
3536 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3537 state
->header
.stream_id
, state
->received
,
3538 state
->left_to_receive
);
3539 goto end_stream_unlock
;
3542 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3544 status
= RELAY_CONNECTION_STATUS_ERROR
;
3545 goto end_stream_unlock
;
3548 if (session_streams_have_index(session
)) {
3549 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3550 state
->rotate_index
, &index_flushed
,
3551 state
->header
.data_size
+ state
->header
.padding_size
);
3553 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3554 stream
->stream_handle
,
3555 state
->header
.net_seq_num
, ret
);
3556 status
= RELAY_CONNECTION_STATUS_ERROR
;
3557 goto end_stream_unlock
;
3561 if (stream
->prev_data_seq
== -1ULL) {
3565 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3566 state
->header
.padding_size
, state
->header
.net_seq_num
,
3569 status
= RELAY_CONNECTION_STATUS_ERROR
;
3570 goto end_stream_unlock
;
3574 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3575 * contents of *state which are aliased (union) to the same location as
3576 * the new state. Don't use it beyond this point.
3578 connection_reset_protocol_state(conn
);
3582 close_requested
= stream
->close_requested
;
3583 pthread_mutex_unlock(&stream
->lock
);
3584 if (close_requested
&& left_to_receive
== 0) {
3585 try_stream_close(stream
);
3589 pthread_mutex_lock(&session
->lock
);
3590 uatomic_set(&session
->new_streams
, 1);
3591 pthread_mutex_unlock(&session
->lock
);
3600 * relay_process_data: Process the data received on the data socket
3602 static enum relay_connection_status
relay_process_data(
3603 struct relay_connection
*conn
)
3605 enum relay_connection_status status
;
3607 switch (conn
->protocol
.data
.state_id
) {
3608 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3609 status
= relay_process_data_receive_header(conn
);
3611 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3612 status
= relay_process_data_receive_payload(conn
);
3615 ERR("Unexpected data connection communication state.");
3622 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3626 (void) lttng_poll_del(events
, pollfd
);
3628 ret
= close(pollfd
);
3630 ERR("Closing pollfd %d", pollfd
);
3634 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3635 int pollfd
, struct relay_connection
*conn
)
3637 const char *type_str
;
3639 switch (conn
->type
) {