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>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/daemonize.h>
50 #include <common/futex.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/sessiond-comm/inet.h>
53 #include <common/sessiond-comm/relayd.h>
54 #include <common/uri.h>
55 #include <common/utils.h>
56 #include <common/config/config.h>
59 #include "ctf-trace.h"
62 #include "lttng-relayd.h"
64 #include "health-relayd.h"
65 #include "testpoint.h"
66 #include "viewer-stream.h"
69 #include "connection.h"
71 /* command line options */
72 char *opt_output_path
;
73 static int opt_daemon
, opt_background
;
76 * We need to wait for listener and live listener threads, as well as
77 * health check thread, before being ready to signal readiness.
79 #define NR_LTTNG_RELAY_READY 3
80 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
81 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
82 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
84 static struct lttng_uri
*control_uri
;
85 static struct lttng_uri
*data_uri
;
86 static struct lttng_uri
*live_uri
;
90 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
91 static int tracing_group_name_override
;
93 const char * const config_section_name
= "relayd";
96 * Quit pipe for all threads. This permits a single cancellation point
97 * for all threads when receiving an event on the pipe.
99 int thread_quit_pipe
[2] = { -1, -1 };
102 * This pipe is used to inform the worker thread that a command is queued and
103 * ready to be processed.
105 static int relay_conn_pipe
[2] = { -1, -1 };
107 /* Shared between threads */
108 static int dispatch_thread_exit
;
110 static pthread_t listener_thread
;
111 static pthread_t dispatcher_thread
;
112 static pthread_t worker_thread
;
113 static pthread_t health_thread
;
115 static uint64_t last_relay_stream_id
;
118 * Relay command queue.
120 * The relay_thread_listener and relay_thread_dispatcher communicate with this
123 static struct relay_conn_queue relay_conn_queue
;
125 /* buffer allocated at startup, used to store the trace data */
126 static char *data_buffer
;
127 static unsigned int data_buffer_size
;
129 /* We need those values for the file/dir creation. */
130 static uid_t relayd_uid
;
131 static gid_t relayd_gid
;
133 /* Global relay stream hash table. */
134 struct lttng_ht
*relay_streams_ht
;
136 /* Global relay viewer stream hash table. */
137 struct lttng_ht
*viewer_streams_ht
;
139 /* Global hash table that stores relay index object. */
140 struct lttng_ht
*indexes_ht
;
142 /* Relayd health monitoring */
143 struct health_app
*health_relayd
;
145 static struct option long_options
[] = {
146 { "control-port", 1, 0, 'C', },
147 { "data-port", 1, 0, 'D', },
148 { "live-port", 1, 0, 'L', },
149 { "daemonize", 0, 0, 'd', },
150 { "background", 0, 0, 'b', },
151 { "group", 1, 0, 'g', },
152 { "help", 0, 0, 'h', },
153 { "output", 1, 0, 'o', },
154 { "verbose", 0, 0, 'v', },
155 { "config", 1, 0, 'f' },
159 static const char *config_ignore_options
[] = { "help", "config" };
162 * usage function on stderr
167 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
168 fprintf(stderr
, " -h, --help Display this usage.\n");
169 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
170 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
171 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
172 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
173 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
174 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
175 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
176 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
177 fprintf(stderr
, " -f --config Load daemon configuration file\n");
181 * Take an option from the getopt output and set it in the right variable to be
184 * Return 0 on success else a negative value.
187 int set_option(int opt
, const char *arg
, const char *optname
)
193 fprintf(stderr
, "option %s", optname
);
195 fprintf(stderr
, " with arg %s\n", arg
);
199 ret
= uri_parse(arg
, &control_uri
);
201 ERR("Invalid control URI specified");
204 if (control_uri
->port
== 0) {
205 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
209 ret
= uri_parse(arg
, &data_uri
);
211 ERR("Invalid data URI specified");
214 if (data_uri
->port
== 0) {
215 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
219 ret
= uri_parse(arg
, &live_uri
);
221 ERR("Invalid live URI specified");
224 if (live_uri
->port
== 0) {
225 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
235 tracing_group_name
= strdup(arg
);
236 if (tracing_group_name
== NULL
) {
241 tracing_group_name_override
= 1;
247 ret
= asprintf(&opt_output_path
, "%s", arg
);
250 PERROR("asprintf opt_output_path");
255 /* Verbose level can increase using multiple -v */
257 lttng_opt_verbose
= config_parse_value(arg
);
259 /* Only 3 level of verbosity (-vvv). */
260 if (lttng_opt_verbose
< 3) {
261 lttng_opt_verbose
+= 1;
266 /* Unknown option or other error.
267 * Error is printed by getopt, just return */
280 * config_entry_handler_cb used to handle options read from a config file.
281 * See config_entry_handler_cb comment in common/config/config.h for the
282 * return value conventions.
285 int config_entry_handler(const struct config_entry
*entry
, void *unused
)
289 if (!entry
|| !entry
->name
|| !entry
->value
) {
294 /* Check if the option is to be ignored */
295 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
296 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
301 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
302 /* Ignore if entry name is not fully matched. */
303 if (strcmp(entry
->name
, long_options
[i
].name
)) {
308 * If the option takes no argument on the command line, we have to
309 * check if the value is "true". We support non-zero numeric values,
312 if (!long_options
[i
].has_arg
) {
313 ret
= config_parse_value(entry
->value
);
316 WARN("Invalid configuration value \"%s\" for option %s",
317 entry
->value
, entry
->name
);
319 /* False, skip boolean config option. */
324 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
328 WARN("Unrecognized option \"%s\" in daemon configuration file.",
336 int set_options(int argc
, char **argv
)
338 int c
, ret
= 0, option_index
= 0;
339 int orig_optopt
= optopt
, orig_optind
= optind
;
340 char *default_address
, *optstring
;
341 const char *config_path
= NULL
;
343 optstring
= utils_generate_optstring(long_options
,
344 sizeof(long_options
) / sizeof(struct option
));
350 /* Check for the --config option */
352 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
353 &option_index
)) != -1) {
357 } else if (c
!= 'f') {
361 config_path
= utils_expand_path(optarg
);
363 ERR("Failed to resolve path: %s", optarg
);
367 ret
= config_get_section_entries(config_path
, config_section_name
,
368 config_entry_handler
, NULL
);
371 ERR("Invalid configuration option at line %i", ret
);
377 /* Reset getopt's global state */
378 optopt
= orig_optopt
;
379 optind
= orig_optind
;
381 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
386 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
392 /* assign default values */
393 if (control_uri
== NULL
) {
394 ret
= asprintf(&default_address
,
395 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
396 DEFAULT_NETWORK_CONTROL_PORT
);
398 PERROR("asprintf default data address");
402 ret
= uri_parse(default_address
, &control_uri
);
403 free(default_address
);
405 ERR("Invalid control URI specified");
409 if (data_uri
== NULL
) {
410 ret
= asprintf(&default_address
,
411 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
412 DEFAULT_NETWORK_DATA_PORT
);
414 PERROR("asprintf default data address");
418 ret
= uri_parse(default_address
, &data_uri
);
419 free(default_address
);
421 ERR("Invalid data URI specified");
425 if (live_uri
== NULL
) {
426 ret
= asprintf(&default_address
,
427 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
428 DEFAULT_NETWORK_VIEWER_PORT
);
430 PERROR("asprintf default viewer control address");
434 ret
= uri_parse(default_address
, &live_uri
);
435 free(default_address
);
437 ERR("Invalid viewer control URI specified");
455 /* free the dynamically allocated opt_output_path */
456 free(opt_output_path
);
458 /* Close thread quit pipes */
459 utils_close_pipe(thread_quit_pipe
);
461 uri_free(control_uri
);
463 /* Live URI is freed in the live thread. */
465 if (tracing_group_name_override
) {
466 free((void *) tracing_group_name
);
471 * Write to writable pipe used to notify a thread.
474 int notify_thread_pipe(int wpipe
)
478 ret
= lttng_write(wpipe
, "!", 1);
480 PERROR("write poll pipe");
486 static void notify_health_quit_pipe(int *pipe
)
490 ret
= lttng_write(pipe
[1], "4", 1);
492 PERROR("write relay health quit");
497 * Stop all threads by closing the thread quit pipe.
500 void stop_threads(void)
504 /* Stopping all threads */
505 DBG("Terminating all threads");
506 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
508 ERR("write error on thread quit pipe");
511 notify_health_quit_pipe(health_quit_pipe
);
513 /* Dispatch thread */
514 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
515 futex_nto1_wake(&relay_conn_queue
.futex
);
519 * Signal handler for the daemon
521 * Simply stop all worker threads, leaving main() return gracefully after
522 * joining all threads and calling cleanup().
525 void sighandler(int sig
)
529 DBG("SIGPIPE caught");
532 DBG("SIGINT caught");
536 DBG("SIGTERM caught");
540 CMM_STORE_SHARED(recv_child_signal
, 1);
548 * Setup signal handler for :
549 * SIGINT, SIGTERM, SIGPIPE
552 int set_signal_handler(void)
558 if ((ret
= sigemptyset(&sigset
)) < 0) {
559 PERROR("sigemptyset");
563 sa
.sa_handler
= sighandler
;
566 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
571 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
576 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
581 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
586 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
591 void lttng_relay_notify_ready(void)
593 /* Notify the parent of the fork() process that we are ready. */
594 if (opt_daemon
|| opt_background
) {
595 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
596 kill(child_ppid
, SIGUSR1
);
602 * Init thread quit pipe.
604 * Return -1 on error or 0 if all pipes are created.
607 int init_thread_quit_pipe(void)
611 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
617 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
620 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
624 if (events
== NULL
|| size
== 0) {
629 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
635 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
647 * Check if the thread quit pipe was triggered.
649 * Return 1 if it was triggered else 0;
652 int check_thread_quit_pipe(int fd
, uint32_t events
)
654 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
662 * Create and init socket from uri.
665 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
668 struct lttcomm_sock
*sock
= NULL
;
670 sock
= lttcomm_alloc_sock_from_uri(uri
);
672 ERR("Allocating socket");
676 ret
= lttcomm_create_sock(sock
);
680 DBG("Listening on sock %d", sock
->fd
);
682 ret
= sock
->ops
->bind(sock
);
687 ret
= sock
->ops
->listen(sock
, -1);
697 lttcomm_destroy_sock(sock
);
703 * Return nonzero if stream needs to be closed.
706 int close_stream_check(struct relay_stream
*stream
)
708 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
710 * We are about to close the stream so set the data pending flag to 1
711 * which will make the end data pending command skip the stream which
712 * is now closed and ready. Note that after proceeding to a file close,
713 * the written file is ready for reading.
715 stream
->data_pending_check_done
= 1;
721 static void try_close_stream(struct relay_session
*session
,
722 struct relay_stream
*stream
)
725 struct ctf_trace
*ctf_trace
;
730 if (!close_stream_check(stream
)) {
731 /* Can't close it, not ready for that. */
735 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
739 pthread_mutex_lock(&session
->viewer_ready_lock
);
740 ctf_trace
->invalid_flag
= 1;
741 pthread_mutex_unlock(&session
->viewer_ready_lock
);
743 ret
= stream_close(session
, stream
);
744 if (ret
|| session
->snapshot
) {
745 /* Already close thus the ctf trace is being or has been destroyed. */
749 ctf_trace_try_destroy(session
, ctf_trace
);
756 * This thread manages the listening for new connections on the network
759 void *relay_thread_listener(void *data
)
761 int i
, ret
, pollfd
, err
= -1;
762 uint32_t revents
, nb_fd
;
763 struct lttng_poll_event events
;
764 struct lttcomm_sock
*control_sock
, *data_sock
;
766 DBG("[thread] Relay listener started");
768 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
770 health_code_update();
772 control_sock
= relay_init_sock(control_uri
);
774 goto error_sock_control
;
777 data_sock
= relay_init_sock(data_uri
);
779 goto error_sock_relay
;
783 * Pass 3 as size here for the thread quit pipe, control and data socket.
785 ret
= create_thread_poll_set(&events
, 3);
787 goto error_create_poll
;
790 /* Add the control socket */
791 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
796 /* Add the data socket */
797 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
802 lttng_relay_notify_ready();
804 if (testpoint(relayd_thread_listener
)) {
805 goto error_testpoint
;
809 health_code_update();
811 DBG("Listener accepting connections");
815 ret
= lttng_poll_wait(&events
, -1);
819 * Restart interrupted system call.
821 if (errno
== EINTR
) {
829 DBG("Relay new connection received");
830 for (i
= 0; i
< nb_fd
; i
++) {
831 health_code_update();
833 /* Fetch once the poll data */
834 revents
= LTTNG_POLL_GETEV(&events
, i
);
835 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
838 /* No activity for this FD (poll implementation). */
842 /* Thread quit pipe has been closed. Killing thread. */
843 ret
= check_thread_quit_pipe(pollfd
, revents
);
849 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
850 ERR("socket poll error");
852 } else if (revents
& LPOLLIN
) {
854 * Get allocated in this thread, enqueued to a global queue,
855 * dequeued and freed in the worker thread.
858 struct relay_connection
*new_conn
;
859 struct lttcomm_sock
*newsock
;
861 new_conn
= connection_create();
866 if (pollfd
== data_sock
->fd
) {
867 new_conn
->type
= RELAY_DATA
;
868 newsock
= data_sock
->ops
->accept(data_sock
);
869 DBG("Relay data connection accepted, socket %d",
872 assert(pollfd
== control_sock
->fd
);
873 new_conn
->type
= RELAY_CONTROL
;
874 newsock
= control_sock
->ops
->accept(control_sock
);
875 DBG("Relay control connection accepted, socket %d",
879 PERROR("accepting sock");
880 connection_free(new_conn
);
884 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
887 PERROR("setsockopt inet");
888 lttcomm_destroy_sock(newsock
);
889 connection_free(new_conn
);
892 new_conn
->sock
= newsock
;
894 /* Enqueue request for the dispatcher thread. */
895 cds_wfq_enqueue(&relay_conn_queue
.queue
, &new_conn
->qnode
);
898 * Wake the dispatch queue futex. Implicit memory barrier with
899 * the exchange in cds_wfq_enqueue.
901 futex_nto1_wake(&relay_conn_queue
.futex
);
910 lttng_poll_clean(&events
);
912 if (data_sock
->fd
>= 0) {
913 ret
= data_sock
->ops
->close(data_sock
);
918 lttcomm_destroy_sock(data_sock
);
920 if (control_sock
->fd
>= 0) {
921 ret
= control_sock
->ops
->close(control_sock
);
926 lttcomm_destroy_sock(control_sock
);
930 ERR("Health error occurred in %s", __func__
);
932 health_unregister(health_relayd
);
933 DBG("Relay listener thread cleanup complete");
939 * This thread manages the dispatching of the requests to worker threads
942 void *relay_thread_dispatcher(void *data
)
946 struct cds_wfq_node
*node
;
947 struct relay_connection
*new_conn
= NULL
;
949 DBG("[thread] Relay dispatcher started");
951 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
953 if (testpoint(relayd_thread_dispatcher
)) {
954 goto error_testpoint
;
957 health_code_update();
959 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
960 health_code_update();
962 /* Atomically prepare the queue futex */
963 futex_nto1_prepare(&relay_conn_queue
.futex
);
966 health_code_update();
968 /* Dequeue commands */
969 node
= cds_wfq_dequeue_blocking(&relay_conn_queue
.queue
);
971 DBG("Woken up but nothing in the relay command queue");
972 /* Continue thread execution */
975 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
977 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
980 * Inform worker thread of the new request. This call is blocking
981 * so we can be assured that the data will be read at some point in
982 * time or wait to the end of the world :)
984 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
986 PERROR("write connection pipe");
987 connection_destroy(new_conn
);
990 } while (node
!= NULL
);
992 /* Futex wait on queue. Blocking call on futex() */
994 futex_nto1_wait(&relay_conn_queue
.futex
);
998 /* Normal exit, no error */
1005 ERR("Health error occurred in %s", __func__
);
1007 health_unregister(health_relayd
);
1008 DBG("Dispatch thread dying");
1013 static void try_close_streams(struct relay_session
*session
)
1015 struct ctf_trace
*ctf_trace
;
1016 struct lttng_ht_iter iter
;
1020 pthread_mutex_lock(&session
->viewer_ready_lock
);
1022 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
1024 struct relay_stream
*stream
;
1026 /* Close streams. */
1027 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
1028 stream_close(session
, stream
);
1031 ctf_trace
->invalid_flag
= 1;
1032 ctf_trace_try_destroy(session
, ctf_trace
);
1035 pthread_mutex_unlock(&session
->viewer_ready_lock
);
1039 * Try to destroy a session within a connection.
1041 static void destroy_session(struct relay_session
*session
,
1042 struct lttng_ht
*sessions_ht
)
1045 assert(sessions_ht
);
1047 /* Indicate that this session can be destroyed from now on. */
1048 session
->close_flag
= 1;
1050 try_close_streams(session
);
1053 * This will try to delete and destroy the session if no viewer is attached
1054 * to it meaning the refcount is down to zero.
1056 session_try_destroy(sessions_ht
, session
);
1060 * Copy index data from the control port to a given index object.
1062 static void copy_index_control_data(struct relay_index
*index
,
1063 struct lttcomm_relayd_index
*data
)
1069 * The index on disk is encoded in big endian, so we don't need to convert
1070 * the data received on the network. The data_offset value is NEVER
1071 * modified here and is updated by the data thread.
1073 index
->index_data
.packet_size
= data
->packet_size
;
1074 index
->index_data
.content_size
= data
->content_size
;
1075 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
1076 index
->index_data
.timestamp_end
= data
->timestamp_end
;
1077 index
->index_data
.events_discarded
= data
->events_discarded
;
1078 index
->index_data
.stream_id
= data
->stream_id
;
1082 * Handle the RELAYD_CREATE_SESSION command.
1084 * On success, send back the session id or else return a negative value.
1087 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1088 struct relay_connection
*conn
)
1090 int ret
= 0, send_ret
;
1091 struct relay_session
*session
;
1092 struct lttcomm_relayd_status_session reply
;
1097 memset(&reply
, 0, sizeof(reply
));
1099 session
= session_create();
1104 session
->minor
= conn
->minor
;
1105 session
->major
= conn
->major
;
1106 conn
->session_id
= session
->id
;
1107 conn
->session
= session
;
1109 reply
.session_id
= htobe64(session
->id
);
1111 switch (conn
->minor
) {
1116 case 4: /* LTTng sessiond 2.4 */
1118 ret
= cmd_create_session_2_4(conn
, session
);
1121 lttng_ht_add_unique_u64(conn
->sessions_ht
, &session
->session_n
);
1122 DBG("Created session %" PRIu64
, session
->id
);
1126 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1128 reply
.ret_code
= htobe32(LTTNG_OK
);
1131 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1133 ERR("Relayd sending session id");
1141 * When we have received all the streams and the metadata for a channel,
1142 * we make them visible to the viewer threads.
1145 void set_viewer_ready_flag(struct relay_connection
*conn
)
1147 struct relay_stream
*stream
, *tmp_stream
;
1149 pthread_mutex_lock(&conn
->session
->viewer_ready_lock
);
1150 cds_list_for_each_entry_safe(stream
, tmp_stream
, &conn
->recv_head
,
1152 stream
->viewer_ready
= 1;
1153 cds_list_del(&stream
->recv_list
);
1155 pthread_mutex_unlock(&conn
->session
->viewer_ready_lock
);
1160 * Add a recv handle node to the connection recv list with the given stream
1161 * handle. A new node is allocated thus must be freed when the node is deleted
1164 static void queue_stream(struct relay_stream
*stream
,
1165 struct relay_connection
*conn
)
1170 cds_list_add(&stream
->recv_list
, &conn
->recv_head
);
1174 * relay_add_stream: allocate a new stream for a session
1177 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1178 struct relay_connection
*conn
)
1181 struct relay_session
*session
= conn
->session
;
1182 struct relay_stream
*stream
= NULL
;
1183 struct lttcomm_relayd_status_stream reply
;
1184 struct ctf_trace
*trace
;
1186 if (!session
|| conn
->version_check_done
== 0) {
1187 ERR("Trying to add a stream before version check");
1189 goto end_no_session
;
1192 stream
= zmalloc(sizeof(struct relay_stream
));
1193 if (stream
== NULL
) {
1194 PERROR("relay stream zmalloc");
1196 goto end_no_session
;
1199 switch (conn
->minor
) {
1200 case 1: /* LTTng sessiond 2.1 */
1201 ret
= cmd_recv_stream_2_1(conn
, stream
);
1203 case 2: /* LTTng sessiond 2.2 */
1205 ret
= cmd_recv_stream_2_2(conn
, stream
);
1209 goto err_free_stream
;
1213 stream
->stream_handle
= ++last_relay_stream_id
;
1214 stream
->prev_seq
= -1ULL;
1215 stream
->session_id
= session
->id
;
1216 stream
->index_fd
= -1;
1217 stream
->read_index_fd
= -1;
1218 stream
->ctf_stream_id
= -1ULL;
1219 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
1220 pthread_mutex_init(&stream
->lock
, NULL
);
1222 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1224 ERR("relay creating output directory");
1229 * No need to use run_as API here because whatever we receives, the relayd
1230 * uses its own credentials for the stream files.
1232 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1233 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1235 ERR("Create output file");
1239 if (stream
->tracefile_size
) {
1240 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1242 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1245 trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, stream
->path_name
);
1247 trace
= ctf_trace_create(stream
->path_name
);
1252 ctf_trace_add(session
->ctf_traces_ht
, trace
);
1254 ctf_trace_get_ref(trace
);
1256 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1257 stream
->metadata_flag
= 1;
1258 /* Assign quick reference to the metadata stream in the trace. */
1259 trace
->metadata_stream
= stream
;
1263 * Add the stream in the recv list of the connection. Once the end stream
1264 * message is received, this list is emptied and streams are set with the
1265 * viewer ready flag.
1267 queue_stream(stream
, conn
);
1270 * Both in the ctf_trace object and the global stream ht since the data
1271 * side of the relayd does not have the concept of session.
1273 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
1274 cds_list_add_tail(&stream
->trace_list
, &trace
->stream_list
);
1276 session
->stream_count
++;
1278 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1279 stream
->stream_handle
);
1282 memset(&reply
, 0, sizeof(reply
));
1283 reply
.handle
= htobe64(stream
->stream_handle
);
1284 /* send the session id to the client or a negative return code on error */
1286 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1287 /* stream was not properly added to the ht, so free it */
1290 reply
.ret_code
= htobe32(LTTNG_OK
);
1293 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1294 sizeof(struct lttcomm_relayd_status_stream
), 0);
1296 ERR("Relay sending stream id");
1305 free(stream
->path_name
);
1306 free(stream
->channel_name
);
1312 * relay_close_stream: close a specific stream
1315 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1316 struct relay_connection
*conn
)
1319 struct relay_session
*session
= conn
->session
;
1320 struct lttcomm_relayd_close_stream stream_info
;
1321 struct lttcomm_relayd_generic_reply reply
;
1322 struct relay_stream
*stream
;
1324 DBG("Close stream received");
1326 if (!session
|| conn
->version_check_done
== 0) {
1327 ERR("Trying to close a stream before version check");
1329 goto end_no_session
;
1332 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1333 sizeof(struct lttcomm_relayd_close_stream
), 0);
1334 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1336 /* Orderly shutdown. Not necessary to print an error. */
1337 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1339 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1342 goto end_no_session
;
1346 stream
= stream_find_by_id(relay_streams_ht
,
1347 be64toh(stream_info
.stream_id
));
1353 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1354 stream
->close_flag
= 1;
1355 session
->stream_count
--;
1356 assert(session
->stream_count
>= 0);
1358 /* Check if we can close it or else the data will do it. */
1359 try_close_stream(session
, stream
);
1364 memset(&reply
, 0, sizeof(reply
));
1366 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1368 reply
.ret_code
= htobe32(LTTNG_OK
);
1370 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1371 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1373 ERR("Relay sending stream id");
1382 * relay_unknown_command: send -1 if received unknown command
1385 void relay_unknown_command(struct relay_connection
*conn
)
1387 struct lttcomm_relayd_generic_reply reply
;
1390 memset(&reply
, 0, sizeof(reply
));
1391 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1392 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1393 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1395 ERR("Relay sending unknown command");
1400 * relay_start: send an acknowledgment to the client to tell if we are
1401 * ready to receive data. We are ready if a session is established.
1404 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1405 struct relay_connection
*conn
)
1407 int ret
= htobe32(LTTNG_OK
);
1408 struct lttcomm_relayd_generic_reply reply
;
1409 struct relay_session
*session
= conn
->session
;
1412 DBG("Trying to start the streaming without a session established");
1413 ret
= htobe32(LTTNG_ERR_UNK
);
1416 memset(&reply
, 0, sizeof(reply
));
1417 reply
.ret_code
= ret
;
1418 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1419 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1421 ERR("Relay sending start ack");
1428 * Append padding to the file pointed by the file descriptor fd.
1430 static int write_padding_to_file(int fd
, uint32_t size
)
1439 zeros
= zmalloc(size
);
1440 if (zeros
== NULL
) {
1441 PERROR("zmalloc zeros for padding");
1446 ret
= lttng_write(fd
, zeros
, size
);
1448 PERROR("write padding to file");
1458 * relay_recv_metadata: receive the metada for the session.
1461 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1462 struct relay_connection
*conn
)
1464 int ret
= htobe32(LTTNG_OK
);
1466 struct relay_session
*session
= conn
->session
;
1467 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1468 struct relay_stream
*metadata_stream
;
1469 uint64_t data_size
, payload_size
;
1470 struct ctf_trace
*ctf_trace
;
1473 ERR("Metadata sent before version check");
1478 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1479 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1480 ERR("Incorrect data size");
1484 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1486 if (data_buffer_size
< data_size
) {
1487 /* In case the realloc fails, we can free the memory */
1490 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1491 if (!tmp_data_ptr
) {
1492 ERR("Allocating data buffer");
1497 data_buffer
= tmp_data_ptr
;
1498 data_buffer_size
= data_size
;
1500 memset(data_buffer
, 0, data_size
);
1501 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1502 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1503 if (ret
< 0 || ret
!= data_size
) {
1505 /* Orderly shutdown. Not necessary to print an error. */
1506 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1508 ERR("Relay didn't receive the whole metadata");
1513 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1516 metadata_stream
= stream_find_by_id(relay_streams_ht
,
1517 be64toh(metadata_struct
->stream_id
));
1518 if (!metadata_stream
) {
1523 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1525 if (size_ret
< payload_size
) {
1526 ERR("Relay error writing metadata on file");
1531 ret
= write_padding_to_file(metadata_stream
->fd
,
1532 be32toh(metadata_struct
->padding_size
));
1537 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1538 metadata_stream
->path_name
);
1540 ctf_trace
->metadata_received
+=
1541 payload_size
+ be32toh(metadata_struct
->padding_size
);
1543 DBG2("Relay metadata written");
1552 * relay_send_version: send relayd version number
1555 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1556 struct relay_connection
*conn
)
1559 struct lttcomm_relayd_version reply
, msg
;
1563 conn
->version_check_done
= 1;
1565 /* Get version from the other side. */
1566 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1567 if (ret
< 0 || ret
!= sizeof(msg
)) {
1569 /* Orderly shutdown. Not necessary to print an error. */
1570 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1572 ERR("Relay failed to receive the version values.");
1578 memset(&reply
, 0, sizeof(reply
));
1579 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1580 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1582 /* Major versions must be the same */
1583 if (reply
.major
!= be32toh(msg
.major
)) {
1584 DBG("Incompatible major versions (%u vs %u), deleting session",
1585 reply
.major
, be32toh(msg
.major
));
1586 destroy_session(conn
->session
, conn
->sessions_ht
);
1591 conn
->major
= reply
.major
;
1592 /* We adapt to the lowest compatible version */
1593 if (reply
.minor
<= be32toh(msg
.minor
)) {
1594 conn
->minor
= reply
.minor
;
1596 conn
->minor
= be32toh(msg
.minor
);
1599 reply
.major
= htobe32(reply
.major
);
1600 reply
.minor
= htobe32(reply
.minor
);
1601 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1602 sizeof(struct lttcomm_relayd_version
), 0);
1604 ERR("Relay sending version");
1607 DBG("Version check done using protocol %u.%u", conn
->major
,
1615 * Check for data pending for a given stream id from the session daemon.
1618 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1619 struct relay_connection
*conn
)
1621 struct relay_session
*session
= conn
->session
;
1622 struct lttcomm_relayd_data_pending msg
;
1623 struct lttcomm_relayd_generic_reply reply
;
1624 struct relay_stream
*stream
;
1626 uint64_t last_net_seq_num
, stream_id
;
1628 DBG("Data pending command received");
1630 if (!session
|| conn
->version_check_done
== 0) {
1631 ERR("Trying to check for data before version check");
1633 goto end_no_session
;
1636 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1637 if (ret
< sizeof(msg
)) {
1639 /* Orderly shutdown. Not necessary to print an error. */
1640 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1642 ERR("Relay didn't receive valid data_pending struct size : %d",
1646 goto end_no_session
;
1649 stream_id
= be64toh(msg
.stream_id
);
1650 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1653 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
1654 if (stream
== NULL
) {
1659 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1660 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1663 /* Avoid wrapping issue */
1664 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1665 /* Data has in fact been written and is NOT pending */
1668 /* Data still being streamed thus pending */
1672 /* Pending check is now done. */
1673 stream
->data_pending_check_done
= 1;
1678 memset(&reply
, 0, sizeof(reply
));
1679 reply
.ret_code
= htobe32(ret
);
1680 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1682 ERR("Relay data pending ret code failed");
1690 * Wait for the control socket to reach a quiescent state.
1692 * Note that for now, when receiving this command from the session daemon, this
1693 * means that every subsequent commands or data received on the control socket
1694 * has been handled. So, this is why we simply return OK here.
1697 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1698 struct relay_connection
*conn
)
1702 struct relay_stream
*stream
;
1703 struct lttng_ht_iter iter
;
1704 struct lttcomm_relayd_quiescent_control msg
;
1705 struct lttcomm_relayd_generic_reply reply
;
1707 DBG("Checking quiescent state on control socket");
1709 if (!conn
->session
|| conn
->version_check_done
== 0) {
1710 ERR("Trying to check for data before version check");
1712 goto end_no_session
;
1715 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1716 if (ret
< sizeof(msg
)) {
1718 /* Orderly shutdown. Not necessary to print an error. */
1719 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1721 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1725 goto end_no_session
;
1728 stream_id
= be64toh(msg
.stream_id
);
1731 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1733 if (stream
->stream_handle
== stream_id
) {
1734 stream
->data_pending_check_done
= 1;
1735 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1742 memset(&reply
, 0, sizeof(reply
));
1743 reply
.ret_code
= htobe32(LTTNG_OK
);
1744 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1746 ERR("Relay data quiescent control ret code failed");
1754 * Initialize a data pending command. This means that a client is about to ask
1755 * for data pending for each stream he/she holds. Simply iterate over all
1756 * streams of a session and set the data_pending_check_done flag.
1758 * This command returns to the client a LTTNG_OK code.
1761 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1762 struct relay_connection
*conn
)
1765 struct lttng_ht_iter iter
;
1766 struct lttcomm_relayd_begin_data_pending msg
;
1767 struct lttcomm_relayd_generic_reply reply
;
1768 struct relay_stream
*stream
;
1769 uint64_t session_id
;
1774 DBG("Init streams for data pending");
1776 if (!conn
->session
|| conn
->version_check_done
== 0) {
1777 ERR("Trying to check for data before version check");
1779 goto end_no_session
;
1782 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1783 if (ret
< sizeof(msg
)) {
1785 /* Orderly shutdown. Not necessary to print an error. */
1786 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1788 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1792 goto end_no_session
;
1795 session_id
= be64toh(msg
.session_id
);
1798 * Iterate over all streams to set the begin data pending flag. For now, the
1799 * streams are indexed by stream handle so we have to iterate over all
1800 * streams to find the one associated with the right session_id.
1803 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1805 if (stream
->session_id
== session_id
) {
1806 stream
->data_pending_check_done
= 0;
1807 DBG("Set begin data pending flag to stream %" PRIu64
,
1808 stream
->stream_handle
);
1813 memset(&reply
, 0, sizeof(reply
));
1814 /* All good, send back reply. */
1815 reply
.ret_code
= htobe32(LTTNG_OK
);
1817 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1819 ERR("Relay begin data pending send reply failed");
1827 * End data pending command. This will check, for a given session id, if each
1828 * stream associated with it has its data_pending_check_done flag set. If not,
1829 * this means that the client lost track of the stream but the data is still
1830 * being streamed on our side. In this case, we inform the client that data is
1833 * Return to the client if there is data in flight or not with a ret_code.
1836 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1837 struct relay_connection
*conn
)
1840 struct lttng_ht_iter iter
;
1841 struct lttcomm_relayd_end_data_pending msg
;
1842 struct lttcomm_relayd_generic_reply reply
;
1843 struct relay_stream
*stream
;
1844 uint64_t session_id
;
1845 uint32_t is_data_inflight
= 0;
1850 DBG("End data pending command");
1852 if (!conn
->session
|| conn
->version_check_done
== 0) {
1853 ERR("Trying to check for data before version check");
1855 goto end_no_session
;
1858 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1859 if (ret
< sizeof(msg
)) {
1861 /* Orderly shutdown. Not necessary to print an error. */
1862 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1864 ERR("Relay didn't receive valid end data_pending struct size: %d",
1868 goto end_no_session
;
1871 session_id
= be64toh(msg
.session_id
);
1873 /* Iterate over all streams to see if the begin data pending flag is set. */
1875 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1877 if (stream
->session_id
== session_id
&&
1878 !stream
->data_pending_check_done
&& !stream
->terminated_flag
) {
1879 is_data_inflight
= 1;
1880 DBG("Data is still in flight for stream %" PRIu64
,
1881 stream
->stream_handle
);
1887 memset(&reply
, 0, sizeof(reply
));
1888 /* All good, send back reply. */
1889 reply
.ret_code
= htobe32(is_data_inflight
);
1891 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1893 ERR("Relay end data pending send reply failed");
1901 * Receive an index for a specific stream.
1903 * Return 0 on success else a negative value.
1906 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1907 struct relay_connection
*conn
)
1909 int ret
, send_ret
, index_created
= 0;
1910 struct relay_session
*session
= conn
->session
;
1911 struct lttcomm_relayd_index index_info
;
1912 struct relay_index
*index
, *wr_index
= NULL
;
1913 struct lttcomm_relayd_generic_reply reply
;
1914 struct relay_stream
*stream
;
1915 uint64_t net_seq_num
;
1919 DBG("Relay receiving index");
1921 if (!session
|| conn
->version_check_done
== 0) {
1922 ERR("Trying to close a stream before version check");
1924 goto end_no_session
;
1927 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
1928 sizeof(index_info
), 0);
1929 if (ret
< sizeof(index_info
)) {
1931 /* Orderly shutdown. Not necessary to print an error. */
1932 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1934 ERR("Relay didn't receive valid index struct size : %d", ret
);
1937 goto end_no_session
;
1940 net_seq_num
= be64toh(index_info
.net_seq_num
);
1943 stream
= stream_find_by_id(relay_streams_ht
,
1944 be64toh(index_info
.relay_stream_id
));
1947 goto end_rcu_unlock
;
1950 /* Live beacon handling */
1951 if (index_info
.packet_size
== 0) {
1952 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1955 * Only flag a stream inactive when it has already received data
1956 * and no indexes are in flight.
1958 if (stream
->total_index_received
> 0 && stream
->indexes_in_flight
== 0) {
1959 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1962 goto end_rcu_unlock
;
1964 stream
->beacon_ts_end
= -1ULL;
1967 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1969 /* A successful creation will add the object to the HT. */
1970 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1972 goto end_rcu_unlock
;
1975 stream
->indexes_in_flight
++;
1978 copy_index_control_data(index
, &index_info
);
1979 if (stream
->ctf_stream_id
== -1ULL) {
1980 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
1983 if (index_created
) {
1985 * Try to add the relay index object to the hash table. If an object
1986 * already exist, destroy back the index created, set the data in this
1987 * object and write it on disk.
1989 relay_index_add(index
, &wr_index
);
1991 copy_index_control_data(wr_index
, &index_info
);
1995 /* The index already exists so write it on disk. */
1999 /* Do we have a writable ready index to write on disk. */
2001 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2003 goto end_rcu_unlock
;
2005 stream
->total_index_received
++;
2006 stream
->indexes_in_flight
--;
2007 assert(stream
->indexes_in_flight
>= 0);
2013 memset(&reply
, 0, sizeof(reply
));
2015 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2017 reply
.ret_code
= htobe32(LTTNG_OK
);
2019 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2021 ERR("Relay sending close index id reply");
2030 * Receive the streams_sent message.
2032 * Return 0 on success else a negative value.
2035 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2036 struct relay_connection
*conn
)
2039 struct lttcomm_relayd_generic_reply reply
;
2043 DBG("Relay receiving streams_sent");
2045 if (!conn
->session
|| conn
->version_check_done
== 0) {
2046 ERR("Trying to close a stream before version check");
2048 goto end_no_session
;
2052 * Flag every pending stream in the connection recv list that they are
2053 * ready to be used by the viewer.
2055 set_viewer_ready_flag(conn
);
2058 * Inform the viewer that there are new streams in the session.
2060 if (conn
->session
->viewer_refcount
) {
2061 uatomic_set(&conn
->session
->new_streams
, 1);
2064 memset(&reply
, 0, sizeof(reply
));
2065 reply
.ret_code
= htobe32(LTTNG_OK
);
2066 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2068 ERR("Relay sending sent_stream reply");
2080 * Process the commands received on the control socket
2083 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2084 struct relay_connection
*conn
)
2088 switch (be32toh(recv_hdr
->cmd
)) {
2089 case RELAYD_CREATE_SESSION
:
2090 ret
= relay_create_session(recv_hdr
, conn
);
2092 case RELAYD_ADD_STREAM
:
2093 ret
= relay_add_stream(recv_hdr
, conn
);
2095 case RELAYD_START_DATA
:
2096 ret
= relay_start(recv_hdr
, conn
);
2098 case RELAYD_SEND_METADATA
:
2099 ret
= relay_recv_metadata(recv_hdr
, conn
);
2101 case RELAYD_VERSION
:
2102 ret
= relay_send_version(recv_hdr
, conn
);
2104 case RELAYD_CLOSE_STREAM
:
2105 ret
= relay_close_stream(recv_hdr
, conn
);
2107 case RELAYD_DATA_PENDING
:
2108 ret
= relay_data_pending(recv_hdr
, conn
);
2110 case RELAYD_QUIESCENT_CONTROL
:
2111 ret
= relay_quiescent_control(recv_hdr
, conn
);
2113 case RELAYD_BEGIN_DATA_PENDING
:
2114 ret
= relay_begin_data_pending(recv_hdr
, conn
);
2116 case RELAYD_END_DATA_PENDING
:
2117 ret
= relay_end_data_pending(recv_hdr
, conn
);
2119 case RELAYD_SEND_INDEX
:
2120 ret
= relay_recv_index(recv_hdr
, conn
);
2122 case RELAYD_STREAMS_SENT
:
2123 ret
= relay_streams_sent(recv_hdr
, conn
);
2125 case RELAYD_UPDATE_SYNC_INFO
:
2127 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2128 relay_unknown_command(conn
);
2138 * Handle index for a data stream.
2140 * RCU read side lock MUST be acquired.
2142 * Return 0 on success else a negative value.
2144 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2147 int ret
= 0, index_created
= 0;
2148 uint64_t stream_id
, data_offset
;
2149 struct relay_index
*index
, *wr_index
= NULL
;
2153 stream_id
= stream
->stream_handle
;
2154 /* Get data offset because we are about to update the index. */
2155 data_offset
= htobe64(stream
->tracefile_size_current
);
2158 * Lookup for an existing index for that stream id/sequence number. If on
2159 * exists, the control thread already received the data for it thus we need
2160 * to write it on disk.
2162 index
= relay_index_find(stream_id
, net_seq_num
);
2164 /* A successful creation will add the object to the HT. */
2165 index
= relay_index_create(stream_id
, net_seq_num
);
2171 stream
->indexes_in_flight
++;
2174 if (rotate_index
|| stream
->index_fd
< 0) {
2175 index
->to_close_fd
= stream
->index_fd
;
2176 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2177 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2178 stream
->tracefile_count_current
);
2180 /* This will close the stream's index fd if one. */
2181 relay_index_free_safe(index
);
2184 stream
->index_fd
= ret
;
2186 index
->fd
= stream
->index_fd
;
2187 index
->index_data
.offset
= data_offset
;
2189 if (index_created
) {
2191 * Try to add the relay index object to the hash table. If an object
2192 * already exist, destroy back the index created and set the data.
2194 relay_index_add(index
, &wr_index
);
2196 /* Copy back data from the created index. */
2197 wr_index
->fd
= index
->fd
;
2198 wr_index
->to_close_fd
= index
->to_close_fd
;
2199 wr_index
->index_data
.offset
= data_offset
;
2203 /* The index already exists so write it on disk. */
2207 /* Do we have a writable ready index to write on disk. */
2209 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2213 stream
->total_index_received
++;
2214 stream
->indexes_in_flight
--;
2215 assert(stream
->indexes_in_flight
>= 0);
2223 * relay_process_data: Process the data received on the data socket
2226 int relay_process_data(struct relay_connection
*conn
)
2228 int ret
= 0, rotate_index
= 0;
2230 struct relay_stream
*stream
;
2231 struct lttcomm_relayd_data_hdr data_hdr
;
2233 uint64_t net_seq_num
;
2235 struct relay_session
*session
;
2239 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2240 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2243 /* Orderly shutdown. Not necessary to print an error. */
2244 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2246 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2252 stream_id
= be64toh(data_hdr
.stream_id
);
2255 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
2258 goto end_rcu_unlock
;
2261 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
2264 data_size
= be32toh(data_hdr
.data_size
);
2265 if (data_buffer_size
< data_size
) {
2268 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2269 if (!tmp_data_ptr
) {
2270 ERR("Allocating data buffer");
2273 goto end_rcu_unlock
;
2275 data_buffer
= tmp_data_ptr
;
2276 data_buffer_size
= data_size
;
2278 memset(data_buffer
, 0, data_size
);
2280 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2282 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2283 data_size
, stream_id
, net_seq_num
);
2284 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
2287 /* Orderly shutdown. Not necessary to print an error. */
2288 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2291 goto end_rcu_unlock
;
2294 /* Check if a rotation is needed. */
2295 if (stream
->tracefile_size
> 0 &&
2296 (stream
->tracefile_size_current
+ data_size
) >
2297 stream
->tracefile_size
) {
2298 struct relay_viewer_stream
*vstream
;
2301 new_id
= (stream
->tracefile_count_current
+ 1) %
2302 stream
->tracefile_count
;
2304 * When we wrap-around back to 0, we start overwriting old
2307 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2308 stream
->tracefile_overwrite
= 1;
2310 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2311 if (stream
->tracefile_overwrite
) {
2312 stream
->oldest_tracefile_id
=
2313 (stream
->oldest_tracefile_id
+ 1) %
2314 stream
->tracefile_count
;
2316 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2319 * The viewer is reading a file about to be
2320 * overwritten. Close the FDs it is
2321 * currently using and let it handle the fault.
2323 if (vstream
->tracefile_count_current
== new_id
) {
2324 pthread_mutex_lock(&vstream
->overwrite_lock
);
2325 vstream
->abort_flag
= 1;
2326 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2327 DBG("Streaming side setting abort_flag on stream %s_%" PRIu64
"\n",
2328 stream
->channel_name
, new_id
);
2329 } else if (vstream
->tracefile_count_current
==
2330 stream
->tracefile_count_current
) {
2332 * The reader and writer were in the
2333 * same trace file, inform the viewer
2334 * that no new index will ever be added
2337 vstream
->close_write_flag
= 1;
2340 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2341 stream
->tracefile_size
, stream
->tracefile_count
,
2342 relayd_uid
, relayd_gid
, stream
->fd
,
2343 &(stream
->tracefile_count_current
), &stream
->fd
);
2344 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2346 ERR("Rotating stream output file");
2347 goto end_rcu_unlock
;
2349 /* Reset current size because we just perform a stream rotation. */
2350 stream
->tracefile_size_current
= 0;
2355 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2356 * index are NOT supported.
2358 if (session
->minor
>= 4 && !session
->snapshot
) {
2359 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2361 goto end_rcu_unlock
;
2365 /* Write data to stream output fd. */
2366 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2367 if (size_ret
< data_size
) {
2368 ERR("Relay error writing data to file");
2370 goto end_rcu_unlock
;
2373 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2374 ret
, stream
->stream_handle
);
2376 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2378 goto end_rcu_unlock
;
2380 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2382 stream
->prev_seq
= net_seq_num
;
2384 try_close_stream(session
, stream
);
2393 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2399 (void) lttng_poll_del(events
, pollfd
);
2401 ret
= close(pollfd
);
2403 ERR("Closing pollfd %d", pollfd
);
2407 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
2408 struct relay_connection
*conn
)
2410 assert(relay_connections_ht
);
2413 connection_delete(relay_connections_ht
, conn
);
2415 /* For the control socket, we try to destroy the session. */
2416 if (conn
->type
== RELAY_CONTROL
&& conn
->session
) {
2417 destroy_session(conn
->session
, conn
->sessions_ht
);
2420 connection_destroy(conn
);
2424 * This thread does the actual work
2427 void *relay_thread_worker(void *data
)
2429 int ret
, err
= -1, last_seen_data_fd
= -1;
2431 struct relay_connection
*conn
;
2432 struct lttng_poll_event events
;
2433 struct lttng_ht
*relay_connections_ht
;
2434 struct lttng_ht_iter iter
;
2435 struct lttcomm_relayd_hdr recv_hdr
;
2436 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2437 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2438 struct relay_index
*index
;
2440 DBG("[thread] Relay worker started");
2442 rcu_register_thread();
2444 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2446 if (testpoint(relayd_thread_worker
)) {
2447 goto error_testpoint
;
2450 health_code_update();
2452 /* table of connections indexed on socket */
2453 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2454 if (!relay_connections_ht
) {
2455 goto relay_connections_ht_error
;
2458 /* Tables of received indexes indexed by index handle and net_seq_num. */
2459 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2461 goto indexes_ht_error
;
2464 ret
= create_thread_poll_set(&events
, 2);
2466 goto error_poll_create
;
2469 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2476 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2478 health_code_update();
2480 /* Infinite blocking call, waiting for transmission */
2481 DBG3("Relayd worker thread polling...");
2482 health_poll_entry();
2483 ret
= lttng_poll_wait(&events
, -1);
2487 * Restart interrupted system call.
2489 if (errno
== EINTR
) {
2498 * Process control. The control connection is prioritised so we don't
2499 * starve it with high throughout put tracing data on the data
2502 for (i
= 0; i
< nb_fd
; i
++) {
2503 /* Fetch once the poll data */
2504 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2505 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2507 health_code_update();
2510 /* No activity for this FD (poll implementation). */
2514 /* Thread quit pipe has been closed. Killing thread. */
2515 ret
= check_thread_quit_pipe(pollfd
, revents
);
2521 /* Inspect the relay conn pipe for new connection */
2522 if (pollfd
== relay_conn_pipe
[0]) {
2523 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2524 ERR("Relay connection pipe error");
2526 } else if (revents
& LPOLLIN
) {
2527 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2531 conn
->sessions_ht
= sessions_ht
;
2532 connection_init(conn
);
2533 lttng_poll_add(&events
, conn
->sock
->fd
,
2534 LPOLLIN
| LPOLLRDHUP
);
2536 lttng_ht_add_unique_ulong(relay_connections_ht
,
2539 DBG("Connection socket %d added", conn
->sock
->fd
);
2543 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2544 /* If not found, there is a synchronization issue. */
2547 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2548 cleanup_connection_pollfd(&events
, pollfd
);
2549 destroy_connection(relay_connections_ht
, conn
);
2550 if (last_seen_data_fd
== pollfd
) {
2551 last_seen_data_fd
= last_notdel_data_fd
;
2553 } else if (revents
& LPOLLIN
) {
2554 if (conn
->type
== RELAY_CONTROL
) {
2555 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2556 sizeof(recv_hdr
), 0);
2558 /* Connection closed */
2559 cleanup_connection_pollfd(&events
, pollfd
);
2560 destroy_connection(relay_connections_ht
, conn
);
2561 DBG("Control connection closed with %d", pollfd
);
2563 ret
= relay_process_control(&recv_hdr
, conn
);
2565 /* Clear the session on error. */
2566 cleanup_connection_pollfd(&events
, pollfd
);
2567 destroy_connection(relay_connections_ht
, conn
);
2568 DBG("Connection closed with %d", pollfd
);
2574 * Flag the last seen data fd not deleted. It will be
2575 * used as the last seen fd if any fd gets deleted in
2578 last_notdel_data_fd
= pollfd
;
2581 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
2588 * The last loop handled a control request, go back to poll to make
2589 * sure we prioritise the control socket.
2595 if (last_seen_data_fd
>= 0) {
2596 for (i
= 0; i
< nb_fd
; i
++) {
2597 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2599 health_code_update();
2601 if (last_seen_data_fd
== pollfd
) {
2608 /* Process data connection. */
2609 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2610 /* Fetch the poll data. */
2611 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2612 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2614 health_code_update();
2617 /* No activity for this FD (poll implementation). */
2621 /* Skip the command pipe. It's handled in the first loop. */
2622 if (pollfd
== relay_conn_pipe
[0]) {
2627 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2629 /* Skip it. Might be removed before. */
2634 if (revents
& LPOLLIN
) {
2635 if (conn
->type
!= RELAY_DATA
) {
2640 ret
= relay_process_data(conn
);
2641 /* Connection closed */
2643 cleanup_connection_pollfd(&events
, pollfd
);
2644 destroy_connection(relay_connections_ht
, conn
);
2645 DBG("Data connection closed with %d", pollfd
);
2647 * Every goto restart call sets the last seen fd where
2648 * here we don't really care since we gracefully
2649 * continue the loop after the connection is deleted.
2652 /* Keep last seen port. */
2653 last_seen_data_fd
= pollfd
;
2660 last_seen_data_fd
= -1;
2663 /* Normal exit, no error */
2668 lttng_poll_clean(&events
);
2670 /* Cleanup reamaining connection object. */
2672 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2674 health_code_update();
2675 destroy_connection(relay_connections_ht
, conn
);
2680 cds_lfht_for_each_entry(indexes_ht
->ht
, &iter
.iter
, index
,
2682 health_code_update();
2683 relay_index_delete(index
);
2684 relay_index_free_safe(index
);
2687 lttng_ht_destroy(indexes_ht
);
2689 lttng_ht_destroy(relay_connections_ht
);
2690 relay_connections_ht_error
:
2691 /* Close relay conn pipes */
2692 utils_close_pipe(relay_conn_pipe
);
2694 DBG("Thread exited with error");
2696 DBG("Worker thread cleanup complete");
2701 ERR("Health error occurred in %s", __func__
);
2703 health_unregister(health_relayd
);
2704 rcu_unregister_thread();
2710 * Create the relay command pipe to wake thread_manage_apps.
2711 * Closed in cleanup().
2713 static int create_relay_conn_pipe(void)
2717 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2725 int main(int argc
, char **argv
)
2729 struct relay_local_data
*relay_ctx
;
2731 /* Parse arguments */
2733 if ((ret
= set_options(argc
, argv
)) < 0) {
2737 if ((ret
= set_signal_handler()) < 0) {
2741 /* Try to create directory if -o, --output is specified. */
2742 if (opt_output_path
) {
2743 if (*opt_output_path
!= '/') {
2744 ERR("Please specify an absolute path for -o, --output PATH");
2748 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2750 ERR("Unable to create %s", opt_output_path
);
2756 if (opt_daemon
|| opt_background
) {
2759 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2766 * We are in the child. Make sure all other file
2767 * descriptors are closed, in case we are called with
2768 * more opened file descriptors than the standard ones.
2770 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2775 /* Create thread quit pipe */
2776 if ((ret
= init_thread_quit_pipe()) < 0) {
2780 /* We need those values for the file/dir creation. */
2781 relayd_uid
= getuid();
2782 relayd_gid
= getgid();
2784 /* Check if daemon is UID = 0 */
2785 if (relayd_uid
== 0) {
2786 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 || live_uri
->port
< 1024) {
2787 ERR("Need to be root to use ports < 1024");
2793 /* Setup the thread apps communication pipe. */
2794 if ((ret
= create_relay_conn_pipe()) < 0) {
2798 /* Init relay command queue. */
2799 cds_wfq_init(&relay_conn_queue
.queue
);
2801 /* Set up max poll set size */
2802 lttng_poll_set_max_size();
2804 /* Initialize communication library */
2806 lttcomm_inet_init();
2808 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2810 PERROR("relay_ctx");
2814 /* tables of sessions indexed by session ID */
2815 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2816 if (!relay_ctx
->sessions_ht
) {
2817 goto exit_relay_ctx_sessions
;
2820 /* tables of streams indexed by stream ID */
2821 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2822 if (!relay_streams_ht
) {
2823 goto exit_relay_ctx_streams
;
2826 /* tables of streams indexed by stream ID */
2827 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2828 if (!viewer_streams_ht
) {
2829 goto exit_relay_ctx_viewer_streams
;
2832 /* Initialize thread health monitoring */
2833 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2834 if (!health_relayd
) {
2835 PERROR("health_app_create error");
2836 goto exit_health_app_create
;
2839 ret
= utils_create_pipe(health_quit_pipe
);
2841 goto error_health_pipe
;
2844 /* Create thread to manage the client socket */
2845 ret
= pthread_create(&health_thread
, NULL
,
2846 thread_manage_health
, (void *) NULL
);
2848 PERROR("pthread_create health");
2852 /* Setup the dispatcher thread */
2853 ret
= pthread_create(&dispatcher_thread
, NULL
,
2854 relay_thread_dispatcher
, (void *) NULL
);
2856 PERROR("pthread_create dispatcher");
2857 goto exit_dispatcher
;
2860 /* Setup the worker thread */
2861 ret
= pthread_create(&worker_thread
, NULL
,
2862 relay_thread_worker
, (void *) relay_ctx
);
2864 PERROR("pthread_create worker");
2868 /* Setup the listener thread */
2869 ret
= pthread_create(&listener_thread
, NULL
,
2870 relay_thread_listener
, (void *) NULL
);
2872 PERROR("pthread_create listener");
2876 ret
= live_start_threads(live_uri
, relay_ctx
);
2878 ERR("Starting live viewer threads");
2883 ret
= pthread_join(listener_thread
, &status
);
2885 PERROR("pthread_join");
2886 goto error
; /* join error, exit without cleanup */
2890 ret
= pthread_join(worker_thread
, &status
);
2892 PERROR("pthread_join");
2893 goto error
; /* join error, exit without cleanup */
2897 ret
= pthread_join(dispatcher_thread
, &status
);
2899 PERROR("pthread_join");
2900 goto error
; /* join error, exit without cleanup */
2904 ret
= pthread_join(health_thread
, &status
);
2906 PERROR("pthread_join health thread");
2907 goto error
; /* join error, exit without cleanup */
2911 * Stop live threads only after joining other threads.
2913 live_stop_threads();
2916 utils_close_pipe(health_quit_pipe
);
2919 health_app_destroy(health_relayd
);
2921 exit_health_app_create
:
2922 lttng_ht_destroy(viewer_streams_ht
);
2924 exit_relay_ctx_viewer_streams
:
2925 lttng_ht_destroy(relay_streams_ht
);
2927 exit_relay_ctx_streams
:
2928 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2930 exit_relay_ctx_sessions
: