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/futex.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49 #include <common/sessiond-comm/inet.h>
50 #include <common/sessiond-comm/relayd.h>
51 #include <common/uri.h>
52 #include <common/utils.h>
55 #include "ctf-trace.h"
58 #include "lttng-relayd.h"
60 #include "health-relayd.h"
62 /* command line options */
63 char *opt_output_path
;
64 static int opt_daemon
;
65 static struct lttng_uri
*control_uri
;
66 static struct lttng_uri
*data_uri
;
67 static struct lttng_uri
*live_uri
;
71 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
74 * Quit pipe for all threads. This permits a single cancellation point
75 * for all threads when receiving an event on the pipe.
77 static int thread_quit_pipe
[2] = { -1, -1 };
80 * This pipe is used to inform the worker thread that a command is queued and
81 * ready to be processed.
83 static int relay_cmd_pipe
[2] = { -1, -1 };
85 /* Shared between threads */
86 static int dispatch_thread_exit
;
88 static pthread_t listener_thread
;
89 static pthread_t dispatcher_thread
;
90 static pthread_t worker_thread
;
91 static pthread_t health_thread
;
93 static uint64_t last_relay_stream_id
;
94 static uint64_t last_relay_session_id
;
97 * Relay command queue.
99 * The relay_thread_listener and relay_thread_dispatcher communicate with this
102 static struct relay_cmd_queue relay_cmd_queue
;
104 /* buffer allocated at startup, used to store the trace data */
105 static char *data_buffer
;
106 static unsigned int data_buffer_size
;
108 /* We need those values for the file/dir creation. */
109 static uid_t relayd_uid
;
110 static gid_t relayd_gid
;
112 /* Global relay stream hash table. */
113 struct lttng_ht
*relay_streams_ht
;
115 /* Global relay viewer stream hash table. */
116 struct lttng_ht
*viewer_streams_ht
;
118 /* Global hash table that stores relay index object. */
119 struct lttng_ht
*indexes_ht
;
121 /* Relayd health monitoring */
122 struct health_app
*health_relayd
;
125 * usage function on stderr
130 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
131 fprintf(stderr
, " -h, --help Display this usage.\n");
132 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
133 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
134 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
135 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
136 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
137 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
141 int parse_args(int argc
, char **argv
)
145 char *default_address
;
147 static struct option long_options
[] = {
148 { "control-port", 1, 0, 'C', },
149 { "data-port", 1, 0, 'D', },
150 { "daemonize", 0, 0, 'd', },
151 { "group", 1, 0, 'g', },
152 { "help", 0, 0, 'h', },
153 { "output", 1, 0, 'o', },
154 { "verbose", 0, 0, 'v', },
159 int option_index
= 0;
160 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:g:",
161 long_options
, &option_index
);
168 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
170 fprintf(stderr
, " with arg %s\n", optarg
);
174 ret
= uri_parse(optarg
, &control_uri
);
176 ERR("Invalid control URI specified");
179 if (control_uri
->port
== 0) {
180 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
184 ret
= uri_parse(optarg
, &data_uri
);
186 ERR("Invalid data URI specified");
189 if (data_uri
->port
== 0) {
190 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
197 tracing_group_name
= optarg
;
203 ret
= asprintf(&opt_output_path
, "%s", optarg
);
205 PERROR("asprintf opt_output_path");
210 /* Verbose level can increase using multiple -v */
211 lttng_opt_verbose
+= 1;
214 /* Unknown option or other error.
215 * Error is printed by getopt, just return */
221 /* assign default values */
222 if (control_uri
== NULL
) {
223 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
224 DEFAULT_NETWORK_CONTROL_PORT
);
226 PERROR("asprintf default data address");
230 ret
= uri_parse(default_address
, &control_uri
);
231 free(default_address
);
233 ERR("Invalid control URI specified");
237 if (data_uri
== NULL
) {
238 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
239 DEFAULT_NETWORK_DATA_PORT
);
241 PERROR("asprintf default data address");
245 ret
= uri_parse(default_address
, &data_uri
);
246 free(default_address
);
248 ERR("Invalid data URI specified");
252 if (live_uri
== NULL
) {
253 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
254 DEFAULT_NETWORK_VIEWER_PORT
);
256 PERROR("asprintf default viewer control address");
260 ret
= uri_parse(default_address
, &live_uri
);
261 free(default_address
);
263 ERR("Invalid viewer control URI specified");
280 /* free the dynamically allocated opt_output_path */
281 free(opt_output_path
);
283 /* Close thread quit pipes */
284 utils_close_pipe(thread_quit_pipe
);
286 uri_free(control_uri
);
291 * Write to writable pipe used to notify a thread.
294 int notify_thread_pipe(int wpipe
)
298 ret
= lttng_write(wpipe
, "!", 1);
300 PERROR("write poll pipe");
306 static void notify_health_quit_pipe(int *pipe
)
310 ret
= lttng_write(pipe
[1], "4", 1);
312 PERROR("write relay health quit");
317 * Stop all threads by closing the thread quit pipe.
320 void stop_threads(void)
324 /* Stopping all threads */
325 DBG("Terminating all threads");
326 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
328 ERR("write error on thread quit pipe");
331 notify_health_quit_pipe(health_quit_pipe
);
333 /* Dispatch thread */
334 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
335 futex_nto1_wake(&relay_cmd_queue
.futex
);
339 * Signal handler for the daemon
341 * Simply stop all worker threads, leaving main() return gracefully after
342 * joining all threads and calling cleanup().
345 void sighandler(int sig
)
349 DBG("SIGPIPE caught");
352 DBG("SIGINT caught");
356 DBG("SIGTERM caught");
365 * Setup signal handler for :
366 * SIGINT, SIGTERM, SIGPIPE
369 int set_signal_handler(void)
375 if ((ret
= sigemptyset(&sigset
)) < 0) {
376 PERROR("sigemptyset");
380 sa
.sa_handler
= sighandler
;
383 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
388 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
393 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
398 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
404 * Init thread quit pipe.
406 * Return -1 on error or 0 if all pipes are created.
409 int init_thread_quit_pipe(void)
413 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
419 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
422 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
426 if (events
== NULL
|| size
== 0) {
431 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
437 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
449 * Check if the thread quit pipe was triggered.
451 * Return 1 if it was triggered else 0;
454 int check_thread_quit_pipe(int fd
, uint32_t events
)
456 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
464 * Create and init socket from uri.
467 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
470 struct lttcomm_sock
*sock
= NULL
;
472 sock
= lttcomm_alloc_sock_from_uri(uri
);
474 ERR("Allocating socket");
478 ret
= lttcomm_create_sock(sock
);
482 DBG("Listening on sock %d", sock
->fd
);
484 ret
= sock
->ops
->bind(sock
);
489 ret
= sock
->ops
->listen(sock
, -1);
499 lttcomm_destroy_sock(sock
);
505 * Return nonzero if stream needs to be closed.
508 int close_stream_check(struct relay_stream
*stream
)
511 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
513 * We are about to close the stream so set the data pending flag to 1
514 * which will make the end data pending command skip the stream which
515 * is now closed and ready. Note that after proceeding to a file close,
516 * the written file is ready for reading.
518 stream
->data_pending_check_done
= 1;
525 * This thread manages the listening for new connections on the network
528 void *relay_thread_listener(void *data
)
530 int i
, ret
, pollfd
, err
= -1;
532 uint32_t revents
, nb_fd
;
533 struct lttng_poll_event events
;
534 struct lttcomm_sock
*control_sock
, *data_sock
;
536 DBG("[thread] Relay listener started");
538 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
540 health_code_update();
542 control_sock
= relay_init_sock(control_uri
);
544 goto error_sock_control
;
547 data_sock
= relay_init_sock(data_uri
);
549 goto error_sock_relay
;
553 * Pass 3 as size here for the thread quit pipe, control and data socket.
555 ret
= create_thread_poll_set(&events
, 3);
557 goto error_create_poll
;
560 /* Add the control socket */
561 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
566 /* Add the data socket */
567 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
573 health_code_update();
575 DBG("Listener accepting connections");
579 ret
= lttng_poll_wait(&events
, -1);
583 * Restart interrupted system call.
585 if (errno
== EINTR
) {
593 DBG("Relay new connection received");
594 for (i
= 0; i
< nb_fd
; i
++) {
595 health_code_update();
597 /* Fetch once the poll data */
598 revents
= LTTNG_POLL_GETEV(&events
, i
);
599 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
601 /* Thread quit pipe has been closed. Killing thread. */
602 ret
= check_thread_quit_pipe(pollfd
, revents
);
608 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
609 ERR("socket poll error");
611 } else if (revents
& LPOLLIN
) {
613 * Get allocated in this thread,
614 * enqueued to a global queue, dequeued
615 * and freed in the worker thread.
617 struct relay_command
*relay_cmd
;
618 struct lttcomm_sock
*newsock
;
620 relay_cmd
= zmalloc(sizeof(struct relay_command
));
621 if (relay_cmd
== NULL
) {
622 PERROR("relay command zmalloc");
626 if (pollfd
== data_sock
->fd
) {
627 newsock
= data_sock
->ops
->accept(data_sock
);
629 PERROR("accepting data sock");
633 relay_cmd
->type
= RELAY_DATA
;
634 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
636 assert(pollfd
== control_sock
->fd
);
637 newsock
= control_sock
->ops
->accept(control_sock
);
639 PERROR("accepting control sock");
643 relay_cmd
->type
= RELAY_CONTROL
;
644 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
646 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
649 PERROR("setsockopt inet");
650 lttcomm_destroy_sock(newsock
);
654 relay_cmd
->sock
= newsock
;
656 * Lock free enqueue the request.
658 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
661 * Wake the dispatch queue futex. Implicit memory
662 * barrier with the exchange in cds_wfq_enqueue.
664 futex_nto1_wake(&relay_cmd_queue
.futex
);
672 lttng_poll_clean(&events
);
674 if (data_sock
->fd
>= 0) {
675 ret
= data_sock
->ops
->close(data_sock
);
680 lttcomm_destroy_sock(data_sock
);
682 if (control_sock
->fd
>= 0) {
683 ret
= control_sock
->ops
->close(control_sock
);
688 lttcomm_destroy_sock(control_sock
);
692 ERR("Health error occurred in %s", __func__
);
694 health_unregister(health_relayd
);
695 DBG("Relay listener thread cleanup complete");
701 * This thread manages the dispatching of the requests to worker threads
704 void *relay_thread_dispatcher(void *data
)
708 struct cds_wfq_node
*node
;
709 struct relay_command
*relay_cmd
= NULL
;
711 DBG("[thread] Relay dispatcher started");
713 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
715 health_code_update();
717 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
718 health_code_update();
720 /* Atomically prepare the queue futex */
721 futex_nto1_prepare(&relay_cmd_queue
.futex
);
724 health_code_update();
726 /* Dequeue commands */
727 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
729 DBG("Woken up but nothing in the relay command queue");
730 /* Continue thread execution */
734 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
735 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
738 * Inform worker thread of the new request. This
739 * call is blocking so we can be assured that the data will be read
740 * at some point in time or wait to the end of the world :)
742 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
743 sizeof(struct relay_command
));
745 if (ret
< sizeof(struct relay_command
)) {
746 PERROR("write cmd pipe");
749 } while (node
!= NULL
);
751 /* Futex wait on queue. Blocking call on futex() */
753 futex_nto1_wait(&relay_cmd_queue
.futex
);
757 /* Normal exit, no error */
763 ERR("Health error occurred in %s", __func__
);
765 health_unregister(health_relayd
);
766 DBG("Dispatch thread dying");
772 * Get stream from stream id.
773 * Need to be called with RCU read-side lock held.
775 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
777 struct lttng_ht_node_ulong
*node
;
778 struct lttng_ht_iter iter
;
779 struct relay_stream
*ret
;
781 lttng_ht_lookup(relay_streams_ht
,
782 (void *)((unsigned long) stream_id
),
784 node
= lttng_ht_iter_get_node_ulong(&iter
);
786 DBG("Relay stream %" PRIu64
" not found", stream_id
);
791 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
798 void deferred_free_stream(struct rcu_head
*head
)
800 struct relay_stream
*stream
=
801 caa_container_of(head
, struct relay_stream
, rcu_node
);
803 ctf_trace_try_destroy(stream
->ctf_trace
);
805 free(stream
->path_name
);
806 free(stream
->channel_name
);
811 void deferred_free_session(struct rcu_head
*head
)
813 struct relay_session
*session
=
814 caa_container_of(head
, struct relay_session
, rcu_node
);
819 * Close a given stream. The stream is freed using a call RCU.
821 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
822 * BEFORE the stream lock MUST be acquired.
824 static void destroy_stream(struct relay_stream
*stream
)
827 struct relay_viewer_stream
*vstream
;
828 struct lttng_ht_iter iter
;
832 delret
= close(stream
->fd
);
834 PERROR("close stream");
837 if (stream
->index_fd
>= 0) {
838 delret
= close(stream
->index_fd
);
840 PERROR("close stream index_fd");
844 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
847 * Set the last good value into the viewer stream. This is done
848 * right before the stream gets deleted from the hash table. The
849 * lookup failure on the live thread side of a stream indicates
850 * that the viewer stream index received value should be used.
852 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
853 vstream
->total_index_received
= stream
->total_index_received
;
854 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
855 vstream
->close_write_flag
= 1;
856 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
859 /* Cleanup index of that stream. */
860 relay_index_destroy_by_stream_id(stream
->stream_handle
);
862 iter
.iter
.node
= &stream
->stream_n
.node
;
863 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
865 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
866 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
868 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
869 DBG("Closed tracefile %d from close stream", stream
->fd
);
873 * relay_delete_session: Free all memory associated with a session and
877 void relay_delete_session(struct relay_command
*cmd
,
878 struct lttng_ht
*sessions_ht
)
880 struct lttng_ht_iter iter
;
881 struct lttng_ht_node_ulong
*node
;
882 struct relay_stream
*stream
;
889 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
892 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
893 node
= lttng_ht_iter_get_node_ulong(&iter
);
897 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
898 if (stream
->session
== cmd
->session
) {
899 destroy_stream(stream
);
900 cmd
->session
->stream_count
--;
901 assert(cmd
->session
->stream_count
>= 0);
905 /* Make this session not visible anymore. */
906 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
907 ret
= lttng_ht_del(sessions_ht
, &iter
);
909 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
914 * Copy index data from the control port to a given index object.
916 static void copy_index_control_data(struct relay_index
*index
,
917 struct lttcomm_relayd_index
*data
)
923 * The index on disk is encoded in big endian, so we don't need to convert
924 * the data received on the network. The data_offset value is NEVER
925 * modified here and is updated by the data thread.
927 index
->index_data
.packet_size
= data
->packet_size
;
928 index
->index_data
.content_size
= data
->content_size
;
929 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
930 index
->index_data
.timestamp_end
= data
->timestamp_end
;
931 index
->index_data
.events_discarded
= data
->events_discarded
;
932 index
->index_data
.stream_id
= data
->stream_id
;
936 * Handle the RELAYD_CREATE_SESSION command.
938 * On success, send back the session id or else return a negative value.
941 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
942 struct relay_command
*cmd
,
943 struct lttng_ht
*sessions_ht
)
945 int ret
= 0, send_ret
;
946 struct relay_session
*session
;
947 struct lttcomm_relayd_status_session reply
;
952 memset(&reply
, 0, sizeof(reply
));
954 session
= zmalloc(sizeof(struct relay_session
));
955 if (session
== NULL
) {
956 PERROR("relay session zmalloc");
961 session
->id
= ++last_relay_session_id
;
962 session
->sock
= cmd
->sock
;
963 session
->minor
= cmd
->minor
;
964 session
->major
= cmd
->major
;
965 cmd
->session
= session
;
967 reply
.session_id
= htobe64(session
->id
);
969 switch (cmd
->minor
) {
970 case 4: /* LTTng sessiond 2.4 */
972 ret
= cmd_create_session_2_4(cmd
, session
);
976 lttng_ht_node_init_ulong(&session
->session_n
,
977 (unsigned long) session
->id
);
978 lttng_ht_add_unique_ulong(sessions_ht
,
979 &session
->session_n
);
981 DBG("Created session %" PRIu64
, session
->id
);
985 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
987 reply
.ret_code
= htobe32(LTTNG_OK
);
990 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
992 ERR("Relayd sending session id");
1000 * relay_add_stream: allocate a new stream for a session
1003 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1004 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1006 struct relay_session
*session
= cmd
->session
;
1007 struct relay_stream
*stream
= NULL
;
1008 struct lttcomm_relayd_status_stream reply
;
1011 if (!session
|| cmd
->version_check_done
== 0) {
1012 ERR("Trying to add a stream before version check");
1014 goto end_no_session
;
1017 stream
= zmalloc(sizeof(struct relay_stream
));
1018 if (stream
== NULL
) {
1019 PERROR("relay stream zmalloc");
1021 goto end_no_session
;
1024 switch (cmd
->minor
) {
1025 case 1: /* LTTng sessiond 2.1 */
1026 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1028 case 2: /* LTTng sessiond 2.2 */
1030 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1034 goto err_free_stream
;
1038 stream
->stream_handle
= ++last_relay_stream_id
;
1039 stream
->prev_seq
= -1ULL;
1040 stream
->session
= session
;
1041 stream
->index_fd
= -1;
1042 stream
->read_index_fd
= -1;
1043 stream
->ctf_trace
= NULL
;
1044 pthread_mutex_init(&stream
->lock
, NULL
);
1046 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1048 ERR("relay creating output directory");
1053 * No need to use run_as API here because whatever we receives, the relayd
1054 * uses its own credentials for the stream files.
1056 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1057 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1059 ERR("Create output file");
1063 if (stream
->tracefile_size
) {
1064 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1066 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1069 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1070 stream
->metadata_flag
= 1;
1072 * When we receive a new metadata stream, we create a new
1073 * ctf_trace and we assign this ctf_trace to all streams with
1076 * If later on we receive a new stream for the same ctf_trace,
1077 * we copy the information from the first hit in the HT to the
1080 stream
->ctf_trace
= ctf_trace_create();
1081 if (!stream
->ctf_trace
) {
1085 stream
->ctf_trace
->refcount
++;
1086 stream
->ctf_trace
->metadata_stream
= stream
;
1088 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1089 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1091 lttng_ht_node_init_ulong(&stream
->stream_n
,
1092 (unsigned long) stream
->stream_handle
);
1093 lttng_ht_add_unique_ulong(relay_streams_ht
,
1096 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1097 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1098 session
->stream_count
++;
1100 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1101 stream
->stream_handle
);
1104 reply
.handle
= htobe64(stream
->stream_handle
);
1105 /* send the session id to the client or a negative return code on error */
1107 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1108 /* stream was not properly added to the ht, so free it */
1111 reply
.ret_code
= htobe32(LTTNG_OK
);
1114 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1115 sizeof(struct lttcomm_relayd_status_stream
), 0);
1117 ERR("Relay sending stream id");
1126 free(stream
->path_name
);
1127 free(stream
->channel_name
);
1133 * relay_close_stream: close a specific stream
1136 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1137 struct relay_command
*cmd
)
1140 struct relay_session
*session
= cmd
->session
;
1141 struct lttcomm_relayd_close_stream stream_info
;
1142 struct lttcomm_relayd_generic_reply reply
;
1143 struct relay_stream
*stream
;
1145 DBG("Close stream received");
1147 if (!session
|| cmd
->version_check_done
== 0) {
1148 ERR("Trying to close a stream before version check");
1150 goto end_no_session
;
1153 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1154 sizeof(struct lttcomm_relayd_close_stream
), 0);
1155 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1157 /* Orderly shutdown. Not necessary to print an error. */
1158 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1160 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1163 goto end_no_session
;
1167 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1173 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1174 stream
->close_flag
= 1;
1175 session
->stream_count
--;
1176 assert(session
->stream_count
>= 0);
1178 if (close_stream_check(stream
)) {
1179 destroy_stream(stream
);
1186 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1188 reply
.ret_code
= htobe32(LTTNG_OK
);
1190 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1191 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1193 ERR("Relay sending stream id");
1202 * relay_unknown_command: send -1 if received unknown command
1205 void relay_unknown_command(struct relay_command
*cmd
)
1207 struct lttcomm_relayd_generic_reply reply
;
1210 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1211 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1212 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1214 ERR("Relay sending unknown command");
1219 * relay_start: send an acknowledgment to the client to tell if we are
1220 * ready to receive data. We are ready if a session is established.
1223 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1224 struct relay_command
*cmd
)
1226 int ret
= htobe32(LTTNG_OK
);
1227 struct lttcomm_relayd_generic_reply reply
;
1228 struct relay_session
*session
= cmd
->session
;
1231 DBG("Trying to start the streaming without a session established");
1232 ret
= htobe32(LTTNG_ERR_UNK
);
1235 reply
.ret_code
= ret
;
1236 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1237 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1239 ERR("Relay sending start ack");
1246 * Append padding to the file pointed by the file descriptor fd.
1248 static int write_padding_to_file(int fd
, uint32_t size
)
1257 zeros
= zmalloc(size
);
1258 if (zeros
== NULL
) {
1259 PERROR("zmalloc zeros for padding");
1264 ret
= lttng_write(fd
, zeros
, size
);
1266 PERROR("write padding to file");
1276 * relay_recv_metadata: receive the metada for the session.
1279 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1280 struct relay_command
*cmd
)
1282 int ret
= htobe32(LTTNG_OK
);
1284 struct relay_session
*session
= cmd
->session
;
1285 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1286 struct relay_stream
*metadata_stream
;
1287 uint64_t data_size
, payload_size
;
1290 ERR("Metadata sent before version check");
1295 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1296 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1297 ERR("Incorrect data size");
1301 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1303 if (data_buffer_size
< data_size
) {
1304 /* In case the realloc fails, we can free the memory */
1307 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1308 if (!tmp_data_ptr
) {
1309 ERR("Allocating data buffer");
1314 data_buffer
= tmp_data_ptr
;
1315 data_buffer_size
= data_size
;
1317 memset(data_buffer
, 0, data_size
);
1318 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1319 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1320 if (ret
< 0 || ret
!= data_size
) {
1322 /* Orderly shutdown. Not necessary to print an error. */
1323 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1325 ERR("Relay didn't receive the whole metadata");
1330 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1333 metadata_stream
= relay_stream_find_by_id(
1334 be64toh(metadata_struct
->stream_id
));
1335 if (!metadata_stream
) {
1340 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1342 if (size_ret
< payload_size
) {
1343 ERR("Relay error writing metadata on file");
1348 ret
= write_padding_to_file(metadata_stream
->fd
,
1349 be32toh(metadata_struct
->padding_size
));
1353 metadata_stream
->ctf_trace
->metadata_received
+=
1354 payload_size
+ be32toh(metadata_struct
->padding_size
);
1356 DBG2("Relay metadata written");
1365 * relay_send_version: send relayd version number
1368 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1369 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1372 struct lttcomm_relayd_version reply
, msg
;
1376 cmd
->version_check_done
= 1;
1378 /* Get version from the other side. */
1379 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1380 if (ret
< 0 || ret
!= sizeof(msg
)) {
1382 /* Orderly shutdown. Not necessary to print an error. */
1383 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1385 ERR("Relay failed to receive the version values.");
1391 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1392 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1394 /* Major versions must be the same */
1395 if (reply
.major
!= be32toh(msg
.major
)) {
1396 DBG("Incompatible major versions (%u vs %u), deleting session",
1397 reply
.major
, be32toh(msg
.major
));
1398 relay_delete_session(cmd
, sessions_ht
);
1403 cmd
->major
= reply
.major
;
1404 /* We adapt to the lowest compatible version */
1405 if (reply
.minor
<= be32toh(msg
.minor
)) {
1406 cmd
->minor
= reply
.minor
;
1408 cmd
->minor
= be32toh(msg
.minor
);
1411 reply
.major
= htobe32(reply
.major
);
1412 reply
.minor
= htobe32(reply
.minor
);
1413 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1414 sizeof(struct lttcomm_relayd_version
), 0);
1416 ERR("Relay sending version");
1419 DBG("Version check done using protocol %u.%u", cmd
->major
,
1427 * Check for data pending for a given stream id from the session daemon.
1430 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1431 struct relay_command
*cmd
)
1433 struct relay_session
*session
= cmd
->session
;
1434 struct lttcomm_relayd_data_pending msg
;
1435 struct lttcomm_relayd_generic_reply reply
;
1436 struct relay_stream
*stream
;
1438 uint64_t last_net_seq_num
, stream_id
;
1440 DBG("Data pending command received");
1442 if (!session
|| cmd
->version_check_done
== 0) {
1443 ERR("Trying to check for data before version check");
1445 goto end_no_session
;
1448 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1449 if (ret
< sizeof(msg
)) {
1451 /* Orderly shutdown. Not necessary to print an error. */
1452 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1454 ERR("Relay didn't receive valid data_pending struct size : %d",
1458 goto end_no_session
;
1461 stream_id
= be64toh(msg
.stream_id
);
1462 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1465 stream
= relay_stream_find_by_id(stream_id
);
1466 if (stream
== NULL
) {
1471 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1472 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1475 /* Avoid wrapping issue */
1476 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1477 /* Data has in fact been written and is NOT pending */
1480 /* Data still being streamed thus pending */
1484 /* Pending check is now done. */
1485 stream
->data_pending_check_done
= 1;
1490 reply
.ret_code
= htobe32(ret
);
1491 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1493 ERR("Relay data pending ret code failed");
1501 * Wait for the control socket to reach a quiescent state.
1503 * Note that for now, when receiving this command from the session daemon, this
1504 * means that every subsequent commands or data received on the control socket
1505 * has been handled. So, this is why we simply return OK here.
1508 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1509 struct relay_command
*cmd
)
1513 struct relay_stream
*stream
;
1514 struct lttng_ht_iter iter
;
1515 struct lttcomm_relayd_quiescent_control msg
;
1516 struct lttcomm_relayd_generic_reply reply
;
1518 DBG("Checking quiescent state on control socket");
1520 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1521 ERR("Trying to check for data before version check");
1523 goto end_no_session
;
1526 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1527 if (ret
< sizeof(msg
)) {
1529 /* Orderly shutdown. Not necessary to print an error. */
1530 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1532 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1536 goto end_no_session
;
1539 stream_id
= be64toh(msg
.stream_id
);
1542 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1544 if (stream
->stream_handle
== stream_id
) {
1545 stream
->data_pending_check_done
= 1;
1546 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1553 reply
.ret_code
= htobe32(LTTNG_OK
);
1554 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1556 ERR("Relay data quiescent control ret code failed");
1564 * Initialize a data pending command. This means that a client is about to ask
1565 * for data pending for each stream he/she holds. Simply iterate over all
1566 * streams of a session and set the data_pending_check_done flag.
1568 * This command returns to the client a LTTNG_OK code.
1571 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1572 struct relay_command
*cmd
)
1575 struct lttng_ht_iter iter
;
1576 struct lttcomm_relayd_begin_data_pending msg
;
1577 struct lttcomm_relayd_generic_reply reply
;
1578 struct relay_stream
*stream
;
1579 uint64_t session_id
;
1584 DBG("Init streams for data pending");
1586 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1587 ERR("Trying to check for data before version check");
1589 goto end_no_session
;
1592 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1593 if (ret
< sizeof(msg
)) {
1595 /* Orderly shutdown. Not necessary to print an error. */
1596 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1598 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1602 goto end_no_session
;
1605 session_id
= be64toh(msg
.session_id
);
1608 * Iterate over all streams to set the begin data pending flag. For now, the
1609 * streams are indexed by stream handle so we have to iterate over all
1610 * streams to find the one associated with the right session_id.
1613 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1615 if (stream
->session
->id
== session_id
) {
1616 stream
->data_pending_check_done
= 0;
1617 DBG("Set begin data pending flag to stream %" PRIu64
,
1618 stream
->stream_handle
);
1623 /* All good, send back reply. */
1624 reply
.ret_code
= htobe32(LTTNG_OK
);
1626 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1628 ERR("Relay begin data pending send reply failed");
1636 * End data pending command. This will check, for a given session id, if each
1637 * stream associated with it has its data_pending_check_done flag set. If not,
1638 * this means that the client lost track of the stream but the data is still
1639 * being streamed on our side. In this case, we inform the client that data is
1642 * Return to the client if there is data in flight or not with a ret_code.
1645 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1646 struct relay_command
*cmd
)
1649 struct lttng_ht_iter iter
;
1650 struct lttcomm_relayd_end_data_pending msg
;
1651 struct lttcomm_relayd_generic_reply reply
;
1652 struct relay_stream
*stream
;
1653 uint64_t session_id
;
1654 uint32_t is_data_inflight
= 0;
1659 DBG("End data pending command");
1661 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1662 ERR("Trying to check for data before version check");
1664 goto end_no_session
;
1667 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1668 if (ret
< sizeof(msg
)) {
1670 /* Orderly shutdown. Not necessary to print an error. */
1671 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1673 ERR("Relay didn't receive valid end data_pending struct size: %d",
1677 goto end_no_session
;
1680 session_id
= be64toh(msg
.session_id
);
1682 /* Iterate over all streams to see if the begin data pending flag is set. */
1684 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1686 if (stream
->session
->id
== session_id
&&
1687 !stream
->data_pending_check_done
) {
1688 is_data_inflight
= 1;
1689 DBG("Data is still in flight for stream %" PRIu64
,
1690 stream
->stream_handle
);
1696 /* All good, send back reply. */
1697 reply
.ret_code
= htobe32(is_data_inflight
);
1699 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1701 ERR("Relay end data pending send reply failed");
1709 * Receive an index for a specific stream.
1711 * Return 0 on success else a negative value.
1714 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1715 struct relay_command
*cmd
)
1717 int ret
, send_ret
, index_created
= 0;
1718 struct relay_session
*session
= cmd
->session
;
1719 struct lttcomm_relayd_index index_info
;
1720 struct relay_index
*index
, *wr_index
= NULL
;
1721 struct lttcomm_relayd_generic_reply reply
;
1722 struct relay_stream
*stream
;
1723 uint64_t net_seq_num
;
1727 DBG("Relay receiving index");
1729 if (!session
|| cmd
->version_check_done
== 0) {
1730 ERR("Trying to close a stream before version check");
1732 goto end_no_session
;
1735 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1736 sizeof(index_info
), 0);
1737 if (ret
< sizeof(index_info
)) {
1739 /* Orderly shutdown. Not necessary to print an error. */
1740 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1742 ERR("Relay didn't receive valid index struct size : %d", ret
);
1745 goto end_no_session
;
1748 net_seq_num
= be64toh(index_info
.net_seq_num
);
1751 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1754 goto end_rcu_unlock
;
1757 /* Live beacon handling */
1758 if (index_info
.packet_size
== 0) {
1759 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1762 * Only flag a stream inactive when it has already received data.
1764 if (stream
->total_index_received
> 0) {
1765 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1768 goto end_rcu_unlock
;
1770 stream
->beacon_ts_end
= -1ULL;
1773 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1775 /* A successful creation will add the object to the HT. */
1776 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1778 goto end_rcu_unlock
;
1783 copy_index_control_data(index
, &index_info
);
1785 if (index_created
) {
1787 * Try to add the relay index object to the hash table. If an object
1788 * already exist, destroy back the index created, set the data in this
1789 * object and write it on disk.
1791 relay_index_add(index
, &wr_index
);
1793 copy_index_control_data(wr_index
, &index_info
);
1797 /* The index already exists so write it on disk. */
1801 /* Do we have a writable ready index to write on disk. */
1803 /* Starting at 2.4, create the index file if none available. */
1804 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1805 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1806 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1807 stream
->tracefile_count_current
);
1809 goto end_rcu_unlock
;
1811 stream
->index_fd
= ret
;
1814 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1816 goto end_rcu_unlock
;
1818 stream
->total_index_received
++;
1825 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1827 reply
.ret_code
= htobe32(LTTNG_OK
);
1829 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1831 ERR("Relay sending close index id reply");
1840 * Process the commands received on the control socket
1843 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1844 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
1848 switch (be32toh(recv_hdr
->cmd
)) {
1849 case RELAYD_CREATE_SESSION
:
1850 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
1852 case RELAYD_ADD_STREAM
:
1853 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
1855 case RELAYD_START_DATA
:
1856 ret
= relay_start(recv_hdr
, cmd
);
1858 case RELAYD_SEND_METADATA
:
1859 ret
= relay_recv_metadata(recv_hdr
, cmd
);
1861 case RELAYD_VERSION
:
1862 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
1864 case RELAYD_CLOSE_STREAM
:
1865 ret
= relay_close_stream(recv_hdr
, cmd
);
1867 case RELAYD_DATA_PENDING
:
1868 ret
= relay_data_pending(recv_hdr
, cmd
);
1870 case RELAYD_QUIESCENT_CONTROL
:
1871 ret
= relay_quiescent_control(recv_hdr
, cmd
);
1873 case RELAYD_BEGIN_DATA_PENDING
:
1874 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
1876 case RELAYD_END_DATA_PENDING
:
1877 ret
= relay_end_data_pending(recv_hdr
, cmd
);
1879 case RELAYD_SEND_INDEX
:
1880 ret
= relay_recv_index(recv_hdr
, cmd
);
1882 case RELAYD_UPDATE_SYNC_INFO
:
1884 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1885 relay_unknown_command(cmd
);
1895 * Handle index for a data stream.
1897 * RCU read side lock MUST be acquired.
1899 * Return 0 on success else a negative value.
1901 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
1904 int ret
= 0, index_created
= 0;
1905 uint64_t stream_id
, data_offset
;
1906 struct relay_index
*index
, *wr_index
= NULL
;
1910 stream_id
= stream
->stream_handle
;
1911 /* Get data offset because we are about to update the index. */
1912 data_offset
= htobe64(stream
->tracefile_size_current
);
1915 * Lookup for an existing index for that stream id/sequence number. If on
1916 * exists, the control thread already received the data for it thus we need
1917 * to write it on disk.
1919 index
= relay_index_find(stream_id
, net_seq_num
);
1921 /* A successful creation will add the object to the HT. */
1922 index
= relay_index_create(stream_id
, net_seq_num
);
1930 if (rotate_index
|| stream
->index_fd
< 0) {
1931 index
->to_close_fd
= stream
->index_fd
;
1932 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1933 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1934 stream
->tracefile_count_current
);
1936 /* This will close the stream's index fd if one. */
1937 relay_index_free_safe(index
);
1940 stream
->index_fd
= ret
;
1942 index
->fd
= stream
->index_fd
;
1943 index
->index_data
.offset
= data_offset
;
1945 if (index_created
) {
1947 * Try to add the relay index object to the hash table. If an object
1948 * already exist, destroy back the index created and set the data.
1950 relay_index_add(index
, &wr_index
);
1952 /* Copy back data from the created index. */
1953 wr_index
->fd
= index
->fd
;
1954 wr_index
->to_close_fd
= index
->to_close_fd
;
1955 wr_index
->index_data
.offset
= data_offset
;
1959 /* The index already exists so write it on disk. */
1963 /* Do we have a writable ready index to write on disk. */
1965 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1969 stream
->total_index_received
++;
1977 * relay_process_data: Process the data received on the data socket
1980 int relay_process_data(struct relay_command
*cmd
)
1982 int ret
= 0, rotate_index
= 0;
1984 struct relay_stream
*stream
;
1985 struct lttcomm_relayd_data_hdr data_hdr
;
1987 uint64_t net_seq_num
;
1990 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1991 sizeof(struct lttcomm_relayd_data_hdr
), 0);
1994 /* Orderly shutdown. Not necessary to print an error. */
1995 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1997 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2003 stream_id
= be64toh(data_hdr
.stream_id
);
2006 stream
= relay_stream_find_by_id(stream_id
);
2009 goto end_rcu_unlock
;
2012 data_size
= be32toh(data_hdr
.data_size
);
2013 if (data_buffer_size
< data_size
) {
2016 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2017 if (!tmp_data_ptr
) {
2018 ERR("Allocating data buffer");
2021 goto end_rcu_unlock
;
2023 data_buffer
= tmp_data_ptr
;
2024 data_buffer_size
= data_size
;
2026 memset(data_buffer
, 0, data_size
);
2028 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2030 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2031 data_size
, stream_id
, net_seq_num
);
2032 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2035 /* Orderly shutdown. Not necessary to print an error. */
2036 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2039 goto end_rcu_unlock
;
2042 /* Check if a rotation is needed. */
2043 if (stream
->tracefile_size
> 0 &&
2044 (stream
->tracefile_size_current
+ data_size
) >
2045 stream
->tracefile_size
) {
2046 struct relay_viewer_stream
*vstream
;
2049 new_id
= (stream
->tracefile_count_current
+ 1) %
2050 stream
->tracefile_count
;
2052 * When we wrap-around back to 0, we start overwriting old
2055 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2056 stream
->tracefile_overwrite
= 1;
2058 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2059 if (stream
->tracefile_overwrite
) {
2060 stream
->oldest_tracefile_id
=
2061 (stream
->oldest_tracefile_id
+ 1) %
2062 stream
->tracefile_count
;
2064 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
2067 * The viewer is reading a file about to be
2068 * overwritten. Close the FDs it is
2069 * currently using and let it handle the fault.
2071 if (vstream
->tracefile_count_current
== new_id
) {
2072 pthread_mutex_lock(&vstream
->overwrite_lock
);
2073 vstream
->abort_flag
= 1;
2074 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2075 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2076 stream
->channel_name
, new_id
);
2077 } else if (vstream
->tracefile_count_current
==
2078 stream
->tracefile_count_current
) {
2080 * The reader and writer were in the
2081 * same trace file, inform the viewer
2082 * that no new index will ever be added
2085 vstream
->close_write_flag
= 1;
2088 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2089 stream
->tracefile_size
, stream
->tracefile_count
,
2090 relayd_uid
, relayd_gid
, stream
->fd
,
2091 &(stream
->tracefile_count_current
), &stream
->fd
);
2092 stream
->total_index_received
= 0;
2093 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2095 ERR("Rotating stream output file");
2096 goto end_rcu_unlock
;
2098 /* Reset current size because we just perform a stream rotation. */
2099 stream
->tracefile_size_current
= 0;
2104 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2105 * index are NOT supported.
2107 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2108 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2110 goto end_rcu_unlock
;
2114 /* Write data to stream output fd. */
2115 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2116 if (size_ret
< data_size
) {
2117 ERR("Relay error writing data to file");
2119 goto end_rcu_unlock
;
2122 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2123 ret
, stream
->stream_handle
);
2125 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2127 goto end_rcu_unlock
;
2129 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2131 stream
->prev_seq
= net_seq_num
;
2133 /* Check if we need to close the FD */
2134 if (close_stream_check(stream
)) {
2135 destroy_stream(stream
);
2145 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2149 lttng_poll_del(events
, pollfd
);
2151 ret
= close(pollfd
);
2153 ERR("Closing pollfd %d", pollfd
);
2158 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2159 struct lttng_ht
*relay_connections_ht
)
2161 struct relay_command
*relay_connection
;
2164 relay_connection
= zmalloc(sizeof(struct relay_command
));
2165 if (relay_connection
== NULL
) {
2166 PERROR("Relay command zmalloc");
2169 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2170 if (ret
< sizeof(struct relay_command
)) {
2171 PERROR("read relay cmd pipe");
2176 * Only used by the control side and the reference is copied inside each
2177 * stream from that connection. Thus a destroy HT must be done after every
2178 * stream has been destroyed.
2180 if (relay_connection
->type
== RELAY_CONTROL
) {
2181 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2182 LTTNG_HT_TYPE_STRING
);
2183 if (!relay_connection
->ctf_traces_ht
) {
2188 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2189 (unsigned long) relay_connection
->sock
->fd
);
2191 lttng_ht_add_unique_ulong(relay_connections_ht
,
2192 &relay_connection
->sock_n
);
2194 return lttng_poll_add(events
,
2195 relay_connection
->sock
->fd
,
2196 LPOLLIN
| LPOLLRDHUP
);
2199 free(relay_connection
);
2205 void deferred_free_connection(struct rcu_head
*head
)
2207 struct relay_command
*relay_connection
=
2208 caa_container_of(head
, struct relay_command
, rcu_node
);
2210 lttcomm_destroy_sock(relay_connection
->sock
);
2211 free(relay_connection
);
2215 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2216 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2217 struct lttng_ht
*sessions_ht
)
2221 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2224 if (relay_connection
->type
== RELAY_CONTROL
) {
2225 relay_delete_session(relay_connection
, sessions_ht
);
2226 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2229 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2233 * This thread does the actual work
2236 void *relay_thread_worker(void *data
)
2238 int ret
, err
= -1, last_seen_data_fd
= -1;
2240 struct relay_command
*relay_connection
;
2241 struct lttng_poll_event events
;
2242 struct lttng_ht
*relay_connections_ht
;
2243 struct lttng_ht_node_ulong
*node
;
2244 struct lttng_ht_iter iter
;
2245 struct lttcomm_relayd_hdr recv_hdr
;
2246 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2247 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2249 DBG("[thread] Relay worker started");
2251 rcu_register_thread();
2253 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2255 health_code_update();
2257 /* table of connections indexed on socket */
2258 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2259 if (!relay_connections_ht
) {
2260 goto relay_connections_ht_error
;
2263 /* Tables of received indexes indexed by index handle and net_seq_num. */
2264 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2266 goto indexes_ht_error
;
2269 ret
= create_thread_poll_set(&events
, 2);
2271 goto error_poll_create
;
2274 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2281 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2283 health_code_update();
2285 /* Infinite blocking call, waiting for transmission */
2286 DBG3("Relayd worker thread polling...");
2287 health_poll_entry();
2288 ret
= lttng_poll_wait(&events
, -1);
2292 * Restart interrupted system call.
2294 if (errno
== EINTR
) {
2303 * Process control. The control connection is prioritised so we don't
2304 * starve it with high throughout put tracing data on the data
2307 for (i
= 0; i
< nb_fd
; i
++) {
2308 /* Fetch once the poll data */
2309 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2310 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2312 health_code_update();
2314 /* Thread quit pipe has been closed. Killing thread. */
2315 ret
= check_thread_quit_pipe(pollfd
, revents
);
2321 /* Inspect the relay cmd pipe for new connection */
2322 if (pollfd
== relay_cmd_pipe
[0]) {
2323 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2324 ERR("Relay pipe error");
2326 } else if (revents
& LPOLLIN
) {
2327 DBG("Relay command received");
2328 ret
= relay_add_connection(relay_cmd_pipe
[0],
2329 &events
, relay_connections_ht
);
2334 } else if (revents
) {
2336 lttng_ht_lookup(relay_connections_ht
,
2337 (void *)((unsigned long) pollfd
),
2339 node
= lttng_ht_iter_get_node_ulong(&iter
);
2341 DBG2("Relay sock %d not found", pollfd
);
2345 relay_connection
= caa_container_of(node
,
2346 struct relay_command
, sock_n
);
2348 if (revents
& (LPOLLERR
)) {
2350 relay_cleanup_poll_connection(&events
, pollfd
);
2351 relay_del_connection(relay_connections_ht
,
2352 &iter
, relay_connection
, sessions_ht
);
2353 if (last_seen_data_fd
== pollfd
) {
2354 last_seen_data_fd
= last_notdel_data_fd
;
2356 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2357 DBG("Socket %d hung up", pollfd
);
2358 relay_cleanup_poll_connection(&events
, pollfd
);
2359 relay_del_connection(relay_connections_ht
,
2360 &iter
, relay_connection
, sessions_ht
);
2361 if (last_seen_data_fd
== pollfd
) {
2362 last_seen_data_fd
= last_notdel_data_fd
;
2364 } else if (revents
& LPOLLIN
) {
2365 /* control socket */
2366 if (relay_connection
->type
== RELAY_CONTROL
) {
2367 ret
= relay_connection
->sock
->ops
->recvmsg(
2368 relay_connection
->sock
, &recv_hdr
,
2369 sizeof(struct lttcomm_relayd_hdr
), 0);
2370 /* connection closed */
2372 relay_cleanup_poll_connection(&events
, pollfd
);
2373 relay_del_connection(relay_connections_ht
,
2374 &iter
, relay_connection
, sessions_ht
);
2375 DBG("Control connection closed with %d", pollfd
);
2377 if (relay_connection
->session
) {
2378 DBG2("Relay worker receiving data for session : %" PRIu64
,
2379 relay_connection
->session
->id
);
2381 ret
= relay_process_control(&recv_hdr
,
2382 relay_connection
, relay_ctx
);
2384 /* Clear the session on error. */
2385 relay_cleanup_poll_connection(&events
, pollfd
);
2386 relay_del_connection(relay_connections_ht
,
2387 &iter
, relay_connection
, sessions_ht
);
2388 DBG("Connection closed with %d", pollfd
);
2394 * Flag the last seen data fd not deleted. It will be
2395 * used as the last seen fd if any fd gets deleted in
2398 last_notdel_data_fd
= pollfd
;
2406 * The last loop handled a control request, go back to poll to make
2407 * sure we prioritise the control socket.
2413 if (last_seen_data_fd
>= 0) {
2414 for (i
= 0; i
< nb_fd
; i
++) {
2415 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2417 health_code_update();
2419 if (last_seen_data_fd
== pollfd
) {
2426 /* Process data connection. */
2427 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2428 /* Fetch the poll data. */
2429 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2430 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2432 health_code_update();
2434 /* Skip the command pipe. It's handled in the first loop. */
2435 if (pollfd
== relay_cmd_pipe
[0]) {
2441 lttng_ht_lookup(relay_connections_ht
,
2442 (void *)((unsigned long) pollfd
),
2444 node
= lttng_ht_iter_get_node_ulong(&iter
);
2446 /* Skip it. Might be removed before. */
2450 relay_connection
= caa_container_of(node
,
2451 struct relay_command
, sock_n
);
2453 if (revents
& LPOLLIN
) {
2454 if (relay_connection
->type
!= RELAY_DATA
) {
2458 ret
= relay_process_data(relay_connection
);
2459 /* connection closed */
2461 relay_cleanup_poll_connection(&events
, pollfd
);
2462 relay_del_connection(relay_connections_ht
,
2463 &iter
, relay_connection
, sessions_ht
);
2464 DBG("Data connection closed with %d", pollfd
);
2466 * Every goto restart call sets the last seen fd where
2467 * here we don't really care since we gracefully
2468 * continue the loop after the connection is deleted.
2471 /* Keep last seen port. */
2472 last_seen_data_fd
= pollfd
;
2480 last_seen_data_fd
= -1;
2483 /* Normal exit, no error */
2488 lttng_poll_clean(&events
);
2490 /* empty the hash table and free the memory */
2492 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2493 health_code_update();
2495 node
= lttng_ht_iter_get_node_ulong(&iter
);
2497 relay_connection
= caa_container_of(node
,
2498 struct relay_command
, sock_n
);
2499 relay_del_connection(relay_connections_ht
,
2500 &iter
, relay_connection
, sessions_ht
);
2505 lttng_ht_destroy(indexes_ht
);
2507 lttng_ht_destroy(relay_connections_ht
);
2508 relay_connections_ht_error
:
2509 /* Close relay cmd pipes */
2510 utils_close_pipe(relay_cmd_pipe
);
2512 DBG("Thread exited with error");
2514 DBG("Worker thread cleanup complete");
2518 ERR("Health error occurred in %s", __func__
);
2520 health_unregister(health_relayd
);
2521 rcu_unregister_thread();
2527 * Create the relay command pipe to wake thread_manage_apps.
2528 * Closed in cleanup().
2530 static int create_relay_cmd_pipe(void)
2534 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2542 int main(int argc
, char **argv
)
2546 struct relay_local_data
*relay_ctx
;
2548 /* Create thread quit pipe */
2549 if ((ret
= init_thread_quit_pipe()) < 0) {
2553 /* Parse arguments */
2555 if ((ret
= parse_args(argc
, argv
)) < 0) {
2559 if ((ret
= set_signal_handler()) < 0) {
2563 /* Try to create directory if -o, --output is specified. */
2564 if (opt_output_path
) {
2565 if (*opt_output_path
!= '/') {
2566 ERR("Please specify an absolute path for -o, --output PATH");
2570 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2572 ERR("Unable to create %s", opt_output_path
);
2586 /* We need those values for the file/dir creation. */
2587 relayd_uid
= getuid();
2588 relayd_gid
= getgid();
2590 /* Check if daemon is UID = 0 */
2591 if (relayd_uid
== 0) {
2592 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
2593 ERR("Need to be root to use ports < 1024");
2599 /* Setup the thread apps communication pipe. */
2600 if ((ret
= create_relay_cmd_pipe()) < 0) {
2604 /* Init relay command queue. */
2605 cds_wfq_init(&relay_cmd_queue
.queue
);
2607 /* Set up max poll set size */
2608 lttng_poll_set_max_size();
2610 /* Initialize communication library */
2613 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2615 PERROR("relay_ctx");
2619 /* tables of sessions indexed by session ID */
2620 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2621 if (!relay_ctx
->sessions_ht
) {
2622 goto exit_relay_ctx_sessions
;
2625 /* tables of streams indexed by stream ID */
2626 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2627 if (!relay_streams_ht
) {
2628 goto exit_relay_ctx_streams
;
2631 /* tables of streams indexed by stream ID */
2632 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2633 if (!viewer_streams_ht
) {
2634 goto exit_relay_ctx_viewer_streams
;
2637 /* Initialize thread health monitoring */
2638 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2639 if (!health_relayd
) {
2640 PERROR("health_app_create error");
2641 goto exit_health_app_create
;
2644 ret
= utils_create_pipe(health_quit_pipe
);
2646 goto error_health_pipe
;
2649 /* Create thread to manage the client socket */
2650 ret
= pthread_create(&health_thread
, NULL
,
2651 thread_manage_health
, (void *) NULL
);
2653 PERROR("pthread_create health");
2657 /* Setup the dispatcher thread */
2658 ret
= pthread_create(&dispatcher_thread
, NULL
,
2659 relay_thread_dispatcher
, (void *) NULL
);
2661 PERROR("pthread_create dispatcher");
2662 goto exit_dispatcher
;
2665 /* Setup the worker thread */
2666 ret
= pthread_create(&worker_thread
, NULL
,
2667 relay_thread_worker
, (void *) relay_ctx
);
2669 PERROR("pthread_create worker");
2673 /* Setup the listener thread */
2674 ret
= pthread_create(&listener_thread
, NULL
,
2675 relay_thread_listener
, (void *) NULL
);
2677 PERROR("pthread_create listener");
2681 ret
= live_start_threads(live_uri
, relay_ctx
, thread_quit_pipe
);
2683 ERR("Starting live viewer threads");
2688 ret
= pthread_join(listener_thread
, &status
);
2690 PERROR("pthread_join");
2691 goto error
; /* join error, exit without cleanup */
2695 ret
= pthread_join(worker_thread
, &status
);
2697 PERROR("pthread_join");
2698 goto error
; /* join error, exit without cleanup */
2702 ret
= pthread_join(dispatcher_thread
, &status
);
2704 PERROR("pthread_join");
2705 goto error
; /* join error, exit without cleanup */
2709 ret
= pthread_join(health_thread
, &status
);
2711 PERROR("pthread_join health thread");
2712 goto error
; /* join error, exit without cleanup */
2716 * Stop live threads only after joining other threads.
2718 live_stop_threads();
2721 utils_close_pipe(health_quit_pipe
);
2724 health_app_destroy(health_relayd
);
2726 exit_health_app_create
:
2727 lttng_ht_destroy(viewer_streams_ht
);
2729 exit_relay_ctx_viewer_streams
:
2730 lttng_ht_destroy(relay_streams_ht
);
2732 exit_relay_ctx_streams
:
2733 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2735 exit_relay_ctx_sessions
: