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
, " -o, --output Output path for traces (PATH)\n");
109 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
113 int parse_args(int argc
, char **argv
)
117 char *default_address
;
119 static struct option long_options
[] = {
120 { "control-port", 1, 0, 'C' },
121 { "data-port", 1, 0, 'D' },
122 { "daemonize", 0, 0, 'd' },
123 { "help", 0, 0, 'h' },
124 { "output", 1, 0, 'o' },
125 { "verbose", 0, 0, 'v' },
130 int option_index
= 0;
131 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
132 long_options
, &option_index
);
139 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
141 fprintf(stderr
, " with arg %s\n", optarg
);
145 ret
= uri_parse(optarg
, &control_uri
);
147 ERR("Invalid control URI specified");
150 if (control_uri
->port
== 0) {
151 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
155 ret
= uri_parse(optarg
, &data_uri
);
157 ERR("Invalid data URI specified");
160 if (data_uri
->port
== 0) {
161 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
171 ret
= asprintf(&opt_output_path
, "%s", optarg
);
173 PERROR("asprintf opt_output_path");
178 /* Verbose level can increase using multiple -v */
179 lttng_opt_verbose
+= 1;
182 /* Unknown option or other error.
183 * Error is printed by getopt, just return */
189 /* assign default values */
190 if (control_uri
== NULL
) {
191 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
192 DEFAULT_NETWORK_CONTROL_PORT
);
194 PERROR("asprintf default data address");
198 ret
= uri_parse(default_address
, &control_uri
);
199 free(default_address
);
201 ERR("Invalid control URI specified");
205 if (data_uri
== NULL
) {
206 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
207 DEFAULT_NETWORK_DATA_PORT
);
209 PERROR("asprintf default data address");
213 ret
= uri_parse(default_address
, &data_uri
);
214 free(default_address
);
216 ERR("Invalid data URI specified");
235 for (i
= 0; i
< 2; i
++) {
236 if (thread_quit_pipe
[i
] >= 0) {
237 ret
= close(thread_quit_pipe
[i
]);
246 * Write to writable pipe used to notify a thread.
249 int notify_thread_pipe(int wpipe
)
253 ret
= write(wpipe
, "!", 1);
255 PERROR("write poll pipe");
262 * Stop all threads by closing the thread quit pipe.
265 void stop_threads(void)
269 /* Stopping all threads */
270 DBG("Terminating all threads");
271 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
273 ERR("write error on thread quit pipe");
276 /* Dispatch thread */
277 dispatch_thread_exit
= 1;
278 futex_nto1_wake(&relay_cmd_queue
.futex
);
282 * Signal handler for the daemon
284 * Simply stop all worker threads, leaving main() return gracefully after
285 * joining all threads and calling cleanup().
288 void sighandler(int sig
)
292 DBG("SIGPIPE caught");
295 DBG("SIGINT caught");
299 DBG("SIGTERM caught");
308 * Setup signal handler for :
309 * SIGINT, SIGTERM, SIGPIPE
312 int set_signal_handler(void)
318 if ((ret
= sigemptyset(&sigset
)) < 0) {
319 PERROR("sigemptyset");
323 sa
.sa_handler
= sighandler
;
326 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
331 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
336 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
341 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
347 * Init thread quit pipe.
349 * Return -1 on error or 0 if all pipes are created.
352 int init_thread_quit_pipe(void)
356 ret
= pipe(thread_quit_pipe
);
358 PERROR("thread quit pipe");
362 for (i
= 0; i
< 2; i
++) {
363 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
375 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
378 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
382 if (events
== NULL
|| size
== 0) {
387 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
393 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
405 * Check if the thread quit pipe was triggered.
407 * Return 1 if it was triggered else 0;
410 int check_thread_quit_pipe(int fd
, uint32_t events
)
412 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
420 * Create and init socket from uri.
423 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
426 struct lttcomm_sock
*sock
= NULL
;
428 sock
= lttcomm_alloc_sock_from_uri(uri
);
430 ERR("Allocating socket");
434 ret
= lttcomm_create_sock(sock
);
438 DBG("Listening on sock %d", sock
->fd
);
440 ret
= sock
->ops
->bind(sock
);
445 ret
= sock
->ops
->listen(sock
, -1);
455 lttcomm_destroy_sock(sock
);
461 * This thread manages the listening for new connections on the network
464 void *relay_thread_listener(void *data
)
468 uint32_t revents
, nb_fd
;
469 struct lttng_poll_event events
;
470 struct lttcomm_sock
*control_sock
, *data_sock
;
473 * Get allocated in this thread, enqueued to a global queue, dequeued and
474 * freed in the worker thread.
476 struct relay_command
*relay_cmd
= NULL
;
478 DBG("[thread] Relay listener started");
480 control_sock
= relay_init_sock(control_uri
);
485 data_sock
= relay_init_sock(data_uri
);
491 * Pass 3 as size here for the thread quit pipe, control and data socket.
493 ret
= create_thread_poll_set(&events
, 3);
495 goto error_create_poll
;
498 /* Add the control socket */
499 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
504 /* Add the data socket */
505 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
511 DBG("Listener accepting connections");
513 nb_fd
= LTTNG_POLL_GETNB(&events
);
516 ret
= lttng_poll_wait(&events
, -1);
519 * Restart interrupted system call.
521 if (errno
== EINTR
) {
527 DBG("Relay new connection received");
528 for (i
= 0; i
< nb_fd
; i
++) {
529 /* Fetch once the poll data */
530 revents
= LTTNG_POLL_GETEV(&events
, i
);
531 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
533 /* Thread quit pipe has been closed. Killing thread. */
534 ret
= check_thread_quit_pipe(pollfd
, revents
);
539 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
540 ERR("socket poll error");
542 } else if (revents
& LPOLLIN
) {
543 struct lttcomm_sock
*newsock
= NULL
;
545 relay_cmd
= zmalloc(sizeof(struct relay_command
));
546 if (relay_cmd
== NULL
) {
547 PERROR("relay command zmalloc");
551 if (pollfd
== data_sock
->fd
) {
552 newsock
= data_sock
->ops
->accept(data_sock
);
554 PERROR("accepting data sock");
557 relay_cmd
->type
= RELAY_DATA
;
558 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
559 } else if (pollfd
== control_sock
->fd
) {
560 newsock
= control_sock
->ops
->accept(control_sock
);
562 PERROR("accepting control sock");
565 relay_cmd
->type
= RELAY_CONTROL
;
566 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
568 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
571 PERROR("setsockopt inet");
574 relay_cmd
->sock
= newsock
;
576 * Lock free enqueue the request.
578 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
581 * Wake the dispatch queue futex. Implicit memory
582 * barrier with the exchange in cds_wfq_enqueue.
584 futex_nto1_wake(&relay_cmd_queue
.futex
);
591 lttng_poll_clean(&events
);
593 if (control_sock
->fd
>= 0) {
594 ret
= control_sock
->ops
->close(control_sock
);
598 lttcomm_destroy_sock(control_sock
);
600 if (data_sock
->fd
>= 0) {
601 ret
= data_sock
->ops
->close(data_sock
);
605 lttcomm_destroy_sock(data_sock
);
608 DBG("Relay listener thread cleanup complete");
615 * This thread manages the dispatching of the requests to worker threads
618 void *relay_thread_dispatcher(void *data
)
621 struct cds_wfq_node
*node
;
622 struct relay_command
*relay_cmd
= NULL
;
624 DBG("[thread] Relay dispatcher started");
626 while (!dispatch_thread_exit
) {
627 /* Atomically prepare the queue futex */
628 futex_nto1_prepare(&relay_cmd_queue
.futex
);
631 /* Dequeue commands */
632 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
634 DBG("Woken up but nothing in the relay command queue");
635 /* Continue thread execution */
639 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
640 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
643 * Inform worker thread of the new request. This
644 * call is blocking so we can be assured that the data will be read
645 * at some point in time or wait to the end of the world :)
647 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
648 sizeof(struct relay_command
));
651 PERROR("write cmd pipe");
654 } while (node
!= NULL
);
656 /* Futex wait on queue. Blocking call on futex() */
657 futex_nto1_wait(&relay_cmd_queue
.futex
);
661 DBG("Dispatch thread dying");
667 * Return the realpath(3) of the path even if the last directory token does not
668 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
669 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
670 * fails if the end point directory does not exist.
673 char *expand_full_path(const char *path
)
675 const char *end_path
= path
;
676 char *next
, *cut_path
, *expanded_path
;
678 /* Find last token delimited by '/' */
679 while ((next
= strpbrk(end_path
+ 1, "/"))) {
683 /* Cut last token from original path */
684 cut_path
= strndup(path
, end_path
- path
);
686 expanded_path
= malloc(PATH_MAX
);
687 if (expanded_path
== NULL
) {
691 expanded_path
= realpath((char *)cut_path
, expanded_path
);
692 if (expanded_path
== NULL
) {
695 ERR("%s: No such file or directory", cut_path
);
704 /* Add end part to expanded path */
705 strcat(expanded_path
, end_path
);
708 return expanded_path
;
717 * config_get_default_path
719 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
722 char *config_get_default_path(void)
724 return getenv("HOME");
728 * Create recursively directory using the FULL path.
731 int mkdir_recursive(char *path
, mode_t mode
)
733 char *p
, tmp
[PATH_MAX
];
738 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
740 PERROR("snprintf mkdir");
745 if (tmp
[len
- 1] == '/') {
749 for (p
= tmp
+ 1; *p
; p
++) {
752 if (tmp
[strlen(tmp
) - 1] == '.' &&
753 tmp
[strlen(tmp
) - 2] == '.' &&
754 tmp
[strlen(tmp
) - 3] == '/') {
755 ERR("Using '/../' is not permitted in the trace path (%s)",
760 ret
= stat(tmp
, &statbuf
);
762 ret
= mkdir(tmp
, mode
);
764 if (!(errno
== EEXIST
)) {
765 PERROR("mkdir recursive");
775 ret
= mkdir(tmp
, mode
);
777 if (!(errno
== EEXIST
)) {
778 PERROR("mkdir recursive last piece");
790 * create_output_path: create the output trace directory
793 char *create_output_path(char *path_name
)
796 char *alloc_path
= NULL
;
797 char *traces_path
= NULL
;
798 char *full_path
= NULL
;
800 /* Auto output path */
801 if (opt_output_path
== NULL
) {
802 alloc_path
= strdup(config_get_default_path());
803 if (alloc_path
== NULL
) {
804 ERR("Home path not found.\n \
805 Please specify an output path using -o, --output PATH");
810 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
811 "/%s", alloc_path
, path_name
);
813 PERROR("asprintf trace dir name");
817 full_path
= expand_full_path(opt_output_path
);
818 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
820 PERROR("asprintf trace dir name");
832 * relay_delete_session: Free all memory associated with a session and
836 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
838 struct lttng_ht_iter iter
;
839 struct lttng_ht_node_ulong
*node
;
840 struct relay_stream
*stream
;
846 DBG("Relay deleting session %lu", cmd
->session
->id
);
847 free(cmd
->session
->sock
);
849 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
850 node
= lttng_ht_iter_get_node_ulong(&iter
);
852 stream
= caa_container_of(node
,
853 struct relay_stream
, stream_n
);
854 if (stream
->session
== cmd
->session
) {
856 ret
= lttng_ht_del(streams_ht
, &iter
);
865 * relay_add_stream: allocate a new stream for a session
868 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
869 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
871 struct relay_session
*session
= cmd
->session
;
872 struct lttcomm_relayd_add_stream stream_info
;
873 struct relay_stream
*stream
= NULL
;
874 struct lttcomm_relayd_status_stream reply
;
875 char *path
= NULL
, *root_path
= NULL
;
878 if (!session
|| session
->version_check_done
== 0) {
879 ERR("Trying to add a stream before version check");
884 /* FIXME : use data_size for something ? */
885 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
886 sizeof(struct lttcomm_relayd_add_stream
), MSG_WAITALL
);
887 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
888 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
892 stream
= zmalloc(sizeof(struct relay_stream
));
893 if (stream
== NULL
) {
894 PERROR("relay stream zmalloc");
899 stream
->stream_handle
= ++last_relay_stream_id
;
901 stream
->session
= session
;
903 root_path
= create_output_path(stream_info
.pathname
);
908 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
911 ERR("relay creating output directory");
915 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
917 PERROR("asprintf stream path");
921 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
923 PERROR("Relay creating trace file");
928 DBG("Tracefile %s created", path
);
930 lttng_ht_node_init_ulong(&stream
->stream_n
,
931 (unsigned long) stream
->stream_handle
);
932 lttng_ht_add_unique_ulong(streams_ht
,
935 DBG("Relay new stream added %s", stream_info
.channel_name
);
940 /* send the session id to the client or a negative return code on error */
942 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
944 reply
.ret_code
= htobe32(LTTCOMM_OK
);
946 reply
.handle
= htobe64(stream
->stream_handle
);
947 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
948 sizeof(struct lttcomm_relayd_status_stream
), 0);
950 ERR("Relay sending stream id");
958 * relay_unknown_command: send -1 if received unknown command
961 void relay_unknown_command(struct relay_command
*cmd
)
963 struct lttcomm_relayd_generic_reply reply
;
966 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
967 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
968 sizeof(struct lttcomm_relayd_generic_reply
), 0);
970 ERR("Relay sending unknown command");
975 * relay_start: send an acknowledgment to the client to tell if we are
976 * ready to receive data. We are ready if a session is established.
979 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
980 struct relay_command
*cmd
)
982 int ret
= htobe32(LTTCOMM_OK
);
983 struct lttcomm_relayd_generic_reply reply
;
984 struct relay_session
*session
= cmd
->session
;
987 DBG("Trying to start the streaming without a session established");
988 ret
= htobe32(LTTCOMM_ERR
);
991 reply
.ret_code
= ret
;
992 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
993 sizeof(struct lttcomm_relayd_generic_reply
), 0);
995 ERR("Relay sending start ack");
1002 * Get stream from stream id.
1005 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
1006 struct lttng_ht
*streams_ht
)
1008 struct lttng_ht_node_ulong
*node
;
1009 struct lttng_ht_iter iter
;
1010 struct relay_stream
*ret
;
1012 lttng_ht_lookup(streams_ht
,
1013 (void *)((unsigned long) stream_id
),
1015 node
= lttng_ht_iter_get_node_ulong(&iter
);
1017 DBG("Relay stream %lu not found", stream_id
);
1022 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1029 * relay_recv_metadata: receive the metada for the session.
1032 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1033 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1035 int ret
= htobe32(LTTCOMM_OK
);
1036 struct relay_session
*session
= cmd
->session
;
1037 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1038 struct relay_stream
*metadata_stream
;
1039 uint64_t data_size
, payload_size
;
1042 ERR("Metadata sent before version check");
1047 data_size
= be64toh(recv_hdr
->data_size
);
1048 payload_size
= data_size
- sizeof(uint64_t);
1049 if (data_buffer_size
< data_size
) {
1050 data_buffer
= realloc(data_buffer
, data_size
);
1052 ERR("Allocating data buffer");
1056 data_buffer_size
= data_size
;
1058 memset(data_buffer
, 0, data_size
);
1059 DBG2("Relay receiving metadata, waiting for %lu bytes", data_size
);
1060 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1061 if (ret
< 0 || ret
!= data_size
) {
1063 ERR("Relay didn't receive the whole metadata");
1066 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1067 metadata_stream
= relay_stream_from_stream_id(
1068 be64toh(metadata_struct
->stream_id
), streams_ht
);
1069 if (!metadata_stream
) {
1074 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1076 if (ret
< (payload_size
)) {
1077 ERR("Relay error writing metadata on file");
1081 DBG2("Relay metadata written");
1088 * relay_send_version: send relayd version number
1091 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1092 struct relay_command
*cmd
)
1094 int ret
= htobe32(LTTCOMM_OK
);
1095 struct lttcomm_relayd_version reply
;
1096 struct relay_session
*session
= NULL
;
1098 if (cmd
->session
== NULL
) {
1099 session
= zmalloc(sizeof(struct relay_session
));
1100 if (session
== NULL
) {
1101 PERROR("relay session zmalloc");
1105 session
->id
= ++last_relay_session_id
;
1106 DBG("Created session %lu", session
->id
);
1107 cmd
->session
= session
;
1109 session
->version_check_done
= 1;
1111 sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1112 reply
.major
= htobe32(reply
.major
);
1113 reply
.minor
= htobe32(reply
.minor
);
1114 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1115 sizeof(struct lttcomm_relayd_version
), 0);
1117 ERR("Relay sending version");
1119 DBG("Version check done");
1126 * relay_process_control: Process the commands received on the control socket
1129 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1130 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1134 switch (be32toh(recv_hdr
->cmd
)) {
1136 case RELAYD_CREATE_SESSION:
1137 ret = relay_create_session(recv_hdr, cmd);
1140 case RELAYD_ADD_STREAM
:
1141 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1143 case RELAYD_START_DATA
:
1144 ret
= relay_start(recv_hdr
, cmd
);
1146 case RELAYD_SEND_METADATA
:
1147 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1149 case RELAYD_VERSION
:
1150 ret
= relay_send_version(recv_hdr
, cmd
);
1152 case RELAYD_UPDATE_SYNC_INFO
:
1154 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1155 relay_unknown_command(cmd
);
1165 * relay_process_data: Process the data received on the data socket
1168 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1171 struct relay_stream
*stream
;
1172 struct lttcomm_relayd_data_hdr data_hdr
;
1176 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1177 sizeof(struct lttcomm_relayd_data_hdr
), MSG_WAITALL
);
1179 ERR("Connections seems to be closed");
1184 stream_id
= be64toh(data_hdr
.stream_id
);
1185 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1191 data_size
= be32toh(data_hdr
.data_size
);
1192 if (data_buffer_size
< data_size
) {
1193 data_buffer
= realloc(data_buffer
, data_size
);
1195 ERR("Allocating data buffer");
1199 data_buffer_size
= data_size
;
1201 memset(data_buffer
, 0, data_size
);
1203 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1209 ret
= write(stream
->fd
, data_buffer
, data_size
);
1210 if (ret
< data_size
) {
1211 ERR("Relay error writing data to file");
1215 DBG2("Relay wrote %d bytes to tracefile for stream id %lu", ret
, stream
->stream_handle
);
1222 void relay_cleanup_connection(struct lttng_ht
*relay_connections_ht
, struct lttng_poll_event
*events
,
1223 struct lttng_ht
*streams_ht
, int pollfd
, struct lttng_ht_iter
*iter
)
1227 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1229 lttng_poll_del(events
, pollfd
);
1231 ret
= close(pollfd
);
1233 ERR("Closing pollfd %d", pollfd
);
1238 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1239 struct lttng_ht
*relay_connections_ht
)
1242 struct relay_command
*relay_connection
;
1244 relay_connection
= zmalloc(sizeof(struct relay_command
));
1245 if (relay_connection
== NULL
) {
1246 PERROR("Relay command zmalloc");
1250 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1251 if (ret
< 0 || ret
< sizeof(relay_connection
)) {
1252 PERROR("read relay cmd pipe");
1257 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1258 (unsigned long) relay_connection
->sock
->fd
);
1259 lttng_ht_add_unique_ulong(relay_connections_ht
,
1260 &relay_connection
->sock_n
);
1261 ret
= lttng_poll_add(events
,
1262 relay_connection
->sock
->fd
,
1263 LPOLLIN
| LPOLLRDHUP
);
1270 * This thread does the actual work
1273 void *relay_thread_worker(void *data
)
1276 uint32_t revents
, nb_fd
;
1277 struct relay_command
*relay_connection
;
1278 struct lttng_poll_event events
;
1279 struct lttng_ht
*relay_connections_ht
;
1280 struct lttng_ht_node_ulong
*node
;
1281 struct lttng_ht_iter iter
;
1282 struct lttng_ht
*streams_ht
;
1283 struct lttcomm_relayd_hdr recv_hdr
;
1285 DBG("[thread] Relay worker started");
1287 /* table of connections indexed on socket */
1288 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1290 /* tables of streams indexed by stream ID */
1291 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1293 ret
= create_thread_poll_set(&events
, 2);
1295 goto error_poll_create
;
1298 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1304 /* Zeroed the events structure */
1305 lttng_poll_reset(&events
);
1307 nb_fd
= LTTNG_POLL_GETNB(&events
);
1309 /* Infinite blocking call, waiting for transmission */
1311 ret
= lttng_poll_wait(&events
, -1);
1314 * Restart interrupted system call.
1316 if (errno
== EINTR
) {
1322 for (i
= 0; i
< nb_fd
; i
++) {
1323 /* Fetch once the poll data */
1324 revents
= LTTNG_POLL_GETEV(&events
, i
);
1325 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1327 /* Thread quit pipe has been closed. Killing thread. */
1328 ret
= check_thread_quit_pipe(pollfd
, revents
);
1333 /* Inspect the relay cmd pipe for new connection */
1334 if (pollfd
== relay_cmd_pipe
[0]) {
1335 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1336 ERR("Relay pipe error");
1338 } else if (revents
& LPOLLIN
) {
1339 DBG("Relay command received");
1340 ret
= relay_add_connection(relay_cmd_pipe
[0],
1341 &events
, relay_connections_ht
);
1346 } else if (revents
> 0) {
1347 lttng_ht_lookup(relay_connections_ht
,
1348 (void *)((unsigned long) pollfd
),
1350 node
= lttng_ht_iter_get_node_ulong(&iter
);
1352 DBG2("Relay sock %d not found", pollfd
);
1355 relay_connection
= caa_container_of(node
,
1356 struct relay_command
, sock_n
);
1358 if (revents
& (LPOLLERR
)) {
1360 relay_cleanup_connection(relay_connections_ht
,
1361 &events
, streams_ht
, pollfd
, &iter
);
1362 free(relay_connection
);
1363 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1364 DBG("Socket %d hung up", pollfd
);
1365 relay_cleanup_connection(relay_connections_ht
,
1366 &events
, streams_ht
, pollfd
, &iter
);
1367 if (relay_connection
->type
== RELAY_CONTROL
) {
1368 relay_delete_session(relay_connection
, streams_ht
);
1370 free(relay_connection
);
1371 } else if (revents
& LPOLLIN
) {
1372 /* control socket */
1373 if (relay_connection
->type
== RELAY_CONTROL
) {
1374 ret
= relay_connection
->sock
->ops
->recvmsg(
1375 relay_connection
->sock
, &recv_hdr
,
1376 sizeof(struct lttcomm_relayd_hdr
), MSG_WAITALL
);
1377 /* connection closed */
1379 relay_cleanup_connection(relay_connections_ht
,
1380 &events
, streams_ht
, pollfd
, &iter
);
1381 relay_delete_session(relay_connection
, streams_ht
);
1382 free(relay_connection
);
1383 DBG("Control connection closed with %d", pollfd
);
1385 if (relay_connection
->session
) {
1386 DBG2("Relay worker receiving data for session : %lu",
1387 relay_connection
->session
->id
);
1389 ret
= relay_process_control(&recv_hdr
,
1393 * there was an error in processing a control
1394 * command: clear the session
1397 relay_cleanup_connection(relay_connections_ht
,
1398 &events
, streams_ht
, pollfd
, &iter
);
1399 free(relay_connection
);
1400 DBG("Connection closed with %d", pollfd
);
1404 } else if (relay_connection
->type
== RELAY_DATA
) {
1405 ret
= relay_process_data(relay_connection
, streams_ht
);
1406 /* connection closed */
1408 relay_cleanup_connection(relay_connections_ht
,
1409 &events
, streams_ht
, pollfd
, &iter
);
1410 relay_delete_session(relay_connection
, streams_ht
);
1411 DBG("Data connection closed with %d", pollfd
);
1420 lttng_poll_clean(&events
);
1422 /* empty the hash table and free the memory */
1423 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1424 node
= lttng_ht_iter_get_node_ulong(&iter
);
1426 relay_connection
= caa_container_of(node
,
1427 struct relay_command
, sock_n
);
1428 free(relay_connection
);
1430 ret
= lttng_ht_del(relay_connections_ht
, &iter
);
1435 lttng_ht_destroy(relay_connections_ht
);
1436 DBG("Worker thread cleanup complete");
1442 * Create the relay command pipe to wake thread_manage_apps.
1443 * Closed in cleanup().
1445 static int create_relay_cmd_pipe(void)
1449 ret
= pipe(relay_cmd_pipe
);
1451 PERROR("relay cmd pipe");
1455 for (i
= 0; i
< 2; i
++) {
1456 ret
= fcntl(relay_cmd_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
1458 PERROR("fcntl relay_cmd_pipe");
1470 int main(int argc
, char **argv
)
1475 /* Create thread quit pipe */
1476 if ((ret
= init_thread_quit_pipe()) < 0) {
1480 /* Parse arguments */
1482 if ((ret
= parse_args(argc
, argv
) < 0)) {
1486 if ((ret
= set_signal_handler()) < 0) {
1499 /* Check if daemon is UID = 0 */
1500 is_root
= !getuid();
1503 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1504 ERR("Need to be root to use ports < 1024");
1510 /* Setup the thread apps communication pipe. */
1511 if ((ret
= create_relay_cmd_pipe()) < 0) {
1515 /* Init relay command queue. */
1516 cds_wfq_init(&relay_cmd_queue
.queue
);
1518 /* Set up max poll set size */
1519 lttng_poll_set_max_size();
1521 /* Setup the dispatcher thread */
1522 ret
= pthread_create(&dispatcher_thread
, NULL
,
1523 relay_thread_dispatcher
, (void *) NULL
);
1525 PERROR("pthread_create dispatcher");
1526 goto exit_dispatcher
;
1529 /* Setup the worker thread */
1530 ret
= pthread_create(&worker_thread
, NULL
,
1531 relay_thread_worker
, (void *) NULL
);
1533 PERROR("pthread_create worker");
1537 /* Setup the listener thread */
1538 ret
= pthread_create(&listener_thread
, NULL
,
1539 relay_thread_listener
, (void *) NULL
);
1541 PERROR("pthread_create listener");
1546 ret
= pthread_join(listener_thread
, &status
);
1548 PERROR("pthread_join");
1549 goto error
; /* join error, exit without cleanup */
1553 ret
= pthread_join(worker_thread
, &status
);
1555 PERROR("pthread_join");
1556 goto error
; /* join error, exit without cleanup */
1560 ret
= pthread_join(dispatcher_thread
, &status
);
1562 PERROR("pthread_join");
1563 goto error
; /* join error, exit without cleanup */