[Olsr-dev] [PATCH v1 25/43] main: move lock file creation into the lock_file file

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


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/lock_file.c | 24 +++++++++++++++++++++++-
 src/main.c      | 23 ++++-------------------
 2 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/src/lock_file.c b/src/lock_file.c
index e850c76..6b4bd6d 100644
--- a/src/lock_file.c
+++ b/src/lock_file.c
@@ -90,7 +90,7 @@ char * olsrd_get_default_lockfile(struct olsrd_config *cnf) {
  * Additionally the lock can be killed by removing the
  * locking file.
  */
-bool olsr_create_lock_file(void) {
+static bool olsr_create_lock_file_internal(void) {
 #ifdef _WIN32
   bool success;
 
@@ -141,6 +141,28 @@ bool olsr_create_lock_file(void) {
   return true;
 }
 
+bool olsr_create_lock_file(void) {
+  bool created = false;
+  int i;
+
+  if (!olsr_cnf->host_emul) {
+    return true;
+  }
+
+  for (i = 5; i >= 0; i--) {
+    OLSR_PRINTF(3, "Trying to get olsrd lock...\n");
+    if (olsr_create_lock_file_internal()) {
+      /* lock successfully created */
+      created = true;
+      break;
+    }
+
+    sleep(1);
+  }
+
+  return created;
+}
+
 void olsr_remove_lock_file(void) {
 #ifdef _WIN32
   if (INVALID_HANDLE_VALUE != lck) {
diff --git a/src/main.c b/src/main.c
index 4a46026..c16179e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -114,7 +114,6 @@ int main(int argc, char *argv[]) {
   int argcLocal = argc;
   char conf_file_name[FILENAME_MAX] = { 0 };
   struct ipaddr_str buf;
-  int i;
 
 #ifdef __linux__
   struct interface_olsr *ifn;
@@ -390,24 +389,10 @@ int main(int argc, char *argv[]) {
   /*
    * Create locking file for olsrd, will be cleared after olsrd exits
    */
-  if (!olsr_cnf->host_emul) {
-    bool created = false;
-    for (i = 5; i >= 0; i--) {
-      OLSR_PRINTF(3, "Trying to get olsrd lock...\n");
-      if (olsr_create_lock_file()) {
-        /* lock successfully created */
-        created = true;
-        break;
-      }
-
-      sleep(1);
-    }
-
-    if (!created) {
-      char buf2[1024];
-      snprintf(buf2, sizeof(buf2), "Error, cannot create OLSR lock file '%s'", olsr_cnf->lock_file);
-      olsr_exit(buf2, EXIT_FAILURE);
-    }
+  if (!olsr_create_lock_file()) {
+    char buf2[1024];
+    snprintf(buf2, sizeof(buf2), "Error, cannot create OLSR lock file '%s'", olsr_cnf->lock_file);
+    olsr_exit(buf2, EXIT_FAILURE);
   }
 
   /* Load plugins */
-- 
2.5.0




More information about the Olsr-dev mailing list