rc.subr revision 1.69
1# $OpenBSD: rc.subr,v 1.69 2013/04/04 06:50:44 zhuk 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_is_supported() { 28 local _enotsup 29 eval _enotsup=\${rc_$1} 30 [ X"${_enotsup}" != X"NO" ] 31} 32 33rc_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 41rc_write_runfile() { 42 [ -d ${_RC_RUNDIR} ] || mkdir -p ${_RC_RUNDIR} && \ 43 print -rn -- "${pexp}" > ${_RC_RUNFILE} 44} 45 46rc_read_runfile() { 47 local _new_pexp 48 [ -f ${_RC_RUNFILE} ] && _new_pexp=$(< ${_RC_RUNFILE}) 49 [ -n "${_new_pexp}" ] && pexp="${_new_pexp}" 50} 51 52rc_rm_runfile() { 53 rm -f ${_RC_RUNFILE} 54} 55 56rc_start() { 57 ${rcexec} "${daemon} ${daemon_flags} ${_bg}" 58} 59 60rc_check() { 61 pgrep -q -f "^${pexp}" 62} 63 64rc_reload() { 65 pkill -HUP -f "^${pexp}" 66} 67 68rc_stop() { 69 pkill -f "^${pexp}" 70} 71 72rc_do() { 73 if [ -n "${_RC_DEBUG}" ]; then 74 echo "doing $@" && "$@" 75 else 76 "$@" >/dev/null 2>&1 77 fi 78} 79 80rc_exit() { 81 local _pfix 82 [ -z "${INRC}" -o X"$1" != X"ok" ] && _pfix="($1)" 83 echo ${INRC:+'-n'} "${_pfix}" 84 [ X"$1" = X"ok" ] && exit 0 || exit 1 85} 86 87rc_wait() { 88 local _i=0 _w=${2:-30} 89 while [ $_i -lt $_w ]; do 90 case "$1" in 91 reload|start) 92 rc_do rc_check && return 0 93 ;; 94 stop) 95 rc_do rc_check || return 0 96 ;; 97 *) 98 break 99 ;; 100 esac 101 sleep 1 102 _i=$((_i+1)) 103 done 104 return 1 105} 106 107rc_cmd() { 108 local _bg _n 109 110 [ "$(id -u)" -eq 0 ] || \ 111 [ X"${rc_usercheck}" != X"NO" -a X"$1" = "Xcheck" ] || \ 112 rc_err "$0: need root privileges" 113 114 if ! (rc_is_supported start && rc_is_supported stop); then 115 rc_restart=NO 116 fi 117 118 if ! rc_is_supported $1; then 119 [ -n "${INRC}" ] && exit 1 120 rc_err "$0: $1 is not supported" 121 fi 122 123 [ X"${rc_bg}" = X"YES" ] && _bg="&" 124 [ -n "${_RC_DEBUG}" ] || _n="-n" 125 126 rc_do rc_read_runfile 127 128 case "$1" in 129 check) 130 rc_do rc_check 131 ;; 132 start) 133 if [ X"${daemon_flags}" = X"NO" ]; then 134 rc_err "$0: no $1 without -f, ${_name}_flags=NO" 135 exit 1 136 fi 137 [ -z "${INRC}" ] && rc_do rc_check && exit 0 138 echo $_n "${INRC:+ }${_name}" 139 while true; do # no real loop, only needed to break 140 if type rc_pre >/dev/null; then 141 rc_do rc_pre || break 142 fi 143 # XXX only checks the status of the return code, 144 # and _not_ that the daemon is actually running 145 rc_do rc_start || break 146 if [ -n "${_bg}" ]; then 147 sleep 1 148 rc_do rc_wait start || break 149 fi 150 rc_do rc_write_runfile 151 rc_exit ok 152 done 153 # handle failure 154 type rc_post >/dev/null && rc_do rc_post 155 rc_do rc_rm_runfile 156 rc_exit failed 157 ;; 158 stop) 159 rc_do rc_check || exit 0 160 echo $_n "${INRC:+ }${_name}" 161 rc_do rc_stop || rc_exit failed 162 rc_do rc_wait stop || rc_exit failed 163 if type rc_post >/dev/null; then \ 164 rc_do rc_post || rc_exit failed 165 fi 166 rc_do rc_rm_runfile 167 rc_exit ok 168 ;; 169 reload) 170 rc_do rc_check || exit 0 171 echo $_n "${INRC:+ }${_name}" 172 rc_do rc_reload || rc_exit failed 173 rc_do rc_wait reload || rc_exit failed 174 rc_exit ok 175 ;; 176 restart) 177 $0 ${_RC_DEBUG} ${_RC_FORCE} stop && 178 $0 ${_RC_DEBUG} ${_RC_FORCE} start 179 ;; 180 *) 181 rc_usage 182 ;; 183 esac 184} 185 186. /etc/rc.conf 187 188[ -n "${daemon}" ] || rc_err "$0: daemon is not set" 189 190unset _RC_DEBUG _RC_FORCE 191while getopts "df" c; do 192 case "$c" in 193 d) _RC_DEBUG=-d;; 194 f) _RC_FORCE=-f;; 195 *) rc_usage;; 196 esac 197done 198shift $((OPTIND-1)) 199 200_name=$(basename $0) 201_RC_RUNDIR=/var/run/rc.d 202_RC_RUNFILE=${_RC_RUNDIR}/${_name} 203 204eval _rcflags=\${${_name}_flags} 205eval _rcuser=\${${_name}_user} 206 207getcap -f /etc/login.conf ${_name} 1>/dev/null 2>&1 && \ 208 daemon_class=${_name} 209 210[ -z "${daemon_class}" ] && daemon_class=daemon 211[ -z "${daemon_user}" ] && daemon_user=root 212 213[ -n "${_RC_FORCE}" ] && [ X"${_rcflags}" = X"NO" ] && unset _rcflags 214[ -n "${_rcflags}" ] && daemon_flags=${_rcflags} 215[ -n "${_rcuser}" ] && daemon_user=${_rcuser} 216 217# sanitize 218daemon_flags=$(printf ' %s' ${daemon_flags}) 219daemon_flags=${daemon_flags## } 220readonly daemon_class 221unset _rcflags _rcuser 222 223pexp="${daemon}${daemon_flags:+ ${daemon_flags}}" 224rcexec="su -l -c ${daemon_class} -s /bin/sh ${daemon_user} -c" 225