#!/bin/sh
# Root-owned, fixed command invoked through polkit. No user command arguments.
set -eu
PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH
[ "$#" -eq 0 ] || exit 64
[ "$(id -u)" -eq 0 ] || exit 77
umask 077
exec 9>/run/overwall-vpn.lock
flock -n 9 || exit 75
helper=/usr/lib/overwall/overwall-vpn
child=
cleanup() {
 trap - EXIT HUP INT TERM
 if [ -n "$child" ] && kill -0 "$child" 2>/dev/null; then
  kill -TERM "$child" 2>/dev/null || true
  wait "$child" 2>/dev/null || true
 fi
 "$helper" --linux-cleanup >/dev/null || exit 1
}
trap cleanup EXIT
trap 'exit 143' HUP INT TERM
# Explicit fd inheritance: asynchronous shell jobs otherwise get /dev/null stdin.
exec 3<&0
"$helper" <&3 &
child=$!
result=0
wait "$child" || result=$?
child=
exit "$result"
