Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/scripts/ck_imac_lab_services_test.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 12 set -euo pipefail 13 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 14 tag="AIENOS_IMAC_LAB_SERVICES" 15 launcher="${CK_LAB_SERVICES:-${repo_root}/scripts/ck_imac_lab_services.sh}" 16 W="$(mktemp -d)" 17 extra=() 18 trap '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 19 fail() { echo "FAIL $1"; echo "${tag}: FAIL"; exit 1; } 20 pass() { echo "PASS $1"; } 21 22 mkdir -p "${W}/stubsrc" 23 printf '#include \nint main(void) { for (;;) pause(); }\n' >"${W}/stubsrc/stub.c" 24 command cc -O2 -Wall -Wextra -Werror -o "${W}/stubsrc/stub" "${W}/stubsrc/stub.c" 25 26 export IMAC_LAB_STATE="${W}/state" 27 # shellcheck source=ck_imac_lab_services.sh 28 source "${launcher}" 29 [[ "${state}" == "${W}/state" ]] || fail "launcher state is ${state}, not the temp dir" 30 out="${W}/out" 31 booter="${W}/NETLDR.EFI" 32 mkdir -p "${out}/host" 33 echo booter >"${booter}" 34 for 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. 36 make() { :; } 37 cc() { :; } 38 sudo() { :; } 39 ss_holder="" 40 ss() { [[ -z "${ss_holder}" ]] || echo "${ss_holder}"; } 41 42 gone() { ! proc_ticks "$1" >/dev/null; } 43 wait_gone() { local i; for i in $(seq 50); do gone "$1" && return 0; sleep 0.1; done; return 1; } 44 reset_state() { rm -f "${state}"/*.pid "${state}"/*.id; } 45 46 unrelated="${W}/unrelated" 47 cp /bin/sleep "${unrelated}" 48 "${unrelated}" 300 & 49 upid=$! 50 extra+=("${upid}") 51 52 # 1. an unrelated live process named in the pid files 53 reset_state 54 for s in serve listen bsdp; do echo "${upid}" >"${state}/${s}.pid"; done 55 for s in serve listen bsdp; do alive "${s}" && fail "unrelated pid accepted as ${s}"; done 56 pass "unrelated live pid is not treated as owned" 57 out_status="$(status)" 58 [[ "$(grep -c ': not running' <<<"${out_status}")" == 3 ]] || fail "status shows unrelated pid as running: ${out_status}" 59 pass "status rejects the unrelated pid" 60 stop >"${W}/stop.out" 61 kill -0 "${upid}" 2>/dev/null && ! gone "${upid}" || fail "stop killed the unrelated process" 62 grep -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" 64 pass "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. 69 reset_state 70 mkdir -p "${W}/other" 71 cp "${W}/stubsrc/stub" "${W}/other/ck_imac_netboot" 72 "${W}/other/ck_imac_netboot" & 73 ipid=$! 74 extra+=("${ipid}") 75 until [[ "$(tr '\0' ' ' <"/proc/${ipid}/cmdline")" == "${W}/other/ck_imac_netboot "* ]]; do sleep 0.05; done # exec done 76 echo "${ipid}" >"${state}/serve.pid" 77 echo "`00000000`-0000-0000-0000-`000000000000` $(proc_ticks "${ipid}")" >"${state}/serve.id" 78 for s in listen bsdp; do echo "${upid}" >"${state}/${s}.pid"; done 79 start >"${W}/start0.out" 80 grep -q 'already running' "${W}/start0.out" && fail "start skipped a daemon on a stale pid file: $(grep 'already running' "${W}/start0.out")" 81 for 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" 85 done 86 stop >/dev/null 87 gone "${ipid}" && fail "stop killed the same-named process from another boot" 88 gone "${upid}" && fail "stop killed the unrelated process" 89 pass "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 92 start >"${W}/start1.out" 93 declare -A pid1 94 for 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" 98 done 99 pass "started daemons are recognised" 100 start >"${W}/start2.out" 101 [[ "$(grep -c 'already running' "${W}/start2.out")" == 3 ]] || fail "second start did not report all three running" 102 for s in serve listen bsdp; do [[ "$(pid_of "${s}")" == "${pid1[${s}]}" ]] || fail "second start replaced ${s}"; done 103 pass "second start launches no competing instance" 104 rm -f "${out}/host/ck_imac_bsdp" 105 alive bsdp || fail "deleted (replaced) binary no longer recognised" 106 pass "a replaced binary still counts as the owned daemon" 107 108 # 3. changed identity is refused, restoring it is accepted 109 good="$(<"${state}/serve.id")" 110 read -r gboot gticks <<<"${good}" 111 echo "${gboot} $((gticks + 1))" >"${state}/serve.id" 112 alive serve && fail "changed start time accepted" 113 echo "`00000000`-0000-0000-0000-`000000000000` ${gticks}" >"${state}/serve.id" 114 alive serve && fail "changed boot id accepted" 115 echo "${good}" >"${state}/serve.id" 116 alive serve || fail "restored identity refused" 117 pass "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) 120 rm -f "${state}/listen.id" 121 alive listen || fail "older launcher's pid file for the live daemon refused" 122 touch -d '1 hour ago' "${state}/listen.pid" 123 alive listen && fail "older launcher's pid file accepted for a process that started after it" 124 touch "${state}/listen.pid" 125 alive listen || fail "fresh older-style pid file refused" 126 pass "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 129 echo "${pid1[bsdp]}" >"${state}/serve.pid" 130 rm -f "${state}/serve.id" 131 alive serve && fail "bsdp process accepted as serve" 132 echo "${pid1[serve]}" >"${state}/serve.pid" 133 echo "${good}" >"${state}/serve.id" 134 cp "${W}/stubsrc/stub" "${W}/ck_imac_bsdp" 135 "${W}/ck_imac_bsdp" & 136 dpid=$! 137 until [[ "$(tr '\0' ' ' <"/proc/${dpid}/cmdline")" == "${W}/ck_imac_bsdp "* ]]; do sleep 0.05; done # exec done 138 kill "${dpid}"; wait "${dpid}" 2>/dev/null || true 139 echo "${dpid}" >"${state}/bsdp.pid" 140 alive bsdp && fail "dead pid accepted" 141 for 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" 144 done 145 pass "another program name, a dead pid and junk pid files are refused" 146 147 # 6. stop ends only the owned daemons 148 echo "${pid1[bsdp]}" >"${state}/bsdp.pid" 149 echo "${good}" >"${state}/serve.id" 150 stop >/dev/null 151 for s in serve listen bsdp; do wait_gone "${pid1[${s}]}" || fail "stop left ${s} running"; done 152 kill -0 "${upid}" 2>/dev/null && ! gone "${upid}" || fail "stop killed the unrelated process" 153 pass "stop ends the owned daemons and nothing else" 154 155 # 7. a foreign receiver on UDP 47031 is left as is 156 reset_state 157 ss_holder='users:(("other",pid=1,fd=3))' 158 cp "${W}/stubsrc/stub" "${out}/host/ck_imac_bsdp" 159 start >"${W}/start3.out" 160 grep -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" 162 pass "a foreign receiver on UDP 47031 is left alone" 163 ss_holder="" 164 stop >/dev/null 165 166 echo "${tag}: PASS"