rc.subr revision 206248
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 206248 2010-04-06 05:20:46Z 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#
1978344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2078344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2178344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2278344Sobrien# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2378344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2478344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2578344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2678344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2778344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2878344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2978344Sobrien# POSSIBILITY OF SUCH DAMAGE.
3078344Sobrien#
3178344Sobrien# rc.subr
3278344Sobrien#	functions used by various rc scripts
3378344Sobrien#
3478344Sobrien
35157473Sflz: ${rcvar_manpage:='rc.conf(5)'}
36169668Smtm: ${RC_PID:=$$}; export RC_PID
37157473Sflz
3878344Sobrien#
3998186Sgordon#	Operating System dependent/independent variables
4098186Sgordon#
4198186Sgordon
42131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then
43131550Scperciva
44131550Scperciva_rc_subr_loaded="YES"
45131550Scperciva
4698186SgordonSYSCTL="/sbin/sysctl"
4798186SgordonSYSCTL_N="${SYSCTL} -n"
48202988SemasteSYSCTL_W="${SYSCTL}"
49124832SmtmID="/usr/bin/id"
50124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
51161435SyarPS="/bin/ps -ww"
52161435SyarJID=`$PS -p $$ -o jid=`
5398186Sgordon
5498186Sgordon#
5578344Sobrien#	functions
5678344Sobrien#	---------
5778344Sobrien
58197144Shrs# set_rcvar [var] [defval] [desc]
5978344Sobrien#
60197144Shrs#	Echo or define a rc.conf(5) variable name.  Global variable
61197144Shrs#	$rcvars is used.
6298186Sgordon#
63197144Shrs#	If no argument is specified, echo "${name}_enable".
64197144Shrs#
65197144Shrs#	If only a var is specified, echo "${var}_enable".
66197144Shrs#
67197144Shrs#	If var and defval are specified, the ${var} is defined as
68197144Shrs#	rc.conf(5) variable and the default value is ${defvar}.  An
69197144Shrs#	optional argument $desc can also be specified to add a
70197144Shrs#	description for that.
71197144Shrs#
7298186Sgordonset_rcvar()
7398186Sgordon{
74197144Shrs	case $# in
75197144Shrs	0)
76197144Shrs		echo ${name}_enable
7798186Sgordon		;;
78197144Shrs	1)
79197144Shrs		echo ${1}_enable
8098186Sgordon		;;
8198186Sgordon	*)
82197144Shrs		debug "rcvar_define: \$$1=$2 is added" \
83197144Shrs		    " as a rc.conf(5) variable."
84197144Shrs
85197144Shrs		local _var
86197144Shrs		_var=$1
87197144Shrs		rcvars="${rcvars# } $_var"
88197144Shrs		eval ${_var}_defval=\"$2\"
89197144Shrs		shift 2
90197144Shrs		# encode multiple lines of _desc
91197144Shrs		for l in "$@"; do
92197144Shrs			eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
93197144Shrs		done
94197144Shrs		eval ${_var}_desc=\"\${${_var}_desc#^^}\"
9598186Sgordon		;;
9698186Sgordon	esac
9798186Sgordon}
9898186Sgordon
99197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg]
100197144Shrs#	Define obsolete variable.
101197144Shrs#	Global variable $rcvars_obsolete is used.
10298186Sgordon#
103197144Shrsset_rcvar_obsolete()
104197144Shrs{
105197144Shrs	local _var
106197144Shrs	_var=$1
107197144Shrs	debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
108197144Shrs
109197144Shrs	rcvars_obsolete="${rcvars_obsolete# } $1"
110197144Shrs	eval ${1}_newvar=\"$2\"
111197144Shrs	shift 2
112197144Shrs	eval ${_var}_obsolete_msg=\"$*\"
113197144Shrs}
114197144Shrs
115197144Shrs#
11698186Sgordon# force_depend script
11798186Sgordon#	Force a service to start. Intended for use by services
11898186Sgordon#	to resolve dependency issues. It is assumed the caller
11998186Sgordon#	has check to make sure this call is necessary
12098186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
12198186Sgordon#
12298186Sgordonforce_depend()
12398186Sgordon{
12498186Sgordon	_depend="$1"
12598186Sgordon
12698186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
127146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
12898186Sgordon		warn "Unable to force ${_depend}. It may already be running."
12998186Sgordon		return 1
13098186Sgordon	fi
13198186Sgordon	return 0
13298186Sgordon}
13398186Sgordon
13498186Sgordon#
13578344Sobrien# checkyesno var
13678344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
13778344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
13878344Sobrien#
13978344Sobriencheckyesno()
14078344Sobrien{
14178344Sobrien	eval _value=\$${1}
14298186Sgordon	debug "checkyesno: $1 is set to $_value."
14378344Sobrien	case $_value in
14478344Sobrien
14578344Sobrien		#	"yes", "true", "on", or "1"
14678344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
14778344Sobrien		return 0
14878344Sobrien		;;
14978344Sobrien
15078344Sobrien		#	"no", "false", "off", or "0"
15178344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
15278344Sobrien		return 1
15378344Sobrien		;;
15478344Sobrien	*)
155157473Sflz		warn "\$${1} is not set properly - see ${rcvar_manpage}."
15678344Sobrien		return 1
15778344Sobrien		;;
15878344Sobrien	esac
15978344Sobrien}
16078344Sobrien
161157473Sflz#
16298186Sgordon# reverse_list list
16398186Sgordon#	print the list in reverse order
16478344Sobrien#
16598186Sgordonreverse_list()
16698186Sgordon{
16798186Sgordon	_revlist=
168126286Smtm	for _revfile; do
16998186Sgordon		_revlist="$_revfile $_revlist"
17098186Sgordon	done
17198186Sgordon	echo $_revlist
17298186Sgordon}
17398186Sgordon
174169668Smtm# stop_boot always
175169668Smtm#	If booting directly to multiuser or $always is enabled,
176169668Smtm#	send SIGTERM to the parent (/etc/rc) to abort the boot.
177169668Smtm#	Otherwise just exit.
17878344Sobrien#
179169668Smtmstop_boot()
180169668Smtm{
181169668Smtm	local always
182169668Smtm
183178776Smaxim	case $1 in
184178776Smaxim		#	"yes", "true", "on", or "1"
185178770Smtm        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
186169668Smtm		always=true
187178770Smtm		;;
188178770Smtm	*)
189169668Smtm		always=false
190178770Smtm		;;
191178775Smaxim	esac
192169668Smtm	if [ "$autoboot" = yes -o "$always" = true ]; then
193169668Smtm		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
194169668Smtm		kill -TERM ${RC_PID}
195169668Smtm	fi
196169668Smtm	exit 1
197169668Smtm}
198169668Smtm
199169668Smtm#
20098186Sgordon# mount_critical_filesystems type
20198186Sgordon#	Go through the list of critical filesystems as provided in
20298186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
20398186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
20498186Sgordon#
20578344Sobrienmount_critical_filesystems()
20678344Sobrien{
20798186Sgordon	eval _fslist=\$critical_filesystems_${1}
20878344Sobrien	for _fs in $_fslist; do
20978344Sobrien		mount | (
210126285Smtm			_ismounted=false
21178344Sobrien			while read what _on on _type type; do
21278344Sobrien				if [ $on = $_fs ]; then
213126285Smtm					_ismounted=true
21478344Sobrien				fi
21578344Sobrien			done
216126285Smtm			if $_ismounted; then
217126285Smtm				:
218126285Smtm			else
21978344Sobrien				mount $_fs >/dev/null 2>&1
22078344Sobrien			fi
22198186Sgordon		)
22278344Sobrien	done
22378344Sobrien}
22478344Sobrien
22578344Sobrien#
22698186Sgordon# check_pidfile pidfile procname [interpreter]
22798186Sgordon#	Parses the first line of pidfile for a PID, and ensures
22878344Sobrien#	that the process is running and matches procname.
22998186Sgordon#	Prints the matching PID upon success, nothing otherwise.
23098186Sgordon#	interpreter is optional; see _find_processes() for details.
23178344Sobrien#
23278344Sobriencheck_pidfile()
23378344Sobrien{
23478344Sobrien	_pidfile=$1
23578344Sobrien	_procname=$2
23698186Sgordon	_interpreter=$3
23778344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
23898186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
23978344Sobrien	fi
24078344Sobrien	if [ ! -f $_pidfile ]; then
241131061Smtm		debug "pid file ($_pidfile): not readable."
24278344Sobrien		return
24378344Sobrien	fi
24478344Sobrien	read _pid _junk < $_pidfile
24578344Sobrien	if [ -z "$_pid" ]; then
246139949Skeramida		debug "pid file ($_pidfile): no pid in file."
24778344Sobrien		return
24878344Sobrien	fi
24998186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
25078344Sobrien}
25178344Sobrien
25278344Sobrien#
25398186Sgordon# check_process procname [interpreter]
25478344Sobrien#	Ensures that a process (or processes) named procname is running.
25598186Sgordon#	Prints a list of matching PIDs.
25698186Sgordon#	interpreter is optional; see _find_processes() for details.
25778344Sobrien#
25878344Sobriencheck_process()
25978344Sobrien{
26078344Sobrien	_procname=$1
26198186Sgordon	_interpreter=$2
26278344Sobrien	if [ -z "$_procname" ]; then
26398186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
26478344Sobrien	fi
26598186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
26698186Sgordon}
26798186Sgordon
26898186Sgordon#
26998186Sgordon# _find_processes procname interpreter psargs
27098186Sgordon#	Search for procname in the output of ps generated by psargs.
27198186Sgordon#	Prints the PIDs of any matching processes, space separated.
27298186Sgordon#
27398186Sgordon#	If interpreter == ".", check the following variations of procname
27498186Sgordon#	against the first word of each command:
27598186Sgordon#		procname
27698186Sgordon#		`basename procname`
27798186Sgordon#		`basename procname` + ":"
27898186Sgordon#		"(" + `basename procname` + ")"
279155719Sceri#		"[" + `basename procname` + "]"
28098186Sgordon#
28198186Sgordon#	If interpreter != ".", read the first line of procname, remove the
28298186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
28398186Sgordon#	match that against each command, either as is, or with extra words
284157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
285157841Sflz#	using perl, the basename of the interpreter plus a colon is also
286157841Sflz#	tried as the prefix to procname.
28798186Sgordon#
28898186Sgordon_find_processes()
28998186Sgordon{
29098186Sgordon	if [ $# -ne 3 ]; then
29198186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
29298186Sgordon	fi
29398186Sgordon	_procname=$1
29498186Sgordon	_interpreter=$2
29598186Sgordon	_psargs=$3
29698186Sgordon
29778344Sobrien	_pref=
29898186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
299170282Syar		_script=${_chroot}${_chroot:+"/"}$_procname
300170282Syar		if [ -r $_script ]; then
301170282Syar			read _interp < $_script	# read interpreter name
302170282Syar			case "$_interp" in
303170282Syar			\#!*)
304170282Syar				_interp=${_interp#\#!}	# strip #!
305170282Syar				set -- $_interp
306170282Syar				case $1 in
307170282Syar				*/bin/env)
308170282Syar					shift	# drop env to get real name
309170282Syar					;;
310170282Syar				esac
311170282Syar				if [ $_interpreter != $1 ]; then
312170282Syar					warn "\$command_interpreter $_interpreter != $1"
313170282Syar				fi
314170282Syar				;;
315170282Syar			*)
316170282Syar				warn "no shebang line in $_script"
317170282Syar				set -- $_interpreter
318170282Syar				;;
319170282Syar			esac
320170282Syar		else
321170282Syar			warn "cannot read shebang line from $_script"
322170282Syar			set -- $_interpreter
32378344Sobrien		fi
32498186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
325157841Sflz		_interpbn=${1##*/}
32698186Sgordon		_fp_args='_argv'
32798186Sgordon		_fp_match='case "$_argv" in
328157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
32998186Sgordon	else					# a normal daemon
33098186Sgordon		_procnamebn=${_procname##*/}
33198186Sgordon		_fp_args='_arg0 _argv'
33298186Sgordon		_fp_match='case "$_arg0" in
333151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
33498186Sgordon	fi
33598186Sgordon
336161435Syar	_proccheck="\
337161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
338157657Sflz		while read _npid _jid '"$_fp_args"'; do
339161436Syar			'"$_fp_match"'
340157657Sflz				if [ "$JID" -eq "$_jid" ];
341157657Sflz				then echo -n "$_pref$_npid";
342157657Sflz				_pref=" ";
343157657Sflz				fi
34498186Sgordon				;;
34598186Sgordon			esac
34698186Sgordon		done'
34798186Sgordon
348114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
34998186Sgordon	eval $_proccheck
35098186Sgordon}
35198186Sgordon
35298186Sgordon#
35398186Sgordon# wait_for_pids pid [pid ...]
35498186Sgordon#	spins until none of the pids exist
35598186Sgordon#
35698186Sgordonwait_for_pids()
35798186Sgordon{
358206248Sdougb	local _list _prefix _nlist _j
359206248Sdougb
360126286Smtm	_list="$@"
36198186Sgordon	if [ -z "$_list" ]; then
36298186Sgordon		return
36398186Sgordon	fi
36498186Sgordon	_prefix=
36598186Sgordon	while true; do
36698186Sgordon		_nlist="";
36798186Sgordon		for _j in $_list; do
36898186Sgordon			if kill -0 $_j 2>/dev/null; then
36998186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
370206248Sdougb				[ -n "$_prefix" ] && sleep 1
37198186Sgordon			fi
37298186Sgordon		done
37398186Sgordon		if [ -z "$_nlist" ]; then
37498186Sgordon			break
37578344Sobrien		fi
37698186Sgordon		_list=$_nlist
37798186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
37898186Sgordon		_prefix=", "
379206248Sdougb		pwait $_list 2>/dev/null
38078344Sobrien	done
38198186Sgordon	if [ -n "$_prefix" ]; then
38298186Sgordon		echo "."
38398186Sgordon	fi
38478344Sobrien}
38578344Sobrien
38678344Sobrien#
387197947Sdougb# check_startmsgs
388197947Sdougb#	If rc_quiet is set (usually as a result of using faststart at
389197947Sdougb#	boot time) check if rc_startmsgs is enabled.
390197947Sdougb#
391197947Sdougbcheck_startmsgs()
392197947Sdougb{
393197947Sdougb	if [ -n "$rc_quiet" ]; then
394197947Sdougb		checkyesno rc_startmsgs
395197947Sdougb	else
396197947Sdougb		return 0
397197947Sdougb	fi
398197947Sdougb}
399197947Sdougb
400197947Sdougb#
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		;;
582198216Sed	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
605198162Sdougb	command=${_override_command:-$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					#
711197947Sdougb			check_startmsgs && 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\""
727201036Sdougb					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
947201038Sdougb#	return value from the script.
948201038Sdougb#	If `file' ends with `.sh', it's sourced into the current environment
949201038Sdougb#	when $rc_fast_and_loose is set, otherwise it is run as a child process.
950201038Sdougb#	If `file' appears to be a backup or scratch file, ignore it.
951201038Sdougb#	Otherwise if it is executable run as a child process.
95278344Sobrien#
95378344Sobrienrun_rc_script()
95478344Sobrien{
95578344Sobrien	_file=$1
95678344Sobrien	_arg=$2
95778344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
95878344Sobrien		err 3 'USAGE: run_rc_script file arg'
95978344Sobrien	fi
96078344Sobrien
96198186Sgordon	unset	name command command_args command_interpreter \
96298186Sgordon		extra_commands pidfile procname \
963197144Shrs		rcvar rcvars rcvars_obsolete required_dirs required_files \
964197144Shrs		required_vars
96598186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
96698186Sgordon
96778344Sobrien	case "$_file" in
968193118Sdougb	/etc/rc.d/*.sh)			# no longer allowed in the base
969193118Sdougb		warn "Ignoring old-style startup script $_file"
97078344Sobrien		;;
971153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
97298186Sgordon		warn "Ignoring scratch file $_file"
97398186Sgordon		;;
97478344Sobrien	*)				# run in subshell
97598186Sgordon		if [ -x $_file ]; then
97698186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
977146490Sschweikh				set $_arg; . $_file
97898186Sgordon			else
979130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
980130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
981184317Sthompsa				  trap "echo Script $_file running" 29
982146490Sschweikh				  set $_arg; . $_file )
98398186Sgordon			fi
98498186Sgordon		fi
98578344Sobrien		;;
98678344Sobrien	esac
98778344Sobrien}
98878344Sobrien
98978344Sobrien#
990157653Sflz# load_rc_config name
991157653Sflz#	Source in the configuration file for a given name.
99278344Sobrien#
99378344Sobrienload_rc_config()
99478344Sobrien{
995197144Shrs	local _name _var _defval _v _msg _new
996157653Sflz	_name=$1
997157653Sflz	if [ -z "$_name" ]; then
998157653Sflz		err 3 'USAGE: load_rc_config name'
99978344Sobrien	fi
100078344Sobrien
1001126285Smtm	if ${_rc_conf_loaded:-false}; then
1002126285Smtm		:
1003126285Smtm	else
100498186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
100598186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
100698186Sgordon			. /etc/defaults/rc.conf
100798186Sgordon			source_rc_confs
100898186Sgordon		elif [ -r /etc/rc.conf ]; then
100998186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
101098186Sgordon			. /etc/rc.conf
101198186Sgordon		fi
1012126285Smtm		_rc_conf_loaded=true
101398186Sgordon	fi
1014161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
1015157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
1016161530Sflz		. /etc/rc.conf.d/"$_name"
1017157653Sflz	fi
1018179872Smtm
1019179872Smtm	# Old variable names support
1020179872Smtm	#
1021179872Smtm	[ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1022197144Shrs
1023197144Shrs	# Set defaults if defined.
1024197144Shrs	for _var in $rcvar $rcvars; do
1025197144Shrs		_defval=`eval echo "\\\$${_var}_defval"`
1026197144Shrs		if [ -n "$_defval" ]; then
1027197144Shrs			eval : \${$_var:=\$${_var}_defval}
1028197144Shrs		fi
1029197144Shrs	done
1030197144Shrs
1031197144Shrs	# check obsolete rc.conf variables
1032197144Shrs	for _var in $rcvars_obsolete; do
1033197144Shrs		_v=`eval echo \\$$_var`
1034197144Shrs		_msg=`eval echo \\$${_var}_obsolete_msg`
1035197144Shrs		_new=`eval echo \\$${_var}_newvar`
1036197144Shrs		case $_v in
1037197144Shrs		"")
1038197144Shrs			;;
1039197144Shrs		*)
1040197144Shrs			if [ -z "$_new" ]; then
1041197144Shrs				_msg="Ignored."
1042197144Shrs			else
1043197144Shrs				eval $_new=\"\$$_var\"
1044197144Shrs				if [ -z "$_msg" ]; then
1045197144Shrs					_msg="Use \$$_new instead."
1046197144Shrs				fi
1047197144Shrs			fi
1048197144Shrs			warn "\$$_var is obsolete.  $_msg"
1049197144Shrs			;;
1050197144Shrs		esac
1051197144Shrs	done
105278344Sobrien}
1053201036Sdougb
1054157473Sflz#
1055157653Sflz# load_rc_config_var name var
1056157653Sflz#	Read the rc.conf(5) var for name and set in the
1057157473Sflz#	current shell, using load_rc_config in a subshell to prevent
1058157473Sflz#	unwanted side effects from other variable assignments.
1059157473Sflz#
1060157473Sflzload_rc_config_var()
1061157473Sflz{
1062157473Sflz	if [ $# -ne 2 ]; then
1063157653Sflz		err 3 'USAGE: load_rc_config_var name var'
1064157473Sflz	fi
1065157473Sflz	eval $(eval '(
1066157473Sflz		load_rc_config '$1' >/dev/null;
1067157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1068157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
1069157473Sflz		fi
1070157473Sflz	)' )
1071157473Sflz}
107278344Sobrien
107378344Sobrien#
107478344Sobrien# rc_usage commands
107578344Sobrien#	Print a usage string for $0, with `commands' being a list of
107678344Sobrien#	valid commands.
107778344Sobrien#
107878344Sobrienrc_usage()
107978344Sobrien{
1080126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
108178344Sobrien
108278344Sobrien	_sep=
1083126286Smtm	for _elem; do
108478344Sobrien		echo -n 1>&2 "$_sep$_elem"
108578344Sobrien		_sep="|"
108678344Sobrien	done
108778344Sobrien	echo 1>&2 ")"
108878344Sobrien	exit 1
108978344Sobrien}
109078344Sobrien
109178344Sobrien#
109278344Sobrien# err exitval message
109378344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
109478344Sobrien#
109578344Sobrienerr()
109678344Sobrien{
109778344Sobrien	exitval=$1
109878344Sobrien	shift
109978344Sobrien
1100106643Sgordon	if [ -x /usr/bin/logger ]; then
1101106643Sgordon		logger "$0: ERROR: $*"
1102106643Sgordon	fi
1103106643Sgordon	echo 1>&2 "$0: ERROR: $*"
110478344Sobrien	exit $exitval
110578344Sobrien}
110678344Sobrien
110778344Sobrien#
110878344Sobrien# warn message
110978344Sobrien#	Display message to stderr and log to the syslog.
111078344Sobrien#
111178344Sobrienwarn()
111278344Sobrien{
1113106643Sgordon	if [ -x /usr/bin/logger ]; then
1114106643Sgordon		logger "$0: WARNING: $*"
1115106643Sgordon	fi
1116106643Sgordon	echo 1>&2 "$0: WARNING: $*"
111778344Sobrien}
111898186Sgordon
111998186Sgordon#
112098186Sgordon# info message
112198186Sgordon#	Display informational message to stdout and log to syslog.
112298186Sgordon#
112398186Sgordoninfo()
112498186Sgordon{
1125119170Smtm	case ${rc_info} in
1126119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1127119170Smtm		if [ -x /usr/bin/logger ]; then
1128119170Smtm			logger "$0: INFO: $*"
1129119170Smtm		fi
1130119170Smtm		echo "$0: INFO: $*"
1131119170Smtm		;;
1132119170Smtm	esac
113398186Sgordon}
113498186Sgordon
113598186Sgordon#
113698186Sgordon# debug message
1137106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
113898186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
113998186Sgordon#	function.
114098186Sgordon#
114198186Sgordondebug()
114298186Sgordon{
114398186Sgordon	case ${rc_debug} in
114498186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1145106700Sgordon		if [ -x /usr/bin/logger ]; then
1146162947Syar			logger "$0: DEBUG: $*"
1147106700Sgordon		fi
1148146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
114998186Sgordon		;;
115098186Sgordon	esac
115198186Sgordon}
115298186Sgordon
115398186Sgordon#
115498186Sgordon# backup_file action file cur backup
115598186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
115698186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
115798186Sgordon#
115898186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
115998186Sgordon#	which can be either YES or NO.
116098186Sgordon#
116198186Sgordon#	The `action' keyword can be one of the following:
116298186Sgordon#
116398186Sgordon#	add		`file' is now being backed up (and is possibly
116498186Sgordon#			being reentered into the backups system).  `cur'
116598186Sgordon#			is created and RCS files, if necessary, are
116698186Sgordon#			created as well.
116798186Sgordon#
116898186Sgordon#	update		`file' has changed and needs to be backed up.
116998186Sgordon#			If `cur' exists, it is copied to to `back' or
117098186Sgordon#			checked into RCS (if the repository file is old),
117198186Sgordon#			and then `file' is copied to `cur'.  Another RCS
117298186Sgordon#			check in done here if RCS is being used.
117398186Sgordon#
117498186Sgordon#	remove		`file' is no longer being tracked by the backups
117598186Sgordon#			system.  If RCS is not being used, `cur' is moved
117698186Sgordon#			to `back', otherwise an empty file is checked in,
117798186Sgordon#			and then `cur' is removed.
117898186Sgordon#
117998186Sgordon#
118098186Sgordonbackup_file()
118198186Sgordon{
118298186Sgordon	_action=$1
118398186Sgordon	_file=$2
118498186Sgordon	_cur=$3
118598186Sgordon	_back=$4
118698186Sgordon
118798186Sgordon	if checkyesno backup_uses_rcs; then
118898186Sgordon		_msg0="backup archive"
118998186Sgordon		_msg1="update"
119098186Sgordon
119198186Sgordon		# ensure that history file is not locked
119298186Sgordon		if [ -f $_cur,v ]; then
119398186Sgordon			rcs -q -u -U -M $_cur
119498186Sgordon		fi
119598186Sgordon
119698186Sgordon		# ensure after switching to rcs that the
119798186Sgordon		# current backup is not lost
119898186Sgordon		if [ -f $_cur ]; then
119998186Sgordon			# no archive, or current newer than archive
120098186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
120198186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
120298186Sgordon				rcs -q -kb -U $_cur
120398186Sgordon				co -q -f -u $_cur
120498186Sgordon			fi
120598186Sgordon		fi
120698186Sgordon
120798186Sgordon		case $_action in
120898186Sgordon		add|update)
120998186Sgordon			cp -p $_file $_cur
121098186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
121198186Sgordon			rcs -q -kb -U $_cur
121298186Sgordon			co -q -f -u $_cur
121398186Sgordon			chown root:wheel $_cur $_cur,v
121498186Sgordon			;;
121598186Sgordon		remove)
121698186Sgordon			cp /dev/null $_cur
121798186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
121898186Sgordon			rcs -q -kb -U $_cur
121998186Sgordon			chown root:wheel $_cur $_cur,v
122098186Sgordon			rm $_cur
122198186Sgordon			;;
122298186Sgordon		esac
122398186Sgordon	else
122498186Sgordon		case $_action in
122598186Sgordon		add|update)
122698186Sgordon			if [ -f $_cur ]; then
122798186Sgordon				cp -p $_cur $_back
122898186Sgordon			fi
122998186Sgordon			cp -p $_file $_cur
123098186Sgordon			chown root:wheel $_cur
123198186Sgordon			;;
123298186Sgordon		remove)
123398186Sgordon			mv -f $_cur $_back
123498186Sgordon			;;
123598186Sgordon		esac
123698186Sgordon	fi
123798186Sgordon}
1238119166Smtm
1239123344Smtm# make_symlink src link
1240123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1241123344Smtm#	directory in which link is to be created does not exist
1242123344Smtm#	a warning will be displayed and an error will be returned.
1243123344Smtm#	Returns 0 on sucess, 1 otherwise.
1244119166Smtm#
1245123344Smtmmake_symlink()
1246119166Smtm{
1247123344Smtm	local src link linkdir _me
1248123344Smtm	src="$1"
1249123344Smtm	link="$2"
1250123344Smtm	linkdir="`dirname $link`"
1251123344Smtm	_me="make_symlink()"
1252119166Smtm
1253123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1254123344Smtm		warn "$_me: requires two arguments."
1255119166Smtm		return 1
1256119166Smtm	fi
1257123344Smtm	if [ ! -d "$linkdir" ]; then
1258160667Syar		warn "$_me: the directory $linkdir does not exist."
1259119166Smtm		return 1
1260119166Smtm	fi
1261146490Sschweikh	if ! ln -sf $src $link; then
1262123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1263119166Smtm		return 1
1264119166Smtm	fi
1265119166Smtm	return 0
1266119166Smtm}
1267119166Smtm
1268119166Smtm# devfs_rulesets_from_file file
1269119166Smtm#	Reads a set of devfs commands from file, and creates
1270119166Smtm#	the specified rulesets with their rules. Returns non-zero
1271119166Smtm#	if there was an error.
1272119166Smtm#
1273119166Smtmdevfs_rulesets_from_file()
1274119166Smtm{
1275119166Smtm	local file _err _me
1276119166Smtm	file="$1"
1277119166Smtm	_me="devfs_rulesets_from_file"
1278119166Smtm	_err=0
1279119166Smtm
1280119166Smtm	if [ -z "$file" ]; then
1281119166Smtm		warn "$_me: you must specify a file"
1282119166Smtm		return 1
1283119166Smtm	fi
1284119166Smtm	if [ ! -e "$file" ]; then
1285119166Smtm		debug "$_me: no such file ($file)"
1286119166Smtm		return 0
1287119166Smtm	fi
1288119166Smtm	debug "reading rulesets from file ($file)"
1289119166Smtm	{ while read line
1290119166Smtm	do
1291119166Smtm		case $line in
1292119166Smtm		\#*)
1293119166Smtm			continue
1294119166Smtm			;;
1295119166Smtm		\[*\]*)
1296119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1297119166Smtm			if [ -z "$rulenum" ]; then
1298119166Smtm				warn "$_me: cannot extract rule number ($line)"
1299119166Smtm				_err=1
1300119166Smtm				break
1301119166Smtm			fi
1302119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1303119166Smtm			if [ -z "$rulename" ]; then
1304119166Smtm				warn "$_me: cannot extract rule name ($line)"
1305119166Smtm				_err=1
1306119166Smtm				break;
1307119166Smtm			fi
1308119166Smtm			eval $rulename=\$rulenum
1309119166Smtm			debug "found ruleset: $rulename=$rulenum"
1310146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1311119166Smtm				_err=1
1312119166Smtm				break
1313119166Smtm			fi
1314119166Smtm			;;
1315119166Smtm		*)
1316119166Smtm			rulecmd="${line%%"\#*"}"
1317119166Smtm			# evaluate the command incase it includes
1318119166Smtm			# other rules
1319119166Smtm			if [ -n "$rulecmd" ]; then
1320119166Smtm				debug "adding rule ($rulecmd)"
1321119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1322119166Smtm				then
1323119166Smtm					_err=1
1324119166Smtm					break
1325119166Smtm				fi
1326119166Smtm			fi
1327119166Smtm			;;
1328119166Smtm		esac
1329119166Smtm		if [ $_err -ne 0 ]; then
1330119166Smtm			debug "error in $_me"
1331119166Smtm			break
1332119166Smtm		fi
1333119166Smtm	done } < $file
1334119166Smtm	return $_err
1335119166Smtm}
1336119166Smtm
1337119166Smtm# devfs_init_rulesets
1338119166Smtm#	Initializes rulesets from configuration files. Returns
1339119166Smtm#	non-zero if there was an error.
1340119166Smtm#
1341119166Smtmdevfs_init_rulesets()
1342119166Smtm{
1343119166Smtm	local file _me
1344119166Smtm	_me="devfs_init_rulesets"
1345119166Smtm
1346119166Smtm	# Go through this only once
1347119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1348119166Smtm		debug "$_me: devfs rulesets already initialized"
1349119166Smtm		return
1350119166Smtm	fi
1351146490Sschweikh	for file in $devfs_rulesets; do
1352119166Smtm		devfs_rulesets_from_file $file || return 1
1353119166Smtm	done
1354119166Smtm	devfs_rulesets_init=1
1355119166Smtm	debug "$_me: devfs rulesets initialized"
1356119166Smtm	return 0
1357119166Smtm}
1358119166Smtm
1359119166Smtm# devfs_set_ruleset ruleset [dir]
1360151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1361119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1362119166Smtm#	Returns non-zero if it could not set it successfully.
1363119166Smtm#
1364119166Smtmdevfs_set_ruleset()
1365119166Smtm{
1366119166Smtm	local devdir rs _me
1367119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1368119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1369119166Smtm	_me="devfs_set_ruleset"
1370119166Smtm
1371119166Smtm	if [ -z "$rs" ]; then
1372119166Smtm		warn "$_me: you must specify a ruleset number"
1373119166Smtm		return 1
1374119166Smtm	fi
1375119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1376146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1377119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1378119166Smtm		return 1
1379119166Smtm	fi
1380119166Smtm	return 0
1381119166Smtm}
1382119166Smtm
1383119166Smtm# devfs_apply_ruleset ruleset [dir]
1384119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1385119166Smtm#	The ruleset argument must be a ruleset name as specified
1386119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1387119166Smtm#	if it could not apply the ruleset.
1388119166Smtm#
1389119166Smtmdevfs_apply_ruleset()
1390119166Smtm{
1391119166Smtm	local devdir rs _me
1392119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1393119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1394119166Smtm	_me="devfs_apply_ruleset"
1395119166Smtm
1396119166Smtm	if [ -z "$rs" ]; then
1397119166Smtm		warn "$_me: you must specify a ruleset"
1398119166Smtm		return 1
1399119166Smtm	fi
1400119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1401146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1402119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1403119166Smtm		return 1
1404119166Smtm	fi
1405119166Smtm	return 0
1406119166Smtm}
1407119166Smtm
1408119166Smtm# devfs_domount dir [ruleset]
1409119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1410119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1411119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1412119166Smtm#
1413119166Smtmdevfs_domount()
1414119166Smtm{
1415119166Smtm	local devdir rs _me
1416119166Smtm	devdir="$1"
1417119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1418119166Smtm	_me="devfs_domount()"
1419119166Smtm
1420119166Smtm	if [ -z "$devdir" ]; then
1421119166Smtm		warn "$_me: you must specify a mount-point"
1422119166Smtm		return 1
1423119166Smtm	fi
1424119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1425146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1426119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1427119166Smtm		return 1
1428119166Smtm	fi
1429119166Smtm	if [ -n "$rs" ]; then
1430119166Smtm		devfs_init_rulesets
1431119166Smtm		devfs_set_ruleset $rs $devdir
1432124797Scperciva		devfs -m $devdir rule applyset
1433119166Smtm	fi
1434119166Smtm	return 0
1435119166Smtm}
1436119166Smtm
1437119166Smtm# devfs_mount_jail dir [ruleset]
1438119166Smtm#	Mounts a devfs file system appropriate for jails
1439119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1440119166Smtm#	it names will be used instead.  If present, ruleset must
1441119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1442119166Smtm#	This function returns non-zero if an error occurs.
1443119166Smtm#
1444119166Smtmdevfs_mount_jail()
1445119166Smtm{
1446119166Smtm	local jdev rs _me
1447119166Smtm	jdev="$1"
1448119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1449119166Smtm	_me="devfs_mount_jail"
1450119166Smtm
1451119166Smtm	devfs_init_rulesets
1452146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1453119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1454119166Smtm		return 1
1455119166Smtm	fi
1456119166Smtm	return 0
1457119166Smtm}
1458127345Sbrooks
1459127345Sbrooks# Provide a function for normalizing the mounting of memory
1460127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1461127345Sbrooks# as close as possible between 5-current and 4-stable.
1462127345Sbrooks#   $1 = size
1463127345Sbrooks#   $2 = mount point
1464137451Skeramida#   $3 = (optional) extra mdmfs flags
1465146490Sschweikhmount_md()
1466146490Sschweikh{
1467127345Sbrooks	if [ -n "$3" ]; then
1468137451Skeramida		flags="$3"
1469127345Sbrooks	fi
1470149421Syar	/sbin/mdmfs $flags -s $1 md $2
1471127345Sbrooks}
1472131550Scperciva
1473159828Syar# Code common to scripts that need to load a kernel module
1474159828Syar# if it isn't in the kernel yet. Syntax:
1475160666Syar#   load_kld [-e regex] [-m module] file
1476159828Syar# where -e or -m chooses the way to check if the module
1477159828Syar# is already loaded:
1478160666Syar#   regex is egrep'd in the output from `kldstat -v',
1479160666Syar#   module is passed to `kldstat -m'.
1480160666Syar# The default way is as though `-m file' were specified.
1481159828Syarload_kld()
1482159828Syar{
1483159828Syar	local _loaded _mod _opt _re
1484159828Syar
1485159828Syar	while getopts "e:m:" _opt; do
1486159828Syar		case "$_opt" in
1487159828Syar		e) _re="$OPTARG" ;;
1488159828Syar		m) _mod="$OPTARG" ;;
1489160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1490159828Syar		esac
1491159828Syar	done
1492159828Syar	shift $(($OPTIND - 1))
1493160666Syar	if [ $# -ne 1 ]; then
1494160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1495160666Syar	fi
1496159828Syar	_mod=${_mod:-$1}
1497159828Syar	_loaded=false
1498159828Syar	if [ -n "$_re" ]; then
1499159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1500159828Syar			_loaded=true
1501159828Syar		fi
1502159828Syar	else
1503159828Syar		if kldstat -q -m "$_mod"; then
1504159828Syar			_loaded=true
1505159828Syar		fi
1506159828Syar	fi
1507159828Syar	if ! $_loaded; then
1508159828Syar		if ! kldload "$1"; then
1509159828Syar			warn "Unable to load kernel module $1"
1510159828Syar			return 1
1511160666Syar		else
1512160666Syar			info "$1 kernel module loaded."
1513159828Syar		fi
1514160666Syar	else
1515160666Syar		debug "load_kld: $1 kernel module already loaded."
1516159828Syar	fi
1517159828Syar	return 0
1518159828Syar}
1519159828Syar
1520149049Spjd# ltr str src dst
1521149049Spjd#	Change every $src in $str to $dst.
1522149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1523149049Spjd#	awk(1).
1524149049Spjdltr()
1525149049Spjd{
1526149049Spjd	local _str _src _dst _out _com
1527149049Spjd	_str=$1
1528149049Spjd	_src=$2
1529149049Spjd	_dst=$3
1530149049Spjd	_out=""
1531149049Spjd
1532149049Spjd	IFS=${_src}
1533149049Spjd	for _com in ${_str}; do
1534149049Spjd		if [ -z "${_out}" ]; then
1535149049Spjd			_out="${_com}"
1536149049Spjd		else
1537149049Spjd			_out="${_out}${_dst}${_com}"
1538149049Spjd		fi
1539149049Spjd	done
1540149049Spjd	echo "${_out}"
1541149049Spjd}
1542149049Spjd
1543149050Spjd# Creates a list of providers for GELI encryption.
1544149050Spjdgeli_make_list()
1545149050Spjd{
1546149050Spjd	local devices devices2
1547149050Spjd	local provider mountpoint type options rest
1548149050Spjd
1549149050Spjd	# Create list of GELI providers from fstab.
1550149050Spjd	while read provider mountpoint type options rest ; do
1551155570Sflz		case ":${options}" in
1552155570Sflz		:*noauto*)
1553155570Sflz			noauto=yes
1554155570Sflz			;;
1555155570Sflz		*)
1556155570Sflz			noauto=no
1557155570Sflz			;;
1558155570Sflz		esac
1559155570Sflz
1560149050Spjd		case ":${provider}" in
1561149050Spjd		:#*)
1562149050Spjd			continue
1563149050Spjd			;;
1564149050Spjd		*.eli)
1565149050Spjd			# Skip swap devices.
1566155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1567149050Spjd				continue
1568149050Spjd			fi
1569149050Spjd			devices="${devices} ${provider}"
1570149050Spjd			;;
1571149050Spjd		esac
1572149050Spjd	done < /etc/fstab
1573149050Spjd
1574149050Spjd	# Append providers from geli_devices.
1575149050Spjd	devices="${devices} ${geli_devices}"
1576149050Spjd
1577149050Spjd	for provider in ${devices}; do
1578149050Spjd		provider=${provider%.eli}
1579149050Spjd		provider=${provider#/dev/}
1580149050Spjd		devices2="${devices2} ${provider}"
1581149050Spjd	done
1582149050Spjd
1583149050Spjd	echo ${devices2}
1584149050Spjd}
1585149050Spjd
1586153027Sdougb# Find scripts in local_startup directories that use the old syntax
1587153027Sdougb#
1588153027Sdougbfind_local_scripts_old () {
1589153027Sdougb	zlist=''
1590153027Sdougb	slist=''
1591153027Sdougb	for dir in ${local_startup}; do
1592153027Sdougb		if [ -d "${dir}" ]; then
1593153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1594153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1595153027Sdougb				    continue
1596153027Sdougb				zlist="$zlist $file"
1597153027Sdougb			done
1598153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1599153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1600153027Sdougb				    continue
1601153027Sdougb				slist="$slist $file"
1602153027Sdougb			done
1603153027Sdougb		fi
1604153027Sdougb	done
1605153027Sdougb}
1606153027Sdougb
1607153027Sdougbfind_local_scripts_new () {
1608153027Sdougb	local_rc=''
1609153027Sdougb	for dir in ${local_startup}; do
1610153027Sdougb		if [ -d "${dir}" ]; then
1611153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1612153027Sdougb				case "$file" in
1613153027Sdougb				*.sample) ;;
1614153027Sdougb				*)	if [ -x "$file" ]; then
1615153027Sdougb						local_rc="${local_rc} ${file}"
1616153027Sdougb					fi
1617153027Sdougb					;;
1618153027Sdougb				esac
1619153027Sdougb			done
1620153027Sdougb		fi
1621153027Sdougb	done
1622153027Sdougb}
1623153027Sdougb
1624165565Syar# check_required_{before|after} command
1625165565Syar#	Check for things required by the command before and after its precmd,
1626165565Syar#	respectively.  The two separate functions are needed because some
1627165565Syar#	conditions should prevent precmd from being run while other things
1628165565Syar#	depend on precmd having already been run.
1629165565Syar#
1630165565Syarcheck_required_before()
1631165565Syar{
1632165565Syar	local _f
1633165565Syar
1634165565Syar	case "$1" in
1635165565Syar	start)
1636165565Syar		for _f in $required_vars; do
1637165565Syar			if ! checkyesno $_f; then
1638165565Syar				warn "\$${_f} is not enabled."
1639165565Syar				if [ -z "$rc_force" ]; then
1640165565Syar					return 1
1641165565Syar				fi
1642165565Syar			fi
1643165565Syar		done
1644165565Syar
1645165565Syar		for _f in $required_dirs; do
1646165565Syar			if [ ! -d "${_f}/." ]; then
1647165565Syar				warn "${_f} is not a directory."
1648165565Syar				if [ -z "$rc_force" ]; then
1649165565Syar					return 1
1650165565Syar				fi
1651165565Syar			fi
1652165565Syar		done
1653165565Syar
1654165565Syar		for _f in $required_files; do
1655165565Syar			if [ ! -r "${_f}" ]; then
1656165565Syar				warn "${_f} is not readable."
1657165565Syar				if [ -z "$rc_force" ]; then
1658165565Syar					return 1
1659165565Syar				fi
1660165565Syar			fi
1661165565Syar		done
1662165565Syar		;;
1663165565Syar	esac
1664165565Syar
1665165565Syar	return 0
1666165565Syar}
1667165565Syar
1668165565Syarcheck_required_after()
1669165565Syar{
1670165565Syar	local _f _args
1671165565Syar
1672165565Syar	case "$1" in
1673165565Syar	start)
1674165565Syar		for _f in $required_modules; do
1675165565Syar			case "${_f}" in
1676165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1677165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1678165565Syar				*)	_args="${_f}" ;;
1679165565Syar			esac
1680165565Syar			if ! load_kld ${_args}; then
1681165565Syar				if [ -z "$rc_force" ]; then
1682165565Syar					return 1
1683165565Syar				fi
1684165565Syar			fi
1685165565Syar		done
1686165565Syar		;;
1687165565Syar	esac
1688165565Syar
1689165565Syar	return 0
1690165565Syar}
1691165565Syar
1692131550Scpercivafi
1693157841Sflz
1694197144Shrs# _echoonce var msg mode
1695197144Shrs#	mode=0: Echo $msg if ${$var} is empty.
1696197144Shrs#	        After doing echo, a string is set to ${$var}.
1697197144Shrs#
1698197144Shrs#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
1699197144Shrs#
1700197144Shrs_echoonce()
1701197144Shrs{
1702197144Shrs	local _var _msg _mode
1703197144Shrs	_var=`eval echo \\$$1`
1704197144Shrs	_msg=$2
1705197144Shrs	_mode=$3
1706197144Shrs
1707197144Shrs	case $_mode in
1708197144Shrs	1)	[ -n "$_var" ] && echo "$_msg" ;;
1709197144Shrs	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1710197144Shrs	esac
1711197144Shrs}
1712197144Shrs
1713157841Sflz_rc_subr_loaded=:
1714