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