2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/daemonize.h>
48 #include <common/futex.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/sessiond-comm/inet.h>
51 #include <common/sessiond-comm/relayd.h>
52 #include <common/uri.h>
53 #include <common/utils.h>
56 #include "ctf-trace.h"
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
62 #include "testpoint.h"
63 #include "viewer-stream.h"
66 #include "connection.h"
68 /* command line options */
69 char *opt_output_path
;
70 static int opt_daemon
, opt_background
;
73 * We need to wait for listener and live listener threads, as well as
74 * health check thread, before being ready to signal readiness.
76 #define NR_LTTNG_RELAY_READY 3
77 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
78 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
79 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
81 static struct lttng_uri
*control_uri
;
82 static struct lttng_uri
*data_uri
;
83 static struct lttng_uri
*live_uri
;
87 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
90 * Quit pipe for all threads. This permits a single cancellation point
91 * for all threads when receiving an event on the pipe.
93 int thread_quit_pipe
[2] = { -1, -1 };
96 * This pipe is used to inform the worker thread that a command is queued and
97 * ready to be processed.
99 static int relay_conn_pipe
[2] = { -1, -1 };
101 /* Shared between threads */
102 static int dispatch_thread_exit
;
104 static pthread_t listener_thread
;
105 static pthread_t dispatcher_thread
;
106 static pthread_t worker_thread
;
107 static pthread_t health_thread
;
109 static uint64_t last_relay_stream_id
;
112 * Relay command queue.
114 * The relay_thread_listener and relay_thread_dispatcher communicate with this
117 static struct relay_conn_queue relay_conn_queue
;
119 /* buffer allocated at startup, used to store the trace data */
120 static char *data_buffer
;
121 static unsigned int data_buffer_size
;
123 /* We need those values for the file/dir creation. */
124 static uid_t relayd_uid
;
125 static gid_t relayd_gid
;
127 /* Global relay stream hash table. */
128 struct lttng_ht
*relay_streams_ht
;
130 /* Global relay viewer stream hash table. */
131 struct lttng_ht
*viewer_streams_ht
;
133 /* Global hash table that stores relay index object. */
134 struct lttng_ht
*indexes_ht
;
136 /* Relayd health monitoring */
137 struct health_app
*health_relayd
;
140 * usage function on stderr
145 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
146 fprintf(stderr
, " -h, --help Display this usage.\n");
147 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
148 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
149 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
150 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
151 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
152 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
153 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
154 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
158 int parse_args(int argc
, char **argv
)
162 char *default_address
;
164 static struct option long_options
[] = {
165 { "control-port", 1, 0, 'C', },
166 { "data-port", 1, 0, 'D', },
167 { "daemonize", 0, 0, 'd', },
168 { "group", 1, 0, 'g', },
169 { "help", 0, 0, 'h', },
170 { "output", 1, 0, 'o', },
171 { "verbose", 0, 0, 'v', },
172 { "background", 0, 0, 'b' },
177 int option_index
= 0;
178 c
= getopt_long(argc
, argv
, "dhv" "C:D:L:o:g:b",
179 long_options
, &option_index
);
186 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
188 fprintf(stderr
, " with arg %s\n", optarg
);
192 ret
= uri_parse(optarg
, &control_uri
);
194 ERR("Invalid control URI specified");
197 if (control_uri
->port
== 0) {
198 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
202 ret
= uri_parse(optarg
, &data_uri
);
204 ERR("Invalid data URI specified");
207 if (data_uri
->port
== 0) {
208 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
212 ret
= uri_parse(optarg
, &live_uri
);
214 ERR("Invalid live URI specified");
217 if (live_uri
->port
== 0) {
218 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
228 tracing_group_name
= optarg
;
234 ret
= asprintf(&opt_output_path
, "%s", optarg
);
236 PERROR("asprintf opt_output_path");
241 /* Verbose level can increase using multiple -v */
242 lttng_opt_verbose
+= 1;
245 /* Unknown option or other error.
246 * Error is printed by getopt, just return */
252 /* assign default values */
253 if (control_uri
== NULL
) {
254 ret
= asprintf(&default_address
,
255 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS
":%d",
256 DEFAULT_NETWORK_CONTROL_PORT
);
258 PERROR("asprintf default data address");
262 ret
= uri_parse(default_address
, &control_uri
);
263 free(default_address
);
265 ERR("Invalid control URI specified");
269 if (data_uri
== NULL
) {
270 ret
= asprintf(&default_address
,
271 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS
":%d",
272 DEFAULT_NETWORK_DATA_PORT
);
274 PERROR("asprintf default data address");
278 ret
= uri_parse(default_address
, &data_uri
);
279 free(default_address
);
281 ERR("Invalid data URI specified");
285 if (live_uri
== NULL
) {
286 ret
= asprintf(&default_address
,
287 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
":%d",
288 DEFAULT_NETWORK_VIEWER_PORT
);
290 PERROR("asprintf default viewer control address");
294 ret
= uri_parse(default_address
, &live_uri
);
295 free(default_address
);
297 ERR("Invalid viewer control URI specified");
314 /* free the dynamically allocated opt_output_path */
315 free(opt_output_path
);
317 /* Close thread quit pipes */
318 utils_close_pipe(thread_quit_pipe
);
320 uri_free(control_uri
);
322 /* Live URI is freed in the live thread. */
326 * Write to writable pipe used to notify a thread.
329 int notify_thread_pipe(int wpipe
)
333 ret
= lttng_write(wpipe
, "!", 1);
335 PERROR("write poll pipe");
341 static void notify_health_quit_pipe(int *pipe
)
345 ret
= lttng_write(pipe
[1], "4", 1);
347 PERROR("write relay health quit");
352 * Stop all threads by closing the thread quit pipe.
355 void stop_threads(void)
359 /* Stopping all threads */
360 DBG("Terminating all threads");
361 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
363 ERR("write error on thread quit pipe");
366 notify_health_quit_pipe(health_quit_pipe
);
368 /* Dispatch thread */
369 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
370 futex_nto1_wake(&relay_conn_queue
.futex
);
374 * Signal handler for the daemon
376 * Simply stop all worker threads, leaving main() return gracefully after
377 * joining all threads and calling cleanup().
380 void sighandler(int sig
)
384 DBG("SIGPIPE caught");
387 DBG("SIGINT caught");
391 DBG("SIGTERM caught");
395 CMM_STORE_SHARED(recv_child_signal
, 1);
403 * Setup signal handler for :
404 * SIGINT, SIGTERM, SIGPIPE
407 int set_signal_handler(void)
413 if ((ret
= sigemptyset(&sigset
)) < 0) {
414 PERROR("sigemptyset");
418 sa
.sa_handler
= sighandler
;
421 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
426 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
431 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
436 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
441 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
446 void lttng_relay_notify_ready(void)
448 /* Notify the parent of the fork() process that we are ready. */
449 if (opt_daemon
|| opt_background
) {
450 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
451 kill(child_ppid
, SIGUSR1
);
457 * Init thread quit pipe.
459 * Return -1 on error or 0 if all pipes are created.
462 int init_thread_quit_pipe(void)
466 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
472 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
475 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
479 if (events
== NULL
|| size
== 0) {
484 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
490 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
502 * Check if the thread quit pipe was triggered.
504 * Return 1 if it was triggered else 0;
507 int check_thread_quit_pipe(int fd
, uint32_t events
)
509 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
517 * Create and init socket from uri.
520 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
523 struct lttcomm_sock
*sock
= NULL
;
525 sock
= lttcomm_alloc_sock_from_uri(uri
);
527 ERR("Allocating socket");
531 ret
= lttcomm_create_sock(sock
);
535 DBG("Listening on sock %d", sock
->fd
);
537 ret
= sock
->ops
->bind(sock
);
542 ret
= sock
->ops
->listen(sock
, -1);
552 lttcomm_destroy_sock(sock
);
558 * Return nonzero if stream needs to be closed.
561 int close_stream_check(struct relay_stream
*stream
)
563 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
565 * We are about to close the stream so set the data pending flag to 1
566 * which will make the end data pending command skip the stream which
567 * is now closed and ready. Note that after proceeding to a file close,
568 * the written file is ready for reading.
570 stream
->data_pending_check_done
= 1;
576 static void try_close_stream(struct relay_session
*session
,
577 struct relay_stream
*stream
)
580 struct ctf_trace
*ctf_trace
;
585 if (!close_stream_check(stream
)) {
586 /* Can't close it, not ready for that. */
590 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
594 pthread_mutex_lock(&session
->viewer_ready_lock
);
595 ctf_trace
->invalid_flag
= 1;
596 pthread_mutex_unlock(&session
->viewer_ready_lock
);
598 ret
= stream_close(session
, stream
);
599 if (ret
|| session
->snapshot
) {
600 /* Already close thus the ctf trace is being or has been destroyed. */
604 ctf_trace_try_destroy(session
, ctf_trace
);
611 * This thread manages the listening for new connections on the network
614 void *relay_thread_listener(void *data
)
616 int i
, ret
, pollfd
, err
= -1;
617 uint32_t revents
, nb_fd
;
618 struct lttng_poll_event events
;
619 struct lttcomm_sock
*control_sock
, *data_sock
;
621 DBG("[thread] Relay listener started");
623 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
625 health_code_update();
627 control_sock
= relay_init_sock(control_uri
);
629 goto error_sock_control
;
632 data_sock
= relay_init_sock(data_uri
);
634 goto error_sock_relay
;
638 * Pass 3 as size here for the thread quit pipe, control and data socket.
640 ret
= create_thread_poll_set(&events
, 3);
642 goto error_create_poll
;
645 /* Add the control socket */
646 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
651 /* Add the data socket */
652 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
657 lttng_relay_notify_ready();
659 if (testpoint(relayd_thread_listener
)) {
660 goto error_testpoint
;
664 health_code_update();
666 DBG("Listener accepting connections");
670 ret
= lttng_poll_wait(&events
, -1);
674 * Restart interrupted system call.
676 if (errno
== EINTR
) {
684 DBG("Relay new connection received");
685 for (i
= 0; i
< nb_fd
; i
++) {
686 health_code_update();
688 /* Fetch once the poll data */
689 revents
= LTTNG_POLL_GETEV(&events
, i
);
690 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
692 /* Thread quit pipe has been closed. Killing thread. */
693 ret
= check_thread_quit_pipe(pollfd
, revents
);
699 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
700 ERR("socket poll error");
702 } else if (revents
& LPOLLIN
) {
704 * Get allocated in this thread, enqueued to a global queue,
705 * dequeued and freed in the worker thread.
708 struct relay_connection
*new_conn
;
709 struct lttcomm_sock
*newsock
;
711 new_conn
= connection_create();
716 if (pollfd
== data_sock
->fd
) {
717 new_conn
->type
= RELAY_DATA
;
718 newsock
= data_sock
->ops
->accept(data_sock
);
719 DBG("Relay data connection accepted, socket %d",
722 assert(pollfd
== control_sock
->fd
);
723 new_conn
->type
= RELAY_CONTROL
;
724 newsock
= control_sock
->ops
->accept(control_sock
);
725 DBG("Relay control connection accepted, socket %d",
729 PERROR("accepting sock");
730 connection_free(new_conn
);
734 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
737 PERROR("setsockopt inet");
738 lttcomm_destroy_sock(newsock
);
739 connection_free(new_conn
);
742 new_conn
->sock
= newsock
;
744 /* Enqueue request for the dispatcher thread. */
745 cds_wfq_enqueue(&relay_conn_queue
.queue
, &new_conn
->qnode
);
748 * Wake the dispatch queue futex. Implicit memory barrier with
749 * the exchange in cds_wfq_enqueue.
751 futex_nto1_wake(&relay_conn_queue
.futex
);
760 lttng_poll_clean(&events
);
762 if (data_sock
->fd
>= 0) {
763 ret
= data_sock
->ops
->close(data_sock
);
768 lttcomm_destroy_sock(data_sock
);
770 if (control_sock
->fd
>= 0) {
771 ret
= control_sock
->ops
->close(control_sock
);
776 lttcomm_destroy_sock(control_sock
);
780 ERR("Health error occurred in %s", __func__
);
782 health_unregister(health_relayd
);
783 DBG("Relay listener thread cleanup complete");
789 * This thread manages the dispatching of the requests to worker threads
792 void *relay_thread_dispatcher(void *data
)
796 struct cds_wfq_node
*node
;
797 struct relay_connection
*new_conn
= NULL
;
799 DBG("[thread] Relay dispatcher started");
801 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
803 if (testpoint(relayd_thread_dispatcher
)) {
804 goto error_testpoint
;
807 health_code_update();
809 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
810 health_code_update();
812 /* Atomically prepare the queue futex */
813 futex_nto1_prepare(&relay_conn_queue
.futex
);
816 health_code_update();
818 /* Dequeue commands */
819 node
= cds_wfq_dequeue_blocking(&relay_conn_queue
.queue
);
821 DBG("Woken up but nothing in the relay command queue");
822 /* Continue thread execution */
825 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
827 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
830 * Inform worker thread of the new request. This call is blocking
831 * so we can be assured that the data will be read at some point in
832 * time or wait to the end of the world :)
834 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
836 PERROR("write connection pipe");
837 connection_destroy(new_conn
);
840 } while (node
!= NULL
);
842 /* Futex wait on queue. Blocking call on futex() */
844 futex_nto1_wait(&relay_conn_queue
.futex
);
848 /* Normal exit, no error */
855 ERR("Health error occurred in %s", __func__
);
857 health_unregister(health_relayd
);
858 DBG("Dispatch thread dying");
863 static void try_close_streams(struct relay_session
*session
)
865 struct ctf_trace
*ctf_trace
;
866 struct lttng_ht_iter iter
;
870 pthread_mutex_lock(&session
->viewer_ready_lock
);
872 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
874 struct relay_stream
*stream
;
877 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
878 stream_close(session
, stream
);
881 ctf_trace
->invalid_flag
= 1;
882 ctf_trace_try_destroy(session
, ctf_trace
);
885 pthread_mutex_unlock(&session
->viewer_ready_lock
);
889 * Try to destroy a session within a connection.
891 static void destroy_session(struct relay_session
*session
,
892 struct lttng_ht
*sessions_ht
)
897 /* Indicate that this session can be destroyed from now on. */
898 session
->close_flag
= 1;
900 try_close_streams(session
);
903 * This will try to delete and destroy the session if no viewer is attached
904 * to it meaning the refcount is down to zero.
906 session_try_destroy(sessions_ht
, session
);
910 * Copy index data from the control port to a given index object.
912 static void copy_index_control_data(struct relay_index
*index
,
913 struct lttcomm_relayd_index
*data
)
919 * The index on disk is encoded in big endian, so we don't need to convert
920 * the data received on the network. The data_offset value is NEVER
921 * modified here and is updated by the data thread.
923 index
->index_data
.packet_size
= data
->packet_size
;
924 index
->index_data
.content_size
= data
->content_size
;
925 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
926 index
->index_data
.timestamp_end
= data
->timestamp_end
;
927 index
->index_data
.events_discarded
= data
->events_discarded
;
928 index
->index_data
.stream_id
= data
->stream_id
;
932 * Handle the RELAYD_CREATE_SESSION command.
934 * On success, send back the session id or else return a negative value.
937 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
938 struct relay_connection
*conn
)
940 int ret
= 0, send_ret
;
941 struct relay_session
*session
;
942 struct lttcomm_relayd_status_session reply
;
947 memset(&reply
, 0, sizeof(reply
));
949 session
= session_create();
954 session
->minor
= conn
->minor
;
955 session
->major
= conn
->major
;
956 conn
->session_id
= session
->id
;
957 conn
->session
= session
;
959 reply
.session_id
= htobe64(session
->id
);
961 switch (conn
->minor
) {
966 case 4: /* LTTng sessiond 2.4 */
968 ret
= cmd_create_session_2_4(conn
, session
);
971 lttng_ht_add_unique_u64(conn
->sessions_ht
, &session
->session_n
);
972 DBG("Created session %" PRIu64
, session
->id
);
976 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
978 reply
.ret_code
= htobe32(LTTNG_OK
);
981 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
983 ERR("Relayd sending session id");
991 * When we have received all the streams and the metadata for a channel,
992 * we make them visible to the viewer threads.
995 void set_viewer_ready_flag(struct relay_connection
*conn
)
997 struct relay_stream
*stream
, *tmp_stream
;
999 pthread_mutex_lock(&conn
->session
->viewer_ready_lock
);
1000 cds_list_for_each_entry_safe(stream
, tmp_stream
, &conn
->recv_head
,
1002 stream
->viewer_ready
= 1;
1003 cds_list_del(&stream
->recv_list
);
1005 pthread_mutex_unlock(&conn
->session
->viewer_ready_lock
);
1010 * Add a recv handle node to the connection recv list with the given stream
1011 * handle. A new node is allocated thus must be freed when the node is deleted
1014 static void queue_stream(struct relay_stream
*stream
,
1015 struct relay_connection
*conn
)
1020 cds_list_add(&stream
->recv_list
, &conn
->recv_head
);
1024 * relay_add_stream: allocate a new stream for a session
1027 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1028 struct relay_connection
*conn
)
1031 struct relay_session
*session
= conn
->session
;
1032 struct relay_stream
*stream
= NULL
;
1033 struct lttcomm_relayd_status_stream reply
;
1034 struct ctf_trace
*trace
;
1036 if (!session
|| conn
->version_check_done
== 0) {
1037 ERR("Trying to add a stream before version check");
1039 goto end_no_session
;
1042 stream
= zmalloc(sizeof(struct relay_stream
));
1043 if (stream
== NULL
) {
1044 PERROR("relay stream zmalloc");
1046 goto end_no_session
;
1049 switch (conn
->minor
) {
1050 case 1: /* LTTng sessiond 2.1 */
1051 ret
= cmd_recv_stream_2_1(conn
, stream
);
1053 case 2: /* LTTng sessiond 2.2 */
1055 ret
= cmd_recv_stream_2_2(conn
, stream
);
1059 goto err_free_stream
;
1063 stream
->stream_handle
= ++last_relay_stream_id
;
1064 stream
->prev_seq
= -1ULL;
1065 stream
->session_id
= session
->id
;
1066 stream
->index_fd
= -1;
1067 stream
->read_index_fd
= -1;
1068 stream
->ctf_stream_id
= -1ULL;
1069 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
1070 pthread_mutex_init(&stream
->lock
, NULL
);
1072 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1074 ERR("relay creating output directory");
1079 * No need to use run_as API here because whatever we receives, the relayd
1080 * uses its own credentials for the stream files.
1082 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1083 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1085 ERR("Create output file");
1089 if (stream
->tracefile_size
) {
1090 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1092 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1095 trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, stream
->path_name
);
1097 trace
= ctf_trace_create(stream
->path_name
);
1102 ctf_trace_add(session
->ctf_traces_ht
, trace
);
1104 ctf_trace_get_ref(trace
);
1106 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1107 stream
->metadata_flag
= 1;
1108 /* Assign quick reference to the metadata stream in the trace. */
1109 trace
->metadata_stream
= stream
;
1113 * Add the stream in the recv list of the connection. Once the end stream
1114 * message is received, this list is emptied and streams are set with the
1115 * viewer ready flag.
1117 queue_stream(stream
, conn
);
1120 * Both in the ctf_trace object and the global stream ht since the data
1121 * side of the relayd does not have the concept of session.
1123 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
1124 cds_list_add_tail(&stream
->trace_list
, &trace
->stream_list
);
1126 session
->stream_count
++;
1128 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1129 stream
->stream_handle
);
1132 memset(&reply
, 0, sizeof(reply
));
1133 reply
.handle
= htobe64(stream
->stream_handle
);
1134 /* send the session id to the client or a negative return code on error */
1136 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1137 /* stream was not properly added to the ht, so free it */
1140 reply
.ret_code
= htobe32(LTTNG_OK
);
1143 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1144 sizeof(struct lttcomm_relayd_status_stream
), 0);
1146 ERR("Relay sending stream id");
1155 free(stream
->path_name
);
1156 free(stream
->channel_name
);
1162 * relay_close_stream: close a specific stream
1165 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1166 struct relay_connection
*conn
)
1169 struct relay_session
*session
= conn
->session
;
1170 struct lttcomm_relayd_close_stream stream_info
;
1171 struct lttcomm_relayd_generic_reply reply
;
1172 struct relay_stream
*stream
;
1174 DBG("Close stream received");
1176 if (!session
|| conn
->version_check_done
== 0) {
1177 ERR("Trying to close a stream before version check");
1179 goto end_no_session
;
1182 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1183 sizeof(struct lttcomm_relayd_close_stream
), 0);
1184 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1186 /* Orderly shutdown. Not necessary to print an error. */
1187 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1189 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1192 goto end_no_session
;
1196 stream
= stream_find_by_id(relay_streams_ht
,
1197 be64toh(stream_info
.stream_id
));
1203 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1204 stream
->close_flag
= 1;
1205 session
->stream_count
--;
1206 assert(session
->stream_count
>= 0);
1208 /* Check if we can close it or else the data will do it. */
1209 try_close_stream(session
, stream
);
1214 memset(&reply
, 0, sizeof(reply
));
1216 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1218 reply
.ret_code
= htobe32(LTTNG_OK
);
1220 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1221 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1223 ERR("Relay sending stream id");
1232 * relay_unknown_command: send -1 if received unknown command
1235 void relay_unknown_command(struct relay_connection
*conn
)
1237 struct lttcomm_relayd_generic_reply reply
;
1240 memset(&reply
, 0, sizeof(reply
));
1241 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1242 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1243 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1245 ERR("Relay sending unknown command");
1250 * relay_start: send an acknowledgment to the client to tell if we are
1251 * ready to receive data. We are ready if a session is established.
1254 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1255 struct relay_connection
*conn
)
1257 int ret
= htobe32(LTTNG_OK
);
1258 struct lttcomm_relayd_generic_reply reply
;
1259 struct relay_session
*session
= conn
->session
;
1262 DBG("Trying to start the streaming without a session established");
1263 ret
= htobe32(LTTNG_ERR_UNK
);
1266 memset(&reply
, 0, sizeof(reply
));
1267 reply
.ret_code
= ret
;
1268 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1269 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1271 ERR("Relay sending start ack");
1278 * Append padding to the file pointed by the file descriptor fd.
1280 static int write_padding_to_file(int fd
, uint32_t size
)
1289 zeros
= zmalloc(size
);
1290 if (zeros
== NULL
) {
1291 PERROR("zmalloc zeros for padding");
1296 ret
= lttng_write(fd
, zeros
, size
);
1298 PERROR("write padding to file");
1308 * relay_recv_metadata: receive the metada for the session.
1311 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1312 struct relay_connection
*conn
)
1314 int ret
= htobe32(LTTNG_OK
);
1316 struct relay_session
*session
= conn
->session
;
1317 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1318 struct relay_stream
*metadata_stream
;
1319 uint64_t data_size
, payload_size
;
1320 struct ctf_trace
*ctf_trace
;
1323 ERR("Metadata sent before version check");
1328 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1329 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1330 ERR("Incorrect data size");
1334 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1336 if (data_buffer_size
< data_size
) {
1337 /* In case the realloc fails, we can free the memory */
1340 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1341 if (!tmp_data_ptr
) {
1342 ERR("Allocating data buffer");
1347 data_buffer
= tmp_data_ptr
;
1348 data_buffer_size
= data_size
;
1350 memset(data_buffer
, 0, data_size
);
1351 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1352 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1353 if (ret
< 0 || ret
!= data_size
) {
1355 /* Orderly shutdown. Not necessary to print an error. */
1356 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1358 ERR("Relay didn't receive the whole metadata");
1363 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1366 metadata_stream
= stream_find_by_id(relay_streams_ht
,
1367 be64toh(metadata_struct
->stream_id
));
1368 if (!metadata_stream
) {
1373 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1375 if (size_ret
< payload_size
) {
1376 ERR("Relay error writing metadata on file");
1381 ret
= write_padding_to_file(metadata_stream
->fd
,
1382 be32toh(metadata_struct
->padding_size
));
1387 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1388 metadata_stream
->path_name
);
1390 ctf_trace
->metadata_received
+=
1391 payload_size
+ be32toh(metadata_struct
->padding_size
);
1393 DBG2("Relay metadata written");
1402 * relay_send_version: send relayd version number
1405 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1406 struct relay_connection
*conn
)
1409 struct lttcomm_relayd_version reply
, msg
;
1413 conn
->version_check_done
= 1;
1415 /* Get version from the other side. */
1416 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1417 if (ret
< 0 || ret
!= sizeof(msg
)) {
1419 /* Orderly shutdown. Not necessary to print an error. */
1420 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1422 ERR("Relay failed to receive the version values.");
1428 memset(&reply
, 0, sizeof(reply
));
1429 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1430 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1432 /* Major versions must be the same */
1433 if (reply
.major
!= be32toh(msg
.major
)) {
1434 DBG("Incompatible major versions (%u vs %u), deleting session",
1435 reply
.major
, be32toh(msg
.major
));
1436 destroy_session(conn
->session
, conn
->sessions_ht
);
1441 conn
->major
= reply
.major
;
1442 /* We adapt to the lowest compatible version */
1443 if (reply
.minor
<= be32toh(msg
.minor
)) {
1444 conn
->minor
= reply
.minor
;
1446 conn
->minor
= be32toh(msg
.minor
);
1449 reply
.major
= htobe32(reply
.major
);
1450 reply
.minor
= htobe32(reply
.minor
);
1451 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1452 sizeof(struct lttcomm_relayd_version
), 0);
1454 ERR("Relay sending version");
1457 DBG("Version check done using protocol %u.%u", conn
->major
,
1465 * Check for data pending for a given stream id from the session daemon.
1468 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1469 struct relay_connection
*conn
)
1471 struct relay_session
*session
= conn
->session
;
1472 struct lttcomm_relayd_data_pending msg
;
1473 struct lttcomm_relayd_generic_reply reply
;
1474 struct relay_stream
*stream
;
1476 uint64_t last_net_seq_num
, stream_id
;
1478 DBG("Data pending command received");
1480 if (!session
|| conn
->version_check_done
== 0) {
1481 ERR("Trying to check for data before version check");
1483 goto end_no_session
;
1486 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1487 if (ret
< sizeof(msg
)) {
1489 /* Orderly shutdown. Not necessary to print an error. */
1490 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1492 ERR("Relay didn't receive valid data_pending struct size : %d",
1496 goto end_no_session
;
1499 stream_id
= be64toh(msg
.stream_id
);
1500 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1503 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
1504 if (stream
== NULL
) {
1509 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1510 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1513 /* Avoid wrapping issue */
1514 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1515 /* Data has in fact been written and is NOT pending */
1518 /* Data still being streamed thus pending */
1522 /* Pending check is now done. */
1523 stream
->data_pending_check_done
= 1;
1528 memset(&reply
, 0, sizeof(reply
));
1529 reply
.ret_code
= htobe32(ret
);
1530 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1532 ERR("Relay data pending ret code failed");
1540 * Wait for the control socket to reach a quiescent state.
1542 * Note that for now, when receiving this command from the session daemon, this
1543 * means that every subsequent commands or data received on the control socket
1544 * has been handled. So, this is why we simply return OK here.
1547 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1548 struct relay_connection
*conn
)
1552 struct relay_stream
*stream
;
1553 struct lttng_ht_iter iter
;
1554 struct lttcomm_relayd_quiescent_control msg
;
1555 struct lttcomm_relayd_generic_reply reply
;
1557 DBG("Checking quiescent state on control socket");
1559 if (!conn
->session
|| conn
->version_check_done
== 0) {
1560 ERR("Trying to check for data before version check");
1562 goto end_no_session
;
1565 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1566 if (ret
< sizeof(msg
)) {
1568 /* Orderly shutdown. Not necessary to print an error. */
1569 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1571 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1575 goto end_no_session
;
1578 stream_id
= be64toh(msg
.stream_id
);
1581 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1583 if (stream
->stream_handle
== stream_id
) {
1584 stream
->data_pending_check_done
= 1;
1585 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1592 memset(&reply
, 0, sizeof(reply
));
1593 reply
.ret_code
= htobe32(LTTNG_OK
);
1594 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1596 ERR("Relay data quiescent control ret code failed");
1604 * Initialize a data pending command. This means that a client is about to ask
1605 * for data pending for each stream he/she holds. Simply iterate over all
1606 * streams of a session and set the data_pending_check_done flag.
1608 * This command returns to the client a LTTNG_OK code.
1611 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1612 struct relay_connection
*conn
)
1615 struct lttng_ht_iter iter
;
1616 struct lttcomm_relayd_begin_data_pending msg
;
1617 struct lttcomm_relayd_generic_reply reply
;
1618 struct relay_stream
*stream
;
1619 uint64_t session_id
;
1624 DBG("Init streams for data pending");
1626 if (!conn
->session
|| conn
->version_check_done
== 0) {
1627 ERR("Trying to check for data before version check");
1629 goto end_no_session
;
1632 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1633 if (ret
< sizeof(msg
)) {
1635 /* Orderly shutdown. Not necessary to print an error. */
1636 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1638 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1642 goto end_no_session
;
1645 session_id
= be64toh(msg
.session_id
);
1648 * Iterate over all streams to set the begin data pending flag. For now, the
1649 * streams are indexed by stream handle so we have to iterate over all
1650 * streams to find the one associated with the right session_id.
1653 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1655 if (stream
->session_id
== session_id
) {
1656 stream
->data_pending_check_done
= 0;
1657 DBG("Set begin data pending flag to stream %" PRIu64
,
1658 stream
->stream_handle
);
1663 memset(&reply
, 0, sizeof(reply
));
1664 /* All good, send back reply. */
1665 reply
.ret_code
= htobe32(LTTNG_OK
);
1667 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1669 ERR("Relay begin data pending send reply failed");
1677 * End data pending command. This will check, for a given session id, if each
1678 * stream associated with it has its data_pending_check_done flag set. If not,
1679 * this means that the client lost track of the stream but the data is still
1680 * being streamed on our side. In this case, we inform the client that data is
1683 * Return to the client if there is data in flight or not with a ret_code.
1686 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1687 struct relay_connection
*conn
)
1690 struct lttng_ht_iter iter
;
1691 struct lttcomm_relayd_end_data_pending msg
;
1692 struct lttcomm_relayd_generic_reply reply
;
1693 struct relay_stream
*stream
;
1694 uint64_t session_id
;
1695 uint32_t is_data_inflight
= 0;
1700 DBG("End data pending command");
1702 if (!conn
->session
|| conn
->version_check_done
== 0) {
1703 ERR("Trying to check for data before version check");
1705 goto end_no_session
;
1708 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1709 if (ret
< sizeof(msg
)) {
1711 /* Orderly shutdown. Not necessary to print an error. */
1712 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1714 ERR("Relay didn't receive valid end data_pending struct size: %d",
1718 goto end_no_session
;
1721 session_id
= be64toh(msg
.session_id
);
1723 /* Iterate over all streams to see if the begin data pending flag is set. */
1725 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1727 if (stream
->session_id
== session_id
&&
1728 !stream
->data_pending_check_done
&& !stream
->terminated_flag
) {
1729 is_data_inflight
= 1;
1730 DBG("Data is still in flight for stream %" PRIu64
,
1731 stream
->stream_handle
);
1737 memset(&reply
, 0, sizeof(reply
));
1738 /* All good, send back reply. */
1739 reply
.ret_code
= htobe32(is_data_inflight
);
1741 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1743 ERR("Relay end data pending send reply failed");
1751 * Receive an index for a specific stream.
1753 * Return 0 on success else a negative value.
1756 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1757 struct relay_connection
*conn
)
1759 int ret
, send_ret
, index_created
= 0;
1760 struct relay_session
*session
= conn
->session
;
1761 struct lttcomm_relayd_index index_info
;
1762 struct relay_index
*index
, *wr_index
= NULL
;
1763 struct lttcomm_relayd_generic_reply reply
;
1764 struct relay_stream
*stream
;
1765 uint64_t net_seq_num
;
1769 DBG("Relay receiving index");
1771 if (!session
|| conn
->version_check_done
== 0) {
1772 ERR("Trying to close a stream before version check");
1774 goto end_no_session
;
1777 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
1778 sizeof(index_info
), 0);
1779 if (ret
< sizeof(index_info
)) {
1781 /* Orderly shutdown. Not necessary to print an error. */
1782 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1784 ERR("Relay didn't receive valid index struct size : %d", ret
);
1787 goto end_no_session
;
1790 net_seq_num
= be64toh(index_info
.net_seq_num
);
1793 stream
= stream_find_by_id(relay_streams_ht
,
1794 be64toh(index_info
.relay_stream_id
));
1797 goto end_rcu_unlock
;
1800 /* Live beacon handling */
1801 if (index_info
.packet_size
== 0) {
1802 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1805 * Only flag a stream inactive when it has already received data.
1807 if (stream
->total_index_received
> 0) {
1808 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1811 goto end_rcu_unlock
;
1813 stream
->beacon_ts_end
= -1ULL;
1816 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1818 /* A successful creation will add the object to the HT. */
1819 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1821 goto end_rcu_unlock
;
1826 copy_index_control_data(index
, &index_info
);
1827 if (stream
->ctf_stream_id
== -1ULL) {
1828 stream
->ctf_stream_id
= be64toh(index_info
.stream_id
);
1831 if (index_created
) {
1833 * Try to add the relay index object to the hash table. If an object
1834 * already exist, destroy back the index created, set the data in this
1835 * object and write it on disk.
1837 relay_index_add(index
, &wr_index
);
1839 copy_index_control_data(wr_index
, &index_info
);
1843 /* The index already exists so write it on disk. */
1847 /* Do we have a writable ready index to write on disk. */
1849 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1851 goto end_rcu_unlock
;
1853 stream
->total_index_received
++;
1859 memset(&reply
, 0, sizeof(reply
));
1861 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1863 reply
.ret_code
= htobe32(LTTNG_OK
);
1865 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1867 ERR("Relay sending close index id reply");
1876 * Receive the streams_sent message.
1878 * Return 0 on success else a negative value.
1881 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
1882 struct relay_connection
*conn
)
1885 struct lttcomm_relayd_generic_reply reply
;
1889 DBG("Relay receiving streams_sent");
1891 if (!conn
->session
|| conn
->version_check_done
== 0) {
1892 ERR("Trying to close a stream before version check");
1894 goto end_no_session
;
1898 * Flag every pending stream in the connection recv list that they are
1899 * ready to be used by the viewer.
1901 set_viewer_ready_flag(conn
);
1904 * Inform the viewer that there are new streams in the session.
1906 if (conn
->session
->viewer_refcount
) {
1907 uatomic_set(&conn
->session
->new_streams
, 1);
1910 memset(&reply
, 0, sizeof(reply
));
1911 reply
.ret_code
= htobe32(LTTNG_OK
);
1912 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1914 ERR("Relay sending sent_stream reply");
1926 * Process the commands received on the control socket
1929 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1930 struct relay_connection
*conn
)
1934 switch (be32toh(recv_hdr
->cmd
)) {
1935 case RELAYD_CREATE_SESSION
:
1936 ret
= relay_create_session(recv_hdr
, conn
);
1938 case RELAYD_ADD_STREAM
:
1939 ret
= relay_add_stream(recv_hdr
, conn
);
1941 case RELAYD_START_DATA
:
1942 ret
= relay_start(recv_hdr
, conn
);
1944 case RELAYD_SEND_METADATA
:
1945 ret
= relay_recv_metadata(recv_hdr
, conn
);
1947 case RELAYD_VERSION
:
1948 ret
= relay_send_version(recv_hdr
, conn
);
1950 case RELAYD_CLOSE_STREAM
:
1951 ret
= relay_close_stream(recv_hdr
, conn
);
1953 case RELAYD_DATA_PENDING
:
1954 ret
= relay_data_pending(recv_hdr
, conn
);
1956 case RELAYD_QUIESCENT_CONTROL
:
1957 ret
= relay_quiescent_control(recv_hdr
, conn
);
1959 case RELAYD_BEGIN_DATA_PENDING
:
1960 ret
= relay_begin_data_pending(recv_hdr
, conn
);
1962 case RELAYD_END_DATA_PENDING
:
1963 ret
= relay_end_data_pending(recv_hdr
, conn
);
1965 case RELAYD_SEND_INDEX
:
1966 ret
= relay_recv_index(recv_hdr
, conn
);
1968 case RELAYD_STREAMS_SENT
:
1969 ret
= relay_streams_sent(recv_hdr
, conn
);
1971 case RELAYD_UPDATE_SYNC_INFO
:
1973 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1974 relay_unknown_command(conn
);
1984 * Handle index for a data stream.
1986 * RCU read side lock MUST be acquired.
1988 * Return 0 on success else a negative value.
1990 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
1993 int ret
= 0, index_created
= 0;
1994 uint64_t stream_id
, data_offset
;
1995 struct relay_index
*index
, *wr_index
= NULL
;
1999 stream_id
= stream
->stream_handle
;
2000 /* Get data offset because we are about to update the index. */
2001 data_offset
= htobe64(stream
->tracefile_size_current
);
2004 * Lookup for an existing index for that stream id/sequence number. If on
2005 * exists, the control thread already received the data for it thus we need
2006 * to write it on disk.
2008 index
= relay_index_find(stream_id
, net_seq_num
);
2010 /* A successful creation will add the object to the HT. */
2011 index
= relay_index_create(stream_id
, net_seq_num
);
2019 if (rotate_index
|| stream
->index_fd
< 0) {
2020 index
->to_close_fd
= stream
->index_fd
;
2021 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2022 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2023 stream
->tracefile_count_current
);
2025 /* This will close the stream's index fd if one. */
2026 relay_index_free_safe(index
);
2029 stream
->index_fd
= ret
;
2031 index
->fd
= stream
->index_fd
;
2032 index
->index_data
.offset
= data_offset
;
2034 if (index_created
) {
2036 * Try to add the relay index object to the hash table. If an object
2037 * already exist, destroy back the index created and set the data.
2039 relay_index_add(index
, &wr_index
);
2041 /* Copy back data from the created index. */
2042 wr_index
->fd
= index
->fd
;
2043 wr_index
->to_close_fd
= index
->to_close_fd
;
2044 wr_index
->index_data
.offset
= data_offset
;
2048 /* The index already exists so write it on disk. */
2052 /* Do we have a writable ready index to write on disk. */
2054 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2058 stream
->total_index_received
++;
2066 * relay_process_data: Process the data received on the data socket
2069 int relay_process_data(struct relay_connection
*conn
)
2071 int ret
= 0, rotate_index
= 0;
2073 struct relay_stream
*stream
;
2074 struct lttcomm_relayd_data_hdr data_hdr
;
2076 uint64_t net_seq_num
;
2078 struct relay_session
*session
;
2082 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2083 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2086 /* Orderly shutdown. Not necessary to print an error. */
2087 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2089 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2095 stream_id
= be64toh(data_hdr
.stream_id
);
2098 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
2101 goto end_rcu_unlock
;
2104 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
2107 data_size
= be32toh(data_hdr
.data_size
);
2108 if (data_buffer_size
< data_size
) {
2111 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2112 if (!tmp_data_ptr
) {
2113 ERR("Allocating data buffer");
2116 goto end_rcu_unlock
;
2118 data_buffer
= tmp_data_ptr
;
2119 data_buffer_size
= data_size
;
2121 memset(data_buffer
, 0, data_size
);
2123 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2125 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2126 data_size
, stream_id
, net_seq_num
);
2127 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
2130 /* Orderly shutdown. Not necessary to print an error. */
2131 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2134 goto end_rcu_unlock
;
2137 /* Check if a rotation is needed. */
2138 if (stream
->tracefile_size
> 0 &&
2139 (stream
->tracefile_size_current
+ data_size
) >
2140 stream
->tracefile_size
) {
2141 struct relay_viewer_stream
*vstream
;
2144 new_id
= (stream
->tracefile_count_current
+ 1) %
2145 stream
->tracefile_count
;
2147 * When we wrap-around back to 0, we start overwriting old
2150 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2151 stream
->tracefile_overwrite
= 1;
2153 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2154 if (stream
->tracefile_overwrite
) {
2155 stream
->oldest_tracefile_id
=
2156 (stream
->oldest_tracefile_id
+ 1) %
2157 stream
->tracefile_count
;
2159 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2162 * The viewer is reading a file about to be
2163 * overwritten. Close the FDs it is
2164 * currently using and let it handle the fault.
2166 if (vstream
->tracefile_count_current
== new_id
) {
2167 pthread_mutex_lock(&vstream
->overwrite_lock
);
2168 vstream
->abort_flag
= 1;
2169 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2170 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2171 stream
->channel_name
, new_id
);
2172 } else if (vstream
->tracefile_count_current
==
2173 stream
->tracefile_count_current
) {
2175 * The reader and writer were in the
2176 * same trace file, inform the viewer
2177 * that no new index will ever be added
2180 vstream
->close_write_flag
= 1;
2183 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2184 stream
->tracefile_size
, stream
->tracefile_count
,
2185 relayd_uid
, relayd_gid
, stream
->fd
,
2186 &(stream
->tracefile_count_current
), &stream
->fd
);
2187 stream
->total_index_received
= 0;
2188 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2190 ERR("Rotating stream output file");
2191 goto end_rcu_unlock
;
2193 /* Reset current size because we just perform a stream rotation. */
2194 stream
->tracefile_size_current
= 0;
2199 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2200 * index are NOT supported.
2202 if (session
->minor
>= 4 && !session
->snapshot
) {
2203 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2205 goto end_rcu_unlock
;
2209 /* Write data to stream output fd. */
2210 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2211 if (size_ret
< data_size
) {
2212 ERR("Relay error writing data to file");
2214 goto end_rcu_unlock
;
2217 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2218 ret
, stream
->stream_handle
);
2220 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2222 goto end_rcu_unlock
;
2224 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2226 stream
->prev_seq
= net_seq_num
;
2228 try_close_stream(session
, stream
);
2237 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2243 (void) lttng_poll_del(events
, pollfd
);
2245 ret
= close(pollfd
);
2247 ERR("Closing pollfd %d", pollfd
);
2251 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
2252 struct relay_connection
*conn
)
2254 assert(relay_connections_ht
);
2257 connection_delete(relay_connections_ht
, conn
);
2259 /* For the control socket, we try to destroy the session. */
2260 if (conn
->type
== RELAY_CONTROL
&& conn
->session
) {
2261 destroy_session(conn
->session
, conn
->sessions_ht
);
2264 connection_destroy(conn
);
2268 * This thread does the actual work
2271 void *relay_thread_worker(void *data
)
2273 int ret
, err
= -1, last_seen_data_fd
= -1;
2275 struct relay_connection
*conn
;
2276 struct lttng_poll_event events
;
2277 struct lttng_ht
*relay_connections_ht
;
2278 struct lttng_ht_iter iter
;
2279 struct lttcomm_relayd_hdr recv_hdr
;
2280 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2281 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2283 DBG("[thread] Relay worker started");
2285 rcu_register_thread();
2287 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2289 if (testpoint(relayd_thread_worker
)) {
2290 goto error_testpoint
;
2293 health_code_update();
2295 /* table of connections indexed on socket */
2296 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2297 if (!relay_connections_ht
) {
2298 goto relay_connections_ht_error
;
2301 /* Tables of received indexes indexed by index handle and net_seq_num. */
2302 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2304 goto indexes_ht_error
;
2307 ret
= create_thread_poll_set(&events
, 2);
2309 goto error_poll_create
;
2312 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2319 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2321 health_code_update();
2323 /* Infinite blocking call, waiting for transmission */
2324 DBG3("Relayd worker thread polling...");
2325 health_poll_entry();
2326 ret
= lttng_poll_wait(&events
, -1);
2330 * Restart interrupted system call.
2332 if (errno
== EINTR
) {
2341 * Process control. The control connection is prioritised so we don't
2342 * starve it with high throughout put tracing data on the data
2345 for (i
= 0; i
< nb_fd
; i
++) {
2346 /* Fetch once the poll data */
2347 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2348 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2350 health_code_update();
2352 /* Thread quit pipe has been closed. Killing thread. */
2353 ret
= check_thread_quit_pipe(pollfd
, revents
);
2359 /* Inspect the relay conn pipe for new connection */
2360 if (pollfd
== relay_conn_pipe
[0]) {
2361 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2362 ERR("Relay connection pipe error");
2364 } else if (revents
& LPOLLIN
) {
2365 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2369 conn
->sessions_ht
= sessions_ht
;
2370 connection_init(conn
);
2371 lttng_poll_add(&events
, conn
->sock
->fd
,
2372 LPOLLIN
| LPOLLRDHUP
);
2374 lttng_ht_add_unique_ulong(relay_connections_ht
,
2377 DBG("Connection socket %d added", conn
->sock
->fd
);
2381 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2382 /* If not found, there is a synchronization issue. */
2385 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2386 cleanup_connection_pollfd(&events
, pollfd
);
2387 destroy_connection(relay_connections_ht
, conn
);
2388 if (last_seen_data_fd
== pollfd
) {
2389 last_seen_data_fd
= last_notdel_data_fd
;
2391 } else if (revents
& LPOLLIN
) {
2392 if (conn
->type
== RELAY_CONTROL
) {
2393 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2394 sizeof(recv_hdr
), 0);
2396 /* Connection closed */
2397 cleanup_connection_pollfd(&events
, pollfd
);
2398 destroy_connection(relay_connections_ht
, conn
);
2399 DBG("Control connection closed with %d", pollfd
);
2401 ret
= relay_process_control(&recv_hdr
, conn
);
2403 /* Clear the session on error. */
2404 cleanup_connection_pollfd(&events
, pollfd
);
2405 destroy_connection(relay_connections_ht
, conn
);
2406 DBG("Connection closed with %d", pollfd
);
2412 * Flag the last seen data fd not deleted. It will be
2413 * used as the last seen fd if any fd gets deleted in
2416 last_notdel_data_fd
= pollfd
;
2419 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
2426 * The last loop handled a control request, go back to poll to make
2427 * sure we prioritise the control socket.
2433 if (last_seen_data_fd
>= 0) {
2434 for (i
= 0; i
< nb_fd
; i
++) {
2435 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2437 health_code_update();
2439 if (last_seen_data_fd
== pollfd
) {
2446 /* Process data connection. */
2447 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2448 /* Fetch the poll data. */
2449 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2450 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2452 health_code_update();
2454 /* Skip the command pipe. It's handled in the first loop. */
2455 if (pollfd
== relay_conn_pipe
[0]) {
2461 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2463 /* Skip it. Might be removed before. */
2468 if (revents
& LPOLLIN
) {
2469 if (conn
->type
!= RELAY_DATA
) {
2473 ret
= relay_process_data(conn
);
2474 /* Connection closed */
2476 cleanup_connection_pollfd(&events
, pollfd
);
2477 destroy_connection(relay_connections_ht
, conn
);
2478 DBG("Data connection closed with %d", pollfd
);
2480 * Every goto restart call sets the last seen fd where
2481 * here we don't really care since we gracefully
2482 * continue the loop after the connection is deleted.
2485 /* Keep last seen port. */
2486 last_seen_data_fd
= pollfd
;
2494 last_seen_data_fd
= -1;
2497 /* Normal exit, no error */
2502 lttng_poll_clean(&events
);
2504 /* Cleanup reamaining connection object. */
2506 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2508 health_code_update();
2509 destroy_connection(relay_connections_ht
, conn
);
2513 lttng_ht_destroy(indexes_ht
);
2515 lttng_ht_destroy(relay_connections_ht
);
2516 relay_connections_ht_error
:
2517 /* Close relay conn pipes */
2518 utils_close_pipe(relay_conn_pipe
);
2520 DBG("Thread exited with error");
2522 DBG("Worker thread cleanup complete");
2527 ERR("Health error occurred in %s", __func__
);
2529 health_unregister(health_relayd
);
2530 rcu_unregister_thread();
2536 * Create the relay command pipe to wake thread_manage_apps.
2537 * Closed in cleanup().
2539 static int create_relay_conn_pipe(void)
2543 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2551 int main(int argc
, char **argv
)
2555 struct relay_local_data
*relay_ctx
;
2557 /* Parse arguments */
2559 if ((ret
= parse_args(argc
, argv
)) < 0) {
2563 if ((ret
= set_signal_handler()) < 0) {
2567 /* Try to create directory if -o, --output is specified. */
2568 if (opt_output_path
) {
2569 if (*opt_output_path
!= '/') {
2570 ERR("Please specify an absolute path for -o, --output PATH");
2574 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2576 ERR("Unable to create %s", opt_output_path
);
2582 if (opt_daemon
|| opt_background
) {
2585 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2592 * We are in the child. Make sure all other file
2593 * descriptors are closed, in case we are called with
2594 * more opened file descriptors than the standard ones.
2596 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2601 /* Create thread quit pipe */
2602 if ((ret
= init_thread_quit_pipe()) < 0) {
2606 /* We need those values for the file/dir creation. */
2607 relayd_uid
= getuid();
2608 relayd_gid
= getgid();
2610 /* Check if daemon is UID = 0 */
2611 if (relayd_uid
== 0) {
2612 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2613 live_uri
->port
< 1024) {
2614 ERR("Need to be root to use ports < 1024");
2620 /* Setup the thread apps communication pipe. */
2621 if ((ret
= create_relay_conn_pipe()) < 0) {
2625 /* Init relay command queue. */
2626 cds_wfq_init(&relay_conn_queue
.queue
);
2628 /* Set up max poll set size */
2629 lttng_poll_set_max_size();
2631 /* Initialize communication library */
2633 lttcomm_inet_init();
2635 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2637 PERROR("relay_ctx");
2641 /* tables of sessions indexed by session ID */
2642 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2643 if (!relay_ctx
->sessions_ht
) {
2644 goto exit_relay_ctx_sessions
;
2647 /* tables of streams indexed by stream ID */
2648 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2649 if (!relay_streams_ht
) {
2650 goto exit_relay_ctx_streams
;
2653 /* tables of streams indexed by stream ID */
2654 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2655 if (!viewer_streams_ht
) {
2656 goto exit_relay_ctx_viewer_streams
;
2659 /* Initialize thread health monitoring */
2660 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2661 if (!health_relayd
) {
2662 PERROR("health_app_create error");
2663 goto exit_health_app_create
;
2666 ret
= utils_create_pipe(health_quit_pipe
);
2668 goto error_health_pipe
;
2671 /* Create thread to manage the client socket */
2672 ret
= pthread_create(&health_thread
, NULL
,
2673 thread_manage_health
, (void *) NULL
);
2675 PERROR("pthread_create health");
2679 /* Setup the dispatcher thread */
2680 ret
= pthread_create(&dispatcher_thread
, NULL
,
2681 relay_thread_dispatcher
, (void *) NULL
);
2683 PERROR("pthread_create dispatcher");
2684 goto exit_dispatcher
;
2687 /* Setup the worker thread */
2688 ret
= pthread_create(&worker_thread
, NULL
,
2689 relay_thread_worker
, (void *) relay_ctx
);
2691 PERROR("pthread_create worker");
2695 /* Setup the listener thread */
2696 ret
= pthread_create(&listener_thread
, NULL
,
2697 relay_thread_listener
, (void *) NULL
);
2699 PERROR("pthread_create listener");
2703 ret
= live_start_threads(live_uri
, relay_ctx
);
2705 ERR("Starting live viewer threads");
2710 ret
= pthread_join(listener_thread
, &status
);
2712 PERROR("pthread_join");
2713 goto error
; /* join error, exit without cleanup */
2717 ret
= pthread_join(worker_thread
, &status
);
2719 PERROR("pthread_join");
2720 goto error
; /* join error, exit without cleanup */
2724 ret
= pthread_join(dispatcher_thread
, &status
);
2726 PERROR("pthread_join");
2727 goto error
; /* join error, exit without cleanup */
2731 ret
= pthread_join(health_thread
, &status
);
2733 PERROR("pthread_join health thread");
2734 goto error
; /* join error, exit without cleanup */
2738 * Stop live threads only after joining other threads.
2740 live_stop_threads();
2743 utils_close_pipe(health_quit_pipe
);
2746 health_app_destroy(health_relayd
);
2748 exit_health_app_create
:
2749 lttng_ht_destroy(viewer_streams_ht
);
2751 exit_relay_ctx_viewer_streams
:
2752 lttng_ht_destroy(relay_streams_ht
);
2754 exit_relay_ctx_streams
:
2755 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2757 exit_relay_ctx_sessions
: