interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / lib_gate_hold.sh

192 lines · 8397 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# shellcheck shell=bash
17# lib_gate_hold.sh: how a QEMU gate script treats the machine quiet flag and
18# the QEMU gate lock (aienos#278). Sourced; sets no shell options.
19#
20# The machine quiet flag (AIENOS_QUIET_FLAG, default ~/workspace/.spark-quiet)
21# belongs to quietlock: gates only READ it (no agent raises it without
22# Drake's approval; holds are taken with `quietlock hold`). A gate refuses
23# while the flag exists, except inside the caller's own quietlock hold (the
24# flag's hold=<id> equals $QUIETLOCK_HOLD, which `quietlock hold` exports to
25# its command).
26#
27# One QEMU gate at a time: the gate lock (AIENOS_GATE_LOCK, default
28# ~/workspace/.qemu-gate-lock) holds one record in the quietlock record format
29# aienos qemu-gate <name> start=<iso> expected_end=<iso> pid=<n> pidstart=<n> hold=<id>
30# (holder = the first three fields), so `quietlock check`-style readers see
31# the holder and whether it is alive. Taking, clearing and releasing run under
32# flock on "<lock>.mutex", and the record is created by exclusive create.
33#
34# Rules (the quietlock rules, gate side):
35# - a live holder is never removed;
36# - a record without a readable holder, pid= or expected_end= is HELD and is
37# never removed (nothing here can prove its holder dead);
38# - a dead holder's record is removed by the same gate (same <name>) at once,
39# or by any gate once its expected_end has passed (quietlock's stale rule);
40# - a gate removes only its own record at exit (exact line match).
41# A holder is dead when /proc/<pid> is gone, is a zombie, or started at a
42# different time than pidstart= records (pid reused).
43#
44# API: gh_quiet_check; gh_lock_take NAME MINUTES; gh_lock_release.
45# The first two return 0 (go) or 3 (refused; $gh_why says why and names the
46# holder). gh_lock_release is safe to call more than once (EXIT trap).
47
48gh_quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}"
49gh_gate_lock="${AIENOS_GATE_LOCK:-${HOME}/workspace/.qemu-gate-lock}"
50gh_why=""
51gh_line=""
52
53_gh_iso() { date -u -d "@$1" +%Y-%m-%dT%H:%M:%SZ; }
54
55# Start time (clock ticks since boot, /proc/<pid>/stat field 22) of a pid.
56_gh_pidstart() {
57 local s
58 s="$(cat "/proc/$1/stat" 2>/dev/null)" || return 1
59 s="${s##*) }"
60 # shellcheck disable=SC2086
61 set -- ${s}
62 [[ $# -ge 20 ]] || return 1
63 printf '%s\n' "${20}"
64}
65
66_gh_alive() { # pid [pidstart]
67 local s st
68 s="$(cat "/proc/$1/stat" 2>/dev/null)" || return 1
69 st="${s##*) }"; st="${st%% *}"
70 [[ "${st}" != Z ]] || return 1
71 [[ -z "${2:-}" || "$(_gh_pidstart "$1")" == "$2" ]]
72}
73
74# Parse the first line of a record file into _gh_holder _gh_name _gh_pid
75# _gh_pidstart _gh_end _gh_end_text _gh_hold. Return 0 when holder (three
76# plain fields), pid= and expected_end= are all readable, else 1.
77_gh_parse() {
78 local line tok n=0
79 _gh_holder="" _gh_name="" _gh_pid="" _gh_pidstart="" _gh_end="" _gh_end_text="" _gh_hold=""
80 IFS= read -r line <"$1" 2>/dev/null || [[ -n "${line:-}" ]] || return 1
81 _gh_first="${line:0:200}"
82 local -a toks
83 read -r -a toks <<<"${line}"
84 for tok in "${toks[@]}"; do
85 case "${tok}" in
86 pid=*) [[ -z "${_gh_pid}" ]] && _gh_pid="${tok#pid=}" ;;
87 pidstart=*) [[ -z "${_gh_pidstart}" ]] && _gh_pidstart="${tok#pidstart=}" ;;
88 expected_end=*) [[ -z "${_gh_end_text}" ]] && _gh_end_text="${tok#expected_end=}" ;;
89 hold=*) [[ -z "${_gh_hold}" ]] && _gh_hold="${tok#hold=}" ;;
90 *=*) ;;
91 *) if [[ ${n} -lt 3 ]]; then
92 n=$((n + 1)); _gh_holder="${_gh_holder:+${_gh_holder} }${tok}"; [[ ${n} == 3 ]] && _gh_name="${tok}"
93 fi ;;
94 esac
95 done
96 [[ ${n} == 3 && "${_gh_pid}" =~ ^[1-9][0-9]*$ ]] || return 1
97 [[ -n "${_gh_end_text}" ]] && _gh_end="$(date -u -d "${_gh_end_text}" +%s 2>/dev/null)" || return 1
98 [[ "${_gh_pidstart}" =~ ^[0-9]*$ ]] || _gh_pidstart=""
99 return 0
100}
101
102_gh_describe() { # file -> "holder='...' pid=... alive=... expected_end=..." or the raw line
103 if _gh_parse "$1"; then
104 local alive=no
105 _gh_alive "${_gh_pid}" "${_gh_pidstart}" && alive=yes
106 printf "holder='%s' pid=%s alive=%s expected_end=%s" "${_gh_holder}" "${_gh_pid}" "${alive}" "${_gh_end_text}"
107 else
108 printf "unreadable record, treated as held: %s" "${_gh_first:-?}"
109 fi
110}
111
112# 0 = the machine is not held for this gate; 3 = refused.
113gh_quiet_check() {
114 gh_why=""
115 [[ -e "${gh_quiet_flag}" ]] || return 0
116 if _gh_parse "${gh_quiet_flag}" && [[ -n "${_gh_hold}" && "${_gh_hold}" == "${QUIETLOCK_HOLD:-}" ]]; then
117 echo "gate_hold: running inside the caller's quietlock hold ${_gh_hold} (quiet flag left alone)" >&2
118 return 0
119 fi
120 gh_why="quiet flag ${gh_quiet_flag} is held ($(_gh_describe "${gh_quiet_flag}"))"
121 return 3
122}
123
124gh_lock_take() { # NAME MINUTES
125 local name="$1" minutes="$2" fd now line rc=0
126 gh_why=""
127 if [[ ! "${name}" =~ ^[A-Za-z0-9._-]{1,64}$ || ! "${minutes}" =~ ^[1-9][0-9]*$ ]]; then
128 gh_why="gate_hold: bad gate name or minutes ('${name}', '${minutes}')"
129 return 3
130 fi
131 if ! command -v flock >/dev/null 2>&1; then
132 gh_why="gate_hold: flock not found; refusing to take the QEMU gate lock"
133 return 3
134 fi
135 mkdir -p "$(dirname "${gh_gate_lock}")" 2>/dev/null || true
136 if ! exec {fd}>>"${gh_gate_lock}.mutex"; then
137 gh_why="QEMU gate lock mutex ${gh_gate_lock}.mutex cannot be opened"
138 return 3
139 fi
140 if ! flock -x -w 30 "${fd}"; then
141 exec {fd}>&-
142 gh_why="QEMU gate lock mutex ${gh_gate_lock}.mutex busy for 30 s"
143 return 3
144 fi
145 now="$(date +%s 2>/dev/null)"
146 if [[ ! "${now}" =~ ^[0-9]+$ ]] || ! _gh_iso "${now}" >/dev/null 2>&1; then
147 flock -u "${fd}"
148 exec {fd}>&-
149 gh_why="gate_hold: the clock (date) is unreadable; refusing to take the QEMU gate lock"
150 return 3
151 fi
152 if [[ -e "${gh_gate_lock}" ]]; then
153 if ! _gh_parse "${gh_gate_lock}"; then
154 gh_why="QEMU gate lock ${gh_gate_lock} is held (unreadable record, treated as held and never removed: ${_gh_first:-?})"
155 rc=3
156 elif _gh_alive "${_gh_pid}" "${_gh_pidstart}"; then
157 gh_why="QEMU gate lock ${gh_gate_lock} is held ($(_gh_describe "${gh_gate_lock}"))"
158 rc=3
159 elif [[ "${_gh_name}" == "${name}" || ${now} -gt ${_gh_end} ]]; then
160 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
161 rm -f "${gh_gate_lock}"
162 else
163 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"
164 rc=3
165 fi
166 fi
167 if [[ ${rc} == 0 ]]; then
168 line="aienos qemu-gate ${name} start=$(_gh_iso "${now}") expected_end=$(_gh_iso $((now + minutes * 60))) pid=$$ pidstart=$(_gh_pidstart $$) hold=gate-$$-${now}"
169 if ( set -C; printf '%s\n' "${line}" >"${gh_gate_lock}" ) 2>/dev/null; then
170 gh_line="${line}"
171 else
172 gh_why="QEMU gate lock ${gh_gate_lock} appeared while taking it ($(_gh_describe "${gh_gate_lock}"))"
173 rc=3
174 fi
175 fi
176 flock -u "${fd}"
177 exec {fd}>&-
178 return ${rc}
179}
180
181gh_lock_release() {
182 local fd cur
183 [[ -n "${gh_line}" ]] || return 0
184 if exec {fd}>>"${gh_gate_lock}.mutex" && flock -x -w 30 "${fd}"; then
185 IFS= read -r cur <"${gh_gate_lock}" 2>/dev/null || true
186 [[ "${cur:-}" == "${gh_line}" ]] && rm -f "${gh_gate_lock}"
187 flock -u "${fd}"
188 exec {fd}>&-
189 fi
190 gh_line=""
191 return 0
192}