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>
44 #include <lttng/lttng.h>
45 #include <common/common.h>
46 #include <common/compat/poll.h>
47 #include <common/compat/socket.h>
48 #include <common/compat/endian.h>
49 #include <common/compat/getenv.h>
50 #include <common/defaults.h>
51 #include <common/daemonize.h>
52 #include <common/futex.h>
53 #include <common/sessiond-comm/sessiond-comm.h>
54 #include <common/sessiond-comm/inet.h>
55 #include <common/sessiond-comm/relayd.h>
56 #include <common/uri.h>
57 #include <common/utils.h>
58 #include <common/align.h>
59 #include <common/config/session-config.h>
60 #include <common/dynamic-buffer.h>
61 #include <common/buffer-view.h>
62 #include <common/string-utils/format.h>
65 #include "ctf-trace.h"
68 #include "lttng-relayd.h"
70 #include "health-relayd.h"
71 #include "testpoint.h"
72 #include "viewer-stream.h"
75 #include "connection.h"
76 #include "tracefile-array.h"
77 #include "tcp_keep_alive.h"
78 #include "sessiond-trace-chunks.h"
80 static const char *help_msg
=
81 #ifdef LTTNG_EMBED_HELP
82 #include <lttng-relayd.8.h>
88 enum relay_connection_status
{
89 RELAY_CONNECTION_STATUS_OK
,
90 /* An error occurred while processing an event on the connection. */
91 RELAY_CONNECTION_STATUS_ERROR
,
92 /* Connection closed/shutdown cleanly. */
93 RELAY_CONNECTION_STATUS_CLOSED
,
96 /* command line options */
97 char *opt_output_path
;
98 static int opt_daemon
, opt_background
;
101 * We need to wait for listener and live listener threads, as well as
102 * health check thread, before being ready to signal readiness.
104 #define NR_LTTNG_RELAY_READY 3
105 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
107 /* Size of receive buffer. */
108 #define RECV_DATA_BUFFER_SIZE 65536
110 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
111 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
113 static struct lttng_uri
*control_uri
;
114 static struct lttng_uri
*data_uri
;
115 static struct lttng_uri
*live_uri
;
117 const char *progname
;
119 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
120 static int tracing_group_name_override
;
122 const char * const config_section_name
= "relayd";
125 * Quit pipe for all threads. This permits a single cancellation point
126 * for all threads when receiving an event on the pipe.
128 int thread_quit_pipe
[2] = { -1, -1 };
131 * This pipe is used to inform the worker thread that a command is queued and
132 * ready to be processed.
134 static int relay_conn_pipe
[2] = { -1, -1 };
136 /* Shared between threads */
137 static int dispatch_thread_exit
;
139 static pthread_t listener_thread
;
140 static pthread_t dispatcher_thread
;
141 static pthread_t worker_thread
;
142 static pthread_t health_thread
;
145 * last_relay_stream_id_lock protects last_relay_stream_id increment
146 * atomicity on 32-bit architectures.
148 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
149 static uint64_t last_relay_stream_id
;
152 * Relay command queue.
154 * The relay_thread_listener and relay_thread_dispatcher communicate with this
157 static struct relay_conn_queue relay_conn_queue
;
159 /* Global relay stream hash table. */
160 struct lttng_ht
*relay_streams_ht
;
162 /* Global relay viewer stream hash table. */
163 struct lttng_ht
*viewer_streams_ht
;
165 /* Global relay sessions hash table. */
166 struct lttng_ht
*sessions_ht
;
168 /* Relayd health monitoring */
169 struct health_app
*health_relayd
;
171 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
173 static struct option long_options
[] = {
174 { "control-port", 1, 0, 'C', },
175 { "data-port", 1, 0, 'D', },
176 { "live-port", 1, 0, 'L', },
177 { "daemonize", 0, 0, 'd', },
178 { "background", 0, 0, 'b', },
179 { "group", 1, 0, 'g', },
180 { "help", 0, 0, 'h', },
181 { "output", 1, 0, 'o', },
182 { "verbose", 0, 0, 'v', },
183 { "config", 1, 0, 'f' },
184 { "version", 0, 0, 'V' },
188 static const char *config_ignore_options
[] = { "help", "config", "version" };
191 * Take an option from the getopt output and set it in the right variable to be
194 * Return 0 on success else a negative value.
196 static int set_option(int opt
, const char *arg
, const char *optname
)
202 fprintf(stderr
, "option %s", optname
);
204 fprintf(stderr
, " with arg %s\n", arg
);
208 if (lttng_is_setuid_setgid()) {
209 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
210 "-C, --control-port");
212 ret
= uri_parse(arg
, &control_uri
);
214 ERR("Invalid control URI specified");
217 if (control_uri
->port
== 0) {
218 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
223 if (lttng_is_setuid_setgid()) {
224 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
227 ret
= uri_parse(arg
, &data_uri
);
229 ERR("Invalid data URI specified");
232 if (data_uri
->port
== 0) {
233 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
238 if (lttng_is_setuid_setgid()) {
239 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
242 ret
= uri_parse(arg
, &live_uri
);
244 ERR("Invalid live URI specified");
247 if (live_uri
->port
== 0) {
248 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
259 if (lttng_is_setuid_setgid()) {
260 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
263 tracing_group_name
= strdup(arg
);
264 if (tracing_group_name
== NULL
) {
269 tracing_group_name_override
= 1;
273 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
275 ERR("Cannot show --help for `lttng-relayd`");
280 fprintf(stdout
, "%s\n", VERSION
);
283 if (lttng_is_setuid_setgid()) {
284 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
287 ret
= asprintf(&opt_output_path
, "%s", arg
);
290 PERROR("asprintf opt_output_path");
296 /* Verbose level can increase using multiple -v */
298 lttng_opt_verbose
= config_parse_value(arg
);
300 /* Only 3 level of verbosity (-vvv). */
301 if (lttng_opt_verbose
< 3) {
302 lttng_opt_verbose
+= 1;
307 /* Unknown option or other error.
308 * Error is printed by getopt, just return */
321 * config_entry_handler_cb used to handle options read from a config file.
322 * See config_entry_handler_cb comment in common/config/session-config.h for the
323 * return value conventions.
325 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
329 if (!entry
|| !entry
->name
|| !entry
->value
) {
334 /* Check if the option is to be ignored */
335 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
336 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
341 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
342 /* Ignore if entry name is not fully matched. */
343 if (strcmp(entry
->name
, long_options
[i
].name
)) {
348 * If the option takes no argument on the command line,
349 * we have to check if the value is "true". We support
350 * non-zero numeric values, true, on and yes.
352 if (!long_options
[i
].has_arg
) {
353 ret
= config_parse_value(entry
->value
);
356 WARN("Invalid configuration value \"%s\" for option %s",
357 entry
->value
, entry
->name
);
359 /* False, skip boolean config option. */
364 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
368 WARN("Unrecognized option \"%s\" in daemon configuration file.",
375 static int set_options(int argc
, char **argv
)
377 int c
, ret
= 0, option_index
= 0, retval
= 0;
378 int orig_optopt
= optopt
, orig_optind
= optind
;
379 char *default_address
, *optstring
;
380 const char *config_path
= NULL
;
382 optstring
= utils_generate_optstring(long_options
,
383 sizeof(long_options
) / sizeof(struct option
));
389 /* Check for the --config option */
391 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
392 &option_index
)) != -1) {
396 } else if (c
!= 'f') {
400 if (lttng_is_setuid_setgid()) {
401 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
404 config_path
= utils_expand_path(optarg
);
406 ERR("Failed to resolve path: %s", optarg
);
411 ret
= config_get_section_entries(config_path
, config_section_name
,
412 config_entry_handler
, NULL
);
415 ERR("Invalid configuration option at line %i", ret
);
421 /* Reset getopt's global state */
422 optopt
= orig_optopt
;
423 optind
= orig_optind
;
425 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
430 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
437 /* assign default values */
438 if (control_uri
== NULL
) {
439 ret
= asprintf(&default_address
,
440 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
441 DEFAULT_NETWORK_CONTROL_PORT
);
443 PERROR("asprintf default data address");
448 ret
= uri_parse(default_address
, &control_uri
);
449 free(default_address
);
451 ERR("Invalid control URI specified");
456 if (data_uri
== NULL
) {
457 ret
= asprintf(&default_address
,
458 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
459 DEFAULT_NETWORK_DATA_PORT
);
461 PERROR("asprintf default data address");
466 ret
= uri_parse(default_address
, &data_uri
);
467 free(default_address
);
469 ERR("Invalid data URI specified");
474 if (live_uri
== NULL
) {
475 ret
= asprintf(&default_address
,
476 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
477 DEFAULT_NETWORK_VIEWER_PORT
);
479 PERROR("asprintf default viewer control address");
484 ret
= uri_parse(default_address
, &live_uri
);
485 free(default_address
);
487 ERR("Invalid viewer control URI specified");
498 static void print_global_objects(void)
500 rcu_register_thread();
502 print_viewer_streams();
503 print_relay_streams();
506 rcu_unregister_thread();
512 static void relayd_cleanup(void)
514 print_global_objects();
518 if (viewer_streams_ht
)
519 lttng_ht_destroy(viewer_streams_ht
);
520 if (relay_streams_ht
)
521 lttng_ht_destroy(relay_streams_ht
);
523 lttng_ht_destroy(sessions_ht
);
525 /* free the dynamically allocated opt_output_path */
526 free(opt_output_path
);
528 /* Close thread quit pipes */
529 utils_close_pipe(thread_quit_pipe
);
531 uri_free(control_uri
);
533 /* Live URI is freed in the live thread. */
535 if (tracing_group_name_override
) {
536 free((void *) tracing_group_name
);
541 * Write to writable pipe used to notify a thread.
543 static int notify_thread_pipe(int wpipe
)
547 ret
= lttng_write(wpipe
, "!", 1);
549 PERROR("write poll pipe");
557 static int notify_health_quit_pipe(int *pipe
)
561 ret
= lttng_write(pipe
[1], "4", 1);
563 PERROR("write relay health quit");
572 * Stop all relayd and relayd-live threads.
574 int lttng_relay_stop_threads(void)
578 /* Stopping all threads */
579 DBG("Terminating all threads");
580 if (notify_thread_pipe(thread_quit_pipe
[1])) {
581 ERR("write error on thread quit pipe");
585 if (notify_health_quit_pipe(health_quit_pipe
)) {
586 ERR("write error on health quit pipe");
589 /* Dispatch thread */
590 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
591 futex_nto1_wake(&relay_conn_queue
.futex
);
593 if (relayd_live_stop()) {
594 ERR("Error stopping live threads");
601 * Signal handler for the daemon
603 * Simply stop all worker threads, leaving main() return gracefully after
604 * joining all threads and calling cleanup().
606 static void sighandler(int sig
)
610 DBG("SIGINT caught");
611 if (lttng_relay_stop_threads()) {
612 ERR("Error stopping threads");
616 DBG("SIGTERM caught");
617 if (lttng_relay_stop_threads()) {
618 ERR("Error stopping threads");
622 CMM_STORE_SHARED(recv_child_signal
, 1);
630 * Setup signal handler for :
631 * SIGINT, SIGTERM, SIGPIPE
633 static int set_signal_handler(void)
639 if ((ret
= sigemptyset(&sigset
)) < 0) {
640 PERROR("sigemptyset");
647 sa
.sa_handler
= sighandler
;
648 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
653 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
658 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
663 sa
.sa_handler
= SIG_IGN
;
664 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
669 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
674 void lttng_relay_notify_ready(void)
676 /* Notify the parent of the fork() process that we are ready. */
677 if (opt_daemon
|| opt_background
) {
678 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
679 kill(child_ppid
, SIGUSR1
);
685 * Init thread quit pipe.
687 * Return -1 on error or 0 if all pipes are created.
689 static int init_thread_quit_pipe(void)
693 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
699 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
701 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
705 if (events
== NULL
|| size
== 0) {
710 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
716 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
728 * Check if the thread quit pipe was triggered.
730 * Return 1 if it was triggered else 0;
732 static int check_thread_quit_pipe(int fd
, uint32_t events
)
734 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
742 * Create and init socket from uri.
744 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
747 struct lttcomm_sock
*sock
= NULL
;
749 sock
= lttcomm_alloc_sock_from_uri(uri
);
751 ERR("Allocating socket");
755 ret
= lttcomm_create_sock(sock
);
759 DBG("Listening on sock %d", sock
->fd
);
761 ret
= sock
->ops
->bind(sock
);
763 PERROR("Failed to bind socket");
767 ret
= sock
->ops
->listen(sock
, -1);
777 lttcomm_destroy_sock(sock
);
783 * This thread manages the listening for new connections on the network
785 static void *relay_thread_listener(void *data
)
787 int i
, ret
, pollfd
, err
= -1;
788 uint32_t revents
, nb_fd
;
789 struct lttng_poll_event events
;
790 struct lttcomm_sock
*control_sock
, *data_sock
;
792 DBG("[thread] Relay listener started");
794 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
796 health_code_update();
798 control_sock
= relay_socket_create(control_uri
);
800 goto error_sock_control
;
803 data_sock
= relay_socket_create(data_uri
);
805 goto error_sock_relay
;
809 * Pass 3 as size here for the thread quit pipe, control and
812 ret
= create_thread_poll_set(&events
, 3);
814 goto error_create_poll
;
817 /* Add the control socket */
818 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
823 /* Add the data socket */
824 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
829 lttng_relay_notify_ready();
831 if (testpoint(relayd_thread_listener
)) {
832 goto error_testpoint
;
836 health_code_update();
838 DBG("Listener accepting connections");
842 ret
= lttng_poll_wait(&events
, -1);
846 * Restart interrupted system call.
848 if (errno
== EINTR
) {
856 DBG("Relay new connection received");
857 for (i
= 0; i
< nb_fd
; i
++) {
858 health_code_update();
860 /* Fetch once the poll data */
861 revents
= LTTNG_POLL_GETEV(&events
, i
);
862 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
864 /* Thread quit pipe has been closed. Killing thread. */
865 ret
= check_thread_quit_pipe(pollfd
, revents
);
871 if (revents
& LPOLLIN
) {
873 * A new connection is requested, therefore a
874 * sessiond/consumerd connection is allocated in
875 * this thread, enqueued to a global queue and
876 * dequeued (and freed) in the worker thread.
879 struct relay_connection
*new_conn
;
880 struct lttcomm_sock
*newsock
;
881 enum connection_type type
;
883 if (pollfd
== data_sock
->fd
) {
885 newsock
= data_sock
->ops
->accept(data_sock
);
886 DBG("Relay data connection accepted, socket %d",
889 assert(pollfd
== control_sock
->fd
);
890 type
= RELAY_CONTROL
;
891 newsock
= control_sock
->ops
->accept(control_sock
);
892 DBG("Relay control connection accepted, socket %d",
896 PERROR("accepting sock");
900 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
903 PERROR("setsockopt inet");
904 lttcomm_destroy_sock(newsock
);
908 ret
= socket_apply_keep_alive_config(newsock
->fd
);
910 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
912 lttcomm_destroy_sock(newsock
);
916 new_conn
= connection_create(newsock
, type
);
918 lttcomm_destroy_sock(newsock
);
922 /* Enqueue request for the dispatcher thread. */
923 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
927 * Wake the dispatch queue futex.
928 * Implicit memory barrier with the
929 * exchange in cds_wfcq_enqueue.
931 futex_nto1_wake(&relay_conn_queue
.futex
);
932 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
933 ERR("socket poll error");
936 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
946 lttng_poll_clean(&events
);
948 if (data_sock
->fd
>= 0) {
949 ret
= data_sock
->ops
->close(data_sock
);
954 lttcomm_destroy_sock(data_sock
);
956 if (control_sock
->fd
>= 0) {
957 ret
= control_sock
->ops
->close(control_sock
);
962 lttcomm_destroy_sock(control_sock
);
966 ERR("Health error occurred in %s", __func__
);
968 health_unregister(health_relayd
);
969 DBG("Relay listener thread cleanup complete");
970 lttng_relay_stop_threads();
975 * This thread manages the dispatching of the requests to worker threads
977 static void *relay_thread_dispatcher(void *data
)
981 struct cds_wfcq_node
*node
;
982 struct relay_connection
*new_conn
= NULL
;
984 DBG("[thread] Relay dispatcher started");
986 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
988 if (testpoint(relayd_thread_dispatcher
)) {
989 goto error_testpoint
;
992 health_code_update();
995 health_code_update();
997 /* Atomically prepare the queue futex */
998 futex_nto1_prepare(&relay_conn_queue
.futex
);
1000 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1005 health_code_update();
1007 /* Dequeue commands */
1008 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1009 &relay_conn_queue
.tail
);
1011 DBG("Woken up but nothing in the relay command queue");
1012 /* Continue thread execution */
1015 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1017 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1020 * Inform worker thread of the new request. This
1021 * call is blocking so we can be assured that
1022 * the data will be read at some point in time
1023 * or wait to the end of the world :)
1025 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1027 PERROR("write connection pipe");
1028 connection_put(new_conn
);
1031 } while (node
!= NULL
);
1033 /* Futex wait on queue. Blocking call on futex() */
1034 health_poll_entry();
1035 futex_nto1_wait(&relay_conn_queue
.futex
);
1039 /* Normal exit, no error */
1046 ERR("Health error occurred in %s", __func__
);
1048 health_unregister(health_relayd
);
1049 DBG("Dispatch thread dying");
1050 lttng_relay_stop_threads();
1054 static bool session_streams_have_index(const struct relay_session
*session
)
1056 return session
->minor
>= 4 && !session
->snapshot
;
1060 * Handle the RELAYD_CREATE_SESSION command.
1062 * On success, send back the session id or else return a negative value.
1064 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1065 struct relay_connection
*conn
,
1066 const struct lttng_buffer_view
*payload
)
1070 struct relay_session
*session
= NULL
;
1071 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
1072 char session_name
[LTTNG_NAME_MAX
] = {};
1073 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1074 uint32_t live_timer
= 0;
1075 bool snapshot
= false;
1076 bool session_name_contains_creation_timestamp
= false;
1077 /* Left nil for peers < 2.11. */
1078 char base_path
[LTTNG_PATH_MAX
] = {};
1079 lttng_uuid sessiond_uuid
= {};
1080 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1081 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1082 LTTNG_OPTIONAL(time_t) creation_time
= {};
1083 struct lttng_dynamic_buffer reply_payload
;
1085 lttng_dynamic_buffer_init(&reply_payload
);
1087 if (conn
->minor
< 4) {
1088 /* From 2.1 to 2.3 */
1090 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1091 /* From 2.4 to 2.10 */
1092 ret
= cmd_create_session_2_4(payload
, session_name
,
1093 hostname
, &live_timer
, &snapshot
);
1095 bool has_current_chunk
;
1096 uint64_t current_chunk_id_value
;
1097 time_t creation_time_value
;
1098 uint64_t id_sessiond_value
;
1100 /* From 2.11 to ... */
1101 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1102 base_path
, &live_timer
, &snapshot
, &id_sessiond_value
,
1103 sessiond_uuid
, &has_current_chunk
,
1104 ¤t_chunk_id_value
, &creation_time_value
,
1105 &session_name_contains_creation_timestamp
);
1106 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1107 /* The nil UUID is reserved for pre-2.11 clients. */
1108 ERR("Illegal nil UUID announced by peer in create session command");
1112 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1113 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1114 if (has_current_chunk
) {
1115 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1116 current_chunk_id_value
);
1124 session
= session_create(session_name
, hostname
, base_path
, live_timer
,
1125 snapshot
, sessiond_uuid
,
1126 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1127 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1128 creation_time
.is_set
? &creation_time
.value
: NULL
,
1129 conn
->major
, conn
->minor
,
1130 session_name_contains_creation_timestamp
);
1135 assert(!conn
->session
);
1136 conn
->session
= session
;
1137 DBG("Created session %" PRIu64
, session
->id
);
1139 reply
.generic
.session_id
= htobe64(session
->id
);
1143 reply
.generic
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1145 reply
.generic
.ret_code
= htobe32(LTTNG_OK
);
1148 if (conn
->minor
< 11) {
1149 /* From 2.1 to 2.10 */
1150 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1151 &reply
.generic
, sizeof(reply
.generic
));
1153 ERR("Failed to append \"create session\" command reply header to payload buffer");
1158 const uint32_t output_path_length
=
1159 strlen(session
->output_path
) + 1;
1161 reply
.output_path_length
= htobe32(output_path_length
);
1162 ret
= lttng_dynamic_buffer_append(&reply_payload
, &reply
,
1165 ERR("Failed to append \"create session\" command reply header to payload buffer");
1169 ret
= lttng_dynamic_buffer_append(&reply_payload
,
1170 session
->output_path
, output_path_length
);
1172 ERR("Failed to append \"create session\" command reply path to payload buffer");
1177 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, reply_payload
.data
,
1178 reply_payload
.size
, 0);
1179 if (send_ret
< (ssize_t
) reply_payload
.size
) {
1180 ERR("Failed to send \"create session\" command reply of %zu bytes (ret = %zd)",
1181 reply_payload
.size
, send_ret
);
1185 if (ret
< 0 && session
) {
1186 session_put(session
);
1188 lttng_dynamic_buffer_reset(&reply_payload
);
1193 * When we have received all the streams and the metadata for a channel,
1194 * we make them visible to the viewer threads.
1196 static void publish_connection_local_streams(struct relay_connection
*conn
)
1198 struct relay_stream
*stream
;
1199 struct relay_session
*session
= conn
->session
;
1202 * We publish all streams belonging to a session atomically wrt
1205 pthread_mutex_lock(&session
->lock
);
1207 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1209 stream_publish(stream
);
1214 * Inform the viewer that there are new streams in the session.
1216 if (session
->viewer_attached
) {
1217 uatomic_set(&session
->new_streams
, 1);
1219 pthread_mutex_unlock(&session
->lock
);
1222 static int conform_channel_path(char *channel_path
)
1226 if (strstr("../", channel_path
)) {
1227 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1233 if (*channel_path
== '/') {
1234 const size_t len
= strlen(channel_path
);
1237 * Channel paths from peers prior to 2.11 are expressed as an
1238 * absolute path that is, in reality, relative to the relay
1239 * daemon's output directory. Remove the leading slash so it
1240 * is correctly interpreted as a relative path later on.
1242 * len (and not len - 1) is used to copy the trailing NULL.
1244 bcopy(channel_path
+ 1, channel_path
, len
);
1251 * relay_add_stream: allocate a new stream for a session
1253 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1254 struct relay_connection
*conn
,
1255 const struct lttng_buffer_view
*payload
)
1259 struct relay_session
*session
= conn
->session
;
1260 struct relay_stream
*stream
= NULL
;
1261 struct lttcomm_relayd_status_stream reply
;
1262 struct ctf_trace
*trace
= NULL
;
1263 uint64_t stream_handle
= -1ULL;
1264 char *path_name
= NULL
, *channel_name
= NULL
;
1265 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1266 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1268 if (!session
|| !conn
->version_check_done
) {
1269 ERR("Trying to add a stream before version check");
1271 goto end_no_session
;
1274 if (session
->minor
== 1) {
1276 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1278 } else if (session
->minor
> 1 && session
->minor
< 11) {
1279 /* From 2.2 to 2.10 */
1280 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1281 &channel_name
, &tracefile_size
, &tracefile_count
);
1283 /* From 2.11 to ... */
1284 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1285 &channel_name
, &tracefile_size
, &tracefile_count
,
1286 &stream_chunk_id
.value
);
1287 stream_chunk_id
.is_set
= true;
1294 if (conform_channel_path(path_name
)) {
1298 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1302 /* This stream here has one reference on the trace. */
1304 pthread_mutex_lock(&last_relay_stream_id_lock
);
1305 stream_handle
= ++last_relay_stream_id
;
1306 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1308 /* We pass ownership of path_name and channel_name. */
1309 stream
= stream_create(trace
, stream_handle
, path_name
,
1310 channel_name
, tracefile_size
, tracefile_count
);
1312 channel_name
= NULL
;
1315 * Streams are the owners of their trace. Reference to trace is
1316 * kept within stream_create().
1318 ctf_trace_put(trace
);
1321 memset(&reply
, 0, sizeof(reply
));
1322 reply
.handle
= htobe64(stream_handle
);
1324 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1326 reply
.ret_code
= htobe32(LTTNG_OK
);
1329 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1330 sizeof(struct lttcomm_relayd_status_stream
), 0);
1331 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1332 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1344 * relay_close_stream: close a specific stream
1346 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1347 struct relay_connection
*conn
,
1348 const struct lttng_buffer_view
*payload
)
1352 struct relay_session
*session
= conn
->session
;
1353 struct lttcomm_relayd_close_stream stream_info
;
1354 struct lttcomm_relayd_generic_reply reply
;
1355 struct relay_stream
*stream
;
1357 DBG("Close stream received");
1359 if (!session
|| !conn
->version_check_done
) {
1360 ERR("Trying to close a stream before version check");
1362 goto end_no_session
;
1365 if (payload
->size
< sizeof(stream_info
)) {
1366 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1367 sizeof(stream_info
), payload
->size
);
1369 goto end_no_session
;
1371 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1372 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1373 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1375 stream
= stream_get_by_id(stream_info
.stream_id
);
1382 * Set last_net_seq_num before the close flag. Required by data
1385 pthread_mutex_lock(&stream
->lock
);
1386 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1387 pthread_mutex_unlock(&stream
->lock
);
1390 * This is one of the conditions which may trigger a stream close
1391 * with the others being:
1392 * 1) A close command is received for a stream
1393 * 2) The control connection owning the stream is closed
1394 * 3) We have received all of the stream's data _after_ a close
1397 try_stream_close(stream
);
1398 if (stream
->is_metadata
) {
1399 struct relay_viewer_stream
*vstream
;
1401 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1403 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1405 * Since all the metadata has been sent to the
1406 * viewer and that we have a request to close
1407 * its stream, we can safely teardown the
1408 * corresponding metadata viewer stream.
1410 viewer_stream_put(vstream
);
1412 /* Put local reference. */
1413 viewer_stream_put(vstream
);
1420 memset(&reply
, 0, sizeof(reply
));
1422 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1424 reply
.ret_code
= htobe32(LTTNG_OK
);
1426 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1427 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1428 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1429 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1439 * relay_reset_metadata: reset a metadata stream
1442 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1443 struct relay_connection
*conn
,
1444 const struct lttng_buffer_view
*payload
)
1448 struct relay_session
*session
= conn
->session
;
1449 struct lttcomm_relayd_reset_metadata stream_info
;
1450 struct lttcomm_relayd_generic_reply reply
;
1451 struct relay_stream
*stream
;
1453 DBG("Reset metadata received");
1455 if (!session
|| !conn
->version_check_done
) {
1456 ERR("Trying to reset a metadata stream before version check");
1458 goto end_no_session
;
1461 if (payload
->size
< sizeof(stream_info
)) {
1462 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1463 sizeof(stream_info
), payload
->size
);
1465 goto end_no_session
;
1467 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1468 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1469 stream_info
.version
= be64toh(stream_info
.version
);
1471 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1473 /* Unsupported for live sessions for now. */
1474 if (session
->live_timer
!= 0) {
1479 stream
= stream_get_by_id(stream_info
.stream_id
);
1484 pthread_mutex_lock(&stream
->lock
);
1485 if (!stream
->is_metadata
) {
1490 ret
= stream_reset_file(stream
);
1492 ERR("Failed to reset metadata stream %" PRIu64
1493 ": stream_path = %s, channel = %s",
1494 stream
->stream_handle
, stream
->path_name
,
1495 stream
->channel_name
);
1499 pthread_mutex_unlock(&stream
->lock
);
1503 memset(&reply
, 0, sizeof(reply
));
1505 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1507 reply
.ret_code
= htobe32(LTTNG_OK
);
1509 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1510 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1511 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1512 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1522 * relay_unknown_command: send -1 if received unknown command
1524 static void relay_unknown_command(struct relay_connection
*conn
)
1526 struct lttcomm_relayd_generic_reply reply
;
1529 memset(&reply
, 0, sizeof(reply
));
1530 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1531 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1532 if (send_ret
< sizeof(reply
)) {
1533 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1538 * relay_start: send an acknowledgment to the client to tell if we are
1539 * ready to receive data. We are ready if a session is established.
1541 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1542 struct relay_connection
*conn
,
1543 const struct lttng_buffer_view
*payload
)
1547 struct lttcomm_relayd_generic_reply reply
;
1548 struct relay_session
*session
= conn
->session
;
1551 DBG("Trying to start the streaming without a session established");
1552 ret
= htobe32(LTTNG_ERR_UNK
);
1555 memset(&reply
, 0, sizeof(reply
));
1556 reply
.ret_code
= htobe32(LTTNG_OK
);
1557 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1559 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1560 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1569 * relay_recv_metadata: receive the metadata for the session.
1571 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1572 struct relay_connection
*conn
,
1573 const struct lttng_buffer_view
*payload
)
1576 struct relay_session
*session
= conn
->session
;
1577 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1578 struct relay_stream
*metadata_stream
;
1579 uint64_t metadata_payload_size
;
1580 struct lttng_buffer_view packet_view
;
1583 ERR("Metadata sent before version check");
1588 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1589 ERR("Incorrect data size");
1593 metadata_payload_size
= recv_hdr
->data_size
-
1594 sizeof(struct lttcomm_relayd_metadata_payload
);
1596 memcpy(&metadata_payload_header
, payload
->data
,
1597 sizeof(metadata_payload_header
));
1598 metadata_payload_header
.stream_id
= be64toh(
1599 metadata_payload_header
.stream_id
);
1600 metadata_payload_header
.padding_size
= be32toh(
1601 metadata_payload_header
.padding_size
);
1603 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1604 if (!metadata_stream
) {
1609 packet_view
= lttng_buffer_view_from_view(payload
,
1610 sizeof(metadata_payload_header
), metadata_payload_size
);
1611 if (!packet_view
.data
) {
1612 ERR("Invalid metadata packet length announced by header");
1617 pthread_mutex_lock(&metadata_stream
->lock
);
1618 ret
= stream_write(metadata_stream
, &packet_view
,
1619 metadata_payload_header
.padding_size
);
1620 pthread_mutex_unlock(&metadata_stream
->lock
);
1626 stream_put(metadata_stream
);
1632 * relay_send_version: send relayd version number
1634 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1635 struct relay_connection
*conn
,
1636 const struct lttng_buffer_view
*payload
)
1640 struct lttcomm_relayd_version reply
, msg
;
1641 bool compatible
= true;
1643 conn
->version_check_done
= true;
1645 /* Get version from the other side. */
1646 if (payload
->size
< sizeof(msg
)) {
1647 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1648 sizeof(msg
), payload
->size
);
1653 memcpy(&msg
, payload
->data
, sizeof(msg
));
1654 msg
.major
= be32toh(msg
.major
);
1655 msg
.minor
= be32toh(msg
.minor
);
1657 memset(&reply
, 0, sizeof(reply
));
1658 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1659 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1661 /* Major versions must be the same */
1662 if (reply
.major
!= msg
.major
) {
1663 DBG("Incompatible major versions (%u vs %u), deleting session",
1664 reply
.major
, msg
.major
);
1668 conn
->major
= reply
.major
;
1669 /* We adapt to the lowest compatible version */
1670 if (reply
.minor
<= msg
.minor
) {
1671 conn
->minor
= reply
.minor
;
1673 conn
->minor
= msg
.minor
;
1676 reply
.major
= htobe32(reply
.major
);
1677 reply
.minor
= htobe32(reply
.minor
);
1678 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1680 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1681 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1694 DBG("Version check done using protocol %u.%u", conn
->major
,
1702 * Check for data pending for a given stream id from the session daemon.
1704 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1705 struct relay_connection
*conn
,
1706 const struct lttng_buffer_view
*payload
)
1708 struct relay_session
*session
= conn
->session
;
1709 struct lttcomm_relayd_data_pending msg
;
1710 struct lttcomm_relayd_generic_reply reply
;
1711 struct relay_stream
*stream
;
1714 uint64_t stream_seq
;
1716 DBG("Data pending command received");
1718 if (!session
|| !conn
->version_check_done
) {
1719 ERR("Trying to check for data before version check");
1721 goto end_no_session
;
1724 if (payload
->size
< sizeof(msg
)) {
1725 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1726 sizeof(msg
), payload
->size
);
1728 goto end_no_session
;
1730 memcpy(&msg
, payload
->data
, sizeof(msg
));
1731 msg
.stream_id
= be64toh(msg
.stream_id
);
1732 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1734 stream
= stream_get_by_id(msg
.stream_id
);
1735 if (stream
== NULL
) {
1740 pthread_mutex_lock(&stream
->lock
);
1742 if (session_streams_have_index(session
)) {
1744 * Ensure that both the index and stream data have been
1745 * flushed up to the requested point.
1747 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1749 stream_seq
= stream
->prev_data_seq
;
1751 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
1752 ", prev_index_seq %" PRIu64
1753 ", and last_seq %" PRIu64
, msg
.stream_id
,
1754 stream
->prev_data_seq
, stream
->prev_index_seq
,
1755 msg
.last_net_seq_num
);
1757 /* Avoid wrapping issue */
1758 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
1759 /* Data has in fact been written and is NOT pending */
1762 /* Data still being streamed thus pending */
1766 stream
->data_pending_check_done
= true;
1767 pthread_mutex_unlock(&stream
->lock
);
1772 memset(&reply
, 0, sizeof(reply
));
1773 reply
.ret_code
= htobe32(ret
);
1774 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1775 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1776 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1786 * Wait for the control socket to reach a quiescent state.
1788 * Note that for now, when receiving this command from the session
1789 * daemon, this means that every subsequent commands or data received on
1790 * the control socket has been handled. So, this is why we simply return
1793 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
1794 struct relay_connection
*conn
,
1795 const struct lttng_buffer_view
*payload
)
1799 struct relay_stream
*stream
;
1800 struct lttcomm_relayd_quiescent_control msg
;
1801 struct lttcomm_relayd_generic_reply reply
;
1803 DBG("Checking quiescent state on control socket");
1805 if (!conn
->session
|| !conn
->version_check_done
) {
1806 ERR("Trying to check for data before version check");
1808 goto end_no_session
;
1811 if (payload
->size
< sizeof(msg
)) {
1812 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
1813 sizeof(msg
), payload
->size
);
1815 goto end_no_session
;
1817 memcpy(&msg
, payload
->data
, sizeof(msg
));
1818 msg
.stream_id
= be64toh(msg
.stream_id
);
1820 stream
= stream_get_by_id(msg
.stream_id
);
1824 pthread_mutex_lock(&stream
->lock
);
1825 stream
->data_pending_check_done
= true;
1826 pthread_mutex_unlock(&stream
->lock
);
1828 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
1831 memset(&reply
, 0, sizeof(reply
));
1832 reply
.ret_code
= htobe32(LTTNG_OK
);
1833 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1834 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1835 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
1847 * Initialize a data pending command. This means that a consumer is about
1848 * to ask for data pending for each stream it holds. Simply iterate over
1849 * all streams of a session and set the data_pending_check_done flag.
1851 * This command returns to the client a LTTNG_OK code.
1853 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1854 struct relay_connection
*conn
,
1855 const struct lttng_buffer_view
*payload
)
1859 struct lttng_ht_iter iter
;
1860 struct lttcomm_relayd_begin_data_pending msg
;
1861 struct lttcomm_relayd_generic_reply reply
;
1862 struct relay_stream
*stream
;
1867 DBG("Init streams for data pending");
1869 if (!conn
->session
|| !conn
->version_check_done
) {
1870 ERR("Trying to check for data before version check");
1872 goto end_no_session
;
1875 if (payload
->size
< sizeof(msg
)) {
1876 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
1877 sizeof(msg
), payload
->size
);
1879 goto end_no_session
;
1881 memcpy(&msg
, payload
->data
, sizeof(msg
));
1882 msg
.session_id
= be64toh(msg
.session_id
);
1885 * Iterate over all streams to set the begin data pending flag.
1886 * For now, the streams are indexed by stream handle so we have
1887 * to iterate over all streams to find the one associated with
1888 * the right session_id.
1891 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1893 if (!stream_get(stream
)) {
1896 if (stream
->trace
->session
->id
== msg
.session_id
) {
1897 pthread_mutex_lock(&stream
->lock
);
1898 stream
->data_pending_check_done
= false;
1899 pthread_mutex_unlock(&stream
->lock
);
1900 DBG("Set begin data pending flag to stream %" PRIu64
,
1901 stream
->stream_handle
);
1907 memset(&reply
, 0, sizeof(reply
));
1908 /* All good, send back reply. */
1909 reply
.ret_code
= htobe32(LTTNG_OK
);
1911 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1912 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1913 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
1925 * End data pending command. This will check, for a given session id, if
1926 * each stream associated with it has its data_pending_check_done flag
1927 * set. If not, this means that the client lost track of the stream but
1928 * the data is still being streamed on our side. In this case, we inform
1929 * the client that data is in flight.
1931 * Return to the client if there is data in flight or not with a ret_code.
1933 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1934 struct relay_connection
*conn
,
1935 const struct lttng_buffer_view
*payload
)
1939 struct lttng_ht_iter iter
;
1940 struct lttcomm_relayd_end_data_pending msg
;
1941 struct lttcomm_relayd_generic_reply reply
;
1942 struct relay_stream
*stream
;
1943 uint32_t is_data_inflight
= 0;
1945 DBG("End data pending command");
1947 if (!conn
->session
|| !conn
->version_check_done
) {
1948 ERR("Trying to check for data before version check");
1950 goto end_no_session
;
1953 if (payload
->size
< sizeof(msg
)) {
1954 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
1955 sizeof(msg
), payload
->size
);
1957 goto end_no_session
;
1959 memcpy(&msg
, payload
->data
, sizeof(msg
));
1960 msg
.session_id
= be64toh(msg
.session_id
);
1963 * Iterate over all streams to see if the begin data pending
1967 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1969 if (!stream_get(stream
)) {
1972 if (stream
->trace
->session
->id
!= msg
.session_id
) {
1976 pthread_mutex_lock(&stream
->lock
);
1977 if (!stream
->data_pending_check_done
) {
1978 uint64_t stream_seq
;
1980 if (session_streams_have_index(conn
->session
)) {
1982 * Ensure that both the index and stream data have been
1983 * flushed up to the requested point.
1985 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1987 stream_seq
= stream
->prev_data_seq
;
1989 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
1990 is_data_inflight
= 1;
1991 DBG("Data is still in flight for stream %" PRIu64
,
1992 stream
->stream_handle
);
1993 pthread_mutex_unlock(&stream
->lock
);
1998 pthread_mutex_unlock(&stream
->lock
);
2003 memset(&reply
, 0, sizeof(reply
));
2004 /* All good, send back reply. */
2005 reply
.ret_code
= htobe32(is_data_inflight
);
2007 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2008 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2009 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2021 * Receive an index for a specific stream.
2023 * Return 0 on success else a negative value.
2025 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2026 struct relay_connection
*conn
,
2027 const struct lttng_buffer_view
*payload
)
2031 struct relay_session
*session
= conn
->session
;
2032 struct lttcomm_relayd_index index_info
;
2033 struct lttcomm_relayd_generic_reply reply
;
2034 struct relay_stream
*stream
;
2039 DBG("Relay receiving index");
2041 if (!session
|| !conn
->version_check_done
) {
2042 ERR("Trying to close a stream before version check");
2044 goto end_no_session
;
2047 msg_len
= lttcomm_relayd_index_len(
2048 lttng_to_index_major(conn
->major
, conn
->minor
),
2049 lttng_to_index_minor(conn
->major
, conn
->minor
));
2050 if (payload
->size
< msg_len
) {
2051 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2052 msg_len
, payload
->size
);
2054 goto end_no_session
;
2056 memcpy(&index_info
, payload
->data
, msg_len
);
2057 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2058 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2059 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2060 index_info
.content_size
= be64toh(index_info
.content_size
);
2061 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2062 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2063 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2064 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2066 if (conn
->minor
>= 8) {
2067 index_info
.stream_instance_id
=
2068 be64toh(index_info
.stream_instance_id
);
2069 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2072 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2074 ERR("stream_get_by_id not found");
2079 pthread_mutex_lock(&stream
->lock
);
2080 ret
= stream_add_index(stream
, &index_info
);
2081 pthread_mutex_unlock(&stream
->lock
);
2083 goto end_stream_put
;
2089 memset(&reply
, 0, sizeof(reply
));
2091 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2093 reply
.ret_code
= htobe32(LTTNG_OK
);
2095 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2096 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2097 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2106 * Receive the streams_sent message.
2108 * Return 0 on success else a negative value.
2110 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2111 struct relay_connection
*conn
,
2112 const struct lttng_buffer_view
*payload
)
2116 struct lttcomm_relayd_generic_reply reply
;
2120 DBG("Relay receiving streams_sent");
2122 if (!conn
->session
|| !conn
->version_check_done
) {
2123 ERR("Trying to close a stream before version check");
2125 goto end_no_session
;
2129 * Publish every pending stream in the connection recv list which are
2130 * now ready to be used by the viewer.
2132 publish_connection_local_streams(conn
);
2134 memset(&reply
, 0, sizeof(reply
));
2135 reply
.ret_code
= htobe32(LTTNG_OK
);
2136 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2137 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2138 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2151 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2152 * session rotation feature (not the tracefile rotation feature).
2154 static int relay_rotate_session_streams(
2155 const struct lttcomm_relayd_hdr
*recv_hdr
,
2156 struct relay_connection
*conn
,
2157 const struct lttng_buffer_view
*payload
)
2162 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2163 struct relay_session
*session
= conn
->session
;
2164 struct lttcomm_relayd_rotate_streams rotate_streams
;
2165 struct lttcomm_relayd_generic_reply reply
= {};
2166 struct relay_stream
*stream
= NULL
;
2167 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2168 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2169 struct lttng_buffer_view stream_positions
;
2170 char chunk_id_buf
[MAX_INT_DEC_LEN(uint64_t)];
2171 const char *chunk_id_str
= "none";
2173 if (!session
|| !conn
->version_check_done
) {
2174 ERR("Trying to rotate a stream before version check");
2179 if (session
->major
== 2 && session
->minor
< 11) {
2180 ERR("Unsupported feature before 2.11");
2185 if (payload
->size
< header_len
) {
2186 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2187 header_len
, payload
->size
);
2192 memcpy(&rotate_streams
, payload
->data
, header_len
);
2194 /* Convert header to host endianness. */
2195 rotate_streams
= (typeof(rotate_streams
)) {
2196 .stream_count
= be32toh(rotate_streams
.stream_count
),
2197 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2198 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2199 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2203 if (rotate_streams
.new_chunk_id
.is_set
) {
2205 * Retrieve the trace chunk the stream must transition to. As
2206 * per the protocol, this chunk should have been created
2207 * before this command is received.
2209 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2210 sessiond_trace_chunk_registry
,
2211 session
->sessiond_uuid
, session
->id
,
2212 rotate_streams
.new_chunk_id
.value
);
2213 if (!next_trace_chunk
) {
2214 char uuid_str
[UUID_STR_LEN
];
2216 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2217 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2218 ", trace_chunk_id = %" PRIu64
,
2219 uuid_str
, session
->id
,
2220 rotate_streams
.new_chunk_id
.value
);
2221 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2226 ret
= snprintf(chunk_id_buf
, sizeof(chunk_id_buf
), "%" PRIu64
,
2227 rotate_streams
.new_chunk_id
.value
);
2228 if (ret
< 0 || ret
>= sizeof(chunk_id_buf
)) {
2229 chunk_id_str
= "formatting error";
2231 chunk_id_str
= chunk_id_buf
;
2233 session
->has_rotated
= true;
2236 DBG("Rotate %" PRIu32
" streams of session \"%s\" to chunk \"%s\"",
2237 rotate_streams
.stream_count
, session
->session_name
,
2240 stream_positions
= lttng_buffer_view_from_view(payload
,
2241 sizeof(rotate_streams
), -1);
2242 if (!stream_positions
.data
||
2243 stream_positions
.size
<
2244 (rotate_streams
.stream_count
*
2245 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2246 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2251 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2252 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2253 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2254 const struct lttcomm_relayd_stream_rotation_position pos
= {
2255 .stream_id
= be64toh(position_comm
->stream_id
),
2256 .rotate_at_seq_num
= be64toh(
2257 position_comm
->rotate_at_seq_num
),
2260 stream
= stream_get_by_id(pos
.stream_id
);
2262 reply_code
= LTTNG_ERR_INVALID
;
2267 pthread_mutex_lock(&stream
->lock
);
2268 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2269 pos
.rotate_at_seq_num
);
2270 pthread_mutex_unlock(&stream
->lock
);
2272 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2280 reply_code
= LTTNG_OK
;
2286 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2287 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2288 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2289 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2290 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2297 lttng_trace_chunk_put(next_trace_chunk
);
2301 static int init_session_output_directory_handle(struct relay_session
*session
,
2302 struct lttng_directory_handle
*handle
)
2306 * relayd_output_path/session_directory
2307 * e.g. /home/user/lttng-traces/hostname/session_name
2309 char *full_session_path
= NULL
;
2311 pthread_mutex_lock(&session
->lock
);
2312 full_session_path
= create_output_path(session
->output_path
);
2313 if (!full_session_path
) {
2318 ret
= utils_mkdir_recursive(
2319 full_session_path
, S_IRWXU
| S_IRWXG
, -1, -1);
2321 ERR("Failed to create session output path \"%s\"",
2326 ret
= lttng_directory_handle_init(handle
, full_session_path
);
2331 pthread_mutex_unlock(&session
->lock
);
2332 free(full_session_path
);
2337 * relay_create_trace_chunk: create a new trace chunk
2339 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2340 struct relay_connection
*conn
,
2341 const struct lttng_buffer_view
*payload
)
2345 struct relay_session
*session
= conn
->session
;
2346 struct lttcomm_relayd_create_trace_chunk
*msg
;
2347 struct lttcomm_relayd_generic_reply reply
= {};
2348 struct lttng_buffer_view header_view
;
2349 struct lttng_buffer_view chunk_name_view
;
2350 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2351 enum lttng_error_code reply_code
= LTTNG_OK
;
2352 enum lttng_trace_chunk_status chunk_status
;
2353 struct lttng_directory_handle session_output
;
2355 if (!session
|| !conn
->version_check_done
) {
2356 ERR("Trying to create a trace chunk before version check");
2361 if (session
->major
== 2 && session
->minor
< 11) {
2362 ERR("Chunk creation command is unsupported before 2.11");
2367 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2368 if (!header_view
.data
) {
2369 ERR("Failed to receive payload of chunk creation command");
2374 /* Convert to host endianness. */
2375 msg
= (typeof(msg
)) header_view
.data
;
2376 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2377 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2378 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2380 chunk
= lttng_trace_chunk_create(
2381 msg
->chunk_id
, msg
->creation_timestamp
);
2383 ERR("Failed to create trace chunk in trace chunk creation command");
2385 reply_code
= LTTNG_ERR_NOMEM
;
2389 if (msg
->override_name_length
) {
2392 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2394 msg
->override_name_length
);
2395 name
= chunk_name_view
.data
;
2396 if (!name
|| name
[msg
->override_name_length
- 1]) {
2397 ERR("Failed to receive payload of chunk creation command");
2399 reply_code
= LTTNG_ERR_INVALID
;
2403 chunk_status
= lttng_trace_chunk_override_name(
2404 chunk
, chunk_name_view
.data
);
2405 switch (chunk_status
) {
2406 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2408 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2409 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2410 reply_code
= LTTNG_ERR_INVALID
;
2414 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2415 reply_code
= LTTNG_ERR_UNK
;
2421 ret
= init_session_output_directory_handle(
2422 conn
->session
, &session_output
);
2424 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2428 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2429 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2430 reply_code
= LTTNG_ERR_UNK
;
2435 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, &session_output
);
2436 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2437 reply_code
= LTTNG_ERR_UNK
;
2442 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2443 sessiond_trace_chunk_registry
,
2444 conn
->session
->sessiond_uuid
,
2447 if (!published_chunk
) {
2448 char uuid_str
[UUID_STR_LEN
];
2450 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2451 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2456 reply_code
= LTTNG_ERR_NOMEM
;
2460 pthread_mutex_lock(&conn
->session
->lock
);
2461 if (conn
->session
->pending_closure_trace_chunk
) {
2463 * Invalid; this means a second create_trace_chunk command was
2464 * received before a close_trace_chunk.
2466 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2467 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2469 goto end_unlock_session
;
2471 conn
->session
->pending_closure_trace_chunk
=
2472 conn
->session
->current_trace_chunk
;
2473 conn
->session
->current_trace_chunk
= published_chunk
;
2474 published_chunk
= NULL
;
2476 pthread_mutex_unlock(&conn
->session
->lock
);
2478 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2479 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2481 sizeof(struct lttcomm_relayd_generic_reply
),
2483 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2484 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2489 lttng_trace_chunk_put(chunk
);
2490 lttng_trace_chunk_put(published_chunk
);
2491 lttng_directory_handle_fini(&session_output
);
2496 * relay_close_trace_chunk: close a trace chunk
2498 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2499 struct relay_connection
*conn
,
2500 const struct lttng_buffer_view
*payload
)
2504 struct relay_session
*session
= conn
->session
;
2505 struct lttcomm_relayd_close_trace_chunk
*msg
;
2506 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
2507 struct lttng_buffer_view header_view
;
2508 struct lttng_trace_chunk
*chunk
= NULL
;
2509 enum lttng_error_code reply_code
= LTTNG_OK
;
2510 enum lttng_trace_chunk_status chunk_status
;
2512 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2513 time_t close_timestamp
;
2514 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2515 size_t path_length
= 0;
2516 const char *chunk_name
= NULL
;
2517 struct lttng_dynamic_buffer reply_payload
;
2519 lttng_dynamic_buffer_init(&reply_payload
);
2521 if (!session
|| !conn
->version_check_done
) {
2522 ERR("Trying to close a trace chunk before version check");
2527 if (session
->major
== 2 && session
->minor
< 11) {
2528 ERR("Chunk close command is unsupported before 2.11");
2533 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2534 if (!header_view
.data
) {
2535 ERR("Failed to receive payload of chunk close command");
2540 /* Convert to host endianness. */
2541 msg
= (typeof(msg
)) header_view
.data
;
2542 chunk_id
= be64toh(msg
->chunk_id
);
2543 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2544 close_command
= (typeof(close_command
)){
2545 .value
= be32toh(msg
->close_command
.value
),
2546 .is_set
= msg
->close_command
.is_set
,
2549 chunk
= sessiond_trace_chunk_registry_get_chunk(
2550 sessiond_trace_chunk_registry
,
2551 conn
->session
->sessiond_uuid
,
2555 char uuid_str
[UUID_STR_LEN
];
2557 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2558 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2563 reply_code
= LTTNG_ERR_NOMEM
;
2567 pthread_mutex_lock(&session
->lock
);
2568 if (session
->pending_closure_trace_chunk
&&
2569 session
->pending_closure_trace_chunk
!= chunk
) {
2570 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2571 session
->session_name
);
2572 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2574 goto end_unlock_session
;
2577 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2578 chunk
, close_timestamp
);
2579 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2580 ERR("Failed to set trace chunk close timestamp");
2582 reply_code
= LTTNG_ERR_UNK
;
2583 goto end_unlock_session
;
2586 if (close_command
.is_set
) {
2587 chunk_status
= lttng_trace_chunk_set_close_command(
2588 chunk
, close_command
.value
);
2589 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2591 reply_code
= LTTNG_ERR_INVALID
;
2592 goto end_unlock_session
;
2595 chunk_status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, NULL
);
2596 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2597 ERR("Failed to get chunk name");
2599 reply_code
= LTTNG_ERR_UNK
;
2600 goto end_unlock_session
;
2602 if (!session
->has_rotated
&& !session
->snapshot
) {
2603 ret
= lttng_strncpy(closed_trace_chunk_path
,
2604 session
->output_path
,
2605 sizeof(closed_trace_chunk_path
));
2607 ERR("Failed to send trace chunk path: path length of %zu bytes exceeds the maximal allowed length of %zu bytes",
2608 strlen(session
->output_path
),
2609 sizeof(closed_trace_chunk_path
));
2610 reply_code
= LTTNG_ERR_NOMEM
;
2612 goto end_unlock_session
;
2615 if (session
->snapshot
) {
2616 ret
= snprintf(closed_trace_chunk_path
,
2617 sizeof(closed_trace_chunk_path
),
2618 "%s/%s", session
->output_path
,
2621 ret
= snprintf(closed_trace_chunk_path
,
2622 sizeof(closed_trace_chunk_path
),
2623 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
2625 session
->output_path
, chunk_name
);
2627 if (ret
< 0 || ret
== sizeof(closed_trace_chunk_path
)) {
2628 ERR("Failed to format closed trace chunk resulting path");
2629 reply_code
= ret
< 0 ? LTTNG_ERR_UNK
: LTTNG_ERR_NOMEM
;
2631 goto end_unlock_session
;
2634 DBG("Reply chunk path on close: %s", closed_trace_chunk_path
);
2635 path_length
= strlen(closed_trace_chunk_path
) + 1;
2636 if (path_length
> UINT32_MAX
) {
2637 ERR("Closed trace chunk path exceeds the maximal length allowed by the protocol");
2639 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2640 goto end_unlock_session
;
2643 if (session
->current_trace_chunk
== chunk
) {
2645 * After a trace chunk close command, no new streams
2646 * referencing the chunk may be created. Hence, on the
2647 * event that no new trace chunk have been created for
2648 * the session, the reference to the current trace chunk
2649 * is released in order to allow it to be reclaimed when
2650 * the last stream releases its reference to it.
2652 lttng_trace_chunk_put(session
->current_trace_chunk
);
2653 session
->current_trace_chunk
= NULL
;
2655 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2656 session
->pending_closure_trace_chunk
= NULL
;
2658 pthread_mutex_unlock(&session
->lock
);
2661 reply
.generic
.ret_code
= htobe32((uint32_t) reply_code
);
2662 reply
.path_length
= htobe32((uint32_t) path_length
);
2663 ret
= lttng_dynamic_buffer_append(
2664 &reply_payload
, &reply
, sizeof(reply
));
2666 ERR("Failed to append \"close trace chunk\" command reply header to payload buffer");
2670 if (reply_code
== LTTNG_OK
) {
2671 ret
= lttng_dynamic_buffer_append(&reply_payload
,
2672 closed_trace_chunk_path
, path_length
);
2674 ERR("Failed to append \"close trace chunk\" command reply path to payload buffer");
2679 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2683 if (send_ret
< reply_payload
.size
) {
2684 ERR("Failed to send \"close trace chunk\" command reply of %zu bytes (ret = %zd)",
2685 reply_payload
.size
, send_ret
);
2690 lttng_trace_chunk_put(chunk
);
2691 lttng_dynamic_buffer_reset(&reply_payload
);
2696 * relay_trace_chunk_exists: check if a trace chunk exists
2698 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2699 struct relay_connection
*conn
,
2700 const struct lttng_buffer_view
*payload
)
2704 struct relay_session
*session
= conn
->session
;
2705 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2706 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2707 struct lttng_buffer_view header_view
;
2708 struct lttng_trace_chunk
*chunk
= NULL
;
2711 if (!session
|| !conn
->version_check_done
) {
2712 ERR("Trying to close a trace chunk before version check");
2717 if (session
->major
== 2 && session
->minor
< 11) {
2718 ERR("Chunk close command is unsupported before 2.11");
2723 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2724 if (!header_view
.data
) {
2725 ERR("Failed to receive payload of chunk close command");
2730 /* Convert to host endianness. */
2731 msg
= (typeof(msg
)) header_view
.data
;
2732 chunk_id
= be64toh(msg
->chunk_id
);
2734 chunk
= sessiond_trace_chunk_registry_get_chunk(
2735 sessiond_trace_chunk_registry
,
2736 conn
->session
->sessiond_uuid
,
2740 reply
= (typeof(reply
)) {
2741 .generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
),
2742 .trace_chunk_exists
= !!chunk
,
2744 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2745 &reply
, sizeof(reply
), 0);
2746 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2747 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2752 lttng_trace_chunk_put(chunk
);
2756 #define DBG_CMD(cmd_name, conn) \
2757 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2759 static int relay_process_control_command(struct relay_connection
*conn
,
2760 const struct lttcomm_relayd_hdr
*header
,
2761 const struct lttng_buffer_view
*payload
)
2765 switch (header
->cmd
) {
2766 case RELAYD_CREATE_SESSION
:
2767 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2768 ret
= relay_create_session(header
, conn
, payload
);
2770 case RELAYD_ADD_STREAM
:
2771 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2772 ret
= relay_add_stream(header
, conn
, payload
);
2774 case RELAYD_START_DATA
:
2775 DBG_CMD("RELAYD_START_DATA", conn
);
2776 ret
= relay_start(header
, conn
, payload
);
2778 case RELAYD_SEND_METADATA
:
2779 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2780 ret
= relay_recv_metadata(header
, conn
, payload
);
2782 case RELAYD_VERSION
:
2783 DBG_CMD("RELAYD_VERSION", conn
);
2784 ret
= relay_send_version(header
, conn
, payload
);
2786 case RELAYD_CLOSE_STREAM
:
2787 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2788 ret
= relay_close_stream(header
, conn
, payload
);
2790 case RELAYD_DATA_PENDING
:
2791 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2792 ret
= relay_data_pending(header
, conn
, payload
);
2794 case RELAYD_QUIESCENT_CONTROL
:
2795 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2796 ret
= relay_quiescent_control(header
, conn
, payload
);
2798 case RELAYD_BEGIN_DATA_PENDING
:
2799 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2800 ret
= relay_begin_data_pending(header
, conn
, payload
);
2802 case RELAYD_END_DATA_PENDING
:
2803 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2804 ret
= relay_end_data_pending(header
, conn
, payload
);
2806 case RELAYD_SEND_INDEX
:
2807 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2808 ret
= relay_recv_index(header
, conn
, payload
);
2810 case RELAYD_STREAMS_SENT
:
2811 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2812 ret
= relay_streams_sent(header
, conn
, payload
);
2814 case RELAYD_RESET_METADATA
:
2815 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2816 ret
= relay_reset_metadata(header
, conn
, payload
);
2818 case RELAYD_ROTATE_STREAMS
:
2819 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
2820 ret
= relay_rotate_session_streams(header
, conn
, payload
);
2822 case RELAYD_CREATE_TRACE_CHUNK
:
2823 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
2824 ret
= relay_create_trace_chunk(header
, conn
, payload
);
2826 case RELAYD_CLOSE_TRACE_CHUNK
:
2827 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
2828 ret
= relay_close_trace_chunk(header
, conn
, payload
);
2830 case RELAYD_TRACE_CHUNK_EXISTS
:
2831 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
2832 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
2834 case RELAYD_UPDATE_SYNC_INFO
:
2836 ERR("Received unknown command (%u)", header
->cmd
);
2837 relay_unknown_command(conn
);
2846 static enum relay_connection_status
relay_process_control_receive_payload(
2847 struct relay_connection
*conn
)
2850 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2851 struct lttng_dynamic_buffer
*reception_buffer
=
2852 &conn
->protocol
.ctrl
.reception_buffer
;
2853 struct ctrl_connection_state_receive_payload
*state
=
2854 &conn
->protocol
.ctrl
.state
.receive_payload
;
2855 struct lttng_buffer_view payload_view
;
2857 if (state
->left_to_receive
== 0) {
2858 /* Short-circuit for payload-less commands. */
2859 goto reception_complete
;
2862 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2863 reception_buffer
->data
+ state
->received
,
2864 state
->left_to_receive
, MSG_DONTWAIT
);
2866 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2867 PERROR("Unable to receive command payload on sock %d",
2869 status
= RELAY_CONNECTION_STATUS_ERROR
;
2872 } else if (ret
== 0) {
2873 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2874 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2879 assert(ret
<= state
->left_to_receive
);
2881 state
->left_to_receive
-= ret
;
2882 state
->received
+= ret
;
2884 if (state
->left_to_receive
> 0) {
2886 * Can't transition to the protocol's next state, wait to
2887 * receive the rest of the header.
2889 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2890 state
->received
, state
->left_to_receive
,
2896 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
2897 conn
->sock
->fd
, state
->received
);
2899 * The payload required to process the command has been received.
2900 * A view to the reception buffer is forwarded to the various
2901 * commands and the state of the control is reset on success.
2903 * Commands are responsible for sending their reply to the peer.
2905 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
2907 ret
= relay_process_control_command(conn
,
2908 &state
->header
, &payload_view
);
2910 status
= RELAY_CONNECTION_STATUS_ERROR
;
2914 ret
= connection_reset_protocol_state(conn
);
2916 status
= RELAY_CONNECTION_STATUS_ERROR
;
2922 static enum relay_connection_status
relay_process_control_receive_header(
2923 struct relay_connection
*conn
)
2926 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2927 struct lttcomm_relayd_hdr header
;
2928 struct lttng_dynamic_buffer
*reception_buffer
=
2929 &conn
->protocol
.ctrl
.reception_buffer
;
2930 struct ctrl_connection_state_receive_header
*state
=
2931 &conn
->protocol
.ctrl
.state
.receive_header
;
2933 assert(state
->left_to_receive
!= 0);
2935 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2936 reception_buffer
->data
+ state
->received
,
2937 state
->left_to_receive
, MSG_DONTWAIT
);
2939 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2940 PERROR("Unable to receive control command header on sock %d",
2942 status
= RELAY_CONNECTION_STATUS_ERROR
;
2945 } else if (ret
== 0) {
2946 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2947 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2952 assert(ret
<= state
->left_to_receive
);
2954 state
->left_to_receive
-= ret
;
2955 state
->received
+= ret
;
2957 if (state
->left_to_receive
> 0) {
2959 * Can't transition to the protocol's next state, wait to
2960 * receive the rest of the header.
2962 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2963 state
->received
, state
->left_to_receive
,
2968 /* Transition to next state: receiving the command's payload. */
2969 conn
->protocol
.ctrl
.state_id
=
2970 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
2971 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
2972 header
.circuit_id
= be64toh(header
.circuit_id
);
2973 header
.data_size
= be64toh(header
.data_size
);
2974 header
.cmd
= be32toh(header
.cmd
);
2975 header
.cmd_version
= be32toh(header
.cmd_version
);
2976 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
2977 &header
, sizeof(header
));
2979 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
2980 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
2983 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
2984 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
2986 status
= RELAY_CONNECTION_STATUS_ERROR
;
2990 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
2992 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
2993 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
2996 status
= RELAY_CONNECTION_STATUS_ERROR
;
3000 if (header
.data_size
== 0) {
3002 * Manually invoke the next state as the poll loop
3003 * will not wake-up to allow us to proceed further.
3005 status
= relay_process_control_receive_payload(conn
);
3012 * Process the commands received on the control socket
3014 static enum relay_connection_status
relay_process_control(
3015 struct relay_connection
*conn
)
3017 enum relay_connection_status status
;
3019 switch (conn
->protocol
.ctrl
.state_id
) {
3020 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3021 status
= relay_process_control_receive_header(conn
);
3023 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3024 status
= relay_process_control_receive_payload(conn
);
3027 ERR("Unknown control connection protocol state encountered.");
3034 static enum relay_connection_status
relay_process_data_receive_header(
3035 struct relay_connection
*conn
)
3038 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3039 struct data_connection_state_receive_header
*state
=
3040 &conn
->protocol
.data
.state
.receive_header
;
3041 struct lttcomm_relayd_data_hdr header
;
3042 struct relay_stream
*stream
;
3044 assert(state
->left_to_receive
!= 0);
3046 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3047 state
->header_reception_buffer
+ state
->received
,
3048 state
->left_to_receive
, MSG_DONTWAIT
);
3050 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3051 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3052 status
= RELAY_CONNECTION_STATUS_ERROR
;
3055 } else if (ret
== 0) {
3056 /* Orderly shutdown. Not necessary to print an error. */
3057 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3058 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3063 assert(ret
<= state
->left_to_receive
);
3065 state
->left_to_receive
-= ret
;
3066 state
->received
+= ret
;
3068 if (state
->left_to_receive
> 0) {
3070 * Can't transition to the protocol's next state, wait to
3071 * receive the rest of the header.
3073 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3074 state
->received
, state
->left_to_receive
,
3079 /* Transition to next state: receiving the payload. */
3080 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3082 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3083 header
.circuit_id
= be64toh(header
.circuit_id
);
3084 header
.stream_id
= be64toh(header
.stream_id
);
3085 header
.data_size
= be32toh(header
.data_size
);
3086 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3087 header
.padding_size
= be32toh(header
.padding_size
);
3088 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3090 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3092 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3093 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3095 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3096 conn
->sock
->fd
, header
.circuit_id
,
3097 header
.stream_id
, header
.data_size
,
3098 header
.net_seq_num
, header
.padding_size
);
3100 stream
= stream_get_by_id(header
.stream_id
);
3102 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3104 /* Protocol error. */
3105 status
= RELAY_CONNECTION_STATUS_ERROR
;
3109 pthread_mutex_lock(&stream
->lock
);
3110 /* Prepare stream for the reception of a new packet. */
3111 ret
= stream_init_packet(stream
, header
.data_size
,
3112 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3113 pthread_mutex_unlock(&stream
->lock
);
3115 ERR("Failed to rotate stream output file");
3116 status
= RELAY_CONNECTION_STATUS_ERROR
;
3117 goto end_stream_unlock
;
3126 static enum relay_connection_status
relay_process_data_receive_payload(
3127 struct relay_connection
*conn
)
3130 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3131 struct relay_stream
*stream
;
3132 struct data_connection_state_receive_payload
*state
=
3133 &conn
->protocol
.data
.state
.receive_payload
;
3134 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3135 char data_buffer
[chunk_size
];
3136 bool partial_recv
= false;
3137 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3138 uint64_t left_to_receive
= state
->left_to_receive
;
3139 struct relay_session
*session
;
3141 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3142 state
->header
.stream_id
, state
->header
.net_seq_num
,
3143 state
->received
, left_to_receive
);
3145 stream
= stream_get_by_id(state
->header
.stream_id
);
3147 /* Protocol error. */
3148 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3149 state
->header
.stream_id
);
3150 status
= RELAY_CONNECTION_STATUS_ERROR
;
3154 pthread_mutex_lock(&stream
->lock
);
3155 session
= stream
->trace
->session
;
3156 if (!conn
->session
) {
3157 ret
= connection_set_session(conn
, session
);
3159 status
= RELAY_CONNECTION_STATUS_ERROR
;
3160 goto end_stream_unlock
;
3165 * The size of the "chunk" received on any iteration is bounded by:
3166 * - the data left to receive,
3167 * - the data immediately available on the socket,
3168 * - the on-stack data buffer
3170 while (left_to_receive
> 0 && !partial_recv
) {
3171 size_t recv_size
= min(left_to_receive
, chunk_size
);
3172 struct lttng_buffer_view packet_chunk
;
3174 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3175 recv_size
, MSG_DONTWAIT
);
3177 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3178 PERROR("Socket %d error", conn
->sock
->fd
);
3179 status
= RELAY_CONNECTION_STATUS_ERROR
;
3181 goto end_stream_unlock
;
3182 } else if (ret
== 0) {
3183 /* No more data ready to be consumed on socket. */
3184 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3185 state
->header
.stream_id
);
3186 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3188 } else if (ret
< (int) recv_size
) {
3190 * All the data available on the socket has been
3193 partial_recv
= true;
3197 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3199 assert(packet_chunk
.data
);
3201 ret
= stream_write(stream
, &packet_chunk
, 0);
3203 ERR("Relay error writing data to file");
3204 status
= RELAY_CONNECTION_STATUS_ERROR
;
3205 goto end_stream_unlock
;
3208 left_to_receive
-= recv_size
;
3209 state
->received
+= recv_size
;
3210 state
->left_to_receive
= left_to_receive
;
3213 if (state
->left_to_receive
> 0) {
3215 * Did not receive all the data expected, wait for more data to
3216 * become available on the socket.
3218 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3219 state
->header
.stream_id
, state
->received
,
3220 state
->left_to_receive
);
3221 goto end_stream_unlock
;
3224 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3226 status
= RELAY_CONNECTION_STATUS_ERROR
;
3227 goto end_stream_unlock
;
3230 if (session_streams_have_index(session
)) {
3231 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3232 state
->rotate_index
, &index_flushed
,
3233 state
->header
.data_size
+ state
->header
.padding_size
);
3235 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3236 stream
->stream_handle
,
3237 state
->header
.net_seq_num
, ret
);
3238 status
= RELAY_CONNECTION_STATUS_ERROR
;
3239 goto end_stream_unlock
;
3243 if (stream
->prev_data_seq
== -1ULL) {
3247 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3248 state
->header
.padding_size
, state
->header
.net_seq_num
,
3251 status
= RELAY_CONNECTION_STATUS_ERROR
;
3252 goto end_stream_unlock
;
3256 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3257 * contents of *state which are aliased (union) to the same location as
3258 * the new state. Don't use it beyond this point.
3260 connection_reset_protocol_state(conn
);
3264 close_requested
= stream
->close_requested
;
3265 pthread_mutex_unlock(&stream
->lock
);
3266 if (close_requested
&& left_to_receive
== 0) {
3267 try_stream_close(stream
);
3271 pthread_mutex_lock(&session
->lock
);
3272 uatomic_set(&session
->new_streams
, 1);
3273 pthread_mutex_unlock(&session
->lock
);
3282 * relay_process_data: Process the data received on the data socket
3284 static enum relay_connection_status
relay_process_data(
3285 struct relay_connection
*conn
)
3287 enum relay_connection_status status
;
3289 switch (conn
->protocol
.data
.state_id
) {
3290 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3291 status
= relay_process_data_receive_header(conn
);
3293 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3294 status
= relay_process_data_receive_payload(conn
);
3297 ERR("Unexpected data connection communication state.");
3304 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3308 (void) lttng_poll_del(events
, pollfd
);
3310 ret
= close(pollfd
);
3312 ERR("Closing pollfd %d", pollfd
);
3316 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3317 int pollfd
, struct relay_connection
*conn
)
3319 const char *type_str
;
3321 switch (conn
->type
) {
3326 type_str
= "Control";
3328 case RELAY_VIEWER_COMMAND
:
3329 type_str
= "Viewer Command";
3331 case RELAY_VIEWER_NOTIFICATION
:
3332 type_str
= "Viewer Notification";
3335 type_str
= "Unknown";
3337 cleanup_connection_pollfd(events
, pollfd
);
3338 connection_put(conn
);
3339 DBG("%s connection closed with %d", type_str
, pollfd
);
3343 * This thread does the actual work
3345 static void *relay_thread_worker(void *data
)
3347 int ret
, err
= -1, last_seen_data_fd
= -1;
3349 struct lttng_poll_event events
;
3350 struct lttng_ht
*relay_connections_ht
;
3351 struct lttng_ht_iter iter
;
3352 struct relay_connection
*destroy_conn
= NULL
;
3354 DBG("[thread] Relay worker started");
3356 rcu_register_thread();
3358 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3360 if (testpoint(relayd_thread_worker
)) {
3361 goto error_testpoint
;
3364 health_code_update();
3366 /* table of connections indexed on socket */
3367 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3368 if (!relay_connections_ht
) {
3369 goto relay_connections_ht_error
;
3372 ret
= create_thread_poll_set(&events
, 2);
3374 goto error_poll_create
;
3377 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3384 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3386 health_code_update();
3388 /* Infinite blocking call, waiting for transmission */
3389 DBG3("Relayd worker thread polling...");
3390 health_poll_entry();
3391 ret
= lttng_poll_wait(&events
, -1);
3395 * Restart interrupted system call.
3397 if (errno
== EINTR
) {
3406 * Process control. The control connection is
3407 * prioritized so we don't starve it with high
3408 * throughput tracing data on the data connection.
3410 for (i
= 0; i
< nb_fd
; i
++) {
3411 /* Fetch once the poll data */
3412 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3413 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3415 health_code_update();
3417 /* Thread quit pipe has been closed. Killing thread. */
3418 ret
= check_thread_quit_pipe(pollfd
, revents
);
3424 /* Inspect the relay conn pipe for new connection */
3425 if (pollfd
== relay_conn_pipe
[0]) {
3426 if (revents
& LPOLLIN
) {
3427 struct relay_connection
*conn
;
3429 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3433 lttng_poll_add(&events
, conn
->sock
->fd
,
3434 LPOLLIN
| LPOLLRDHUP
);
3435 connection_ht_add(relay_connections_ht
, conn
);
3436 DBG("Connection socket %d added", conn
->sock
->fd
);
3437 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3438 ERR("Relay connection pipe error");
3441 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3445 struct relay_connection
*ctrl_conn
;
3447 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3448 /* If not found, there is a synchronization issue. */
3451 if (ctrl_conn
->type
== RELAY_DATA
) {
3452 if (revents
& LPOLLIN
) {
3454 * Flag the last seen data fd not deleted. It will be
3455 * used as the last seen fd if any fd gets deleted in
3458 last_notdel_data_fd
= pollfd
;
3460 goto put_ctrl_connection
;
3462 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3464 if (revents
& LPOLLIN
) {
3465 enum relay_connection_status status
;
3467 status
= relay_process_control(ctrl_conn
);
3468 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3470 * On socket error flag the session as aborted to force
3471 * the cleanup of its stream otherwise it can leak
3472 * during the lifetime of the relayd.
3474 * This prevents situations in which streams can be
3475 * left opened because an index was received, the
3476 * control connection is closed, and the data
3477 * connection is closed (uncleanly) before the packet's
3480 * Since the control connection encountered an error,
3481 * it is okay to be conservative and close the
3482 * session right now as we can't rely on the protocol
3483 * being respected anymore.
3485 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3486 session_abort(ctrl_conn
->session
);
3489 /* Clear the connection on error or close. */
3490 relay_thread_close_connection(&events
,
3495 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3496 relay_thread_close_connection(&events
,
3498 if (last_seen_data_fd
== pollfd
) {
3499 last_seen_data_fd
= last_notdel_data_fd
;
3502 ERR("Unexpected poll events %u for control sock %d",
3504 connection_put(ctrl_conn
);
3507 put_ctrl_connection
:
3508 connection_put(ctrl_conn
);
3513 * The last loop handled a control request, go back to poll to make
3514 * sure we prioritise the control socket.
3520 if (last_seen_data_fd
>= 0) {
3521 for (i
= 0; i
< nb_fd
; i
++) {
3522 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3524 health_code_update();
3526 if (last_seen_data_fd
== pollfd
) {
3533 /* Process data connection. */
3534 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3535 /* Fetch the poll data. */
3536 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3537 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3538 struct relay_connection
*data_conn
;
3540 health_code_update();
3543 /* No activity for this FD (poll implementation). */
3547 /* Skip the command pipe. It's handled in the first loop. */
3548 if (pollfd
== relay_conn_pipe
[0]) {
3552 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3554 /* Skip it. Might be removed before. */
3557 if (data_conn
->type
== RELAY_CONTROL
) {
3558 goto put_data_connection
;
3560 assert(data_conn
->type
== RELAY_DATA
);
3562 if (revents
& LPOLLIN
) {
3563 enum relay_connection_status status
;
3565 status
= relay_process_data(data_conn
);
3566 /* Connection closed or error. */
3567 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3569 * On socket error flag the session as aborted to force
3570 * the cleanup of its stream otherwise it can leak
3571 * during the lifetime of the relayd.
3573 * This prevents situations in which streams can be
3574 * left opened because an index was received, the
3575 * control connection is closed, and the data
3576 * connection is closed (uncleanly) before the packet's
3579 * Since the data connection encountered an error,
3580 * it is okay to be conservative and close the
3581 * session right now as we can't rely on the protocol
3582 * being respected anymore.
3584 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3585 session_abort(data_conn
->session
);
3587 relay_thread_close_connection(&events
, pollfd
,
3590 * Every goto restart call sets the last seen fd where
3591 * here we don't really care since we gracefully
3592 * continue the loop after the connection is deleted.
3595 /* Keep last seen port. */
3596 last_seen_data_fd
= pollfd
;
3597 connection_put(data_conn
);
3600 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3601 relay_thread_close_connection(&events
, pollfd
,
3604 ERR("Unknown poll events %u for data sock %d",
3607 put_data_connection
:
3608 connection_put(data_conn
);
3610 last_seen_data_fd
= -1;
3613 /* Normal exit, no error */
3618 /* Cleanup remaining connection object. */
3620 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3623 health_code_update();
3625 session_abort(destroy_conn
->session
);
3628 * No need to grab another ref, because we own
3631 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3636 lttng_poll_clean(&events
);
3638 lttng_ht_destroy(relay_connections_ht
);
3639 relay_connections_ht_error
:
3640 /* Close relay conn pipes */
3641 utils_close_pipe(relay_conn_pipe
);
3643 DBG("Thread exited with error");
3645 DBG("Worker thread cleanup complete");
3649 ERR("Health error occurred in %s", __func__
);
3651 health_unregister(health_relayd
);
3652 rcu_unregister_thread();
3653 lttng_relay_stop_threads();
3658 * Create the relay command pipe to wake thread_manage_apps.
3659 * Closed in cleanup().
3661 static int create_relay_conn_pipe(void)
3665 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3673 int main(int argc
, char **argv
)
3675 int ret
= 0, retval
= 0;
3678 /* Parse arguments */
3680 if (set_options(argc
, argv
)) {
3685 if (set_signal_handler()) {
3690 /* Try to create directory if -o, --output is specified. */
3691 if (opt_output_path
) {
3692 if (*opt_output_path
!= '/') {
3693 ERR("Please specify an absolute path for -o, --output PATH");
3698 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3701 ERR("Unable to create %s", opt_output_path
);
3708 if (opt_daemon
|| opt_background
) {
3711 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3719 * We are in the child. Make sure all other file
3720 * descriptors are closed, in case we are called with
3721 * more opened file descriptors than the standard ones.
3723 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3728 sessiond_trace_chunk_registry
= sessiond_trace_chunk_registry_create();
3729 if (!sessiond_trace_chunk_registry
) {
3730 ERR("Failed to initialize session daemon trace chunk registry");
3732 goto exit_sessiond_trace_chunk_registry
;
3735 /* Initialize thread health monitoring */
3736 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3737 if (!health_relayd
) {
3738 PERROR("health_app_create error");
3740 goto exit_health_app_create
;
3743 /* Create thread quit pipe */
3744 if (init_thread_quit_pipe()) {
3746 goto exit_init_data
;
3749 /* Setup the thread apps communication pipe. */
3750 if (create_relay_conn_pipe()) {
3752 goto exit_init_data
;
3755 /* Init relay command queue. */
3756 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3758 /* Initialize communication library */
3760 lttcomm_inet_init();
3762 /* tables of sessions indexed by session ID */
3763 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3766 goto exit_init_data
;
3769 /* tables of streams indexed by stream ID */
3770 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3771 if (!relay_streams_ht
) {
3773 goto exit_init_data
;
3776 /* tables of streams indexed by stream ID */
3777 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3778 if (!viewer_streams_ht
) {
3780 goto exit_init_data
;
3783 ret
= utils_create_pipe(health_quit_pipe
);
3786 goto exit_health_quit_pipe
;
3789 /* Create thread to manage the client socket */
3790 ret
= pthread_create(&health_thread
, default_pthread_attr(),
3791 thread_manage_health
, (void *) NULL
);
3794 PERROR("pthread_create health");
3796 goto exit_health_thread
;
3799 /* Setup the dispatcher thread */
3800 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
3801 relay_thread_dispatcher
, (void *) NULL
);
3804 PERROR("pthread_create dispatcher");
3806 goto exit_dispatcher_thread
;
3809 /* Setup the worker thread */
3810 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
3811 relay_thread_worker
, NULL
);
3814 PERROR("pthread_create worker");
3816 goto exit_worker_thread
;
3819 /* Setup the listener thread */
3820 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
3821 relay_thread_listener
, (void *) NULL
);
3824 PERROR("pthread_create listener");
3826 goto exit_listener_thread
;
3829 ret
= relayd_live_create(live_uri
);
3831 ERR("Starting live viewer threads");
3837 * This is where we start awaiting program completion (e.g. through
3838 * signal that asks threads to teardown).
3841 ret
= relayd_live_join();
3847 ret
= pthread_join(listener_thread
, &status
);
3850 PERROR("pthread_join listener_thread");
3854 exit_listener_thread
:
3855 ret
= pthread_join(worker_thread
, &status
);
3858 PERROR("pthread_join worker_thread");
3863 ret
= pthread_join(dispatcher_thread
, &status
);
3866 PERROR("pthread_join dispatcher_thread");
3869 exit_dispatcher_thread
:
3871 ret
= pthread_join(health_thread
, &status
);
3874 PERROR("pthread_join health_thread");
3879 utils_close_pipe(health_quit_pipe
);
3880 exit_health_quit_pipe
:
3883 health_app_destroy(health_relayd
);
3884 sessiond_trace_chunk_registry_destroy(sessiond_trace_chunk_registry
);
3885 exit_health_app_create
:
3886 exit_sessiond_trace_chunk_registry
:
3889 * Wait for all pending call_rcu work to complete before tearing
3890 * down data structures. call_rcu worker may be trying to
3891 * perform lookups in those structures.
3896 /* Ensure all prior call_rcu are done. */