Commit 08497afcb2 for qemu.org

commit 08497afcb2a737794991f17a37f0a0971fca411e
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Tue Mar 17 09:48:06 2026 +0000

    scripts/qemu-guest-agent/fsfreeze-hook: Fix syslog-fallback logic

    In the fsfreeze script we attempt to implement "log to a file if we
    can, and fall back to syslog if we cannot".  We do this with:

      [ ! -w "$LOGFILE" ] && USE_SYSLOG=1
      touch "$LOGFILE" >/dev/null 2>&1 || USE_SYSLOG=1

    This has a weird behaviour if it is run in a setup where we have
    permissions that would allow us to write to $LOGFILE but it does not
    currently exist.  On the first execution, the '-w' fails and so we
    set USE_SYSLOG=1.  But since we also do the "touch $LOGFILE" step we
    create an empty logfile.  Then on the second time the script is
    executed, we see a writeable logfile and will use it.  The effect is
    "log to syslog once, then to the logfile thereafter", which is not
    likely to be what anybody wants.

    Update the condition of the first check to only pick syslog if
    the logfile exists but is not writable. This means that:
     * if the logfile doesn't exist but we are able to create it,
       we will create it and use it
     * if the logfile already exists and we can write to it,
       we will use it
     * if the logfile already exists but we can't write to it,
       we will fall back to syslog
     * if the logfile doesn't exist and we can't create it,
       we will fall back to syslog

    Cc: qemu-stable@nongnu.org
    Fixes: 85978dfb6b1c133 ("qemu-ga: Optimize freeze-hook script logic of logging error")
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Reviewed-by: Kostiantyn Kostiuk <kkostiuk@redhat.com>
    Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
    Link: https://lore.kernel.org/qemu-devel/20260317094806.1944053-4-peter.maydell@linaro.org
    Signed-off-by: Kostiantyn Kostiuk <kkostiuk@redhat.com>

diff --git a/scripts/qemu-guest-agent/fsfreeze-hook b/scripts/qemu-guest-agent/fsfreeze-hook
index 21eb5c5145..76669f5caf 100755
--- a/scripts/qemu-guest-agent/fsfreeze-hook
+++ b/scripts/qemu-guest-agent/fsfreeze-hook
@@ -21,8 +21,8 @@ is_ignored_file() {
 }

 USE_SYSLOG=0
-# if log file is not writable, fallback to syslog
-[ ! -w "$LOGFILE" ] && USE_SYSLOG=1
+# if log file exists but is not writable, fallback to syslog
+[ -e "$LOGFILE" ] && [ ! -w "$LOGFILE" ] && USE_SYSLOG=1
 # try to update log file and fallback to syslog if it fails
 touch "$LOGFILE" >/dev/null 2>&1 || USE_SYSLOG=1