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
)
250 ret
= write(wpipe
, "!", 1);
251 } while (ret
< 0 && errno
== EINTR
);
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
= utils_create_pipe_cloexec(thread_quit_pipe
);
360 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
363 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
367 if (events
== NULL
|| size
== 0) {
372 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
378 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
390 * Check if the thread quit pipe was triggered.
392 * Return 1 if it was triggered else 0;
395 int check_thread_quit_pipe(int fd
, uint32_t events
)
397 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
405 * Create and init socket from uri.
408 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
411 struct lttcomm_sock
*sock
= NULL
;
413 sock
= lttcomm_alloc_sock_from_uri(uri
);
415 ERR("Allocating socket");
419 ret
= lttcomm_create_sock(sock
);
423 DBG("Listening on sock %d", sock
->fd
);
425 ret
= sock
->ops
->bind(sock
);
430 ret
= sock
->ops
->listen(sock
, -1);
440 lttcomm_destroy_sock(sock
);
446 * This thread manages the listening for new connections on the network
449 void *relay_thread_listener(void *data
)
453 uint32_t revents
, nb_fd
;
454 struct lttng_poll_event events
;
455 struct lttcomm_sock
*control_sock
, *data_sock
;
458 * Get allocated in this thread, enqueued to a global queue, dequeued and
459 * freed in the worker thread.
461 struct relay_command
*relay_cmd
= NULL
;
463 DBG("[thread] Relay listener started");
465 control_sock
= relay_init_sock(control_uri
);
470 data_sock
= relay_init_sock(data_uri
);
476 * Pass 3 as size here for the thread quit pipe, control and data socket.
478 ret
= create_thread_poll_set(&events
, 3);
480 goto error_create_poll
;
483 /* Add the control socket */
484 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
489 /* Add the data socket */
490 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
496 DBG("Listener accepting connections");
498 nb_fd
= LTTNG_POLL_GETNB(&events
);
501 ret
= lttng_poll_wait(&events
, -1);
504 * Restart interrupted system call.
506 if (errno
== EINTR
) {
512 DBG("Relay new connection received");
513 for (i
= 0; i
< nb_fd
; i
++) {
514 /* Fetch once the poll data */
515 revents
= LTTNG_POLL_GETEV(&events
, i
);
516 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
518 /* Thread quit pipe has been closed. Killing thread. */
519 ret
= check_thread_quit_pipe(pollfd
, revents
);
524 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
525 ERR("socket poll error");
527 } else if (revents
& LPOLLIN
) {
528 struct lttcomm_sock
*newsock
= NULL
;
530 relay_cmd
= zmalloc(sizeof(struct relay_command
));
531 if (relay_cmd
== NULL
) {
532 PERROR("relay command zmalloc");
536 if (pollfd
== data_sock
->fd
) {
537 newsock
= data_sock
->ops
->accept(data_sock
);
539 PERROR("accepting data sock");
542 relay_cmd
->type
= RELAY_DATA
;
543 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
544 } else if (pollfd
== control_sock
->fd
) {
545 newsock
= control_sock
->ops
->accept(control_sock
);
547 PERROR("accepting control sock");
550 relay_cmd
->type
= RELAY_CONTROL
;
551 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
553 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
556 PERROR("setsockopt inet");
559 relay_cmd
->sock
= newsock
;
561 * Lock free enqueue the request.
563 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
566 * Wake the dispatch queue futex. Implicit memory
567 * barrier with the exchange in cds_wfq_enqueue.
569 futex_nto1_wake(&relay_cmd_queue
.futex
);
576 lttng_poll_clean(&events
);
578 if (control_sock
->fd
>= 0) {
579 ret
= control_sock
->ops
->close(control_sock
);
583 lttcomm_destroy_sock(control_sock
);
585 if (data_sock
->fd
>= 0) {
586 ret
= data_sock
->ops
->close(data_sock
);
590 lttcomm_destroy_sock(data_sock
);
593 DBG("Relay listener thread cleanup complete");
600 * This thread manages the dispatching of the requests to worker threads
603 void *relay_thread_dispatcher(void *data
)
606 struct cds_wfq_node
*node
;
607 struct relay_command
*relay_cmd
= NULL
;
609 DBG("[thread] Relay dispatcher started");
611 while (!dispatch_thread_exit
) {
612 /* Atomically prepare the queue futex */
613 futex_nto1_prepare(&relay_cmd_queue
.futex
);
616 /* Dequeue commands */
617 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
619 DBG("Woken up but nothing in the relay command queue");
620 /* Continue thread execution */
624 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
625 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
628 * Inform worker thread of the new request. This
629 * call is blocking so we can be assured that the data will be read
630 * at some point in time or wait to the end of the world :)
633 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
634 sizeof(struct relay_command
));
635 } while (ret
< 0 && errno
== EINTR
);
638 PERROR("write cmd pipe");
641 } while (node
!= NULL
);
643 /* Futex wait on queue. Blocking call on futex() */
644 futex_nto1_wait(&relay_cmd_queue
.futex
);
648 DBG("Dispatch thread dying");
654 * Return the realpath(3) of the path even if the last directory token does not
655 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
656 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
657 * fails if the end point directory does not exist.
660 char *expand_full_path(const char *path
)
662 const char *end_path
= path
;
663 char *next
, *cut_path
, *expanded_path
;
665 /* Find last token delimited by '/' */
666 while ((next
= strpbrk(end_path
+ 1, "/"))) {
670 /* Cut last token from original path */
671 cut_path
= strndup(path
, end_path
- path
);
673 expanded_path
= malloc(PATH_MAX
);
674 if (expanded_path
== NULL
) {
678 expanded_path
= realpath((char *)cut_path
, expanded_path
);
679 if (expanded_path
== NULL
) {
682 ERR("%s: No such file or directory", cut_path
);
691 /* Add end part to expanded path */
692 strcat(expanded_path
, end_path
);
695 return expanded_path
;
704 * config_get_default_path
706 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
709 char *config_get_default_path(void)
711 return getenv("HOME");
715 * Create recursively directory using the FULL path.
718 int mkdir_recursive(char *path
, mode_t mode
)
720 char *p
, tmp
[PATH_MAX
];
725 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
727 PERROR("snprintf mkdir");
732 if (tmp
[len
- 1] == '/') {
736 for (p
= tmp
+ 1; *p
; p
++) {
739 if (tmp
[strlen(tmp
) - 1] == '.' &&
740 tmp
[strlen(tmp
) - 2] == '.' &&
741 tmp
[strlen(tmp
) - 3] == '/') {
742 ERR("Using '/../' is not permitted in the trace path (%s)",
747 ret
= stat(tmp
, &statbuf
);
749 ret
= mkdir(tmp
, mode
);
751 if (!(errno
== EEXIST
)) {
752 PERROR("mkdir recursive");
762 ret
= mkdir(tmp
, mode
);
764 if (!(errno
== EEXIST
)) {
765 PERROR("mkdir recursive last piece");
777 * create_output_path: create the output trace directory
780 char *create_output_path(char *path_name
)
783 char *alloc_path
= NULL
;
784 char *traces_path
= NULL
;
785 char *full_path
= NULL
;
787 /* Auto output path */
788 if (opt_output_path
== NULL
) {
789 alloc_path
= strdup(config_get_default_path());
790 if (alloc_path
== NULL
) {
791 ERR("Home path not found.\n \
792 Please specify an output path using -o, --output PATH");
797 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
798 "/%s", alloc_path
, path_name
);
800 PERROR("asprintf trace dir name");
804 full_path
= expand_full_path(opt_output_path
);
805 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
807 PERROR("asprintf trace dir name");
819 * relay_delete_session: Free all memory associated with a session and
823 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
825 struct lttng_ht_iter iter
;
826 struct lttng_ht_node_ulong
*node
;
827 struct relay_stream
*stream
;
833 DBG("Relay deleting session %lu", cmd
->session
->id
);
834 free(cmd
->session
->sock
);
836 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
837 node
= lttng_ht_iter_get_node_ulong(&iter
);
839 stream
= caa_container_of(node
,
840 struct relay_stream
, stream_n
);
841 if (stream
->session
== cmd
->session
) {
843 ret
= lttng_ht_del(streams_ht
, &iter
);
852 * relay_add_stream: allocate a new stream for a session
855 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
856 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
858 struct relay_session
*session
= cmd
->session
;
859 struct lttcomm_relayd_add_stream stream_info
;
860 struct relay_stream
*stream
= NULL
;
861 struct lttcomm_relayd_status_stream reply
;
862 char *path
= NULL
, *root_path
= NULL
;
865 if (!session
|| session
->version_check_done
== 0) {
866 ERR("Trying to add a stream before version check");
871 /* FIXME : use data_size for something ? */
872 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
873 sizeof(struct lttcomm_relayd_add_stream
), MSG_WAITALL
);
874 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
875 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
879 stream
= zmalloc(sizeof(struct relay_stream
));
880 if (stream
== NULL
) {
881 PERROR("relay stream zmalloc");
886 stream
->stream_handle
= ++last_relay_stream_id
;
888 stream
->session
= session
;
890 root_path
= create_output_path(stream_info
.pathname
);
895 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
898 ERR("relay creating output directory");
902 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
904 PERROR("asprintf stream path");
908 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
910 PERROR("Relay creating trace file");
915 DBG("Tracefile %s created", path
);
917 lttng_ht_node_init_ulong(&stream
->stream_n
,
918 (unsigned long) stream
->stream_handle
);
919 lttng_ht_add_unique_ulong(streams_ht
,
922 DBG("Relay new stream added %s", stream_info
.channel_name
);
927 /* send the session id to the client or a negative return code on error */
929 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
931 reply
.ret_code
= htobe32(LTTCOMM_OK
);
933 reply
.handle
= htobe64(stream
->stream_handle
);
934 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
935 sizeof(struct lttcomm_relayd_status_stream
), 0);
937 ERR("Relay sending stream id");
945 * relay_unknown_command: send -1 if received unknown command
948 void relay_unknown_command(struct relay_command
*cmd
)
950 struct lttcomm_relayd_generic_reply reply
;
953 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
954 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
955 sizeof(struct lttcomm_relayd_generic_reply
), 0);
957 ERR("Relay sending unknown command");
962 * relay_start: send an acknowledgment to the client to tell if we are
963 * ready to receive data. We are ready if a session is established.
966 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
967 struct relay_command
*cmd
)
969 int ret
= htobe32(LTTCOMM_OK
);
970 struct lttcomm_relayd_generic_reply reply
;
971 struct relay_session
*session
= cmd
->session
;
974 DBG("Trying to start the streaming without a session established");
975 ret
= htobe32(LTTCOMM_ERR
);
978 reply
.ret_code
= ret
;
979 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
980 sizeof(struct lttcomm_relayd_generic_reply
), 0);
982 ERR("Relay sending start ack");
989 * Get stream from stream id.
992 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
993 struct lttng_ht
*streams_ht
)
995 struct lttng_ht_node_ulong
*node
;
996 struct lttng_ht_iter iter
;
997 struct relay_stream
*ret
;
999 lttng_ht_lookup(streams_ht
,
1000 (void *)((unsigned long) stream_id
),
1002 node
= lttng_ht_iter_get_node_ulong(&iter
);
1004 DBG("Relay stream %lu not found", stream_id
);
1009 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1016 * relay_recv_metadata: receive the metada for the session.
1019 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1020 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1022 int ret
= htobe32(LTTCOMM_OK
);
1023 struct relay_session
*session
= cmd
->session
;
1024 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1025 struct relay_stream
*metadata_stream
;
1026 uint64_t data_size
, payload_size
;
1029 ERR("Metadata sent before version check");
1034 data_size
= be64toh(recv_hdr
->data_size
);
1035 payload_size
= data_size
- sizeof(uint64_t);
1036 if (data_buffer_size
< data_size
) {
1037 data_buffer
= realloc(data_buffer
, data_size
);
1039 ERR("Allocating data buffer");
1043 data_buffer_size
= data_size
;
1045 memset(data_buffer
, 0, data_size
);
1046 DBG2("Relay receiving metadata, waiting for %lu bytes", data_size
);
1047 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1048 if (ret
< 0 || ret
!= data_size
) {
1050 ERR("Relay didn't receive the whole metadata");
1053 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1054 metadata_stream
= relay_stream_from_stream_id(
1055 be64toh(metadata_struct
->stream_id
), streams_ht
);
1056 if (!metadata_stream
) {
1062 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1064 } while (ret
< 0 && errno
== EINTR
);
1065 if (ret
< (payload_size
)) {
1066 ERR("Relay error writing metadata on file");
1070 DBG2("Relay metadata written");
1077 * relay_send_version: send relayd version number
1080 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1081 struct relay_command
*cmd
)
1083 int ret
= htobe32(LTTCOMM_OK
);
1084 struct lttcomm_relayd_version reply
;
1085 struct relay_session
*session
= NULL
;
1087 if (cmd
->session
== NULL
) {
1088 session
= zmalloc(sizeof(struct relay_session
));
1089 if (session
== NULL
) {
1090 PERROR("relay session zmalloc");
1094 session
->id
= ++last_relay_session_id
;
1095 DBG("Created session %lu", session
->id
);
1096 cmd
->session
= session
;
1098 session
->version_check_done
= 1;
1100 sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1101 reply
.major
= htobe32(reply
.major
);
1102 reply
.minor
= htobe32(reply
.minor
);
1103 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1104 sizeof(struct lttcomm_relayd_version
), 0);
1106 ERR("Relay sending version");
1108 DBG("Version check done");
1115 * relay_process_control: Process the commands received on the control socket
1118 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1119 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1123 switch (be32toh(recv_hdr
->cmd
)) {
1125 case RELAYD_CREATE_SESSION:
1126 ret = relay_create_session(recv_hdr, cmd);
1129 case RELAYD_ADD_STREAM
:
1130 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1132 case RELAYD_START_DATA
:
1133 ret
= relay_start(recv_hdr
, cmd
);
1135 case RELAYD_SEND_METADATA
:
1136 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1138 case RELAYD_VERSION
:
1139 ret
= relay_send_version(recv_hdr
, cmd
);
1141 case RELAYD_UPDATE_SYNC_INFO
:
1143 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1144 relay_unknown_command(cmd
);
1154 * relay_process_data: Process the data received on the data socket
1157 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1160 struct relay_stream
*stream
;
1161 struct lttcomm_relayd_data_hdr data_hdr
;
1165 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1166 sizeof(struct lttcomm_relayd_data_hdr
), MSG_WAITALL
);
1168 ERR("Connections seems to be closed");
1173 stream_id
= be64toh(data_hdr
.stream_id
);
1174 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1180 data_size
= be32toh(data_hdr
.data_size
);
1181 if (data_buffer_size
< data_size
) {
1182 data_buffer
= realloc(data_buffer
, data_size
);
1184 ERR("Allocating data buffer");
1188 data_buffer_size
= data_size
;
1190 memset(data_buffer
, 0, data_size
);
1192 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1199 ret
= write(stream
->fd
, data_buffer
, data_size
);
1200 } while (ret
< 0 && errno
== EINTR
);
1201 if (ret
< data_size
) {
1202 ERR("Relay error writing data to file");
1206 DBG2("Relay wrote %d bytes to tracefile for stream id %lu", ret
, stream
->stream_handle
);
1213 void relay_cleanup_connection(struct lttng_ht
*relay_connections_ht
, struct lttng_poll_event
*events
,
1214 struct lttng_ht
*streams_ht
, int pollfd
, struct lttng_ht_iter
*iter
)
1218 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1220 lttng_poll_del(events
, pollfd
);
1222 ret
= close(pollfd
);
1224 ERR("Closing pollfd %d", pollfd
);
1229 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1230 struct lttng_ht
*relay_connections_ht
)
1233 struct relay_command
*relay_connection
;
1235 relay_connection
= zmalloc(sizeof(struct relay_command
));
1236 if (relay_connection
== NULL
) {
1237 PERROR("Relay command zmalloc");
1241 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1242 if (ret
< 0 || ret
< sizeof(relay_connection
)) {
1243 PERROR("read relay cmd pipe");
1248 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1249 (unsigned long) relay_connection
->sock
->fd
);
1250 lttng_ht_add_unique_ulong(relay_connections_ht
,
1251 &relay_connection
->sock_n
);
1252 ret
= lttng_poll_add(events
,
1253 relay_connection
->sock
->fd
,
1254 LPOLLIN
| LPOLLRDHUP
);
1261 * This thread does the actual work
1264 void *relay_thread_worker(void *data
)
1267 uint32_t revents
, nb_fd
;
1268 struct relay_command
*relay_connection
;
1269 struct lttng_poll_event events
;
1270 struct lttng_ht
*relay_connections_ht
;
1271 struct lttng_ht_node_ulong
*node
;
1272 struct lttng_ht_iter iter
;
1273 struct lttng_ht
*streams_ht
;
1274 struct lttcomm_relayd_hdr recv_hdr
;
1276 DBG("[thread] Relay worker started");
1278 /* table of connections indexed on socket */
1279 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1281 /* tables of streams indexed by stream ID */
1282 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1284 ret
= create_thread_poll_set(&events
, 2);
1286 goto error_poll_create
;
1289 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1295 /* Zeroed the events structure */
1296 lttng_poll_reset(&events
);
1298 nb_fd
= LTTNG_POLL_GETNB(&events
);
1300 /* Infinite blocking call, waiting for transmission */
1302 ret
= lttng_poll_wait(&events
, -1);
1305 * Restart interrupted system call.
1307 if (errno
== EINTR
) {
1313 for (i
= 0; i
< nb_fd
; i
++) {
1314 /* Fetch once the poll data */
1315 revents
= LTTNG_POLL_GETEV(&events
, i
);
1316 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1318 /* Thread quit pipe has been closed. Killing thread. */
1319 ret
= check_thread_quit_pipe(pollfd
, revents
);
1324 /* Inspect the relay cmd pipe for new connection */
1325 if (pollfd
== relay_cmd_pipe
[0]) {
1326 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1327 ERR("Relay pipe error");
1329 } else if (revents
& LPOLLIN
) {
1330 DBG("Relay command received");
1331 ret
= relay_add_connection(relay_cmd_pipe
[0],
1332 &events
, relay_connections_ht
);
1337 } else if (revents
> 0) {
1338 lttng_ht_lookup(relay_connections_ht
,
1339 (void *)((unsigned long) pollfd
),
1341 node
= lttng_ht_iter_get_node_ulong(&iter
);
1343 DBG2("Relay sock %d not found", pollfd
);
1346 relay_connection
= caa_container_of(node
,
1347 struct relay_command
, sock_n
);
1349 if (revents
& (LPOLLERR
)) {
1351 relay_cleanup_connection(relay_connections_ht
,
1352 &events
, streams_ht
, pollfd
, &iter
);
1353 free(relay_connection
);
1354 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1355 DBG("Socket %d hung up", pollfd
);
1356 relay_cleanup_connection(relay_connections_ht
,
1357 &events
, streams_ht
, pollfd
, &iter
);
1358 if (relay_connection
->type
== RELAY_CONTROL
) {
1359 relay_delete_session(relay_connection
, streams_ht
);
1361 free(relay_connection
);
1362 } else if (revents
& LPOLLIN
) {
1363 /* control socket */
1364 if (relay_connection
->type
== RELAY_CONTROL
) {
1365 ret
= relay_connection
->sock
->ops
->recvmsg(
1366 relay_connection
->sock
, &recv_hdr
,
1367 sizeof(struct lttcomm_relayd_hdr
), MSG_WAITALL
);
1368 /* connection closed */
1370 relay_cleanup_connection(relay_connections_ht
,
1371 &events
, streams_ht
, pollfd
, &iter
);
1372 relay_delete_session(relay_connection
, streams_ht
);
1373 free(relay_connection
);
1374 DBG("Control connection closed with %d", pollfd
);
1376 if (relay_connection
->session
) {
1377 DBG2("Relay worker receiving data for session : %lu",
1378 relay_connection
->session
->id
);
1380 ret
= relay_process_control(&recv_hdr
,
1384 * there was an error in processing a control
1385 * command: clear the session
1388 relay_cleanup_connection(relay_connections_ht
,
1389 &events
, streams_ht
, pollfd
, &iter
);
1390 free(relay_connection
);
1391 DBG("Connection closed with %d", pollfd
);
1395 } else if (relay_connection
->type
== RELAY_DATA
) {
1396 ret
= relay_process_data(relay_connection
, streams_ht
);
1397 /* connection closed */
1399 relay_cleanup_connection(relay_connections_ht
,
1400 &events
, streams_ht
, pollfd
, &iter
);
1401 relay_delete_session(relay_connection
, streams_ht
);
1402 DBG("Data connection closed with %d", pollfd
);
1411 lttng_poll_clean(&events
);
1413 /* empty the hash table and free the memory */
1414 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1415 node
= lttng_ht_iter_get_node_ulong(&iter
);
1417 relay_connection
= caa_container_of(node
,
1418 struct relay_command
, sock_n
);
1419 free(relay_connection
);
1421 ret
= lttng_ht_del(relay_connections_ht
, &iter
);
1426 lttng_ht_destroy(relay_connections_ht
);
1427 DBG("Worker thread cleanup complete");
1433 * Create the relay command pipe to wake thread_manage_apps.
1434 * Closed in cleanup().
1436 static int create_relay_cmd_pipe(void)
1440 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
1448 int main(int argc
, char **argv
)
1453 /* Create thread quit pipe */
1454 if ((ret
= init_thread_quit_pipe()) < 0) {
1458 /* Parse arguments */
1460 if ((ret
= parse_args(argc
, argv
) < 0)) {
1464 if ((ret
= set_signal_handler()) < 0) {
1477 /* Check if daemon is UID = 0 */
1478 is_root
= !getuid();
1481 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1482 ERR("Need to be root to use ports < 1024");
1488 /* Setup the thread apps communication pipe. */
1489 if ((ret
= create_relay_cmd_pipe()) < 0) {
1493 /* Init relay command queue. */
1494 cds_wfq_init(&relay_cmd_queue
.queue
);
1496 /* Set up max poll set size */
1497 lttng_poll_set_max_size();
1499 /* Setup the dispatcher thread */
1500 ret
= pthread_create(&dispatcher_thread
, NULL
,
1501 relay_thread_dispatcher
, (void *) NULL
);
1503 PERROR("pthread_create dispatcher");
1504 goto exit_dispatcher
;
1507 /* Setup the worker thread */
1508 ret
= pthread_create(&worker_thread
, NULL
,
1509 relay_thread_worker
, (void *) NULL
);
1511 PERROR("pthread_create worker");
1515 /* Setup the listener thread */
1516 ret
= pthread_create(&listener_thread
, NULL
,
1517 relay_thread_listener
, (void *) NULL
);
1519 PERROR("pthread_create listener");
1524 ret
= pthread_join(listener_thread
, &status
);
1526 PERROR("pthread_join");
1527 goto error
; /* join error, exit without cleanup */
1531 ret
= pthread_join(worker_thread
, &status
);
1533 PERROR("pthread_join");
1534 goto error
; /* join error, exit without cleanup */
1538 ret
= pthread_join(dispatcher_thread
, &status
);
1540 PERROR("pthread_join");
1541 goto error
; /* join error, exit without cleanup */