2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/defaults.h>
48 #include <common/daemonize.h>
49 #include <common/futex.h>
50 #include <common/sessiond-comm/sessiond-comm.h>
51 #include <common/sessiond-comm/inet.h>
52 #include <common/sessiond-comm/relayd.h>
53 #include <common/uri.h>
54 #include <common/utils.h>
55 #include <common/config/config.h>
58 #include "ctf-trace.h"
61 #include "lttng-relayd.h"
63 #include "health-relayd.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
;
85 static int tracing_group_name_override
;
87 const char * const config_section_name
= "relayd";
90 * Quit pipe for all threads. This permits a single cancellation point
91 * for all threads when receiving an event on the pipe.
93 static int thread_quit_pipe
[2] = { -1, -1 };
96 * This pipe is used to inform the worker thread that a command is queued and
97 * ready to be processed.
99 static int relay_cmd_pipe
[2] = { -1, -1 };
101 /* Shared between threads */
102 static int dispatch_thread_exit
;
104 static pthread_t listener_thread
;
105 static pthread_t dispatcher_thread
;
106 static pthread_t worker_thread
;
107 static pthread_t health_thread
;
109 static uint64_t last_relay_stream_id
;
110 static uint64_t last_relay_session_id
;
113 * Relay command queue.
115 * The relay_thread_listener and relay_thread_dispatcher communicate with this
118 static struct relay_cmd_queue relay_cmd_queue
;
120 /* buffer allocated at startup, used to store the trace data */
121 static char *data_buffer
;
122 static unsigned int data_buffer_size
;
124 /* We need those values for the file/dir creation. */
125 static uid_t relayd_uid
;
126 static gid_t relayd_gid
;
128 /* Global relay stream hash table. */
129 struct lttng_ht
*relay_streams_ht
;
131 /* Global relay viewer stream hash table. */
132 struct lttng_ht
*viewer_streams_ht
;
134 /* Global hash table that stores relay index object. */
135 struct lttng_ht
*indexes_ht
;
137 /* Relayd health monitoring */
138 struct health_app
*health_relayd
;
140 static struct option long_options
[] = {
141 { "control-port", 1, 0, 'C', },
142 { "data-port", 1, 0, 'D', },
143 { "live-port", 1, 0, 'L', },
144 { "daemonize", 0, 0, 'd', },
145 { "background", 0, 0, 'b', },
146 { "group", 1, 0, 'g', },
147 { "help", 0, 0, 'h', },
148 { "output", 1, 0, 'o', },
149 { "verbose", 0, 0, 'v', },
150 { "config", 1, 0, 'f' },
154 static const char *config_ignore_options
[] = { "help", "config" };
157 * usage function on stderr
162 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
163 fprintf(stderr
, " -h, --help Display this usage.\n");
164 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
165 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
166 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
167 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
168 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
169 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
170 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
171 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
172 fprintf(stderr
, " -f --config Load daemon configuration file\n");
176 * Take an option from the getopt output and set it in the right variable to be
179 * Return 0 on success else a negative value.
182 int set_option(int opt
, const char *arg
, const char *optname
)
188 fprintf(stderr
, "option %s", optname
);
190 fprintf(stderr
, " with arg %s\n", arg
);
194 ret
= uri_parse(arg
, &control_uri
);
196 ERR("Invalid control URI specified");
199 if (control_uri
->port
== 0) {
200 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
204 ret
= uri_parse(arg
, &data_uri
);
206 ERR("Invalid data URI specified");
209 if (data_uri
->port
== 0) {
210 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
214 ret
= uri_parse(arg
, &live_uri
);
216 ERR("Invalid live URI specified");
219 if (live_uri
->port
== 0) {
220 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
230 tracing_group_name
= strdup(arg
);
231 tracing_group_name_override
= 1;
237 ret
= asprintf(&opt_output_path
, "%s", arg
);
240 PERROR("asprintf opt_output_path");
245 /* Verbose level can increase using multiple -v */
247 lttng_opt_verbose
= config_parse_value(arg
);
249 lttng_opt_verbose
+= 1;
253 /* Unknown option or other error.
254 * Error is printed by getopt, just return */
267 * config_entry_handler_cb used to handle options read from a config file.
268 * See config_entry_handler_cb comment in common/config/config.h for the
269 * return value conventions.
272 int config_entry_handler(const struct config_entry
*entry
, void *unused
)
276 if (!entry
|| !entry
->name
|| !entry
->value
) {
281 /* Check if the option is to be ignored */
282 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
283 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
288 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
289 /* Ignore if entry name is not fully matched. */
290 if (strcmp(entry
->name
, long_options
[i
].name
)) {
295 * If the option takes no argument on the command line, we have to
296 * check if the value is "true". We support non-zero numeric values,
299 if (!long_options
[i
].has_arg
) {
300 ret
= config_parse_value(entry
->value
);
303 WARN("Invalid configuration value \"%s\" for option %s",
304 entry
->value
, entry
->name
);
306 /* False, skip boolean config option. */
311 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
315 WARN("Unrecognized option \"%s\" in daemon configuration file.",
323 int set_options(int argc
, char **argv
)
325 int c
, ret
= 0, option_index
= 0;
326 int orig_optopt
= optopt
, orig_optind
= optind
;
327 char *default_address
, *optstring
;
328 const char *config_path
= NULL
;
330 optstring
= utils_generate_optstring(long_options
,
331 sizeof(long_options
) / sizeof(struct option
));
337 /* Check for the --config option */
339 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
340 &option_index
)) != -1) {
344 } else if (c
!= 'f') {
348 config_path
= utils_expand_path(optarg
);
350 ERR("Failed to resolve path: %s", optarg
);
354 ret
= config_get_section_entries(config_path
, config_section_name
,
355 config_entry_handler
, NULL
);
358 ERR("Invalid configuration option at line %i", ret
);
364 /* Reset getopt's global state */
365 optopt
= orig_optopt
;
366 optind
= orig_optind
;
368 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
373 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
379 /* assign default values */
380 if (control_uri
== NULL
) {
381 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
382 DEFAULT_NETWORK_CONTROL_PORT
);
384 PERROR("asprintf default data address");
388 ret
= uri_parse(default_address
, &control_uri
);
389 free(default_address
);
391 ERR("Invalid control URI specified");
395 if (data_uri
== NULL
) {
396 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
397 DEFAULT_NETWORK_DATA_PORT
);
399 PERROR("asprintf default data address");
403 ret
= uri_parse(default_address
, &data_uri
);
404 free(default_address
);
406 ERR("Invalid data URI specified");
410 if (live_uri
== NULL
) {
411 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
412 DEFAULT_NETWORK_VIEWER_PORT
);
414 PERROR("asprintf default viewer control address");
418 ret
= uri_parse(default_address
, &live_uri
);
419 free(default_address
);
421 ERR("Invalid viewer control URI specified");
439 /* free the dynamically allocated opt_output_path */
440 free(opt_output_path
);
442 /* Close thread quit pipes */
443 utils_close_pipe(thread_quit_pipe
);
445 uri_free(control_uri
);
447 /* Live URI is freed in the live thread. */
449 if (tracing_group_name_override
) {
450 free((void *) tracing_group_name
);
455 * Write to writable pipe used to notify a thread.
458 int notify_thread_pipe(int wpipe
)
462 ret
= lttng_write(wpipe
, "!", 1);
464 PERROR("write poll pipe");
470 static void notify_health_quit_pipe(int *pipe
)
474 ret
= lttng_write(pipe
[1], "4", 1);
476 PERROR("write relay health quit");
481 * Stop all threads by closing the thread quit pipe.
484 void stop_threads(void)
488 /* Stopping all threads */
489 DBG("Terminating all threads");
490 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
492 ERR("write error on thread quit pipe");
495 notify_health_quit_pipe(health_quit_pipe
);
497 /* Dispatch thread */
498 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
499 futex_nto1_wake(&relay_cmd_queue
.futex
);
503 * Signal handler for the daemon
505 * Simply stop all worker threads, leaving main() return gracefully after
506 * joining all threads and calling cleanup().
509 void sighandler(int sig
)
513 DBG("SIGPIPE caught");
516 DBG("SIGINT caught");
520 DBG("SIGTERM caught");
524 CMM_STORE_SHARED(recv_child_signal
, 1);
532 * Setup signal handler for :
533 * SIGINT, SIGTERM, SIGPIPE
536 int set_signal_handler(void)
542 if ((ret
= sigemptyset(&sigset
)) < 0) {
543 PERROR("sigemptyset");
547 sa
.sa_handler
= sighandler
;
550 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
555 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
560 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
565 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
570 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
575 void lttng_relay_notify_ready(void)
577 /* Notify the parent of the fork() process that we are ready. */
578 if (opt_daemon
|| opt_background
) {
579 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
580 kill(child_ppid
, SIGUSR1
);
586 * Init thread quit pipe.
588 * Return -1 on error or 0 if all pipes are created.
591 int init_thread_quit_pipe(void)
595 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
601 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
604 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
608 if (events
== NULL
|| size
== 0) {
613 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
619 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
631 * Check if the thread quit pipe was triggered.
633 * Return 1 if it was triggered else 0;
636 int check_thread_quit_pipe(int fd
, uint32_t events
)
638 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
646 * Create and init socket from uri.
649 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
652 struct lttcomm_sock
*sock
= NULL
;
654 sock
= lttcomm_alloc_sock_from_uri(uri
);
656 ERR("Allocating socket");
660 ret
= lttcomm_create_sock(sock
);
664 DBG("Listening on sock %d", sock
->fd
);
666 ret
= sock
->ops
->bind(sock
);
671 ret
= sock
->ops
->listen(sock
, -1);
681 lttcomm_destroy_sock(sock
);
687 * Return nonzero if stream needs to be closed.
690 int close_stream_check(struct relay_stream
*stream
)
693 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
695 * We are about to close the stream so set the data pending flag to 1
696 * which will make the end data pending command skip the stream which
697 * is now closed and ready. Note that after proceeding to a file close,
698 * the written file is ready for reading.
700 stream
->data_pending_check_done
= 1;
707 * This thread manages the listening for new connections on the network
710 void *relay_thread_listener(void *data
)
712 int i
, ret
, pollfd
, err
= -1;
714 uint32_t revents
, nb_fd
;
715 struct lttng_poll_event events
;
716 struct lttcomm_sock
*control_sock
, *data_sock
;
718 DBG("[thread] Relay listener started");
720 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
722 health_code_update();
724 control_sock
= relay_init_sock(control_uri
);
726 goto error_sock_control
;
729 data_sock
= relay_init_sock(data_uri
);
731 goto error_sock_relay
;
735 * Pass 3 as size here for the thread quit pipe, control and data socket.
737 ret
= create_thread_poll_set(&events
, 3);
739 goto error_create_poll
;
742 /* Add the control socket */
743 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
748 /* Add the data socket */
749 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
754 lttng_relay_notify_ready();
757 health_code_update();
759 DBG("Listener accepting connections");
763 ret
= lttng_poll_wait(&events
, -1);
767 * Restart interrupted system call.
769 if (errno
== EINTR
) {
777 DBG("Relay new connection received");
778 for (i
= 0; i
< nb_fd
; i
++) {
779 health_code_update();
781 /* Fetch once the poll data */
782 revents
= LTTNG_POLL_GETEV(&events
, i
);
783 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
785 /* Thread quit pipe has been closed. Killing thread. */
786 ret
= check_thread_quit_pipe(pollfd
, revents
);
792 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
793 ERR("socket poll error");
795 } else if (revents
& LPOLLIN
) {
797 * Get allocated in this thread,
798 * enqueued to a global queue, dequeued
799 * and freed in the worker thread.
801 struct relay_command
*relay_cmd
;
802 struct lttcomm_sock
*newsock
;
804 relay_cmd
= zmalloc(sizeof(struct relay_command
));
805 if (relay_cmd
== NULL
) {
806 PERROR("relay command zmalloc");
810 if (pollfd
== data_sock
->fd
) {
811 newsock
= data_sock
->ops
->accept(data_sock
);
813 PERROR("accepting data sock");
817 relay_cmd
->type
= RELAY_DATA
;
818 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
820 assert(pollfd
== control_sock
->fd
);
821 newsock
= control_sock
->ops
->accept(control_sock
);
823 PERROR("accepting control sock");
827 relay_cmd
->type
= RELAY_CONTROL
;
828 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
830 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
833 PERROR("setsockopt inet");
834 lttcomm_destroy_sock(newsock
);
838 relay_cmd
->sock
= newsock
;
840 * Lock free enqueue the request.
842 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
845 * Wake the dispatch queue futex. Implicit memory
846 * barrier with the exchange in cds_wfq_enqueue.
848 futex_nto1_wake(&relay_cmd_queue
.futex
);
856 lttng_poll_clean(&events
);
858 if (data_sock
->fd
>= 0) {
859 ret
= data_sock
->ops
->close(data_sock
);
864 lttcomm_destroy_sock(data_sock
);
866 if (control_sock
->fd
>= 0) {
867 ret
= control_sock
->ops
->close(control_sock
);
872 lttcomm_destroy_sock(control_sock
);
876 ERR("Health error occurred in %s", __func__
);
878 health_unregister(health_relayd
);
879 DBG("Relay listener thread cleanup complete");
885 * This thread manages the dispatching of the requests to worker threads
888 void *relay_thread_dispatcher(void *data
)
892 struct cds_wfq_node
*node
;
893 struct relay_command
*relay_cmd
= NULL
;
895 DBG("[thread] Relay dispatcher started");
897 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
899 health_code_update();
901 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
902 health_code_update();
904 /* Atomically prepare the queue futex */
905 futex_nto1_prepare(&relay_cmd_queue
.futex
);
908 health_code_update();
910 /* Dequeue commands */
911 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
913 DBG("Woken up but nothing in the relay command queue");
914 /* Continue thread execution */
918 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
919 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
922 * Inform worker thread of the new request. This
923 * call is blocking so we can be assured that the data will be read
924 * at some point in time or wait to the end of the world :)
926 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
927 sizeof(struct relay_command
));
929 if (ret
< sizeof(struct relay_command
)) {
930 PERROR("write cmd pipe");
933 } while (node
!= NULL
);
935 /* Futex wait on queue. Blocking call on futex() */
937 futex_nto1_wait(&relay_cmd_queue
.futex
);
941 /* Normal exit, no error */
947 ERR("Health error occurred in %s", __func__
);
949 health_unregister(health_relayd
);
950 DBG("Dispatch thread dying");
956 * Get stream from stream id.
957 * Need to be called with RCU read-side lock held.
959 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
961 struct lttng_ht_node_ulong
*node
;
962 struct lttng_ht_iter iter
;
963 struct relay_stream
*ret
;
965 lttng_ht_lookup(relay_streams_ht
,
966 (void *)((unsigned long) stream_id
),
968 node
= lttng_ht_iter_get_node_ulong(&iter
);
970 DBG("Relay stream %" PRIu64
" not found", stream_id
);
975 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
982 void deferred_free_stream(struct rcu_head
*head
)
984 struct relay_stream
*stream
=
985 caa_container_of(head
, struct relay_stream
, rcu_node
);
987 free(stream
->path_name
);
988 free(stream
->channel_name
);
993 void deferred_free_session(struct rcu_head
*head
)
995 struct relay_session
*session
=
996 caa_container_of(head
, struct relay_session
, rcu_node
);
1001 * Close a given stream. The stream is freed using a call RCU.
1003 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
1004 * BEFORE the stream lock MUST be acquired.
1006 static void destroy_stream(struct relay_stream
*stream
)
1009 struct relay_viewer_stream
*vstream
;
1010 struct lttng_ht_iter iter
;
1014 delret
= close(stream
->fd
);
1016 PERROR("close stream");
1019 if (stream
->index_fd
>= 0) {
1020 delret
= close(stream
->index_fd
);
1022 PERROR("close stream index_fd");
1026 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
1029 * Set the last good value into the viewer stream. This is done
1030 * right before the stream gets deleted from the hash table. The
1031 * lookup failure on the live thread side of a stream indicates
1032 * that the viewer stream index received value should be used.
1034 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
1035 vstream
->total_index_received
= stream
->total_index_received
;
1036 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
1037 vstream
->close_write_flag
= 1;
1038 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
1041 /* Cleanup index of that stream. */
1042 relay_index_destroy_by_stream_id(stream
->stream_handle
);
1044 iter
.iter
.node
= &stream
->stream_n
.node
;
1045 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
1047 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
1048 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
1051 if (stream
->ctf_trace
) {
1052 ctf_trace_try_destroy(stream
->ctf_trace
);
1055 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
1056 DBG("Closed tracefile %d from close stream", stream
->fd
);
1060 * relay_delete_session: Free all memory associated with a session and
1064 void relay_delete_session(struct relay_command
*cmd
,
1065 struct lttng_ht
*sessions_ht
)
1067 struct lttng_ht_iter iter
;
1068 struct lttng_ht_node_ulong
*node
;
1069 struct relay_stream
*stream
;
1072 if (!cmd
->session
) {
1076 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
1079 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1080 node
= lttng_ht_iter_get_node_ulong(&iter
);
1084 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1085 if (stream
->session
== cmd
->session
) {
1086 destroy_stream(stream
);
1087 cmd
->session
->stream_count
--;
1088 assert(cmd
->session
->stream_count
>= 0);
1092 /* Make this session not visible anymore. */
1093 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
1094 ret
= lttng_ht_del(sessions_ht
, &iter
);
1096 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
1101 * Copy index data from the control port to a given index object.
1103 static void copy_index_control_data(struct relay_index
*index
,
1104 struct lttcomm_relayd_index
*data
)
1110 * The index on disk is encoded in big endian, so we don't need to convert
1111 * the data received on the network. The data_offset value is NEVER
1112 * modified here and is updated by the data thread.
1114 index
->index_data
.packet_size
= data
->packet_size
;
1115 index
->index_data
.content_size
= data
->content_size
;
1116 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
1117 index
->index_data
.timestamp_end
= data
->timestamp_end
;
1118 index
->index_data
.events_discarded
= data
->events_discarded
;
1119 index
->index_data
.stream_id
= data
->stream_id
;
1123 * Handle the RELAYD_CREATE_SESSION command.
1125 * On success, send back the session id or else return a negative value.
1128 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1129 struct relay_command
*cmd
,
1130 struct lttng_ht
*sessions_ht
)
1132 int ret
= 0, send_ret
;
1133 struct relay_session
*session
;
1134 struct lttcomm_relayd_status_session reply
;
1139 memset(&reply
, 0, sizeof(reply
));
1141 session
= zmalloc(sizeof(struct relay_session
));
1142 if (session
== NULL
) {
1143 PERROR("relay session zmalloc");
1148 session
->id
= ++last_relay_session_id
;
1149 session
->sock
= cmd
->sock
;
1150 session
->minor
= cmd
->minor
;
1151 session
->major
= cmd
->major
;
1152 cmd
->session
= session
;
1154 reply
.session_id
= htobe64(session
->id
);
1156 switch (cmd
->minor
) {
1157 case 4: /* LTTng sessiond 2.4 */
1159 ret
= cmd_create_session_2_4(cmd
, session
);
1163 lttng_ht_node_init_ulong(&session
->session_n
,
1164 (unsigned long) session
->id
);
1165 lttng_ht_add_unique_ulong(sessions_ht
,
1166 &session
->session_n
);
1168 DBG("Created session %" PRIu64
, session
->id
);
1172 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1174 reply
.ret_code
= htobe32(LTTNG_OK
);
1177 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1179 ERR("Relayd sending session id");
1187 * When we have received all the streams and the metadata for a channel,
1188 * we make them visible to the viewer threads.
1191 void set_viewer_ready_flag(struct relay_command
*cmd
)
1193 struct relay_stream_recv_handle
*node
, *tmp_node
;
1195 cds_list_for_each_entry_safe(node
, tmp_node
, &cmd
->recv_head
, node
) {
1196 struct relay_stream
*stream
;
1199 stream
= relay_stream_find_by_id(node
->id
);
1202 * Stream is most probably being cleaned up by the data thread thus
1203 * simply continue to the next one.
1209 stream
->viewer_ready
= 1;
1212 /* Clean stream handle node. */
1213 cds_list_del(&node
->node
);
1221 * Add a recv handle node to the connection recv list with the given stream
1222 * handle. A new node is allocated thus must be freed when the node is deleted
1225 static void queue_stream_handle(uint64_t handle
, struct relay_command
*cmd
)
1227 struct relay_stream_recv_handle
*node
;
1231 node
= zmalloc(sizeof(*node
));
1233 PERROR("zmalloc queue stream handle");
1238 cds_list_add(&node
->node
, &cmd
->recv_head
);
1242 * relay_add_stream: allocate a new stream for a session
1245 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1246 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1248 struct relay_session
*session
= cmd
->session
;
1249 struct relay_stream
*stream
= NULL
;
1250 struct lttcomm_relayd_status_stream reply
;
1253 if (!session
|| cmd
->version_check_done
== 0) {
1254 ERR("Trying to add a stream before version check");
1256 goto end_no_session
;
1259 stream
= zmalloc(sizeof(struct relay_stream
));
1260 if (stream
== NULL
) {
1261 PERROR("relay stream zmalloc");
1263 goto end_no_session
;
1266 switch (cmd
->minor
) {
1267 case 1: /* LTTng sessiond 2.1 */
1268 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1270 case 2: /* LTTng sessiond 2.2 */
1272 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1276 goto err_free_stream
;
1280 stream
->stream_handle
= ++last_relay_stream_id
;
1281 stream
->prev_seq
= -1ULL;
1282 stream
->session
= session
;
1283 stream
->index_fd
= -1;
1284 stream
->read_index_fd
= -1;
1285 stream
->ctf_trace
= NULL
;
1286 pthread_mutex_init(&stream
->lock
, NULL
);
1288 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1290 ERR("relay creating output directory");
1295 * No need to use run_as API here because whatever we receives, the relayd
1296 * uses its own credentials for the stream files.
1298 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1299 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1301 ERR("Create output file");
1305 if (stream
->tracefile_size
) {
1306 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1308 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1311 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1312 stream
->metadata_flag
= 1;
1314 * When we receive a new metadata stream, we create a new
1315 * ctf_trace and we assign this ctf_trace to all streams with
1318 * If later on we receive a new stream for the same ctf_trace,
1319 * we copy the information from the first hit in the HT to the
1322 stream
->ctf_trace
= ctf_trace_create();
1323 if (!stream
->ctf_trace
) {
1327 stream
->ctf_trace
->refcount
++;
1328 stream
->ctf_trace
->metadata_stream
= stream
;
1330 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1331 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1334 * Add the stream handle in the recv list of the connection. Once the end
1335 * stream message is received, this list is emptied and streams are set
1336 * with the viewer ready flag.
1338 if (stream
->metadata_flag
) {
1339 stream
->viewer_ready
= 1;
1341 queue_stream_handle(stream
->stream_handle
, cmd
);
1344 lttng_ht_node_init_ulong(&stream
->stream_n
,
1345 (unsigned long) stream
->stream_handle
);
1346 lttng_ht_add_unique_ulong(relay_streams_ht
,
1349 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1350 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1351 session
->stream_count
++;
1353 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1354 stream
->stream_handle
);
1357 reply
.handle
= htobe64(stream
->stream_handle
);
1358 /* send the session id to the client or a negative return code on error */
1360 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1361 /* stream was not properly added to the ht, so free it */
1364 reply
.ret_code
= htobe32(LTTNG_OK
);
1367 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1368 sizeof(struct lttcomm_relayd_status_stream
), 0);
1370 ERR("Relay sending stream id");
1379 free(stream
->path_name
);
1380 free(stream
->channel_name
);
1386 * relay_close_stream: close a specific stream
1389 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1390 struct relay_command
*cmd
)
1393 struct relay_session
*session
= cmd
->session
;
1394 struct lttcomm_relayd_close_stream stream_info
;
1395 struct lttcomm_relayd_generic_reply reply
;
1396 struct relay_stream
*stream
;
1398 DBG("Close stream received");
1400 if (!session
|| cmd
->version_check_done
== 0) {
1401 ERR("Trying to close a stream before version check");
1403 goto end_no_session
;
1406 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1407 sizeof(struct lttcomm_relayd_close_stream
), 0);
1408 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1410 /* Orderly shutdown. Not necessary to print an error. */
1411 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1413 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1416 goto end_no_session
;
1420 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1426 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1427 stream
->close_flag
= 1;
1428 session
->stream_count
--;
1429 assert(session
->stream_count
>= 0);
1431 if (close_stream_check(stream
)) {
1432 destroy_stream(stream
);
1439 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1441 reply
.ret_code
= htobe32(LTTNG_OK
);
1443 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1444 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1446 ERR("Relay sending stream id");
1455 * relay_unknown_command: send -1 if received unknown command
1458 void relay_unknown_command(struct relay_command
*cmd
)
1460 struct lttcomm_relayd_generic_reply reply
;
1463 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1464 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1465 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1467 ERR("Relay sending unknown command");
1472 * relay_start: send an acknowledgment to the client to tell if we are
1473 * ready to receive data. We are ready if a session is established.
1476 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1477 struct relay_command
*cmd
)
1479 int ret
= htobe32(LTTNG_OK
);
1480 struct lttcomm_relayd_generic_reply reply
;
1481 struct relay_session
*session
= cmd
->session
;
1484 DBG("Trying to start the streaming without a session established");
1485 ret
= htobe32(LTTNG_ERR_UNK
);
1488 reply
.ret_code
= ret
;
1489 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1490 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1492 ERR("Relay sending start ack");
1499 * Append padding to the file pointed by the file descriptor fd.
1501 static int write_padding_to_file(int fd
, uint32_t size
)
1510 zeros
= zmalloc(size
);
1511 if (zeros
== NULL
) {
1512 PERROR("zmalloc zeros for padding");
1517 ret
= lttng_write(fd
, zeros
, size
);
1519 PERROR("write padding to file");
1529 * relay_recv_metadata: receive the metada for the session.
1532 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1533 struct relay_command
*cmd
)
1535 int ret
= htobe32(LTTNG_OK
);
1537 struct relay_session
*session
= cmd
->session
;
1538 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1539 struct relay_stream
*metadata_stream
;
1540 uint64_t data_size
, payload_size
;
1543 ERR("Metadata sent before version check");
1548 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1549 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1550 ERR("Incorrect data size");
1554 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1556 if (data_buffer_size
< data_size
) {
1557 /* In case the realloc fails, we can free the memory */
1560 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1561 if (!tmp_data_ptr
) {
1562 ERR("Allocating data buffer");
1567 data_buffer
= tmp_data_ptr
;
1568 data_buffer_size
= data_size
;
1570 memset(data_buffer
, 0, data_size
);
1571 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1572 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1573 if (ret
< 0 || ret
!= data_size
) {
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 the whole metadata");
1583 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1586 metadata_stream
= relay_stream_find_by_id(
1587 be64toh(metadata_struct
->stream_id
));
1588 if (!metadata_stream
) {
1593 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1595 if (size_ret
< payload_size
) {
1596 ERR("Relay error writing metadata on file");
1601 ret
= write_padding_to_file(metadata_stream
->fd
,
1602 be32toh(metadata_struct
->padding_size
));
1606 metadata_stream
->ctf_trace
->metadata_received
+=
1607 payload_size
+ be32toh(metadata_struct
->padding_size
);
1609 DBG2("Relay metadata written");
1618 * relay_send_version: send relayd version number
1621 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1622 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1625 struct lttcomm_relayd_version reply
, msg
;
1629 cmd
->version_check_done
= 1;
1631 /* Get version from the other side. */
1632 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1633 if (ret
< 0 || ret
!= sizeof(msg
)) {
1635 /* Orderly shutdown. Not necessary to print an error. */
1636 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1638 ERR("Relay failed to receive the version values.");
1644 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1645 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1647 /* Major versions must be the same */
1648 if (reply
.major
!= be32toh(msg
.major
)) {
1649 DBG("Incompatible major versions (%u vs %u), deleting session",
1650 reply
.major
, be32toh(msg
.major
));
1651 relay_delete_session(cmd
, sessions_ht
);
1656 cmd
->major
= reply
.major
;
1657 /* We adapt to the lowest compatible version */
1658 if (reply
.minor
<= be32toh(msg
.minor
)) {
1659 cmd
->minor
= reply
.minor
;
1661 cmd
->minor
= be32toh(msg
.minor
);
1664 reply
.major
= htobe32(reply
.major
);
1665 reply
.minor
= htobe32(reply
.minor
);
1666 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1667 sizeof(struct lttcomm_relayd_version
), 0);
1669 ERR("Relay sending version");
1672 DBG("Version check done using protocol %u.%u", cmd
->major
,
1680 * Check for data pending for a given stream id from the session daemon.
1683 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1684 struct relay_command
*cmd
)
1686 struct relay_session
*session
= cmd
->session
;
1687 struct lttcomm_relayd_data_pending msg
;
1688 struct lttcomm_relayd_generic_reply reply
;
1689 struct relay_stream
*stream
;
1691 uint64_t last_net_seq_num
, stream_id
;
1693 DBG("Data pending command received");
1695 if (!session
|| cmd
->version_check_done
== 0) {
1696 ERR("Trying to check for data before version check");
1698 goto end_no_session
;
1701 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1702 if (ret
< sizeof(msg
)) {
1704 /* Orderly shutdown. Not necessary to print an error. */
1705 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1707 ERR("Relay didn't receive valid data_pending struct size : %d",
1711 goto end_no_session
;
1714 stream_id
= be64toh(msg
.stream_id
);
1715 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1718 stream
= relay_stream_find_by_id(stream_id
);
1719 if (stream
== NULL
) {
1724 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1725 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1728 /* Avoid wrapping issue */
1729 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1730 /* Data has in fact been written and is NOT pending */
1733 /* Data still being streamed thus pending */
1737 /* Pending check is now done. */
1738 stream
->data_pending_check_done
= 1;
1743 reply
.ret_code
= htobe32(ret
);
1744 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1746 ERR("Relay data pending ret code failed");
1754 * Wait for the control socket to reach a quiescent state.
1756 * Note that for now, when receiving this command from the session daemon, this
1757 * means that every subsequent commands or data received on the control socket
1758 * has been handled. So, this is why we simply return OK here.
1761 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1762 struct relay_command
*cmd
)
1766 struct relay_stream
*stream
;
1767 struct lttng_ht_iter iter
;
1768 struct lttcomm_relayd_quiescent_control msg
;
1769 struct lttcomm_relayd_generic_reply reply
;
1771 DBG("Checking quiescent state on control socket");
1773 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1774 ERR("Trying to check for data before version check");
1776 goto end_no_session
;
1779 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1780 if (ret
< sizeof(msg
)) {
1782 /* Orderly shutdown. Not necessary to print an error. */
1783 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1785 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1789 goto end_no_session
;
1792 stream_id
= be64toh(msg
.stream_id
);
1795 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1797 if (stream
->stream_handle
== stream_id
) {
1798 stream
->data_pending_check_done
= 1;
1799 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1806 reply
.ret_code
= htobe32(LTTNG_OK
);
1807 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1809 ERR("Relay data quiescent control ret code failed");
1817 * Initialize a data pending command. This means that a client is about to ask
1818 * for data pending for each stream he/she holds. Simply iterate over all
1819 * streams of a session and set the data_pending_check_done flag.
1821 * This command returns to the client a LTTNG_OK code.
1824 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1825 struct relay_command
*cmd
)
1828 struct lttng_ht_iter iter
;
1829 struct lttcomm_relayd_begin_data_pending msg
;
1830 struct lttcomm_relayd_generic_reply reply
;
1831 struct relay_stream
*stream
;
1832 uint64_t session_id
;
1837 DBG("Init streams for data pending");
1839 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1840 ERR("Trying to check for data before version check");
1842 goto end_no_session
;
1845 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1846 if (ret
< sizeof(msg
)) {
1848 /* Orderly shutdown. Not necessary to print an error. */
1849 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1851 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1855 goto end_no_session
;
1858 session_id
= be64toh(msg
.session_id
);
1861 * Iterate over all streams to set the begin data pending flag. For now, the
1862 * streams are indexed by stream handle so we have to iterate over all
1863 * streams to find the one associated with the right session_id.
1866 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1868 if (stream
->session
->id
== session_id
) {
1869 stream
->data_pending_check_done
= 0;
1870 DBG("Set begin data pending flag to stream %" PRIu64
,
1871 stream
->stream_handle
);
1876 /* All good, send back reply. */
1877 reply
.ret_code
= htobe32(LTTNG_OK
);
1879 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1881 ERR("Relay begin data pending send reply failed");
1889 * End data pending command. This will check, for a given session id, if each
1890 * stream associated with it has its data_pending_check_done flag set. If not,
1891 * this means that the client lost track of the stream but the data is still
1892 * being streamed on our side. In this case, we inform the client that data is
1895 * Return to the client if there is data in flight or not with a ret_code.
1898 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1899 struct relay_command
*cmd
)
1902 struct lttng_ht_iter iter
;
1903 struct lttcomm_relayd_end_data_pending msg
;
1904 struct lttcomm_relayd_generic_reply reply
;
1905 struct relay_stream
*stream
;
1906 uint64_t session_id
;
1907 uint32_t is_data_inflight
= 0;
1912 DBG("End data pending command");
1914 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1915 ERR("Trying to check for data before version check");
1917 goto end_no_session
;
1920 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1921 if (ret
< sizeof(msg
)) {
1923 /* Orderly shutdown. Not necessary to print an error. */
1924 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1926 ERR("Relay didn't receive valid end data_pending struct size: %d",
1930 goto end_no_session
;
1933 session_id
= be64toh(msg
.session_id
);
1935 /* Iterate over all streams to see if the begin data pending flag is set. */
1937 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1939 if (stream
->session
->id
== session_id
&&
1940 !stream
->data_pending_check_done
) {
1941 is_data_inflight
= 1;
1942 DBG("Data is still in flight for stream %" PRIu64
,
1943 stream
->stream_handle
);
1949 /* All good, send back reply. */
1950 reply
.ret_code
= htobe32(is_data_inflight
);
1952 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1954 ERR("Relay end data pending send reply failed");
1962 * Receive an index for a specific stream.
1964 * Return 0 on success else a negative value.
1967 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1968 struct relay_command
*cmd
)
1970 int ret
, send_ret
, index_created
= 0;
1971 struct relay_session
*session
= cmd
->session
;
1972 struct lttcomm_relayd_index index_info
;
1973 struct relay_index
*index
, *wr_index
= NULL
;
1974 struct lttcomm_relayd_generic_reply reply
;
1975 struct relay_stream
*stream
;
1976 uint64_t net_seq_num
;
1980 DBG("Relay receiving index");
1982 if (!session
|| cmd
->version_check_done
== 0) {
1983 ERR("Trying to close a stream before version check");
1985 goto end_no_session
;
1988 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1989 sizeof(index_info
), 0);
1990 if (ret
< sizeof(index_info
)) {
1992 /* Orderly shutdown. Not necessary to print an error. */
1993 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1995 ERR("Relay didn't receive valid index struct size : %d", ret
);
1998 goto end_no_session
;
2001 net_seq_num
= be64toh(index_info
.net_seq_num
);
2004 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
2007 goto end_rcu_unlock
;
2010 /* Live beacon handling */
2011 if (index_info
.packet_size
== 0) {
2012 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
2015 * Only flag a stream inactive when it has already received data.
2017 if (stream
->total_index_received
> 0) {
2018 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
2021 goto end_rcu_unlock
;
2023 stream
->beacon_ts_end
= -1ULL;
2026 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
2028 /* A successful creation will add the object to the HT. */
2029 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
2031 goto end_rcu_unlock
;
2036 copy_index_control_data(index
, &index_info
);
2038 if (index_created
) {
2040 * Try to add the relay index object to the hash table. If an object
2041 * already exist, destroy back the index created, set the data in this
2042 * object and write it on disk.
2044 relay_index_add(index
, &wr_index
);
2046 copy_index_control_data(wr_index
, &index_info
);
2050 /* The index already exists so write it on disk. */
2054 /* Do we have a writable ready index to write on disk. */
2056 /* Starting at 2.4, create the index file if none available. */
2057 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
2058 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2059 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2060 stream
->tracefile_count_current
);
2062 goto end_rcu_unlock
;
2064 stream
->index_fd
= ret
;
2067 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2069 goto end_rcu_unlock
;
2071 stream
->total_index_received
++;
2078 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2080 reply
.ret_code
= htobe32(LTTNG_OK
);
2082 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2084 ERR("Relay sending close index id reply");
2093 * Receive the streams_sent message.
2095 * Return 0 on success else a negative value.
2098 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2099 struct relay_command
*cmd
)
2102 struct lttcomm_relayd_generic_reply reply
;
2106 DBG("Relay receiving streams_sent");
2108 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
2109 ERR("Trying to close a stream before version check");
2111 goto end_no_session
;
2115 * Flag every pending stream in the connection recv list that they are
2116 * ready to be used by the viewer.
2118 set_viewer_ready_flag(cmd
);
2120 reply
.ret_code
= htobe32(LTTNG_OK
);
2121 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2123 ERR("Relay sending sent_stream reply");
2135 * Process the commands received on the control socket
2138 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2139 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
2143 switch (be32toh(recv_hdr
->cmd
)) {
2144 case RELAYD_CREATE_SESSION
:
2145 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
2147 case RELAYD_ADD_STREAM
:
2148 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
2150 case RELAYD_START_DATA
:
2151 ret
= relay_start(recv_hdr
, cmd
);
2153 case RELAYD_SEND_METADATA
:
2154 ret
= relay_recv_metadata(recv_hdr
, cmd
);
2156 case RELAYD_VERSION
:
2157 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
2159 case RELAYD_CLOSE_STREAM
:
2160 ret
= relay_close_stream(recv_hdr
, cmd
);
2162 case RELAYD_DATA_PENDING
:
2163 ret
= relay_data_pending(recv_hdr
, cmd
);
2165 case RELAYD_QUIESCENT_CONTROL
:
2166 ret
= relay_quiescent_control(recv_hdr
, cmd
);
2168 case RELAYD_BEGIN_DATA_PENDING
:
2169 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
2171 case RELAYD_END_DATA_PENDING
:
2172 ret
= relay_end_data_pending(recv_hdr
, cmd
);
2174 case RELAYD_SEND_INDEX
:
2175 ret
= relay_recv_index(recv_hdr
, cmd
);
2177 case RELAYD_STREAMS_SENT
:
2178 ret
= relay_streams_sent(recv_hdr
, cmd
);
2180 case RELAYD_UPDATE_SYNC_INFO
:
2182 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2183 relay_unknown_command(cmd
);
2193 * Handle index for a data stream.
2195 * RCU read side lock MUST be acquired.
2197 * Return 0 on success else a negative value.
2199 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2202 int ret
= 0, index_created
= 0;
2203 uint64_t stream_id
, data_offset
;
2204 struct relay_index
*index
, *wr_index
= NULL
;
2208 stream_id
= stream
->stream_handle
;
2209 /* Get data offset because we are about to update the index. */
2210 data_offset
= htobe64(stream
->tracefile_size_current
);
2213 * Lookup for an existing index for that stream id/sequence number. If on
2214 * exists, the control thread already received the data for it thus we need
2215 * to write it on disk.
2217 index
= relay_index_find(stream_id
, net_seq_num
);
2219 /* A successful creation will add the object to the HT. */
2220 index
= relay_index_create(stream_id
, net_seq_num
);
2228 if (rotate_index
|| stream
->index_fd
< 0) {
2229 index
->to_close_fd
= stream
->index_fd
;
2230 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2231 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2232 stream
->tracefile_count_current
);
2234 /* This will close the stream's index fd if one. */
2235 relay_index_free_safe(index
);
2238 stream
->index_fd
= ret
;
2240 index
->fd
= stream
->index_fd
;
2241 index
->index_data
.offset
= data_offset
;
2243 if (index_created
) {
2245 * Try to add the relay index object to the hash table. If an object
2246 * already exist, destroy back the index created and set the data.
2248 relay_index_add(index
, &wr_index
);
2250 /* Copy back data from the created index. */
2251 wr_index
->fd
= index
->fd
;
2252 wr_index
->to_close_fd
= index
->to_close_fd
;
2253 wr_index
->index_data
.offset
= data_offset
;
2257 /* The index already exists so write it on disk. */
2261 /* Do we have a writable ready index to write on disk. */
2263 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2267 stream
->total_index_received
++;
2275 * relay_process_data: Process the data received on the data socket
2278 int relay_process_data(struct relay_command
*cmd
)
2280 int ret
= 0, rotate_index
= 0;
2282 struct relay_stream
*stream
;
2283 struct lttcomm_relayd_data_hdr data_hdr
;
2285 uint64_t net_seq_num
;
2288 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2289 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2292 /* Orderly shutdown. Not necessary to print an error. */
2293 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2295 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2301 stream_id
= be64toh(data_hdr
.stream_id
);
2304 stream
= relay_stream_find_by_id(stream_id
);
2307 goto end_rcu_unlock
;
2310 data_size
= be32toh(data_hdr
.data_size
);
2311 if (data_buffer_size
< data_size
) {
2314 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2315 if (!tmp_data_ptr
) {
2316 ERR("Allocating data buffer");
2319 goto end_rcu_unlock
;
2321 data_buffer
= tmp_data_ptr
;
2322 data_buffer_size
= data_size
;
2324 memset(data_buffer
, 0, data_size
);
2326 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2328 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2329 data_size
, stream_id
, net_seq_num
);
2330 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2333 /* Orderly shutdown. Not necessary to print an error. */
2334 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2337 goto end_rcu_unlock
;
2340 /* Check if a rotation is needed. */
2341 if (stream
->tracefile_size
> 0 &&
2342 (stream
->tracefile_size_current
+ data_size
) >
2343 stream
->tracefile_size
) {
2344 struct relay_viewer_stream
*vstream
;
2347 new_id
= (stream
->tracefile_count_current
+ 1) %
2348 stream
->tracefile_count
;
2350 * When we wrap-around back to 0, we start overwriting old
2353 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2354 stream
->tracefile_overwrite
= 1;
2356 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2357 if (stream
->tracefile_overwrite
) {
2358 stream
->oldest_tracefile_id
=
2359 (stream
->oldest_tracefile_id
+ 1) %
2360 stream
->tracefile_count
;
2362 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
2365 * The viewer is reading a file about to be
2366 * overwritten. Close the FDs it is
2367 * currently using and let it handle the fault.
2369 if (vstream
->tracefile_count_current
== new_id
) {
2370 pthread_mutex_lock(&vstream
->overwrite_lock
);
2371 vstream
->abort_flag
= 1;
2372 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2373 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2374 stream
->channel_name
, new_id
);
2375 } else if (vstream
->tracefile_count_current
==
2376 stream
->tracefile_count_current
) {
2378 * The reader and writer were in the
2379 * same trace file, inform the viewer
2380 * that no new index will ever be added
2383 vstream
->close_write_flag
= 1;
2386 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2387 stream
->tracefile_size
, stream
->tracefile_count
,
2388 relayd_uid
, relayd_gid
, stream
->fd
,
2389 &(stream
->tracefile_count_current
), &stream
->fd
);
2390 stream
->total_index_received
= 0;
2391 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2393 ERR("Rotating stream output file");
2394 goto end_rcu_unlock
;
2396 /* Reset current size because we just perform a stream rotation. */
2397 stream
->tracefile_size_current
= 0;
2402 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2403 * index are NOT supported.
2405 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2406 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2408 goto end_rcu_unlock
;
2412 /* Write data to stream output fd. */
2413 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2414 if (size_ret
< data_size
) {
2415 ERR("Relay error writing data to file");
2417 goto end_rcu_unlock
;
2420 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2421 ret
, stream
->stream_handle
);
2423 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2425 goto end_rcu_unlock
;
2427 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2429 stream
->prev_seq
= net_seq_num
;
2431 /* Check if we need to close the FD */
2432 if (close_stream_check(stream
)) {
2433 destroy_stream(stream
);
2443 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2447 lttng_poll_del(events
, pollfd
);
2449 ret
= close(pollfd
);
2451 ERR("Closing pollfd %d", pollfd
);
2456 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2457 struct lttng_ht
*relay_connections_ht
)
2459 struct relay_command
*relay_connection
;
2462 relay_connection
= zmalloc(sizeof(struct relay_command
));
2463 if (relay_connection
== NULL
) {
2464 PERROR("Relay command zmalloc");
2467 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2468 if (ret
< sizeof(struct relay_command
)) {
2469 PERROR("read relay cmd pipe");
2472 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2475 * Only used by the control side and the reference is copied inside each
2476 * stream from that connection. Thus a destroy HT must be done after every
2477 * stream has been destroyed.
2479 if (relay_connection
->type
== RELAY_CONTROL
) {
2480 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2481 LTTNG_HT_TYPE_STRING
);
2482 if (!relay_connection
->ctf_traces_ht
) {
2487 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2488 (unsigned long) relay_connection
->sock
->fd
);
2490 lttng_ht_add_unique_ulong(relay_connections_ht
,
2491 &relay_connection
->sock_n
);
2493 return lttng_poll_add(events
,
2494 relay_connection
->sock
->fd
,
2495 LPOLLIN
| LPOLLRDHUP
);
2498 free(relay_connection
);
2504 void deferred_free_connection(struct rcu_head
*head
)
2506 struct relay_command
*relay_connection
=
2507 caa_container_of(head
, struct relay_command
, rcu_node
);
2509 lttcomm_destroy_sock(relay_connection
->sock
);
2510 free(relay_connection
);
2514 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2515 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2516 struct lttng_ht
*sessions_ht
)
2520 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2523 if (relay_connection
->type
== RELAY_CONTROL
) {
2524 struct relay_stream_recv_handle
*node
, *tmp_node
;
2526 relay_delete_session(relay_connection
, sessions_ht
);
2527 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2529 /* Clean up recv list. */
2530 cds_list_for_each_entry_safe(node
, tmp_node
,
2531 &relay_connection
->recv_head
, node
) {
2532 cds_list_del(&node
->node
);
2537 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2541 * This thread does the actual work
2544 void *relay_thread_worker(void *data
)
2546 int ret
, err
= -1, last_seen_data_fd
= -1;
2548 struct relay_command
*relay_connection
;
2549 struct lttng_poll_event events
;
2550 struct lttng_ht
*relay_connections_ht
;
2551 struct lttng_ht_node_ulong
*node
;
2552 struct lttng_ht_iter iter
;
2553 struct lttcomm_relayd_hdr recv_hdr
;
2554 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2555 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2557 DBG("[thread] Relay worker started");
2559 rcu_register_thread();
2561 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2563 health_code_update();
2565 /* table of connections indexed on socket */
2566 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2567 if (!relay_connections_ht
) {
2568 goto relay_connections_ht_error
;
2571 /* Tables of received indexes indexed by index handle and net_seq_num. */
2572 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2574 goto indexes_ht_error
;
2577 ret
= create_thread_poll_set(&events
, 2);
2579 goto error_poll_create
;
2582 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2589 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2591 health_code_update();
2593 /* Infinite blocking call, waiting for transmission */
2594 DBG3("Relayd worker thread polling...");
2595 health_poll_entry();
2596 ret
= lttng_poll_wait(&events
, -1);
2600 * Restart interrupted system call.
2602 if (errno
== EINTR
) {
2611 * Process control. The control connection is prioritised so we don't
2612 * starve it with high throughout put tracing data on the data
2615 for (i
= 0; i
< nb_fd
; i
++) {
2616 /* Fetch once the poll data */
2617 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2618 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2620 health_code_update();
2622 /* Thread quit pipe has been closed. Killing thread. */
2623 ret
= check_thread_quit_pipe(pollfd
, revents
);
2629 /* Inspect the relay cmd pipe for new connection */
2630 if (pollfd
== relay_cmd_pipe
[0]) {
2631 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2632 ERR("Relay pipe error");
2634 } else if (revents
& LPOLLIN
) {
2635 DBG("Relay command received");
2636 ret
= relay_add_connection(relay_cmd_pipe
[0],
2637 &events
, relay_connections_ht
);
2642 } else if (revents
) {
2644 lttng_ht_lookup(relay_connections_ht
,
2645 (void *)((unsigned long) pollfd
),
2647 node
= lttng_ht_iter_get_node_ulong(&iter
);
2649 DBG2("Relay sock %d not found", pollfd
);
2653 relay_connection
= caa_container_of(node
,
2654 struct relay_command
, sock_n
);
2656 if (revents
& (LPOLLERR
)) {
2658 relay_cleanup_poll_connection(&events
, pollfd
);
2659 relay_del_connection(relay_connections_ht
,
2660 &iter
, relay_connection
, sessions_ht
);
2661 if (last_seen_data_fd
== pollfd
) {
2662 last_seen_data_fd
= last_notdel_data_fd
;
2664 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2665 DBG("Socket %d hung up", pollfd
);
2666 relay_cleanup_poll_connection(&events
, pollfd
);
2667 relay_del_connection(relay_connections_ht
,
2668 &iter
, relay_connection
, sessions_ht
);
2669 if (last_seen_data_fd
== pollfd
) {
2670 last_seen_data_fd
= last_notdel_data_fd
;
2672 } else if (revents
& LPOLLIN
) {
2673 /* control socket */
2674 if (relay_connection
->type
== RELAY_CONTROL
) {
2675 ret
= relay_connection
->sock
->ops
->recvmsg(
2676 relay_connection
->sock
, &recv_hdr
,
2677 sizeof(struct lttcomm_relayd_hdr
), 0);
2678 /* connection closed */
2680 relay_cleanup_poll_connection(&events
, pollfd
);
2681 relay_del_connection(relay_connections_ht
,
2682 &iter
, relay_connection
, sessions_ht
);
2683 DBG("Control connection closed with %d", pollfd
);
2685 if (relay_connection
->session
) {
2686 DBG2("Relay worker receiving data for session : %" PRIu64
,
2687 relay_connection
->session
->id
);
2689 ret
= relay_process_control(&recv_hdr
,
2690 relay_connection
, relay_ctx
);
2692 /* Clear the session on error. */
2693 relay_cleanup_poll_connection(&events
, pollfd
);
2694 relay_del_connection(relay_connections_ht
,
2695 &iter
, relay_connection
, sessions_ht
);
2696 DBG("Connection closed with %d", pollfd
);
2702 * Flag the last seen data fd not deleted. It will be
2703 * used as the last seen fd if any fd gets deleted in
2706 last_notdel_data_fd
= pollfd
;
2714 * The last loop handled a control request, go back to poll to make
2715 * sure we prioritise the control socket.
2721 if (last_seen_data_fd
>= 0) {
2722 for (i
= 0; i
< nb_fd
; i
++) {
2723 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2725 health_code_update();
2727 if (last_seen_data_fd
== pollfd
) {
2734 /* Process data connection. */
2735 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2736 /* Fetch the poll data. */
2737 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2738 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2740 health_code_update();
2742 /* Skip the command pipe. It's handled in the first loop. */
2743 if (pollfd
== relay_cmd_pipe
[0]) {
2749 lttng_ht_lookup(relay_connections_ht
,
2750 (void *)((unsigned long) pollfd
),
2752 node
= lttng_ht_iter_get_node_ulong(&iter
);
2754 /* Skip it. Might be removed before. */
2758 relay_connection
= caa_container_of(node
,
2759 struct relay_command
, sock_n
);
2761 if (revents
& LPOLLIN
) {
2762 if (relay_connection
->type
!= RELAY_DATA
) {
2766 ret
= relay_process_data(relay_connection
);
2767 /* connection closed */
2769 relay_cleanup_poll_connection(&events
, pollfd
);
2770 relay_del_connection(relay_connections_ht
,
2771 &iter
, relay_connection
, sessions_ht
);
2772 DBG("Data connection closed with %d", pollfd
);
2774 * Every goto restart call sets the last seen fd where
2775 * here we don't really care since we gracefully
2776 * continue the loop after the connection is deleted.
2779 /* Keep last seen port. */
2780 last_seen_data_fd
= pollfd
;
2788 last_seen_data_fd
= -1;
2791 /* Normal exit, no error */
2796 lttng_poll_clean(&events
);
2798 /* empty the hash table and free the memory */
2800 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2801 health_code_update();
2803 node
= lttng_ht_iter_get_node_ulong(&iter
);
2805 relay_connection
= caa_container_of(node
,
2806 struct relay_command
, sock_n
);
2807 relay_del_connection(relay_connections_ht
,
2808 &iter
, relay_connection
, sessions_ht
);
2813 lttng_ht_destroy(indexes_ht
);
2815 lttng_ht_destroy(relay_connections_ht
);
2816 relay_connections_ht_error
:
2817 /* Close relay cmd pipes */
2818 utils_close_pipe(relay_cmd_pipe
);
2820 DBG("Thread exited with error");
2822 DBG("Worker thread cleanup complete");
2826 ERR("Health error occurred in %s", __func__
);
2828 health_unregister(health_relayd
);
2829 rcu_unregister_thread();
2835 * Create the relay command pipe to wake thread_manage_apps.
2836 * Closed in cleanup().
2838 static int create_relay_cmd_pipe(void)
2842 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2850 int main(int argc
, char **argv
)
2854 struct relay_local_data
*relay_ctx
;
2856 /* Parse arguments */
2858 if ((ret
= set_options(argc
, argv
)) < 0) {
2862 if ((ret
= set_signal_handler()) < 0) {
2866 /* Try to create directory if -o, --output is specified. */
2867 if (opt_output_path
) {
2868 if (*opt_output_path
!= '/') {
2869 ERR("Please specify an absolute path for -o, --output PATH");
2873 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2875 ERR("Unable to create %s", opt_output_path
);
2881 if (opt_daemon
|| opt_background
) {
2884 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2891 * We are in the child. Make sure all other file
2892 * descriptors are closed, in case we are called with
2893 * more opened file descriptors than the standard ones.
2895 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2900 /* Create thread quit pipe */
2901 if ((ret
= init_thread_quit_pipe()) < 0) {
2905 /* We need those values for the file/dir creation. */
2906 relayd_uid
= getuid();
2907 relayd_gid
= getgid();
2909 /* Check if daemon is UID = 0 */
2910 if (relayd_uid
== 0) {
2911 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 || live_uri
->port
< 1024) {
2912 ERR("Need to be root to use ports < 1024");
2918 /* Setup the thread apps communication pipe. */
2919 if ((ret
= create_relay_cmd_pipe()) < 0) {
2923 /* Init relay command queue. */
2924 cds_wfq_init(&relay_cmd_queue
.queue
);
2926 /* Set up max poll set size */
2927 lttng_poll_set_max_size();
2929 /* Initialize communication library */
2931 lttcomm_inet_init();
2933 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2935 PERROR("relay_ctx");
2939 /* tables of sessions indexed by session ID */
2940 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2941 if (!relay_ctx
->sessions_ht
) {
2942 goto exit_relay_ctx_sessions
;
2945 /* tables of streams indexed by stream ID */
2946 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2947 if (!relay_streams_ht
) {
2948 goto exit_relay_ctx_streams
;
2951 /* tables of streams indexed by stream ID */
2952 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2953 if (!viewer_streams_ht
) {
2954 goto exit_relay_ctx_viewer_streams
;
2957 /* Initialize thread health monitoring */
2958 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2959 if (!health_relayd
) {
2960 PERROR("health_app_create error");
2961 goto exit_health_app_create
;
2964 ret
= utils_create_pipe(health_quit_pipe
);
2966 goto error_health_pipe
;
2969 /* Create thread to manage the client socket */
2970 ret
= pthread_create(&health_thread
, NULL
,
2971 thread_manage_health
, (void *) NULL
);
2973 PERROR("pthread_create health");
2977 /* Setup the dispatcher thread */
2978 ret
= pthread_create(&dispatcher_thread
, NULL
,
2979 relay_thread_dispatcher
, (void *) NULL
);
2981 PERROR("pthread_create dispatcher");
2982 goto exit_dispatcher
;
2985 /* Setup the worker thread */
2986 ret
= pthread_create(&worker_thread
, NULL
,
2987 relay_thread_worker
, (void *) relay_ctx
);
2989 PERROR("pthread_create worker");
2993 /* Setup the listener thread */
2994 ret
= pthread_create(&listener_thread
, NULL
,
2995 relay_thread_listener
, (void *) NULL
);
2997 PERROR("pthread_create listener");
3001 ret
= live_start_threads(live_uri
, relay_ctx
, thread_quit_pipe
);
3003 ERR("Starting live viewer threads");
3008 ret
= pthread_join(listener_thread
, &status
);
3010 PERROR("pthread_join");
3011 goto error
; /* join error, exit without cleanup */
3015 ret
= pthread_join(worker_thread
, &status
);
3017 PERROR("pthread_join");
3018 goto error
; /* join error, exit without cleanup */
3022 ret
= pthread_join(dispatcher_thread
, &status
);
3024 PERROR("pthread_join");
3025 goto error
; /* join error, exit without cleanup */
3029 ret
= pthread_join(health_thread
, &status
);
3031 PERROR("pthread_join health thread");
3032 goto error
; /* join error, exit without cleanup */
3036 * Stop live threads only after joining other threads.
3038 live_stop_threads();
3041 utils_close_pipe(health_quit_pipe
);
3044 health_app_destroy(health_relayd
);
3046 exit_health_app_create
:
3047 lttng_ht_destroy(viewer_streams_ht
);
3049 exit_relay_ctx_viewer_streams
:
3050 lttng_ht_destroy(relay_streams_ht
);
3052 exit_relay_ctx_streams
:
3053 lttng_ht_destroy(relay_ctx
->sessions_ht
);
3055 exit_relay_ctx_sessions
: