aboutsummaryrefslogtreecommitdiff
path: root/os-posix.c
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2018-08-31 16:53:12 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2018-10-02 18:47:55 +0200
commit9e6bdef224f700c057462a7d5e9b4a2770e04569 (patch)
treea9328d62c36923ef04d2896fda9f05f348d03587 /os-posix.c
parent71bb4ce1b5592cdc03abc48cdf4ecb15b2db81a0 (diff)
downloadqemu-9e6bdef224f700c057462a7d5e9b4a2770e04569.zip
qemu-9e6bdef224f700c057462a7d5e9b4a2770e04569.tar.gz
qemu-9e6bdef224f700c057462a7d5e9b4a2770e04569.tar.bz2
util: add qemu_write_pidfile()
There are variants of qemu_create_pidfile() in qemu-pr-helper and qemu-ga. Let's have a common implementation in libqemuutil. The code is initially based from pr-helper write_pidfile(), with various improvements and suggestions from Daniel Berrangé: QEMU will leave the pidfile existing on disk when it exits which initially made me think it avoids the deletion race. The app managing QEMU, however, may well delete the pidfile after it has seen QEMU exit, and even if the app locks the pidfile before deleting it, there is still a race. eg consider the following sequence QEMU 1 libvirtd QEMU 2 1. lock(pidfile) 2. exit() 3. open(pidfile) 4. lock(pidfile) 5. open(pidfile) 6. unlink(pidfile) 7. close(pidfile) 8. lock(pidfile) IOW, at step 8 the new QEMU has successfully acquired the lock, but the pidfile no longer exists on disk because it was deleted after the original QEMU exited. While we could just say no external app should ever delete the pidfile, I don't think that is satisfactory as people don't read docs, and admins don't like stale pidfiles being left around on disk. To make this robust, I think we might want to copy libvirt's approach to pidfile acquisition which runs in a loop and checks that the file on disk /after/ acquiring the lock matches the file that was locked. Then we could in fact safely let QEMU delete its own pidfiles on clean exit.. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'os-posix.c')
-rw-r--r--os-posix.c24
1 files changed, 0 insertions, 24 deletions
diff --git a/os-posix.c b/os-posix.c
index 8f39447..4bd80e4 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -344,30 +344,6 @@ void os_set_line_buffering(void)
setvbuf(stdout, NULL, _IOLBF, 0);
}
-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) {
- close(fd);
- return -1;
- }
- len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid());
- if (write(fd, buffer, len) != len) {
- close(fd);
- return -1;
- }
-
- /* keep pidfile open & locked forever */
- return 0;
-}
-
bool is_daemonized(void)
{
return daemonize;