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/config/session-config.h>
58 #include <urcu/rculist.h>
61 #include "ctf-trace.h"
64 #include "lttng-relayd.h"
66 #include "health-relayd.h"
67 #include "testpoint.h"
68 #include "viewer-stream.h"
71 #include "connection.h"
72 #include "tracefile-array.h"
74 /* command line options */
75 char *opt_output_path
;
76 static int opt_daemon
, opt_background
;
79 * We need to wait for listener and live listener threads, as well as
80 * health check thread, before being ready to signal readiness.
82 #define NR_LTTNG_RELAY_READY 3
83 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
85 /* Size of receive buffer. */
86 #define RECV_DATA_BUFFER_SIZE 65536
88 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
89 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
91 static struct lttng_uri
*control_uri
;
92 static struct lttng_uri
*data_uri
;
93 static struct lttng_uri
*live_uri
;
97 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
98 static int tracing_group_name_override
;
100 const char * const config_section_name
= "relayd";
103 * Quit pipe for all threads. This permits a single cancellation point
104 * for all threads when receiving an event on the pipe.
106 int thread_quit_pipe
[2] = { -1, -1 };
109 * This pipe is used to inform the worker thread that a command is queued and
110 * ready to be processed.
112 static int relay_conn_pipe
[2] = { -1, -1 };
114 /* Shared between threads */
115 static int dispatch_thread_exit
;
117 static pthread_t listener_thread
;
118 static pthread_t dispatcher_thread
;
119 static pthread_t worker_thread
;
120 static pthread_t health_thread
;
123 * last_relay_stream_id_lock protects last_relay_stream_id increment
124 * atomicity on 32-bit architectures.
126 static pthread_mutex_t last_relay_stream_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
127 static uint64_t last_relay_stream_id
;
130 * Relay command queue.
132 * The relay_thread_listener and relay_thread_dispatcher communicate with this
135 static struct relay_conn_queue relay_conn_queue
;
137 /* buffer allocated at startup, used to store the trace data */
138 static char *data_buffer
;
139 static unsigned int data_buffer_size
;
141 /* Global relay stream hash table. */
142 struct lttng_ht
*relay_streams_ht
;
144 /* Global relay viewer stream hash table. */
145 struct lttng_ht
*viewer_streams_ht
;
147 /* Global relay sessions hash table. */
148 struct lttng_ht
*sessions_ht
;
150 /* Relayd health monitoring */
151 struct health_app
*health_relayd
;
153 static struct option long_options
[] = {
154 { "control-port", 1, 0, 'C', },
155 { "data-port", 1, 0, 'D', },
156 { "live-port", 1, 0, 'L', },
157 { "daemonize", 0, 0, 'd', },
158 { "background", 0, 0, 'b', },
159 { "group", 1, 0, 'g', },
160 { "help", 0, 0, 'h', },
161 { "output", 1, 0, 'o', },
162 { "verbose", 0, 0, 'v', },
163 { "config", 1, 0, 'f' },
167 static const char *config_ignore_options
[] = { "help", "config" };
170 * Take an option from the getopt output and set it in the right variable to be
173 * Return 0 on success else a negative value.
175 static int set_option(int opt
, const char *arg
, const char *optname
)
181 fprintf(stderr
, "option %s", optname
);
183 fprintf(stderr
, " with arg %s\n", arg
);
187 if (lttng_is_setuid_setgid()) {
188 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
189 "-C, --control-port");
191 ret
= uri_parse(arg
, &control_uri
);
193 ERR("Invalid control URI specified");
196 if (control_uri
->port
== 0) {
197 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
202 if (lttng_is_setuid_setgid()) {
203 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
206 ret
= uri_parse(arg
, &data_uri
);
208 ERR("Invalid data URI specified");
211 if (data_uri
->port
== 0) {
212 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
217 if (lttng_is_setuid_setgid()) {
218 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
221 ret
= uri_parse(arg
, &live_uri
);
223 ERR("Invalid live URI specified");
226 if (live_uri
->port
== 0) {
227 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
238 if (lttng_is_setuid_setgid()) {
239 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
242 tracing_group_name
= strdup(arg
);
243 if (tracing_group_name
== NULL
) {
248 tracing_group_name_override
= 1;
252 ret
= utils_show_man_page(8, "lttng-relayd");
254 ERR("Cannot view man page lttng-relayd(8)");
259 if (lttng_is_setuid_setgid()) {
260 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
263 ret
= asprintf(&opt_output_path
, "%s", arg
);
266 PERROR("asprintf opt_output_path");
272 /* Verbose level can increase using multiple -v */
274 lttng_opt_verbose
= config_parse_value(arg
);
276 /* Only 3 level of verbosity (-vvv). */
277 if (lttng_opt_verbose
< 3) {
278 lttng_opt_verbose
+= 1;
283 /* Unknown option or other error.
284 * Error is printed by getopt, just return */
297 * config_entry_handler_cb used to handle options read from a config file.
298 * See config_entry_handler_cb comment in common/config/session-config.h for the
299 * return value conventions.
301 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
305 if (!entry
|| !entry
->name
|| !entry
->value
) {
310 /* Check if the option is to be ignored */
311 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
312 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
317 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
318 /* Ignore if entry name is not fully matched. */
319 if (strcmp(entry
->name
, long_options
[i
].name
)) {
324 * If the option takes no argument on the command line,
325 * we have to check if the value is "true". We support
326 * non-zero numeric values, true, on and yes.
328 if (!long_options
[i
].has_arg
) {
329 ret
= config_parse_value(entry
->value
);
332 WARN("Invalid configuration value \"%s\" for option %s",
333 entry
->value
, entry
->name
);
335 /* False, skip boolean config option. */
340 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
344 WARN("Unrecognized option \"%s\" in daemon configuration file.",
351 static int set_options(int argc
, char **argv
)
353 int c
, ret
= 0, option_index
= 0, retval
= 0;
354 int orig_optopt
= optopt
, orig_optind
= optind
;
355 char *default_address
, *optstring
;
356 const char *config_path
= NULL
;
358 optstring
= utils_generate_optstring(long_options
,
359 sizeof(long_options
) / sizeof(struct option
));
365 /* Check for the --config option */
367 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
368 &option_index
)) != -1) {
372 } else if (c
!= 'f') {
376 if (lttng_is_setuid_setgid()) {
377 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
380 config_path
= utils_expand_path(optarg
);
382 ERR("Failed to resolve path: %s", optarg
);
387 ret
= config_get_section_entries(config_path
, config_section_name
,
388 config_entry_handler
, NULL
);
391 ERR("Invalid configuration option at line %i", ret
);
397 /* Reset getopt's global state */
398 optopt
= orig_optopt
;
399 optind
= orig_optind
;
401 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
406 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
413 /* assign default values */
414 if (control_uri
== NULL
) {
415 ret
= asprintf(&default_address
,
416 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
417 DEFAULT_NETWORK_CONTROL_PORT
);
419 PERROR("asprintf default data address");
424 ret
= uri_parse(default_address
, &control_uri
);
425 free(default_address
);
427 ERR("Invalid control URI specified");
432 if (data_uri
== NULL
) {
433 ret
= asprintf(&default_address
,
434 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
435 DEFAULT_NETWORK_DATA_PORT
);
437 PERROR("asprintf default data address");
442 ret
= uri_parse(default_address
, &data_uri
);
443 free(default_address
);
445 ERR("Invalid data URI specified");
450 if (live_uri
== NULL
) {
451 ret
= asprintf(&default_address
,
452 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
453 DEFAULT_NETWORK_VIEWER_PORT
);
455 PERROR("asprintf default viewer control address");
460 ret
= uri_parse(default_address
, &live_uri
);
461 free(default_address
);
463 ERR("Invalid viewer control URI specified");
474 static void print_global_objects(void)
476 rcu_register_thread();
478 print_viewer_streams();
479 print_relay_streams();
482 rcu_unregister_thread();
488 static void relayd_cleanup(void)
490 print_global_objects();
494 if (viewer_streams_ht
)
495 lttng_ht_destroy(viewer_streams_ht
);
496 if (relay_streams_ht
)
497 lttng_ht_destroy(relay_streams_ht
);
499 lttng_ht_destroy(sessions_ht
);
501 /* free the dynamically allocated opt_output_path */
502 free(opt_output_path
);
504 /* Close thread quit pipes */
505 utils_close_pipe(thread_quit_pipe
);
507 uri_free(control_uri
);
509 /* Live URI is freed in the live thread. */
511 if (tracing_group_name_override
) {
512 free((void *) tracing_group_name
);
517 * Write to writable pipe used to notify a thread.
519 static int notify_thread_pipe(int wpipe
)
523 ret
= lttng_write(wpipe
, "!", 1);
525 PERROR("write poll pipe");
533 static int notify_health_quit_pipe(int *pipe
)
537 ret
= lttng_write(pipe
[1], "4", 1);
539 PERROR("write relay health quit");
548 * Stop all relayd and relayd-live threads.
550 int lttng_relay_stop_threads(void)
554 /* Stopping all threads */
555 DBG("Terminating all threads");
556 if (notify_thread_pipe(thread_quit_pipe
[1])) {
557 ERR("write error on thread quit pipe");
561 if (notify_health_quit_pipe(health_quit_pipe
)) {
562 ERR("write error on health quit pipe");
565 /* Dispatch thread */
566 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
567 futex_nto1_wake(&relay_conn_queue
.futex
);
569 if (relayd_live_stop()) {
570 ERR("Error stopping live threads");
577 * Signal handler for the daemon
579 * Simply stop all worker threads, leaving main() return gracefully after
580 * joining all threads and calling cleanup().
582 static void sighandler(int sig
)
586 DBG("SIGPIPE caught");
589 DBG("SIGINT caught");
590 if (lttng_relay_stop_threads()) {
591 ERR("Error stopping threads");
595 DBG("SIGTERM caught");
596 if (lttng_relay_stop_threads()) {
597 ERR("Error stopping threads");
601 CMM_STORE_SHARED(recv_child_signal
, 1);
609 * Setup signal handler for :
610 * SIGINT, SIGTERM, SIGPIPE
612 static int set_signal_handler(void)
618 if ((ret
= sigemptyset(&sigset
)) < 0) {
619 PERROR("sigemptyset");
623 sa
.sa_handler
= sighandler
;
626 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
631 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
636 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
641 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
646 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
651 void lttng_relay_notify_ready(void)
653 /* Notify the parent of the fork() process that we are ready. */
654 if (opt_daemon
|| opt_background
) {
655 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
656 kill(child_ppid
, SIGUSR1
);
662 * Init thread quit pipe.
664 * Return -1 on error or 0 if all pipes are created.
666 static int init_thread_quit_pipe(void)
670 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
676 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
678 static int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
682 if (events
== NULL
|| size
== 0) {
687 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
693 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
705 * Check if the thread quit pipe was triggered.
707 * Return 1 if it was triggered else 0;
709 static int check_thread_quit_pipe(int fd
, uint32_t events
)
711 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
719 * Create and init socket from uri.
721 static struct lttcomm_sock
*relay_socket_create(struct lttng_uri
*uri
)
724 struct lttcomm_sock
*sock
= NULL
;
726 sock
= lttcomm_alloc_sock_from_uri(uri
);
728 ERR("Allocating socket");
732 ret
= lttcomm_create_sock(sock
);
736 DBG("Listening on sock %d", sock
->fd
);
738 ret
= sock
->ops
->bind(sock
);
743 ret
= sock
->ops
->listen(sock
, -1);
753 lttcomm_destroy_sock(sock
);
759 * This thread manages the listening for new connections on the network
761 static void *relay_thread_listener(void *data
)
763 int i
, ret
, pollfd
, err
= -1;
764 uint32_t revents
, nb_fd
;
765 struct lttng_poll_event events
;
766 struct lttcomm_sock
*control_sock
, *data_sock
;
768 DBG("[thread] Relay listener started");
770 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
772 health_code_update();
774 control_sock
= relay_socket_create(control_uri
);
776 goto error_sock_control
;
779 data_sock
= relay_socket_create(data_uri
);
781 goto error_sock_relay
;
785 * Pass 3 as size here for the thread quit pipe, control and
788 ret
= create_thread_poll_set(&events
, 3);
790 goto error_create_poll
;
793 /* Add the control socket */
794 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
799 /* Add the data socket */
800 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
805 lttng_relay_notify_ready();
807 if (testpoint(relayd_thread_listener
)) {
808 goto error_testpoint
;
812 health_code_update();
814 DBG("Listener accepting connections");
818 ret
= lttng_poll_wait(&events
, -1);
822 * Restart interrupted system call.
824 if (errno
== EINTR
) {
832 DBG("Relay new connection received");
833 for (i
= 0; i
< nb_fd
; i
++) {
834 health_code_update();
836 /* Fetch once the poll data */
837 revents
= LTTNG_POLL_GETEV(&events
, i
);
838 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
842 * No activity for this FD (poll
848 /* Thread quit pipe has been closed. Killing thread. */
849 ret
= check_thread_quit_pipe(pollfd
, revents
);
855 if (revents
& LPOLLIN
) {
857 * A new connection is requested, therefore a
858 * sessiond/consumerd connection is allocated in
859 * this thread, enqueued to a global queue and
860 * dequeued (and freed) in the worker thread.
863 struct relay_connection
*new_conn
;
864 struct lttcomm_sock
*newsock
;
865 enum connection_type type
;
867 if (pollfd
== data_sock
->fd
) {
869 newsock
= data_sock
->ops
->accept(data_sock
);
870 DBG("Relay data connection accepted, socket %d",
873 assert(pollfd
== control_sock
->fd
);
874 type
= RELAY_CONTROL
;
875 newsock
= control_sock
->ops
->accept(control_sock
);
876 DBG("Relay control connection accepted, socket %d",
880 PERROR("accepting sock");
884 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
887 PERROR("setsockopt inet");
888 lttcomm_destroy_sock(newsock
);
891 new_conn
= connection_create(newsock
, type
);
893 lttcomm_destroy_sock(newsock
);
897 /* Enqueue request for the dispatcher thread. */
898 cds_wfcq_enqueue(&relay_conn_queue
.head
, &relay_conn_queue
.tail
,
902 * Wake the dispatch queue futex.
903 * Implicit memory barrier with the
904 * exchange in cds_wfcq_enqueue.
906 futex_nto1_wake(&relay_conn_queue
.futex
);
907 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
908 ERR("socket poll error");
911 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
921 lttng_poll_clean(&events
);
923 if (data_sock
->fd
>= 0) {
924 ret
= data_sock
->ops
->close(data_sock
);
929 lttcomm_destroy_sock(data_sock
);
931 if (control_sock
->fd
>= 0) {
932 ret
= control_sock
->ops
->close(control_sock
);
937 lttcomm_destroy_sock(control_sock
);
941 ERR("Health error occurred in %s", __func__
);
943 health_unregister(health_relayd
);
944 DBG("Relay listener thread cleanup complete");
945 lttng_relay_stop_threads();
950 * This thread manages the dispatching of the requests to worker threads
952 static void *relay_thread_dispatcher(void *data
)
956 struct cds_wfcq_node
*node
;
957 struct relay_connection
*new_conn
= NULL
;
959 DBG("[thread] Relay dispatcher started");
961 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
963 if (testpoint(relayd_thread_dispatcher
)) {
964 goto error_testpoint
;
967 health_code_update();
969 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
970 health_code_update();
972 /* Atomically prepare the queue futex */
973 futex_nto1_prepare(&relay_conn_queue
.futex
);
976 health_code_update();
978 /* Dequeue commands */
979 node
= cds_wfcq_dequeue_blocking(&relay_conn_queue
.head
,
980 &relay_conn_queue
.tail
);
982 DBG("Woken up but nothing in the relay command queue");
983 /* Continue thread execution */
986 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
988 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
991 * Inform worker thread of the new request. This
992 * call is blocking so we can be assured that
993 * the data will be read at some point in time
994 * or wait to the end of the world :)
996 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
998 PERROR("write connection pipe");
999 connection_put(new_conn
);
1002 } while (node
!= NULL
);
1004 /* Futex wait on queue. Blocking call on futex() */
1005 health_poll_entry();
1006 futex_nto1_wait(&relay_conn_queue
.futex
);
1010 /* Normal exit, no error */
1017 ERR("Health error occurred in %s", __func__
);
1019 health_unregister(health_relayd
);
1020 DBG("Dispatch thread dying");
1021 lttng_relay_stop_threads();
1026 * Set index data from the control port to a given index object.
1028 static int set_index_control_data(struct relay_index
*index
,
1029 struct lttcomm_relayd_index
*data
,
1030 struct relay_connection
*conn
)
1032 struct ctf_packet_index index_data
;
1035 * The index on disk is encoded in big endian, so we don't need
1036 * to convert the data received on the network. The data_offset
1037 * value is NEVER modified here and is updated by the data
1040 index_data
.packet_size
= data
->packet_size
;
1041 index_data
.content_size
= data
->content_size
;
1042 index_data
.timestamp_begin
= data
->timestamp_begin
;
1043 index_data
.timestamp_end
= data
->timestamp_end
;
1044 index_data
.events_discarded
= data
->events_discarded
;
1045 index_data
.stream_id
= data
->stream_id
;
1047 if (conn
->minor
>= 8) {
1048 index
->index_data
.stream_instance_id
= data
->stream_instance_id
;
1049 index
->index_data
.packet_seq_num
= data
->packet_seq_num
;
1052 return relay_index_set_data(index
, &index_data
);
1056 * Handle the RELAYD_CREATE_SESSION command.
1058 * On success, send back the session id or else return a negative value.
1060 static int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
1061 struct relay_connection
*conn
)
1063 int ret
= 0, send_ret
;
1064 struct relay_session
*session
;
1065 struct lttcomm_relayd_status_session reply
;
1066 char session_name
[LTTNG_NAME_MAX
];
1067 char hostname
[LTTNG_HOST_NAME_MAX
];
1068 uint32_t live_timer
= 0;
1069 bool snapshot
= false;
1071 memset(session_name
, 0, LTTNG_NAME_MAX
);
1072 memset(hostname
, 0, LTTNG_HOST_NAME_MAX
);
1074 memset(&reply
, 0, sizeof(reply
));
1076 switch (conn
->minor
) {
1081 case 4: /* LTTng sessiond 2.4 */
1083 ret
= cmd_create_session_2_4(conn
, session_name
,
1084 hostname
, &live_timer
, &snapshot
);
1090 session
= session_create(session_name
, hostname
, live_timer
,
1091 snapshot
, conn
->major
, conn
->minor
);
1096 assert(!conn
->session
);
1097 conn
->session
= session
;
1098 DBG("Created session %" PRIu64
, session
->id
);
1100 reply
.session_id
= htobe64(session
->id
);
1104 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1106 reply
.ret_code
= htobe32(LTTNG_OK
);
1109 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1111 ERR("Relayd sending session id");
1119 * When we have received all the streams and the metadata for a channel,
1120 * we make them visible to the viewer threads.
1122 static void publish_connection_local_streams(struct relay_connection
*conn
)
1124 struct relay_stream
*stream
;
1125 struct relay_session
*session
= conn
->session
;
1128 * We publish all streams belonging to a session atomically wrt
1131 pthread_mutex_lock(&session
->lock
);
1133 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
1135 stream_publish(stream
);
1140 * Inform the viewer that there are new streams in the session.
1142 if (session
->viewer_attached
) {
1143 uatomic_set(&session
->new_streams
, 1);
1145 pthread_mutex_unlock(&session
->lock
);
1149 * relay_add_stream: allocate a new stream for a session
1151 static int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1152 struct relay_connection
*conn
)
1156 struct relay_session
*session
= conn
->session
;
1157 struct relay_stream
*stream
= NULL
;
1158 struct lttcomm_relayd_status_stream reply
;
1159 struct ctf_trace
*trace
= NULL
;
1160 uint64_t stream_handle
= -1ULL;
1161 char *path_name
= NULL
, *channel_name
= NULL
;
1162 uint64_t tracefile_size
= 0, tracefile_count
= 0;
1164 if (!session
|| conn
->version_check_done
== 0) {
1165 ERR("Trying to add a stream before version check");
1167 goto end_no_session
;
1170 switch (session
->minor
) {
1171 case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */
1172 ret
= cmd_recv_stream_2_1(conn
, &path_name
,
1175 case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
1177 ret
= cmd_recv_stream_2_2(conn
, &path_name
,
1178 &channel_name
, &tracefile_size
, &tracefile_count
);
1185 trace
= ctf_trace_get_by_path_or_create(session
, path_name
);
1189 /* This stream here has one reference on the trace. */
1191 pthread_mutex_lock(&last_relay_stream_id_lock
);
1192 stream_handle
= ++last_relay_stream_id
;
1193 pthread_mutex_unlock(&last_relay_stream_id_lock
);
1195 /* We pass ownership of path_name and channel_name. */
1196 stream
= stream_create(trace
, stream_handle
, path_name
,
1197 channel_name
, tracefile_size
, tracefile_count
);
1199 channel_name
= NULL
;
1202 * Streams are the owners of their trace. Reference to trace is
1203 * kept within stream_create().
1205 ctf_trace_put(trace
);
1208 memset(&reply
, 0, sizeof(reply
));
1209 reply
.handle
= htobe64(stream_handle
);
1211 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1213 reply
.ret_code
= htobe32(LTTNG_OK
);
1216 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1217 sizeof(struct lttcomm_relayd_status_stream
), 0);
1219 ERR("Relay sending stream id");
1220 ret
= (int) send_ret
;
1230 * relay_close_stream: close a specific stream
1232 static int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1233 struct relay_connection
*conn
)
1236 struct relay_session
*session
= conn
->session
;
1237 struct lttcomm_relayd_close_stream stream_info
;
1238 struct lttcomm_relayd_generic_reply reply
;
1239 struct relay_stream
*stream
;
1241 DBG("Close stream received");
1243 if (!session
|| conn
->version_check_done
== 0) {
1244 ERR("Trying to close a stream before version check");
1246 goto end_no_session
;
1249 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1250 sizeof(struct lttcomm_relayd_close_stream
), 0);
1251 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1253 /* Orderly shutdown. Not necessary to print an error. */
1254 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1256 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1259 goto end_no_session
;
1262 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1269 * Set last_net_seq_num before the close flag. Required by data
1272 pthread_mutex_lock(&stream
->lock
);
1273 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1274 pthread_mutex_unlock(&stream
->lock
);
1277 * This is one of the conditions which may trigger a stream close
1278 * with the others being:
1279 * 1) A close command is received for a stream
1280 * 2) The control connection owning the stream is closed
1281 * 3) We have received all of the stream's data _after_ a close
1284 try_stream_close(stream
);
1285 if (stream
->is_metadata
) {
1286 struct relay_viewer_stream
*vstream
;
1288 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
1290 if (vstream
->metadata_sent
== stream
->metadata_received
) {
1292 * Since all the metadata has been sent to the
1293 * viewer and that we have a request to close
1294 * its stream, we can safely teardown the
1295 * corresponding metadata viewer stream.
1297 viewer_stream_put(vstream
);
1299 /* Put local reference. */
1300 viewer_stream_put(vstream
);
1306 memset(&reply
, 0, sizeof(reply
));
1308 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1310 reply
.ret_code
= htobe32(LTTNG_OK
);
1312 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1313 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1315 ERR("Relay sending stream id");
1324 * relay_reset_metadata: reset a metadata stream
1327 int relay_reset_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1328 struct relay_connection
*conn
)
1331 struct relay_session
*session
= conn
->session
;
1332 struct lttcomm_relayd_reset_metadata stream_info
;
1333 struct lttcomm_relayd_generic_reply reply
;
1334 struct relay_stream
*stream
;
1336 DBG("Reset metadata received");
1338 if (!session
|| conn
->version_check_done
== 0) {
1339 ERR("Trying to reset a metadata stream before version check");
1341 goto end_no_session
;
1344 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1345 sizeof(struct lttcomm_relayd_reset_metadata
), 0);
1346 if (ret
< sizeof(struct lttcomm_relayd_reset_metadata
)) {
1348 /* Orderly shutdown. Not necessary to print an error. */
1349 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1351 ERR("Relay didn't receive valid reset_metadata struct "
1355 goto end_no_session
;
1357 DBG("Update metadata to version %" PRIu64
, be64toh(stream_info
.version
));
1359 /* Unsupported for live sessions for now. */
1360 if (session
->live_timer
!= 0) {
1365 stream
= stream_get_by_id(be64toh(stream_info
.stream_id
));
1370 pthread_mutex_lock(&stream
->lock
);
1371 if (!stream
->is_metadata
) {
1376 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1377 0, 0, -1, -1, stream
->stream_fd
->fd
, NULL
,
1378 &stream
->stream_fd
->fd
);
1380 ERR("Failed to rotate metadata file %s of channel %s",
1381 stream
->path_name
, stream
->channel_name
);
1386 pthread_mutex_unlock(&stream
->lock
);
1390 memset(&reply
, 0, sizeof(reply
));
1392 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1394 reply
.ret_code
= htobe32(LTTNG_OK
);
1396 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1397 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1399 ERR("Relay sending reset metadata reply");
1408 * relay_unknown_command: send -1 if received unknown command
1410 static void relay_unknown_command(struct relay_connection
*conn
)
1412 struct lttcomm_relayd_generic_reply reply
;
1415 memset(&reply
, 0, sizeof(reply
));
1416 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1417 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1418 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1420 ERR("Relay sending unknown command");
1425 * relay_start: send an acknowledgment to the client to tell if we are
1426 * ready to receive data. We are ready if a session is established.
1428 static int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1429 struct relay_connection
*conn
)
1431 int ret
= htobe32(LTTNG_OK
);
1432 struct lttcomm_relayd_generic_reply reply
;
1433 struct relay_session
*session
= conn
->session
;
1436 DBG("Trying to start the streaming without a session established");
1437 ret
= htobe32(LTTNG_ERR_UNK
);
1440 memset(&reply
, 0, sizeof(reply
));
1441 reply
.ret_code
= ret
;
1442 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1443 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1445 ERR("Relay sending start ack");
1452 * Append padding to the file pointed by the file descriptor fd.
1454 static int write_padding_to_file(int fd
, uint32_t size
)
1463 zeros
= zmalloc(size
);
1464 if (zeros
== NULL
) {
1465 PERROR("zmalloc zeros for padding");
1470 ret
= lttng_write(fd
, zeros
, size
);
1472 PERROR("write padding to file");
1482 * relay_recv_metadata: receive the metadata for the session.
1484 static int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1485 struct relay_connection
*conn
)
1489 struct relay_session
*session
= conn
->session
;
1490 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1491 struct relay_stream
*metadata_stream
;
1492 uint64_t data_size
, payload_size
;
1495 ERR("Metadata sent before version check");
1500 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1501 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1502 ERR("Incorrect data size");
1506 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1508 if (data_buffer_size
< data_size
) {
1509 /* In case the realloc fails, we can free the memory */
1512 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1513 if (!tmp_data_ptr
) {
1514 ERR("Allocating data buffer");
1519 data_buffer
= tmp_data_ptr
;
1520 data_buffer_size
= data_size
;
1522 memset(data_buffer
, 0, data_size
);
1523 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1524 size_ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1525 if (size_ret
< 0 || size_ret
!= data_size
) {
1526 if (size_ret
== 0) {
1527 /* Orderly shutdown. Not necessary to print an error. */
1528 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1530 ERR("Relay didn't receive the whole metadata");
1535 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1537 metadata_stream
= stream_get_by_id(be64toh(metadata_struct
->stream_id
));
1538 if (!metadata_stream
) {
1543 pthread_mutex_lock(&metadata_stream
->lock
);
1545 size_ret
= lttng_write(metadata_stream
->stream_fd
->fd
, metadata_struct
->payload
,
1547 if (size_ret
< payload_size
) {
1548 ERR("Relay error writing metadata on file");
1553 size_ret
= write_padding_to_file(metadata_stream
->stream_fd
->fd
,
1554 be32toh(metadata_struct
->padding_size
));
1559 metadata_stream
->metadata_received
+=
1560 payload_size
+ be32toh(metadata_struct
->padding_size
);
1561 DBG2("Relay metadata written. Updated metadata_received %" PRIu64
,
1562 metadata_stream
->metadata_received
);
1565 pthread_mutex_unlock(&metadata_stream
->lock
);
1566 stream_put(metadata_stream
);
1572 * relay_send_version: send relayd version number
1574 static int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1575 struct relay_connection
*conn
)
1578 struct lttcomm_relayd_version reply
, msg
;
1580 conn
->version_check_done
= 1;
1582 /* Get version from the other side. */
1583 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1584 if (ret
< 0 || ret
!= sizeof(msg
)) {
1586 /* Orderly shutdown. Not necessary to print an error. */
1587 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1589 ERR("Relay failed to receive the version values.");
1595 memset(&reply
, 0, sizeof(reply
));
1596 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1597 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1599 /* Major versions must be the same */
1600 if (reply
.major
!= be32toh(msg
.major
)) {
1601 DBG("Incompatible major versions (%u vs %u), deleting session",
1602 reply
.major
, be32toh(msg
.major
));
1603 connection_put(conn
);
1608 conn
->major
= reply
.major
;
1609 /* We adapt to the lowest compatible version */
1610 if (reply
.minor
<= be32toh(msg
.minor
)) {
1611 conn
->minor
= reply
.minor
;
1613 conn
->minor
= be32toh(msg
.minor
);
1616 reply
.major
= htobe32(reply
.major
);
1617 reply
.minor
= htobe32(reply
.minor
);
1618 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1619 sizeof(struct lttcomm_relayd_version
), 0);
1621 ERR("Relay sending version");
1624 DBG("Version check done using protocol %u.%u", conn
->major
,
1632 * Check for data pending for a given stream id from the session daemon.
1634 static int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1635 struct relay_connection
*conn
)
1637 struct relay_session
*session
= conn
->session
;
1638 struct lttcomm_relayd_data_pending msg
;
1639 struct lttcomm_relayd_generic_reply reply
;
1640 struct relay_stream
*stream
;
1642 uint64_t last_net_seq_num
, stream_id
;
1644 DBG("Data pending command received");
1646 if (!session
|| conn
->version_check_done
== 0) {
1647 ERR("Trying to check for data before version check");
1649 goto end_no_session
;
1652 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1653 if (ret
< sizeof(msg
)) {
1655 /* Orderly shutdown. Not necessary to print an error. */
1656 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1658 ERR("Relay didn't receive valid data_pending struct size : %d",
1662 goto end_no_session
;
1665 stream_id
= be64toh(msg
.stream_id
);
1666 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1668 stream
= stream_get_by_id(stream_id
);
1669 if (stream
== NULL
) {
1674 pthread_mutex_lock(&stream
->lock
);
1676 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1677 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1680 /* Avoid wrapping issue */
1681 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1682 /* Data has in fact been written and is NOT pending */
1685 /* Data still being streamed thus pending */
1689 stream
->data_pending_check_done
= true;
1690 pthread_mutex_unlock(&stream
->lock
);
1695 memset(&reply
, 0, sizeof(reply
));
1696 reply
.ret_code
= htobe32(ret
);
1697 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1699 ERR("Relay data pending ret code failed");
1707 * Wait for the control socket to reach a quiescent state.
1709 * Note that for now, when receiving this command from the session
1710 * daemon, this means that every subsequent commands or data received on
1711 * the control socket has been handled. So, this is why we simply return
1714 static int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1715 struct relay_connection
*conn
)
1719 struct relay_stream
*stream
;
1720 struct lttcomm_relayd_quiescent_control msg
;
1721 struct lttcomm_relayd_generic_reply reply
;
1723 DBG("Checking quiescent state on control socket");
1725 if (!conn
->session
|| conn
->version_check_done
== 0) {
1726 ERR("Trying to check for data before version check");
1728 goto end_no_session
;
1731 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1732 if (ret
< sizeof(msg
)) {
1734 /* Orderly shutdown. Not necessary to print an error. */
1735 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1737 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1741 goto end_no_session
;
1744 stream_id
= be64toh(msg
.stream_id
);
1745 stream
= stream_get_by_id(stream_id
);
1749 pthread_mutex_lock(&stream
->lock
);
1750 stream
->data_pending_check_done
= true;
1751 pthread_mutex_unlock(&stream
->lock
);
1752 DBG("Relay quiescent control pending flag set to %" PRIu64
, stream_id
);
1755 memset(&reply
, 0, sizeof(reply
));
1756 reply
.ret_code
= htobe32(LTTNG_OK
);
1757 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1759 ERR("Relay data quiescent control ret code failed");
1767 * Initialize a data pending command. This means that a consumer is about
1768 * to ask for data pending for each stream it holds. Simply iterate over
1769 * all streams of a session and set the data_pending_check_done flag.
1771 * This command returns to the client a LTTNG_OK code.
1773 static int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1774 struct relay_connection
*conn
)
1777 struct lttng_ht_iter iter
;
1778 struct lttcomm_relayd_begin_data_pending msg
;
1779 struct lttcomm_relayd_generic_reply reply
;
1780 struct relay_stream
*stream
;
1781 uint64_t session_id
;
1786 DBG("Init streams for data pending");
1788 if (!conn
->session
|| conn
->version_check_done
== 0) {
1789 ERR("Trying to check for data before version check");
1791 goto end_no_session
;
1794 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1795 if (ret
< sizeof(msg
)) {
1797 /* Orderly shutdown. Not necessary to print an error. */
1798 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1800 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1804 goto end_no_session
;
1807 session_id
= be64toh(msg
.session_id
);
1810 * Iterate over all streams to set the begin data pending flag.
1811 * For now, the streams are indexed by stream handle so we have
1812 * to iterate over all streams to find the one associated with
1813 * the right session_id.
1816 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1818 if (!stream_get(stream
)) {
1821 if (stream
->trace
->session
->id
== session_id
) {
1822 pthread_mutex_lock(&stream
->lock
);
1823 stream
->data_pending_check_done
= false;
1824 pthread_mutex_unlock(&stream
->lock
);
1825 DBG("Set begin data pending flag to stream %" PRIu64
,
1826 stream
->stream_handle
);
1832 memset(&reply
, 0, sizeof(reply
));
1833 /* All good, send back reply. */
1834 reply
.ret_code
= htobe32(LTTNG_OK
);
1836 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1838 ERR("Relay begin data pending send reply failed");
1846 * End data pending command. This will check, for a given session id, if
1847 * each stream associated with it has its data_pending_check_done flag
1848 * set. If not, this means that the client lost track of the stream but
1849 * the data is still being streamed on our side. In this case, we inform
1850 * the client that data is in flight.
1852 * Return to the client if there is data in flight or not with a ret_code.
1854 static int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1855 struct relay_connection
*conn
)
1858 struct lttng_ht_iter iter
;
1859 struct lttcomm_relayd_end_data_pending msg
;
1860 struct lttcomm_relayd_generic_reply reply
;
1861 struct relay_stream
*stream
;
1862 uint64_t session_id
;
1863 uint32_t is_data_inflight
= 0;
1865 DBG("End data pending command");
1867 if (!conn
->session
|| conn
->version_check_done
== 0) {
1868 ERR("Trying to check for data before version check");
1870 goto end_no_session
;
1873 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1874 if (ret
< sizeof(msg
)) {
1876 /* Orderly shutdown. Not necessary to print an error. */
1877 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1879 ERR("Relay didn't receive valid end data_pending struct size: %d",
1883 goto end_no_session
;
1886 session_id
= be64toh(msg
.session_id
);
1889 * Iterate over all streams to see if the begin data pending
1893 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1895 if (!stream_get(stream
)) {
1898 if (stream
->trace
->session
->id
!= session_id
) {
1902 pthread_mutex_lock(&stream
->lock
);
1903 if (!stream
->data_pending_check_done
) {
1904 if (!stream
->closed
|| !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
1905 is_data_inflight
= 1;
1906 DBG("Data is still in flight for stream %" PRIu64
,
1907 stream
->stream_handle
);
1908 pthread_mutex_unlock(&stream
->lock
);
1913 pthread_mutex_unlock(&stream
->lock
);
1918 memset(&reply
, 0, sizeof(reply
));
1919 /* All good, send back reply. */
1920 reply
.ret_code
= htobe32(is_data_inflight
);
1922 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1924 ERR("Relay end data pending send reply failed");
1932 * Receive an index for a specific stream.
1934 * Return 0 on success else a negative value.
1936 static int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1937 struct relay_connection
*conn
)
1940 struct relay_session
*session
= conn
->session
;
1941 struct lttcomm_relayd_index index_info
;
1942 struct relay_index
*index
;
1943 struct lttcomm_relayd_generic_reply reply
;
1944 struct relay_stream
*stream
;
1945 uint64_t net_seq_num
;
1949 DBG("Relay receiving index");
1951 if (!session
|| conn
->version_check_done
== 0) {
1952 ERR("Trying to close a stream before version check");
1954 goto end_no_session
;
1957 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
1958 sizeof(index_info
), 0);
1959 if (ret
< sizeof(index_info
)) {
1961 /* Orderly shutdown. Not necessary to print an error. */
1962 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1964 ERR("Relay didn't receive valid index struct size : %d", ret
);
1967 goto end_no_session
;
1970 net_seq_num
= be64toh(index_info
.net_seq_num
);
1972 stream
= stream_get_by_id(be64toh(index_info
.relay_stream_id
));
1974 ERR("stream_get_by_id not found");
1978 pthread_mutex_lock(&stream
->lock
);
1980 /* Live beacon handling */
1981 if (index_info
.packet_size
== 0) {
1982 DBG("Received live beacon for stream %" PRIu64
,
1983 stream
->stream_handle
);
1986 * Only flag a stream inactive when it has already
1987 * received data and no indexes are in flight.
1989 if (stream
->index_received_seqcount
> 0
1990 && stream
->indexes_in_flight
== 0) {
1991 stream
->beacon_ts_end
=
1992 be64toh(index_info
.timestamp_end
);
1995 goto end_stream_put
;
1997 stream
->beacon_ts_end
= -1ULL;
2000 if (stream
->ctf_stream_id
== -1ULL) {
2001 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
2003 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
2006 ERR("relay_index_get_by_id_or_create index NULL");
2007 goto end_stream_put
;
2009 if (set_index_control_data(index
, &index_info
, conn
)) {
2010 ERR("set_index_control_data error");
2011 relay_index_put(index
);
2013 goto end_stream_put
;
2015 ret
= relay_index_try_flush(index
);
2017 tracefile_array_commit_seq(stream
->tfa
);
2018 stream
->index_received_seqcount
++;
2019 } else if (ret
> 0) {
2023 ERR("relay_index_try_flush error %d", ret
);
2024 relay_index_put(index
);
2029 pthread_mutex_unlock(&stream
->lock
);
2034 memset(&reply
, 0, sizeof(reply
));
2036 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2038 reply
.ret_code
= htobe32(LTTNG_OK
);
2040 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2042 ERR("Relay sending close index id reply");
2051 * Receive the streams_sent message.
2053 * Return 0 on success else a negative value.
2055 static int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
2056 struct relay_connection
*conn
)
2059 struct lttcomm_relayd_generic_reply reply
;
2063 DBG("Relay receiving streams_sent");
2065 if (!conn
->session
|| conn
->version_check_done
== 0) {
2066 ERR("Trying to close a stream before version check");
2068 goto end_no_session
;
2072 * Publish every pending stream in the connection recv list which are
2073 * now ready to be used by the viewer.
2075 publish_connection_local_streams(conn
);
2077 memset(&reply
, 0, sizeof(reply
));
2078 reply
.ret_code
= htobe32(LTTNG_OK
);
2079 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
2081 ERR("Relay sending sent_stream reply");
2093 * Process the commands received on the control socket
2095 static int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
2096 struct relay_connection
*conn
)
2100 switch (be32toh(recv_hdr
->cmd
)) {
2101 case RELAYD_CREATE_SESSION
:
2102 ret
= relay_create_session(recv_hdr
, conn
);
2104 case RELAYD_ADD_STREAM
:
2105 ret
= relay_add_stream(recv_hdr
, conn
);
2107 case RELAYD_START_DATA
:
2108 ret
= relay_start(recv_hdr
, conn
);
2110 case RELAYD_SEND_METADATA
:
2111 ret
= relay_recv_metadata(recv_hdr
, conn
);
2113 case RELAYD_VERSION
:
2114 ret
= relay_send_version(recv_hdr
, conn
);
2116 case RELAYD_CLOSE_STREAM
:
2117 ret
= relay_close_stream(recv_hdr
, conn
);
2119 case RELAYD_DATA_PENDING
:
2120 ret
= relay_data_pending(recv_hdr
, conn
);
2122 case RELAYD_QUIESCENT_CONTROL
:
2123 ret
= relay_quiescent_control(recv_hdr
, conn
);
2125 case RELAYD_BEGIN_DATA_PENDING
:
2126 ret
= relay_begin_data_pending(recv_hdr
, conn
);
2128 case RELAYD_END_DATA_PENDING
:
2129 ret
= relay_end_data_pending(recv_hdr
, conn
);
2131 case RELAYD_SEND_INDEX
:
2132 ret
= relay_recv_index(recv_hdr
, conn
);
2134 case RELAYD_STREAMS_SENT
:
2135 ret
= relay_streams_sent(recv_hdr
, conn
);
2137 case RELAYD_RESET_METADATA
:
2138 ret
= relay_reset_metadata(recv_hdr
, conn
);
2140 case RELAYD_UPDATE_SYNC_INFO
:
2142 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
2143 relay_unknown_command(conn
);
2153 * Handle index for a data stream.
2155 * Called with the stream lock held.
2157 * Return 0 on success else a negative value.
2159 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
2163 uint64_t data_offset
;
2164 struct relay_index
*index
;
2166 /* Get data offset because we are about to update the index. */
2167 data_offset
= htobe64(stream
->tracefile_size_current
);
2169 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
2170 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
2173 * Lookup for an existing index for that stream id/sequence
2174 * number. If it exists, the control thread has already received the
2175 * data for it, thus we need to write it to disk.
2177 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
2183 if (rotate_index
|| !stream
->index_fd
) {
2186 /* Put ref on previous index_fd. */
2187 if (stream
->index_fd
) {
2188 stream_fd_put(stream
->index_fd
);
2189 stream
->index_fd
= NULL
;
2192 fd
= index_create_file(stream
->path_name
, stream
->channel_name
,
2193 -1, -1, stream
->tracefile_size
,
2194 tracefile_array_get_file_index_head(stream
->tfa
));
2197 /* Put self-ref for this index due to error. */
2198 relay_index_put(index
);
2201 stream
->index_fd
= stream_fd_create(fd
);
2202 if (!stream
->index_fd
) {
2205 PERROR("Error closing FD %d", fd
);
2207 /* Put self-ref for this index due to error. */
2208 relay_index_put(index
);
2209 /* Will put the local ref. */
2214 if (relay_index_set_fd(index
, stream
->index_fd
, data_offset
)) {
2216 /* Put self-ref for this index due to error. */
2217 relay_index_put(index
);
2221 ret
= relay_index_try_flush(index
);
2223 tracefile_array_commit_seq(stream
->tfa
);
2224 stream
->index_received_seqcount
++;
2225 } else if (ret
> 0) {
2229 /* Put self-ref for this index due to error. */
2230 relay_index_put(index
);
2238 * relay_process_data: Process the data received on the data socket
2240 static int relay_process_data(struct relay_connection
*conn
)
2242 int ret
= 0, rotate_index
= 0;
2244 struct relay_stream
*stream
;
2245 struct lttcomm_relayd_data_hdr data_hdr
;
2247 uint64_t net_seq_num
;
2249 struct relay_session
*session
;
2250 bool new_stream
= false, close_requested
= false;
2251 size_t chunk_size
= RECV_DATA_BUFFER_SIZE
;
2252 size_t recv_off
= 0;
2253 char data_buffer
[chunk_size
];
2255 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2256 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2259 /* Orderly shutdown. Not necessary to print an error. */
2260 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2262 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2268 stream_id
= be64toh(data_hdr
.stream_id
);
2269 stream
= stream_get_by_id(stream_id
);
2271 ERR("relay_process_data: Cannot find stream %" PRIu64
, stream_id
);
2275 session
= stream
->trace
->session
;
2276 data_size
= be32toh(data_hdr
.data_size
);
2278 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2280 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2281 data_size
, stream_id
, net_seq_num
);
2283 pthread_mutex_lock(&stream
->lock
);
2285 /* Check if a rotation is needed. */
2286 if (stream
->tracefile_size
> 0 &&
2287 (stream
->tracefile_size_current
+ data_size
) >
2288 stream
->tracefile_size
) {
2289 uint64_t old_id
, new_id
;
2291 old_id
= tracefile_array_get_file_index_head(stream
->tfa
);
2292 tracefile_array_file_rotate(stream
->tfa
);
2294 /* new_id is updated by utils_rotate_stream_file. */
2297 ret
= utils_rotate_stream_file(stream
->path_name
,
2298 stream
->channel_name
, stream
->tracefile_size
,
2299 stream
->tracefile_count
, -1,
2300 -1, stream
->stream_fd
->fd
,
2301 &new_id
, &stream
->stream_fd
->fd
);
2303 ERR("Rotating stream output file");
2304 goto end_stream_unlock
;
2307 * Reset current size because we just performed a stream
2310 stream
->tracefile_size_current
= 0;
2315 * Index are handled in protocol version 2.4 and above. Also,
2316 * snapshot and index are NOT supported.
2318 if (session
->minor
>= 4 && !session
->snapshot
) {
2319 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2321 ERR("handle_index_data: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
2322 stream
->stream_handle
, net_seq_num
, ret
);
2323 goto end_stream_unlock
;
2327 for (recv_off
= 0; recv_off
< data_size
; recv_off
+= chunk_size
) {
2328 size_t recv_size
= min(data_size
- recv_off
, chunk_size
);
2330 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, recv_size
, 0);
2333 /* Orderly shutdown. Not necessary to print an error. */
2334 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2336 ERR("Socket %d error %d", conn
->sock
->fd
, ret
);
2339 goto end_stream_unlock
;
2342 /* Write data to stream output fd. */
2343 size_ret
= lttng_write(stream
->stream_fd
->fd
, data_buffer
,
2345 if (size_ret
< recv_size
) {
2346 ERR("Relay error writing data to file");
2348 goto end_stream_unlock
;
2351 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64
,
2352 size_ret
, stream
->stream_handle
);
2355 ret
= write_padding_to_file(stream
->stream_fd
->fd
,
2356 be32toh(data_hdr
.padding_size
));
2358 ERR("write_padding_to_file: fail stream %" PRIu64
" net_seq_num %" PRIu64
" ret %d",
2359 stream
->stream_handle
, net_seq_num
, ret
);
2360 goto end_stream_unlock
;
2362 stream
->tracefile_size_current
+=
2363 data_size
+ be32toh(data_hdr
.padding_size
);
2364 if (stream
->prev_seq
== -1ULL) {
2368 stream
->prev_seq
= net_seq_num
;
2371 close_requested
= stream
->close_requested
;
2372 pthread_mutex_unlock(&stream
->lock
);
2373 if (close_requested
) {
2374 try_stream_close(stream
);
2378 pthread_mutex_lock(&session
->lock
);
2379 uatomic_set(&session
->new_streams
, 1);
2380 pthread_mutex_unlock(&session
->lock
);
2387 static void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2391 (void) lttng_poll_del(events
, pollfd
);
2393 ret
= close(pollfd
);
2395 ERR("Closing pollfd %d", pollfd
);
2399 static void relay_thread_close_connection(struct lttng_poll_event
*events
,
2400 int pollfd
, struct relay_connection
*conn
)
2402 const char *type_str
;
2404 switch (conn
->type
) {
2409 type_str
= "Control";
2411 case RELAY_VIEWER_COMMAND
:
2412 type_str
= "Viewer Command";
2414 case RELAY_VIEWER_NOTIFICATION
:
2415 type_str
= "Viewer Notification";
2418 type_str
= "Unknown";
2420 cleanup_connection_pollfd(events
, pollfd
);
2421 connection_put(conn
);
2422 DBG("%s connection closed with %d", type_str
, pollfd
);
2426 * This thread does the actual work
2428 static void *relay_thread_worker(void *data
)
2430 int ret
, err
= -1, last_seen_data_fd
= -1;
2432 struct lttng_poll_event events
;
2433 struct lttng_ht
*relay_connections_ht
;
2434 struct lttng_ht_iter iter
;
2435 struct lttcomm_relayd_hdr recv_hdr
;
2436 struct relay_connection
*destroy_conn
= NULL
;
2438 DBG("[thread] Relay worker started");
2440 rcu_register_thread();
2442 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2444 if (testpoint(relayd_thread_worker
)) {
2445 goto error_testpoint
;
2448 health_code_update();
2450 /* table of connections indexed on socket */
2451 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2452 if (!relay_connections_ht
) {
2453 goto relay_connections_ht_error
;
2456 ret
= create_thread_poll_set(&events
, 2);
2458 goto error_poll_create
;
2461 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2468 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2470 health_code_update();
2472 /* Infinite blocking call, waiting for transmission */
2473 DBG3("Relayd worker thread polling...");
2474 health_poll_entry();
2475 ret
= lttng_poll_wait(&events
, -1);
2479 * Restart interrupted system call.
2481 if (errno
== EINTR
) {
2490 * Process control. The control connection is
2491 * prioritized so we don't starve it with high
2492 * throughput tracing data on the data connection.
2494 for (i
= 0; i
< nb_fd
; i
++) {
2495 /* Fetch once the poll data */
2496 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2497 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2499 health_code_update();
2503 * No activity for this FD (poll
2509 /* Thread quit pipe has been closed. Killing thread. */
2510 ret
= check_thread_quit_pipe(pollfd
, revents
);
2516 /* Inspect the relay conn pipe for new connection */
2517 if (pollfd
== relay_conn_pipe
[0]) {
2518 if (revents
& LPOLLIN
) {
2519 struct relay_connection
*conn
;
2521 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2525 lttng_poll_add(&events
, conn
->sock
->fd
,
2526 LPOLLIN
| LPOLLRDHUP
);
2527 connection_ht_add(relay_connections_ht
, conn
);
2528 DBG("Connection socket %d added", conn
->sock
->fd
);
2529 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2530 ERR("Relay connection pipe error");
2533 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2537 struct relay_connection
*ctrl_conn
;
2539 ctrl_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
2540 /* If not found, there is a synchronization issue. */
2543 if (ctrl_conn
->type
== RELAY_DATA
) {
2544 if (revents
& LPOLLIN
) {
2546 * Flag the last seen data fd not deleted. It will be
2547 * used as the last seen fd if any fd gets deleted in
2550 last_notdel_data_fd
= pollfd
;
2552 goto put_ctrl_connection
;
2554 assert(ctrl_conn
->type
== RELAY_CONTROL
);
2556 if (revents
& LPOLLIN
) {
2557 ret
= ctrl_conn
->sock
->ops
->recvmsg(ctrl_conn
->sock
,
2558 &recv_hdr
, sizeof(recv_hdr
), 0);
2560 /* Connection closed */
2561 relay_thread_close_connection(&events
, pollfd
,
2564 ret
= relay_process_control(&recv_hdr
, ctrl_conn
);
2566 /* Clear the session on error. */
2567 relay_thread_close_connection(&events
,
2572 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2573 relay_thread_close_connection(&events
,
2575 if (last_seen_data_fd
== pollfd
) {
2576 last_seen_data_fd
= last_notdel_data_fd
;
2579 ERR("Unexpected poll events %u for control sock %d",
2581 connection_put(ctrl_conn
);
2584 put_ctrl_connection
:
2585 connection_put(ctrl_conn
);
2590 * The last loop handled a control request, go back to poll to make
2591 * sure we prioritise the control socket.
2597 if (last_seen_data_fd
>= 0) {
2598 for (i
= 0; i
< nb_fd
; i
++) {
2599 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2601 health_code_update();
2603 if (last_seen_data_fd
== pollfd
) {
2610 /* Process data connection. */
2611 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2612 /* Fetch the poll data. */
2613 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2614 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2615 struct relay_connection
*data_conn
;
2617 health_code_update();
2620 /* No activity for this FD (poll implementation). */
2624 /* Skip the command pipe. It's handled in the first loop. */
2625 if (pollfd
== relay_conn_pipe
[0]) {
2629 data_conn
= connection_get_by_sock(relay_connections_ht
, pollfd
);
2631 /* Skip it. Might be removed before. */
2634 if (data_conn
->type
== RELAY_CONTROL
) {
2635 goto put_data_connection
;
2637 assert(data_conn
->type
== RELAY_DATA
);
2639 if (revents
& LPOLLIN
) {
2640 ret
= relay_process_data(data_conn
);
2641 /* Connection closed */
2643 relay_thread_close_connection(&events
, pollfd
,
2646 * Every goto restart call sets the last seen fd where
2647 * here we don't really care since we gracefully
2648 * continue the loop after the connection is deleted.
2651 /* Keep last seen port. */
2652 last_seen_data_fd
= pollfd
;
2653 connection_put(data_conn
);
2656 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2657 relay_thread_close_connection(&events
, pollfd
,
2660 ERR("Unknown poll events %u for data sock %d",
2663 put_data_connection
:
2664 connection_put(data_conn
);
2666 last_seen_data_fd
= -1;
2669 /* Normal exit, no error */
2674 /* Cleanup reamaining connection object. */
2676 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
,
2679 health_code_update();
2681 * No need to grab another ref, because we own
2684 relay_thread_close_connection(&events
, destroy_conn
->sock
->fd
,
2689 lttng_poll_clean(&events
);
2691 lttng_ht_destroy(relay_connections_ht
);
2692 relay_connections_ht_error
:
2693 /* Close relay conn pipes */
2694 utils_close_pipe(relay_conn_pipe
);
2696 DBG("Thread exited with error");
2698 DBG("Worker thread cleanup complete");
2702 ERR("Health error occurred in %s", __func__
);
2704 health_unregister(health_relayd
);
2705 rcu_unregister_thread();
2706 lttng_relay_stop_threads();
2711 * Create the relay command pipe to wake thread_manage_apps.
2712 * Closed in cleanup().
2714 static int create_relay_conn_pipe(void)
2718 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2726 int main(int argc
, char **argv
)
2728 int ret
= 0, retval
= 0;
2731 /* Parse arguments */
2733 if (set_options(argc
, argv
)) {
2738 if (set_signal_handler()) {
2743 /* Try to create directory if -o, --output is specified. */
2744 if (opt_output_path
) {
2745 if (*opt_output_path
!= '/') {
2746 ERR("Please specify an absolute path for -o, --output PATH");
2751 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
,
2754 ERR("Unable to create %s", opt_output_path
);
2761 if (opt_daemon
|| opt_background
) {
2764 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2772 * We are in the child. Make sure all other file
2773 * descriptors are closed, in case we are called with
2774 * more opened file descriptors than the standard ones.
2776 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2782 /* Initialize thread health monitoring */
2783 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2784 if (!health_relayd
) {
2785 PERROR("health_app_create error");
2787 goto exit_health_app_create
;
2790 /* Create thread quit pipe */
2791 if (init_thread_quit_pipe()) {
2793 goto exit_init_data
;
2796 /* Setup the thread apps communication pipe. */
2797 if (create_relay_conn_pipe()) {
2799 goto exit_init_data
;
2802 /* Init relay command queue. */
2803 cds_wfcq_init(&relay_conn_queue
.head
, &relay_conn_queue
.tail
);
2805 /* Set up max poll set size */
2806 if (lttng_poll_set_max_size()) {
2808 goto exit_init_data
;
2811 /* Initialize communication library */
2813 lttcomm_inet_init();
2815 /* tables of sessions indexed by session ID */
2816 sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2819 goto exit_init_data
;
2822 /* tables of streams indexed by stream ID */
2823 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2824 if (!relay_streams_ht
) {
2826 goto exit_init_data
;
2829 /* tables of streams indexed by stream ID */
2830 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2831 if (!viewer_streams_ht
) {
2833 goto exit_init_data
;
2836 ret
= utils_create_pipe(health_quit_pipe
);
2839 goto exit_health_quit_pipe
;
2842 /* Create thread to manage the client socket */
2843 ret
= pthread_create(&health_thread
, NULL
,
2844 thread_manage_health
, (void *) NULL
);
2847 PERROR("pthread_create health");
2849 goto exit_health_thread
;
2852 /* Setup the dispatcher thread */
2853 ret
= pthread_create(&dispatcher_thread
, NULL
,
2854 relay_thread_dispatcher
, (void *) NULL
);
2857 PERROR("pthread_create dispatcher");
2859 goto exit_dispatcher_thread
;
2862 /* Setup the worker thread */
2863 ret
= pthread_create(&worker_thread
, NULL
,
2864 relay_thread_worker
, NULL
);
2867 PERROR("pthread_create worker");
2869 goto exit_worker_thread
;
2872 /* Setup the listener thread */
2873 ret
= pthread_create(&listener_thread
, NULL
,
2874 relay_thread_listener
, (void *) NULL
);
2877 PERROR("pthread_create listener");
2879 goto exit_listener_thread
;
2882 ret
= relayd_live_create(live_uri
);
2884 ERR("Starting live viewer threads");
2890 * This is where we start awaiting program completion (e.g. through
2891 * signal that asks threads to teardown).
2894 ret
= relayd_live_join();
2900 ret
= pthread_join(listener_thread
, &status
);
2903 PERROR("pthread_join listener_thread");
2907 exit_listener_thread
:
2908 ret
= pthread_join(worker_thread
, &status
);
2911 PERROR("pthread_join worker_thread");
2916 ret
= pthread_join(dispatcher_thread
, &status
);
2919 PERROR("pthread_join dispatcher_thread");
2922 exit_dispatcher_thread
:
2924 ret
= pthread_join(health_thread
, &status
);
2927 PERROR("pthread_join health_thread");
2932 utils_close_pipe(health_quit_pipe
);
2933 exit_health_quit_pipe
:
2936 health_app_destroy(health_relayd
);
2937 exit_health_app_create
:
2941 /* Ensure all prior call_rcu are done. */