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/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/daemonize.h>
50 #include <common/futex.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/sessiond-comm/inet.h>
53 #include <common/sessiond-comm/relayd.h>
54 #include <common/uri.h>
55 #include <common/utils.h>
56 #include <common/config/config.h>
59 #include "ctf-trace.h"
62 #include "lttng-relayd.h"
64 #include "health-relayd.h"
65 #include "testpoint.h"
66 #include "viewer-stream.h"
69 #include "connection.h"
71 /* command line options */
72 char *opt_output_path
;
73 static int opt_daemon
, opt_background
;
76 * We need to wait for listener and live listener threads, as well as
77 * health check thread, before being ready to signal readiness.
79 #define NR_LTTNG_RELAY_READY 3
80 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
81 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
82 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
84 static struct lttng_uri
*control_uri
;
85 static struct lttng_uri
*data_uri
;
86 static struct lttng_uri
*live_uri
;
90 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
91 static int tracing_group_name_override
;
93 const char * const config_section_name
= "relayd";
96 * Quit pipe for all threads. This permits a single cancellation point
97 * for all threads when receiving an event on the pipe.
99 int thread_quit_pipe
[2] = { -1, -1 };
102 * This pipe is used to inform the worker thread that a command is queued and
103 * ready to be processed.
105 static int relay_conn_pipe
[2] = { -1, -1 };
107 /* Shared between threads */
108 static int dispatch_thread_exit
;
110 static pthread_t listener_thread
;
111 static pthread_t dispatcher_thread
;
112 static pthread_t worker_thread
;
113 static pthread_t health_thread
;
115 static uint64_t last_relay_stream_id
;
118 * Relay command queue.
120 * The relay_thread_listener and relay_thread_dispatcher communicate with this
123 static struct relay_conn_queue relay_conn_queue
;
125 /* buffer allocated at startup, used to store the trace data */
126 static char *data_buffer
;
127 static unsigned int data_buffer_size
;
129 /* We need those values for the file/dir creation. */
130 static uid_t relayd_uid
;
131 static gid_t relayd_gid
;
133 /* Global relay stream hash table. */
134 struct lttng_ht
*relay_streams_ht
;
136 /* Global relay viewer stream hash table. */
137 struct lttng_ht
*viewer_streams_ht
;
139 /* Global hash table that stores relay index object. */
140 struct lttng_ht
*indexes_ht
;
142 /* Relayd health monitoring */
143 struct health_app
*health_relayd
;
145 static struct option long_options
[] = {
146 { "control-port", 1, 0, 'C', },
147 { "data-port", 1, 0, 'D', },
148 { "live-port", 1, 0, 'L', },
149 { "daemonize", 0, 0, 'd', },
150 { "background", 0, 0, 'b', },
151 { "group", 1, 0, 'g', },
152 { "help", 0, 0, 'h', },
153 { "output", 1, 0, 'o', },
154 { "verbose", 0, 0, 'v', },
155 { "config", 1, 0, 'f' },
159 static const char *config_ignore_options
[] = { "help", "config" };
162 * usage function on stderr
167 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
168 fprintf(stderr
, " -h, --help Display this usage.\n");
169 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
170 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
171 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
172 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
173 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
174 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
175 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
176 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
177 fprintf(stderr
, " -f --config Load daemon configuration file\n");
181 * Take an option from the getopt output and set it in the right variable to be
184 * Return 0 on success else a negative value.
187 int set_option(int opt
, const char *arg
, const char *optname
)
193 fprintf(stderr
, "option %s", optname
);
195 fprintf(stderr
, " with arg %s\n", arg
);
199 ret
= uri_parse(arg
, &control_uri
);
201 ERR("Invalid control URI specified");
204 if (control_uri
->port
== 0) {
205 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
209 ret
= uri_parse(arg
, &data_uri
);
211 ERR("Invalid data URI specified");
214 if (data_uri
->port
== 0) {
215 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
219 ret
= uri_parse(arg
, &live_uri
);
221 ERR("Invalid live URI specified");
224 if (live_uri
->port
== 0) {
225 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
235 tracing_group_name
= strdup(arg
);
236 tracing_group_name_override
= 1;
242 ret
= asprintf(&opt_output_path
, "%s", arg
);
245 PERROR("asprintf opt_output_path");
250 /* Verbose level can increase using multiple -v */
252 lttng_opt_verbose
= config_parse_value(arg
);
254 /* Only 3 level of verbosity (-vvv). */
255 if (lttng_opt_verbose
< 3) {
256 lttng_opt_verbose
+= 1;
261 /* Unknown option or other error.
262 * Error is printed by getopt, just return */
275 * config_entry_handler_cb used to handle options read from a config file.
276 * See config_entry_handler_cb comment in common/config/config.h for the
277 * return value conventions.
280 int config_entry_handler(const struct config_entry
*entry
, void *unused
)
284 if (!entry
|| !entry
->name
|| !entry
->value
) {
289 /* Check if the option is to be ignored */
290 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
291 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
296 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
297 /* Ignore if entry name is not fully matched. */
298 if (strcmp(entry
->name
, long_options
[i
].name
)) {
303 * If the option takes no argument on the command line, we have to
304 * check if the value is "true". We support non-zero numeric values,
307 if (!long_options
[i
].has_arg
) {
308 ret
= config_parse_value(entry
->value
);
311 WARN("Invalid configuration value \"%s\" for option %s",
312 entry
->value
, entry
->name
);
314 /* False, skip boolean config option. */
319 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
323 WARN("Unrecognized option \"%s\" in daemon configuration file.",
331 int set_options(int argc
, char **argv
)
333 int c
, ret
= 0, option_index
= 0;
334 int orig_optopt
= optopt
, orig_optind
= optind
;
335 char *default_address
, *optstring
;
336 const char *config_path
= NULL
;
338 optstring
= utils_generate_optstring(long_options
,
339 sizeof(long_options
) / sizeof(struct option
));
345 /* Check for the --config option */
347 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
348 &option_index
)) != -1) {
352 } else if (c
!= 'f') {
356 config_path
= utils_expand_path(optarg
);
358 ERR("Failed to resolve path: %s", optarg
);
362 ret
= config_get_section_entries(config_path
, config_section_name
,
363 config_entry_handler
, NULL
);
366 ERR("Invalid configuration option at line %i", ret
);
372 /* Reset getopt's global state */
373 optopt
= orig_optopt
;
374 optind
= orig_optind
;
376 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
381 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
387 /* assign default values */
388 if (control_uri
== NULL
) {
389 ret
= asprintf(&default_address
,
390 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
391 DEFAULT_NETWORK_CONTROL_PORT
);
393 PERROR("asprintf default data address");
397 ret
= uri_parse(default_address
, &control_uri
);
398 free(default_address
);
400 ERR("Invalid control URI specified");
404 if (data_uri
== NULL
) {
405 ret
= asprintf(&default_address
,
406 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
407 DEFAULT_NETWORK_DATA_PORT
);
409 PERROR("asprintf default data address");
413 ret
= uri_parse(default_address
, &data_uri
);
414 free(default_address
);
416 ERR("Invalid data URI specified");
420 if (live_uri
== NULL
) {
421 ret
= asprintf(&default_address
,
422 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
423 DEFAULT_NETWORK_VIEWER_PORT
);
425 PERROR("asprintf default viewer control address");
429 ret
= uri_parse(default_address
, &live_uri
);
430 free(default_address
);
432 ERR("Invalid viewer control URI specified");
450 /* free the dynamically allocated opt_output_path */
451 free(opt_output_path
);
453 /* Close thread quit pipes */
454 utils_close_pipe(thread_quit_pipe
);
456 uri_free(control_uri
);
458 /* Live URI is freed in the live thread. */
460 if (tracing_group_name_override
) {
461 free((void *) tracing_group_name
);
466 * Write to writable pipe used to notify a thread.
469 int notify_thread_pipe(int wpipe
)
473 ret
= lttng_write(wpipe
, "!", 1);
475 PERROR("write poll pipe");
481 static void notify_health_quit_pipe(int *pipe
)
485 ret
= lttng_write(pipe
[1], "4", 1);
487 PERROR("write relay health quit");
492 * Stop all threads by closing the thread quit pipe.
495 void stop_threads(void)
499 /* Stopping all threads */
500 DBG("Terminating all threads");
501 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
503 ERR("write error on thread quit pipe");
506 notify_health_quit_pipe(health_quit_pipe
);
508 /* Dispatch thread */
509 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
510 futex_nto1_wake(&relay_conn_queue
.futex
);
514 * Signal handler for the daemon
516 * Simply stop all worker threads, leaving main() return gracefully after
517 * joining all threads and calling cleanup().
520 void sighandler(int sig
)
524 DBG("SIGPIPE caught");
527 DBG("SIGINT caught");
531 DBG("SIGTERM caught");
535 CMM_STORE_SHARED(recv_child_signal
, 1);
543 * Setup signal handler for :
544 * SIGINT, SIGTERM, SIGPIPE
547 int set_signal_handler(void)
553 if ((ret
= sigemptyset(&sigset
)) < 0) {
554 PERROR("sigemptyset");
558 sa
.sa_handler
= sighandler
;
561 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
566 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
571 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
576 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
581 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
586 void lttng_relay_notify_ready(void)
588 /* Notify the parent of the fork() process that we are ready. */
589 if (opt_daemon
|| opt_background
) {
590 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
591 kill(child_ppid
, SIGUSR1
);
597 * Init thread quit pipe.
599 * Return -1 on error or 0 if all pipes are created.
602 int init_thread_quit_pipe(void)
606 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
612 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
615 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
619 if (events
== NULL
|| size
== 0) {
624 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
630 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
642 * Check if the thread quit pipe was triggered.
644 * Return 1 if it was triggered else 0;
647 int check_thread_quit_pipe(int fd
, uint32_t events
)
649 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
657 * Create and init socket from uri.
660 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
663 struct lttcomm_sock
*sock
= NULL
;
665 sock
= lttcomm_alloc_sock_from_uri(uri
);
667 ERR("Allocating socket");
671 ret
= lttcomm_create_sock(sock
);
675 DBG("Listening on sock %d", sock
->fd
);
677 ret
= sock
->ops
->bind(sock
);
682 ret
= sock
->ops
->listen(sock
, -1);
692 lttcomm_destroy_sock(sock
);
698 * Return nonzero if stream needs to be closed.
701 int close_stream_check(struct relay_stream
*stream
)
703 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
705 * We are about to close the stream so set the data pending flag to 1
706 * which will make the end data pending command skip the stream which
707 * is now closed and ready. Note that after proceeding to a file close,
708 * the written file is ready for reading.
710 stream
->data_pending_check_done
= 1;
716 static void try_close_stream(struct relay_session
*session
,
717 struct relay_stream
*stream
)
720 struct ctf_trace
*ctf_trace
;
725 if (!close_stream_check(stream
)) {
726 /* Can't close it, not ready for that. */
730 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
734 pthread_mutex_lock(&session
->viewer_ready_lock
);
735 ctf_trace
->invalid_flag
= 1;
736 pthread_mutex_unlock(&session
->viewer_ready_lock
);
738 ret
= stream_close(session
, stream
);
739 if (ret
|| session
->snapshot
) {
740 /* Already close thus the ctf trace is being or has been destroyed. */
744 ctf_trace_try_destroy(session
, ctf_trace
);
751 * This thread manages the listening for new connections on the network
754 void *relay_thread_listener(void *data
)
756 int i
, ret
, pollfd
, err
= -1;
757 uint32_t revents
, nb_fd
;
758 struct lttng_poll_event events
;
759 struct lttcomm_sock
*control_sock
, *data_sock
;
761 DBG("[thread] Relay listener started");
763 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
765 health_code_update();
767 control_sock
= relay_init_sock(control_uri
);
769 goto error_sock_control
;
772 data_sock
= relay_init_sock(data_uri
);
774 goto error_sock_relay
;
778 * Pass 3 as size here for the thread quit pipe, control and data socket.
780 ret
= create_thread_poll_set(&events
, 3);
782 goto error_create_poll
;
785 /* Add the control socket */
786 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
791 /* Add the data socket */
792 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
797 lttng_relay_notify_ready();
799 if (testpoint(relayd_thread_listener
)) {
800 goto error_testpoint
;
804 health_code_update();
806 DBG("Listener accepting connections");
810 ret
= lttng_poll_wait(&events
, -1);
814 * Restart interrupted system call.
816 if (errno
== EINTR
) {
824 DBG("Relay new connection received");
825 for (i
= 0; i
< nb_fd
; i
++) {
826 health_code_update();
828 /* Fetch once the poll data */
829 revents
= LTTNG_POLL_GETEV(&events
, i
);
830 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
832 /* Thread quit pipe has been closed. Killing thread. */
833 ret
= check_thread_quit_pipe(pollfd
, revents
);
839 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
840 ERR("socket poll error");
842 } else if (revents
& LPOLLIN
) {
844 * Get allocated in this thread, enqueued to a global queue,
845 * dequeued and freed in the worker thread.
848 struct relay_connection
*new_conn
;
849 struct lttcomm_sock
*newsock
;
851 new_conn
= connection_create();
856 if (pollfd
== data_sock
->fd
) {
857 new_conn
->type
= RELAY_DATA
;
858 newsock
= data_sock
->ops
->accept(data_sock
);
859 DBG("Relay data connection accepted, socket %d",
862 assert(pollfd
== control_sock
->fd
);
863 new_conn
->type
= RELAY_CONTROL
;
864 newsock
= control_sock
->ops
->accept(control_sock
);
865 DBG("Relay control connection accepted, socket %d",
869 PERROR("accepting sock");
870 connection_free(new_conn
);
874 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
877 PERROR("setsockopt inet");
878 lttcomm_destroy_sock(newsock
);
879 connection_free(new_conn
);
882 new_conn
->sock
= newsock
;
884 /* Enqueue request for the dispatcher thread. */
885 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
889 * Wake the dispatch queue futex. Implicit memory barrier with
890 * the exchange in cds_wfcq_enqueue.
892 futex_nto1_wake(&relay_conn_queue
.futex
);
901 lttng_poll_clean(&events
);
903 if (data_sock
->fd
>= 0) {
904 ret
= data_sock
->ops
->close(data_sock
);
909 lttcomm_destroy_sock(data_sock
);
911 if (control_sock
->fd
>= 0) {
912 ret
= control_sock
->ops
->close(control_sock
);
917 lttcomm_destroy_sock(control_sock
);
921 ERR("Health error occurred in %s", __func__
);
923 health_unregister(health_relayd
);
924 DBG("Relay listener thread cleanup complete");
930 * This thread manages the dispatching of the requests to worker threads
933 void *relay_thread_dispatcher(void *data
)
937 struct cds_wfcq_node
*node
;
938 struct relay_connection
*new_conn
= NULL
;
940 DBG("[thread] Relay dispatcher started");
942 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
944 if (testpoint(relayd_thread_dispatcher
)) {
945 goto error_testpoint
;
948 health_code_update();
950 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
951 health_code_update();
953 /* Atomically prepare the queue futex */
954 futex_nto1_prepare(&relay_conn_queue
.futex
);
957 health_code_update();
959 /* Dequeue commands */
960 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
961 &relay_conn_queue
.tail
);
963 DBG("Woken up but nothing in the relay command queue");
964 /* Continue thread execution */
967 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
969 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
972 * Inform worker thread of the new request. This call is blocking
973 * so we can be assured that the data will be read at some point in
974 * time or wait to the end of the world :)
976 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
978 PERROR("write connection pipe");
979 connection_destroy(new_conn
);
982 } while (node
!= NULL
);
984 /* Futex wait on queue. Blocking call on futex() */
986 futex_nto1_wait(&relay_conn_queue
.futex
);
990 /* Normal exit, no error */
997 ERR("Health error occurred in %s", __func__
);
999 health_unregister(health_relayd
);
1000 DBG("Dispatch thread dying");
1005 static void try_close_streams(struct relay_session
*session
)
1007 struct ctf_trace
*ctf_trace
;
1008 struct lttng_ht_iter iter
;
1012 pthread_mutex_lock(&session
->viewer_ready_lock
);
1014 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
1016 struct relay_stream
*stream
;
1018 /* Close streams. */
1019 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
1020 stream_close(session
, stream
);
1023 ctf_trace
->invalid_flag
= 1;
1024 ctf_trace_try_destroy(session
, ctf_trace
);
1027 pthread_mutex_unlock(&session
->viewer_ready_lock
);
1031 * Try to destroy a session within a connection.
1033 static void destroy_session(struct relay_session
*session
,
1034 struct lttng_ht
*sessions_ht
)
1037 assert(sessions_ht
);
1039 /* Indicate that this session can be destroyed from now on. */
1040 session
->close_flag
= 1;
1042 try_close_streams(session
);
1045 * This will try to delete and destroy the session if no viewer is attached
1046 * to it meaning the refcount is down to zero.
1048 session_try_destroy(sessions_ht
, session
);
1052 * Copy index data from the control port to a given index object.
1054 static void copy_index_control_data(struct relay_index
*index
,
1055 struct lttcomm_relayd_index
*data
)
1061 * The index on disk is encoded in big endian, so we don't need to convert
1062 * the data received on the network. The data_offset value is NEVER
1063 * modified here and is updated by the data thread.
1065 index
->index_data
.packet_size
= data
->packet_size
;
1066 index
->index_data
.content_size
= data
->content_size
;
1067 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
1068 index
->index_data
.timestamp_end
= data
->timestamp_end
;
1069 index
->index_data
.events_discarded
= data
->events_discarded
;
1070 index
->index_data
.stream_id
= data
->stream_id
;
1074 * Handle the RELAYD_CREATE_SESSION command.
1076 * On success, send back the session id or else return a negative value.
1079 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1080 struct relay_connection
*conn
)
1082 int ret
= 0, send_ret
;
1083 struct relay_session
*session
;
1084 struct lttcomm_relayd_status_session reply
;
1089 memset(&reply
, 0, sizeof(reply
));
1091 session
= session_create();
1096 session
->minor
= conn
->minor
;
1097 session
->major
= conn
->major
;
1098 conn
->session_id
= session
->id
;
1099 conn
->session
= session
;
1101 reply
.session_id
= htobe64(session
->id
);
1103 switch (conn
->minor
) {
1108 case 4: /* LTTng sessiond 2.4 */
1110 ret
= cmd_create_session_2_4(conn
, session
);
1113 lttng_ht_add_unique_u64(conn
->sessions_ht
, &session
->session_n
);
1114 DBG("Created session %" PRIu64
, session
->id
);
1118 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1120 reply
.ret_code
= htobe32(LTTNG_OK
);
1123 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1125 ERR("Relayd sending session id");
1133 * When we have received all the streams and the metadata for a channel,
1134 * we make them visible to the viewer threads.
1137 void set_viewer_ready_flag(struct relay_connection
*conn
)
1139 struct relay_stream
*stream
, *tmp_stream
;
1141 pthread_mutex_lock(&conn
->session
->viewer_ready_lock
);
1142 cds_list_for_each_entry_safe(stream
, tmp_stream
, &conn
->recv_head
,
1144 stream
->viewer_ready
= 1;
1145 cds_list_del(&stream
->recv_list
);
1147 pthread_mutex_unlock(&conn
->session
->viewer_ready_lock
);
1152 * Add a recv handle node to the connection recv list with the given stream
1153 * handle. A new node is allocated thus must be freed when the node is deleted
1156 static void queue_stream(struct relay_stream
*stream
,
1157 struct relay_connection
*conn
)
1162 cds_list_add(&stream
->recv_list
, &conn
->recv_head
);
1166 * relay_add_stream: allocate a new stream for a session
1169 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1170 struct relay_connection
*conn
)
1173 struct relay_session
*session
= conn
->session
;
1174 struct relay_stream
*stream
= NULL
;
1175 struct lttcomm_relayd_status_stream reply
;
1176 struct ctf_trace
*trace
;
1178 if (!session
|| conn
->version_check_done
== 0) {
1179 ERR("Trying to add a stream before version check");
1181 goto end_no_session
;
1184 stream
= zmalloc(sizeof(struct relay_stream
));
1185 if (stream
== NULL
) {
1186 PERROR("relay stream zmalloc");
1188 goto end_no_session
;
1191 switch (conn
->minor
) {
1192 case 1: /* LTTng sessiond 2.1 */
1193 ret
= cmd_recv_stream_2_1(conn
, stream
);
1195 case 2: /* LTTng sessiond 2.2 */
1197 ret
= cmd_recv_stream_2_2(conn
, stream
);
1201 goto err_free_stream
;
1205 stream
->stream_handle
= ++last_relay_stream_id
;
1206 stream
->prev_seq
= -1ULL;
1207 stream
->session_id
= session
->id
;
1208 stream
->index_fd
= -1;
1209 stream
->read_index_fd
= -1;
1210 stream
->ctf_stream_id
= -1ULL;
1211 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
1212 pthread_mutex_init(&stream
->lock
, NULL
);
1214 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1216 ERR("relay creating output directory");
1221 * No need to use run_as API here because whatever we receives, the relayd
1222 * uses its own credentials for the stream files.
1224 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1225 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1227 ERR("Create output file");
1231 if (stream
->tracefile_size
) {
1232 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1234 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1237 trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, stream
->path_name
);
1239 trace
= ctf_trace_create(stream
->path_name
);
1244 ctf_trace_add(session
->ctf_traces_ht
, trace
);
1246 ctf_trace_get_ref(trace
);
1248 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1249 stream
->metadata_flag
= 1;
1250 /* Assign quick reference to the metadata stream in the trace. */
1251 trace
->metadata_stream
= stream
;
1255 * Add the stream in the recv list of the connection. Once the end stream
1256 * message is received, this list is emptied and streams are set with the
1257 * viewer ready flag.
1259 queue_stream(stream
, conn
);
1262 * Both in the ctf_trace object and the global stream ht since the data
1263 * side of the relayd does not have the concept of session.
1265 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
1266 cds_list_add_tail(&stream
->trace_list
, &trace
->stream_list
);
1268 session
->stream_count
++;
1270 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1271 stream
->stream_handle
);
1274 memset(&reply
, 0, sizeof(reply
));
1275 reply
.handle
= htobe64(stream
->stream_handle
);
1276 /* send the session id to the client or a negative return code on error */
1278 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1279 /* stream was not properly added to the ht, so free it */
1282 reply
.ret_code
= htobe32(LTTNG_OK
);
1285 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1286 sizeof(struct lttcomm_relayd_status_stream
), 0);
1288 ERR("Relay sending stream id");
1297 free(stream
->path_name
);
1298 free(stream
->channel_name
);
1304 * relay_close_stream: close a specific stream
1307 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1308 struct relay_connection
*conn
)
1311 struct relay_session
*session
= conn
->session
;
1312 struct lttcomm_relayd_close_stream stream_info
;
1313 struct lttcomm_relayd_generic_reply reply
;
1314 struct relay_stream
*stream
;
1316 DBG("Close stream received");
1318 if (!session
|| conn
->version_check_done
== 0) {
1319 ERR("Trying to close a stream before version check");
1321 goto end_no_session
;
1324 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1325 sizeof(struct lttcomm_relayd_close_stream
), 0);
1326 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1328 /* Orderly shutdown. Not necessary to print an error. */
1329 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1331 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1334 goto end_no_session
;
1338 stream
= stream_find_by_id(relay_streams_ht
,
1339 be64toh(stream_info
.stream_id
));
1345 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1346 stream
->close_flag
= 1;
1347 session
->stream_count
--;
1349 /* Check if we can close it or else the data will do it. */
1350 try_close_stream(session
, stream
);
1355 memset(&reply
, 0, sizeof(reply
));
1357 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1359 reply
.ret_code
= htobe32(LTTNG_OK
);
1361 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1362 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1364 ERR("Relay sending stream id");
1373 * relay_unknown_command: send -1 if received unknown command
1376 void relay_unknown_command(struct relay_connection
*conn
)
1378 struct lttcomm_relayd_generic_reply reply
;
1381 memset(&reply
, 0, sizeof(reply
));
1382 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1383 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1384 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1386 ERR("Relay sending unknown command");
1391 * relay_start: send an acknowledgment to the client to tell if we are
1392 * ready to receive data. We are ready if a session is established.
1395 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1396 struct relay_connection
*conn
)
1398 int ret
= htobe32(LTTNG_OK
);
1399 struct lttcomm_relayd_generic_reply reply
;
1400 struct relay_session
*session
= conn
->session
;
1403 DBG("Trying to start the streaming without a session established");
1404 ret
= htobe32(LTTNG_ERR_UNK
);
1407 memset(&reply
, 0, sizeof(reply
));
1408 reply
.ret_code
= ret
;
1409 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1410 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1412 ERR("Relay sending start ack");
1419 * Append padding to the file pointed by the file descriptor fd.
1421 static int write_padding_to_file(int fd
, uint32_t size
)
1430 zeros
= zmalloc(size
);
1431 if (zeros
== NULL
) {
1432 PERROR("zmalloc zeros for padding");
1437 ret
= lttng_write(fd
, zeros
, size
);
1439 PERROR("write padding to file");
1449 * relay_recv_metadata: receive the metada for the session.
1452 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1453 struct relay_connection
*conn
)
1455 int ret
= htobe32(LTTNG_OK
);
1457 struct relay_session
*session
= conn
->session
;
1458 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1459 struct relay_stream
*metadata_stream
;
1460 uint64_t data_size
, payload_size
;
1461 struct ctf_trace
*ctf_trace
;
1464 ERR("Metadata sent before version check");
1469 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1470 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1471 ERR("Incorrect data size");
1475 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1477 if (data_buffer_size
< data_size
) {
1478 /* In case the realloc fails, we can free the memory */
1481 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1482 if (!tmp_data_ptr
) {
1483 ERR("Allocating data buffer");
1488 data_buffer
= tmp_data_ptr
;
1489 data_buffer_size
= data_size
;
1491 memset(data_buffer
, 0, data_size
);
1492 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1493 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1494 if (ret
< 0 || ret
!= data_size
) {
1496 /* Orderly shutdown. Not necessary to print an error. */
1497 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1499 ERR("Relay didn't receive the whole metadata");
1504 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1507 metadata_stream
= stream_find_by_id(relay_streams_ht
,
1508 be64toh(metadata_struct
->stream_id
));
1509 if (!metadata_stream
) {
1514 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1516 if (size_ret
< payload_size
) {
1517 ERR("Relay error writing metadata on file");
1522 ret
= write_padding_to_file(metadata_stream
->fd
,
1523 be32toh(metadata_struct
->padding_size
));
1528 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1529 metadata_stream
->path_name
);
1531 ctf_trace
->metadata_received
+=
1532 payload_size
+ be32toh(metadata_struct
->padding_size
);
1534 DBG2("Relay metadata written");
1543 * relay_send_version: send relayd version number
1546 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1547 struct relay_connection
*conn
)
1550 struct lttcomm_relayd_version reply
, msg
;
1554 conn
->version_check_done
= 1;
1556 /* Get version from the other side. */
1557 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1558 if (ret
< 0 || ret
!= sizeof(msg
)) {
1560 /* Orderly shutdown. Not necessary to print an error. */
1561 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1563 ERR("Relay failed to receive the version values.");
1569 memset(&reply
, 0, sizeof(reply
));
1570 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1571 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1573 /* Major versions must be the same */
1574 if (reply
.major
!= be32toh(msg
.major
)) {
1575 DBG("Incompatible major versions (%u vs %u), deleting session",
1576 reply
.major
, be32toh(msg
.major
));
1577 destroy_session(conn
->session
, conn
->sessions_ht
);
1582 conn
->major
= reply
.major
;
1583 /* We adapt to the lowest compatible version */
1584 if (reply
.minor
<= be32toh(msg
.minor
)) {
1585 conn
->minor
= reply
.minor
;
1587 conn
->minor
= be32toh(msg
.minor
);
1590 reply
.major
= htobe32(reply
.major
);
1591 reply
.minor
= htobe32(reply
.minor
);
1592 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1593 sizeof(struct lttcomm_relayd_version
), 0);
1595 ERR("Relay sending version");
1598 DBG("Version check done using protocol %u.%u", conn
->major
,
1606 * Check for data pending for a given stream id from the session daemon.
1609 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1610 struct relay_connection
*conn
)
1612 struct relay_session
*session
= conn
->session
;
1613 struct lttcomm_relayd_data_pending msg
;
1614 struct lttcomm_relayd_generic_reply reply
;
1615 struct relay_stream
*stream
;
1617 uint64_t last_net_seq_num
, stream_id
;
1619 DBG("Data pending command received");
1621 if (!session
|| conn
->version_check_done
== 0) {
1622 ERR("Trying to check for data before version check");
1624 goto end_no_session
;
1627 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1628 if (ret
< sizeof(msg
)) {
1630 /* Orderly shutdown. Not necessary to print an error. */
1631 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1633 ERR("Relay didn't receive valid data_pending struct size : %d",
1637 goto end_no_session
;
1640 stream_id
= be64toh(msg
.stream_id
);
1641 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1644 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
1645 if (stream
== NULL
) {
1650 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1651 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1654 /* Avoid wrapping issue */
1655 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1656 /* Data has in fact been written and is NOT pending */
1659 /* Data still being streamed thus pending */
1663 /* Pending check is now done. */
1664 stream
->data_pending_check_done
= 1;
1669 memset(&reply
, 0, sizeof(reply
));
1670 reply
.ret_code
= htobe32(ret
);
1671 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1673 ERR("Relay data pending ret code failed");
1681 * Wait for the control socket to reach a quiescent state.
1683 * Note that for now, when receiving this command from the session daemon, this
1684 * means that every subsequent commands or data received on the control socket
1685 * has been handled. So, this is why we simply return OK here.
1688 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1689 struct relay_connection
*conn
)
1693 struct relay_stream
*stream
;
1694 struct lttng_ht_iter iter
;
1695 struct lttcomm_relayd_quiescent_control msg
;
1696 struct lttcomm_relayd_generic_reply reply
;
1698 DBG("Checking quiescent state on control socket");
1700 if (!conn
->session
|| conn
->version_check_done
== 0) {
1701 ERR("Trying to check for data before version check");
1703 goto end_no_session
;
1706 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1707 if (ret
< sizeof(msg
)) {
1709 /* Orderly shutdown. Not necessary to print an error. */
1710 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1712 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1716 goto end_no_session
;
1719 stream_id
= be64toh(msg
.stream_id
);
1722 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1724 if (stream
->stream_handle
== stream_id
) {
1725 stream
->data_pending_check_done
= 1;
1726 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1733 memset(&reply
, 0, sizeof(reply
));
1734 reply
.ret_code
= htobe32(LTTNG_OK
);
1735 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1737 ERR("Relay data quiescent control ret code failed");
1745 * Initialize a data pending command. This means that a client is about to ask
1746 * for data pending for each stream he/she holds. Simply iterate over all
1747 * streams of a session and set the data_pending_check_done flag.
1749 * This command returns to the client a LTTNG_OK code.
1752 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1753 struct relay_connection
*conn
)
1756 struct lttng_ht_iter iter
;
1757 struct lttcomm_relayd_begin_data_pending msg
;
1758 struct lttcomm_relayd_generic_reply reply
;
1759 struct relay_stream
*stream
;
1760 uint64_t session_id
;
1765 DBG("Init streams for data pending");
1767 if (!conn
->session
|| conn
->version_check_done
== 0) {
1768 ERR("Trying to check for data before version check");
1770 goto end_no_session
;
1773 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1774 if (ret
< sizeof(msg
)) {
1776 /* Orderly shutdown. Not necessary to print an error. */
1777 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1779 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1783 goto end_no_session
;
1786 session_id
= be64toh(msg
.session_id
);
1789 * Iterate over all streams to set the begin data pending flag. For now, the
1790 * streams are indexed by stream handle so we have to iterate over all
1791 * streams to find the one associated with the right session_id.
1794 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1796 if (stream
->session_id
== session_id
) {
1797 stream
->data_pending_check_done
= 0;
1798 DBG("Set begin data pending flag to stream %" PRIu64
,
1799 stream
->stream_handle
);
1804 memset(&reply
, 0, sizeof(reply
));
1805 /* All good, send back reply. */
1806 reply
.ret_code
= htobe32(LTTNG_OK
);
1808 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1810 ERR("Relay begin data pending send reply failed");
1818 * End data pending command. This will check, for a given session id, if each
1819 * stream associated with it has its data_pending_check_done flag set. If not,
1820 * this means that the client lost track of the stream but the data is still
1821 * being streamed on our side. In this case, we inform the client that data is
1824 * Return to the client if there is data in flight or not with a ret_code.
1827 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1828 struct relay_connection
*conn
)
1831 struct lttng_ht_iter iter
;
1832 struct lttcomm_relayd_end_data_pending msg
;
1833 struct lttcomm_relayd_generic_reply reply
;
1834 struct relay_stream
*stream
;
1835 uint64_t session_id
;
1836 uint32_t is_data_inflight
= 0;
1841 DBG("End data pending command");
1843 if (!conn
->session
|| conn
->version_check_done
== 0) {
1844 ERR("Trying to check for data before version check");
1846 goto end_no_session
;
1849 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1850 if (ret
< sizeof(msg
)) {
1852 /* Orderly shutdown. Not necessary to print an error. */
1853 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1855 ERR("Relay didn't receive valid end data_pending struct size: %d",
1859 goto end_no_session
;
1862 session_id
= be64toh(msg
.session_id
);
1864 /* Iterate over all streams to see if the begin data pending flag is set. */
1866 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1868 if (stream
->session_id
== session_id
&&
1869 !stream
->data_pending_check_done
&& !stream
->terminated_flag
) {
1870 is_data_inflight
= 1;
1871 DBG("Data is still in flight for stream %" PRIu64
,
1872 stream
->stream_handle
);
1878 memset(&reply
, 0, sizeof(reply
));
1879 /* All good, send back reply. */
1880 reply
.ret_code
= htobe32(is_data_inflight
);
1882 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1884 ERR("Relay end data pending send reply failed");
1892 * Receive an index for a specific stream.
1894 * Return 0 on success else a negative value.
1897 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1898 struct relay_connection
*conn
)
1900 int ret
, send_ret
, index_created
= 0;
1901 struct relay_session
*session
= conn
->session
;
1902 struct lttcomm_relayd_index index_info
;
1903 struct relay_index
*index
, *wr_index
= NULL
;
1904 struct lttcomm_relayd_generic_reply reply
;
1905 struct relay_stream
*stream
;
1906 uint64_t net_seq_num
;
1910 DBG("Relay receiving index");
1912 if (!session
|| conn
->version_check_done
== 0) {
1913 ERR("Trying to close a stream before version check");
1915 goto end_no_session
;
1918 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
1919 sizeof(index_info
), 0);
1920 if (ret
< sizeof(index_info
)) {
1922 /* Orderly shutdown. Not necessary to print an error. */
1923 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1925 ERR("Relay didn't receive valid index struct size : %d", ret
);
1928 goto end_no_session
;
1931 net_seq_num
= be64toh(index_info
.net_seq_num
);
1934 stream
= stream_find_by_id(relay_streams_ht
,
1935 be64toh(index_info
.relay_stream_id
));
1938 goto end_rcu_unlock
;
1941 /* Live beacon handling */
1942 if (index_info
.packet_size
== 0) {
1943 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1946 * Only flag a stream inactive when it has already received data
1947 * and no indexes are in flight.
1949 if (stream
->total_index_received
> 0 && stream
->indexes_in_flight
== 0) {
1950 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1953 goto end_rcu_unlock
;
1955 stream
->beacon_ts_end
= -1ULL;
1958 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1960 /* A successful creation will add the object to the HT. */
1961 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1963 goto end_rcu_unlock
;
1966 stream
->indexes_in_flight
++;
1969 copy_index_control_data(index
, &index_info
);
1970 if (stream
->ctf_stream_id
== -1ULL) {
1971 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
1974 if (index_created
) {
1976 * Try to add the relay index object to the hash table. If an object
1977 * already exist, destroy back the index created, set the data in this
1978 * object and write it on disk.
1980 relay_index_add(index
, &wr_index
);
1982 copy_index_control_data(wr_index
, &index_info
);
1986 /* The index already exists so write it on disk. */
1990 /* Do we have a writable ready index to write on disk. */
1992 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1994 goto end_rcu_unlock
;
1996 stream
->total_index_received
++;
1997 stream
->indexes_in_flight
--;
1998 assert(stream
->indexes_in_flight
>= 0);
2004 memset(&reply
, 0, sizeof(reply
));
2006 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2008 reply
.ret_code
= htobe32(LTTNG_OK
);
2010 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2012 ERR("Relay sending close index id reply");
2021 * Receive the streams_sent message.
2023 * Return 0 on success else a negative value.
2026 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2027 struct relay_connection
*conn
)
2030 struct lttcomm_relayd_generic_reply reply
;
2034 DBG("Relay receiving streams_sent");
2036 if (!conn
->session
|| conn
->version_check_done
== 0) {
2037 ERR("Trying to close a stream before version check");
2039 goto end_no_session
;
2043 * Flag every pending stream in the connection recv list that they are
2044 * ready to be used by the viewer.
2046 set_viewer_ready_flag(conn
);
2049 * Inform the viewer that there are new streams in the session.
2051 if (conn
->session
->viewer_refcount
) {
2052 uatomic_set(&conn
->session
->new_streams
, 1);
2055 memset(&reply
, 0, sizeof(reply
));
2056 reply
.ret_code
= htobe32(LTTNG_OK
);
2057 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2059 ERR("Relay sending sent_stream reply");
2071 * Process the commands received on the control socket
2074 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2075 struct relay_connection
*conn
)
2079 switch (be32toh(recv_hdr
->cmd
)) {
2080 case RELAYD_CREATE_SESSION
:
2081 ret
= relay_create_session(recv_hdr
, conn
);
2083 case RELAYD_ADD_STREAM
:
2084 ret
= relay_add_stream(recv_hdr
, conn
);
2086 case RELAYD_START_DATA
:
2087 ret
= relay_start(recv_hdr
, conn
);
2089 case RELAYD_SEND_METADATA
:
2090 ret
= relay_recv_metadata(recv_hdr
, conn
);
2092 case RELAYD_VERSION
:
2093 ret
= relay_send_version(recv_hdr
, conn
);
2095 case RELAYD_CLOSE_STREAM
:
2096 ret
= relay_close_stream(recv_hdr
, conn
);
2098 case RELAYD_DATA_PENDING
:
2099 ret
= relay_data_pending(recv_hdr
, conn
);
2101 case RELAYD_QUIESCENT_CONTROL
:
2102 ret
= relay_quiescent_control(recv_hdr
, conn
);
2104 case RELAYD_BEGIN_DATA_PENDING
:
2105 ret
= relay_begin_data_pending(recv_hdr
, conn
);
2107 case RELAYD_END_DATA_PENDING
:
2108 ret
= relay_end_data_pending(recv_hdr
, conn
);
2110 case RELAYD_SEND_INDEX
:
2111 ret
= relay_recv_index(recv_hdr
, conn
);
2113 case RELAYD_STREAMS_SENT
:
2114 ret
= relay_streams_sent(recv_hdr
, conn
);
2116 case RELAYD_UPDATE_SYNC_INFO
:
2118 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2119 relay_unknown_command(conn
);
2129 * Handle index for a data stream.
2131 * RCU read side lock MUST be acquired.
2133 * Return 0 on success else a negative value.
2135 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2138 int ret
= 0, index_created
= 0;
2139 uint64_t stream_id
, data_offset
;
2140 struct relay_index
*index
, *wr_index
= NULL
;
2144 stream_id
= stream
->stream_handle
;
2145 /* Get data offset because we are about to update the index. */
2146 data_offset
= htobe64(stream
->tracefile_size_current
);
2149 * Lookup for an existing index for that stream id/sequence number. If on
2150 * exists, the control thread already received the data for it thus we need
2151 * to write it on disk.
2153 index
= relay_index_find(stream_id
, net_seq_num
);
2155 /* A successful creation will add the object to the HT. */
2156 index
= relay_index_create(stream_id
, net_seq_num
);
2162 stream
->indexes_in_flight
++;
2165 if (rotate_index
|| stream
->index_fd
< 0) {
2166 index
->to_close_fd
= stream
->index_fd
;
2167 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2168 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2169 stream
->tracefile_count_current
);
2171 /* This will close the stream's index fd if one. */
2172 relay_index_free_safe(index
);
2175 stream
->index_fd
= ret
;
2177 index
->fd
= stream
->index_fd
;
2178 index
->index_data
.offset
= data_offset
;
2180 if (index_created
) {
2182 * Try to add the relay index object to the hash table. If an object
2183 * already exist, destroy back the index created and set the data.
2185 relay_index_add(index
, &wr_index
);
2187 /* Copy back data from the created index. */
2188 wr_index
->fd
= index
->fd
;
2189 wr_index
->to_close_fd
= index
->to_close_fd
;
2190 wr_index
->index_data
.offset
= data_offset
;
2194 /* The index already exists so write it on disk. */
2198 /* Do we have a writable ready index to write on disk. */
2200 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2204 stream
->total_index_received
++;
2205 stream
->indexes_in_flight
--;
2206 assert(stream
->indexes_in_flight
>= 0);
2214 * relay_process_data: Process the data received on the data socket
2217 int relay_process_data(struct relay_connection
*conn
)
2219 int ret
= 0, rotate_index
= 0;
2221 struct relay_stream
*stream
;
2222 struct lttcomm_relayd_data_hdr data_hdr
;
2224 uint64_t net_seq_num
;
2226 struct relay_session
*session
;
2230 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2231 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2234 /* Orderly shutdown. Not necessary to print an error. */
2235 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2237 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2243 stream_id
= be64toh(data_hdr
.stream_id
);
2246 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
2249 goto end_rcu_unlock
;
2252 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
2255 data_size
= be32toh(data_hdr
.data_size
);
2256 if (data_buffer_size
< data_size
) {
2259 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2260 if (!tmp_data_ptr
) {
2261 ERR("Allocating data buffer");
2264 goto end_rcu_unlock
;
2266 data_buffer
= tmp_data_ptr
;
2267 data_buffer_size
= data_size
;
2269 memset(data_buffer
, 0, data_size
);
2271 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2273 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2274 data_size
, stream_id
, net_seq_num
);
2275 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
2278 /* Orderly shutdown. Not necessary to print an error. */
2279 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2282 goto end_rcu_unlock
;
2285 /* Check if a rotation is needed. */
2286 if (stream
->tracefile_size
> 0 &&
2287 (stream
->tracefile_size_current
+ data_size
) >
2288 stream
->tracefile_size
) {
2289 struct relay_viewer_stream
*vstream
;
2292 new_id
= (stream
->tracefile_count_current
+ 1) %
2293 stream
->tracefile_count
;
2295 * When we wrap-around back to 0, we start overwriting old
2298 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2299 stream
->tracefile_overwrite
= 1;
2301 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2302 if (stream
->tracefile_overwrite
) {
2303 stream
->oldest_tracefile_id
=
2304 (stream
->oldest_tracefile_id
+ 1) %
2305 stream
->tracefile_count
;
2307 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2310 * The viewer is reading a file about to be
2311 * overwritten. Close the FDs it is
2312 * currently using and let it handle the fault.
2314 if (vstream
->tracefile_count_current
== new_id
) {
2315 pthread_mutex_lock(&vstream
->overwrite_lock
);
2316 vstream
->abort_flag
= 1;
2317 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2318 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2319 stream
->channel_name
, new_id
);
2320 } else if (vstream
->tracefile_count_current
==
2321 stream
->tracefile_count_current
) {
2323 * The reader and writer were in the
2324 * same trace file, inform the viewer
2325 * that no new index will ever be added
2328 vstream
->close_write_flag
= 1;
2331 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2332 stream
->tracefile_size
, stream
->tracefile_count
,
2333 relayd_uid
, relayd_gid
, stream
->fd
,
2334 &(stream
->tracefile_count_current
), &stream
->fd
);
2335 stream
->total_index_received
= 0;
2336 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2338 ERR("Rotating stream output file");
2339 goto end_rcu_unlock
;
2341 /* Reset current size because we just perform a stream rotation. */
2342 stream
->tracefile_size_current
= 0;
2347 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2348 * index are NOT supported.
2350 if (session
->minor
>= 4 && !session
->snapshot
) {
2351 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2353 goto end_rcu_unlock
;
2357 /* Write data to stream output fd. */
2358 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2359 if (size_ret
< data_size
) {
2360 ERR("Relay error writing data to file");
2362 goto end_rcu_unlock
;
2365 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2366 ret
, stream
->stream_handle
);
2368 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2370 goto end_rcu_unlock
;
2372 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2374 stream
->prev_seq
= net_seq_num
;
2376 try_close_stream(session
, stream
);
2385 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2391 (void) lttng_poll_del(events
, pollfd
);
2393 ret
= close(pollfd
);
2395 ERR("Closing pollfd %d", pollfd
);
2399 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
2400 struct relay_connection
*conn
)
2402 assert(relay_connections_ht
);
2405 connection_delete(relay_connections_ht
, conn
);
2407 /* For the control socket, we try to destroy the session. */
2408 if (conn
->type
== RELAY_CONTROL
&& conn
->session
) {
2409 destroy_session(conn
->session
, conn
->sessions_ht
);
2412 connection_destroy(conn
);
2416 * This thread does the actual work
2419 void *relay_thread_worker(void *data
)
2421 int ret
, err
= -1, last_seen_data_fd
= -1;
2423 struct relay_connection
*conn
;
2424 struct lttng_poll_event events
;
2425 struct lttng_ht
*relay_connections_ht
;
2426 struct lttng_ht_iter iter
;
2427 struct lttcomm_relayd_hdr recv_hdr
;
2428 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2429 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2431 DBG("[thread] Relay worker started");
2433 rcu_register_thread();
2435 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2437 if (testpoint(relayd_thread_worker
)) {
2438 goto error_testpoint
;
2441 health_code_update();
2443 /* table of connections indexed on socket */
2444 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2445 if (!relay_connections_ht
) {
2446 goto relay_connections_ht_error
;
2449 /* Tables of received indexes indexed by index handle and net_seq_num. */
2450 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2452 goto indexes_ht_error
;
2455 ret
= create_thread_poll_set(&events
, 2);
2457 goto error_poll_create
;
2460 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2467 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2469 health_code_update();
2471 /* Infinite blocking call, waiting for transmission */
2472 DBG3("Relayd worker thread polling...");
2473 health_poll_entry();
2474 ret
= lttng_poll_wait(&events
, -1);
2478 * Restart interrupted system call.
2480 if (errno
== EINTR
) {
2489 * Process control. The control connection is prioritised so we don't
2490 * starve it with high throughout put tracing data on the data
2493 for (i
= 0; i
< nb_fd
; i
++) {
2494 /* Fetch once the poll data */
2495 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2496 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2498 health_code_update();
2500 /* Thread quit pipe has been closed. Killing thread. */
2501 ret
= check_thread_quit_pipe(pollfd
, revents
);
2507 /* Inspect the relay conn pipe for new connection */
2508 if (pollfd
== relay_conn_pipe
[0]) {
2509 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2510 ERR("Relay connection pipe error");
2512 } else if (revents
& LPOLLIN
) {
2513 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2517 conn
->sessions_ht
= sessions_ht
;
2518 connection_init(conn
);
2519 lttng_poll_add(&events
, conn
->sock
->fd
,
2520 LPOLLIN
| LPOLLRDHUP
);
2522 lttng_ht_add_unique_ulong(relay_connections_ht
,
2525 DBG("Connection socket %d added", conn
->sock
->fd
);
2529 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2530 /* If not found, there is a synchronization issue. */
2533 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2534 cleanup_connection_pollfd(&events
, pollfd
);
2535 destroy_connection(relay_connections_ht
, conn
);
2536 if (last_seen_data_fd
== pollfd
) {
2537 last_seen_data_fd
= last_notdel_data_fd
;
2539 } else if (revents
& LPOLLIN
) {
2540 if (conn
->type
== RELAY_CONTROL
) {
2541 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2542 sizeof(recv_hdr
), 0);
2544 /* Connection closed */
2545 cleanup_connection_pollfd(&events
, pollfd
);
2546 destroy_connection(relay_connections_ht
, conn
);
2547 DBG("Control connection closed with %d", pollfd
);
2549 ret
= relay_process_control(&recv_hdr
, conn
);
2551 /* Clear the session on error. */
2552 cleanup_connection_pollfd(&events
, pollfd
);
2553 destroy_connection(relay_connections_ht
, conn
);
2554 DBG("Connection closed with %d", pollfd
);
2560 * Flag the last seen data fd not deleted. It will be
2561 * used as the last seen fd if any fd gets deleted in
2564 last_notdel_data_fd
= pollfd
;
2567 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
2574 * The last loop handled a control request, go back to poll to make
2575 * sure we prioritise the control socket.
2581 if (last_seen_data_fd
>= 0) {
2582 for (i
= 0; i
< nb_fd
; i
++) {
2583 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2585 health_code_update();
2587 if (last_seen_data_fd
== pollfd
) {
2594 /* Process data connection. */
2595 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2596 /* Fetch the poll data. */
2597 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2598 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2600 health_code_update();
2602 /* Skip the command pipe. It's handled in the first loop. */
2603 if (pollfd
== relay_conn_pipe
[0]) {
2609 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2611 /* Skip it. Might be removed before. */
2616 if (revents
& LPOLLIN
) {
2617 if (conn
->type
!= RELAY_DATA
) {
2621 ret
= relay_process_data(conn
);
2622 /* Connection closed */
2624 cleanup_connection_pollfd(&events
, pollfd
);
2625 destroy_connection(relay_connections_ht
, conn
);
2626 DBG("Data connection closed with %d", pollfd
);
2628 * Every goto restart call sets the last seen fd where
2629 * here we don't really care since we gracefully
2630 * continue the loop after the connection is deleted.
2633 /* Keep last seen port. */
2634 last_seen_data_fd
= pollfd
;
2642 last_seen_data_fd
= -1;
2645 /* Normal exit, no error */
2650 lttng_poll_clean(&events
);
2652 /* Cleanup reamaining connection object. */
2654 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2656 health_code_update();
2657 destroy_connection(relay_connections_ht
, conn
);
2661 lttng_ht_destroy(indexes_ht
);
2663 lttng_ht_destroy(relay_connections_ht
);
2664 relay_connections_ht_error
:
2665 /* Close relay conn pipes */
2666 utils_close_pipe(relay_conn_pipe
);
2668 DBG("Thread exited with error");
2670 DBG("Worker thread cleanup complete");
2675 ERR("Health error occurred in %s", __func__
);
2677 health_unregister(health_relayd
);
2678 rcu_unregister_thread();
2684 * Create the relay command pipe to wake thread_manage_apps.
2685 * Closed in cleanup().
2687 static int create_relay_conn_pipe(void)
2691 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2699 int main(int argc
, char **argv
)
2703 struct relay_local_data
*relay_ctx
;
2705 /* Parse arguments */
2707 if ((ret
= set_options(argc
, argv
)) < 0) {
2711 if ((ret
= set_signal_handler()) < 0) {
2715 /* Try to create directory if -o, --output is specified. */
2716 if (opt_output_path
) {
2717 if (*opt_output_path
!= '/') {
2718 ERR("Please specify an absolute path for -o, --output PATH");
2722 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2724 ERR("Unable to create %s", opt_output_path
);
2730 if (opt_daemon
|| opt_background
) {
2733 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2740 * We are in the child. Make sure all other file
2741 * descriptors are closed, in case we are called with
2742 * more opened file descriptors than the standard ones.
2744 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2749 /* Create thread quit pipe */
2750 if ((ret
= init_thread_quit_pipe()) < 0) {
2754 /* We need those values for the file/dir creation. */
2755 relayd_uid
= getuid();
2756 relayd_gid
= getgid();
2758 /* Check if daemon is UID = 0 */
2759 if (relayd_uid
== 0) {
2760 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 || live_uri
->port
< 1024) {
2761 ERR("Need to be root to use ports < 1024");
2767 /* Setup the thread apps communication pipe. */
2768 if ((ret
= create_relay_conn_pipe()) < 0) {
2772 /* Init relay command queue. */
2773 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
2775 /* Set up max poll set size */
2776 lttng_poll_set_max_size();
2778 /* Initialize communication library */
2780 lttcomm_inet_init();
2782 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2784 PERROR("relay_ctx");
2788 /* tables of sessions indexed by session ID */
2789 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2790 if (!relay_ctx
->sessions_ht
) {
2791 goto exit_relay_ctx_sessions
;
2794 /* tables of streams indexed by stream ID */
2795 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2796 if (!relay_streams_ht
) {
2797 goto exit_relay_ctx_streams
;
2800 /* tables of streams indexed by stream ID */
2801 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2802 if (!viewer_streams_ht
) {
2803 goto exit_relay_ctx_viewer_streams
;
2806 /* Initialize thread health monitoring */
2807 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2808 if (!health_relayd
) {
2809 PERROR("health_app_create error");
2810 goto exit_health_app_create
;
2813 ret
= utils_create_pipe(health_quit_pipe
);
2815 goto error_health_pipe
;
2818 /* Create thread to manage the client socket */
2819 ret
= pthread_create(&health_thread
, NULL
,
2820 thread_manage_health
, (void *) NULL
);
2822 PERROR("pthread_create health");
2826 /* Setup the dispatcher thread */
2827 ret
= pthread_create(&dispatcher_thread
, NULL
,
2828 relay_thread_dispatcher
, (void *) NULL
);
2830 PERROR("pthread_create dispatcher");
2831 goto exit_dispatcher
;
2834 /* Setup the worker thread */
2835 ret
= pthread_create(&worker_thread
, NULL
,
2836 relay_thread_worker
, (void *) relay_ctx
);
2838 PERROR("pthread_create worker");
2842 /* Setup the listener thread */
2843 ret
= pthread_create(&listener_thread
, NULL
,
2844 relay_thread_listener
, (void *) NULL
);
2846 PERROR("pthread_create listener");
2850 ret
= live_start_threads(live_uri
, relay_ctx
);
2852 ERR("Starting live viewer threads");
2857 ret
= pthread_join(listener_thread
, &status
);
2859 PERROR("pthread_join");
2860 goto error
; /* join error, exit without cleanup */
2864 ret
= pthread_join(worker_thread
, &status
);
2866 PERROR("pthread_join");
2867 goto error
; /* join error, exit without cleanup */
2871 ret
= pthread_join(dispatcher_thread
, &status
);
2873 PERROR("pthread_join");
2874 goto error
; /* join error, exit without cleanup */
2878 ret
= pthread_join(health_thread
, &status
);
2880 PERROR("pthread_join health thread");
2881 goto error
; /* join error, exit without cleanup */
2885 * Stop live threads only after joining other threads.
2887 live_stop_threads();
2890 utils_close_pipe(health_quit_pipe
);
2893 health_app_destroy(health_relayd
);
2895 exit_health_app_create
:
2896 lttng_ht_destroy(viewer_streams_ht
);
2898 exit_relay_ctx_viewer_streams
:
2899 lttng_ht_destroy(relay_streams_ht
);
2901 exit_relay_ctx_streams
:
2902 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2904 exit_relay_ctx_sessions
: