qemu: power-signal test waits for the trap install (flare-edge-next) #24

Merged
noah merged 1 commits from lf-fix-sdk22-next into flare-edge-next 2026-10-17 21:19:48 -06:00
+47 -5
View File
@@ -3,7 +3,9 @@
# busybox reboot/poweroff/halt, which only signal PID 1. Runs the real
# helpers from qemu/rootfs/etc/warden-lib.sh in a plain sh with stub
# commands, idles it the way /sbin/init does, sends each signal and requires
# the matching action within two seconds.
# the matching action within two seconds. Each signal goes out only once the
# shell has its handler installed (SigCgt), so a slow start cannot turn the
# signal's default action into a false failure (bfe-core1106-sdk#22).
set -uo pipefail
# Job control: each case's shell leads its own process group, so cleanup can
# kill the whole group. An orphaned `sleep 3600` would otherwise keep this
@@ -16,15 +18,41 @@ SH="${WARDEN_TEST_SH:-sh}"
WORK="$(mktemp -d)" || exit 1
trap 'rm -rf "$WORK"' EXIT
FAIL=0
# Hang backstop for the handler install; a quiet host takes milliseconds.
TRAP_WAIT_S="${WARDEN_TEST_TRAP_WAIT_S:-10}"
run_case() { # run_case <signal> <expected marker> <label>
local sig="$1" want="$2" label="$3" out="$WORK/$1.out"
# Succeeds once <pid> catches <signal>: its bit is set in /proc SigCgt.
catches() { # catches <pid> <signal name>
local mask num
mask="$(awk '/^SigCgt:/ { print $2 }' "/proc/$1/status" 2>/dev/null)"
num="$(kill -l "$2")" || return 1
[ -n "$mask" ] && (( (16#$mask >> (num - 1)) & 1 ))
}
# Waits up to TRAP_WAIT_S for <pid> to catch <signal>.
wait_trapped() { # wait_trapped <pid> <signal name>
local n=0 limit=$((TRAP_WAIT_S * 10))
until catches "$1" "$2"; do
[ "$n" -ge "$limit" ] && return 1
sleep 0.1; n=$((n + 1))
done
}
# <delay> holds the handler install back, as a loaded runner does.
run_case() { # run_case <signal> <expected marker> <label> [delay s]
local sig="$1" want="$2" label="$3" delay="${4:-0}" out="$WORK/$1.out"
: > "$out"
WARDEN_REBOOT_CMD="eval echo reboot >> $out; exit 0" \
WARDEN_POWEROFF_CMD="eval echo poweroff >> $out; exit 0" \
"$SH" -c ". '$LIB'; warden_trap_power_signals; warden_idle_forever" &
"$SH" -c "sleep $delay; . '$LIB'; warden_trap_power_signals; warden_idle_forever" &
local pid=$!
sleep 0.5
if ! wait_trapped "$pid" "$sig"; then
echo "[FAIL] $label (SIG$sig handler not installed within ${TRAP_WAIT_S} s)"
FAIL=1
kill -KILL -- "-$pid" 2>/dev/null
wait "$pid" 2>/dev/null
return
fi
kill -"$sig" "$pid"
local n=0
while ! grep -qx "$want" "$out" && [ "$n" -lt 20 ]; do
@@ -42,5 +70,19 @@ run_case() { # run_case <signal> <expected marker> <label>
run_case TERM reboot "SIGTERM (busybox reboot) reboots at once"
run_case USR2 poweroff "SIGUSR2 (busybox poweroff) powers off at once"
run_case USR1 poweroff "SIGUSR1 (busybox halt) powers off at once"
# Regression for bfe-core1106-sdk#22: the old fixed 0.5 s sleep sent SIGTERM
# before this shell installed its trap, killing it with nothing written.
run_case TERM reboot "SIGTERM after a slow handler install still reboots" 1.5
# The wait itself must fail, not hang or pass, when no handler ever appears.
"$SH" -c 'sleep 3600' &
pid=$!
if TRAP_WAIT_S=1 wait_trapped "$pid" TERM; then
echo "[FAIL] wait_trapped reported a handler a plain shell never installs"; FAIL=1
else
echo "[PASS] wait_trapped gives up on a shell without a handler"
fi
kill -KILL -- "-$pid" 2>/dev/null
wait "$pid" 2>/dev/null
exit "$FAIL"