rc.subr revision 1.59
1# $OpenBSD: rc.subr,v 1.59 2012/08/04 15:30:25 ajacoutot Exp $ 2# 3# Copyright (c) 2010, 2011 Antoine Jacoutot <ajacoutot@openbsd.org> 4# Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org> 5# Copyright (c) 2010, 2011 Robert Nagy <robert@openbsd.org> 6# 7# Permission to use, copy, modify, and distribute this software for any 8# purpose with or without fee is hereby granted, provided that the above 9# copyright notice and this permission notice appear in all copies. 10# 11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 19 20# Default functions and variables used by rc.d(8) scripts. 21 22rc_err() { 23 echo $1 1>&2 24 exit 1 25} 26 27rc_usage() { 28 rc_err "usage: $0 [-df] {start|check|reload|restart|stop}" 29} 30 31rc_write_runfile() { 32 [ -d ${_RC_RUNDIR} ] || mkdir -p ${_RC_RUNDIR} && \ 33 print -rn -- "${pexp}" > ${_RC_RUNFILE} 34} 35 36rc_read_runfile() { 37 local _new_pexp 38 [ -f ${_RC_RUNFILE} ] && _new_pexp=$(< ${_RC_RUNFILE}) 39 [ -n "${_new_pexp}" ] && pexp="${_new_pexp}" 40} 41 42rc_rm_runfile() { 43 [ -f ${_RC_RUNFILE} ] && rm -f ${_RC_RUNFILE} 44} 45 46rc_start() { 47 ${rcexec} "${daemon} ${daemon_flags} ${_bg}" 48} 49 50rc_check() { 51 pgrep -f "^${pexp}" >/dev/null 52} 53 54rc_reload() { 55 pkill -HUP -f "^${pexp}" 56} 57 58rc_stop() { 59 pkill -f "^${pexp}" 60} 61 62rc_do() { 63 if [ -n "${_RC_DEBUG}" ]; then 64 echo "doing $@" && "$@" 65 else 66 "$@" >/dev/null 2>&1 67 fi 68} 69 70rc_exit() { 71 [ -z "${INRC}" -o X"$1" != X"ok" ] && _pfix="($1)" 72 echo ${INRC:+'-n'} "${_pfix}" 73 [ X"$1" = X"ok" ] && exit 0 || exit 1 74} 75 76rc_wait() { 77 local _i=0 _w=${2:-30} 78 while [ $_i -lt $_w ]; do 79 case "$1" in 80 reload|start) 81 rc_do rc_check && return 0 82 ;; 83 stop) 84 rc_do rc_check || return 0 85 ;; 86 *) 87 break 88 ;; 89 esac 90 sleep 1 91 _i=$((_i+1)) 92 done 93 return 1 94} 95 96rc_cmd() { 97 local _bg _n 98 99 [ "$(id -u)" -eq 0 ] || \ 100 [ X"${rc_usercheck}" != X"NO" -a X"$1" = "Xcheck" ] || \ 101 rc_err "$0: need root privileges" 102 103 if [ -z "${_RC_FORCE}" ] && [ X"${daemon_flags}" = X"NO" ] 104 then 105 if [ -n "${_RC_DEBUG}" ] 106 then 107 rc_err "$0: no $1 without -f, ${_name}_flags=NO" 108 fi 109 exit 1 110 fi 111 112 eval _enotsup=\${rc_${1}} 113 [ X"${_enotsup}" != X"NO" ] || rc_err "$0: $1 is not supported" 114 115 [ X"${rc_bg}" = X"YES" ] && _bg="&" 116 [ -n "${_RC_DEBUG}" ] || _n="-n" 117 118 rc_do rc_read_runfile 119 120 case "$1" in 121 check) 122 rc_do rc_check 123 ;; 124 start) 125 [ -z "${INRC}" ] && rc_do rc_check && exit 0 126 echo $_n "${INRC:+ }${_name}" 127 while true; do # no real loop, only needed to break 128 if type rc_pre >/dev/null; then 129 rc_do rc_pre || break 130 fi 131 # XXX only checks the status of the return code, 132 # and _not_ that the daemon is actually running 133 rc_do rc_start || break 134 if [ -n "${_bg}" ]; then 135 sleep 1 136 rc_do rc_wait start || break 137 fi 138 rc_do rc_write_runfile 139 rc_exit ok 140 done 141 # handle failure 142 type rc_post >/dev/null && rc_do rc_post 143 rc_do rc_rm_runfile 144 rc_exit failed 145 ;; 146 stop) 147 rc_do rc_check || exit 0 148 echo $_n "${INRC:+ }${_name}" 149 rc_do rc_stop || rc_exit failed 150 rc_do rc_wait stop || rc_exit failed 151 if type rc_post >/dev/null; then \ 152 rc_do rc_post || rc_exit failed 153 fi 154 rc_do rc_rm_runfile 155 rc_exit ok 156 ;; 157 reload) 158 rc_do rc_check || exit 0 159 echo $_n "${INRC:+ }${_name}" 160 rc_do rc_reload || rc_exit failed 161 rc_do rc_wait reload || rc_exit failed 162 rc_exit ok 163 ;; 164 restart) 165 /etc/rc.d/${_name} ${_RC_DEBUG} ${_RC_FORCE} stop && 166 /etc/rc.d/${_name} ${_RC_DEBUG} ${_RC_FORCE} start 167 ;; 168 *) 169 rc_usage 170 ;; 171 esac 172} 173 174[ -z "${local_rcconf}" ] && . /etc/rc.conf 175 176[ -n "${daemon}" ] || rc_err "$0: daemon is not set" 177 178unset _RC_DEBUG _RC_FORCE 179while getopts "df" c; do 180 case "$c" in 181 d) _RC_DEBUG=-d;; 182 f) _RC_FORCE=-f;; 183 *) rc_usage;; 184 esac 185done 186shift $((OPTIND-1)) 187 188_name=$(basename $0) 189_RC_RUNDIR=/var/run/rc.d 190_RC_RUNFILE=${_RC_RUNDIR}/${_name} 191 192eval _rcflags=\${${_name}_flags} 193eval _rcuser=\${${_name}_user} 194 195getcap -f /etc/login.conf ${_name} 1>/dev/null 2>&1 && \ 196 daemon_class=${_name} 197 198[ -z "${daemon_class}" ] && daemon_class=daemon 199[ -z "${daemon_user}" ] && daemon_user=root 200 201[ -n "${_RC_FORCE}" ] && [ X"${_rcflags}" = X"NO" ] && unset _rcflags 202[ -n "${_rcflags}" ] && daemon_flags=${_rcflags} 203[ -n "${_rcuser}" ] && daemon_user=${_rcuser} 204 205daemon_flags=$(printf ' %s' ${daemon_flags}) 206daemon_flags=${daemon_flags## } 207pexp="${daemon}${daemon_flags:+ ${daemon_flags}}" 208rcexec="su -l -c ${daemon_class} -s /bin/sh ${daemon_user} -c" 209