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"
64 #include "testpoint.h"
66 /* command line options */
67 char *opt_output_path
;
68 static int opt_daemon
, opt_background
;
71 * We need to wait for listener and live listener threads, as well as
72 * health check thread, before being ready to signal readiness.
74 #define NR_LTTNG_RELAY_READY 3
75 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
76 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
77 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
79 static struct lttng_uri
*control_uri
;
80 static struct lttng_uri
*data_uri
;
81 static struct lttng_uri
*live_uri
;
85 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
86 static int tracing_group_name_override
;
88 const char * const config_section_name
= "relayd";
91 * Quit pipe for all threads. This permits a single cancellation point
92 * for all threads when receiving an event on the pipe.
94 int thread_quit_pipe
[2] = { -1, -1 };
97 * This pipe is used to inform the worker thread that a command is queued and
98 * ready to be processed.
100 static int relay_cmd_pipe
[2] = { -1, -1 };
102 /* Shared between threads */
103 static int dispatch_thread_exit
;
105 static pthread_t listener_thread
;
106 static pthread_t dispatcher_thread
;
107 static pthread_t worker_thread
;
108 static pthread_t health_thread
;
110 static uint64_t last_relay_stream_id
;
111 static uint64_t last_relay_session_id
;
114 * Relay command queue.
116 * The relay_thread_listener and relay_thread_dispatcher communicate with this
119 static struct relay_cmd_queue relay_cmd_queue
;
121 /* buffer allocated at startup, used to store the trace data */
122 static char *data_buffer
;
123 static unsigned int data_buffer_size
;
125 /* We need those values for the file/dir creation. */
126 static uid_t relayd_uid
;
127 static gid_t relayd_gid
;
129 /* Global relay stream hash table. */
130 struct lttng_ht
*relay_streams_ht
;
132 /* Global relay viewer stream hash table. */
133 struct lttng_ht
*viewer_streams_ht
;
135 /* Global hash table that stores relay index object. */
136 struct lttng_ht
*indexes_ht
;
138 /* Relayd health monitoring */
139 struct health_app
*health_relayd
;
141 static struct option long_options
[] = {
142 { "control-port", 1, 0, 'C', },
143 { "data-port", 1, 0, 'D', },
144 { "live-port", 1, 0, 'L', },
145 { "daemonize", 0, 0, 'd', },
146 { "background", 0, 0, 'b', },
147 { "group", 1, 0, 'g', },
148 { "help", 0, 0, 'h', },
149 { "output", 1, 0, 'o', },
150 { "verbose", 0, 0, 'v', },
151 { "config", 1, 0, 'f' },
155 static const char *config_ignore_options
[] = { "help", "config" };
158 * usage function on stderr
163 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
164 fprintf(stderr
, " -h, --help Display this usage.\n");
165 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
166 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
167 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
168 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
169 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
170 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
171 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
172 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
173 fprintf(stderr
, " -f --config Load daemon configuration file\n");
177 * Take an option from the getopt output and set it in the right variable to be
180 * Return 0 on success else a negative value.
183 int set_option(int opt
, const char *arg
, const char *optname
)
189 fprintf(stderr
, "option %s", optname
);
191 fprintf(stderr
, " with arg %s\n", arg
);
195 ret
= uri_parse(arg
, &control_uri
);
197 ERR("Invalid control URI specified");
200 if (control_uri
->port
== 0) {
201 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
205 ret
= uri_parse(arg
, &data_uri
);
207 ERR("Invalid data URI specified");
210 if (data_uri
->port
== 0) {
211 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
215 ret
= uri_parse(arg
, &live_uri
);
217 ERR("Invalid live URI specified");
220 if (live_uri
->port
== 0) {
221 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
231 tracing_group_name
= strdup(arg
);
232 tracing_group_name_override
= 1;
238 ret
= asprintf(&opt_output_path
, "%s", arg
);
241 PERROR("asprintf opt_output_path");
246 /* Verbose level can increase using multiple -v */
248 lttng_opt_verbose
= config_parse_value(arg
);
250 lttng_opt_verbose
+= 1;
254 /* Unknown option or other error.
255 * Error is printed by getopt, just return */
268 * config_entry_handler_cb used to handle options read from a config file.
269 * See config_entry_handler_cb comment in common/config/config.h for the
270 * return value conventions.
273 int config_entry_handler(const struct config_entry
*entry
, void *unused
)
277 if (!entry
|| !entry
->name
|| !entry
->value
) {
282 /* Check if the option is to be ignored */
283 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
284 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
289 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
290 /* Ignore if entry name is not fully matched. */
291 if (strcmp(entry
->name
, long_options
[i
].name
)) {
296 * If the option takes no argument on the command line, we have to
297 * check if the value is "true". We support non-zero numeric values,
300 if (!long_options
[i
].has_arg
) {
301 ret
= config_parse_value(entry
->value
);
304 WARN("Invalid configuration value \"%s\" for option %s",
305 entry
->value
, entry
->name
);
307 /* False, skip boolean config option. */
312 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
316 WARN("Unrecognized option \"%s\" in daemon configuration file.",
324 int set_options(int argc
, char **argv
)
326 int c
, ret
= 0, option_index
= 0;
327 int orig_optopt
= optopt
, orig_optind
= optind
;
328 char *default_address
, *optstring
;
329 const char *config_path
= NULL
;
331 optstring
= utils_generate_optstring(long_options
,
332 sizeof(long_options
) / sizeof(struct option
));
338 /* Check for the --config option */
340 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
341 &option_index
)) != -1) {
345 } else if (c
!= 'f') {
349 config_path
= utils_expand_path(optarg
);
351 ERR("Failed to resolve path: %s", optarg
);
355 ret
= config_get_section_entries(config_path
, config_section_name
,
356 config_entry_handler
, NULL
);
359 ERR("Invalid configuration option at line %i", ret
);
365 /* Reset getopt's global state */
366 optopt
= orig_optopt
;
367 optind
= orig_optind
;
369 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
374 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
380 /* assign default values */
381 if (control_uri
== NULL
) {
382 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
383 DEFAULT_NETWORK_CONTROL_PORT
);
385 PERROR("asprintf default data address");
389 ret
= uri_parse(default_address
, &control_uri
);
390 free(default_address
);
392 ERR("Invalid control URI specified");
396 if (data_uri
== NULL
) {
397 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
398 DEFAULT_NETWORK_DATA_PORT
);
400 PERROR("asprintf default data address");
404 ret
= uri_parse(default_address
, &data_uri
);
405 free(default_address
);
407 ERR("Invalid data URI specified");
411 if (live_uri
== NULL
) {
412 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
413 DEFAULT_NETWORK_VIEWER_PORT
);
415 PERROR("asprintf default viewer control address");
419 ret
= uri_parse(default_address
, &live_uri
);
420 free(default_address
);
422 ERR("Invalid viewer control URI specified");
440 /* free the dynamically allocated opt_output_path */
441 free(opt_output_path
);
443 /* Close thread quit pipes */
444 utils_close_pipe(thread_quit_pipe
);
446 uri_free(control_uri
);
448 /* Live URI is freed in the live thread. */
450 if (tracing_group_name_override
) {
451 free((void *) tracing_group_name
);
456 * Write to writable pipe used to notify a thread.
459 int notify_thread_pipe(int wpipe
)
463 ret
= lttng_write(wpipe
, "!", 1);
465 PERROR("write poll pipe");
471 static void notify_health_quit_pipe(int *pipe
)
475 ret
= lttng_write(pipe
[1], "4", 1);
477 PERROR("write relay health quit");
482 * Stop all threads by closing the thread quit pipe.
485 void stop_threads(void)
489 /* Stopping all threads */
490 DBG("Terminating all threads");
491 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
493 ERR("write error on thread quit pipe");
496 notify_health_quit_pipe(health_quit_pipe
);
498 /* Dispatch thread */
499 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
500 futex_nto1_wake(&relay_cmd_queue
.futex
);
504 * Signal handler for the daemon
506 * Simply stop all worker threads, leaving main() return gracefully after
507 * joining all threads and calling cleanup().
510 void sighandler(int sig
)
514 DBG("SIGPIPE caught");
517 DBG("SIGINT caught");
521 DBG("SIGTERM caught");
525 CMM_STORE_SHARED(recv_child_signal
, 1);
533 * Setup signal handler for :
534 * SIGINT, SIGTERM, SIGPIPE
537 int set_signal_handler(void)
543 if ((ret
= sigemptyset(&sigset
)) < 0) {
544 PERROR("sigemptyset");
548 sa
.sa_handler
= sighandler
;
551 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
556 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
561 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
566 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
571 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
576 void lttng_relay_notify_ready(void)
578 /* Notify the parent of the fork() process that we are ready. */
579 if (opt_daemon
|| opt_background
) {
580 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
581 kill(child_ppid
, SIGUSR1
);
587 * Init thread quit pipe.
589 * Return -1 on error or 0 if all pipes are created.
592 int init_thread_quit_pipe(void)
596 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
602 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
605 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
609 if (events
== NULL
|| size
== 0) {
614 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
620 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
632 * Check if the thread quit pipe was triggered.
634 * Return 1 if it was triggered else 0;
637 int check_thread_quit_pipe(int fd
, uint32_t events
)
639 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
647 * Create and init socket from uri.
650 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
653 struct lttcomm_sock
*sock
= NULL
;
655 sock
= lttcomm_alloc_sock_from_uri(uri
);
657 ERR("Allocating socket");
661 ret
= lttcomm_create_sock(sock
);
665 DBG("Listening on sock %d", sock
->fd
);
667 ret
= sock
->ops
->bind(sock
);
672 ret
= sock
->ops
->listen(sock
, -1);
682 lttcomm_destroy_sock(sock
);
688 * Return nonzero if stream needs to be closed.
691 int close_stream_check(struct relay_stream
*stream
)
694 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
696 * We are about to close the stream so set the data pending flag to 1
697 * which will make the end data pending command skip the stream which
698 * is now closed and ready. Note that after proceeding to a file close,
699 * the written file is ready for reading.
701 stream
->data_pending_check_done
= 1;
708 * This thread manages the listening for new connections on the network
711 void *relay_thread_listener(void *data
)
713 int i
, ret
, pollfd
, err
= -1;
715 uint32_t revents
, nb_fd
;
716 struct lttng_poll_event events
;
717 struct lttcomm_sock
*control_sock
, *data_sock
;
719 DBG("[thread] Relay listener started");
721 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
723 health_code_update();
725 control_sock
= relay_init_sock(control_uri
);
727 goto error_sock_control
;
730 data_sock
= relay_init_sock(data_uri
);
732 goto error_sock_relay
;
736 * Pass 3 as size here for the thread quit pipe, control and data socket.
738 ret
= create_thread_poll_set(&events
, 3);
740 goto error_create_poll
;
743 /* Add the control socket */
744 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
749 /* Add the data socket */
750 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
755 lttng_relay_notify_ready();
757 if (testpoint(relayd_thread_listener
)) {
758 goto error_testpoint
;
762 health_code_update();
764 DBG("Listener accepting connections");
768 ret
= lttng_poll_wait(&events
, -1);
772 * Restart interrupted system call.
774 if (errno
== EINTR
) {
782 DBG("Relay new connection received");
783 for (i
= 0; i
< nb_fd
; i
++) {
784 health_code_update();
786 /* Fetch once the poll data */
787 revents
= LTTNG_POLL_GETEV(&events
, i
);
788 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
790 /* Thread quit pipe has been closed. Killing thread. */
791 ret
= check_thread_quit_pipe(pollfd
, revents
);
797 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
798 ERR("socket poll error");
800 } else if (revents
& LPOLLIN
) {
802 * Get allocated in this thread,
803 * enqueued to a global queue, dequeued
804 * and freed in the worker thread.
806 struct relay_command
*relay_cmd
;
807 struct lttcomm_sock
*newsock
;
809 relay_cmd
= zmalloc(sizeof(struct relay_command
));
810 if (relay_cmd
== NULL
) {
811 PERROR("relay command zmalloc");
815 if (pollfd
== data_sock
->fd
) {
816 newsock
= data_sock
->ops
->accept(data_sock
);
818 PERROR("accepting data sock");
822 relay_cmd
->type
= RELAY_DATA
;
823 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
825 assert(pollfd
== control_sock
->fd
);
826 newsock
= control_sock
->ops
->accept(control_sock
);
828 PERROR("accepting control sock");
832 relay_cmd
->type
= RELAY_CONTROL
;
833 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
835 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
838 PERROR("setsockopt inet");
839 lttcomm_destroy_sock(newsock
);
843 relay_cmd
->sock
= newsock
;
845 * Lock free enqueue the request.
847 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
850 * Wake the dispatch queue futex. Implicit memory
851 * barrier with the exchange in cds_wfq_enqueue.
853 futex_nto1_wake(&relay_cmd_queue
.futex
);
862 lttng_poll_clean(&events
);
864 if (data_sock
->fd
>= 0) {
865 ret
= data_sock
->ops
->close(data_sock
);
870 lttcomm_destroy_sock(data_sock
);
872 if (control_sock
->fd
>= 0) {
873 ret
= control_sock
->ops
->close(control_sock
);
878 lttcomm_destroy_sock(control_sock
);
882 ERR("Health error occurred in %s", __func__
);
884 health_unregister(health_relayd
);
885 DBG("Relay listener thread cleanup complete");
891 * This thread manages the dispatching of the requests to worker threads
894 void *relay_thread_dispatcher(void *data
)
898 struct cds_wfq_node
*node
;
899 struct relay_command
*relay_cmd
= NULL
;
901 DBG("[thread] Relay dispatcher started");
903 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
905 if (testpoint(relayd_thread_dispatcher
)) {
906 goto error_testpoint
;
909 health_code_update();
911 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
912 health_code_update();
914 /* Atomically prepare the queue futex */
915 futex_nto1_prepare(&relay_cmd_queue
.futex
);
918 health_code_update();
920 /* Dequeue commands */
921 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
923 DBG("Woken up but nothing in the relay command queue");
924 /* Continue thread execution */
928 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
929 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
932 * Inform worker thread of the new request. This
933 * call is blocking so we can be assured that the data will be read
934 * at some point in time or wait to the end of the world :)
936 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
937 sizeof(struct relay_command
));
939 if (ret
< sizeof(struct relay_command
)) {
940 PERROR("write cmd pipe");
943 } while (node
!= NULL
);
945 /* Futex wait on queue. Blocking call on futex() */
947 futex_nto1_wait(&relay_cmd_queue
.futex
);
951 /* Normal exit, no error */
958 ERR("Health error occurred in %s", __func__
);
960 health_unregister(health_relayd
);
961 DBG("Dispatch thread dying");
967 * Get stream from stream id.
968 * Need to be called with RCU read-side lock held.
970 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
972 struct lttng_ht_node_ulong
*node
;
973 struct lttng_ht_iter iter
;
974 struct relay_stream
*ret
;
976 lttng_ht_lookup(relay_streams_ht
,
977 (void *)((unsigned long) stream_id
),
979 node
= lttng_ht_iter_get_node_ulong(&iter
);
981 DBG("Relay stream %" PRIu64
" not found", stream_id
);
986 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
993 void deferred_free_stream(struct rcu_head
*head
)
995 struct relay_stream
*stream
=
996 caa_container_of(head
, struct relay_stream
, rcu_node
);
998 free(stream
->path_name
);
999 free(stream
->channel_name
);
1004 void deferred_free_session(struct rcu_head
*head
)
1006 struct relay_session
*session
=
1007 caa_container_of(head
, struct relay_session
, rcu_node
);
1012 * Close a given stream. The stream is freed using a call RCU.
1014 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
1015 * BEFORE the stream lock MUST be acquired.
1017 static void destroy_stream(struct relay_stream
*stream
)
1020 struct relay_viewer_stream
*vstream
;
1021 struct lttng_ht_iter iter
;
1025 delret
= close(stream
->fd
);
1027 PERROR("close stream");
1030 if (stream
->index_fd
>= 0) {
1031 delret
= close(stream
->index_fd
);
1033 PERROR("close stream index_fd");
1037 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
1040 * Set the last good value into the viewer stream. This is done
1041 * right before the stream gets deleted from the hash table. The
1042 * lookup failure on the live thread side of a stream indicates
1043 * that the viewer stream index received value should be used.
1045 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
1046 vstream
->total_index_received
= stream
->total_index_received
;
1047 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
1048 vstream
->close_write_flag
= 1;
1049 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
1052 /* Cleanup index of that stream. */
1053 relay_index_destroy_by_stream_id(stream
->stream_handle
);
1055 iter
.iter
.node
= &stream
->stream_n
.node
;
1056 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
1058 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
1059 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
1062 if (stream
->ctf_trace
) {
1063 ctf_trace_try_destroy(stream
->ctf_trace
);
1066 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
1067 DBG("Closed tracefile %d from close stream", stream
->fd
);
1071 * relay_delete_session: Free all memory associated with a session and
1075 void relay_delete_session(struct relay_command
*cmd
,
1076 struct lttng_ht
*sessions_ht
)
1078 struct lttng_ht_iter iter
;
1079 struct lttng_ht_node_ulong
*node
;
1080 struct relay_stream
*stream
;
1083 if (!cmd
->session
) {
1087 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
1090 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1091 node
= lttng_ht_iter_get_node_ulong(&iter
);
1095 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1096 if (stream
->session
== cmd
->session
) {
1097 destroy_stream(stream
);
1098 cmd
->session
->stream_count
--;
1099 assert(cmd
->session
->stream_count
>= 0);
1103 /* Make this session not visible anymore. */
1104 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
1105 ret
= lttng_ht_del(sessions_ht
, &iter
);
1107 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
1112 * Copy index data from the control port to a given index object.
1114 static void copy_index_control_data(struct relay_index
*index
,
1115 struct lttcomm_relayd_index
*data
)
1121 * The index on disk is encoded in big endian, so we don't need to convert
1122 * the data received on the network. The data_offset value is NEVER
1123 * modified here and is updated by the data thread.
1125 index
->index_data
.packet_size
= data
->packet_size
;
1126 index
->index_data
.content_size
= data
->content_size
;
1127 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
1128 index
->index_data
.timestamp_end
= data
->timestamp_end
;
1129 index
->index_data
.events_discarded
= data
->events_discarded
;
1130 index
->index_data
.stream_id
= data
->stream_id
;
1134 * Handle the RELAYD_CREATE_SESSION command.
1136 * On success, send back the session id or else return a negative value.
1139 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1140 struct relay_command
*cmd
,
1141 struct lttng_ht
*sessions_ht
)
1143 int ret
= 0, send_ret
;
1144 struct relay_session
*session
;
1145 struct lttcomm_relayd_status_session reply
;
1150 memset(&reply
, 0, sizeof(reply
));
1152 session
= zmalloc(sizeof(struct relay_session
));
1153 if (session
== NULL
) {
1154 PERROR("relay session zmalloc");
1159 session
->id
= ++last_relay_session_id
;
1160 session
->sock
= cmd
->sock
;
1161 session
->minor
= cmd
->minor
;
1162 session
->major
= cmd
->major
;
1163 cmd
->session
= session
;
1165 reply
.session_id
= htobe64(session
->id
);
1167 switch (cmd
->minor
) {
1172 case 4: /* LTTng sessiond 2.4 */
1174 ret
= cmd_create_session_2_4(cmd
, session
);
1178 lttng_ht_node_init_ulong(&session
->session_n
,
1179 (unsigned long) session
->id
);
1180 lttng_ht_add_unique_ulong(sessions_ht
,
1181 &session
->session_n
);
1183 DBG("Created session %" PRIu64
, session
->id
);
1187 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1189 reply
.ret_code
= htobe32(LTTNG_OK
);
1192 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1194 ERR("Relayd sending session id");
1202 * When we have received all the streams and the metadata for a channel,
1203 * we make them visible to the viewer threads.
1206 void set_viewer_ready_flag(struct relay_command
*cmd
)
1208 struct relay_stream_recv_handle
*node
, *tmp_node
;
1210 cds_list_for_each_entry_safe(node
, tmp_node
, &cmd
->recv_head
, node
) {
1211 struct relay_stream
*stream
;
1214 stream
= relay_stream_find_by_id(node
->id
);
1217 * Stream is most probably being cleaned up by the data thread thus
1218 * simply continue to the next one.
1224 stream
->viewer_ready
= 1;
1227 /* Clean stream handle node. */
1228 cds_list_del(&node
->node
);
1236 * Add a recv handle node to the connection recv list with the given stream
1237 * handle. A new node is allocated thus must be freed when the node is deleted
1240 static void queue_stream_handle(uint64_t handle
, struct relay_command
*cmd
)
1242 struct relay_stream_recv_handle
*node
;
1246 node
= zmalloc(sizeof(*node
));
1248 PERROR("zmalloc queue stream handle");
1253 cds_list_add(&node
->node
, &cmd
->recv_head
);
1257 * relay_add_stream: allocate a new stream for a session
1260 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1261 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1263 struct relay_session
*session
= cmd
->session
;
1264 struct relay_stream
*stream
= NULL
;
1265 struct lttcomm_relayd_status_stream reply
;
1268 if (!session
|| cmd
->version_check_done
== 0) {
1269 ERR("Trying to add a stream before version check");
1271 goto end_no_session
;
1274 stream
= zmalloc(sizeof(struct relay_stream
));
1275 if (stream
== NULL
) {
1276 PERROR("relay stream zmalloc");
1278 goto end_no_session
;
1281 switch (cmd
->minor
) {
1282 case 1: /* LTTng sessiond 2.1 */
1283 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1285 case 2: /* LTTng sessiond 2.2 */
1287 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1291 goto err_free_stream
;
1295 stream
->stream_handle
= ++last_relay_stream_id
;
1296 stream
->prev_seq
= -1ULL;
1297 stream
->session
= session
;
1298 stream
->index_fd
= -1;
1299 stream
->read_index_fd
= -1;
1300 stream
->ctf_trace
= NULL
;
1301 pthread_mutex_init(&stream
->lock
, NULL
);
1303 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1305 ERR("relay creating output directory");
1310 * No need to use run_as API here because whatever we receives, the relayd
1311 * uses its own credentials for the stream files.
1313 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1314 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1316 ERR("Create output file");
1320 if (stream
->tracefile_size
) {
1321 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1323 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1326 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1327 stream
->metadata_flag
= 1;
1329 * When we receive a new metadata stream, we create a new
1330 * ctf_trace and we assign this ctf_trace to all streams with
1333 * If later on we receive a new stream for the same ctf_trace,
1334 * we copy the information from the first hit in the HT to the
1337 stream
->ctf_trace
= ctf_trace_create();
1338 if (!stream
->ctf_trace
) {
1342 stream
->ctf_trace
->refcount
++;
1343 stream
->ctf_trace
->metadata_stream
= stream
;
1345 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1346 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1349 * Add the stream handle in the recv list of the connection. Once the end
1350 * stream message is received, this list is emptied and streams are set
1351 * with the viewer ready flag.
1353 queue_stream_handle(stream
->stream_handle
, cmd
);
1355 lttng_ht_node_init_ulong(&stream
->stream_n
,
1356 (unsigned long) stream
->stream_handle
);
1357 lttng_ht_add_unique_ulong(relay_streams_ht
,
1360 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1361 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1362 session
->stream_count
++;
1364 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1365 stream
->stream_handle
);
1368 reply
.handle
= htobe64(stream
->stream_handle
);
1369 /* send the session id to the client or a negative return code on error */
1371 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1372 /* stream was not properly added to the ht, so free it */
1375 reply
.ret_code
= htobe32(LTTNG_OK
);
1378 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1379 sizeof(struct lttcomm_relayd_status_stream
), 0);
1381 ERR("Relay sending stream id");
1390 free(stream
->path_name
);
1391 free(stream
->channel_name
);
1397 * relay_close_stream: close a specific stream
1400 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1401 struct relay_command
*cmd
)
1404 struct relay_session
*session
= cmd
->session
;
1405 struct lttcomm_relayd_close_stream stream_info
;
1406 struct lttcomm_relayd_generic_reply reply
;
1407 struct relay_stream
*stream
;
1409 DBG("Close stream received");
1411 if (!session
|| cmd
->version_check_done
== 0) {
1412 ERR("Trying to close a stream before version check");
1414 goto end_no_session
;
1417 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1418 sizeof(struct lttcomm_relayd_close_stream
), 0);
1419 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1421 /* Orderly shutdown. Not necessary to print an error. */
1422 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1424 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1427 goto end_no_session
;
1431 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1437 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1438 stream
->close_flag
= 1;
1439 session
->stream_count
--;
1440 assert(session
->stream_count
>= 0);
1442 if (close_stream_check(stream
)) {
1443 destroy_stream(stream
);
1450 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1452 reply
.ret_code
= htobe32(LTTNG_OK
);
1454 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1455 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1457 ERR("Relay sending stream id");
1466 * relay_unknown_command: send -1 if received unknown command
1469 void relay_unknown_command(struct relay_command
*cmd
)
1471 struct lttcomm_relayd_generic_reply reply
;
1474 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1475 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1476 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1478 ERR("Relay sending unknown command");
1483 * relay_start: send an acknowledgment to the client to tell if we are
1484 * ready to receive data. We are ready if a session is established.
1487 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1488 struct relay_command
*cmd
)
1490 int ret
= htobe32(LTTNG_OK
);
1491 struct lttcomm_relayd_generic_reply reply
;
1492 struct relay_session
*session
= cmd
->session
;
1495 DBG("Trying to start the streaming without a session established");
1496 ret
= htobe32(LTTNG_ERR_UNK
);
1499 reply
.ret_code
= ret
;
1500 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1501 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1503 ERR("Relay sending start ack");
1510 * Append padding to the file pointed by the file descriptor fd.
1512 static int write_padding_to_file(int fd
, uint32_t size
)
1521 zeros
= zmalloc(size
);
1522 if (zeros
== NULL
) {
1523 PERROR("zmalloc zeros for padding");
1528 ret
= lttng_write(fd
, zeros
, size
);
1530 PERROR("write padding to file");
1540 * relay_recv_metadata: receive the metada for the session.
1543 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1544 struct relay_command
*cmd
)
1546 int ret
= htobe32(LTTNG_OK
);
1548 struct relay_session
*session
= cmd
->session
;
1549 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1550 struct relay_stream
*metadata_stream
;
1551 uint64_t data_size
, payload_size
;
1554 ERR("Metadata sent before version check");
1559 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1560 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1561 ERR("Incorrect data size");
1565 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1567 if (data_buffer_size
< data_size
) {
1568 /* In case the realloc fails, we can free the memory */
1571 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1572 if (!tmp_data_ptr
) {
1573 ERR("Allocating data buffer");
1578 data_buffer
= tmp_data_ptr
;
1579 data_buffer_size
= data_size
;
1581 memset(data_buffer
, 0, data_size
);
1582 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1583 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1584 if (ret
< 0 || ret
!= data_size
) {
1586 /* Orderly shutdown. Not necessary to print an error. */
1587 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1589 ERR("Relay didn't receive the whole metadata");
1594 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1597 metadata_stream
= relay_stream_find_by_id(
1598 be64toh(metadata_struct
->stream_id
));
1599 if (!metadata_stream
) {
1604 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1606 if (size_ret
< payload_size
) {
1607 ERR("Relay error writing metadata on file");
1612 ret
= write_padding_to_file(metadata_stream
->fd
,
1613 be32toh(metadata_struct
->padding_size
));
1617 metadata_stream
->ctf_trace
->metadata_received
+=
1618 payload_size
+ be32toh(metadata_struct
->padding_size
);
1620 DBG2("Relay metadata written");
1629 * relay_send_version: send relayd version number
1632 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1633 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1636 struct lttcomm_relayd_version reply
, msg
;
1640 cmd
->version_check_done
= 1;
1642 /* Get version from the other side. */
1643 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1644 if (ret
< 0 || ret
!= sizeof(msg
)) {
1646 /* Orderly shutdown. Not necessary to print an error. */
1647 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1649 ERR("Relay failed to receive the version values.");
1655 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1656 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1658 /* Major versions must be the same */
1659 if (reply
.major
!= be32toh(msg
.major
)) {
1660 DBG("Incompatible major versions (%u vs %u), deleting session",
1661 reply
.major
, be32toh(msg
.major
));
1662 relay_delete_session(cmd
, sessions_ht
);
1667 cmd
->major
= reply
.major
;
1668 /* We adapt to the lowest compatible version */
1669 if (reply
.minor
<= be32toh(msg
.minor
)) {
1670 cmd
->minor
= reply
.minor
;
1672 cmd
->minor
= be32toh(msg
.minor
);
1675 reply
.major
= htobe32(reply
.major
);
1676 reply
.minor
= htobe32(reply
.minor
);
1677 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1678 sizeof(struct lttcomm_relayd_version
), 0);
1680 ERR("Relay sending version");
1683 DBG("Version check done using protocol %u.%u", cmd
->major
,
1691 * Check for data pending for a given stream id from the session daemon.
1694 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1695 struct relay_command
*cmd
)
1697 struct relay_session
*session
= cmd
->session
;
1698 struct lttcomm_relayd_data_pending msg
;
1699 struct lttcomm_relayd_generic_reply reply
;
1700 struct relay_stream
*stream
;
1702 uint64_t last_net_seq_num
, stream_id
;
1704 DBG("Data pending command received");
1706 if (!session
|| cmd
->version_check_done
== 0) {
1707 ERR("Trying to check for data before version check");
1709 goto end_no_session
;
1712 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1713 if (ret
< sizeof(msg
)) {
1715 /* Orderly shutdown. Not necessary to print an error. */
1716 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1718 ERR("Relay didn't receive valid data_pending struct size : %d",
1722 goto end_no_session
;
1725 stream_id
= be64toh(msg
.stream_id
);
1726 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1729 stream
= relay_stream_find_by_id(stream_id
);
1730 if (stream
== NULL
) {
1735 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1736 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1739 /* Avoid wrapping issue */
1740 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1741 /* Data has in fact been written and is NOT pending */
1744 /* Data still being streamed thus pending */
1748 /* Pending check is now done. */
1749 stream
->data_pending_check_done
= 1;
1754 reply
.ret_code
= htobe32(ret
);
1755 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1757 ERR("Relay data pending ret code failed");
1765 * Wait for the control socket to reach a quiescent state.
1767 * Note that for now, when receiving this command from the session daemon, this
1768 * means that every subsequent commands or data received on the control socket
1769 * has been handled. So, this is why we simply return OK here.
1772 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1773 struct relay_command
*cmd
)
1777 struct relay_stream
*stream
;
1778 struct lttng_ht_iter iter
;
1779 struct lttcomm_relayd_quiescent_control msg
;
1780 struct lttcomm_relayd_generic_reply reply
;
1782 DBG("Checking quiescent state on control socket");
1784 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1785 ERR("Trying to check for data before version check");
1787 goto end_no_session
;
1790 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1791 if (ret
< sizeof(msg
)) {
1793 /* Orderly shutdown. Not necessary to print an error. */
1794 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1796 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1800 goto end_no_session
;
1803 stream_id
= be64toh(msg
.stream_id
);
1806 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1808 if (stream
->stream_handle
== stream_id
) {
1809 stream
->data_pending_check_done
= 1;
1810 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1817 reply
.ret_code
= htobe32(LTTNG_OK
);
1818 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1820 ERR("Relay data quiescent control ret code failed");
1828 * Initialize a data pending command. This means that a client is about to ask
1829 * for data pending for each stream he/she holds. Simply iterate over all
1830 * streams of a session and set the data_pending_check_done flag.
1832 * This command returns to the client a LTTNG_OK code.
1835 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1836 struct relay_command
*cmd
)
1839 struct lttng_ht_iter iter
;
1840 struct lttcomm_relayd_begin_data_pending msg
;
1841 struct lttcomm_relayd_generic_reply reply
;
1842 struct relay_stream
*stream
;
1843 uint64_t session_id
;
1848 DBG("Init streams for data pending");
1850 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1851 ERR("Trying to check for data before version check");
1853 goto end_no_session
;
1856 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1857 if (ret
< sizeof(msg
)) {
1859 /* Orderly shutdown. Not necessary to print an error. */
1860 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1862 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1866 goto end_no_session
;
1869 session_id
= be64toh(msg
.session_id
);
1872 * Iterate over all streams to set the begin data pending flag. For now, the
1873 * streams are indexed by stream handle so we have to iterate over all
1874 * streams to find the one associated with the right session_id.
1877 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1879 if (stream
->session
->id
== session_id
) {
1880 stream
->data_pending_check_done
= 0;
1881 DBG("Set begin data pending flag to stream %" PRIu64
,
1882 stream
->stream_handle
);
1887 /* All good, send back reply. */
1888 reply
.ret_code
= htobe32(LTTNG_OK
);
1890 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1892 ERR("Relay begin data pending send reply failed");
1900 * End data pending command. This will check, for a given session id, if each
1901 * stream associated with it has its data_pending_check_done flag set. If not,
1902 * this means that the client lost track of the stream but the data is still
1903 * being streamed on our side. In this case, we inform the client that data is
1906 * Return to the client if there is data in flight or not with a ret_code.
1909 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1910 struct relay_command
*cmd
)
1913 struct lttng_ht_iter iter
;
1914 struct lttcomm_relayd_end_data_pending msg
;
1915 struct lttcomm_relayd_generic_reply reply
;
1916 struct relay_stream
*stream
;
1917 uint64_t session_id
;
1918 uint32_t is_data_inflight
= 0;
1923 DBG("End data pending command");
1925 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1926 ERR("Trying to check for data before version check");
1928 goto end_no_session
;
1931 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1932 if (ret
< sizeof(msg
)) {
1934 /* Orderly shutdown. Not necessary to print an error. */
1935 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1937 ERR("Relay didn't receive valid end data_pending struct size: %d",
1941 goto end_no_session
;
1944 session_id
= be64toh(msg
.session_id
);
1946 /* Iterate over all streams to see if the begin data pending flag is set. */
1948 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1950 if (stream
->session
->id
== session_id
&&
1951 !stream
->data_pending_check_done
) {
1952 is_data_inflight
= 1;
1953 DBG("Data is still in flight for stream %" PRIu64
,
1954 stream
->stream_handle
);
1960 /* All good, send back reply. */
1961 reply
.ret_code
= htobe32(is_data_inflight
);
1963 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1965 ERR("Relay end data pending send reply failed");
1973 * Receive an index for a specific stream.
1975 * Return 0 on success else a negative value.
1978 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1979 struct relay_command
*cmd
)
1981 int ret
, send_ret
, index_created
= 0;
1982 struct relay_session
*session
= cmd
->session
;
1983 struct lttcomm_relayd_index index_info
;
1984 struct relay_index
*index
, *wr_index
= NULL
;
1985 struct lttcomm_relayd_generic_reply reply
;
1986 struct relay_stream
*stream
;
1987 uint64_t net_seq_num
;
1991 DBG("Relay receiving index");
1993 if (!session
|| cmd
->version_check_done
== 0) {
1994 ERR("Trying to close a stream before version check");
1996 goto end_no_session
;
1999 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
2000 sizeof(index_info
), 0);
2001 if (ret
< sizeof(index_info
)) {
2003 /* Orderly shutdown. Not necessary to print an error. */
2004 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2006 ERR("Relay didn't receive valid index struct size : %d", ret
);
2009 goto end_no_session
;
2012 net_seq_num
= be64toh(index_info
.net_seq_num
);
2015 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
2018 goto end_rcu_unlock
;
2021 /* Live beacon handling */
2022 if (index_info
.packet_size
== 0) {
2023 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
2026 * Only flag a stream inactive when it has already received data.
2028 if (stream
->total_index_received
> 0) {
2029 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
2032 goto end_rcu_unlock
;
2034 stream
->beacon_ts_end
= -1ULL;
2037 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
2039 /* A successful creation will add the object to the HT. */
2040 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
2042 goto end_rcu_unlock
;
2047 copy_index_control_data(index
, &index_info
);
2049 if (index_created
) {
2051 * Try to add the relay index object to the hash table. If an object
2052 * already exist, destroy back the index created, set the data in this
2053 * object and write it on disk.
2055 relay_index_add(index
, &wr_index
);
2057 copy_index_control_data(wr_index
, &index_info
);
2061 /* The index already exists so write it on disk. */
2065 /* Do we have a writable ready index to write on disk. */
2067 /* Starting at 2.4, create the index file if none available. */
2068 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
2069 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2070 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2071 stream
->tracefile_count_current
);
2073 goto end_rcu_unlock
;
2075 stream
->index_fd
= ret
;
2078 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2080 goto end_rcu_unlock
;
2082 stream
->total_index_received
++;
2089 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2091 reply
.ret_code
= htobe32(LTTNG_OK
);
2093 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2095 ERR("Relay sending close index id reply");
2104 * Receive the streams_sent message.
2106 * Return 0 on success else a negative value.
2109 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2110 struct relay_command
*cmd
)
2113 struct lttcomm_relayd_generic_reply reply
;
2117 DBG("Relay receiving streams_sent");
2119 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
2120 ERR("Trying to close a stream before version check");
2122 goto end_no_session
;
2126 * Flag every pending stream in the connection recv list that they are
2127 * ready to be used by the viewer.
2129 set_viewer_ready_flag(cmd
);
2132 * Inform the viewer that there are new streams in the session.
2134 uatomic_set(&cmd
->session
->new_streams
, 1);
2136 reply
.ret_code
= htobe32(LTTNG_OK
);
2137 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2139 ERR("Relay sending sent_stream reply");
2151 * Process the commands received on the control socket
2154 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2155 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
2159 switch (be32toh(recv_hdr
->cmd
)) {
2160 case RELAYD_CREATE_SESSION
:
2161 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
2163 case RELAYD_ADD_STREAM
:
2164 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
2166 case RELAYD_START_DATA
:
2167 ret
= relay_start(recv_hdr
, cmd
);
2169 case RELAYD_SEND_METADATA
:
2170 ret
= relay_recv_metadata(recv_hdr
, cmd
);
2172 case RELAYD_VERSION
:
2173 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
2175 case RELAYD_CLOSE_STREAM
:
2176 ret
= relay_close_stream(recv_hdr
, cmd
);
2178 case RELAYD_DATA_PENDING
:
2179 ret
= relay_data_pending(recv_hdr
, cmd
);
2181 case RELAYD_QUIESCENT_CONTROL
:
2182 ret
= relay_quiescent_control(recv_hdr
, cmd
);
2184 case RELAYD_BEGIN_DATA_PENDING
:
2185 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
2187 case RELAYD_END_DATA_PENDING
:
2188 ret
= relay_end_data_pending(recv_hdr
, cmd
);
2190 case RELAYD_SEND_INDEX
:
2191 ret
= relay_recv_index(recv_hdr
, cmd
);
2193 case RELAYD_STREAMS_SENT
:
2194 ret
= relay_streams_sent(recv_hdr
, cmd
);
2196 case RELAYD_UPDATE_SYNC_INFO
:
2198 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2199 relay_unknown_command(cmd
);
2209 * Handle index for a data stream.
2211 * RCU read side lock MUST be acquired.
2213 * Return 0 on success else a negative value.
2215 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2218 int ret
= 0, index_created
= 0;
2219 uint64_t stream_id
, data_offset
;
2220 struct relay_index
*index
, *wr_index
= NULL
;
2224 stream_id
= stream
->stream_handle
;
2225 /* Get data offset because we are about to update the index. */
2226 data_offset
= htobe64(stream
->tracefile_size_current
);
2229 * Lookup for an existing index for that stream id/sequence number. If on
2230 * exists, the control thread already received the data for it thus we need
2231 * to write it on disk.
2233 index
= relay_index_find(stream_id
, net_seq_num
);
2235 /* A successful creation will add the object to the HT. */
2236 index
= relay_index_create(stream_id
, net_seq_num
);
2244 if (rotate_index
|| stream
->index_fd
< 0) {
2245 index
->to_close_fd
= stream
->index_fd
;
2246 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2247 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2248 stream
->tracefile_count_current
);
2250 /* This will close the stream's index fd if one. */
2251 relay_index_free_safe(index
);
2254 stream
->index_fd
= ret
;
2256 index
->fd
= stream
->index_fd
;
2257 index
->index_data
.offset
= data_offset
;
2259 if (index_created
) {
2261 * Try to add the relay index object to the hash table. If an object
2262 * already exist, destroy back the index created and set the data.
2264 relay_index_add(index
, &wr_index
);
2266 /* Copy back data from the created index. */
2267 wr_index
->fd
= index
->fd
;
2268 wr_index
->to_close_fd
= index
->to_close_fd
;
2269 wr_index
->index_data
.offset
= data_offset
;
2273 /* The index already exists so write it on disk. */
2277 /* Do we have a writable ready index to write on disk. */
2279 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2283 stream
->total_index_received
++;
2291 * relay_process_data: Process the data received on the data socket
2294 int relay_process_data(struct relay_command
*cmd
)
2296 int ret
= 0, rotate_index
= 0;
2298 struct relay_stream
*stream
;
2299 struct lttcomm_relayd_data_hdr data_hdr
;
2301 uint64_t net_seq_num
;
2304 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2305 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2308 /* Orderly shutdown. Not necessary to print an error. */
2309 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2311 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2317 stream_id
= be64toh(data_hdr
.stream_id
);
2320 stream
= relay_stream_find_by_id(stream_id
);
2323 goto end_rcu_unlock
;
2326 data_size
= be32toh(data_hdr
.data_size
);
2327 if (data_buffer_size
< data_size
) {
2330 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2331 if (!tmp_data_ptr
) {
2332 ERR("Allocating data buffer");
2335 goto end_rcu_unlock
;
2337 data_buffer
= tmp_data_ptr
;
2338 data_buffer_size
= data_size
;
2340 memset(data_buffer
, 0, data_size
);
2342 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2344 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2345 data_size
, stream_id
, net_seq_num
);
2346 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2349 /* Orderly shutdown. Not necessary to print an error. */
2350 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2353 goto end_rcu_unlock
;
2356 /* Check if a rotation is needed. */
2357 if (stream
->tracefile_size
> 0 &&
2358 (stream
->tracefile_size_current
+ data_size
) >
2359 stream
->tracefile_size
) {
2360 struct relay_viewer_stream
*vstream
;
2363 new_id
= (stream
->tracefile_count_current
+ 1) %
2364 stream
->tracefile_count
;
2366 * When we wrap-around back to 0, we start overwriting old
2369 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2370 stream
->tracefile_overwrite
= 1;
2372 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2373 if (stream
->tracefile_overwrite
) {
2374 stream
->oldest_tracefile_id
=
2375 (stream
->oldest_tracefile_id
+ 1) %
2376 stream
->tracefile_count
;
2378 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
2381 * The viewer is reading a file about to be
2382 * overwritten. Close the FDs it is
2383 * currently using and let it handle the fault.
2385 if (vstream
->tracefile_count_current
== new_id
) {
2386 pthread_mutex_lock(&vstream
->overwrite_lock
);
2387 vstream
->abort_flag
= 1;
2388 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2389 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2390 stream
->channel_name
, new_id
);
2391 } else if (vstream
->tracefile_count_current
==
2392 stream
->tracefile_count_current
) {
2394 * The reader and writer were in the
2395 * same trace file, inform the viewer
2396 * that no new index will ever be added
2399 vstream
->close_write_flag
= 1;
2402 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2403 stream
->tracefile_size
, stream
->tracefile_count
,
2404 relayd_uid
, relayd_gid
, stream
->fd
,
2405 &(stream
->tracefile_count_current
), &stream
->fd
);
2406 stream
->total_index_received
= 0;
2407 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2409 ERR("Rotating stream output file");
2410 goto end_rcu_unlock
;
2412 /* Reset current size because we just perform a stream rotation. */
2413 stream
->tracefile_size_current
= 0;
2418 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2419 * index are NOT supported.
2421 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2422 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2424 goto end_rcu_unlock
;
2428 /* Write data to stream output fd. */
2429 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2430 if (size_ret
< data_size
) {
2431 ERR("Relay error writing data to file");
2433 goto end_rcu_unlock
;
2436 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2437 ret
, stream
->stream_handle
);
2439 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2441 goto end_rcu_unlock
;
2443 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2445 stream
->prev_seq
= net_seq_num
;
2447 /* Check if we need to close the FD */
2448 if (close_stream_check(stream
)) {
2449 destroy_stream(stream
);
2459 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2463 lttng_poll_del(events
, pollfd
);
2465 ret
= close(pollfd
);
2467 ERR("Closing pollfd %d", pollfd
);
2472 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2473 struct lttng_ht
*relay_connections_ht
)
2475 struct relay_command
*relay_connection
;
2478 relay_connection
= zmalloc(sizeof(struct relay_command
));
2479 if (relay_connection
== NULL
) {
2480 PERROR("Relay command zmalloc");
2483 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2484 if (ret
< sizeof(struct relay_command
)) {
2485 PERROR("read relay cmd pipe");
2488 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2491 * Only used by the control side and the reference is copied inside each
2492 * stream from that connection. Thus a destroy HT must be done after every
2493 * stream has been destroyed.
2495 if (relay_connection
->type
== RELAY_CONTROL
) {
2496 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2497 LTTNG_HT_TYPE_STRING
);
2498 if (!relay_connection
->ctf_traces_ht
) {
2503 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2504 (unsigned long) relay_connection
->sock
->fd
);
2506 lttng_ht_add_unique_ulong(relay_connections_ht
,
2507 &relay_connection
->sock_n
);
2509 return lttng_poll_add(events
,
2510 relay_connection
->sock
->fd
,
2511 LPOLLIN
| LPOLLRDHUP
);
2514 free(relay_connection
);
2520 void deferred_free_connection(struct rcu_head
*head
)
2522 struct relay_command
*relay_connection
=
2523 caa_container_of(head
, struct relay_command
, rcu_node
);
2525 lttcomm_destroy_sock(relay_connection
->sock
);
2526 free(relay_connection
);
2530 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2531 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2532 struct lttng_ht
*sessions_ht
)
2536 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2539 if (relay_connection
->type
== RELAY_CONTROL
) {
2540 struct relay_stream_recv_handle
*node
, *tmp_node
;
2542 relay_delete_session(relay_connection
, sessions_ht
);
2543 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2545 /* Clean up recv list. */
2546 cds_list_for_each_entry_safe(node
, tmp_node
,
2547 &relay_connection
->recv_head
, node
) {
2548 cds_list_del(&node
->node
);
2553 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2557 * This thread does the actual work
2560 void *relay_thread_worker(void *data
)
2562 int ret
, err
= -1, last_seen_data_fd
= -1;
2564 struct relay_command
*relay_connection
;
2565 struct lttng_poll_event events
;
2566 struct lttng_ht
*relay_connections_ht
;
2567 struct lttng_ht_node_ulong
*node
;
2568 struct lttng_ht_iter iter
;
2569 struct lttcomm_relayd_hdr recv_hdr
;
2570 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2571 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2573 DBG("[thread] Relay worker started");
2575 rcu_register_thread();
2577 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2579 if (testpoint(relayd_thread_worker
)) {
2580 goto error_testpoint
;
2583 health_code_update();
2585 /* table of connections indexed on socket */
2586 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2587 if (!relay_connections_ht
) {
2588 goto relay_connections_ht_error
;
2591 /* Tables of received indexes indexed by index handle and net_seq_num. */
2592 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2594 goto indexes_ht_error
;
2597 ret
= create_thread_poll_set(&events
, 2);
2599 goto error_poll_create
;
2602 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2609 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2611 health_code_update();
2613 /* Infinite blocking call, waiting for transmission */
2614 DBG3("Relayd worker thread polling...");
2615 health_poll_entry();
2616 ret
= lttng_poll_wait(&events
, -1);
2620 * Restart interrupted system call.
2622 if (errno
== EINTR
) {
2631 * Process control. The control connection is prioritised so we don't
2632 * starve it with high throughout put tracing data on the data
2635 for (i
= 0; i
< nb_fd
; i
++) {
2636 /* Fetch once the poll data */
2637 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2638 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2640 health_code_update();
2642 /* Thread quit pipe has been closed. Killing thread. */
2643 ret
= check_thread_quit_pipe(pollfd
, revents
);
2649 /* Inspect the relay cmd pipe for new connection */
2650 if (pollfd
== relay_cmd_pipe
[0]) {
2651 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2652 ERR("Relay pipe error");
2654 } else if (revents
& LPOLLIN
) {
2655 DBG("Relay command received");
2656 ret
= relay_add_connection(relay_cmd_pipe
[0],
2657 &events
, relay_connections_ht
);
2662 } else if (revents
) {
2664 lttng_ht_lookup(relay_connections_ht
,
2665 (void *)((unsigned long) pollfd
),
2667 node
= lttng_ht_iter_get_node_ulong(&iter
);
2669 DBG2("Relay sock %d not found", pollfd
);
2673 relay_connection
= caa_container_of(node
,
2674 struct relay_command
, sock_n
);
2676 if (revents
& (LPOLLERR
)) {
2678 relay_cleanup_poll_connection(&events
, pollfd
);
2679 relay_del_connection(relay_connections_ht
,
2680 &iter
, relay_connection
, sessions_ht
);
2681 if (last_seen_data_fd
== pollfd
) {
2682 last_seen_data_fd
= last_notdel_data_fd
;
2684 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2685 DBG("Socket %d hung up", pollfd
);
2686 relay_cleanup_poll_connection(&events
, pollfd
);
2687 relay_del_connection(relay_connections_ht
,
2688 &iter
, relay_connection
, sessions_ht
);
2689 if (last_seen_data_fd
== pollfd
) {
2690 last_seen_data_fd
= last_notdel_data_fd
;
2692 } else if (revents
& LPOLLIN
) {
2693 /* control socket */
2694 if (relay_connection
->type
== RELAY_CONTROL
) {
2695 ret
= relay_connection
->sock
->ops
->recvmsg(
2696 relay_connection
->sock
, &recv_hdr
,
2697 sizeof(struct lttcomm_relayd_hdr
), 0);
2698 /* connection closed */
2700 relay_cleanup_poll_connection(&events
, pollfd
);
2701 relay_del_connection(relay_connections_ht
,
2702 &iter
, relay_connection
, sessions_ht
);
2703 DBG("Control connection closed with %d", pollfd
);
2705 if (relay_connection
->session
) {
2706 DBG2("Relay worker receiving data for session : %" PRIu64
,
2707 relay_connection
->session
->id
);
2709 ret
= relay_process_control(&recv_hdr
,
2710 relay_connection
, relay_ctx
);
2712 /* Clear the session on error. */
2713 relay_cleanup_poll_connection(&events
, pollfd
);
2714 relay_del_connection(relay_connections_ht
,
2715 &iter
, relay_connection
, sessions_ht
);
2716 DBG("Connection closed with %d", pollfd
);
2722 * Flag the last seen data fd not deleted. It will be
2723 * used as the last seen fd if any fd gets deleted in
2726 last_notdel_data_fd
= pollfd
;
2734 * The last loop handled a control request, go back to poll to make
2735 * sure we prioritise the control socket.
2741 if (last_seen_data_fd
>= 0) {
2742 for (i
= 0; i
< nb_fd
; i
++) {
2743 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2745 health_code_update();
2747 if (last_seen_data_fd
== pollfd
) {
2754 /* Process data connection. */
2755 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2756 /* Fetch the poll data. */
2757 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2758 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2760 health_code_update();
2762 /* Skip the command pipe. It's handled in the first loop. */
2763 if (pollfd
== relay_cmd_pipe
[0]) {
2769 lttng_ht_lookup(relay_connections_ht
,
2770 (void *)((unsigned long) pollfd
),
2772 node
= lttng_ht_iter_get_node_ulong(&iter
);
2774 /* Skip it. Might be removed before. */
2778 relay_connection
= caa_container_of(node
,
2779 struct relay_command
, sock_n
);
2781 if (revents
& LPOLLIN
) {
2782 if (relay_connection
->type
!= RELAY_DATA
) {
2786 ret
= relay_process_data(relay_connection
);
2787 /* connection closed */
2789 relay_cleanup_poll_connection(&events
, pollfd
);
2790 relay_del_connection(relay_connections_ht
,
2791 &iter
, relay_connection
, sessions_ht
);
2792 DBG("Data connection closed with %d", pollfd
);
2794 * Every goto restart call sets the last seen fd where
2795 * here we don't really care since we gracefully
2796 * continue the loop after the connection is deleted.
2799 /* Keep last seen port. */
2800 last_seen_data_fd
= pollfd
;
2808 last_seen_data_fd
= -1;
2811 /* Normal exit, no error */
2816 lttng_poll_clean(&events
);
2818 /* empty the hash table and free the memory */
2820 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2821 health_code_update();
2823 node
= lttng_ht_iter_get_node_ulong(&iter
);
2825 relay_connection
= caa_container_of(node
,
2826 struct relay_command
, sock_n
);
2827 relay_del_connection(relay_connections_ht
,
2828 &iter
, relay_connection
, sessions_ht
);
2833 lttng_ht_destroy(indexes_ht
);
2835 lttng_ht_destroy(relay_connections_ht
);
2836 relay_connections_ht_error
:
2837 /* Close relay cmd pipes */
2838 utils_close_pipe(relay_cmd_pipe
);
2840 DBG("Thread exited with error");
2842 DBG("Worker thread cleanup complete");
2847 ERR("Health error occurred in %s", __func__
);
2849 health_unregister(health_relayd
);
2850 rcu_unregister_thread();
2856 * Create the relay command pipe to wake thread_manage_apps.
2857 * Closed in cleanup().
2859 static int create_relay_cmd_pipe(void)
2863 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2871 int main(int argc
, char **argv
)
2875 struct relay_local_data
*relay_ctx
;
2877 /* Parse arguments */
2879 if ((ret
= set_options(argc
, argv
)) < 0) {
2883 if ((ret
= set_signal_handler()) < 0) {
2887 /* Try to create directory if -o, --output is specified. */
2888 if (opt_output_path
) {
2889 if (*opt_output_path
!= '/') {
2890 ERR("Please specify an absolute path for -o, --output PATH");
2894 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2896 ERR("Unable to create %s", opt_output_path
);
2902 if (opt_daemon
|| opt_background
) {
2905 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2912 * We are in the child. Make sure all other file
2913 * descriptors are closed, in case we are called with
2914 * more opened file descriptors than the standard ones.
2916 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2921 /* Create thread quit pipe */
2922 if ((ret
= init_thread_quit_pipe()) < 0) {
2926 /* We need those values for the file/dir creation. */
2927 relayd_uid
= getuid();
2928 relayd_gid
= getgid();
2930 /* Check if daemon is UID = 0 */
2931 if (relayd_uid
== 0) {
2932 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 || live_uri
->port
< 1024) {
2933 ERR("Need to be root to use ports < 1024");
2939 /* Setup the thread apps communication pipe. */
2940 if ((ret
= create_relay_cmd_pipe()) < 0) {
2944 /* Init relay command queue. */
2945 cds_wfq_init(&relay_cmd_queue
.queue
);
2947 /* Set up max poll set size */
2948 lttng_poll_set_max_size();
2950 /* Initialize communication library */
2952 lttcomm_inet_init();
2954 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2956 PERROR("relay_ctx");
2960 /* tables of sessions indexed by session ID */
2961 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2962 if (!relay_ctx
->sessions_ht
) {
2963 goto exit_relay_ctx_sessions
;
2966 /* tables of streams indexed by stream ID */
2967 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2968 if (!relay_streams_ht
) {
2969 goto exit_relay_ctx_streams
;
2972 /* tables of streams indexed by stream ID */
2973 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2974 if (!viewer_streams_ht
) {
2975 goto exit_relay_ctx_viewer_streams
;
2978 /* Initialize thread health monitoring */
2979 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2980 if (!health_relayd
) {
2981 PERROR("health_app_create error");
2982 goto exit_health_app_create
;
2985 ret
= utils_create_pipe(health_quit_pipe
);
2987 goto error_health_pipe
;
2990 /* Create thread to manage the client socket */
2991 ret
= pthread_create(&health_thread
, NULL
,
2992 thread_manage_health
, (void *) NULL
);
2994 PERROR("pthread_create health");
2998 /* Setup the dispatcher thread */
2999 ret
= pthread_create(&dispatcher_thread
, NULL
,
3000 relay_thread_dispatcher
, (void *) NULL
);
3002 PERROR("pthread_create dispatcher");
3003 goto exit_dispatcher
;
3006 /* Setup the worker thread */
3007 ret
= pthread_create(&worker_thread
, NULL
,
3008 relay_thread_worker
, (void *) relay_ctx
);
3010 PERROR("pthread_create worker");
3014 /* Setup the listener thread */
3015 ret
= pthread_create(&listener_thread
, NULL
,
3016 relay_thread_listener
, (void *) NULL
);
3018 PERROR("pthread_create listener");
3022 ret
= live_start_threads(live_uri
, relay_ctx
);
3024 ERR("Starting live viewer threads");
3029 ret
= pthread_join(listener_thread
, &status
);
3031 PERROR("pthread_join");
3032 goto error
; /* join error, exit without cleanup */
3036 ret
= pthread_join(worker_thread
, &status
);
3038 PERROR("pthread_join");
3039 goto error
; /* join error, exit without cleanup */
3043 ret
= pthread_join(dispatcher_thread
, &status
);
3045 PERROR("pthread_join");
3046 goto error
; /* join error, exit without cleanup */
3050 ret
= pthread_join(health_thread
, &status
);
3052 PERROR("pthread_join health thread");
3053 goto error
; /* join error, exit without cleanup */
3057 * Stop live threads only after joining other threads.
3059 live_stop_threads();
3062 utils_close_pipe(health_quit_pipe
);
3065 health_app_destroy(health_relayd
);
3067 exit_health_app_create
:
3068 lttng_ht_destroy(viewer_streams_ht
);
3070 exit_relay_ctx_viewer_streams
:
3071 lttng_ht_destroy(relay_streams_ht
);
3073 exit_relay_ctx_streams
:
3074 lttng_ht_destroy(relay_ctx
->sessions_ht
);
3076 exit_relay_ctx_sessions
: