util: fix fd leak in qemu_write_pidfile()
PATCH v2 1/1 util: fix fd leak in qemu_write_pidfile()
if execute qemu_open success, have no branch to free the fd, so unlink it inadvance, let it free by process exit.
--- util/oslib-posix.c | 1 + 1 file changed, 1 insertion(+)
util/oslib-posix.c +1 -0
@@ -131,6 +131,7 @@ bool qemu_write_pidfile(const char *path, Error **errp)error_setg_errno (errp, errno,"Cannot open pid file" );return false; } +unlink (path);if (fstat (fd, &b) <0 ) {error_setg_errno (errp, errno,"Cannot stat file" );
Signature · 1 line
2.23.0
Quoted 14 lines · Jie Wang
if execute
qemu_opensuccess, have no branch to free the fd, so unlink it inadvance, let it free by process exit.Signed-off-by Jie Wang <[email protected]>--- util/oslib-posix.c | 1 + 1 file changed, 1 insertion(+)util/oslib-posix.c +0
-0
util/oslib-posix.c bool qemu_write_pidfile(const char *path, Error **errp) · line 137
Quoted 7 lines
util/oslib-posix.c +1
-0@@ -131,6 +131,7 @@ bool qemu_write_pidfile(const char *path, Error **errp)error_setg_errno (errp, errno,"Cannot open pid file" );return false; } +unlink (path);if (fstat (fd, &b) <0 ) {error_setg_errno (errp, errno,"Cannot stat file" );
This code change doesn't match the commit message -- the commit message says it's trying to free a filedescriptor, but the code change is unlinking a file.
Unlinking the file is definitely wrong, because the purpose of the pidfile is to comminucate the QEMU PID to other processes -- if we delete the file then those other processes can't find it. (The file gets deleted when QEMU exits -- see qemu_unlink_pidfile() and the code that calls it.)
thanks
-- PMM
Quoted 27 lines · Peter Maydell
Quoted 14 lines · Jie Wang
if execute
qemu_opensuccess, have no branch to free the fd, so unlink it inadvance, let it free by process exit.Signed-off-by Jie Wang <[email protected]>--- util/oslib-posix.c | 1 + 1 file changed, 1 insertion(+)util/oslib-posix.c +0
-0util/oslib-posix.c bool qemu_write_pidfile(const char *path, Error **errp) · line 137
Quoted 7 lines
util/oslib-posix.c +1
-0@@ -131,6 +131,7 @@ bool qemu_write_pidfile(const char *path, Error **errp)error_setg_errno (errp, errno,"Cannot open pid file" );return false; } +unlink (path);if (fstat (fd, &b) <0 ) {error_setg_errno (errp, errno,"Cannot stat file" );This code change doesn't match the commit message -- the commit message says it's trying to free a filedescriptor, but the code change is unlinking a file.
Quoted 5 lines
Unlinking the file is definitely wrong, because the purpose of the pidfile is to comminucate the QEMU PID to other processes -- if we delete the file then those other processes can't find it. (The file gets deleted when QEMU exits -- see
qemu_unlink_pidfile()and the code that calls it.)
We are also *intentionally* leaking the file descriptor. We have a lock held on the FD and that lock must not be released until QEMU exits. We thus leak the FD and rely on the OS to do cleanup when we exit.
We really ought to put a explicit comment in this method and people are repeatedly trying to fix this non-bug.
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 :|