[Olsr-dev] [PATCH v1 1/1] main: add -pidfile command line option

Ferry Huberts (spam-protected)
Wed Jan 30 13:33:50 CET 2013


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/cfgparser/olsrd_conf.c |  1 +
 src/main.c                 | 57 +++++++++++++++++++++++++++++++++++++++++++++-
 src/olsr_cfg.h             |  1 +
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/src/cfgparser/olsrd_conf.c b/src/cfgparser/olsrd_conf.c
index 8c4adb8..b2cc735 100644
--- a/src/cfgparser/olsrd_conf.c
+++ b/src/cfgparser/olsrd_conf.c
@@ -838,6 +838,7 @@ set_default_cnf(struct olsrd_config *cnf)
 
   cnf->debug_level = DEF_DEBUGLVL;
   cnf->no_fork = false;
+  cnf->pidfile = NULL;
   cnf->host_emul = false;
   cnf->ip_version = AF_INET;
   cnf->ipsize = sizeof(struct in_addr);
diff --git a/src/main.c b/src/main.c
index 649bc9d..2e9afa1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -210,6 +210,50 @@ static int olsr_create_lock_file(bool noExitOnFail) {
 }
 
 /**
+ * Write the current PID to the configured PID file (if one is configured)
+ */
+static void writePidFile(void) {
+  if (olsr_cnf->pidfile) {
+    char buf[PATH_MAX + 256];
+
+    /* create / open the PID file */
+    int fd = open(olsr_cnf->pidfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+    if (fd < 0) {
+      snprintf(buf, sizeof(buf), "Could not open PID file %s", olsr_cnf->pidfile);
+      perror(buf);
+      olsr_shutdown(1);
+    }
+
+    /* write the PID */
+    {
+      pid_t pid = getpid();
+      int chars = snprintf(buf, sizeof(buf), "%d", pid);
+      ssize_t chars_written = write(fd, buf, chars);
+      if (chars_written != chars) {
+        close(fd);
+        snprintf(buf, sizeof(buf), "Could not write the PID %d to the PID file %s", pid, olsr_cnf->pidfile);
+        perror(buf);
+        if (remove(olsr_cnf->pidfile) < 0) {
+          snprintf(buf, sizeof(buf), "Could not remove the PID file %s", olsr_cnf->pidfile);
+          perror(buf);
+        }
+        olsr_shutdown(1);
+      }
+    }
+
+    if (close(fd) < 0) {
+      snprintf(buf, sizeof(buf), "Could not close PID file %s", olsr_cnf->pidfile);
+      perror(buf);
+      if (remove(olsr_cnf->pidfile) < 0) {
+        snprintf(buf, sizeof(buf), "Could not remove the PID file %s", olsr_cnf->pidfile);
+        perror(buf);
+      }
+      olsr_shutdown(1);
+    }
+  }
+}
+
+/**
  * loads a config file
  * @return <0 if load failed, 0 otherwise
  */
@@ -581,6 +625,8 @@ int main(int argc, char *argv[]) {
   }
 #endif /* _WIN32 */
 
+  writePidFile();
+
   /*
    * Create locking file for olsrd, will be cleared after olsrd exits
    */
@@ -880,7 +926,8 @@ static void print_usage(bool error) {
         "  [-hint <hello interval (secs)>] [-tcint <tc interval (secs)>]\n"
         "  [-midint <mid interval (secs)>] [-hnaint <hna interval (secs)>]\n"
         "  [-T <Polling Rate (secs)>] [-nofork] [-hemu <ip_address>]\n"
-        "  [-lql <LQ level>] [-lqa <LQ aging factor>]\n",
+        "  [-lql <LQ level>] [-lqa <LQ aging factor>]\n"
+        "  [-pidfile <pid file>]\n",
         error ? "Error in command line parameters!\n" : "");
 }
 
@@ -1174,6 +1221,14 @@ static int olsr_process_arguments(int argc, char *argv[],
       continue;
     }
 
+    if (strcmp(*argv, "-pidfile") == 0) {
+      NEXT_ARG;
+      CHECK_ARGC;
+
+      cnf->pidfile = *argv;
+      continue;
+    }
+
     return -1;
   }
   return 0;
diff --git a/src/olsr_cfg.h b/src/olsr_cfg.h
index 80660a5..24bd69c 100644
--- a/src/olsr_cfg.h
+++ b/src/olsr_cfg.h
@@ -254,6 +254,7 @@ struct olsrd_config {
   uint16_t olsrport;
   int debug_level;
   bool no_fork;
+  char * pidfile;
   bool host_emul;
   int ip_version;
   bool allow_no_interfaces;
-- 
1.7.11.7





More information about the Olsr-dev mailing list