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.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #include <urcu/uatomic.h>
44 #include <lttng/lttng.h>
45 #include <common/common.h>
46 #include <common/compat/poll.h>
47 #include <common/compat/socket.h>
48 #include <common/compat/endian.h>
49 #include <common/compat/getenv.h>
50 #include <common/defaults.h>
51 #include <common/daemonize.h>
52 #include <common/futex.h>
53 #include <common/sessiond-comm/sessiond-comm.h>
54 #include <common/sessiond-comm/inet.h>
55 #include <common/sessiond-comm/relayd.h>
56 #include <common/uri.h>
57 #include <common/utils.h>
58 #include <common/config/config.h>
61 #include "ctf-trace.h"
64 #include "lttng-relayd.h"
66 #include "health-relayd.h"
67 #include "testpoint.h"
68 #include "viewer-stream.h"
71 #include "connection.h"
73 /* command line options */
74 char *opt_output_path
;
75 static int opt_daemon
, opt_background
;
78 * We need to wait for listener and live listener threads, as well as
79 * health check thread, before being ready to signal readiness.
81 #define NR_LTTNG_RELAY_READY 3
82 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
83 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
84 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
86 static struct lttng_uri
*control_uri
;
87 static struct lttng_uri
*data_uri
;
88 static struct lttng_uri
*live_uri
;
92 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
93 static int tracing_group_name_override
;
95 const char * const config_section_name
= "relayd";
98 * Quit pipe for all threads. This permits a single cancellation point
99 * for all threads when receiving an event on the pipe.
101 int thread_quit_pipe
[2] = { -1, -1 };
104 * This pipe is used to inform the worker thread that a command is queued and
105 * ready to be processed.
107 static int relay_conn_pipe
[2] = { -1, -1 };
109 /* Shared between threads */
110 static int dispatch_thread_exit
;
112 static pthread_t listener_thread
;
113 static pthread_t dispatcher_thread
;
114 static pthread_t worker_thread
;
115 static pthread_t health_thread
;
117 static uint64_t last_relay_stream_id
;
120 * Relay command queue.
122 * The relay_thread_listener and relay_thread_dispatcher communicate with this
125 static struct relay_conn_queue relay_conn_queue
;
127 /* buffer allocated at startup, used to store the trace data */
128 static char *data_buffer
;
129 static unsigned int data_buffer_size
;
131 /* We need those values for the file/dir creation. */
132 static uid_t relayd_uid
;
133 static gid_t relayd_gid
;
135 /* Global relay stream hash table. */
136 struct lttng_ht
*relay_streams_ht
;
138 /* Global relay viewer stream hash table. */
139 struct lttng_ht
*viewer_streams_ht
;
141 /* Global hash table that stores relay index object. */
142 struct lttng_ht
*indexes_ht
;
144 /* Relayd health monitoring */
145 struct health_app
*health_relayd
;
147 static struct option long_options
[] = {
148 { "control-port", 1, 0, 'C', },
149 { "data-port", 1, 0, 'D', },
150 { "live-port", 1, 0, 'L', },
151 { "daemonize", 0, 0, 'd', },
152 { "background", 0, 0, 'b', },
153 { "group", 1, 0, 'g', },
154 { "help", 0, 0, 'h', },
155 { "output", 1, 0, 'o', },
156 { "verbose", 0, 0, 'v', },
157 { "config", 1, 0, 'f' },
161 static const char *config_ignore_options
[] = { "help", "config" };
164 * usage function on stderr
169 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
170 fprintf(stderr
, " -h, --help Display this usage.\n");
171 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
172 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
173 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
174 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
175 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
176 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
177 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
178 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
179 fprintf(stderr
, " -f --config Load daemon configuration file\n");
183 * Take an option from the getopt output and set it in the right variable to be
186 * Return 0 on success else a negative value.
189 int set_option(int opt
, const char *arg
, const char *optname
)
195 fprintf(stderr
, "option %s", optname
);
197 fprintf(stderr
, " with arg %s\n", arg
);
201 if (lttng_is_setuid_setgid()) {
202 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
203 "-C, --control-port");
205 ret
= uri_parse(arg
, &control_uri
);
207 ERR("Invalid control URI specified");
210 if (control_uri
->port
== 0) {
211 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
216 if (lttng_is_setuid_setgid()) {
217 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
220 ret
= uri_parse(arg
, &data_uri
);
222 ERR("Invalid data URI specified");
225 if (data_uri
->port
== 0) {
226 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
231 if (lttng_is_setuid_setgid()) {
232 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
235 ret
= uri_parse(arg
, &live_uri
);
237 ERR("Invalid live URI specified");
240 if (live_uri
->port
== 0) {
241 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
252 if (lttng_is_setuid_setgid()) {
253 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
256 tracing_group_name
= strdup(arg
);
257 if (tracing_group_name
== NULL
) {
262 tracing_group_name_override
= 1;
269 if (lttng_is_setuid_setgid()) {
270 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
273 ret
= asprintf(&opt_output_path
, "%s", arg
);
276 PERROR("asprintf opt_output_path");
282 /* Verbose level can increase using multiple -v */
284 lttng_opt_verbose
= config_parse_value(arg
);
286 /* Only 3 level of verbosity (-vvv). */
287 if (lttng_opt_verbose
< 3) {
288 lttng_opt_verbose
+= 1;
293 /* Unknown option or other error.
294 * Error is printed by getopt, just return */
307 * config_entry_handler_cb used to handle options read from a config file.
308 * See config_entry_handler_cb comment in common/config/config.h for the
309 * return value conventions.
312 int config_entry_handler(const struct config_entry
*entry
, void *unused
)
316 if (!entry
|| !entry
->name
|| !entry
->value
) {
321 /* Check if the option is to be ignored */
322 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
323 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
328 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
329 /* Ignore if entry name is not fully matched. */
330 if (strcmp(entry
->name
, long_options
[i
].name
)) {
335 * If the option takes no argument on the command line, we have to
336 * check if the value is "true". We support non-zero numeric values,
339 if (!long_options
[i
].has_arg
) {
340 ret
= config_parse_value(entry
->value
);
343 WARN("Invalid configuration value \"%s\" for option %s",
344 entry
->value
, entry
->name
);
346 /* False, skip boolean config option. */
351 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
355 WARN("Unrecognized option \"%s\" in daemon configuration file.",
363 int set_options(int argc
, char **argv
)
365 int c
, ret
= 0, option_index
= 0, retval
= 0;
366 int orig_optopt
= optopt
, orig_optind
= optind
;
367 char *default_address
, *optstring
;
368 const char *config_path
= NULL
;
370 optstring
= utils_generate_optstring(long_options
,
371 sizeof(long_options
) / sizeof(struct option
));
377 /* Check for the --config option */
379 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
380 &option_index
)) != -1) {
384 } else if (c
!= 'f') {
388 if (lttng_is_setuid_setgid()) {
389 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
392 config_path
= utils_expand_path(optarg
);
394 ERR("Failed to resolve path: %s", optarg
);
399 ret
= config_get_section_entries(config_path
, config_section_name
,
400 config_entry_handler
, NULL
);
403 ERR("Invalid configuration option at line %i", ret
);
409 /* Reset getopt's global state */
410 optopt
= orig_optopt
;
411 optind
= orig_optind
;
413 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
418 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
425 /* assign default values */
426 if (control_uri
== NULL
) {
427 ret
= asprintf(&default_address
,
428 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
429 DEFAULT_NETWORK_CONTROL_PORT
);
431 PERROR("asprintf default data address");
436 ret
= uri_parse(default_address
, &control_uri
);
437 free(default_address
);
439 ERR("Invalid control URI specified");
444 if (data_uri
== NULL
) {
445 ret
= asprintf(&default_address
,
446 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
447 DEFAULT_NETWORK_DATA_PORT
);
449 PERROR("asprintf default data address");
454 ret
= uri_parse(default_address
, &data_uri
);
455 free(default_address
);
457 ERR("Invalid data URI specified");
462 if (live_uri
== NULL
) {
463 ret
= asprintf(&default_address
,
464 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
465 DEFAULT_NETWORK_VIEWER_PORT
);
467 PERROR("asprintf default viewer control address");
472 ret
= uri_parse(default_address
, &live_uri
);
473 free(default_address
);
475 ERR("Invalid viewer control URI specified");
490 void relayd_cleanup(struct relay_local_data
*relay_ctx
)
494 if (viewer_streams_ht
)
495 lttng_ht_destroy(viewer_streams_ht
);
496 if (relay_streams_ht
)
497 lttng_ht_destroy(relay_streams_ht
);
498 if (relay_ctx
&& relay_ctx
->sessions_ht
)
499 lttng_ht_destroy(relay_ctx
->sessions_ht
);
502 /* free the dynamically allocated opt_output_path */
503 free(opt_output_path
);
505 /* Close thread quit pipes */
506 utils_close_pipe(thread_quit_pipe
);
508 uri_free(control_uri
);
510 /* Live URI is freed in the live thread. */
512 if (tracing_group_name_override
) {
513 free((void *) tracing_group_name
);
518 * Write to writable pipe used to notify a thread.
521 int notify_thread_pipe(int wpipe
)
525 ret
= lttng_write(wpipe
, "!", 1);
527 PERROR("write poll pipe");
536 int notify_health_quit_pipe(int *pipe
)
540 ret
= lttng_write(pipe
[1], "4", 1);
542 PERROR("write relay health quit");
551 * Stop all relayd and relayd-live threads.
553 int lttng_relay_stop_threads(void)
557 /* Stopping all threads */
558 DBG("Terminating all threads");
559 if (notify_thread_pipe(thread_quit_pipe
[1])) {
560 ERR("write error on thread quit pipe");
564 if (notify_health_quit_pipe(health_quit_pipe
)) {
565 ERR("write error on health quit pipe");
568 /* Dispatch thread */
569 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
570 futex_nto1_wake(&relay_conn_queue
.futex
);
572 if (relayd_live_stop()) {
573 ERR("Error stopping live threads");
580 * Signal handler for the daemon
582 * Simply stop all worker threads, leaving main() return gracefully after
583 * joining all threads and calling cleanup().
586 void sighandler(int sig
)
590 DBG("SIGPIPE caught");
593 DBG("SIGINT caught");
594 if (lttng_relay_stop_threads()) {
595 ERR("Error stopping threads");
599 DBG("SIGTERM caught");
600 if (lttng_relay_stop_threads()) {
601 ERR("Error stopping threads");
605 CMM_STORE_SHARED(recv_child_signal
, 1);
613 * Setup signal handler for :
614 * SIGINT, SIGTERM, SIGPIPE
617 int set_signal_handler(void)
623 if ((ret
= sigemptyset(&sigset
)) < 0) {
624 PERROR("sigemptyset");
628 sa
.sa_handler
= sighandler
;
631 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
636 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
641 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
646 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
651 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
656 void lttng_relay_notify_ready(void)
658 /* Notify the parent of the fork() process that we are ready. */
659 if (opt_daemon
|| opt_background
) {
660 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
661 kill(child_ppid
, SIGUSR1
);
667 * Init thread quit pipe.
669 * Return -1 on error or 0 if all pipes are created.
672 int init_thread_quit_pipe(void)
676 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
682 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
685 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
689 if (events
== NULL
|| size
== 0) {
694 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
700 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
712 * Check if the thread quit pipe was triggered.
714 * Return 1 if it was triggered else 0;
717 int check_thread_quit_pipe(int fd
, uint32_t events
)
719 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
727 * Create and init socket from uri.
730 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
733 struct lttcomm_sock
*sock
= NULL
;
735 sock
= lttcomm_alloc_sock_from_uri(uri
);
737 ERR("Allocating socket");
741 ret
= lttcomm_create_sock(sock
);
745 DBG("Listening on sock %d", sock
->fd
);
747 ret
= sock
->ops
->bind(sock
);
752 ret
= sock
->ops
->listen(sock
, -1);
762 lttcomm_destroy_sock(sock
);
768 * Return nonzero if stream needs to be closed.
771 int close_stream_check(struct relay_stream
*stream
)
773 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
775 * We are about to close the stream so set the data pending flag to 1
776 * which will make the end data pending command skip the stream which
777 * is now closed and ready. Note that after proceeding to a file close,
778 * the written file is ready for reading.
780 stream
->data_pending_check_done
= 1;
786 static void try_close_stream(struct relay_session
*session
,
787 struct relay_stream
*stream
)
790 struct ctf_trace
*ctf_trace
;
795 if (!close_stream_check(stream
)) {
796 /* Can't close it, not ready for that. */
800 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
804 pthread_mutex_lock(&session
->viewer_ready_lock
);
805 ctf_trace
->invalid_flag
= 1;
806 pthread_mutex_unlock(&session
->viewer_ready_lock
);
808 ret
= stream_close(session
, stream
);
809 if (ret
|| session
->snapshot
) {
810 /* Already close thus the ctf trace is being or has been destroyed. */
814 ctf_trace_try_destroy(session
, ctf_trace
);
821 * This thread manages the listening for new connections on the network
824 void *relay_thread_listener(void *data
)
826 int i
, ret
, pollfd
, err
= -1;
827 uint32_t revents
, nb_fd
;
828 struct lttng_poll_event events
;
829 struct lttcomm_sock
*control_sock
, *data_sock
;
831 DBG("[thread] Relay listener started");
833 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
835 health_code_update();
837 control_sock
= relay_init_sock(control_uri
);
839 goto error_sock_control
;
842 data_sock
= relay_init_sock(data_uri
);
844 goto error_sock_relay
;
848 * Pass 3 as size here for the thread quit pipe, control and data socket.
850 ret
= create_thread_poll_set(&events
, 3);
852 goto error_create_poll
;
855 /* Add the control socket */
856 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
861 /* Add the data socket */
862 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
867 lttng_relay_notify_ready();
869 if (testpoint(relayd_thread_listener
)) {
870 goto error_testpoint
;
874 health_code_update();
876 DBG("Listener accepting connections");
880 ret
= lttng_poll_wait(&events
, -1);
884 * Restart interrupted system call.
886 if (errno
== EINTR
) {
894 DBG("Relay new connection received");
895 for (i
= 0; i
< nb_fd
; i
++) {
896 health_code_update();
898 /* Fetch once the poll data */
899 revents
= LTTNG_POLL_GETEV(&events
, i
);
900 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
903 /* No activity for this FD (poll implementation). */
907 /* Thread quit pipe has been closed. Killing thread. */
908 ret
= check_thread_quit_pipe(pollfd
, revents
);
914 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
915 ERR("socket poll error");
917 } else if (revents
& LPOLLIN
) {
919 * Get allocated in this thread, enqueued to a global queue,
920 * dequeued and freed in the worker thread.
923 struct relay_connection
*new_conn
;
924 struct lttcomm_sock
*newsock
;
926 new_conn
= connection_create();
931 if (pollfd
== data_sock
->fd
) {
932 new_conn
->type
= RELAY_DATA
;
933 newsock
= data_sock
->ops
->accept(data_sock
);
934 DBG("Relay data connection accepted, socket %d",
937 assert(pollfd
== control_sock
->fd
);
938 new_conn
->type
= RELAY_CONTROL
;
939 newsock
= control_sock
->ops
->accept(control_sock
);
940 DBG("Relay control connection accepted, socket %d",
944 PERROR("accepting sock");
945 connection_free(new_conn
);
949 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
952 PERROR("setsockopt inet");
953 lttcomm_destroy_sock(newsock
);
954 connection_free(new_conn
);
957 new_conn
->sock
= newsock
;
959 /* Enqueue request for the dispatcher thread. */
960 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
964 * Wake the dispatch queue futex. Implicit memory barrier with
965 * the exchange in cds_wfcq_enqueue.
967 futex_nto1_wake(&relay_conn_queue
.futex
);
976 lttng_poll_clean(&events
);
978 if (data_sock
->fd
>= 0) {
979 ret
= data_sock
->ops
->close(data_sock
);
984 lttcomm_destroy_sock(data_sock
);
986 if (control_sock
->fd
>= 0) {
987 ret
= control_sock
->ops
->close(control_sock
);
992 lttcomm_destroy_sock(control_sock
);
996 ERR("Health error occurred in %s", __func__
);
998 health_unregister(health_relayd
);
999 DBG("Relay listener thread cleanup complete");
1000 lttng_relay_stop_threads();
1005 * This thread manages the dispatching of the requests to worker threads
1008 void *relay_thread_dispatcher(void *data
)
1012 struct cds_wfcq_node
*node
;
1013 struct relay_connection
*new_conn
= NULL
;
1015 DBG("[thread] Relay dispatcher started");
1017 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1019 if (testpoint(relayd_thread_dispatcher
)) {
1020 goto error_testpoint
;
1023 health_code_update();
1025 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1026 health_code_update();
1028 /* Atomically prepare the queue futex */
1029 futex_nto1_prepare(&relay_conn_queue
.futex
);
1032 health_code_update();
1034 /* Dequeue commands */
1035 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1036 &relay_conn_queue
.tail
);
1038 DBG("Woken up but nothing in the relay command queue");
1039 /* Continue thread execution */
1042 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1044 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1047 * Inform worker thread of the new request. This call is blocking
1048 * so we can be assured that the data will be read at some point in
1049 * time or wait to the end of the world :)
1051 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1053 PERROR("write connection pipe");
1054 connection_destroy(new_conn
);
1057 } while (node
!= NULL
);
1059 /* Futex wait on queue. Blocking call on futex() */
1060 health_poll_entry();
1061 futex_nto1_wait(&relay_conn_queue
.futex
);
1065 /* Normal exit, no error */
1072 ERR("Health error occurred in %s", __func__
);
1074 health_unregister(health_relayd
);
1075 DBG("Dispatch thread dying");
1076 lttng_relay_stop_threads();
1080 static void try_close_streams(struct relay_session
*session
)
1082 struct ctf_trace
*ctf_trace
;
1083 struct lttng_ht_iter iter
;
1087 pthread_mutex_lock(&session
->viewer_ready_lock
);
1089 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
1091 struct relay_stream
*stream
;
1093 /* Close streams. */
1094 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
1095 stream_close(session
, stream
);
1098 ctf_trace
->invalid_flag
= 1;
1099 ctf_trace_try_destroy(session
, ctf_trace
);
1102 pthread_mutex_unlock(&session
->viewer_ready_lock
);
1106 * Try to destroy a session within a connection.
1108 static void destroy_session(struct relay_session
*session
,
1109 struct lttng_ht
*sessions_ht
)
1112 assert(sessions_ht
);
1114 /* Indicate that this session can be destroyed from now on. */
1115 session
->close_flag
= 1;
1117 try_close_streams(session
);
1120 * This will try to delete and destroy the session if no viewer is attached
1121 * to it meaning the refcount is down to zero.
1123 session_try_destroy(sessions_ht
, session
);
1127 * Copy index data from the control port to a given index object.
1129 static void copy_index_control_data(struct relay_index
*index
,
1130 struct lttcomm_relayd_index
*data
)
1136 * The index on disk is encoded in big endian, so we don't need to convert
1137 * the data received on the network. The data_offset value is NEVER
1138 * modified here and is updated by the data thread.
1140 index
->index_data
.packet_size
= data
->packet_size
;
1141 index
->index_data
.content_size
= data
->content_size
;
1142 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
1143 index
->index_data
.timestamp_end
= data
->timestamp_end
;
1144 index
->index_data
.events_discarded
= data
->events_discarded
;
1145 index
->index_data
.stream_id
= data
->stream_id
;
1149 * Handle the RELAYD_CREATE_SESSION command.
1151 * On success, send back the session id or else return a negative value.
1154 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1155 struct relay_connection
*conn
)
1157 int ret
= 0, send_ret
;
1158 struct relay_session
*session
;
1159 struct lttcomm_relayd_status_session reply
;
1164 memset(&reply
, 0, sizeof(reply
));
1166 session
= session_create();
1171 session
->minor
= conn
->minor
;
1172 session
->major
= conn
->major
;
1173 conn
->session_id
= session
->id
;
1174 conn
->session
= session
;
1176 reply
.session_id
= htobe64(session
->id
);
1178 switch (conn
->minor
) {
1183 case 4: /* LTTng sessiond 2.4 */
1185 ret
= cmd_create_session_2_4(conn
, session
);
1188 lttng_ht_add_unique_u64(conn
->sessions_ht
, &session
->session_n
);
1189 DBG("Created session %" PRIu64
, session
->id
);
1193 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1195 reply
.ret_code
= htobe32(LTTNG_OK
);
1198 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1200 ERR("Relayd sending session id");
1208 * When we have received all the streams and the metadata for a channel,
1209 * we make them visible to the viewer threads.
1212 void set_viewer_ready_flag(struct relay_connection
*conn
)
1214 struct relay_stream
*stream
, *tmp_stream
;
1216 pthread_mutex_lock(&conn
->session
->viewer_ready_lock
);
1217 cds_list_for_each_entry_safe(stream
, tmp_stream
, &conn
->recv_head
,
1219 stream
->viewer_ready
= 1;
1220 cds_list_del(&stream
->recv_list
);
1222 pthread_mutex_unlock(&conn
->session
->viewer_ready_lock
);
1227 * Add a recv handle node to the connection recv list with the given stream
1228 * handle. A new node is allocated thus must be freed when the node is deleted
1231 static void queue_stream(struct relay_stream
*stream
,
1232 struct relay_connection
*conn
)
1237 cds_list_add(&stream
->recv_list
, &conn
->recv_head
);
1241 * relay_add_stream: allocate a new stream for a session
1244 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1245 struct relay_connection
*conn
)
1248 struct relay_session
*session
= conn
->session
;
1249 struct relay_stream
*stream
= NULL
;
1250 struct lttcomm_relayd_status_stream reply
;
1251 struct ctf_trace
*trace
;
1253 if (!session
|| conn
->version_check_done
== 0) {
1254 ERR("Trying to add a stream before version check");
1256 goto end_no_session
;
1259 stream
= zmalloc(sizeof(struct relay_stream
));
1260 if (stream
== NULL
) {
1261 PERROR("relay stream zmalloc");
1263 goto end_no_session
;
1266 switch (conn
->minor
) {
1267 case 1: /* LTTng sessiond 2.1 */
1268 ret
= cmd_recv_stream_2_1(conn
, stream
);
1270 case 2: /* LTTng sessiond 2.2 */
1272 ret
= cmd_recv_stream_2_2(conn
, stream
);
1276 goto err_free_stream
;
1280 stream
->stream_handle
= ++last_relay_stream_id
;
1281 stream
->prev_seq
= -1ULL;
1282 stream
->session_id
= session
->id
;
1283 stream
->index_fd
= -1;
1284 stream
->read_index_fd
= -1;
1285 stream
->ctf_stream_id
= -1ULL;
1286 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
1287 pthread_mutex_init(&stream
->lock
, NULL
);
1289 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1291 ERR("relay creating output directory");
1296 * No need to use run_as API here because whatever we receives, the relayd
1297 * uses its own credentials for the stream files.
1299 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1300 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1302 ERR("Create output file");
1306 if (stream
->tracefile_size
) {
1307 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1309 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1312 trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, stream
->path_name
);
1314 trace
= ctf_trace_create(stream
->path_name
);
1319 ctf_trace_add(session
->ctf_traces_ht
, trace
);
1321 ctf_trace_get_ref(trace
);
1323 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1324 stream
->metadata_flag
= 1;
1325 /* Assign quick reference to the metadata stream in the trace. */
1326 trace
->metadata_stream
= stream
;
1330 * Add the stream in the recv list of the connection. Once the end stream
1331 * message is received, this list is emptied and streams are set with the
1332 * viewer ready flag.
1334 queue_stream(stream
, conn
);
1337 * Both in the ctf_trace object and the global stream ht since the data
1338 * side of the relayd does not have the concept of session.
1340 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
1341 cds_list_add_tail(&stream
->trace_list
, &trace
->stream_list
);
1343 session
->stream_count
++;
1345 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1346 stream
->stream_handle
);
1349 memset(&reply
, 0, sizeof(reply
));
1350 reply
.handle
= htobe64(stream
->stream_handle
);
1351 /* send the session id to the client or a negative return code on error */
1353 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1354 /* stream was not properly added to the ht, so free it */
1357 reply
.ret_code
= htobe32(LTTNG_OK
);
1360 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1361 sizeof(struct lttcomm_relayd_status_stream
), 0);
1363 ERR("Relay sending stream id");
1372 free(stream
->path_name
);
1373 free(stream
->channel_name
);
1379 * relay_close_stream: close a specific stream
1382 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1383 struct relay_connection
*conn
)
1386 struct relay_session
*session
= conn
->session
;
1387 struct lttcomm_relayd_close_stream stream_info
;
1388 struct lttcomm_relayd_generic_reply reply
;
1389 struct relay_stream
*stream
;
1391 DBG("Close stream received");
1393 if (!session
|| conn
->version_check_done
== 0) {
1394 ERR("Trying to close a stream before version check");
1396 goto end_no_session
;
1399 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1400 sizeof(struct lttcomm_relayd_close_stream
), 0);
1401 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1403 /* Orderly shutdown. Not necessary to print an error. */
1404 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1406 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1409 goto end_no_session
;
1413 stream
= stream_find_by_id(relay_streams_ht
,
1414 be64toh(stream_info
.stream_id
));
1420 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1421 stream
->close_flag
= 1;
1422 session
->stream_count
--;
1424 /* Check if we can close it or else the data will do it. */
1425 try_close_stream(session
, stream
);
1430 memset(&reply
, 0, sizeof(reply
));
1432 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1434 reply
.ret_code
= htobe32(LTTNG_OK
);
1436 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1437 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1439 ERR("Relay sending stream id");
1448 * relay_unknown_command: send -1 if received unknown command
1451 void relay_unknown_command(struct relay_connection
*conn
)
1453 struct lttcomm_relayd_generic_reply reply
;
1456 memset(&reply
, 0, sizeof(reply
));
1457 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1458 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1459 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1461 ERR("Relay sending unknown command");
1466 * relay_start: send an acknowledgment to the client to tell if we are
1467 * ready to receive data. We are ready if a session is established.
1470 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1471 struct relay_connection
*conn
)
1473 int ret
= htobe32(LTTNG_OK
);
1474 struct lttcomm_relayd_generic_reply reply
;
1475 struct relay_session
*session
= conn
->session
;
1478 DBG("Trying to start the streaming without a session established");
1479 ret
= htobe32(LTTNG_ERR_UNK
);
1482 memset(&reply
, 0, sizeof(reply
));
1483 reply
.ret_code
= ret
;
1484 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1485 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1487 ERR("Relay sending start ack");
1494 * Append padding to the file pointed by the file descriptor fd.
1496 static int write_padding_to_file(int fd
, uint32_t size
)
1505 zeros
= zmalloc(size
);
1506 if (zeros
== NULL
) {
1507 PERROR("zmalloc zeros for padding");
1512 ret
= lttng_write(fd
, zeros
, size
);
1514 PERROR("write padding to file");
1524 * relay_recv_metadata: receive the metada for the session.
1527 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1528 struct relay_connection
*conn
)
1530 int ret
= htobe32(LTTNG_OK
);
1532 struct relay_session
*session
= conn
->session
;
1533 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1534 struct relay_stream
*metadata_stream
;
1535 uint64_t data_size
, payload_size
;
1536 struct ctf_trace
*ctf_trace
;
1539 ERR("Metadata sent before version check");
1544 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1545 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1546 ERR("Incorrect data size");
1550 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1552 if (data_buffer_size
< data_size
) {
1553 /* In case the realloc fails, we can free the memory */
1556 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1557 if (!tmp_data_ptr
) {
1558 ERR("Allocating data buffer");
1563 data_buffer
= tmp_data_ptr
;
1564 data_buffer_size
= data_size
;
1566 memset(data_buffer
, 0, data_size
);
1567 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1568 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1569 if (ret
< 0 || ret
!= data_size
) {
1571 /* Orderly shutdown. Not necessary to print an error. */
1572 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1574 ERR("Relay didn't receive the whole metadata");
1579 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1582 metadata_stream
= stream_find_by_id(relay_streams_ht
,
1583 be64toh(metadata_struct
->stream_id
));
1584 if (!metadata_stream
) {
1589 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1591 if (size_ret
< payload_size
) {
1592 ERR("Relay error writing metadata on file");
1597 ret
= write_padding_to_file(metadata_stream
->fd
,
1598 be32toh(metadata_struct
->padding_size
));
1603 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1604 metadata_stream
->path_name
);
1606 ctf_trace
->metadata_received
+=
1607 payload_size
+ be32toh(metadata_struct
->padding_size
);
1609 DBG2("Relay metadata written");
1618 * relay_send_version: send relayd version number
1621 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1622 struct relay_connection
*conn
)
1625 struct lttcomm_relayd_version reply
, msg
;
1629 conn
->version_check_done
= 1;
1631 /* Get version from the other side. */
1632 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1633 if (ret
< 0 || ret
!= sizeof(msg
)) {
1635 /* Orderly shutdown. Not necessary to print an error. */
1636 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1638 ERR("Relay failed to receive the version values.");
1644 memset(&reply
, 0, sizeof(reply
));
1645 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1646 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1648 /* Major versions must be the same */
1649 if (reply
.major
!= be32toh(msg
.major
)) {
1650 DBG("Incompatible major versions (%u vs %u), deleting session",
1651 reply
.major
, be32toh(msg
.major
));
1652 destroy_session(conn
->session
, conn
->sessions_ht
);
1657 conn
->major
= reply
.major
;
1658 /* We adapt to the lowest compatible version */
1659 if (reply
.minor
<= be32toh(msg
.minor
)) {
1660 conn
->minor
= reply
.minor
;
1662 conn
->minor
= be32toh(msg
.minor
);
1665 reply
.major
= htobe32(reply
.major
);
1666 reply
.minor
= htobe32(reply
.minor
);
1667 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1668 sizeof(struct lttcomm_relayd_version
), 0);
1670 ERR("Relay sending version");
1673 DBG("Version check done using protocol %u.%u", conn
->major
,
1681 * Check for data pending for a given stream id from the session daemon.
1684 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1685 struct relay_connection
*conn
)
1687 struct relay_session
*session
= conn
->session
;
1688 struct lttcomm_relayd_data_pending msg
;
1689 struct lttcomm_relayd_generic_reply reply
;
1690 struct relay_stream
*stream
;
1692 uint64_t last_net_seq_num
, stream_id
;
1694 DBG("Data pending command received");
1696 if (!session
|| conn
->version_check_done
== 0) {
1697 ERR("Trying to check for data before version check");
1699 goto end_no_session
;
1702 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1703 if (ret
< sizeof(msg
)) {
1705 /* Orderly shutdown. Not necessary to print an error. */
1706 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1708 ERR("Relay didn't receive valid data_pending struct size : %d",
1712 goto end_no_session
;
1715 stream_id
= be64toh(msg
.stream_id
);
1716 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1719 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
1720 if (stream
== NULL
) {
1725 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1726 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1729 /* Avoid wrapping issue */
1730 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1731 /* Data has in fact been written and is NOT pending */
1734 /* Data still being streamed thus pending */
1738 /* Pending check is now done. */
1739 stream
->data_pending_check_done
= 1;
1744 memset(&reply
, 0, sizeof(reply
));
1745 reply
.ret_code
= htobe32(ret
);
1746 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1748 ERR("Relay data pending ret code failed");
1756 * Wait for the control socket to reach a quiescent state.
1758 * Note that for now, when receiving this command from the session daemon, this
1759 * means that every subsequent commands or data received on the control socket
1760 * has been handled. So, this is why we simply return OK here.
1763 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1764 struct relay_connection
*conn
)
1768 struct relay_stream
*stream
;
1769 struct lttng_ht_iter iter
;
1770 struct lttcomm_relayd_quiescent_control msg
;
1771 struct lttcomm_relayd_generic_reply reply
;
1773 DBG("Checking quiescent state on control socket");
1775 if (!conn
->session
|| conn
->version_check_done
== 0) {
1776 ERR("Trying to check for data before version check");
1778 goto end_no_session
;
1781 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1782 if (ret
< sizeof(msg
)) {
1784 /* Orderly shutdown. Not necessary to print an error. */
1785 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1787 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1791 goto end_no_session
;
1794 stream_id
= be64toh(msg
.stream_id
);
1797 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1799 if (stream
->stream_handle
== stream_id
) {
1800 stream
->data_pending_check_done
= 1;
1801 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1808 memset(&reply
, 0, sizeof(reply
));
1809 reply
.ret_code
= htobe32(LTTNG_OK
);
1810 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1812 ERR("Relay data quiescent control ret code failed");
1820 * Initialize a data pending command. This means that a client is about to ask
1821 * for data pending for each stream he/she holds. Simply iterate over all
1822 * streams of a session and set the data_pending_check_done flag.
1824 * This command returns to the client a LTTNG_OK code.
1827 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1828 struct relay_connection
*conn
)
1831 struct lttng_ht_iter iter
;
1832 struct lttcomm_relayd_begin_data_pending msg
;
1833 struct lttcomm_relayd_generic_reply reply
;
1834 struct relay_stream
*stream
;
1835 uint64_t session_id
;
1840 DBG("Init streams for data pending");
1842 if (!conn
->session
|| conn
->version_check_done
== 0) {
1843 ERR("Trying to check for data before version check");
1845 goto end_no_session
;
1848 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1849 if (ret
< sizeof(msg
)) {
1851 /* Orderly shutdown. Not necessary to print an error. */
1852 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1854 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1858 goto end_no_session
;
1861 session_id
= be64toh(msg
.session_id
);
1864 * Iterate over all streams to set the begin data pending flag. For now, the
1865 * streams are indexed by stream handle so we have to iterate over all
1866 * streams to find the one associated with the right session_id.
1869 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1871 if (stream
->session_id
== session_id
) {
1872 stream
->data_pending_check_done
= 0;
1873 DBG("Set begin data pending flag to stream %" PRIu64
,
1874 stream
->stream_handle
);
1879 memset(&reply
, 0, sizeof(reply
));
1880 /* All good, send back reply. */
1881 reply
.ret_code
= htobe32(LTTNG_OK
);
1883 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1885 ERR("Relay begin data pending send reply failed");
1893 * End data pending command. This will check, for a given session id, if each
1894 * stream associated with it has its data_pending_check_done flag set. If not,
1895 * this means that the client lost track of the stream but the data is still
1896 * being streamed on our side. In this case, we inform the client that data is
1899 * Return to the client if there is data in flight or not with a ret_code.
1902 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1903 struct relay_connection
*conn
)
1906 struct lttng_ht_iter iter
;
1907 struct lttcomm_relayd_end_data_pending msg
;
1908 struct lttcomm_relayd_generic_reply reply
;
1909 struct relay_stream
*stream
;
1910 uint64_t session_id
;
1911 uint32_t is_data_inflight
= 0;
1916 DBG("End data pending command");
1918 if (!conn
->session
|| conn
->version_check_done
== 0) {
1919 ERR("Trying to check for data before version check");
1921 goto end_no_session
;
1924 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1925 if (ret
< sizeof(msg
)) {
1927 /* Orderly shutdown. Not necessary to print an error. */
1928 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1930 ERR("Relay didn't receive valid end data_pending struct size: %d",
1934 goto end_no_session
;
1937 session_id
= be64toh(msg
.session_id
);
1939 /* Iterate over all streams to see if the begin data pending flag is set. */
1941 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1943 if (stream
->session_id
== session_id
&&
1944 !stream
->data_pending_check_done
&& !stream
->terminated_flag
) {
1945 is_data_inflight
= 1;
1946 DBG("Data is still in flight for stream %" PRIu64
,
1947 stream
->stream_handle
);
1953 memset(&reply
, 0, sizeof(reply
));
1954 /* All good, send back reply. */
1955 reply
.ret_code
= htobe32(is_data_inflight
);
1957 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1959 ERR("Relay end data pending send reply failed");
1967 * Receive an index for a specific stream.
1969 * Return 0 on success else a negative value.
1972 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1973 struct relay_connection
*conn
)
1975 int ret
, send_ret
, index_created
= 0;
1976 struct relay_session
*session
= conn
->session
;
1977 struct lttcomm_relayd_index index_info
;
1978 struct relay_index
*index
, *wr_index
= NULL
;
1979 struct lttcomm_relayd_generic_reply reply
;
1980 struct relay_stream
*stream
;
1981 uint64_t net_seq_num
;
1985 DBG("Relay receiving index");
1987 if (!session
|| conn
->version_check_done
== 0) {
1988 ERR("Trying to close a stream before version check");
1990 goto end_no_session
;
1993 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
1994 sizeof(index_info
), 0);
1995 if (ret
< sizeof(index_info
)) {
1997 /* Orderly shutdown. Not necessary to print an error. */
1998 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2000 ERR("Relay didn't receive valid index struct size : %d", ret
);
2003 goto end_no_session
;
2006 net_seq_num
= be64toh(index_info
.net_seq_num
);
2009 stream
= stream_find_by_id(relay_streams_ht
,
2010 be64toh(index_info
.relay_stream_id
));
2013 goto end_rcu_unlock
;
2016 /* Live beacon handling */
2017 if (index_info
.packet_size
== 0) {
2018 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
2021 * Only flag a stream inactive when it has already received data
2022 * and no indexes are in flight.
2024 if (stream
->total_index_received
> 0 && stream
->indexes_in_flight
== 0) {
2025 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
2028 goto end_rcu_unlock
;
2030 stream
->beacon_ts_end
= -1ULL;
2033 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
2035 /* A successful creation will add the object to the HT. */
2036 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
2038 goto end_rcu_unlock
;
2041 stream
->indexes_in_flight
++;
2044 copy_index_control_data(index
, &index_info
);
2045 if (stream
->ctf_stream_id
== -1ULL) {
2046 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
2049 if (index_created
) {
2051 * Try to add the relay index object to the hash table. If an object
2052 * already exist, destroy back the index created, set the data in this
2053 * object and write it on disk.
2055 relay_index_add(index
, &wr_index
);
2057 copy_index_control_data(wr_index
, &index_info
);
2061 /* The index already exists so write it on disk. */
2065 /* Do we have a writable ready index to write on disk. */
2067 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2069 goto end_rcu_unlock
;
2071 stream
->total_index_received
++;
2072 stream
->indexes_in_flight
--;
2073 assert(stream
->indexes_in_flight
>= 0);
2079 memset(&reply
, 0, sizeof(reply
));
2081 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2083 reply
.ret_code
= htobe32(LTTNG_OK
);
2085 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2087 ERR("Relay sending close index id reply");
2096 * Receive the streams_sent message.
2098 * Return 0 on success else a negative value.
2101 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2102 struct relay_connection
*conn
)
2105 struct lttcomm_relayd_generic_reply reply
;
2109 DBG("Relay receiving streams_sent");
2111 if (!conn
->session
|| conn
->version_check_done
== 0) {
2112 ERR("Trying to close a stream before version check");
2114 goto end_no_session
;
2118 * Flag every pending stream in the connection recv list that they are
2119 * ready to be used by the viewer.
2121 set_viewer_ready_flag(conn
);
2124 * Inform the viewer that there are new streams in the session.
2126 if (conn
->session
->viewer_refcount
) {
2127 uatomic_set(&conn
->session
->new_streams
, 1);
2130 memset(&reply
, 0, sizeof(reply
));
2131 reply
.ret_code
= htobe32(LTTNG_OK
);
2132 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2134 ERR("Relay sending sent_stream reply");
2146 * Process the commands received on the control socket
2149 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2150 struct relay_connection
*conn
)
2154 switch (be32toh(recv_hdr
->cmd
)) {
2155 case RELAYD_CREATE_SESSION
:
2156 ret
= relay_create_session(recv_hdr
, conn
);
2158 case RELAYD_ADD_STREAM
:
2159 ret
= relay_add_stream(recv_hdr
, conn
);
2161 case RELAYD_START_DATA
:
2162 ret
= relay_start(recv_hdr
, conn
);
2164 case RELAYD_SEND_METADATA
:
2165 ret
= relay_recv_metadata(recv_hdr
, conn
);
2167 case RELAYD_VERSION
:
2168 ret
= relay_send_version(recv_hdr
, conn
);
2170 case RELAYD_CLOSE_STREAM
:
2171 ret
= relay_close_stream(recv_hdr
, conn
);
2173 case RELAYD_DATA_PENDING
:
2174 ret
= relay_data_pending(recv_hdr
, conn
);
2176 case RELAYD_QUIESCENT_CONTROL
:
2177 ret
= relay_quiescent_control(recv_hdr
, conn
);
2179 case RELAYD_BEGIN_DATA_PENDING
:
2180 ret
= relay_begin_data_pending(recv_hdr
, conn
);
2182 case RELAYD_END_DATA_PENDING
:
2183 ret
= relay_end_data_pending(recv_hdr
, conn
);
2185 case RELAYD_SEND_INDEX
:
2186 ret
= relay_recv_index(recv_hdr
, conn
);
2188 case RELAYD_STREAMS_SENT
:
2189 ret
= relay_streams_sent(recv_hdr
, conn
);
2191 case RELAYD_UPDATE_SYNC_INFO
:
2193 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2194 relay_unknown_command(conn
);
2204 * Handle index for a data stream.
2206 * RCU read side lock MUST be acquired.
2208 * Return 0 on success else a negative value.
2210 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2213 int ret
= 0, index_created
= 0;
2214 uint64_t stream_id
, data_offset
;
2215 struct relay_index
*index
, *wr_index
= NULL
;
2219 stream_id
= stream
->stream_handle
;
2220 /* Get data offset because we are about to update the index. */
2221 data_offset
= htobe64(stream
->tracefile_size_current
);
2224 * Lookup for an existing index for that stream id/sequence number. If on
2225 * exists, the control thread already received the data for it thus we need
2226 * to write it on disk.
2228 index
= relay_index_find(stream_id
, net_seq_num
);
2230 /* A successful creation will add the object to the HT. */
2231 index
= relay_index_create(stream_id
, net_seq_num
);
2237 stream
->indexes_in_flight
++;
2240 if (rotate_index
|| stream
->index_fd
< 0) {
2241 index
->to_close_fd
= stream
->index_fd
;
2242 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2243 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2244 stream
->tracefile_count_current
);
2246 /* This will close the stream's index fd if one. */
2247 relay_index_free_safe(index
);
2250 stream
->index_fd
= ret
;
2252 index
->fd
= stream
->index_fd
;
2253 index
->index_data
.offset
= data_offset
;
2255 if (index_created
) {
2257 * Try to add the relay index object to the hash table. If an object
2258 * already exist, destroy back the index created and set the data.
2260 relay_index_add(index
, &wr_index
);
2262 /* Copy back data from the created index. */
2263 wr_index
->fd
= index
->fd
;
2264 wr_index
->to_close_fd
= index
->to_close_fd
;
2265 wr_index
->index_data
.offset
= data_offset
;
2269 /* The index already exists so write it on disk. */
2273 /* Do we have a writable ready index to write on disk. */
2275 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2279 stream
->total_index_received
++;
2280 stream
->indexes_in_flight
--;
2281 assert(stream
->indexes_in_flight
>= 0);
2289 * relay_process_data: Process the data received on the data socket
2292 int relay_process_data(struct relay_connection
*conn
)
2294 int ret
= 0, rotate_index
= 0;
2296 struct relay_stream
*stream
;
2297 struct lttcomm_relayd_data_hdr data_hdr
;
2299 uint64_t net_seq_num
;
2301 struct relay_session
*session
;
2305 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2306 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2309 /* Orderly shutdown. Not necessary to print an error. */
2310 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2312 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2318 stream_id
= be64toh(data_hdr
.stream_id
);
2321 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
2324 goto end_rcu_unlock
;
2327 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
2330 data_size
= be32toh(data_hdr
.data_size
);
2331 if (data_buffer_size
< data_size
) {
2334 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2335 if (!tmp_data_ptr
) {
2336 ERR("Allocating data buffer");
2339 goto end_rcu_unlock
;
2341 data_buffer
= tmp_data_ptr
;
2342 data_buffer_size
= data_size
;
2344 memset(data_buffer
, 0, data_size
);
2346 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2348 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2349 data_size
, stream_id
, net_seq_num
);
2350 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
2353 /* Orderly shutdown. Not necessary to print an error. */
2354 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2357 goto end_rcu_unlock
;
2360 /* Check if a rotation is needed. */
2361 if (stream
->tracefile_size
> 0 &&
2362 (stream
->tracefile_size_current
+ data_size
) >
2363 stream
->tracefile_size
) {
2364 struct relay_viewer_stream
*vstream
;
2367 new_id
= (stream
->tracefile_count_current
+ 1) %
2368 stream
->tracefile_count
;
2370 * When we wrap-around back to 0, we start overwriting old
2373 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2374 stream
->tracefile_overwrite
= 1;
2376 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2377 if (stream
->tracefile_overwrite
) {
2378 stream
->oldest_tracefile_id
=
2379 (stream
->oldest_tracefile_id
+ 1) %
2380 stream
->tracefile_count
;
2382 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2385 * The viewer is reading a file about to be
2386 * overwritten. Close the FDs it is
2387 * currently using and let it handle the fault.
2389 if (vstream
->tracefile_count_current
== new_id
) {
2390 pthread_mutex_lock(&vstream
->overwrite_lock
);
2391 vstream
->abort_flag
= 1;
2392 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2393 DBG("Streaming side setting abort_flag on stream %s_%" PRIu64
"\n",
2394 stream
->channel_name
, new_id
);
2395 } else if (vstream
->tracefile_count_current
==
2396 stream
->tracefile_count_current
) {
2398 * The reader and writer were in the
2399 * same trace file, inform the viewer
2400 * that no new index will ever be added
2403 vstream
->close_write_flag
= 1;
2406 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2407 stream
->tracefile_size
, stream
->tracefile_count
,
2408 relayd_uid
, relayd_gid
, stream
->fd
,
2409 &(stream
->tracefile_count_current
), &stream
->fd
);
2410 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2412 ERR("Rotating stream output file");
2413 goto end_rcu_unlock
;
2415 /* Reset current size because we just perform a stream rotation. */
2416 stream
->tracefile_size_current
= 0;
2421 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2422 * index are NOT supported.
2424 if (session
->minor
>= 4 && !session
->snapshot
) {
2425 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2427 goto end_rcu_unlock
;
2431 /* Write data to stream output fd. */
2432 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2433 if (size_ret
< data_size
) {
2434 ERR("Relay error writing data to file");
2436 goto end_rcu_unlock
;
2439 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2440 ret
, stream
->stream_handle
);
2442 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2444 goto end_rcu_unlock
;
2446 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2448 stream
->prev_seq
= net_seq_num
;
2450 try_close_stream(session
, stream
);
2459 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2465 (void) lttng_poll_del(events
, pollfd
);
2467 ret
= close(pollfd
);
2469 ERR("Closing pollfd %d", pollfd
);
2473 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
2474 struct relay_connection
*conn
)
2476 assert(relay_connections_ht
);
2479 connection_delete(relay_connections_ht
, conn
);
2481 /* For the control socket, we try to destroy the session. */
2482 if (conn
->type
== RELAY_CONTROL
&& conn
->session
) {
2483 destroy_session(conn
->session
, conn
->sessions_ht
);
2486 connection_destroy(conn
);
2490 * This thread does the actual work
2493 void *relay_thread_worker(void *data
)
2495 int ret
, err
= -1, last_seen_data_fd
= -1;
2497 struct lttng_poll_event events
;
2498 struct lttng_ht
*relay_connections_ht
;
2499 struct lttng_ht_iter iter
;
2500 struct lttcomm_relayd_hdr recv_hdr
;
2501 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2502 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2503 struct relay_index
*index
;
2504 struct relay_connection
*destroy_conn
= NULL
;
2506 DBG("[thread] Relay worker started");
2508 rcu_register_thread();
2510 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2512 if (testpoint(relayd_thread_worker
)) {
2513 goto error_testpoint
;
2516 health_code_update();
2518 /* table of connections indexed on socket */
2519 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2520 if (!relay_connections_ht
) {
2521 goto relay_connections_ht_error
;
2524 /* Tables of received indexes indexed by index handle and net_seq_num. */
2525 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2527 goto indexes_ht_error
;
2530 ret
= create_thread_poll_set(&events
, 2);
2532 goto error_poll_create
;
2535 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2542 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2544 health_code_update();
2546 /* Infinite blocking call, waiting for transmission */
2547 DBG3("Relayd worker thread polling...");
2548 health_poll_entry();
2549 ret
= lttng_poll_wait(&events
, -1);
2553 * Restart interrupted system call.
2555 if (errno
== EINTR
) {
2564 * Process control. The control connection is prioritised so we don't
2565 * starve it with high throughout put tracing data on the data
2568 for (i
= 0; i
< nb_fd
; i
++) {
2569 /* Fetch once the poll data */
2570 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2571 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2573 health_code_update();
2576 /* No activity for this FD (poll implementation). */
2580 /* Thread quit pipe has been closed. Killing thread. */
2581 ret
= check_thread_quit_pipe(pollfd
, revents
);
2587 /* Inspect the relay conn pipe for new connection */
2588 if (pollfd
== relay_conn_pipe
[0]) {
2589 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2590 ERR("Relay connection pipe error");
2592 } else if (revents
& LPOLLIN
) {
2593 struct relay_connection
*conn
;
2595 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2599 conn
->sessions_ht
= sessions_ht
;
2600 connection_init(conn
);
2601 lttng_poll_add(&events
, conn
->sock
->fd
,
2602 LPOLLIN
| LPOLLRDHUP
);
2604 lttng_ht_add_unique_ulong(relay_connections_ht
,
2607 DBG("Connection socket %d added", conn
->sock
->fd
);
2610 struct relay_connection
*ctrl_conn
;
2613 ctrl_conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2614 /* If not found, there is a synchronization issue. */
2617 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2618 cleanup_connection_pollfd(&events
, pollfd
);
2619 destroy_connection(relay_connections_ht
, ctrl_conn
);
2620 if (last_seen_data_fd
== pollfd
) {
2621 last_seen_data_fd
= last_notdel_data_fd
;
2623 } else if (revents
& LPOLLIN
) {
2624 if (ctrl_conn
->type
== RELAY_CONTROL
) {
2625 ret
= ctrl_conn
->sock
->ops
->recvmsg(ctrl_conn
->sock
, &recv_hdr
,
2626 sizeof(recv_hdr
), 0);
2628 /* Connection closed */
2629 cleanup_connection_pollfd(&events
, pollfd
);
2630 destroy_connection(relay_connections_ht
, ctrl_conn
);
2631 DBG("Control connection closed with %d", pollfd
);
2633 ret
= relay_process_control(&recv_hdr
, ctrl_conn
);
2635 /* Clear the session on error. */
2636 cleanup_connection_pollfd(&events
, pollfd
);
2637 destroy_connection(relay_connections_ht
, ctrl_conn
);
2638 DBG("Connection closed with %d", pollfd
);
2644 * Flag the last seen data fd not deleted. It will be
2645 * used as the last seen fd if any fd gets deleted in
2648 last_notdel_data_fd
= pollfd
;
2651 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
2658 * The last loop handled a control request, go back to poll to make
2659 * sure we prioritise the control socket.
2665 if (last_seen_data_fd
>= 0) {
2666 for (i
= 0; i
< nb_fd
; i
++) {
2667 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2669 health_code_update();
2671 if (last_seen_data_fd
== pollfd
) {
2678 /* Process data connection. */
2679 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2680 /* Fetch the poll data. */
2681 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2682 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2683 struct relay_connection
*data_conn
;
2685 health_code_update();
2688 /* No activity for this FD (poll implementation). */
2692 /* Skip the command pipe. It's handled in the first loop. */
2693 if (pollfd
== relay_conn_pipe
[0]) {
2698 data_conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2700 /* Skip it. Might be removed before. */
2705 if (revents
& LPOLLIN
) {
2706 if (data_conn
->type
!= RELAY_DATA
) {
2711 ret
= relay_process_data(data_conn
);
2712 /* Connection closed */
2714 cleanup_connection_pollfd(&events
, pollfd
);
2715 destroy_connection(relay_connections_ht
, data_conn
);
2716 DBG("Data connection closed with %d", pollfd
);
2718 * Every goto restart call sets the last seen fd where
2719 * here we don't really care since we gracefully
2720 * continue the loop after the connection is deleted.
2723 /* Keep last seen port. */
2724 last_seen_data_fd
= pollfd
;
2731 last_seen_data_fd
= -1;
2734 /* Normal exit, no error */
2739 lttng_poll_clean(&events
);
2741 /* Cleanup reamaining connection object. */
2743 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
2746 health_code_update();
2747 destroy_connection(relay_connections_ht
, destroy_conn
);
2752 cds_lfht_for_each_entry(indexes_ht
->ht
, &iter
.iter
, index
,
2754 health_code_update();
2755 relay_index_delete(index
);
2756 relay_index_free_safe(index
);
2759 lttng_ht_destroy(indexes_ht
);
2761 lttng_ht_destroy(relay_connections_ht
);
2762 relay_connections_ht_error
:
2763 /* Close relay conn pipes */
2764 utils_close_pipe(relay_conn_pipe
);
2766 DBG("Thread exited with error");
2768 DBG("Worker thread cleanup complete");
2773 ERR("Health error occurred in %s", __func__
);
2775 health_unregister(health_relayd
);
2776 rcu_unregister_thread();
2777 lttng_relay_stop_threads();
2782 * Create the relay command pipe to wake thread_manage_apps.
2783 * Closed in cleanup().
2785 static int create_relay_conn_pipe(void)
2789 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2797 int main(int argc
, char **argv
)
2799 int ret
= 0, retval
= 0;
2801 struct relay_local_data
*relay_ctx
= NULL
;
2803 /* Parse arguments */
2805 if (set_options(argc
, argv
)) {
2810 if (set_signal_handler()) {
2815 /* Try to create directory if -o, --output is specified. */
2816 if (opt_output_path
) {
2817 if (*opt_output_path
!= '/') {
2818 ERR("Please specify an absolute path for -o, --output PATH");
2823 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2825 ERR("Unable to create %s", opt_output_path
);
2832 if (opt_daemon
|| opt_background
) {
2835 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2843 * We are in the child. Make sure all other file
2844 * descriptors are closed, in case we are called with
2845 * more opened file descriptors than the standard ones.
2847 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2853 /* Initialize thread health monitoring */
2854 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2855 if (!health_relayd
) {
2856 PERROR("health_app_create error");
2858 goto exit_health_app_create
;
2861 /* Create thread quit pipe */
2862 if (init_thread_quit_pipe()) {
2864 goto exit_init_data
;
2867 /* We need those values for the file/dir creation. */
2868 relayd_uid
= getuid();
2869 relayd_gid
= getgid();
2871 /* Check if daemon is UID = 0 */
2872 if (relayd_uid
== 0) {
2873 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 || live_uri
->port
< 1024) {
2874 ERR("Need to be root to use ports < 1024");
2876 goto exit_init_data
;
2880 /* Setup the thread apps communication pipe. */
2881 if (create_relay_conn_pipe()) {
2883 goto exit_init_data
;
2886 /* Init relay command queue. */
2887 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
2889 /* Set up max poll set size */
2890 if (lttng_poll_set_max_size()) {
2892 goto exit_init_data
;
2895 /* Initialize communication library */
2897 lttcomm_inet_init();
2899 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2901 PERROR("relay_ctx");
2903 goto exit_init_data
;
2906 /* tables of sessions indexed by session ID */
2907 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2908 if (!relay_ctx
->sessions_ht
) {
2910 goto exit_init_data
;
2913 /* tables of streams indexed by stream ID */
2914 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2915 if (!relay_streams_ht
) {
2917 goto exit_init_data
;
2920 /* tables of streams indexed by stream ID */
2921 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2922 if (!viewer_streams_ht
) {
2924 goto exit_init_data
;
2927 ret
= utils_create_pipe(health_quit_pipe
);
2930 goto exit_health_quit_pipe
;
2933 /* Create thread to manage the client socket */
2934 ret
= pthread_create(&health_thread
, NULL
,
2935 thread_manage_health
, (void *) NULL
);
2938 PERROR("pthread_create health");
2940 goto exit_health_thread
;
2943 /* Setup the dispatcher thread */
2944 ret
= pthread_create(&dispatcher_thread
, NULL
,
2945 relay_thread_dispatcher
, (void *) NULL
);
2948 PERROR("pthread_create dispatcher");
2950 goto exit_dispatcher_thread
;
2953 /* Setup the worker thread */
2954 ret
= pthread_create(&worker_thread
, NULL
,
2955 relay_thread_worker
, (void *) relay_ctx
);
2958 PERROR("pthread_create worker");
2960 goto exit_worker_thread
;
2963 /* Setup the listener thread */
2964 ret
= pthread_create(&listener_thread
, NULL
,
2965 relay_thread_listener
, (void *) NULL
);
2968 PERROR("pthread_create listener");
2970 goto exit_listener_thread
;
2973 ret
= relayd_live_create(live_uri
, relay_ctx
);
2975 ERR("Starting live viewer threads");
2981 * This is where we start awaiting program completion (e.g. through
2982 * signal that asks threads to teardown).
2985 ret
= relayd_live_join();
2991 ret
= pthread_join(listener_thread
, &status
);
2994 PERROR("pthread_join listener_thread");
2998 exit_listener_thread
:
2999 ret
= pthread_join(worker_thread
, &status
);
3002 PERROR("pthread_join worker_thread");
3007 ret
= pthread_join(dispatcher_thread
, &status
);
3010 PERROR("pthread_join dispatcher_thread");
3013 exit_dispatcher_thread
:
3015 ret
= pthread_join(health_thread
, &status
);
3018 PERROR("pthread_join health_thread");
3023 utils_close_pipe(health_quit_pipe
);
3024 exit_health_quit_pipe
:
3027 health_app_destroy(health_relayd
);
3028 exit_health_app_create
:
3030 relayd_cleanup(relay_ctx
);