From: Zbigniew Jędrzejewski-Szmek Date: Fri, 15 May 2026 08:18:28 +0000 (+0200) Subject: manager: skip reopening of console and signal reset when running as normal program X-Git-Tag: v261-rc1~157^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09f107c811a591efa90bd2cfb4982f82c955a349;p=thirdparty%2Fsystemd.git manager: skip reopening of console and signal reset when running as normal program We want to reopen the console used for logging when running as PID1, but also when running a user manager (c.f. 48a601fe5de8aa0d89ba6dadde168769fa7ce992 and 2a646b1d624e510a79785e1268b55a9c3a441db5). But this can cause problems when the binary is invoked directly, e.g. to print --help. E.g. if we ignore SIGPIPE, we'd remain running briefly after '/usr/lib/systemd/systemd --help | head -n1'. Previusly, the getopt machinery would print to stderr unconditionally. But after the rework of option parsing, which means that we use the log_* functions to repor errors, the test that checks if we print errors to stderr started failing. So let's skip some more of the setup if !invoked_by_systemd(). --- diff --git a/src/core/main.c b/src/core/main.c index ce72d4431f9..54f510614cf 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -3273,15 +3273,19 @@ int main(int argc, char *argv[]) { /* The efivarfs is now mounted, let's lock down the system token. */ lock_down_efi_variables(); + } else { /* Running as user instance */ arg_runtime_scope = RUNTIME_SCOPE_USER; - log_set_always_reopen_console(true); - log_set_target_and_open(LOG_TARGET_AUTO); /* clear the kernel timestamp, because we are not PID 1 */ kernel_timestamp = DUAL_TIMESTAMP_NULL; + if (invoked_by_systemd()) { + log_set_always_reopen_console(true); + log_set_target_and_open(LOG_TARGET_AUTO); + } + r = mac_init(); if (r < 0) { error_message = "Failed to initialize MAC support"; @@ -3293,9 +3297,11 @@ int main(int argc, char *argv[]) { * transitioning from the initrd to the main systemd or suchlike. */ save_rlimits(&saved_rlimit_nofile, &saved_rlimit_memlock); - /* Reset all signal handlers. */ + /* Reset all signal handlers to clean up after reexec. */ (void) reset_all_signal_handlers(); - (void) ignore_signals(SIGNALS_IGNORE); + + if (getpid_cached() == 1 || invoked_by_systemd()) + (void) ignore_signals(SIGNALS_IGNORE); (void) parse_configuration(&saved_rlimit_nofile, &saved_rlimit_memlock);