bd1529
From b14c82dd9f9fcc42810614cf02efe8651897d36f Mon Sep 17 00:00:00 2001
bd1529
From: Daan De Meyer <daan.j.demeyer@gmail.com>
bd1529
Date: Wed, 10 Jun 2020 20:19:41 +0200
bd1529
Subject: [PATCH] log: Prefer logging to CLI unless JOURNAL_STREAM is set
bd1529
bd1529
(cherry picked from commit bc694c06e60505efeb09e5278a7b22cdfa23975e)
bd1529
bd1529
Resolves: #1865840
bd1529
---
bd1529
 src/basic/log.c               | 32 +++++++++++++++++++++++++++++---
bd1529
 test/TEST-21-SYSUSERS/test.sh |  3 +--
bd1529
 2 files changed, 30 insertions(+), 5 deletions(-)
bd1529
bd1529
diff --git a/src/basic/log.c b/src/basic/log.c
bd1529
index 48c094b548..9387e56a57 100644
bd1529
--- a/src/basic/log.c
bd1529
+++ b/src/basic/log.c
bd1529
@@ -10,6 +10,7 @@
bd1529
 #include <string.h>
bd1529
 #include <sys/signalfd.h>
bd1529
 #include <sys/socket.h>
bd1529
+#include <sys/stat.h>
bd1529
 #include <sys/time.h>
bd1529
 #include <sys/uio.h>
bd1529
 #include <sys/un.h>
bd1529
@@ -19,6 +20,7 @@
bd1529
 #include "sd-messages.h"
bd1529
 
bd1529
 #include "alloc-util.h"
bd1529
+#include "extract-word.h"
bd1529
 #include "fd-util.h"
bd1529
 #include "format-util.h"
bd1529
 #include "io-util.h"
bd1529
@@ -220,6 +222,32 @@ fail:
bd1529
         return r;
bd1529
 }
bd1529
 
bd1529
+static bool stderr_is_journal(void) {
bd1529
+        _cleanup_free_ char *w = NULL;
bd1529
+        const char *e;
bd1529
+        uint64_t dev, ino;
bd1529
+        struct stat st;
bd1529
+
bd1529
+        e = getenv("JOURNAL_STREAM");
bd1529
+        if (!e)
bd1529
+                return false;
bd1529
+
bd1529
+        if (extract_first_word(&e, &w, ":", EXTRACT_DONT_COALESCE_SEPARATORS) <= 0)
bd1529
+                return false;
bd1529
+        if (!e)
bd1529
+                return false;
bd1529
+
bd1529
+        if (safe_atou64(w, &dev) < 0)
bd1529
+                return false;
bd1529
+        if (safe_atou64(e, &ino) < 0)
bd1529
+                return false;
bd1529
+
bd1529
+        if (fstat(STDERR_FILENO, &st) < 0)
bd1529
+                return false;
bd1529
+
bd1529
+        return st.st_dev == dev && st.st_ino == ino;
bd1529
+}
bd1529
+
bd1529
 int log_open(void) {
bd1529
         int r;
bd1529
 
bd1529
@@ -239,9 +267,7 @@ int log_open(void) {
bd1529
                 return 0;
bd1529
         }
bd1529
 
bd1529
-        if (log_target != LOG_TARGET_AUTO ||
bd1529
-            getpid_cached() == 1 ||
bd1529
-            isatty(STDERR_FILENO) <= 0) {
bd1529
+        if (log_target != LOG_TARGET_AUTO || getpid_cached() == 1 || stderr_is_journal()) {
bd1529
 
bd1529
                 if (!prohibit_ipc &&
bd1529
                     IN_SET(log_target, LOG_TARGET_AUTO,
bd1529
diff --git a/test/TEST-21-SYSUSERS/test.sh b/test/TEST-21-SYSUSERS/test.sh
bd1529
index b1049e720d..3460d71f22 100755
bd1529
--- a/test/TEST-21-SYSUSERS/test.sh
bd1529
+++ b/test/TEST-21-SYSUSERS/test.sh
bd1529
@@ -108,8 +108,7 @@ test_run() {
bd1529
                 echo "*** Running test $f"
bd1529
                 prepare_testdir ${f%.input}
bd1529
                 cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
bd1529
-                systemd-sysusers --root=$TESTDIR 2> /dev/null
bd1529
-                journalctl -t systemd-sysusers -o cat | tail -n1 > $TESTDIR/tmp/err
bd1529
+                systemd-sysusers --root=$TESTDIR 2>&1 | tail -n1 > $TESTDIR/tmp/err
bd1529
                 if ! diff -u $TESTDIR/tmp/err  ${f%.*}.expected-err; then
bd1529
                         echo "**** Unexpected error output for $f"
bd1529
                         cat $TESTDIR/tmp/err