2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #include <urcu/uatomic.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/compat/getenv.h>
49 #include <common/defaults.h>
50 #include <common/daemonize.h>
51 #include <common/futex.h>
52 #include <common/sessiond-comm/sessiond-comm.h>
53 #include <common/sessiond-comm/inet.h>
54 #include <common/sessiond-comm/relayd.h>
55 #include <common/uri.h>
56 #include <common/utils.h>
57 #include <common/align.h>
58 #include <common/config/session-config.h>
59 #include <common/dynamic-buffer.h>
60 #include <common/buffer-view.h>
61 #include <urcu/rculist.h>
64 #include "ctf-trace.h"
67 #include "lttng-relayd.h"
69 #include "health-relayd.h"
70 #include "testpoint.h"
71 #include "viewer-stream.h"
74 #include "connection.h"
75 #include "tracefile-array.h"
76 #include "tcp_keep_alive.h"
77 #include "sessiond-trace-chunks.h"
79 static const char *help_msg
=
80 #ifdef LTTNG_EMBED_HELP
81 #include <lttng-relayd.8.h>
87 enum relay_connection_status
{
88 RELAY_CONNECTION_STATUS_OK
,
89 /* An error occurred while processing an event on the connection. */
90 RELAY_CONNECTION_STATUS_ERROR
,
91 /* Connection closed/shutdown cleanly. */
92 RELAY_CONNECTION_STATUS_CLOSED
,
95 /* command line options */
96 char *opt_output_path
;
97 static int opt_daemon
, opt_background
;
100 * We need to wait for listener and live listener threads, as well as
101 * health check thread, before being ready to signal readiness.
103 #define NR_LTTNG_RELAY_READY 3
104 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
106 /* Size of receive buffer. */
107 #define RECV_DATA_BUFFER_SIZE 65536
109 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
110 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
112 static struct lttng_uri
*control_uri
;
113 static struct lttng_uri
*data_uri
;
114 static struct lttng_uri
*live_uri
;
116 const char *progname
;
118 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
119 static int tracing_group_name_override
;
121 const char * const config_section_name
= "relayd";
124 * Quit pipe for all threads. This permits a single cancellation point
125 * for all threads when receiving an event on the pipe.
127 int thread_quit_pipe
[2] = { -1, -1 };
130 * This pipe is used to inform the worker thread that a command is queued and
131 * ready to be processed.
133 static int relay_conn_pipe
[2] = { -1, -1 };
135 /* Shared between threads */
136 static int dispatch_thread_exit
;
138 static pthread_t listener_thread
;
139 static pthread_t dispatcher_thread
;
140 static pthread_t worker_thread
;
141 static pthread_t health_thread
;
144 * last_relay_stream_id_lock protects last_relay_stream_id increment
145 * atomicity on 32-bit architectures.
147 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
148 static uint64_t last_relay_stream_id
;
151 * Relay command queue.
153 * The relay_thread_listener and relay_thread_dispatcher communicate with this
156 static struct relay_conn_queue relay_conn_queue
;
158 /* Global relay stream hash table. */
159 struct lttng_ht
*relay_streams_ht
;
161 /* Global relay viewer stream hash table. */
162 struct lttng_ht
*viewer_streams_ht
;
164 /* Global relay sessions hash table. */
165 struct lttng_ht
*sessions_ht
;
167 /* Relayd health monitoring */
168 struct health_app
*health_relayd
;
170 struct sessiond_trace_chunk_registry
*sessiond_trace_chunk_registry
;
172 static struct option long_options
[] = {
173 { "control-port", 1, 0, 'C', },
174 { "data-port", 1, 0, 'D', },
175 { "live-port", 1, 0, 'L', },
176 { "daemonize", 0, 0, 'd', },
177 { "background", 0, 0, 'b', },
178 { "group", 1, 0, 'g', },
179 { "help", 0, 0, 'h', },
180 { "output", 1, 0, 'o', },
181 { "verbose", 0, 0, 'v', },
182 { "config", 1, 0, 'f' },
183 { "version", 0, 0, 'V' },
187 static const char *config_ignore_options
[] = { "help", "config", "version" };
190 * Take an option from the getopt output and set it in the right variable to be
193 * Return 0 on success else a negative value.
195 static int set_option(int opt
, const char *arg
, const char *optname
)
201 fprintf(stderr
, "option %s", optname
);
203 fprintf(stderr
, " with arg %s\n", arg
);
207 if (lttng_is_setuid_setgid()) {
208 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
209 "-C, --control-port");
211 ret
= uri_parse(arg
, &control_uri
);
213 ERR("Invalid control URI specified");
216 if (control_uri
->port
== 0) {
217 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
222 if (lttng_is_setuid_setgid()) {
223 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
226 ret
= uri_parse(arg
, &data_uri
);
228 ERR("Invalid data URI specified");
231 if (data_uri
->port
== 0) {
232 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
237 if (lttng_is_setuid_setgid()) {
238 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
241 ret
= uri_parse(arg
, &live_uri
);
243 ERR("Invalid live URI specified");
246 if (live_uri
->port
== 0) {
247 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
258 if (lttng_is_setuid_setgid()) {
259 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
262 tracing_group_name
= strdup(arg
);
263 if (tracing_group_name
== NULL
) {
268 tracing_group_name_override
= 1;
272 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
274 ERR("Cannot show --help for `lttng-relayd`");
279 fprintf(stdout
, "%s\n", VERSION
);
282 if (lttng_is_setuid_setgid()) {
283 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
286 ret
= asprintf(&opt_output_path
, "%s", arg
);
289 PERROR("asprintf opt_output_path");
295 /* Verbose level can increase using multiple -v */
297 lttng_opt_verbose
= config_parse_value(arg
);
299 /* Only 3 level of verbosity (-vvv). */
300 if (lttng_opt_verbose
< 3) {
301 lttng_opt_verbose
+= 1;
306 /* Unknown option or other error.
307 * Error is printed by getopt, just return */
320 * config_entry_handler_cb used to handle options read from a config file.
321 * See config_entry_handler_cb comment in common/config/session-config.h for the
322 * return value conventions.
324 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
328 if (!entry
|| !entry
->name
|| !entry
->value
) {
333 /* Check if the option is to be ignored */
334 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
335 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
340 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
341 /* Ignore if entry name is not fully matched. */
342 if (strcmp(entry
->name
, long_options
[i
].name
)) {
347 * If the option takes no argument on the command line,
348 * we have to check if the value is "true". We support
349 * non-zero numeric values, true, on and yes.
351 if (!long_options
[i
].has_arg
) {
352 ret
= config_parse_value(entry
->value
);
355 WARN("Invalid configuration value \"%s\" for option %s",
356 entry
->value
, entry
->name
);
358 /* False, skip boolean config option. */
363 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
367 WARN("Unrecognized option \"%s\" in daemon configuration file.",
374 static int set_options(int argc
, char **argv
)
376 int c
, ret
= 0, option_index
= 0, retval
= 0;
377 int orig_optopt
= optopt
, orig_optind
= optind
;
378 char *default_address
, *optstring
;
379 const char *config_path
= NULL
;
381 optstring
= utils_generate_optstring(long_options
,
382 sizeof(long_options
) / sizeof(struct option
));
388 /* Check for the --config option */
390 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
391 &option_index
)) != -1) {
395 } else if (c
!= 'f') {
399 if (lttng_is_setuid_setgid()) {
400 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
403 config_path
= utils_expand_path(optarg
);
405 ERR("Failed to resolve path: %s", optarg
);
410 ret
= config_get_section_entries(config_path
, config_section_name
,
411 config_entry_handler
, NULL
);
414 ERR("Invalid configuration option at line %i", ret
);
420 /* Reset getopt's global state */
421 optopt
= orig_optopt
;
422 optind
= orig_optind
;
424 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
429 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
436 /* assign default values */
437 if (control_uri
== NULL
) {
438 ret
= asprintf(&default_address
,
439 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
440 DEFAULT_NETWORK_CONTROL_PORT
);
442 PERROR("asprintf default data address");
447 ret
= uri_parse(default_address
, &control_uri
);
448 free(default_address
);
450 ERR("Invalid control URI specified");
455 if (data_uri
== NULL
) {
456 ret
= asprintf(&default_address
,
457 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
458 DEFAULT_NETWORK_DATA_PORT
);
460 PERROR("asprintf default data address");
465 ret
= uri_parse(default_address
, &data_uri
);
466 free(default_address
);
468 ERR("Invalid data URI specified");
473 if (live_uri
== NULL
) {
474 ret
= asprintf(&default_address
,
475 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
476 DEFAULT_NETWORK_VIEWER_PORT
);
478 PERROR("asprintf default viewer control address");
483 ret
= uri_parse(default_address
, &live_uri
);
484 free(default_address
);
486 ERR("Invalid viewer control URI specified");
497 static void print_global_objects(void)
499 rcu_register_thread();
501 print_viewer_streams();
502 print_relay_streams();
505 rcu_unregister_thread();
511 static void relayd_cleanup(void)
513 print_global_objects();
517 if (viewer_streams_ht
)
518 lttng_ht_destroy(viewer_streams_ht
);
519 if (relay_streams_ht
)
520 lttng_ht_destroy(relay_streams_ht
);
522 lttng_ht_destroy(sessions_ht
);
524 /* free the dynamically allocated opt_output_path */
525 free(opt_output_path
);
527 /* Close thread quit pipes */
528 utils_close_pipe(thread_quit_pipe
);
530 uri_free(control_uri
);
532 /* Live URI is freed in the live thread. */
534 if (tracing_group_name_override
) {
535 free((void *) tracing_group_name
);
540 * Write to writable pipe used to notify a thread.
542 static int notify_thread_pipe(int wpipe
)
546 ret
= lttng_write(wpipe
, "!", 1);
548 PERROR("write poll pipe");
556 static int notify_health_quit_pipe(int *pipe
)
560 ret
= lttng_write(pipe
[1], "4", 1);
562 PERROR("write relay health quit");
571 * Stop all relayd and relayd-live threads.
573 int lttng_relay_stop_threads(void)
577 /* Stopping all threads */
578 DBG("Terminating all threads");
579 if (notify_thread_pipe(thread_quit_pipe
[1])) {
580 ERR("write error on thread quit pipe");
584 if (notify_health_quit_pipe(health_quit_pipe
)) {
585 ERR("write error on health quit pipe");
588 /* Dispatch thread */
589 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
590 futex_nto1_wake(&relay_conn_queue
.futex
);
592 if (relayd_live_stop()) {
593 ERR("Error stopping live threads");
600 * Signal handler for the daemon
602 * Simply stop all worker threads, leaving main() return gracefully after
603 * joining all threads and calling cleanup().
605 static void sighandler(int sig
)
609 DBG("SIGINT caught");
610 if (lttng_relay_stop_threads()) {
611 ERR("Error stopping threads");
615 DBG("SIGTERM caught");
616 if (lttng_relay_stop_threads()) {
617 ERR("Error stopping threads");
621 CMM_STORE_SHARED(recv_child_signal
, 1);
629 * Setup signal handler for :
630 * SIGINT, SIGTERM, SIGPIPE
632 static int set_signal_handler(void)
638 if ((ret
= sigemptyset(&sigset
)) < 0) {
639 PERROR("sigemptyset");
646 sa
.sa_handler
= sighandler
;
647 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
652 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
657 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
662 sa
.sa_handler
= SIG_IGN
;
663 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
668 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
673 void lttng_relay_notify_ready(void)
675 /* Notify the parent of the fork() process that we are ready. */
676 if (opt_daemon
|| opt_background
) {
677 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
678 kill(child_ppid
, SIGUSR1
);
684 * Init thread quit pipe.
686 * Return -1 on error or 0 if all pipes are created.
688 static int init_thread_quit_pipe(void)
692 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
698 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
700 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
704 if (events
== NULL
|| size
== 0) {
709 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
715 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
727 * Check if the thread quit pipe was triggered.
729 * Return 1 if it was triggered else 0;
731 static int check_thread_quit_pipe(int fd
, uint32_t events
)
733 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
741 * Create and init socket from uri.
743 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
746 struct lttcomm_sock
*sock
= NULL
;
748 sock
= lttcomm_alloc_sock_from_uri(uri
);
750 ERR("Allocating socket");
754 ret
= lttcomm_create_sock(sock
);
758 DBG("Listening on sock %d", sock
->fd
);
760 ret
= sock
->ops
->bind(sock
);
762 PERROR("Failed to bind socket");
766 ret
= sock
->ops
->listen(sock
, -1);
776 lttcomm_destroy_sock(sock
);
782 * This thread manages the listening for new connections on the network
784 static void *relay_thread_listener(void *data
)
786 int i
, ret
, pollfd
, err
= -1;
787 uint32_t revents
, nb_fd
;
788 struct lttng_poll_event events
;
789 struct lttcomm_sock
*control_sock
, *data_sock
;
791 DBG("[thread] Relay listener started");
793 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
795 health_code_update();
797 control_sock
= relay_socket_create(control_uri
);
799 goto error_sock_control
;
802 data_sock
= relay_socket_create(data_uri
);
804 goto error_sock_relay
;
808 * Pass 3 as size here for the thread quit pipe, control and
811 ret
= create_thread_poll_set(&events
, 3);
813 goto error_create_poll
;
816 /* Add the control socket */
817 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
822 /* Add the data socket */
823 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
828 lttng_relay_notify_ready();
830 if (testpoint(relayd_thread_listener
)) {
831 goto error_testpoint
;
835 health_code_update();
837 DBG("Listener accepting connections");
841 ret
= lttng_poll_wait(&events
, -1);
845 * Restart interrupted system call.
847 if (errno
== EINTR
) {
855 DBG("Relay new connection received");
856 for (i
= 0; i
< nb_fd
; i
++) {
857 health_code_update();
859 /* Fetch once the poll data */
860 revents
= LTTNG_POLL_GETEV(&events
, i
);
861 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
863 /* Thread quit pipe has been closed. Killing thread. */
864 ret
= check_thread_quit_pipe(pollfd
, revents
);
870 if (revents
& LPOLLIN
) {
872 * A new connection is requested, therefore a
873 * sessiond/consumerd connection is allocated in
874 * this thread, enqueued to a global queue and
875 * dequeued (and freed) in the worker thread.
878 struct relay_connection
*new_conn
;
879 struct lttcomm_sock
*newsock
;
880 enum connection_type type
;
882 if (pollfd
== data_sock
->fd
) {
884 newsock
= data_sock
->ops
->accept(data_sock
);
885 DBG("Relay data connection accepted, socket %d",
888 assert(pollfd
== control_sock
->fd
);
889 type
= RELAY_CONTROL
;
890 newsock
= control_sock
->ops
->accept(control_sock
);
891 DBG("Relay control connection accepted, socket %d",
895 PERROR("accepting sock");
899 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
902 PERROR("setsockopt inet");
903 lttcomm_destroy_sock(newsock
);
907 ret
= socket_apply_keep_alive_config(newsock
->fd
);
909 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
911 lttcomm_destroy_sock(newsock
);
915 new_conn
= connection_create(newsock
, type
);
917 lttcomm_destroy_sock(newsock
);
921 /* Enqueue request for the dispatcher thread. */
922 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
926 * Wake the dispatch queue futex.
927 * Implicit memory barrier with the
928 * exchange in cds_wfcq_enqueue.
930 futex_nto1_wake(&relay_conn_queue
.futex
);
931 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
932 ERR("socket poll error");
935 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
945 lttng_poll_clean(&events
);
947 if (data_sock
->fd
>= 0) {
948 ret
= data_sock
->ops
->close(data_sock
);
953 lttcomm_destroy_sock(data_sock
);
955 if (control_sock
->fd
>= 0) {
956 ret
= control_sock
->ops
->close(control_sock
);
961 lttcomm_destroy_sock(control_sock
);
965 ERR("Health error occurred in %s", __func__
);
967 health_unregister(health_relayd
);
968 DBG("Relay listener thread cleanup complete");
969 lttng_relay_stop_threads();
974 * This thread manages the dispatching of the requests to worker threads
976 static void *relay_thread_dispatcher(void *data
)
980 struct cds_wfcq_node
*node
;
981 struct relay_connection
*new_conn
= NULL
;
983 DBG("[thread] Relay dispatcher started");
985 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
987 if (testpoint(relayd_thread_dispatcher
)) {
988 goto error_testpoint
;
991 health_code_update();
994 health_code_update();
996 /* Atomically prepare the queue futex */
997 futex_nto1_prepare(&relay_conn_queue
.futex
);
999 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1004 health_code_update();
1006 /* Dequeue commands */
1007 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1008 &relay_conn_queue
.tail
);
1010 DBG("Woken up but nothing in the relay command queue");
1011 /* Continue thread execution */
1014 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1016 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1019 * Inform worker thread of the new request. This
1020 * call is blocking so we can be assured that
1021 * the data will be read at some point in time
1022 * or wait to the end of the world :)
1024 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1026 PERROR("write connection pipe");
1027 connection_put(new_conn
);
1030 } while (node
!= NULL
);
1032 /* Futex wait on queue. Blocking call on futex() */
1033 health_poll_entry();
1034 futex_nto1_wait(&relay_conn_queue
.futex
);
1038 /* Normal exit, no error */
1045 ERR("Health error occurred in %s", __func__
);
1047 health_unregister(health_relayd
);
1048 DBG("Dispatch thread dying");
1049 lttng_relay_stop_threads();
1053 static bool session_streams_have_index(const struct relay_session
*session
)
1055 return session
->minor
>= 4 && !session
->snapshot
;
1059 * Handle the RELAYD_CREATE_SESSION command.
1061 * On success, send back the session id or else return a negative value.
1063 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1064 struct relay_connection
*conn
,
1065 const struct lttng_buffer_view
*payload
)
1069 struct relay_session
*session
= NULL
;
1070 struct lttcomm_relayd_status_session reply
= {};
1071 char session_name
[LTTNG_NAME_MAX
] = {};
1072 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
1073 uint32_t live_timer
= 0;
1074 bool snapshot
= false;
1075 /* Left nil for peers < 2.11. */
1076 lttng_uuid sessiond_uuid
= {};
1077 LTTNG_OPTIONAL(uint64_t) id_sessiond
= {};
1078 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1079 LTTNG_OPTIONAL(time_t) creation_time
= {};
1081 if (conn
->minor
< 4) {
1082 /* From 2.1 to 2.3 */
1084 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1085 /* From 2.4 to 2.10 */
1086 ret
= cmd_create_session_2_4(payload
, session_name
,
1087 hostname
, &live_timer
, &snapshot
);
1089 bool has_current_chunk
;
1090 uint64_t current_chunk_id_value
;
1091 time_t creation_time_value
;
1092 uint64_t id_sessiond_value
;
1094 /* From 2.11 to ... */
1095 ret
= cmd_create_session_2_11(payload
, session_name
, hostname
,
1096 &live_timer
, &snapshot
, &id_sessiond_value
,
1097 sessiond_uuid
, &has_current_chunk
,
1098 ¤t_chunk_id_value
, &creation_time_value
);
1099 if (lttng_uuid_is_nil(sessiond_uuid
)) {
1100 /* The nil UUID is reserved for pre-2.11 clients. */
1101 ERR("Illegal nil UUID announced by peer in create session command");
1105 LTTNG_OPTIONAL_SET(&id_sessiond
, id_sessiond_value
);
1106 LTTNG_OPTIONAL_SET(&creation_time
, creation_time_value
);
1107 if (has_current_chunk
) {
1108 LTTNG_OPTIONAL_SET(¤t_chunk_id
,
1109 current_chunk_id_value
);
1117 session
= session_create(session_name
, hostname
, live_timer
,
1118 snapshot
, sessiond_uuid
,
1119 id_sessiond
.is_set
? &id_sessiond
.value
: NULL
,
1120 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1121 creation_time
.is_set
? &creation_time
.value
: NULL
,
1122 conn
->major
, conn
->minor
);
1127 assert(!conn
->session
);
1128 conn
->session
= session
;
1129 DBG("Created session %" PRIu64
, session
->id
);
1131 reply
.session_id
= htobe64(session
->id
);
1135 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1137 reply
.ret_code
= htobe32(LTTNG_OK
);
1140 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1141 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1142 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1146 if (ret
< 0 && session
) {
1147 session_put(session
);
1153 * When we have received all the streams and the metadata for a channel,
1154 * we make them visible to the viewer threads.
1156 static void publish_connection_local_streams(struct relay_connection
*conn
)
1158 struct relay_stream
*stream
;
1159 struct relay_session
*session
= conn
->session
;
1162 * We publish all streams belonging to a session atomically wrt
1165 pthread_mutex_lock(&session
->lock
);
1167 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1169 stream_publish(stream
);
1174 * Inform the viewer that there are new streams in the session.
1176 if (session
->viewer_attached
) {
1177 uatomic_set(&session
->new_streams
, 1);
1179 pthread_mutex_unlock(&session
->lock
);
1182 static int conform_channel_path(char *channel_path
)
1186 if (strstr("../", channel_path
)) {
1187 ERR("Refusing channel path as it walks up the path hierarchy: \"%s\"",
1193 if (*channel_path
== '/') {
1194 const size_t len
= strlen(channel_path
);
1197 * Channel paths from peers prior to 2.11 are expressed as an
1198 * absolute path that is, in reality, relative to the relay
1199 * daemon's output directory. Remove the leading slash so it
1200 * is correctly interpreted as a relative path later on.
1202 * len (and not len - 1) is used to copy the trailing NULL.
1204 bcopy(channel_path
+ 1, channel_path
, len
);
1211 * relay_add_stream: allocate a new stream for a session
1213 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1214 struct relay_connection
*conn
,
1215 const struct lttng_buffer_view
*payload
)
1219 struct relay_session
*session
= conn
->session
;
1220 struct relay_stream
*stream
= NULL
;
1221 struct lttcomm_relayd_status_stream reply
;
1222 struct ctf_trace
*trace
= NULL
;
1223 uint64_t stream_handle
= -1ULL;
1224 char *path_name
= NULL
, *channel_name
= NULL
;
1225 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1226 LTTNG_OPTIONAL(uint64_t) stream_chunk_id
= {};
1228 if (!session
|| !conn
->version_check_done
) {
1229 ERR("Trying to add a stream before version check");
1231 goto end_no_session
;
1234 if (session
->minor
== 1) {
1236 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1238 } else if (session
->minor
> 1 && session
->minor
< 11) {
1239 /* From 2.2 to 2.10 */
1240 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1241 &channel_name
, &tracefile_size
, &tracefile_count
);
1243 /* From 2.11 to ... */
1244 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1245 &channel_name
, &tracefile_size
, &tracefile_count
,
1246 &stream_chunk_id
.value
);
1247 stream_chunk_id
.is_set
= true;
1254 if (conform_channel_path(path_name
)) {
1258 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1262 /* This stream here has one reference on the trace. */
1264 pthread_mutex_lock(&last_relay_stream_id_lock
);
1265 stream_handle
= ++last_relay_stream_id
;
1266 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1268 /* We pass ownership of path_name and channel_name. */
1269 stream
= stream_create(trace
, stream_handle
, path_name
,
1270 channel_name
, tracefile_size
, tracefile_count
);
1272 channel_name
= NULL
;
1275 * Streams are the owners of their trace. Reference to trace is
1276 * kept within stream_create().
1278 ctf_trace_put(trace
);
1281 memset(&reply
, 0, sizeof(reply
));
1282 reply
.handle
= htobe64(stream_handle
);
1284 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1286 reply
.ret_code
= htobe32(LTTNG_OK
);
1289 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1290 sizeof(struct lttcomm_relayd_status_stream
), 0);
1291 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1292 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1304 * relay_close_stream: close a specific stream
1306 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1307 struct relay_connection
*conn
,
1308 const struct lttng_buffer_view
*payload
)
1312 struct relay_session
*session
= conn
->session
;
1313 struct lttcomm_relayd_close_stream stream_info
;
1314 struct lttcomm_relayd_generic_reply reply
;
1315 struct relay_stream
*stream
;
1317 DBG("Close stream received");
1319 if (!session
|| !conn
->version_check_done
) {
1320 ERR("Trying to close a stream before version check");
1322 goto end_no_session
;
1325 if (payload
->size
< sizeof(stream_info
)) {
1326 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1327 sizeof(stream_info
), payload
->size
);
1329 goto end_no_session
;
1331 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1332 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1333 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1335 stream
= stream_get_by_id(stream_info
.stream_id
);
1342 * Set last_net_seq_num before the close flag. Required by data
1345 pthread_mutex_lock(&stream
->lock
);
1346 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1347 pthread_mutex_unlock(&stream
->lock
);
1350 * This is one of the conditions which may trigger a stream close
1351 * with the others being:
1352 * 1) A close command is received for a stream
1353 * 2) The control connection owning the stream is closed
1354 * 3) We have received all of the stream's data _after_ a close
1357 try_stream_close(stream
);
1358 if (stream
->is_metadata
) {
1359 struct relay_viewer_stream
*vstream
;
1361 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1363 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1365 * Since all the metadata has been sent to the
1366 * viewer and that we have a request to close
1367 * its stream, we can safely teardown the
1368 * corresponding metadata viewer stream.
1370 viewer_stream_put(vstream
);
1372 /* Put local reference. */
1373 viewer_stream_put(vstream
);
1380 memset(&reply
, 0, sizeof(reply
));
1382 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1384 reply
.ret_code
= htobe32(LTTNG_OK
);
1386 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1387 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1388 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1389 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1399 * relay_reset_metadata: reset a metadata stream
1402 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1403 struct relay_connection
*conn
,
1404 const struct lttng_buffer_view
*payload
)
1408 struct relay_session
*session
= conn
->session
;
1409 struct lttcomm_relayd_reset_metadata stream_info
;
1410 struct lttcomm_relayd_generic_reply reply
;
1411 struct relay_stream
*stream
;
1413 DBG("Reset metadata received");
1415 if (!session
|| !conn
->version_check_done
) {
1416 ERR("Trying to reset a metadata stream before version check");
1418 goto end_no_session
;
1421 if (payload
->size
< sizeof(stream_info
)) {
1422 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1423 sizeof(stream_info
), payload
->size
);
1425 goto end_no_session
;
1427 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1428 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1429 stream_info
.version
= be64toh(stream_info
.version
);
1431 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1433 /* Unsupported for live sessions for now. */
1434 if (session
->live_timer
!= 0) {
1439 stream
= stream_get_by_id(stream_info
.stream_id
);
1444 pthread_mutex_lock(&stream
->lock
);
1445 if (!stream
->is_metadata
) {
1450 ret
= stream_reset_file(stream
);
1452 ERR("Failed to reset metadata stream %" PRIu64
1453 ": stream_path = %s, channel = %s",
1454 stream
->stream_handle
, stream
->path_name
,
1455 stream
->channel_name
);
1459 pthread_mutex_unlock(&stream
->lock
);
1463 memset(&reply
, 0, sizeof(reply
));
1465 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1467 reply
.ret_code
= htobe32(LTTNG_OK
);
1469 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1470 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1471 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1472 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1482 * relay_unknown_command: send -1 if received unknown command
1484 static void relay_unknown_command(struct relay_connection
*conn
)
1486 struct lttcomm_relayd_generic_reply reply
;
1489 memset(&reply
, 0, sizeof(reply
));
1490 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1491 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1492 if (send_ret
< sizeof(reply
)) {
1493 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1498 * relay_start: send an acknowledgment to the client to tell if we are
1499 * ready to receive data. We are ready if a session is established.
1501 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1502 struct relay_connection
*conn
,
1503 const struct lttng_buffer_view
*payload
)
1507 struct lttcomm_relayd_generic_reply reply
;
1508 struct relay_session
*session
= conn
->session
;
1511 DBG("Trying to start the streaming without a session established");
1512 ret
= htobe32(LTTNG_ERR_UNK
);
1515 memset(&reply
, 0, sizeof(reply
));
1516 reply
.ret_code
= htobe32(LTTNG_OK
);
1517 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1519 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1520 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1529 * relay_recv_metadata: receive the metadata for the session.
1531 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1532 struct relay_connection
*conn
,
1533 const struct lttng_buffer_view
*payload
)
1536 struct relay_session
*session
= conn
->session
;
1537 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1538 struct relay_stream
*metadata_stream
;
1539 uint64_t metadata_payload_size
;
1540 struct lttng_buffer_view packet_view
;
1543 ERR("Metadata sent before version check");
1548 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1549 ERR("Incorrect data size");
1553 metadata_payload_size
= recv_hdr
->data_size
-
1554 sizeof(struct lttcomm_relayd_metadata_payload
);
1556 memcpy(&metadata_payload_header
, payload
->data
,
1557 sizeof(metadata_payload_header
));
1558 metadata_payload_header
.stream_id
= be64toh(
1559 metadata_payload_header
.stream_id
);
1560 metadata_payload_header
.padding_size
= be32toh(
1561 metadata_payload_header
.padding_size
);
1563 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1564 if (!metadata_stream
) {
1569 packet_view
= lttng_buffer_view_from_view(payload
,
1570 sizeof(metadata_payload_header
), metadata_payload_size
);
1571 if (!packet_view
.data
) {
1572 ERR("Invalid metadata packet length announced by header");
1577 pthread_mutex_lock(&metadata_stream
->lock
);
1578 ret
= stream_write(metadata_stream
, &packet_view
,
1579 metadata_payload_header
.padding_size
);
1580 pthread_mutex_unlock(&metadata_stream
->lock
);
1586 stream_put(metadata_stream
);
1592 * relay_send_version: send relayd version number
1594 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1595 struct relay_connection
*conn
,
1596 const struct lttng_buffer_view
*payload
)
1600 struct lttcomm_relayd_version reply
, msg
;
1601 bool compatible
= true;
1603 conn
->version_check_done
= true;
1605 /* Get version from the other side. */
1606 if (payload
->size
< sizeof(msg
)) {
1607 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1608 sizeof(msg
), payload
->size
);
1613 memcpy(&msg
, payload
->data
, sizeof(msg
));
1614 msg
.major
= be32toh(msg
.major
);
1615 msg
.minor
= be32toh(msg
.minor
);
1617 memset(&reply
, 0, sizeof(reply
));
1618 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1619 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1621 /* Major versions must be the same */
1622 if (reply
.major
!= msg
.major
) {
1623 DBG("Incompatible major versions (%u vs %u), deleting session",
1624 reply
.major
, msg
.major
);
1628 conn
->major
= reply
.major
;
1629 /* We adapt to the lowest compatible version */
1630 if (reply
.minor
<= msg
.minor
) {
1631 conn
->minor
= reply
.minor
;
1633 conn
->minor
= msg
.minor
;
1636 reply
.major
= htobe32(reply
.major
);
1637 reply
.minor
= htobe32(reply
.minor
);
1638 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1640 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1641 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1654 DBG("Version check done using protocol %u.%u", conn
->major
,
1662 * Check for data pending for a given stream id from the session daemon.
1664 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1665 struct relay_connection
*conn
,
1666 const struct lttng_buffer_view
*payload
)
1668 struct relay_session
*session
= conn
->session
;
1669 struct lttcomm_relayd_data_pending msg
;
1670 struct lttcomm_relayd_generic_reply reply
;
1671 struct relay_stream
*stream
;
1674 uint64_t stream_seq
;
1676 DBG("Data pending command received");
1678 if (!session
|| !conn
->version_check_done
) {
1679 ERR("Trying to check for data before version check");
1681 goto end_no_session
;
1684 if (payload
->size
< sizeof(msg
)) {
1685 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1686 sizeof(msg
), payload
->size
);
1688 goto end_no_session
;
1690 memcpy(&msg
, payload
->data
, sizeof(msg
));
1691 msg
.stream_id
= be64toh(msg
.stream_id
);
1692 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1694 stream
= stream_get_by_id(msg
.stream_id
);
1695 if (stream
== NULL
) {
1700 pthread_mutex_lock(&stream
->lock
);
1702 if (session_streams_have_index(session
)) {
1704 * Ensure that both the index and stream data have been
1705 * flushed up to the requested point.
1707 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1709 stream_seq
= stream
->prev_data_seq
;
1711 DBG("Data pending for stream id %" PRIu64
": prev_data_seq %" PRIu64
1712 ", prev_index_seq %" PRIu64
1713 ", and last_seq %" PRIu64
, msg
.stream_id
,
1714 stream
->prev_data_seq
, stream
->prev_index_seq
,
1715 msg
.last_net_seq_num
);
1717 /* Avoid wrapping issue */
1718 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
1719 /* Data has in fact been written and is NOT pending */
1722 /* Data still being streamed thus pending */
1726 stream
->data_pending_check_done
= true;
1727 pthread_mutex_unlock(&stream
->lock
);
1732 memset(&reply
, 0, sizeof(reply
));
1733 reply
.ret_code
= htobe32(ret
);
1734 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1735 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1736 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1746 * Wait for the control socket to reach a quiescent state.
1748 * Note that for now, when receiving this command from the session
1749 * daemon, this means that every subsequent commands or data received on
1750 * the control socket has been handled. So, this is why we simply return
1753 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
1754 struct relay_connection
*conn
,
1755 const struct lttng_buffer_view
*payload
)
1759 struct relay_stream
*stream
;
1760 struct lttcomm_relayd_quiescent_control msg
;
1761 struct lttcomm_relayd_generic_reply reply
;
1763 DBG("Checking quiescent state on control socket");
1765 if (!conn
->session
|| !conn
->version_check_done
) {
1766 ERR("Trying to check for data before version check");
1768 goto end_no_session
;
1771 if (payload
->size
< sizeof(msg
)) {
1772 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
1773 sizeof(msg
), payload
->size
);
1775 goto end_no_session
;
1777 memcpy(&msg
, payload
->data
, sizeof(msg
));
1778 msg
.stream_id
= be64toh(msg
.stream_id
);
1780 stream
= stream_get_by_id(msg
.stream_id
);
1784 pthread_mutex_lock(&stream
->lock
);
1785 stream
->data_pending_check_done
= true;
1786 pthread_mutex_unlock(&stream
->lock
);
1788 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
1791 memset(&reply
, 0, sizeof(reply
));
1792 reply
.ret_code
= htobe32(LTTNG_OK
);
1793 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1794 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1795 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
1807 * Initialize a data pending command. This means that a consumer is about
1808 * to ask for data pending for each stream it holds. Simply iterate over
1809 * all streams of a session and set the data_pending_check_done flag.
1811 * This command returns to the client a LTTNG_OK code.
1813 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1814 struct relay_connection
*conn
,
1815 const struct lttng_buffer_view
*payload
)
1819 struct lttng_ht_iter iter
;
1820 struct lttcomm_relayd_begin_data_pending msg
;
1821 struct lttcomm_relayd_generic_reply reply
;
1822 struct relay_stream
*stream
;
1827 DBG("Init streams for data pending");
1829 if (!conn
->session
|| !conn
->version_check_done
) {
1830 ERR("Trying to check for data before version check");
1832 goto end_no_session
;
1835 if (payload
->size
< sizeof(msg
)) {
1836 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
1837 sizeof(msg
), payload
->size
);
1839 goto end_no_session
;
1841 memcpy(&msg
, payload
->data
, sizeof(msg
));
1842 msg
.session_id
= be64toh(msg
.session_id
);
1845 * Iterate over all streams to set the begin data pending flag.
1846 * For now, the streams are indexed by stream handle so we have
1847 * to iterate over all streams to find the one associated with
1848 * the right session_id.
1851 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1853 if (!stream_get(stream
)) {
1856 if (stream
->trace
->session
->id
== msg
.session_id
) {
1857 pthread_mutex_lock(&stream
->lock
);
1858 stream
->data_pending_check_done
= false;
1859 pthread_mutex_unlock(&stream
->lock
);
1860 DBG("Set begin data pending flag to stream %" PRIu64
,
1861 stream
->stream_handle
);
1867 memset(&reply
, 0, sizeof(reply
));
1868 /* All good, send back reply. */
1869 reply
.ret_code
= htobe32(LTTNG_OK
);
1871 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1872 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1873 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
1885 * End data pending command. This will check, for a given session id, if
1886 * each stream associated with it has its data_pending_check_done flag
1887 * set. If not, this means that the client lost track of the stream but
1888 * the data is still being streamed on our side. In this case, we inform
1889 * the client that data is in flight.
1891 * Return to the client if there is data in flight or not with a ret_code.
1893 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1894 struct relay_connection
*conn
,
1895 const struct lttng_buffer_view
*payload
)
1899 struct lttng_ht_iter iter
;
1900 struct lttcomm_relayd_end_data_pending msg
;
1901 struct lttcomm_relayd_generic_reply reply
;
1902 struct relay_stream
*stream
;
1903 uint32_t is_data_inflight
= 0;
1905 DBG("End data pending command");
1907 if (!conn
->session
|| !conn
->version_check_done
) {
1908 ERR("Trying to check for data before version check");
1910 goto end_no_session
;
1913 if (payload
->size
< sizeof(msg
)) {
1914 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
1915 sizeof(msg
), payload
->size
);
1917 goto end_no_session
;
1919 memcpy(&msg
, payload
->data
, sizeof(msg
));
1920 msg
.session_id
= be64toh(msg
.session_id
);
1923 * Iterate over all streams to see if the begin data pending
1927 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1929 if (!stream_get(stream
)) {
1932 if (stream
->trace
->session
->id
!= msg
.session_id
) {
1936 pthread_mutex_lock(&stream
->lock
);
1937 if (!stream
->data_pending_check_done
) {
1938 uint64_t stream_seq
;
1940 if (session_streams_have_index(conn
->session
)) {
1942 * Ensure that both the index and stream data have been
1943 * flushed up to the requested point.
1945 stream_seq
= min(stream
->prev_data_seq
, stream
->prev_index_seq
);
1947 stream_seq
= stream
->prev_data_seq
;
1949 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
1950 is_data_inflight
= 1;
1951 DBG("Data is still in flight for stream %" PRIu64
,
1952 stream
->stream_handle
);
1953 pthread_mutex_unlock(&stream
->lock
);
1958 pthread_mutex_unlock(&stream
->lock
);
1963 memset(&reply
, 0, sizeof(reply
));
1964 /* All good, send back reply. */
1965 reply
.ret_code
= htobe32(is_data_inflight
);
1967 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1968 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1969 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
1981 * Receive an index for a specific stream.
1983 * Return 0 on success else a negative value.
1985 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
1986 struct relay_connection
*conn
,
1987 const struct lttng_buffer_view
*payload
)
1991 struct relay_session
*session
= conn
->session
;
1992 struct lttcomm_relayd_index index_info
;
1993 struct lttcomm_relayd_generic_reply reply
;
1994 struct relay_stream
*stream
;
1999 DBG("Relay receiving index");
2001 if (!session
|| !conn
->version_check_done
) {
2002 ERR("Trying to close a stream before version check");
2004 goto end_no_session
;
2007 msg_len
= lttcomm_relayd_index_len(
2008 lttng_to_index_major(conn
->major
, conn
->minor
),
2009 lttng_to_index_minor(conn
->major
, conn
->minor
));
2010 if (payload
->size
< msg_len
) {
2011 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2012 msg_len
, payload
->size
);
2014 goto end_no_session
;
2016 memcpy(&index_info
, payload
->data
, msg_len
);
2017 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2018 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2019 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2020 index_info
.content_size
= be64toh(index_info
.content_size
);
2021 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2022 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2023 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2024 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2026 if (conn
->minor
>= 8) {
2027 index_info
.stream_instance_id
=
2028 be64toh(index_info
.stream_instance_id
);
2029 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2032 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2034 ERR("stream_get_by_id not found");
2039 pthread_mutex_lock(&stream
->lock
);
2040 ret
= stream_add_index(stream
, &index_info
);
2041 pthread_mutex_unlock(&stream
->lock
);
2043 goto end_stream_put
;
2049 memset(&reply
, 0, sizeof(reply
));
2051 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2053 reply
.ret_code
= htobe32(LTTNG_OK
);
2055 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2056 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2057 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2066 * Receive the streams_sent message.
2068 * Return 0 on success else a negative value.
2070 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2071 struct relay_connection
*conn
,
2072 const struct lttng_buffer_view
*payload
)
2076 struct lttcomm_relayd_generic_reply reply
;
2080 DBG("Relay receiving streams_sent");
2082 if (!conn
->session
|| !conn
->version_check_done
) {
2083 ERR("Trying to close a stream before version check");
2085 goto end_no_session
;
2089 * Publish every pending stream in the connection recv list which are
2090 * now ready to be used by the viewer.
2092 publish_connection_local_streams(conn
);
2094 memset(&reply
, 0, sizeof(reply
));
2095 reply
.ret_code
= htobe32(LTTNG_OK
);
2096 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2097 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2098 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2111 * relay_rotate_session_stream: rotate a stream to a new tracefile for the
2112 * session rotation feature (not the tracefile rotation feature).
2114 static int relay_rotate_session_streams(
2115 const struct lttcomm_relayd_hdr
*recv_hdr
,
2116 struct relay_connection
*conn
,
2117 const struct lttng_buffer_view
*payload
)
2122 enum lttng_error_code reply_code
= LTTNG_ERR_UNK
;
2123 struct relay_session
*session
= conn
->session
;
2124 struct lttcomm_relayd_rotate_streams rotate_streams
;
2125 struct lttcomm_relayd_generic_reply reply
= {};
2126 struct relay_stream
*stream
= NULL
;
2127 const size_t header_len
= sizeof(struct lttcomm_relayd_rotate_streams
);
2128 struct lttng_trace_chunk
*next_trace_chunk
= NULL
;
2129 struct lttng_buffer_view stream_positions
;
2131 if (!session
|| !conn
->version_check_done
) {
2132 ERR("Trying to rotate a stream before version check");
2137 if (session
->major
== 2 && session
->minor
< 11) {
2138 ERR("Unsupported feature before 2.11");
2143 if (payload
->size
< header_len
) {
2144 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2145 header_len
, payload
->size
);
2150 memcpy(&rotate_streams
, payload
->data
, header_len
);
2152 /* Convert header to host endianness. */
2153 rotate_streams
= (typeof(rotate_streams
)) {
2154 .stream_count
= be32toh(rotate_streams
.stream_count
),
2155 .new_chunk_id
= (typeof(rotate_streams
.new_chunk_id
)) {
2156 .is_set
= !!rotate_streams
.new_chunk_id
.is_set
,
2157 .value
= be64toh(rotate_streams
.new_chunk_id
.value
),
2161 if (rotate_streams
.new_chunk_id
.is_set
) {
2163 * Retrieve the trace chunk the stream must transition to. As
2164 * per the protocol, this chunk should have been created
2165 * before this command is received.
2167 next_trace_chunk
= sessiond_trace_chunk_registry_get_chunk(
2168 sessiond_trace_chunk_registry
,
2169 session
->sessiond_uuid
, session
->id
,
2170 rotate_streams
.new_chunk_id
.value
);
2171 if (!next_trace_chunk
) {
2172 char uuid_str
[UUID_STR_LEN
];
2174 lttng_uuid_to_str(session
->sessiond_uuid
, uuid_str
);
2175 ERR("Unknown next trace chunk in ROTATE_STREAMS command: sessiond_uuid = {%s}, session_id = %" PRIu64
2176 ", trace_chunk_id = %" PRIu64
,
2177 uuid_str
, session
->id
,
2178 rotate_streams
.new_chunk_id
.value
);
2179 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2185 stream_positions
= lttng_buffer_view_from_view(payload
,
2186 sizeof(rotate_streams
), -1);
2187 if (!stream_positions
.data
||
2188 stream_positions
.size
<
2189 (rotate_streams
.stream_count
*
2190 sizeof(struct lttcomm_relayd_stream_rotation_position
))) {
2191 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2196 for (i
= 0; i
< rotate_streams
.stream_count
; i
++) {
2197 struct lttcomm_relayd_stream_rotation_position
*position_comm
=
2198 &((typeof(position_comm
)) stream_positions
.data
)[i
];
2199 const struct lttcomm_relayd_stream_rotation_position pos
= {
2200 .stream_id
= be64toh(position_comm
->stream_id
),
2201 .rotate_at_seq_num
= be64toh(
2202 position_comm
->rotate_at_seq_num
),
2205 stream
= stream_get_by_id(pos
.stream_id
);
2207 reply_code
= LTTNG_ERR_INVALID
;
2212 pthread_mutex_lock(&stream
->lock
);
2213 ret
= stream_set_pending_rotation(stream
, next_trace_chunk
,
2214 pos
.rotate_at_seq_num
);
2215 pthread_mutex_unlock(&stream
->lock
);
2217 reply_code
= LTTNG_ERR_FILE_CREATION_ERROR
;
2225 reply_code
= LTTNG_OK
;
2231 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2232 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2233 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2234 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2235 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2241 lttng_trace_chunk_put(next_trace_chunk
);
2245 static int init_session_output_directory_handle(struct relay_session
*session
,
2246 struct lttng_directory_handle
*handle
)
2249 /* hostname/session_name */
2250 char *session_directory
= NULL
;
2252 * base path + session_directory
2253 * e.g. /home/user/lttng-traces/hostname/session_name
2255 char *full_session_path
= NULL
;
2256 char creation_time_str
[16];
2257 struct tm
*timeinfo
;
2259 assert(session
->creation_time
.is_set
);
2260 timeinfo
= localtime(&session
->creation_time
.value
);
2265 strftime(creation_time_str
, sizeof(creation_time_str
), "%Y%m%d-%H%M%S",
2268 pthread_mutex_lock(&session
->lock
);
2269 ret
= asprintf(&session_directory
, "%s/%s-%s", session
->hostname
,
2270 session
->session_name
, creation_time_str
);
2271 pthread_mutex_unlock(&session
->lock
);
2273 PERROR("Failed to format session directory name");
2277 full_session_path
= create_output_path(session_directory
);
2278 if (!full_session_path
) {
2283 ret
= utils_mkdir_recursive(
2284 full_session_path
, S_IRWXU
| S_IRWXG
, -1, -1);
2286 ERR("Failed to create session output path \"%s\"",
2291 ret
= lttng_directory_handle_init(handle
, full_session_path
);
2296 free(session_directory
);
2297 free(full_session_path
);
2302 * relay_create_trace_chunk: create a new trace chunk
2304 static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2305 struct relay_connection
*conn
,
2306 const struct lttng_buffer_view
*payload
)
2310 struct relay_session
*session
= conn
->session
;
2311 struct lttcomm_relayd_create_trace_chunk
*msg
;
2312 struct lttcomm_relayd_generic_reply reply
= {};
2313 struct lttng_buffer_view header_view
;
2314 struct lttng_buffer_view chunk_name_view
;
2315 struct lttng_trace_chunk
*chunk
= NULL
, *published_chunk
= NULL
;
2316 enum lttng_error_code reply_code
= LTTNG_OK
;
2317 enum lttng_trace_chunk_status chunk_status
;
2318 struct lttng_directory_handle session_output
;
2320 if (!session
|| !conn
->version_check_done
) {
2321 ERR("Trying to create a trace chunk before version check");
2326 if (session
->major
== 2 && session
->minor
< 11) {
2327 ERR("Chunk creation command is unsupported before 2.11");
2332 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2333 if (!header_view
.data
) {
2334 ERR("Failed to receive payload of chunk creation command");
2339 /* Convert to host endianness. */
2340 msg
= (typeof(msg
)) header_view
.data
;
2341 msg
->chunk_id
= be64toh(msg
->chunk_id
);
2342 msg
->creation_timestamp
= be64toh(msg
->creation_timestamp
);
2343 msg
->override_name_length
= be32toh(msg
->override_name_length
);
2345 chunk
= lttng_trace_chunk_create(
2346 msg
->chunk_id
, msg
->creation_timestamp
);
2348 ERR("Failed to create trace chunk in trace chunk creation command");
2350 reply_code
= LTTNG_ERR_NOMEM
;
2354 if (msg
->override_name_length
) {
2357 chunk_name_view
= lttng_buffer_view_from_view(payload
,
2359 msg
->override_name_length
);
2360 name
= chunk_name_view
.data
;
2361 if (!name
|| name
[msg
->override_name_length
- 1]) {
2362 ERR("Failed to receive payload of chunk creation command");
2364 reply_code
= LTTNG_ERR_INVALID
;
2368 chunk_status
= lttng_trace_chunk_override_name(
2369 chunk
, chunk_name_view
.data
);
2370 switch (chunk_status
) {
2371 case LTTNG_TRACE_CHUNK_STATUS_OK
:
2373 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
2374 ERR("Failed to set the name of new trace chunk in trace chunk creation command (invalid name)");
2375 reply_code
= LTTNG_ERR_INVALID
;
2379 ERR("Failed to set the name of new trace chunk in trace chunk creation command (unknown error)");
2380 reply_code
= LTTNG_ERR_UNK
;
2386 ret
= init_session_output_directory_handle(
2387 conn
->session
, &session_output
);
2389 reply_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
2393 chunk_status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
2394 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2395 reply_code
= LTTNG_ERR_UNK
;
2400 chunk_status
= lttng_trace_chunk_set_as_owner(chunk
, &session_output
);
2401 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2402 reply_code
= LTTNG_ERR_UNK
;
2407 published_chunk
= sessiond_trace_chunk_registry_publish_chunk(
2408 sessiond_trace_chunk_registry
,
2409 conn
->session
->sessiond_uuid
,
2412 if (!published_chunk
) {
2413 char uuid_str
[UUID_STR_LEN
];
2415 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2416 ERR("Failed to publish chunk: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2421 reply_code
= LTTNG_ERR_NOMEM
;
2425 pthread_mutex_lock(&conn
->session
->lock
);
2426 if (conn
->session
->pending_closure_trace_chunk
) {
2428 * Invalid; this means a second create_trace_chunk command was
2429 * received before a close_trace_chunk.
2431 ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
2432 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2434 goto end_unlock_session
;
2436 conn
->session
->pending_closure_trace_chunk
=
2437 conn
->session
->current_trace_chunk
;
2438 conn
->session
->current_trace_chunk
= published_chunk
;
2439 published_chunk
= NULL
;
2441 pthread_mutex_unlock(&conn
->session
->lock
);
2443 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2444 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2446 sizeof(struct lttcomm_relayd_generic_reply
),
2448 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2449 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2454 lttng_trace_chunk_put(chunk
);
2455 lttng_trace_chunk_put(published_chunk
);
2456 lttng_directory_handle_fini(&session_output
);
2461 * relay_close_trace_chunk: close a trace chunk
2463 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2464 struct relay_connection
*conn
,
2465 const struct lttng_buffer_view
*payload
)
2469 struct relay_session
*session
= conn
->session
;
2470 struct lttcomm_relayd_close_trace_chunk
*msg
;
2471 struct lttcomm_relayd_generic_reply reply
= {};
2472 struct lttng_buffer_view header_view
;
2473 struct lttng_trace_chunk
*chunk
= NULL
;
2474 enum lttng_error_code reply_code
= LTTNG_OK
;
2475 enum lttng_trace_chunk_status chunk_status
;
2477 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2478 time_t close_timestamp
;
2480 if (!session
|| !conn
->version_check_done
) {
2481 ERR("Trying to close a trace chunk before version check");
2486 if (session
->major
== 2 && session
->minor
< 11) {
2487 ERR("Chunk close command is unsupported before 2.11");
2492 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2493 if (!header_view
.data
) {
2494 ERR("Failed to receive payload of chunk close command");
2499 /* Convert to host endianness. */
2500 msg
= (typeof(msg
)) header_view
.data
;
2501 chunk_id
= be64toh(msg
->chunk_id
);
2502 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2503 close_command
= (typeof(close_command
)){
2504 .value
= be32toh(msg
->close_command
.value
),
2505 .is_set
= msg
->close_command
.is_set
,
2508 chunk
= sessiond_trace_chunk_registry_get_chunk(
2509 sessiond_trace_chunk_registry
,
2510 conn
->session
->sessiond_uuid
,
2514 char uuid_str
[UUID_STR_LEN
];
2516 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2517 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2522 reply_code
= LTTNG_ERR_NOMEM
;
2526 pthread_mutex_lock(&session
->lock
);
2527 if (session
->pending_closure_trace_chunk
&&
2528 session
->pending_closure_trace_chunk
!= chunk
) {
2529 ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
2530 session
->session_name
);
2531 reply_code
= LTTNG_ERR_INVALID_PROTOCOL
;
2533 goto end_unlock_session
;
2536 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2537 chunk
, close_timestamp
);
2538 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2539 ERR("Failed to set trace chunk close timestamp");
2541 reply_code
= LTTNG_ERR_UNK
;
2542 goto end_unlock_session
;
2545 if (close_command
.is_set
) {
2546 chunk_status
= lttng_trace_chunk_set_close_command(
2547 chunk
, close_command
.value
);
2548 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2550 reply_code
= LTTNG_ERR_INVALID
;
2551 goto end_unlock_session
;
2555 if (session
->current_trace_chunk
== chunk
) {
2557 * After a trace chunk close command, no new streams
2558 * referencing the chunk may be created. Hence, on the
2559 * event that no new trace chunk have been created for
2560 * the session, the reference to the current trace chunk
2561 * is released in order to allow it to be reclaimed when
2562 * the last stream releases its reference to it.
2564 lttng_trace_chunk_put(session
->current_trace_chunk
);
2565 session
->current_trace_chunk
= NULL
;
2567 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
2568 session
->pending_closure_trace_chunk
= NULL
;
2570 pthread_mutex_unlock(&session
->lock
);
2573 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2574 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2576 sizeof(struct lttcomm_relayd_generic_reply
),
2578 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2579 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2584 lttng_trace_chunk_put(chunk
);
2589 * relay_trace_chunk_exists: check if a trace chunk exists
2591 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2592 struct relay_connection
*conn
,
2593 const struct lttng_buffer_view
*payload
)
2597 struct relay_session
*session
= conn
->session
;
2598 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2599 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2600 struct lttng_buffer_view header_view
;
2601 struct lttng_trace_chunk
*chunk
= NULL
;
2604 if (!session
|| !conn
->version_check_done
) {
2605 ERR("Trying to close a trace chunk before version check");
2610 if (session
->major
== 2 && session
->minor
< 11) {
2611 ERR("Chunk close command is unsupported before 2.11");
2616 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2617 if (!header_view
.data
) {
2618 ERR("Failed to receive payload of chunk close command");
2623 /* Convert to host endianness. */
2624 msg
= (typeof(msg
)) header_view
.data
;
2625 chunk_id
= be64toh(msg
->chunk_id
);
2627 chunk
= sessiond_trace_chunk_registry_get_chunk(
2628 sessiond_trace_chunk_registry
,
2629 conn
->session
->sessiond_uuid
,
2633 reply
= (typeof(reply
)) {
2634 .generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
),
2635 .trace_chunk_exists
= !!chunk
,
2637 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2638 &reply
, sizeof(reply
), 0);
2639 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2640 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2645 lttng_trace_chunk_put(chunk
);
2649 #define DBG_CMD(cmd_name, conn) \
2650 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2652 static int relay_process_control_command(struct relay_connection
*conn
,
2653 const struct lttcomm_relayd_hdr
*header
,
2654 const struct lttng_buffer_view
*payload
)
2658 switch (header
->cmd
) {
2659 case RELAYD_CREATE_SESSION
:
2660 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2661 ret
= relay_create_session(header
, conn
, payload
);
2663 case RELAYD_ADD_STREAM
:
2664 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2665 ret
= relay_add_stream(header
, conn
, payload
);
2667 case RELAYD_START_DATA
:
2668 DBG_CMD("RELAYD_START_DATA", conn
);
2669 ret
= relay_start(header
, conn
, payload
);
2671 case RELAYD_SEND_METADATA
:
2672 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2673 ret
= relay_recv_metadata(header
, conn
, payload
);
2675 case RELAYD_VERSION
:
2676 DBG_CMD("RELAYD_VERSION", conn
);
2677 ret
= relay_send_version(header
, conn
, payload
);
2679 case RELAYD_CLOSE_STREAM
:
2680 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2681 ret
= relay_close_stream(header
, conn
, payload
);
2683 case RELAYD_DATA_PENDING
:
2684 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2685 ret
= relay_data_pending(header
, conn
, payload
);
2687 case RELAYD_QUIESCENT_CONTROL
:
2688 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2689 ret
= relay_quiescent_control(header
, conn
, payload
);
2691 case RELAYD_BEGIN_DATA_PENDING
:
2692 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2693 ret
= relay_begin_data_pending(header
, conn
, payload
);
2695 case RELAYD_END_DATA_PENDING
:
2696 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2697 ret
= relay_end_data_pending(header
, conn
, payload
);
2699 case RELAYD_SEND_INDEX
:
2700 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2701 ret
= relay_recv_index(header
, conn
, payload
);
2703 case RELAYD_STREAMS_SENT
:
2704 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2705 ret
= relay_streams_sent(header
, conn
, payload
);
2707 case RELAYD_RESET_METADATA
:
2708 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2709 ret
= relay_reset_metadata(header
, conn
, payload
);
2711 case RELAYD_ROTATE_STREAMS
:
2712 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
2713 ret
= relay_rotate_session_streams(header
, conn
, payload
);
2715 case RELAYD_CREATE_TRACE_CHUNK
:
2716 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
2717 ret
= relay_create_trace_chunk(header
, conn
, payload
);
2719 case RELAYD_CLOSE_TRACE_CHUNK
:
2720 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
2721 ret
= relay_close_trace_chunk(header
, conn
, payload
);
2723 case RELAYD_TRACE_CHUNK_EXISTS
:
2724 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
2725 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
2727 case RELAYD_UPDATE_SYNC_INFO
:
2729 ERR("Received unknown command (%u)", header
->cmd
);
2730 relay_unknown_command(conn
);
2739 static enum relay_connection_status
relay_process_control_receive_payload(
2740 struct relay_connection
*conn
)
2743 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2744 struct lttng_dynamic_buffer
*reception_buffer
=
2745 &conn
->protocol
.ctrl
.reception_buffer
;
2746 struct ctrl_connection_state_receive_payload
*state
=
2747 &conn
->protocol
.ctrl
.state
.receive_payload
;
2748 struct lttng_buffer_view payload_view
;
2750 if (state
->left_to_receive
== 0) {
2751 /* Short-circuit for payload-less commands. */
2752 goto reception_complete
;
2755 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2756 reception_buffer
->data
+ state
->received
,
2757 state
->left_to_receive
, MSG_DONTWAIT
);
2759 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2760 PERROR("Unable to receive command payload on sock %d",
2762 status
= RELAY_CONNECTION_STATUS_ERROR
;
2765 } else if (ret
== 0) {
2766 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2767 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2772 assert(ret
<= state
->left_to_receive
);
2774 state
->left_to_receive
-= ret
;
2775 state
->received
+= ret
;
2777 if (state
->left_to_receive
> 0) {
2779 * Can't transition to the protocol's next state, wait to
2780 * receive the rest of the header.
2782 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2783 state
->received
, state
->left_to_receive
,
2789 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
2790 conn
->sock
->fd
, state
->received
);
2792 * The payload required to process the command has been received.
2793 * A view to the reception buffer is forwarded to the various
2794 * commands and the state of the control is reset on success.
2796 * Commands are responsible for sending their reply to the peer.
2798 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
2800 ret
= relay_process_control_command(conn
,
2801 &state
->header
, &payload_view
);
2803 status
= RELAY_CONNECTION_STATUS_ERROR
;
2807 ret
= connection_reset_protocol_state(conn
);
2809 status
= RELAY_CONNECTION_STATUS_ERROR
;
2815 static enum relay_connection_status
relay_process_control_receive_header(
2816 struct relay_connection
*conn
)
2819 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2820 struct lttcomm_relayd_hdr header
;
2821 struct lttng_dynamic_buffer
*reception_buffer
=
2822 &conn
->protocol
.ctrl
.reception_buffer
;
2823 struct ctrl_connection_state_receive_header
*state
=
2824 &conn
->protocol
.ctrl
.state
.receive_header
;
2826 assert(state
->left_to_receive
!= 0);
2828 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2829 reception_buffer
->data
+ state
->received
,
2830 state
->left_to_receive
, MSG_DONTWAIT
);
2832 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2833 PERROR("Unable to receive control command header on sock %d",
2835 status
= RELAY_CONNECTION_STATUS_ERROR
;
2838 } else if (ret
== 0) {
2839 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2840 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2845 assert(ret
<= state
->left_to_receive
);
2847 state
->left_to_receive
-= ret
;
2848 state
->received
+= ret
;
2850 if (state
->left_to_receive
> 0) {
2852 * Can't transition to the protocol's next state, wait to
2853 * receive the rest of the header.
2855 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2856 state
->received
, state
->left_to_receive
,
2861 /* Transition to next state: receiving the command's payload. */
2862 conn
->protocol
.ctrl
.state_id
=
2863 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
2864 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
2865 header
.circuit_id
= be64toh(header
.circuit_id
);
2866 header
.data_size
= be64toh(header
.data_size
);
2867 header
.cmd
= be32toh(header
.cmd
);
2868 header
.cmd_version
= be32toh(header
.cmd_version
);
2869 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
2870 &header
, sizeof(header
));
2872 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
2873 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
2876 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
2877 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
2879 status
= RELAY_CONNECTION_STATUS_ERROR
;
2883 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
2885 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
2886 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
2889 status
= RELAY_CONNECTION_STATUS_ERROR
;
2893 if (header
.data_size
== 0) {
2895 * Manually invoke the next state as the poll loop
2896 * will not wake-up to allow us to proceed further.
2898 status
= relay_process_control_receive_payload(conn
);
2905 * Process the commands received on the control socket
2907 static enum relay_connection_status
relay_process_control(
2908 struct relay_connection
*conn
)
2910 enum relay_connection_status status
;
2912 switch (conn
->protocol
.ctrl
.state_id
) {
2913 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
2914 status
= relay_process_control_receive_header(conn
);
2916 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
2917 status
= relay_process_control_receive_payload(conn
);
2920 ERR("Unknown control connection protocol state encountered.");
2927 static enum relay_connection_status
relay_process_data_receive_header(
2928 struct relay_connection
*conn
)
2931 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2932 struct data_connection_state_receive_header
*state
=
2933 &conn
->protocol
.data
.state
.receive_header
;
2934 struct lttcomm_relayd_data_hdr header
;
2935 struct relay_stream
*stream
;
2937 assert(state
->left_to_receive
!= 0);
2939 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2940 state
->header_reception_buffer
+ state
->received
,
2941 state
->left_to_receive
, MSG_DONTWAIT
);
2943 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2944 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2945 status
= RELAY_CONNECTION_STATUS_ERROR
;
2948 } else if (ret
== 0) {
2949 /* Orderly shutdown. Not necessary to print an error. */
2950 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2951 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2956 assert(ret
<= state
->left_to_receive
);
2958 state
->left_to_receive
-= ret
;
2959 state
->received
+= ret
;
2961 if (state
->left_to_receive
> 0) {
2963 * Can't transition to the protocol's next state, wait to
2964 * receive the rest of the header.
2966 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2967 state
->received
, state
->left_to_receive
,
2972 /* Transition to next state: receiving the payload. */
2973 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
2975 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
2976 header
.circuit_id
= be64toh(header
.circuit_id
);
2977 header
.stream_id
= be64toh(header
.stream_id
);
2978 header
.data_size
= be32toh(header
.data_size
);
2979 header
.net_seq_num
= be64toh(header
.net_seq_num
);
2980 header
.padding_size
= be32toh(header
.padding_size
);
2981 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
2983 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
2985 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
2986 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
2988 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
2989 conn
->sock
->fd
, header
.circuit_id
,
2990 header
.stream_id
, header
.data_size
,
2991 header
.net_seq_num
, header
.padding_size
);
2993 stream
= stream_get_by_id(header
.stream_id
);
2995 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
2997 /* Protocol error. */
2998 status
= RELAY_CONNECTION_STATUS_ERROR
;
3002 pthread_mutex_lock(&stream
->lock
);
3003 /* Prepare stream for the reception of a new packet. */
3004 ret
= stream_init_packet(stream
, header
.data_size
,
3005 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
3006 pthread_mutex_unlock(&stream
->lock
);
3008 ERR("Failed to rotate stream output file");
3009 status
= RELAY_CONNECTION_STATUS_ERROR
;
3010 goto end_stream_unlock
;
3019 static enum relay_connection_status
relay_process_data_receive_payload(
3020 struct relay_connection
*conn
)
3023 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3024 struct relay_stream
*stream
;
3025 struct data_connection_state_receive_payload
*state
=
3026 &conn
->protocol
.data
.state
.receive_payload
;
3027 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3028 char data_buffer
[chunk_size
];
3029 bool partial_recv
= false;
3030 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3031 uint64_t left_to_receive
= state
->left_to_receive
;
3032 struct relay_session
*session
;
3034 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3035 state
->header
.stream_id
, state
->header
.net_seq_num
,
3036 state
->received
, left_to_receive
);
3038 stream
= stream_get_by_id(state
->header
.stream_id
);
3040 /* Protocol error. */
3041 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3042 state
->header
.stream_id
);
3043 status
= RELAY_CONNECTION_STATUS_ERROR
;
3047 pthread_mutex_lock(&stream
->lock
);
3048 session
= stream
->trace
->session
;
3049 if (!conn
->session
) {
3050 ret
= connection_set_session(conn
, session
);
3052 status
= RELAY_CONNECTION_STATUS_ERROR
;
3053 goto end_stream_unlock
;
3058 * The size of the "chunk" received on any iteration is bounded by:
3059 * - the data left to receive,
3060 * - the data immediately available on the socket,
3061 * - the on-stack data buffer
3063 while (left_to_receive
> 0 && !partial_recv
) {
3064 size_t recv_size
= min(left_to_receive
, chunk_size
);
3065 struct lttng_buffer_view packet_chunk
;
3067 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3068 recv_size
, MSG_DONTWAIT
);
3070 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3071 PERROR("Socket %d error", conn
->sock
->fd
);
3072 status
= RELAY_CONNECTION_STATUS_ERROR
;
3074 goto end_stream_unlock
;
3075 } else if (ret
== 0) {
3076 /* No more data ready to be consumed on socket. */
3077 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3078 state
->header
.stream_id
);
3079 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3081 } else if (ret
< (int) recv_size
) {
3083 * All the data available on the socket has been
3086 partial_recv
= true;
3090 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3092 assert(packet_chunk
.data
);
3094 ret
= stream_write(stream
, &packet_chunk
, 0);
3096 ERR("Relay error writing data to file");
3097 status
= RELAY_CONNECTION_STATUS_ERROR
;
3098 goto end_stream_unlock
;
3101 left_to_receive
-= recv_size
;
3102 state
->received
+= recv_size
;
3103 state
->left_to_receive
= left_to_receive
;
3106 if (state
->left_to_receive
> 0) {
3108 * Did not receive all the data expected, wait for more data to
3109 * become available on the socket.
3111 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3112 state
->header
.stream_id
, state
->received
,
3113 state
->left_to_receive
);
3114 goto end_stream_unlock
;
3117 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3119 status
= RELAY_CONNECTION_STATUS_ERROR
;
3120 goto end_stream_unlock
;
3123 if (session_streams_have_index(session
)) {
3124 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3125 state
->rotate_index
, &index_flushed
,
3126 state
->header
.data_size
+ state
->header
.padding_size
);
3128 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3129 stream
->stream_handle
,
3130 state
->header
.net_seq_num
, ret
);
3131 status
= RELAY_CONNECTION_STATUS_ERROR
;
3132 goto end_stream_unlock
;
3136 if (stream
->prev_data_seq
== -1ULL) {
3140 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3141 state
->header
.padding_size
, state
->header
.net_seq_num
,
3144 status
= RELAY_CONNECTION_STATUS_ERROR
;
3145 goto end_stream_unlock
;
3149 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3150 * contents of *state which are aliased (union) to the same location as
3151 * the new state. Don't use it beyond this point.
3153 connection_reset_protocol_state(conn
);
3157 close_requested
= stream
->close_requested
;
3158 pthread_mutex_unlock(&stream
->lock
);
3159 if (close_requested
&& left_to_receive
== 0) {
3160 try_stream_close(stream
);
3164 pthread_mutex_lock(&session
->lock
);
3165 uatomic_set(&session
->new_streams
, 1);
3166 pthread_mutex_unlock(&session
->lock
);
3175 * relay_process_data: Process the data received on the data socket
3177 static enum relay_connection_status
relay_process_data(
3178 struct relay_connection
*conn
)
3180 enum relay_connection_status status
;
3182 switch (conn
->protocol
.data
.state_id
) {
3183 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3184 status
= relay_process_data_receive_header(conn
);
3186 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3187 status
= relay_process_data_receive_payload(conn
);
3190 ERR("Unexpected data connection communication state.");
3197 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3201 (void) lttng_poll_del(events
, pollfd
);
3203 ret
= close(pollfd
);
3205 ERR("Closing pollfd %d", pollfd
);
3209 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3210 int pollfd
, struct relay_connection
*conn
)
3212 const char *type_str
;
3214 switch (conn
->type
) {
3219 type_str
= "Control";
3221 case RELAY_VIEWER_COMMAND
:
3222 type_str
= "Viewer Command";
3224 case RELAY_VIEWER_NOTIFICATION
:
3225 type_str
= "Viewer Notification";
3228 type_str
= "Unknown";
3230 cleanup_connection_pollfd(events
, pollfd
);
3231 connection_put(conn
);
3232 DBG("%s connection closed with %d", type_str
, pollfd
);
3236 * This thread does the actual work
3238 static void *relay_thread_worker(void *data
)
3240 int ret
, err
= -1, last_seen_data_fd
= -1;
3242 struct lttng_poll_event events
;
3243 struct lttng_ht
*relay_connections_ht
;
3244 struct lttng_ht_iter iter
;
3245 struct relay_connection
*destroy_conn
= NULL
;
3247 DBG("[thread] Relay worker started");
3249 rcu_register_thread();
3251 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3253 if (testpoint(relayd_thread_worker
)) {
3254 goto error_testpoint
;
3257 health_code_update();
3259 /* table of connections indexed on socket */
3260 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3261 if (!relay_connections_ht
) {
3262 goto relay_connections_ht_error
;
3265 ret
= create_thread_poll_set(&events
, 2);
3267 goto error_poll_create
;
3270 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3277 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3279 health_code_update();
3281 /* Infinite blocking call, waiting for transmission */
3282 DBG3("Relayd worker thread polling...");
3283 health_poll_entry();
3284 ret
= lttng_poll_wait(&events
, -1);
3288 * Restart interrupted system call.
3290 if (errno
== EINTR
) {
3299 * Process control. The control connection is
3300 * prioritized so we don't starve it with high
3301 * throughput tracing data on the data connection.
3303 for (i
= 0; i
< nb_fd
; i
++) {
3304 /* Fetch once the poll data */
3305 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3306 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3308 health_code_update();
3310 /* Thread quit pipe has been closed. Killing thread. */
3311 ret
= check_thread_quit_pipe(pollfd
, revents
);
3317 /* Inspect the relay conn pipe for new connection */
3318 if (pollfd
== relay_conn_pipe
[0]) {
3319 if (revents
& LPOLLIN
) {
3320 struct relay_connection
*conn
;
3322 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3326 lttng_poll_add(&events
, conn
->sock
->fd
,
3327 LPOLLIN
| LPOLLRDHUP
);
3328 connection_ht_add(relay_connections_ht
, conn
);
3329 DBG("Connection socket %d added", conn
->sock
->fd
);
3330 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3331 ERR("Relay connection pipe error");
3334 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3338 struct relay_connection
*ctrl_conn
;
3340 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3341 /* If not found, there is a synchronization issue. */
3344 if (ctrl_conn
->type
== RELAY_DATA
) {
3345 if (revents
& LPOLLIN
) {
3347 * Flag the last seen data fd not deleted. It will be
3348 * used as the last seen fd if any fd gets deleted in
3351 last_notdel_data_fd
= pollfd
;
3353 goto put_ctrl_connection
;
3355 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3357 if (revents
& LPOLLIN
) {
3358 enum relay_connection_status status
;
3360 status
= relay_process_control(ctrl_conn
);
3361 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3363 * On socket error flag the session as aborted to force
3364 * the cleanup of its stream otherwise it can leak
3365 * during the lifetime of the relayd.
3367 * This prevents situations in which streams can be
3368 * left opened because an index was received, the
3369 * control connection is closed, and the data
3370 * connection is closed (uncleanly) before the packet's
3373 * Since the control connection encountered an error,
3374 * it is okay to be conservative and close the
3375 * session right now as we can't rely on the protocol
3376 * being respected anymore.
3378 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3379 session_abort(ctrl_conn
->session
);
3382 /* Clear the connection on error or close. */
3383 relay_thread_close_connection(&events
,
3388 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3389 relay_thread_close_connection(&events
,
3391 if (last_seen_data_fd
== pollfd
) {
3392 last_seen_data_fd
= last_notdel_data_fd
;
3395 ERR("Unexpected poll events %u for control sock %d",
3397 connection_put(ctrl_conn
);
3400 put_ctrl_connection
:
3401 connection_put(ctrl_conn
);
3406 * The last loop handled a control request, go back to poll to make
3407 * sure we prioritise the control socket.
3413 if (last_seen_data_fd
>= 0) {
3414 for (i
= 0; i
< nb_fd
; i
++) {
3415 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3417 health_code_update();
3419 if (last_seen_data_fd
== pollfd
) {
3426 /* Process data connection. */
3427 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3428 /* Fetch the poll data. */
3429 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3430 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3431 struct relay_connection
*data_conn
;
3433 health_code_update();
3436 /* No activity for this FD (poll implementation). */
3440 /* Skip the command pipe. It's handled in the first loop. */
3441 if (pollfd
== relay_conn_pipe
[0]) {
3445 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3447 /* Skip it. Might be removed before. */
3450 if (data_conn
->type
== RELAY_CONTROL
) {
3451 goto put_data_connection
;
3453 assert(data_conn
->type
== RELAY_DATA
);
3455 if (revents
& LPOLLIN
) {
3456 enum relay_connection_status status
;
3458 status
= relay_process_data(data_conn
);
3459 /* Connection closed or error. */
3460 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3462 * On socket error flag the session as aborted to force
3463 * the cleanup of its stream otherwise it can leak
3464 * during the lifetime of the relayd.
3466 * This prevents situations in which streams can be
3467 * left opened because an index was received, the
3468 * control connection is closed, and the data
3469 * connection is closed (uncleanly) before the packet's
3472 * Since the data connection encountered an error,
3473 * it is okay to be conservative and close the
3474 * session right now as we can't rely on the protocol
3475 * being respected anymore.
3477 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3478 session_abort(data_conn
->session
);
3480 relay_thread_close_connection(&events
, pollfd
,
3483 * Every goto restart call sets the last seen fd where
3484 * here we don't really care since we gracefully
3485 * continue the loop after the connection is deleted.
3488 /* Keep last seen port. */
3489 last_seen_data_fd
= pollfd
;
3490 connection_put(data_conn
);
3493 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3494 relay_thread_close_connection(&events
, pollfd
,
3497 ERR("Unknown poll events %u for data sock %d",
3500 put_data_connection
:
3501 connection_put(data_conn
);
3503 last_seen_data_fd
= -1;
3506 /* Normal exit, no error */
3511 /* Cleanup remaining connection object. */
3513 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3516 health_code_update();
3518 session_abort(destroy_conn
->session
);
3521 * No need to grab another ref, because we own
3524 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3529 lttng_poll_clean(&events
);
3531 lttng_ht_destroy(relay_connections_ht
);
3532 relay_connections_ht_error
:
3533 /* Close relay conn pipes */
3534 utils_close_pipe(relay_conn_pipe
);
3536 DBG("Thread exited with error");
3538 DBG("Worker thread cleanup complete");
3542 ERR("Health error occurred in %s", __func__
);
3544 health_unregister(health_relayd
);
3545 rcu_unregister_thread();
3546 lttng_relay_stop_threads();
3551 * Create the relay command pipe to wake thread_manage_apps.
3552 * Closed in cleanup().
3554 static int create_relay_conn_pipe(void)
3558 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3566 int main(int argc
, char **argv
)
3568 int ret
= 0, retval
= 0;
3571 /* Parse arguments */
3573 if (set_options(argc
, argv
)) {
3578 if (set_signal_handler()) {
3583 /* Try to create directory if -o, --output is specified. */
3584 if (opt_output_path
) {
3585 if (*opt_output_path
!= '/') {
3586 ERR("Please specify an absolute path for -o, --output PATH");
3591 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3594 ERR("Unable to create %s", opt_output_path
);
3601 if (opt_daemon
|| opt_background
) {
3604 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3612 * We are in the child. Make sure all other file
3613 * descriptors are closed, in case we are called with
3614 * more opened file descriptors than the standard ones.
3616 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3621 sessiond_trace_chunk_registry
= sessiond_trace_chunk_registry_create();
3622 if (!sessiond_trace_chunk_registry
) {
3623 ERR("Failed to initialize session daemon trace chunk registry");
3625 goto exit_sessiond_trace_chunk_registry
;
3628 /* Initialize thread health monitoring */
3629 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3630 if (!health_relayd
) {
3631 PERROR("health_app_create error");
3633 goto exit_health_app_create
;
3636 /* Create thread quit pipe */
3637 if (init_thread_quit_pipe()) {
3639 goto exit_init_data
;
3642 /* Setup the thread apps communication pipe. */
3643 if (create_relay_conn_pipe()) {
3645 goto exit_init_data
;
3648 /* Init relay command queue. */
3649 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3651 /* Initialize communication library */
3653 lttcomm_inet_init();
3655 /* tables of sessions indexed by session ID */
3656 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3659 goto exit_init_data
;
3662 /* tables of streams indexed by stream ID */
3663 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3664 if (!relay_streams_ht
) {
3666 goto exit_init_data
;
3669 /* tables of streams indexed by stream ID */
3670 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3671 if (!viewer_streams_ht
) {
3673 goto exit_init_data
;
3676 ret
= utils_create_pipe(health_quit_pipe
);
3679 goto exit_health_quit_pipe
;
3682 /* Create thread to manage the client socket */
3683 ret
= pthread_create(&health_thread
, default_pthread_attr(),
3684 thread_manage_health
, (void *) NULL
);
3687 PERROR("pthread_create health");
3689 goto exit_health_thread
;
3692 /* Setup the dispatcher thread */
3693 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
3694 relay_thread_dispatcher
, (void *) NULL
);
3697 PERROR("pthread_create dispatcher");
3699 goto exit_dispatcher_thread
;
3702 /* Setup the worker thread */
3703 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
3704 relay_thread_worker
, NULL
);
3707 PERROR("pthread_create worker");
3709 goto exit_worker_thread
;
3712 /* Setup the listener thread */
3713 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
3714 relay_thread_listener
, (void *) NULL
);
3717 PERROR("pthread_create listener");
3719 goto exit_listener_thread
;
3722 ret
= relayd_live_create(live_uri
);
3724 ERR("Starting live viewer threads");
3730 * This is where we start awaiting program completion (e.g. through
3731 * signal that asks threads to teardown).
3734 ret
= relayd_live_join();
3740 ret
= pthread_join(listener_thread
, &status
);
3743 PERROR("pthread_join listener_thread");
3747 exit_listener_thread
:
3748 ret
= pthread_join(worker_thread
, &status
);
3751 PERROR("pthread_join worker_thread");
3756 ret
= pthread_join(dispatcher_thread
, &status
);
3759 PERROR("pthread_join dispatcher_thread");
3762 exit_dispatcher_thread
:
3764 ret
= pthread_join(health_thread
, &status
);
3767 PERROR("pthread_join health_thread");
3772 utils_close_pipe(health_quit_pipe
);
3773 exit_health_quit_pipe
:
3776 health_app_destroy(health_relayd
);
3777 sessiond_trace_chunk_registry_destroy(sessiond_trace_chunk_registry
);
3778 exit_health_app_create
:
3779 exit_sessiond_trace_chunk_registry
:
3782 * Wait for all pending call_rcu work to complete before tearing
3783 * down data structures. call_rcu worker may be trying to
3784 * perform lookups in those structures.
3789 /* Ensure all prior call_rcu are done. */