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/hashtable/hashtable.h>
51 #include <common/sessiond-comm/relayd.h>
52 #include <common/uri.h>
53 #include <common/utils.h>
55 #include "lttng-relayd.h"
57 /* command line options */
58 static int opt_daemon
;
59 static char *opt_output_path
;
60 static struct lttng_uri
*control_uri
;
61 static struct lttng_uri
*data_uri
;
64 static int is_root
; /* Set to 1 if the daemon is running as root */
67 * Quit pipe for all threads. This permits a single cancellation point
68 * for all threads when receiving an event on the pipe.
70 static int thread_quit_pipe
[2] = { -1, -1 };
73 * This pipe is used to inform the worker thread that a command is queued and
74 * ready to be processed.
76 static int relay_cmd_pipe
[2] = { -1, -1 };
78 /* Shared between threads */
79 static int dispatch_thread_exit
;
81 static pthread_t listener_thread
;
82 static pthread_t dispatcher_thread
;
83 static pthread_t worker_thread
;
85 static uint64_t last_relay_stream_id
;
86 static uint64_t last_relay_session_id
;
89 * Relay command queue.
91 * The relay_thread_listener and relay_thread_dispatcher communicate with this
94 static struct relay_cmd_queue relay_cmd_queue
;
96 /* buffer allocated at startup, used to store the trace data */
97 static char *data_buffer
;
98 static unsigned int data_buffer_size
;
101 * usage function on stderr
106 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
107 fprintf(stderr
, " -h, --help Display this usage.\n");
108 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
109 fprintf(stderr
, " -C, --control-port Control port listening (URI)\n");
110 fprintf(stderr
, " -D, --data-port Data port listening (URI)\n");
111 fprintf(stderr
, " -o, --output Output path for traces (PATH)\n");
112 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
116 int parse_args(int argc
, char **argv
)
120 char *default_address
;
122 static struct option long_options
[] = {
123 { "control-port", 1, 0, 'C', },
124 { "data-port", 1, 0, 'D', },
125 { "daemonize", 0, 0, 'd', },
126 { "help", 0, 0, 'h', },
127 { "output", 1, 0, 'o', },
128 { "verbose", 0, 0, 'v', },
133 int option_index
= 0;
134 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
135 long_options
, &option_index
);
142 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
144 fprintf(stderr
, " with arg %s\n", optarg
);
148 ret
= uri_parse(optarg
, &control_uri
);
150 ERR("Invalid control URI specified");
153 if (control_uri
->port
== 0) {
154 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
158 ret
= uri_parse(optarg
, &data_uri
);
160 ERR("Invalid data URI specified");
163 if (data_uri
->port
== 0) {
164 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
174 ret
= asprintf(&opt_output_path
, "%s", optarg
);
176 PERROR("asprintf opt_output_path");
181 /* Verbose level can increase using multiple -v */
182 lttng_opt_verbose
+= 1;
185 /* Unknown option or other error.
186 * Error is printed by getopt, just return */
192 /* assign default values */
193 if (control_uri
== NULL
) {
194 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
195 DEFAULT_NETWORK_CONTROL_PORT
);
197 PERROR("asprintf default data address");
201 ret
= uri_parse(default_address
, &control_uri
);
202 free(default_address
);
204 ERR("Invalid control URI specified");
208 if (data_uri
== NULL
) {
209 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
210 DEFAULT_NETWORK_DATA_PORT
);
212 PERROR("asprintf default data address");
216 ret
= uri_parse(default_address
, &data_uri
);
217 free(default_address
);
219 ERR("Invalid data URI specified");
236 /* free the dynamically allocated opt_output_path */
237 free(opt_output_path
);
239 /* Close thread quit pipes */
240 utils_close_pipe(thread_quit_pipe
);
242 uri_free(control_uri
);
247 * Write to writable pipe used to notify a thread.
250 int notify_thread_pipe(int wpipe
)
255 ret
= write(wpipe
, "!", 1);
256 } while (ret
< 0 && errno
== EINTR
);
257 if (ret
< 0 || ret
!= 1) {
258 PERROR("write poll pipe");
265 * Stop all threads by closing the thread quit pipe.
268 void stop_threads(void)
272 /* Stopping all threads */
273 DBG("Terminating all threads");
274 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
276 ERR("write error on thread quit pipe");
279 /* Dispatch thread */
280 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
281 futex_nto1_wake(&relay_cmd_queue
.futex
);
285 * Signal handler for the daemon
287 * Simply stop all worker threads, leaving main() return gracefully after
288 * joining all threads and calling cleanup().
291 void sighandler(int sig
)
295 DBG("SIGPIPE caught");
298 DBG("SIGINT caught");
302 DBG("SIGTERM caught");
311 * Setup signal handler for :
312 * SIGINT, SIGTERM, SIGPIPE
315 int set_signal_handler(void)
321 if ((ret
= sigemptyset(&sigset
)) < 0) {
322 PERROR("sigemptyset");
326 sa
.sa_handler
= sighandler
;
329 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
334 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
339 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
344 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
350 * Init thread quit pipe.
352 * Return -1 on error or 0 if all pipes are created.
355 int init_thread_quit_pipe(void)
359 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
365 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
368 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
372 if (events
== NULL
|| size
== 0) {
377 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
383 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
395 * Check if the thread quit pipe was triggered.
397 * Return 1 if it was triggered else 0;
400 int check_thread_quit_pipe(int fd
, uint32_t events
)
402 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
410 * Create and init socket from uri.
413 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
416 struct lttcomm_sock
*sock
= NULL
;
418 sock
= lttcomm_alloc_sock_from_uri(uri
);
420 ERR("Allocating socket");
424 ret
= lttcomm_create_sock(sock
);
428 DBG("Listening on sock %d", sock
->fd
);
430 ret
= sock
->ops
->bind(sock
);
435 ret
= sock
->ops
->listen(sock
, -1);
445 lttcomm_destroy_sock(sock
);
451 * Return nonzero if stream needs to be closed.
454 int close_stream_check(struct relay_stream
*stream
)
457 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
459 * We are about to close the stream so set the data pending flag to 1
460 * which will make the end data pending command skip the stream which
461 * is now closed and ready. Note that after proceeding to a file close,
462 * the written file is ready for reading.
464 stream
->data_pending_check_done
= 1;
471 * This thread manages the listening for new connections on the network
474 void *relay_thread_listener(void *data
)
476 int i
, ret
, pollfd
, err
= -1;
478 uint32_t revents
, nb_fd
;
479 struct lttng_poll_event events
;
480 struct lttcomm_sock
*control_sock
, *data_sock
;
482 DBG("[thread] Relay listener started");
484 control_sock
= relay_init_sock(control_uri
);
486 goto error_sock_control
;
489 data_sock
= relay_init_sock(data_uri
);
491 goto error_sock_relay
;
495 * Pass 3 as size here for the thread quit pipe, control and data socket.
497 ret
= create_thread_poll_set(&events
, 3);
499 goto error_create_poll
;
502 /* Add the control socket */
503 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
508 /* Add the data socket */
509 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
515 DBG("Listener accepting connections");
518 ret
= lttng_poll_wait(&events
, -1);
521 * Restart interrupted system call.
523 if (errno
== EINTR
) {
531 DBG("Relay new connection received");
532 for (i
= 0; i
< nb_fd
; i
++) {
533 /* Fetch once the poll data */
534 revents
= LTTNG_POLL_GETEV(&events
, i
);
535 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
537 /* Thread quit pipe has been closed. Killing thread. */
538 ret
= check_thread_quit_pipe(pollfd
, revents
);
544 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
545 ERR("socket poll error");
547 } else if (revents
& LPOLLIN
) {
549 * Get allocated in this thread,
550 * enqueued to a global queue, dequeued
551 * and freed in the worker thread.
553 struct relay_command
*relay_cmd
;
554 struct lttcomm_sock
*newsock
;
556 relay_cmd
= zmalloc(sizeof(struct relay_command
));
557 if (relay_cmd
== NULL
) {
558 PERROR("relay command zmalloc");
562 if (pollfd
== data_sock
->fd
) {
563 newsock
= data_sock
->ops
->accept(data_sock
);
565 PERROR("accepting data sock");
569 relay_cmd
->type
= RELAY_DATA
;
570 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
572 assert(pollfd
== control_sock
->fd
);
573 newsock
= control_sock
->ops
->accept(control_sock
);
575 PERROR("accepting control sock");
579 relay_cmd
->type
= RELAY_CONTROL
;
580 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
582 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
585 PERROR("setsockopt inet");
586 lttcomm_destroy_sock(newsock
);
590 relay_cmd
->sock
= newsock
;
592 * Lock free enqueue the request.
594 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
597 * Wake the dispatch queue futex. Implicit memory
598 * barrier with the exchange in cds_wfq_enqueue.
600 futex_nto1_wake(&relay_cmd_queue
.futex
);
608 lttng_poll_clean(&events
);
610 if (data_sock
->fd
>= 0) {
611 ret
= data_sock
->ops
->close(data_sock
);
616 lttcomm_destroy_sock(data_sock
);
618 if (control_sock
->fd
>= 0) {
619 ret
= control_sock
->ops
->close(control_sock
);
624 lttcomm_destroy_sock(control_sock
);
627 DBG("Thread exited with error");
629 DBG("Relay listener thread cleanup complete");
635 * This thread manages the dispatching of the requests to worker threads
638 void *relay_thread_dispatcher(void *data
)
641 struct cds_wfq_node
*node
;
642 struct relay_command
*relay_cmd
= NULL
;
644 DBG("[thread] Relay dispatcher started");
646 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
647 /* Atomically prepare the queue futex */
648 futex_nto1_prepare(&relay_cmd_queue
.futex
);
651 /* Dequeue commands */
652 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
654 DBG("Woken up but nothing in the relay command queue");
655 /* Continue thread execution */
659 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
660 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
663 * Inform worker thread of the new request. This
664 * call is blocking so we can be assured that the data will be read
665 * at some point in time or wait to the end of the world :)
668 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
669 sizeof(struct relay_command
));
670 } while (ret
< 0 && errno
== EINTR
);
672 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
673 PERROR("write cmd pipe");
676 } while (node
!= NULL
);
678 /* Futex wait on queue. Blocking call on futex() */
679 futex_nto1_wait(&relay_cmd_queue
.futex
);
683 DBG("Dispatch thread dying");
689 * config_get_default_path
691 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
694 char *config_get_default_path(void)
696 return getenv("HOME");
700 char *create_output_path_auto(char *path_name
)
703 char *traces_path
= NULL
;
704 char *alloc_path
= NULL
;
707 default_path
= config_get_default_path();
708 if (default_path
== NULL
) {
709 ERR("Home path not found.\n \
710 Please specify an output path using -o, --output PATH");
713 alloc_path
= strdup(default_path
);
714 if (alloc_path
== NULL
) {
715 PERROR("Path allocation");
718 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
719 "/%s", alloc_path
, path_name
);
721 PERROR("asprintf trace dir name");
730 char *create_output_path_noauto(char *path_name
)
733 char *traces_path
= NULL
;
736 full_path
= utils_expand_path(opt_output_path
);
741 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
743 PERROR("asprintf trace dir name");
752 * create_output_path: create the output trace directory
755 char *create_output_path(char *path_name
)
757 if (opt_output_path
== NULL
) {
758 return create_output_path_auto(path_name
);
760 return create_output_path_noauto(path_name
);
765 * Get stream from stream id.
766 * Need to be called with RCU read-side lock held.
769 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
770 struct lttng_ht
*streams_ht
)
772 struct lttng_ht_node_ulong
*node
;
773 struct lttng_ht_iter iter
;
774 struct relay_stream
*ret
;
776 lttng_ht_lookup(streams_ht
,
777 (void *)((unsigned long) stream_id
),
779 node
= lttng_ht_iter_get_node_ulong(&iter
);
781 DBG("Relay stream %" PRIu64
" not found", stream_id
);
786 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
793 void deferred_free_stream(struct rcu_head
*head
)
795 struct relay_stream
*stream
=
796 caa_container_of(head
, struct relay_stream
, rcu_node
);
801 * relay_delete_session: Free all memory associated with a session and
805 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
807 struct lttng_ht_iter iter
;
808 struct lttng_ht_node_ulong
*node
;
809 struct relay_stream
*stream
;
816 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
819 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
820 node
= lttng_ht_iter_get_node_ulong(&iter
);
822 stream
= caa_container_of(node
,
823 struct relay_stream
, stream_n
);
824 if (stream
->session
== cmd
->session
) {
825 ret
= close(stream
->fd
);
827 PERROR("close stream fd on delete session");
829 ret
= lttng_ht_del(streams_ht
, &iter
);
831 call_rcu(&stream
->rcu_node
,
832 deferred_free_stream
);
842 * Handle the RELAYD_CREATE_SESSION command.
844 * On success, send back the session id or else return a negative value.
847 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
848 struct relay_command
*cmd
)
850 int ret
= 0, send_ret
;
851 struct relay_session
*session
;
852 struct lttcomm_relayd_status_session reply
;
857 memset(&reply
, 0, sizeof(reply
));
859 session
= zmalloc(sizeof(struct relay_session
));
860 if (session
== NULL
) {
861 PERROR("relay session zmalloc");
866 session
->id
= ++last_relay_session_id
;
867 session
->sock
= cmd
->sock
;
868 cmd
->session
= session
;
870 reply
.session_id
= htobe64(session
->id
);
872 DBG("Created session %" PRIu64
, session
->id
);
876 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
878 reply
.ret_code
= htobe32(LTTNG_OK
);
881 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
883 ERR("Relayd sending session id");
891 * relay_add_stream: allocate a new stream for a session
894 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
895 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
897 struct relay_session
*session
= cmd
->session
;
898 struct lttcomm_relayd_add_stream stream_info
;
899 struct relay_stream
*stream
= NULL
;
900 struct lttcomm_relayd_status_stream reply
;
901 char *path
= NULL
, *root_path
= NULL
;
904 if (!session
|| cmd
->version_check_done
== 0) {
905 ERR("Trying to add a stream before version check");
910 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
911 sizeof(struct lttcomm_relayd_add_stream
), 0);
912 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
914 /* Orderly shutdown. Not necessary to print an error. */
915 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
917 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
922 stream
= zmalloc(sizeof(struct relay_stream
));
923 if (stream
== NULL
) {
924 PERROR("relay stream zmalloc");
930 stream
->stream_handle
= ++last_relay_stream_id
;
931 stream
->prev_seq
= -1ULL;
932 stream
->session
= session
;
934 root_path
= create_output_path(stream_info
.pathname
);
939 ret
= utils_mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
941 ERR("relay creating output directory");
945 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
947 PERROR("asprintf stream path");
952 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
954 PERROR("Relay creating trace file");
959 DBG("Tracefile %s created", path
);
961 lttng_ht_node_init_ulong(&stream
->stream_n
,
962 (unsigned long) stream
->stream_handle
);
963 lttng_ht_add_unique_ulong(streams_ht
,
966 DBG("Relay new stream added %s", stream_info
.channel_name
);
972 reply
.handle
= htobe64(stream
->stream_handle
);
973 /* send the session id to the client or a negative return code on error */
975 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
976 /* stream was not properly added to the ht, so free it */
979 reply
.ret_code
= htobe32(LTTNG_OK
);
982 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
983 sizeof(struct lttcomm_relayd_status_stream
), 0);
985 ERR("Relay sending stream id");
995 * relay_close_stream: close a specific stream
998 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
999 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1001 struct relay_session
*session
= cmd
->session
;
1002 struct lttcomm_relayd_close_stream stream_info
;
1003 struct lttcomm_relayd_generic_reply reply
;
1004 struct relay_stream
*stream
;
1006 struct lttng_ht_iter iter
;
1008 DBG("Close stream received");
1010 if (!session
|| cmd
->version_check_done
== 0) {
1011 ERR("Trying to close a stream before version check");
1013 goto end_no_session
;
1016 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1017 sizeof(struct lttcomm_relayd_close_stream
), 0);
1018 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1020 /* Orderly shutdown. Not necessary to print an error. */
1021 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1023 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1026 goto end_no_session
;
1030 stream
= relay_stream_from_stream_id(be64toh(stream_info
.stream_id
),
1037 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1038 stream
->close_flag
= 1;
1040 if (close_stream_check(stream
)) {
1043 delret
= close(stream
->fd
);
1045 PERROR("close stream");
1047 iter
.iter
.node
= &stream
->stream_n
.node
;
1048 delret
= lttng_ht_del(streams_ht
, &iter
);
1050 call_rcu(&stream
->rcu_node
,
1051 deferred_free_stream
);
1052 DBG("Closed tracefile %d from close stream", stream
->fd
);
1059 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1061 reply
.ret_code
= htobe32(LTTNG_OK
);
1063 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1064 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1066 ERR("Relay sending stream id");
1075 * relay_unknown_command: send -1 if received unknown command
1078 void relay_unknown_command(struct relay_command
*cmd
)
1080 struct lttcomm_relayd_generic_reply reply
;
1083 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1084 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1085 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1087 ERR("Relay sending unknown command");
1092 * relay_start: send an acknowledgment to the client to tell if we are
1093 * ready to receive data. We are ready if a session is established.
1096 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1097 struct relay_command
*cmd
)
1099 int ret
= htobe32(LTTNG_OK
);
1100 struct lttcomm_relayd_generic_reply reply
;
1101 struct relay_session
*session
= cmd
->session
;
1104 DBG("Trying to start the streaming without a session established");
1105 ret
= htobe32(LTTNG_ERR_UNK
);
1108 reply
.ret_code
= ret
;
1109 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1110 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1112 ERR("Relay sending start ack");
1119 * Append padding to the file pointed by the file descriptor fd.
1121 static int write_padding_to_file(int fd
, uint32_t size
)
1130 zeros
= zmalloc(size
);
1131 if (zeros
== NULL
) {
1132 PERROR("zmalloc zeros for padding");
1138 ret
= write(fd
, zeros
, size
);
1139 } while (ret
< 0 && errno
== EINTR
);
1140 if (ret
< 0 || ret
!= size
) {
1141 PERROR("write padding to file");
1151 * relay_recv_metadata: receive the metada for the session.
1154 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1155 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1157 int ret
= htobe32(LTTNG_OK
);
1158 struct relay_session
*session
= cmd
->session
;
1159 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1160 struct relay_stream
*metadata_stream
;
1161 uint64_t data_size
, payload_size
;
1164 ERR("Metadata sent before version check");
1169 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1170 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1171 ERR("Incorrect data size");
1175 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1177 if (data_buffer_size
< data_size
) {
1178 /* In case the realloc fails, we can free the memory */
1181 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1182 if (!tmp_data_ptr
) {
1183 ERR("Allocating data buffer");
1188 data_buffer
= tmp_data_ptr
;
1189 data_buffer_size
= data_size
;
1191 memset(data_buffer
, 0, data_size
);
1192 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1193 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1194 if (ret
< 0 || ret
!= data_size
) {
1196 /* Orderly shutdown. Not necessary to print an error. */
1197 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1199 ERR("Relay didn't receive the whole metadata");
1204 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1207 metadata_stream
= relay_stream_from_stream_id(
1208 be64toh(metadata_struct
->stream_id
), streams_ht
);
1209 if (!metadata_stream
) {
1215 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1217 } while (ret
< 0 && errno
== EINTR
);
1218 if (ret
< 0 || ret
!= payload_size
) {
1219 ERR("Relay error writing metadata on file");
1224 ret
= write_padding_to_file(metadata_stream
->fd
,
1225 be32toh(metadata_struct
->padding_size
));
1230 DBG2("Relay metadata written");
1239 * relay_send_version: send relayd version number
1242 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1243 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1246 struct lttcomm_relayd_version reply
, msg
;
1250 cmd
->version_check_done
= 1;
1252 /* Get version from the other side. */
1253 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1254 if (ret
< 0 || ret
!= sizeof(msg
)) {
1256 /* Orderly shutdown. Not necessary to print an error. */
1257 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1259 ERR("Relay failed to receive the version values.");
1265 ret
= sscanf(VERSION
, "%10u.%10u", &reply
.major
, &reply
.minor
);
1267 ERR("Error in scanning version");
1272 /* Major versions must be the same */
1273 if (reply
.major
!= be32toh(msg
.major
)) {
1274 DBG("Incompatible major versions (%u vs %u), deleting session",
1275 reply
.major
, be32toh(msg
.major
));
1276 relay_delete_session(cmd
, streams_ht
);
1281 reply
.major
= htobe32(reply
.major
);
1282 reply
.minor
= htobe32(reply
.minor
);
1283 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1284 sizeof(struct lttcomm_relayd_version
), 0);
1286 ERR("Relay sending version");
1290 cmd
->session
->major
= reply
.major
;
1291 /* We adapt to the lowest compatible version */
1292 if (reply
.minor
<= be32toh(msg
.minor
)) {
1293 cmd
->session
->minor
= reply
.minor
;
1295 cmd
->session
->minor
= be32toh(msg
.minor
);
1298 DBG("Version check done using protocol %u.%u", cmd
->session
->major
,
1299 cmd
->session
->minor
);
1307 * Check for data pending for a given stream id from the session daemon.
1310 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1311 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1313 struct relay_session
*session
= cmd
->session
;
1314 struct lttcomm_relayd_data_pending msg
;
1315 struct lttcomm_relayd_generic_reply reply
;
1316 struct relay_stream
*stream
;
1318 uint64_t last_net_seq_num
, stream_id
;
1320 DBG("Data pending command received");
1322 if (!session
|| cmd
->version_check_done
== 0) {
1323 ERR("Trying to check for data before version check");
1325 goto end_no_session
;
1328 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1329 if (ret
< sizeof(msg
)) {
1331 /* Orderly shutdown. Not necessary to print an error. */
1332 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1334 ERR("Relay didn't receive valid data_pending struct size : %d",
1338 goto end_no_session
;
1341 stream_id
= be64toh(msg
.stream_id
);
1342 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1345 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1346 if (stream
== NULL
) {
1351 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1352 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1355 /* Avoid wrapping issue */
1356 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1357 /* Data has in fact been written and is NOT pending */
1360 /* Data still being streamed thus pending */
1364 /* Pending check is now done. */
1365 stream
->data_pending_check_done
= 1;
1370 reply
.ret_code
= htobe32(ret
);
1371 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1373 ERR("Relay data pending ret code failed");
1381 * Wait for the control socket to reach a quiescent state.
1383 * Note that for now, when receiving this command from the session daemon, this
1384 * means that every subsequent commands or data received on the control socket
1385 * has been handled. So, this is why we simply return OK here.
1388 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1389 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1393 struct relay_stream
*stream
;
1394 struct lttng_ht_iter iter
;
1395 struct lttcomm_relayd_quiescent_control msg
;
1396 struct lttcomm_relayd_generic_reply reply
;
1398 DBG("Checking quiescent state on control socket");
1400 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1401 ERR("Trying to check for data before version check");
1403 goto end_no_session
;
1406 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1407 if (ret
< sizeof(msg
)) {
1409 /* Orderly shutdown. Not necessary to print an error. */
1410 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1412 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1416 goto end_no_session
;
1419 stream_id
= be64toh(msg
.stream_id
);
1422 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1423 if (stream
->stream_handle
== stream_id
) {
1424 stream
->data_pending_check_done
= 1;
1425 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1432 reply
.ret_code
= htobe32(LTTNG_OK
);
1433 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1435 ERR("Relay data quiescent control ret code failed");
1443 * Initialize a data pending command. This means that a client is about to ask
1444 * for data pending for each stream he/she holds. Simply iterate over all
1445 * streams of a session and set the data_pending_check_done flag.
1447 * This command returns to the client a LTTNG_OK code.
1450 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1451 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1454 struct lttng_ht_iter iter
;
1455 struct lttcomm_relayd_begin_data_pending msg
;
1456 struct lttcomm_relayd_generic_reply reply
;
1457 struct relay_stream
*stream
;
1458 uint64_t session_id
;
1464 DBG("Init streams for data pending");
1466 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1467 ERR("Trying to check for data before version check");
1469 goto end_no_session
;
1472 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1473 if (ret
< sizeof(msg
)) {
1475 /* Orderly shutdown. Not necessary to print an error. */
1476 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1478 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1482 goto end_no_session
;
1485 session_id
= be64toh(msg
.session_id
);
1488 * Iterate over all streams to set the begin data pending flag. For now, the
1489 * streams are indexed by stream handle so we have to iterate over all
1490 * streams to find the one associated with the right session_id.
1493 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1494 if (stream
->session
->id
== session_id
) {
1495 stream
->data_pending_check_done
= 0;
1496 DBG("Set begin data pending flag to stream %" PRIu64
,
1497 stream
->stream_handle
);
1502 /* All good, send back reply. */
1503 reply
.ret_code
= htobe32(LTTNG_OK
);
1505 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1507 ERR("Relay begin data pending send reply failed");
1515 * End data pending command. This will check, for a given session id, if each
1516 * stream associated with it has its data_pending_check_done flag set. If not,
1517 * this means that the client lost track of the stream but the data is still
1518 * being streamed on our side. In this case, we inform the client that data is
1521 * Return to the client if there is data in flight or not with a ret_code.
1524 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1525 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1528 struct lttng_ht_iter iter
;
1529 struct lttcomm_relayd_end_data_pending msg
;
1530 struct lttcomm_relayd_generic_reply reply
;
1531 struct relay_stream
*stream
;
1532 uint64_t session_id
;
1533 uint32_t is_data_inflight
= 0;
1539 DBG("End data pending command");
1541 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1542 ERR("Trying to check for data before version check");
1544 goto end_no_session
;
1547 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1548 if (ret
< sizeof(msg
)) {
1550 /* Orderly shutdown. Not necessary to print an error. */
1551 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1553 ERR("Relay didn't receive valid end data_pending struct size: %d",
1557 goto end_no_session
;
1560 session_id
= be64toh(msg
.session_id
);
1562 /* Iterate over all streams to see if the begin data pending flag is set. */
1564 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1565 if (stream
->session
->id
== session_id
&&
1566 !stream
->data_pending_check_done
) {
1567 is_data_inflight
= 1;
1568 DBG("Data is still in flight for stream %" PRIu64
,
1569 stream
->stream_handle
);
1575 /* All good, send back reply. */
1576 reply
.ret_code
= htobe32(is_data_inflight
);
1578 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1580 ERR("Relay end data pending send reply failed");
1588 * relay_process_control: Process the commands received on the control socket
1591 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1592 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1596 switch (be32toh(recv_hdr
->cmd
)) {
1597 case RELAYD_CREATE_SESSION
:
1598 ret
= relay_create_session(recv_hdr
, cmd
);
1600 case RELAYD_ADD_STREAM
:
1601 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1603 case RELAYD_START_DATA
:
1604 ret
= relay_start(recv_hdr
, cmd
);
1606 case RELAYD_SEND_METADATA
:
1607 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1609 case RELAYD_VERSION
:
1610 ret
= relay_send_version(recv_hdr
, cmd
, streams_ht
);
1612 case RELAYD_CLOSE_STREAM
:
1613 ret
= relay_close_stream(recv_hdr
, cmd
, streams_ht
);
1615 case RELAYD_DATA_PENDING
:
1616 ret
= relay_data_pending(recv_hdr
, cmd
, streams_ht
);
1618 case RELAYD_QUIESCENT_CONTROL
:
1619 ret
= relay_quiescent_control(recv_hdr
, cmd
, streams_ht
);
1621 case RELAYD_BEGIN_DATA_PENDING
:
1622 ret
= relay_begin_data_pending(recv_hdr
, cmd
, streams_ht
);
1624 case RELAYD_END_DATA_PENDING
:
1625 ret
= relay_end_data_pending(recv_hdr
, cmd
, streams_ht
);
1627 case RELAYD_UPDATE_SYNC_INFO
:
1629 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1630 relay_unknown_command(cmd
);
1640 * relay_process_data: Process the data received on the data socket
1643 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1646 struct relay_stream
*stream
;
1647 struct lttcomm_relayd_data_hdr data_hdr
;
1649 uint64_t net_seq_num
;
1652 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1653 sizeof(struct lttcomm_relayd_data_hdr
), 0);
1656 /* Orderly shutdown. Not necessary to print an error. */
1657 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1659 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
1665 stream_id
= be64toh(data_hdr
.stream_id
);
1668 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1674 data_size
= be32toh(data_hdr
.data_size
);
1675 if (data_buffer_size
< data_size
) {
1678 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1679 if (!tmp_data_ptr
) {
1680 ERR("Allocating data buffer");
1685 data_buffer
= tmp_data_ptr
;
1686 data_buffer_size
= data_size
;
1688 memset(data_buffer
, 0, data_size
);
1690 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1692 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1693 data_size
, stream_id
, net_seq_num
);
1694 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1697 /* Orderly shutdown. Not necessary to print an error. */
1698 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1705 ret
= write(stream
->fd
, data_buffer
, data_size
);
1706 } while (ret
< 0 && errno
== EINTR
);
1707 if (ret
< 0 || ret
!= data_size
) {
1708 ERR("Relay error writing data to file");
1713 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
1714 ret
, stream
->stream_handle
);
1716 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
1721 stream
->prev_seq
= net_seq_num
;
1723 /* Check if we need to close the FD */
1724 if (close_stream_check(stream
)) {
1726 struct lttng_ht_iter iter
;
1728 cret
= close(stream
->fd
);
1730 PERROR("close stream process data");
1732 iter
.iter
.node
= &stream
->stream_n
.node
;
1733 ret
= lttng_ht_del(streams_ht
, &iter
);
1735 call_rcu(&stream
->rcu_node
,
1736 deferred_free_stream
);
1737 DBG("Closed tracefile %d after recv data", stream
->fd
);
1747 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1751 lttng_poll_del(events
, pollfd
);
1753 ret
= close(pollfd
);
1755 ERR("Closing pollfd %d", pollfd
);
1760 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1761 struct lttng_ht
*relay_connections_ht
)
1763 struct relay_command
*relay_connection
;
1766 relay_connection
= zmalloc(sizeof(struct relay_command
));
1767 if (relay_connection
== NULL
) {
1768 PERROR("Relay command zmalloc");
1772 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1773 } while (ret
< 0 && errno
== EINTR
);
1774 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
1775 PERROR("read relay cmd pipe");
1779 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1780 (unsigned long) relay_connection
->sock
->fd
);
1782 lttng_ht_add_unique_ulong(relay_connections_ht
,
1783 &relay_connection
->sock_n
);
1785 return lttng_poll_add(events
,
1786 relay_connection
->sock
->fd
,
1787 LPOLLIN
| LPOLLRDHUP
);
1790 free(relay_connection
);
1796 void deferred_free_connection(struct rcu_head
*head
)
1798 struct relay_command
*relay_connection
=
1799 caa_container_of(head
, struct relay_command
, rcu_node
);
1801 lttcomm_destroy_sock(relay_connection
->sock
);
1802 free(relay_connection
);
1806 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
1807 struct lttng_ht
*streams_ht
, struct lttng_ht_iter
*iter
,
1808 struct relay_command
*relay_connection
)
1812 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1814 if (relay_connection
->type
== RELAY_CONTROL
) {
1815 relay_delete_session(relay_connection
, streams_ht
);
1818 call_rcu(&relay_connection
->rcu_node
,
1819 deferred_free_connection
);
1823 * This thread does the actual work
1826 void *relay_thread_worker(void *data
)
1828 int ret
, err
= -1, last_seen_data_fd
= -1;
1830 struct relay_command
*relay_connection
;
1831 struct lttng_poll_event events
;
1832 struct lttng_ht
*relay_connections_ht
;
1833 struct lttng_ht_node_ulong
*node
;
1834 struct lttng_ht_iter iter
;
1835 struct lttng_ht
*streams_ht
;
1836 struct lttcomm_relayd_hdr recv_hdr
;
1838 DBG("[thread] Relay worker started");
1840 rcu_register_thread();
1842 /* table of connections indexed on socket */
1843 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1844 if (!relay_connections_ht
) {
1845 goto relay_connections_ht_error
;
1848 /* tables of streams indexed by stream ID */
1849 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1851 goto streams_ht_error
;
1854 ret
= create_thread_poll_set(&events
, 2);
1856 goto error_poll_create
;
1859 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1866 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
1868 /* Infinite blocking call, waiting for transmission */
1869 DBG3("Relayd worker thread polling...");
1870 ret
= lttng_poll_wait(&events
, -1);
1873 * Restart interrupted system call.
1875 if (errno
== EINTR
) {
1884 * Process control. The control connection is prioritised so we don't
1885 * starve it with high throughout put tracing data on the data
1888 for (i
= 0; i
< nb_fd
; i
++) {
1889 /* Fetch once the poll data */
1890 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1891 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1893 /* Thread quit pipe has been closed. Killing thread. */
1894 ret
= check_thread_quit_pipe(pollfd
, revents
);
1900 /* Inspect the relay cmd pipe for new connection */
1901 if (pollfd
== relay_cmd_pipe
[0]) {
1902 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1903 ERR("Relay pipe error");
1905 } else if (revents
& LPOLLIN
) {
1906 DBG("Relay command received");
1907 ret
= relay_add_connection(relay_cmd_pipe
[0],
1908 &events
, relay_connections_ht
);
1913 } else if (revents
) {
1915 lttng_ht_lookup(relay_connections_ht
,
1916 (void *)((unsigned long) pollfd
),
1918 node
= lttng_ht_iter_get_node_ulong(&iter
);
1920 DBG2("Relay sock %d not found", pollfd
);
1924 relay_connection
= caa_container_of(node
,
1925 struct relay_command
, sock_n
);
1927 if (revents
& (LPOLLERR
)) {
1929 relay_cleanup_poll_connection(&events
, pollfd
);
1930 relay_del_connection(relay_connections_ht
,
1933 if (last_seen_data_fd
== pollfd
) {
1934 last_seen_data_fd
= last_notdel_data_fd
;
1936 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1937 DBG("Socket %d hung up", pollfd
);
1938 relay_cleanup_poll_connection(&events
, pollfd
);
1939 relay_del_connection(relay_connections_ht
,
1942 if (last_seen_data_fd
== pollfd
) {
1943 last_seen_data_fd
= last_notdel_data_fd
;
1945 } else if (revents
& LPOLLIN
) {
1946 /* control socket */
1947 if (relay_connection
->type
== RELAY_CONTROL
) {
1948 ret
= relay_connection
->sock
->ops
->recvmsg(
1949 relay_connection
->sock
, &recv_hdr
,
1950 sizeof(struct lttcomm_relayd_hdr
), 0);
1951 /* connection closed */
1953 relay_cleanup_poll_connection(&events
, pollfd
);
1954 relay_del_connection(relay_connections_ht
,
1957 DBG("Control connection closed with %d", pollfd
);
1959 if (relay_connection
->session
) {
1960 DBG2("Relay worker receiving data for session : %" PRIu64
,
1961 relay_connection
->session
->id
);
1963 ret
= relay_process_control(&recv_hdr
,
1967 /* Clear the session on error. */
1968 relay_cleanup_poll_connection(&events
, pollfd
);
1969 relay_del_connection(relay_connections_ht
,
1972 DBG("Connection closed with %d", pollfd
);
1978 * Flag the last seen data fd not deleted. It will be
1979 * used as the last seen fd if any fd gets deleted in
1982 last_notdel_data_fd
= pollfd
;
1990 * The last loop handled a control request, go back to poll to make
1991 * sure we prioritise the control socket.
1997 if (last_seen_data_fd
>= 0) {
1998 for (i
= 0; i
< nb_fd
; i
++) {
1999 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2000 if (last_seen_data_fd
== pollfd
) {
2007 /* Process data connection. */
2008 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2009 /* Fetch the poll data. */
2010 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2011 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2013 /* Skip the command pipe. It's handled in the first loop. */
2014 if (pollfd
== relay_cmd_pipe
[0]) {
2020 lttng_ht_lookup(relay_connections_ht
,
2021 (void *)((unsigned long) pollfd
),
2023 node
= lttng_ht_iter_get_node_ulong(&iter
);
2025 /* Skip it. Might be removed before. */
2029 relay_connection
= caa_container_of(node
,
2030 struct relay_command
, sock_n
);
2032 if (revents
& LPOLLIN
) {
2033 if (relay_connection
->type
!= RELAY_DATA
) {
2037 ret
= relay_process_data(relay_connection
, streams_ht
);
2038 /* connection closed */
2040 relay_cleanup_poll_connection(&events
, pollfd
);
2041 relay_del_connection(relay_connections_ht
,
2044 DBG("Data connection closed with %d", pollfd
);
2046 * Every goto restart call sets the last seen fd where
2047 * here we don't really care since we gracefully
2048 * continue the loop after the connection is deleted.
2051 /* Keep last seen port. */
2052 last_seen_data_fd
= pollfd
;
2060 last_seen_data_fd
= -1;
2065 lttng_poll_clean(&events
);
2067 /* empty the hash table and free the memory */
2069 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2070 node
= lttng_ht_iter_get_node_ulong(&iter
);
2072 relay_connection
= caa_container_of(node
,
2073 struct relay_command
, sock_n
);
2074 relay_del_connection(relay_connections_ht
,
2081 lttng_ht_destroy(streams_ht
);
2083 lttng_ht_destroy(relay_connections_ht
);
2084 relay_connections_ht_error
:
2085 /* Close relay cmd pipes */
2086 utils_close_pipe(relay_cmd_pipe
);
2088 DBG("Thread exited with error");
2090 DBG("Worker thread cleanup complete");
2093 rcu_unregister_thread();
2098 * Create the relay command pipe to wake thread_manage_apps.
2099 * Closed in cleanup().
2101 static int create_relay_cmd_pipe(void)
2105 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2113 int main(int argc
, char **argv
)
2118 /* Create thread quit pipe */
2119 if ((ret
= init_thread_quit_pipe()) < 0) {
2123 /* Parse arguments */
2125 if ((ret
= parse_args(argc
, argv
)) < 0) {
2129 if ((ret
= set_signal_handler()) < 0) {
2133 /* Try to create directory if -o, --output is specified. */
2134 if (opt_output_path
) {
2135 ret
= utils_mkdir_recursive(opt_output_path
, S_IRWXU
| S_IRWXG
);
2137 ERR("Unable to create %s", opt_output_path
);
2151 /* Check if daemon is UID = 0 */
2152 is_root
= !getuid();
2155 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
2156 ERR("Need to be root to use ports < 1024");
2162 /* Setup the thread apps communication pipe. */
2163 if ((ret
= create_relay_cmd_pipe()) < 0) {
2167 /* Init relay command queue. */
2168 cds_wfq_init(&relay_cmd_queue
.queue
);
2170 /* Set up max poll set size */
2171 lttng_poll_set_max_size();
2173 /* Setup the dispatcher thread */
2174 ret
= pthread_create(&dispatcher_thread
, NULL
,
2175 relay_thread_dispatcher
, (void *) NULL
);
2177 PERROR("pthread_create dispatcher");
2178 goto exit_dispatcher
;
2181 /* Setup the worker thread */
2182 ret
= pthread_create(&worker_thread
, NULL
,
2183 relay_thread_worker
, (void *) NULL
);
2185 PERROR("pthread_create worker");
2189 /* Setup the listener thread */
2190 ret
= pthread_create(&listener_thread
, NULL
,
2191 relay_thread_listener
, (void *) NULL
);
2193 PERROR("pthread_create listener");
2198 ret
= pthread_join(listener_thread
, &status
);
2200 PERROR("pthread_join");
2201 goto error
; /* join error, exit without cleanup */
2205 ret
= pthread_join(worker_thread
, &status
);
2207 PERROR("pthread_join");
2208 goto error
; /* join error, exit without cleanup */
2212 ret
= pthread_join(dispatcher_thread
, &status
);
2214 PERROR("pthread_join");
2215 goto error
; /* join error, exit without cleanup */