Save/load: support session trace format
[lttng-tools.git] / src / bin / lttng / utils.cpp
index 9a9f39bf2529b1cd6328f5052b73672715d12663..fd839ecbfed9bf3756fbd74df34c25409bdb0e36 100644 (file)
@@ -6,15 +6,16 @@
  */
 
 #define _LGPL_SOURCE
-#include <stdlib.h>
+#include <arpa/inet.h>
 #include <ctype.h>
+#include <inttypes.h>
 #include <limits.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <signal.h>
 #include <netinet/in.h>
-#include <arpa/inet.h>
-#include <inttypes.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #include <common/error.hpp>
@@ -348,16 +349,51 @@ int spawn_relayd(const char *pathname, int port)
                 * Spawn session daemon and tell
                 * it to signal us when ready.
                 */
-               execlp(pathname, "lttng-relayd", "-L", url, NULL);
+               execlp(pathname, "lttng-relayd", "-L", url, "--daemonize", NULL);
                /* execlp only returns if error happened */
                if (errno == ENOENT) {
                        ERR("No relayd found. Use --relayd-path.");
                } else {
                        PERROR("execlp");
                }
-               kill(getppid(), SIGTERM);       /* wake parent */
+               kill(getppid(), SIGTERM); /* wake parent */
                exit(EXIT_FAILURE);
        } else if (pid > 0) {
+               /*
+                * In daemon mode (--daemonize), lttng-relayd only exits when
+                * it's ready to accept commands.
+                */
+               for (;;) {
+                       int status;
+                       pid_t wait_pid_ret = waitpid(pid, &status, 0);
+
+                       if (wait_pid_ret < 0) {
+                               if (errno == EINTR) {
+                                       continue;
+                               }
+                               PERROR("waitpid");
+                               ret = -errno;
+                               goto end;
+                       }
+
+                       if (WIFSIGNALED(status)) {
+                               ERR("Relay daemon was killed by signal %d", WTERMSIG(status));
+                               ret = -1;
+                               goto end;
+                       } else if (WIFEXITED(status)) {
+                               DBG("Relay daemon terminated normally (exit status: %d)",
+                                               WEXITSTATUS(status));
+
+                               if (WEXITSTATUS(status) != 0) {
+                                       ERR("Relay daemon terminated with an error (exit status: %d)",
+                                                       WEXITSTATUS(status));
+                                       ret = -1;
+                                       goto end;
+                               }
+                               break;
+                       }
+               }
+
                goto end;
        } else {
                PERROR("fork");
This page took 0.024576 seconds and 5 git commands to generate.