From fced4775b188ec04adfc44ad3e036adcf31f4749 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 29 Mar 2016 15:40:14 +0300 Subject: [PATCH 1/5] wait flag for killall5 --- rc.in | 4 ++-- tools/killall5.in | 48 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) 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 } From 24d64da2edaa3b5c5bbbf3d80c68c68e81546598 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 29 Mar 2016 15:48:51 +0300 Subject: [PATCH 2/5] stupid typo --- tools/killall5.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/killall5.in b/tools/killall5.in index 7806ec6..a4b6f2c 100644 --- a/tools/killall5.in +++ b/tools/killall5.in @@ -49,7 +49,7 @@ pid_wait() { declare counter while pids_exist "$@"; do - if (( counter = timeout )); then + if (( counter == timeout )); then return 1 fi From c799ab796bb94af8780ade522d5534b403bdaa6f Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 29 Mar 2016 16:03:44 +0300 Subject: [PATCH 3/5] we need to actually return 1 if no pids are alive. DUH --- tools/killall5.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/killall5.in b/tools/killall5.in index a4b6f2c..f896b83 100644 --- a/tools/killall5.in +++ b/tools/killall5.in @@ -43,6 +43,8 @@ pids_exist() { return 0 fi done + + return 1 } pid_wait() { From 69a1c39cbc0278d45a3d8092ee9bb82e0645b001 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 29 Mar 2016 16:23:55 +0300 Subject: [PATCH 4/5] default timeout in the config --- rc.conf | 3 +++ rc.in | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/rc.conf b/rc.conf index f0c9dd0..80e5ec7 100644 --- a/rc.conf +++ b/rc.conf @@ -40,3 +40,6 @@ cfg_mounts=( # Some temporary directories and files cfg_tmpdirs+=( '/run/lock' '/run/lock/lvm' '/run/lvm' '/run/user' ) cfg_tmpfiles+=( '/run/utmp' ) + +# How long do we wait for all processes to die nicely? +cfg_killall5_timeout=30 diff --git a/rc.in b/rc.in index a1fca5a..45f3a1c 100644 --- a/rc.in +++ b/rc.in @@ -101,7 +101,7 @@ rc.services_stop() { rc.stop_everything() { echo "Politely asking all processes to shut down..." - killall5 -s 15 -w "${cfg_killall5_timeout:-30}" + killall5 -s 15 -w "$cfg_killall5_timeout" echo "Killing the remaning ones..." killall5 -s 9 From 987c655fcc1508a68d97a67f2cd86d7e1ec1e0d0 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 29 Mar 2016 19:21:53 +0300 Subject: [PATCH 5/5] Just use /proc to see if a process is running --- README.md | 1 + tools/killall5.in | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 247ebde..68ecd32 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,4 @@ A simple rc script to kickstart your system. ## depends * Enabled sysrq or halt from suckless.org's ubase. +* /proc support. diff --git a/tools/killall5.in b/tools/killall5.in index f896b83..e13f9fe 100644 --- a/tools/killall5.in +++ b/tools/killall5.in @@ -39,7 +39,7 @@ get_procs_to_kill() { pids_exist() { for i in "$@"; do - if kill -0 "$i" 2>/dev/null; then + if [[ -d "/proc/$i" ]]; then return 0 fi done