[package] base-files: sysupgrade: kill all but essential processes before starting...
[openwrt/svn-archive/archive.git] / package / base-files / files / lib / upgrade / common.sh
index 65ce816db263adc7edee20e91ea4ad7defe5da7e..2126ca1c388d191a5b527495f73dcc5c809a6368 100644 (file)
@@ -77,6 +77,30 @@ run_ramfs() { # <command> [...]
        exec /bin/busybox ash -c "$*"
 }
 
+kill_remaining() { # [ <signal> ]
+       local sig="${1:-TERM}"
+       echo -n "Sending $sig to remaing processes ... "
+       top -bn1 | while read pid ppid user stat vsz pvsz pcpu cmd args; do
+               case "$pid" in
+                       [0-9]*) : ;;
+                       *) continue ;;
+               esac
+               case "$cmd" in
+                       # Skip kernel threads and essential services
+                       \[*\]|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*) : ;;
+
+                       # Killable process
+                       *)
+                               if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
+                                       echo -n "${cmd##*/} "
+                                       kill -$sig $pid 2>/dev/null
+                               fi
+                       ;;
+               esac
+       done
+       echo ""
+}
+
 run_hooks() {
        local arg="$1"; shift
        for func in "$@"; do