2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #include <urcu/uatomic.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/compat/getenv.h>
49 #include <common/defaults.h>
50 #include <common/daemonize.h>
51 #include <common/futex.h>
52 #include <common/sessiond-comm/sessiond-comm.h>
53 #include <common/sessiond-comm/inet.h>
54 #include <common/sessiond-comm/relayd.h>
55 #include <common/uri.h>
56 #include <common/utils.h>
57 #include <common/align.h>
58 #include <common/config/session-config.h>
59 #include <common/dynamic-buffer.h>
60 #include <common/buffer-view.h>
61 #include <urcu/rculist.h>
64 #include "ctf-trace.h"
67 #include "lttng-relayd.h"
69 #include "health-relayd.h"
70 #include "testpoint.h"
71 #include "viewer-stream.h"
74 #include "connection.h"
75 #include "tracefile-array.h"
76 #include "tcp_keep_alive.h"
78 static const char *help_msg
=
79 #ifdef LTTNG_EMBED_HELP
80 #include <lttng-relayd.8.h>
86 enum relay_connection_status
{
87 RELAY_CONNECTION_STATUS_OK
,
88 /* An error occured while processing an event on the connection. */
89 RELAY_CONNECTION_STATUS_ERROR
,
90 /* Connection closed/shutdown cleanly. */
91 RELAY_CONNECTION_STATUS_CLOSED
,
94 /* command line options */
95 char *opt_output_path
;
96 static int opt_daemon
, opt_background
;
99 * We need to wait for listener and live listener threads, as well as
100 * health check thread, before being ready to signal readiness.
102 #define NR_LTTNG_RELAY_READY 3
103 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
105 /* Size of receive buffer. */
106 #define RECV_DATA_BUFFER_SIZE 65536
107 #define FILE_COPY_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 static struct option long_options
[] = {
171 { "control-port", 1, 0, 'C', },
172 { "data-port", 1, 0, 'D', },
173 { "live-port", 1, 0, 'L', },
174 { "daemonize", 0, 0, 'd', },
175 { "background", 0, 0, 'b', },
176 { "group", 1, 0, 'g', },
177 { "help", 0, 0, 'h', },
178 { "output", 1, 0, 'o', },
179 { "verbose", 0, 0, 'v', },
180 { "config", 1, 0, 'f' },
181 { "version", 0, 0, 'V' },
185 static const char *config_ignore_options
[] = { "help", "config", "version" };
188 * Take an option from the getopt output and set it in the right variable to be
191 * Return 0 on success else a negative value.
193 static int set_option(int opt
, const char *arg
, const char *optname
)
199 fprintf(stderr
, "option %s", optname
);
201 fprintf(stderr
, " with arg %s\n", arg
);
205 if (lttng_is_setuid_setgid()) {
206 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
207 "-C, --control-port");
209 ret
= uri_parse(arg
, &control_uri
);
211 ERR("Invalid control URI specified");
214 if (control_uri
->port
== 0) {
215 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
220 if (lttng_is_setuid_setgid()) {
221 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
224 ret
= uri_parse(arg
, &data_uri
);
226 ERR("Invalid data URI specified");
229 if (data_uri
->port
== 0) {
230 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
235 if (lttng_is_setuid_setgid()) {
236 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
239 ret
= uri_parse(arg
, &live_uri
);
241 ERR("Invalid live URI specified");
244 if (live_uri
->port
== 0) {
245 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
256 if (lttng_is_setuid_setgid()) {
257 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
260 tracing_group_name
= strdup(arg
);
261 if (tracing_group_name
== NULL
) {
266 tracing_group_name_override
= 1;
270 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
272 ERR("Cannot show --help for `lttng-relayd`");
277 fprintf(stdout
, "%s\n", VERSION
);
280 if (lttng_is_setuid_setgid()) {
281 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
284 ret
= asprintf(&opt_output_path
, "%s", arg
);
287 PERROR("asprintf opt_output_path");
293 /* Verbose level can increase using multiple -v */
295 lttng_opt_verbose
= config_parse_value(arg
);
297 /* Only 3 level of verbosity (-vvv). */
298 if (lttng_opt_verbose
< 3) {
299 lttng_opt_verbose
+= 1;
304 /* Unknown option or other error.
305 * Error is printed by getopt, just return */
318 * config_entry_handler_cb used to handle options read from a config file.
319 * See config_entry_handler_cb comment in common/config/session-config.h for the
320 * return value conventions.
322 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
326 if (!entry
|| !entry
->name
|| !entry
->value
) {
331 /* Check if the option is to be ignored */
332 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
333 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
338 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
339 /* Ignore if entry name is not fully matched. */
340 if (strcmp(entry
->name
, long_options
[i
].name
)) {
345 * If the option takes no argument on the command line,
346 * we have to check if the value is "true". We support
347 * non-zero numeric values, true, on and yes.
349 if (!long_options
[i
].has_arg
) {
350 ret
= config_parse_value(entry
->value
);
353 WARN("Invalid configuration value \"%s\" for option %s",
354 entry
->value
, entry
->name
);
356 /* False, skip boolean config option. */
361 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
365 WARN("Unrecognized option \"%s\" in daemon configuration file.",
372 static int set_options(int argc
, char **argv
)
374 int c
, ret
= 0, option_index
= 0, retval
= 0;
375 int orig_optopt
= optopt
, orig_optind
= optind
;
376 char *default_address
, *optstring
;
377 const char *config_path
= NULL
;
379 optstring
= utils_generate_optstring(long_options
,
380 sizeof(long_options
) / sizeof(struct option
));
386 /* Check for the --config option */
388 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
389 &option_index
)) != -1) {
393 } else if (c
!= 'f') {
397 if (lttng_is_setuid_setgid()) {
398 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
401 config_path
= utils_expand_path(optarg
);
403 ERR("Failed to resolve path: %s", optarg
);
408 ret
= config_get_section_entries(config_path
, config_section_name
,
409 config_entry_handler
, NULL
);
412 ERR("Invalid configuration option at line %i", ret
);
418 /* Reset getopt's global state */
419 optopt
= orig_optopt
;
420 optind
= orig_optind
;
422 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
427 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
434 /* assign default values */
435 if (control_uri
== NULL
) {
436 ret
= asprintf(&default_address
,
437 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
438 DEFAULT_NETWORK_CONTROL_PORT
);
440 PERROR("asprintf default data address");
445 ret
= uri_parse(default_address
, &control_uri
);
446 free(default_address
);
448 ERR("Invalid control URI specified");
453 if (data_uri
== NULL
) {
454 ret
= asprintf(&default_address
,
455 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
456 DEFAULT_NETWORK_DATA_PORT
);
458 PERROR("asprintf default data address");
463 ret
= uri_parse(default_address
, &data_uri
);
464 free(default_address
);
466 ERR("Invalid data URI specified");
471 if (live_uri
== NULL
) {
472 ret
= asprintf(&default_address
,
473 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
474 DEFAULT_NETWORK_VIEWER_PORT
);
476 PERROR("asprintf default viewer control address");
481 ret
= uri_parse(default_address
, &live_uri
);
482 free(default_address
);
484 ERR("Invalid viewer control URI specified");
495 static void print_global_objects(void)
497 rcu_register_thread();
499 print_viewer_streams();
500 print_relay_streams();
503 rcu_unregister_thread();
509 static void relayd_cleanup(void)
511 print_global_objects();
515 if (viewer_streams_ht
)
516 lttng_ht_destroy(viewer_streams_ht
);
517 if (relay_streams_ht
)
518 lttng_ht_destroy(relay_streams_ht
);
520 lttng_ht_destroy(sessions_ht
);
522 /* free the dynamically allocated opt_output_path */
523 free(opt_output_path
);
525 /* Close thread quit pipes */
526 utils_close_pipe(thread_quit_pipe
);
528 uri_free(control_uri
);
530 /* Live URI is freed in the live thread. */
532 if (tracing_group_name_override
) {
533 free((void *) tracing_group_name
);
538 * Write to writable pipe used to notify a thread.
540 static int notify_thread_pipe(int wpipe
)
544 ret
= lttng_write(wpipe
, "!", 1);
546 PERROR("write poll pipe");
554 static int notify_health_quit_pipe(int *pipe
)
558 ret
= lttng_write(pipe
[1], "4", 1);
560 PERROR("write relay health quit");
569 * Stop all relayd and relayd-live threads.
571 int lttng_relay_stop_threads(void)
575 /* Stopping all threads */
576 DBG("Terminating all threads");
577 if (notify_thread_pipe(thread_quit_pipe
[1])) {
578 ERR("write error on thread quit pipe");
582 if (notify_health_quit_pipe(health_quit_pipe
)) {
583 ERR("write error on health quit pipe");
586 /* Dispatch thread */
587 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
588 futex_nto1_wake(&relay_conn_queue
.futex
);
590 if (relayd_live_stop()) {
591 ERR("Error stopping live threads");
598 * Signal handler for the daemon
600 * Simply stop all worker threads, leaving main() return gracefully after
601 * joining all threads and calling cleanup().
603 static void sighandler(int sig
)
607 DBG("SIGINT caught");
608 if (lttng_relay_stop_threads()) {
609 ERR("Error stopping threads");
613 DBG("SIGTERM caught");
614 if (lttng_relay_stop_threads()) {
615 ERR("Error stopping threads");
619 CMM_STORE_SHARED(recv_child_signal
, 1);
627 * Setup signal handler for :
628 * SIGINT, SIGTERM, SIGPIPE
630 static int set_signal_handler(void)
636 if ((ret
= sigemptyset(&sigset
)) < 0) {
637 PERROR("sigemptyset");
644 sa
.sa_handler
= sighandler
;
645 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
650 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
655 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
660 sa
.sa_handler
= SIG_IGN
;
661 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
666 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
671 void lttng_relay_notify_ready(void)
673 /* Notify the parent of the fork() process that we are ready. */
674 if (opt_daemon
|| opt_background
) {
675 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
676 kill(child_ppid
, SIGUSR1
);
682 * Init thread quit pipe.
684 * Return -1 on error or 0 if all pipes are created.
686 static int init_thread_quit_pipe(void)
690 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
696 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
698 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
702 if (events
== NULL
|| size
== 0) {
707 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
713 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
725 * Check if the thread quit pipe was triggered.
727 * Return 1 if it was triggered else 0;
729 static int check_thread_quit_pipe(int fd
, uint32_t events
)
731 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
739 * Create and init socket from uri.
741 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
744 struct lttcomm_sock
*sock
= NULL
;
746 sock
= lttcomm_alloc_sock_from_uri(uri
);
748 ERR("Allocating socket");
752 ret
= lttcomm_create_sock(sock
);
756 DBG("Listening on sock %d", sock
->fd
);
758 ret
= sock
->ops
->bind(sock
);
760 PERROR("Failed to bind socket");
764 ret
= sock
->ops
->listen(sock
, -1);
774 lttcomm_destroy_sock(sock
);
780 * This thread manages the listening for new connections on the network
782 static void *relay_thread_listener(void *data
)
784 int i
, ret
, pollfd
, err
= -1;
785 uint32_t revents
, nb_fd
;
786 struct lttng_poll_event events
;
787 struct lttcomm_sock
*control_sock
, *data_sock
;
789 DBG("[thread] Relay listener started");
791 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
793 health_code_update();
795 control_sock
= relay_socket_create(control_uri
);
797 goto error_sock_control
;
800 data_sock
= relay_socket_create(data_uri
);
802 goto error_sock_relay
;
806 * Pass 3 as size here for the thread quit pipe, control and
809 ret
= create_thread_poll_set(&events
, 3);
811 goto error_create_poll
;
814 /* Add the control socket */
815 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
820 /* Add the data socket */
821 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
826 lttng_relay_notify_ready();
828 if (testpoint(relayd_thread_listener
)) {
829 goto error_testpoint
;
833 health_code_update();
835 DBG("Listener accepting connections");
839 ret
= lttng_poll_wait(&events
, -1);
843 * Restart interrupted system call.
845 if (errno
== EINTR
) {
853 DBG("Relay new connection received");
854 for (i
= 0; i
< nb_fd
; i
++) {
855 health_code_update();
857 /* Fetch once the poll data */
858 revents
= LTTNG_POLL_GETEV(&events
, i
);
859 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
863 * No activity for this FD (poll
869 /* Thread quit pipe has been closed. Killing thread. */
870 ret
= check_thread_quit_pipe(pollfd
, revents
);
876 if (revents
& LPOLLIN
) {
878 * A new connection is requested, therefore a
879 * sessiond/consumerd connection is allocated in
880 * this thread, enqueued to a global queue and
881 * dequeued (and freed) in the worker thread.
884 struct relay_connection
*new_conn
;
885 struct lttcomm_sock
*newsock
;
886 enum connection_type type
;
888 if (pollfd
== data_sock
->fd
) {
890 newsock
= data_sock
->ops
->accept(data_sock
);
891 DBG("Relay data connection accepted, socket %d",
894 assert(pollfd
== control_sock
->fd
);
895 type
= RELAY_CONTROL
;
896 newsock
= control_sock
->ops
->accept(control_sock
);
897 DBG("Relay control connection accepted, socket %d",
901 PERROR("accepting sock");
905 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
908 PERROR("setsockopt inet");
909 lttcomm_destroy_sock(newsock
);
913 ret
= socket_apply_keep_alive_config(newsock
->fd
);
915 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
917 lttcomm_destroy_sock(newsock
);
921 new_conn
= connection_create(newsock
, type
);
923 lttcomm_destroy_sock(newsock
);
927 /* Enqueue request for the dispatcher thread. */
928 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
932 * Wake the dispatch queue futex.
933 * Implicit memory barrier with the
934 * exchange in cds_wfcq_enqueue.
936 futex_nto1_wake(&relay_conn_queue
.futex
);
937 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
938 ERR("socket poll error");
941 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
951 lttng_poll_clean(&events
);
953 if (data_sock
->fd
>= 0) {
954 ret
= data_sock
->ops
->close(data_sock
);
959 lttcomm_destroy_sock(data_sock
);
961 if (control_sock
->fd
>= 0) {
962 ret
= control_sock
->ops
->close(control_sock
);
967 lttcomm_destroy_sock(control_sock
);
971 ERR("Health error occurred in %s", __func__
);
973 health_unregister(health_relayd
);
974 DBG("Relay listener thread cleanup complete");
975 lttng_relay_stop_threads();
980 * This thread manages the dispatching of the requests to worker threads
982 static void *relay_thread_dispatcher(void *data
)
986 struct cds_wfcq_node
*node
;
987 struct relay_connection
*new_conn
= NULL
;
989 DBG("[thread] Relay dispatcher started");
991 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
993 if (testpoint(relayd_thread_dispatcher
)) {
994 goto error_testpoint
;
997 health_code_update();
1000 health_code_update();
1002 /* Atomically prepare the queue futex */
1003 futex_nto1_prepare(&relay_conn_queue
.futex
);
1005 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1010 health_code_update();
1012 /* Dequeue commands */
1013 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1014 &relay_conn_queue
.tail
);
1016 DBG("Woken up but nothing in the relay command queue");
1017 /* Continue thread execution */
1020 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1022 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1025 * Inform worker thread of the new request. This
1026 * call is blocking so we can be assured that
1027 * the data will be read at some point in time
1028 * or wait to the end of the world :)
1030 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1032 PERROR("write connection pipe");
1033 connection_put(new_conn
);
1036 } while (node
!= NULL
);
1038 /* Futex wait on queue. Blocking call on futex() */
1039 health_poll_entry();
1040 futex_nto1_wait(&relay_conn_queue
.futex
);
1044 /* Normal exit, no error */
1051 ERR("Health error occurred in %s", __func__
);
1053 health_unregister(health_relayd
);
1054 DBG("Dispatch thread dying");
1055 lttng_relay_stop_threads();
1060 * Set index data from the control port to a given index object.
1062 static int set_index_control_data(struct relay_index
*index
,
1063 struct lttcomm_relayd_index
*data
,
1064 struct relay_connection
*conn
)
1066 struct ctf_packet_index index_data
;
1069 * The index on disk is encoded in big endian.
1071 index_data
.packet_size
= htobe64(data
->packet_size
);
1072 index_data
.content_size
= htobe64(data
->content_size
);
1073 index_data
.timestamp_begin
= htobe64(data
->timestamp_begin
);
1074 index_data
.timestamp_end
= htobe64(data
->timestamp_end
);
1075 index_data
.events_discarded
= htobe64(data
->events_discarded
);
1076 index_data
.stream_id
= htobe64(data
->stream_id
);
1078 if (conn
->minor
>= 8) {
1079 index
->index_data
.stream_instance_id
= htobe64(data
->stream_instance_id
);
1080 index
->index_data
.packet_seq_num
= htobe64(data
->packet_seq_num
);
1083 return relay_index_set_data(index
, &index_data
);
1087 * Handle the RELAYD_CREATE_SESSION command.
1089 * On success, send back the session id or else return a negative value.
1091 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1092 struct relay_connection
*conn
,
1093 const struct lttng_buffer_view
*payload
)
1097 struct relay_session
*session
;
1098 struct lttcomm_relayd_status_session reply
;
1099 char session_name
[LTTNG_NAME_MAX
];
1100 char hostname
[LTTNG_HOST_NAME_MAX
];
1101 uint32_t live_timer
= 0;
1102 bool snapshot
= false;
1104 memset(session_name
, 0, LTTNG_NAME_MAX
);
1105 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1107 memset(&reply
, 0, sizeof(reply
));
1109 if (conn
->minor
< 4) {
1110 /* From 2.1 to 2.3 */
1112 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1113 /* From 2.4 to 2.10 */
1114 ret
= cmd_create_session_2_4(payload
, session_name
,
1115 hostname
, &live_timer
, &snapshot
);
1117 /* From 2.11 to ... */
1118 ret
= cmd_create_session_2_11(payload
, session_name
,
1119 hostname
, &live_timer
, &snapshot
);
1126 session
= session_create(session_name
, hostname
, live_timer
,
1127 snapshot
, conn
->major
, conn
->minor
);
1132 assert(!conn
->session
);
1133 conn
->session
= session
;
1134 DBG("Created session %" PRIu64
, session
->id
);
1136 reply
.session_id
= htobe64(session
->id
);
1140 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1142 reply
.ret_code
= htobe32(LTTNG_OK
);
1145 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1146 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1147 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1156 * When we have received all the streams and the metadata for a channel,
1157 * we make them visible to the viewer threads.
1159 static void publish_connection_local_streams(struct relay_connection
*conn
)
1161 struct relay_stream
*stream
;
1162 struct relay_session
*session
= conn
->session
;
1165 * We publish all streams belonging to a session atomically wrt
1168 pthread_mutex_lock(&session
->lock
);
1170 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1172 stream_publish(stream
);
1177 * Inform the viewer that there are new streams in the session.
1179 if (session
->viewer_attached
) {
1180 uatomic_set(&session
->new_streams
, 1);
1182 pthread_mutex_unlock(&session
->lock
);
1186 * relay_add_stream: allocate a new stream for a session
1188 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1189 struct relay_connection
*conn
,
1190 const struct lttng_buffer_view
*payload
)
1194 struct relay_session
*session
= conn
->session
;
1195 struct relay_stream
*stream
= NULL
;
1196 struct lttcomm_relayd_status_stream reply
;
1197 struct ctf_trace
*trace
= NULL
;
1198 uint64_t stream_handle
= -1ULL;
1199 char *path_name
= NULL
, *channel_name
= NULL
;
1200 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1201 struct relay_stream_chunk_id stream_chunk_id
= { 0 };
1203 if (!session
|| !conn
->version_check_done
) {
1204 ERR("Trying to add a stream before version check");
1206 goto end_no_session
;
1209 if (session
->minor
== 1) {
1211 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1213 } else if (session
->minor
> 1 && session
->minor
< 11) {
1214 /* From 2.2 to 2.10 */
1215 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1216 &channel_name
, &tracefile_size
, &tracefile_count
);
1218 /* From 2.11 to ... */
1219 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1220 &channel_name
, &tracefile_size
, &tracefile_count
,
1221 &stream_chunk_id
.value
);
1222 stream_chunk_id
.is_set
= true;
1229 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1233 /* This stream here has one reference on the trace. */
1235 pthread_mutex_lock(&last_relay_stream_id_lock
);
1236 stream_handle
= ++last_relay_stream_id
;
1237 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1239 /* We pass ownership of path_name and channel_name. */
1240 stream
= stream_create(trace
, stream_handle
, path_name
,
1241 channel_name
, tracefile_size
, tracefile_count
,
1244 channel_name
= NULL
;
1247 * Streams are the owners of their trace. Reference to trace is
1248 * kept within stream_create().
1250 ctf_trace_put(trace
);
1253 memset(&reply
, 0, sizeof(reply
));
1254 reply
.handle
= htobe64(stream_handle
);
1256 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1258 reply
.ret_code
= htobe32(LTTNG_OK
);
1261 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1262 sizeof(struct lttcomm_relayd_status_stream
), 0);
1263 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1264 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1276 * relay_close_stream: close a specific stream
1278 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1279 struct relay_connection
*conn
,
1280 const struct lttng_buffer_view
*payload
)
1284 struct relay_session
*session
= conn
->session
;
1285 struct lttcomm_relayd_close_stream stream_info
;
1286 struct lttcomm_relayd_generic_reply reply
;
1287 struct relay_stream
*stream
;
1289 DBG("Close stream received");
1291 if (!session
|| !conn
->version_check_done
) {
1292 ERR("Trying to close a stream before version check");
1294 goto end_no_session
;
1297 if (payload
->size
< sizeof(stream_info
)) {
1298 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1299 sizeof(stream_info
), payload
->size
);
1301 goto end_no_session
;
1303 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1304 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1305 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1307 stream
= stream_get_by_id(stream_info
.stream_id
);
1314 * Set last_net_seq_num before the close flag. Required by data
1317 pthread_mutex_lock(&stream
->lock
);
1318 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1319 pthread_mutex_unlock(&stream
->lock
);
1322 * This is one of the conditions which may trigger a stream close
1323 * with the others being:
1324 * 1) A close command is received for a stream
1325 * 2) The control connection owning the stream is closed
1326 * 3) We have received all of the stream's data _after_ a close
1329 try_stream_close(stream
);
1330 if (stream
->is_metadata
) {
1331 struct relay_viewer_stream
*vstream
;
1333 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1335 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1337 * Since all the metadata has been sent to the
1338 * viewer and that we have a request to close
1339 * its stream, we can safely teardown the
1340 * corresponding metadata viewer stream.
1342 viewer_stream_put(vstream
);
1344 /* Put local reference. */
1345 viewer_stream_put(vstream
);
1352 memset(&reply
, 0, sizeof(reply
));
1354 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1356 reply
.ret_code
= htobe32(LTTNG_OK
);
1358 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1359 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1360 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1361 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1371 * relay_reset_metadata: reset a metadata stream
1374 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1375 struct relay_connection
*conn
,
1376 const struct lttng_buffer_view
*payload
)
1380 struct relay_session
*session
= conn
->session
;
1381 struct lttcomm_relayd_reset_metadata stream_info
;
1382 struct lttcomm_relayd_generic_reply reply
;
1383 struct relay_stream
*stream
;
1385 DBG("Reset metadata received");
1387 if (!session
|| !conn
->version_check_done
) {
1388 ERR("Trying to reset a metadata stream before version check");
1390 goto end_no_session
;
1393 if (payload
->size
< sizeof(stream_info
)) {
1394 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1395 sizeof(stream_info
), payload
->size
);
1397 goto end_no_session
;
1399 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1400 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1401 stream_info
.version
= be64toh(stream_info
.version
);
1403 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1405 /* Unsupported for live sessions for now. */
1406 if (session
->live_timer
!= 0) {
1411 stream
= stream_get_by_id(stream_info
.stream_id
);
1416 pthread_mutex_lock(&stream
->lock
);
1417 if (!stream
->is_metadata
) {
1422 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1423 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1424 &stream
->stream_fd
->fd
);
1426 ERR("Failed to rotate metadata file %s of channel %s",
1427 stream
->path_name
, stream
->channel_name
);
1432 pthread_mutex_unlock(&stream
->lock
);
1436 memset(&reply
, 0, sizeof(reply
));
1438 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1440 reply
.ret_code
= htobe32(LTTNG_OK
);
1442 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1443 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1444 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1445 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1455 * relay_unknown_command: send -1 if received unknown command
1457 static void relay_unknown_command(struct relay_connection
*conn
)
1459 struct lttcomm_relayd_generic_reply reply
;
1462 memset(&reply
, 0, sizeof(reply
));
1463 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1464 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1465 if (send_ret
< sizeof(reply
)) {
1466 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1471 * relay_start: send an acknowledgment to the client to tell if we are
1472 * ready to receive data. We are ready if a session is established.
1474 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1475 struct relay_connection
*conn
,
1476 const struct lttng_buffer_view
*payload
)
1480 struct lttcomm_relayd_generic_reply reply
;
1481 struct relay_session
*session
= conn
->session
;
1484 DBG("Trying to start the streaming without a session established");
1485 ret
= htobe32(LTTNG_ERR_UNK
);
1488 memset(&reply
, 0, sizeof(reply
));
1489 reply
.ret_code
= htobe32(LTTNG_OK
);
1490 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1492 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1493 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1502 * Append padding to the file pointed by the file descriptor fd.
1504 static int write_padding_to_file(int fd
, uint32_t size
)
1513 zeros
= zmalloc(size
);
1514 if (zeros
== NULL
) {
1515 PERROR("zmalloc zeros for padding");
1520 ret
= lttng_write(fd
, zeros
, size
);
1522 PERROR("write padding to file");
1532 * Close the current index file if it is open, and create a new one.
1534 * Return 0 on success, -1 on error.
1537 int create_rotate_index_file(struct relay_stream
*stream
)
1540 uint32_t major
, minor
;
1542 /* Put ref on previous index_file. */
1543 if (stream
->index_file
) {
1544 lttng_index_file_put(stream
->index_file
);
1545 stream
->index_file
= NULL
;
1547 major
= stream
->trace
->session
->major
;
1548 minor
= stream
->trace
->session
->minor
;
1549 stream
->index_file
= lttng_index_file_create(stream
->path_name
,
1550 stream
->channel_name
,
1551 -1, -1, stream
->tracefile_size
,
1552 tracefile_array_get_file_index_head(stream
->tfa
),
1553 lttng_to_index_major(major
, minor
),
1554 lttng_to_index_minor(major
, minor
));
1555 if (!stream
->index_file
) {
1567 int do_rotate_stream(struct relay_stream
*stream
)
1571 /* Perform the stream rotation. */
1572 ret
= utils_rotate_stream_file(stream
->path_name
,
1573 stream
->channel_name
, stream
->tracefile_size
,
1574 stream
->tracefile_count
, -1,
1575 -1, stream
->stream_fd
->fd
,
1576 NULL
, &stream
->stream_fd
->fd
);
1578 ERR("Rotating stream output file");
1581 stream
->tracefile_size_current
= 0;
1583 /* Rotate also the index if the stream is not a metadata stream. */
1584 if (!stream
->is_metadata
) {
1585 ret
= create_rotate_index_file(stream
);
1587 ERR("Failed to rotate index file");
1592 stream
->rotate_at_seq_num
= -1ULL;
1593 stream
->pos_after_last_complete_data_index
= 0;
1600 * If too much data has been written in a tracefile before we received the
1601 * rotation command, we have to move the excess data to the new tracefile and
1602 * perform the rotation. This can happen because the control and data
1603 * connections are separate, the indexes as well as the commands arrive from
1604 * the control connection and we have no control over the order so we could be
1605 * in a situation where too much data has been received on the data connection
1606 * before the rotation command on the control connection arrives. We don't need
1607 * to update the index because its order is guaranteed with the rotation
1611 int rotate_truncate_stream(struct relay_stream
*stream
)
1615 uint64_t diff
, pos
= 0;
1616 char buf
[FILE_COPY_BUFFER_SIZE
];
1618 assert(!stream
->is_metadata
);
1620 assert(stream
->tracefile_size_current
>
1621 stream
->pos_after_last_complete_data_index
);
1622 diff
= stream
->tracefile_size_current
-
1623 stream
->pos_after_last_complete_data_index
;
1625 /* Create the new tracefile. */
1626 new_fd
= utils_create_stream_file(stream
->path_name
,
1627 stream
->channel_name
,
1628 stream
->tracefile_size
, stream
->tracefile_count
,
1629 /* uid */ -1, /* gid */ -1, /* suffix */ NULL
);
1631 ERR("Failed to create new stream file at path %s for channel %s",
1632 stream
->path_name
, stream
->channel_name
);
1638 * Rewind the current tracefile to the position at which the rotation
1639 * should have occured.
1641 lseek_ret
= lseek(stream
->stream_fd
->fd
,
1642 stream
->pos_after_last_complete_data_index
, SEEK_SET
);
1643 if (lseek_ret
< 0) {
1644 PERROR("seek truncate stream");
1649 /* Move data from the old file to the new file. */
1650 while (pos
< diff
) {
1651 uint64_t count
, bytes_left
;
1654 bytes_left
= diff
- pos
;
1655 count
= bytes_left
> sizeof(buf
) ? sizeof(buf
) : bytes_left
;
1656 assert(count
<= SIZE_MAX
);
1658 io_ret
= lttng_read(stream
->stream_fd
->fd
, buf
, count
);
1659 if (io_ret
< (ssize_t
) count
) {
1660 char error_string
[256];
1662 snprintf(error_string
, sizeof(error_string
),
1663 "Failed to read %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1664 count
, stream
->stream_fd
->fd
, io_ret
);
1666 PERROR("%s", error_string
);
1668 ERR("%s", error_string
);
1674 io_ret
= lttng_write(new_fd
, buf
, count
);
1675 if (io_ret
< (ssize_t
) count
) {
1676 char error_string
[256];
1678 snprintf(error_string
, sizeof(error_string
),
1679 "Failed to write %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1680 count
, new_fd
, io_ret
);
1682 PERROR("%s", error_string
);
1684 ERR("%s", error_string
);
1693 /* Truncate the file to get rid of the excess data. */
1694 ret
= ftruncate(stream
->stream_fd
->fd
,
1695 stream
->pos_after_last_complete_data_index
);
1697 PERROR("ftruncate");
1701 ret
= close(stream
->stream_fd
->fd
);
1703 PERROR("Closing tracefile");
1707 ret
= create_rotate_index_file(stream
);
1709 ERR("Rotate stream index file");
1714 * Update the offset and FD of all the eventual indexes created by the
1715 * data connection before the rotation command arrived.
1717 ret
= relay_index_switch_all_files(stream
);
1719 ERR("Failed to rotate index file");
1723 stream
->stream_fd
->fd
= new_fd
;
1724 stream
->tracefile_size_current
= diff
;
1725 stream
->pos_after_last_complete_data_index
= 0;
1726 stream
->rotate_at_seq_num
= -1ULL;
1735 * Check if a stream should perform a rotation (for session rotation).
1736 * Must be called with the stream lock held.
1738 * Return 0 on success, a negative value on error.
1741 int try_rotate_stream(struct relay_stream
*stream
)
1745 /* No rotation expected. */
1746 if (stream
->rotate_at_seq_num
== -1ULL) {
1750 if (stream
->prev_seq
< stream
->rotate_at_seq_num
||
1751 stream
->prev_seq
== -1ULL) {
1752 DBG("Stream %" PRIu64
" no yet ready for rotation",
1753 stream
->stream_handle
);
1755 } else if (stream
->prev_seq
> stream
->rotate_at_seq_num
) {
1756 DBG("Rotation after too much data has been written in tracefile "
1757 "for stream %" PRIu64
", need to truncate before "
1758 "rotating", stream
->stream_handle
);
1759 ret
= rotate_truncate_stream(stream
);
1761 ERR("Failed to truncate stream");
1765 /* stream->prev_seq == stream->rotate_at_seq_num */
1766 DBG("Stream %" PRIu64
" ready for rotation",
1767 stream
->stream_handle
);
1768 ret
= do_rotate_stream(stream
);
1776 * relay_recv_metadata: receive the metadata for the session.
1778 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1779 struct relay_connection
*conn
,
1780 const struct lttng_buffer_view
*payload
)
1784 struct relay_session
*session
= conn
->session
;
1785 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1786 struct relay_stream
*metadata_stream
;
1787 uint64_t metadata_payload_size
;
1790 ERR("Metadata sent before version check");
1795 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1796 ERR("Incorrect data size");
1800 metadata_payload_size
= recv_hdr
->data_size
-
1801 sizeof(struct lttcomm_relayd_metadata_payload
);
1803 memcpy(&metadata_payload_header
, payload
->data
,
1804 sizeof(metadata_payload_header
));
1805 metadata_payload_header
.stream_id
= be64toh(
1806 metadata_payload_header
.stream_id
);
1807 metadata_payload_header
.padding_size
= be32toh(
1808 metadata_payload_header
.padding_size
);
1810 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1811 if (!metadata_stream
) {
1816 pthread_mutex_lock(&metadata_stream
->lock
);
1818 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
,
1819 payload
->data
+ sizeof(metadata_payload_header
),
1820 metadata_payload_size
);
1821 if (size_ret
< metadata_payload_size
) {
1822 ERR("Relay error writing metadata on file");
1827 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1828 metadata_payload_header
.padding_size
);
1829 if (size_ret
< (int64_t) metadata_payload_header
.padding_size
) {
1834 metadata_stream
->metadata_received
+=
1835 metadata_payload_size
+ metadata_payload_header
.padding_size
;
1836 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1837 metadata_stream
->metadata_received
);
1839 ret
= try_rotate_stream(metadata_stream
);
1845 pthread_mutex_unlock(&metadata_stream
->lock
);
1846 stream_put(metadata_stream
);
1852 * relay_send_version: send relayd version number
1854 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1855 struct relay_connection
*conn
,
1856 const struct lttng_buffer_view
*payload
)
1860 struct lttcomm_relayd_version reply
, msg
;
1861 bool compatible
= true;
1863 conn
->version_check_done
= true;
1865 /* Get version from the other side. */
1866 if (payload
->size
< sizeof(msg
)) {
1867 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1868 sizeof(msg
), payload
->size
);
1873 memcpy(&msg
, payload
->data
, sizeof(msg
));
1874 msg
.major
= be32toh(msg
.major
);
1875 msg
.minor
= be32toh(msg
.minor
);
1877 memset(&reply
, 0, sizeof(reply
));
1878 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1879 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1881 /* Major versions must be the same */
1882 if (reply
.major
!= msg
.major
) {
1883 DBG("Incompatible major versions (%u vs %u), deleting session",
1884 reply
.major
, msg
.major
);
1888 conn
->major
= reply
.major
;
1889 /* We adapt to the lowest compatible version */
1890 if (reply
.minor
<= msg
.minor
) {
1891 conn
->minor
= reply
.minor
;
1893 conn
->minor
= msg
.minor
;
1896 reply
.major
= htobe32(reply
.major
);
1897 reply
.minor
= htobe32(reply
.minor
);
1898 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1900 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1901 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1914 DBG("Version check done using protocol %u.%u", conn
->major
,
1922 * Check for data pending for a given stream id from the session daemon.
1924 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1925 struct relay_connection
*conn
,
1926 const struct lttng_buffer_view
*payload
)
1928 struct relay_session
*session
= conn
->session
;
1929 struct lttcomm_relayd_data_pending msg
;
1930 struct lttcomm_relayd_generic_reply reply
;
1931 struct relay_stream
*stream
;
1935 DBG("Data pending command received");
1937 if (!session
|| !conn
->version_check_done
) {
1938 ERR("Trying to check for data before version check");
1940 goto end_no_session
;
1943 if (payload
->size
< sizeof(msg
)) {
1944 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1945 sizeof(msg
), payload
->size
);
1947 goto end_no_session
;
1949 memcpy(&msg
, payload
->data
, sizeof(msg
));
1950 msg
.stream_id
= be64toh(msg
.stream_id
);
1951 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1953 stream
= stream_get_by_id(msg
.stream_id
);
1954 if (stream
== NULL
) {
1959 pthread_mutex_lock(&stream
->lock
);
1961 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1962 " and last_seq %" PRIu64
, msg
.stream_id
,
1963 stream
->prev_seq
, msg
.last_net_seq_num
);
1965 /* Avoid wrapping issue */
1966 if (((int64_t) (stream
->prev_seq
- msg
.last_net_seq_num
)) >= 0) {
1967 /* Data has in fact been written and is NOT pending */
1970 /* Data still being streamed thus pending */
1974 stream
->data_pending_check_done
= true;
1975 pthread_mutex_unlock(&stream
->lock
);
1980 memset(&reply
, 0, sizeof(reply
));
1981 reply
.ret_code
= htobe32(ret
);
1982 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1983 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1984 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1994 * Wait for the control socket to reach a quiescent state.
1996 * Note that for now, when receiving this command from the session
1997 * daemon, this means that every subsequent commands or data received on
1998 * the control socket has been handled. So, this is why we simply return
2001 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2002 struct relay_connection
*conn
,
2003 const struct lttng_buffer_view
*payload
)
2007 struct relay_stream
*stream
;
2008 struct lttcomm_relayd_quiescent_control msg
;
2009 struct lttcomm_relayd_generic_reply reply
;
2011 DBG("Checking quiescent state on control socket");
2013 if (!conn
->session
|| !conn
->version_check_done
) {
2014 ERR("Trying to check for data before version check");
2016 goto end_no_session
;
2019 if (payload
->size
< sizeof(msg
)) {
2020 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2021 sizeof(msg
), payload
->size
);
2023 goto end_no_session
;
2025 memcpy(&msg
, payload
->data
, sizeof(msg
));
2026 msg
.stream_id
= be64toh(msg
.stream_id
);
2028 stream
= stream_get_by_id(msg
.stream_id
);
2032 pthread_mutex_lock(&stream
->lock
);
2033 stream
->data_pending_check_done
= true;
2034 pthread_mutex_unlock(&stream
->lock
);
2036 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2039 memset(&reply
, 0, sizeof(reply
));
2040 reply
.ret_code
= htobe32(LTTNG_OK
);
2041 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2042 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2043 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2055 * Initialize a data pending command. This means that a consumer is about
2056 * to ask for data pending for each stream it holds. Simply iterate over
2057 * all streams of a session and set the data_pending_check_done flag.
2059 * This command returns to the client a LTTNG_OK code.
2061 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2062 struct relay_connection
*conn
,
2063 const struct lttng_buffer_view
*payload
)
2067 struct lttng_ht_iter iter
;
2068 struct lttcomm_relayd_begin_data_pending msg
;
2069 struct lttcomm_relayd_generic_reply reply
;
2070 struct relay_stream
*stream
;
2075 DBG("Init streams for data pending");
2077 if (!conn
->session
|| !conn
->version_check_done
) {
2078 ERR("Trying to check for data before version check");
2080 goto end_no_session
;
2083 if (payload
->size
< sizeof(msg
)) {
2084 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2085 sizeof(msg
), payload
->size
);
2087 goto end_no_session
;
2089 memcpy(&msg
, payload
->data
, sizeof(msg
));
2090 msg
.session_id
= be64toh(msg
.session_id
);
2093 * Iterate over all streams to set the begin data pending flag.
2094 * For now, the streams are indexed by stream handle so we have
2095 * to iterate over all streams to find the one associated with
2096 * the right session_id.
2099 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2101 if (!stream_get(stream
)) {
2104 if (stream
->trace
->session
->id
== msg
.session_id
) {
2105 pthread_mutex_lock(&stream
->lock
);
2106 stream
->data_pending_check_done
= false;
2107 pthread_mutex_unlock(&stream
->lock
);
2108 DBG("Set begin data pending flag to stream %" PRIu64
,
2109 stream
->stream_handle
);
2115 memset(&reply
, 0, sizeof(reply
));
2116 /* All good, send back reply. */
2117 reply
.ret_code
= htobe32(LTTNG_OK
);
2119 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2120 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2121 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2133 * End data pending command. This will check, for a given session id, if
2134 * each stream associated with it has its data_pending_check_done flag
2135 * set. If not, this means that the client lost track of the stream but
2136 * the data is still being streamed on our side. In this case, we inform
2137 * the client that data is in flight.
2139 * Return to the client if there is data in flight or not with a ret_code.
2141 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2142 struct relay_connection
*conn
,
2143 const struct lttng_buffer_view
*payload
)
2147 struct lttng_ht_iter iter
;
2148 struct lttcomm_relayd_end_data_pending msg
;
2149 struct lttcomm_relayd_generic_reply reply
;
2150 struct relay_stream
*stream
;
2151 uint32_t is_data_inflight
= 0;
2153 DBG("End data pending command");
2155 if (!conn
->session
|| !conn
->version_check_done
) {
2156 ERR("Trying to check for data before version check");
2158 goto end_no_session
;
2161 if (payload
->size
< sizeof(msg
)) {
2162 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2163 sizeof(msg
), payload
->size
);
2165 goto end_no_session
;
2167 memcpy(&msg
, payload
->data
, sizeof(msg
));
2168 msg
.session_id
= be64toh(msg
.session_id
);
2171 * Iterate over all streams to see if the begin data pending
2175 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2177 if (!stream_get(stream
)) {
2180 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2184 pthread_mutex_lock(&stream
->lock
);
2185 if (!stream
->data_pending_check_done
) {
2186 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
2187 is_data_inflight
= 1;
2188 DBG("Data is still in flight for stream %" PRIu64
,
2189 stream
->stream_handle
);
2190 pthread_mutex_unlock(&stream
->lock
);
2195 pthread_mutex_unlock(&stream
->lock
);
2200 memset(&reply
, 0, sizeof(reply
));
2201 /* All good, send back reply. */
2202 reply
.ret_code
= htobe32(is_data_inflight
);
2204 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2205 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2206 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2218 * Receive an index for a specific stream.
2220 * Return 0 on success else a negative value.
2222 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2223 struct relay_connection
*conn
,
2224 const struct lttng_buffer_view
*payload
)
2228 struct relay_session
*session
= conn
->session
;
2229 struct lttcomm_relayd_index index_info
;
2230 struct relay_index
*index
;
2231 struct lttcomm_relayd_generic_reply reply
;
2232 struct relay_stream
*stream
;
2237 DBG("Relay receiving index");
2239 if (!session
|| !conn
->version_check_done
) {
2240 ERR("Trying to close a stream before version check");
2242 goto end_no_session
;
2245 msg_len
= lttcomm_relayd_index_len(
2246 lttng_to_index_major(conn
->major
, conn
->minor
),
2247 lttng_to_index_minor(conn
->major
, conn
->minor
));
2248 if (payload
->size
< msg_len
) {
2249 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2250 msg_len
, payload
->size
);
2252 goto end_no_session
;
2254 memcpy(&index_info
, payload
->data
, msg_len
);
2255 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2256 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2257 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2258 index_info
.content_size
= be64toh(index_info
.content_size
);
2259 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2260 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2261 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2262 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2264 if (conn
->minor
>= 8) {
2265 index_info
.stream_instance_id
=
2266 be64toh(index_info
.stream_instance_id
);
2267 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2270 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2272 ERR("stream_get_by_id not found");
2276 pthread_mutex_lock(&stream
->lock
);
2278 /* Live beacon handling */
2279 if (index_info
.packet_size
== 0) {
2280 DBG("Received live beacon for stream %" PRIu64
,
2281 stream
->stream_handle
);
2284 * Only flag a stream inactive when it has already
2285 * received data and no indexes are in flight.
2287 if (stream
->index_received_seqcount
> 0
2288 && stream
->indexes_in_flight
== 0) {
2289 stream
->beacon_ts_end
= index_info
.timestamp_end
;
2292 goto end_stream_put
;
2294 stream
->beacon_ts_end
= -1ULL;
2297 if (stream
->ctf_stream_id
== -1ULL) {
2298 stream
->ctf_stream_id
= index_info
.stream_id
;
2300 index
= relay_index_get_by_id_or_create(stream
, index_info
.net_seq_num
);
2303 ERR("relay_index_get_by_id_or_create index NULL");
2304 goto end_stream_put
;
2306 if (set_index_control_data(index
, &index_info
, conn
)) {
2307 ERR("set_index_control_data error");
2308 relay_index_put(index
);
2310 goto end_stream_put
;
2312 ret
= relay_index_try_flush(index
);
2314 tracefile_array_commit_seq(stream
->tfa
);
2315 stream
->index_received_seqcount
++;
2316 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
2317 } else if (ret
> 0) {
2321 ERR("relay_index_try_flush error %d", ret
);
2322 relay_index_put(index
);
2327 pthread_mutex_unlock(&stream
->lock
);
2332 memset(&reply
, 0, sizeof(reply
));
2334 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2336 reply
.ret_code
= htobe32(LTTNG_OK
);
2338 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2339 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2340 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2349 * Receive the streams_sent message.
2351 * Return 0 on success else a negative value.
2353 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2354 struct relay_connection
*conn
,
2355 const struct lttng_buffer_view
*payload
)
2359 struct lttcomm_relayd_generic_reply reply
;
2363 DBG("Relay receiving streams_sent");
2365 if (!conn
->session
|| !conn
->version_check_done
) {
2366 ERR("Trying to close a stream before version check");
2368 goto end_no_session
;
2372 * Publish every pending stream in the connection recv list which are
2373 * now ready to be used by the viewer.
2375 publish_connection_local_streams(conn
);
2377 memset(&reply
, 0, sizeof(reply
));
2378 reply
.ret_code
= htobe32(LTTNG_OK
);
2379 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2380 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2381 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2394 * relay_rotate_session_stream: rotate a stream to a new tracefile for the session
2395 * rotation feature (not the tracefile rotation feature).
2397 static int relay_rotate_session_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
2398 struct relay_connection
*conn
,
2399 const struct lttng_buffer_view
*payload
)
2403 struct relay_session
*session
= conn
->session
;
2404 struct lttcomm_relayd_rotate_stream stream_info
;
2405 struct lttcomm_relayd_generic_reply reply
;
2406 struct relay_stream
*stream
;
2409 struct lttng_buffer_view new_path_view
;
2411 DBG("Rotate stream received");
2413 if (!session
|| !conn
->version_check_done
) {
2414 ERR("Trying to rotate a stream before version check");
2419 if (session
->major
== 2 && session
->minor
< 11) {
2420 ERR("Unsupported feature before 2.11");
2425 header_len
= sizeof(struct lttcomm_relayd_rotate_stream
);
2427 if (payload
->size
< header_len
) {
2428 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2429 header_len
, payload
->size
);
2434 memcpy(&stream_info
, payload
->data
, header_len
);
2436 /* Convert to host */
2437 stream_info
.pathname_length
= be32toh(stream_info
.pathname_length
);
2438 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
2439 stream_info
.new_chunk_id
= be64toh(stream_info
.new_chunk_id
);
2440 stream_info
.rotate_at_seq_num
= be64toh(stream_info
.rotate_at_seq_num
);
2442 path_len
= stream_info
.pathname_length
;
2443 if (payload
->size
< header_len
+ path_len
) {
2444 ERR("Unexpected payload size in \"relay_rotate_session_stream\" including path: expected >= %zu bytes, got %zu bytes",
2445 header_len
+ path_len
, payload
->size
);
2450 /* Ensure it fits in local filename length. */
2451 if (path_len
>= LTTNG_PATH_MAX
) {
2452 ret
= -ENAMETOOLONG
;
2453 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2454 path_len
, LTTNG_PATH_MAX
);
2458 new_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2459 stream_info
.pathname_length
);
2461 stream
= stream_get_by_id(stream_info
.stream_id
);
2467 pthread_mutex_lock(&stream
->lock
);
2470 * Update the trace path (just the folder, the stream name does not
2473 free(stream
->path_name
);
2474 stream
->path_name
= create_output_path(new_path_view
.data
);
2475 if (!stream
->path_name
) {
2476 ERR("Failed to create a new output path");
2478 goto end_stream_unlock
;
2480 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
,
2483 ERR("relay creating output directory");
2485 goto end_stream_unlock
;
2488 assert(stream
->current_chunk_id
.is_set
);
2489 stream
->current_chunk_id
.value
= stream_info
.new_chunk_id
;
2491 if (stream
->is_metadata
) {
2493 * The metadata stream is sent only over the control connection
2494 * so we know we have all the data to perform the stream
2497 ret
= do_rotate_stream(stream
);
2499 stream
->rotate_at_seq_num
= stream_info
.rotate_at_seq_num
;
2500 ret
= try_rotate_stream(stream
);
2503 goto end_stream_unlock
;
2507 pthread_mutex_unlock(&stream
->lock
);
2510 memset(&reply
, 0, sizeof(reply
));
2512 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2514 reply
.ret_code
= htobe32(LTTNG_OK
);
2516 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2517 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2518 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2519 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2529 * relay_mkdir: Create a folder on the disk.
2531 static int relay_mkdir(const struct lttcomm_relayd_hdr
*recv_hdr
,
2532 struct relay_connection
*conn
,
2533 const struct lttng_buffer_view
*payload
)
2536 struct relay_session
*session
= conn
->session
;
2537 struct lttcomm_relayd_mkdir path_info_header
;
2538 struct lttcomm_relayd_generic_reply reply
;
2542 struct lttng_buffer_view path_view
;
2544 if (!session
|| !conn
->version_check_done
) {
2545 ERR("Trying to create a directory before version check");
2547 goto end_no_session
;
2550 if (session
->major
== 2 && session
->minor
< 11) {
2552 * This client is not supposed to use this command since
2553 * it predates its introduction.
2555 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2557 goto end_no_session
;
2560 header_len
= sizeof(path_info_header
);
2561 if (payload
->size
< header_len
) {
2562 ERR("Unexpected payload size in \"relay_mkdir\": expected >= %zu bytes, got %zu bytes",
2563 header_len
, payload
->size
);
2565 goto end_no_session
;
2568 memcpy(&path_info_header
, payload
->data
, header_len
);
2570 path_info_header
.length
= be32toh(path_info_header
.length
);
2572 if (payload
->size
< header_len
+ path_info_header
.length
) {
2573 ERR("Unexpected payload size in \"relay_mkdir\" including path: expected >= %zu bytes, got %zu bytes",
2574 header_len
+ path_info_header
.length
, payload
->size
);
2576 goto end_no_session
;
2579 /* Ensure that it fits in local path length. */
2580 if (path_info_header
.length
>= LTTNG_PATH_MAX
) {
2581 ret
= -ENAMETOOLONG
;
2582 ERR("Path name argument of mkdir command (%" PRIu32
" bytes) exceeds the maximal length allowed (%d bytes)",
2583 path_info_header
.length
, LTTNG_PATH_MAX
);
2587 path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2588 path_info_header
.length
);
2590 path
= create_output_path(path_view
.data
);
2592 ERR("Failed to create output path");
2597 ret
= utils_mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, -1, -1);
2599 ERR("relay creating output directory");
2606 memset(&reply
, 0, sizeof(reply
));
2608 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2610 reply
.ret_code
= htobe32(LTTNG_OK
);
2612 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2613 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2614 ERR("Failed to send \"mkdir\" command reply (ret = %zd)", send_ret
);
2623 static int validate_rotate_rename_path_length(const char *path_type
,
2624 uint32_t path_length
)
2628 if (path_length
> LTTNG_PATH_MAX
) {
2629 ret
= -ENAMETOOLONG
;
2630 ERR("rotate rename \"%s\" path name length (%" PRIu32
" bytes) exceeds the allowed size of %i bytes",
2631 path_type
, path_length
, LTTNG_PATH_MAX
);
2632 } else if (path_length
== 0) {
2634 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type
);
2640 * relay_rotate_rename: rename the trace folder after a rotation is
2641 * completed. We are not closing any fd here, just moving the folder, so it
2642 * works even if data is still in-flight.
2644 static int relay_rotate_rename(const struct lttcomm_relayd_hdr
*recv_hdr
,
2645 struct relay_connection
*conn
,
2646 const struct lttng_buffer_view
*payload
)
2650 struct relay_session
*session
= conn
->session
;
2651 struct lttcomm_relayd_generic_reply reply
;
2652 struct lttcomm_relayd_rotate_rename header
;
2654 size_t received_paths_size
;
2655 char *complete_old_path
= NULL
, *complete_new_path
= NULL
;
2656 struct lttng_buffer_view old_path_view
;
2657 struct lttng_buffer_view new_path_view
;
2659 if (!session
|| !conn
->version_check_done
) {
2660 ERR("Trying to rename a trace folder before version check");
2665 if (session
->major
== 2 && session
->minor
< 11) {
2666 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2671 header_len
= sizeof(header
);
2672 if (payload
->size
< header_len
) {
2673 ERR("Unexpected payload size in \"relay_rotate_rename\": expected >= %zu bytes, got %zu bytes",
2674 header_len
, payload
->size
);
2679 memcpy(&header
, payload
->data
, header_len
);
2681 header
.old_path_length
= be32toh(header
.old_path_length
);
2682 header
.new_path_length
= be32toh(header
.new_path_length
);
2683 received_paths_size
= header
.old_path_length
+ header
.new_path_length
;
2685 if (payload
->size
< header_len
+ received_paths_size
) {
2686 ERR("Unexpected payload size in \"relay_rotate_rename\" including paths: expected >= %zu bytes, got %zu bytes",
2687 header_len
, payload
->size
);
2692 /* Ensure the paths don't exceed their allowed size. */
2693 ret
= validate_rotate_rename_path_length("old", header
.old_path_length
);
2697 ret
= validate_rotate_rename_path_length("new", header
.new_path_length
);
2702 old_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2703 header
.old_path_length
);
2704 new_path_view
= lttng_buffer_view_from_view(payload
,
2705 header_len
+ header
.old_path_length
,
2706 header
.new_path_length
);
2708 /* Validate that both paths received are NULL terminated. */
2709 if (old_path_view
.data
[old_path_view
.size
- 1] != '\0') {
2710 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2714 if (new_path_view
.data
[new_path_view
.size
- 1] != '\0') {
2715 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2720 complete_old_path
= create_output_path(old_path_view
.data
);
2721 if (!complete_old_path
) {
2722 ERR("Failed to build old output path in rotate_rename command");
2727 complete_new_path
= create_output_path(new_path_view
.data
);
2728 if (!complete_new_path
) {
2729 ERR("Failed to build new output path in rotate_rename command");
2734 ret
= utils_mkdir_recursive(complete_new_path
, S_IRWXU
| S_IRWXG
,
2737 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2743 * If a domain has not yet created its channel, the domain-specific
2744 * folder might not exist, but this is not an error.
2746 ret
= rename(complete_old_path
, complete_new_path
);
2747 if (ret
< 0 && errno
!= ENOENT
) {
2748 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2749 complete_old_path
, complete_new_path
);
2755 memset(&reply
, 0, sizeof(reply
));
2757 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2759 reply
.ret_code
= htobe32(LTTNG_OK
);
2761 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2763 if (send_ret
< sizeof(reply
)) {
2764 ERR("Failed to send \"rotate rename\" command reply (ret = %zd)",
2770 free(complete_old_path
);
2771 free(complete_new_path
);
2776 * Check if all the streams in the session have completed the last rotation.
2777 * The chunk_id value is used to distinguish the cases where a stream was
2778 * closed on the consumerd before the rotation started but it still active on
2779 * the relayd, and the case where a stream appeared on the consumerd/relayd
2780 * after the last rotation started (in that case, it is already writing in the
2781 * new chunk folder).
2784 int relay_rotate_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2785 struct relay_connection
*conn
,
2786 const struct lttng_buffer_view
*payload
)
2788 struct relay_session
*session
= conn
->session
;
2789 struct lttcomm_relayd_rotate_pending msg
;
2790 struct lttcomm_relayd_rotate_pending_reply reply
;
2791 struct lttng_ht_iter iter
;
2792 struct relay_stream
*stream
;
2796 bool rotate_pending
= false;
2798 DBG("Rotate pending command received");
2800 if (!session
|| !conn
->version_check_done
) {
2801 ERR("Trying to check for data before version check");
2806 if (session
->major
== 2 && session
->minor
< 11) {
2807 ERR("Unsupported feature before 2.11");
2812 if (payload
->size
< sizeof(msg
)) {
2813 ERR("Unexpected payload size in \"relay_rotate_pending\": expected >= %zu bytes, got %zu bytes",
2814 sizeof(msg
), payload
->size
);
2819 memcpy(&msg
, payload
->data
, sizeof(msg
));
2821 chunk_id
= be64toh(msg
.chunk_id
);
2823 DBG("Evaluating rotate pending for session \"%s\" and chunk id %" PRIu64
,
2824 session
->session_name
, chunk_id
);
2827 * Iterate over all the streams in the session and check if they are
2828 * still waiting for data to perform their rotation.
2831 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2833 if (!stream_get(stream
)) {
2836 if (stream
->trace
->session
!= session
) {
2840 pthread_mutex_lock(&stream
->lock
);
2841 if (stream
->rotate_at_seq_num
!= -1ULL) {
2842 /* We have not yet performed the rotation. */
2843 rotate_pending
= true;
2844 DBG("Stream %" PRIu64
" is still rotating",
2845 stream
->stream_handle
);
2846 } else if (stream
->current_chunk_id
.value
< chunk_id
) {
2848 * Stream closed on the consumer but still active on the
2851 rotate_pending
= true;
2852 DBG("Stream %" PRIu64
" did not exist on the consumer "
2853 "when the last rotation started, but is"
2854 "still waiting for data before getting"
2856 stream
->stream_handle
);
2858 pthread_mutex_unlock(&stream
->lock
);
2860 if (rotate_pending
) {
2867 memset(&reply
, 0, sizeof(reply
));
2868 reply
.generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
);
2869 reply
.is_pending
= (uint8_t) !!rotate_pending
;
2870 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2872 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2873 ERR("Failed to send \"rotate pending\" command reply (ret = %zd)",
2882 #define DBG_CMD(cmd_name, conn) \
2883 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2885 static int relay_process_control_command(struct relay_connection
*conn
,
2886 const struct lttcomm_relayd_hdr
*header
,
2887 const struct lttng_buffer_view
*payload
)
2891 switch (header
->cmd
) {
2892 case RELAYD_CREATE_SESSION
:
2893 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2894 ret
= relay_create_session(header
, conn
, payload
);
2896 case RELAYD_ADD_STREAM
:
2897 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2898 ret
= relay_add_stream(header
, conn
, payload
);
2900 case RELAYD_START_DATA
:
2901 DBG_CMD("RELAYD_START_DATA", conn
);
2902 ret
= relay_start(header
, conn
, payload
);
2904 case RELAYD_SEND_METADATA
:
2905 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2906 ret
= relay_recv_metadata(header
, conn
, payload
);
2908 case RELAYD_VERSION
:
2909 DBG_CMD("RELAYD_VERSION", conn
);
2910 ret
= relay_send_version(header
, conn
, payload
);
2912 case RELAYD_CLOSE_STREAM
:
2913 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2914 ret
= relay_close_stream(header
, conn
, payload
);
2916 case RELAYD_DATA_PENDING
:
2917 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2918 ret
= relay_data_pending(header
, conn
, payload
);
2920 case RELAYD_QUIESCENT_CONTROL
:
2921 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2922 ret
= relay_quiescent_control(header
, conn
, payload
);
2924 case RELAYD_BEGIN_DATA_PENDING
:
2925 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2926 ret
= relay_begin_data_pending(header
, conn
, payload
);
2928 case RELAYD_END_DATA_PENDING
:
2929 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2930 ret
= relay_end_data_pending(header
, conn
, payload
);
2932 case RELAYD_SEND_INDEX
:
2933 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2934 ret
= relay_recv_index(header
, conn
, payload
);
2936 case RELAYD_STREAMS_SENT
:
2937 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2938 ret
= relay_streams_sent(header
, conn
, payload
);
2940 case RELAYD_RESET_METADATA
:
2941 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2942 ret
= relay_reset_metadata(header
, conn
, payload
);
2944 case RELAYD_ROTATE_STREAM
:
2945 DBG_CMD("RELAYD_ROTATE_STREAM", conn
);
2946 ret
= relay_rotate_session_stream(header
, conn
, payload
);
2948 case RELAYD_ROTATE_RENAME
:
2949 DBG_CMD("RELAYD_ROTATE_RENAME", conn
);
2950 ret
= relay_rotate_rename(header
, conn
, payload
);
2952 case RELAYD_ROTATE_PENDING
:
2953 DBG_CMD("RELAYD_ROTATE_PENDING", conn
);
2954 ret
= relay_rotate_pending(header
, conn
, payload
);
2957 DBG_CMD("RELAYD_MKDIR", conn
);
2958 ret
= relay_mkdir(header
, conn
, payload
);
2960 case RELAYD_UPDATE_SYNC_INFO
:
2962 ERR("Received unknown command (%u)", header
->cmd
);
2963 relay_unknown_command(conn
);
2972 static enum relay_connection_status
relay_process_control_receive_payload(
2973 struct relay_connection
*conn
)
2976 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
2977 struct lttng_dynamic_buffer
*reception_buffer
=
2978 &conn
->protocol
.ctrl
.reception_buffer
;
2979 struct ctrl_connection_state_receive_payload
*state
=
2980 &conn
->protocol
.ctrl
.state
.receive_payload
;
2981 struct lttng_buffer_view payload_view
;
2983 if (state
->left_to_receive
== 0) {
2984 /* Short-circuit for payload-less commands. */
2985 goto reception_complete
;
2988 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2989 reception_buffer
->data
+ state
->received
,
2990 state
->left_to_receive
, MSG_DONTWAIT
);
2992 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2993 PERROR("Unable to receive command payload on sock %d",
2995 status
= RELAY_CONNECTION_STATUS_ERROR
;
2998 } else if (ret
== 0) {
2999 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3000 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3005 assert(ret
<= state
->left_to_receive
);
3007 state
->left_to_receive
-= ret
;
3008 state
->received
+= ret
;
3010 if (state
->left_to_receive
> 0) {
3012 * Can't transition to the protocol's next state, wait to
3013 * receive the rest of the header.
3015 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3016 state
->received
, state
->left_to_receive
,
3022 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3023 conn
->sock
->fd
, state
->received
);
3025 * The payload required to process the command has been received.
3026 * A view to the reception buffer is forwarded to the various
3027 * commands and the state of the control is reset on success.
3029 * Commands are responsible for sending their reply to the peer.
3031 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3033 ret
= relay_process_control_command(conn
,
3034 &state
->header
, &payload_view
);
3036 status
= RELAY_CONNECTION_STATUS_ERROR
;
3040 ret
= connection_reset_protocol_state(conn
);
3042 status
= RELAY_CONNECTION_STATUS_ERROR
;
3048 static enum relay_connection_status
relay_process_control_receive_header(
3049 struct relay_connection
*conn
)
3052 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3053 struct lttcomm_relayd_hdr header
;
3054 struct lttng_dynamic_buffer
*reception_buffer
=
3055 &conn
->protocol
.ctrl
.reception_buffer
;
3056 struct ctrl_connection_state_receive_header
*state
=
3057 &conn
->protocol
.ctrl
.state
.receive_header
;
3059 assert(state
->left_to_receive
!= 0);
3061 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3062 reception_buffer
->data
+ state
->received
,
3063 state
->left_to_receive
, MSG_DONTWAIT
);
3065 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3066 PERROR("Unable to receive control command header on sock %d",
3068 status
= RELAY_CONNECTION_STATUS_ERROR
;
3071 } else if (ret
== 0) {
3072 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3073 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3078 assert(ret
<= state
->left_to_receive
);
3080 state
->left_to_receive
-= ret
;
3081 state
->received
+= ret
;
3083 if (state
->left_to_receive
> 0) {
3085 * Can't transition to the protocol's next state, wait to
3086 * receive the rest of the header.
3088 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3089 state
->received
, state
->left_to_receive
,
3094 /* Transition to next state: receiving the command's payload. */
3095 conn
->protocol
.ctrl
.state_id
=
3096 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3097 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3098 header
.circuit_id
= be64toh(header
.circuit_id
);
3099 header
.data_size
= be64toh(header
.data_size
);
3100 header
.cmd
= be32toh(header
.cmd
);
3101 header
.cmd_version
= be32toh(header
.cmd_version
);
3102 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3103 &header
, sizeof(header
));
3105 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3106 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3109 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3110 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3112 status
= RELAY_CONNECTION_STATUS_ERROR
;
3116 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3118 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3119 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3122 status
= RELAY_CONNECTION_STATUS_ERROR
;
3126 if (header
.data_size
== 0) {
3128 * Manually invoke the next state as the poll loop
3129 * will not wake-up to allow us to proceed further.
3131 status
= relay_process_control_receive_payload(conn
);
3138 * Process the commands received on the control socket
3140 static enum relay_connection_status
relay_process_control(
3141 struct relay_connection
*conn
)
3143 enum relay_connection_status status
;
3145 switch (conn
->protocol
.ctrl
.state_id
) {
3146 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3147 status
= relay_process_control_receive_header(conn
);
3149 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3150 status
= relay_process_control_receive_payload(conn
);
3153 ERR("Unknown control connection protocol state encountered.");
3161 * Handle index for a data stream.
3163 * Called with the stream lock held.
3165 * Return 0 on success else a negative value.
3167 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
3168 bool rotate_index
, bool *flushed
, uint64_t total_size
)
3171 uint64_t data_offset
;
3172 struct relay_index
*index
;
3174 /* Get data offset because we are about to update the index. */
3175 data_offset
= htobe64(stream
->tracefile_size_current
);
3177 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
3178 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
3181 * Lookup for an existing index for that stream id/sequence
3182 * number. If it exists, the control thread has already received the
3183 * data for it, thus we need to write it to disk.
3185 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
3191 if (rotate_index
|| !stream
->index_file
) {
3192 ret
= create_rotate_index_file(stream
);
3194 ERR("Failed to rotate index");
3195 /* Put self-ref for this index due to error. */
3196 relay_index_put(index
);
3202 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
3204 /* Put self-ref for this index due to error. */
3205 relay_index_put(index
);
3210 ret
= relay_index_try_flush(index
);
3212 tracefile_array_commit_seq(stream
->tfa
);
3213 stream
->index_received_seqcount
++;
3215 } else if (ret
> 0) {
3216 index
->total_size
= total_size
;
3220 /* Put self-ref for this index due to error. */
3221 relay_index_put(index
);
3229 static enum relay_connection_status
relay_process_data_receive_header(
3230 struct relay_connection
*conn
)
3233 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3234 struct data_connection_state_receive_header
*state
=
3235 &conn
->protocol
.data
.state
.receive_header
;
3236 struct lttcomm_relayd_data_hdr header
;
3237 struct relay_stream
*stream
;
3239 assert(state
->left_to_receive
!= 0);
3241 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3242 state
->header_reception_buffer
+ state
->received
,
3243 state
->left_to_receive
, MSG_DONTWAIT
);
3245 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3246 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3247 status
= RELAY_CONNECTION_STATUS_ERROR
;
3250 } else if (ret
== 0) {
3251 /* Orderly shutdown. Not necessary to print an error. */
3252 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3253 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3258 assert(ret
<= state
->left_to_receive
);
3260 state
->left_to_receive
-= ret
;
3261 state
->received
+= ret
;
3263 if (state
->left_to_receive
> 0) {
3265 * Can't transition to the protocol's next state, wait to
3266 * receive the rest of the header.
3268 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3269 state
->received
, state
->left_to_receive
,
3274 /* Transition to next state: receiving the payload. */
3275 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3277 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3278 header
.circuit_id
= be64toh(header
.circuit_id
);
3279 header
.stream_id
= be64toh(header
.stream_id
);
3280 header
.data_size
= be32toh(header
.data_size
);
3281 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3282 header
.padding_size
= be32toh(header
.padding_size
);
3283 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3285 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3287 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3288 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3290 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3291 conn
->sock
->fd
, header
.circuit_id
,
3292 header
.stream_id
, header
.data_size
,
3293 header
.net_seq_num
, header
.padding_size
);
3295 stream
= stream_get_by_id(header
.stream_id
);
3297 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3299 /* Protocol error. */
3300 status
= RELAY_CONNECTION_STATUS_ERROR
;
3304 pthread_mutex_lock(&stream
->lock
);
3306 /* Check if a rotation is needed. */
3307 if (stream
->tracefile_size
> 0 &&
3308 (stream
->tracefile_size_current
+ header
.data_size
) >
3309 stream
->tracefile_size
) {
3310 uint64_t old_id
, new_id
;
3312 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
3313 tracefile_array_file_rotate(stream
->tfa
);
3315 /* new_id is updated by utils_rotate_stream_file. */
3318 ret
= utils_rotate_stream_file(stream
->path_name
,
3319 stream
->channel_name
, stream
->tracefile_size
,
3320 stream
->tracefile_count
, -1,
3321 -1, stream
->stream_fd
->fd
,
3322 &new_id
, &stream
->stream_fd
->fd
);
3324 ERR("Failed to rotate stream output file");
3325 status
= RELAY_CONNECTION_STATUS_ERROR
;
3326 goto end_stream_unlock
;
3330 * Reset current size because we just performed a stream
3333 stream
->tracefile_size_current
= 0;
3334 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= true;
3338 pthread_mutex_unlock(&stream
->lock
);
3344 static enum relay_connection_status
relay_process_data_receive_payload(
3345 struct relay_connection
*conn
)
3348 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3349 struct relay_stream
*stream
;
3350 struct data_connection_state_receive_payload
*state
=
3351 &conn
->protocol
.data
.state
.receive_payload
;
3352 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3353 char data_buffer
[chunk_size
];
3354 bool partial_recv
= false;
3355 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3356 uint64_t left_to_receive
= state
->left_to_receive
;
3357 struct relay_session
*session
;
3359 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3360 state
->header
.stream_id
, state
->header
.net_seq_num
,
3361 state
->received
, left_to_receive
);
3363 stream
= stream_get_by_id(state
->header
.stream_id
);
3365 /* Protocol error. */
3366 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3367 state
->header
.stream_id
);
3368 status
= RELAY_CONNECTION_STATUS_ERROR
;
3372 pthread_mutex_lock(&stream
->lock
);
3373 session
= stream
->trace
->session
;
3374 if (!conn
->session
) {
3375 ret
= connection_set_session(conn
, session
);
3377 status
= RELAY_CONNECTION_STATUS_ERROR
;
3378 goto end_stream_unlock
;
3383 * The size of the "chunk" received on any iteration is bounded by:
3384 * - the data left to receive,
3385 * - the data immediately available on the socket,
3386 * - the on-stack data buffer
3388 while (left_to_receive
> 0 && !partial_recv
) {
3390 size_t recv_size
= min(left_to_receive
, chunk_size
);
3392 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3393 recv_size
, MSG_DONTWAIT
);
3395 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3396 PERROR("Socket %d error", conn
->sock
->fd
);
3397 status
= RELAY_CONNECTION_STATUS_ERROR
;
3399 goto end_stream_unlock
;
3400 } else if (ret
== 0) {
3401 /* No more data ready to be consumed on socket. */
3402 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3403 state
->header
.stream_id
);
3404 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3406 } else if (ret
< (int) recv_size
) {
3408 * All the data available on the socket has been
3411 partial_recv
= true;
3416 /* Write data to stream output fd. */
3417 write_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
3419 if (write_ret
< (ssize_t
) recv_size
) {
3420 ERR("Relay error writing data to file");
3421 status
= RELAY_CONNECTION_STATUS_ERROR
;
3422 goto end_stream_unlock
;
3425 left_to_receive
-= recv_size
;
3426 state
->received
+= recv_size
;
3427 state
->left_to_receive
= left_to_receive
;
3429 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
3430 write_ret
, stream
->stream_handle
);
3433 if (state
->left_to_receive
> 0) {
3435 * Did not receive all the data expected, wait for more data to
3436 * become available on the socket.
3438 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3439 state
->header
.stream_id
, state
->received
,
3440 state
->left_to_receive
);
3441 goto end_stream_unlock
;
3444 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
3445 state
->header
.padding_size
);
3446 if ((int64_t) ret
< (int64_t) state
->header
.padding_size
) {
3447 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3448 stream
->stream_handle
,
3449 state
->header
.net_seq_num
, ret
);
3450 status
= RELAY_CONNECTION_STATUS_ERROR
;
3451 goto end_stream_unlock
;
3455 if (session
->minor
>= 4 && !session
->snapshot
) {
3456 ret
= handle_index_data(stream
, state
->header
.net_seq_num
,
3457 state
->rotate_index
, &index_flushed
, state
->header
.data_size
+ state
->header
.padding_size
);
3459 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3460 stream
->stream_handle
,
3461 state
->header
.net_seq_num
, ret
);
3462 status
= RELAY_CONNECTION_STATUS_ERROR
;
3463 goto end_stream_unlock
;
3467 stream
->tracefile_size_current
+= state
->header
.data_size
+
3468 state
->header
.padding_size
;
3470 if (stream
->prev_seq
== -1ULL) {
3473 if (index_flushed
) {
3474 stream
->pos_after_last_complete_data_index
=
3475 stream
->tracefile_size_current
;
3478 stream
->prev_seq
= state
->header
.net_seq_num
;
3481 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3482 * contents of *state which are aliased (union) to the same location as
3483 * the new state. Don't use it beyond this point.
3485 connection_reset_protocol_state(conn
);
3488 ret
= try_rotate_stream(stream
);
3490 status
= RELAY_CONNECTION_STATUS_ERROR
;
3491 goto end_stream_unlock
;
3495 close_requested
= stream
->close_requested
;
3496 pthread_mutex_unlock(&stream
->lock
);
3497 if (close_requested
&& left_to_receive
== 0) {
3498 try_stream_close(stream
);
3502 pthread_mutex_lock(&session
->lock
);
3503 uatomic_set(&session
->new_streams
, 1);
3504 pthread_mutex_unlock(&session
->lock
);
3513 * relay_process_data: Process the data received on the data socket
3515 static enum relay_connection_status
relay_process_data(
3516 struct relay_connection
*conn
)
3518 enum relay_connection_status status
;
3520 switch (conn
->protocol
.data
.state_id
) {
3521 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3522 status
= relay_process_data_receive_header(conn
);
3524 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3525 status
= relay_process_data_receive_payload(conn
);
3528 ERR("Unexpected data connection communication state.");
3535 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3539 (void) lttng_poll_del(events
, pollfd
);
3541 ret
= close(pollfd
);
3543 ERR("Closing pollfd %d", pollfd
);
3547 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3548 int pollfd
, struct relay_connection
*conn
)
3550 const char *type_str
;
3552 switch (conn
->type
) {
3557 type_str
= "Control";
3559 case RELAY_VIEWER_COMMAND
:
3560 type_str
= "Viewer Command";
3562 case RELAY_VIEWER_NOTIFICATION
:
3563 type_str
= "Viewer Notification";
3566 type_str
= "Unknown";
3568 cleanup_connection_pollfd(events
, pollfd
);
3569 connection_put(conn
);
3570 DBG("%s connection closed with %d", type_str
, pollfd
);
3574 * This thread does the actual work
3576 static void *relay_thread_worker(void *data
)
3578 int ret
, err
= -1, last_seen_data_fd
= -1;
3580 struct lttng_poll_event events
;
3581 struct lttng_ht
*relay_connections_ht
;
3582 struct lttng_ht_iter iter
;
3583 struct relay_connection
*destroy_conn
= NULL
;
3585 DBG("[thread] Relay worker started");
3587 rcu_register_thread();
3589 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3591 if (testpoint(relayd_thread_worker
)) {
3592 goto error_testpoint
;
3595 health_code_update();
3597 /* table of connections indexed on socket */
3598 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3599 if (!relay_connections_ht
) {
3600 goto relay_connections_ht_error
;
3603 ret
= create_thread_poll_set(&events
, 2);
3605 goto error_poll_create
;
3608 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3615 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3617 health_code_update();
3619 /* Infinite blocking call, waiting for transmission */
3620 DBG3("Relayd worker thread polling...");
3621 health_poll_entry();
3622 ret
= lttng_poll_wait(&events
, -1);
3626 * Restart interrupted system call.
3628 if (errno
== EINTR
) {
3637 * Process control. The control connection is
3638 * prioritized so we don't starve it with high
3639 * throughput tracing data on the data connection.
3641 for (i
= 0; i
< nb_fd
; i
++) {
3642 /* Fetch once the poll data */
3643 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3644 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3646 health_code_update();
3650 * No activity for this FD (poll
3656 /* Thread quit pipe has been closed. Killing thread. */
3657 ret
= check_thread_quit_pipe(pollfd
, revents
);
3663 /* Inspect the relay conn pipe for new connection */
3664 if (pollfd
== relay_conn_pipe
[0]) {
3665 if (revents
& LPOLLIN
) {
3666 struct relay_connection
*conn
;
3668 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3672 lttng_poll_add(&events
, conn
->sock
->fd
,
3673 LPOLLIN
| LPOLLRDHUP
);
3674 connection_ht_add(relay_connections_ht
, conn
);
3675 DBG("Connection socket %d added", conn
->sock
->fd
);
3676 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3677 ERR("Relay connection pipe error");
3680 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3684 struct relay_connection
*ctrl_conn
;
3686 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3687 /* If not found, there is a synchronization issue. */
3690 if (ctrl_conn
->type
== RELAY_DATA
) {
3691 if (revents
& LPOLLIN
) {
3693 * Flag the last seen data fd not deleted. It will be
3694 * used as the last seen fd if any fd gets deleted in
3697 last_notdel_data_fd
= pollfd
;
3699 goto put_ctrl_connection
;
3701 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3703 if (revents
& LPOLLIN
) {
3704 enum relay_connection_status status
;
3706 status
= relay_process_control(ctrl_conn
);
3707 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3709 * On socket error flag the session as aborted to force
3710 * the cleanup of its stream otherwise it can leak
3711 * during the lifetime of the relayd.
3713 * This prevents situations in which streams can be
3714 * left opened because an index was received, the
3715 * control connection is closed, and the data
3716 * connection is closed (uncleanly) before the packet's
3719 * Since the control connection encountered an error,
3720 * it is okay to be conservative and close the
3721 * session right now as we can't rely on the protocol
3722 * being respected anymore.
3724 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3725 session_abort(ctrl_conn
->session
);
3728 /* Clear the connection on error or close. */
3729 relay_thread_close_connection(&events
,
3734 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3735 relay_thread_close_connection(&events
,
3737 if (last_seen_data_fd
== pollfd
) {
3738 last_seen_data_fd
= last_notdel_data_fd
;
3741 ERR("Unexpected poll events %u for control sock %d",
3743 connection_put(ctrl_conn
);
3746 put_ctrl_connection
:
3747 connection_put(ctrl_conn
);
3752 * The last loop handled a control request, go back to poll to make
3753 * sure we prioritise the control socket.
3759 if (last_seen_data_fd
>= 0) {
3760 for (i
= 0; i
< nb_fd
; i
++) {
3761 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3763 health_code_update();
3765 if (last_seen_data_fd
== pollfd
) {
3772 /* Process data connection. */
3773 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3774 /* Fetch the poll data. */
3775 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3776 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3777 struct relay_connection
*data_conn
;
3779 health_code_update();
3782 /* No activity for this FD (poll implementation). */
3786 /* Skip the command pipe. It's handled in the first loop. */
3787 if (pollfd
== relay_conn_pipe
[0]) {
3791 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3793 /* Skip it. Might be removed before. */
3796 if (data_conn
->type
== RELAY_CONTROL
) {
3797 goto put_data_connection
;
3799 assert(data_conn
->type
== RELAY_DATA
);
3801 if (revents
& LPOLLIN
) {
3802 enum relay_connection_status status
;
3804 status
= relay_process_data(data_conn
);
3805 /* Connection closed or error. */
3806 if (status
!= RELAY_CONNECTION_STATUS_OK
) {
3808 * On socket error flag the session as aborted to force
3809 * the cleanup of its stream otherwise it can leak
3810 * during the lifetime of the relayd.
3812 * This prevents situations in which streams can be
3813 * left opened because an index was received, the
3814 * control connection is closed, and the data
3815 * connection is closed (uncleanly) before the packet's
3818 * Since the data connection encountered an error,
3819 * it is okay to be conservative and close the
3820 * session right now as we can't rely on the protocol
3821 * being respected anymore.
3823 if (status
== RELAY_CONNECTION_STATUS_ERROR
) {
3824 session_abort(data_conn
->session
);
3826 relay_thread_close_connection(&events
, pollfd
,
3829 * Every goto restart call sets the last seen fd where
3830 * here we don't really care since we gracefully
3831 * continue the loop after the connection is deleted.
3834 /* Keep last seen port. */
3835 last_seen_data_fd
= pollfd
;
3836 connection_put(data_conn
);
3839 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3840 relay_thread_close_connection(&events
, pollfd
,
3843 ERR("Unknown poll events %u for data sock %d",
3846 put_data_connection
:
3847 connection_put(data_conn
);
3849 last_seen_data_fd
= -1;
3852 /* Normal exit, no error */
3857 /* Cleanup remaining connection object. */
3859 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3862 health_code_update();
3864 session_abort(destroy_conn
->session
);
3867 * No need to grab another ref, because we own
3870 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3875 lttng_poll_clean(&events
);
3877 lttng_ht_destroy(relay_connections_ht
);
3878 relay_connections_ht_error
:
3879 /* Close relay conn pipes */
3880 utils_close_pipe(relay_conn_pipe
);
3882 DBG("Thread exited with error");
3884 DBG("Worker thread cleanup complete");
3888 ERR("Health error occurred in %s", __func__
);
3890 health_unregister(health_relayd
);
3891 rcu_unregister_thread();
3892 lttng_relay_stop_threads();
3897 * Create the relay command pipe to wake thread_manage_apps.
3898 * Closed in cleanup().
3900 static int create_relay_conn_pipe(void)
3904 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3912 int main(int argc
, char **argv
)
3914 int ret
= 0, retval
= 0;
3917 /* Parse arguments */
3919 if (set_options(argc
, argv
)) {
3924 if (set_signal_handler()) {
3929 /* Try to create directory if -o, --output is specified. */
3930 if (opt_output_path
) {
3931 if (*opt_output_path
!= '/') {
3932 ERR("Please specify an absolute path for -o, --output PATH");
3937 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3940 ERR("Unable to create %s", opt_output_path
);
3947 if (opt_daemon
|| opt_background
) {
3950 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3958 * We are in the child. Make sure all other file
3959 * descriptors are closed, in case we are called with
3960 * more opened file descriptors than the standard ones.
3962 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3967 /* Initialize thread health monitoring */
3968 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3969 if (!health_relayd
) {
3970 PERROR("health_app_create error");
3972 goto exit_health_app_create
;
3975 /* Create thread quit pipe */
3976 if (init_thread_quit_pipe()) {
3978 goto exit_init_data
;
3981 /* Setup the thread apps communication pipe. */
3982 if (create_relay_conn_pipe()) {
3984 goto exit_init_data
;
3987 /* Init relay command queue. */
3988 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3990 /* Initialize communication library */
3992 lttcomm_inet_init();
3994 /* tables of sessions indexed by session ID */
3995 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3998 goto exit_init_data
;
4001 /* tables of streams indexed by stream ID */
4002 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4003 if (!relay_streams_ht
) {
4005 goto exit_init_data
;
4008 /* tables of streams indexed by stream ID */
4009 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4010 if (!viewer_streams_ht
) {
4012 goto exit_init_data
;
4015 ret
= utils_create_pipe(health_quit_pipe
);
4018 goto exit_health_quit_pipe
;
4021 /* Create thread to manage the client socket */
4022 ret
= pthread_create(&health_thread
, default_pthread_attr(),
4023 thread_manage_health
, (void *) NULL
);
4026 PERROR("pthread_create health");
4028 goto exit_health_thread
;
4031 /* Setup the dispatcher thread */
4032 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
4033 relay_thread_dispatcher
, (void *) NULL
);
4036 PERROR("pthread_create dispatcher");
4038 goto exit_dispatcher_thread
;
4041 /* Setup the worker thread */
4042 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
4043 relay_thread_worker
, NULL
);
4046 PERROR("pthread_create worker");
4048 goto exit_worker_thread
;
4051 /* Setup the listener thread */
4052 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
4053 relay_thread_listener
, (void *) NULL
);
4056 PERROR("pthread_create listener");
4058 goto exit_listener_thread
;
4061 ret
= relayd_live_create(live_uri
);
4063 ERR("Starting live viewer threads");
4069 * This is where we start awaiting program completion (e.g. through
4070 * signal that asks threads to teardown).
4073 ret
= relayd_live_join();
4079 ret
= pthread_join(listener_thread
, &status
);
4082 PERROR("pthread_join listener_thread");
4086 exit_listener_thread
:
4087 ret
= pthread_join(worker_thread
, &status
);
4090 PERROR("pthread_join worker_thread");
4095 ret
= pthread_join(dispatcher_thread
, &status
);
4098 PERROR("pthread_join dispatcher_thread");
4101 exit_dispatcher_thread
:
4103 ret
= pthread_join(health_thread
, &status
);
4106 PERROR("pthread_join health_thread");
4111 utils_close_pipe(health_quit_pipe
);
4112 exit_health_quit_pipe
:
4115 health_app_destroy(health_relayd
);
4116 exit_health_app_create
:
4119 * Wait for all pending call_rcu work to complete before tearing
4120 * down data structures. call_rcu worker may be trying to
4121 * perform lookups in those structures.
4126 /* Ensure all prior call_rcu are done. */