[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL for-7.1 35/36] util/log: Limit RCUCloseFILE to file closing
|
From: |
Richard Henderson |
|
Subject: |
[PULL for-7.1 35/36] util/log: Limit RCUCloseFILE to file closing |
|
Date: |
Sun, 20 Mar 2022 10:11:34 -0700 |
Use FILE* for global_file. We can perform an rcu_read on that
just as easily as RCUCloseFILE*. This simplifies a couple of
places, where previously we required taking the rcu_read_lock
simply to avoid racing to dereference RCUCloseFile->fd.
Only allocate the RCUCloseFile prior to call_rcu.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
util/log.c | 67 +++++++++++++++++++++++-------------------------------
1 file changed, 28 insertions(+), 39 deletions(-)
diff --git a/util/log.c b/util/log.c
index e966d1091d..713a0744be 100644
--- a/util/log.c
+++ b/util/log.c
@@ -37,7 +37,7 @@ typedef struct RCUCloseFILE {
/* Mutex covering the other global_* variables. */
static QemuMutex global_mutex;
static char *global_filename;
-static RCUCloseFILE *global_file;
+static FILE *global_file;
int qemu_loglevel;
static int log_append = 0;
@@ -46,46 +46,37 @@ static GArray *debug_regions;
/* Returns true if qemu_log() will really write somewhere. */
bool qemu_log_enabled(void)
{
- return global_file != NULL;
+ return qatomic_read(&global_file) != NULL;
}
/* Returns true if qemu_log() will write somewhere other than stderr. */
bool qemu_log_separate(void)
{
- RCUCloseFILE *logfile;
- bool res = false;
-
- rcu_read_lock();
- logfile = qatomic_rcu_read(&global_file);
- if (logfile && logfile->fd != stderr) {
- res = true;
- }
- rcu_read_unlock();
- return res;
+ FILE *logfile = qatomic_read(&global_file);
+ return logfile && logfile != stderr;
}
/* Lock/unlock output. */
FILE *qemu_log_lock(void)
{
- RCUCloseFILE *logfile;
+ FILE *logfile;
rcu_read_lock();
logfile = qatomic_rcu_read(&global_file);
if (logfile) {
- qemu_flockfile(logfile->fd);
- return logfile->fd;
+ qemu_flockfile(logfile);
} else {
rcu_read_unlock();
- return NULL;
}
+ return logfile;
}
-void qemu_log_unlock(FILE *fd)
+void qemu_log_unlock(FILE *logfile)
{
- if (fd) {
- fflush(fd);
- qemu_funlockfile(fd);
+ if (logfile) {
+ fflush(logfile);
+ qemu_funlockfile(logfile);
rcu_read_unlock();
}
}
@@ -93,16 +84,16 @@ void qemu_log_unlock(FILE *fd)
/* Return the number of characters emitted. */
int qemu_log(const char *fmt, ...)
{
- FILE *f = qemu_log_lock();
+ FILE *logfile = qemu_log_lock();
int ret = 0;
- if (f) {
+ if (logfile) {
va_list ap;
va_start(ap, fmt);
- ret = vfprintf(f, fmt, ap);
+ ret = vfprintf(logfile, fmt, ap);
va_end(ap);
- qemu_log_unlock(f);
+ qemu_log_unlock(logfile);
/* Don't pass back error results. */
if (ret < 0) {
@@ -119,9 +110,7 @@ static void __attribute__((__constructor__)) startup(void)
static void rcu_close_file(RCUCloseFILE *r)
{
- if (r->fd != stderr) {
- fclose(r->fd);
- }
+ fclose(r->fd);
g_free(r);
}
@@ -131,7 +120,7 @@ static void qemu_set_log_internal(const char *filename,
bool changed_name,
{
bool need_to_open_file;
bool daemonized;
- RCUCloseFILE *logfile;
+ FILE *logfile;
QEMU_LOCK_GUARD(&global_mutex);
logfile = global_file;
@@ -185,37 +174,37 @@ static void qemu_set_log_internal(const char *filename,
bool changed_name,
if (logfile && (!need_to_open_file || changed_name)) {
qatomic_rcu_set(&global_file, NULL);
- call_rcu(logfile, rcu_close_file, rcu);
+ if (logfile != stderr) {
+ RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
+ r->fd = logfile;
+ call_rcu(r, rcu_close_file, rcu);
+ }
logfile = NULL;
}
if (!logfile && need_to_open_file) {
- FILE *fd;
-
if (filename) {
- fd = fopen(filename, log_append ? "a" : "w");
- if (!fd) {
+ logfile = fopen(filename, log_append ? "a" : "w");
+ if (!logfile) {
error_setg_errno(errp, errno, "Error opening logfile %s",
filename);
return;
}
/* In case we are a daemon redirect stderr to logfile */
if (daemonized) {
- dup2(fileno(fd), STDERR_FILENO);
- fclose(fd);
+ dup2(fileno(logfile), STDERR_FILENO);
+ fclose(logfile);
/* This will skip closing logfile in rcu_close_file. */
- fd = stderr;
+ logfile = stderr;
}
} else {
/* Default to stderr if no log file specified */
assert(!daemonized);
- fd = stderr;
+ logfile = stderr;
}
log_append = 1;
- logfile = g_new0(RCUCloseFILE, 1);
- logfile->fd = fd;
qatomic_rcu_set(&global_file, logfile);
}
}
--
2.25.1
- [PULL for-7.1 17/36] util/log: Drop call to setvbuf, (continued)
- [PULL for-7.1 17/36] util/log: Drop call to setvbuf, Richard Henderson, 2022/03/20
- [PULL for-7.1 21/36] include/exec/log: Do not reference QemuLogFile directly, Richard Henderson, 2022/03/20
- [PULL for-7.1 22/36] include/qemu/log: Move entire implementation out-of-line, Richard Henderson, 2022/03/20
- [PULL for-7.1 24/36] util/log: Introduce qemu_set_log_filename_flags, Richard Henderson, 2022/03/20
- [PULL for-7.1 23/36] sysemu/os-win32: Test for and use _lock_file/_unlock_file, Richard Henderson, 2022/03/20
- [PULL for-7.1 26/36] linux-user: Use qemu_set_log_filename_flags, Richard Henderson, 2022/03/20
- [PULL for-7.1 27/36] softmmu: Use qemu_set_log_filename_flags, Richard Henderson, 2022/03/20
- [PULL for-7.1 28/36] util/log: Remove qemu_log_close, Richard Henderson, 2022/03/20
- [PULL for-7.1 31/36] util/log: Rename qemu_logfile_mutex to global_mutex, Richard Henderson, 2022/03/20
- [PULL for-7.1 33/36] util/log: Combine two logfile closes, Richard Henderson, 2022/03/20
- [PULL for-7.1 35/36] util/log: Limit RCUCloseFILE to file closing,
Richard Henderson <=
- [PULL for-7.1 32/36] util/log: Hoist the eval of is_daemonized in qemu_set_log_internal, Richard Henderson, 2022/03/20
- [PULL for-7.1 36/36] util/log: Support per-thread log files, Richard Henderson, 2022/03/20
- [PULL for-7.1 34/36] util/log: Rename QemuLogFile to RCUCloseFILE, Richard Henderson, 2022/03/20
- Re: [PULL for-7.1 00/36] Logging cleanup and per-thread logfiles, Richard Henderson, 2022/03/20