Implement Win32 locking in vl.c (create_pidfile).
PATCH 1/1 Implement Win32 locking in vl.c (create_pidfile).
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.
Signature · 104 lines
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; }
Quoted 8 lines · Carlos O'Donell
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.
Instead of using an #ifdef, I think it would be better to have a win32 specific file and a unix specific file that compiles conditionally depending on the host.
Sort of like and osdep-posix.c and and osdep-win32.c. Both would implement a create_pidfile() function. I think this general approach would help clean up a lot of the win32 code.
Thoughts?
Regards,
Anthony Liguori
Quoted 8 lines
Patch attached for both. Built on linux and tested on Windows Server 2003.
Comments? Please include me in the CC.
Cheers,
Carlos.
Quoted 5 lines · Carlos O'Donell
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.
Quoted 8 lines
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.
I renamed it to qemu_create_pidfile and moved it to osdep.h. I hope that has all necessary includes. Please test.
Thiemo
Quoted 10 lines · Thiemo Seufer
Quoted 9 lines · Carlos O'Donell
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.
Quoted 7 lines
Quoted 4 lines
Patch attached for both. Built on linux and tested on Windows Server 2003.
Comments? Please include me in the CC.
I renamed it to
qemu_create_pidfileand moved it toosdep.h. I hope that has all necessary includes. Please test.
osdep.c, that is, only a tiny bit was for osdep.h. :-)
Thiemo
Quoted 4 lines · Thiemo Seufer
Quoted 2 lines
I renamed it to
qemu_create_pidfileand moved it toosdep.h. I hope that has all necessary includes. Please test.
osdep.c, that is, only a tiny bit was forosdep.h. :-)
Tested on Windows Server 2003, thanks for the quick response.
Cheers,
Carlos.
Signature · 4 lines
Carlos O'Donell CodeSourcery [email protected] (650) 331-3385 x716