mirror of
https://gitlab.com/qemu-project/qemu
synced 2024-11-05 20:35:44 +00:00
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
This commit is contained in:
parent
209afb9e0d
commit
aa26bb2dac
3 changed files with 51 additions and 24 deletions
48
osdep.c
48
osdep.c
|
@ -27,6 +27,7 @@
|
|||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#ifdef HOST_SOLARIS
|
||||
#include <sys/types.h>
|
||||
#include <sys/statvfs.h>
|
||||
|
@ -216,3 +217,50 @@ char *qemu_strdup(const char *str)
|
|||
strcpy(ptr, str);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
int qemu_create_pidfile(const char *filename)
|
||||
{
|
||||
char buffer[128];
|
||||
int len;
|
||||
#ifndef _WIN32
|
||||
int fd;
|
||||
|
||||
fd = open(filename, O_RDWR | O_CREAT, 0600);
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
|
||||
if (lockf(fd, F_TLOCK, 0) == -1)
|
||||
return -1;
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
2
osdep.h
2
osdep.h
|
@ -15,4 +15,6 @@ void qemu_vfree(void *ptr);
|
|||
|
||||
void *get_mmap_addr(unsigned long size);
|
||||
|
||||
int qemu_create_pidfile(const char *filename);
|
||||
|
||||
#endif
|
||||
|
|
25
vl.c
25
vl.c
|
@ -4403,29 +4403,6 @@ void usb_info(void)
|
|||
}
|
||||
}
|
||||
|
||||
static int create_pidfile(const char *filename)
|
||||
{
|
||||
int fd;
|
||||
char buffer[128];
|
||||
int len;
|
||||
|
||||
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;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/***********************************************************/
|
||||
/* dumb display */
|
||||
|
||||
|
@ -7405,7 +7382,7 @@ int main(int argc, char **argv)
|
|||
}
|
||||
#endif
|
||||
|
||||
if (pid_file && create_pidfile(pid_file) != 0) {
|
||||
if (pid_file && qemu_create_pidfile(pid_file) != 0) {
|
||||
if (daemonize) {
|
||||
uint8_t status = 1;
|
||||
write(fds[1], &status, 1);
|
||||
|
|
Loading…
Reference in a new issue