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>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #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/compat/getenv.h>
49 #include <common/defaults.h>
50 #include <common/daemonize.h>
51 #include <common/futex.h>
52 #include <common/sessiond-comm/sessiond-comm.h>
53 #include <common/sessiond-comm/inet.h>
54 #include <common/sessiond-comm/relayd.h>
55 #include <common/uri.h>
56 #include <common/utils.h>
57 #include <common/config/session-config.h>
58 #include <urcu/rculist.h>
61 #include "ctf-trace.h"
64 #include "lttng-relayd.h"
66 #include "health-relayd.h"
67 #include "testpoint.h"
68 #include "viewer-stream.h"
71 #include "connection.h"
72 #include "tracefile-array.h"
74 /* command line options */
75 char *opt_output_path
;
76 static int opt_daemon
, opt_background
;
79 * We need to wait for listener and live listener threads, as well as
80 * health check thread, before being ready to signal readiness.
82 #define NR_LTTNG_RELAY_READY 3
83 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
85 /* Size of receive buffer. */
86 #define RECV_DATA_BUFFER_SIZE 65536
88 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
89 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
91 static struct lttng_uri
*control_uri
;
92 static struct lttng_uri
*data_uri
;
93 static struct lttng_uri
*live_uri
;
97 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
98 static int tracing_group_name_override
;
100 const char * const config_section_name
= "relayd";
103 * Quit pipe for all threads. This permits a single cancellation point
104 * for all threads when receiving an event on the pipe.
106 int thread_quit_pipe
[2] = { -1, -1 };
109 * This pipe is used to inform the worker thread that a command is queued and
110 * ready to be processed.
112 static int relay_conn_pipe
[2] = { -1, -1 };
114 /* Shared between threads */
115 static int dispatch_thread_exit
;
117 static pthread_t listener_thread
;
118 static pthread_t dispatcher_thread
;
119 static pthread_t worker_thread
;
120 static pthread_t health_thread
;
123 * last_relay_stream_id_lock protects last_relay_stream_id increment
124 * atomicity on 32-bit architectures.
126 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
127 static uint64_t last_relay_stream_id
;
130 * Relay command queue.
132 * The relay_thread_listener and relay_thread_dispatcher communicate with this
135 static struct relay_conn_queue relay_conn_queue
;
137 /* buffer allocated at startup, used to store the trace data */
138 static char *data_buffer
;
139 static unsigned int data_buffer_size
;
141 /* Global relay stream hash table. */
142 struct lttng_ht
*relay_streams_ht
;
144 /* Global relay viewer stream hash table. */
145 struct lttng_ht
*viewer_streams_ht
;
147 /* Global relay sessions hash table. */
148 struct lttng_ht
*sessions_ht
;
150 /* Relayd health monitoring */
151 struct health_app
*health_relayd
;
153 static struct option long_options
[] = {
154 { "control-port", 1, 0, 'C', },
155 { "data-port", 1, 0, 'D', },
156 { "live-port", 1, 0, 'L', },
157 { "daemonize", 0, 0, 'd', },
158 { "background", 0, 0, 'b', },
159 { "group", 1, 0, 'g', },
160 { "help", 0, 0, 'h', },
161 { "output", 1, 0, 'o', },
162 { "verbose", 0, 0, 'v', },
163 { "config", 1, 0, 'f' },
167 static const char *config_ignore_options
[] = { "help", "config" };
170 * Take an option from the getopt output and set it in the right variable to be
173 * Return 0 on success else a negative value.
175 static int set_option(int opt
, const char *arg
, const char *optname
)
181 fprintf(stderr
, "option %s", optname
);
183 fprintf(stderr
, " with arg %s\n", arg
);
187 if (lttng_is_setuid_setgid()) {
188 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
189 "-C, --control-port");
191 ret
= uri_parse(arg
, &control_uri
);
193 ERR("Invalid control URI specified");
196 if (control_uri
->port
== 0) {
197 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
202 if (lttng_is_setuid_setgid()) {
203 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
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
;
217 if (lttng_is_setuid_setgid()) {
218 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
221 ret
= uri_parse(arg
, &live_uri
);
223 ERR("Invalid live URI specified");
226 if (live_uri
->port
== 0) {
227 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
238 if (lttng_is_setuid_setgid()) {
239 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
242 tracing_group_name
= strdup(arg
);
243 if (tracing_group_name
== NULL
) {
248 tracing_group_name_override
= 1;
252 ret
= utils_show_man_page(8, "lttng-relayd");
254 ERR("Cannot view man page lttng-relayd(8)");
259 if (lttng_is_setuid_setgid()) {
260 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
263 ret
= asprintf(&opt_output_path
, "%s", arg
);
266 PERROR("asprintf opt_output_path");
272 /* Verbose level can increase using multiple -v */
274 lttng_opt_verbose
= config_parse_value(arg
);
276 /* Only 3 level of verbosity (-vvv). */
277 if (lttng_opt_verbose
< 3) {
278 lttng_opt_verbose
+= 1;
283 /* Unknown option or other error.
284 * Error is printed by getopt, just return */
297 * config_entry_handler_cb used to handle options read from a config file.
298 * See config_entry_handler_cb comment in common/config/session-config.h for the
299 * return value conventions.
301 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
305 if (!entry
|| !entry
->name
|| !entry
->value
) {
310 /* Check if the option is to be ignored */
311 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
312 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
317 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
318 /* Ignore if entry name is not fully matched. */
319 if (strcmp(entry
->name
, long_options
[i
].name
)) {
324 * If the option takes no argument on the command line,
325 * we have to check if the value is "true". We support
326 * non-zero numeric values, true, on and yes.
328 if (!long_options
[i
].has_arg
) {
329 ret
= config_parse_value(entry
->value
);
332 WARN("Invalid configuration value \"%s\" for option %s",
333 entry
->value
, entry
->name
);
335 /* False, skip boolean config option. */
340 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
344 WARN("Unrecognized option \"%s\" in daemon configuration file.",
351 static int set_options(int argc
, char **argv
)
353 int c
, ret
= 0, option_index
= 0, retval
= 0;
354 int orig_optopt
= optopt
, orig_optind
= optind
;
355 char *default_address
, *optstring
;
356 const char *config_path
= NULL
;
358 optstring
= utils_generate_optstring(long_options
,
359 sizeof(long_options
) / sizeof(struct option
));
365 /* Check for the --config option */
367 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
368 &option_index
)) != -1) {
372 } else if (c
!= 'f') {
376 if (lttng_is_setuid_setgid()) {
377 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
380 config_path
= utils_expand_path(optarg
);
382 ERR("Failed to resolve path: %s", optarg
);
387 ret
= config_get_section_entries(config_path
, config_section_name
,
388 config_entry_handler
, NULL
);
391 ERR("Invalid configuration option at line %i", ret
);
397 /* Reset getopt's global state */
398 optopt
= orig_optopt
;
399 optind
= orig_optind
;
401 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
406 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
413 /* assign default values */
414 if (control_uri
== NULL
) {
415 ret
= asprintf(&default_address
,
416 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
417 DEFAULT_NETWORK_CONTROL_PORT
);
419 PERROR("asprintf default data address");
424 ret
= uri_parse(default_address
, &control_uri
);
425 free(default_address
);
427 ERR("Invalid control URI specified");
432 if (data_uri
== NULL
) {
433 ret
= asprintf(&default_address
,
434 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
435 DEFAULT_NETWORK_DATA_PORT
);
437 PERROR("asprintf default data address");
442 ret
= uri_parse(default_address
, &data_uri
);
443 free(default_address
);
445 ERR("Invalid data URI specified");
450 if (live_uri
== NULL
) {
451 ret
= asprintf(&default_address
,
452 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
453 DEFAULT_NETWORK_VIEWER_PORT
);
455 PERROR("asprintf default viewer control address");
460 ret
= uri_parse(default_address
, &live_uri
);
461 free(default_address
);
463 ERR("Invalid viewer control URI specified");
474 static void print_global_objects(void)
476 rcu_register_thread();
478 print_viewer_streams();
479 print_relay_streams();
482 rcu_unregister_thread();
488 static void relayd_cleanup(void)
490 print_global_objects();
494 if (viewer_streams_ht
)
495 lttng_ht_destroy(viewer_streams_ht
);
496 if (relay_streams_ht
)
497 lttng_ht_destroy(relay_streams_ht
);
499 lttng_ht_destroy(sessions_ht
);
501 /* free the dynamically allocated opt_output_path */
502 free(opt_output_path
);
504 /* Close thread quit pipes */
505 utils_close_pipe(thread_quit_pipe
);
507 uri_free(control_uri
);
509 /* Live URI is freed in the live thread. */
511 if (tracing_group_name_override
) {
512 free((void *) tracing_group_name
);
517 * Write to writable pipe used to notify a thread.
519 static int notify_thread_pipe(int wpipe
)
523 ret
= lttng_write(wpipe
, "!", 1);
525 PERROR("write poll pipe");
533 static int notify_health_quit_pipe(int *pipe
)
537 ret
= lttng_write(pipe
[1], "4", 1);
539 PERROR("write relay health quit");
548 * Stop all relayd and relayd-live threads.
550 int lttng_relay_stop_threads(void)
554 /* Stopping all threads */
555 DBG("Terminating all threads");
556 if (notify_thread_pipe(thread_quit_pipe
[1])) {
557 ERR("write error on thread quit pipe");
561 if (notify_health_quit_pipe(health_quit_pipe
)) {
562 ERR("write error on health quit pipe");
565 /* Dispatch thread */
566 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
567 futex_nto1_wake(&relay_conn_queue
.futex
);
569 if (relayd_live_stop()) {
570 ERR("Error stopping live threads");
577 * Signal handler for the daemon
579 * Simply stop all worker threads, leaving main() return gracefully after
580 * joining all threads and calling cleanup().
582 static void sighandler(int sig
)
586 DBG("SIGINT caught");
587 if (lttng_relay_stop_threads()) {
588 ERR("Error stopping threads");
592 DBG("SIGTERM caught");
593 if (lttng_relay_stop_threads()) {
594 ERR("Error stopping threads");
598 CMM_STORE_SHARED(recv_child_signal
, 1);
606 * Setup signal handler for :
607 * SIGINT, SIGTERM, SIGPIPE
609 static int set_signal_handler(void)
615 if ((ret
= sigemptyset(&sigset
)) < 0) {
616 PERROR("sigemptyset");
623 sa
.sa_handler
= sighandler
;
624 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
629 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
634 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
639 sa
.sa_handler
= SIG_IGN
;
640 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
645 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
650 void lttng_relay_notify_ready(void)
652 /* Notify the parent of the fork() process that we are ready. */
653 if (opt_daemon
|| opt_background
) {
654 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
655 kill(child_ppid
, SIGUSR1
);
661 * Init thread quit pipe.
663 * Return -1 on error or 0 if all pipes are created.
665 static int init_thread_quit_pipe(void)
669 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
675 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
677 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
681 if (events
== NULL
|| size
== 0) {
686 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
692 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
704 * Check if the thread quit pipe was triggered.
706 * Return 1 if it was triggered else 0;
708 static int check_thread_quit_pipe(int fd
, uint32_t events
)
710 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
718 * Create and init socket from uri.
720 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
723 struct lttcomm_sock
*sock
= NULL
;
725 sock
= lttcomm_alloc_sock_from_uri(uri
);
727 ERR("Allocating socket");
731 ret
= lttcomm_create_sock(sock
);
735 DBG("Listening on sock %d", sock
->fd
);
737 ret
= sock
->ops
->bind(sock
);
742 ret
= sock
->ops
->listen(sock
, -1);
752 lttcomm_destroy_sock(sock
);
758 * This thread manages the listening for new connections on the network
760 static void *relay_thread_listener(void *data
)
762 int i
, ret
, pollfd
, err
= -1;
763 uint32_t revents
, nb_fd
;
764 struct lttng_poll_event events
;
765 struct lttcomm_sock
*control_sock
, *data_sock
;
767 DBG("[thread] Relay listener started");
769 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
771 health_code_update();
773 control_sock
= relay_socket_create(control_uri
);
775 goto error_sock_control
;
778 data_sock
= relay_socket_create(data_uri
);
780 goto error_sock_relay
;
784 * Pass 3 as size here for the thread quit pipe, control and
787 ret
= create_thread_poll_set(&events
, 3);
789 goto error_create_poll
;
792 /* Add the control socket */
793 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
798 /* Add the data socket */
799 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
804 lttng_relay_notify_ready();
806 if (testpoint(relayd_thread_listener
)) {
807 goto error_testpoint
;
811 health_code_update();
813 DBG("Listener accepting connections");
817 ret
= lttng_poll_wait(&events
, -1);
821 * Restart interrupted system call.
823 if (errno
== EINTR
) {
831 DBG("Relay new connection received");
832 for (i
= 0; i
< nb_fd
; i
++) {
833 health_code_update();
835 /* Fetch once the poll data */
836 revents
= LTTNG_POLL_GETEV(&events
, i
);
837 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
841 * No activity for this FD (poll
847 /* Thread quit pipe has been closed. Killing thread. */
848 ret
= check_thread_quit_pipe(pollfd
, revents
);
854 if (revents
& LPOLLIN
) {
856 * A new connection is requested, therefore a
857 * sessiond/consumerd connection is allocated in
858 * this thread, enqueued to a global queue and
859 * dequeued (and freed) in the worker thread.
862 struct relay_connection
*new_conn
;
863 struct lttcomm_sock
*newsock
;
864 enum connection_type type
;
866 if (pollfd
== data_sock
->fd
) {
868 newsock
= data_sock
->ops
->accept(data_sock
);
869 DBG("Relay data connection accepted, socket %d",
872 assert(pollfd
== control_sock
->fd
);
873 type
= RELAY_CONTROL
;
874 newsock
= control_sock
->ops
->accept(control_sock
);
875 DBG("Relay control connection accepted, socket %d",
879 PERROR("accepting sock");
883 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
886 PERROR("setsockopt inet");
887 lttcomm_destroy_sock(newsock
);
890 new_conn
= connection_create(newsock
, type
);
892 lttcomm_destroy_sock(newsock
);
896 /* Enqueue request for the dispatcher thread. */
897 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
901 * Wake the dispatch queue futex.
902 * Implicit memory barrier with the
903 * exchange in cds_wfcq_enqueue.
905 futex_nto1_wake(&relay_conn_queue
.futex
);
906 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
907 ERR("socket poll error");
910 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
920 lttng_poll_clean(&events
);
922 if (data_sock
->fd
>= 0) {
923 ret
= data_sock
->ops
->close(data_sock
);
928 lttcomm_destroy_sock(data_sock
);
930 if (control_sock
->fd
>= 0) {
931 ret
= control_sock
->ops
->close(control_sock
);
936 lttcomm_destroy_sock(control_sock
);
940 ERR("Health error occurred in %s", __func__
);
942 health_unregister(health_relayd
);
943 DBG("Relay listener thread cleanup complete");
944 lttng_relay_stop_threads();
949 * This thread manages the dispatching of the requests to worker threads
951 static void *relay_thread_dispatcher(void *data
)
955 struct cds_wfcq_node
*node
;
956 struct relay_connection
*new_conn
= NULL
;
958 DBG("[thread] Relay dispatcher started");
960 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
962 if (testpoint(relayd_thread_dispatcher
)) {
963 goto error_testpoint
;
966 health_code_update();
968 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
969 health_code_update();
971 /* Atomically prepare the queue futex */
972 futex_nto1_prepare(&relay_conn_queue
.futex
);
975 health_code_update();
977 /* Dequeue commands */
978 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
979 &relay_conn_queue
.tail
);
981 DBG("Woken up but nothing in the relay command queue");
982 /* Continue thread execution */
985 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
987 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
990 * Inform worker thread of the new request. This
991 * call is blocking so we can be assured that
992 * the data will be read at some point in time
993 * or wait to the end of the world :)
995 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
997 PERROR("write connection pipe");
998 connection_put(new_conn
);
1001 } while (node
!= NULL
);
1003 /* Futex wait on queue. Blocking call on futex() */
1004 health_poll_entry();
1005 futex_nto1_wait(&relay_conn_queue
.futex
);
1009 /* Normal exit, no error */
1016 ERR("Health error occurred in %s", __func__
);
1018 health_unregister(health_relayd
);
1019 DBG("Dispatch thread dying");
1020 lttng_relay_stop_threads();
1025 * Set index data from the control port to a given index object.
1027 static int set_index_control_data(struct relay_index
*index
,
1028 struct lttcomm_relayd_index
*data
,
1029 struct relay_connection
*conn
)
1031 struct ctf_packet_index index_data
;
1034 * The index on disk is encoded in big endian, so we don't need
1035 * to convert the data received on the network. The data_offset
1036 * value is NEVER modified here and is updated by the data
1039 index_data
.packet_size
= data
->packet_size
;
1040 index_data
.content_size
= data
->content_size
;
1041 index_data
.timestamp_begin
= data
->timestamp_begin
;
1042 index_data
.timestamp_end
= data
->timestamp_end
;
1043 index_data
.events_discarded
= data
->events_discarded
;
1044 index_data
.stream_id
= data
->stream_id
;
1046 if (conn
->minor
>= 8) {
1047 index
->index_data
.stream_instance_id
= data
->stream_instance_id
;
1048 index
->index_data
.packet_seq_num
= data
->packet_seq_num
;
1051 return relay_index_set_data(index
, &index_data
);
1055 * Handle the RELAYD_CREATE_SESSION command.
1057 * On success, send back the session id or else return a negative value.
1059 static int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1060 struct relay_connection
*conn
)
1062 int ret
= 0, send_ret
;
1063 struct relay_session
*session
;
1064 struct lttcomm_relayd_status_session reply
;
1065 char session_name
[LTTNG_NAME_MAX
];
1066 char hostname
[LTTNG_HOST_NAME_MAX
];
1067 uint32_t live_timer
= 0;
1068 bool snapshot
= false;
1070 memset(session_name
, 0, LTTNG_NAME_MAX
);
1071 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1073 memset(&reply
, 0, sizeof(reply
));
1075 switch (conn
->minor
) {
1080 case 4: /* LTTng sessiond 2.4 */
1082 ret
= cmd_create_session_2_4(conn
, session_name
,
1083 hostname
, &live_timer
, &snapshot
);
1089 session
= session_create(session_name
, hostname
, live_timer
,
1090 snapshot
, conn
->major
, conn
->minor
);
1095 assert(!conn
->session
);
1096 conn
->session
= session
;
1097 DBG("Created session %" PRIu64
, session
->id
);
1099 reply
.session_id
= htobe64(session
->id
);
1103 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1105 reply
.ret_code
= htobe32(LTTNG_OK
);
1108 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1110 ERR("Relayd sending session id");
1118 * When we have received all the streams and the metadata for a channel,
1119 * we make them visible to the viewer threads.
1121 static void publish_connection_local_streams(struct relay_connection
*conn
)
1123 struct relay_stream
*stream
;
1124 struct relay_session
*session
= conn
->session
;
1127 * We publish all streams belonging to a session atomically wrt
1130 pthread_mutex_lock(&session
->lock
);
1132 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1134 stream_publish(stream
);
1139 * Inform the viewer that there are new streams in the session.
1141 if (session
->viewer_attached
) {
1142 uatomic_set(&session
->new_streams
, 1);
1144 pthread_mutex_unlock(&session
->lock
);
1148 * relay_add_stream: allocate a new stream for a session
1150 static int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1151 struct relay_connection
*conn
)
1155 struct relay_session
*session
= conn
->session
;
1156 struct relay_stream
*stream
= NULL
;
1157 struct lttcomm_relayd_status_stream reply
;
1158 struct ctf_trace
*trace
= NULL
;
1159 uint64_t stream_handle
= -1ULL;
1160 char *path_name
= NULL
, *channel_name
= NULL
;
1161 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1163 if (!session
|| conn
->version_check_done
== 0) {
1164 ERR("Trying to add a stream before version check");
1166 goto end_no_session
;
1169 switch (session
->minor
) {
1170 case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */
1171 ret
= cmd_recv_stream_2_1(conn
, &path_name
,
1174 case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
1176 ret
= cmd_recv_stream_2_2(conn
, &path_name
,
1177 &channel_name
, &tracefile_size
, &tracefile_count
);
1184 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1188 /* This stream here has one reference on the trace. */
1190 pthread_mutex_lock(&last_relay_stream_id_lock
);
1191 stream_handle
= ++last_relay_stream_id
;
1192 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1194 /* We pass ownership of path_name and channel_name. */
1195 stream
= stream_create(trace
, stream_handle
, path_name
,
1196 channel_name
, tracefile_size
, tracefile_count
);
1198 channel_name
= NULL
;
1201 * Streams are the owners of their trace. Reference to trace is
1202 * kept within stream_create().
1204 ctf_trace_put(trace
);
1207 memset(&reply
, 0, sizeof(reply
));
1208 reply
.handle
= htobe64(stream_handle
);
1210 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1212 reply
.ret_code
= htobe32(LTTNG_OK
);
1215 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1216 sizeof(struct lttcomm_relayd_status_stream
), 0);
1218 ERR("Relay sending stream id");
1219 ret
= (int) send_ret
;
1229 * relay_close_stream: close a specific stream
1231 static int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1232 struct relay_connection
*conn
)
1235 struct relay_session
*session
= conn
->session
;
1236 struct lttcomm_relayd_close_stream stream_info
;
1237 struct lttcomm_relayd_generic_reply reply
;
1238 struct relay_stream
*stream
;
1240 DBG("Close stream received");
1242 if (!session
|| conn
->version_check_done
== 0) {
1243 ERR("Trying to close a stream before version check");
1245 goto end_no_session
;
1248 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1249 sizeof(struct lttcomm_relayd_close_stream
), 0);
1250 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1252 /* Orderly shutdown. Not necessary to print an error. */
1253 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1255 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1258 goto end_no_session
;
1261 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1268 * Set last_net_seq_num before the close flag. Required by data
1271 pthread_mutex_lock(&stream
->lock
);
1272 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1273 pthread_mutex_unlock(&stream
->lock
);
1276 * This is one of the conditions which may trigger a stream close
1277 * with the others being:
1278 * 1) A close command is received for a stream
1279 * 2) The control connection owning the stream is closed
1280 * 3) We have received all of the stream's data _after_ a close
1283 try_stream_close(stream
);
1284 if (stream
->is_metadata
) {
1285 struct relay_viewer_stream
*vstream
;
1287 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1289 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1291 * Since all the metadata has been sent to the
1292 * viewer and that we have a request to close
1293 * its stream, we can safely teardown the
1294 * corresponding metadata viewer stream.
1296 viewer_stream_put(vstream
);
1298 /* Put local reference. */
1299 viewer_stream_put(vstream
);
1305 memset(&reply
, 0, sizeof(reply
));
1307 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1309 reply
.ret_code
= htobe32(LTTNG_OK
);
1311 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1312 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1314 ERR("Relay sending stream id");
1323 * relay_reset_metadata: reset a metadata stream
1326 int relay_reset_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1327 struct relay_connection
*conn
)
1330 struct relay_session
*session
= conn
->session
;
1331 struct lttcomm_relayd_reset_metadata stream_info
;
1332 struct lttcomm_relayd_generic_reply reply
;
1333 struct relay_stream
*stream
;
1335 DBG("Reset metadata received");
1337 if (!session
|| conn
->version_check_done
== 0) {
1338 ERR("Trying to reset a metadata stream before version check");
1340 goto end_no_session
;
1343 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1344 sizeof(struct lttcomm_relayd_reset_metadata
), 0);
1345 if (ret
< sizeof(struct lttcomm_relayd_reset_metadata
)) {
1347 /* Orderly shutdown. Not necessary to print an error. */
1348 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1350 ERR("Relay didn't receive valid reset_metadata struct "
1354 goto end_no_session
;
1356 DBG("Update metadata to version %" PRIu64
, be64toh(stream_info
.version
));
1358 /* Unsupported for live sessions for now. */
1359 if (session
->live_timer
!= 0) {
1364 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1369 pthread_mutex_lock(&stream
->lock
);
1370 if (!stream
->is_metadata
) {
1375 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1376 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1377 &stream
->stream_fd
->fd
);
1379 ERR("Failed to rotate metadata file %s of channel %s",
1380 stream
->path_name
, stream
->channel_name
);
1385 pthread_mutex_unlock(&stream
->lock
);
1389 memset(&reply
, 0, sizeof(reply
));
1391 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1393 reply
.ret_code
= htobe32(LTTNG_OK
);
1395 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1396 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1398 ERR("Relay sending reset metadata reply");
1407 * relay_unknown_command: send -1 if received unknown command
1409 static void relay_unknown_command(struct relay_connection
*conn
)
1411 struct lttcomm_relayd_generic_reply reply
;
1414 memset(&reply
, 0, sizeof(reply
));
1415 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1416 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1417 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1419 ERR("Relay sending unknown command");
1424 * relay_start: send an acknowledgment to the client to tell if we are
1425 * ready to receive data. We are ready if a session is established.
1427 static int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1428 struct relay_connection
*conn
)
1430 int ret
= htobe32(LTTNG_OK
);
1431 struct lttcomm_relayd_generic_reply reply
;
1432 struct relay_session
*session
= conn
->session
;
1435 DBG("Trying to start the streaming without a session established");
1436 ret
= htobe32(LTTNG_ERR_UNK
);
1439 memset(&reply
, 0, sizeof(reply
));
1440 reply
.ret_code
= ret
;
1441 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1442 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1444 ERR("Relay sending start ack");
1451 * Append padding to the file pointed by the file descriptor fd.
1453 static int write_padding_to_file(int fd
, uint32_t size
)
1462 zeros
= zmalloc(size
);
1463 if (zeros
== NULL
) {
1464 PERROR("zmalloc zeros for padding");
1469 ret
= lttng_write(fd
, zeros
, size
);
1471 PERROR("write padding to file");
1481 * relay_recv_metadata: receive the metadata for the session.
1483 static int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1484 struct relay_connection
*conn
)
1488 struct relay_session
*session
= conn
->session
;
1489 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1490 struct relay_stream
*metadata_stream
;
1491 uint64_t data_size
, payload_size
;
1494 ERR("Metadata sent before version check");
1499 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1500 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1501 ERR("Incorrect data size");
1505 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1507 if (data_buffer_size
< data_size
) {
1508 /* In case the realloc fails, we can free the memory */
1511 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1512 if (!tmp_data_ptr
) {
1513 ERR("Allocating data buffer");
1518 data_buffer
= tmp_data_ptr
;
1519 data_buffer_size
= data_size
;
1521 memset(data_buffer
, 0, data_size
);
1522 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1523 size_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1524 if (size_ret
< 0 || size_ret
!= data_size
) {
1525 if (size_ret
== 0) {
1526 /* Orderly shutdown. Not necessary to print an error. */
1527 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1529 ERR("Relay didn't receive the whole metadata");
1534 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1536 metadata_stream
= stream_get_by_id(be64toh(metadata_struct
->stream_id
));
1537 if (!metadata_stream
) {
1542 pthread_mutex_lock(&metadata_stream
->lock
);
1544 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
, metadata_struct
->payload
,
1546 if (size_ret
< payload_size
) {
1547 ERR("Relay error writing metadata on file");
1552 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1553 be32toh(metadata_struct
->padding_size
));
1558 metadata_stream
->metadata_received
+=
1559 payload_size
+ be32toh(metadata_struct
->padding_size
);
1560 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1561 metadata_stream
->metadata_received
);
1564 pthread_mutex_unlock(&metadata_stream
->lock
);
1565 stream_put(metadata_stream
);
1571 * relay_send_version: send relayd version number
1573 static int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1574 struct relay_connection
*conn
)
1577 struct lttcomm_relayd_version reply
, msg
;
1579 conn
->version_check_done
= 1;
1581 /* Get version from the other side. */
1582 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1583 if (ret
< 0 || ret
!= sizeof(msg
)) {
1585 /* Orderly shutdown. Not necessary to print an error. */
1586 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1588 ERR("Relay failed to receive the version values.");
1594 memset(&reply
, 0, sizeof(reply
));
1595 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1596 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1598 /* Major versions must be the same */
1599 if (reply
.major
!= be32toh(msg
.major
)) {
1600 DBG("Incompatible major versions (%u vs %u), deleting session",
1601 reply
.major
, be32toh(msg
.major
));
1602 connection_put(conn
);
1607 conn
->major
= reply
.major
;
1608 /* We adapt to the lowest compatible version */
1609 if (reply
.minor
<= be32toh(msg
.minor
)) {
1610 conn
->minor
= reply
.minor
;
1612 conn
->minor
= be32toh(msg
.minor
);
1615 reply
.major
= htobe32(reply
.major
);
1616 reply
.minor
= htobe32(reply
.minor
);
1617 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1618 sizeof(struct lttcomm_relayd_version
), 0);
1620 ERR("Relay sending version");
1623 DBG("Version check done using protocol %u.%u", conn
->major
,
1631 * Check for data pending for a given stream id from the session daemon.
1633 static int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1634 struct relay_connection
*conn
)
1636 struct relay_session
*session
= conn
->session
;
1637 struct lttcomm_relayd_data_pending msg
;
1638 struct lttcomm_relayd_generic_reply reply
;
1639 struct relay_stream
*stream
;
1641 uint64_t last_net_seq_num
, stream_id
;
1643 DBG("Data pending command received");
1645 if (!session
|| conn
->version_check_done
== 0) {
1646 ERR("Trying to check for data before version check");
1648 goto end_no_session
;
1651 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1652 if (ret
< sizeof(msg
)) {
1654 /* Orderly shutdown. Not necessary to print an error. */
1655 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1657 ERR("Relay didn't receive valid data_pending struct size : %d",
1661 goto end_no_session
;
1664 stream_id
= be64toh(msg
.stream_id
);
1665 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1667 stream
= stream_get_by_id(stream_id
);
1668 if (stream
== NULL
) {
1673 pthread_mutex_lock(&stream
->lock
);
1675 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1676 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1679 /* Avoid wrapping issue */
1680 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1681 /* Data has in fact been written and is NOT pending */
1684 /* Data still being streamed thus pending */
1688 stream
->data_pending_check_done
= true;
1689 pthread_mutex_unlock(&stream
->lock
);
1694 memset(&reply
, 0, sizeof(reply
));
1695 reply
.ret_code
= htobe32(ret
);
1696 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1698 ERR("Relay data pending ret code failed");
1706 * Wait for the control socket to reach a quiescent state.
1708 * Note that for now, when receiving this command from the session
1709 * daemon, this means that every subsequent commands or data received on
1710 * the control socket has been handled. So, this is why we simply return
1713 static int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1714 struct relay_connection
*conn
)
1718 struct relay_stream
*stream
;
1719 struct lttcomm_relayd_quiescent_control msg
;
1720 struct lttcomm_relayd_generic_reply reply
;
1722 DBG("Checking quiescent state on control socket");
1724 if (!conn
->session
|| conn
->version_check_done
== 0) {
1725 ERR("Trying to check for data before version check");
1727 goto end_no_session
;
1730 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1731 if (ret
< sizeof(msg
)) {
1733 /* Orderly shutdown. Not necessary to print an error. */
1734 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1736 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1740 goto end_no_session
;
1743 stream_id
= be64toh(msg
.stream_id
);
1744 stream
= stream_get_by_id(stream_id
);
1748 pthread_mutex_lock(&stream
->lock
);
1749 stream
->data_pending_check_done
= true;
1750 pthread_mutex_unlock(&stream
->lock
);
1751 DBG("Relay quiescent control pending flag set to %" PRIu64
, stream_id
);
1754 memset(&reply
, 0, sizeof(reply
));
1755 reply
.ret_code
= htobe32(LTTNG_OK
);
1756 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1758 ERR("Relay data quiescent control ret code failed");
1766 * Initialize a data pending command. This means that a consumer is about
1767 * to ask for data pending for each stream it holds. Simply iterate over
1768 * all streams of a session and set the data_pending_check_done flag.
1770 * This command returns to the client a LTTNG_OK code.
1772 static int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1773 struct relay_connection
*conn
)
1776 struct lttng_ht_iter iter
;
1777 struct lttcomm_relayd_begin_data_pending msg
;
1778 struct lttcomm_relayd_generic_reply reply
;
1779 struct relay_stream
*stream
;
1780 uint64_t session_id
;
1785 DBG("Init streams for data pending");
1787 if (!conn
->session
|| conn
->version_check_done
== 0) {
1788 ERR("Trying to check for data before version check");
1790 goto end_no_session
;
1793 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1794 if (ret
< sizeof(msg
)) {
1796 /* Orderly shutdown. Not necessary to print an error. */
1797 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1799 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1803 goto end_no_session
;
1806 session_id
= be64toh(msg
.session_id
);
1809 * Iterate over all streams to set the begin data pending flag.
1810 * For now, the streams are indexed by stream handle so we have
1811 * to iterate over all streams to find the one associated with
1812 * the right session_id.
1815 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1817 if (!stream_get(stream
)) {
1820 if (stream
->trace
->session
->id
== session_id
) {
1821 pthread_mutex_lock(&stream
->lock
);
1822 stream
->data_pending_check_done
= false;
1823 pthread_mutex_unlock(&stream
->lock
);
1824 DBG("Set begin data pending flag to stream %" PRIu64
,
1825 stream
->stream_handle
);
1831 memset(&reply
, 0, sizeof(reply
));
1832 /* All good, send back reply. */
1833 reply
.ret_code
= htobe32(LTTNG_OK
);
1835 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1837 ERR("Relay begin data pending send reply failed");
1845 * End data pending command. This will check, for a given session id, if
1846 * each stream associated with it has its data_pending_check_done flag
1847 * set. If not, this means that the client lost track of the stream but
1848 * the data is still being streamed on our side. In this case, we inform
1849 * the client that data is in flight.
1851 * Return to the client if there is data in flight or not with a ret_code.
1853 static int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1854 struct relay_connection
*conn
)
1857 struct lttng_ht_iter iter
;
1858 struct lttcomm_relayd_end_data_pending msg
;
1859 struct lttcomm_relayd_generic_reply reply
;
1860 struct relay_stream
*stream
;
1861 uint64_t session_id
;
1862 uint32_t is_data_inflight
= 0;
1864 DBG("End data pending command");
1866 if (!conn
->session
|| conn
->version_check_done
== 0) {
1867 ERR("Trying to check for data before version check");
1869 goto end_no_session
;
1872 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1873 if (ret
< sizeof(msg
)) {
1875 /* Orderly shutdown. Not necessary to print an error. */
1876 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1878 ERR("Relay didn't receive valid end data_pending struct size: %d",
1882 goto end_no_session
;
1885 session_id
= be64toh(msg
.session_id
);
1888 * Iterate over all streams to see if the begin data pending
1892 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1894 if (!stream_get(stream
)) {
1897 if (stream
->trace
->session
->id
!= session_id
) {
1901 pthread_mutex_lock(&stream
->lock
);
1902 if (!stream
->data_pending_check_done
) {
1903 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
1904 is_data_inflight
= 1;
1905 DBG("Data is still in flight for stream %" PRIu64
,
1906 stream
->stream_handle
);
1907 pthread_mutex_unlock(&stream
->lock
);
1912 pthread_mutex_unlock(&stream
->lock
);
1917 memset(&reply
, 0, sizeof(reply
));
1918 /* All good, send back reply. */
1919 reply
.ret_code
= htobe32(is_data_inflight
);
1921 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1923 ERR("Relay end data pending send reply failed");
1931 * Receive an index for a specific stream.
1933 * Return 0 on success else a negative value.
1935 static int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1936 struct relay_connection
*conn
)
1939 struct relay_session
*session
= conn
->session
;
1940 struct lttcomm_relayd_index index_info
;
1941 struct relay_index
*index
;
1942 struct lttcomm_relayd_generic_reply reply
;
1943 struct relay_stream
*stream
;
1944 uint64_t net_seq_num
;
1948 DBG("Relay receiving index");
1950 if (!session
|| conn
->version_check_done
== 0) {
1951 ERR("Trying to close a stream before version check");
1953 goto end_no_session
;
1956 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
1957 sizeof(index_info
), 0);
1958 if (ret
< sizeof(index_info
)) {
1960 /* Orderly shutdown. Not necessary to print an error. */
1961 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1963 ERR("Relay didn't receive valid index struct size : %d", ret
);
1966 goto end_no_session
;
1969 net_seq_num
= be64toh(index_info
.net_seq_num
);
1971 stream
= stream_get_by_id(be64toh(index_info
.relay_stream_id
));
1973 ERR("stream_get_by_id not found");
1977 pthread_mutex_lock(&stream
->lock
);
1979 /* Live beacon handling */
1980 if (index_info
.packet_size
== 0) {
1981 DBG("Received live beacon for stream %" PRIu64
,
1982 stream
->stream_handle
);
1985 * Only flag a stream inactive when it has already
1986 * received data and no indexes are in flight.
1988 if (stream
->index_received_seqcount
> 0
1989 && stream
->indexes_in_flight
== 0) {
1990 stream
->beacon_ts_end
=
1991 be64toh(index_info
.timestamp_end
);
1994 goto end_stream_put
;
1996 stream
->beacon_ts_end
= -1ULL;
1999 if (stream
->ctf_stream_id
== -1ULL) {
2000 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
2002 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
2005 ERR("relay_index_get_by_id_or_create index NULL");
2006 goto end_stream_put
;
2008 if (set_index_control_data(index
, &index_info
, conn
)) {
2009 ERR("set_index_control_data error");
2010 relay_index_put(index
);
2012 goto end_stream_put
;
2014 ret
= relay_index_try_flush(index
);
2016 tracefile_array_commit_seq(stream
->tfa
);
2017 stream
->index_received_seqcount
++;
2018 } else if (ret
> 0) {
2022 ERR("relay_index_try_flush error %d", ret
);
2023 relay_index_put(index
);
2028 pthread_mutex_unlock(&stream
->lock
);
2033 memset(&reply
, 0, sizeof(reply
));
2035 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2037 reply
.ret_code
= htobe32(LTTNG_OK
);
2039 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2041 ERR("Relay sending close index id reply");
2050 * Receive the streams_sent message.
2052 * Return 0 on success else a negative value.
2054 static int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2055 struct relay_connection
*conn
)
2058 struct lttcomm_relayd_generic_reply reply
;
2062 DBG("Relay receiving streams_sent");
2064 if (!conn
->session
|| conn
->version_check_done
== 0) {
2065 ERR("Trying to close a stream before version check");
2067 goto end_no_session
;
2071 * Publish every pending stream in the connection recv list which are
2072 * now ready to be used by the viewer.
2074 publish_connection_local_streams(conn
);
2076 memset(&reply
, 0, sizeof(reply
));
2077 reply
.ret_code
= htobe32(LTTNG_OK
);
2078 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2080 ERR("Relay sending sent_stream reply");
2092 * Process the commands received on the control socket
2094 static int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2095 struct relay_connection
*conn
)
2099 switch (be32toh(recv_hdr
->cmd
)) {
2100 case RELAYD_CREATE_SESSION
:
2101 ret
= relay_create_session(recv_hdr
, conn
);
2103 case RELAYD_ADD_STREAM
:
2104 ret
= relay_add_stream(recv_hdr
, conn
);
2106 case RELAYD_START_DATA
:
2107 ret
= relay_start(recv_hdr
, conn
);
2109 case RELAYD_SEND_METADATA
:
2110 ret
= relay_recv_metadata(recv_hdr
, conn
);
2112 case RELAYD_VERSION
:
2113 ret
= relay_send_version(recv_hdr
, conn
);
2115 case RELAYD_CLOSE_STREAM
:
2116 ret
= relay_close_stream(recv_hdr
, conn
);
2118 case RELAYD_DATA_PENDING
:
2119 ret
= relay_data_pending(recv_hdr
, conn
);
2121 case RELAYD_QUIESCENT_CONTROL
:
2122 ret
= relay_quiescent_control(recv_hdr
, conn
);
2124 case RELAYD_BEGIN_DATA_PENDING
:
2125 ret
= relay_begin_data_pending(recv_hdr
, conn
);
2127 case RELAYD_END_DATA_PENDING
:
2128 ret
= relay_end_data_pending(recv_hdr
, conn
);
2130 case RELAYD_SEND_INDEX
:
2131 ret
= relay_recv_index(recv_hdr
, conn
);
2133 case RELAYD_STREAMS_SENT
:
2134 ret
= relay_streams_sent(recv_hdr
, conn
);
2136 case RELAYD_RESET_METADATA
:
2137 ret
= relay_reset_metadata(recv_hdr
, conn
);
2139 case RELAYD_UPDATE_SYNC_INFO
:
2141 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2142 relay_unknown_command(conn
);
2152 * Handle index for a data stream.
2154 * Called with the stream lock held.
2156 * Return 0 on success else a negative value.
2158 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2162 uint64_t data_offset
;
2163 struct relay_index
*index
;
2165 /* Get data offset because we are about to update the index. */
2166 data_offset
= htobe64(stream
->tracefile_size_current
);
2168 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
2169 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
2172 * Lookup for an existing index for that stream id/sequence
2173 * number. If it exists, the control thread has already received the
2174 * data for it, thus we need to write it to disk.
2176 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
2182 if (rotate_index
|| !stream
->index_fd
) {
2185 /* Put ref on previous index_fd. */
2186 if (stream
->index_fd
) {
2187 stream_fd_put(stream
->index_fd
);
2188 stream
->index_fd
= NULL
;
2191 fd
= index_create_file(stream
->path_name
, stream
->channel_name
,
2192 -1, -1, stream
->tracefile_size
,
2193 tracefile_array_get_file_index_head(stream
->tfa
));
2196 /* Put self-ref for this index due to error. */
2197 relay_index_put(index
);
2200 stream
->index_fd
= stream_fd_create(fd
);
2201 if (!stream
->index_fd
) {
2204 PERROR("Error closing FD %d", fd
);
2206 /* Put self-ref for this index due to error. */
2207 relay_index_put(index
);
2208 /* Will put the local ref. */
2213 if (relay_index_set_fd(index
, stream
->index_fd
, data_offset
)) {
2215 /* Put self-ref for this index due to error. */
2216 relay_index_put(index
);
2220 ret
= relay_index_try_flush(index
);
2222 tracefile_array_commit_seq(stream
->tfa
);
2223 stream
->index_received_seqcount
++;
2224 } else if (ret
> 0) {
2228 /* Put self-ref for this index due to error. */
2229 relay_index_put(index
);
2237 * relay_process_data: Process the data received on the data socket
2239 static int relay_process_data(struct relay_connection
*conn
)
2241 int ret
= 0, rotate_index
= 0;
2243 struct relay_stream
*stream
;
2244 struct lttcomm_relayd_data_hdr data_hdr
;
2246 uint64_t net_seq_num
;
2248 struct relay_session
*session
;
2249 bool new_stream
= false, close_requested
= false;
2250 size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
2251 size_t recv_off
= 0;
2252 char data_buffer
[chunk_size
];
2254 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2255 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2258 /* Orderly shutdown. Not necessary to print an error. */
2259 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2261 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2267 stream_id
= be64toh(data_hdr
.stream_id
);
2268 stream
= stream_get_by_id(stream_id
);
2270 ERR("relay_process_data: Cannot find stream %" PRIu64
, stream_id
);
2274 session
= stream
->trace
->session
;
2275 data_size
= be32toh(data_hdr
.data_size
);
2277 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2279 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2280 data_size
, stream_id
, net_seq_num
);
2282 pthread_mutex_lock(&stream
->lock
);
2284 /* Check if a rotation is needed. */
2285 if (stream
->tracefile_size
> 0 &&
2286 (stream
->tracefile_size_current
+ data_size
) >
2287 stream
->tracefile_size
) {
2288 uint64_t old_id
, new_id
;
2290 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
2291 tracefile_array_file_rotate(stream
->tfa
);
2293 /* new_id is updated by utils_rotate_stream_file. */
2296 ret
= utils_rotate_stream_file(stream
->path_name
,
2297 stream
->channel_name
, stream
->tracefile_size
,
2298 stream
->tracefile_count
, -1,
2299 -1, stream
->stream_fd
->fd
,
2300 &new_id
, &stream
->stream_fd
->fd
);
2302 ERR("Rotating stream output file");
2303 goto end_stream_unlock
;
2306 * Reset current size because we just performed a stream
2309 stream
->tracefile_size_current
= 0;
2314 * Index are handled in protocol version 2.4 and above. Also,
2315 * snapshot and index are NOT supported.
2317 if (session
->minor
>= 4 && !session
->snapshot
) {
2318 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2320 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
2321 stream
->stream_handle
, net_seq_num
, ret
);
2322 goto end_stream_unlock
;
2326 for (recv_off
= 0; recv_off
< data_size
; recv_off
+= chunk_size
) {
2327 size_t recv_size
= min(data_size
- recv_off
, chunk_size
);
2329 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, recv_size
, 0);
2332 /* Orderly shutdown. Not necessary to print an error. */
2333 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2335 ERR("Socket %d error %d", conn
->sock
->fd
, ret
);
2338 goto end_stream_unlock
;
2341 /* Write data to stream output fd. */
2342 size_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
2344 if (size_ret
< recv_size
) {
2345 ERR("Relay error writing data to file");
2347 goto end_stream_unlock
;
2350 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
2351 size_ret
, stream
->stream_handle
);
2354 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
2355 be32toh(data_hdr
.padding_size
));
2357 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
2358 stream
->stream_handle
, net_seq_num
, ret
);
2359 goto end_stream_unlock
;
2361 stream
->tracefile_size_current
+=
2362 data_size
+ be32toh(data_hdr
.padding_size
);
2363 if (stream
->prev_seq
== -1ULL) {
2367 stream
->prev_seq
= net_seq_num
;
2370 close_requested
= stream
->close_requested
;
2371 pthread_mutex_unlock(&stream
->lock
);
2372 if (close_requested
) {
2373 try_stream_close(stream
);
2377 pthread_mutex_lock(&session
->lock
);
2378 uatomic_set(&session
->new_streams
, 1);
2379 pthread_mutex_unlock(&session
->lock
);
2386 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2390 (void) lttng_poll_del(events
, pollfd
);
2392 ret
= close(pollfd
);
2394 ERR("Closing pollfd %d", pollfd
);
2398 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
2399 int pollfd
, struct relay_connection
*conn
)
2401 const char *type_str
;
2403 switch (conn
->type
) {
2408 type_str
= "Control";
2410 case RELAY_VIEWER_COMMAND
:
2411 type_str
= "Viewer Command";
2413 case RELAY_VIEWER_NOTIFICATION
:
2414 type_str
= "Viewer Notification";
2417 type_str
= "Unknown";
2419 cleanup_connection_pollfd(events
, pollfd
);
2420 connection_put(conn
);
2421 DBG("%s connection closed with %d", type_str
, pollfd
);
2425 * This thread does the actual work
2427 static void *relay_thread_worker(void *data
)
2429 int ret
, err
= -1, last_seen_data_fd
= -1;
2431 struct lttng_poll_event events
;
2432 struct lttng_ht
*relay_connections_ht
;
2433 struct lttng_ht_iter iter
;
2434 struct lttcomm_relayd_hdr recv_hdr
;
2435 struct relay_connection
*destroy_conn
= NULL
;
2437 DBG("[thread] Relay worker started");
2439 rcu_register_thread();
2441 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2443 if (testpoint(relayd_thread_worker
)) {
2444 goto error_testpoint
;
2447 health_code_update();
2449 /* table of connections indexed on socket */
2450 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2451 if (!relay_connections_ht
) {
2452 goto relay_connections_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
2490 * prioritized so we don't starve it with high
2491 * throughput tracing data on the data connection.
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();
2502 * No activity for this FD (poll
2508 /* Thread quit pipe has been closed. Killing thread. */
2509 ret
= check_thread_quit_pipe(pollfd
, revents
);
2515 /* Inspect the relay conn pipe for new connection */
2516 if (pollfd
== relay_conn_pipe
[0]) {
2517 if (revents
& LPOLLIN
) {
2518 struct relay_connection
*conn
;
2520 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2524 lttng_poll_add(&events
, conn
->sock
->fd
,
2525 LPOLLIN
| LPOLLRDHUP
);
2526 connection_ht_add(relay_connections_ht
, conn
);
2527 DBG("Connection socket %d added", conn
->sock
->fd
);
2528 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2529 ERR("Relay connection pipe error");
2532 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2536 struct relay_connection
*ctrl_conn
;
2538 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
2539 /* If not found, there is a synchronization issue. */
2542 if (ctrl_conn
->type
== RELAY_DATA
) {
2543 if (revents
& LPOLLIN
) {
2545 * Flag the last seen data fd not deleted. It will be
2546 * used as the last seen fd if any fd gets deleted in
2549 last_notdel_data_fd
= pollfd
;
2551 goto put_ctrl_connection
;
2553 assert(ctrl_conn
->type
== RELAY_CONTROL
);
2555 if (revents
& LPOLLIN
) {
2556 ret
= ctrl_conn
->sock
->ops
->recvmsg(ctrl_conn
->sock
,
2557 &recv_hdr
, sizeof(recv_hdr
), 0);
2559 /* Connection closed */
2560 relay_thread_close_connection(&events
, pollfd
,
2563 ret
= relay_process_control(&recv_hdr
, ctrl_conn
);
2565 /* Clear the session on error. */
2566 relay_thread_close_connection(&events
,
2571 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2572 relay_thread_close_connection(&events
,
2574 if (last_seen_data_fd
== pollfd
) {
2575 last_seen_data_fd
= last_notdel_data_fd
;
2578 ERR("Unexpected poll events %u for control sock %d",
2580 connection_put(ctrl_conn
);
2583 put_ctrl_connection
:
2584 connection_put(ctrl_conn
);
2589 * The last loop handled a control request, go back to poll to make
2590 * sure we prioritise the control socket.
2596 if (last_seen_data_fd
>= 0) {
2597 for (i
= 0; i
< nb_fd
; i
++) {
2598 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2600 health_code_update();
2602 if (last_seen_data_fd
== pollfd
) {
2609 /* Process data connection. */
2610 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2611 /* Fetch the poll data. */
2612 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2613 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2614 struct relay_connection
*data_conn
;
2616 health_code_update();
2619 /* No activity for this FD (poll implementation). */
2623 /* Skip the command pipe. It's handled in the first loop. */
2624 if (pollfd
== relay_conn_pipe
[0]) {
2628 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
2630 /* Skip it. Might be removed before. */
2633 if (data_conn
->type
== RELAY_CONTROL
) {
2634 goto put_data_connection
;
2636 assert(data_conn
->type
== RELAY_DATA
);
2638 if (revents
& LPOLLIN
) {
2639 ret
= relay_process_data(data_conn
);
2640 /* Connection closed */
2642 relay_thread_close_connection(&events
, pollfd
,
2645 * Every goto restart call sets the last seen fd where
2646 * here we don't really care since we gracefully
2647 * continue the loop after the connection is deleted.
2650 /* Keep last seen port. */
2651 last_seen_data_fd
= pollfd
;
2652 connection_put(data_conn
);
2655 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2656 relay_thread_close_connection(&events
, pollfd
,
2659 ERR("Unknown poll events %u for data sock %d",
2662 put_data_connection
:
2663 connection_put(data_conn
);
2665 last_seen_data_fd
= -1;
2668 /* Normal exit, no error */
2673 /* Cleanup reamaining connection object. */
2675 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
2678 health_code_update();
2680 * No need to grab another ref, because we own
2683 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
2688 lttng_poll_clean(&events
);
2690 lttng_ht_destroy(relay_connections_ht
);
2691 relay_connections_ht_error
:
2692 /* Close relay conn pipes */
2693 utils_close_pipe(relay_conn_pipe
);
2695 DBG("Thread exited with error");
2697 DBG("Worker thread cleanup complete");
2701 ERR("Health error occurred in %s", __func__
);
2703 health_unregister(health_relayd
);
2704 rcu_unregister_thread();
2705 lttng_relay_stop_threads();
2710 * Create the relay command pipe to wake thread_manage_apps.
2711 * Closed in cleanup().
2713 static int create_relay_conn_pipe(void)
2717 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2725 int main(int argc
, char **argv
)
2727 int ret
= 0, retval
= 0;
2730 /* Parse arguments */
2732 if (set_options(argc
, argv
)) {
2737 if (set_signal_handler()) {
2742 /* Try to create directory if -o, --output is specified. */
2743 if (opt_output_path
) {
2744 if (*opt_output_path
!= '/') {
2745 ERR("Please specify an absolute path for -o, --output PATH");
2750 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
2753 ERR("Unable to create %s", opt_output_path
);
2760 if (opt_daemon
|| opt_background
) {
2763 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2771 * We are in the child. Make sure all other file
2772 * descriptors are closed, in case we are called with
2773 * more opened file descriptors than the standard ones.
2775 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2780 /* Initialize thread health monitoring */
2781 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2782 if (!health_relayd
) {
2783 PERROR("health_app_create error");
2785 goto exit_health_app_create
;
2788 /* Create thread quit pipe */
2789 if (init_thread_quit_pipe()) {
2791 goto exit_init_data
;
2794 /* Setup the thread apps communication pipe. */
2795 if (create_relay_conn_pipe()) {
2797 goto exit_init_data
;
2800 /* Init relay command queue. */
2801 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
2803 /* Initialize communication library */
2805 lttcomm_inet_init();
2807 /* tables of sessions indexed by session ID */
2808 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2811 goto exit_init_data
;
2814 /* tables of streams indexed by stream ID */
2815 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2816 if (!relay_streams_ht
) {
2818 goto exit_init_data
;
2821 /* tables of streams indexed by stream ID */
2822 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2823 if (!viewer_streams_ht
) {
2825 goto exit_init_data
;
2828 ret
= utils_create_pipe(health_quit_pipe
);
2831 goto exit_health_quit_pipe
;
2834 /* Create thread to manage the client socket */
2835 ret
= pthread_create(&health_thread
, default_pthread_attr(),
2836 thread_manage_health
, (void *) NULL
);
2839 PERROR("pthread_create health");
2841 goto exit_health_thread
;
2844 /* Setup the dispatcher thread */
2845 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
2846 relay_thread_dispatcher
, (void *) NULL
);
2849 PERROR("pthread_create dispatcher");
2851 goto exit_dispatcher_thread
;
2854 /* Setup the worker thread */
2855 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
2856 relay_thread_worker
, NULL
);
2859 PERROR("pthread_create worker");
2861 goto exit_worker_thread
;
2864 /* Setup the listener thread */
2865 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
2866 relay_thread_listener
, (void *) NULL
);
2869 PERROR("pthread_create listener");
2871 goto exit_listener_thread
;
2874 ret
= relayd_live_create(live_uri
);
2876 ERR("Starting live viewer threads");
2882 * This is where we start awaiting program completion (e.g. through
2883 * signal that asks threads to teardown).
2886 ret
= relayd_live_join();
2892 ret
= pthread_join(listener_thread
, &status
);
2895 PERROR("pthread_join listener_thread");
2899 exit_listener_thread
:
2900 ret
= pthread_join(worker_thread
, &status
);
2903 PERROR("pthread_join worker_thread");
2908 ret
= pthread_join(dispatcher_thread
, &status
);
2911 PERROR("pthread_join dispatcher_thread");
2914 exit_dispatcher_thread
:
2916 ret
= pthread_join(health_thread
, &status
);
2919 PERROR("pthread_join health_thread");
2924 utils_close_pipe(health_quit_pipe
);
2925 exit_health_quit_pipe
:
2928 health_app_destroy(health_relayd
);
2929 exit_health_app_create
:
2933 /* Ensure all prior call_rcu are done. */