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 /* Close relay cmd pipes */
243 utils_close_pipe(relay_cmd_pipe
);
245 uri_free(control_uri
);
250 * Write to writable pipe used to notify a thread.
253 int notify_thread_pipe(int wpipe
)
258 ret
= write(wpipe
, "!", 1);
259 } while (ret
< 0 && errno
== EINTR
);
261 PERROR("write poll pipe");
268 * Stop all threads by closing the thread quit pipe.
271 void stop_threads(void)
275 /* Stopping all threads */
276 DBG("Terminating all threads");
277 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
279 ERR("write error on thread quit pipe");
282 /* Dispatch thread */
283 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
284 futex_nto1_wake(&relay_cmd_queue
.futex
);
288 * Signal handler for the daemon
290 * Simply stop all worker threads, leaving main() return gracefully after
291 * joining all threads and calling cleanup().
294 void sighandler(int sig
)
298 DBG("SIGPIPE caught");
301 DBG("SIGINT caught");
305 DBG("SIGTERM caught");
314 * Setup signal handler for :
315 * SIGINT, SIGTERM, SIGPIPE
318 int set_signal_handler(void)
324 if ((ret
= sigemptyset(&sigset
)) < 0) {
325 PERROR("sigemptyset");
329 sa
.sa_handler
= sighandler
;
332 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
337 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
342 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
347 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
353 * Init thread quit pipe.
355 * Return -1 on error or 0 if all pipes are created.
358 int init_thread_quit_pipe(void)
362 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
368 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
371 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
375 if (events
== NULL
|| size
== 0) {
380 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
386 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
398 * Check if the thread quit pipe was triggered.
400 * Return 1 if it was triggered else 0;
403 int check_thread_quit_pipe(int fd
, uint32_t events
)
405 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
413 * Create and init socket from uri.
416 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
419 struct lttcomm_sock
*sock
= NULL
;
421 sock
= lttcomm_alloc_sock_from_uri(uri
);
423 ERR("Allocating socket");
427 ret
= lttcomm_create_sock(sock
);
431 DBG("Listening on sock %d", sock
->fd
);
433 ret
= sock
->ops
->bind(sock
);
438 ret
= sock
->ops
->listen(sock
, -1);
448 lttcomm_destroy_sock(sock
);
454 * Return nonzero if stream needs to be closed.
457 int close_stream_check(struct relay_stream
*stream
)
460 if (stream
->close_flag
&& stream
->prev_seq
== stream
->last_net_seq_num
) {
467 * This thread manages the listening for new connections on the network
470 void *relay_thread_listener(void *data
)
472 int i
, ret
, pollfd
, err
= -1;
474 uint32_t revents
, nb_fd
;
475 struct lttng_poll_event events
;
476 struct lttcomm_sock
*control_sock
, *data_sock
;
478 DBG("[thread] Relay listener started");
480 control_sock
= relay_init_sock(control_uri
);
482 goto error_sock_control
;
485 data_sock
= relay_init_sock(data_uri
);
487 goto error_sock_relay
;
491 * Pass 3 as size here for the thread quit pipe, control and data socket.
493 ret
= create_thread_poll_set(&events
, 3);
495 goto error_create_poll
;
498 /* Add the control socket */
499 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
504 /* Add the data socket */
505 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
511 DBG("Listener accepting connections");
513 nb_fd
= LTTNG_POLL_GETNB(&events
);
516 ret
= lttng_poll_wait(&events
, -1);
519 * Restart interrupted system call.
521 if (errno
== EINTR
) {
527 DBG("Relay new connection received");
528 for (i
= 0; i
< nb_fd
; i
++) {
529 /* Fetch once the poll data */
530 revents
= LTTNG_POLL_GETEV(&events
, i
);
531 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
533 /* Thread quit pipe has been closed. Killing thread. */
534 ret
= check_thread_quit_pipe(pollfd
, revents
);
540 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
541 ERR("socket poll error");
543 } else if (revents
& LPOLLIN
) {
545 * Get allocated in this thread,
546 * enqueued to a global queue, dequeued
547 * and freed in the worker thread.
549 struct relay_command
*relay_cmd
;
550 struct lttcomm_sock
*newsock
;
552 relay_cmd
= zmalloc(sizeof(struct relay_command
));
553 if (relay_cmd
== NULL
) {
554 PERROR("relay command zmalloc");
558 if (pollfd
== data_sock
->fd
) {
559 newsock
= data_sock
->ops
->accept(data_sock
);
561 PERROR("accepting data sock");
565 relay_cmd
->type
= RELAY_DATA
;
566 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
568 assert(pollfd
== control_sock
->fd
);
569 newsock
= control_sock
->ops
->accept(control_sock
);
571 PERROR("accepting control sock");
575 relay_cmd
->type
= RELAY_CONTROL
;
576 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
578 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
581 PERROR("setsockopt inet");
582 lttcomm_destroy_sock(newsock
);
586 relay_cmd
->sock
= newsock
;
588 * Lock free enqueue the request.
590 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
593 * Wake the dispatch queue futex. Implicit memory
594 * barrier with the exchange in cds_wfq_enqueue.
596 futex_nto1_wake(&relay_cmd_queue
.futex
);
604 lttng_poll_clean(&events
);
606 if (data_sock
->fd
>= 0) {
607 ret
= data_sock
->ops
->close(data_sock
);
612 lttcomm_destroy_sock(data_sock
);
614 if (control_sock
->fd
>= 0) {
615 ret
= control_sock
->ops
->close(control_sock
);
620 lttcomm_destroy_sock(control_sock
);
623 DBG("Thread exited with error");
625 DBG("Relay listener thread cleanup complete");
631 * This thread manages the dispatching of the requests to worker threads
634 void *relay_thread_dispatcher(void *data
)
637 struct cds_wfq_node
*node
;
638 struct relay_command
*relay_cmd
= NULL
;
640 DBG("[thread] Relay dispatcher started");
642 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
643 /* Atomically prepare the queue futex */
644 futex_nto1_prepare(&relay_cmd_queue
.futex
);
647 /* Dequeue commands */
648 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
650 DBG("Woken up but nothing in the relay command queue");
651 /* Continue thread execution */
655 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
656 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
659 * Inform worker thread of the new request. This
660 * call is blocking so we can be assured that the data will be read
661 * at some point in time or wait to the end of the world :)
664 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
665 sizeof(struct relay_command
));
666 } while (ret
< 0 && errno
== EINTR
);
669 PERROR("write cmd pipe");
672 } while (node
!= NULL
);
674 /* Futex wait on queue. Blocking call on futex() */
675 futex_nto1_wait(&relay_cmd_queue
.futex
);
679 DBG("Dispatch thread dying");
685 * Return the realpath(3) of the path even if the last directory token does not
686 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
687 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
688 * fails if the end point directory does not exist.
691 char *expand_full_path(const char *path
)
693 const char *end_path
= path
;
694 char *next
, *cut_path
, *expanded_path
, *respath
;
696 /* Find last token delimited by '/' */
697 while ((next
= strpbrk(end_path
+ 1, "/"))) {
701 /* Cut last token from original path */
702 cut_path
= strndup(path
, end_path
- path
);
704 expanded_path
= malloc(PATH_MAX
);
705 if (expanded_path
== NULL
) {
710 respath
= realpath(cut_path
, expanded_path
);
711 if (respath
== NULL
) {
714 ERR("%s: No such file or directory", cut_path
);
722 /* Add end part to expanded path */
723 strcat(respath
, end_path
);
732 * config_get_default_path
734 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
737 char *config_get_default_path(void)
739 return getenv("HOME");
743 * Create recursively directory using the FULL path.
746 int mkdir_recursive(char *path
, mode_t mode
)
748 char *p
, tmp
[PATH_MAX
];
753 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
755 PERROR("snprintf mkdir");
760 if (tmp
[len
- 1] == '/') {
764 for (p
= tmp
+ 1; *p
; p
++) {
767 if (tmp
[strlen(tmp
) - 1] == '.' &&
768 tmp
[strlen(tmp
) - 2] == '.' &&
769 tmp
[strlen(tmp
) - 3] == '/') {
770 ERR("Using '/../' is not permitted in the trace path (%s)",
775 ret
= stat(tmp
, &statbuf
);
777 ret
= mkdir(tmp
, mode
);
779 if (errno
!= EEXIST
) {
780 PERROR("mkdir recursive");
790 ret
= mkdir(tmp
, mode
);
792 if (errno
!= EEXIST
) {
793 PERROR("mkdir recursive last piece");
805 char *create_output_path_auto(char *path_name
)
808 char *traces_path
= NULL
;
809 char *alloc_path
= NULL
;
812 default_path
= config_get_default_path();
813 if (default_path
== NULL
) {
814 ERR("Home path not found.\n \
815 Please specify an output path using -o, --output PATH");
818 alloc_path
= strdup(default_path
);
819 if (alloc_path
== NULL
) {
820 PERROR("Path allocation");
823 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
824 "/%s", alloc_path
, path_name
);
826 PERROR("asprintf trace dir name");
835 char *create_output_path_noauto(char *path_name
)
838 char *traces_path
= NULL
;
841 full_path
= expand_full_path(opt_output_path
);
842 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
844 PERROR("asprintf trace dir name");
853 * create_output_path: create the output trace directory
856 char *create_output_path(char *path_name
)
858 if (opt_output_path
== NULL
) {
859 return create_output_path_auto(path_name
);
861 return create_output_path_noauto(path_name
);
866 void deferred_free_stream(struct rcu_head
*head
)
868 struct relay_stream
*stream
=
869 caa_container_of(head
, struct relay_stream
, rcu_node
);
874 * relay_delete_session: Free all memory associated with a session and
878 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
880 struct lttng_ht_iter iter
;
881 struct lttng_ht_node_ulong
*node
;
882 struct relay_stream
*stream
;
889 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
891 lttcomm_destroy_sock(cmd
->session
->sock
);
894 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
895 node
= lttng_ht_iter_get_node_ulong(&iter
);
897 stream
= caa_container_of(node
,
898 struct relay_stream
, stream_n
);
899 if (stream
->session
== cmd
->session
) {
901 ret
= lttng_ht_del(streams_ht
, &iter
);
903 call_rcu(&stream
->rcu_node
,
904 deferred_free_stream
);
914 * relay_add_stream: allocate a new stream for a session
917 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
918 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
920 struct relay_session
*session
= cmd
->session
;
921 struct lttcomm_relayd_add_stream stream_info
;
922 struct relay_stream
*stream
= NULL
;
923 struct lttcomm_relayd_status_stream reply
;
924 char *path
= NULL
, *root_path
= NULL
;
927 if (!session
|| session
->version_check_done
== 0) {
928 ERR("Trying to add a stream before version check");
933 /* FIXME : use data_size for something ? */
934 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
935 sizeof(struct lttcomm_relayd_add_stream
), MSG_WAITALL
);
936 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
937 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
941 stream
= zmalloc(sizeof(struct relay_stream
));
942 if (stream
== NULL
) {
943 PERROR("relay stream zmalloc");
949 stream
->stream_handle
= ++last_relay_stream_id
;
950 stream
->prev_seq
= -1ULL;
951 stream
->session
= session
;
953 root_path
= create_output_path(stream_info
.pathname
);
958 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
960 ERR("relay creating output directory");
964 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
966 PERROR("asprintf stream path");
970 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
972 PERROR("Relay creating trace file");
977 DBG("Tracefile %s created", path
);
979 lttng_ht_node_init_ulong(&stream
->stream_n
,
980 (unsigned long) stream
->stream_handle
);
981 lttng_ht_add_unique_ulong(streams_ht
,
984 DBG("Relay new stream added %s", stream_info
.channel_name
);
989 /* send the session id to the client or a negative return code on error */
991 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
993 reply
.ret_code
= htobe32(LTTNG_OK
);
995 reply
.handle
= htobe64(stream
->stream_handle
);
996 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
997 sizeof(struct lttcomm_relayd_status_stream
), 0);
999 ERR("Relay sending stream id");
1008 * relay_close_stream: close a specific stream
1011 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1012 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1014 struct relay_session
*session
= cmd
->session
;
1015 struct lttcomm_relayd_close_stream stream_info
;
1016 struct lttcomm_relayd_generic_reply reply
;
1017 struct relay_stream
*stream
;
1019 struct lttng_ht_node_ulong
*node
;
1020 struct lttng_ht_iter iter
;
1022 DBG("Close stream received");
1024 if (!session
|| session
->version_check_done
== 0) {
1025 ERR("Trying to close a stream before version check");
1027 goto end_no_session
;
1030 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1031 sizeof(struct lttcomm_relayd_close_stream
), MSG_WAITALL
);
1032 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1033 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1035 goto end_no_session
;
1039 lttng_ht_lookup(streams_ht
,
1040 (void *)((unsigned long) be64toh(stream_info
.stream_id
)),
1042 node
= lttng_ht_iter_get_node_ulong(&iter
);
1044 DBG("Relay stream %" PRIu64
" not found", be64toh(stream_info
.stream_id
));
1049 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1055 stream
->last_net_seq_num
= be64toh(stream_info
.last_net_seq_num
);
1056 stream
->close_flag
= 1;
1058 if (close_stream_check(stream
)) {
1062 delret
= lttng_ht_del(streams_ht
, &iter
);
1064 call_rcu(&stream
->rcu_node
,
1065 deferred_free_stream
);
1066 DBG("Closed tracefile %d from close stream", stream
->fd
);
1073 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1075 reply
.ret_code
= htobe32(LTTNG_OK
);
1077 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1078 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1080 ERR("Relay sending stream id");
1088 * relay_unknown_command: send -1 if received unknown command
1091 void relay_unknown_command(struct relay_command
*cmd
)
1093 struct lttcomm_relayd_generic_reply reply
;
1096 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1097 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1098 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1100 ERR("Relay sending unknown command");
1105 * relay_start: send an acknowledgment to the client to tell if we are
1106 * ready to receive data. We are ready if a session is established.
1109 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1110 struct relay_command
*cmd
)
1112 int ret
= htobe32(LTTNG_OK
);
1113 struct lttcomm_relayd_generic_reply reply
;
1114 struct relay_session
*session
= cmd
->session
;
1117 DBG("Trying to start the streaming without a session established");
1118 ret
= htobe32(LTTNG_ERR_UNK
);
1121 reply
.ret_code
= ret
;
1122 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1123 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1125 ERR("Relay sending start ack");
1132 * Get stream from stream id.
1133 * Need to be called with RCU read-side lock held.
1136 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
1137 struct lttng_ht
*streams_ht
)
1139 struct lttng_ht_node_ulong
*node
;
1140 struct lttng_ht_iter iter
;
1141 struct relay_stream
*ret
;
1143 lttng_ht_lookup(streams_ht
,
1144 (void *)((unsigned long) stream_id
),
1146 node
= lttng_ht_iter_get_node_ulong(&iter
);
1148 DBG("Relay stream %" PRIu64
" not found", stream_id
);
1153 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1160 * Append padding to the file pointed by the file descriptor fd.
1162 static int write_padding_to_file(int fd
, uint32_t size
)
1171 zeros
= zmalloc(size
);
1172 if (zeros
== NULL
) {
1173 PERROR("zmalloc zeros for padding");
1179 ret
= write(fd
, zeros
, size
);
1180 } while (ret
< 0 && errno
== EINTR
);
1182 PERROR("write padding to file");
1192 * relay_recv_metadata: receive the metada for the session.
1195 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1196 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1198 int ret
= htobe32(LTTNG_OK
);
1199 struct relay_session
*session
= cmd
->session
;
1200 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1201 struct relay_stream
*metadata_stream
;
1202 uint64_t data_size
, payload_size
;
1205 ERR("Metadata sent before version check");
1210 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1211 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1212 ERR("Incorrect data size");
1216 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1218 if (data_buffer_size
< data_size
) {
1219 /* In case the realloc fails, we can free the memory */
1220 char *tmp_data_ptr
= data_buffer
;
1221 data_buffer
= realloc(data_buffer
, data_size
);
1223 ERR("Allocating data buffer");
1228 data_buffer_size
= data_size
;
1230 memset(data_buffer
, 0, data_size
);
1231 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1232 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
,
1234 if (ret
< 0 || ret
!= data_size
) {
1236 ERR("Relay didn't receive the whole metadata");
1239 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1242 metadata_stream
= relay_stream_from_stream_id(
1243 be64toh(metadata_struct
->stream_id
), streams_ht
);
1244 if (!metadata_stream
) {
1250 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1252 } while (ret
< 0 && errno
== EINTR
);
1253 if (ret
< payload_size
) {
1254 ERR("Relay error writing metadata on file");
1259 ret
= write_padding_to_file(metadata_stream
->fd
,
1260 be32toh(metadata_struct
->padding_size
));
1265 DBG2("Relay metadata written");
1274 * relay_send_version: send relayd version number
1277 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1278 struct relay_command
*cmd
)
1281 struct lttcomm_relayd_version reply
;
1282 struct relay_session
*session
;
1284 if (cmd
->session
== NULL
) {
1285 session
= zmalloc(sizeof(struct relay_session
));
1286 if (session
== NULL
) {
1287 PERROR("relay session zmalloc");
1291 session
->id
= ++last_relay_session_id
;
1292 DBG("Created session %" PRIu64
, session
->id
);
1293 cmd
->session
= session
;
1295 session
= cmd
->session
;
1297 session
->version_check_done
= 1;
1299 ret
= sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1301 ERR("Error in scanning version");
1305 reply
.major
= htobe32(reply
.major
);
1306 reply
.minor
= htobe32(reply
.minor
);
1307 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1308 sizeof(struct lttcomm_relayd_version
), 0);
1310 ERR("Relay sending version");
1312 DBG("Version check done (%u.%u)", be32toh(reply
.major
),
1313 be32toh(reply
.minor
));
1320 * Check for data availability for a given stream id from the session daemon.
1323 int relay_data_available(struct lttcomm_relayd_hdr
*recv_hdr
,
1324 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1326 struct relay_session
*session
= cmd
->session
;
1327 struct lttcomm_relayd_data_available msg
;
1328 struct lttcomm_relayd_generic_reply reply
;
1329 struct relay_stream
*stream
;
1331 struct lttng_ht_node_ulong
*node
;
1332 struct lttng_ht_iter iter
;
1333 uint64_t last_net_seq_num
, stream_id
;
1335 DBG("Data available command received");
1337 if (!session
|| session
->version_check_done
== 0) {
1338 ERR("Trying to check for data before version check");
1340 goto end_no_session
;
1343 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), MSG_WAITALL
);
1344 if (ret
< sizeof(msg
)) {
1345 ERR("Relay didn't receive valid data_available struct size : %d", ret
);
1347 goto end_no_session
;
1350 stream_id
= be64toh(msg
.stream_id
);
1351 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1354 lttng_ht_lookup(streams_ht
, (void *)((unsigned long) stream_id
), &iter
);
1355 node
= lttng_ht_iter_get_node_ulong(&iter
);
1357 DBG("Relay stream %" PRIu64
" not found", stream_id
);
1362 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1365 DBG("Data available for stream id %" PRIu64
" prev_seq %" PRIu64
1366 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1369 /* Avoid wrapping issue */
1370 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) <= 0) {
1371 /* Data has in fact been written and is available */
1374 /* Data still being streamed. */
1381 reply
.ret_code
= htobe32(ret
);
1382 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1384 ERR("Relay data available ret code failed");
1392 * Wait for the control socket to reach a quiescent state.
1394 * Note that for now, when receiving this command from the session daemon, this
1395 * means that every subsequent commands or data received on the control socket
1396 * has been handled. So, this is why we simply return OK here.
1399 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1400 struct relay_command
*cmd
)
1403 struct lttcomm_relayd_generic_reply reply
;
1405 DBG("Checking quiescent state on control socket");
1407 reply
.ret_code
= htobe32(LTTNG_OK
);
1408 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1410 ERR("Relay data available ret code failed");
1417 * relay_process_control: Process the commands received on the control socket
1420 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1421 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1425 switch (be32toh(recv_hdr
->cmd
)) {
1427 case RELAYD_CREATE_SESSION:
1428 ret = relay_create_session(recv_hdr, cmd);
1431 case RELAYD_ADD_STREAM
:
1432 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1434 case RELAYD_START_DATA
:
1435 ret
= relay_start(recv_hdr
, cmd
);
1437 case RELAYD_SEND_METADATA
:
1438 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1440 case RELAYD_VERSION
:
1441 ret
= relay_send_version(recv_hdr
, cmd
);
1443 case RELAYD_CLOSE_STREAM
:
1444 ret
= relay_close_stream(recv_hdr
, cmd
, streams_ht
);
1446 case RELAYD_DATA_AVAILABLE
:
1447 ret
= relay_data_available(recv_hdr
, cmd
, streams_ht
);
1449 case RELAYD_QUIESCENT_CONTROL
:
1450 ret
= relay_quiescent_control(recv_hdr
, cmd
);
1452 case RELAYD_UPDATE_SYNC_INFO
:
1454 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1455 relay_unknown_command(cmd
);
1465 * relay_process_data: Process the data received on the data socket
1468 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1471 struct relay_stream
*stream
;
1472 struct lttcomm_relayd_data_hdr data_hdr
;
1474 uint64_t net_seq_num
;
1477 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1478 sizeof(struct lttcomm_relayd_data_hdr
), MSG_WAITALL
);
1480 ERR("Connections seems to be closed");
1485 stream_id
= be64toh(data_hdr
.stream_id
);
1488 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1494 data_size
= be32toh(data_hdr
.data_size
);
1495 if (data_buffer_size
< data_size
) {
1496 char *tmp_data_ptr
= data_buffer
;
1497 data_buffer
= realloc(data_buffer
, data_size
);
1499 ERR("Allocating data buffer");
1504 data_buffer_size
= data_size
;
1506 memset(data_buffer
, 0, data_size
);
1508 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1510 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1511 data_size
, stream_id
, net_seq_num
);
1512 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1519 ret
= write(stream
->fd
, data_buffer
, data_size
);
1520 } while (ret
< 0 && errno
== EINTR
);
1521 if (ret
< data_size
) {
1522 ERR("Relay error writing data to file");
1527 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
1528 ret
, stream
->stream_handle
);
1530 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
1535 stream
->prev_seq
= net_seq_num
;
1537 /* Check if we need to close the FD */
1538 if (close_stream_check(stream
)) {
1539 struct lttng_ht_iter iter
;
1542 iter
.iter
.node
= &stream
->stream_n
.node
;
1543 ret
= lttng_ht_del(streams_ht
, &iter
);
1545 call_rcu(&stream
->rcu_node
,
1546 deferred_free_stream
);
1547 DBG("Closed tracefile %d after recv data", stream
->fd
);
1557 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1561 lttng_poll_del(events
, pollfd
);
1563 ret
= close(pollfd
);
1565 ERR("Closing pollfd %d", pollfd
);
1570 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1571 struct lttng_ht
*relay_connections_ht
)
1573 struct relay_command
*relay_connection
;
1576 relay_connection
= zmalloc(sizeof(struct relay_command
));
1577 if (relay_connection
== NULL
) {
1578 PERROR("Relay command zmalloc");
1581 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1582 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
1583 PERROR("read relay cmd pipe");
1587 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1588 (unsigned long) relay_connection
->sock
->fd
);
1590 lttng_ht_add_unique_ulong(relay_connections_ht
,
1591 &relay_connection
->sock_n
);
1593 return lttng_poll_add(events
,
1594 relay_connection
->sock
->fd
,
1595 LPOLLIN
| LPOLLRDHUP
);
1598 free(relay_connection
);
1604 void deferred_free_connection(struct rcu_head
*head
)
1606 struct relay_command
*relay_connection
=
1607 caa_container_of(head
, struct relay_command
, rcu_node
);
1609 lttcomm_destroy_sock(relay_connection
->sock
);
1610 free(relay_connection
);
1614 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
1615 struct lttng_ht
*streams_ht
, struct lttng_ht_iter
*iter
,
1616 struct relay_command
*relay_connection
)
1620 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1622 if (relay_connection
->type
== RELAY_CONTROL
) {
1623 relay_delete_session(relay_connection
, streams_ht
);
1626 call_rcu(&relay_connection
->rcu_node
,
1627 deferred_free_connection
);
1631 * This thread does the actual work
1634 void *relay_thread_worker(void *data
)
1636 int i
, ret
, pollfd
, err
= -1;
1637 uint32_t revents
, nb_fd
;
1638 struct relay_command
*relay_connection
;
1639 struct lttng_poll_event events
;
1640 struct lttng_ht
*relay_connections_ht
;
1641 struct lttng_ht_node_ulong
*node
;
1642 struct lttng_ht_iter iter
;
1643 struct lttng_ht
*streams_ht
;
1644 struct lttcomm_relayd_hdr recv_hdr
;
1646 DBG("[thread] Relay worker started");
1648 rcu_register_thread();
1650 /* table of connections indexed on socket */
1651 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1652 if (!relay_connections_ht
) {
1653 goto relay_connections_ht_error
;
1656 /* tables of streams indexed by stream ID */
1657 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1659 goto streams_ht_error
;
1662 ret
= create_thread_poll_set(&events
, 2);
1664 goto error_poll_create
;
1667 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1673 /* Zeroed the events structure */
1674 lttng_poll_reset(&events
);
1676 nb_fd
= LTTNG_POLL_GETNB(&events
);
1678 /* Infinite blocking call, waiting for transmission */
1680 DBG3("Relayd worker thread polling...");
1681 ret
= lttng_poll_wait(&events
, -1);
1684 * Restart interrupted system call.
1686 if (errno
== EINTR
) {
1692 for (i
= 0; i
< nb_fd
; i
++) {
1693 /* Fetch once the poll data */
1694 revents
= LTTNG_POLL_GETEV(&events
, i
);
1695 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1697 /* Thread quit pipe has been closed. Killing thread. */
1698 ret
= check_thread_quit_pipe(pollfd
, revents
);
1704 /* Inspect the relay cmd pipe for new connection */
1705 if (pollfd
== relay_cmd_pipe
[0]) {
1706 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1707 ERR("Relay pipe error");
1709 } else if (revents
& LPOLLIN
) {
1710 DBG("Relay command received");
1711 ret
= relay_add_connection(relay_cmd_pipe
[0],
1712 &events
, relay_connections_ht
);
1717 } else if (revents
> 0) {
1719 lttng_ht_lookup(relay_connections_ht
,
1720 (void *)((unsigned long) pollfd
),
1722 node
= lttng_ht_iter_get_node_ulong(&iter
);
1724 DBG2("Relay sock %d not found", pollfd
);
1728 relay_connection
= caa_container_of(node
,
1729 struct relay_command
, sock_n
);
1731 if (revents
& (LPOLLERR
)) {
1733 relay_cleanup_poll_connection(&events
, pollfd
);
1734 relay_del_connection(relay_connections_ht
,
1737 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1738 DBG("Socket %d hung up", pollfd
);
1739 relay_cleanup_poll_connection(&events
, pollfd
);
1740 relay_del_connection(relay_connections_ht
,
1743 } else if (revents
& LPOLLIN
) {
1744 /* control socket */
1745 if (relay_connection
->type
== RELAY_CONTROL
) {
1746 ret
= relay_connection
->sock
->ops
->recvmsg(
1747 relay_connection
->sock
, &recv_hdr
,
1748 sizeof(struct lttcomm_relayd_hdr
), MSG_WAITALL
);
1749 /* connection closed */
1751 relay_cleanup_poll_connection(&events
, pollfd
);
1752 relay_del_connection(relay_connections_ht
,
1755 DBG("Control connection closed with %d", pollfd
);
1757 if (relay_connection
->session
) {
1758 DBG2("Relay worker receiving data for session : %" PRIu64
,
1759 relay_connection
->session
->id
);
1761 ret
= relay_process_control(&recv_hdr
,
1765 * there was an error in processing a control
1766 * command: clear the session
1769 relay_cleanup_poll_connection(&events
, pollfd
);
1770 relay_del_connection(relay_connections_ht
,
1773 DBG("Connection closed with %d", pollfd
);
1777 } else if (relay_connection
->type
== RELAY_DATA
) {
1778 ret
= relay_process_data(relay_connection
, streams_ht
);
1779 /* connection closed */
1781 relay_cleanup_poll_connection(&events
, pollfd
);
1782 relay_del_connection(relay_connections_ht
,
1785 DBG("Data connection closed with %d", pollfd
);
1796 lttng_poll_clean(&events
);
1798 /* empty the hash table and free the memory */
1800 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1801 node
= lttng_ht_iter_get_node_ulong(&iter
);
1803 relay_connection
= caa_container_of(node
,
1804 struct relay_command
, sock_n
);
1805 relay_del_connection(relay_connections_ht
,
1812 lttng_ht_destroy(streams_ht
);
1814 lttng_ht_destroy(relay_connections_ht
);
1815 relay_connections_ht_error
:
1817 DBG("Thread exited with error");
1819 DBG("Worker thread cleanup complete");
1822 rcu_unregister_thread();
1827 * Create the relay command pipe to wake thread_manage_apps.
1828 * Closed in cleanup().
1830 static int create_relay_cmd_pipe(void)
1834 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
1842 int main(int argc
, char **argv
)
1847 /* Create thread quit pipe */
1848 if ((ret
= init_thread_quit_pipe()) < 0) {
1852 /* Parse arguments */
1854 if ((ret
= parse_args(argc
, argv
) < 0)) {
1858 if ((ret
= set_signal_handler()) < 0) {
1871 /* Check if daemon is UID = 0 */
1872 is_root
= !getuid();
1875 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1876 ERR("Need to be root to use ports < 1024");
1882 /* Setup the thread apps communication pipe. */
1883 if ((ret
= create_relay_cmd_pipe()) < 0) {
1887 /* Init relay command queue. */
1888 cds_wfq_init(&relay_cmd_queue
.queue
);
1890 /* Set up max poll set size */
1891 lttng_poll_set_max_size();
1893 /* Setup the dispatcher thread */
1894 ret
= pthread_create(&dispatcher_thread
, NULL
,
1895 relay_thread_dispatcher
, (void *) NULL
);
1897 PERROR("pthread_create dispatcher");
1898 goto exit_dispatcher
;
1901 /* Setup the worker thread */
1902 ret
= pthread_create(&worker_thread
, NULL
,
1903 relay_thread_worker
, (void *) NULL
);
1905 PERROR("pthread_create worker");
1909 /* Setup the listener thread */
1910 ret
= pthread_create(&listener_thread
, NULL
,
1911 relay_thread_listener
, (void *) NULL
);
1913 PERROR("pthread_create listener");
1918 ret
= pthread_join(listener_thread
, &status
);
1920 PERROR("pthread_join");
1921 goto error
; /* join error, exit without cleanup */
1925 ret
= pthread_join(worker_thread
, &status
);
1927 PERROR("pthread_join");
1928 goto error
; /* join error, exit without cleanup */
1932 ret
= pthread_join(dispatcher_thread
, &status
);
1934 PERROR("pthread_join");
1935 goto error
; /* join error, exit without cleanup */