Fix: add -b, --background option
[lttng-tools.git] / src / bin / lttng-relayd / main.c
index 10a4bb41c6e7485bd173bd53e3daa736682dab98..b1b48185bdb5f3ae6132fd8027e2aea15ca54e78 100644 (file)
@@ -44,6 +44,7 @@
 #include <common/compat/poll.h>
 #include <common/compat/socket.h>
 #include <common/defaults.h>
+#include <common/daemonize.h>
 #include <common/futex.h>
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/sessiond-comm/inet.h>
 #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;
@@ -74,7 +86,7 @@ const char *tracing_group_name = DEFAULT_TRACING_GROUP;
  * 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
@@ -130,6 +142,7 @@ 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");
@@ -153,12 +166,13 @@ int parse_args(int argc, char **argv)
                { "help", 0, 0, 'h', },
                { "output", 1, 0, 'o', },
                { "verbose", 0, 0, 'v', },
+               { "background", 0, 0, 'b' },
                { NULL, 0, 0, 0, },
        };
 
        while (1) {
                int option_index = 0;
-               c = getopt_long(argc, argv, "dhv" "C:D:L:o:g:",
+               c = getopt_long(argc, argv, "dhv" "C:D:L:o:g:b",
                                long_options, &option_index);
                if (c == -1) {
                        break;
@@ -204,6 +218,9 @@ int parse_args(int argc, char **argv)
                case 'd':
                        opt_daemon = 1;
                        break;
+               case 'b':
+                       opt_background = 1;
+                       break;
                case 'g':
                        tracing_group_name = optarg;
                        break;
@@ -368,6 +385,9 @@ void sighandler(int sig)
                DBG("SIGTERM caught");
                stop_threads();
                break;
+       case SIGUSR1:
+               CMM_STORE_SHARED(recv_child_signal, 1);
+               break;
        default:
                break;
        }
@@ -407,11 +427,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(&lttng_relay_ready, 1) == 0) {
+                       kill(child_ppid, SIGUSR1);
+               }
+       }
+}
+
 /*
  * Init thread quit pipe.
  *
@@ -446,7 +481,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;
        }
@@ -581,6 +616,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();
 
@@ -681,6 +722,7 @@ restart:
 exit:
 error:
 error_poll_add:
+error_testpoint:
        lttng_poll_clean(&events);
 error_create_poll:
        if (data_sock->fd >= 0) {
@@ -724,6 +766,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)) {
@@ -770,6 +816,7 @@ void *relay_thread_dispatcher(void *data)
        err = 0;
 
 error:
+error_testpoint:
        if (err) {
                health_error();
                ERR("Health error occurred in %s", __func__);
@@ -1030,18 +1077,10 @@ void set_viewer_ready_flag(struct relay_command *cmd)
                         * Stream is most probably being cleaned up by the data thread thus
                         * simply continue to the next one.
                         */
+                       rcu_read_unlock();
                        continue;
                }
 
-               /*
-                * If any of the streams in the list doesn't have a ctf_trace assigned,
-                * it means that we never received the metadata stream, so we have to
-                * wait until it arrives to make the streams available to the viewer.
-                */
-               if (!stream->ctf_trace) {
-                       goto end;
-               }
-
                stream->viewer_ready = 1;
                rcu_read_unlock();
 
@@ -1050,7 +1089,6 @@ void set_viewer_ready_flag(struct relay_command *cmd)
                free(node);
        }
 
-end:
        return;
 }
 
@@ -1172,7 +1210,11 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
         * stream message is received, this list is emptied and streams are set
         * with the viewer ready flag.
         */
-       queue_stream_handle(stream->stream_handle, cmd);
+       if (stream->metadata_flag) {
+               stream->viewer_ready = 1;
+       } else {
+               queue_stream_handle(stream->stream_handle, cmd);
+       }
 
        lttng_ht_node_init_ulong(&stream->stream_n,
                        (unsigned long) stream->stream_handle);
@@ -2393,6 +2435,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 */
@@ -2654,6 +2700,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__);
@@ -2686,11 +2733,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 = parse_args(argc, argv)) < 0) {
@@ -2716,12 +2758,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. */
@@ -2751,6 +2809,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) {
@@ -2820,7 +2879,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;
This page took 0.027786 seconds and 4 git commands to generate.