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>
40 #include <urcu/rculist.h>
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/compat/getenv.h>
51 #include <common/defaults.h>
52 #include <common/daemonize.h>
53 #include <common/futex.h>
54 #include <common/sessiond-comm/sessiond-comm.h>
55 #include <common/sessiond-comm/inet.h>
56 #include <common/sessiond-comm/relayd.h>
57 #include <common/uri.h>
58 #include <common/utils.h>
59 #include <common/align.h>
60 #include <common/config/session-config.h>
61 #include <common/dynamic-buffer.h>
62 #include <common/buffer-view.h>
63 #include <common/string-utils/format.h>
66 #include "ctf-trace.h"
69 #include "lttng-relayd.h"
71 #include "health-relayd.h"
72 #include "testpoint.h"
73 #include "viewer-stream.h"
76 #include "connection.h"
77 #include "tracefile-array.h"
78 #include "tcp_keep_alive.h"
79 #include "sessiond-trace-chunks.h"
81 static const char *help_msg
=
82 #ifdef LTTNG_EMBED_HELP
83 #include <lttng-relayd.8.h>
89 enum relay_connection_status
{
90 RELAY_CONNECTION_STATUS_OK
,
91 /* An error occurred while processing an event on the connection. */
92 RELAY_CONNECTION_STATUS_ERROR
,
93 /* Connection closed/shutdown cleanly. */
94 RELAY_CONNECTION_STATUS_CLOSED
,
97 /* command line options */
98 char *opt_output_path
;
99 static int opt_daemon
, opt_background
;
102 * We need to wait for listener and live listener threads, as well as
103 * health check thread, before being ready to signal readiness.
105 #define NR_LTTNG_RELAY_READY 3
106 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
108 /* Size of receive buffer. */
109 #define RECV_DATA_BUFFER_SIZE 65536
111 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
112 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
114 static struct lttng_uri
*control_uri
;
115 static struct lttng_uri
*data_uri
;
116 static struct lttng_uri
*live_uri
;
118 const char *progname
;
120 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
121 static int tracing_group_name_override
;
123 const char * const config_section_name
= "relayd";
126 * Quit pipe for all threads. This permits a single cancellation point
127 * for all threads when receiving an event on the pipe.
129 int thread_quit_pipe
[2] = { -1, -1 };
132 * This pipe is used to inform the worker thread that a command is queued and
133 * ready to be processed.
135 static int relay_conn_pipe
[2] = { -1, -1 };
137 /* Shared between threads */
138 static int dispatch_thread_exit
;
140 static pthread_t listener_thread
;
141 static pthread_t dispatcher_thread
;
142 static pthread_t worker_thread
;
143 static pthread_t health_thread
;
146 * last_relay_stream_id_lock protects last_relay_stream_id increment
147 * atomicity on 32-bit architectures.
149 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
150 static uint64_t last_relay_stream_id
;
153 * Relay command queue.
155 * The relay_thread_listener and relay_thread_dispatcher communicate with this
158 static struct relay_conn_queue relay_conn_queue
;
160 /* Global relay stream hash table. */
161 struct lttng_ht
*relay_streams_ht
;
163 /* Global relay viewer stream hash table. */
164 struct lttng_ht
*viewer_streams_ht
;
166 /* Global relay sessions hash table. */
167 struct lttng_ht
*sessions_ht
;
169 /* Relayd health monitoring */
170 struct health_app
*health_relayd
;
172 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
174 static struct option long_options
[] = {
175 { "control-port", 1, 0, 'C', },
176 { "data-port", 1, 0, 'D', },
177 { "live-port", 1, 0, 'L', },
178 { "daemonize", 0, 0, 'd', },
179 { "background", 0, 0, 'b', },
180 { "group", 1, 0, 'g', },
181 { "help", 0, 0, 'h', },
182 { "output", 1, 0, 'o', },
183 { "verbose", 0, 0, 'v', },
184 { "config", 1, 0, 'f' },
185 { "version", 0, 0, 'V' },
189 static const char *config_ignore_options
[] = { "help", "config", "version" };
192 * Take an option from the getopt output and set it in the right variable to be
195 * Return 0 on success else a negative value.
197 static int set_option(int opt
, const char *arg
, const char *optname
)
203 fprintf(stderr
, "option %s", optname
);
205 fprintf(stderr
, " with arg %s\n", arg
);
209 if (lttng_is_setuid_setgid()) {
210 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
211 "-C, --control-port");
213 ret
= uri_parse(arg
, &control_uri
);
215 ERR("Invalid control URI specified");
218 if (control_uri
->port
== 0) {
219 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
224 if (lttng_is_setuid_setgid()) {
225 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
228 ret
= uri_parse(arg
, &data_uri
);
230 ERR("Invalid data URI specified");
233 if (data_uri
->port
== 0) {
234 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
239 if (lttng_is_setuid_setgid()) {
240 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
243 ret
= uri_parse(arg
, &live_uri
);
245 ERR("Invalid live URI specified");
248 if (live_uri
->port
== 0) {
249 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
260 if (lttng_is_setuid_setgid()) {
261 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
264 tracing_group_name
= strdup(arg
);
265 if (tracing_group_name
== NULL
) {
270 tracing_group_name_override
= 1;
274 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
276 ERR("Cannot show --help for `lttng-relayd`");
281 fprintf(stdout
, "%s\n", VERSION
);
284 if (lttng_is_setuid_setgid()) {
285 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
288 ret
= asprintf(&opt_output_path
, "%s", arg
);
291 PERROR("asprintf opt_output_path");
297 /* Verbose level can increase using multiple -v */
299 lttng_opt_verbose
= config_parse_value(arg
);
301 /* Only 3 level of verbosity (-vvv). */
302 if (lttng_opt_verbose
< 3) {
303 lttng_opt_verbose
+= 1;
308 /* Unknown option or other error.
309 * Error is printed by getopt, just return */
322 * config_entry_handler_cb used to handle options read from a config file.
323 * See config_entry_handler_cb comment in common/config/session-config.h for the
324 * return value conventions.
326 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
330 if (!entry
|| !entry
->name
|| !entry
->value
) {
335 /* Check if the option is to be ignored */
336 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
337 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
342 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
343 /* Ignore if entry name is not fully matched. */
344 if (strcmp(entry
->name
, long_options
[i
].name
)) {
349 * If the option takes no argument on the command line,
350 * we have to check if the value is "true". We support
351 * non-zero numeric values, true, on and yes.
353 if (!long_options
[i
].has_arg
) {
354 ret
= config_parse_value(entry
->value
);
357 WARN("Invalid configuration value \"%s\" for option %s",
358 entry
->value
, entry
->name
);
360 /* False, skip boolean config option. */
365 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
369 WARN("Unrecognized option \"%s\" in daemon configuration file.",
376 static int set_options(int argc
, char **argv
)
378 int c
, ret
= 0, option_index
= 0, retval
= 0;
379 int orig_optopt
= optopt
, orig_optind
= optind
;
380 char *default_address
, *optstring
;
381 const char *config_path
= NULL
;
383 optstring
= utils_generate_optstring(long_options
,
384 sizeof(long_options
) / sizeof(struct option
));
390 /* Check for the --config option */
392 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
393 &option_index
)) != -1) {
397 } else if (c
!= 'f') {
401 if (lttng_is_setuid_setgid()) {
402 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
405 config_path
= utils_expand_path(optarg
);
407 ERR("Failed to resolve path: %s", optarg
);
412 ret
= config_get_section_entries(config_path
, config_section_name
,
413 config_entry_handler
, NULL
);
416 ERR("Invalid configuration option at line %i", ret
);
422 /* Reset getopt's global state */
423 optopt
= orig_optopt
;
424 optind
= orig_optind
;
426 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
431 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
438 /* assign default values */
439 if (control_uri
== NULL
) {
440 ret
= asprintf(&default_address
,
441 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
442 DEFAULT_NETWORK_CONTROL_PORT
);
444 PERROR("asprintf default data address");
449 ret
= uri_parse(default_address
, &control_uri
);
450 free(default_address
);
452 ERR("Invalid control URI specified");
457 if (data_uri
== NULL
) {
458 ret
= asprintf(&default_address
,
459 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
460 DEFAULT_NETWORK_DATA_PORT
);
462 PERROR("asprintf default data address");
467 ret
= uri_parse(default_address
, &data_uri
);
468 free(default_address
);
470 ERR("Invalid data URI specified");
475 if (live_uri
== NULL
) {
476 ret
= asprintf(&default_address
,
477 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
478 DEFAULT_NETWORK_VIEWER_PORT
);
480 PERROR("asprintf default viewer control address");
485 ret
= uri_parse(default_address
, &live_uri
);
486 free(default_address
);
488 ERR("Invalid viewer control URI specified");
499 static void print_global_objects(void)
501 rcu_register_thread();
503 print_viewer_streams();
504 print_relay_streams();
507 rcu_unregister_thread();
513 static void relayd_cleanup(void)
515 print_global_objects();
519 if (viewer_streams_ht
)
520 lttng_ht_destroy(viewer_streams_ht
);
521 if (relay_streams_ht
)
522 lttng_ht_destroy(relay_streams_ht
);
524 lttng_ht_destroy(sessions_ht
);
526 /* free the dynamically allocated opt_output_path */
527 free(opt_output_path
);
529 /* Close thread quit pipes */
530 utils_close_pipe(thread_quit_pipe
);
532 uri_free(control_uri
);
534 /* Live URI is freed in the live thread. */
536 if (tracing_group_name_override
) {
537 free((void *) tracing_group_name
);
542 * Write to writable pipe used to notify a thread.
544 static int notify_thread_pipe(int wpipe
)
548 ret
= lttng_write(wpipe
, "!", 1);
550 PERROR("write poll pipe");
558 static int notify_health_quit_pipe(int *pipe
)
562 ret
= lttng_write(pipe
[1], "4", 1);
564 PERROR("write relay health quit");
573 * Stop all relayd and relayd-live threads.
575 int lttng_relay_stop_threads(void)
579 /* Stopping all threads */
580 DBG("Terminating all threads");
581 if (notify_thread_pipe(thread_quit_pipe
[1])) {
582 ERR("write error on thread quit pipe");
586 if (notify_health_quit_pipe(health_quit_pipe
)) {
587 ERR("write error on health quit pipe");
590 /* Dispatch thread */
591 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
592 futex_nto1_wake(&relay_conn_queue
.futex
);
594 if (relayd_live_stop()) {
595 ERR("Error stopping live threads");
602 * Signal handler for the daemon
604 * Simply stop all worker threads, leaving main() return gracefully after
605 * joining all threads and calling cleanup().
607 static void sighandler(int sig
)
611 DBG("SIGINT caught");
612 if (lttng_relay_stop_threads()) {
613 ERR("Error stopping threads");
617 DBG("SIGTERM caught");
618 if (lttng_relay_stop_threads()) {
619 ERR("Error stopping threads");
623 CMM_STORE_SHARED(recv_child_signal
, 1);
631 * Setup signal handler for :
632 * SIGINT, SIGTERM, SIGPIPE
634 static int set_signal_handler(void)
640 if ((ret
= sigemptyset(&sigset
)) < 0) {
641 PERROR("sigemptyset");
648 sa
.sa_handler
= sighandler
;
649 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
654 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
659 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
664 sa
.sa_handler
= SIG_IGN
;
665 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
670 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
675 void lttng_relay_notify_ready(void)
677 /* Notify the parent of the fork() process that we are ready. */
678 if (opt_daemon
|| opt_background
) {
679 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
680 kill(child_ppid
, SIGUSR1
);
686 * Init thread quit pipe.
688 * Return -1 on error or 0 if all pipes are created.
690 static int init_thread_quit_pipe(void)
694 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
700 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
702 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
706 if (events
== NULL
|| size
== 0) {
711 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
717 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
729 * Check if the thread quit pipe was triggered.
731 * Return 1 if it was triggered else 0;
733 static int check_thread_quit_pipe(int fd
, uint32_t events
)
735 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
743 * Create and init socket from uri.
745 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
748 struct lttcomm_sock
*sock
= NULL
;
750 sock
= lttcomm_alloc_sock_from_uri(uri
);
752 ERR("Allocating socket");
756 ret
= lttcomm_create_sock(sock
);
760 DBG("Listening on sock %d", sock
->fd
);
762 ret
= sock
->ops
->bind(sock
);
764 PERROR("Failed to bind socket");
768 ret
= sock
->ops
->listen(sock
, -1);
778 lttcomm_destroy_sock(sock
);
784 * This thread manages the listening for new connections on the network
786 static void *relay_thread_listener(void *data
)
788 int i
, ret
, pollfd
, err
= -1;
789 uint32_t revents
, nb_fd
;
790 struct lttng_poll_event events
;
791 struct lttcomm_sock
*control_sock
, *data_sock
;
793 DBG("[thread] Relay listener started");
795 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
797 health_code_update();
799 control_sock
= relay_socket_create(control_uri
);
801 goto error_sock_control
;
804 data_sock
= relay_socket_create(data_uri
);
806 goto error_sock_relay
;
810 * Pass 3 as size here for the thread quit pipe, control and
813 ret
= create_thread_poll_set(&events
, 3);
815 goto error_create_poll
;
818 /* Add the control socket */
819 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
824 /* Add the data socket */
825 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
830 lttng_relay_notify_ready();
832 if (testpoint(relayd_thread_listener
)) {
833 goto error_testpoint
;
837 health_code_update();
839 DBG("Listener accepting connections");
843 ret
= lttng_poll_wait(&events
, -1);
847 * Restart interrupted system call.
849 if (errno
== EINTR
) {
857 DBG("Relay new connection received");
858 for (i
= 0; i
< nb_fd
; i
++) {
859 health_code_update();
861 /* Fetch once the poll data */
862 revents
= LTTNG_POLL_GETEV(&events
, i
);
863 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
865 /* Thread quit pipe has been closed. Killing thread. */
866 ret
= check_thread_quit_pipe(pollfd
, revents
);
872 if (revents
& LPOLLIN
) {
874 * A new connection is requested, therefore a
875 * sessiond/consumerd connection is allocated in
876 * this thread, enqueued to a global queue and
877 * dequeued (and freed) in the worker thread.
880 struct relay_connection
*new_conn
;
881 struct lttcomm_sock
*newsock
;
882 enum connection_type type
;
884 if (pollfd
== data_sock
->fd
) {
886 newsock
= data_sock
->ops
->accept(data_sock
);
887 DBG("Relay data connection accepted, socket %d",
890 assert(pollfd
== control_sock
->fd
);
891 type
= RELAY_CONTROL
;
892 newsock
= control_sock
->ops
->accept(control_sock
);
893 DBG("Relay control connection accepted, socket %d",
897 PERROR("accepting sock");
901 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
904 PERROR("setsockopt inet");
905 lttcomm_destroy_sock(newsock
);
909 ret
= socket_apply_keep_alive_config(newsock
->fd
);
911 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
913 lttcomm_destroy_sock(newsock
);
917 new_conn
= connection_create(newsock
, type
);
919 lttcomm_destroy_sock(newsock
);
923 /* Enqueue request for the dispatcher thread. */
924 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
928 * Wake the dispatch queue futex.
929 * Implicit memory barrier with the
930 * exchange in cds_wfcq_enqueue.
932 futex_nto1_wake(&relay_conn_queue
.futex
);
933 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
934 ERR("socket poll error");
937 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
947 lttng_poll_clean(&events
);
949 if (data_sock
->fd
>= 0) {
950 ret
= data_sock
->ops
->close(data_sock
);
955 lttcomm_destroy_sock(data_sock
);
957 if (control_sock
->fd
>= 0) {
958 ret
= control_sock
->ops
->close(control_sock
);
963 lttcomm_destroy_sock(control_sock
);
967 ERR("Health error occurred in %s", __func__
);
969 health_unregister(health_relayd
);
970 DBG("Relay listener thread cleanup complete");
971 lttng_relay_stop_threads();
976 * This thread manages the dispatching of the requests to worker threads
978 static void *relay_thread_dispatcher(void *data
)
982 struct cds_wfcq_node
*node
;
983 struct relay_connection
*new_conn
= NULL
;
985 DBG("[thread] Relay dispatcher started");
987 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
989 if (testpoint(relayd_thread_dispatcher
)) {
990 goto error_testpoint
;
993 health_code_update();
996 health_code_update();
998 /* Atomically prepare the queue futex */
999 futex_nto1_prepare(&relay_conn_queue
.futex
);
1001 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1006 health_code_update();
1008 /* Dequeue commands */
1009 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1010 &relay_conn_queue
.tail
);
1012 DBG("Woken up but nothing in the relay command queue");
1013 /* Continue thread execution */
1016 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1018 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1021 * Inform worker thread of the new request. This
1022 * call is blocking so we can be assured that
1023 * the data will be read at some point in time
1024 * or wait to the end of the world :)
1026 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1028 PERROR("write connection pipe");
1029 connection_put(new_conn
);
1032 } while (node
!= NULL
);
1034 /* Futex wait on queue. Blocking call on futex() */
1035 health_poll_entry();
1036 futex_nto1_wait(&relay_conn_queue
.futex
);
1040 /* Normal exit, no error */
1047 ERR("Health error occurred in %s", __func__
);
1049 health_unregister(health_relayd
);
1050 DBG("Dispatch thread dying");
1051 lttng_relay_stop_threads();
1055 static bool session_streams_have_index(const struct relay_session
*session
)
1057 return session
->minor
>= 4 && !session
->snapshot
;
1061 * Handle the RELAYD_CREATE_SESSION command.
1063 * On success, send back the session id or else return a negative value.
1065 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1066 struct relay_connection
*conn
,
1067 const struct lttng_buffer_view
*payload
)
1071 struct relay_session
*session
= NULL
;
1072 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1073 char session_name
[LTTNG_NAME_MAX
] = {};
1074 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1075 uint32_t live_timer
= 0;
1076 bool snapshot
= false;
1077 bool session_name_contains_creation_timestamp
= false;
1078 /* Left nil for peers < 2.11. */
1079 char base_path
[LTTNG_PATH_MAX
] = {};
1080 lttng_uuid sessiond_uuid
= {};
1081 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1082 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1083 LTTNG_OPTIONAL(time_t) creation_time
= {};
1084 struct lttng_dynamic_buffer reply_payload
;
1086 lttng_dynamic_buffer_init(&reply_payload
);
1088 if (conn
->minor
< 4) {
1089 /* From 2.1 to 2.3 */
1091 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1092 /* From 2.4 to 2.10 */
1093 ret
= cmd_create_session_2_4(payload
, session_name
,
1094 hostname
, &live_timer
, &snapshot
);
1096 bool has_current_chunk
;
1097 uint64_t current_chunk_id_value
;
1098 time_t creation_time_value
;
1099 uint64_t id_sessiond_value
;
1101 /* From 2.11 to ... */
1102 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1103 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1104 sessiond_uuid
, &has_current_chunk
,
1105 ¤t_chunk_id_value
, &creation_time_value
,
1106 &session_name_contains_creation_timestamp
);
1107 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1108 /* The nil UUID is reserved for pre-2.11 clients. */
1109 ERR("Illegal nil UUID announced by peer in create session command");
1113 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1114 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1115 if (has_current_chunk
) {
1116 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1117 current_chunk_id_value
);
1125 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1126 snapshot
, sessiond_uuid
,
1127 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1128 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1129 creation_time
.is_set
? &creation_time
.value
: NULL
,
1130 conn
->major
, conn
->minor
,
1131 session_name_contains_creation_timestamp
);
1136 assert(!conn
->session
);
1137 conn
->session
= session
;
1138 DBG("Created session %" PRIu64
, session
->id
);
1140 reply
.generic
.session_id
= htobe64(session
->id
);
1144 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1146 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1149 if (conn
->minor
< 11) {
1150 /* From 2.1 to 2.10 */
1151 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1152 &reply
.generic
, sizeof(reply
.generic
));
1154 ERR("Failed to append \"create session\" command reply header to payload buffer");
1159 const uint32_t output_path_length
=
1160 session
? strlen(session
->output_path
) + 1 : 0;
1162 reply
.output_path_length
= htobe32(output_path_length
);
1163 ret
= lttng_dynamic_buffer_append(
1164 &reply_payload
, &reply
, sizeof(reply
));
1166 ERR("Failed to append \"create session\" command reply header to payload buffer");
1170 if (output_path_length
) {
1171 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1172 session
->output_path
,
1173 output_path_length
);
1175 ERR("Failed to append \"create session\" command reply path to payload buffer");
1181 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1182 reply_payload
.size
, 0);
1183 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1184 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1185 reply_payload
.size
, send_ret
);
1189 if (ret
< 0 && session
) {
1190 session_put(session
);
1192 lttng_dynamic_buffer_reset(&reply_payload
);
1197 * When we have received all the streams and the metadata for a channel,
1198 * we make them visible to the viewer threads.
1200 static void publish_connection_local_streams(struct relay_connection
*conn
)
1202 struct relay_stream
*stream
;
1203 struct relay_session
*session
= conn
->session
;
1206 * We publish all streams belonging to a session atomically wrt
1209 pthread_mutex_lock(&session
->lock
);
1211 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1213 stream_publish(stream
);
1218 * Inform the viewer that there are new streams in the session.
1220 if (session
->viewer_attached
) {
1221 uatomic_set(&session
->new_streams
, 1);
1223 pthread_mutex_unlock(&session
->lock
);
1226 static int conform_channel_path(char *channel_path
)
1230 if (strstr("../", channel_path
)) {
1231 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1237 if (*channel_path
== '/') {
1238 const size_t len
= strlen(channel_path
);
1241 * Channel paths from peers prior to 2.11 are expressed as an
1242 * absolute path that is, in reality, relative to the relay
1243 * daemon's output directory. Remove the leading slash so it
1244 * is correctly interpreted as a relative path later on.
1246 * len (and not len - 1) is used to copy the trailing NULL.
1248 bcopy(channel_path
+ 1, channel_path
, len
);
1255 * relay_add_stream: allocate a new stream for a session
1257 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1258 struct relay_connection
*conn
,
1259 const struct lttng_buffer_view
*payload
)
1263 struct relay_session
*session
= conn
->session
;
1264 struct relay_stream
*stream
= NULL
;
1265 struct lttcomm_relayd_status_stream reply
;
1266 struct ctf_trace
*trace
= NULL
;
1267 uint64_t stream_handle
= -1ULL;
1268 char *path_name
= NULL
, *channel_name
= NULL
;
1269 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1270 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1272 if (!session
|| !conn
->version_check_done
) {
1273 ERR("Trying to add a stream before version check");
1275 goto end_no_session
;
1278 if (session
->minor
== 1) {
1280 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1282 } else if (session
->minor
> 1 && session
->minor
< 11) {
1283 /* From 2.2 to 2.10 */
1284 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1285 &channel_name
, &tracefile_size
, &tracefile_count
);
1287 /* From 2.11 to ... */
1288 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1289 &channel_name
, &tracefile_size
, &tracefile_count
,
1290 &stream_chunk_id
.value
);
1291 stream_chunk_id
.is_set
= true;
1298 if (conform_channel_path(path_name
)) {
1302 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1306 /* This stream here has one reference on the trace. */
1308 pthread_mutex_lock(&last_relay_stream_id_lock
);
1309 stream_handle
= ++last_relay_stream_id
;
1310 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1312 /* We pass ownership of path_name and channel_name. */
1313 stream
= stream_create(trace
, stream_handle
, path_name
,
1314 channel_name
, tracefile_size
, tracefile_count
);
1316 channel_name
= NULL
;
1319 * Streams are the owners of their trace. Reference to trace is
1320 * kept within stream_create().
1322 ctf_trace_put(trace
);
1325 memset(&reply
, 0, sizeof(reply
));
1326 reply
.handle
= htobe64(stream_handle
);
1328 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1330 reply
.ret_code
= htobe32(LTTNG_OK
);
1333 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1334 sizeof(struct lttcomm_relayd_status_stream
), 0);
1335 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1336 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1348 * relay_close_stream: close a specific stream
1350 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1351 struct relay_connection
*conn
,
1352 const struct lttng_buffer_view
*payload
)
1356 struct relay_session
*session
= conn
->session
;
1357 struct lttcomm_relayd_close_stream stream_info
;
1358 struct lttcomm_relayd_generic_reply reply
;
1359 struct relay_stream
*stream
;
1361 DBG("Close stream received");
1363 if (!session
|| !conn
->version_check_done
) {
1364 ERR("Trying to close a stream before version check");
1366 goto end_no_session
;
1369 if (payload
->size
< sizeof(stream_info
)) {
1370 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1371 sizeof(stream_info
), payload
->size
);
1373 goto end_no_session
;
1375 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1376 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1377 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1379 stream
= stream_get_by_id(stream_info
.stream_id
);
1386 * Set last_net_seq_num before the close flag. Required by data
1389 pthread_mutex_lock(&stream
->lock
);
1390 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1391 pthread_mutex_unlock(&stream
->lock
);
1394 * This is one of the conditions which may trigger a stream close
1395 * with the others being:
1396 * 1) A close command is received for a stream
1397 * 2) The control connection owning the stream is closed
1398 * 3) We have received all of the stream's data _after_ a close
1401 try_stream_close(stream
);
1402 if (stream
->is_metadata
) {
1403 struct relay_viewer_stream
*vstream
;
1405 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1407 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1409 * Since all the metadata has been sent to the
1410 * viewer and that we have a request to close
1411 * its stream, we can safely teardown the
1412 * corresponding metadata viewer stream.
1414 viewer_stream_put(vstream
);
1416 /* Put local reference. */
1417 viewer_stream_put(vstream
);
1424 memset(&reply
, 0, sizeof(reply
));
1426 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1428 reply
.ret_code
= htobe32(LTTNG_OK
);
1430 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1431 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1432 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1433 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1443 * relay_reset_metadata: reset a metadata stream
1446 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1447 struct relay_connection
*conn
,
1448 const struct lttng_buffer_view
*payload
)
1452 struct relay_session
*session
= conn
->session
;
1453 struct lttcomm_relayd_reset_metadata stream_info
;
1454 struct lttcomm_relayd_generic_reply reply
;
1455 struct relay_stream
*stream
;
1457 DBG("Reset metadata received");
1459 if (!session
|| !conn
->version_check_done
) {
1460 ERR("Trying to reset a metadata stream before version check");
1462 goto end_no_session
;
1465 if (payload
->size
< sizeof(stream_info
)) {
1466 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1467 sizeof(stream_info
), payload
->size
);
1469 goto end_no_session
;
1471 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1472 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1473 stream_info
.version
= be64toh(stream_info
.version
);
1475 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1477 /* Unsupported for live sessions for now. */
1478 if (session
->live_timer
!= 0) {
1483 stream
= stream_get_by_id(stream_info
.stream_id
);
1488 pthread_mutex_lock(&stream
->lock
);
1489 if (!stream
->is_metadata
) {
1494 ret
= stream_reset_file(stream
);
1496 ERR("Failed to reset metadata stream %" PRIu64
1497 ": stream_path = %s, channel = %s",
1498 stream
->stream_handle
, stream
->path_name
,
1499 stream
->channel_name
);
1503 pthread_mutex_unlock(&stream
->lock
);
1507 memset(&reply
, 0, sizeof(reply
));
1509 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1511 reply
.ret_code
= htobe32(LTTNG_OK
);
1513 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1514 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1515 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1516 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1526 * relay_unknown_command: send -1 if received unknown command
1528 static void relay_unknown_command(struct relay_connection
*conn
)
1530 struct lttcomm_relayd_generic_reply reply
;
1533 memset(&reply
, 0, sizeof(reply
));
1534 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1535 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1536 if (send_ret
< sizeof(reply
)) {
1537 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1542 * relay_start: send an acknowledgment to the client to tell if we are
1543 * ready to receive data. We are ready if a session is established.
1545 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1546 struct relay_connection
*conn
,
1547 const struct lttng_buffer_view
*payload
)
1551 struct lttcomm_relayd_generic_reply reply
;
1552 struct relay_session
*session
= conn
->session
;
1555 DBG("Trying to start the streaming without a session established");
1556 ret
= htobe32(LTTNG_ERR_UNK
);
1559 memset(&reply
, 0, sizeof(reply
));
1560 reply
.ret_code
= htobe32(LTTNG_OK
);
1561 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1563 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1564 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1573 * relay_recv_metadata: receive the metadata for the session.
1575 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1576 struct relay_connection
*conn
,
1577 const struct lttng_buffer_view
*payload
)
1580 struct relay_session
*session
= conn
->session
;
1581 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1582 struct relay_stream
*metadata_stream
;
1583 uint64_t metadata_payload_size
;
1584 struct lttng_buffer_view packet_view
;
1587 ERR("Metadata sent before version check");
1592 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1593 ERR("Incorrect data size");
1597 metadata_payload_size
= recv_hdr
->data_size
-
1598 sizeof(struct lttcomm_relayd_metadata_payload
);
1600 memcpy(&metadata_payload_header
, payload
->data
,
1601 sizeof(metadata_payload_header
));
1602 metadata_payload_header
.stream_id
= be64toh(
1603 metadata_payload_header
.stream_id
);
1604 metadata_payload_header
.padding_size
= be32toh(
1605 metadata_payload_header
.padding_size
);
1607 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1608 if (!metadata_stream
) {
1613 packet_view
= lttng_buffer_view_from_view(payload
,
1614 sizeof(metadata_payload_header
), metadata_payload_size
);
1615 if (!packet_view
.data
) {
1616 ERR("Invalid metadata packet length announced by header");
1621 pthread_mutex_lock(&metadata_stream
->lock
);
1622 ret
= stream_write(metadata_stream
, &packet_view
,
1623 metadata_payload_header
.padding_size
);
1624 pthread_mutex_unlock(&metadata_stream
->lock
);
1630 stream_put(metadata_stream
);
1636 * relay_send_version: send relayd version number
1638 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1639 struct relay_connection
*conn
,
1640 const struct lttng_buffer_view
*payload
)
1644 struct lttcomm_relayd_version reply
, msg
;
1645 bool compatible
= true;
1647 conn
->version_check_done
= true;
1649 /* Get version from the other side. */
1650 if (payload
->size
< sizeof(msg
)) {
1651 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1652 sizeof(msg
), payload
->size
);
1657 memcpy(&msg
, payload
->data
, sizeof(msg
));
1658 msg
.major
= be32toh(msg
.major
);
1659 msg
.minor
= be32toh(msg
.minor
);
1661 memset(&reply
, 0, sizeof(reply
));
1662 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1663 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1665 /* Major versions must be the same */
1666 if (reply
.major
!= msg
.major
) {
1667 DBG("Incompatible major versions (%u vs %u), deleting session",
1668 reply
.major
, msg
.major
);
1672 conn
->major
= reply
.major
;
1673 /* We adapt to the lowest compatible version */
1674 if (reply
.minor
<= msg
.minor
) {
1675 conn
->minor
= reply
.minor
;
1677 conn
->minor
= msg
.minor
;
1680 reply
.major
= htobe32(reply
.major
);
1681 reply
.minor
= htobe32(reply
.minor
);
1682 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1684 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1685 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1698 DBG("Version check done using protocol %u.%u", conn
->major
,
1706 * Check for data pending for a given stream id from the session daemon.
1708 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1709 struct relay_connection
*conn
,
1710 const struct lttng_buffer_view
*payload
)
1712 struct relay_session
*session
= conn
->session
;
1713 struct lttcomm_relayd_data_pending msg
;
1714 struct lttcomm_relayd_generic_reply reply
;
1715 struct relay_stream
*stream
;
1718 uint64_t stream_seq
;
1720 DBG("Data pending command received");
1722 if (!session
|| !conn
->version_check_done
) {
1723 ERR("Trying to check for data before version check");
1725 goto end_no_session
;
1728 if (payload
->size
< sizeof(msg
)) {
1729 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1730 sizeof(msg
), payload
->size
);
1732 goto end_no_session
;
1734 memcpy(&msg
, payload
->data
, sizeof(msg
));
1735 msg
.stream_id
= be64toh(msg
.stream_id
);
1736 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1738 stream
= stream_get_by_id(msg
.stream_id
);
1739 if (stream
== NULL
) {
1744 pthread_mutex_lock(&stream
->lock
);
1746 if (session_streams_have_index(session
)) {
1748 * Ensure that both the index and stream data have been
1749 * flushed up to the requested point.
1751 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1753 stream_seq
= stream
->prev_data_seq
;
1755 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
1756 ", prev_index_seq %" PRIu64
1757 ", and last_seq %" PRIu64
, msg
.stream_id
,
1758 stream
->prev_data_seq
, stream
->prev_index_seq
,
1759 msg
.last_net_seq_num
);
1761 /* Avoid wrapping issue */
1762 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
1763 /* Data has in fact been written and is NOT pending */
1766 /* Data still being streamed thus pending */
1770 stream
->data_pending_check_done
= true;
1771 pthread_mutex_unlock(&stream
->lock
);
1776 memset(&reply
, 0, sizeof(reply
));
1777 reply
.ret_code
= htobe32(ret
);
1778 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1779 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1780 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1790 * Wait for the control socket to reach a quiescent state.
1792 * Note that for now, when receiving this command from the session
1793 * daemon, this means that every subsequent commands or data received on
1794 * the control socket has been handled. So, this is why we simply return
1797 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
1798 struct relay_connection
*conn
,
1799 const struct lttng_buffer_view
*payload
)
1803 struct relay_stream
*stream
;
1804 struct lttcomm_relayd_quiescent_control msg
;
1805 struct lttcomm_relayd_generic_reply reply
;
1807 DBG("Checking quiescent state on control socket");
1809 if (!conn
->session
|| !conn
->version_check_done
) {
1810 ERR("Trying to check for data before version check");
1812 goto end_no_session
;
1815 if (payload
->size
< sizeof(msg
)) {
1816 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
1817 sizeof(msg
), payload
->size
);
1819 goto end_no_session
;
1821 memcpy(&msg
, payload
->data
, sizeof(msg
));
1822 msg
.stream_id
= be64toh(msg
.stream_id
);
1824 stream
= stream_get_by_id(msg
.stream_id
);
1828 pthread_mutex_lock(&stream
->lock
);
1829 stream
->data_pending_check_done
= true;
1830 pthread_mutex_unlock(&stream
->lock
);
1832 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
1835 memset(&reply
, 0, sizeof(reply
));
1836 reply
.ret_code
= htobe32(LTTNG_OK
);
1837 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1838 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1839 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
1851 * Initialize a data pending command. This means that a consumer is about
1852 * to ask for data pending for each stream it holds. Simply iterate over
1853 * all streams of a session and set the data_pending_check_done flag.
1855 * This command returns to the client a LTTNG_OK code.
1857 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1858 struct relay_connection
*conn
,
1859 const struct lttng_buffer_view
*payload
)
1863 struct lttng_ht_iter iter
;
1864 struct lttcomm_relayd_begin_data_pending msg
;
1865 struct lttcomm_relayd_generic_reply reply
;
1866 struct relay_stream
*stream
;
1871 DBG("Init streams for data pending");
1873 if (!conn
->session
|| !conn
->version_check_done
) {
1874 ERR("Trying to check for data before version check");
1876 goto end_no_session
;
1879 if (payload
->size
< sizeof(msg
)) {
1880 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
1881 sizeof(msg
), payload
->size
);
1883 goto end_no_session
;
1885 memcpy(&msg
, payload
->data
, sizeof(msg
));
1886 msg
.session_id
= be64toh(msg
.session_id
);
1889 * Iterate over all streams to set the begin data pending flag.
1890 * For now, the streams are indexed by stream handle so we have
1891 * to iterate over all streams to find the one associated with
1892 * the right session_id.
1895 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1897 if (!stream_get(stream
)) {
1900 if (stream
->trace
->session
->id
== msg
.session_id
) {
1901 pthread_mutex_lock(&stream
->lock
);
1902 stream
->data_pending_check_done
= false;
1903 pthread_mutex_unlock(&stream
->lock
);
1904 DBG("Set begin data pending flag to stream %" PRIu64
,
1905 stream
->stream_handle
);
1911 memset(&reply
, 0, sizeof(reply
));
1912 /* All good, send back reply. */
1913 reply
.ret_code
= htobe32(LTTNG_OK
);
1915 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1916 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1917 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
1929 * End data pending command. This will check, for a given session id, if
1930 * each stream associated with it has its data_pending_check_done flag
1931 * set. If not, this means that the client lost track of the stream but
1932 * the data is still being streamed on our side. In this case, we inform
1933 * the client that data is in flight.
1935 * Return to the client if there is data in flight or not with a ret_code.
1937 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1938 struct relay_connection
*conn
,
1939 const struct lttng_buffer_view
*payload
)
1943 struct lttng_ht_iter iter
;
1944 struct lttcomm_relayd_end_data_pending msg
;
1945 struct lttcomm_relayd_generic_reply reply
;
1946 struct relay_stream
*stream
;
1947 uint32_t is_data_inflight
= 0;
1949 DBG("End data pending command");
1951 if (!conn
->session
|| !conn
->version_check_done
) {
1952 ERR("Trying to check for data before version check");
1954 goto end_no_session
;
1957 if (payload
->size
< sizeof(msg
)) {
1958 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
1959 sizeof(msg
), payload
->size
);
1961 goto end_no_session
;
1963 memcpy(&msg
, payload
->data
, sizeof(msg
));
1964 msg
.session_id
= be64toh(msg
.session_id
);
1967 * Iterate over all streams to see if the begin data pending
1971 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1973 if (!stream_get(stream
)) {
1976 if (stream
->trace
->session
->id
!= msg
.session_id
) {
1980 pthread_mutex_lock(&stream
->lock
);
1981 if (!stream
->data_pending_check_done
) {
1982 uint64_t stream_seq
;
1984 if (session_streams_have_index(conn
->session
)) {
1986 * Ensure that both the index and stream data have been
1987 * flushed up to the requested point.
1989 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1991 stream_seq
= stream
->prev_data_seq
;
1993 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
1994 is_data_inflight
= 1;
1995 DBG("Data is still in flight for stream %" PRIu64
,
1996 stream
->stream_handle
);
1997 pthread_mutex_unlock(&stream
->lock
);
2002 pthread_mutex_unlock(&stream
->lock
);
2007 memset(&reply
, 0, sizeof(reply
));
2008 /* All good, send back reply. */
2009 reply
.ret_code
= htobe32(is_data_inflight
);
2011 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2012 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2013 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2025 * Receive an index for a specific stream.
2027 * Return 0 on success else a negative value.
2029 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2030 struct relay_connection
*conn
,
2031 const struct lttng_buffer_view
*payload
)
2035 struct relay_session
*session
= conn
->session
;
2036 struct lttcomm_relayd_index index_info
;
2037 struct lttcomm_relayd_generic_reply reply
;
2038 struct relay_stream
*stream
;
2043 DBG("Relay receiving index");
2045 if (!session
|| !conn
->version_check_done
) {
2046 ERR("Trying to close a stream before version check");
2048 goto end_no_session
;
2051 msg_len
= lttcomm_relayd_index_len(
2052 lttng_to_index_major(conn
->major
, conn
->minor
),
2053 lttng_to_index_minor(conn
->major
, conn
->minor
));
2054 if (payload
->size
< msg_len
) {
2055 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2056 msg_len
, payload
->size
);
2058 goto end_no_session
;
2060 memcpy(&index_info
, payload
->data
, msg_len
);
2061 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2062 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2063 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2064 index_info
.content_size
= be64toh(index_info
.content_size
);
2065 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2066 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2067 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2068 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2070 if (conn
->minor
>= 8) {
2071 index_info
.stream_instance_id
=
2072 be64toh(index_info
.stream_instance_id
);
2073 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2076 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2078 ERR("stream_get_by_id not found");
2083 pthread_mutex_lock(&stream
->lock
);
2084 ret
= stream_add_index(stream
, &index_info
);
2085 pthread_mutex_unlock(&stream
->lock
);
2087 goto end_stream_put
;
2093 memset(&reply
, 0, sizeof(reply
));
2095 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2097 reply
.ret_code
= htobe32(LTTNG_OK
);
2099 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2100 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2101 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2110 * Receive the streams_sent message.
2112 * Return 0 on success else a negative value.
2114 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2115 struct relay_connection
*conn
,
2116 const struct lttng_buffer_view
*payload
)
2120 struct lttcomm_relayd_generic_reply reply
;
2124 DBG("Relay receiving streams_sent");
2126 if (!conn
->session
|| !conn
->version_check_done
) {
2127 ERR("Trying to close a stream before version check");
2129 goto end_no_session
;
2133 * Publish every pending stream in the connection recv list which are
2134 * now ready to be used by the viewer.
2136 publish_connection_local_streams(conn
);
2138 memset(&reply
, 0, sizeof(reply
));
2139 reply
.ret_code
= htobe32(LTTNG_OK
);
2140 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2141 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2142 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2155 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2156 * session rotation feature (not the tracefile rotation feature).
2158 static int relay_rotate_session_streams(
2159 const struct lttcomm_relayd_hdr
*recv_hdr
,
2160 struct relay_connection
*conn
,
2161 const struct lttng_buffer_view
*payload
)
2166 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2167 struct relay_session
*session
= conn
->session
;
2168 struct lttcomm_relayd_rotate_streams rotate_streams
;
2169 struct lttcomm_relayd_generic_reply reply
= {};
2170 struct relay_stream
*stream
= NULL
;
2171 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2172 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2173 struct lttng_buffer_view stream_positions
;
2174 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2175 const char *chunk_id_str
= "none";
2177 if (!session
|| !conn
->version_check_done
) {
2178 ERR("Trying to rotate a stream before version check");
2183 if (session
->major
== 2 && session
->minor
< 11) {
2184 ERR("Unsupported feature before 2.11");
2189 if (payload
->size
< header_len
) {
2190 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2191 header_len
, payload
->size
);
2196 memcpy(&rotate_streams
, payload
->data
, header_len
);
2198 /* Convert header to host endianness. */
2199 rotate_streams
= (typeof(rotate_streams
)) {
2200 .stream_count
= be32toh(rotate_streams
.stream_count
),
2201 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2202 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2203 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2207 if (rotate_streams
.new_chunk_id
.is_set
) {
2209 * Retrieve the trace chunk the stream must transition to. As
2210 * per the protocol, this chunk should have been created
2211 * before this command is received.
2213 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2214 sessiond_trace_chunk_registry
,
2215 session
->sessiond_uuid
, session
->id
,
2216 rotate_streams
.new_chunk_id
.value
);
2217 if (!next_trace_chunk
) {
2218 char uuid_str
[UUID_STR_LEN
];
2220 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2221 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2222 ", trace_chunk_id = %" PRIu64
,
2223 uuid_str
, session
->id
,
2224 rotate_streams
.new_chunk_id
.value
);
2225 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2230 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2231 rotate_streams
.new_chunk_id
.value
);
2232 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2233 chunk_id_str
= "formatting error";
2235 chunk_id_str
= chunk_id_buf
;
2237 session
->has_rotated
= true;
2240 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2241 rotate_streams
.stream_count
, session
->session_name
,
2244 stream_positions
= lttng_buffer_view_from_view(payload
,
2245 sizeof(rotate_streams
), -1);
2246 if (!stream_positions
.data
||
2247 stream_positions
.size
<
2248 (rotate_streams
.stream_count
*
2249 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2250 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2255 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2256 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2257 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2258 const struct lttcomm_relayd_stream_rotation_position pos
= {
2259 .stream_id
= be64toh(position_comm
->stream_id
),
2260 .rotate_at_seq_num
= be64toh(
2261 position_comm
->rotate_at_seq_num
),
2264 stream
= stream_get_by_id(pos
.stream_id
);
2266 reply_code
= LTTNG_ERR_INVALID
;
2271 pthread_mutex_lock(&stream
->lock
);
2272 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2273 pos
.rotate_at_seq_num
);
2274 pthread_mutex_unlock(&stream
->lock
);
2276 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2284 reply_code
= LTTNG_OK
;
2290 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2291 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2292 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2293 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2294 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2301 lttng_trace_chunk_put(next_trace_chunk
);
2308 * relay_create_trace_chunk: create a new trace chunk
2310 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2311 struct relay_connection
*conn
,
2312 const struct lttng_buffer_view
*payload
)
2316 struct relay_session
*session
= conn
->session
;
2317 struct lttcomm_relayd_create_trace_chunk
*msg
;
2318 struct lttcomm_relayd_generic_reply reply
= {};
2319 struct lttng_buffer_view header_view
;
2320 struct lttng_buffer_view chunk_name_view
;
2321 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2322 enum lttng_error_code reply_code
= LTTNG_OK
;
2323 enum lttng_trace_chunk_status chunk_status
;
2324 struct lttng_directory_handle session_output
;
2326 if (!session
|| !conn
->version_check_done
) {
2327 ERR("Trying to create a trace chunk before version check");
2332 if (session
->major
== 2 && session
->minor
< 11) {
2333 ERR("Chunk creation command is unsupported before 2.11");
2338 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2339 if (!header_view
.data
) {
2340 ERR("Failed to receive payload of chunk creation command");
2345 /* Convert to host endianness. */
2346 msg
= (typeof(msg
)) header_view
.data
;
2347 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2348 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2349 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2351 chunk
= lttng_trace_chunk_create(
2352 msg
->chunk_id
, msg
->creation_timestamp
);
2354 ERR("Failed to create trace chunk in trace chunk creation command");
2356 reply_code
= LTTNG_ERR_NOMEM
;
2360 if (msg
->override_name_length
) {
2363 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2365 msg
->override_name_length
);
2366 name
= chunk_name_view
.data
;
2367 if (!name
|| name
[msg
->override_name_length
- 1]) {
2368 ERR("Failed to receive payload of chunk creation command");
2370 reply_code
= LTTNG_ERR_INVALID
;
2374 chunk_status
= lttng_trace_chunk_override_name(
2375 chunk
, chunk_name_view
.data
);
2376 switch (chunk_status
) {
2377 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2379 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2380 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2381 reply_code
= LTTNG_ERR_INVALID
;
2385 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2386 reply_code
= LTTNG_ERR_UNK
;
2392 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2393 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2394 reply_code
= LTTNG_ERR_UNK
;
2399 ret
= session_init_output_directory_handle(
2400 conn
->session
, &session_output
);
2402 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2405 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, &session_output
);
2406 lttng_directory_handle_fini(&session_output
);
2407 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2408 reply_code
= LTTNG_ERR_UNK
;
2413 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2414 sessiond_trace_chunk_registry
,
2415 conn
->session
->sessiond_uuid
,
2418 if (!published_chunk
) {
2419 char uuid_str
[UUID_STR_LEN
];
2421 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2422 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2427 reply_code
= LTTNG_ERR_NOMEM
;
2431 pthread_mutex_lock(&conn
->session
->lock
);
2432 if (conn
->session
->pending_closure_trace_chunk
) {
2434 * Invalid; this means a second create_trace_chunk command was
2435 * received before a close_trace_chunk.
2437 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2438 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2440 goto end_unlock_session
;
2442 conn
->session
->pending_closure_trace_chunk
=
2443 conn
->session
->current_trace_chunk
;
2444 conn
->session
->current_trace_chunk
= published_chunk
;
2445 published_chunk
= NULL
;
2447 pthread_mutex_unlock(&conn
->session
->lock
);
2449 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2450 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2452 sizeof(struct lttcomm_relayd_generic_reply
),
2454 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2455 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2460 lttng_trace_chunk_put(chunk
);
2461 lttng_trace_chunk_put(published_chunk
);
2466 * relay_close_trace_chunk: close a trace chunk
2468 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2469 struct relay_connection
*conn
,
2470 const struct lttng_buffer_view
*payload
)
2474 struct relay_session
*session
= conn
->session
;
2475 struct lttcomm_relayd_close_trace_chunk
*msg
;
2476 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2477 struct lttng_buffer_view header_view
;
2478 struct lttng_trace_chunk
*chunk
= NULL
;
2479 enum lttng_error_code reply_code
= LTTNG_OK
;
2480 enum lttng_trace_chunk_status chunk_status
;
2482 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2483 time_t close_timestamp
;
2484 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2485 size_t path_length
= 0;
2486 const char *chunk_name
= NULL
;
2487 struct lttng_dynamic_buffer reply_payload
;
2489 lttng_dynamic_buffer_init(&reply_payload
);
2491 if (!session
|| !conn
->version_check_done
) {
2492 ERR("Trying to close a trace chunk before version check");
2497 if (session
->major
== 2 && session
->minor
< 11) {
2498 ERR("Chunk close command is unsupported before 2.11");
2503 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2504 if (!header_view
.data
) {
2505 ERR("Failed to receive payload of chunk close command");
2510 /* Convert to host endianness. */
2511 msg
= (typeof(msg
)) header_view
.data
;
2512 chunk_id
= be64toh(msg
->chunk_id
);
2513 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2514 close_command
= (typeof(close_command
)){
2515 .value
= be32toh(msg
->close_command
.value
),
2516 .is_set
= msg
->close_command
.is_set
,
2519 chunk
= sessiond_trace_chunk_registry_get_chunk(
2520 sessiond_trace_chunk_registry
,
2521 conn
->session
->sessiond_uuid
,
2525 char uuid_str
[UUID_STR_LEN
];
2527 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2528 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2533 reply_code
= LTTNG_ERR_NOMEM
;
2537 pthread_mutex_lock(&session
->lock
);
2538 if (session
->pending_closure_trace_chunk
&&
2539 session
->pending_closure_trace_chunk
!= chunk
) {
2540 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2541 session
->session_name
);
2542 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2544 goto end_unlock_session
;
2547 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2548 chunk
, close_timestamp
);
2549 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2550 ERR("Failed to set trace chunk close timestamp");
2552 reply_code
= LTTNG_ERR_UNK
;
2553 goto end_unlock_session
;
2556 if (close_command
.is_set
) {
2557 chunk_status
= lttng_trace_chunk_set_close_command(
2558 chunk
, close_command
.value
);
2559 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2561 reply_code
= LTTNG_ERR_INVALID
;
2562 goto end_unlock_session
;
2565 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2566 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2567 ERR("Failed to get chunk name");
2569 reply_code
= LTTNG_ERR_UNK
;
2570 goto end_unlock_session
;
2572 if (!session
->has_rotated
&& !session
->snapshot
) {
2573 ret
= lttng_strncpy(closed_trace_chunk_path
,
2574 session
->output_path
,
2575 sizeof(closed_trace_chunk_path
));
2577 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2578 strlen(session
->output_path
),
2579 sizeof(closed_trace_chunk_path
));
2580 reply_code
= LTTNG_ERR_NOMEM
;
2582 goto end_unlock_session
;
2585 if (session
->snapshot
) {
2586 ret
= snprintf(closed_trace_chunk_path
,
2587 sizeof(closed_trace_chunk_path
),
2588 "%s/%s", session
->output_path
,
2591 ret
= snprintf(closed_trace_chunk_path
,
2592 sizeof(closed_trace_chunk_path
),
2593 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2595 session
->output_path
, chunk_name
);
2597 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2598 ERR("Failed to format closed trace chunk resulting path");
2599 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2601 goto end_unlock_session
;
2604 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2605 path_length
= strlen(closed_trace_chunk_path
) + 1;
2606 if (path_length
> UINT32_MAX
) {
2607 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2609 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2610 goto end_unlock_session
;
2613 if (session
->current_trace_chunk
== chunk
) {
2615 * After a trace chunk close command, no new streams
2616 * referencing the chunk may be created. Hence, on the
2617 * event that no new trace chunk have been created for
2618 * the session, the reference to the current trace chunk
2619 * is released in order to allow it to be reclaimed when
2620 * the last stream releases its reference to it.
2622 lttng_trace_chunk_put(session
->current_trace_chunk
);
2623 session
->current_trace_chunk
= NULL
;
2625 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2626 session
->pending_closure_trace_chunk
= NULL
;
2628 pthread_mutex_unlock(&session
->lock
);
2631 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
2632 reply
.path_length
= htobe32((uint32_t) path_length
);
2633 ret
= lttng_dynamic_buffer_append(
2634 &reply_payload
, &reply
, sizeof(reply
));
2636 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2640 if (reply_code
== LTTNG_OK
) {
2641 ret
= lttng_dynamic_buffer_append(&reply_payload
,
2642 closed_trace_chunk_path
, path_length
);
2644 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2649 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2653 if (send_ret
< reply_payload
.size
) {
2654 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2655 reply_payload
.size
, send_ret
);
2660 lttng_trace_chunk_put(chunk
);
2661 lttng_dynamic_buffer_reset(&reply_payload
);
2666 * relay_trace_chunk_exists: check if a trace chunk exists
2668 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2669 struct relay_connection
*conn
,
2670 const struct lttng_buffer_view
*payload
)
2674 struct relay_session
*session
= conn
->session
;
2675 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2676 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2677 struct lttng_buffer_view header_view
;
2678 struct lttng_trace_chunk
*chunk
= NULL
;
2681 if (!session
|| !conn
->version_check_done
) {
2682 ERR("Trying to close a trace chunk before version check");
2687 if (session
->major
== 2 && session
->minor
< 11) {
2688 ERR("Chunk close command is unsupported before 2.11");
2693 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2694 if (!header_view
.data
) {
2695 ERR("Failed to receive payload of chunk close command");
2700 /* Convert to host endianness. */
2701 msg
= (typeof(msg
)) header_view
.data
;
2702 chunk_id
= be64toh(msg
->chunk_id
);
2704 chunk
= sessiond_trace_chunk_registry_get_chunk(
2705 sessiond_trace_chunk_registry
,
2706 conn
->session
->sessiond_uuid
,
2710 reply
= (typeof(reply
)) {
2711 .generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
),
2712 .trace_chunk_exists
= !!chunk
,
2714 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2715 &reply
, sizeof(reply
), 0);
2716 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2717 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2722 lttng_trace_chunk_put(chunk
);
2726 #define DBG_CMD(cmd_name, conn) \
2727 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2729 static int relay_process_control_command(struct relay_connection
*conn
,
2730 const struct lttcomm_relayd_hdr
*header
,
2731 const struct lttng_buffer_view
*payload
)
2735 switch (header
->cmd
) {
2736 case RELAYD_CREATE_SESSION
:
2737 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2738 ret
= relay_create_session(header
, conn
, payload
);
2740 case RELAYD_ADD_STREAM
:
2741 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2742 ret
= relay_add_stream(header
, conn
, payload
);
2744 case RELAYD_START_DATA
:
2745 DBG_CMD("RELAYD_START_DATA", conn
);
2746 ret
= relay_start(header
, conn
, payload
);
2748 case RELAYD_SEND_METADATA
:
2749 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2750 ret
= relay_recv_metadata(header
, conn
, payload
);
2752 case RELAYD_VERSION
:
2753 DBG_CMD("RELAYD_VERSION", conn
);
2754 ret
= relay_send_version(header
, conn
, payload
);
2756 case RELAYD_CLOSE_STREAM
:
2757 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2758 ret
= relay_close_stream(header
, conn
, payload
);
2760 case RELAYD_DATA_PENDING
:
2761 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2762 ret
= relay_data_pending(header
, conn
, payload
);
2764 case RELAYD_QUIESCENT_CONTROL
:
2765 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2766 ret
= relay_quiescent_control(header
, conn
, payload
);
2768 case RELAYD_BEGIN_DATA_PENDING
:
2769 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2770 ret
= relay_begin_data_pending(header
, conn
, payload
);
2772 case RELAYD_END_DATA_PENDING
:
2773 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2774 ret
= relay_end_data_pending(header
, conn
, payload
);
2776 case RELAYD_SEND_INDEX
:
2777 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2778 ret
= relay_recv_index(header
, conn
, payload
);
2780 case RELAYD_STREAMS_SENT
:
2781 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2782 ret
= relay_streams_sent(header
, conn
, payload
);
2784 case RELAYD_RESET_METADATA
:
2785 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2786 ret
= relay_reset_metadata(header
, conn
, payload
);
2788 case RELAYD_ROTATE_STREAMS
:
2789 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
2790 ret
= relay_rotate_session_streams(header
, conn
, payload
);
2792 case RELAYD_CREATE_TRACE_CHUNK
:
2793 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
2794 ret
= relay_create_trace_chunk(header
, conn
, payload
);
2796 case RELAYD_CLOSE_TRACE_CHUNK
:
2797 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
2798 ret
= relay_close_trace_chunk(header
, conn
, payload
);
2800 case RELAYD_TRACE_CHUNK_EXISTS
:
2801 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
2802 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
2804 case RELAYD_UPDATE_SYNC_INFO
:
2806 ERR("Received unknown command (%u)", header
->cmd
);
2807 relay_unknown_command(conn
);
2816 static enum relay_connection_status
relay_process_control_receive_payload(
2817 struct relay_connection
*conn
)
2820 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2821 struct lttng_dynamic_buffer
*reception_buffer
=
2822 &conn
->protocol
.ctrl
.reception_buffer
;
2823 struct ctrl_connection_state_receive_payload
*state
=
2824 &conn
->protocol
.ctrl
.state
.receive_payload
;
2825 struct lttng_buffer_view payload_view
;
2827 if (state
->left_to_receive
== 0) {
2828 /* Short-circuit for payload-less commands. */
2829 goto reception_complete
;
2832 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2833 reception_buffer
->data
+ state
->received
,
2834 state
->left_to_receive
, MSG_DONTWAIT
);
2836 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2837 PERROR("Unable to receive command payload on sock %d",
2839 status
= RELAY_CONNECTION_STATUS_ERROR
;
2842 } else if (ret
== 0) {
2843 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2844 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2849 assert(ret
<= state
->left_to_receive
);
2851 state
->left_to_receive
-= ret
;
2852 state
->received
+= ret
;
2854 if (state
->left_to_receive
> 0) {
2856 * Can't transition to the protocol's next state, wait to
2857 * receive the rest of the header.
2859 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2860 state
->received
, state
->left_to_receive
,
2866 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
2867 conn
->sock
->fd
, state
->received
);
2869 * The payload required to process the command has been received.
2870 * A view to the reception buffer is forwarded to the various
2871 * commands and the state of the control is reset on success.
2873 * Commands are responsible for sending their reply to the peer.
2875 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
2877 ret
= relay_process_control_command(conn
,
2878 &state
->header
, &payload_view
);
2880 status
= RELAY_CONNECTION_STATUS_ERROR
;
2884 ret
= connection_reset_protocol_state(conn
);
2886 status
= RELAY_CONNECTION_STATUS_ERROR
;
2892 static enum relay_connection_status
relay_process_control_receive_header(
2893 struct relay_connection
*conn
)
2896 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2897 struct lttcomm_relayd_hdr header
;
2898 struct lttng_dynamic_buffer
*reception_buffer
=
2899 &conn
->protocol
.ctrl
.reception_buffer
;
2900 struct ctrl_connection_state_receive_header
*state
=
2901 &conn
->protocol
.ctrl
.state
.receive_header
;
2903 assert(state
->left_to_receive
!= 0);
2905 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2906 reception_buffer
->data
+ state
->received
,
2907 state
->left_to_receive
, MSG_DONTWAIT
);
2909 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2910 PERROR("Unable to receive control command header on sock %d",
2912 status
= RELAY_CONNECTION_STATUS_ERROR
;
2915 } else if (ret
== 0) {
2916 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2917 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2922 assert(ret
<= state
->left_to_receive
);
2924 state
->left_to_receive
-= ret
;
2925 state
->received
+= ret
;
2927 if (state
->left_to_receive
> 0) {
2929 * Can't transition to the protocol's next state, wait to
2930 * receive the rest of the header.
2932 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2933 state
->received
, state
->left_to_receive
,
2938 /* Transition to next state: receiving the command's payload. */
2939 conn
->protocol
.ctrl
.state_id
=
2940 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
2941 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
2942 header
.circuit_id
= be64toh(header
.circuit_id
);
2943 header
.data_size
= be64toh(header
.data_size
);
2944 header
.cmd
= be32toh(header
.cmd
);
2945 header
.cmd_version
= be32toh(header
.cmd_version
);
2946 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
2947 &header
, sizeof(header
));
2949 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
2950 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
2953 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
2954 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
2956 status
= RELAY_CONNECTION_STATUS_ERROR
;
2960 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
2962 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
2963 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
2966 status
= RELAY_CONNECTION_STATUS_ERROR
;
2970 if (header
.data_size
== 0) {
2972 * Manually invoke the next state as the poll loop
2973 * will not wake-up to allow us to proceed further.
2975 status
= relay_process_control_receive_payload(conn
);
2982 * Process the commands received on the control socket
2984 static enum relay_connection_status
relay_process_control(
2985 struct relay_connection
*conn
)
2987 enum relay_connection_status status
;
2989 switch (conn
->protocol
.ctrl
.state_id
) {
2990 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
2991 status
= relay_process_control_receive_header(conn
);
2993 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
2994 status
= relay_process_control_receive_payload(conn
);
2997 ERR("Unknown control connection protocol state encountered.");
3004 static enum relay_connection_status
relay_process_data_receive_header(
3005 struct relay_connection
*conn
)
3008 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3009 struct data_connection_state_receive_header
*state
=
3010 &conn
->protocol
.data
.state
.receive_header
;
3011 struct lttcomm_relayd_data_hdr header
;
3012 struct relay_stream
*stream
;
3014 assert(state
->left_to_receive
!= 0);
3016 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3017 state
->header_reception_buffer
+ state
->received
,
3018 state
->left_to_receive
, MSG_DONTWAIT
);
3020 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3021 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3022 status
= RELAY_CONNECTION_STATUS_ERROR
;
3025 } else if (ret
== 0) {
3026 /* Orderly shutdown. Not necessary to print an error. */
3027 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3028 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3033 assert(ret
<= state
->left_to_receive
);
3035 state
->left_to_receive
-= ret
;
3036 state
->received
+= ret
;
3038 if (state
->left_to_receive
> 0) {
3040 * Can't transition to the protocol's next state, wait to
3041 * receive the rest of the header.
3043 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3044 state
->received
, state
->left_to_receive
,
3049 /* Transition to next state: receiving the payload. */
3050 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3052 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3053 header
.circuit_id
= be64toh(header
.circuit_id
);
3054 header
.stream_id
= be64toh(header
.stream_id
);
3055 header
.data_size
= be32toh(header
.data_size
);
3056 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3057 header
.padding_size
= be32toh(header
.padding_size
);
3058 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3060 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3062 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3063 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3065 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3066 conn
->sock
->fd
, header
.circuit_id
,
3067 header
.stream_id
, header
.data_size
,
3068 header
.net_seq_num
, header
.padding_size
);
3070 stream
= stream_get_by_id(header
.stream_id
);
3072 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3074 /* Protocol error. */
3075 status
= RELAY_CONNECTION_STATUS_ERROR
;
3079 pthread_mutex_lock(&stream
->lock
);
3080 /* Prepare stream for the reception of a new packet. */
3081 ret
= stream_init_packet(stream
, header
.data_size
,
3082 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3083 pthread_mutex_unlock(&stream
->lock
);
3085 ERR("Failed to rotate stream output file");
3086 status
= RELAY_CONNECTION_STATUS_ERROR
;
3087 goto end_stream_unlock
;
3096 static enum relay_connection_status
relay_process_data_receive_payload(
3097 struct relay_connection
*conn
)
3100 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3101 struct relay_stream
*stream
;
3102 struct data_connection_state_receive_payload
*state
=
3103 &conn
->protocol
.data
.state
.receive_payload
;
3104 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3105 char data_buffer
[chunk_size
];
3106 bool partial_recv
= false;
3107 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3108 uint64_t left_to_receive
= state
->left_to_receive
;
3109 struct relay_session
*session
;
3111 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3112 state
->header
.stream_id
, state
->header
.net_seq_num
,
3113 state
->received
, left_to_receive
);
3115 stream
= stream_get_by_id(state
->header
.stream_id
);
3117 /* Protocol error. */
3118 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3119 state
->header
.stream_id
);
3120 status
= RELAY_CONNECTION_STATUS_ERROR
;
3124 pthread_mutex_lock(&stream
->lock
);
3125 session
= stream
->trace
->session
;
3126 if (!conn
->session
) {
3127 ret
= connection_set_session(conn
, session
);
3129 status
= RELAY_CONNECTION_STATUS_ERROR
;
3130 goto end_stream_unlock
;
3135 * The size of the "chunk" received on any iteration is bounded by:
3136 * - the data left to receive,
3137 * - the data immediately available on the socket,
3138 * - the on-stack data buffer
3140 while (left_to_receive
> 0 && !partial_recv
) {
3141 size_t recv_size
= min(left_to_receive
, chunk_size
);
3142 struct lttng_buffer_view packet_chunk
;
3144 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3145 recv_size
, MSG_DONTWAIT
);
3147 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3148 PERROR("Socket %d error", conn
->sock
->fd
);
3149 status
= RELAY_CONNECTION_STATUS_ERROR
;
3151 goto end_stream_unlock
;
3152 } else if (ret
== 0) {
3153 /* No more data ready to be consumed on socket. */
3154 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3155 state
->header
.stream_id
);
3156 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3158 } else if (ret
< (int) recv_size
) {
3160 * All the data available on the socket has been
3163 partial_recv
= true;
3167 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3169 assert(packet_chunk
.data
);
3171 ret
= stream_write(stream
, &packet_chunk
, 0);
3173 ERR("Relay error writing data to file");
3174 status
= RELAY_CONNECTION_STATUS_ERROR
;
3175 goto end_stream_unlock
;
3178 left_to_receive
-= recv_size
;
3179 state
->received
+= recv_size
;
3180 state
->left_to_receive
= left_to_receive
;
3183 if (state
->left_to_receive
> 0) {
3185 * Did not receive all the data expected, wait for more data to
3186 * become available on the socket.
3188 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3189 state
->header
.stream_id
, state
->received
,
3190 state
->left_to_receive
);
3191 goto end_stream_unlock
;
3194 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3196 status
= RELAY_CONNECTION_STATUS_ERROR
;
3197 goto end_stream_unlock
;
3200 if (session_streams_have_index(session
)) {
3201 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3202 state
->rotate_index
, &index_flushed
,
3203 state
->header
.data_size
+ state
->header
.padding_size
);
3205 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3206 stream
->stream_handle
,
3207 state
->header
.net_seq_num
, ret
);
3208 status
= RELAY_CONNECTION_STATUS_ERROR
;
3209 goto end_stream_unlock
;
3213 if (stream
->prev_data_seq
== -1ULL) {
3217 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3218 state
->header
.padding_size
, state
->header
.net_seq_num
,
3221 status
= RELAY_CONNECTION_STATUS_ERROR
;
3222 goto end_stream_unlock
;
3226 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3227 * contents of *state which are aliased (union) to the same location as
3228 * the new state. Don't use it beyond this point.
3230 connection_reset_protocol_state(conn
);
3234 close_requested
= stream
->close_requested
;
3235 pthread_mutex_unlock(&stream
->lock
);
3236 if (close_requested
&& left_to_receive
== 0) {
3237 try_stream_close(stream
);
3241 pthread_mutex_lock(&session
->lock
);
3242 uatomic_set(&session
->new_streams
, 1);
3243 pthread_mutex_unlock(&session
->lock
);
3252 * relay_process_data: Process the data received on the data socket
3254 static enum relay_connection_status
relay_process_data(
3255 struct relay_connection
*conn
)
3257 enum relay_connection_status status
;
3259 switch (conn
->protocol
.data
.state_id
) {
3260 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3261 status
= relay_process_data_receive_header(conn
);
3263 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3264 status
= relay_process_data_receive_payload(conn
);
3267 ERR("Unexpected data connection communication state.");
3274 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3278 (void) lttng_poll_del(events
, pollfd
);
3280 ret
= close(pollfd
);
3282 ERR("Closing pollfd %d", pollfd
);
3286 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3287 int pollfd
, struct relay_connection
*conn
)
3289 const char *type_str
;
3291 switch (conn
->type
) {
3296 type_str
= "Control";
3298 case RELAY_VIEWER_COMMAND
:
3299 type_str
= "Viewer Command";
3301 case RELAY_VIEWER_NOTIFICATION
:
3302 type_str
= "Viewer Notification";
3305 type_str
= "Unknown";
3307 cleanup_connection_pollfd(events
, pollfd
);
3308 connection_put(conn
);
3309 DBG("%s connection closed with %d", type_str
, pollfd
);
3313 * This thread does the actual work
3315 static void *relay_thread_worker(void *data
)
3317 int ret
, err
= -1, last_seen_data_fd
= -1;
3319 struct lttng_poll_event events
;
3320 struct lttng_ht
*relay_connections_ht
;
3321 struct lttng_ht_iter iter
;
3322 struct relay_connection
*destroy_conn
= NULL
;
3324 DBG("[thread] Relay worker started");
3326 rcu_register_thread();
3328 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3330 if (testpoint(relayd_thread_worker
)) {
3331 goto error_testpoint
;
3334 health_code_update();
3336 /* table of connections indexed on socket */
3337 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3338 if (!relay_connections_ht
) {
3339 goto relay_connections_ht_error
;
3342 ret
= create_thread_poll_set(&events
, 2);
3344 goto error_poll_create
;
3347 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3354 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3356 health_code_update();
3358 /* Infinite blocking call, waiting for transmission */
3359 DBG3("Relayd worker thread polling...");
3360 health_poll_entry();
3361 ret
= lttng_poll_wait(&events
, -1);
3365 * Restart interrupted system call.
3367 if (errno
== EINTR
) {
3376 * Process control. The control connection is
3377 * prioritized so we don't starve it with high
3378 * throughput tracing data on the data connection.
3380 for (i
= 0; i
< nb_fd
; i
++) {
3381 /* Fetch once the poll data */
3382 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3383 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3385 health_code_update();
3387 /* Thread quit pipe has been closed. Killing thread. */
3388 ret
= check_thread_quit_pipe(pollfd
, revents
);
3394 /* Inspect the relay conn pipe for new connection */
3395 if (pollfd
== relay_conn_pipe
[0]) {
3396 if (revents
& LPOLLIN
) {
3397 struct relay_connection
*conn
;
3399 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3403 lttng_poll_add(&events
, conn
->sock
->fd
,
3404 LPOLLIN
| LPOLLRDHUP
);
3405 connection_ht_add(relay_connections_ht
, conn
);
3406 DBG("Connection socket %d added", conn
->sock
->fd
);
3407 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3408 ERR("Relay connection pipe error");
3411 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3415 struct relay_connection
*ctrl_conn
;
3417 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3418 /* If not found, there is a synchronization issue. */
3421 if (ctrl_conn
->type
== RELAY_DATA
) {
3422 if (revents
& LPOLLIN
) {
3424 * Flag the last seen data fd not deleted. It will be
3425 * used as the last seen fd if any fd gets deleted in
3428 last_notdel_data_fd
= pollfd
;
3430 goto put_ctrl_connection
;
3432 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3434 if (revents
& LPOLLIN
) {
3435 enum relay_connection_status status
;
3437 status
= relay_process_control(ctrl_conn
);
3438 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3440 * On socket error flag the session as aborted to force
3441 * the cleanup of its stream otherwise it can leak
3442 * during the lifetime of the relayd.
3444 * This prevents situations in which streams can be
3445 * left opened because an index was received, the
3446 * control connection is closed, and the data
3447 * connection is closed (uncleanly) before the packet's
3450 * Since the control connection encountered an error,
3451 * it is okay to be conservative and close the
3452 * session right now as we can't rely on the protocol
3453 * being respected anymore.
3455 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3456 session_abort(ctrl_conn
->session
);
3459 /* Clear the connection on error or close. */
3460 relay_thread_close_connection(&events
,
3465 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3466 relay_thread_close_connection(&events
,
3468 if (last_seen_data_fd
== pollfd
) {
3469 last_seen_data_fd
= last_notdel_data_fd
;
3472 ERR("Unexpected poll events %u for control sock %d",
3474 connection_put(ctrl_conn
);
3477 put_ctrl_connection
:
3478 connection_put(ctrl_conn
);
3483 * The last loop handled a control request, go back to poll to make
3484 * sure we prioritise the control socket.
3490 if (last_seen_data_fd
>= 0) {
3491 for (i
= 0; i
< nb_fd
; i
++) {
3492 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3494 health_code_update();
3496 if (last_seen_data_fd
== pollfd
) {
3503 /* Process data connection. */
3504 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3505 /* Fetch the poll data. */
3506 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3507 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3508 struct relay_connection
*data_conn
;
3510 health_code_update();
3513 /* No activity for this FD (poll implementation). */
3517 /* Skip the command pipe. It's handled in the first loop. */
3518 if (pollfd
== relay_conn_pipe
[0]) {
3522 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3524 /* Skip it. Might be removed before. */
3527 if (data_conn
->type
== RELAY_CONTROL
) {
3528 goto put_data_connection
;
3530 assert(data_conn
->type
== RELAY_DATA
);
3532 if (revents
& LPOLLIN
) {
3533 enum relay_connection_status status
;
3535 status
= relay_process_data(data_conn
);
3536 /* Connection closed or error. */
3537 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3539 * On socket error flag the session as aborted to force
3540 * the cleanup of its stream otherwise it can leak
3541 * during the lifetime of the relayd.
3543 * This prevents situations in which streams can be
3544 * left opened because an index was received, the
3545 * control connection is closed, and the data
3546 * connection is closed (uncleanly) before the packet's
3549 * Since the data connection encountered an error,
3550 * it is okay to be conservative and close the
3551 * session right now as we can't rely on the protocol
3552 * being respected anymore.
3554 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3555 session_abort(data_conn
->session
);
3557 relay_thread_close_connection(&events
, pollfd
,
3560 * Every goto restart call sets the last seen fd where
3561 * here we don't really care since we gracefully
3562 * continue the loop after the connection is deleted.
3565 /* Keep last seen port. */
3566 last_seen_data_fd
= pollfd
;
3567 connection_put(data_conn
);
3570 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3571 relay_thread_close_connection(&events
, pollfd
,
3574 ERR("Unknown poll events %u for data sock %d",
3577 put_data_connection
:
3578 connection_put(data_conn
);
3580 last_seen_data_fd
= -1;
3583 /* Normal exit, no error */
3588 /* Cleanup remaining connection object. */
3590 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3593 health_code_update();
3595 session_abort(destroy_conn
->session
);
3598 * No need to grab another ref, because we own
3601 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3606 lttng_poll_clean(&events
);
3608 lttng_ht_destroy(relay_connections_ht
);
3609 relay_connections_ht_error
:
3610 /* Close relay conn pipes */
3611 utils_close_pipe(relay_conn_pipe
);
3613 DBG("Thread exited with error");
3615 DBG("Worker thread cleanup complete");
3619 ERR("Health error occurred in %s", __func__
);
3621 health_unregister(health_relayd
);
3622 rcu_unregister_thread();
3623 lttng_relay_stop_threads();
3628 * Create the relay command pipe to wake thread_manage_apps.
3629 * Closed in cleanup().
3631 static int create_relay_conn_pipe(void)
3635 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3643 int main(int argc
, char **argv
)
3645 int ret
= 0, retval
= 0;
3648 /* Parse arguments */
3650 if (set_options(argc
, argv
)) {
3655 if (set_signal_handler()) {
3660 /* Try to create directory if -o, --output is specified. */
3661 if (opt_output_path
) {
3662 if (*opt_output_path
!= '/') {
3663 ERR("Please specify an absolute path for -o, --output PATH");
3668 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3671 ERR("Unable to create %s", opt_output_path
);
3678 if (opt_daemon
|| opt_background
) {
3681 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3689 * We are in the child. Make sure all other file
3690 * descriptors are closed, in case we are called with
3691 * more opened file descriptors than the standard ones.
3693 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3698 sessiond_trace_chunk_registry
= sessiond_trace_chunk_registry_create();
3699 if (!sessiond_trace_chunk_registry
) {
3700 ERR("Failed to initialize session daemon trace chunk registry");
3702 goto exit_sessiond_trace_chunk_registry
;
3705 /* Initialize thread health monitoring */
3706 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3707 if (!health_relayd
) {
3708 PERROR("health_app_create error");
3710 goto exit_health_app_create
;
3713 /* Create thread quit pipe */
3714 if (init_thread_quit_pipe()) {
3716 goto exit_init_data
;
3719 /* Setup the thread apps communication pipe. */
3720 if (create_relay_conn_pipe()) {
3722 goto exit_init_data
;
3725 /* Init relay command queue. */
3726 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3728 /* Initialize communication library */
3730 lttcomm_inet_init();
3732 /* tables of sessions indexed by session ID */
3733 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3736 goto exit_init_data
;
3739 /* tables of streams indexed by stream ID */
3740 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3741 if (!relay_streams_ht
) {
3743 goto exit_init_data
;
3746 /* tables of streams indexed by stream ID */
3747 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3748 if (!viewer_streams_ht
) {
3750 goto exit_init_data
;
3753 ret
= utils_create_pipe(health_quit_pipe
);
3756 goto exit_health_quit_pipe
;
3759 /* Create thread to manage the client socket */
3760 ret
= pthread_create(&health_thread
, default_pthread_attr(),
3761 thread_manage_health
, (void *) NULL
);
3764 PERROR("pthread_create health");
3766 goto exit_health_thread
;
3769 /* Setup the dispatcher thread */
3770 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
3771 relay_thread_dispatcher
, (void *) NULL
);
3774 PERROR("pthread_create dispatcher");
3776 goto exit_dispatcher_thread
;
3779 /* Setup the worker thread */
3780 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
3781 relay_thread_worker
, NULL
);
3784 PERROR("pthread_create worker");
3786 goto exit_worker_thread
;
3789 /* Setup the listener thread */
3790 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
3791 relay_thread_listener
, (void *) NULL
);
3794 PERROR("pthread_create listener");
3796 goto exit_listener_thread
;
3799 ret
= relayd_live_create(live_uri
);
3801 ERR("Starting live viewer threads");
3807 * This is where we start awaiting program completion (e.g. through
3808 * signal that asks threads to teardown).
3811 ret
= relayd_live_join();
3817 ret
= pthread_join(listener_thread
, &status
);
3820 PERROR("pthread_join listener_thread");
3824 exit_listener_thread
:
3825 ret
= pthread_join(worker_thread
, &status
);
3828 PERROR("pthread_join worker_thread");
3833 ret
= pthread_join(dispatcher_thread
, &status
);
3836 PERROR("pthread_join dispatcher_thread");
3839 exit_dispatcher_thread
:
3841 ret
= pthread_join(health_thread
, &status
);
3844 PERROR("pthread_join health_thread");
3849 utils_close_pipe(health_quit_pipe
);
3850 exit_health_quit_pipe
:
3853 health_app_destroy(health_relayd
);
3854 sessiond_trace_chunk_registry_destroy(sessiond_trace_chunk_registry
);
3855 exit_health_app_create
:
3856 exit_sessiond_trace_chunk_registry
:
3859 * Wait for all pending call_rcu work to complete before tearing
3860 * down data structures. call_rcu worker may be trying to
3861 * perform lookups in those structures.
3866 /* Ensure all prior call_rcu are done. */