diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5349fe..d14239b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -113,6 +113,8 @@ jobs: qemu/rootfs/sbin/init qemu/rootfs/init tests/mk-bootimg/*.sh \ tests/fetch-vendor/*.sh tests/fetch-buildroot-tarball/*.sh \ tests/unpack-buildroot/*.sh + - name: qemu init shellcheck regression test + run: bash tests/qemu-init/run-init-shellcheck-tests.sh - name: cache apt archives (python3-pil) # Same cost class as the busybox binary cached below: a system package # plus its libjpeg/libpng transitive deps, downloaded fresh on every diff --git a/qemu/rootfs/init b/qemu/rootfs/init index 0204b7a..2a701cf 100755 --- a/qemu/rootfs/init +++ b/qemu/rootfs/init @@ -50,7 +50,8 @@ fi # warden.run=: run a script that mkinitramfs.sh staged from EXTRA_DIR, then # power off. Used by the M0 limit tests (tools/m0/limits-test.sh in flare-edge). -for w in $(cat /proc/cmdline); do +read -r CMDLINE < /proc/cmdline +for w in $CMDLINE; do case "$w" in warden.run=*) RUN_SCRIPT="${w#warden.run=}" ;; esac done if [ -n "${RUN_SCRIPT:-}" ]; then diff --git a/tests/qemu-init/run-init-shellcheck-tests.sh b/tests/qemu-init/run-init-shellcheck-tests.sh new file mode 100755 index 0000000..cdc9fb7 --- /dev/null +++ b/tests/qemu-init/run-init-shellcheck-tests.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# Offline test: the QEMU initramfs scripts pass the same shellcheck the qemu-tools CI job runs +# (issue #18: `for w in $(cat /proc/cmdline)` failed it with SC2013), and the check really fires. +# Skips with a notice when shellcheck is not installed (CI installs it). +set -uo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO="$(cd "$HERE/../.." && pwd)" +TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT +FAIL=0 +ok() { printf '[PASS] %s\n' "$1"; } +bad() { printf '[FAIL] %s\n' "$1"; FAIL=1; } + +if ! command -v shellcheck >/dev/null 2>&1; then + echo "[SKIP] shellcheck is not installed"; exit 0 +fi + +cd "$REPO" || exit 1 +if shellcheck -x qemu/rootfs/init qemu/rootfs/etc/rc qemu/rootfs/sbin/init > "$TMP/out" 2>&1; then + ok "qemu/rootfs scripts pass shellcheck" +else + bad "qemu/rootfs scripts fail shellcheck"; head -20 "$TMP/out" +fi + +if grep -q 'read -r CMDLINE < /proc/cmdline' qemu/rootfs/init; then + ok "init reads /proc/cmdline into a variable" +else + bad "init no longer reads /proc/cmdline into a variable" +fi + +# the old form must be red +# shellcheck disable=SC2016 # the text is the literal old source line +old_loop='for w in $(cat /proc/cmdline); do' +sed -e '/^read -r CMDLINE < \/proc\/cmdline$/d' -e "s|^for w in \\\$CMDLINE; do\$|$old_loop|" qemu/rootfs/init > "$TMP/init-old" +grep -qF "$old_loop" "$TMP/init-old" || bad "the doctored copy was not built" +out="$(shellcheck -x "$TMP/init-old" 2>&1 || true)" +if printf '%s' "$out" | grep -q 'SC2013'; then + ok "doctored: the cat-in-a-for-loop form is reported as SC2013" +else + bad "doctored: the old form was not caught" +fi + +if [ "$FAIL" -eq 0 ]; then echo "ALL QEMU-INIT SHELLCHECK TESTS PASSED"; else echo "QEMU-INIT SHELLCHECK TESTS FAILED"; fi +exit $FAIL