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"
62 #include "testpoint.h"
64 /* command line options */
65 char *opt_output_path
;
66 static int opt_daemon
, opt_background
;
69 * We need to wait for listener and live listener threads, as well as
70 * health check thread, before being ready to signal readiness.
72 #define NR_LTTNG_RELAY_READY 3
73 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
74 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
75 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
77 static struct lttng_uri
*control_uri
;
78 static struct lttng_uri
*data_uri
;
79 static struct lttng_uri
*live_uri
;
83 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
86 * Quit pipe for all threads. This permits a single cancellation point
87 * for all threads when receiving an event on the pipe.
89 int thread_quit_pipe
[2] = { -1, -1 };
92 * This pipe is used to inform the worker thread that a command is queued and
93 * ready to be processed.
95 static int relay_cmd_pipe
[2] = { -1, -1 };
97 /* Shared between threads */
98 static int dispatch_thread_exit
;
100 static pthread_t listener_thread
;
101 static pthread_t dispatcher_thread
;
102 static pthread_t worker_thread
;
103 static pthread_t health_thread
;
105 static uint64_t last_relay_stream_id
;
106 static uint64_t last_relay_session_id
;
109 * Relay command queue.
111 * The relay_thread_listener and relay_thread_dispatcher communicate with this
114 static struct relay_cmd_queue relay_cmd_queue
;
116 /* buffer allocated at startup, used to store the trace data */
117 static char *data_buffer
;
118 static unsigned int data_buffer_size
;
120 /* We need those values for the file/dir creation. */
121 static uid_t relayd_uid
;
122 static gid_t relayd_gid
;
124 /* Global relay stream hash table. */
125 struct lttng_ht
*relay_streams_ht
;
127 /* Global relay viewer stream hash table. */
128 struct lttng_ht
*viewer_streams_ht
;
130 /* Global hash table that stores relay index object. */
131 struct lttng_ht
*indexes_ht
;
133 /* Relayd health monitoring */
134 struct health_app
*health_relayd
;
137 * usage function on stderr
142 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
143 fprintf(stderr
, " -h, --help Display this usage.\n");
144 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
145 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
146 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
147 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
148 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
149 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
150 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
151 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
155 int parse_args(int argc
, char **argv
)
159 char *default_address
;
161 static struct option long_options
[] = {
162 { "control-port", 1, 0, 'C', },
163 { "data-port", 1, 0, 'D', },
164 { "daemonize", 0, 0, 'd', },
165 { "group", 1, 0, 'g', },
166 { "help", 0, 0, 'h', },
167 { "output", 1, 0, 'o', },
168 { "verbose", 0, 0, 'v', },
169 { "background", 0, 0, 'b' },
174 int option_index
= 0;
175 c
= getopt_long(argc
, argv
, "dhv" "C:D:L:o:g:b",
176 long_options
, &option_index
);
183 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
185 fprintf(stderr
, " with arg %s\n", optarg
);
189 ret
= uri_parse(optarg
, &control_uri
);
191 ERR("Invalid control URI specified");
194 if (control_uri
->port
== 0) {
195 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
199 ret
= uri_parse(optarg
, &data_uri
);
201 ERR("Invalid data URI specified");
204 if (data_uri
->port
== 0) {
205 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
209 ret
= uri_parse(optarg
, &live_uri
);
211 ERR("Invalid live URI specified");
214 if (live_uri
->port
== 0) {
215 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
225 tracing_group_name
= optarg
;
231 ret
= asprintf(&opt_output_path
, "%s", optarg
);
233 PERROR("asprintf opt_output_path");
238 /* Verbose level can increase using multiple -v */
239 lttng_opt_verbose
+= 1;
242 /* Unknown option or other error.
243 * Error is printed by getopt, just return */
249 /* assign default values */
250 if (control_uri
== NULL
) {
251 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
252 DEFAULT_NETWORK_CONTROL_PORT
);
254 PERROR("asprintf default data address");
258 ret
= uri_parse(default_address
, &control_uri
);
259 free(default_address
);
261 ERR("Invalid control URI specified");
265 if (data_uri
== NULL
) {
266 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
267 DEFAULT_NETWORK_DATA_PORT
);
269 PERROR("asprintf default data address");
273 ret
= uri_parse(default_address
, &data_uri
);
274 free(default_address
);
276 ERR("Invalid data URI specified");
280 if (live_uri
== NULL
) {
281 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
282 DEFAULT_NETWORK_VIEWER_PORT
);
284 PERROR("asprintf default viewer control address");
288 ret
= uri_parse(default_address
, &live_uri
);
289 free(default_address
);
291 ERR("Invalid viewer control URI specified");
308 /* free the dynamically allocated opt_output_path */
309 free(opt_output_path
);
311 /* Close thread quit pipes */
312 utils_close_pipe(thread_quit_pipe
);
314 uri_free(control_uri
);
316 /* Live URI is freed in the live thread. */
320 * Write to writable pipe used to notify a thread.
323 int notify_thread_pipe(int wpipe
)
327 ret
= lttng_write(wpipe
, "!", 1);
329 PERROR("write poll pipe");
335 static void notify_health_quit_pipe(int *pipe
)
339 ret
= lttng_write(pipe
[1], "4", 1);
341 PERROR("write relay health quit");
346 * Stop all threads by closing the thread quit pipe.
349 void stop_threads(void)
353 /* Stopping all threads */
354 DBG("Terminating all threads");
355 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
357 ERR("write error on thread quit pipe");
360 notify_health_quit_pipe(health_quit_pipe
);
362 /* Dispatch thread */
363 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
364 futex_nto1_wake(&relay_cmd_queue
.futex
);
368 * Signal handler for the daemon
370 * Simply stop all worker threads, leaving main() return gracefully after
371 * joining all threads and calling cleanup().
374 void sighandler(int sig
)
378 DBG("SIGPIPE caught");
381 DBG("SIGINT caught");
385 DBG("SIGTERM caught");
389 CMM_STORE_SHARED(recv_child_signal
, 1);
397 * Setup signal handler for :
398 * SIGINT, SIGTERM, SIGPIPE
401 int set_signal_handler(void)
407 if ((ret
= sigemptyset(&sigset
)) < 0) {
408 PERROR("sigemptyset");
412 sa
.sa_handler
= sighandler
;
415 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
420 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
425 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
430 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
435 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
440 void lttng_relay_notify_ready(void)
442 /* Notify the parent of the fork() process that we are ready. */
443 if (opt_daemon
|| opt_background
) {
444 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
445 kill(child_ppid
, SIGUSR1
);
451 * Init thread quit pipe.
453 * Return -1 on error or 0 if all pipes are created.
456 int init_thread_quit_pipe(void)
460 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
466 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
469 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
473 if (events
== NULL
|| size
== 0) {
478 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
484 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
496 * Check if the thread quit pipe was triggered.
498 * Return 1 if it was triggered else 0;
501 int check_thread_quit_pipe(int fd
, uint32_t events
)
503 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
511 * Create and init socket from uri.
514 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
517 struct lttcomm_sock
*sock
= NULL
;
519 sock
= lttcomm_alloc_sock_from_uri(uri
);
521 ERR("Allocating socket");
525 ret
= lttcomm_create_sock(sock
);
529 DBG("Listening on sock %d", sock
->fd
);
531 ret
= sock
->ops
->bind(sock
);
536 ret
= sock
->ops
->listen(sock
, -1);
546 lttcomm_destroy_sock(sock
);
552 * Return nonzero if stream needs to be closed.
555 int close_stream_check(struct relay_stream
*stream
)
558 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
560 * We are about to close the stream so set the data pending flag to 1
561 * which will make the end data pending command skip the stream which
562 * is now closed and ready. Note that after proceeding to a file close,
563 * the written file is ready for reading.
565 stream
->data_pending_check_done
= 1;
572 * This thread manages the listening for new connections on the network
575 void *relay_thread_listener(void *data
)
577 int i
, ret
, pollfd
, err
= -1;
579 uint32_t revents
, nb_fd
;
580 struct lttng_poll_event events
;
581 struct lttcomm_sock
*control_sock
, *data_sock
;
583 DBG("[thread] Relay listener started");
585 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
587 health_code_update();
589 control_sock
= relay_init_sock(control_uri
);
591 goto error_sock_control
;
594 data_sock
= relay_init_sock(data_uri
);
596 goto error_sock_relay
;
600 * Pass 3 as size here for the thread quit pipe, control and data socket.
602 ret
= create_thread_poll_set(&events
, 3);
604 goto error_create_poll
;
607 /* Add the control socket */
608 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
613 /* Add the data socket */
614 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
619 lttng_relay_notify_ready();
621 if (testpoint(relayd_thread_listener
)) {
622 goto error_testpoint
;
626 health_code_update();
628 DBG("Listener accepting connections");
632 ret
= lttng_poll_wait(&events
, -1);
636 * Restart interrupted system call.
638 if (errno
== EINTR
) {
646 DBG("Relay new connection received");
647 for (i
= 0; i
< nb_fd
; i
++) {
648 health_code_update();
650 /* Fetch once the poll data */
651 revents
= LTTNG_POLL_GETEV(&events
, i
);
652 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
654 /* Thread quit pipe has been closed. Killing thread. */
655 ret
= check_thread_quit_pipe(pollfd
, revents
);
661 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
662 ERR("socket poll error");
664 } else if (revents
& LPOLLIN
) {
666 * Get allocated in this thread,
667 * enqueued to a global queue, dequeued
668 * and freed in the worker thread.
670 struct relay_command
*relay_cmd
;
671 struct lttcomm_sock
*newsock
;
673 relay_cmd
= zmalloc(sizeof(struct relay_command
));
674 if (relay_cmd
== NULL
) {
675 PERROR("relay command zmalloc");
679 if (pollfd
== data_sock
->fd
) {
680 newsock
= data_sock
->ops
->accept(data_sock
);
682 PERROR("accepting data sock");
686 relay_cmd
->type
= RELAY_DATA
;
687 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
689 assert(pollfd
== control_sock
->fd
);
690 newsock
= control_sock
->ops
->accept(control_sock
);
692 PERROR("accepting control sock");
696 relay_cmd
->type
= RELAY_CONTROL
;
697 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
699 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
702 PERROR("setsockopt inet");
703 lttcomm_destroy_sock(newsock
);
707 relay_cmd
->sock
= newsock
;
709 * Lock free enqueue the request.
711 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
714 * Wake the dispatch queue futex. Implicit memory
715 * barrier with the exchange in cds_wfq_enqueue.
717 futex_nto1_wake(&relay_cmd_queue
.futex
);
726 lttng_poll_clean(&events
);
728 if (data_sock
->fd
>= 0) {
729 ret
= data_sock
->ops
->close(data_sock
);
734 lttcomm_destroy_sock(data_sock
);
736 if (control_sock
->fd
>= 0) {
737 ret
= control_sock
->ops
->close(control_sock
);
742 lttcomm_destroy_sock(control_sock
);
746 ERR("Health error occurred in %s", __func__
);
748 health_unregister(health_relayd
);
749 DBG("Relay listener thread cleanup complete");
755 * This thread manages the dispatching of the requests to worker threads
758 void *relay_thread_dispatcher(void *data
)
762 struct cds_wfq_node
*node
;
763 struct relay_command
*relay_cmd
= NULL
;
765 DBG("[thread] Relay dispatcher started");
767 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
769 if (testpoint(relayd_thread_dispatcher
)) {
770 goto error_testpoint
;
773 health_code_update();
775 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
776 health_code_update();
778 /* Atomically prepare the queue futex */
779 futex_nto1_prepare(&relay_cmd_queue
.futex
);
782 health_code_update();
784 /* Dequeue commands */
785 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
787 DBG("Woken up but nothing in the relay command queue");
788 /* Continue thread execution */
792 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
793 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
796 * Inform worker thread of the new request. This
797 * call is blocking so we can be assured that the data will be read
798 * at some point in time or wait to the end of the world :)
800 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
801 sizeof(struct relay_command
));
803 if (ret
< sizeof(struct relay_command
)) {
804 PERROR("write cmd pipe");
807 } while (node
!= NULL
);
809 /* Futex wait on queue. Blocking call on futex() */
811 futex_nto1_wait(&relay_cmd_queue
.futex
);
815 /* Normal exit, no error */
822 ERR("Health error occurred in %s", __func__
);
824 health_unregister(health_relayd
);
825 DBG("Dispatch thread dying");
831 * Get stream from stream id.
832 * Need to be called with RCU read-side lock held.
834 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
836 struct lttng_ht_node_ulong
*node
;
837 struct lttng_ht_iter iter
;
838 struct relay_stream
*ret
;
840 lttng_ht_lookup(relay_streams_ht
,
841 (void *)((unsigned long) stream_id
),
843 node
= lttng_ht_iter_get_node_ulong(&iter
);
845 DBG("Relay stream %" PRIu64
" not found", stream_id
);
850 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
857 void deferred_free_stream(struct rcu_head
*head
)
859 struct relay_stream
*stream
=
860 caa_container_of(head
, struct relay_stream
, rcu_node
);
862 free(stream
->path_name
);
863 free(stream
->channel_name
);
868 void deferred_free_session(struct rcu_head
*head
)
870 struct relay_session
*session
=
871 caa_container_of(head
, struct relay_session
, rcu_node
);
876 * Close a given stream. The stream is freed using a call RCU.
878 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
879 * BEFORE the stream lock MUST be acquired.
881 static void destroy_stream(struct relay_stream
*stream
)
884 struct relay_viewer_stream
*vstream
;
885 struct lttng_ht_iter iter
;
889 delret
= close(stream
->fd
);
891 PERROR("close stream");
894 if (stream
->index_fd
>= 0) {
895 delret
= close(stream
->index_fd
);
897 PERROR("close stream index_fd");
901 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
904 * Set the last good value into the viewer stream. This is done
905 * right before the stream gets deleted from the hash table. The
906 * lookup failure on the live thread side of a stream indicates
907 * that the viewer stream index received value should be used.
909 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
910 vstream
->total_index_received
= stream
->total_index_received
;
911 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
912 vstream
->close_write_flag
= 1;
913 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
916 /* Cleanup index of that stream. */
917 relay_index_destroy_by_stream_id(stream
->stream_handle
);
919 iter
.iter
.node
= &stream
->stream_n
.node
;
920 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
922 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
923 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
926 if (stream
->ctf_trace
) {
927 ctf_trace_try_destroy(stream
->ctf_trace
);
930 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
931 DBG("Closed tracefile %d from close stream", stream
->fd
);
935 * relay_delete_session: Free all memory associated with a session and
939 void relay_delete_session(struct relay_command
*cmd
,
940 struct lttng_ht
*sessions_ht
)
942 struct lttng_ht_iter iter
;
943 struct lttng_ht_node_ulong
*node
;
944 struct relay_stream
*stream
;
951 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
954 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
955 node
= lttng_ht_iter_get_node_ulong(&iter
);
959 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
960 if (stream
->session
== cmd
->session
) {
961 destroy_stream(stream
);
962 cmd
->session
->stream_count
--;
963 assert(cmd
->session
->stream_count
>= 0);
967 /* Make this session not visible anymore. */
968 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
969 ret
= lttng_ht_del(sessions_ht
, &iter
);
971 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
976 * Copy index data from the control port to a given index object.
978 static void copy_index_control_data(struct relay_index
*index
,
979 struct lttcomm_relayd_index
*data
)
985 * The index on disk is encoded in big endian, so we don't need to convert
986 * the data received on the network. The data_offset value is NEVER
987 * modified here and is updated by the data thread.
989 index
->index_data
.packet_size
= data
->packet_size
;
990 index
->index_data
.content_size
= data
->content_size
;
991 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
992 index
->index_data
.timestamp_end
= data
->timestamp_end
;
993 index
->index_data
.events_discarded
= data
->events_discarded
;
994 index
->index_data
.stream_id
= data
->stream_id
;
998 * Handle the RELAYD_CREATE_SESSION command.
1000 * On success, send back the session id or else return a negative value.
1003 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1004 struct relay_command
*cmd
,
1005 struct lttng_ht
*sessions_ht
)
1007 int ret
= 0, send_ret
;
1008 struct relay_session
*session
;
1009 struct lttcomm_relayd_status_session reply
;
1014 memset(&reply
, 0, sizeof(reply
));
1016 session
= zmalloc(sizeof(struct relay_session
));
1017 if (session
== NULL
) {
1018 PERROR("relay session zmalloc");
1023 session
->id
= ++last_relay_session_id
;
1024 session
->sock
= cmd
->sock
;
1025 session
->minor
= cmd
->minor
;
1026 session
->major
= cmd
->major
;
1027 cmd
->session
= session
;
1029 reply
.session_id
= htobe64(session
->id
);
1031 switch (cmd
->minor
) {
1036 case 4: /* LTTng sessiond 2.4 */
1038 ret
= cmd_create_session_2_4(cmd
, session
);
1042 lttng_ht_node_init_ulong(&session
->session_n
,
1043 (unsigned long) session
->id
);
1044 lttng_ht_add_unique_ulong(sessions_ht
,
1045 &session
->session_n
);
1047 DBG("Created session %" PRIu64
, session
->id
);
1051 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1053 reply
.ret_code
= htobe32(LTTNG_OK
);
1056 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1058 ERR("Relayd sending session id");
1066 * When we have received all the streams and the metadata for a channel,
1067 * we make them visible to the viewer threads.
1070 void set_viewer_ready_flag(struct relay_command
*cmd
)
1072 struct relay_stream_recv_handle
*node
, *tmp_node
;
1074 cds_list_for_each_entry_safe(node
, tmp_node
, &cmd
->recv_head
, node
) {
1075 struct relay_stream
*stream
;
1078 stream
= relay_stream_find_by_id(node
->id
);
1081 * Stream is most probably being cleaned up by the data thread thus
1082 * simply continue to the next one.
1088 stream
->viewer_ready
= 1;
1091 /* Clean stream handle node. */
1092 cds_list_del(&node
->node
);
1100 * Add a recv handle node to the connection recv list with the given stream
1101 * handle. A new node is allocated thus must be freed when the node is deleted
1104 static void queue_stream_handle(uint64_t handle
, struct relay_command
*cmd
)
1106 struct relay_stream_recv_handle
*node
;
1110 node
= zmalloc(sizeof(*node
));
1112 PERROR("zmalloc queue stream handle");
1117 cds_list_add(&node
->node
, &cmd
->recv_head
);
1121 * relay_add_stream: allocate a new stream for a session
1124 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1125 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1127 struct relay_session
*session
= cmd
->session
;
1128 struct relay_stream
*stream
= NULL
;
1129 struct lttcomm_relayd_status_stream reply
;
1132 if (!session
|| cmd
->version_check_done
== 0) {
1133 ERR("Trying to add a stream before version check");
1135 goto end_no_session
;
1138 stream
= zmalloc(sizeof(struct relay_stream
));
1139 if (stream
== NULL
) {
1140 PERROR("relay stream zmalloc");
1142 goto end_no_session
;
1145 switch (cmd
->minor
) {
1146 case 1: /* LTTng sessiond 2.1 */
1147 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1149 case 2: /* LTTng sessiond 2.2 */
1151 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1155 goto err_free_stream
;
1159 stream
->stream_handle
= ++last_relay_stream_id
;
1160 stream
->prev_seq
= -1ULL;
1161 stream
->session
= session
;
1162 stream
->index_fd
= -1;
1163 stream
->read_index_fd
= -1;
1164 stream
->ctf_trace
= NULL
;
1165 pthread_mutex_init(&stream
->lock
, NULL
);
1167 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1169 ERR("relay creating output directory");
1174 * No need to use run_as API here because whatever we receives, the relayd
1175 * uses its own credentials for the stream files.
1177 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1178 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1180 ERR("Create output file");
1184 if (stream
->tracefile_size
) {
1185 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1187 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1190 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1191 stream
->metadata_flag
= 1;
1193 * When we receive a new metadata stream, we create a new
1194 * ctf_trace and we assign this ctf_trace to all streams with
1197 * If later on we receive a new stream for the same ctf_trace,
1198 * we copy the information from the first hit in the HT to the
1201 stream
->ctf_trace
= ctf_trace_create();
1202 if (!stream
->ctf_trace
) {
1206 stream
->ctf_trace
->refcount
++;
1207 stream
->ctf_trace
->metadata_stream
= stream
;
1209 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1210 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1213 * Add the stream handle in the recv list of the connection. Once the end
1214 * stream message is received, this list is emptied and streams are set
1215 * with the viewer ready flag.
1217 if (stream
->metadata_flag
) {
1218 stream
->viewer_ready
= 1;
1220 queue_stream_handle(stream
->stream_handle
, cmd
);
1223 lttng_ht_node_init_ulong(&stream
->stream_n
,
1224 (unsigned long) stream
->stream_handle
);
1225 lttng_ht_add_unique_ulong(relay_streams_ht
,
1228 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1229 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1230 session
->stream_count
++;
1232 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1233 stream
->stream_handle
);
1236 reply
.handle
= htobe64(stream
->stream_handle
);
1237 /* send the session id to the client or a negative return code on error */
1239 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1240 /* stream was not properly added to the ht, so free it */
1243 reply
.ret_code
= htobe32(LTTNG_OK
);
1246 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1247 sizeof(struct lttcomm_relayd_status_stream
), 0);
1249 ERR("Relay sending stream id");
1258 free(stream
->path_name
);
1259 free(stream
->channel_name
);
1265 * relay_close_stream: close a specific stream
1268 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1269 struct relay_command
*cmd
)
1272 struct relay_session
*session
= cmd
->session
;
1273 struct lttcomm_relayd_close_stream stream_info
;
1274 struct lttcomm_relayd_generic_reply reply
;
1275 struct relay_stream
*stream
;
1277 DBG("Close stream received");
1279 if (!session
|| cmd
->version_check_done
== 0) {
1280 ERR("Trying to close a stream before version check");
1282 goto end_no_session
;
1285 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1286 sizeof(struct lttcomm_relayd_close_stream
), 0);
1287 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1289 /* Orderly shutdown. Not necessary to print an error. */
1290 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1292 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1295 goto end_no_session
;
1299 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1305 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1306 stream
->close_flag
= 1;
1307 session
->stream_count
--;
1308 assert(session
->stream_count
>= 0);
1310 if (close_stream_check(stream
)) {
1311 destroy_stream(stream
);
1318 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1320 reply
.ret_code
= htobe32(LTTNG_OK
);
1322 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1323 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1325 ERR("Relay sending stream id");
1334 * relay_unknown_command: send -1 if received unknown command
1337 void relay_unknown_command(struct relay_command
*cmd
)
1339 struct lttcomm_relayd_generic_reply reply
;
1342 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1343 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1344 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1346 ERR("Relay sending unknown command");
1351 * relay_start: send an acknowledgment to the client to tell if we are
1352 * ready to receive data. We are ready if a session is established.
1355 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1356 struct relay_command
*cmd
)
1358 int ret
= htobe32(LTTNG_OK
);
1359 struct lttcomm_relayd_generic_reply reply
;
1360 struct relay_session
*session
= cmd
->session
;
1363 DBG("Trying to start the streaming without a session established");
1364 ret
= htobe32(LTTNG_ERR_UNK
);
1367 reply
.ret_code
= ret
;
1368 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1369 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1371 ERR("Relay sending start ack");
1378 * Append padding to the file pointed by the file descriptor fd.
1380 static int write_padding_to_file(int fd
, uint32_t size
)
1389 zeros
= zmalloc(size
);
1390 if (zeros
== NULL
) {
1391 PERROR("zmalloc zeros for padding");
1396 ret
= lttng_write(fd
, zeros
, size
);
1398 PERROR("write padding to file");
1408 * relay_recv_metadata: receive the metada for the session.
1411 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1412 struct relay_command
*cmd
)
1414 int ret
= htobe32(LTTNG_OK
);
1416 struct relay_session
*session
= cmd
->session
;
1417 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1418 struct relay_stream
*metadata_stream
;
1419 uint64_t data_size
, payload_size
;
1422 ERR("Metadata sent before version check");
1427 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1428 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1429 ERR("Incorrect data size");
1433 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1435 if (data_buffer_size
< data_size
) {
1436 /* In case the realloc fails, we can free the memory */
1439 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1440 if (!tmp_data_ptr
) {
1441 ERR("Allocating data buffer");
1446 data_buffer
= tmp_data_ptr
;
1447 data_buffer_size
= data_size
;
1449 memset(data_buffer
, 0, data_size
);
1450 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1451 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1452 if (ret
< 0 || ret
!= data_size
) {
1454 /* Orderly shutdown. Not necessary to print an error. */
1455 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1457 ERR("Relay didn't receive the whole metadata");
1462 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1465 metadata_stream
= relay_stream_find_by_id(
1466 be64toh(metadata_struct
->stream_id
));
1467 if (!metadata_stream
) {
1472 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1474 if (size_ret
< payload_size
) {
1475 ERR("Relay error writing metadata on file");
1480 ret
= write_padding_to_file(metadata_stream
->fd
,
1481 be32toh(metadata_struct
->padding_size
));
1485 metadata_stream
->ctf_trace
->metadata_received
+=
1486 payload_size
+ be32toh(metadata_struct
->padding_size
);
1488 DBG2("Relay metadata written");
1497 * relay_send_version: send relayd version number
1500 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1501 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1504 struct lttcomm_relayd_version reply
, msg
;
1508 cmd
->version_check_done
= 1;
1510 /* Get version from the other side. */
1511 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1512 if (ret
< 0 || ret
!= sizeof(msg
)) {
1514 /* Orderly shutdown. Not necessary to print an error. */
1515 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1517 ERR("Relay failed to receive the version values.");
1523 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1524 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1526 /* Major versions must be the same */
1527 if (reply
.major
!= be32toh(msg
.major
)) {
1528 DBG("Incompatible major versions (%u vs %u), deleting session",
1529 reply
.major
, be32toh(msg
.major
));
1530 relay_delete_session(cmd
, sessions_ht
);
1535 cmd
->major
= reply
.major
;
1536 /* We adapt to the lowest compatible version */
1537 if (reply
.minor
<= be32toh(msg
.minor
)) {
1538 cmd
->minor
= reply
.minor
;
1540 cmd
->minor
= be32toh(msg
.minor
);
1543 reply
.major
= htobe32(reply
.major
);
1544 reply
.minor
= htobe32(reply
.minor
);
1545 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1546 sizeof(struct lttcomm_relayd_version
), 0);
1548 ERR("Relay sending version");
1551 DBG("Version check done using protocol %u.%u", cmd
->major
,
1559 * Check for data pending for a given stream id from the session daemon.
1562 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1563 struct relay_command
*cmd
)
1565 struct relay_session
*session
= cmd
->session
;
1566 struct lttcomm_relayd_data_pending msg
;
1567 struct lttcomm_relayd_generic_reply reply
;
1568 struct relay_stream
*stream
;
1570 uint64_t last_net_seq_num
, stream_id
;
1572 DBG("Data pending command received");
1574 if (!session
|| cmd
->version_check_done
== 0) {
1575 ERR("Trying to check for data before version check");
1577 goto end_no_session
;
1580 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1581 if (ret
< sizeof(msg
)) {
1583 /* Orderly shutdown. Not necessary to print an error. */
1584 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1586 ERR("Relay didn't receive valid data_pending struct size : %d",
1590 goto end_no_session
;
1593 stream_id
= be64toh(msg
.stream_id
);
1594 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1597 stream
= relay_stream_find_by_id(stream_id
);
1598 if (stream
== NULL
) {
1603 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1604 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1607 /* Avoid wrapping issue */
1608 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1609 /* Data has in fact been written and is NOT pending */
1612 /* Data still being streamed thus pending */
1616 /* Pending check is now done. */
1617 stream
->data_pending_check_done
= 1;
1622 reply
.ret_code
= htobe32(ret
);
1623 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1625 ERR("Relay data pending ret code failed");
1633 * Wait for the control socket to reach a quiescent state.
1635 * Note that for now, when receiving this command from the session daemon, this
1636 * means that every subsequent commands or data received on the control socket
1637 * has been handled. So, this is why we simply return OK here.
1640 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1641 struct relay_command
*cmd
)
1645 struct relay_stream
*stream
;
1646 struct lttng_ht_iter iter
;
1647 struct lttcomm_relayd_quiescent_control msg
;
1648 struct lttcomm_relayd_generic_reply reply
;
1650 DBG("Checking quiescent state on control socket");
1652 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1653 ERR("Trying to check for data before version check");
1655 goto end_no_session
;
1658 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1659 if (ret
< sizeof(msg
)) {
1661 /* Orderly shutdown. Not necessary to print an error. */
1662 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1664 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1668 goto end_no_session
;
1671 stream_id
= be64toh(msg
.stream_id
);
1674 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1676 if (stream
->stream_handle
== stream_id
) {
1677 stream
->data_pending_check_done
= 1;
1678 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1685 reply
.ret_code
= htobe32(LTTNG_OK
);
1686 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1688 ERR("Relay data quiescent control ret code failed");
1696 * Initialize a data pending command. This means that a client is about to ask
1697 * for data pending for each stream he/she holds. Simply iterate over all
1698 * streams of a session and set the data_pending_check_done flag.
1700 * This command returns to the client a LTTNG_OK code.
1703 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1704 struct relay_command
*cmd
)
1707 struct lttng_ht_iter iter
;
1708 struct lttcomm_relayd_begin_data_pending msg
;
1709 struct lttcomm_relayd_generic_reply reply
;
1710 struct relay_stream
*stream
;
1711 uint64_t session_id
;
1716 DBG("Init streams for data pending");
1718 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1719 ERR("Trying to check for data before version check");
1721 goto end_no_session
;
1724 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1725 if (ret
< sizeof(msg
)) {
1727 /* Orderly shutdown. Not necessary to print an error. */
1728 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1730 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1734 goto end_no_session
;
1737 session_id
= be64toh(msg
.session_id
);
1740 * Iterate over all streams to set the begin data pending flag. For now, the
1741 * streams are indexed by stream handle so we have to iterate over all
1742 * streams to find the one associated with the right session_id.
1745 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1747 if (stream
->session
->id
== session_id
) {
1748 stream
->data_pending_check_done
= 0;
1749 DBG("Set begin data pending flag to stream %" PRIu64
,
1750 stream
->stream_handle
);
1755 /* All good, send back reply. */
1756 reply
.ret_code
= htobe32(LTTNG_OK
);
1758 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1760 ERR("Relay begin data pending send reply failed");
1768 * End data pending command. This will check, for a given session id, if each
1769 * stream associated with it has its data_pending_check_done flag set. If not,
1770 * this means that the client lost track of the stream but the data is still
1771 * being streamed on our side. In this case, we inform the client that data is
1774 * Return to the client if there is data in flight or not with a ret_code.
1777 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1778 struct relay_command
*cmd
)
1781 struct lttng_ht_iter iter
;
1782 struct lttcomm_relayd_end_data_pending msg
;
1783 struct lttcomm_relayd_generic_reply reply
;
1784 struct relay_stream
*stream
;
1785 uint64_t session_id
;
1786 uint32_t is_data_inflight
= 0;
1791 DBG("End data pending command");
1793 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1794 ERR("Trying to check for data before version check");
1796 goto end_no_session
;
1799 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1800 if (ret
< sizeof(msg
)) {
1802 /* Orderly shutdown. Not necessary to print an error. */
1803 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1805 ERR("Relay didn't receive valid end data_pending struct size: %d",
1809 goto end_no_session
;
1812 session_id
= be64toh(msg
.session_id
);
1814 /* Iterate over all streams to see if the begin data pending flag is set. */
1816 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1818 if (stream
->session
->id
== session_id
&&
1819 !stream
->data_pending_check_done
) {
1820 is_data_inflight
= 1;
1821 DBG("Data is still in flight for stream %" PRIu64
,
1822 stream
->stream_handle
);
1828 /* All good, send back reply. */
1829 reply
.ret_code
= htobe32(is_data_inflight
);
1831 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1833 ERR("Relay end data pending send reply failed");
1841 * Receive an index for a specific stream.
1843 * Return 0 on success else a negative value.
1846 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1847 struct relay_command
*cmd
)
1849 int ret
, send_ret
, index_created
= 0;
1850 struct relay_session
*session
= cmd
->session
;
1851 struct lttcomm_relayd_index index_info
;
1852 struct relay_index
*index
, *wr_index
= NULL
;
1853 struct lttcomm_relayd_generic_reply reply
;
1854 struct relay_stream
*stream
;
1855 uint64_t net_seq_num
;
1859 DBG("Relay receiving index");
1861 if (!session
|| cmd
->version_check_done
== 0) {
1862 ERR("Trying to close a stream before version check");
1864 goto end_no_session
;
1867 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1868 sizeof(index_info
), 0);
1869 if (ret
< sizeof(index_info
)) {
1871 /* Orderly shutdown. Not necessary to print an error. */
1872 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1874 ERR("Relay didn't receive valid index struct size : %d", ret
);
1877 goto end_no_session
;
1880 net_seq_num
= be64toh(index_info
.net_seq_num
);
1883 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1886 goto end_rcu_unlock
;
1889 /* Live beacon handling */
1890 if (index_info
.packet_size
== 0) {
1891 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1894 * Only flag a stream inactive when it has already received data.
1896 if (stream
->total_index_received
> 0) {
1897 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1900 goto end_rcu_unlock
;
1902 stream
->beacon_ts_end
= -1ULL;
1905 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1907 /* A successful creation will add the object to the HT. */
1908 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1910 goto end_rcu_unlock
;
1915 copy_index_control_data(index
, &index_info
);
1917 if (index_created
) {
1919 * Try to add the relay index object to the hash table. If an object
1920 * already exist, destroy back the index created, set the data in this
1921 * object and write it on disk.
1923 relay_index_add(index
, &wr_index
);
1925 copy_index_control_data(wr_index
, &index_info
);
1929 /* The index already exists so write it on disk. */
1933 /* Do we have a writable ready index to write on disk. */
1935 /* Starting at 2.4, create the index file if none available. */
1936 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1937 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1938 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1939 stream
->tracefile_count_current
);
1941 goto end_rcu_unlock
;
1943 stream
->index_fd
= ret
;
1946 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1948 goto end_rcu_unlock
;
1950 stream
->total_index_received
++;
1957 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1959 reply
.ret_code
= htobe32(LTTNG_OK
);
1961 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1963 ERR("Relay sending close index id reply");
1972 * Receive the streams_sent message.
1974 * Return 0 on success else a negative value.
1977 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
1978 struct relay_command
*cmd
)
1981 struct lttcomm_relayd_generic_reply reply
;
1985 DBG("Relay receiving streams_sent");
1987 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1988 ERR("Trying to close a stream before version check");
1990 goto end_no_session
;
1994 * Flag every pending stream in the connection recv list that they are
1995 * ready to be used by the viewer.
1997 set_viewer_ready_flag(cmd
);
1999 reply
.ret_code
= htobe32(LTTNG_OK
);
2000 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2002 ERR("Relay sending sent_stream reply");
2014 * Process the commands received on the control socket
2017 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2018 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
2022 switch (be32toh(recv_hdr
->cmd
)) {
2023 case RELAYD_CREATE_SESSION
:
2024 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
2026 case RELAYD_ADD_STREAM
:
2027 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
2029 case RELAYD_START_DATA
:
2030 ret
= relay_start(recv_hdr
, cmd
);
2032 case RELAYD_SEND_METADATA
:
2033 ret
= relay_recv_metadata(recv_hdr
, cmd
);
2035 case RELAYD_VERSION
:
2036 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
2038 case RELAYD_CLOSE_STREAM
:
2039 ret
= relay_close_stream(recv_hdr
, cmd
);
2041 case RELAYD_DATA_PENDING
:
2042 ret
= relay_data_pending(recv_hdr
, cmd
);
2044 case RELAYD_QUIESCENT_CONTROL
:
2045 ret
= relay_quiescent_control(recv_hdr
, cmd
);
2047 case RELAYD_BEGIN_DATA_PENDING
:
2048 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
2050 case RELAYD_END_DATA_PENDING
:
2051 ret
= relay_end_data_pending(recv_hdr
, cmd
);
2053 case RELAYD_SEND_INDEX
:
2054 ret
= relay_recv_index(recv_hdr
, cmd
);
2056 case RELAYD_STREAMS_SENT
:
2057 ret
= relay_streams_sent(recv_hdr
, cmd
);
2059 case RELAYD_UPDATE_SYNC_INFO
:
2061 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2062 relay_unknown_command(cmd
);
2072 * Handle index for a data stream.
2074 * RCU read side lock MUST be acquired.
2076 * Return 0 on success else a negative value.
2078 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2081 int ret
= 0, index_created
= 0;
2082 uint64_t stream_id
, data_offset
;
2083 struct relay_index
*index
, *wr_index
= NULL
;
2087 stream_id
= stream
->stream_handle
;
2088 /* Get data offset because we are about to update the index. */
2089 data_offset
= htobe64(stream
->tracefile_size_current
);
2092 * Lookup for an existing index for that stream id/sequence number. If on
2093 * exists, the control thread already received the data for it thus we need
2094 * to write it on disk.
2096 index
= relay_index_find(stream_id
, net_seq_num
);
2098 /* A successful creation will add the object to the HT. */
2099 index
= relay_index_create(stream_id
, net_seq_num
);
2107 if (rotate_index
|| stream
->index_fd
< 0) {
2108 index
->to_close_fd
= stream
->index_fd
;
2109 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2110 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2111 stream
->tracefile_count_current
);
2113 /* This will close the stream's index fd if one. */
2114 relay_index_free_safe(index
);
2117 stream
->index_fd
= ret
;
2119 index
->fd
= stream
->index_fd
;
2120 index
->index_data
.offset
= data_offset
;
2122 if (index_created
) {
2124 * Try to add the relay index object to the hash table. If an object
2125 * already exist, destroy back the index created and set the data.
2127 relay_index_add(index
, &wr_index
);
2129 /* Copy back data from the created index. */
2130 wr_index
->fd
= index
->fd
;
2131 wr_index
->to_close_fd
= index
->to_close_fd
;
2132 wr_index
->index_data
.offset
= data_offset
;
2136 /* The index already exists so write it on disk. */
2140 /* Do we have a writable ready index to write on disk. */
2142 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2146 stream
->total_index_received
++;
2154 * relay_process_data: Process the data received on the data socket
2157 int relay_process_data(struct relay_command
*cmd
)
2159 int ret
= 0, rotate_index
= 0;
2161 struct relay_stream
*stream
;
2162 struct lttcomm_relayd_data_hdr data_hdr
;
2164 uint64_t net_seq_num
;
2167 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2168 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2171 /* Orderly shutdown. Not necessary to print an error. */
2172 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2174 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2180 stream_id
= be64toh(data_hdr
.stream_id
);
2183 stream
= relay_stream_find_by_id(stream_id
);
2186 goto end_rcu_unlock
;
2189 data_size
= be32toh(data_hdr
.data_size
);
2190 if (data_buffer_size
< data_size
) {
2193 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2194 if (!tmp_data_ptr
) {
2195 ERR("Allocating data buffer");
2198 goto end_rcu_unlock
;
2200 data_buffer
= tmp_data_ptr
;
2201 data_buffer_size
= data_size
;
2203 memset(data_buffer
, 0, data_size
);
2205 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2207 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2208 data_size
, stream_id
, net_seq_num
);
2209 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2212 /* Orderly shutdown. Not necessary to print an error. */
2213 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2216 goto end_rcu_unlock
;
2219 /* Check if a rotation is needed. */
2220 if (stream
->tracefile_size
> 0 &&
2221 (stream
->tracefile_size_current
+ data_size
) >
2222 stream
->tracefile_size
) {
2223 struct relay_viewer_stream
*vstream
;
2226 new_id
= (stream
->tracefile_count_current
+ 1) %
2227 stream
->tracefile_count
;
2229 * When we wrap-around back to 0, we start overwriting old
2232 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2233 stream
->tracefile_overwrite
= 1;
2235 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2236 if (stream
->tracefile_overwrite
) {
2237 stream
->oldest_tracefile_id
=
2238 (stream
->oldest_tracefile_id
+ 1) %
2239 stream
->tracefile_count
;
2241 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
2244 * The viewer is reading a file about to be
2245 * overwritten. Close the FDs it is
2246 * currently using and let it handle the fault.
2248 if (vstream
->tracefile_count_current
== new_id
) {
2249 pthread_mutex_lock(&vstream
->overwrite_lock
);
2250 vstream
->abort_flag
= 1;
2251 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2252 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2253 stream
->channel_name
, new_id
);
2254 } else if (vstream
->tracefile_count_current
==
2255 stream
->tracefile_count_current
) {
2257 * The reader and writer were in the
2258 * same trace file, inform the viewer
2259 * that no new index will ever be added
2262 vstream
->close_write_flag
= 1;
2265 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2266 stream
->tracefile_size
, stream
->tracefile_count
,
2267 relayd_uid
, relayd_gid
, stream
->fd
,
2268 &(stream
->tracefile_count_current
), &stream
->fd
);
2269 stream
->total_index_received
= 0;
2270 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2272 ERR("Rotating stream output file");
2273 goto end_rcu_unlock
;
2275 /* Reset current size because we just perform a stream rotation. */
2276 stream
->tracefile_size_current
= 0;
2281 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2282 * index are NOT supported.
2284 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2285 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2287 goto end_rcu_unlock
;
2291 /* Write data to stream output fd. */
2292 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2293 if (size_ret
< data_size
) {
2294 ERR("Relay error writing data to file");
2296 goto end_rcu_unlock
;
2299 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2300 ret
, stream
->stream_handle
);
2302 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2304 goto end_rcu_unlock
;
2306 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2308 stream
->prev_seq
= net_seq_num
;
2310 /* Check if we need to close the FD */
2311 if (close_stream_check(stream
)) {
2312 destroy_stream(stream
);
2322 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2326 lttng_poll_del(events
, pollfd
);
2328 ret
= close(pollfd
);
2330 ERR("Closing pollfd %d", pollfd
);
2335 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2336 struct lttng_ht
*relay_connections_ht
)
2338 struct relay_command
*relay_connection
;
2341 relay_connection
= zmalloc(sizeof(struct relay_command
));
2342 if (relay_connection
== NULL
) {
2343 PERROR("Relay command zmalloc");
2346 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2347 if (ret
< sizeof(struct relay_command
)) {
2348 PERROR("read relay cmd pipe");
2351 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2354 * Only used by the control side and the reference is copied inside each
2355 * stream from that connection. Thus a destroy HT must be done after every
2356 * stream has been destroyed.
2358 if (relay_connection
->type
== RELAY_CONTROL
) {
2359 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2360 LTTNG_HT_TYPE_STRING
);
2361 if (!relay_connection
->ctf_traces_ht
) {
2366 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2367 (unsigned long) relay_connection
->sock
->fd
);
2369 lttng_ht_add_unique_ulong(relay_connections_ht
,
2370 &relay_connection
->sock_n
);
2372 return lttng_poll_add(events
,
2373 relay_connection
->sock
->fd
,
2374 LPOLLIN
| LPOLLRDHUP
);
2377 free(relay_connection
);
2383 void deferred_free_connection(struct rcu_head
*head
)
2385 struct relay_command
*relay_connection
=
2386 caa_container_of(head
, struct relay_command
, rcu_node
);
2388 lttcomm_destroy_sock(relay_connection
->sock
);
2389 free(relay_connection
);
2393 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2394 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2395 struct lttng_ht
*sessions_ht
)
2399 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2402 if (relay_connection
->type
== RELAY_CONTROL
) {
2403 struct relay_stream_recv_handle
*node
, *tmp_node
;
2405 relay_delete_session(relay_connection
, sessions_ht
);
2406 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2408 /* Clean up recv list. */
2409 cds_list_for_each_entry_safe(node
, tmp_node
,
2410 &relay_connection
->recv_head
, node
) {
2411 cds_list_del(&node
->node
);
2416 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2420 * This thread does the actual work
2423 void *relay_thread_worker(void *data
)
2425 int ret
, err
= -1, last_seen_data_fd
= -1;
2427 struct relay_command
*relay_connection
;
2428 struct lttng_poll_event events
;
2429 struct lttng_ht
*relay_connections_ht
;
2430 struct lttng_ht_node_ulong
*node
;
2431 struct lttng_ht_iter iter
;
2432 struct lttcomm_relayd_hdr recv_hdr
;
2433 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2434 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2436 DBG("[thread] Relay worker started");
2438 rcu_register_thread();
2440 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2442 if (testpoint(relayd_thread_worker
)) {
2443 goto error_testpoint
;
2446 health_code_update();
2448 /* table of connections indexed on socket */
2449 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2450 if (!relay_connections_ht
) {
2451 goto relay_connections_ht_error
;
2454 /* Tables of received indexes indexed by index handle and net_seq_num. */
2455 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2457 goto indexes_ht_error
;
2460 ret
= create_thread_poll_set(&events
, 2);
2462 goto error_poll_create
;
2465 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2472 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2474 health_code_update();
2476 /* Infinite blocking call, waiting for transmission */
2477 DBG3("Relayd worker thread polling...");
2478 health_poll_entry();
2479 ret
= lttng_poll_wait(&events
, -1);
2483 * Restart interrupted system call.
2485 if (errno
== EINTR
) {
2494 * Process control. The control connection is prioritised so we don't
2495 * starve it with high throughout put tracing data on the data
2498 for (i
= 0; i
< nb_fd
; i
++) {
2499 /* Fetch once the poll data */
2500 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2501 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2503 health_code_update();
2505 /* Thread quit pipe has been closed. Killing thread. */
2506 ret
= check_thread_quit_pipe(pollfd
, revents
);
2512 /* Inspect the relay cmd pipe for new connection */
2513 if (pollfd
== relay_cmd_pipe
[0]) {
2514 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2515 ERR("Relay pipe error");
2517 } else if (revents
& LPOLLIN
) {
2518 DBG("Relay command received");
2519 ret
= relay_add_connection(relay_cmd_pipe
[0],
2520 &events
, relay_connections_ht
);
2525 } else if (revents
) {
2527 lttng_ht_lookup(relay_connections_ht
,
2528 (void *)((unsigned long) pollfd
),
2530 node
= lttng_ht_iter_get_node_ulong(&iter
);
2532 DBG2("Relay sock %d not found", pollfd
);
2536 relay_connection
= caa_container_of(node
,
2537 struct relay_command
, sock_n
);
2539 if (revents
& (LPOLLERR
)) {
2541 relay_cleanup_poll_connection(&events
, pollfd
);
2542 relay_del_connection(relay_connections_ht
,
2543 &iter
, relay_connection
, sessions_ht
);
2544 if (last_seen_data_fd
== pollfd
) {
2545 last_seen_data_fd
= last_notdel_data_fd
;
2547 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2548 DBG("Socket %d hung up", pollfd
);
2549 relay_cleanup_poll_connection(&events
, pollfd
);
2550 relay_del_connection(relay_connections_ht
,
2551 &iter
, relay_connection
, sessions_ht
);
2552 if (last_seen_data_fd
== pollfd
) {
2553 last_seen_data_fd
= last_notdel_data_fd
;
2555 } else if (revents
& LPOLLIN
) {
2556 /* control socket */
2557 if (relay_connection
->type
== RELAY_CONTROL
) {
2558 ret
= relay_connection
->sock
->ops
->recvmsg(
2559 relay_connection
->sock
, &recv_hdr
,
2560 sizeof(struct lttcomm_relayd_hdr
), 0);
2561 /* connection closed */
2563 relay_cleanup_poll_connection(&events
, pollfd
);
2564 relay_del_connection(relay_connections_ht
,
2565 &iter
, relay_connection
, sessions_ht
);
2566 DBG("Control connection closed with %d", pollfd
);
2568 if (relay_connection
->session
) {
2569 DBG2("Relay worker receiving data for session : %" PRIu64
,
2570 relay_connection
->session
->id
);
2572 ret
= relay_process_control(&recv_hdr
,
2573 relay_connection
, relay_ctx
);
2575 /* Clear the session on error. */
2576 relay_cleanup_poll_connection(&events
, pollfd
);
2577 relay_del_connection(relay_connections_ht
,
2578 &iter
, relay_connection
, sessions_ht
);
2579 DBG("Connection closed with %d", pollfd
);
2585 * Flag the last seen data fd not deleted. It will be
2586 * used as the last seen fd if any fd gets deleted in
2589 last_notdel_data_fd
= pollfd
;
2597 * The last loop handled a control request, go back to poll to make
2598 * sure we prioritise the control socket.
2604 if (last_seen_data_fd
>= 0) {
2605 for (i
= 0; i
< nb_fd
; i
++) {
2606 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2608 health_code_update();
2610 if (last_seen_data_fd
== pollfd
) {
2617 /* Process data connection. */
2618 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2619 /* Fetch the poll data. */
2620 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2621 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2623 health_code_update();
2625 /* Skip the command pipe. It's handled in the first loop. */
2626 if (pollfd
== relay_cmd_pipe
[0]) {
2632 lttng_ht_lookup(relay_connections_ht
,
2633 (void *)((unsigned long) pollfd
),
2635 node
= lttng_ht_iter_get_node_ulong(&iter
);
2637 /* Skip it. Might be removed before. */
2641 relay_connection
= caa_container_of(node
,
2642 struct relay_command
, sock_n
);
2644 if (revents
& LPOLLIN
) {
2645 if (relay_connection
->type
!= RELAY_DATA
) {
2649 ret
= relay_process_data(relay_connection
);
2650 /* connection closed */
2652 relay_cleanup_poll_connection(&events
, pollfd
);
2653 relay_del_connection(relay_connections_ht
,
2654 &iter
, relay_connection
, sessions_ht
);
2655 DBG("Data connection closed with %d", pollfd
);
2657 * Every goto restart call sets the last seen fd where
2658 * here we don't really care since we gracefully
2659 * continue the loop after the connection is deleted.
2662 /* Keep last seen port. */
2663 last_seen_data_fd
= pollfd
;
2671 last_seen_data_fd
= -1;
2674 /* Normal exit, no error */
2679 lttng_poll_clean(&events
);
2681 /* empty the hash table and free the memory */
2683 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2684 health_code_update();
2686 node
= lttng_ht_iter_get_node_ulong(&iter
);
2688 relay_connection
= caa_container_of(node
,
2689 struct relay_command
, sock_n
);
2690 relay_del_connection(relay_connections_ht
,
2691 &iter
, relay_connection
, sessions_ht
);
2696 lttng_ht_destroy(indexes_ht
);
2698 lttng_ht_destroy(relay_connections_ht
);
2699 relay_connections_ht_error
:
2700 /* Close relay cmd pipes */
2701 utils_close_pipe(relay_cmd_pipe
);
2703 DBG("Thread exited with error");
2705 DBG("Worker thread cleanup complete");
2710 ERR("Health error occurred in %s", __func__
);
2712 health_unregister(health_relayd
);
2713 rcu_unregister_thread();
2719 * Create the relay command pipe to wake thread_manage_apps.
2720 * Closed in cleanup().
2722 static int create_relay_cmd_pipe(void)
2726 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2734 int main(int argc
, char **argv
)
2738 struct relay_local_data
*relay_ctx
;
2740 /* Parse arguments */
2742 if ((ret
= parse_args(argc
, argv
)) < 0) {
2746 if ((ret
= set_signal_handler()) < 0) {
2750 /* Try to create directory if -o, --output is specified. */
2751 if (opt_output_path
) {
2752 if (*opt_output_path
!= '/') {
2753 ERR("Please specify an absolute path for -o, --output PATH");
2757 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2759 ERR("Unable to create %s", opt_output_path
);
2765 if (opt_daemon
|| opt_background
) {
2768 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2775 * We are in the child. Make sure all other file
2776 * descriptors are closed, in case we are called with
2777 * more opened file descriptors than the standard ones.
2779 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2784 /* Create thread quit pipe */
2785 if ((ret
= init_thread_quit_pipe()) < 0) {
2789 /* We need those values for the file/dir creation. */
2790 relayd_uid
= getuid();
2791 relayd_gid
= getgid();
2793 /* Check if daemon is UID = 0 */
2794 if (relayd_uid
== 0) {
2795 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2796 live_uri
->port
< 1024) {
2797 ERR("Need to be root to use ports < 1024");
2803 /* Setup the thread apps communication pipe. */
2804 if ((ret
= create_relay_cmd_pipe()) < 0) {
2808 /* Init relay command queue. */
2809 cds_wfq_init(&relay_cmd_queue
.queue
);
2811 /* Set up max poll set size */
2812 lttng_poll_set_max_size();
2814 /* Initialize communication library */
2816 lttcomm_inet_init();
2818 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2820 PERROR("relay_ctx");
2824 /* tables of sessions indexed by session ID */
2825 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2826 if (!relay_ctx
->sessions_ht
) {
2827 goto exit_relay_ctx_sessions
;
2830 /* tables of streams indexed by stream ID */
2831 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2832 if (!relay_streams_ht
) {
2833 goto exit_relay_ctx_streams
;
2836 /* tables of streams indexed by stream ID */
2837 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2838 if (!viewer_streams_ht
) {
2839 goto exit_relay_ctx_viewer_streams
;
2842 /* Initialize thread health monitoring */
2843 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2844 if (!health_relayd
) {
2845 PERROR("health_app_create error");
2846 goto exit_health_app_create
;
2849 ret
= utils_create_pipe(health_quit_pipe
);
2851 goto error_health_pipe
;
2854 /* Create thread to manage the client socket */
2855 ret
= pthread_create(&health_thread
, NULL
,
2856 thread_manage_health
, (void *) NULL
);
2858 PERROR("pthread_create health");
2862 /* Setup the dispatcher thread */
2863 ret
= pthread_create(&dispatcher_thread
, NULL
,
2864 relay_thread_dispatcher
, (void *) NULL
);
2866 PERROR("pthread_create dispatcher");
2867 goto exit_dispatcher
;
2870 /* Setup the worker thread */
2871 ret
= pthread_create(&worker_thread
, NULL
,
2872 relay_thread_worker
, (void *) relay_ctx
);
2874 PERROR("pthread_create worker");
2878 /* Setup the listener thread */
2879 ret
= pthread_create(&listener_thread
, NULL
,
2880 relay_thread_listener
, (void *) NULL
);
2882 PERROR("pthread_create listener");
2886 ret
= live_start_threads(live_uri
, relay_ctx
);
2888 ERR("Starting live viewer threads");
2893 ret
= pthread_join(listener_thread
, &status
);
2895 PERROR("pthread_join");
2896 goto error
; /* join error, exit without cleanup */
2900 ret
= pthread_join(worker_thread
, &status
);
2902 PERROR("pthread_join");
2903 goto error
; /* join error, exit without cleanup */
2907 ret
= pthread_join(dispatcher_thread
, &status
);
2909 PERROR("pthread_join");
2910 goto error
; /* join error, exit without cleanup */
2914 ret
= pthread_join(health_thread
, &status
);
2916 PERROR("pthread_join health thread");
2917 goto error
; /* join error, exit without cleanup */
2921 * Stop live threads only after joining other threads.
2923 live_stop_threads();
2926 utils_close_pipe(health_quit_pipe
);
2929 health_app_destroy(health_relayd
);
2931 exit_health_app_create
:
2932 lttng_ht_destroy(viewer_streams_ht
);
2934 exit_relay_ctx_viewer_streams
:
2935 lttng_ht_destroy(relay_streams_ht
);
2937 exit_relay_ctx_streams
:
2938 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2940 exit_relay_ctx_sessions
: