# $OpenBSD: rc.subr,v 1.28 2011/03/19 19:03:57 ian Exp $ # Default functions and variables used by rc.d(8) scripts. rc_err() { echo $1 exit 1 } rc_start() { ${rcexec} "${daemon} ${daemon_flags} ${_bg}" } rc_check() { pkill -0 -f "^${pexp}" } rc_reload() { pkill -HUP -f "^${pexp}" } rc_stop() { pkill -f "^${pexp}" } rc_do() { if [ X"${RC_DEBUG}" = X"1" ]; then echo "doing $@" && "$@" else "$@" >/dev/null 2>&1 fi } rc_print() { _ret=$? echo ${INRC:+'-n'} "${INRC:+ }${_name}($1)" return ${_ret} } rc_wait() { i=0 while [ $i -lt 5 ]; do rc_do rc_check || return 0 sleep 1 i=$((i+1)) done return 1 } rc_cmd() { [ $(id -u) -eq 0 ] || \ rc_err "$0: need root privileges" eval _enotsup=\${rc_${1}} [ X"${_enotsup}" != X"NO" ] || rc_err "$0: $1 is not supported" [ X"${rc_bg}" = X"YES" ] && local _bg="&" case "$1" in check) rc_do rc_check ;; start) rc_do rc_check || \ ( if type rc_pre >/dev/null; then rc_do rc_pre fi [ $? -eq 0 ] && \ rc_do rc_start && \ rc_print ok || rc_print failed ) ;; stop) if rc_do rc_check; then rc_do rc_stop || \ ( rc_print failed ) && \ ( rc_do rc_wait && ( if type rc_post >/dev/null; then \ rc_do rc_post fi ) && rc_print ok || rc_print failed ) else return 0 fi ;; reload) rc_do rc_check && rc_do rc_reload ;; restart) /etc/rc.d/${_name} stop && /etc/rc.d/${_name} start ;; *) rc_err "usage: $0 {start|check|reload|restart|stop}" esac } [ -z "${local_rcconf}" ] && . /etc/rc.conf [ -n "${daemon}" ] || rc_err "$0: daemon is not set" _name=$(basename $0) eval _rcflags=\${${_name}_flags} eval _rcuser=\${${_name}_user} getcap -f /etc/login.conf ${_name} 1>/dev/null 2>&1 && \ daemon_class=${_name} [ -z "${daemon_class}" ] && daemon_class=daemon [ -z "${daemon_user}" ] && daemon_user=root [ -n "${_rcflags}" ] && daemon_flags=${_rcflags} [ -n "${_rcuser}" ] && daemon_user=${_rcuser} daemon_flags=$(echo ${daemon_flags} | tr -s "[:space:]") pexp="${daemon}${daemon_flags:+ ${daemon_flags}}" rcexec="su -l -c ${daemon_class} -s /bin/sh ${daemon_user} -c"