rc.subr revision 198162
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 198162 2009-10-15 23:20:23Z dougb $
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#
401197947Sdougb# check_startmsgs
402197947Sdougb#	If rc_quiet is set (usually as a result of using faststart at
403197947Sdougb#	boot time) check if rc_startmsgs is enabled.
404197947Sdougb#
405197947Sdougbcheck_startmsgs()
406197947Sdougb{
407197947Sdougb	if [ -n "$rc_quiet" ]; then
408197947Sdougb		checkyesno rc_startmsgs
409197947Sdougb	else
410197947Sdougb		return 0
411197947Sdougb	fi
412197947Sdougb}
413197947Sdougb
414197947Sdougb#
41598186Sgordon# run_rc_command argument
41698186Sgordon#	Search for argument in the list of supported commands, which is:
41798186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
41898186Sgordon#	If there's a match, run ${argument}_cmd or the default method
41998186Sgordon#	(see below).
42078344Sobrien#
42198186Sgordon#	If argument has a given prefix, then change the operation as follows:
42298186Sgordon#		Prefix	Operation
42378344Sobrien#		------	---------
424175676Smtm#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
42598186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
426126303Smtm#		one	Set ${rcvar} to YES
427175676Smtm#		quiet	Don't output some diagnostics, and set rc_quiet=yes
42878344Sobrien#
42978344Sobrien#	The following globals are used:
43078344Sobrien#
43198186Sgordon#	Name		Needed	Purpose
43298186Sgordon#	----		------	-------
43378344Sobrien#	name		y	Name of script.
43478344Sobrien#
43578344Sobrien#	command		n	Full path to command.
43698186Sgordon#				Not needed if ${rc_arg}_cmd is set for
43778344Sobrien#				each keyword.
43878344Sobrien#
43978344Sobrien#	command_args	n	Optional args/shell directives for command.
44078344Sobrien#
44198186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
44298186Sgordon#				call check_{pidfile,process}() appropriately.
44398186Sgordon#
444197144Shrs#	desc		n	Description of script.
445197144Shrs#
44678344Sobrien#	extra_commands	n	List of extra commands supported.
44778344Sobrien#
44898186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
44998186Sgordon#				otherwise use check_process $command.
45098186Sgordon#				In either case, only check if $command is set.
45178344Sobrien#
45298186Sgordon#	procname	n	Process name to check for instead of $command.
45398186Sgordon#
45478344Sobrien#	rcvar		n	This is checked with checkyesno to determine
45578344Sobrien#				if the action should be run.
45678344Sobrien#
457157653Sflz#	${name}_program	n	Full path to command.
458157653Sflz#				Meant to be used in /etc/rc.conf to override
459157653Sflz#				${command}.
460157653Sflz#
46178344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
46298186Sgordon#				Requires /usr to be mounted.
46378344Sobrien#
46478344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
46578344Sobrien#				(if not using ${name}_chroot).
46678344Sobrien#
46778344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
46878344Sobrien#				NOTE:	$flags from the parent environment
46978344Sobrien#					can be used to override this.
47078344Sobrien#
47178344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
47278344Sobrien#
47378344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
47478344Sobrien#				using ${name}_chroot.
47598186Sgordon#				Requires /usr to be mounted.
47678344Sobrien#
47778344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
47898186Sgordon#				Requires /usr to be mounted.
47978344Sobrien#
48098186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
48198186Sgordon#				to run the chrooted ${command} with.
48298186Sgordon#				Requires /usr to be mounted.
48378344Sobrien#
48498186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
48578344Sobrien#				Otherwise, use default command (see below)
48678344Sobrien#
48798186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
48898186Sgordon#				${rc_arg}_cmd method in the default
48998186Sgordon#				operation (i.e, after checking for required
49098186Sgordon#				bits and process (non)existence).
49178344Sobrien#				If this completes with a non-zero exit code,
49298186Sgordon#				don't run ${rc_arg}_cmd.
49378344Sobrien#
49498186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
49598186Sgordon#				${rc_arg}_cmd method, if that method
49698186Sgordon#				returned a zero exit code.
49798186Sgordon#
49878344Sobrien#	required_dirs	n	If set, check for the existence of the given
499165565Syar#				directories before running a (re)start command.
50078344Sobrien#
50178344Sobrien#	required_files	n	If set, check for the readability of the given
502165565Syar#				files before running a (re)start command.
50378344Sobrien#
504165565Syar#	required_modules n	If set, ensure the given kernel modules are
505165565Syar#				loaded before running a (re)start command.
506165565Syar#				The check and possible loads are actually
507165565Syar#				done after start_precmd so that the modules
508165565Syar#				aren't loaded in vain, should the precmd
509165565Syar#				return a non-zero status to indicate a error.
510165565Syar#				If a word in the list looks like "foo:bar",
511165565Syar#				"foo" is the KLD file name and "bar" is the
512165565Syar#				module name.  If a word looks like "foo~bar",
513165565Syar#				"foo" is the KLD file name and "bar" is a
514165565Syar#				egrep(1) pattern matching the module name.
515165565Syar#				Otherwise the module name is assumed to be
516165565Syar#				the same as the KLD file name, which is most
517165565Syar#				common.  See load_kld().
518165565Syar#
51978344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
52078344Sobrien#				listed variables before running the default
52178344Sobrien#				(re)start command.
52278344Sobrien#
52398186Sgordon#	Default behaviour for a given argument, if no override method is
52498186Sgordon#	provided:
52578344Sobrien#
52698186Sgordon#	Argument	Default behaviour
52798186Sgordon#	--------	-----------------
52878344Sobrien#	start		if !running && checkyesno ${rcvar}
52978344Sobrien#				${command}
53078344Sobrien#
53178344Sobrien#	stop		if ${pidfile}
53298186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
53378344Sobrien#			else
53498186Sgordon#				rc_pid=$(check_process $command)
53598186Sgordon#			kill $sig_stop $rc_pid
53698186Sgordon#			wait_for_pids $rc_pid
53798186Sgordon#			($sig_stop defaults to TERM.)
53878344Sobrien#
53998186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
54098186Sgordon#			and doesn't wait_for_pids.
54178344Sobrien#			$sig_reload defaults to HUP.
542151685Syar#			Note that `reload' isn't provided by default,
543151685Syar#			it should be enabled via $extra_commands.
54478344Sobrien#
54578344Sobrien#	restart		Run `stop' then `start'.
54678344Sobrien#
54798186Sgordon#	status		Show if ${command} is running, etc.
54878344Sobrien#
54998186Sgordon#	poll		Wait for ${command} to exit.
55098186Sgordon#
55198186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
55298186Sgordon#
55398186Sgordon#	Variables available to methods, and after run_rc_command() has
55498186Sgordon#	completed:
55598186Sgordon#
55698186Sgordon#	Variable	Purpose
55798186Sgordon#	--------	-------
558126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
55998186Sgordon#			performed
56098186Sgordon#
56198186Sgordon#	rc_flags	Flags to start the default command with.
56298186Sgordon#			Defaults to ${name}_flags, unless overridden
56398186Sgordon#			by $flags from the environment.
56498186Sgordon#			This variable may be changed by the precmd method.
56598186Sgordon#
56698186Sgordon#	rc_pid		PID of command (if appropriate)
56798186Sgordon#
56898186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
56998186Sgordon#
57098186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
57198186Sgordon#
572175676Smtm#	rc_quiet	Not empty if "quiet" was provided
57398186Sgordon#
574175676Smtm#
57578344Sobrienrun_rc_command()
57678344Sobrien{
577116097Smtm	_return=0
57898186Sgordon	rc_arg=$1
57978344Sobrien	if [ -z "$name" ]; then
58098186Sgordon		err 3 'run_rc_command: $name is not set.'
58178344Sobrien	fi
58278344Sobrien
583132892Smtm	# Don't repeat the first argument when passing additional command-
584132892Smtm	# line arguments to the command subroutines.
585132892Smtm	#
586132892Smtm	shift 1
587132892Smtm	rc_extra_args="$*"
588132892Smtm
589126303Smtm	_rc_prefix=
59098186Sgordon	case "$rc_arg" in
59178344Sobrien	fast*)				# "fast" prefix; don't check pid
59298186Sgordon		rc_arg=${rc_arg#fast}
59398186Sgordon		rc_fast=yes
594175676Smtm		rc_quiet=yes
59578344Sobrien		;;
596126303Smtm	force*)				# "force prefix; always run
59798186Sgordon		rc_force=yes
598126303Smtm		_rc_prefix=force
599126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
60078344Sobrien		if [ -n "${rcvar}" ]; then
60178344Sobrien			eval ${rcvar}=YES
60278344Sobrien		fi
60378344Sobrien		;;
604126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
605126303Smtm		_rc_prefix=one
606126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
607126303Smtm		if [ -n "${rcvar}" ]; then
608126303Smtm			eval ${rcvar}=YES
609126303Smtm		fi
610126303Smtm		;;
611175676Smtm	quiet*)				# "quiet" prefix; omit some messages
612175676Smtm		_rc_prefix=quiet
613175676Smtm		rc_arg=${rc_arg#${_rc_prefix}}
614175676Smtm		rc_quiet=yes
615175676Smtm		;;
61678344Sobrien	esac
61778344Sobrien
618161530Sflz	eval _override_command=\$${name}_program
619198162Sdougb	command=${_override_command:-$command}
620161530Sflz
62178344Sobrien	_keywords="start stop restart rcvar $extra_commands"
62298186Sgordon	rc_pid=
62378344Sobrien	_pidcmd=
62498186Sgordon	_procname=${procname:-${command}}
62598186Sgordon
626131135Smtm					# setup pid check command
627131135Smtm	if [ -n "$_procname" ]; then
62878344Sobrien		if [ -n "$pidfile" ]; then
62998186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
63098186Sgordon		else
63198186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
63278344Sobrien		fi
63378344Sobrien		if [ -n "$_pidcmd" ]; then
63498186Sgordon			_keywords="${_keywords} status poll"
63578344Sobrien		fi
63678344Sobrien	fi
63778344Sobrien
63898186Sgordon	if [ -z "$rc_arg" ]; then
639150796Syar		rc_usage $_keywords
64078344Sobrien	fi
64178344Sobrien
64278344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
64398186Sgordon		rc_flags=$flags
64478344Sobrien	else
64598186Sgordon		eval rc_flags=\$${name}_flags
64678344Sobrien	fi
64798186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
64898186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
64998186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
65078344Sobrien
65198186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
652124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
65398186Sgordon			unset _user
65498186Sgordon		fi
65598186Sgordon	fi
65698186Sgordon
657179870Smtm	eval $_pidcmd			# determine the pid if necessary
658179870Smtm
659179870Smtm	for _elem in $_keywords; do
660179870Smtm		if [ "$_elem" != "$rc_arg" ]; then
661179870Smtm			continue
662179870Smtm		fi
66378344Sobrien					# if ${rcvar} is set, and $1 is not
66498186Sgordon					# "rcvar", then run
66578344Sobrien					#	checkyesno ${rcvar}
66678344Sobrien					# and return if that failed
66778344Sobrien					#
668179870Smtm		if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
669179870Smtm			if ! checkyesno ${rcvar}; then
670179870Smtm				if [ -n "${rc_quiet}" ]; then
671179870Smtm					return 0
672179870Smtm				fi
673179870Smtm				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
674179870Smtm				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
675179870Smtm				echo "instead of '${rc_arg}'."
676175676Smtm				return 0
677175676Smtm			fi
67878344Sobrien		fi
67978344Sobrien
68078344Sobrien					# if there's a custom ${XXX_cmd},
68178344Sobrien					# run that instead of the default
68278344Sobrien					#
683165565Syar		eval _cmd=\$${rc_arg}_cmd \
684165565Syar		     _precmd=\$${rc_arg}_precmd \
685165565Syar		     _postcmd=\$${rc_arg}_postcmd
686165565Syar
68778344Sobrien		if [ -n "$_cmd" ]; then
688165565Syar			_run_rc_precmd || return 1
689165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
690165565Syar			_run_rc_postcmd
691116097Smtm			return $_return
69278344Sobrien		fi
69378344Sobrien
69498186Sgordon		case "$rc_arg" in	# default operations...
69578344Sobrien
69678344Sobrien		status)
697165565Syar			_run_rc_precmd || return 1
69898186Sgordon			if [ -n "$rc_pid" ]; then
69998186Sgordon				echo "${name} is running as pid $rc_pid."
70078344Sobrien			else
70178344Sobrien				echo "${name} is not running."
70278344Sobrien				return 1
70378344Sobrien			fi
704165565Syar			_run_rc_postcmd
70578344Sobrien			;;
70678344Sobrien
70778344Sobrien		start)
708131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
709157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
710153152Syar				return 1
71178344Sobrien			fi
71278344Sobrien
713167413Syar			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
714160667Syar				warn "run_rc_command: cannot run $command"
715153152Syar				return 1
71678344Sobrien			fi
71778344Sobrien
718179946Smtm			if ! _run_rc_precmd; then
719179946Smtm				warn "failed precmd routine for ${name}"
720179946Smtm				return 1
721179946Smtm			fi
72278344Sobrien
723160668Syar					# setup the full command to run
72478344Sobrien					#
725197947Sdougb			check_startmsgs && echo "Starting ${name}."
72678344Sobrien			if [ -n "$_chroot" ]; then
72778344Sobrien				_doit="\
72878344Sobrien${_nice:+nice -n $_nice }\
72978344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
73098186Sgordon$_chroot $command $rc_flags $command_args"
73178344Sobrien			else
73278344Sobrien				_doit="\
733161396Syar${_chdir:+cd $_chdir && }\
73498186Sgordon$command $rc_flags $command_args"
73598186Sgordon				if [ -n "$_user" ]; then
73698186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
73798186Sgordon				fi
738161396Syar				if [ -n "$_nice" ]; then
739161396Syar					if [ -z "$_user" ]; then
740161396Syar						_doit="sh -c \"$_doit\""
741161396Syar					fi	
742161396Syar					_doit="nice -n $_nice $_doit"
743161396Syar				fi
74478344Sobrien			fi
74598186Sgordon
746165565Syar					# run the full command
74798186Sgordon					#
748179946Smtm			if ! _run_rc_doit "$_doit"; then
749179946Smtm				warn "failed to start ${name}"
750179946Smtm				return 1
751179946Smtm			fi
75298186Sgordon
75398186Sgordon					# finally, run postcmd
75498186Sgordon					#
755165565Syar			_run_rc_postcmd
75678344Sobrien			;;
75778344Sobrien
75878344Sobrien		stop)
75998186Sgordon			if [ -z "$rc_pid" ]; then
760153152Syar				[ -n "$rc_fast" ] && return 0
761165565Syar				_run_rc_notrunning
762153152Syar				return 1
76378344Sobrien			fi
76478344Sobrien
765165565Syar			_run_rc_precmd || return 1
76698186Sgordon
76798186Sgordon					# send the signal to stop
76898186Sgordon					#
76978344Sobrien			echo "Stopping ${name}."
770165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
771165565Syar			_run_rc_doit "$_doit" || return 1
77298186Sgordon
77398186Sgordon					# wait for the command to exit,
77498186Sgordon					# and run postcmd.
77598186Sgordon			wait_for_pids $rc_pid
776165565Syar
777165565Syar			_run_rc_postcmd
77878344Sobrien			;;
77978344Sobrien
78078344Sobrien		reload)
78198186Sgordon			if [ -z "$rc_pid" ]; then
782165565Syar				_run_rc_notrunning
783153152Syar				return 1
78478344Sobrien			fi
785165565Syar
786165565Syar			_run_rc_precmd || return 1
787165565Syar
788165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
789165565Syar			_run_rc_doit "$_doit" || return 1
790165565Syar
791165565Syar			_run_rc_postcmd
79278344Sobrien			;;
79378344Sobrien
79478344Sobrien		restart)
79578344Sobrien					# prevent restart being called more
79678344Sobrien					# than once by any given script
79778344Sobrien					#
798126285Smtm			if ${_rc_restart_done:-false}; then
79978344Sobrien				return 0
80078344Sobrien			fi
801126285Smtm			_rc_restart_done=true
80278344Sobrien
803165565Syar			_run_rc_precmd || return 1
804165565Syar
805165565Syar			# run those in a subshell to keep global variables
806152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
807165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
808165565Syar			_return=$?
809165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
81098186Sgordon
811165565Syar			_run_rc_postcmd
81278344Sobrien			;;
81378344Sobrien
81498186Sgordon		poll)
815165565Syar			_run_rc_precmd || return 1
81698186Sgordon			if [ -n "$rc_pid" ]; then
81798186Sgordon				wait_for_pids $rc_pid
81898186Sgordon			fi
819165565Syar			_run_rc_postcmd
82098186Sgordon			;;
82198186Sgordon
82278344Sobrien		rcvar)
823197144Shrs			echo -n "# $name"
824197144Shrs			if [ -n "$desc" ]; then
825197144Shrs				echo " : $desc"
826197144Shrs			else
827197144Shrs				echo ""
828197144Shrs			fi
829197144Shrs			echo "#"
830197144Shrs			# Get unique vars in $rcvar $rcvars
831197144Shrs			for _v in $rcvar $rcvars; do
832197144Shrs				case $v in
833197144Shrs				$_v\ *|\ *$_v|*\ $_v\ *) ;;
834197144Shrs				*)	v="${v# } $_v" ;;
835197144Shrs				esac
836197144Shrs			done
837197144Shrs
838197144Shrs			# Display variables.
839197144Shrs			for _v in $v; do
840197144Shrs				if [ -z "$_v" ]; then
841197144Shrs					continue
84278344Sobrien				fi
843197144Shrs
844197144Shrs				eval _desc=\$${_v}_desc
845197144Shrs				eval _defval=\$${_v}_defval
846197144Shrs				_h="-"
847197144Shrs
848197144Shrs				eval echo \"$_v=\\\"\$$_v\\\"\"
849197144Shrs				# decode multiple lines of _desc
850197144Shrs				while [ -n "$_desc" ]; do
851197144Shrs					case $_desc in
852197144Shrs					*^^*)
853197144Shrs						echo "# $_h ${_desc%%^^*}"
854197144Shrs						_desc=${_desc#*^^}
855197144Shrs						_h=" "
856197144Shrs						;;
857197144Shrs					*)
858197144Shrs						echo "# $_h ${_desc}"
859197144Shrs						break
860197144Shrs						;;
861197144Shrs					esac
862197144Shrs				done
863197144Shrs				echo "#   (default: \"$_defval\")"
864197144Shrs			done
865197144Shrs			echo ""
86678344Sobrien			;;
86778344Sobrien
86878344Sobrien		*)
869150796Syar			rc_usage $_keywords
87078344Sobrien			;;
87178344Sobrien
87278344Sobrien		esac
873116097Smtm		return $_return
87478344Sobrien	done
87578344Sobrien
87698186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
877150796Syar	rc_usage $_keywords
878153152Syar	# not reached
87978344Sobrien}
88078344Sobrien
88178344Sobrien#
882165565Syar# Helper functions for run_rc_command: common code.
883165565Syar# They use such global variables besides the exported rc_* ones:
884165565Syar#
885165565Syar#	name	       R/W
886165565Syar#	------------------
887165565Syar#	_precmd		R
888165565Syar#	_postcmd	R
889165565Syar#	_return		W
890165565Syar#
891165565Syar_run_rc_precmd()
892165565Syar{
893165565Syar	check_required_before "$rc_arg" || return 1
894165565Syar
895165565Syar	if [ -n "$_precmd" ]; then
896165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
897165565Syar		eval "$_precmd $rc_extra_args"
898165565Syar		_return=$?
899165565Syar
900165565Syar		# If precmd failed and force isn't set, request exit.
901165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
902165565Syar			return 1
903165565Syar		fi
904165565Syar	fi
905165565Syar
906165565Syar	check_required_after "$rc_arg" || return 1
907165565Syar
908165565Syar	return 0
909165565Syar}
910165565Syar
911165565Syar_run_rc_postcmd()
912165565Syar{
913165565Syar	if [ -n "$_postcmd" ]; then
914165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
915165565Syar		eval "$_postcmd $rc_extra_args"
916165565Syar		_return=$?
917165565Syar	fi
918165565Syar	return 0
919165565Syar}
920165565Syar
921165565Syar_run_rc_doit()
922165565Syar{
923165565Syar	debug "run_rc_command: doit: $*"
924165565Syar	eval "$@"
925165565Syar	_return=$?
926165565Syar
927165565Syar	# If command failed and force isn't set, request exit.
928165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
929165565Syar		return 1
930165565Syar	fi
931165565Syar
932165565Syar	return 0
933165565Syar}
934165565Syar
935165565Syar_run_rc_notrunning()
936165565Syar{
937165565Syar	local _pidmsg
938165565Syar
939165565Syar	if [ -n "$pidfile" ]; then
940165565Syar		_pidmsg=" (check $pidfile)."
941165565Syar	else
942165565Syar		_pidmsg=
943165565Syar	fi
944165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
945165565Syar}
946165565Syar
947165565Syar_run_rc_killcmd()
948165565Syar{
949165565Syar	local _cmd
950165565Syar
951165565Syar	_cmd="kill -$1 $rc_pid"
952165565Syar	if [ -n "$_user" ]; then
953165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
954165565Syar	fi
955165565Syar	echo "$_cmd"
956165565Syar}
957165565Syar
958165565Syar#
95978344Sobrien# run_rc_script file arg
96078344Sobrien#	Start the script `file' with `arg', and correctly handle the
96178344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
96298186Sgordon#	sourced into the current environment.  If `file' appears to be
96398186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
96498186Sgordon#	executable run as a child process.
96578344Sobrien#
96678344Sobrienrun_rc_script()
96778344Sobrien{
96878344Sobrien	_file=$1
96978344Sobrien	_arg=$2
97078344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
97178344Sobrien		err 3 'USAGE: run_rc_script file arg'
97278344Sobrien	fi
97378344Sobrien
97498186Sgordon	unset	name command command_args command_interpreter \
97598186Sgordon		extra_commands pidfile procname \
976197144Shrs		rcvar rcvars rcvars_obsolete required_dirs required_files \
977197144Shrs		required_vars
97898186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
97998186Sgordon
98078344Sobrien	case "$_file" in
981193118Sdougb	/etc/rc.d/*.sh)			# no longer allowed in the base
982193118Sdougb		warn "Ignoring old-style startup script $_file"
98378344Sobrien		;;
984153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
98598186Sgordon		warn "Ignoring scratch file $_file"
98698186Sgordon		;;
98778344Sobrien	*)				# run in subshell
98898186Sgordon		if [ -x $_file ]; then
98998186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
990146490Sschweikh				set $_arg; . $_file
99198186Sgordon			else
992130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
993130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
994184317Sthompsa				  trap "echo Script $_file running" 29
995146490Sschweikh				  set $_arg; . $_file )
99698186Sgordon			fi
99798186Sgordon		fi
99878344Sobrien		;;
99978344Sobrien	esac
100078344Sobrien}
100178344Sobrien
100278344Sobrien#
1003157653Sflz# load_rc_config name
1004157653Sflz#	Source in the configuration file for a given name.
100578344Sobrien#
100678344Sobrienload_rc_config()
100778344Sobrien{
1008197144Shrs	local _name _var _defval _v _msg _new
1009157653Sflz	_name=$1
1010157653Sflz	if [ -z "$_name" ]; then
1011157653Sflz		err 3 'USAGE: load_rc_config name'
101278344Sobrien	fi
101378344Sobrien
1014126285Smtm	if ${_rc_conf_loaded:-false}; then
1015126285Smtm		:
1016126285Smtm	else
101798186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
101898186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
101998186Sgordon			. /etc/defaults/rc.conf
102098186Sgordon			source_rc_confs
102198186Sgordon		elif [ -r /etc/rc.conf ]; then
102298186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
102398186Sgordon			. /etc/rc.conf
102498186Sgordon		fi
1025126285Smtm		_rc_conf_loaded=true
102698186Sgordon	fi
1027161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
1028157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
1029161530Sflz		. /etc/rc.conf.d/"$_name"
1030157653Sflz	fi
1031179872Smtm
1032179872Smtm	# Old variable names support
1033179872Smtm	#
1034179872Smtm	[ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1035197144Shrs
1036197144Shrs	# Set defaults if defined.
1037197144Shrs	for _var in $rcvar $rcvars; do
1038197144Shrs		_defval=`eval echo "\\\$${_var}_defval"`
1039197144Shrs		if [ -n "$_defval" ]; then
1040197144Shrs			eval : \${$_var:=\$${_var}_defval}
1041197144Shrs		fi
1042197144Shrs	done
1043197144Shrs
1044197144Shrs	# check obsolete rc.conf variables
1045197144Shrs	for _var in $rcvars_obsolete; do
1046197144Shrs		_v=`eval echo \\$$_var`
1047197144Shrs		_msg=`eval echo \\$${_var}_obsolete_msg`
1048197144Shrs		_new=`eval echo \\$${_var}_newvar`
1049197144Shrs		case $_v in
1050197144Shrs		"")
1051197144Shrs			;;
1052197144Shrs		*)
1053197144Shrs			if [ -z "$_new" ]; then
1054197144Shrs				_msg="Ignored."
1055197144Shrs			else
1056197144Shrs				eval $_new=\"\$$_var\"
1057197144Shrs				if [ -z "$_msg" ]; then
1058197144Shrs					_msg="Use \$$_new instead."
1059197144Shrs				fi
1060197144Shrs			fi
1061197144Shrs			warn "\$$_var is obsolete.  $_msg"
1062197144Shrs			;;
1063197144Shrs		esac
1064197144Shrs	done
106578344Sobrien}
1066157473Sflz  
1067157473Sflz#
1068157653Sflz# load_rc_config_var name var
1069157653Sflz#	Read the rc.conf(5) var for name and set in the
1070157473Sflz#	current shell, using load_rc_config in a subshell to prevent
1071157473Sflz#	unwanted side effects from other variable assignments.
1072157473Sflz#
1073157473Sflzload_rc_config_var()
1074157473Sflz{
1075157473Sflz	if [ $# -ne 2 ]; then
1076157653Sflz		err 3 'USAGE: load_rc_config_var name var'
1077157473Sflz	fi
1078157473Sflz	eval $(eval '(
1079157473Sflz		load_rc_config '$1' >/dev/null;
1080157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1081157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
1082157473Sflz		fi
1083157473Sflz	)' )
1084157473Sflz}
108578344Sobrien
108678344Sobrien#
108778344Sobrien# rc_usage commands
108878344Sobrien#	Print a usage string for $0, with `commands' being a list of
108978344Sobrien#	valid commands.
109078344Sobrien#
109178344Sobrienrc_usage()
109278344Sobrien{
1093126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
109478344Sobrien
109578344Sobrien	_sep=
1096126286Smtm	for _elem; do
109778344Sobrien		echo -n 1>&2 "$_sep$_elem"
109878344Sobrien		_sep="|"
109978344Sobrien	done
110078344Sobrien	echo 1>&2 ")"
110178344Sobrien	exit 1
110278344Sobrien}
110378344Sobrien
110478344Sobrien#
110578344Sobrien# err exitval message
110678344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
110778344Sobrien#
110878344Sobrienerr()
110978344Sobrien{
111078344Sobrien	exitval=$1
111178344Sobrien	shift
111278344Sobrien
1113106643Sgordon	if [ -x /usr/bin/logger ]; then
1114106643Sgordon		logger "$0: ERROR: $*"
1115106643Sgordon	fi
1116106643Sgordon	echo 1>&2 "$0: ERROR: $*"
111778344Sobrien	exit $exitval
111878344Sobrien}
111978344Sobrien
112078344Sobrien#
112178344Sobrien# warn message
112278344Sobrien#	Display message to stderr and log to the syslog.
112378344Sobrien#
112478344Sobrienwarn()
112578344Sobrien{
1126106643Sgordon	if [ -x /usr/bin/logger ]; then
1127106643Sgordon		logger "$0: WARNING: $*"
1128106643Sgordon	fi
1129106643Sgordon	echo 1>&2 "$0: WARNING: $*"
113078344Sobrien}
113198186Sgordon
113298186Sgordon#
113398186Sgordon# info message
113498186Sgordon#	Display informational message to stdout and log to syslog.
113598186Sgordon#
113698186Sgordoninfo()
113798186Sgordon{
1138119170Smtm	case ${rc_info} in
1139119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1140119170Smtm		if [ -x /usr/bin/logger ]; then
1141119170Smtm			logger "$0: INFO: $*"
1142119170Smtm		fi
1143119170Smtm		echo "$0: INFO: $*"
1144119170Smtm		;;
1145119170Smtm	esac
114698186Sgordon}
114798186Sgordon
114898186Sgordon#
114998186Sgordon# debug message
1150106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
115198186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
115298186Sgordon#	function.
115398186Sgordon#
115498186Sgordondebug()
115598186Sgordon{
115698186Sgordon	case ${rc_debug} in
115798186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1158106700Sgordon		if [ -x /usr/bin/logger ]; then
1159162947Syar			logger "$0: DEBUG: $*"
1160106700Sgordon		fi
1161146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
116298186Sgordon		;;
116398186Sgordon	esac
116498186Sgordon}
116598186Sgordon
116698186Sgordon#
116798186Sgordon# backup_file action file cur backup
116898186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
116998186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
117098186Sgordon#
117198186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
117298186Sgordon#	which can be either YES or NO.
117398186Sgordon#
117498186Sgordon#	The `action' keyword can be one of the following:
117598186Sgordon#
117698186Sgordon#	add		`file' is now being backed up (and is possibly
117798186Sgordon#			being reentered into the backups system).  `cur'
117898186Sgordon#			is created and RCS files, if necessary, are
117998186Sgordon#			created as well.
118098186Sgordon#
118198186Sgordon#	update		`file' has changed and needs to be backed up.
118298186Sgordon#			If `cur' exists, it is copied to to `back' or
118398186Sgordon#			checked into RCS (if the repository file is old),
118498186Sgordon#			and then `file' is copied to `cur'.  Another RCS
118598186Sgordon#			check in done here if RCS is being used.
118698186Sgordon#
118798186Sgordon#	remove		`file' is no longer being tracked by the backups
118898186Sgordon#			system.  If RCS is not being used, `cur' is moved
118998186Sgordon#			to `back', otherwise an empty file is checked in,
119098186Sgordon#			and then `cur' is removed.
119198186Sgordon#
119298186Sgordon#
119398186Sgordonbackup_file()
119498186Sgordon{
119598186Sgordon	_action=$1
119698186Sgordon	_file=$2
119798186Sgordon	_cur=$3
119898186Sgordon	_back=$4
119998186Sgordon
120098186Sgordon	if checkyesno backup_uses_rcs; then
120198186Sgordon		_msg0="backup archive"
120298186Sgordon		_msg1="update"
120398186Sgordon
120498186Sgordon		# ensure that history file is not locked
120598186Sgordon		if [ -f $_cur,v ]; then
120698186Sgordon			rcs -q -u -U -M $_cur
120798186Sgordon		fi
120898186Sgordon
120998186Sgordon		# ensure after switching to rcs that the
121098186Sgordon		# current backup is not lost
121198186Sgordon		if [ -f $_cur ]; then
121298186Sgordon			# no archive, or current newer than archive
121398186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
121498186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
121598186Sgordon				rcs -q -kb -U $_cur
121698186Sgordon				co -q -f -u $_cur
121798186Sgordon			fi
121898186Sgordon		fi
121998186Sgordon
122098186Sgordon		case $_action in
122198186Sgordon		add|update)
122298186Sgordon			cp -p $_file $_cur
122398186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
122498186Sgordon			rcs -q -kb -U $_cur
122598186Sgordon			co -q -f -u $_cur
122698186Sgordon			chown root:wheel $_cur $_cur,v
122798186Sgordon			;;
122898186Sgordon		remove)
122998186Sgordon			cp /dev/null $_cur
123098186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
123198186Sgordon			rcs -q -kb -U $_cur
123298186Sgordon			chown root:wheel $_cur $_cur,v
123398186Sgordon			rm $_cur
123498186Sgordon			;;
123598186Sgordon		esac
123698186Sgordon	else
123798186Sgordon		case $_action in
123898186Sgordon		add|update)
123998186Sgordon			if [ -f $_cur ]; then
124098186Sgordon				cp -p $_cur $_back
124198186Sgordon			fi
124298186Sgordon			cp -p $_file $_cur
124398186Sgordon			chown root:wheel $_cur
124498186Sgordon			;;
124598186Sgordon		remove)
124698186Sgordon			mv -f $_cur $_back
124798186Sgordon			;;
124898186Sgordon		esac
124998186Sgordon	fi
125098186Sgordon}
1251119166Smtm
1252123344Smtm# make_symlink src link
1253123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1254123344Smtm#	directory in which link is to be created does not exist
1255123344Smtm#	a warning will be displayed and an error will be returned.
1256123344Smtm#	Returns 0 on sucess, 1 otherwise.
1257119166Smtm#
1258123344Smtmmake_symlink()
1259119166Smtm{
1260123344Smtm	local src link linkdir _me
1261123344Smtm	src="$1"
1262123344Smtm	link="$2"
1263123344Smtm	linkdir="`dirname $link`"
1264123344Smtm	_me="make_symlink()"
1265119166Smtm
1266123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1267123344Smtm		warn "$_me: requires two arguments."
1268119166Smtm		return 1
1269119166Smtm	fi
1270123344Smtm	if [ ! -d "$linkdir" ]; then
1271160667Syar		warn "$_me: the directory $linkdir does not exist."
1272119166Smtm		return 1
1273119166Smtm	fi
1274146490Sschweikh	if ! ln -sf $src $link; then
1275123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1276119166Smtm		return 1
1277119166Smtm	fi
1278119166Smtm	return 0
1279119166Smtm}
1280119166Smtm
1281119166Smtm# devfs_rulesets_from_file file
1282119166Smtm#	Reads a set of devfs commands from file, and creates
1283119166Smtm#	the specified rulesets with their rules. Returns non-zero
1284119166Smtm#	if there was an error.
1285119166Smtm#
1286119166Smtmdevfs_rulesets_from_file()
1287119166Smtm{
1288119166Smtm	local file _err _me
1289119166Smtm	file="$1"
1290119166Smtm	_me="devfs_rulesets_from_file"
1291119166Smtm	_err=0
1292119166Smtm
1293119166Smtm	if [ -z "$file" ]; then
1294119166Smtm		warn "$_me: you must specify a file"
1295119166Smtm		return 1
1296119166Smtm	fi
1297119166Smtm	if [ ! -e "$file" ]; then
1298119166Smtm		debug "$_me: no such file ($file)"
1299119166Smtm		return 0
1300119166Smtm	fi
1301119166Smtm	debug "reading rulesets from file ($file)"
1302119166Smtm	{ while read line
1303119166Smtm	do
1304119166Smtm		case $line in
1305119166Smtm		\#*)
1306119166Smtm			continue
1307119166Smtm			;;
1308119166Smtm		\[*\]*)
1309119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1310119166Smtm			if [ -z "$rulenum" ]; then
1311119166Smtm				warn "$_me: cannot extract rule number ($line)"
1312119166Smtm				_err=1
1313119166Smtm				break
1314119166Smtm			fi
1315119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1316119166Smtm			if [ -z "$rulename" ]; then
1317119166Smtm				warn "$_me: cannot extract rule name ($line)"
1318119166Smtm				_err=1
1319119166Smtm				break;
1320119166Smtm			fi
1321119166Smtm			eval $rulename=\$rulenum
1322119166Smtm			debug "found ruleset: $rulename=$rulenum"
1323146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1324119166Smtm				_err=1
1325119166Smtm				break
1326119166Smtm			fi
1327119166Smtm			;;
1328119166Smtm		*)
1329119166Smtm			rulecmd="${line%%"\#*"}"
1330119166Smtm			# evaluate the command incase it includes
1331119166Smtm			# other rules
1332119166Smtm			if [ -n "$rulecmd" ]; then
1333119166Smtm				debug "adding rule ($rulecmd)"
1334119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1335119166Smtm				then
1336119166Smtm					_err=1
1337119166Smtm					break
1338119166Smtm				fi
1339119166Smtm			fi
1340119166Smtm			;;
1341119166Smtm		esac
1342119166Smtm		if [ $_err -ne 0 ]; then
1343119166Smtm			debug "error in $_me"
1344119166Smtm			break
1345119166Smtm		fi
1346119166Smtm	done } < $file
1347119166Smtm	return $_err
1348119166Smtm}
1349119166Smtm
1350119166Smtm# devfs_init_rulesets
1351119166Smtm#	Initializes rulesets from configuration files. Returns
1352119166Smtm#	non-zero if there was an error.
1353119166Smtm#
1354119166Smtmdevfs_init_rulesets()
1355119166Smtm{
1356119166Smtm	local file _me
1357119166Smtm	_me="devfs_init_rulesets"
1358119166Smtm
1359119166Smtm	# Go through this only once
1360119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1361119166Smtm		debug "$_me: devfs rulesets already initialized"
1362119166Smtm		return
1363119166Smtm	fi
1364146490Sschweikh	for file in $devfs_rulesets; do
1365119166Smtm		devfs_rulesets_from_file $file || return 1
1366119166Smtm	done
1367119166Smtm	devfs_rulesets_init=1
1368119166Smtm	debug "$_me: devfs rulesets initialized"
1369119166Smtm	return 0
1370119166Smtm}
1371119166Smtm
1372119166Smtm# devfs_set_ruleset ruleset [dir]
1373151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1374119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1375119166Smtm#	Returns non-zero if it could not set it successfully.
1376119166Smtm#
1377119166Smtmdevfs_set_ruleset()
1378119166Smtm{
1379119166Smtm	local devdir rs _me
1380119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1381119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1382119166Smtm	_me="devfs_set_ruleset"
1383119166Smtm
1384119166Smtm	if [ -z "$rs" ]; then
1385119166Smtm		warn "$_me: you must specify a ruleset number"
1386119166Smtm		return 1
1387119166Smtm	fi
1388119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1389146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1390119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1391119166Smtm		return 1
1392119166Smtm	fi
1393119166Smtm	return 0
1394119166Smtm}
1395119166Smtm
1396119166Smtm# devfs_apply_ruleset ruleset [dir]
1397119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1398119166Smtm#	The ruleset argument must be a ruleset name as specified
1399119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1400119166Smtm#	if it could not apply the ruleset.
1401119166Smtm#
1402119166Smtmdevfs_apply_ruleset()
1403119166Smtm{
1404119166Smtm	local devdir rs _me
1405119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1406119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1407119166Smtm	_me="devfs_apply_ruleset"
1408119166Smtm
1409119166Smtm	if [ -z "$rs" ]; then
1410119166Smtm		warn "$_me: you must specify a ruleset"
1411119166Smtm		return 1
1412119166Smtm	fi
1413119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1414146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1415119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1416119166Smtm		return 1
1417119166Smtm	fi
1418119166Smtm	return 0
1419119166Smtm}
1420119166Smtm
1421119166Smtm# devfs_domount dir [ruleset]
1422119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1423119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1424119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1425119166Smtm#
1426119166Smtmdevfs_domount()
1427119166Smtm{
1428119166Smtm	local devdir rs _me
1429119166Smtm	devdir="$1"
1430119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1431119166Smtm	_me="devfs_domount()"
1432119166Smtm
1433119166Smtm	if [ -z "$devdir" ]; then
1434119166Smtm		warn "$_me: you must specify a mount-point"
1435119166Smtm		return 1
1436119166Smtm	fi
1437119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1438146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1439119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1440119166Smtm		return 1
1441119166Smtm	fi
1442119166Smtm	if [ -n "$rs" ]; then
1443119166Smtm		devfs_init_rulesets
1444119166Smtm		devfs_set_ruleset $rs $devdir
1445124797Scperciva		devfs -m $devdir rule applyset
1446119166Smtm	fi
1447119166Smtm	return 0
1448119166Smtm}
1449119166Smtm
1450119166Smtm# devfs_mount_jail dir [ruleset]
1451119166Smtm#	Mounts a devfs file system appropriate for jails
1452119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1453119166Smtm#	it names will be used instead.  If present, ruleset must
1454119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1455119166Smtm#	This function returns non-zero if an error occurs.
1456119166Smtm#
1457119166Smtmdevfs_mount_jail()
1458119166Smtm{
1459119166Smtm	local jdev rs _me
1460119166Smtm	jdev="$1"
1461119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1462119166Smtm	_me="devfs_mount_jail"
1463119166Smtm
1464119166Smtm	devfs_init_rulesets
1465146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1466119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1467119166Smtm		return 1
1468119166Smtm	fi
1469119166Smtm	return 0
1470119166Smtm}
1471127345Sbrooks
1472127345Sbrooks# Provide a function for normalizing the mounting of memory
1473127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1474127345Sbrooks# as close as possible between 5-current and 4-stable.
1475127345Sbrooks#   $1 = size
1476127345Sbrooks#   $2 = mount point
1477137451Skeramida#   $3 = (optional) extra mdmfs flags
1478146490Sschweikhmount_md()
1479146490Sschweikh{
1480127345Sbrooks	if [ -n "$3" ]; then
1481137451Skeramida		flags="$3"
1482127345Sbrooks	fi
1483149421Syar	/sbin/mdmfs $flags -s $1 md $2
1484127345Sbrooks}
1485131550Scperciva
1486159828Syar# Code common to scripts that need to load a kernel module
1487159828Syar# if it isn't in the kernel yet. Syntax:
1488160666Syar#   load_kld [-e regex] [-m module] file
1489159828Syar# where -e or -m chooses the way to check if the module
1490159828Syar# is already loaded:
1491160666Syar#   regex is egrep'd in the output from `kldstat -v',
1492160666Syar#   module is passed to `kldstat -m'.
1493160666Syar# The default way is as though `-m file' were specified.
1494159828Syarload_kld()
1495159828Syar{
1496159828Syar	local _loaded _mod _opt _re
1497159828Syar
1498159828Syar	while getopts "e:m:" _opt; do
1499159828Syar		case "$_opt" in
1500159828Syar		e) _re="$OPTARG" ;;
1501159828Syar		m) _mod="$OPTARG" ;;
1502160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1503159828Syar		esac
1504159828Syar	done
1505159828Syar	shift $(($OPTIND - 1))
1506160666Syar	if [ $# -ne 1 ]; then
1507160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1508160666Syar	fi
1509159828Syar	_mod=${_mod:-$1}
1510159828Syar	_loaded=false
1511159828Syar	if [ -n "$_re" ]; then
1512159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1513159828Syar			_loaded=true
1514159828Syar		fi
1515159828Syar	else
1516159828Syar		if kldstat -q -m "$_mod"; then
1517159828Syar			_loaded=true
1518159828Syar		fi
1519159828Syar	fi
1520159828Syar	if ! $_loaded; then
1521159828Syar		if ! kldload "$1"; then
1522159828Syar			warn "Unable to load kernel module $1"
1523159828Syar			return 1
1524160666Syar		else
1525160666Syar			info "$1 kernel module loaded."
1526159828Syar		fi
1527160666Syar	else
1528160666Syar		debug "load_kld: $1 kernel module already loaded."
1529159828Syar	fi
1530159828Syar	return 0
1531159828Syar}
1532159828Syar
1533149049Spjd# ltr str src dst
1534149049Spjd#	Change every $src in $str to $dst.
1535149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1536149049Spjd#	awk(1).
1537149049Spjdltr()
1538149049Spjd{
1539149049Spjd	local _str _src _dst _out _com
1540149049Spjd	_str=$1
1541149049Spjd	_src=$2
1542149049Spjd	_dst=$3
1543149049Spjd	_out=""
1544149049Spjd
1545149049Spjd	IFS=${_src}
1546149049Spjd	for _com in ${_str}; do
1547149049Spjd		if [ -z "${_out}" ]; then
1548149049Spjd			_out="${_com}"
1549149049Spjd		else
1550149049Spjd			_out="${_out}${_dst}${_com}"
1551149049Spjd		fi
1552149049Spjd	done
1553149049Spjd	echo "${_out}"
1554149049Spjd}
1555149049Spjd
1556149050Spjd# Creates a list of providers for GELI encryption.
1557149050Spjdgeli_make_list()
1558149050Spjd{
1559149050Spjd	local devices devices2
1560149050Spjd	local provider mountpoint type options rest
1561149050Spjd
1562149050Spjd	# Create list of GELI providers from fstab.
1563149050Spjd	while read provider mountpoint type options rest ; do
1564155570Sflz		case ":${options}" in
1565155570Sflz		:*noauto*)
1566155570Sflz			noauto=yes
1567155570Sflz			;;
1568155570Sflz		*)
1569155570Sflz			noauto=no
1570155570Sflz			;;
1571155570Sflz		esac
1572155570Sflz
1573149050Spjd		case ":${provider}" in
1574149050Spjd		:#*)
1575149050Spjd			continue
1576149050Spjd			;;
1577149050Spjd		*.eli)
1578149050Spjd			# Skip swap devices.
1579155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1580149050Spjd				continue
1581149050Spjd			fi
1582149050Spjd			devices="${devices} ${provider}"
1583149050Spjd			;;
1584149050Spjd		esac
1585149050Spjd	done < /etc/fstab
1586149050Spjd
1587149050Spjd	# Append providers from geli_devices.
1588149050Spjd	devices="${devices} ${geli_devices}"
1589149050Spjd
1590149050Spjd	for provider in ${devices}; do
1591149050Spjd		provider=${provider%.eli}
1592149050Spjd		provider=${provider#/dev/}
1593149050Spjd		devices2="${devices2} ${provider}"
1594149050Spjd	done
1595149050Spjd
1596149050Spjd	echo ${devices2}
1597149050Spjd}
1598149050Spjd
1599153027Sdougb# Find scripts in local_startup directories that use the old syntax
1600153027Sdougb#
1601153027Sdougbfind_local_scripts_old () {
1602153027Sdougb	zlist=''
1603153027Sdougb	slist=''
1604153027Sdougb	for dir in ${local_startup}; do
1605153027Sdougb		if [ -d "${dir}" ]; then
1606153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1607153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1608153027Sdougb				    continue
1609153027Sdougb				zlist="$zlist $file"
1610153027Sdougb			done
1611153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1612153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1613153027Sdougb				    continue
1614153027Sdougb				slist="$slist $file"
1615153027Sdougb			done
1616153027Sdougb		fi
1617153027Sdougb	done
1618153027Sdougb}
1619153027Sdougb
1620153027Sdougbfind_local_scripts_new () {
1621153027Sdougb	local_rc=''
1622153027Sdougb	for dir in ${local_startup}; do
1623153027Sdougb		if [ -d "${dir}" ]; then
1624153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1625153027Sdougb				case "$file" in
1626153027Sdougb				*.sample) ;;
1627153027Sdougb				*)	if [ -x "$file" ]; then
1628153027Sdougb						local_rc="${local_rc} ${file}"
1629153027Sdougb					fi
1630153027Sdougb					;;
1631153027Sdougb				esac
1632153027Sdougb			done
1633153027Sdougb		fi
1634153027Sdougb	done
1635153027Sdougb}
1636153027Sdougb
1637165565Syar# check_required_{before|after} command
1638165565Syar#	Check for things required by the command before and after its precmd,
1639165565Syar#	respectively.  The two separate functions are needed because some
1640165565Syar#	conditions should prevent precmd from being run while other things
1641165565Syar#	depend on precmd having already been run.
1642165565Syar#
1643165565Syarcheck_required_before()
1644165565Syar{
1645165565Syar	local _f
1646165565Syar
1647165565Syar	case "$1" in
1648165565Syar	start)
1649165565Syar		for _f in $required_vars; do
1650165565Syar			if ! checkyesno $_f; then
1651165565Syar				warn "\$${_f} is not enabled."
1652165565Syar				if [ -z "$rc_force" ]; then
1653165565Syar					return 1
1654165565Syar				fi
1655165565Syar			fi
1656165565Syar		done
1657165565Syar
1658165565Syar		for _f in $required_dirs; do
1659165565Syar			if [ ! -d "${_f}/." ]; then
1660165565Syar				warn "${_f} is not a directory."
1661165565Syar				if [ -z "$rc_force" ]; then
1662165565Syar					return 1
1663165565Syar				fi
1664165565Syar			fi
1665165565Syar		done
1666165565Syar
1667165565Syar		for _f in $required_files; do
1668165565Syar			if [ ! -r "${_f}" ]; then
1669165565Syar				warn "${_f} is not readable."
1670165565Syar				if [ -z "$rc_force" ]; then
1671165565Syar					return 1
1672165565Syar				fi
1673165565Syar			fi
1674165565Syar		done
1675165565Syar		;;
1676165565Syar	esac
1677165565Syar
1678165565Syar	return 0
1679165565Syar}
1680165565Syar
1681165565Syarcheck_required_after()
1682165565Syar{
1683165565Syar	local _f _args
1684165565Syar
1685165565Syar	case "$1" in
1686165565Syar	start)
1687165565Syar		for _f in $required_modules; do
1688165565Syar			case "${_f}" in
1689165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1690165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1691165565Syar				*)	_args="${_f}" ;;
1692165565Syar			esac
1693165565Syar			if ! load_kld ${_args}; then
1694165565Syar				if [ -z "$rc_force" ]; then
1695165565Syar					return 1
1696165565Syar				fi
1697165565Syar			fi
1698165565Syar		done
1699165565Syar		;;
1700165565Syar	esac
1701165565Syar
1702165565Syar	return 0
1703165565Syar}
1704165565Syar
1705131550Scpercivafi
1706157841Sflz
1707197144Shrs# _echoonce var msg mode
1708197144Shrs#	mode=0: Echo $msg if ${$var} is empty.
1709197144Shrs#	        After doing echo, a string is set to ${$var}.
1710197144Shrs#
1711197144Shrs#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
1712197144Shrs#
1713197144Shrs_echoonce()
1714197144Shrs{
1715197144Shrs	local _var _msg _mode
1716197144Shrs	_var=`eval echo \\$$1`
1717197144Shrs	_msg=$2
1718197144Shrs	_mode=$3
1719197144Shrs
1720197144Shrs	case $_mode in
1721197144Shrs	1)	[ -n "$_var" ] && echo "$_msg" ;;
1722197144Shrs	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1723197144Shrs	esac
1724197144Shrs}
1725197144Shrs
1726157841Sflz_rc_subr_loaded=:
1727