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"
63 #include "viewer-stream.h"
65 /* command line options */
66 char *opt_output_path
;
67 static int opt_daemon
, opt_background
;
70 * We need to wait for listener and live listener threads, as well as
71 * health check thread, before being ready to signal readiness.
73 #define NR_LTTNG_RELAY_READY 3
74 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
75 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
76 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
78 static struct lttng_uri
*control_uri
;
79 static struct lttng_uri
*data_uri
;
80 static struct lttng_uri
*live_uri
;
84 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
87 * Quit pipe for all threads. This permits a single cancellation point
88 * for all threads when receiving an event on the pipe.
90 int thread_quit_pipe
[2] = { -1, -1 };
93 * This pipe is used to inform the worker thread that a command is queued and
94 * ready to be processed.
96 static int relay_cmd_pipe
[2] = { -1, -1 };
98 /* Shared between threads */
99 static int dispatch_thread_exit
;
101 static pthread_t listener_thread
;
102 static pthread_t dispatcher_thread
;
103 static pthread_t worker_thread
;
104 static pthread_t health_thread
;
106 static uint64_t last_relay_stream_id
;
107 static uint64_t last_relay_session_id
;
110 * Relay command queue.
112 * The relay_thread_listener and relay_thread_dispatcher communicate with this
115 static struct relay_cmd_queue relay_cmd_queue
;
117 /* buffer allocated at startup, used to store the trace data */
118 static char *data_buffer
;
119 static unsigned int data_buffer_size
;
121 /* We need those values for the file/dir creation. */
122 static uid_t relayd_uid
;
123 static gid_t relayd_gid
;
125 /* Global relay stream hash table. */
126 struct lttng_ht
*relay_streams_ht
;
128 /* Global relay viewer stream hash table. */
129 struct lttng_ht
*viewer_streams_ht
;
131 /* Global hash table that stores relay index object. */
132 struct lttng_ht
*indexes_ht
;
134 /* Relayd health monitoring */
135 struct health_app
*health_relayd
;
138 * usage function on stderr
143 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
144 fprintf(stderr
, " -h, --help Display this usage.\n");
145 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
146 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
147 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
148 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
149 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
150 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
151 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
152 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
156 int parse_args(int argc
, char **argv
)
160 char *default_address
;
162 static struct option long_options
[] = {
163 { "control-port", 1, 0, 'C', },
164 { "data-port", 1, 0, 'D', },
165 { "daemonize", 0, 0, 'd', },
166 { "group", 1, 0, 'g', },
167 { "help", 0, 0, 'h', },
168 { "output", 1, 0, 'o', },
169 { "verbose", 0, 0, 'v', },
170 { "background", 0, 0, 'b' },
175 int option_index
= 0;
176 c
= getopt_long(argc
, argv
, "dhv" "C:D:L:o:g:b",
177 long_options
, &option_index
);
184 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
186 fprintf(stderr
, " with arg %s\n", optarg
);
190 ret
= uri_parse(optarg
, &control_uri
);
192 ERR("Invalid control URI specified");
195 if (control_uri
->port
== 0) {
196 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
200 ret
= uri_parse(optarg
, &data_uri
);
202 ERR("Invalid data URI specified");
205 if (data_uri
->port
== 0) {
206 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
210 ret
= uri_parse(optarg
, &live_uri
);
212 ERR("Invalid live URI specified");
215 if (live_uri
->port
== 0) {
216 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
226 tracing_group_name
= optarg
;
232 ret
= asprintf(&opt_output_path
, "%s", optarg
);
234 PERROR("asprintf opt_output_path");
239 /* Verbose level can increase using multiple -v */
240 lttng_opt_verbose
+= 1;
243 /* Unknown option or other error.
244 * Error is printed by getopt, just return */
250 /* assign default values */
251 if (control_uri
== NULL
) {
252 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
253 DEFAULT_NETWORK_CONTROL_PORT
);
255 PERROR("asprintf default data address");
259 ret
= uri_parse(default_address
, &control_uri
);
260 free(default_address
);
262 ERR("Invalid control URI specified");
266 if (data_uri
== NULL
) {
267 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
268 DEFAULT_NETWORK_DATA_PORT
);
270 PERROR("asprintf default data address");
274 ret
= uri_parse(default_address
, &data_uri
);
275 free(default_address
);
277 ERR("Invalid data URI specified");
281 if (live_uri
== NULL
) {
282 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
283 DEFAULT_NETWORK_VIEWER_PORT
);
285 PERROR("asprintf default viewer control address");
289 ret
= uri_parse(default_address
, &live_uri
);
290 free(default_address
);
292 ERR("Invalid viewer control URI specified");
309 /* free the dynamically allocated opt_output_path */
310 free(opt_output_path
);
312 /* Close thread quit pipes */
313 utils_close_pipe(thread_quit_pipe
);
315 uri_free(control_uri
);
317 /* Live URI is freed in the live thread. */
321 * Write to writable pipe used to notify a thread.
324 int notify_thread_pipe(int wpipe
)
328 ret
= lttng_write(wpipe
, "!", 1);
330 PERROR("write poll pipe");
336 static void notify_health_quit_pipe(int *pipe
)
340 ret
= lttng_write(pipe
[1], "4", 1);
342 PERROR("write relay health quit");
347 * Stop all threads by closing the thread quit pipe.
350 void stop_threads(void)
354 /* Stopping all threads */
355 DBG("Terminating all threads");
356 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
358 ERR("write error on thread quit pipe");
361 notify_health_quit_pipe(health_quit_pipe
);
363 /* Dispatch thread */
364 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
365 futex_nto1_wake(&relay_cmd_queue
.futex
);
369 * Signal handler for the daemon
371 * Simply stop all worker threads, leaving main() return gracefully after
372 * joining all threads and calling cleanup().
375 void sighandler(int sig
)
379 DBG("SIGPIPE caught");
382 DBG("SIGINT caught");
386 DBG("SIGTERM caught");
390 CMM_STORE_SHARED(recv_child_signal
, 1);
398 * Setup signal handler for :
399 * SIGINT, SIGTERM, SIGPIPE
402 int set_signal_handler(void)
408 if ((ret
= sigemptyset(&sigset
)) < 0) {
409 PERROR("sigemptyset");
413 sa
.sa_handler
= sighandler
;
416 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
421 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
426 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
431 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
436 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
441 void lttng_relay_notify_ready(void)
443 /* Notify the parent of the fork() process that we are ready. */
444 if (opt_daemon
|| opt_background
) {
445 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
446 kill(child_ppid
, SIGUSR1
);
452 * Init thread quit pipe.
454 * Return -1 on error or 0 if all pipes are created.
457 int init_thread_quit_pipe(void)
461 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
467 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
470 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
474 if (events
== NULL
|| size
== 0) {
479 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
485 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
497 * Check if the thread quit pipe was triggered.
499 * Return 1 if it was triggered else 0;
502 int check_thread_quit_pipe(int fd
, uint32_t events
)
504 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
512 * Create and init socket from uri.
515 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
518 struct lttcomm_sock
*sock
= NULL
;
520 sock
= lttcomm_alloc_sock_from_uri(uri
);
522 ERR("Allocating socket");
526 ret
= lttcomm_create_sock(sock
);
530 DBG("Listening on sock %d", sock
->fd
);
532 ret
= sock
->ops
->bind(sock
);
537 ret
= sock
->ops
->listen(sock
, -1);
547 lttcomm_destroy_sock(sock
);
553 * Return nonzero if stream needs to be closed.
556 int close_stream_check(struct relay_stream
*stream
)
559 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
561 * We are about to close the stream so set the data pending flag to 1
562 * which will make the end data pending command skip the stream which
563 * is now closed and ready. Note that after proceeding to a file close,
564 * the written file is ready for reading.
566 stream
->data_pending_check_done
= 1;
573 * This thread manages the listening for new connections on the network
576 void *relay_thread_listener(void *data
)
578 int i
, ret
, pollfd
, err
= -1;
580 uint32_t revents
, nb_fd
;
581 struct lttng_poll_event events
;
582 struct lttcomm_sock
*control_sock
, *data_sock
;
584 DBG("[thread] Relay listener started");
586 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
588 health_code_update();
590 control_sock
= relay_init_sock(control_uri
);
592 goto error_sock_control
;
595 data_sock
= relay_init_sock(data_uri
);
597 goto error_sock_relay
;
601 * Pass 3 as size here for the thread quit pipe, control and data socket.
603 ret
= create_thread_poll_set(&events
, 3);
605 goto error_create_poll
;
608 /* Add the control socket */
609 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
614 /* Add the data socket */
615 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
620 lttng_relay_notify_ready();
622 if (testpoint(relayd_thread_listener
)) {
623 goto error_testpoint
;
627 health_code_update();
629 DBG("Listener accepting connections");
633 ret
= lttng_poll_wait(&events
, -1);
637 * Restart interrupted system call.
639 if (errno
== EINTR
) {
647 DBG("Relay new connection received");
648 for (i
= 0; i
< nb_fd
; i
++) {
649 health_code_update();
651 /* Fetch once the poll data */
652 revents
= LTTNG_POLL_GETEV(&events
, i
);
653 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
655 /* Thread quit pipe has been closed. Killing thread. */
656 ret
= check_thread_quit_pipe(pollfd
, revents
);
662 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
663 ERR("socket poll error");
665 } else if (revents
& LPOLLIN
) {
667 * Get allocated in this thread,
668 * enqueued to a global queue, dequeued
669 * and freed in the worker thread.
671 struct relay_command
*relay_cmd
;
672 struct lttcomm_sock
*newsock
;
674 relay_cmd
= zmalloc(sizeof(struct relay_command
));
675 if (relay_cmd
== NULL
) {
676 PERROR("relay command zmalloc");
680 if (pollfd
== data_sock
->fd
) {
681 newsock
= data_sock
->ops
->accept(data_sock
);
683 PERROR("accepting data sock");
687 relay_cmd
->type
= RELAY_DATA
;
688 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
690 assert(pollfd
== control_sock
->fd
);
691 newsock
= control_sock
->ops
->accept(control_sock
);
693 PERROR("accepting control sock");
697 relay_cmd
->type
= RELAY_CONTROL
;
698 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
700 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
703 PERROR("setsockopt inet");
704 lttcomm_destroy_sock(newsock
);
708 relay_cmd
->sock
= newsock
;
710 * Lock free enqueue the request.
712 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
715 * Wake the dispatch queue futex. Implicit memory
716 * barrier with the exchange in cds_wfq_enqueue.
718 futex_nto1_wake(&relay_cmd_queue
.futex
);
727 lttng_poll_clean(&events
);
729 if (data_sock
->fd
>= 0) {
730 ret
= data_sock
->ops
->close(data_sock
);
735 lttcomm_destroy_sock(data_sock
);
737 if (control_sock
->fd
>= 0) {
738 ret
= control_sock
->ops
->close(control_sock
);
743 lttcomm_destroy_sock(control_sock
);
747 ERR("Health error occurred in %s", __func__
);
749 health_unregister(health_relayd
);
750 DBG("Relay listener thread cleanup complete");
756 * This thread manages the dispatching of the requests to worker threads
759 void *relay_thread_dispatcher(void *data
)
763 struct cds_wfq_node
*node
;
764 struct relay_command
*relay_cmd
= NULL
;
766 DBG("[thread] Relay dispatcher started");
768 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
770 if (testpoint(relayd_thread_dispatcher
)) {
771 goto error_testpoint
;
774 health_code_update();
776 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
777 health_code_update();
779 /* Atomically prepare the queue futex */
780 futex_nto1_prepare(&relay_cmd_queue
.futex
);
783 health_code_update();
785 /* Dequeue commands */
786 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
788 DBG("Woken up but nothing in the relay command queue");
789 /* Continue thread execution */
793 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
794 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
797 * Inform worker thread of the new request. This
798 * call is blocking so we can be assured that the data will be read
799 * at some point in time or wait to the end of the world :)
801 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
802 sizeof(struct relay_command
));
804 if (ret
< sizeof(struct relay_command
)) {
805 PERROR("write cmd pipe");
808 } while (node
!= NULL
);
810 /* Futex wait on queue. Blocking call on futex() */
812 futex_nto1_wait(&relay_cmd_queue
.futex
);
816 /* Normal exit, no error */
823 ERR("Health error occurred in %s", __func__
);
825 health_unregister(health_relayd
);
826 DBG("Dispatch thread dying");
832 * Get stream from stream id.
833 * Need to be called with RCU read-side lock held.
835 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
837 struct lttng_ht_node_ulong
*node
;
838 struct lttng_ht_iter iter
;
839 struct relay_stream
*ret
;
841 lttng_ht_lookup(relay_streams_ht
,
842 (void *)((unsigned long) stream_id
),
844 node
= lttng_ht_iter_get_node_ulong(&iter
);
846 DBG("Relay stream %" PRIu64
" not found", stream_id
);
851 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
858 void deferred_free_stream(struct rcu_head
*head
)
860 struct relay_stream
*stream
=
861 caa_container_of(head
, struct relay_stream
, rcu_node
);
863 free(stream
->path_name
);
864 free(stream
->channel_name
);
869 void deferred_free_session(struct rcu_head
*head
)
871 struct relay_session
*session
=
872 caa_container_of(head
, struct relay_session
, rcu_node
);
877 * Close a given stream. The stream is freed using a call RCU.
879 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
880 * BEFORE the stream lock MUST be acquired.
882 static void destroy_stream(struct relay_stream
*stream
)
885 struct relay_viewer_stream
*vstream
;
886 struct lttng_ht_iter iter
;
890 delret
= close(stream
->fd
);
892 PERROR("close stream");
895 if (stream
->index_fd
>= 0) {
896 delret
= close(stream
->index_fd
);
898 PERROR("close stream index_fd");
902 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
905 * Set the last good value into the viewer stream. This is done
906 * right before the stream gets deleted from the hash table. The
907 * lookup failure on the live thread side of a stream indicates
908 * that the viewer stream index received value should be used.
910 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
911 vstream
->total_index_received
= stream
->total_index_received
;
912 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
913 vstream
->close_write_flag
= 1;
914 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
917 /* Cleanup index of that stream. */
918 relay_index_destroy_by_stream_id(stream
->stream_handle
);
920 iter
.iter
.node
= &stream
->stream_n
.node
;
921 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
923 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
924 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
927 if (stream
->ctf_trace
) {
928 ctf_trace_try_destroy(stream
->ctf_trace
);
931 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
932 DBG("Closed tracefile %d from close stream", stream
->fd
);
936 * relay_delete_session: Free all memory associated with a session and
940 void relay_delete_session(struct relay_command
*cmd
,
941 struct lttng_ht
*sessions_ht
)
943 struct lttng_ht_iter iter
;
944 struct lttng_ht_node_ulong
*node
;
945 struct relay_stream
*stream
;
952 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
955 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
956 node
= lttng_ht_iter_get_node_ulong(&iter
);
960 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
961 if (stream
->session
== cmd
->session
) {
962 destroy_stream(stream
);
963 cmd
->session
->stream_count
--;
964 assert(cmd
->session
->stream_count
>= 0);
968 /* Make this session not visible anymore. */
969 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
970 ret
= lttng_ht_del(sessions_ht
, &iter
);
972 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
977 * Copy index data from the control port to a given index object.
979 static void copy_index_control_data(struct relay_index
*index
,
980 struct lttcomm_relayd_index
*data
)
986 * The index on disk is encoded in big endian, so we don't need to convert
987 * the data received on the network. The data_offset value is NEVER
988 * modified here and is updated by the data thread.
990 index
->index_data
.packet_size
= data
->packet_size
;
991 index
->index_data
.content_size
= data
->content_size
;
992 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
993 index
->index_data
.timestamp_end
= data
->timestamp_end
;
994 index
->index_data
.events_discarded
= data
->events_discarded
;
995 index
->index_data
.stream_id
= data
->stream_id
;
999 * Handle the RELAYD_CREATE_SESSION command.
1001 * On success, send back the session id or else return a negative value.
1004 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1005 struct relay_command
*cmd
,
1006 struct lttng_ht
*sessions_ht
)
1008 int ret
= 0, send_ret
;
1009 struct relay_session
*session
;
1010 struct lttcomm_relayd_status_session reply
;
1015 memset(&reply
, 0, sizeof(reply
));
1017 session
= zmalloc(sizeof(struct relay_session
));
1018 if (session
== NULL
) {
1019 PERROR("relay session zmalloc");
1024 session
->id
= ++last_relay_session_id
;
1025 session
->sock
= cmd
->sock
;
1026 session
->minor
= cmd
->minor
;
1027 session
->major
= cmd
->major
;
1028 pthread_mutex_init(&session
->viewer_ready_lock
, NULL
);
1029 cmd
->session
= session
;
1031 reply
.session_id
= htobe64(session
->id
);
1033 switch (cmd
->minor
) {
1038 case 4: /* LTTng sessiond 2.4 */
1040 ret
= cmd_create_session_2_4(cmd
, session
);
1044 lttng_ht_node_init_ulong(&session
->session_n
,
1045 (unsigned long) session
->id
);
1046 lttng_ht_add_unique_ulong(sessions_ht
,
1047 &session
->session_n
);
1049 DBG("Created session %" PRIu64
, session
->id
);
1053 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1055 reply
.ret_code
= htobe32(LTTNG_OK
);
1058 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1060 ERR("Relayd sending session id");
1068 * When we have received all the streams and the metadata for a channel,
1069 * we make them visible to the viewer threads.
1072 void set_viewer_ready_flag(struct relay_command
*cmd
)
1074 struct relay_stream_recv_handle
*node
, *tmp_node
;
1076 pthread_mutex_lock(&cmd
->session
->viewer_ready_lock
);
1078 cds_list_for_each_entry_safe(node
, tmp_node
, &cmd
->recv_head
, node
) {
1079 struct relay_stream
*stream
;
1082 stream
= relay_stream_find_by_id(node
->id
);
1085 * Stream is most probably being cleaned up by the data thread thus
1086 * simply continue to the next one.
1092 stream
->viewer_ready
= 1;
1095 /* Clean stream handle node. */
1096 cds_list_del(&node
->node
);
1100 pthread_mutex_unlock(&cmd
->session
->viewer_ready_lock
);
1105 * Add a recv handle node to the connection recv list with the given stream
1106 * handle. A new node is allocated thus must be freed when the node is deleted
1109 static void queue_stream_handle(uint64_t handle
, struct relay_command
*cmd
)
1111 struct relay_stream_recv_handle
*node
;
1115 node
= zmalloc(sizeof(*node
));
1117 PERROR("zmalloc queue stream handle");
1122 cds_list_add(&node
->node
, &cmd
->recv_head
);
1126 * relay_add_stream: allocate a new stream for a session
1129 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1130 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1132 struct relay_session
*session
= cmd
->session
;
1133 struct relay_stream
*stream
= NULL
;
1134 struct lttcomm_relayd_status_stream reply
;
1137 if (!session
|| cmd
->version_check_done
== 0) {
1138 ERR("Trying to add a stream before version check");
1140 goto end_no_session
;
1143 stream
= zmalloc(sizeof(struct relay_stream
));
1144 if (stream
== NULL
) {
1145 PERROR("relay stream zmalloc");
1147 goto end_no_session
;
1150 switch (cmd
->minor
) {
1151 case 1: /* LTTng sessiond 2.1 */
1152 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1154 case 2: /* LTTng sessiond 2.2 */
1156 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1160 goto err_free_stream
;
1164 stream
->stream_handle
= ++last_relay_stream_id
;
1165 stream
->prev_seq
= -1ULL;
1166 stream
->session
= session
;
1167 stream
->index_fd
= -1;
1168 stream
->read_index_fd
= -1;
1169 stream
->ctf_trace
= NULL
;
1170 pthread_mutex_init(&stream
->lock
, NULL
);
1172 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1174 ERR("relay creating output directory");
1179 * No need to use run_as API here because whatever we receives, the relayd
1180 * uses its own credentials for the stream files.
1182 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1183 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1185 ERR("Create output file");
1189 if (stream
->tracefile_size
) {
1190 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1192 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1195 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1196 stream
->metadata_flag
= 1;
1198 * When we receive a new metadata stream, we create a new
1199 * ctf_trace and we assign this ctf_trace to all streams with
1202 * If later on we receive a new stream for the same ctf_trace,
1203 * we copy the information from the first hit in the HT to the
1206 stream
->ctf_trace
= ctf_trace_create();
1207 if (!stream
->ctf_trace
) {
1211 stream
->ctf_trace
->refcount
++;
1212 stream
->ctf_trace
->metadata_stream
= stream
;
1214 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1215 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1218 * Add the stream handle in the recv list of the connection. Once the end
1219 * stream message is received, this list is emptied and streams are set
1220 * with the viewer ready flag.
1222 queue_stream_handle(stream
->stream_handle
, cmd
);
1224 lttng_ht_node_init_ulong(&stream
->stream_n
,
1225 (unsigned long) stream
->stream_handle
);
1226 lttng_ht_add_unique_ulong(relay_streams_ht
,
1229 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1230 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1231 session
->stream_count
++;
1233 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1234 stream
->stream_handle
);
1237 reply
.handle
= htobe64(stream
->stream_handle
);
1238 /* send the session id to the client or a negative return code on error */
1240 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1241 /* stream was not properly added to the ht, so free it */
1244 reply
.ret_code
= htobe32(LTTNG_OK
);
1247 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1248 sizeof(struct lttcomm_relayd_status_stream
), 0);
1250 ERR("Relay sending stream id");
1259 free(stream
->path_name
);
1260 free(stream
->channel_name
);
1266 * relay_close_stream: close a specific stream
1269 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1270 struct relay_command
*cmd
)
1273 struct relay_session
*session
= cmd
->session
;
1274 struct lttcomm_relayd_close_stream stream_info
;
1275 struct lttcomm_relayd_generic_reply reply
;
1276 struct relay_stream
*stream
;
1278 DBG("Close stream received");
1280 if (!session
|| cmd
->version_check_done
== 0) {
1281 ERR("Trying to close a stream before version check");
1283 goto end_no_session
;
1286 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1287 sizeof(struct lttcomm_relayd_close_stream
), 0);
1288 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1290 /* Orderly shutdown. Not necessary to print an error. */
1291 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1293 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1296 goto end_no_session
;
1300 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1306 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1307 stream
->close_flag
= 1;
1308 session
->stream_count
--;
1309 assert(session
->stream_count
>= 0);
1311 if (close_stream_check(stream
)) {
1312 destroy_stream(stream
);
1319 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1321 reply
.ret_code
= htobe32(LTTNG_OK
);
1323 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1324 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1326 ERR("Relay sending stream id");
1335 * relay_unknown_command: send -1 if received unknown command
1338 void relay_unknown_command(struct relay_command
*cmd
)
1340 struct lttcomm_relayd_generic_reply reply
;
1343 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1344 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1345 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1347 ERR("Relay sending unknown command");
1352 * relay_start: send an acknowledgment to the client to tell if we are
1353 * ready to receive data. We are ready if a session is established.
1356 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1357 struct relay_command
*cmd
)
1359 int ret
= htobe32(LTTNG_OK
);
1360 struct lttcomm_relayd_generic_reply reply
;
1361 struct relay_session
*session
= cmd
->session
;
1364 DBG("Trying to start the streaming without a session established");
1365 ret
= htobe32(LTTNG_ERR_UNK
);
1368 reply
.ret_code
= ret
;
1369 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1370 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1372 ERR("Relay sending start ack");
1379 * Append padding to the file pointed by the file descriptor fd.
1381 static int write_padding_to_file(int fd
, uint32_t size
)
1390 zeros
= zmalloc(size
);
1391 if (zeros
== NULL
) {
1392 PERROR("zmalloc zeros for padding");
1397 ret
= lttng_write(fd
, zeros
, size
);
1399 PERROR("write padding to file");
1409 * relay_recv_metadata: receive the metada for the session.
1412 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1413 struct relay_command
*cmd
)
1415 int ret
= htobe32(LTTNG_OK
);
1417 struct relay_session
*session
= cmd
->session
;
1418 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1419 struct relay_stream
*metadata_stream
;
1420 uint64_t data_size
, payload_size
;
1423 ERR("Metadata sent before version check");
1428 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1429 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1430 ERR("Incorrect data size");
1434 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1436 if (data_buffer_size
< data_size
) {
1437 /* In case the realloc fails, we can free the memory */
1440 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1441 if (!tmp_data_ptr
) {
1442 ERR("Allocating data buffer");
1447 data_buffer
= tmp_data_ptr
;
1448 data_buffer_size
= data_size
;
1450 memset(data_buffer
, 0, data_size
);
1451 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1452 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1453 if (ret
< 0 || ret
!= data_size
) {
1455 /* Orderly shutdown. Not necessary to print an error. */
1456 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1458 ERR("Relay didn't receive the whole metadata");
1463 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1466 metadata_stream
= relay_stream_find_by_id(
1467 be64toh(metadata_struct
->stream_id
));
1468 if (!metadata_stream
) {
1473 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1475 if (size_ret
< payload_size
) {
1476 ERR("Relay error writing metadata on file");
1481 ret
= write_padding_to_file(metadata_stream
->fd
,
1482 be32toh(metadata_struct
->padding_size
));
1486 metadata_stream
->ctf_trace
->metadata_received
+=
1487 payload_size
+ be32toh(metadata_struct
->padding_size
);
1489 DBG2("Relay metadata written");
1498 * relay_send_version: send relayd version number
1501 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1502 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1505 struct lttcomm_relayd_version reply
, msg
;
1509 cmd
->version_check_done
= 1;
1511 /* Get version from the other side. */
1512 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1513 if (ret
< 0 || ret
!= sizeof(msg
)) {
1515 /* Orderly shutdown. Not necessary to print an error. */
1516 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1518 ERR("Relay failed to receive the version values.");
1524 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1525 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1527 /* Major versions must be the same */
1528 if (reply
.major
!= be32toh(msg
.major
)) {
1529 DBG("Incompatible major versions (%u vs %u), deleting session",
1530 reply
.major
, be32toh(msg
.major
));
1531 relay_delete_session(cmd
, sessions_ht
);
1536 cmd
->major
= reply
.major
;
1537 /* We adapt to the lowest compatible version */
1538 if (reply
.minor
<= be32toh(msg
.minor
)) {
1539 cmd
->minor
= reply
.minor
;
1541 cmd
->minor
= be32toh(msg
.minor
);
1544 reply
.major
= htobe32(reply
.major
);
1545 reply
.minor
= htobe32(reply
.minor
);
1546 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1547 sizeof(struct lttcomm_relayd_version
), 0);
1549 ERR("Relay sending version");
1552 DBG("Version check done using protocol %u.%u", cmd
->major
,
1560 * Check for data pending for a given stream id from the session daemon.
1563 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1564 struct relay_command
*cmd
)
1566 struct relay_session
*session
= cmd
->session
;
1567 struct lttcomm_relayd_data_pending msg
;
1568 struct lttcomm_relayd_generic_reply reply
;
1569 struct relay_stream
*stream
;
1571 uint64_t last_net_seq_num
, stream_id
;
1573 DBG("Data pending command received");
1575 if (!session
|| cmd
->version_check_done
== 0) {
1576 ERR("Trying to check for data before version check");
1578 goto end_no_session
;
1581 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1582 if (ret
< sizeof(msg
)) {
1584 /* Orderly shutdown. Not necessary to print an error. */
1585 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1587 ERR("Relay didn't receive valid data_pending struct size : %d",
1591 goto end_no_session
;
1594 stream_id
= be64toh(msg
.stream_id
);
1595 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1598 stream
= relay_stream_find_by_id(stream_id
);
1599 if (stream
== NULL
) {
1604 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1605 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1608 /* Avoid wrapping issue */
1609 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1610 /* Data has in fact been written and is NOT pending */
1613 /* Data still being streamed thus pending */
1617 /* Pending check is now done. */
1618 stream
->data_pending_check_done
= 1;
1623 reply
.ret_code
= htobe32(ret
);
1624 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1626 ERR("Relay data pending ret code failed");
1634 * Wait for the control socket to reach a quiescent state.
1636 * Note that for now, when receiving this command from the session daemon, this
1637 * means that every subsequent commands or data received on the control socket
1638 * has been handled. So, this is why we simply return OK here.
1641 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1642 struct relay_command
*cmd
)
1646 struct relay_stream
*stream
;
1647 struct lttng_ht_iter iter
;
1648 struct lttcomm_relayd_quiescent_control msg
;
1649 struct lttcomm_relayd_generic_reply reply
;
1651 DBG("Checking quiescent state on control socket");
1653 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1654 ERR("Trying to check for data before version check");
1656 goto end_no_session
;
1659 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1660 if (ret
< sizeof(msg
)) {
1662 /* Orderly shutdown. Not necessary to print an error. */
1663 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1665 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1669 goto end_no_session
;
1672 stream_id
= be64toh(msg
.stream_id
);
1675 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1677 if (stream
->stream_handle
== stream_id
) {
1678 stream
->data_pending_check_done
= 1;
1679 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1686 reply
.ret_code
= htobe32(LTTNG_OK
);
1687 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1689 ERR("Relay data quiescent control ret code failed");
1697 * Initialize a data pending command. This means that a client is about to ask
1698 * for data pending for each stream he/she holds. Simply iterate over all
1699 * streams of a session and set the data_pending_check_done flag.
1701 * This command returns to the client a LTTNG_OK code.
1704 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1705 struct relay_command
*cmd
)
1708 struct lttng_ht_iter iter
;
1709 struct lttcomm_relayd_begin_data_pending msg
;
1710 struct lttcomm_relayd_generic_reply reply
;
1711 struct relay_stream
*stream
;
1712 uint64_t session_id
;
1717 DBG("Init streams for data pending");
1719 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1720 ERR("Trying to check for data before version check");
1722 goto end_no_session
;
1725 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1726 if (ret
< sizeof(msg
)) {
1728 /* Orderly shutdown. Not necessary to print an error. */
1729 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1731 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1735 goto end_no_session
;
1738 session_id
= be64toh(msg
.session_id
);
1741 * Iterate over all streams to set the begin data pending flag. For now, the
1742 * streams are indexed by stream handle so we have to iterate over all
1743 * streams to find the one associated with the right session_id.
1746 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1748 if (stream
->session
->id
== session_id
) {
1749 stream
->data_pending_check_done
= 0;
1750 DBG("Set begin data pending flag to stream %" PRIu64
,
1751 stream
->stream_handle
);
1756 /* All good, send back reply. */
1757 reply
.ret_code
= htobe32(LTTNG_OK
);
1759 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1761 ERR("Relay begin data pending send reply failed");
1769 * End data pending command. This will check, for a given session id, if each
1770 * stream associated with it has its data_pending_check_done flag set. If not,
1771 * this means that the client lost track of the stream but the data is still
1772 * being streamed on our side. In this case, we inform the client that data is
1775 * Return to the client if there is data in flight or not with a ret_code.
1778 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1779 struct relay_command
*cmd
)
1782 struct lttng_ht_iter iter
;
1783 struct lttcomm_relayd_end_data_pending msg
;
1784 struct lttcomm_relayd_generic_reply reply
;
1785 struct relay_stream
*stream
;
1786 uint64_t session_id
;
1787 uint32_t is_data_inflight
= 0;
1792 DBG("End data pending command");
1794 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1795 ERR("Trying to check for data before version check");
1797 goto end_no_session
;
1800 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1801 if (ret
< sizeof(msg
)) {
1803 /* Orderly shutdown. Not necessary to print an error. */
1804 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1806 ERR("Relay didn't receive valid end data_pending struct size: %d",
1810 goto end_no_session
;
1813 session_id
= be64toh(msg
.session_id
);
1815 /* Iterate over all streams to see if the begin data pending flag is set. */
1817 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1819 if (stream
->session
->id
== session_id
&&
1820 !stream
->data_pending_check_done
) {
1821 is_data_inflight
= 1;
1822 DBG("Data is still in flight for stream %" PRIu64
,
1823 stream
->stream_handle
);
1829 /* All good, send back reply. */
1830 reply
.ret_code
= htobe32(is_data_inflight
);
1832 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1834 ERR("Relay end data pending send reply failed");
1842 * Receive an index for a specific stream.
1844 * Return 0 on success else a negative value.
1847 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1848 struct relay_command
*cmd
)
1850 int ret
, send_ret
, index_created
= 0;
1851 struct relay_session
*session
= cmd
->session
;
1852 struct lttcomm_relayd_index index_info
;
1853 struct relay_index
*index
, *wr_index
= NULL
;
1854 struct lttcomm_relayd_generic_reply reply
;
1855 struct relay_stream
*stream
;
1856 uint64_t net_seq_num
;
1860 DBG("Relay receiving index");
1862 if (!session
|| cmd
->version_check_done
== 0) {
1863 ERR("Trying to close a stream before version check");
1865 goto end_no_session
;
1868 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1869 sizeof(index_info
), 0);
1870 if (ret
< sizeof(index_info
)) {
1872 /* Orderly shutdown. Not necessary to print an error. */
1873 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1875 ERR("Relay didn't receive valid index struct size : %d", ret
);
1878 goto end_no_session
;
1881 net_seq_num
= be64toh(index_info
.net_seq_num
);
1884 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1887 goto end_rcu_unlock
;
1890 /* Live beacon handling */
1891 if (index_info
.packet_size
== 0) {
1892 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1895 * Only flag a stream inactive when it has already received data.
1897 if (stream
->total_index_received
> 0) {
1898 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1901 goto end_rcu_unlock
;
1903 stream
->beacon_ts_end
= -1ULL;
1906 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1908 /* A successful creation will add the object to the HT. */
1909 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1911 goto end_rcu_unlock
;
1916 copy_index_control_data(index
, &index_info
);
1918 if (index_created
) {
1920 * Try to add the relay index object to the hash table. If an object
1921 * already exist, destroy back the index created, set the data in this
1922 * object and write it on disk.
1924 relay_index_add(index
, &wr_index
);
1926 copy_index_control_data(wr_index
, &index_info
);
1930 /* The index already exists so write it on disk. */
1934 /* Do we have a writable ready index to write on disk. */
1936 /* Starting at 2.4, create the index file if none available. */
1937 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1938 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1939 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1940 stream
->tracefile_count_current
);
1942 goto end_rcu_unlock
;
1944 stream
->index_fd
= ret
;
1947 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1949 goto end_rcu_unlock
;
1951 stream
->total_index_received
++;
1958 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1960 reply
.ret_code
= htobe32(LTTNG_OK
);
1962 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1964 ERR("Relay sending close index id reply");
1973 * Receive the streams_sent message.
1975 * Return 0 on success else a negative value.
1978 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
1979 struct relay_command
*cmd
)
1982 struct lttcomm_relayd_generic_reply reply
;
1986 DBG("Relay receiving streams_sent");
1988 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1989 ERR("Trying to close a stream before version check");
1991 goto end_no_session
;
1995 * Flag every pending stream in the connection recv list that they are
1996 * ready to be used by the viewer.
1998 set_viewer_ready_flag(cmd
);
2001 * Inform the viewer that there are new streams in the session.
2003 uatomic_set(&cmd
->session
->new_streams
, 1);
2005 reply
.ret_code
= htobe32(LTTNG_OK
);
2006 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2008 ERR("Relay sending sent_stream reply");
2020 * Process the commands received on the control socket
2023 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2024 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
2028 switch (be32toh(recv_hdr
->cmd
)) {
2029 case RELAYD_CREATE_SESSION
:
2030 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
2032 case RELAYD_ADD_STREAM
:
2033 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
2035 case RELAYD_START_DATA
:
2036 ret
= relay_start(recv_hdr
, cmd
);
2038 case RELAYD_SEND_METADATA
:
2039 ret
= relay_recv_metadata(recv_hdr
, cmd
);
2041 case RELAYD_VERSION
:
2042 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
2044 case RELAYD_CLOSE_STREAM
:
2045 ret
= relay_close_stream(recv_hdr
, cmd
);
2047 case RELAYD_DATA_PENDING
:
2048 ret
= relay_data_pending(recv_hdr
, cmd
);
2050 case RELAYD_QUIESCENT_CONTROL
:
2051 ret
= relay_quiescent_control(recv_hdr
, cmd
);
2053 case RELAYD_BEGIN_DATA_PENDING
:
2054 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
2056 case RELAYD_END_DATA_PENDING
:
2057 ret
= relay_end_data_pending(recv_hdr
, cmd
);
2059 case RELAYD_SEND_INDEX
:
2060 ret
= relay_recv_index(recv_hdr
, cmd
);
2062 case RELAYD_STREAMS_SENT
:
2063 ret
= relay_streams_sent(recv_hdr
, cmd
);
2065 case RELAYD_UPDATE_SYNC_INFO
:
2067 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2068 relay_unknown_command(cmd
);
2078 * Handle index for a data stream.
2080 * RCU read side lock MUST be acquired.
2082 * Return 0 on success else a negative value.
2084 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2087 int ret
= 0, index_created
= 0;
2088 uint64_t stream_id
, data_offset
;
2089 struct relay_index
*index
, *wr_index
= NULL
;
2093 stream_id
= stream
->stream_handle
;
2094 /* Get data offset because we are about to update the index. */
2095 data_offset
= htobe64(stream
->tracefile_size_current
);
2098 * Lookup for an existing index for that stream id/sequence number. If on
2099 * exists, the control thread already received the data for it thus we need
2100 * to write it on disk.
2102 index
= relay_index_find(stream_id
, net_seq_num
);
2104 /* A successful creation will add the object to the HT. */
2105 index
= relay_index_create(stream_id
, net_seq_num
);
2113 if (rotate_index
|| stream
->index_fd
< 0) {
2114 index
->to_close_fd
= stream
->index_fd
;
2115 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2116 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2117 stream
->tracefile_count_current
);
2119 /* This will close the stream's index fd if one. */
2120 relay_index_free_safe(index
);
2123 stream
->index_fd
= ret
;
2125 index
->fd
= stream
->index_fd
;
2126 index
->index_data
.offset
= data_offset
;
2128 if (index_created
) {
2130 * Try to add the relay index object to the hash table. If an object
2131 * already exist, destroy back the index created and set the data.
2133 relay_index_add(index
, &wr_index
);
2135 /* Copy back data from the created index. */
2136 wr_index
->fd
= index
->fd
;
2137 wr_index
->to_close_fd
= index
->to_close_fd
;
2138 wr_index
->index_data
.offset
= data_offset
;
2142 /* The index already exists so write it on disk. */
2146 /* Do we have a writable ready index to write on disk. */
2148 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2152 stream
->total_index_received
++;
2160 * relay_process_data: Process the data received on the data socket
2163 int relay_process_data(struct relay_command
*cmd
)
2165 int ret
= 0, rotate_index
= 0;
2167 struct relay_stream
*stream
;
2168 struct lttcomm_relayd_data_hdr data_hdr
;
2170 uint64_t net_seq_num
;
2173 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2174 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2177 /* Orderly shutdown. Not necessary to print an error. */
2178 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2180 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2186 stream_id
= be64toh(data_hdr
.stream_id
);
2189 stream
= relay_stream_find_by_id(stream_id
);
2192 goto end_rcu_unlock
;
2195 data_size
= be32toh(data_hdr
.data_size
);
2196 if (data_buffer_size
< data_size
) {
2199 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2200 if (!tmp_data_ptr
) {
2201 ERR("Allocating data buffer");
2204 goto end_rcu_unlock
;
2206 data_buffer
= tmp_data_ptr
;
2207 data_buffer_size
= data_size
;
2209 memset(data_buffer
, 0, data_size
);
2211 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2213 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2214 data_size
, stream_id
, net_seq_num
);
2215 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2218 /* Orderly shutdown. Not necessary to print an error. */
2219 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2222 goto end_rcu_unlock
;
2225 /* Check if a rotation is needed. */
2226 if (stream
->tracefile_size
> 0 &&
2227 (stream
->tracefile_size_current
+ data_size
) >
2228 stream
->tracefile_size
) {
2229 struct relay_viewer_stream
*vstream
;
2232 new_id
= (stream
->tracefile_count_current
+ 1) %
2233 stream
->tracefile_count
;
2235 * When we wrap-around back to 0, we start overwriting old
2238 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2239 stream
->tracefile_overwrite
= 1;
2241 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2242 if (stream
->tracefile_overwrite
) {
2243 stream
->oldest_tracefile_id
=
2244 (stream
->oldest_tracefile_id
+ 1) %
2245 stream
->tracefile_count
;
2247 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2250 * The viewer is reading a file about to be
2251 * overwritten. Close the FDs it is
2252 * currently using and let it handle the fault.
2254 if (vstream
->tracefile_count_current
== new_id
) {
2255 pthread_mutex_lock(&vstream
->overwrite_lock
);
2256 vstream
->abort_flag
= 1;
2257 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2258 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2259 stream
->channel_name
, new_id
);
2260 } else if (vstream
->tracefile_count_current
==
2261 stream
->tracefile_count_current
) {
2263 * The reader and writer were in the
2264 * same trace file, inform the viewer
2265 * that no new index will ever be added
2268 vstream
->close_write_flag
= 1;
2271 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2272 stream
->tracefile_size
, stream
->tracefile_count
,
2273 relayd_uid
, relayd_gid
, stream
->fd
,
2274 &(stream
->tracefile_count_current
), &stream
->fd
);
2275 stream
->total_index_received
= 0;
2276 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2278 ERR("Rotating stream output file");
2279 goto end_rcu_unlock
;
2281 /* Reset current size because we just perform a stream rotation. */
2282 stream
->tracefile_size_current
= 0;
2287 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2288 * index are NOT supported.
2290 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2291 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2293 goto end_rcu_unlock
;
2297 /* Write data to stream output fd. */
2298 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2299 if (size_ret
< data_size
) {
2300 ERR("Relay error writing data to file");
2302 goto end_rcu_unlock
;
2305 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2306 ret
, stream
->stream_handle
);
2308 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2310 goto end_rcu_unlock
;
2312 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2314 stream
->prev_seq
= net_seq_num
;
2316 /* Check if we need to close the FD */
2317 if (close_stream_check(stream
)) {
2318 destroy_stream(stream
);
2328 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2332 lttng_poll_del(events
, pollfd
);
2334 ret
= close(pollfd
);
2336 ERR("Closing pollfd %d", pollfd
);
2341 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2342 struct lttng_ht
*relay_connections_ht
)
2344 struct relay_command
*relay_connection
;
2347 relay_connection
= zmalloc(sizeof(struct relay_command
));
2348 if (relay_connection
== NULL
) {
2349 PERROR("Relay command zmalloc");
2352 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2353 if (ret
< sizeof(struct relay_command
)) {
2354 PERROR("read relay cmd pipe");
2357 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2360 * Only used by the control side and the reference is copied inside each
2361 * stream from that connection. Thus a destroy HT must be done after every
2362 * stream has been destroyed.
2364 if (relay_connection
->type
== RELAY_CONTROL
) {
2365 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2366 LTTNG_HT_TYPE_STRING
);
2367 if (!relay_connection
->ctf_traces_ht
) {
2372 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2373 (unsigned long) relay_connection
->sock
->fd
);
2375 lttng_ht_add_unique_ulong(relay_connections_ht
,
2376 &relay_connection
->sock_n
);
2378 return lttng_poll_add(events
,
2379 relay_connection
->sock
->fd
,
2380 LPOLLIN
| LPOLLRDHUP
);
2383 free(relay_connection
);
2389 void deferred_free_connection(struct rcu_head
*head
)
2391 struct relay_command
*relay_connection
=
2392 caa_container_of(head
, struct relay_command
, rcu_node
);
2394 lttcomm_destroy_sock(relay_connection
->sock
);
2395 free(relay_connection
);
2399 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2400 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2401 struct lttng_ht
*sessions_ht
)
2405 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2408 if (relay_connection
->type
== RELAY_CONTROL
) {
2409 struct relay_stream_recv_handle
*node
, *tmp_node
;
2411 relay_delete_session(relay_connection
, sessions_ht
);
2412 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2414 /* Clean up recv list. */
2415 cds_list_for_each_entry_safe(node
, tmp_node
,
2416 &relay_connection
->recv_head
, node
) {
2417 cds_list_del(&node
->node
);
2422 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2426 * This thread does the actual work
2429 void *relay_thread_worker(void *data
)
2431 int ret
, err
= -1, last_seen_data_fd
= -1;
2433 struct relay_command
*relay_connection
;
2434 struct lttng_poll_event events
;
2435 struct lttng_ht
*relay_connections_ht
;
2436 struct lttng_ht_node_ulong
*node
;
2437 struct lttng_ht_iter iter
;
2438 struct lttcomm_relayd_hdr recv_hdr
;
2439 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2440 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2442 DBG("[thread] Relay worker started");
2444 rcu_register_thread();
2446 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2448 if (testpoint(relayd_thread_worker
)) {
2449 goto error_testpoint
;
2452 health_code_update();
2454 /* table of connections indexed on socket */
2455 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2456 if (!relay_connections_ht
) {
2457 goto relay_connections_ht_error
;
2460 /* Tables of received indexes indexed by index handle and net_seq_num. */
2461 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2463 goto indexes_ht_error
;
2466 ret
= create_thread_poll_set(&events
, 2);
2468 goto error_poll_create
;
2471 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2478 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2480 health_code_update();
2482 /* Infinite blocking call, waiting for transmission */
2483 DBG3("Relayd worker thread polling...");
2484 health_poll_entry();
2485 ret
= lttng_poll_wait(&events
, -1);
2489 * Restart interrupted system call.
2491 if (errno
== EINTR
) {
2500 * Process control. The control connection is prioritised so we don't
2501 * starve it with high throughout put tracing data on the data
2504 for (i
= 0; i
< nb_fd
; i
++) {
2505 /* Fetch once the poll data */
2506 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2507 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2509 health_code_update();
2511 /* Thread quit pipe has been closed. Killing thread. */
2512 ret
= check_thread_quit_pipe(pollfd
, revents
);
2518 /* Inspect the relay cmd pipe for new connection */
2519 if (pollfd
== relay_cmd_pipe
[0]) {
2520 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2521 ERR("Relay pipe error");
2523 } else if (revents
& LPOLLIN
) {
2524 DBG("Relay command received");
2525 ret
= relay_add_connection(relay_cmd_pipe
[0],
2526 &events
, relay_connections_ht
);
2531 } else if (revents
) {
2533 lttng_ht_lookup(relay_connections_ht
,
2534 (void *)((unsigned long) pollfd
),
2536 node
= lttng_ht_iter_get_node_ulong(&iter
);
2538 DBG2("Relay sock %d not found", pollfd
);
2542 relay_connection
= caa_container_of(node
,
2543 struct relay_command
, sock_n
);
2545 if (revents
& (LPOLLERR
)) {
2547 relay_cleanup_poll_connection(&events
, pollfd
);
2548 relay_del_connection(relay_connections_ht
,
2549 &iter
, relay_connection
, sessions_ht
);
2550 if (last_seen_data_fd
== pollfd
) {
2551 last_seen_data_fd
= last_notdel_data_fd
;
2553 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2554 DBG("Socket %d hung up", pollfd
);
2555 relay_cleanup_poll_connection(&events
, pollfd
);
2556 relay_del_connection(relay_connections_ht
,
2557 &iter
, relay_connection
, sessions_ht
);
2558 if (last_seen_data_fd
== pollfd
) {
2559 last_seen_data_fd
= last_notdel_data_fd
;
2561 } else if (revents
& LPOLLIN
) {
2562 /* control socket */
2563 if (relay_connection
->type
== RELAY_CONTROL
) {
2564 ret
= relay_connection
->sock
->ops
->recvmsg(
2565 relay_connection
->sock
, &recv_hdr
,
2566 sizeof(struct lttcomm_relayd_hdr
), 0);
2567 /* connection closed */
2569 relay_cleanup_poll_connection(&events
, pollfd
);
2570 relay_del_connection(relay_connections_ht
,
2571 &iter
, relay_connection
, sessions_ht
);
2572 DBG("Control connection closed with %d", pollfd
);
2574 if (relay_connection
->session
) {
2575 DBG2("Relay worker receiving data for session : %" PRIu64
,
2576 relay_connection
->session
->id
);
2578 ret
= relay_process_control(&recv_hdr
,
2579 relay_connection
, relay_ctx
);
2581 /* Clear the session on error. */
2582 relay_cleanup_poll_connection(&events
, pollfd
);
2583 relay_del_connection(relay_connections_ht
,
2584 &iter
, relay_connection
, sessions_ht
);
2585 DBG("Connection closed with %d", pollfd
);
2591 * Flag the last seen data fd not deleted. It will be
2592 * used as the last seen fd if any fd gets deleted in
2595 last_notdel_data_fd
= pollfd
;
2603 * The last loop handled a control request, go back to poll to make
2604 * sure we prioritise the control socket.
2610 if (last_seen_data_fd
>= 0) {
2611 for (i
= 0; i
< nb_fd
; i
++) {
2612 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2614 health_code_update();
2616 if (last_seen_data_fd
== pollfd
) {
2623 /* Process data connection. */
2624 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2625 /* Fetch the poll data. */
2626 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2627 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2629 health_code_update();
2631 /* Skip the command pipe. It's handled in the first loop. */
2632 if (pollfd
== relay_cmd_pipe
[0]) {
2638 lttng_ht_lookup(relay_connections_ht
,
2639 (void *)((unsigned long) pollfd
),
2641 node
= lttng_ht_iter_get_node_ulong(&iter
);
2643 /* Skip it. Might be removed before. */
2647 relay_connection
= caa_container_of(node
,
2648 struct relay_command
, sock_n
);
2650 if (revents
& LPOLLIN
) {
2651 if (relay_connection
->type
!= RELAY_DATA
) {
2655 ret
= relay_process_data(relay_connection
);
2656 /* connection closed */
2658 relay_cleanup_poll_connection(&events
, pollfd
);
2659 relay_del_connection(relay_connections_ht
,
2660 &iter
, relay_connection
, sessions_ht
);
2661 DBG("Data connection closed with %d", pollfd
);
2663 * Every goto restart call sets the last seen fd where
2664 * here we don't really care since we gracefully
2665 * continue the loop after the connection is deleted.
2668 /* Keep last seen port. */
2669 last_seen_data_fd
= pollfd
;
2677 last_seen_data_fd
= -1;
2680 /* Normal exit, no error */
2685 lttng_poll_clean(&events
);
2687 /* empty the hash table and free the memory */
2689 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2690 health_code_update();
2692 node
= lttng_ht_iter_get_node_ulong(&iter
);
2694 relay_connection
= caa_container_of(node
,
2695 struct relay_command
, sock_n
);
2696 relay_del_connection(relay_connections_ht
,
2697 &iter
, relay_connection
, sessions_ht
);
2702 lttng_ht_destroy(indexes_ht
);
2704 lttng_ht_destroy(relay_connections_ht
);
2705 relay_connections_ht_error
:
2706 /* Close relay cmd pipes */
2707 utils_close_pipe(relay_cmd_pipe
);
2709 DBG("Thread exited with error");
2711 DBG("Worker thread cleanup complete");
2716 ERR("Health error occurred in %s", __func__
);
2718 health_unregister(health_relayd
);
2719 rcu_unregister_thread();
2725 * Create the relay command pipe to wake thread_manage_apps.
2726 * Closed in cleanup().
2728 static int create_relay_cmd_pipe(void)
2732 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2740 int main(int argc
, char **argv
)
2744 struct relay_local_data
*relay_ctx
;
2746 /* Parse arguments */
2748 if ((ret
= parse_args(argc
, argv
)) < 0) {
2752 if ((ret
= set_signal_handler()) < 0) {
2756 /* Try to create directory if -o, --output is specified. */
2757 if (opt_output_path
) {
2758 if (*opt_output_path
!= '/') {
2759 ERR("Please specify an absolute path for -o, --output PATH");
2763 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2765 ERR("Unable to create %s", opt_output_path
);
2771 if (opt_daemon
|| opt_background
) {
2774 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2781 * We are in the child. Make sure all other file
2782 * descriptors are closed, in case we are called with
2783 * more opened file descriptors than the standard ones.
2785 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2790 /* Create thread quit pipe */
2791 if ((ret
= init_thread_quit_pipe()) < 0) {
2795 /* We need those values for the file/dir creation. */
2796 relayd_uid
= getuid();
2797 relayd_gid
= getgid();
2799 /* Check if daemon is UID = 0 */
2800 if (relayd_uid
== 0) {
2801 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2802 live_uri
->port
< 1024) {
2803 ERR("Need to be root to use ports < 1024");
2809 /* Setup the thread apps communication pipe. */
2810 if ((ret
= create_relay_cmd_pipe()) < 0) {
2814 /* Init relay command queue. */
2815 cds_wfq_init(&relay_cmd_queue
.queue
);
2817 /* Set up max poll set size */
2818 lttng_poll_set_max_size();
2820 /* Initialize communication library */
2822 lttcomm_inet_init();
2824 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2826 PERROR("relay_ctx");
2830 /* tables of sessions indexed by session ID */
2831 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2832 if (!relay_ctx
->sessions_ht
) {
2833 goto exit_relay_ctx_sessions
;
2836 /* tables of streams indexed by stream ID */
2837 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2838 if (!relay_streams_ht
) {
2839 goto exit_relay_ctx_streams
;
2842 /* tables of streams indexed by stream ID */
2843 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2844 if (!viewer_streams_ht
) {
2845 goto exit_relay_ctx_viewer_streams
;
2848 /* Initialize thread health monitoring */
2849 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2850 if (!health_relayd
) {
2851 PERROR("health_app_create error");
2852 goto exit_health_app_create
;
2855 ret
= utils_create_pipe(health_quit_pipe
);
2857 goto error_health_pipe
;
2860 /* Create thread to manage the client socket */
2861 ret
= pthread_create(&health_thread
, NULL
,
2862 thread_manage_health
, (void *) NULL
);
2864 PERROR("pthread_create health");
2868 /* Setup the dispatcher thread */
2869 ret
= pthread_create(&dispatcher_thread
, NULL
,
2870 relay_thread_dispatcher
, (void *) NULL
);
2872 PERROR("pthread_create dispatcher");
2873 goto exit_dispatcher
;
2876 /* Setup the worker thread */
2877 ret
= pthread_create(&worker_thread
, NULL
,
2878 relay_thread_worker
, (void *) relay_ctx
);
2880 PERROR("pthread_create worker");
2884 /* Setup the listener thread */
2885 ret
= pthread_create(&listener_thread
, NULL
,
2886 relay_thread_listener
, (void *) NULL
);
2888 PERROR("pthread_create listener");
2892 ret
= live_start_threads(live_uri
, relay_ctx
);
2894 ERR("Starting live viewer threads");
2899 ret
= pthread_join(listener_thread
, &status
);
2901 PERROR("pthread_join");
2902 goto error
; /* join error, exit without cleanup */
2906 ret
= pthread_join(worker_thread
, &status
);
2908 PERROR("pthread_join");
2909 goto error
; /* join error, exit without cleanup */
2913 ret
= pthread_join(dispatcher_thread
, &status
);
2915 PERROR("pthread_join");
2916 goto error
; /* join error, exit without cleanup */
2920 ret
= pthread_join(health_thread
, &status
);
2922 PERROR("pthread_join health thread");
2923 goto error
; /* join error, exit without cleanup */
2927 * Stop live threads only after joining other threads.
2929 live_stop_threads();
2932 utils_close_pipe(health_quit_pipe
);
2935 health_app_destroy(health_relayd
);
2937 exit_health_app_create
:
2938 lttng_ht_destroy(viewer_streams_ht
);
2940 exit_relay_ctx_viewer_streams
:
2941 lttng_ht_destroy(relay_streams_ht
);
2943 exit_relay_ctx_streams
:
2944 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2946 exit_relay_ctx_sessions
: