aredn/files/usr/local/bin/upgrade_kill_prep

60 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Code in this script is derived from OpenWRT
# GPLv2 License
kill_remaining() { # [ <signal> ]
local sig="${1:-TERM}"
printf "Sending %s to remaining processes ... " "$sig"
local my_pid=$$
local my_ppid
my_ppid=$(cut -d' ' -f4 /proc/$my_pid/stat)
local my_ppisupgraded=
grep -q upgraded "/proc/$my_ppid/cmdline" >/dev/null && {
local my_ppisupgraded=1
}
local stat
for stat in /proc/[0-9]*/stat; do
[ -f "$stat" ] || continue
local pid name _ ppid rest
read pid name _ ppid rest < "$stat"
name="${name#(}"; name="${name%)}"
local cmdline
read cmdline < "/proc/$pid/cmdline"
# Skip kernel threads
[ -n "$cmdline" ] || continue
if [ $$ -eq 1 ] || [ "$my_ppid" -eq 1 ] && [ -n "$my_ppisupgraded" ]; then
# Running as init process, kill everything except me
if [ "$pid" -ne $$ ] && [ "$pid" -ne "$my_ppid" ]; then
printf "%s " "$name"
kill "-$sig" "$pid" 2>/dev/null
fi
else
case "$name" in
# Skip essential services
*procd*|*ash*|*init*|*ssh*|*dropbear*|*login*|*uhttpd*|*admin*|*sysupgrade*|*upgrade_kill_prep*|*olsr*|*ubusd*) : ;;
# Killable process
*)
if [ "$pid" -ne $$ ] && [ "$ppid" -ne $$ ]; then
printf "%s " "$name"
kill "-$sig" "$pid" 2>/dev/null
fi
;;
esac
fi
done
echo ""
}
kill_remaining TERM
sleep 3
kill_remaining KILL
sleep 1