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', },
173 int option_index
= 0;
174 c
= getopt_long(argc
, argv
, "dhv" "C:D:L:o:g:",
175 long_options
, &option_index
);
182 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
184 fprintf(stderr
, " with arg %s\n", optarg
);
188 ret
= uri_parse(optarg
, &control_uri
);
190 ERR("Invalid control URI specified");
193 if (control_uri
->port
== 0) {
194 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
198 ret
= uri_parse(optarg
, &data_uri
);
200 ERR("Invalid data URI specified");
203 if (data_uri
->port
== 0) {
204 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
208 ret
= uri_parse(optarg
, &live_uri
);
210 ERR("Invalid live URI specified");
213 if (live_uri
->port
== 0) {
214 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
221 tracing_group_name
= optarg
;
227 ret
= asprintf(&opt_output_path
, "%s", optarg
);
229 PERROR("asprintf opt_output_path");
234 /* Verbose level can increase using multiple -v */
235 lttng_opt_verbose
+= 1;
238 /* Unknown option or other error.
239 * Error is printed by getopt, just return */
245 /* assign default values */
246 if (control_uri
== NULL
) {
247 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
248 DEFAULT_NETWORK_CONTROL_PORT
);
250 PERROR("asprintf default data address");
254 ret
= uri_parse(default_address
, &control_uri
);
255 free(default_address
);
257 ERR("Invalid control URI specified");
261 if (data_uri
== NULL
) {
262 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
263 DEFAULT_NETWORK_DATA_PORT
);
265 PERROR("asprintf default data address");
269 ret
= uri_parse(default_address
, &data_uri
);
270 free(default_address
);
272 ERR("Invalid data URI specified");
276 if (live_uri
== NULL
) {
277 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
278 DEFAULT_NETWORK_VIEWER_PORT
);
280 PERROR("asprintf default viewer control address");
284 ret
= uri_parse(default_address
, &live_uri
);
285 free(default_address
);
287 ERR("Invalid viewer control URI specified");
304 /* free the dynamically allocated opt_output_path */
305 free(opt_output_path
);
307 /* Close thread quit pipes */
308 utils_close_pipe(thread_quit_pipe
);
310 uri_free(control_uri
);
312 /* Live URI is freed in the live thread. */
316 * Write to writable pipe used to notify a thread.
319 int notify_thread_pipe(int wpipe
)
323 ret
= lttng_write(wpipe
, "!", 1);
325 PERROR("write poll pipe");
331 static void notify_health_quit_pipe(int *pipe
)
335 ret
= lttng_write(pipe
[1], "4", 1);
337 PERROR("write relay health quit");
342 * Stop all threads by closing the thread quit pipe.
345 void stop_threads(void)
349 /* Stopping all threads */
350 DBG("Terminating all threads");
351 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
353 ERR("write error on thread quit pipe");
356 notify_health_quit_pipe(health_quit_pipe
);
358 /* Dispatch thread */
359 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
360 futex_nto1_wake(&relay_cmd_queue
.futex
);
364 * Signal handler for the daemon
366 * Simply stop all worker threads, leaving main() return gracefully after
367 * joining all threads and calling cleanup().
370 void sighandler(int sig
)
374 DBG("SIGPIPE caught");
377 DBG("SIGINT caught");
381 DBG("SIGTERM caught");
385 CMM_STORE_SHARED(recv_child_signal
, 1);
393 * Setup signal handler for :
394 * SIGINT, SIGTERM, SIGPIPE
397 int set_signal_handler(void)
403 if ((ret
= sigemptyset(&sigset
)) < 0) {
404 PERROR("sigemptyset");
408 sa
.sa_handler
= sighandler
;
411 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
416 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
421 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
426 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
431 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
436 void lttng_relay_notify_ready(void)
438 /* Notify the parent of the fork() process that we are ready. */
439 if (opt_daemon
|| opt_background
) {
440 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
441 kill(child_ppid
, SIGUSR1
);
447 * Init thread quit pipe.
449 * Return -1 on error or 0 if all pipes are created.
452 int init_thread_quit_pipe(void)
456 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
462 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
465 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
469 if (events
== NULL
|| size
== 0) {
474 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
480 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
492 * Check if the thread quit pipe was triggered.
494 * Return 1 if it was triggered else 0;
497 int check_thread_quit_pipe(int fd
, uint32_t events
)
499 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
507 * Create and init socket from uri.
510 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
513 struct lttcomm_sock
*sock
= NULL
;
515 sock
= lttcomm_alloc_sock_from_uri(uri
);
517 ERR("Allocating socket");
521 ret
= lttcomm_create_sock(sock
);
525 DBG("Listening on sock %d", sock
->fd
);
527 ret
= sock
->ops
->bind(sock
);
532 ret
= sock
->ops
->listen(sock
, -1);
542 lttcomm_destroy_sock(sock
);
548 * Return nonzero if stream needs to be closed.
551 int close_stream_check(struct relay_stream
*stream
)
554 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
556 * We are about to close the stream so set the data pending flag to 1
557 * which will make the end data pending command skip the stream which
558 * is now closed and ready. Note that after proceeding to a file close,
559 * the written file is ready for reading.
561 stream
->data_pending_check_done
= 1;
568 * This thread manages the listening for new connections on the network
571 void *relay_thread_listener(void *data
)
573 int i
, ret
, pollfd
, err
= -1;
575 uint32_t revents
, nb_fd
;
576 struct lttng_poll_event events
;
577 struct lttcomm_sock
*control_sock
, *data_sock
;
579 DBG("[thread] Relay listener started");
581 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
583 health_code_update();
585 control_sock
= relay_init_sock(control_uri
);
587 goto error_sock_control
;
590 data_sock
= relay_init_sock(data_uri
);
592 goto error_sock_relay
;
596 * Pass 3 as size here for the thread quit pipe, control and data socket.
598 ret
= create_thread_poll_set(&events
, 3);
600 goto error_create_poll
;
603 /* Add the control socket */
604 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
609 /* Add the data socket */
610 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
615 lttng_relay_notify_ready();
617 if (testpoint(relayd_thread_listener
)) {
618 goto error_testpoint
;
622 health_code_update();
624 DBG("Listener accepting connections");
628 ret
= lttng_poll_wait(&events
, -1);
632 * Restart interrupted system call.
634 if (errno
== EINTR
) {
642 DBG("Relay new connection received");
643 for (i
= 0; i
< nb_fd
; i
++) {
644 health_code_update();
646 /* Fetch once the poll data */
647 revents
= LTTNG_POLL_GETEV(&events
, i
);
648 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
650 /* Thread quit pipe has been closed. Killing thread. */
651 ret
= check_thread_quit_pipe(pollfd
, revents
);
657 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
658 ERR("socket poll error");
660 } else if (revents
& LPOLLIN
) {
662 * Get allocated in this thread,
663 * enqueued to a global queue, dequeued
664 * and freed in the worker thread.
666 struct relay_command
*relay_cmd
;
667 struct lttcomm_sock
*newsock
;
669 relay_cmd
= zmalloc(sizeof(struct relay_command
));
670 if (relay_cmd
== NULL
) {
671 PERROR("relay command zmalloc");
675 if (pollfd
== data_sock
->fd
) {
676 newsock
= data_sock
->ops
->accept(data_sock
);
678 PERROR("accepting data sock");
682 relay_cmd
->type
= RELAY_DATA
;
683 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
685 assert(pollfd
== control_sock
->fd
);
686 newsock
= control_sock
->ops
->accept(control_sock
);
688 PERROR("accepting control sock");
692 relay_cmd
->type
= RELAY_CONTROL
;
693 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
695 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
698 PERROR("setsockopt inet");
699 lttcomm_destroy_sock(newsock
);
703 relay_cmd
->sock
= newsock
;
705 * Lock free enqueue the request.
707 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
710 * Wake the dispatch queue futex. Implicit memory
711 * barrier with the exchange in cds_wfq_enqueue.
713 futex_nto1_wake(&relay_cmd_queue
.futex
);
722 lttng_poll_clean(&events
);
724 if (data_sock
->fd
>= 0) {
725 ret
= data_sock
->ops
->close(data_sock
);
730 lttcomm_destroy_sock(data_sock
);
732 if (control_sock
->fd
>= 0) {
733 ret
= control_sock
->ops
->close(control_sock
);
738 lttcomm_destroy_sock(control_sock
);
742 ERR("Health error occurred in %s", __func__
);
744 health_unregister(health_relayd
);
745 DBG("Relay listener thread cleanup complete");
751 * This thread manages the dispatching of the requests to worker threads
754 void *relay_thread_dispatcher(void *data
)
758 struct cds_wfq_node
*node
;
759 struct relay_command
*relay_cmd
= NULL
;
761 DBG("[thread] Relay dispatcher started");
763 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
765 if (testpoint(relayd_thread_dispatcher
)) {
766 goto error_testpoint
;
769 health_code_update();
771 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
772 health_code_update();
774 /* Atomically prepare the queue futex */
775 futex_nto1_prepare(&relay_cmd_queue
.futex
);
778 health_code_update();
780 /* Dequeue commands */
781 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
783 DBG("Woken up but nothing in the relay command queue");
784 /* Continue thread execution */
788 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
789 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
792 * Inform worker thread of the new request. This
793 * call is blocking so we can be assured that the data will be read
794 * at some point in time or wait to the end of the world :)
796 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
797 sizeof(struct relay_command
));
799 if (ret
< sizeof(struct relay_command
)) {
800 PERROR("write cmd pipe");
803 } while (node
!= NULL
);
805 /* Futex wait on queue. Blocking call on futex() */
807 futex_nto1_wait(&relay_cmd_queue
.futex
);
811 /* Normal exit, no error */
818 ERR("Health error occurred in %s", __func__
);
820 health_unregister(health_relayd
);
821 DBG("Dispatch thread dying");
827 * Get stream from stream id.
828 * Need to be called with RCU read-side lock held.
830 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
832 struct lttng_ht_node_ulong
*node
;
833 struct lttng_ht_iter iter
;
834 struct relay_stream
*ret
;
836 lttng_ht_lookup(relay_streams_ht
,
837 (void *)((unsigned long) stream_id
),
839 node
= lttng_ht_iter_get_node_ulong(&iter
);
841 DBG("Relay stream %" PRIu64
" not found", stream_id
);
846 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
853 void deferred_free_stream(struct rcu_head
*head
)
855 struct relay_stream
*stream
=
856 caa_container_of(head
, struct relay_stream
, rcu_node
);
858 free(stream
->path_name
);
859 free(stream
->channel_name
);
864 void deferred_free_session(struct rcu_head
*head
)
866 struct relay_session
*session
=
867 caa_container_of(head
, struct relay_session
, rcu_node
);
872 * Close a given stream. The stream is freed using a call RCU.
874 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
875 * BEFORE the stream lock MUST be acquired.
877 static void destroy_stream(struct relay_stream
*stream
)
880 struct relay_viewer_stream
*vstream
;
881 struct lttng_ht_iter iter
;
885 delret
= close(stream
->fd
);
887 PERROR("close stream");
890 if (stream
->index_fd
>= 0) {
891 delret
= close(stream
->index_fd
);
893 PERROR("close stream index_fd");
897 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
900 * Set the last good value into the viewer stream. This is done
901 * right before the stream gets deleted from the hash table. The
902 * lookup failure on the live thread side of a stream indicates
903 * that the viewer stream index received value should be used.
905 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
906 vstream
->total_index_received
= stream
->total_index_received
;
907 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
908 vstream
->close_write_flag
= 1;
909 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
912 /* Cleanup index of that stream. */
913 relay_index_destroy_by_stream_id(stream
->stream_handle
);
915 iter
.iter
.node
= &stream
->stream_n
.node
;
916 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
918 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
919 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
922 if (stream
->ctf_trace
) {
923 ctf_trace_try_destroy(stream
->ctf_trace
);
926 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
927 DBG("Closed tracefile %d from close stream", stream
->fd
);
931 * relay_delete_session: Free all memory associated with a session and
935 void relay_delete_session(struct relay_command
*cmd
,
936 struct lttng_ht
*sessions_ht
)
938 struct lttng_ht_iter iter
;
939 struct lttng_ht_node_ulong
*node
;
940 struct relay_stream
*stream
;
947 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
950 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
951 node
= lttng_ht_iter_get_node_ulong(&iter
);
955 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
956 if (stream
->session
== cmd
->session
) {
957 destroy_stream(stream
);
958 cmd
->session
->stream_count
--;
959 assert(cmd
->session
->stream_count
>= 0);
963 /* Make this session not visible anymore. */
964 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
965 ret
= lttng_ht_del(sessions_ht
, &iter
);
967 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
972 * Copy index data from the control port to a given index object.
974 static void copy_index_control_data(struct relay_index
*index
,
975 struct lttcomm_relayd_index
*data
)
981 * The index on disk is encoded in big endian, so we don't need to convert
982 * the data received on the network. The data_offset value is NEVER
983 * modified here and is updated by the data thread.
985 index
->index_data
.packet_size
= data
->packet_size
;
986 index
->index_data
.content_size
= data
->content_size
;
987 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
988 index
->index_data
.timestamp_end
= data
->timestamp_end
;
989 index
->index_data
.events_discarded
= data
->events_discarded
;
990 index
->index_data
.stream_id
= data
->stream_id
;
994 * Handle the RELAYD_CREATE_SESSION command.
996 * On success, send back the session id or else return a negative value.
999 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1000 struct relay_command
*cmd
,
1001 struct lttng_ht
*sessions_ht
)
1003 int ret
= 0, send_ret
;
1004 struct relay_session
*session
;
1005 struct lttcomm_relayd_status_session reply
;
1010 memset(&reply
, 0, sizeof(reply
));
1012 session
= zmalloc(sizeof(struct relay_session
));
1013 if (session
== NULL
) {
1014 PERROR("relay session zmalloc");
1019 session
->id
= ++last_relay_session_id
;
1020 session
->sock
= cmd
->sock
;
1021 session
->minor
= cmd
->minor
;
1022 session
->major
= cmd
->major
;
1023 cmd
->session
= session
;
1025 reply
.session_id
= htobe64(session
->id
);
1027 switch (cmd
->minor
) {
1028 case 4: /* LTTng sessiond 2.4 */
1030 ret
= cmd_create_session_2_4(cmd
, session
);
1034 lttng_ht_node_init_ulong(&session
->session_n
,
1035 (unsigned long) session
->id
);
1036 lttng_ht_add_unique_ulong(sessions_ht
,
1037 &session
->session_n
);
1039 DBG("Created session %" PRIu64
, session
->id
);
1043 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1045 reply
.ret_code
= htobe32(LTTNG_OK
);
1048 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1050 ERR("Relayd sending session id");
1058 * When we have received all the streams and the metadata for a channel,
1059 * we make them visible to the viewer threads.
1062 void set_viewer_ready_flag(struct relay_command
*cmd
)
1064 struct relay_stream_recv_handle
*node
, *tmp_node
;
1066 cds_list_for_each_entry_safe(node
, tmp_node
, &cmd
->recv_head
, node
) {
1067 struct relay_stream
*stream
;
1070 stream
= relay_stream_find_by_id(node
->id
);
1073 * Stream is most probably being cleaned up by the data thread thus
1074 * simply continue to the next one.
1080 stream
->viewer_ready
= 1;
1083 /* Clean stream handle node. */
1084 cds_list_del(&node
->node
);
1092 * Add a recv handle node to the connection recv list with the given stream
1093 * handle. A new node is allocated thus must be freed when the node is deleted
1096 static void queue_stream_handle(uint64_t handle
, struct relay_command
*cmd
)
1098 struct relay_stream_recv_handle
*node
;
1102 node
= zmalloc(sizeof(*node
));
1104 PERROR("zmalloc queue stream handle");
1109 cds_list_add(&node
->node
, &cmd
->recv_head
);
1113 * relay_add_stream: allocate a new stream for a session
1116 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1117 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1119 struct relay_session
*session
= cmd
->session
;
1120 struct relay_stream
*stream
= NULL
;
1121 struct lttcomm_relayd_status_stream reply
;
1124 if (!session
|| cmd
->version_check_done
== 0) {
1125 ERR("Trying to add a stream before version check");
1127 goto end_no_session
;
1130 stream
= zmalloc(sizeof(struct relay_stream
));
1131 if (stream
== NULL
) {
1132 PERROR("relay stream zmalloc");
1134 goto end_no_session
;
1137 switch (cmd
->minor
) {
1138 case 1: /* LTTng sessiond 2.1 */
1139 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1141 case 2: /* LTTng sessiond 2.2 */
1143 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1147 goto err_free_stream
;
1151 stream
->stream_handle
= ++last_relay_stream_id
;
1152 stream
->prev_seq
= -1ULL;
1153 stream
->session
= session
;
1154 stream
->index_fd
= -1;
1155 stream
->read_index_fd
= -1;
1156 stream
->ctf_trace
= NULL
;
1157 pthread_mutex_init(&stream
->lock
, NULL
);
1159 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1161 ERR("relay creating output directory");
1166 * No need to use run_as API here because whatever we receives, the relayd
1167 * uses its own credentials for the stream files.
1169 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1170 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1172 ERR("Create output file");
1176 if (stream
->tracefile_size
) {
1177 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1179 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1182 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1183 stream
->metadata_flag
= 1;
1185 * When we receive a new metadata stream, we create a new
1186 * ctf_trace and we assign this ctf_trace to all streams with
1189 * If later on we receive a new stream for the same ctf_trace,
1190 * we copy the information from the first hit in the HT to the
1193 stream
->ctf_trace
= ctf_trace_create();
1194 if (!stream
->ctf_trace
) {
1198 stream
->ctf_trace
->refcount
++;
1199 stream
->ctf_trace
->metadata_stream
= stream
;
1201 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1202 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1205 * Add the stream handle in the recv list of the connection. Once the end
1206 * stream message is received, this list is emptied and streams are set
1207 * with the viewer ready flag.
1209 if (stream
->metadata_flag
) {
1210 stream
->viewer_ready
= 1;
1212 queue_stream_handle(stream
->stream_handle
, cmd
);
1215 lttng_ht_node_init_ulong(&stream
->stream_n
,
1216 (unsigned long) stream
->stream_handle
);
1217 lttng_ht_add_unique_ulong(relay_streams_ht
,
1220 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1221 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1222 session
->stream_count
++;
1224 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1225 stream
->stream_handle
);
1228 reply
.handle
= htobe64(stream
->stream_handle
);
1229 /* send the session id to the client or a negative return code on error */
1231 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1232 /* stream was not properly added to the ht, so free it */
1235 reply
.ret_code
= htobe32(LTTNG_OK
);
1238 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1239 sizeof(struct lttcomm_relayd_status_stream
), 0);
1241 ERR("Relay sending stream id");
1250 free(stream
->path_name
);
1251 free(stream
->channel_name
);
1257 * relay_close_stream: close a specific stream
1260 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1261 struct relay_command
*cmd
)
1264 struct relay_session
*session
= cmd
->session
;
1265 struct lttcomm_relayd_close_stream stream_info
;
1266 struct lttcomm_relayd_generic_reply reply
;
1267 struct relay_stream
*stream
;
1269 DBG("Close stream received");
1271 if (!session
|| cmd
->version_check_done
== 0) {
1272 ERR("Trying to close a stream before version check");
1274 goto end_no_session
;
1277 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1278 sizeof(struct lttcomm_relayd_close_stream
), 0);
1279 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1281 /* Orderly shutdown. Not necessary to print an error. */
1282 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1284 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1287 goto end_no_session
;
1291 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1297 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1298 stream
->close_flag
= 1;
1299 session
->stream_count
--;
1300 assert(session
->stream_count
>= 0);
1302 if (close_stream_check(stream
)) {
1303 destroy_stream(stream
);
1310 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1312 reply
.ret_code
= htobe32(LTTNG_OK
);
1314 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1315 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1317 ERR("Relay sending stream id");
1326 * relay_unknown_command: send -1 if received unknown command
1329 void relay_unknown_command(struct relay_command
*cmd
)
1331 struct lttcomm_relayd_generic_reply reply
;
1334 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1335 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1336 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1338 ERR("Relay sending unknown command");
1343 * relay_start: send an acknowledgment to the client to tell if we are
1344 * ready to receive data. We are ready if a session is established.
1347 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1348 struct relay_command
*cmd
)
1350 int ret
= htobe32(LTTNG_OK
);
1351 struct lttcomm_relayd_generic_reply reply
;
1352 struct relay_session
*session
= cmd
->session
;
1355 DBG("Trying to start the streaming without a session established");
1356 ret
= htobe32(LTTNG_ERR_UNK
);
1359 reply
.ret_code
= ret
;
1360 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1361 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1363 ERR("Relay sending start ack");
1370 * Append padding to the file pointed by the file descriptor fd.
1372 static int write_padding_to_file(int fd
, uint32_t size
)
1381 zeros
= zmalloc(size
);
1382 if (zeros
== NULL
) {
1383 PERROR("zmalloc zeros for padding");
1388 ret
= lttng_write(fd
, zeros
, size
);
1390 PERROR("write padding to file");
1400 * relay_recv_metadata: receive the metada for the session.
1403 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1404 struct relay_command
*cmd
)
1406 int ret
= htobe32(LTTNG_OK
);
1408 struct relay_session
*session
= cmd
->session
;
1409 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1410 struct relay_stream
*metadata_stream
;
1411 uint64_t data_size
, payload_size
;
1414 ERR("Metadata sent before version check");
1419 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1420 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1421 ERR("Incorrect data size");
1425 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1427 if (data_buffer_size
< data_size
) {
1428 /* In case the realloc fails, we can free the memory */
1431 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1432 if (!tmp_data_ptr
) {
1433 ERR("Allocating data buffer");
1438 data_buffer
= tmp_data_ptr
;
1439 data_buffer_size
= data_size
;
1441 memset(data_buffer
, 0, data_size
);
1442 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1443 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1444 if (ret
< 0 || ret
!= data_size
) {
1446 /* Orderly shutdown. Not necessary to print an error. */
1447 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1449 ERR("Relay didn't receive the whole metadata");
1454 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1457 metadata_stream
= relay_stream_find_by_id(
1458 be64toh(metadata_struct
->stream_id
));
1459 if (!metadata_stream
) {
1464 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1466 if (size_ret
< payload_size
) {
1467 ERR("Relay error writing metadata on file");
1472 ret
= write_padding_to_file(metadata_stream
->fd
,
1473 be32toh(metadata_struct
->padding_size
));
1477 metadata_stream
->ctf_trace
->metadata_received
+=
1478 payload_size
+ be32toh(metadata_struct
->padding_size
);
1480 DBG2("Relay metadata written");
1489 * relay_send_version: send relayd version number
1492 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1493 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1496 struct lttcomm_relayd_version reply
, msg
;
1500 cmd
->version_check_done
= 1;
1502 /* Get version from the other side. */
1503 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1504 if (ret
< 0 || ret
!= sizeof(msg
)) {
1506 /* Orderly shutdown. Not necessary to print an error. */
1507 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1509 ERR("Relay failed to receive the version values.");
1515 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1516 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1518 /* Major versions must be the same */
1519 if (reply
.major
!= be32toh(msg
.major
)) {
1520 DBG("Incompatible major versions (%u vs %u), deleting session",
1521 reply
.major
, be32toh(msg
.major
));
1522 relay_delete_session(cmd
, sessions_ht
);
1527 cmd
->major
= reply
.major
;
1528 /* We adapt to the lowest compatible version */
1529 if (reply
.minor
<= be32toh(msg
.minor
)) {
1530 cmd
->minor
= reply
.minor
;
1532 cmd
->minor
= be32toh(msg
.minor
);
1535 reply
.major
= htobe32(reply
.major
);
1536 reply
.minor
= htobe32(reply
.minor
);
1537 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1538 sizeof(struct lttcomm_relayd_version
), 0);
1540 ERR("Relay sending version");
1543 DBG("Version check done using protocol %u.%u", cmd
->major
,
1551 * Check for data pending for a given stream id from the session daemon.
1554 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1555 struct relay_command
*cmd
)
1557 struct relay_session
*session
= cmd
->session
;
1558 struct lttcomm_relayd_data_pending msg
;
1559 struct lttcomm_relayd_generic_reply reply
;
1560 struct relay_stream
*stream
;
1562 uint64_t last_net_seq_num
, stream_id
;
1564 DBG("Data pending command received");
1566 if (!session
|| cmd
->version_check_done
== 0) {
1567 ERR("Trying to check for data before version check");
1569 goto end_no_session
;
1572 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1573 if (ret
< sizeof(msg
)) {
1575 /* Orderly shutdown. Not necessary to print an error. */
1576 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1578 ERR("Relay didn't receive valid data_pending struct size : %d",
1582 goto end_no_session
;
1585 stream_id
= be64toh(msg
.stream_id
);
1586 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1589 stream
= relay_stream_find_by_id(stream_id
);
1590 if (stream
== NULL
) {
1595 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1596 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1599 /* Avoid wrapping issue */
1600 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1601 /* Data has in fact been written and is NOT pending */
1604 /* Data still being streamed thus pending */
1608 /* Pending check is now done. */
1609 stream
->data_pending_check_done
= 1;
1614 reply
.ret_code
= htobe32(ret
);
1615 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1617 ERR("Relay data pending ret code failed");
1625 * Wait for the control socket to reach a quiescent state.
1627 * Note that for now, when receiving this command from the session daemon, this
1628 * means that every subsequent commands or data received on the control socket
1629 * has been handled. So, this is why we simply return OK here.
1632 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1633 struct relay_command
*cmd
)
1637 struct relay_stream
*stream
;
1638 struct lttng_ht_iter iter
;
1639 struct lttcomm_relayd_quiescent_control msg
;
1640 struct lttcomm_relayd_generic_reply reply
;
1642 DBG("Checking quiescent state on control socket");
1644 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1645 ERR("Trying to check for data before version check");
1647 goto end_no_session
;
1650 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1651 if (ret
< sizeof(msg
)) {
1653 /* Orderly shutdown. Not necessary to print an error. */
1654 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1656 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1660 goto end_no_session
;
1663 stream_id
= be64toh(msg
.stream_id
);
1666 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1668 if (stream
->stream_handle
== stream_id
) {
1669 stream
->data_pending_check_done
= 1;
1670 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1677 reply
.ret_code
= htobe32(LTTNG_OK
);
1678 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1680 ERR("Relay data quiescent control ret code failed");
1688 * Initialize a data pending command. This means that a client is about to ask
1689 * for data pending for each stream he/she holds. Simply iterate over all
1690 * streams of a session and set the data_pending_check_done flag.
1692 * This command returns to the client a LTTNG_OK code.
1695 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1696 struct relay_command
*cmd
)
1699 struct lttng_ht_iter iter
;
1700 struct lttcomm_relayd_begin_data_pending msg
;
1701 struct lttcomm_relayd_generic_reply reply
;
1702 struct relay_stream
*stream
;
1703 uint64_t session_id
;
1708 DBG("Init streams for data pending");
1710 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1711 ERR("Trying to check for data before version check");
1713 goto end_no_session
;
1716 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1717 if (ret
< sizeof(msg
)) {
1719 /* Orderly shutdown. Not necessary to print an error. */
1720 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1722 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1726 goto end_no_session
;
1729 session_id
= be64toh(msg
.session_id
);
1732 * Iterate over all streams to set the begin data pending flag. For now, the
1733 * streams are indexed by stream handle so we have to iterate over all
1734 * streams to find the one associated with the right session_id.
1737 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1739 if (stream
->session
->id
== session_id
) {
1740 stream
->data_pending_check_done
= 0;
1741 DBG("Set begin data pending flag to stream %" PRIu64
,
1742 stream
->stream_handle
);
1747 /* All good, send back reply. */
1748 reply
.ret_code
= htobe32(LTTNG_OK
);
1750 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1752 ERR("Relay begin data pending send reply failed");
1760 * End data pending command. This will check, for a given session id, if each
1761 * stream associated with it has its data_pending_check_done flag set. If not,
1762 * this means that the client lost track of the stream but the data is still
1763 * being streamed on our side. In this case, we inform the client that data is
1766 * Return to the client if there is data in flight or not with a ret_code.
1769 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1770 struct relay_command
*cmd
)
1773 struct lttng_ht_iter iter
;
1774 struct lttcomm_relayd_end_data_pending msg
;
1775 struct lttcomm_relayd_generic_reply reply
;
1776 struct relay_stream
*stream
;
1777 uint64_t session_id
;
1778 uint32_t is_data_inflight
= 0;
1783 DBG("End data pending command");
1785 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1786 ERR("Trying to check for data before version check");
1788 goto end_no_session
;
1791 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1792 if (ret
< sizeof(msg
)) {
1794 /* Orderly shutdown. Not necessary to print an error. */
1795 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1797 ERR("Relay didn't receive valid end data_pending struct size: %d",
1801 goto end_no_session
;
1804 session_id
= be64toh(msg
.session_id
);
1806 /* Iterate over all streams to see if the begin data pending flag is set. */
1808 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1810 if (stream
->session
->id
== session_id
&&
1811 !stream
->data_pending_check_done
) {
1812 is_data_inflight
= 1;
1813 DBG("Data is still in flight for stream %" PRIu64
,
1814 stream
->stream_handle
);
1820 /* All good, send back reply. */
1821 reply
.ret_code
= htobe32(is_data_inflight
);
1823 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1825 ERR("Relay end data pending send reply failed");
1833 * Receive an index for a specific stream.
1835 * Return 0 on success else a negative value.
1838 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1839 struct relay_command
*cmd
)
1841 int ret
, send_ret
, index_created
= 0;
1842 struct relay_session
*session
= cmd
->session
;
1843 struct lttcomm_relayd_index index_info
;
1844 struct relay_index
*index
, *wr_index
= NULL
;
1845 struct lttcomm_relayd_generic_reply reply
;
1846 struct relay_stream
*stream
;
1847 uint64_t net_seq_num
;
1851 DBG("Relay receiving index");
1853 if (!session
|| cmd
->version_check_done
== 0) {
1854 ERR("Trying to close a stream before version check");
1856 goto end_no_session
;
1859 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1860 sizeof(index_info
), 0);
1861 if (ret
< sizeof(index_info
)) {
1863 /* Orderly shutdown. Not necessary to print an error. */
1864 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1866 ERR("Relay didn't receive valid index struct size : %d", ret
);
1869 goto end_no_session
;
1872 net_seq_num
= be64toh(index_info
.net_seq_num
);
1875 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1878 goto end_rcu_unlock
;
1881 /* Live beacon handling */
1882 if (index_info
.packet_size
== 0) {
1883 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1886 * Only flag a stream inactive when it has already received data.
1888 if (stream
->total_index_received
> 0) {
1889 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1892 goto end_rcu_unlock
;
1894 stream
->beacon_ts_end
= -1ULL;
1897 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1899 /* A successful creation will add the object to the HT. */
1900 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1902 goto end_rcu_unlock
;
1907 copy_index_control_data(index
, &index_info
);
1909 if (index_created
) {
1911 * Try to add the relay index object to the hash table. If an object
1912 * already exist, destroy back the index created, set the data in this
1913 * object and write it on disk.
1915 relay_index_add(index
, &wr_index
);
1917 copy_index_control_data(wr_index
, &index_info
);
1921 /* The index already exists so write it on disk. */
1925 /* Do we have a writable ready index to write on disk. */
1927 /* Starting at 2.4, create the index file if none available. */
1928 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1929 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1930 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1931 stream
->tracefile_count_current
);
1933 goto end_rcu_unlock
;
1935 stream
->index_fd
= ret
;
1938 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1940 goto end_rcu_unlock
;
1942 stream
->total_index_received
++;
1949 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1951 reply
.ret_code
= htobe32(LTTNG_OK
);
1953 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1955 ERR("Relay sending close index id reply");
1964 * Receive the streams_sent message.
1966 * Return 0 on success else a negative value.
1969 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
1970 struct relay_command
*cmd
)
1973 struct lttcomm_relayd_generic_reply reply
;
1977 DBG("Relay receiving streams_sent");
1979 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1980 ERR("Trying to close a stream before version check");
1982 goto end_no_session
;
1986 * Flag every pending stream in the connection recv list that they are
1987 * ready to be used by the viewer.
1989 set_viewer_ready_flag(cmd
);
1991 reply
.ret_code
= htobe32(LTTNG_OK
);
1992 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1994 ERR("Relay sending sent_stream reply");
2006 * Process the commands received on the control socket
2009 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2010 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
2014 switch (be32toh(recv_hdr
->cmd
)) {
2015 case RELAYD_CREATE_SESSION
:
2016 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
2018 case RELAYD_ADD_STREAM
:
2019 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
2021 case RELAYD_START_DATA
:
2022 ret
= relay_start(recv_hdr
, cmd
);
2024 case RELAYD_SEND_METADATA
:
2025 ret
= relay_recv_metadata(recv_hdr
, cmd
);
2027 case RELAYD_VERSION
:
2028 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
2030 case RELAYD_CLOSE_STREAM
:
2031 ret
= relay_close_stream(recv_hdr
, cmd
);
2033 case RELAYD_DATA_PENDING
:
2034 ret
= relay_data_pending(recv_hdr
, cmd
);
2036 case RELAYD_QUIESCENT_CONTROL
:
2037 ret
= relay_quiescent_control(recv_hdr
, cmd
);
2039 case RELAYD_BEGIN_DATA_PENDING
:
2040 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
2042 case RELAYD_END_DATA_PENDING
:
2043 ret
= relay_end_data_pending(recv_hdr
, cmd
);
2045 case RELAYD_SEND_INDEX
:
2046 ret
= relay_recv_index(recv_hdr
, cmd
);
2048 case RELAYD_STREAMS_SENT
:
2049 ret
= relay_streams_sent(recv_hdr
, cmd
);
2051 case RELAYD_UPDATE_SYNC_INFO
:
2053 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2054 relay_unknown_command(cmd
);
2064 * Handle index for a data stream.
2066 * RCU read side lock MUST be acquired.
2068 * Return 0 on success else a negative value.
2070 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2073 int ret
= 0, index_created
= 0;
2074 uint64_t stream_id
, data_offset
;
2075 struct relay_index
*index
, *wr_index
= NULL
;
2079 stream_id
= stream
->stream_handle
;
2080 /* Get data offset because we are about to update the index. */
2081 data_offset
= htobe64(stream
->tracefile_size_current
);
2084 * Lookup for an existing index for that stream id/sequence number. If on
2085 * exists, the control thread already received the data for it thus we need
2086 * to write it on disk.
2088 index
= relay_index_find(stream_id
, net_seq_num
);
2090 /* A successful creation will add the object to the HT. */
2091 index
= relay_index_create(stream_id
, net_seq_num
);
2099 if (rotate_index
|| stream
->index_fd
< 0) {
2100 index
->to_close_fd
= stream
->index_fd
;
2101 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2102 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2103 stream
->tracefile_count_current
);
2105 /* This will close the stream's index fd if one. */
2106 relay_index_free_safe(index
);
2109 stream
->index_fd
= ret
;
2111 index
->fd
= stream
->index_fd
;
2112 index
->index_data
.offset
= data_offset
;
2114 if (index_created
) {
2116 * Try to add the relay index object to the hash table. If an object
2117 * already exist, destroy back the index created and set the data.
2119 relay_index_add(index
, &wr_index
);
2121 /* Copy back data from the created index. */
2122 wr_index
->fd
= index
->fd
;
2123 wr_index
->to_close_fd
= index
->to_close_fd
;
2124 wr_index
->index_data
.offset
= data_offset
;
2128 /* The index already exists so write it on disk. */
2132 /* Do we have a writable ready index to write on disk. */
2134 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2138 stream
->total_index_received
++;
2146 * relay_process_data: Process the data received on the data socket
2149 int relay_process_data(struct relay_command
*cmd
)
2151 int ret
= 0, rotate_index
= 0;
2153 struct relay_stream
*stream
;
2154 struct lttcomm_relayd_data_hdr data_hdr
;
2156 uint64_t net_seq_num
;
2159 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2160 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2163 /* Orderly shutdown. Not necessary to print an error. */
2164 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2166 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2172 stream_id
= be64toh(data_hdr
.stream_id
);
2175 stream
= relay_stream_find_by_id(stream_id
);
2178 goto end_rcu_unlock
;
2181 data_size
= be32toh(data_hdr
.data_size
);
2182 if (data_buffer_size
< data_size
) {
2185 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2186 if (!tmp_data_ptr
) {
2187 ERR("Allocating data buffer");
2190 goto end_rcu_unlock
;
2192 data_buffer
= tmp_data_ptr
;
2193 data_buffer_size
= data_size
;
2195 memset(data_buffer
, 0, data_size
);
2197 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2199 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2200 data_size
, stream_id
, net_seq_num
);
2201 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2204 /* Orderly shutdown. Not necessary to print an error. */
2205 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2208 goto end_rcu_unlock
;
2211 /* Check if a rotation is needed. */
2212 if (stream
->tracefile_size
> 0 &&
2213 (stream
->tracefile_size_current
+ data_size
) >
2214 stream
->tracefile_size
) {
2215 struct relay_viewer_stream
*vstream
;
2218 new_id
= (stream
->tracefile_count_current
+ 1) %
2219 stream
->tracefile_count
;
2221 * When we wrap-around back to 0, we start overwriting old
2224 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2225 stream
->tracefile_overwrite
= 1;
2227 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2228 if (stream
->tracefile_overwrite
) {
2229 stream
->oldest_tracefile_id
=
2230 (stream
->oldest_tracefile_id
+ 1) %
2231 stream
->tracefile_count
;
2233 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
2236 * The viewer is reading a file about to be
2237 * overwritten. Close the FDs it is
2238 * currently using and let it handle the fault.
2240 if (vstream
->tracefile_count_current
== new_id
) {
2241 pthread_mutex_lock(&vstream
->overwrite_lock
);
2242 vstream
->abort_flag
= 1;
2243 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2244 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2245 stream
->channel_name
, new_id
);
2246 } else if (vstream
->tracefile_count_current
==
2247 stream
->tracefile_count_current
) {
2249 * The reader and writer were in the
2250 * same trace file, inform the viewer
2251 * that no new index will ever be added
2254 vstream
->close_write_flag
= 1;
2257 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2258 stream
->tracefile_size
, stream
->tracefile_count
,
2259 relayd_uid
, relayd_gid
, stream
->fd
,
2260 &(stream
->tracefile_count_current
), &stream
->fd
);
2261 stream
->total_index_received
= 0;
2262 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2264 ERR("Rotating stream output file");
2265 goto end_rcu_unlock
;
2267 /* Reset current size because we just perform a stream rotation. */
2268 stream
->tracefile_size_current
= 0;
2273 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2274 * index are NOT supported.
2276 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2277 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2279 goto end_rcu_unlock
;
2283 /* Write data to stream output fd. */
2284 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2285 if (size_ret
< data_size
) {
2286 ERR("Relay error writing data to file");
2288 goto end_rcu_unlock
;
2291 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2292 ret
, stream
->stream_handle
);
2294 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2296 goto end_rcu_unlock
;
2298 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2300 stream
->prev_seq
= net_seq_num
;
2302 /* Check if we need to close the FD */
2303 if (close_stream_check(stream
)) {
2304 destroy_stream(stream
);
2314 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2318 lttng_poll_del(events
, pollfd
);
2320 ret
= close(pollfd
);
2322 ERR("Closing pollfd %d", pollfd
);
2327 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2328 struct lttng_ht
*relay_connections_ht
)
2330 struct relay_command
*relay_connection
;
2333 relay_connection
= zmalloc(sizeof(struct relay_command
));
2334 if (relay_connection
== NULL
) {
2335 PERROR("Relay command zmalloc");
2338 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2339 if (ret
< sizeof(struct relay_command
)) {
2340 PERROR("read relay cmd pipe");
2343 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2346 * Only used by the control side and the reference is copied inside each
2347 * stream from that connection. Thus a destroy HT must be done after every
2348 * stream has been destroyed.
2350 if (relay_connection
->type
== RELAY_CONTROL
) {
2351 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2352 LTTNG_HT_TYPE_STRING
);
2353 if (!relay_connection
->ctf_traces_ht
) {
2358 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2359 (unsigned long) relay_connection
->sock
->fd
);
2361 lttng_ht_add_unique_ulong(relay_connections_ht
,
2362 &relay_connection
->sock_n
);
2364 return lttng_poll_add(events
,
2365 relay_connection
->sock
->fd
,
2366 LPOLLIN
| LPOLLRDHUP
);
2369 free(relay_connection
);
2375 void deferred_free_connection(struct rcu_head
*head
)
2377 struct relay_command
*relay_connection
=
2378 caa_container_of(head
, struct relay_command
, rcu_node
);
2380 lttcomm_destroy_sock(relay_connection
->sock
);
2381 free(relay_connection
);
2385 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2386 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2387 struct lttng_ht
*sessions_ht
)
2391 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2394 if (relay_connection
->type
== RELAY_CONTROL
) {
2395 struct relay_stream_recv_handle
*node
, *tmp_node
;
2397 relay_delete_session(relay_connection
, sessions_ht
);
2398 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2400 /* Clean up recv list. */
2401 cds_list_for_each_entry_safe(node
, tmp_node
,
2402 &relay_connection
->recv_head
, node
) {
2403 cds_list_del(&node
->node
);
2408 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2412 * This thread does the actual work
2415 void *relay_thread_worker(void *data
)
2417 int ret
, err
= -1, last_seen_data_fd
= -1;
2419 struct relay_command
*relay_connection
;
2420 struct lttng_poll_event events
;
2421 struct lttng_ht
*relay_connections_ht
;
2422 struct lttng_ht_node_ulong
*node
;
2423 struct lttng_ht_iter iter
;
2424 struct lttcomm_relayd_hdr recv_hdr
;
2425 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2426 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2428 DBG("[thread] Relay worker started");
2430 rcu_register_thread();
2432 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2434 if (testpoint(relayd_thread_worker
)) {
2435 goto error_testpoint
;
2438 health_code_update();
2440 /* table of connections indexed on socket */
2441 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2442 if (!relay_connections_ht
) {
2443 goto relay_connections_ht_error
;
2446 /* Tables of received indexes indexed by index handle and net_seq_num. */
2447 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2449 goto indexes_ht_error
;
2452 ret
= create_thread_poll_set(&events
, 2);
2454 goto error_poll_create
;
2457 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2464 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2466 health_code_update();
2468 /* Infinite blocking call, waiting for transmission */
2469 DBG3("Relayd worker thread polling...");
2470 health_poll_entry();
2471 ret
= lttng_poll_wait(&events
, -1);
2475 * Restart interrupted system call.
2477 if (errno
== EINTR
) {
2486 * Process control. The control connection is prioritised so we don't
2487 * starve it with high throughout put tracing data on the data
2490 for (i
= 0; i
< nb_fd
; i
++) {
2491 /* Fetch once the poll data */
2492 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2493 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2495 health_code_update();
2497 /* Thread quit pipe has been closed. Killing thread. */
2498 ret
= check_thread_quit_pipe(pollfd
, revents
);
2504 /* Inspect the relay cmd pipe for new connection */
2505 if (pollfd
== relay_cmd_pipe
[0]) {
2506 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2507 ERR("Relay pipe error");
2509 } else if (revents
& LPOLLIN
) {
2510 DBG("Relay command received");
2511 ret
= relay_add_connection(relay_cmd_pipe
[0],
2512 &events
, relay_connections_ht
);
2517 } else if (revents
) {
2519 lttng_ht_lookup(relay_connections_ht
,
2520 (void *)((unsigned long) pollfd
),
2522 node
= lttng_ht_iter_get_node_ulong(&iter
);
2524 DBG2("Relay sock %d not found", pollfd
);
2528 relay_connection
= caa_container_of(node
,
2529 struct relay_command
, sock_n
);
2531 if (revents
& (LPOLLERR
)) {
2533 relay_cleanup_poll_connection(&events
, pollfd
);
2534 relay_del_connection(relay_connections_ht
,
2535 &iter
, relay_connection
, sessions_ht
);
2536 if (last_seen_data_fd
== pollfd
) {
2537 last_seen_data_fd
= last_notdel_data_fd
;
2539 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2540 DBG("Socket %d hung up", pollfd
);
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
& LPOLLIN
) {
2548 /* control socket */
2549 if (relay_connection
->type
== RELAY_CONTROL
) {
2550 ret
= relay_connection
->sock
->ops
->recvmsg(
2551 relay_connection
->sock
, &recv_hdr
,
2552 sizeof(struct lttcomm_relayd_hdr
), 0);
2553 /* connection closed */
2555 relay_cleanup_poll_connection(&events
, pollfd
);
2556 relay_del_connection(relay_connections_ht
,
2557 &iter
, relay_connection
, sessions_ht
);
2558 DBG("Control connection closed with %d", pollfd
);
2560 if (relay_connection
->session
) {
2561 DBG2("Relay worker receiving data for session : %" PRIu64
,
2562 relay_connection
->session
->id
);
2564 ret
= relay_process_control(&recv_hdr
,
2565 relay_connection
, relay_ctx
);
2567 /* Clear the session on error. */
2568 relay_cleanup_poll_connection(&events
, pollfd
);
2569 relay_del_connection(relay_connections_ht
,
2570 &iter
, relay_connection
, sessions_ht
);
2571 DBG("Connection closed with %d", pollfd
);
2577 * Flag the last seen data fd not deleted. It will be
2578 * used as the last seen fd if any fd gets deleted in
2581 last_notdel_data_fd
= pollfd
;
2589 * The last loop handled a control request, go back to poll to make
2590 * sure we prioritise the control socket.
2596 if (last_seen_data_fd
>= 0) {
2597 for (i
= 0; i
< nb_fd
; i
++) {
2598 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2600 health_code_update();
2602 if (last_seen_data_fd
== pollfd
) {
2609 /* Process data connection. */
2610 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2611 /* Fetch the poll data. */
2612 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2613 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2615 health_code_update();
2617 /* Skip the command pipe. It's handled in the first loop. */
2618 if (pollfd
== relay_cmd_pipe
[0]) {
2624 lttng_ht_lookup(relay_connections_ht
,
2625 (void *)((unsigned long) pollfd
),
2627 node
= lttng_ht_iter_get_node_ulong(&iter
);
2629 /* Skip it. Might be removed before. */
2633 relay_connection
= caa_container_of(node
,
2634 struct relay_command
, sock_n
);
2636 if (revents
& LPOLLIN
) {
2637 if (relay_connection
->type
!= RELAY_DATA
) {
2641 ret
= relay_process_data(relay_connection
);
2642 /* connection closed */
2644 relay_cleanup_poll_connection(&events
, pollfd
);
2645 relay_del_connection(relay_connections_ht
,
2646 &iter
, relay_connection
, sessions_ht
);
2647 DBG("Data connection closed with %d", pollfd
);
2649 * Every goto restart call sets the last seen fd where
2650 * here we don't really care since we gracefully
2651 * continue the loop after the connection is deleted.
2654 /* Keep last seen port. */
2655 last_seen_data_fd
= pollfd
;
2663 last_seen_data_fd
= -1;
2666 /* Normal exit, no error */
2671 lttng_poll_clean(&events
);
2673 /* empty the hash table and free the memory */
2675 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2676 health_code_update();
2678 node
= lttng_ht_iter_get_node_ulong(&iter
);
2680 relay_connection
= caa_container_of(node
,
2681 struct relay_command
, sock_n
);
2682 relay_del_connection(relay_connections_ht
,
2683 &iter
, relay_connection
, sessions_ht
);
2688 lttng_ht_destroy(indexes_ht
);
2690 lttng_ht_destroy(relay_connections_ht
);
2691 relay_connections_ht_error
:
2692 /* Close relay cmd pipes */
2693 utils_close_pipe(relay_cmd_pipe
);
2695 DBG("Thread exited with error");
2697 DBG("Worker thread cleanup complete");
2702 ERR("Health error occurred in %s", __func__
);
2704 health_unregister(health_relayd
);
2705 rcu_unregister_thread();
2711 * Create the relay command pipe to wake thread_manage_apps.
2712 * Closed in cleanup().
2714 static int create_relay_cmd_pipe(void)
2718 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2726 int main(int argc
, char **argv
)
2730 struct relay_local_data
*relay_ctx
;
2732 /* Parse arguments */
2734 if ((ret
= parse_args(argc
, argv
)) < 0) {
2738 if ((ret
= set_signal_handler()) < 0) {
2742 /* Try to create directory if -o, --output is specified. */
2743 if (opt_output_path
) {
2744 if (*opt_output_path
!= '/') {
2745 ERR("Please specify an absolute path for -o, --output PATH");
2749 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2751 ERR("Unable to create %s", opt_output_path
);
2757 if (opt_daemon
|| opt_background
) {
2760 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2767 * We are in the child. Make sure all other file
2768 * descriptors are closed, in case we are called with
2769 * more opened file descriptors than the standard ones.
2771 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2776 /* Create thread quit pipe */
2777 if ((ret
= init_thread_quit_pipe()) < 0) {
2781 /* We need those values for the file/dir creation. */
2782 relayd_uid
= getuid();
2783 relayd_gid
= getgid();
2785 /* Check if daemon is UID = 0 */
2786 if (relayd_uid
== 0) {
2787 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2788 live_uri
->port
< 1024) {
2789 ERR("Need to be root to use ports < 1024");
2795 /* Setup the thread apps communication pipe. */
2796 if ((ret
= create_relay_cmd_pipe()) < 0) {
2800 /* Init relay command queue. */
2801 cds_wfq_init(&relay_cmd_queue
.queue
);
2803 /* Set up max poll set size */
2804 lttng_poll_set_max_size();
2806 /* Initialize communication library */
2808 lttcomm_inet_init();
2810 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2812 PERROR("relay_ctx");
2816 /* tables of sessions indexed by session ID */
2817 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2818 if (!relay_ctx
->sessions_ht
) {
2819 goto exit_relay_ctx_sessions
;
2822 /* tables of streams indexed by stream ID */
2823 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2824 if (!relay_streams_ht
) {
2825 goto exit_relay_ctx_streams
;
2828 /* tables of streams indexed by stream ID */
2829 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2830 if (!viewer_streams_ht
) {
2831 goto exit_relay_ctx_viewer_streams
;
2834 /* Initialize thread health monitoring */
2835 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2836 if (!health_relayd
) {
2837 PERROR("health_app_create error");
2838 goto exit_health_app_create
;
2841 ret
= utils_create_pipe(health_quit_pipe
);
2843 goto error_health_pipe
;
2846 /* Create thread to manage the client socket */
2847 ret
= pthread_create(&health_thread
, NULL
,
2848 thread_manage_health
, (void *) NULL
);
2850 PERROR("pthread_create health");
2854 /* Setup the dispatcher thread */
2855 ret
= pthread_create(&dispatcher_thread
, NULL
,
2856 relay_thread_dispatcher
, (void *) NULL
);
2858 PERROR("pthread_create dispatcher");
2859 goto exit_dispatcher
;
2862 /* Setup the worker thread */
2863 ret
= pthread_create(&worker_thread
, NULL
,
2864 relay_thread_worker
, (void *) relay_ctx
);
2866 PERROR("pthread_create worker");
2870 /* Setup the listener thread */
2871 ret
= pthread_create(&listener_thread
, NULL
,
2872 relay_thread_listener
, (void *) NULL
);
2874 PERROR("pthread_create listener");
2878 ret
= live_start_threads(live_uri
, relay_ctx
);
2880 ERR("Starting live viewer threads");
2885 ret
= pthread_join(listener_thread
, &status
);
2887 PERROR("pthread_join");
2888 goto error
; /* join error, exit without cleanup */
2892 ret
= pthread_join(worker_thread
, &status
);
2894 PERROR("pthread_join");
2895 goto error
; /* join error, exit without cleanup */
2899 ret
= pthread_join(dispatcher_thread
, &status
);
2901 PERROR("pthread_join");
2902 goto error
; /* join error, exit without cleanup */
2906 ret
= pthread_join(health_thread
, &status
);
2908 PERROR("pthread_join health thread");
2909 goto error
; /* join error, exit without cleanup */
2913 * Stop live threads only after joining other threads.
2915 live_stop_threads();
2918 utils_close_pipe(health_quit_pipe
);
2921 health_app_destroy(health_relayd
);
2923 exit_health_app_create
:
2924 lttng_ht_destroy(viewer_streams_ht
);
2926 exit_relay_ctx_viewer_streams
:
2927 lttng_ht_destroy(relay_streams_ht
);
2929 exit_relay_ctx_streams
:
2930 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2932 exit_relay_ctx_sessions
: