2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/daemonize.h>
48 #include <common/futex.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/sessiond-comm/inet.h>
51 #include <common/sessiond-comm/relayd.h>
52 #include <common/uri.h>
53 #include <common/utils.h>
56 #include "ctf-trace.h"
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
63 /* command line options */
64 char *opt_output_path
;
65 static int opt_daemon
, opt_background
;
68 * We need to wait for listener and live listener threads, as well as
69 * health check thread, before being ready to signal readiness.
71 #define NR_LTTNG_RELAY_READY 3
72 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
73 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
74 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
76 static struct lttng_uri
*control_uri
;
77 static struct lttng_uri
*data_uri
;
78 static struct lttng_uri
*live_uri
;
82 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
85 * Quit pipe for all threads. This permits a single cancellation point
86 * for all threads when receiving an event on the pipe.
88 int thread_quit_pipe
[2] = { -1, -1 };
91 * This pipe is used to inform the worker thread that a command is queued and
92 * ready to be processed.
94 static int relay_cmd_pipe
[2] = { -1, -1 };
96 /* Shared between threads */
97 static int dispatch_thread_exit
;
99 static pthread_t listener_thread
;
100 static pthread_t dispatcher_thread
;
101 static pthread_t worker_thread
;
102 static pthread_t health_thread
;
104 static uint64_t last_relay_stream_id
;
105 static uint64_t last_relay_session_id
;
108 * Relay command queue.
110 * The relay_thread_listener and relay_thread_dispatcher communicate with this
113 static struct relay_cmd_queue relay_cmd_queue
;
115 /* buffer allocated at startup, used to store the trace data */
116 static char *data_buffer
;
117 static unsigned int data_buffer_size
;
119 /* We need those values for the file/dir creation. */
120 static uid_t relayd_uid
;
121 static gid_t relayd_gid
;
123 /* Global relay stream hash table. */
124 struct lttng_ht
*relay_streams_ht
;
126 /* Global relay viewer stream hash table. */
127 struct lttng_ht
*viewer_streams_ht
;
129 /* Global hash table that stores relay index object. */
130 struct lttng_ht
*indexes_ht
;
132 /* Relayd health monitoring */
133 struct health_app
*health_relayd
;
136 * usage function on stderr
141 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
142 fprintf(stderr
, " -h, --help Display this usage.\n");
143 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
144 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
145 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
146 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
147 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
148 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
149 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
150 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
154 int parse_args(int argc
, char **argv
)
158 char *default_address
;
160 static struct option long_options
[] = {
161 { "control-port", 1, 0, 'C', },
162 { "data-port", 1, 0, 'D', },
163 { "daemonize", 0, 0, 'd', },
164 { "group", 1, 0, 'g', },
165 { "help", 0, 0, 'h', },
166 { "output", 1, 0, 'o', },
167 { "verbose", 0, 0, 'v', },
172 int option_index
= 0;
173 c
= getopt_long(argc
, argv
, "dhv" "C:D:L:o:g:",
174 long_options
, &option_index
);
181 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
183 fprintf(stderr
, " with arg %s\n", optarg
);
187 ret
= uri_parse(optarg
, &control_uri
);
189 ERR("Invalid control URI specified");
192 if (control_uri
->port
== 0) {
193 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
197 ret
= uri_parse(optarg
, &data_uri
);
199 ERR("Invalid data URI specified");
202 if (data_uri
->port
== 0) {
203 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
207 ret
= uri_parse(optarg
, &live_uri
);
209 ERR("Invalid live URI specified");
212 if (live_uri
->port
== 0) {
213 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
220 tracing_group_name
= optarg
;
226 ret
= asprintf(&opt_output_path
, "%s", optarg
);
228 PERROR("asprintf opt_output_path");
233 /* Verbose level can increase using multiple -v */
234 lttng_opt_verbose
+= 1;
237 /* Unknown option or other error.
238 * Error is printed by getopt, just return */
244 /* assign default values */
245 if (control_uri
== NULL
) {
246 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
247 DEFAULT_NETWORK_CONTROL_PORT
);
249 PERROR("asprintf default data address");
253 ret
= uri_parse(default_address
, &control_uri
);
254 free(default_address
);
256 ERR("Invalid control URI specified");
260 if (data_uri
== NULL
) {
261 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
262 DEFAULT_NETWORK_DATA_PORT
);
264 PERROR("asprintf default data address");
268 ret
= uri_parse(default_address
, &data_uri
);
269 free(default_address
);
271 ERR("Invalid data URI specified");
275 if (live_uri
== NULL
) {
276 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
277 DEFAULT_NETWORK_VIEWER_PORT
);
279 PERROR("asprintf default viewer control address");
283 ret
= uri_parse(default_address
, &live_uri
);
284 free(default_address
);
286 ERR("Invalid viewer control URI specified");
303 /* free the dynamically allocated opt_output_path */
304 free(opt_output_path
);
306 /* Close thread quit pipes */
307 utils_close_pipe(thread_quit_pipe
);
309 uri_free(control_uri
);
311 /* Live URI is freed in the live thread. */
315 * Write to writable pipe used to notify a thread.
318 int notify_thread_pipe(int wpipe
)
322 ret
= lttng_write(wpipe
, "!", 1);
324 PERROR("write poll pipe");
330 static void notify_health_quit_pipe(int *pipe
)
334 ret
= lttng_write(pipe
[1], "4", 1);
336 PERROR("write relay health quit");
341 * Stop all threads by closing the thread quit pipe.
344 void stop_threads(void)
348 /* Stopping all threads */
349 DBG("Terminating all threads");
350 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
352 ERR("write error on thread quit pipe");
355 notify_health_quit_pipe(health_quit_pipe
);
357 /* Dispatch thread */
358 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
359 futex_nto1_wake(&relay_cmd_queue
.futex
);
363 * Signal handler for the daemon
365 * Simply stop all worker threads, leaving main() return gracefully after
366 * joining all threads and calling cleanup().
369 void sighandler(int sig
)
373 DBG("SIGPIPE caught");
376 DBG("SIGINT caught");
380 DBG("SIGTERM caught");
384 CMM_STORE_SHARED(recv_child_signal
, 1);
392 * Setup signal handler for :
393 * SIGINT, SIGTERM, SIGPIPE
396 int set_signal_handler(void)
402 if ((ret
= sigemptyset(&sigset
)) < 0) {
403 PERROR("sigemptyset");
407 sa
.sa_handler
= sighandler
;
410 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
415 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
420 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
425 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
430 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
435 void lttng_relay_notify_ready(void)
437 /* Notify the parent of the fork() process that we are ready. */
438 if (opt_daemon
|| opt_background
) {
439 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
440 kill(child_ppid
, SIGUSR1
);
446 * Init thread quit pipe.
448 * Return -1 on error or 0 if all pipes are created.
451 int init_thread_quit_pipe(void)
455 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
461 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
464 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
468 if (events
== NULL
|| size
== 0) {
473 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
479 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
491 * Check if the thread quit pipe was triggered.
493 * Return 1 if it was triggered else 0;
496 int check_thread_quit_pipe(int fd
, uint32_t events
)
498 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
506 * Create and init socket from uri.
509 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
512 struct lttcomm_sock
*sock
= NULL
;
514 sock
= lttcomm_alloc_sock_from_uri(uri
);
516 ERR("Allocating socket");
520 ret
= lttcomm_create_sock(sock
);
524 DBG("Listening on sock %d", sock
->fd
);
526 ret
= sock
->ops
->bind(sock
);
531 ret
= sock
->ops
->listen(sock
, -1);
541 lttcomm_destroy_sock(sock
);
547 * Return nonzero if stream needs to be closed.
550 int close_stream_check(struct relay_stream
*stream
)
553 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
555 * We are about to close the stream so set the data pending flag to 1
556 * which will make the end data pending command skip the stream which
557 * is now closed and ready. Note that after proceeding to a file close,
558 * the written file is ready for reading.
560 stream
->data_pending_check_done
= 1;
567 * This thread manages the listening for new connections on the network
570 void *relay_thread_listener(void *data
)
572 int i
, ret
, pollfd
, err
= -1;
574 uint32_t revents
, nb_fd
;
575 struct lttng_poll_event events
;
576 struct lttcomm_sock
*control_sock
, *data_sock
;
578 DBG("[thread] Relay listener started");
580 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
582 health_code_update();
584 control_sock
= relay_init_sock(control_uri
);
586 goto error_sock_control
;
589 data_sock
= relay_init_sock(data_uri
);
591 goto error_sock_relay
;
595 * Pass 3 as size here for the thread quit pipe, control and data socket.
597 ret
= create_thread_poll_set(&events
, 3);
599 goto error_create_poll
;
602 /* Add the control socket */
603 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
608 /* Add the data socket */
609 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
614 lttng_relay_notify_ready();
617 health_code_update();
619 DBG("Listener accepting connections");
623 ret
= lttng_poll_wait(&events
, -1);
627 * Restart interrupted system call.
629 if (errno
== EINTR
) {
637 DBG("Relay new connection received");
638 for (i
= 0; i
< nb_fd
; i
++) {
639 health_code_update();
641 /* Fetch once the poll data */
642 revents
= LTTNG_POLL_GETEV(&events
, i
);
643 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
645 /* Thread quit pipe has been closed. Killing thread. */
646 ret
= check_thread_quit_pipe(pollfd
, revents
);
652 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
653 ERR("socket poll error");
655 } else if (revents
& LPOLLIN
) {
657 * Get allocated in this thread,
658 * enqueued to a global queue, dequeued
659 * and freed in the worker thread.
661 struct relay_command
*relay_cmd
;
662 struct lttcomm_sock
*newsock
;
664 relay_cmd
= zmalloc(sizeof(struct relay_command
));
665 if (relay_cmd
== NULL
) {
666 PERROR("relay command zmalloc");
670 if (pollfd
== data_sock
->fd
) {
671 newsock
= data_sock
->ops
->accept(data_sock
);
673 PERROR("accepting data sock");
677 relay_cmd
->type
= RELAY_DATA
;
678 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
680 assert(pollfd
== control_sock
->fd
);
681 newsock
= control_sock
->ops
->accept(control_sock
);
683 PERROR("accepting control sock");
687 relay_cmd
->type
= RELAY_CONTROL
;
688 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
690 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
693 PERROR("setsockopt inet");
694 lttcomm_destroy_sock(newsock
);
698 relay_cmd
->sock
= newsock
;
700 * Lock free enqueue the request.
702 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
705 * Wake the dispatch queue futex. Implicit memory
706 * barrier with the exchange in cds_wfq_enqueue.
708 futex_nto1_wake(&relay_cmd_queue
.futex
);
716 lttng_poll_clean(&events
);
718 if (data_sock
->fd
>= 0) {
719 ret
= data_sock
->ops
->close(data_sock
);
724 lttcomm_destroy_sock(data_sock
);
726 if (control_sock
->fd
>= 0) {
727 ret
= control_sock
->ops
->close(control_sock
);
732 lttcomm_destroy_sock(control_sock
);
736 ERR("Health error occurred in %s", __func__
);
738 health_unregister(health_relayd
);
739 DBG("Relay listener thread cleanup complete");
745 * This thread manages the dispatching of the requests to worker threads
748 void *relay_thread_dispatcher(void *data
)
752 struct cds_wfq_node
*node
;
753 struct relay_command
*relay_cmd
= NULL
;
755 DBG("[thread] Relay dispatcher started");
757 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
759 health_code_update();
761 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
762 health_code_update();
764 /* Atomically prepare the queue futex */
765 futex_nto1_prepare(&relay_cmd_queue
.futex
);
768 health_code_update();
770 /* Dequeue commands */
771 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
773 DBG("Woken up but nothing in the relay command queue");
774 /* Continue thread execution */
778 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
779 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
782 * Inform worker thread of the new request. This
783 * call is blocking so we can be assured that the data will be read
784 * at some point in time or wait to the end of the world :)
786 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
787 sizeof(struct relay_command
));
789 if (ret
< sizeof(struct relay_command
)) {
790 PERROR("write cmd pipe");
793 } while (node
!= NULL
);
795 /* Futex wait on queue. Blocking call on futex() */
797 futex_nto1_wait(&relay_cmd_queue
.futex
);
801 /* Normal exit, no error */
807 ERR("Health error occurred in %s", __func__
);
809 health_unregister(health_relayd
);
810 DBG("Dispatch thread dying");
816 * Get stream from stream id.
817 * Need to be called with RCU read-side lock held.
819 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
821 struct lttng_ht_node_ulong
*node
;
822 struct lttng_ht_iter iter
;
823 struct relay_stream
*ret
;
825 lttng_ht_lookup(relay_streams_ht
,
826 (void *)((unsigned long) stream_id
),
828 node
= lttng_ht_iter_get_node_ulong(&iter
);
830 DBG("Relay stream %" PRIu64
" not found", stream_id
);
835 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
842 void deferred_free_stream(struct rcu_head
*head
)
844 struct relay_stream
*stream
=
845 caa_container_of(head
, struct relay_stream
, rcu_node
);
847 free(stream
->path_name
);
848 free(stream
->channel_name
);
853 void deferred_free_session(struct rcu_head
*head
)
855 struct relay_session
*session
=
856 caa_container_of(head
, struct relay_session
, rcu_node
);
861 * Close a given stream. The stream is freed using a call RCU.
863 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
864 * BEFORE the stream lock MUST be acquired.
866 static void destroy_stream(struct relay_stream
*stream
)
869 struct relay_viewer_stream
*vstream
;
870 struct lttng_ht_iter iter
;
874 delret
= close(stream
->fd
);
876 PERROR("close stream");
879 if (stream
->index_fd
>= 0) {
880 delret
= close(stream
->index_fd
);
882 PERROR("close stream index_fd");
886 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
889 * Set the last good value into the viewer stream. This is done
890 * right before the stream gets deleted from the hash table. The
891 * lookup failure on the live thread side of a stream indicates
892 * that the viewer stream index received value should be used.
894 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
895 vstream
->total_index_received
= stream
->total_index_received
;
896 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
897 vstream
->close_write_flag
= 1;
898 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
901 /* Cleanup index of that stream. */
902 relay_index_destroy_by_stream_id(stream
->stream_handle
);
904 iter
.iter
.node
= &stream
->stream_n
.node
;
905 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
907 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
908 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
911 if (stream
->ctf_trace
) {
912 ctf_trace_try_destroy(stream
->ctf_trace
);
915 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
916 DBG("Closed tracefile %d from close stream", stream
->fd
);
920 * relay_delete_session: Free all memory associated with a session and
924 void relay_delete_session(struct relay_command
*cmd
,
925 struct lttng_ht
*sessions_ht
)
927 struct lttng_ht_iter iter
;
928 struct lttng_ht_node_ulong
*node
;
929 struct relay_stream
*stream
;
936 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
939 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
940 node
= lttng_ht_iter_get_node_ulong(&iter
);
944 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
945 if (stream
->session
== cmd
->session
) {
946 destroy_stream(stream
);
947 cmd
->session
->stream_count
--;
948 assert(cmd
->session
->stream_count
>= 0);
952 /* Make this session not visible anymore. */
953 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
954 ret
= lttng_ht_del(sessions_ht
, &iter
);
956 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
961 * Copy index data from the control port to a given index object.
963 static void copy_index_control_data(struct relay_index
*index
,
964 struct lttcomm_relayd_index
*data
)
970 * The index on disk is encoded in big endian, so we don't need to convert
971 * the data received on the network. The data_offset value is NEVER
972 * modified here and is updated by the data thread.
974 index
->index_data
.packet_size
= data
->packet_size
;
975 index
->index_data
.content_size
= data
->content_size
;
976 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
977 index
->index_data
.timestamp_end
= data
->timestamp_end
;
978 index
->index_data
.events_discarded
= data
->events_discarded
;
979 index
->index_data
.stream_id
= data
->stream_id
;
983 * Handle the RELAYD_CREATE_SESSION command.
985 * On success, send back the session id or else return a negative value.
988 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
989 struct relay_command
*cmd
,
990 struct lttng_ht
*sessions_ht
)
992 int ret
= 0, send_ret
;
993 struct relay_session
*session
;
994 struct lttcomm_relayd_status_session reply
;
999 memset(&reply
, 0, sizeof(reply
));
1001 session
= zmalloc(sizeof(struct relay_session
));
1002 if (session
== NULL
) {
1003 PERROR("relay session zmalloc");
1008 session
->id
= ++last_relay_session_id
;
1009 session
->sock
= cmd
->sock
;
1010 session
->minor
= cmd
->minor
;
1011 session
->major
= cmd
->major
;
1012 cmd
->session
= session
;
1014 reply
.session_id
= htobe64(session
->id
);
1016 switch (cmd
->minor
) {
1017 case 4: /* LTTng sessiond 2.4 */
1019 ret
= cmd_create_session_2_4(cmd
, session
);
1023 lttng_ht_node_init_ulong(&session
->session_n
,
1024 (unsigned long) session
->id
);
1025 lttng_ht_add_unique_ulong(sessions_ht
,
1026 &session
->session_n
);
1028 DBG("Created session %" PRIu64
, session
->id
);
1032 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1034 reply
.ret_code
= htobe32(LTTNG_OK
);
1037 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1039 ERR("Relayd sending session id");
1047 * When we have received all the streams and the metadata for a channel,
1048 * we make them visible to the viewer threads.
1051 void set_viewer_ready_flag(struct relay_command
*cmd
)
1053 struct relay_stream_recv_handle
*node
, *tmp_node
;
1055 cds_list_for_each_entry_safe(node
, tmp_node
, &cmd
->recv_head
, node
) {
1056 struct relay_stream
*stream
;
1059 stream
= relay_stream_find_by_id(node
->id
);
1062 * Stream is most probably being cleaned up by the data thread thus
1063 * simply continue to the next one.
1069 stream
->viewer_ready
= 1;
1072 /* Clean stream handle node. */
1073 cds_list_del(&node
->node
);
1081 * Add a recv handle node to the connection recv list with the given stream
1082 * handle. A new node is allocated thus must be freed when the node is deleted
1085 static void queue_stream_handle(uint64_t handle
, struct relay_command
*cmd
)
1087 struct relay_stream_recv_handle
*node
;
1091 node
= zmalloc(sizeof(*node
));
1093 PERROR("zmalloc queue stream handle");
1098 cds_list_add(&node
->node
, &cmd
->recv_head
);
1102 * relay_add_stream: allocate a new stream for a session
1105 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1106 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1108 struct relay_session
*session
= cmd
->session
;
1109 struct relay_stream
*stream
= NULL
;
1110 struct lttcomm_relayd_status_stream reply
;
1113 if (!session
|| cmd
->version_check_done
== 0) {
1114 ERR("Trying to add a stream before version check");
1116 goto end_no_session
;
1119 stream
= zmalloc(sizeof(struct relay_stream
));
1120 if (stream
== NULL
) {
1121 PERROR("relay stream zmalloc");
1123 goto end_no_session
;
1126 switch (cmd
->minor
) {
1127 case 1: /* LTTng sessiond 2.1 */
1128 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1130 case 2: /* LTTng sessiond 2.2 */
1132 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1136 goto err_free_stream
;
1140 stream
->stream_handle
= ++last_relay_stream_id
;
1141 stream
->prev_seq
= -1ULL;
1142 stream
->session
= session
;
1143 stream
->index_fd
= -1;
1144 stream
->read_index_fd
= -1;
1145 stream
->ctf_trace
= NULL
;
1146 pthread_mutex_init(&stream
->lock
, NULL
);
1148 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1150 ERR("relay creating output directory");
1155 * No need to use run_as API here because whatever we receives, the relayd
1156 * uses its own credentials for the stream files.
1158 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1159 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1161 ERR("Create output file");
1165 if (stream
->tracefile_size
) {
1166 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1168 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1171 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1172 stream
->metadata_flag
= 1;
1174 * When we receive a new metadata stream, we create a new
1175 * ctf_trace and we assign this ctf_trace to all streams with
1178 * If later on we receive a new stream for the same ctf_trace,
1179 * we copy the information from the first hit in the HT to the
1182 stream
->ctf_trace
= ctf_trace_create();
1183 if (!stream
->ctf_trace
) {
1187 stream
->ctf_trace
->refcount
++;
1188 stream
->ctf_trace
->metadata_stream
= stream
;
1190 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1191 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1194 * Add the stream handle in the recv list of the connection. Once the end
1195 * stream message is received, this list is emptied and streams are set
1196 * with the viewer ready flag.
1198 if (stream
->metadata_flag
) {
1199 stream
->viewer_ready
= 1;
1201 queue_stream_handle(stream
->stream_handle
, cmd
);
1204 lttng_ht_node_init_ulong(&stream
->stream_n
,
1205 (unsigned long) stream
->stream_handle
);
1206 lttng_ht_add_unique_ulong(relay_streams_ht
,
1209 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1210 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1211 session
->stream_count
++;
1213 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1214 stream
->stream_handle
);
1217 reply
.handle
= htobe64(stream
->stream_handle
);
1218 /* send the session id to the client or a negative return code on error */
1220 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1221 /* stream was not properly added to the ht, so free it */
1224 reply
.ret_code
= htobe32(LTTNG_OK
);
1227 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1228 sizeof(struct lttcomm_relayd_status_stream
), 0);
1230 ERR("Relay sending stream id");
1239 free(stream
->path_name
);
1240 free(stream
->channel_name
);
1246 * relay_close_stream: close a specific stream
1249 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1250 struct relay_command
*cmd
)
1253 struct relay_session
*session
= cmd
->session
;
1254 struct lttcomm_relayd_close_stream stream_info
;
1255 struct lttcomm_relayd_generic_reply reply
;
1256 struct relay_stream
*stream
;
1258 DBG("Close stream received");
1260 if (!session
|| cmd
->version_check_done
== 0) {
1261 ERR("Trying to close a stream before version check");
1263 goto end_no_session
;
1266 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1267 sizeof(struct lttcomm_relayd_close_stream
), 0);
1268 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1270 /* Orderly shutdown. Not necessary to print an error. */
1271 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1273 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1276 goto end_no_session
;
1280 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1286 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1287 stream
->close_flag
= 1;
1288 session
->stream_count
--;
1289 assert(session
->stream_count
>= 0);
1291 if (close_stream_check(stream
)) {
1292 destroy_stream(stream
);
1299 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1301 reply
.ret_code
= htobe32(LTTNG_OK
);
1303 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1304 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1306 ERR("Relay sending stream id");
1315 * relay_unknown_command: send -1 if received unknown command
1318 void relay_unknown_command(struct relay_command
*cmd
)
1320 struct lttcomm_relayd_generic_reply reply
;
1323 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1324 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1325 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1327 ERR("Relay sending unknown command");
1332 * relay_start: send an acknowledgment to the client to tell if we are
1333 * ready to receive data. We are ready if a session is established.
1336 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1337 struct relay_command
*cmd
)
1339 int ret
= htobe32(LTTNG_OK
);
1340 struct lttcomm_relayd_generic_reply reply
;
1341 struct relay_session
*session
= cmd
->session
;
1344 DBG("Trying to start the streaming without a session established");
1345 ret
= htobe32(LTTNG_ERR_UNK
);
1348 reply
.ret_code
= ret
;
1349 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1350 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1352 ERR("Relay sending start ack");
1359 * Append padding to the file pointed by the file descriptor fd.
1361 static int write_padding_to_file(int fd
, uint32_t size
)
1370 zeros
= zmalloc(size
);
1371 if (zeros
== NULL
) {
1372 PERROR("zmalloc zeros for padding");
1377 ret
= lttng_write(fd
, zeros
, size
);
1379 PERROR("write padding to file");
1389 * relay_recv_metadata: receive the metada for the session.
1392 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1393 struct relay_command
*cmd
)
1395 int ret
= htobe32(LTTNG_OK
);
1397 struct relay_session
*session
= cmd
->session
;
1398 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1399 struct relay_stream
*metadata_stream
;
1400 uint64_t data_size
, payload_size
;
1403 ERR("Metadata sent before version check");
1408 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1409 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1410 ERR("Incorrect data size");
1414 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1416 if (data_buffer_size
< data_size
) {
1417 /* In case the realloc fails, we can free the memory */
1420 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1421 if (!tmp_data_ptr
) {
1422 ERR("Allocating data buffer");
1427 data_buffer
= tmp_data_ptr
;
1428 data_buffer_size
= data_size
;
1430 memset(data_buffer
, 0, data_size
);
1431 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1432 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1433 if (ret
< 0 || ret
!= data_size
) {
1435 /* Orderly shutdown. Not necessary to print an error. */
1436 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1438 ERR("Relay didn't receive the whole metadata");
1443 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1446 metadata_stream
= relay_stream_find_by_id(
1447 be64toh(metadata_struct
->stream_id
));
1448 if (!metadata_stream
) {
1453 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1455 if (size_ret
< payload_size
) {
1456 ERR("Relay error writing metadata on file");
1461 ret
= write_padding_to_file(metadata_stream
->fd
,
1462 be32toh(metadata_struct
->padding_size
));
1466 metadata_stream
->ctf_trace
->metadata_received
+=
1467 payload_size
+ be32toh(metadata_struct
->padding_size
);
1469 DBG2("Relay metadata written");
1478 * relay_send_version: send relayd version number
1481 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1482 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1485 struct lttcomm_relayd_version reply
, msg
;
1489 cmd
->version_check_done
= 1;
1491 /* Get version from the other side. */
1492 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1493 if (ret
< 0 || ret
!= sizeof(msg
)) {
1495 /* Orderly shutdown. Not necessary to print an error. */
1496 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1498 ERR("Relay failed to receive the version values.");
1504 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1505 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1507 /* Major versions must be the same */
1508 if (reply
.major
!= be32toh(msg
.major
)) {
1509 DBG("Incompatible major versions (%u vs %u), deleting session",
1510 reply
.major
, be32toh(msg
.major
));
1511 relay_delete_session(cmd
, sessions_ht
);
1516 cmd
->major
= reply
.major
;
1517 /* We adapt to the lowest compatible version */
1518 if (reply
.minor
<= be32toh(msg
.minor
)) {
1519 cmd
->minor
= reply
.minor
;
1521 cmd
->minor
= be32toh(msg
.minor
);
1524 reply
.major
= htobe32(reply
.major
);
1525 reply
.minor
= htobe32(reply
.minor
);
1526 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1527 sizeof(struct lttcomm_relayd_version
), 0);
1529 ERR("Relay sending version");
1532 DBG("Version check done using protocol %u.%u", cmd
->major
,
1540 * Check for data pending for a given stream id from the session daemon.
1543 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1544 struct relay_command
*cmd
)
1546 struct relay_session
*session
= cmd
->session
;
1547 struct lttcomm_relayd_data_pending msg
;
1548 struct lttcomm_relayd_generic_reply reply
;
1549 struct relay_stream
*stream
;
1551 uint64_t last_net_seq_num
, stream_id
;
1553 DBG("Data pending command received");
1555 if (!session
|| cmd
->version_check_done
== 0) {
1556 ERR("Trying to check for data before version check");
1558 goto end_no_session
;
1561 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1562 if (ret
< sizeof(msg
)) {
1564 /* Orderly shutdown. Not necessary to print an error. */
1565 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1567 ERR("Relay didn't receive valid data_pending struct size : %d",
1571 goto end_no_session
;
1574 stream_id
= be64toh(msg
.stream_id
);
1575 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1578 stream
= relay_stream_find_by_id(stream_id
);
1579 if (stream
== NULL
) {
1584 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1585 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1588 /* Avoid wrapping issue */
1589 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1590 /* Data has in fact been written and is NOT pending */
1593 /* Data still being streamed thus pending */
1597 /* Pending check is now done. */
1598 stream
->data_pending_check_done
= 1;
1603 reply
.ret_code
= htobe32(ret
);
1604 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1606 ERR("Relay data pending ret code failed");
1614 * Wait for the control socket to reach a quiescent state.
1616 * Note that for now, when receiving this command from the session daemon, this
1617 * means that every subsequent commands or data received on the control socket
1618 * has been handled. So, this is why we simply return OK here.
1621 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1622 struct relay_command
*cmd
)
1626 struct relay_stream
*stream
;
1627 struct lttng_ht_iter iter
;
1628 struct lttcomm_relayd_quiescent_control msg
;
1629 struct lttcomm_relayd_generic_reply reply
;
1631 DBG("Checking quiescent state on control socket");
1633 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1634 ERR("Trying to check for data before version check");
1636 goto end_no_session
;
1639 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1640 if (ret
< sizeof(msg
)) {
1642 /* Orderly shutdown. Not necessary to print an error. */
1643 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1645 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1649 goto end_no_session
;
1652 stream_id
= be64toh(msg
.stream_id
);
1655 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1657 if (stream
->stream_handle
== stream_id
) {
1658 stream
->data_pending_check_done
= 1;
1659 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1666 reply
.ret_code
= htobe32(LTTNG_OK
);
1667 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1669 ERR("Relay data quiescent control ret code failed");
1677 * Initialize a data pending command. This means that a client is about to ask
1678 * for data pending for each stream he/she holds. Simply iterate over all
1679 * streams of a session and set the data_pending_check_done flag.
1681 * This command returns to the client a LTTNG_OK code.
1684 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1685 struct relay_command
*cmd
)
1688 struct lttng_ht_iter iter
;
1689 struct lttcomm_relayd_begin_data_pending msg
;
1690 struct lttcomm_relayd_generic_reply reply
;
1691 struct relay_stream
*stream
;
1692 uint64_t session_id
;
1697 DBG("Init streams for data pending");
1699 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1700 ERR("Trying to check for data before version check");
1702 goto end_no_session
;
1705 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1706 if (ret
< sizeof(msg
)) {
1708 /* Orderly shutdown. Not necessary to print an error. */
1709 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1711 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1715 goto end_no_session
;
1718 session_id
= be64toh(msg
.session_id
);
1721 * Iterate over all streams to set the begin data pending flag. For now, the
1722 * streams are indexed by stream handle so we have to iterate over all
1723 * streams to find the one associated with the right session_id.
1726 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1728 if (stream
->session
->id
== session_id
) {
1729 stream
->data_pending_check_done
= 0;
1730 DBG("Set begin data pending flag to stream %" PRIu64
,
1731 stream
->stream_handle
);
1736 /* All good, send back reply. */
1737 reply
.ret_code
= htobe32(LTTNG_OK
);
1739 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1741 ERR("Relay begin data pending send reply failed");
1749 * End data pending command. This will check, for a given session id, if each
1750 * stream associated with it has its data_pending_check_done flag set. If not,
1751 * this means that the client lost track of the stream but the data is still
1752 * being streamed on our side. In this case, we inform the client that data is
1755 * Return to the client if there is data in flight or not with a ret_code.
1758 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1759 struct relay_command
*cmd
)
1762 struct lttng_ht_iter iter
;
1763 struct lttcomm_relayd_end_data_pending msg
;
1764 struct lttcomm_relayd_generic_reply reply
;
1765 struct relay_stream
*stream
;
1766 uint64_t session_id
;
1767 uint32_t is_data_inflight
= 0;
1772 DBG("End data pending command");
1774 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1775 ERR("Trying to check for data before version check");
1777 goto end_no_session
;
1780 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1781 if (ret
< sizeof(msg
)) {
1783 /* Orderly shutdown. Not necessary to print an error. */
1784 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1786 ERR("Relay didn't receive valid end data_pending struct size: %d",
1790 goto end_no_session
;
1793 session_id
= be64toh(msg
.session_id
);
1795 /* Iterate over all streams to see if the begin data pending flag is set. */
1797 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1799 if (stream
->session
->id
== session_id
&&
1800 !stream
->data_pending_check_done
) {
1801 is_data_inflight
= 1;
1802 DBG("Data is still in flight for stream %" PRIu64
,
1803 stream
->stream_handle
);
1809 /* All good, send back reply. */
1810 reply
.ret_code
= htobe32(is_data_inflight
);
1812 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1814 ERR("Relay end data pending send reply failed");
1822 * Receive an index for a specific stream.
1824 * Return 0 on success else a negative value.
1827 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1828 struct relay_command
*cmd
)
1830 int ret
, send_ret
, index_created
= 0;
1831 struct relay_session
*session
= cmd
->session
;
1832 struct lttcomm_relayd_index index_info
;
1833 struct relay_index
*index
, *wr_index
= NULL
;
1834 struct lttcomm_relayd_generic_reply reply
;
1835 struct relay_stream
*stream
;
1836 uint64_t net_seq_num
;
1840 DBG("Relay receiving index");
1842 if (!session
|| cmd
->version_check_done
== 0) {
1843 ERR("Trying to close a stream before version check");
1845 goto end_no_session
;
1848 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1849 sizeof(index_info
), 0);
1850 if (ret
< sizeof(index_info
)) {
1852 /* Orderly shutdown. Not necessary to print an error. */
1853 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1855 ERR("Relay didn't receive valid index struct size : %d", ret
);
1858 goto end_no_session
;
1861 net_seq_num
= be64toh(index_info
.net_seq_num
);
1864 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1867 goto end_rcu_unlock
;
1870 /* Live beacon handling */
1871 if (index_info
.packet_size
== 0) {
1872 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1875 * Only flag a stream inactive when it has already received data.
1877 if (stream
->total_index_received
> 0) {
1878 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1881 goto end_rcu_unlock
;
1883 stream
->beacon_ts_end
= -1ULL;
1886 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1888 /* A successful creation will add the object to the HT. */
1889 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1891 goto end_rcu_unlock
;
1896 copy_index_control_data(index
, &index_info
);
1898 if (index_created
) {
1900 * Try to add the relay index object to the hash table. If an object
1901 * already exist, destroy back the index created, set the data in this
1902 * object and write it on disk.
1904 relay_index_add(index
, &wr_index
);
1906 copy_index_control_data(wr_index
, &index_info
);
1910 /* The index already exists so write it on disk. */
1914 /* Do we have a writable ready index to write on disk. */
1916 /* Starting at 2.4, create the index file if none available. */
1917 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1918 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1919 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1920 stream
->tracefile_count_current
);
1922 goto end_rcu_unlock
;
1924 stream
->index_fd
= ret
;
1927 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1929 goto end_rcu_unlock
;
1931 stream
->total_index_received
++;
1938 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1940 reply
.ret_code
= htobe32(LTTNG_OK
);
1942 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1944 ERR("Relay sending close index id reply");
1953 * Receive the streams_sent message.
1955 * Return 0 on success else a negative value.
1958 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
1959 struct relay_command
*cmd
)
1962 struct lttcomm_relayd_generic_reply reply
;
1966 DBG("Relay receiving streams_sent");
1968 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1969 ERR("Trying to close a stream before version check");
1971 goto end_no_session
;
1975 * Flag every pending stream in the connection recv list that they are
1976 * ready to be used by the viewer.
1978 set_viewer_ready_flag(cmd
);
1980 reply
.ret_code
= htobe32(LTTNG_OK
);
1981 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1983 ERR("Relay sending sent_stream reply");
1995 * Process the commands received on the control socket
1998 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1999 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
2003 switch (be32toh(recv_hdr
->cmd
)) {
2004 case RELAYD_CREATE_SESSION
:
2005 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
2007 case RELAYD_ADD_STREAM
:
2008 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
2010 case RELAYD_START_DATA
:
2011 ret
= relay_start(recv_hdr
, cmd
);
2013 case RELAYD_SEND_METADATA
:
2014 ret
= relay_recv_metadata(recv_hdr
, cmd
);
2016 case RELAYD_VERSION
:
2017 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
2019 case RELAYD_CLOSE_STREAM
:
2020 ret
= relay_close_stream(recv_hdr
, cmd
);
2022 case RELAYD_DATA_PENDING
:
2023 ret
= relay_data_pending(recv_hdr
, cmd
);
2025 case RELAYD_QUIESCENT_CONTROL
:
2026 ret
= relay_quiescent_control(recv_hdr
, cmd
);
2028 case RELAYD_BEGIN_DATA_PENDING
:
2029 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
2031 case RELAYD_END_DATA_PENDING
:
2032 ret
= relay_end_data_pending(recv_hdr
, cmd
);
2034 case RELAYD_SEND_INDEX
:
2035 ret
= relay_recv_index(recv_hdr
, cmd
);
2037 case RELAYD_STREAMS_SENT
:
2038 ret
= relay_streams_sent(recv_hdr
, cmd
);
2040 case RELAYD_UPDATE_SYNC_INFO
:
2042 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2043 relay_unknown_command(cmd
);
2053 * Handle index for a data stream.
2055 * RCU read side lock MUST be acquired.
2057 * Return 0 on success else a negative value.
2059 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2062 int ret
= 0, index_created
= 0;
2063 uint64_t stream_id
, data_offset
;
2064 struct relay_index
*index
, *wr_index
= NULL
;
2068 stream_id
= stream
->stream_handle
;
2069 /* Get data offset because we are about to update the index. */
2070 data_offset
= htobe64(stream
->tracefile_size_current
);
2073 * Lookup for an existing index for that stream id/sequence number. If on
2074 * exists, the control thread already received the data for it thus we need
2075 * to write it on disk.
2077 index
= relay_index_find(stream_id
, net_seq_num
);
2079 /* A successful creation will add the object to the HT. */
2080 index
= relay_index_create(stream_id
, net_seq_num
);
2088 if (rotate_index
|| stream
->index_fd
< 0) {
2089 index
->to_close_fd
= stream
->index_fd
;
2090 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2091 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2092 stream
->tracefile_count_current
);
2094 /* This will close the stream's index fd if one. */
2095 relay_index_free_safe(index
);
2098 stream
->index_fd
= ret
;
2100 index
->fd
= stream
->index_fd
;
2101 index
->index_data
.offset
= data_offset
;
2103 if (index_created
) {
2105 * Try to add the relay index object to the hash table. If an object
2106 * already exist, destroy back the index created and set the data.
2108 relay_index_add(index
, &wr_index
);
2110 /* Copy back data from the created index. */
2111 wr_index
->fd
= index
->fd
;
2112 wr_index
->to_close_fd
= index
->to_close_fd
;
2113 wr_index
->index_data
.offset
= data_offset
;
2117 /* The index already exists so write it on disk. */
2121 /* Do we have a writable ready index to write on disk. */
2123 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2127 stream
->total_index_received
++;
2135 * relay_process_data: Process the data received on the data socket
2138 int relay_process_data(struct relay_command
*cmd
)
2140 int ret
= 0, rotate_index
= 0;
2142 struct relay_stream
*stream
;
2143 struct lttcomm_relayd_data_hdr data_hdr
;
2145 uint64_t net_seq_num
;
2148 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2149 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2152 /* Orderly shutdown. Not necessary to print an error. */
2153 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2155 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2161 stream_id
= be64toh(data_hdr
.stream_id
);
2164 stream
= relay_stream_find_by_id(stream_id
);
2167 goto end_rcu_unlock
;
2170 data_size
= be32toh(data_hdr
.data_size
);
2171 if (data_buffer_size
< data_size
) {
2174 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2175 if (!tmp_data_ptr
) {
2176 ERR("Allocating data buffer");
2179 goto end_rcu_unlock
;
2181 data_buffer
= tmp_data_ptr
;
2182 data_buffer_size
= data_size
;
2184 memset(data_buffer
, 0, data_size
);
2186 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2188 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2189 data_size
, stream_id
, net_seq_num
);
2190 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2193 /* Orderly shutdown. Not necessary to print an error. */
2194 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2197 goto end_rcu_unlock
;
2200 /* Check if a rotation is needed. */
2201 if (stream
->tracefile_size
> 0 &&
2202 (stream
->tracefile_size_current
+ data_size
) >
2203 stream
->tracefile_size
) {
2204 struct relay_viewer_stream
*vstream
;
2207 new_id
= (stream
->tracefile_count_current
+ 1) %
2208 stream
->tracefile_count
;
2210 * When we wrap-around back to 0, we start overwriting old
2213 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2214 stream
->tracefile_overwrite
= 1;
2216 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2217 if (stream
->tracefile_overwrite
) {
2218 stream
->oldest_tracefile_id
=
2219 (stream
->oldest_tracefile_id
+ 1) %
2220 stream
->tracefile_count
;
2222 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
2225 * The viewer is reading a file about to be
2226 * overwritten. Close the FDs it is
2227 * currently using and let it handle the fault.
2229 if (vstream
->tracefile_count_current
== new_id
) {
2230 pthread_mutex_lock(&vstream
->overwrite_lock
);
2231 vstream
->abort_flag
= 1;
2232 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2233 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2234 stream
->channel_name
, new_id
);
2235 } else if (vstream
->tracefile_count_current
==
2236 stream
->tracefile_count_current
) {
2238 * The reader and writer were in the
2239 * same trace file, inform the viewer
2240 * that no new index will ever be added
2243 vstream
->close_write_flag
= 1;
2246 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2247 stream
->tracefile_size
, stream
->tracefile_count
,
2248 relayd_uid
, relayd_gid
, stream
->fd
,
2249 &(stream
->tracefile_count_current
), &stream
->fd
);
2250 stream
->total_index_received
= 0;
2251 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2253 ERR("Rotating stream output file");
2254 goto end_rcu_unlock
;
2256 /* Reset current size because we just perform a stream rotation. */
2257 stream
->tracefile_size_current
= 0;
2262 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2263 * index are NOT supported.
2265 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2266 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2268 goto end_rcu_unlock
;
2272 /* Write data to stream output fd. */
2273 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2274 if (size_ret
< data_size
) {
2275 ERR("Relay error writing data to file");
2277 goto end_rcu_unlock
;
2280 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2281 ret
, stream
->stream_handle
);
2283 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2285 goto end_rcu_unlock
;
2287 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2289 stream
->prev_seq
= net_seq_num
;
2291 /* Check if we need to close the FD */
2292 if (close_stream_check(stream
)) {
2293 destroy_stream(stream
);
2303 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2307 lttng_poll_del(events
, pollfd
);
2309 ret
= close(pollfd
);
2311 ERR("Closing pollfd %d", pollfd
);
2316 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2317 struct lttng_ht
*relay_connections_ht
)
2319 struct relay_command
*relay_connection
;
2322 relay_connection
= zmalloc(sizeof(struct relay_command
));
2323 if (relay_connection
== NULL
) {
2324 PERROR("Relay command zmalloc");
2327 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2328 if (ret
< sizeof(struct relay_command
)) {
2329 PERROR("read relay cmd pipe");
2332 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2335 * Only used by the control side and the reference is copied inside each
2336 * stream from that connection. Thus a destroy HT must be done after every
2337 * stream has been destroyed.
2339 if (relay_connection
->type
== RELAY_CONTROL
) {
2340 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2341 LTTNG_HT_TYPE_STRING
);
2342 if (!relay_connection
->ctf_traces_ht
) {
2347 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2348 (unsigned long) relay_connection
->sock
->fd
);
2350 lttng_ht_add_unique_ulong(relay_connections_ht
,
2351 &relay_connection
->sock_n
);
2353 return lttng_poll_add(events
,
2354 relay_connection
->sock
->fd
,
2355 LPOLLIN
| LPOLLRDHUP
);
2358 free(relay_connection
);
2364 void deferred_free_connection(struct rcu_head
*head
)
2366 struct relay_command
*relay_connection
=
2367 caa_container_of(head
, struct relay_command
, rcu_node
);
2369 lttcomm_destroy_sock(relay_connection
->sock
);
2370 free(relay_connection
);
2374 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2375 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2376 struct lttng_ht
*sessions_ht
)
2380 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2383 if (relay_connection
->type
== RELAY_CONTROL
) {
2384 struct relay_stream_recv_handle
*node
, *tmp_node
;
2386 relay_delete_session(relay_connection
, sessions_ht
);
2387 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2389 /* Clean up recv list. */
2390 cds_list_for_each_entry_safe(node
, tmp_node
,
2391 &relay_connection
->recv_head
, node
) {
2392 cds_list_del(&node
->node
);
2397 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2401 * This thread does the actual work
2404 void *relay_thread_worker(void *data
)
2406 int ret
, err
= -1, last_seen_data_fd
= -1;
2408 struct relay_command
*relay_connection
;
2409 struct lttng_poll_event events
;
2410 struct lttng_ht
*relay_connections_ht
;
2411 struct lttng_ht_node_ulong
*node
;
2412 struct lttng_ht_iter iter
;
2413 struct lttcomm_relayd_hdr recv_hdr
;
2414 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2415 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2417 DBG("[thread] Relay worker started");
2419 rcu_register_thread();
2421 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2423 health_code_update();
2425 /* table of connections indexed on socket */
2426 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2427 if (!relay_connections_ht
) {
2428 goto relay_connections_ht_error
;
2431 /* Tables of received indexes indexed by index handle and net_seq_num. */
2432 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2434 goto indexes_ht_error
;
2437 ret
= create_thread_poll_set(&events
, 2);
2439 goto error_poll_create
;
2442 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2449 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2451 health_code_update();
2453 /* Infinite blocking call, waiting for transmission */
2454 DBG3("Relayd worker thread polling...");
2455 health_poll_entry();
2456 ret
= lttng_poll_wait(&events
, -1);
2460 * Restart interrupted system call.
2462 if (errno
== EINTR
) {
2471 * Process control. The control connection is prioritised so we don't
2472 * starve it with high throughout put tracing data on the data
2475 for (i
= 0; i
< nb_fd
; i
++) {
2476 /* Fetch once the poll data */
2477 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2478 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2480 health_code_update();
2482 /* Thread quit pipe has been closed. Killing thread. */
2483 ret
= check_thread_quit_pipe(pollfd
, revents
);
2489 /* Inspect the relay cmd pipe for new connection */
2490 if (pollfd
== relay_cmd_pipe
[0]) {
2491 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2492 ERR("Relay pipe error");
2494 } else if (revents
& LPOLLIN
) {
2495 DBG("Relay command received");
2496 ret
= relay_add_connection(relay_cmd_pipe
[0],
2497 &events
, relay_connections_ht
);
2502 } else if (revents
) {
2504 lttng_ht_lookup(relay_connections_ht
,
2505 (void *)((unsigned long) pollfd
),
2507 node
= lttng_ht_iter_get_node_ulong(&iter
);
2509 DBG2("Relay sock %d not found", pollfd
);
2513 relay_connection
= caa_container_of(node
,
2514 struct relay_command
, sock_n
);
2516 if (revents
& (LPOLLERR
)) {
2518 relay_cleanup_poll_connection(&events
, pollfd
);
2519 relay_del_connection(relay_connections_ht
,
2520 &iter
, relay_connection
, sessions_ht
);
2521 if (last_seen_data_fd
== pollfd
) {
2522 last_seen_data_fd
= last_notdel_data_fd
;
2524 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2525 DBG("Socket %d hung up", pollfd
);
2526 relay_cleanup_poll_connection(&events
, pollfd
);
2527 relay_del_connection(relay_connections_ht
,
2528 &iter
, relay_connection
, sessions_ht
);
2529 if (last_seen_data_fd
== pollfd
) {
2530 last_seen_data_fd
= last_notdel_data_fd
;
2532 } else if (revents
& LPOLLIN
) {
2533 /* control socket */
2534 if (relay_connection
->type
== RELAY_CONTROL
) {
2535 ret
= relay_connection
->sock
->ops
->recvmsg(
2536 relay_connection
->sock
, &recv_hdr
,
2537 sizeof(struct lttcomm_relayd_hdr
), 0);
2538 /* connection closed */
2540 relay_cleanup_poll_connection(&events
, pollfd
);
2541 relay_del_connection(relay_connections_ht
,
2542 &iter
, relay_connection
, sessions_ht
);
2543 DBG("Control connection closed with %d", pollfd
);
2545 if (relay_connection
->session
) {
2546 DBG2("Relay worker receiving data for session : %" PRIu64
,
2547 relay_connection
->session
->id
);
2549 ret
= relay_process_control(&recv_hdr
,
2550 relay_connection
, relay_ctx
);
2552 /* Clear the session on error. */
2553 relay_cleanup_poll_connection(&events
, pollfd
);
2554 relay_del_connection(relay_connections_ht
,
2555 &iter
, relay_connection
, sessions_ht
);
2556 DBG("Connection closed with %d", pollfd
);
2562 * Flag the last seen data fd not deleted. It will be
2563 * used as the last seen fd if any fd gets deleted in
2566 last_notdel_data_fd
= pollfd
;
2574 * The last loop handled a control request, go back to poll to make
2575 * sure we prioritise the control socket.
2581 if (last_seen_data_fd
>= 0) {
2582 for (i
= 0; i
< nb_fd
; i
++) {
2583 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2585 health_code_update();
2587 if (last_seen_data_fd
== pollfd
) {
2594 /* Process data connection. */
2595 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2596 /* Fetch the poll data. */
2597 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2598 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2600 health_code_update();
2602 /* Skip the command pipe. It's handled in the first loop. */
2603 if (pollfd
== relay_cmd_pipe
[0]) {
2609 lttng_ht_lookup(relay_connections_ht
,
2610 (void *)((unsigned long) pollfd
),
2612 node
= lttng_ht_iter_get_node_ulong(&iter
);
2614 /* Skip it. Might be removed before. */
2618 relay_connection
= caa_container_of(node
,
2619 struct relay_command
, sock_n
);
2621 if (revents
& LPOLLIN
) {
2622 if (relay_connection
->type
!= RELAY_DATA
) {
2626 ret
= relay_process_data(relay_connection
);
2627 /* connection closed */
2629 relay_cleanup_poll_connection(&events
, pollfd
);
2630 relay_del_connection(relay_connections_ht
,
2631 &iter
, relay_connection
, sessions_ht
);
2632 DBG("Data connection closed with %d", pollfd
);
2634 * Every goto restart call sets the last seen fd where
2635 * here we don't really care since we gracefully
2636 * continue the loop after the connection is deleted.
2639 /* Keep last seen port. */
2640 last_seen_data_fd
= pollfd
;
2648 last_seen_data_fd
= -1;
2651 /* Normal exit, no error */
2656 lttng_poll_clean(&events
);
2658 /* empty the hash table and free the memory */
2660 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2661 health_code_update();
2663 node
= lttng_ht_iter_get_node_ulong(&iter
);
2665 relay_connection
= caa_container_of(node
,
2666 struct relay_command
, sock_n
);
2667 relay_del_connection(relay_connections_ht
,
2668 &iter
, relay_connection
, sessions_ht
);
2673 lttng_ht_destroy(indexes_ht
);
2675 lttng_ht_destroy(relay_connections_ht
);
2676 relay_connections_ht_error
:
2677 /* Close relay cmd pipes */
2678 utils_close_pipe(relay_cmd_pipe
);
2680 DBG("Thread exited with error");
2682 DBG("Worker thread cleanup complete");
2686 ERR("Health error occurred in %s", __func__
);
2688 health_unregister(health_relayd
);
2689 rcu_unregister_thread();
2695 * Create the relay command pipe to wake thread_manage_apps.
2696 * Closed in cleanup().
2698 static int create_relay_cmd_pipe(void)
2702 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2710 int main(int argc
, char **argv
)
2714 struct relay_local_data
*relay_ctx
;
2716 /* Parse arguments */
2718 if ((ret
= parse_args(argc
, argv
)) < 0) {
2722 if ((ret
= set_signal_handler()) < 0) {
2726 /* Try to create directory if -o, --output is specified. */
2727 if (opt_output_path
) {
2728 if (*opt_output_path
!= '/') {
2729 ERR("Please specify an absolute path for -o, --output PATH");
2733 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2735 ERR("Unable to create %s", opt_output_path
);
2741 if (opt_daemon
|| opt_background
) {
2744 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2751 * We are in the child. Make sure all other file
2752 * descriptors are closed, in case we are called with
2753 * more opened file descriptors than the standard ones.
2755 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2760 /* Create thread quit pipe */
2761 if ((ret
= init_thread_quit_pipe()) < 0) {
2765 /* We need those values for the file/dir creation. */
2766 relayd_uid
= getuid();
2767 relayd_gid
= getgid();
2769 /* Check if daemon is UID = 0 */
2770 if (relayd_uid
== 0) {
2771 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2772 live_uri
->port
< 1024) {
2773 ERR("Need to be root to use ports < 1024");
2779 /* Setup the thread apps communication pipe. */
2780 if ((ret
= create_relay_cmd_pipe()) < 0) {
2784 /* Init relay command queue. */
2785 cds_wfq_init(&relay_cmd_queue
.queue
);
2787 /* Set up max poll set size */
2788 lttng_poll_set_max_size();
2790 /* Initialize communication library */
2792 lttcomm_inet_init();
2794 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2796 PERROR("relay_ctx");
2800 /* tables of sessions indexed by session ID */
2801 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2802 if (!relay_ctx
->sessions_ht
) {
2803 goto exit_relay_ctx_sessions
;
2806 /* tables of streams indexed by stream ID */
2807 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2808 if (!relay_streams_ht
) {
2809 goto exit_relay_ctx_streams
;
2812 /* tables of streams indexed by stream ID */
2813 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2814 if (!viewer_streams_ht
) {
2815 goto exit_relay_ctx_viewer_streams
;
2818 /* Initialize thread health monitoring */
2819 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2820 if (!health_relayd
) {
2821 PERROR("health_app_create error");
2822 goto exit_health_app_create
;
2825 ret
= utils_create_pipe(health_quit_pipe
);
2827 goto error_health_pipe
;
2830 /* Create thread to manage the client socket */
2831 ret
= pthread_create(&health_thread
, NULL
,
2832 thread_manage_health
, (void *) NULL
);
2834 PERROR("pthread_create health");
2838 /* Setup the dispatcher thread */
2839 ret
= pthread_create(&dispatcher_thread
, NULL
,
2840 relay_thread_dispatcher
, (void *) NULL
);
2842 PERROR("pthread_create dispatcher");
2843 goto exit_dispatcher
;
2846 /* Setup the worker thread */
2847 ret
= pthread_create(&worker_thread
, NULL
,
2848 relay_thread_worker
, (void *) relay_ctx
);
2850 PERROR("pthread_create worker");
2854 /* Setup the listener thread */
2855 ret
= pthread_create(&listener_thread
, NULL
,
2856 relay_thread_listener
, (void *) NULL
);
2858 PERROR("pthread_create listener");
2862 ret
= live_start_threads(live_uri
, relay_ctx
);
2864 ERR("Starting live viewer threads");
2869 ret
= pthread_join(listener_thread
, &status
);
2871 PERROR("pthread_join");
2872 goto error
; /* join error, exit without cleanup */
2876 ret
= pthread_join(worker_thread
, &status
);
2878 PERROR("pthread_join");
2879 goto error
; /* join error, exit without cleanup */
2883 ret
= pthread_join(dispatcher_thread
, &status
);
2885 PERROR("pthread_join");
2886 goto error
; /* join error, exit without cleanup */
2890 ret
= pthread_join(health_thread
, &status
);
2892 PERROR("pthread_join health thread");
2893 goto error
; /* join error, exit without cleanup */
2897 * Stop live threads only after joining other threads.
2899 live_stop_threads();
2902 utils_close_pipe(health_quit_pipe
);
2905 health_app_destroy(health_relayd
);
2907 exit_health_app_create
:
2908 lttng_ht_destroy(viewer_streams_ht
);
2910 exit_relay_ctx_viewer_streams
:
2911 lttng_ht_destroy(relay_streams_ht
);
2913 exit_relay_ctx_streams
:
2914 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2916 exit_relay_ctx_sessions
: