rc.subr revision 197144
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 197144 2009-09-12 22:19:48Z hrs $
378344Sobrien#
4157473Sflz# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
578344Sobrien# All rights reserved.
678344Sobrien#
778344Sobrien# This code is derived from software contributed to The NetBSD Foundation
878344Sobrien# by Luke Mewburn.
978344Sobrien#
1078344Sobrien# Redistribution and use in source and binary forms, with or without
1178344Sobrien# modification, are permitted provided that the following conditions
1278344Sobrien# are met:
1378344Sobrien# 1. Redistributions of source code must retain the above copyright
1478344Sobrien#    notice, this list of conditions and the following disclaimer.
1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1678344Sobrien#    notice, this list of conditions and the following disclaimer in the
1778344Sobrien#    documentation and/or other materials provided with the distribution.
1878344Sobrien# 3. All advertising materials mentioning features or use of this software
1978344Sobrien#    must display the following acknowledgement:
2078344Sobrien#        This product includes software developed by the NetBSD
2178344Sobrien#        Foundation, Inc. and its contributors.
2278344Sobrien# 4. Neither the name of The NetBSD Foundation nor the names of its
2378344Sobrien#    contributors may be used to endorse or promote products derived
2478344Sobrien#    from this software without specific prior written permission.
2578344Sobrien#
2678344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2778344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2878344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2978344Sobrien# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3078344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3178344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3278344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3378344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3478344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3578344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3678344Sobrien# POSSIBILITY OF SUCH DAMAGE.
3778344Sobrien#
3878344Sobrien# rc.subr
3978344Sobrien#	functions used by various rc scripts
4078344Sobrien#
4178344Sobrien
42157473Sflz: ${rcvar_manpage:='rc.conf(5)'}
43169668Smtm: ${RC_PID:=$$}; export RC_PID
44157473Sflz
4578344Sobrien#
4698186Sgordon#	Operating System dependent/independent variables
4798186Sgordon#
4898186Sgordon
49131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then
50131550Scperciva
51131550Scperciva_rc_subr_loaded="YES"
52131550Scperciva
5398186SgordonSYSCTL="/sbin/sysctl"
5498186SgordonSYSCTL_N="${SYSCTL} -n"
5598186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype"
56103018SgordonOSTYPE=`${CMD_OSTYPE}`
57124832SmtmID="/usr/bin/id"
58124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
59161435SyarPS="/bin/ps -ww"
60161435SyarJID=`$PS -p $$ -o jid=`
6198186Sgordon
62103018Sgordoncase ${OSTYPE} in
6398186SgordonFreeBSD)
6498186Sgordon	SYSCTL_W="${SYSCTL}"
6598186Sgordon	;;
6698186SgordonNetBSD)
6798186Sgordon	SYSCTL_W="${SYSCTL} -w"
6898186Sgordon	;;
6998186Sgordonesac
7098186Sgordon
7198186Sgordon#
7278344Sobrien#	functions
7378344Sobrien#	---------
7478344Sobrien
75197144Shrs# set_rcvar [var] [defval] [desc]
7678344Sobrien#
77197144Shrs#	Echo or define a rc.conf(5) variable name.  Global variable
78197144Shrs#	$rcvars is used.
7998186Sgordon#
80197144Shrs#	If no argument is specified, echo "${name}_enable".
81197144Shrs#
82197144Shrs#	If only a var is specified, echo "${var}_enable".
83197144Shrs#
84197144Shrs#	If var and defval are specified, the ${var} is defined as
85197144Shrs#	rc.conf(5) variable and the default value is ${defvar}.  An
86197144Shrs#	optional argument $desc can also be specified to add a
87197144Shrs#	description for that.
88197144Shrs#
8998186Sgordonset_rcvar()
9098186Sgordon{
91197144Shrs	case $# in
92197144Shrs	0)
93197144Shrs		echo ${name}_enable
9498186Sgordon		;;
95197144Shrs	1)
96197144Shrs		echo ${1}_enable
9798186Sgordon		;;
9898186Sgordon	*)
99197144Shrs		debug "rcvar_define: \$$1=$2 is added" \
100197144Shrs		    " as a rc.conf(5) variable."
101197144Shrs
102197144Shrs		local _var
103197144Shrs		_var=$1
104197144Shrs		rcvars="${rcvars# } $_var"
105197144Shrs		eval ${_var}_defval=\"$2\"
106197144Shrs		shift 2
107197144Shrs		# encode multiple lines of _desc
108197144Shrs		for l in "$@"; do
109197144Shrs			eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
110197144Shrs		done
111197144Shrs		eval ${_var}_desc=\"\${${_var}_desc#^^}\"
11298186Sgordon		;;
11398186Sgordon	esac
11498186Sgordon}
11598186Sgordon
116197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg]
117197144Shrs#	Define obsolete variable.
118197144Shrs#	Global variable $rcvars_obsolete is used.
11998186Sgordon#
120197144Shrsset_rcvar_obsolete()
121197144Shrs{
122197144Shrs	local _var
123197144Shrs	_var=$1
124197144Shrs	debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
125197144Shrs
126197144Shrs	rcvars_obsolete="${rcvars_obsolete# } $1"
127197144Shrs	eval ${1}_newvar=\"$2\"
128197144Shrs	shift 2
129197144Shrs	eval ${_var}_obsolete_msg=\"$*\"
130197144Shrs}
131197144Shrs
132197144Shrs#
13398186Sgordon# force_depend script
13498186Sgordon#	Force a service to start. Intended for use by services
13598186Sgordon#	to resolve dependency issues. It is assumed the caller
13698186Sgordon#	has check to make sure this call is necessary
13798186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
13898186Sgordon#
13998186Sgordonforce_depend()
14098186Sgordon{
14198186Sgordon	_depend="$1"
14298186Sgordon
14398186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
144146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
14598186Sgordon		warn "Unable to force ${_depend}. It may already be running."
14698186Sgordon		return 1
14798186Sgordon	fi
14898186Sgordon	return 0
14998186Sgordon}
15098186Sgordon
15198186Sgordon#
15278344Sobrien# checkyesno var
15378344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
15478344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
15578344Sobrien#
15678344Sobriencheckyesno()
15778344Sobrien{
15878344Sobrien	eval _value=\$${1}
15998186Sgordon	debug "checkyesno: $1 is set to $_value."
16078344Sobrien	case $_value in
16178344Sobrien
16278344Sobrien		#	"yes", "true", "on", or "1"
16378344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
16478344Sobrien		return 0
16578344Sobrien		;;
16678344Sobrien
16778344Sobrien		#	"no", "false", "off", or "0"
16878344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
16978344Sobrien		return 1
17078344Sobrien		;;
17178344Sobrien	*)
172157473Sflz		warn "\$${1} is not set properly - see ${rcvar_manpage}."
17378344Sobrien		return 1
17478344Sobrien		;;
17578344Sobrien	esac
17678344Sobrien}
17778344Sobrien
178157473Sflz#
17998186Sgordon# reverse_list list
18098186Sgordon#	print the list in reverse order
18178344Sobrien#
18298186Sgordonreverse_list()
18398186Sgordon{
18498186Sgordon	_revlist=
185126286Smtm	for _revfile; do
18698186Sgordon		_revlist="$_revfile $_revlist"
18798186Sgordon	done
18898186Sgordon	echo $_revlist
18998186Sgordon}
19098186Sgordon
191169668Smtm# stop_boot always
192169668Smtm#	If booting directly to multiuser or $always is enabled,
193169668Smtm#	send SIGTERM to the parent (/etc/rc) to abort the boot.
194169668Smtm#	Otherwise just exit.
19578344Sobrien#
196169668Smtmstop_boot()
197169668Smtm{
198169668Smtm	local always
199169668Smtm
200178776Smaxim	case $1 in
201178776Smaxim		#	"yes", "true", "on", or "1"
202178770Smtm        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
203169668Smtm		always=true
204178770Smtm		;;
205178770Smtm	*)
206169668Smtm		always=false
207178770Smtm		;;
208178775Smaxim	esac
209169668Smtm	if [ "$autoboot" = yes -o "$always" = true ]; then
210169668Smtm		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
211169668Smtm		kill -TERM ${RC_PID}
212169668Smtm	fi
213169668Smtm	exit 1
214169668Smtm}
215169668Smtm
216169668Smtm#
21798186Sgordon# mount_critical_filesystems type
21898186Sgordon#	Go through the list of critical filesystems as provided in
21998186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
22098186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
22198186Sgordon#
22278344Sobrienmount_critical_filesystems()
22378344Sobrien{
22498186Sgordon	eval _fslist=\$critical_filesystems_${1}
22578344Sobrien	for _fs in $_fslist; do
22678344Sobrien		mount | (
227126285Smtm			_ismounted=false
22878344Sobrien			while read what _on on _type type; do
22978344Sobrien				if [ $on = $_fs ]; then
230126285Smtm					_ismounted=true
23178344Sobrien				fi
23278344Sobrien			done
233126285Smtm			if $_ismounted; then
234126285Smtm				:
235126285Smtm			else
23678344Sobrien				mount $_fs >/dev/null 2>&1
23778344Sobrien			fi
23898186Sgordon		)
23978344Sobrien	done
24078344Sobrien}
24178344Sobrien
24278344Sobrien#
24398186Sgordon# check_pidfile pidfile procname [interpreter]
24498186Sgordon#	Parses the first line of pidfile for a PID, and ensures
24578344Sobrien#	that the process is running and matches procname.
24698186Sgordon#	Prints the matching PID upon success, nothing otherwise.
24798186Sgordon#	interpreter is optional; see _find_processes() for details.
24878344Sobrien#
24978344Sobriencheck_pidfile()
25078344Sobrien{
25178344Sobrien	_pidfile=$1
25278344Sobrien	_procname=$2
25398186Sgordon	_interpreter=$3
25478344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
25598186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
25678344Sobrien	fi
25778344Sobrien	if [ ! -f $_pidfile ]; then
258131061Smtm		debug "pid file ($_pidfile): not readable."
25978344Sobrien		return
26078344Sobrien	fi
26178344Sobrien	read _pid _junk < $_pidfile
26278344Sobrien	if [ -z "$_pid" ]; then
263139949Skeramida		debug "pid file ($_pidfile): no pid in file."
26478344Sobrien		return
26578344Sobrien	fi
26698186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
26778344Sobrien}
26878344Sobrien
26978344Sobrien#
27098186Sgordon# check_process procname [interpreter]
27178344Sobrien#	Ensures that a process (or processes) named procname is running.
27298186Sgordon#	Prints a list of matching PIDs.
27398186Sgordon#	interpreter is optional; see _find_processes() for details.
27478344Sobrien#
27578344Sobriencheck_process()
27678344Sobrien{
27778344Sobrien	_procname=$1
27898186Sgordon	_interpreter=$2
27978344Sobrien	if [ -z "$_procname" ]; then
28098186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
28178344Sobrien	fi
28298186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
28398186Sgordon}
28498186Sgordon
28598186Sgordon#
28698186Sgordon# _find_processes procname interpreter psargs
28798186Sgordon#	Search for procname in the output of ps generated by psargs.
28898186Sgordon#	Prints the PIDs of any matching processes, space separated.
28998186Sgordon#
29098186Sgordon#	If interpreter == ".", check the following variations of procname
29198186Sgordon#	against the first word of each command:
29298186Sgordon#		procname
29398186Sgordon#		`basename procname`
29498186Sgordon#		`basename procname` + ":"
29598186Sgordon#		"(" + `basename procname` + ")"
296155719Sceri#		"[" + `basename procname` + "]"
29798186Sgordon#
29898186Sgordon#	If interpreter != ".", read the first line of procname, remove the
29998186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
30098186Sgordon#	match that against each command, either as is, or with extra words
301157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
302157841Sflz#	using perl, the basename of the interpreter plus a colon is also
303157841Sflz#	tried as the prefix to procname.
30498186Sgordon#
30598186Sgordon_find_processes()
30698186Sgordon{
30798186Sgordon	if [ $# -ne 3 ]; then
30898186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
30998186Sgordon	fi
31098186Sgordon	_procname=$1
31198186Sgordon	_interpreter=$2
31298186Sgordon	_psargs=$3
31398186Sgordon
31478344Sobrien	_pref=
31598186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
316170282Syar		_script=${_chroot}${_chroot:+"/"}$_procname
317170282Syar		if [ -r $_script ]; then
318170282Syar			read _interp < $_script	# read interpreter name
319170282Syar			case "$_interp" in
320170282Syar			\#!*)
321170282Syar				_interp=${_interp#\#!}	# strip #!
322170282Syar				set -- $_interp
323170282Syar				case $1 in
324170282Syar				*/bin/env)
325170282Syar					shift	# drop env to get real name
326170282Syar					;;
327170282Syar				esac
328170282Syar				if [ $_interpreter != $1 ]; then
329170282Syar					warn "\$command_interpreter $_interpreter != $1"
330170282Syar				fi
331170282Syar				;;
332170282Syar			*)
333170282Syar				warn "no shebang line in $_script"
334170282Syar				set -- $_interpreter
335170282Syar				;;
336170282Syar			esac
337170282Syar		else
338170282Syar			warn "cannot read shebang line from $_script"
339170282Syar			set -- $_interpreter
34078344Sobrien		fi
34198186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
342157841Sflz		_interpbn=${1##*/}
34398186Sgordon		_fp_args='_argv'
34498186Sgordon		_fp_match='case "$_argv" in
345157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
34698186Sgordon	else					# a normal daemon
34798186Sgordon		_procnamebn=${_procname##*/}
34898186Sgordon		_fp_args='_arg0 _argv'
34998186Sgordon		_fp_match='case "$_arg0" in
350151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
35198186Sgordon	fi
35298186Sgordon
353161435Syar	_proccheck="\
354161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
355157657Sflz		while read _npid _jid '"$_fp_args"'; do
356161436Syar			'"$_fp_match"'
357157657Sflz				if [ "$JID" -eq "$_jid" ];
358157657Sflz				then echo -n "$_pref$_npid";
359157657Sflz				_pref=" ";
360157657Sflz				fi
36198186Sgordon				;;
36298186Sgordon			esac
36398186Sgordon		done'
36498186Sgordon
365114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
36698186Sgordon	eval $_proccheck
36798186Sgordon}
36898186Sgordon
36998186Sgordon#
37098186Sgordon# wait_for_pids pid [pid ...]
37198186Sgordon#	spins until none of the pids exist
37298186Sgordon#
37398186Sgordonwait_for_pids()
37498186Sgordon{
375126286Smtm	_list="$@"
37698186Sgordon	if [ -z "$_list" ]; then
37798186Sgordon		return
37898186Sgordon	fi
37998186Sgordon	_prefix=
38098186Sgordon	while true; do
38198186Sgordon		_nlist="";
38298186Sgordon		for _j in $_list; do
38398186Sgordon			if kill -0 $_j 2>/dev/null; then
38498186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
38598186Sgordon			fi
38698186Sgordon		done
38798186Sgordon		if [ -z "$_nlist" ]; then
38898186Sgordon			break
38978344Sobrien		fi
39098186Sgordon		_list=$_nlist
39198186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
39298186Sgordon		_prefix=", "
39398186Sgordon		sleep 2
39478344Sobrien	done
39598186Sgordon	if [ -n "$_prefix" ]; then
39698186Sgordon		echo "."
39798186Sgordon	fi
39878344Sobrien}
39978344Sobrien
40078344Sobrien#
40198186Sgordon# run_rc_command argument
40298186Sgordon#	Search for argument in the list of supported commands, which is:
40398186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
40498186Sgordon#	If there's a match, run ${argument}_cmd or the default method
40598186Sgordon#	(see below).
40678344Sobrien#
40798186Sgordon#	If argument has a given prefix, then change the operation as follows:
40898186Sgordon#		Prefix	Operation
40978344Sobrien#		------	---------
410175676Smtm#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
41198186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
412126303Smtm#		one	Set ${rcvar} to YES
413175676Smtm#		quiet	Don't output some diagnostics, and set rc_quiet=yes
41478344Sobrien#
41578344Sobrien#	The following globals are used:
41678344Sobrien#
41798186Sgordon#	Name		Needed	Purpose
41898186Sgordon#	----		------	-------
41978344Sobrien#	name		y	Name of script.
42078344Sobrien#
42178344Sobrien#	command		n	Full path to command.
42298186Sgordon#				Not needed if ${rc_arg}_cmd is set for
42378344Sobrien#				each keyword.
42478344Sobrien#
42578344Sobrien#	command_args	n	Optional args/shell directives for command.
42678344Sobrien#
42798186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
42898186Sgordon#				call check_{pidfile,process}() appropriately.
42998186Sgordon#
430197144Shrs#	desc		n	Description of script.
431197144Shrs#
43278344Sobrien#	extra_commands	n	List of extra commands supported.
43378344Sobrien#
43498186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
43598186Sgordon#				otherwise use check_process $command.
43698186Sgordon#				In either case, only check if $command is set.
43778344Sobrien#
43898186Sgordon#	procname	n	Process name to check for instead of $command.
43998186Sgordon#
44078344Sobrien#	rcvar		n	This is checked with checkyesno to determine
44178344Sobrien#				if the action should be run.
44278344Sobrien#
443157653Sflz#	${name}_program	n	Full path to command.
444157653Sflz#				Meant to be used in /etc/rc.conf to override
445157653Sflz#				${command}.
446157653Sflz#
44778344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
44898186Sgordon#				Requires /usr to be mounted.
44978344Sobrien#
45078344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
45178344Sobrien#				(if not using ${name}_chroot).
45278344Sobrien#
45378344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
45478344Sobrien#				NOTE:	$flags from the parent environment
45578344Sobrien#					can be used to override this.
45678344Sobrien#
45778344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
45878344Sobrien#
45978344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
46078344Sobrien#				using ${name}_chroot.
46198186Sgordon#				Requires /usr to be mounted.
46278344Sobrien#
46378344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
46498186Sgordon#				Requires /usr to be mounted.
46578344Sobrien#
46698186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
46798186Sgordon#				to run the chrooted ${command} with.
46898186Sgordon#				Requires /usr to be mounted.
46978344Sobrien#
47098186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
47178344Sobrien#				Otherwise, use default command (see below)
47278344Sobrien#
47398186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
47498186Sgordon#				${rc_arg}_cmd method in the default
47598186Sgordon#				operation (i.e, after checking for required
47698186Sgordon#				bits and process (non)existence).
47778344Sobrien#				If this completes with a non-zero exit code,
47898186Sgordon#				don't run ${rc_arg}_cmd.
47978344Sobrien#
48098186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
48198186Sgordon#				${rc_arg}_cmd method, if that method
48298186Sgordon#				returned a zero exit code.
48398186Sgordon#
48478344Sobrien#	required_dirs	n	If set, check for the existence of the given
485165565Syar#				directories before running a (re)start command.
48678344Sobrien#
48778344Sobrien#	required_files	n	If set, check for the readability of the given
488165565Syar#				files before running a (re)start command.
48978344Sobrien#
490165565Syar#	required_modules n	If set, ensure the given kernel modules are
491165565Syar#				loaded before running a (re)start command.
492165565Syar#				The check and possible loads are actually
493165565Syar#				done after start_precmd so that the modules
494165565Syar#				aren't loaded in vain, should the precmd
495165565Syar#				return a non-zero status to indicate a error.
496165565Syar#				If a word in the list looks like "foo:bar",
497165565Syar#				"foo" is the KLD file name and "bar" is the
498165565Syar#				module name.  If a word looks like "foo~bar",
499165565Syar#				"foo" is the KLD file name and "bar" is a
500165565Syar#				egrep(1) pattern matching the module name.
501165565Syar#				Otherwise the module name is assumed to be
502165565Syar#				the same as the KLD file name, which is most
503165565Syar#				common.  See load_kld().
504165565Syar#
50578344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
50678344Sobrien#				listed variables before running the default
50778344Sobrien#				(re)start command.
50878344Sobrien#
50998186Sgordon#	Default behaviour for a given argument, if no override method is
51098186Sgordon#	provided:
51178344Sobrien#
51298186Sgordon#	Argument	Default behaviour
51398186Sgordon#	--------	-----------------
51478344Sobrien#	start		if !running && checkyesno ${rcvar}
51578344Sobrien#				${command}
51678344Sobrien#
51778344Sobrien#	stop		if ${pidfile}
51898186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
51978344Sobrien#			else
52098186Sgordon#				rc_pid=$(check_process $command)
52198186Sgordon#			kill $sig_stop $rc_pid
52298186Sgordon#			wait_for_pids $rc_pid
52398186Sgordon#			($sig_stop defaults to TERM.)
52478344Sobrien#
52598186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
52698186Sgordon#			and doesn't wait_for_pids.
52778344Sobrien#			$sig_reload defaults to HUP.
528151685Syar#			Note that `reload' isn't provided by default,
529151685Syar#			it should be enabled via $extra_commands.
53078344Sobrien#
53178344Sobrien#	restart		Run `stop' then `start'.
53278344Sobrien#
53398186Sgordon#	status		Show if ${command} is running, etc.
53478344Sobrien#
53598186Sgordon#	poll		Wait for ${command} to exit.
53698186Sgordon#
53798186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
53898186Sgordon#
53998186Sgordon#	Variables available to methods, and after run_rc_command() has
54098186Sgordon#	completed:
54198186Sgordon#
54298186Sgordon#	Variable	Purpose
54398186Sgordon#	--------	-------
544126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
54598186Sgordon#			performed
54698186Sgordon#
54798186Sgordon#	rc_flags	Flags to start the default command with.
54898186Sgordon#			Defaults to ${name}_flags, unless overridden
54998186Sgordon#			by $flags from the environment.
55098186Sgordon#			This variable may be changed by the precmd method.
55198186Sgordon#
55298186Sgordon#	rc_pid		PID of command (if appropriate)
55398186Sgordon#
55498186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
55598186Sgordon#
55698186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
55798186Sgordon#
558175676Smtm#	rc_quiet	Not empty if "quiet" was provided
55998186Sgordon#
560175676Smtm#
56178344Sobrienrun_rc_command()
56278344Sobrien{
563116097Smtm	_return=0
56498186Sgordon	rc_arg=$1
56578344Sobrien	if [ -z "$name" ]; then
56698186Sgordon		err 3 'run_rc_command: $name is not set.'
56778344Sobrien	fi
56878344Sobrien
569132892Smtm	# Don't repeat the first argument when passing additional command-
570132892Smtm	# line arguments to the command subroutines.
571132892Smtm	#
572132892Smtm	shift 1
573132892Smtm	rc_extra_args="$*"
574132892Smtm
575126303Smtm	_rc_prefix=
57698186Sgordon	case "$rc_arg" in
57778344Sobrien	fast*)				# "fast" prefix; don't check pid
57898186Sgordon		rc_arg=${rc_arg#fast}
57998186Sgordon		rc_fast=yes
580175676Smtm		rc_quiet=yes
58178344Sobrien		;;
582126303Smtm	force*)				# "force prefix; always run
58398186Sgordon		rc_force=yes
584126303Smtm		_rc_prefix=force
585126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
58678344Sobrien		if [ -n "${rcvar}" ]; then
58778344Sobrien			eval ${rcvar}=YES
58878344Sobrien		fi
58978344Sobrien		;;
590126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
591126303Smtm		_rc_prefix=one
592126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
593126303Smtm		if [ -n "${rcvar}" ]; then
594126303Smtm			eval ${rcvar}=YES
595126303Smtm		fi
596126303Smtm		;;
597175676Smtm	quiet*)				# "quiet" prefix; omit some messages
598175676Smtm		_rc_prefix=quiet
599175676Smtm		rc_arg=${rc_arg#${_rc_prefix}}
600175676Smtm		rc_quiet=yes
601175676Smtm		;;
60278344Sobrien	esac
60378344Sobrien
604161530Sflz	eval _override_command=\$${name}_program
605197144Shrs	command=${command:-${_override_command}}
606161530Sflz
60778344Sobrien	_keywords="start stop restart rcvar $extra_commands"
60898186Sgordon	rc_pid=
60978344Sobrien	_pidcmd=
61098186Sgordon	_procname=${procname:-${command}}
61198186Sgordon
612131135Smtm					# setup pid check command
613131135Smtm	if [ -n "$_procname" ]; then
61478344Sobrien		if [ -n "$pidfile" ]; then
61598186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
61698186Sgordon		else
61798186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
61878344Sobrien		fi
61978344Sobrien		if [ -n "$_pidcmd" ]; then
62098186Sgordon			_keywords="${_keywords} status poll"
62178344Sobrien		fi
62278344Sobrien	fi
62378344Sobrien
62498186Sgordon	if [ -z "$rc_arg" ]; then
625150796Syar		rc_usage $_keywords
62678344Sobrien	fi
62778344Sobrien
62878344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
62998186Sgordon		rc_flags=$flags
63078344Sobrien	else
63198186Sgordon		eval rc_flags=\$${name}_flags
63278344Sobrien	fi
63398186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
63498186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
63598186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
63678344Sobrien
63798186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
638124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
63998186Sgordon			unset _user
64098186Sgordon		fi
64198186Sgordon	fi
64298186Sgordon
643179870Smtm	eval $_pidcmd			# determine the pid if necessary
644179870Smtm
645179870Smtm	for _elem in $_keywords; do
646179870Smtm		if [ "$_elem" != "$rc_arg" ]; then
647179870Smtm			continue
648179870Smtm		fi
64978344Sobrien					# if ${rcvar} is set, and $1 is not
65098186Sgordon					# "rcvar", then run
65178344Sobrien					#	checkyesno ${rcvar}
65278344Sobrien					# and return if that failed
65378344Sobrien					#
654179870Smtm		if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
655179870Smtm			if ! checkyesno ${rcvar}; then
656179870Smtm				if [ -n "${rc_quiet}" ]; then
657179870Smtm					return 0
658179870Smtm				fi
659179870Smtm				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
660179870Smtm				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
661179870Smtm				echo "instead of '${rc_arg}'."
662175676Smtm				return 0
663175676Smtm			fi
66478344Sobrien		fi
66578344Sobrien
66678344Sobrien					# if there's a custom ${XXX_cmd},
66778344Sobrien					# run that instead of the default
66878344Sobrien					#
669165565Syar		eval _cmd=\$${rc_arg}_cmd \
670165565Syar		     _precmd=\$${rc_arg}_precmd \
671165565Syar		     _postcmd=\$${rc_arg}_postcmd
672165565Syar
67378344Sobrien		if [ -n "$_cmd" ]; then
674165565Syar			_run_rc_precmd || return 1
675165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
676165565Syar			_run_rc_postcmd
677116097Smtm			return $_return
67878344Sobrien		fi
67978344Sobrien
68098186Sgordon		case "$rc_arg" in	# default operations...
68178344Sobrien
68278344Sobrien		status)
683165565Syar			_run_rc_precmd || return 1
68498186Sgordon			if [ -n "$rc_pid" ]; then
68598186Sgordon				echo "${name} is running as pid $rc_pid."
68678344Sobrien			else
68778344Sobrien				echo "${name} is not running."
68878344Sobrien				return 1
68978344Sobrien			fi
690165565Syar			_run_rc_postcmd
69178344Sobrien			;;
69278344Sobrien
69378344Sobrien		start)
694131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
695157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
696153152Syar				return 1
69778344Sobrien			fi
69878344Sobrien
699167413Syar			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
700160667Syar				warn "run_rc_command: cannot run $command"
701153152Syar				return 1
70278344Sobrien			fi
70378344Sobrien
704179946Smtm			if ! _run_rc_precmd; then
705179946Smtm				warn "failed precmd routine for ${name}"
706179946Smtm				return 1
707179946Smtm			fi
70878344Sobrien
709160668Syar					# setup the full command to run
71078344Sobrien					#
711179946Smtm			[ -z "${rc_quiet}" ] && echo "Starting ${name}."
71278344Sobrien			if [ -n "$_chroot" ]; then
71378344Sobrien				_doit="\
71478344Sobrien${_nice:+nice -n $_nice }\
71578344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
71698186Sgordon$_chroot $command $rc_flags $command_args"
71778344Sobrien			else
71878344Sobrien				_doit="\
719161396Syar${_chdir:+cd $_chdir && }\
72098186Sgordon$command $rc_flags $command_args"
72198186Sgordon				if [ -n "$_user" ]; then
72298186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
72398186Sgordon				fi
724161396Syar				if [ -n "$_nice" ]; then
725161396Syar					if [ -z "$_user" ]; then
726161396Syar						_doit="sh -c \"$_doit\""
727161396Syar					fi	
728161396Syar					_doit="nice -n $_nice $_doit"
729161396Syar				fi
73078344Sobrien			fi
73198186Sgordon
732165565Syar					# run the full command
73398186Sgordon					#
734179946Smtm			if ! _run_rc_doit "$_doit"; then
735179946Smtm				warn "failed to start ${name}"
736179946Smtm				return 1
737179946Smtm			fi
73898186Sgordon
73998186Sgordon					# finally, run postcmd
74098186Sgordon					#
741165565Syar			_run_rc_postcmd
74278344Sobrien			;;
74378344Sobrien
74478344Sobrien		stop)
74598186Sgordon			if [ -z "$rc_pid" ]; then
746153152Syar				[ -n "$rc_fast" ] && return 0
747165565Syar				_run_rc_notrunning
748153152Syar				return 1
74978344Sobrien			fi
75078344Sobrien
751165565Syar			_run_rc_precmd || return 1
75298186Sgordon
75398186Sgordon					# send the signal to stop
75498186Sgordon					#
75578344Sobrien			echo "Stopping ${name}."
756165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
757165565Syar			_run_rc_doit "$_doit" || return 1
75898186Sgordon
75998186Sgordon					# wait for the command to exit,
76098186Sgordon					# and run postcmd.
76198186Sgordon			wait_for_pids $rc_pid
762165565Syar
763165565Syar			_run_rc_postcmd
76478344Sobrien			;;
76578344Sobrien
76678344Sobrien		reload)
76798186Sgordon			if [ -z "$rc_pid" ]; then
768165565Syar				_run_rc_notrunning
769153152Syar				return 1
77078344Sobrien			fi
771165565Syar
772165565Syar			_run_rc_precmd || return 1
773165565Syar
774165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
775165565Syar			_run_rc_doit "$_doit" || return 1
776165565Syar
777165565Syar			_run_rc_postcmd
77878344Sobrien			;;
77978344Sobrien
78078344Sobrien		restart)
78178344Sobrien					# prevent restart being called more
78278344Sobrien					# than once by any given script
78378344Sobrien					#
784126285Smtm			if ${_rc_restart_done:-false}; then
78578344Sobrien				return 0
78678344Sobrien			fi
787126285Smtm			_rc_restart_done=true
78878344Sobrien
789165565Syar			_run_rc_precmd || return 1
790165565Syar
791165565Syar			# run those in a subshell to keep global variables
792152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
793165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
794165565Syar			_return=$?
795165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
79698186Sgordon
797165565Syar			_run_rc_postcmd
79878344Sobrien			;;
79978344Sobrien
80098186Sgordon		poll)
801165565Syar			_run_rc_precmd || return 1
80298186Sgordon			if [ -n "$rc_pid" ]; then
80398186Sgordon				wait_for_pids $rc_pid
80498186Sgordon			fi
805165565Syar			_run_rc_postcmd
80698186Sgordon			;;
80798186Sgordon
80878344Sobrien		rcvar)
809197144Shrs			echo -n "# $name"
810197144Shrs			if [ -n "$desc" ]; then
811197144Shrs				echo " : $desc"
812197144Shrs			else
813197144Shrs				echo ""
814197144Shrs			fi
815197144Shrs			echo "#"
816197144Shrs			# Get unique vars in $rcvar $rcvars
817197144Shrs			for _v in $rcvar $rcvars; do
818197144Shrs				case $v in
819197144Shrs				$_v\ *|\ *$_v|*\ $_v\ *) ;;
820197144Shrs				*)	v="${v# } $_v" ;;
821197144Shrs				esac
822197144Shrs			done
823197144Shrs
824197144Shrs			# Display variables.
825197144Shrs			for _v in $v; do
826197144Shrs				if [ -z "$_v" ]; then
827197144Shrs					continue
82878344Sobrien				fi
829197144Shrs
830197144Shrs				eval _desc=\$${_v}_desc
831197144Shrs				eval _defval=\$${_v}_defval
832197144Shrs				_h="-"
833197144Shrs
834197144Shrs				eval echo \"$_v=\\\"\$$_v\\\"\"
835197144Shrs				# decode multiple lines of _desc
836197144Shrs				while [ -n "$_desc" ]; do
837197144Shrs					case $_desc in
838197144Shrs					*^^*)
839197144Shrs						echo "# $_h ${_desc%%^^*}"
840197144Shrs						_desc=${_desc#*^^}
841197144Shrs						_h=" "
842197144Shrs						;;
843197144Shrs					*)
844197144Shrs						echo "# $_h ${_desc}"
845197144Shrs						break
846197144Shrs						;;
847197144Shrs					esac
848197144Shrs				done
849197144Shrs				echo "#   (default: \"$_defval\")"
850197144Shrs			done
851197144Shrs			echo ""
85278344Sobrien			;;
85378344Sobrien
85478344Sobrien		*)
855150796Syar			rc_usage $_keywords
85678344Sobrien			;;
85778344Sobrien
85878344Sobrien		esac
859116097Smtm		return $_return
86078344Sobrien	done
86178344Sobrien
86298186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
863150796Syar	rc_usage $_keywords
864153152Syar	# not reached
86578344Sobrien}
86678344Sobrien
86778344Sobrien#
868165565Syar# Helper functions for run_rc_command: common code.
869165565Syar# They use such global variables besides the exported rc_* ones:
870165565Syar#
871165565Syar#	name	       R/W
872165565Syar#	------------------
873165565Syar#	_precmd		R
874165565Syar#	_postcmd	R
875165565Syar#	_return		W
876165565Syar#
877165565Syar_run_rc_precmd()
878165565Syar{
879165565Syar	check_required_before "$rc_arg" || return 1
880165565Syar
881165565Syar	if [ -n "$_precmd" ]; then
882165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
883165565Syar		eval "$_precmd $rc_extra_args"
884165565Syar		_return=$?
885165565Syar
886165565Syar		# If precmd failed and force isn't set, request exit.
887165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
888165565Syar			return 1
889165565Syar		fi
890165565Syar	fi
891165565Syar
892165565Syar	check_required_after "$rc_arg" || return 1
893165565Syar
894165565Syar	return 0
895165565Syar}
896165565Syar
897165565Syar_run_rc_postcmd()
898165565Syar{
899165565Syar	if [ -n "$_postcmd" ]; then
900165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
901165565Syar		eval "$_postcmd $rc_extra_args"
902165565Syar		_return=$?
903165565Syar	fi
904165565Syar	return 0
905165565Syar}
906165565Syar
907165565Syar_run_rc_doit()
908165565Syar{
909165565Syar	debug "run_rc_command: doit: $*"
910165565Syar	eval "$@"
911165565Syar	_return=$?
912165565Syar
913165565Syar	# If command failed and force isn't set, request exit.
914165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
915165565Syar		return 1
916165565Syar	fi
917165565Syar
918165565Syar	return 0
919165565Syar}
920165565Syar
921165565Syar_run_rc_notrunning()
922165565Syar{
923165565Syar	local _pidmsg
924165565Syar
925165565Syar	if [ -n "$pidfile" ]; then
926165565Syar		_pidmsg=" (check $pidfile)."
927165565Syar	else
928165565Syar		_pidmsg=
929165565Syar	fi
930165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
931165565Syar}
932165565Syar
933165565Syar_run_rc_killcmd()
934165565Syar{
935165565Syar	local _cmd
936165565Syar
937165565Syar	_cmd="kill -$1 $rc_pid"
938165565Syar	if [ -n "$_user" ]; then
939165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
940165565Syar	fi
941165565Syar	echo "$_cmd"
942165565Syar}
943165565Syar
944165565Syar#
94578344Sobrien# run_rc_script file arg
94678344Sobrien#	Start the script `file' with `arg', and correctly handle the
94778344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
94898186Sgordon#	sourced into the current environment.  If `file' appears to be
94998186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
95098186Sgordon#	executable run as a child process.
95178344Sobrien#
95278344Sobrienrun_rc_script()
95378344Sobrien{
95478344Sobrien	_file=$1
95578344Sobrien	_arg=$2
95678344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
95778344Sobrien		err 3 'USAGE: run_rc_script file arg'
95878344Sobrien	fi
95978344Sobrien
96098186Sgordon	unset	name command command_args command_interpreter \
96198186Sgordon		extra_commands pidfile procname \
962197144Shrs		rcvar rcvars rcvars_obsolete required_dirs required_files \
963197144Shrs		required_vars
96498186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
96598186Sgordon
96678344Sobrien	case "$_file" in
967193118Sdougb	/etc/rc.d/*.sh)			# no longer allowed in the base
968193118Sdougb		warn "Ignoring old-style startup script $_file"
96978344Sobrien		;;
970153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
97198186Sgordon		warn "Ignoring scratch file $_file"
97298186Sgordon		;;
97378344Sobrien	*)				# run in subshell
97498186Sgordon		if [ -x $_file ]; then
97598186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
976146490Sschweikh				set $_arg; . $_file
97798186Sgordon			else
978130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
979130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
980184317Sthompsa				  trap "echo Script $_file running" 29
981146490Sschweikh				  set $_arg; . $_file )
98298186Sgordon			fi
98398186Sgordon		fi
98478344Sobrien		;;
98578344Sobrien	esac
98678344Sobrien}
98778344Sobrien
98878344Sobrien#
989157653Sflz# load_rc_config name
990157653Sflz#	Source in the configuration file for a given name.
99178344Sobrien#
99278344Sobrienload_rc_config()
99378344Sobrien{
994197144Shrs	local _name _var _defval _v _msg _new
995157653Sflz	_name=$1
996157653Sflz	if [ -z "$_name" ]; then
997157653Sflz		err 3 'USAGE: load_rc_config name'
99878344Sobrien	fi
99978344Sobrien
1000126285Smtm	if ${_rc_conf_loaded:-false}; then
1001126285Smtm		:
1002126285Smtm	else
100398186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
100498186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
100598186Sgordon			. /etc/defaults/rc.conf
100698186Sgordon			source_rc_confs
100798186Sgordon		elif [ -r /etc/rc.conf ]; then
100898186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
100998186Sgordon			. /etc/rc.conf
101098186Sgordon		fi
1011126285Smtm		_rc_conf_loaded=true
101298186Sgordon	fi
1013161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
1014157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
1015161530Sflz		. /etc/rc.conf.d/"$_name"
1016157653Sflz	fi
1017179872Smtm
1018179872Smtm	# Old variable names support
1019179872Smtm	#
1020179872Smtm	[ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1021197144Shrs
1022197144Shrs	# Set defaults if defined.
1023197144Shrs	for _var in $rcvar $rcvars; do
1024197144Shrs		_defval=`eval echo "\\\$${_var}_defval"`
1025197144Shrs		if [ -n "$_defval" ]; then
1026197144Shrs			eval : \${$_var:=\$${_var}_defval}
1027197144Shrs		fi
1028197144Shrs	done
1029197144Shrs
1030197144Shrs	# check obsolete rc.conf variables
1031197144Shrs	for _var in $rcvars_obsolete; do
1032197144Shrs		_v=`eval echo \\$$_var`
1033197144Shrs		_msg=`eval echo \\$${_var}_obsolete_msg`
1034197144Shrs		_new=`eval echo \\$${_var}_newvar`
1035197144Shrs		case $_v in
1036197144Shrs		"")
1037197144Shrs			;;
1038197144Shrs		*)
1039197144Shrs			if [ -z "$_new" ]; then
1040197144Shrs				_msg="Ignored."
1041197144Shrs			else
1042197144Shrs				eval $_new=\"\$$_var\"
1043197144Shrs				if [ -z "$_msg" ]; then
1044197144Shrs					_msg="Use \$$_new instead."
1045197144Shrs				fi
1046197144Shrs			fi
1047197144Shrs			warn "\$$_var is obsolete.  $_msg"
1048197144Shrs			;;
1049197144Shrs		esac
1050197144Shrs	done
105178344Sobrien}
1052157473Sflz  
1053157473Sflz#
1054157653Sflz# load_rc_config_var name var
1055157653Sflz#	Read the rc.conf(5) var for name and set in the
1056157473Sflz#	current shell, using load_rc_config in a subshell to prevent
1057157473Sflz#	unwanted side effects from other variable assignments.
1058157473Sflz#
1059157473Sflzload_rc_config_var()
1060157473Sflz{
1061157473Sflz	if [ $# -ne 2 ]; then
1062157653Sflz		err 3 'USAGE: load_rc_config_var name var'
1063157473Sflz	fi
1064157473Sflz	eval $(eval '(
1065157473Sflz		load_rc_config '$1' >/dev/null;
1066157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1067157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
1068157473Sflz		fi
1069157473Sflz	)' )
1070157473Sflz}
107178344Sobrien
107278344Sobrien#
107378344Sobrien# rc_usage commands
107478344Sobrien#	Print a usage string for $0, with `commands' being a list of
107578344Sobrien#	valid commands.
107678344Sobrien#
107778344Sobrienrc_usage()
107878344Sobrien{
1079126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
108078344Sobrien
108178344Sobrien	_sep=
1082126286Smtm	for _elem; do
108378344Sobrien		echo -n 1>&2 "$_sep$_elem"
108478344Sobrien		_sep="|"
108578344Sobrien	done
108678344Sobrien	echo 1>&2 ")"
108778344Sobrien	exit 1
108878344Sobrien}
108978344Sobrien
109078344Sobrien#
109178344Sobrien# err exitval message
109278344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
109378344Sobrien#
109478344Sobrienerr()
109578344Sobrien{
109678344Sobrien	exitval=$1
109778344Sobrien	shift
109878344Sobrien
1099106643Sgordon	if [ -x /usr/bin/logger ]; then
1100106643Sgordon		logger "$0: ERROR: $*"
1101106643Sgordon	fi
1102106643Sgordon	echo 1>&2 "$0: ERROR: $*"
110378344Sobrien	exit $exitval
110478344Sobrien}
110578344Sobrien
110678344Sobrien#
110778344Sobrien# warn message
110878344Sobrien#	Display message to stderr and log to the syslog.
110978344Sobrien#
111078344Sobrienwarn()
111178344Sobrien{
1112106643Sgordon	if [ -x /usr/bin/logger ]; then
1113106643Sgordon		logger "$0: WARNING: $*"
1114106643Sgordon	fi
1115106643Sgordon	echo 1>&2 "$0: WARNING: $*"
111678344Sobrien}
111798186Sgordon
111898186Sgordon#
111998186Sgordon# info message
112098186Sgordon#	Display informational message to stdout and log to syslog.
112198186Sgordon#
112298186Sgordoninfo()
112398186Sgordon{
1124119170Smtm	case ${rc_info} in
1125119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1126119170Smtm		if [ -x /usr/bin/logger ]; then
1127119170Smtm			logger "$0: INFO: $*"
1128119170Smtm		fi
1129119170Smtm		echo "$0: INFO: $*"
1130119170Smtm		;;
1131119170Smtm	esac
113298186Sgordon}
113398186Sgordon
113498186Sgordon#
113598186Sgordon# debug message
1136106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
113798186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
113898186Sgordon#	function.
113998186Sgordon#
114098186Sgordondebug()
114198186Sgordon{
114298186Sgordon	case ${rc_debug} in
114398186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1144106700Sgordon		if [ -x /usr/bin/logger ]; then
1145162947Syar			logger "$0: DEBUG: $*"
1146106700Sgordon		fi
1147146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
114898186Sgordon		;;
114998186Sgordon	esac
115098186Sgordon}
115198186Sgordon
115298186Sgordon#
115398186Sgordon# backup_file action file cur backup
115498186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
115598186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
115698186Sgordon#
115798186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
115898186Sgordon#	which can be either YES or NO.
115998186Sgordon#
116098186Sgordon#	The `action' keyword can be one of the following:
116198186Sgordon#
116298186Sgordon#	add		`file' is now being backed up (and is possibly
116398186Sgordon#			being reentered into the backups system).  `cur'
116498186Sgordon#			is created and RCS files, if necessary, are
116598186Sgordon#			created as well.
116698186Sgordon#
116798186Sgordon#	update		`file' has changed and needs to be backed up.
116898186Sgordon#			If `cur' exists, it is copied to to `back' or
116998186Sgordon#			checked into RCS (if the repository file is old),
117098186Sgordon#			and then `file' is copied to `cur'.  Another RCS
117198186Sgordon#			check in done here if RCS is being used.
117298186Sgordon#
117398186Sgordon#	remove		`file' is no longer being tracked by the backups
117498186Sgordon#			system.  If RCS is not being used, `cur' is moved
117598186Sgordon#			to `back', otherwise an empty file is checked in,
117698186Sgordon#			and then `cur' is removed.
117798186Sgordon#
117898186Sgordon#
117998186Sgordonbackup_file()
118098186Sgordon{
118198186Sgordon	_action=$1
118298186Sgordon	_file=$2
118398186Sgordon	_cur=$3
118498186Sgordon	_back=$4
118598186Sgordon
118698186Sgordon	if checkyesno backup_uses_rcs; then
118798186Sgordon		_msg0="backup archive"
118898186Sgordon		_msg1="update"
118998186Sgordon
119098186Sgordon		# ensure that history file is not locked
119198186Sgordon		if [ -f $_cur,v ]; then
119298186Sgordon			rcs -q -u -U -M $_cur
119398186Sgordon		fi
119498186Sgordon
119598186Sgordon		# ensure after switching to rcs that the
119698186Sgordon		# current backup is not lost
119798186Sgordon		if [ -f $_cur ]; then
119898186Sgordon			# no archive, or current newer than archive
119998186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
120098186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
120198186Sgordon				rcs -q -kb -U $_cur
120298186Sgordon				co -q -f -u $_cur
120398186Sgordon			fi
120498186Sgordon		fi
120598186Sgordon
120698186Sgordon		case $_action in
120798186Sgordon		add|update)
120898186Sgordon			cp -p $_file $_cur
120998186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
121098186Sgordon			rcs -q -kb -U $_cur
121198186Sgordon			co -q -f -u $_cur
121298186Sgordon			chown root:wheel $_cur $_cur,v
121398186Sgordon			;;
121498186Sgordon		remove)
121598186Sgordon			cp /dev/null $_cur
121698186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
121798186Sgordon			rcs -q -kb -U $_cur
121898186Sgordon			chown root:wheel $_cur $_cur,v
121998186Sgordon			rm $_cur
122098186Sgordon			;;
122198186Sgordon		esac
122298186Sgordon	else
122398186Sgordon		case $_action in
122498186Sgordon		add|update)
122598186Sgordon			if [ -f $_cur ]; then
122698186Sgordon				cp -p $_cur $_back
122798186Sgordon			fi
122898186Sgordon			cp -p $_file $_cur
122998186Sgordon			chown root:wheel $_cur
123098186Sgordon			;;
123198186Sgordon		remove)
123298186Sgordon			mv -f $_cur $_back
123398186Sgordon			;;
123498186Sgordon		esac
123598186Sgordon	fi
123698186Sgordon}
1237119166Smtm
1238123344Smtm# make_symlink src link
1239123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1240123344Smtm#	directory in which link is to be created does not exist
1241123344Smtm#	a warning will be displayed and an error will be returned.
1242123344Smtm#	Returns 0 on sucess, 1 otherwise.
1243119166Smtm#
1244123344Smtmmake_symlink()
1245119166Smtm{
1246123344Smtm	local src link linkdir _me
1247123344Smtm	src="$1"
1248123344Smtm	link="$2"
1249123344Smtm	linkdir="`dirname $link`"
1250123344Smtm	_me="make_symlink()"
1251119166Smtm
1252123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1253123344Smtm		warn "$_me: requires two arguments."
1254119166Smtm		return 1
1255119166Smtm	fi
1256123344Smtm	if [ ! -d "$linkdir" ]; then
1257160667Syar		warn "$_me: the directory $linkdir does not exist."
1258119166Smtm		return 1
1259119166Smtm	fi
1260146490Sschweikh	if ! ln -sf $src $link; then
1261123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1262119166Smtm		return 1
1263119166Smtm	fi
1264119166Smtm	return 0
1265119166Smtm}
1266119166Smtm
1267119166Smtm# devfs_rulesets_from_file file
1268119166Smtm#	Reads a set of devfs commands from file, and creates
1269119166Smtm#	the specified rulesets with their rules. Returns non-zero
1270119166Smtm#	if there was an error.
1271119166Smtm#
1272119166Smtmdevfs_rulesets_from_file()
1273119166Smtm{
1274119166Smtm	local file _err _me
1275119166Smtm	file="$1"
1276119166Smtm	_me="devfs_rulesets_from_file"
1277119166Smtm	_err=0
1278119166Smtm
1279119166Smtm	if [ -z "$file" ]; then
1280119166Smtm		warn "$_me: you must specify a file"
1281119166Smtm		return 1
1282119166Smtm	fi
1283119166Smtm	if [ ! -e "$file" ]; then
1284119166Smtm		debug "$_me: no such file ($file)"
1285119166Smtm		return 0
1286119166Smtm	fi
1287119166Smtm	debug "reading rulesets from file ($file)"
1288119166Smtm	{ while read line
1289119166Smtm	do
1290119166Smtm		case $line in
1291119166Smtm		\#*)
1292119166Smtm			continue
1293119166Smtm			;;
1294119166Smtm		\[*\]*)
1295119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1296119166Smtm			if [ -z "$rulenum" ]; then
1297119166Smtm				warn "$_me: cannot extract rule number ($line)"
1298119166Smtm				_err=1
1299119166Smtm				break
1300119166Smtm			fi
1301119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1302119166Smtm			if [ -z "$rulename" ]; then
1303119166Smtm				warn "$_me: cannot extract rule name ($line)"
1304119166Smtm				_err=1
1305119166Smtm				break;
1306119166Smtm			fi
1307119166Smtm			eval $rulename=\$rulenum
1308119166Smtm			debug "found ruleset: $rulename=$rulenum"
1309146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1310119166Smtm				_err=1
1311119166Smtm				break
1312119166Smtm			fi
1313119166Smtm			;;
1314119166Smtm		*)
1315119166Smtm			rulecmd="${line%%"\#*"}"
1316119166Smtm			# evaluate the command incase it includes
1317119166Smtm			# other rules
1318119166Smtm			if [ -n "$rulecmd" ]; then
1319119166Smtm				debug "adding rule ($rulecmd)"
1320119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1321119166Smtm				then
1322119166Smtm					_err=1
1323119166Smtm					break
1324119166Smtm				fi
1325119166Smtm			fi
1326119166Smtm			;;
1327119166Smtm		esac
1328119166Smtm		if [ $_err -ne 0 ]; then
1329119166Smtm			debug "error in $_me"
1330119166Smtm			break
1331119166Smtm		fi
1332119166Smtm	done } < $file
1333119166Smtm	return $_err
1334119166Smtm}
1335119166Smtm
1336119166Smtm# devfs_init_rulesets
1337119166Smtm#	Initializes rulesets from configuration files. Returns
1338119166Smtm#	non-zero if there was an error.
1339119166Smtm#
1340119166Smtmdevfs_init_rulesets()
1341119166Smtm{
1342119166Smtm	local file _me
1343119166Smtm	_me="devfs_init_rulesets"
1344119166Smtm
1345119166Smtm	# Go through this only once
1346119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1347119166Smtm		debug "$_me: devfs rulesets already initialized"
1348119166Smtm		return
1349119166Smtm	fi
1350146490Sschweikh	for file in $devfs_rulesets; do
1351119166Smtm		devfs_rulesets_from_file $file || return 1
1352119166Smtm	done
1353119166Smtm	devfs_rulesets_init=1
1354119166Smtm	debug "$_me: devfs rulesets initialized"
1355119166Smtm	return 0
1356119166Smtm}
1357119166Smtm
1358119166Smtm# devfs_set_ruleset ruleset [dir]
1359151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1360119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1361119166Smtm#	Returns non-zero if it could not set it successfully.
1362119166Smtm#
1363119166Smtmdevfs_set_ruleset()
1364119166Smtm{
1365119166Smtm	local devdir rs _me
1366119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1367119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1368119166Smtm	_me="devfs_set_ruleset"
1369119166Smtm
1370119166Smtm	if [ -z "$rs" ]; then
1371119166Smtm		warn "$_me: you must specify a ruleset number"
1372119166Smtm		return 1
1373119166Smtm	fi
1374119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1375146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1376119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1377119166Smtm		return 1
1378119166Smtm	fi
1379119166Smtm	return 0
1380119166Smtm}
1381119166Smtm
1382119166Smtm# devfs_apply_ruleset ruleset [dir]
1383119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1384119166Smtm#	The ruleset argument must be a ruleset name as specified
1385119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1386119166Smtm#	if it could not apply the ruleset.
1387119166Smtm#
1388119166Smtmdevfs_apply_ruleset()
1389119166Smtm{
1390119166Smtm	local devdir rs _me
1391119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1392119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1393119166Smtm	_me="devfs_apply_ruleset"
1394119166Smtm
1395119166Smtm	if [ -z "$rs" ]; then
1396119166Smtm		warn "$_me: you must specify a ruleset"
1397119166Smtm		return 1
1398119166Smtm	fi
1399119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1400146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1401119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1402119166Smtm		return 1
1403119166Smtm	fi
1404119166Smtm	return 0
1405119166Smtm}
1406119166Smtm
1407119166Smtm# devfs_domount dir [ruleset]
1408119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1409119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1410119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1411119166Smtm#
1412119166Smtmdevfs_domount()
1413119166Smtm{
1414119166Smtm	local devdir rs _me
1415119166Smtm	devdir="$1"
1416119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1417119166Smtm	_me="devfs_domount()"
1418119166Smtm
1419119166Smtm	if [ -z "$devdir" ]; then
1420119166Smtm		warn "$_me: you must specify a mount-point"
1421119166Smtm		return 1
1422119166Smtm	fi
1423119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1424146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1425119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1426119166Smtm		return 1
1427119166Smtm	fi
1428119166Smtm	if [ -n "$rs" ]; then
1429119166Smtm		devfs_init_rulesets
1430119166Smtm		devfs_set_ruleset $rs $devdir
1431124797Scperciva		devfs -m $devdir rule applyset
1432119166Smtm	fi
1433119166Smtm	return 0
1434119166Smtm}
1435119166Smtm
1436119166Smtm# devfs_mount_jail dir [ruleset]
1437119166Smtm#	Mounts a devfs file system appropriate for jails
1438119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1439119166Smtm#	it names will be used instead.  If present, ruleset must
1440119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1441119166Smtm#	This function returns non-zero if an error occurs.
1442119166Smtm#
1443119166Smtmdevfs_mount_jail()
1444119166Smtm{
1445119166Smtm	local jdev rs _me
1446119166Smtm	jdev="$1"
1447119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1448119166Smtm	_me="devfs_mount_jail"
1449119166Smtm
1450119166Smtm	devfs_init_rulesets
1451146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1452119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1453119166Smtm		return 1
1454119166Smtm	fi
1455119166Smtm	return 0
1456119166Smtm}
1457127345Sbrooks
1458127345Sbrooks# Provide a function for normalizing the mounting of memory
1459127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1460127345Sbrooks# as close as possible between 5-current and 4-stable.
1461127345Sbrooks#   $1 = size
1462127345Sbrooks#   $2 = mount point
1463137451Skeramida#   $3 = (optional) extra mdmfs flags
1464146490Sschweikhmount_md()
1465146490Sschweikh{
1466127345Sbrooks	if [ -n "$3" ]; then
1467137451Skeramida		flags="$3"
1468127345Sbrooks	fi
1469149421Syar	/sbin/mdmfs $flags -s $1 md $2
1470127345Sbrooks}
1471131550Scperciva
1472159828Syar# Code common to scripts that need to load a kernel module
1473159828Syar# if it isn't in the kernel yet. Syntax:
1474160666Syar#   load_kld [-e regex] [-m module] file
1475159828Syar# where -e or -m chooses the way to check if the module
1476159828Syar# is already loaded:
1477160666Syar#   regex is egrep'd in the output from `kldstat -v',
1478160666Syar#   module is passed to `kldstat -m'.
1479160666Syar# The default way is as though `-m file' were specified.
1480159828Syarload_kld()
1481159828Syar{
1482159828Syar	local _loaded _mod _opt _re
1483159828Syar
1484159828Syar	while getopts "e:m:" _opt; do
1485159828Syar		case "$_opt" in
1486159828Syar		e) _re="$OPTARG" ;;
1487159828Syar		m) _mod="$OPTARG" ;;
1488160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1489159828Syar		esac
1490159828Syar	done
1491159828Syar	shift $(($OPTIND - 1))
1492160666Syar	if [ $# -ne 1 ]; then
1493160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1494160666Syar	fi
1495159828Syar	_mod=${_mod:-$1}
1496159828Syar	_loaded=false
1497159828Syar	if [ -n "$_re" ]; then
1498159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1499159828Syar			_loaded=true
1500159828Syar		fi
1501159828Syar	else
1502159828Syar		if kldstat -q -m "$_mod"; then
1503159828Syar			_loaded=true
1504159828Syar		fi
1505159828Syar	fi
1506159828Syar	if ! $_loaded; then
1507159828Syar		if ! kldload "$1"; then
1508159828Syar			warn "Unable to load kernel module $1"
1509159828Syar			return 1
1510160666Syar		else
1511160666Syar			info "$1 kernel module loaded."
1512159828Syar		fi
1513160666Syar	else
1514160666Syar		debug "load_kld: $1 kernel module already loaded."
1515159828Syar	fi
1516159828Syar	return 0
1517159828Syar}
1518159828Syar
1519149049Spjd# ltr str src dst
1520149049Spjd#	Change every $src in $str to $dst.
1521149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1522149049Spjd#	awk(1).
1523149049Spjdltr()
1524149049Spjd{
1525149049Spjd	local _str _src _dst _out _com
1526149049Spjd	_str=$1
1527149049Spjd	_src=$2
1528149049Spjd	_dst=$3
1529149049Spjd	_out=""
1530149049Spjd
1531149049Spjd	IFS=${_src}
1532149049Spjd	for _com in ${_str}; do
1533149049Spjd		if [ -z "${_out}" ]; then
1534149049Spjd			_out="${_com}"
1535149049Spjd		else
1536149049Spjd			_out="${_out}${_dst}${_com}"
1537149049Spjd		fi
1538149049Spjd	done
1539149049Spjd	echo "${_out}"
1540149049Spjd}
1541149049Spjd
1542149050Spjd# Creates a list of providers for GELI encryption.
1543149050Spjdgeli_make_list()
1544149050Spjd{
1545149050Spjd	local devices devices2
1546149050Spjd	local provider mountpoint type options rest
1547149050Spjd
1548149050Spjd	# Create list of GELI providers from fstab.
1549149050Spjd	while read provider mountpoint type options rest ; do
1550155570Sflz		case ":${options}" in
1551155570Sflz		:*noauto*)
1552155570Sflz			noauto=yes
1553155570Sflz			;;
1554155570Sflz		*)
1555155570Sflz			noauto=no
1556155570Sflz			;;
1557155570Sflz		esac
1558155570Sflz
1559149050Spjd		case ":${provider}" in
1560149050Spjd		:#*)
1561149050Spjd			continue
1562149050Spjd			;;
1563149050Spjd		*.eli)
1564149050Spjd			# Skip swap devices.
1565155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1566149050Spjd				continue
1567149050Spjd			fi
1568149050Spjd			devices="${devices} ${provider}"
1569149050Spjd			;;
1570149050Spjd		esac
1571149050Spjd	done < /etc/fstab
1572149050Spjd
1573149050Spjd	# Append providers from geli_devices.
1574149050Spjd	devices="${devices} ${geli_devices}"
1575149050Spjd
1576149050Spjd	for provider in ${devices}; do
1577149050Spjd		provider=${provider%.eli}
1578149050Spjd		provider=${provider#/dev/}
1579149050Spjd		devices2="${devices2} ${provider}"
1580149050Spjd	done
1581149050Spjd
1582149050Spjd	echo ${devices2}
1583149050Spjd}
1584149050Spjd
1585153027Sdougb# Find scripts in local_startup directories that use the old syntax
1586153027Sdougb#
1587153027Sdougbfind_local_scripts_old () {
1588153027Sdougb	zlist=''
1589153027Sdougb	slist=''
1590153027Sdougb	for dir in ${local_startup}; do
1591153027Sdougb		if [ -d "${dir}" ]; then
1592153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1593153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1594153027Sdougb				    continue
1595153027Sdougb				zlist="$zlist $file"
1596153027Sdougb			done
1597153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1598153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1599153027Sdougb				    continue
1600153027Sdougb				slist="$slist $file"
1601153027Sdougb			done
1602153027Sdougb		fi
1603153027Sdougb	done
1604153027Sdougb}
1605153027Sdougb
1606153027Sdougbfind_local_scripts_new () {
1607153027Sdougb	local_rc=''
1608153027Sdougb	for dir in ${local_startup}; do
1609153027Sdougb		if [ -d "${dir}" ]; then
1610153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1611153027Sdougb				case "$file" in
1612153027Sdougb				*.sample) ;;
1613153027Sdougb				*)	if [ -x "$file" ]; then
1614153027Sdougb						local_rc="${local_rc} ${file}"
1615153027Sdougb					fi
1616153027Sdougb					;;
1617153027Sdougb				esac
1618153027Sdougb			done
1619153027Sdougb		fi
1620153027Sdougb	done
1621153027Sdougb}
1622153027Sdougb
1623165565Syar# check_required_{before|after} command
1624165565Syar#	Check for things required by the command before and after its precmd,
1625165565Syar#	respectively.  The two separate functions are needed because some
1626165565Syar#	conditions should prevent precmd from being run while other things
1627165565Syar#	depend on precmd having already been run.
1628165565Syar#
1629165565Syarcheck_required_before()
1630165565Syar{
1631165565Syar	local _f
1632165565Syar
1633165565Syar	case "$1" in
1634165565Syar	start)
1635165565Syar		for _f in $required_vars; do
1636165565Syar			if ! checkyesno $_f; then
1637165565Syar				warn "\$${_f} is not enabled."
1638165565Syar				if [ -z "$rc_force" ]; then
1639165565Syar					return 1
1640165565Syar				fi
1641165565Syar			fi
1642165565Syar		done
1643165565Syar
1644165565Syar		for _f in $required_dirs; do
1645165565Syar			if [ ! -d "${_f}/." ]; then
1646165565Syar				warn "${_f} is not a directory."
1647165565Syar				if [ -z "$rc_force" ]; then
1648165565Syar					return 1
1649165565Syar				fi
1650165565Syar			fi
1651165565Syar		done
1652165565Syar
1653165565Syar		for _f in $required_files; do
1654165565Syar			if [ ! -r "${_f}" ]; then
1655165565Syar				warn "${_f} is not readable."
1656165565Syar				if [ -z "$rc_force" ]; then
1657165565Syar					return 1
1658165565Syar				fi
1659165565Syar			fi
1660165565Syar		done
1661165565Syar		;;
1662165565Syar	esac
1663165565Syar
1664165565Syar	return 0
1665165565Syar}
1666165565Syar
1667165565Syarcheck_required_after()
1668165565Syar{
1669165565Syar	local _f _args
1670165565Syar
1671165565Syar	case "$1" in
1672165565Syar	start)
1673165565Syar		for _f in $required_modules; do
1674165565Syar			case "${_f}" in
1675165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1676165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1677165565Syar				*)	_args="${_f}" ;;
1678165565Syar			esac
1679165565Syar			if ! load_kld ${_args}; then
1680165565Syar				if [ -z "$rc_force" ]; then
1681165565Syar					return 1
1682165565Syar				fi
1683165565Syar			fi
1684165565Syar		done
1685165565Syar		;;
1686165565Syar	esac
1687165565Syar
1688165565Syar	return 0
1689165565Syar}
1690165565Syar
1691131550Scpercivafi
1692157841Sflz
1693197144Shrs# _echoonce var msg mode
1694197144Shrs#	mode=0: Echo $msg if ${$var} is empty.
1695197144Shrs#	        After doing echo, a string is set to ${$var}.
1696197144Shrs#
1697197144Shrs#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
1698197144Shrs#
1699197144Shrs_echoonce()
1700197144Shrs{
1701197144Shrs	local _var _msg _mode
1702197144Shrs	_var=`eval echo \\$$1`
1703197144Shrs	_msg=$2
1704197144Shrs	_mode=$3
1705197144Shrs
1706197144Shrs	case $_mode in
1707197144Shrs	1)	[ -n "$_var" ] && echo "$_msg" ;;
1708197144Shrs	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1709197144Shrs	esac
1710197144Shrs}
1711197144Shrs
1712157841Sflz_rc_subr_loaded=:
1713