Hi,

here's the updated version of sendsigs script: better English, sed is not required anymore,
useless assignment is removed.

bye,

Przemek
#! /bin/sh
### BEGIN INIT INFO
# Provides:          sendsigs
# Required-Start:    
# Required-Stop:     umountnfs
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Kill all remaining processes except for crucial ones.
# Description: 
### END INIT INFO

PATH=/usr/sbin:/usr/bin:/sbin:/bin

. /lib/lsb/init-functions

#lists all ancestor pids, until pid number 0 is reached
getAncestorPids() {
        local pid=$1
        local ppids=

        while true; do
                ppid=$(ps --no-headers --format ppid $pid)
                if [ $ppid -eq 0 ]; then
                        break;
                fi
                ppids=$ppid" "$ppids
                pid=$ppid
        done
        echo $ppids
}


# Replacement for killall5 command. Just like killall5 does, it sends
# a signal to all processes except for: ancestor processes, itself
# and kernel threads.
# Addtionally, it skips all processes that had been run using command
# name passed as a parameter.
#
# arguments:
# signalnumber
# list of command names that should not be killed
killall5_except() {
        local signalNumber="$1"
        shift 
        local myPid=$$
        local preciousPids=$(ps --no-headers --format pid -C "$*")
        #initial precious pids are: my pid and pids of processes given as 
parameters
        preciousPids=$myPid" "$preciousPids
        local ancestorPids=

        for preciousPid in $preciousPids; do
                ancestorPids=$ancestorPids" "$(getAncestorPids $preciousPid)
        done

        #kernel threads should not be killed, either. Kernel threads have 
userspace size of 0.
        local kernelThreadPids=$(ps --format pid,size --no-headers -A |
                while read pid size; do 
                        [ $size -eq 0 ] && echo $pid
                done
        )

        allPreciousPids="$preciousPids $ancestorPids $kernelThreadPids"

        allPidsToTerminate=$(ps --deselect --no-headers --format pid --pid 
$allPreciousPids)
        
        #a trick to eliminate ps pid from the list
        allPidsToTerminate=$(ps --no-headers --format pid --pid 
$allPidsToTerminate)

        if [ ! -z "$allPidsToTerminate" ]; then
            kill $signalNumber $allPidsToTerminate
        fi
}

do_stop () {
        # Kill all processes.
        log_action_begin_msg "Asking all remaining processes to terminate"
        killall5_except -15 wpa_supplicant usplash
        log_action_end_msg 0
        sleep 5
        log_action_begin_msg "Killing all remaining processes"
        killall5_except -9 wpa_supplicant usplash
        log_action_end_msg 0
}

case "$1" in
  start)
        # No-op
        ;;
  restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
  stop)
        do_stop
        ;;
  *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

:

Reply via email to