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 <common/dynamic-buffer.h>
60 #include <common/buffer-view.h>
61 #include <urcu/rculist.h>
64 #include "ctf-trace.h"
67 #include "lttng-relayd.h"
69 #include "health-relayd.h"
70 #include "testpoint.h"
71 #include "viewer-stream.h"
74 #include "connection.h"
75 #include "tracefile-array.h"
76 #include "tcp_keep_alive.h"
78 static const char *help_msg
=
79 #ifdef LTTNG_EMBED_HELP
80 #include <lttng-relayd.8.h>
86 /* command line options */
87 char *opt_output_path
;
88 static int opt_daemon
, opt_background
;
91 * We need to wait for listener and live listener threads, as well as
92 * health check thread, before being ready to signal readiness.
94 #define NR_LTTNG_RELAY_READY 3
95 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
97 /* Size of receive buffer. */
98 #define RECV_DATA_BUFFER_SIZE 65536
99 #define FILE_COPY_BUFFER_SIZE 65536
101 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
102 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
104 static struct lttng_uri
*control_uri
;
105 static struct lttng_uri
*data_uri
;
106 static struct lttng_uri
*live_uri
;
108 const char *progname
;
110 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
111 static int tracing_group_name_override
;
113 const char * const config_section_name
= "relayd";
116 * Quit pipe for all threads. This permits a single cancellation point
117 * for all threads when receiving an event on the pipe.
119 int thread_quit_pipe
[2] = { -1, -1 };
122 * This pipe is used to inform the worker thread that a command is queued and
123 * ready to be processed.
125 static int relay_conn_pipe
[2] = { -1, -1 };
127 /* Shared between threads */
128 static int dispatch_thread_exit
;
130 static pthread_t listener_thread
;
131 static pthread_t dispatcher_thread
;
132 static pthread_t worker_thread
;
133 static pthread_t health_thread
;
136 * last_relay_stream_id_lock protects last_relay_stream_id increment
137 * atomicity on 32-bit architectures.
139 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
140 static uint64_t last_relay_stream_id
;
143 * Relay command queue.
145 * The relay_thread_listener and relay_thread_dispatcher communicate with this
148 static struct relay_conn_queue relay_conn_queue
;
150 /* Global relay stream hash table. */
151 struct lttng_ht
*relay_streams_ht
;
153 /* Global relay viewer stream hash table. */
154 struct lttng_ht
*viewer_streams_ht
;
156 /* Global relay sessions hash table. */
157 struct lttng_ht
*sessions_ht
;
159 /* Relayd health monitoring */
160 struct health_app
*health_relayd
;
162 static struct option long_options
[] = {
163 { "control-port", 1, 0, 'C', },
164 { "data-port", 1, 0, 'D', },
165 { "live-port", 1, 0, 'L', },
166 { "daemonize", 0, 0, 'd', },
167 { "background", 0, 0, 'b', },
168 { "group", 1, 0, 'g', },
169 { "help", 0, 0, 'h', },
170 { "output", 1, 0, 'o', },
171 { "verbose", 0, 0, 'v', },
172 { "config", 1, 0, 'f' },
173 { "version", 0, 0, 'V' },
177 static const char *config_ignore_options
[] = { "help", "config", "version" };
180 * Take an option from the getopt output and set it in the right variable to be
183 * Return 0 on success else a negative value.
185 static int set_option(int opt
, const char *arg
, const char *optname
)
191 fprintf(stderr
, "option %s", optname
);
193 fprintf(stderr
, " with arg %s\n", arg
);
197 if (lttng_is_setuid_setgid()) {
198 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
199 "-C, --control-port");
201 ret
= uri_parse(arg
, &control_uri
);
203 ERR("Invalid control URI specified");
206 if (control_uri
->port
== 0) {
207 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
212 if (lttng_is_setuid_setgid()) {
213 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
216 ret
= uri_parse(arg
, &data_uri
);
218 ERR("Invalid data URI specified");
221 if (data_uri
->port
== 0) {
222 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
227 if (lttng_is_setuid_setgid()) {
228 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
231 ret
= uri_parse(arg
, &live_uri
);
233 ERR("Invalid live URI specified");
236 if (live_uri
->port
== 0) {
237 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
248 if (lttng_is_setuid_setgid()) {
249 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
252 tracing_group_name
= strdup(arg
);
253 if (tracing_group_name
== NULL
) {
258 tracing_group_name_override
= 1;
262 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
264 ERR("Cannot show --help for `lttng-relayd`");
269 fprintf(stdout
, "%s\n", VERSION
);
272 if (lttng_is_setuid_setgid()) {
273 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
276 ret
= asprintf(&opt_output_path
, "%s", arg
);
279 PERROR("asprintf opt_output_path");
285 /* Verbose level can increase using multiple -v */
287 lttng_opt_verbose
= config_parse_value(arg
);
289 /* Only 3 level of verbosity (-vvv). */
290 if (lttng_opt_verbose
< 3) {
291 lttng_opt_verbose
+= 1;
296 /* Unknown option or other error.
297 * Error is printed by getopt, just return */
310 * config_entry_handler_cb used to handle options read from a config file.
311 * See config_entry_handler_cb comment in common/config/session-config.h for the
312 * return value conventions.
314 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
318 if (!entry
|| !entry
->name
|| !entry
->value
) {
323 /* Check if the option is to be ignored */
324 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
325 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
330 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
331 /* Ignore if entry name is not fully matched. */
332 if (strcmp(entry
->name
, long_options
[i
].name
)) {
337 * If the option takes no argument on the command line,
338 * we have to check if the value is "true". We support
339 * non-zero numeric values, true, on and yes.
341 if (!long_options
[i
].has_arg
) {
342 ret
= config_parse_value(entry
->value
);
345 WARN("Invalid configuration value \"%s\" for option %s",
346 entry
->value
, entry
->name
);
348 /* False, skip boolean config option. */
353 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
357 WARN("Unrecognized option \"%s\" in daemon configuration file.",
364 static int set_options(int argc
, char **argv
)
366 int c
, ret
= 0, option_index
= 0, retval
= 0;
367 int orig_optopt
= optopt
, orig_optind
= optind
;
368 char *default_address
, *optstring
;
369 const char *config_path
= NULL
;
371 optstring
= utils_generate_optstring(long_options
,
372 sizeof(long_options
) / sizeof(struct option
));
378 /* Check for the --config option */
380 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
381 &option_index
)) != -1) {
385 } else if (c
!= 'f') {
389 if (lttng_is_setuid_setgid()) {
390 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
393 config_path
= utils_expand_path(optarg
);
395 ERR("Failed to resolve path: %s", optarg
);
400 ret
= config_get_section_entries(config_path
, config_section_name
,
401 config_entry_handler
, NULL
);
404 ERR("Invalid configuration option at line %i", ret
);
410 /* Reset getopt's global state */
411 optopt
= orig_optopt
;
412 optind
= orig_optind
;
414 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
419 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
426 /* assign default values */
427 if (control_uri
== NULL
) {
428 ret
= asprintf(&default_address
,
429 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
430 DEFAULT_NETWORK_CONTROL_PORT
);
432 PERROR("asprintf default data address");
437 ret
= uri_parse(default_address
, &control_uri
);
438 free(default_address
);
440 ERR("Invalid control URI specified");
445 if (data_uri
== NULL
) {
446 ret
= asprintf(&default_address
,
447 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
448 DEFAULT_NETWORK_DATA_PORT
);
450 PERROR("asprintf default data address");
455 ret
= uri_parse(default_address
, &data_uri
);
456 free(default_address
);
458 ERR("Invalid data URI specified");
463 if (live_uri
== NULL
) {
464 ret
= asprintf(&default_address
,
465 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
466 DEFAULT_NETWORK_VIEWER_PORT
);
468 PERROR("asprintf default viewer control address");
473 ret
= uri_parse(default_address
, &live_uri
);
474 free(default_address
);
476 ERR("Invalid viewer control URI specified");
487 static void print_global_objects(void)
489 rcu_register_thread();
491 print_viewer_streams();
492 print_relay_streams();
495 rcu_unregister_thread();
501 static void relayd_cleanup(void)
503 print_global_objects();
507 if (viewer_streams_ht
)
508 lttng_ht_destroy(viewer_streams_ht
);
509 if (relay_streams_ht
)
510 lttng_ht_destroy(relay_streams_ht
);
512 lttng_ht_destroy(sessions_ht
);
514 /* free the dynamically allocated opt_output_path */
515 free(opt_output_path
);
517 /* Close thread quit pipes */
518 utils_close_pipe(thread_quit_pipe
);
520 uri_free(control_uri
);
522 /* Live URI is freed in the live thread. */
524 if (tracing_group_name_override
) {
525 free((void *) tracing_group_name
);
530 * Write to writable pipe used to notify a thread.
532 static int notify_thread_pipe(int wpipe
)
536 ret
= lttng_write(wpipe
, "!", 1);
538 PERROR("write poll pipe");
546 static int notify_health_quit_pipe(int *pipe
)
550 ret
= lttng_write(pipe
[1], "4", 1);
552 PERROR("write relay health quit");
561 * Stop all relayd and relayd-live threads.
563 int lttng_relay_stop_threads(void)
567 /* Stopping all threads */
568 DBG("Terminating all threads");
569 if (notify_thread_pipe(thread_quit_pipe
[1])) {
570 ERR("write error on thread quit pipe");
574 if (notify_health_quit_pipe(health_quit_pipe
)) {
575 ERR("write error on health quit pipe");
578 /* Dispatch thread */
579 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
580 futex_nto1_wake(&relay_conn_queue
.futex
);
582 if (relayd_live_stop()) {
583 ERR("Error stopping live threads");
590 * Signal handler for the daemon
592 * Simply stop all worker threads, leaving main() return gracefully after
593 * joining all threads and calling cleanup().
595 static void sighandler(int sig
)
599 DBG("SIGINT caught");
600 if (lttng_relay_stop_threads()) {
601 ERR("Error stopping threads");
605 DBG("SIGTERM caught");
606 if (lttng_relay_stop_threads()) {
607 ERR("Error stopping threads");
611 CMM_STORE_SHARED(recv_child_signal
, 1);
619 * Setup signal handler for :
620 * SIGINT, SIGTERM, SIGPIPE
622 static int set_signal_handler(void)
628 if ((ret
= sigemptyset(&sigset
)) < 0) {
629 PERROR("sigemptyset");
636 sa
.sa_handler
= sighandler
;
637 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
642 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
647 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
652 sa
.sa_handler
= SIG_IGN
;
653 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
658 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
663 void lttng_relay_notify_ready(void)
665 /* Notify the parent of the fork() process that we are ready. */
666 if (opt_daemon
|| opt_background
) {
667 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
668 kill(child_ppid
, SIGUSR1
);
674 * Init thread quit pipe.
676 * Return -1 on error or 0 if all pipes are created.
678 static int init_thread_quit_pipe(void)
682 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
688 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
690 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
694 if (events
== NULL
|| size
== 0) {
699 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
705 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
717 * Check if the thread quit pipe was triggered.
719 * Return 1 if it was triggered else 0;
721 static int check_thread_quit_pipe(int fd
, uint32_t events
)
723 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
731 * Create and init socket from uri.
733 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
736 struct lttcomm_sock
*sock
= NULL
;
738 sock
= lttcomm_alloc_sock_from_uri(uri
);
740 ERR("Allocating socket");
744 ret
= lttcomm_create_sock(sock
);
748 DBG("Listening on sock %d", sock
->fd
);
750 ret
= sock
->ops
->bind(sock
);
752 PERROR("Failed to bind socket");
756 ret
= sock
->ops
->listen(sock
, -1);
766 lttcomm_destroy_sock(sock
);
772 * This thread manages the listening for new connections on the network
774 static void *relay_thread_listener(void *data
)
776 int i
, ret
, pollfd
, err
= -1;
777 uint32_t revents
, nb_fd
;
778 struct lttng_poll_event events
;
779 struct lttcomm_sock
*control_sock
, *data_sock
;
781 DBG("[thread] Relay listener started");
783 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
785 health_code_update();
787 control_sock
= relay_socket_create(control_uri
);
789 goto error_sock_control
;
792 data_sock
= relay_socket_create(data_uri
);
794 goto error_sock_relay
;
798 * Pass 3 as size here for the thread quit pipe, control and
801 ret
= create_thread_poll_set(&events
, 3);
803 goto error_create_poll
;
806 /* Add the control socket */
807 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
812 /* Add the data socket */
813 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
818 lttng_relay_notify_ready();
820 if (testpoint(relayd_thread_listener
)) {
821 goto error_testpoint
;
825 health_code_update();
827 DBG("Listener accepting connections");
831 ret
= lttng_poll_wait(&events
, -1);
835 * Restart interrupted system call.
837 if (errno
== EINTR
) {
845 DBG("Relay new connection received");
846 for (i
= 0; i
< nb_fd
; i
++) {
847 health_code_update();
849 /* Fetch once the poll data */
850 revents
= LTTNG_POLL_GETEV(&events
, i
);
851 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
855 * No activity for this FD (poll
861 /* Thread quit pipe has been closed. Killing thread. */
862 ret
= check_thread_quit_pipe(pollfd
, revents
);
868 if (revents
& LPOLLIN
) {
870 * A new connection is requested, therefore a
871 * sessiond/consumerd connection is allocated in
872 * this thread, enqueued to a global queue and
873 * dequeued (and freed) in the worker thread.
876 struct relay_connection
*new_conn
;
877 struct lttcomm_sock
*newsock
;
878 enum connection_type type
;
880 if (pollfd
== data_sock
->fd
) {
882 newsock
= data_sock
->ops
->accept(data_sock
);
883 DBG("Relay data connection accepted, socket %d",
886 assert(pollfd
== control_sock
->fd
);
887 type
= RELAY_CONTROL
;
888 newsock
= control_sock
->ops
->accept(control_sock
);
889 DBG("Relay control connection accepted, socket %d",
893 PERROR("accepting sock");
897 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
900 PERROR("setsockopt inet");
901 lttcomm_destroy_sock(newsock
);
905 ret
= socket_apply_keep_alive_config(newsock
->fd
);
907 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
909 lttcomm_destroy_sock(newsock
);
913 new_conn
= connection_create(newsock
, type
);
915 lttcomm_destroy_sock(newsock
);
919 /* Enqueue request for the dispatcher thread. */
920 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
924 * Wake the dispatch queue futex.
925 * Implicit memory barrier with the
926 * exchange in cds_wfcq_enqueue.
928 futex_nto1_wake(&relay_conn_queue
.futex
);
929 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
930 ERR("socket poll error");
933 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
943 lttng_poll_clean(&events
);
945 if (data_sock
->fd
>= 0) {
946 ret
= data_sock
->ops
->close(data_sock
);
951 lttcomm_destroy_sock(data_sock
);
953 if (control_sock
->fd
>= 0) {
954 ret
= control_sock
->ops
->close(control_sock
);
959 lttcomm_destroy_sock(control_sock
);
963 ERR("Health error occurred in %s", __func__
);
965 health_unregister(health_relayd
);
966 DBG("Relay listener thread cleanup complete");
967 lttng_relay_stop_threads();
972 * This thread manages the dispatching of the requests to worker threads
974 static void *relay_thread_dispatcher(void *data
)
978 struct cds_wfcq_node
*node
;
979 struct relay_connection
*new_conn
= NULL
;
981 DBG("[thread] Relay dispatcher started");
983 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
985 if (testpoint(relayd_thread_dispatcher
)) {
986 goto error_testpoint
;
989 health_code_update();
992 health_code_update();
994 /* Atomically prepare the queue futex */
995 futex_nto1_prepare(&relay_conn_queue
.futex
);
997 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1002 health_code_update();
1004 /* Dequeue commands */
1005 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1006 &relay_conn_queue
.tail
);
1008 DBG("Woken up but nothing in the relay command queue");
1009 /* Continue thread execution */
1012 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1014 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1017 * Inform worker thread of the new request. This
1018 * call is blocking so we can be assured that
1019 * the data will be read at some point in time
1020 * or wait to the end of the world :)
1022 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1024 PERROR("write connection pipe");
1025 connection_put(new_conn
);
1028 } while (node
!= NULL
);
1030 /* Futex wait on queue. Blocking call on futex() */
1031 health_poll_entry();
1032 futex_nto1_wait(&relay_conn_queue
.futex
);
1036 /* Normal exit, no error */
1043 ERR("Health error occurred in %s", __func__
);
1045 health_unregister(health_relayd
);
1046 DBG("Dispatch thread dying");
1047 lttng_relay_stop_threads();
1052 * Set index data from the control port to a given index object.
1054 static int set_index_control_data(struct relay_index
*index
,
1055 struct lttcomm_relayd_index
*data
,
1056 struct relay_connection
*conn
)
1058 struct ctf_packet_index index_data
;
1061 * The index on disk is encoded in big endian.
1063 index_data
.packet_size
= htobe64(data
->packet_size
);
1064 index_data
.content_size
= htobe64(data
->content_size
);
1065 index_data
.timestamp_begin
= htobe64(data
->timestamp_begin
);
1066 index_data
.timestamp_end
= htobe64(data
->timestamp_end
);
1067 index_data
.events_discarded
= htobe64(data
->events_discarded
);
1068 index_data
.stream_id
= htobe64(data
->stream_id
);
1070 if (conn
->minor
>= 8) {
1071 index
->index_data
.stream_instance_id
= htobe64(data
->stream_instance_id
);
1072 index
->index_data
.packet_seq_num
= htobe64(data
->packet_seq_num
);
1075 return relay_index_set_data(index
, &index_data
);
1079 * Handle the RELAYD_CREATE_SESSION command.
1081 * On success, send back the session id or else return a negative value.
1083 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1084 struct relay_connection
*conn
,
1085 const struct lttng_buffer_view
*payload
)
1089 struct relay_session
*session
;
1090 struct lttcomm_relayd_status_session reply
;
1091 char session_name
[LTTNG_NAME_MAX
];
1092 char hostname
[LTTNG_HOST_NAME_MAX
];
1093 uint32_t live_timer
= 0;
1094 bool snapshot
= false;
1096 memset(session_name
, 0, LTTNG_NAME_MAX
);
1097 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1099 memset(&reply
, 0, sizeof(reply
));
1101 switch (conn
->minor
) {
1106 case 4: /* LTTng sessiond 2.4 */
1108 ret
= cmd_create_session_2_4(payload
, session_name
,
1109 hostname
, &live_timer
, &snapshot
);
1115 session
= session_create(session_name
, hostname
, live_timer
,
1116 snapshot
, conn
->major
, conn
->minor
);
1121 assert(!conn
->session
);
1122 conn
->session
= session
;
1123 DBG("Created session %" PRIu64
, session
->id
);
1125 reply
.session_id
= htobe64(session
->id
);
1129 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1131 reply
.ret_code
= htobe32(LTTNG_OK
);
1134 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1135 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1136 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1145 * When we have received all the streams and the metadata for a channel,
1146 * we make them visible to the viewer threads.
1148 static void publish_connection_local_streams(struct relay_connection
*conn
)
1150 struct relay_stream
*stream
;
1151 struct relay_session
*session
= conn
->session
;
1154 * We publish all streams belonging to a session atomically wrt
1157 pthread_mutex_lock(&session
->lock
);
1159 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1161 stream_publish(stream
);
1166 * Inform the viewer that there are new streams in the session.
1168 if (session
->viewer_attached
) {
1169 uatomic_set(&session
->new_streams
, 1);
1171 pthread_mutex_unlock(&session
->lock
);
1175 * relay_add_stream: allocate a new stream for a session
1177 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1178 struct relay_connection
*conn
,
1179 const struct lttng_buffer_view
*payload
)
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
) {
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(payload
, &path_name
,
1202 case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
1204 ret
= cmd_recv_stream_2_2(payload
, &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);
1245 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1246 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1258 * relay_close_stream: close a specific stream
1260 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1261 struct relay_connection
*conn
,
1262 const struct lttng_buffer_view
*payload
)
1266 struct relay_session
*session
= conn
->session
;
1267 struct lttcomm_relayd_close_stream stream_info
;
1268 struct lttcomm_relayd_generic_reply reply
;
1269 struct relay_stream
*stream
;
1271 DBG("Close stream received");
1273 if (!session
|| !conn
->version_check_done
) {
1274 ERR("Trying to close a stream before version check");
1276 goto end_no_session
;
1279 if (payload
->size
< sizeof(stream_info
)) {
1280 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1281 sizeof(stream_info
), payload
->size
);
1283 goto end_no_session
;
1285 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1286 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1287 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1289 stream
= stream_get_by_id(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
= 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
);
1334 memset(&reply
, 0, sizeof(reply
));
1336 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1338 reply
.ret_code
= htobe32(LTTNG_OK
);
1340 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1341 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1342 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1343 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1353 * relay_reset_metadata: reset a metadata stream
1356 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1357 struct relay_connection
*conn
,
1358 const struct lttng_buffer_view
*payload
)
1362 struct relay_session
*session
= conn
->session
;
1363 struct lttcomm_relayd_reset_metadata stream_info
;
1364 struct lttcomm_relayd_generic_reply reply
;
1365 struct relay_stream
*stream
;
1367 DBG("Reset metadata received");
1369 if (!session
|| !conn
->version_check_done
) {
1370 ERR("Trying to reset a metadata stream before version check");
1372 goto end_no_session
;
1375 if (payload
->size
< sizeof(stream_info
)) {
1376 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1377 sizeof(stream_info
), payload
->size
);
1379 goto end_no_session
;
1381 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1382 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1383 stream_info
.version
= be64toh(stream_info
.version
);
1385 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1387 /* Unsupported for live sessions for now. */
1388 if (session
->live_timer
!= 0) {
1393 stream
= stream_get_by_id(stream_info
.stream_id
);
1398 pthread_mutex_lock(&stream
->lock
);
1399 if (!stream
->is_metadata
) {
1404 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1405 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1406 &stream
->stream_fd
->fd
);
1408 ERR("Failed to rotate metadata file %s of channel %s",
1409 stream
->path_name
, stream
->channel_name
);
1414 pthread_mutex_unlock(&stream
->lock
);
1418 memset(&reply
, 0, sizeof(reply
));
1420 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1422 reply
.ret_code
= htobe32(LTTNG_OK
);
1424 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1425 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1426 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1427 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1437 * relay_unknown_command: send -1 if received unknown command
1439 static void relay_unknown_command(struct relay_connection
*conn
)
1441 struct lttcomm_relayd_generic_reply reply
;
1444 memset(&reply
, 0, sizeof(reply
));
1445 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1446 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1447 if (send_ret
< sizeof(reply
)) {
1448 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1453 * relay_start: send an acknowledgment to the client to tell if we are
1454 * ready to receive data. We are ready if a session is established.
1456 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1457 struct relay_connection
*conn
,
1458 const struct lttng_buffer_view
*payload
)
1462 struct lttcomm_relayd_generic_reply reply
;
1463 struct relay_session
*session
= conn
->session
;
1466 DBG("Trying to start the streaming without a session established");
1467 ret
= htobe32(LTTNG_ERR_UNK
);
1470 memset(&reply
, 0, sizeof(reply
));
1471 reply
.ret_code
= htobe32(LTTNG_OK
);
1472 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1474 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1475 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1484 * Append padding to the file pointed by the file descriptor fd.
1486 static int write_padding_to_file(int fd
, uint32_t size
)
1495 zeros
= zmalloc(size
);
1496 if (zeros
== NULL
) {
1497 PERROR("zmalloc zeros for padding");
1502 ret
= lttng_write(fd
, zeros
, size
);
1504 PERROR("write padding to file");
1514 * Close the current index file if it is open, and create a new one.
1516 * Return 0 on success, -1 on error.
1519 int create_rotate_index_file(struct relay_stream
*stream
)
1522 uint32_t major
, minor
;
1524 /* Put ref on previous index_file. */
1525 if (stream
->index_file
) {
1526 lttng_index_file_put(stream
->index_file
);
1527 stream
->index_file
= NULL
;
1529 major
= stream
->trace
->session
->major
;
1530 minor
= stream
->trace
->session
->minor
;
1531 stream
->index_file
= lttng_index_file_create(stream
->path_name
,
1532 stream
->channel_name
,
1533 -1, -1, stream
->tracefile_size
,
1534 tracefile_array_get_file_index_head(stream
->tfa
),
1535 lttng_to_index_major(major
, minor
),
1536 lttng_to_index_minor(major
, minor
));
1537 if (!stream
->index_file
) {
1549 int do_rotate_stream(struct relay_stream
*stream
)
1553 /* Perform the stream rotation. */
1554 ret
= utils_rotate_stream_file(stream
->path_name
,
1555 stream
->channel_name
, stream
->tracefile_size
,
1556 stream
->tracefile_count
, -1,
1557 -1, stream
->stream_fd
->fd
,
1558 NULL
, &stream
->stream_fd
->fd
);
1560 ERR("Rotating stream output file");
1563 stream
->tracefile_size_current
= 0;
1565 /* Rotate also the index if the stream is not a metadata stream. */
1566 if (!stream
->is_metadata
) {
1567 ret
= create_rotate_index_file(stream
);
1569 ERR("Failed to rotate index file");
1574 stream
->rotate_at_seq_num
= -1ULL;
1575 stream
->pos_after_last_complete_data_index
= 0;
1582 * If too much data has been written in a tracefile before we received the
1583 * rotation command, we have to move the excess data to the new tracefile and
1584 * perform the rotation. This can happen because the control and data
1585 * connections are separate, the indexes as well as the commands arrive from
1586 * the control connection and we have no control over the order so we could be
1587 * in a situation where too much data has been received on the data connection
1588 * before the rotation command on the control connection arrives. We don't need
1589 * to update the index because its order is guaranteed with the rotation
1593 int rotate_truncate_stream(struct relay_stream
*stream
)
1597 uint64_t diff
, pos
= 0;
1598 char buf
[FILE_COPY_BUFFER_SIZE
];
1600 assert(!stream
->is_metadata
);
1602 assert(stream
->tracefile_size_current
>
1603 stream
->pos_after_last_complete_data_index
);
1604 diff
= stream
->tracefile_size_current
-
1605 stream
->pos_after_last_complete_data_index
;
1607 /* Create the new tracefile. */
1608 new_fd
= utils_create_stream_file(stream
->path_name
,
1609 stream
->channel_name
,
1610 stream
->tracefile_size
, stream
->tracefile_count
,
1611 /* uid */ -1, /* gid */ -1, /* suffix */ NULL
);
1613 ERR("Failed to create new stream file at path %s for channel %s",
1614 stream
->path_name
, stream
->channel_name
);
1620 * Rewind the current tracefile to the position at which the rotation
1621 * should have occured.
1623 lseek_ret
= lseek(stream
->stream_fd
->fd
,
1624 stream
->pos_after_last_complete_data_index
, SEEK_SET
);
1625 if (lseek_ret
< 0) {
1626 PERROR("seek truncate stream");
1631 /* Move data from the old file to the new file. */
1632 while (pos
< diff
) {
1633 uint64_t count
, bytes_left
;
1636 bytes_left
= diff
- pos
;
1637 count
= bytes_left
> sizeof(buf
) ? sizeof(buf
) : bytes_left
;
1638 assert(count
<= SIZE_MAX
);
1640 io_ret
= lttng_read(stream
->stream_fd
->fd
, buf
, count
);
1641 if (io_ret
< (ssize_t
) count
) {
1642 char error_string
[256];
1644 snprintf(error_string
, sizeof(error_string
),
1645 "Failed to read %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1646 count
, stream
->stream_fd
->fd
, io_ret
);
1648 PERROR("%s", error_string
);
1650 ERR("%s", error_string
);
1656 io_ret
= lttng_write(new_fd
, buf
, count
);
1657 if (io_ret
< (ssize_t
) count
) {
1658 char error_string
[256];
1660 snprintf(error_string
, sizeof(error_string
),
1661 "Failed to write %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1662 count
, new_fd
, io_ret
);
1664 PERROR("%s", error_string
);
1666 ERR("%s", error_string
);
1675 /* Truncate the file to get rid of the excess data. */
1676 ret
= ftruncate(stream
->stream_fd
->fd
,
1677 stream
->pos_after_last_complete_data_index
);
1679 PERROR("ftruncate");
1683 ret
= close(stream
->stream_fd
->fd
);
1685 PERROR("Closing tracefile");
1689 ret
= create_rotate_index_file(stream
);
1691 ERR("Rotate stream index file");
1696 * Update the offset and FD of all the eventual indexes created by the
1697 * data connection before the rotation command arrived.
1699 ret
= relay_index_switch_all_files(stream
);
1701 ERR("Failed to rotate index file");
1705 stream
->stream_fd
->fd
= new_fd
;
1706 stream
->tracefile_size_current
= diff
;
1707 stream
->pos_after_last_complete_data_index
= 0;
1708 stream
->rotate_at_seq_num
= -1ULL;
1717 * Check if a stream should perform a rotation (for session rotation).
1718 * Must be called with the stream lock held.
1720 * Return 0 on success, a negative value on error.
1723 int try_rotate_stream(struct relay_stream
*stream
)
1727 /* No rotation expected. */
1728 if (stream
->rotate_at_seq_num
== -1ULL) {
1732 if (stream
->prev_seq
< stream
->rotate_at_seq_num
||
1733 stream
->prev_seq
== -1ULL) {
1734 DBG("Stream %" PRIu64
" no yet ready for rotation",
1735 stream
->stream_handle
);
1737 } else if (stream
->prev_seq
> stream
->rotate_at_seq_num
) {
1738 DBG("Rotation after too much data has been written in tracefile "
1739 "for stream %" PRIu64
", need to truncate before "
1740 "rotating", stream
->stream_handle
);
1741 ret
= rotate_truncate_stream(stream
);
1743 ERR("Failed to truncate stream");
1747 /* stream->prev_seq == stream->rotate_at_seq_num */
1748 DBG("Stream %" PRIu64
" ready for rotation",
1749 stream
->stream_handle
);
1750 ret
= do_rotate_stream(stream
);
1758 * relay_recv_metadata: receive the metadata for the session.
1760 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1761 struct relay_connection
*conn
,
1762 const struct lttng_buffer_view
*payload
)
1766 struct relay_session
*session
= conn
->session
;
1767 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1768 struct relay_stream
*metadata_stream
;
1769 uint64_t metadata_payload_size
;
1772 ERR("Metadata sent before version check");
1777 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1778 ERR("Incorrect data size");
1782 metadata_payload_size
= recv_hdr
->data_size
-
1783 sizeof(struct lttcomm_relayd_metadata_payload
);
1785 memcpy(&metadata_payload_header
, payload
->data
,
1786 sizeof(metadata_payload_header
));
1787 metadata_payload_header
.stream_id
= be64toh(
1788 metadata_payload_header
.stream_id
);
1789 metadata_payload_header
.padding_size
= be32toh(
1790 metadata_payload_header
.padding_size
);
1792 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1793 if (!metadata_stream
) {
1798 pthread_mutex_lock(&metadata_stream
->lock
);
1800 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
,
1801 payload
->data
+ sizeof(metadata_payload_header
),
1802 metadata_payload_size
);
1803 if (size_ret
< metadata_payload_size
) {
1804 ERR("Relay error writing metadata on file");
1809 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1810 metadata_payload_header
.padding_size
);
1811 if (size_ret
< (int64_t) metadata_payload_header
.padding_size
) {
1816 metadata_stream
->metadata_received
+=
1817 metadata_payload_size
+ metadata_payload_header
.padding_size
;
1818 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1819 metadata_stream
->metadata_received
);
1821 ret
= try_rotate_stream(metadata_stream
);
1827 pthread_mutex_unlock(&metadata_stream
->lock
);
1828 stream_put(metadata_stream
);
1834 * relay_send_version: send relayd version number
1836 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1837 struct relay_connection
*conn
,
1838 const struct lttng_buffer_view
*payload
)
1842 struct lttcomm_relayd_version reply
, msg
;
1843 bool compatible
= true;
1845 conn
->version_check_done
= true;
1847 /* Get version from the other side. */
1848 if (payload
->size
< sizeof(msg
)) {
1849 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1850 sizeof(msg
), payload
->size
);
1855 memcpy(&msg
, payload
->data
, sizeof(msg
));
1856 msg
.major
= be32toh(msg
.major
);
1857 msg
.minor
= be32toh(msg
.minor
);
1859 memset(&reply
, 0, sizeof(reply
));
1860 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1861 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1863 /* Major versions must be the same */
1864 if (reply
.major
!= msg
.major
) {
1865 DBG("Incompatible major versions (%u vs %u), deleting session",
1866 reply
.major
, msg
.major
);
1870 conn
->major
= reply
.major
;
1871 /* We adapt to the lowest compatible version */
1872 if (reply
.minor
<= msg
.minor
) {
1873 conn
->minor
= reply
.minor
;
1875 conn
->minor
= msg
.minor
;
1878 reply
.major
= htobe32(reply
.major
);
1879 reply
.minor
= htobe32(reply
.minor
);
1880 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1882 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1883 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1896 DBG("Version check done using protocol %u.%u", conn
->major
,
1904 * Check for data pending for a given stream id from the session daemon.
1906 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1907 struct relay_connection
*conn
,
1908 const struct lttng_buffer_view
*payload
)
1910 struct relay_session
*session
= conn
->session
;
1911 struct lttcomm_relayd_data_pending msg
;
1912 struct lttcomm_relayd_generic_reply reply
;
1913 struct relay_stream
*stream
;
1917 DBG("Data pending command received");
1919 if (!session
|| !conn
->version_check_done
) {
1920 ERR("Trying to check for data before version check");
1922 goto end_no_session
;
1925 if (payload
->size
< sizeof(msg
)) {
1926 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1927 sizeof(msg
), payload
->size
);
1929 goto end_no_session
;
1931 memcpy(&msg
, payload
->data
, sizeof(msg
));
1932 msg
.stream_id
= be64toh(msg
.stream_id
);
1933 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1935 stream
= stream_get_by_id(msg
.stream_id
);
1936 if (stream
== NULL
) {
1941 pthread_mutex_lock(&stream
->lock
);
1943 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1944 " and last_seq %" PRIu64
, msg
.stream_id
,
1945 stream
->prev_seq
, msg
.last_net_seq_num
);
1947 /* Avoid wrapping issue */
1948 if (((int64_t) (stream
->prev_seq
- msg
.last_net_seq_num
)) >= 0) {
1949 /* Data has in fact been written and is NOT pending */
1952 /* Data still being streamed thus pending */
1956 stream
->data_pending_check_done
= true;
1957 pthread_mutex_unlock(&stream
->lock
);
1962 memset(&reply
, 0, sizeof(reply
));
1963 reply
.ret_code
= htobe32(ret
);
1964 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1965 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1966 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1976 * Wait for the control socket to reach a quiescent state.
1978 * Note that for now, when receiving this command from the session
1979 * daemon, this means that every subsequent commands or data received on
1980 * the control socket has been handled. So, this is why we simply return
1983 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
1984 struct relay_connection
*conn
,
1985 const struct lttng_buffer_view
*payload
)
1989 struct relay_stream
*stream
;
1990 struct lttcomm_relayd_quiescent_control msg
;
1991 struct lttcomm_relayd_generic_reply reply
;
1993 DBG("Checking quiescent state on control socket");
1995 if (!conn
->session
|| !conn
->version_check_done
) {
1996 ERR("Trying to check for data before version check");
1998 goto end_no_session
;
2001 if (payload
->size
< sizeof(msg
)) {
2002 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2003 sizeof(msg
), payload
->size
);
2005 goto end_no_session
;
2007 memcpy(&msg
, payload
->data
, sizeof(msg
));
2008 msg
.stream_id
= be64toh(msg
.stream_id
);
2010 stream
= stream_get_by_id(msg
.stream_id
);
2014 pthread_mutex_lock(&stream
->lock
);
2015 stream
->data_pending_check_done
= true;
2016 pthread_mutex_unlock(&stream
->lock
);
2018 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2021 memset(&reply
, 0, sizeof(reply
));
2022 reply
.ret_code
= htobe32(LTTNG_OK
);
2023 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2024 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2025 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2037 * Initialize a data pending command. This means that a consumer is about
2038 * to ask for data pending for each stream it holds. Simply iterate over
2039 * all streams of a session and set the data_pending_check_done flag.
2041 * This command returns to the client a LTTNG_OK code.
2043 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2044 struct relay_connection
*conn
,
2045 const struct lttng_buffer_view
*payload
)
2049 struct lttng_ht_iter iter
;
2050 struct lttcomm_relayd_begin_data_pending msg
;
2051 struct lttcomm_relayd_generic_reply reply
;
2052 struct relay_stream
*stream
;
2057 DBG("Init streams for data pending");
2059 if (!conn
->session
|| !conn
->version_check_done
) {
2060 ERR("Trying to check for data before version check");
2062 goto end_no_session
;
2065 if (payload
->size
< sizeof(msg
)) {
2066 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2067 sizeof(msg
), payload
->size
);
2069 goto end_no_session
;
2071 memcpy(&msg
, payload
->data
, sizeof(msg
));
2072 msg
.session_id
= be64toh(msg
.session_id
);
2075 * Iterate over all streams to set the begin data pending flag.
2076 * For now, the streams are indexed by stream handle so we have
2077 * to iterate over all streams to find the one associated with
2078 * the right session_id.
2081 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2083 if (!stream_get(stream
)) {
2086 if (stream
->trace
->session
->id
== msg
.session_id
) {
2087 pthread_mutex_lock(&stream
->lock
);
2088 stream
->data_pending_check_done
= false;
2089 pthread_mutex_unlock(&stream
->lock
);
2090 DBG("Set begin data pending flag to stream %" PRIu64
,
2091 stream
->stream_handle
);
2097 memset(&reply
, 0, sizeof(reply
));
2098 /* All good, send back reply. */
2099 reply
.ret_code
= htobe32(LTTNG_OK
);
2101 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2102 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2103 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2115 * End data pending command. This will check, for a given session id, if
2116 * each stream associated with it has its data_pending_check_done flag
2117 * set. If not, this means that the client lost track of the stream but
2118 * the data is still being streamed on our side. In this case, we inform
2119 * the client that data is in flight.
2121 * Return to the client if there is data in flight or not with a ret_code.
2123 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2124 struct relay_connection
*conn
,
2125 const struct lttng_buffer_view
*payload
)
2129 struct lttng_ht_iter iter
;
2130 struct lttcomm_relayd_end_data_pending msg
;
2131 struct lttcomm_relayd_generic_reply reply
;
2132 struct relay_stream
*stream
;
2133 uint32_t is_data_inflight
= 0;
2135 DBG("End data pending command");
2137 if (!conn
->session
|| !conn
->version_check_done
) {
2138 ERR("Trying to check for data before version check");
2140 goto end_no_session
;
2143 if (payload
->size
< sizeof(msg
)) {
2144 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2145 sizeof(msg
), payload
->size
);
2147 goto end_no_session
;
2149 memcpy(&msg
, payload
->data
, sizeof(msg
));
2150 msg
.session_id
= be64toh(msg
.session_id
);
2153 * Iterate over all streams to see if the begin data pending
2157 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2159 if (!stream_get(stream
)) {
2162 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2166 pthread_mutex_lock(&stream
->lock
);
2167 if (!stream
->data_pending_check_done
) {
2168 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
2169 is_data_inflight
= 1;
2170 DBG("Data is still in flight for stream %" PRIu64
,
2171 stream
->stream_handle
);
2172 pthread_mutex_unlock(&stream
->lock
);
2177 pthread_mutex_unlock(&stream
->lock
);
2182 memset(&reply
, 0, sizeof(reply
));
2183 /* All good, send back reply. */
2184 reply
.ret_code
= htobe32(is_data_inflight
);
2186 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2187 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2188 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2200 * Receive an index for a specific stream.
2202 * Return 0 on success else a negative value.
2204 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2205 struct relay_connection
*conn
,
2206 const struct lttng_buffer_view
*payload
)
2210 struct relay_session
*session
= conn
->session
;
2211 struct lttcomm_relayd_index index_info
;
2212 struct relay_index
*index
;
2213 struct lttcomm_relayd_generic_reply reply
;
2214 struct relay_stream
*stream
;
2219 DBG("Relay receiving index");
2221 if (!session
|| !conn
->version_check_done
) {
2222 ERR("Trying to close a stream before version check");
2224 goto end_no_session
;
2227 msg_len
= lttcomm_relayd_index_len(
2228 lttng_to_index_major(conn
->major
, conn
->minor
),
2229 lttng_to_index_minor(conn
->major
, conn
->minor
));
2230 if (payload
->size
< msg_len
) {
2231 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2232 msg_len
, payload
->size
);
2234 goto end_no_session
;
2236 memcpy(&index_info
, payload
->data
, msg_len
);
2237 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2238 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2239 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2240 index_info
.content_size
= be64toh(index_info
.content_size
);
2241 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2242 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2243 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2244 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2246 if (conn
->minor
>= 8) {
2247 index_info
.stream_instance_id
=
2248 be64toh(index_info
.stream_instance_id
);
2249 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2252 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2254 ERR("stream_get_by_id not found");
2258 pthread_mutex_lock(&stream
->lock
);
2260 /* Live beacon handling */
2261 if (index_info
.packet_size
== 0) {
2262 DBG("Received live beacon for stream %" PRIu64
,
2263 stream
->stream_handle
);
2266 * Only flag a stream inactive when it has already
2267 * received data and no indexes are in flight.
2269 if (stream
->index_received_seqcount
> 0
2270 && stream
->indexes_in_flight
== 0) {
2271 stream
->beacon_ts_end
= index_info
.timestamp_end
;
2274 goto end_stream_put
;
2276 stream
->beacon_ts_end
= -1ULL;
2279 if (stream
->ctf_stream_id
== -1ULL) {
2280 stream
->ctf_stream_id
= index_info
.stream_id
;
2282 index
= relay_index_get_by_id_or_create(stream
, index_info
.net_seq_num
);
2285 ERR("relay_index_get_by_id_or_create index NULL");
2286 goto end_stream_put
;
2288 if (set_index_control_data(index
, &index_info
, conn
)) {
2289 ERR("set_index_control_data error");
2290 relay_index_put(index
);
2292 goto end_stream_put
;
2294 ret
= relay_index_try_flush(index
);
2296 tracefile_array_commit_seq(stream
->tfa
);
2297 stream
->index_received_seqcount
++;
2298 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
2299 } else if (ret
> 0) {
2303 ERR("relay_index_try_flush error %d", ret
);
2304 relay_index_put(index
);
2309 pthread_mutex_unlock(&stream
->lock
);
2314 memset(&reply
, 0, sizeof(reply
));
2316 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2318 reply
.ret_code
= htobe32(LTTNG_OK
);
2320 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2321 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2322 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2331 * Receive the streams_sent message.
2333 * Return 0 on success else a negative value.
2335 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2336 struct relay_connection
*conn
,
2337 const struct lttng_buffer_view
*payload
)
2341 struct lttcomm_relayd_generic_reply reply
;
2345 DBG("Relay receiving streams_sent");
2347 if (!conn
->session
|| !conn
->version_check_done
) {
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);
2362 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2363 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2376 * relay_rotate_session_stream: rotate a stream to a new tracefile for the session
2377 * rotation feature (not the tracefile rotation feature).
2379 static int relay_rotate_session_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
2380 struct relay_connection
*conn
,
2381 const struct lttng_buffer_view
*payload
)
2385 struct relay_session
*session
= conn
->session
;
2386 struct lttcomm_relayd_rotate_stream stream_info
;
2387 struct lttcomm_relayd_generic_reply reply
;
2388 struct relay_stream
*stream
;
2391 struct lttng_buffer_view new_path_view
;
2393 DBG("Rotate stream received");
2395 if (!session
|| !conn
->version_check_done
) {
2396 ERR("Trying to rotate a stream before version check");
2401 if (session
->major
== 2 && session
->minor
< 11) {
2402 ERR("Unsupported feature before 2.11");
2407 header_len
= sizeof(struct lttcomm_relayd_rotate_stream
);
2409 if (payload
->size
< header_len
) {
2410 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2411 header_len
, payload
->size
);
2416 memcpy(&stream_info
, payload
->data
, header_len
);
2418 /* Convert to host */
2419 stream_info
.pathname_length
= be32toh(stream_info
.pathname_length
);
2420 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
2421 stream_info
.new_chunk_id
= be64toh(stream_info
.new_chunk_id
);
2422 stream_info
.rotate_at_seq_num
= be64toh(stream_info
.rotate_at_seq_num
);
2424 path_len
= stream_info
.pathname_length
;
2425 if (payload
->size
< header_len
+ path_len
) {
2426 ERR("Unexpected payload size in \"relay_rotate_session_stream\" including path: expected >= %zu bytes, got %zu bytes",
2427 header_len
+ path_len
, payload
->size
);
2432 /* Ensure it fits in local filename length. */
2433 if (path_len
>= LTTNG_PATH_MAX
) {
2434 ret
= -ENAMETOOLONG
;
2435 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2436 path_len
, LTTNG_PATH_MAX
);
2440 new_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2441 stream_info
.pathname_length
);
2443 stream
= stream_get_by_id(stream_info
.stream_id
);
2449 pthread_mutex_lock(&stream
->lock
);
2452 * Update the trace path (just the folder, the stream name does not
2455 free(stream
->path_name
);
2456 stream
->path_name
= create_output_path(new_path_view
.data
);
2457 if (!stream
->path_name
) {
2458 ERR("Failed to create a new output path");
2460 goto end_stream_unlock
;
2462 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
,
2465 ERR("relay creating output directory");
2467 goto end_stream_unlock
;
2470 stream
->chunk_id
= stream_info
.new_chunk_id
;
2472 if (stream
->is_metadata
) {
2474 * The metadata stream is sent only over the control connection
2475 * so we know we have all the data to perform the stream
2478 ret
= do_rotate_stream(stream
);
2480 stream
->rotate_at_seq_num
= stream_info
.rotate_at_seq_num
;
2481 ret
= try_rotate_stream(stream
);
2484 goto end_stream_unlock
;
2488 pthread_mutex_unlock(&stream
->lock
);
2491 memset(&reply
, 0, sizeof(reply
));
2493 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2495 reply
.ret_code
= htobe32(LTTNG_OK
);
2497 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2498 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2499 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2500 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2510 * relay_mkdir: Create a folder on the disk.
2512 static int relay_mkdir(const struct lttcomm_relayd_hdr
*recv_hdr
,
2513 struct relay_connection
*conn
,
2514 const struct lttng_buffer_view
*payload
)
2517 struct relay_session
*session
= conn
->session
;
2518 struct lttcomm_relayd_mkdir path_info_header
;
2519 struct lttcomm_relayd_generic_reply reply
;
2523 struct lttng_buffer_view path_view
;
2525 if (!session
|| !conn
->version_check_done
) {
2526 ERR("Trying to create a directory before version check");
2528 goto end_no_session
;
2531 if (session
->major
== 2 && session
->minor
< 11) {
2533 * This client is not supposed to use this command since
2534 * it predates its introduction.
2536 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2538 goto end_no_session
;
2541 header_len
= sizeof(path_info_header
);
2542 if (payload
->size
< header_len
) {
2543 ERR("Unexpected payload size in \"relay_mkdir\": expected >= %zu bytes, got %zu bytes",
2544 header_len
, payload
->size
);
2546 goto end_no_session
;
2549 memcpy(&path_info_header
, payload
->data
, header_len
);
2551 path_info_header
.length
= be32toh(path_info_header
.length
);
2553 if (payload
->size
< header_len
+ path_info_header
.length
) {
2554 ERR("Unexpected payload size in \"relay_mkdir\" including path: expected >= %zu bytes, got %zu bytes",
2555 header_len
+ path_info_header
.length
, payload
->size
);
2557 goto end_no_session
;
2560 /* Ensure that it fits in local path length. */
2561 if (path_info_header
.length
>= LTTNG_PATH_MAX
) {
2562 ret
= -ENAMETOOLONG
;
2563 ERR("Path name argument of mkdir command (%" PRIu32
" bytes) exceeds the maximal length allowed (%d bytes)",
2564 path_info_header
.length
, LTTNG_PATH_MAX
);
2568 path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2569 path_info_header
.length
);
2571 path
= create_output_path(path_view
.data
);
2573 ERR("Failed to create output path");
2578 ret
= utils_mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, -1, -1);
2580 ERR("relay creating output directory");
2587 memset(&reply
, 0, sizeof(reply
));
2589 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2591 reply
.ret_code
= htobe32(LTTNG_OK
);
2593 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2594 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2595 ERR("Failed to send \"mkdir\" command reply (ret = %zd)", send_ret
);
2604 static int validate_rotate_rename_path_length(const char *path_type
,
2605 uint32_t path_length
)
2609 if (path_length
> LTTNG_PATH_MAX
) {
2610 ret
= -ENAMETOOLONG
;
2611 ERR("rotate rename \"%s\" path name length (%" PRIu32
" bytes) exceeds the allowed size of %i bytes",
2612 path_type
, path_length
, LTTNG_PATH_MAX
);
2613 } else if (path_length
== 0) {
2615 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type
);
2621 * relay_rotate_rename: rename the trace folder after a rotation is
2622 * completed. We are not closing any fd here, just moving the folder, so it
2623 * works even if data is still in-flight.
2625 static int relay_rotate_rename(const struct lttcomm_relayd_hdr
*recv_hdr
,
2626 struct relay_connection
*conn
,
2627 const struct lttng_buffer_view
*payload
)
2631 struct relay_session
*session
= conn
->session
;
2632 struct lttcomm_relayd_generic_reply reply
;
2633 struct lttcomm_relayd_rotate_rename header
;
2635 size_t received_paths_size
;
2636 char *complete_old_path
= NULL
, *complete_new_path
= NULL
;
2637 struct lttng_buffer_view old_path_view
;
2638 struct lttng_buffer_view new_path_view
;
2640 if (!session
|| !conn
->version_check_done
) {
2641 ERR("Trying to rename a trace folder before version check");
2646 if (session
->major
== 2 && session
->minor
< 11) {
2647 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2652 header_len
= sizeof(header
);
2653 if (payload
->size
< header_len
) {
2654 ERR("Unexpected payload size in \"relay_rotate_rename\": expected >= %zu bytes, got %zu bytes",
2655 header_len
, payload
->size
);
2660 memcpy(&header
, payload
->data
, header_len
);
2662 header
.old_path_length
= be32toh(header
.old_path_length
);
2663 header
.new_path_length
= be32toh(header
.new_path_length
);
2664 received_paths_size
= header
.old_path_length
+ header
.new_path_length
;
2666 if (payload
->size
< header_len
+ received_paths_size
) {
2667 ERR("Unexpected payload size in \"relay_rotate_rename\" including paths: expected >= %zu bytes, got %zu bytes",
2668 header_len
, payload
->size
);
2673 /* Ensure the paths don't exceed their allowed size. */
2674 ret
= validate_rotate_rename_path_length("old", header
.old_path_length
);
2678 ret
= validate_rotate_rename_path_length("new", header
.new_path_length
);
2683 old_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2684 header
.old_path_length
);
2685 new_path_view
= lttng_buffer_view_from_view(payload
,
2686 header_len
+ header
.old_path_length
,
2687 header
.new_path_length
);
2689 /* Validate that both paths received are NULL terminated. */
2690 if (old_path_view
.data
[old_path_view
.size
- 1] != '\0') {
2691 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2695 if (new_path_view
.data
[new_path_view
.size
- 1] != '\0') {
2696 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2701 complete_old_path
= create_output_path(old_path_view
.data
);
2702 if (!complete_old_path
) {
2703 ERR("Failed to build old output path in rotate_rename command");
2708 complete_new_path
= create_output_path(new_path_view
.data
);
2709 if (!complete_new_path
) {
2710 ERR("Failed to build new output path in rotate_rename command");
2715 ret
= utils_mkdir_recursive(complete_new_path
, S_IRWXU
| S_IRWXG
,
2718 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2724 * If a domain has not yet created its channel, the domain-specific
2725 * folder might not exist, but this is not an error.
2727 ret
= rename(complete_old_path
, complete_new_path
);
2728 if (ret
< 0 && errno
!= ENOENT
) {
2729 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2730 complete_old_path
, complete_new_path
);
2736 memset(&reply
, 0, sizeof(reply
));
2738 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2740 reply
.ret_code
= htobe32(LTTNG_OK
);
2742 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2744 if (send_ret
< sizeof(reply
)) {
2745 ERR("Failed to send \"rotate rename\" command reply (ret = %zd)",
2751 free(complete_old_path
);
2752 free(complete_new_path
);
2757 * Check if all the streams in the session have completed the last rotation.
2758 * The chunk_id value is used to distinguish the cases where a stream was
2759 * closed on the consumerd before the rotation started but it still active on
2760 * the relayd, and the case where a stream appeared on the consumerd/relayd
2761 * after the last rotation started (in that case, it is already writing in the
2762 * new chunk folder).
2765 int relay_rotate_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2766 struct relay_connection
*conn
,
2767 const struct lttng_buffer_view
*payload
)
2769 struct relay_session
*session
= conn
->session
;
2770 struct lttcomm_relayd_rotate_pending msg
;
2771 struct lttcomm_relayd_rotate_pending_reply reply
;
2772 struct lttng_ht_iter iter
;
2773 struct relay_stream
*stream
;
2777 bool rotate_pending
= false;
2779 DBG("Rotate pending command received");
2781 if (!session
|| !conn
->version_check_done
) {
2782 ERR("Trying to check for data before version check");
2787 if (session
->major
== 2 && session
->minor
< 11) {
2788 ERR("Unsupported feature before 2.11");
2793 if (payload
->size
< sizeof(msg
)) {
2794 ERR("Unexpected payload size in \"relay_rotate_pending\": expected >= %zu bytes, got %zu bytes",
2795 sizeof(msg
), payload
->size
);
2800 memcpy(&msg
, payload
->data
, sizeof(msg
));
2802 chunk_id
= be64toh(msg
.chunk_id
);
2804 DBG("Evaluating rotate pending for chunk id %" PRIu64
, chunk_id
);
2807 * Iterate over all the streams in the session and check if they are
2808 * still waiting for data to perform their rotation.
2811 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2813 if (!stream_get(stream
)) {
2816 if (stream
->trace
->session
!= session
) {
2820 pthread_mutex_lock(&stream
->lock
);
2821 if (stream
->rotate_at_seq_num
!= -1ULL) {
2822 /* We have not yet performed the rotation. */
2823 rotate_pending
= true;
2824 DBG("Stream %" PRIu64
" is still rotating",
2825 stream
->stream_handle
);
2826 } else if (stream
->chunk_id
< chunk_id
) {
2828 * Stream closed on the consumer but still active on the
2831 rotate_pending
= true;
2832 DBG("Stream %" PRIu64
" did not exist on the consumer "
2833 "when the last rotation started, but is"
2834 "still waiting for data before getting"
2836 stream
->stream_handle
);
2838 pthread_mutex_unlock(&stream
->lock
);
2840 if (rotate_pending
) {
2847 memset(&reply
, 0, sizeof(reply
));
2848 reply
.generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
);
2849 reply
.is_pending
= (uint8_t) !!rotate_pending
;
2850 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2852 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2853 ERR("Failed to send \"rotate pending\" command reply (ret = %zd)",
2862 #define DBG_CMD(cmd_name, conn) \
2863 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2865 static int relay_process_control_command(struct relay_connection
*conn
,
2866 const struct lttcomm_relayd_hdr
*header
,
2867 const struct lttng_buffer_view
*payload
)
2871 switch (header
->cmd
) {
2872 case RELAYD_CREATE_SESSION
:
2873 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2874 ret
= relay_create_session(header
, conn
, payload
);
2876 case RELAYD_ADD_STREAM
:
2877 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2878 ret
= relay_add_stream(header
, conn
, payload
);
2880 case RELAYD_START_DATA
:
2881 DBG_CMD("RELAYD_START_DATA", conn
);
2882 ret
= relay_start(header
, conn
, payload
);
2884 case RELAYD_SEND_METADATA
:
2885 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2886 ret
= relay_recv_metadata(header
, conn
, payload
);
2888 case RELAYD_VERSION
:
2889 DBG_CMD("RELAYD_VERSION", conn
);
2890 ret
= relay_send_version(header
, conn
, payload
);
2892 case RELAYD_CLOSE_STREAM
:
2893 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2894 ret
= relay_close_stream(header
, conn
, payload
);
2896 case RELAYD_DATA_PENDING
:
2897 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2898 ret
= relay_data_pending(header
, conn
, payload
);
2900 case RELAYD_QUIESCENT_CONTROL
:
2901 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2902 ret
= relay_quiescent_control(header
, conn
, payload
);
2904 case RELAYD_BEGIN_DATA_PENDING
:
2905 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2906 ret
= relay_begin_data_pending(header
, conn
, payload
);
2908 case RELAYD_END_DATA_PENDING
:
2909 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2910 ret
= relay_end_data_pending(header
, conn
, payload
);
2912 case RELAYD_SEND_INDEX
:
2913 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2914 ret
= relay_recv_index(header
, conn
, payload
);
2916 case RELAYD_STREAMS_SENT
:
2917 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2918 ret
= relay_streams_sent(header
, conn
, payload
);
2920 case RELAYD_RESET_METADATA
:
2921 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2922 ret
= relay_reset_metadata(header
, conn
, payload
);
2924 case RELAYD_ROTATE_STREAM
:
2925 DBG_CMD("RELAYD_ROTATE_STREAM", conn
);
2926 ret
= relay_rotate_session_stream(header
, conn
, payload
);
2928 case RELAYD_ROTATE_RENAME
:
2929 DBG_CMD("RELAYD_ROTATE_RENAME", conn
);
2930 ret
= relay_rotate_rename(header
, conn
, payload
);
2932 case RELAYD_ROTATE_PENDING
:
2933 DBG_CMD("RELAYD_ROTATE_PENDING", conn
);
2934 ret
= relay_rotate_pending(header
, conn
, payload
);
2937 DBG_CMD("RELAYD_MKDIR", conn
);
2938 ret
= relay_mkdir(header
, conn
, payload
);
2940 case RELAYD_UPDATE_SYNC_INFO
:
2942 ERR("Received unknown command (%u)", header
->cmd
);
2943 relay_unknown_command(conn
);
2952 static int relay_process_control_receive_payload(struct relay_connection
*conn
)
2955 struct lttng_dynamic_buffer
*reception_buffer
=
2956 &conn
->protocol
.ctrl
.reception_buffer
;
2957 struct ctrl_connection_state_receive_payload
*state
=
2958 &conn
->protocol
.ctrl
.state
.receive_payload
;
2959 struct lttng_buffer_view payload_view
;
2961 if (state
->left_to_receive
== 0) {
2962 /* Short-circuit for payload-less commands. */
2963 goto reception_complete
;
2966 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2967 reception_buffer
->data
+ state
->received
,
2968 state
->left_to_receive
, MSG_DONTWAIT
);
2970 ERR("Unable to receive command payload on sock %d", conn
->sock
->fd
);
2972 } else if (ret
== 0) {
2973 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2979 assert(ret
<= state
->left_to_receive
);
2981 state
->left_to_receive
-= ret
;
2982 state
->received
+= ret
;
2984 if (state
->left_to_receive
> 0) {
2986 * Can't transition to the protocol's next state, wait to
2987 * receive the rest of the header.
2989 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2990 state
->received
, state
->left_to_receive
,
2997 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
2998 conn
->sock
->fd
, state
->received
);
3000 * The payload required to process the command has been received.
3001 * A view to the reception buffer is forwarded to the various
3002 * commands and the state of the control is reset on success.
3004 * Commands are responsible for sending their reply to the peer.
3006 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3008 ret
= relay_process_control_command(conn
,
3009 &state
->header
, &payload_view
);
3014 ret
= connection_reset_protocol_state(conn
);
3019 static int relay_process_control_receive_header(struct relay_connection
*conn
)
3022 struct lttcomm_relayd_hdr header
;
3023 struct lttng_dynamic_buffer
*reception_buffer
=
3024 &conn
->protocol
.ctrl
.reception_buffer
;
3025 struct ctrl_connection_state_receive_header
*state
=
3026 &conn
->protocol
.ctrl
.state
.receive_header
;
3028 assert(state
->left_to_receive
!= 0);
3030 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3031 reception_buffer
->data
+ state
->received
,
3032 state
->left_to_receive
, MSG_DONTWAIT
);
3034 ERR("Unable to receive control command header on sock %d", conn
->sock
->fd
);
3036 } else if (ret
== 0) {
3037 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3043 assert(ret
<= state
->left_to_receive
);
3045 state
->left_to_receive
-= ret
;
3046 state
->received
+= ret
;
3048 if (state
->left_to_receive
> 0) {
3050 * Can't transition to the protocol's next state, wait to
3051 * receive the rest of the header.
3053 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3054 state
->received
, state
->left_to_receive
,
3060 /* Transition to next state: receiving the command's payload. */
3061 conn
->protocol
.ctrl
.state_id
=
3062 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3063 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3064 header
.circuit_id
= be64toh(header
.circuit_id
);
3065 header
.data_size
= be64toh(header
.data_size
);
3066 header
.cmd
= be32toh(header
.cmd
);
3067 header
.cmd_version
= be32toh(header
.cmd_version
);
3068 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3069 &header
, sizeof(header
));
3071 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3072 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3075 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3076 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3082 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3084 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3085 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3091 if (header
.data_size
== 0) {
3093 * Manually invoke the next state as the poll loop
3094 * will not wake-up to allow us to proceed further.
3096 ret
= relay_process_control_receive_payload(conn
);
3103 * Process the commands received on the control socket
3105 static int relay_process_control(struct relay_connection
*conn
)
3109 switch (conn
->protocol
.ctrl
.state_id
) {
3110 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3111 ret
= relay_process_control_receive_header(conn
);
3113 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3114 ret
= relay_process_control_receive_payload(conn
);
3117 ERR("Unknown control connection protocol state encountered.");
3125 * Handle index for a data stream.
3127 * Called with the stream lock held.
3129 * Return 0 on success else a negative value.
3131 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
3132 bool rotate_index
, bool *flushed
, uint64_t total_size
)
3135 uint64_t data_offset
;
3136 struct relay_index
*index
;
3138 /* Get data offset because we are about to update the index. */
3139 data_offset
= htobe64(stream
->tracefile_size_current
);
3141 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
3142 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
3145 * Lookup for an existing index for that stream id/sequence
3146 * number. If it exists, the control thread has already received the
3147 * data for it, thus we need to write it to disk.
3149 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
3155 if (rotate_index
|| !stream
->index_file
) {
3156 ret
= create_rotate_index_file(stream
);
3158 ERR("Failed to rotate index");
3159 /* Put self-ref for this index due to error. */
3160 relay_index_put(index
);
3166 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
3168 /* Put self-ref for this index due to error. */
3169 relay_index_put(index
);
3174 ret
= relay_index_try_flush(index
);
3176 tracefile_array_commit_seq(stream
->tfa
);
3177 stream
->index_received_seqcount
++;
3179 } else if (ret
> 0) {
3180 index
->total_size
= total_size
;
3184 /* Put self-ref for this index due to error. */
3185 relay_index_put(index
);
3193 static int relay_process_data_receive_header(struct relay_connection
*conn
)
3196 struct data_connection_state_receive_header
*state
=
3197 &conn
->protocol
.data
.state
.receive_header
;
3198 struct lttcomm_relayd_data_hdr header
;
3199 struct relay_stream
*stream
;
3201 assert(state
->left_to_receive
!= 0);
3203 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3204 state
->header_reception_buffer
+ state
->received
,
3205 state
->left_to_receive
, MSG_DONTWAIT
);
3207 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3209 } else if (ret
== 0) {
3210 /* Orderly shutdown. Not necessary to print an error. */
3211 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3217 assert(ret
<= state
->left_to_receive
);
3219 state
->left_to_receive
-= ret
;
3220 state
->received
+= ret
;
3222 if (state
->left_to_receive
> 0) {
3224 * Can't transition to the protocol's next state, wait to
3225 * receive the rest of the header.
3227 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3228 state
->received
, state
->left_to_receive
,
3234 /* Transition to next state: receiving the payload. */
3235 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3237 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3238 header
.circuit_id
= be64toh(header
.circuit_id
);
3239 header
.stream_id
= be64toh(header
.stream_id
);
3240 header
.data_size
= be32toh(header
.data_size
);
3241 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3242 header
.padding_size
= be32toh(header
.padding_size
);
3243 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3245 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3247 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3248 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3250 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3251 conn
->sock
->fd
, header
.circuit_id
,
3252 header
.stream_id
, header
.data_size
,
3253 header
.net_seq_num
, header
.padding_size
);
3255 stream
= stream_get_by_id(header
.stream_id
);
3257 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3263 pthread_mutex_lock(&stream
->lock
);
3265 /* Check if a rotation is needed. */
3266 if (stream
->tracefile_size
> 0 &&
3267 (stream
->tracefile_size_current
+ header
.data_size
) >
3268 stream
->tracefile_size
) {
3269 uint64_t old_id
, new_id
;
3271 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
3272 tracefile_array_file_rotate(stream
->tfa
);
3274 /* new_id is updated by utils_rotate_stream_file. */
3277 ret
= utils_rotate_stream_file(stream
->path_name
,
3278 stream
->channel_name
, stream
->tracefile_size
,
3279 stream
->tracefile_count
, -1,
3280 -1, stream
->stream_fd
->fd
,
3281 &new_id
, &stream
->stream_fd
->fd
);
3283 ERR("Failed to rotate stream output file");
3284 goto end_stream_unlock
;
3288 * Reset current size because we just performed a stream
3291 stream
->tracefile_size_current
= 0;
3292 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= true;
3297 pthread_mutex_unlock(&stream
->lock
);
3303 static int relay_process_data_receive_payload(struct relay_connection
*conn
)
3306 struct relay_stream
*stream
;
3307 struct data_connection_state_receive_payload
*state
=
3308 &conn
->protocol
.data
.state
.receive_payload
;
3309 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3310 char data_buffer
[chunk_size
];
3311 bool partial_recv
= false;
3312 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3313 uint64_t left_to_receive
= state
->left_to_receive
;
3314 struct relay_session
*session
;
3316 stream
= stream_get_by_id(state
->header
.stream_id
);
3318 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3319 state
->header
.stream_id
);
3324 pthread_mutex_lock(&stream
->lock
);
3325 session
= stream
->trace
->session
;
3327 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3328 state
->header
.stream_id
, state
->header
.net_seq_num
,
3329 state
->received
, left_to_receive
);
3332 * The size of the "chunk" received on any iteration is bounded by:
3333 * - the data left to receive,
3334 * - the data immediately available on the socket,
3335 * - the on-stack data buffer
3337 while (left_to_receive
> 0 && !partial_recv
) {
3339 size_t recv_size
= min(left_to_receive
, chunk_size
);
3341 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3342 recv_size
, MSG_DONTWAIT
);
3344 ERR("Socket %d error %d", conn
->sock
->fd
, ret
);
3346 goto end_stream_unlock
;
3347 } else if (ret
== 0) {
3348 /* No more data ready to be consumed on socket. */
3349 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3350 state
->header
.stream_id
);
3352 } else if (ret
< (int) recv_size
) {
3354 * All the data available on the socket has been
3357 partial_recv
= true;
3362 /* Write data to stream output fd. */
3363 write_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
3365 if (write_ret
< (ssize_t
) recv_size
) {
3366 ERR("Relay error writing data to file");
3368 goto end_stream_unlock
;
3371 left_to_receive
-= recv_size
;
3372 state
->received
+= recv_size
;
3373 state
->left_to_receive
= left_to_receive
;
3375 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
3376 write_ret
, stream
->stream_handle
);
3379 if (state
->left_to_receive
> 0) {
3381 * Did not receive all the data expected, wait for more data to
3382 * become available on the socket.
3384 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3385 state
->header
.stream_id
, state
->received
,
3386 state
->left_to_receive
);
3388 goto end_stream_unlock
;
3391 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
3392 state
->header
.padding_size
);
3394 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3395 stream
->stream_handle
,
3396 state
->header
.net_seq_num
, ret
);
3397 goto end_stream_unlock
;
3401 if (session
->minor
>= 4 && !session
->snapshot
) {
3402 ret
= handle_index_data(stream
, state
->header
.net_seq_num
,
3403 state
->rotate_index
, &index_flushed
, state
->header
.data_size
+ state
->header
.padding_size
);
3405 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3406 stream
->stream_handle
,
3407 state
->header
.net_seq_num
, ret
);
3408 goto end_stream_unlock
;
3412 stream
->tracefile_size_current
+= state
->header
.data_size
+
3413 state
->header
.padding_size
;
3415 if (stream
->prev_seq
== -1ULL) {
3418 if (index_flushed
) {
3419 stream
->pos_after_last_complete_data_index
=
3420 stream
->tracefile_size_current
;
3423 stream
->prev_seq
= state
->header
.net_seq_num
;
3426 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3427 * contents of *state which are aliased (union) to the same location as
3428 * the new state. Don't use it beyond this point.
3430 connection_reset_protocol_state(conn
);
3433 ret
= try_rotate_stream(stream
);
3435 goto end_stream_unlock
;
3439 close_requested
= stream
->close_requested
;
3440 pthread_mutex_unlock(&stream
->lock
);
3441 if (close_requested
&& left_to_receive
== 0) {
3442 try_stream_close(stream
);
3446 pthread_mutex_lock(&session
->lock
);
3447 uatomic_set(&session
->new_streams
, 1);
3448 pthread_mutex_unlock(&session
->lock
);
3457 * relay_process_data: Process the data received on the data socket
3459 static int relay_process_data(struct relay_connection
*conn
)
3463 switch (conn
->protocol
.data
.state_id
) {
3464 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3465 ret
= relay_process_data_receive_header(conn
);
3467 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3468 ret
= relay_process_data_receive_payload(conn
);
3471 ERR("Unexpected data connection communication state.");
3478 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3482 (void) lttng_poll_del(events
, pollfd
);
3484 ret
= close(pollfd
);
3486 ERR("Closing pollfd %d", pollfd
);
3490 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3491 int pollfd
, struct relay_connection
*conn
)
3493 const char *type_str
;
3495 switch (conn
->type
) {
3500 type_str
= "Control";
3502 case RELAY_VIEWER_COMMAND
:
3503 type_str
= "Viewer Command";
3505 case RELAY_VIEWER_NOTIFICATION
:
3506 type_str
= "Viewer Notification";
3509 type_str
= "Unknown";
3511 cleanup_connection_pollfd(events
, pollfd
);
3512 connection_put(conn
);
3513 DBG("%s connection closed with %d", type_str
, pollfd
);
3517 * This thread does the actual work
3519 static void *relay_thread_worker(void *data
)
3521 int ret
, err
= -1, last_seen_data_fd
= -1;
3523 struct lttng_poll_event events
;
3524 struct lttng_ht
*relay_connections_ht
;
3525 struct lttng_ht_iter iter
;
3526 struct relay_connection
*destroy_conn
= NULL
;
3528 DBG("[thread] Relay worker started");
3530 rcu_register_thread();
3532 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3534 if (testpoint(relayd_thread_worker
)) {
3535 goto error_testpoint
;
3538 health_code_update();
3540 /* table of connections indexed on socket */
3541 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3542 if (!relay_connections_ht
) {
3543 goto relay_connections_ht_error
;
3546 ret
= create_thread_poll_set(&events
, 2);
3548 goto error_poll_create
;
3551 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3558 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3560 health_code_update();
3562 /* Infinite blocking call, waiting for transmission */
3563 DBG3("Relayd worker thread polling...");
3564 health_poll_entry();
3565 ret
= lttng_poll_wait(&events
, -1);
3569 * Restart interrupted system call.
3571 if (errno
== EINTR
) {
3580 * Process control. The control connection is
3581 * prioritized so we don't starve it with high
3582 * throughput tracing data on the data connection.
3584 for (i
= 0; i
< nb_fd
; i
++) {
3585 /* Fetch once the poll data */
3586 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3587 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3589 health_code_update();
3593 * No activity for this FD (poll
3599 /* Thread quit pipe has been closed. Killing thread. */
3600 ret
= check_thread_quit_pipe(pollfd
, revents
);
3606 /* Inspect the relay conn pipe for new connection */
3607 if (pollfd
== relay_conn_pipe
[0]) {
3608 if (revents
& LPOLLIN
) {
3609 struct relay_connection
*conn
;
3611 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3615 lttng_poll_add(&events
, conn
->sock
->fd
,
3616 LPOLLIN
| LPOLLRDHUP
);
3617 connection_ht_add(relay_connections_ht
, conn
);
3618 DBG("Connection socket %d added", conn
->sock
->fd
);
3619 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3620 ERR("Relay connection pipe error");
3623 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3627 struct relay_connection
*ctrl_conn
;
3629 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3630 /* If not found, there is a synchronization issue. */
3633 if (ctrl_conn
->type
== RELAY_DATA
) {
3634 if (revents
& LPOLLIN
) {
3636 * Flag the last seen data fd not deleted. It will be
3637 * used as the last seen fd if any fd gets deleted in
3640 last_notdel_data_fd
= pollfd
;
3642 goto put_ctrl_connection
;
3644 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3646 if (revents
& LPOLLIN
) {
3647 ret
= relay_process_control(ctrl_conn
);
3649 /* Clear the connection on error. */
3650 relay_thread_close_connection(&events
,
3655 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3656 relay_thread_close_connection(&events
,
3658 if (last_seen_data_fd
== pollfd
) {
3659 last_seen_data_fd
= last_notdel_data_fd
;
3662 ERR("Unexpected poll events %u for control sock %d",
3664 connection_put(ctrl_conn
);
3667 put_ctrl_connection
:
3668 connection_put(ctrl_conn
);
3673 * The last loop handled a control request, go back to poll to make
3674 * sure we prioritise the control socket.
3680 if (last_seen_data_fd
>= 0) {
3681 for (i
= 0; i
< nb_fd
; i
++) {
3682 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3684 health_code_update();
3686 if (last_seen_data_fd
== pollfd
) {
3693 /* Process data connection. */
3694 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3695 /* Fetch the poll data. */
3696 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3697 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3698 struct relay_connection
*data_conn
;
3700 health_code_update();
3703 /* No activity for this FD (poll implementation). */
3707 /* Skip the command pipe. It's handled in the first loop. */
3708 if (pollfd
== relay_conn_pipe
[0]) {
3712 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3714 /* Skip it. Might be removed before. */
3717 if (data_conn
->type
== RELAY_CONTROL
) {
3718 goto put_data_connection
;
3720 assert(data_conn
->type
== RELAY_DATA
);
3722 if (revents
& LPOLLIN
) {
3723 ret
= relay_process_data(data_conn
);
3724 /* Connection closed */
3726 relay_thread_close_connection(&events
, pollfd
,
3729 * Every goto restart call sets the last seen fd where
3730 * here we don't really care since we gracefully
3731 * continue the loop after the connection is deleted.
3734 /* Keep last seen port. */
3735 last_seen_data_fd
= pollfd
;
3736 connection_put(data_conn
);
3739 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3740 relay_thread_close_connection(&events
, pollfd
,
3743 ERR("Unknown poll events %u for data sock %d",
3746 put_data_connection
:
3747 connection_put(data_conn
);
3749 last_seen_data_fd
= -1;
3752 /* Normal exit, no error */
3757 /* Cleanup reamaining connection object. */
3759 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3762 health_code_update();
3764 if (session_abort(destroy_conn
->session
)) {
3769 * No need to grab another ref, because we own
3772 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3777 lttng_poll_clean(&events
);
3779 lttng_ht_destroy(relay_connections_ht
);
3780 relay_connections_ht_error
:
3781 /* Close relay conn pipes */
3782 utils_close_pipe(relay_conn_pipe
);
3784 DBG("Thread exited with error");
3786 DBG("Worker thread cleanup complete");
3790 ERR("Health error occurred in %s", __func__
);
3792 health_unregister(health_relayd
);
3793 rcu_unregister_thread();
3794 lttng_relay_stop_threads();
3799 * Create the relay command pipe to wake thread_manage_apps.
3800 * Closed in cleanup().
3802 static int create_relay_conn_pipe(void)
3806 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3814 int main(int argc
, char **argv
)
3816 int ret
= 0, retval
= 0;
3819 /* Parse arguments */
3821 if (set_options(argc
, argv
)) {
3826 if (set_signal_handler()) {
3831 /* Try to create directory if -o, --output is specified. */
3832 if (opt_output_path
) {
3833 if (*opt_output_path
!= '/') {
3834 ERR("Please specify an absolute path for -o, --output PATH");
3839 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3842 ERR("Unable to create %s", opt_output_path
);
3849 if (opt_daemon
|| opt_background
) {
3852 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3860 * We are in the child. Make sure all other file
3861 * descriptors are closed, in case we are called with
3862 * more opened file descriptors than the standard ones.
3864 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3869 /* Initialize thread health monitoring */
3870 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3871 if (!health_relayd
) {
3872 PERROR("health_app_create error");
3874 goto exit_health_app_create
;
3877 /* Create thread quit pipe */
3878 if (init_thread_quit_pipe()) {
3880 goto exit_init_data
;
3883 /* Setup the thread apps communication pipe. */
3884 if (create_relay_conn_pipe()) {
3886 goto exit_init_data
;
3889 /* Init relay command queue. */
3890 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3892 /* Initialize communication library */
3894 lttcomm_inet_init();
3896 /* tables of sessions indexed by session ID */
3897 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3900 goto exit_init_data
;
3903 /* tables of streams indexed by stream ID */
3904 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3905 if (!relay_streams_ht
) {
3907 goto exit_init_data
;
3910 /* tables of streams indexed by stream ID */
3911 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3912 if (!viewer_streams_ht
) {
3914 goto exit_init_data
;
3917 ret
= utils_create_pipe(health_quit_pipe
);
3920 goto exit_health_quit_pipe
;
3923 /* Create thread to manage the client socket */
3924 ret
= pthread_create(&health_thread
, default_pthread_attr(),
3925 thread_manage_health
, (void *) NULL
);
3928 PERROR("pthread_create health");
3930 goto exit_health_thread
;
3933 /* Setup the dispatcher thread */
3934 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
3935 relay_thread_dispatcher
, (void *) NULL
);
3938 PERROR("pthread_create dispatcher");
3940 goto exit_dispatcher_thread
;
3943 /* Setup the worker thread */
3944 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
3945 relay_thread_worker
, NULL
);
3948 PERROR("pthread_create worker");
3950 goto exit_worker_thread
;
3953 /* Setup the listener thread */
3954 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
3955 relay_thread_listener
, (void *) NULL
);
3958 PERROR("pthread_create listener");
3960 goto exit_listener_thread
;
3963 ret
= relayd_live_create(live_uri
);
3965 ERR("Starting live viewer threads");
3971 * This is where we start awaiting program completion (e.g. through
3972 * signal that asks threads to teardown).
3975 ret
= relayd_live_join();
3981 ret
= pthread_join(listener_thread
, &status
);
3984 PERROR("pthread_join listener_thread");
3988 exit_listener_thread
:
3989 ret
= pthread_join(worker_thread
, &status
);
3992 PERROR("pthread_join worker_thread");
3997 ret
= pthread_join(dispatcher_thread
, &status
);
4000 PERROR("pthread_join dispatcher_thread");
4003 exit_dispatcher_thread
:
4005 ret
= pthread_join(health_thread
, &status
);
4008 PERROR("pthread_join health_thread");
4013 utils_close_pipe(health_quit_pipe
);
4014 exit_health_quit_pipe
:
4017 health_app_destroy(health_relayd
);
4018 exit_health_app_create
:
4021 * Wait for all pending call_rcu work to complete before tearing
4022 * down data structures. call_rcu worker may be trying to
4023 * perform lookups in those structures.
4028 /* Ensure all prior call_rcu are done. */