util/log: Close per-thread log file on thread termination
PATCH 1/1 util/log: Close per-thread log file on thread termination
When -D ${logfile} -d tid is passed, qemu_log_trylock() creates a dedicated log file for the current thread and opens it. The corresponding file descriptor is cached in a __thread variable. Nothing is done to close the corresponding file descriptor when the thread terminates though and the file descriptor is leaked.
The issue was found during code inspection and reproduced manually.
Fix that with an atexit notifier.
--- util/log.c | 9 +++++++++ 1 file changed, 9 insertions(+)
util/log.c +9 -0
@@ -42,6 +42,7 @@ static QemuMutex global_mutex;static char *global_filename;static FILE *global_file;static __threadFILE *thread_file; +static __threadNotifier qemu_log_thread_cleanup_notifier;int qemu_loglevel;static bool log_append;
@@ -77,6 +78,12 @@ static int log_thread_id(void)#endif } +static void qemu_log_thread_cleanup (Notifier *n,void *unused) +{ +fclose (thread_file); + thread_file =NULL ; +} +/* Lock/unlock output. */ FILE *qemu_log_trylock (void )
@@ -93,6 +100,8 @@ FILE *qemu_log_trylock(void)return NULL ; } thread_file = logfile; + qemu_log_thread_cleanup_notifier.notify = qemu_log_thread_cleanup; +qemu_thread_atexit_add (&qemu_log_thread_cleanup_notifier); }else {rcu_read_lock ();/*
Signature · 1 line
2.37.3
Paolo Bonzini
AppliedQueued, thanks.
Paolo
Cc'ing stable
Quoted 51 lines · On Fri, 21 Oct 2022 12:57:34 +0200 Greg Kurz <[email protected]> wrote:
When
-D ${logfile} -d tidis passed,qemu_log_trylock()creates a dedicated log file for the current thread and opens it. The corresponding file descriptor is cached in a__threadvariable. Nothing is done to close the corresponding file descriptor when the thread terminates though and the file descriptor is leaked.The issue was found during code inspection and reproduced manually.
Fix that with an atexit notifier.
Fixes 4e51069d6793 ("util/log: Support per-thread log files")Signed-off-by Greg Kurz <[email protected]>--- util/log.c | 9 +++++++++ 1 file changed, 9 insertions(+)util/log.c +9
-0@@ -42,6 +42,7 @@ static QemuMutex global_mutex;static char *global_filename;static FILE *global_file;static __threadFILE *thread_file; +static __threadNotifier qemu_log_thread_cleanup_notifier;int qemu_loglevel;static bool log_append;@@ -77,6 +78,12 @@ static int log_thread_id(void)#endif } +static void qemu_log_thread_cleanup (Notifier *n,void *unused) +{ +fclose (thread_file); + thread_file =NULL ; +} +/* Lock/unlock output. */ FILE *qemu_log_trylock (void )@@ -93,6 +100,8 @@ FILE *qemu_log_trylock(void)return NULL ; } thread_file = logfile; + qemu_log_thread_cleanup_notifier.notify = qemu_log_thread_cleanup; +qemu_thread_atexit_add (&qemu_log_thread_cleanup_notifier); }else {rcu_read_lock ();/*