Skip to content

Commit

Permalink
sulogin: fix control lost of the current terminal when default.target…
Browse files Browse the repository at this point in the history
… is rescue.target

When default.target is rescue.target, exiting from the single-user shell
results in lost of the control of the current terminal. This is because the
operation performed to continue to boot is systemctl default but default.target
is now rescue.target and it is already active. Hence, no new process that
controls the current terminal is created. Users need to make hardware reset to
recover the situation.

This sounds like a bit corner case issue and some might feel configuring
default.target as rescue.target is odd because there are several other ways to
transition to rescue.mode without configuring default.target to rescue.target
such as systemctl rescue or systemd.unit=rescue.target something like
that. However, users unfamiliar with systemd operations tend to come up with
systemctl set-default rescue.target.

To fix this issue, let's transition to default.target only when default.target
is inactive. Otherwise, invoke the single-user shell again to keep control of
the current terminal for users.

This new logic depends on whether D-Bus working well. Exiting without any check
of result of systemctl default could lead to again the control lost of the
current terminal. Hence, add checking results of each D-Bus operations
including systemctl default and invoke the single-user shell if they fail.

(cherry picked from commit 937ca8330d11e406b8ef343bead6f4f6244e39c7)

Resolves: #2169932
  • Loading branch information
d-hatayama authored and systemd-rhel-bot committed Jun 14, 2023
1 parent ab5a221 commit 30cbb55
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions src/sulogin-shell/sulogin-shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "process-util.h"
#include "sd-bus.h"
#include "signal-util.h"
#include "special.h"
#include "unit-def.h"

static int reload_manager(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
Expand Down Expand Up @@ -43,6 +45,28 @@ static int reload_manager(sd_bus *bus) {
return 0;
}

static int default_target_is_inactive(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_free_ char *path = NULL, *state = NULL;
int r;

path = unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET);
if (!path)
return log_oom();

r = sd_bus_get_property_string(bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"ActiveState",
&error,
&state);
if (r < 0)
return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r));

return streq_ptr(state, "inactive");
}

static int start_default_target(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
Expand Down Expand Up @@ -95,7 +119,6 @@ int main(int argc, char *argv[]) {
NULL, /* --force */
NULL
};
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
Expand All @@ -108,17 +131,34 @@ int main(int argc, char *argv[]) {
/* allows passwordless logins if root account is locked. */
sulogin_cmdline[1] = "--force";

(void) fork_wait(sulogin_cmdline);
for (;;) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;

(void) fork_wait(sulogin_cmdline);

r = bus_connect_system_systemd(&bus);
if (r < 0) {
log_warning_errno(r, "Failed to get D-Bus connection: %m");
goto fallback;
}

r = bus_connect_system_systemd(&bus);
if (r < 0) {
log_warning_errno(r, "Failed to get D-Bus connection: %m");
r = 0;
} else {
(void) reload_manager(bus);
if (reload_manager(bus) < 0)
goto fallback;

r = start_default_target(bus);
r = default_target_is_inactive(bus);
if (r < 0)
goto fallback;
if (!r) {
log_warning(SPECIAL_DEFAULT_TARGET" is not inactive. Please review the "SPECIAL_DEFAULT_TARGET" setting.\n");
goto fallback;
}

if (start_default_target(bus) >= 0)
break;

fallback:
log_warning("Fallback to the single-user shell.\n");
}

return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
return 0;
}

0 comments on commit 30cbb55

Please sign in to comment.