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
);
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
;
476 * Get allocated in this thread, enqueued to a global queue, dequeued and
477 * freed in the worker thread.
479 struct relay_command
*relay_cmd
= NULL
;
481 DBG("[thread] Relay listener started");
483 control_sock
= relay_init_sock(control_uri
);
485 goto error_sock_control
;
488 data_sock
= relay_init_sock(data_uri
);
490 goto error_sock_relay
;
494 * Pass 3 as size here for the thread quit pipe, control and data socket.
496 ret
= create_thread_poll_set(&events
, 3);
498 goto error_create_poll
;
501 /* Add the control socket */
502 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
507 /* Add the data socket */
508 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
514 DBG("Listener accepting connections");
516 nb_fd
= LTTNG_POLL_GETNB(&events
);
519 ret
= lttng_poll_wait(&events
, -1);
522 * Restart interrupted system call.
524 if (errno
== EINTR
) {
530 DBG("Relay new connection received");
531 for (i
= 0; i
< nb_fd
; i
++) {
532 /* Fetch once the poll data */
533 revents
= LTTNG_POLL_GETEV(&events
, i
);
534 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
536 /* Thread quit pipe has been closed. Killing thread. */
537 ret
= check_thread_quit_pipe(pollfd
, revents
);
543 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
544 ERR("socket poll error");
546 } else if (revents
& LPOLLIN
) {
547 struct lttcomm_sock
*newsock
= NULL
;
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");
561 relay_cmd
->type
= RELAY_DATA
;
562 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
563 } else if (pollfd
== control_sock
->fd
) {
564 newsock
= control_sock
->ops
->accept(control_sock
);
566 PERROR("accepting control sock");
569 relay_cmd
->type
= RELAY_CONTROL
;
570 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
572 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
575 PERROR("setsockopt inet");
578 relay_cmd
->sock
= newsock
;
580 * Lock free enqueue the request.
582 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
585 * Wake the dispatch queue futex. Implicit memory
586 * barrier with the exchange in cds_wfq_enqueue.
588 futex_nto1_wake(&relay_cmd_queue
.futex
);
596 lttng_poll_clean(&events
);
598 if (data_sock
->fd
>= 0) {
599 ret
= data_sock
->ops
->close(data_sock
);
604 lttcomm_destroy_sock(data_sock
);
606 if (control_sock
->fd
>= 0) {
607 ret
= control_sock
->ops
->close(control_sock
);
612 lttcomm_destroy_sock(control_sock
);
615 DBG("Thread exited with error");
617 DBG("Relay listener thread cleanup complete");
623 * This thread manages the dispatching of the requests to worker threads
626 void *relay_thread_dispatcher(void *data
)
629 struct cds_wfq_node
*node
;
630 struct relay_command
*relay_cmd
= NULL
;
632 DBG("[thread] Relay dispatcher started");
634 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
635 /* Atomically prepare the queue futex */
636 futex_nto1_prepare(&relay_cmd_queue
.futex
);
639 /* Dequeue commands */
640 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
642 DBG("Woken up but nothing in the relay command queue");
643 /* Continue thread execution */
647 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
648 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
651 * Inform worker thread of the new request. This
652 * call is blocking so we can be assured that the data will be read
653 * at some point in time or wait to the end of the world :)
656 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
657 sizeof(struct relay_command
));
658 } while (ret
< 0 && errno
== EINTR
);
661 PERROR("write cmd pipe");
664 } while (node
!= NULL
);
666 /* Futex wait on queue. Blocking call on futex() */
667 futex_nto1_wait(&relay_cmd_queue
.futex
);
671 DBG("Dispatch thread dying");
677 * Return the realpath(3) of the path even if the last directory token does not
678 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
679 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
680 * fails if the end point directory does not exist.
683 char *expand_full_path(const char *path
)
685 const char *end_path
= path
;
686 char *next
, *cut_path
, *expanded_path
, *respath
;
688 /* Find last token delimited by '/' */
689 while ((next
= strpbrk(end_path
+ 1, "/"))) {
693 /* Cut last token from original path */
694 cut_path
= strndup(path
, end_path
- path
);
696 expanded_path
= malloc(PATH_MAX
);
697 if (expanded_path
== NULL
) {
702 respath
= realpath(cut_path
, expanded_path
);
703 if (respath
== NULL
) {
706 ERR("%s: No such file or directory", cut_path
);
714 /* Add end part to expanded path */
715 strcat(respath
, end_path
);
724 * config_get_default_path
726 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
729 char *config_get_default_path(void)
731 return getenv("HOME");
735 * Create recursively directory using the FULL path.
738 int mkdir_recursive(char *path
, mode_t mode
)
740 char *p
, tmp
[PATH_MAX
];
745 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
747 PERROR("snprintf mkdir");
752 if (tmp
[len
- 1] == '/') {
756 for (p
= tmp
+ 1; *p
; p
++) {
759 if (tmp
[strlen(tmp
) - 1] == '.' &&
760 tmp
[strlen(tmp
) - 2] == '.' &&
761 tmp
[strlen(tmp
) - 3] == '/') {
762 ERR("Using '/../' is not permitted in the trace path (%s)",
767 ret
= stat(tmp
, &statbuf
);
769 ret
= mkdir(tmp
, mode
);
771 if (errno
!= EEXIST
) {
772 PERROR("mkdir recursive");
782 ret
= mkdir(tmp
, mode
);
784 if (errno
!= EEXIST
) {
785 PERROR("mkdir recursive last piece");
797 char *create_output_path_auto(char *path_name
)
800 char *traces_path
= NULL
;
801 char *alloc_path
= NULL
;
804 default_path
= config_get_default_path();
805 if (default_path
== NULL
) {
806 ERR("Home path not found.\n \
807 Please specify an output path using -o, --output PATH");
810 alloc_path
= strdup(default_path
);
811 if (alloc_path
== NULL
) {
812 PERROR("Path allocation");
815 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
816 "/%s", alloc_path
, path_name
);
818 PERROR("asprintf trace dir name");
827 char *create_output_path_noauto(char *path_name
)
830 char *traces_path
= NULL
;
833 full_path
= expand_full_path(opt_output_path
);
834 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
836 PERROR("asprintf trace dir name");
845 * create_output_path: create the output trace directory
848 char *create_output_path(char *path_name
)
850 if (opt_output_path
== NULL
) {
851 return create_output_path_auto(path_name
);
853 return create_output_path_noauto(path_name
);
858 void deferred_free_stream(struct rcu_head
*head
)
860 struct relay_stream
*stream
=
861 caa_container_of(head
, struct relay_stream
, rcu_node
);
866 * relay_delete_session: Free all memory associated with a session and
870 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
872 struct lttng_ht_iter iter
;
873 struct lttng_ht_node_ulong
*node
;
874 struct relay_stream
*stream
;
881 DBG("Relay deleting session %" PRIu64
, cmd
->session
->id
);
882 free(cmd
->session
->sock
);
885 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
886 node
= lttng_ht_iter_get_node_ulong(&iter
);
888 stream
= caa_container_of(node
,
889 struct relay_stream
, stream_n
);
890 if (stream
->session
== cmd
->session
) {
892 ret
= lttng_ht_del(streams_ht
, &iter
);
894 call_rcu(&stream
->rcu_node
,
895 deferred_free_stream
);
903 * relay_add_stream: allocate a new stream for a session
906 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
907 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
909 struct relay_session
*session
= cmd
->session
;
910 struct lttcomm_relayd_add_stream stream_info
;
911 struct relay_stream
*stream
= NULL
;
912 struct lttcomm_relayd_status_stream reply
;
913 char *path
= NULL
, *root_path
= NULL
;
916 if (!session
|| session
->version_check_done
== 0) {
917 ERR("Trying to add a stream before version check");
922 /* FIXME : use data_size for something ? */
923 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
924 sizeof(struct lttcomm_relayd_add_stream
), MSG_WAITALL
);
925 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
926 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
930 stream
= zmalloc(sizeof(struct relay_stream
));
931 if (stream
== NULL
) {
932 PERROR("relay stream zmalloc");
938 stream
->stream_handle
= ++last_relay_stream_id
;
939 stream
->prev_seq
= -1ULL;
940 stream
->session
= session
;
942 root_path
= create_output_path(stream_info
.pathname
);
947 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
949 ERR("relay creating output directory");
953 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
955 PERROR("asprintf stream path");
959 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
961 PERROR("Relay creating trace file");
966 DBG("Tracefile %s created", path
);
968 lttng_ht_node_init_ulong(&stream
->stream_n
,
969 (unsigned long) stream
->stream_handle
);
970 lttng_ht_add_unique_ulong(streams_ht
,
973 DBG("Relay new stream added %s", stream_info
.channel_name
);
978 /* send the session id to the client or a negative return code on error */
980 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
982 reply
.ret_code
= htobe32(LTTNG_OK
);
984 reply
.handle
= htobe64(stream
->stream_handle
);
985 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
986 sizeof(struct lttcomm_relayd_status_stream
), 0);
988 ERR("Relay sending stream id");
997 * relay_close_stream: close a specific stream
1000 int relay_close_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
1001 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1003 struct relay_session
*session
= cmd
->session
;
1004 struct lttcomm_relayd_close_stream stream_info
;
1005 struct lttcomm_relayd_generic_reply reply
;
1006 struct relay_stream
*stream
;
1008 struct lttng_ht_node_ulong
*node
;
1009 struct lttng_ht_iter iter
;
1011 DBG("Close stream received");
1013 if (!session
|| session
->version_check_done
== 0) {
1014 ERR("Trying to close a stream before version check");
1016 goto end_no_session
;
1019 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
1020 sizeof(struct lttcomm_relayd_close_stream
), MSG_WAITALL
);
1021 if (ret
< sizeof(struct lttcomm_relayd_close_stream
)) {
1022 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
1024 goto end_no_session
;
1028 lttng_ht_lookup(streams_ht
,
1029 (void *)((unsigned long) be64toh(stream_info
.stream_id
)),
1031 node
= lttng_ht_iter_get_node_ulong(&iter
);
1033 DBG("Relay stream %" PRIu64
" not found", be64toh(stream_info
.stream_id
));
1038 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
1044 stream
->close_flag
= 1;
1046 if (close_stream_check(stream
)) {
1050 delret
= lttng_ht_del(streams_ht
, &iter
);
1052 call_rcu(&stream
->rcu_node
,
1053 deferred_free_stream
);
1054 DBG("Closed tracefile %d from close stream", stream
->fd
);
1061 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1063 reply
.ret_code
= htobe32(LTTNG_OK
);
1065 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1066 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1068 ERR("Relay sending stream id");
1076 * relay_unknown_command: send -1 if received unknown command
1079 void relay_unknown_command(struct relay_command
*cmd
)
1081 struct lttcomm_relayd_generic_reply reply
;
1084 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1085 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1086 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1088 ERR("Relay sending unknown command");
1093 * relay_start: send an acknowledgment to the client to tell if we are
1094 * ready to receive data. We are ready if a session is established.
1097 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
1098 struct relay_command
*cmd
)
1100 int ret
= htobe32(LTTNG_OK
);
1101 struct lttcomm_relayd_generic_reply reply
;
1102 struct relay_session
*session
= cmd
->session
;
1105 DBG("Trying to start the streaming without a session established");
1106 ret
= htobe32(LTTNG_ERR_UNK
);
1109 reply
.ret_code
= ret
;
1110 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1111 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1113 ERR("Relay sending start ack");
1120 * Get stream from stream id.
1121 * Need to be called with RCU read-side lock held.
1124 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
1125 struct lttng_ht
*streams_ht
)
1127 struct lttng_ht_node_ulong
*node
;
1128 struct lttng_ht_iter iter
;
1129 struct relay_stream
*ret
;
1131 lttng_ht_lookup(streams_ht
,
1132 (void *)((unsigned long) stream_id
),
1134 node
= lttng_ht_iter_get_node_ulong(&iter
);
1136 DBG("Relay stream %" PRIu64
" not found", stream_id
);
1141 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1148 * Append padding to the file pointed by the file descriptor fd.
1150 static int write_padding_to_file(int fd
, uint32_t size
)
1159 zeros
= zmalloc(size
);
1160 if (zeros
== NULL
) {
1161 PERROR("zmalloc zeros for padding");
1167 ret
= write(fd
, zeros
, size
);
1168 } while (ret
< 0 && errno
== EINTR
);
1170 PERROR("write padding to file");
1178 * relay_recv_metadata: receive the metada for the session.
1181 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1182 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1184 int ret
= htobe32(LTTNG_OK
);
1185 struct relay_session
*session
= cmd
->session
;
1186 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1187 struct relay_stream
*metadata_stream
;
1188 uint64_t data_size
, payload_size
;
1191 ERR("Metadata sent before version check");
1196 data_size
= payload_size
= be64toh(recv_hdr
->data_size
);
1197 if (data_size
< sizeof(struct lttcomm_relayd_metadata_payload
)) {
1198 ERR("Incorrect data size");
1202 payload_size
-= sizeof(struct lttcomm_relayd_metadata_payload
);
1204 if (data_buffer_size
< data_size
) {
1205 data_buffer
= realloc(data_buffer
, data_size
);
1207 ERR("Allocating data buffer");
1211 data_buffer_size
= data_size
;
1213 memset(data_buffer
, 0, data_size
);
1214 DBG2("Relay receiving metadata, waiting for %" PRIu64
" bytes", data_size
);
1215 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
,
1217 if (ret
< 0 || ret
!= data_size
) {
1219 ERR("Relay didn't receive the whole metadata");
1222 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1225 metadata_stream
= relay_stream_from_stream_id(
1226 be64toh(metadata_struct
->stream_id
), streams_ht
);
1227 if (!metadata_stream
) {
1233 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1235 } while (ret
< 0 && errno
== EINTR
);
1236 if (ret
< payload_size
) {
1237 ERR("Relay error writing metadata on file");
1242 ret
= write_padding_to_file(metadata_stream
->fd
,
1243 be32toh(metadata_struct
->padding_size
));
1248 DBG2("Relay metadata written");
1257 * relay_send_version: send relayd version number
1260 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1261 struct relay_command
*cmd
)
1263 int ret
= htobe32(LTTNG_OK
);
1264 struct lttcomm_relayd_version reply
;
1265 struct relay_session
*session
= NULL
;
1267 if (cmd
->session
== NULL
) {
1268 session
= zmalloc(sizeof(struct relay_session
));
1269 if (session
== NULL
) {
1270 PERROR("relay session zmalloc");
1274 session
->id
= ++last_relay_session_id
;
1275 DBG("Created session %" PRIu64
, session
->id
);
1276 cmd
->session
= session
;
1278 session
->version_check_done
= 1;
1280 sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1281 reply
.major
= htobe32(reply
.major
);
1282 reply
.minor
= htobe32(reply
.minor
);
1283 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1284 sizeof(struct lttcomm_relayd_version
), 0);
1286 ERR("Relay sending version");
1288 DBG("Version check done (%u.%u)", be32toh(reply
.major
),
1289 be32toh(reply
.minor
));
1296 * relay_process_control: Process the commands received on the control socket
1299 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1300 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1304 switch (be32toh(recv_hdr
->cmd
)) {
1306 case RELAYD_CREATE_SESSION:
1307 ret = relay_create_session(recv_hdr, cmd);
1310 case RELAYD_ADD_STREAM
:
1311 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1313 case RELAYD_START_DATA
:
1314 ret
= relay_start(recv_hdr
, cmd
);
1316 case RELAYD_SEND_METADATA
:
1317 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1319 case RELAYD_VERSION
:
1320 ret
= relay_send_version(recv_hdr
, cmd
);
1322 case RELAYD_CLOSE_STREAM
:
1323 ret
= relay_close_stream(recv_hdr
, cmd
, streams_ht
);
1325 case RELAYD_UPDATE_SYNC_INFO
:
1327 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1328 relay_unknown_command(cmd
);
1338 * relay_process_data: Process the data received on the data socket
1341 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1344 struct relay_stream
*stream
;
1345 struct lttcomm_relayd_data_hdr data_hdr
;
1347 uint64_t net_seq_num
;
1350 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1351 sizeof(struct lttcomm_relayd_data_hdr
), MSG_WAITALL
);
1353 ERR("Connections seems to be closed");
1358 stream_id
= be64toh(data_hdr
.stream_id
);
1361 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1367 data_size
= be32toh(data_hdr
.data_size
);
1368 if (data_buffer_size
< data_size
) {
1369 data_buffer
= realloc(data_buffer
, data_size
);
1371 ERR("Allocating data buffer");
1375 data_buffer_size
= data_size
;
1377 memset(data_buffer
, 0, data_size
);
1379 net_seq_num
= be64toh(data_hdr
.net_seq_num
);
1381 DBG3("Receiving data of size %u for stream id %" PRIu64
" seqnum %" PRIu64
,
1382 data_size
, stream_id
, net_seq_num
);
1383 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1390 ret
= write(stream
->fd
, data_buffer
, data_size
);
1391 } while (ret
< 0 && errno
== EINTR
);
1392 if (ret
< data_size
) {
1393 ERR("Relay error writing data to file");
1398 ret
= write_padding_to_file(stream
->fd
, be32toh(data_hdr
.padding_size
));
1403 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64
,
1404 ret
, stream
->stream_handle
);
1406 stream
->prev_seq
= net_seq_num
;
1408 /* Check if we need to close the FD */
1409 if (close_stream_check(stream
)) {
1410 struct lttng_ht_iter iter
;
1413 iter
.iter
.node
= &stream
->stream_n
.node
;
1414 ret
= lttng_ht_del(streams_ht
, &iter
);
1416 call_rcu(&stream
->rcu_node
,
1417 deferred_free_stream
);
1418 DBG("Closed tracefile %d after recv data", stream
->fd
);
1428 void relay_cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1432 lttng_poll_del(events
, pollfd
);
1434 ret
= close(pollfd
);
1436 ERR("Closing pollfd %d", pollfd
);
1441 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1442 struct lttng_ht
*relay_connections_ht
)
1444 struct relay_command
*relay_connection
;
1447 relay_connection
= zmalloc(sizeof(struct relay_command
));
1448 if (relay_connection
== NULL
) {
1449 PERROR("Relay command zmalloc");
1452 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1453 if (ret
< 0 || ret
< sizeof(struct relay_command
)) {
1454 PERROR("read relay cmd pipe");
1458 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1459 (unsigned long) relay_connection
->sock
->fd
);
1461 lttng_ht_add_unique_ulong(relay_connections_ht
,
1462 &relay_connection
->sock_n
);
1464 return lttng_poll_add(events
,
1465 relay_connection
->sock
->fd
,
1466 LPOLLIN
| LPOLLRDHUP
);
1469 free(relay_connection
);
1475 void deferred_free_connection(struct rcu_head
*head
)
1477 struct relay_command
*relay_connection
=
1478 caa_container_of(head
, struct relay_command
, rcu_node
);
1479 free(relay_connection
);
1483 void relay_del_connection(struct lttng_ht
*relay_connections_ht
,
1484 struct lttng_ht
*streams_ht
, struct lttng_ht_iter
*iter
,
1485 struct relay_command
*relay_connection
)
1489 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1491 if (relay_connection
->type
== RELAY_CONTROL
) {
1492 relay_delete_session(relay_connection
, streams_ht
);
1494 call_rcu(&relay_connection
->rcu_node
,
1495 deferred_free_connection
);
1499 * This thread does the actual work
1502 void *relay_thread_worker(void *data
)
1504 int i
, ret
, pollfd
, err
= -1;
1505 uint32_t revents
, nb_fd
;
1506 struct relay_command
*relay_connection
;
1507 struct lttng_poll_event events
;
1508 struct lttng_ht
*relay_connections_ht
;
1509 struct lttng_ht_node_ulong
*node
;
1510 struct lttng_ht_iter iter
;
1511 struct lttng_ht
*streams_ht
;
1512 struct lttcomm_relayd_hdr recv_hdr
;
1514 DBG("[thread] Relay worker started");
1516 rcu_register_thread();
1518 /* table of connections indexed on socket */
1519 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1520 if (!relay_connections_ht
) {
1521 goto relay_connections_ht_error
;
1524 /* tables of streams indexed by stream ID */
1525 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1527 goto streams_ht_error
;
1530 ret
= create_thread_poll_set(&events
, 2);
1532 goto error_poll_create
;
1535 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1541 /* Zeroed the events structure */
1542 lttng_poll_reset(&events
);
1544 nb_fd
= LTTNG_POLL_GETNB(&events
);
1546 /* Infinite blocking call, waiting for transmission */
1548 DBG3("Relayd worker thread polling...");
1549 ret
= lttng_poll_wait(&events
, -1);
1552 * Restart interrupted system call.
1554 if (errno
== EINTR
) {
1560 for (i
= 0; i
< nb_fd
; i
++) {
1561 /* Fetch once the poll data */
1562 revents
= LTTNG_POLL_GETEV(&events
, i
);
1563 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1565 /* Thread quit pipe has been closed. Killing thread. */
1566 ret
= check_thread_quit_pipe(pollfd
, revents
);
1572 /* Inspect the relay cmd pipe for new connection */
1573 if (pollfd
== relay_cmd_pipe
[0]) {
1574 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1575 ERR("Relay pipe error");
1577 } else if (revents
& LPOLLIN
) {
1578 DBG("Relay command received");
1579 ret
= relay_add_connection(relay_cmd_pipe
[0],
1580 &events
, relay_connections_ht
);
1585 } else if (revents
> 0) {
1587 lttng_ht_lookup(relay_connections_ht
,
1588 (void *)((unsigned long) pollfd
),
1590 node
= lttng_ht_iter_get_node_ulong(&iter
);
1592 DBG2("Relay sock %d not found", pollfd
);
1596 relay_connection
= caa_container_of(node
,
1597 struct relay_command
, sock_n
);
1599 if (revents
& (LPOLLERR
)) {
1601 relay_cleanup_poll_connection(&events
, pollfd
);
1602 relay_del_connection(relay_connections_ht
,
1605 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1606 DBG("Socket %d hung up", pollfd
);
1607 relay_cleanup_poll_connection(&events
, pollfd
);
1608 relay_del_connection(relay_connections_ht
,
1611 } else if (revents
& LPOLLIN
) {
1612 /* control socket */
1613 if (relay_connection
->type
== RELAY_CONTROL
) {
1614 ret
= relay_connection
->sock
->ops
->recvmsg(
1615 relay_connection
->sock
, &recv_hdr
,
1616 sizeof(struct lttcomm_relayd_hdr
), MSG_WAITALL
);
1617 /* connection closed */
1619 relay_cleanup_poll_connection(&events
, pollfd
);
1620 relay_del_connection(relay_connections_ht
,
1623 DBG("Control connection closed with %d", pollfd
);
1625 if (relay_connection
->session
) {
1626 DBG2("Relay worker receiving data for session : %" PRIu64
,
1627 relay_connection
->session
->id
);
1629 ret
= relay_process_control(&recv_hdr
,
1633 * there was an error in processing a control
1634 * command: clear the session
1637 relay_cleanup_poll_connection(&events
, pollfd
);
1638 relay_del_connection(relay_connections_ht
,
1641 DBG("Connection closed with %d", pollfd
);
1645 } else if (relay_connection
->type
== RELAY_DATA
) {
1646 ret
= relay_process_data(relay_connection
, streams_ht
);
1647 /* connection closed */
1649 relay_cleanup_poll_connection(&events
, pollfd
);
1650 relay_del_connection(relay_connections_ht
,
1653 DBG("Data connection closed with %d", pollfd
);
1664 lttng_poll_clean(&events
);
1666 /* empty the hash table and free the memory */
1668 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1669 node
= lttng_ht_iter_get_node_ulong(&iter
);
1671 relay_connection
= caa_container_of(node
,
1672 struct relay_command
, sock_n
);
1673 relay_del_connection(relay_connections_ht
,
1680 lttng_ht_destroy(streams_ht
);
1682 lttng_ht_destroy(relay_connections_ht
);
1683 relay_connections_ht_error
:
1685 DBG("Thread exited with error");
1687 DBG("Worker thread cleanup complete");
1690 rcu_unregister_thread();
1695 * Create the relay command pipe to wake thread_manage_apps.
1696 * Closed in cleanup().
1698 static int create_relay_cmd_pipe(void)
1702 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
1710 int main(int argc
, char **argv
)
1715 /* Create thread quit pipe */
1716 if ((ret
= init_thread_quit_pipe()) < 0) {
1720 /* Parse arguments */
1722 if ((ret
= parse_args(argc
, argv
) < 0)) {
1726 if ((ret
= set_signal_handler()) < 0) {
1739 /* Check if daemon is UID = 0 */
1740 is_root
= !getuid();
1743 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1744 ERR("Need to be root to use ports < 1024");
1750 /* Setup the thread apps communication pipe. */
1751 if ((ret
= create_relay_cmd_pipe()) < 0) {
1755 /* Init relay command queue. */
1756 cds_wfq_init(&relay_cmd_queue
.queue
);
1758 /* Set up max poll set size */
1759 lttng_poll_set_max_size();
1761 /* Setup the dispatcher thread */
1762 ret
= pthread_create(&dispatcher_thread
, NULL
,
1763 relay_thread_dispatcher
, (void *) NULL
);
1765 PERROR("pthread_create dispatcher");
1766 goto exit_dispatcher
;
1769 /* Setup the worker thread */
1770 ret
= pthread_create(&worker_thread
, NULL
,
1771 relay_thread_worker
, (void *) NULL
);
1773 PERROR("pthread_create worker");
1777 /* Setup the listener thread */
1778 ret
= pthread_create(&listener_thread
, NULL
,
1779 relay_thread_listener
, (void *) NULL
);
1781 PERROR("pthread_create listener");
1786 ret
= pthread_join(listener_thread
, &status
);
1788 PERROR("pthread_join");
1789 goto error
; /* join error, exit without cleanup */
1793 ret
= pthread_join(worker_thread
, &status
);
1795 PERROR("pthread_join");
1796 goto error
; /* join error, exit without cleanup */
1800 ret
= pthread_join(dispatcher_thread
, &status
);
1802 PERROR("pthread_join");
1803 goto error
; /* join error, exit without cleanup */