[Olsr-dev] [PATCH v1 14/43] lock_file: add olsr_remove_lock_file function

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


From: Ferry Huberts <(spam-protected)>

And use it in the error paths during creation

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

diff --git a/src/lock_file.c b/src/lock_file.c
index 5ed30dd..e850c76 100644
--- a/src/lock_file.c
+++ b/src/lock_file.c
@@ -104,18 +104,14 @@ bool olsr_create_lock_file(void) {
       NULL);
   CreateEvent(NULL, TRUE, FALSE, olsr_cnf->lock_file);
   if ((INVALID_HANDLE_VALUE == lck) || (ERROR_ALREADY_EXISTS == GetLastError())) {
-    if (INVALID_HANDLE_VALUE != lck) {
-      CloseHandle(lck);
-      lck = INVALID_HANDLE_VALUE;
-    }
+    olsr_remove_lock_file();
     return false;
   }
 
   success = LockFile( lck, 0, 0, 0, 0);
 
   if (!success) {
-    CloseHandle(lck);
-    lck = INVALID_HANDLE_VALUE;
+    olsr_remove_lock_file();
     return false;
   }
 
@@ -125,6 +121,7 @@ bool olsr_create_lock_file(void) {
   /* create file for lock */
   lock_fd = open(olsr_cnf->lock_file, O_WRONLY | O_CREAT, S_IRWXU);
   if (lock_fd < 0) {
+    olsr_remove_lock_file();
     return false;
   }
 
@@ -136,11 +133,30 @@ bool olsr_create_lock_file(void) {
   lck.l_pid = 0;
 
   if (fcntl(lock_fd, F_SETLK, &lck) == -1) {
-    close(lock_fd);
-    lock_fd = -1;
+    olsr_remove_lock_file();
     return false;
   }
 #endif /* _WIN32 */
 
   return true;
 }
+
+void olsr_remove_lock_file(void) {
+#ifdef _WIN32
+  if (INVALID_HANDLE_VALUE != lck) {
+    CloseHandle(lck);
+    lck = INVALID_HANDLE_VALUE;
+  }
+#else
+  if (lock_fd >= 0) {
+    close(lock_fd);
+    lock_fd = -1;
+  }
+#endif
+
+  if (!olsr_cnf->lock_file || !strlen(olsr_cnf->lock_file)) {
+    return;
+  }
+
+  remove(olsr_cnf->lock_file);
+}
diff --git a/src/lock_file.h b/src/lock_file.h
index a7d31d4..0ff9d95 100644
--- a/src/lock_file.h
+++ b/src/lock_file.h
@@ -49,4 +49,6 @@ char * olsrd_get_default_lockfile(struct olsrd_config *cnf);
 
 bool olsr_create_lock_file(void);
 
+void olsr_remove_lock_file(void);
+
 #endif /* SRC_LOCK_FILE_H_ */
-- 
2.5.0




More information about the Olsr-dev mailing list