* [PATCH] util/log: Close per-thread log file on thread termination
@ 2022-10-21 10:57 Greg Kurz
2022-10-27 14:06 ` Paolo Bonzini
2022-10-27 14:36 ` Greg Kurz
0 siblings, 2 replies; 3+ messages in thread
From: Greg Kurz @ 2022-10-21 10:57 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Alex Bennée, Daniel P . Berrangé,
Greg Kurz, richard.henderson
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.
Fixes: 4e51069d6793 ("util/log: Support per-thread log files")
Cc: richard.henderson@linaro.org
Signed-off-by: Greg Kurz <groug@kaod.org>
---
util/log.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/util/log.c b/util/log.c
index d6eb0378c3a3..39866bdaf2fa 100644
--- a/util/log.c
+++ b/util/log.c
@@ -42,6 +42,7 @@ static QemuMutex global_mutex;
static char *global_filename;
static FILE *global_file;
static __thread FILE *thread_file;
+static __thread Notifier 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();
/*
--
2.37.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] util/log: Close per-thread log file on thread termination
2022-10-21 10:57 [PATCH] util/log: Close per-thread log file on thread termination Greg Kurz
@ 2022-10-27 14:06 ` Paolo Bonzini
2022-10-27 14:36 ` Greg Kurz
1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2022-10-27 14:06 UTC (permalink / raw)
To: Greg Kurz
Cc: qemu-devel, Alex Bennée, Daniel P . Berrangé,
richard.henderson
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] util/log: Close per-thread log file on thread termination
2022-10-21 10:57 [PATCH] util/log: Close per-thread log file on thread termination Greg Kurz
2022-10-27 14:06 ` Paolo Bonzini
@ 2022-10-27 14:36 ` Greg Kurz
1 sibling, 0 replies; 3+ messages in thread
From: Greg Kurz @ 2022-10-27 14:36 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Alex Bennée, Daniel P . Berrangé,
richard.henderson, qemu-stable
Cc'ing stable
On Fri, 21 Oct 2022 12:57:34 +0200
Greg Kurz <groug@kaod.org> wrote:
> 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.
>
> Fixes: 4e51069d6793 ("util/log: Support per-thread log files")
> Cc: richard.henderson@linaro.org
> Signed-off-by: Greg Kurz <groug@kaod.org>
> ---
> util/log.c | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/util/log.c b/util/log.c
> index d6eb0378c3a3..39866bdaf2fa 100644
> --- a/util/log.c
> +++ b/util/log.c
> @@ -42,6 +42,7 @@ static QemuMutex global_mutex;
> static char *global_filename;
> static FILE *global_file;
> static __thread FILE *thread_file;
> +static __thread Notifier 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();
> /*
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-10-27 14:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-21 10:57 [PATCH] util/log: Close per-thread log file on thread termination Greg Kurz
2022-10-27 14:06 ` Paolo Bonzini
2022-10-27 14:36 ` Greg Kurz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).