Daemonize sessiond on `lttng create`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 27 Aug 2015 15:52:33 +0000 (11:52 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 6 Sep 2015 03:16:38 +0000 (23:16 -0400)
Since the session daemon forked by `lttng create` shares its
standard output/error FDs when not using `--daemonize`, redirecting
the standard output/error of this command to another program "hangs"
because the session daemon never terminates.

Example that's not working (when sessiond is not running):

    lttng create | wc

or:

    lttng 2>&1 | wc

Using sessiond's `--daemonize` option makes it close its FDs. This
option also ensures that when the sessiond process exits, it has forked
itself as a daemon and is ready to accept commands. Therefore we don't
need to catch SIGCHLD and SIGUSR1; just waitpid() on sessiond's PID and
make sure it exited normally and with an exit status of 0 to continue.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng/commands/create.c
src/bin/lttng/lttng.c
src/bin/lttng/utils.h

index 94c1c61836f5edd61dc5c4b8784d346875d0284b..3b679ca71d75e77c3a80bad4b9237fab7f068d61 100644 (file)
@@ -28,6 +28,7 @@
 #include <time.h>
 #include <unistd.h>
 #include <signal.h>
+#include <sys/wait.h>
 
 #include <common/mi-lttng.h>
 
@@ -562,14 +563,13 @@ static int spawn_sessiond(char *pathname)
        pid_t pid;
 
        MSG("Spawning a session daemon");
-       recv_child_signal = 0;
        pid = fork();
        if (pid == 0) {
                /*
-                * Spawn session daemon and tell
-                * it to signal us when ready.
+                * Spawn session daemon in daemon mode.
                 */
-               execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
+               execlp(pathname, "lttng-sessiond",
+                               "--daemonize", NULL);
                /* execlp only returns if error happened */
                if (errno == ENOENT) {
                        ERR("No session daemon found. Use --sessiond-path.");
@@ -579,25 +579,34 @@ static int spawn_sessiond(char *pathname)
                kill(getppid(), SIGTERM);       /* wake parent */
                exit(EXIT_FAILURE);
        } else if (pid > 0) {
-               sessiond_pid = pid;
-               /*
-                * Wait for lttng-sessiond to start. We need to use a flag to check if
-                * the signal has been sent to us, because the child can be scheduled
-                * before the parent, and thus send the signal before this check. In
-                * the signal handler, we set the recv_child_signal flag, so anytime we
-                * check it after the fork is fine. Note that sleep() is interrupted
-                * before the 1 second delay as soon as the signal is received, so it
-                * will not cause visible delay for the user.
-                */
-               while (!recv_child_signal) {
-                       sleep(1);
-               }
+               int status;
+
                /*
-                * The signal handler will nullify sessiond_pid on SIGCHLD
+                * In daemon mode (--daemonize), sessiond only exits when
+                * it's ready to accept commands.
                 */
-               if (!sessiond_pid) {
-                       exit(EXIT_FAILURE);
+               for (;;) {
+                       waitpid(pid, &status, 0);
+
+                       if (WIFSIGNALED(status)) {
+                               ERR("Session daemon was killed by signal %d",
+                                               WTERMSIG(status));
+                               ret = -1;
+                               goto end;
+                       } else if (WIFEXITED(status)) {
+                               DBG("Session daemon terminated normally (exit status: %d)",
+                                               WEXITSTATUS(status));
+
+                               if (WEXITSTATUS(status) != 0) {
+                                       ERR("Session daemon terminated with an error (exit status: %d)",
+                                                       WEXITSTATUS(status));
+                                       ret = -1;
+                                       goto end;
+                               }
+                               break;
+                       }
                }
+
                goto end;
        } else {
                PERROR("fork");
index 4d2df2deb999832f1d816eefc44ebb441e7ce614..bf07c83b76058219e88ceb1c41b41122242252d2 100644 (file)
@@ -38,8 +38,6 @@
 static char *progname;
 int opt_no_sessiond;
 char *opt_sessiond_path;
-pid_t sessiond_pid;
-volatile int recv_child_signal;
 
 char *opt_relayd_path;
 
@@ -203,26 +201,11 @@ static void clean_exit(int code)
  */
 static void sighandler(int sig)
 {
-       int status;
-
        switch (sig) {
                case SIGTERM:
                        DBG("SIGTERM caught");
                        clean_exit(EXIT_FAILURE);
                        break;
-               case SIGCHLD:
-                       DBG("SIGCHLD caught");
-                       waitpid(sessiond_pid, &status, 0);
-                       recv_child_signal = 1;
-                       /* Indicate that the session daemon died */
-                       sessiond_pid = 0;
-                       ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
-                       break;
-               case SIGUSR1:
-                       /* Notify is done */
-                       recv_child_signal = 1;
-                       DBG("SIGUSR1 caught");
-                       break;
                default:
                        DBG("Unknown signal %d caught", sig);
                        break;
@@ -250,21 +233,12 @@ static int set_signal_handler(void)
        sa.sa_handler = sighandler;
        sa.sa_mask = sigset;
        sa.sa_flags = 0;
-       if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
-               PERROR("sigaction");
-               goto end;
-       }
 
        if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
                PERROR("sigaction");
                goto end;
        }
 
-       if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
-               PERROR("sigaction");
-               goto end;
-       }
-
 end:
        return ret;
 }
index 0755731936fda70e52b4e59fc8e07ca6c9391b6f..b0e44f66e01c4fb47f64820a1e61a162c33ba95e 100644 (file)
@@ -26,7 +26,6 @@
 extern char *opt_relayd_path;
 extern int opt_no_sessiond;
 extern char * opt_sessiond_path;
-extern volatile int recv_child_signal;
 extern pid_t sessiond_pid;
 
 struct cmd_struct;
This page took 0.0291 seconds and 4 git commands to generate.