X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Fmain.c;h=fe4a898bc6d349dfd866218e597d77ef12a746aa;hb=719af1bb6e6719c6170e059a7e473aa7923ddae9;hp=7ac5630fb9790262b7355df43f7066deadfb671b;hpb=b5218ffbf53aa46007927f1e2cda0ad4549abcf9;p=lttng-tools.git diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index 7ac5630fb..fe4a898bc 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, 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 @@ -509,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; } @@ -548,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. * @@ -587,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; } @@ -722,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(); @@ -822,6 +858,7 @@ restart: exit: error: error_poll_add: +error_testpoint: lttng_poll_clean(&events); error_create_poll: if (data_sock->fd >= 0) { @@ -865,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)) { @@ -911,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__); @@ -1123,6 +1165,10 @@ int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr, 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); @@ -2529,6 +2575,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 */ @@ -2790,6 +2840,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__); @@ -2822,11 +2873,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) { @@ -2853,11 +2899,27 @@ int main(int argc, char **argv) /* Daemonize */ if (opt_daemon || opt_background) { - ret = daemon(0, 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. */ @@ -2956,7 +3018,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;