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 * Return the realpath(3) of the path even if the last directory token does not
690 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
691 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
692 * fails if the end point directory does not exist.
695 char *expand_full_path(const char *path
)
697 const char *end_path
= path
;
698 char *next
, *cut_path
, *expanded_path
, *respath
;
700 /* Find last token delimited by '/' */
701 while ((next
= strpbrk(end_path
+ 1, "/"))) {
705 /* Cut last token from original path */
706 cut_path
= strndup(path
, end_path
- path
);
708 expanded_path
= malloc(PATH_MAX
);
709 if (expanded_path
== NULL
) {
714 respath
= realpath(cut_path
, expanded_path
);
715 if (respath
== NULL
) {
718 ERR("%s: No such file or directory", cut_path
);
726 /* Add end part to expanded path */
727 strcat(respath
, end_path
);
736 * config_get_default_path
738 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
741 char *config_get_default_path(void)
743 return getenv("HOME");
747 * Create recursively directory using the FULL path.
750 int mkdir_recursive(char *path
, mode_t mode
)
752 char *p
, tmp
[PATH_MAX
];
757 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
759 PERROR("snprintf mkdir");
764 if (tmp
[len
- 1] == '/') {
768 for (p
= tmp
+ 1; *p
; p
++) {
771 if (tmp
[strlen(tmp
) - 1] == '.' &&
772 tmp
[strlen(tmp
) - 2] == '.' &&
773 tmp
[strlen(tmp
) - 3] == '/') {
774 ERR("Using '/../' is not permitted in the trace path (%s)",
779 ret
= stat(tmp
, &statbuf
);
781 ret
= mkdir(tmp
, mode
);
783 if (errno
!= EEXIST
) {
784 PERROR("mkdir recursive");
794 ret
= mkdir(tmp
, mode
);
796 if (errno
!= EEXIST
) {
797 PERROR("mkdir recursive last piece");
809 char *create_output_path_auto(char *path_name
)
812 char *traces_path
= NULL
;
813 char *alloc_path
= NULL
;
816 default_path
= config_get_default_path();
817 if (default_path
== NULL
) {
818 ERR("Home path not found.\n \
819 Please specify an output path using -o, --output PATH");
822 alloc_path
= strdup(default_path
);
823 if (alloc_path
== NULL
) {
824 PERROR("Path allocation");
827 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
828 "/%s", alloc_path
, path_name
);
830 PERROR("asprintf trace dir name");
839 char *create_output_path_noauto(char *path_name
)
842 char *traces_path
= NULL
;
845 full_path
= expand_full_path(opt_output_path
);
846 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
848 PERROR("asprintf trace dir name");
857 * create_output_path: create the output trace directory
860 char *create_output_path(char *path_name
)
862 if (opt_output_path
== NULL
) {
863 return create_output_path_auto(path_name
);
865 return create_output_path_noauto(path_name
);
870 void deferred_free_stream(struct rcu_head
*head
)
872 struct relay_stream
*stream
=
873 caa_container_of(head
, struct relay_stream
, rcu_node
);
878 * relay_delete_session: Free all memory associated with a session and
882 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
884 struct lttng_ht_iter iter
;
885 struct lttng_ht_node_ulong
*node
;
886 struct relay_stream
*stream
;
893 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
896 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
897 node
= lttng_ht_iter_get_node_ulong(&iter
);
899 stream
= caa_container_of(node
,
900 struct relay_stream
, stream_n
);
901 if (stream
->session
== cmd
->session
) {
902 ret
= close(stream
->fd
);
904 PERROR("close stream fd on delete session");
906 ret
= lttng_ht_del(streams_ht
, &iter
);
908 call_rcu(&stream
->rcu_node
,
909 deferred_free_stream
);
919 * Handle the RELAYD_CREATE_SESSION command.
921 * On success, send back the session id or else return a negative value.
924 int relay_create_session(struct lttcomm_relayd_hdr
*recv_hdr
,
925 struct relay_command
*cmd
)
927 int ret
= 0, send_ret
;
928 struct relay_session
*session
;
929 struct lttcomm_relayd_status_session reply
;
934 memset(&reply
, 0, sizeof(reply
));
936 session
= zmalloc(sizeof(struct relay_session
));
937 if (session
== NULL
) {
938 PERROR("relay session zmalloc");
943 session
->id
= ++last_relay_session_id
;
944 session
->sock
= cmd
->sock
;
945 cmd
->session
= session
;
947 reply
.session_id
= htobe64(session
->id
);
949 DBG("Created session %" PRIu64
, session
->id
);
953 reply
.ret_code
= htobe32(LTTNG_ERR_FATAL
);
955 reply
.ret_code
= htobe32(LTTNG_OK
);
958 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
960 ERR("Relayd sending session id");
968 * relay_add_stream: allocate a new stream for a session
971 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
972 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
974 struct relay_session
*session
= cmd
->session
;
975 struct lttcomm_relayd_add_stream stream_info
;
976 struct relay_stream
*stream
= NULL
;
977 struct lttcomm_relayd_status_stream reply
;
978 char *path
= NULL
, *root_path
= NULL
;
981 if (!session
|| cmd
->version_check_done
== 0) {
982 ERR("Trying to add a stream before version check");
987 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
988 sizeof(struct lttcomm_relayd_add_stream
), 0);
989 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
991 /* Orderly shutdown. Not necessary to print an error. */
992 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
994 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
999 stream
= zmalloc(sizeof(struct relay_stream
));
1000 if (stream
== NULL
) {
1001 PERROR("relay stream zmalloc");
1003 goto end_no_session
;
1007 stream
->stream_handle
= ++last_relay_stream_id
;
1008 stream
->prev_seq
= -1ULL;
1009 stream
->session
= session
;
1011 root_path
= create_output_path(stream_info
.pathname
);
1016 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
1018 ERR("relay creating output directory");
1022 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
1024 PERROR("asprintf stream path");
1028 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1030 PERROR("Relay creating trace file");
1035 DBG("Tracefile %s created", path
);
1037 lttng_ht_node_init_ulong(&stream
->stream_n
,
1038 (unsigned long) stream
->stream_handle
);
1039 lttng_ht_add_unique_ulong(streams_ht
,
1042 DBG("Relay new stream added %s", stream_info
.channel_name
);
1047 /* send the session id to the client or a negative return code on error */
1049 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1051 reply
.ret_code
= htobe32(LTTNG_OK
);
1053 reply
.handle
= htobe64(stream
->stream_handle
);
1054 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1055 sizeof(struct lttcomm_relayd_status_stream
), 0);
1057 ERR("Relay sending stream id");
1067 * relay_close_stream: close a specific stream
1070 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1071 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1073 struct relay_session
*session
= cmd
->session
;
1074 struct lttcomm_relayd_close_stream stream_info
;
1075 struct lttcomm_relayd_generic_reply reply
;
1076 struct relay_stream
*stream
;
1078 struct lttng_ht_node_ulong
*node
;
1079 struct lttng_ht_iter iter
;
1081 DBG("Close stream received");
1083 if (!session
|| cmd
->version_check_done
== 0) {
1084 ERR("Trying to close a stream before version check");
1086 goto end_no_session
;
1089 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1090 sizeof(struct lttcomm_relayd_close_stream
), 0);
1091 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1093 /* Orderly shutdown. Not necessary to print an error. */
1094 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1096 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1099 goto end_no_session
;
1103 lttng_ht_lookup(streams_ht
,
1104 (void *)((unsigned long) be64toh(stream_info
.stream_id
)),
1106 node
= lttng_ht_iter_get_node_ulong(&iter
);
1108 DBG("Relay stream %" PRIu64
" not found", be64toh(stream_info
.stream_id
));
1113 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1119 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1120 stream
->close_flag
= 1;
1122 if (close_stream_check(stream
)) {
1125 delret
= close(stream
->fd
);
1127 PERROR("close stream");
1129 delret
= lttng_ht_del(streams_ht
, &iter
);
1131 call_rcu(&stream
->rcu_node
,
1132 deferred_free_stream
);
1133 DBG("Closed tracefile %d from close stream", stream
->fd
);
1140 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1142 reply
.ret_code
= htobe32(LTTNG_OK
);
1144 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1145 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1147 ERR("Relay sending stream id");
1156 * relay_unknown_command: send -1 if received unknown command
1159 void relay_unknown_command(struct relay_command
*cmd
)
1161 struct lttcomm_relayd_generic_reply reply
;
1164 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1165 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1166 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1168 ERR("Relay sending unknown command");
1173 * relay_start: send an acknowledgment to the client to tell if we are
1174 * ready to receive data. We are ready if a session is established.
1177 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1178 struct relay_command
*cmd
)
1180 int ret
= htobe32(LTTNG_OK
);
1181 struct lttcomm_relayd_generic_reply reply
;
1182 struct relay_session
*session
= cmd
->session
;
1185 DBG("Trying to start the streaming without a session established");
1186 ret
= htobe32(LTTNG_ERR_UNK
);
1189 reply
.ret_code
= ret
;
1190 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1191 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1193 ERR("Relay sending start ack");
1200 * Get stream from stream id.
1201 * Need to be called with RCU read-side lock held.
1204 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
1205 struct lttng_ht
*streams_ht
)
1207 struct lttng_ht_node_ulong
*node
;
1208 struct lttng_ht_iter iter
;
1209 struct relay_stream
*ret
;
1211 lttng_ht_lookup(streams_ht
,
1212 (void *)((unsigned long) stream_id
),
1214 node
= lttng_ht_iter_get_node_ulong(&iter
);
1216 DBG("Relay stream %" PRIu64
" not found", stream_id
);
1221 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1228 * Append padding to the file pointed by the file descriptor fd.
1230 static int write_padding_to_file(int fd
, uint32_t size
)
1239 zeros
= zmalloc(size
);
1240 if (zeros
== NULL
) {
1241 PERROR("zmalloc zeros for padding");
1247 ret
= write(fd
, zeros
, size
);
1248 } while (ret
< 0 && errno
== EINTR
);
1249 if (ret
< 0 || ret
!= size
) {
1250 PERROR("write padding to file");
1260 * relay_recv_metadata: receive the metada for the session.
1263 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1264 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1266 int ret
= htobe32(LTTNG_OK
);
1267 struct relay_session
*session
= cmd
->session
;
1268 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1269 struct relay_stream
*metadata_stream
;
1270 uint64_t data_size
, payload_size
;
1273 ERR("Metadata sent before version check");
1278 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1279 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1280 ERR("Incorrect data size");
1284 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1286 if (data_buffer_size
< data_size
) {
1287 /* In case the realloc fails, we can free the memory */
1290 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1291 if (!tmp_data_ptr
) {
1292 ERR("Allocating data buffer");
1297 data_buffer
= tmp_data_ptr
;
1298 data_buffer_size
= data_size
;
1300 memset(data_buffer
, 0, data_size
);
1301 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1302 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1303 if (ret
< 0 || ret
!= data_size
) {
1305 /* Orderly shutdown. Not necessary to print an error. */
1306 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1308 ERR("Relay didn't receive the whole metadata");
1313 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1316 metadata_stream
= relay_stream_from_stream_id(
1317 be64toh(metadata_struct
->stream_id
), streams_ht
);
1318 if (!metadata_stream
) {
1324 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1326 } while (ret
< 0 && errno
== EINTR
);
1327 if (ret
< 0 || ret
!= payload_size
) {
1328 ERR("Relay error writing metadata on file");
1333 ret
= write_padding_to_file(metadata_stream
->fd
,
1334 be32toh(metadata_struct
->padding_size
));
1339 DBG2("Relay metadata written");
1348 * relay_send_version: send relayd version number
1351 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1352 struct relay_command
*cmd
)
1355 struct lttcomm_relayd_version reply
, msg
;
1359 cmd
->version_check_done
= 1;
1361 /* Get version from the other side. */
1362 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1363 if (ret
< 0 || ret
!= sizeof(msg
)) {
1365 /* Orderly shutdown. Not necessary to print an error. */
1366 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1368 ERR("Relay failed to receive the version values.");
1375 * For now, we just ignore the received version but after 2.1 stable
1376 * release, a check must be done to see if we either adapt to the other
1377 * side version (which MUST be lower than us) or keep the latest data
1378 * structure considering that the other side will adapt.
1381 ret
= sscanf(VERSION
, "%10u.%10u", &reply
.major
, &reply
.minor
);
1383 ERR("Error in scanning version");
1387 reply
.major
= htobe32(reply
.major
);
1388 reply
.minor
= htobe32(reply
.minor
);
1389 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1390 sizeof(struct lttcomm_relayd_version
), 0);
1392 ERR("Relay sending version");
1394 DBG("Version check done (%u.%u)", be32toh(reply
.major
),
1395 be32toh(reply
.minor
));
1402 * Check for data pending for a given stream id from the session daemon.
1405 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1406 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1408 struct relay_session
*session
= cmd
->session
;
1409 struct lttcomm_relayd_data_pending msg
;
1410 struct lttcomm_relayd_generic_reply reply
;
1411 struct relay_stream
*stream
;
1413 struct lttng_ht_node_ulong
*node
;
1414 struct lttng_ht_iter iter
;
1415 uint64_t last_net_seq_num
, stream_id
;
1417 DBG("Data pending command received");
1419 if (!session
|| cmd
->version_check_done
== 0) {
1420 ERR("Trying to check for data before version check");
1422 goto end_no_session
;
1425 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1426 if (ret
< sizeof(msg
)) {
1428 /* Orderly shutdown. Not necessary to print an error. */
1429 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1431 ERR("Relay didn't receive valid data_pending struct size : %d",
1435 goto end_no_session
;
1438 stream_id
= be64toh(msg
.stream_id
);
1439 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1442 lttng_ht_lookup(streams_ht
, (void *)((unsigned long) stream_id
), &iter
);
1443 node
= lttng_ht_iter_get_node_ulong(&iter
);
1445 DBG("Relay stream %" PRIu64
" not found", stream_id
);
1450 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1453 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1454 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1457 /* Avoid wrapping issue */
1458 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) >= 0) {
1459 /* Data has in fact been written and is NOT pending */
1462 /* Data still being streamed thus pending */
1466 /* Pending check is now done. */
1467 stream
->data_pending_check_done
= 1;
1472 reply
.ret_code
= htobe32(ret
);
1473 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1475 ERR("Relay data pending ret code failed");
1483 * Wait for the control socket to reach a quiescent state.
1485 * Note that for now, when receiving this command from the session daemon, this
1486 * means that every subsequent commands or data received on the control socket
1487 * has been handled. So, this is why we simply return OK here.
1490 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1491 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1495 struct relay_stream
*stream
;
1496 struct lttng_ht_iter iter
;
1497 struct lttcomm_relayd_quiescent_control msg
;
1498 struct lttcomm_relayd_generic_reply reply
;
1500 DBG("Checking quiescent state on control socket");
1502 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1503 ERR("Trying to check for data before version check");
1505 goto end_no_session
;
1508 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1509 if (ret
< sizeof(msg
)) {
1511 /* Orderly shutdown. Not necessary to print an error. */
1512 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1514 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1518 goto end_no_session
;
1521 stream_id
= be64toh(msg
.stream_id
);
1524 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1525 if (stream
->stream_handle
== stream_id
) {
1526 stream
->data_pending_check_done
= 1;
1527 DBG("Relay quiescent control pending flag set to %" PRIu64
,
1534 reply
.ret_code
= htobe32(LTTNG_OK
);
1535 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1537 ERR("Relay data quiescent control ret code failed");
1545 * Initialize a data pending command. This means that a client is about to ask
1546 * for data pending for each stream he/she holds. Simply iterate over all
1547 * streams of a session and set the data_pending_check_done flag.
1549 * This command returns to the client a LTTNG_OK code.
1552 int relay_begin_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1553 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1556 struct lttng_ht_iter iter
;
1557 struct lttcomm_relayd_begin_data_pending msg
;
1558 struct lttcomm_relayd_generic_reply reply
;
1559 struct relay_stream
*stream
;
1560 uint64_t session_id
;
1566 DBG("Init streams for data pending");
1568 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1569 ERR("Trying to check for data before version check");
1571 goto end_no_session
;
1574 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1575 if (ret
< sizeof(msg
)) {
1577 /* Orderly shutdown. Not necessary to print an error. */
1578 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1580 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1584 goto end_no_session
;
1587 session_id
= be64toh(msg
.session_id
);
1590 * Iterate over all streams to set the begin data pending flag. For now, the
1591 * streams are indexed by stream handle so we have to iterate over all
1592 * streams to find the one associated with the right session_id.
1595 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1596 if (stream
->session
->id
== session_id
) {
1597 stream
->data_pending_check_done
= 0;
1598 DBG("Set begin data pending flag to stream %" PRIu64
,
1599 stream
->stream_handle
);
1604 /* All good, send back reply. */
1605 reply
.ret_code
= htobe32(LTTNG_OK
);
1607 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1609 ERR("Relay begin data pending send reply failed");
1617 * End data pending command. This will check, for a given session id, if each
1618 * stream associated with it has its data_pending_check_done flag set. If not,
1619 * this means that the client lost track of the stream but the data is still
1620 * being streamed on our side. In this case, we inform the client that data is
1623 * Return to the client if there is data in flight or not with a ret_code.
1626 int relay_end_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1627 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1630 struct lttng_ht_iter iter
;
1631 struct lttcomm_relayd_end_data_pending msg
;
1632 struct lttcomm_relayd_generic_reply reply
;
1633 struct relay_stream
*stream
;
1634 uint64_t session_id
;
1635 uint32_t is_data_inflight
= 0;
1641 DBG("End data pending command");
1643 if (!cmd
->session
|| cmd
->version_check_done
== 0) {
1644 ERR("Trying to check for data before version check");
1646 goto end_no_session
;
1649 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1650 if (ret
< sizeof(msg
)) {
1652 /* Orderly shutdown. Not necessary to print an error. */
1653 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1655 ERR("Relay didn't receive valid end data_pending struct size: %d",
1659 goto end_no_session
;
1662 session_id
= be64toh(msg
.session_id
);
1664 /* Iterate over all streams to see if the begin data pending flag is set. */
1666 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, stream
, stream_n
.node
) {
1667 if (stream
->session
->id
== session_id
&&
1668 !stream
->data_pending_check_done
) {
1669 is_data_inflight
= 1;
1670 DBG("Data is still in flight for stream %" PRIu64
,
1671 stream
->stream_handle
);
1677 /* All good, send back reply. */
1678 reply
.ret_code
= htobe32(is_data_inflight
);
1680 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1682 ERR("Relay end data pending send reply failed");
1690 * relay_process_control: Process the commands received on the control socket
1693 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1694 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1698 switch (be32toh(recv_hdr
->cmd
)) {
1699 case RELAYD_CREATE_SESSION
:
1700 ret
= relay_create_session(recv_hdr
, cmd
);
1702 case RELAYD_ADD_STREAM
:
1703 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1705 case RELAYD_START_DATA
:
1706 ret
= relay_start(recv_hdr
, cmd
);
1708 case RELAYD_SEND_METADATA
:
1709 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1711 case RELAYD_VERSION
:
1712 ret
= relay_send_version(recv_hdr
, cmd
);
1714 case RELAYD_CLOSE_STREAM
:
1715 ret
= relay_close_stream(recv_hdr
, cmd
, streams_ht
);
1717 case RELAYD_DATA_PENDING
:
1718 ret
= relay_data_pending(recv_hdr
, cmd
, streams_ht
);
1720 case RELAYD_QUIESCENT_CONTROL
:
1721 ret
= relay_quiescent_control(recv_hdr
, cmd
, streams_ht
);
1723 case RELAYD_BEGIN_DATA_PENDING
:
1724 ret
= relay_begin_data_pending(recv_hdr
, cmd
, streams_ht
);
1726 case RELAYD_END_DATA_PENDING
:
1727 ret
= relay_end_data_pending(recv_hdr
, cmd
, streams_ht
);
1729 case RELAYD_UPDATE_SYNC_INFO
:
1731 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1732 relay_unknown_command(cmd
);
1742 * relay_process_data: Process the data received on the data socket
1745 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1748 struct relay_stream
*stream
;
1749 struct lttcomm_relayd_data_hdr data_hdr
;
1751 uint64_t net_seq_num
;
1754 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1755 sizeof(struct lttcomm_relayd_data_hdr
), 0);
1758 /* Orderly shutdown. Not necessary to print an error. */
1759 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1761 ERR("Unable to receive data header on sock %d", cmd
->sock
->fd
);
1767 stream_id
= be64toh(data_hdr
.stream_id
);
1770 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1776 data_size
= be32toh(data_hdr
.data_size
);
1777 if (data_buffer_size
< data_size
) {
1780 tmp_data_ptr
= realloc(data_buffer
, data_size
);
1781 if (!tmp_data_ptr
) {
1782 ERR("Allocating data buffer");
1787 data_buffer
= tmp_data_ptr
;
1788 data_buffer_size
= data_size
;
1790 memset(data_buffer
, 0, data_size
);
1792 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1794 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1795 data_size
, stream_id
, net_seq_num
);
1796 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1799 /* Orderly shutdown. Not necessary to print an error. */
1800 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
1807 ret
= write(stream
->fd
, data_buffer
, data_size
);
1808 } while (ret
< 0 && errno
== EINTR
);
1809 if (ret
< 0 || ret
!= data_size
) {
1810 ERR("Relay error writing data to file");
1815 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
1816 ret
, stream
->stream_handle
);
1818 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
1823 stream
->prev_seq
= net_seq_num
;
1825 /* Check if we need to close the FD */
1826 if (close_stream_check(stream
)) {
1828 struct lttng_ht_iter iter
;
1830 cret
= close(stream
->fd
);
1832 PERROR("close stream process data");
1834 iter
.iter
.node
= &stream
->stream_n
.node
;
1835 ret
= lttng_ht_del(streams_ht
, &iter
);
1837 call_rcu(&stream
->rcu_node
,
1838 deferred_free_stream
);
1839 DBG("Closed tracefile %d after recv data", stream
->fd
);
1849 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1853 lttng_poll_del(events
, pollfd
);
1855 ret
= close(pollfd
);
1857 ERR("Closing pollfd %d", pollfd
);
1862 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1863 struct lttng_ht
*relay_connections_ht
)
1865 struct relay_command
*relay_connection
;
1868 relay_connection
= zmalloc(sizeof(struct relay_command
));
1869 if (relay_connection
== NULL
) {
1870 PERROR("Relay command zmalloc");
1874 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1875 } while (ret
< 0 && errno
== EINTR
);
1876 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
1877 PERROR("read relay cmd pipe");
1881 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1882 (unsigned long) relay_connection
->sock
->fd
);
1884 lttng_ht_add_unique_ulong(relay_connections_ht
,
1885 &relay_connection
->sock_n
);
1887 return lttng_poll_add(events
,
1888 relay_connection
->sock
->fd
,
1889 LPOLLIN
| LPOLLRDHUP
);
1892 free(relay_connection
);
1898 void deferred_free_connection(struct rcu_head
*head
)
1900 struct relay_command
*relay_connection
=
1901 caa_container_of(head
, struct relay_command
, rcu_node
);
1903 lttcomm_destroy_sock(relay_connection
->sock
);
1904 free(relay_connection
);
1908 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
1909 struct lttng_ht
*streams_ht
, struct lttng_ht_iter
*iter
,
1910 struct relay_command
*relay_connection
)
1914 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1916 if (relay_connection
->type
== RELAY_CONTROL
) {
1917 relay_delete_session(relay_connection
, streams_ht
);
1920 call_rcu(&relay_connection
->rcu_node
,
1921 deferred_free_connection
);
1925 * This thread does the actual work
1928 void *relay_thread_worker(void *data
)
1930 int ret
, err
= -1, last_seen_data_fd
= -1;
1932 struct relay_command
*relay_connection
;
1933 struct lttng_poll_event events
;
1934 struct lttng_ht
*relay_connections_ht
;
1935 struct lttng_ht_node_ulong
*node
;
1936 struct lttng_ht_iter iter
;
1937 struct lttng_ht
*streams_ht
;
1938 struct lttcomm_relayd_hdr recv_hdr
;
1940 DBG("[thread] Relay worker started");
1942 rcu_register_thread();
1944 /* table of connections indexed on socket */
1945 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1946 if (!relay_connections_ht
) {
1947 goto relay_connections_ht_error
;
1950 /* tables of streams indexed by stream ID */
1951 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1953 goto streams_ht_error
;
1956 ret
= create_thread_poll_set(&events
, 2);
1958 goto error_poll_create
;
1961 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1968 int idx
= -1, i
, seen_control
= 0, last_notdel_data_fd
= -1;
1970 /* Infinite blocking call, waiting for transmission */
1971 DBG3("Relayd worker thread polling...");
1972 ret
= lttng_poll_wait(&events
, -1);
1975 * Restart interrupted system call.
1977 if (errno
== EINTR
) {
1986 * Process control. The control connection is prioritised so we don't
1987 * starve it with high throughout put tracing data on the data
1990 for (i
= 0; i
< nb_fd
; i
++) {
1991 /* Fetch once the poll data */
1992 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1993 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1995 /* Thread quit pipe has been closed. Killing thread. */
1996 ret
= check_thread_quit_pipe(pollfd
, revents
);
2002 /* Inspect the relay cmd pipe for new connection */
2003 if (pollfd
== relay_cmd_pipe
[0]) {
2004 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2005 ERR("Relay pipe error");
2007 } else if (revents
& LPOLLIN
) {
2008 DBG("Relay command received");
2009 ret
= relay_add_connection(relay_cmd_pipe
[0],
2010 &events
, relay_connections_ht
);
2015 } else if (revents
) {
2017 lttng_ht_lookup(relay_connections_ht
,
2018 (void *)((unsigned long) pollfd
),
2020 node
= lttng_ht_iter_get_node_ulong(&iter
);
2022 DBG2("Relay sock %d not found", pollfd
);
2026 relay_connection
= caa_container_of(node
,
2027 struct relay_command
, sock_n
);
2029 if (revents
& (LPOLLERR
)) {
2031 relay_cleanup_poll_connection(&events
, pollfd
);
2032 relay_del_connection(relay_connections_ht
,
2035 if (last_seen_data_fd
== pollfd
) {
2036 last_seen_data_fd
= last_notdel_data_fd
;
2038 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
2039 DBG("Socket %d hung up", pollfd
);
2040 relay_cleanup_poll_connection(&events
, pollfd
);
2041 relay_del_connection(relay_connections_ht
,
2044 if (last_seen_data_fd
== pollfd
) {
2045 last_seen_data_fd
= last_notdel_data_fd
;
2047 } else if (revents
& LPOLLIN
) {
2048 /* control socket */
2049 if (relay_connection
->type
== RELAY_CONTROL
) {
2050 ret
= relay_connection
->sock
->ops
->recvmsg(
2051 relay_connection
->sock
, &recv_hdr
,
2052 sizeof(struct lttcomm_relayd_hdr
), 0);
2053 /* connection closed */
2055 relay_cleanup_poll_connection(&events
, pollfd
);
2056 relay_del_connection(relay_connections_ht
,
2059 DBG("Control connection closed with %d", pollfd
);
2061 if (relay_connection
->session
) {
2062 DBG2("Relay worker receiving data for session : %" PRIu64
,
2063 relay_connection
->session
->id
);
2065 ret
= relay_process_control(&recv_hdr
,
2069 /* Clear the session on error. */
2070 relay_cleanup_poll_connection(&events
, pollfd
);
2071 relay_del_connection(relay_connections_ht
,
2074 DBG("Connection closed with %d", pollfd
);
2080 * Flag the last seen data fd not deleted. It will be
2081 * used as the last seen fd if any fd gets deleted in
2084 last_notdel_data_fd
= pollfd
;
2092 * The last loop handled a control request, go back to poll to make
2093 * sure we prioritise the control socket.
2099 if (last_seen_data_fd
>= 0) {
2100 for (i
= 0; i
< nb_fd
; i
++) {
2101 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2102 if (last_seen_data_fd
== pollfd
) {
2109 /* Process data connection. */
2110 for (i
= idx
+ 1; i
< nb_fd
; i
++) {
2111 /* Fetch the poll data. */
2112 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2113 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2115 /* Skip the command pipe. It's handled in the first loop. */
2116 if (pollfd
== relay_cmd_pipe
[0]) {
2122 lttng_ht_lookup(relay_connections_ht
,
2123 (void *)((unsigned long) pollfd
),
2125 node
= lttng_ht_iter_get_node_ulong(&iter
);
2127 /* Skip it. Might be removed before. */
2131 relay_connection
= caa_container_of(node
,
2132 struct relay_command
, sock_n
);
2134 if (revents
& LPOLLIN
) {
2135 if (relay_connection
->type
!= RELAY_DATA
) {
2139 ret
= relay_process_data(relay_connection
, streams_ht
);
2140 /* connection closed */
2142 relay_cleanup_poll_connection(&events
, pollfd
);
2143 relay_del_connection(relay_connections_ht
,
2146 DBG("Data connection closed with %d", pollfd
);
2148 * Every goto restart call sets the last seen fd where
2149 * here we don't really care since we gracefully
2150 * continue the loop after the connection is deleted.
2153 /* Keep last seen port. */
2154 last_seen_data_fd
= pollfd
;
2162 last_seen_data_fd
= -1;
2167 lttng_poll_clean(&events
);
2169 /* empty the hash table and free the memory */
2171 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
2172 node
= lttng_ht_iter_get_node_ulong(&iter
);
2174 relay_connection
= caa_container_of(node
,
2175 struct relay_command
, sock_n
);
2176 relay_del_connection(relay_connections_ht
,
2183 lttng_ht_destroy(streams_ht
);
2185 lttng_ht_destroy(relay_connections_ht
);
2186 relay_connections_ht_error
:
2187 /* Close relay cmd pipes */
2188 utils_close_pipe(relay_cmd_pipe
);
2190 DBG("Thread exited with error");
2192 DBG("Worker thread cleanup complete");
2195 rcu_unregister_thread();
2200 * Create the relay command pipe to wake thread_manage_apps.
2201 * Closed in cleanup().
2203 static int create_relay_cmd_pipe(void)
2207 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
2215 int main(int argc
, char **argv
)
2220 /* Create thread quit pipe */
2221 if ((ret
= init_thread_quit_pipe()) < 0) {
2225 /* Parse arguments */
2227 if ((ret
= parse_args(argc
, argv
)) < 0) {
2231 if ((ret
= set_signal_handler()) < 0) {
2244 /* Check if daemon is UID = 0 */
2245 is_root
= !getuid();
2248 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
2249 ERR("Need to be root to use ports < 1024");
2255 /* Setup the thread apps communication pipe. */
2256 if ((ret
= create_relay_cmd_pipe()) < 0) {
2260 /* Init relay command queue. */
2261 cds_wfq_init(&relay_cmd_queue
.queue
);
2263 /* Set up max poll set size */
2264 lttng_poll_set_max_size();
2266 /* Setup the dispatcher thread */
2267 ret
= pthread_create(&dispatcher_thread
, NULL
,
2268 relay_thread_dispatcher
, (void *) NULL
);
2270 PERROR("pthread_create dispatcher");
2271 goto exit_dispatcher
;
2274 /* Setup the worker thread */
2275 ret
= pthread_create(&worker_thread
, NULL
,
2276 relay_thread_worker
, (void *) NULL
);
2278 PERROR("pthread_create worker");
2282 /* Setup the listener thread */
2283 ret
= pthread_create(&listener_thread
, NULL
,
2284 relay_thread_listener
, (void *) NULL
);
2286 PERROR("pthread_create listener");
2291 ret
= pthread_join(listener_thread
, &status
);
2293 PERROR("pthread_join");
2294 goto error
; /* join error, exit without cleanup */
2298 ret
= pthread_join(worker_thread
, &status
);
2300 PERROR("pthread_join");
2301 goto error
; /* join error, exit without cleanup */
2305 ret
= pthread_join(dispatcher_thread
, &status
);
2307 PERROR("pthread_join");
2308 goto error
; /* join error, exit without cleanup */