2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/daemonize.h>
48 #include <common/futex.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/sessiond-comm/inet.h>
51 #include <common/sessiond-comm/relayd.h>
52 #include <common/uri.h>
53 #include <common/utils.h>
56 #include "ctf-trace.h"
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
62 #include "testpoint.h"
63 #include "viewer-stream.h"
67 /* command line options */
68 char *opt_output_path
;
69 static int opt_daemon
, opt_background
;
72 * We need to wait for listener and live listener threads, as well as
73 * health check thread, before being ready to signal readiness.
75 #define NR_LTTNG_RELAY_READY 3
76 static int lttng_relay_ready
= NR_LTTNG_RELAY_READY
;
77 static int recv_child_signal
; /* Set to 1 when a SIGUSR1 signal is received. */
78 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
80 static struct lttng_uri
*control_uri
;
81 static struct lttng_uri
*data_uri
;
82 static struct lttng_uri
*live_uri
;
86 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
89 * Quit pipe for all threads. This permits a single cancellation point
90 * for all threads when receiving an event on the pipe.
92 int thread_quit_pipe
[2] = { -1, -1 };
95 * This pipe is used to inform the worker thread that a command is queued and
96 * ready to be processed.
98 static int relay_cmd_pipe
[2] = { -1, -1 };
100 /* Shared between threads */
101 static int dispatch_thread_exit
;
103 static pthread_t listener_thread
;
104 static pthread_t dispatcher_thread
;
105 static pthread_t worker_thread
;
106 static pthread_t health_thread
;
108 static uint64_t last_relay_stream_id
;
111 * Relay command queue.
113 * The relay_thread_listener and relay_thread_dispatcher communicate with this
116 static struct relay_cmd_queue relay_cmd_queue
;
118 /* buffer allocated at startup, used to store the trace data */
119 static char *data_buffer
;
120 static unsigned int data_buffer_size
;
122 /* We need those values for the file/dir creation. */
123 static uid_t relayd_uid
;
124 static gid_t relayd_gid
;
126 /* Global relay stream hash table. */
127 struct lttng_ht
*relay_streams_ht
;
129 /* Global relay viewer stream hash table. */
130 struct lttng_ht
*viewer_streams_ht
;
132 /* Global hash table that stores relay index object. */
133 struct lttng_ht
*indexes_ht
;
135 /* Relayd health monitoring */
136 struct health_app
*health_relayd
;
139 * usage function on stderr
144 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
145 fprintf(stderr
, " -h, --help Display this usage.\n");
146 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
147 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
148 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
149 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
150 fprintf(stderr
, " -L, --live-port URL Live view port listening.\n");
151 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
152 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
153 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
157 int parse_args(int argc
, char **argv
)
161 char *default_address
;
163 static struct option long_options
[] = {
164 { "control-port", 1, 0, 'C', },
165 { "data-port", 1, 0, 'D', },
166 { "daemonize", 0, 0, 'd', },
167 { "group", 1, 0, 'g', },
168 { "help", 0, 0, 'h', },
169 { "output", 1, 0, 'o', },
170 { "verbose", 0, 0, 'v', },
171 { "background", 0, 0, 'b' },
176 int option_index
= 0;
177 c
= getopt_long(argc
, argv
, "dhv" "C:D:L:o:g:b",
178 long_options
, &option_index
);
185 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
187 fprintf(stderr
, " with arg %s\n", optarg
);
191 ret
= uri_parse(optarg
, &control_uri
);
193 ERR("Invalid control URI specified");
196 if (control_uri
->port
== 0) {
197 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
201 ret
= uri_parse(optarg
, &data_uri
);
203 ERR("Invalid data URI specified");
206 if (data_uri
->port
== 0) {
207 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
211 ret
= uri_parse(optarg
, &live_uri
);
213 ERR("Invalid live URI specified");
216 if (live_uri
->port
== 0) {
217 live_uri
->port
= DEFAULT_NETWORK_VIEWER_PORT
;
227 tracing_group_name
= optarg
;
233 ret
= asprintf(&opt_output_path
, "%s", optarg
);
235 PERROR("asprintf opt_output_path");
240 /* Verbose level can increase using multiple -v */
241 lttng_opt_verbose
+= 1;
244 /* Unknown option or other error.
245 * Error is printed by getopt, just return */
251 /* assign default values */
252 if (control_uri
== NULL
) {
253 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
254 DEFAULT_NETWORK_CONTROL_PORT
);
256 PERROR("asprintf default data address");
260 ret
= uri_parse(default_address
, &control_uri
);
261 free(default_address
);
263 ERR("Invalid control URI specified");
267 if (data_uri
== NULL
) {
268 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
269 DEFAULT_NETWORK_DATA_PORT
);
271 PERROR("asprintf default data address");
275 ret
= uri_parse(default_address
, &data_uri
);
276 free(default_address
);
278 ERR("Invalid data URI specified");
282 if (live_uri
== NULL
) {
283 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
284 DEFAULT_NETWORK_VIEWER_PORT
);
286 PERROR("asprintf default viewer control address");
290 ret
= uri_parse(default_address
, &live_uri
);
291 free(default_address
);
293 ERR("Invalid viewer control URI specified");
310 /* free the dynamically allocated opt_output_path */
311 free(opt_output_path
);
313 /* Close thread quit pipes */
314 utils_close_pipe(thread_quit_pipe
);
316 uri_free(control_uri
);
318 /* Live URI is freed in the live thread. */
322 * Write to writable pipe used to notify a thread.
325 int notify_thread_pipe(int wpipe
)
329 ret
= lttng_write(wpipe
, "!", 1);
331 PERROR("write poll pipe");
337 static void notify_health_quit_pipe(int *pipe
)
341 ret
= lttng_write(pipe
[1], "4", 1);
343 PERROR("write relay health quit");
348 * Stop all threads by closing the thread quit pipe.
351 void stop_threads(void)
355 /* Stopping all threads */
356 DBG("Terminating all threads");
357 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
359 ERR("write error on thread quit pipe");
362 notify_health_quit_pipe(health_quit_pipe
);
364 /* Dispatch thread */
365 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
366 futex_nto1_wake(&relay_cmd_queue
.futex
);
370 * Signal handler for the daemon
372 * Simply stop all worker threads, leaving main() return gracefully after
373 * joining all threads and calling cleanup().
376 void sighandler(int sig
)
380 DBG("SIGPIPE caught");
383 DBG("SIGINT caught");
387 DBG("SIGTERM caught");
391 CMM_STORE_SHARED(recv_child_signal
, 1);
399 * Setup signal handler for :
400 * SIGINT, SIGTERM, SIGPIPE
403 int set_signal_handler(void)
409 if ((ret
= sigemptyset(&sigset
)) < 0) {
410 PERROR("sigemptyset");
414 sa
.sa_handler
= sighandler
;
417 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
422 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
427 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
432 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
437 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
442 void lttng_relay_notify_ready(void)
444 /* Notify the parent of the fork() process that we are ready. */
445 if (opt_daemon
|| opt_background
) {
446 if (uatomic_sub_return(<tng_relay_ready
, 1) == 0) {
447 kill(child_ppid
, SIGUSR1
);
453 * Init thread quit pipe.
455 * Return -1 on error or 0 if all pipes are created.
458 int init_thread_quit_pipe(void)
462 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
468 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
471 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
475 if (events
== NULL
|| size
== 0) {
480 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
486 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
498 * Check if the thread quit pipe was triggered.
500 * Return 1 if it was triggered else 0;
503 int check_thread_quit_pipe(int fd
, uint32_t events
)
505 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
513 * Create and init socket from uri.
516 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
519 struct lttcomm_sock
*sock
= NULL
;
521 sock
= lttcomm_alloc_sock_from_uri(uri
);
523 ERR("Allocating socket");
527 ret
= lttcomm_create_sock(sock
);
531 DBG("Listening on sock %d", sock
->fd
);
533 ret
= sock
->ops
->bind(sock
);
538 ret
= sock
->ops
->listen(sock
, -1);
548 lttcomm_destroy_sock(sock
);
554 * Return nonzero if stream needs to be closed.
557 int close_stream_check(struct relay_stream
*stream
)
559 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
561 * We are about to close the stream so set the data pending flag to 1
562 * which will make the end data pending command skip the stream which
563 * is now closed and ready. Note that after proceeding to a file close,
564 * the written file is ready for reading.
566 stream
->data_pending_check_done
= 1;
572 static void try_close_stream(struct relay_session
*session
,
573 struct relay_stream
*stream
)
576 struct ctf_trace
*ctf_trace
;
581 if (!close_stream_check(stream
)) {
582 /* Can't close it, not ready for that. */
586 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
590 pthread_mutex_lock(&session
->viewer_ready_lock
);
591 ctf_trace
->invalid_flag
= 1;
592 pthread_mutex_unlock(&session
->viewer_ready_lock
);
594 ret
= stream_close(session
, stream
);
596 /* Already close thus the ctf trace is being or has been destroyed. */
600 ctf_trace_try_destroy(session
, ctf_trace
);
607 * This thread manages the listening for new connections on the network
610 void *relay_thread_listener(void *data
)
612 int i
, ret
, pollfd
, err
= -1;
614 uint32_t revents
, nb_fd
;
615 struct lttng_poll_event events
;
616 struct lttcomm_sock
*control_sock
, *data_sock
;
618 DBG("[thread] Relay listener started");
620 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
622 health_code_update();
624 control_sock
= relay_init_sock(control_uri
);
626 goto error_sock_control
;
629 data_sock
= relay_init_sock(data_uri
);
631 goto error_sock_relay
;
635 * Pass 3 as size here for the thread quit pipe, control and data socket.
637 ret
= create_thread_poll_set(&events
, 3);
639 goto error_create_poll
;
642 /* Add the control socket */
643 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
648 /* Add the data socket */
649 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
654 lttng_relay_notify_ready();
656 if (testpoint(relayd_thread_listener
)) {
657 goto error_testpoint
;
661 health_code_update();
663 DBG("Listener accepting connections");
667 ret
= lttng_poll_wait(&events
, -1);
671 * Restart interrupted system call.
673 if (errno
== EINTR
) {
681 DBG("Relay new connection received");
682 for (i
= 0; i
< nb_fd
; i
++) {
683 health_code_update();
685 /* Fetch once the poll data */
686 revents
= LTTNG_POLL_GETEV(&events
, i
);
687 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
689 /* Thread quit pipe has been closed. Killing thread. */
690 ret
= check_thread_quit_pipe(pollfd
, revents
);
696 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
697 ERR("socket poll error");
699 } else if (revents
& LPOLLIN
) {
701 * Get allocated in this thread,
702 * enqueued to a global queue, dequeued
703 * and freed in the worker thread.
705 struct relay_command
*relay_cmd
;
706 struct lttcomm_sock
*newsock
;
708 relay_cmd
= zmalloc(sizeof(struct relay_command
));
709 if (relay_cmd
== NULL
) {
710 PERROR("relay command zmalloc");
714 if (pollfd
== data_sock
->fd
) {
715 newsock
= data_sock
->ops
->accept(data_sock
);
717 PERROR("accepting data sock");
721 relay_cmd
->type
= RELAY_DATA
;
722 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
724 assert(pollfd
== control_sock
->fd
);
725 newsock
= control_sock
->ops
->accept(control_sock
);
727 PERROR("accepting control sock");
731 relay_cmd
->type
= RELAY_CONTROL
;
732 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
734 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
737 PERROR("setsockopt inet");
738 lttcomm_destroy_sock(newsock
);
742 relay_cmd
->sock
= newsock
;
744 * Lock free enqueue the request.
746 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
749 * Wake the dispatch queue futex. Implicit memory
750 * barrier with the exchange in cds_wfq_enqueue.
752 futex_nto1_wake(&relay_cmd_queue
.futex
);
761 lttng_poll_clean(&events
);
763 if (data_sock
->fd
>= 0) {
764 ret
= data_sock
->ops
->close(data_sock
);
769 lttcomm_destroy_sock(data_sock
);
771 if (control_sock
->fd
>= 0) {
772 ret
= control_sock
->ops
->close(control_sock
);
777 lttcomm_destroy_sock(control_sock
);
781 ERR("Health error occurred in %s", __func__
);
783 health_unregister(health_relayd
);
784 DBG("Relay listener thread cleanup complete");
790 * This thread manages the dispatching of the requests to worker threads
793 void *relay_thread_dispatcher(void *data
)
797 struct cds_wfq_node
*node
;
798 struct relay_command
*relay_cmd
= NULL
;
800 DBG("[thread] Relay dispatcher started");
802 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
804 if (testpoint(relayd_thread_dispatcher
)) {
805 goto error_testpoint
;
808 health_code_update();
810 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
811 health_code_update();
813 /* Atomically prepare the queue futex */
814 futex_nto1_prepare(&relay_cmd_queue
.futex
);
817 health_code_update();
819 /* Dequeue commands */
820 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
822 DBG("Woken up but nothing in the relay command queue");
823 /* Continue thread execution */
827 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
828 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
831 * Inform worker thread of the new request. This
832 * call is blocking so we can be assured that the data will be read
833 * at some point in time or wait to the end of the world :)
835 ret
= lttng_write(relay_cmd_pipe
[1], relay_cmd
,
836 sizeof(struct relay_command
));
838 if (ret
< sizeof(struct relay_command
)) {
839 PERROR("write cmd pipe");
842 } while (node
!= NULL
);
844 /* Futex wait on queue. Blocking call on futex() */
846 futex_nto1_wait(&relay_cmd_queue
.futex
);
850 /* Normal exit, no error */
857 ERR("Health error occurred in %s", __func__
);
859 health_unregister(health_relayd
);
860 DBG("Dispatch thread dying");
865 static void try_close_streams(struct relay_session
*session
)
867 struct ctf_trace
*ctf_trace
;
868 struct lttng_ht_iter iter
;
872 pthread_mutex_lock(&session
->viewer_ready_lock
);
874 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
876 struct relay_stream
*stream
;
879 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
880 stream_close(session
, stream
);
883 ctf_trace
->invalid_flag
= 1;
884 ctf_trace_try_destroy(session
, ctf_trace
);
887 pthread_mutex_unlock(&session
->viewer_ready_lock
);
891 * Try to destroy a session within a connection.
894 void relay_delete_session(struct relay_command
*cmd
,
895 struct lttng_ht
*sessions_ht
)
900 /* Indicate that this session can be destroyed from now on. */
901 cmd
->session
->close_flag
= 1;
903 try_close_streams(cmd
->session
);
906 * This will try to delete and destroy the session if no viewer is attached
907 * to it meaning the refcount is down to zero.
909 session_try_destroy(sessions_ht
, cmd
->session
);
913 * Copy index data from the control port to a given index object.
915 static void copy_index_control_data(struct relay_index
*index
,
916 struct lttcomm_relayd_index
*data
)
922 * The index on disk is encoded in big endian, so we don't need to convert
923 * the data received on the network. The data_offset value is NEVER
924 * modified here and is updated by the data thread.
926 index
->index_data
.packet_size
= data
->packet_size
;
927 index
->index_data
.content_size
= data
->content_size
;
928 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
929 index
->index_data
.timestamp_end
= data
->timestamp_end
;
930 index
->index_data
.events_discarded
= data
->events_discarded
;
931 index
->index_data
.stream_id
= data
->stream_id
;
935 * Handle the RELAYD_CREATE_SESSION command.
937 * On success, send back the session id or else return a negative value.
940 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
941 struct relay_command
*cmd
,
942 struct lttng_ht
*sessions_ht
)
944 int ret
= 0, send_ret
;
945 struct relay_session
*session
;
946 struct lttcomm_relayd_status_session reply
;
951 memset(&reply
, 0, sizeof(reply
));
953 session
= session_create();
958 session
->minor
= cmd
->minor
;
959 session
->major
= cmd
->major
;
960 cmd
->session_id
= session
->id
;
961 cmd
->session
= session
;
963 reply
.session_id
= htobe64(session
->id
);
965 switch (cmd
->minor
) {
970 case 4: /* LTTng sessiond 2.4 */
972 ret
= cmd_create_session_2_4(cmd
, session
);
976 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
977 DBG("Created session %" PRIu64
, session
->id
);
981 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
983 reply
.ret_code
= htobe32(LTTNG_OK
);
986 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
988 ERR("Relayd sending session id");
996 * When we have received all the streams and the metadata for a channel,
997 * we make them visible to the viewer threads.
1000 void set_viewer_ready_flag(struct relay_command
*cmd
)
1002 struct relay_stream
*stream
, *tmp_stream
;
1004 pthread_mutex_lock(&cmd
->session
->viewer_ready_lock
);
1005 cds_list_for_each_entry_safe(stream
, tmp_stream
, &cmd
->recv_head
,
1007 stream
->viewer_ready
= 1;
1008 cds_list_del(&stream
->recv_list
);
1010 pthread_mutex_unlock(&cmd
->session
->viewer_ready_lock
);
1015 * Add a recv handle node to the connection recv list with the given stream
1016 * handle. A new node is allocated thus must be freed when the node is deleted
1019 static void queue_stream(struct relay_stream
*stream
, struct relay_command
*cmd
)
1024 cds_list_add(&stream
->recv_list
, &cmd
->recv_head
);
1028 * relay_add_stream: allocate a new stream for a session
1031 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1032 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1035 struct relay_session
*session
= cmd
->session
;
1036 struct relay_stream
*stream
= NULL
;
1037 struct lttcomm_relayd_status_stream reply
;
1038 struct ctf_trace
*trace
;
1040 if (!session
|| cmd
->version_check_done
== 0) {
1041 ERR("Trying to add a stream before version check");
1043 goto end_no_session
;
1046 stream
= zmalloc(sizeof(struct relay_stream
));
1047 if (stream
== NULL
) {
1048 PERROR("relay stream zmalloc");
1050 goto end_no_session
;
1053 switch (cmd
->minor
) {
1054 case 1: /* LTTng sessiond 2.1 */
1055 ret
= cmd_recv_stream_2_1(cmd
, stream
);
1057 case 2: /* LTTng sessiond 2.2 */
1059 ret
= cmd_recv_stream_2_2(cmd
, stream
);
1063 goto err_free_stream
;
1067 stream
->stream_handle
= ++last_relay_stream_id
;
1068 stream
->prev_seq
= -1ULL;
1069 stream
->session_id
= session
->id
;
1070 stream
->index_fd
= -1;
1071 stream
->read_index_fd
= -1;
1072 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
1073 pthread_mutex_init(&stream
->lock
, NULL
);
1075 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1077 ERR("relay creating output directory");
1082 * No need to use run_as API here because whatever we receives, the relayd
1083 * uses its own credentials for the stream files.
1085 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1086 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1088 ERR("Create output file");
1092 if (stream
->tracefile_size
) {
1093 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1095 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1098 trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, stream
->path_name
);
1100 trace
= ctf_trace_create(stream
->path_name
);
1105 ctf_trace_add(session
->ctf_traces_ht
, trace
);
1107 ctf_trace_get_ref(trace
);
1109 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1110 stream
->metadata_flag
= 1;
1111 /* Assign quick reference to the metadata stream in the trace. */
1112 trace
->metadata_stream
= stream
;
1116 * Add the stream in the recv list of the connection. Once the end stream
1117 * message is received, this list is emptied and streams are set with the
1118 * viewer ready flag.
1120 queue_stream(stream
, cmd
);
1123 * Both in the ctf_trace object and the global stream ht since the data
1124 * side of the relayd does not have the concept of session.
1126 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
1127 cds_list_add_tail(&stream
->trace_list
, &trace
->stream_list
);
1129 session
->stream_count
++;
1131 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1132 stream
->stream_handle
);
1135 reply
.handle
= htobe64(stream
->stream_handle
);
1136 /* send the session id to the client or a negative return code on error */
1138 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1139 /* stream was not properly added to the ht, so free it */
1142 reply
.ret_code
= htobe32(LTTNG_OK
);
1145 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1146 sizeof(struct lttcomm_relayd_status_stream
), 0);
1148 ERR("Relay sending stream id");
1157 free(stream
->path_name
);
1158 free(stream
->channel_name
);
1164 * relay_close_stream: close a specific stream
1167 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1168 struct relay_command
*cmd
)
1171 struct relay_session
*session
= cmd
->session
;
1172 struct lttcomm_relayd_close_stream stream_info
;
1173 struct lttcomm_relayd_generic_reply reply
;
1174 struct relay_stream
*stream
;
1176 DBG("Close stream received");
1178 if (!session
|| cmd
->version_check_done
== 0) {
1179 ERR("Trying to close a stream before version check");
1181 goto end_no_session
;
1184 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1185 sizeof(struct lttcomm_relayd_close_stream
), 0);
1186 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1188 /* Orderly shutdown. Not necessary to print an error. */
1189 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1191 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1194 goto end_no_session
;
1198 stream
= stream_find_by_id(relay_streams_ht
,
1199 be64toh(stream_info
.stream_id
));
1205 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1206 stream
->close_flag
= 1;
1207 session
->stream_count
--;
1208 assert(session
->stream_count
>= 0);
1210 /* Check if we can close it or else the data will do it. */
1211 try_close_stream(session
, stream
);
1217 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1219 reply
.ret_code
= htobe32(LTTNG_OK
);
1221 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1222 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1224 ERR("Relay sending stream id");
1233 * relay_unknown_command: send -1 if received unknown command
1236 void relay_unknown_command(struct relay_command
*cmd
)
1238 struct lttcomm_relayd_generic_reply reply
;
1241 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1242 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1243 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1245 ERR("Relay sending unknown command");
1250 * relay_start: send an acknowledgment to the client to tell if we are
1251 * ready to receive data. We are ready if a session is established.
1254 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1255 struct relay_command
*cmd
)
1257 int ret
= htobe32(LTTNG_OK
);
1258 struct lttcomm_relayd_generic_reply reply
;
1259 struct relay_session
*session
= cmd
->session
;
1262 DBG("Trying to start the streaming without a session established");
1263 ret
= htobe32(LTTNG_ERR_UNK
);
1266 reply
.ret_code
= ret
;
1267 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1268 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1270 ERR("Relay sending start ack");
1277 * Append padding to the file pointed by the file descriptor fd.
1279 static int write_padding_to_file(int fd
, uint32_t size
)
1288 zeros
= zmalloc(size
);
1289 if (zeros
== NULL
) {
1290 PERROR("zmalloc zeros for padding");
1295 ret
= lttng_write(fd
, zeros
, size
);
1297 PERROR("write padding to file");
1307 * relay_recv_metadata: receive the metada for the session.
1310 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1311 struct relay_command
*cmd
)
1313 int ret
= htobe32(LTTNG_OK
);
1315 struct relay_session
*session
= cmd
->session
;
1316 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1317 struct relay_stream
*metadata_stream
;
1318 uint64_t data_size
, payload_size
;
1319 struct ctf_trace
*ctf_trace
;
1322 ERR("Metadata sent before version check");
1327 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1328 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1329 ERR("Incorrect data size");
1333 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1335 if (data_buffer_size
< data_size
) {
1336 /* In case the realloc fails, we can free the memory */
1339 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1340 if (!tmp_data_ptr
) {
1341 ERR("Allocating data buffer");
1346 data_buffer
= tmp_data_ptr
;
1347 data_buffer_size
= data_size
;
1349 memset(data_buffer
, 0, data_size
);
1350 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1351 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1352 if (ret
< 0 || ret
!= data_size
) {
1354 /* Orderly shutdown. Not necessary to print an error. */
1355 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1357 ERR("Relay didn't receive the whole metadata");
1362 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1365 metadata_stream
= stream_find_by_id(relay_streams_ht
,
1366 be64toh(metadata_struct
->stream_id
));
1367 if (!metadata_stream
) {
1372 size_ret
= lttng_write(metadata_stream
->fd
, metadata_struct
->payload
,
1374 if (size_ret
< payload_size
) {
1375 ERR("Relay error writing metadata on file");
1380 ret
= write_padding_to_file(metadata_stream
->fd
,
1381 be32toh(metadata_struct
->padding_size
));
1386 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1387 metadata_stream
->path_name
);
1389 ctf_trace
->metadata_received
+=
1390 payload_size
+ be32toh(metadata_struct
->padding_size
);
1392 DBG2("Relay metadata written");
1401 * relay_send_version: send relayd version number
1404 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1405 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1408 struct lttcomm_relayd_version reply
, msg
;
1412 cmd
->version_check_done
= 1;
1414 /* Get version from the other side. */
1415 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1416 if (ret
< 0 || ret
!= sizeof(msg
)) {
1418 /* Orderly shutdown. Not necessary to print an error. */
1419 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1421 ERR("Relay failed to receive the version values.");
1427 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1428 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1430 /* Major versions must be the same */
1431 if (reply
.major
!= be32toh(msg
.major
)) {
1432 DBG("Incompatible major versions (%u vs %u), deleting session",
1433 reply
.major
, be32toh(msg
.major
));
1434 relay_delete_session(cmd
, sessions_ht
);
1439 cmd
->major
= reply
.major
;
1440 /* We adapt to the lowest compatible version */
1441 if (reply
.minor
<= be32toh(msg
.minor
)) {
1442 cmd
->minor
= reply
.minor
;
1444 cmd
->minor
= be32toh(msg
.minor
);
1447 reply
.major
= htobe32(reply
.major
);
1448 reply
.minor
= htobe32(reply
.minor
);
1449 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1450 sizeof(struct lttcomm_relayd_version
), 0);
1452 ERR("Relay sending version");
1455 DBG("Version check done using protocol %u.%u", cmd
->major
,
1463 * Check for data pending for a given stream id from the session daemon.
1466 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1467 struct relay_command
*cmd
)
1469 struct relay_session
*session
= cmd
->session
;
1470 struct lttcomm_relayd_data_pending msg
;
1471 struct lttcomm_relayd_generic_reply reply
;
1472 struct relay_stream
*stream
;
1474 uint64_t last_net_seq_num
, stream_id
;
1476 DBG("Data pending command received");
1478 if (!session
|| cmd
->version_check_done
== 0) {
1479 ERR("Trying to check for data before version check");
1481 goto end_no_session
;
1484 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1485 if (ret
< sizeof(msg
)) {
1487 /* Orderly shutdown. Not necessary to print an error. */
1488 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1490 ERR("Relay didn't receive valid data_pending struct size : %d",
1494 goto end_no_session
;
1497 stream_id
= be64toh(msg
.stream_id
);
1498 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1501 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
1502 if (stream
== NULL
) {
1507 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1508 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1511 /* Avoid wrapping issue */
1512 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1513 /* Data has in fact been written and is NOT pending */
1516 /* Data still being streamed thus pending */
1520 /* Pending check is now done. */
1521 stream
->data_pending_check_done
= 1;
1526 reply
.ret_code
= htobe32(ret
);
1527 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1529 ERR("Relay data pending ret code failed");
1537 * Wait for the control socket to reach a quiescent state.
1539 * Note that for now, when receiving this command from the session daemon, this
1540 * means that every subsequent commands or data received on the control socket
1541 * has been handled. So, this is why we simply return OK here.
1544 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1545 struct relay_command
*cmd
)
1549 struct relay_stream
*stream
;
1550 struct lttng_ht_iter iter
;
1551 struct lttcomm_relayd_quiescent_control msg
;
1552 struct lttcomm_relayd_generic_reply reply
;
1554 DBG("Checking quiescent state on control socket");
1556 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1557 ERR("Trying to check for data before version check");
1559 goto end_no_session
;
1562 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1563 if (ret
< sizeof(msg
)) {
1565 /* Orderly shutdown. Not necessary to print an error. */
1566 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1568 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1572 goto end_no_session
;
1575 stream_id
= be64toh(msg
.stream_id
);
1578 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1580 if (stream
->stream_handle
== stream_id
) {
1581 stream
->data_pending_check_done
= 1;
1582 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1589 reply
.ret_code
= htobe32(LTTNG_OK
);
1590 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1592 ERR("Relay data quiescent control ret code failed");
1600 * Initialize a data pending command. This means that a client is about to ask
1601 * for data pending for each stream he/she holds. Simply iterate over all
1602 * streams of a session and set the data_pending_check_done flag.
1604 * This command returns to the client a LTTNG_OK code.
1607 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1608 struct relay_command
*cmd
)
1611 struct lttng_ht_iter iter
;
1612 struct lttcomm_relayd_begin_data_pending msg
;
1613 struct lttcomm_relayd_generic_reply reply
;
1614 struct relay_stream
*stream
;
1615 uint64_t session_id
;
1620 DBG("Init streams for data pending");
1622 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1623 ERR("Trying to check for data before version check");
1625 goto end_no_session
;
1628 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1629 if (ret
< sizeof(msg
)) {
1631 /* Orderly shutdown. Not necessary to print an error. */
1632 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1634 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1638 goto end_no_session
;
1641 session_id
= be64toh(msg
.session_id
);
1644 * Iterate over all streams to set the begin data pending flag. For now, the
1645 * streams are indexed by stream handle so we have to iterate over all
1646 * streams to find the one associated with the right session_id.
1649 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1651 if (stream
->session_id
== session_id
) {
1652 stream
->data_pending_check_done
= 0;
1653 DBG("Set begin data pending flag to stream %" PRIu64
,
1654 stream
->stream_handle
);
1659 /* All good, send back reply. */
1660 reply
.ret_code
= htobe32(LTTNG_OK
);
1662 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1664 ERR("Relay begin data pending send reply failed");
1672 * End data pending command. This will check, for a given session id, if each
1673 * stream associated with it has its data_pending_check_done flag set. If not,
1674 * this means that the client lost track of the stream but the data is still
1675 * being streamed on our side. In this case, we inform the client that data is
1678 * Return to the client if there is data in flight or not with a ret_code.
1681 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1682 struct relay_command
*cmd
)
1685 struct lttng_ht_iter iter
;
1686 struct lttcomm_relayd_end_data_pending msg
;
1687 struct lttcomm_relayd_generic_reply reply
;
1688 struct relay_stream
*stream
;
1689 uint64_t session_id
;
1690 uint32_t is_data_inflight
= 0;
1695 DBG("End data pending command");
1697 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1698 ERR("Trying to check for data before version check");
1700 goto end_no_session
;
1703 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1704 if (ret
< sizeof(msg
)) {
1706 /* Orderly shutdown. Not necessary to print an error. */
1707 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1709 ERR("Relay didn't receive valid end data_pending struct size: %d",
1713 goto end_no_session
;
1716 session_id
= be64toh(msg
.session_id
);
1718 /* Iterate over all streams to see if the begin data pending flag is set. */
1720 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1722 if (stream
->session_id
== session_id
&&
1723 !stream
->data_pending_check_done
) {
1724 is_data_inflight
= 1;
1725 DBG("Data is still in flight for stream %" PRIu64
,
1726 stream
->stream_handle
);
1732 /* All good, send back reply. */
1733 reply
.ret_code
= htobe32(is_data_inflight
);
1735 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1737 ERR("Relay end data pending send reply failed");
1745 * Receive an index for a specific stream.
1747 * Return 0 on success else a negative value.
1750 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1751 struct relay_command
*cmd
)
1753 int ret
, send_ret
, index_created
= 0;
1754 struct relay_session
*session
= cmd
->session
;
1755 struct lttcomm_relayd_index index_info
;
1756 struct relay_index
*index
, *wr_index
= NULL
;
1757 struct lttcomm_relayd_generic_reply reply
;
1758 struct relay_stream
*stream
;
1759 uint64_t net_seq_num
;
1763 DBG("Relay receiving index");
1765 if (!session
|| cmd
->version_check_done
== 0) {
1766 ERR("Trying to close a stream before version check");
1768 goto end_no_session
;
1771 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1772 sizeof(index_info
), 0);
1773 if (ret
< sizeof(index_info
)) {
1775 /* Orderly shutdown. Not necessary to print an error. */
1776 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1778 ERR("Relay didn't receive valid index struct size : %d", ret
);
1781 goto end_no_session
;
1784 net_seq_num
= be64toh(index_info
.net_seq_num
);
1787 stream
= stream_find_by_id(relay_streams_ht
,
1788 be64toh(index_info
.relay_stream_id
));
1791 goto end_rcu_unlock
;
1794 /* Live beacon handling */
1795 if (index_info
.packet_size
== 0) {
1796 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1799 * Only flag a stream inactive when it has already received data.
1801 if (stream
->total_index_received
> 0) {
1802 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1805 goto end_rcu_unlock
;
1807 stream
->beacon_ts_end
= -1ULL;
1810 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1812 /* A successful creation will add the object to the HT. */
1813 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1815 goto end_rcu_unlock
;
1820 copy_index_control_data(index
, &index_info
);
1822 if (index_created
) {
1824 * Try to add the relay index object to the hash table. If an object
1825 * already exist, destroy back the index created, set the data in this
1826 * object and write it on disk.
1828 relay_index_add(index
, &wr_index
);
1830 copy_index_control_data(wr_index
, &index_info
);
1834 /* The index already exists so write it on disk. */
1838 /* Do we have a writable ready index to write on disk. */
1840 /* Starting at 2.4, create the index file if none available. */
1841 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1842 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1843 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1844 stream
->tracefile_count_current
);
1846 goto end_rcu_unlock
;
1848 stream
->index_fd
= ret
;
1851 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1853 goto end_rcu_unlock
;
1855 stream
->total_index_received
++;
1862 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1864 reply
.ret_code
= htobe32(LTTNG_OK
);
1866 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1868 ERR("Relay sending close index id reply");
1877 * Receive the streams_sent message.
1879 * Return 0 on success else a negative value.
1882 int relay_streams_sent(struct lttcomm_relayd_hdr
*recv_hdr
,
1883 struct relay_command
*cmd
)
1886 struct lttcomm_relayd_generic_reply reply
;
1890 DBG("Relay receiving streams_sent");
1892 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1893 ERR("Trying to close a stream before version check");
1895 goto end_no_session
;
1899 * Flag every pending stream in the connection recv list that they are
1900 * ready to be used by the viewer.
1902 set_viewer_ready_flag(cmd
);
1905 * Inform the viewer that there are new streams in the session.
1907 uatomic_set(&cmd
->session
->new_streams
, 1);
1909 reply
.ret_code
= htobe32(LTTNG_OK
);
1910 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1912 ERR("Relay sending sent_stream reply");
1924 * Process the commands received on the control socket
1927 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1928 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
1932 switch (be32toh(recv_hdr
->cmd
)) {
1933 case RELAYD_CREATE_SESSION
:
1934 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
1936 case RELAYD_ADD_STREAM
:
1937 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
1939 case RELAYD_START_DATA
:
1940 ret
= relay_start(recv_hdr
, cmd
);
1942 case RELAYD_SEND_METADATA
:
1943 ret
= relay_recv_metadata(recv_hdr
, cmd
);
1945 case RELAYD_VERSION
:
1946 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
1948 case RELAYD_CLOSE_STREAM
:
1949 ret
= relay_close_stream(recv_hdr
, cmd
);
1951 case RELAYD_DATA_PENDING
:
1952 ret
= relay_data_pending(recv_hdr
, cmd
);
1954 case RELAYD_QUIESCENT_CONTROL
:
1955 ret
= relay_quiescent_control(recv_hdr
, cmd
);
1957 case RELAYD_BEGIN_DATA_PENDING
:
1958 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
1960 case RELAYD_END_DATA_PENDING
:
1961 ret
= relay_end_data_pending(recv_hdr
, cmd
);
1963 case RELAYD_SEND_INDEX
:
1964 ret
= relay_recv_index(recv_hdr
, cmd
);
1966 case RELAYD_STREAMS_SENT
:
1967 ret
= relay_streams_sent(recv_hdr
, cmd
);
1969 case RELAYD_UPDATE_SYNC_INFO
:
1971 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1972 relay_unknown_command(cmd
);
1982 * Handle index for a data stream.
1984 * RCU read side lock MUST be acquired.
1986 * Return 0 on success else a negative value.
1988 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
1991 int ret
= 0, index_created
= 0;
1992 uint64_t stream_id
, data_offset
;
1993 struct relay_index
*index
, *wr_index
= NULL
;
1997 stream_id
= stream
->stream_handle
;
1998 /* Get data offset because we are about to update the index. */
1999 data_offset
= htobe64(stream
->tracefile_size_current
);
2002 * Lookup for an existing index for that stream id/sequence number. If on
2003 * exists, the control thread already received the data for it thus we need
2004 * to write it on disk.
2006 index
= relay_index_find(stream_id
, net_seq_num
);
2008 /* A successful creation will add the object to the HT. */
2009 index
= relay_index_create(stream_id
, net_seq_num
);
2017 if (rotate_index
|| stream
->index_fd
< 0) {
2018 index
->to_close_fd
= stream
->index_fd
;
2019 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
2020 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
2021 stream
->tracefile_count_current
);
2023 /* This will close the stream's index fd if one. */
2024 relay_index_free_safe(index
);
2027 stream
->index_fd
= ret
;
2029 index
->fd
= stream
->index_fd
;
2030 index
->index_data
.offset
= data_offset
;
2032 if (index_created
) {
2034 * Try to add the relay index object to the hash table. If an object
2035 * already exist, destroy back the index created and set the data.
2037 relay_index_add(index
, &wr_index
);
2039 /* Copy back data from the created index. */
2040 wr_index
->fd
= index
->fd
;
2041 wr_index
->to_close_fd
= index
->to_close_fd
;
2042 wr_index
->index_data
.offset
= data_offset
;
2046 /* The index already exists so write it on disk. */
2050 /* Do we have a writable ready index to write on disk. */
2052 ret
= relay_index_write(wr_index
->fd
, wr_index
);
2056 stream
->total_index_received
++;
2064 * relay_process_data: Process the data received on the data socket
2067 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
2069 int ret
= 0, rotate_index
= 0;
2071 struct relay_stream
*stream
;
2072 struct lttcomm_relayd_data_hdr data_hdr
;
2074 uint64_t net_seq_num
;
2076 struct relay_session
*session
;
2078 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
2079 sizeof(struct lttcomm_relayd_data_hdr
), 0);
2082 /* Orderly shutdown. Not necessary to print an error. */
2083 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2085 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
2091 stream_id
= be64toh(data_hdr
.stream_id
);
2094 stream
= stream_find_by_id(relay_streams_ht
, stream_id
);
2097 goto end_rcu_unlock
;
2100 session
= session_find_by_id(sessions_ht
, stream
->session_id
);
2103 data_size
= be32toh(data_hdr
.data_size
);
2104 if (data_buffer_size
< data_size
) {
2107 tmp_data_ptr
= realloc(data_buffer
, data_size
);
2108 if (!tmp_data_ptr
) {
2109 ERR("Allocating data buffer");
2112 goto end_rcu_unlock
;
2114 data_buffer
= tmp_data_ptr
;
2115 data_buffer_size
= data_size
;
2117 memset(data_buffer
, 0, data_size
);
2119 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
2121 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
2122 data_size
, stream_id
, net_seq_num
);
2123 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
2126 /* Orderly shutdown. Not necessary to print an error. */
2127 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
2130 goto end_rcu_unlock
;
2133 /* Check if a rotation is needed. */
2134 if (stream
->tracefile_size
> 0 &&
2135 (stream
->tracefile_size_current
+ data_size
) >
2136 stream
->tracefile_size
) {
2137 struct relay_viewer_stream
*vstream
;
2140 new_id
= (stream
->tracefile_count_current
+ 1) %
2141 stream
->tracefile_count
;
2143 * When we wrap-around back to 0, we start overwriting old
2146 if (!stream
->tracefile_overwrite
&& new_id
== 0) {
2147 stream
->tracefile_overwrite
= 1;
2149 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
2150 if (stream
->tracefile_overwrite
) {
2151 stream
->oldest_tracefile_id
=
2152 (stream
->oldest_tracefile_id
+ 1) %
2153 stream
->tracefile_count
;
2155 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
2158 * The viewer is reading a file about to be
2159 * overwritten. Close the FDs it is
2160 * currently using and let it handle the fault.
2162 if (vstream
->tracefile_count_current
== new_id
) {
2163 pthread_mutex_lock(&vstream
->overwrite_lock
);
2164 vstream
->abort_flag
= 1;
2165 pthread_mutex_unlock(&vstream
->overwrite_lock
);
2166 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2167 stream
->channel_name
, new_id
);
2168 } else if (vstream
->tracefile_count_current
==
2169 stream
->tracefile_count_current
) {
2171 * The reader and writer were in the
2172 * same trace file, inform the viewer
2173 * that no new index will ever be added
2176 vstream
->close_write_flag
= 1;
2179 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
2180 stream
->tracefile_size
, stream
->tracefile_count
,
2181 relayd_uid
, relayd_gid
, stream
->fd
,
2182 &(stream
->tracefile_count_current
), &stream
->fd
);
2183 stream
->total_index_received
= 0;
2184 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
2186 ERR("Rotating stream output file");
2187 goto end_rcu_unlock
;
2189 /* Reset current size because we just perform a stream rotation. */
2190 stream
->tracefile_size_current
= 0;
2195 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2196 * index are NOT supported.
2198 if (session
->minor
>= 4 && !session
->snapshot
) {
2199 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2201 goto end_rcu_unlock
;
2205 /* Write data to stream output fd. */
2206 size_ret
= lttng_write(stream
->fd
, data_buffer
, data_size
);
2207 if (size_ret
< data_size
) {
2208 ERR("Relay error writing data to file");
2210 goto end_rcu_unlock
;
2213 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2214 ret
, stream
->stream_handle
);
2216 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2218 goto end_rcu_unlock
;
2220 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2222 stream
->prev_seq
= net_seq_num
;
2224 try_close_stream(session
, stream
);
2233 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2237 lttng_poll_del(events
, pollfd
);
2239 ret
= close(pollfd
);
2241 ERR("Closing pollfd %d", pollfd
);
2246 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2247 struct lttng_ht
*relay_connections_ht
)
2249 struct relay_command
*relay_connection
;
2252 relay_connection
= zmalloc(sizeof(struct relay_command
));
2253 if (relay_connection
== NULL
) {
2254 PERROR("Relay command zmalloc");
2257 ret
= lttng_read(fd
, relay_connection
, sizeof(struct relay_command
));
2258 if (ret
< sizeof(struct relay_command
)) {
2259 PERROR("read relay cmd pipe");
2262 CDS_INIT_LIST_HEAD(&relay_connection
->recv_head
);
2264 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2265 (unsigned long) relay_connection
->sock
->fd
);
2267 lttng_ht_add_unique_ulong(relay_connections_ht
,
2268 &relay_connection
->sock_n
);
2270 return lttng_poll_add(events
,
2271 relay_connection
->sock
->fd
,
2272 LPOLLIN
| LPOLLRDHUP
);
2275 free(relay_connection
);
2281 void deferred_free_connection(struct rcu_head
*head
)
2283 struct relay_command
*relay_connection
=
2284 caa_container_of(head
, struct relay_command
, rcu_node
);
2286 lttcomm_destroy_sock(relay_connection
->sock
);
2287 free(relay_connection
);
2291 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2292 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2293 struct lttng_ht
*sessions_ht
)
2297 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2300 if (relay_connection
->type
== RELAY_CONTROL
) {
2301 struct relay_stream
*stream
, *tmp_stream
;
2303 /* Clean up recv list. */
2304 cds_list_for_each_entry_safe(stream
, tmp_stream
,
2305 &relay_connection
->recv_head
, recv_list
) {
2306 cds_list_del(&stream
->recv_list
);
2309 relay_delete_session(relay_connection
, sessions_ht
);
2313 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
2317 * This thread does the actual work
2320 void *relay_thread_worker(void *data
)
2322 int ret
, err
= -1, last_seen_data_fd
= -1;
2324 struct relay_command
*relay_connection
;
2325 struct lttng_poll_event events
;
2326 struct lttng_ht
*relay_connections_ht
;
2327 struct lttng_ht_node_ulong
*node
;
2328 struct lttng_ht_iter iter
;
2329 struct lttcomm_relayd_hdr recv_hdr
;
2330 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2331 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2333 DBG("[thread] Relay worker started");
2335 rcu_register_thread();
2337 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2339 if (testpoint(relayd_thread_worker
)) {
2340 goto error_testpoint
;
2343 health_code_update();
2345 /* table of connections indexed on socket */
2346 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2347 if (!relay_connections_ht
) {
2348 goto relay_connections_ht_error
;
2351 /* Tables of received indexes indexed by index handle and net_seq_num. */
2352 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2354 goto indexes_ht_error
;
2357 ret
= create_thread_poll_set(&events
, 2);
2359 goto error_poll_create
;
2362 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2369 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2371 health_code_update();
2373 /* Infinite blocking call, waiting for transmission */
2374 DBG3("Relayd worker thread polling...");
2375 health_poll_entry();
2376 ret
= lttng_poll_wait(&events
, -1);
2380 * Restart interrupted system call.
2382 if (errno
== EINTR
) {
2391 * Process control. The control connection is prioritised so we don't
2392 * starve it with high throughout put tracing data on the data
2395 for (i
= 0; i
< nb_fd
; i
++) {
2396 /* Fetch once the poll data */
2397 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2398 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2400 health_code_update();
2402 /* Thread quit pipe has been closed. Killing thread. */
2403 ret
= check_thread_quit_pipe(pollfd
, revents
);
2409 /* Inspect the relay cmd pipe for new connection */
2410 if (pollfd
== relay_cmd_pipe
[0]) {
2411 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2412 ERR("Relay pipe error");
2414 } else if (revents
& LPOLLIN
) {
2415 DBG("Relay command received");
2416 ret
= relay_add_connection(relay_cmd_pipe
[0],
2417 &events
, relay_connections_ht
);
2422 } else if (revents
) {
2424 lttng_ht_lookup(relay_connections_ht
,
2425 (void *)((unsigned long) pollfd
),
2427 node
= lttng_ht_iter_get_node_ulong(&iter
);
2429 DBG2("Relay sock %d not found", pollfd
);
2433 relay_connection
= caa_container_of(node
,
2434 struct relay_command
, sock_n
);
2436 if (revents
& (LPOLLERR
)) {
2438 relay_cleanup_poll_connection(&events
, pollfd
);
2439 relay_del_connection(relay_connections_ht
,
2440 &iter
, relay_connection
, sessions_ht
);
2441 if (last_seen_data_fd
== pollfd
) {
2442 last_seen_data_fd
= last_notdel_data_fd
;
2444 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2445 DBG("Socket %d hung up", pollfd
);
2446 relay_cleanup_poll_connection(&events
, pollfd
);
2447 relay_del_connection(relay_connections_ht
,
2448 &iter
, relay_connection
, sessions_ht
);
2449 if (last_seen_data_fd
== pollfd
) {
2450 last_seen_data_fd
= last_notdel_data_fd
;
2452 } else if (revents
& LPOLLIN
) {
2453 /* control socket */
2454 if (relay_connection
->type
== RELAY_CONTROL
) {
2455 ret
= relay_connection
->sock
->ops
->recvmsg(
2456 relay_connection
->sock
, &recv_hdr
,
2457 sizeof(struct lttcomm_relayd_hdr
), 0);
2458 /* connection closed */
2460 relay_cleanup_poll_connection(&events
, pollfd
);
2461 relay_del_connection(relay_connections_ht
,
2462 &iter
, relay_connection
, sessions_ht
);
2463 DBG("Control connection closed with %d", pollfd
);
2465 if (relay_connection
->session
) {
2466 DBG2("Relay worker receiving data for session : %" PRIu64
,
2467 relay_connection
->session
->id
);
2469 ret
= relay_process_control(&recv_hdr
,
2470 relay_connection
, relay_ctx
);
2472 /* Clear the session on error. */
2473 relay_cleanup_poll_connection(&events
, pollfd
);
2474 relay_del_connection(relay_connections_ht
,
2475 &iter
, relay_connection
, sessions_ht
);
2476 DBG("Connection closed with %d", pollfd
);
2482 * Flag the last seen data fd not deleted. It will be
2483 * used as the last seen fd if any fd gets deleted in
2486 last_notdel_data_fd
= pollfd
;
2494 * The last loop handled a control request, go back to poll to make
2495 * sure we prioritise the control socket.
2501 if (last_seen_data_fd
>= 0) {
2502 for (i
= 0; i
< nb_fd
; i
++) {
2503 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2505 health_code_update();
2507 if (last_seen_data_fd
== pollfd
) {
2514 /* Process data connection. */
2515 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2516 /* Fetch the poll data. */
2517 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2518 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2520 health_code_update();
2522 /* Skip the command pipe. It's handled in the first loop. */
2523 if (pollfd
== relay_cmd_pipe
[0]) {
2529 lttng_ht_lookup(relay_connections_ht
,
2530 (void *)((unsigned long) pollfd
),
2532 node
= lttng_ht_iter_get_node_ulong(&iter
);
2534 /* Skip it. Might be removed before. */
2538 relay_connection
= caa_container_of(node
,
2539 struct relay_command
, sock_n
);
2541 if (revents
& LPOLLIN
) {
2542 if (relay_connection
->type
!= RELAY_DATA
) {
2546 ret
= relay_process_data(relay_connection
,
2548 /* connection closed */
2550 relay_cleanup_poll_connection(&events
, pollfd
);
2551 relay_del_connection(relay_connections_ht
,
2552 &iter
, relay_connection
, sessions_ht
);
2553 DBG("Data connection closed with %d", pollfd
);
2555 * Every goto restart call sets the last seen fd where
2556 * here we don't really care since we gracefully
2557 * continue the loop after the connection is deleted.
2560 /* Keep last seen port. */
2561 last_seen_data_fd
= pollfd
;
2569 last_seen_data_fd
= -1;
2572 /* Normal exit, no error */
2577 lttng_poll_clean(&events
);
2579 /* empty the hash table and free the memory */
2581 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2582 health_code_update();
2584 node
= lttng_ht_iter_get_node_ulong(&iter
);
2586 relay_connection
= caa_container_of(node
,
2587 struct relay_command
, sock_n
);
2588 relay_del_connection(relay_connections_ht
,
2589 &iter
, relay_connection
, sessions_ht
);
2594 lttng_ht_destroy(indexes_ht
);
2596 lttng_ht_destroy(relay_connections_ht
);
2597 relay_connections_ht_error
:
2598 /* Close relay cmd pipes */
2599 utils_close_pipe(relay_cmd_pipe
);
2601 DBG("Thread exited with error");
2603 DBG("Worker thread cleanup complete");
2608 ERR("Health error occurred in %s", __func__
);
2610 health_unregister(health_relayd
);
2611 rcu_unregister_thread();
2617 * Create the relay command pipe to wake thread_manage_apps.
2618 * Closed in cleanup().
2620 static int create_relay_cmd_pipe(void)
2624 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2632 int main(int argc
, char **argv
)
2636 struct relay_local_data
*relay_ctx
;
2638 /* Parse arguments */
2640 if ((ret
= parse_args(argc
, argv
)) < 0) {
2644 if ((ret
= set_signal_handler()) < 0) {
2648 /* Try to create directory if -o, --output is specified. */
2649 if (opt_output_path
) {
2650 if (*opt_output_path
!= '/') {
2651 ERR("Please specify an absolute path for -o, --output PATH");
2655 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2657 ERR("Unable to create %s", opt_output_path
);
2663 if (opt_daemon
|| opt_background
) {
2666 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2673 * We are in the child. Make sure all other file
2674 * descriptors are closed, in case we are called with
2675 * more opened file descriptors than the standard ones.
2677 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2682 /* Create thread quit pipe */
2683 if ((ret
= init_thread_quit_pipe()) < 0) {
2687 /* We need those values for the file/dir creation. */
2688 relayd_uid
= getuid();
2689 relayd_gid
= getgid();
2691 /* Check if daemon is UID = 0 */
2692 if (relayd_uid
== 0) {
2693 if (control_uri
->port
< 1024 || data_uri
->port
< 1024 ||
2694 live_uri
->port
< 1024) {
2695 ERR("Need to be root to use ports < 1024");
2701 /* Setup the thread apps communication pipe. */
2702 if ((ret
= create_relay_cmd_pipe()) < 0) {
2706 /* Init relay command queue. */
2707 cds_wfq_init(&relay_cmd_queue
.queue
);
2709 /* Set up max poll set size */
2710 lttng_poll_set_max_size();
2712 /* Initialize communication library */
2714 lttcomm_inet_init();
2716 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2718 PERROR("relay_ctx");
2722 /* tables of sessions indexed by session ID */
2723 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2724 if (!relay_ctx
->sessions_ht
) {
2725 goto exit_relay_ctx_sessions
;
2728 /* tables of streams indexed by stream ID */
2729 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2730 if (!relay_streams_ht
) {
2731 goto exit_relay_ctx_streams
;
2734 /* tables of streams indexed by stream ID */
2735 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2736 if (!viewer_streams_ht
) {
2737 goto exit_relay_ctx_viewer_streams
;
2740 /* Initialize thread health monitoring */
2741 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2742 if (!health_relayd
) {
2743 PERROR("health_app_create error");
2744 goto exit_health_app_create
;
2747 ret
= utils_create_pipe(health_quit_pipe
);
2749 goto error_health_pipe
;
2752 /* Create thread to manage the client socket */
2753 ret
= pthread_create(&health_thread
, NULL
,
2754 thread_manage_health
, (void *) NULL
);
2756 PERROR("pthread_create health");
2760 /* Setup the dispatcher thread */
2761 ret
= pthread_create(&dispatcher_thread
, NULL
,
2762 relay_thread_dispatcher
, (void *) NULL
);
2764 PERROR("pthread_create dispatcher");
2765 goto exit_dispatcher
;
2768 /* Setup the worker thread */
2769 ret
= pthread_create(&worker_thread
, NULL
,
2770 relay_thread_worker
, (void *) relay_ctx
);
2772 PERROR("pthread_create worker");
2776 /* Setup the listener thread */
2777 ret
= pthread_create(&listener_thread
, NULL
,
2778 relay_thread_listener
, (void *) NULL
);
2780 PERROR("pthread_create listener");
2784 ret
= live_start_threads(live_uri
, relay_ctx
);
2786 ERR("Starting live viewer threads");
2791 ret
= pthread_join(listener_thread
, &status
);
2793 PERROR("pthread_join");
2794 goto error
; /* join error, exit without cleanup */
2798 ret
= pthread_join(worker_thread
, &status
);
2800 PERROR("pthread_join");
2801 goto error
; /* join error, exit without cleanup */
2805 ret
= pthread_join(dispatcher_thread
, &status
);
2807 PERROR("pthread_join");
2808 goto error
; /* join error, exit without cleanup */
2812 ret
= pthread_join(health_thread
, &status
);
2814 PERROR("pthread_join health thread");
2815 goto error
; /* join error, exit without cleanup */
2819 * Stop live threads only after joining other threads.
2821 live_stop_threads();
2824 utils_close_pipe(health_quit_pipe
);
2827 health_app_destroy(health_relayd
);
2829 exit_health_app_create
:
2830 lttng_ht_destroy(viewer_streams_ht
);
2832 exit_relay_ctx_viewer_streams
:
2833 lttng_ht_destroy(relay_streams_ht
);
2835 exit_relay_ctx_streams
:
2836 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2838 exit_relay_ctx_sessions
: