#!/bin/sh
# Exact reproduction sequence used for the recorded verdict (2026-09-03).
#
#   ./run.sh            - full sequence on the QEMU guest via vm.sh
#
# Guest: dfbsd-qemu/vm.sh (DragonFly 6.5-DEVELOPMENT #0, INVARIANTS kernel,
# X86_64_GENERIC, 6 vCPU, 4GB).  Takes ~2.5 minutes; leaves the guest
# panicked (reset it afterwards: vm.sh reset with-src).

set -eu
cd "$(dirname "$0")/../.."           # repo root
VM=dfbsd-qemu/vm.sh

"$VM" status | grep -q '^up$' || "$VM" up 240

# 0. privilege gate: unprivileged write must be denied
"$VM" run_user 'sysctl -w debug.spin_lock_test=1; echo rc=$?'

# 1. clear msgbuf and fire the sysctl detached
"$VM" run_root 'dmesg -c >/dev/null; date; (sysctl -w debug.spin_lock_test=1 >/tmp/df2928.log 2>&1 &); echo launched'

# 2. poll the live msgbuf at ~22s: wait running unbroken (1..18 secs)
sleep 13
"$VM" run_root 'date; dmesg | grep -E "indefinite|spin_lock" | head -20; echo ---; dmesg | grep -c indefinite'

# 3. poll again at ~57s: wait STILL running (57 secs), 3s from panic
sleep 25
"$VM" run_root 'date; dmesg | grep indefinite | tail -4'

# 4. after the 60s mark the kernel panics; guest stops answering ssh
sleep 30
"$VM" status || true
date

# cleanup: restore clean-source snapshot
# "$VM" reset with-src
