diff --git a/.crumb b/.crumb index ab43d4e8..08aab5a1 100644 --- a/.crumb +++ b/.crumb @@ -28,32 +28,32 @@ }, { "digest": "4b1e9c63f6fd3b920b9f3f87060222ce2fba8342db53734ffc1748ba15fb74b5", - "last_commit": "94e8d58", + "last_commit": "fef6fe4", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { "digest": "754f7fe4dd0eafbab2cf6a9f33ccbaca08497dfca1459dc30c76214322d7d9cb", - "last_commit": "1ab797d", + "last_commit": "fef6fe4", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "76d29d214c40f89bd922e34a16760a2652ee259bc0f16d5bf6a847fcad3e5cad", - "last_commit": "bde85f8", + "digest": "7911dc6ee76b1fbf7fdc8a7593a34cccf8736783af2ae03ad52f57315bad24eb", + "last_commit": "f65912a", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:e2730ee97698209a116fcec3c63b59131770d42e76dc6f266ed86ef0a869e023", - "compiled_at": "2026-10-08T11:03:01Z", + "children_root": "sha256:6a476ae427333981961fd6201076130a343900a26eb56ddb1e17c1497fbda567", + "compiled_at": "2026-10-08T11:48:48Z", "compiler": "crumb-compile/1", - "digest": "sha256:ba0c7ee021274b14a59383b6ba55c9ac9f647713bd0af9b80f65cf6240b017f7", + "digest": "sha256:b13a40c5aac9c951cf183f268592b44cc5e922de708a2f9b7d61d0f8ba507ab9", "evidence_files": 153, "evidence_root": "sha256:29aa8775feb12d9e0e64b41fc1fc48b97e32dc16bf9176562cbe9fe1ea934f00", - "files": 2955, - "generated_at_commit": "1ab797d6c0c42fee6b5fbebebaa81e1e86a55609", - "source_tree": "sha256:d1db949af9f063f43edb8c89550ee3f096b3382d4a87d18d80997c8356c77198" + "files": 2957, + "generated_at_commit": "f65912aa75332b739f2d408118af9f2de7ee6c57", + "source_tree": "sha256:24fbeb88683bdbe3807edbc167f9748dc197d4d435e01be63065c013d3770ebc" }, "owner_lane": "", "provenance": { diff --git a/.github/workflows/ck-kernel.yml b/.github/workflows/ck-kernel.yml index be89a8f0..6ad5584d 100644 --- a/.github/workflows/ck-kernel.yml +++ b/.github/workflows/ck-kernel.yml @@ -5,6 +5,7 @@ # 1. host unit tests: make test, make sanitize (ASan + UBSan) # 2. stage host tests: make stage-test, make stage-sanitize # 3. the gate runner's own self-test: scripts/ck_gates.sh --self-test +# and the gate hold self-test: scripts/gate_hold_selftest.sh # 4. the QEMU gates: scripts/ck_gates.sh (scripts/qemu_ck_boot_test.sh and # scripts/qemu_ck_store_test.sh, and the other children; M0_ROLLBACK from # scripts/qemu_ck_rollback_test.sh also needs cargo and the Rust target @@ -37,6 +38,8 @@ on: - 'crates/aienos-fpu-probe/**' - 'crates/aienos-infer-kernel/**' - 'scripts/lib_ck_m1_checks.sh' + - 'scripts/lib_gate_hold.sh' + - 'scripts/gate_hold_selftest.sh' - '.github/workflows/ck-kernel.yml' pull_request: branches: [main] @@ -47,6 +50,8 @@ on: - 'crates/aienos-fpu-probe/**' - 'crates/aienos-infer-kernel/**' - 'scripts/lib_ck_m1_checks.sh' + - 'scripts/lib_gate_hold.sh' + - 'scripts/gate_hold_selftest.sh' - '.github/workflows/ck-kernel.yml' permissions: @@ -84,9 +89,13 @@ jobs: - name: Gate runner self-test run: bash scripts/ck_gates.sh --self-test + - name: Gate hold self-test (quiet flag read only, QEMU gate lock in quietlock record format, aienos#278) + run: bash scripts/gate_hold_selftest.sh + - name: Hardware staging image refuses TEST keys (build only) env: AIENOS_QUIET_FLAG: ${{ runner.temp }}/spark-quiet + AIENOS_GATE_LOCK: ${{ runner.temp }}/qemu-gate-lock run: bash scripts/ck_owner_keys_check.sh - name: Check the host tests left the tree clean (the gate runner refuses a dirty tree) @@ -97,8 +106,8 @@ jobs: - name: QEMU TCG gates env: AIENOS_QUIET_FLAG: ${{ runner.temp }}/spark-quiet - # The FPU, INFER and SCREEN children take the QEMU gate lock by exclusive create; - # its default home (~/workspace) does not exist on the runner. + # Every child takes the QEMU gate lock (scripts/lib_gate_hold.sh); keep it + # in runner temp rather than ~/workspace. AIENOS_GATE_LOCK: ${{ runner.temp }}/qemu-gate-lock run: | mkdir -p "${RUNNER_TEMP}/ck-receipt" diff --git a/.github/workflows/native-suites.yml b/.github/workflows/native-suites.yml index 9965b3df..1a44cb27 100644 --- a/.github/workflows/native-suites.yml +++ b/.github/workflows/native-suites.yml @@ -33,6 +33,7 @@ on: - "native/**" - ".github/workflows/native-suites.yml" - "scripts/qemu_native_nvme_test.sh" + - "scripts/lib_gate_hold.sh" workflow_dispatch: permissions: @@ -104,6 +105,7 @@ jobs: - name: Run QEMU NVMe gate env: AIENOS_QUIET_FLAG: ${{ runner.temp }}/aienos-quiet-flag + AIENOS_GATE_LOCK: ${{ runner.temp }}/aienos-gate-lock run: | bash scripts/qemu_native_nvme_test.sh 2>&1 | tee qemu.log # The script exits 0 on NOT_RUN; only an explicit PASS counts. diff --git a/scripts/.crumb b/scripts/.crumb index 45e10e31..b9eb9d93 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-08T11:00:47Z", + "compiled_at": "2026-10-08T11:48:48Z", "compiler": "crumb-compile/1", - "digest": "sha256:76d29d214c40f89bd922e34a16760a2652ee259bc0f16d5bf6a847fcad3e5cad", + "digest": "sha256:7911dc6ee76b1fbf7fdc8a7593a34cccf8736783af2ae03ad52f57315bad24eb", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 71, - "generated_at_commit": "bde85f8671a90d33a46667a5d915cb948035954a", - "source_tree": "sha256:e5b59853bf58aaaf4d1e0b20c7ae66becb340940d413b37abe93c08e8c000a53" + "files": 73, + "generated_at_commit": "f65912aa75332b739f2d408118af9f2de7ee6c57", + "source_tree": "sha256:ea47cf7d1dc97c81b749d0a4ba5ebfdf25b63dc2afcc2315e3c78836708bd78c" }, "owner_lane": "", "provenance": { diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh index 7bebf215..bf9f4a85 100755 --- a/scripts/ck_gates.sh +++ b/scripts/ck_gates.sh @@ -23,13 +23,12 @@ # in native/kernel/GATES.md. # # QEMU is an emulator: a PASS here qualifies nothing physical (the receipt -# says "physical": "NOT_RUN"). The child scripts serialise themselves: the -# KEYBOARD, FPU, INFER, SCREEN and CONSOLE children take the QEMU gate lock (~/workspace/.qemu-gate-lock, -# exclusive create) and report NOT_RUN while another gate run holds it or -# while the machine quiet flag (~/workspace/.spark-quiet) exists, which they -# only read (no agent raises the quiet flag without Drake's approval); the -# older children still take the quiet flag themselves. This script takes -# neither. +# says "physical": "NOT_RUN"). The child scripts serialise themselves: every +# child takes the QEMU gate lock (~/workspace/.qemu-gate-lock, quietlock record +# format, scripts/lib_gate_hold.sh, aienos#278) and reports NOT_RUN while +# another gate run holds it or while the machine quiet flag +# (~/workspace/.spark-quiet) is held, which every child only reads (no agent +# raises the quiet flag without Drake's approval). This script takes neither. # # Usage: bash scripts/ck_gates.sh [--out DIR] # bash scripts/ck_gates.sh --self-test diff --git a/scripts/ck_owner_keys_check.sh b/scripts/ck_owner_keys_check.sh index 8e46480c..4e8ca923 100755 --- a/scripts/ck_owner_keys_check.sh +++ b/scripts/ck_owner_keys_check.sh @@ -22,8 +22,9 @@ # label (the grep is not vacuous). # 5. The default image carries the "argus: TEST machine id 0xA1" banner (control); the # Makefile owner_check pattern matches a blob holding it (counterexample). -# Build-only: nothing is booted. Takes the Spark quiet flag like the qemu_ck_* -# scripts (NOT_RUN, exit 3, if another run holds it). No Python. +# Build-only: nothing is booted. Like the qemu_ck_* scripts it only reads the +# Spark quiet flag and takes the QEMU gate lock (scripts/lib_gate_hold.sh); +# NOT_RUN, exit 3, while either is held by another run. No Python. set -euo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -33,17 +34,18 @@ out="${CK_OWNER_CHECK_OUT:-${repo_root}/target/ck-owner-keys-check}" cross="" if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-ck_owner_keys_check $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "${repo_root}/scripts/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take ck_owner_keys_check "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "CK_OWNER_KEYS_CHECK: NOT_RUN" exit 3 fi tmp="$(mktemp -d)" cleanup() { rm -rf "${tmp}" - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi + gh_lock_release } trap cleanup EXIT diff --git a/scripts/gate_hold_selftest.sh b/scripts/gate_hold_selftest.sh new file mode 100644 index 00000000..4041a551 --- /dev/null +++ b/scripts/gate_hold_selftest.sh @@ -0,0 +1,193 @@ +#!/usr/bin/env bash +# Self-test of scripts/lib_gate_hold.sh, the one way QEMU gate scripts treat +# the machine quiet flag and the QEMU gate lock (aienos#278). +# +# Runs only in a temp folder: AIENOS_QUIET_FLAG and AIENOS_GATE_LOCK point +# there, never at ~/workspace. No QEMU, no build, no GPU. +# +# Cases: +# 1 two competing gates: the second refuses (exit 3) and names the holder +# 2 a gate killed mid-run (SIGKILL, no trap): another gate does not remove +# that dead hold before its expected_end; the same gate removes its own +# dead hold and runs; a dead hold past expected_end is stale for any gate +# 3 a record without a readable pid (old " " or "lane tag" form) +# is held and never removed, in the gate lock and in the quiet flag +# 4 the quiet flag held by a live owner: the gate refuses by name, the flag +# is unchanged +# 5 the quiet flag of the caller's own quietlock hold (hold= equal to +# QUIETLOCK_HOLD): the gate runs and leaves the flag alone +# 6 release removes only this run's own record +# 7 the gate lock record is in the quietlock record format (holder, start=, +# expected_end=, pid=, hold=); when quietlock is installed it reads the +# holder as alive +# 8 no script under scripts/ writes or removes the machine quiet flag +# 9 a "*" in a record is not globbed; a reused pid (other start time) is +# dead; no date or no flock: the gate refuses and writes nothing +# Exit 0 when every case passes, 1 otherwise. +set -uo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +lib="${here}/lib_gate_hold.sh" +tmp="$(mktemp -d)" +trap 'rm -rf "${tmp}"' EXIT +export AIENOS_QUIET_FLAG="${tmp}/spark-quiet" +export AIENOS_GATE_LOCK="${tmp}/qemu-gate-lock" +unset QUIETLOCK_HOLD +case "${AIENOS_QUIET_FLAG}${AIENOS_GATE_LOCK}" in + *"${HOME}/workspace/"*) echo "gate_hold_selftest: refusing to run on the real flag paths" >&2; exit 1 ;; +esac + +pass=0 fail=0 +ok() { pass=$((pass + 1)); echo "ok $1"; } +bad() { fail=$((fail + 1)); echo "FAIL $1"; } +check() { if eval "$2"; then ok "$1"; else bad "$1"; fi; } + +# A fake gate: takes the quiet check and the gate lock like a real gate, then +# runs its body ("sleep N" or "true"). Exit 3 + "NOT_RUN " when refused. +gate="${tmp}/fake_gate.sh" +cat >"${gate}" </dev/null; echo "${p}"; } +iso() { date -u -d "$1" +%Y-%m-%dT%H:%M:%SZ; } +wait_lock() { local i; for i in $(seq 1 100); do [[ -s "${AIENOS_GATE_LOCK}" ]] && return 0; sleep 0.05; done; return 1; } + +if [[ ! -r "${lib}" ]]; then + bad "lib_gate_hold.sh exists" +fi + +# ---- 1: two competing gates +"${gate}" gateA sleep 30 >"${tmp}/a.out" 2>&1 & a=$! +wait_lock +out="$("${gate}" gateB true 2>&1)"; rc=$? +check "1 second gate refuses (exit 3)" '[[ ${rc} == 3 ]]' +check "1 refusal names the holder" '[[ ${out} == *"gateA"* ]]' +out="$("${gate}" gateA true 2>&1)"; rc=$? +check "1 same gate name, live holder: refuses" '[[ ${rc} == 3 && ${out} == *"gateA"* ]]' + +# ---- 2: kill mid-gate, then cleanup removes only its own dead hold +kill -KILL "${a}" 2>/dev/null; wait "${a}" 2>/dev/null +check "2 SIGKILL leaves the hold behind" '[[ -s ${AIENOS_GATE_LOCK} ]]' +before="$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" +out="$("${gate}" gateB true 2>&1)"; rc=$? +check "2 another gate does not remove a dead hold before expected_end" \ + '[[ ${rc} == 3 && "$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" == "${before}" ]]' +out="$("${gate}" gateA true 2>&1)"; rc=$? +check "2 the same gate removes its own dead hold and runs" '[[ ${rc} == 0 ]]' +check "2 and releases it at exit" '[[ ! -e ${AIENOS_GATE_LOCK} ]]' +p="$(dead_pid)" +printf 'aienos qemu-gate gateX start=%s expected_end=%s pid=%s hold=old\n' \ + "$(iso '-2 hours')" "$(iso '-1 hour')" "${p}" >"${AIENOS_GATE_LOCK}" +out="$("${gate}" gateA true 2>&1)"; rc=$? +check "2 dead hold past expected_end is stale for any gate" '[[ ${rc} == 0 && ! -e ${AIENOS_GATE_LOCK} ]]' + +# ---- 3: unreadable records are held, never removed +for rec in "qemu_ck_boot_test 12345" "lane11 qemu_native_nvme_test" "garbage"; do + printf '%s\n' "${rec}" >"${AIENOS_GATE_LOCK}" + out="$("${gate}" gateA true 2>&1)"; rc=$? + check "3 gate lock '${rec}' is held" '[[ ${rc} == 3 ]]' + check "3 gate lock '${rec}' is not removed" '[[ "$(cat "${AIENOS_GATE_LOCK}")" == "${rec}" ]]' +done +rm -f "${AIENOS_GATE_LOCK}" +printf 'qemu_ck_boot_test 12345\n' >"${AIENOS_QUIET_FLAG}" +out="$("${gate}" gateA true 2>&1)"; rc=$? +check "3 unreadable quiet flag is held" '[[ ${rc} == 3 && -e ${AIENOS_QUIET_FLAG} ]]' +rm -f "${AIENOS_QUIET_FLAG}" + +# ---- 4: quiet flag of a live owner +printf 'gb10 t4fix measure start=%s expected_end=%s pid=%s hold=q-1\n' \ + "$(iso 'now')" "$(iso '+20 minutes')" "$$" >"${AIENOS_QUIET_FLAG}" +before="$(cat "${AIENOS_QUIET_FLAG}")" +out="$("${gate}" gateA true 2>&1)"; rc=$? +check "4 gate refuses while a live owner holds the machine" '[[ ${rc} == 3 ]]' +check "4 refusal names the machine holder" '[[ ${out} == *"gb10 t4fix measure"* ]]' +check "4 quiet flag unchanged" '[[ "$(cat "${AIENOS_QUIET_FLAG}")" == "${before}" && ! -e ${AIENOS_GATE_LOCK} ]]' + +# ---- 5: inside the caller's own quietlock hold +out="$(QUIETLOCK_HOLD=q-1 "${gate}" gateA true 2>&1)"; rc=$? +check "5 gate runs inside the caller's quietlock hold" '[[ ${rc} == 0 ]]' +check "5 quiet flag left alone" '[[ "$(cat "${AIENOS_QUIET_FLAG}")" == "${before}" ]]' +out="$(QUIETLOCK_HOLD=q-2 "${gate}" gateA true 2>&1)"; rc=$? +check "5 a different hold id does not count" '[[ ${rc} == 3 ]]' +rm -f "${AIENOS_QUIET_FLAG}" + +# ---- 6: release removes only this run's own record +"${gate}" gateA sleep 1 >"${tmp}/a6.out" 2>&1 & a=$! +wait_lock +other="aienos qemu-gate gateZ start=$(iso now) expected_end=$(iso '+5 minutes') pid=$$ hold=z" +printf '%s\n' "${other}" >"${AIENOS_GATE_LOCK}" +wait "${a}" +check "6 a replaced record is left alone at exit" '[[ "$(cat "${AIENOS_GATE_LOCK}" 2>/dev/null)" == "${other}" ]]' +rm -f "${AIENOS_GATE_LOCK}" + +# ---- 7: record format +"${gate}" gateA sleep 30 >/dev/null 2>&1 & a=$! +wait_lock +rec="$(head -n1 "${AIENOS_GATE_LOCK}")" +check "7 record names holder, start, expected_end, pid and hold" \ + '[[ ${rec} =~ ^aienos\ qemu-gate\ gateA\ start=[0-9TZ:-]+\ expected_end=[0-9TZ:-]+\ pid=[0-9]+\ .*hold=[A-Za-z0-9._-]+ ]]' +if command -v quietlock >/dev/null 2>&1; then + mkdir -p "${tmp}/ql" + cp "${AIENOS_GATE_LOCK}" "${tmp}/ql/.spark-quiet" + qout="$(QUIETLOCK_DIR="${tmp}/ql" quietlock check 2>&1)" + check "7 quietlock reads the record as alive" '[[ ${qout} == *"pid="[0-9]* && ${qout} == *"alive=yes"* ]]' +else + echo "skip 7 quietlock not installed" +fi +kill -TERM "${a}" 2>/dev/null; wait "${a}" 2>/dev/null +check "7 SIGTERM releases the hold" '[[ ! -e ${AIENOS_GATE_LOCK} ]]' + +# ---- 9: review hardening (aienos#281 review) +# 9a a "*" in a record is a literal token, never a filename glob +mkdir -p "${tmp}/globdir" && touch "${tmp}/globdir/gateA" +p="$(dead_pid)" +printf 'aienos qemu-gate * start=%s expected_end=%s pid=%s hold=g\n' "$(iso '-1 minute')" "$(iso '+30 minutes')" "${p}" >"${AIENOS_GATE_LOCK}" +before="$(cat "${AIENOS_GATE_LOCK}")" +out="$(cd "${tmp}/globdir" && "${gate}" gateA true 2>&1)"; rc=$? +check "9a holder '*' is not read as a filename (dead, before expected_end: held)" \ + '[[ ${rc} == 3 && "$(cat "${AIENOS_GATE_LOCK}")" == "${before}" ]]' +rm -f "${AIENOS_GATE_LOCK}" +# 9b pid reused: the recorded start time differs from the live pid's, so the +# holder is dead (removed by the same gate name) +printf 'aienos qemu-gate gateA start=%s expected_end=%s pid=%s pidstart=1 hold=g\n' "$(iso now)" "$(iso '+30 minutes')" "$$" >"${AIENOS_GATE_LOCK}" +out="$("${gate}" gateA true 2>&1)"; rc=$? +check "9b live pid with a different start time counts as dead" '[[ ${rc} == 0 && ! -e ${AIENOS_GATE_LOCK} ]]' +printf 'aienos qemu-gate gateA start=%s expected_end=%s pid=%s pidstart=1 hold=g\n' "$(iso now)" "$(iso '+30 minutes')" "$$" >"${AIENOS_GATE_LOCK}" +out="$("${gate}" gateB true 2>&1)"; rc=$? +check "9b and another gate still waits for its expected_end" '[[ ${rc} == 3 && -e ${AIENOS_GATE_LOCK} ]]' +rm -f "${AIENOS_GATE_LOCK}" +# 9c no clock or no flock: refuse, write nothing +mkdir -p "${tmp}/nodate" "${tmp}/noflock" +for t in bash cat flock mkdir dirname rm sleep true; do ln -sf "$(command -v "${t}")" "${tmp}/nodate/${t}"; done +for t in bash cat date mkdir dirname rm sleep true; do ln -sf "$(command -v "${t}")" "${tmp}/noflock/${t}"; done +out="$(PATH="${tmp}/nodate" "${gate}" gateA true 2>&1)"; rc=$? +check "9c no date: refuses and writes no record" '[[ ${rc} == 3 && ${out} == *"clock"* && ! -e ${AIENOS_GATE_LOCK} ]]' +out="$(PATH="${tmp}/noflock" "${gate}" gateA true 2>&1)"; rc=$? +check "9c no flock: refuses by name and writes no record" '[[ ${rc} == 3 && ${out} == *"flock not found"* && ! -e ${AIENOS_GATE_LOCK} ]]' + +# ---- 8: no script writes or removes the machine quiet flag +# trust1_m5_qualify.sh is left out of the grep: it writes and removes a flag +# only in --self-test, after pointing QUIET_FLAG at a temp file; 8b checks that +# every reassignment of its QUIET_FLAG is such a temp file or a restore. +writers="$(grep -n -E '(>|rm -f) *"?\$\{?(quiet_flag|quiet|QUIET_FLAG)\}?"' "${here}"/*.sh 2>/dev/null \ + | grep -v -E '/(gate_hold_selftest|trust1_m5_qualify)\.sh:' | cut -d: -f1,2 || true)" +check "8 no script writes or removes the quiet flag" '[[ -z ${writers} ]]' +[[ -n ${writers} ]] && printf ' %s\n' ${writers} +reassign="$(grep -n -E '(^|[ ;])QUIET_FLAG=' "${here}/trust1_m5_qualify.sh" \ + | grep -v -E 'QUIET_FLAG="(\$\{AIENOS_QUIET_FLAG:-\$\{HOME\}/workspace/\.spark-quiet\}|\$\{tmp\}/[A-Za-z0-9_]+|\$\{saved_quiet2?\})"' || true)" +check "8b trust1_m5_qualify.sh points QUIET_FLAG only at the default, temp files or a restore" '[[ -z ${reassign} ]]' +[[ -n ${reassign} ]] && printf ' %s\n' "${reassign}" + +echo "gate_hold_selftest: ${pass} passed, ${fail} failed" +[[ ${fail} == 0 ]] diff --git a/scripts/lib_ck_cont_qemu.sh b/scripts/lib_ck_cont_qemu.sh index 061c3e9a..d2f55bd6 100644 --- a/scripts/lib_ck_cont_qemu.sh +++ b/scripts/lib_ck_cont_qemu.sh @@ -109,20 +109,16 @@ cq_default_image_checks() { } cq_quiet_and_trap() { - quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" - quiet_tag="${AIENOS_QUIET_TAG:-${script_tag} $$}" - if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" + # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): + # scripts/lib_gate_hold.sh (aienos#278). + . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" + if ! gh_quiet_check || ! gh_lock_take "${script_tag}" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" verdict NOT_RUN exit 3 fi - own_flag=1 - release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi - } + # Release only this run's own gate lock record, at most once. + release_flag() { gh_lock_release; } top="$(mktemp -d)" qemu_pid="" cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null; rm -rf "${top}"; release_flag; } diff --git a/scripts/lib_gate_hold.sh b/scripts/lib_gate_hold.sh new file mode 100644 index 00000000..d5f3b31a --- /dev/null +++ b/scripts/lib_gate_hold.sh @@ -0,0 +1,177 @@ +# shellcheck shell=bash +# lib_gate_hold.sh: how a QEMU gate script treats the machine quiet flag and +# the QEMU gate lock (aienos#278). Sourced; sets no shell options. +# +# The machine quiet flag (AIENOS_QUIET_FLAG, default ~/workspace/.spark-quiet) +# belongs to quietlock: gates only READ it (no agent raises it without +# Drake's approval; holds are taken with `quietlock hold`). A gate refuses +# while the flag exists, except inside the caller's own quietlock hold (the +# flag's hold= equals $QUIETLOCK_HOLD, which `quietlock hold` exports to +# its command). +# +# One QEMU gate at a time: the gate lock (AIENOS_GATE_LOCK, default +# ~/workspace/.qemu-gate-lock) holds one record in the quietlock record format +# aienos qemu-gate start= expected_end= pid= pidstart= hold= +# (holder = the first three fields), so `quietlock check`-style readers see +# the holder and whether it is alive. Taking, clearing and releasing run under +# flock on ".mutex", and the record is created by exclusive create. +# +# Rules (the quietlock rules, gate side): +# - a live holder is never removed; +# - a record without a readable holder, pid= or expected_end= is HELD and is +# never removed (nothing here can prove its holder dead); +# - a dead holder's record is removed by the same gate (same ) at once, +# or by any gate once its expected_end has passed (quietlock's stale rule); +# - a gate removes only its own record at exit (exact line match). +# A holder is dead when /proc/ is gone, is a zombie, or started at a +# different time than pidstart= records (pid reused). +# +# API: gh_quiet_check; gh_lock_take NAME MINUTES; gh_lock_release. +# The first two return 0 (go) or 3 (refused; $gh_why says why and names the +# holder). gh_lock_release is safe to call more than once (EXIT trap). + +gh_quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" +gh_gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}" +gh_why="" +gh_line="" + +_gh_iso() { date -u -d "@$1" +%Y-%m-%dT%H:%M:%SZ; } + +# Start time (clock ticks since boot, /proc//stat field 22) of a pid. +_gh_pidstart() { + local s + s="$(cat "/proc/$1/stat" 2>/dev/null)" || return 1 + s="${s##*) }" + # shellcheck disable=SC2086 + set -- ${s} + [[ $# -ge 20 ]] || return 1 + printf '%s\n' "${20}" +} + +_gh_alive() { # pid [pidstart] + local s st + s="$(cat "/proc/$1/stat" 2>/dev/null)" || return 1 + st="${s##*) }"; st="${st%% *}" + [[ "${st}" != Z ]] || return 1 + [[ -z "${2:-}" || "$(_gh_pidstart "$1")" == "$2" ]] +} + +# Parse the first line of a record file into _gh_holder _gh_name _gh_pid +# _gh_pidstart _gh_end _gh_end_text _gh_hold. Return 0 when holder (three +# plain fields), pid= and expected_end= are all readable, else 1. +_gh_parse() { + local line tok n=0 + _gh_holder="" _gh_name="" _gh_pid="" _gh_pidstart="" _gh_end="" _gh_end_text="" _gh_hold="" + IFS= read -r line <"$1" 2>/dev/null || [[ -n "${line:-}" ]] || return 1 + _gh_first="${line:0:200}" + local -a toks + read -r -a toks <<<"${line}" + for tok in "${toks[@]}"; do + case "${tok}" in + pid=*) [[ -z "${_gh_pid}" ]] && _gh_pid="${tok#pid=}" ;; + pidstart=*) [[ -z "${_gh_pidstart}" ]] && _gh_pidstart="${tok#pidstart=}" ;; + expected_end=*) [[ -z "${_gh_end_text}" ]] && _gh_end_text="${tok#expected_end=}" ;; + hold=*) [[ -z "${_gh_hold}" ]] && _gh_hold="${tok#hold=}" ;; + *=*) ;; + *) if [[ ${n} -lt 3 ]]; then + n=$((n + 1)); _gh_holder="${_gh_holder:+${_gh_holder} }${tok}"; [[ ${n} == 3 ]] && _gh_name="${tok}" + fi ;; + esac + done + [[ ${n} == 3 && "${_gh_pid}" =~ ^[1-9][0-9]*$ ]] || return 1 + [[ -n "${_gh_end_text}" ]] && _gh_end="$(date -u -d "${_gh_end_text}" +%s 2>/dev/null)" || return 1 + [[ "${_gh_pidstart}" =~ ^[0-9]*$ ]] || _gh_pidstart="" + return 0 +} + +_gh_describe() { # file -> "holder='...' pid=... alive=... expected_end=..." or the raw line + if _gh_parse "$1"; then + local alive=no + _gh_alive "${_gh_pid}" "${_gh_pidstart}" && alive=yes + printf "holder='%s' pid=%s alive=%s expected_end=%s" "${_gh_holder}" "${_gh_pid}" "${alive}" "${_gh_end_text}" + else + printf "unreadable record, treated as held: %s" "${_gh_first:-?}" + fi +} + +# 0 = the machine is not held for this gate; 3 = refused. +gh_quiet_check() { + gh_why="" + [[ -e "${gh_quiet_flag}" ]] || return 0 + if _gh_parse "${gh_quiet_flag}" && [[ -n "${_gh_hold}" && "${_gh_hold}" == "${QUIETLOCK_HOLD:-}" ]]; then + echo "gate_hold: running inside the caller's quietlock hold ${_gh_hold} (quiet flag left alone)" >&2 + return 0 + fi + gh_why="quiet flag ${gh_quiet_flag} is held ($(_gh_describe "${gh_quiet_flag}"))" + return 3 +} + +gh_lock_take() { # NAME MINUTES + local name="$1" minutes="$2" fd now line rc=0 + gh_why="" + if [[ ! "${name}" =~ ^[A-Za-z0-9._-]{1,64}$ || ! "${minutes}" =~ ^[1-9][0-9]*$ ]]; then + gh_why="gate_hold: bad gate name or minutes ('${name}', '${minutes}')" + return 3 + fi + if ! command -v flock >/dev/null 2>&1; then + gh_why="gate_hold: flock not found; refusing to take the QEMU gate lock" + return 3 + fi + mkdir -p "$(dirname "${gh_gate_lock}")" 2>/dev/null || true + if ! exec {fd}>>"${gh_gate_lock}.mutex"; then + gh_why="QEMU gate lock mutex ${gh_gate_lock}.mutex cannot be opened" + return 3 + fi + if ! flock -x -w 30 "${fd}"; then + exec {fd}>&- + gh_why="QEMU gate lock mutex ${gh_gate_lock}.mutex busy for 30 s" + return 3 + fi + now="$(date +%s 2>/dev/null)" + if [[ ! "${now}" =~ ^[0-9]+$ ]] || ! _gh_iso "${now}" >/dev/null 2>&1; then + flock -u "${fd}" + exec {fd}>&- + gh_why="gate_hold: the clock (date) is unreadable; refusing to take the QEMU gate lock" + return 3 + fi + if [[ -e "${gh_gate_lock}" ]]; then + if ! _gh_parse "${gh_gate_lock}"; then + gh_why="QEMU gate lock ${gh_gate_lock} is held (unreadable record, treated as held and never removed: ${_gh_first:-?})" + rc=3 + elif _gh_alive "${_gh_pid}" "${_gh_pidstart}"; then + gh_why="QEMU gate lock ${gh_gate_lock} is held ($(_gh_describe "${gh_gate_lock}"))" + rc=3 + elif [[ "${_gh_name}" == "${name}" || ${now} -gt ${_gh_end} ]]; then + echo "gate_hold: removing dead QEMU gate lock record ($(_gh_describe "${gh_gate_lock}"); $([[ "${_gh_name}" == "${name}" ]] && echo "this gate's own" || echo "past expected_end"))" >&2 + rm -f "${gh_gate_lock}" + else + gh_why="QEMU gate lock ${gh_gate_lock} is held by a dead gate before its expected_end ($(_gh_describe "${gh_gate_lock}")); only that gate, or any gate after expected_end, removes it" + rc=3 + fi + fi + if [[ ${rc} == 0 ]]; then + line="aienos qemu-gate ${name} start=$(_gh_iso "${now}") expected_end=$(_gh_iso $((now + minutes * 60))) pid=$$ pidstart=$(_gh_pidstart $$) hold=gate-$$-${now}" + if ( set -C; printf '%s\n' "${line}" >"${gh_gate_lock}" ) 2>/dev/null; then + gh_line="${line}" + else + gh_why="QEMU gate lock ${gh_gate_lock} appeared while taking it ($(_gh_describe "${gh_gate_lock}"))" + rc=3 + fi + fi + flock -u "${fd}" + exec {fd}>&- + return ${rc} +} + +gh_lock_release() { + local fd cur + [[ -n "${gh_line}" ]] || return 0 + if exec {fd}>>"${gh_gate_lock}.mutex" && flock -x -w 30 "${fd}"; then + IFS= read -r cur <"${gh_gate_lock}" 2>/dev/null || true + [[ "${cur:-}" == "${gh_line}" ]] && rm -f "${gh_gate_lock}" + flock -u "${fd}" + exec {fd}>&- + fi + gh_line="" + return 0 +} diff --git a/scripts/qemu_ck_artifact_test.sh b/scripts/qemu_ck_artifact_test.sh index 833192d1..c6c82320 100755 --- a/scripts/qemu_ck_artifact_test.sh +++ b/scripts/qemu_ck_artifact_test.sh @@ -36,9 +36,8 @@ # - candidates are read from the sealed Store on the NVMe boot disk instead of # ESP files read by a UEFI loader: the C boot stub reads no files; # - no Machine 1 captured-log mode (SEED-0B Machine 1 stays with the Rust gate); -# - takes the machine quiet flag itself (noclobber) before building and prints -# NOT_RUN while another run holds it; AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG as -# in qemu_ck_boot_test.sh; +# - before building, prints NOT_RUN while the machine quiet flag or the QEMU +# gate lock is held, as in qemu_ck_boot_test.sh (scripts/lib_gate_hold.sh); # - stricter: any QEMU exit status other than 0 fails. # Last line: AIENOS_CK_P2_ARTIFACT: PASS|FAIL|NOT_RUN. # Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). @@ -54,20 +53,16 @@ command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not ins [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; } # Machine quiet flag: one heavy run (builds included) at a time on the Spark. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_artifact_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take "qemu_ck_artifact_test" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${verdict}: NOT_RUN" exit 3 fi -own_flag=1 -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } work="$(mktemp -d)" cleanup() { rm -rf "${work}" diff --git a/scripts/qemu_ck_boot_test.sh b/scripts/qemu_ck_boot_test.sh index 199f2667..fe735408 100755 --- a/scripts/qemu_ck_boot_test.sh +++ b/scripts/qemu_ck_boot_test.sh @@ -8,10 +8,9 @@ # # Differences from qemu_boot_test.sh, all deliberate: # - builds with make (gcc + binutils), not cargo; -# - takes the machine quiet flag itself (noclobber) and prints NOT_RUN if -# another run holds it; AIENOS_QUIET_FLAG overrides the flag path and -# AIENOS_QUIET_TAG the text written into it (the flag is removed only if -# it still holds exactly that text, and at most once per run); +# - prints NOT_RUN while the machine quiet flag (AIENOS_QUIET_FLAG) is held, +# which it only reads, or while another gate holds the QEMU gate lock +# (AIENOS_GATE_LOCK); both through scripts/lib_gate_hold.sh (aienos#278); # - the five M3 checks (threads, el0, preempt, placement, ipc) use the same # patterns and give their own verdict AIENOS_CK_M3, which is PASS only if # all five pass AND every M1 check of the same boot passes (no fault, no @@ -44,25 +43,17 @@ commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" out="${repo_root}/target/native-kernel" make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" >/dev/null -# Machine quiet flag: one heavy run at a time on the Spark. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_boot_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take qemu_ck_boot_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "AIENOS_CK_M3: NOT_RUN" echo "AIENOS_CK_M1: NOT_RUN" exit 3 fi - -own_flag=1 -# Release the flag exactly once, and only while this run still owns it, so a -# later run that wrote the same tag never loses its flag to this EXIT trap. -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } work="$(mktemp -d)" cleanup() { rm -rf "${work}" diff --git a/scripts/qemu_ck_console_test.sh b/scripts/qemu_ck_console_test.sh index eee211cc..c76f9809 100755 --- a/scripts/qemu_ck_console_test.sh +++ b/scripts/qemu_ck_console_test.sh @@ -95,26 +95,21 @@ else session_efi="${out}/full-console-session/BOOTAA64.EFI" fi -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -if [[ -e "${quiet_flag}" ]]; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check; then + echo "NOT_RUN ${gh_why}" echo "${final_tag}: NOT_RUN (quiet flag held)" exit 3 fi -gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}" -gate_tag="${AIENOS_GATE_TAG:-qemu_ck_console_test $$}" -if ! ( set -C; echo "${gate_tag}" > "${gate_lock}" ) 2>/dev/null; then - echo "NOT_RUN QEMU gate lock ${gate_lock} is held: $(head -c 200 "${gate_lock}" 2>/dev/null || true)" +if ! gh_lock_take qemu_ck_console_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${final_tag}: NOT_RUN (QEMU gate lock held)" exit 3 fi -own_lock=1 -release_lock() { - if [[ "${own_lock}" == 1 ]]; then - own_lock=0 - if [[ -f "${gate_lock}" ]] && grep -qxF -- "${gate_tag}" "${gate_lock}"; then rm -f "${gate_lock}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_lock() { gh_lock_release; } top="$(mktemp -d)" qemu_pid="" drain_pid="" diff --git a/scripts/qemu_ck_disk_layout_test.sh b/scripts/qemu_ck_disk_layout_test.sh index 87b4b780..a464cad9 100755 --- a/scripts/qemu_ck_disk_layout_test.sh +++ b/scripts/qemu_ck_disk_layout_test.sh @@ -44,8 +44,8 @@ # "NOT REFUSED", so the Store is never reached); the default image file must # not contain the bypass warning text. # -# Takes the machine quiet flag itself like qemu_ck_store_test.sh -# (AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG). Any QEMU exit status other than 0 +# Reads the machine quiet flag and takes the QEMU gate lock like +# qemu_ck_store_test.sh (scripts/lib_gate_hold.sh). Any QEMU exit status other than 0 # fails (via the M1 checks). Final line: AIENOS_CK_DISK_LAYOUT: # PASS|FAIL|NOT_RUN. Needs qemu-system-aarch64 and AAVMF. set -euo pipefail @@ -71,20 +71,16 @@ safe_efi="${out}/full/BOOTAA64.EFI" bypass_efi="${out}/full-test-xlate-bypass/BOOTAA64.EFI" # Machine quiet flag: one heavy run at a time on the Spark. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_disk_layout_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take "qemu_ck_disk_layout_test" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" verdicts NOT_RUN exit 3 fi -own_flag=1 -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } top="$(mktemp -d)" cleanup() { rm -rf "${top}" diff --git a/scripts/qemu_ck_fpu_test.sh b/scripts/qemu_ck_fpu_test.sh index e5ab78df..eb9fff95 100755 --- a/scripts/qemu_ck_fpu_test.sh +++ b/scripts/qemu_ck_fpu_test.sh @@ -19,7 +19,7 @@ # # Usage: bash scripts/qemu_ck_fpu_test.sh the gate # bash scripts/qemu_ck_fpu_test.sh --self-test canned logs, no QEMU/build -# Environment: AIENOS_QEMU_TIMEOUT (180 s), AIENOS_GATE_LOCK / AIENOS_GATE_TAG, +# Environment: AIENOS_QEMU_TIMEOUT (180 s), AIENOS_GATE_LOCK, AIENOS_GATE_MINUTES, # AIENOS_QUIET_FLAG (read only), # AIENOS_LOG_DIR, AIENOS_QEMU_VERBOSE. set -euo pipefail @@ -97,27 +97,21 @@ make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" # Machine-wide quiet flag: read only, never written here (Drake's rule: no # agent raises it without approval). If someone holds it: NOT_RUN. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -if [[ -e "${quiet_flag}" ]]; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -# One QEMU gate at a time: exclusive create (set -C) of the gate lock. -gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}" -gate_tag="${AIENOS_GATE_TAG:-qemu_ck_fpu_test $$}" -if ! ( set -C; echo "${gate_tag}" > "${gate_lock}" ) 2>/dev/null; then - echo "NOT_RUN QEMU gate lock ${gate_lock} is held: $(head -c 200 "${gate_lock}" 2>/dev/null || true)" +if ! gh_lock_take qemu_ck_fpu_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -own_lock=1 -release_lock() { - if [[ "${own_lock}" == 1 ]]; then - own_lock=0 - if [[ -f "${gate_lock}" ]] && grep -qxF -- "${gate_tag}" "${gate_lock}"; then rm -f "${gate_lock}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_lock() { gh_lock_release; } work="$(mktemp -d)" cleanup() { rm -rf "${work}"; release_lock; } trap cleanup EXIT diff --git a/scripts/qemu_ck_infer_test.sh b/scripts/qemu_ck_infer_test.sh index a1a9b8f7..51343710 100755 --- a/scripts/qemu_ck_infer_test.sh +++ b/scripts/qemu_ck_infer_test.sh @@ -46,8 +46,8 @@ # AIENOS_CK_INFER_DISK_NEGATIVE_CONTROL: PASS when it does # bash scripts/qemu_ck_infer_test.sh --self-test canned logs, no QEMU/build # Environment: AIENOS_MODEL (the GGUF), AIENOS_QEMU_TIMEOUT (3600 s: TCG runs -# the 25 forward passes at roughly 20-30 s each), AIENOS_GATE_LOCK / -# AIENOS_GATE_TAG, AIENOS_QUIET_FLAG (read only), AIENOS_LOG_DIR, +# the 25 forward passes at roughly 20-30 s each), AIENOS_GATE_LOCK, +# AIENOS_GATE_MINUTES, AIENOS_QUIET_FLAG (read only), AIENOS_LOG_DIR, # AIENOS_QEMU_VERBOSE. set -euo pipefail @@ -272,27 +272,21 @@ make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" # Machine-wide quiet flag: read only, never written here. If someone holds # it, a CPU-heavy TCG run would disturb their measurement: NOT_RUN. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -if [[ -e "${quiet_flag}" ]]; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -# One QEMU gate at a time: exclusive create (set -C) of the gate lock. -gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}" -gate_tag="${AIENOS_GATE_TAG:-qemu_ck_infer_test $$}" -if ! ( set -C; echo "${gate_tag}" > "${gate_lock}" ) 2>/dev/null; then - echo "NOT_RUN QEMU gate lock ${gate_lock} is held: $(head -c 200 "${gate_lock}" 2>/dev/null || true)" +if ! gh_lock_take qemu_ck_infer_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -own_lock=1 -release_lock() { - if [[ "${own_lock}" == 1 ]]; then - own_lock=0 - if [[ -f "${gate_lock}" ]] && grep -qxF -- "${gate_tag}" "${gate_lock}"; then rm -f "${gate_lock}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_lock() { gh_lock_release; } work="$(mktemp -d)" cleanup() { rm -rf "${work}"; release_lock; } trap cleanup EXIT diff --git a/scripts/qemu_ck_keyboard_test.sh b/scripts/qemu_ck_keyboard_test.sh index b1e91634..c3921466 100755 --- a/scripts/qemu_ck_keyboard_test.sh +++ b/scripts/qemu_ck_keyboard_test.sh @@ -129,27 +129,21 @@ fi # Machine-wide quiet flag: read only, never written here (Drake's rule: no # agent raises it without approval). If someone holds it: NOT_RUN. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -if [[ -e "${quiet_flag}" ]]; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check; then + echo "NOT_RUN ${gh_why}" echo "${final_tag}: NOT_RUN (quiet flag held)" exit 3 fi -# One QEMU gate at a time: exclusive create (set -C) of the gate lock. -gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}" -gate_tag="${AIENOS_GATE_TAG:-qemu_ck_keyboard_test $$}" -if ! ( set -C; echo "${gate_tag}" > "${gate_lock}" ) 2>/dev/null; then - echo "NOT_RUN QEMU gate lock ${gate_lock} is held: $(head -c 200 "${gate_lock}" 2>/dev/null || true)" +if ! gh_lock_take qemu_ck_keyboard_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${final_tag}: NOT_RUN (QEMU gate lock held)" exit 3 fi -own_lock=1 -release_lock() { - if [[ "${own_lock}" == 1 ]]; then - own_lock=0 - if [[ -f "${gate_lock}" ]] && grep -qxF -- "${gate_tag}" "${gate_lock}"; then rm -f "${gate_lock}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_lock() { gh_lock_release; } top="$(mktemp -d)" qemu_pid="" cleanup() { diff --git a/scripts/qemu_ck_net_test.sh b/scripts/qemu_ck_net_test.sh index aa2e6818..c5842129 100755 --- a/scripts/qemu_ck_net_test.sh +++ b/scripts/qemu_ck_net_test.sh @@ -59,8 +59,8 @@ # 127.0.0.1), so restricted slirp cannot carry this round trip; nothing else # listens for the guest and the run lasts two short boots. # -# Takes the machine quiet flag itself like the other qemu_ck_* scripts -# (AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG). Final line: AIENOS_CK_NET: +# Reads the machine quiet flag and takes the QEMU gate lock through +# scripts/lib_gate_hold.sh like the other qemu_ck_* scripts. Final line: AIENOS_CK_NET: # PASS|FAIL|NOT_RUN. Exit 0 PASS, 1 FAIL, 2 missing tools, 3 NOT_RUN. # Needs qemu-system-aarch64, AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64) # and a C compiler. @@ -86,20 +86,16 @@ port="$(sed -nE 's/^#define CK_NET_ECHO_PORT ([0-9]+)u.*/\1/p' native/kernel/dev lport="$(sed -nE 's/^#define CK_NET_LOCAL_PORT ([0-9]+)u.*/\1/p' native/kernel/dev/net_udp.h)" [[ -n "${port}" && -n "${lport}" ]] || { echo "FAIL ports not found in native/kernel/dev/net_udp.h"; echo "AIENOS_CK_NET: FAIL"; exit 1; } -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_net_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take "qemu_ck_net_test" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "AIENOS_CK_NET: NOT_RUN" exit 3 fi -own_flag=1 -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } top="$(mktemp -d)" helper_pid="" cleanup() { diff --git a/scripts/qemu_ck_osc_unit_test.sh b/scripts/qemu_ck_osc_unit_test.sh index d12b8edf..6048b478 100755 --- a/scripts/qemu_ck_osc_unit_test.sh +++ b/scripts/qemu_ck_osc_unit_test.sh @@ -35,20 +35,16 @@ vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; } [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; } -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_osc_unit_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take "qemu_ck_osc_unit_test" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${verdict}: NOT_RUN" exit 3 fi -own_flag=1 -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } work="$(mktemp -d)" cleanup() { rm -rf "${work}"; release_flag; } trap cleanup EXIT diff --git a/scripts/qemu_ck_rollback_test.sh b/scripts/qemu_ck_rollback_test.sh index 62e65271..3e28069e 100755 --- a/scripts/qemu_ck_rollback_test.sh +++ b/scripts/qemu_ck_rollback_test.sh @@ -68,7 +68,7 @@ # logs and store dumps, no QEMU) # Environment: AAVMF_CODE, AAVMF_VARS, AIENOS_QEMU_TIMEOUT (per boot, 120 s), # AIENOS_HANG_HOLD (10 s), AIENOS_QUIET_FLAG (read only), AIENOS_GATE_LOCK / -# AIENOS_GATE_TAG (exclusive QEMU gate lock), AIENOS_ROLLBACK_KEEP=DIR (copy +# AIENOS_GATE_MINUTES (QEMU gate lock, scripts/lib_gate_hold.sh), AIENOS_ROLLBACK_KEEP=DIR (copy # the lane logs and store dumps there). # Exit: 0 PASS, 1 FAIL, 2 missing tool, 3 NOT_RUN (quiet flag or lock held). set -euo pipefail @@ -221,26 +221,21 @@ command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not ins [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict_name}: NOT_RUN"; exit 2; } command -v cargo >/dev/null || { echo "cargo not installed (the stager/Default mock is Rust)"; echo "${verdict_name}: NOT_RUN"; exit 2; } -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -if [[ -e "${quiet_flag}" ]]; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}" -gate_tag="${AIENOS_GATE_TAG:-qemu_ck_rollback_test $$}" -if ! ( set -C; echo "${gate_tag}" > "${gate_lock}" ) 2>/dev/null; then - echo "NOT_RUN QEMU gate lock ${gate_lock} is held: $(head -c 200 "${gate_lock}" 2>/dev/null || true)" +if ! gh_lock_take qemu_ck_rollback_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -own_lock=1 -release_lock() { - if [[ "${own_lock}" == 1 ]]; then - own_lock=0 - if [[ -f "${gate_lock}" ]] && grep -qxF -- "${gate_tag}" "${gate_lock}"; then rm -f "${gate_lock}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_lock() { gh_lock_release; } work="$(mktemp -d)" qemu_pid="" cleanup() { diff --git a/scripts/qemu_ck_screen_test.sh b/scripts/qemu_ck_screen_test.sh index 41ffca45..817af5d6 100755 --- a/scripts/qemu_ck_screen_test.sh +++ b/scripts/qemu_ck_screen_test.sh @@ -34,8 +34,8 @@ # Usage: bash scripts/qemu_ck_screen_test.sh the gate # bash scripts/qemu_ck_screen_test.sh --self-test canned logs and # rendered screens, no QEMU -# Environment: AIENOS_QEMU_TIMEOUT (300 s per boot), AIENOS_GATE_LOCK / -# AIENOS_GATE_TAG, AIENOS_QUIET_FLAG (read only), AIENOS_LOG_DIR (serial logs, +# Environment: AIENOS_QEMU_TIMEOUT (300 s per boot), AIENOS_GATE_LOCK, +# AIENOS_GATE_MINUTES, AIENOS_QUIET_FLAG (read only), AIENOS_LOG_DIR (serial logs, # screendumps, decoded rows and PNGs when ffmpeg exists), AIENOS_QEMU_VERBOSE, # AIENOS_FB_CHECK_TOOL (prebuilt checker, for a red run on an older commit). set -euo pipefail @@ -157,27 +157,21 @@ make -s -C native/kernel CROSS="${cross}" OUT="${out_neg}" AIENOS_COMMIT="${comm tool="$(build_tool "${out}")" # Machine-wide quiet flag: read only, never written here. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -if [[ -e "${quiet_flag}" ]]; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -# One QEMU gate at a time: exclusive create (set -C) of the gate lock. -gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}" -gate_tag="${AIENOS_GATE_TAG:-qemu_ck_screen_test $$}" -if ! ( set -C; echo "${gate_tag}" > "${gate_lock}" ) 2>/dev/null; then - echo "NOT_RUN QEMU gate lock ${gate_lock} is held: $(head -c 200 "${gate_lock}" 2>/dev/null || true)" +if ! gh_lock_take qemu_ck_screen_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -own_lock=1 -release_lock() { - if [[ "${own_lock}" == 1 ]]; then - own_lock=0 - if [[ -f "${gate_lock}" ]] && grep -qxF -- "${gate_tag}" "${gate_lock}"; then rm -f "${gate_lock}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_lock() { gh_lock_release; } work="$(mktemp -d)" cleanup() { rm -rf "${work}"; release_lock; } trap cleanup EXIT diff --git a/scripts/qemu_ck_smp_test.sh b/scripts/qemu_ck_smp_test.sh index f60386cf..2d1eab86 100755 --- a/scripts/qemu_ck_smp_test.sh +++ b/scripts/qemu_ck_smp_test.sh @@ -27,10 +27,10 @@ # no compile (make -n stops at parse time). Ends # AIENOS_CK_SMP_SELF_TEST: PASS|FAIL # Environment: AIENOS_CK_SMP_CPUS (default 4), AIENOS_QEMU_TIMEOUT (180 s), -# AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG (as scripts/qemu_ck_boot_test.sh), +# AIENOS_QUIET_FLAG (read only), AIENOS_GATE_LOCK / AIENOS_GATE_MINUTES, # AIENOS_LOG_DIR (copy of the serial log), AIENOS_QEMU_VERBOSE. -# Takes the machine quiet flag itself and prints NOT_RUN if another run holds -# it (exit 3). Needs qemu-system-aarch64 and AAVMF. +# Prints NOT_RUN (exit 3) while the quiet flag or the QEMU gate lock is held +# (scripts/lib_gate_hold.sh). Needs qemu-system-aarch64 and AAVMF. set -euo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -227,20 +227,16 @@ else fi # Machine quiet flag: one heavy run at a time on the Spark. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_smp_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take "qemu_ck_smp_test" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "${verdict_name}: NOT_RUN" exit 3 fi -own_flag=1 -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } work="$(mktemp -d)" cleanup() { rm -rf "${work}" diff --git a/scripts/qemu_ck_store_crash_test.sh b/scripts/qemu_ck_store_crash_test.sh index 40fe2398..e1f9fe96 100755 --- a/scripts/qemu_ck_store_crash_test.sh +++ b/scripts/qemu_ck_store_crash_test.sh @@ -77,7 +77,8 @@ # AIENOS_CK_STORE_CRASH_MUTANT_: KILLED (exit 0) when the # gate's checks FAIL on it, SURVIVED (exit 1) otherwise. Never # prints the AIENOS_CK_M4_STORE_CRASH line. -# Takes the machine quiet flag like qemu_ck_store_test.sh (not in --self-test). +# Reads the quiet flag and takes the QEMU gate lock like qemu_ck_store_test.sh +# (not in --self-test). # Final line (gate): AIENOS_CK_M4_STORE_CRASH: PASS|FAIL|NOT_RUN. # Exit: 0 PASS, 1 FAIL, 2 missing tools/build failure, 3 quiet flag held. set -uo pipefail @@ -428,20 +429,16 @@ if ! make -s -C native/kernel OUT="${out}" gpt-image >/dev/null; then echo "GPT image tool build failed"; verdict NOT_RUN; exit 2 fi -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_store_crash_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take "qemu_ck_store_crash_test" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" verdict NOT_RUN exit 3 fi -own_flag=1 -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } top="$(mktemp -d)" qemu_pid="" cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null; rm -rf "${top}"; release_flag; } diff --git a/scripts/qemu_ck_store_test.sh b/scripts/qemu_ck_store_test.sh index 4bfb5428..72c48152 100755 --- a/scripts/qemu_ck_store_test.sh +++ b/scripts/qemu_ck_store_test.sh @@ -82,9 +82,9 @@ # show the shutdown landing between the BME set and BME clear writes. Boot 5 # must show no shutdown. Every boot must show the ck_reset quiesce hook. # -# Takes the machine quiet flag itself like qemu_ck_boot_test.sh -# (AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG; released at most once, only if it -# still holds exactly this run's text). Any QEMU exit status other than 0 fails. +# Reads the machine quiet flag and takes the QEMU gate lock like +# qemu_ck_boot_test.sh (scripts/lib_gate_hold.sh; the lock is released at most +# once, only if it still holds exactly this run's record). Any QEMU exit status other than 0 fails. # Final lines: AIENOS_CK_M4_NVME, AIENOS_CK_M4_STORE, AIENOS_CK_ARGUS1_REVOKE, # AIENOS_CK_SMMU, AIENOS_CK_NVME_SHUTDOWN, each PASS|FAIL|NOT_RUN. A boot that # fails an M1 check fails all five. @@ -118,20 +118,16 @@ unsafe_efi="${out}/full-qemu-unsafe-dma/BOOTAA64.EFI" safe_efi="${out}/full/BOOTAA64.EFI" # Machine quiet flag: one heavy run at a time on the Spark. -quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" -quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_store_test $$}" -if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then - echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" +# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): +# scripts/lib_gate_hold.sh (aienos#278). +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check || ! gh_lock_take "qemu_ck_store_test" "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" verdicts NOT_RUN exit 3 fi -own_flag=1 -release_flag() { - if [[ "${own_flag}" == 1 ]]; then - own_flag=0 - if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi - fi -} +# Release only this run's own gate lock record, at most once. +release_flag() { gh_lock_release; } top="$(mktemp -d)" printf '%s\n' pci_nvme_mmio_shutdown_set pci_nvme_mmio_shutdown_cleared pci_cfg_write >"${top}/trace.events" cleanup() { diff --git a/scripts/qemu_native_nvme_test.sh b/scripts/qemu_native_nvme_test.sh index 757d704c..ad38819e 100755 --- a/scripts/qemu_native_nvme_test.sh +++ b/scripts/qemu_native_nvme_test.sh @@ -9,18 +9,19 @@ # Verdict line: AIENOS_STORE_NVME_QEMU: PASS | FAIL | NOT_RUN # QEMU is an emulator. PASS here qualifies nothing about physical hardware. # -# Respects the shared quiet flag: refuses to run (NOT_RUN) while -# ~/workspace/.spark-quiet exists; otherwise holds it for the run. +# Respects the shared quiet flag: refuses to run (NOT_RUN, exit 0) while +# ~/workspace/.spark-quiet exists, which it only reads, or while another gate +# holds the QEMU gate lock (scripts/lib_gate_hold.sh, aienos#278). set -euo pipefail repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" -quiet="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" qemu_timeout="${AIENOS_QEMU_TIMEOUT:-60}" out_rel="out" out="${repo}/native/disk/qemu/${out_rel}" +. "${repo}/scripts/lib_gate_hold.sh" -if [ -e "${quiet}" ]; then - echo "quiet flag ${quiet} is present ($(head -c 200 "${quiet}" 2>/dev/null || true)); another heavy run owns the machine" +if ! gh_quiet_check; then + echo "NOT_RUN ${gh_why}" echo "AIENOS_STORE_NVME_QEMU: NOT_RUN" exit 0 fi @@ -32,14 +33,14 @@ for tool in qemu-system-aarch64 timeout make; do fi done -# Take the flag atomically (noclobber): if another run created it since the -# check above, refuse instead of overwriting it, and never delete its flag. -if ! ( set -C; echo "lane11 qemu_native_nvme_test" > "${quiet}" ) 2>/dev/null; then - echo "quiet flag ${quiet} appeared during setup; another heavy run owns the machine" +# One QEMU gate at a time; the gate lock record is removed only if it is still +# this run's own (never another run's hold). +if ! gh_lock_take qemu_native_nvme_test "${AIENOS_GATE_MINUTES:-60}"; then + echo "NOT_RUN ${gh_why}" echo "AIENOS_STORE_NVME_QEMU: NOT_RUN" exit 0 fi -cleanup() { rm -f "${quiet}"; } +cleanup() { gh_lock_release; } trap cleanup EXIT cross=""