From 56a5f4969b96529c82ec8cc08db4fa8e9c61e7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 11 Jan 2022 13:23:27 +0100 Subject: [PATCH 1/2] nss: drop dummy setup_logging() helpers log_parse_environment() stopped being a macro in 9fdee66f2d9. As reported by @bauen1 in https://github.com/systemd/systemd/issues/22020, the comment was out of date. --- src/nss-mymachines/nss-mymachines.c | 7 +------ src/nss-systemd/nss-systemd.c | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/nss-mymachines/nss-mymachines.c b/src/nss-mymachines/nss-mymachines.c index 44715bb3e5..781fd48d72 100644 --- a/src/nss-mymachines/nss-mymachines.c +++ b/src/nss-mymachines/nss-mymachines.c @@ -22,14 +22,9 @@ #include "signal-util.h" #include "string-util.h" -static void setup_logging(void) { - /* We need a dummy function because log_parse_environment is a macro. */ - log_parse_environment(); -} - static void setup_logging_once(void) { static pthread_once_t once = PTHREAD_ONCE_INIT; - assert_se(pthread_once(&once, setup_logging) == 0); + assert_se(pthread_once(&once, log_parse_environment) == 0); } #define NSS_ENTRYPOINT_BEGIN \ diff --git a/src/nss-systemd/nss-systemd.c b/src/nss-systemd/nss-systemd.c index 36486b96e3..c6c00c40e6 100644 --- a/src/nss-systemd/nss-systemd.c +++ b/src/nss-systemd/nss-systemd.c @@ -116,14 +116,9 @@ static GetentData getsgent_data = { .mutex = PTHREAD_MUTEX_INITIALIZER, }; -static void setup_logging(void) { - /* We need a dummy function because log_parse_environment is a macro. */ - log_parse_environment(); -} - static void setup_logging_once(void) { static pthread_once_t once = PTHREAD_ONCE_INIT; - assert_se(pthread_once(&once, setup_logging) == 0); + assert_se(pthread_once(&once, log_parse_environment) == 0); } #define NSS_ENTRYPOINT_BEGIN \ From a7d15a24659770b0fa9f4cd26fc7bbb17765cbb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 11 Jan 2022 13:36:39 +0100 Subject: [PATCH 2/2] nss: only read logging config from environment variables log_parse_environment() uses should_parse_proc_cmdline() to determine whether it should parse settings from the kernel command line. But the checks that should_parse_proc_cmdline() apply to the whole process, and we could get a positive answer also when log_parse_environment() was called from one of the nss modules. In case of nss-modules, we don't want to look at the kernel command line. log_parse_environment_variables() that only looks at the environment variables is split out and used in the nss modules. Fixes #22020. --- src/basic/log.c | 16 ++++++++++------ src/basic/log.h | 1 + src/nss-mymachines/nss-mymachines.c | 2 +- src/nss-resolve/nss-resolve.c | 2 +- src/nss-systemd/nss-systemd.c | 2 +- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index 7bc2f28007..12071e2ebd 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -1189,14 +1189,9 @@ static bool should_parse_proc_cmdline(void) { return getpid_cached() == p; } -void log_parse_environment(void) { +void log_parse_environment_variables(void) { const char *e; - /* Do not call from library code. */ - - if (should_parse_proc_cmdline()) - (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); - e = getenv("SYSTEMD_LOG_TARGET"); if (e && log_set_target_from_string(e) < 0) log_warning("Failed to parse log target '%s'. Ignoring.", e); @@ -1222,6 +1217,15 @@ void log_parse_environment(void) { log_warning("Failed to parse log tid '%s'. Ignoring.", e); } +void log_parse_environment(void) { + /* Do not call from library code. */ + + if (should_parse_proc_cmdline()) + (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); + + log_parse_environment_variables(); +} + LogTarget log_get_target(void) { return log_target; } diff --git a/src/basic/log.h b/src/basic/log.h index 7218b4bf71..1e2bec1646 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -82,6 +82,7 @@ int log_open(void); void log_close(void); void log_forget_fds(void); +void log_parse_environment_variables(void); void log_parse_environment(void); int log_dispatch_internal( diff --git a/src/nss-mymachines/nss-mymachines.c b/src/nss-mymachines/nss-mymachines.c index 781fd48d72..c64e79bdff 100644 --- a/src/nss-mymachines/nss-mymachines.c +++ b/src/nss-mymachines/nss-mymachines.c @@ -24,7 +24,7 @@ static void setup_logging_once(void) { static pthread_once_t once = PTHREAD_ONCE_INIT; - assert_se(pthread_once(&once, log_parse_environment) == 0); + assert_se(pthread_once(&once, log_parse_environment_variables) == 0); } #define NSS_ENTRYPOINT_BEGIN \ diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index 6b0c762d03..e857d42db6 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -22,7 +22,7 @@ static JsonDispatchFlags json_dispatch_flags = 0; static void setup_logging(void) { - log_parse_environment(); + log_parse_environment_variables(); if (DEBUG_LOGGING) json_dispatch_flags = JSON_LOG; diff --git a/src/nss-systemd/nss-systemd.c b/src/nss-systemd/nss-systemd.c index c6c00c40e6..e87f1d31b3 100644 --- a/src/nss-systemd/nss-systemd.c +++ b/src/nss-systemd/nss-systemd.c @@ -118,7 +118,7 @@ static GetentData getsgent_data = { static void setup_logging_once(void) { static pthread_once_t once = PTHREAD_ONCE_INIT; - assert_se(pthread_once(&once, log_parse_environment) == 0); + assert_se(pthread_once(&once, log_parse_environment_variables) == 0); } #define NSS_ENTRYPOINT_BEGIN \