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
, "tcp://0.0.0.0:%d",
255 DEFAULT_NETWORK_CONTROL_PORT
);
257 PERROR("asprintf default data address");
261 ret
= uri_parse(default_address
, &control_uri
);
262 free(default_address
);
264 ERR("Invalid control URI specified");
268 if (data_uri
== NULL
) {
269 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
270 DEFAULT_NETWORK_DATA_PORT
);
272 PERROR("asprintf default data address");
276 ret
= uri_parse(default_address
, &data_uri
);
277 free(default_address
);
279 ERR("Invalid data URI specified");
283 if (live_uri
== NULL
) {
284 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
285 DEFAULT_NETWORK_VIEWER_PORT
);
287 PERROR("asprintf default viewer control address");
291 ret
= uri_parse(default_address
, &live_uri
);
292 free(default_address
);
294 ERR("Invalid viewer control URI specified");
311 /* free the dynamically allocated opt_output_path */
312 free(opt_output_path
);
314 /* Close thread quit pipes */
315 utils_close_pipe(thread_quit_pipe
);
317 uri_free(control_uri
);
319 /* Live URI is freed in the live thread. */
323 * Write to writable pipe used to notify a thread.
326 int notify_thread_pipe(int wpipe
)
330 ret
= lttng_write(wpipe
, "!", 1);
332 PERROR("write poll pipe");
338 static void notify_health_quit_pipe(int *pipe
)
342 ret
= lttng_write(pipe
[1], "4", 1);
344 PERROR("write relay health quit");
349 * Stop all threads by closing the thread quit pipe.
352 void stop_threads(void)
356 /* Stopping all threads */
357 DBG("Terminating all threads");
358 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
360 ERR("write error on thread quit pipe");
363 notify_health_quit_pipe(health_quit_pipe
);
365 /* Dispatch thread */
366 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
367 futex_nto1_wake(&relay_conn_queue
.futex
);
371 * Signal handler for the daemon
373 * Simply stop all worker threads, leaving main() return gracefully after
374 * joining all threads and calling cleanup().
377 void sighandler(int sig
)
381 DBG("SIGPIPE caught");
384 DBG("SIGINT caught");
388 DBG("SIGTERM caught");
392 CMM_STORE_SHARED(recv_child_signal
, 1);
400 * Setup signal handler for :
401 * SIGINT, SIGTERM, SIGPIPE
404 int set_signal_handler(void)
410 if ((ret
= sigemptyset(&sigset
)) < 0) {
411 PERROR("sigemptyset");
415 sa
.sa_handler
= sighandler
;
418 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
423 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
428 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
433 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
438 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
443 void lttng_relay_notify_ready(void)
445 /* Notify the parent of the fork() process that we are ready. */
446 if (opt_daemon
|| opt_background
) {
447 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
448 kill(child_ppid
, SIGUSR1
);
454 * Init thread quit pipe.
456 * Return -1 on error or 0 if all pipes are created.
459 int init_thread_quit_pipe(void)
463 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
469 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
472 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
476 if (events
== NULL
|| size
== 0) {
481 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
487 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
499 * Check if the thread quit pipe was triggered.
501 * Return 1 if it was triggered else 0;
504 int check_thread_quit_pipe(int fd
, uint32_t events
)
506 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
514 * Create and init socket from uri.
517 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
520 struct lttcomm_sock
*sock
= NULL
;
522 sock
= lttcomm_alloc_sock_from_uri(uri
);
524 ERR("Allocating socket");
528 ret
= lttcomm_create_sock(sock
);
532 DBG("Listening on sock %d", sock
->fd
);
534 ret
= sock
->ops
->bind(sock
);
539 ret
= sock
->ops
->listen(sock
, -1);
549 lttcomm_destroy_sock(sock
);
555 * Return nonzero if stream needs to be closed.
558 int close_stream_check(struct relay_stream
*stream
)
560 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
562 * We are about to close the stream so set the data pending flag to 1
563 * which will make the end data pending command skip the stream which
564 * is now closed and ready. Note that after proceeding to a file close,
565 * the written file is ready for reading.
567 stream
->data_pending_check_done
= 1;
573 static void try_close_stream(struct relay_session
*session
,
574 struct relay_stream
*stream
)
577 struct ctf_trace
*ctf_trace
;
582 if (!close_stream_check(stream
)) {
583 /* Can't close it, not ready for that. */
587 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
591 pthread_mutex_lock(&session
->viewer_ready_lock
);
592 ctf_trace
->invalid_flag
= 1;
593 pthread_mutex_unlock(&session
->viewer_ready_lock
);
595 ret
= stream_close(session
, stream
);
596 if (ret
|| session
->snapshot
) {
597 /* Already close thus the ctf trace is being or has been destroyed. */
601 ctf_trace_try_destroy(session
, ctf_trace
);
608 * This thread manages the listening for new connections on the network
611 void *relay_thread_listener(void *data
)
613 int i
, ret
, pollfd
, err
= -1;
614 uint32_t revents
, nb_fd
;
615 struct lttng_poll_event events
;
616 struct lttcomm_sock
*control_sock
, *data_sock
;
618 DBG("[thread] Relay listener started");
620 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
622 health_code_update();
624 control_sock
= relay_init_sock(control_uri
);
626 goto error_sock_control
;
629 data_sock
= relay_init_sock(data_uri
);
631 goto error_sock_relay
;
635 * Pass 3 as size here for the thread quit pipe, control and data socket.
637 ret
= create_thread_poll_set(&events
, 3);
639 goto error_create_poll
;
642 /* Add the control socket */
643 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
648 /* Add the data socket */
649 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
654 lttng_relay_notify_ready();
656 if (testpoint(relayd_thread_listener
)) {
657 goto error_testpoint
;
661 health_code_update();
663 DBG("Listener accepting connections");
667 ret
= lttng_poll_wait(&events
, -1);
671 * Restart interrupted system call.
673 if (errno
== EINTR
) {
681 DBG("Relay new connection received");
682 for (i
= 0; i
< nb_fd
; i
++) {
683 health_code_update();
685 /* Fetch once the poll data */
686 revents
= LTTNG_POLL_GETEV(&events
, i
);
687 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
689 /* Thread quit pipe has been closed. Killing thread. */
690 ret
= check_thread_quit_pipe(pollfd
, revents
);
696 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
697 ERR("socket poll error");
699 } else if (revents
& LPOLLIN
) {
701 * Get allocated in this thread, enqueued to a global queue,
702 * dequeued and freed in the worker thread.
705 struct relay_connection
*new_conn
;
706 struct lttcomm_sock
*newsock
;
708 new_conn
= connection_create();
713 if (pollfd
== data_sock
->fd
) {
714 new_conn
->type
= RELAY_DATA
;
715 newsock
= data_sock
->ops
->accept(data_sock
);
716 DBG("Relay data connection accepted, socket %d",
719 assert(pollfd
== control_sock
->fd
);
720 new_conn
->type
= RELAY_CONTROL
;
721 newsock
= control_sock
->ops
->accept(control_sock
);
722 DBG("Relay control connection accepted, socket %d",
726 PERROR("accepting sock");
727 connection_free(new_conn
);
731 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
734 PERROR("setsockopt inet");
735 lttcomm_destroy_sock(newsock
);
736 connection_free(new_conn
);
739 new_conn
->sock
= newsock
;
741 /* Enqueue request for the dispatcher thread. */
742 cds_wfq_enqueue(&relay_conn_queue
.queue
, &new_conn
->qnode
);
745 * Wake the dispatch queue futex. Implicit memory barrier with
746 * the exchange in cds_wfq_enqueue.
748 futex_nto1_wake(&relay_conn_queue
.futex
);
757 lttng_poll_clean(&events
);
759 if (data_sock
->fd
>= 0) {
760 ret
= data_sock
->ops
->close(data_sock
);
765 lttcomm_destroy_sock(data_sock
);
767 if (control_sock
->fd
>= 0) {
768 ret
= control_sock
->ops
->close(control_sock
);
773 lttcomm_destroy_sock(control_sock
);
777 ERR("Health error occurred in %s", __func__
);
779 health_unregister(health_relayd
);
780 DBG("Relay listener thread cleanup complete");
786 * This thread manages the dispatching of the requests to worker threads
789 void *relay_thread_dispatcher(void *data
)
793 struct cds_wfq_node
*node
;
794 struct relay_connection
*new_conn
= NULL
;
796 DBG("[thread] Relay dispatcher started");
798 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
800 if (testpoint(relayd_thread_dispatcher
)) {
801 goto error_testpoint
;
804 health_code_update();
806 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
807 health_code_update();
809 /* Atomically prepare the queue futex */
810 futex_nto1_prepare(&relay_conn_queue
.futex
);
813 health_code_update();
815 /* Dequeue commands */
816 node
= cds_wfq_dequeue_blocking(&relay_conn_queue
.queue
);
818 DBG("Woken up but nothing in the relay command queue");
819 /* Continue thread execution */
822 new_conn
= caa_container_of(node
, struct relay_connection
, qnode
);
824 DBG("Dispatching request waiting on sock %d", new_conn
->sock
->fd
);
827 * Inform worker thread of the new request. This call is blocking
828 * so we can be assured that the data will be read at some point in
829 * time or wait to the end of the world :)
831 ret
= lttng_write(relay_conn_pipe
[1], &new_conn
, sizeof(new_conn
));
833 PERROR("write connection pipe");
834 connection_destroy(new_conn
);
837 } while (node
!= NULL
);
839 /* Futex wait on queue. Blocking call on futex() */
841 futex_nto1_wait(&relay_conn_queue
.futex
);
845 /* Normal exit, no error */
852 ERR("Health error occurred in %s", __func__
);
854 health_unregister(health_relayd
);
855 DBG("Dispatch thread dying");
860 static void try_close_streams(struct relay_session
*session
)
862 struct ctf_trace
*ctf_trace
;
863 struct lttng_ht_iter iter
;
867 pthread_mutex_lock(&session
->viewer_ready_lock
);
869 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
871 struct relay_stream
*stream
;
874 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
875 stream_close(session
, stream
);
878 ctf_trace
->invalid_flag
= 1;
879 ctf_trace_try_destroy(session
, ctf_trace
);
882 pthread_mutex_unlock(&session
->viewer_ready_lock
);
886 * Try to destroy a session within a connection.
888 static void destroy_session(struct relay_session
*session
,
889 struct lttng_ht
*sessions_ht
)
894 /* Indicate that this session can be destroyed from now on. */
895 session
->close_flag
= 1;
897 try_close_streams(session
);
900 * This will try to delete and destroy the session if no viewer is attached
901 * to it meaning the refcount is down to zero.
903 session_try_destroy(sessions_ht
, session
);
907 * Copy index data from the control port to a given index object.
909 static void copy_index_control_data(struct relay_index
*index
,
910 struct lttcomm_relayd_index
*data
)
916 * The index on disk is encoded in big endian, so we don't need to convert
917 * the data received on the network. The data_offset value is NEVER
918 * modified here and is updated by the data thread.
920 index
->index_data
.packet_size
= data
->packet_size
;
921 index
->index_data
.content_size
= data
->content_size
;
922 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
923 index
->index_data
.timestamp_end
= data
->timestamp_end
;
924 index
->index_data
.events_discarded
= data
->events_discarded
;
925 index
->index_data
.stream_id
= data
->stream_id
;
929 * Handle the RELAYD_CREATE_SESSION command.
931 * On success, send back the session id or else return a negative value.
934 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
935 struct relay_connection
*conn
)
937 int ret
= 0, send_ret
;
938 struct relay_session
*session
;
939 struct lttcomm_relayd_status_session reply
;
944 memset(&reply
, 0, sizeof(reply
));
946 session
= session_create();
951 session
->minor
= conn
->minor
;
952 session
->major
= conn
->major
;
953 conn
->session_id
= session
->id
;
954 conn
->session
= session
;
956 reply
.session_id
= htobe64(session
->id
);
958 switch (conn
->minor
) {
963 case 4: /* LTTng sessiond 2.4 */
965 ret
= cmd_create_session_2_4(conn
, session
);
968 lttng_ht_add_unique_u64(conn
->sessions_ht
, &session
->session_n
);
969 DBG("Created session %" PRIu64
, session
->id
);
973 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
975 reply
.ret_code
= htobe32(LTTNG_OK
);
978 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
980 ERR("Relayd sending session id");
988 * When we have received all the streams and the metadata for a channel,
989 * we make them visible to the viewer threads.
992 void set_viewer_ready_flag(struct relay_connection
*conn
)
994 struct relay_stream
*stream
, *tmp_stream
;
996 pthread_mutex_lock(&conn
->session
->viewer_ready_lock
);
997 cds_list_for_each_entry_safe(stream
, tmp_stream
, &conn
->recv_head
,
999 stream
->viewer_ready
= 1;
1000 cds_list_del(&stream
->recv_list
);
1002 pthread_mutex_unlock(&conn
->session
->viewer_ready_lock
);
1007 * Add a recv handle node to the connection recv list with the given stream
1008 * handle. A new node is allocated thus must be freed when the node is deleted
1011 static void queue_stream(struct relay_stream
*stream
,
1012 struct relay_connection
*conn
)
1017 cds_list_add(&stream
->recv_list
, &conn
->recv_head
);
1021 * relay_add_stream: allocate a new stream for a session
1024 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1025 struct relay_connection
*conn
)
1028 struct relay_session
*session
= conn
->session
;
1029 struct relay_stream
*stream
= NULL
;
1030 struct lttcomm_relayd_status_stream reply
;
1031 struct ctf_trace
*trace
;
1033 if (!session
|| conn
->version_check_done
== 0) {
1034 ERR("Trying to add a stream before version check");
1036 goto end_no_session
;
1039 stream
= zmalloc(sizeof(struct relay_stream
));
1040 if (stream
== NULL
) {
1041 PERROR("relay stream zmalloc");
1043 goto end_no_session
;
1046 switch (conn
->minor
) {
1047 case 1: /* LTTng sessiond 2.1 */
1048 ret
= cmd_recv_stream_2_1(conn
, stream
);
1050 case 2: /* LTTng sessiond 2.2 */
1052 ret
= cmd_recv_stream_2_2(conn
, stream
);
1056 goto err_free_stream
;
1060 stream
->stream_handle
= ++last_relay_stream_id
;
1061 stream
->prev_seq
= -1ULL;
1062 stream
->session_id
= session
->id
;
1063 stream
->index_fd
= -1;
1064 stream
->read_index_fd
= -1;
1065 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
1066 pthread_mutex_init(&stream
->lock
, NULL
);
1068 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1070 ERR("relay creating output directory");
1075 * No need to use run_as API here because whatever we receives, the relayd
1076 * uses its own credentials for the stream files.
1078 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1079 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1081 ERR("Create output file");
1085 if (stream
->tracefile_size
) {
1086 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1088 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1091 trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, stream
->path_name
);
1093 trace
= ctf_trace_create(stream
->path_name
);
1098 ctf_trace_add(session
->ctf_traces_ht
, trace
);
1100 ctf_trace_get_ref(trace
);
1102 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1103 stream
->metadata_flag
= 1;
1104 /* Assign quick reference to the metadata stream in the trace. */
1105 trace
->metadata_stream
= stream
;
1109 * Add the stream in the recv list of the connection. Once the end stream
1110 * message is received, this list is emptied and streams are set with the
1111 * viewer ready flag.
1113 queue_stream(stream
, conn
);
1116 * Both in the ctf_trace object and the global stream ht since the data
1117 * side of the relayd does not have the concept of session.
1119 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
1120 cds_list_add_tail(&stream
->trace_list
, &trace
->stream_list
);
1122 session
->stream_count
++;
1124 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1125 stream
->stream_handle
);
1128 reply
.handle
= htobe64(stream
->stream_handle
);
1129 /* send the session id to the client or a negative return code on error */
1131 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1132 /* stream was not properly added to the ht, so free it */
1135 reply
.ret_code
= htobe32(LTTNG_OK
);
1138 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1139 sizeof(struct lttcomm_relayd_status_stream
), 0);
1141 ERR("Relay sending stream id");
1150 free(stream
->path_name
);
1151 free(stream
->channel_name
);
1157 * relay_close_stream: close a specific stream
1160 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1161 struct relay_connection
*conn
)
1164 struct relay_session
*session
= conn
->session
;
1165 struct lttcomm_relayd_close_stream stream_info
;
1166 struct lttcomm_relayd_generic_reply reply
;
1167 struct relay_stream
*stream
;
1169 DBG("Close stream received");
1171 if (!session
|| conn
->version_check_done
== 0) {
1172 ERR("Trying to close a stream before version check");
1174 goto end_no_session
;
1177 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &stream_info
,
1178 sizeof(struct lttcomm_relayd_close_stream
), 0);
1179 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1181 /* Orderly shutdown. Not necessary to print an error. */
1182 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1184 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1187 goto end_no_session
;
1191 stream
= stream_find_by_id(relay_streams_ht
,
1192 be64toh(stream_info
.stream_id
));
1198 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1199 stream
->close_flag
= 1;
1200 session
->stream_count
--;
1201 assert(session
->stream_count
>= 0);
1203 /* Check if we can close it or else the data will do it. */
1204 try_close_stream(session
, stream
);
1210 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1212 reply
.ret_code
= htobe32(LTTNG_OK
);
1214 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1215 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1217 ERR("Relay sending stream id");
1226 * relay_unknown_command: send -1 if received unknown command
1229 void relay_unknown_command(struct relay_connection
*conn
)
1231 struct lttcomm_relayd_generic_reply reply
;
1234 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1235 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1236 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1238 ERR("Relay sending unknown command");
1243 * relay_start: send an acknowledgment to the client to tell if we are
1244 * ready to receive data. We are ready if a session is established.
1247 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1248 struct relay_connection
*conn
)
1250 int ret
= htobe32(LTTNG_OK
);
1251 struct lttcomm_relayd_generic_reply reply
;
1252 struct relay_session
*session
= conn
->session
;
1255 DBG("Trying to start the streaming without a session established");
1256 ret
= htobe32(LTTNG_ERR_UNK
);
1259 reply
.ret_code
= ret
;
1260 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1261 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1263 ERR("Relay sending start ack");
1270 * Append padding to the file pointed by the file descriptor fd.
1272 static int write_padding_to_file(int fd
, uint32_t size
)
1281 zeros
= zmalloc(size
);
1282 if (zeros
== NULL
) {
1283 PERROR("zmalloc zeros for padding");
1288 ret
= lttng_write(fd
, zeros
, size
);
1290 PERROR("write padding to file");
1300 * relay_recv_metadata: receive the metada for the session.
1303 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1304 struct relay_connection
*conn
)
1306 int ret
= htobe32(LTTNG_OK
);
1308 struct relay_session
*session
= conn
->session
;
1309 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1310 struct relay_stream
*metadata_stream
;
1311 uint64_t data_size
, payload_size
;
1312 struct ctf_trace
*ctf_trace
;
1315 ERR("Metadata sent before version check");
1320 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1321 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1322 ERR("Incorrect data size");
1326 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1328 if (data_buffer_size
< data_size
) {
1329 /* In case the realloc fails, we can free the memory */
1332 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1333 if (!tmp_data_ptr
) {
1334 ERR("Allocating data buffer");
1339 data_buffer
= tmp_data_ptr
;
1340 data_buffer_size
= data_size
;
1342 memset(data_buffer
, 0, data_size
);
1343 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1344 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
1345 if (ret
< 0 || ret
!= data_size
) {
1347 /* Orderly shutdown. Not necessary to print an error. */
1348 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1350 ERR("Relay didn't receive the whole metadata");
1355 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1358 metadata_stream
= stream_find_by_id(relay_streams_ht
,
1359 be64toh(metadata_struct
->stream_id
));
1360 if (!metadata_stream
) {
1365 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1367 if (size_ret
< payload_size
) {
1368 ERR("Relay error writing metadata on file");
1373 ret
= write_padding_to_file(metadata_stream
->fd
,
1374 be32toh(metadata_struct
->padding_size
));
1379 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1380 metadata_stream
->path_name
);
1382 ctf_trace
->metadata_received
+=
1383 payload_size
+ be32toh(metadata_struct
->padding_size
);
1385 DBG2("Relay metadata written");
1394 * relay_send_version: send relayd version number
1397 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1398 struct relay_connection
*conn
)
1401 struct lttcomm_relayd_version reply
, msg
;
1405 conn
->version_check_done
= 1;
1407 /* Get version from the other side. */
1408 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1409 if (ret
< 0 || ret
!= sizeof(msg
)) {
1411 /* Orderly shutdown. Not necessary to print an error. */
1412 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1414 ERR("Relay failed to receive the version values.");
1420 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1421 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1423 /* Major versions must be the same */
1424 if (reply
.major
!= be32toh(msg
.major
)) {
1425 DBG("Incompatible major versions (%u vs %u), deleting session",
1426 reply
.major
, be32toh(msg
.major
));
1427 destroy_session(conn
->session
, conn
->sessions_ht
);
1432 conn
->major
= reply
.major
;
1433 /* We adapt to the lowest compatible version */
1434 if (reply
.minor
<= be32toh(msg
.minor
)) {
1435 conn
->minor
= reply
.minor
;
1437 conn
->minor
= be32toh(msg
.minor
);
1440 reply
.major
= htobe32(reply
.major
);
1441 reply
.minor
= htobe32(reply
.minor
);
1442 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
,
1443 sizeof(struct lttcomm_relayd_version
), 0);
1445 ERR("Relay sending version");
1448 DBG("Version check done using protocol %u.%u", conn
->major
,
1456 * Check for data pending for a given stream id from the session daemon.
1459 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1460 struct relay_connection
*conn
)
1462 struct relay_session
*session
= conn
->session
;
1463 struct lttcomm_relayd_data_pending msg
;
1464 struct lttcomm_relayd_generic_reply reply
;
1465 struct relay_stream
*stream
;
1467 uint64_t last_net_seq_num
, stream_id
;
1469 DBG("Data pending command received");
1471 if (!session
|| conn
->version_check_done
== 0) {
1472 ERR("Trying to check for data before version check");
1474 goto end_no_session
;
1477 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1478 if (ret
< sizeof(msg
)) {
1480 /* Orderly shutdown. Not necessary to print an error. */
1481 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1483 ERR("Relay didn't receive valid data_pending struct size : %d",
1487 goto end_no_session
;
1490 stream_id
= be64toh(msg
.stream_id
);
1491 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1494 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
1495 if (stream
== NULL
) {
1500 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1501 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1504 /* Avoid wrapping issue */
1505 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1506 /* Data has in fact been written and is NOT pending */
1509 /* Data still being streamed thus pending */
1513 /* Pending check is now done. */
1514 stream
->data_pending_check_done
= 1;
1519 reply
.ret_code
= htobe32(ret
);
1520 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1522 ERR("Relay data pending ret code failed");
1530 * Wait for the control socket to reach a quiescent state.
1532 * Note that for now, when receiving this command from the session daemon, this
1533 * means that every subsequent commands or data received on the control socket
1534 * has been handled. So, this is why we simply return OK here.
1537 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1538 struct relay_connection
*conn
)
1542 struct relay_stream
*stream
;
1543 struct lttng_ht_iter iter
;
1544 struct lttcomm_relayd_quiescent_control msg
;
1545 struct lttcomm_relayd_generic_reply reply
;
1547 DBG("Checking quiescent state on control socket");
1549 if (!conn
->session
|| conn
->version_check_done
== 0) {
1550 ERR("Trying to check for data before version check");
1552 goto end_no_session
;
1555 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1556 if (ret
< sizeof(msg
)) {
1558 /* Orderly shutdown. Not necessary to print an error. */
1559 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1561 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1565 goto end_no_session
;
1568 stream_id
= be64toh(msg
.stream_id
);
1571 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1573 if (stream
->stream_handle
== stream_id
) {
1574 stream
->data_pending_check_done
= 1;
1575 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1582 reply
.ret_code
= htobe32(LTTNG_OK
);
1583 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1585 ERR("Relay data quiescent control ret code failed");
1593 * Initialize a data pending command. This means that a client is about to ask
1594 * for data pending for each stream he/she holds. Simply iterate over all
1595 * streams of a session and set the data_pending_check_done flag.
1597 * This command returns to the client a LTTNG_OK code.
1600 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1601 struct relay_connection
*conn
)
1604 struct lttng_ht_iter iter
;
1605 struct lttcomm_relayd_begin_data_pending msg
;
1606 struct lttcomm_relayd_generic_reply reply
;
1607 struct relay_stream
*stream
;
1608 uint64_t session_id
;
1613 DBG("Init streams for data pending");
1615 if (!conn
->session
|| conn
->version_check_done
== 0) {
1616 ERR("Trying to check for data before version check");
1618 goto end_no_session
;
1621 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1622 if (ret
< sizeof(msg
)) {
1624 /* Orderly shutdown. Not necessary to print an error. */
1625 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1627 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1631 goto end_no_session
;
1634 session_id
= be64toh(msg
.session_id
);
1637 * Iterate over all streams to set the begin data pending flag. For now, the
1638 * streams are indexed by stream handle so we have to iterate over all
1639 * streams to find the one associated with the right session_id.
1642 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1644 if (stream
->session_id
== session_id
) {
1645 stream
->data_pending_check_done
= 0;
1646 DBG("Set begin data pending flag to stream %" PRIu64
,
1647 stream
->stream_handle
);
1652 /* All good, send back reply. */
1653 reply
.ret_code
= htobe32(LTTNG_OK
);
1655 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1657 ERR("Relay begin data pending send reply failed");
1665 * End data pending command. This will check, for a given session id, if each
1666 * stream associated with it has its data_pending_check_done flag set. If not,
1667 * this means that the client lost track of the stream but the data is still
1668 * being streamed on our side. In this case, we inform the client that data is
1671 * Return to the client if there is data in flight or not with a ret_code.
1674 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1675 struct relay_connection
*conn
)
1678 struct lttng_ht_iter iter
;
1679 struct lttcomm_relayd_end_data_pending msg
;
1680 struct lttcomm_relayd_generic_reply reply
;
1681 struct relay_stream
*stream
;
1682 uint64_t session_id
;
1683 uint32_t is_data_inflight
= 0;
1688 DBG("End data pending command");
1690 if (!conn
->session
|| conn
->version_check_done
== 0) {
1691 ERR("Trying to check for data before version check");
1693 goto end_no_session
;
1696 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &msg
, sizeof(msg
), 0);
1697 if (ret
< sizeof(msg
)) {
1699 /* Orderly shutdown. Not necessary to print an error. */
1700 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1702 ERR("Relay didn't receive valid end data_pending struct size: %d",
1706 goto end_no_session
;
1709 session_id
= be64toh(msg
.session_id
);
1711 /* Iterate over all streams to see if the begin data pending flag is set. */
1713 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1715 if (stream
->session_id
== session_id
&&
1716 !stream
->data_pending_check_done
&& !stream
->terminated_flag
) {
1717 is_data_inflight
= 1;
1718 DBG("Data is still in flight for stream %" PRIu64
,
1719 stream
->stream_handle
);
1725 /* All good, send back reply. */
1726 reply
.ret_code
= htobe32(is_data_inflight
);
1728 ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1730 ERR("Relay end data pending send reply failed");
1738 * Receive an index for a specific stream.
1740 * Return 0 on success else a negative value.
1743 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1744 struct relay_connection
*conn
)
1746 int ret
, send_ret
, index_created
= 0;
1747 struct relay_session
*session
= conn
->session
;
1748 struct lttcomm_relayd_index index_info
;
1749 struct relay_index
*index
, *wr_index
= NULL
;
1750 struct lttcomm_relayd_generic_reply reply
;
1751 struct relay_stream
*stream
;
1752 uint64_t net_seq_num
;
1756 DBG("Relay receiving index");
1758 if (!session
|| conn
->version_check_done
== 0) {
1759 ERR("Trying to close a stream before version check");
1761 goto end_no_session
;
1764 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &index_info
,
1765 sizeof(index_info
), 0);
1766 if (ret
< sizeof(index_info
)) {
1768 /* Orderly shutdown. Not necessary to print an error. */
1769 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
1771 ERR("Relay didn't receive valid index struct size : %d", ret
);
1774 goto end_no_session
;
1777 net_seq_num
= be64toh(index_info
.net_seq_num
);
1780 stream
= stream_find_by_id(relay_streams_ht
,
1781 be64toh(index_info
.relay_stream_id
));
1784 goto end_rcu_unlock
;
1787 /* Live beacon handling */
1788 if (index_info
.packet_size
== 0) {
1789 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1792 * Only flag a stream inactive when it has already received data.
1794 if (stream
->total_index_received
> 0) {
1795 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1798 goto end_rcu_unlock
;
1800 stream
->beacon_ts_end
= -1ULL;
1803 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1805 /* A successful creation will add the object to the HT. */
1806 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1808 goto end_rcu_unlock
;
1813 copy_index_control_data(index
, &index_info
);
1815 if (index_created
) {
1817 * Try to add the relay index object to the hash table. If an object
1818 * already exist, destroy back the index created, set the data in this
1819 * object and write it on disk.
1821 relay_index_add(index
, &wr_index
);
1823 copy_index_control_data(wr_index
, &index_info
);
1827 /* The index already exists so write it on disk. */
1831 /* Do we have a writable ready index to write on disk. */
1833 /* Starting at 2.4, create the index file if none available. */
1834 if (conn
->minor
>= 4 && stream
->index_fd
< 0) {
1835 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1836 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1837 stream
->tracefile_count_current
);
1839 goto end_rcu_unlock
;
1841 stream
->index_fd
= ret
;
1844 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1846 goto end_rcu_unlock
;
1848 stream
->total_index_received
++;
1855 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1857 reply
.ret_code
= htobe32(LTTNG_OK
);
1859 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1861 ERR("Relay sending close index id reply");
1870 * Receive the streams_sent message.
1872 * Return 0 on success else a negative value.
1875 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
1876 struct relay_connection
*conn
)
1879 struct lttcomm_relayd_generic_reply reply
;
1883 DBG("Relay receiving streams_sent");
1885 if (!conn
->session
|| conn
->version_check_done
== 0) {
1886 ERR("Trying to close a stream before version check");
1888 goto end_no_session
;
1892 * Flag every pending stream in the connection recv list that they are
1893 * ready to be used by the viewer.
1895 set_viewer_ready_flag(conn
);
1898 * Inform the viewer that there are new streams in the session.
1900 if (conn
->session
->viewer_refcount
) {
1901 uatomic_set(&conn
->session
->new_streams
, 1);
1904 reply
.ret_code
= htobe32(LTTNG_OK
);
1905 send_ret
= conn
->sock
->ops
->sendmsg(conn
->sock
, &reply
, sizeof(reply
), 0);
1907 ERR("Relay sending sent_stream reply");
1919 * Process the commands received on the control socket
1922 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1923 struct relay_connection
*conn
)
1927 switch (be32toh(recv_hdr
->cmd
)) {
1928 case RELAYD_CREATE_SESSION
:
1929 ret
= relay_create_session(recv_hdr
, conn
);
1931 case RELAYD_ADD_STREAM
:
1932 ret
= relay_add_stream(recv_hdr
, conn
);
1934 case RELAYD_START_DATA
:
1935 ret
= relay_start(recv_hdr
, conn
);
1937 case RELAYD_SEND_METADATA
:
1938 ret
= relay_recv_metadata(recv_hdr
, conn
);
1940 case RELAYD_VERSION
:
1941 ret
= relay_send_version(recv_hdr
, conn
);
1943 case RELAYD_CLOSE_STREAM
:
1944 ret
= relay_close_stream(recv_hdr
, conn
);
1946 case RELAYD_DATA_PENDING
:
1947 ret
= relay_data_pending(recv_hdr
, conn
);
1949 case RELAYD_QUIESCENT_CONTROL
:
1950 ret
= relay_quiescent_control(recv_hdr
, conn
);
1952 case RELAYD_BEGIN_DATA_PENDING
:
1953 ret
= relay_begin_data_pending(recv_hdr
, conn
);
1955 case RELAYD_END_DATA_PENDING
:
1956 ret
= relay_end_data_pending(recv_hdr
, conn
);
1958 case RELAYD_SEND_INDEX
:
1959 ret
= relay_recv_index(recv_hdr
, conn
);
1961 case RELAYD_STREAMS_SENT
:
1962 ret
= relay_streams_sent(recv_hdr
, conn
);
1964 case RELAYD_UPDATE_SYNC_INFO
:
1966 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1967 relay_unknown_command(conn
);
1977 * Handle index for a data stream.
1979 * RCU read side lock MUST be acquired.
1981 * Return 0 on success else a negative value.
1983 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
1986 int ret
= 0, index_created
= 0;
1987 uint64_t stream_id
, data_offset
;
1988 struct relay_index
*index
, *wr_index
= NULL
;
1992 stream_id
= stream
->stream_handle
;
1993 /* Get data offset because we are about to update the index. */
1994 data_offset
= htobe64(stream
->tracefile_size_current
);
1997 * Lookup for an existing index for that stream id/sequence number. If on
1998 * exists, the control thread already received the data for it thus we need
1999 * to write it on disk.
2001 index
= relay_index_find(stream_id
, net_seq_num
);
2003 /* A successful creation will add the object to the HT. */
2004 index
= relay_index_create(stream_id
, net_seq_num
);
2012 if (rotate_index
|| stream
->index_fd
< 0) {
2013 index
->to_close_fd
= stream
->index_fd
;
2014 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2015 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2016 stream
->tracefile_count_current
);
2018 /* This will close the stream's index fd if one. */
2019 relay_index_free_safe(index
);
2022 stream
->index_fd
= ret
;
2024 index
->fd
= stream
->index_fd
;
2025 index
->index_data
.offset
= data_offset
;
2027 if (index_created
) {
2029 * Try to add the relay index object to the hash table. If an object
2030 * already exist, destroy back the index created and set the data.
2032 relay_index_add(index
, &wr_index
);
2034 /* Copy back data from the created index. */
2035 wr_index
->fd
= index
->fd
;
2036 wr_index
->to_close_fd
= index
->to_close_fd
;
2037 wr_index
->index_data
.offset
= data_offset
;
2041 /* The index already exists so write it on disk. */
2045 /* Do we have a writable ready index to write on disk. */
2047 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2051 stream
->total_index_received
++;
2059 * relay_process_data: Process the data received on the data socket
2062 int relay_process_data(struct relay_connection
*conn
)
2064 int ret
= 0, rotate_index
= 0;
2066 struct relay_stream
*stream
;
2067 struct lttcomm_relayd_data_hdr data_hdr
;
2069 uint64_t net_seq_num
;
2071 struct relay_session
*session
;
2075 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &data_hdr
,
2076 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2079 /* Orderly shutdown. Not necessary to print an error. */
2080 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2082 ERR("Unable to receive data header on sock %d", conn
->sock
->fd
);
2088 stream_id
= be64toh(data_hdr
.stream_id
);
2091 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
2094 goto end_rcu_unlock
;
2097 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
2100 data_size
= be32toh(data_hdr
.data_size
);
2101 if (data_buffer_size
< data_size
) {
2104 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2105 if (!tmp_data_ptr
) {
2106 ERR("Allocating data buffer");
2109 goto end_rcu_unlock
;
2111 data_buffer
= tmp_data_ptr
;
2112 data_buffer_size
= data_size
;
2114 memset(data_buffer
, 0, data_size
);
2116 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2118 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2119 data_size
, stream_id
, net_seq_num
);
2120 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, data_buffer
, data_size
, 0);
2123 /* Orderly shutdown. Not necessary to print an error. */
2124 DBG("Socket %d did an orderly shutdown", conn
->sock
->fd
);
2127 goto end_rcu_unlock
;
2130 /* Check if a rotation is needed. */
2131 if (stream
->tracefile_size
> 0 &&
2132 (stream
->tracefile_size_current
+ data_size
) >
2133 stream
->tracefile_size
) {
2134 struct relay_viewer_stream
*vstream
;
2137 new_id
= (stream
->tracefile_count_current
+ 1) %
2138 stream
->tracefile_count
;
2140 * When we wrap-around back to 0, we start overwriting old
2143 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2144 stream
->tracefile_overwrite
= 1;
2146 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2147 if (stream
->tracefile_overwrite
) {
2148 stream
->oldest_tracefile_id
=
2149 (stream
->oldest_tracefile_id
+ 1) %
2150 stream
->tracefile_count
;
2152 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2155 * The viewer is reading a file about to be
2156 * overwritten. Close the FDs it is
2157 * currently using and let it handle the fault.
2159 if (vstream
->tracefile_count_current
== new_id
) {
2160 pthread_mutex_lock(&vstream
->overwrite_lock
);
2161 vstream
->abort_flag
= 1;
2162 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2163 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2164 stream
->channel_name
, new_id
);
2165 } else if (vstream
->tracefile_count_current
==
2166 stream
->tracefile_count_current
) {
2168 * The reader and writer were in the
2169 * same trace file, inform the viewer
2170 * that no new index will ever be added
2173 vstream
->close_write_flag
= 1;
2176 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2177 stream
->tracefile_size
, stream
->tracefile_count
,
2178 relayd_uid
, relayd_gid
, stream
->fd
,
2179 &(stream
->tracefile_count_current
), &stream
->fd
);
2180 stream
->total_index_received
= 0;
2181 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2183 ERR("Rotating stream output file");
2184 goto end_rcu_unlock
;
2186 /* Reset current size because we just perform a stream rotation. */
2187 stream
->tracefile_size_current
= 0;
2192 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2193 * index are NOT supported.
2195 if (session
->minor
>= 4 && !session
->snapshot
) {
2196 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2198 goto end_rcu_unlock
;
2202 /* Write data to stream output fd. */
2203 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2204 if (size_ret
< data_size
) {
2205 ERR("Relay error writing data to file");
2207 goto end_rcu_unlock
;
2210 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2211 ret
, stream
->stream_handle
);
2213 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2215 goto end_rcu_unlock
;
2217 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2219 stream
->prev_seq
= net_seq_num
;
2221 try_close_stream(session
, stream
);
2230 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2236 (void) lttng_poll_del(events
, pollfd
);
2238 ret
= close(pollfd
);
2240 ERR("Closing pollfd %d", pollfd
);
2244 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
2245 struct relay_connection
*conn
)
2247 assert(relay_connections_ht
);
2250 connection_delete(relay_connections_ht
, conn
);
2252 /* For the control socket, we try to destroy the session. */
2253 if (conn
->type
== RELAY_CONTROL
) {
2254 destroy_session(conn
->session
, conn
->sessions_ht
);
2257 connection_destroy(conn
);
2261 * This thread does the actual work
2264 void *relay_thread_worker(void *data
)
2266 int ret
, err
= -1, last_seen_data_fd
= -1;
2268 struct relay_connection
*conn
;
2269 struct lttng_poll_event events
;
2270 struct lttng_ht
*relay_connections_ht
;
2271 struct lttng_ht_iter iter
;
2272 struct lttcomm_relayd_hdr recv_hdr
;
2273 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2274 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2276 DBG("[thread] Relay worker started");
2278 rcu_register_thread();
2280 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2282 if (testpoint(relayd_thread_worker
)) {
2283 goto error_testpoint
;
2286 health_code_update();
2288 /* table of connections indexed on socket */
2289 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2290 if (!relay_connections_ht
) {
2291 goto relay_connections_ht_error
;
2294 /* Tables of received indexes indexed by index handle and net_seq_num. */
2295 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2297 goto indexes_ht_error
;
2300 ret
= create_thread_poll_set(&events
, 2);
2302 goto error_poll_create
;
2305 ret
= lttng_poll_add(&events
, relay_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2312 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2314 health_code_update();
2316 /* Infinite blocking call, waiting for transmission */
2317 DBG3("Relayd worker thread polling...");
2318 health_poll_entry();
2319 ret
= lttng_poll_wait(&events
, -1);
2323 * Restart interrupted system call.
2325 if (errno
== EINTR
) {
2334 * Process control. The control connection is prioritised so we don't
2335 * starve it with high throughout put tracing data on the data
2338 for (i
= 0; i
< nb_fd
; i
++) {
2339 /* Fetch once the poll data */
2340 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2341 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2343 health_code_update();
2345 /* Thread quit pipe has been closed. Killing thread. */
2346 ret
= check_thread_quit_pipe(pollfd
, revents
);
2352 /* Inspect the relay conn pipe for new connection */
2353 if (pollfd
== relay_conn_pipe
[0]) {
2354 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2355 ERR("Relay connection pipe error");
2357 } else if (revents
& LPOLLIN
) {
2358 ret
= lttng_read(relay_conn_pipe
[0], &conn
, sizeof(conn
));
2362 conn
->sessions_ht
= sessions_ht
;
2363 connection_init(conn
);
2364 lttng_poll_add(&events
, conn
->sock
->fd
,
2365 LPOLLIN
| LPOLLRDHUP
);
2367 lttng_ht_add_unique_ulong(relay_connections_ht
,
2370 DBG("Connection socket %d added", conn
->sock
->fd
);
2374 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2375 /* If not found, there is a synchronization issue. */
2378 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2379 cleanup_connection_pollfd(&events
, pollfd
);
2380 destroy_connection(relay_connections_ht
, conn
);
2381 if (last_seen_data_fd
== pollfd
) {
2382 last_seen_data_fd
= last_notdel_data_fd
;
2384 } else if (revents
& LPOLLIN
) {
2385 if (conn
->type
== RELAY_CONTROL
) {
2386 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2387 sizeof(recv_hdr
), 0);
2389 /* Connection closed */
2390 cleanup_connection_pollfd(&events
, pollfd
);
2391 destroy_connection(relay_connections_ht
, conn
);
2392 DBG("Control connection closed with %d", pollfd
);
2394 ret
= relay_process_control(&recv_hdr
, conn
);
2396 /* Clear the session on error. */
2397 cleanup_connection_pollfd(&events
, pollfd
);
2398 destroy_connection(relay_connections_ht
, conn
);
2399 DBG("Connection closed with %d", pollfd
);
2405 * Flag the last seen data fd not deleted. It will be
2406 * used as the last seen fd if any fd gets deleted in
2409 last_notdel_data_fd
= pollfd
;
2412 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
2419 * The last loop handled a control request, go back to poll to make
2420 * sure we prioritise the control socket.
2426 if (last_seen_data_fd
>= 0) {
2427 for (i
= 0; i
< nb_fd
; i
++) {
2428 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2430 health_code_update();
2432 if (last_seen_data_fd
== pollfd
) {
2439 /* Process data connection. */
2440 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2441 /* Fetch the poll data. */
2442 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2443 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2445 health_code_update();
2447 /* Skip the command pipe. It's handled in the first loop. */
2448 if (pollfd
== relay_conn_pipe
[0]) {
2454 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
2456 /* Skip it. Might be removed before. */
2461 if (revents
& LPOLLIN
) {
2462 if (conn
->type
!= RELAY_DATA
) {
2466 ret
= relay_process_data(conn
);
2467 /* Connection closed */
2469 cleanup_connection_pollfd(&events
, pollfd
);
2470 destroy_connection(relay_connections_ht
, conn
);
2471 DBG("Data connection closed with %d", pollfd
);
2473 * Every goto restart call sets the last seen fd where
2474 * here we don't really care since we gracefully
2475 * continue the loop after the connection is deleted.
2478 /* Keep last seen port. */
2479 last_seen_data_fd
= pollfd
;
2487 last_seen_data_fd
= -1;
2490 /* Normal exit, no error */
2495 lttng_poll_clean(&events
);
2497 /* Cleanup reamaining connection object. */
2499 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2501 health_code_update();
2502 destroy_connection(relay_connections_ht
, conn
);
2506 lttng_ht_destroy(indexes_ht
);
2508 lttng_ht_destroy(relay_connections_ht
);
2509 relay_connections_ht_error
:
2510 /* Close relay conn pipes */
2511 utils_close_pipe(relay_conn_pipe
);
2513 DBG("Thread exited with error");
2515 DBG("Worker thread cleanup complete");
2520 ERR("Health error occurred in %s", __func__
);
2522 health_unregister(health_relayd
);
2523 rcu_unregister_thread();
2529 * Create the relay command pipe to wake thread_manage_apps.
2530 * Closed in cleanup().
2532 static int create_relay_conn_pipe(void)
2536 ret
= utils_create_pipe_cloexec(relay_conn_pipe
);
2544 int main(int argc
, char **argv
)
2548 struct relay_local_data
*relay_ctx
;
2550 /* Parse arguments */
2552 if ((ret
= parse_args(argc
, argv
)) < 0) {
2556 if ((ret
= set_signal_handler()) < 0) {
2560 /* Try to create directory if -o, --output is specified. */
2561 if (opt_output_path
) {
2562 if (*opt_output_path
!= '/') {
2563 ERR("Please specify an absolute path for -o, --output PATH");
2567 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2569 ERR("Unable to create %s", opt_output_path
);
2575 if (opt_daemon
|| opt_background
) {
2578 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2585 * We are in the child. Make sure all other file
2586 * descriptors are closed, in case we are called with
2587 * more opened file descriptors than the standard ones.
2589 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2594 /* Create thread quit pipe */
2595 if ((ret
= init_thread_quit_pipe()) < 0) {
2599 /* We need those values for the file/dir creation. */
2600 relayd_uid
= getuid();
2601 relayd_gid
= getgid();
2603 /* Check if daemon is UID = 0 */
2604 if (relayd_uid
== 0) {
2605 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2606 live_uri
->port
< 1024) {
2607 ERR("Need to be root to use ports < 1024");
2613 /* Setup the thread apps communication pipe. */
2614 if ((ret
= create_relay_conn_pipe()) < 0) {
2618 /* Init relay command queue. */
2619 cds_wfq_init(&relay_conn_queue
.queue
);
2621 /* Set up max poll set size */
2622 lttng_poll_set_max_size();
2624 /* Initialize communication library */
2626 lttcomm_inet_init();
2628 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2630 PERROR("relay_ctx");
2634 /* tables of sessions indexed by session ID */
2635 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2636 if (!relay_ctx
->sessions_ht
) {
2637 goto exit_relay_ctx_sessions
;
2640 /* tables of streams indexed by stream ID */
2641 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2642 if (!relay_streams_ht
) {
2643 goto exit_relay_ctx_streams
;
2646 /* tables of streams indexed by stream ID */
2647 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2648 if (!viewer_streams_ht
) {
2649 goto exit_relay_ctx_viewer_streams
;
2652 /* Initialize thread health monitoring */
2653 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2654 if (!health_relayd
) {
2655 PERROR("health_app_create error");
2656 goto exit_health_app_create
;
2659 ret
= utils_create_pipe(health_quit_pipe
);
2661 goto error_health_pipe
;
2664 /* Create thread to manage the client socket */
2665 ret
= pthread_create(&health_thread
, NULL
,
2666 thread_manage_health
, (void *) NULL
);
2668 PERROR("pthread_create health");
2672 /* Setup the dispatcher thread */
2673 ret
= pthread_create(&dispatcher_thread
, NULL
,
2674 relay_thread_dispatcher
, (void *) NULL
);
2676 PERROR("pthread_create dispatcher");
2677 goto exit_dispatcher
;
2680 /* Setup the worker thread */
2681 ret
= pthread_create(&worker_thread
, NULL
,
2682 relay_thread_worker
, (void *) relay_ctx
);
2684 PERROR("pthread_create worker");
2688 /* Setup the listener thread */
2689 ret
= pthread_create(&listener_thread
, NULL
,
2690 relay_thread_listener
, (void *) NULL
);
2692 PERROR("pthread_create listener");
2696 ret
= live_start_threads(live_uri
, relay_ctx
);
2698 ERR("Starting live viewer threads");
2703 ret
= pthread_join(listener_thread
, &status
);
2705 PERROR("pthread_join");
2706 goto error
; /* join error, exit without cleanup */
2710 ret
= pthread_join(worker_thread
, &status
);
2712 PERROR("pthread_join");
2713 goto error
; /* join error, exit without cleanup */
2717 ret
= pthread_join(dispatcher_thread
, &status
);
2719 PERROR("pthread_join");
2720 goto error
; /* join error, exit without cleanup */
2724 ret
= pthread_join(health_thread
, &status
);
2726 PERROR("pthread_join health thread");
2727 goto error
; /* join error, exit without cleanup */
2731 * Stop live threads only after joining other threads.
2733 live_stop_threads();
2736 utils_close_pipe(health_quit_pipe
);
2739 health_app_destroy(health_relayd
);
2741 exit_health_app_create
:
2742 lttng_ht_destroy(viewer_streams_ht
);
2744 exit_relay_ctx_viewer_streams
:
2745 lttng_ht_destroy(relay_streams_ht
);
2747 exit_relay_ctx_streams
:
2748 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2750 exit_relay_ctx_sessions
: