diff --git a/rc.in b/rc.in index d68280b..aef540f 100644 --- a/rc.in +++ b/rc.in @@ -101,10 +101,10 @@ rc.services_stop() { rc.stop_everything() { echo "Politely asking all processes to shut down..." - killall5 -15; sleep 3 + killall5 -s 15 -w "${cfg_killall5_timeout:-30}" echo "Killing the remaning ones..." - killall5 -9 + killall5 -s 9 } rc.unmount_everything() { diff --git a/tools/killall5.in b/tools/killall5.in index e3fa5a3..7806ec6 100644 --- a/tools/killall5.in +++ b/tools/killall5.in @@ -37,14 +37,58 @@ get_procs_to_kill() { done } +pids_exist() { + for i in "$@"; do + if kill -0 "$i" 2>/dev/null; then + return 0 + fi + done +} + +pid_wait() { + declare counter + + while pids_exist "$@"; do + if (( counter = timeout )); then + return 1 + fi + + sleep 1 + + (( counter++ )) + done +} + main() { + timeout=30 + signal=15 + + while (( $# )); do + case $1 in + -w) + flag_wait=1 + timeout=$2 + shift + ;; + + -s) + signal=$2 + shift + ;; + esac + + shift + done + my_tree=( $(get_my_tree) self thread-self ) all_procs=( $(get_all_procs) ) procs_to_kill=( $(get_procs_to_kill) ) - signal="${1:-"-15"}" + kill -n "$signal" "${procs_to_kill[@]}" 2>/dev/null - kill "$signal" "${procs_to_kill[@]}" 2>/dev/null + if (( flag_wait )); then + pid_wait "${procs_to_kill[@]}" + fi return 0 }