qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Carlos O'Donell <carlos@codesourcery.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH] Implement Win32 locking in vl.c (create_pidfile).
Date: Fri, 23 Mar 2007 19:10:15 -0400	[thread overview]
Message-ID: <20070323231014.GR26700@lios> (raw)


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
carlos@codesourcery.com
(650) 331-3385 x716

2007-03-23  Carlos O'Donell  <carlos@codesourcery.com>

	* 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	other threads:[~2007-03-23 23:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-23 23:10 Carlos O'Donell [this message]
2007-03-25 17:58 ` [Qemu-devel] [PATCH] Implement Win32 locking in vl.c (create_pidfile) Anthony Liguori
2007-03-25 21:35 ` Thiemo Seufer
2007-03-25 23:30   ` Thiemo Seufer
2007-03-27 13:22     ` Carlos O'Donell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070323231014.GR26700@lios \
    --to=carlos@codesourcery.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).