aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / ck_imac_lab_services.sh
| 1 | #!/usr/bin/env bash |
| 2 | # ck_imac_lab_services.sh -- keep the iMac lab services running on the Spark: |
| 3 | # ck_imac_netboot serve answers NETLDR.EFI on the iMac (UDP 47032) with queued kernels |
| 4 | # ck_imac_report_listen collects the lab kernel's UNAUTHENTICATED report (UDP 47031) |
| 5 | # ck_imac_bsdp answers the iMac's Apple NetBoot (N key held, UDP 67) and hands it |
| 6 | # NETLDR.EFI over TFTP (UDP 69): the iMac starts with no stick at all |
| 7 | # Detached with setsid/nohup (no systemd). For a restart after a Spark reboot, a crontab line: |
| 8 | # @reboot <repo>/scripts/ck_imac_lab_services.sh start |
| 9 | # |
| 10 | # scripts/ck_imac_lab_services.sh start|stop|status |
| 11 | # |
| 12 | # State (spool, logs, reports, pids) lives in ${IMAC_LAB_STATE:-~/.local/state/aienos/imac-netboot}: |
| 13 | # next.job (queued by scripts/ck_imac_netboot_push.sh), serve.log, reports/listen.log. |
| 14 | # Binaries are built into target/native-kernel-imac-netboot. IMAC_LAB_BCAST (default |
| 15 | # 192.168.1.255) is where server answers go; IMAC_LAB_MAC limits answers to one loader. |
| 16 | # |
| 17 | # Ownership: a pid file is one integer, and after a reboot or a long uptime that number can name an |
| 18 | # unrelated process. A pid counts as ours only when /proc says the live process is that daemon: |
| 19 | # its argv[0] names the expected program (a rebuilt or deleted binary does not matter), its state is |
| 20 | # not zombie, and its start time matches <name>.id (boot id + start ticks, written by start). A pid |
| 21 | # file without <name>.id (written by an older launcher) is accepted only when the process cannot |
| 22 | # have started after the pid file was written. Anything else is "not running" and is never killed. |
| 23 | set -euo pipefail |
| 24 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 25 | state="${IMAC_LAB_STATE:-${HOME}/.local/state/aienos/imac-netboot}" |
| 26 | out="${repo_root}/target/native-kernel-imac-netboot" |
| 27 | bcast="${IMAC_LAB_BCAST:-192.168.1.255}" |
| 28 | imac_mac="${IMAC_LAB_IMAC_MAC:-78:7b:8a:bd:d0:51}" |
| 29 | server_ip="${IMAC_LAB_SERVER_IP:-192.168.1.108}" |
| 30 | router="${IMAC_LAB_ROUTER:-192.168.1.1}" |
| 31 | # This LAN filters UDP 67 -> 68; the iMac accepts BSDP answers from 1067. |
| 32 | reply_source_port="${IMAC_LAB_BSDP_REPLY_SOURCE_PORT:-1067}" |
| 33 | booter="${IMAC_LAB_BOOTER:-${out}/NETLDR.EFI}" |
| 34 | mkdir -p "${state}/reports" |
| 35 | |
| 36 | pid_of() { local p=""; [[ -r "${state}/$1.pid" ]] && read -r p <"${state}/$1.pid" || true; echo "${p}"; } |
| 37 | boot_id() { cat /proc/sys/kernel/random/boot_id; } |
| 38 | daemon_prog() { case "$1" in serve) echo ck_imac_netboot ;; listen) echo ck_imac_report_listen ;; bsdp) echo ck_imac_bsdp ;; esac; } |
| 39 | |
| 40 | # Start time of a live, non-zombie pid in clock ticks after boot (/proc/PID/stat field 22); fails if none. |
| 41 | proc_ticks() { |
| 42 | local stat rest |
| 43 | { read -r stat <"/proc/$1/stat"; } 2>/dev/null || return 1 |
| 44 | rest="${stat##*) }" # the command name may hold spaces and parens; fields resume after the last ") " |
| 45 | set -- ${rest} |
| 46 | [[ "$1" != Z && "$1" != X ]] || return 1 |
| 47 | echo "${20}" |
| 48 | } |
| 49 | |
| 50 | # owned_pid NAME PID: is PID the live daemon this launcher started for NAME? |
| 51 | owned_pid() { |
| 52 | local s="$1" pid="$2" ticks argv0="" id_boot="" id_ticks="" btime="" k v hz mtime |
| 53 | [[ "${pid}" =~ ^[1-9][0-9]*$ ]] || return 1 |
| 54 | ticks="$(proc_ticks "${pid}")" || return 1 |
| 55 | { IFS= read -r -d '' argv0 <"/proc/${pid}/cmdline"; } 2>/dev/null || true |
| 56 | [[ "${argv0##*/}" == "$(daemon_prog "${s}")" ]] || return 1 |
| 57 | if [[ -r "${state}/${s}.id" ]]; then |
| 58 | read -r id_boot id_ticks <"${state}/${s}.id" || true |
| 59 | [[ "${id_boot}" == "$(boot_id)" && "${id_ticks}" == "${ticks}" ]] |
| 60 | return |
| 61 | fi |
| 62 | # Pid file from an older launcher: the daemon is forked just before its pid is written, so a |
| 63 | # process that started later than that (10 s of clock slack) is a reused pid, not our daemon. |
| 64 | while read -r k v; do [[ "${k}" != btime ]] || btime="${v}"; done </proc/stat |
| 65 | hz="$(getconf CLK_TCK)" |
| 66 | mtime="$(stat -c %Y "${state}/${s}.pid")" |
| 67 | (( btime + ticks / hz <= mtime + 10 )) |
| 68 | } |
| 69 | |
| 70 | alive() { owned_pid "$1" "$(pid_of "$1")"; } |
| 71 | |
| 72 | # record_owner NAME PID: remember which process this launcher started, so a reused pid is told apart later. |
| 73 | record_owner() { |
| 74 | local ticks |
| 75 | rm -f "${state}/$1.id" |
| 76 | if ticks="$(proc_ticks "$2")"; then echo "$(boot_id) ${ticks}" >"${state}/$1.id"; fi |
| 77 | } |
| 78 | |
| 79 | start() { |
| 80 | make -s -C "${repo_root}/native/kernel" OUT="${out}" imac-netboot-tool >/dev/null |
| 81 | mkdir -p "${out}/host" |
| 82 | cc -O2 -Wall -Wextra -o "${out}/host/ck_imac_report_listen" \ |
| 83 | "${repo_root}/native/kernel/tools/ck_imac_report_listen.c" |
| 84 | if alive serve; then echo "serve: already running (pid $(pid_of serve))"; else |
| 85 | local mac=() |
| 86 | [[ -z "${IMAC_LAB_MAC:-}" ]] || mac=(--mac "${IMAC_LAB_MAC}") |
| 87 | setsid nohup "${out}/host/ck_imac_netboot" serve "${state}" --bcast "${bcast}" "${mac[@]}" \ |
| 88 | >>"${state}/serve.log" 2>&1 </dev/null & |
| 89 | echo $! >"${state}/serve.pid" |
| 90 | record_owner serve $! |
| 91 | echo "serve: started (pid $!, log ${state}/serve.log)" |
| 92 | fi |
| 93 | if alive listen; then echo "listen: already running (pid $(pid_of listen))" |
| 94 | elif ss -Hulpn 'sport = :47031' | grep -q .; then |
| 95 | echo "listen: another receiver already holds UDP 47031, left as is: $(ss -Hulpn 'sport = :47031' | grep -o 'users:.*')" |
| 96 | else |
| 97 | # 30 days is the listener's longest deadline; the @reboot line or a rerun starts the next one. |
| 98 | setsid nohup "${out}/host/ck_imac_report_listen" 47031 "${state}/reports" 2592000 \ |
| 99 | >>"${state}/reports/listen.out" 2>&1 </dev/null & |
| 100 | echo $! >"${state}/listen.pid" |
| 101 | record_owner listen $! |
| 102 | echo "listen: started (pid $!, log ${state}/reports/listen.log)" |
| 103 | fi |
| 104 | if alive bsdp; then echo "bsdp: already running (pid $(pid_of bsdp))"; else |
| 105 | # The booter is copied so a rebuild cannot change it under a running transfer; ports 67/69 |
| 106 | # are below 1024, so the binary gets CAP_NET_BIND_SERVICE (never run as root). |
| 107 | install -m 0644 "${booter}" "${state}/netldr.efi" |
| 108 | sudo -n setcap cap_net_bind_service=+ep "${out}/host/ck_imac_bsdp" |
| 109 | setsid nohup "${out}/host/ck_imac_bsdp" serve --booter "${state}/netldr.efi" --server-ip "${server_ip}" \ |
| 110 | --mac "${imac_mac}" --router "${router}" --also-broadcast yes --reply-source-port "${reply_source_port}" >>"${state}/bsdp.log" 2>&1 </dev/null & |
| 111 | echo $! >"${state}/bsdp.pid" |
| 112 | record_owner bsdp $! |
| 113 | echo "bsdp: started (pid $!, log ${state}/bsdp.log, booter sha256 $(sha256sum "${state}/netldr.efi" | cut -c1-16))" |
| 114 | fi |
| 115 | } |
| 116 | |
| 117 | stop() { |
| 118 | for s in serve listen bsdp; do |
| 119 | if alive "${s}"; then kill "$(pid_of "${s}")"; echo "${s}: stopped" |
| 120 | elif [[ -e "${state}/${s}.pid" ]]; then echo "${s}: not running (stale pid file dropped, that pid is not our daemon and was left alone)" |
| 121 | else echo "${s}: not running"; fi |
| 122 | rm -f "${state}/${s}.pid" "${state}/${s}.id" |
| 123 | done |
| 124 | } |
| 125 | |
| 126 | status() { |
| 127 | for s in serve listen bsdp; do |
| 128 | if alive "${s}"; then echo "${s}: running (pid $(pid_of "${s}"))"; else echo "${s}: not running"; fi |
| 129 | done |
| 130 | echo "UDP 47031 (reports): $(ss -Hulpn 'sport = :47031' | grep -o 'users:.*' || echo 'nobody')" |
| 131 | ls "${state}" | grep -E '\.job$' || echo "jobs: none" |
| 132 | } |
| 133 | |
| 134 | # Sourced by scripts/ck_imac_lab_services_test.sh, which calls the functions above directly. |
| 135 | if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 136 | case "${1:-}" in |
| 137 | start) start ;; |
| 138 | stop) stop ;; |
| 139 | status) status ;; |
| 140 | *) echo "usage: ck_imac_lab_services.sh start|stop|status"; exit 2 ;; |
| 141 | esac |
| 142 | fi |