]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
generators: only redirect logging when invoked by systemd
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 13 Jul 2022 16:43:05 +0000 (18:43 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 15 Jul 2022 13:48:05 +0000 (15:48 +0200)
We would always print output to the kmsg or journal, but that is only needed
and useful when invoked by systemd. So let's skip redirection unless we are
invoked by systemd. Otherwise, let's log normally. This makes test invocations
easier, and also helps when the generator is invoked by mistake. If redirection
is necessary, the generator can be invoked with SYSTEMD_LOG_TARGET=… even
during tests.

src/shared/generator.c

index ca66673d84172153bfa9890be7437961fc0d1209..12170d3642b25ee5c2e591b47aaa6af26ad7e736 100644 (file)
@@ -15,6 +15,7 @@
 #include "macro.h"
 #include "mkdir-label.h"
 #include "path-util.h"
+#include "process-util.h"
 #include "special.h"
 #include "specifier.h"
 #include "string-util.h"
@@ -737,11 +738,15 @@ int generator_write_veritysetup_service_section(
 }
 
 void log_setup_generator(void) {
-        /* Disable talking to syslog/journal (i.e. the two IPC-based loggers) if we run in system context. */
-        if (cg_pid_get_owner_uid(0, NULL) == -ENXIO /* not running in a per-user slice */)
-                log_set_prohibit_ipc(true);
+        if (invoked_by_systemd()) {
+                /* Disable talking to syslog/journal (i.e. the two IPC-based loggers) if we run in system context. */
+                if (cg_pid_get_owner_uid(0, NULL) == -ENXIO /* not running in a per-user slice */)
+                        log_set_prohibit_ipc(true);
+
+                /* This effectively means: journal for per-user generators, kmsg otherwise */
+                log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
+        }
 
-        log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); /* This effectively means: journal for per-user generators, kmsg otherwise */
         log_parse_environment();
         (void) log_open();
 }