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 /* command line options */
87 char *opt_output_path
;
88 static int opt_daemon
, opt_background
;
91 * We need to wait for listener and live listener threads, as well as
92 * health check thread, before being ready to signal readiness.
94 #define NR_LTTNG_RELAY_READY 3
95 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
97 /* Size of receive buffer. */
98 #define RECV_DATA_BUFFER_SIZE 65536
99 #define FILE_COPY_BUFFER_SIZE 65536
101 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
102 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
104 static struct lttng_uri
*control_uri
;
105 static struct lttng_uri
*data_uri
;
106 static struct lttng_uri
*live_uri
;
108 const char *progname
;
110 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
111 static int tracing_group_name_override
;
113 const char * const config_section_name
= "relayd";
116 * Quit pipe for all threads. This permits a single cancellation point
117 * for all threads when receiving an event on the pipe.
119 int thread_quit_pipe
[2] = { -1, -1 };
122 * This pipe is used to inform the worker thread that a command is queued and
123 * ready to be processed.
125 static int relay_conn_pipe
[2] = { -1, -1 };
127 /* Shared between threads */
128 static int dispatch_thread_exit
;
130 static pthread_t listener_thread
;
131 static pthread_t dispatcher_thread
;
132 static pthread_t worker_thread
;
133 static pthread_t health_thread
;
136 * last_relay_stream_id_lock protects last_relay_stream_id increment
137 * atomicity on 32-bit architectures.
139 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
140 static uint64_t last_relay_stream_id
;
143 * Relay command queue.
145 * The relay_thread_listener and relay_thread_dispatcher communicate with this
148 static struct relay_conn_queue relay_conn_queue
;
150 /* Global relay stream hash table. */
151 struct lttng_ht
*relay_streams_ht
;
153 /* Global relay viewer stream hash table. */
154 struct lttng_ht
*viewer_streams_ht
;
156 /* Global relay sessions hash table. */
157 struct lttng_ht
*sessions_ht
;
159 /* Relayd health monitoring */
160 struct health_app
*health_relayd
;
162 static struct option long_options
[] = {
163 { "control-port", 1, 0, 'C', },
164 { "data-port", 1, 0, 'D', },
165 { "live-port", 1, 0, 'L', },
166 { "daemonize", 0, 0, 'd', },
167 { "background", 0, 0, 'b', },
168 { "group", 1, 0, 'g', },
169 { "help", 0, 0, 'h', },
170 { "output", 1, 0, 'o', },
171 { "verbose", 0, 0, 'v', },
172 { "config", 1, 0, 'f' },
173 { "version", 0, 0, 'V' },
177 static const char *config_ignore_options
[] = { "help", "config", "version" };
180 * Take an option from the getopt output and set it in the right variable to be
183 * Return 0 on success else a negative value.
185 static int set_option(int opt
, const char *arg
, const char *optname
)
191 fprintf(stderr
, "option %s", optname
);
193 fprintf(stderr
, " with arg %s\n", arg
);
197 if (lttng_is_setuid_setgid()) {
198 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
199 "-C, --control-port");
201 ret
= uri_parse(arg
, &control_uri
);
203 ERR("Invalid control URI specified");
206 if (control_uri
->port
== 0) {
207 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
212 if (lttng_is_setuid_setgid()) {
213 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
216 ret
= uri_parse(arg
, &data_uri
);
218 ERR("Invalid data URI specified");
221 if (data_uri
->port
== 0) {
222 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
227 if (lttng_is_setuid_setgid()) {
228 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
231 ret
= uri_parse(arg
, &live_uri
);
233 ERR("Invalid live URI specified");
236 if (live_uri
->port
== 0) {
237 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
248 if (lttng_is_setuid_setgid()) {
249 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
252 tracing_group_name
= strdup(arg
);
253 if (tracing_group_name
== NULL
) {
258 tracing_group_name_override
= 1;
262 ret
= utils_show_help(8, "lttng-relayd", help_msg
);
264 ERR("Cannot show --help for `lttng-relayd`");
269 fprintf(stdout
, "%s\n", VERSION
);
272 if (lttng_is_setuid_setgid()) {
273 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
276 ret
= asprintf(&opt_output_path
, "%s", arg
);
279 PERROR("asprintf opt_output_path");
285 /* Verbose level can increase using multiple -v */
287 lttng_opt_verbose
= config_parse_value(arg
);
289 /* Only 3 level of verbosity (-vvv). */
290 if (lttng_opt_verbose
< 3) {
291 lttng_opt_verbose
+= 1;
296 /* Unknown option or other error.
297 * Error is printed by getopt, just return */
310 * config_entry_handler_cb used to handle options read from a config file.
311 * See config_entry_handler_cb comment in common/config/session-config.h for the
312 * return value conventions.
314 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
318 if (!entry
|| !entry
->name
|| !entry
->value
) {
323 /* Check if the option is to be ignored */
324 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
325 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
330 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
331 /* Ignore if entry name is not fully matched. */
332 if (strcmp(entry
->name
, long_options
[i
].name
)) {
337 * If the option takes no argument on the command line,
338 * we have to check if the value is "true". We support
339 * non-zero numeric values, true, on and yes.
341 if (!long_options
[i
].has_arg
) {
342 ret
= config_parse_value(entry
->value
);
345 WARN("Invalid configuration value \"%s\" for option %s",
346 entry
->value
, entry
->name
);
348 /* False, skip boolean config option. */
353 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
357 WARN("Unrecognized option \"%s\" in daemon configuration file.",
364 static int set_options(int argc
, char **argv
)
366 int c
, ret
= 0, option_index
= 0, retval
= 0;
367 int orig_optopt
= optopt
, orig_optind
= optind
;
368 char *default_address
, *optstring
;
369 const char *config_path
= NULL
;
371 optstring
= utils_generate_optstring(long_options
,
372 sizeof(long_options
) / sizeof(struct option
));
378 /* Check for the --config option */
380 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
381 &option_index
)) != -1) {
385 } else if (c
!= 'f') {
389 if (lttng_is_setuid_setgid()) {
390 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
393 config_path
= utils_expand_path(optarg
);
395 ERR("Failed to resolve path: %s", optarg
);
400 ret
= config_get_section_entries(config_path
, config_section_name
,
401 config_entry_handler
, NULL
);
404 ERR("Invalid configuration option at line %i", ret
);
410 /* Reset getopt's global state */
411 optopt
= orig_optopt
;
412 optind
= orig_optind
;
414 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
419 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
426 /* assign default values */
427 if (control_uri
== NULL
) {
428 ret
= asprintf(&default_address
,
429 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
430 DEFAULT_NETWORK_CONTROL_PORT
);
432 PERROR("asprintf default data address");
437 ret
= uri_parse(default_address
, &control_uri
);
438 free(default_address
);
440 ERR("Invalid control URI specified");
445 if (data_uri
== NULL
) {
446 ret
= asprintf(&default_address
,
447 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
448 DEFAULT_NETWORK_DATA_PORT
);
450 PERROR("asprintf default data address");
455 ret
= uri_parse(default_address
, &data_uri
);
456 free(default_address
);
458 ERR("Invalid data URI specified");
463 if (live_uri
== NULL
) {
464 ret
= asprintf(&default_address
,
465 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
466 DEFAULT_NETWORK_VIEWER_PORT
);
468 PERROR("asprintf default viewer control address");
473 ret
= uri_parse(default_address
, &live_uri
);
474 free(default_address
);
476 ERR("Invalid viewer control URI specified");
487 static void print_global_objects(void)
489 rcu_register_thread();
491 print_viewer_streams();
492 print_relay_streams();
495 rcu_unregister_thread();
501 static void relayd_cleanup(void)
503 print_global_objects();
507 if (viewer_streams_ht
)
508 lttng_ht_destroy(viewer_streams_ht
);
509 if (relay_streams_ht
)
510 lttng_ht_destroy(relay_streams_ht
);
512 lttng_ht_destroy(sessions_ht
);
514 /* free the dynamically allocated opt_output_path */
515 free(opt_output_path
);
517 /* Close thread quit pipes */
518 utils_close_pipe(thread_quit_pipe
);
520 uri_free(control_uri
);
522 /* Live URI is freed in the live thread. */
524 if (tracing_group_name_override
) {
525 free((void *) tracing_group_name
);
530 * Write to writable pipe used to notify a thread.
532 static int notify_thread_pipe(int wpipe
)
536 ret
= lttng_write(wpipe
, "!", 1);
538 PERROR("write poll pipe");
546 static int notify_health_quit_pipe(int *pipe
)
550 ret
= lttng_write(pipe
[1], "4", 1);
552 PERROR("write relay health quit");
561 * Stop all relayd and relayd-live threads.
563 int lttng_relay_stop_threads(void)
567 /* Stopping all threads */
568 DBG("Terminating all threads");
569 if (notify_thread_pipe(thread_quit_pipe
[1])) {
570 ERR("write error on thread quit pipe");
574 if (notify_health_quit_pipe(health_quit_pipe
)) {
575 ERR("write error on health quit pipe");
578 /* Dispatch thread */
579 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
580 futex_nto1_wake(&relay_conn_queue
.futex
);
582 if (relayd_live_stop()) {
583 ERR("Error stopping live threads");
590 * Signal handler for the daemon
592 * Simply stop all worker threads, leaving main() return gracefully after
593 * joining all threads and calling cleanup().
595 static void sighandler(int sig
)
599 DBG("SIGINT caught");
600 if (lttng_relay_stop_threads()) {
601 ERR("Error stopping threads");
605 DBG("SIGTERM caught");
606 if (lttng_relay_stop_threads()) {
607 ERR("Error stopping threads");
611 CMM_STORE_SHARED(recv_child_signal
, 1);
619 * Setup signal handler for :
620 * SIGINT, SIGTERM, SIGPIPE
622 static int set_signal_handler(void)
628 if ((ret
= sigemptyset(&sigset
)) < 0) {
629 PERROR("sigemptyset");
636 sa
.sa_handler
= sighandler
;
637 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
642 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
647 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
652 sa
.sa_handler
= SIG_IGN
;
653 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
658 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
663 void lttng_relay_notify_ready(void)
665 /* Notify the parent of the fork() process that we are ready. */
666 if (opt_daemon
|| opt_background
) {
667 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
668 kill(child_ppid
, SIGUSR1
);
674 * Init thread quit pipe.
676 * Return -1 on error or 0 if all pipes are created.
678 static int init_thread_quit_pipe(void)
682 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
688 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
690 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
694 if (events
== NULL
|| size
== 0) {
699 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
705 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
717 * Check if the thread quit pipe was triggered.
719 * Return 1 if it was triggered else 0;
721 static int check_thread_quit_pipe(int fd
, uint32_t events
)
723 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
731 * Create and init socket from uri.
733 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
736 struct lttcomm_sock
*sock
= NULL
;
738 sock
= lttcomm_alloc_sock_from_uri(uri
);
740 ERR("Allocating socket");
744 ret
= lttcomm_create_sock(sock
);
748 DBG("Listening on sock %d", sock
->fd
);
750 ret
= sock
->ops
->bind(sock
);
752 PERROR("Failed to bind socket");
756 ret
= sock
->ops
->listen(sock
, -1);
766 lttcomm_destroy_sock(sock
);
772 * This thread manages the listening for new connections on the network
774 static void *relay_thread_listener(void *data
)
776 int i
, ret
, pollfd
, err
= -1;
777 uint32_t revents
, nb_fd
;
778 struct lttng_poll_event events
;
779 struct lttcomm_sock
*control_sock
, *data_sock
;
781 DBG("[thread] Relay listener started");
783 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
785 health_code_update();
787 control_sock
= relay_socket_create(control_uri
);
789 goto error_sock_control
;
792 data_sock
= relay_socket_create(data_uri
);
794 goto error_sock_relay
;
798 * Pass 3 as size here for the thread quit pipe, control and
801 ret
= create_thread_poll_set(&events
, 3);
803 goto error_create_poll
;
806 /* Add the control socket */
807 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
812 /* Add the data socket */
813 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
818 lttng_relay_notify_ready();
820 if (testpoint(relayd_thread_listener
)) {
821 goto error_testpoint
;
825 health_code_update();
827 DBG("Listener accepting connections");
831 ret
= lttng_poll_wait(&events
, -1);
835 * Restart interrupted system call.
837 if (errno
== EINTR
) {
845 DBG("Relay new connection received");
846 for (i
= 0; i
< nb_fd
; i
++) {
847 health_code_update();
849 /* Fetch once the poll data */
850 revents
= LTTNG_POLL_GETEV(&events
, i
);
851 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
855 * No activity for this FD (poll
861 /* Thread quit pipe has been closed. Killing thread. */
862 ret
= check_thread_quit_pipe(pollfd
, revents
);
868 if (revents
& LPOLLIN
) {
870 * A new connection is requested, therefore a
871 * sessiond/consumerd connection is allocated in
872 * this thread, enqueued to a global queue and
873 * dequeued (and freed) in the worker thread.
876 struct relay_connection
*new_conn
;
877 struct lttcomm_sock
*newsock
;
878 enum connection_type type
;
880 if (pollfd
== data_sock
->fd
) {
882 newsock
= data_sock
->ops
->accept(data_sock
);
883 DBG("Relay data connection accepted, socket %d",
886 assert(pollfd
== control_sock
->fd
);
887 type
= RELAY_CONTROL
;
888 newsock
= control_sock
->ops
->accept(control_sock
);
889 DBG("Relay control connection accepted, socket %d",
893 PERROR("accepting sock");
897 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
900 PERROR("setsockopt inet");
901 lttcomm_destroy_sock(newsock
);
905 ret
= socket_apply_keep_alive_config(newsock
->fd
);
907 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
909 lttcomm_destroy_sock(newsock
);
913 new_conn
= connection_create(newsock
, type
);
915 lttcomm_destroy_sock(newsock
);
919 /* Enqueue request for the dispatcher thread. */
920 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
924 * Wake the dispatch queue futex.
925 * Implicit memory barrier with the
926 * exchange in cds_wfcq_enqueue.
928 futex_nto1_wake(&relay_conn_queue
.futex
);
929 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
930 ERR("socket poll error");
933 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
943 lttng_poll_clean(&events
);
945 if (data_sock
->fd
>= 0) {
946 ret
= data_sock
->ops
->close(data_sock
);
951 lttcomm_destroy_sock(data_sock
);
953 if (control_sock
->fd
>= 0) {
954 ret
= control_sock
->ops
->close(control_sock
);
959 lttcomm_destroy_sock(control_sock
);
963 ERR("Health error occurred in %s", __func__
);
965 health_unregister(health_relayd
);
966 DBG("Relay listener thread cleanup complete");
967 lttng_relay_stop_threads();
972 * This thread manages the dispatching of the requests to worker threads
974 static void *relay_thread_dispatcher(void *data
)
978 struct cds_wfcq_node
*node
;
979 struct relay_connection
*new_conn
= NULL
;
981 DBG("[thread] Relay dispatcher started");
983 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
985 if (testpoint(relayd_thread_dispatcher
)) {
986 goto error_testpoint
;
989 health_code_update();
992 health_code_update();
994 /* Atomically prepare the queue futex */
995 futex_nto1_prepare(&relay_conn_queue
.futex
);
997 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1002 health_code_update();
1004 /* Dequeue commands */
1005 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
1006 &relay_conn_queue
.tail
);
1008 DBG("Woken up but nothing in the relay command queue");
1009 /* Continue thread execution */
1012 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
1014 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
1017 * Inform worker thread of the new request. This
1018 * call is blocking so we can be assured that
1019 * the data will be read at some point in time
1020 * or wait to the end of the world :)
1022 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
1024 PERROR("write connection pipe");
1025 connection_put(new_conn
);
1028 } while (node
!= NULL
);
1030 /* Futex wait on queue. Blocking call on futex() */
1031 health_poll_entry();
1032 futex_nto1_wait(&relay_conn_queue
.futex
);
1036 /* Normal exit, no error */
1043 ERR("Health error occurred in %s", __func__
);
1045 health_unregister(health_relayd
);
1046 DBG("Dispatch thread dying");
1047 lttng_relay_stop_threads();
1052 * Set index data from the control port to a given index object.
1054 static int set_index_control_data(struct relay_index
*index
,
1055 struct lttcomm_relayd_index
*data
,
1056 struct relay_connection
*conn
)
1058 struct ctf_packet_index index_data
;
1061 * The index on disk is encoded in big endian.
1063 index_data
.packet_size
= htobe64(data
->packet_size
);
1064 index_data
.content_size
= htobe64(data
->content_size
);
1065 index_data
.timestamp_begin
= htobe64(data
->timestamp_begin
);
1066 index_data
.timestamp_end
= htobe64(data
->timestamp_end
);
1067 index_data
.events_discarded
= htobe64(data
->events_discarded
);
1068 index_data
.stream_id
= htobe64(data
->stream_id
);
1070 if (conn
->minor
>= 8) {
1071 index
->index_data
.stream_instance_id
= htobe64(data
->stream_instance_id
);
1072 index
->index_data
.packet_seq_num
= htobe64(data
->packet_seq_num
);
1075 return relay_index_set_data(index
, &index_data
);
1079 * Handle the RELAYD_CREATE_SESSION command.
1081 * On success, send back the session id or else return a negative value.
1083 static int relay_create_session(const struct lttcomm_relayd_hdr
*recv_hdr
,
1084 struct relay_connection
*conn
,
1085 const struct lttng_buffer_view
*payload
)
1089 struct relay_session
*session
;
1090 struct lttcomm_relayd_status_session reply
;
1091 char session_name
[LTTNG_NAME_MAX
];
1092 char hostname
[LTTNG_HOST_NAME_MAX
];
1093 uint32_t live_timer
= 0;
1094 bool snapshot
= false;
1096 memset(session_name
, 0, LTTNG_NAME_MAX
);
1097 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1099 memset(&reply
, 0, sizeof(reply
));
1101 switch (conn
->minor
) {
1106 case 4: /* LTTng sessiond 2.4 */
1108 ret
= cmd_create_session_2_4(payload
, session_name
,
1109 hostname
, &live_timer
, &snapshot
);
1115 session
= session_create(session_name
, hostname
, live_timer
,
1116 snapshot
, conn
->major
, conn
->minor
);
1121 assert(!conn
->session
);
1122 conn
->session
= session
;
1123 DBG("Created session %" PRIu64
, session
->id
);
1125 reply
.session_id
= htobe64(session
->id
);
1129 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1131 reply
.ret_code
= htobe32(LTTNG_OK
);
1134 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1135 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1136 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1145 * When we have received all the streams and the metadata for a channel,
1146 * we make them visible to the viewer threads.
1148 static void publish_connection_local_streams(struct relay_connection
*conn
)
1150 struct relay_stream
*stream
;
1151 struct relay_session
*session
= conn
->session
;
1154 * We publish all streams belonging to a session atomically wrt
1157 pthread_mutex_lock(&session
->lock
);
1159 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1161 stream_publish(stream
);
1166 * Inform the viewer that there are new streams in the session.
1168 if (session
->viewer_attached
) {
1169 uatomic_set(&session
->new_streams
, 1);
1171 pthread_mutex_unlock(&session
->lock
);
1175 * relay_add_stream: allocate a new stream for a session
1177 static int relay_add_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1178 struct relay_connection
*conn
,
1179 const struct lttng_buffer_view
*payload
)
1183 struct relay_session
*session
= conn
->session
;
1184 struct relay_stream
*stream
= NULL
;
1185 struct lttcomm_relayd_status_stream reply
;
1186 struct ctf_trace
*trace
= NULL
;
1187 uint64_t stream_handle
= -1ULL;
1188 char *path_name
= NULL
, *channel_name
= NULL
;
1189 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1191 if (!session
|| !conn
->version_check_done
) {
1192 ERR("Trying to add a stream before version check");
1194 goto end_no_session
;
1197 switch (session
->minor
) {
1198 case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */
1199 ret
= cmd_recv_stream_2_1(payload
, &path_name
,
1202 case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
1204 ret
= cmd_recv_stream_2_2(payload
, &path_name
,
1205 &channel_name
, &tracefile_size
, &tracefile_count
);
1212 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1216 /* This stream here has one reference on the trace. */
1218 pthread_mutex_lock(&last_relay_stream_id_lock
);
1219 stream_handle
= ++last_relay_stream_id
;
1220 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1222 /* We pass ownership of path_name and channel_name. */
1223 stream
= stream_create(trace
, stream_handle
, path_name
,
1224 channel_name
, tracefile_size
, tracefile_count
);
1226 channel_name
= NULL
;
1229 * Streams are the owners of their trace. Reference to trace is
1230 * kept within stream_create().
1232 ctf_trace_put(trace
);
1235 memset(&reply
, 0, sizeof(reply
));
1236 reply
.handle
= htobe64(stream_handle
);
1238 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1240 reply
.ret_code
= htobe32(LTTNG_OK
);
1243 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1244 sizeof(struct lttcomm_relayd_status_stream
), 0);
1245 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1246 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1258 * relay_close_stream: close a specific stream
1260 static int relay_close_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
1261 struct relay_connection
*conn
,
1262 const struct lttng_buffer_view
*payload
)
1266 struct relay_session
*session
= conn
->session
;
1267 struct lttcomm_relayd_close_stream stream_info
;
1268 struct lttcomm_relayd_generic_reply reply
;
1269 struct relay_stream
*stream
;
1271 DBG("Close stream received");
1273 if (!session
|| !conn
->version_check_done
) {
1274 ERR("Trying to close a stream before version check");
1276 goto end_no_session
;
1279 if (payload
->size
< sizeof(stream_info
)) {
1280 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1281 sizeof(stream_info
), payload
->size
);
1283 goto end_no_session
;
1285 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1286 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1287 stream_info
.last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1289 stream
= stream_get_by_id(stream_info
.stream_id
);
1296 * Set last_net_seq_num before the close flag. Required by data
1299 pthread_mutex_lock(&stream
->lock
);
1300 stream
->last_net_seq_num
= stream_info
.last_net_seq_num
;
1301 pthread_mutex_unlock(&stream
->lock
);
1304 * This is one of the conditions which may trigger a stream close
1305 * with the others being:
1306 * 1) A close command is received for a stream
1307 * 2) The control connection owning the stream is closed
1308 * 3) We have received all of the stream's data _after_ a close
1311 try_stream_close(stream
);
1312 if (stream
->is_metadata
) {
1313 struct relay_viewer_stream
*vstream
;
1315 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1317 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1319 * Since all the metadata has been sent to the
1320 * viewer and that we have a request to close
1321 * its stream, we can safely teardown the
1322 * corresponding metadata viewer stream.
1324 viewer_stream_put(vstream
);
1326 /* Put local reference. */
1327 viewer_stream_put(vstream
);
1334 memset(&reply
, 0, sizeof(reply
));
1336 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1338 reply
.ret_code
= htobe32(LTTNG_OK
);
1340 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1341 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1342 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1343 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1353 * relay_reset_metadata: reset a metadata stream
1356 int relay_reset_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1357 struct relay_connection
*conn
,
1358 const struct lttng_buffer_view
*payload
)
1362 struct relay_session
*session
= conn
->session
;
1363 struct lttcomm_relayd_reset_metadata stream_info
;
1364 struct lttcomm_relayd_generic_reply reply
;
1365 struct relay_stream
*stream
;
1367 DBG("Reset metadata received");
1369 if (!session
|| !conn
->version_check_done
) {
1370 ERR("Trying to reset a metadata stream before version check");
1372 goto end_no_session
;
1375 if (payload
->size
< sizeof(stream_info
)) {
1376 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1377 sizeof(stream_info
), payload
->size
);
1379 goto end_no_session
;
1381 memcpy(&stream_info
, payload
->data
, sizeof(stream_info
));
1382 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
1383 stream_info
.version
= be64toh(stream_info
.version
);
1385 DBG("Update metadata to version %" PRIu64
, stream_info
.version
);
1387 /* Unsupported for live sessions for now. */
1388 if (session
->live_timer
!= 0) {
1393 stream
= stream_get_by_id(stream_info
.stream_id
);
1398 pthread_mutex_lock(&stream
->lock
);
1399 if (!stream
->is_metadata
) {
1404 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1405 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1406 &stream
->stream_fd
->fd
);
1408 ERR("Failed to rotate metadata file %s of channel %s",
1409 stream
->path_name
, stream
->channel_name
);
1414 pthread_mutex_unlock(&stream
->lock
);
1418 memset(&reply
, 0, sizeof(reply
));
1420 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1422 reply
.ret_code
= htobe32(LTTNG_OK
);
1424 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1425 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1426 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1427 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1437 * relay_unknown_command: send -1 if received unknown command
1439 static void relay_unknown_command(struct relay_connection
*conn
)
1441 struct lttcomm_relayd_generic_reply reply
;
1444 memset(&reply
, 0, sizeof(reply
));
1445 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1446 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1447 if (send_ret
< sizeof(reply
)) {
1448 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret
);
1453 * relay_start: send an acknowledgment to the client to tell if we are
1454 * ready to receive data. We are ready if a session is established.
1456 static int relay_start(const struct lttcomm_relayd_hdr
*recv_hdr
,
1457 struct relay_connection
*conn
,
1458 const struct lttng_buffer_view
*payload
)
1462 struct lttcomm_relayd_generic_reply reply
;
1463 struct relay_session
*session
= conn
->session
;
1466 DBG("Trying to start the streaming without a session established");
1467 ret
= htobe32(LTTNG_ERR_UNK
);
1470 memset(&reply
, 0, sizeof(reply
));
1471 reply
.ret_code
= htobe32(LTTNG_OK
);
1472 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1474 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1475 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1484 * Append padding to the file pointed by the file descriptor fd.
1486 static int write_padding_to_file(int fd
, uint32_t size
)
1495 zeros
= zmalloc(size
);
1496 if (zeros
== NULL
) {
1497 PERROR("zmalloc zeros for padding");
1502 ret
= lttng_write(fd
, zeros
, size
);
1504 PERROR("write padding to file");
1514 * Close the current index file if it is open, and create a new one.
1516 * Return 0 on success, -1 on error.
1519 int create_rotate_index_file(struct relay_stream
*stream
)
1522 uint32_t major
, minor
;
1524 /* Put ref on previous index_file. */
1525 if (stream
->index_file
) {
1526 lttng_index_file_put(stream
->index_file
);
1527 stream
->index_file
= NULL
;
1529 major
= stream
->trace
->session
->major
;
1530 minor
= stream
->trace
->session
->minor
;
1531 stream
->index_file
= lttng_index_file_create(stream
->path_name
,
1532 stream
->channel_name
,
1533 -1, -1, stream
->tracefile_size
,
1534 tracefile_array_get_file_index_head(stream
->tfa
),
1535 lttng_to_index_major(major
, minor
),
1536 lttng_to_index_minor(major
, minor
));
1537 if (!stream
->index_file
) {
1549 int do_rotate_stream(struct relay_stream
*stream
)
1553 /* Perform the stream rotation. */
1554 ret
= utils_rotate_stream_file(stream
->path_name
,
1555 stream
->channel_name
, stream
->tracefile_size
,
1556 stream
->tracefile_count
, -1,
1557 -1, stream
->stream_fd
->fd
,
1558 NULL
, &stream
->stream_fd
->fd
);
1560 ERR("Rotating stream output file");
1563 stream
->tracefile_size_current
= 0;
1565 /* Rotate also the index if the stream is not a metadata stream. */
1566 if (!stream
->is_metadata
) {
1567 ret
= create_rotate_index_file(stream
);
1569 ERR("Failed to rotate index file");
1574 stream
->rotate_at_seq_num
= -1ULL;
1575 stream
->pos_after_last_complete_data_index
= 0;
1582 * If too much data has been written in a tracefile before we received the
1583 * rotation command, we have to move the excess data to the new tracefile and
1584 * perform the rotation. This can happen because the control and data
1585 * connections are separate, the indexes as well as the commands arrive from
1586 * the control connection and we have no control over the order so we could be
1587 * in a situation where too much data has been received on the data connection
1588 * before the rotation command on the control connection arrives. We don't need
1589 * to update the index because its order is guaranteed with the rotation
1593 int rotate_truncate_stream(struct relay_stream
*stream
)
1597 uint64_t diff
, pos
= 0;
1598 char buf
[FILE_COPY_BUFFER_SIZE
];
1600 assert(!stream
->is_metadata
);
1602 assert(stream
->tracefile_size_current
>
1603 stream
->pos_after_last_complete_data_index
);
1604 diff
= stream
->tracefile_size_current
-
1605 stream
->pos_after_last_complete_data_index
;
1607 /* Create the new tracefile. */
1608 new_fd
= utils_create_stream_file(stream
->path_name
,
1609 stream
->channel_name
,
1610 stream
->tracefile_size
, stream
->tracefile_count
,
1611 /* uid */ -1, /* gid */ -1, /* suffix */ NULL
);
1613 ERR("Failed to create new stream file at path %s for channel %s",
1614 stream
->path_name
, stream
->channel_name
);
1620 * Rewind the current tracefile to the position at which the rotation
1621 * should have occured.
1623 lseek_ret
= lseek(stream
->stream_fd
->fd
,
1624 stream
->pos_after_last_complete_data_index
, SEEK_SET
);
1625 if (lseek_ret
< 0) {
1626 PERROR("seek truncate stream");
1631 /* Move data from the old file to the new file. */
1632 while (pos
< diff
) {
1633 uint64_t count
, bytes_left
;
1636 bytes_left
= diff
- pos
;
1637 count
= bytes_left
> sizeof(buf
) ? sizeof(buf
) : bytes_left
;
1638 assert(count
<= SIZE_MAX
);
1640 io_ret
= lttng_read(stream
->stream_fd
->fd
, buf
, count
);
1641 if (io_ret
< (ssize_t
) count
) {
1642 char error_string
[256];
1644 snprintf(error_string
, sizeof(error_string
),
1645 "Failed to read %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1646 count
, stream
->stream_fd
->fd
, io_ret
);
1648 PERROR("%s", error_string
);
1650 ERR("%s", error_string
);
1656 io_ret
= lttng_write(new_fd
, buf
, count
);
1657 if (io_ret
< (ssize_t
) count
) {
1658 char error_string
[256];
1660 snprintf(error_string
, sizeof(error_string
),
1661 "Failed to write %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
1662 count
, new_fd
, io_ret
);
1664 PERROR("%s", error_string
);
1666 ERR("%s", error_string
);
1675 /* Truncate the file to get rid of the excess data. */
1676 ret
= ftruncate(stream
->stream_fd
->fd
,
1677 stream
->pos_after_last_complete_data_index
);
1679 PERROR("ftruncate");
1683 ret
= close(stream
->stream_fd
->fd
);
1685 PERROR("Closing tracefile");
1689 ret
= create_rotate_index_file(stream
);
1691 ERR("Rotate stream index file");
1696 * Update the offset and FD of all the eventual indexes created by the
1697 * data connection before the rotation command arrived.
1699 ret
= relay_index_switch_all_files(stream
);
1701 ERR("Failed to rotate index file");
1705 stream
->stream_fd
->fd
= new_fd
;
1706 stream
->tracefile_size_current
= diff
;
1707 stream
->pos_after_last_complete_data_index
= 0;
1708 stream
->rotate_at_seq_num
= -1ULL;
1717 * Check if a stream should perform a rotation (for session rotation).
1718 * Must be called with the stream lock held.
1720 * Return 0 on success, a negative value on error.
1723 int try_rotate_stream(struct relay_stream
*stream
)
1727 /* No rotation expected. */
1728 if (stream
->rotate_at_seq_num
== -1ULL) {
1732 if (stream
->prev_seq
< stream
->rotate_at_seq_num
||
1733 stream
->prev_seq
== -1ULL) {
1734 DBG("Stream %" PRIu64
" no yet ready for rotation",
1735 stream
->stream_handle
);
1737 } else if (stream
->prev_seq
> stream
->rotate_at_seq_num
) {
1738 DBG("Rotation after too much data has been written in tracefile "
1739 "for stream %" PRIu64
", need to truncate before "
1740 "rotating", stream
->stream_handle
);
1741 ret
= rotate_truncate_stream(stream
);
1743 ERR("Failed to truncate stream");
1747 /* stream->prev_seq == stream->rotate_at_seq_num */
1748 DBG("Stream %" PRIu64
" ready for rotation",
1749 stream
->stream_handle
);
1750 ret
= do_rotate_stream(stream
);
1758 * relay_recv_metadata: receive the metadata for the session.
1760 static int relay_recv_metadata(const struct lttcomm_relayd_hdr
*recv_hdr
,
1761 struct relay_connection
*conn
,
1762 const struct lttng_buffer_view
*payload
)
1766 struct relay_session
*session
= conn
->session
;
1767 struct lttcomm_relayd_metadata_payload metadata_payload_header
;
1768 struct relay_stream
*metadata_stream
;
1769 uint64_t metadata_payload_size
;
1772 ERR("Metadata sent before version check");
1777 if (recv_hdr
->data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1778 ERR("Incorrect data size");
1782 metadata_payload_size
= recv_hdr
->data_size
-
1783 sizeof(struct lttcomm_relayd_metadata_payload
);
1785 memcpy(&metadata_payload_header
, payload
->data
,
1786 sizeof(metadata_payload_header
));
1787 metadata_payload_header
.stream_id
= be64toh(
1788 metadata_payload_header
.stream_id
);
1789 metadata_payload_header
.padding_size
= be32toh(
1790 metadata_payload_header
.padding_size
);
1792 metadata_stream
= stream_get_by_id(metadata_payload_header
.stream_id
);
1793 if (!metadata_stream
) {
1798 pthread_mutex_lock(&metadata_stream
->lock
);
1800 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
,
1801 payload
->data
+ sizeof(metadata_payload_header
),
1802 metadata_payload_size
);
1803 if (size_ret
< metadata_payload_size
) {
1804 ERR("Relay error writing metadata on file");
1809 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1810 metadata_payload_header
.padding_size
);
1816 metadata_stream
->metadata_received
+=
1817 metadata_payload_size
+ metadata_payload_header
.padding_size
;
1818 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1819 metadata_stream
->metadata_received
);
1821 ret
= try_rotate_stream(metadata_stream
);
1827 pthread_mutex_unlock(&metadata_stream
->lock
);
1828 stream_put(metadata_stream
);
1834 * relay_send_version: send relayd version number
1836 static int relay_send_version(const struct lttcomm_relayd_hdr
*recv_hdr
,
1837 struct relay_connection
*conn
,
1838 const struct lttng_buffer_view
*payload
)
1842 struct lttcomm_relayd_version reply
, msg
;
1843 bool compatible
= true;
1845 conn
->version_check_done
= true;
1847 /* Get version from the other side. */
1848 if (payload
->size
< sizeof(msg
)) {
1849 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1850 sizeof(msg
), payload
->size
);
1855 memcpy(&msg
, payload
->data
, sizeof(msg
));
1856 msg
.major
= be32toh(msg
.major
);
1857 msg
.minor
= be32toh(msg
.minor
);
1859 memset(&reply
, 0, sizeof(reply
));
1860 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1861 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1863 /* Major versions must be the same */
1864 if (reply
.major
!= msg
.major
) {
1865 DBG("Incompatible major versions (%u vs %u), deleting session",
1866 reply
.major
, msg
.major
);
1870 conn
->major
= reply
.major
;
1871 /* We adapt to the lowest compatible version */
1872 if (reply
.minor
<= msg
.minor
) {
1873 conn
->minor
= reply
.minor
;
1875 conn
->minor
= msg
.minor
;
1878 reply
.major
= htobe32(reply
.major
);
1879 reply
.minor
= htobe32(reply
.minor
);
1880 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1882 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1883 ERR("Failed to send \"send version\" command reply (ret = %zd)",
1896 DBG("Version check done using protocol %u.%u", conn
->major
,
1904 * Check for data pending for a given stream id from the session daemon.
1906 static int relay_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
1907 struct relay_connection
*conn
,
1908 const struct lttng_buffer_view
*payload
)
1910 struct relay_session
*session
= conn
->session
;
1911 struct lttcomm_relayd_data_pending msg
;
1912 struct lttcomm_relayd_generic_reply reply
;
1913 struct relay_stream
*stream
;
1917 DBG("Data pending command received");
1919 if (!session
|| !conn
->version_check_done
) {
1920 ERR("Trying to check for data before version check");
1922 goto end_no_session
;
1925 if (payload
->size
< sizeof(msg
)) {
1926 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
1927 sizeof(msg
), payload
->size
);
1929 goto end_no_session
;
1931 memcpy(&msg
, payload
->data
, sizeof(msg
));
1932 msg
.stream_id
= be64toh(msg
.stream_id
);
1933 msg
.last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1935 stream
= stream_get_by_id(msg
.stream_id
);
1936 if (stream
== NULL
) {
1941 pthread_mutex_lock(&stream
->lock
);
1943 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1944 " and last_seq %" PRIu64
, msg
.stream_id
,
1945 stream
->prev_seq
, msg
.last_net_seq_num
);
1947 /* Avoid wrapping issue */
1948 if (((int64_t) (stream
->prev_seq
- msg
.last_net_seq_num
)) >= 0) {
1949 /* Data has in fact been written and is NOT pending */
1952 /* Data still being streamed thus pending */
1956 stream
->data_pending_check_done
= true;
1957 pthread_mutex_unlock(&stream
->lock
);
1962 memset(&reply
, 0, sizeof(reply
));
1963 reply
.ret_code
= htobe32(ret
);
1964 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1965 if (send_ret
< (ssize_t
) sizeof(reply
)) {
1966 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
1976 * Wait for the control socket to reach a quiescent state.
1978 * Note that for now, when receiving this command from the session
1979 * daemon, this means that every subsequent commands or data received on
1980 * the control socket has been handled. So, this is why we simply return
1983 static int relay_quiescent_control(const struct lttcomm_relayd_hdr
*recv_hdr
,
1984 struct relay_connection
*conn
,
1985 const struct lttng_buffer_view
*payload
)
1989 struct relay_stream
*stream
;
1990 struct lttcomm_relayd_quiescent_control msg
;
1991 struct lttcomm_relayd_generic_reply reply
;
1993 DBG("Checking quiescent state on control socket");
1995 if (!conn
->session
|| !conn
->version_check_done
) {
1996 ERR("Trying to check for data before version check");
1998 goto end_no_session
;
2001 if (payload
->size
< sizeof(msg
)) {
2002 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2003 sizeof(msg
), payload
->size
);
2005 goto end_no_session
;
2007 memcpy(&msg
, payload
->data
, sizeof(msg
));
2008 msg
.stream_id
= be64toh(msg
.stream_id
);
2010 stream
= stream_get_by_id(msg
.stream_id
);
2014 pthread_mutex_lock(&stream
->lock
);
2015 stream
->data_pending_check_done
= true;
2016 pthread_mutex_unlock(&stream
->lock
);
2018 DBG("Relay quiescent control pending flag set to %" PRIu64
, msg
.stream_id
);
2021 memset(&reply
, 0, sizeof(reply
));
2022 reply
.ret_code
= htobe32(LTTNG_OK
);
2023 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2024 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2025 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2037 * Initialize a data pending command. This means that a consumer is about
2038 * to ask for data pending for each stream it holds. Simply iterate over
2039 * all streams of a session and set the data_pending_check_done flag.
2041 * This command returns to the client a LTTNG_OK code.
2043 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2044 struct relay_connection
*conn
,
2045 const struct lttng_buffer_view
*payload
)
2049 struct lttng_ht_iter iter
;
2050 struct lttcomm_relayd_begin_data_pending msg
;
2051 struct lttcomm_relayd_generic_reply reply
;
2052 struct relay_stream
*stream
;
2057 DBG("Init streams for data pending");
2059 if (!conn
->session
|| !conn
->version_check_done
) {
2060 ERR("Trying to check for data before version check");
2062 goto end_no_session
;
2065 if (payload
->size
< sizeof(msg
)) {
2066 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2067 sizeof(msg
), payload
->size
);
2069 goto end_no_session
;
2071 memcpy(&msg
, payload
->data
, sizeof(msg
));
2072 msg
.session_id
= be64toh(msg
.session_id
);
2075 * Iterate over all streams to set the begin data pending flag.
2076 * For now, the streams are indexed by stream handle so we have
2077 * to iterate over all streams to find the one associated with
2078 * the right session_id.
2081 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2083 if (!stream_get(stream
)) {
2086 if (stream
->trace
->session
->id
== msg
.session_id
) {
2087 pthread_mutex_lock(&stream
->lock
);
2088 stream
->data_pending_check_done
= false;
2089 pthread_mutex_unlock(&stream
->lock
);
2090 DBG("Set begin data pending flag to stream %" PRIu64
,
2091 stream
->stream_handle
);
2097 memset(&reply
, 0, sizeof(reply
));
2098 /* All good, send back reply. */
2099 reply
.ret_code
= htobe32(LTTNG_OK
);
2101 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2102 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2103 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2115 * End data pending command. This will check, for a given session id, if
2116 * each stream associated with it has its data_pending_check_done flag
2117 * set. If not, this means that the client lost track of the stream but
2118 * the data is still being streamed on our side. In this case, we inform
2119 * the client that data is in flight.
2121 * Return to the client if there is data in flight or not with a ret_code.
2123 static int relay_end_data_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2124 struct relay_connection
*conn
,
2125 const struct lttng_buffer_view
*payload
)
2129 struct lttng_ht_iter iter
;
2130 struct lttcomm_relayd_end_data_pending msg
;
2131 struct lttcomm_relayd_generic_reply reply
;
2132 struct relay_stream
*stream
;
2133 uint32_t is_data_inflight
= 0;
2135 DBG("End data pending command");
2137 if (!conn
->session
|| !conn
->version_check_done
) {
2138 ERR("Trying to check for data before version check");
2140 goto end_no_session
;
2143 if (payload
->size
< sizeof(msg
)) {
2144 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2145 sizeof(msg
), payload
->size
);
2147 goto end_no_session
;
2149 memcpy(&msg
, payload
->data
, sizeof(msg
));
2150 msg
.session_id
= be64toh(msg
.session_id
);
2153 * Iterate over all streams to see if the begin data pending
2157 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2159 if (!stream_get(stream
)) {
2162 if (stream
->trace
->session
->id
!= msg
.session_id
) {
2166 pthread_mutex_lock(&stream
->lock
);
2167 if (!stream
->data_pending_check_done
) {
2168 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
2169 is_data_inflight
= 1;
2170 DBG("Data is still in flight for stream %" PRIu64
,
2171 stream
->stream_handle
);
2172 pthread_mutex_unlock(&stream
->lock
);
2177 pthread_mutex_unlock(&stream
->lock
);
2182 memset(&reply
, 0, sizeof(reply
));
2183 /* All good, send back reply. */
2184 reply
.ret_code
= htobe32(is_data_inflight
);
2186 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2187 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2188 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2200 * Receive an index for a specific stream.
2202 * Return 0 on success else a negative value.
2204 static int relay_recv_index(const struct lttcomm_relayd_hdr
*recv_hdr
,
2205 struct relay_connection
*conn
,
2206 const struct lttng_buffer_view
*payload
)
2210 struct relay_session
*session
= conn
->session
;
2211 struct lttcomm_relayd_index index_info
;
2212 struct relay_index
*index
;
2213 struct lttcomm_relayd_generic_reply reply
;
2214 struct relay_stream
*stream
;
2219 DBG("Relay receiving index");
2221 if (!session
|| !conn
->version_check_done
) {
2222 ERR("Trying to close a stream before version check");
2224 goto end_no_session
;
2227 msg_len
= lttcomm_relayd_index_len(
2228 lttng_to_index_major(conn
->major
, conn
->minor
),
2229 lttng_to_index_minor(conn
->major
, conn
->minor
));
2230 if (payload
->size
< msg_len
) {
2231 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2232 msg_len
, payload
->size
);
2234 goto end_no_session
;
2236 memcpy(&index_info
, payload
->data
, msg_len
);
2237 index_info
.relay_stream_id
= be64toh(index_info
.relay_stream_id
);
2238 index_info
.net_seq_num
= be64toh(index_info
.net_seq_num
);
2239 index_info
.packet_size
= be64toh(index_info
.packet_size
);
2240 index_info
.content_size
= be64toh(index_info
.content_size
);
2241 index_info
.timestamp_begin
= be64toh(index_info
.timestamp_begin
);
2242 index_info
.timestamp_end
= be64toh(index_info
.timestamp_end
);
2243 index_info
.events_discarded
= be64toh(index_info
.events_discarded
);
2244 index_info
.stream_id
= be64toh(index_info
.stream_id
);
2245 index_info
.stream_instance_id
= be64toh(index_info
.stream_instance_id
);
2246 index_info
.packet_seq_num
= be64toh(index_info
.packet_seq_num
);
2248 stream
= stream_get_by_id(index_info
.relay_stream_id
);
2250 ERR("stream_get_by_id not found");
2254 pthread_mutex_lock(&stream
->lock
);
2256 /* Live beacon handling */
2257 if (index_info
.packet_size
== 0) {
2258 DBG("Received live beacon for stream %" PRIu64
,
2259 stream
->stream_handle
);
2262 * Only flag a stream inactive when it has already
2263 * received data and no indexes are in flight.
2265 if (stream
->index_received_seqcount
> 0
2266 && stream
->indexes_in_flight
== 0) {
2267 stream
->beacon_ts_end
= index_info
.timestamp_end
;
2270 goto end_stream_put
;
2272 stream
->beacon_ts_end
= -1ULL;
2275 if (stream
->ctf_stream_id
== -1ULL) {
2276 stream
->ctf_stream_id
= index_info
.stream_id
;
2278 index
= relay_index_get_by_id_or_create(stream
, index_info
.net_seq_num
);
2281 ERR("relay_index_get_by_id_or_create index NULL");
2282 goto end_stream_put
;
2284 if (set_index_control_data(index
, &index_info
, conn
)) {
2285 ERR("set_index_control_data error");
2286 relay_index_put(index
);
2288 goto end_stream_put
;
2290 ret
= relay_index_try_flush(index
);
2292 tracefile_array_commit_seq(stream
->tfa
);
2293 stream
->index_received_seqcount
++;
2294 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
2295 } else if (ret
> 0) {
2299 ERR("relay_index_try_flush error %d", ret
);
2300 relay_index_put(index
);
2305 pthread_mutex_unlock(&stream
->lock
);
2310 memset(&reply
, 0, sizeof(reply
));
2312 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2314 reply
.ret_code
= htobe32(LTTNG_OK
);
2316 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2317 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2318 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret
);
2327 * Receive the streams_sent message.
2329 * Return 0 on success else a negative value.
2331 static int relay_streams_sent(const struct lttcomm_relayd_hdr
*recv_hdr
,
2332 struct relay_connection
*conn
,
2333 const struct lttng_buffer_view
*payload
)
2337 struct lttcomm_relayd_generic_reply reply
;
2341 DBG("Relay receiving streams_sent");
2343 if (!conn
->session
|| !conn
->version_check_done
) {
2344 ERR("Trying to close a stream before version check");
2346 goto end_no_session
;
2350 * Publish every pending stream in the connection recv list which are
2351 * now ready to be used by the viewer.
2353 publish_connection_local_streams(conn
);
2355 memset(&reply
, 0, sizeof(reply
));
2356 reply
.ret_code
= htobe32(LTTNG_OK
);
2357 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2358 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2359 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2372 * relay_rotate_session_stream: rotate a stream to a new tracefile for the session
2373 * rotation feature (not the tracefile rotation feature).
2375 static int relay_rotate_session_stream(const struct lttcomm_relayd_hdr
*recv_hdr
,
2376 struct relay_connection
*conn
,
2377 const struct lttng_buffer_view
*payload
)
2381 struct relay_session
*session
= conn
->session
;
2382 struct lttcomm_relayd_rotate_stream stream_info
;
2383 struct lttcomm_relayd_generic_reply reply
;
2384 struct relay_stream
*stream
;
2387 struct lttng_buffer_view new_path_view
;
2389 DBG("Rotate stream received");
2391 if (!session
|| !conn
->version_check_done
) {
2392 ERR("Trying to rotate a stream before version check");
2397 if (session
->major
== 2 && session
->minor
< 11) {
2398 ERR("Unsupported feature before 2.11");
2403 header_len
= sizeof(struct lttcomm_relayd_rotate_stream
);
2405 if (payload
->size
< header_len
) {
2406 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2407 header_len
, payload
->size
);
2412 memcpy(&stream_info
, payload
->data
, header_len
);
2414 /* Convert to host */
2415 stream_info
.pathname_length
= be32toh(stream_info
.pathname_length
);
2416 stream_info
.stream_id
= be64toh(stream_info
.stream_id
);
2417 stream_info
.new_chunk_id
= be64toh(stream_info
.new_chunk_id
);
2418 stream_info
.rotate_at_seq_num
= be64toh(stream_info
.rotate_at_seq_num
);
2420 path_len
= stream_info
.pathname_length
;
2421 if (payload
->size
< header_len
+ path_len
) {
2422 ERR("Unexpected payload size in \"relay_rotate_session_stream\" including path: expected >= %zu bytes, got %zu bytes",
2423 header_len
+ path_len
, payload
->size
);
2428 /* Ensure it fits in local filename length. */
2429 if (path_len
>= LTTNG_PATH_MAX
) {
2430 ret
= -ENAMETOOLONG
;
2431 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2432 path_len
, LTTNG_PATH_MAX
);
2436 new_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2437 stream_info
.pathname_length
);
2439 stream
= stream_get_by_id(stream_info
.stream_id
);
2445 pthread_mutex_lock(&stream
->lock
);
2448 * Update the trace path (just the folder, the stream name does not
2451 free(stream
->path_name
);
2452 stream
->path_name
= create_output_path(new_path_view
.data
);
2453 if (!stream
->path_name
) {
2454 ERR("Failed to create a new output path");
2456 goto end_stream_unlock
;
2458 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
,
2461 ERR("relay creating output directory");
2463 goto end_stream_unlock
;
2466 stream
->chunk_id
= stream_info
.new_chunk_id
;
2468 if (stream
->is_metadata
) {
2470 * The metadata stream is sent only over the control connection
2471 * so we know we have all the data to perform the stream
2474 ret
= do_rotate_stream(stream
);
2476 stream
->rotate_at_seq_num
= stream_info
.rotate_at_seq_num
;
2477 ret
= try_rotate_stream(stream
);
2480 goto end_stream_unlock
;
2484 pthread_mutex_unlock(&stream
->lock
);
2487 memset(&reply
, 0, sizeof(reply
));
2489 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2491 reply
.ret_code
= htobe32(LTTNG_OK
);
2493 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2494 sizeof(struct lttcomm_relayd_generic_reply
), 0);
2495 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2496 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2506 * relay_mkdir: Create a folder on the disk.
2508 static int relay_mkdir(const struct lttcomm_relayd_hdr
*recv_hdr
,
2509 struct relay_connection
*conn
,
2510 const struct lttng_buffer_view
*payload
)
2513 struct relay_session
*session
= conn
->session
;
2514 struct lttcomm_relayd_mkdir path_info_header
;
2515 struct lttcomm_relayd_generic_reply reply
;
2519 struct lttng_buffer_view path_view
;
2521 if (!session
|| !conn
->version_check_done
) {
2522 ERR("Trying to create a directory before version check");
2524 goto end_no_session
;
2527 if (session
->major
== 2 && session
->minor
< 11) {
2529 * This client is not supposed to use this command since
2530 * it predates its introduction.
2532 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2534 goto end_no_session
;
2537 header_len
= sizeof(path_info_header
);
2538 if (payload
->size
< header_len
) {
2539 ERR("Unexpected payload size in \"relay_mkdir\": expected >= %zu bytes, got %zu bytes",
2540 header_len
, payload
->size
);
2542 goto end_no_session
;
2545 memcpy(&path_info_header
, payload
->data
, header_len
);
2547 path_info_header
.length
= be32toh(path_info_header
.length
);
2549 if (payload
->size
< header_len
+ path_info_header
.length
) {
2550 ERR("Unexpected payload size in \"relay_mkdir\" including path: expected >= %zu bytes, got %zu bytes",
2551 header_len
+ path_info_header
.length
, payload
->size
);
2553 goto end_no_session
;
2556 /* Ensure that it fits in local path length. */
2557 if (path_info_header
.length
>= LTTNG_PATH_MAX
) {
2558 ret
= -ENAMETOOLONG
;
2559 ERR("Path name argument of mkdir command (%" PRIu32
" bytes) exceeds the maximal length allowed (%d bytes)",
2560 path_info_header
.length
, LTTNG_PATH_MAX
);
2564 path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2565 path_info_header
.length
);
2567 path
= create_output_path(path_view
.data
);
2569 ERR("Failed to create output path");
2574 ret
= utils_mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, -1, -1);
2576 ERR("relay creating output directory");
2583 memset(&reply
, 0, sizeof(reply
));
2585 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2587 reply
.ret_code
= htobe32(LTTNG_OK
);
2589 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2590 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2591 ERR("Failed to send \"mkdir\" command reply (ret = %zd)", send_ret
);
2600 static int validate_rotate_rename_path_length(const char *path_type
,
2601 uint32_t path_length
)
2605 if (path_length
> LTTNG_PATH_MAX
) {
2606 ret
= -ENAMETOOLONG
;
2607 ERR("rotate rename \"%s\" path name length (%" PRIu32
" bytes) exceeds the allowed size of %i bytes",
2608 path_type
, path_length
, LTTNG_PATH_MAX
);
2609 } else if (path_length
== 0) {
2611 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type
);
2617 * relay_rotate_rename: rename the trace folder after a rotation is
2618 * completed. We are not closing any fd here, just moving the folder, so it
2619 * works even if data is still in-flight.
2621 static int relay_rotate_rename(const struct lttcomm_relayd_hdr
*recv_hdr
,
2622 struct relay_connection
*conn
,
2623 const struct lttng_buffer_view
*payload
)
2627 struct relay_session
*session
= conn
->session
;
2628 struct lttcomm_relayd_generic_reply reply
;
2629 struct lttcomm_relayd_rotate_rename header
;
2631 size_t received_paths_size
;
2632 char *complete_old_path
= NULL
, *complete_new_path
= NULL
;
2633 struct lttng_buffer_view old_path_view
;
2634 struct lttng_buffer_view new_path_view
;
2636 if (!session
|| !conn
->version_check_done
) {
2637 ERR("Trying to rename a trace folder before version check");
2642 if (session
->major
== 2 && session
->minor
< 11) {
2643 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2648 header_len
= sizeof(header
);
2649 if (payload
->size
< header_len
) {
2650 ERR("Unexpected payload size in \"relay_rotate_rename\": expected >= %zu bytes, got %zu bytes",
2651 header_len
, payload
->size
);
2656 memcpy(&header
, payload
->data
, header_len
);
2658 header
.old_path_length
= be32toh(header
.old_path_length
);
2659 header
.new_path_length
= be32toh(header
.new_path_length
);
2660 received_paths_size
= header
.old_path_length
+ header
.new_path_length
;
2662 if (payload
->size
< header_len
+ received_paths_size
) {
2663 ERR("Unexpected payload size in \"relay_rotate_rename\" including paths: expected >= %zu bytes, got %zu bytes",
2664 header_len
, payload
->size
);
2669 /* Ensure the paths don't exceed their allowed size. */
2670 ret
= validate_rotate_rename_path_length("old", header
.old_path_length
);
2674 ret
= validate_rotate_rename_path_length("new", header
.new_path_length
);
2679 old_path_view
= lttng_buffer_view_from_view(payload
, header_len
,
2680 header
.old_path_length
);
2681 new_path_view
= lttng_buffer_view_from_view(payload
,
2682 header_len
+ header
.old_path_length
,
2683 header
.new_path_length
);
2685 /* Validate that both paths received are NULL terminated. */
2686 if (old_path_view
.data
[old_path_view
.size
- 1] != '\0') {
2687 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2691 if (new_path_view
.data
[new_path_view
.size
- 1] != '\0') {
2692 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2697 complete_old_path
= create_output_path(old_path_view
.data
);
2698 if (!complete_old_path
) {
2699 ERR("Failed to build old output path in rotate_rename command");
2704 complete_new_path
= create_output_path(new_path_view
.data
);
2705 if (!complete_new_path
) {
2706 ERR("Failed to build new output path in rotate_rename command");
2711 ret
= utils_mkdir_recursive(complete_new_path
, S_IRWXU
| S_IRWXG
,
2714 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2720 * If a domain has not yet created its channel, the domain-specific
2721 * folder might not exist, but this is not an error.
2723 ret
= rename(complete_old_path
, complete_new_path
);
2724 if (ret
< 0 && errno
!= ENOENT
) {
2725 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2726 complete_old_path
, complete_new_path
);
2732 memset(&reply
, 0, sizeof(reply
));
2734 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2736 reply
.ret_code
= htobe32(LTTNG_OK
);
2738 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2740 if (send_ret
< sizeof(reply
)) {
2741 ERR("Failed to send \"rotate rename\" command reply (ret = %zd)",
2747 free(complete_old_path
);
2748 free(complete_new_path
);
2753 * Check if all the streams in the session have completed the last rotation.
2754 * The chunk_id value is used to distinguish the cases where a stream was
2755 * closed on the consumerd before the rotation started but it still active on
2756 * the relayd, and the case where a stream appeared on the consumerd/relayd
2757 * after the last rotation started (in that case, it is already writing in the
2758 * new chunk folder).
2761 int relay_rotate_pending(const struct lttcomm_relayd_hdr
*recv_hdr
,
2762 struct relay_connection
*conn
,
2763 const struct lttng_buffer_view
*payload
)
2765 struct relay_session
*session
= conn
->session
;
2766 struct lttcomm_relayd_rotate_pending msg
;
2767 struct lttcomm_relayd_rotate_pending_reply reply
;
2768 struct lttng_ht_iter iter
;
2769 struct relay_stream
*stream
;
2773 bool rotate_pending
= false;
2775 DBG("Rotate pending command received");
2777 if (!session
|| !conn
->version_check_done
) {
2778 ERR("Trying to check for data before version check");
2783 if (session
->major
== 2 && session
->minor
< 11) {
2784 ERR("Unsupported feature before 2.11");
2789 if (payload
->size
< sizeof(msg
)) {
2790 ERR("Unexpected payload size in \"relay_rotate_pending\": expected >= %zu bytes, got %zu bytes",
2791 sizeof(msg
), payload
->size
);
2796 memcpy(&msg
, payload
->data
, sizeof(msg
));
2798 chunk_id
= be64toh(msg
.chunk_id
);
2800 DBG("Evaluating rotate pending for chunk id %" PRIu64
, chunk_id
);
2803 * Iterate over all the streams in the session and check if they are
2804 * still waiting for data to perform their rotation.
2807 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
2809 if (!stream_get(stream
)) {
2812 if (stream
->trace
->session
!= session
) {
2816 pthread_mutex_lock(&stream
->lock
);
2817 if (stream
->rotate_at_seq_num
!= -1ULL) {
2818 /* We have not yet performed the rotation. */
2819 rotate_pending
= true;
2820 DBG("Stream %" PRIu64
" is still rotating",
2821 stream
->stream_handle
);
2822 } else if (stream
->chunk_id
< chunk_id
) {
2824 * Stream closed on the consumer but still active on the
2827 rotate_pending
= true;
2828 DBG("Stream %" PRIu64
" did not exist on the consumer "
2829 "when the last rotation started, but is"
2830 "still waiting for data before getting"
2832 stream
->stream_handle
);
2834 pthread_mutex_unlock(&stream
->lock
);
2836 if (rotate_pending
) {
2843 memset(&reply
, 0, sizeof(reply
));
2844 reply
.generic
.ret_code
= htobe32((uint32_t) LTTNG_OK
);
2845 reply
.is_pending
= (uint8_t) !!rotate_pending
;
2846 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
2848 if (send_ret
< (ssize_t
) sizeof(reply
)) {
2849 ERR("Failed to send \"rotate pending\" command reply (ret = %zd)",
2858 #define DBG_CMD(cmd_name, conn) \
2859 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
2861 static int relay_process_control_command(struct relay_connection
*conn
,
2862 const struct lttcomm_relayd_hdr
*header
,
2863 const struct lttng_buffer_view
*payload
)
2867 switch (header
->cmd
) {
2868 case RELAYD_CREATE_SESSION
:
2869 DBG_CMD("RELAYD_CREATE_SESSION", conn
);
2870 ret
= relay_create_session(header
, conn
, payload
);
2872 case RELAYD_ADD_STREAM
:
2873 DBG_CMD("RELAYD_ADD_STREAM", conn
);
2874 ret
= relay_add_stream(header
, conn
, payload
);
2876 case RELAYD_START_DATA
:
2877 DBG_CMD("RELAYD_START_DATA", conn
);
2878 ret
= relay_start(header
, conn
, payload
);
2880 case RELAYD_SEND_METADATA
:
2881 DBG_CMD("RELAYD_SEND_METADATA", conn
);
2882 ret
= relay_recv_metadata(header
, conn
, payload
);
2884 case RELAYD_VERSION
:
2885 DBG_CMD("RELAYD_VERSION", conn
);
2886 ret
= relay_send_version(header
, conn
, payload
);
2888 case RELAYD_CLOSE_STREAM
:
2889 DBG_CMD("RELAYD_CLOSE_STREAM", conn
);
2890 ret
= relay_close_stream(header
, conn
, payload
);
2892 case RELAYD_DATA_PENDING
:
2893 DBG_CMD("RELAYD_DATA_PENDING", conn
);
2894 ret
= relay_data_pending(header
, conn
, payload
);
2896 case RELAYD_QUIESCENT_CONTROL
:
2897 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn
);
2898 ret
= relay_quiescent_control(header
, conn
, payload
);
2900 case RELAYD_BEGIN_DATA_PENDING
:
2901 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn
);
2902 ret
= relay_begin_data_pending(header
, conn
, payload
);
2904 case RELAYD_END_DATA_PENDING
:
2905 DBG_CMD("RELAYD_END_DATA_PENDING", conn
);
2906 ret
= relay_end_data_pending(header
, conn
, payload
);
2908 case RELAYD_SEND_INDEX
:
2909 DBG_CMD("RELAYD_SEND_INDEX", conn
);
2910 ret
= relay_recv_index(header
, conn
, payload
);
2912 case RELAYD_STREAMS_SENT
:
2913 DBG_CMD("RELAYD_STREAMS_SENT", conn
);
2914 ret
= relay_streams_sent(header
, conn
, payload
);
2916 case RELAYD_RESET_METADATA
:
2917 DBG_CMD("RELAYD_RESET_METADATA", conn
);
2918 ret
= relay_reset_metadata(header
, conn
, payload
);
2920 case RELAYD_ROTATE_STREAM
:
2921 DBG_CMD("RELAYD_ROTATE_STREAM", conn
);
2922 ret
= relay_rotate_session_stream(header
, conn
, payload
);
2924 case RELAYD_ROTATE_RENAME
:
2925 DBG_CMD("RELAYD_ROTATE_RENAME", conn
);
2926 ret
= relay_rotate_rename(header
, conn
, payload
);
2928 case RELAYD_ROTATE_PENDING
:
2929 DBG_CMD("RELAYD_ROTATE_PENDING", conn
);
2930 ret
= relay_rotate_pending(header
, conn
, payload
);
2933 DBG_CMD("RELAYD_MKDIR", conn
);
2934 ret
= relay_mkdir(header
, conn
, payload
);
2936 case RELAYD_UPDATE_SYNC_INFO
:
2938 ERR("Received unknown command (%u)", header
->cmd
);
2939 relay_unknown_command(conn
);
2948 static int relay_process_control_receive_payload(struct relay_connection
*conn
)
2951 struct lttng_dynamic_buffer
*reception_buffer
=
2952 &conn
->protocol
.ctrl
.reception_buffer
;
2953 struct ctrl_connection_state_receive_payload
*state
=
2954 &conn
->protocol
.ctrl
.state
.receive_payload
;
2955 struct lttng_buffer_view payload_view
;
2957 if (state
->left_to_receive
== 0) {
2958 /* Short-circuit for payload-less commands. */
2959 goto reception_complete
;
2962 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
2963 reception_buffer
->data
+ state
->received
,
2964 state
->left_to_receive
, MSG_DONTWAIT
);
2966 ERR("Unable to receive command payload on sock %d", conn
->sock
->fd
);
2968 } else if (ret
== 0) {
2969 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
2975 assert(ret
<= state
->left_to_receive
);
2977 state
->left_to_receive
-= ret
;
2978 state
->received
+= ret
;
2980 if (state
->left_to_receive
> 0) {
2982 * Can't transition to the protocol's next state, wait to
2983 * receive the rest of the header.
2985 DBG3("Partial reception of control connection protocol payload (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
2986 state
->received
, state
->left_to_receive
,
2993 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64
" bytes",
2994 conn
->sock
->fd
, state
->received
);
2996 * The payload required to process the command has been received.
2997 * A view to the reception buffer is forwarded to the various
2998 * commands and the state of the control is reset on success.
3000 * Commands are responsible for sending their reply to the peer.
3002 payload_view
= lttng_buffer_view_from_dynamic_buffer(reception_buffer
,
3004 ret
= relay_process_control_command(conn
,
3005 &state
->header
, &payload_view
);
3010 ret
= connection_reset_protocol_state(conn
);
3015 static int relay_process_control_receive_header(struct relay_connection
*conn
)
3018 struct lttcomm_relayd_hdr header
;
3019 struct lttng_dynamic_buffer
*reception_buffer
=
3020 &conn
->protocol
.ctrl
.reception_buffer
;
3021 struct ctrl_connection_state_receive_header
*state
=
3022 &conn
->protocol
.ctrl
.state
.receive_header
;
3024 assert(state
->left_to_receive
!= 0);
3026 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3027 reception_buffer
->data
+ state
->received
,
3028 state
->left_to_receive
, MSG_DONTWAIT
);
3030 ERR("Unable to receive control command header on sock %d", conn
->sock
->fd
);
3032 } else if (ret
== 0) {
3033 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3039 assert(ret
<= state
->left_to_receive
);
3041 state
->left_to_receive
-= ret
;
3042 state
->received
+= ret
;
3044 if (state
->left_to_receive
> 0) {
3046 * Can't transition to the protocol's next state, wait to
3047 * receive the rest of the header.
3049 DBG3("Partial reception of control connection protocol header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3050 state
->received
, state
->left_to_receive
,
3056 /* Transition to next state: receiving the command's payload. */
3057 conn
->protocol
.ctrl
.state_id
=
3058 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3059 memcpy(&header
, reception_buffer
->data
, sizeof(header
));
3060 header
.circuit_id
= be64toh(header
.circuit_id
);
3061 header
.data_size
= be64toh(header
.data_size
);
3062 header
.cmd
= be32toh(header
.cmd
);
3063 header
.cmd_version
= be32toh(header
.cmd_version
);
3064 memcpy(&conn
->protocol
.ctrl
.state
.receive_payload
.header
,
3065 &header
, sizeof(header
));
3067 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32
", cmd_version = %" PRIu32
", payload size = %" PRIu64
" bytes",
3068 conn
->sock
->fd
, header
.cmd
, header
.cmd_version
,
3071 /* FIXME temporary arbitrary limit on data size. */
3072 if (header
.data_size
> (128 * 1024 * 1024)) {
3073 ERR("Command header indicates a payload (%" PRIu64
" bytes) that exceeds the maximal payload size allowed on a control connection.",
3079 conn
->protocol
.ctrl
.state
.receive_payload
.left_to_receive
=
3081 conn
->protocol
.ctrl
.state
.receive_payload
.received
= 0;
3082 ret
= lttng_dynamic_buffer_set_size(reception_buffer
,
3088 if (header
.data_size
== 0) {
3090 * Manually invoke the next state as the poll loop
3091 * will not wake-up to allow us to proceed further.
3093 ret
= relay_process_control_receive_payload(conn
);
3100 * Process the commands received on the control socket
3102 static int relay_process_control(struct relay_connection
*conn
)
3106 switch (conn
->protocol
.ctrl
.state_id
) {
3107 case CTRL_CONNECTION_STATE_RECEIVE_HEADER
:
3108 ret
= relay_process_control_receive_header(conn
);
3110 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3111 ret
= relay_process_control_receive_payload(conn
);
3114 ERR("Unknown control connection protocol state encountered.");
3122 * Handle index for a data stream.
3124 * Called with the stream lock held.
3126 * Return 0 on success else a negative value.
3128 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
3129 bool rotate_index
, bool *flushed
, uint64_t total_size
)
3132 uint64_t data_offset
;
3133 struct relay_index
*index
;
3135 /* Get data offset because we are about to update the index. */
3136 data_offset
= htobe64(stream
->tracefile_size_current
);
3138 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
3139 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
3142 * Lookup for an existing index for that stream id/sequence
3143 * number. If it exists, the control thread has already received the
3144 * data for it, thus we need to write it to disk.
3146 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
3152 if (rotate_index
|| !stream
->index_file
) {
3153 ret
= create_rotate_index_file(stream
);
3155 ERR("Failed to rotate index");
3156 /* Put self-ref for this index due to error. */
3157 relay_index_put(index
);
3163 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
3165 /* Put self-ref for this index due to error. */
3166 relay_index_put(index
);
3171 ret
= relay_index_try_flush(index
);
3173 tracefile_array_commit_seq(stream
->tfa
);
3174 stream
->index_received_seqcount
++;
3176 } else if (ret
> 0) {
3177 index
->total_size
= total_size
;
3181 /* Put self-ref for this index due to error. */
3182 relay_index_put(index
);
3190 static int relay_process_data_receive_header(struct relay_connection
*conn
)
3193 struct data_connection_state_receive_header
*state
=
3194 &conn
->protocol
.data
.state
.receive_header
;
3195 struct lttcomm_relayd_data_hdr header
;
3196 struct relay_stream
*stream
;
3198 assert(state
->left_to_receive
!= 0);
3200 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
,
3201 state
->header_reception_buffer
+ state
->received
,
3202 state
->left_to_receive
, MSG_DONTWAIT
);
3204 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
3206 } else if (ret
== 0) {
3207 /* Orderly shutdown. Not necessary to print an error. */
3208 DBG("Socket %d performed an orderly shutdown (received EOF)", conn
->sock
->fd
);
3214 assert(ret
<= state
->left_to_receive
);
3216 state
->left_to_receive
-= ret
;
3217 state
->received
+= ret
;
3219 if (state
->left_to_receive
> 0) {
3221 * Can't transition to the protocol's next state, wait to
3222 * receive the rest of the header.
3224 DBG3("Partial reception of data connection header (received %" PRIu64
" bytes, %" PRIu64
" bytes left to receive, fd = %i)",
3225 state
->received
, state
->left_to_receive
,
3231 /* Transition to next state: receiving the payload. */
3232 conn
->protocol
.data
.state_id
= DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
;
3234 memcpy(&header
, state
->header_reception_buffer
, sizeof(header
));
3235 header
.circuit_id
= be64toh(header
.circuit_id
);
3236 header
.stream_id
= be64toh(header
.stream_id
);
3237 header
.data_size
= be32toh(header
.data_size
);
3238 header
.net_seq_num
= be64toh(header
.net_seq_num
);
3239 header
.padding_size
= be32toh(header
.padding_size
);
3240 memcpy(&conn
->protocol
.data
.state
.receive_payload
.header
, &header
, sizeof(header
));
3242 conn
->protocol
.data
.state
.receive_payload
.left_to_receive
=
3244 conn
->protocol
.data
.state
.receive_payload
.received
= 0;
3245 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= false;
3247 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64
", stream_id = %" PRIu64
", data_size = %" PRIu32
", net_seq_num = %" PRIu64
", padding_size = %" PRIu32
,
3248 conn
->sock
->fd
, header
.circuit_id
,
3249 header
.stream_id
, header
.data_size
,
3250 header
.net_seq_num
, header
.padding_size
);
3252 stream
= stream_get_by_id(header
.stream_id
);
3254 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3260 pthread_mutex_lock(&stream
->lock
);
3262 /* Check if a rotation is needed. */
3263 if (stream
->tracefile_size
> 0 &&
3264 (stream
->tracefile_size_current
+ header
.data_size
) >
3265 stream
->tracefile_size
) {
3266 uint64_t old_id
, new_id
;
3268 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
3269 tracefile_array_file_rotate(stream
->tfa
);
3271 /* new_id is updated by utils_rotate_stream_file. */
3274 ret
= utils_rotate_stream_file(stream
->path_name
,
3275 stream
->channel_name
, stream
->tracefile_size
,
3276 stream
->tracefile_count
, -1,
3277 -1, stream
->stream_fd
->fd
,
3278 &new_id
, &stream
->stream_fd
->fd
);
3280 ERR("Failed to rotate stream output file");
3281 goto end_stream_unlock
;
3285 * Reset current size because we just performed a stream
3288 stream
->tracefile_size_current
= 0;
3289 conn
->protocol
.data
.state
.receive_payload
.rotate_index
= true;
3294 pthread_mutex_unlock(&stream
->lock
);
3300 static int relay_process_data_receive_payload(struct relay_connection
*conn
)
3303 struct relay_stream
*stream
;
3304 struct data_connection_state_receive_payload
*state
=
3305 &conn
->protocol
.data
.state
.receive_payload
;
3306 const size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
3307 char data_buffer
[chunk_size
];
3308 bool partial_recv
= false;
3309 bool new_stream
= false, close_requested
= false, index_flushed
= false;
3310 uint64_t left_to_receive
= state
->left_to_receive
;
3311 struct relay_session
*session
;
3313 stream
= stream_get_by_id(state
->header
.stream_id
);
3315 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64
,
3316 state
->header
.stream_id
);
3321 pthread_mutex_lock(&stream
->lock
);
3322 session
= stream
->trace
->session
;
3324 DBG3("Receiving data for stream id %" PRIu64
" seqnum %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3325 state
->header
.stream_id
, state
->header
.net_seq_num
,
3326 state
->received
, left_to_receive
);
3329 * The size of the "chunk" received on any iteration is bounded by:
3330 * - the data left to receive,
3331 * - the data immediately available on the socket,
3332 * - the on-stack data buffer
3334 while (left_to_receive
> 0 && !partial_recv
) {
3336 size_t recv_size
= min(left_to_receive
, chunk_size
);
3338 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
,
3339 recv_size
, MSG_DONTWAIT
);
3341 ERR("Socket %d error %d", conn
->sock
->fd
, ret
);
3343 goto end_stream_unlock
;
3344 } else if (ret
== 0) {
3345 /* No more data ready to be consumed on socket. */
3346 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64
,
3347 state
->header
.stream_id
);
3349 } else if (ret
< (int) recv_size
) {
3351 * All the data available on the socket has been
3354 partial_recv
= true;
3359 /* Write data to stream output fd. */
3360 write_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
3362 if (write_ret
< (ssize_t
) recv_size
) {
3363 ERR("Relay error writing data to file");
3365 goto end_stream_unlock
;
3368 left_to_receive
-= recv_size
;
3369 state
->received
+= recv_size
;
3370 state
->left_to_receive
= left_to_receive
;
3372 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
3373 write_ret
, stream
->stream_handle
);
3376 if (state
->left_to_receive
> 0) {
3378 * Did not receive all the data expected, wait for more data to
3379 * become available on the socket.
3381 DBG3("Partial receive on data connection of stream id %" PRIu64
", %" PRIu64
" bytes received, %" PRIu64
" bytes left to receive",
3382 state
->header
.stream_id
, state
->received
,
3383 state
->left_to_receive
);
3385 goto end_stream_unlock
;
3388 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
3389 state
->header
.padding_size
);
3391 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3392 stream
->stream_handle
,
3393 state
->header
.net_seq_num
, ret
);
3394 goto end_stream_unlock
;
3398 if (session
->minor
>= 4 && !session
->snapshot
) {
3399 ret
= handle_index_data(stream
, state
->header
.net_seq_num
,
3400 state
->rotate_index
, &index_flushed
, state
->header
.data_size
+ state
->header
.padding_size
);
3402 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
3403 stream
->stream_handle
,
3404 state
->header
.net_seq_num
, ret
);
3405 goto end_stream_unlock
;
3409 stream
->tracefile_size_current
+= state
->header
.data_size
+
3410 state
->header
.padding_size
;
3412 if (stream
->prev_seq
== -1ULL) {
3415 if (index_flushed
) {
3416 stream
->pos_after_last_complete_data_index
=
3417 stream
->tracefile_size_current
;
3420 stream
->prev_seq
= state
->header
.net_seq_num
;
3423 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3424 * contents of *state which are aliased (union) to the same location as
3425 * the new state. Don't use it beyond this point.
3427 connection_reset_protocol_state(conn
);
3430 ret
= try_rotate_stream(stream
);
3432 goto end_stream_unlock
;
3436 close_requested
= stream
->close_requested
;
3437 pthread_mutex_unlock(&stream
->lock
);
3438 if (close_requested
&& left_to_receive
== 0) {
3439 try_stream_close(stream
);
3443 pthread_mutex_lock(&session
->lock
);
3444 uatomic_set(&session
->new_streams
, 1);
3445 pthread_mutex_unlock(&session
->lock
);
3454 * relay_process_data: Process the data received on the data socket
3456 static int relay_process_data(struct relay_connection
*conn
)
3460 switch (conn
->protocol
.data
.state_id
) {
3461 case DATA_CONNECTION_STATE_RECEIVE_HEADER
:
3462 ret
= relay_process_data_receive_header(conn
);
3464 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD
:
3465 ret
= relay_process_data_receive_payload(conn
);
3468 ERR("Unexpected data connection communication state.");
3475 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
3479 (void) lttng_poll_del(events
, pollfd
);
3481 ret
= close(pollfd
);
3483 ERR("Closing pollfd %d", pollfd
);
3487 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
3488 int pollfd
, struct relay_connection
*conn
)
3490 const char *type_str
;
3492 switch (conn
->type
) {
3497 type_str
= "Control";
3499 case RELAY_VIEWER_COMMAND
:
3500 type_str
= "Viewer Command";
3502 case RELAY_VIEWER_NOTIFICATION
:
3503 type_str
= "Viewer Notification";
3506 type_str
= "Unknown";
3508 cleanup_connection_pollfd(events
, pollfd
);
3509 connection_put(conn
);
3510 DBG("%s connection closed with %d", type_str
, pollfd
);
3514 * This thread does the actual work
3516 static void *relay_thread_worker(void *data
)
3518 int ret
, err
= -1, last_seen_data_fd
= -1;
3520 struct lttng_poll_event events
;
3521 struct lttng_ht
*relay_connections_ht
;
3522 struct lttng_ht_iter iter
;
3523 struct relay_connection
*destroy_conn
= NULL
;
3525 DBG("[thread] Relay worker started");
3527 rcu_register_thread();
3529 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
3531 if (testpoint(relayd_thread_worker
)) {
3532 goto error_testpoint
;
3535 health_code_update();
3537 /* table of connections indexed on socket */
3538 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3539 if (!relay_connections_ht
) {
3540 goto relay_connections_ht_error
;
3543 ret
= create_thread_poll_set(&events
, 2);
3545 goto error_poll_create
;
3548 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
3555 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
3557 health_code_update();
3559 /* Infinite blocking call, waiting for transmission */
3560 DBG3("Relayd worker thread polling...");
3561 health_poll_entry();
3562 ret
= lttng_poll_wait(&events
, -1);
3566 * Restart interrupted system call.
3568 if (errno
== EINTR
) {
3577 * Process control. The control connection is
3578 * prioritized so we don't starve it with high
3579 * throughput tracing data on the data connection.
3581 for (i
= 0; i
< nb_fd
; i
++) {
3582 /* Fetch once the poll data */
3583 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3584 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3586 health_code_update();
3590 * No activity for this FD (poll
3596 /* Thread quit pipe has been closed. Killing thread. */
3597 ret
= check_thread_quit_pipe(pollfd
, revents
);
3603 /* Inspect the relay conn pipe for new connection */
3604 if (pollfd
== relay_conn_pipe
[0]) {
3605 if (revents
& LPOLLIN
) {
3606 struct relay_connection
*conn
;
3608 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
3612 lttng_poll_add(&events
, conn
->sock
->fd
,
3613 LPOLLIN
| LPOLLRDHUP
);
3614 connection_ht_add(relay_connections_ht
, conn
);
3615 DBG("Connection socket %d added", conn
->sock
->fd
);
3616 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3617 ERR("Relay connection pipe error");
3620 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3624 struct relay_connection
*ctrl_conn
;
3626 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3627 /* If not found, there is a synchronization issue. */
3630 if (ctrl_conn
->type
== RELAY_DATA
) {
3631 if (revents
& LPOLLIN
) {
3633 * Flag the last seen data fd not deleted. It will be
3634 * used as the last seen fd if any fd gets deleted in
3637 last_notdel_data_fd
= pollfd
;
3639 goto put_ctrl_connection
;
3641 assert(ctrl_conn
->type
== RELAY_CONTROL
);
3643 if (revents
& LPOLLIN
) {
3644 ret
= relay_process_control(ctrl_conn
);
3646 /* Clear the connection on error. */
3647 relay_thread_close_connection(&events
,
3652 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3653 relay_thread_close_connection(&events
,
3655 if (last_seen_data_fd
== pollfd
) {
3656 last_seen_data_fd
= last_notdel_data_fd
;
3659 ERR("Unexpected poll events %u for control sock %d",
3661 connection_put(ctrl_conn
);
3664 put_ctrl_connection
:
3665 connection_put(ctrl_conn
);
3670 * The last loop handled a control request, go back to poll to make
3671 * sure we prioritise the control socket.
3677 if (last_seen_data_fd
>= 0) {
3678 for (i
= 0; i
< nb_fd
; i
++) {
3679 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3681 health_code_update();
3683 if (last_seen_data_fd
== pollfd
) {
3690 /* Process data connection. */
3691 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
3692 /* Fetch the poll data. */
3693 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
3694 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3695 struct relay_connection
*data_conn
;
3697 health_code_update();
3700 /* No activity for this FD (poll implementation). */
3704 /* Skip the command pipe. It's handled in the first loop. */
3705 if (pollfd
== relay_conn_pipe
[0]) {
3709 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
3711 /* Skip it. Might be removed before. */
3714 if (data_conn
->type
== RELAY_CONTROL
) {
3715 goto put_data_connection
;
3717 assert(data_conn
->type
== RELAY_DATA
);
3719 if (revents
& LPOLLIN
) {
3720 ret
= relay_process_data(data_conn
);
3721 /* Connection closed */
3723 relay_thread_close_connection(&events
, pollfd
,
3726 * Every goto restart call sets the last seen fd where
3727 * here we don't really care since we gracefully
3728 * continue the loop after the connection is deleted.
3731 /* Keep last seen port. */
3732 last_seen_data_fd
= pollfd
;
3733 connection_put(data_conn
);
3736 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3737 relay_thread_close_connection(&events
, pollfd
,
3740 ERR("Unknown poll events %u for data sock %d",
3743 put_data_connection
:
3744 connection_put(data_conn
);
3746 last_seen_data_fd
= -1;
3749 /* Normal exit, no error */
3754 /* Cleanup reamaining connection object. */
3756 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
3759 health_code_update();
3761 if (session_abort(destroy_conn
->session
)) {
3766 * No need to grab another ref, because we own
3769 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
3774 lttng_poll_clean(&events
);
3776 lttng_ht_destroy(relay_connections_ht
);
3777 relay_connections_ht_error
:
3778 /* Close relay conn pipes */
3779 utils_close_pipe(relay_conn_pipe
);
3781 DBG("Thread exited with error");
3783 DBG("Worker thread cleanup complete");
3787 ERR("Health error occurred in %s", __func__
);
3789 health_unregister(health_relayd
);
3790 rcu_unregister_thread();
3791 lttng_relay_stop_threads();
3796 * Create the relay command pipe to wake thread_manage_apps.
3797 * Closed in cleanup().
3799 static int create_relay_conn_pipe(void)
3803 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
3811 int main(int argc
, char **argv
)
3813 int ret
= 0, retval
= 0;
3816 /* Parse arguments */
3818 if (set_options(argc
, argv
)) {
3823 if (set_signal_handler()) {
3828 /* Try to create directory if -o, --output is specified. */
3829 if (opt_output_path
) {
3830 if (*opt_output_path
!= '/') {
3831 ERR("Please specify an absolute path for -o, --output PATH");
3836 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
3839 ERR("Unable to create %s", opt_output_path
);
3846 if (opt_daemon
|| opt_background
) {
3849 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
3857 * We are in the child. Make sure all other file
3858 * descriptors are closed, in case we are called with
3859 * more opened file descriptors than the standard ones.
3861 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3866 /* Initialize thread health monitoring */
3867 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
3868 if (!health_relayd
) {
3869 PERROR("health_app_create error");
3871 goto exit_health_app_create
;
3874 /* Create thread quit pipe */
3875 if (init_thread_quit_pipe()) {
3877 goto exit_init_data
;
3880 /* Setup the thread apps communication pipe. */
3881 if (create_relay_conn_pipe()) {
3883 goto exit_init_data
;
3886 /* Init relay command queue. */
3887 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
3889 /* Initialize communication library */
3891 lttcomm_inet_init();
3893 /* tables of sessions indexed by session ID */
3894 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3897 goto exit_init_data
;
3900 /* tables of streams indexed by stream ID */
3901 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3902 if (!relay_streams_ht
) {
3904 goto exit_init_data
;
3907 /* tables of streams indexed by stream ID */
3908 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3909 if (!viewer_streams_ht
) {
3911 goto exit_init_data
;
3914 ret
= utils_create_pipe(health_quit_pipe
);
3917 goto exit_health_quit_pipe
;
3920 /* Create thread to manage the client socket */
3921 ret
= pthread_create(&health_thread
, default_pthread_attr(),
3922 thread_manage_health
, (void *) NULL
);
3925 PERROR("pthread_create health");
3927 goto exit_health_thread
;
3930 /* Setup the dispatcher thread */
3931 ret
= pthread_create(&dispatcher_thread
, default_pthread_attr(),
3932 relay_thread_dispatcher
, (void *) NULL
);
3935 PERROR("pthread_create dispatcher");
3937 goto exit_dispatcher_thread
;
3940 /* Setup the worker thread */
3941 ret
= pthread_create(&worker_thread
, default_pthread_attr(),
3942 relay_thread_worker
, NULL
);
3945 PERROR("pthread_create worker");
3947 goto exit_worker_thread
;
3950 /* Setup the listener thread */
3951 ret
= pthread_create(&listener_thread
, default_pthread_attr(),
3952 relay_thread_listener
, (void *) NULL
);
3955 PERROR("pthread_create listener");
3957 goto exit_listener_thread
;
3960 ret
= relayd_live_create(live_uri
);
3962 ERR("Starting live viewer threads");
3968 * This is where we start awaiting program completion (e.g. through
3969 * signal that asks threads to teardown).
3972 ret
= relayd_live_join();
3978 ret
= pthread_join(listener_thread
, &status
);
3981 PERROR("pthread_join listener_thread");
3985 exit_listener_thread
:
3986 ret
= pthread_join(worker_thread
, &status
);
3989 PERROR("pthread_join worker_thread");
3994 ret
= pthread_join(dispatcher_thread
, &status
);
3997 PERROR("pthread_join dispatcher_thread");
4000 exit_dispatcher_thread
:
4002 ret
= pthread_join(health_thread
, &status
);
4005 PERROR("pthread_join health_thread");
4010 utils_close_pipe(health_quit_pipe
);
4011 exit_health_quit_pipe
:
4014 health_app_destroy(health_relayd
);
4015 exit_health_app_create
:
4018 * Wait for all pending call_rcu work to complete before tearing
4019 * down data structures. call_rcu worker may be trying to
4020 * perform lookups in those structures.
4025 /* Ensure all prior call_rcu are done. */