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
;
72 * Quit pipe for all threads. This permits a single cancellation point
73 * for all threads when receiving an event on the pipe.
75 static int thread_quit_pipe
[2] = { -1, -1 };
78 * This pipe is used to inform the worker thread that a command is queued and
79 * ready to be processed.
81 static int relay_cmd_pipe
[2] = { -1, -1 };
83 /* Shared between threads */
84 static int dispatch_thread_exit
;
86 static pthread_t listener_thread
;
87 static pthread_t dispatcher_thread
;
88 static pthread_t worker_thread
;
90 static uint64_t last_relay_stream_id
;
91 static uint64_t last_relay_session_id
;
94 * Relay command queue.
96 * The relay_thread_listener and relay_thread_dispatcher communicate with this
99 static struct relay_cmd_queue relay_cmd_queue
;
101 /* buffer allocated at startup, used to store the trace data */
102 static char *data_buffer
;
103 static unsigned int data_buffer_size
;
105 /* We need those values for the file/dir creation. */
106 static uid_t relayd_uid
;
107 static gid_t relayd_gid
;
109 /* Global relay stream hash table. */
110 struct lttng_ht
*relay_streams_ht
;
112 /* Global relay viewer stream hash table. */
113 struct lttng_ht
*viewer_streams_ht
;
115 /* Global hash table that stores relay index object. */
116 struct lttng_ht
*indexes_ht
;
118 /* Relayd health monitoring */
119 static struct health_app
*health_relayd
;
122 * usage function on stderr
127 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
128 fprintf(stderr
, " -h, --help Display this usage.\n");
129 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
130 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
131 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
132 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
133 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
137 int parse_args(int argc
, char **argv
)
141 char *default_address
;
143 static struct option long_options
[] = {
144 { "control-port", 1, 0, 'C', },
145 { "data-port", 1, 0, 'D', },
146 { "daemonize", 0, 0, 'd', },
147 { "help", 0, 0, 'h', },
148 { "output", 1, 0, 'o', },
149 { "verbose", 0, 0, 'v', },
154 int option_index
= 0;
155 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
156 long_options
, &option_index
);
163 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
165 fprintf(stderr
, " with arg %s\n", optarg
);
169 ret
= uri_parse(optarg
, &control_uri
);
171 ERR("Invalid control URI specified");
174 if (control_uri
->port
== 0) {
175 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
179 ret
= uri_parse(optarg
, &data_uri
);
181 ERR("Invalid data URI specified");
184 if (data_uri
->port
== 0) {
185 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
195 ret
= asprintf(&opt_output_path
, "%s", optarg
);
197 PERROR("asprintf opt_output_path");
202 /* Verbose level can increase using multiple -v */
203 lttng_opt_verbose
+= 1;
206 /* Unknown option or other error.
207 * Error is printed by getopt, just return */
213 /* assign default values */
214 if (control_uri
== NULL
) {
215 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
216 DEFAULT_NETWORK_CONTROL_PORT
);
218 PERROR("asprintf default data address");
222 ret
= uri_parse(default_address
, &control_uri
);
223 free(default_address
);
225 ERR("Invalid control URI specified");
229 if (data_uri
== NULL
) {
230 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
231 DEFAULT_NETWORK_DATA_PORT
);
233 PERROR("asprintf default data address");
237 ret
= uri_parse(default_address
, &data_uri
);
238 free(default_address
);
240 ERR("Invalid data URI specified");
244 if (live_uri
== NULL
) {
245 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
246 DEFAULT_NETWORK_VIEWER_PORT
);
248 PERROR("asprintf default viewer control address");
252 ret
= uri_parse(default_address
, &live_uri
);
253 free(default_address
);
255 ERR("Invalid viewer control URI specified");
272 /* free the dynamically allocated opt_output_path */
273 free(opt_output_path
);
275 /* Close thread quit pipes */
276 utils_close_pipe(thread_quit_pipe
);
278 uri_free(control_uri
);
283 * Write to writable pipe used to notify a thread.
286 int notify_thread_pipe(int wpipe
)
291 ret
= write(wpipe
, "!", 1);
292 } while (ret
< 0 && errno
== EINTR
);
293 if (ret
< 0 || ret
!= 1) {
294 PERROR("write poll pipe");
301 * Stop all threads by closing the thread quit pipe.
304 void stop_threads(void)
308 /* Stopping all threads */
309 DBG("Terminating all threads");
310 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
312 ERR("write error on thread quit pipe");
315 /* Dispatch thread */
316 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
317 futex_nto1_wake(&relay_cmd_queue
.futex
);
321 * Signal handler for the daemon
323 * Simply stop all worker threads, leaving main() return gracefully after
324 * joining all threads and calling cleanup().
327 void sighandler(int sig
)
331 DBG("SIGPIPE caught");
334 DBG("SIGINT caught");
338 DBG("SIGTERM caught");
347 * Setup signal handler for :
348 * SIGINT, SIGTERM, SIGPIPE
351 int set_signal_handler(void)
357 if ((ret
= sigemptyset(&sigset
)) < 0) {
358 PERROR("sigemptyset");
362 sa
.sa_handler
= sighandler
;
365 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
370 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
375 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
380 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
386 * Init thread quit pipe.
388 * Return -1 on error or 0 if all pipes are created.
391 int init_thread_quit_pipe(void)
395 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
401 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
404 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
408 if (events
== NULL
|| size
== 0) {
413 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
419 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
431 * Check if the thread quit pipe was triggered.
433 * Return 1 if it was triggered else 0;
436 int check_thread_quit_pipe(int fd
, uint32_t events
)
438 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
446 * Create and init socket from uri.
449 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
452 struct lttcomm_sock
*sock
= NULL
;
454 sock
= lttcomm_alloc_sock_from_uri(uri
);
456 ERR("Allocating socket");
460 ret
= lttcomm_create_sock(sock
);
464 DBG("Listening on sock %d", sock
->fd
);
466 ret
= sock
->ops
->bind(sock
);
471 ret
= sock
->ops
->listen(sock
, -1);
481 lttcomm_destroy_sock(sock
);
487 * Return nonzero if stream needs to be closed.
490 int close_stream_check(struct relay_stream
*stream
)
493 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
495 * We are about to close the stream so set the data pending flag to 1
496 * which will make the end data pending command skip the stream which
497 * is now closed and ready. Note that after proceeding to a file close,
498 * the written file is ready for reading.
500 stream
->data_pending_check_done
= 1;
507 * This thread manages the listening for new connections on the network
510 void *relay_thread_listener(void *data
)
512 int i
, ret
, pollfd
, err
= -1;
514 uint32_t revents
, nb_fd
;
515 struct lttng_poll_event events
;
516 struct lttcomm_sock
*control_sock
, *data_sock
;
518 DBG("[thread] Relay listener started");
520 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LISTENER
);
522 control_sock
= relay_init_sock(control_uri
);
524 goto error_sock_control
;
527 data_sock
= relay_init_sock(data_uri
);
529 goto error_sock_relay
;
533 * Pass 3 as size here for the thread quit pipe, control and data socket.
535 ret
= create_thread_poll_set(&events
, 3);
537 goto error_create_poll
;
540 /* Add the control socket */
541 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
546 /* Add the data socket */
547 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
553 DBG("Listener accepting connections");
556 ret
= lttng_poll_wait(&events
, -1);
559 * Restart interrupted system call.
561 if (errno
== EINTR
) {
569 DBG("Relay new connection received");
570 for (i
= 0; i
< nb_fd
; i
++) {
571 /* Fetch once the poll data */
572 revents
= LTTNG_POLL_GETEV(&events
, i
);
573 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
575 /* Thread quit pipe has been closed. Killing thread. */
576 ret
= check_thread_quit_pipe(pollfd
, revents
);
582 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
583 ERR("socket poll error");
585 } else if (revents
& LPOLLIN
) {
587 * Get allocated in this thread,
588 * enqueued to a global queue, dequeued
589 * and freed in the worker thread.
591 struct relay_command
*relay_cmd
;
592 struct lttcomm_sock
*newsock
;
594 relay_cmd
= zmalloc(sizeof(struct relay_command
));
595 if (relay_cmd
== NULL
) {
596 PERROR("relay command zmalloc");
600 if (pollfd
== data_sock
->fd
) {
601 newsock
= data_sock
->ops
->accept(data_sock
);
603 PERROR("accepting data sock");
607 relay_cmd
->type
= RELAY_DATA
;
608 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
610 assert(pollfd
== control_sock
->fd
);
611 newsock
= control_sock
->ops
->accept(control_sock
);
613 PERROR("accepting control sock");
617 relay_cmd
->type
= RELAY_CONTROL
;
618 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
620 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
623 PERROR("setsockopt inet");
624 lttcomm_destroy_sock(newsock
);
628 relay_cmd
->sock
= newsock
;
630 * Lock free enqueue the request.
632 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
635 * Wake the dispatch queue futex. Implicit memory
636 * barrier with the exchange in cds_wfq_enqueue.
638 futex_nto1_wake(&relay_cmd_queue
.futex
);
646 lttng_poll_clean(&events
);
648 if (data_sock
->fd
>= 0) {
649 ret
= data_sock
->ops
->close(data_sock
);
654 lttcomm_destroy_sock(data_sock
);
656 if (control_sock
->fd
>= 0) {
657 ret
= control_sock
->ops
->close(control_sock
);
662 lttcomm_destroy_sock(control_sock
);
665 DBG("Thread exited with error");
667 health_unregister(health_relayd
);
668 DBG("Relay listener thread cleanup complete");
674 * This thread manages the dispatching of the requests to worker threads
677 void *relay_thread_dispatcher(void *data
)
680 struct cds_wfq_node
*node
;
681 struct relay_command
*relay_cmd
= NULL
;
683 DBG("[thread] Relay dispatcher started");
685 health_register(health_relayd
, HEALTH_RELAYD_TYPE_DISPATCHER
);
687 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
688 /* Atomically prepare the queue futex */
689 futex_nto1_prepare(&relay_cmd_queue
.futex
);
692 /* Dequeue commands */
693 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
695 DBG("Woken up but nothing in the relay command queue");
696 /* Continue thread execution */
700 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
701 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
704 * Inform worker thread of the new request. This
705 * call is blocking so we can be assured that the data will be read
706 * at some point in time or wait to the end of the world :)
709 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
710 sizeof(struct relay_command
));
711 } while (ret
< 0 && errno
== EINTR
);
713 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
714 PERROR("write cmd pipe");
717 } while (node
!= NULL
);
719 /* Futex wait on queue. Blocking call on futex() */
720 futex_nto1_wait(&relay_cmd_queue
.futex
);
724 health_unregister(health_relayd
);
725 DBG("Dispatch thread dying");
731 * Get stream from stream id.
732 * Need to be called with RCU read-side lock held.
734 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
736 struct lttng_ht_node_ulong
*node
;
737 struct lttng_ht_iter iter
;
738 struct relay_stream
*ret
;
740 lttng_ht_lookup(relay_streams_ht
,
741 (void *)((unsigned long) stream_id
),
743 node
= lttng_ht_iter_get_node_ulong(&iter
);
745 DBG("Relay stream %" PRIu64
" not found", stream_id
);
750 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
757 void deferred_free_stream(struct rcu_head
*head
)
759 struct relay_stream
*stream
=
760 caa_container_of(head
, struct relay_stream
, rcu_node
);
762 ctf_trace_try_destroy(stream
->ctf_trace
);
764 free(stream
->path_name
);
765 free(stream
->channel_name
);
770 void deferred_free_session(struct rcu_head
*head
)
772 struct relay_session
*session
=
773 caa_container_of(head
, struct relay_session
, rcu_node
);
778 * Close a given stream. The stream is freed using a call RCU.
780 * RCU read side lock MUST be acquired. If NO close_stream_check() was called
781 * BEFORE the stream lock MUST be acquired.
783 static void destroy_stream(struct relay_stream
*stream
,
784 struct lttng_ht
*ctf_traces_ht
)
787 struct relay_viewer_stream
*vstream
;
788 struct lttng_ht_iter iter
;
792 delret
= close(stream
->fd
);
794 PERROR("close stream");
797 if (stream
->index_fd
>= 0) {
798 delret
= close(stream
->index_fd
);
800 PERROR("close stream index_fd");
804 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
807 * Set the last good value into the viewer stream. This is done
808 * right before the stream gets deleted from the hash table. The
809 * lookup failure on the live thread side of a stream indicates
810 * that the viewer stream index received value should be used.
812 vstream
->total_index_received
= stream
->total_index_received
;
815 /* Cleanup index of that stream. */
816 relay_index_destroy_by_stream_id(stream
->stream_handle
);
818 iter
.iter
.node
= &stream
->stream_n
.node
;
819 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
821 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
822 delret
= lttng_ht_del(ctf_traces_ht
, &iter
);
824 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
825 DBG("Closed tracefile %d from close stream", stream
->fd
);
829 * relay_delete_session: Free all memory associated with a session and
833 void relay_delete_session(struct relay_command
*cmd
,
834 struct lttng_ht
*sessions_ht
)
836 struct lttng_ht_iter iter
;
837 struct lttng_ht_node_ulong
*node
;
838 struct relay_stream
*stream
;
845 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
848 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
849 node
= lttng_ht_iter_get_node_ulong(&iter
);
853 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
854 if (stream
->session
== cmd
->session
) {
855 destroy_stream(stream
, cmd
->ctf_traces_ht
);
859 /* Make this session not visible anymore. */
860 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
861 ret
= lttng_ht_del(sessions_ht
, &iter
);
863 call_rcu(&cmd
->session
->rcu_node
, deferred_free_session
);
868 * Copy index data from the control port to a given index object.
870 static void copy_index_control_data(struct relay_index
*index
,
871 struct lttcomm_relayd_index
*data
)
877 * The index on disk is encoded in big endian, so we don't need to convert
878 * the data received on the network. The data_offset value is NEVER
879 * modified here and is updated by the data thread.
881 index
->index_data
.packet_size
= data
->packet_size
;
882 index
->index_data
.content_size
= data
->content_size
;
883 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
884 index
->index_data
.timestamp_end
= data
->timestamp_end
;
885 index
->index_data
.events_discarded
= data
->events_discarded
;
886 index
->index_data
.stream_id
= data
->stream_id
;
890 * Handle the RELAYD_CREATE_SESSION command.
892 * On success, send back the session id or else return a negative value.
895 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
896 struct relay_command
*cmd
,
897 struct lttng_ht
*sessions_ht
)
899 int ret
= 0, send_ret
;
900 struct relay_session
*session
;
901 struct lttcomm_relayd_status_session reply
;
906 memset(&reply
, 0, sizeof(reply
));
908 session
= zmalloc(sizeof(struct relay_session
));
909 if (session
== NULL
) {
910 PERROR("relay session zmalloc");
915 session
->id
= ++last_relay_session_id
;
916 session
->sock
= cmd
->sock
;
917 session
->minor
= cmd
->minor
;
918 session
->major
= cmd
->major
;
919 cmd
->session
= session
;
921 reply
.session_id
= htobe64(session
->id
);
923 switch (cmd
->minor
) {
924 case 4: /* LTTng sessiond 2.4 */
926 ret
= cmd_create_session_2_4(cmd
, session
);
930 lttng_ht_node_init_ulong(&session
->session_n
,
931 (unsigned long) session
->id
);
932 lttng_ht_add_unique_ulong(sessions_ht
,
933 &session
->session_n
);
935 DBG("Created session %" PRIu64
, session
->id
);
939 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
941 reply
.ret_code
= htobe32(LTTNG_OK
);
944 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
946 ERR("Relayd sending session id");
954 * relay_add_stream: allocate a new stream for a session
957 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
958 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
960 struct relay_session
*session
= cmd
->session
;
961 struct relay_stream
*stream
= NULL
;
962 struct lttcomm_relayd_status_stream reply
;
965 if (!session
|| cmd
->version_check_done
== 0) {
966 ERR("Trying to add a stream before version check");
971 stream
= zmalloc(sizeof(struct relay_stream
));
972 if (stream
== NULL
) {
973 PERROR("relay stream zmalloc");
978 switch (cmd
->minor
) {
979 case 1: /* LTTng sessiond 2.1 */
980 ret
= cmd_recv_stream_2_1(cmd
, stream
);
982 case 2: /* LTTng sessiond 2.2 */
984 ret
= cmd_recv_stream_2_2(cmd
, stream
);
988 goto err_free_stream
;
992 stream
->stream_handle
= ++last_relay_stream_id
;
993 stream
->prev_seq
= -1ULL;
994 stream
->session
= session
;
995 stream
->index_fd
= -1;
996 stream
->read_index_fd
= -1;
997 stream
->ctf_trace
= NULL
;
998 pthread_mutex_init(&stream
->lock
, NULL
);
1000 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
1002 ERR("relay creating output directory");
1007 * No need to use run_as API here because whatever we receives, the relayd
1008 * uses its own credentials for the stream files.
1010 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
1011 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1013 ERR("Create output file");
1017 if (stream
->tracefile_size
) {
1018 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1020 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1023 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1024 stream
->metadata_flag
= 1;
1026 * When we receive a new metadata stream, we create a new
1027 * ctf_trace and we assign this ctf_trace to all streams with
1030 * If later on we receive a new stream for the same ctf_trace,
1031 * we copy the information from the first hit in the HT to the
1034 stream
->ctf_trace
= ctf_trace_create();
1035 if (!stream
->ctf_trace
) {
1039 stream
->ctf_trace
->refcount
++;
1040 stream
->ctf_trace
->metadata_stream
= stream
;
1042 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1044 lttng_ht_node_init_ulong(&stream
->stream_n
,
1045 (unsigned long) stream
->stream_handle
);
1046 lttng_ht_add_unique_ulong(relay_streams_ht
,
1049 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1050 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1052 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1053 stream
->stream_handle
);
1056 reply
.handle
= htobe64(stream
->stream_handle
);
1057 /* send the session id to the client or a negative return code on error */
1059 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1060 /* stream was not properly added to the ht, so free it */
1063 reply
.ret_code
= htobe32(LTTNG_OK
);
1066 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1067 sizeof(struct lttcomm_relayd_status_stream
), 0);
1069 ERR("Relay sending stream id");
1078 free(stream
->path_name
);
1079 free(stream
->channel_name
);
1085 * relay_close_stream: close a specific stream
1088 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1089 struct relay_command
*cmd
)
1092 struct relay_session
*session
= cmd
->session
;
1093 struct lttcomm_relayd_close_stream stream_info
;
1094 struct lttcomm_relayd_generic_reply reply
;
1095 struct relay_stream
*stream
;
1097 DBG("Close stream received");
1099 if (!session
|| cmd
->version_check_done
== 0) {
1100 ERR("Trying to close a stream before version check");
1102 goto end_no_session
;
1105 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1106 sizeof(struct lttcomm_relayd_close_stream
), 0);
1107 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1109 /* Orderly shutdown. Not necessary to print an error. */
1110 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1112 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1115 goto end_no_session
;
1119 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1125 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1126 stream
->close_flag
= 1;
1128 if (close_stream_check(stream
)) {
1129 destroy_stream(stream
, cmd
->ctf_traces_ht
);
1136 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1138 reply
.ret_code
= htobe32(LTTNG_OK
);
1140 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1141 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1143 ERR("Relay sending stream id");
1152 * relay_unknown_command: send -1 if received unknown command
1155 void relay_unknown_command(struct relay_command
*cmd
)
1157 struct lttcomm_relayd_generic_reply reply
;
1160 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1161 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1162 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1164 ERR("Relay sending unknown command");
1169 * relay_start: send an acknowledgment to the client to tell if we are
1170 * ready to receive data. We are ready if a session is established.
1173 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1174 struct relay_command
*cmd
)
1176 int ret
= htobe32(LTTNG_OK
);
1177 struct lttcomm_relayd_generic_reply reply
;
1178 struct relay_session
*session
= cmd
->session
;
1181 DBG("Trying to start the streaming without a session established");
1182 ret
= htobe32(LTTNG_ERR_UNK
);
1185 reply
.ret_code
= ret
;
1186 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1187 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1189 ERR("Relay sending start ack");
1196 * Append padding to the file pointed by the file descriptor fd.
1198 static int write_padding_to_file(int fd
, uint32_t size
)
1207 zeros
= zmalloc(size
);
1208 if (zeros
== NULL
) {
1209 PERROR("zmalloc zeros for padding");
1215 ret
= write(fd
, zeros
, size
);
1216 } while (ret
< 0 && errno
== EINTR
);
1217 if (ret
< 0 || ret
!= size
) {
1218 PERROR("write padding to file");
1228 * relay_recv_metadata: receive the metada for the session.
1231 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1232 struct relay_command
*cmd
)
1234 int ret
= htobe32(LTTNG_OK
);
1235 struct relay_session
*session
= cmd
->session
;
1236 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1237 struct relay_stream
*metadata_stream
;
1238 uint64_t data_size
, payload_size
;
1241 ERR("Metadata sent before version check");
1246 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1247 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1248 ERR("Incorrect data size");
1252 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1254 if (data_buffer_size
< data_size
) {
1255 /* In case the realloc fails, we can free the memory */
1258 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1259 if (!tmp_data_ptr
) {
1260 ERR("Allocating data buffer");
1265 data_buffer
= tmp_data_ptr
;
1266 data_buffer_size
= data_size
;
1268 memset(data_buffer
, 0, data_size
);
1269 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1270 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1271 if (ret
< 0 || ret
!= data_size
) {
1273 /* Orderly shutdown. Not necessary to print an error. */
1274 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1276 ERR("Relay didn't receive the whole metadata");
1281 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1284 metadata_stream
= relay_stream_find_by_id(
1285 be64toh(metadata_struct
->stream_id
));
1286 if (!metadata_stream
) {
1292 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1294 } while (ret
< 0 && errno
== EINTR
);
1295 if (ret
< 0 || ret
!= payload_size
) {
1296 ERR("Relay error writing metadata on file");
1301 ret
= write_padding_to_file(metadata_stream
->fd
,
1302 be32toh(metadata_struct
->padding_size
));
1306 metadata_stream
->ctf_trace
->metadata_received
+=
1307 payload_size
+ be32toh(metadata_struct
->padding_size
);
1309 DBG2("Relay metadata written");
1318 * relay_send_version: send relayd version number
1321 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1322 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1325 struct lttcomm_relayd_version reply
, msg
;
1329 cmd
->version_check_done
= 1;
1331 /* Get version from the other side. */
1332 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1333 if (ret
< 0 || ret
!= sizeof(msg
)) {
1335 /* Orderly shutdown. Not necessary to print an error. */
1336 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1338 ERR("Relay failed to receive the version values.");
1344 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1345 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1347 /* Major versions must be the same */
1348 if (reply
.major
!= be32toh(msg
.major
)) {
1349 DBG("Incompatible major versions (%u vs %u), deleting session",
1350 reply
.major
, be32toh(msg
.major
));
1351 relay_delete_session(cmd
, sessions_ht
);
1356 cmd
->major
= reply
.major
;
1357 /* We adapt to the lowest compatible version */
1358 if (reply
.minor
<= be32toh(msg
.minor
)) {
1359 cmd
->minor
= reply
.minor
;
1361 cmd
->minor
= be32toh(msg
.minor
);
1364 reply
.major
= htobe32(reply
.major
);
1365 reply
.minor
= htobe32(reply
.minor
);
1366 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1367 sizeof(struct lttcomm_relayd_version
), 0);
1369 ERR("Relay sending version");
1372 DBG("Version check done using protocol %u.%u", cmd
->major
,
1380 * Check for data pending for a given stream id from the session daemon.
1383 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1384 struct relay_command
*cmd
)
1386 struct relay_session
*session
= cmd
->session
;
1387 struct lttcomm_relayd_data_pending msg
;
1388 struct lttcomm_relayd_generic_reply reply
;
1389 struct relay_stream
*stream
;
1391 uint64_t last_net_seq_num
, stream_id
;
1393 DBG("Data pending command received");
1395 if (!session
|| cmd
->version_check_done
== 0) {
1396 ERR("Trying to check for data before version check");
1398 goto end_no_session
;
1401 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1402 if (ret
< sizeof(msg
)) {
1404 /* Orderly shutdown. Not necessary to print an error. */
1405 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1407 ERR("Relay didn't receive valid data_pending struct size : %d",
1411 goto end_no_session
;
1414 stream_id
= be64toh(msg
.stream_id
);
1415 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1418 stream
= relay_stream_find_by_id(stream_id
);
1419 if (stream
== NULL
) {
1424 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1425 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1428 /* Avoid wrapping issue */
1429 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1430 /* Data has in fact been written and is NOT pending */
1433 /* Data still being streamed thus pending */
1437 /* Pending check is now done. */
1438 stream
->data_pending_check_done
= 1;
1443 reply
.ret_code
= htobe32(ret
);
1444 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1446 ERR("Relay data pending ret code failed");
1454 * Wait for the control socket to reach a quiescent state.
1456 * Note that for now, when receiving this command from the session daemon, this
1457 * means that every subsequent commands or data received on the control socket
1458 * has been handled. So, this is why we simply return OK here.
1461 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1462 struct relay_command
*cmd
)
1466 struct relay_stream
*stream
;
1467 struct lttng_ht_iter iter
;
1468 struct lttcomm_relayd_quiescent_control msg
;
1469 struct lttcomm_relayd_generic_reply reply
;
1471 DBG("Checking quiescent state on control socket");
1473 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1474 ERR("Trying to check for data before version check");
1476 goto end_no_session
;
1479 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1480 if (ret
< sizeof(msg
)) {
1482 /* Orderly shutdown. Not necessary to print an error. */
1483 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1485 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1489 goto end_no_session
;
1492 stream_id
= be64toh(msg
.stream_id
);
1495 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1497 if (stream
->stream_handle
== stream_id
) {
1498 stream
->data_pending_check_done
= 1;
1499 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1506 reply
.ret_code
= htobe32(LTTNG_OK
);
1507 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1509 ERR("Relay data quiescent control ret code failed");
1517 * Initialize a data pending command. This means that a client is about to ask
1518 * for data pending for each stream he/she holds. Simply iterate over all
1519 * streams of a session and set the data_pending_check_done flag.
1521 * This command returns to the client a LTTNG_OK code.
1524 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1525 struct relay_command
*cmd
)
1528 struct lttng_ht_iter iter
;
1529 struct lttcomm_relayd_begin_data_pending msg
;
1530 struct lttcomm_relayd_generic_reply reply
;
1531 struct relay_stream
*stream
;
1532 uint64_t session_id
;
1537 DBG("Init streams for data pending");
1539 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1540 ERR("Trying to check for data before version check");
1542 goto end_no_session
;
1545 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1546 if (ret
< sizeof(msg
)) {
1548 /* Orderly shutdown. Not necessary to print an error. */
1549 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1551 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1555 goto end_no_session
;
1558 session_id
= be64toh(msg
.session_id
);
1561 * Iterate over all streams to set the begin data pending flag. For now, the
1562 * streams are indexed by stream handle so we have to iterate over all
1563 * streams to find the one associated with the right session_id.
1566 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1568 if (stream
->session
->id
== session_id
) {
1569 stream
->data_pending_check_done
= 0;
1570 DBG("Set begin data pending flag to stream %" PRIu64
,
1571 stream
->stream_handle
);
1576 /* All good, send back reply. */
1577 reply
.ret_code
= htobe32(LTTNG_OK
);
1579 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1581 ERR("Relay begin data pending send reply failed");
1589 * End data pending command. This will check, for a given session id, if each
1590 * stream associated with it has its data_pending_check_done flag set. If not,
1591 * this means that the client lost track of the stream but the data is still
1592 * being streamed on our side. In this case, we inform the client that data is
1595 * Return to the client if there is data in flight or not with a ret_code.
1598 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1599 struct relay_command
*cmd
)
1602 struct lttng_ht_iter iter
;
1603 struct lttcomm_relayd_end_data_pending msg
;
1604 struct lttcomm_relayd_generic_reply reply
;
1605 struct relay_stream
*stream
;
1606 uint64_t session_id
;
1607 uint32_t is_data_inflight
= 0;
1612 DBG("End data pending command");
1614 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1615 ERR("Trying to check for data before version check");
1617 goto end_no_session
;
1620 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1621 if (ret
< sizeof(msg
)) {
1623 /* Orderly shutdown. Not necessary to print an error. */
1624 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1626 ERR("Relay didn't receive valid end data_pending struct size: %d",
1630 goto end_no_session
;
1633 session_id
= be64toh(msg
.session_id
);
1635 /* Iterate over all streams to see if the begin data pending flag is set. */
1637 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1639 if (stream
->session
->id
== session_id
&&
1640 !stream
->data_pending_check_done
) {
1641 is_data_inflight
= 1;
1642 DBG("Data is still in flight for stream %" PRIu64
,
1643 stream
->stream_handle
);
1649 /* All good, send back reply. */
1650 reply
.ret_code
= htobe32(is_data_inflight
);
1652 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1654 ERR("Relay end data pending send reply failed");
1662 * Receive an index for a specific stream.
1664 * Return 0 on success else a negative value.
1667 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1668 struct relay_command
*cmd
)
1670 int ret
, send_ret
, index_created
= 0;
1671 struct relay_session
*session
= cmd
->session
;
1672 struct lttcomm_relayd_index index_info
;
1673 struct relay_index
*index
, *wr_index
= NULL
;
1674 struct lttcomm_relayd_generic_reply reply
;
1675 struct relay_stream
*stream
;
1676 uint64_t net_seq_num
;
1680 DBG("Relay receiving index");
1682 if (!session
|| cmd
->version_check_done
== 0) {
1683 ERR("Trying to close a stream before version check");
1685 goto end_no_session
;
1688 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1689 sizeof(index_info
), 0);
1690 if (ret
< sizeof(index_info
)) {
1692 /* Orderly shutdown. Not necessary to print an error. */
1693 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1695 ERR("Relay didn't receive valid index struct size : %d", ret
);
1698 goto end_no_session
;
1701 net_seq_num
= be64toh(index_info
.net_seq_num
);
1704 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1707 goto end_rcu_unlock
;
1710 /* Live beacon handling */
1711 if (index_info
.packet_size
== 0) {
1712 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1715 * Only flag a stream inactive when it has already received data.
1717 if (stream
->total_index_received
> 0) {
1718 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1721 goto end_rcu_unlock
;
1723 stream
->beacon_ts_end
= -1ULL;
1726 index
= relay_index_find(stream
->stream_handle
, net_seq_num
);
1728 /* A successful creation will add the object to the HT. */
1729 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1731 goto end_rcu_unlock
;
1736 copy_index_control_data(index
, &index_info
);
1738 if (index_created
) {
1740 * Try to add the relay index object to the hash table. If an object
1741 * already exist, destroy back the index created, set the data in this
1742 * object and write it on disk.
1744 relay_index_add(index
, &wr_index
);
1746 copy_index_control_data(wr_index
, &index_info
);
1750 /* The index already exists so write it on disk. */
1754 /* Do we have a writable ready index to write on disk. */
1756 /* Starting at 2.4, create the index file if none available. */
1757 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1758 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1759 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1760 stream
->tracefile_count_current
);
1762 goto end_rcu_unlock
;
1764 stream
->index_fd
= ret
;
1767 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1769 goto end_rcu_unlock
;
1771 stream
->total_index_received
++;
1778 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1780 reply
.ret_code
= htobe32(LTTNG_OK
);
1782 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1784 ERR("Relay sending close index id reply");
1793 * Process the commands received on the control socket
1796 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1797 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
1801 switch (be32toh(recv_hdr
->cmd
)) {
1802 case RELAYD_CREATE_SESSION
:
1803 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
1805 case RELAYD_ADD_STREAM
:
1806 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
1808 case RELAYD_START_DATA
:
1809 ret
= relay_start(recv_hdr
, cmd
);
1811 case RELAYD_SEND_METADATA
:
1812 ret
= relay_recv_metadata(recv_hdr
, cmd
);
1814 case RELAYD_VERSION
:
1815 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
1817 case RELAYD_CLOSE_STREAM
:
1818 ret
= relay_close_stream(recv_hdr
, cmd
);
1820 case RELAYD_DATA_PENDING
:
1821 ret
= relay_data_pending(recv_hdr
, cmd
);
1823 case RELAYD_QUIESCENT_CONTROL
:
1824 ret
= relay_quiescent_control(recv_hdr
, cmd
);
1826 case RELAYD_BEGIN_DATA_PENDING
:
1827 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
1829 case RELAYD_END_DATA_PENDING
:
1830 ret
= relay_end_data_pending(recv_hdr
, cmd
);
1832 case RELAYD_SEND_INDEX
:
1833 ret
= relay_recv_index(recv_hdr
, cmd
);
1835 case RELAYD_UPDATE_SYNC_INFO
:
1837 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1838 relay_unknown_command(cmd
);
1848 * Handle index for a data stream.
1850 * RCU read side lock MUST be acquired.
1852 * Return 0 on success else a negative value.
1854 static int handle_index_data(struct relay_stream
*stream
, uint64_t net_seq_num
,
1857 int ret
= 0, index_created
= 0;
1858 uint64_t stream_id
, data_offset
;
1859 struct relay_index
*index
, *wr_index
= NULL
;
1863 stream_id
= stream
->stream_handle
;
1864 /* Get data offset because we are about to update the index. */
1865 data_offset
= htobe64(stream
->tracefile_size_current
);
1868 * Lookup for an existing index for that stream id/sequence number. If on
1869 * exists, the control thread already received the data for it thus we need
1870 * to write it on disk.
1872 index
= relay_index_find(stream_id
, net_seq_num
);
1874 /* A successful creation will add the object to the HT. */
1875 index
= relay_index_create(stream_id
, net_seq_num
);
1883 if (rotate_index
|| stream
->index_fd
< 0) {
1884 index
->to_close_fd
= stream
->index_fd
;
1885 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1886 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1887 stream
->tracefile_count_current
);
1889 /* This will close the stream's index fd if one. */
1890 relay_index_free_safe(index
);
1893 stream
->index_fd
= ret
;
1895 index
->fd
= stream
->index_fd
;
1896 index
->index_data
.offset
= data_offset
;
1898 if (index_created
) {
1900 * Try to add the relay index object to the hash table. If an object
1901 * already exist, destroy back the index created and set the data.
1903 relay_index_add(index
, &wr_index
);
1905 /* Copy back data from the created index. */
1906 wr_index
->fd
= index
->fd
;
1907 wr_index
->to_close_fd
= index
->to_close_fd
;
1908 wr_index
->index_data
.offset
= data_offset
;
1912 /* The index already exists so write it on disk. */
1916 /* Do we have a writable ready index to write on disk. */
1918 ret
= relay_index_write(wr_index
->fd
, wr_index
);
1922 stream
->total_index_received
++;
1930 * relay_process_data: Process the data received on the data socket
1933 int relay_process_data(struct relay_command
*cmd
)
1935 int ret
= 0, rotate_index
= 0;
1936 struct relay_stream
*stream
;
1937 struct lttcomm_relayd_data_hdr data_hdr
;
1939 uint64_t net_seq_num
;
1942 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1943 sizeof(struct lttcomm_relayd_data_hdr
), 0);
1946 /* Orderly shutdown. Not necessary to print an error. */
1947 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1949 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
1955 stream_id
= be64toh(data_hdr
.stream_id
);
1958 stream
= relay_stream_find_by_id(stream_id
);
1961 goto end_rcu_unlock
;
1964 data_size
= be32toh(data_hdr
.data_size
);
1965 if (data_buffer_size
< data_size
) {
1968 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1969 if (!tmp_data_ptr
) {
1970 ERR("Allocating data buffer");
1973 goto end_rcu_unlock
;
1975 data_buffer
= tmp_data_ptr
;
1976 data_buffer_size
= data_size
;
1978 memset(data_buffer
, 0, data_size
);
1980 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1982 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1983 data_size
, stream_id
, net_seq_num
);
1984 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1987 /* Orderly shutdown. Not necessary to print an error. */
1988 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1991 goto end_rcu_unlock
;
1994 /* Check if a rotation is needed. */
1995 if (stream
->tracefile_size
> 0 &&
1996 (stream
->tracefile_size_current
+ data_size
) >
1997 stream
->tracefile_size
) {
1998 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1999 stream
->tracefile_size
, stream
->tracefile_count
,
2000 relayd_uid
, relayd_gid
, stream
->fd
,
2001 &(stream
->tracefile_count_current
), &stream
->fd
);
2003 ERR("Rotating stream output file");
2004 goto end_rcu_unlock
;
2006 /* Reset current size because we just perform a stream rotation. */
2007 stream
->tracefile_size_current
= 0;
2012 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2013 * index are NOT supported.
2015 if (stream
->session
->minor
>= 4 && !stream
->session
->snapshot
) {
2016 ret
= handle_index_data(stream
, net_seq_num
, rotate_index
);
2018 goto end_rcu_unlock
;
2022 /* Write data to stream output fd. */
2024 ret
= write(stream
->fd
, data_buffer
, data_size
);
2025 } while (ret
< 0 && errno
== EINTR
);
2026 if (ret
< 0 || ret
!= data_size
) {
2027 ERR("Relay error writing data to file");
2029 goto end_rcu_unlock
;
2032 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2033 ret
, stream
->stream_handle
);
2035 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2037 goto end_rcu_unlock
;
2039 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2041 stream
->prev_seq
= net_seq_num
;
2043 /* Check if we need to close the FD */
2044 if (close_stream_check(stream
)) {
2045 destroy_stream(stream
, cmd
->ctf_traces_ht
);
2055 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2059 lttng_poll_del(events
, pollfd
);
2061 ret
= close(pollfd
);
2063 ERR("Closing pollfd %d", pollfd
);
2068 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2069 struct lttng_ht
*relay_connections_ht
)
2071 struct relay_command
*relay_connection
;
2074 relay_connection
= zmalloc(sizeof(struct relay_command
));
2075 if (relay_connection
== NULL
) {
2076 PERROR("Relay command zmalloc");
2080 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
2081 } while (ret
< 0 && errno
== EINTR
);
2082 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
2083 PERROR("read relay cmd pipe");
2087 relay_connection
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
2088 if (!relay_connection
->ctf_traces_ht
) {
2092 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2093 (unsigned long) relay_connection
->sock
->fd
);
2095 lttng_ht_add_unique_ulong(relay_connections_ht
,
2096 &relay_connection
->sock_n
);
2098 return lttng_poll_add(events
,
2099 relay_connection
->sock
->fd
,
2100 LPOLLIN
| LPOLLRDHUP
);
2103 free(relay_connection
);
2109 void deferred_free_connection(struct rcu_head
*head
)
2111 struct relay_command
*relay_connection
=
2112 caa_container_of(head
, struct relay_command
, rcu_node
);
2114 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2115 lttcomm_destroy_sock(relay_connection
->sock
);
2116 free(relay_connection
);
2120 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2121 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2122 struct lttng_ht
*sessions_ht
)
2126 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2128 if (relay_connection
->type
== RELAY_CONTROL
) {
2129 relay_delete_session(relay_connection
, sessions_ht
);
2132 call_rcu(&relay_connection
->rcu_node
,
2133 deferred_free_connection
);
2137 * This thread does the actual work
2140 void *relay_thread_worker(void *data
)
2142 int ret
, err
= -1, last_seen_data_fd
= -1;
2144 struct relay_command
*relay_connection
;
2145 struct lttng_poll_event events
;
2146 struct lttng_ht
*relay_connections_ht
;
2147 struct lttng_ht_node_ulong
*node
;
2148 struct lttng_ht_iter iter
;
2149 struct lttcomm_relayd_hdr recv_hdr
;
2150 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2151 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2153 DBG("[thread] Relay worker started");
2155 rcu_register_thread();
2157 health_register(health_relayd
, HEALTH_RELAYD_TYPE_WORKER
);
2159 /* table of connections indexed on socket */
2160 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2161 if (!relay_connections_ht
) {
2162 goto relay_connections_ht_error
;
2165 /* Tables of received indexes indexed by index handle and net_seq_num. */
2166 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2168 goto indexes_ht_error
;
2171 ret
= create_thread_poll_set(&events
, 2);
2173 goto error_poll_create
;
2176 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2183 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2185 /* Infinite blocking call, waiting for transmission */
2186 DBG3("Relayd worker thread polling...");
2187 ret
= lttng_poll_wait(&events
, -1);
2190 * Restart interrupted system call.
2192 if (errno
== EINTR
) {
2201 * Process control. The control connection is prioritised so we don't
2202 * starve it with high throughout put tracing data on the data
2205 for (i
= 0; i
< nb_fd
; i
++) {
2206 /* Fetch once the poll data */
2207 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2208 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2210 /* Thread quit pipe has been closed. Killing thread. */
2211 ret
= check_thread_quit_pipe(pollfd
, revents
);
2217 /* Inspect the relay cmd pipe for new connection */
2218 if (pollfd
== relay_cmd_pipe
[0]) {
2219 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2220 ERR("Relay pipe error");
2222 } else if (revents
& LPOLLIN
) {
2223 DBG("Relay command received");
2224 ret
= relay_add_connection(relay_cmd_pipe
[0],
2225 &events
, relay_connections_ht
);
2230 } else if (revents
) {
2232 lttng_ht_lookup(relay_connections_ht
,
2233 (void *)((unsigned long) pollfd
),
2235 node
= lttng_ht_iter_get_node_ulong(&iter
);
2237 DBG2("Relay sock %d not found", pollfd
);
2241 relay_connection
= caa_container_of(node
,
2242 struct relay_command
, sock_n
);
2244 if (revents
& (LPOLLERR
)) {
2246 relay_cleanup_poll_connection(&events
, pollfd
);
2247 relay_del_connection(relay_connections_ht
,
2248 &iter
, relay_connection
, sessions_ht
);
2249 if (last_seen_data_fd
== pollfd
) {
2250 last_seen_data_fd
= last_notdel_data_fd
;
2252 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2253 DBG("Socket %d hung up", pollfd
);
2254 relay_cleanup_poll_connection(&events
, pollfd
);
2255 relay_del_connection(relay_connections_ht
,
2256 &iter
, relay_connection
, sessions_ht
);
2257 if (last_seen_data_fd
== pollfd
) {
2258 last_seen_data_fd
= last_notdel_data_fd
;
2260 } else if (revents
& LPOLLIN
) {
2261 /* control socket */
2262 if (relay_connection
->type
== RELAY_CONTROL
) {
2263 ret
= relay_connection
->sock
->ops
->recvmsg(
2264 relay_connection
->sock
, &recv_hdr
,
2265 sizeof(struct lttcomm_relayd_hdr
), 0);
2266 /* connection closed */
2268 relay_cleanup_poll_connection(&events
, pollfd
);
2269 relay_del_connection(relay_connections_ht
,
2270 &iter
, relay_connection
, sessions_ht
);
2271 DBG("Control connection closed with %d", pollfd
);
2273 if (relay_connection
->session
) {
2274 DBG2("Relay worker receiving data for session : %" PRIu64
,
2275 relay_connection
->session
->id
);
2277 ret
= relay_process_control(&recv_hdr
,
2278 relay_connection
, relay_ctx
);
2280 /* Clear the session on error. */
2281 relay_cleanup_poll_connection(&events
, pollfd
);
2282 relay_del_connection(relay_connections_ht
,
2283 &iter
, relay_connection
, sessions_ht
);
2284 DBG("Connection closed with %d", pollfd
);
2290 * Flag the last seen data fd not deleted. It will be
2291 * used as the last seen fd if any fd gets deleted in
2294 last_notdel_data_fd
= pollfd
;
2302 * The last loop handled a control request, go back to poll to make
2303 * sure we prioritise the control socket.
2309 if (last_seen_data_fd
>= 0) {
2310 for (i
= 0; i
< nb_fd
; i
++) {
2311 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2312 if (last_seen_data_fd
== pollfd
) {
2319 /* Process data connection. */
2320 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2321 /* Fetch the poll data. */
2322 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2323 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2325 /* Skip the command pipe. It's handled in the first loop. */
2326 if (pollfd
== relay_cmd_pipe
[0]) {
2332 lttng_ht_lookup(relay_connections_ht
,
2333 (void *)((unsigned long) pollfd
),
2335 node
= lttng_ht_iter_get_node_ulong(&iter
);
2337 /* Skip it. Might be removed before. */
2341 relay_connection
= caa_container_of(node
,
2342 struct relay_command
, sock_n
);
2344 if (revents
& LPOLLIN
) {
2345 if (relay_connection
->type
!= RELAY_DATA
) {
2349 ret
= relay_process_data(relay_connection
);
2350 /* connection closed */
2352 relay_cleanup_poll_connection(&events
, pollfd
);
2353 relay_del_connection(relay_connections_ht
,
2354 &iter
, relay_connection
, sessions_ht
);
2355 DBG("Data connection closed with %d", pollfd
);
2357 * Every goto restart call sets the last seen fd where
2358 * here we don't really care since we gracefully
2359 * continue the loop after the connection is deleted.
2362 /* Keep last seen port. */
2363 last_seen_data_fd
= pollfd
;
2371 last_seen_data_fd
= -1;
2376 lttng_poll_clean(&events
);
2378 /* empty the hash table and free the memory */
2380 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2381 node
= lttng_ht_iter_get_node_ulong(&iter
);
2383 relay_connection
= caa_container_of(node
,
2384 struct relay_command
, sock_n
);
2385 relay_del_connection(relay_connections_ht
,
2386 &iter
, relay_connection
, sessions_ht
);
2391 lttng_ht_destroy(indexes_ht
);
2393 lttng_ht_destroy(relay_connections_ht
);
2394 relay_connections_ht_error
:
2395 /* Close relay cmd pipes */
2396 utils_close_pipe(relay_cmd_pipe
);
2398 DBG("Thread exited with error");
2400 health_unregister(health_relayd
);
2401 DBG("Worker thread cleanup complete");
2404 rcu_unregister_thread();
2409 * Create the relay command pipe to wake thread_manage_apps.
2410 * Closed in cleanup().
2412 static int create_relay_cmd_pipe(void)
2416 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2424 int main(int argc
, char **argv
)
2428 struct relay_local_data
*relay_ctx
;
2430 /* Create thread quit pipe */
2431 if ((ret
= init_thread_quit_pipe()) < 0) {
2435 /* Parse arguments */
2437 if ((ret
= parse_args(argc
, argv
)) < 0) {
2441 if ((ret
= set_signal_handler()) < 0) {
2445 /* Try to create directory if -o, --output is specified. */
2446 if (opt_output_path
) {
2447 if (*opt_output_path
!= '/') {
2448 ERR("Please specify an absolute path for -o, --output PATH");
2452 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2454 ERR("Unable to create %s", opt_output_path
);
2468 /* We need those values for the file/dir creation. */
2469 relayd_uid
= getuid();
2470 relayd_gid
= getgid();
2472 /* Check if daemon is UID = 0 */
2473 if (relayd_uid
== 0) {
2474 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
2475 ERR("Need to be root to use ports < 1024");
2481 /* Setup the thread apps communication pipe. */
2482 if ((ret
= create_relay_cmd_pipe()) < 0) {
2486 /* Init relay command queue. */
2487 cds_wfq_init(&relay_cmd_queue
.queue
);
2489 /* Set up max poll set size */
2490 lttng_poll_set_max_size();
2492 /* Initialize communication library */
2495 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2497 PERROR("relay_ctx");
2501 /* tables of sessions indexed by session ID */
2502 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2503 if (!relay_ctx
->sessions_ht
) {
2504 goto exit_relay_ctx_sessions
;
2507 /* tables of streams indexed by stream ID */
2508 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2509 if (!relay_streams_ht
) {
2510 goto exit_relay_ctx_streams
;
2513 /* tables of streams indexed by stream ID */
2514 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2515 if (!viewer_streams_ht
) {
2516 goto exit_relay_ctx_viewer_streams
;
2519 /* Initialize thread health monitoring */
2520 health_relayd
= health_app_create(NR_HEALTH_RELAYD_TYPES
);
2521 if (!health_relayd
) {
2522 PERROR("health_app_create error");
2523 goto exit_health_app_create
;
2526 /* Setup the dispatcher thread */
2527 ret
= pthread_create(&dispatcher_thread
, NULL
,
2528 relay_thread_dispatcher
, (void *) NULL
);
2530 PERROR("pthread_create dispatcher");
2531 goto exit_dispatcher
;
2534 /* Setup the worker thread */
2535 ret
= pthread_create(&worker_thread
, NULL
,
2536 relay_thread_worker
, (void *) relay_ctx
);
2538 PERROR("pthread_create worker");
2542 /* Setup the listener thread */
2543 ret
= pthread_create(&listener_thread
, NULL
,
2544 relay_thread_listener
, (void *) NULL
);
2546 PERROR("pthread_create listener");
2550 ret
= live_start_threads(live_uri
, relay_ctx
, thread_quit_pipe
);
2552 ERR("Starting live viewer threads");
2556 live_stop_threads();
2559 ret
= pthread_join(listener_thread
, &status
);
2561 PERROR("pthread_join");
2562 goto error
; /* join error, exit without cleanup */
2566 ret
= pthread_join(worker_thread
, &status
);
2568 PERROR("pthread_join");
2569 goto error
; /* join error, exit without cleanup */
2573 ret
= pthread_join(dispatcher_thread
, &status
);
2575 PERROR("pthread_join");
2576 goto error
; /* join error, exit without cleanup */
2580 health_app_destroy(health_relayd
);
2582 exit_health_app_create
:
2583 lttng_ht_destroy(viewer_streams_ht
);
2585 exit_relay_ctx_viewer_streams
:
2586 lttng_ht_destroy(relay_streams_ht
);
2588 exit_relay_ctx_streams
:
2589 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2591 exit_relay_ctx_sessions
: