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 utils_close_pipe(health_quit_pipe
);
668 if (thread_quit_pipe
[0] != -1) {
669 (void) fd_tracker_util_pipe_close(
670 the_fd_tracker
, thread_quit_pipe
);
672 if (sessiond_trace_chunk_registry
) {
673 sessiond_trace_chunk_registry_destroy(
674 sessiond_trace_chunk_registry
);
676 if (the_fd_tracker
) {
677 fd_tracker_destroy(the_fd_tracker
);
680 uri_free(control_uri
);
682 /* Live URI is freed in the live thread. */
684 if (tracing_group_name_override
) {
685 free((void *) tracing_group_name
);
687 fd_tracker_log(the_fd_tracker
);
691 * Write to writable pipe used to notify a thread.
693 static int notify_thread_pipe(int wpipe
)
697 ret
= lttng_write(wpipe
, "!", 1);
699 PERROR("write poll pipe");
707 static int notify_health_quit_pipe(int *pipe
)
711 ret
= lttng_write(pipe
[1], "4", 1);
713 PERROR("write relay health quit");
722 * Stop all relayd and relayd-live threads.
724 int lttng_relay_stop_threads(void)
728 /* Stopping all threads */
729 DBG("Terminating all threads");
730 if (notify_thread_pipe(thread_quit_pipe
[1])) {
731 ERR("write error on thread quit pipe");
735 if (notify_health_quit_pipe(health_quit_pipe
)) {
736 ERR("write error on health quit pipe");
739 /* Dispatch thread */
740 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
741 futex_nto1_wake(&relay_conn_queue
.futex
);
743 if (relayd_live_stop()) {
744 ERR("Error stopping live threads");
751 * Signal handler for the daemon
753 * Simply stop all worker threads, leaving main() return gracefully after
754 * joining all threads and calling cleanup().
756 static void sighandler(int sig
)
760 DBG("SIGINT caught");
761 if (lttng_relay_stop_threads()) {
762 ERR("Error stopping threads");
766 DBG("SIGTERM caught");
767 if (lttng_relay_stop_threads()) {
768 ERR("Error stopping threads");
772 CMM_STORE_SHARED(recv_child_signal
, 1);
780 * Setup signal handler for :
781 * SIGINT, SIGTERM, SIGPIPE
783 static int set_signal_handler(void)
789 if ((ret
= sigemptyset(&sigset
)) < 0) {
790 PERROR("sigemptyset");
797 sa
.sa_handler
= sighandler
;
798 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
803 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
808 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
813 sa
.sa_handler
= SIG_IGN
;
814 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
819 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
824 void lttng_relay_notify_ready(void)
826 /* Notify the parent of the fork() process that we are ready. */
827 if (opt_daemon
|| opt_background
) {
828 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
829 kill(child_ppid
, SIGUSR1
);
835 * Init thread quit pipe.
837 * Return -1 on error or 0 if all pipes are created.
839 static int init_thread_quit_pipe(void)
841 return fd_tracker_util_pipe_open_cloexec(
842 the_fd_tracker
, "Quit pipe", thread_quit_pipe
);
846 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
848 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
852 if (events
== NULL
|| size
== 0) {
857 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
863 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
875 * Check if the thread quit pipe was triggered.
877 * Return 1 if it was triggered else 0;
879 static int check_thread_quit_pipe(int fd
, uint32_t events
)
881 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
889 * Create and init socket from uri.
891 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
894 struct lttcomm_sock
*sock
= NULL
;
896 sock
= lttcomm_alloc_sock_from_uri(uri
);
898 ERR("Allocating socket");
902 ret
= lttcomm_create_sock(sock
);
906 DBG("Listening on sock %d", sock
->fd
);
908 ret
= sock
->ops
->bind(sock
);
910 PERROR("Failed to bind socket");
914 ret
= sock
->ops
->listen(sock
, -1);
924 lttcomm_destroy_sock(sock
);
930 * This thread manages the listening for new connections on the network
932 static void *relay_thread_listener(void *data
)
934 int i
, ret
, pollfd
, err
= -1;
935 uint32_t revents
, nb_fd
;
936 struct lttng_poll_event events
;
937 struct lttcomm_sock
*control_sock
, *data_sock
;
939 DBG("[thread] Relay listener started");
941 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
943 health_code_update();
945 control_sock
= relay_socket_create(control_uri
);
947 goto error_sock_control
;
950 data_sock
= relay_socket_create(data_uri
);
952 goto error_sock_relay
;
956 * Pass 3 as size here for the thread quit pipe, control and
959 ret
= create_thread_poll_set(&events
, 3);
961 goto error_create_poll
;
964 /* Add the control socket */
965 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
970 /* Add the data socket */
971 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
976 lttng_relay_notify_ready();
978 if (testpoint(relayd_thread_listener
)) {
979 goto error_testpoint
;
983 health_code_update();
985 DBG("Listener accepting connections");
989 ret
= lttng_poll_wait(&events
, -1);
993 * Restart interrupted system call.
995 if (errno
== EINTR
) {
1003 DBG("Relay new connection received");
1004 for (i
= 0; i
< nb_fd
; i
++) {
1005 health_code_update();
1007 /* Fetch once the poll data */
1008 revents
= LTTNG_POLL_GETEV(&events
, i
);
1009 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1011 /* Thread quit pipe has been closed. Killing thread. */
1012 ret
= check_thread_quit_pipe(pollfd
, revents
);
1018 if (revents
& LPOLLIN
) {
1020 * A new connection is requested, therefore a
1021 * sessiond/consumerd connection is allocated in
1022 * this thread, enqueued to a global queue and
1023 * dequeued (and freed) in the worker thread.
1026 struct relay_connection
*new_conn
;
1027 struct lttcomm_sock
*newsock
;
1028 enum connection_type type
;
1030 if (pollfd
== data_sock
->fd
) {
1032 newsock
= data_sock
->ops
->accept(data_sock
);
1033 DBG("Relay data connection accepted, socket %d",
1036 assert(pollfd
== control_sock
->fd
);
1037 type
= RELAY_CONTROL
;
1038 newsock
= control_sock
->ops
->accept(control_sock
);
1039 DBG("Relay control connection accepted, socket %d",
1043 PERROR("accepting sock");
1047 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
1050 PERROR("setsockopt inet");
1051 lttcomm_destroy_sock(newsock
);
1055 ret
= socket_apply_keep_alive_config(newsock
->fd
);
1057 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1059 lttcomm_destroy_sock(newsock
);
1063 new_conn
= connection_create(newsock
, type
);
1065 lttcomm_destroy_sock(newsock
);
1069 /* Enqueue request for the dispatcher thread. */
1070 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
1074 * Wake the dispatch queue futex.
1075 * Implicit memory barrier with the
1076 * exchange in cds_wfcq_enqueue.
1078 futex_nto1_wake(&relay_conn_queue
.futex
);
1079 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1080 ERR("socket poll error");
1083 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1093 lttng_poll_clean(&events
);
1095 if (data_sock
->fd
>= 0) {
1096 ret
= data_sock
->ops
->close(data_sock
);
1101 lttcomm_destroy_sock(data_sock
);
1103 if (control_sock
->fd
>= 0) {
1104 ret
= control_sock
->ops
->close(control_sock
);
1109 lttcomm_destroy_sock(control_sock
);
1113 ERR("Health error occurred in %s", __func__
);
1115 health_unregister(health_relayd
);
1116 DBG("Relay listener thread cleanup complete");
1117 lttng_relay_stop_threads();
1122 * This thread manages the dispatching of the requests to worker threads
1124 static void *relay_thread_dispatcher(void *data
)
1128 struct cds_wfcq_node
*node
;
1129 struct relay_connection
*new_conn
= NULL
;
1131 DBG("[thread] Relay dispatcher started");
1133 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1135 if (testpoint(relayd_thread_dispatcher
)) {
1136 goto error_testpoint
;
1139 health_code_update();
1142 health_code_update();
1144 /* Atomically prepare the queue futex */
1145 futex_nto1_prepare(&relay_conn_queue
.futex
);
1147 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1152 health_code_update();
1154 /* Dequeue commands */
1155 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1156 &relay_conn_queue
.tail
);
1158 DBG("Woken up but nothing in the relay command queue");
1159 /* Continue thread execution */
1162 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1164 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1167 * Inform worker thread of the new request. This
1168 * call is blocking so we can be assured that
1169 * the data will be read at some point in time
1170 * or wait to the end of the world :)
1172 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1174 PERROR("write connection pipe");
1175 connection_put(new_conn
);
1178 } while (node
!= NULL
);
1180 /* Futex wait on queue. Blocking call on futex() */
1181 health_poll_entry();
1182 futex_nto1_wait(&relay_conn_queue
.futex
);
1186 /* Normal exit, no error */
1193 ERR("Health error occurred in %s", __func__
);
1195 health_unregister(health_relayd
);
1196 DBG("Dispatch thread dying");
1197 lttng_relay_stop_threads();
1201 static bool session_streams_have_index(const struct relay_session
*session
)
1203 return session
->minor
>= 4 && !session
->snapshot
;
1207 * Handle the RELAYD_CREATE_SESSION command.
1209 * On success, send back the session id or else return a negative value.
1211 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1212 struct relay_connection
*conn
,
1213 const struct lttng_buffer_view
*payload
)
1217 struct relay_session
*session
= NULL
;
1218 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1219 char session_name
[LTTNG_NAME_MAX
] = {};
1220 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1221 uint32_t live_timer
= 0;
1222 bool snapshot
= false;
1223 bool session_name_contains_creation_timestamp
= false;
1224 /* Left nil for peers < 2.11. */
1225 char base_path
[LTTNG_PATH_MAX
] = {};
1226 lttng_uuid sessiond_uuid
= {};
1227 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1228 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1229 LTTNG_OPTIONAL(time_t) creation_time
= {};
1230 struct lttng_dynamic_buffer reply_payload
;
1232 lttng_dynamic_buffer_init(&reply_payload
);
1234 if (conn
->minor
< 4) {
1235 /* From 2.1 to 2.3 */
1237 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1238 /* From 2.4 to 2.10 */
1239 ret
= cmd_create_session_2_4(payload
, session_name
,
1240 hostname
, &live_timer
, &snapshot
);
1242 bool has_current_chunk
;
1243 uint64_t current_chunk_id_value
;
1244 time_t creation_time_value
;
1245 uint64_t id_sessiond_value
;
1247 /* From 2.11 to ... */
1248 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1249 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1250 sessiond_uuid
, &has_current_chunk
,
1251 ¤t_chunk_id_value
, &creation_time_value
,
1252 &session_name_contains_creation_timestamp
);
1253 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1254 /* The nil UUID is reserved for pre-2.11 clients. */
1255 ERR("Illegal nil UUID announced by peer in create session command");
1259 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1260 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1261 if (has_current_chunk
) {
1262 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1263 current_chunk_id_value
);
1271 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1272 snapshot
, sessiond_uuid
,
1273 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1274 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1275 creation_time
.is_set
? &creation_time
.value
: NULL
,
1276 conn
->major
, conn
->minor
,
1277 session_name_contains_creation_timestamp
);
1282 assert(!conn
->session
);
1283 conn
->session
= session
;
1284 DBG("Created session %" PRIu64
, session
->id
);
1286 reply
.generic
.session_id
= htobe64(session
->id
);
1290 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1292 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1295 if (conn
->minor
< 11) {
1296 /* From 2.1 to 2.10 */
1297 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1298 &reply
.generic
, sizeof(reply
.generic
));
1300 ERR("Failed to append \"create session\" command reply header to payload buffer");
1305 const uint32_t output_path_length
=
1306 session
? strlen(session
->output_path
) + 1 : 0;
1308 reply
.output_path_length
= htobe32(output_path_length
);
1309 ret
= lttng_dynamic_buffer_append(
1310 &reply_payload
, &reply
, sizeof(reply
));
1312 ERR("Failed to append \"create session\" command reply header to payload buffer");
1316 if (output_path_length
) {
1317 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1318 session
->output_path
,
1319 output_path_length
);
1321 ERR("Failed to append \"create session\" command reply path to payload buffer");
1327 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1328 reply_payload
.size
, 0);
1329 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1330 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1331 reply_payload
.size
, send_ret
);
1335 if (ret
< 0 && session
) {
1336 session_put(session
);
1338 lttng_dynamic_buffer_reset(&reply_payload
);
1343 * When we have received all the streams and the metadata for a channel,
1344 * we make them visible to the viewer threads.
1346 static void publish_connection_local_streams(struct relay_connection
*conn
)
1348 struct relay_stream
*stream
;
1349 struct relay_session
*session
= conn
->session
;
1352 * We publish all streams belonging to a session atomically wrt
1355 pthread_mutex_lock(&session
->lock
);
1357 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1359 stream_publish(stream
);
1364 * Inform the viewer that there are new streams in the session.
1366 if (session
->viewer_attached
) {
1367 uatomic_set(&session
->new_streams
, 1);
1369 pthread_mutex_unlock(&session
->lock
);
1372 static int conform_channel_path(char *channel_path
)
1376 if (strstr("../", channel_path
)) {
1377 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1383 if (*channel_path
== '/') {
1384 const size_t len
= strlen(channel_path
);
1387 * Channel paths from peers prior to 2.11 are expressed as an
1388 * absolute path that is, in reality, relative to the relay
1389 * daemon's output directory. Remove the leading slash so it
1390 * is correctly interpreted as a relative path later on.
1392 * len (and not len - 1) is used to copy the trailing NULL.
1394 bcopy(channel_path
+ 1, channel_path
, len
);
1401 * relay_add_stream: allocate a new stream for a session
1403 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1404 struct relay_connection
*conn
,
1405 const struct lttng_buffer_view
*payload
)
1409 struct relay_session
*session
= conn
->session
;
1410 struct relay_stream
*stream
= NULL
;
1411 struct lttcomm_relayd_status_stream reply
;
1412 struct ctf_trace
*trace
= NULL
;
1413 uint64_t stream_handle
= -1ULL;
1414 char *path_name
= NULL
, *channel_name
= NULL
;
1415 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1416 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1418 if (!session
|| !conn
->version_check_done
) {
1419 ERR("Trying to add a stream before version check");
1421 goto end_no_session
;
1424 if (session
->minor
== 1) {
1426 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1428 } else if (session
->minor
> 1 && session
->minor
< 11) {
1429 /* From 2.2 to 2.10 */
1430 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1431 &channel_name
, &tracefile_size
, &tracefile_count
);
1433 /* From 2.11 to ... */
1434 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1435 &channel_name
, &tracefile_size
, &tracefile_count
,
1436 &stream_chunk_id
.value
);
1437 stream_chunk_id
.is_set
= true;
1444 if (conform_channel_path(path_name
)) {
1449 * Backward compatibility for --group-output-by-session.
1450 * Prior to lttng 2.11, the complete path is passed by the stream.
1451 * Starting at 2.11, lttng-relayd uses chunk. When dealing with producer
1452 * >=2.11 the chunk is responsible for the output path. When dealing
1453 * with producer < 2.11 the chunk output_path is the root output path
1454 * and the stream carries the complete path (path_name).
1455 * To support --group-output-by-session with older producer (<2.11), we
1456 * need to craft the path based on the stream path.
1458 if (opt_group_output_by
== RELAYD_GROUP_OUTPUT_BY_SESSION
) {
1459 if (conn
->minor
< 4) {
1461 * From 2.1 to 2.3, the session_name is not passed on
1462 * the RELAYD_CREATE_SESSION command. The session name
1463 * is necessary to detect the presence of a base_path
1464 * inside the stream path. Without it we cannot perform
1465 * a valid group-output-by-session transformation.
1467 WARN("Unable to perform a --group-by-session transformation for session %" PRIu64
1468 " for stream with path \"%s\" as it is produced by a peer using a protocol older than v2.4",
1469 session
->id
, path_name
);
1470 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1471 char *group_by_session_path_name
;
1473 assert(session
->session_name
[0] != '\0');
1475 group_by_session_path_name
=
1476 backward_compat_group_by_session(
1478 session
->session_name
);
1479 if (!group_by_session_path_name
) {
1480 ERR("Failed to apply group by session to stream of session %" PRIu64
,
1485 DBG("Transformed session path from \"%s\" to \"%s\" to honor per-session name grouping",
1486 path_name
, group_by_session_path_name
);
1489 path_name
= group_by_session_path_name
;
1493 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1498 /* This stream here has one reference on the trace. */
1499 pthread_mutex_lock(&last_relay_stream_id_lock
);
1500 stream_handle
= ++last_relay_stream_id
;
1501 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1503 /* We pass ownership of path_name and channel_name. */
1504 stream
= stream_create(trace
, stream_handle
, path_name
,
1505 channel_name
, tracefile_size
, tracefile_count
);
1507 channel_name
= NULL
;
1510 * Streams are the owners of their trace. Reference to trace is
1511 * kept within stream_create().
1513 ctf_trace_put(trace
);
1516 memset(&reply
, 0, sizeof(reply
));
1517 reply
.handle
= htobe64(stream_handle
);
1519 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1521 reply
.ret_code
= htobe32(LTTNG_OK
);
1524 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1525 sizeof(struct lttcomm_relayd_status_stream
), 0);
1526 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1527 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1539 * relay_close_stream: close a specific stream
1541 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1542 struct relay_connection
*conn
,
1543 const struct lttng_buffer_view
*payload
)
1547 struct relay_session
*session
= conn
->session
;
1548 struct lttcomm_relayd_close_stream stream_info
;
1549 struct lttcomm_relayd_generic_reply reply
;
1550 struct relay_stream
*stream
;
1552 DBG("Close stream received");
1554 if (!session
|| !conn
->version_check_done
) {
1555 ERR("Trying to close a stream before version check");
1557 goto end_no_session
;
1560 if (payload
->size
< sizeof(stream_info
)) {
1561 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1562 sizeof(stream_info
), payload
->size
);
1564 goto end_no_session
;
1566 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1567 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1568 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1570 stream
= stream_get_by_id(stream_info
.stream_id
);
1577 * Set last_net_seq_num before the close flag. Required by data
1580 pthread_mutex_lock(&stream
->lock
);
1581 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1582 pthread_mutex_unlock(&stream
->lock
);
1585 * This is one of the conditions which may trigger a stream close
1586 * with the others being:
1587 * 1) A close command is received for a stream
1588 * 2) The control connection owning the stream is closed
1589 * 3) We have received all of the stream's data _after_ a close
1592 try_stream_close(stream
);
1593 if (stream
->is_metadata
) {
1594 struct relay_viewer_stream
*vstream
;
1596 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1598 if (stream
->no_new_metadata_notified
) {
1600 * Since all the metadata has been sent to the
1601 * viewer and that we have a request to close
1602 * its stream, we can safely teardown the
1603 * corresponding metadata viewer stream.
1605 viewer_stream_put(vstream
);
1607 /* Put local reference. */
1608 viewer_stream_put(vstream
);
1615 memset(&reply
, 0, sizeof(reply
));
1617 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1619 reply
.ret_code
= htobe32(LTTNG_OK
);
1621 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1622 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1623 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1624 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1634 * relay_reset_metadata: reset a metadata stream
1637 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1638 struct relay_connection
*conn
,
1639 const struct lttng_buffer_view
*payload
)
1643 struct relay_session
*session
= conn
->session
;
1644 struct lttcomm_relayd_reset_metadata stream_info
;
1645 struct lttcomm_relayd_generic_reply reply
;
1646 struct relay_stream
*stream
;
1648 DBG("Reset metadata received");
1650 if (!session
|| !conn
->version_check_done
) {
1651 ERR("Trying to reset a metadata stream before version check");
1653 goto end_no_session
;
1656 if (payload
->size
< sizeof(stream_info
)) {
1657 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1658 sizeof(stream_info
), payload
->size
);
1660 goto end_no_session
;
1662 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1663 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1664 stream_info
.version
= be64toh(stream_info
.version
);
1666 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1668 /* Unsupported for live sessions for now. */
1669 if (session
->live_timer
!= 0) {
1674 stream
= stream_get_by_id(stream_info
.stream_id
);
1679 pthread_mutex_lock(&stream
->lock
);
1680 if (!stream
->is_metadata
) {
1685 ret
= stream_reset_file(stream
);
1687 ERR("Failed to reset metadata stream %" PRIu64
1688 ": stream_path = %s, channel = %s",
1689 stream
->stream_handle
, stream
->path_name
,
1690 stream
->channel_name
);
1694 pthread_mutex_unlock(&stream
->lock
);
1698 memset(&reply
, 0, sizeof(reply
));
1700 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1702 reply
.ret_code
= htobe32(LTTNG_OK
);
1704 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1705 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1706 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1707 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1717 * relay_unknown_command: send -1 if received unknown command
1719 static void relay_unknown_command(struct relay_connection
*conn
)
1721 struct lttcomm_relayd_generic_reply reply
;
1724 memset(&reply
, 0, sizeof(reply
));
1725 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1726 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1727 if (send_ret
< sizeof(reply
)) {
1728 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1733 * relay_start: send an acknowledgment to the client to tell if we are
1734 * ready to receive data. We are ready if a session is established.
1736 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1737 struct relay_connection
*conn
,
1738 const struct lttng_buffer_view
*payload
)
1742 struct lttcomm_relayd_generic_reply reply
;
1743 struct relay_session
*session
= conn
->session
;
1746 DBG("Trying to start the streaming without a session established");
1747 ret
= htobe32(LTTNG_ERR_UNK
);
1750 memset(&reply
, 0, sizeof(reply
));
1751 reply
.ret_code
= htobe32(LTTNG_OK
);
1752 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1754 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1755 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1764 * relay_recv_metadata: receive the metadata for the session.
1766 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1767 struct relay_connection
*conn
,
1768 const struct lttng_buffer_view
*payload
)
1771 struct relay_session
*session
= conn
->session
;
1772 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1773 struct relay_stream
*metadata_stream
;
1774 uint64_t metadata_payload_size
;
1775 struct lttng_buffer_view packet_view
;
1778 ERR("Metadata sent before version check");
1783 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1784 ERR("Incorrect data size");
1788 metadata_payload_size
= recv_hdr
->data_size
-
1789 sizeof(struct lttcomm_relayd_metadata_payload
);
1791 memcpy(&metadata_payload_header
, payload
->data
,
1792 sizeof(metadata_payload_header
));
1793 metadata_payload_header
.stream_id
= be64toh(
1794 metadata_payload_header
.stream_id
);
1795 metadata_payload_header
.padding_size
= be32toh(
1796 metadata_payload_header
.padding_size
);
1798 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1799 if (!metadata_stream
) {
1804 packet_view
= lttng_buffer_view_from_view(payload
,
1805 sizeof(metadata_payload_header
), metadata_payload_size
);
1806 if (!packet_view
.data
) {
1807 ERR("Invalid metadata packet length announced by header");
1812 pthread_mutex_lock(&metadata_stream
->lock
);
1813 ret
= stream_write(metadata_stream
, &packet_view
,
1814 metadata_payload_header
.padding_size
);
1815 pthread_mutex_unlock(&metadata_stream
->lock
);
1821 stream_put(metadata_stream
);
1827 * relay_send_version: send relayd version number
1829 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1830 struct relay_connection
*conn
,
1831 const struct lttng_buffer_view
*payload
)
1835 struct lttcomm_relayd_version reply
, msg
;
1836 bool compatible
= true;
1838 conn
->version_check_done
= true;
1840 /* Get version from the other side. */
1841 if (payload
->size
< sizeof(msg
)) {
1842 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1843 sizeof(msg
), payload
->size
);
1848 memcpy(&msg
, payload
->data
, sizeof(msg
));
1849 msg
.major
= be32toh(msg
.major
);
1850 msg
.minor
= be32toh(msg
.minor
);
1852 memset(&reply
, 0, sizeof(reply
));
1853 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1854 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1856 /* Major versions must be the same */
1857 if (reply
.major
!= msg
.major
) {
1858 DBG("Incompatible major versions (%u vs %u), deleting session",
1859 reply
.major
, msg
.major
);
1863 conn
->major
= reply
.major
;
1864 /* We adapt to the lowest compatible version */
1865 if (reply
.minor
<= msg
.minor
) {
1866 conn
->minor
= reply
.minor
;
1868 conn
->minor
= msg
.minor
;
1871 reply
.major
= htobe32(reply
.major
);
1872 reply
.minor
= htobe32(reply
.minor
);
1873 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1875 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1876 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1889 DBG("Version check done using protocol %u.%u", conn
->major
,
1897 * Check for data pending for a given stream id from the session daemon.
1899 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1900 struct relay_connection
*conn
,
1901 const struct lttng_buffer_view
*payload
)
1903 struct relay_session
*session
= conn
->session
;
1904 struct lttcomm_relayd_data_pending msg
;
1905 struct lttcomm_relayd_generic_reply reply
;
1906 struct relay_stream
*stream
;
1909 uint64_t stream_seq
;
1911 DBG("Data pending command received");
1913 if (!session
|| !conn
->version_check_done
) {
1914 ERR("Trying to check for data before version check");
1916 goto end_no_session
;
1919 if (payload
->size
< sizeof(msg
)) {
1920 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1921 sizeof(msg
), payload
->size
);
1923 goto end_no_session
;
1925 memcpy(&msg
, payload
->data
, sizeof(msg
));
1926 msg
.stream_id
= be64toh(msg
.stream_id
);
1927 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1929 stream
= stream_get_by_id(msg
.stream_id
);
1930 if (stream
== NULL
) {
1935 pthread_mutex_lock(&stream
->lock
);
1937 if (session_streams_have_index(session
)) {
1939 * Ensure that both the index and stream data have been
1940 * flushed up to the requested point.
1942 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1944 stream_seq
= stream
->prev_data_seq
;
1946 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
1947 ", prev_index_seq %" PRIu64
1948 ", and last_seq %" PRIu64
, msg
.stream_id
,
1949 stream
->prev_data_seq
, stream
->prev_index_seq
,
1950 msg
.last_net_seq_num
);
1952 /* Avoid wrapping issue */
1953 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
1954 /* Data has in fact been written and is NOT pending */
1957 /* Data still being streamed thus pending */
1961 stream
->data_pending_check_done
= true;
1962 pthread_mutex_unlock(&stream
->lock
);
1967 memset(&reply
, 0, sizeof(reply
));
1968 reply
.ret_code
= htobe32(ret
);
1969 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1970 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1971 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1981 * Wait for the control socket to reach a quiescent state.
1983 * Note that for now, when receiving this command from the session
1984 * daemon, this means that every subsequent commands or data received on
1985 * the control socket has been handled. So, this is why we simply return
1988 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
1989 struct relay_connection
*conn
,
1990 const struct lttng_buffer_view
*payload
)
1994 struct relay_stream
*stream
;
1995 struct lttcomm_relayd_quiescent_control msg
;
1996 struct lttcomm_relayd_generic_reply reply
;
1998 DBG("Checking quiescent state on control socket");
2000 if (!conn
->session
|| !conn
->version_check_done
) {
2001 ERR("Trying to check for data before version check");
2003 goto end_no_session
;
2006 if (payload
->size
< sizeof(msg
)) {
2007 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2008 sizeof(msg
), payload
->size
);
2010 goto end_no_session
;
2012 memcpy(&msg
, payload
->data
, sizeof(msg
));
2013 msg
.stream_id
= be64toh(msg
.stream_id
);
2015 stream
= stream_get_by_id(msg
.stream_id
);
2019 pthread_mutex_lock(&stream
->lock
);
2020 stream
->data_pending_check_done
= true;
2021 pthread_mutex_unlock(&stream
->lock
);
2023 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2026 memset(&reply
, 0, sizeof(reply
));
2027 reply
.ret_code
= htobe32(LTTNG_OK
);
2028 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2029 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2030 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2042 * Initialize a data pending command. This means that a consumer is about
2043 * to ask for data pending for each stream it holds. Simply iterate over
2044 * all streams of a session and set the data_pending_check_done flag.
2046 * This command returns to the client a LTTNG_OK code.
2048 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2049 struct relay_connection
*conn
,
2050 const struct lttng_buffer_view
*payload
)
2054 struct lttng_ht_iter iter
;
2055 struct lttcomm_relayd_begin_data_pending msg
;
2056 struct lttcomm_relayd_generic_reply reply
;
2057 struct relay_stream
*stream
;
2062 DBG("Init streams for data pending");
2064 if (!conn
->session
|| !conn
->version_check_done
) {
2065 ERR("Trying to check for data before version check");
2067 goto end_no_session
;
2070 if (payload
->size
< sizeof(msg
)) {
2071 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2072 sizeof(msg
), payload
->size
);
2074 goto end_no_session
;
2076 memcpy(&msg
, payload
->data
, sizeof(msg
));
2077 msg
.session_id
= be64toh(msg
.session_id
);
2080 * Iterate over all streams to set the begin data pending flag.
2081 * For now, the streams are indexed by stream handle so we have
2082 * to iterate over all streams to find the one associated with
2083 * the right session_id.
2086 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2088 if (!stream_get(stream
)) {
2091 if (stream
->trace
->session
->id
== msg
.session_id
) {
2092 pthread_mutex_lock(&stream
->lock
);
2093 stream
->data_pending_check_done
= false;
2094 pthread_mutex_unlock(&stream
->lock
);
2095 DBG("Set begin data pending flag to stream %" PRIu64
,
2096 stream
->stream_handle
);
2102 memset(&reply
, 0, sizeof(reply
));
2103 /* All good, send back reply. */
2104 reply
.ret_code
= htobe32(LTTNG_OK
);
2106 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2107 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2108 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2120 * End data pending command. This will check, for a given session id, if
2121 * each stream associated with it has its data_pending_check_done flag
2122 * set. If not, this means that the client lost track of the stream but
2123 * the data is still being streamed on our side. In this case, we inform
2124 * the client that data is in flight.
2126 * Return to the client if there is data in flight or not with a ret_code.
2128 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2129 struct relay_connection
*conn
,
2130 const struct lttng_buffer_view
*payload
)
2134 struct lttng_ht_iter iter
;
2135 struct lttcomm_relayd_end_data_pending msg
;
2136 struct lttcomm_relayd_generic_reply reply
;
2137 struct relay_stream
*stream
;
2138 uint32_t is_data_inflight
= 0;
2140 DBG("End data pending command");
2142 if (!conn
->session
|| !conn
->version_check_done
) {
2143 ERR("Trying to check for data before version check");
2145 goto end_no_session
;
2148 if (payload
->size
< sizeof(msg
)) {
2149 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2150 sizeof(msg
), payload
->size
);
2152 goto end_no_session
;
2154 memcpy(&msg
, payload
->data
, sizeof(msg
));
2155 msg
.session_id
= be64toh(msg
.session_id
);
2158 * Iterate over all streams to see if the begin data pending
2162 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2164 if (!stream_get(stream
)) {
2167 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2171 pthread_mutex_lock(&stream
->lock
);
2172 if (!stream
->data_pending_check_done
) {
2173 uint64_t stream_seq
;
2175 if (session_streams_have_index(conn
->session
)) {
2177 * Ensure that both the index and stream data have been
2178 * flushed up to the requested point.
2180 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
2182 stream_seq
= stream
->prev_data_seq
;
2184 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2185 is_data_inflight
= 1;
2186 DBG("Data is still in flight for stream %" PRIu64
,
2187 stream
->stream_handle
);
2188 pthread_mutex_unlock(&stream
->lock
);
2193 pthread_mutex_unlock(&stream
->lock
);
2198 memset(&reply
, 0, sizeof(reply
));
2199 /* All good, send back reply. */
2200 reply
.ret_code
= htobe32(is_data_inflight
);
2202 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2203 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2204 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2216 * Receive an index for a specific stream.
2218 * Return 0 on success else a negative value.
2220 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2221 struct relay_connection
*conn
,
2222 const struct lttng_buffer_view
*payload
)
2226 struct relay_session
*session
= conn
->session
;
2227 struct lttcomm_relayd_index index_info
;
2228 struct lttcomm_relayd_generic_reply reply
;
2229 struct relay_stream
*stream
;
2234 DBG("Relay receiving index");
2236 if (!session
|| !conn
->version_check_done
) {
2237 ERR("Trying to close a stream before version check");
2239 goto end_no_session
;
2242 msg_len
= lttcomm_relayd_index_len(
2243 lttng_to_index_major(conn
->major
, conn
->minor
),
2244 lttng_to_index_minor(conn
->major
, conn
->minor
));
2245 if (payload
->size
< msg_len
) {
2246 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2247 msg_len
, payload
->size
);
2249 goto end_no_session
;
2251 memcpy(&index_info
, payload
->data
, msg_len
);
2252 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2253 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2254 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2255 index_info
.content_size
= be64toh(index_info
.content_size
);
2256 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2257 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2258 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2259 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2261 if (conn
->minor
>= 8) {
2262 index_info
.stream_instance_id
=
2263 be64toh(index_info
.stream_instance_id
);
2264 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2266 index_info
.stream_instance_id
= -1ULL;
2267 index_info
.packet_seq_num
= -1ULL;
2270 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2272 ERR("stream_get_by_id not found");
2277 pthread_mutex_lock(&stream
->lock
);
2278 ret
= stream_add_index(stream
, &index_info
);
2279 pthread_mutex_unlock(&stream
->lock
);
2281 goto end_stream_put
;
2287 memset(&reply
, 0, sizeof(reply
));
2289 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2291 reply
.ret_code
= htobe32(LTTNG_OK
);
2293 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2294 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2295 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2304 * Receive the streams_sent message.
2306 * Return 0 on success else a negative value.
2308 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2309 struct relay_connection
*conn
,
2310 const struct lttng_buffer_view
*payload
)
2314 struct lttcomm_relayd_generic_reply reply
;
2318 DBG("Relay receiving streams_sent");
2320 if (!conn
->session
|| !conn
->version_check_done
) {
2321 ERR("Trying to close a stream before version check");
2323 goto end_no_session
;
2327 * Publish every pending stream in the connection recv list which are
2328 * now ready to be used by the viewer.
2330 publish_connection_local_streams(conn
);
2332 memset(&reply
, 0, sizeof(reply
));
2333 reply
.ret_code
= htobe32(LTTNG_OK
);
2334 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2335 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2336 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2349 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2350 * session rotation feature (not the tracefile rotation feature).
2352 static int relay_rotate_session_streams(
2353 const struct lttcomm_relayd_hdr
*recv_hdr
,
2354 struct relay_connection
*conn
,
2355 const struct lttng_buffer_view
*payload
)
2360 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2361 struct relay_session
*session
= conn
->session
;
2362 struct lttcomm_relayd_rotate_streams rotate_streams
;
2363 struct lttcomm_relayd_generic_reply reply
= {};
2364 struct relay_stream
*stream
= NULL
;
2365 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2366 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2367 struct lttng_buffer_view stream_positions
;
2368 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2369 const char *chunk_id_str
= "none";
2371 if (!session
|| !conn
->version_check_done
) {
2372 ERR("Trying to rotate a stream before version check");
2377 if (session
->major
== 2 && session
->minor
< 11) {
2378 ERR("Unsupported feature before 2.11");
2383 if (payload
->size
< header_len
) {
2384 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2385 header_len
, payload
->size
);
2390 memcpy(&rotate_streams
, payload
->data
, header_len
);
2392 /* Convert header to host endianness. */
2393 rotate_streams
= (typeof(rotate_streams
)) {
2394 .stream_count
= be32toh(rotate_streams
.stream_count
),
2395 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2396 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2397 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2401 if (rotate_streams
.new_chunk_id
.is_set
) {
2403 * Retrieve the trace chunk the stream must transition to. As
2404 * per the protocol, this chunk should have been created
2405 * before this command is received.
2407 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2408 sessiond_trace_chunk_registry
,
2409 session
->sessiond_uuid
, session
->id
,
2410 rotate_streams
.new_chunk_id
.value
);
2411 if (!next_trace_chunk
) {
2412 char uuid_str
[LTTNG_UUID_STR_LEN
];
2414 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2415 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2416 ", trace_chunk_id = %" PRIu64
,
2417 uuid_str
, session
->id
,
2418 rotate_streams
.new_chunk_id
.value
);
2419 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2424 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2425 rotate_streams
.new_chunk_id
.value
);
2426 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2427 chunk_id_str
= "formatting error";
2429 chunk_id_str
= chunk_id_buf
;
2433 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2434 rotate_streams
.stream_count
, session
->session_name
,
2437 stream_positions
= lttng_buffer_view_from_view(payload
,
2438 sizeof(rotate_streams
), -1);
2439 if (!stream_positions
.data
||
2440 stream_positions
.size
<
2441 (rotate_streams
.stream_count
*
2442 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2443 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2448 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2449 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2450 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2451 const struct lttcomm_relayd_stream_rotation_position pos
= {
2452 .stream_id
= be64toh(position_comm
->stream_id
),
2453 .rotate_at_seq_num
= be64toh(
2454 position_comm
->rotate_at_seq_num
),
2457 stream
= stream_get_by_id(pos
.stream_id
);
2459 reply_code
= LTTNG_ERR_INVALID
;
2464 pthread_mutex_lock(&stream
->lock
);
2465 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2466 pos
.rotate_at_seq_num
);
2467 pthread_mutex_unlock(&stream
->lock
);
2469 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2477 reply_code
= LTTNG_OK
;
2484 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2485 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2486 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2487 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2488 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2493 lttng_trace_chunk_put(next_trace_chunk
);
2500 * relay_create_trace_chunk: create a new trace chunk
2502 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2503 struct relay_connection
*conn
,
2504 const struct lttng_buffer_view
*payload
)
2508 struct relay_session
*session
= conn
->session
;
2509 struct lttcomm_relayd_create_trace_chunk
*msg
;
2510 struct lttcomm_relayd_generic_reply reply
= {};
2511 struct lttng_buffer_view header_view
;
2512 struct lttng_buffer_view chunk_name_view
;
2513 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2514 enum lttng_error_code reply_code
= LTTNG_OK
;
2515 enum lttng_trace_chunk_status chunk_status
;
2516 struct lttng_directory_handle
*session_output
= NULL
;
2517 const char *new_path
;
2519 if (!session
|| !conn
->version_check_done
) {
2520 ERR("Trying to create a trace chunk before version check");
2525 if (session
->major
== 2 && session
->minor
< 11) {
2526 ERR("Chunk creation command is unsupported before 2.11");
2531 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2532 if (!header_view
.data
) {
2533 ERR("Failed to receive payload of chunk creation command");
2538 /* Convert to host endianness. */
2539 msg
= (typeof(msg
)) header_view
.data
;
2540 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2541 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2542 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2544 if (session
->current_trace_chunk
&&
2545 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2546 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2547 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
2548 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2549 ERR("Failed to rename old chunk");
2551 reply_code
= LTTNG_ERR_UNK
;
2555 session
->ongoing_rotation
= true;
2556 if (!session
->current_trace_chunk
) {
2557 if (!session
->has_rotated
) {
2563 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
2565 chunk
= lttng_trace_chunk_create(
2566 msg
->chunk_id
, msg
->creation_timestamp
, new_path
);
2568 ERR("Failed to create trace chunk in trace chunk creation command");
2570 reply_code
= LTTNG_ERR_NOMEM
;
2574 if (msg
->override_name_length
) {
2577 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2579 msg
->override_name_length
);
2580 name
= chunk_name_view
.data
;
2581 if (!name
|| name
[msg
->override_name_length
- 1]) {
2582 ERR("Failed to receive payload of chunk creation command");
2584 reply_code
= LTTNG_ERR_INVALID
;
2588 chunk_status
= lttng_trace_chunk_override_name(
2589 chunk
, chunk_name_view
.data
);
2590 switch (chunk_status
) {
2591 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2593 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2594 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2595 reply_code
= LTTNG_ERR_INVALID
;
2599 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2600 reply_code
= LTTNG_ERR_UNK
;
2606 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2607 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2608 reply_code
= LTTNG_ERR_UNK
;
2613 session_output
= session_create_output_directory_handle(
2615 if (!session_output
) {
2616 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2619 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, session_output
);
2620 lttng_directory_handle_put(session_output
);
2621 session_output
= NULL
;
2622 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2623 reply_code
= LTTNG_ERR_UNK
;
2628 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2629 sessiond_trace_chunk_registry
,
2630 conn
->session
->sessiond_uuid
,
2633 if (!published_chunk
) {
2634 char uuid_str
[LTTNG_UUID_STR_LEN
];
2636 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2637 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2642 reply_code
= LTTNG_ERR_NOMEM
;
2646 pthread_mutex_lock(&conn
->session
->lock
);
2647 if (conn
->session
->pending_closure_trace_chunk
) {
2649 * Invalid; this means a second create_trace_chunk command was
2650 * received before a close_trace_chunk.
2652 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2653 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2655 goto end_unlock_session
;
2657 conn
->session
->pending_closure_trace_chunk
=
2658 conn
->session
->current_trace_chunk
;
2659 conn
->session
->current_trace_chunk
= published_chunk
;
2660 published_chunk
= NULL
;
2661 if (!conn
->session
->pending_closure_trace_chunk
) {
2662 session
->ongoing_rotation
= false;
2665 pthread_mutex_unlock(&conn
->session
->lock
);
2667 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2668 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2670 sizeof(struct lttcomm_relayd_generic_reply
),
2672 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2673 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2678 lttng_trace_chunk_put(chunk
);
2679 lttng_trace_chunk_put(published_chunk
);
2680 lttng_directory_handle_put(session_output
);
2685 * relay_close_trace_chunk: close a trace chunk
2687 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2688 struct relay_connection
*conn
,
2689 const struct lttng_buffer_view
*payload
)
2691 int ret
= 0, buf_ret
;
2693 struct relay_session
*session
= conn
->session
;
2694 struct lttcomm_relayd_close_trace_chunk
*msg
;
2695 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2696 struct lttng_buffer_view header_view
;
2697 struct lttng_trace_chunk
*chunk
= NULL
;
2698 enum lttng_error_code reply_code
= LTTNG_OK
;
2699 enum lttng_trace_chunk_status chunk_status
;
2701 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2702 time_t close_timestamp
;
2703 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2704 size_t path_length
= 0;
2705 const char *chunk_name
= NULL
;
2706 struct lttng_dynamic_buffer reply_payload
;
2707 const char *new_path
;
2709 lttng_dynamic_buffer_init(&reply_payload
);
2711 if (!session
|| !conn
->version_check_done
) {
2712 ERR("Trying to close a trace chunk before version check");
2717 if (session
->major
== 2 && session
->minor
< 11) {
2718 ERR("Chunk close command is unsupported before 2.11");
2723 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2724 if (!header_view
.data
) {
2725 ERR("Failed to receive payload of chunk close command");
2730 /* Convert to host endianness. */
2731 msg
= (typeof(msg
)) header_view
.data
;
2732 chunk_id
= be64toh(msg
->chunk_id
);
2733 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2734 close_command
= (typeof(close_command
)){
2735 .value
= be32toh(msg
->close_command
.value
),
2736 .is_set
= msg
->close_command
.is_set
,
2739 chunk
= sessiond_trace_chunk_registry_get_chunk(
2740 sessiond_trace_chunk_registry
,
2741 conn
->session
->sessiond_uuid
,
2745 char uuid_str
[LTTNG_UUID_STR_LEN
];
2747 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2748 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2753 reply_code
= LTTNG_ERR_NOMEM
;
2757 pthread_mutex_lock(&session
->lock
);
2758 if (close_command
.is_set
&&
2759 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
) {
2761 * Clear command. It is a protocol error to ask for a
2762 * clear on a relay which does not allow it. Querying
2763 * the configuration allows figuring out whether
2764 * clearing is allowed before doing the clear.
2766 if (!opt_allow_clear
) {
2768 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2769 goto end_unlock_session
;
2772 if (session
->pending_closure_trace_chunk
&&
2773 session
->pending_closure_trace_chunk
!= chunk
) {
2774 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2775 session
->session_name
);
2776 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2778 goto end_unlock_session
;
2781 if (session
->current_trace_chunk
&& session
->current_trace_chunk
!= chunk
&&
2782 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
2783 if (close_command
.is_set
&&
2784 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&&
2785 !session
->has_rotated
) {
2786 /* New chunk stays in session output directory. */
2789 /* Use chunk name for new chunk. */
2792 /* Rename new chunk path. */
2793 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
2795 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2799 session
->ongoing_rotation
= false;
2801 if ((!close_command
.is_set
||
2802 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) &&
2803 !lttng_trace_chunk_get_name_overridden(chunk
)) {
2804 const char *old_path
;
2806 if (!session
->has_rotated
) {
2811 /* We need to move back the .tmp_old_chunk to its rightful place. */
2812 chunk_status
= lttng_trace_chunk_rename_path(chunk
, old_path
);
2813 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2818 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2819 chunk
, close_timestamp
);
2820 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2821 ERR("Failed to set trace chunk close timestamp");
2823 reply_code
= LTTNG_ERR_UNK
;
2824 goto end_unlock_session
;
2827 if (close_command
.is_set
) {
2828 chunk_status
= lttng_trace_chunk_set_close_command(
2829 chunk
, close_command
.value
);
2830 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2832 reply_code
= LTTNG_ERR_INVALID
;
2833 goto end_unlock_session
;
2836 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2837 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2838 ERR("Failed to get chunk name");
2840 reply_code
= LTTNG_ERR_UNK
;
2841 goto end_unlock_session
;
2843 if (!session
->has_rotated
&& !session
->snapshot
) {
2844 ret
= lttng_strncpy(closed_trace_chunk_path
,
2845 session
->output_path
,
2846 sizeof(closed_trace_chunk_path
));
2848 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2849 strlen(session
->output_path
),
2850 sizeof(closed_trace_chunk_path
));
2851 reply_code
= LTTNG_ERR_NOMEM
;
2853 goto end_unlock_session
;
2856 if (session
->snapshot
) {
2857 ret
= snprintf(closed_trace_chunk_path
,
2858 sizeof(closed_trace_chunk_path
),
2859 "%s/%s", session
->output_path
,
2862 ret
= snprintf(closed_trace_chunk_path
,
2863 sizeof(closed_trace_chunk_path
),
2864 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2866 session
->output_path
, chunk_name
);
2868 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2869 ERR("Failed to format closed trace chunk resulting path");
2870 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2872 goto end_unlock_session
;
2875 if (close_command
.is_set
&&
2876 close_command
.value
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
2877 session
->has_rotated
= true;
2879 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2880 path_length
= strlen(closed_trace_chunk_path
) + 1;
2881 if (path_length
> UINT32_MAX
) {
2882 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2884 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2885 goto end_unlock_session
;
2888 if (session
->current_trace_chunk
== chunk
) {
2890 * After a trace chunk close command, no new streams
2891 * referencing the chunk may be created. Hence, on the
2892 * event that no new trace chunk have been created for
2893 * the session, the reference to the current trace chunk
2894 * is released in order to allow it to be reclaimed when
2895 * the last stream releases its reference to it.
2897 lttng_trace_chunk_put(session
->current_trace_chunk
);
2898 session
->current_trace_chunk
= NULL
;
2900 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2901 session
->pending_closure_trace_chunk
= NULL
;
2903 pthread_mutex_unlock(&session
->lock
);
2906 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
2907 reply
.path_length
= htobe32((uint32_t) path_length
);
2908 buf_ret
= lttng_dynamic_buffer_append(
2909 &reply_payload
, &reply
, sizeof(reply
));
2911 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2915 if (reply_code
== LTTNG_OK
) {
2916 buf_ret
= lttng_dynamic_buffer_append(&reply_payload
,
2917 closed_trace_chunk_path
, path_length
);
2919 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2924 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2928 if (send_ret
< reply_payload
.size
) {
2929 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2930 reply_payload
.size
, send_ret
);
2935 lttng_trace_chunk_put(chunk
);
2936 lttng_dynamic_buffer_reset(&reply_payload
);
2941 * relay_trace_chunk_exists: check if a trace chunk exists
2943 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2944 struct relay_connection
*conn
,
2945 const struct lttng_buffer_view
*payload
)
2949 struct relay_session
*session
= conn
->session
;
2950 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2951 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2952 struct lttng_buffer_view header_view
;
2956 if (!session
|| !conn
->version_check_done
) {
2957 ERR("Trying to close a trace chunk before version check");
2962 if (session
->major
== 2 && session
->minor
< 11) {
2963 ERR("Chunk close command is unsupported before 2.11");
2968 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2969 if (!header_view
.data
) {
2970 ERR("Failed to receive payload of chunk close command");
2975 /* Convert to host endianness. */
2976 msg
= (typeof(msg
)) header_view
.data
;
2977 chunk_id
= be64toh(msg
->chunk_id
);
2979 ret
= sessiond_trace_chunk_registry_chunk_exists(
2980 sessiond_trace_chunk_registry
,
2981 conn
->session
->sessiond_uuid
,
2983 chunk_id
, &chunk_exists
);
2985 * If ret is not 0, send the reply and report the error to the caller.
2986 * It is a protocol (or internal) error and the session/connection
2987 * should be torn down.
2989 reply
= (typeof(reply
)){
2990 .generic
.ret_code
= htobe32((uint32_t)
2991 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
2992 .trace_chunk_exists
= ret
== 0 ? chunk_exists
: 0,
2994 send_ret
= conn
->sock
->ops
->sendmsg(
2995 conn
->sock
, &reply
, sizeof(reply
), 0);
2996 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2997 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
3006 * relay_get_configuration: query whether feature is available
3008 static int relay_get_configuration(const struct lttcomm_relayd_hdr
*recv_hdr
,
3009 struct relay_connection
*conn
,
3010 const struct lttng_buffer_view
*payload
)
3014 struct lttcomm_relayd_get_configuration
*msg
;
3015 struct lttcomm_relayd_get_configuration_reply reply
= {};
3016 struct lttng_buffer_view header_view
;
3017 uint64_t query_flags
= 0;
3018 uint64_t result_flags
= 0;
3020 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
3021 if (!header_view
.data
) {
3022 ERR("Failed to receive payload of chunk close command");
3027 /* Convert to host endianness. */
3028 msg
= (typeof(msg
)) header_view
.data
;
3029 query_flags
= be64toh(msg
->query_flags
);
3032 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
3035 if (opt_allow_clear
) {
3036 result_flags
|= LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
;
3040 reply
= (typeof(reply
)){
3041 .generic
.ret_code
= htobe32((uint32_t)
3042 (ret
== 0 ? LTTNG_OK
: LTTNG_ERR_INVALID_PROTOCOL
)),
3043 .relayd_configuration_flags
= htobe64(result_flags
),
3045 send_ret
= conn
->sock
->ops
->sendmsg(
3046 conn
->sock
, &reply
, sizeof(reply
), 0);
3047 if (send_ret
< (ssize_t
) sizeof(reply
)) {
3048 ERR("Failed to send \"get configuration\" command reply (ret = %zd)",
3056 #define DBG_CMD(cmd_name, conn) \
3057 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3059 static int relay_process_control_command(struct relay_connection
*conn
,
3060 const struct lttcomm_relayd_hdr
*header
,
3061 const struct lttng_buffer_view
*payload
)
3065 switch (header
->cmd
) {
3066 case RELAYD_CREATE_SESSION
:
3067 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
3068 ret
= relay_create_session(header
, conn
, payload
);
3070 case RELAYD_ADD_STREAM
:
3071 DBG_CMD("RELAYD_ADD_STREAM", conn
);
3072 ret
= relay_add_stream(header
, conn
, payload
);
3074 case RELAYD_START_DATA
:
3075 DBG_CMD("RELAYD_START_DATA", conn
);
3076 ret
= relay_start(header
, conn
, payload
);
3078 case RELAYD_SEND_METADATA
:
3079 DBG_CMD("RELAYD_SEND_METADATA", conn
);
3080 ret
= relay_recv_metadata(header
, conn
, payload
);
3082 case RELAYD_VERSION
:
3083 DBG_CMD("RELAYD_VERSION", conn
);
3084 ret
= relay_send_version(header
, conn
, payload
);
3086 case RELAYD_CLOSE_STREAM
:
3087 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
3088 ret
= relay_close_stream(header
, conn
, payload
);
3090 case RELAYD_DATA_PENDING
:
3091 DBG_CMD("RELAYD_DATA_PENDING", conn
);
3092 ret
= relay_data_pending(header
, conn
, payload
);
3094 case RELAYD_QUIESCENT_CONTROL
:
3095 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
3096 ret
= relay_quiescent_control(header
, conn
, payload
);
3098 case RELAYD_BEGIN_DATA_PENDING
:
3099 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
3100 ret
= relay_begin_data_pending(header
, conn
, payload
);
3102 case RELAYD_END_DATA_PENDING
:
3103 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
3104 ret
= relay_end_data_pending(header
, conn
, payload
);
3106 case RELAYD_SEND_INDEX
:
3107 DBG_CMD("RELAYD_SEND_INDEX", conn
);
3108 ret
= relay_recv_index(header
, conn
, payload
);
3110 case RELAYD_STREAMS_SENT
:
3111 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
3112 ret
= relay_streams_sent(header
, conn
, payload
);
3114 case RELAYD_RESET_METADATA
:
3115 DBG_CMD("RELAYD_RESET_METADATA", conn
);
3116 ret
= relay_reset_metadata(header
, conn
, payload
);
3118 case RELAYD_ROTATE_STREAMS
:
3119 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
3120 ret
= relay_rotate_session_streams(header
, conn
, payload
);
3122 case RELAYD_CREATE_TRACE_CHUNK
:
3123 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
3124 ret
= relay_create_trace_chunk(header
, conn
, payload
);
3126 case RELAYD_CLOSE_TRACE_CHUNK
:
3127 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
3128 ret
= relay_close_trace_chunk(header
, conn
, payload
);
3130 case RELAYD_TRACE_CHUNK_EXISTS
:
3131 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
3132 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
3134 case RELAYD_GET_CONFIGURATION
:
3135 DBG_CMD("RELAYD_GET_CONFIGURATION", conn
);
3136 ret
= relay_get_configuration(header
, conn
, payload
);
3138 case RELAYD_UPDATE_SYNC_INFO
:
3140 ERR("Received unknown command (%u)", header
->cmd
);
3141 relay_unknown_command(conn
);
3150 static enum relay_connection_status
relay_process_control_receive_payload(
3151 struct relay_connection
*conn
)
3154 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3155 struct lttng_dynamic_buffer
*reception_buffer
=
3156 &conn
->protocol
.ctrl
.reception_buffer
;
3157 struct ctrl_connection_state_receive_payload
*state
=
3158 &conn
->protocol
.ctrl
.state
.receive_payload
;
3159 struct lttng_buffer_view payload_view
;
3161 if (state
->left_to_receive
== 0) {
3162 /* Short-circuit for payload-less commands. */
3163 goto reception_complete
;
3166 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3167 reception_buffer
->data
+ state
->received
,
3168 state
->left_to_receive
, MSG_DONTWAIT
);
3170 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3171 PERROR("Unable to receive command payload on sock %d",
3173 status
= RELAY_CONNECTION_STATUS_ERROR
;
3176 } else if (ret
== 0) {
3177 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3178 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3183 assert(ret
<= state
->left_to_receive
);
3185 state
->left_to_receive
-= ret
;
3186 state
->received
+= ret
;
3188 if (state
->left_to_receive
> 0) {
3190 * Can't transition to the protocol's next state, wait to
3191 * receive the rest of the header.
3193 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3194 state
->received
, state
->left_to_receive
,
3200 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3201 conn
->sock
->fd
, state
->received
);
3203 * The payload required to process the command has been received.
3204 * A view to the reception buffer is forwarded to the various
3205 * commands and the state of the control is reset on success.
3207 * Commands are responsible for sending their reply to the peer.
3209 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3211 ret
= relay_process_control_command(conn
,
3212 &state
->header
, &payload_view
);
3214 status
= RELAY_CONNECTION_STATUS_ERROR
;
3218 ret
= connection_reset_protocol_state(conn
);
3220 status
= RELAY_CONNECTION_STATUS_ERROR
;
3226 static enum relay_connection_status
relay_process_control_receive_header(
3227 struct relay_connection
*conn
)
3230 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3231 struct lttcomm_relayd_hdr header
;
3232 struct lttng_dynamic_buffer
*reception_buffer
=
3233 &conn
->protocol
.ctrl
.reception_buffer
;
3234 struct ctrl_connection_state_receive_header
*state
=
3235 &conn
->protocol
.ctrl
.state
.receive_header
;
3237 assert(state
->left_to_receive
!= 0);
3239 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3240 reception_buffer
->data
+ state
->received
,
3241 state
->left_to_receive
, MSG_DONTWAIT
);
3243 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3244 PERROR("Unable to receive control command header on sock %d",
3246 status
= RELAY_CONNECTION_STATUS_ERROR
;
3249 } else if (ret
== 0) {
3250 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3251 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3256 assert(ret
<= state
->left_to_receive
);
3258 state
->left_to_receive
-= ret
;
3259 state
->received
+= ret
;
3261 if (state
->left_to_receive
> 0) {
3263 * Can't transition to the protocol's next state, wait to
3264 * receive the rest of the header.
3266 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3267 state
->received
, state
->left_to_receive
,
3272 /* Transition to next state: receiving the command's payload. */
3273 conn
->protocol
.ctrl
.state_id
=
3274 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3275 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3276 header
.circuit_id
= be64toh(header
.circuit_id
);
3277 header
.data_size
= be64toh(header
.data_size
);
3278 header
.cmd
= be32toh(header
.cmd
);
3279 header
.cmd_version
= be32toh(header
.cmd_version
);
3280 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3281 &header
, sizeof(header
));
3283 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3284 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3287 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3288 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3290 status
= RELAY_CONNECTION_STATUS_ERROR
;
3294 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3296 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3297 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3300 status
= RELAY_CONNECTION_STATUS_ERROR
;
3304 if (header
.data_size
== 0) {
3306 * Manually invoke the next state as the poll loop
3307 * will not wake-up to allow us to proceed further.
3309 status
= relay_process_control_receive_payload(conn
);
3316 * Process the commands received on the control socket
3318 static enum relay_connection_status
relay_process_control(
3319 struct relay_connection
*conn
)
3321 enum relay_connection_status status
;
3323 switch (conn
->protocol
.ctrl
.state_id
) {
3324 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3325 status
= relay_process_control_receive_header(conn
);
3327 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3328 status
= relay_process_control_receive_payload(conn
);
3331 ERR("Unknown control connection protocol state encountered.");
3338 static enum relay_connection_status
relay_process_data_receive_header(
3339 struct relay_connection
*conn
)
3342 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3343 struct data_connection_state_receive_header
*state
=
3344 &conn
->protocol
.data
.state
.receive_header
;
3345 struct lttcomm_relayd_data_hdr header
;
3346 struct relay_stream
*stream
;
3348 assert(state
->left_to_receive
!= 0);
3350 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3351 state
->header_reception_buffer
+ state
->received
,
3352 state
->left_to_receive
, MSG_DONTWAIT
);
3354 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3355 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3356 status
= RELAY_CONNECTION_STATUS_ERROR
;
3359 } else if (ret
== 0) {
3360 /* Orderly shutdown. Not necessary to print an error. */
3361 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3362 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3367 assert(ret
<= state
->left_to_receive
);
3369 state
->left_to_receive
-= ret
;
3370 state
->received
+= ret
;
3372 if (state
->left_to_receive
> 0) {
3374 * Can't transition to the protocol's next state, wait to
3375 * receive the rest of the header.
3377 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3378 state
->received
, state
->left_to_receive
,
3383 /* Transition to next state: receiving the payload. */
3384 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3386 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3387 header
.circuit_id
= be64toh(header
.circuit_id
);
3388 header
.stream_id
= be64toh(header
.stream_id
);
3389 header
.data_size
= be32toh(header
.data_size
);
3390 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3391 header
.padding_size
= be32toh(header
.padding_size
);
3392 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3394 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3396 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3397 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3399 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3400 conn
->sock
->fd
, header
.circuit_id
,
3401 header
.stream_id
, header
.data_size
,
3402 header
.net_seq_num
, header
.padding_size
);
3404 stream
= stream_get_by_id(header
.stream_id
);
3406 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3408 /* Protocol error. */
3409 status
= RELAY_CONNECTION_STATUS_ERROR
;
3413 pthread_mutex_lock(&stream
->lock
);
3414 /* Prepare stream for the reception of a new packet. */
3415 ret
= stream_init_packet(stream
, header
.data_size
,
3416 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3417 pthread_mutex_unlock(&stream
->lock
);
3419 ERR("Failed to rotate stream output file");
3420 status
= RELAY_CONNECTION_STATUS_ERROR
;
3421 goto end_stream_unlock
;
3430 static enum relay_connection_status
relay_process_data_receive_payload(
3431 struct relay_connection
*conn
)
3434 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3435 struct relay_stream
*stream
;
3436 struct data_connection_state_receive_payload
*state
=
3437 &conn
->protocol
.data
.state
.receive_payload
;
3438 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3439 char data_buffer
[chunk_size
];
3440 bool partial_recv
= false;
3441 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3442 uint64_t left_to_receive
= state
->left_to_receive
;
3443 struct relay_session
*session
;
3445 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3446 state
->header
.stream_id
, state
->header
.net_seq_num
,
3447 state
->received
, left_to_receive
);
3449 stream
= stream_get_by_id(state
->header
.stream_id
);
3451 /* Protocol error. */
3452 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3453 state
->header
.stream_id
);
3454 status
= RELAY_CONNECTION_STATUS_ERROR
;
3458 pthread_mutex_lock(&stream
->lock
);
3459 session
= stream
->trace
->session
;
3460 if (!conn
->session
) {
3461 ret
= connection_set_session(conn
, session
);
3463 status
= RELAY_CONNECTION_STATUS_ERROR
;
3464 goto end_stream_unlock
;
3469 * The size of the "chunk" received on any iteration is bounded by:
3470 * - the data left to receive,
3471 * - the data immediately available on the socket,
3472 * - the on-stack data buffer
3474 while (left_to_receive
> 0 && !partial_recv
) {
3475 size_t recv_size
= min(left_to_receive
, chunk_size
);
3476 struct lttng_buffer_view packet_chunk
;
3478 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3479 recv_size
, MSG_DONTWAIT
);
3481 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3482 PERROR("Socket %d error", conn
->sock
->fd
);
3483 status
= RELAY_CONNECTION_STATUS_ERROR
;
3485 goto end_stream_unlock
;
3486 } else if (ret
== 0) {
3487 /* No more data ready to be consumed on socket. */
3488 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3489 state
->header
.stream_id
);
3490 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3492 } else if (ret
< (int) recv_size
) {
3494 * All the data available on the socket has been
3497 partial_recv
= true;
3501 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3503 assert(packet_chunk
.data
);
3505 ret
= stream_write(stream
, &packet_chunk
, 0);
3507 ERR("Relay error writing data to file");
3508 status
= RELAY_CONNECTION_STATUS_ERROR
;
3509 goto end_stream_unlock
;
3512 left_to_receive
-= recv_size
;
3513 state
->received
+= recv_size
;
3514 state
->left_to_receive
= left_to_receive
;
3517 if (state
->left_to_receive
> 0) {
3519 * Did not receive all the data expected, wait for more data to
3520 * become available on the socket.
3522 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3523 state
->header
.stream_id
, state
->received
,
3524 state
->left_to_receive
);
3525 goto end_stream_unlock
;
3528 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3530 status
= RELAY_CONNECTION_STATUS_ERROR
;
3531 goto end_stream_unlock
;
3534 if (session_streams_have_index(session
)) {
3535 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3536 state
->rotate_index
, &index_flushed
,
3537 state
->header
.data_size
+ state
->header
.padding_size
);
3539 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3540 stream
->stream_handle
,
3541 state
->header
.net_seq_num
, ret
);
3542 status
= RELAY_CONNECTION_STATUS_ERROR
;
3543 goto end_stream_unlock
;
3547 if (stream
->prev_data_seq
== -1ULL) {
3551 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3552 state
->header
.padding_size
, state
->header
.net_seq_num
,
3555 status
= RELAY_CONNECTION_STATUS_ERROR
;
3556 goto end_stream_unlock
;
3560 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3561 * contents of *state which are aliased (union) to the same location as
3562 * the new state. Don't use it beyond this point.
3564 connection_reset_protocol_state(conn
);
3568 close_requested
= stream
->close_requested
;
3569 pthread_mutex_unlock(&stream
->lock
);
3570 if (close_requested
&& left_to_receive
== 0) {
3571 try_stream_close(stream
);
3575 pthread_mutex_lock(&session
->lock
);
3576 uatomic_set(&session
->new_streams
, 1);
3577 pthread_mutex_unlock(&session
->lock
);
3586 * relay_process_data: Process the data received on the data socket
3588 static enum relay_connection_status
relay_process_data(
3589 struct relay_connection
*conn
)
3591 enum relay_connection_status status
;
3593 switch (conn
->protocol
.data
.state_id
) {
3594 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3595 status
= relay_process_data_receive_header(conn
);
3597 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3598 status
= relay_process_data_receive_payload(conn
);
3601 ERR("Unexpected data connection communication state.");
3608 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3612 (void) lttng_poll_del(events
, pollfd
);
3614 ret
= close(pollfd
);
3616 ERR("Closing pollfd %d", pollfd
);
3620 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3621 int pollfd
, struct relay_connection
*conn
)
3623 const char *type_str
;
3625 switch (conn
->type
) {
3630 type_str
= "Control";
3632 case RELAY_VIEWER_COMMAND
:
3633 type_str
= "Viewer Command";
3635 case RELAY_VIEWER_NOTIFICATION
:
3636 type_str
= "Viewer Notification";
3639 type_str
= "Unknown";
3641 cleanup_connection_pollfd(events
, pollfd