From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1HUt2F-00039D-0l for qemu-devel@nongnu.org; Fri, 23 Mar 2007 19:19:03 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1HUt2E-00038H-E2 for qemu-devel@nongnu.org; Fri, 23 Mar 2007 19:19:02 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HUt2E-000386-6N for qemu-devel@nongnu.org; Fri, 23 Mar 2007 18:19:02 -0500 Received: from mail.codesourcery.com ([65.74.133.4]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1HUt0G-00013y-Ex for qemu-devel@nongnu.org; Fri, 23 Mar 2007 19:17:00 -0400 Date: Fri, 23 Mar 2007 19:10:15 -0400 From: Carlos O'Donell Message-ID: <20070323231014.GR26700@lios> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Subject: [Qemu-devel] [PATCH] Implement Win32 locking in vl.c (create_pidfile). Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org 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 * 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; }