diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2011-06-20 21:26:28 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2011-06-20 21:26:28 +0000 |
commit | 22c7435dce6420fd94c18995b8529e940e17d682 (patch) | |
tree | 1b083e5d22b271b4cee4b97899c5982a937e562a | |
parent | 2d5cc9e3a3a39bee477dd8511442616575a69eae (diff) |
Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@
-rw-r--r-- | etc/rc.d/rc.subr | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/etc/rc.d/rc.subr b/etc/rc.d/rc.subr index 1c34625ddde..21cb896902c 100644 --- a/etc/rc.d/rc.subr +++ b/etc/rc.d/rc.subr @@ -1,4 +1,4 @@ -# $OpenBSD: rc.subr,v 1.37 2011/06/10 08:43:26 ajacoutot Exp $ +# $OpenBSD: rc.subr,v 1.38 2011/06/20 21:26:27 schwarze Exp $ # Default functions and variables used by rc.d(8) scripts. @@ -31,11 +31,10 @@ rc_do() { fi } -rc_print() { - _ret=$? +rc_exit() { [ -z "${INRC}" -o X"$1" != X"ok" ] && _pfix="($1)" echo ${INRC:+'-n'} "${INRC:+ }${_name}${_pfix}" - return ${_ret} + [ X"$1" = X"ok" ] && exit 0 || exit 1 } rc_wait() { @@ -75,44 +74,34 @@ rc_cmd() { rc_do rc_check ;; start) - rc_do rc_check || \ - ( + rc_do rc_check && exit 0 + while true; do # no real loop, only needed to break if type rc_pre >/dev/null; then - rc_do rc_pre + rc_do rc_pre || break fi - [ $? -eq 0 ] && rc_do rc_start - [ $? -eq 0 ] && \ - if [ -n "${_bg}" ]; then - sleep 1 && rc_do rc_wait start - else - : # do nothing - fi - [ $? -eq 0 ] && \ - rc_print ok || \ - ( - type rc_post >/dev/null && rc_do rc_post - rc_print failed - return 1 - ) - ) + rc_do rc_start || break + if [ -n "${_bg}" ]; then + sleep 1 + rc_do rc_wait start || break + fi + rc_exit ok + done + # handle failure + type rc_post >/dev/null && rc_do rc_post + rc_exit failed ;; stop) - if rc_do rc_check; then rc_do rc_stop || \ - ( rc_print failed ) && \ - ( - rc_do rc_wait stop && - ( - if type rc_post >/dev/null; then \ - rc_do rc_post - fi - ) && rc_print ok || rc_print failed - ) - else - return 0 + rc_do rc_check || exit 0 + rc_do rc_stop || rc_exit failed + rc_do rc_wait stop || rc_exit failed + if type rc_post >/dev/null; then \ + rc_do rc_post || rc_exit failed fi + rc_exit ok ;; reload) - rc_do rc_check && ( rc_do rc_reload || rc_print failed ) + rc_do rc_check || exit 1 + rc_do rc_reload || rc_exit failed ;; restart) /etc/rc.d/${_name} stop && /etc/rc.d/${_name} start |