--- rc.subr.orig 2012-11-30 17:29:49.000000000 +0900 +++ rc.subr 2012-11-30 18:32:36.000000000 +0900 @@ -398,6 +398,42 @@ } # +# wait_max_for_pids timeout pid [pid ...] +# spins until none of the pids exist, or until a maximum of +# "timeout" seconds have elapsed (whichever comes first). +# +wait_max_for_pids() +{ + _timeout=$1 + shift + _list="$@" + if [ -z "$_list" ]; then + return 0 + fi + _prefix= + while [ $_timeout -gt 0 ] ; 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 + return 0 + fi + _list=$_nlist + echo -n ${_prefix:-"Waiting (max $_timeout secs) for PIDS: "}$_list + _prefix=", " + sleep 2 + _timeout=$(($_timeout-2)) + done + if [ -n "$_prefix" ]; then + echo "." + fi + return 1 +} + +# # run_rc_command argument # Search for argument in the list of supported commands, which is: # "start stop restart rcvar status poll ${extra_commands}" @@ -757,8 +793,18 @@ _run_rc_doit "$_doit" || return 1 # wait for the command to exit, + # kill it if a timeout is specified, # and run postcmd. - wait_for_pids $rc_pid + eval _timeout=\$${name}_stop_timeout + if [ -z $_timeout ]; then + eval _timeout=$default_stop_timeout + fi + if [ ${_timeout:-0} -gt 0 ]; then + wait_max_for_pids ${_timeout} $rc_pid || \ + echo kill -KILL $rc_pid + else + wait_for_pids $rc_pid + fi _run_rc_postcmd ;;