util: add qemu_write_pidfile()
cover util: add qemu_write_pidfile()
Hi,
Here are a few PID file related patches extracted from "[PATCH v4 00/29] vhost-user for input & GPU" series, with suggestions from Daniel Berrangé.
thanks
Marc-André Lureau (3):
util: add qemu_write_pidfile()
util: use fcntl() for qemu_write_pidfile() locking
RFC: delete PID file on exit
include/qemu/osdep.h | 3 +- os-posix.c | 24 -------------- os-win32.c | 25 --------------- qga/main.c | 54 ++++++-------------------------- scsi/qemu-pr-helper.c | 40 +++--------------------- util/oslib-posix.c | 73 +++++++++++++++++++++++++++++++++++++++++++ util/oslib-win32.c | 27 ++++++++++++++++ vl.c | 15 +++++++-- 8 files changed, 129 insertions(+), 132 deletions(-)
Signature · 1 line
2.19.0.rc1
PATCH 1/3 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..
--- include/qemu/osdep.h | 3 +- os-posix.c | 24 --------------- os-win32.c | 25 ---------------- qga/main.c | 54 +++++++--------------------------- scsi/qemu-pr-helper.c | 40 ++++--------------------- util/oslib-posix.c | 68 +++++++++++++++++++++++++++++++++++++++++++ util/oslib-win32.c | 27 +++++++++++++++++ vl.c | 4 +-- 8 files changed, 114 insertions(+), 131 deletions(-)
include/qemu/osdep.h +2 -1
@@ -448,7 +448,8 @@ bool qemu_has_ofd_lock(void);#define FMT_pid "%d"#endif -int qemu_create_pidfile (const char *filename); +bool qemu_write_pidfile (const char *pidfile,Error **errp); +int qemu_get_thread_id (void );#ifndef CONFIG_IOVEC
os-posix.c +0 -24
@@ -352,30 +352,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;
os-win32.c +0 -25
@@ -97,28 +97,3 @@ int os_parse_cmd_args(int index, const char *optarg) {return -1 ; } - -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),"%d\n" ,getpid ()); - ret =WriteFile (file, (LPCVOID )buffer, (DWORD )len, -NULL , &overlap); -CloseHandle (file); -if (ret ==0 ) { -return -1 ; - } -return 0 ; -}
qga/main.c +10 -44
72 lines not on this page · raw message
scsi/qemu-pr-helper.c +5 -35
@@ -117,39 +117,6 @@ QEMU_COPYRIGHT "\n" , name); } -static void write_pidfile (void ) -{ -int pidfd; -char pidstr[32 ]; - - pidfd =qemu_open (pidfile,O_CREAT |O_WRONLY ,S_IRUSR |S_IWUSR ); -if (pidfd ==-1 ) { -error_report ("Cannot open pid file, %s" ,strerror (errno)); -exit (EXIT_FAILURE ); - } - -if (lockf (pidfd,F_TLOCK ,0 )) { -error_report ("Cannot lock pid file, %s" ,strerror (errno)); - goto fail; - } -if (ftruncate (pidfd,0 )) { -error_report ("Failed to truncate pid file" ); - goto fail; - } - -snprintf (pidstr,sizeof (pidstr),"%d\n" ,getpid ()); -if (write (pidfd, pidstr,strlen (pidstr)) !=strlen (pidstr)) { -error_report ("Failed to write pid file" ); - goto fail; - } -return ; - -fail: -unlink (pidfile); -close (pidfd); -exit (EXIT_FAILURE ); -} -/* SG_IO support */ typedef struct PRHelperSGIOData {
@@ -1080,8 +1047,11 @@ int main(int argc, char **argv) } } -if (daemonize || pidfile_specified) -write_pidfile (); +if ((daemonize || pidfile_specified) && + !qemu_write_pidfile (pidfile, &local_err)) { +error_report_err (local_err); +exit (EXIT_FAILURE ); + }#ifdef CONFIG_LIBCAP if (drop_privileges () <0 ) {
util/oslib-posix.c +68 -0
74 lines not on this page · raw message
util/oslib-win32.c +27 -0
@@ -776,3 +776,30 @@ ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags, }return ret; } + +bool qemu_write_pidfile (const char *filename,Error **errp) +{ +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 ) { +error_setg (errp,"Failed to create PID file" ); +return false; + } + len =snprintf (buffer,sizeof (buffer), FMT_pid"\n" ,getpid ()); + ret =WriteFile (file, (LPCVOID )buffer, (DWORD )len, +NULL , &overlap); +CloseHandle (file); +if (ret ==0 ) { +error_setg (errp,"Failed to write PID file" ); +return false; + } +return true; +}
vl.c +2 -2
@@ -3996,8 +3996,8 @@ int main(int argc, char **argv, char **envp)os_daemonize ();rcu_disable_atfork (); -if (pid_file &&qemu_create_pidfile (pid_file) !=0 ) { -error_report ("could not acquire pid file: %s" ,strerror (errno)); +if (pid_file && !qemu_write_pidfile (pid_file, &err)) { +error_reportf_err (err,"cannot create PID file: " );exit (1 ); }
Signature · 1 line
2.19.0.rc1
Quoted 48 lines · Marc-André Lureau
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 <[email protected]>
Quoted 10 lines
--- include/qemu/osdep.h | 3 +- os-posix.c | 24 --------------- os-win32.c | 25 ---------------- qga/main.c | 54 +++++++--------------------------- scsi/qemu-pr-helper.c | 40 ++++--------------------- util/oslib-posix.c | 68 +++++++++++++++++++++++++++++++++++++++++++ util/oslib-win32.c | 27 +++++++++++++++++ vl.c | 4 +-- 8 files changed, 114 insertions(+), 131 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 2/3 util: use fcntl() for qemu_write_pidfile() locking
In reply to Marc-André LureauDaniel Berrangé suggested to use fcntl() locks rather than lockf().
'man lockf':
On Linux, lockf() is just an interface on top of fcntl(2) locking. Many other systems implement lockf() in this way, but note that POSIX.1 leaves the relationship between lockf() and fcntl(2) locks unspecified. A portable application should probably avoid mixing calls to these interfaces.
IOW, if its just a shim around fcntl() on many systems, it is clearer if we just use fcntl() directly, as we then know how fcntl() locks will behave if they're on a network filesystem like NFS.
--- util/oslib-posix.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
util/oslib-posix.c +6 -1
@@ -95,6 +95,11 @@ bool qemu_write_pidfile(const char *path, Error **errp)while (1 ) {struct stat a, b; +struct flock lock = { + .l_type =F_WRLCK , + .l_whence =SEEK_SET , + .l_len =0 , + }; fd =qemu_open (path,O_CREAT |O_WRONLY ,S_IRUSR |S_IWUSR );if (fd ==-1 ) {
@@ -107,7 +112,7 @@ bool qemu_write_pidfile(const char *path, Error **errp) goto fail_close; } -if (lockf (fd,F_TLOCK ,0 ) <0 ) { +if (fcntl (fd,F_SETLK , &lock)) {error_setg_errno (errp, errno,"Cannot lock pid file" ); goto fail_close; }
Signature · 1 line
2.19.0.rc1
PATCH 3/3 RFC: delete PID file on exit
In reply to Marc-André LureauRegister an exit handler to remove the PID file. By the time atexit() is called, qemu_write_pidfile() guarantees QEMU owns the PID file, thus we could safely remove it when exiting.
--- vl.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
vl.c +10 -1
@@ -2603,6 +2603,15 @@ static void qemu_run_exit_notifiers(void)notifier_list_notify (&exit_notifiers,NULL ); } +static const char *pid_file; + +static void qemu_unlink_pidfile (void ) +{ +if (pid_file) { +unlink (pid_file); + } +} +bool machine_init_done;void qemu_add_machine_init_done_notifier (Notifier *notify)
@@ -2927,7 +2936,6 @@ int main(int argc, char **argv, char **envp)const char *vga_model =NULL ;const char *qtest_chrdev =NULL ;const char *qtest_log =NULL ; -const char *pid_file =NULL ;const char *incoming =NULL ;bool userconfig = true;bool nographic = false;
@@ -4000,6 +4008,7 @@ int main(int argc, char **argv, char **envp)error_reportf_err (err,"cannot create PID file: " );exit (1 ); } +atexit (qemu_unlink_pidfile);if (qemu_init_main_loop (&main_loop_err)) {error_report_err (main_loop_err);
Signature · 1 line
2.19.0.rc1
Re: [Qemu-devel] [PATCH 3/3] RFC: delete PID file on exit
Am 31.08.2018 um 16:53 schrieb Marc-André Lureau:
[...]
Quoted 21 lines
vl.c +9
-1+static const char *pid_file; + +static void qemu_unlink_pidfile (void ) +{ +if (pid_file) { +unlink (pid_file); + } +} +bool machine_init_done;void qemu_add_machine_init_done_notifier (Notifier *notify)@@ -2927,7 +2936,6 @@ int main(int argc, char **argv, char **envp)const char *vga_model =NULL ;const char *qtest_chrdev =NULL ;const char *qtest_log =NULL ; -const char *pid_file =NULL ;const char *incoming =NULL ;bool userconfig = true;bool nographic = false;
vl.c int main(int argc, char **argv, char **envp) · line 4011
Quoted 4 lines
vl.c +1
-0@@ -4000,6 +4008,7 @@ int main(int argc, char **argv, char **envp)error_reportf_err (err,"cannot create PID file: " );exit (1 ); } +atexit (qemu_unlink_pidfile);
Could qemu_unlink_pidfile be combined with qemu_run_exit_notifiers to avoid an additional call of atexit from the same function (main)?
Even if there is support for 32 or more atexit calls, it might be good to be a little bit more restrictive.
Regards
Stefan
Re: [Qemu-devel] [PATCH 3/3] RFC: delete PID file on exit
Hi
Quoted 32 lines · Stefan Weil
Am 31.08.2018 um 16:53 schrieb Marc-André Lureau:
[...]Quoted 21 lines
vl.c +9
-1+static const char *pid_file; + +static void qemu_unlink_pidfile (void ) +{ +if (pid_file) { +unlink (pid_file); + } +} +bool machine_init_done;void qemu_add_machine_init_done_notifier (Notifier *notify)@@ -2927,7 +2936,6 @@ int main(int argc, char **argv, char **envp)const char *vga_model =NULL ;const char *qtest_chrdev =NULL ;const char *qtest_log =NULL ; -const char *pid_file =NULL ;const char *incoming =NULL ;bool userconfig = true;bool nographic = false;vl.c int main(int argc, char **argv, char **envp) · line 4011
Quoted 4 lines
vl.c +1
-0@@ -4000,6 +4008,7 @@ int main(int argc, char **argv, char **envp)error_reportf_err (err,"cannot create PID file: " );exit (1 ); } +atexit (qemu_unlink_pidfile);Could
qemu_unlink_pidfilebe combined withqemu_run_exit_notifiersto avoid an additional call of atexit from the same function (main)?
Quoted 2 lines
Even if there is support for 32 or more atexit calls, it might be good to be a little bit more restrictive.
Sure, that would require an additional Notifier, but that's not much anyway. I'll update the patch.
Paolo Bonzini
AppliedQuoted 13 lines · Marc-André Lureau
Hi,
Here are a few PID file related patches extracted from "[PATCH v4 00/29] vhost-user for input & GPU" series, with suggestions from Daniel Berrangé.
thanks
Marc-André Lureau (3):
util: addqemu_write_pidfile()
util: usefcntl()forqemu_write_pidfile()locking
RFC: delete PID file on exit
Quoted 10 lines
include/qemu/osdep.h | 3 +- os-posix.c | 24 -------------- os-win32.c | 25 --------------- qga/main.c | 54 ++++++-------------------------- scsi/qemu-pr-helper.c | 40 +++--------------------- util/oslib-posix.c | 73 +++++++++++++++++++++++++++++++++++++++++++ util/oslib-win32.c | 27 ++++++++++++++++ vl.c | 15 +++++++-- 8 files changed, 129 insertions(+), 132 deletions(-)
Queued 1-2, thanks.
Paolo