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
);
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
) {
464 * This thread manages the listening for new connections on the network
467 void *relay_thread_listener(void *data
)
469 int i
, ret
, pollfd
, err
= -1;
471 uint32_t revents
, nb_fd
;
472 struct lttng_poll_event events
;
473 struct lttcomm_sock
*control_sock
, *data_sock
;
475 DBG("[thread] Relay listener started");
477 control_sock
= relay_init_sock(control_uri
);
479 goto error_sock_control
;
482 data_sock
= relay_init_sock(data_uri
);
484 goto error_sock_relay
;
488 * Pass 3 as size here for the thread quit pipe, control and data socket.
490 ret
= create_thread_poll_set(&events
, 3);
492 goto error_create_poll
;
495 /* Add the control socket */
496 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
501 /* Add the data socket */
502 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
508 DBG("Listener accepting connections");
510 nb_fd
= LTTNG_POLL_GETNB(&events
);
513 ret
= lttng_poll_wait(&events
, -1);
516 * Restart interrupted system call.
518 if (errno
== EINTR
) {
524 DBG("Relay new connection received");
525 for (i
= 0; i
< nb_fd
; i
++) {
526 /* Fetch once the poll data */
527 revents
= LTTNG_POLL_GETEV(&events
, i
);
528 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
530 /* Thread quit pipe has been closed. Killing thread. */
531 ret
= check_thread_quit_pipe(pollfd
, revents
);
537 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
538 ERR("socket poll error");
540 } else if (revents
& LPOLLIN
) {
542 * Get allocated in this thread,
543 * enqueued to a global queue, dequeued
544 * and freed in the worker thread.
546 struct relay_command
*relay_cmd
;
547 struct lttcomm_sock
*newsock
;
549 relay_cmd
= zmalloc(sizeof(struct relay_command
));
550 if (relay_cmd
== NULL
) {
551 PERROR("relay command zmalloc");
555 if (pollfd
== data_sock
->fd
) {
556 newsock
= data_sock
->ops
->accept(data_sock
);
558 PERROR("accepting data sock");
562 relay_cmd
->type
= RELAY_DATA
;
563 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
565 assert(pollfd
== control_sock
->fd
);
566 newsock
= control_sock
->ops
->accept(control_sock
);
568 PERROR("accepting control sock");
572 relay_cmd
->type
= RELAY_CONTROL
;
573 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
575 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
578 PERROR("setsockopt inet");
579 lttcomm_destroy_sock(newsock
);
583 relay_cmd
->sock
= newsock
;
585 * Lock free enqueue the request.
587 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
590 * Wake the dispatch queue futex. Implicit memory
591 * barrier with the exchange in cds_wfq_enqueue.
593 futex_nto1_wake(&relay_cmd_queue
.futex
);
601 lttng_poll_clean(&events
);
603 if (data_sock
->fd
>= 0) {
604 ret
= data_sock
->ops
->close(data_sock
);
609 lttcomm_destroy_sock(data_sock
);
611 if (control_sock
->fd
>= 0) {
612 ret
= control_sock
->ops
->close(control_sock
);
617 lttcomm_destroy_sock(control_sock
);
620 DBG("Thread exited with error");
622 DBG("Relay listener thread cleanup complete");
628 * This thread manages the dispatching of the requests to worker threads
631 void *relay_thread_dispatcher(void *data
)
634 struct cds_wfq_node
*node
;
635 struct relay_command
*relay_cmd
= NULL
;
637 DBG("[thread] Relay dispatcher started");
639 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
640 /* Atomically prepare the queue futex */
641 futex_nto1_prepare(&relay_cmd_queue
.futex
);
644 /* Dequeue commands */
645 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
647 DBG("Woken up but nothing in the relay command queue");
648 /* Continue thread execution */
652 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
653 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
656 * Inform worker thread of the new request. This
657 * call is blocking so we can be assured that the data will be read
658 * at some point in time or wait to the end of the world :)
661 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
662 sizeof(struct relay_command
));
663 } while (ret
< 0 && errno
== EINTR
);
666 PERROR("write cmd pipe");
669 } while (node
!= NULL
);
671 /* Futex wait on queue. Blocking call on futex() */
672 futex_nto1_wait(&relay_cmd_queue
.futex
);
676 DBG("Dispatch thread dying");
682 * Return the realpath(3) of the path even if the last directory token does not
683 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
684 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
685 * fails if the end point directory does not exist.
688 char *expand_full_path(const char *path
)
690 const char *end_path
= path
;
691 char *next
, *cut_path
, *expanded_path
, *respath
;
693 /* Find last token delimited by '/' */
694 while ((next
= strpbrk(end_path
+ 1, "/"))) {
698 /* Cut last token from original path */
699 cut_path
= strndup(path
, end_path
- path
);
701 expanded_path
= malloc(PATH_MAX
);
702 if (expanded_path
== NULL
) {
707 respath
= realpath(cut_path
, expanded_path
);
708 if (respath
== NULL
) {
711 ERR("%s: No such file or directory", cut_path
);
719 /* Add end part to expanded path */
720 strcat(respath
, end_path
);
729 * config_get_default_path
731 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
734 char *config_get_default_path(void)
736 return getenv("HOME");
740 * Create recursively directory using the FULL path.
743 int mkdir_recursive(char *path
, mode_t mode
)
745 char *p
, tmp
[PATH_MAX
];
750 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
752 PERROR("snprintf mkdir");
757 if (tmp
[len
- 1] == '/') {
761 for (p
= tmp
+ 1; *p
; p
++) {
764 if (tmp
[strlen(tmp
) - 1] == '.' &&
765 tmp
[strlen(tmp
) - 2] == '.' &&
766 tmp
[strlen(tmp
) - 3] == '/') {
767 ERR("Using '/../' is not permitted in the trace path (%s)",
772 ret
= stat(tmp
, &statbuf
);
774 ret
= mkdir(tmp
, mode
);
776 if (errno
!= EEXIST
) {
777 PERROR("mkdir recursive");
787 ret
= mkdir(tmp
, mode
);
789 if (errno
!= EEXIST
) {
790 PERROR("mkdir recursive last piece");
802 char *create_output_path_auto(char *path_name
)
805 char *traces_path
= NULL
;
806 char *alloc_path
= NULL
;
809 default_path
= config_get_default_path();
810 if (default_path
== NULL
) {
811 ERR("Home path not found.\n \
812 Please specify an output path using -o, --output PATH");
815 alloc_path
= strdup(default_path
);
816 if (alloc_path
== NULL
) {
817 PERROR("Path allocation");
820 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
821 "/%s", alloc_path
, path_name
);
823 PERROR("asprintf trace dir name");
832 char *create_output_path_noauto(char *path_name
)
835 char *traces_path
= NULL
;
838 full_path
= expand_full_path(opt_output_path
);
839 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
841 PERROR("asprintf trace dir name");
850 * create_output_path: create the output trace directory
853 char *create_output_path(char *path_name
)
855 if (opt_output_path
== NULL
) {
856 return create_output_path_auto(path_name
);
858 return create_output_path_noauto(path_name
);
863 void deferred_free_stream(struct rcu_head
*head
)
865 struct relay_stream
*stream
=
866 caa_container_of(head
, struct relay_stream
, rcu_node
);
871 * relay_delete_session: Free all memory associated with a session and
875 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
877 struct lttng_ht_iter iter
;
878 struct lttng_ht_node_ulong
*node
;
879 struct relay_stream
*stream
;
886 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
888 lttcomm_destroy_sock(cmd
->session
->sock
);
891 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
892 node
= lttng_ht_iter_get_node_ulong(&iter
);
894 stream
= caa_container_of(node
,
895 struct relay_stream
, stream_n
);
896 if (stream
->session
== cmd
->session
) {
897 ret
= close(stream
->fd
);
899 PERROR("close stream fd on delete 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
), 0);
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
), 0);
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
)) {
1061 delret
= close(stream
->fd
);
1063 PERROR("close stream");
1065 delret
= lttng_ht_del(streams_ht
, &iter
);
1067 call_rcu(&stream
->rcu_node
,
1068 deferred_free_stream
);
1069 DBG("Closed tracefile %d from close stream", stream
->fd
);
1076 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1078 reply
.ret_code
= htobe32(LTTNG_OK
);
1080 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1081 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1083 ERR("Relay sending stream id");
1091 * relay_unknown_command: send -1 if received unknown command
1094 void relay_unknown_command(struct relay_command
*cmd
)
1096 struct lttcomm_relayd_generic_reply reply
;
1099 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1100 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1101 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1103 ERR("Relay sending unknown command");
1108 * relay_start: send an acknowledgment to the client to tell if we are
1109 * ready to receive data. We are ready if a session is established.
1112 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1113 struct relay_command
*cmd
)
1115 int ret
= htobe32(LTTNG_OK
);
1116 struct lttcomm_relayd_generic_reply reply
;
1117 struct relay_session
*session
= cmd
->session
;
1120 DBG("Trying to start the streaming without a session established");
1121 ret
= htobe32(LTTNG_ERR_UNK
);
1124 reply
.ret_code
= ret
;
1125 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1126 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1128 ERR("Relay sending start ack");
1135 * Get stream from stream id.
1136 * Need to be called with RCU read-side lock held.
1139 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
1140 struct lttng_ht
*streams_ht
)
1142 struct lttng_ht_node_ulong
*node
;
1143 struct lttng_ht_iter iter
;
1144 struct relay_stream
*ret
;
1146 lttng_ht_lookup(streams_ht
,
1147 (void *)((unsigned long) stream_id
),
1149 node
= lttng_ht_iter_get_node_ulong(&iter
);
1151 DBG("Relay stream %" PRIu64
" not found", stream_id
);
1156 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1163 * Append padding to the file pointed by the file descriptor fd.
1165 static int write_padding_to_file(int fd
, uint32_t size
)
1174 zeros
= zmalloc(size
);
1175 if (zeros
== NULL
) {
1176 PERROR("zmalloc zeros for padding");
1182 ret
= write(fd
, zeros
, size
);
1183 } while (ret
< 0 && errno
== EINTR
);
1185 PERROR("write padding to file");
1195 * relay_recv_metadata: receive the metada for the session.
1198 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1199 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1201 int ret
= htobe32(LTTNG_OK
);
1202 struct relay_session
*session
= cmd
->session
;
1203 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1204 struct relay_stream
*metadata_stream
;
1205 uint64_t data_size
, payload_size
;
1208 ERR("Metadata sent before version check");
1213 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1214 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1215 ERR("Incorrect data size");
1219 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1221 if (data_buffer_size
< data_size
) {
1222 /* In case the realloc fails, we can free the memory */
1223 char *tmp_data_ptr
= data_buffer
;
1224 data_buffer
= realloc(data_buffer
, data_size
);
1226 ERR("Allocating data buffer");
1231 data_buffer_size
= data_size
;
1233 memset(data_buffer
, 0, data_size
);
1234 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1235 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1236 if (ret
< 0 || ret
!= data_size
) {
1238 ERR("Relay didn't receive the whole metadata");
1241 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1244 metadata_stream
= relay_stream_from_stream_id(
1245 be64toh(metadata_struct
->stream_id
), streams_ht
);
1246 if (!metadata_stream
) {
1252 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1254 } while (ret
< 0 && errno
== EINTR
);
1255 if (ret
< payload_size
) {
1256 ERR("Relay error writing metadata on file");
1261 ret
= write_padding_to_file(metadata_stream
->fd
,
1262 be32toh(metadata_struct
->padding_size
));
1267 DBG2("Relay metadata written");
1276 * relay_send_version: send relayd version number
1279 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1280 struct relay_command
*cmd
)
1283 struct lttcomm_relayd_version reply
, msg
;
1284 struct relay_session
*session
;
1286 if (cmd
->session
== NULL
) {
1287 session
= zmalloc(sizeof(struct relay_session
));
1288 if (session
== NULL
) {
1289 PERROR("relay session zmalloc");
1293 session
->id
= ++last_relay_session_id
;
1294 DBG("Created session %" PRIu64
, session
->id
);
1295 cmd
->session
= session
;
1297 session
= cmd
->session
;
1299 session
->version_check_done
= 1;
1301 /* Get version from the other side. */
1302 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1303 if (ret
< 0 || ret
!= sizeof(msg
)) {
1305 ERR("Relay failed to receive the version values.");
1310 * For now, we just ignore the received version but after 2.1 stable
1311 * release, a check must be done to see if we either adapt to the other
1312 * side version (which MUST be lower than us) or keep the latest data
1313 * structure considering that the other side will adapt.
1316 ret
= sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1318 ERR("Error in scanning version");
1322 reply
.major
= htobe32(reply
.major
);
1323 reply
.minor
= htobe32(reply
.minor
);
1324 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1325 sizeof(struct lttcomm_relayd_version
), 0);
1327 ERR("Relay sending version");
1329 DBG("Version check done (%u.%u)", be32toh(reply
.major
),
1330 be32toh(reply
.minor
));
1337 * Check for data pending for a given stream id from the session daemon.
1340 int relay_data_pending(struct lttcomm_relayd_hdr
*recv_hdr
,
1341 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1343 struct relay_session
*session
= cmd
->session
;
1344 struct lttcomm_relayd_data_pending msg
;
1345 struct lttcomm_relayd_generic_reply reply
;
1346 struct relay_stream
*stream
;
1348 struct lttng_ht_node_ulong
*node
;
1349 struct lttng_ht_iter iter
;
1350 uint64_t last_net_seq_num
, stream_id
;
1352 DBG("Data pending command received");
1354 if (!session
|| session
->version_check_done
== 0) {
1355 ERR("Trying to check for data before version check");
1357 goto end_no_session
;
1360 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
1361 if (ret
< sizeof(msg
)) {
1362 ERR("Relay didn't receive valid data_pending struct size : %d", ret
);
1364 goto end_no_session
;
1367 stream_id
= be64toh(msg
.stream_id
);
1368 last_net_seq_num
= be64toh(msg
.last_net_seq_num
);
1371 lttng_ht_lookup(streams_ht
, (void *)((unsigned long) stream_id
), &iter
);
1372 node
= lttng_ht_iter_get_node_ulong(&iter
);
1374 DBG("Relay stream %" PRIu64
" not found", stream_id
);
1379 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1382 DBG("Data pending for stream id %" PRIu64
" prev_seq %" PRIu64
1383 " and last_seq %" PRIu64
, stream_id
, stream
->prev_seq
,
1386 /* Avoid wrapping issue */
1387 if (((int64_t) (stream
->prev_seq
- last_net_seq_num
)) <= 0) {
1388 /* Data has in fact been written and is NOT pending */
1391 /* Data still being streamed thus pending */
1398 reply
.ret_code
= htobe32(ret
);
1399 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1401 ERR("Relay data pending ret code failed");
1409 * Wait for the control socket to reach a quiescent state.
1411 * Note that for now, when receiving this command from the session daemon, this
1412 * means that every subsequent commands or data received on the control socket
1413 * has been handled. So, this is why we simply return OK here.
1416 int relay_quiescent_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1417 struct relay_command
*cmd
)
1420 struct lttcomm_relayd_generic_reply reply
;
1422 DBG("Checking quiescent state on control socket");
1424 reply
.ret_code
= htobe32(LTTNG_OK
);
1425 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1427 ERR("Relay data quiescent control ret code failed");
1434 * relay_process_control: Process the commands received on the control socket
1437 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1438 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1442 switch (be32toh(recv_hdr
->cmd
)) {
1444 case RELAYD_CREATE_SESSION:
1445 ret = relay_create_session(recv_hdr, cmd);
1448 case RELAYD_ADD_STREAM
:
1449 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1451 case RELAYD_START_DATA
:
1452 ret
= relay_start(recv_hdr
, cmd
);
1454 case RELAYD_SEND_METADATA
:
1455 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1457 case RELAYD_VERSION
:
1458 ret
= relay_send_version(recv_hdr
, cmd
);
1460 case RELAYD_CLOSE_STREAM
:
1461 ret
= relay_close_stream(recv_hdr
, cmd
, streams_ht
);
1463 case RELAYD_DATA_PENDING
:
1464 ret
= relay_data_pending(recv_hdr
, cmd
, streams_ht
);
1466 case RELAYD_QUIESCENT_CONTROL
:
1467 ret
= relay_quiescent_control(recv_hdr
, cmd
);
1469 case RELAYD_UPDATE_SYNC_INFO
:
1471 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1472 relay_unknown_command(cmd
);
1482 * relay_process_data: Process the data received on the data socket
1485 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1488 struct relay_stream
*stream
;
1489 struct lttcomm_relayd_data_hdr data_hdr
;
1491 uint64_t net_seq_num
;
1494 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1495 sizeof(struct lttcomm_relayd_data_hdr
), 0);
1497 ERR("Connections seems to be closed");
1502 stream_id
= be64toh(data_hdr
.stream_id
);
1505 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1511 data_size
= be32toh(data_hdr
.data_size
);
1512 if (data_buffer_size
< data_size
) {
1513 char *tmp_data_ptr
= data_buffer
;
1514 data_buffer
= realloc(data_buffer
, data_size
);
1516 ERR("Allocating data buffer");
1521 data_buffer_size
= data_size
;
1523 memset(data_buffer
, 0, data_size
);
1525 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1527 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1528 data_size
, stream_id
, net_seq_num
);
1529 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, 0);
1536 ret
= write(stream
->fd
, data_buffer
, data_size
);
1537 } while (ret
< 0 && errno
== EINTR
);
1538 if (ret
< data_size
) {
1539 ERR("Relay error writing data to file");
1544 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
1545 ret
, stream
->stream_handle
);
1547 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
1552 stream
->prev_seq
= net_seq_num
;
1554 /* Check if we need to close the FD */
1555 if (close_stream_check(stream
)) {
1557 struct lttng_ht_iter iter
;
1559 cret
= close(stream
->fd
);
1561 PERROR("close stream process data");
1563 iter
.iter
.node
= &stream
->stream_n
.node
;
1564 ret
= lttng_ht_del(streams_ht
, &iter
);
1566 call_rcu(&stream
->rcu_node
,
1567 deferred_free_stream
);
1568 DBG("Closed tracefile %d after recv data", stream
->fd
);
1578 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1582 lttng_poll_del(events
, pollfd
);
1584 ret
= close(pollfd
);
1586 ERR("Closing pollfd %d", pollfd
);
1591 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1592 struct lttng_ht
*relay_connections_ht
)
1594 struct relay_command
*relay_connection
;
1597 relay_connection
= zmalloc(sizeof(struct relay_command
));
1598 if (relay_connection
== NULL
) {
1599 PERROR("Relay command zmalloc");
1602 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1603 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
1604 PERROR("read relay cmd pipe");
1608 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1609 (unsigned long) relay_connection
->sock
->fd
);
1611 lttng_ht_add_unique_ulong(relay_connections_ht
,
1612 &relay_connection
->sock_n
);
1614 return lttng_poll_add(events
,
1615 relay_connection
->sock
->fd
,
1616 LPOLLIN
| LPOLLRDHUP
);
1619 free(relay_connection
);
1625 void deferred_free_connection(struct rcu_head
*head
)
1627 struct relay_command
*relay_connection
=
1628 caa_container_of(head
, struct relay_command
, rcu_node
);
1630 lttcomm_destroy_sock(relay_connection
->sock
);
1631 free(relay_connection
);
1635 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
1636 struct lttng_ht
*streams_ht
, struct lttng_ht_iter
*iter
,
1637 struct relay_command
*relay_connection
)
1641 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1643 if (relay_connection
->type
== RELAY_CONTROL
) {
1644 relay_delete_session(relay_connection
, streams_ht
);
1647 call_rcu(&relay_connection
->rcu_node
,
1648 deferred_free_connection
);
1652 * This thread does the actual work
1655 void *relay_thread_worker(void *data
)
1657 int i
, ret
, pollfd
, err
= -1;
1658 uint32_t revents
, nb_fd
;
1659 struct relay_command
*relay_connection
;
1660 struct lttng_poll_event events
;
1661 struct lttng_ht
*relay_connections_ht
;
1662 struct lttng_ht_node_ulong
*node
;
1663 struct lttng_ht_iter iter
;
1664 struct lttng_ht
*streams_ht
;
1665 struct lttcomm_relayd_hdr recv_hdr
;
1667 DBG("[thread] Relay worker started");
1669 rcu_register_thread();
1671 /* table of connections indexed on socket */
1672 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1673 if (!relay_connections_ht
) {
1674 goto relay_connections_ht_error
;
1677 /* tables of streams indexed by stream ID */
1678 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1680 goto streams_ht_error
;
1683 ret
= create_thread_poll_set(&events
, 2);
1685 goto error_poll_create
;
1688 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1694 /* Zeroed the events structure */
1695 lttng_poll_reset(&events
);
1697 nb_fd
= LTTNG_POLL_GETNB(&events
);
1699 /* Infinite blocking call, waiting for transmission */
1701 DBG3("Relayd worker thread polling...");
1702 ret
= lttng_poll_wait(&events
, -1);
1705 * Restart interrupted system call.
1707 if (errno
== EINTR
) {
1713 for (i
= 0; i
< nb_fd
; i
++) {
1714 /* Fetch once the poll data */
1715 revents
= LTTNG_POLL_GETEV(&events
, i
);
1716 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1718 /* Thread quit pipe has been closed. Killing thread. */
1719 ret
= check_thread_quit_pipe(pollfd
, revents
);
1725 /* Inspect the relay cmd pipe for new connection */
1726 if (pollfd
== relay_cmd_pipe
[0]) {
1727 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1728 ERR("Relay pipe error");
1730 } else if (revents
& LPOLLIN
) {
1731 DBG("Relay command received");
1732 ret
= relay_add_connection(relay_cmd_pipe
[0],
1733 &events
, relay_connections_ht
);
1738 } else if (revents
> 0) {
1740 lttng_ht_lookup(relay_connections_ht
,
1741 (void *)((unsigned long) pollfd
),
1743 node
= lttng_ht_iter_get_node_ulong(&iter
);
1745 DBG2("Relay sock %d not found", pollfd
);
1749 relay_connection
= caa_container_of(node
,
1750 struct relay_command
, sock_n
);
1752 if (revents
& (LPOLLERR
)) {
1754 relay_cleanup_poll_connection(&events
, pollfd
);
1755 relay_del_connection(relay_connections_ht
,
1758 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1759 DBG("Socket %d hung up", pollfd
);
1760 relay_cleanup_poll_connection(&events
, pollfd
);
1761 relay_del_connection(relay_connections_ht
,
1764 } else if (revents
& LPOLLIN
) {
1765 /* control socket */
1766 if (relay_connection
->type
== RELAY_CONTROL
) {
1767 ret
= relay_connection
->sock
->ops
->recvmsg(
1768 relay_connection
->sock
, &recv_hdr
,
1769 sizeof(struct lttcomm_relayd_hdr
), 0);
1770 /* connection closed */
1772 relay_cleanup_poll_connection(&events
, pollfd
);
1773 relay_del_connection(relay_connections_ht
,
1776 DBG("Control connection closed with %d", pollfd
);
1778 if (relay_connection
->session
) {
1779 DBG2("Relay worker receiving data for session : %" PRIu64
,
1780 relay_connection
->session
->id
);
1782 ret
= relay_process_control(&recv_hdr
,
1786 * there was an error in processing a control
1787 * command: clear the session
1790 relay_cleanup_poll_connection(&events
, pollfd
);
1791 relay_del_connection(relay_connections_ht
,
1794 DBG("Connection closed with %d", pollfd
);
1798 } else if (relay_connection
->type
== RELAY_DATA
) {
1799 ret
= relay_process_data(relay_connection
, streams_ht
);
1800 /* connection closed */
1802 relay_cleanup_poll_connection(&events
, pollfd
);
1803 relay_del_connection(relay_connections_ht
,
1806 DBG("Data connection closed with %d", pollfd
);
1817 lttng_poll_clean(&events
);
1819 /* empty the hash table and free the memory */
1821 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1822 node
= lttng_ht_iter_get_node_ulong(&iter
);
1824 relay_connection
= caa_container_of(node
,
1825 struct relay_command
, sock_n
);
1826 relay_del_connection(relay_connections_ht
,
1833 lttng_ht_destroy(streams_ht
);
1835 lttng_ht_destroy(relay_connections_ht
);
1836 relay_connections_ht_error
:
1837 /* Close relay cmd pipes */
1838 utils_close_pipe(relay_cmd_pipe
);
1840 DBG("Thread exited with error");
1842 DBG("Worker thread cleanup complete");
1845 rcu_unregister_thread();
1850 * Create the relay command pipe to wake thread_manage_apps.
1851 * Closed in cleanup().
1853 static int create_relay_cmd_pipe(void)
1857 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
1865 int main(int argc
, char **argv
)
1870 /* Create thread quit pipe */
1871 if ((ret
= init_thread_quit_pipe()) < 0) {
1875 /* Parse arguments */
1877 if ((ret
= parse_args(argc
, argv
) < 0)) {
1881 if ((ret
= set_signal_handler()) < 0) {
1894 /* Check if daemon is UID = 0 */
1895 is_root
= !getuid();
1898 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1899 ERR("Need to be root to use ports < 1024");
1905 /* Setup the thread apps communication pipe. */
1906 if ((ret
= create_relay_cmd_pipe()) < 0) {
1910 /* Init relay command queue. */
1911 cds_wfq_init(&relay_cmd_queue
.queue
);
1913 /* Set up max poll set size */
1914 lttng_poll_set_max_size();
1916 /* Setup the dispatcher thread */
1917 ret
= pthread_create(&dispatcher_thread
, NULL
,
1918 relay_thread_dispatcher
, (void *) NULL
);
1920 PERROR("pthread_create dispatcher");
1921 goto exit_dispatcher
;
1924 /* Setup the worker thread */
1925 ret
= pthread_create(&worker_thread
, NULL
,
1926 relay_thread_worker
, (void *) NULL
);
1928 PERROR("pthread_create worker");
1932 /* Setup the listener thread */
1933 ret
= pthread_create(&listener_thread
, NULL
,
1934 relay_thread_listener
, (void *) NULL
);
1936 PERROR("pthread_create listener");
1941 ret
= pthread_join(listener_thread
, &status
);
1943 PERROR("pthread_join");
1944 goto error
; /* join error, exit without cleanup */
1948 ret
= pthread_join(worker_thread
, &status
);
1950 PERROR("pthread_join");
1951 goto error
; /* join error, exit without cleanup */
1955 ret
= pthread_join(dispatcher_thread
, &status
);
1957 PERROR("pthread_join");
1958 goto error
; /* join error, exit without cleanup */