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>
35 #include <urcu/futex.h>
36 #include <urcu/uatomic.h>
41 #include <lttng/lttng.h>
42 #include <common/common.h>
43 #include <common/compat/poll.h>
44 #include <common/compat/socket.h>
45 #include <common/defaults.h>
46 #include <common/futex.h>
47 #include <common/sessiond-comm/sessiond-comm.h>
48 #include <common/sessiond-comm/inet.h>
49 #include <common/hashtable/hashtable.h>
50 #include <common/sessiond-comm/relayd.h>
51 #include <common/uri.h>
53 #include "lttng-relayd.h"
55 /* command line options */
56 static int opt_daemon
;
57 static char *opt_output_path
;
58 static struct lttng_uri
*control_uri
= NULL
;
59 static struct lttng_uri
*data_uri
= NULL
;
62 static int is_root
; /* Set to 1 if the daemon is running as root */
65 * Quit pipe for all threads. This permits a single cancellation point
66 * for all threads when receiving an event on the pipe.
68 static int thread_quit_pipe
[2] = { -1, -1 };
71 * This pipe is used to inform the worker thread that a command is queued and
72 * ready to be processed.
74 static int relay_cmd_pipe
[2] = { -1, -1 };
76 static int dispatch_thread_exit
;
78 static pthread_t listener_thread
;
79 static pthread_t dispatcher_thread
;
80 static pthread_t worker_thread
;
82 static uint64_t last_relay_stream_id
= 0;
83 static uint64_t last_relay_session_id
= 0;
86 * Relay command queue.
88 * The relay_thread_listener and relay_thread_dispatcher communicate with this
91 static struct relay_cmd_queue relay_cmd_queue
;
93 /* buffer allocated at startup, used to store the trace data */
94 static char *data_buffer
= NULL
;
95 static unsigned int data_buffer_size
= 0;
98 * usage function on stderr
103 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
104 fprintf(stderr
, " -h, --help Display this usage.\n");
105 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
106 fprintf(stderr
, " -C, --control-port Control port listening (URI)\n");
107 fprintf(stderr
, " -D, --data-port Data port listening (URI)\n");
108 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
112 int parse_args(int argc
, char **argv
)
116 char *default_address
;
118 static struct option long_options
[] = {
119 { "control-port", 1, 0, 'C' },
120 { "data-port", 1, 0, 'D' },
121 { "daemonize", 0, 0, 'd' },
122 { "output", 1, 0, 'o' },
123 { "verbose", 0, 0, 'v' },
128 int option_index
= 0;
129 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
130 long_options
, &option_index
);
137 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
139 fprintf(stderr
, " with arg %s\n", optarg
);
143 ret
= uri_parse(optarg
, &control_uri
);
145 ERR("Invalid control URI specified");
148 if (control_uri
->port
== 0) {
149 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
153 ret
= uri_parse(optarg
, &data_uri
);
155 ERR("Invalid data URI specified");
158 if (data_uri
->port
== 0) {
159 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
169 ret
= asprintf(&opt_output_path
, "%s", optarg
);
171 PERROR("asprintf opt_output_path");
176 /* Verbose level can increase using multiple -v */
177 lttng_opt_verbose
+= 1;
180 /* Unknown option or other error.
181 * Error is printed by getopt, just return */
187 /* assign default values */
188 if (control_uri
== NULL
) {
189 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
190 DEFAULT_NETWORK_CONTROL_PORT
);
192 PERROR("asprintf default data address");
196 ret
= uri_parse(default_address
, &control_uri
);
197 free(default_address
);
199 ERR("Invalid control URI specified");
203 if (data_uri
== NULL
) {
204 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
205 DEFAULT_NETWORK_DATA_PORT
);
207 PERROR("asprintf default data address");
211 ret
= uri_parse(default_address
, &data_uri
);
212 free(default_address
);
214 ERR("Invalid data URI specified");
233 for (i
= 0; i
< 2; i
++) {
234 if (thread_quit_pipe
[i
] >= 0) {
235 ret
= close(thread_quit_pipe
[i
]);
244 * Write to writable pipe used to notify a thread.
247 int notify_thread_pipe(int wpipe
)
251 ret
= write(wpipe
, "!", 1);
253 PERROR("write poll pipe");
260 * Stop all threads by closing the thread quit pipe.
263 void stop_threads(void)
267 /* Stopping all threads */
268 DBG("Terminating all threads");
269 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
271 ERR("write error on thread quit pipe");
274 /* Dispatch thread */
275 dispatch_thread_exit
= 1;
276 futex_nto1_wake(&relay_cmd_queue
.futex
);
280 * Signal handler for the daemon
282 * Simply stop all worker threads, leaving main() return gracefully after
283 * joining all threads and calling cleanup().
286 void sighandler(int sig
)
290 DBG("SIGPIPE caught");
293 DBG("SIGINT caught");
297 DBG("SIGTERM caught");
306 * Setup signal handler for :
307 * SIGINT, SIGTERM, SIGPIPE
310 int set_signal_handler(void)
316 if ((ret
= sigemptyset(&sigset
)) < 0) {
317 PERROR("sigemptyset");
321 sa
.sa_handler
= sighandler
;
324 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
329 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
334 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
339 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
345 * Init thread quit pipe.
347 * Return -1 on error or 0 if all pipes are created.
350 int init_thread_quit_pipe(void)
354 ret
= pipe(thread_quit_pipe
);
356 PERROR("thread quit pipe");
360 for (i
= 0; i
< 2; i
++) {
361 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
373 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
376 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
380 if (events
== NULL
|| size
== 0) {
385 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
391 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
403 * Check if the thread quit pipe was triggered.
405 * Return 1 if it was triggered else 0;
408 int check_thread_quit_pipe(int fd
, uint32_t events
)
410 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
418 * Create and init socket from uri.
421 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
424 struct lttcomm_sock
*sock
= NULL
;
426 sock
= lttcomm_alloc_sock_from_uri(uri
);
428 ERR("Allocating socket");
432 ret
= lttcomm_create_sock(sock
);
436 DBG("Listening on sock %d", sock
->fd
);
438 ret
= sock
->ops
->bind(sock
);
443 ret
= sock
->ops
->listen(sock
, -1);
453 lttcomm_destroy_sock(sock
);
459 * This thread manages the listening for new connections on the network
462 void *relay_thread_listener(void *data
)
466 uint32_t revents
, nb_fd
;
467 struct lttng_poll_event events
;
468 struct lttcomm_sock
*control_sock
, *data_sock
;
471 * Get allocated in this thread, enqueued to a global queue, dequeued and
472 * freed in the worker thread.
474 struct relay_command
*relay_cmd
= NULL
;
476 DBG("[thread] Relay listener started");
478 control_sock
= relay_init_sock(control_uri
);
483 data_sock
= relay_init_sock(data_uri
);
489 * Pass 3 as size here for the thread quit pipe, control and data socket.
491 ret
= create_thread_poll_set(&events
, 3);
493 goto error_create_poll
;
496 /* Add the control socket */
497 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
502 /* Add the data socket */
503 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
509 DBG("Listener accepting connections");
511 nb_fd
= LTTNG_POLL_GETNB(&events
);
514 ret
= lttng_poll_wait(&events
, -1);
517 * Restart interrupted system call.
519 if (errno
== EINTR
) {
525 DBG("Relay new connection received");
526 for (i
= 0; i
< nb_fd
; i
++) {
527 /* Fetch once the poll data */
528 revents
= LTTNG_POLL_GETEV(&events
, i
);
529 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
531 /* Thread quit pipe has been closed. Killing thread. */
532 ret
= check_thread_quit_pipe(pollfd
, revents
);
537 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
538 ERR("socket poll error");
540 } else if (revents
& LPOLLIN
) {
541 struct lttcomm_sock
*newsock
= NULL
;
543 relay_cmd
= zmalloc(sizeof(struct relay_command
));
544 if (relay_cmd
== NULL
) {
545 PERROR("relay command zmalloc");
549 if (pollfd
== data_sock
->fd
) {
550 newsock
= data_sock
->ops
->accept(data_sock
);
552 PERROR("accepting data sock");
555 relay_cmd
->type
= RELAY_DATA
;
556 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
557 } else if (pollfd
== control_sock
->fd
) {
558 newsock
= control_sock
->ops
->accept(control_sock
);
560 PERROR("accepting control sock");
563 relay_cmd
->type
= RELAY_CONTROL
;
564 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
566 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
569 PERROR("setsockopt inet");
572 relay_cmd
->sock
= newsock
;
574 * Lock free enqueue the request.
576 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
579 * Wake the dispatch queue futex. Implicit memory
580 * barrier with the exchange in cds_wfq_enqueue.
582 futex_nto1_wake(&relay_cmd_queue
.futex
);
589 lttng_poll_clean(&events
);
591 if (control_sock
->fd
>= 0) {
592 ret
= control_sock
->ops
->close(control_sock
);
596 lttcomm_destroy_sock(control_sock
);
598 if (data_sock
->fd
>= 0) {
599 ret
= data_sock
->ops
->close(data_sock
);
603 lttcomm_destroy_sock(data_sock
);
606 DBG("Relay listener thread cleanup complete");
613 * This thread manages the dispatching of the requests to worker threads
616 void *relay_thread_dispatcher(void *data
)
619 struct cds_wfq_node
*node
;
620 struct relay_command
*relay_cmd
= NULL
;
622 DBG("[thread] Relay dispatcher started");
624 while (!dispatch_thread_exit
) {
625 /* Atomically prepare the queue futex */
626 futex_nto1_prepare(&relay_cmd_queue
.futex
);
629 /* Dequeue commands */
630 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
632 DBG("Woken up but nothing in the relay command queue");
633 /* Continue thread execution */
637 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
638 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
641 * Inform worker thread of the new request. This
642 * call is blocking so we can be assured that the data will be read
643 * at some point in time or wait to the end of the world :)
645 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
646 sizeof(struct relay_command
));
649 PERROR("write cmd pipe");
652 } while (node
!= NULL
);
654 /* Futex wait on queue. Blocking call on futex() */
655 futex_nto1_wait(&relay_cmd_queue
.futex
);
659 DBG("Dispatch thread dying");
665 * Return the realpath(3) of the path even if the last directory token does not
666 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
667 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
668 * fails if the end point directory does not exist.
671 char *expand_full_path(const char *path
)
673 const char *end_path
= path
;
674 char *next
, *cut_path
, *expanded_path
;
676 /* Find last token delimited by '/' */
677 while ((next
= strpbrk(end_path
+ 1, "/"))) {
681 /* Cut last token from original path */
682 cut_path
= strndup(path
, end_path
- path
);
684 expanded_path
= malloc(PATH_MAX
);
685 if (expanded_path
== NULL
) {
689 expanded_path
= realpath((char *)cut_path
, expanded_path
);
690 if (expanded_path
== NULL
) {
693 ERR("%s: No such file or directory", cut_path
);
702 /* Add end part to expanded path */
703 strcat(expanded_path
, end_path
);
706 return expanded_path
;
715 * config_get_default_path
717 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
720 char *config_get_default_path(void)
722 return getenv("HOME");
726 * Create recursively directory using the FULL path.
729 int mkdir_recursive(char *path
, mode_t mode
)
731 char *p
, tmp
[PATH_MAX
];
736 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
738 PERROR("snprintf mkdir");
743 if (tmp
[len
- 1] == '/') {
747 for (p
= tmp
+ 1; *p
; p
++) {
750 if (tmp
[strlen(tmp
) - 1] == '.' &&
751 tmp
[strlen(tmp
) - 2] == '.' &&
752 tmp
[strlen(tmp
) - 3] == '/') {
753 ERR("Using '/../' is not permitted in the trace path (%s)",
758 ret
= stat(tmp
, &statbuf
);
760 ret
= mkdir(tmp
, mode
);
762 if (!(errno
== EEXIST
)) {
763 PERROR("mkdir recursive");
773 ret
= mkdir(tmp
, mode
);
775 if (!(errno
== EEXIST
)) {
776 PERROR("mkdir recursive last piece");
788 * create_output_path: create the output trace directory
791 char *create_output_path(char *path_name
)
794 char *alloc_path
= NULL
;
795 char *traces_path
= NULL
;
796 char *full_path
= NULL
;
798 /* Auto output path */
799 if (opt_output_path
== NULL
) {
800 alloc_path
= strdup(config_get_default_path());
801 if (alloc_path
== NULL
) {
802 ERR("Home path not found.\n \
803 Please specify an output path using -o, --output PATH");
808 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
809 "/%s", alloc_path
, path_name
);
811 PERROR("asprintf trace dir name");
815 full_path
= expand_full_path(opt_output_path
);
816 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
818 PERROR("asprintf trace dir name");
830 * relay_delete_session: Free all memory associated with a session and
834 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
836 struct lttng_ht_iter iter
;
837 struct lttng_ht_node_ulong
*node
;
838 struct relay_stream
*stream
;
844 DBG("Relay deleting session %lu", cmd
->session
->id
);
845 free(cmd
->session
->sock
);
847 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
848 node
= lttng_ht_iter_get_node_ulong(&iter
);
850 stream
= caa_container_of(node
,
851 struct relay_stream
, stream_n
);
852 if (stream
->session
== cmd
->session
) {
854 ret
= lttng_ht_del(streams_ht
, &iter
);
863 * relay_add_stream: allocate a new stream for a session
866 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
867 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
869 struct relay_session
*session
= cmd
->session
;
870 struct lttcomm_relayd_add_stream stream_info
;
871 struct relay_stream
*stream
= NULL
;
872 struct lttcomm_relayd_status_stream reply
;
873 char *path
= NULL
, *root_path
= NULL
;
876 if (!session
|| session
->version_check_done
== 0) {
877 ERR("Trying to add a stream before version check");
882 /* FIXME : use data_size for something ? */
883 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
884 sizeof(struct lttcomm_relayd_add_stream
), MSG_WAITALL
);
885 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
886 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
890 stream
= zmalloc(sizeof(struct relay_stream
));
891 if (stream
== NULL
) {
892 PERROR("relay stream zmalloc");
897 stream
->stream_handle
= ++last_relay_stream_id
;
899 stream
->session
= session
;
901 root_path
= create_output_path(stream_info
.pathname
);
906 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
909 ERR("relay creating output directory");
913 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
915 PERROR("asprintf stream path");
919 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
921 PERROR("Relay creating trace file");
926 DBG("Tracefile %s created", path
);
928 lttng_ht_node_init_ulong(&stream
->stream_n
,
929 (unsigned long) stream
->stream_handle
);
930 lttng_ht_add_unique_ulong(streams_ht
,
933 DBG("Relay new stream added %s", stream_info
.channel_name
);
938 /* send the session id to the client or a negative return code on error */
940 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
942 reply
.ret_code
= htobe32(LTTCOMM_OK
);
944 reply
.handle
= htobe64(stream
->stream_handle
);
945 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
946 sizeof(struct lttcomm_relayd_status_stream
), 0);
948 ERR("Relay sending stream id");
956 * relay_unknown_command: send -1 if received unknown command
959 void relay_unknown_command(struct relay_command
*cmd
)
961 struct lttcomm_relayd_generic_reply reply
;
964 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
965 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
966 sizeof(struct lttcomm_relayd_generic_reply
), 0);
968 ERR("Relay sending unknown command");
973 * relay_start: send an acknowledgment to the client to tell if we are
974 * ready to receive data. We are ready if a session is established.
977 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
978 struct relay_command
*cmd
)
980 int ret
= htobe32(LTTCOMM_OK
);
981 struct lttcomm_relayd_generic_reply reply
;
982 struct relay_session
*session
= cmd
->session
;
985 DBG("Trying to start the streaming without a session established");
986 ret
= htobe32(LTTCOMM_ERR
);
989 reply
.ret_code
= ret
;
990 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
991 sizeof(struct lttcomm_relayd_generic_reply
), 0);
993 ERR("Relay sending start ack");
1000 * Get stream from stream id.
1003 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
1004 struct lttng_ht
*streams_ht
)
1006 struct lttng_ht_node_ulong
*node
;
1007 struct lttng_ht_iter iter
;
1008 struct relay_stream
*ret
;
1010 lttng_ht_lookup(streams_ht
,
1011 (void *)((unsigned long) stream_id
),
1013 node
= lttng_ht_iter_get_node_ulong(&iter
);
1015 DBG("Relay stream %lu not found", stream_id
);
1020 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1027 * relay_recv_metadata: receive the metada for the session.
1030 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1031 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1033 int ret
= htobe32(LTTCOMM_OK
);
1034 struct relay_session
*session
= cmd
->session
;
1035 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1036 struct relay_stream
*metadata_stream
;
1037 uint64_t data_size
, payload_size
;
1040 ERR("Metadata sent before version check");
1045 data_size
= be64toh(recv_hdr
->data_size
);
1046 payload_size
= data_size
- sizeof(uint64_t);
1047 if (data_buffer_size
< data_size
) {
1048 data_buffer
= realloc(data_buffer
, data_size
);
1050 ERR("Allocating data buffer");
1054 data_buffer_size
= data_size
;
1056 memset(data_buffer
, 0, data_size
);
1057 DBG2("Relay receiving metadata, waiting for %lu bytes", data_size
);
1058 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1059 if (ret
< 0 || ret
!= data_size
) {
1061 ERR("Relay didn't receive the whole metadata");
1064 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1065 metadata_stream
= relay_stream_from_stream_id(
1066 be64toh(metadata_struct
->stream_id
), streams_ht
);
1067 if (!metadata_stream
) {
1072 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1074 if (ret
< (payload_size
)) {
1075 ERR("Relay error writing metadata on file");
1079 DBG2("Relay metadata written");
1086 * relay_send_version: send relayd version number
1089 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1090 struct relay_command
*cmd
)
1092 int ret
= htobe32(LTTCOMM_OK
);
1093 struct lttcomm_relayd_version reply
;
1094 struct relay_session
*session
= NULL
;
1096 if (cmd
->session
== NULL
) {
1097 session
= zmalloc(sizeof(struct relay_session
));
1098 if (session
== NULL
) {
1099 PERROR("relay session zmalloc");
1103 session
->id
= ++last_relay_session_id
;
1104 DBG("Created session %lu", session
->id
);
1105 cmd
->session
= session
;
1107 session
->version_check_done
= 1;
1109 sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1110 reply
.major
= htobe32(reply
.major
);
1111 reply
.minor
= htobe32(reply
.minor
);
1112 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1113 sizeof(struct lttcomm_relayd_version
), 0);
1115 ERR("Relay sending version");
1117 DBG("Version check done");
1124 * relay_process_control: Process the commands received on the control socket
1127 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1128 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1132 switch (be32toh(recv_hdr
->cmd
)) {
1134 case RELAYD_CREATE_SESSION:
1135 ret = relay_create_session(recv_hdr, cmd);
1138 case RELAYD_ADD_STREAM
:
1139 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1141 case RELAYD_START_DATA
:
1142 ret
= relay_start(recv_hdr
, cmd
);
1144 case RELAYD_SEND_METADATA
:
1145 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1147 case RELAYD_VERSION
:
1148 ret
= relay_send_version(recv_hdr
, cmd
);
1150 case RELAYD_UPDATE_SYNC_INFO
:
1152 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1153 relay_unknown_command(cmd
);
1163 * relay_process_data: Process the data received on the data socket
1166 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1169 struct relay_stream
*stream
;
1170 struct lttcomm_relayd_data_hdr data_hdr
;
1174 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1175 sizeof(struct lttcomm_relayd_data_hdr
), MSG_WAITALL
);
1177 ERR("Connections seems to be closed");
1182 stream_id
= be64toh(data_hdr
.stream_id
);
1183 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1189 data_size
= be32toh(data_hdr
.data_size
);
1190 if (data_buffer_size
< data_size
) {
1191 data_buffer
= realloc(data_buffer
, data_size
);
1193 ERR("Allocating data buffer");
1197 data_buffer_size
= data_size
;
1199 memset(data_buffer
, 0, data_size
);
1201 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1207 ret
= write(stream
->fd
, data_buffer
, data_size
);
1208 if (ret
< data_size
) {
1209 ERR("Relay error writing data to file");
1213 DBG2("Relay wrote %d bytes to tracefile for stream id %lu", ret
, stream
->stream_handle
);
1220 void relay_cleanup_connection(struct lttng_ht
*relay_connections_ht
, struct lttng_poll_event
*events
,
1221 struct lttng_ht
*streams_ht
, int pollfd
, struct lttng_ht_iter
*iter
)
1225 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1227 lttng_poll_del(events
, pollfd
);
1229 ret
= close(pollfd
);
1231 ERR("Closing pollfd %d", pollfd
);
1236 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1237 struct lttng_ht
*relay_connections_ht
)
1240 struct relay_command
*relay_connection
;
1242 relay_connection
= zmalloc(sizeof(struct relay_command
));
1243 if (relay_connection
== NULL
) {
1244 PERROR("Relay command zmalloc");
1248 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1249 if (ret
< 0 || ret
< sizeof(relay_connection
)) {
1250 PERROR("read relay cmd pipe");
1255 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1256 (unsigned long) relay_connection
->sock
->fd
);
1257 lttng_ht_add_unique_ulong(relay_connections_ht
,
1258 &relay_connection
->sock_n
);
1259 ret
= lttng_poll_add(events
,
1260 relay_connection
->sock
->fd
,
1261 LPOLLIN
| LPOLLRDHUP
);
1268 * This thread does the actual work
1271 void *relay_thread_worker(void *data
)
1274 uint32_t revents
, nb_fd
;
1275 struct relay_command
*relay_connection
;
1276 struct lttng_poll_event events
;
1277 struct lttng_ht
*relay_connections_ht
;
1278 struct lttng_ht_node_ulong
*node
;
1279 struct lttng_ht_iter iter
;
1280 struct lttng_ht
*streams_ht
;
1281 struct lttcomm_relayd_hdr recv_hdr
;
1283 DBG("[thread] Relay worker started");
1285 /* table of connections indexed on socket */
1286 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1288 /* tables of streams indexed by stream ID */
1289 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1291 ret
= create_thread_poll_set(&events
, 2);
1293 goto error_poll_create
;
1296 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1302 /* Zeroed the events structure */
1303 lttng_poll_reset(&events
);
1305 nb_fd
= LTTNG_POLL_GETNB(&events
);
1307 /* Infinite blocking call, waiting for transmission */
1309 ret
= lttng_poll_wait(&events
, -1);
1312 * Restart interrupted system call.
1314 if (errno
== EINTR
) {
1320 for (i
= 0; i
< nb_fd
; i
++) {
1321 /* Fetch once the poll data */
1322 revents
= LTTNG_POLL_GETEV(&events
, i
);
1323 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1325 /* Thread quit pipe has been closed. Killing thread. */
1326 ret
= check_thread_quit_pipe(pollfd
, revents
);
1331 /* Inspect the relay cmd pipe for new connection */
1332 if (pollfd
== relay_cmd_pipe
[0]) {
1333 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1334 ERR("Relay pipe error");
1336 } else if (revents
& LPOLLIN
) {
1337 DBG("Relay command received");
1338 ret
= relay_add_connection(relay_cmd_pipe
[0],
1339 &events
, relay_connections_ht
);
1344 } else if (revents
> 0) {
1345 lttng_ht_lookup(relay_connections_ht
,
1346 (void *)((unsigned long) pollfd
),
1348 node
= lttng_ht_iter_get_node_ulong(&iter
);
1350 DBG2("Relay sock %d not found", pollfd
);
1353 relay_connection
= caa_container_of(node
,
1354 struct relay_command
, sock_n
);
1356 if (revents
& (LPOLLERR
)) {
1358 relay_cleanup_connection(relay_connections_ht
,
1359 &events
, streams_ht
, pollfd
, &iter
);
1360 free(relay_connection
);
1361 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1362 DBG("Socket %d hung up", pollfd
);
1363 relay_cleanup_connection(relay_connections_ht
,
1364 &events
, streams_ht
, pollfd
, &iter
);
1365 if (relay_connection
->type
== RELAY_CONTROL
) {
1366 relay_delete_session(relay_connection
, streams_ht
);
1368 free(relay_connection
);
1369 } else if (revents
& LPOLLIN
) {
1370 /* control socket */
1371 if (relay_connection
->type
== RELAY_CONTROL
) {
1372 ret
= relay_connection
->sock
->ops
->recvmsg(
1373 relay_connection
->sock
, &recv_hdr
,
1374 sizeof(struct lttcomm_relayd_hdr
), MSG_WAITALL
);
1375 /* connection closed */
1377 relay_cleanup_connection(relay_connections_ht
,
1378 &events
, streams_ht
, pollfd
, &iter
);
1379 relay_delete_session(relay_connection
, streams_ht
);
1380 free(relay_connection
);
1381 DBG("Control connection closed with %d", pollfd
);
1383 if (relay_connection
->session
) {
1384 DBG2("Relay worker receiving data for session : %lu",
1385 relay_connection
->session
->id
);
1387 ret
= relay_process_control(&recv_hdr
,
1391 * there was an error in processing a control
1392 * command: clear the session
1395 relay_cleanup_connection(relay_connections_ht
,
1396 &events
, streams_ht
, pollfd
, &iter
);
1397 free(relay_connection
);
1398 DBG("Connection closed with %d", pollfd
);
1402 } else if (relay_connection
->type
== RELAY_DATA
) {
1403 ret
= relay_process_data(relay_connection
, streams_ht
);
1404 /* connection closed */
1406 relay_cleanup_connection(relay_connections_ht
,
1407 &events
, streams_ht
, pollfd
, &iter
);
1408 relay_delete_session(relay_connection
, streams_ht
);
1409 DBG("Data connection closed with %d", pollfd
);
1418 lttng_poll_clean(&events
);
1420 /* empty the hash table and free the memory */
1421 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1422 node
= lttng_ht_iter_get_node_ulong(&iter
);
1424 relay_connection
= caa_container_of(node
,
1425 struct relay_command
, sock_n
);
1426 free(relay_connection
);
1428 ret
= lttng_ht_del(relay_connections_ht
, &iter
);
1433 lttng_ht_destroy(relay_connections_ht
);
1434 DBG("Worker thread cleanup complete");
1440 * Create the relay command pipe to wake thread_manage_apps.
1441 * Closed in cleanup().
1443 static int create_relay_cmd_pipe(void)
1447 ret
= pipe(relay_cmd_pipe
);
1449 PERROR("relay cmd pipe");
1453 for (i
= 0; i
< 2; i
++) {
1454 ret
= fcntl(relay_cmd_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
1456 PERROR("fcntl relay_cmd_pipe");
1468 int main(int argc
, char **argv
)
1473 /* Create thread quit pipe */
1474 if ((ret
= init_thread_quit_pipe()) < 0) {
1478 /* Parse arguments */
1480 if ((ret
= parse_args(argc
, argv
) < 0)) {
1484 if ((ret
= set_signal_handler()) < 0) {
1497 /* Check if daemon is UID = 0 */
1498 is_root
= !getuid();
1501 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1502 ERR("Need to be root to use ports < 1024");
1508 /* Setup the thread apps communication pipe. */
1509 if ((ret
= create_relay_cmd_pipe()) < 0) {
1513 /* Init relay command queue. */
1514 cds_wfq_init(&relay_cmd_queue
.queue
);
1516 /* Set up max poll set size */
1517 lttng_poll_set_max_size();
1519 /* Setup the dispatcher thread */
1520 ret
= pthread_create(&dispatcher_thread
, NULL
,
1521 relay_thread_dispatcher
, (void *) NULL
);
1523 PERROR("pthread_create dispatcher");
1524 goto exit_dispatcher
;
1527 /* Setup the worker thread */
1528 ret
= pthread_create(&worker_thread
, NULL
,
1529 relay_thread_worker
, (void *) NULL
);
1531 PERROR("pthread_create worker");
1535 /* Setup the listener thread */
1536 ret
= pthread_create(&listener_thread
, NULL
,
1537 relay_thread_listener
, (void *) NULL
);
1539 PERROR("pthread_create listener");
1544 ret
= pthread_join(listener_thread
, &status
);
1546 PERROR("pthread_join");
1547 goto error
; /* join error, exit without cleanup */
1551 ret
= pthread_join(worker_thread
, &status
);
1553 PERROR("pthread_join");
1554 goto error
; /* join error, exit without cleanup */
1558 ret
= pthread_join(dispatcher_thread
, &status
);
1560 PERROR("pthread_join");
1561 goto error
; /* join error, exit without cleanup */