buildKillallShim function

String buildKillallShim({
  1. required String realKillall,
  2. required String realPgrep,
})

The killall PATH shim: exact comm-name match via the real pgrep -x (killall semantics), same exclusion set as buildPkillShim. Any flag the shim does not model (-i, -q, -u, -o, -y, …) execs the real killall.

Implementation

String buildKillallShim({
  required String realKillall,
  required String realPgrep,
}) =>
    r'''#!/bin/sh
# fa bash-guard shim for killall (gh-1763): never signals the harness or
# its ancestor chain, the invoking shell, or itself.
set -u
FA_REAL_KILLALL='__FA_REAL_KILLALL__'
FA_REAL_PGREP='__FA_REAL_PGREP__'

sig=''
names=''
while [ $# -gt 0 ]; do
  case "$1" in
    --signal)
      if [ "$#" -ge 2 ]; then sig="$2"; shift 2
      else exec "$FA_REAL_KILLALL" "$@"; fi
      ;;
    --signal=*) sig="${1#--signal=}"; shift ;;
    -[0-9]*) sig="$1"; shift ;;
    -[Ss][Ii][Gg][A-Za-z]*|-[A-Z][A-Z]*) sig="$1"; shift ;;
    -*) exec "$FA_REAL_KILLALL" "$@" ;;
    *) names="$names $1"; shift ;;
  esac
done
[ -n "$names" ] || exec "$FA_REAL_KILLALL" "$@"
# --signal/--signal= store the NAME without the leading dash; normalize
# so `kill "$sig" <pid>` works for TERM, KILL and 9 alike.
case "$sig" in -*) ;; ?*) sig="-$sig" ;; esac

self=$$
parent="${PPID:-}"
out=0
for n in $names; do
  pids=$("$FA_REAL_PGREP" -x "$n" 2>/dev/null) || continue
  for p in $pids; do
    case " ${FA_PROTECTED_PIDS:-} " in *" $p "*) continue ;; esac
    if [ "$p" = "$parent" ] || [ "$p" = "$self" ]; then continue; fi
    if [ -n "$sig" ]; then
      kill "$sig" "$p" 2>/dev/null && out=1
    else
      kill -TERM "$p" 2>/dev/null && out=1
    fi
  done
done
[ "$out" -eq 1 ] && exit 0
exit 1
'''
        .replaceAll('__FA_REAL_KILLALL__', realKillall)
        .replaceAll('__FA_REAL_PGREP__', realPgrep);