2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #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/compat/getenv.h>
49 #include <common/defaults.h>
50 #include <common/daemonize.h>
51 #include <common/futex.h>
52 #include <common/sessiond-comm/sessiond-comm.h>
53 #include <common/sessiond-comm/inet.h>
54 #include <common/sessiond-comm/relayd.h>
55 #include <common/uri.h>
56 #include <common/utils.h>
57 #include <common/config/session-config.h>
58 #include <urcu/rculist.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"
72 #include "tracefile-array.h"
73 #include "tcp_keep_alive.h"
75 static const char *help_msg
=
76 #ifdef LTTNG_EMBED_HELP
77 #include <lttng-relayd.8.h>
83 /* command line options */
84 char *opt_output_path
;
85 static int opt_daemon
, opt_background
;
86 static int opt_allow_clear
= 1;
89 * We need to wait for listener and live listener threads, as well as
90 * health check thread, before being ready to signal readiness.
92 #define NR_LTTNG_RELAY_READY 3
93 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
95 /* Size of receive buffer. */
96 #define RECV_DATA_BUFFER_SIZE 65536
98 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
99 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
101 static struct lttng_uri
*control_uri
;
102 static struct lttng_uri
*data_uri
;
103 static struct lttng_uri
*live_uri
;
105 const char *progname
;
107 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
108 static int tracing_group_name_override
;
110 const char * const config_section_name
= "relayd";
113 * Quit pipe for all threads. This permits a single cancellation point
114 * for all threads when receiving an event on the pipe.
116 int thread_quit_pipe
[2] = { -1, -1 };
119 * This pipe is used to inform the worker thread that a command is queued and
120 * ready to be processed.
122 static int relay_conn_pipe
[2] = { -1, -1 };
124 /* Shared between threads */
125 static int dispatch_thread_exit
;
127 static pthread_t listener_thread
;
128 static pthread_t dispatcher_thread
;
129 static pthread_t worker_thread
;
130 static pthread_t health_thread
;
133 * last_relay_stream_id_lock protects last_relay_stream_id increment
134 * atomicity on 32-bit architectures.
136 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
137 static uint64_t last_relay_stream_id
;
140 * Relay command queue.
142 * The relay_thread_listener and relay_thread_dispatcher communicate with this
145 static struct relay_conn_queue relay_conn_queue
;
147 /* buffer allocated at startup, used to store the trace data */
148 static char *data_buffer
;
149 static unsigned int data_buffer_size
;
151 /* Global relay stream hash table. */
152 struct lttng_ht
*relay_streams_ht
;
154 /* Global relay viewer stream hash table. */
155 struct lttng_ht
*viewer_streams_ht
;
157 /* Global relay sessions hash table. */
158 struct lttng_ht
*sessions_ht
;
160 /* Relayd health monitoring */
161 struct health_app
*health_relayd
;
163 static struct option long_options
[] = {
164 { "control-port", 1, 0, 'C', },
165 { "data-port", 1, 0, 'D', },
166 { "live-port", 1, 0, 'L', },
167 { "daemonize", 0, 0, 'd', },
168 { "background", 0, 0, 'b', },
169 { "group", 1, 0, 'g', },
170 { "help", 0, 0, 'h', },
171 { "output", 1, 0, 'o', },
172 { "verbose", 0, 0, 'v', },
173 { "config", 1, 0, 'f' },
174 { "version", 0, 0, 'V' },
175 { "disallow-clear", 0, 0, 'x' },
179 static const char *config_ignore_options
[] = { "help", "config", "version" };
182 * Take an option from the getopt output and set it in the right variable to be
185 * Return 0 on success else a negative value.
187 static 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 if (lttng_is_setuid_setgid()) {
200 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
201 "-C, --control-port");
203 ret
= uri_parse(arg
, &control_uri
);
205 ERR("Invalid control URI specified");
208 if (control_uri
->port
== 0) {
209 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
214 if (lttng_is_setuid_setgid()) {
215 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
218 ret
= uri_parse(arg
, &data_uri
);
220 ERR("Invalid data URI specified");
223 if (data_uri
->port
== 0) {
224 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
229 if (lttng_is_setuid_setgid()) {
230 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
233 ret
= uri_parse(arg
, &live_uri
);
235 ERR("Invalid live URI specified");
238 if (live_uri
->port
== 0) {
239 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
250 if (lttng_is_setuid_setgid()) {
251 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
254 tracing_group_name
= strdup(arg
);
255 if (tracing_group_name
== NULL
) {
260 tracing_group_name_override
= 1;
264 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
266 ERR("Cannot show --help for `lttng-relayd`");
271 fprintf(stdout
, "%s\n", VERSION
);
274 if (lttng_is_setuid_setgid()) {
275 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
278 ret
= asprintf(&opt_output_path
, "%s", arg
);
281 PERROR("asprintf opt_output_path");
287 /* Verbose level can increase using multiple -v */
289 lttng_opt_verbose
= config_parse_value(arg
);
291 /* Only 3 level of verbosity (-vvv). */
292 if (lttng_opt_verbose
< 3) {
293 lttng_opt_verbose
+= 1;
302 /* Unknown option or other error.
303 * Error is printed by getopt, just return */
316 * config_entry_handler_cb used to handle options read from a config file.
317 * See config_entry_handler_cb comment in common/config/session-config.h for the
318 * return value conventions.
320 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
324 if (!entry
|| !entry
->name
|| !entry
->value
) {
329 /* Check if the option is to be ignored */
330 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
331 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
336 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
337 /* Ignore if entry name is not fully matched. */
338 if (strcmp(entry
->name
, long_options
[i
].name
)) {
343 * If the option takes no argument on the command line,
344 * we have to check if the value is "true". We support
345 * non-zero numeric values, true, on and yes.
347 if (!long_options
[i
].has_arg
) {
348 ret
= config_parse_value(entry
->value
);
351 WARN("Invalid configuration value \"%s\" for option %s",
352 entry
->value
, entry
->name
);
354 /* False, skip boolean config option. */
359 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
363 WARN("Unrecognized option \"%s\" in daemon configuration file.",
370 static int set_options(int argc
, char **argv
)
372 int c
, ret
= 0, option_index
= 0, retval
= 0;
373 int orig_optopt
= optopt
, orig_optind
= optind
;
374 char *default_address
, *optstring
;
375 const char *config_path
= NULL
;
376 const char *value
= NULL
;
378 optstring
= utils_generate_optstring(long_options
,
379 sizeof(long_options
) / sizeof(struct option
));
385 /* Check for the --config option */
387 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
388 &option_index
)) != -1) {
392 } else if (c
!= 'f') {
396 if (lttng_is_setuid_setgid()) {
397 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
400 config_path
= utils_expand_path(optarg
);
402 ERR("Failed to resolve path: %s", optarg
);
407 ret
= config_get_section_entries(config_path
, config_section_name
,
408 config_entry_handler
, NULL
);
411 ERR("Invalid configuration option at line %i", ret
);
417 /* Reset getopt's global state */
418 optopt
= orig_optopt
;
419 optind
= orig_optind
;
421 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
426 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
433 /* assign default values */
434 if (control_uri
== NULL
) {
435 ret
= asprintf(&default_address
,
436 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
437 DEFAULT_NETWORK_CONTROL_PORT
);
439 PERROR("asprintf default data address");
444 ret
= uri_parse(default_address
, &control_uri
);
445 free(default_address
);
447 ERR("Invalid control URI specified");
452 if (data_uri
== NULL
) {
453 ret
= asprintf(&default_address
,
454 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
455 DEFAULT_NETWORK_DATA_PORT
);
457 PERROR("asprintf default data address");
462 ret
= uri_parse(default_address
, &data_uri
);
463 free(default_address
);
465 ERR("Invalid data URI specified");
470 if (live_uri
== NULL
) {
471 ret
= asprintf(&default_address
,
472 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
473 DEFAULT_NETWORK_VIEWER_PORT
);
475 PERROR("asprintf default viewer control address");
480 ret
= uri_parse(default_address
, &live_uri
);
481 free(default_address
);
483 ERR("Invalid viewer control URI specified");
489 if (opt_allow_clear
) {
490 /* Check if env variable exists. */
491 value
= lttng_secure_getenv(DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
493 ret
= config_parse_value(value
);
495 ERR("Invalid value for %s specified", DEFAULT_LTTNG_RELAYD_DISALLOW_CLEAR_ENV
);
499 opt_allow_clear
= !ret
;
508 static void print_global_objects(void)
510 rcu_register_thread();
512 print_viewer_streams();
513 print_relay_streams();
516 rcu_unregister_thread();
522 static void relayd_cleanup(void)
524 print_global_objects();
528 if (viewer_streams_ht
)
529 lttng_ht_destroy(viewer_streams_ht
);
530 if (relay_streams_ht
)
531 lttng_ht_destroy(relay_streams_ht
);
533 lttng_ht_destroy(sessions_ht
);
535 /* free the dynamically allocated opt_output_path */
536 free(opt_output_path
);
538 /* Close thread quit pipes */
539 utils_close_pipe(thread_quit_pipe
);
541 uri_free(control_uri
);
543 /* Live URI is freed in the live thread. */
545 if (tracing_group_name_override
) {
546 free((void *) tracing_group_name
);
551 * Write to writable pipe used to notify a thread.
553 static int notify_thread_pipe(int wpipe
)
557 ret
= lttng_write(wpipe
, "!", 1);
559 PERROR("write poll pipe");
567 static int notify_health_quit_pipe(int *pipe
)
571 ret
= lttng_write(pipe
[1], "4", 1);
573 PERROR("write relay health quit");
582 * Stop all relayd and relayd-live threads.
584 int lttng_relay_stop_threads(void)
588 /* Stopping all threads */
589 DBG("Terminating all threads");
590 if (notify_thread_pipe(thread_quit_pipe
[1])) {
591 ERR("write error on thread quit pipe");
595 if (notify_health_quit_pipe(health_quit_pipe
)) {
596 ERR("write error on health quit pipe");
599 /* Dispatch thread */
600 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
601 futex_nto1_wake(&relay_conn_queue
.futex
);
603 if (relayd_live_stop()) {
604 ERR("Error stopping live threads");
611 * Signal handler for the daemon
613 * Simply stop all worker threads, leaving main() return gracefully after
614 * joining all threads and calling cleanup().
616 static void sighandler(int sig
)
620 DBG("SIGINT caught");
621 if (lttng_relay_stop_threads()) {
622 ERR("Error stopping threads");
626 DBG("SIGTERM caught");
627 if (lttng_relay_stop_threads()) {
628 ERR("Error stopping threads");
632 CMM_STORE_SHARED(recv_child_signal
, 1);
640 * Setup signal handler for :
641 * SIGINT, SIGTERM, SIGPIPE
643 static int set_signal_handler(void)
649 if ((ret
= sigemptyset(&sigset
)) < 0) {
650 PERROR("sigemptyset");
657 sa
.sa_handler
= sighandler
;
658 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
663 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
668 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
673 sa
.sa_handler
= SIG_IGN
;
674 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
679 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
684 void lttng_relay_notify_ready(void)
686 /* Notify the parent of the fork() process that we are ready. */
687 if (opt_daemon
|| opt_background
) {
688 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
689 kill(child_ppid
, SIGUSR1
);
695 * Init thread quit pipe.
697 * Return -1 on error or 0 if all pipes are created.
699 static int init_thread_quit_pipe(void)
703 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
709 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
711 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
715 if (events
== NULL
|| size
== 0) {
720 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
726 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
738 * Check if the thread quit pipe was triggered.
740 * Return 1 if it was triggered else 0;
742 static int check_thread_quit_pipe(int fd
, uint32_t events
)
744 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
752 * Create and init socket from uri.
754 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
757 struct lttcomm_sock
*sock
= NULL
;
759 sock
= lttcomm_alloc_sock_from_uri(uri
);
761 ERR("Allocating socket");
765 ret
= lttcomm_create_sock(sock
);
769 DBG("Listening on sock %d", sock
->fd
);
771 ret
= sock
->ops
->bind(sock
);
773 PERROR("Failed to bind socket");
777 ret
= sock
->ops
->listen(sock
, -1);
787 lttcomm_destroy_sock(sock
);
793 * This thread manages the listening for new connections on the network
795 static void *relay_thread_listener(void *data
)
797 int i
, ret
, pollfd
, err
= -1;
798 uint32_t revents
, nb_fd
;
799 struct lttng_poll_event events
;
800 struct lttcomm_sock
*control_sock
, *data_sock
;
802 DBG("[thread] Relay listener started");
804 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
806 health_code_update();
808 control_sock
= relay_socket_create(control_uri
);
810 goto error_sock_control
;
813 data_sock
= relay_socket_create(data_uri
);
815 goto error_sock_relay
;
819 * Pass 3 as size here for the thread quit pipe, control and
822 ret
= create_thread_poll_set(&events
, 3);
824 goto error_create_poll
;
827 /* Add the control socket */
828 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
833 /* Add the data socket */
834 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
839 lttng_relay_notify_ready();
841 if (testpoint(relayd_thread_listener
)) {
842 goto error_testpoint
;
846 health_code_update();
848 DBG("Listener accepting connections");
852 ret
= lttng_poll_wait(&events
, -1);
856 * Restart interrupted system call.
858 if (errno
== EINTR
) {
866 DBG("Relay new connection received");
867 for (i
= 0; i
< nb_fd
; i
++) {
868 health_code_update();
870 /* Fetch once the poll data */
871 revents
= LTTNG_POLL_GETEV(&events
, i
);
872 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
876 * No activity for this FD (poll
882 /* Thread quit pipe has been closed. Killing thread. */
883 ret
= check_thread_quit_pipe(pollfd
, revents
);
889 if (revents
& LPOLLIN
) {
891 * A new connection is requested, therefore a
892 * sessiond/consumerd connection is allocated in
893 * this thread, enqueued to a global queue and
894 * dequeued (and freed) in the worker thread.
897 struct relay_connection
*new_conn
;
898 struct lttcomm_sock
*newsock
;
899 enum connection_type type
;
901 if (pollfd
== data_sock
->fd
) {
903 newsock
= data_sock
->ops
->accept(data_sock
);
904 DBG("Relay data connection accepted, socket %d",
907 assert(pollfd
== control_sock
->fd
);
908 type
= RELAY_CONTROL
;
909 newsock
= control_sock
->ops
->accept(control_sock
);
910 DBG("Relay control connection accepted, socket %d",
914 PERROR("accepting sock");
918 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
921 PERROR("setsockopt inet");
922 lttcomm_destroy_sock(newsock
);
926 ret
= socket_apply_keep_alive_config(newsock
->fd
);
928 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
930 lttcomm_destroy_sock(newsock
);
934 new_conn
= connection_create(newsock
, type
);
936 lttcomm_destroy_sock(newsock
);
940 /* Enqueue request for the dispatcher thread. */
941 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
945 * Wake the dispatch queue futex.
946 * Implicit memory barrier with the
947 * exchange in cds_wfcq_enqueue.
949 futex_nto1_wake(&relay_conn_queue
.futex
);
950 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
951 ERR("socket poll error");
954 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
964 lttng_poll_clean(&events
);
966 if (data_sock
->fd
>= 0) {
967 ret
= data_sock
->ops
->close(data_sock
);
972 lttcomm_destroy_sock(data_sock
);
974 if (control_sock
->fd
>= 0) {
975 ret
= control_sock
->ops
->close(control_sock
);
980 lttcomm_destroy_sock(control_sock
);
984 ERR("Health error occurred in %s", __func__
);
986 health_unregister(health_relayd
);
987 DBG("Relay listener thread cleanup complete");
988 lttng_relay_stop_threads();
993 * This thread manages the dispatching of the requests to worker threads
995 static void *relay_thread_dispatcher(void *data
)
999 struct cds_wfcq_node
*node
;
1000 struct relay_connection
*new_conn
= NULL
;
1002 DBG("[thread] Relay dispatcher started");
1004 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
1006 if (testpoint(relayd_thread_dispatcher
)) {
1007 goto error_testpoint
;
1010 health_code_update();
1013 health_code_update();
1015 /* Atomically prepare the queue futex */
1016 futex_nto1_prepare(&relay_conn_queue
.futex
);
1018 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1023 health_code_update();
1025 /* Dequeue commands */
1026 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1027 &relay_conn_queue
.tail
);
1029 DBG("Woken up but nothing in the relay command queue");
1030 /* Continue thread execution */
1033 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1035 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1038 * Inform worker thread of the new request. This
1039 * call is blocking so we can be assured that
1040 * the data will be read at some point in time
1041 * or wait to the end of the world :)
1043 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1045 PERROR("write connection pipe");
1046 connection_put(new_conn
);
1049 } while (node
!= NULL
);
1051 /* Futex wait on queue. Blocking call on futex() */
1052 health_poll_entry();
1053 futex_nto1_wait(&relay_conn_queue
.futex
);
1057 /* Normal exit, no error */
1064 ERR("Health error occurred in %s", __func__
);
1066 health_unregister(health_relayd
);
1067 DBG("Dispatch thread dying");
1068 lttng_relay_stop_threads();
1073 * Set index data from the control port to a given index object.
1075 static int set_index_control_data(struct relay_index
*index
,
1076 struct lttcomm_relayd_index
*data
,
1077 struct relay_connection
*conn
)
1079 struct ctf_packet_index index_data
;
1082 * The index on disk is encoded in big endian, so we don't need
1083 * to convert the data received on the network. The data_offset
1084 * value is NEVER modified here and is updated by the data
1087 index_data
.packet_size
= data
->packet_size
;
1088 index_data
.content_size
= data
->content_size
;
1089 index_data
.timestamp_begin
= data
->timestamp_begin
;
1090 index_data
.timestamp_end
= data
->timestamp_end
;
1091 index_data
.events_discarded
= data
->events_discarded
;
1092 index_data
.stream_id
= data
->stream_id
;
1094 if (conn
->minor
>= 8) {
1095 index
->index_data
.stream_instance_id
= data
->stream_instance_id
;
1096 index
->index_data
.packet_seq_num
= data
->packet_seq_num
;
1099 return relay_index_set_data(index
, &index_data
);
1103 * Handle the RELAYD_CREATE_SESSION command.
1105 * On success, send back the session id or else return a negative value.
1107 static int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1108 struct relay_connection
*conn
)
1110 int ret
= 0, send_ret
;
1111 struct relay_session
*session
;
1112 struct lttcomm_relayd_status_session reply
;
1113 char session_name
[LTTNG_NAME_MAX
];
1114 char hostname
[LTTNG_HOST_NAME_MAX
];
1115 uint32_t live_timer
= 0;
1116 bool snapshot
= false;
1118 memset(session_name
, 0, LTTNG_NAME_MAX
);
1119 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1121 memset(&reply
, 0, sizeof(reply
));
1123 switch (conn
->minor
) {
1128 case 4: /* LTTng sessiond 2.4 */
1130 ret
= cmd_create_session_2_4(conn
, session_name
,
1131 hostname
, &live_timer
, &snapshot
);
1137 session
= session_create(session_name
, hostname
, live_timer
,
1138 snapshot
, conn
->major
, conn
->minor
);
1143 assert(!conn
->session
);
1144 conn
->session
= session
;
1145 DBG("Created session %" PRIu64
, session
->id
);
1147 reply
.session_id
= htobe64(session
->id
);
1151 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1153 reply
.ret_code
= htobe32(LTTNG_OK
);
1156 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1158 ERR("Relayd sending session id");
1166 * When we have received all the streams and the metadata for a channel,
1167 * we make them visible to the viewer threads.
1169 static void publish_connection_local_streams(struct relay_connection
*conn
)
1171 struct relay_stream
*stream
;
1172 struct relay_session
*session
= conn
->session
;
1175 * We publish all streams belonging to a session atomically wrt
1178 pthread_mutex_lock(&session
->lock
);
1180 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1182 stream_publish(stream
);
1187 * Inform the viewer that there are new streams in the session.
1189 if (session
->viewer_attached
) {
1190 uatomic_set(&session
->new_streams
, 1);
1192 pthread_mutex_unlock(&session
->lock
);
1196 * relay_add_stream: allocate a new stream for a session
1198 static int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1199 struct relay_connection
*conn
)
1203 struct relay_session
*session
= conn
->session
;
1204 struct relay_stream
*stream
= NULL
;
1205 struct lttcomm_relayd_status_stream reply
;
1206 struct ctf_trace
*trace
= NULL
;
1207 uint64_t stream_handle
= -1ULL;
1208 char *path_name
= NULL
, *channel_name
= NULL
;
1209 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1211 if (!session
|| conn
->version_check_done
== 0) {
1212 ERR("Trying to add a stream before version check");
1214 goto end_no_session
;
1217 switch (session
->minor
) {
1218 case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */
1219 ret
= cmd_recv_stream_2_1(conn
, &path_name
,
1222 case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
1224 ret
= cmd_recv_stream_2_2(conn
, &path_name
,
1225 &channel_name
, &tracefile_size
, &tracefile_count
);
1232 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1236 /* This stream here has one reference on the trace. */
1238 pthread_mutex_lock(&last_relay_stream_id_lock
);
1239 stream_handle
= ++last_relay_stream_id
;
1240 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1242 /* We pass ownership of path_name and channel_name. */
1243 stream
= stream_create(trace
, stream_handle
, path_name
,
1244 channel_name
, tracefile_size
, tracefile_count
);
1246 channel_name
= NULL
;
1249 * Streams are the owners of their trace. Reference to trace is
1250 * kept within stream_create().
1252 ctf_trace_put(trace
);
1255 memset(&reply
, 0, sizeof(reply
));
1256 reply
.handle
= htobe64(stream_handle
);
1258 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1260 reply
.ret_code
= htobe32(LTTNG_OK
);
1263 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1264 sizeof(struct lttcomm_relayd_status_stream
), 0);
1266 ERR("Relay sending stream id");
1267 ret
= (int) send_ret
;
1277 * relay_close_stream: close a specific stream
1279 static int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1280 struct relay_connection
*conn
)
1283 struct relay_session
*session
= conn
->session
;
1284 struct lttcomm_relayd_close_stream stream_info
;
1285 struct lttcomm_relayd_generic_reply reply
;
1286 struct relay_stream
*stream
;
1288 DBG("Close stream received");
1290 if (!session
|| conn
->version_check_done
== 0) {
1291 ERR("Trying to close a stream before version check");
1293 goto end_no_session
;
1296 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1297 sizeof(struct lttcomm_relayd_close_stream
), 0);
1298 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1300 /* Orderly shutdown. Not necessary to print an error. */
1301 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1303 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1306 goto end_no_session
;
1309 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1316 * Set last_net_seq_num before the close flag. Required by data
1319 pthread_mutex_lock(&stream
->lock
);
1320 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1321 pthread_mutex_unlock(&stream
->lock
);
1324 * This is one of the conditions which may trigger a stream close
1325 * with the others being:
1326 * 1) A close command is received for a stream
1327 * 2) The control connection owning the stream is closed
1328 * 3) We have received all of the stream's data _after_ a close
1331 try_stream_close(stream
);
1332 if (stream
->is_metadata
) {
1333 struct relay_viewer_stream
*vstream
;
1335 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1337 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1339 * Since all the metadata has been sent to the
1340 * viewer and that we have a request to close
1341 * its stream, we can safely teardown the
1342 * corresponding metadata viewer stream.
1344 viewer_stream_put(vstream
);
1346 /* Put local reference. */
1347 viewer_stream_put(vstream
);
1353 memset(&reply
, 0, sizeof(reply
));
1355 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1357 reply
.ret_code
= htobe32(LTTNG_OK
);
1359 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1360 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1362 ERR("Relay sending stream id");
1371 * relay_reset_metadata: reset a metadata stream
1374 int relay_reset_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1375 struct relay_connection
*conn
)
1378 struct relay_session
*session
= conn
->session
;
1379 struct lttcomm_relayd_reset_metadata stream_info
;
1380 struct lttcomm_relayd_generic_reply reply
;
1381 struct relay_stream
*stream
;
1383 DBG("Reset metadata received");
1385 if (!session
|| conn
->version_check_done
== 0) {
1386 ERR("Trying to reset a metadata stream before version check");
1388 goto end_no_session
;
1391 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1392 sizeof(struct lttcomm_relayd_reset_metadata
), 0);
1393 if (ret
< sizeof(struct lttcomm_relayd_reset_metadata
)) {
1395 /* Orderly shutdown. Not necessary to print an error. */
1396 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1398 ERR("Relay didn't receive valid reset_metadata struct "
1402 goto end_no_session
;
1404 DBG("Update metadata to version %" PRIu64
, be64toh(stream_info
.version
));
1406 /* Unsupported for live sessions for now. */
1407 if (session
->live_timer
!= 0) {
1412 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1417 pthread_mutex_lock(&stream
->lock
);
1418 if (!stream
->is_metadata
) {
1423 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1424 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1425 &stream
->stream_fd
->fd
);
1427 ERR("Failed to rotate metadata file %s of channel %s",
1428 stream
->path_name
, stream
->channel_name
);
1433 pthread_mutex_unlock(&stream
->lock
);
1437 memset(&reply
, 0, sizeof(reply
));
1439 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1441 reply
.ret_code
= htobe32(LTTNG_OK
);
1443 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1444 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1446 ERR("Relay sending reset metadata reply");
1455 * relay_unknown_command: send -1 if received unknown command
1457 static void relay_unknown_command(struct relay_connection
*conn
)
1459 struct lttcomm_relayd_generic_reply reply
;
1462 memset(&reply
, 0, sizeof(reply
));
1463 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1464 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1465 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1467 ERR("Relay sending unknown command");
1472 * relay_start: send an acknowledgment to the client to tell if we are
1473 * ready to receive data. We are ready if a session is established.
1475 static int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1476 struct relay_connection
*conn
)
1478 int ret
= htobe32(LTTNG_OK
);
1479 struct lttcomm_relayd_generic_reply reply
;
1480 struct relay_session
*session
= conn
->session
;
1483 DBG("Trying to start the streaming without a session established");
1484 ret
= htobe32(LTTNG_ERR_UNK
);
1487 memset(&reply
, 0, sizeof(reply
));
1488 reply
.ret_code
= ret
;
1489 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1490 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1492 ERR("Relay sending start ack");
1499 * Append padding to the file pointed by the file descriptor fd.
1501 static int write_padding_to_file(int fd
, uint32_t size
)
1510 zeros
= zmalloc(size
);
1511 if (zeros
== NULL
) {
1512 PERROR("zmalloc zeros for padding");
1517 ret
= lttng_write(fd
, zeros
, size
);
1519 PERROR("write padding to file");
1529 * relay_recv_metadata: receive the metadata for the session.
1531 static int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1532 struct relay_connection
*conn
)
1536 struct relay_session
*session
= conn
->session
;
1537 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1538 struct relay_stream
*metadata_stream
;
1539 uint64_t data_size
, payload_size
;
1542 ERR("Metadata sent before version check");
1547 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1548 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1549 ERR("Incorrect data size");
1553 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1555 if (data_buffer_size
< data_size
) {
1556 /* In case the realloc fails, we can free the memory */
1559 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1560 if (!tmp_data_ptr
) {
1561 ERR("Allocating data buffer");
1566 data_buffer
= tmp_data_ptr
;
1567 data_buffer_size
= data_size
;
1569 memset(data_buffer
, 0, data_size
);
1570 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1571 size_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1572 if (size_ret
< 0 || size_ret
!= data_size
) {
1573 if (size_ret
== 0) {
1574 /* Orderly shutdown. Not necessary to print an error. */
1575 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1577 ERR("Relay didn't receive the whole metadata");
1582 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1584 metadata_stream
= stream_get_by_id(be64toh(metadata_struct
->stream_id
));
1585 if (!metadata_stream
) {
1590 pthread_mutex_lock(&metadata_stream
->lock
);
1592 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
, metadata_struct
->payload
,
1594 if (size_ret
< payload_size
) {
1595 ERR("Relay error writing metadata on file");
1600 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1601 be32toh(metadata_struct
->padding_size
));
1606 metadata_stream
->metadata_received
+=
1607 payload_size
+ be32toh(metadata_struct
->padding_size
);
1608 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1609 metadata_stream
->metadata_received
);
1612 pthread_mutex_unlock(&metadata_stream
->lock
);
1613 stream_put(metadata_stream
);
1619 * relay_send_version: send relayd version number
1621 static int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1622 struct relay_connection
*conn
)
1625 struct lttcomm_relayd_version reply
, msg
;
1626 bool compatible
= true;
1628 conn
->version_check_done
= 1;
1630 /* Get version from the other side. */
1631 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1632 if (ret
< 0 || ret
!= sizeof(msg
)) {
1634 /* Orderly shutdown. Not necessary to print an error. */
1635 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1637 ERR("Relay failed to receive the version values.");
1643 memset(&reply
, 0, sizeof(reply
));
1644 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1645 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1647 /* Major versions must be the same */
1648 if (reply
.major
!= be32toh(msg
.major
)) {
1649 DBG("Incompatible major versions (%u vs %u), deleting session",
1650 reply
.major
, be32toh(msg
.major
));
1654 conn
->major
= reply
.major
;
1655 /* We adapt to the lowest compatible version */
1656 if (reply
.minor
<= be32toh(msg
.minor
)) {
1657 conn
->minor
= reply
.minor
;
1659 conn
->minor
= be32toh(msg
.minor
);
1662 reply
.major
= htobe32(reply
.major
);
1663 reply
.minor
= htobe32(reply
.minor
);
1664 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1665 sizeof(struct lttcomm_relayd_version
), 0);
1667 ERR("Relay sending version");
1675 DBG("Version check done using protocol %u.%u", conn
->major
,
1683 * Check for data pending for a given stream id from the session daemon.
1685 static int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1686 struct relay_connection
*conn
)
1688 struct relay_session
*session
= conn
->session
;
1689 struct lttcomm_relayd_data_pending msg
;
1690 struct lttcomm_relayd_generic_reply reply
;
1691 struct relay_stream
*stream
;
1693 uint64_t last_net_seq_num
, stream_id
;
1695 DBG("Data pending command received");
1697 if (!session
|| conn
->version_check_done
== 0) {
1698 ERR("Trying to check for data before version check");
1700 goto end_no_session
;
1703 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1704 if (ret
< sizeof(msg
)) {
1706 /* Orderly shutdown. Not necessary to print an error. */
1707 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1709 ERR("Relay didn't receive valid data_pending struct size : %d",
1713 goto end_no_session
;
1716 stream_id
= be64toh(msg
.stream_id
);
1717 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1719 stream
= stream_get_by_id(stream_id
);
1720 if (stream
== NULL
) {
1725 pthread_mutex_lock(&stream
->lock
);
1727 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1728 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1731 /* Avoid wrapping issue */
1732 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1733 /* Data has in fact been written and is NOT pending */
1736 /* Data still being streamed thus pending */
1740 stream
->data_pending_check_done
= true;
1741 pthread_mutex_unlock(&stream
->lock
);
1746 memset(&reply
, 0, sizeof(reply
));
1747 reply
.ret_code
= htobe32(ret
);
1748 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1750 ERR("Relay data pending ret code failed");
1758 * Wait for the control socket to reach a quiescent state.
1760 * Note that for now, when receiving this command from the session
1761 * daemon, this means that every subsequent commands or data received on
1762 * the control socket has been handled. So, this is why we simply return
1765 static int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1766 struct relay_connection
*conn
)
1770 struct relay_stream
*stream
;
1771 struct lttcomm_relayd_quiescent_control msg
;
1772 struct lttcomm_relayd_generic_reply reply
;
1774 DBG("Checking quiescent state on control socket");
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 stream_id
= be64toh(msg
.stream_id
);
1796 stream
= stream_get_by_id(stream_id
);
1800 pthread_mutex_lock(&stream
->lock
);
1801 stream
->data_pending_check_done
= true;
1802 pthread_mutex_unlock(&stream
->lock
);
1803 DBG("Relay quiescent control pending flag set to %" PRIu64
, stream_id
);
1806 memset(&reply
, 0, sizeof(reply
));
1807 reply
.ret_code
= htobe32(LTTNG_OK
);
1808 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1810 ERR("Relay data quiescent control ret code failed");
1818 * Initialize a data pending command. This means that a consumer is about
1819 * to ask for data pending for each stream it holds. Simply iterate over
1820 * all streams of a session and set the data_pending_check_done flag.
1822 * This command returns to the client a LTTNG_OK code.
1824 static int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1825 struct relay_connection
*conn
)
1828 struct lttng_ht_iter iter
;
1829 struct lttcomm_relayd_begin_data_pending msg
;
1830 struct lttcomm_relayd_generic_reply reply
;
1831 struct relay_stream
*stream
;
1832 uint64_t session_id
;
1837 DBG("Init streams for data pending");
1839 if (!conn
->session
|| conn
->version_check_done
== 0) {
1840 ERR("Trying to check for data before version check");
1842 goto end_no_session
;
1845 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1846 if (ret
< sizeof(msg
)) {
1848 /* Orderly shutdown. Not necessary to print an error. */
1849 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1851 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1855 goto end_no_session
;
1858 session_id
= be64toh(msg
.session_id
);
1861 * Iterate over all streams to set the begin data pending flag.
1862 * For now, the streams are indexed by stream handle so we have
1863 * to iterate over all streams to find the one associated with
1864 * the right session_id.
1867 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1869 if (!stream_get(stream
)) {
1872 if (stream
->trace
->session
->id
== session_id
) {
1873 pthread_mutex_lock(&stream
->lock
);
1874 stream
->data_pending_check_done
= false;
1875 pthread_mutex_unlock(&stream
->lock
);
1876 DBG("Set begin data pending flag to stream %" PRIu64
,
1877 stream
->stream_handle
);
1883 memset(&reply
, 0, sizeof(reply
));
1884 /* All good, send back reply. */
1885 reply
.ret_code
= htobe32(LTTNG_OK
);
1887 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1889 ERR("Relay begin data pending send reply failed");
1897 * End data pending command. This will check, for a given session id, if
1898 * each stream associated with it has its data_pending_check_done flag
1899 * set. If not, this means that the client lost track of the stream but
1900 * the data is still being streamed on our side. In this case, we inform
1901 * the client that data is in flight.
1903 * Return to the client if there is data in flight or not with a ret_code.
1905 static int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1906 struct relay_connection
*conn
)
1909 struct lttng_ht_iter iter
;
1910 struct lttcomm_relayd_end_data_pending msg
;
1911 struct lttcomm_relayd_generic_reply reply
;
1912 struct relay_stream
*stream
;
1913 uint64_t session_id
;
1914 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
);
1940 * Iterate over all streams to see if the begin data pending
1944 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1946 if (!stream_get(stream
)) {
1949 if (stream
->trace
->session
->id
!= session_id
) {
1953 pthread_mutex_lock(&stream
->lock
);
1954 if (!stream
->data_pending_check_done
) {
1955 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
1956 is_data_inflight
= 1;
1957 DBG("Data is still in flight for stream %" PRIu64
,
1958 stream
->stream_handle
);
1959 pthread_mutex_unlock(&stream
->lock
);
1964 pthread_mutex_unlock(&stream
->lock
);
1969 memset(&reply
, 0, sizeof(reply
));
1970 /* All good, send back reply. */
1971 reply
.ret_code
= htobe32(is_data_inflight
);
1973 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1975 ERR("Relay end data pending send reply failed");
1983 * Receive an index for a specific stream.
1985 * Return 0 on success else a negative value.
1987 static int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1988 struct relay_connection
*conn
)
1991 struct relay_session
*session
= conn
->session
;
1992 struct lttcomm_relayd_index index_info
;
1993 struct relay_index
*index
;
1994 struct lttcomm_relayd_generic_reply reply
;
1995 struct relay_stream
*stream
;
1996 uint64_t net_seq_num
;
2001 DBG("Relay receiving index");
2003 if (!session
|| conn
->version_check_done
== 0) {
2004 ERR("Trying to close a stream before version check");
2006 goto end_no_session
;
2009 msg_len
= lttcomm_relayd_index_len(
2010 lttng_to_index_major(conn
->major
, conn
->minor
),
2011 lttng_to_index_minor(conn
->major
, conn
->minor
));
2012 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
2014 if (ret
< msg_len
) {
2016 /* Orderly shutdown. Not necessary to print an error. */
2017 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2019 ERR("Relay didn't receive valid index struct size : %d", ret
);
2022 goto end_no_session
;
2025 net_seq_num
= be64toh(index_info
.net_seq_num
);
2027 stream
= stream_get_by_id(be64toh(index_info
.relay_stream_id
));
2029 ERR("stream_get_by_id not found");
2033 pthread_mutex_lock(&stream
->lock
);
2035 /* Live beacon handling */
2036 if (index_info
.packet_size
== 0) {
2037 DBG("Received live beacon for stream %" PRIu64
,
2038 stream
->stream_handle
);
2041 * Only flag a stream inactive when it has already
2042 * received data and no indexes are in flight.
2044 if (stream
->index_received_seqcount
> 0
2045 && stream
->indexes_in_flight
== 0) {
2046 stream
->beacon_ts_end
=
2047 be64toh(index_info
.timestamp_end
);
2050 goto end_stream_put
;
2052 stream
->beacon_ts_end
= -1ULL;
2055 if (stream
->ctf_stream_id
== -1ULL) {
2056 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
2058 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
2061 ERR("relay_index_get_by_id_or_create index NULL");
2062 goto end_stream_put
;
2064 if (set_index_control_data(index
, &index_info
, conn
)) {
2065 ERR("set_index_control_data error");
2066 relay_index_put(index
);
2068 goto end_stream_put
;
2070 ret
= relay_index_try_flush(index
);
2072 tracefile_array_commit_seq(stream
->tfa
);
2073 stream
->index_received_seqcount
++;
2074 } else if (ret
> 0) {
2081 * relay_index_try_flush is responsible for the self-reference
2082 * put of the index object on error.
2084 ERR("relay_index_try_flush error %d", ret
);
2089 pthread_mutex_unlock(&stream
->lock
);
2094 memset(&reply
, 0, sizeof(reply
));
2096 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2098 reply
.ret_code
= htobe32(LTTNG_OK
);
2100 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2102 ERR("Relay sending close index id reply");
2111 * Receive the streams_sent message.
2113 * Return 0 on success else a negative value.
2115 static int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2116 struct relay_connection
*conn
)
2119 struct lttcomm_relayd_generic_reply reply
;
2123 DBG("Relay receiving streams_sent");
2125 if (!conn
->session
|| conn
->version_check_done
== 0) {
2126 ERR("Trying to close a stream before version check");
2128 goto end_no_session
;
2132 * Publish every pending stream in the connection recv list which are
2133 * now ready to be used by the viewer.
2135 publish_connection_local_streams(conn
);
2137 memset(&reply
, 0, sizeof(reply
));
2138 reply
.ret_code
= htobe32(LTTNG_OK
);
2139 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2141 ERR("Relay sending sent_stream reply");
2153 * Process the commands received on the control socket
2155 static int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2156 struct relay_connection
*conn
)
2160 switch (be32toh(recv_hdr
->cmd
)) {
2161 case RELAYD_CREATE_SESSION
:
2162 ret
= relay_create_session(recv_hdr
, conn
);
2164 case RELAYD_ADD_STREAM
:
2165 ret
= relay_add_stream(recv_hdr
, conn
);
2167 case RELAYD_START_DATA
:
2168 ret
= relay_start(recv_hdr
, conn
);
2170 case RELAYD_SEND_METADATA
:
2171 ret
= relay_recv_metadata(recv_hdr
, conn
);
2173 case RELAYD_VERSION
:
2174 ret
= relay_send_version(recv_hdr
, conn
);
2176 case RELAYD_CLOSE_STREAM
:
2177 ret
= relay_close_stream(recv_hdr
, conn
);
2179 case RELAYD_DATA_PENDING
:
2180 ret
= relay_data_pending(recv_hdr
, conn
);
2182 case RELAYD_QUIESCENT_CONTROL
:
2183 ret
= relay_quiescent_control(recv_hdr
, conn
);
2185 case RELAYD_BEGIN_DATA_PENDING
:
2186 ret
= relay_begin_data_pending(recv_hdr
, conn
);
2188 case RELAYD_END_DATA_PENDING
:
2189 ret
= relay_end_data_pending(recv_hdr
, conn
);
2191 case RELAYD_SEND_INDEX
:
2192 ret
= relay_recv_index(recv_hdr
, conn
);
2194 case RELAYD_STREAMS_SENT
:
2195 ret
= relay_streams_sent(recv_hdr
, conn
);
2197 case RELAYD_RESET_METADATA
:
2198 ret
= relay_reset_metadata(recv_hdr
, conn
);
2200 case RELAYD_UPDATE_SYNC_INFO
:
2202 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2203 relay_unknown_command(conn
);
2213 * Handle index for a data stream.
2215 * Called with the stream lock held.
2217 * Return 0 on success else a negative value.
2219 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2223 uint64_t data_offset
;
2224 struct relay_index
*index
;
2226 /* Get data offset because we are about to update the index. */
2227 data_offset
= htobe64(stream
->tracefile_size_current
);
2229 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
2230 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
2233 * Lookup for an existing index for that stream id/sequence
2234 * number. If it exists, the control thread has already received the
2235 * data for it, thus we need to write it to disk.
2237 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
2243 if (rotate_index
|| !stream
->index_file
) {
2244 uint32_t major
, minor
;
2246 /* Put ref on previous index_file. */
2247 if (stream
->index_file
) {
2248 lttng_index_file_put(stream
->index_file
);
2249 stream
->index_file
= NULL
;
2251 major
= stream
->trace
->session
->major
;
2252 minor
= stream
->trace
->session
->minor
;
2253 stream
->index_file
= lttng_index_file_create(stream
->path_name
,
2254 stream
->channel_name
,
2255 -1, -1, stream
->tracefile_size
,
2256 tracefile_array_get_file_index_head(stream
->tfa
),
2257 lttng_to_index_major(major
, minor
),
2258 lttng_to_index_minor(major
, minor
));
2259 if (!stream
->index_file
) {
2261 /* Put self-ref for this index due to error. */
2262 relay_index_put(index
);
2268 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
2270 /* Put self-ref for this index due to error. */
2271 relay_index_put(index
);
2276 ret
= relay_index_try_flush(index
);
2278 tracefile_array_commit_seq(stream
->tfa
);
2279 stream
->index_received_seqcount
++;
2280 } else if (ret
> 0) {
2287 * relay_index_try_flush is responsible for the self-reference
2288 * put of the index object on error.
2290 ERR("relay_index_try_flush error %d", ret
);
2298 * relay_process_data: Process the data received on the data socket
2300 static int relay_process_data(struct relay_connection
*conn
)
2302 int ret
= 0, rotate_index
= 0;
2304 struct relay_stream
*stream
;
2305 struct lttcomm_relayd_data_hdr data_hdr
;
2307 uint64_t net_seq_num
;
2309 struct relay_session
*session
;
2310 bool new_stream
= false, close_requested
= false;
2311 size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
2312 size_t recv_off
= 0;
2313 char data_buffer
[chunk_size
];
2315 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2316 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2319 /* Orderly shutdown. Not necessary to print an error. */
2320 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2322 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2328 stream_id
= be64toh(data_hdr
.stream_id
);
2329 stream
= stream_get_by_id(stream_id
);
2331 ERR("relay_process_data: Cannot find stream %" PRIu64
, stream_id
);
2335 session
= stream
->trace
->session
;
2336 data_size
= be32toh(data_hdr
.data_size
);
2338 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2340 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2341 data_size
, stream_id
, net_seq_num
);
2343 pthread_mutex_lock(&stream
->lock
);
2345 /* Check if a rotation is needed. */
2346 if (stream
->tracefile_size
> 0 &&
2347 (stream
->tracefile_size_current
+ data_size
) >
2348 stream
->tracefile_size
) {
2349 uint64_t old_id
, new_id
;
2351 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
2352 tracefile_array_file_rotate(stream
->tfa
);
2354 /* new_id is updated by utils_rotate_stream_file. */
2357 ret
= utils_rotate_stream_file(stream
->path_name
,
2358 stream
->channel_name
, stream
->tracefile_size
,
2359 stream
->tracefile_count
, -1,
2360 -1, stream
->stream_fd
->fd
,
2361 &new_id
, &stream
->stream_fd
->fd
);
2363 ERR("Rotating stream output file");
2364 goto end_stream_unlock
;
2367 * Reset current size because we just performed a stream
2370 stream
->tracefile_size_current
= 0;
2375 * Index are handled in protocol version 2.4 and above. Also,
2376 * snapshot and index are NOT supported.
2378 if (session
->minor
>= 4 && !session
->snapshot
) {
2379 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2381 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
2382 stream
->stream_handle
, net_seq_num
, ret
);
2383 goto end_stream_unlock
;
2387 for (recv_off
= 0; recv_off
< data_size
; recv_off
+= chunk_size
) {
2388 size_t recv_size
= min(data_size
- recv_off
, chunk_size
);
2390 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, recv_size
, 0);
2393 /* Orderly shutdown. Not necessary to print an error. */
2394 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2396 ERR("Socket %d error %d", conn
->sock
->fd
, ret
);
2399 goto end_stream_unlock
;
2402 /* Write data to stream output fd. */
2403 size_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
2405 if (size_ret
< recv_size
) {
2406 ERR("Relay error writing data to file");
2408 goto end_stream_unlock
;
2411 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
2412 size_ret
, stream
->stream_handle
);
2415 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
2416 be32toh(data_hdr
.padding_size
));
2418 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
2419 stream
->stream_handle
, net_seq_num
, ret
);
2420 goto end_stream_unlock
;
2422 stream
->tracefile_size_current
+=
2423 data_size
+ be32toh(data_hdr
.padding_size
);
2424 if (stream
->prev_seq
== -1ULL) {
2428 stream
->prev_seq
= net_seq_num
;
2431 close_requested
= stream
->close_requested
;
2432 pthread_mutex_unlock(&stream
->lock
);
2433 if (close_requested
) {
2434 try_stream_close(stream
);
2438 pthread_mutex_lock(&session
->lock
);
2439 uatomic_set(&session
->new_streams
, 1);
2440 pthread_mutex_unlock(&session
->lock
);
2447 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2451 (void) lttng_poll_del(events
, pollfd
);
2453 ret
= close(pollfd
);
2455 ERR("Closing pollfd %d", pollfd
);
2459 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
2460 int pollfd
, struct relay_connection
*conn
)
2462 const char *type_str
;
2464 switch (conn
->type
) {
2469 type_str
= "Control";
2471 case RELAY_VIEWER_COMMAND
:
2472 type_str
= "Viewer Command";
2474 case RELAY_VIEWER_NOTIFICATION
:
2475 type_str
= "Viewer Notification";
2478 type_str
= "Unknown";
2480 cleanup_connection_pollfd(events
, pollfd
);
2481 connection_put(conn
);
2482 DBG("%s connection closed with %d", type_str
, pollfd
);
2486 * This thread does the actual work
2488 static void *relay_thread_worker(void *data
)
2490 int ret
, err
= -1, last_seen_data_fd
= -1;
2492 struct lttng_poll_event events
;
2493 struct lttng_ht
*relay_connections_ht
;
2494 struct lttng_ht_iter iter
;
2495 struct lttcomm_relayd_hdr recv_hdr
;
2496 struct relay_connection
*destroy_conn
= NULL
;
2498 DBG("[thread] Relay worker started");
2500 rcu_register_thread();
2502 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2504 if (testpoint(relayd_thread_worker
)) {
2505 goto error_testpoint
;
2508 health_code_update();
2510 /* table of connections indexed on socket */
2511 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2512 if (!relay_connections_ht
) {
2513 goto relay_connections_ht_error
;
2516 ret
= create_thread_poll_set(&events
, 2);
2518 goto error_poll_create
;
2521 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2528 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2530 health_code_update();
2532 /* Infinite blocking call, waiting for transmission */
2533 DBG3("Relayd worker thread polling...");
2534 health_poll_entry();
2535 ret
= lttng_poll_wait(&events
, -1);
2539 * Restart interrupted system call.
2541 if (errno
== EINTR
) {
2550 * Process control. The control connection is
2551 * prioritized so we don't starve it with high
2552 * throughput tracing data on the data connection.
2554 for (i
= 0; i
< nb_fd
; i
++) {
2555 /* Fetch once the poll data */
2556 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2557 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2559 health_code_update();
2563 * No activity for this FD (poll
2569 /* Thread quit pipe has been closed. Killing thread. */
2570 ret
= check_thread_quit_pipe(pollfd
, revents
);
2576 /* Inspect the relay conn pipe for new connection */
2577 if (pollfd
== relay_conn_pipe
[0]) {
2578 if (revents
& LPOLLIN
) {
2579 struct relay_connection
*conn
;
2581 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2585 lttng_poll_add(&events
, conn
->sock
->fd
,
2586 LPOLLIN
| LPOLLRDHUP
);
2587 connection_ht_add(relay_connections_ht
, conn
);
2588 DBG("Connection socket %d added", conn
->sock
->fd
);
2589 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2590 ERR("Relay connection pipe error");
2593 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2597 struct relay_connection
*ctrl_conn
;
2599 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
2600 /* If not found, there is a synchronization issue. */
2603 if (ctrl_conn
->type
== RELAY_DATA
) {
2604 if (revents
& LPOLLIN
) {
2606 * Flag the last seen data fd not deleted. It will be
2607 * used as the last seen fd if any fd gets deleted in
2610 last_notdel_data_fd
= pollfd
;
2612 goto put_ctrl_connection
;
2614 assert(ctrl_conn
->type
== RELAY_CONTROL
);
2616 if (revents
& LPOLLIN
) {
2617 ret
= ctrl_conn
->sock
->ops
->recvmsg(ctrl_conn
->sock
,
2618 &recv_hdr
, sizeof(recv_hdr
), 0);
2620 /* Connection closed */
2621 relay_thread_close_connection(&events
, pollfd
,
2624 ret
= relay_process_control(&recv_hdr
, ctrl_conn
);
2626 /* Clear the session on error. */
2627 relay_thread_close_connection(&events
,
2632 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2633 relay_thread_close_connection(&events
,
2635 if (last_seen_data_fd
== pollfd
) {
2636 last_seen_data_fd
= last_notdel_data_fd
;
2639 ERR("Unexpected poll events %u for control sock %d",
2641 connection_put(ctrl_conn
);
2644 put_ctrl_connection
:
2645 connection_put(ctrl_conn
);
2650 * The last loop handled a control request, go back to poll to make
2651 * sure we prioritise the control socket.
2657 if (last_seen_data_fd
>= 0) {
2658 for (i
= 0; i
< nb_fd
; i
++) {
2659 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2661 health_code_update();
2663 if (last_seen_data_fd
== pollfd
) {
2670 /* Process data connection. */
2671 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2672 /* Fetch the poll data. */
2673 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2674 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2675 struct relay_connection
*data_conn
;
2677 health_code_update();
2680 /* No activity for this FD (poll implementation). */
2684 /* Skip the command pipe. It's handled in the first loop. */
2685 if (pollfd
== relay_conn_pipe
[0]) {
2689 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
2691 /* Skip it. Might be removed before. */
2694 if (data_conn
->type
== RELAY_CONTROL
) {
2695 goto put_data_connection
;
2697 assert(data_conn
->type
== RELAY_DATA
);
2699 if (revents
& LPOLLIN
) {
2700 ret
= relay_process_data(data_conn
);
2701 /* Connection closed */
2703 relay_thread_close_connection(&events
, pollfd
,
2706 * Every goto restart call sets the last seen fd where
2707 * here we don't really care since we gracefully
2708 * continue the loop after the connection is deleted.
2711 /* Keep last seen port. */
2712 last_seen_data_fd
= pollfd
;
2713 connection_put(data_conn
);
2716 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2717 relay_thread_close_connection(&events
, pollfd
,
2720 ERR("Unknown poll events %u for data sock %d",
2723 put_data_connection
:
2724 connection_put(data_conn
);
2726 last_seen_data_fd
= -1;
2729 /* Normal exit, no error */
2734 /* Cleanup reamaining connection object. */
2736 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
2739 health_code_update();
2741 if (session_abort(destroy_conn
->session
)) {
2746 * No need to grab another ref, because we own
2749 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
2754 lttng_poll_clean(&events
);
2756 lttng_ht_destroy(relay_connections_ht
);
2757 relay_connections_ht_error
:
2758 /* Close relay conn pipes */
2759 utils_close_pipe(relay_conn_pipe
);
2761 DBG("Thread exited with error");
2763 DBG("Worker thread cleanup complete");
2767 ERR("Health error occurred in %s", __func__
);
2769 health_unregister(health_relayd
);
2770 rcu_unregister_thread();
2771 lttng_relay_stop_threads();
2776 * Create the relay command pipe to wake thread_manage_apps.
2777 * Closed in cleanup().
2779 static int create_relay_conn_pipe(void)
2783 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2791 int main(int argc
, char **argv
)
2793 int ret
= 0, retval
= 0;
2796 /* Parse arguments */
2798 if (set_options(argc
, argv
)) {
2803 if (set_signal_handler()) {
2808 if (!opt_allow_clear
) {
2809 DBG("Clear command disallowed.");
2812 /* Try to create directory if -o, --output is specified. */
2813 if (opt_output_path
) {
2814 if (*opt_output_path
!= '/') {
2815 ERR("Please specify an absolute path for -o, --output PATH");
2820 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
2823 ERR("Unable to create %s", opt_output_path
);
2830 if (opt_daemon
|| opt_background
) {
2833 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2841 * We are in the child. Make sure all other file
2842 * descriptors are closed, in case we are called with
2843 * more opened file descriptors than the standard ones.
2845 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2850 /* Initialize thread health monitoring */
2851 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2852 if (!health_relayd
) {
2853 PERROR("health_app_create error");
2855 goto exit_health_app_create
;
2858 /* Create thread quit pipe */
2859 if (init_thread_quit_pipe()) {
2861 goto exit_init_data
;
2864 /* Setup the thread apps communication pipe. */
2865 if (create_relay_conn_pipe()) {
2867 goto exit_init_data
;
2870 /* Init relay command queue. */
2871 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
2873 /* Initialize communication library */
2875 lttcomm_inet_init();
2877 /* tables of sessions indexed by session ID */
2878 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2881 goto exit_init_data
;
2884 /* tables of streams indexed by stream ID */
2885 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2886 if (!relay_streams_ht
) {
2888 goto exit_init_data
;
2891 /* tables of streams indexed by stream ID */
2892 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2893 if (!viewer_streams_ht
) {
2895 goto exit_init_data
;
2898 ret
= utils_create_pipe(health_quit_pipe
);
2901 goto exit_health_quit_pipe
;
2904 /* Create thread to manage the client socket */
2905 ret
= pthread_create(&health_thread
, default_pthread_attr(),
2906 thread_manage_health
, (void *) NULL
);
2909 PERROR("pthread_create health");
2911 goto exit_health_thread
;
2914 /* Setup the dispatcher thread */
2915 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
2916 relay_thread_dispatcher
, (void *) NULL
);
2919 PERROR("pthread_create dispatcher");
2921 goto exit_dispatcher_thread
;
2924 /* Setup the worker thread */
2925 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
2926 relay_thread_worker
, NULL
);
2929 PERROR("pthread_create worker");
2931 goto exit_worker_thread
;
2934 /* Setup the listener thread */
2935 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
2936 relay_thread_listener
, (void *) NULL
);
2939 PERROR("pthread_create listener");
2941 goto exit_listener_thread
;
2944 ret
= relayd_live_create(live_uri
);
2946 ERR("Starting live viewer threads");
2952 * This is where we start awaiting program completion (e.g. through
2953 * signal that asks threads to teardown).
2956 ret
= relayd_live_join();
2962 ret
= pthread_join(listener_thread
, &status
);
2965 PERROR("pthread_join listener_thread");
2969 exit_listener_thread
:
2970 ret
= pthread_join(worker_thread
, &status
);
2973 PERROR("pthread_join worker_thread");
2978 ret
= pthread_join(dispatcher_thread
, &status
);
2981 PERROR("pthread_join dispatcher_thread");
2984 exit_dispatcher_thread
:
2986 ret
= pthread_join(health_thread
, &status
);
2989 PERROR("pthread_join health_thread");
2994 utils_close_pipe(health_quit_pipe
);
2995 exit_health_quit_pipe
:
2998 health_app_destroy(health_relayd
);
2999 exit_health_app_create
:
3002 * Wait for all pending call_rcu work to complete before tearing
3003 * down data structures. call_rcu worker may be trying to
3004 * perform lookups in those structures.
3009 /* Ensure all prior call_rcu are done. */