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
, " -L, --live-port URL Live view port listening.\n");
136 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
137 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
138 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
142 int parse_args(int argc
, char **argv
)
146 char *default_address
;
148 static struct option long_options
[] = {
149 { "control-port", 1, 0, 'C', },
150 { "data-port", 1, 0, 'D', },
151 { "daemonize", 0, 0, 'd', },
152 { "group", 1, 0, 'g', },
153 { "help", 0, 0, 'h', },
154 { "output", 1, 0, 'o', },
155 { "verbose", 0, 0, 'v', },
160 int option_index
= 0;
161 c
= getopt_long(argc
, argv
, "dhv" "C:D:L:o:g:",
162 long_options
, &option_index
);
169 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
171 fprintf(stderr
, " with arg %s\n", optarg
);
175 ret
= uri_parse(optarg
, &control_uri
);
177 ERR("Invalid control URI specified");
180 if (control_uri
->port
== 0) {
181 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
185 ret
= uri_parse(optarg
, &data_uri
);
187 ERR("Invalid data URI specified");
190 if (data_uri
->port
== 0) {
191 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
195 ret
= uri_parse(optarg
, &live_uri
);
197 ERR("Invalid live URI specified");
200 if (live_uri
->port
== 0) {
201 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
208 tracing_group_name
= optarg
;
214 ret
= asprintf(&opt_output_path
, "%s", optarg
);
216 PERROR("asprintf opt_output_path");
221 /* Verbose level can increase using multiple -v */
222 lttng_opt_verbose
+= 1;
225 /* Unknown option or other error.
226 * Error is printed by getopt, just return */
232 /* assign default values */
233 if (control_uri
== NULL
) {
234 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
235 DEFAULT_NETWORK_CONTROL_PORT
);
237 PERROR("asprintf default data address");
241 ret
= uri_parse(default_address
, &control_uri
);
242 free(default_address
);
244 ERR("Invalid control URI specified");
248 if (data_uri
== NULL
) {
249 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
250 DEFAULT_NETWORK_DATA_PORT
);
252 PERROR("asprintf default data address");
256 ret
= uri_parse(default_address
, &data_uri
);
257 free(default_address
);
259 ERR("Invalid data URI specified");
263 if (live_uri
== NULL
) {
264 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
265 DEFAULT_NETWORK_VIEWER_PORT
);
267 PERROR("asprintf default viewer control address");
271 ret
= uri_parse(default_address
, &live_uri
);
272 free(default_address
);
274 ERR("Invalid viewer control URI specified");
291 /* free the dynamically allocated opt_output_path */
292 free(opt_output_path
);
294 /* Close thread quit pipes */
295 utils_close_pipe(thread_quit_pipe
);
297 uri_free(control_uri
);
299 /* Live URI is freed in the live thread. */
303 * Write to writable pipe used to notify a thread.
306 int notify_thread_pipe(int wpipe
)
310 ret
= lttng_write(wpipe
, "!", 1);
312 PERROR("write poll pipe");
318 static void notify_health_quit_pipe(int *pipe
)
322 ret
= lttng_write(pipe
[1], "4", 1);
324 PERROR("write relay health quit");
329 * Stop all threads by closing the thread quit pipe.
332 void stop_threads(void)
336 /* Stopping all threads */
337 DBG("Terminating all threads");
338 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
340 ERR("write error on thread quit pipe");
343 notify_health_quit_pipe(health_quit_pipe
);
345 /* Dispatch thread */
346 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
347 futex_nto1_wake(&relay_cmd_queue
.futex
);
351 * Signal handler for the daemon
353 * Simply stop all worker threads, leaving main() return gracefully after
354 * joining all threads and calling cleanup().
357 void sighandler(int sig
)
361 DBG("SIGPIPE caught");
364 DBG("SIGINT caught");
368 DBG("SIGTERM caught");
377 * Setup signal handler for :
378 * SIGINT, SIGTERM, SIGPIPE
381 int set_signal_handler(void)
387 if ((ret
= sigemptyset(&sigset
)) < 0) {
388 PERROR("sigemptyset");
392 sa
.sa_handler
= sighandler
;
395 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
400 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
405 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
410 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
416 * Init thread quit pipe.
418 * Return -1 on error or 0 if all pipes are created.
421 int init_thread_quit_pipe(void)
425 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
431 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
434 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
438 if (events
== NULL
|| size
== 0) {
443 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
449 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
461 * Check if the thread quit pipe was triggered.
463 * Return 1 if it was triggered else 0;
466 int check_thread_quit_pipe(int fd
, uint32_t events
)
468 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
476 * Create and init socket from uri.
479 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
482 struct lttcomm_sock
*sock
= NULL
;
484 sock
= lttcomm_alloc_sock_from_uri(uri
);
486 ERR("Allocating socket");
490 ret
= lttcomm_create_sock(sock
);
494 DBG("Listening on sock %d", sock
->fd
);
496 ret
= sock
->ops
->bind(sock
);
501 ret
= sock
->ops
->listen(sock
, -1);
511 lttcomm_destroy_sock(sock
);
517 * Return nonzero if stream needs to be closed.
520 int close_stream_check(struct relay_stream
*stream
)
523 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
525 * We are about to close the stream so set the data pending flag to 1
526 * which will make the end data pending command skip the stream which
527 * is now closed and ready. Note that after proceeding to a file close,
528 * the written file is ready for reading.
530 stream
->data_pending_check_done
= 1;
537 * This thread manages the listening for new connections on the network
540 void *relay_thread_listener(void *data
)
542 int i
, ret
, pollfd
, err
= -1;
544 uint32_t revents
, nb_fd
;
545 struct lttng_poll_event events
;
546 struct lttcomm_sock
*control_sock
, *data_sock
;
548 DBG("[thread] Relay listener started");
550 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
552 health_code_update();
554 control_sock
= relay_init_sock(control_uri
);
556 goto error_sock_control
;
559 data_sock
= relay_init_sock(data_uri
);
561 goto error_sock_relay
;
565 * Pass 3 as size here for the thread quit pipe, control and data socket.
567 ret
= create_thread_poll_set(&events
, 3);
569 goto error_create_poll
;
572 /* Add the control socket */
573 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
578 /* Add the data socket */
579 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
585 health_code_update();
587 DBG("Listener accepting connections");
591 ret
= lttng_poll_wait(&events
, -1);
595 * Restart interrupted system call.
597 if (errno
== EINTR
) {
605 DBG("Relay new connection received");
606 for (i
= 0; i
< nb_fd
; i
++) {
607 health_code_update();
609 /* Fetch once the poll data */
610 revents
= LTTNG_POLL_GETEV(&events
, i
);
611 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
613 /* Thread quit pipe has been closed. Killing thread. */
614 ret
= check_thread_quit_pipe(pollfd
, revents
);
620 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
621 ERR("socket poll error");
623 } else if (revents
& LPOLLIN
) {
625 * Get allocated in this thread,
626 * enqueued to a global queue, dequeued
627 * and freed in the worker thread.
629 struct relay_command
*relay_cmd
;
630 struct lttcomm_sock
*newsock
;
632 relay_cmd
= zmalloc(sizeof(struct relay_command
));
633 if (relay_cmd
== NULL
) {
634 PERROR("relay command zmalloc");
638 if (pollfd
== data_sock
->fd
) {
639 newsock
= data_sock
->ops
->accept(data_sock
);
641 PERROR("accepting data sock");
645 relay_cmd
->type
= RELAY_DATA
;
646 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
648 assert(pollfd
== control_sock
->fd
);
649 newsock
= control_sock
->ops
->accept(control_sock
);
651 PERROR("accepting control sock");
655 relay_cmd
->type
= RELAY_CONTROL
;
656 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
658 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
661 PERROR("setsockopt inet");
662 lttcomm_destroy_sock(newsock
);
666 relay_cmd
->sock
= newsock
;
668 * Lock free enqueue the request.
670 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
673 * Wake the dispatch queue futex. Implicit memory
674 * barrier with the exchange in cds_wfq_enqueue.
676 futex_nto1_wake(&relay_cmd_queue
.futex
);
684 lttng_poll_clean(&events
);
686 if (data_sock
->fd
>= 0) {
687 ret
= data_sock
->ops
->close(data_sock
);
692 lttcomm_destroy_sock(data_sock
);
694 if (control_sock
->fd
>= 0) {
695 ret
= control_sock
->ops
->close(control_sock
);
700 lttcomm_destroy_sock(control_sock
);
704 ERR("Health error occurred in %s", __func__
);
706 health_unregister(health_relayd
);
707 DBG("Relay listener thread cleanup complete");
713 * This thread manages the dispatching of the requests to worker threads
716 void *relay_thread_dispatcher(void *data
)
720 struct cds_wfq_node
*node
;
721 struct relay_command
*relay_cmd
= NULL
;
723 DBG("[thread] Relay dispatcher started");
725 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
727 health_code_update();
729 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
730 health_code_update();
732 /* Atomically prepare the queue futex */
733 futex_nto1_prepare(&relay_cmd_queue
.futex
);
736 health_code_update();
738 /* Dequeue commands */
739 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
741 DBG("Woken up but nothing in the relay command queue");
742 /* Continue thread execution */
746 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
747 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
750 * Inform worker thread of the new request. This
751 * call is blocking so we can be assured that the data will be read
752 * at some point in time or wait to the end of the world :)
754 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
755 sizeof(struct relay_command
));
757 if (ret
< sizeof(struct relay_command
)) {
758 PERROR("write cmd pipe");
761 } while (node
!= NULL
);
763 /* Futex wait on queue. Blocking call on futex() */
765 futex_nto1_wait(&relay_cmd_queue
.futex
);
769 /* Normal exit, no error */
775 ERR("Health error occurred in %s", __func__
);
777 health_unregister(health_relayd
);
778 DBG("Dispatch thread dying");
784 * Get stream from stream id.
785 * Need to be called with RCU read-side lock held.
787 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
789 struct lttng_ht_node_ulong
*node
;
790 struct lttng_ht_iter iter
;
791 struct relay_stream
*ret
;
793 lttng_ht_lookup(relay_streams_ht
,
794 (void *)((unsigned long) stream_id
),
796 node
= lttng_ht_iter_get_node_ulong(&iter
);
798 DBG("Relay stream %" PRIu64
" not found", stream_id
);
803 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
810 void deferred_free_stream(struct rcu_head
*head
)
812 struct relay_stream
*stream
=
813 caa_container_of(head
, struct relay_stream
, rcu_node
);
815 free(stream
->path_name
);
816 free(stream
->channel_name
);
821 void deferred_free_session(struct rcu_head
*head
)
823 struct relay_session
*session
=
824 caa_container_of(head
, struct relay_session
, rcu_node
);
829 * Close a given stream. The stream is freed using a call RCU.
831 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
832 * BEFORE the stream lock MUST be acquired.
834 static void destroy_stream(struct relay_stream
*stream
)
837 struct relay_viewer_stream
*vstream
;
838 struct lttng_ht_iter iter
;
842 delret
= close(stream
->fd
);
844 PERROR("close stream");
847 if (stream
->index_fd
>= 0) {
848 delret
= close(stream
->index_fd
);
850 PERROR("close stream index_fd");
854 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
857 * Set the last good value into the viewer stream. This is done
858 * right before the stream gets deleted from the hash table. The
859 * lookup failure on the live thread side of a stream indicates
860 * that the viewer stream index received value should be used.
862 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
863 vstream
->total_index_received
= stream
->total_index_received
;
864 vstream
->tracefile_count_last
= stream
->tracefile_count_current
;
865 vstream
->close_write_flag
= 1;
866 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
869 /* Cleanup index of that stream. */
870 relay_index_destroy_by_stream_id(stream
->stream_handle
);
872 iter
.iter
.node
= &stream
->stream_n
.node
;
873 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
875 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
876 delret
= lttng_ht_del(stream
->ctf_traces_ht
, &iter
);
879 if (stream
->ctf_trace
) {
880 ctf_trace_try_destroy(stream
->ctf_trace
);
883 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
884 DBG("Closed tracefile %d from close stream", stream
->fd
);
888 * relay_delete_session: Free all memory associated with a session and
892 void relay_delete_session(struct relay_command
*cmd
,
893 struct lttng_ht
*sessions_ht
)
895 struct lttng_ht_iter iter
;
896 struct lttng_ht_node_ulong
*node
;
897 struct relay_stream
*stream
;
904 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
907 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
908 node
= lttng_ht_iter_get_node_ulong(&iter
);
912 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
913 if (stream
->session
== cmd
->session
) {
914 destroy_stream(stream
);
915 cmd
->session
->stream_count
--;
916 assert(cmd
->session
->stream_count
>= 0);
920 /* Make this session not visible anymore. */
921 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
922 ret
= lttng_ht_del(sessions_ht
, &iter
);
924 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
929 * Copy index data from the control port to a given index object.
931 static void copy_index_control_data(struct relay_index
*index
,
932 struct lttcomm_relayd_index
*data
)
938 * The index on disk is encoded in big endian, so we don't need to convert
939 * the data received on the network. The data_offset value is NEVER
940 * modified here and is updated by the data thread.
942 index
->index_data
.packet_size
= data
->packet_size
;
943 index
->index_data
.content_size
= data
->content_size
;
944 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
945 index
->index_data
.timestamp_end
= data
->timestamp_end
;
946 index
->index_data
.events_discarded
= data
->events_discarded
;
947 index
->index_data
.stream_id
= data
->stream_id
;
951 * Handle the RELAYD_CREATE_SESSION command.
953 * On success, send back the session id or else return a negative value.
956 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
957 struct relay_command
*cmd
,
958 struct lttng_ht
*sessions_ht
)
960 int ret
= 0, send_ret
;
961 struct relay_session
*session
;
962 struct lttcomm_relayd_status_session reply
;
967 memset(&reply
, 0, sizeof(reply
));
969 session
= zmalloc(sizeof(struct relay_session
));
970 if (session
== NULL
) {
971 PERROR("relay session zmalloc");
976 session
->id
= ++last_relay_session_id
;
977 session
->sock
= cmd
->sock
;
978 session
->minor
= cmd
->minor
;
979 session
->major
= cmd
->major
;
980 cmd
->session
= session
;
982 reply
.session_id
= htobe64(session
->id
);
984 switch (cmd
->minor
) {
985 case 4: /* LTTng sessiond 2.4 */
987 ret
= cmd_create_session_2_4(cmd
, session
);
991 lttng_ht_node_init_ulong(&session
->session_n
,
992 (unsigned long) session
->id
);
993 lttng_ht_add_unique_ulong(sessions_ht
,
994 &session
->session_n
);
996 DBG("Created session %" PRIu64
, session
->id
);
1000 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
1002 reply
.ret_code
= htobe32(LTTNG_OK
);
1005 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1007 ERR("Relayd sending session id");
1015 * relay_add_stream: allocate a new stream for a session
1018 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1019 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1021 struct relay_session
*session
= cmd
->session
;
1022 struct relay_stream
*stream
= NULL
;
1023 struct lttcomm_relayd_status_stream reply
;
1026 if (!session
|| cmd
->version_check_done
== 0) {
1027 ERR("Trying to add a stream before version check");
1029 goto end_no_session
;
1032 stream
= zmalloc(sizeof(struct relay_stream
));
1033 if (stream
== NULL
) {
1034 PERROR("relay stream zmalloc");
1036 goto end_no_session
;
1039 switch (cmd
->minor
) {
1040 case 1: /* LTTng sessiond 2.1 */
1041 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1043 case 2: /* LTTng sessiond 2.2 */
1045 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1049 goto err_free_stream
;
1053 stream
->stream_handle
= ++last_relay_stream_id
;
1054 stream
->prev_seq
= -1ULL;
1055 stream
->session
= session
;
1056 stream
->index_fd
= -1;
1057 stream
->read_index_fd
= -1;
1058 stream
->ctf_trace
= NULL
;
1059 pthread_mutex_init(&stream
->lock
, NULL
);
1061 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1063 ERR("relay creating output directory");
1068 * No need to use run_as API here because whatever we receives, the relayd
1069 * uses its own credentials for the stream files.
1071 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1072 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1074 ERR("Create output file");
1078 if (stream
->tracefile_size
) {
1079 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1081 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1084 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1085 stream
->metadata_flag
= 1;
1087 * When we receive a new metadata stream, we create a new
1088 * ctf_trace and we assign this ctf_trace to all streams with
1091 * If later on we receive a new stream for the same ctf_trace,
1092 * we copy the information from the first hit in the HT to the
1095 stream
->ctf_trace
= ctf_trace_create();
1096 if (!stream
->ctf_trace
) {
1100 stream
->ctf_trace
->refcount
++;
1101 stream
->ctf_trace
->metadata_stream
= stream
;
1103 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1104 stream
->ctf_traces_ht
= cmd
->ctf_traces_ht
;
1106 lttng_ht_node_init_ulong(&stream
->stream_n
,
1107 (unsigned long) stream
->stream_handle
);
1108 lttng_ht_add_unique_ulong(relay_streams_ht
,
1111 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1112 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1113 session
->stream_count
++;
1115 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1116 stream
->stream_handle
);
1119 reply
.handle
= htobe64(stream
->stream_handle
);
1120 /* send the session id to the client or a negative return code on error */
1122 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1123 /* stream was not properly added to the ht, so free it */
1126 reply
.ret_code
= htobe32(LTTNG_OK
);
1129 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1130 sizeof(struct lttcomm_relayd_status_stream
), 0);
1132 ERR("Relay sending stream id");
1141 free(stream
->path_name
);
1142 free(stream
->channel_name
);
1148 * relay_close_stream: close a specific stream
1151 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1152 struct relay_command
*cmd
)
1155 struct relay_session
*session
= cmd
->session
;
1156 struct lttcomm_relayd_close_stream stream_info
;
1157 struct lttcomm_relayd_generic_reply reply
;
1158 struct relay_stream
*stream
;
1160 DBG("Close stream received");
1162 if (!session
|| cmd
->version_check_done
== 0) {
1163 ERR("Trying to close a stream before version check");
1165 goto end_no_session
;
1168 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1169 sizeof(struct lttcomm_relayd_close_stream
), 0);
1170 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1172 /* Orderly shutdown. Not necessary to print an error. */
1173 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1175 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1178 goto end_no_session
;
1182 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1188 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1189 stream
->close_flag
= 1;
1190 session
->stream_count
--;
1191 assert(session
->stream_count
>= 0);
1193 if (close_stream_check(stream
)) {
1194 destroy_stream(stream
);
1201 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1203 reply
.ret_code
= htobe32(LTTNG_OK
);
1205 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1206 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1208 ERR("Relay sending stream id");
1217 * relay_unknown_command: send -1 if received unknown command
1220 void relay_unknown_command(struct relay_command
*cmd
)
1222 struct lttcomm_relayd_generic_reply reply
;
1225 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1226 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1227 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1229 ERR("Relay sending unknown command");
1234 * relay_start: send an acknowledgment to the client to tell if we are
1235 * ready to receive data. We are ready if a session is established.
1238 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1239 struct relay_command
*cmd
)
1241 int ret
= htobe32(LTTNG_OK
);
1242 struct lttcomm_relayd_generic_reply reply
;
1243 struct relay_session
*session
= cmd
->session
;
1246 DBG("Trying to start the streaming without a session established");
1247 ret
= htobe32(LTTNG_ERR_UNK
);
1250 reply
.ret_code
= ret
;
1251 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1252 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1254 ERR("Relay sending start ack");
1261 * Append padding to the file pointed by the file descriptor fd.
1263 static int write_padding_to_file(int fd
, uint32_t size
)
1272 zeros
= zmalloc(size
);
1273 if (zeros
== NULL
) {
1274 PERROR("zmalloc zeros for padding");
1279 ret
= lttng_write(fd
, zeros
, size
);
1281 PERROR("write padding to file");
1291 * relay_recv_metadata: receive the metada for the session.
1294 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1295 struct relay_command
*cmd
)
1297 int ret
= htobe32(LTTNG_OK
);
1299 struct relay_session
*session
= cmd
->session
;
1300 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1301 struct relay_stream
*metadata_stream
;
1302 uint64_t data_size
, payload_size
;
1305 ERR("Metadata sent before version check");
1310 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1311 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1312 ERR("Incorrect data size");
1316 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1318 if (data_buffer_size
< data_size
) {
1319 /* In case the realloc fails, we can free the memory */
1322 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1323 if (!tmp_data_ptr
) {
1324 ERR("Allocating data buffer");
1329 data_buffer
= tmp_data_ptr
;
1330 data_buffer_size
= data_size
;
1332 memset(data_buffer
, 0, data_size
);
1333 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1334 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1335 if (ret
< 0 || ret
!= data_size
) {
1337 /* Orderly shutdown. Not necessary to print an error. */
1338 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1340 ERR("Relay didn't receive the whole metadata");
1345 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1348 metadata_stream
= relay_stream_find_by_id(
1349 be64toh(metadata_struct
->stream_id
));
1350 if (!metadata_stream
) {
1355 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1357 if (size_ret
< payload_size
) {
1358 ERR("Relay error writing metadata on file");
1363 ret
= write_padding_to_file(metadata_stream
->fd
,
1364 be32toh(metadata_struct
->padding_size
));
1368 metadata_stream
->ctf_trace
->metadata_received
+=
1369 payload_size
+ be32toh(metadata_struct
->padding_size
);
1371 DBG2("Relay metadata written");
1380 * relay_send_version: send relayd version number
1383 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1384 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1387 struct lttcomm_relayd_version reply
, msg
;
1391 cmd
->version_check_done
= 1;
1393 /* Get version from the other side. */
1394 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1395 if (ret
< 0 || ret
!= sizeof(msg
)) {
1397 /* Orderly shutdown. Not necessary to print an error. */
1398 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1400 ERR("Relay failed to receive the version values.");
1406 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1407 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1409 /* Major versions must be the same */
1410 if (reply
.major
!= be32toh(msg
.major
)) {
1411 DBG("Incompatible major versions (%u vs %u), deleting session",
1412 reply
.major
, be32toh(msg
.major
));
1413 relay_delete_session(cmd
, sessions_ht
);
1418 cmd
->major
= reply
.major
;
1419 /* We adapt to the lowest compatible version */
1420 if (reply
.minor
<= be32toh(msg
.minor
)) {
1421 cmd
->minor
= reply
.minor
;
1423 cmd
->minor
= be32toh(msg
.minor
);
1426 reply
.major
= htobe32(reply
.major
);
1427 reply
.minor
= htobe32(reply
.minor
);
1428 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1429 sizeof(struct lttcomm_relayd_version
), 0);
1431 ERR("Relay sending version");
1434 DBG("Version check done using protocol %u.%u", cmd
->major
,
1442 * Check for data pending for a given stream id from the session daemon.
1445 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1446 struct relay_command
*cmd
)
1448 struct relay_session
*session
= cmd
->session
;
1449 struct lttcomm_relayd_data_pending msg
;
1450 struct lttcomm_relayd_generic_reply reply
;
1451 struct relay_stream
*stream
;
1453 uint64_t last_net_seq_num
, stream_id
;
1455 DBG("Data pending command received");
1457 if (!session
|| cmd
->version_check_done
== 0) {
1458 ERR("Trying to check for data before version check");
1460 goto end_no_session
;
1463 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1464 if (ret
< sizeof(msg
)) {
1466 /* Orderly shutdown. Not necessary to print an error. */
1467 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1469 ERR("Relay didn't receive valid data_pending struct size : %d",
1473 goto end_no_session
;
1476 stream_id
= be64toh(msg
.stream_id
);
1477 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1480 stream
= relay_stream_find_by_id(stream_id
);
1481 if (stream
== NULL
) {
1486 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1487 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1490 /* Avoid wrapping issue */
1491 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1492 /* Data has in fact been written and is NOT pending */
1495 /* Data still being streamed thus pending */
1499 /* Pending check is now done. */
1500 stream
->data_pending_check_done
= 1;
1505 reply
.ret_code
= htobe32(ret
);
1506 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1508 ERR("Relay data pending ret code failed");
1516 * Wait for the control socket to reach a quiescent state.
1518 * Note that for now, when receiving this command from the session daemon, this
1519 * means that every subsequent commands or data received on the control socket
1520 * has been handled. So, this is why we simply return OK here.
1523 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1524 struct relay_command
*cmd
)
1528 struct relay_stream
*stream
;
1529 struct lttng_ht_iter iter
;
1530 struct lttcomm_relayd_quiescent_control msg
;
1531 struct lttcomm_relayd_generic_reply reply
;
1533 DBG("Checking quiescent state on control socket");
1535 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1536 ERR("Trying to check for data before version check");
1538 goto end_no_session
;
1541 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1542 if (ret
< sizeof(msg
)) {
1544 /* Orderly shutdown. Not necessary to print an error. */
1545 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1547 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1551 goto end_no_session
;
1554 stream_id
= be64toh(msg
.stream_id
);
1557 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1559 if (stream
->stream_handle
== stream_id
) {
1560 stream
->data_pending_check_done
= 1;
1561 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1568 reply
.ret_code
= htobe32(LTTNG_OK
);
1569 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1571 ERR("Relay data quiescent control ret code failed");
1579 * Initialize a data pending command. This means that a client is about to ask
1580 * for data pending for each stream he/she holds. Simply iterate over all
1581 * streams of a session and set the data_pending_check_done flag.
1583 * This command returns to the client a LTTNG_OK code.
1586 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1587 struct relay_command
*cmd
)
1590 struct lttng_ht_iter iter
;
1591 struct lttcomm_relayd_begin_data_pending msg
;
1592 struct lttcomm_relayd_generic_reply reply
;
1593 struct relay_stream
*stream
;
1594 uint64_t session_id
;
1599 DBG("Init streams for data pending");
1601 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1602 ERR("Trying to check for data before version check");
1604 goto end_no_session
;
1607 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1608 if (ret
< sizeof(msg
)) {
1610 /* Orderly shutdown. Not necessary to print an error. */
1611 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1613 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1617 goto end_no_session
;
1620 session_id
= be64toh(msg
.session_id
);
1623 * Iterate over all streams to set the begin data pending flag. For now, the
1624 * streams are indexed by stream handle so we have to iterate over all
1625 * streams to find the one associated with the right session_id.
1628 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1630 if (stream
->session
->id
== session_id
) {
1631 stream
->data_pending_check_done
= 0;
1632 DBG("Set begin data pending flag to stream %" PRIu64
,
1633 stream
->stream_handle
);
1638 /* All good, send back reply. */
1639 reply
.ret_code
= htobe32(LTTNG_OK
);
1641 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1643 ERR("Relay begin data pending send reply failed");
1651 * End data pending command. This will check, for a given session id, if each
1652 * stream associated with it has its data_pending_check_done flag set. If not,
1653 * this means that the client lost track of the stream but the data is still
1654 * being streamed on our side. In this case, we inform the client that data is
1657 * Return to the client if there is data in flight or not with a ret_code.
1660 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1661 struct relay_command
*cmd
)
1664 struct lttng_ht_iter iter
;
1665 struct lttcomm_relayd_end_data_pending msg
;
1666 struct lttcomm_relayd_generic_reply reply
;
1667 struct relay_stream
*stream
;
1668 uint64_t session_id
;
1669 uint32_t is_data_inflight
= 0;
1674 DBG("End data pending command");
1676 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1677 ERR("Trying to check for data before version check");
1679 goto end_no_session
;
1682 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1683 if (ret
< sizeof(msg
)) {
1685 /* Orderly shutdown. Not necessary to print an error. */
1686 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1688 ERR("Relay didn't receive valid end data_pending struct size: %d",
1692 goto end_no_session
;
1695 session_id
= be64toh(msg
.session_id
);
1697 /* Iterate over all streams to see if the begin data pending flag is set. */
1699 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1701 if (stream
->session
->id
== session_id
&&
1702 !stream
->data_pending_check_done
) {
1703 is_data_inflight
= 1;
1704 DBG("Data is still in flight for stream %" PRIu64
,
1705 stream
->stream_handle
);
1711 /* All good, send back reply. */
1712 reply
.ret_code
= htobe32(is_data_inflight
);
1714 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1716 ERR("Relay end data pending send reply failed");
1724 * Receive an index for a specific stream.
1726 * Return 0 on success else a negative value.
1729 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1730 struct relay_command
*cmd
)
1732 int ret
, send_ret
, index_created
= 0;
1733 struct relay_session
*session
= cmd
->session
;
1734 struct lttcomm_relayd_index index_info
;
1735 struct relay_index
*index
, *wr_index
= NULL
;
1736 struct lttcomm_relayd_generic_reply reply
;
1737 struct relay_stream
*stream
;
1738 uint64_t net_seq_num
;
1742 DBG("Relay receiving index");
1744 if (!session
|| cmd
->version_check_done
== 0) {
1745 ERR("Trying to close a stream before version check");
1747 goto end_no_session
;
1750 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1751 sizeof(index_info
), 0);
1752 if (ret
< sizeof(index_info
)) {
1754 /* Orderly shutdown. Not necessary to print an error. */
1755 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1757 ERR("Relay didn't receive valid index struct size : %d", ret
);
1760 goto end_no_session
;
1763 net_seq_num
= be64toh(index_info
.net_seq_num
);
1766 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1769 goto end_rcu_unlock
;
1772 /* Live beacon handling */
1773 if (index_info
.packet_size
== 0) {
1774 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1777 * Only flag a stream inactive when it has already received data.
1779 if (stream
->total_index_received
> 0) {
1780 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1783 goto end_rcu_unlock
;
1785 stream
->beacon_ts_end
= -1ULL;
1788 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1790 /* A successful creation will add the object to the HT. */
1791 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1793 goto end_rcu_unlock
;
1798 copy_index_control_data(index
, &index_info
);
1800 if (index_created
) {
1802 * Try to add the relay index object to the hash table. If an object
1803 * already exist, destroy back the index created, set the data in this
1804 * object and write it on disk.
1806 relay_index_add(index
, &wr_index
);
1808 copy_index_control_data(wr_index
, &index_info
);
1812 /* The index already exists so write it on disk. */
1816 /* Do we have a writable ready index to write on disk. */
1818 /* Starting at 2.4, create the index file if none available. */
1819 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1820 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1821 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1822 stream
->tracefile_count_current
);
1824 goto end_rcu_unlock
;
1826 stream
->index_fd
= ret
;
1829 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1831 goto end_rcu_unlock
;
1833 stream
->total_index_received
++;
1840 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1842 reply
.ret_code
= htobe32(LTTNG_OK
);
1844 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1846 ERR("Relay sending close index id reply");
1855 * Process the commands received on the control socket
1858 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1859 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
1863 switch (be32toh(recv_hdr
->cmd
)) {
1864 case RELAYD_CREATE_SESSION
:
1865 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
1867 case RELAYD_ADD_STREAM
:
1868 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
1870 case RELAYD_START_DATA
:
1871 ret
= relay_start(recv_hdr
, cmd
);
1873 case RELAYD_SEND_METADATA
:
1874 ret
= relay_recv_metadata(recv_hdr
, cmd
);
1876 case RELAYD_VERSION
:
1877 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
1879 case RELAYD_CLOSE_STREAM
:
1880 ret
= relay_close_stream(recv_hdr
, cmd
);
1882 case RELAYD_DATA_PENDING
:
1883 ret
= relay_data_pending(recv_hdr
, cmd
);
1885 case RELAYD_QUIESCENT_CONTROL
:
1886 ret
= relay_quiescent_control(recv_hdr
, cmd
);
1888 case RELAYD_BEGIN_DATA_PENDING
:
1889 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
1891 case RELAYD_END_DATA_PENDING
:
1892 ret
= relay_end_data_pending(recv_hdr
, cmd
);
1894 case RELAYD_SEND_INDEX
:
1895 ret
= relay_recv_index(recv_hdr
, cmd
);
1897 case RELAYD_UPDATE_SYNC_INFO
:
1899 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1900 relay_unknown_command(cmd
);
1910 * Handle index for a data stream.
1912 * RCU read side lock MUST be acquired.
1914 * Return 0 on success else a negative value.
1916 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
1919 int ret
= 0, index_created
= 0;
1920 uint64_t stream_id
, data_offset
;
1921 struct relay_index
*index
, *wr_index
= NULL
;
1925 stream_id
= stream
->stream_handle
;
1926 /* Get data offset because we are about to update the index. */
1927 data_offset
= htobe64(stream
->tracefile_size_current
);
1930 * Lookup for an existing index for that stream id/sequence number. If on
1931 * exists, the control thread already received the data for it thus we need
1932 * to write it on disk.
1934 index
= relay_index_find(stream_id
, net_seq_num
);
1936 /* A successful creation will add the object to the HT. */
1937 index
= relay_index_create(stream_id
, net_seq_num
);
1945 if (rotate_index
|| stream
->index_fd
< 0) {
1946 index
->to_close_fd
= stream
->index_fd
;
1947 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1948 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1949 stream
->tracefile_count_current
);
1951 /* This will close the stream's index fd if one. */
1952 relay_index_free_safe(index
);
1955 stream
->index_fd
= ret
;
1957 index
->fd
= stream
->index_fd
;
1958 index
->index_data
.offset
= data_offset
;
1960 if (index_created
) {
1962 * Try to add the relay index object to the hash table. If an object
1963 * already exist, destroy back the index created and set the data.
1965 relay_index_add(index
, &wr_index
);
1967 /* Copy back data from the created index. */
1968 wr_index
->fd
= index
->fd
;
1969 wr_index
->to_close_fd
= index
->to_close_fd
;
1970 wr_index
->index_data
.offset
= data_offset
;
1974 /* The index already exists so write it on disk. */
1978 /* Do we have a writable ready index to write on disk. */
1980 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1984 stream
->total_index_received
++;
1992 * relay_process_data: Process the data received on the data socket
1995 int relay_process_data(struct relay_command
*cmd
)
1997 int ret
= 0, rotate_index
= 0;
1999 struct relay_stream
*stream
;
2000 struct lttcomm_relayd_data_hdr data_hdr
;
2002 uint64_t net_seq_num
;
2005 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2006 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2009 /* Orderly shutdown. Not necessary to print an error. */
2010 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2012 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2018 stream_id
= be64toh(data_hdr
.stream_id
);
2021 stream
= relay_stream_find_by_id(stream_id
);
2024 goto end_rcu_unlock
;
2027 data_size
= be32toh(data_hdr
.data_size
);
2028 if (data_buffer_size
< data_size
) {
2031 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2032 if (!tmp_data_ptr
) {
2033 ERR("Allocating data buffer");
2036 goto end_rcu_unlock
;
2038 data_buffer
= tmp_data_ptr
;
2039 data_buffer_size
= data_size
;
2041 memset(data_buffer
, 0, data_size
);
2043 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2045 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2046 data_size
, stream_id
, net_seq_num
);
2047 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2050 /* Orderly shutdown. Not necessary to print an error. */
2051 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2054 goto end_rcu_unlock
;
2057 /* Check if a rotation is needed. */
2058 if (stream
->tracefile_size
> 0 &&
2059 (stream
->tracefile_size_current
+ data_size
) >
2060 stream
->tracefile_size
) {
2061 struct relay_viewer_stream
*vstream
;
2064 new_id
= (stream
->tracefile_count_current
+ 1) %
2065 stream
->tracefile_count
;
2067 * When we wrap-around back to 0, we start overwriting old
2070 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2071 stream
->tracefile_overwrite
= 1;
2073 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2074 if (stream
->tracefile_overwrite
) {
2075 stream
->oldest_tracefile_id
=
2076 (stream
->oldest_tracefile_id
+ 1) %
2077 stream
->tracefile_count
;
2079 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
2082 * The viewer is reading a file about to be
2083 * overwritten. Close the FDs it is
2084 * currently using and let it handle the fault.
2086 if (vstream
->tracefile_count_current
== new_id
) {
2087 pthread_mutex_lock(&vstream
->overwrite_lock
);
2088 vstream
->abort_flag
= 1;
2089 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2090 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2091 stream
->channel_name
, new_id
);
2092 } else if (vstream
->tracefile_count_current
==
2093 stream
->tracefile_count_current
) {
2095 * The reader and writer were in the
2096 * same trace file, inform the viewer
2097 * that no new index will ever be added
2100 vstream
->close_write_flag
= 1;
2103 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2104 stream
->tracefile_size
, stream
->tracefile_count
,
2105 relayd_uid
, relayd_gid
, stream
->fd
,
2106 &(stream
->tracefile_count_current
), &stream
->fd
);
2107 stream
->total_index_received
= 0;
2108 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2110 ERR("Rotating stream output file");
2111 goto end_rcu_unlock
;
2113 /* Reset current size because we just perform a stream rotation. */
2114 stream
->tracefile_size_current
= 0;
2119 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2120 * index are NOT supported.
2122 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2123 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2125 goto end_rcu_unlock
;
2129 /* Write data to stream output fd. */
2130 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2131 if (size_ret
< data_size
) {
2132 ERR("Relay error writing data to file");
2134 goto end_rcu_unlock
;
2137 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2138 ret
, stream
->stream_handle
);
2140 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2142 goto end_rcu_unlock
;
2144 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2146 stream
->prev_seq
= net_seq_num
;
2148 /* Check if we need to close the FD */
2149 if (close_stream_check(stream
)) {
2150 destroy_stream(stream
);
2160 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2164 lttng_poll_del(events
, pollfd
);
2166 ret
= close(pollfd
);
2168 ERR("Closing pollfd %d", pollfd
);
2173 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2174 struct lttng_ht
*relay_connections_ht
)
2176 struct relay_command
*relay_connection
;
2179 relay_connection
= zmalloc(sizeof(struct relay_command
));
2180 if (relay_connection
== NULL
) {
2181 PERROR("Relay command zmalloc");
2184 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2185 if (ret
< sizeof(struct relay_command
)) {
2186 PERROR("read relay cmd pipe");
2191 * Only used by the control side and the reference is copied inside each
2192 * stream from that connection. Thus a destroy HT must be done after every
2193 * stream has been destroyed.
2195 if (relay_connection
->type
== RELAY_CONTROL
) {
2196 relay_connection
->ctf_traces_ht
= lttng_ht_new(0,
2197 LTTNG_HT_TYPE_STRING
);
2198 if (!relay_connection
->ctf_traces_ht
) {
2203 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2204 (unsigned long) relay_connection
->sock
->fd
);
2206 lttng_ht_add_unique_ulong(relay_connections_ht
,
2207 &relay_connection
->sock_n
);
2209 return lttng_poll_add(events
,
2210 relay_connection
->sock
->fd
,
2211 LPOLLIN
| LPOLLRDHUP
);
2214 free(relay_connection
);
2220 void deferred_free_connection(struct rcu_head
*head
)
2222 struct relay_command
*relay_connection
=
2223 caa_container_of(head
, struct relay_command
, rcu_node
);
2225 lttcomm_destroy_sock(relay_connection
->sock
);
2226 free(relay_connection
);
2230 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2231 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2232 struct lttng_ht
*sessions_ht
)
2236 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2239 if (relay_connection
->type
== RELAY_CONTROL
) {
2240 relay_delete_session(relay_connection
, sessions_ht
);
2241 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2244 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2248 * This thread does the actual work
2251 void *relay_thread_worker(void *data
)
2253 int ret
, err
= -1, last_seen_data_fd
= -1;
2255 struct relay_command
*relay_connection
;
2256 struct lttng_poll_event events
;
2257 struct lttng_ht
*relay_connections_ht
;
2258 struct lttng_ht_node_ulong
*node
;
2259 struct lttng_ht_iter iter
;
2260 struct lttcomm_relayd_hdr recv_hdr
;
2261 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2262 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2264 DBG("[thread] Relay worker started");
2266 rcu_register_thread();
2268 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2270 health_code_update();
2272 /* table of connections indexed on socket */
2273 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2274 if (!relay_connections_ht
) {
2275 goto relay_connections_ht_error
;
2278 /* Tables of received indexes indexed by index handle and net_seq_num. */
2279 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2281 goto indexes_ht_error
;
2284 ret
= create_thread_poll_set(&events
, 2);
2286 goto error_poll_create
;
2289 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2296 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2298 health_code_update();
2300 /* Infinite blocking call, waiting for transmission */
2301 DBG3("Relayd worker thread polling...");
2302 health_poll_entry();
2303 ret
= lttng_poll_wait(&events
, -1);
2307 * Restart interrupted system call.
2309 if (errno
== EINTR
) {
2318 * Process control. The control connection is prioritised so we don't
2319 * starve it with high throughout put tracing data on the data
2322 for (i
= 0; i
< nb_fd
; i
++) {
2323 /* Fetch once the poll data */
2324 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2325 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2327 health_code_update();
2329 /* Thread quit pipe has been closed. Killing thread. */
2330 ret
= check_thread_quit_pipe(pollfd
, revents
);
2336 /* Inspect the relay cmd pipe for new connection */
2337 if (pollfd
== relay_cmd_pipe
[0]) {
2338 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2339 ERR("Relay pipe error");
2341 } else if (revents
& LPOLLIN
) {
2342 DBG("Relay command received");
2343 ret
= relay_add_connection(relay_cmd_pipe
[0],
2344 &events
, relay_connections_ht
);
2349 } else if (revents
) {
2351 lttng_ht_lookup(relay_connections_ht
,
2352 (void *)((unsigned long) pollfd
),
2354 node
= lttng_ht_iter_get_node_ulong(&iter
);
2356 DBG2("Relay sock %d not found", pollfd
);
2360 relay_connection
= caa_container_of(node
,
2361 struct relay_command
, sock_n
);
2363 if (revents
& (LPOLLERR
)) {
2365 relay_cleanup_poll_connection(&events
, pollfd
);
2366 relay_del_connection(relay_connections_ht
,
2367 &iter
, relay_connection
, sessions_ht
);
2368 if (last_seen_data_fd
== pollfd
) {
2369 last_seen_data_fd
= last_notdel_data_fd
;
2371 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2372 DBG("Socket %d hung up", pollfd
);
2373 relay_cleanup_poll_connection(&events
, pollfd
);
2374 relay_del_connection(relay_connections_ht
,
2375 &iter
, relay_connection
, sessions_ht
);
2376 if (last_seen_data_fd
== pollfd
) {
2377 last_seen_data_fd
= last_notdel_data_fd
;
2379 } else if (revents
& LPOLLIN
) {
2380 /* control socket */
2381 if (relay_connection
->type
== RELAY_CONTROL
) {
2382 ret
= relay_connection
->sock
->ops
->recvmsg(
2383 relay_connection
->sock
, &recv_hdr
,
2384 sizeof(struct lttcomm_relayd_hdr
), 0);
2385 /* connection closed */
2387 relay_cleanup_poll_connection(&events
, pollfd
);
2388 relay_del_connection(relay_connections_ht
,
2389 &iter
, relay_connection
, sessions_ht
);
2390 DBG("Control connection closed with %d", pollfd
);
2392 if (relay_connection
->session
) {
2393 DBG2("Relay worker receiving data for session : %" PRIu64
,
2394 relay_connection
->session
->id
);
2396 ret
= relay_process_control(&recv_hdr
,
2397 relay_connection
, relay_ctx
);
2399 /* Clear the session on error. */
2400 relay_cleanup_poll_connection(&events
, pollfd
);
2401 relay_del_connection(relay_connections_ht
,
2402 &iter
, relay_connection
, sessions_ht
);
2403 DBG("Connection closed with %d", pollfd
);
2409 * Flag the last seen data fd not deleted. It will be
2410 * used as the last seen fd if any fd gets deleted in
2413 last_notdel_data_fd
= pollfd
;
2421 * The last loop handled a control request, go back to poll to make
2422 * sure we prioritise the control socket.
2428 if (last_seen_data_fd
>= 0) {
2429 for (i
= 0; i
< nb_fd
; i
++) {
2430 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2432 health_code_update();
2434 if (last_seen_data_fd
== pollfd
) {
2441 /* Process data connection. */
2442 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2443 /* Fetch the poll data. */
2444 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2445 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2447 health_code_update();
2449 /* Skip the command pipe. It's handled in the first loop. */
2450 if (pollfd
== relay_cmd_pipe
[0]) {
2456 lttng_ht_lookup(relay_connections_ht
,
2457 (void *)((unsigned long) pollfd
),
2459 node
= lttng_ht_iter_get_node_ulong(&iter
);
2461 /* Skip it. Might be removed before. */
2465 relay_connection
= caa_container_of(node
,
2466 struct relay_command
, sock_n
);
2468 if (revents
& LPOLLIN
) {
2469 if (relay_connection
->type
!= RELAY_DATA
) {
2473 ret
= relay_process_data(relay_connection
);
2474 /* connection closed */
2476 relay_cleanup_poll_connection(&events
, pollfd
);
2477 relay_del_connection(relay_connections_ht
,
2478 &iter
, relay_connection
, sessions_ht
);
2479 DBG("Data connection closed with %d", pollfd
);
2481 * Every goto restart call sets the last seen fd where
2482 * here we don't really care since we gracefully
2483 * continue the loop after the connection is deleted.
2486 /* Keep last seen port. */
2487 last_seen_data_fd
= pollfd
;
2495 last_seen_data_fd
= -1;
2498 /* Normal exit, no error */
2503 lttng_poll_clean(&events
);
2505 /* empty the hash table and free the memory */
2507 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2508 health_code_update();
2510 node
= lttng_ht_iter_get_node_ulong(&iter
);
2512 relay_connection
= caa_container_of(node
,
2513 struct relay_command
, sock_n
);
2514 relay_del_connection(relay_connections_ht
,
2515 &iter
, relay_connection
, sessions_ht
);
2520 lttng_ht_destroy(indexes_ht
);
2522 lttng_ht_destroy(relay_connections_ht
);
2523 relay_connections_ht_error
:
2524 /* Close relay cmd pipes */
2525 utils_close_pipe(relay_cmd_pipe
);
2527 DBG("Thread exited with error");
2529 DBG("Worker thread cleanup complete");
2533 ERR("Health error occurred in %s", __func__
);
2535 health_unregister(health_relayd
);
2536 rcu_unregister_thread();
2542 * Create the relay command pipe to wake thread_manage_apps.
2543 * Closed in cleanup().
2545 static int create_relay_cmd_pipe(void)
2549 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2557 int main(int argc
, char **argv
)
2561 struct relay_local_data
*relay_ctx
;
2563 /* Create thread quit pipe */
2564 if ((ret
= init_thread_quit_pipe()) < 0) {
2568 /* Parse arguments */
2570 if ((ret
= parse_args(argc
, argv
)) < 0) {
2574 if ((ret
= set_signal_handler()) < 0) {
2578 /* Try to create directory if -o, --output is specified. */
2579 if (opt_output_path
) {
2580 if (*opt_output_path
!= '/') {
2581 ERR("Please specify an absolute path for -o, --output PATH");
2585 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2587 ERR("Unable to create %s", opt_output_path
);
2601 /* We need those values for the file/dir creation. */
2602 relayd_uid
= getuid();
2603 relayd_gid
= getgid();
2605 /* Check if daemon is UID = 0 */
2606 if (relayd_uid
== 0) {
2607 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2608 live_uri
->port
< 1024) {
2609 ERR("Need to be root to use ports < 1024");
2615 /* Setup the thread apps communication pipe. */
2616 if ((ret
= create_relay_cmd_pipe()) < 0) {
2620 /* Init relay command queue. */
2621 cds_wfq_init(&relay_cmd_queue
.queue
);
2623 /* Set up max poll set size */
2624 lttng_poll_set_max_size();
2626 /* Initialize communication library */
2629 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2631 PERROR("relay_ctx");
2635 /* tables of sessions indexed by session ID */
2636 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2637 if (!relay_ctx
->sessions_ht
) {
2638 goto exit_relay_ctx_sessions
;
2641 /* tables of streams indexed by stream ID */
2642 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2643 if (!relay_streams_ht
) {
2644 goto exit_relay_ctx_streams
;
2647 /* tables of streams indexed by stream ID */
2648 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2649 if (!viewer_streams_ht
) {
2650 goto exit_relay_ctx_viewer_streams
;
2653 /* Initialize thread health monitoring */
2654 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2655 if (!health_relayd
) {
2656 PERROR("health_app_create error");
2657 goto exit_health_app_create
;
2660 ret
= utils_create_pipe(health_quit_pipe
);
2662 goto error_health_pipe
;
2665 /* Create thread to manage the client socket */
2666 ret
= pthread_create(&health_thread
, NULL
,
2667 thread_manage_health
, (void *) NULL
);
2669 PERROR("pthread_create health");
2673 /* Setup the dispatcher thread */
2674 ret
= pthread_create(&dispatcher_thread
, NULL
,
2675 relay_thread_dispatcher
, (void *) NULL
);
2677 PERROR("pthread_create dispatcher");
2678 goto exit_dispatcher
;
2681 /* Setup the worker thread */
2682 ret
= pthread_create(&worker_thread
, NULL
,
2683 relay_thread_worker
, (void *) relay_ctx
);
2685 PERROR("pthread_create worker");
2689 /* Setup the listener thread */
2690 ret
= pthread_create(&listener_thread
, NULL
,
2691 relay_thread_listener
, (void *) NULL
);
2693 PERROR("pthread_create listener");
2697 ret
= live_start_threads(live_uri
, relay_ctx
, thread_quit_pipe
);
2699 ERR("Starting live viewer threads");
2704 ret
= pthread_join(listener_thread
, &status
);
2706 PERROR("pthread_join");
2707 goto error
; /* join error, exit without cleanup */
2711 ret
= pthread_join(worker_thread
, &status
);
2713 PERROR("pthread_join");
2714 goto error
; /* join error, exit without cleanup */
2718 ret
= pthread_join(dispatcher_thread
, &status
);
2720 PERROR("pthread_join");
2721 goto error
; /* join error, exit without cleanup */
2725 ret
= pthread_join(health_thread
, &status
);
2727 PERROR("pthread_join health thread");
2728 goto error
; /* join error, exit without cleanup */
2732 * Stop live threads only after joining other threads.
2734 live_stop_threads();
2737 utils_close_pipe(health_quit_pipe
);
2740 health_app_destroy(health_relayd
);
2742 exit_health_app_create
:
2743 lttng_ht_destroy(viewer_streams_ht
);
2745 exit_relay_ctx_viewer_streams
:
2746 lttng_ht_destroy(relay_streams_ht
);
2748 exit_relay_ctx_streams
:
2749 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2751 exit_relay_ctx_sessions
: