Prevent waiting for processes to exit forever by introducing _rc_kill_ntries,

which if set, will SIGKILL the processes that did not die yet.
This commit is contained in:
christos 2020-04-05 21:03:08 +00:00
parent 3c6295a06d
commit 3dd07195ab
1 changed files with 43 additions and 16 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: rc.subr,v 1.103 2018/09/23 23:02:39 kre Exp $
# $NetBSD: rc.subr,v 1.104 2020/04/05 21:03:08 christos Exp $
#
# Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
# All rights reserved.
@ -43,6 +43,7 @@ _rc_pid=
_rc_original_stdout_fd=
_rc_original_stderr_fd=
_rc_postprocessor_fd=
_rc_kill_ntries=
"
#
@ -312,36 +313,62 @@ _find_processes()
eval $_proccheck
}
#
# kill_pids signal pid [pid ...]
# kills the given pids with signal.
# returns the list of pids killed successfully.
#
kill_pids()
{
local signal=$1
shift
local list="$@"
local j=
local nlist=
for j in $list; do
if kill -$signal $j 2>/dev/null; then
nlist="${nlist}${nlist:+ }$j"
fi
done
echo $nlist
}
#
# wait_for_pids pid [pid ...]
# spins until none of the pids exist
# if _rc_kill_ntries is set and exceeded, it SIGKILLS the remaining
# pids
#
wait_for_pids()
{
_list="$@"
if [ -z "$_list" ]; then
local ntries=0
local prefix=
local list="$@"
if [ -z "$list" ]; then
return
fi
_prefix=
while true; do
_nlist="";
for _j in $_list; do
if kill -0 $_j 2>/dev/null; then
_nlist="${_nlist}${_nlist:+ }$_j"
fi
done
if [ -z "$_nlist" ]; then
local nlist=$(kill_pids 0 $list)
if [ -z "$nlist" ]; then
break
fi
if [ "$_list" != "$_nlist" ]; then
_list=$_nlist
echo -n ${_prefix:-"Waiting for PIDS: "}$_list
_prefix=", "
if [ "$list" != "$nlist" ]; then
list=$nlist
echo -n ${prefix:-"Waiting for PIDS: "}$list
prefix=", "
fi
# We want this to be a tight loop for a fast exit
sleep 0.05
ntries=$((ntries + 1))
if [ -n "${_rc_kill_ntries}" ]; then
if [ ${ntries} -gt ${_rc_kill_ntries} ]; then
kill_pids 9 $list > /dev/null
fi
fi
done
if [ -n "$_prefix" ]; then
if [ -n "$prefix" ]; then
echo "."
fi
}