[Olsr-dev] [PATCH v1 24/43] pid: move pid file handling into its own file

Ferry Huberts (spam-protected)
Wed Nov 11 17:21:57 CET 2015


From: Ferry Huberts <(spam-protected)>

And refactor it to not invoke olsr_shutdown

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/main.c                      | 55 +++------------------------------
 src/{lock_file.h => pid_file.c} | 68 ++++++++++++++++++++++++++++++++++++-----
 src/{lock_file.h => pid_file.h} | 14 +++------
 3 files changed, 69 insertions(+), 68 deletions(-)
 copy src/{lock_file.h => pid_file.c} (53%)
 copy src/{lock_file.h => pid_file.h} (89%)

diff --git a/src/main.c b/src/main.c
index 70d225c..4a46026 100644
--- a/src/main.c
+++ b/src/main.c
@@ -64,6 +64,7 @@
 #include "gateway.h"
 #include "olsr_niit.h"
 #include "olsr_random.h"
+#include "pid_file.h"
 #include "lock_file.h"
 
 #ifdef __linux__
@@ -90,6 +91,7 @@ static void olsr_shutdown(int) __attribute__ ((noreturn));
 /*
  * Local function prototypes
  */
+
 void olsr_reconfigure(int signo) __attribute__ ((noreturn));
 
 static void print_version(void);
@@ -105,55 +107,6 @@ static char **olsr_argv = NULL;
 struct olsr_cookie_info *def_timer_ci = NULL;
 
 /**
- * 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 */
-#ifdef __WIN32
-    mode_t mode = S_IRUSR | S_IWUSR;
-#else
-    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
-#endif
-    int fd = open(olsr_cnf->pidfile, O_CREAT | O_WRONLY, mode);
-    if (fd < 0) {
-      snprintf(buf, sizeof(buf), "Could not open PID file %s", olsr_cnf->pidfile);
-      perror(buf);
-      olsr_shutdown(0);
-    }
-
-    /* write the PID */
-    {
-      pid_t pid = getpid();
-      int chars = snprintf(buf, sizeof(buf), "%d", (int)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", (int)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(0);
-      }
-    }
-
-    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(0);
-    }
-  }
-}
-
-/**
  * Main entrypoint
  */
 
@@ -430,7 +383,9 @@ int main(int argc, char *argv[]) {
   }
 #endif /* _WIN32 */
 
-  writePidFile();
+  if (!writePidFile()) {
+    olsr_shutdown(0);
+  }
 
   /*
    * Create locking file for olsrd, will be cleared after olsrd exits
diff --git a/src/lock_file.h b/src/pid_file.c
similarity index 53%
copy from src/lock_file.h
copy to src/pid_file.c
index 0ff9d95..d438721 100644
--- a/src/lock_file.h
+++ b/src/pid_file.c
@@ -38,17 +38,69 @@
  *
  */
 
-#ifndef SRC_LOCK_FILE_H_
-#define SRC_LOCK_FILE_H_
-
+#include "pid_file.h"
 #include "olsr_cfg.h"
 
-#include <stdbool.h>
+#include <unistd.h>
+#include <limits.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#ifdef __WIN32
+#include <process.h>
+#endif
+
+/**
+ * Write the current PID to the configured PID file (if one is configured)
+ */
+bool writePidFile(void) {
+  if (olsr_cnf->pidfile) {
+    char buf[PATH_MAX + 256];
 
-char * olsrd_get_default_lockfile(struct olsrd_config *cnf);
+    /* create / open the PID file */
+#ifdef __WIN32
+    mode_t mode = S_IRUSR | S_IWUSR;
+#else
+    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+#endif
+    int fd = open(olsr_cnf->pidfile, O_CREAT | O_WRONLY, mode);
+    if (fd < 0) {
+      snprintf(buf, sizeof(buf), "Could not open PID file %s", olsr_cnf->pidfile);
+      perror(buf);
+      return false;
+    }
 
-bool olsr_create_lock_file(void);
+    /* write the PID */
+    {
+#ifdef __WIN32
+      pid_t pid = _getpid();
+#else
+      pid_t pid = getpid();
+#endif
+      int chars = snprintf(buf, sizeof(buf), "%d", (int) 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", (int) 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);
+        }
+        return false;
+      }
+    }
 
-void olsr_remove_lock_file(void);
+    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);
+      }
+      return false;
+    }
+  }
 
-#endif /* SRC_LOCK_FILE_H_ */
+  return true;
+}
diff --git a/src/lock_file.h b/src/pid_file.h
similarity index 89%
copy from src/lock_file.h
copy to src/pid_file.h
index 0ff9d95..5858550 100644
--- a/src/lock_file.h
+++ b/src/pid_file.h
@@ -38,17 +38,11 @@
  *
  */
 
-#ifndef SRC_LOCK_FILE_H_
-#define SRC_LOCK_FILE_H_
-
-#include "olsr_cfg.h"
+#ifndef SRC_PID_FILE_H_
+#define SRC_PID_FILE_H_
 
 #include <stdbool.h>
 
-char * olsrd_get_default_lockfile(struct olsrd_config *cnf);
-
-bool olsr_create_lock_file(void);
-
-void olsr_remove_lock_file(void);
+bool writePidFile(void);
 
-#endif /* SRC_LOCK_FILE_H_ */
+#endif /* SRC_PID_FILE_H_ */
-- 
2.5.0




More information about the Olsr-dev mailing list