rc.subr revision 1.82
1# $OpenBSD: rc.subr,v 1.82 2014/08/21 10:06:14 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 _conf _i _l _val 118 set -A _allowed_keys -- \ 119 spamd_black pf ipsec check_quotas accounting \ 120 multicast_host multicast_router amd_master \ 121 pf_rules ipsec_rules shlib_dirs pkg_scripts \ 122 nfs_server 123 124 [ $# -gt 0 ] || set -- /etc/rc.conf /etc/rc.conf.local 125 for _rcfile; do 126 [[ -f $_rcfile ]] || continue 127 while IFS=' ' read -r _l; do 128 [[ $_l == [!#=]*=* ]] || continue 129 _key=${_l%%*([[:blank:]])=*} 130 [[ $_key == *_@(flags|user|timeout) ]] || \ 131 [[ " ${_allowed_keys[*]} " == *" $_key "* ]] || \ 132 continue 133 [[ $_key == "" ]] && continue 134 _val=${_l##*([!=])=*([[:blank:]])} 135 _val=${_val%%#*} 136 _val=${_val%%*([[:blank:]])} 137 # remove leading and trailing quotes (backwards compat) 138 [[ $_val == @(\"*\"|\'*\') ]] && _val=${_val#?} _val=${_val%?} 139 eval "${_key}=\${_val}" 140 _conf="${_conf} ${_key}" 141 done < $_rcfile 142 done 143 144 _rc_do _rc_quirks 145 146 if [ -n "${_RC_DEBUG}" ]; then 147 for _i in ${_conf}; do 148 printf "%18s\t>$(eval echo '$'${_i})<\n" ${_i} 149 done | sort -uk 1b 150 fi 151} 152 153[ -n "${FUNCS_ONLY}" ] && return 154 155rc_start() { 156 ${rcexec} "${daemon} ${daemon_flags} ${_bg}" 157} 158 159rc_check() { 160 pgrep -q -f "^${pexp}" 161} 162 163rc_reload() { 164 pkill -HUP -f "^${pexp}" 165} 166 167rc_stop() { 168 pkill -f "^${pexp}" 169} 170 171rc_cmd() { 172 local _bg _n 173 174 [ "$(id -u)" -eq 0 ] || \ 175 [ X"${rc_usercheck}" != X"NO" -a X"$1" = "Xcheck" ] || \ 176 _rc_err "$0: need root privileges" 177 178 if ! (_rc_is_supported start && _rc_is_supported stop); then 179 rc_restart=NO 180 fi 181 182 if ! _rc_is_supported $1; then 183 [ -n "${INRC}" ] && exit 1 184 _rc_err "$0: $1 is not supported" 185 fi 186 187 [ X"${rc_bg}" = X"YES" ] && _bg="&" 188 [ -n "${_RC_DEBUG}" ] || _n="-n" 189 190 _rc_do _rc_read_runfile 191 192 case "$1" in 193 check) 194 echo $_n "${INRC:+ }${_name}" 195 _rc_do rc_check && _rc_exit ok 196 _rc_exit failed 197 ;; 198 start) 199 if [ X"${daemon_flags}" = X"NO" ]; then 200 _rc_err "$0: need -f to force $1 since ${_name}_flags=NO" 201 exit 1 202 fi 203 [ -z "${INRC}" ] && _rc_do rc_check && exit 0 204 echo $_n "${INRC:+ }${_name}" 205 while true; do # no real loop, only needed to break 206 if type rc_pre >/dev/null; then 207 _rc_do rc_pre || break 208 fi 209 # XXX only checks the status of the return code, 210 # and _not_ that the daemon is actually running 211 _rc_do rc_start || break 212 if [ -n "${_bg}" ]; then 213 sleep 1 214 _rc_do _rc_wait start || break 215 fi 216 _rc_do _rc_write_runfile 217 _rc_exit ok 218 done 219 # handle failure 220 type rc_post >/dev/null && _rc_do rc_post 221 _rc_do _rc_rm_runfile 222 _rc_exit failed 223 ;; 224 stop) 225 _rc_do rc_check || exit 0 226 echo $_n "${INRC:+ }${_name}" 227 _rc_do rc_stop || _rc_exit failed 228 _rc_do _rc_wait stop || _rc_exit failed 229 if type rc_post >/dev/null; then \ 230 _rc_do rc_post || _rc_exit failed 231 fi 232 _rc_do _rc_rm_runfile 233 _rc_exit ok 234 ;; 235 reload) 236 _rc_do rc_check || exit 0 237 echo $_n "${INRC:+ }${_name}" 238 _rc_do rc_reload || _rc_exit failed 239 _rc_do _rc_wait reload || _rc_exit failed 240 _rc_exit ok 241 ;; 242 restart) 243 $0 ${_RC_DEBUG} ${_RC_FORCE} stop && 244 $0 ${_RC_DEBUG} ${_RC_FORCE} start 245 ;; 246 *) 247 _rc_usage 248 ;; 249 esac 250} 251 252[ -n "${daemon}" ] || _rc_err "$0: daemon is not set" 253 254unset _RC_DEBUG _RC_FORCE 255while getopts "df" c; do 256 case "$c" in 257 d) _RC_DEBUG=-d;; 258 f) _RC_FORCE=-f;; 259 *) _rc_usage;; 260 esac 261done 262shift $((OPTIND-1)) 263 264_name=$(basename $0) 265_RC_RUNDIR=/var/run/rc.d 266_RC_RUNFILE=${_RC_RUNDIR}/${_name} 267 268# parse /etc/rc.conf{.local} for the daemon_flags 269_rc_do _rc_parse_conf 270 271eval _rcflags=\${${_name}_flags} 272eval _rcuser=\${${_name}_user} 273eval _rctimeout=\${${_name}_timeout} 274 275getcap -f /etc/login.conf ${_name} 1>/dev/null 2>&1 && \ 276 daemon_class=${_name} 277 278[ -z "${daemon_class}" ] && daemon_class=daemon 279[ -z "${daemon_user}" ] && daemon_user=root 280[ -z "${daemon_timeout}" ] && daemon_timeout=30 281 282[ -n "${_RC_FORCE}" ] && [ X"${_rcflags}" = X"NO" ] && unset _rcflags 283[ -n "${_rcflags}" ] && daemon_flags=${_rcflags} 284[ -n "${_rcuser}" ] && daemon_user=${_rcuser} 285[ -n "${_rctimeout}" ] && daemon_timeout=${_rctimeout} 286 287readonly daemon_class 288unset _rcflags _rcuser _rctimeout 289pexp="${daemon}${daemon_flags:+ ${daemon_flags}}" 290rcexec="su -l -c ${daemon_class} -s /bin/sh ${daemon_user} -c" 291