Skip to contentSkip to conversation

log: do not log if QEMU is daemonized but without -D

PATCH In review Paolo Bonzini 4 participants 4 messages Mar 1, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 log: do not log if QEMU is daemonized but without -D

Commit 96c33a4 ("log: Redirect stderr to logfile if deamonized", 2016-02-22) wanted to move stderr of a daemonized QEMU to the file specified with -D.

However, if -D was not passed, the patch had the side effect of not redirecting stderr to /dev/null. This happened because qemu_logfile was set to stderr rather than the expected value of NULL. The fix is simply in the "if" condition of do_qemu_set_log; the "if" for closing the file is also changed to match.

---
 util/log.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
util/log.c +5 -2
index a7ddc7e..8b921de 100644
@@ -56,7 +56,8 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
 #ifdef CONFIG_TRACE_LOG
     qemu_loglevel |= LOG_TRACE;
 #endif
-    if ((qemu_loglevel || is_daemonized()) && !qemu_logfile) {
+    if (!qemu_logfile &&
+        (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
         if (logfilename) {
             qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
             if (!qemu_logfile) {
@@ -72,6 +73,7 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
             }
         } else {
             /* Default to stderr if no log file specified */
+            assert(!is_daemonized());
             qemu_logfile = stderr;
         }
         /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
@@ -89,7 +91,8 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
             log_append = 1;
         }
     }
-    if (!qemu_loglevel && !is_daemonized() && qemu_logfile) {
+    if (qemu_logfile &&
+        (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
         qemu_log_close();
     }
 }
Signature · 1 line
2.5.0
Tested-by on v1 1/1
#1 lore raw
Quoted 10 lines · On Tue, 1 Mar 2016 12:50:53 +0100 Paolo Bonzini <[email protected]> wrote:

Commit 96c33a4 ("log: Redirect stderr to logfile if deamonized", 2016-02-22) wanted to move stderr of a daemonized QEMU to the file specified with -D.

However, if -D was not passed, the patch had the side effect of not redirecting stderr to /dev/null. This happened because qemu_logfile was set to stderr rather than the expected value of NULL. The fix is simply in the "if" condition of do_qemu_set_log; the "if" for closing the file is also changed to match.

Quoted 5 lines
---
 util/log.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

This fixes running qemu under libvirt for me.

Tested-by on v1 1/1
#2 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

Commit 96c33a4 ("log: Redirect stderr to logfile if deamonized", 2016-02-22) wanted to move stderr of a daemonized QEMU to the file specified with -D.

However, if -D was not passed, the patch had the side effect of not redirecting stderr to /dev/null. This happened because qemu_logfile was set to stderr rather than the expected value of NULL. The fix is simply in the "if" condition of do_qemu_set_log; the "if" for closing the file is also changed to match.

cheers,
Gerd

#3 lore raw
In reply to Paolo Bonzini

Hi all,

Quoted 10 lines · Paolo Bonzini

Commit 96c33a4 ("log: Redirect stderr to logfile if deamonized", 2016-02-22) wanted to move stderr of a daemonized QEMU to the file specified with -D.

However, if -D was not passed, the patch had the side effect of not redirecting stderr to /dev/null. This happened because qemu_logfile was set to stderr rather than the expected value of NULL. The fix is simply in the "if" condition of do_qemu_set_log; the "if" for closing the file is also changed to match.

Sorry, that was my bad.

I tested the proposed fix as well and it works. I would suggest to add some sort of comments since the whole do_qemu_set_log() is hard to parse in one pass. For example:

util/log.c +8 -0
index 8b921de..1d73be0 100644
@@ -56,6 +56,10 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
 #ifdef CONFIG_TRACE_LOG
     qemu_loglevel |= LOG_TRACE;
 #endif
+    /* Setup the logfile when:
+     *  - The loglevel is set via -d option or CONFIG_TRACE_LOG, or
+     *  - Both -daemonize and -D options are given
+     */
     if (!qemu_logfile &&
         (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
         if (logfilename) {
@@ -91,6 +95,10 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
             log_append = 1;
         }
     }
+    /* Close the already setup logfile when loglevel gets reset (e.g.
+     * via the log HMP command), unless both -daemonize and -D options
+     * are given.
+     */
     if (qemu_logfile &&
         (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
         qemu_log_close();

Thanks for this quick fix and sorry again for any trouble caused in the first place.

dimara

Quoted 41 lines
---
 util/log.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
util/log.c +5 -2
index a7ddc7e..8b921de 100644
@@ -56,7 +56,8 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
 #ifdef CONFIG_TRACE_LOG
     qemu_loglevel |= LOG_TRACE;
 #endif
-    if ((qemu_loglevel || is_daemonized()) && !qemu_logfile) {
+    if (!qemu_logfile &&
+        (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
         if (logfilename) {
             qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
             if (!qemu_logfile) {
@@ -72,6 +73,7 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
             }
         } else {
             /* Default to stderr if no log file specified */
+            assert(!is_daemonized());
             qemu_logfile = stderr;
         }
         /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
@@ -89,7 +91,8 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
             log_append = 1;
         }
     }
-    if (!qemu_loglevel && !is_daemonized() && qemu_logfile) {
+    if (qemu_logfile &&
+        (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
         qemu_log_close();
     }
 }
Signature · 1 line
2.5.0