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>
56 #include "lttng-relayd.h"
58 /* command line options */
59 char *opt_output_path
;
60 static int opt_daemon
;
61 static struct lttng_uri
*control_uri
;
62 static struct lttng_uri
*data_uri
;
65 static int is_root
; /* Set to 1 if the daemon is running as root */
68 * Quit pipe for all threads. This permits a single cancellation point
69 * for all threads when receiving an event on the pipe.
71 static int thread_quit_pipe
[2] = { -1, -1 };
74 * This pipe is used to inform the worker thread that a command is queued and
75 * ready to be processed.
77 static int relay_cmd_pipe
[2] = { -1, -1 };
79 /* Shared between threads */
80 static int dispatch_thread_exit
;
82 static pthread_t listener_thread
;
83 static pthread_t dispatcher_thread
;
84 static pthread_t worker_thread
;
86 static uint64_t last_relay_stream_id
;
87 static uint64_t last_relay_session_id
;
90 * Relay command queue.
92 * The relay_thread_listener and relay_thread_dispatcher communicate with this
95 static struct relay_cmd_queue relay_cmd_queue
;
97 /* buffer allocated at startup, used to store the trace data */
98 static char *data_buffer
;
99 static unsigned int data_buffer_size
;
102 * usage function on stderr
107 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
108 fprintf(stderr
, " -h, --help Display this usage.\n");
109 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
110 fprintf(stderr
, " -C, --control-port URL Control port listening.\n");
111 fprintf(stderr
, " -D, --data-port URL Data port listening.\n");
112 fprintf(stderr
, " -o, --output PATH Output path for traces. Must use an absolute path.\n");
113 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
117 int parse_args(int argc
, char **argv
)
121 char *default_address
;
123 static struct option long_options
[] = {
124 { "control-port", 1, 0, 'C', },
125 { "data-port", 1, 0, 'D', },
126 { "daemonize", 0, 0, 'd', },
127 { "help", 0, 0, 'h', },
128 { "output", 1, 0, 'o', },
129 { "verbose", 0, 0, 'v', },
134 int option_index
= 0;
135 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
136 long_options
, &option_index
);
143 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
145 fprintf(stderr
, " with arg %s\n", optarg
);
149 ret
= uri_parse(optarg
, &control_uri
);
151 ERR("Invalid control URI specified");
154 if (control_uri
->port
== 0) {
155 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
159 ret
= uri_parse(optarg
, &data_uri
);
161 ERR("Invalid data URI specified");
164 if (data_uri
->port
== 0) {
165 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
175 ret
= asprintf(&opt_output_path
, "%s", optarg
);
177 PERROR("asprintf opt_output_path");
182 /* Verbose level can increase using multiple -v */
183 lttng_opt_verbose
+= 1;
186 /* Unknown option or other error.
187 * Error is printed by getopt, just return */
193 /* assign default values */
194 if (control_uri
== NULL
) {
195 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
196 DEFAULT_NETWORK_CONTROL_PORT
);
198 PERROR("asprintf default data address");
202 ret
= uri_parse(default_address
, &control_uri
);
203 free(default_address
);
205 ERR("Invalid control URI specified");
209 if (data_uri
== NULL
) {
210 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
211 DEFAULT_NETWORK_DATA_PORT
);
213 PERROR("asprintf default data address");
217 ret
= uri_parse(default_address
, &data_uri
);
218 free(default_address
);
220 ERR("Invalid data URI specified");
237 /* free the dynamically allocated opt_output_path */
238 free(opt_output_path
);
240 /* Close thread quit pipes */
241 utils_close_pipe(thread_quit_pipe
);
243 uri_free(control_uri
);
248 * Write to writable pipe used to notify a thread.
251 int notify_thread_pipe(int wpipe
)
256 ret
= write(wpipe
, "!", 1);
257 } while (ret
< 0 && errno
== EINTR
);
258 if (ret
< 0 || ret
!= 1) {
259 PERROR("write poll pipe");
266 * Stop all threads by closing the thread quit pipe.
269 void stop_threads(void)
273 /* Stopping all threads */
274 DBG("Terminating all threads");
275 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
277 ERR("write error on thread quit pipe");
280 /* Dispatch thread */
281 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
282 futex_nto1_wake(&relay_cmd_queue
.futex
);
286 * Signal handler for the daemon
288 * Simply stop all worker threads, leaving main() return gracefully after
289 * joining all threads and calling cleanup().
292 void sighandler(int sig
)
296 DBG("SIGPIPE caught");
299 DBG("SIGINT caught");
303 DBG("SIGTERM caught");
312 * Setup signal handler for :
313 * SIGINT, SIGTERM, SIGPIPE
316 int set_signal_handler(void)
322 if ((ret
= sigemptyset(&sigset
)) < 0) {
323 PERROR("sigemptyset");
327 sa
.sa_handler
= sighandler
;
330 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
335 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
340 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
345 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
351 * Init thread quit pipe.
353 * Return -1 on error or 0 if all pipes are created.
356 int init_thread_quit_pipe(void)
360 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
366 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
369 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
373 if (events
== NULL
|| size
== 0) {
378 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
384 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
396 * Check if the thread quit pipe was triggered.
398 * Return 1 if it was triggered else 0;
401 int check_thread_quit_pipe(int fd
, uint32_t events
)
403 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
411 * Create and init socket from uri.
414 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
417 struct lttcomm_sock
*sock
= NULL
;
419 sock
= lttcomm_alloc_sock_from_uri(uri
);
421 ERR("Allocating socket");
425 ret
= lttcomm_create_sock(sock
);
429 DBG("Listening on sock %d", sock
->fd
);
431 ret
= sock
->ops
->bind(sock
);
436 ret
= sock
->ops
->listen(sock
, -1);
446 lttcomm_destroy_sock(sock
);
452 * Return nonzero if stream needs to be closed.
455 int close_stream_check(struct relay_stream
*stream
)
458 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
460 * We are about to close the stream so set the data pending flag to 1
461 * which will make the end data pending command skip the stream which
462 * is now closed and ready. Note that after proceeding to a file close,
463 * the written file is ready for reading.
465 stream
->data_pending_check_done
= 1;
472 * This thread manages the listening for new connections on the network
475 void *relay_thread_listener(void *data
)
477 int i
, ret
, pollfd
, err
= -1;
479 uint32_t revents
, nb_fd
;
480 struct lttng_poll_event events
;
481 struct lttcomm_sock
*control_sock
, *data_sock
;
483 DBG("[thread] Relay listener started");
485 control_sock
= relay_init_sock(control_uri
);
487 goto error_sock_control
;
490 data_sock
= relay_init_sock(data_uri
);
492 goto error_sock_relay
;
496 * Pass 3 as size here for the thread quit pipe, control and data socket.
498 ret
= create_thread_poll_set(&events
, 3);
500 goto error_create_poll
;
503 /* Add the control socket */
504 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
509 /* Add the data socket */
510 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
516 DBG("Listener accepting connections");
519 ret
= lttng_poll_wait(&events
, -1);
522 * Restart interrupted system call.
524 if (errno
== EINTR
) {
532 DBG("Relay new connection received");
533 for (i
= 0; i
< nb_fd
; i
++) {
534 /* Fetch once the poll data */
535 revents
= LTTNG_POLL_GETEV(&events
, i
);
536 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
538 /* Thread quit pipe has been closed. Killing thread. */
539 ret
= check_thread_quit_pipe(pollfd
, revents
);
545 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
546 ERR("socket poll error");
548 } else if (revents
& LPOLLIN
) {
550 * Get allocated in this thread,
551 * enqueued to a global queue, dequeued
552 * and freed in the worker thread.
554 struct relay_command
*relay_cmd
;
555 struct lttcomm_sock
*newsock
;
557 relay_cmd
= zmalloc(sizeof(struct relay_command
));
558 if (relay_cmd
== NULL
) {
559 PERROR("relay command zmalloc");
563 if (pollfd
== data_sock
->fd
) {
564 newsock
= data_sock
->ops
->accept(data_sock
);
566 PERROR("accepting data sock");
570 relay_cmd
->type
= RELAY_DATA
;
571 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
573 assert(pollfd
== control_sock
->fd
);
574 newsock
= control_sock
->ops
->accept(control_sock
);
576 PERROR("accepting control sock");
580 relay_cmd
->type
= RELAY_CONTROL
;
581 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
583 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
586 PERROR("setsockopt inet");
587 lttcomm_destroy_sock(newsock
);
591 relay_cmd
->sock
= newsock
;
593 * Lock free enqueue the request.
595 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
598 * Wake the dispatch queue futex. Implicit memory
599 * barrier with the exchange in cds_wfq_enqueue.
601 futex_nto1_wake(&relay_cmd_queue
.futex
);
609 lttng_poll_clean(&events
);
611 if (data_sock
->fd
>= 0) {
612 ret
= data_sock
->ops
->close(data_sock
);
617 lttcomm_destroy_sock(data_sock
);
619 if (control_sock
->fd
>= 0) {
620 ret
= control_sock
->ops
->close(control_sock
);
625 lttcomm_destroy_sock(control_sock
);
628 DBG("Thread exited with error");
630 DBG("Relay listener thread cleanup complete");
636 * This thread manages the dispatching of the requests to worker threads
639 void *relay_thread_dispatcher(void *data
)
642 struct cds_wfq_node
*node
;
643 struct relay_command
*relay_cmd
= NULL
;
645 DBG("[thread] Relay dispatcher started");
647 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
648 /* Atomically prepare the queue futex */
649 futex_nto1_prepare(&relay_cmd_queue
.futex
);
652 /* Dequeue commands */
653 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
655 DBG("Woken up but nothing in the relay command queue");
656 /* Continue thread execution */
660 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
661 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
664 * Inform worker thread of the new request. This
665 * call is blocking so we can be assured that the data will be read
666 * at some point in time or wait to the end of the world :)
669 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
670 sizeof(struct relay_command
));
671 } while (ret
< 0 && errno
== EINTR
);
673 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
674 PERROR("write cmd pipe");
677 } while (node
!= NULL
);
679 /* Futex wait on queue. Blocking call on futex() */
680 futex_nto1_wait(&relay_cmd_queue
.futex
);
684 DBG("Dispatch thread dying");
690 * Get stream from stream id.
691 * Need to be called with RCU read-side lock held.
694 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
695 struct lttng_ht
*streams_ht
)
697 struct lttng_ht_node_ulong
*node
;
698 struct lttng_ht_iter iter
;
699 struct relay_stream
*ret
;
701 lttng_ht_lookup(streams_ht
,
702 (void *)((unsigned long) stream_id
),
704 node
= lttng_ht_iter_get_node_ulong(&iter
);
706 DBG("Relay stream %" PRIu64
" not found", stream_id
);
711 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
718 void deferred_free_stream(struct rcu_head
*head
)
720 struct relay_stream
*stream
=
721 caa_container_of(head
, struct relay_stream
, rcu_node
);
722 free(stream
->path_name
);
723 free(stream
->channel_name
);
728 * relay_delete_session: Free all memory associated with a session and
732 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
734 struct lttng_ht_iter iter
;
735 struct lttng_ht_node_ulong
*node
;
736 struct relay_stream
*stream
;
743 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
746 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
747 node
= lttng_ht_iter_get_node_ulong(&iter
);
749 stream
= caa_container_of(node
,
750 struct relay_stream
, stream_n
);
751 if (stream
->session
== cmd
->session
) {
752 ret
= close(stream
->fd
);
754 PERROR("close stream fd on delete session");
756 ret
= lttng_ht_del(streams_ht
, &iter
);
758 call_rcu(&stream
->rcu_node
,
759 deferred_free_stream
);
769 * Handle the RELAYD_CREATE_SESSION command.
771 * On success, send back the session id or else return a negative value.
774 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
775 struct relay_command
*cmd
)
777 int ret
= 0, send_ret
;
778 struct relay_session
*session
;
779 struct lttcomm_relayd_status_session reply
;
784 memset(&reply
, 0, sizeof(reply
));
786 session
= zmalloc(sizeof(struct relay_session
));
787 if (session
== NULL
) {
788 PERROR("relay session zmalloc");
793 session
->id
= ++last_relay_session_id
;
794 session
->sock
= cmd
->sock
;
795 cmd
->session
= session
;
797 reply
.session_id
= htobe64(session
->id
);
799 DBG("Created session %" PRIu64
, session
->id
);
803 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
805 reply
.ret_code
= htobe32(LTTNG_OK
);
808 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
810 ERR("Relayd sending session id");
818 * relay_add_stream: allocate a new stream for a session
821 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
822 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
824 struct relay_session
*session
= cmd
->session
;
825 struct relay_stream
*stream
= NULL
;
826 struct lttcomm_relayd_status_stream reply
;
829 if (!session
|| cmd
->version_check_done
== 0) {
830 ERR("Trying to add a stream before version check");
835 stream
= zmalloc(sizeof(struct relay_stream
));
836 if (stream
== NULL
) {
837 PERROR("relay stream zmalloc");
842 switch (cmd
->minor
) {
843 case 1: /* LTTng sessiond 2.1 */
844 ret
= cmd_recv_stream_2_1(cmd
, stream
);
846 case 2: /* LTTng sessiond 2.2 */
848 ret
= cmd_recv_stream_2_2(cmd
, stream
);
852 goto err_free_stream
;
856 stream
->stream_handle
= ++last_relay_stream_id
;
857 stream
->prev_seq
= -1ULL;
858 stream
->session
= session
;
860 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
);
862 ERR("relay creating output directory");
867 * No need to use run_as API here because whatever we receives, the relayd
868 * uses its own credentials for the stream files.
870 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
871 stream
->tracefile_size
, 0, -1, -1);
873 ERR("Create output file");
877 if (stream
->tracefile_size
) {
878 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
880 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
883 lttng_ht_node_init_ulong(&stream
->stream_n
,
884 (unsigned long) stream
->stream_handle
);
885 lttng_ht_add_unique_ulong(streams_ht
,
888 DBG("Relay new stream added %s", stream
->channel_name
);
891 reply
.handle
= htobe64(stream
->stream_handle
);
892 /* send the session id to the client or a negative return code on error */
894 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
895 /* stream was not properly added to the ht, so free it */
898 reply
.ret_code
= htobe32(LTTNG_OK
);
901 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
902 sizeof(struct lttcomm_relayd_status_stream
), 0);
904 ERR("Relay sending stream id");
913 free(stream
->path_name
);
914 free(stream
->channel_name
);
920 * relay_close_stream: close a specific stream
923 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
924 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
926 struct relay_session
*session
= cmd
->session
;
927 struct lttcomm_relayd_close_stream stream_info
;
928 struct lttcomm_relayd_generic_reply reply
;
929 struct relay_stream
*stream
;
931 struct lttng_ht_iter iter
;
933 DBG("Close stream received");
935 if (!session
|| cmd
->version_check_done
== 0) {
936 ERR("Trying to close a stream before version check");
941 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
942 sizeof(struct lttcomm_relayd_close_stream
), 0);
943 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
945 /* Orderly shutdown. Not necessary to print an error. */
946 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
948 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
955 stream
= relay_stream_from_stream_id(be64toh(stream_info
.stream_id
),
962 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
963 stream
->close_flag
= 1;
965 if (close_stream_check(stream
)) {
968 delret
= close(stream
->fd
);
970 PERROR("close stream");
972 iter
.iter
.node
= &stream
->stream_n
.node
;
973 delret
= lttng_ht_del(streams_ht
, &iter
);
975 call_rcu(&stream
->rcu_node
,
976 deferred_free_stream
);
977 DBG("Closed tracefile %d from close stream", stream
->fd
);
984 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
986 reply
.ret_code
= htobe32(LTTNG_OK
);
988 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
989 sizeof(struct lttcomm_relayd_generic_reply
), 0);
991 ERR("Relay sending stream id");
1000 * relay_unknown_command: send -1 if received unknown command
1003 void relay_unknown_command(struct relay_command
*cmd
)
1005 struct lttcomm_relayd_generic_reply reply
;
1008 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1009 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1010 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1012 ERR("Relay sending unknown command");
1017 * relay_start: send an acknowledgment to the client to tell if we are
1018 * ready to receive data. We are ready if a session is established.
1021 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1022 struct relay_command
*cmd
)
1024 int ret
= htobe32(LTTNG_OK
);
1025 struct lttcomm_relayd_generic_reply reply
;
1026 struct relay_session
*session
= cmd
->session
;
1029 DBG("Trying to start the streaming without a session established");
1030 ret
= htobe32(LTTNG_ERR_UNK
);
1033 reply
.ret_code
= ret
;
1034 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1035 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1037 ERR("Relay sending start ack");
1044 * Append padding to the file pointed by the file descriptor fd.
1046 static int write_padding_to_file(int fd
, uint32_t size
)
1055 zeros
= zmalloc(size
);
1056 if (zeros
== NULL
) {
1057 PERROR("zmalloc zeros for padding");
1063 ret
= write(fd
, zeros
, size
);
1064 } while (ret
< 0 && errno
== EINTR
);
1065 if (ret
< 0 || ret
!= size
) {
1066 PERROR("write padding to file");
1076 * relay_recv_metadata: receive the metada for the session.
1079 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1080 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1082 int ret
= htobe32(LTTNG_OK
);
1083 struct relay_session
*session
= cmd
->session
;
1084 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1085 struct relay_stream
*metadata_stream
;
1086 uint64_t data_size
, payload_size
;
1089 ERR("Metadata sent before version check");
1094 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1095 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1096 ERR("Incorrect data size");
1100 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1102 if (data_buffer_size
< data_size
) {
1103 /* In case the realloc fails, we can free the memory */
1106 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1107 if (!tmp_data_ptr
) {
1108 ERR("Allocating data buffer");
1113 data_buffer
= tmp_data_ptr
;
1114 data_buffer_size
= data_size
;
1116 memset(data_buffer
, 0, data_size
);
1117 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1118 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1119 if (ret
< 0 || ret
!= data_size
) {
1121 /* Orderly shutdown. Not necessary to print an error. */
1122 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1124 ERR("Relay didn't receive the whole metadata");
1129 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1132 metadata_stream
= relay_stream_from_stream_id(
1133 be64toh(metadata_struct
->stream_id
), streams_ht
);
1134 if (!metadata_stream
) {
1140 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1142 } while (ret
< 0 && errno
== EINTR
);
1143 if (ret
< 0 || ret
!= payload_size
) {
1144 ERR("Relay error writing metadata on file");
1149 ret
= write_padding_to_file(metadata_stream
->fd
,
1150 be32toh(metadata_struct
->padding_size
));
1155 DBG2("Relay metadata written");
1164 * relay_send_version: send relayd version number
1167 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1168 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1171 struct lttcomm_relayd_version reply
, msg
;
1175 cmd
->version_check_done
= 1;
1177 /* Get version from the other side. */
1178 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1179 if (ret
< 0 || ret
!= sizeof(msg
)) {
1181 /* Orderly shutdown. Not necessary to print an error. */
1182 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1184 ERR("Relay failed to receive the version values.");
1190 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1191 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1193 /* Major versions must be the same */
1194 if (reply
.major
!= be32toh(msg
.major
)) {
1195 DBG("Incompatible major versions (%u vs %u), deleting session",
1196 reply
.major
, be32toh(msg
.major
));
1197 relay_delete_session(cmd
, streams_ht
);
1202 cmd
->major
= reply
.major
;
1203 /* We adapt to the lowest compatible version */
1204 if (reply
.minor
<= be32toh(msg
.minor
)) {
1205 cmd
->minor
= reply
.minor
;
1207 cmd
->minor
= be32toh(msg
.minor
);
1210 reply
.major
= htobe32(reply
.major
);
1211 reply
.minor
= htobe32(reply
.minor
);
1212 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1213 sizeof(struct lttcomm_relayd_version
), 0);
1215 ERR("Relay sending version");
1218 DBG("Version check done using protocol %u.%u", cmd
->major
,
1226 * Check for data pending for a given stream id from the session daemon.
1229 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1230 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1232 struct relay_session
*session
= cmd
->session
;
1233 struct lttcomm_relayd_data_pending msg
;
1234 struct lttcomm_relayd_generic_reply reply
;
1235 struct relay_stream
*stream
;
1237 uint64_t last_net_seq_num
, stream_id
;
1239 DBG("Data pending command received");
1241 if (!session
|| cmd
->version_check_done
== 0) {
1242 ERR("Trying to check for data before version check");
1244 goto end_no_session
;
1247 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1248 if (ret
< sizeof(msg
)) {
1250 /* Orderly shutdown. Not necessary to print an error. */
1251 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1253 ERR("Relay didn't receive valid data_pending struct size : %d",
1257 goto end_no_session
;
1260 stream_id
= be64toh(msg
.stream_id
);
1261 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1264 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1265 if (stream
== NULL
) {
1270 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1271 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1274 /* Avoid wrapping issue */
1275 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1276 /* Data has in fact been written and is NOT pending */
1279 /* Data still being streamed thus pending */
1283 /* Pending check is now done. */
1284 stream
->data_pending_check_done
= 1;
1289 reply
.ret_code
= htobe32(ret
);
1290 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1292 ERR("Relay data pending ret code failed");
1300 * Wait for the control socket to reach a quiescent state.
1302 * Note that for now, when receiving this command from the session daemon, this
1303 * means that every subsequent commands or data received on the control socket
1304 * has been handled. So, this is why we simply return OK here.
1307 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1308 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1312 struct relay_stream
*stream
;
1313 struct lttng_ht_iter iter
;
1314 struct lttcomm_relayd_quiescent_control msg
;
1315 struct lttcomm_relayd_generic_reply reply
;
1317 DBG("Checking quiescent state on control socket");
1319 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1320 ERR("Trying to check for data before version check");
1322 goto end_no_session
;
1325 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1326 if (ret
< sizeof(msg
)) {
1328 /* Orderly shutdown. Not necessary to print an error. */
1329 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1331 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1335 goto end_no_session
;
1338 stream_id
= be64toh(msg
.stream_id
);
1341 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1342 if (stream
->stream_handle
== stream_id
) {
1343 stream
->data_pending_check_done
= 1;
1344 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1351 reply
.ret_code
= htobe32(LTTNG_OK
);
1352 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1354 ERR("Relay data quiescent control ret code failed");
1362 * Initialize a data pending command. This means that a client is about to ask
1363 * for data pending for each stream he/she holds. Simply iterate over all
1364 * streams of a session and set the data_pending_check_done flag.
1366 * This command returns to the client a LTTNG_OK code.
1369 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1370 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1373 struct lttng_ht_iter iter
;
1374 struct lttcomm_relayd_begin_data_pending msg
;
1375 struct lttcomm_relayd_generic_reply reply
;
1376 struct relay_stream
*stream
;
1377 uint64_t session_id
;
1383 DBG("Init streams for data pending");
1385 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1386 ERR("Trying to check for data before version check");
1388 goto end_no_session
;
1391 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1392 if (ret
< sizeof(msg
)) {
1394 /* Orderly shutdown. Not necessary to print an error. */
1395 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1397 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1401 goto end_no_session
;
1404 session_id
= be64toh(msg
.session_id
);
1407 * Iterate over all streams to set the begin data pending flag. For now, the
1408 * streams are indexed by stream handle so we have to iterate over all
1409 * streams to find the one associated with the right session_id.
1412 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1413 if (stream
->session
->id
== session_id
) {
1414 stream
->data_pending_check_done
= 0;
1415 DBG("Set begin data pending flag to stream %" PRIu64
,
1416 stream
->stream_handle
);
1421 /* All good, send back reply. */
1422 reply
.ret_code
= htobe32(LTTNG_OK
);
1424 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1426 ERR("Relay begin data pending send reply failed");
1434 * End data pending command. This will check, for a given session id, if each
1435 * stream associated with it has its data_pending_check_done flag set. If not,
1436 * this means that the client lost track of the stream but the data is still
1437 * being streamed on our side. In this case, we inform the client that data is
1440 * Return to the client if there is data in flight or not with a ret_code.
1443 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1444 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1447 struct lttng_ht_iter iter
;
1448 struct lttcomm_relayd_end_data_pending msg
;
1449 struct lttcomm_relayd_generic_reply reply
;
1450 struct relay_stream
*stream
;
1451 uint64_t session_id
;
1452 uint32_t is_data_inflight
= 0;
1458 DBG("End data pending command");
1460 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1461 ERR("Trying to check for data before version check");
1463 goto end_no_session
;
1466 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1467 if (ret
< sizeof(msg
)) {
1469 /* Orderly shutdown. Not necessary to print an error. */
1470 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1472 ERR("Relay didn't receive valid end data_pending struct size: %d",
1476 goto end_no_session
;
1479 session_id
= be64toh(msg
.session_id
);
1481 /* Iterate over all streams to see if the begin data pending flag is set. */
1483 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1484 if (stream
->session
->id
== session_id
&&
1485 !stream
->data_pending_check_done
) {
1486 is_data_inflight
= 1;
1487 DBG("Data is still in flight for stream %" PRIu64
,
1488 stream
->stream_handle
);
1494 /* All good, send back reply. */
1495 reply
.ret_code
= htobe32(is_data_inflight
);
1497 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1499 ERR("Relay end data pending send reply failed");
1507 * relay_process_control: Process the commands received on the control socket
1510 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1511 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1515 switch (be32toh(recv_hdr
->cmd
)) {
1516 case RELAYD_CREATE_SESSION
:
1517 ret
= relay_create_session(recv_hdr
, cmd
);
1519 case RELAYD_ADD_STREAM
:
1520 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1522 case RELAYD_START_DATA
:
1523 ret
= relay_start(recv_hdr
, cmd
);
1525 case RELAYD_SEND_METADATA
:
1526 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1528 case RELAYD_VERSION
:
1529 ret
= relay_send_version(recv_hdr
, cmd
, streams_ht
);
1531 case RELAYD_CLOSE_STREAM
:
1532 ret
= relay_close_stream(recv_hdr
, cmd
, streams_ht
);
1534 case RELAYD_DATA_PENDING
:
1535 ret
= relay_data_pending(recv_hdr
, cmd
, streams_ht
);
1537 case RELAYD_QUIESCENT_CONTROL
:
1538 ret
= relay_quiescent_control(recv_hdr
, cmd
, streams_ht
);
1540 case RELAYD_BEGIN_DATA_PENDING
:
1541 ret
= relay_begin_data_pending(recv_hdr
, cmd
, streams_ht
);
1543 case RELAYD_END_DATA_PENDING
:
1544 ret
= relay_end_data_pending(recv_hdr
, cmd
, streams_ht
);
1546 case RELAYD_UPDATE_SYNC_INFO
:
1548 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1549 relay_unknown_command(cmd
);
1559 * relay_process_data: Process the data received on the data socket
1562 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1565 struct relay_stream
*stream
;
1566 struct lttcomm_relayd_data_hdr data_hdr
;
1568 uint64_t net_seq_num
;
1571 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1572 sizeof(struct lttcomm_relayd_data_hdr
), 0);
1575 /* Orderly shutdown. Not necessary to print an error. */
1576 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1578 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
1584 stream_id
= be64toh(data_hdr
.stream_id
);
1587 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1593 data_size
= be32toh(data_hdr
.data_size
);
1594 if (data_buffer_size
< data_size
) {
1597 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1598 if (!tmp_data_ptr
) {
1599 ERR("Allocating data buffer");
1604 data_buffer
= tmp_data_ptr
;
1605 data_buffer_size
= data_size
;
1607 memset(data_buffer
, 0, data_size
);
1609 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1611 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1612 data_size
, stream_id
, net_seq_num
);
1613 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1616 /* Orderly shutdown. Not necessary to print an error. */
1617 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1623 if (stream
->tracefile_size
> 0 &&
1624 (stream
->tracefile_size_current
+ data_size
) >
1625 stream
->tracefile_size
) {
1626 ret
= utils_rotate_stream_file(stream
->path_name
,
1627 stream
->channel_name
, stream
->tracefile_size
,
1628 stream
->tracefile_count
, -1, -1,
1629 stream
->fd
, &(stream
->tracefile_count_current
));
1631 ERR("Rotating output file");
1635 /* Reset current size because we just perform a stream rotation. */
1636 stream
->tracefile_size_current
= 0;
1638 stream
->tracefile_size_current
+= data_size
;
1640 ret
= write(stream
->fd
, data_buffer
, data_size
);
1641 } while (ret
< 0 && errno
== EINTR
);
1642 if (ret
< 0 || ret
!= data_size
) {
1643 ERR("Relay error writing data to file");
1648 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
1649 ret
, stream
->stream_handle
);
1651 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
1656 stream
->prev_seq
= net_seq_num
;
1658 /* Check if we need to close the FD */
1659 if (close_stream_check(stream
)) {
1661 struct lttng_ht_iter iter
;
1663 cret
= close(stream
->fd
);
1665 PERROR("close stream process data");
1667 iter
.iter
.node
= &stream
->stream_n
.node
;
1668 ret
= lttng_ht_del(streams_ht
, &iter
);
1670 call_rcu(&stream
->rcu_node
,
1671 deferred_free_stream
);
1672 DBG("Closed tracefile %d after recv data", stream
->fd
);
1682 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1686 lttng_poll_del(events
, pollfd
);
1688 ret
= close(pollfd
);
1690 ERR("Closing pollfd %d", pollfd
);
1695 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1696 struct lttng_ht
*relay_connections_ht
)
1698 struct relay_command
*relay_connection
;
1701 relay_connection
= zmalloc(sizeof(struct relay_command
));
1702 if (relay_connection
== NULL
) {
1703 PERROR("Relay command zmalloc");
1707 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1708 } while (ret
< 0 && errno
== EINTR
);
1709 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
1710 PERROR("read relay cmd pipe");
1714 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1715 (unsigned long) relay_connection
->sock
->fd
);
1717 lttng_ht_add_unique_ulong(relay_connections_ht
,
1718 &relay_connection
->sock_n
);
1720 return lttng_poll_add(events
,
1721 relay_connection
->sock
->fd
,
1722 LPOLLIN
| LPOLLRDHUP
);
1725 free(relay_connection
);
1731 void deferred_free_connection(struct rcu_head
*head
)
1733 struct relay_command
*relay_connection
=
1734 caa_container_of(head
, struct relay_command
, rcu_node
);
1736 lttcomm_destroy_sock(relay_connection
->sock
);
1737 free(relay_connection
);
1741 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
1742 struct lttng_ht
*streams_ht
, struct lttng_ht_iter
*iter
,
1743 struct relay_command
*relay_connection
)
1747 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1749 if (relay_connection
->type
== RELAY_CONTROL
) {
1750 relay_delete_session(relay_connection
, streams_ht
);
1753 call_rcu(&relay_connection
->rcu_node
,
1754 deferred_free_connection
);
1758 * This thread does the actual work
1761 void *relay_thread_worker(void *data
)
1763 int ret
, err
= -1, last_seen_data_fd
= -1;
1765 struct relay_command
*relay_connection
;
1766 struct lttng_poll_event events
;
1767 struct lttng_ht
*relay_connections_ht
;
1768 struct lttng_ht_node_ulong
*node
;
1769 struct lttng_ht_iter iter
;
1770 struct lttng_ht
*streams_ht
;
1771 struct lttcomm_relayd_hdr recv_hdr
;
1773 DBG("[thread] Relay worker started");
1775 rcu_register_thread();
1777 /* table of connections indexed on socket */
1778 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1779 if (!relay_connections_ht
) {
1780 goto relay_connections_ht_error
;
1783 /* tables of streams indexed by stream ID */
1784 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1786 goto streams_ht_error
;
1789 ret
= create_thread_poll_set(&events
, 2);
1791 goto error_poll_create
;
1794 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1801 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
1803 /* Infinite blocking call, waiting for transmission */
1804 DBG3("Relayd worker thread polling...");
1805 ret
= lttng_poll_wait(&events
, -1);
1808 * Restart interrupted system call.
1810 if (errno
== EINTR
) {
1819 * Process control. The control connection is prioritised so we don't
1820 * starve it with high throughout put tracing data on the data
1823 for (i
= 0; i
< nb_fd
; i
++) {
1824 /* Fetch once the poll data */
1825 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1826 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1828 /* Thread quit pipe has been closed. Killing thread. */
1829 ret
= check_thread_quit_pipe(pollfd
, revents
);
1835 /* Inspect the relay cmd pipe for new connection */
1836 if (pollfd
== relay_cmd_pipe
[0]) {
1837 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1838 ERR("Relay pipe error");
1840 } else if (revents
& LPOLLIN
) {
1841 DBG("Relay command received");
1842 ret
= relay_add_connection(relay_cmd_pipe
[0],
1843 &events
, relay_connections_ht
);
1848 } else if (revents
) {
1850 lttng_ht_lookup(relay_connections_ht
,
1851 (void *)((unsigned long) pollfd
),
1853 node
= lttng_ht_iter_get_node_ulong(&iter
);
1855 DBG2("Relay sock %d not found", pollfd
);
1859 relay_connection
= caa_container_of(node
,
1860 struct relay_command
, sock_n
);
1862 if (revents
& (LPOLLERR
)) {
1864 relay_cleanup_poll_connection(&events
, pollfd
);
1865 relay_del_connection(relay_connections_ht
,
1868 if (last_seen_data_fd
== pollfd
) {
1869 last_seen_data_fd
= last_notdel_data_fd
;
1871 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1872 DBG("Socket %d hung up", pollfd
);
1873 relay_cleanup_poll_connection(&events
, pollfd
);
1874 relay_del_connection(relay_connections_ht
,
1877 if (last_seen_data_fd
== pollfd
) {
1878 last_seen_data_fd
= last_notdel_data_fd
;
1880 } else if (revents
& LPOLLIN
) {
1881 /* control socket */
1882 if (relay_connection
->type
== RELAY_CONTROL
) {
1883 ret
= relay_connection
->sock
->ops
->recvmsg(
1884 relay_connection
->sock
, &recv_hdr
,
1885 sizeof(struct lttcomm_relayd_hdr
), 0);
1886 /* connection closed */
1888 relay_cleanup_poll_connection(&events
, pollfd
);
1889 relay_del_connection(relay_connections_ht
,
1892 DBG("Control connection closed with %d", pollfd
);
1894 if (relay_connection
->session
) {
1895 DBG2("Relay worker receiving data for session : %" PRIu64
,
1896 relay_connection
->session
->id
);
1898 ret
= relay_process_control(&recv_hdr
,
1902 /* Clear the session on error. */
1903 relay_cleanup_poll_connection(&events
, pollfd
);
1904 relay_del_connection(relay_connections_ht
,
1907 DBG("Connection closed with %d", pollfd
);
1913 * Flag the last seen data fd not deleted. It will be
1914 * used as the last seen fd if any fd gets deleted in
1917 last_notdel_data_fd
= pollfd
;
1925 * The last loop handled a control request, go back to poll to make
1926 * sure we prioritise the control socket.
1932 if (last_seen_data_fd
>= 0) {
1933 for (i
= 0; i
< nb_fd
; i
++) {
1934 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1935 if (last_seen_data_fd
== pollfd
) {
1942 /* Process data connection. */
1943 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
1944 /* Fetch the poll data. */
1945 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1946 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1948 /* Skip the command pipe. It's handled in the first loop. */
1949 if (pollfd
== relay_cmd_pipe
[0]) {
1955 lttng_ht_lookup(relay_connections_ht
,
1956 (void *)((unsigned long) pollfd
),
1958 node
= lttng_ht_iter_get_node_ulong(&iter
);
1960 /* Skip it. Might be removed before. */
1964 relay_connection
= caa_container_of(node
,
1965 struct relay_command
, sock_n
);
1967 if (revents
& LPOLLIN
) {
1968 if (relay_connection
->type
!= RELAY_DATA
) {
1972 ret
= relay_process_data(relay_connection
, streams_ht
);
1973 /* connection closed */
1975 relay_cleanup_poll_connection(&events
, pollfd
);
1976 relay_del_connection(relay_connections_ht
,
1979 DBG("Data connection closed with %d", pollfd
);
1981 * Every goto restart call sets the last seen fd where
1982 * here we don't really care since we gracefully
1983 * continue the loop after the connection is deleted.
1986 /* Keep last seen port. */
1987 last_seen_data_fd
= pollfd
;
1995 last_seen_data_fd
= -1;
2000 lttng_poll_clean(&events
);
2002 /* empty the hash table and free the memory */
2004 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2005 node
= lttng_ht_iter_get_node_ulong(&iter
);
2007 relay_connection
= caa_container_of(node
,
2008 struct relay_command
, sock_n
);
2009 relay_del_connection(relay_connections_ht
,
2016 lttng_ht_destroy(streams_ht
);
2018 lttng_ht_destroy(relay_connections_ht
);
2019 relay_connections_ht_error
:
2020 /* Close relay cmd pipes */
2021 utils_close_pipe(relay_cmd_pipe
);
2023 DBG("Thread exited with error");
2025 DBG("Worker thread cleanup complete");
2028 rcu_unregister_thread();
2033 * Create the relay command pipe to wake thread_manage_apps.
2034 * Closed in cleanup().
2036 static int create_relay_cmd_pipe(void)
2040 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2048 int main(int argc
, char **argv
)
2053 /* Create thread quit pipe */
2054 if ((ret
= init_thread_quit_pipe()) < 0) {
2058 /* Parse arguments */
2060 if ((ret
= parse_args(argc
, argv
)) < 0) {
2064 if ((ret
= set_signal_handler()) < 0) {
2068 /* Try to create directory if -o, --output is specified. */
2069 if (opt_output_path
) {
2070 if (*opt_output_path
!= '/') {
2071 ERR("Please specify an absolute path for -o, --output PATH");
2075 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2077 ERR("Unable to create %s", opt_output_path
);
2091 /* Check if daemon is UID = 0 */
2092 is_root
= !getuid();
2095 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
2096 ERR("Need to be root to use ports < 1024");
2102 /* Setup the thread apps communication pipe. */
2103 if ((ret
= create_relay_cmd_pipe()) < 0) {
2107 /* Init relay command queue. */
2108 cds_wfq_init(&relay_cmd_queue
.queue
);
2110 /* Set up max poll set size */
2111 lttng_poll_set_max_size();
2113 /* Initialize communication library */
2116 /* Setup the dispatcher thread */
2117 ret
= pthread_create(&dispatcher_thread
, NULL
,
2118 relay_thread_dispatcher
, (void *) NULL
);
2120 PERROR("pthread_create dispatcher");
2121 goto exit_dispatcher
;
2124 /* Setup the worker thread */
2125 ret
= pthread_create(&worker_thread
, NULL
,
2126 relay_thread_worker
, (void *) NULL
);
2128 PERROR("pthread_create worker");
2132 /* Setup the listener thread */
2133 ret
= pthread_create(&listener_thread
, NULL
,
2134 relay_thread_listener
, (void *) NULL
);
2136 PERROR("pthread_create listener");
2141 ret
= pthread_join(listener_thread
, &status
);
2143 PERROR("pthread_join");
2144 goto error
; /* join error, exit without cleanup */
2148 ret
= pthread_join(worker_thread
, &status
);
2150 PERROR("pthread_join");
2151 goto error
; /* join error, exit without cleanup */
2155 ret
= pthread_join(dispatcher_thread
, &status
);
2157 PERROR("pthread_join");
2158 goto error
; /* join error, exit without cleanup */