Separate qemu_pidfile() into OS specific versions

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
This commit is contained in:
Jes Sorensen 2010-10-26 10:39:25 +02:00 committed by Blue Swirl
parent ff753bb9a6
commit bc4a957c46
3 changed files with 45 additions and 38 deletions

View File

@ -361,3 +361,24 @@ int qemu_eventfd(int fds[2])
return qemu_pipe(fds); return qemu_pipe(fds);
} }
int qemu_create_pidfile(const char *filename)
{
char buffer[128];
int len;
int fd;
fd = qemu_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;
}
return 0;
}

View File

@ -240,3 +240,27 @@ void os_pidfile_error(void)
{ {
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
} }
int qemu_create_pidfile(const char *filename)
{
char buffer[128];
int len;
HANDLE file;
OVERLAPPED overlap;
BOOL ret;
memset(&overlap, 0, sizeof(overlap));
file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
return -1;
}
len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
&overlap, NULL);
if (ret == 0) {
return -1;
}
return 0;
}

38
osdep.c
View File

@ -73,44 +73,6 @@ int qemu_madvise(void *addr, size_t len, int advice)
#endif #endif
} }
int qemu_create_pidfile(const char *filename)
{
char buffer[128];
int len;
#ifndef _WIN32
int fd;
fd = qemu_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;
OVERLAPPED overlap;
BOOL ret;
memset(&overlap, 0, sizeof(overlap));
file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
return -1;
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;
}
/* /*
* Opens a file with FD_CLOEXEC set * Opens a file with FD_CLOEXEC set