dryang / rpms / systemd

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