interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / tools / gen-syscalls.sh

62 lines · 2995 bytes Raw Blame History
1#!/bin/sh
2# gen-syscalls.sh: regenerate om/syscalls.om, the Linux ARM64 syscall numbers of the systemd syscall
3# groups that service files name (syscalls @system-service @mount @ns, syscalls-deny @privileged
4# @resources). This is a data generator, not part of the supervisor: the oracles are the host's
5# `systemd-analyze syscall-filter` (group membership) and the kernel headers through `cpp`
6# (<asm/unistd.h>, ARM64 numbers). tests/run.sh runs it with --check and fails on any difference.
7# tools/gen-syscalls.sh rewrite om/syscalls.om
8# tools/gen-syscalls.sh --check compare only (exit 1 on a difference)
9set -eu
10here=$(cd "$(dirname "$0")/.." && pwd)
11out=$here/om/syscalls.om
12tmp=$(mktemp -d)
13trap 'rm -rf "$tmp"' EXIT
14
15[ "$(uname -m)" = aarch64 ] || { echo "gen-syscalls: ARM64 host required" >&2; exit 2; }
16
17# Kernel table: name -> number, aliases (__NR3264_*) resolved by the preprocessor.
18{ echo '#include <asm/unistd.h>'
19 echo '#include <asm/unistd.h>' | cpp -dM |
20 awk '$1=="#define" && $2 ~ /^__NR_/ {n=$2; sub(/^__NR_/,"",n); print "SYS " n " __NR_" n}'
21} | cpp -P | awk '$1=="SYS" && $3 ~ /^[0-9]+$/ {print $2, $3}' |
22 grep -v -E '^(syscalls|arch_specific_syscall) ' | sort -u > "$tmp/all"
23
24SYSTEMD_COLORS=0 systemd-analyze syscall-filter --no-pager 2>/dev/null | sed 's/\x1b\[[0-9;]*m//g' > "$tmp/groups"
25members() { awk -v G="$1" '/^@/ {g=$1; next} /^[[:space:]]*#/ || /^[[:space:]]*$/ {next} g==G {gsub(/[[:space:]]/,""); print}' "$tmp/groups"; }
26expand() {
27 todo="$*"; seen=""; : > "$tmp/acc"
28 while [ -n "$todo" ]; do
29 set -- $todo; x=$1; shift; todo="$*"
30 case " $seen " in *" $x "*) continue ;; esac
31 seen="$seen $x"
32 for w in $(members "$x"); do case $w in @*) todo="$todo $w" ;; *) echo "$w" >> "$tmp/acc" ;; esac; done
33 done
34 sort -u "$tmp/acc"
35}
36numbers() { awk 'NR==FNR {a[$1]=1; next} ($1 in a) {print $2}' "$1" "$tmp/all" | sort -n | paste -sd ' ' -; }
37
38expand @system-service > "$tmp/ss"
39expand @mount > "$tmp/mount"
40expand @privileged > "$tmp/priv"
41expand @resources > "$tmp/res"
42printf 'unshare\nsetns\n' > "$tmp/ns"
43[ -s "$tmp/ss" ] && [ "$(wc -l < "$tmp/ss")" -gt 300 ] || { echo "gen-syscalls: @system-service expansion looks wrong" >&2; exit 2; }
44
45{
46 echo "note GENERATED by tools/gen-syscalls.sh from systemd-analyze syscall-filter ($(systemd-analyze --version | head -1 | awk '{print $2}'))"
47 echo "note and <asm/unistd.h> (ARM64). Do not edit; rerun the generator. Numbers allowed per group."
48 echo "text sysss \"$(numbers "$tmp/ss")\""
49 echo "text sysmnt \"$(numbers "$tmp/mount")\""
50 echo "text sysns \"$(numbers "$tmp/ns")\""
51 echo "text syspriv \"$(numbers "$tmp/priv")\""
52 echo "text sysres \"$(numbers "$tmp/res")\""
53} > "$tmp/out"
54
55if [ "${1:-}" = --check ]; then
56 if cmp -s "$tmp/out" "$out"; then echo "SYSCALLS-TABLE PASS"; exit 0; fi
57 echo "SYSCALLS-TABLE FAIL: om/syscalls.om differs from the host oracle" >&2
58 diff "$out" "$tmp/out" >&2 || true
59 exit 1
60fi
61cp "$tmp/out" "$out"
62echo "wrote $out"