42 lines
1.1 KiB
Bash
42 lines
1.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
|
|
}
|
|
|
|
if [ "${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
|