Publish a complete PID acknowledgement by private temporary file and same-directory rename. Preserve unknown acknowledgement artifacts and remove only retained owned stale state. Keep malformed acknowledgement failures closed. Cover deterministic publication barriers, shared apply/monitor/qualify launch, foreign artifacts, owned replacement, and private-directory checks. This does not establish hostile root or same-UID writer no-clobber guarantees; the private mode-700 directory is the trust boundary. Co-authored-by: GPT-6 <gpt-6@openai.com>
117 lines
4.1 KiB
Bash
117 lines
4.1 KiB
Bash
#!/bin/sh
|
|
# Shared owned-tree signalling; sourced by PID 1, executed afresh for watchdogs.
|
|
signal_tree() {
|
|
if [ -r "/proc/$1/task/$1/children" ]; then
|
|
descendants=
|
|
read -r descendants < "/proc/$1/task/$1/children" || true
|
|
for descendant in $descendants; do
|
|
signal_tree "$descendant" "$2"
|
|
done
|
|
fi
|
|
kill "-$2" "$1" 2>/dev/null || true
|
|
}
|
|
|
|
# /proc comm may contain spaces and closing parentheses: remove through the
|
|
# LAST ') ', then fields are state, PPID, PGID, SID (not whitespace-split comm).
|
|
proc_identity() {
|
|
proc_sid=
|
|
proc_state=
|
|
proc_line=
|
|
[ -r "/proc/$1/stat" ] || return 1
|
|
read -r proc_line < "/proc/$1/stat" || return 1
|
|
[ "${proc_line%% *}" = "$1" ] || return 1
|
|
proc_tail=${proc_line##*) }
|
|
[ "$proc_tail" != "$proc_line" ] || return 1
|
|
set -- $proc_tail
|
|
[ "$#" -ge 4 ] || return 1
|
|
proc_state=$1
|
|
proc_sid=$4
|
|
}
|
|
|
|
signal_session() {
|
|
case "$1" in ''|*[!0-9]*) return 1 ;; esac
|
|
[ "$1" -gt 1 ] && [ "$1" != "$$" ] || return 1
|
|
proc_identity "$$" || return 1
|
|
[ "$proc_sid" != "$1" ] || return 1
|
|
session_live=0
|
|
for session_stat in /proc/[0-9]*/stat; do
|
|
session_pid=${session_stat#/proc/}
|
|
session_pid=${session_pid%/stat}
|
|
if ! proc_identity "$session_pid"; then
|
|
# Disappearing processes are normal; an unreadable surviving stat
|
|
# is not evidence that the owned session is empty.
|
|
[ ! -e "$session_stat" ] || return 1
|
|
continue
|
|
fi
|
|
[ "$proc_sid" = "$1" ] || continue
|
|
[ "$session_pid" -gt 1 ] && [ "$session_pid" != "$$" ] || return 1
|
|
if [ "$proc_state" = Z ]; then
|
|
# PID 1 may have adopted this worker. Nonchildren return immediately;
|
|
# zombies never block. Shell SIGCHLD handling also reaps adoptees.
|
|
wait "$session_pid" 2>/dev/null || true
|
|
continue
|
|
fi
|
|
session_live=1
|
|
[ "$2" = CHECK ] || kill "-$2" "$session_pid" 2>/dev/null || true
|
|
done
|
|
}
|
|
|
|
if [ "${1-}" = --monitor ]; then
|
|
set -eu
|
|
[ "$#" -ge 3 ] || exit 64
|
|
config_dir=$2
|
|
shift 2
|
|
proc_identity "$$" || { echo 'controller-startup: wrapper=identity_unavailable' >&2; exit 78; }
|
|
[ "$proc_sid" = "$$" ] && [ "$$" -gt 1 ] || {
|
|
echo 'controller-startup: wrapper=not_session_leader' >&2; exit 78;
|
|
}
|
|
# Acknowledge the private session before exec, so even an immediate monitor
|
|
# crash leaves its ownership recorded. No worker can precede this handshake.
|
|
trap 'exit 143' INT TERM
|
|
ack_temp=
|
|
trap '[ -z "$ack_temp" ] || rm -f "$ack_temp"' EXIT
|
|
ack_publish() {
|
|
# Private same-UID directory: no untrusted writer can race the absent
|
|
# destination check. Rename exposes complete bytes with a single link.
|
|
[ -d "$config_dir" ] && [ ! -L "$config_dir" ] &&
|
|
[ "$(stat -c '%u:%a' "$config_dir")" = "$(id -u):700" ] || return 1
|
|
umask 077
|
|
ack_temp=$(mktemp "$config_dir/.monitor-ready.XXXXXX") || return 1
|
|
printf '%s\n' "$$" > "$ack_temp" || return 1
|
|
[ ! -e "$config_dir/monitor-ready" ] && [ ! -L "$config_dir/monitor-ready" ] || return 1
|
|
mv -T "$ack_temp" "$config_dir/monitor-ready" || return 1
|
|
ack_temp=
|
|
}
|
|
ack_publish || { echo 'controller-startup: wrapper=ack_publish_failed' >&2; exit 78; }
|
|
trap - EXIT
|
|
echo 'controller-startup: wrapper=exec_pending' >&2
|
|
exec "$@"
|
|
elif [ "${1-}" = --watchdog ]; then
|
|
set -eu
|
|
[ "$#" -eq 4 ] || exit 64
|
|
case "$2" in ''|*[!0-9]*) exit 64 ;; esac
|
|
case "$3" in ''|*[!0-9]*) exit 64 ;; esac
|
|
[ "$2" -gt 1 ] && [ "$3" -gt 0 ] || exit 64
|
|
child=$2
|
|
seconds=$3
|
|
config_dir=$4
|
|
sleeper=
|
|
watchdog_cleanup() {
|
|
trap - EXIT
|
|
trap ':' INT TERM
|
|
if [ -n "$sleeper" ]; then
|
|
kill "$sleeper" 2>/dev/null || true
|
|
wait "$sleeper" 2>/dev/null || true
|
|
fi
|
|
}
|
|
trap watchdog_cleanup EXIT
|
|
trap 'exit 0' INT TERM
|
|
sleep "$seconds" & sleeper=$!
|
|
: > "$config_dir/join-ready"
|
|
wait "$sleeper"
|
|
: > "$config_dir/join-timeout"
|
|
signal_tree "$child" KILL
|
|
elif [ "$#" -ne 0 ]; then
|
|
exit 64
|
|
fi
|