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