]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
manager: skip reopening of console and signal reset when running as normal program 42099/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Fri, 15 May 2026 08:18:28 +0000 (10:18 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Fri, 15 May 2026 08:57:04 +0000 (10:57 +0200)
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().

src/core/main.c

index ce72d4431f90623b444db8985323a32017df308d..54f510614cf63180fe2bc58ba013cd0e62da752c 100644 (file)
@@ -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);