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 lttng_trace_chunk_put(conn
->session
->current_trace_chunk
);
2427 conn
->session
->current_trace_chunk
= published_chunk
;
2428 published_chunk
= NULL
;
2429 pthread_mutex_unlock(&conn
->session
->lock
);
2432 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2433 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2435 sizeof(struct lttcomm_relayd_generic_reply
),
2437 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2438 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2443 lttng_trace_chunk_put(chunk
);
2444 lttng_trace_chunk_put(published_chunk
);
2445 lttng_directory_handle_fini(&session_output
);
2450 * relay_close_trace_chunk: close a trace chunk
2452 static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr
*recv_hdr
,
2453 struct relay_connection
*conn
,
2454 const struct lttng_buffer_view
*payload
)
2458 struct relay_session
*session
= conn
->session
;
2459 struct lttcomm_relayd_close_trace_chunk
*msg
;
2460 struct lttcomm_relayd_generic_reply reply
= {};
2461 struct lttng_buffer_view header_view
;
2462 struct lttng_trace_chunk
*chunk
= NULL
;
2463 enum lttng_error_code reply_code
= LTTNG_OK
;
2464 enum lttng_trace_chunk_status chunk_status
;
2466 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
2467 time_t close_timestamp
;
2469 if (!session
|| !conn
->version_check_done
) {
2470 ERR("Trying to close a trace chunk before version check");
2475 if (session
->major
== 2 && session
->minor
< 11) {
2476 ERR("Chunk close command is unsupported before 2.11");
2481 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2482 if (!header_view
.data
) {
2483 ERR("Failed to receive payload of chunk close command");
2488 /* Convert to host endianness. */
2489 msg
= (typeof(msg
)) header_view
.data
;
2490 chunk_id
= be64toh(msg
->chunk_id
);
2491 close_timestamp
= (time_t) be64toh(msg
->close_timestamp
);
2492 close_command
= (typeof(close_command
)){
2493 .value
= be32toh(msg
->close_command
.value
),
2494 .is_set
= msg
->close_command
.is_set
,
2497 chunk
= sessiond_trace_chunk_registry_get_chunk(
2498 sessiond_trace_chunk_registry
,
2499 conn
->session
->sessiond_uuid
,
2503 char uuid_str
[UUID_STR_LEN
];
2505 lttng_uuid_to_str(conn
->session
->sessiond_uuid
, uuid_str
);
2506 ERR("Failed to find chunk to close: sessiond_uuid = %s, session_id = %" PRIu64
", chunk_id = %" PRIu64
,
2511 reply_code
= LTTNG_ERR_NOMEM
;
2515 chunk_status
= lttng_trace_chunk_set_close_timestamp(
2516 chunk
, close_timestamp
);
2517 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2518 ERR("Failed to set trace chunk close timestamp");
2520 reply_code
= LTTNG_ERR_UNK
;
2524 if (close_command
.is_set
) {
2525 chunk_status
= lttng_trace_chunk_set_close_command(
2526 chunk
, close_command
.value
);
2527 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2529 reply_code
= LTTNG_ERR_INVALID
;
2534 pthread_mutex_lock(&session
->lock
);
2535 if (session
->current_trace_chunk
== chunk
) {
2537 * After a trace chunk close command, no new streams
2538 * referencing the chunk may be created. Hence, on the
2539 * event that no new trace chunk have been created for
2540 * the session, the reference to the current trace chunk
2541 * is released in order to allow it to be reclaimed when
2542 * the last stream releases its reference to it.
2544 lttng_trace_chunk_put(session
->current_trace_chunk
);
2545 session
->current_trace_chunk
= NULL
;
2547 pthread_mutex_unlock(&session
->lock
);
2550 reply
.ret_code
= htobe32((uint32_t) reply_code
);
2551 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2553 sizeof(struct lttcomm_relayd_generic_reply
),
2555 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2556 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2561 lttng_trace_chunk_put(chunk
);
2566 * relay_trace_chunk_exists: check if a trace chunk exists
2568 static int relay_trace_chunk_exists(const struct lttcomm_relayd_hdr
*recv_hdr
,
2569 struct relay_connection
*conn
,
2570 const struct lttng_buffer_view
*payload
)
2574 struct relay_session
*session
= conn
->session
;
2575 struct lttcomm_relayd_trace_chunk_exists
*msg
;
2576 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
2577 struct lttng_buffer_view header_view
;
2578 struct lttng_trace_chunk
*chunk
= NULL
;
2581 if (!session
|| !conn
->version_check_done
) {
2582 ERR("Trying to close a trace chunk before version check");
2587 if (session
->major
== 2 && session
->minor
< 11) {
2588 ERR("Chunk close command is unsupported before 2.11");
2593 header_view
= lttng_buffer_view_from_view(payload
, 0, sizeof(*msg
));
2594 if (!header_view
.data
) {
2595 ERR("Failed to receive payload of chunk close command");
2600 /* Convert to host endianness. */
2601 msg
= (typeof(msg
)) header_view
.data
;
2602 chunk_id
= be64toh(msg
->chunk_id
);
2604 chunk
= sessiond_trace_chunk_registry_get_chunk(
2605 sessiond_trace_chunk_registry
,
2606 conn
->session
->sessiond_uuid
,
2610 reply
= (typeof(reply
)) {
2611 .generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
),
2612 .trace_chunk_exists
= !!chunk
,
2614 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
,
2615 &reply
, sizeof(reply
), 0);
2616 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2617 ERR("Failed to send \"create trace chunk\" command reply (ret = %zd)",
2622 lttng_trace_chunk_put(chunk
);
2626 #define DBG_CMD(cmd_name, conn) \
2627 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2629 static int relay_process_control_command(struct relay_connection
*conn
,
2630 const struct lttcomm_relayd_hdr
*header
,
2631 const struct lttng_buffer_view
*payload
)
2635 switch (header
->cmd
) {
2636 case RELAYD_CREATE_SESSION
:
2637 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2638 ret
= relay_create_session(header
, conn
, payload
);
2640 case RELAYD_ADD_STREAM
:
2641 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2642 ret
= relay_add_stream(header
, conn
, payload
);
2644 case RELAYD_START_DATA
:
2645 DBG_CMD("RELAYD_START_DATA", conn
);
2646 ret
= relay_start(header
, conn
, payload
);
2648 case RELAYD_SEND_METADATA
:
2649 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2650 ret
= relay_recv_metadata(header
, conn
, payload
);
2652 case RELAYD_VERSION
:
2653 DBG_CMD("RELAYD_VERSION", conn
);
2654 ret
= relay_send_version(header
, conn
, payload
);
2656 case RELAYD_CLOSE_STREAM
:
2657 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2658 ret
= relay_close_stream(header
, conn
, payload
);
2660 case RELAYD_DATA_PENDING
:
2661 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2662 ret
= relay_data_pending(header
, conn
, payload
);
2664 case RELAYD_QUIESCENT_CONTROL
:
2665 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2666 ret
= relay_quiescent_control(header
, conn
, payload
);
2668 case RELAYD_BEGIN_DATA_PENDING
:
2669 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2670 ret
= relay_begin_data_pending(header
, conn
, payload
);
2672 case RELAYD_END_DATA_PENDING
:
2673 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2674 ret
= relay_end_data_pending(header
, conn
, payload
);
2676 case RELAYD_SEND_INDEX
:
2677 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2678 ret
= relay_recv_index(header
, conn
, payload
);
2680 case RELAYD_STREAMS_SENT
:
2681 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2682 ret
= relay_streams_sent(header
, conn
, payload
);
2684 case RELAYD_RESET_METADATA
:
2685 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2686 ret
= relay_reset_metadata(header
, conn
, payload
);
2688 case RELAYD_ROTATE_STREAMS
:
2689 DBG_CMD("RELAYD_ROTATE_STREAMS", conn
);
2690 ret
= relay_rotate_session_streams(header
, conn
, payload
);
2692 case RELAYD_CREATE_TRACE_CHUNK
:
2693 DBG_CMD("RELAYD_CREATE_TRACE_CHUNK", conn
);
2694 ret
= relay_create_trace_chunk(header
, conn
, payload
);
2696 case RELAYD_CLOSE_TRACE_CHUNK
:
2697 DBG_CMD("RELAYD_CLOSE_TRACE_CHUNK", conn
);
2698 ret
= relay_close_trace_chunk(header
, conn
, payload
);
2700 case RELAYD_TRACE_CHUNK_EXISTS
:
2701 DBG_CMD("RELAYD_TRACE_CHUNK_EXISTS", conn
);
2702 ret
= relay_trace_chunk_exists(header
, conn
, payload
);
2704 case RELAYD_UPDATE_SYNC_INFO
:
2706 ERR("Received unknown command (%u)", header
->cmd
);
2707 relay_unknown_command(conn
);
2716 static enum relay_connection_status
relay_process_control_receive_payload(
2717 struct relay_connection
*conn
)
2720 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2721 struct lttng_dynamic_buffer
*reception_buffer
=
2722 &conn
->protocol
.ctrl
.reception_buffer
;
2723 struct ctrl_connection_state_receive_payload
*state
=
2724 &conn
->protocol
.ctrl
.state
.receive_payload
;
2725 struct lttng_buffer_view payload_view
;
2727 if (state
->left_to_receive
== 0) {
2728 /* Short-circuit for payload-less commands. */
2729 goto reception_complete
;
2732 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2733 reception_buffer
->data
+ state
->received
,
2734 state
->left_to_receive
, MSG_DONTWAIT
);
2736 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2737 PERROR("Unable to receive command payload on sock %d",
2739 status
= RELAY_CONNECTION_STATUS_ERROR
;
2742 } else if (ret
== 0) {
2743 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2744 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2749 assert(ret
<= state
->left_to_receive
);
2751 state
->left_to_receive
-= ret
;
2752 state
->received
+= ret
;
2754 if (state
->left_to_receive
> 0) {
2756 * Can't transition to the protocol's next state, wait to
2757 * receive the rest of the header.
2759 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2760 state
->received
, state
->left_to_receive
,
2766 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
2767 conn
->sock
->fd
, state
->received
);
2769 * The payload required to process the command has been received.
2770 * A view to the reception buffer is forwarded to the various
2771 * commands and the state of the control is reset on success.
2773 * Commands are responsible for sending their reply to the peer.
2775 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
2777 ret
= relay_process_control_command(conn
,
2778 &state
->header
, &payload_view
);
2780 status
= RELAY_CONNECTION_STATUS_ERROR
;
2784 ret
= connection_reset_protocol_state(conn
);
2786 status
= RELAY_CONNECTION_STATUS_ERROR
;
2792 static enum relay_connection_status
relay_process_control_receive_header(
2793 struct relay_connection
*conn
)
2796 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2797 struct lttcomm_relayd_hdr header
;
2798 struct lttng_dynamic_buffer
*reception_buffer
=
2799 &conn
->protocol
.ctrl
.reception_buffer
;
2800 struct ctrl_connection_state_receive_header
*state
=
2801 &conn
->protocol
.ctrl
.state
.receive_header
;
2803 assert(state
->left_to_receive
!= 0);
2805 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2806 reception_buffer
->data
+ state
->received
,
2807 state
->left_to_receive
, MSG_DONTWAIT
);
2809 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2810 PERROR("Unable to receive control command header on sock %d",
2812 status
= RELAY_CONNECTION_STATUS_ERROR
;
2815 } else if (ret
== 0) {
2816 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2817 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2822 assert(ret
<= state
->left_to_receive
);
2824 state
->left_to_receive
-= ret
;
2825 state
->received
+= ret
;
2827 if (state
->left_to_receive
> 0) {
2829 * Can't transition to the protocol's next state, wait to
2830 * receive the rest of the header.
2832 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2833 state
->received
, state
->left_to_receive
,
2838 /* Transition to next state: receiving the command's payload. */
2839 conn
->protocol
.ctrl
.state_id
=
2840 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
2841 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
2842 header
.circuit_id
= be64toh(header
.circuit_id
);
2843 header
.data_size
= be64toh(header
.data_size
);
2844 header
.cmd
= be32toh(header
.cmd
);
2845 header
.cmd_version
= be32toh(header
.cmd_version
);
2846 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
2847 &header
, sizeof(header
));
2849 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
2850 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
2853 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
2854 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
2856 status
= RELAY_CONNECTION_STATUS_ERROR
;
2860 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
2862 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
2863 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
2866 status
= RELAY_CONNECTION_STATUS_ERROR
;
2870 if (header
.data_size
== 0) {
2872 * Manually invoke the next state as the poll loop
2873 * will not wake-up to allow us to proceed further.
2875 status
= relay_process_control_receive_payload(conn
);
2882 * Process the commands received on the control socket
2884 static enum relay_connection_status
relay_process_control(
2885 struct relay_connection
*conn
)
2887 enum relay_connection_status status
;
2889 switch (conn
->protocol
.ctrl
.state_id
) {
2890 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
2891 status
= relay_process_control_receive_header(conn
);
2893 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
2894 status
= relay_process_control_receive_payload(conn
);
2897 ERR("Unknown control connection protocol state encountered.");
2904 static enum relay_connection_status
relay_process_data_receive_header(
2905 struct relay_connection
*conn
)
2908 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2909 struct data_connection_state_receive_header
*state
=
2910 &conn
->protocol
.data
.state
.receive_header
;
2911 struct lttcomm_relayd_data_hdr header
;
2912 struct relay_stream
*stream
;
2914 assert(state
->left_to_receive
!= 0);
2916 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2917 state
->header_reception_buffer
+ state
->received
,
2918 state
->left_to_receive
, MSG_DONTWAIT
);
2920 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2921 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2922 status
= RELAY_CONNECTION_STATUS_ERROR
;
2925 } else if (ret
== 0) {
2926 /* Orderly shutdown. Not necessary to print an error. */
2927 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2928 status
= RELAY_CONNECTION_STATUS_CLOSED
;
2933 assert(ret
<= state
->left_to_receive
);
2935 state
->left_to_receive
-= ret
;
2936 state
->received
+= ret
;
2938 if (state
->left_to_receive
> 0) {
2940 * Can't transition to the protocol's next state, wait to
2941 * receive the rest of the header.
2943 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2944 state
->received
, state
->left_to_receive
,
2949 /* Transition to next state: receiving the payload. */
2950 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
2952 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
2953 header
.circuit_id
= be64toh(header
.circuit_id
);
2954 header
.stream_id
= be64toh(header
.stream_id
);
2955 header
.data_size
= be32toh(header
.data_size
);
2956 header
.net_seq_num
= be64toh(header
.net_seq_num
);
2957 header
.padding_size
= be32toh(header
.padding_size
);
2958 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
2960 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
2962 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
2963 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
2965 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
2966 conn
->sock
->fd
, header
.circuit_id
,
2967 header
.stream_id
, header
.data_size
,
2968 header
.net_seq_num
, header
.padding_size
);
2970 stream
= stream_get_by_id(header
.stream_id
);
2972 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
2974 /* Protocol error. */
2975 status
= RELAY_CONNECTION_STATUS_ERROR
;
2979 pthread_mutex_lock(&stream
->lock
);
2980 /* Prepare stream for the reception of a new packet. */
2981 ret
= stream_init_packet(stream
, header
.data_size
,
2982 &conn
->protocol
.data
.state
.receive_payload
.rotate_index
);
2983 pthread_mutex_unlock(&stream
->lock
);
2985 ERR("Failed to rotate stream output file");
2986 status
= RELAY_CONNECTION_STATUS_ERROR
;
2987 goto end_stream_unlock
;
2996 static enum relay_connection_status
relay_process_data_receive_payload(
2997 struct relay_connection
*conn
)
3000 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3001 struct relay_stream
*stream
;
3002 struct data_connection_state_receive_payload
*state
=
3003 &conn
->protocol
.data
.state
.receive_payload
;
3004 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3005 char data_buffer
[chunk_size
];
3006 bool partial_recv
= false;
3007 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3008 uint64_t left_to_receive
= state
->left_to_receive
;
3009 struct relay_session
*session
;
3011 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3012 state
->header
.stream_id
, state
->header
.net_seq_num
,
3013 state
->received
, left_to_receive
);
3015 stream
= stream_get_by_id(state
->header
.stream_id
);
3017 /* Protocol error. */
3018 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3019 state
->header
.stream_id
);
3020 status
= RELAY_CONNECTION_STATUS_ERROR
;
3024 pthread_mutex_lock(&stream
->lock
);
3025 session
= stream
->trace
->session
;
3026 if (!conn
->session
) {
3027 ret
= connection_set_session(conn
, session
);
3029 status
= RELAY_CONNECTION_STATUS_ERROR
;
3030 goto end_stream_unlock
;
3035 * The size of the "chunk" received on any iteration is bounded by:
3036 * - the data left to receive,
3037 * - the data immediately available on the socket,
3038 * - the on-stack data buffer
3040 while (left_to_receive
> 0 && !partial_recv
) {
3041 size_t recv_size
= min(left_to_receive
, chunk_size
);
3042 struct lttng_buffer_view packet_chunk
;
3044 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3045 recv_size
, MSG_DONTWAIT
);
3047 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3048 PERROR("Socket %d error", conn
->sock
->fd
);
3049 status
= RELAY_CONNECTION_STATUS_ERROR
;
3051 goto end_stream_unlock
;
3052 } else if (ret
== 0) {
3053 /* No more data ready to be consumed on socket. */
3054 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3055 state
->header
.stream_id
);
3056 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3058 } else if (ret
< (int) recv_size
) {
3060 * All the data available on the socket has been
3063 partial_recv
= true;
3067 packet_chunk
= lttng_buffer_view_init(data_buffer
,
3069 assert(packet_chunk
.data
);
3071 ret
= stream_write(stream
, &packet_chunk
, 0);
3073 ERR("Relay error writing data to file");
3074 status
= RELAY_CONNECTION_STATUS_ERROR
;
3075 goto end_stream_unlock
;
3078 left_to_receive
-= recv_size
;
3079 state
->received
+= recv_size
;
3080 state
->left_to_receive
= left_to_receive
;
3083 if (state
->left_to_receive
> 0) {
3085 * Did not receive all the data expected, wait for more data to
3086 * become available on the socket.
3088 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3089 state
->header
.stream_id
, state
->received
,
3090 state
->left_to_receive
);
3091 goto end_stream_unlock
;
3094 ret
= stream_write(stream
, NULL
, state
->header
.padding_size
);
3096 status
= RELAY_CONNECTION_STATUS_ERROR
;
3097 goto end_stream_unlock
;
3100 if (session_streams_have_index(session
)) {
3101 ret
= stream_update_index(stream
, state
->header
.net_seq_num
,
3102 state
->rotate_index
, &index_flushed
,
3103 state
->header
.data_size
+ state
->header
.padding_size
);
3105 ERR("Failed to update index: stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3106 stream
->stream_handle
,
3107 state
->header
.net_seq_num
, ret
);
3108 status
= RELAY_CONNECTION_STATUS_ERROR
;
3109 goto end_stream_unlock
;
3113 if (stream
->prev_data_seq
== -1ULL) {
3117 ret
= stream_complete_packet(stream
, state
->header
.data_size
+
3118 state
->header
.padding_size
, state
->header
.net_seq_num
,
3121 status
= RELAY_CONNECTION_STATUS_ERROR
;
3122 goto end_stream_unlock
;
3126 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3127 * contents of *state which are aliased (union) to the same location as
3128 * the new state. Don't use it beyond this point.
3130 connection_reset_protocol_state(conn
);
3134 close_requested
= stream
->close_requested
;
3135 pthread_mutex_unlock(&stream
->lock
);
3136 if (close_requested
&& left_to_receive
== 0) {
3137 try_stream_close(stream
);
3141 pthread_mutex_lock(&session
->lock
);
3142 uatomic_set(&session
->new_streams
, 1);
3143 pthread_mutex_unlock(&session
->lock
);
3152 * relay_process_data: Process the data received on the data socket
3154 static enum relay_connection_status
relay_process_data(
3155 struct relay_connection
*conn
)
3157 enum relay_connection_status status
;
3159 switch (conn
->protocol
.data
.state_id
) {
3160 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3161 status
= relay_process_data_receive_header(conn
);
3163 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3164 status
= relay_process_data_receive_payload(conn
);
3167 ERR("Unexpected data connection communication state.");
3174 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3178 (void) lttng_poll_del(events
, pollfd
);
3180 ret
= close(pollfd
);
3182 ERR("Closing pollfd %d", pollfd
);
3186 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3187 int pollfd
, struct relay_connection
*conn
)
3189 const char *type_str
;
3191 switch (conn
->type
) {
3196 type_str
= "Control";
3198 case RELAY_VIEWER_COMMAND
:
3199 type_str
= "Viewer Command";
3201 case RELAY_VIEWER_NOTIFICATION
:
3202 type_str
= "Viewer Notification";
3205 type_str
= "Unknown";
3207 cleanup_connection_pollfd(events
, pollfd
);
3208 connection_put(conn
);
3209 DBG("%s connection closed with %d", type_str
, pollfd
);
3213 * This thread does the actual work
3215 static void *relay_thread_worker(void *data
)
3217 int ret
, err
= -1, last_seen_data_fd
= -1;
3219 struct lttng_poll_event events
;
3220 struct lttng_ht
*relay_connections_ht
;
3221 struct lttng_ht_iter iter
;
3222 struct relay_connection
*destroy_conn
= NULL
;
3224 DBG("[thread] Relay worker started");
3226 rcu_register_thread();
3228 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3230 if (testpoint(relayd_thread_worker
)) {
3231 goto error_testpoint
;
3234 health_code_update();
3236 /* table of connections indexed on socket */
3237 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3238 if (!relay_connections_ht
) {
3239 goto relay_connections_ht_error
;
3242 ret
= create_thread_poll_set(&events
, 2);
3244 goto error_poll_create
;
3247 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3254 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3256 health_code_update();
3258 /* Infinite blocking call, waiting for transmission */
3259 DBG3("Relayd worker thread polling...");
3260 health_poll_entry();
3261 ret
= lttng_poll_wait(&events
, -1);
3265 * Restart interrupted system call.
3267 if (errno
== EINTR
) {
3276 * Process control. The control connection is
3277 * prioritized so we don't starve it with high
3278 * throughput tracing data on the data connection.
3280 for (i
= 0; i
< nb_fd
; i
++) {
3281 /* Fetch once the poll data */
3282 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3283 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3285 health_code_update();
3287 /* Thread quit pipe has been closed. Killing thread. */
3288 ret
= check_thread_quit_pipe(pollfd
, revents
);
3294 /* Inspect the relay conn pipe for new connection */
3295 if (pollfd
== relay_conn_pipe
[0]) {
3296 if (revents
& LPOLLIN
) {
3297 struct relay_connection
*conn
;
3299 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3303 lttng_poll_add(&events
, conn
->sock
->fd
,
3304 LPOLLIN
| LPOLLRDHUP
);
3305 connection_ht_add(relay_connections_ht
, conn
);
3306 DBG("Connection socket %d added", conn
->sock
->fd
);
3307 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3308 ERR("Relay connection pipe error");
3311 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3315 struct relay_connection
*ctrl_conn
;
3317 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3318 /* If not found, there is a synchronization issue. */
3321 if (ctrl_conn
->type
== RELAY_DATA
) {
3322 if (revents
& LPOLLIN
) {
3324 * Flag the last seen data fd not deleted. It will be
3325 * used as the last seen fd if any fd gets deleted in
3328 last_notdel_data_fd
= pollfd
;
3330 goto put_ctrl_connection
;
3332 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3334 if (revents
& LPOLLIN
) {
3335 enum relay_connection_status status
;
3337 status
= relay_process_control(ctrl_conn
);
3338 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3340 * On socket error flag the session as aborted to force
3341 * the cleanup of its stream otherwise it can leak
3342 * during the lifetime of the relayd.
3344 * This prevents situations in which streams can be
3345 * left opened because an index was received, the
3346 * control connection is closed, and the data
3347 * connection is closed (uncleanly) before the packet's
3350 * Since the control connection encountered an error,
3351 * it is okay to be conservative and close the
3352 * session right now as we can't rely on the protocol
3353 * being respected anymore.
3355 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3356 session_abort(ctrl_conn
->session
);
3359 /* Clear the connection on error or close. */
3360 relay_thread_close_connection(&events
,
3365 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3366 relay_thread_close_connection(&events
,
3368 if (last_seen_data_fd
== pollfd
) {
3369 last_seen_data_fd
= last_notdel_data_fd
;
3372 ERR("Unexpected poll events %u for control sock %d",
3374 connection_put(ctrl_conn
);
3377 put_ctrl_connection
:
3378 connection_put(ctrl_conn
);
3383 * The last loop handled a control request, go back to poll to make
3384 * sure we prioritise the control socket.
3390 if (last_seen_data_fd
>= 0) {
3391 for (i
= 0; i
< nb_fd
; i
++) {
3392 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3394 health_code_update();
3396 if (last_seen_data_fd
== pollfd
) {
3403 /* Process data connection. */
3404 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3405 /* Fetch the poll data. */
3406 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3407 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3408 struct relay_connection
*data_conn
;
3410 health_code_update();
3413 /* No activity for this FD (poll implementation). */
3417 /* Skip the command pipe. It's handled in the first loop. */
3418 if (pollfd
== relay_conn_pipe
[0]) {
3422 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3424 /* Skip it. Might be removed before. */
3427 if (data_conn
->type
== RELAY_CONTROL
) {
3428 goto put_data_connection
;
3430 assert(data_conn
->type
== RELAY_DATA
);
3432 if (revents
& LPOLLIN
) {
3433 enum relay_connection_status status
;
3435 status
= relay_process_data(data_conn
);
3436 /* Connection closed or error. */
3437 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3439 * On socket error flag the session as aborted to force
3440 * the cleanup of its stream otherwise it can leak
3441 * during the lifetime of the relayd.
3443 * This prevents situations in which streams can be
3444 * left opened because an index was received, the
3445 * control connection is closed, and the data
3446 * connection is closed (uncleanly) before the packet's
3449 * Since the data connection encountered an error,
3450 * it is okay to be conservative and close the
3451 * session right now as we can't rely on the protocol
3452 * being respected anymore.
3454 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3455 session_abort(data_conn
->session
);
3457 relay_thread_close_connection(&events
, pollfd
,
3460 * Every goto restart call sets the last seen fd where
3461 * here we don't really care since we gracefully
3462 * continue the loop after the connection is deleted.
3465 /* Keep last seen port. */
3466 last_seen_data_fd
= pollfd
;
3467 connection_put(data_conn
);
3470 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3471 relay_thread_close_connection(&events
, pollfd
,
3474 ERR("Unknown poll events %u for data sock %d",
3477 put_data_connection
:
3478 connection_put(data_conn
);
3480 last_seen_data_fd
= -1;
3483 /* Normal exit, no error */
3488 /* Cleanup remaining connection object. */
3490 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3493 health_code_update();
3495 session_abort(destroy_conn
->session
);
3498 * No need to grab another ref, because we own
3501 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3506 lttng_poll_clean(&events
);
3508 lttng_ht_destroy(relay_connections_ht
);
3509 relay_connections_ht_error
:
3510 /* Close relay conn pipes */
3511 utils_close_pipe(relay_conn_pipe
);
3513 DBG("Thread exited with error");
3515 DBG("Worker thread cleanup complete");
3519 ERR("Health error occurred in %s", __func__
);
3521 health_unregister(health_relayd
);
3522 rcu_unregister_thread();
3523 lttng_relay_stop_threads();
3528 * Create the relay command pipe to wake thread_manage_apps.
3529 * Closed in cleanup().
3531 static int create_relay_conn_pipe(void)
3535 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3543 int main(int argc
, char **argv
)
3545 int ret
= 0, retval
= 0;
3548 /* Parse arguments */
3550 if (set_options(argc
, argv
)) {
3555 if (set_signal_handler()) {
3560 /* Try to create directory if -o, --output is specified. */
3561 if (opt_output_path
) {
3562 if (*opt_output_path
!= '/') {
3563 ERR("Please specify an absolute path for -o, --output PATH");
3568 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3571 ERR("Unable to create %s", opt_output_path
);
3578 if (opt_daemon
|| opt_background
) {
3581 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3589 * We are in the child. Make sure all other file
3590 * descriptors are closed, in case we are called with
3591 * more opened file descriptors than the standard ones.
3593 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3598 sessiond_trace_chunk_registry
= sessiond_trace_chunk_registry_create();
3599 if (!sessiond_trace_chunk_registry
) {
3600 ERR("Failed to initialize session daemon trace chunk registry");
3602 goto exit_sessiond_trace_chunk_registry
;
3605 /* Initialize thread health monitoring */
3606 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3607 if (!health_relayd
) {
3608 PERROR("health_app_create error");
3610 goto exit_health_app_create
;
3613 /* Create thread quit pipe */
3614 if (init_thread_quit_pipe()) {
3616 goto exit_init_data
;
3619 /* Setup the thread apps communication pipe. */
3620 if (create_relay_conn_pipe()) {
3622 goto exit_init_data
;
3625 /* Init relay command queue. */
3626 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3628 /* Initialize communication library */
3630 lttcomm_inet_init();
3632 /* tables of sessions indexed by session ID */
3633 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3636 goto exit_init_data
;
3639 /* tables of streams indexed by stream ID */
3640 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3641 if (!relay_streams_ht
) {
3643 goto exit_init_data
;
3646 /* tables of streams indexed by stream ID */
3647 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3648 if (!viewer_streams_ht
) {
3650 goto exit_init_data
;
3653 ret
= utils_create_pipe(health_quit_pipe
);
3656 goto exit_health_quit_pipe
;
3659 /* Create thread to manage the client socket */
3660 ret
= pthread_create(&health_thread
, default_pthread_attr(),
3661 thread_manage_health
, (void *) NULL
);
3664 PERROR("pthread_create health");
3666 goto exit_health_thread
;
3669 /* Setup the dispatcher thread */
3670 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
3671 relay_thread_dispatcher
, (void *) NULL
);
3674 PERROR("pthread_create dispatcher");
3676 goto exit_dispatcher_thread
;
3679 /* Setup the worker thread */
3680 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
3681 relay_thread_worker
, NULL
);
3684 PERROR("pthread_create worker");
3686 goto exit_worker_thread
;
3689 /* Setup the listener thread */
3690 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
3691 relay_thread_listener
, (void *) NULL
);
3694 PERROR("pthread_create listener");
3696 goto exit_listener_thread
;
3699 ret
= relayd_live_create(live_uri
);
3701 ERR("Starting live viewer threads");
3707 * This is where we start awaiting program completion (e.g. through
3708 * signal that asks threads to teardown).
3711 ret
= relayd_live_join();
3717 ret
= pthread_join(listener_thread
, &status
);
3720 PERROR("pthread_join listener_thread");
3724 exit_listener_thread
:
3725 ret
= pthread_join(worker_thread
, &status
);
3728 PERROR("pthread_join worker_thread");
3733 ret
= pthread_join(dispatcher_thread
, &status
);
3736 PERROR("pthread_join dispatcher_thread");
3739 exit_dispatcher_thread
:
3741 ret
= pthread_join(health_thread
, &status
);
3744 PERROR("pthread_join health_thread");
3749 utils_close_pipe(health_quit_pipe
);
3750 exit_health_quit_pipe
:
3753 health_app_destroy(health_relayd
);
3754 sessiond_trace_chunk_registry_destroy(sessiond_trace_chunk_registry
);
3755 exit_health_app_create
:
3756 exit_sessiond_trace_chunk_registry
:
3759 * Wait for all pending call_rcu work to complete before tearing
3760 * down data structures. call_rcu worker may be trying to
3761 * perform lookups in those structures.
3766 /* Ensure all prior call_rcu are done. */