diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c68e5f6..235a553 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -148,6 +148,8 @@ jobs: # on the debug channel. All offline (a stub qemu-system-arm on # PATH), so it runs on every push/PR, not only a real boot. run: bash qemu/tests/run-sh-args-test.sh + - name: rig init honours reboot/poweroff/halt (#5) + run: bash qemu/tests/init-power-signals-test.sh - name: mkimage.sh SEED_DIR regression test # Only ui-drive.sh --seed (a real VM boot) exercises this hook # otherwise; this builds the same unprivileged mkfs.ext4 image and diff --git a/qemu/rootfs/etc/warden-lib.sh b/qemu/rootfs/etc/warden-lib.sh index 864ec19..cb66226 100644 --- a/qemu/rootfs/etc/warden-lib.sh +++ b/qemu/rootfs/etc/warden-lib.sh @@ -41,3 +41,24 @@ warden_slot() { esac echo "$slot" } + +# PID 1 must answer busybox's reboot/poweroff/halt itself: they only signal +# PID 1 (TERM, USR2, USR1), and the kernel drops any signal PID 1 has no +# handler for. A panel's busybox init handles them; without this the rig +# ignored a plain `reboot` (bfe-core1106-sdk#5). The -f forms skip init, and +# the kernel still prints its orderly "reboot: Restarting system" line. +# WARDEN_REBOOT_CMD / WARDEN_POWEROFF_CMD exist for the host test only. +warden_trap_power_signals() { + trap 'echo "init: reboot requested"; sync; ${WARDEN_REBOOT_CMD:-reboot -f}' TERM + trap 'echo "init: poweroff requested"; sync; ${WARDEN_POWEROFF_CMD:-poweroff -f}' USR2 + trap 'echo "init: halt requested"; sync; ${WARDEN_POWEROFF_CMD:-poweroff -f}' USR1 +} + +# Idle forever in a way a trapped signal interrupts at once: a foreground +# `sleep` would hold the trap until it returned, up to an hour later. +warden_idle_forever() { + while :; do + sleep 3600 & + wait $! + done +} diff --git a/qemu/rootfs/sbin/init b/qemu/rootfs/sbin/init index 1df3557..b639aec 100755 --- a/qemu/rootfs/sbin/init +++ b/qemu/rootfs/sbin/init @@ -239,5 +239,7 @@ if grep -qw warden.shell /proc/cmdline; then poweroff -f fi -# Hold: daemons run, console idles, scenarios drive the VM from outside. -while :; do sleep 3600; done +# Hold: daemons run, console idles, scenarios drive the VM from outside. As +# PID 1 this shell must honour reboot/poweroff/halt itself (#5). +warden_trap_power_signals +warden_idle_forever diff --git a/qemu/tests/init-power-signals-test.sh b/qemu/tests/init-power-signals-test.sh new file mode 100755 index 0000000..b11f05e --- /dev/null +++ b/qemu/tests/init-power-signals-test.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# Regression test for bfe-core1106-sdk#5: the rig's PID 1 script ignored +# 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. +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 +# script's output pipe open for an hour when a case fails. +set -m + +HERE="$(cd "$(dirname "$0")" && pwd)" +LIB="$HERE/../rootfs/etc/warden-lib.sh" +SH="${WARDEN_TEST_SH:-sh}" +WORK="$(mktemp -d)" || exit 1 +trap 'rm -rf "$WORK"' EXIT +FAIL=0 + +run_case() { # run_case