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>
52 #include <common/utils.h>
54 #include "lttng-relayd.h"
56 /* command line options */
57 static int opt_daemon
;
58 static char *opt_output_path
;
59 static struct lttng_uri
*control_uri
= NULL
;
60 static struct lttng_uri
*data_uri
= NULL
;
63 static int is_root
; /* Set to 1 if the daemon is running as root */
66 * Quit pipe for all threads. This permits a single cancellation point
67 * for all threads when receiving an event on the pipe.
69 static int thread_quit_pipe
[2] = { -1, -1 };
72 * This pipe is used to inform the worker thread that a command is queued and
73 * ready to be processed.
75 static int relay_cmd_pipe
[2] = { -1, -1 };
77 static int dispatch_thread_exit
;
79 static pthread_t listener_thread
;
80 static pthread_t dispatcher_thread
;
81 static pthread_t worker_thread
;
83 static uint64_t last_relay_stream_id
= 0;
84 static uint64_t last_relay_session_id
= 0;
87 * Relay command queue.
89 * The relay_thread_listener and relay_thread_dispatcher communicate with this
92 static struct relay_cmd_queue relay_cmd_queue
;
94 /* buffer allocated at startup, used to store the trace data */
95 static char *data_buffer
= NULL
;
96 static unsigned int data_buffer_size
= 0;
99 * usage function on stderr
104 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
105 fprintf(stderr
, " -h, --help Display this usage.\n");
106 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
107 fprintf(stderr
, " -C, --control-port Control port listening (URI)\n");
108 fprintf(stderr
, " -D, --data-port Data port listening (URI)\n");
109 fprintf(stderr
, " -o, --output Output path for traces (PATH)\n");
110 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
114 int parse_args(int argc
, char **argv
)
118 char *default_address
;
120 static struct option long_options
[] = {
121 { "control-port", 1, 0, 'C', },
122 { "data-port", 1, 0, 'D', },
123 { "daemonize", 0, 0, 'd', },
124 { "help", 0, 0, 'h', },
125 { "output", 1, 0, 'o', },
126 { "verbose", 0, 0, 'v', },
131 int option_index
= 0;
132 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
133 long_options
, &option_index
);
140 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
142 fprintf(stderr
, " with arg %s\n", optarg
);
146 ret
= uri_parse(optarg
, &control_uri
);
148 ERR("Invalid control URI specified");
151 if (control_uri
->port
== 0) {
152 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
156 ret
= uri_parse(optarg
, &data_uri
);
158 ERR("Invalid data URI specified");
161 if (data_uri
->port
== 0) {
162 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
172 ret
= asprintf(&opt_output_path
, "%s", optarg
);
174 PERROR("asprintf opt_output_path");
179 /* Verbose level can increase using multiple -v */
180 lttng_opt_verbose
+= 1;
183 /* Unknown option or other error.
184 * Error is printed by getopt, just return */
190 /* assign default values */
191 if (control_uri
== NULL
) {
192 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
193 DEFAULT_NETWORK_CONTROL_PORT
);
195 PERROR("asprintf default data address");
199 ret
= uri_parse(default_address
, &control_uri
);
200 free(default_address
);
202 ERR("Invalid control URI specified");
206 if (data_uri
== NULL
) {
207 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
208 DEFAULT_NETWORK_DATA_PORT
);
210 PERROR("asprintf default data address");
214 ret
= uri_parse(default_address
, &data_uri
);
215 free(default_address
);
217 ERR("Invalid data URI specified");
234 /* Close thread quit pipes */
235 utils_close_pipe(thread_quit_pipe
);
237 /* Close relay cmd pipes */
238 utils_close_pipe(relay_cmd_pipe
);
242 * Write to writable pipe used to notify a thread.
245 int notify_thread_pipe(int wpipe
)
249 ret
= write(wpipe
, "!", 1);
251 PERROR("write poll pipe");
258 * Stop all threads by closing the thread quit pipe.
261 void stop_threads(void)
265 /* Stopping all threads */
266 DBG("Terminating all threads");
267 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
269 ERR("write error on thread quit pipe");
272 /* Dispatch thread */
273 dispatch_thread_exit
= 1;
274 futex_nto1_wake(&relay_cmd_queue
.futex
);
278 * Signal handler for the daemon
280 * Simply stop all worker threads, leaving main() return gracefully after
281 * joining all threads and calling cleanup().
284 void sighandler(int sig
)
288 DBG("SIGPIPE caught");
291 DBG("SIGINT caught");
295 DBG("SIGTERM caught");
304 * Setup signal handler for :
305 * SIGINT, SIGTERM, SIGPIPE
308 int set_signal_handler(void)
314 if ((ret
= sigemptyset(&sigset
)) < 0) {
315 PERROR("sigemptyset");
319 sa
.sa_handler
= sighandler
;
322 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
327 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
332 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
337 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
343 * Init thread quit pipe.
345 * Return -1 on error or 0 if all pipes are created.
348 int init_thread_quit_pipe(void)
352 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
358 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
361 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
365 if (events
== NULL
|| size
== 0) {
370 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
376 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
388 * Check if the thread quit pipe was triggered.
390 * Return 1 if it was triggered else 0;
393 int check_thread_quit_pipe(int fd
, uint32_t events
)
395 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
403 * Create and init socket from uri.
406 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
409 struct lttcomm_sock
*sock
= NULL
;
411 sock
= lttcomm_alloc_sock_from_uri(uri
);
413 ERR("Allocating socket");
417 ret
= lttcomm_create_sock(sock
);
421 DBG("Listening on sock %d", sock
->fd
);
423 ret
= sock
->ops
->bind(sock
);
428 ret
= sock
->ops
->listen(sock
, -1);
438 lttcomm_destroy_sock(sock
);
444 * This thread manages the listening for new connections on the network
447 void *relay_thread_listener(void *data
)
451 uint32_t revents
, nb_fd
;
452 struct lttng_poll_event events
;
453 struct lttcomm_sock
*control_sock
, *data_sock
;
456 * Get allocated in this thread, enqueued to a global queue, dequeued and
457 * freed in the worker thread.
459 struct relay_command
*relay_cmd
= NULL
;
461 DBG("[thread] Relay listener started");
463 control_sock
= relay_init_sock(control_uri
);
468 data_sock
= relay_init_sock(data_uri
);
474 * Pass 3 as size here for the thread quit pipe, control and data socket.
476 ret
= create_thread_poll_set(&events
, 3);
478 goto error_create_poll
;
481 /* Add the control socket */
482 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
487 /* Add the data socket */
488 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
494 DBG("Listener accepting connections");
496 nb_fd
= LTTNG_POLL_GETNB(&events
);
499 ret
= lttng_poll_wait(&events
, -1);
502 * Restart interrupted system call.
504 if (errno
== EINTR
) {
510 DBG("Relay new connection received");
511 for (i
= 0; i
< nb_fd
; i
++) {
512 /* Fetch once the poll data */
513 revents
= LTTNG_POLL_GETEV(&events
, i
);
514 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
516 /* Thread quit pipe has been closed. Killing thread. */
517 ret
= check_thread_quit_pipe(pollfd
, revents
);
522 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
523 ERR("socket poll error");
525 } else if (revents
& LPOLLIN
) {
526 struct lttcomm_sock
*newsock
= NULL
;
528 relay_cmd
= zmalloc(sizeof(struct relay_command
));
529 if (relay_cmd
== NULL
) {
530 PERROR("relay command zmalloc");
534 if (pollfd
== data_sock
->fd
) {
535 newsock
= data_sock
->ops
->accept(data_sock
);
537 PERROR("accepting data sock");
540 relay_cmd
->type
= RELAY_DATA
;
541 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
542 } else if (pollfd
== control_sock
->fd
) {
543 newsock
= control_sock
->ops
->accept(control_sock
);
545 PERROR("accepting control sock");
548 relay_cmd
->type
= RELAY_CONTROL
;
549 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
551 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
554 PERROR("setsockopt inet");
557 relay_cmd
->sock
= newsock
;
559 * Lock free enqueue the request.
561 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
564 * Wake the dispatch queue futex. Implicit memory
565 * barrier with the exchange in cds_wfq_enqueue.
567 futex_nto1_wake(&relay_cmd_queue
.futex
);
574 lttng_poll_clean(&events
);
576 if (control_sock
->fd
>= 0) {
577 ret
= control_sock
->ops
->close(control_sock
);
581 lttcomm_destroy_sock(control_sock
);
583 if (data_sock
->fd
>= 0) {
584 ret
= data_sock
->ops
->close(data_sock
);
588 lttcomm_destroy_sock(data_sock
);
591 DBG("Relay listener thread cleanup complete");
598 * This thread manages the dispatching of the requests to worker threads
601 void *relay_thread_dispatcher(void *data
)
604 struct cds_wfq_node
*node
;
605 struct relay_command
*relay_cmd
= NULL
;
607 DBG("[thread] Relay dispatcher started");
609 while (!dispatch_thread_exit
) {
610 /* Atomically prepare the queue futex */
611 futex_nto1_prepare(&relay_cmd_queue
.futex
);
614 /* Dequeue commands */
615 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
617 DBG("Woken up but nothing in the relay command queue");
618 /* Continue thread execution */
622 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
623 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
626 * Inform worker thread of the new request. This
627 * call is blocking so we can be assured that the data will be read
628 * at some point in time or wait to the end of the world :)
630 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
631 sizeof(struct relay_command
));
634 PERROR("write cmd pipe");
637 } while (node
!= NULL
);
639 /* Futex wait on queue. Blocking call on futex() */
640 futex_nto1_wait(&relay_cmd_queue
.futex
);
644 DBG("Dispatch thread dying");
650 * Return the realpath(3) of the path even if the last directory token does not
651 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
652 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
653 * fails if the end point directory does not exist.
656 char *expand_full_path(const char *path
)
658 const char *end_path
= path
;
659 char *next
, *cut_path
, *expanded_path
;
661 /* Find last token delimited by '/' */
662 while ((next
= strpbrk(end_path
+ 1, "/"))) {
666 /* Cut last token from original path */
667 cut_path
= strndup(path
, end_path
- path
);
669 expanded_path
= malloc(PATH_MAX
);
670 if (expanded_path
== NULL
) {
674 expanded_path
= realpath((char *)cut_path
, expanded_path
);
675 if (expanded_path
== NULL
) {
678 ERR("%s: No such file or directory", cut_path
);
687 /* Add end part to expanded path */
688 strcat(expanded_path
, end_path
);
691 return expanded_path
;
700 * config_get_default_path
702 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
705 char *config_get_default_path(void)
707 return getenv("HOME");
711 * Create recursively directory using the FULL path.
714 int mkdir_recursive(char *path
, mode_t mode
)
716 char *p
, tmp
[PATH_MAX
];
721 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
723 PERROR("snprintf mkdir");
728 if (tmp
[len
- 1] == '/') {
732 for (p
= tmp
+ 1; *p
; p
++) {
735 if (tmp
[strlen(tmp
) - 1] == '.' &&
736 tmp
[strlen(tmp
) - 2] == '.' &&
737 tmp
[strlen(tmp
) - 3] == '/') {
738 ERR("Using '/../' is not permitted in the trace path (%s)",
743 ret
= stat(tmp
, &statbuf
);
745 ret
= mkdir(tmp
, mode
);
747 if (!(errno
== EEXIST
)) {
748 PERROR("mkdir recursive");
758 ret
= mkdir(tmp
, mode
);
760 if (!(errno
== EEXIST
)) {
761 PERROR("mkdir recursive last piece");
773 * create_output_path: create the output trace directory
776 char *create_output_path(char *path_name
)
779 char *alloc_path
= NULL
;
780 char *traces_path
= NULL
;
781 char *full_path
= NULL
;
783 /* Auto output path */
784 if (opt_output_path
== NULL
) {
785 alloc_path
= strdup(config_get_default_path());
786 if (alloc_path
== NULL
) {
787 ERR("Home path not found.\n \
788 Please specify an output path using -o, --output PATH");
793 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
794 "/%s", alloc_path
, path_name
);
796 PERROR("asprintf trace dir name");
800 full_path
= expand_full_path(opt_output_path
);
801 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
803 PERROR("asprintf trace dir name");
815 * relay_delete_session: Free all memory associated with a session and
819 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
821 struct lttng_ht_iter iter
;
822 struct lttng_ht_node_ulong
*node
;
823 struct relay_stream
*stream
;
829 DBG("Relay deleting session %lu", cmd
->session
->id
);
830 free(cmd
->session
->sock
);
832 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
833 node
= lttng_ht_iter_get_node_ulong(&iter
);
835 stream
= caa_container_of(node
,
836 struct relay_stream
, stream_n
);
837 if (stream
->session
== cmd
->session
) {
839 ret
= lttng_ht_del(streams_ht
, &iter
);
848 * relay_add_stream: allocate a new stream for a session
851 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
852 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
854 struct relay_session
*session
= cmd
->session
;
855 struct lttcomm_relayd_add_stream stream_info
;
856 struct relay_stream
*stream
= NULL
;
857 struct lttcomm_relayd_status_stream reply
;
858 char *path
= NULL
, *root_path
= NULL
;
861 if (!session
|| session
->version_check_done
== 0) {
862 ERR("Trying to add a stream before version check");
867 /* FIXME : use data_size for something ? */
868 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
869 sizeof(struct lttcomm_relayd_add_stream
), MSG_WAITALL
);
870 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
871 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
875 stream
= zmalloc(sizeof(struct relay_stream
));
876 if (stream
== NULL
) {
877 PERROR("relay stream zmalloc");
882 stream
->stream_handle
= ++last_relay_stream_id
;
884 stream
->session
= session
;
886 root_path
= create_output_path(stream_info
.pathname
);
891 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
894 ERR("relay creating output directory");
898 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
900 PERROR("asprintf stream path");
904 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
906 PERROR("Relay creating trace file");
911 DBG("Tracefile %s created", path
);
913 lttng_ht_node_init_ulong(&stream
->stream_n
,
914 (unsigned long) stream
->stream_handle
);
915 lttng_ht_add_unique_ulong(streams_ht
,
918 DBG("Relay new stream added %s", stream_info
.channel_name
);
923 /* send the session id to the client or a negative return code on error */
925 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
927 reply
.ret_code
= htobe32(LTTCOMM_OK
);
929 reply
.handle
= htobe64(stream
->stream_handle
);
930 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
931 sizeof(struct lttcomm_relayd_status_stream
), 0);
933 ERR("Relay sending stream id");
941 * relay_unknown_command: send -1 if received unknown command
944 void relay_unknown_command(struct relay_command
*cmd
)
946 struct lttcomm_relayd_generic_reply reply
;
949 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
950 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
951 sizeof(struct lttcomm_relayd_generic_reply
), 0);
953 ERR("Relay sending unknown command");
958 * relay_start: send an acknowledgment to the client to tell if we are
959 * ready to receive data. We are ready if a session is established.
962 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
963 struct relay_command
*cmd
)
965 int ret
= htobe32(LTTCOMM_OK
);
966 struct lttcomm_relayd_generic_reply reply
;
967 struct relay_session
*session
= cmd
->session
;
970 DBG("Trying to start the streaming without a session established");
971 ret
= htobe32(LTTCOMM_ERR
);
974 reply
.ret_code
= ret
;
975 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
976 sizeof(struct lttcomm_relayd_generic_reply
), 0);
978 ERR("Relay sending start ack");
985 * Get stream from stream id.
988 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
989 struct lttng_ht
*streams_ht
)
991 struct lttng_ht_node_ulong
*node
;
992 struct lttng_ht_iter iter
;
993 struct relay_stream
*ret
;
995 lttng_ht_lookup(streams_ht
,
996 (void *)((unsigned long) stream_id
),
998 node
= lttng_ht_iter_get_node_ulong(&iter
);
1000 DBG("Relay stream %lu not found", stream_id
);
1005 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1012 * relay_recv_metadata: receive the metada for the session.
1015 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1016 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1018 int ret
= htobe32(LTTCOMM_OK
);
1019 struct relay_session
*session
= cmd
->session
;
1020 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1021 struct relay_stream
*metadata_stream
;
1022 uint64_t data_size
, payload_size
;
1025 ERR("Metadata sent before version check");
1030 data_size
= be64toh(recv_hdr
->data_size
);
1031 payload_size
= data_size
- sizeof(uint64_t);
1032 if (data_buffer_size
< data_size
) {
1033 data_buffer
= realloc(data_buffer
, data_size
);
1035 ERR("Allocating data buffer");
1039 data_buffer_size
= data_size
;
1041 memset(data_buffer
, 0, data_size
);
1042 DBG2("Relay receiving metadata, waiting for %lu bytes", data_size
);
1043 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1044 if (ret
< 0 || ret
!= data_size
) {
1046 ERR("Relay didn't receive the whole metadata");
1049 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1050 metadata_stream
= relay_stream_from_stream_id(
1051 be64toh(metadata_struct
->stream_id
), streams_ht
);
1052 if (!metadata_stream
) {
1057 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1059 if (ret
< (payload_size
)) {
1060 ERR("Relay error writing metadata on file");
1064 DBG2("Relay metadata written");
1071 * relay_send_version: send relayd version number
1074 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1075 struct relay_command
*cmd
)
1077 int ret
= htobe32(LTTCOMM_OK
);
1078 struct lttcomm_relayd_version reply
;
1079 struct relay_session
*session
= NULL
;
1081 if (cmd
->session
== NULL
) {
1082 session
= zmalloc(sizeof(struct relay_session
));
1083 if (session
== NULL
) {
1084 PERROR("relay session zmalloc");
1088 session
->id
= ++last_relay_session_id
;
1089 DBG("Created session %lu", session
->id
);
1090 cmd
->session
= session
;
1092 session
->version_check_done
= 1;
1094 sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1095 reply
.major
= htobe32(reply
.major
);
1096 reply
.minor
= htobe32(reply
.minor
);
1097 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1098 sizeof(struct lttcomm_relayd_version
), 0);
1100 ERR("Relay sending version");
1102 DBG("Version check done");
1109 * relay_process_control: Process the commands received on the control socket
1112 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1113 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1117 switch (be32toh(recv_hdr
->cmd
)) {
1119 case RELAYD_CREATE_SESSION:
1120 ret = relay_create_session(recv_hdr, cmd);
1123 case RELAYD_ADD_STREAM
:
1124 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1126 case RELAYD_START_DATA
:
1127 ret
= relay_start(recv_hdr
, cmd
);
1129 case RELAYD_SEND_METADATA
:
1130 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1132 case RELAYD_VERSION
:
1133 ret
= relay_send_version(recv_hdr
, cmd
);
1135 case RELAYD_UPDATE_SYNC_INFO
:
1137 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1138 relay_unknown_command(cmd
);
1148 * relay_process_data: Process the data received on the data socket
1151 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1154 struct relay_stream
*stream
;
1155 struct lttcomm_relayd_data_hdr data_hdr
;
1159 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1160 sizeof(struct lttcomm_relayd_data_hdr
), MSG_WAITALL
);
1162 ERR("Connections seems to be closed");
1167 stream_id
= be64toh(data_hdr
.stream_id
);
1168 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1174 data_size
= be32toh(data_hdr
.data_size
);
1175 if (data_buffer_size
< data_size
) {
1176 data_buffer
= realloc(data_buffer
, data_size
);
1178 ERR("Allocating data buffer");
1182 data_buffer_size
= data_size
;
1184 memset(data_buffer
, 0, data_size
);
1186 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1192 ret
= write(stream
->fd
, data_buffer
, data_size
);
1193 if (ret
< data_size
) {
1194 ERR("Relay error writing data to file");
1198 DBG2("Relay wrote %d bytes to tracefile for stream id %lu", ret
, stream
->stream_handle
);
1205 void relay_cleanup_connection(struct lttng_ht
*relay_connections_ht
, struct lttng_poll_event
*events
,
1206 struct lttng_ht
*streams_ht
, int pollfd
, struct lttng_ht_iter
*iter
)
1210 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1212 lttng_poll_del(events
, pollfd
);
1214 ret
= close(pollfd
);
1216 ERR("Closing pollfd %d", pollfd
);
1221 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1222 struct lttng_ht
*relay_connections_ht
)
1225 struct relay_command
*relay_connection
;
1227 relay_connection
= zmalloc(sizeof(struct relay_command
));
1228 if (relay_connection
== NULL
) {
1229 PERROR("Relay command zmalloc");
1233 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1234 if (ret
< 0 || ret
< sizeof(relay_connection
)) {
1235 PERROR("read relay cmd pipe");
1240 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1241 (unsigned long) relay_connection
->sock
->fd
);
1242 lttng_ht_add_unique_ulong(relay_connections_ht
,
1243 &relay_connection
->sock_n
);
1244 ret
= lttng_poll_add(events
,
1245 relay_connection
->sock
->fd
,
1246 LPOLLIN
| LPOLLRDHUP
);
1253 * This thread does the actual work
1256 void *relay_thread_worker(void *data
)
1259 uint32_t revents
, nb_fd
;
1260 struct relay_command
*relay_connection
;
1261 struct lttng_poll_event events
;
1262 struct lttng_ht
*relay_connections_ht
;
1263 struct lttng_ht_node_ulong
*node
;
1264 struct lttng_ht_iter iter
;
1265 struct lttng_ht
*streams_ht
;
1266 struct lttcomm_relayd_hdr recv_hdr
;
1268 DBG("[thread] Relay worker started");
1270 /* table of connections indexed on socket */
1271 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1273 /* tables of streams indexed by stream ID */
1274 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1276 ret
= create_thread_poll_set(&events
, 2);
1278 goto error_poll_create
;
1281 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1287 /* Zeroed the events structure */
1288 lttng_poll_reset(&events
);
1290 nb_fd
= LTTNG_POLL_GETNB(&events
);
1292 /* Infinite blocking call, waiting for transmission */
1294 ret
= lttng_poll_wait(&events
, -1);
1297 * Restart interrupted system call.
1299 if (errno
== EINTR
) {
1305 for (i
= 0; i
< nb_fd
; i
++) {
1306 /* Fetch once the poll data */
1307 revents
= LTTNG_POLL_GETEV(&events
, i
);
1308 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1310 /* Thread quit pipe has been closed. Killing thread. */
1311 ret
= check_thread_quit_pipe(pollfd
, revents
);
1316 /* Inspect the relay cmd pipe for new connection */
1317 if (pollfd
== relay_cmd_pipe
[0]) {
1318 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1319 ERR("Relay pipe error");
1321 } else if (revents
& LPOLLIN
) {
1322 DBG("Relay command received");
1323 ret
= relay_add_connection(relay_cmd_pipe
[0],
1324 &events
, relay_connections_ht
);
1329 } else if (revents
> 0) {
1330 lttng_ht_lookup(relay_connections_ht
,
1331 (void *)((unsigned long) pollfd
),
1333 node
= lttng_ht_iter_get_node_ulong(&iter
);
1335 DBG2("Relay sock %d not found", pollfd
);
1338 relay_connection
= caa_container_of(node
,
1339 struct relay_command
, sock_n
);
1341 if (revents
& (LPOLLERR
)) {
1343 relay_cleanup_connection(relay_connections_ht
,
1344 &events
, streams_ht
, pollfd
, &iter
);
1345 free(relay_connection
);
1346 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1347 DBG("Socket %d hung up", pollfd
);
1348 relay_cleanup_connection(relay_connections_ht
,
1349 &events
, streams_ht
, pollfd
, &iter
);
1350 if (relay_connection
->type
== RELAY_CONTROL
) {
1351 relay_delete_session(relay_connection
, streams_ht
);
1353 free(relay_connection
);
1354 } else if (revents
& LPOLLIN
) {
1355 /* control socket */
1356 if (relay_connection
->type
== RELAY_CONTROL
) {
1357 ret
= relay_connection
->sock
->ops
->recvmsg(
1358 relay_connection
->sock
, &recv_hdr
,
1359 sizeof(struct lttcomm_relayd_hdr
), MSG_WAITALL
);
1360 /* connection closed */
1362 relay_cleanup_connection(relay_connections_ht
,
1363 &events
, streams_ht
, pollfd
, &iter
);
1364 relay_delete_session(relay_connection
, streams_ht
);
1365 free(relay_connection
);
1366 DBG("Control connection closed with %d", pollfd
);
1368 if (relay_connection
->session
) {
1369 DBG2("Relay worker receiving data for session : %lu",
1370 relay_connection
->session
->id
);
1372 ret
= relay_process_control(&recv_hdr
,
1376 * there was an error in processing a control
1377 * command: clear the session
1380 relay_cleanup_connection(relay_connections_ht
,
1381 &events
, streams_ht
, pollfd
, &iter
);
1382 free(relay_connection
);
1383 DBG("Connection closed with %d", pollfd
);
1387 } else if (relay_connection
->type
== RELAY_DATA
) {
1388 ret
= relay_process_data(relay_connection
, streams_ht
);
1389 /* connection closed */
1391 relay_cleanup_connection(relay_connections_ht
,
1392 &events
, streams_ht
, pollfd
, &iter
);
1393 relay_delete_session(relay_connection
, streams_ht
);
1394 DBG("Data connection closed with %d", pollfd
);
1403 lttng_poll_clean(&events
);
1405 /* empty the hash table and free the memory */
1406 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1407 node
= lttng_ht_iter_get_node_ulong(&iter
);
1409 relay_connection
= caa_container_of(node
,
1410 struct relay_command
, sock_n
);
1411 free(relay_connection
);
1413 ret
= lttng_ht_del(relay_connections_ht
, &iter
);
1418 lttng_ht_destroy(relay_connections_ht
);
1419 DBG("Worker thread cleanup complete");
1425 * Create the relay command pipe to wake thread_manage_apps.
1426 * Closed in cleanup().
1428 static int create_relay_cmd_pipe(void)
1432 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
1440 int main(int argc
, char **argv
)
1445 /* Create thread quit pipe */
1446 if ((ret
= init_thread_quit_pipe()) < 0) {
1450 /* Parse arguments */
1452 if ((ret
= parse_args(argc
, argv
) < 0)) {
1456 if ((ret
= set_signal_handler()) < 0) {
1469 /* Check if daemon is UID = 0 */
1470 is_root
= !getuid();
1473 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1474 ERR("Need to be root to use ports < 1024");
1480 /* Setup the thread apps communication pipe. */
1481 if ((ret
= create_relay_cmd_pipe()) < 0) {
1485 /* Init relay command queue. */
1486 cds_wfq_init(&relay_cmd_queue
.queue
);
1488 /* Set up max poll set size */
1489 lttng_poll_set_max_size();
1491 /* Setup the dispatcher thread */
1492 ret
= pthread_create(&dispatcher_thread
, NULL
,
1493 relay_thread_dispatcher
, (void *) NULL
);
1495 PERROR("pthread_create dispatcher");
1496 goto exit_dispatcher
;
1499 /* Setup the worker thread */
1500 ret
= pthread_create(&worker_thread
, NULL
,
1501 relay_thread_worker
, (void *) NULL
);
1503 PERROR("pthread_create worker");
1507 /* Setup the listener thread */
1508 ret
= pthread_create(&listener_thread
, NULL
,
1509 relay_thread_listener
, (void *) NULL
);
1511 PERROR("pthread_create listener");
1516 ret
= pthread_join(listener_thread
, &status
);
1518 PERROR("pthread_join");
1519 goto error
; /* join error, exit without cleanup */
1523 ret
= pthread_join(worker_thread
, &status
);
1525 PERROR("pthread_join");
1526 goto error
; /* join error, exit without cleanup */
1530 ret
= pthread_join(dispatcher_thread
, &status
);
1532 PERROR("pthread_join");
1533 goto error
; /* join error, exit without cleanup */