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"
61 /* command line options */
62 char *opt_output_path
;
63 static int opt_daemon
;
64 static struct lttng_uri
*control_uri
;
65 static struct lttng_uri
*data_uri
;
66 static struct lttng_uri
*live_uri
;
71 * Quit pipe for all threads. This permits a single cancellation point
72 * for all threads when receiving an event on the pipe.
74 static int thread_quit_pipe
[2] = { -1, -1 };
77 * This pipe is used to inform the worker thread that a command is queued and
78 * ready to be processed.
80 static int relay_cmd_pipe
[2] = { -1, -1 };
82 /* Shared between threads */
83 static int dispatch_thread_exit
;
85 static pthread_t listener_thread
;
86 static pthread_t dispatcher_thread
;
87 static pthread_t worker_thread
;
89 static uint64_t last_relay_stream_id
;
90 static uint64_t last_relay_session_id
;
93 * Relay command queue.
95 * The relay_thread_listener and relay_thread_dispatcher communicate with this
98 static struct relay_cmd_queue relay_cmd_queue
;
100 /* buffer allocated at startup, used to store the trace data */
101 static char *data_buffer
;
102 static unsigned int data_buffer_size
;
104 /* Global hash table that stores relay index object. */
105 static struct lttng_ht
*indexes_ht
;
107 /* We need those values for the file/dir creation. */
108 static uid_t relayd_uid
;
109 static gid_t relayd_gid
;
111 /* Global relay stream hash table. */
112 struct lttng_ht
*relay_streams_ht
;
114 /* Global relay viewer stream hash table. */
115 struct lttng_ht
*viewer_streams_ht
;
118 * usage function on stderr
123 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
124 fprintf(stderr
, " -h, --help Display this usage.\n");
125 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
126 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
127 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
128 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
129 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
133 int parse_args(int argc
, char **argv
)
137 char *default_address
;
139 static struct option long_options
[] = {
140 { "control-port", 1, 0, 'C', },
141 { "data-port", 1, 0, 'D', },
142 { "daemonize", 0, 0, 'd', },
143 { "help", 0, 0, 'h', },
144 { "output", 1, 0, 'o', },
145 { "verbose", 0, 0, 'v', },
150 int option_index
= 0;
151 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
152 long_options
, &option_index
);
159 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
161 fprintf(stderr
, " with arg %s\n", optarg
);
165 ret
= uri_parse(optarg
, &control_uri
);
167 ERR("Invalid control URI specified");
170 if (control_uri
->port
== 0) {
171 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
175 ret
= uri_parse(optarg
, &data_uri
);
177 ERR("Invalid data URI specified");
180 if (data_uri
->port
== 0) {
181 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
191 ret
= asprintf(&opt_output_path
, "%s", optarg
);
193 PERROR("asprintf opt_output_path");
198 /* Verbose level can increase using multiple -v */
199 lttng_opt_verbose
+= 1;
202 /* Unknown option or other error.
203 * Error is printed by getopt, just return */
209 /* assign default values */
210 if (control_uri
== NULL
) {
211 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
212 DEFAULT_NETWORK_CONTROL_PORT
);
214 PERROR("asprintf default data address");
218 ret
= uri_parse(default_address
, &control_uri
);
219 free(default_address
);
221 ERR("Invalid control URI specified");
225 if (data_uri
== NULL
) {
226 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
227 DEFAULT_NETWORK_DATA_PORT
);
229 PERROR("asprintf default data address");
233 ret
= uri_parse(default_address
, &data_uri
);
234 free(default_address
);
236 ERR("Invalid data URI specified");
240 if (live_uri
== NULL
) {
241 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
242 DEFAULT_NETWORK_VIEWER_PORT
);
244 PERROR("asprintf default viewer control address");
248 ret
= uri_parse(default_address
, &live_uri
);
249 free(default_address
);
251 ERR("Invalid viewer control URI specified");
268 /* free the dynamically allocated opt_output_path */
269 free(opt_output_path
);
271 /* Close thread quit pipes */
272 utils_close_pipe(thread_quit_pipe
);
274 uri_free(control_uri
);
279 * Write to writable pipe used to notify a thread.
282 int notify_thread_pipe(int wpipe
)
287 ret
= write(wpipe
, "!", 1);
288 } while (ret
< 0 && errno
== EINTR
);
289 if (ret
< 0 || ret
!= 1) {
290 PERROR("write poll pipe");
297 * Stop all threads by closing the thread quit pipe.
300 void stop_threads(void)
304 /* Stopping all threads */
305 DBG("Terminating all threads");
306 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
308 ERR("write error on thread quit pipe");
311 /* Dispatch thread */
312 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
313 futex_nto1_wake(&relay_cmd_queue
.futex
);
317 * Signal handler for the daemon
319 * Simply stop all worker threads, leaving main() return gracefully after
320 * joining all threads and calling cleanup().
323 void sighandler(int sig
)
327 DBG("SIGPIPE caught");
330 DBG("SIGINT caught");
334 DBG("SIGTERM caught");
343 * Setup signal handler for :
344 * SIGINT, SIGTERM, SIGPIPE
347 int set_signal_handler(void)
353 if ((ret
= sigemptyset(&sigset
)) < 0) {
354 PERROR("sigemptyset");
358 sa
.sa_handler
= sighandler
;
361 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
366 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
371 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
376 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
382 * Init thread quit pipe.
384 * Return -1 on error or 0 if all pipes are created.
387 int init_thread_quit_pipe(void)
391 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
397 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
400 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
404 if (events
== NULL
|| size
== 0) {
409 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
415 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
427 * Check if the thread quit pipe was triggered.
429 * Return 1 if it was triggered else 0;
432 int check_thread_quit_pipe(int fd
, uint32_t events
)
434 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
442 * Create and init socket from uri.
445 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
448 struct lttcomm_sock
*sock
= NULL
;
450 sock
= lttcomm_alloc_sock_from_uri(uri
);
452 ERR("Allocating socket");
456 ret
= lttcomm_create_sock(sock
);
460 DBG("Listening on sock %d", sock
->fd
);
462 ret
= sock
->ops
->bind(sock
);
467 ret
= sock
->ops
->listen(sock
, -1);
477 lttcomm_destroy_sock(sock
);
483 * Return nonzero if stream needs to be closed.
486 int close_stream_check(struct relay_stream
*stream
)
489 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
491 * We are about to close the stream so set the data pending flag to 1
492 * which will make the end data pending command skip the stream which
493 * is now closed and ready. Note that after proceeding to a file close,
494 * the written file is ready for reading.
496 stream
->data_pending_check_done
= 1;
503 * This thread manages the listening for new connections on the network
506 void *relay_thread_listener(void *data
)
508 int i
, ret
, pollfd
, err
= -1;
510 uint32_t revents
, nb_fd
;
511 struct lttng_poll_event events
;
512 struct lttcomm_sock
*control_sock
, *data_sock
;
514 DBG("[thread] Relay listener started");
516 control_sock
= relay_init_sock(control_uri
);
518 goto error_sock_control
;
521 data_sock
= relay_init_sock(data_uri
);
523 goto error_sock_relay
;
527 * Pass 3 as size here for the thread quit pipe, control and data socket.
529 ret
= create_thread_poll_set(&events
, 3);
531 goto error_create_poll
;
534 /* Add the control socket */
535 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
540 /* Add the data socket */
541 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
547 DBG("Listener accepting connections");
550 ret
= lttng_poll_wait(&events
, -1);
553 * Restart interrupted system call.
555 if (errno
== EINTR
) {
563 DBG("Relay new connection received");
564 for (i
= 0; i
< nb_fd
; i
++) {
565 /* Fetch once the poll data */
566 revents
= LTTNG_POLL_GETEV(&events
, i
);
567 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
569 /* Thread quit pipe has been closed. Killing thread. */
570 ret
= check_thread_quit_pipe(pollfd
, revents
);
576 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
577 ERR("socket poll error");
579 } else if (revents
& LPOLLIN
) {
581 * Get allocated in this thread,
582 * enqueued to a global queue, dequeued
583 * and freed in the worker thread.
585 struct relay_command
*relay_cmd
;
586 struct lttcomm_sock
*newsock
;
588 relay_cmd
= zmalloc(sizeof(struct relay_command
));
589 if (relay_cmd
== NULL
) {
590 PERROR("relay command zmalloc");
594 if (pollfd
== data_sock
->fd
) {
595 newsock
= data_sock
->ops
->accept(data_sock
);
597 PERROR("accepting data sock");
601 relay_cmd
->type
= RELAY_DATA
;
602 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
604 assert(pollfd
== control_sock
->fd
);
605 newsock
= control_sock
->ops
->accept(control_sock
);
607 PERROR("accepting control sock");
611 relay_cmd
->type
= RELAY_CONTROL
;
612 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
614 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
617 PERROR("setsockopt inet");
618 lttcomm_destroy_sock(newsock
);
622 relay_cmd
->sock
= newsock
;
624 * Lock free enqueue the request.
626 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
629 * Wake the dispatch queue futex. Implicit memory
630 * barrier with the exchange in cds_wfq_enqueue.
632 futex_nto1_wake(&relay_cmd_queue
.futex
);
640 lttng_poll_clean(&events
);
642 if (data_sock
->fd
>= 0) {
643 ret
= data_sock
->ops
->close(data_sock
);
648 lttcomm_destroy_sock(data_sock
);
650 if (control_sock
->fd
>= 0) {
651 ret
= control_sock
->ops
->close(control_sock
);
656 lttcomm_destroy_sock(control_sock
);
659 DBG("Thread exited with error");
661 DBG("Relay listener thread cleanup complete");
667 * This thread manages the dispatching of the requests to worker threads
670 void *relay_thread_dispatcher(void *data
)
673 struct cds_wfq_node
*node
;
674 struct relay_command
*relay_cmd
= NULL
;
676 DBG("[thread] Relay dispatcher started");
678 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
679 /* Atomically prepare the queue futex */
680 futex_nto1_prepare(&relay_cmd_queue
.futex
);
683 /* Dequeue commands */
684 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
686 DBG("Woken up but nothing in the relay command queue");
687 /* Continue thread execution */
691 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
692 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
695 * Inform worker thread of the new request. This
696 * call is blocking so we can be assured that the data will be read
697 * at some point in time or wait to the end of the world :)
700 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
701 sizeof(struct relay_command
));
702 } while (ret
< 0 && errno
== EINTR
);
704 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
705 PERROR("write cmd pipe");
708 } while (node
!= NULL
);
710 /* Futex wait on queue. Blocking call on futex() */
711 futex_nto1_wait(&relay_cmd_queue
.futex
);
715 DBG("Dispatch thread dying");
721 * Get stream from stream id.
722 * Need to be called with RCU read-side lock held.
724 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
)
726 struct lttng_ht_node_ulong
*node
;
727 struct lttng_ht_iter iter
;
728 struct relay_stream
*ret
;
730 lttng_ht_lookup(relay_streams_ht
,
731 (void *)((unsigned long) stream_id
),
733 node
= lttng_ht_iter_get_node_ulong(&iter
);
735 DBG("Relay stream %" PRIu64
" not found", stream_id
);
740 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
747 void deferred_free_stream(struct rcu_head
*head
)
749 struct relay_stream
*stream
=
750 caa_container_of(head
, struct relay_stream
, rcu_node
);
752 ctf_trace_try_destroy(stream
->ctf_trace
);
754 free(stream
->path_name
);
755 free(stream
->channel_name
);
760 void deferred_free_session(struct rcu_head
*head
)
762 struct relay_session
*session
=
763 caa_container_of(head
, struct relay_session
, rcu_node
);
767 static void close_stream(struct relay_stream
*stream
,
768 struct lttng_ht
*ctf_traces_ht
)
771 struct relay_viewer_stream
*vstream
;
772 struct lttng_ht_iter iter
;
776 delret
= close(stream
->fd
);
778 PERROR("close stream");
781 if (stream
->index_fd
>= 0) {
782 delret
= close(stream
->index_fd
);
784 PERROR("close stream index_fd");
788 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
791 * Set the last good value into the viewer stream. This is done
792 * right before the stream gets deleted from the hash table. The
793 * lookup failure on the live thread side of a stream indicates
794 * that the viewer stream index received value should be used.
796 vstream
->total_index_received
= stream
->total_index_received
;
799 iter
.iter
.node
= &stream
->stream_n
.node
;
800 delret
= lttng_ht_del(relay_streams_ht
, &iter
);
802 iter
.iter
.node
= &stream
->ctf_trace_node
.node
;
803 delret
= lttng_ht_del(ctf_traces_ht
, &iter
);
805 call_rcu(&stream
->rcu_node
, deferred_free_stream
);
806 DBG("Closed tracefile %d from close stream", stream
->fd
);
810 * relay_delete_session: Free all memory associated with a session and
814 void relay_delete_session(struct relay_command
*cmd
,
815 struct lttng_ht
*sessions_ht
)
817 struct lttng_ht_iter iter
;
818 struct lttng_ht_node_ulong
*node
;
819 struct relay_stream
*stream
;
826 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
829 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
830 node
= lttng_ht_iter_get_node_ulong(&iter
);
832 stream
= caa_container_of(node
,
833 struct relay_stream
, stream_n
);
834 if (stream
->session
== cmd
->session
) {
835 ret
= close(stream
->fd
);
837 PERROR("close stream fd on delete session");
839 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
841 call_rcu(&stream
->rcu_node
,
842 deferred_free_stream
);
844 /* Cleanup index of that stream. */
845 relay_index_destroy_by_stream_id(stream
->stream_handle
,
849 iter
.iter
.node
= &cmd
->session
->session_n
.node
;
850 ret
= lttng_ht_del(sessions_ht
, &iter
);
852 call_rcu(&cmd
->session
->rcu_node
,
853 deferred_free_session
);
858 * Copy index data from the control port to a given index object.
860 static void copy_index_control_data(struct relay_index
*index
,
861 struct lttcomm_relayd_index
*data
)
867 * The index on disk is encoded in big endian, so we don't need to convert
868 * the data received on the network. The data_offset value is NEVER
869 * modified here and is updated by the data thread.
871 index
->index_data
.packet_size
= data
->packet_size
;
872 index
->index_data
.content_size
= data
->content_size
;
873 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
874 index
->index_data
.timestamp_end
= data
->timestamp_end
;
875 index
->index_data
.events_discarded
= data
->events_discarded
;
876 index
->index_data
.stream_id
= data
->stream_id
;
880 * Handle the RELAYD_CREATE_SESSION command.
882 * On success, send back the session id or else return a negative value.
885 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
886 struct relay_command
*cmd
,
887 struct lttng_ht
*sessions_ht
)
889 int ret
= 0, send_ret
;
890 struct relay_session
*session
;
891 struct lttcomm_relayd_status_session reply
;
896 memset(&reply
, 0, sizeof(reply
));
898 session
= zmalloc(sizeof(struct relay_session
));
899 if (session
== NULL
) {
900 PERROR("relay session zmalloc");
905 session
->id
= ++last_relay_session_id
;
906 session
->sock
= cmd
->sock
;
907 cmd
->session
= session
;
909 reply
.session_id
= htobe64(session
->id
);
911 switch (cmd
->minor
) {
912 case 4: /* LTTng sessiond 2.4 */
914 ret
= cmd_create_session_2_4(cmd
, session
);
918 lttng_ht_node_init_ulong(&session
->session_n
,
919 (unsigned long) session
->id
);
920 lttng_ht_add_unique_ulong(sessions_ht
,
921 &session
->session_n
);
923 DBG("Created session %" PRIu64
, session
->id
);
927 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
929 reply
.ret_code
= htobe32(LTTNG_OK
);
932 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
934 ERR("Relayd sending session id");
942 * relay_add_stream: allocate a new stream for a session
945 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
946 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
948 struct relay_session
*session
= cmd
->session
;
949 struct relay_stream
*stream
= NULL
;
950 struct lttcomm_relayd_status_stream reply
;
953 if (!session
|| cmd
->version_check_done
== 0) {
954 ERR("Trying to add a stream before version check");
959 stream
= zmalloc(sizeof(struct relay_stream
));
960 if (stream
== NULL
) {
961 PERROR("relay stream zmalloc");
966 switch (cmd
->minor
) {
967 case 1: /* LTTng sessiond 2.1 */
968 ret
= cmd_recv_stream_2_1(cmd
, stream
);
970 case 2: /* LTTng sessiond 2.2 */
972 ret
= cmd_recv_stream_2_2(cmd
, stream
);
976 goto err_free_stream
;
980 stream
->stream_handle
= ++last_relay_stream_id
;
981 stream
->prev_seq
= -1ULL;
982 stream
->session
= session
;
983 stream
->index_fd
= -1;
984 stream
->read_index_fd
= -1;
985 stream
->ctf_trace
= NULL
;
986 pthread_mutex_init(&stream
->lock
, NULL
);
988 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
990 ERR("relay creating output directory");
995 * No need to use run_as API here because whatever we receives, the relayd
996 * uses its own credentials for the stream files.
998 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
999 stream
->tracefile_size
, 0, relayd_uid
, relayd_gid
, NULL
);
1001 ERR("Create output file");
1005 if (stream
->tracefile_size
) {
1006 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
1008 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
1011 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
1012 stream
->metadata_flag
= 1;
1014 * When we receive a new metadata stream, we create a new
1015 * ctf_trace and we assign this ctf_trace to all streams with
1018 * If later on we receive a new stream for the same ctf_trace,
1019 * we copy the information from the first hit in the HT to the
1022 stream
->ctf_trace
= ctf_trace_create();
1023 if (!stream
->ctf_trace
) {
1027 stream
->ctf_trace
->refcount
++;
1028 stream
->ctf_trace
->metadata_stream
= stream
;
1030 ctf_trace_assign(cmd
->ctf_traces_ht
, stream
);
1032 lttng_ht_node_init_ulong(&stream
->stream_n
,
1033 (unsigned long) stream
->stream_handle
);
1034 lttng_ht_add_unique_ulong(relay_streams_ht
,
1037 lttng_ht_node_init_str(&stream
->ctf_trace_node
, stream
->path_name
);
1038 lttng_ht_add_str(cmd
->ctf_traces_ht
, &stream
->ctf_trace_node
);
1040 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
1041 stream
->stream_handle
);
1044 reply
.handle
= htobe64(stream
->stream_handle
);
1045 /* send the session id to the client or a negative return code on error */
1047 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1048 /* stream was not properly added to the ht, so free it */
1051 reply
.ret_code
= htobe32(LTTNG_OK
);
1054 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1055 sizeof(struct lttcomm_relayd_status_stream
), 0);
1057 ERR("Relay sending stream id");
1066 free(stream
->path_name
);
1067 free(stream
->channel_name
);
1073 * relay_close_stream: close a specific stream
1076 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1077 struct relay_command
*cmd
)
1080 struct relay_session
*session
= cmd
->session
;
1081 struct lttcomm_relayd_close_stream stream_info
;
1082 struct lttcomm_relayd_generic_reply reply
;
1083 struct relay_stream
*stream
;
1085 DBG("Close stream received");
1087 if (!session
|| cmd
->version_check_done
== 0) {
1088 ERR("Trying to close a stream before version check");
1090 goto end_no_session
;
1093 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1094 sizeof(struct lttcomm_relayd_close_stream
), 0);
1095 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1097 /* Orderly shutdown. Not necessary to print an error. */
1098 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1100 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1103 goto end_no_session
;
1107 stream
= relay_stream_find_by_id(be64toh(stream_info
.stream_id
));
1113 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1114 stream
->close_flag
= 1;
1116 if (close_stream_check(stream
)) {
1117 close_stream(stream
, cmd
->ctf_traces_ht
);
1124 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1126 reply
.ret_code
= htobe32(LTTNG_OK
);
1128 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1129 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1131 ERR("Relay sending stream id");
1140 * relay_unknown_command: send -1 if received unknown command
1143 void relay_unknown_command(struct relay_command
*cmd
)
1145 struct lttcomm_relayd_generic_reply reply
;
1148 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1149 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1150 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1152 ERR("Relay sending unknown command");
1157 * relay_start: send an acknowledgment to the client to tell if we are
1158 * ready to receive data. We are ready if a session is established.
1161 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1162 struct relay_command
*cmd
)
1164 int ret
= htobe32(LTTNG_OK
);
1165 struct lttcomm_relayd_generic_reply reply
;
1166 struct relay_session
*session
= cmd
->session
;
1169 DBG("Trying to start the streaming without a session established");
1170 ret
= htobe32(LTTNG_ERR_UNK
);
1173 reply
.ret_code
= ret
;
1174 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1175 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1177 ERR("Relay sending start ack");
1184 * Append padding to the file pointed by the file descriptor fd.
1186 static int write_padding_to_file(int fd
, uint32_t size
)
1195 zeros
= zmalloc(size
);
1196 if (zeros
== NULL
) {
1197 PERROR("zmalloc zeros for padding");
1203 ret
= write(fd
, zeros
, size
);
1204 } while (ret
< 0 && errno
== EINTR
);
1205 if (ret
< 0 || ret
!= size
) {
1206 PERROR("write padding to file");
1216 * relay_recv_metadata: receive the metada for the session.
1219 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1220 struct relay_command
*cmd
)
1222 int ret
= htobe32(LTTNG_OK
);
1223 struct relay_session
*session
= cmd
->session
;
1224 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1225 struct relay_stream
*metadata_stream
;
1226 uint64_t data_size
, payload_size
;
1229 ERR("Metadata sent before version check");
1234 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1235 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1236 ERR("Incorrect data size");
1240 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1242 if (data_buffer_size
< data_size
) {
1243 /* In case the realloc fails, we can free the memory */
1246 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1247 if (!tmp_data_ptr
) {
1248 ERR("Allocating data buffer");
1253 data_buffer
= tmp_data_ptr
;
1254 data_buffer_size
= data_size
;
1256 memset(data_buffer
, 0, data_size
);
1257 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1258 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1259 if (ret
< 0 || ret
!= data_size
) {
1261 /* Orderly shutdown. Not necessary to print an error. */
1262 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1264 ERR("Relay didn't receive the whole metadata");
1269 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1272 metadata_stream
= relay_stream_find_by_id(
1273 be64toh(metadata_struct
->stream_id
));
1274 if (!metadata_stream
) {
1280 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1282 } while (ret
< 0 && errno
== EINTR
);
1283 if (ret
< 0 || ret
!= payload_size
) {
1284 ERR("Relay error writing metadata on file");
1289 ret
= write_padding_to_file(metadata_stream
->fd
,
1290 be32toh(metadata_struct
->padding_size
));
1294 metadata_stream
->ctf_trace
->metadata_received
+=
1295 payload_size
+ be32toh(metadata_struct
->padding_size
);
1297 DBG2("Relay metadata written");
1306 * relay_send_version: send relayd version number
1309 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1310 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1313 struct lttcomm_relayd_version reply
, msg
;
1317 cmd
->version_check_done
= 1;
1319 /* Get version from the other side. */
1320 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1321 if (ret
< 0 || ret
!= sizeof(msg
)) {
1323 /* Orderly shutdown. Not necessary to print an error. */
1324 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1326 ERR("Relay failed to receive the version values.");
1332 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1333 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1335 /* Major versions must be the same */
1336 if (reply
.major
!= be32toh(msg
.major
)) {
1337 DBG("Incompatible major versions (%u vs %u), deleting session",
1338 reply
.major
, be32toh(msg
.major
));
1339 relay_delete_session(cmd
, sessions_ht
);
1344 cmd
->major
= reply
.major
;
1345 /* We adapt to the lowest compatible version */
1346 if (reply
.minor
<= be32toh(msg
.minor
)) {
1347 cmd
->minor
= reply
.minor
;
1349 cmd
->minor
= be32toh(msg
.minor
);
1352 reply
.major
= htobe32(reply
.major
);
1353 reply
.minor
= htobe32(reply
.minor
);
1354 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1355 sizeof(struct lttcomm_relayd_version
), 0);
1357 ERR("Relay sending version");
1360 DBG("Version check done using protocol %u.%u", cmd
->major
,
1368 * Check for data pending for a given stream id from the session daemon.
1371 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1372 struct relay_command
*cmd
)
1374 struct relay_session
*session
= cmd
->session
;
1375 struct lttcomm_relayd_data_pending msg
;
1376 struct lttcomm_relayd_generic_reply reply
;
1377 struct relay_stream
*stream
;
1379 uint64_t last_net_seq_num
, stream_id
;
1381 DBG("Data pending command received");
1383 if (!session
|| cmd
->version_check_done
== 0) {
1384 ERR("Trying to check for data before version check");
1386 goto end_no_session
;
1389 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1390 if (ret
< sizeof(msg
)) {
1392 /* Orderly shutdown. Not necessary to print an error. */
1393 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1395 ERR("Relay didn't receive valid data_pending struct size : %d",
1399 goto end_no_session
;
1402 stream_id
= be64toh(msg
.stream_id
);
1403 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1406 stream
= relay_stream_find_by_id(stream_id
);
1407 if (stream
== NULL
) {
1412 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1413 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1416 /* Avoid wrapping issue */
1417 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1418 /* Data has in fact been written and is NOT pending */
1421 /* Data still being streamed thus pending */
1425 /* Pending check is now done. */
1426 stream
->data_pending_check_done
= 1;
1431 reply
.ret_code
= htobe32(ret
);
1432 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1434 ERR("Relay data pending ret code failed");
1442 * Wait for the control socket to reach a quiescent state.
1444 * Note that for now, when receiving this command from the session daemon, this
1445 * means that every subsequent commands or data received on the control socket
1446 * has been handled. So, this is why we simply return OK here.
1449 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1450 struct relay_command
*cmd
)
1454 struct relay_stream
*stream
;
1455 struct lttng_ht_iter iter
;
1456 struct lttcomm_relayd_quiescent_control msg
;
1457 struct lttcomm_relayd_generic_reply reply
;
1459 DBG("Checking quiescent state on control socket");
1461 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1462 ERR("Trying to check for data before version check");
1464 goto end_no_session
;
1467 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1468 if (ret
< sizeof(msg
)) {
1470 /* Orderly shutdown. Not necessary to print an error. */
1471 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1473 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1477 goto end_no_session
;
1480 stream_id
= be64toh(msg
.stream_id
);
1483 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1485 if (stream
->stream_handle
== stream_id
) {
1486 stream
->data_pending_check_done
= 1;
1487 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1494 reply
.ret_code
= htobe32(LTTNG_OK
);
1495 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1497 ERR("Relay data quiescent control ret code failed");
1505 * Initialize a data pending command. This means that a client is about to ask
1506 * for data pending for each stream he/she holds. Simply iterate over all
1507 * streams of a session and set the data_pending_check_done flag.
1509 * This command returns to the client a LTTNG_OK code.
1512 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1513 struct relay_command
*cmd
)
1516 struct lttng_ht_iter iter
;
1517 struct lttcomm_relayd_begin_data_pending msg
;
1518 struct lttcomm_relayd_generic_reply reply
;
1519 struct relay_stream
*stream
;
1520 uint64_t session_id
;
1525 DBG("Init streams for data pending");
1527 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1528 ERR("Trying to check for data before version check");
1530 goto end_no_session
;
1533 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1534 if (ret
< sizeof(msg
)) {
1536 /* Orderly shutdown. Not necessary to print an error. */
1537 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1539 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1543 goto end_no_session
;
1546 session_id
= be64toh(msg
.session_id
);
1549 * Iterate over all streams to set the begin data pending flag. For now, the
1550 * streams are indexed by stream handle so we have to iterate over all
1551 * streams to find the one associated with the right session_id.
1554 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1556 if (stream
->session
->id
== session_id
) {
1557 stream
->data_pending_check_done
= 0;
1558 DBG("Set begin data pending flag to stream %" PRIu64
,
1559 stream
->stream_handle
);
1564 /* All good, send back reply. */
1565 reply
.ret_code
= htobe32(LTTNG_OK
);
1567 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1569 ERR("Relay begin data pending send reply failed");
1577 * End data pending command. This will check, for a given session id, if each
1578 * stream associated with it has its data_pending_check_done flag set. If not,
1579 * this means that the client lost track of the stream but the data is still
1580 * being streamed on our side. In this case, we inform the client that data is
1583 * Return to the client if there is data in flight or not with a ret_code.
1586 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1587 struct relay_command
*cmd
)
1590 struct lttng_ht_iter iter
;
1591 struct lttcomm_relayd_end_data_pending msg
;
1592 struct lttcomm_relayd_generic_reply reply
;
1593 struct relay_stream
*stream
;
1594 uint64_t session_id
;
1595 uint32_t is_data_inflight
= 0;
1600 DBG("End data pending command");
1602 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1603 ERR("Trying to check for data before version check");
1605 goto end_no_session
;
1608 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1609 if (ret
< sizeof(msg
)) {
1611 /* Orderly shutdown. Not necessary to print an error. */
1612 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1614 ERR("Relay didn't receive valid end data_pending struct size: %d",
1618 goto end_no_session
;
1621 session_id
= be64toh(msg
.session_id
);
1623 /* Iterate over all streams to see if the begin data pending flag is set. */
1625 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1627 if (stream
->session
->id
== session_id
&&
1628 !stream
->data_pending_check_done
) {
1629 is_data_inflight
= 1;
1630 DBG("Data is still in flight for stream %" PRIu64
,
1631 stream
->stream_handle
);
1637 /* All good, send back reply. */
1638 reply
.ret_code
= htobe32(is_data_inflight
);
1640 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1642 ERR("Relay end data pending send reply failed");
1650 * Receive an index for a specific stream.
1652 * Return 0 on success else a negative value.
1655 int relay_recv_index(struct lttcomm_relayd_hdr
*recv_hdr
,
1656 struct relay_command
*cmd
, struct lttng_ht
*indexes_ht
)
1658 int ret
, send_ret
, index_created
= 0;
1659 struct relay_session
*session
= cmd
->session
;
1660 struct lttcomm_relayd_index index_info
;
1661 struct relay_index
*index
, *wr_index
= NULL
;
1662 struct lttcomm_relayd_generic_reply reply
;
1663 struct relay_stream
*stream
;
1664 uint64_t net_seq_num
;
1669 DBG("Relay receiving index");
1671 if (!session
|| cmd
->version_check_done
== 0) {
1672 ERR("Trying to close a stream before version check");
1674 goto end_no_session
;
1677 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &index_info
,
1678 sizeof(index_info
), 0);
1679 if (ret
< sizeof(index_info
)) {
1681 /* Orderly shutdown. Not necessary to print an error. */
1682 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1684 ERR("Relay didn't receive valid index struct size : %d", ret
);
1687 goto end_no_session
;
1690 net_seq_num
= be64toh(index_info
.net_seq_num
);
1693 stream
= relay_stream_find_by_id(be64toh(index_info
.relay_stream_id
));
1696 goto end_rcu_unlock
;
1699 /* Live beacon handling */
1700 if (index_info
.packet_size
== 0) {
1701 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1704 * Only flag a stream inactive when it has already received data.
1706 if (stream
->total_index_received
> 0) {
1707 stream
->beacon_ts_end
= be64toh(index_info
.timestamp_end
);
1710 goto end_rcu_unlock
;
1712 stream
->beacon_ts_end
= -1ULL;
1715 index
= relay_index_find(stream
->stream_handle
, net_seq_num
, indexes_ht
);
1717 /* A successful creation will add the object to the HT. */
1718 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1720 goto end_rcu_unlock
;
1725 copy_index_control_data(index
, &index_info
);
1727 if (index_created
) {
1729 * Try to add the relay index object to the hash table. If an object
1730 * already exist, destroy back the index created, set the data in this
1731 * object and write it on disk.
1733 relay_index_add(index
, indexes_ht
, &wr_index
);
1735 copy_index_control_data(wr_index
, &index_info
);
1739 /* The index already exists so write it on disk. */
1743 /* Do we have a writable ready index to write on disk. */
1745 /* Starting at 2.4, create the index file if none available. */
1746 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1747 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1748 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1749 stream
->tracefile_count_current
);
1751 goto end_rcu_unlock
;
1753 stream
->index_fd
= ret
;
1756 ret
= relay_index_write(wr_index
->fd
, wr_index
, indexes_ht
);
1758 goto end_rcu_unlock
;
1760 stream
->total_index_received
++;
1767 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1769 reply
.ret_code
= htobe32(LTTNG_OK
);
1771 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1773 ERR("Relay sending close index id reply");
1782 * Process the commands received on the control socket
1785 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1786 struct relay_command
*cmd
, struct relay_local_data
*ctx
)
1790 switch (be32toh(recv_hdr
->cmd
)) {
1791 case RELAYD_CREATE_SESSION
:
1792 ret
= relay_create_session(recv_hdr
, cmd
, ctx
->sessions_ht
);
1794 case RELAYD_ADD_STREAM
:
1795 ret
= relay_add_stream(recv_hdr
, cmd
, ctx
->sessions_ht
);
1797 case RELAYD_START_DATA
:
1798 ret
= relay_start(recv_hdr
, cmd
);
1800 case RELAYD_SEND_METADATA
:
1801 ret
= relay_recv_metadata(recv_hdr
, cmd
);
1803 case RELAYD_VERSION
:
1804 ret
= relay_send_version(recv_hdr
, cmd
, ctx
->sessions_ht
);
1806 case RELAYD_CLOSE_STREAM
:
1807 ret
= relay_close_stream(recv_hdr
, cmd
);
1809 case RELAYD_DATA_PENDING
:
1810 ret
= relay_data_pending(recv_hdr
, cmd
);
1812 case RELAYD_QUIESCENT_CONTROL
:
1813 ret
= relay_quiescent_control(recv_hdr
, cmd
);
1815 case RELAYD_BEGIN_DATA_PENDING
:
1816 ret
= relay_begin_data_pending(recv_hdr
, cmd
);
1818 case RELAYD_END_DATA_PENDING
:
1819 ret
= relay_end_data_pending(recv_hdr
, cmd
);
1821 case RELAYD_SEND_INDEX
:
1822 ret
= relay_recv_index(recv_hdr
, cmd
, indexes_ht
);
1824 case RELAYD_UPDATE_SYNC_INFO
:
1826 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1827 relay_unknown_command(cmd
);
1837 * relay_process_data: Process the data received on the data socket
1840 int relay_process_data(struct relay_command
*cmd
,
1841 struct lttng_ht
*indexes_ht
)
1843 int ret
= 0, rotate_index
= 0, index_created
= 0;
1844 struct relay_stream
*stream
;
1845 struct relay_index
*index
, *wr_index
= NULL
;
1846 struct lttcomm_relayd_data_hdr data_hdr
;
1847 uint64_t stream_id
, data_offset
;
1848 uint64_t net_seq_num
;
1851 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1852 sizeof(struct lttcomm_relayd_data_hdr
), 0);
1855 /* Orderly shutdown. Not necessary to print an error. */
1856 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1858 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
1864 stream_id
= be64toh(data_hdr
.stream_id
);
1867 stream
= relay_stream_find_by_id(stream_id
);
1870 goto end_rcu_unlock
;
1873 data_size
= be32toh(data_hdr
.data_size
);
1874 if (data_buffer_size
< data_size
) {
1877 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1878 if (!tmp_data_ptr
) {
1879 ERR("Allocating data buffer");
1882 goto end_rcu_unlock
;
1884 data_buffer
= tmp_data_ptr
;
1885 data_buffer_size
= data_size
;
1887 memset(data_buffer
, 0, data_size
);
1889 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1891 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1892 data_size
, stream_id
, net_seq_num
);
1893 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1896 /* Orderly shutdown. Not necessary to print an error. */
1897 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1900 goto end_rcu_unlock
;
1903 /* Check if a rotation is needed. */
1904 if (stream
->tracefile_size
> 0 &&
1905 (stream
->tracefile_size_current
+ data_size
) >
1906 stream
->tracefile_size
) {
1907 ret
= utils_rotate_stream_file(stream
->path_name
, stream
->channel_name
,
1908 stream
->tracefile_size
, stream
->tracefile_count
,
1909 relayd_uid
, relayd_gid
, stream
->fd
,
1910 &(stream
->tracefile_count_current
), &stream
->fd
);
1912 ERR("Rotating stream output file");
1913 goto end_rcu_unlock
;
1915 /* Reset current size because we just perform a stream rotation. */
1916 stream
->tracefile_size_current
= 0;
1920 /* Get data offset because we are about to update the index. */
1921 data_offset
= htobe64(stream
->tracefile_size_current
);
1924 * Lookup for an existing index for that stream id/sequence number. If on
1925 * exists, the control thread already received the data for it thus we need
1926 * to write it on disk.
1928 index
= relay_index_find(stream_id
, net_seq_num
, indexes_ht
);
1930 /* A successful creation will add the object to the HT. */
1931 index
= relay_index_create(stream
->stream_handle
, net_seq_num
);
1933 goto end_rcu_unlock
;
1938 if (rotate_index
|| stream
->index_fd
< 0) {
1939 index
->to_close_fd
= stream
->index_fd
;
1940 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1941 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1942 stream
->tracefile_count_current
);
1944 /* This will close the stream's index fd if one. */
1945 relay_index_free_safe(index
);
1946 goto end_rcu_unlock
;
1948 stream
->index_fd
= ret
;
1950 index
->fd
= stream
->index_fd
;
1951 index
->index_data
.offset
= data_offset
;
1953 if (index_created
) {
1955 * Try to add the relay index object to the hash table. If an object
1956 * already exist, destroy back the index created and set the data.
1958 relay_index_add(index
, indexes_ht
, &wr_index
);
1960 /* Copy back data from the created index. */
1961 wr_index
->fd
= index
->fd
;
1962 wr_index
->to_close_fd
= index
->to_close_fd
;
1963 wr_index
->index_data
.offset
= data_offset
;
1967 /* The index already exists so write it on disk. */
1971 /* Do we have a writable ready index to write on disk. */
1973 /* Starting at 2.4, create the index file if none available. */
1974 if (cmd
->minor
>= 4 && stream
->index_fd
< 0) {
1975 ret
= index_create_file(stream
->path_name
, stream
->channel_name
,
1976 relayd_uid
, relayd_gid
, stream
->tracefile_size
,
1977 stream
->tracefile_count_current
);
1979 goto end_rcu_unlock
;
1981 stream
->index_fd
= ret
;
1984 ret
= relay_index_write(wr_index
->fd
, wr_index
, indexes_ht
);
1986 goto end_rcu_unlock
;
1988 stream
->total_index_received
++;
1992 ret
= write(stream
->fd
, data_buffer
, data_size
);
1993 } while (ret
< 0 && errno
== EINTR
);
1994 if (ret
< 0 || ret
!= data_size
) {
1995 ERR("Relay error writing data to file");
1997 goto end_rcu_unlock
;
2000 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
2001 ret
, stream
->stream_handle
);
2003 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
2005 goto end_rcu_unlock
;
2007 stream
->tracefile_size_current
+= data_size
+ be32toh(data_hdr
.padding_size
);
2009 stream
->prev_seq
= net_seq_num
;
2011 /* Check if we need to close the FD */
2012 if (close_stream_check(stream
)) {
2013 close_stream(stream
, cmd
->ctf_traces_ht
);
2023 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
2027 lttng_poll_del(events
, pollfd
);
2029 ret
= close(pollfd
);
2031 ERR("Closing pollfd %d", pollfd
);
2036 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
2037 struct lttng_ht
*relay_connections_ht
)
2039 struct relay_command
*relay_connection
;
2042 relay_connection
= zmalloc(sizeof(struct relay_command
));
2043 if (relay_connection
== NULL
) {
2044 PERROR("Relay command zmalloc");
2048 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
2049 } while (ret
< 0 && errno
== EINTR
);
2050 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
2051 PERROR("read relay cmd pipe");
2055 relay_connection
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
2056 if (!relay_connection
->ctf_traces_ht
) {
2060 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
2061 (unsigned long) relay_connection
->sock
->fd
);
2063 lttng_ht_add_unique_ulong(relay_connections_ht
,
2064 &relay_connection
->sock_n
);
2066 return lttng_poll_add(events
,
2067 relay_connection
->sock
->fd
,
2068 LPOLLIN
| LPOLLRDHUP
);
2071 free(relay_connection
);
2077 void deferred_free_connection(struct rcu_head
*head
)
2079 struct relay_command
*relay_connection
=
2080 caa_container_of(head
, struct relay_command
, rcu_node
);
2082 lttng_ht_destroy(relay_connection
->ctf_traces_ht
);
2083 lttcomm_destroy_sock(relay_connection
->sock
);
2084 free(relay_connection
);
2088 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
2089 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
2090 struct lttng_ht
*sessions_ht
)
2094 ret
= lttng_ht_del(relay_connections_ht
, iter
);
2096 if (relay_connection
->type
== RELAY_CONTROL
) {
2097 relay_delete_session(relay_connection
, sessions_ht
);
2100 call_rcu(&relay_connection
->rcu_node
,
2101 deferred_free_connection
);
2105 * This thread does the actual work
2108 void *relay_thread_worker(void *data
)
2110 int ret
, err
= -1, last_seen_data_fd
= -1;
2112 struct relay_command
*relay_connection
;
2113 struct lttng_poll_event events
;
2114 struct lttng_ht
*relay_connections_ht
;
2115 struct lttng_ht_node_ulong
*node
;
2116 struct lttng_ht_iter iter
;
2117 struct lttcomm_relayd_hdr recv_hdr
;
2118 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
2119 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
2121 DBG("[thread] Relay worker started");
2123 rcu_register_thread();
2125 /* table of connections indexed on socket */
2126 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2127 if (!relay_connections_ht
) {
2128 goto relay_connections_ht_error
;
2131 /* Tables of received indexes indexed by index handle and net_seq_num. */
2132 indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64
);
2134 goto indexes_ht_error
;
2137 ret
= create_thread_poll_set(&events
, 2);
2139 goto error_poll_create
;
2142 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2149 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
2151 /* Infinite blocking call, waiting for transmission */
2152 DBG3("Relayd worker thread polling...");
2153 ret
= lttng_poll_wait(&events
, -1);
2156 * Restart interrupted system call.
2158 if (errno
== EINTR
) {
2167 * Process control. The control connection is prioritised so we don't
2168 * starve it with high throughout put tracing data on the data
2171 for (i
= 0; i
< nb_fd
; i
++) {
2172 /* Fetch once the poll data */
2173 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2174 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2176 /* Thread quit pipe has been closed. Killing thread. */
2177 ret
= check_thread_quit_pipe(pollfd
, revents
);
2183 /* Inspect the relay cmd pipe for new connection */
2184 if (pollfd
== relay_cmd_pipe
[0]) {
2185 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2186 ERR("Relay pipe error");
2188 } else if (revents
& LPOLLIN
) {
2189 DBG("Relay command received");
2190 ret
= relay_add_connection(relay_cmd_pipe
[0],
2191 &events
, relay_connections_ht
);
2196 } else if (revents
) {
2198 lttng_ht_lookup(relay_connections_ht
,
2199 (void *)((unsigned long) pollfd
),
2201 node
= lttng_ht_iter_get_node_ulong(&iter
);
2203 DBG2("Relay sock %d not found", pollfd
);
2207 relay_connection
= caa_container_of(node
,
2208 struct relay_command
, sock_n
);
2210 if (revents
& (LPOLLERR
)) {
2212 relay_cleanup_poll_connection(&events
, pollfd
);
2213 relay_del_connection(relay_connections_ht
,
2214 &iter
, relay_connection
, sessions_ht
);
2215 if (last_seen_data_fd
== pollfd
) {
2216 last_seen_data_fd
= last_notdel_data_fd
;
2218 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2219 DBG("Socket %d hung up", pollfd
);
2220 relay_cleanup_poll_connection(&events
, pollfd
);
2221 relay_del_connection(relay_connections_ht
,
2222 &iter
, relay_connection
, sessions_ht
);
2223 if (last_seen_data_fd
== pollfd
) {
2224 last_seen_data_fd
= last_notdel_data_fd
;
2226 } else if (revents
& LPOLLIN
) {
2227 /* control socket */
2228 if (relay_connection
->type
== RELAY_CONTROL
) {
2229 ret
= relay_connection
->sock
->ops
->recvmsg(
2230 relay_connection
->sock
, &recv_hdr
,
2231 sizeof(struct lttcomm_relayd_hdr
), 0);
2232 /* connection closed */
2234 relay_cleanup_poll_connection(&events
, pollfd
);
2235 relay_del_connection(relay_connections_ht
,
2236 &iter
, relay_connection
, sessions_ht
);
2237 DBG("Control connection closed with %d", pollfd
);
2239 if (relay_connection
->session
) {
2240 DBG2("Relay worker receiving data for session : %" PRIu64
,
2241 relay_connection
->session
->id
);
2243 ret
= relay_process_control(&recv_hdr
,
2244 relay_connection
, relay_ctx
);
2246 /* Clear the session on error. */
2247 relay_cleanup_poll_connection(&events
, pollfd
);
2248 relay_del_connection(relay_connections_ht
,
2249 &iter
, relay_connection
, sessions_ht
);
2250 DBG("Connection closed with %d", pollfd
);
2256 * Flag the last seen data fd not deleted. It will be
2257 * used as the last seen fd if any fd gets deleted in
2260 last_notdel_data_fd
= pollfd
;
2268 * The last loop handled a control request, go back to poll to make
2269 * sure we prioritise the control socket.
2275 if (last_seen_data_fd
>= 0) {
2276 for (i
= 0; i
< nb_fd
; i
++) {
2277 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2278 if (last_seen_data_fd
== pollfd
) {
2285 /* Process data connection. */
2286 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2287 /* Fetch the poll data. */
2288 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2289 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2291 /* Skip the command pipe. It's handled in the first loop. */
2292 if (pollfd
== relay_cmd_pipe
[0]) {
2298 lttng_ht_lookup(relay_connections_ht
,
2299 (void *)((unsigned long) pollfd
),
2301 node
= lttng_ht_iter_get_node_ulong(&iter
);
2303 /* Skip it. Might be removed before. */
2307 relay_connection
= caa_container_of(node
,
2308 struct relay_command
, sock_n
);
2310 if (revents
& LPOLLIN
) {
2311 if (relay_connection
->type
!= RELAY_DATA
) {
2315 ret
= relay_process_data(relay_connection
, indexes_ht
);
2316 /* connection closed */
2318 relay_cleanup_poll_connection(&events
, pollfd
);
2319 relay_del_connection(relay_connections_ht
,
2320 &iter
, relay_connection
, sessions_ht
);
2321 DBG("Data connection closed with %d", pollfd
);
2323 * Every goto restart call sets the last seen fd where
2324 * here we don't really care since we gracefully
2325 * continue the loop after the connection is deleted.
2328 /* Keep last seen port. */
2329 last_seen_data_fd
= pollfd
;
2337 last_seen_data_fd
= -1;
2342 lttng_poll_clean(&events
);
2344 /* empty the hash table and free the memory */
2346 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2347 node
= lttng_ht_iter_get_node_ulong(&iter
);
2349 relay_connection
= caa_container_of(node
,
2350 struct relay_command
, sock_n
);
2351 relay_del_connection(relay_connections_ht
,
2352 &iter
, relay_connection
, sessions_ht
);
2357 struct relay_index
*index
;
2358 cds_lfht_for_each_entry(indexes_ht
->ht
, &iter
.iter
, index
, index_n
.node
) {
2359 relay_index_delete(index
, indexes_ht
);
2361 lttng_ht_destroy(indexes_ht
);
2365 lttng_ht_destroy(relay_connections_ht
);
2366 relay_connections_ht_error
:
2367 /* Close relay cmd pipes */
2368 utils_close_pipe(relay_cmd_pipe
);
2370 DBG("Thread exited with error");
2372 DBG("Worker thread cleanup complete");
2375 rcu_unregister_thread();
2380 * Create the relay command pipe to wake thread_manage_apps.
2381 * Closed in cleanup().
2383 static int create_relay_cmd_pipe(void)
2387 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2395 int main(int argc
, char **argv
)
2399 struct relay_local_data
*relay_ctx
;
2401 /* Create thread quit pipe */
2402 if ((ret
= init_thread_quit_pipe()) < 0) {
2406 /* Parse arguments */
2408 if ((ret
= parse_args(argc
, argv
)) < 0) {
2412 if ((ret
= set_signal_handler()) < 0) {
2416 /* Try to create directory if -o, --output is specified. */
2417 if (opt_output_path
) {
2418 if (*opt_output_path
!= '/') {
2419 ERR("Please specify an absolute path for -o, --output PATH");
2423 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2425 ERR("Unable to create %s", opt_output_path
);
2439 /* We need those values for the file/dir creation. */
2440 relayd_uid
= getuid();
2441 relayd_gid
= getgid();
2443 /* Check if daemon is UID = 0 */
2444 if (relayd_uid
== 0) {
2445 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
2446 ERR("Need to be root to use ports < 1024");
2452 /* Setup the thread apps communication pipe. */
2453 if ((ret
= create_relay_cmd_pipe()) < 0) {
2457 /* Init relay command queue. */
2458 cds_wfq_init(&relay_cmd_queue
.queue
);
2460 /* Set up max poll set size */
2461 lttng_poll_set_max_size();
2463 /* Initialize communication library */
2466 relay_ctx
= zmalloc(sizeof(struct relay_local_data
));
2468 PERROR("relay_ctx");
2472 /* tables of sessions indexed by session ID */
2473 relay_ctx
->sessions_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2474 if (!relay_ctx
->sessions_ht
) {
2475 goto exit_relay_ctx_sessions
;
2478 /* tables of streams indexed by stream ID */
2479 relay_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2480 if (!relay_streams_ht
) {
2481 goto exit_relay_ctx_streams
;
2484 /* tables of streams indexed by stream ID */
2485 viewer_streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2486 if (!viewer_streams_ht
) {
2487 goto exit_relay_ctx_viewer_streams
;
2490 /* Setup the dispatcher thread */
2491 ret
= pthread_create(&dispatcher_thread
, NULL
,
2492 relay_thread_dispatcher
, (void *) NULL
);
2494 PERROR("pthread_create dispatcher");
2495 goto exit_dispatcher
;
2498 /* Setup the worker thread */
2499 ret
= pthread_create(&worker_thread
, NULL
,
2500 relay_thread_worker
, (void *) relay_ctx
);
2502 PERROR("pthread_create worker");
2506 /* Setup the listener thread */
2507 ret
= pthread_create(&listener_thread
, NULL
,
2508 relay_thread_listener
, (void *) NULL
);
2510 PERROR("pthread_create listener");
2514 ret
= live_start_threads(live_uri
, relay_ctx
);
2516 ERR("Starting live viewer threads");
2520 ret
= pthread_join(listener_thread
, &status
);
2522 PERROR("pthread_join");
2523 goto error
; /* join error, exit without cleanup */
2527 ret
= pthread_join(worker_thread
, &status
);
2529 PERROR("pthread_join");
2530 goto error
; /* join error, exit without cleanup */
2534 ret
= pthread_join(dispatcher_thread
, &status
);
2536 PERROR("pthread_join");
2537 goto error
; /* join error, exit without cleanup */
2539 lttng_ht_destroy(viewer_streams_ht
);
2541 exit_relay_ctx_viewer_streams
:
2542 lttng_ht_destroy(relay_streams_ht
);
2544 exit_relay_ctx_streams
:
2545 lttng_ht_destroy(relay_ctx
->sessions_ht
);
2547 exit_relay_ctx_sessions
:
2551 live_stop_threads();