Add support for understanding ICMP type and code in flow entries.
[sliver-openvswitch.git] / lib / daemon.c
index 22339f9..b8fc5af 100644 (file)
@@ -39,6 +39,7 @@
 #include <string.h>
 #include <unistd.h>
 #include "fatal-signal.h"
+#include "dirs.h"
 #include "util.h"
 
 #define THIS_MODULE VLM_daemon
@@ -58,9 +59,9 @@ static bool force;
 char *
 make_pidfile_name(const char *name) 
 {
-    return (!name ? xasprintf("%s/%s.pid", RUNDIR, program_name)
+    return (!name ? xasprintf("%s/%s.pid", ofp_rundir, program_name)
             : *name == '/' ? xstrdup(name)
-            : xasprintf("%s/%s", RUNDIR, name));
+            : xasprintf("%s/%s", ofp_rundir, name));
 }
 
 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
@@ -134,8 +135,8 @@ die_if_already_running(void)
     pid_t pid = already_running();
     if (pid) {
         if (!force) {
-            fatal(0, "%s: already running as pid %ld",
-                  get_pidfile(), (long int) pid);
+            ofp_fatal(0, "%s: already running as pid %ld",
+                      get_pidfile(), (long int) pid);
         } else {
             VLOG_WARN("%s: %s already running as pid %ld",
                       get_pidfile(), program_name, (long int) pid);
@@ -204,7 +205,7 @@ daemonize(void)
         char c = 0;
         int fds[2];
         if (pipe(fds) < 0) {
-            fatal(errno, "pipe failed");
+            ofp_fatal(errno, "pipe failed");
         }
 
         switch (fork()) {
@@ -227,7 +228,7 @@ daemonize(void)
 
         case -1:
             /* Error. */
-            fatal(errno, "could not fork");
+            ofp_fatal(errno, "could not fork");
             break;
         }
     } else {
@@ -235,3 +236,61 @@ daemonize(void)
     }
 }
 
+void
+daemon_usage(void)
+{
+    printf(
+        "\nDaemon options:\n"
+        "  -D, --detach            run in background as daemon\n"
+        "  -P, --pidfile[=FILE]    create pidfile (default: %s/%s.pid)\n"
+        "  -f, --force             with -P, start even if already running\n",
+        ofp_rundir, program_name);
+}
+
+/* Opens and reads a PID from 'pidfile'.  Returns the nonnegative PID if
+ * successful, otherwise a negative errno value. */
+pid_t
+read_pidfile(const char *pidfile)
+{
+    char line[128];
+    struct flock lck;
+    FILE *file;
+    int error;
+
+    file = fopen(pidfile, "r");
+    if (!file) {
+        error = errno;
+        VLOG_WARN("%s: open: %s", pidfile, strerror(error));
+        goto error;
+    }
+
+    lck.l_type = F_WRLCK;
+    lck.l_whence = SEEK_SET;
+    lck.l_start = 0;
+    lck.l_len = 0;
+    if (fcntl(fileno(file), F_GETLK, &lck)) {
+        error = errno;
+        VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
+        goto error;
+    }
+
+    if (!fgets(line, sizeof line, file)) {
+        error = errno;
+        VLOG_WARN("%s: read: %s", pidfile, strerror(error));
+        goto error;
+    }
+
+    if (lck.l_pid != strtoul(line, NULL, 10)) {
+        error = ESRCH;
+        VLOG_WARN("l_pid (%ld) != %s pid (%s)",
+                   (long int) lck.l_pid, pidfile, line);
+        goto error;
+    }
+
+    fclose(file);
+    return lck.l_pid;
+
+error:
+    fclose(file);
+    return -error;
+}