2015-08-26 11:44:08 +00:00
|
|
|
#!@BASH_PATH@
|
2014-10-18 13:50:53 +00:00
|
|
|
# A simple reimplementation of killall5 from sysvinit-tools
|
|
|
|
|
|
|
|
get_my_tree() {
|
|
|
|
local my_pid
|
|
|
|
read my_pid garbage < "/proc/self/stat"
|
|
|
|
child="$my_pid"
|
|
|
|
|
|
|
|
while [[ "$parent" != 1 ]]; do
|
|
|
|
read a b c parent d < "/proc/$child/stat";
|
|
|
|
child="$parent"
|
|
|
|
echo "$parent"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
get_all_procs() {
|
|
|
|
local processlist
|
|
|
|
|
|
|
|
for i in /proc/*/exe; do
|
|
|
|
process_dir="${i%/*}"
|
|
|
|
process_pid="${process_dir##*/}"
|
|
|
|
|
|
|
|
readlink -e "$i" &>/dev/null && {
|
|
|
|
echo "$process_pid"
|
|
|
|
}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
get_procs_to_kill() {
|
|
|
|
for i in "${all_procs[@]}"; do
|
|
|
|
unset skip
|
|
|
|
for p in "${my_tree[@]}"; do
|
|
|
|
[[ "$i" == "$p" ]] && { skip=1; }
|
|
|
|
done
|
|
|
|
|
|
|
|
[[ "$skip" ]] || { echo "$i"; }
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-03-29 12:40:14 +00:00
|
|
|
pids_exist() {
|
|
|
|
for i in "$@"; do
|
2016-03-29 16:21:53 +00:00
|
|
|
if [[ -d "/proc/$i" ]]; then
|
2016-03-29 12:40:14 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
2016-03-29 13:03:44 +00:00
|
|
|
|
|
|
|
return 1
|
2016-03-29 12:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_wait() {
|
|
|
|
declare counter
|
|
|
|
|
|
|
|
while pids_exist "$@"; do
|
2016-03-29 12:48:51 +00:00
|
|
|
if (( counter == timeout )); then
|
2016-03-29 12:40:14 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
(( counter++ ))
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-10-18 13:50:53 +00:00
|
|
|
main() {
|
2016-03-29 12:40:14 +00:00
|
|
|
timeout=30
|
|
|
|
signal=15
|
|
|
|
|
|
|
|
while (( $# )); do
|
|
|
|
case $1 in
|
|
|
|
-w)
|
|
|
|
flag_wait=1
|
|
|
|
timeout=$2
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
|
|
|
|
-s)
|
|
|
|
signal=$2
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2014-10-18 13:50:53 +00:00
|
|
|
my_tree=( $(get_my_tree) self thread-self )
|
|
|
|
all_procs=( $(get_all_procs) )
|
|
|
|
procs_to_kill=( $(get_procs_to_kill) )
|
|
|
|
|
2016-03-29 12:40:14 +00:00
|
|
|
kill -n "$signal" "${procs_to_kill[@]}" 2>/dev/null
|
2014-10-18 13:50:53 +00:00
|
|
|
|
2016-03-29 12:40:14 +00:00
|
|
|
if (( flag_wait )); then
|
|
|
|
pid_wait "${procs_to_kill[@]}"
|
|
|
|
fi
|
2014-10-18 13:50:53 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|