rc.subr revision 1.75
1# $OpenBSD: rc.subr,v 1.75 2014/07/30 13:18:59 ajacoutot Exp $ 2# 3# Copyright (c) 2010, 2011, 2014 Antoine Jacoutot <ajacoutot@openbsd.org> 4# Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org> 5# Copyright (c) 2010, 2011, 2014 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 22_rc_err() { 23 echo $1 1>&2 24 exit 1 25} 26 27_rc_is_supported() { 28 local _enotsup 29 eval _enotsup=\${rc_$1} 30 [ X"${_enotsup}" != X"NO" ] 31} 32 33_rc_usage() { 34 local _a _allsup 35 for _a in start stop restart reload check; do 36 _rc_is_supported ${_a} && _allsup="${_allsup:+$_allsup|}${_a}" 37 done 38 _rc_err "usage: $0 [-df] (${_allsup})" 39} 40 41_rc_write_runfile() { 42 [ -d ${_RC_RUNDIR} ] || mkdir -p ${_RC_RUNDIR} && \ 43 print -rn -- "${pexp}" > ${_RC_RUNFILE} 44} 45 46_rc_read_runfile() { 47 local _new_pexp 48 [ -f ${_RC_RUNFILE} ] && _new_pexp=$(< ${_RC_RUNFILE}) 49 [ -n "${_new_pexp}" ] && pexp="${_new_pexp}" 50} 51 52_rc_rm_runfile() { 53 rm -f ${_RC_RUNFILE} 54} 55 56_rc_do() { 57 if [ -n "${_RC_DEBUG}" ]; then 58 echo "doing $@" && "$@" 59 else 60 "$@" >/dev/null 2>&1 61 fi 62} 63 64_rc_exit() { 65 local _pfix 66 [ -z "${INRC}" -o X"$1" != X"ok" ] && _pfix="($1)" 67 echo ${INRC:+'-n'} "${_pfix}" 68 [ X"$1" = X"ok" ] && exit 0 || exit 1 69} 70 71_rc_wait() { 72 local _i=0 73 while [ $_i -lt ${daemon_timeout} ]; do 74 case "$1" in 75 reload|start) 76 _rc_do rc_check && return 0 77 ;; 78 stop) 79 _rc_do rc_check || return 0 80 ;; 81 *) 82 break 83 ;; 84 esac 85 sleep 1 86 _i=$((_i+1)) 87 done 88 return 1 89} 90 91_rc_quirks() { 92 # special care needed for spamlogd to avoid starting it up and failing 93 # all the time 94 if [ X"${spamd_flags}" = X"NO" -o X"${spamd_black}" != X"NO" ]; then 95 spamlogd_flags=NO 96 fi 97 98 # special care needed for pflogd to avoid starting it up and failing 99 # if pf is not enabled 100 if [ X"${pf}" = X"NO" ]; then 101 pflogd_flags=NO 102 fi 103 104 # special care needed if nfs_server=YES to startup nfsd and mountd with 105 # sane default flags 106 if [ X"${nfs_server}" = X"YES" ]; then 107 [ X"${nfsd_flags}" = X"NO" ] && nfsd_flags="-tun 4" 108 [ X"${mountd_flags}" = X"NO" ] && mountd_flags= 109 fi 110 111 # in case domainname is set and /var/yp/binding exists enable ypbind 112 [ X"`domainname`" != X"" -a -d /var/yp/binding ] && ypbind_flags= 113} 114 115_rc_parse_conf() { 116 typeset -l _key 117 local _l _val 118 local _rcconf="/etc/rc.conf" 119 local _rcconf_local="/etc/rc.conf.local" 120 set -A _allowed_keys -- \ 121 spamd_black pf ipsec check_quotas accounting \ 122 multicast_host multicast_router amd_master \ 123 pf_rules ipsec_rules shlib_dirs pkg_scripts \ 124 nfs_server 125 126 for _rcfile in $_rcconf $_rcconf_local; do 127 [[ -f $_rcfile ]] || return 128 while IFS=' ' read -r _l; do 129 [[ $_l == [!#=]*=* ]] || continue 130 _key=${_l%%*([[:blank:]])=*} 131 [[ $_key == *_@(flags|user|timeout) ]] || \ 132 [[ " ${_allowed_keys[*]} " == *" $_key "* ]] || \ 133 continue 134 [[ $_key == "" ]] && continue 135 _val=${_l##*([!=])=*([[:blank:]])} 136 _val=${_val%%#*} 137 _val=${_val%%*([[:blank:]])} 138 # remove leading and trailing quotes (backwards compat) 139 [[ $_val == @(\"*\"|\'*\') ]] && _val=${_val#?} _val=${_val%?} 140 [ -n "${_RC_DEBUG}" ] && printf "%18s\t>$_val<\n" $_key 141 eval "${_key}=\${_val}" 142 done < $_rcfile 143 done 144 145 _rc_do _rc_quirks 146} 147 148[ -n "${FUNCS_ONLY}" ] && return 149 150rc_start() { 151 ${rcexec} "${daemon} ${daemon_flags} ${_bg}" 152} 153 154rc_check() { 155 pgrep -q -f "^${pexp}" 156} 157 158rc_reload() { 159 pkill -HUP -f "^${pexp}" 160} 161 162rc_stop() { 163 pkill -f "^${pexp}" 164} 165 166rc_cmd() { 167 local _bg _n 168 169 [ "$(id -u)" -eq 0 ] || \ 170 [ X"${rc_usercheck}" != X"NO" -a X"$1" = "Xcheck" ] || \ 171 _rc_err "$0: need root privileges" 172 173 if ! (_rc_is_supported start && _rc_is_supported stop); then 174 rc_restart=NO 175 fi 176 177 if ! _rc_is_supported $1; then 178 [ -n "${INRC}" ] && exit 1 179 _rc_err "$0: $1 is not supported" 180 fi 181 182 [ X"${rc_bg}" = X"YES" ] && _bg="&" 183 [ -n "${_RC_DEBUG}" ] || _n="-n" 184 185 _rc_do _rc_read_runfile 186 187 case "$1" in 188 check) 189 _rc_do rc_check 190 ;; 191 start) 192 if [ X"${daemon_flags}" = X"NO" ]; then 193 _rc_err "$0: need -f to force $1 since ${_name}_flags=NO" 194 exit 1 195 fi 196 [ -z "${INRC}" ] && _rc_do rc_check && exit 0 197 echo $_n "${INRC:+ }${_name}" 198 while true; do # no real loop, only needed to break 199 if type rc_pre >/dev/null; then 200 _rc_do rc_pre || break 201 fi 202 # XXX only checks the status of the return code, 203 # and _not_ that the daemon is actually running 204 _rc_do rc_start || break 205 if [ -n "${_bg}" ]; then 206 sleep 1 207 _rc_do _rc_wait start || break 208 fi 209 _rc_do _rc_write_runfile 210 _rc_exit ok 211 done 212 # handle failure 213 type rc_post >/dev/null && _rc_do rc_post 214 _rc_do _rc_rm_runfile 215 _rc_exit failed 216 ;; 217 stop) 218 _rc_do rc_check || exit 0 219 echo $_n "${INRC:+ }${_name}" 220 _rc_do rc_stop || _rc_exit failed 221 _rc_do _rc_wait stop || _rc_exit failed 222 if type rc_post >/dev/null; then \ 223 _rc_do rc_post || _rc_exit failed 224 fi 225 _rc_do _rc_rm_runfile 226 _rc_exit ok 227 ;; 228 reload) 229 _rc_do rc_check || exit 0 230 echo $_n "${INRC:+ }${_name}" 231 _rc_do rc_reload || _rc_exit failed 232 _rc_do _rc_wait reload || _rc_exit failed 233 _rc_exit ok 234 ;; 235 restart) 236 $0 ${_RC_DEBUG} ${_RC_FORCE} stop && 237 $0 ${_RC_DEBUG} ${_RC_FORCE} start 238 ;; 239 *) 240 _rc_usage 241 ;; 242 esac 243} 244 245[ -n "${daemon}" ] || _rc_err "$0: daemon is not set" 246 247unset _RC_DEBUG _RC_FORCE 248while getopts "df" c; do 249 case "$c" in 250 d) _RC_DEBUG=-d;; 251 f) _RC_FORCE=-f;; 252 *) _rc_usage;; 253 esac 254done 255shift $((OPTIND-1)) 256 257_name=$(basename $0) 258_RC_RUNDIR=/var/run/rc.d 259_RC_RUNFILE=${_RC_RUNDIR}/${_name} 260 261# parse /etc/rc.conf{.local} for the daemon_flags 262_rc_do _rc_parse_conf 263 264eval _rcflags=\${${_name}_flags} 265eval _rcuser=\${${_name}_user} 266eval _rctimeout=\${${_name}_timeout} 267 268getcap -f /etc/login.conf ${_name} 1>/dev/null 2>&1 && \ 269 daemon_class=${_name} 270 271[ -z "${daemon_class}" ] && daemon_class=daemon 272[ -z "${daemon_user}" ] && daemon_user=root 273[ -z "${daemon_timeout}" ] && daemon_timeout=30 274 275[ -n "${_RC_FORCE}" ] && [ X"${_rcflags}" = X"NO" ] && unset _rcflags 276[ -n "${_rcflags}" ] && daemon_flags=${_rcflags} 277[ -n "${_rcuser}" ] && daemon_user=${_rcuser} 278[ -n "${_rctimeout}" ] && daemon_timeout=${_rctimeout} 279 280# sanitize 281daemon_flags=$(printf ' %s' ${daemon_flags}) 282daemon_flags=${daemon_flags## } 283readonly daemon_class 284unset _rcflags _rcuser 285 286pexp="${daemon}${daemon_flags:+ ${daemon_flags}}" 287rcexec="su -l -c ${daemon_class} -s /bin/sh ${daemon_user} -c" 288