From 27ffec083140467a03f463a446c6d19dc5e437ab Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 30 Sep 2020 16:06:53 +0200 Subject: [PATCH 1/3] log: update comment The logic was changed in bc694c06e60505efeb09e5278a7b22cdfa23975e, let's update the comment accordingly. --- src/basic/log.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/log.h b/src/basic/log.h index c65419b699..ce8bb42ea1 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -30,7 +30,7 @@ typedef enum LogTarget{ LOG_TARGET_JOURNAL_OR_KMSG, LOG_TARGET_SYSLOG, LOG_TARGET_SYSLOG_OR_KMSG, - LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */ + LOG_TARGET_AUTO, /* console if stderr is not journal, JOURNAL_OR_KMSG otherwise */ LOG_TARGET_NULL, _LOG_TARGET_MAX, _LOG_TARGET_INVALID = -1 From ef9bddb79984aa1b9d605d44b8c0890e8289bef1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 30 Sep 2020 16:17:12 +0200 Subject: [PATCH 2/3] log: normalize log target condition check THis doesn't change the condition's logic at all, but is an attempt to make things a bit more readable: instead of checking log_target != LOG_TARGET_AUTO let's actually list the targets where we want to consider journal/syslog/kmsg, to make things a bit less confusing. After all the message here is not to avoid them if LOG_TARGET_AUTO is set, but to definitely do them in the other cases. --- src/basic/log.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index 7c68258ea8..1d796c5180 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -267,28 +267,39 @@ int log_open(void) { return 0; } - if (log_target != LOG_TARGET_AUTO || getpid_cached() == 1 || stderr_is_journal()) { + if (getpid_cached() == 1 || + stderr_is_journal() || + IN_SET(log_target, + LOG_TARGET_KMSG, + LOG_TARGET_JOURNAL, + LOG_TARGET_JOURNAL_OR_KMSG, + LOG_TARGET_SYSLOG, + LOG_TARGET_SYSLOG_OR_KMSG)) { - if (!prohibit_ipc && - IN_SET(log_target, LOG_TARGET_AUTO, - LOG_TARGET_JOURNAL_OR_KMSG, - LOG_TARGET_JOURNAL)) { - r = log_open_journal(); - if (r >= 0) { - log_close_syslog(); - log_close_console(); - return r; + if (!prohibit_ipc) { + if (IN_SET(log_target, + LOG_TARGET_AUTO, + LOG_TARGET_JOURNAL_OR_KMSG, + LOG_TARGET_JOURNAL)) { + + r = log_open_journal(); + if (r >= 0) { + log_close_syslog(); + log_close_console(); + return r; + } } - } - if (!prohibit_ipc && - IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG, - LOG_TARGET_SYSLOG)) { - r = log_open_syslog(); - if (r >= 0) { - log_close_journal(); - log_close_console(); - return r; + if (IN_SET(log_target, + LOG_TARGET_SYSLOG_OR_KMSG, + LOG_TARGET_SYSLOG)) { + + r = log_open_syslog(); + if (r >= 0) { + log_close_journal(); + log_close_console(); + return r; + } } } From dee29aeb5909f4f5604012ced250488286b8d468 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 30 Sep 2020 16:19:12 +0200 Subject: [PATCH 3/3] generator: use kmsg in system-level generators, journal otherwise Fixes: #17129. --- src/shared/generator.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/shared/generator.c b/src/shared/generator.c index 04d2f86a4a..d61b367d33 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -4,6 +4,7 @@ #include #include "alloc-util.h" +#include "cgroup-util.h" #include "dropin.h" #include "escape.h" #include "fd-util.h" @@ -620,6 +621,11 @@ int generator_write_cryptsetup_service_section( } void log_setup_generator(void) { - log_set_prohibit_ipc(true); - log_setup_service(); + /* 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); + + log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); /* This effectively means: journal for per-user generators, kmsg otherwise */ + log_parse_environment(); + (void) log_open(); }