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
;
60 static struct lttng_uri
*data_uri
;
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 /* Shared between threads */
78 static int dispatch_thread_exit
;
80 static pthread_t listener_thread
;
81 static pthread_t dispatcher_thread
;
82 static pthread_t worker_thread
;
84 static uint64_t last_relay_stream_id
;
85 static uint64_t last_relay_session_id
;
88 * Relay command queue.
90 * The relay_thread_listener and relay_thread_dispatcher communicate with this
93 static struct relay_cmd_queue relay_cmd_queue
;
95 /* buffer allocated at startup, used to store the trace data */
96 static char *data_buffer
;
97 static unsigned int data_buffer_size
;
100 * usage function on stderr
105 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
106 fprintf(stderr
, " -h, --help Display this usage.\n");
107 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
108 fprintf(stderr
, " -C, --control-port Control port listening (URI)\n");
109 fprintf(stderr
, " -D, --data-port Data port listening (URI)\n");
110 fprintf(stderr
, " -o, --output Output path for traces (PATH)\n");
111 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
115 int parse_args(int argc
, char **argv
)
119 char *default_address
;
121 static struct option long_options
[] = {
122 { "control-port", 1, 0, 'C', },
123 { "data-port", 1, 0, 'D', },
124 { "daemonize", 0, 0, 'd', },
125 { "help", 0, 0, 'h', },
126 { "output", 1, 0, 'o', },
127 { "verbose", 0, 0, 'v', },
132 int option_index
= 0;
133 c
= getopt_long(argc
, argv
, "dhv" "C:D:o:",
134 long_options
, &option_index
);
141 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
143 fprintf(stderr
, " with arg %s\n", optarg
);
147 ret
= uri_parse(optarg
, &control_uri
);
149 ERR("Invalid control URI specified");
152 if (control_uri
->port
== 0) {
153 control_uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
157 ret
= uri_parse(optarg
, &data_uri
);
159 ERR("Invalid data URI specified");
162 if (data_uri
->port
== 0) {
163 data_uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
173 ret
= asprintf(&opt_output_path
, "%s", optarg
);
175 PERROR("asprintf opt_output_path");
180 /* Verbose level can increase using multiple -v */
181 lttng_opt_verbose
+= 1;
184 /* Unknown option or other error.
185 * Error is printed by getopt, just return */
191 /* assign default values */
192 if (control_uri
== NULL
) {
193 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
194 DEFAULT_NETWORK_CONTROL_PORT
);
196 PERROR("asprintf default data address");
200 ret
= uri_parse(default_address
, &control_uri
);
201 free(default_address
);
203 ERR("Invalid control URI specified");
207 if (data_uri
== NULL
) {
208 ret
= asprintf(&default_address
, "tcp://0.0.0.0:%d",
209 DEFAULT_NETWORK_DATA_PORT
);
211 PERROR("asprintf default data address");
215 ret
= uri_parse(default_address
, &data_uri
);
216 free(default_address
);
218 ERR("Invalid data URI specified");
235 /* free the dynamically allocated opt_output_path */
236 free(opt_output_path
);
238 /* Close thread quit pipes */
239 utils_close_pipe(thread_quit_pipe
);
241 /* Close relay cmd pipes */
242 utils_close_pipe(relay_cmd_pipe
);
246 * Write to writable pipe used to notify a thread.
249 int notify_thread_pipe(int wpipe
)
254 ret
= write(wpipe
, "!", 1);
255 } while (ret
< 0 && errno
== EINTR
);
257 PERROR("write poll pipe");
264 * Stop all threads by closing the thread quit pipe.
267 void stop_threads(void)
271 /* Stopping all threads */
272 DBG("Terminating all threads");
273 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
275 ERR("write error on thread quit pipe");
278 /* Dispatch thread */
279 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
280 futex_nto1_wake(&relay_cmd_queue
.futex
);
284 * Signal handler for the daemon
286 * Simply stop all worker threads, leaving main() return gracefully after
287 * joining all threads and calling cleanup().
290 void sighandler(int sig
)
294 DBG("SIGPIPE caught");
297 DBG("SIGINT caught");
301 DBG("SIGTERM caught");
310 * Setup signal handler for :
311 * SIGINT, SIGTERM, SIGPIPE
314 int set_signal_handler(void)
320 if ((ret
= sigemptyset(&sigset
)) < 0) {
321 PERROR("sigemptyset");
325 sa
.sa_handler
= sighandler
;
328 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
333 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
338 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
343 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
349 * Init thread quit pipe.
351 * Return -1 on error or 0 if all pipes are created.
354 int init_thread_quit_pipe(void)
358 ret
= utils_create_pipe_cloexec(thread_quit_pipe
);
364 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
367 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
371 if (events
== NULL
|| size
== 0) {
376 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
382 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
394 * Check if the thread quit pipe was triggered.
396 * Return 1 if it was triggered else 0;
399 int check_thread_quit_pipe(int fd
, uint32_t events
)
401 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
409 * Create and init socket from uri.
412 struct lttcomm_sock
*relay_init_sock(struct lttng_uri
*uri
)
415 struct lttcomm_sock
*sock
= NULL
;
417 sock
= lttcomm_alloc_sock_from_uri(uri
);
419 ERR("Allocating socket");
423 ret
= lttcomm_create_sock(sock
);
427 DBG("Listening on sock %d", sock
->fd
);
429 ret
= sock
->ops
->bind(sock
);
434 ret
= sock
->ops
->listen(sock
, -1);
444 lttcomm_destroy_sock(sock
);
450 * This thread manages the listening for new connections on the network
453 void *relay_thread_listener(void *data
)
455 int i
, ret
, pollfd
, err
= -1;
457 uint32_t revents
, nb_fd
;
458 struct lttng_poll_event events
;
459 struct lttcomm_sock
*control_sock
, *data_sock
;
462 * Get allocated in this thread, enqueued to a global queue, dequeued and
463 * freed in the worker thread.
465 struct relay_command
*relay_cmd
= NULL
;
467 DBG("[thread] Relay listener started");
469 control_sock
= relay_init_sock(control_uri
);
471 goto error_sock_control
;
474 data_sock
= relay_init_sock(data_uri
);
476 goto error_sock_relay
;
480 * Pass 3 as size here for the thread quit pipe, control and data socket.
482 ret
= create_thread_poll_set(&events
, 3);
484 goto error_create_poll
;
487 /* Add the control socket */
488 ret
= lttng_poll_add(&events
, control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
493 /* Add the data socket */
494 ret
= lttng_poll_add(&events
, data_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
500 DBG("Listener accepting connections");
502 nb_fd
= LTTNG_POLL_GETNB(&events
);
505 ret
= lttng_poll_wait(&events
, -1);
508 * Restart interrupted system call.
510 if (errno
== EINTR
) {
516 DBG("Relay new connection received");
517 for (i
= 0; i
< nb_fd
; i
++) {
518 /* Fetch once the poll data */
519 revents
= LTTNG_POLL_GETEV(&events
, i
);
520 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
522 /* Thread quit pipe has been closed. Killing thread. */
523 ret
= check_thread_quit_pipe(pollfd
, revents
);
529 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
530 ERR("socket poll error");
532 } else if (revents
& LPOLLIN
) {
533 struct lttcomm_sock
*newsock
= NULL
;
535 relay_cmd
= zmalloc(sizeof(struct relay_command
));
536 if (relay_cmd
== NULL
) {
537 PERROR("relay command zmalloc");
541 if (pollfd
== data_sock
->fd
) {
542 newsock
= data_sock
->ops
->accept(data_sock
);
544 PERROR("accepting data sock");
547 relay_cmd
->type
= RELAY_DATA
;
548 DBG("Relay data connection accepted, socket %d", newsock
->fd
);
549 } else if (pollfd
== control_sock
->fd
) {
550 newsock
= control_sock
->ops
->accept(control_sock
);
552 PERROR("accepting control sock");
555 relay_cmd
->type
= RELAY_CONTROL
;
556 DBG("Relay control connection accepted, socket %d", newsock
->fd
);
558 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
561 PERROR("setsockopt inet");
564 relay_cmd
->sock
= newsock
;
566 * Lock free enqueue the request.
568 cds_wfq_enqueue(&relay_cmd_queue
.queue
, &relay_cmd
->node
);
571 * Wake the dispatch queue futex. Implicit memory
572 * barrier with the exchange in cds_wfq_enqueue.
574 futex_nto1_wake(&relay_cmd_queue
.futex
);
582 lttng_poll_clean(&events
);
584 if (data_sock
->fd
>= 0) {
585 ret
= data_sock
->ops
->close(data_sock
);
590 lttcomm_destroy_sock(data_sock
);
592 if (control_sock
->fd
>= 0) {
593 ret
= control_sock
->ops
->close(control_sock
);
598 lttcomm_destroy_sock(control_sock
);
601 DBG("Thread exited with error");
603 DBG("Relay listener thread cleanup complete");
609 * This thread manages the dispatching of the requests to worker threads
612 void *relay_thread_dispatcher(void *data
)
615 struct cds_wfq_node
*node
;
616 struct relay_command
*relay_cmd
= NULL
;
618 DBG("[thread] Relay dispatcher started");
620 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
621 /* Atomically prepare the queue futex */
622 futex_nto1_prepare(&relay_cmd_queue
.futex
);
625 /* Dequeue commands */
626 node
= cds_wfq_dequeue_blocking(&relay_cmd_queue
.queue
);
628 DBG("Woken up but nothing in the relay command queue");
629 /* Continue thread execution */
633 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
634 DBG("Dispatching request waiting on sock %d", relay_cmd
->sock
->fd
);
637 * Inform worker thread of the new request. This
638 * call is blocking so we can be assured that the data will be read
639 * at some point in time or wait to the end of the world :)
642 ret
= write(relay_cmd_pipe
[1], relay_cmd
,
643 sizeof(struct relay_command
));
644 } while (ret
< 0 && errno
== EINTR
);
647 PERROR("write cmd pipe");
650 } while (node
!= NULL
);
652 /* Futex wait on queue. Blocking call on futex() */
653 futex_nto1_wait(&relay_cmd_queue
.futex
);
657 DBG("Dispatch thread dying");
663 * Return the realpath(3) of the path even if the last directory token does not
664 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
665 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
666 * fails if the end point directory does not exist.
669 char *expand_full_path(const char *path
)
671 const char *end_path
= path
;
672 char *next
, *cut_path
, *expanded_path
, *respath
;
674 /* Find last token delimited by '/' */
675 while ((next
= strpbrk(end_path
+ 1, "/"))) {
679 /* Cut last token from original path */
680 cut_path
= strndup(path
, end_path
- path
);
682 expanded_path
= malloc(PATH_MAX
);
683 if (expanded_path
== NULL
) {
688 respath
= realpath(cut_path
, expanded_path
);
689 if (respath
== NULL
) {
692 ERR("%s: No such file or directory", cut_path
);
700 /* Add end part to expanded path */
701 strcat(respath
, end_path
);
710 * config_get_default_path
712 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
715 char *config_get_default_path(void)
717 return getenv("HOME");
721 * Create recursively directory using the FULL path.
724 int mkdir_recursive(char *path
, mode_t mode
)
726 char *p
, tmp
[PATH_MAX
];
731 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
733 PERROR("snprintf mkdir");
738 if (tmp
[len
- 1] == '/') {
742 for (p
= tmp
+ 1; *p
; p
++) {
745 if (tmp
[strlen(tmp
) - 1] == '.' &&
746 tmp
[strlen(tmp
) - 2] == '.' &&
747 tmp
[strlen(tmp
) - 3] == '/') {
748 ERR("Using '/../' is not permitted in the trace path (%s)",
753 ret
= stat(tmp
, &statbuf
);
755 ret
= mkdir(tmp
, mode
);
757 if (errno
!= EEXIST
) {
758 PERROR("mkdir recursive");
768 ret
= mkdir(tmp
, mode
);
770 if (errno
!= EEXIST
) {
771 PERROR("mkdir recursive last piece");
783 char *create_output_path_auto(char *path_name
)
786 char *traces_path
= NULL
;
787 char *alloc_path
= NULL
;
790 default_path
= config_get_default_path();
791 if (default_path
== NULL
) {
792 ERR("Home path not found.\n \
793 Please specify an output path using -o, --output PATH");
796 alloc_path
= strdup(default_path
);
797 if (alloc_path
== NULL
) {
798 PERROR("Path allocation");
801 ret
= asprintf(&traces_path
, "%s/" DEFAULT_TRACE_DIR_NAME
802 "/%s", alloc_path
, path_name
);
804 PERROR("asprintf trace dir name");
813 char *create_output_path_noauto(char *path_name
)
816 char *traces_path
= NULL
;
819 full_path
= expand_full_path(opt_output_path
);
820 ret
= asprintf(&traces_path
, "%s/%s", full_path
, path_name
);
822 PERROR("asprintf trace dir name");
831 * create_output_path: create the output trace directory
834 char *create_output_path(char *path_name
)
836 if (opt_output_path
== NULL
) {
837 return create_output_path_auto(path_name
);
839 return create_output_path_noauto(path_name
);
844 * relay_delete_session: Free all memory associated with a session and
848 void relay_delete_session(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
850 struct lttng_ht_iter iter
;
851 struct lttng_ht_node_ulong
*node
;
852 struct relay_stream
*stream
;
859 DBG("Relay deleting session %lu", cmd
->session
->id
);
860 free(cmd
->session
->sock
);
862 cds_lfht_for_each_entry(streams_ht
->ht
, &iter
.iter
, node
, node
) {
863 node
= lttng_ht_iter_get_node_ulong(&iter
);
865 stream
= caa_container_of(node
,
866 struct relay_stream
, stream_n
);
867 if (stream
->session
== cmd
->session
) {
869 ret
= lttng_ht_del(streams_ht
, &iter
);
878 * relay_add_stream: allocate a new stream for a session
881 int relay_add_stream(struct lttcomm_relayd_hdr
*recv_hdr
,
882 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
884 struct relay_session
*session
= cmd
->session
;
885 struct lttcomm_relayd_add_stream stream_info
;
886 struct relay_stream
*stream
= NULL
;
887 struct lttcomm_relayd_status_stream reply
;
888 char *path
= NULL
, *root_path
= NULL
;
891 if (!session
|| session
->version_check_done
== 0) {
892 ERR("Trying to add a stream before version check");
897 /* FIXME : use data_size for something ? */
898 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &stream_info
,
899 sizeof(struct lttcomm_relayd_add_stream
), MSG_WAITALL
);
900 if (ret
< sizeof(struct lttcomm_relayd_add_stream
)) {
901 ERR("Relay didn't receive valid add_stream struct size : %d", ret
);
905 stream
= zmalloc(sizeof(struct relay_stream
));
906 if (stream
== NULL
) {
907 PERROR("relay stream zmalloc");
912 stream
->stream_handle
= ++last_relay_stream_id
;
914 stream
->session
= session
;
916 root_path
= create_output_path(stream_info
.pathname
);
921 ret
= mkdir_recursive(root_path
, S_IRWXU
| S_IRWXG
);
923 ERR("relay creating output directory");
927 ret
= asprintf(&path
, "%s/%s", root_path
, stream_info
.channel_name
);
929 PERROR("asprintf stream path");
933 ret
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
935 PERROR("Relay creating trace file");
940 DBG("Tracefile %s created", path
);
942 lttng_ht_node_init_ulong(&stream
->stream_n
,
943 (unsigned long) stream
->stream_handle
);
944 lttng_ht_add_unique_ulong(streams_ht
,
947 DBG("Relay new stream added %s", stream_info
.channel_name
);
952 /* send the session id to the client or a negative return code on error */
954 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
956 reply
.ret_code
= htobe32(LTTCOMM_OK
);
958 reply
.handle
= htobe64(stream
->stream_handle
);
959 send_ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
960 sizeof(struct lttcomm_relayd_status_stream
), 0);
962 ERR("Relay sending stream id");
970 * relay_unknown_command: send -1 if received unknown command
973 void relay_unknown_command(struct relay_command
*cmd
)
975 struct lttcomm_relayd_generic_reply reply
;
978 reply
.ret_code
= htobe32(LTTCOMM_ERR
);
979 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
980 sizeof(struct lttcomm_relayd_generic_reply
), 0);
982 ERR("Relay sending unknown command");
987 * relay_start: send an acknowledgment to the client to tell if we are
988 * ready to receive data. We are ready if a session is established.
991 int relay_start(struct lttcomm_relayd_hdr
*recv_hdr
,
992 struct relay_command
*cmd
)
994 int ret
= htobe32(LTTCOMM_OK
);
995 struct lttcomm_relayd_generic_reply reply
;
996 struct relay_session
*session
= cmd
->session
;
999 DBG("Trying to start the streaming without a session established");
1000 ret
= htobe32(LTTCOMM_ERR
);
1003 reply
.ret_code
= ret
;
1004 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1005 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1007 ERR("Relay sending start ack");
1014 * Get stream from stream id.
1017 struct relay_stream
*relay_stream_from_stream_id(uint64_t stream_id
,
1018 struct lttng_ht
*streams_ht
)
1020 struct lttng_ht_node_ulong
*node
;
1021 struct lttng_ht_iter iter
;
1022 struct relay_stream
*ret
;
1024 lttng_ht_lookup(streams_ht
,
1025 (void *)((unsigned long) stream_id
),
1027 node
= lttng_ht_iter_get_node_ulong(&iter
);
1029 DBG("Relay stream %lu not found", stream_id
);
1034 ret
= caa_container_of(node
, struct relay_stream
, stream_n
);
1041 * relay_recv_metadata: receive the metada for the session.
1044 int relay_recv_metadata(struct lttcomm_relayd_hdr
*recv_hdr
,
1045 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1047 int ret
= htobe32(LTTCOMM_OK
);
1048 struct relay_session
*session
= cmd
->session
;
1049 struct lttcomm_relayd_metadata_payload
*metadata_struct
;
1050 struct relay_stream
*metadata_stream
;
1051 uint64_t data_size
, payload_size
;
1054 ERR("Metadata sent before version check");
1059 data_size
= be64toh(recv_hdr
->data_size
);
1060 payload_size
= data_size
- sizeof(uint64_t);
1061 if (data_buffer_size
< data_size
) {
1062 data_buffer
= realloc(data_buffer
, data_size
);
1064 ERR("Allocating data buffer");
1068 data_buffer_size
= data_size
;
1070 memset(data_buffer
, 0, data_size
);
1071 DBG2("Relay receiving metadata, waiting for %lu bytes", data_size
);
1072 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1073 if (ret
< 0 || ret
!= data_size
) {
1075 ERR("Relay didn't receive the whole metadata");
1078 metadata_struct
= (struct lttcomm_relayd_metadata_payload
*) data_buffer
;
1079 metadata_stream
= relay_stream_from_stream_id(
1080 be64toh(metadata_struct
->stream_id
), streams_ht
);
1081 if (!metadata_stream
) {
1087 ret
= write(metadata_stream
->fd
, metadata_struct
->payload
,
1089 } while (ret
< 0 && errno
== EINTR
);
1090 if (ret
< (payload_size
)) {
1091 ERR("Relay error writing metadata on file");
1095 DBG2("Relay metadata written");
1102 * relay_send_version: send relayd version number
1105 int relay_send_version(struct lttcomm_relayd_hdr
*recv_hdr
,
1106 struct relay_command
*cmd
)
1108 int ret
= htobe32(LTTCOMM_OK
);
1109 struct lttcomm_relayd_version reply
;
1110 struct relay_session
*session
= NULL
;
1112 if (cmd
->session
== NULL
) {
1113 session
= zmalloc(sizeof(struct relay_session
));
1114 if (session
== NULL
) {
1115 PERROR("relay session zmalloc");
1119 session
->id
= ++last_relay_session_id
;
1120 DBG("Created session %lu", session
->id
);
1121 cmd
->session
= session
;
1123 session
->version_check_done
= 1;
1125 sscanf(VERSION
, "%u.%u", &reply
.major
, &reply
.minor
);
1126 reply
.major
= htobe32(reply
.major
);
1127 reply
.minor
= htobe32(reply
.minor
);
1128 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1129 sizeof(struct lttcomm_relayd_version
), 0);
1131 ERR("Relay sending version");
1133 DBG("Version check done");
1140 * relay_process_control: Process the commands received on the control socket
1143 int relay_process_control(struct lttcomm_relayd_hdr
*recv_hdr
,
1144 struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1148 switch (be32toh(recv_hdr
->cmd
)) {
1150 case RELAYD_CREATE_SESSION:
1151 ret = relay_create_session(recv_hdr, cmd);
1154 case RELAYD_ADD_STREAM
:
1155 ret
= relay_add_stream(recv_hdr
, cmd
, streams_ht
);
1157 case RELAYD_START_DATA
:
1158 ret
= relay_start(recv_hdr
, cmd
);
1160 case RELAYD_SEND_METADATA
:
1161 ret
= relay_recv_metadata(recv_hdr
, cmd
, streams_ht
);
1163 case RELAYD_VERSION
:
1164 ret
= relay_send_version(recv_hdr
, cmd
);
1166 case RELAYD_UPDATE_SYNC_INFO
:
1168 ERR("Received unknown command (%u)", be32toh(recv_hdr
->cmd
));
1169 relay_unknown_command(cmd
);
1179 * relay_process_data: Process the data received on the data socket
1182 int relay_process_data(struct relay_command
*cmd
, struct lttng_ht
*streams_ht
)
1185 struct relay_stream
*stream
;
1186 struct lttcomm_relayd_data_hdr data_hdr
;
1190 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &data_hdr
,
1191 sizeof(struct lttcomm_relayd_data_hdr
), MSG_WAITALL
);
1193 ERR("Connections seems to be closed");
1198 stream_id
= be64toh(data_hdr
.stream_id
);
1199 stream
= relay_stream_from_stream_id(stream_id
, streams_ht
);
1205 data_size
= be32toh(data_hdr
.data_size
);
1206 if (data_buffer_size
< data_size
) {
1207 data_buffer
= realloc(data_buffer
, data_size
);
1209 ERR("Allocating data buffer");
1213 data_buffer_size
= data_size
;
1215 memset(data_buffer
, 0, data_size
);
1217 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, data_buffer
, data_size
, MSG_WAITALL
);
1224 ret
= write(stream
->fd
, data_buffer
, data_size
);
1225 } while (ret
< 0 && errno
== EINTR
);
1226 if (ret
< data_size
) {
1227 ERR("Relay error writing data to file");
1231 DBG2("Relay wrote %d bytes to tracefile for stream id %lu", ret
, stream
->stream_handle
);
1238 void relay_cleanup_connection(struct lttng_ht
*relay_connections_ht
, struct lttng_poll_event
*events
,
1239 struct lttng_ht
*streams_ht
, int pollfd
, struct lttng_ht_iter
*iter
)
1243 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1245 lttng_poll_del(events
, pollfd
);
1247 ret
= close(pollfd
);
1249 ERR("Closing pollfd %d", pollfd
);
1254 int relay_add_connection(int fd
, struct lttng_poll_event
*events
,
1255 struct lttng_ht
*relay_connections_ht
)
1258 struct relay_command
*relay_connection
;
1260 relay_connection
= zmalloc(sizeof(struct relay_command
));
1261 if (relay_connection
== NULL
) {
1262 PERROR("Relay command zmalloc");
1266 ret
= read(fd
, relay_connection
, sizeof(struct relay_command
));
1267 if (ret
< 0 || ret
< sizeof(relay_connection
)) {
1268 PERROR("read relay cmd pipe");
1273 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1274 (unsigned long) relay_connection
->sock
->fd
);
1275 lttng_ht_add_unique_ulong(relay_connections_ht
,
1276 &relay_connection
->sock_n
);
1277 ret
= lttng_poll_add(events
,
1278 relay_connection
->sock
->fd
,
1279 LPOLLIN
| LPOLLRDHUP
);
1286 * This thread does the actual work
1289 void *relay_thread_worker(void *data
)
1291 int i
, ret
, pollfd
, err
= -1;
1292 uint32_t revents
, nb_fd
;
1293 struct relay_command
*relay_connection
;
1294 struct lttng_poll_event events
;
1295 struct lttng_ht
*relay_connections_ht
;
1296 struct lttng_ht_node_ulong
*node
;
1297 struct lttng_ht_iter iter
;
1298 struct lttng_ht
*streams_ht
;
1299 struct lttcomm_relayd_hdr recv_hdr
;
1301 DBG("[thread] Relay worker started");
1303 /* table of connections indexed on socket */
1304 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1305 if (!relay_connections_ht
) {
1306 goto relay_connections_ht_error
;
1309 /* tables of streams indexed by stream ID */
1310 streams_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1312 goto streams_ht_error
;
1315 ret
= create_thread_poll_set(&events
, 2);
1317 goto error_poll_create
;
1320 ret
= lttng_poll_add(&events
, relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1326 /* Zeroed the events structure */
1327 lttng_poll_reset(&events
);
1329 nb_fd
= LTTNG_POLL_GETNB(&events
);
1331 /* Infinite blocking call, waiting for transmission */
1333 ret
= lttng_poll_wait(&events
, -1);
1336 * Restart interrupted system call.
1338 if (errno
== EINTR
) {
1344 for (i
= 0; i
< nb_fd
; i
++) {
1345 /* Fetch once the poll data */
1346 revents
= LTTNG_POLL_GETEV(&events
, i
);
1347 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1349 /* Thread quit pipe has been closed. Killing thread. */
1350 ret
= check_thread_quit_pipe(pollfd
, revents
);
1356 /* Inspect the relay cmd pipe for new connection */
1357 if (pollfd
== relay_cmd_pipe
[0]) {
1358 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1359 ERR("Relay pipe error");
1361 } else if (revents
& LPOLLIN
) {
1362 DBG("Relay command received");
1363 ret
= relay_add_connection(relay_cmd_pipe
[0],
1364 &events
, relay_connections_ht
);
1369 } else if (revents
> 0) {
1370 lttng_ht_lookup(relay_connections_ht
,
1371 (void *)((unsigned long) pollfd
),
1373 node
= lttng_ht_iter_get_node_ulong(&iter
);
1375 DBG2("Relay sock %d not found", pollfd
);
1378 relay_connection
= caa_container_of(node
,
1379 struct relay_command
, sock_n
);
1381 if (revents
& (LPOLLERR
)) {
1383 relay_cleanup_connection(relay_connections_ht
,
1384 &events
, streams_ht
, pollfd
, &iter
);
1385 if (relay_connection
->type
== RELAY_CONTROL
) {
1386 relay_delete_session(relay_connection
, streams_ht
);
1388 free(relay_connection
);
1389 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1390 DBG("Socket %d hung up", pollfd
);
1391 relay_cleanup_connection(relay_connections_ht
,
1392 &events
, streams_ht
, pollfd
, &iter
);
1393 if (relay_connection
->type
== RELAY_CONTROL
) {
1394 relay_delete_session(relay_connection
, streams_ht
);
1396 free(relay_connection
);
1397 } else if (revents
& LPOLLIN
) {
1398 /* control socket */
1399 if (relay_connection
->type
== RELAY_CONTROL
) {
1400 ret
= relay_connection
->sock
->ops
->recvmsg(
1401 relay_connection
->sock
, &recv_hdr
,
1402 sizeof(struct lttcomm_relayd_hdr
), MSG_WAITALL
);
1403 /* connection closed */
1405 relay_cleanup_connection(relay_connections_ht
,
1406 &events
, streams_ht
, pollfd
, &iter
);
1407 relay_delete_session(relay_connection
, streams_ht
);
1408 free(relay_connection
);
1409 DBG("Control connection closed with %d", pollfd
);
1411 if (relay_connection
->session
) {
1412 DBG2("Relay worker receiving data for session : %lu",
1413 relay_connection
->session
->id
);
1415 ret
= relay_process_control(&recv_hdr
,
1419 * there was an error in processing a control
1420 * command: clear the session
1423 relay_cleanup_connection(relay_connections_ht
,
1424 &events
, streams_ht
, pollfd
, &iter
);
1425 relay_delete_session(relay_connection
, streams_ht
);
1426 free(relay_connection
);
1427 DBG("Connection closed with %d", pollfd
);
1431 } else if (relay_connection
->type
== RELAY_DATA
) {
1432 ret
= relay_process_data(relay_connection
, streams_ht
);
1433 /* connection closed */
1435 relay_cleanup_connection(relay_connections_ht
,
1436 &events
, streams_ht
, pollfd
, &iter
);
1437 relay_delete_session(relay_connection
, streams_ht
);
1438 free(relay_connection
);
1439 DBG("Data connection closed with %d", pollfd
);
1449 lttng_poll_clean(&events
);
1451 /* empty the hash table and free the memory */
1452 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1453 node
= lttng_ht_iter_get_node_ulong(&iter
);
1455 relay_connection
= caa_container_of(node
,
1456 struct relay_command
, sock_n
);
1457 if (relay_connection
->type
== RELAY_CONTROL
) {
1458 relay_delete_session(relay_connection
, streams_ht
);
1460 free(relay_connection
);
1462 ret
= lttng_ht_del(relay_connections_ht
, &iter
);
1466 lttng_ht_destroy(streams_ht
);
1468 lttng_ht_destroy(relay_connections_ht
);
1469 relay_connections_ht_error
:
1471 DBG("Thread exited with error");
1473 DBG("Worker thread cleanup complete");
1480 * Create the relay command pipe to wake thread_manage_apps.
1481 * Closed in cleanup().
1483 static int create_relay_cmd_pipe(void)
1487 ret
= utils_create_pipe_cloexec(relay_cmd_pipe
);
1495 int main(int argc
, char **argv
)
1500 /* Create thread quit pipe */
1501 if ((ret
= init_thread_quit_pipe()) < 0) {
1505 /* Parse arguments */
1507 if ((ret
= parse_args(argc
, argv
) < 0)) {
1511 if ((ret
= set_signal_handler()) < 0) {
1524 /* Check if daemon is UID = 0 */
1525 is_root
= !getuid();
1528 if (control_uri
->port
< 1024 || data_uri
->port
< 1024) {
1529 ERR("Need to be root to use ports < 1024");
1535 /* Setup the thread apps communication pipe. */
1536 if ((ret
= create_relay_cmd_pipe()) < 0) {
1540 /* Init relay command queue. */
1541 cds_wfq_init(&relay_cmd_queue
.queue
);
1543 /* Set up max poll set size */
1544 lttng_poll_set_max_size();
1546 /* Setup the dispatcher thread */
1547 ret
= pthread_create(&dispatcher_thread
, NULL
,
1548 relay_thread_dispatcher
, (void *) NULL
);
1550 PERROR("pthread_create dispatcher");
1551 goto exit_dispatcher
;
1554 /* Setup the worker thread */
1555 ret
= pthread_create(&worker_thread
, NULL
,
1556 relay_thread_worker
, (void *) NULL
);
1558 PERROR("pthread_create worker");
1562 /* Setup the listener thread */
1563 ret
= pthread_create(&listener_thread
, NULL
,
1564 relay_thread_listener
, (void *) NULL
);
1566 PERROR("pthread_create listener");
1571 ret
= pthread_join(listener_thread
, &status
);
1573 PERROR("pthread_join");
1574 goto error
; /* join error, exit without cleanup */
1578 ret
= pthread_join(worker_thread
, &status
);
1580 PERROR("pthread_join");
1581 goto error
; /* join error, exit without cleanup */
1585 ret
= pthread_join(dispatcher_thread
, &status
);
1587 PERROR("pthread_join");
1588 goto error
; /* join error, exit without cleanup */