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 / ck_imac_lab_services_test.sh

166 lines · 8285 bytes Raw Blame History
1#!/usr/bin/env bash
2# ck_imac_lab_services_test.sh -- host test for the pid ownership check in ck_imac_lab_services.sh.
3# The launcher is sourced with IMAC_LAB_STATE in a temp dir, make/cc/sudo/ss are shadowed by
4# functions, and the three daemons are replaced by a tiny C stub that only waits for a signal, so
5# nothing real is built, bound, started or stopped. The cases: an unrelated live pid in a pid file
6# is not ours (status says not running, stop leaves it alive); a started stub is recognised, start
7# again does not launch a second one, and a deleted binary does not matter; a changed start time or
8# boot id, a pid file from an older launcher older than the process, a dead pid, a pid whose program
9# has another name, and junk or zero in the pid file are all refused; an older launcher's pid file
10# for the real stub is still accepted; a foreign receiver on UDP 47031 is left alone.
11# Final line: AIENOS_IMAC_LAB_SERVICES: PASS|FAIL exit 0 / 1
12set -euo pipefail
13repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
14tag="AIENOS_IMAC_LAB_SERVICES"
15launcher="${CK_LAB_SERVICES:-${repo_root}/scripts/ck_imac_lab_services.sh}"
16W="$(mktemp -d)"
17extra=()
18trap 'for p in "${extra[@]}"; do kill "${p}" 2>/dev/null || true; done; for f in "${W}"/state*/*.pid; do [[ -e "${f}" ]] && read -r p <"${f}" && [[ "${p}" =~ ^[1-9][0-9]*$ ]] && kill "${p}" 2>/dev/null || true; done; rm -rf "${W}"' EXIT
19fail() { echo "FAIL $1"; echo "${tag}: FAIL"; exit 1; }
20pass() { echo "PASS $1"; }
21
22mkdir -p "${W}/stubsrc"
23printf '#include <unistd.h>\nint main(void) { for (;;) pause(); }\n' >"${W}/stubsrc/stub.c"
24command cc -O2 -Wall -Wextra -Werror -o "${W}/stubsrc/stub" "${W}/stubsrc/stub.c"
25
26export IMAC_LAB_STATE="${W}/state"
27# shellcheck source=ck_imac_lab_services.sh
28source "${launcher}"
29[[ "${state}" == "${W}/state" ]] || fail "launcher state is ${state}, not the temp dir"
30out="${W}/out"
31booter="${W}/NETLDR.EFI"
32mkdir -p "${out}/host"
33echo booter >"${booter}"
34for n in ck_imac_netboot ck_imac_report_listen ck_imac_bsdp; do cp "${W}/stubsrc/stub" "${out}/host/${n}"; done
35# Nothing is built, granted capabilities or looked up on the real machine.
36make() { :; }
37cc() { :; }
38sudo() { :; }
39ss_holder=""
40ss() { [[ -z "${ss_holder}" ]] || echo "${ss_holder}"; }
41
42gone() { ! proc_ticks "$1" >/dev/null; }
43wait_gone() { local i; for i in $(seq 50); do gone "$1" && return 0; sleep 0.1; done; return 1; }
44reset_state() { rm -f "${state}"/*.pid "${state}"/*.id; }
45
46unrelated="${W}/unrelated"
47cp /bin/sleep "${unrelated}"
48"${unrelated}" 300 &
49upid=$!
50extra+=("${upid}")
51
52# 1. an unrelated live process named in the pid files
53reset_state
54for s in serve listen bsdp; do echo "${upid}" >"${state}/${s}.pid"; done
55for s in serve listen bsdp; do alive "${s}" && fail "unrelated pid accepted as ${s}"; done
56pass "unrelated live pid is not treated as owned"
57out_status="$(status)"
58[[ "$(grep -c ': not running' <<<"${out_status}")" == 3 ]] || fail "status shows unrelated pid as running: ${out_status}"
59pass "status rejects the unrelated pid"
60stop >"${W}/stop.out"
61kill -0 "${upid}" 2>/dev/null && ! gone "${upid}" || fail "stop killed the unrelated process"
62grep -q 'left alone' "${W}/stop.out" || fail "stop did not say it left the pid alone"
63[[ ! -e "${state}/serve.pid" ]] || fail "stale pid file kept"
64pass "stop leaves the unrelated process alive and drops the stale pid file"
65
66# 1b. stale pid files left from before a reboot cannot make start skip a daemon: serve names a live
67# process of the right program name whose .id is from another boot, listen and bsdp name the
68# unrelated process. start must launch all three and leave both foreign processes alone.
69reset_state
70mkdir -p "${W}/other"
71cp "${W}/stubsrc/stub" "${W}/other/ck_imac_netboot"
72"${W}/other/ck_imac_netboot" &
73ipid=$!
74extra+=("${ipid}")
75until [[ "$(tr '\0' ' ' <"/proc/${ipid}/cmdline")" == "${W}/other/ck_imac_netboot "* ]]; do sleep 0.05; done # exec done
76echo "${ipid}" >"${state}/serve.pid"
77echo "00000000-0000-0000-0000-000000000000 $(proc_ticks "${ipid}")" >"${state}/serve.id"
78for s in listen bsdp; do echo "${upid}" >"${state}/${s}.pid"; done
79start >"${W}/start0.out"
80grep -q 'already running' "${W}/start0.out" && fail "start skipped a daemon on a stale pid file: $(grep 'already running' "${W}/start0.out")"
81for s in serve listen bsdp; do
82 p="$(pid_of "${s}")"
83 [[ "${p}" != "${ipid}" && "${p}" != "${upid}" ]] || fail "start kept the stale pid ${p} for ${s}"
84 alive "${s}" || fail "start did not leave an owned ${s} running"
85done
86stop >/dev/null
87gone "${ipid}" && fail "stop killed the same-named process from another boot"
88gone "${upid}" && fail "stop killed the unrelated process"
89pass "stale pid files cannot make start skip a daemon, and the foreign processes survive start and stop"
90
91# 2. a started set of daemons is recognised, a second start adds nothing, a deleted binary still counts
92start >"${W}/start1.out"
93declare -A pid1
94for s in serve listen bsdp; do
95 [[ -s "${state}/${s}.pid" && -s "${state}/${s}.id" ]] || fail "start left no pid/id for ${s}"
96 pid1[${s}]="$(pid_of "${s}")"
97 alive "${s}" || fail "started ${s} (pid ${pid1[${s}]}) not recognised"
98done
99pass "started daemons are recognised"
100start >"${W}/start2.out"
101[[ "$(grep -c 'already running' "${W}/start2.out")" == 3 ]] || fail "second start did not report all three running"
102for s in serve listen bsdp; do [[ "$(pid_of "${s}")" == "${pid1[${s}]}" ]] || fail "second start replaced ${s}"; done
103pass "second start launches no competing instance"
104rm -f "${out}/host/ck_imac_bsdp"
105alive bsdp || fail "deleted (replaced) binary no longer recognised"
106pass "a replaced binary still counts as the owned daemon"
107
108# 3. changed identity is refused, restoring it is accepted
109good="$(<"${state}/serve.id")"
110read -r gboot gticks <<<"${good}"
111echo "${gboot} $((gticks + 1))" >"${state}/serve.id"
112alive serve && fail "changed start time accepted"
113echo "00000000-0000-0000-0000-000000000000 ${gticks}" >"${state}/serve.id"
114alive serve && fail "changed boot id accepted"
115echo "${good}" >"${state}/serve.id"
116alive serve || fail "restored identity refused"
117pass "changed start time or boot id is refused, the valid identity is accepted"
118
119# 4. a pid file from an older launcher (no .id)
120rm -f "${state}/listen.id"
121alive listen || fail "older launcher's pid file for the live daemon refused"
122touch -d '1 hour ago' "${state}/listen.pid"
123alive listen && fail "older launcher's pid file accepted for a process that started after it"
124touch "${state}/listen.pid"
125alive listen || fail "fresh older-style pid file refused"
126pass "older one-integer pid files: live daemon accepted, later-started process refused"
127
128# 5. a process with another program name, a dead pid and junk
129echo "${pid1[bsdp]}" >"${state}/serve.pid"
130rm -f "${state}/serve.id"
131alive serve && fail "bsdp process accepted as serve"
132echo "${pid1[serve]}" >"${state}/serve.pid"
133echo "${good}" >"${state}/serve.id"
134cp "${W}/stubsrc/stub" "${W}/ck_imac_bsdp"
135"${W}/ck_imac_bsdp" &
136dpid=$!
137until [[ "$(tr '\0' ' ' <"/proc/${dpid}/cmdline")" == "${W}/ck_imac_bsdp "* ]]; do sleep 0.05; done # exec done
138kill "${dpid}"; wait "${dpid}" 2>/dev/null || true
139echo "${dpid}" >"${state}/bsdp.pid"
140alive bsdp && fail "dead pid accepted"
141for junk in "" 0 -1 1 12x "1 2" "${upid}"; do
142 echo "${junk}" >"${state}/bsdp.pid"
143 alive bsdp && fail "junk pid file '${junk}' accepted"
144done
145pass "another program name, a dead pid and junk pid files are refused"
146
147# 6. stop ends only the owned daemons
148echo "${pid1[bsdp]}" >"${state}/bsdp.pid"
149echo "${good}" >"${state}/serve.id"
150stop >/dev/null
151for s in serve listen bsdp; do wait_gone "${pid1[${s}]}" || fail "stop left ${s} running"; done
152kill -0 "${upid}" 2>/dev/null && ! gone "${upid}" || fail "stop killed the unrelated process"
153pass "stop ends the owned daemons and nothing else"
154
155# 7. a foreign receiver on UDP 47031 is left as is
156reset_state
157ss_holder='users:(("other",pid=1,fd=3))'
158cp "${W}/stubsrc/stub" "${out}/host/ck_imac_bsdp"
159start >"${W}/start3.out"
160grep -q 'another receiver already holds' "${W}/start3.out" || fail "foreign receiver not detected"
161[[ ! -e "${state}/listen.pid" ]] || fail "listen started next to a foreign receiver"
162pass "a foreign receiver on UDP 47031 is left alone"
163ss_holder=""
164stop >/dev/null
165
166echo "${tag}: PASS"