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/interplane-forge/blob/main/infra/spark/forged-counts.sh # interplane-forge INTERPLANE Forge 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 `interplane-forge / infra / spark / forged-counts.sh` 70 lines ยท 2831 bytes Raw Blame History 1 #!/usr/bin/env bash 2 # Owner-side supervisor adapter. No private input is mounted in forged. 3 counts_init() { 4 COUNTS_STATE=${FORGED_COUNTS_STATE:-$DATA/../forge-counts-state} 5 COUNTS_INTERVAL=${FORGED_COUNTS_INTERVAL:-900} 6 [[ $COUNTS_INTERVAL =~ ^[1-9][0-9]{0,3}$ ]] && ((COUNTS_INTERVAL >= 1 && COUNTS_INTERVAL <= 1800)) || return 2 7 [[ ! -L $COUNTS_STATE ]] || return 2 8 COUNTS_STATE=$(realpath -m "$COUNTS_STATE") || return 2 9 local data_real 10 data_real=$(realpath -e "$DATA") || return 2 11 case "$COUNTS_STATE/" in "$data_real/"*|"$PUBLIC/"*) return 2;; esac 12 case "$data_real/" in "$COUNTS_STATE/"*) return 2;; esac 13 case "$PUBLIC/" in "$COUNTS_STATE/"*) return 2;; esac 14 mkdir -m 700 -p -- "$COUNTS_STATE" || return 2 15 [[ ! -L $COUNTS_STATE/counts.log ]] || return 2 16 counts_bin_ok || return 2 17 counts_last=-999999 18 counts_set= 19 counts_pid= 20 } 21 # The collector runs as the owner, unsandboxed. Refuse a binary the sandboxed child could 22 # have replaced: one that resolves inside the writable data directory or the export. 23 counts_bin_ok() { 24 local data_real 25 data_real=$(realpath -e "$DATA") || return 2 26 COUNTS_BIN=$(realpath -e "$BIN") || { echo 'forged-counts: collector binary missing' >&2; return 2; } 27 case "$COUNTS_BIN" in "$data_real"/*|"$PUBLIC"/*) 28 echo "forged-counts: refusing collector binary inside writable data or export: $COUNTS_BIN" >&2 29 return 2;; esac 30 } 31 counts_tick() { 32 if [[ -n $counts_pid ]]; then 33 if kill -0 "$counts_pid" 2>/dev/null; then return; fi 34 wait "$counts_pid" || true 35 counts_pid= 36 fi 37 if ((SECONDS - counts_last < COUNTS_INTERVAL)) && [[ $counts_set = "$1" ]]; then return; fi 38 counts_last=$SECONDS 39 counts_set=$1 40 if ! counts_bin_ok >>"$LOG" 2>&1; then 41 rm -f -- "$PUBLIC/counts.snapshot" 42 return 43 fi 44 # Spawn independently so log scanning never stalls HTTP supervision or refresh. It closes every 45 # descriptor above 2 first, so it never holds the supervisor's listener or log pipe. 46 taskset -c "$CPUS" nice -n 10 bash -c ' 47 for fd in /proc/self/fd/*; do 48 fd=${fd##*/} 49 if [[ $fd =~ ^[0-9]+$ ]] && ((fd > 2)); then eval "exec $fd>&-"; fi 50 done 51 set -eu 52 tmp=$(mktemp "$1/.mirror.counts.XXXXXX") 53 trap '\''rm -f -- "$tmp"'\'' EXIT 54 if timeout --signal=TERM --kill-after=5 300 "$2" --counts-snapshot "$3/counts.log" > "$tmp"; then 55 mv -Tf -- "$tmp" "$1/counts.snapshot" 56 else 57 rm -f -- "$1/counts.snapshot" 58 echo "forged-counts: collection failed; counts unavailable" >&2 59 fi 60 ' _ "$PUBLIC" "$COUNTS_BIN" "$COUNTS_STATE" >>"$LOG" 2>&1 & 61 counts_pid=$! 62 } 63 counts_stop() { 64 if [[ -n ${counts_pid:-} ]]; then 65 # Stop only this worker and its timeout/collector descendants. 66 pkill -TERM -P "$counts_pid" 2>/dev/null || true 67 wait "$counts_pid" 2>/dev/null || true 68 counts_pid= 69 fi 70 }