ustd: add daemon mode
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Tue, 13 Oct 2009 21:55:01 +0000 (17:55 -0400)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Tue, 13 Oct 2009 21:55:01 +0000 (17:55 -0400)
ustd/ustd.c
usttrace

index 2393ea3d45b5befa4c01cc628eeb57279b48cea4..001977660a6ec0c86d809ce4a0ccb298a638427b 100644 (file)
@@ -52,6 +52,7 @@
 
 char *sock_path=NULL;
 char *trace_path=NULL;
+int daemon_mode = 0;
 
 /* Number of active buffers and the mutex to protect it. */
 int active_buffers = 0;
@@ -473,7 +474,9 @@ void usage(void)
        fprintf(stderr, "Usage:\nustd OPTIONS\n\nOptions:\n"
                        "\t-h\t\tDisplay this usage.\n"
                        "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
-                       "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n");
+                       "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n"
+                       "\t-d\t\tStart as a daemon.\n"
+                       "\t--pidfile FILE\tWrite the PID in this file (when using -d).\n");
 }
 
 int parse_args(int argc, char **argv)
@@ -483,12 +486,13 @@ int parse_args(int argc, char **argv)
        while (1) {
                int option_index = 0;
                static struct option long_options[] = {
+                       {"pidfile", 1, 0, 'p'},
                        {"help", 0, 0, 'h'},
                        {"version", 0, 0, 'V'},
                        {0, 0, 0, 0}
                };
 
-               c = getopt_long(argc, argv, "hs:o:", long_options, &option_index);
+               c = getopt_long(argc, argv, "hs:o:d", long_options, &option_index);
                if (c == -1)
                        break;
 
@@ -509,6 +513,9 @@ int parse_args(int argc, char **argv)
                                return -1;
                        }
                        break;
+               case 'd':
+                       daemon_mode = 1;
+                       break;
                case 'h':
                        usage();
                        exit(0);
@@ -531,7 +538,7 @@ void sigterm_handler(int sig)
        terminate_req = 1;
 }
 
-int main(int argc, char **argv)
+int start_ustd(int fd)
 {
        struct ustcomm_ustd ustd;
        int result;
@@ -552,11 +559,6 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       result = parse_args(argc, argv);
-       if(result == -1) {
-               exit(1);
-       }
-
        result = ustcomm_init_ustd(&ustd, sock_path);
        if(result == -1) {
                ERR("failed to initialize socket");
@@ -580,6 +582,24 @@ int main(int argc, char **argv)
                return 1;
        }
 
+       /* Notify parent that we are successfully started. */
+       if(fd != -1) {
+               /* write any one character */
+               result = write(fd, "!", 1);
+               if(result == -1) {
+                       PERROR("write");
+                       return -1;
+               }
+               if(result != 1) {
+                       ERR("Problem sending confirmation of daemon start to parent");
+                       return -1;
+               }
+               result = close(fd);
+               if(result == -1) {
+                       PERROR("close");
+               }
+       }
+
        /* app loop */
        for(;;) {
                char *recvbuf;
@@ -629,3 +649,64 @@ int main(int argc, char **argv)
 
        return 0;
 }
+
+int start_ustd_daemon()
+{
+       int result;
+       int fd[2];
+
+       result = pipe(fd);
+
+       result = fork();
+       if(result == -1) {
+               PERROR("fork");
+               return -1;
+       }
+       else if(result == 0) {
+               return start_ustd(fd[1]);
+       }
+       else {
+               char buf;
+
+               result = read(fd[0], &buf, 1);
+               if(result == -1) {
+                       PERROR("read");
+                       return -1;
+               }
+               if(result != 1) {
+                       ERR("did not receive valid confirmation that the daemon is started");
+                       return -1;
+               }
+
+               result = close(fd[0]);
+               if(result == -1) {
+                       PERROR("close");
+               }
+
+               DBG("The daemon is now successfully started");
+       }
+
+       /* Wait for confirmation that the server is ready. */
+
+
+       return 0;
+}
+
+int main(int argc, char **argv)
+{
+       int result;
+
+       result = parse_args(argc, argv);
+       if(result == -1) {
+               exit(1);
+       }
+
+       if(daemon_mode) {
+               result = start_ustd_daemon();
+       }
+       else {
+               result = start_ustd(-1);
+       }
+
+       return result;
+}
index e8ab13c7079ea2ca84eaa048805e2e13ae58bc0c..cb9464086adb7e82efad62059f2dc14e06874eca 100755 (executable)
--- a/usttrace
+++ b/usttrace
@@ -79,7 +79,10 @@ fi
 # Wait for the daemon to settle
 # If we don't, the process may try to connect to its socket before
 # it's ready.
-# FIXME: is there a way to make sure this works every time?
+# FIXME: is there a way to make sure this works every time? Yes,
+# we could have a mode where ustd daemonizes itself. Therefore
+# by the time the control returns to the console, it's ready.
+
 sleep 0.5
 
 # Establish the environment for the command
This page took 0.026767 seconds and 4 git commands to generate.