The following patch implements Win32 locking for vl.c (create_pidfile).
Builds for mingw32, and tested on a Windows Server 2003 host by running
two qemu's with the same -pidfile option.

When cross-compiling the use of --enable-mingw32 should effect AIOLIBS,
otherwise the build will use "-lrt" and this is not valid when compiling
with mingw32.

Patch attached for both. Built on linux and tested on Windows Server
2003.

Comments? Please include me in the CC.

Cheers,
Carlos.
-- 
Carlos O'Donell
CodeSourcery
[EMAIL PROTECTED]
(650) 331-3385 x716

2007-03-23  Carlos O'Donell  <[EMAIL PROTECTED]>

        * configure: Move determination of AIOLIBS until after
        all configure options have been handled.
        * vl.c (create_pidfile) [_WIN32]: Implement Win32 file
        locking for pid file.

Index: configure
===================================================================
RCS file: /sources/qemu/qemu/configure,v
retrieving revision 1.132
diff -u -p -r1.132 configure
--- configure   19 Mar 2007 12:22:40 -0000      1.132
+++ configure   23 Mar 2007 23:08:08 -0000
@@ -159,12 +159,6 @@ if [ "$bsd" = "yes" ] ; then
   fi
 fi
 
-if [ "$bsd" = "yes" -o "$darwin" = "yes" -o "$mingw32" = "yes" ] ; then
-    AIOLIBS=
-else
-    AIOLIBS="-lrt"
-fi
-
 # find source path
 source_path=`dirname "$0"`
 if [ -z "$source_path" ]; then
@@ -260,6 +254,12 @@ for opt do
   esac
 done
 
+if [ "$bsd" = "yes" -o "$darwin" = "yes" -o "$mingw32" = "yes" ] ; then
+    AIOLIBS=
+else
+    AIOLIBS="-lrt"
+fi
+
 # default flags for all hosts
 CFLAGS="$CFLAGS -Wall -O2 -g -fno-strict-aliasing"
 LDFLAGS="$LDFLAGS -g"
Index: vl.c
===================================================================
RCS file: /sources/qemu/qemu/vl.c,v
retrieving revision 1.270
diff -u -p -r1.270 vl.c
--- vl.c        22 Mar 2007 12:36:53 -0000      1.270
+++ vl.c        23 Mar 2007 23:08:08 -0000
@@ -4397,24 +4397,48 @@ void usb_info(void)
 
 static int create_pidfile(const char *filename)
 {
-    int fd;
     char buffer[128];
     int len;
+#ifndef _WIN32
+    int fd;
 
     fd = open(filename, O_RDWR | O_CREAT, 0600);
     if (fd == -1)
         return -1;
 
-    /* XXX: No locking for Win32 implemented */
-#ifndef _WIN32
     if (lockf(fd, F_TLOCK, 0) == -1)
         return -1;
-#endif
 
     len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
     if (write(fd, buffer, len) != len)
         return -1;
+#else
+    HANDLE file;
+    DWORD flags;
+    OVERLAPPED overlap;
+    BOOL ret;
+
+    /* Open for writing with no sharing. */
+    file = CreateFile(filename, GENERIC_WRITE, 0, NULL, 
+                     OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+
+    if (file == INVALID_HANDLE_VALUE)
+      return -1;
+
+    flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
+    overlap.hEvent = 0;
+    /* Lock 1 byte. */
+    ret = LockFileEx(file, flags, 0, 0, 1, &overlap);
+    if (ret == 0)
+      return -1;
 
+    /* Write PID to file. */
+    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
+    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len, 
+                     &overlap, NULL);
+    if (ret == 0)
+      return -1;
+#endif
     return 0;
 }
 


Reply via email to