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
);
1086 static bool session_streams_have_index(const struct relay_session
*session
)
1088 return session
->minor
>= 4 && !session
->snapshot
;
1092 * Handle the RELAYD_CREATE_SESSION command.
1094 * On success, send back the session id or else return a negative value.
1096 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1097 struct relay_connection
*conn
,
1098 const struct lttng_buffer_view
*payload
)
1102 struct relay_session
*session
;
1103 struct lttcomm_relayd_status_session reply
;
1104 char session_name
[LTTNG_NAME_MAX
];
1105 char hostname
[LTTNG_HOST_NAME_MAX
];
1106 uint32_t live_timer
= 0;
1107 bool snapshot
= false;
1109 memset(session_name
, 0, LTTNG_NAME_MAX
);
1110 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1112 memset(&reply
, 0, sizeof(reply
));
1114 if (conn
->minor
< 4) {
1115 /* From 2.1 to 2.3 */
1117 } else if (conn
->minor
>= 4 && conn
->minor
< 11) {
1118 /* From 2.4 to 2.10 */
1119 ret
= cmd_create_session_2_4(payload
, session_name
,
1120 hostname
, &live_timer
, &snapshot
);
1122 /* From 2.11 to ... */
1123 ret
= cmd_create_session_2_11(payload
, session_name
,
1124 hostname
, &live_timer
, &snapshot
);
1131 session
= session_create(session_name
, hostname
, live_timer
,
1132 snapshot
, conn
->major
, conn
->minor
);
1137 assert(!conn
->session
);
1138 conn
->session
= session
;
1139 DBG("Created session %" PRIu64
, session
->id
);
1141 reply
.session_id
= htobe64(session
->id
);
1145 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1147 reply
.ret_code
= htobe32(LTTNG_OK
);
1150 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1151 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1152 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1161 * When we have received all the streams and the metadata for a channel,
1162 * we make them visible to the viewer threads.
1164 static void publish_connection_local_streams(struct relay_connection
*conn
)
1166 struct relay_stream
*stream
;
1167 struct relay_session
*session
= conn
->session
;
1170 * We publish all streams belonging to a session atomically wrt
1173 pthread_mutex_lock(&session
->lock
);
1175 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1177 stream_publish(stream
);
1182 * Inform the viewer that there are new streams in the session.
1184 if (session
->viewer_attached
) {
1185 uatomic_set(&session
->new_streams
, 1);
1187 pthread_mutex_unlock(&session
->lock
);
1191 * relay_add_stream: allocate a new stream for a session
1193 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1194 struct relay_connection
*conn
,
1195 const struct lttng_buffer_view
*payload
)
1199 struct relay_session
*session
= conn
->session
;
1200 struct relay_stream
*stream
= NULL
;
1201 struct lttcomm_relayd_status_stream reply
;
1202 struct ctf_trace
*trace
= NULL
;
1203 uint64_t stream_handle
= -1ULL;
1204 char *path_name
= NULL
, *channel_name
= NULL
;
1205 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1206 struct relay_stream_chunk_id stream_chunk_id
= { 0 };
1208 if (!session
|| !conn
->version_check_done
) {
1209 ERR("Trying to add a stream before version check");
1211 goto end_no_session
;
1214 if (session
->minor
== 1) {
1216 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1218 } else if (session
->minor
> 1 && session
->minor
< 11) {
1219 /* From 2.2 to 2.10 */
1220 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1221 &channel_name
, &tracefile_size
, &tracefile_count
);
1223 /* From 2.11 to ... */
1224 ret
= cmd_recv_stream_2_11(payload
, &path_name
,
1225 &channel_name
, &tracefile_size
, &tracefile_count
,
1226 &stream_chunk_id
.value
);
1227 stream_chunk_id
.is_set
= true;
1234 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1238 /* This stream here has one reference on the trace. */
1240 pthread_mutex_lock(&last_relay_stream_id_lock
);
1241 stream_handle
= ++last_relay_stream_id
;
1242 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1244 /* We pass ownership of path_name and channel_name. */
1245 stream
= stream_create(trace
, stream_handle
, path_name
,
1246 channel_name
, tracefile_size
, tracefile_count
,
1249 channel_name
= NULL
;
1252 * Streams are the owners of their trace. Reference to trace is
1253 * kept within stream_create().
1255 ctf_trace_put(trace
);
1258 memset(&reply
, 0, sizeof(reply
));
1259 reply
.handle
= htobe64(stream_handle
);
1261 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1263 reply
.ret_code
= htobe32(LTTNG_OK
);
1266 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1267 sizeof(struct lttcomm_relayd_status_stream
), 0);
1268 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1269 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1281 * relay_close_stream: close a specific stream
1283 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1284 struct relay_connection
*conn
,
1285 const struct lttng_buffer_view
*payload
)
1289 struct relay_session
*session
= conn
->session
;
1290 struct lttcomm_relayd_close_stream stream_info
;
1291 struct lttcomm_relayd_generic_reply reply
;
1292 struct relay_stream
*stream
;
1294 DBG("Close stream received");
1296 if (!session
|| !conn
->version_check_done
) {
1297 ERR("Trying to close a stream before version check");
1299 goto end_no_session
;
1302 if (payload
->size
< sizeof(stream_info
)) {
1303 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1304 sizeof(stream_info
), payload
->size
);
1306 goto end_no_session
;
1308 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1309 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1310 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1312 stream
= stream_get_by_id(stream_info
.stream_id
);
1319 * Set last_net_seq_num before the close flag. Required by data
1322 pthread_mutex_lock(&stream
->lock
);
1323 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1324 pthread_mutex_unlock(&stream
->lock
);
1327 * This is one of the conditions which may trigger a stream close
1328 * with the others being:
1329 * 1) A close command is received for a stream
1330 * 2) The control connection owning the stream is closed
1331 * 3) We have received all of the stream's data _after_ a close
1334 try_stream_close(stream
);
1335 if (stream
->is_metadata
) {
1336 struct relay_viewer_stream
*vstream
;
1338 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1340 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1342 * Since all the metadata has been sent to the
1343 * viewer and that we have a request to close
1344 * its stream, we can safely teardown the
1345 * corresponding metadata viewer stream.
1347 viewer_stream_put(vstream
);
1349 /* Put local reference. */
1350 viewer_stream_put(vstream
);
1357 memset(&reply
, 0, sizeof(reply
));
1359 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1361 reply
.ret_code
= htobe32(LTTNG_OK
);
1363 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1364 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1365 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1366 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1376 * relay_reset_metadata: reset a metadata stream
1379 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1380 struct relay_connection
*conn
,
1381 const struct lttng_buffer_view
*payload
)
1385 struct relay_session
*session
= conn
->session
;
1386 struct lttcomm_relayd_reset_metadata stream_info
;
1387 struct lttcomm_relayd_generic_reply reply
;
1388 struct relay_stream
*stream
;
1390 DBG("Reset metadata received");
1392 if (!session
|| !conn
->version_check_done
) {
1393 ERR("Trying to reset a metadata stream before version check");
1395 goto end_no_session
;
1398 if (payload
->size
< sizeof(stream_info
)) {
1399 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1400 sizeof(stream_info
), payload
->size
);
1402 goto end_no_session
;
1404 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1405 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1406 stream_info
.version
= be64toh(stream_info
.version
);
1408 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1410 /* Unsupported for live sessions for now. */
1411 if (session
->live_timer
!= 0) {
1416 stream
= stream_get_by_id(stream_info
.stream_id
);
1421 pthread_mutex_lock(&stream
->lock
);
1422 if (!stream
->is_metadata
) {
1427 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1428 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1429 &stream
->stream_fd
->fd
);
1431 ERR("Failed to rotate metadata file %s of channel %s",
1432 stream
->path_name
, stream
->channel_name
);
1437 pthread_mutex_unlock(&stream
->lock
);
1441 memset(&reply
, 0, sizeof(reply
));
1443 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1445 reply
.ret_code
= htobe32(LTTNG_OK
);
1447 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1448 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1449 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1450 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1460 * relay_unknown_command: send -1 if received unknown command
1462 static void relay_unknown_command(struct relay_connection
*conn
)
1464 struct lttcomm_relayd_generic_reply reply
;
1467 memset(&reply
, 0, sizeof(reply
));
1468 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1469 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1470 if (send_ret
< sizeof(reply
)) {
1471 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1476 * relay_start: send an acknowledgment to the client to tell if we are
1477 * ready to receive data. We are ready if a session is established.
1479 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1480 struct relay_connection
*conn
,
1481 const struct lttng_buffer_view
*payload
)
1485 struct lttcomm_relayd_generic_reply reply
;
1486 struct relay_session
*session
= conn
->session
;
1489 DBG("Trying to start the streaming without a session established");
1490 ret
= htobe32(LTTNG_ERR_UNK
);
1493 memset(&reply
, 0, sizeof(reply
));
1494 reply
.ret_code
= htobe32(LTTNG_OK
);
1495 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1497 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1498 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1507 * Append padding to the file pointed by the file descriptor fd.
1509 static int write_padding_to_file(int fd
, uint32_t size
)
1518 zeros
= zmalloc(size
);
1519 if (zeros
== NULL
) {
1520 PERROR("zmalloc zeros for padding");
1525 ret
= lttng_write(fd
, zeros
, size
);
1527 PERROR("write padding to file");
1537 * Close the current index file if it is open, and create a new one.
1539 * Return 0 on success, -1 on error.
1542 int create_rotate_index_file(struct relay_stream
*stream
)
1545 uint32_t major
, minor
;
1547 /* Put ref on previous index_file. */
1548 if (stream
->index_file
) {
1549 lttng_index_file_put(stream
->index_file
);
1550 stream
->index_file
= NULL
;
1552 major
= stream
->trace
->session
->major
;
1553 minor
= stream
->trace
->session
->minor
;
1554 stream
->index_file
= lttng_index_file_create(stream
->path_name
,
1555 stream
->channel_name
,
1556 -1, -1, stream
->tracefile_size
,
1557 tracefile_array_get_file_index_head(stream
->tfa
),
1558 lttng_to_index_major(major
, minor
),
1559 lttng_to_index_minor(major
, minor
));
1560 if (!stream
->index_file
) {
1572 int do_rotate_stream(struct relay_stream
*stream
)
1576 /* Perform the stream rotation. */
1577 ret
= utils_rotate_stream_file(stream
->path_name
,
1578 stream
->channel_name
, stream
->tracefile_size
,
1579 stream
->tracefile_count
, -1,
1580 -1, stream
->stream_fd
->fd
,
1581 NULL
, &stream
->stream_fd
->fd
);
1583 ERR("Rotating stream output file");
1586 stream
->tracefile_size_current
= 0;
1588 /* Rotate also the index if the stream is not a metadata stream. */
1589 if (!stream
->is_metadata
) {
1590 ret
= create_rotate_index_file(stream
);
1592 ERR("Failed to rotate index file");
1597 stream
->rotate_at_seq_num
= -1ULL;
1598 stream
->pos_after_last_complete_data_index
= 0;
1605 * If too much data has been written in a tracefile before we received the
1606 * rotation command, we have to move the excess data to the new tracefile and
1607 * perform the rotation. This can happen because the control and data
1608 * connections are separate, the indexes as well as the commands arrive from
1609 * the control connection and we have no control over the order so we could be
1610 * in a situation where too much data has been received on the data connection
1611 * before the rotation command on the control connection arrives. We don't need
1612 * to update the index because its order is guaranteed with the rotation
1616 int rotate_truncate_stream(struct relay_stream
*stream
)
1620 uint64_t diff
, pos
= 0;
1621 char buf
[FILE_COPY_BUFFER_SIZE
];
1623 assert(!stream
->is_metadata
);
1625 assert(stream
->tracefile_size_current
>
1626 stream
->pos_after_last_complete_data_index
);
1627 diff
= stream
->tracefile_size_current
-
1628 stream
->pos_after_last_complete_data_index
;
1630 /* Create the new tracefile. */
1631 new_fd
= utils_create_stream_file(stream
->path_name
,
1632 stream
->channel_name
,
1633 stream
->tracefile_size
, stream
->tracefile_count
,
1634 /* uid */ -1, /* gid */ -1, /* suffix */ NULL
);
1636 ERR("Failed to create new stream file at path %s for channel %s",
1637 stream
->path_name
, stream
->channel_name
);
1643 * Rewind the current tracefile to the position at which the rotation
1644 * should have occured.
1646 lseek_ret
= lseek(stream
->stream_fd
->fd
,
1647 stream
->pos_after_last_complete_data_index
, SEEK_SET
);
1648 if (lseek_ret
< 0) {
1649 PERROR("seek truncate stream");
1654 /* Move data from the old file to the new file. */
1655 while (pos
< diff
) {
1656 uint64_t count
, bytes_left
;
1659 bytes_left
= diff
- pos
;
1660 count
= bytes_left
> sizeof(buf
) ? sizeof(buf
) : bytes_left
;
1661 assert(count
<= SIZE_MAX
);
1663 io_ret
= lttng_read(stream
->stream_fd
->fd
, buf
, count
);
1664 if (io_ret
< (ssize_t
) count
) {
1665 char error_string
[256];
1667 snprintf(error_string
, sizeof(error_string
),
1668 "Failed to read %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1669 count
, stream
->stream_fd
->fd
, io_ret
);
1671 PERROR("%s", error_string
);
1673 ERR("%s", error_string
);
1679 io_ret
= lttng_write(new_fd
, buf
, count
);
1680 if (io_ret
< (ssize_t
) count
) {
1681 char error_string
[256];
1683 snprintf(error_string
, sizeof(error_string
),
1684 "Failed to write %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1685 count
, new_fd
, io_ret
);
1687 PERROR("%s", error_string
);
1689 ERR("%s", error_string
);
1698 /* Truncate the file to get rid of the excess data. */
1699 ret
= ftruncate(stream
->stream_fd
->fd
,
1700 stream
->pos_after_last_complete_data_index
);
1702 PERROR("ftruncate");
1706 ret
= close(stream
->stream_fd
->fd
);
1708 PERROR("Closing tracefile");
1712 ret
= create_rotate_index_file(stream
);
1714 ERR("Rotate stream index file");
1719 * Update the offset and FD of all the eventual indexes created by the
1720 * data connection before the rotation command arrived.
1722 ret
= relay_index_switch_all_files(stream
);
1724 ERR("Failed to rotate index file");
1728 stream
->stream_fd
->fd
= new_fd
;
1729 stream
->tracefile_size_current
= diff
;
1730 stream
->pos_after_last_complete_data_index
= 0;
1731 stream
->rotate_at_seq_num
= -1ULL;
1740 * Check if a stream should perform a rotation (for session rotation).
1741 * Must be called with the stream lock held.
1743 * Return 0 on success, a negative value on error.
1746 int try_rotate_stream(struct relay_stream
*stream
)
1751 /* No rotation expected. */
1752 if (stream
->rotate_at_seq_num
== -1ULL) {
1756 trace_seq
= min(stream
->prev_seq
, stream
->prev_index_seq
);
1757 if (stream
->prev_seq
== -1ULL || stream
->prev_index_seq
== -1ULL ||
1758 trace_seq
< stream
->rotate_at_seq_num
) {
1759 DBG("Stream %" PRIu64
" not yet ready for rotation (rotate_at_seq_num = %" PRIu64
", prev_seq = %" PRIu64
", prev_index_seq = %" PRIu64
")",
1760 stream
->stream_handle
,
1761 stream
->rotate_at_seq_num
,
1763 stream
->prev_index_seq
);
1765 } else if (stream
->prev_seq
> stream
->rotate_at_seq_num
) {
1767 * prev_seq is checked here since indexes and rotation
1768 * commands are serialized with respect to each other.
1770 DBG("Rotation after too much data has been written in tracefile "
1771 "for stream %" PRIu64
", need to truncate before "
1772 "rotating", stream
->stream_handle
);
1773 ret
= rotate_truncate_stream(stream
);
1775 ERR("Failed to truncate stream");
1779 if (trace_seq
!= stream
->rotate_at_seq_num
) {
1781 * Unexpected, protocol error/bug.
1782 * It could mean that we received a rotation position
1783 * that is in the past.
1785 ERR("Stream %" PRIu64
" is in an inconsistent state (rotate_at_seq_num = %" PRIu64
", prev_seq = %" PRIu64
", prev_index_seq = %" PRIu64
")",
1786 stream
->stream_handle
,
1787 stream
->rotate_at_seq_num
,
1789 stream
->prev_index_seq
);
1793 DBG("Stream %" PRIu64
" ready for rotation",
1794 stream
->stream_handle
);
1795 ret
= do_rotate_stream(stream
);
1803 * relay_recv_metadata: receive the metadata for the session.
1805 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1806 struct relay_connection
*conn
,
1807 const struct lttng_buffer_view
*payload
)
1811 struct relay_session
*session
= conn
->session
;
1812 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1813 struct relay_stream
*metadata_stream
;
1814 uint64_t metadata_payload_size
;
1817 ERR("Metadata sent before version check");
1822 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1823 ERR("Incorrect data size");
1827 metadata_payload_size
= recv_hdr
->data_size
-
1828 sizeof(struct lttcomm_relayd_metadata_payload
);
1830 memcpy(&metadata_payload_header
, payload
->data
,
1831 sizeof(metadata_payload_header
));
1832 metadata_payload_header
.stream_id
= be64toh(
1833 metadata_payload_header
.stream_id
);
1834 metadata_payload_header
.padding_size
= be32toh(
1835 metadata_payload_header
.padding_size
);
1837 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1838 if (!metadata_stream
) {
1843 pthread_mutex_lock(&metadata_stream
->lock
);
1845 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
,
1846 payload
->data
+ sizeof(metadata_payload_header
),
1847 metadata_payload_size
);
1848 if (size_ret
< metadata_payload_size
) {
1849 ERR("Relay error writing metadata on file");
1854 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1855 metadata_payload_header
.padding_size
);
1856 if (size_ret
< (int64_t) metadata_payload_header
.padding_size
) {
1861 metadata_stream
->metadata_received
+=
1862 metadata_payload_size
+ metadata_payload_header
.padding_size
;
1863 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1864 metadata_stream
->metadata_received
);
1866 ret
= try_rotate_stream(metadata_stream
);
1872 pthread_mutex_unlock(&metadata_stream
->lock
);
1873 stream_put(metadata_stream
);
1879 * relay_send_version: send relayd version number
1881 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1882 struct relay_connection
*conn
,
1883 const struct lttng_buffer_view
*payload
)
1887 struct lttcomm_relayd_version reply
, msg
;
1888 bool compatible
= true;
1890 conn
->version_check_done
= true;
1892 /* Get version from the other side. */
1893 if (payload
->size
< sizeof(msg
)) {
1894 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1895 sizeof(msg
), payload
->size
);
1900 memcpy(&msg
, payload
->data
, sizeof(msg
));
1901 msg
.major
= be32toh(msg
.major
);
1902 msg
.minor
= be32toh(msg
.minor
);
1904 memset(&reply
, 0, sizeof(reply
));
1905 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1906 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1908 /* Major versions must be the same */
1909 if (reply
.major
!= msg
.major
) {
1910 DBG("Incompatible major versions (%u vs %u), deleting session",
1911 reply
.major
, msg
.major
);
1915 conn
->major
= reply
.major
;
1916 /* We adapt to the lowest compatible version */
1917 if (reply
.minor
<= msg
.minor
) {
1918 conn
->minor
= reply
.minor
;
1920 conn
->minor
= msg
.minor
;
1923 reply
.major
= htobe32(reply
.major
);
1924 reply
.minor
= htobe32(reply
.minor
);
1925 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1927 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1928 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1941 DBG("Version check done using protocol %u.%u", conn
->major
,
1949 * Check for data pending for a given stream id from the session daemon.
1951 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1952 struct relay_connection
*conn
,
1953 const struct lttng_buffer_view
*payload
)
1955 struct relay_session
*session
= conn
->session
;
1956 struct lttcomm_relayd_data_pending msg
;
1957 struct lttcomm_relayd_generic_reply reply
;
1958 struct relay_stream
*stream
;
1961 uint64_t stream_seq
;
1963 DBG("Data pending command received");
1965 if (!session
|| !conn
->version_check_done
) {
1966 ERR("Trying to check for data before version check");
1968 goto end_no_session
;
1971 if (payload
->size
< sizeof(msg
)) {
1972 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1973 sizeof(msg
), payload
->size
);
1975 goto end_no_session
;
1977 memcpy(&msg
, payload
->data
, sizeof(msg
));
1978 msg
.stream_id
= be64toh(msg
.stream_id
);
1979 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1981 stream
= stream_get_by_id(msg
.stream_id
);
1982 if (stream
== NULL
) {
1987 pthread_mutex_lock(&stream
->lock
);
1989 if (session_streams_have_index(session
)) {
1991 * Ensure that both the index and stream data have been
1992 * flushed up to the requested point.
1994 stream_seq
= min(stream
->prev_seq
, stream
->prev_index_seq
);
1996 stream_seq
= stream
->prev_seq
;
1998 DBG("Data pending for stream id %" PRIu64
": prev_seq %" PRIu64
1999 ", prev_index_seq %" PRIu64
2000 ", and last_seq %" PRIu64
, msg
.stream_id
,
2001 stream
->prev_seq
, stream
->prev_index_seq
,
2002 msg
.last_net_seq_num
);
2004 /* Avoid wrapping issue */
2005 if (((int64_t) (stream_seq
- msg
.last_net_seq_num
)) >= 0) {
2006 /* Data has in fact been written and is NOT pending */
2009 /* Data still being streamed thus pending */
2013 stream
->data_pending_check_done
= true;
2014 pthread_mutex_unlock(&stream
->lock
);
2019 memset(&reply
, 0, sizeof(reply
));
2020 reply
.ret_code
= htobe32(ret
);
2021 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2022 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2023 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
2033 * Wait for the control socket to reach a quiescent state.
2035 * Note that for now, when receiving this command from the session
2036 * daemon, this means that every subsequent commands or data received on
2037 * the control socket has been handled. So, this is why we simply return
2040 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
2041 struct relay_connection
*conn
,
2042 const struct lttng_buffer_view
*payload
)
2046 struct relay_stream
*stream
;
2047 struct lttcomm_relayd_quiescent_control msg
;
2048 struct lttcomm_relayd_generic_reply reply
;
2050 DBG("Checking quiescent state on control socket");
2052 if (!conn
->session
|| !conn
->version_check_done
) {
2053 ERR("Trying to check for data before version check");
2055 goto end_no_session
;
2058 if (payload
->size
< sizeof(msg
)) {
2059 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2060 sizeof(msg
), payload
->size
);
2062 goto end_no_session
;
2064 memcpy(&msg
, payload
->data
, sizeof(msg
));
2065 msg
.stream_id
= be64toh(msg
.stream_id
);
2067 stream
= stream_get_by_id(msg
.stream_id
);
2071 pthread_mutex_lock(&stream
->lock
);
2072 stream
->data_pending_check_done
= true;
2073 pthread_mutex_unlock(&stream
->lock
);
2075 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2078 memset(&reply
, 0, sizeof(reply
));
2079 reply
.ret_code
= htobe32(LTTNG_OK
);
2080 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2081 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2082 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2094 * Initialize a data pending command. This means that a consumer is about
2095 * to ask for data pending for each stream it holds. Simply iterate over
2096 * all streams of a session and set the data_pending_check_done flag.
2098 * This command returns to the client a LTTNG_OK code.
2100 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2101 struct relay_connection
*conn
,
2102 const struct lttng_buffer_view
*payload
)
2106 struct lttng_ht_iter iter
;
2107 struct lttcomm_relayd_begin_data_pending msg
;
2108 struct lttcomm_relayd_generic_reply reply
;
2109 struct relay_stream
*stream
;
2114 DBG("Init streams for data pending");
2116 if (!conn
->session
|| !conn
->version_check_done
) {
2117 ERR("Trying to check for data before version check");
2119 goto end_no_session
;
2122 if (payload
->size
< sizeof(msg
)) {
2123 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2124 sizeof(msg
), payload
->size
);
2126 goto end_no_session
;
2128 memcpy(&msg
, payload
->data
, sizeof(msg
));
2129 msg
.session_id
= be64toh(msg
.session_id
);
2132 * Iterate over all streams to set the begin data pending flag.
2133 * For now, the streams are indexed by stream handle so we have
2134 * to iterate over all streams to find the one associated with
2135 * the right session_id.
2138 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2140 if (!stream_get(stream
)) {
2143 if (stream
->trace
->session
->id
== msg
.session_id
) {
2144 pthread_mutex_lock(&stream
->lock
);
2145 stream
->data_pending_check_done
= false;
2146 pthread_mutex_unlock(&stream
->lock
);
2147 DBG("Set begin data pending flag to stream %" PRIu64
,
2148 stream
->stream_handle
);
2154 memset(&reply
, 0, sizeof(reply
));
2155 /* All good, send back reply. */
2156 reply
.ret_code
= htobe32(LTTNG_OK
);
2158 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2159 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2160 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2172 * End data pending command. This will check, for a given session id, if
2173 * each stream associated with it has its data_pending_check_done flag
2174 * set. If not, this means that the client lost track of the stream but
2175 * the data is still being streamed on our side. In this case, we inform
2176 * the client that data is in flight.
2178 * Return to the client if there is data in flight or not with a ret_code.
2180 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2181 struct relay_connection
*conn
,
2182 const struct lttng_buffer_view
*payload
)
2186 struct lttng_ht_iter iter
;
2187 struct lttcomm_relayd_end_data_pending msg
;
2188 struct lttcomm_relayd_generic_reply reply
;
2189 struct relay_stream
*stream
;
2190 uint32_t is_data_inflight
= 0;
2192 DBG("End data pending command");
2194 if (!conn
->session
|| !conn
->version_check_done
) {
2195 ERR("Trying to check for data before version check");
2197 goto end_no_session
;
2200 if (payload
->size
< sizeof(msg
)) {
2201 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2202 sizeof(msg
), payload
->size
);
2204 goto end_no_session
;
2206 memcpy(&msg
, payload
->data
, sizeof(msg
));
2207 msg
.session_id
= be64toh(msg
.session_id
);
2210 * Iterate over all streams to see if the begin data pending
2214 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2216 if (!stream_get(stream
)) {
2219 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2223 pthread_mutex_lock(&stream
->lock
);
2224 if (!stream
->data_pending_check_done
) {
2225 uint64_t stream_seq
;
2227 if (session_streams_have_index(conn
->session
)) {
2229 * Ensure that both the index and stream data have been
2230 * flushed up to the requested point.
2232 stream_seq
= min(stream
->prev_seq
, stream
->prev_index_seq
);
2234 stream_seq
= stream
->prev_seq
;
2236 if (!stream
->closed
|| !(((int64_t) (stream_seq
- stream
->last_net_seq_num
)) >= 0)) {
2237 is_data_inflight
= 1;
2238 DBG("Data is still in flight for stream %" PRIu64
,
2239 stream
->stream_handle
);
2240 pthread_mutex_unlock(&stream
->lock
);
2245 pthread_mutex_unlock(&stream
->lock
);
2250 memset(&reply
, 0, sizeof(reply
));
2251 /* All good, send back reply. */
2252 reply
.ret_code
= htobe32(is_data_inflight
);
2254 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2255 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2256 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2268 * Receive an index for a specific stream.
2270 * Return 0 on success else a negative value.
2272 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2273 struct relay_connection
*conn
,
2274 const struct lttng_buffer_view
*payload
)
2278 struct relay_session
*session
= conn
->session
;
2279 struct lttcomm_relayd_index index_info
;
2280 struct relay_index
*index
;
2281 struct lttcomm_relayd_generic_reply reply
;
2282 struct relay_stream
*stream
;
2287 DBG("Relay receiving index");
2289 if (!session
|| !conn
->version_check_done
) {
2290 ERR("Trying to close a stream before version check");
2292 goto end_no_session
;
2295 msg_len
= lttcomm_relayd_index_len(
2296 lttng_to_index_major(conn
->major
, conn
->minor
),
2297 lttng_to_index_minor(conn
->major
, conn
->minor
));
2298 if (payload
->size
< msg_len
) {
2299 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2300 msg_len
, payload
->size
);
2302 goto end_no_session
;
2304 memcpy(&index_info
, payload
->data
, msg_len
);
2305 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2306 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2307 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2308 index_info
.content_size
= be64toh(index_info
.content_size
);
2309 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2310 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2311 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2312 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2314 if (conn
->minor
>= 8) {
2315 index_info
.stream_instance_id
=
2316 be64toh(index_info
.stream_instance_id
);
2317 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2320 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2322 ERR("stream_get_by_id not found");
2326 pthread_mutex_lock(&stream
->lock
);
2328 /* Live beacon handling */
2329 if (index_info
.packet_size
== 0) {
2330 DBG("Received live beacon for stream %" PRIu64
,
2331 stream
->stream_handle
);
2334 * Only flag a stream inactive when it has already
2335 * received data and no indexes are in flight.
2337 if (stream
->index_received_seqcount
> 0
2338 && stream
->indexes_in_flight
== 0) {
2339 stream
->beacon_ts_end
= index_info
.timestamp_end
;
2342 goto end_stream_put
;
2344 stream
->beacon_ts_end
= -1ULL;
2347 if (stream
->ctf_stream_id
== -1ULL) {
2348 stream
->ctf_stream_id
= index_info
.stream_id
;
2350 index
= relay_index_get_by_id_or_create(stream
, index_info
.net_seq_num
);
2353 ERR("relay_index_get_by_id_or_create index NULL");
2354 goto end_stream_put
;
2356 if (set_index_control_data(index
, &index_info
, conn
)) {
2357 ERR("set_index_control_data error");
2358 relay_index_put(index
);
2360 goto end_stream_put
;
2362 ret
= relay_index_try_flush(index
);
2364 tracefile_array_commit_seq(stream
->tfa
);
2365 stream
->index_received_seqcount
++;
2366 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
2367 stream
->prev_index_seq
= index_info
.net_seq_num
;
2368 } else if (ret
> 0) {
2375 * relay_index_try_flush is responsible for the self-reference
2376 * put of the index object on error.
2378 ERR("relay_index_try_flush error %d", ret
);
2383 pthread_mutex_unlock(&stream
->lock
);
2388 memset(&reply
, 0, sizeof(reply
));
2390 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2392 reply
.ret_code
= htobe32(LTTNG_OK
);
2394 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2395 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2396 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2405 * Receive the streams_sent message.
2407 * Return 0 on success else a negative value.
2409 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2410 struct relay_connection
*conn
,
2411 const struct lttng_buffer_view
*payload
)
2415 struct lttcomm_relayd_generic_reply reply
;
2419 DBG("Relay receiving streams_sent");
2421 if (!conn
->session
|| !conn
->version_check_done
) {
2422 ERR("Trying to close a stream before version check");
2424 goto end_no_session
;
2428 * Publish every pending stream in the connection recv list which are
2429 * now ready to be used by the viewer.
2431 publish_connection_local_streams(conn
);
2433 memset(&reply
, 0, sizeof(reply
));
2434 reply
.ret_code
= htobe32(LTTNG_OK
);
2435 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2436 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2437 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2450 * relay_rotate_session_stream: rotate a stream to a new tracefile for the session
2451 * rotation feature (not the tracefile rotation feature).
2453 static int relay_rotate_session_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
2454 struct relay_connection
*conn
,
2455 const struct lttng_buffer_view
*payload
)
2459 struct relay_session
*session
= conn
->session
;
2460 struct lttcomm_relayd_rotate_stream stream_info
;
2461 struct lttcomm_relayd_generic_reply reply
;
2462 struct relay_stream
*stream
;
2465 struct lttng_buffer_view new_path_view
;
2467 DBG("Rotate stream received");
2469 if (!session
|| !conn
->version_check_done
) {
2470 ERR("Trying to rotate a stream before version check");
2475 if (session
->major
== 2 && session
->minor
< 11) {
2476 ERR("Unsupported feature before 2.11");
2481 header_len
= sizeof(struct lttcomm_relayd_rotate_stream
);
2483 if (payload
->size
< header_len
) {
2484 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2485 header_len
, payload
->size
);
2490 memcpy(&stream_info
, payload
->data
, header_len
);
2492 /* Convert to host */
2493 stream_info
.pathname_length
= be32toh(stream_info
.pathname_length
);
2494 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
2495 stream_info
.new_chunk_id
= be64toh(stream_info
.new_chunk_id
);
2496 stream_info
.rotate_at_seq_num
= be64toh(stream_info
.rotate_at_seq_num
);
2498 path_len
= stream_info
.pathname_length
;
2499 if (payload
->size
< header_len
+ path_len
) {
2500 ERR("Unexpected payload size in \"relay_rotate_session_stream\" including path: expected >= %zu bytes, got %zu bytes",
2501 header_len
+ path_len
, payload
->size
);
2506 /* Ensure it fits in local filename length. */
2507 if (path_len
>= LTTNG_PATH_MAX
) {
2508 ret
= -ENAMETOOLONG
;
2509 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2510 path_len
, LTTNG_PATH_MAX
);
2514 new_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2515 stream_info
.pathname_length
);
2517 stream
= stream_get_by_id(stream_info
.stream_id
);
2523 pthread_mutex_lock(&stream
->lock
);
2526 * Update the trace path (just the folder, the stream name does not
2529 free(stream
->path_name
);
2530 stream
->path_name
= create_output_path(new_path_view
.data
);
2531 if (!stream
->path_name
) {
2532 ERR("Failed to create a new output path");
2534 goto end_stream_unlock
;
2536 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
,
2539 ERR("relay creating output directory");
2541 goto end_stream_unlock
;
2544 assert(stream
->current_chunk_id
.is_set
);
2545 stream
->current_chunk_id
.value
= stream_info
.new_chunk_id
;
2547 if (stream
->is_metadata
) {
2549 * The metadata stream is sent only over the control connection
2550 * so we know we have all the data to perform the stream
2553 ret
= do_rotate_stream(stream
);
2555 stream
->rotate_at_seq_num
= stream_info
.rotate_at_seq_num
;
2556 ret
= try_rotate_stream(stream
);
2559 goto end_stream_unlock
;
2563 pthread_mutex_unlock(&stream
->lock
);
2566 memset(&reply
, 0, sizeof(reply
));
2568 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2570 reply
.ret_code
= htobe32(LTTNG_OK
);
2572 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2573 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2574 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2575 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2585 * relay_mkdir: Create a folder on the disk.
2587 static int relay_mkdir(const struct lttcomm_relayd_hdr
*recv_hdr
,
2588 struct relay_connection
*conn
,
2589 const struct lttng_buffer_view
*payload
)
2592 struct relay_session
*session
= conn
->session
;
2593 struct lttcomm_relayd_mkdir path_info_header
;
2594 struct lttcomm_relayd_generic_reply reply
;
2598 struct lttng_buffer_view path_view
;
2600 if (!session
|| !conn
->version_check_done
) {
2601 ERR("Trying to create a directory before version check");
2603 goto end_no_session
;
2606 if (session
->major
== 2 && session
->minor
< 11) {
2608 * This client is not supposed to use this command since
2609 * it predates its introduction.
2611 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2613 goto end_no_session
;
2616 header_len
= sizeof(path_info_header
);
2617 if (payload
->size
< header_len
) {
2618 ERR("Unexpected payload size in \"relay_mkdir\": expected >= %zu bytes, got %zu bytes",
2619 header_len
, payload
->size
);
2621 goto end_no_session
;
2624 memcpy(&path_info_header
, payload
->data
, header_len
);
2626 path_info_header
.length
= be32toh(path_info_header
.length
);
2628 if (payload
->size
< header_len
+ path_info_header
.length
) {
2629 ERR("Unexpected payload size in \"relay_mkdir\" including path: expected >= %zu bytes, got %zu bytes",
2630 header_len
+ path_info_header
.length
, payload
->size
);
2632 goto end_no_session
;
2635 /* Ensure that it fits in local path length. */
2636 if (path_info_header
.length
>= LTTNG_PATH_MAX
) {
2637 ret
= -ENAMETOOLONG
;
2638 ERR("Path name argument of mkdir command (%" PRIu32
" bytes) exceeds the maximal length allowed (%d bytes)",
2639 path_info_header
.length
, LTTNG_PATH_MAX
);
2643 path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2644 path_info_header
.length
);
2646 path
= create_output_path(path_view
.data
);
2648 ERR("Failed to create output path");
2653 ret
= utils_mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, -1, -1);
2655 ERR("relay creating output directory");
2662 memset(&reply
, 0, sizeof(reply
));
2664 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2666 reply
.ret_code
= htobe32(LTTNG_OK
);
2668 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2669 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2670 ERR("Failed to send \"mkdir\" command reply (ret = %zd)", send_ret
);
2679 static int validate_rotate_rename_path_length(const char *path_type
,
2680 uint32_t path_length
)
2684 if (path_length
> LTTNG_PATH_MAX
) {
2685 ret
= -ENAMETOOLONG
;
2686 ERR("rotate rename \"%s\" path name length (%" PRIu32
" bytes) exceeds the allowed size of %i bytes",
2687 path_type
, path_length
, LTTNG_PATH_MAX
);
2688 } else if (path_length
== 0) {
2690 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type
);
2696 * relay_rotate_rename: rename the trace folder after a rotation is
2697 * completed. We are not closing any fd here, just moving the folder, so it
2698 * works even if data is still in-flight.
2700 static int relay_rotate_rename(const struct lttcomm_relayd_hdr
*recv_hdr
,
2701 struct relay_connection
*conn
,
2702 const struct lttng_buffer_view
*payload
)
2706 struct relay_session
*session
= conn
->session
;
2707 struct lttcomm_relayd_generic_reply reply
;
2708 struct lttcomm_relayd_rotate_rename header
;
2710 size_t received_paths_size
;
2711 char *complete_old_path
= NULL
, *complete_new_path
= NULL
;
2712 struct lttng_buffer_view old_path_view
;
2713 struct lttng_buffer_view new_path_view
;
2715 if (!session
|| !conn
->version_check_done
) {
2716 ERR("Trying to rename a trace folder before version check");
2721 if (session
->major
== 2 && session
->minor
< 11) {
2722 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2727 header_len
= sizeof(header
);
2728 if (payload
->size
< header_len
) {
2729 ERR("Unexpected payload size in \"relay_rotate_rename\": expected >= %zu bytes, got %zu bytes",
2730 header_len
, payload
->size
);
2735 memcpy(&header
, payload
->data
, header_len
);
2737 header
.old_path_length
= be32toh(header
.old_path_length
);
2738 header
.new_path_length
= be32toh(header
.new_path_length
);
2739 received_paths_size
= header
.old_path_length
+ header
.new_path_length
;
2741 if (payload
->size
< header_len
+ received_paths_size
) {
2742 ERR("Unexpected payload size in \"relay_rotate_rename\" including paths: expected >= %zu bytes, got %zu bytes",
2743 header_len
, payload
->size
);
2748 /* Ensure the paths don't exceed their allowed size. */
2749 ret
= validate_rotate_rename_path_length("old", header
.old_path_length
);
2753 ret
= validate_rotate_rename_path_length("new", header
.new_path_length
);
2758 old_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2759 header
.old_path_length
);
2760 new_path_view
= lttng_buffer_view_from_view(payload
,
2761 header_len
+ header
.old_path_length
,
2762 header
.new_path_length
);
2764 /* Validate that both paths received are NULL terminated. */
2765 if (old_path_view
.data
[old_path_view
.size
- 1] != '\0') {
2766 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2770 if (new_path_view
.data
[new_path_view
.size
- 1] != '\0') {
2771 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2776 complete_old_path
= create_output_path(old_path_view
.data
);
2777 if (!complete_old_path
) {
2778 ERR("Failed to build old output path in rotate_rename command");
2783 complete_new_path
= create_output_path(new_path_view
.data
);
2784 if (!complete_new_path
) {
2785 ERR("Failed to build new output path in rotate_rename command");
2790 ret
= utils_mkdir_recursive(complete_new_path
, S_IRWXU
| S_IRWXG
,
2793 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2799 * If a domain has not yet created its channel, the domain-specific
2800 * folder might not exist, but this is not an error.
2802 ret
= rename(complete_old_path
, complete_new_path
);
2803 if (ret
< 0 && errno
!= ENOENT
) {
2804 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2805 complete_old_path
, complete_new_path
);
2811 memset(&reply
, 0, sizeof(reply
));
2813 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2815 reply
.ret_code
= htobe32(LTTNG_OK
);
2817 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2819 if (send_ret
< sizeof(reply
)) {
2820 ERR("Failed to send \"rotate rename\" command reply (ret = %zd)",
2826 free(complete_old_path
);
2827 free(complete_new_path
);
2832 * Check if all the streams in the session have completed the last rotation.
2833 * The chunk_id value is used to distinguish the cases where a stream was
2834 * closed on the consumerd before the rotation started but it still active on
2835 * the relayd, and the case where a stream appeared on the consumerd/relayd
2836 * after the last rotation started (in that case, it is already writing in the
2837 * new chunk folder).
2840 int relay_rotate_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2841 struct relay_connection
*conn
,
2842 const struct lttng_buffer_view
*payload
)
2844 struct relay_session
*session
= conn
->session
;
2845 struct lttcomm_relayd_rotate_pending msg
;
2846 struct lttcomm_relayd_rotate_pending_reply reply
;
2847 struct lttng_ht_iter iter
;
2848 struct relay_stream
*stream
;
2852 bool rotate_pending
= false;
2854 DBG("Rotate pending command received");
2856 if (!session
|| !conn
->version_check_done
) {
2857 ERR("Trying to check for data before version check");
2862 if (session
->major
== 2 && session
->minor
< 11) {
2863 ERR("Unsupported feature before 2.11");
2868 if (payload
->size
< sizeof(msg
)) {
2869 ERR("Unexpected payload size in \"relay_rotate_pending\": expected >= %zu bytes, got %zu bytes",
2870 sizeof(msg
), payload
->size
);
2875 memcpy(&msg
, payload
->data
, sizeof(msg
));
2877 chunk_id
= be64toh(msg
.chunk_id
);
2879 DBG("Evaluating rotate pending for session \"%s\" and chunk id %" PRIu64
,
2880 session
->session_name
, chunk_id
);
2883 * Iterate over all the streams in the session and check if they are
2884 * still waiting for data to perform their rotation.
2887 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2889 if (!stream_get(stream
)) {
2892 if (stream
->trace
->session
!= session
) {
2896 pthread_mutex_lock(&stream
->lock
);
2897 if (stream
->rotate_at_seq_num
!= -1ULL) {
2898 /* We have not yet performed the rotation. */
2899 rotate_pending
= true;
2900 DBG("Stream %" PRIu64
" is still rotating",
2901 stream
->stream_handle
);
2902 } else if (stream
->current_chunk_id
.value
< chunk_id
) {
2904 * Stream closed on the consumer but still active on the
2907 rotate_pending
= true;
2908 DBG("Stream %" PRIu64
" did not exist on the consumer "
2909 "when the last rotation started, but is"
2910 "still waiting for data before getting"
2912 stream
->stream_handle
);
2914 pthread_mutex_unlock(&stream
->lock
);
2916 if (rotate_pending
) {
2923 memset(&reply
, 0, sizeof(reply
));
2924 reply
.generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
);
2925 reply
.is_pending
= (uint8_t) !!rotate_pending
;
2926 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2928 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2929 ERR("Failed to send \"rotate pending\" command reply (ret = %zd)",
2938 #define DBG_CMD(cmd_name, conn) \
2939 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2941 static int relay_process_control_command(struct relay_connection
*conn
,
2942 const struct lttcomm_relayd_hdr
*header
,
2943 const struct lttng_buffer_view
*payload
)
2947 switch (header
->cmd
) {
2948 case RELAYD_CREATE_SESSION
:
2949 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2950 ret
= relay_create_session(header
, conn
, payload
);
2952 case RELAYD_ADD_STREAM
:
2953 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2954 ret
= relay_add_stream(header
, conn
, payload
);
2956 case RELAYD_START_DATA
:
2957 DBG_CMD("RELAYD_START_DATA", conn
);
2958 ret
= relay_start(header
, conn
, payload
);
2960 case RELAYD_SEND_METADATA
:
2961 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2962 ret
= relay_recv_metadata(header
, conn
, payload
);
2964 case RELAYD_VERSION
:
2965 DBG_CMD("RELAYD_VERSION", conn
);
2966 ret
= relay_send_version(header
, conn
, payload
);
2968 case RELAYD_CLOSE_STREAM
:
2969 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2970 ret
= relay_close_stream(header
, conn
, payload
);
2972 case RELAYD_DATA_PENDING
:
2973 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2974 ret
= relay_data_pending(header
, conn
, payload
);
2976 case RELAYD_QUIESCENT_CONTROL
:
2977 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2978 ret
= relay_quiescent_control(header
, conn
, payload
);
2980 case RELAYD_BEGIN_DATA_PENDING
:
2981 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2982 ret
= relay_begin_data_pending(header
, conn
, payload
);
2984 case RELAYD_END_DATA_PENDING
:
2985 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2986 ret
= relay_end_data_pending(header
, conn
, payload
);
2988 case RELAYD_SEND_INDEX
:
2989 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2990 ret
= relay_recv_index(header
, conn
, payload
);
2992 case RELAYD_STREAMS_SENT
:
2993 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2994 ret
= relay_streams_sent(header
, conn
, payload
);
2996 case RELAYD_RESET_METADATA
:
2997 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2998 ret
= relay_reset_metadata(header
, conn
, payload
);
3000 case RELAYD_ROTATE_STREAM
:
3001 DBG_CMD("RELAYD_ROTATE_STREAM", conn
);
3002 ret
= relay_rotate_session_stream(header
, conn
, payload
);
3004 case RELAYD_ROTATE_RENAME
:
3005 DBG_CMD("RELAYD_ROTATE_RENAME", conn
);
3006 ret
= relay_rotate_rename(header
, conn
, payload
);
3008 case RELAYD_ROTATE_PENDING
:
3009 DBG_CMD("RELAYD_ROTATE_PENDING", conn
);
3010 ret
= relay_rotate_pending(header
, conn
, payload
);
3013 DBG_CMD("RELAYD_MKDIR", conn
);
3014 ret
= relay_mkdir(header
, conn
, payload
);
3016 case RELAYD_UPDATE_SYNC_INFO
:
3018 ERR("Received unknown command (%u)", header
->cmd
);
3019 relay_unknown_command(conn
);
3028 static enum relay_connection_status
relay_process_control_receive_payload(
3029 struct relay_connection
*conn
)
3032 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3033 struct lttng_dynamic_buffer
*reception_buffer
=
3034 &conn
->protocol
.ctrl
.reception_buffer
;
3035 struct ctrl_connection_state_receive_payload
*state
=
3036 &conn
->protocol
.ctrl
.state
.receive_payload
;
3037 struct lttng_buffer_view payload_view
;
3039 if (state
->left_to_receive
== 0) {
3040 /* Short-circuit for payload-less commands. */
3041 goto reception_complete
;
3044 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3045 reception_buffer
->data
+ state
->received
,
3046 state
->left_to_receive
, MSG_DONTWAIT
);
3048 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3049 PERROR("Unable to receive command payload on sock %d",
3051 status
= RELAY_CONNECTION_STATUS_ERROR
;
3054 } else if (ret
== 0) {
3055 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3056 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3061 assert(ret
<= state
->left_to_receive
);
3063 state
->left_to_receive
-= ret
;
3064 state
->received
+= ret
;
3066 if (state
->left_to_receive
> 0) {
3068 * Can't transition to the protocol's next state, wait to
3069 * receive the rest of the header.
3071 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3072 state
->received
, state
->left_to_receive
,
3078 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
3079 conn
->sock
->fd
, state
->received
);
3081 * The payload required to process the command has been received.
3082 * A view to the reception buffer is forwarded to the various
3083 * commands and the state of the control is reset on success.
3085 * Commands are responsible for sending their reply to the peer.
3087 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3089 ret
= relay_process_control_command(conn
,
3090 &state
->header
, &payload_view
);
3092 status
= RELAY_CONNECTION_STATUS_ERROR
;
3096 ret
= connection_reset_protocol_state(conn
);
3098 status
= RELAY_CONNECTION_STATUS_ERROR
;
3104 static enum relay_connection_status
relay_process_control_receive_header(
3105 struct relay_connection
*conn
)
3108 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3109 struct lttcomm_relayd_hdr header
;
3110 struct lttng_dynamic_buffer
*reception_buffer
=
3111 &conn
->protocol
.ctrl
.reception_buffer
;
3112 struct ctrl_connection_state_receive_header
*state
=
3113 &conn
->protocol
.ctrl
.state
.receive_header
;
3115 assert(state
->left_to_receive
!= 0);
3117 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3118 reception_buffer
->data
+ state
->received
,
3119 state
->left_to_receive
, MSG_DONTWAIT
);
3121 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3122 PERROR("Unable to receive control command header on sock %d",
3124 status
= RELAY_CONNECTION_STATUS_ERROR
;
3127 } else if (ret
== 0) {
3128 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3129 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3134 assert(ret
<= state
->left_to_receive
);
3136 state
->left_to_receive
-= ret
;
3137 state
->received
+= ret
;
3139 if (state
->left_to_receive
> 0) {
3141 * Can't transition to the protocol's next state, wait to
3142 * receive the rest of the header.
3144 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3145 state
->received
, state
->left_to_receive
,
3150 /* Transition to next state: receiving the command's payload. */
3151 conn
->protocol
.ctrl
.state_id
=
3152 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3153 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3154 header
.circuit_id
= be64toh(header
.circuit_id
);
3155 header
.data_size
= be64toh(header
.data_size
);
3156 header
.cmd
= be32toh(header
.cmd
);
3157 header
.cmd_version
= be32toh(header
.cmd_version
);
3158 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3159 &header
, sizeof(header
));
3161 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3162 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3165 if (header
.data_size
> DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE
) {
3166 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3168 status
= RELAY_CONNECTION_STATUS_ERROR
;
3172 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3174 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3175 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3178 status
= RELAY_CONNECTION_STATUS_ERROR
;
3182 if (header
.data_size
== 0) {
3184 * Manually invoke the next state as the poll loop
3185 * will not wake-up to allow us to proceed further.
3187 status
= relay_process_control_receive_payload(conn
);
3194 * Process the commands received on the control socket
3196 static enum relay_connection_status
relay_process_control(
3197 struct relay_connection
*conn
)
3199 enum relay_connection_status status
;
3201 switch (conn
->protocol
.ctrl
.state_id
) {
3202 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3203 status
= relay_process_control_receive_header(conn
);
3205 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3206 status
= relay_process_control_receive_payload(conn
);
3209 ERR("Unknown control connection protocol state encountered.");
3217 * Handle index for a data stream.
3219 * Called with the stream lock held.
3221 * Return 0 on success else a negative value.
3223 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
3224 bool rotate_index
, bool *flushed
, uint64_t total_size
)
3227 uint64_t data_offset
;
3228 struct relay_index
*index
;
3230 /* Get data offset because we are about to update the index. */
3231 data_offset
= htobe64(stream
->tracefile_size_current
);
3233 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
3234 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
3237 * Lookup for an existing index for that stream id/sequence
3238 * number. If it exists, the control thread has already received the
3239 * data for it, thus we need to write it to disk.
3241 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
3247 if (rotate_index
|| !stream
->index_file
) {
3248 ret
= create_rotate_index_file(stream
);
3250 ERR("Failed to rotate index");
3251 /* Put self-ref for this index due to error. */
3252 relay_index_put(index
);
3258 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
3260 /* Put self-ref for this index due to error. */
3261 relay_index_put(index
);
3266 ret
= relay_index_try_flush(index
);
3268 tracefile_array_commit_seq(stream
->tfa
);
3269 stream
->index_received_seqcount
++;
3271 } else if (ret
> 0) {
3272 index
->total_size
= total_size
;
3279 * relay_index_try_flush is responsible for the self-reference
3280 * put of the index object on error.
3282 ERR("relay_index_try_flush error %d", ret
);
3289 static enum relay_connection_status
relay_process_data_receive_header(
3290 struct relay_connection
*conn
)
3293 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3294 struct data_connection_state_receive_header
*state
=
3295 &conn
->protocol
.data
.state
.receive_header
;
3296 struct lttcomm_relayd_data_hdr header
;
3297 struct relay_stream
*stream
;
3299 assert(state
->left_to_receive
!= 0);
3301 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3302 state
->header_reception_buffer
+ state
->received
,
3303 state
->left_to_receive
, MSG_DONTWAIT
);
3305 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3306 PERROR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3307 status
= RELAY_CONNECTION_STATUS_ERROR
;
3310 } else if (ret
== 0) {
3311 /* Orderly shutdown. Not necessary to print an error. */
3312 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3313 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3318 assert(ret
<= state
->left_to_receive
);
3320 state
->left_to_receive
-= ret
;
3321 state
->received
+= ret
;
3323 if (state
->left_to_receive
> 0) {
3325 * Can't transition to the protocol's next state, wait to
3326 * receive the rest of the header.
3328 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3329 state
->received
, state
->left_to_receive
,
3334 /* Transition to next state: receiving the payload. */
3335 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3337 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3338 header
.circuit_id
= be64toh(header
.circuit_id
);
3339 header
.stream_id
= be64toh(header
.stream_id
);
3340 header
.data_size
= be32toh(header
.data_size
);
3341 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3342 header
.padding_size
= be32toh(header
.padding_size
);
3343 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3345 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3347 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3348 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3350 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3351 conn
->sock
->fd
, header
.circuit_id
,
3352 header
.stream_id
, header
.data_size
,
3353 header
.net_seq_num
, header
.padding_size
);
3355 stream
= stream_get_by_id(header
.stream_id
);
3357 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3359 /* Protocol error. */
3360 status
= RELAY_CONNECTION_STATUS_ERROR
;
3364 pthread_mutex_lock(&stream
->lock
);
3366 /* Check if a rotation is needed. */
3367 if (stream
->tracefile_size
> 0 &&
3368 (stream
->tracefile_size_current
+ header
.data_size
) >
3369 stream
->tracefile_size
) {
3370 uint64_t old_id
, new_id
;
3372 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
3373 tracefile_array_file_rotate(stream
->tfa
);
3375 /* new_id is updated by utils_rotate_stream_file. */
3378 ret
= utils_rotate_stream_file(stream
->path_name
,
3379 stream
->channel_name
, stream
->tracefile_size
,
3380 stream
->tracefile_count
, -1,
3381 -1, stream
->stream_fd
->fd
,
3382 &new_id
, &stream
->stream_fd
->fd
);
3384 ERR("Failed to rotate stream output file");
3385 status
= RELAY_CONNECTION_STATUS_ERROR
;
3386 goto end_stream_unlock
;
3390 * Reset current size because we just performed a stream
3393 stream
->tracefile_size_current
= 0;
3394 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= true;
3398 pthread_mutex_unlock(&stream
->lock
);
3404 static enum relay_connection_status
relay_process_data_receive_payload(
3405 struct relay_connection
*conn
)
3408 enum relay_connection_status status
= RELAY_CONNECTION_STATUS_OK
;
3409 struct relay_stream
*stream
;
3410 struct data_connection_state_receive_payload
*state
=
3411 &conn
->protocol
.data
.state
.receive_payload
;
3412 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3413 char data_buffer
[chunk_size
];
3414 bool partial_recv
= false;
3415 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3416 uint64_t left_to_receive
= state
->left_to_receive
;
3417 struct relay_session
*session
;
3419 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3420 state
->header
.stream_id
, state
->header
.net_seq_num
,
3421 state
->received
, left_to_receive
);
3423 stream
= stream_get_by_id(state
->header
.stream_id
);
3425 /* Protocol error. */
3426 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64
,
3427 state
->header
.stream_id
);
3428 status
= RELAY_CONNECTION_STATUS_ERROR
;
3432 pthread_mutex_lock(&stream
->lock
);
3433 session
= stream
->trace
->session
;
3434 if (!conn
->session
) {
3435 ret
= connection_set_session(conn
, session
);
3437 status
= RELAY_CONNECTION_STATUS_ERROR
;
3438 goto end_stream_unlock
;
3443 * The size of the "chunk" received on any iteration is bounded by:
3444 * - the data left to receive,
3445 * - the data immediately available on the socket,
3446 * - the on-stack data buffer
3448 while (left_to_receive
> 0 && !partial_recv
) {
3450 size_t recv_size
= min(left_to_receive
, chunk_size
);
3452 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3453 recv_size
, MSG_DONTWAIT
);
3455 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
3456 PERROR("Socket %d error", conn
->sock
->fd
);
3457 status
= RELAY_CONNECTION_STATUS_ERROR
;
3459 goto end_stream_unlock
;
3460 } else if (ret
== 0) {
3461 /* No more data ready to be consumed on socket. */
3462 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3463 state
->header
.stream_id
);
3464 status
= RELAY_CONNECTION_STATUS_CLOSED
;
3466 } else if (ret
< (int) recv_size
) {
3468 * All the data available on the socket has been
3471 partial_recv
= true;
3476 /* Write data to stream output fd. */
3477 write_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
3479 if (write_ret
< (ssize_t
) recv_size
) {
3480 ERR("Relay error writing data to file");
3481 status
= RELAY_CONNECTION_STATUS_ERROR
;
3482 goto end_stream_unlock
;
3485 left_to_receive
-= recv_size
;
3486 state
->received
+= recv_size
;
3487 state
->left_to_receive
= left_to_receive
;
3489 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
3490 write_ret
, stream
->stream_handle
);
3493 if (state
->left_to_receive
> 0) {
3495 * Did not receive all the data expected, wait for more data to
3496 * become available on the socket.
3498 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3499 state
->header
.stream_id
, state
->received
,
3500 state
->left_to_receive
);
3501 goto end_stream_unlock
;
3504 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
3505 state
->header
.padding_size
);
3506 if ((int64_t) ret
< (int64_t) state
->header
.padding_size
) {
3507 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3508 stream
->stream_handle
,
3509 state
->header
.net_seq_num
, ret
);
3510 status
= RELAY_CONNECTION_STATUS_ERROR
;
3511 goto end_stream_unlock
;
3515 if (session_streams_have_index(session
)) {
3516 ret
= handle_index_data(stream
, state
->header
.net_seq_num
,
3517 state
->rotate_index
, &index_flushed
, state
->header
.data_size
+ state
->header
.padding_size
);
3519 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3520 stream
->stream_handle
,
3521 state
->header
.net_seq_num
, ret
);
3522 status
= RELAY_CONNECTION_STATUS_ERROR
;
3523 goto end_stream_unlock
;
3527 stream
->tracefile_size_current
+= state
->header
.data_size
+
3528 state
->header
.padding_size
;
3530 if (stream
->prev_seq
== -1ULL) {
3533 if (index_flushed
) {
3534 stream
->pos_after_last_complete_data_index
=
3535 stream
->tracefile_size_current
;
3536 stream
->prev_index_seq
= state
->header
.net_seq_num
;
3539 stream
->prev_seq
= state
->header
.net_seq_num
;
3542 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3543 * contents of *state which are aliased (union) to the same location as
3544 * the new state. Don't use it beyond this point.
3546 connection_reset_protocol_state(conn
);
3549 ret
= try_rotate_stream(stream
);
3551 status
= RELAY_CONNECTION_STATUS_ERROR
;
3552 goto end_stream_unlock
;
3556 close_requested
= stream
->close_requested
;
3557 pthread_mutex_unlock(&stream
->lock
);
3558 if (close_requested
&& left_to_receive
== 0) {
3559 try_stream_close(stream
);
3563 pthread_mutex_lock(&session
->lock
);
3564 uatomic_set(&session
->new_streams
, 1);
3565 pthread_mutex_unlock(&session
->lock
);
3574 * relay_process_data: Process the data received on the data socket
3576 static enum relay_connection_status
relay_process_data(
3577 struct relay_connection
*conn
)
3579 enum relay_connection_status status
;
3581 switch (conn
->protocol
.data
.state_id
) {
3582 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3583 status
= relay_process_data_receive_header(conn
);
3585 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3586 status
= relay_process_data_receive_payload(conn
);
3589 ERR("Unexpected data connection communication state.");
3596 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3600 (void) lttng_poll_del(events
, pollfd
);
3602 ret
= close(pollfd
);
3604 ERR("Closing pollfd %d", pollfd
);
3608 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3609 int pollfd
, struct relay_connection
*conn
)
3611 const char *type_str
;
3613 switch (conn
->type
) {
3618 type_str
= "Control";
3620 case RELAY_VIEWER_COMMAND
:
3621 type_str
= "Viewer Command";
3623 case RELAY_VIEWER_NOTIFICATION
:
3624 type_str
= "Viewer Notification";
3627 type_str
= "Unknown";
3629 cleanup_connection_pollfd(events
, pollfd
);
3630 connection_put(conn
);
3631 DBG("%s connection closed with %d", type_str
, pollfd
);
3635 * This thread does the actual work
3637 static void *relay_thread_worker(void *data
)
3639 int ret
, err
= -1, last_seen_data_fd
= -1;
3641 struct lttng_poll_event events
;
3642 struct lttng_ht
*relay_connections_ht
;
3643 struct lttng_ht_iter iter
;
3644 struct relay_connection
*destroy_conn
= NULL
;
3646 DBG("[thread] Relay worker started");
3648 rcu_register_thread();
3650 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3652 if (testpoint(relayd_thread_worker
)) {
3653 goto error_testpoint
;
3656 health_code_update();
3658 /* table of connections indexed on socket */
3659 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3660 if (!relay_connections_ht
) {
3661 goto relay_connections_ht_error
;
3664 ret
= create_thread_poll_set(&events
, 2);
3666 goto error_poll_create
;
3669 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3676 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -