X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Fmain.c;h=730eaf203102886b206aa5d267afc2f8bc9e98b6;hp=71d11ebdfa2be05c640c869799f553b35b1ea97f;hb=80e8027abb847655ebe43b2b5aec1a5141bb9668;hpb=cd60b05aa790a1355e50fd0248f5dcf7a58b88c3 diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index 71d11ebdf..730eaf203 100644 --- a/src/bin/lttng-relayd/main.c +++ b/src/bin/lttng-relayd/main.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -60,10 +61,21 @@ #include "lttng-relayd.h" #include "live.h" #include "health-relayd.h" +#include "testpoint.h" /* command line options */ char *opt_output_path; -static int opt_daemon; +static int opt_daemon, opt_background; + +/* + * We need to wait for listener and live listener threads, as well as + * health check thread, before being ready to signal readiness. + */ +#define NR_LTTNG_RELAY_READY 3 +static int lttng_relay_ready = NR_LTTNG_RELAY_READY; +static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */ +static pid_t child_ppid; /* Internal parent PID use with daemonize. */ + static struct lttng_uri *control_uri; static struct lttng_uri *data_uri; static struct lttng_uri *live_uri; @@ -79,7 +91,7 @@ const char * const config_section_name = "relayd"; * Quit pipe for all threads. This permits a single cancellation point * for all threads when receiving an event on the pipe. */ -static int thread_quit_pipe[2] = { -1, -1 }; +int thread_quit_pipe[2] = { -1, -1 }; /* * This pipe is used to inform the worker thread that a command is queued and @@ -129,7 +141,9 @@ struct health_app *health_relayd; static struct option long_options[] = { { "control-port", 1, 0, 'C', }, { "data-port", 1, 0, 'D', }, + { "live-port", 1, 0, 'L', }, { "daemonize", 0, 0, 'd', }, + { "background", 0, 0, 'b', }, { "group", 1, 0, 'g', }, { "help", 0, 0, 'h', }, { "output", 1, 0, 'o', }, @@ -149,8 +163,10 @@ void usage(void) fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); fprintf(stderr, " -h, --help Display this usage.\n"); fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); + fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n"); fprintf(stderr, " -C, --control-port URL Control port listening.\n"); fprintf(stderr, " -D, --data-port URL Data port listening.\n"); + fprintf(stderr, " -L, --live-port URL Live view port listening.\n"); fprintf(stderr, " -o, --output PATH Output path for traces. Must use an absolute path.\n"); fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); @@ -195,9 +211,22 @@ int set_option(int opt, const char *arg, const char *optname) data_uri->port = DEFAULT_NETWORK_DATA_PORT; } break; + case 'L': + ret = uri_parse(arg, &live_uri); + if (ret < 0) { + ERR("Invalid live URI specified"); + goto end; + } + if (live_uri->port == 0) { + live_uri->port = DEFAULT_NETWORK_VIEWER_PORT; + } + break; case 'd': opt_daemon = 1; break; + case 'b': + opt_background = 1; + break; case 'g': tracing_group_name = strdup(arg); tracing_group_name_override = 1; @@ -416,6 +445,7 @@ void cleanup(void) uri_free(control_uri); uri_free(data_uri); + /* Live URI is freed in the live thread. */ if (tracing_group_name_override) { free((void *) tracing_group_name); @@ -491,6 +521,9 @@ void sighandler(int sig) DBG("SIGTERM caught"); stop_threads(); break; + case SIGUSR1: + CMM_STORE_SHARED(recv_child_signal, 1); + break; default: break; } @@ -530,11 +563,26 @@ int set_signal_handler(void) return ret; } - DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); + if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { + PERROR("sigaction"); + return ret; + } + + DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); return ret; } +void lttng_relay_notify_ready(void) +{ + /* Notify the parent of the fork() process that we are ready. */ + if (opt_daemon || opt_background) { + if (uatomic_sub_return(<tng_relay_ready, 1) == 0) { + kill(child_ppid, SIGUSR1); + } + } +} + /* * Init thread quit pipe. * @@ -569,7 +617,7 @@ int create_thread_poll_set(struct lttng_poll_event *events, int size) } /* Add quit pipe */ - ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); + ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); if (ret < 0) { goto error; } @@ -704,6 +752,12 @@ void *relay_thread_listener(void *data) goto error_poll_add; } + lttng_relay_notify_ready(); + + if (testpoint(relayd_thread_listener)) { + goto error_testpoint; + } + while (1) { health_code_update(); @@ -804,6 +858,7 @@ restart: exit: error: error_poll_add: +error_testpoint: lttng_poll_clean(&events); error_create_poll: if (data_sock->fd >= 0) { @@ -847,6 +902,10 @@ void *relay_thread_dispatcher(void *data) health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER); + if (testpoint(relayd_thread_dispatcher)) { + goto error_testpoint; + } + health_code_update(); while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { @@ -893,6 +952,7 @@ void *relay_thread_dispatcher(void *data) err = 0; error: +error_testpoint: if (err) { health_error(); ERR("Health error occurred in %s", __func__); @@ -1100,11 +1160,16 @@ int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr, session->sock = cmd->sock; session->minor = cmd->minor; session->major = cmd->major; + pthread_mutex_init(&session->viewer_ready_lock, NULL); cmd->session = session; reply.session_id = htobe64(session->id); switch (cmd->minor) { + case 1: + case 2: + case 3: + break; case 4: /* LTTng sessiond 2.4 */ default: ret = cmd_create_session_2_4(cmd, session); @@ -1134,6 +1199,64 @@ error: return ret; } +/* + * When we have received all the streams and the metadata for a channel, + * we make them visible to the viewer threads. + */ +static +void set_viewer_ready_flag(struct relay_command *cmd) +{ + struct relay_stream_recv_handle *node, *tmp_node; + + pthread_mutex_lock(&cmd->session->viewer_ready_lock); + + cds_list_for_each_entry_safe(node, tmp_node, &cmd->recv_head, node) { + struct relay_stream *stream; + + rcu_read_lock(); + stream = relay_stream_find_by_id(node->id); + if (!stream) { + /* + * Stream is most probably being cleaned up by the data thread thus + * simply continue to the next one. + */ + rcu_read_unlock(); + continue; + } + + stream->viewer_ready = 1; + rcu_read_unlock(); + + /* Clean stream handle node. */ + cds_list_del(&node->node); + free(node); + } + + pthread_mutex_unlock(&cmd->session->viewer_ready_lock); + return; +} + +/* + * Add a recv handle node to the connection recv list with the given stream + * handle. A new node is allocated thus must be freed when the node is deleted + * from the list. + */ +static void queue_stream_handle(uint64_t handle, struct relay_command *cmd) +{ + struct relay_stream_recv_handle *node; + + assert(cmd); + + node = zmalloc(sizeof(*node)); + if (!node) { + PERROR("zmalloc queue stream handle"); + return; + } + + node->id = handle; + cds_list_add(&node->node, &cmd->recv_head); +} + /* * relay_add_stream: allocate a new stream for a session */ @@ -1226,6 +1349,13 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, ctf_trace_assign(cmd->ctf_traces_ht, stream); stream->ctf_traces_ht = cmd->ctf_traces_ht; + /* + * Add the stream handle in the recv list of the connection. Once the end + * stream message is received, this list is emptied and streams are set + * with the viewer ready flag. + */ + queue_stream_handle(stream->stream_handle, cmd); + lttng_ht_node_init_ulong(&stream->stream_n, (unsigned long) stream->stream_handle); lttng_ht_add_unique_ulong(relay_streams_ht, @@ -1974,6 +2104,53 @@ end_no_session: return ret; } +/* + * Receive the streams_sent message. + * + * Return 0 on success else a negative value. + */ +static +int relay_streams_sent(struct lttcomm_relayd_hdr *recv_hdr, + struct relay_command *cmd) +{ + int ret, send_ret; + struct lttcomm_relayd_generic_reply reply; + + assert(cmd); + + DBG("Relay receiving streams_sent"); + + if (!cmd->session || cmd->version_check_done == 0) { + ERR("Trying to close a stream before version check"); + ret = -1; + goto end_no_session; + } + + /* + * Flag every pending stream in the connection recv list that they are + * ready to be used by the viewer. + */ + set_viewer_ready_flag(cmd); + + /* + * Inform the viewer that there are new streams in the session. + */ + uatomic_set(&cmd->session->new_streams, 1); + + reply.ret_code = htobe32(LTTNG_OK); + send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0); + if (send_ret < 0) { + ERR("Relay sending sent_stream reply"); + ret = send_ret; + } else { + /* Success. */ + ret = 0; + } + +end_no_session: + return ret; +} + /* * Process the commands received on the control socket */ @@ -2017,6 +2194,9 @@ int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, case RELAYD_SEND_INDEX: ret = relay_recv_index(recv_hdr, cmd); break; + case RELAYD_STREAMS_SENT: + ret = relay_streams_sent(recv_hdr, cmd); + break; case RELAYD_UPDATE_SYNC_INFO: default: ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd)); @@ -2309,6 +2489,7 @@ int relay_add_connection(int fd, struct lttng_poll_event *events, PERROR("read relay cmd pipe"); goto error_read; } + CDS_INIT_LIST_HEAD(&relay_connection->recv_head); /* * Only used by the control side and the reference is copied inside each @@ -2360,8 +2541,17 @@ void relay_del_connection(struct lttng_ht *relay_connections_ht, assert(!ret); if (relay_connection->type == RELAY_CONTROL) { + struct relay_stream_recv_handle *node, *tmp_node; + relay_delete_session(relay_connection, sessions_ht); lttng_ht_destroy(relay_connection->ctf_traces_ht); + + /* Clean up recv list. */ + cds_list_for_each_entry_safe(node, tmp_node, + &relay_connection->recv_head, node) { + cds_list_del(&node->node); + free(node); + } } call_rcu(&relay_connection->rcu_node, deferred_free_connection); @@ -2390,6 +2580,10 @@ void *relay_thread_worker(void *data) health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER); + if (testpoint(relayd_thread_worker)) { + goto error_testpoint; + } + health_code_update(); /* table of connections indexed on socket */ @@ -2651,6 +2845,7 @@ relay_connections_ht_error: } DBG("Worker thread cleanup complete"); free(data_buffer); +error_testpoint: if (err) { health_error(); ERR("Health error occurred in %s", __func__); @@ -2683,11 +2878,6 @@ int main(int argc, char **argv) void *status; struct relay_local_data *relay_ctx; - /* Create thread quit pipe */ - if ((ret = init_thread_quit_pipe()) < 0) { - goto error; - } - /* Parse arguments */ progname = argv[0]; if ((ret = set_options(argc, argv)) < 0) { @@ -2713,12 +2903,28 @@ int main(int argc, char **argv) } /* Daemonize */ - if (opt_daemon) { - ret = daemon(0, 0); + if (opt_daemon || opt_background) { + int i; + + ret = lttng_daemonize(&child_ppid, &recv_child_signal, + !opt_background); if (ret < 0) { - PERROR("daemon"); goto exit; } + + /* + * We are in the child. Make sure all other file + * descriptors are closed, in case we are called with + * more opened file descriptors than the standard ones. + */ + for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { + (void) close(i); + } + } + + /* Create thread quit pipe */ + if ((ret = init_thread_quit_pipe()) < 0) { + goto error; } /* We need those values for the file/dir creation. */ @@ -2727,7 +2933,7 @@ int main(int argc, char **argv) /* Check if daemon is UID = 0 */ if (relayd_uid == 0) { - if (control_uri->port < 1024 || data_uri->port < 1024) { + if (control_uri->port < 1024 || data_uri->port < 1024 || live_uri->port < 1024) { ERR("Need to be root to use ports < 1024"); ret = -1; goto exit; @@ -2747,6 +2953,7 @@ int main(int argc, char **argv) /* Initialize communication library */ lttcomm_init(); + lttcomm_inet_init(); relay_ctx = zmalloc(sizeof(struct relay_local_data)); if (!relay_ctx) { @@ -2816,7 +3023,7 @@ int main(int argc, char **argv) goto exit_listener; } - ret = live_start_threads(live_uri, relay_ctx, thread_quit_pipe); + ret = live_start_threads(live_uri, relay_ctx); if (ret != 0) { ERR("Starting live viewer threads"); goto exit_live;