Skip to content

fix(init): only report systemd when it is actually running - #147

Open
rvalitov wants to merge 1 commit into
SamNet-dev:mainfrom
rvalitov:fix/init-detection
Open

rvalitov wants to merge 1 commit into
SamNet-dev:mainfrom
rvalitov:fix/init-detection

Conversation

@rvalitov

Copy link
Copy Markdown
Contributor

Problem

detect_init_system() tested for the presence of the systemctl binary, not for systemd actually running as PID 1:

if command -v systemctl &>/dev/null; then
    echo "systemd"

A chroot, a container that pulled systemd in as a dependency, and a half-booted host all have the binary while every systemctl call fails. On those hosts the systemd branch of setup_autostart() was selected, then:

systemctl daemon-reload
systemctl enable mtproxymax.service 2>/dev/null
log_success "Auto-start enabled (systemd)"     # <-- unconditional

Both statuses were discarded, so the user was told "Auto-start enabled" when nothing had been enabled. The OpenRC branch directly below already verifies that the runlevel symlink actually landed, and returns 1 with a warning when it did not — the systemd branch did not.

Note systemctl --version is not a usable liveness probe either: it exits 0 with no bus at all.

Fix

  • _systemd_is_running() gates detection on liveness: /run/systemd/system must exist and systemctl is-system-running must return something other than offline. The value is compared rather than the exit status, because degraded is still a booted system and exits non-zero.
  • The systemd branch of setup_autostart() now checks the result of systemctl enable before claiming success, and returns 1 with a warning otherwise.

Checking enable's status (rather than is-enabled output) keeps this consistent with the existing systemctl stubs in tests/test_telegram_service_openrc.sh, which model exit statuses rather than stdout.

Testing

New tests/test_init_detection.sh drives a systemctl stub through three scenarios: binary present but not booted (must not be detected as systemd, must not report success), booted but enable failing (must not report success), and a working enable (must still report success). The last one guards against over-correcting into always failing.

Verified against real systemd — the integration test in the accompanying CI work still passes 14/14 on a container running systemd as PID 1, so the stricter detection does not reject a genuine systemd host.

detect_init_system() tested for the presence of the `systemctl` binary
rather than for systemd running as PID 1. A chroot, a container that
pulled systemd in as a dependency, and a half-booted host all have the
binary while every systemctl call fails — so the systemd branch of
setup_autostart() was chosen on hosts that could not run it.

That branch then called `systemctl daemon-reload` and `systemctl enable`
with no status check and printed "Auto-start enabled (systemd)"
unconditionally, so the user was told autostart was on when nothing had
been enabled. The OpenRC branch directly below already verifies and
returns 1 with a warning on failure.

Gate detection on liveness — /run/systemd/system plus a non-offline
`systemctl is-system-running`; note `systemctl --version` exits 0 with no
bus at all, so it is not usable as a probe — and check the result of
`systemctl enable` before claiming success.

Verified: the integration test against a real systemd (PID 1) still
passes 14/14, and no test changes result on Debian 12, Alpine 3.20 or
Fedora 41.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant