+static void
+flock(FILE* fd, int operation)
+{
+ HANDLE hFile;
+ OVERLAPPED ov = {0};
+
+ hFile = (HANDLE)_get_osfhandle(fileno(fd));
+ if (hFile == INVALID_HANDLE_VALUE) {
+ VLOG_FATAL("Failed to get PID file handle (%s).",
+ ovs_lasterror_to_string());
+ }
+
+ if (operation & LOCK_UNLOCK) {
+ if (UnlockFileEx(hFile, 0, 1, 0, &ov) == 0) {
+ VLOG_FATAL("Failed to unlock PID file (%s).",
+ ovs_lasterror_to_string());
+ }
+ } else {
+ /* Use LOCKFILE_FAIL_IMMEDIATELY flag to avoid hang of another daemon that tries to
+ acquire exclusive lock over the same PID file */
+ if (LockFileEx(hFile, operation | LOCKFILE_FAIL_IMMEDIATELY,
+ 0, 1, 0, &ov) == FALSE) {
+ VLOG_FATAL("Failed to lock PID file (%s).",
+ ovs_lasterror_to_string());
+ }
+ }
+}
+