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/align.h>
58 #include <common/config/session-config.h>
59 #include <urcu/rculist.h>
62 #include "ctf-trace.h"
65 #include "lttng-relayd.h"
67 #include "health-relayd.h"
68 #include "testpoint.h"
69 #include "viewer-stream.h"
72 #include "connection.h"
73 #include "tracefile-array.h"
74 #include "tcp_keep_alive.h"
76 static const char *help_msg
=
77 #ifdef LTTNG_EMBED_HELP
78 #include <lttng-relayd.8.h>
84 /* command line options */
85 char *opt_output_path
;
86 static int opt_daemon
, opt_background
;
89 * We need to wait for listener and live listener threads, as well as
90 * health check thread, before being ready to signal readiness.
92 #define NR_LTTNG_RELAY_READY 3
93 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
95 /* Size of receive buffer. */
96 #define RECV_DATA_BUFFER_SIZE 65536
97 #define FILE_COPY_BUFFER_SIZE 65536
99 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
100 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
102 static struct lttng_uri
*control_uri
;
103 static struct lttng_uri
*data_uri
;
104 static struct lttng_uri
*live_uri
;
106 const char *progname
;
108 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
109 static int tracing_group_name_override
;
111 const char * const config_section_name
= "relayd";
114 * Quit pipe for all threads. This permits a single cancellation point
115 * for all threads when receiving an event on the pipe.
117 int thread_quit_pipe
[2] = { -1, -1 };
120 * This pipe is used to inform the worker thread that a command is queued and
121 * ready to be processed.
123 static int relay_conn_pipe
[2] = { -1, -1 };
125 /* Shared between threads */
126 static int dispatch_thread_exit
;
128 static pthread_t listener_thread
;
129 static pthread_t dispatcher_thread
;
130 static pthread_t worker_thread
;
131 static pthread_t health_thread
;
134 * last_relay_stream_id_lock protects last_relay_stream_id increment
135 * atomicity on 32-bit architectures.
137 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
138 static uint64_t last_relay_stream_id
;
141 * Relay command queue.
143 * The relay_thread_listener and relay_thread_dispatcher communicate with this
146 static struct relay_conn_queue relay_conn_queue
;
148 /* buffer allocated at startup, used to store the trace data */
149 static char *data_buffer
;
150 static unsigned int data_buffer_size
;
152 /* Global relay stream hash table. */
153 struct lttng_ht
*relay_streams_ht
;
155 /* Global relay viewer stream hash table. */
156 struct lttng_ht
*viewer_streams_ht
;
158 /* Global relay sessions hash table. */
159 struct lttng_ht
*sessions_ht
;
161 /* Relayd health monitoring */
162 struct health_app
*health_relayd
;
164 static struct option long_options
[] = {
165 { "control-port", 1, 0, 'C', },
166 { "data-port", 1, 0, 'D', },
167 { "live-port", 1, 0, 'L', },
168 { "daemonize", 0, 0, 'd', },
169 { "background", 0, 0, 'b', },
170 { "group", 1, 0, 'g', },
171 { "help", 0, 0, 'h', },
172 { "output", 1, 0, 'o', },
173 { "verbose", 0, 0, 'v', },
174 { "config", 1, 0, 'f' },
175 { "version", 0, 0, 'V' },
179 static const char *config_ignore_options
[] = { "help", "config", "version" };
182 * Take an option from the getopt output and set it in the right variable to be
185 * Return 0 on success else a negative value.
187 static 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 if (lttng_is_setuid_setgid()) {
200 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
201 "-C, --control-port");
203 ret
= uri_parse(arg
, &control_uri
);
205 ERR("Invalid control URI specified");
208 if (control_uri
->port
== 0) {
209 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
214 if (lttng_is_setuid_setgid()) {
215 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
218 ret
= uri_parse(arg
, &data_uri
);
220 ERR("Invalid data URI specified");
223 if (data_uri
->port
== 0) {
224 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
229 if (lttng_is_setuid_setgid()) {
230 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
233 ret
= uri_parse(arg
, &live_uri
);
235 ERR("Invalid live URI specified");
238 if (live_uri
->port
== 0) {
239 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
250 if (lttng_is_setuid_setgid()) {
251 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
254 tracing_group_name
= strdup(arg
);
255 if (tracing_group_name
== NULL
) {
260 tracing_group_name_override
= 1;
264 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
266 ERR("Cannot show --help for `lttng-relayd`");
271 fprintf(stdout
, "%s\n", VERSION
);
274 if (lttng_is_setuid_setgid()) {
275 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
278 ret
= asprintf(&opt_output_path
, "%s", arg
);
281 PERROR("asprintf opt_output_path");
287 /* Verbose level can increase using multiple -v */
289 lttng_opt_verbose
= config_parse_value(arg
);
291 /* Only 3 level of verbosity (-vvv). */
292 if (lttng_opt_verbose
< 3) {
293 lttng_opt_verbose
+= 1;
298 /* Unknown option or other error.
299 * Error is printed by getopt, just return */
312 * config_entry_handler_cb used to handle options read from a config file.
313 * See config_entry_handler_cb comment in common/config/session-config.h for the
314 * return value conventions.
316 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
320 if (!entry
|| !entry
->name
|| !entry
->value
) {
325 /* Check if the option is to be ignored */
326 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
327 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
332 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
333 /* Ignore if entry name is not fully matched. */
334 if (strcmp(entry
->name
, long_options
[i
].name
)) {
339 * If the option takes no argument on the command line,
340 * we have to check if the value is "true". We support
341 * non-zero numeric values, true, on and yes.
343 if (!long_options
[i
].has_arg
) {
344 ret
= config_parse_value(entry
->value
);
347 WARN("Invalid configuration value \"%s\" for option %s",
348 entry
->value
, entry
->name
);
350 /* False, skip boolean config option. */
355 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
359 WARN("Unrecognized option \"%s\" in daemon configuration file.",
366 static int set_options(int argc
, char **argv
)
368 int c
, ret
= 0, option_index
= 0, retval
= 0;
369 int orig_optopt
= optopt
, orig_optind
= optind
;
370 char *default_address
, *optstring
;
371 const char *config_path
= NULL
;
373 optstring
= utils_generate_optstring(long_options
,
374 sizeof(long_options
) / sizeof(struct option
));
380 /* Check for the --config option */
382 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
383 &option_index
)) != -1) {
387 } else if (c
!= 'f') {
391 if (lttng_is_setuid_setgid()) {
392 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
395 config_path
= utils_expand_path(optarg
);
397 ERR("Failed to resolve path: %s", optarg
);
402 ret
= config_get_section_entries(config_path
, config_section_name
,
403 config_entry_handler
, NULL
);
406 ERR("Invalid configuration option at line %i", ret
);
412 /* Reset getopt's global state */
413 optopt
= orig_optopt
;
414 optind
= orig_optind
;
416 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
421 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
428 /* assign default values */
429 if (control_uri
== NULL
) {
430 ret
= asprintf(&default_address
,
431 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
432 DEFAULT_NETWORK_CONTROL_PORT
);
434 PERROR("asprintf default data address");
439 ret
= uri_parse(default_address
, &control_uri
);
440 free(default_address
);
442 ERR("Invalid control URI specified");
447 if (data_uri
== NULL
) {
448 ret
= asprintf(&default_address
,
449 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
450 DEFAULT_NETWORK_DATA_PORT
);
452 PERROR("asprintf default data address");
457 ret
= uri_parse(default_address
, &data_uri
);
458 free(default_address
);
460 ERR("Invalid data URI specified");
465 if (live_uri
== NULL
) {
466 ret
= asprintf(&default_address
,
467 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
468 DEFAULT_NETWORK_VIEWER_PORT
);
470 PERROR("asprintf default viewer control address");
475 ret
= uri_parse(default_address
, &live_uri
);
476 free(default_address
);
478 ERR("Invalid viewer control URI specified");
489 static void print_global_objects(void)
491 rcu_register_thread();
493 print_viewer_streams();
494 print_relay_streams();
497 rcu_unregister_thread();
503 static void relayd_cleanup(void)
505 print_global_objects();
509 if (viewer_streams_ht
)
510 lttng_ht_destroy(viewer_streams_ht
);
511 if (relay_streams_ht
)
512 lttng_ht_destroy(relay_streams_ht
);
514 lttng_ht_destroy(sessions_ht
);
516 /* free the dynamically allocated opt_output_path */
517 free(opt_output_path
);
519 /* Close thread quit pipes */
520 utils_close_pipe(thread_quit_pipe
);
522 uri_free(control_uri
);
524 /* Live URI is freed in the live thread. */
526 if (tracing_group_name_override
) {
527 free((void *) tracing_group_name
);
532 * Write to writable pipe used to notify a thread.
534 static int notify_thread_pipe(int wpipe
)
538 ret
= lttng_write(wpipe
, "!", 1);
540 PERROR("write poll pipe");
548 static int notify_health_quit_pipe(int *pipe
)
552 ret
= lttng_write(pipe
[1], "4", 1);
554 PERROR("write relay health quit");
563 * Stop all relayd and relayd-live threads.
565 int lttng_relay_stop_threads(void)
569 /* Stopping all threads */
570 DBG("Terminating all threads");
571 if (notify_thread_pipe(thread_quit_pipe
[1])) {
572 ERR("write error on thread quit pipe");
576 if (notify_health_quit_pipe(health_quit_pipe
)) {
577 ERR("write error on health quit pipe");
580 /* Dispatch thread */
581 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
582 futex_nto1_wake(&relay_conn_queue
.futex
);
584 if (relayd_live_stop()) {
585 ERR("Error stopping live threads");
592 * Signal handler for the daemon
594 * Simply stop all worker threads, leaving main() return gracefully after
595 * joining all threads and calling cleanup().
597 static void sighandler(int sig
)
601 DBG("SIGINT caught");
602 if (lttng_relay_stop_threads()) {
603 ERR("Error stopping threads");
607 DBG("SIGTERM caught");
608 if (lttng_relay_stop_threads()) {
609 ERR("Error stopping threads");
613 CMM_STORE_SHARED(recv_child_signal
, 1);
621 * Setup signal handler for :
622 * SIGINT, SIGTERM, SIGPIPE
624 static int set_signal_handler(void)
630 if ((ret
= sigemptyset(&sigset
)) < 0) {
631 PERROR("sigemptyset");
638 sa
.sa_handler
= sighandler
;
639 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
644 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
649 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
654 sa
.sa_handler
= SIG_IGN
;
655 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
660 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
665 void lttng_relay_notify_ready(void)
667 /* Notify the parent of the fork() process that we are ready. */
668 if (opt_daemon
|| opt_background
) {
669 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
670 kill(child_ppid
, SIGUSR1
);
676 * Init thread quit pipe.
678 * Return -1 on error or 0 if all pipes are created.
680 static int init_thread_quit_pipe(void)
684 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
690 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
692 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
696 if (events
== NULL
|| size
== 0) {
701 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
707 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
719 * Check if the thread quit pipe was triggered.
721 * Return 1 if it was triggered else 0;
723 static int check_thread_quit_pipe(int fd
, uint32_t events
)
725 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
733 * Create and init socket from uri.
735 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
738 struct lttcomm_sock
*sock
= NULL
;
740 sock
= lttcomm_alloc_sock_from_uri(uri
);
742 ERR("Allocating socket");
746 ret
= lttcomm_create_sock(sock
);
750 DBG("Listening on sock %d", sock
->fd
);
752 ret
= sock
->ops
->bind(sock
);
757 ret
= sock
->ops
->listen(sock
, -1);
767 lttcomm_destroy_sock(sock
);
773 * This thread manages the listening for new connections on the network
775 static void *relay_thread_listener(void *data
)
777 int i
, ret
, pollfd
, err
= -1;
778 uint32_t revents
, nb_fd
;
779 struct lttng_poll_event events
;
780 struct lttcomm_sock
*control_sock
, *data_sock
;
782 DBG("[thread] Relay listener started");
784 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
786 health_code_update();
788 control_sock
= relay_socket_create(control_uri
);
790 goto error_sock_control
;
793 data_sock
= relay_socket_create(data_uri
);
795 goto error_sock_relay
;
799 * Pass 3 as size here for the thread quit pipe, control and
802 ret
= create_thread_poll_set(&events
, 3);
804 goto error_create_poll
;
807 /* Add the control socket */
808 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
813 /* Add the data socket */
814 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
819 lttng_relay_notify_ready();
821 if (testpoint(relayd_thread_listener
)) {
822 goto error_testpoint
;
826 health_code_update();
828 DBG("Listener accepting connections");
832 ret
= lttng_poll_wait(&events
, -1);
836 * Restart interrupted system call.
838 if (errno
== EINTR
) {
846 DBG("Relay new connection received");
847 for (i
= 0; i
< nb_fd
; i
++) {
848 health_code_update();
850 /* Fetch once the poll data */
851 revents
= LTTNG_POLL_GETEV(&events
, i
);
852 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
856 * No activity for this FD (poll
862 /* Thread quit pipe has been closed. Killing thread. */
863 ret
= check_thread_quit_pipe(pollfd
, revents
);
869 if (revents
& LPOLLIN
) {
871 * A new connection is requested, therefore a
872 * sessiond/consumerd connection is allocated in
873 * this thread, enqueued to a global queue and
874 * dequeued (and freed) in the worker thread.
877 struct relay_connection
*new_conn
;
878 struct lttcomm_sock
*newsock
;
879 enum connection_type type
;
881 if (pollfd
== data_sock
->fd
) {
883 newsock
= data_sock
->ops
->accept(data_sock
);
884 DBG("Relay data connection accepted, socket %d",
887 assert(pollfd
== control_sock
->fd
);
888 type
= RELAY_CONTROL
;
889 newsock
= control_sock
->ops
->accept(control_sock
);
890 DBG("Relay control connection accepted, socket %d",
894 PERROR("accepting sock");
898 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
901 PERROR("setsockopt inet");
902 lttcomm_destroy_sock(newsock
);
906 ret
= socket_apply_keep_alive_config(newsock
->fd
);
908 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
910 lttcomm_destroy_sock(newsock
);
914 new_conn
= connection_create(newsock
, type
);
916 lttcomm_destroy_sock(newsock
);
920 /* Enqueue request for the dispatcher thread. */
921 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
925 * Wake the dispatch queue futex.
926 * Implicit memory barrier with the
927 * exchange in cds_wfcq_enqueue.
929 futex_nto1_wake(&relay_conn_queue
.futex
);
930 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
931 ERR("socket poll error");
934 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
944 lttng_poll_clean(&events
);
946 if (data_sock
->fd
>= 0) {
947 ret
= data_sock
->ops
->close(data_sock
);
952 lttcomm_destroy_sock(data_sock
);
954 if (control_sock
->fd
>= 0) {
955 ret
= control_sock
->ops
->close(control_sock
);
960 lttcomm_destroy_sock(control_sock
);
964 ERR("Health error occurred in %s", __func__
);
966 health_unregister(health_relayd
);
967 DBG("Relay listener thread cleanup complete");
968 lttng_relay_stop_threads();
973 * This thread manages the dispatching of the requests to worker threads
975 static void *relay_thread_dispatcher(void *data
)
979 struct cds_wfcq_node
*node
;
980 struct relay_connection
*new_conn
= NULL
;
982 DBG("[thread] Relay dispatcher started");
984 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
986 if (testpoint(relayd_thread_dispatcher
)) {
987 goto error_testpoint
;
990 health_code_update();
993 health_code_update();
995 /* Atomically prepare the queue futex */
996 futex_nto1_prepare(&relay_conn_queue
.futex
);
998 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1003 health_code_update();
1005 /* Dequeue commands */
1006 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1007 &relay_conn_queue
.tail
);
1009 DBG("Woken up but nothing in the relay command queue");
1010 /* Continue thread execution */
1013 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1015 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1018 * Inform worker thread of the new request. This
1019 * call is blocking so we can be assured that
1020 * the data will be read at some point in time
1021 * or wait to the end of the world :)
1023 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1025 PERROR("write connection pipe");
1026 connection_put(new_conn
);
1029 } while (node
!= NULL
);
1031 /* Futex wait on queue. Blocking call on futex() */
1032 health_poll_entry();
1033 futex_nto1_wait(&relay_conn_queue
.futex
);
1037 /* Normal exit, no error */
1044 ERR("Health error occurred in %s", __func__
);
1046 health_unregister(health_relayd
);
1047 DBG("Dispatch thread dying");
1048 lttng_relay_stop_threads();
1053 * Set index data from the control port to a given index object.
1055 static int set_index_control_data(struct relay_index
*index
,
1056 struct lttcomm_relayd_index
*data
,
1057 struct relay_connection
*conn
)
1059 struct ctf_packet_index index_data
;
1062 * The index on disk is encoded in big endian, so we don't need
1063 * to convert the data received on the network. The data_offset
1064 * value is NEVER modified here and is updated by the data
1067 index_data
.packet_size
= data
->packet_size
;
1068 index_data
.content_size
= data
->content_size
;
1069 index_data
.timestamp_begin
= data
->timestamp_begin
;
1070 index_data
.timestamp_end
= data
->timestamp_end
;
1071 index_data
.events_discarded
= data
->events_discarded
;
1072 index_data
.stream_id
= data
->stream_id
;
1074 if (conn
->minor
>= 8) {
1075 index
->index_data
.stream_instance_id
= data
->stream_instance_id
;
1076 index
->index_data
.packet_seq_num
= data
->packet_seq_num
;
1079 return relay_index_set_data(index
, &index_data
);
1083 * Handle the RELAYD_CREATE_SESSION command.
1085 * On success, send back the session id or else return a negative value.
1087 static int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1088 struct relay_connection
*conn
)
1090 int ret
= 0, send_ret
;
1091 struct relay_session
*session
;
1092 struct lttcomm_relayd_status_session reply
;
1093 char session_name
[LTTNG_NAME_MAX
];
1094 char hostname
[LTTNG_HOST_NAME_MAX
];
1095 uint32_t live_timer
= 0;
1096 bool snapshot
= false;
1098 memset(session_name
, 0, LTTNG_NAME_MAX
);
1099 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1101 memset(&reply
, 0, sizeof(reply
));
1103 switch (conn
->minor
) {
1108 case 4: /* LTTng sessiond 2.4 */
1110 ret
= cmd_create_session_2_4(conn
, session_name
,
1111 hostname
, &live_timer
, &snapshot
);
1117 session
= session_create(session_name
, hostname
, live_timer
,
1118 snapshot
, conn
->major
, conn
->minor
);
1123 assert(!conn
->session
);
1124 conn
->session
= session
;
1125 DBG("Created session %" PRIu64
, session
->id
);
1127 reply
.session_id
= htobe64(session
->id
);
1131 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1133 reply
.ret_code
= htobe32(LTTNG_OK
);
1136 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1138 ERR("Relayd sending session id");
1146 * When we have received all the streams and the metadata for a channel,
1147 * we make them visible to the viewer threads.
1149 static void publish_connection_local_streams(struct relay_connection
*conn
)
1151 struct relay_stream
*stream
;
1152 struct relay_session
*session
= conn
->session
;
1155 * We publish all streams belonging to a session atomically wrt
1158 pthread_mutex_lock(&session
->lock
);
1160 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1162 stream_publish(stream
);
1167 * Inform the viewer that there are new streams in the session.
1169 if (session
->viewer_attached
) {
1170 uatomic_set(&session
->new_streams
, 1);
1172 pthread_mutex_unlock(&session
->lock
);
1176 * relay_add_stream: allocate a new stream for a session
1178 static int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1179 struct relay_connection
*conn
)
1183 struct relay_session
*session
= conn
->session
;
1184 struct relay_stream
*stream
= NULL
;
1185 struct lttcomm_relayd_status_stream reply
;
1186 struct ctf_trace
*trace
= NULL
;
1187 uint64_t stream_handle
= -1ULL;
1188 char *path_name
= NULL
, *channel_name
= NULL
;
1189 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1191 if (!session
|| conn
->version_check_done
== 0) {
1192 ERR("Trying to add a stream before version check");
1194 goto end_no_session
;
1197 switch (session
->minor
) {
1198 case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */
1199 ret
= cmd_recv_stream_2_1(conn
, &path_name
,
1202 case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
1204 ret
= cmd_recv_stream_2_2(conn
, &path_name
,
1205 &channel_name
, &tracefile_size
, &tracefile_count
);
1212 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1216 /* This stream here has one reference on the trace. */
1218 pthread_mutex_lock(&last_relay_stream_id_lock
);
1219 stream_handle
= ++last_relay_stream_id
;
1220 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1222 /* We pass ownership of path_name and channel_name. */
1223 stream
= stream_create(trace
, stream_handle
, path_name
,
1224 channel_name
, tracefile_size
, tracefile_count
);
1226 channel_name
= NULL
;
1229 * Streams are the owners of their trace. Reference to trace is
1230 * kept within stream_create().
1232 ctf_trace_put(trace
);
1235 memset(&reply
, 0, sizeof(reply
));
1236 reply
.handle
= htobe64(stream_handle
);
1238 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1240 reply
.ret_code
= htobe32(LTTNG_OK
);
1243 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1244 sizeof(struct lttcomm_relayd_status_stream
), 0);
1246 ERR("Relay sending stream id");
1247 ret
= (int) send_ret
;
1257 * relay_close_stream: close a specific stream
1259 static int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1260 struct relay_connection
*conn
)
1263 struct relay_session
*session
= conn
->session
;
1264 struct lttcomm_relayd_close_stream stream_info
;
1265 struct lttcomm_relayd_generic_reply reply
;
1266 struct relay_stream
*stream
;
1268 DBG("Close stream received");
1270 if (!session
|| conn
->version_check_done
== 0) {
1271 ERR("Trying to close a stream before version check");
1273 goto end_no_session
;
1276 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1277 sizeof(struct lttcomm_relayd_close_stream
), 0);
1278 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1280 /* Orderly shutdown. Not necessary to print an error. */
1281 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1283 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1286 goto end_no_session
;
1289 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1296 * Set last_net_seq_num before the close flag. Required by data
1299 pthread_mutex_lock(&stream
->lock
);
1300 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1301 pthread_mutex_unlock(&stream
->lock
);
1304 * This is one of the conditions which may trigger a stream close
1305 * with the others being:
1306 * 1) A close command is received for a stream
1307 * 2) The control connection owning the stream is closed
1308 * 3) We have received all of the stream's data _after_ a close
1311 try_stream_close(stream
);
1312 if (stream
->is_metadata
) {
1313 struct relay_viewer_stream
*vstream
;
1315 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1317 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1319 * Since all the metadata has been sent to the
1320 * viewer and that we have a request to close
1321 * its stream, we can safely teardown the
1322 * corresponding metadata viewer stream.
1324 viewer_stream_put(vstream
);
1326 /* Put local reference. */
1327 viewer_stream_put(vstream
);
1333 memset(&reply
, 0, sizeof(reply
));
1335 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1337 reply
.ret_code
= htobe32(LTTNG_OK
);
1339 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1340 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1342 ERR("Relay sending stream id");
1351 * relay_reset_metadata: reset a metadata stream
1354 int relay_reset_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1355 struct relay_connection
*conn
)
1358 struct relay_session
*session
= conn
->session
;
1359 struct lttcomm_relayd_reset_metadata stream_info
;
1360 struct lttcomm_relayd_generic_reply reply
;
1361 struct relay_stream
*stream
;
1363 DBG("Reset metadata received");
1365 if (!session
|| conn
->version_check_done
== 0) {
1366 ERR("Trying to reset a metadata stream before version check");
1368 goto end_no_session
;
1371 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1372 sizeof(struct lttcomm_relayd_reset_metadata
), 0);
1373 if (ret
< sizeof(struct lttcomm_relayd_reset_metadata
)) {
1375 /* Orderly shutdown. Not necessary to print an error. */
1376 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1378 ERR("Relay didn't receive valid reset_metadata struct "
1382 goto end_no_session
;
1384 DBG("Update metadata to version %" PRIu64
, be64toh(stream_info
.version
));
1386 /* Unsupported for live sessions for now. */
1387 if (session
->live_timer
!= 0) {
1392 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1397 pthread_mutex_lock(&stream
->lock
);
1398 if (!stream
->is_metadata
) {
1403 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1404 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1405 &stream
->stream_fd
->fd
);
1407 ERR("Failed to rotate metadata file %s of channel %s",
1408 stream
->path_name
, stream
->channel_name
);
1413 pthread_mutex_unlock(&stream
->lock
);
1417 memset(&reply
, 0, sizeof(reply
));
1419 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1421 reply
.ret_code
= htobe32(LTTNG_OK
);
1423 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1424 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1426 ERR("Relay sending reset metadata reply");
1435 * relay_unknown_command: send -1 if received unknown command
1437 static void relay_unknown_command(struct relay_connection
*conn
)
1439 struct lttcomm_relayd_generic_reply reply
;
1442 memset(&reply
, 0, sizeof(reply
));
1443 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1444 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1445 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1447 ERR("Relay sending unknown command");
1452 * relay_start: send an acknowledgment to the client to tell if we are
1453 * ready to receive data. We are ready if a session is established.
1455 static int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1456 struct relay_connection
*conn
)
1458 int ret
= htobe32(LTTNG_OK
);
1459 struct lttcomm_relayd_generic_reply reply
;
1460 struct relay_session
*session
= conn
->session
;
1463 DBG("Trying to start the streaming without a session established");
1464 ret
= htobe32(LTTNG_ERR_UNK
);
1467 memset(&reply
, 0, sizeof(reply
));
1468 reply
.ret_code
= ret
;
1469 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1470 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1472 ERR("Relay sending start ack");
1479 * Append padding to the file pointed by the file descriptor fd.
1481 static int write_padding_to_file(int fd
, uint32_t size
)
1490 zeros
= zmalloc(size
);
1491 if (zeros
== NULL
) {
1492 PERROR("zmalloc zeros for padding");
1497 ret
= lttng_write(fd
, zeros
, size
);
1499 PERROR("write padding to file");
1509 * Close the current index file if it is open, and create a new one.
1511 * Return 0 on success, -1 on error.
1514 int create_rotate_index_file(struct relay_stream
*stream
)
1517 uint32_t major
, minor
;
1519 /* Put ref on previous index_file. */
1520 if (stream
->index_file
) {
1521 lttng_index_file_put(stream
->index_file
);
1522 stream
->index_file
= NULL
;
1524 major
= stream
->trace
->session
->major
;
1525 minor
= stream
->trace
->session
->minor
;
1526 stream
->index_file
= lttng_index_file_create(stream
->path_name
,
1527 stream
->channel_name
,
1528 -1, -1, stream
->tracefile_size
,
1529 tracefile_array_get_file_index_head(stream
->tfa
),
1530 lttng_to_index_major(major
, minor
),
1531 lttng_to_index_minor(major
, minor
));
1532 if (!stream
->index_file
) {
1544 int do_rotate_stream(struct relay_stream
*stream
)
1548 /* Perform the stream rotation. */
1549 ret
= utils_rotate_stream_file(stream
->path_name
,
1550 stream
->channel_name
, stream
->tracefile_size
,
1551 stream
->tracefile_count
, -1,
1552 -1, stream
->stream_fd
->fd
,
1553 NULL
, &stream
->stream_fd
->fd
);
1555 ERR("Rotating stream output file");
1558 stream
->tracefile_size_current
= 0;
1560 /* Rotate also the index if the stream is not a metadata stream. */
1561 if (!stream
->is_metadata
) {
1562 ret
= create_rotate_index_file(stream
);
1564 ERR("Failed to rotate index file");
1569 stream
->rotate_at_seq_num
= -1ULL;
1570 stream
->pos_after_last_complete_data_index
= 0;
1577 * If too much data has been written in a tracefile before we received the
1578 * rotation command, we have to move the excess data to the new tracefile and
1579 * perform the rotation. This can happen because the control and data
1580 * connections are separate, the indexes as well as the commands arrive from
1581 * the control connection and we have no control over the order so we could be
1582 * in a situation where too much data has been received on the data connection
1583 * before the rotation command on the control connection arrives. We don't need
1584 * to update the index because its order is guaranteed with the rotation
1588 int rotate_truncate_stream(struct relay_stream
*stream
)
1591 uint64_t diff
, pos
= 0;
1592 char buf
[FILE_COPY_BUFFER_SIZE
];
1594 assert(!stream
->is_metadata
);
1596 assert(stream
->tracefile_size_current
>
1597 stream
->pos_after_last_complete_data_index
);
1598 diff
= stream
->tracefile_size_current
-
1599 stream
->pos_after_last_complete_data_index
;
1601 /* Create the new tracefile. */
1602 new_fd
= utils_create_stream_file(stream
->path_name
,
1603 stream
->channel_name
,
1604 stream
->tracefile_size
, stream
->tracefile_count
,
1605 /* uid */ -1, /* gid */ -1, /* suffix */ NULL
);
1607 ERR("Failed to create new stream file at path %s for channel %s",
1608 stream
->path_name
, stream
->channel_name
);
1614 * Rewind the current tracefile to the position at which the rotation
1615 * should have occured.
1617 ret
= lseek(stream
->stream_fd
->fd
,
1618 stream
->pos_after_last_complete_data_index
, SEEK_SET
);
1620 PERROR("seek truncate stream");
1624 /* Move data from the old file to the new file. */
1625 while (pos
< diff
) {
1626 uint64_t count
, bytes_left
;
1629 bytes_left
= diff
- pos
;
1630 count
= bytes_left
> sizeof(buf
) ? sizeof(buf
) : bytes_left
;
1631 assert(count
<= SIZE_MAX
);
1633 io_ret
= lttng_read(stream
->stream_fd
->fd
, buf
, count
);
1634 if (io_ret
< (ssize_t
) count
) {
1635 char error_string
[256];
1637 snprintf(error_string
, sizeof(error_string
),
1638 "Failed to read %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1639 count
, stream
->stream_fd
->fd
, io_ret
);
1641 PERROR("%s", error_string
);
1643 ERR("%s", error_string
);
1649 io_ret
= lttng_write(new_fd
, buf
, count
);
1650 if (io_ret
< (ssize_t
) count
) {
1651 char error_string
[256];
1653 snprintf(error_string
, sizeof(error_string
),
1654 "Failed to write %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1655 count
, new_fd
, io_ret
);
1657 PERROR("%s", error_string
);
1659 ERR("%s", error_string
);
1668 /* Truncate the file to get rid of the excess data. */
1669 ret
= ftruncate(stream
->stream_fd
->fd
,
1670 stream
->pos_after_last_complete_data_index
);
1672 PERROR("ftruncate");
1676 ret
= close(stream
->stream_fd
->fd
);
1678 PERROR("Closing tracefile");
1682 ret
= create_rotate_index_file(stream
);
1684 ERR("Rotate stream index file");
1689 * Update the offset and FD of all the eventual indexes created by the
1690 * data connection before the rotation command arrived.
1692 ret
= relay_index_switch_all_files(stream
);
1694 ERR("Failed to rotate index file");
1698 stream
->stream_fd
->fd
= new_fd
;
1699 stream
->tracefile_size_current
= diff
;
1700 stream
->pos_after_last_complete_data_index
= 0;
1701 stream
->rotate_at_seq_num
= -1ULL;
1710 * Check if a stream should perform a rotation (for session rotation).
1711 * Must be called with the stream lock held.
1713 * Return 0 on success, a negative value on error.
1716 int try_rotate_stream(struct relay_stream
*stream
)
1720 /* No rotation expected. */
1721 if (stream
->rotate_at_seq_num
== -1ULL) {
1725 if (stream
->prev_seq
< stream
->rotate_at_seq_num
) {
1726 DBG("Stream %" PRIu64
" no yet ready for rotation",
1727 stream
->stream_handle
);
1729 } else if (stream
->prev_seq
> stream
->rotate_at_seq_num
) {
1730 DBG("Rotation after too much data has been written in tracefile "
1731 "for stream %" PRIu64
", need to truncate before "
1732 "rotating", stream
->stream_handle
);
1733 ret
= rotate_truncate_stream(stream
);
1735 ERR("Failed to truncate stream");
1739 /* stream->prev_seq == stream->rotate_at_seq_num */
1740 DBG("Stream %" PRIu64
" ready for rotation",
1741 stream
->stream_handle
);
1742 ret
= do_rotate_stream(stream
);
1750 * relay_recv_metadata: receive the metadata for the session.
1752 static int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1753 struct relay_connection
*conn
)
1757 struct relay_session
*session
= conn
->session
;
1758 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1759 struct relay_stream
*metadata_stream
;
1760 uint64_t data_size
, payload_size
;
1763 ERR("Metadata sent before version check");
1768 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1769 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1770 ERR("Incorrect data size");
1774 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1776 if (data_buffer_size
< data_size
) {
1777 /* In case the realloc fails, we can free the memory */
1780 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1781 if (!tmp_data_ptr
) {
1782 ERR("Allocating data buffer");
1787 data_buffer
= tmp_data_ptr
;
1788 data_buffer_size
= data_size
;
1790 memset(data_buffer
, 0, data_size
);
1791 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1792 size_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1793 if (size_ret
< 0 || size_ret
!= data_size
) {
1794 if (size_ret
== 0) {
1795 /* Orderly shutdown. Not necessary to print an error. */
1796 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1798 ERR("Relay didn't receive the whole metadata");
1803 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1805 metadata_stream
= stream_get_by_id(be64toh(metadata_struct
->stream_id
));
1806 if (!metadata_stream
) {
1811 pthread_mutex_lock(&metadata_stream
->lock
);
1813 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
, metadata_struct
->payload
,
1815 if (size_ret
< payload_size
) {
1816 ERR("Relay error writing metadata on file");
1821 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1822 be32toh(metadata_struct
->padding_size
));
1827 metadata_stream
->metadata_received
+=
1828 payload_size
+ be32toh(metadata_struct
->padding_size
);
1829 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1830 metadata_stream
->metadata_received
);
1832 ret
= try_rotate_stream(metadata_stream
);
1838 pthread_mutex_unlock(&metadata_stream
->lock
);
1839 stream_put(metadata_stream
);
1845 * relay_send_version: send relayd version number
1847 static int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1848 struct relay_connection
*conn
)
1851 struct lttcomm_relayd_version reply
, msg
;
1852 bool compatible
= true;
1854 conn
->version_check_done
= 1;
1856 /* Get version from the other side. */
1857 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1858 if (ret
< 0 || ret
!= sizeof(msg
)) {
1860 /* Orderly shutdown. Not necessary to print an error. */
1861 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1863 ERR("Relay failed to receive the version values.");
1869 memset(&reply
, 0, sizeof(reply
));
1870 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1871 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1873 /* Major versions must be the same */
1874 if (reply
.major
!= be32toh(msg
.major
)) {
1875 DBG("Incompatible major versions (%u vs %u), deleting session",
1876 reply
.major
, be32toh(msg
.major
));
1880 conn
->major
= reply
.major
;
1881 /* We adapt to the lowest compatible version */
1882 if (reply
.minor
<= be32toh(msg
.minor
)) {
1883 conn
->minor
= reply
.minor
;
1885 conn
->minor
= be32toh(msg
.minor
);
1888 reply
.major
= htobe32(reply
.major
);
1889 reply
.minor
= htobe32(reply
.minor
);
1890 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1891 sizeof(struct lttcomm_relayd_version
), 0);
1893 ERR("Relay sending version");
1901 DBG("Version check done using protocol %u.%u", conn
->major
,
1909 * Check for data pending for a given stream id from the session daemon.
1911 static int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1912 struct relay_connection
*conn
)
1914 struct relay_session
*session
= conn
->session
;
1915 struct lttcomm_relayd_data_pending msg
;
1916 struct lttcomm_relayd_generic_reply reply
;
1917 struct relay_stream
*stream
;
1919 uint64_t last_net_seq_num
, stream_id
;
1921 DBG("Data pending command received");
1923 if (!session
|| conn
->version_check_done
== 0) {
1924 ERR("Trying to check for data before version check");
1926 goto end_no_session
;
1929 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1930 if (ret
< sizeof(msg
)) {
1932 /* Orderly shutdown. Not necessary to print an error. */
1933 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1935 ERR("Relay didn't receive valid data_pending struct size : %d",
1939 goto end_no_session
;
1942 stream_id
= be64toh(msg
.stream_id
);
1943 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1945 stream
= stream_get_by_id(stream_id
);
1946 if (stream
== NULL
) {
1951 pthread_mutex_lock(&stream
->lock
);
1953 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1954 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1957 /* Avoid wrapping issue */
1958 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1959 /* Data has in fact been written and is NOT pending */
1962 /* Data still being streamed thus pending */
1966 stream
->data_pending_check_done
= true;
1967 pthread_mutex_unlock(&stream
->lock
);
1972 memset(&reply
, 0, sizeof(reply
));
1973 reply
.ret_code
= htobe32(ret
);
1974 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1976 ERR("Relay data pending ret code failed");
1984 * Wait for the control socket to reach a quiescent state.
1986 * Note that for now, when receiving this command from the session
1987 * daemon, this means that every subsequent commands or data received on
1988 * the control socket has been handled. So, this is why we simply return
1991 static int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1992 struct relay_connection
*conn
)
1996 struct relay_stream
*stream
;
1997 struct lttcomm_relayd_quiescent_control msg
;
1998 struct lttcomm_relayd_generic_reply reply
;
2000 DBG("Checking quiescent state on control socket");
2002 if (!conn
->session
|| conn
->version_check_done
== 0) {
2003 ERR("Trying to check for data before version check");
2005 goto end_no_session
;
2008 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
2009 if (ret
< sizeof(msg
)) {
2011 /* Orderly shutdown. Not necessary to print an error. */
2012 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2014 ERR("Relay didn't receive valid begin data_pending struct size: %d",
2018 goto end_no_session
;
2021 stream_id
= be64toh(msg
.stream_id
);
2022 stream
= stream_get_by_id(stream_id
);
2026 pthread_mutex_lock(&stream
->lock
);
2027 stream
->data_pending_check_done
= true;
2028 pthread_mutex_unlock(&stream
->lock
);
2029 DBG("Relay quiescent control pending flag set to %" PRIu64
, stream_id
);
2032 memset(&reply
, 0, sizeof(reply
));
2033 reply
.ret_code
= htobe32(LTTNG_OK
);
2034 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2036 ERR("Relay data quiescent control ret code failed");
2044 * Initialize a data pending command. This means that a consumer is about
2045 * to ask for data pending for each stream it holds. Simply iterate over
2046 * all streams of a session and set the data_pending_check_done flag.
2048 * This command returns to the client a LTTNG_OK code.
2050 static int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
2051 struct relay_connection
*conn
)
2054 struct lttng_ht_iter iter
;
2055 struct lttcomm_relayd_begin_data_pending msg
;
2056 struct lttcomm_relayd_generic_reply reply
;
2057 struct relay_stream
*stream
;
2058 uint64_t session_id
;
2063 DBG("Init streams for data pending");
2065 if (!conn
->session
|| conn
->version_check_done
== 0) {
2066 ERR("Trying to check for data before version check");
2068 goto end_no_session
;
2071 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
2072 if (ret
< sizeof(msg
)) {
2074 /* Orderly shutdown. Not necessary to print an error. */
2075 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2077 ERR("Relay didn't receive valid begin data_pending struct size: %d",
2081 goto end_no_session
;
2084 session_id
= be64toh(msg
.session_id
);
2087 * Iterate over all streams to set the begin data pending flag.
2088 * For now, the streams are indexed by stream handle so we have
2089 * to iterate over all streams to find the one associated with
2090 * the right session_id.
2093 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2095 if (!stream_get(stream
)) {
2098 if (stream
->trace
->session
->id
== session_id
) {
2099 pthread_mutex_lock(&stream
->lock
);
2100 stream
->data_pending_check_done
= false;
2101 pthread_mutex_unlock(&stream
->lock
);
2102 DBG("Set begin data pending flag to stream %" PRIu64
,
2103 stream
->stream_handle
);
2109 memset(&reply
, 0, sizeof(reply
));
2110 /* All good, send back reply. */
2111 reply
.ret_code
= htobe32(LTTNG_OK
);
2113 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2115 ERR("Relay begin data pending send reply failed");
2123 * End data pending command. This will check, for a given session id, if
2124 * each stream associated with it has its data_pending_check_done flag
2125 * set. If not, this means that the client lost track of the stream but
2126 * the data is still being streamed on our side. In this case, we inform
2127 * the client that data is in flight.
2129 * Return to the client if there is data in flight or not with a ret_code.
2131 static int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
2132 struct relay_connection
*conn
)
2135 struct lttng_ht_iter iter
;
2136 struct lttcomm_relayd_end_data_pending msg
;
2137 struct lttcomm_relayd_generic_reply reply
;
2138 struct relay_stream
*stream
;
2139 uint64_t session_id
;
2140 uint32_t is_data_inflight
= 0;
2142 DBG("End data pending command");
2144 if (!conn
->session
|| conn
->version_check_done
== 0) {
2145 ERR("Trying to check for data before version check");
2147 goto end_no_session
;
2150 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
2151 if (ret
< sizeof(msg
)) {
2153 /* Orderly shutdown. Not necessary to print an error. */
2154 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2156 ERR("Relay didn't receive valid end data_pending struct size: %d",
2160 goto end_no_session
;
2163 session_id
= be64toh(msg
.session_id
);
2166 * Iterate over all streams to see if the begin data pending
2170 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2172 if (!stream_get(stream
)) {
2175 if (stream
->trace
->session
->id
!= session_id
) {
2179 pthread_mutex_lock(&stream
->lock
);
2180 if (!stream
->data_pending_check_done
) {
2181 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
2182 is_data_inflight
= 1;
2183 DBG("Data is still in flight for stream %" PRIu64
,
2184 stream
->stream_handle
);
2185 pthread_mutex_unlock(&stream
->lock
);
2190 pthread_mutex_unlock(&stream
->lock
);
2195 memset(&reply
, 0, sizeof(reply
));
2196 /* All good, send back reply. */
2197 reply
.ret_code
= htobe32(is_data_inflight
);
2199 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2201 ERR("Relay end data pending send reply failed");
2209 * Receive an index for a specific stream.
2211 * Return 0 on success else a negative value.
2213 static int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
2214 struct relay_connection
*conn
)
2217 struct relay_session
*session
= conn
->session
;
2218 struct lttcomm_relayd_index index_info
;
2219 struct relay_index
*index
;
2220 struct lttcomm_relayd_generic_reply reply
;
2221 struct relay_stream
*stream
;
2222 uint64_t net_seq_num
;
2227 DBG("Relay receiving index");
2229 if (!session
|| conn
->version_check_done
== 0) {
2230 ERR("Trying to close a stream before version check");
2232 goto end_no_session
;
2235 msg_len
= lttcomm_relayd_index_len(
2236 lttng_to_index_major(conn
->major
, conn
->minor
),
2237 lttng_to_index_minor(conn
->major
, conn
->minor
));
2238 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
2240 if (ret
< msg_len
) {
2242 /* Orderly shutdown. Not necessary to print an error. */
2243 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2245 ERR("Relay didn't receive valid index struct size : %d", ret
);
2248 goto end_no_session
;
2251 net_seq_num
= be64toh(index_info
.net_seq_num
);
2253 stream
= stream_get_by_id(be64toh(index_info
.relay_stream_id
));
2255 ERR("stream_get_by_id not found");
2259 pthread_mutex_lock(&stream
->lock
);
2261 /* Live beacon handling */
2262 if (index_info
.packet_size
== 0) {
2263 DBG("Received live beacon for stream %" PRIu64
,
2264 stream
->stream_handle
);
2267 * Only flag a stream inactive when it has already
2268 * received data and no indexes are in flight.
2270 if (stream
->index_received_seqcount
> 0
2271 && stream
->indexes_in_flight
== 0) {
2272 stream
->beacon_ts_end
=
2273 be64toh(index_info
.timestamp_end
);
2276 goto end_stream_put
;
2278 stream
->beacon_ts_end
= -1ULL;
2281 if (stream
->ctf_stream_id
== -1ULL) {
2282 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
2284 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
2287 ERR("relay_index_get_by_id_or_create index NULL");
2288 goto end_stream_put
;
2290 if (set_index_control_data(index
, &index_info
, conn
)) {
2291 ERR("set_index_control_data error");
2292 relay_index_put(index
);
2294 goto end_stream_put
;
2296 ret
= relay_index_try_flush(index
);
2298 tracefile_array_commit_seq(stream
->tfa
);
2299 stream
->index_received_seqcount
++;
2300 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
2301 } else if (ret
> 0) {
2305 ERR("relay_index_try_flush error %d", ret
);
2306 relay_index_put(index
);
2311 pthread_mutex_unlock(&stream
->lock
);
2316 memset(&reply
, 0, sizeof(reply
));
2318 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2320 reply
.ret_code
= htobe32(LTTNG_OK
);
2322 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2324 ERR("Relay sending close index id reply");
2333 * Receive the streams_sent message.
2335 * Return 0 on success else a negative value.
2337 static int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2338 struct relay_connection
*conn
)
2341 struct lttcomm_relayd_generic_reply reply
;
2345 DBG("Relay receiving streams_sent");
2347 if (!conn
->session
|| conn
->version_check_done
== 0) {
2348 ERR("Trying to close a stream before version check");
2350 goto end_no_session
;
2354 * Publish every pending stream in the connection recv list which are
2355 * now ready to be used by the viewer.
2357 publish_connection_local_streams(conn
);
2359 memset(&reply
, 0, sizeof(reply
));
2360 reply
.ret_code
= htobe32(LTTNG_OK
);
2361 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2363 ERR("Relay sending sent_stream reply");
2375 * relay_rotate_stream: rotate a stream to a new tracefile for the session
2376 * rotation feature (not the tracefile rotation feature).
2378 static int relay_rotate_session_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
2379 struct relay_connection
*conn
)
2382 struct relay_session
*session
= conn
->session
;
2383 struct lttcomm_relayd_rotate_stream stream_info
;
2384 struct lttcomm_relayd_generic_reply reply
;
2385 struct relay_stream
*stream
;
2387 char *new_pathname
= NULL
;
2389 DBG("Rotate stream received");
2391 if (!session
|| !conn
->version_check_done
) {
2392 ERR("Trying to rotate a stream before version check");
2397 if (session
->major
== 2 && session
->minor
< 11) {
2398 ERR("Unsupported feature before 2.11");
2403 memset(&stream_info
, 0, sizeof(struct lttcomm_relayd_rotate_stream
));
2406 * Receive the struct up to the new_pathname member since we don't know
2409 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
2410 sizeof(struct lttcomm_relayd_rotate_stream
), 0);
2411 if (ret
< sizeof(struct lttcomm_relayd_rotate_stream
)) {
2413 /* Orderly shutdown. Not necessary to print an error. */
2414 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2416 ERR("Relay didn't receive valid rotate_stream struct size : %d", ret
);
2422 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
2428 len
= be32toh(stream_info
.pathname_length
);
2429 /* Ensure it fits in local filename length. */
2430 if (len
>= LTTNG_PATH_MAX
) {
2431 ret
= -ENAMETOOLONG
;
2432 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2433 len
, LTTNG_PATH_MAX
);
2437 new_pathname
= zmalloc(len
);
2438 if (!new_pathname
) {
2439 PERROR("Failed to allocation new path name of relay_rotate_session_stream command");
2444 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, new_pathname
, len
, 0);
2447 /* Orderly shutdown. Not necessary to print an error. */
2448 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2450 ERR("Relay didn't receive valid rotate_stream struct size : %d", ret
);
2456 pthread_mutex_lock(&stream
->lock
);
2459 * Update the trace path (just the folder, the stream name does not
2462 free(stream
->path_name
);
2463 stream
->path_name
= create_output_path(new_pathname
);
2464 if (!stream
->path_name
) {
2465 ERR("Failed to create a new output path");
2466 goto end_stream_unlock
;
2468 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
,
2471 ERR("relay creating output directory");
2472 goto end_stream_unlock
;
2474 stream
->chunk_id
= be64toh(stream_info
.new_chunk_id
);
2476 if (stream
->is_metadata
) {
2478 * The metadata stream is sent only over the control connection
2479 * so we know we have all the data to perform the stream
2482 ret
= do_rotate_stream(stream
);
2484 stream
->rotate_at_seq_num
= be64toh(stream_info
.rotate_at_seq_num
);
2485 ret
= try_rotate_stream(stream
);
2488 goto end_stream_unlock
;
2492 pthread_mutex_unlock(&stream
->lock
);
2495 memset(&reply
, 0, sizeof(reply
));
2497 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2499 reply
.ret_code
= htobe32(LTTNG_OK
);
2501 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2502 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2504 ERR("Failed to send reply of rotate session stream command");
2514 * relay_mkdir: Create a folder on the disk.
2516 static int relay_mkdir(struct lttcomm_relayd_hdr
*recv_hdr
,
2517 struct relay_connection
*conn
)
2520 ssize_t network_ret
;
2521 struct relay_session
*session
= conn
->session
;
2522 struct lttcomm_relayd_mkdir path_info_header
;
2523 struct lttcomm_relayd_mkdir
*path_info
= NULL
;
2524 struct lttcomm_relayd_generic_reply reply
;
2527 if (!session
|| !conn
->version_check_done
) {
2528 ERR("Trying to create a directory before version check");
2530 goto end_no_session
;
2533 if (session
->major
== 2 && session
->minor
< 11) {
2535 * This client is not supposed to use this command since
2536 * it predates its introduction.
2538 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2540 goto end_no_session
;
2543 network_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &path_info_header
,
2544 sizeof(path_info_header
), 0);
2545 if (network_ret
< (ssize_t
) sizeof(path_info_header
)) {
2546 if (network_ret
== 0) {
2547 /* Orderly shutdown. Not necessary to print an error. */
2548 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2550 ERR("Reception of mkdir command argument length failed with ret = %zi, expected %zu",
2551 network_ret
, sizeof(path_info_header
));
2554 goto end_no_session
;
2557 path_info_header
.length
= be32toh(path_info_header
.length
);
2559 /* Ensure that it fits in local path length. */
2560 if (path_info_header
.length
>= LTTNG_PATH_MAX
) {
2561 ret
= -ENAMETOOLONG
;
2562 ERR("Path name argument of mkdir command (%" PRIu32
" bytes) exceeds the maximal length allowed (%d bytes)",
2563 path_info_header
.length
, LTTNG_PATH_MAX
);
2567 path_info
= zmalloc(sizeof(path_info_header
) + path_info_header
.length
);
2569 PERROR("zmalloc of mkdir command path");
2574 network_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, path_info
->path
,
2575 path_info_header
.length
, 0);
2576 if (network_ret
< (ssize_t
) path_info_header
.length
) {
2577 if (network_ret
== 0) {
2578 /* Orderly shutdown. Not necessary to print an error. */
2579 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2581 ERR("Reception of mkdir path argument failed with ret = %zi, expected %" PRIu32
,
2582 network_ret
, path_info_header
.length
);
2585 goto end_no_session
;
2588 path
= create_output_path(path_info
->path
);
2590 ERR("Failed to create output path");
2595 ret
= utils_mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, -1, -1);
2597 ERR("relay creating output directory");
2604 memset(&reply
, 0, sizeof(reply
));
2606 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2608 reply
.ret_code
= htobe32(LTTNG_OK
);
2610 network_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2611 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2612 if (network_ret
< (ssize_t
) sizeof(struct lttcomm_relayd_generic_reply
)) {
2613 ERR("Failed to send mkdir command status code with ret = %zi, expected %zu",
2615 sizeof(struct lttcomm_relayd_generic_reply
));
2625 static int validate_rotate_rename_path_length(const char *path_type
,
2626 uint32_t path_length
)
2630 if (path_length
> LTTNG_PATH_MAX
) {
2631 ret
= -ENAMETOOLONG
;
2632 ERR("rotate rename \"%s\" path name length (%" PRIu32
" bytes) exceeds the allowed size of %i bytes",
2633 path_type
, path_length
, LTTNG_PATH_MAX
);
2634 } else if (path_length
== 0) {
2636 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type
);
2642 * relay_rotate_rename: rename the trace folder after a rotation is
2643 * completed. We are not closing any fd here, just moving the folder, so it
2644 * works even if data is still in-flight.
2646 static int relay_rotate_rename(struct lttcomm_relayd_hdr
*recv_hdr
,
2647 struct relay_connection
*conn
)
2650 ssize_t network_ret
;
2651 struct relay_session
*session
= conn
->session
;
2652 struct lttcomm_relayd_generic_reply reply
;
2653 struct lttcomm_relayd_rotate_rename header
;
2654 char *received_paths
= NULL
;
2655 size_t received_paths_size
;
2656 const char *received_old_path
, *received_new_path
;
2657 char *complete_old_path
= NULL
, *complete_new_path
= NULL
;
2659 if (!session
|| !conn
->version_check_done
) {
2660 ERR("Trying to rename a trace folder before version check");
2665 if (session
->major
== 2 && session
->minor
< 11) {
2666 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2671 network_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &header
,
2673 if (network_ret
< (ssize_t
) sizeof(header
)) {
2674 if (network_ret
== 0) {
2675 /* Orderly shutdown. Not necessary to print an error. */
2676 DBG("Socket %d did an orderly shutdown",
2679 ERR("Relay didn't receive a valid rotate_rename command header: expected %zu bytes, recvmsg() returned %zi",
2680 sizeof(header
), network_ret
);
2686 header
.old_path_length
= be32toh(header
.old_path_length
);
2687 header
.new_path_length
= be32toh(header
.new_path_length
);
2688 received_paths_size
= header
.old_path_length
+ header
.new_path_length
;
2690 /* Ensure the paths don't exceed their allowed size. */
2691 ret
= validate_rotate_rename_path_length("old", header
.old_path_length
);
2695 ret
= validate_rotate_rename_path_length("new", header
.new_path_length
);
2700 received_paths
= zmalloc(received_paths_size
);
2701 if (!received_paths
) {
2702 PERROR("Could not allocate rotate commands paths reception buffer");
2707 network_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, received_paths
,
2708 received_paths_size
, 0);
2709 if (network_ret
< (ssize_t
) received_paths_size
) {
2710 if (network_ret
== 0) {
2711 /* Orderly shutdown. Not necessary to print an error. */
2712 DBG("Socket %d did an orderly shutdown",
2715 ERR("Relay failed to received rename command paths (%zu bytes): recvmsg() returned %zi",
2716 received_paths_size
, network_ret
);
2722 /* Validate that both paths received are NULL terminated. */
2723 if (received_paths
[header
.old_path_length
- 1] != '\0') {
2724 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2728 if (received_paths
[received_paths_size
- 1] != '\0') {
2729 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2734 received_old_path
= received_paths
;
2735 received_new_path
= received_paths
+ header
.old_path_length
;
2737 complete_old_path
= create_output_path(received_old_path
);
2738 if (!complete_old_path
) {
2739 ERR("Failed to build old output path in rotate_rename command");
2744 complete_new_path
= create_output_path(received_new_path
);
2745 if (!complete_new_path
) {
2746 ERR("Failed to build new output path in rotate_rename command");
2751 ret
= utils_mkdir_recursive(complete_new_path
, S_IRWXU
| S_IRWXG
,
2754 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2760 * If a domain has not yet created its channel, the domain-specific
2761 * folder might not exist, but this is not an error.
2763 ret
= rename(complete_old_path
, complete_new_path
);
2764 if (ret
< 0 && errno
!= ENOENT
) {
2765 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2766 complete_old_path
, complete_new_path
);
2772 memset(&reply
, 0, sizeof(reply
));
2774 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2776 reply
.ret_code
= htobe32(LTTNG_OK
);
2778 network_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2779 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2780 if (network_ret
< sizeof(struct lttcomm_relayd_generic_reply
)) {
2781 ERR("Relay sending stream id");
2786 free(received_paths
);
2787 free(complete_old_path
);
2788 free(complete_new_path
);
2793 * Check if all the streams in the session have completed the last rotation.
2794 * The chunk_id value is used to distinguish the cases where a stream was
2795 * closed on the consumerd before the rotation started but it still active on
2796 * the relayd, and the case where a stream appeared on the consumerd/relayd
2797 * after the last rotation started (in that case, it is already writing in the
2798 * new chunk folder).
2801 int relay_rotate_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
2802 struct relay_connection
*conn
)
2804 struct relay_session
*session
= conn
->session
;
2805 struct lttcomm_relayd_rotate_pending msg
;
2806 struct lttcomm_relayd_rotate_pending_reply reply
;
2807 struct lttng_ht_iter iter
;
2808 struct relay_stream
*stream
;
2810 ssize_t network_ret
;
2812 bool rotate_pending
= false;
2814 DBG("Rotate pending command received");
2816 if (!session
|| !conn
->version_check_done
) {
2817 ERR("Trying to check for data before version check");
2822 if (session
->major
== 2 && session
->minor
< 11) {
2823 ERR("Unsupported feature before 2.11");
2828 network_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
2829 if (network_ret
< (ssize_t
) sizeof(msg
)) {
2830 if (network_ret
== 0) {
2831 /* Orderly shutdown. Not necessary to print an error. */
2832 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2834 ERR("Relay didn't receive valid rotate_pending struct size : %zi",
2841 chunk_id
= be64toh(msg
.chunk_id
);
2842 DBG("Evaluating rotate pending for chunk id %" PRIu64
, chunk_id
);
2845 * Iterate over all the streams in the session and check if they are
2846 * still waiting for data to perform their rotation.
2849 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2851 if (!stream_get(stream
)) {
2854 if (stream
->trace
->session
!= session
) {
2858 pthread_mutex_lock(&stream
->lock
);
2859 if (stream
->rotate_at_seq_num
!= -1ULL) {
2860 /* We have not yet performed the rotation. */
2861 rotate_pending
= true;
2862 DBG("Stream %" PRIu64
" is still rotating",
2863 stream
->stream_handle
);
2864 } else if (stream
->chunk_id
< chunk_id
) {
2866 * Stream closed on the consumer but still active on the
2869 rotate_pending
= true;
2870 DBG("Stream %" PRIu64
" did not exist on the consumer "
2871 "when the last rotation started, but is"
2872 "still waiting for data before getting"
2874 stream
->stream_handle
);
2876 pthread_mutex_unlock(&stream
->lock
);
2878 if (rotate_pending
) {
2885 memset(&reply
, 0, sizeof(reply
));
2886 reply
.generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
);
2887 reply
.is_pending
= (uint8_t) !!rotate_pending
;
2888 network_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2890 if (network_ret
< (ssize_t
) sizeof(reply
)) {
2891 ERR("Relay rotate pending ret code failed");
2900 * Process the commands received on the control socket
2902 static int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2903 struct relay_connection
*conn
)
2907 switch (be32toh(recv_hdr
->cmd
)) {
2908 case RELAYD_CREATE_SESSION
:
2909 ret
= relay_create_session(recv_hdr
, conn
);
2911 case RELAYD_ADD_STREAM
:
2912 ret
= relay_add_stream(recv_hdr
, conn
);
2914 case RELAYD_START_DATA
:
2915 ret
= relay_start(recv_hdr
, conn
);
2917 case RELAYD_SEND_METADATA
:
2918 ret
= relay_recv_metadata(recv_hdr
, conn
);
2920 case RELAYD_VERSION
:
2921 ret
= relay_send_version(recv_hdr
, conn
);
2923 case RELAYD_CLOSE_STREAM
:
2924 ret
= relay_close_stream(recv_hdr
, conn
);
2926 case RELAYD_DATA_PENDING
:
2927 ret
= relay_data_pending(recv_hdr
, conn
);
2929 case RELAYD_QUIESCENT_CONTROL
:
2930 ret
= relay_quiescent_control(recv_hdr
, conn
);
2932 case RELAYD_BEGIN_DATA_PENDING
:
2933 ret
= relay_begin_data_pending(recv_hdr
, conn
);
2935 case RELAYD_END_DATA_PENDING
:
2936 ret
= relay_end_data_pending(recv_hdr
, conn
);
2938 case RELAYD_SEND_INDEX
:
2939 ret
= relay_recv_index(recv_hdr
, conn
);
2941 case RELAYD_STREAMS_SENT
:
2942 ret
= relay_streams_sent(recv_hdr
, conn
);
2944 case RELAYD_RESET_METADATA
:
2945 ret
= relay_reset_metadata(recv_hdr
, conn
);
2947 case RELAYD_ROTATE_STREAM
:
2948 ret
= relay_rotate_session_stream(recv_hdr
, conn
);
2950 case RELAYD_ROTATE_RENAME
:
2951 ret
= relay_rotate_rename(recv_hdr
, conn
);
2953 case RELAYD_ROTATE_PENDING
:
2954 ret
= relay_rotate_pending(recv_hdr
, conn
);
2957 ret
= relay_mkdir(recv_hdr
, conn
);
2959 case RELAYD_UPDATE_SYNC_INFO
:
2961 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2962 relay_unknown_command(conn
);
2972 * Handle index for a data stream.
2974 * Called with the stream lock held.
2976 * Return 0 on success else a negative value.
2978 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2979 int rotate_index
, bool *flushed
, uint64_t total_size
)
2982 uint64_t data_offset
;
2983 struct relay_index
*index
;
2985 /* Get data offset because we are about to update the index. */
2986 data_offset
= htobe64(stream
->tracefile_size_current
);
2988 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
2989 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
2992 * Lookup for an existing index for that stream id/sequence
2993 * number. If it exists, the control thread has already received the
2994 * data for it, thus we need to write it to disk.
2996 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
3002 if (rotate_index
|| !stream
->index_file
) {
3003 ret
= create_rotate_index_file(stream
);
3005 ERR("Failed to rotate index");
3006 /* Put self-ref for this index due to error. */
3007 relay_index_put(index
);
3013 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
3015 /* Put self-ref for this index due to error. */
3016 relay_index_put(index
);
3021 ret
= relay_index_try_flush(index
);
3023 tracefile_array_commit_seq(stream
->tfa
);
3024 stream
->index_received_seqcount
++;
3026 } else if (ret
> 0) {
3027 index
->total_size
= total_size
;
3031 /* Put self-ref for this index due to error. */
3032 relay_index_put(index
);
3041 * relay_process_data: Process the data received on the data socket
3043 static int relay_process_data(struct relay_connection
*conn
)
3045 int ret
= 0, rotate_index
= 0;
3047 struct relay_stream
*stream
;
3048 struct lttcomm_relayd_data_hdr data_hdr
;
3050 uint64_t net_seq_num
;
3052 struct relay_session
*session
;
3053 bool new_stream
= false, close_requested
= false;
3054 size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3055 size_t recv_off
= 0;
3056 char data_buffer
[chunk_size
];
3057 bool index_flushed
= false;
3059 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
3060 sizeof(struct lttcomm_relayd_data_hdr
), 0);
3063 /* Orderly shutdown. Not necessary to print an error. */
3064 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
3066 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3072 stream_id
= be64toh(data_hdr
.stream_id
);
3073 stream
= stream_get_by_id(stream_id
);
3075 ERR("relay_process_data: Cannot find stream %" PRIu64
, stream_id
);
3079 session
= stream
->trace
->session
;
3080 data_size
= be32toh(data_hdr
.data_size
);
3082 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
3084 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
3085 data_size
, stream_id
, net_seq_num
);
3087 pthread_mutex_lock(&stream
->lock
);
3089 /* Check if a rotation is needed. */
3090 if (stream
->tracefile_size
> 0 &&
3091 (stream
->tracefile_size_current
+ data_size
) >
3092 stream
->tracefile_size
) {
3093 uint64_t old_id
, new_id
;
3095 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
3096 tracefile_array_file_rotate(stream
->tfa
);
3098 /* new_id is updated by utils_rotate_stream_file. */
3101 ret
= utils_rotate_stream_file(stream
->path_name
,
3102 stream
->channel_name
, stream
->tracefile_size
,
3103 stream
->tracefile_count
, -1,
3104 -1, stream
->stream_fd
->fd
,
3105 &new_id
, &stream
->stream_fd
->fd
);
3107 ERR("Rotating stream output file");
3108 goto end_stream_unlock
;
3111 * Reset current size because we just performed a stream
3114 stream
->tracefile_size_current
= 0;
3119 * Index are handled in protocol version 2.4 and above. Also,
3120 * snapshot and index are NOT supported.
3122 if (session
->minor
>= 4 && !session
->snapshot
) {
3123 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
,
3125 data_size
+ be32toh(data_hdr
.padding_size
));
3127 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3128 stream
->stream_handle
, net_seq_num
, ret
);
3129 goto end_stream_unlock
;
3133 for (recv_off
= 0; recv_off
< data_size
; recv_off
+= chunk_size
) {
3134 size_t recv_size
= min(data_size
- recv_off
, chunk_size
);
3136 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, recv_size
, 0);
3139 /* Orderly shutdown. Not necessary to print an error. */
3140 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
3142 ERR("Socket %d error %d", conn
->sock
->fd
, ret
);
3145 goto end_stream_unlock
;
3148 /* Write data to stream output fd. */
3149 size_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
3151 if (size_ret
< recv_size
) {
3152 ERR("Relay error writing data to file");
3154 goto end_stream_unlock
;
3157 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
3158 size_ret
, stream
->stream_handle
);
3161 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
3162 be32toh(data_hdr
.padding_size
));
3164 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3165 stream
->stream_handle
, net_seq_num
, ret
);
3166 goto end_stream_unlock
;
3168 stream
->tracefile_size_current
+=
3169 data_size
+ be32toh(data_hdr
.padding_size
);
3170 if (stream
->prev_seq
== -1ULL) {
3173 if (index_flushed
) {
3174 stream
->pos_after_last_complete_data_index
=
3175 stream
->tracefile_size_current
;
3178 stream
->prev_seq
= net_seq_num
;
3180 ret
= try_rotate_stream(stream
);
3182 goto end_stream_unlock
;
3186 close_requested
= stream
->close_requested
;
3187 pthread_mutex_unlock(&stream
->lock
);
3188 if (close_requested
) {
3189 try_stream_close(stream
);
3193 pthread_mutex_lock(&session
->lock
);
3194 uatomic_set(&session
->new_streams
, 1);
3195 pthread_mutex_unlock(&session
->lock
);
3202 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3206 (void) lttng_poll_del(events
, pollfd
);
3208 ret
= close(pollfd
);
3210 ERR("Closing pollfd %d", pollfd
);
3214 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3215 int pollfd
, struct relay_connection
*conn
)
3217 const char *type_str
;
3219 switch (conn
->type
) {
3224 type_str
= "Control";
3226 case RELAY_VIEWER_COMMAND
:
3227 type_str
= "Viewer Command";
3229 case RELAY_VIEWER_NOTIFICATION
:
3230 type_str
= "Viewer Notification";
3233 type_str
= "Unknown";
3235 cleanup_connection_pollfd(events
, pollfd
);
3236 connection_put(conn
);
3237 DBG("%s connection closed with %d", type_str
, pollfd
);
3241 * This thread does the actual work
3243 static void *relay_thread_worker(void *data
)
3245 int ret
, err
= -1, last_seen_data_fd
= -1;
3247 struct lttng_poll_event events
;
3248 struct lttng_ht
*relay_connections_ht
;
3249 struct lttng_ht_iter iter
;
3250 struct lttcomm_relayd_hdr recv_hdr
;
3251 struct relay_connection
*destroy_conn
= NULL
;
3253 DBG("[thread] Relay worker started");
3255 rcu_register_thread();
3257 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3259 if (testpoint(relayd_thread_worker
)) {
3260 goto error_testpoint
;
3263 health_code_update();
3265 /* table of connections indexed on socket */
3266 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3267 if (!relay_connections_ht
) {
3268 goto relay_connections_ht_error
;
3271 ret
= create_thread_poll_set(&events
, 2);
3273 goto error_poll_create
;
3276 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3283 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3285 health_code_update();
3287 /* Infinite blocking call, waiting for transmission */
3288 DBG3("Relayd worker thread polling...");
3289 health_poll_entry();
3290 ret
= lttng_poll_wait(&events
, -1);
3294 * Restart interrupted system call.
3296 if (errno
== EINTR
) {
3305 * Process control. The control connection is
3306 * prioritized so we don't starve it with high
3307 * throughput tracing data on the data connection.
3309 for (i
= 0; i
< nb_fd
; i
++) {
3310 /* Fetch once the poll data */
3311 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3312 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3314 health_code_update();
3318 * No activity for this FD (poll
3324 /* Thread quit pipe has been closed. Killing thread. */
3325 ret
= check_thread_quit_pipe(pollfd
, revents
);
3331 /* Inspect the relay conn pipe for new connection */
3332 if (pollfd
== relay_conn_pipe
[0]) {
3333 if (revents
& LPOLLIN
) {
3334 struct relay_connection
*conn
;
3336 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3340 lttng_poll_add(&events
, conn
->sock
->fd
,
3341 LPOLLIN
| LPOLLRDHUP
);
3342 connection_ht_add(relay_connections_ht
, conn
);
3343 DBG("Connection socket %d added", conn
->sock
->fd
);
3344 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3345 ERR("Relay connection pipe error");
3348 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3352 struct relay_connection
*ctrl_conn
;
3354 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3355 /* If not found, there is a synchronization issue. */
3358 if (ctrl_conn
->type
== RELAY_DATA
) {
3359 if (revents
& LPOLLIN
) {
3361 * Flag the last seen data fd not deleted. It will be
3362 * used as the last seen fd if any fd gets deleted in
3365 last_notdel_data_fd
= pollfd
;
3367 goto put_ctrl_connection
;
3369 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3371 if (revents
& LPOLLIN
) {
3372 ret
= ctrl_conn
->sock
->ops
->recvmsg(ctrl_conn
->sock
,
3373 &recv_hdr
, sizeof(recv_hdr
), 0);
3375 /* Connection closed */
3376 relay_thread_close_connection(&events
, pollfd
,
3379 ret
= relay_process_control(&recv_hdr
, ctrl_conn
);
3381 /* Clear the session on error. */
3382 relay_thread_close_connection(&events
,
3387 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3388 relay_thread_close_connection(&events
,
3390 if (last_seen_data_fd
== pollfd
) {
3391 last_seen_data_fd
= last_notdel_data_fd
;
3394 ERR("Unexpected poll events %u for control sock %d",
3396 connection_put(ctrl_conn
);
3399 put_ctrl_connection
:
3400 connection_put(ctrl_conn
);
3405 * The last loop handled a control request, go back to poll to make
3406 * sure we prioritise the control socket.
3412 if (last_seen_data_fd
>= 0) {
3413 for (i
= 0; i
< nb_fd
; i
++) {
3414 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3416 health_code_update();
3418 if (last_seen_data_fd
== pollfd
) {
3425 /* Process data connection. */
3426 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3427 /* Fetch the poll data. */
3428 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3429 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3430 struct relay_connection
*data_conn
;
3432 health_code_update();
3435 /* No activity for this FD (poll implementation). */
3439 /* Skip the command pipe. It's handled in the first loop. */
3440 if (pollfd
== relay_conn_pipe
[0]) {
3444 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3446 /* Skip it. Might be removed before. */
3449 if (data_conn
->type
== RELAY_CONTROL
) {
3450 goto put_data_connection
;
3452 assert(data_conn
->type
== RELAY_DATA
);
3454 if (revents
& LPOLLIN
) {
3455 ret
= relay_process_data(data_conn
);
3456 /* Connection closed */
3458 relay_thread_close_connection(&events
, pollfd
,
3461 * Every goto restart call sets the last seen fd where
3462 * here we don't really care since we gracefully
3463 * continue the loop after the connection is deleted.
3466 /* Keep last seen port. */
3467 last_seen_data_fd
= pollfd
;
3468 connection_put(data_conn
);
3471 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3472 relay_thread_close_connection(&events
, pollfd
,
3475 ERR("Unknown poll events %u for data sock %d",
3478 put_data_connection
:
3479 connection_put(data_conn
);
3481 last_seen_data_fd
= -1;
3484 /* Normal exit, no error */
3489 /* Cleanup reamaining connection object. */
3491 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3494 health_code_update();
3496 if (session_abort(destroy_conn
->session
)) {
3501 * No need to grab another ref, because we own
3504 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3509 lttng_poll_clean(&events
);
3511 lttng_ht_destroy(relay_connections_ht
);
3512 relay_connections_ht_error
:
3513 /* Close relay conn pipes */
3514 utils_close_pipe(relay_conn_pipe
);
3516 DBG("Thread exited with error");
3518 DBG("Worker thread cleanup complete");
3522 ERR("Health error occurred in %s", __func__
);
3524 health_unregister(health_relayd
);
3525 rcu_unregister_thread();
3526 lttng_relay_stop_threads();
3531 * Create the relay command pipe to wake thread_manage_apps.
3532 * Closed in cleanup().
3534 static int create_relay_conn_pipe(void)
3538 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3546 int main(int argc
, char **argv
)
3548 int ret
= 0, retval
= 0;
3551 /* Parse arguments */
3553 if (set_options(argc
, argv
)) {
3558 if (set_signal_handler()) {
3563 /* Try to create directory if -o, --output is specified. */
3564 if (opt_output_path
) {
3565 if (*opt_output_path
!= '/') {
3566 ERR("Please specify an absolute path for -o, --output PATH");
3571 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3574 ERR("Unable to create %s", opt_output_path
);
3581 if (opt_daemon
|| opt_background
) {
3584 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3592 * We are in the child. Make sure all other file
3593 * descriptors are closed, in case we are called with
3594 * more opened file descriptors than the standard ones.
3596 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3601 /* Initialize thread health monitoring */
3602 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3603 if (!health_relayd
) {
3604 PERROR("health_app_create error");
3606 goto exit_health_app_create
;
3609 /* Create thread quit pipe */
3610 if (init_thread_quit_pipe()) {
3612 goto exit_init_data
;
3615 /* Setup the thread apps communication pipe. */
3616 if (create_relay_conn_pipe()) {
3618 goto exit_init_data
;
3621 /* Init relay command queue. */
3622 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3624 /* Initialize communication library */
3626 lttcomm_inet_init();
3628 /* tables of sessions indexed by session ID */
3629 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3632 goto exit_init_data
;
3635 /* tables of streams indexed by stream ID */
3636 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3637 if (!relay_streams_ht
) {
3639 goto exit_init_data
;
3642 /* tables of streams indexed by stream ID */
3643 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3644 if (!viewer_streams_ht
) {
3646 goto exit_init_data
;
3649 ret
= utils_create_pipe(health_quit_pipe
);
3652 goto exit_health_quit_pipe
;
3655 /* Create thread to manage the client socket */
3656 ret
= pthread_create(&health_thread
, default_pthread_attr(),
3657 thread_manage_health
, (void *) NULL
);
3660 PERROR("pthread_create health");
3662 goto exit_health_thread
;
3665 /* Setup the dispatcher thread */
3666 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
3667 relay_thread_dispatcher
, (void *) NULL
);
3670 PERROR("pthread_create dispatcher");
3672 goto exit_dispatcher_thread
;
3675 /* Setup the worker thread */
3676 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
3677 relay_thread_worker
, NULL
);
3680 PERROR("pthread_create worker");
3682 goto exit_worker_thread
;
3685 /* Setup the listener thread */
3686 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
3687 relay_thread_listener
, (void *) NULL
);
3690 PERROR("pthread_create listener");
3692 goto exit_listener_thread
;
3695 ret
= relayd_live_create(live_uri
);
3697 ERR("Starting live viewer threads");
3703 * This is where we start awaiting program completion (e.g. through
3704 * signal that asks threads to teardown).
3707 ret
= relayd_live_join();
3713 ret
= pthread_join(listener_thread
, &status
);
3716 PERROR("pthread_join listener_thread");
3720 exit_listener_thread
:
3721 ret
= pthread_join(worker_thread
, &status
);
3724 PERROR("pthread_join worker_thread");
3729 ret
= pthread_join(dispatcher_thread
, &status
);
3732 PERROR("pthread_join dispatcher_thread");
3735 exit_dispatcher_thread
:
3737 ret
= pthread_join(health_thread
, &status
);
3740 PERROR("pthread_join health_thread");
3745 utils_close_pipe(health_quit_pipe
);
3746 exit_health_quit_pipe
:
3749 health_app_destroy(health_relayd
);
3750 exit_health_app_create
:
3753 * Wait for all pending call_rcu work to complete before tearing
3754 * down data structures. call_rcu worker may be trying to
3755 * perform lookups in those structures.
3760 /* Ensure all prior call_rcu are done. */