+lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
+ OVS_REQUIRES(&lock_table_mutex)
+{
+ HANDLE lock_handle;
+ BOOL retval;
+ OVERLAPPED overl;
+ struct lockfile *lockfile;
+ int fd;
+
+ *pidp = 0;
+
+ fd = open(name, O_RDWR | O_CREAT, 0600);
+ if (fd < 0) {
+ VLOG_WARN("%s: failed to open lock file: %s",
+ name, ovs_strerror(errno));
+ return errno;
+ }
+
+ lock_handle = (HANDLE)_get_osfhandle(fd);
+ if (lock_handle < 0) {
+ VLOG_WARN("%s: failed to get the file handle: %s",
+ name, ovs_strerror(errno));
+ return errno;
+ }
+
+ /* Lock the file 'name' for the region that includes just the first
+ * byte. */
+ overl.hEvent = 0;
+ overl.Offset = 0;
+ overl.OffsetHigh = 0;
+ retval = LockFileEx(lock_handle, LOCKFILE_EXCLUSIVE_LOCK
+ | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &overl);
+ if (!retval) {
+ VLOG_WARN("Failed to lock file : %s", ovs_lasterror_to_string());
+ return EEXIST;
+ }
+
+ lockfile = xmalloc(sizeof *lockfile);
+ lockfile->name = xstrdup(name);
+ lockfile->fd = fd;
+ lockfile->lock_handle = lock_handle;
+
+ *lockfilep = lockfile;
+ return 0;
+}
+#else /* !_WIN32 */
+static void
+lockfile_do_unlock(struct lockfile *lockfile)
+{
+ lockfile_unhash(lockfile);
+}
+
+static int
+lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
+ OVS_REQUIRES(&lock_table_mutex)