]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
log: Prefer logging to CLI unless JOURNAL_STREAM is set
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 10 Jun 2020 18:19:41 +0000 (20:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 22 Jun 2020 12:23:48 +0000 (14:23 +0200)
src/basic/log.c
test/TEST-21-SYSUSERS/test.sh

index ca024d970e6bec9256d8ea687cd32693504417ef..893181b23d65aba23864e9ac77beb22ac1bb1544 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdarg.h>
 #include <stddef.h>
 #include <sys/signalfd.h>
+#include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/uio.h>
 #include <sys/un.h>
@@ -219,6 +220,32 @@ fail:
         return r;
 }
 
+static bool stderr_is_journal(void) {
+        _cleanup_free_ char *w = NULL;
+        const char *e;
+        uint64_t dev, ino;
+        struct stat st;
+
+        e = getenv("JOURNAL_STREAM");
+        if (!e)
+                return false;
+
+        if (extract_first_word(&e, &w, ":", EXTRACT_DONT_COALESCE_SEPARATORS) <= 0)
+                return false;
+        if (!e)
+                return false;
+
+        if (safe_atou64(w, &dev) < 0)
+                return false;
+        if (safe_atou64(e, &ino) < 0)
+                return false;
+
+        if (fstat(STDERR_FILENO, &st) < 0)
+                return false;
+
+        return st.st_dev == dev && st.st_ino == ino;
+}
+
 int log_open(void) {
         int r;
 
@@ -238,9 +265,7 @@ int log_open(void) {
                 return 0;
         }
 
-        if (log_target != LOG_TARGET_AUTO ||
-            getpid_cached() == 1 ||
-            isatty(STDERR_FILENO) <= 0) {
+        if (log_target != LOG_TARGET_AUTO || getpid_cached() == 1 || stderr_is_journal()) {
 
                 if (!prohibit_ipc &&
                     IN_SET(log_target, LOG_TARGET_AUTO,
index 527db85b072a9e6e0c242bf8a685d573e1fa58b1..2992beaa0b88213775bce9618860d3d7be42e132 100755 (executable)
@@ -115,9 +115,7 @@ test_run() {
         echo "*** Running test $f"
         prepare_testdir ${f%.input}
         cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
-        systemd-sysusers --root=$TESTDIR 2> /dev/null
-        journalctl --sync
-        journalctl -t systemd-sysusers -o cat | tail -n1 > $TESTDIR/tmp/err
+        systemd-sysusers --root=$TESTDIR 2>&1 | tail -n1 > $TESTDIR/tmp/err
         if ! diff -u $TESTDIR/tmp/err  ${f%.*}.expected-err; then
             echo "**** Unexpected error output for $f"
             cat $TESTDIR/tmp/err