[Olsr-dev] [PATCH v1 38/43] pid_file: add removePidFile function

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


From: Ferry Huberts <(spam-protected)>

So that we can remove it during shutdown

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/pid_file.c | 26 ++++++++++++++++----------
 src/pid_file.h |  2 ++
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/src/pid_file.c b/src/pid_file.c
index d438721..1c6c32b 100644
--- a/src/pid_file.c
+++ b/src/pid_file.c
@@ -65,7 +65,7 @@ bool writePidFile(void) {
 #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);
+      snprintf(buf, sizeof(buf), "Could not create the PID file %s", olsr_cnf->pidfile);
       perror(buf);
       return false;
     }
@@ -81,12 +81,9 @@ bool writePidFile(void) {
       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);
+        snprintf(buf, sizeof(buf), "Could not write 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);
-        }
+        removePidFile();
         return false;
       }
     }
@@ -94,13 +91,22 @@ bool writePidFile(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);
-      }
+      removePidFile();
       return false;
     }
   }
 
   return true;
 }
+
+void removePidFile(void) {
+  if (!olsr_cnf || !olsr_cnf->pidfile) {
+    return;
+  }
+
+  if (remove(olsr_cnf->pidfile) < 0) {
+    char buf[PATH_MAX + 256];
+    snprintf(buf, sizeof(buf), "Could not remove the PID file %s", olsr_cnf->pidfile);
+    perror(buf);
+  }
+}
diff --git a/src/pid_file.h b/src/pid_file.h
index 5858550..5e401e6 100644
--- a/src/pid_file.h
+++ b/src/pid_file.h
@@ -45,4 +45,6 @@
 
 bool writePidFile(void);
 
+void removePidFile(void);
+
 #endif /* SRC_PID_FILE_H_ */
-- 
2.5.0




More information about the Olsr-dev mailing list