[ovs-dev] [PATCH 02/16] windows: Added lockf function and lock PID file

Paul Boca pboca at cloudbasesolutions.com
Fri Jun 17 16:06:39 UTC 2016


If the PID file isn't locked then appctl.py detects it as stale and
bails out without doing anything.
Because of this lots of Python tests fail.

Signed-off-by: Paul-Daniel Boca <pboca at cloudbasesolutions.com>
---
 lib/daemon-windows.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/lib/daemon-windows.c b/lib/daemon-windows.c
index 8cf0fea..161e754 100644
--- a/lib/daemon-windows.c
+++ b/lib/daemon-windows.c
@@ -18,6 +18,7 @@
 #include "daemon.h"
 #include "daemon-private.h"
 #include <stdio.h>
+#include <io.h>
 #include <stdlib.h>
 #include "dirs.h"
 #include "ovs-thread.h"
@@ -26,6 +27,12 @@
 
 VLOG_DEFINE_THIS_MODULE(daemon_windows);
 
+/* Constants for flock function */
+#define	LOCK_SH	0x0                         /* Shared lock. */
+#define	LOCK_EX	LOCKFILE_EXCLUSIVE_LOCK     /* Exclusive lock. */
+#define	LOCK_NB	LOCKFILE_FAIL_IMMEDIATELY	/* Don't block when locking. */
+#define	LOCK_UN	0x80000000                  /* Unlock. */
+
 static bool service_create;          /* Was --service specified? */
 static bool service_started;         /* Have we dispatched service to start? */
 
@@ -414,6 +421,33 @@ unlink_pidfile(void)
     }
 }
 
+static int
+flock(FILE* fd, int operation)
+{
+    NTSTATUS status;
+    HANDLE hFile;
+    OVERLAPPED ov = {0};
+
+    hFile = (HANDLE)_get_osfhandle(fileno(filep_pidfile));
+    if (hFile == INVALID_HANDLE_VALUE) {
+        VLOG_FATAL("Invalid handle value");
+        return -1;
+    }
+
+    if (operation & LOCK_UN) {
+        if (UnlockFileEx(hFile, 0, 0, 0xFFFF0000, &ov) == FALSE) {
+            return -1;
+        }
+    } else {
+        if (LockFileEx(hFile, operation, 0, 0, 0xFFFF0000, &ov) == FALSE) {
+            VLOG_FATAL("LockFileEx failed, status = 0x%08x\n", status);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
 /* If a pidfile has been configured, creates it and stores the running
  * process's pid in it.  Ensures that the pidfile will be deleted when the
  * process exits. */
@@ -444,6 +478,16 @@ make_pidfile(void)
         VLOG_FATAL("Failed to write into the pidfile %s", pidfile);
     }
 
+    fflush(filep_pidfile);
+    error = flock(filep_pidfile, LOCK_SH);
+    if (error) {
+        /* Looks like we failed to acquire the lock.  Note that, if we failed
+         * for some other reason (and '!overwrite_pidfile'), we will have
+         * left 'tmpfile' as garbage in the file system. */
+        VLOG_FATAL("%s: fcntl(F_SETLK) failed (%s)", pidfile,
+                   ovs_strerror(error));
+    }
+
     /* Don't close the pidfile till the process exits. */
 }
 
-- 
2.7.2.windows.1



More information about the dev mailing list