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>
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/compat/getenv.h>
51 #include <common/defaults.h>
52 #include <common/daemonize.h>
53 #include <common/futex.h>
54 #include <common/sessiond-comm/sessiond-comm.h>
55 #include <common/sessiond-comm/inet.h>
56 #include <common/sessiond-comm/relayd.h>
57 #include <common/uri.h>
58 #include <common/utils.h>
59 #include <common/align.h>
60 #include <common/config/session-config.h>
61 #include <common/dynamic-buffer.h>
62 #include <common/buffer-view.h>
63 #include <common/fd-tracker/utils.h>
64 #include <urcu/rculist.h>
67 #include "ctf-trace.h"
70 #include "lttng-relayd.h"
72 #include "health-relayd.h"
73 #include "testpoint.h"
74 #include "viewer-stream.h"
77 #include "connection.h"
78 #include "tracefile-array.h"
79 #include "tcp_keep_alive.h"
81 static const char *help_msg
=
82 #ifdef LTTNG_EMBED_HELP
83 #include <lttng-relayd.8.h>
89 enum relay_connection_status
{
90 RELAY_CONNECTION_STATUS_OK
,
91 /* An error occured while processing an event on the connection. */
92 RELAY_CONNECTION_STATUS_ERROR
,
93 /* Connection closed/shutdown cleanly. */
94 RELAY_CONNECTION_STATUS_CLOSED
,
97 /* command line options */
98 char *opt_output_path
;
99 static int opt_daemon
, opt_background
;
102 * We need to wait for listener and live listener threads, as well as
103 * health check thread, before being ready to signal readiness.
105 #define NR_LTTNG_RELAY_READY 3
106 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
108 /* Size of receive buffer. */
109 #define RECV_DATA_BUFFER_SIZE 65536
110 #define FILE_COPY_BUFFER_SIZE 65536
112 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
113 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
115 static struct lttng_uri
*control_uri
;
116 static struct lttng_uri
*data_uri
;
117 static struct lttng_uri
*live_uri
;
119 const char *progname
;
121 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
122 static int tracing_group_name_override
;
124 const char * const config_section_name
= "relayd";
127 * Quit pipe for all threads. This permits a single cancellation point
128 * for all threads when receiving an event on the pipe.
130 int thread_quit_pipe
[2] = { -1, -1 };
133 * This pipe is used to inform the worker thread that a command is queued and
134 * ready to be processed.
136 static int relay_conn_pipe
[2] = { -1, -1 };
138 /* Shared between threads */
139 static int dispatch_thread_exit
;
141 static pthread_t listener_thread
;
142 static pthread_t dispatcher_thread
;
143 static pthread_t worker_thread
;
144 static pthread_t health_thread
;
147 * last_relay_stream_id_lock protects last_relay_stream_id increment
148 * atomicity on 32-bit architectures.
150 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
151 static uint64_t last_relay_stream_id
;
154 * Relay command queue.
156 * The relay_thread_listener and relay_thread_dispatcher communicate with this
159 static struct relay_conn_queue relay_conn_queue
;
161 /* Cap of file desriptors to be in simultaneous use by the relay daemon. */
162 static unsigned int lttng_opt_fd_cap
;
164 /* Global relay stream hash table. */
165 struct lttng_ht
*relay_streams_ht
;
167 /* Global relay viewer stream hash table. */
168 struct lttng_ht
*viewer_streams_ht
;
170 /* Global relay sessions hash table. */
171 struct lttng_ht
*sessions_ht
;
173 /* Relayd health monitoring */
174 struct health_app
*health_relayd
;
176 /* Global fd tracker. */
177 struct fd_tracker
*the_fd_tracker
;
179 static struct option long_options
[] = {
180 { "control-port", 1, 0, 'C', },
181 { "data-port", 1, 0, 'D', },
182 { "live-port", 1, 0, 'L', },
183 { "daemonize", 0, 0, 'd', },
184 { "background", 0, 0, 'b', },
185 { "group", 1, 0, 'g', },
186 { "fd-cap", 1, 0, '\0', },
187 { "help", 0, 0, 'h', },
188 { "output", 1, 0, 'o', },
189 { "verbose", 0, 0, 'v', },
190 { "config", 1, 0, 'f' },
191 { "version", 0, 0, 'V' },
195 static const char *config_ignore_options
[] = { "help", "config", "version" };
198 * Take an option from the getopt output and set it in the right variable to be
201 * Return 0 on success else a negative value.
203 static int set_option(int opt
, const char *arg
, const char *optname
)
209 if (!strcmp(optname
, "fd-cap")) {
213 v
= strtoul(arg
, NULL
, 0);
214 if (errno
!= 0 || !isdigit(arg
[0])) {
215 ERR("Wrong value in --fd-cap parameter: %s", arg
);
219 if (v
< DEFAULT_RELAYD_MINIMAL_FD_CAP
) {
220 ERR("File descriptor cap must be set to at least %d",
221 DEFAULT_RELAYD_MINIMAL_FD_CAP
);
224 ERR("File descriptor cap overflow in --fd-cap parameter: %s", arg
);
228 lttng_opt_fd_cap
= (unsigned int) v
;
229 DBG3("File descriptor cap set to %u", lttng_opt_fd_cap
);
232 fprintf(stderr
, "unknown option %s", optname
);
234 fprintf(stderr
, " with arg %s\n", arg
);
239 if (lttng_is_setuid_setgid()) {
240 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
241 "-C, --control-port");
243 ret
= uri_parse(arg
, &control_uri
);
245 ERR("Invalid control URI specified");
248 if (control_uri
->port
== 0) {
249 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
254 if (lttng_is_setuid_setgid()) {
255 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
258 ret
= uri_parse(arg
, &data_uri
);
260 ERR("Invalid data URI specified");
263 if (data_uri
->port
== 0) {
264 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
269 if (lttng_is_setuid_setgid()) {
270 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
273 ret
= uri_parse(arg
, &live_uri
);
275 ERR("Invalid live URI specified");
278 if (live_uri
->port
== 0) {
279 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
290 if (lttng_is_setuid_setgid()) {
291 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
294 tracing_group_name
= strdup(arg
);
295 if (tracing_group_name
== NULL
) {
300 tracing_group_name_override
= 1;
304 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
306 ERR("Cannot show --help for `lttng-relayd`");
311 fprintf(stdout
, "%s\n", VERSION
);
314 if (lttng_is_setuid_setgid()) {
315 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
318 ret
= asprintf(&opt_output_path
, "%s", arg
);
321 PERROR("asprintf opt_output_path");
327 /* Verbose level can increase using multiple -v */
329 lttng_opt_verbose
= config_parse_value(arg
);
331 /* Only 3 level of verbosity (-vvv). */
332 if (lttng_opt_verbose
< 3) {
333 lttng_opt_verbose
+= 1;
338 /* Unknown option or other error.
339 * Error is printed by getopt, just return */
352 * config_entry_handler_cb used to handle options read from a config file.
353 * See config_entry_handler_cb comment in common/config/session-config.h for the
354 * return value conventions.
356 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
360 if (!entry
|| !entry
->name
|| !entry
->value
) {
365 /* Check if the option is to be ignored */
366 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
367 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
372 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
373 /* Ignore if entry name is not fully matched. */
374 if (strcmp(entry
->name
, long_options
[i
].name
)) {
379 * If the option takes no argument on the command line,
380 * we have to check if the value is "true". We support
381 * non-zero numeric values, true, on and yes.
383 if (!long_options
[i
].has_arg
) {
384 ret
= config_parse_value(entry
->value
);
387 WARN("Invalid configuration value \"%s\" for option %s",
388 entry
->value
, entry
->name
);
390 /* False, skip boolean config option. */
395 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
399 WARN("Unrecognized option \"%s\" in daemon configuration file.",
406 static int set_options(int argc
, char **argv
)
408 int c
, ret
= 0, option_index
= 0, retval
= 0;
409 int orig_optopt
= optopt
, orig_optind
= optind
;
410 char *default_address
, *optstring
;
411 const char *config_path
= NULL
;
413 optstring
= utils_generate_optstring(long_options
,
414 sizeof(long_options
) / sizeof(struct option
));
420 /* Check for the --config option */
422 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
423 &option_index
)) != -1) {
427 } else if (c
!= 'f') {
431 if (lttng_is_setuid_setgid()) {
432 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
435 config_path
= utils_expand_path(optarg
);
437 ERR("Failed to resolve path: %s", optarg
);
442 ret
= config_get_section_entries(config_path
, config_section_name
,
443 config_entry_handler
, NULL
);
446 ERR("Invalid configuration option at line %i", ret
);
452 /* Reset getopt's global state */
453 optopt
= orig_optopt
;
454 optind
= orig_optind
;
456 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
461 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
468 /* assign default values */
469 if (control_uri
== NULL
) {
470 ret
= asprintf(&default_address
,
471 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
472 DEFAULT_NETWORK_CONTROL_PORT
);
474 PERROR("asprintf default data address");
479 ret
= uri_parse(default_address
, &control_uri
);
480 free(default_address
);
482 ERR("Invalid control URI specified");
487 if (data_uri
== NULL
) {
488 ret
= asprintf(&default_address
,
489 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
490 DEFAULT_NETWORK_DATA_PORT
);
492 PERROR("asprintf default data address");
497 ret
= uri_parse(default_address
, &data_uri
);
498 free(default_address
);
500 ERR("Invalid data URI specified");
505 if (live_uri
== NULL
) {
506 ret
= asprintf(&default_address
,
507 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
508 DEFAULT_NETWORK_VIEWER_PORT
);
510 PERROR("asprintf default viewer control address");
515 ret
= uri_parse(default_address
, &live_uri
);
516 free(default_address
);
518 ERR("Invalid viewer control URI specified");
523 if (lttng_opt_fd_cap
== 0) {
525 struct rlimit rlimit
;
527 ret
= getrlimit(RLIMIT_NOFILE
, &rlimit
);
529 PERROR("Failed to get file descriptor limit");
533 lttng_opt_fd_cap
= rlimit
.rlim_cur
;
541 static void print_global_objects(void)
543 print_viewer_streams();
544 print_relay_streams();
551 static void relayd_cleanup(void)
553 print_global_objects();
557 if (viewer_streams_ht
)
558 lttng_ht_destroy(viewer_streams_ht
);
559 if (relay_streams_ht
)
560 lttng_ht_destroy(relay_streams_ht
);
562 lttng_ht_destroy(sessions_ht
);
564 /* free the dynamically allocated opt_output_path */
565 free(opt_output_path
);
567 /* Close thread quit pipes */
568 (void) fd_tracker_util_pipe_close(the_fd_tracker
, thread_quit_pipe
);
570 uri_free(control_uri
);
572 /* Live URI is freed in the live thread. */
574 if (tracing_group_name_override
) {
575 free((void *) tracing_group_name
);
577 fd_tracker_log(the_fd_tracker
);
581 * Write to writable pipe used to notify a thread.
583 static int notify_thread_pipe(int wpipe
)
587 ret
= lttng_write(wpipe
, "!", 1);
589 PERROR("write poll pipe");
597 static int notify_health_quit_pipe(int *pipe
)
601 ret
= lttng_write(pipe
[1], "4", 1);
603 PERROR("write relay health quit");
612 * Stop all relayd and relayd-live threads.
614 int lttng_relay_stop_threads(void)
618 /* Stopping all threads */
619 DBG("Terminating all threads");
620 if (notify_thread_pipe(thread_quit_pipe
[1])) {
621 ERR("write error on thread quit pipe");
625 if (notify_health_quit_pipe(health_quit_pipe
)) {
626 ERR("write error on health quit pipe");
629 /* Dispatch thread */
630 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
631 futex_nto1_wake(&relay_conn_queue
.futex
);
633 if (relayd_live_stop()) {
634 ERR("Error stopping live threads");
641 * Signal handler for the daemon
643 * Simply stop all worker threads, leaving main() return gracefully after
644 * joining all threads and calling cleanup().
646 static void sighandler(int sig
)
650 DBG("SIGINT caught");
651 if (lttng_relay_stop_threads()) {
652 ERR("Error stopping threads");
656 DBG("SIGTERM caught");
657 if (lttng_relay_stop_threads()) {
658 ERR("Error stopping threads");
662 CMM_STORE_SHARED(recv_child_signal
, 1);
670 * Setup signal handler for :
671 * SIGINT, SIGTERM, SIGPIPE
673 static int set_signal_handler(void)
679 if ((ret
= sigemptyset(&sigset
)) < 0) {
680 PERROR("sigemptyset");
687 sa
.sa_handler
= sighandler
;
688 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
693 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
698 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
703 sa
.sa_handler
= SIG_IGN
;
704 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
709 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
714 void lttng_relay_notify_ready(void)
716 /* Notify the parent of the fork() process that we are ready. */
717 if (opt_daemon
|| opt_background
) {
718 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
719 kill(child_ppid
, SIGUSR1
);
725 * Init thread quit pipe.
727 * Return -1 on error or 0 if all pipes are created.
729 static int init_thread_quit_pipe(void)
731 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
732 "Quit pipe", thread_quit_pipe
);
736 * Init health quit pipe.
738 * Return -1 on error or 0 if all pipes are created.
740 static int init_health_quit_pipe(void)
742 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
743 "Health quit pipe", health_quit_pipe
);
747 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
749 static int create_named_thread_poll_set(struct lttng_poll_event
*events
,
750 int size
, const char *name
)
754 if (events
== NULL
|| size
== 0) {
759 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
760 name
, events
, 1, LTTNG_CLOEXEC
);
763 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
775 * Check if the thread quit pipe was triggered.
777 * Return 1 if it was triggered else 0;
779 static int check_thread_quit_pipe(int fd
, uint32_t events
)
781 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
789 * Create and init socket from uri.
791 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
794 struct lttcomm_sock
*sock
= NULL
;
796 sock
= lttcomm_alloc_sock_from_uri(uri
);
798 ERR("Allocating socket");
802 ret
= lttcomm_create_sock(sock
);
806 DBG("Listening on sock %d", sock
->fd
);
808 ret
= sock
->ops
->bind(sock
);
810 PERROR("Failed to bind socket");
814 ret
= sock
->ops
->listen(sock
, -1);
824 lttcomm_destroy_sock(sock
);
830 * This thread manages the listening for new connections on the network
832 static void *relay_thread_listener(void *data
)
834 int i
, ret
, pollfd
, err
= -1;
835 uint32_t revents
, nb_fd
;
836 struct lttng_poll_event events
;
837 struct lttcomm_sock
*control_sock
, *data_sock
;
839 DBG("[thread] Relay listener started");
841 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
843 health_code_update();
845 control_sock
= relay_socket_create(control_uri
);
847 goto error_sock_control
;
850 data_sock
= relay_socket_create(data_uri
);
852 goto error_sock_relay
;
856 * Pass 3 as size here for the thread quit pipe, control and
859 ret
= create_named_thread_poll_set(&events
, 3, "Listener thread epoll");
861 goto error_create_poll
;
864 /* Add the control socket */
865 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
870 /* Add the data socket */
871 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
876 lttng_relay_notify_ready();
878 if (testpoint(relayd_thread_listener
)) {
879 goto error_testpoint
;
883 health_code_update();
885 DBG("Listener accepting connections");
889 ret
= lttng_poll_wait(&events
, -1);
893 * Restart interrupted system call.
895 if (errno
== EINTR
) {
903 DBG("Relay new connection received");
904 for (i
= 0; i
< nb_fd
; i
++) {
905 health_code_update();
907 /* Fetch once the poll data */
908 revents
= LTTNG_POLL_GETEV(&events
, i
);
909 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
913 * No activity for this FD (poll
919 /* Thread quit pipe has been closed. Killing thread. */
920 ret
= check_thread_quit_pipe(pollfd
, revents
);
926 if (revents
& LPOLLIN
) {
928 * A new connection is requested, therefore a
929 * sessiond/consumerd connection is allocated in
930 * this thread, enqueued to a global queue and
931 * dequeued (and freed) in the worker thread.
934 struct relay_connection
*new_conn
;
935 struct lttcomm_sock
*newsock
;
936 enum connection_type type
;
938 if (pollfd
== data_sock
->fd
) {
940 newsock
= data_sock
->ops
->accept(data_sock
);
941 DBG("Relay data connection accepted, socket %d",
944 assert(pollfd
== control_sock
->fd
);
945 type
= RELAY_CONTROL
;
946 newsock
= control_sock
->ops
->accept(control_sock
);
947 DBG("Relay control connection accepted, socket %d",
951 PERROR("accepting sock");
955 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
958 PERROR("setsockopt inet");
959 lttcomm_destroy_sock(newsock
);
963 ret
= socket_apply_keep_alive_config(newsock
->fd
);
965 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
967 lttcomm_destroy_sock(newsock
);
971 new_conn
= connection_create(newsock
, type
);
973 lttcomm_destroy_sock(newsock
);
977 /* Enqueue request for the dispatcher thread. */
978 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
982 * Wake the dispatch queue futex.
983 * Implicit memory barrier with the
984 * exchange in cds_wfcq_enqueue.
986 futex_nto1_wake(&relay_conn_queue
.futex
);
987 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
988 ERR("socket poll error");
991 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1001 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
1003 if (data_sock
->fd
>= 0) {
1004 ret
= data_sock
->ops
->close(data_sock
);
1009 lttcomm_destroy_sock(data_sock
);
1011 if (control_sock
->fd
>= 0) {
1012 ret
= control_sock
->ops
->close(control_sock
);
1017 lttcomm_destroy_sock(control_sock
);
1021 ERR("Health error occurred in %s", __func__
);
1023 health_unregister(health_relayd
);
1024 DBG("Relay listener thread cleanup complete");
1025 lttng_relay_stop_threads();
1030 * This thread manages the dispatching of the requests to worker threads
1032 static void *relay_thread_dispatcher(void *data
)
1036 struct cds_wfcq_node
*node
;
1037 struct relay_connection
*new_conn
= NULL
;
1039 DBG("[thread] Relay dispatcher started");
1041 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1043 if (testpoint(relayd_thread_dispatcher
)) {
1044 goto error_testpoint
;
1047 health_code_update();
1050 health_code_update();
1052 /* Atomically prepare the queue futex */
1053 futex_nto1_prepare(&relay_conn_queue
.futex
);
1055 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1060 health_code_update();
1062 /* Dequeue commands */
1063 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1064 &relay_conn_queue
.tail
);
1066 DBG("Woken up but nothing in the relay command queue");
1067 /* Continue thread execution */
1070 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1072 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1075 * Inform worker thread of the new request. This
1076 * call is blocking so we can be assured that
1077 * the data will be read at some point in time
1078 * or wait to the end of the world :)
1080 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1082 PERROR("write connection pipe");
1083 connection_put(new_conn
);
1086 } while (node
!= NULL
);
1088 /* Futex wait on queue. Blocking call on futex() */
1089 health_poll_entry();
1090 futex_nto1_wait(&relay_conn_queue
.futex
);
1094 /* Normal exit, no error */
1101 ERR("Health error occurred in %s", __func__
);
1103 health_unregister(health_relayd
);
1104 DBG("Dispatch thread dying");
1105 lttng_relay_stop_threads();
1110 * Set index data from the control port to a given index object.
1112 static int set_index_control_data(struct relay_index
*index
,
1113 struct lttcomm_relayd_index
*data
,
1114 struct relay_connection
*conn
)
1116 struct ctf_packet_index index_data
;
1119 * The index on disk is encoded in big endian.
1121 index_data
.packet_size
= htobe64(data
->packet_size
);
1122 index_data
.content_size
= htobe64(data
->content_size
);
1123 index_data
.timestamp_begin
= htobe64(data
->timestamp_begin
);
1124 index_data
.timestamp_end
= htobe64(data
->timestamp_end
);
1125 index_data
.events_discarded
= htobe64(data
->events_discarded
);
1126 index_data
.stream_id
= htobe64(data
->stream_id
);
1128 if (conn
->minor
>= 8) {
1129 index
->index_data
.stream_instance_id
= htobe64(data
->stream_instance_id
);
1130 index
->index_data
.packet_seq_num
= htobe64(data
->packet_seq_num
);
1133 return relay_index_set_data(index
, &index_data
);
1137 * Handle the RELAYD_CREATE_SESSION command.
1139 * On success, send back the session id or else return a negative value.
1141 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1142 struct relay_connection
*conn
,
1143 const struct lttng_buffer_view
*payload
)
1147 struct relay_session
*session
;
1148 struct lttcomm_relayd_status_session reply
;
1149 char session_name
[LTTNG_NAME_MAX
];
1150 char hostname
[LTTNG_HOST_NAME_MAX
];
1151 uint32_t live_timer
= 0;
1152 bool snapshot
= false;
1154 memset(session_name
, 0, LTTNG_NAME_MAX
);
1155 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1157 memset(&reply
, 0, sizeof(reply
));
1159 if (conn
->minor
< 4) {
1160 /* From 2.1 to 2.3 */
1162 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1163 /* From 2.4 to 2.10 */
1164 ret
= cmd_create_session_2_4(payload
, session_name
,
1165 hostname
, &live_timer
, &snapshot
);
1167 /* From 2.11 to ... */
1168 ret
= cmd_create_session_2_11(payload
, session_name
,
1169 hostname
, &live_timer
, &snapshot
);
1176 session
= session_create(session_name
, hostname
, live_timer
,
1177 snapshot
, conn
->major
, conn
->minor
);
1182 assert(!conn
->session
);
1183 conn
->session
= session
;
1184 DBG("Created session %" PRIu64
, session
->id
);
1186 reply
.session_id
= htobe64(session
->id
);
1190 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1192 reply
.ret_code
= htobe32(LTTNG_OK
);
1195 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1196 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1197 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1206 * When we have received all the streams and the metadata for a channel,
1207 * we make them visible to the viewer threads.
1209 static void publish_connection_local_streams(struct relay_connection
*conn
)
1211 struct relay_stream
*stream
;
1212 struct relay_session
*session
= conn
->session
;
1215 * We publish all streams belonging to a session atomically wrt
1218 pthread_mutex_lock(&session
->lock
);
1220 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1222 stream_publish(stream
);
1227 * Inform the viewer that there are new streams in the session.
1229 if (session
->viewer_attached
) {
1230 uatomic_set(&session
->new_streams
, 1);
1232 pthread_mutex_unlock(&session
->lock
);
1236 * relay_add_stream: allocate a new stream for a session
1238 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1239 struct relay_connection
*conn
,
1240 const struct lttng_buffer_view
*payload
)
1244 struct relay_session
*session
= conn
->session
;
1245 struct relay_stream
*stream
= NULL
;
1246 struct lttcomm_relayd_status_stream reply
;
1247 struct ctf_trace
*trace
= NULL
;
1248 uint64_t stream_handle
= -1ULL;
1249 char *path_name
= NULL
, *channel_name
= NULL
;
1250 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1251 struct relay_stream_chunk_id stream_chunk_id
= { 0 };
1253 if (!session
|| !conn
->version_check_done
) {
1254 ERR("Trying to add a stream before version check");
1256 goto end_no_session
;
1259 if (session
->minor
== 1) {
1261 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1263 } else if (session
->minor
> 1 && session
->minor
< 11) {
1264 /* From 2.2 to 2.10 */
1265 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1266 &channel_name
, &tracefile_size
, &tracefile_count
);
1268 /* From 2.11 to ... */
1269 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1270 &channel_name
, &tracefile_size
, &tracefile_count
,
1271 &stream_chunk_id
.value
);
1272 stream_chunk_id
.is_set
= true;
1279 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1283 /* This stream here has one reference on the trace. */
1285 pthread_mutex_lock(&last_relay_stream_id_lock
);
1286 stream_handle
= ++last_relay_stream_id
;
1287 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1289 /* We pass ownership of path_name and channel_name. */
1290 stream
= stream_create(trace
, stream_handle
, path_name
,
1291 channel_name
, tracefile_size
, tracefile_count
,
1294 channel_name
= NULL
;
1297 * Streams are the owners of their trace. Reference to trace is
1298 * kept within stream_create().
1300 ctf_trace_put(trace
);
1303 memset(&reply
, 0, sizeof(reply
));
1304 reply
.handle
= htobe64(stream_handle
);
1306 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1308 reply
.ret_code
= htobe32(LTTNG_OK
);
1311 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1312 sizeof(struct lttcomm_relayd_status_stream
), 0);
1313 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1314 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1326 * relay_close_stream: close a specific stream
1328 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1329 struct relay_connection
*conn
,
1330 const struct lttng_buffer_view
*payload
)
1334 struct relay_session
*session
= conn
->session
;
1335 struct lttcomm_relayd_close_stream stream_info
;
1336 struct lttcomm_relayd_generic_reply reply
;
1337 struct relay_stream
*stream
;
1339 DBG("Close stream received");
1341 if (!session
|| !conn
->version_check_done
) {
1342 ERR("Trying to close a stream before version check");
1344 goto end_no_session
;
1347 if (payload
->size
< sizeof(stream_info
)) {
1348 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1349 sizeof(stream_info
), payload
->size
);
1351 goto end_no_session
;
1353 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1354 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1355 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1357 stream
= stream_get_by_id(stream_info
.stream_id
);
1364 * Set last_net_seq_num before the close flag. Required by data
1367 pthread_mutex_lock(&stream
->lock
);
1368 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1369 pthread_mutex_unlock(&stream
->lock
);
1372 * This is one of the conditions which may trigger a stream close
1373 * with the others being:
1374 * 1) A close command is received for a stream
1375 * 2) The control connection owning the stream is closed
1376 * 3) We have received all of the stream's data _after_ a close
1379 try_stream_close(stream
);
1380 if (stream
->is_metadata
) {
1381 struct relay_viewer_stream
*vstream
;
1383 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1385 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1387 * Since all the metadata has been sent to the
1388 * viewer and that we have a request to close
1389 * its stream, we can safely teardown the
1390 * corresponding metadata viewer stream.
1392 viewer_stream_put(vstream
);
1394 /* Put local reference. */
1395 viewer_stream_put(vstream
);
1402 memset(&reply
, 0, sizeof(reply
));
1404 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1406 reply
.ret_code
= htobe32(LTTNG_OK
);
1408 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1409 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1410 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1411 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1421 * relay_reset_metadata: reset a metadata stream
1424 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1425 struct relay_connection
*conn
,
1426 const struct lttng_buffer_view
*payload
)
1430 struct relay_session
*session
= conn
->session
;
1431 struct lttcomm_relayd_reset_metadata stream_info
;
1432 struct lttcomm_relayd_generic_reply reply
;
1433 struct relay_stream
*stream
;
1435 DBG("Reset metadata received");
1437 if (!session
|| !conn
->version_check_done
) {
1438 ERR("Trying to reset a metadata stream before version check");
1440 goto end_no_session
;
1443 if (payload
->size
< sizeof(stream_info
)) {
1444 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1445 sizeof(stream_info
), payload
->size
);
1447 goto end_no_session
;
1449 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1450 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1451 stream_info
.version
= be64toh(stream_info
.version
);
1453 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1455 /* Unsupported for live sessions for now. */
1456 if (session
->live_timer
!= 0) {
1461 stream
= stream_get_by_id(stream_info
.stream_id
);
1466 pthread_mutex_lock(&stream
->lock
);
1467 if (!stream
->is_metadata
) {
1472 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1473 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1474 &stream
->stream_fd
->fd
);
1476 ERR("Failed to rotate metadata file %s of channel %s",
1477 stream
->path_name
, stream
->channel_name
);
1482 pthread_mutex_unlock(&stream
->lock
);
1486 memset(&reply
, 0, sizeof(reply
));
1488 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1490 reply
.ret_code
= htobe32(LTTNG_OK
);
1492 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1493 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1494 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1495 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1505 * relay_unknown_command: send -1 if received unknown command
1507 static void relay_unknown_command(struct relay_connection
*conn
)
1509 struct lttcomm_relayd_generic_reply reply
;
1512 memset(&reply
, 0, sizeof(reply
));
1513 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1514 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1515 if (send_ret
< sizeof(reply
)) {
1516 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1521 * relay_start: send an acknowledgment to the client to tell if we are
1522 * ready to receive data. We are ready if a session is established.
1524 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1525 struct relay_connection
*conn
,
1526 const struct lttng_buffer_view
*payload
)
1530 struct lttcomm_relayd_generic_reply reply
;
1531 struct relay_session
*session
= conn
->session
;
1534 DBG("Trying to start the streaming without a session established");
1535 ret
= htobe32(LTTNG_ERR_UNK
);
1538 memset(&reply
, 0, sizeof(reply
));
1539 reply
.ret_code
= htobe32(LTTNG_OK
);
1540 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1542 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1543 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1552 * Append padding to the file pointed by the file descriptor fd.
1554 static int write_padding_to_file(int fd
, uint32_t size
)
1563 zeros
= zmalloc(size
);
1564 if (zeros
== NULL
) {
1565 PERROR("zmalloc zeros for padding");
1570 ret
= lttng_write(fd
, zeros
, size
);
1572 PERROR("write padding to file");
1582 * Close the current index file if it is open, and create a new one.
1584 * Return 0 on success, -1 on error.
1587 int create_rotate_index_file(struct relay_stream
*stream
)
1590 uint32_t major
, minor
;
1592 /* Put ref on previous index_file. */
1593 if (stream
->index_file
) {
1594 lttng_index_file_put(stream
->index_file
);
1595 stream
->index_file
= NULL
;
1597 major
= stream
->trace
->session
->major
;
1598 minor
= stream
->trace
->session
->minor
;
1599 stream
->index_file
= lttng_index_file_create(stream
->path_name
,
1600 stream
->channel_name
,
1601 -1, -1, stream
->tracefile_size
,
1602 tracefile_array_get_file_index_head(stream
->tfa
),
1603 lttng_to_index_major(major
, minor
),
1604 lttng_to_index_minor(major
, minor
));
1605 if (!stream
->index_file
) {
1617 int do_rotate_stream(struct relay_stream
*stream
)
1621 /* Perform the stream rotation. */
1622 ret
= utils_rotate_stream_file(stream
->path_name
,
1623 stream
->channel_name
, stream
->tracefile_size
,
1624 stream
->tracefile_count
, -1,
1625 -1, stream
->stream_fd
->fd
,
1626 NULL
, &stream
->stream_fd
->fd
);
1628 ERR("Rotating stream output file");
1631 stream
->tracefile_size_current
= 0;
1633 /* Rotate also the index if the stream is not a metadata stream. */
1634 if (!stream
->is_metadata
) {
1635 ret
= create_rotate_index_file(stream
);
1637 ERR("Failed to rotate index file");
1642 stream
->rotate_at_seq_num
= -1ULL;
1643 stream
->pos_after_last_complete_data_index
= 0;
1650 * If too much data has been written in a tracefile before we received the
1651 * rotation command, we have to move the excess data to the new tracefile and
1652 * perform the rotation. This can happen because the control and data
1653 * connections are separate, the indexes as well as the commands arrive from
1654 * the control connection and we have no control over the order so we could be
1655 * in a situation where too much data has been received on the data connection
1656 * before the rotation command on the control connection arrives. We don't need
1657 * to update the index because its order is guaranteed with the rotation
1661 int rotate_truncate_stream(struct relay_stream
*stream
)
1665 uint64_t diff
, pos
= 0;
1666 char buf
[FILE_COPY_BUFFER_SIZE
];
1668 assert(!stream
->is_metadata
);
1670 assert(stream
->tracefile_size_current
>
1671 stream
->pos_after_last_complete_data_index
);
1672 diff
= stream
->tracefile_size_current
-
1673 stream
->pos_after_last_complete_data_index
;
1675 /* Create the new tracefile. */
1676 new_fd
= utils_create_stream_file(stream
->path_name
,
1677 stream
->channel_name
,
1678 stream
->tracefile_size
, stream
->tracefile_count
,
1679 /* uid */ -1, /* gid */ -1, /* suffix */ NULL
);
1681 ERR("Failed to create new stream file at path %s for channel %s",
1682 stream
->path_name
, stream
->channel_name
);
1688 * Rewind the current tracefile to the position at which the rotation
1689 * should have occured.
1691 lseek_ret
= lseek(stream
->stream_fd
->fd
,
1692 stream
->pos_after_last_complete_data_index
, SEEK_SET
);
1693 if (lseek_ret
< 0) {
1694 PERROR("seek truncate stream");
1699 /* Move data from the old file to the new file. */
1700 while (pos
< diff
) {
1701 uint64_t count
, bytes_left
;
1704 bytes_left
= diff
- pos
;
1705 count
= bytes_left
> sizeof(buf
) ? sizeof(buf
) : bytes_left
;
1706 assert(count
<= SIZE_MAX
);
1708 io_ret
= lttng_read(stream
->stream_fd
->fd
, buf
, count
);
1709 if (io_ret
< (ssize_t
) count
) {
1710 char error_string
[256];
1712 snprintf(error_string
, sizeof(error_string
),
1713 "Failed to read %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1714 count
, stream
->stream_fd
->fd
, io_ret
);
1716 PERROR("%s", error_string
);
1718 ERR("%s", error_string
);
1724 io_ret
= lttng_write(new_fd
, buf
, count
);
1725 if (io_ret
< (ssize_t
) count
) {
1726 char error_string
[256];
1728 snprintf(error_string
, sizeof(error_string
),
1729 "Failed to write %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1730 count
, new_fd
, io_ret
);
1732 PERROR("%s", error_string
);
1734 ERR("%s", error_string
);
1743 /* Truncate the file to get rid of the excess data. */
1744 ret
= ftruncate(stream
->stream_fd
->fd
,
1745 stream
->pos_after_last_complete_data_index
);
1747 PERROR("ftruncate");
1751 ret
= close(stream
->stream_fd
->fd
);
1753 PERROR("Closing tracefile");
1757 ret
= create_rotate_index_file(stream
);
1759 ERR("Rotate stream index file");
1764 * Update the offset and FD of all the eventual indexes created by the
1765 * data connection before the rotation command arrived.
1767 ret
= relay_index_switch_all_files(stream
);
1769 ERR("Failed to rotate index file");
1773 stream
->stream_fd
->fd
= new_fd
;
1774 stream
->tracefile_size_current
= diff
;
1775 stream
->pos_after_last_complete_data_index
= 0;
1776 stream
->rotate_at_seq_num
= -1ULL;
1785 * Check if a stream should perform a rotation (for session rotation).
1786 * Must be called with the stream lock held.
1788 * Return 0 on success, a negative value on error.
1791 int try_rotate_stream(struct relay_stream
*stream
)
1795 /* No rotation expected. */
1796 if (stream
->rotate_at_seq_num
== -1ULL) {
1800 if (stream
->prev_seq
< stream
->rotate_at_seq_num
||
1801 stream
->prev_seq
== -1ULL) {
1802 DBG("Stream %" PRIu64
" no yet ready for rotation",
1803 stream
->stream_handle
);
1805 } else if (stream
->prev_seq
> stream
->rotate_at_seq_num
) {
1806 DBG("Rotation after too much data has been written in tracefile "
1807 "for stream %" PRIu64
", need to truncate before "
1808 "rotating", stream
->stream_handle
);
1809 ret
= rotate_truncate_stream(stream
);
1811 ERR("Failed to truncate stream");
1815 /* stream->prev_seq == stream->rotate_at_seq_num */
1816 DBG("Stream %" PRIu64
" ready for rotation",
1817 stream
->stream_handle
);
1818 ret
= do_rotate_stream(stream
);
1826 * relay_recv_metadata: receive the metadata for the session.
1828 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1829 struct relay_connection
*conn
,
1830 const struct lttng_buffer_view
*payload
)
1834 struct relay_session
*session
= conn
->session
;
1835 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1836 struct relay_stream
*metadata_stream
;
1837 uint64_t metadata_payload_size
;
1840 ERR("Metadata sent before version check");
1845 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1846 ERR("Incorrect data size");
1850 metadata_payload_size
= recv_hdr
->data_size
-
1851 sizeof(struct lttcomm_relayd_metadata_payload
);
1853 memcpy(&metadata_payload_header
, payload
->data
,
1854 sizeof(metadata_payload_header
));
1855 metadata_payload_header
.stream_id
= be64toh(
1856 metadata_payload_header
.stream_id
);
1857 metadata_payload_header
.padding_size
= be32toh(
1858 metadata_payload_header
.padding_size
);
1860 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1861 if (!metadata_stream
) {
1866 pthread_mutex_lock(&metadata_stream
->lock
);
1868 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
,
1869 payload
->data
+ sizeof(metadata_payload_header
),
1870 metadata_payload_size
);
1871 if (size_ret
< metadata_payload_size
) {
1872 ERR("Relay error writing metadata on file");
1877 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1878 metadata_payload_header
.padding_size
);
1879 if (size_ret
< (int64_t) metadata_payload_header
.padding_size
) {
1884 metadata_stream
->metadata_received
+=
1885 metadata_payload_size
+ metadata_payload_header
.padding_size
;
1886 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1887 metadata_stream
->metadata_received
);
1889 ret
= try_rotate_stream(metadata_stream
);
1895 pthread_mutex_unlock(&metadata_stream
->lock
);
1896 stream_put(metadata_stream
);
1902 * relay_send_version: send relayd version number
1904 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1905 struct relay_connection
*conn
,
1906 const struct lttng_buffer_view
*payload
)
1910 struct lttcomm_relayd_version reply
, msg
;
1911 bool compatible
= true;
1913 conn
->version_check_done
= true;
1915 /* Get version from the other side. */
1916 if (payload
->size
< sizeof(msg
)) {
1917 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1918 sizeof(msg
), payload
->size
);
1923 memcpy(&msg
, payload
->data
, sizeof(msg
));
1924 msg
.major
= be32toh(msg
.major
);
1925 msg
.minor
= be32toh(msg
.minor
);
1927 memset(&reply
, 0, sizeof(reply
));
1928 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1929 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1931 /* Major versions must be the same */
1932 if (reply
.major
!= msg
.major
) {
1933 DBG("Incompatible major versions (%u vs %u), deleting session",
1934 reply
.major
, msg
.major
);
1938 conn
->major
= reply
.major
;
1939 /* We adapt to the lowest compatible version */
1940 if (reply
.minor
<= msg
.minor
) {
1941 conn
->minor
= reply
.minor
;
1943 conn
->minor
= msg
.minor
;
1946 reply
.major
= htobe32(reply
.major
);
1947 reply
.minor
= htobe32(reply
.minor
);
1948 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1950 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1951 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1964 DBG("Version check done using protocol %u.%u", conn
->major
,
1972 * Check for data pending for a given stream id from the session daemon.
1974 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1975 struct relay_connection
*conn
,
1976 const struct lttng_buffer_view
*payload
)
1978 struct relay_session
*session
= conn
->session
;
1979 struct lttcomm_relayd_data_pending msg
;
1980 struct lttcomm_relayd_generic_reply reply
;
1981 struct relay_stream
*stream
;
1985 DBG("Data pending command received");
1987 if (!session
|| !conn
->version_check_done
) {
1988 ERR("Trying to check for data before version check");
1990 goto end_no_session
;
1993 if (payload
->size
< sizeof(msg
)) {
1994 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1995 sizeof(msg
), payload
->size
);
1997 goto end_no_session
;
1999 memcpy(&msg
, payload
->data
, sizeof(msg
));
2000 msg
.stream_id
= be64toh(msg
.stream_id
);
2001 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
2003 stream
= stream_get_by_id(msg
.stream_id
);
2004 if (stream
== NULL
) {
2009 pthread_mutex_lock(&stream
->lock
);
2011 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
2012 " and last_seq %" PRIu64
, msg
.stream_id
,
2013 stream
->prev_seq
, msg
.last_net_seq_num
);
2015 /* Avoid wrapping issue */
2016 if (((int64_t) (stream
->prev_seq
- msg
.last_net_seq_num
)) >= 0) {
2017 /* Data has in fact been written and is NOT pending */
2020 /* Data still being streamed thus pending */
2024 stream
->data_pending_check_done
= true;
2025 pthread_mutex_unlock(&stream
->lock
);
2030 memset(&reply
, 0, sizeof(reply
));
2031 reply
.ret_code
= htobe32(ret
);
2032 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2033 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2034 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
2044 * Wait for the control socket to reach a quiescent state.
2046 * Note that for now, when receiving this command from the session
2047 * daemon, this means that every subsequent commands or data received on
2048 * the control socket has been handled. So, this is why we simply return
2051 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2052 struct relay_connection
*conn
,
2053 const struct lttng_buffer_view
*payload
)
2057 struct relay_stream
*stream
;
2058 struct lttcomm_relayd_quiescent_control msg
;
2059 struct lttcomm_relayd_generic_reply reply
;
2061 DBG("Checking quiescent state on control socket");
2063 if (!conn
->session
|| !conn
->version_check_done
) {
2064 ERR("Trying to check for data before version check");
2066 goto end_no_session
;
2069 if (payload
->size
< sizeof(msg
)) {
2070 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2071 sizeof(msg
), payload
->size
);
2073 goto end_no_session
;
2075 memcpy(&msg
, payload
->data
, sizeof(msg
));
2076 msg
.stream_id
= be64toh(msg
.stream_id
);
2078 stream
= stream_get_by_id(msg
.stream_id
);
2082 pthread_mutex_lock(&stream
->lock
);
2083 stream
->data_pending_check_done
= true;
2084 pthread_mutex_unlock(&stream
->lock
);
2086 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2089 memset(&reply
, 0, sizeof(reply
));
2090 reply
.ret_code
= htobe32(LTTNG_OK
);
2091 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2092 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2093 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2105 * Initialize a data pending command. This means that a consumer is about
2106 * to ask for data pending for each stream it holds. Simply iterate over
2107 * all streams of a session and set the data_pending_check_done flag.
2109 * This command returns to the client a LTTNG_OK code.
2111 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2112 struct relay_connection
*conn
,
2113 const struct lttng_buffer_view
*payload
)
2117 struct lttng_ht_iter iter
;
2118 struct lttcomm_relayd_begin_data_pending msg
;
2119 struct lttcomm_relayd_generic_reply reply
;
2120 struct relay_stream
*stream
;
2125 DBG("Init streams for data pending");
2127 if (!conn
->session
|| !conn
->version_check_done
) {
2128 ERR("Trying to check for data before version check");
2130 goto end_no_session
;
2133 if (payload
->size
< sizeof(msg
)) {
2134 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2135 sizeof(msg
), payload
->size
);
2137 goto end_no_session
;
2139 memcpy(&msg
, payload
->data
, sizeof(msg
));
2140 msg
.session_id
= be64toh(msg
.session_id
);
2143 * Iterate over all streams to set the begin data pending flag.
2144 * For now, the streams are indexed by stream handle so we have
2145 * to iterate over all streams to find the one associated with
2146 * the right session_id.
2149 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2151 if (!stream_get(stream
)) {
2154 if (stream
->trace
->session
->id
== msg
.session_id
) {
2155 pthread_mutex_lock(&stream
->lock
);
2156 stream
->data_pending_check_done
= false;
2157 pthread_mutex_unlock(&stream
->lock
);
2158 DBG("Set begin data pending flag to stream %" PRIu64
,
2159 stream
->stream_handle
);
2165 memset(&reply
, 0, sizeof(reply
));
2166 /* All good, send back reply. */
2167 reply
.ret_code
= htobe32(LTTNG_OK
);
2169 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2170 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2171 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2183 * End data pending command. This will check, for a given session id, if
2184 * each stream associated with it has its data_pending_check_done flag
2185 * set. If not, this means that the client lost track of the stream but
2186 * the data is still being streamed on our side. In this case, we inform
2187 * the client that data is in flight.
2189 * Return to the client if there is data in flight or not with a ret_code.
2191 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2192 struct relay_connection
*conn
,
2193 const struct lttng_buffer_view
*payload
)
2197 struct lttng_ht_iter iter
;
2198 struct lttcomm_relayd_end_data_pending msg
;
2199 struct lttcomm_relayd_generic_reply reply
;
2200 struct relay_stream
*stream
;
2201 uint32_t is_data_inflight
= 0;
2203 DBG("End data pending command");
2205 if (!conn
->session
|| !conn
->version_check_done
) {
2206 ERR("Trying to check for data before version check");
2208 goto end_no_session
;
2211 if (payload
->size
< sizeof(msg
)) {
2212 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2213 sizeof(msg
), payload
->size
);
2215 goto end_no_session
;
2217 memcpy(&msg
, payload
->data
, sizeof(msg
));
2218 msg
.session_id
= be64toh(msg
.session_id
);
2221 * Iterate over all streams to see if the begin data pending
2225 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2227 if (!stream_get(stream
)) {
2230 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2234 pthread_mutex_lock(&stream
->lock
);
2235 if (!stream
->data_pending_check_done
) {
2236 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
2237 is_data_inflight
= 1;
2238 DBG("Data is still in flight for stream %" PRIu64
,
2239 stream
->stream_handle
);
2240 pthread_mutex_unlock(&stream
->lock
);
2245 pthread_mutex_unlock(&stream
->lock
);
2250 memset(&reply
, 0, sizeof(reply
));
2251 /* All good, send back reply. */
2252 reply
.ret_code
= htobe32(is_data_inflight
);
2254 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2255 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2256 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2268 * Receive an index for a specific stream.
2270 * Return 0 on success else a negative value.
2272 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2273 struct relay_connection
*conn
,
2274 const struct lttng_buffer_view
*payload
)
2278 struct relay_session
*session
= conn
->session
;
2279 struct lttcomm_relayd_index index_info
;
2280 struct relay_index
*index
;
2281 struct lttcomm_relayd_generic_reply reply
;
2282 struct relay_stream
*stream
;
2287 DBG("Relay receiving index");
2289 if (!session
|| !conn
->version_check_done
) {
2290 ERR("Trying to close a stream before version check");
2292 goto end_no_session
;
2295 msg_len
= lttcomm_relayd_index_len(
2296 lttng_to_index_major(conn
->major
, conn
->minor
),
2297 lttng_to_index_minor(conn
->major
, conn
->minor
));
2298 if (payload
->size
< msg_len
) {
2299 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2300 msg_len
, payload
->size
);
2302 goto end_no_session
;
2304 memcpy(&index_info
, payload
->data
, msg_len
);
2305 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2306 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2307 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2308 index_info
.content_size
= be64toh(index_info
.content_size
);
2309 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2310 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2311 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2312 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2314 if (conn
->minor
>= 8) {
2315 index_info
.stream_instance_id
=
2316 be64toh(index_info
.stream_instance_id
);
2317 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2320 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2322 ERR("stream_get_by_id not found");
2326 pthread_mutex_lock(&stream
->lock
);
2328 /* Live beacon handling */
2329 if (index_info
.packet_size
== 0) {
2330 DBG("Received live beacon for stream %" PRIu64
,
2331 stream
->stream_handle
);
2334 * Only flag a stream inactive when it has already
2335 * received data and no indexes are in flight.
2337 if (stream
->index_received_seqcount
> 0
2338 && stream
->indexes_in_flight
== 0) {
2339 stream
->beacon_ts_end
= index_info
.timestamp_end
;
2342 goto end_stream_put
;
2344 stream
->beacon_ts_end
= -1ULL;
2347 if (stream
->ctf_stream_id
== -1ULL) {
2348 stream
->ctf_stream_id
= index_info
.stream_id
;
2350 index
= relay_index_get_by_id_or_create(stream
, index_info
.net_seq_num
);
2353 ERR("relay_index_get_by_id_or_create index NULL");
2354 goto end_stream_put
;
2356 if (set_index_control_data(index
, &index_info
, conn
)) {
2357 ERR("set_index_control_data error");
2358 relay_index_put(index
);
2360 goto end_stream_put
;
2362 ret
= relay_index_try_flush(index
);
2364 tracefile_array_commit_seq(stream
->tfa
);
2365 stream
->index_received_seqcount
++;
2366 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
2367 } else if (ret
> 0) {
2371 ERR("relay_index_try_flush error %d", ret
);
2372 relay_index_put(index
);
2377 pthread_mutex_unlock(&stream
->lock
);
2382 memset(&reply
, 0, sizeof(reply
));
2384 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2386 reply
.ret_code
= htobe32(LTTNG_OK
);
2388 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2389 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2390 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2399 * Receive the streams_sent message.
2401 * Return 0 on success else a negative value.
2403 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2404 struct relay_connection
*conn
,
2405 const struct lttng_buffer_view
*payload
)
2409 struct lttcomm_relayd_generic_reply reply
;
2413 DBG("Relay receiving streams_sent");
2415 if (!conn
->session
|| !conn
->version_check_done
) {
2416 ERR("Trying to close a stream before version check");
2418 goto end_no_session
;
2422 * Publish every pending stream in the connection recv list which are
2423 * now ready to be used by the viewer.
2425 publish_connection_local_streams(conn
);
2427 memset(&reply
, 0, sizeof(reply
));
2428 reply
.ret_code
= htobe32(LTTNG_OK
);
2429 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2430 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2431 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2444 * relay_rotate_session_stream: rotate a stream to a new tracefile for the session
2445 * rotation feature (not the tracefile rotation feature).
2447 static int relay_rotate_session_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
2448 struct relay_connection
*conn
,
2449 const struct lttng_buffer_view
*payload
)
2453 struct relay_session
*session
= conn
->session
;
2454 struct lttcomm_relayd_rotate_stream stream_info
;
2455 struct lttcomm_relayd_generic_reply reply
;
2456 struct relay_stream
*stream
;
2459 struct lttng_buffer_view new_path_view
;
2461 DBG("Rotate stream received");
2463 if (!session
|| !conn
->version_check_done
) {
2464 ERR("Trying to rotate a stream before version check");
2469 if (session
->major
== 2 && session
->minor
< 11) {
2470 ERR("Unsupported feature before 2.11");
2475 header_len
= sizeof(struct lttcomm_relayd_rotate_stream
);
2477 if (payload
->size
< header_len
) {
2478 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2479 header_len
, payload
->size
);
2484 memcpy(&stream_info
, payload
->data
, header_len
);
2486 /* Convert to host */
2487 stream_info
.pathname_length
= be32toh(stream_info
.pathname_length
);
2488 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
2489 stream_info
.new_chunk_id
= be64toh(stream_info
.new_chunk_id
);
2490 stream_info
.rotate_at_seq_num
= be64toh(stream_info
.rotate_at_seq_num
);
2492 path_len
= stream_info
.pathname_length
;
2493 if (payload
->size
< header_len
+ path_len
) {
2494 ERR("Unexpected payload size in \"relay_rotate_session_stream\" including path: expected >= %zu bytes, got %zu bytes",
2495 header_len
+ path_len
, payload
->size
);
2500 /* Ensure it fits in local filename length. */
2501 if (path_len
>= LTTNG_PATH_MAX
) {
2502 ret
= -ENAMETOOLONG
;
2503 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2504 path_len
, LTTNG_PATH_MAX
);
2508 new_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2509 stream_info
.pathname_length
);
2511 stream
= stream_get_by_id(stream_info
.stream_id
);
2517 pthread_mutex_lock(&stream
->lock
);
2520 * Update the trace path (just the folder, the stream name does not
2523 free(stream
->path_name
);
2524 stream
->path_name
= create_output_path(new_path_view
.data
);
2525 if (!stream
->path_name
) {
2526 ERR("Failed to create a new output path");
2528 goto end_stream_unlock
;
2530 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
,
2533 ERR("relay creating output directory");
2535 goto end_stream_unlock
;
2538 assert(stream
->current_chunk_id
.is_set
);
2539 stream
->current_chunk_id
.value
= stream_info
.new_chunk_id
;
2541 if (stream
->is_metadata
) {
2543 * The metadata stream is sent only over the control connection
2544 * so we know we have all the data to perform the stream
2547 ret
= do_rotate_stream(stream
);
2549 stream
->rotate_at_seq_num
= stream_info
.rotate_at_seq_num
;
2550 ret
= try_rotate_stream(stream
);
2553 goto end_stream_unlock
;
2557 pthread_mutex_unlock(&stream
->lock
);
2560 memset(&reply
, 0, sizeof(reply
));
2562 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2564 reply
.ret_code
= htobe32(LTTNG_OK
);
2566 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2567 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2568 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2569 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2579 * relay_mkdir: Create a folder on the disk.
2581 static int relay_mkdir(const struct lttcomm_relayd_hdr
*recv_hdr
,
2582 struct relay_connection
*conn
,
2583 const struct lttng_buffer_view
*payload
)
2586 struct relay_session
*session
= conn
->session
;
2587 struct lttcomm_relayd_mkdir path_info_header
;
2588 struct lttcomm_relayd_generic_reply reply
;
2592 struct lttng_buffer_view path_view
;
2594 if (!session
|| !conn
->version_check_done
) {
2595 ERR("Trying to create a directory before version check");
2597 goto end_no_session
;
2600 if (session
->major
== 2 && session
->minor
< 11) {
2602 * This client is not supposed to use this command since
2603 * it predates its introduction.
2605 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2607 goto end_no_session
;
2610 header_len
= sizeof(path_info_header
);
2611 if (payload
->size
< header_len
) {
2612 ERR("Unexpected payload size in \"relay_mkdir\": expected >= %zu bytes, got %zu bytes",
2613 header_len
, payload
->size
);
2615 goto end_no_session
;
2618 memcpy(&path_info_header
, payload
->data
, header_len
);
2620 path_info_header
.length
= be32toh(path_info_header
.length
);
2622 if (payload
->size
< header_len
+ path_info_header
.length
) {
2623 ERR("Unexpected payload size in \"relay_mkdir\" including path: expected >= %zu bytes, got %zu bytes",
2624 header_len
+ path_info_header
.length
, payload
->size
);
2626 goto end_no_session
;
2629 /* Ensure that it fits in local path length. */
2630 if (path_info_header
.length
>= LTTNG_PATH_MAX
) {
2631 ret
= -ENAMETOOLONG
;
2632 ERR("Path name argument of mkdir command (%" PRIu32
" bytes) exceeds the maximal length allowed (%d bytes)",
2633 path_info_header
.length
, LTTNG_PATH_MAX
);
2637 path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2638 path_info_header
.length
);
2640 path
= create_output_path(path_view
.data
);
2642 ERR("Failed to create output path");
2647 ret
= utils_mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, -1, -1);
2649 ERR("relay creating output directory");
2656 memset(&reply
, 0, sizeof(reply
));
2658 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2660 reply
.ret_code
= htobe32(LTTNG_OK
);
2662 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2663 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2664 ERR("Failed to send \"mkdir\" command reply (ret = %zd)", send_ret
);
2673 static int validate_rotate_rename_path_length(const char *path_type
,
2674 uint32_t path_length
)
2678 if (path_length
> LTTNG_PATH_MAX
) {
2679 ret
= -ENAMETOOLONG
;
2680 ERR("rotate rename \"%s\" path name length (%" PRIu32
" bytes) exceeds the allowed size of %i bytes",
2681 path_type
, path_length
, LTTNG_PATH_MAX
);
2682 } else if (path_length
== 0) {
2684 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type
);
2690 * relay_rotate_rename: rename the trace folder after a rotation is
2691 * completed. We are not closing any fd here, just moving the folder, so it
2692 * works even if data is still in-flight.
2694 static int relay_rotate_rename(const struct lttcomm_relayd_hdr
*recv_hdr
,
2695 struct relay_connection
*conn
,
2696 const struct lttng_buffer_view
*payload
)
2700 struct relay_session
*session
= conn
->session
;
2701 struct lttcomm_relayd_generic_reply reply
;
2702 struct lttcomm_relayd_rotate_rename header
;
2704 size_t received_paths_size
;
2705 char *complete_old_path
= NULL
, *complete_new_path
= NULL
;
2706 struct lttng_buffer_view old_path_view
;
2707 struct lttng_buffer_view new_path_view
;
2709 if (!session
|| !conn
->version_check_done
) {
2710 ERR("Trying to rename a trace folder before version check");
2715 if (session
->major
== 2 && session
->minor
< 11) {
2716 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2721 header_len
= sizeof(header
);
2722 if (payload
->size
< header_len
) {
2723 ERR("Unexpected payload size in \"relay_rotate_rename\": expected >= %zu bytes, got %zu bytes",
2724 header_len
, payload
->size
);
2729 memcpy(&header
, payload
->data
, header_len
);
2731 header
.old_path_length
= be32toh(header
.old_path_length
);
2732 header
.new_path_length
= be32toh(header
.new_path_length
);
2733 received_paths_size
= header
.old_path_length
+ header
.new_path_length
;
2735 if (payload
->size
< header_len
+ received_paths_size
) {
2736 ERR("Unexpected payload size in \"relay_rotate_rename\" including paths: expected >= %zu bytes, got %zu bytes",
2737 header_len
, payload
->size
);
2742 /* Ensure the paths don't exceed their allowed size. */
2743 ret
= validate_rotate_rename_path_length("old", header
.old_path_length
);
2747 ret
= validate_rotate_rename_path_length("new", header
.new_path_length
);
2752 old_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2753 header
.old_path_length
);
2754 new_path_view
= lttng_buffer_view_from_view(payload
,
2755 header_len
+ header
.old_path_length
,
2756 header
.new_path_length
);
2758 /* Validate that both paths received are NULL terminated. */
2759 if (old_path_view
.data
[old_path_view
.size
- 1] != '\0') {
2760 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2764 if (new_path_view
.data
[new_path_view
.size
- 1] != '\0') {
2765 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2770 complete_old_path
= create_output_path(old_path_view
.data
);
2771 if (!complete_old_path
) {
2772 ERR("Failed to build old output path in rotate_rename command");
2777 complete_new_path
= create_output_path(new_path_view
.data
);
2778 if (!complete_new_path
) {
2779 ERR("Failed to build new output path in rotate_rename command");
2784 ret
= utils_mkdir_recursive(complete_new_path
, S_IRWXU
| S_IRWXG
,
2787 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2793 * If a domain has not yet created its channel, the domain-specific
2794 * folder might not exist, but this is not an error.
2796 ret
= rename(complete_old_path
, complete_new_path
);
2797 if (ret
< 0 && errno
!= ENOENT
) {
2798 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2799 complete_old_path
, complete_new_path
);
2805 memset(&reply
, 0, sizeof(reply
));
2807 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2809 reply
.ret_code
= htobe32(LTTNG_OK
);
2811 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2813 if (send_ret
< sizeof(reply
)) {
2814 ERR("Failed to send \"rotate rename\" command reply (ret = %zd)",
2820 free(complete_old_path
);
2821 free(complete_new_path
);
2826 * Check if all the streams in the session have completed the last rotation.
2827 * The chunk_id value is used to distinguish the cases where a stream was
2828 * closed on the consumerd before the rotation started but it still active on
2829 * the relayd, and the case where a stream appeared on the consumerd/relayd
2830 * after the last rotation started (in that case, it is already writing in the
2831 * new chunk folder).
2834 int relay_rotate_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2835 struct relay_connection
*conn
,
2836 const struct lttng_buffer_view
*payload
)
2838 struct relay_session
*session
= conn
->session
;
2839 struct lttcomm_relayd_rotate_pending msg
;
2840 struct lttcomm_relayd_rotate_pending_reply reply
;
2841 struct lttng_ht_iter iter
;
2842 struct relay_stream
*stream
;
2846 bool rotate_pending
= false;
2848 DBG("Rotate pending command received");
2850 if (!session
|| !conn
->version_check_done
) {
2851 ERR("Trying to check for data before version check");
2856 if (session
->major
== 2 && session
->minor
< 11) {
2857 ERR("Unsupported feature before 2.11");
2862 if (payload
->size
< sizeof(msg
)) {
2863 ERR("Unexpected payload size in \"relay_rotate_pending\": expected >= %zu bytes, got %zu bytes",
2864 sizeof(msg
), payload
->size
);
2869 memcpy(&msg
, payload
->data
, sizeof(msg
));
2871 chunk_id
= be64toh(msg
.chunk_id
);
2873 DBG("Evaluating rotate pending for session \"%s\" and chunk id %" PRIu64
,
2874 session
->session_name
, chunk_id
);
2877 * Iterate over all the streams in the session and check if they are
2878 * still waiting for data to perform their rotation.
2881 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2883 if (!stream_get(stream
)) {
2886 if (stream
->trace
->session
!= session
) {
2890 pthread_mutex_lock(&stream
->lock
);
2891 if (stream
->rotate_at_seq_num
!= -1ULL) {
2892 /* We have not yet performed the rotation. */
2893 rotate_pending
= true;
2894 DBG("Stream %" PRIu64
" is still rotating",
2895 stream
->stream_handle
);
2896 } else if (stream
->current_chunk_id
.value
< chunk_id
) {
2898 * Stream closed on the consumer but still active on the
2901 rotate_pending
= true;
2902 DBG("Stream %" PRIu64
" did not exist on the consumer "
2903 "when the last rotation started, but is"
2904 "still waiting for data before getting"
2906 stream
->stream_handle
);
2908 pthread_mutex_unlock(&stream
->lock
);
2910 if (rotate_pending
) {
2917 memset(&reply
, 0, sizeof(reply
));
2918 reply
.generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
);
2919 reply
.is_pending
= (uint8_t) !!rotate_pending
;
2920 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2922 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2923 ERR("Failed to send \"rotate pending\" command reply (ret = %zd)",
2932 #define DBG_CMD(cmd_name, conn) \
2933 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2935 static int relay_process_control_command(struct relay_connection
*conn
,
2936 const struct lttcomm_relayd_hdr
*header
,
2937 const struct lttng_buffer_view
*payload
)
2941 switch (header
->cmd
) {
2942 case RELAYD_CREATE_SESSION
:
2943 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2944 ret
= relay_create_session(header
, conn
, payload
);
2946 case RELAYD_ADD_STREAM
:
2947 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2948 ret
= relay_add_stream(header
, conn
, payload
);
2950 case RELAYD_START_DATA
:
2951 DBG_CMD("RELAYD_START_DATA", conn
);
2952 ret
= relay_start(header
, conn
, payload
);
2954 case RELAYD_SEND_METADATA
:
2955 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2956 ret
= relay_recv_metadata(header
, conn
, payload
);
2958 case RELAYD_VERSION
:
2959 DBG_CMD("RELAYD_VERSION", conn
);
2960 ret
= relay_send_version(header
, conn
, payload
);
2962 case RELAYD_CLOSE_STREAM
:
2963 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2964 ret
= relay_close_stream(header
, conn
, payload
);
2966 case RELAYD_DATA_PENDING
:
2967 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2968 ret
= relay_data_pending(header
, conn
, payload
);
2970 case RELAYD_QUIESCENT_CONTROL
:
2971 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2972 ret
= relay_quiescent_control(header
, conn
, payload
);
2974 case RELAYD_BEGIN_DATA_PENDING
:
2975 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2976 ret
= relay_begin_data_pending(header
, conn
, payload
);
2978 case RELAYD_END_DATA_PENDING
:
2979 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2980 ret
= relay_end_data_pending(header
, conn
, payload
);
2982 case RELAYD_SEND_INDEX
:
2983 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2984 ret
= relay_recv_index(header
, conn
, payload
);
2986 case RELAYD_STREAMS_SENT
:
2987 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2988 ret
= relay_streams_sent(header
, conn
, payload
);
2990 case RELAYD_RESET_METADATA
:
2991 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2992 ret
= relay_reset_metadata(header
, conn
, payload
);
2994 case RELAYD_ROTATE_STREAM
:
2995 DBG_CMD("RELAYD_ROTATE_STREAM", conn
);
2996 ret
= relay_rotate_session_stream(header
, conn
, payload
);
2998 case RELAYD_ROTATE_RENAME
:
2999 DBG_CMD("RELAYD_ROTATE_RENAME", conn
);
3000 ret
= relay_rotate_rename(header
, conn
, payload
);
3002 case RELAYD_ROTATE_PENDING
:
3003 DBG_CMD("RELAYD_ROTATE_PENDING", conn
);
3004 ret
= relay_rotate_pending(header
, conn
, payload
);
3007 DBG_CMD("RELAYD_MKDIR", conn
);
3008 ret
= relay_mkdir(header
, conn
, payload
);
3010 case RELAYD_UPDATE_SYNC_INFO
:
3012 ERR("Received unknown command (%u)", header
->cmd
);
3013 relay_unknown_command(conn
);
3022 static enum relay_connection_status
relay_process_control_receive_payload(
3023 struct relay_connection
*conn
)
3026 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3027 struct lttng_dynamic_buffer
*reception_buffer
=
3028 &conn
->protocol
.ctrl
.reception_buffer
;
3029 struct ctrl_connection_state_receive_payload
*state
=
3030 &conn
->protocol
.ctrl
.state
.receive_payload
;
3031 struct lttng_buffer_view payload_view
;
3033 if (state
->left_to_receive
== 0) {
3034 /* Short-circuit for payload-less commands. */
3035 goto reception_complete
;
3038 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3039 reception_buffer
->data
+ state
->received
,
3040 state
->left_to_receive
, MSG_DONTWAIT
);
3042 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3043 PERROR("Unable to receive command payload on sock %d",
3045 status
= RELAY_CONNECTION_STATUS_ERROR
;
3048 } else if (ret
== 0) {
3049 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3050 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3055 assert(ret
<= state
->left_to_receive
);
3057 state
->left_to_receive
-= ret
;
3058 state
->received
+= ret
;
3060 if (state
->left_to_receive
> 0) {
3062 * Can't transition to the protocol's next state, wait to
3063 * receive the rest of the header.
3065 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3066 state
->received
, state
->left_to_receive
,
3072 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3073 conn
->sock
->fd
, state
->received
);
3075 * The payload required to process the command has been received.
3076 * A view to the reception buffer is forwarded to the various
3077 * commands and the state of the control is reset on success.
3079 * Commands are responsible for sending their reply to the peer.
3081 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3083 ret
= relay_process_control_command(conn
,
3084 &state
->header
, &payload_view
);
3086 status
= RELAY_CONNECTION_STATUS_ERROR
;
3090 ret
= connection_reset_protocol_state(conn
);
3092 status
= RELAY_CONNECTION_STATUS_ERROR
;
3098 static enum relay_connection_status
relay_process_control_receive_header(
3099 struct relay_connection
*conn
)
3102 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3103 struct lttcomm_relayd_hdr header
;
3104 struct lttng_dynamic_buffer
*reception_buffer
=
3105 &conn
->protocol
.ctrl
.reception_buffer
;
3106 struct ctrl_connection_state_receive_header
*state
=
3107 &conn
->protocol
.ctrl
.state
.receive_header
;
3109 assert(state
->left_to_receive
!= 0);
3111 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3112 reception_buffer
->data
+ state
->received
,
3113 state
->left_to_receive
, MSG_DONTWAIT
);
3115 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3116 PERROR("Unable to receive control command header on sock %d",
3118 status
= RELAY_CONNECTION_STATUS_ERROR
;
3121 } else if (ret
== 0) {
3122 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3123 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3128 assert(ret
<= state
->left_to_receive
);
3130 state
->left_to_receive
-= ret
;
3131 state
->received
+= ret
;
3133 if (state
->left_to_receive
> 0) {
3135 * Can't transition to the protocol's next state, wait to
3136 * receive the rest of the header.
3138 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3139 state
->received
, state
->left_to_receive
,
3144 /* Transition to next state: receiving the command's payload. */
3145 conn
->protocol
.ctrl
.state_id
=
3146 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3147 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3148 header
.circuit_id
= be64toh(header
.circuit_id
);
3149 header
.data_size
= be64toh(header
.data_size
);
3150 header
.cmd
= be32toh(header
.cmd
);
3151 header
.cmd_version
= be32toh(header
.cmd_version
);
3152 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3153 &header
, sizeof(header
));
3155 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3156 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3159 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3160 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3162 status
= RELAY_CONNECTION_STATUS_ERROR
;
3166 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3168 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3169 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3172 status
= RELAY_CONNECTION_STATUS_ERROR
;
3176 if (header
.data_size
== 0) {
3178 * Manually invoke the next state as the poll loop
3179 * will not wake-up to allow us to proceed further.
3181 status
= relay_process_control_receive_payload(conn
);
3188 * Process the commands received on the control socket
3190 static enum relay_connection_status
relay_process_control(
3191 struct relay_connection
*conn
)
3193 enum relay_connection_status status
;
3195 switch (conn
->protocol
.ctrl
.state_id
) {
3196 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3197 status
= relay_process_control_receive_header(conn
);
3199 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3200 status
= relay_process_control_receive_payload(conn
);
3203 ERR("Unknown control connection protocol state encountered.");
3211 * Handle index for a data stream.
3213 * Called with the stream lock held.
3215 * Return 0 on success else a negative value.
3217 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
3218 bool rotate_index
, bool *flushed
, uint64_t total_size
)
3221 uint64_t data_offset
;
3222 struct relay_index
*index
;
3224 /* Get data offset because we are about to update the index. */
3225 data_offset
= htobe64(stream
->tracefile_size_current
);
3227 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
3228 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
3231 * Lookup for an existing index for that stream id/sequence
3232 * number. If it exists, the control thread has already received the
3233 * data for it, thus we need to write it to disk.
3235 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
3241 if (rotate_index
|| !stream
->index_file
) {
3242 ret
= create_rotate_index_file(stream
);
3244 ERR("Failed to rotate index");
3245 /* Put self-ref for this index due to error. */
3246 relay_index_put(index
);
3252 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
3254 /* Put self-ref for this index due to error. */
3255 relay_index_put(index
);
3260 ret
= relay_index_try_flush(index
);
3262 tracefile_array_commit_seq(stream
->tfa
);
3263 stream
->index_received_seqcount
++;
3265 } else if (ret
> 0) {
3266 index
->total_size
= total_size
;
3270 /* Put self-ref for this index due to error. */
3271 relay_index_put(index
);
3279 static enum relay_connection_status
relay_process_data_receive_header(
3280 struct relay_connection
*conn
)
3283 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3284 struct data_connection_state_receive_header
*state
=
3285 &conn
->protocol
.data
.state
.receive_header
;
3286 struct lttcomm_relayd_data_hdr header
;
3287 struct relay_stream
*stream
;
3289 assert(state
->left_to_receive
!= 0);
3291 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3292 state
->header_reception_buffer
+ state
->received
,
3293 state
->left_to_receive
, MSG_DONTWAIT
);
3295 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3296 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3297 status
= RELAY_CONNECTION_STATUS_ERROR
;
3300 } else if (ret
== 0) {
3301 /* Orderly shutdown. Not necessary to print an error. */
3302 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3303 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3308 assert(ret
<= state
->left_to_receive
);
3310 state
->left_to_receive
-= ret
;
3311 state
->received
+= ret
;
3313 if (state
->left_to_receive
> 0) {
3315 * Can't transition to the protocol's next state, wait to
3316 * receive the rest of the header.
3318 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3319 state
->received
, state
->left_to_receive
,
3324 /* Transition to next state: receiving the payload. */
3325 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3327 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3328 header
.circuit_id
= be64toh(header
.circuit_id
);
3329 header
.stream_id
= be64toh(header
.stream_id
);
3330 header
.data_size
= be32toh(header
.data_size
);
3331 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3332 header
.padding_size
= be32toh(header
.padding_size
);
3333 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3335 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3337 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3338 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3340 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3341 conn
->sock
->fd
, header
.circuit_id
,
3342 header
.stream_id
, header
.data_size
,
3343 header
.net_seq_num
, header
.padding_size
);
3345 stream
= stream_get_by_id(header
.stream_id
);
3347 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3349 /* Protocol error. */
3350 status
= RELAY_CONNECTION_STATUS_ERROR
;
3354 pthread_mutex_lock(&stream
->lock
);
3356 /* Check if a rotation is needed. */
3357 if (stream
->tracefile_size
> 0 &&
3358 (stream
->tracefile_size_current
+ header
.data_size
) >
3359 stream
->tracefile_size
) {
3360 uint64_t old_id
, new_id
;
3362 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
3363 tracefile_array_file_rotate(stream
->tfa
);
3365 /* new_id is updated by utils_rotate_stream_file. */
3368 ret
= utils_rotate_stream_file(stream
->path_name
,
3369 stream
->channel_name
, stream
->tracefile_size
,
3370 stream
->tracefile_count
, -1,
3371 -1, stream
->stream_fd
->fd
,
3372 &new_id
, &stream
->stream_fd
->fd
);
3374 ERR("Failed to rotate stream output file");
3375 status
= RELAY_CONNECTION_STATUS_ERROR
;
3376 goto end_stream_unlock
;
3380 * Reset current size because we just performed a stream
3383 stream
->tracefile_size_current
= 0;
3384 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= true;
3388 pthread_mutex_unlock(&stream
->lock
);
3394 static enum relay_connection_status
relay_process_data_receive_payload(
3395 struct relay_connection
*conn
)
3398 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3399 struct relay_stream
*stream
;
3400 struct data_connection_state_receive_payload
*state
=
3401 &conn
->protocol
.data
.state
.receive_payload
;
3402 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3403 char data_buffer
[chunk_size
];
3404 bool partial_recv
= false;
3405 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3406 uint64_t left_to_receive
= state
->left_to_receive
;
3407 struct relay_session
*session
;
3409 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3410 state
->header
.stream_id
, state
->header
.net_seq_num
,
3411 state
->received
, left_to_receive
);
3413 stream
= stream_get_by_id(state
->header
.stream_id
);
3415 /* Protocol error. */
3416 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3417 state
->header
.stream_id
);
3418 status
= RELAY_CONNECTION_STATUS_ERROR
;
3422 pthread_mutex_lock(&stream
->lock
);
3423 session
= stream
->trace
->session
;
3424 if (!conn
->session
) {
3425 ret
= connection_set_session(conn
, session
);
3427 status
= RELAY_CONNECTION_STATUS_ERROR
;
3428 goto end_stream_unlock
;
3433 * The size of the "chunk" received on any iteration is bounded by:
3434 * - the data left to receive,
3435 * - the data immediately available on the socket,
3436 * - the on-stack data buffer
3438 while (left_to_receive
> 0 && !partial_recv
) {
3440 size_t recv_size
= min(left_to_receive
, chunk_size
);
3442 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3443 recv_size
, MSG_DONTWAIT
);
3445 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3446 PERROR("Socket %d error", conn
->sock
->fd
);
3447 status
= RELAY_CONNECTION_STATUS_ERROR
;
3449 goto end_stream_unlock
;
3450 } else if (ret
== 0) {
3451 /* No more data ready to be consumed on socket. */
3452 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3453 state
->header
.stream_id
);
3454 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3456 } else if (ret
< (int) recv_size
) {
3458 * All the data available on the socket has been
3461 partial_recv
= true;
3466 /* Write data to stream output fd. */
3467 write_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
3469 if (write_ret
< (ssize_t
) recv_size
) {
3470 ERR("Relay error writing data to file");
3471 status
= RELAY_CONNECTION_STATUS_ERROR
;
3472 goto end_stream_unlock
;
3475 left_to_receive
-= recv_size
;
3476 state
->received
+= recv_size
;
3477 state
->left_to_receive
= left_to_receive
;
3479 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
3480 write_ret
, stream
->stream_handle
);
3483 if (state
->left_to_receive
> 0) {
3485 * Did not receive all the data expected, wait for more data to
3486 * become available on the socket.
3488 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3489 state
->header
.stream_id
, state
->received
,
3490 state
->left_to_receive
);
3491 goto end_stream_unlock
;
3494 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
3495 state
->header
.padding_size
);
3496 if ((int64_t) ret
< (int64_t) state
->header
.padding_size
) {
3497 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3498 stream
->stream_handle
,
3499 state
->header
.net_seq_num
, ret
);
3500 status
= RELAY_CONNECTION_STATUS_ERROR
;
3501 goto end_stream_unlock
;
3505 if (session
->minor
>= 4 && !session
->snapshot
) {
3506 ret
= handle_index_data(stream
, state
->header
.net_seq_num
,
3507 state
->rotate_index
, &index_flushed
, state
->header
.data_size
+ state
->header
.padding_size
);
3509 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3510 stream
->stream_handle
,
3511 state
->header
.net_seq_num
, ret
);
3512 status
= RELAY_CONNECTION_STATUS_ERROR
;
3513 goto end_stream_unlock
;
3517 stream
->tracefile_size_current
+= state
->header
.data_size
+
3518 state
->header
.padding_size
;
3520 if (stream
->prev_seq
== -1ULL) {
3523 if (index_flushed
) {
3524 stream
->pos_after_last_complete_data_index
=
3525 stream
->tracefile_size_current
;
3528 stream
->prev_seq
= state
->header
.net_seq_num
;
3531 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3532 * contents of *state which are aliased (union) to the same location as
3533 * the new state. Don't use it beyond this point.
3535 connection_reset_protocol_state(conn
);
3538 ret
= try_rotate_stream(stream
);
3540 status
= RELAY_CONNECTION_STATUS_ERROR
;
3541 goto end_stream_unlock
;
3545 close_requested
= stream
->close_requested
;
3546 pthread_mutex_unlock(&stream
->lock
);
3547 if (close_requested
&& left_to_receive
== 0) {
3548 try_stream_close(stream
);
3552 pthread_mutex_lock(&session
->lock
);
3553 uatomic_set(&session
->new_streams
, 1);
3554 pthread_mutex_unlock(&session
->lock
);
3563 * relay_process_data: Process the data received on the data socket
3565 static enum relay_connection_status
relay_process_data(
3566 struct relay_connection
*conn
)
3568 enum relay_connection_status status
;
3570 switch (conn
->protocol
.data
.state_id
) {
3571 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3572 status
= relay_process_data_receive_header(conn
);
3574 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3575 status
= relay_process_data_receive_payload(conn
);
3578 ERR("Unexpected data connection communication state.");
3585 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3589 (void) lttng_poll_del(events
, pollfd
);
3591 ret
= close(pollfd
);
3593 ERR("Closing pollfd %d", pollfd
);
3597 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3598 int pollfd
, struct relay_connection
*conn
)
3600 const char *type_str
;
3602 switch (conn
->type
) {
3607 type_str
= "Control";
3609 case RELAY_VIEWER_COMMAND
:
3610 type_str
= "Viewer Command";
3612 case RELAY_VIEWER_NOTIFICATION
:
3613 type_str
= "Viewer Notification";
3616 type_str
= "Unknown";
3618 cleanup_connection_pollfd(events
, pollfd
);
3619 connection_put(conn
);
3620 DBG("%s connection closed with %d", type_str
, pollfd
);
3624 * This thread does the actual work
3626 static void *relay_thread_worker(void *data
)
3628 int ret
, err
= -1, last_seen_data_fd
= -1;
3630 struct lttng_poll_event events
;
3631 struct lttng_ht
*relay_connections_ht
;
3632 struct lttng_ht_iter iter
;
3633 struct relay_connection
*destroy_conn
= NULL
;
3635 DBG("[thread] Relay worker started");
3637 rcu_register_thread();
3639 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3641 if (testpoint(relayd_thread_worker
)) {
3642 goto error_testpoint
;
3645 health_code_update();
3647 /* table of connections indexed on socket */
3648 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3649 if (!relay_connections_ht
) {
3650 goto relay_connections_ht_error
;
3653 ret
= create_named_thread_poll_set(&events
, 2, "Worker thread epoll");
3655 goto error_poll_create
;
3658 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3665 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3667 health_code_update();
3669 /* Infinite blocking call, waiting for transmission */
3670 DBG3("Relayd worker thread polling...");
3671 health_poll_entry();
3672 ret
= lttng_poll_wait(&events
, -1);
3676 * Restart interrupted system call.
3678 if (errno
== EINTR
) {
3687 * Process control. The control connection is
3688 * prioritized so we don't starve it with high
3689 * throughput tracing data on the data connection.
3691 for (i
= 0; i
< nb_fd
; i
++) {
3692 /* Fetch once the poll data */
3693 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3694 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3696 health_code_update();
3700 * No activity for this FD (poll
3706 /* Thread quit pipe has been closed. Killing thread. */
3707 ret
= check_thread_quit_pipe(pollfd
, revents
);
3713 /* Inspect the relay conn pipe for new connection */
3714 if (pollfd
== relay_conn_pipe
[0]) {
3715 if (revents
& LPOLLIN
) {
3716 struct relay_connection
*conn
;
3718 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3722 lttng_poll_add(&events
, conn
->sock
->fd
,
3723 LPOLLIN
| LPOLLRDHUP
);
3724 connection_ht_add(relay_connections_ht
, conn
);
3725 DBG("Connection socket %d added", conn
->sock
->fd
);
3726 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3727 ERR("Relay connection pipe error");
3730 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3734 struct relay_connection
*ctrl_conn
;
3736 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3737 /* If not found, there is a synchronization issue. */
3740 if (ctrl_conn
->type
== RELAY_DATA
) {
3741 if (revents
& LPOLLIN
) {
3743 * Flag the last seen data fd not deleted. It will be
3744 * used as the last seen fd if any fd gets deleted in
3747 last_notdel_data_fd
= pollfd
;
3749 goto put_ctrl_connection
;
3751 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3753 if (revents
& LPOLLIN
) {
3754 enum relay_connection_status status
;
3756 status
= relay_process_control(ctrl_conn
);
3757 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3759 * On socket error flag the session as aborted to force
3760 * the cleanup of its stream otherwise it can leak
3761 * during the lifetime of the relayd.
3763 * This prevents situations in which streams can be
3764 * left opened because an index was received, the
3765 * control connection is closed, and the data
3766 * connection is closed (uncleanly) before the packet's
3769 * Since the control connection encountered an error,
3770 * it is okay to be conservative and close the
3771 * session right now as we can't rely on the protocol
3772 * being respected anymore.
3774 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3775 session_abort(ctrl_conn
->session
);
3778 /* Clear the connection on error or close. */
3779 relay_thread_close_connection(&events
,
3784 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3785 relay_thread_close_connection(&events
,
3787 if (last_seen_data_fd
== pollfd
) {
3788 last_seen_data_fd
= last_notdel_data_fd
;
3791 ERR("Unexpected poll events %u for control sock %d",
3793 connection_put(ctrl_conn
);
3796 put_ctrl_connection
:
3797 connection_put(ctrl_conn
);
3802 * The last loop handled a control request, go back to poll to make
3803 * sure we prioritise the control socket.
3809 if (last_seen_data_fd
>= 0) {
3810 for (i
= 0; i
< nb_fd
; i
++) {
3811 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3813 health_code_update();
3815 if (last_seen_data_fd
== pollfd
) {
3822 /* Process data connection. */
3823 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3824 /* Fetch the poll data. */
3825 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3826 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3827 struct relay_connection
*data_conn
;
3829 health_code_update();
3832 /* No activity for this FD (poll implementation). */
3836 /* Skip the command pipe. It's handled in the first loop. */
3837 if (pollfd
== relay_conn_pipe
[0]) {
3841 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3843 /* Skip it. Might be removed before. */
3846 if (data_conn
->type
== RELAY_CONTROL
) {
3847 goto put_data_connection
;
3849 assert(data_conn
->type
== RELAY_DATA
);
3851 if (revents
& LPOLLIN
) {
3852 enum relay_connection_status status
;
3854 status
= relay_process_data(data_conn
);
3855 /* Connection closed or error. */
3856 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3858 * On socket error flag the session as aborted to force
3859 * the cleanup of its stream otherwise it can leak
3860 * during the lifetime of the relayd.
3862 * This prevents situations in which streams can be
3863 * left opened because an index was received, the
3864 * control connection is closed, and the data
3865 * connection is closed (uncleanly) before the packet's
3868 * Since the data connection encountered an error,
3869 * it is okay to be conservative and close the
3870 * session right now as we can't rely on the protocol
3871 * being respected anymore.
3873 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3874 session_abort(data_conn
->session
);
3876 relay_thread_close_connection(&events
, pollfd
,
3879 * Every goto restart call sets the last seen fd where
3880 * here we don't really care since we gracefully
3881 * continue the loop after the connection is deleted.
3884 /* Keep last seen port. */
3885 last_seen_data_fd
= pollfd
;
3886 connection_put(data_conn
);
3889 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3890 relay_thread_close_connection(&events
, pollfd
,
3893 ERR("Unknown poll events %u for data sock %d",
3896 put_data_connection
:
3897 connection_put(data_conn
);
3899 last_seen_data_fd
= -1;
3902 /* Normal exit, no error */
3907 /* Cleanup remaining connection object. */
3909 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3912 health_code_update();
3914 session_abort(destroy_conn
->session
);
3917 * No need to grab another ref, because we own
3920 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3925 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
3927 lttng_ht_destroy(relay_connections_ht
);
3928 relay_connections_ht_error
:
3929 /* Close relay conn pipes */
3930 (void) fd_tracker_util_pipe_close(the_fd_tracker
,
3933 DBG("Thread exited with error");
3935 DBG("Worker thread cleanup complete");
3939 ERR("Health error occurred in %s", __func__
);
3941 health_unregister(health_relayd
);
3942 rcu_unregister_thread();
3943 lttng_relay_stop_threads();
3948 * Create the relay command pipe to wake thread_manage_apps.
3949 * Closed in cleanup().
3951 static int create_relay_conn_pipe(void)
3953 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
3954 "Relayd connection pipe", relay_conn_pipe
);
3960 int main(int argc
, char **argv
)
3962 int ret
= 0, retval
= 0;
3965 /* Parse arguments */
3967 if (set_options(argc
, argv
)) {
3972 if (set_signal_handler()) {
3977 ret
= fclose(stdin
);
3979 PERROR("Failed to close stdin");
3982 /* Try to create directory if -o, --output is specified. */
3983 if (opt_output_path
) {
3984 if (*opt_output_path
!= '/') {
3985 ERR("Please specify an absolute path for -o, --output PATH");
3990 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3993 ERR("Unable to create %s", opt_output_path
);
4000 if (opt_daemon
|| opt_background
) {
4001 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4010 * The RCU thread registration (and use, through the fd-tracker's
4011 * creation) is done after the daemonization to allow us to not
4012 * deal with liburcu's fork() management as the call RCU needs to
4015 rcu_register_thread();
4017 the_fd_tracker
= fd_tracker_create(lttng_opt_fd_cap
);
4018 if (!the_fd_tracker
) {
4023 /* Initialize thread health monitoring */
4024 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
4025 if (!health_relayd
) {
4026 PERROR("health_app_create error");
4028 goto exit_health_app_create
;
4031 /* Create thread quit pipe */
4032 if (init_thread_quit_pipe()) {
4034 goto exit_init_data
;
4037 /* Setup the thread apps communication pipe. */
4038 if (create_relay_conn_pipe()) {
4040 goto exit_init_data
;
4043 /* Init relay command queue. */
4044 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
4046 /* Initialize communication library */
4048 lttcomm_inet_init();
4050 /* tables of sessions indexed by session ID */
4051 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4054 goto exit_init_data
;
4057 /* tables of streams indexed by stream ID */
4058 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4059 if (!relay_streams_ht
) {
4061 goto exit_init_data
;
4064 /* tables of streams indexed by stream ID */
4065 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4066 if (!viewer_streams_ht
) {
4068 goto exit_init_data
;
4071 ret
= init_health_quit_pipe();
4074 goto exit_health_quit_pipe
;
4077 /* Create thread to manage the client socket */
4078 ret
= pthread_create(&health_thread
, default_pthread_attr(),
4079 thread_manage_health
, (void *) NULL
);
4082 PERROR("pthread_create health");
4084 goto exit_health_thread
;
4087 /* Setup the dispatcher thread */
4088 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
4089 relay_thread_dispatcher
, (void *) NULL
);
4092 PERROR("pthread_create dispatcher");
4094 goto exit_dispatcher_thread
;
4097 /* Setup the worker thread */
4098 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
4099 relay_thread_worker
, NULL
);
4102 PERROR("pthread_create worker");
4104 goto exit_worker_thread
;
4107 /* Setup the listener thread */
4108 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
4109 relay_thread_listener
, (void *) NULL
);
4112 PERROR("pthread_create listener");
4114 goto exit_listener_thread
;
4117 ret
= relayd_live_create(live_uri
);
4119 ERR("Starting live viewer threads");
4125 * This is where we start awaiting program completion (e.g. through
4126 * signal that asks threads to teardown).
4129 ret
= relayd_live_join();
4135 ret
= pthread_join(listener_thread
, &status
);
4138 PERROR("pthread_join listener_thread");
4142 exit_listener_thread
:
4143 ret
= pthread_join(worker_thread
, &status
);
4146 PERROR("pthread_join worker_thread");
4151 ret
= pthread_join(dispatcher_thread
, &status
);
4154 PERROR("pthread_join dispatcher_thread");
4157 exit_dispatcher_thread
:
4159 ret
= pthread_join(health_thread
, &status
);
4162 PERROR("pthread_join health_thread");
4167 (void) fd_tracker_util_pipe_close(the_fd_tracker
, health_quit_pipe
);
4168 exit_health_quit_pipe
:
4171 health_app_destroy(health_relayd
);
4172 exit_health_app_create
:
4175 * Wait for all pending call_rcu work to complete before tearing
4176 * down data structures. call_rcu worker may be trying to
4177 * perform lookups in those structures.
4182 /* Ensure all prior call_rcu are done. */
4185 fd_tracker_destroy(the_fd_tracker
);
4186 rcu_unregister_thread();