diff --git a/default/splash/hold b/default/splash/hold index c24867fa3b2a..ec7f5289fc85 100755 --- a/default/splash/hold +++ b/default/splash/hold @@ -99,8 +99,14 @@ wait_for_session() { mapfile -t nodes < <(splash_devices) if (( ${#nodes[@]} == 0 )); then - echo "the splash has no display open" - exit 1 + session=$(cat /run/omarchy-splash/session.pid 2>/dev/null || true) + if [[ -n $session ]] && kill -0 "$session" 2>/dev/null; then + echo "the splash is running but has no display open; nothing to hold" >&2 + else + echo "the splash is no longer running; nothing to hold" >&2 + fi + systemd-notify --ready + exit 0 fi # A compositor takes a device through logind, which opens it, then reads the diff --git a/default/splash/session b/default/splash/session index d2aeb5e38d38..b347fd7c2a11 100755 --- a/default/splash/session +++ b/default/splash/session @@ -52,7 +52,7 @@ has_connectors() { wait_native() { i=0 find_cards - while [ -z "$native" ] && [ "$i" -lt "$1" ]; do + while [ -z "$native" ] && [ ! -e "$run/stopping" ] && [ "$i" -lt "$1" ]; do sleep 0.1 i=$((i + 1)) find_cards @@ -60,12 +60,14 @@ wait_native() { } wait_native 80 +[ ! -e "$run/stopping" ] || exit 0 devices=${native:-$firmware} if [ -z "$devices" ]; then echo "omarchy-splash: no display to draw on" exit 1 fi echo "omarchy-splash: drawing on $devices" +touch "$run/starting" # amdgpu powers a GPU down once no display is connected to it, and a GPU that # is powered down misses a display plugged in later. The GPUs stay powered @@ -168,6 +170,7 @@ esac while :; do seatd-launch -- sway $debug --config /usr/lib/omarchy-splash/sway.conf status=$? + rm -f "$run/ready" if [ -e "$run/stopping" ] || [ -e "$run/dismissed" ] || [ "$devices" != "$firmware" ]; then exit "$status" fi diff --git a/etc/initcpio/hooks/omarchy-splash b/etc/initcpio/hooks/omarchy-splash index 8c51aed94d8a..cfaa2176656f 100644 --- a/etc/initcpio/hooks/omarchy-splash +++ b/etc/initcpio/hooks/omarchy-splash @@ -42,7 +42,7 @@ asks_passphrase() { # screen, a console login or an emergency shell, with the copy of `stop` the # real root has: the initramfs's own is gone by then. run_latehook() { - local run=/run/omarchy-splash session target + local run=/run/omarchy-splash session target i # A volume asks_passphrase finds asks on the console while the real root # boots, which the splash would cover. @@ -52,6 +52,22 @@ run_latehook() { return 0 fi + # switch_root deletes the initramfs and moves /dev and /sys away, so a splash + # that has not finished starting cannot finish afterwards. Wait for one that + # has already picked a display; one still waiting for a native GPU (or that + # died before coming up) is taken down instead of stalling the boot. + if [ ! -e "$run/ready" ] && [ -e "$run/starting" ]; then + i=0 + while [ ! -e "$run/ready" ] && kill -0 "$session" 2>/dev/null && [ "$i" -lt 50 ]; do + sleep 0.1 + i=$((i + 1)) + done + fi + if [ ! -e "$run/ready" ] || ! kill -0 "$session" 2>/dev/null; then + /usr/lib/omarchy-splash/stop + return 0 + fi + # The real root keeps the /dev/shm the session mounted as its own. rm -f "$run/shm" diff --git a/test/shell.d/boot-splash-hold-test.sh b/test/shell.d/boot-splash-hold-test.sh index 4caccd3e91eb..a8fb1cfb8f58 100644 --- a/test/shell.d/boot-splash-hold-test.sh +++ b/test/shell.d/boot-splash-hold-test.sh @@ -90,3 +90,47 @@ mkfifo "$test_tmp/events" out=$(holder_waits "$test_tmp/events" 1) || fail "the holder lets go at its time limit when no session comes" "still holding after 5 s" [[ $out == *"no session took"* ]] || fail "the holder lets go at its time limit when no session comes" "$out" pass "the holder lets go at its time limit when no session comes" + +# When the splash already exited before hold starts (e.g. Escape or simpledrm +# unbind in the real root), hold notifies systemd and exits 0 without creating +# the adoption marker. +fake_bin=$test_tmp/bin +hold_fixture=$test_tmp/hold +mkdir -p "$fake_bin" +sed -e "s|/run/omarchy-splash-hold/adopt|$adopt|g" \ + -e "s|/run/omarchy-splash/session.pid|$test_tmp/session.pid|g" \ + "$hold" >"$hold_fixture" +grep -qF "$adopt" "$hold_fixture" || fail "hold fixture writes its adoption marker in the test dir" +cat >"$fake_bin/pidof" <<'SH' +#!/bin/bash +exit 1 +SH +cat >"$fake_bin/systemd-notify" <"$test_tmp/notified" +SH +chmod +x "$fake_bin/pidof" "$fake_bin/systemd-notify" +rm -f "$adopt" "$test_tmp/notified" "$test_tmp/session.pid" +out=$(PATH="$fake_bin:$PATH" bash "$hold_fixture" 2>&1) || + fail "hold exits 0 when the splash is no longer running" "$out" +[[ $out == *"the splash is no longer running; nothing to hold"* ]] || + fail "hold logs when the splash is no longer running" "$out" +[[ $(cat "$test_tmp/notified" 2>/dev/null) == "--ready" ]] || + fail "hold notifies systemd --ready before exiting when nothing is held" "$(cat "$test_tmp/notified" 2>/dev/null)" +[[ ! -e $adopt ]] || + fail "hold does not create the adoption marker when nothing is held" +sleep 10 & +live_session=$! +echo "$live_session" >"$test_tmp/session.pid" +rm -f "$adopt" "$test_tmp/notified" +out=$(PATH="$fake_bin:$PATH" bash "$hold_fixture" 2>&1) || + fail "hold exits 0 when the splash is running without a display" "$out" +kill "$live_session" 2>/dev/null || true +wait "$live_session" 2>/dev/null || true +[[ $out == *"the splash is running but has no display open; nothing to hold"* ]] || + fail "hold distinguishes a running splash with no open display" "$out" +[[ $(cat "$test_tmp/notified" 2>/dev/null) == "--ready" ]] || + fail "hold notifies systemd --ready for a running splash with no open display" "$(cat "$test_tmp/notified" 2>/dev/null)" +[[ ! -e $adopt ]] || + fail "hold does not create the adoption marker for a running splash with no open display" +pass "hold notifies systemd and exits 0 when the splash has no display open" diff --git a/test/shell.d/boot-splash-session-test.sh b/test/shell.d/boot-splash-session-test.sh index 7fbe8b9376fc..cc98caf3f97e 100644 --- a/test/shell.d/boot-splash-session-test.sh +++ b/test/shell.d/boot-splash-session-test.sh @@ -82,3 +82,66 @@ signalled=$(sh -c '. "$1"; pidof() { echo 1 2 3; }; kill() { echo "$2"; }; signa [[ $signalled == "1 2 " ]] || fail "stop signals the splash's processes in the initramfs and the real root, and no unit's" "signalled: $signalled" pass "stop signals the splash's processes in the initramfs and the real root, and no unit's" + +# The late hook waits for a splash that has already picked a display to finish +# starting before switch_root deletes the initramfs under it, and takes down a +# splash that is still waiting for a GPU or died before coming up. +late_root=$test_tmp/late +late_hook=$test_tmp/late-hook.sh +sed -e "s|/run/omarchy-splash|$late_root/run/omarchy-splash|g" \ + -e "s|/run/systemd/system|$late_root/run/systemd/system|g" \ + -e "s|/sysroot/etc/crypttab|$late_root/sysroot/etc/crypttab|g" \ + -e "s|/usr/lib/omarchy-splash/stop|$late_root/stop|g" \ + -e "s|/etc/systemd/system/omarchy-splash-hold.service|$late_root/etc/omarchy-splash-hold.service|g" \ + "$ROOT/etc/initcpio/hooks/omarchy-splash" >"$late_hook" +grep -qF "$late_root/run/omarchy-splash" "$late_hook" || fail "late hook reads its state from the fixture" + +reset_late() { + rm -rf "$late_root" + mkdir -p "$late_root/run/omarchy-splash" "$late_root/sysroot/etc" "$late_root/etc" + cat >"$late_root/stop" <"$late_root/run/omarchy-splash/session.pid" +touch "$late_root/run/omarchy-splash/starting" +(sleep 0.2 && touch "$late_root/run/omarchy-splash/ready") & +ready_writer=$! +sh -c '. "$1" && run_latehook' sh "$late_hook" +wait "$ready_writer" 2>/dev/null || true +kill "$bg_pid" 2>/dev/null || true +wait "$bg_pid" 2>/dev/null || true +[[ ! -e "$late_root/stopped" && -L "$late_root/run/systemd/system/graphical.target.wants/omarchy-splash-hold.service" ]] || + fail "late hook waits for a starting splash to become ready and schedules hold" +pass "late hook waits for a starting splash to become ready and schedules hold" + +# Case 2: splash is still waiting for a native GPU (no starting marker). +reset_late +sleep 10 & +bg_pid=$! +echo "$bg_pid" >"$late_root/run/omarchy-splash/session.pid" +sh -c '. "$1" && run_latehook' sh "$late_hook" +kill "$bg_pid" 2>/dev/null || true +wait "$bg_pid" 2>/dev/null || true +[[ -e "$late_root/stopped" && ! -e "$late_root/run/systemd/system/graphical.target.wants/omarchy-splash-hold.service" ]] || + fail "late hook stops a splash still waiting for a GPU instead of stalling boot" +pass "late hook stops a splash still waiting for a GPU instead of stalling boot" + +# Case 3: splash picked a display, then died before writing ready. +reset_late +(sleep 0.15) & +bg_pid=$! +echo "$bg_pid" >"$late_root/run/omarchy-splash/session.pid" +touch "$late_root/run/omarchy-splash/starting" +sh -c '. "$1" && run_latehook' sh "$late_hook" +wait "$bg_pid" 2>/dev/null || true +[[ -e "$late_root/stopped" && ! -e "$late_root/run/systemd/system/graphical.target.wants/omarchy-splash-hold.service" ]] || + fail "late hook stops cleanly when a starting splash exits before ready" +pass "late hook stops cleanly when a starting splash exits before ready"