Browse Source

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@
OPENBSD_5_0
schwarze 13 years ago
parent
commit
bfd452983a
1 changed files with 24 additions and 35 deletions
  1. +24
    -35
      src/etc/rc.d/rc.subr

+ 24
- 35
src/etc/rc.d/rc.subr View File

@ -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


Loading…
Cancel
Save