Commit aa26bb2dacdc32bac84e7c72c501b4691721679e

Authored by ths
1 parent 209afb9e

qemu_create_pidfile implementation for Win32, based on a patch by

Carlos O'Donell.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2540 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 3 changed files with 51 additions and 24 deletions
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
27 #include <string.h> 27 #include <string.h>
28 #include <errno.h> 28 #include <errno.h>
29 #include <unistd.h> 29 #include <unistd.h>
  30 +#include <fcntl.h>
30 #ifdef HOST_SOLARIS 31 #ifdef HOST_SOLARIS
31 #include <sys/types.h> 32 #include <sys/types.h>
32 #include <sys/statvfs.h> 33 #include <sys/statvfs.h>
@@ -216,3 +217,50 @@ char *qemu_strdup(const char *str) @@ -216,3 +217,50 @@ char *qemu_strdup(const char *str)
216 strcpy(ptr, str); 217 strcpy(ptr, str);
217 return ptr; 218 return ptr;
218 } 219 }
  220 +
  221 +int qemu_create_pidfile(const char *filename)
  222 +{
  223 + char buffer[128];
  224 + int len;
  225 +#ifndef _WIN32
  226 + int fd;
  227 +
  228 + fd = open(filename, O_RDWR | O_CREAT, 0600);
  229 + if (fd == -1)
  230 + return -1;
  231 +
  232 + if (lockf(fd, F_TLOCK, 0) == -1)
  233 + return -1;
  234 +
  235 + len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
  236 + if (write(fd, buffer, len) != len)
  237 + return -1;
  238 +#else
  239 + HANDLE file;
  240 + DWORD flags;
  241 + OVERLAPPED overlap;
  242 + BOOL ret;
  243 +
  244 + /* Open for writing with no sharing. */
  245 + file = CreateFile(filename, GENERIC_WRITE, 0, NULL,
  246 + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  247 +
  248 + if (file == INVALID_HANDLE_VALUE)
  249 + return -1;
  250 +
  251 + flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
  252 + overlap.hEvent = 0;
  253 + /* Lock 1 byte. */
  254 + ret = LockFileEx(file, flags, 0, 0, 1, &overlap);
  255 + if (ret == 0)
  256 + return -1;
  257 +
  258 + /* Write PID to file. */
  259 + len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
  260 + ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
  261 + &overlap, NULL);
  262 + if (ret == 0)
  263 + return -1;
  264 +#endif
  265 + return 0;
  266 +}
@@ -15,4 +15,6 @@ void qemu_vfree(void *ptr); @@ -15,4 +15,6 @@ void qemu_vfree(void *ptr);
15 15
16 void *get_mmap_addr(unsigned long size); 16 void *get_mmap_addr(unsigned long size);
17 17
  18 +int qemu_create_pidfile(const char *filename);
  19 +
18 #endif 20 #endif
@@ -4403,29 +4403,6 @@ void usb_info(void) @@ -4403,29 +4403,6 @@ void usb_info(void)
4403 } 4403 }
4404 } 4404 }
4405 4405
4406 -static int create_pidfile(const char *filename)  
4407 -{  
4408 - int fd;  
4409 - char buffer[128];  
4410 - int len;  
4411 -  
4412 - fd = open(filename, O_RDWR | O_CREAT, 0600);  
4413 - if (fd == -1)  
4414 - return -1;  
4415 -  
4416 - /* XXX: No locking for Win32 implemented */  
4417 -#ifndef _WIN32  
4418 - if (lockf(fd, F_TLOCK, 0) == -1)  
4419 - return -1;  
4420 -#endif  
4421 -  
4422 - len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());  
4423 - if (write(fd, buffer, len) != len)  
4424 - return -1;  
4425 -  
4426 - return 0;  
4427 -}  
4428 -  
4429 /***********************************************************/ 4406 /***********************************************************/
4430 /* dumb display */ 4407 /* dumb display */
4431 4408
@@ -7405,7 +7382,7 @@ int main(int argc, char **argv) @@ -7405,7 +7382,7 @@ int main(int argc, char **argv)
7405 } 7382 }
7406 #endif 7383 #endif
7407 7384
7408 - if (pid_file && create_pidfile(pid_file) != 0) { 7385 + if (pid_file && qemu_create_pidfile(pid_file) != 0) {
7409 if (daemonize) { 7386 if (daemonize) {
7410 uint8_t status = 1; 7387 uint8_t status = 1;
7411 write(fds[1], &status, 1); 7388 write(fds[1], &status, 1);