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"
65 #include "viewer-stream.h"
67 /* command line options */
68 char *opt_output_path
;
69 static int opt_daemon
, opt_background
;
72 * We need to wait for listener and live listener threads, as well as
73 * health check thread, before being ready to signal readiness.
75 #define NR_LTTNG_RELAY_READY 3
76 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
77 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
78 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
80 static struct lttng_uri
*control_uri
;
81 static struct lttng_uri
*data_uri
;
82 static struct lttng_uri
*live_uri
;
86 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
87 static int tracing_group_name_override
;
89 const char * const config_section_name
= "relayd";
92 * Quit pipe for all threads. This permits a single cancellation point
93 * for all threads when receiving an event on the pipe.
95 int thread_quit_pipe
[2] = { -1, -1 };
98 * This pipe is used to inform the worker thread that a command is queued and
99 * ready to be processed.
101 static int relay_cmd_pipe
[2] = { -1, -1 };
103 /* Shared between threads */
104 static int dispatch_thread_exit
;
106 static pthread_t listener_thread
;
107 static pthread_t dispatcher_thread
;
108 static pthread_t worker_thread
;
109 static pthread_t health_thread
;
111 static uint64_t last_relay_stream_id
;
112 static uint64_t last_relay_session_id
;
115 * Relay command queue.
117 * The relay_thread_listener and relay_thread_dispatcher communicate with this
120 static struct relay_cmd_queue relay_cmd_queue
;
122 /* buffer allocated at startup, used to store the trace data */
123 static char *data_buffer
;
124 static unsigned int data_buffer_size
;
126 /* We need those values for the file/dir creation. */
127 static uid_t relayd_uid
;
128 static gid_t relayd_gid
;
130 /* Global relay stream hash table. */
131 struct lttng_ht
*relay_streams_ht
;
133 /* Global relay viewer stream hash table. */
134 struct lttng_ht
*viewer_streams_ht
;
136 /* Global hash table that stores relay index object. */
137 struct lttng_ht
*indexes_ht
;
139 /* Relayd health monitoring */
140 struct health_app
*health_relayd
;
142 static struct option long_options
[] = {
143 { "control-port", 1, 0, 'C', },
144 { "data-port", 1, 0, 'D', },
145 { "live-port", 1, 0, 'L', },
146 { "daemonize", 0, 0, 'd', },
147 { "background", 0, 0, 'b', },
148 { "group", 1, 0, 'g', },
149 { "help", 0, 0, 'h', },
150 { "output", 1, 0, 'o', },
151 { "verbose", 0, 0, 'v', },
152 { "config", 1, 0, 'f' },
156 static const char *config_ignore_options
[] = { "help", "config" };
159 * usage function on stderr
164 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
165 fprintf(stderr
, " -h, --help Display this usage.\n");
166 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
167 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
168 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
169 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
170 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
171 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
172 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
173 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
174 fprintf(stderr
, " -f --config Load daemon configuration file\n");
178 * Take an option from the getopt output and set it in the right variable to be
181 * Return 0 on success else a negative value.
184 int set_option(int opt
, const char *arg
, const char *optname
)
190 fprintf(stderr
, "option %s", optname
);
192 fprintf(stderr
, " with arg %s\n", arg
);
196 ret
= uri_parse(arg
, &control_uri
);
198 ERR("Invalid control URI specified");
201 if (control_uri
->port
== 0) {
202 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
206 ret
= uri_parse(arg
, &data_uri
);
208 ERR("Invalid data URI specified");
211 if (data_uri
->port
== 0) {
212 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
216 ret
= uri_parse(arg
, &live_uri
);
218 ERR("Invalid live URI specified");
221 if (live_uri
->port
== 0) {
222 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
232 tracing_group_name
= strdup(arg
);
233 tracing_group_name_override
= 1;
239 ret
= asprintf(&opt_output_path
, "%s", arg
);
242 PERROR("asprintf opt_output_path");
247 /* Verbose level can increase using multiple -v */
249 lttng_opt_verbose
= config_parse_value(arg
);
251 lttng_opt_verbose
+= 1;
255 /* Unknown option or other error.
256 * Error is printed by getopt, just return */
269 * config_entry_handler_cb used to handle options read from a config file.
270 * See config_entry_handler_cb comment in common/config/config.h for the
271 * return value conventions.
274 int config_entry_handler(const struct config_entry
*entry
, void *unused
)
278 if (!entry
|| !entry
->name
|| !entry
->value
) {
283 /* Check if the option is to be ignored */
284 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
285 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
290 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
291 /* Ignore if entry name is not fully matched. */
292 if (strcmp(entry
->name
, long_options
[i
].name
)) {
297 * If the option takes no argument on the command line, we have to
298 * check if the value is "true". We support non-zero numeric values,
301 if (!long_options
[i
].has_arg
) {
302 ret
= config_parse_value(entry
->value
);
305 WARN("Invalid configuration value \"%s\" for option %s",
306 entry
->value
, entry
->name
);
308 /* False, skip boolean config option. */
313 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
317 WARN("Unrecognized option \"%s\" in daemon configuration file.",
325 int set_options(int argc
, char **argv
)
327 int c
, ret
= 0, option_index
= 0;
328 int orig_optopt
= optopt
, orig_optind
= optind
;
329 char *default_address
, *optstring
;
330 const char *config_path
= NULL
;
332 optstring
= utils_generate_optstring(long_options
,
333 sizeof(long_options
) / sizeof(struct option
));
339 /* Check for the --config option */
341 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
342 &option_index
)) != -1) {
346 } else if (c
!= 'f') {
350 config_path
= utils_expand_path(optarg
);
352 ERR("Failed to resolve path: %s", optarg
);
356 ret
= config_get_section_entries(config_path
, config_section_name
,
357 config_entry_handler
, NULL
);
360 ERR("Invalid configuration option at line %i", ret
);
366 /* Reset getopt's global state */
367 optopt
= orig_optopt
;
368 optind
= orig_optind
;
370 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
375 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
381 /* assign default values */
382 if (control_uri
== NULL
) {
383 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
384 DEFAULT_NETWORK_CONTROL_PORT
);
386 PERROR("asprintf default data address");
390 ret
= uri_parse(default_address
, &control_uri
);
391 free(default_address
);
393 ERR("Invalid control URI specified");
397 if (data_uri
== NULL
) {
398 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
399 DEFAULT_NETWORK_DATA_PORT
);
401 PERROR("asprintf default data address");
405 ret
= uri_parse(default_address
, &data_uri
);
406 free(default_address
);
408 ERR("Invalid data URI specified");
412 if (live_uri
== NULL
) {
413 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
414 DEFAULT_NETWORK_VIEWER_PORT
);
416 PERROR("asprintf default viewer control address");
420 ret
= uri_parse(default_address
, &live_uri
);
421 free(default_address
);
423 ERR("Invalid viewer control URI specified");
441 /* free the dynamically allocated opt_output_path */
442 free(opt_output_path
);
444 /* Close thread quit pipes */
445 utils_close_pipe(thread_quit_pipe
);
447 uri_free(control_uri
);
449 /* Live URI is freed in the live thread. */
451 if (tracing_group_name_override
) {
452 free((void *) tracing_group_name
);
457 * Write to writable pipe used to notify a thread.
460 int notify_thread_pipe(int wpipe
)
464 ret
= lttng_write(wpipe
, "!", 1);
466 PERROR("write poll pipe");
472 static void notify_health_quit_pipe(int *pipe
)
476 ret
= lttng_write(pipe
[1], "4", 1);
478 PERROR("write relay health quit");
483 * Stop all threads by closing the thread quit pipe.
486 void stop_threads(void)
490 /* Stopping all threads */
491 DBG("Terminating all threads");
492 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
494 ERR("write error on thread quit pipe");
497 notify_health_quit_pipe(health_quit_pipe
);
499 /* Dispatch thread */
500 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
501 futex_nto1_wake(&relay_cmd_queue
.futex
);
505 * Signal handler for the daemon
507 * Simply stop all worker threads, leaving main() return gracefully after
508 * joining all threads and calling cleanup().
511 void sighandler(int sig
)
515 DBG("SIGPIPE caught");
518 DBG("SIGINT caught");
522 DBG("SIGTERM caught");
526 CMM_STORE_SHARED(recv_child_signal
, 1);
534 * Setup signal handler for :
535 * SIGINT, SIGTERM, SIGPIPE
538 int set_signal_handler(void)
544 if ((ret
= sigemptyset(&sigset
)) < 0) {
545 PERROR("sigemptyset");
549 sa
.sa_handler
= sighandler
;
552 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
557 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
562 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
567 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
572 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
577 void lttng_relay_notify_ready(void)
579 /* Notify the parent of the fork() process that we are ready. */
580 if (opt_daemon
|| opt_background
) {
581 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
582 kill(child_ppid
, SIGUSR1
);
588 * Init thread quit pipe.
590 * Return -1 on error or 0 if all pipes are created.
593 int init_thread_quit_pipe(void)
597 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
603 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
606 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
610 if (events
== NULL
|| size
== 0) {
615 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
621 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
633 * Check if the thread quit pipe was triggered.
635 * Return 1 if it was triggered else 0;
638 int check_thread_quit_pipe(int fd
, uint32_t events
)
640 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
648 * Create and init socket from uri.
651 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
654 struct lttcomm_sock
*sock
= NULL
;
656 sock
= lttcomm_alloc_sock_from_uri(uri
);
658 ERR("Allocating socket");
662 ret
= lttcomm_create_sock(sock
);
666 DBG("Listening on sock %d", sock
->fd
);
668 ret
= sock
->ops
->bind(sock
);
673 ret
= sock
->ops
->listen(sock
, -1);
683 lttcomm_destroy_sock(sock
);
689 * Return nonzero if stream needs to be closed.
692 int close_stream_check(struct relay_stream
*stream
)
695 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
697 * We are about to close the stream so set the data pending flag to 1
698 * which will make the end data pending command skip the stream which
699 * is now closed and ready. Note that after proceeding to a file close,
700 * the written file is ready for reading.
702 stream
->data_pending_check_done
= 1;
709 * This thread manages the listening for new connections on the network
712 void *relay_thread_listener(void *data
)
714 int i
, ret
, pollfd
, err
= -1;
716 uint32_t revents
, nb_fd
;
717 struct lttng_poll_event events
;
718 struct lttcomm_sock
*control_sock
, *data_sock
;
720 DBG("[thread] Relay listener started");
722 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
724 health_code_update();
726 control_sock
= relay_init_sock(control_uri
);
728 goto error_sock_control
;
731 data_sock
= relay_init_sock(data_uri
);
733 goto error_sock_relay
;
737 * Pass 3 as size here for the thread quit pipe, control and data socket.
739 ret
= create_thread_poll_set(&events
, 3);
741 goto error_create_poll
;
744 /* Add the control socket */
745 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
750 /* Add the data socket */
751 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
756 lttng_relay_notify_ready();
758 if (testpoint(relayd_thread_listener
)) {
759 goto error_testpoint
;
763 health_code_update();
765 DBG("Listener accepting connections");
769 ret
= lttng_poll_wait(&events
, -1);
773 * Restart interrupted system call.
775 if (errno
== EINTR
) {
783 DBG("Relay new connection received");
784 for (i
= 0; i
< nb_fd
; i
++) {
785 health_code_update();
787 /* Fetch once the poll data */
788 revents
= LTTNG_POLL_GETEV(&events
, i
);
789 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
791 /* Thread quit pipe has been closed. Killing thread. */
792 ret
= check_thread_quit_pipe(pollfd
, revents
);
798 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
799 ERR("socket poll error");
801 } else if (revents
& LPOLLIN
) {
803 * Get allocated in this thread,
804 * enqueued to a global queue, dequeued
805 * and freed in the worker thread.
807 struct relay_command
*relay_cmd
;
808 struct lttcomm_sock
*newsock
;
810 relay_cmd
= zmalloc(sizeof(struct relay_command
));
811 if (relay_cmd
== NULL
) {
812 PERROR("relay command zmalloc");
816 if (pollfd
== data_sock
->fd
) {
817 newsock
= data_sock
->ops
->accept(data_sock
);
819 PERROR("accepting data sock");
823 relay_cmd
->type
= RELAY_DATA
;
824 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
826 assert(pollfd
== control_sock
->fd
);
827 newsock
= control_sock
->ops
->accept(control_sock
);
829 PERROR("accepting control sock");
833 relay_cmd
->type
= RELAY_CONTROL
;
834 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
836 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
839 PERROR("setsockopt inet");
840 lttcomm_destroy_sock(newsock
);
844 relay_cmd
->sock
= newsock
;
846 * Lock free enqueue the request.
848 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
851 * Wake the dispatch queue futex. Implicit memory
852 * barrier with the exchange in cds_wfq_enqueue.
854 futex_nto1_wake(&relay_cmd_queue
.futex
);
863 lttng_poll_clean(&events
);
865 if (data_sock
->fd
>= 0) {
866 ret
= data_sock
->ops
->close(data_sock
);
871 lttcomm_destroy_sock(data_sock
);
873 if (control_sock
->fd
>= 0) {
874 ret
= control_sock
->ops
->close(control_sock
);
879 lttcomm_destroy_sock(control_sock
);
883 ERR("Health error occurred in %s", __func__
);
885 health_unregister(health_relayd
);
886 DBG("Relay listener thread cleanup complete");
892 * This thread manages the dispatching of the requests to worker threads
895 void *relay_thread_dispatcher(void *data
)
899 struct cds_wfq_node
*node
;
900 struct relay_command
*relay_cmd
= NULL
;
902 DBG("[thread] Relay dispatcher started");
904 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
906 if (testpoint(relayd_thread_dispatcher
)) {
907 goto error_testpoint
;
910 health_code_update();
912 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
913 health_code_update();
915 /* Atomically prepare the queue futex */
916 futex_nto1_prepare(&relay_cmd_queue
.futex
);
919 health_code_update();
921 /* Dequeue commands */
922 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
924 DBG("Woken up but nothing in the relay command queue");
925 /* Continue thread execution */
929 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
930 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
933 * Inform worker thread of the new request. This
934 * call is blocking so we can be assured that the data will be read
935 * at some point in time or wait to the end of the world :)
937 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
938 sizeof(struct relay_command
));
940 if (ret
< sizeof(struct relay_command
)) {
941 PERROR("write cmd pipe");
944 } while (node
!= NULL
);
946 /* Futex wait on queue. Blocking call on futex() */
948 futex_nto1_wait(&relay_cmd_queue
.futex
);
952 /* Normal exit, no error */
959 ERR("Health error occurred in %s", __func__
);
961 health_unregister(health_relayd
);
962 DBG("Dispatch thread dying");
968 * Get stream from stream id.
969 * Need to be called with RCU read-side lock held.
971 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
973 struct lttng_ht_node_ulong
*node
;
974 struct lttng_ht_iter iter
;
975 struct relay_stream
*ret
;
977 lttng_ht_lookup(relay_streams_ht
,
978 (void *)((unsigned long) stream_id
),
980 node
= lttng_ht_iter_get_node_ulong(&iter
);
982 DBG("Relay stream %" PRIu64
" not found", stream_id
);
987 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
994 void deferred_free_stream(struct rcu_head
*head
)
996 struct relay_stream
*stream
=
997 caa_container_of(head
, struct relay_stream
, rcu_node
);
999 free(stream
->path_name
);
1000 free(stream
->channel_name
);
1005 void deferred_free_session(struct rcu_head
*head
)
1007 struct relay_session
*session
=
1008 caa_container_of(head
, struct relay_session
, rcu_node
);
1013 * Close a given stream. The stream is freed using a call RCU.
1015 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
1016 * BEFORE the stream lock MUST be acquired.
1018 static void destroy_stream(struct relay_stream
*stream
)
1021 struct relay_viewer_stream
*vstream
;
1022 struct lttng_ht_iter iter
;
1026 delret
= close(stream
->fd
);
1028 PERROR("close stream");
1031 if (stream
->index_fd
>= 0) {
1032 delret
= close(stream
->index_fd
);
1034 PERROR("close stream index_fd");
1038 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
1041 * Set the last good value into the viewer stream. This is done
1042 * right before the stream gets deleted from the hash table. The
1043 * lookup failure on the live thread side of a stream indicates
1044 * that the viewer stream index received value should be used.
1046 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
1047 vstream
->total_index_received
= stream
->total_index_received
;
1048 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
1049 vstream
->close_write_flag
= 1;
1050 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
1053 /* Cleanup index of that stream. */
1054 relay_index_destroy_by_stream_id(stream
->stream_handle
);
1056 iter
.iter
.node
= &stream
->stream_n
.node
;
1057 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
1059 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
1060 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
1063 if (stream
->ctf_trace
) {
1064 ctf_trace_try_destroy(stream
->ctf_trace
);
1067 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
1068 DBG("Closed tracefile %d from close stream", stream
->fd
);
1072 * relay_delete_session: Free all memory associated with a session and
1076 void relay_delete_session(struct relay_command
*cmd
,
1077 struct lttng_ht
*sessions_ht
)
1079 struct lttng_ht_iter iter
;
1080 struct lttng_ht_node_ulong
*node
;
1081 struct relay_stream
*stream
;
1084 if (!cmd
->session
) {
1088 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
1091 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1092 node
= lttng_ht_iter_get_node_ulong(&iter
);
1096 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1097 if (stream
->session
== cmd
->session
) {
1098 destroy_stream(stream
);
1099 cmd
->session
->stream_count
--;
1100 assert(cmd
->session
->stream_count
>= 0);
1104 /* Make this session not visible anymore. */
1105 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
1106 ret
= lttng_ht_del(sessions_ht
, &iter
);
1108 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
1113 * Copy index data from the control port to a given index object.
1115 static void copy_index_control_data(struct relay_index
*index
,
1116 struct lttcomm_relayd_index
*data
)
1122 * The index on disk is encoded in big endian, so we don't need to convert
1123 * the data received on the network. The data_offset value is NEVER
1124 * modified here and is updated by the data thread.
1126 index
->index_data
.packet_size
= data
->packet_size
;
1127 index
->index_data
.content_size
= data
->content_size
;
1128 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
1129 index
->index_data
.timestamp_end
= data
->timestamp_end
;
1130 index
->index_data
.events_discarded
= data
->events_discarded
;
1131 index
->index_data
.stream_id
= data
->stream_id
;
1135 * Handle the RELAYD_CREATE_SESSION command.
1137 * On success, send back the session id or else return a negative value.
1140 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1141 struct relay_command
*cmd
,
1142 struct lttng_ht
*sessions_ht
)
1144 int ret
= 0, send_ret
;
1145 struct relay_session
*session
;
1146 struct lttcomm_relayd_status_session reply
;
1151 memset(&reply
, 0, sizeof(reply
));
1153 session
= zmalloc(sizeof(struct relay_session
));
1154 if (session
== NULL
) {
1155 PERROR("relay session zmalloc");
1160 session
->id
= ++last_relay_session_id
;
1161 session
->sock
= cmd
->sock
;
1162 session
->minor
= cmd
->minor
;
1163 session
->major
= cmd
->major
;
1164 pthread_mutex_init(&session
->viewer_ready_lock
, NULL
);
1165 cmd
->session
= session
;
1167 reply
.session_id
= htobe64(session
->id
);
1169 switch (cmd
->minor
) {
1174 case 4: /* LTTng sessiond 2.4 */
1176 ret
= cmd_create_session_2_4(cmd
, session
);
1180 lttng_ht_node_init_ulong(&session
->session_n
,
1181 (unsigned long) session
->id
);
1182 lttng_ht_add_unique_ulong(sessions_ht
,
1183 &session
->session_n
);
1185 DBG("Created session %" PRIu64
, session
->id
);
1189 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1191 reply
.ret_code
= htobe32(LTTNG_OK
);
1194 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1196 ERR("Relayd sending session id");
1204 * When we have received all the streams and the metadata for a channel,
1205 * we make them visible to the viewer threads.
1208 void set_viewer_ready_flag(struct relay_command
*cmd
)
1210 struct relay_stream_recv_handle
*node
, *tmp_node
;
1212 pthread_mutex_lock(&cmd
->session
->viewer_ready_lock
);
1214 cds_list_for_each_entry_safe(node
, tmp_node
, &cmd
->recv_head
, node
) {
1215 struct relay_stream
*stream
;
1218 stream
= relay_stream_find_by_id(node
->id
);
1221 * Stream is most probably being cleaned up by the data thread thus
1222 * simply continue to the next one.
1228 stream
->viewer_ready
= 1;
1231 /* Clean stream handle node. */
1232 cds_list_del(&node
->node
);
1236 pthread_mutex_unlock(&cmd
->session
->viewer_ready_lock
);
1241 * Add a recv handle node to the connection recv list with the given stream
1242 * handle. A new node is allocated thus must be freed when the node is deleted
1245 static void queue_stream_handle(uint64_t handle
, struct relay_command
*cmd
)
1247 struct relay_stream_recv_handle
*node
;
1251 node
= zmalloc(sizeof(*node
));
1253 PERROR("zmalloc queue stream handle");
1258 cds_list_add(&node
->node
, &cmd
->recv_head
);
1262 * relay_add_stream: allocate a new stream for a session
1265 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1266 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1268 struct relay_session
*session
= cmd
->session
;
1269 struct relay_stream
*stream
= NULL
;
1270 struct lttcomm_relayd_status_stream reply
;
1273 if (!session
|| cmd
->version_check_done
== 0) {
1274 ERR("Trying to add a stream before version check");
1276 goto end_no_session
;
1279 stream
= zmalloc(sizeof(struct relay_stream
));
1280 if (stream
== NULL
) {
1281 PERROR("relay stream zmalloc");
1283 goto end_no_session
;
1286 switch (cmd
->minor
) {
1287 case 1: /* LTTng sessiond 2.1 */
1288 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1290 case 2: /* LTTng sessiond 2.2 */
1292 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1296 goto err_free_stream
;
1300 stream
->stream_handle
= ++last_relay_stream_id
;
1301 stream
->prev_seq
= -1ULL;
1302 stream
->session
= session
;
1303 stream
->index_fd
= -1;
1304 stream
->read_index_fd
= -1;
1305 stream
->ctf_trace
= NULL
;
1306 pthread_mutex_init(&stream
->lock
, NULL
);
1308 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1310 ERR("relay creating output directory");
1315 * No need to use run_as API here because whatever we receives, the relayd
1316 * uses its own credentials for the stream files.
1318 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1319 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1321 ERR("Create output file");
1325 if (stream
->tracefile_size
) {
1326 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1328 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1331 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1332 stream
->metadata_flag
= 1;
1334 * When we receive a new metadata stream, we create a new
1335 * ctf_trace and we assign this ctf_trace to all streams with
1338 * If later on we receive a new stream for the same ctf_trace,
1339 * we copy the information from the first hit in the HT to the
1342 stream
->ctf_trace
= ctf_trace_create();
1343 if (!stream
->ctf_trace
) {
1347 stream
->ctf_trace
->refcount
++;
1348 stream
->ctf_trace
->metadata_stream
= stream
;
1350 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1351 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1354 * Add the stream handle in the recv list of the connection. Once the end
1355 * stream message is received, this list is emptied and streams are set
1356 * with the viewer ready flag.
1358 queue_stream_handle(stream
->stream_handle
, cmd
);
1360 lttng_ht_node_init_ulong(&stream
->stream_n
,
1361 (unsigned long) stream
->stream_handle
);
1362 lttng_ht_add_unique_ulong(relay_streams_ht
,
1365 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1366 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1367 session
->stream_count
++;
1369 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1370 stream
->stream_handle
);
1373 reply
.handle
= htobe64(stream
->stream_handle
);
1374 /* send the session id to the client or a negative return code on error */
1376 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1377 /* stream was not properly added to the ht, so free it */
1380 reply
.ret_code
= htobe32(LTTNG_OK
);
1383 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1384 sizeof(struct lttcomm_relayd_status_stream
), 0);
1386 ERR("Relay sending stream id");
1395 free(stream
->path_name
);
1396 free(stream
->channel_name
);
1402 * relay_close_stream: close a specific stream
1405 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1406 struct relay_command
*cmd
)
1409 struct relay_session
*session
= cmd
->session
;
1410 struct lttcomm_relayd_close_stream stream_info
;
1411 struct lttcomm_relayd_generic_reply reply
;
1412 struct relay_stream
*stream
;
1414 DBG("Close stream received");
1416 if (!session
|| cmd
->version_check_done
== 0) {
1417 ERR("Trying to close a stream before version check");
1419 goto end_no_session
;
1422 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1423 sizeof(struct lttcomm_relayd_close_stream
), 0);
1424 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1426 /* Orderly shutdown. Not necessary to print an error. */
1427 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1429 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1432 goto end_no_session
;
1436 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1442 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1443 stream
->close_flag
= 1;
1444 session
->stream_count
--;
1445 assert(session
->stream_count
>= 0);
1447 if (close_stream_check(stream
)) {
1448 destroy_stream(stream
);
1455 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1457 reply
.ret_code
= htobe32(LTTNG_OK
);
1459 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1460 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1462 ERR("Relay sending stream id");
1471 * relay_unknown_command: send -1 if received unknown command
1474 void relay_unknown_command(struct relay_command
*cmd
)
1476 struct lttcomm_relayd_generic_reply reply
;
1479 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1480 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1481 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1483 ERR("Relay sending unknown command");
1488 * relay_start: send an acknowledgment to the client to tell if we are
1489 * ready to receive data. We are ready if a session is established.
1492 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1493 struct relay_command
*cmd
)
1495 int ret
= htobe32(LTTNG_OK
);
1496 struct lttcomm_relayd_generic_reply reply
;
1497 struct relay_session
*session
= cmd
->session
;
1500 DBG("Trying to start the streaming without a session established");
1501 ret
= htobe32(LTTNG_ERR_UNK
);
1504 reply
.ret_code
= ret
;
1505 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1506 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1508 ERR("Relay sending start ack");
1515 * Append padding to the file pointed by the file descriptor fd.
1517 static int write_padding_to_file(int fd
, uint32_t size
)
1526 zeros
= zmalloc(size
);
1527 if (zeros
== NULL
) {
1528 PERROR("zmalloc zeros for padding");
1533 ret
= lttng_write(fd
, zeros
, size
);
1535 PERROR("write padding to file");
1545 * relay_recv_metadata: receive the metada for the session.
1548 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1549 struct relay_command
*cmd
)
1551 int ret
= htobe32(LTTNG_OK
);
1553 struct relay_session
*session
= cmd
->session
;
1554 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1555 struct relay_stream
*metadata_stream
;
1556 uint64_t data_size
, payload_size
;
1559 ERR("Metadata sent before version check");
1564 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1565 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1566 ERR("Incorrect data size");
1570 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1572 if (data_buffer_size
< data_size
) {
1573 /* In case the realloc fails, we can free the memory */
1576 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1577 if (!tmp_data_ptr
) {
1578 ERR("Allocating data buffer");
1583 data_buffer
= tmp_data_ptr
;
1584 data_buffer_size
= data_size
;
1586 memset(data_buffer
, 0, data_size
);
1587 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1588 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1589 if (ret
< 0 || ret
!= data_size
) {
1591 /* Orderly shutdown. Not necessary to print an error. */
1592 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1594 ERR("Relay didn't receive the whole metadata");
1599 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1602 metadata_stream
= relay_stream_find_by_id(
1603 be64toh(metadata_struct
->stream_id
));
1604 if (!metadata_stream
) {
1609 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1611 if (size_ret
< payload_size
) {
1612 ERR("Relay error writing metadata on file");
1617 ret
= write_padding_to_file(metadata_stream
->fd
,
1618 be32toh(metadata_struct
->padding_size
));
1622 metadata_stream
->ctf_trace
->metadata_received
+=
1623 payload_size
+ be32toh(metadata_struct
->padding_size
);
1625 DBG2("Relay metadata written");
1634 * relay_send_version: send relayd version number
1637 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1638 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1641 struct lttcomm_relayd_version reply
, msg
;
1645 cmd
->version_check_done
= 1;
1647 /* Get version from the other side. */
1648 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1649 if (ret
< 0 || ret
!= sizeof(msg
)) {
1651 /* Orderly shutdown. Not necessary to print an error. */
1652 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1654 ERR("Relay failed to receive the version values.");
1660 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1661 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1663 /* Major versions must be the same */
1664 if (reply
.major
!= be32toh(msg
.major
)) {
1665 DBG("Incompatible major versions (%u vs %u), deleting session",
1666 reply
.major
, be32toh(msg
.major
));
1667 relay_delete_session(cmd
, sessions_ht
);
1672 cmd
->major
= reply
.major
;
1673 /* We adapt to the lowest compatible version */
1674 if (reply
.minor
<= be32toh(msg
.minor
)) {
1675 cmd
->minor
= reply
.minor
;
1677 cmd
->minor
= be32toh(msg
.minor
);
1680 reply
.major
= htobe32(reply
.major
);
1681 reply
.minor
= htobe32(reply
.minor
);
1682 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1683 sizeof(struct lttcomm_relayd_version
), 0);
1685 ERR("Relay sending version");
1688 DBG("Version check done using protocol %u.%u", cmd
->major
,
1696 * Check for data pending for a given stream id from the session daemon.
1699 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1700 struct relay_command
*cmd
)
1702 struct relay_session
*session
= cmd
->session
;
1703 struct lttcomm_relayd_data_pending msg
;
1704 struct lttcomm_relayd_generic_reply reply
;
1705 struct relay_stream
*stream
;
1707 uint64_t last_net_seq_num
, stream_id
;
1709 DBG("Data pending command received");
1711 if (!session
|| cmd
->version_check_done
== 0) {
1712 ERR("Trying to check for data before version check");
1714 goto end_no_session
;
1717 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1718 if (ret
< sizeof(msg
)) {
1720 /* Orderly shutdown. Not necessary to print an error. */
1721 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1723 ERR("Relay didn't receive valid data_pending struct size : %d",
1727 goto end_no_session
;
1730 stream_id
= be64toh(msg
.stream_id
);
1731 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1734 stream
= relay_stream_find_by_id(stream_id
);
1735 if (stream
== NULL
) {
1740 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1741 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1744 /* Avoid wrapping issue */
1745 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1746 /* Data has in fact been written and is NOT pending */
1749 /* Data still being streamed thus pending */
1753 /* Pending check is now done. */
1754 stream
->data_pending_check_done
= 1;
1759 reply
.ret_code
= htobe32(ret
);
1760 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1762 ERR("Relay data pending ret code failed");
1770 * Wait for the control socket to reach a quiescent state.
1772 * Note that for now, when receiving this command from the session daemon, this
1773 * means that every subsequent commands or data received on the control socket
1774 * has been handled. So, this is why we simply return OK here.
1777 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1778 struct relay_command
*cmd
)
1782 struct relay_stream
*stream
;
1783 struct lttng_ht_iter iter
;
1784 struct lttcomm_relayd_quiescent_control msg
;
1785 struct lttcomm_relayd_generic_reply reply
;
1787 DBG("Checking quiescent state on control socket");
1789 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1790 ERR("Trying to check for data before version check");
1792 goto end_no_session
;
1795 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1796 if (ret
< sizeof(msg
)) {
1798 /* Orderly shutdown. Not necessary to print an error. */
1799 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1801 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1805 goto end_no_session
;
1808 stream_id
= be64toh(msg
.stream_id
);
1811 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1813 if (stream
->stream_handle
== stream_id
) {
1814 stream
->data_pending_check_done
= 1;
1815 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1822 reply
.ret_code
= htobe32(LTTNG_OK
);
1823 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1825 ERR("Relay data quiescent control ret code failed");
1833 * Initialize a data pending command. This means that a client is about to ask
1834 * for data pending for each stream he/she holds. Simply iterate over all
1835 * streams of a session and set the data_pending_check_done flag.
1837 * This command returns to the client a LTTNG_OK code.
1840 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1841 struct relay_command
*cmd
)
1844 struct lttng_ht_iter iter
;
1845 struct lttcomm_relayd_begin_data_pending msg
;
1846 struct lttcomm_relayd_generic_reply reply
;
1847 struct relay_stream
*stream
;
1848 uint64_t session_id
;
1853 DBG("Init streams for data pending");
1855 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1856 ERR("Trying to check for data before version check");
1858 goto end_no_session
;
1861 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1862 if (ret
< sizeof(msg
)) {
1864 /* Orderly shutdown. Not necessary to print an error. */
1865 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1867 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1871 goto end_no_session
;
1874 session_id
= be64toh(msg
.session_id
);
1877 * Iterate over all streams to set the begin data pending flag. For now, the
1878 * streams are indexed by stream handle so we have to iterate over all
1879 * streams to find the one associated with the right session_id.
1882 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1884 if (stream
->session
->id
== session_id
) {
1885 stream
->data_pending_check_done
= 0;
1886 DBG("Set begin data pending flag to stream %" PRIu64
,
1887 stream
->stream_handle
);
1892 /* All good, send back reply. */
1893 reply
.ret_code
= htobe32(LTTNG_OK
);
1895 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1897 ERR("Relay begin data pending send reply failed");
1905 * End data pending command. This will check, for a given session id, if each
1906 * stream associated with it has its data_pending_check_done flag set. If not,
1907 * this means that the client lost track of the stream but the data is still
1908 * being streamed on our side. In this case, we inform the client that data is
1911 * Return to the client if there is data in flight or not with a ret_code.
1914 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1915 struct relay_command
*cmd
)
1918 struct lttng_ht_iter iter
;
1919 struct lttcomm_relayd_end_data_pending msg
;
1920 struct lttcomm_relayd_generic_reply reply
;
1921 struct relay_stream
*stream
;
1922 uint64_t session_id
;
1923 uint32_t is_data_inflight
= 0;
1928 DBG("End data pending command");
1930 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1931 ERR("Trying to check for data before version check");
1933 goto end_no_session
;
1936 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1937 if (ret
< sizeof(msg
)) {
1939 /* Orderly shutdown. Not necessary to print an error. */
1940 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1942 ERR("Relay didn't receive valid end data_pending struct size: %d",
1946 goto end_no_session
;
1949 session_id
= be64toh(msg
.session_id
);
1951 /* Iterate over all streams to see if the begin data pending flag is set. */
1953 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1955 if (stream
->session
->id
== session_id
&&
1956 !stream
->data_pending_check_done
) {
1957 is_data_inflight
= 1;
1958 DBG("Data is still in flight for stream %" PRIu64
,
1959 stream
->stream_handle
);
1965 /* All good, send back reply. */
1966 reply
.ret_code
= htobe32(is_data_inflight
);
1968 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1970 ERR("Relay end data pending send reply failed");
1978 * Receive an index for a specific stream.
1980 * Return 0 on success else a negative value.
1983 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1984 struct relay_command
*cmd
)
1986 int ret
, send_ret
, index_created
= 0;
1987 struct relay_session
*session
= cmd
->session
;
1988 struct lttcomm_relayd_index index_info
;
1989 struct relay_index
*index
, *wr_index
= NULL
;
1990 struct lttcomm_relayd_generic_reply reply
;
1991 struct relay_stream
*stream
;
1992 uint64_t net_seq_num
;
1996 DBG("Relay receiving index");
1998 if (!session
|| cmd
->version_check_done
== 0) {
1999 ERR("Trying to close a stream before version check");
2001 goto end_no_session
;
2004 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
2005 sizeof(index_info
), 0);
2006 if (ret
< sizeof(index_info
)) {
2008 /* Orderly shutdown. Not necessary to print an error. */
2009 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2011 ERR("Relay didn't receive valid index struct size : %d", ret
);
2014 goto end_no_session
;
2017 net_seq_num
= be64toh(index_info
.net_seq_num
);
2020 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
2023 goto end_rcu_unlock
;
2026 /* Live beacon handling */
2027 if (index_info
.packet_size
== 0) {
2028 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
2031 * Only flag a stream inactive when it has already received data.
2033 if (stream
->total_index_received
> 0) {
2034 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
2037 goto end_rcu_unlock
;
2039 stream
->beacon_ts_end
= -1ULL;
2042 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
2044 /* A successful creation will add the object to the HT. */
2045 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
2047 goto end_rcu_unlock
;
2052 copy_index_control_data(index
, &index_info
);
2054 if (index_created
) {
2056 * Try to add the relay index object to the hash table. If an object
2057 * already exist, destroy back the index created, set the data in this
2058 * object and write it on disk.
2060 relay_index_add(index
, &wr_index
);
2062 copy_index_control_data(wr_index
, &index_info
);
2066 /* The index already exists so write it on disk. */
2070 /* Do we have a writable ready index to write on disk. */
2072 /* Starting at 2.4, create the index file if none available. */
2073 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
2074 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2075 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2076 stream
->tracefile_count_current
);
2078 goto end_rcu_unlock
;
2080 stream
->index_fd
= ret
;
2083 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2085 goto end_rcu_unlock
;
2087 stream
->total_index_received
++;
2094 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2096 reply
.ret_code
= htobe32(LTTNG_OK
);
2098 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2100 ERR("Relay sending close index id reply");
2109 * Receive the streams_sent message.
2111 * Return 0 on success else a negative value.
2114 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2115 struct relay_command
*cmd
)
2118 struct lttcomm_relayd_generic_reply reply
;
2122 DBG("Relay receiving streams_sent");
2124 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
2125 ERR("Trying to close a stream before version check");
2127 goto end_no_session
;
2131 * Flag every pending stream in the connection recv list that they are
2132 * ready to be used by the viewer.
2134 set_viewer_ready_flag(cmd
);
2137 * Inform the viewer that there are new streams in the session.
2139 uatomic_set(&cmd
->session
->new_streams
, 1);
2141 reply
.ret_code
= htobe32(LTTNG_OK
);
2142 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
2144 ERR("Relay sending sent_stream reply");
2156 * Process the commands received on the control socket
2159 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2160 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
2164 switch (be32toh(recv_hdr
->cmd
)) {
2165 case RELAYD_CREATE_SESSION
:
2166 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
2168 case RELAYD_ADD_STREAM
:
2169 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
2171 case RELAYD_START_DATA
:
2172 ret
= relay_start(recv_hdr
, cmd
);
2174 case RELAYD_SEND_METADATA
:
2175 ret
= relay_recv_metadata(recv_hdr
, cmd
);
2177 case RELAYD_VERSION
:
2178 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
2180 case RELAYD_CLOSE_STREAM
:
2181 ret
= relay_close_stream(recv_hdr
, cmd
);
2183 case RELAYD_DATA_PENDING
:
2184 ret
= relay_data_pending(recv_hdr
, cmd
);
2186 case RELAYD_QUIESCENT_CONTROL
:
2187 ret
= relay_quiescent_control(recv_hdr
, cmd
);
2189 case RELAYD_BEGIN_DATA_PENDING
:
2190 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
2192 case RELAYD_END_DATA_PENDING
:
2193 ret
= relay_end_data_pending(recv_hdr
, cmd
);
2195 case RELAYD_SEND_INDEX
:
2196 ret
= relay_recv_index(recv_hdr
, cmd
);
2198 case RELAYD_STREAMS_SENT
:
2199 ret
= relay_streams_sent(recv_hdr
, cmd
);
2201 case RELAYD_UPDATE_SYNC_INFO
:
2203 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2204 relay_unknown_command(cmd
);
2214 * Handle index for a data stream.
2216 * RCU read side lock MUST be acquired.
2218 * Return 0 on success else a negative value.
2220 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2223 int ret
= 0, index_created
= 0;
2224 uint64_t stream_id
, data_offset
;
2225 struct relay_index
*index
, *wr_index
= NULL
;
2229 stream_id
= stream
->stream_handle
;
2230 /* Get data offset because we are about to update the index. */
2231 data_offset
= htobe64(stream
->tracefile_size_current
);
2234 * Lookup for an existing index for that stream id/sequence number. If on
2235 * exists, the control thread already received the data for it thus we need
2236 * to write it on disk.
2238 index
= relay_index_find(stream_id
, net_seq_num
);
2240 /* A successful creation will add the object to the HT. */
2241 index
= relay_index_create(stream_id
, net_seq_num
);
2249 if (rotate_index
|| stream
->index_fd
< 0) {
2250 index
->to_close_fd
= stream
->index_fd
;
2251 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2252 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2253 stream
->tracefile_count_current
);
2255 /* This will close the stream's index fd if one. */
2256 relay_index_free_safe(index
);
2259 stream
->index_fd
= ret
;
2261 index
->fd
= stream
->index_fd
;
2262 index
->index_data
.offset
= data_offset
;
2264 if (index_created
) {
2266 * Try to add the relay index object to the hash table. If an object
2267 * already exist, destroy back the index created and set the data.
2269 relay_index_add(index
, &wr_index
);
2271 /* Copy back data from the created index. */
2272 wr_index
->fd
= index
->fd
;
2273 wr_index
->to_close_fd
= index
->to_close_fd
;
2274 wr_index
->index_data
.offset
= data_offset
;
2278 /* The index already exists so write it on disk. */
2282 /* Do we have a writable ready index to write on disk. */
2284 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2288 stream
->total_index_received
++;
2296 * relay_process_data: Process the data received on the data socket
2299 int relay_process_data(struct relay_command
*cmd
)
2301 int ret
= 0, rotate_index
= 0;
2303 struct relay_stream
*stream
;
2304 struct lttcomm_relayd_data_hdr data_hdr
;
2306 uint64_t net_seq_num
;
2309 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2310 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2313 /* Orderly shutdown. Not necessary to print an error. */
2314 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2316 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2322 stream_id
= be64toh(data_hdr
.stream_id
);
2325 stream
= relay_stream_find_by_id(stream_id
);
2328 goto end_rcu_unlock
;
2331 data_size
= be32toh(data_hdr
.data_size
);
2332 if (data_buffer_size
< data_size
) {
2335 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2336 if (!tmp_data_ptr
) {
2337 ERR("Allocating data buffer");
2340 goto end_rcu_unlock
;
2342 data_buffer
= tmp_data_ptr
;
2343 data_buffer_size
= data_size
;
2345 memset(data_buffer
, 0, data_size
);
2347 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2349 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2350 data_size
, stream_id
, net_seq_num
);
2351 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2354 /* Orderly shutdown. Not necessary to print an error. */
2355 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2358 goto end_rcu_unlock
;
2361 /* Check if a rotation is needed. */
2362 if (stream
->tracefile_size
> 0 &&
2363 (stream
->tracefile_size_current
+ data_size
) >
2364 stream
->tracefile_size
) {
2365 struct relay_viewer_stream
*vstream
;
2368 new_id
= (stream
->tracefile_count_current
+ 1) %
2369 stream
->tracefile_count
;
2371 * When we wrap-around back to 0, we start overwriting old
2374 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2375 stream
->tracefile_overwrite
= 1;
2377 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2378 if (stream
->tracefile_overwrite
) {
2379 stream
->oldest_tracefile_id
=
2380 (stream
->oldest_tracefile_id
+ 1) %
2381 stream
->tracefile_count
;
2383 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2386 * The viewer is reading a file about to be
2387 * overwritten. Close the FDs it is
2388 * currently using and let it handle the fault.
2390 if (vstream
->tracefile_count_current
== new_id
) {
2391 pthread_mutex_lock(&vstream
->overwrite_lock
);
2392 vstream
->abort_flag
= 1;
2393 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2394 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2395 stream
->channel_name
, new_id
);
2396 } else if (vstream
->tracefile_count_current
==
2397 stream
->tracefile_count_current
) {
2399 * The reader and writer were in the
2400 * same trace file, inform the viewer
2401 * that no new index will ever be added
2404 vstream
->close_write_flag
= 1;
2407 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2408 stream
->tracefile_size
, stream
->tracefile_count
,
2409 relayd_uid
, relayd_gid
, stream
->fd
,
2410 &(stream
->tracefile_count_current
), &stream
->fd
);
2411 stream
->total_index_received
= 0;
2412 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2414 ERR("Rotating stream output file");
2415 goto end_rcu_unlock
;
2417 /* Reset current size because we just perform a stream rotation. */
2418 stream
->tracefile_size_current
= 0;
2423 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2424 * index are NOT supported.
2426 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2427 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2429 goto end_rcu_unlock
;
2433 /* Write data to stream output fd. */
2434 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2435 if (size_ret
< data_size
) {
2436 ERR("Relay error writing data to file");
2438 goto end_rcu_unlock
;
2441 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2442 ret
, stream
->stream_handle
);
2444 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2446 goto end_rcu_unlock
;
2448 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2450 stream
->prev_seq
= net_seq_num
;
2452 /* Check if we need to close the FD */
2453 if (close_stream_check(stream
)) {
2454 destroy_stream(stream
);
2464 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2468 lttng_poll_del(events
, pollfd
);
2470 ret
= close(pollfd
);
2472 ERR("Closing pollfd %d", pollfd
);
2477 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2478 struct lttng_ht
*relay_connections_ht
)
2480 struct relay_command
*relay_connection
;
2483 relay_connection
= zmalloc(sizeof(struct relay_command
));
2484 if (relay_connection
== NULL
) {
2485 PERROR("Relay command zmalloc");
2488 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2489 if (ret
< sizeof(struct relay_command
)) {
2490 PERROR("read relay cmd pipe");
2493 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2496 * Only used by the control side and the reference is copied inside each
2497 * stream from that connection. Thus a destroy HT must be done after every
2498 * stream has been destroyed.
2500 if (relay_connection
->type
== RELAY_CONTROL
) {
2501 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2502 LTTNG_HT_TYPE_STRING
);
2503 if (!relay_connection
->ctf_traces_ht
) {
2508 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2509 (unsigned long) relay_connection
->sock
->fd
);
2511 lttng_ht_add_unique_ulong(relay_connections_ht
,
2512 &relay_connection
->sock_n
);
2514 return lttng_poll_add(events
,
2515 relay_connection
->sock
->fd
,
2516 LPOLLIN
| LPOLLRDHUP
);
2519 free(relay_connection
);
2525 void deferred_free_connection(struct rcu_head
*head
)
2527 struct relay_command
*relay_connection
=
2528 caa_container_of(head
, struct relay_command
, rcu_node
);
2530 lttcomm_destroy_sock(relay_connection
->sock
);
2531 free(relay_connection
);
2535 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2536 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2537 struct lttng_ht
*sessions_ht
)
2541 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2544 if (relay_connection
->type
== RELAY_CONTROL
) {
2545 struct relay_stream_recv_handle
*node
, *tmp_node
;
2547 relay_delete_session(relay_connection
, sessions_ht
);
2548 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2550 /* Clean up recv list. */
2551 cds_list_for_each_entry_safe(node
, tmp_node
,
2552 &relay_connection
->recv_head
, node
) {
2553 cds_list_del(&node
->node
);
2558 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2562 * This thread does the actual work
2565 void *relay_thread_worker(void *data
)
2567 int ret
, err
= -1, last_seen_data_fd
= -1;
2569 struct relay_command
*relay_connection
;
2570 struct lttng_poll_event events
;
2571 struct lttng_ht
*relay_connections_ht
;
2572 struct lttng_ht_node_ulong
*node
;
2573 struct lttng_ht_iter iter
;
2574 struct lttcomm_relayd_hdr recv_hdr
;
2575 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2576 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2578 DBG("[thread] Relay worker started");
2580 rcu_register_thread();
2582 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2584 if (testpoint(relayd_thread_worker
)) {
2585 goto error_testpoint
;
2588 health_code_update();
2590 /* table of connections indexed on socket */
2591 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2592 if (!relay_connections_ht
) {
2593 goto relay_connections_ht_error
;
2596 /* Tables of received indexes indexed by index handle and net_seq_num. */
2597 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2599 goto indexes_ht_error
;
2602 ret
= create_thread_poll_set(&events
, 2);
2604 goto error_poll_create
;
2607 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2614 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2616 health_code_update();
2618 /* Infinite blocking call, waiting for transmission */
2619 DBG3("Relayd worker thread polling...");
2620 health_poll_entry();
2621 ret
= lttng_poll_wait(&events
, -1);
2625 * Restart interrupted system call.
2627 if (errno
== EINTR
) {
2636 * Process control. The control connection is prioritised so we don't
2637 * starve it with high throughout put tracing data on the data
2640 for (i
= 0; i
< nb_fd
; i
++) {
2641 /* Fetch once the poll data */
2642 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2643 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2645 health_code_update();
2647 /* Thread quit pipe has been closed. Killing thread. */
2648 ret
= check_thread_quit_pipe(pollfd
, revents
);
2654 /* Inspect the relay cmd pipe for new connection */
2655 if (pollfd
== relay_cmd_pipe
[0]) {
2656 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2657 ERR("Relay pipe error");
2659 } else if (revents
& LPOLLIN
) {
2660 DBG("Relay command received");
2661 ret
= relay_add_connection(relay_cmd_pipe
[0],
2662 &events
, relay_connections_ht
);
2667 } else if (revents
) {
2669 lttng_ht_lookup(relay_connections_ht
,
2670 (void *)((unsigned long) pollfd
),
2672 node
= lttng_ht_iter_get_node_ulong(&iter
);
2674 DBG2("Relay sock %d not found", pollfd
);
2678 relay_connection
= caa_container_of(node
,
2679 struct relay_command
, sock_n
);
2681 if (revents
& (LPOLLERR
)) {
2683 relay_cleanup_poll_connection(&events
, pollfd
);
2684 relay_del_connection(relay_connections_ht
,
2685 &iter
, relay_connection
, sessions_ht
);
2686 if (last_seen_data_fd
== pollfd
) {
2687 last_seen_data_fd
= last_notdel_data_fd
;
2689 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2690 DBG("Socket %d hung up", pollfd
);
2691 relay_cleanup_poll_connection(&events
, pollfd
);
2692 relay_del_connection(relay_connections_ht
,
2693 &iter
, relay_connection
, sessions_ht
);
2694 if (last_seen_data_fd
== pollfd
) {
2695 last_seen_data_fd
= last_notdel_data_fd
;
2697 } else if (revents
& LPOLLIN
) {
2698 /* control socket */
2699 if (relay_connection
->type
== RELAY_CONTROL
) {
2700 ret
= relay_connection
->sock
->ops
->recvmsg(
2701 relay_connection
->sock
, &recv_hdr
,
2702 sizeof(struct lttcomm_relayd_hdr
), 0);
2703 /* connection closed */
2705 relay_cleanup_poll_connection(&events
, pollfd
);
2706 relay_del_connection(relay_connections_ht
,
2707 &iter
, relay_connection
, sessions_ht
);
2708 DBG("Control connection closed with %d", pollfd
);
2710 if (relay_connection
->session
) {
2711 DBG2("Relay worker receiving data for session : %" PRIu64
,
2712 relay_connection
->session
->id
);
2714 ret
= relay_process_control(&recv_hdr
,
2715 relay_connection
, relay_ctx
);
2717 /* Clear the session on error. */
2718 relay_cleanup_poll_connection(&events
, pollfd
);
2719 relay_del_connection(relay_connections_ht
,
2720 &iter
, relay_connection
, sessions_ht
);
2721 DBG("Connection closed with %d", pollfd
);
2727 * Flag the last seen data fd not deleted. It will be
2728 * used as the last seen fd if any fd gets deleted in
2731 last_notdel_data_fd
= pollfd
;
2739 * The last loop handled a control request, go back to poll to make
2740 * sure we prioritise the control socket.
2746 if (last_seen_data_fd
>= 0) {
2747 for (i
= 0; i
< nb_fd
; i
++) {
2748 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2750 health_code_update();
2752 if (last_seen_data_fd
== pollfd
) {
2759 /* Process data connection. */
2760 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2761 /* Fetch the poll data. */
2762 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2763 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2765 health_code_update();
2767 /* Skip the command pipe. It's handled in the first loop. */
2768 if (pollfd
== relay_cmd_pipe
[0]) {
2774 lttng_ht_lookup(relay_connections_ht
,
2775 (void *)((unsigned long) pollfd
),
2777 node
= lttng_ht_iter_get_node_ulong(&iter
);
2779 /* Skip it. Might be removed before. */
2783 relay_connection
= caa_container_of(node
,
2784 struct relay_command
, sock_n
);
2786 if (revents
& LPOLLIN
) {
2787 if (relay_connection
->type
!= RELAY_DATA
) {
2791 ret
= relay_process_data(relay_connection
);
2792 /* connection closed */
2794 relay_cleanup_poll_connection(&events
, pollfd
);
2795 relay_del_connection(relay_connections_ht
,
2796 &iter
, relay_connection
, sessions_ht
);
2797 DBG("Data connection closed with %d", pollfd
);
2799 * Every goto restart call sets the last seen fd where
2800 * here we don't really care since we gracefully
2801 * continue the loop after the connection is deleted.
2804 /* Keep last seen port. */
2805 last_seen_data_fd
= pollfd
;
2813 last_seen_data_fd
= -1;
2816 /* Normal exit, no error */
2821 lttng_poll_clean(&events
);
2823 /* empty the hash table and free the memory */
2825 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2826 health_code_update();
2828 node
= lttng_ht_iter_get_node_ulong(&iter
);
2830 relay_connection
= caa_container_of(node
,
2831 struct relay_command
, sock_n
);
2832 relay_del_connection(relay_connections_ht
,
2833 &iter
, relay_connection
, sessions_ht
);
2838 lttng_ht_destroy(indexes_ht
);
2840 lttng_ht_destroy(relay_connections_ht
);
2841 relay_connections_ht_error
:
2842 /* Close relay cmd pipes */
2843 utils_close_pipe(relay_cmd_pipe
);
2845 DBG("Thread exited with error");
2847 DBG("Worker thread cleanup complete");
2852 ERR("Health error occurred in %s", __func__
);
2854 health_unregister(health_relayd
);
2855 rcu_unregister_thread();
2861 * Create the relay command pipe to wake thread_manage_apps.
2862 * Closed in cleanup().
2864 static int create_relay_cmd_pipe(void)
2868 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2876 int main(int argc
, char **argv
)
2880 struct relay_local_data
*relay_ctx
;
2882 /* Parse arguments */
2884 if ((ret
= set_options(argc
, argv
)) < 0) {
2888 if ((ret
= set_signal_handler()) < 0) {
2892 /* Try to create directory if -o, --output is specified. */
2893 if (opt_output_path
) {
2894 if (*opt_output_path
!= '/') {
2895 ERR("Please specify an absolute path for -o, --output PATH");
2899 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2901 ERR("Unable to create %s", opt_output_path
);
2907 if (opt_daemon
|| opt_background
) {
2910 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2917 * We are in the child. Make sure all other file
2918 * descriptors are closed, in case we are called with
2919 * more opened file descriptors than the standard ones.
2921 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2926 /* Create thread quit pipe */
2927 if ((ret
= init_thread_quit_pipe()) < 0) {
2931 /* We need those values for the file/dir creation. */
2932 relayd_uid
= getuid();
2933 relayd_gid
= getgid();
2935 /* Check if daemon is UID = 0 */
2936 if (relayd_uid
== 0) {
2937 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 || live_uri
->port
< 1024) {
2938 ERR("Need to be root to use ports < 1024");
2944 /* Setup the thread apps communication pipe. */
2945 if ((ret
= create_relay_cmd_pipe()) < 0) {
2949 /* Init relay command queue. */
2950 cds_wfq_init(&relay_cmd_queue
.queue
);
2952 /* Set up max poll set size */
2953 lttng_poll_set_max_size();
2955 /* Initialize communication library */
2957 lttcomm_inet_init();
2959 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2961 PERROR("relay_ctx");
2965 /* tables of sessions indexed by session ID */
2966 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2967 if (!relay_ctx
->sessions_ht
) {
2968 goto exit_relay_ctx_sessions
;
2971 /* tables of streams indexed by stream ID */
2972 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2973 if (!relay_streams_ht
) {
2974 goto exit_relay_ctx_streams
;
2977 /* tables of streams indexed by stream ID */
2978 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2979 if (!viewer_streams_ht
) {
2980 goto exit_relay_ctx_viewer_streams
;
2983 /* Initialize thread health monitoring */
2984 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2985 if (!health_relayd
) {
2986 PERROR("health_app_create error");
2987 goto exit_health_app_create
;
2990 ret
= utils_create_pipe(health_quit_pipe
);
2992 goto error_health_pipe
;
2995 /* Create thread to manage the client socket */
2996 ret
= pthread_create(&health_thread
, NULL
,
2997 thread_manage_health
, (void *) NULL
);
2999 PERROR("pthread_create health");
3003 /* Setup the dispatcher thread */
3004 ret
= pthread_create(&dispatcher_thread
, NULL
,
3005 relay_thread_dispatcher
, (void *) NULL
);
3007 PERROR("pthread_create dispatcher");
3008 goto exit_dispatcher
;
3011 /* Setup the worker thread */
3012 ret
= pthread_create(&worker_thread
, NULL
,
3013 relay_thread_worker
, (void *) relay_ctx
);
3015 PERROR("pthread_create worker");
3019 /* Setup the listener thread */
3020 ret
= pthread_create(&listener_thread
, NULL
,
3021 relay_thread_listener
, (void *) NULL
);
3023 PERROR("pthread_create listener");
3027 ret
= live_start_threads(live_uri
, relay_ctx
);
3029 ERR("Starting live viewer threads");
3034 ret
= pthread_join(listener_thread
, &status
);
3036 PERROR("pthread_join");
3037 goto error
; /* join error, exit without cleanup */
3041 ret
= pthread_join(worker_thread
, &status
);
3043 PERROR("pthread_join");
3044 goto error
; /* join error, exit without cleanup */
3048 ret
= pthread_join(dispatcher_thread
, &status
);
3050 PERROR("pthread_join");
3051 goto error
; /* join error, exit without cleanup */
3055 ret
= pthread_join(health_thread
, &status
);
3057 PERROR("pthread_join health thread");
3058 goto error
; /* join error, exit without cleanup */
3062 * Stop live threads only after joining other threads.
3064 live_stop_threads();
3067 utils_close_pipe(health_quit_pipe
);
3070 health_app_destroy(health_relayd
);
3072 exit_health_app_create
:
3073 lttng_ht_destroy(viewer_streams_ht
);
3075 exit_relay_ctx_viewer_streams
:
3076 lttng_ht_destroy(relay_streams_ht
);
3078 exit_relay_ctx_streams
:
3079 lttng_ht_destroy(relay_ctx
->sessions_ht
);
3081 exit_relay_ctx_sessions
: