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