rc.subr revision 202988
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 202988 2010-01-25 20:59:04Z emaste $
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"
55202988SemasteSYSCTL_W="${SYSCTL}"
56124832SmtmID="/usr/bin/id"
57124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
58161435SyarPS="/bin/ps -ww"
59161435SyarJID=`$PS -p $$ -o jid=`
6098186Sgordon
6198186Sgordon#
6278344Sobrien#	functions
6378344Sobrien#	---------
6478344Sobrien
65197144Shrs# set_rcvar [var] [defval] [desc]
6678344Sobrien#
67197144Shrs#	Echo or define a rc.conf(5) variable name.  Global variable
68197144Shrs#	$rcvars is used.
6998186Sgordon#
70197144Shrs#	If no argument is specified, echo "${name}_enable".
71197144Shrs#
72197144Shrs#	If only a var is specified, echo "${var}_enable".
73197144Shrs#
74197144Shrs#	If var and defval are specified, the ${var} is defined as
75197144Shrs#	rc.conf(5) variable and the default value is ${defvar}.  An
76197144Shrs#	optional argument $desc can also be specified to add a
77197144Shrs#	description for that.
78197144Shrs#
7998186Sgordonset_rcvar()
8098186Sgordon{
81197144Shrs	case $# in
82197144Shrs	0)
83197144Shrs		echo ${name}_enable
8498186Sgordon		;;
85197144Shrs	1)
86197144Shrs		echo ${1}_enable
8798186Sgordon		;;
8898186Sgordon	*)
89197144Shrs		debug "rcvar_define: \$$1=$2 is added" \
90197144Shrs		    " as a rc.conf(5) variable."
91197144Shrs
92197144Shrs		local _var
93197144Shrs		_var=$1
94197144Shrs		rcvars="${rcvars# } $_var"
95197144Shrs		eval ${_var}_defval=\"$2\"
96197144Shrs		shift 2
97197144Shrs		# encode multiple lines of _desc
98197144Shrs		for l in "$@"; do
99197144Shrs			eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
100197144Shrs		done
101197144Shrs		eval ${_var}_desc=\"\${${_var}_desc#^^}\"
10298186Sgordon		;;
10398186Sgordon	esac
10498186Sgordon}
10598186Sgordon
106197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg]
107197144Shrs#	Define obsolete variable.
108197144Shrs#	Global variable $rcvars_obsolete is used.
10998186Sgordon#
110197144Shrsset_rcvar_obsolete()
111197144Shrs{
112197144Shrs	local _var
113197144Shrs	_var=$1
114197144Shrs	debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
115197144Shrs
116197144Shrs	rcvars_obsolete="${rcvars_obsolete# } $1"
117197144Shrs	eval ${1}_newvar=\"$2\"
118197144Shrs	shift 2
119197144Shrs	eval ${_var}_obsolete_msg=\"$*\"
120197144Shrs}
121197144Shrs
122197144Shrs#
12398186Sgordon# force_depend script
12498186Sgordon#	Force a service to start. Intended for use by services
12598186Sgordon#	to resolve dependency issues. It is assumed the caller
12698186Sgordon#	has check to make sure this call is necessary
12798186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
12898186Sgordon#
12998186Sgordonforce_depend()
13098186Sgordon{
13198186Sgordon	_depend="$1"
13298186Sgordon
13398186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
134146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
13598186Sgordon		warn "Unable to force ${_depend}. It may already be running."
13698186Sgordon		return 1
13798186Sgordon	fi
13898186Sgordon	return 0
13998186Sgordon}
14098186Sgordon
14198186Sgordon#
14278344Sobrien# checkyesno var
14378344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
14478344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
14578344Sobrien#
14678344Sobriencheckyesno()
14778344Sobrien{
14878344Sobrien	eval _value=\$${1}
14998186Sgordon	debug "checkyesno: $1 is set to $_value."
15078344Sobrien	case $_value in
15178344Sobrien
15278344Sobrien		#	"yes", "true", "on", or "1"
15378344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
15478344Sobrien		return 0
15578344Sobrien		;;
15678344Sobrien
15778344Sobrien		#	"no", "false", "off", or "0"
15878344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
15978344Sobrien		return 1
16078344Sobrien		;;
16178344Sobrien	*)
162157473Sflz		warn "\$${1} is not set properly - see ${rcvar_manpage}."
16378344Sobrien		return 1
16478344Sobrien		;;
16578344Sobrien	esac
16678344Sobrien}
16778344Sobrien
168157473Sflz#
16998186Sgordon# reverse_list list
17098186Sgordon#	print the list in reverse order
17178344Sobrien#
17298186Sgordonreverse_list()
17398186Sgordon{
17498186Sgordon	_revlist=
175126286Smtm	for _revfile; do
17698186Sgordon		_revlist="$_revfile $_revlist"
17798186Sgordon	done
17898186Sgordon	echo $_revlist
17998186Sgordon}
18098186Sgordon
181169668Smtm# stop_boot always
182169668Smtm#	If booting directly to multiuser or $always is enabled,
183169668Smtm#	send SIGTERM to the parent (/etc/rc) to abort the boot.
184169668Smtm#	Otherwise just exit.
18578344Sobrien#
186169668Smtmstop_boot()
187169668Smtm{
188169668Smtm	local always
189169668Smtm
190178776Smaxim	case $1 in
191178776Smaxim		#	"yes", "true", "on", or "1"
192178770Smtm        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
193169668Smtm		always=true
194178770Smtm		;;
195178770Smtm	*)
196169668Smtm		always=false
197178770Smtm		;;
198178775Smaxim	esac
199169668Smtm	if [ "$autoboot" = yes -o "$always" = true ]; then
200169668Smtm		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
201169668Smtm		kill -TERM ${RC_PID}
202169668Smtm	fi
203169668Smtm	exit 1
204169668Smtm}
205169668Smtm
206169668Smtm#
20798186Sgordon# mount_critical_filesystems type
20898186Sgordon#	Go through the list of critical filesystems as provided in
20998186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
21098186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
21198186Sgordon#
21278344Sobrienmount_critical_filesystems()
21378344Sobrien{
21498186Sgordon	eval _fslist=\$critical_filesystems_${1}
21578344Sobrien	for _fs in $_fslist; do
21678344Sobrien		mount | (
217126285Smtm			_ismounted=false
21878344Sobrien			while read what _on on _type type; do
21978344Sobrien				if [ $on = $_fs ]; then
220126285Smtm					_ismounted=true
22178344Sobrien				fi
22278344Sobrien			done
223126285Smtm			if $_ismounted; then
224126285Smtm				:
225126285Smtm			else
22678344Sobrien				mount $_fs >/dev/null 2>&1
22778344Sobrien			fi
22898186Sgordon		)
22978344Sobrien	done
23078344Sobrien}
23178344Sobrien
23278344Sobrien#
23398186Sgordon# check_pidfile pidfile procname [interpreter]
23498186Sgordon#	Parses the first line of pidfile for a PID, and ensures
23578344Sobrien#	that the process is running and matches procname.
23698186Sgordon#	Prints the matching PID upon success, nothing otherwise.
23798186Sgordon#	interpreter is optional; see _find_processes() for details.
23878344Sobrien#
23978344Sobriencheck_pidfile()
24078344Sobrien{
24178344Sobrien	_pidfile=$1
24278344Sobrien	_procname=$2
24398186Sgordon	_interpreter=$3
24478344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
24598186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
24678344Sobrien	fi
24778344Sobrien	if [ ! -f $_pidfile ]; then
248131061Smtm		debug "pid file ($_pidfile): not readable."
24978344Sobrien		return
25078344Sobrien	fi
25178344Sobrien	read _pid _junk < $_pidfile
25278344Sobrien	if [ -z "$_pid" ]; then
253139949Skeramida		debug "pid file ($_pidfile): no pid in file."
25478344Sobrien		return
25578344Sobrien	fi
25698186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
25778344Sobrien}
25878344Sobrien
25978344Sobrien#
26098186Sgordon# check_process procname [interpreter]
26178344Sobrien#	Ensures that a process (or processes) named procname is running.
26298186Sgordon#	Prints a list of matching PIDs.
26398186Sgordon#	interpreter is optional; see _find_processes() for details.
26478344Sobrien#
26578344Sobriencheck_process()
26678344Sobrien{
26778344Sobrien	_procname=$1
26898186Sgordon	_interpreter=$2
26978344Sobrien	if [ -z "$_procname" ]; then
27098186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
27178344Sobrien	fi
27298186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
27398186Sgordon}
27498186Sgordon
27598186Sgordon#
27698186Sgordon# _find_processes procname interpreter psargs
27798186Sgordon#	Search for procname in the output of ps generated by psargs.
27898186Sgordon#	Prints the PIDs of any matching processes, space separated.
27998186Sgordon#
28098186Sgordon#	If interpreter == ".", check the following variations of procname
28198186Sgordon#	against the first word of each command:
28298186Sgordon#		procname
28398186Sgordon#		`basename procname`
28498186Sgordon#		`basename procname` + ":"
28598186Sgordon#		"(" + `basename procname` + ")"
286155719Sceri#		"[" + `basename procname` + "]"
28798186Sgordon#
28898186Sgordon#	If interpreter != ".", read the first line of procname, remove the
28998186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
29098186Sgordon#	match that against each command, either as is, or with extra words
291157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
292157841Sflz#	using perl, the basename of the interpreter plus a colon is also
293157841Sflz#	tried as the prefix to procname.
29498186Sgordon#
29598186Sgordon_find_processes()
29698186Sgordon{
29798186Sgordon	if [ $# -ne 3 ]; then
29898186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
29998186Sgordon	fi
30098186Sgordon	_procname=$1
30198186Sgordon	_interpreter=$2
30298186Sgordon	_psargs=$3
30398186Sgordon
30478344Sobrien	_pref=
30598186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
306170282Syar		_script=${_chroot}${_chroot:+"/"}$_procname
307170282Syar		if [ -r $_script ]; then
308170282Syar			read _interp < $_script	# read interpreter name
309170282Syar			case "$_interp" in
310170282Syar			\#!*)
311170282Syar				_interp=${_interp#\#!}	# strip #!
312170282Syar				set -- $_interp
313170282Syar				case $1 in
314170282Syar				*/bin/env)
315170282Syar					shift	# drop env to get real name
316170282Syar					;;
317170282Syar				esac
318170282Syar				if [ $_interpreter != $1 ]; then
319170282Syar					warn "\$command_interpreter $_interpreter != $1"
320170282Syar				fi
321170282Syar				;;
322170282Syar			*)
323170282Syar				warn "no shebang line in $_script"
324170282Syar				set -- $_interpreter
325170282Syar				;;
326170282Syar			esac
327170282Syar		else
328170282Syar			warn "cannot read shebang line from $_script"
329170282Syar			set -- $_interpreter
33078344Sobrien		fi
33198186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
332157841Sflz		_interpbn=${1##*/}
33398186Sgordon		_fp_args='_argv'
33498186Sgordon		_fp_match='case "$_argv" in
335157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
33698186Sgordon	else					# a normal daemon
33798186Sgordon		_procnamebn=${_procname##*/}
33898186Sgordon		_fp_args='_arg0 _argv'
33998186Sgordon		_fp_match='case "$_arg0" in
340151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
34198186Sgordon	fi
34298186Sgordon
343161435Syar	_proccheck="\
344161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
345157657Sflz		while read _npid _jid '"$_fp_args"'; do
346161436Syar			'"$_fp_match"'
347157657Sflz				if [ "$JID" -eq "$_jid" ];
348157657Sflz				then echo -n "$_pref$_npid";
349157657Sflz				_pref=" ";
350157657Sflz				fi
35198186Sgordon				;;
35298186Sgordon			esac
35398186Sgordon		done'
35498186Sgordon
355114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
35698186Sgordon	eval $_proccheck
35798186Sgordon}
35898186Sgordon
35998186Sgordon#
36098186Sgordon# wait_for_pids pid [pid ...]
36198186Sgordon#	spins until none of the pids exist
36298186Sgordon#
36398186Sgordonwait_for_pids()
36498186Sgordon{
365126286Smtm	_list="$@"
36698186Sgordon	if [ -z "$_list" ]; then
36798186Sgordon		return
36898186Sgordon	fi
36998186Sgordon	_prefix=
37098186Sgordon	while true; do
37198186Sgordon		_nlist="";
37298186Sgordon		for _j in $_list; do
37398186Sgordon			if kill -0 $_j 2>/dev/null; then
37498186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
37598186Sgordon			fi
37698186Sgordon		done
37798186Sgordon		if [ -z "$_nlist" ]; then
37898186Sgordon			break
37978344Sobrien		fi
38098186Sgordon		_list=$_nlist
38198186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
38298186Sgordon		_prefix=", "
383200818Sjilles		pwait $_list 2>/dev/null || sleep 2
38478344Sobrien	done
38598186Sgordon	if [ -n "$_prefix" ]; then
38698186Sgordon		echo "."
38798186Sgordon	fi
38878344Sobrien}
38978344Sobrien
39078344Sobrien#
391197947Sdougb# check_startmsgs
392197947Sdougb#	If rc_quiet is set (usually as a result of using faststart at
393197947Sdougb#	boot time) check if rc_startmsgs is enabled.
394197947Sdougb#
395197947Sdougbcheck_startmsgs()
396197947Sdougb{
397197947Sdougb	if [ -n "$rc_quiet" ]; then
398197947Sdougb		checkyesno rc_startmsgs
399197947Sdougb	else
400197947Sdougb		return 0
401197947Sdougb	fi
402197947Sdougb}
403197947Sdougb
404197947Sdougb#
40598186Sgordon# run_rc_command argument
40698186Sgordon#	Search for argument in the list of supported commands, which is:
40798186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
40898186Sgordon#	If there's a match, run ${argument}_cmd or the default method
40998186Sgordon#	(see below).
41078344Sobrien#
41198186Sgordon#	If argument has a given prefix, then change the operation as follows:
41298186Sgordon#		Prefix	Operation
41378344Sobrien#		------	---------
414175676Smtm#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
41598186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
416126303Smtm#		one	Set ${rcvar} to YES
417175676Smtm#		quiet	Don't output some diagnostics, and set rc_quiet=yes
41878344Sobrien#
41978344Sobrien#	The following globals are used:
42078344Sobrien#
42198186Sgordon#	Name		Needed	Purpose
42298186Sgordon#	----		------	-------
42378344Sobrien#	name		y	Name of script.
42478344Sobrien#
42578344Sobrien#	command		n	Full path to command.
42698186Sgordon#				Not needed if ${rc_arg}_cmd is set for
42778344Sobrien#				each keyword.
42878344Sobrien#
42978344Sobrien#	command_args	n	Optional args/shell directives for command.
43078344Sobrien#
43198186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
43298186Sgordon#				call check_{pidfile,process}() appropriately.
43398186Sgordon#
434197144Shrs#	desc		n	Description of script.
435197144Shrs#
43678344Sobrien#	extra_commands	n	List of extra commands supported.
43778344Sobrien#
43898186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
43998186Sgordon#				otherwise use check_process $command.
44098186Sgordon#				In either case, only check if $command is set.
44178344Sobrien#
44298186Sgordon#	procname	n	Process name to check for instead of $command.
44398186Sgordon#
44478344Sobrien#	rcvar		n	This is checked with checkyesno to determine
44578344Sobrien#				if the action should be run.
44678344Sobrien#
447157653Sflz#	${name}_program	n	Full path to command.
448157653Sflz#				Meant to be used in /etc/rc.conf to override
449157653Sflz#				${command}.
450157653Sflz#
45178344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
45298186Sgordon#				Requires /usr to be mounted.
45378344Sobrien#
45478344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
45578344Sobrien#				(if not using ${name}_chroot).
45678344Sobrien#
45778344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
45878344Sobrien#				NOTE:	$flags from the parent environment
45978344Sobrien#					can be used to override this.
46078344Sobrien#
46178344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
46278344Sobrien#
46378344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
46478344Sobrien#				using ${name}_chroot.
46598186Sgordon#				Requires /usr to be mounted.
46678344Sobrien#
46778344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
46898186Sgordon#				Requires /usr to be mounted.
46978344Sobrien#
47098186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
47198186Sgordon#				to run the chrooted ${command} with.
47298186Sgordon#				Requires /usr to be mounted.
47378344Sobrien#
47498186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
47578344Sobrien#				Otherwise, use default command (see below)
47678344Sobrien#
47798186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
47898186Sgordon#				${rc_arg}_cmd method in the default
47998186Sgordon#				operation (i.e, after checking for required
48098186Sgordon#				bits and process (non)existence).
48178344Sobrien#				If this completes with a non-zero exit code,
48298186Sgordon#				don't run ${rc_arg}_cmd.
48378344Sobrien#
48498186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
48598186Sgordon#				${rc_arg}_cmd method, if that method
48698186Sgordon#				returned a zero exit code.
48798186Sgordon#
48878344Sobrien#	required_dirs	n	If set, check for the existence of the given
489165565Syar#				directories before running a (re)start command.
49078344Sobrien#
49178344Sobrien#	required_files	n	If set, check for the readability of the given
492165565Syar#				files before running a (re)start command.
49378344Sobrien#
494165565Syar#	required_modules n	If set, ensure the given kernel modules are
495165565Syar#				loaded before running a (re)start command.
496165565Syar#				The check and possible loads are actually
497165565Syar#				done after start_precmd so that the modules
498165565Syar#				aren't loaded in vain, should the precmd
499165565Syar#				return a non-zero status to indicate a error.
500165565Syar#				If a word in the list looks like "foo:bar",
501165565Syar#				"foo" is the KLD file name and "bar" is the
502165565Syar#				module name.  If a word looks like "foo~bar",
503165565Syar#				"foo" is the KLD file name and "bar" is a
504165565Syar#				egrep(1) pattern matching the module name.
505165565Syar#				Otherwise the module name is assumed to be
506165565Syar#				the same as the KLD file name, which is most
507165565Syar#				common.  See load_kld().
508165565Syar#
50978344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
51078344Sobrien#				listed variables before running the default
51178344Sobrien#				(re)start command.
51278344Sobrien#
51398186Sgordon#	Default behaviour for a given argument, if no override method is
51498186Sgordon#	provided:
51578344Sobrien#
51698186Sgordon#	Argument	Default behaviour
51798186Sgordon#	--------	-----------------
51878344Sobrien#	start		if !running && checkyesno ${rcvar}
51978344Sobrien#				${command}
52078344Sobrien#
52178344Sobrien#	stop		if ${pidfile}
52298186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
52378344Sobrien#			else
52498186Sgordon#				rc_pid=$(check_process $command)
52598186Sgordon#			kill $sig_stop $rc_pid
52698186Sgordon#			wait_for_pids $rc_pid
52798186Sgordon#			($sig_stop defaults to TERM.)
52878344Sobrien#
52998186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
53098186Sgordon#			and doesn't wait_for_pids.
53178344Sobrien#			$sig_reload defaults to HUP.
532151685Syar#			Note that `reload' isn't provided by default,
533151685Syar#			it should be enabled via $extra_commands.
53478344Sobrien#
53578344Sobrien#	restart		Run `stop' then `start'.
53678344Sobrien#
53798186Sgordon#	status		Show if ${command} is running, etc.
53878344Sobrien#
53998186Sgordon#	poll		Wait for ${command} to exit.
54098186Sgordon#
54198186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
54298186Sgordon#
54398186Sgordon#	Variables available to methods, and after run_rc_command() has
54498186Sgordon#	completed:
54598186Sgordon#
54698186Sgordon#	Variable	Purpose
54798186Sgordon#	--------	-------
548126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
54998186Sgordon#			performed
55098186Sgordon#
55198186Sgordon#	rc_flags	Flags to start the default command with.
55298186Sgordon#			Defaults to ${name}_flags, unless overridden
55398186Sgordon#			by $flags from the environment.
55498186Sgordon#			This variable may be changed by the precmd method.
55598186Sgordon#
55698186Sgordon#	rc_pid		PID of command (if appropriate)
55798186Sgordon#
55898186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
55998186Sgordon#
56098186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
56198186Sgordon#
562175676Smtm#	rc_quiet	Not empty if "quiet" was provided
56398186Sgordon#
564175676Smtm#
56578344Sobrienrun_rc_command()
56678344Sobrien{
567116097Smtm	_return=0
56898186Sgordon	rc_arg=$1
56978344Sobrien	if [ -z "$name" ]; then
57098186Sgordon		err 3 'run_rc_command: $name is not set.'
57178344Sobrien	fi
57278344Sobrien
573132892Smtm	# Don't repeat the first argument when passing additional command-
574132892Smtm	# line arguments to the command subroutines.
575132892Smtm	#
576132892Smtm	shift 1
577132892Smtm	rc_extra_args="$*"
578132892Smtm
579126303Smtm	_rc_prefix=
58098186Sgordon	case "$rc_arg" in
58178344Sobrien	fast*)				# "fast" prefix; don't check pid
58298186Sgordon		rc_arg=${rc_arg#fast}
58398186Sgordon		rc_fast=yes
584175676Smtm		rc_quiet=yes
58578344Sobrien		;;
586198216Sed	force*)				# "force" prefix; always run
58798186Sgordon		rc_force=yes
588126303Smtm		_rc_prefix=force
589126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
59078344Sobrien		if [ -n "${rcvar}" ]; then
59178344Sobrien			eval ${rcvar}=YES
59278344Sobrien		fi
59378344Sobrien		;;
594126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
595126303Smtm		_rc_prefix=one
596126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
597126303Smtm		if [ -n "${rcvar}" ]; then
598126303Smtm			eval ${rcvar}=YES
599126303Smtm		fi
600126303Smtm		;;
601175676Smtm	quiet*)				# "quiet" prefix; omit some messages
602175676Smtm		_rc_prefix=quiet
603175676Smtm		rc_arg=${rc_arg#${_rc_prefix}}
604175676Smtm		rc_quiet=yes
605175676Smtm		;;
60678344Sobrien	esac
60778344Sobrien
608161530Sflz	eval _override_command=\$${name}_program
609198162Sdougb	command=${_override_command:-$command}
610161530Sflz
61178344Sobrien	_keywords="start stop restart rcvar $extra_commands"
61298186Sgordon	rc_pid=
61378344Sobrien	_pidcmd=
61498186Sgordon	_procname=${procname:-${command}}
61598186Sgordon
616131135Smtm					# setup pid check command
617131135Smtm	if [ -n "$_procname" ]; then
61878344Sobrien		if [ -n "$pidfile" ]; then
61998186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
62098186Sgordon		else
62198186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
62278344Sobrien		fi
62378344Sobrien		if [ -n "$_pidcmd" ]; then
62498186Sgordon			_keywords="${_keywords} status poll"
62578344Sobrien		fi
62678344Sobrien	fi
62778344Sobrien
62898186Sgordon	if [ -z "$rc_arg" ]; then
629150796Syar		rc_usage $_keywords
63078344Sobrien	fi
63178344Sobrien
63278344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
63398186Sgordon		rc_flags=$flags
63478344Sobrien	else
63598186Sgordon		eval rc_flags=\$${name}_flags
63678344Sobrien	fi
63798186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
63898186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
63998186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
64078344Sobrien
64198186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
642124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
64398186Sgordon			unset _user
64498186Sgordon		fi
64598186Sgordon	fi
64698186Sgordon
647179870Smtm	eval $_pidcmd			# determine the pid if necessary
648179870Smtm
649179870Smtm	for _elem in $_keywords; do
650179870Smtm		if [ "$_elem" != "$rc_arg" ]; then
651179870Smtm			continue
652179870Smtm		fi
65378344Sobrien					# if ${rcvar} is set, and $1 is not
65498186Sgordon					# "rcvar", then run
65578344Sobrien					#	checkyesno ${rcvar}
65678344Sobrien					# and return if that failed
65778344Sobrien					#
658179870Smtm		if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
659179870Smtm			if ! checkyesno ${rcvar}; then
660179870Smtm				if [ -n "${rc_quiet}" ]; then
661179870Smtm					return 0
662179870Smtm				fi
663179870Smtm				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
664179870Smtm				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
665179870Smtm				echo "instead of '${rc_arg}'."
666175676Smtm				return 0
667175676Smtm			fi
66878344Sobrien		fi
66978344Sobrien
67078344Sobrien					# if there's a custom ${XXX_cmd},
67178344Sobrien					# run that instead of the default
67278344Sobrien					#
673165565Syar		eval _cmd=\$${rc_arg}_cmd \
674165565Syar		     _precmd=\$${rc_arg}_precmd \
675165565Syar		     _postcmd=\$${rc_arg}_postcmd
676165565Syar
67778344Sobrien		if [ -n "$_cmd" ]; then
678165565Syar			_run_rc_precmd || return 1
679165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
680165565Syar			_run_rc_postcmd
681116097Smtm			return $_return
68278344Sobrien		fi
68378344Sobrien
68498186Sgordon		case "$rc_arg" in	# default operations...
68578344Sobrien
68678344Sobrien		status)
687165565Syar			_run_rc_precmd || return 1
68898186Sgordon			if [ -n "$rc_pid" ]; then
68998186Sgordon				echo "${name} is running as pid $rc_pid."
69078344Sobrien			else
69178344Sobrien				echo "${name} is not running."
69278344Sobrien				return 1
69378344Sobrien			fi
694165565Syar			_run_rc_postcmd
69578344Sobrien			;;
69678344Sobrien
69778344Sobrien		start)
698131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
699157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
700153152Syar				return 1
70178344Sobrien			fi
70278344Sobrien
703167413Syar			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
704160667Syar				warn "run_rc_command: cannot run $command"
705153152Syar				return 1
70678344Sobrien			fi
70778344Sobrien
708179946Smtm			if ! _run_rc_precmd; then
709179946Smtm				warn "failed precmd routine for ${name}"
710179946Smtm				return 1
711179946Smtm			fi
71278344Sobrien
713160668Syar					# setup the full command to run
71478344Sobrien					#
715197947Sdougb			check_startmsgs && echo "Starting ${name}."
71678344Sobrien			if [ -n "$_chroot" ]; then
71778344Sobrien				_doit="\
71878344Sobrien${_nice:+nice -n $_nice }\
71978344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
72098186Sgordon$_chroot $command $rc_flags $command_args"
72178344Sobrien			else
72278344Sobrien				_doit="\
723161396Syar${_chdir:+cd $_chdir && }\
72498186Sgordon$command $rc_flags $command_args"
72598186Sgordon				if [ -n "$_user" ]; then
72698186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
72798186Sgordon				fi
728161396Syar				if [ -n "$_nice" ]; then
729161396Syar					if [ -z "$_user" ]; then
730161396Syar						_doit="sh -c \"$_doit\""
731201036Sdougb					fi
732161396Syar					_doit="nice -n $_nice $_doit"
733161396Syar				fi
73478344Sobrien			fi
73598186Sgordon
736165565Syar					# run the full command
73798186Sgordon					#
738179946Smtm			if ! _run_rc_doit "$_doit"; then
739179946Smtm				warn "failed to start ${name}"
740179946Smtm				return 1
741179946Smtm			fi
74298186Sgordon
74398186Sgordon					# finally, run postcmd
74498186Sgordon					#
745165565Syar			_run_rc_postcmd
74678344Sobrien			;;
74778344Sobrien
74878344Sobrien		stop)
74998186Sgordon			if [ -z "$rc_pid" ]; then
750153152Syar				[ -n "$rc_fast" ] && return 0
751165565Syar				_run_rc_notrunning
752153152Syar				return 1
75378344Sobrien			fi
75478344Sobrien
755165565Syar			_run_rc_precmd || return 1
75698186Sgordon
75798186Sgordon					# send the signal to stop
75898186Sgordon					#
75978344Sobrien			echo "Stopping ${name}."
760165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
761165565Syar			_run_rc_doit "$_doit" || return 1
76298186Sgordon
76398186Sgordon					# wait for the command to exit,
76498186Sgordon					# and run postcmd.
76598186Sgordon			wait_for_pids $rc_pid
766165565Syar
767165565Syar			_run_rc_postcmd
76878344Sobrien			;;
76978344Sobrien
77078344Sobrien		reload)
77198186Sgordon			if [ -z "$rc_pid" ]; then
772165565Syar				_run_rc_notrunning
773153152Syar				return 1
77478344Sobrien			fi
775165565Syar
776165565Syar			_run_rc_precmd || return 1
777165565Syar
778165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
779165565Syar			_run_rc_doit "$_doit" || return 1
780165565Syar
781165565Syar			_run_rc_postcmd
78278344Sobrien			;;
78378344Sobrien
78478344Sobrien		restart)
78578344Sobrien					# prevent restart being called more
78678344Sobrien					# than once by any given script
78778344Sobrien					#
788126285Smtm			if ${_rc_restart_done:-false}; then
78978344Sobrien				return 0
79078344Sobrien			fi
791126285Smtm			_rc_restart_done=true
79278344Sobrien
793165565Syar			_run_rc_precmd || return 1
794165565Syar
795165565Syar			# run those in a subshell to keep global variables
796152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
797165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
798165565Syar			_return=$?
799165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
80098186Sgordon
801165565Syar			_run_rc_postcmd
80278344Sobrien			;;
80378344Sobrien
80498186Sgordon		poll)
805165565Syar			_run_rc_precmd || return 1
80698186Sgordon			if [ -n "$rc_pid" ]; then
80798186Sgordon				wait_for_pids $rc_pid
80898186Sgordon			fi
809165565Syar			_run_rc_postcmd
81098186Sgordon			;;
81198186Sgordon
81278344Sobrien		rcvar)
813197144Shrs			echo -n "# $name"
814197144Shrs			if [ -n "$desc" ]; then
815197144Shrs				echo " : $desc"
816197144Shrs			else
817197144Shrs				echo ""
818197144Shrs			fi
819197144Shrs			echo "#"
820197144Shrs			# Get unique vars in $rcvar $rcvars
821197144Shrs			for _v in $rcvar $rcvars; do
822197144Shrs				case $v in
823197144Shrs				$_v\ *|\ *$_v|*\ $_v\ *) ;;
824197144Shrs				*)	v="${v# } $_v" ;;
825197144Shrs				esac
826197144Shrs			done
827197144Shrs
828197144Shrs			# Display variables.
829197144Shrs			for _v in $v; do
830197144Shrs				if [ -z "$_v" ]; then
831197144Shrs					continue
83278344Sobrien				fi
833197144Shrs
834197144Shrs				eval _desc=\$${_v}_desc
835197144Shrs				eval _defval=\$${_v}_defval
836197144Shrs				_h="-"
837197144Shrs
838197144Shrs				eval echo \"$_v=\\\"\$$_v\\\"\"
839197144Shrs				# decode multiple lines of _desc
840197144Shrs				while [ -n "$_desc" ]; do
841197144Shrs					case $_desc in
842197144Shrs					*^^*)
843197144Shrs						echo "# $_h ${_desc%%^^*}"
844197144Shrs						_desc=${_desc#*^^}
845197144Shrs						_h=" "
846197144Shrs						;;
847197144Shrs					*)
848197144Shrs						echo "# $_h ${_desc}"
849197144Shrs						break
850197144Shrs						;;
851197144Shrs					esac
852197144Shrs				done
853197144Shrs				echo "#   (default: \"$_defval\")"
854197144Shrs			done
855197144Shrs			echo ""
85678344Sobrien			;;
85778344Sobrien
85878344Sobrien		*)
859150796Syar			rc_usage $_keywords
86078344Sobrien			;;
86178344Sobrien
86278344Sobrien		esac
863116097Smtm		return $_return
86478344Sobrien	done
86578344Sobrien
86698186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
867150796Syar	rc_usage $_keywords
868153152Syar	# not reached
86978344Sobrien}
87078344Sobrien
87178344Sobrien#
872165565Syar# Helper functions for run_rc_command: common code.
873165565Syar# They use such global variables besides the exported rc_* ones:
874165565Syar#
875165565Syar#	name	       R/W
876165565Syar#	------------------
877165565Syar#	_precmd		R
878165565Syar#	_postcmd	R
879165565Syar#	_return		W
880165565Syar#
881165565Syar_run_rc_precmd()
882165565Syar{
883165565Syar	check_required_before "$rc_arg" || return 1
884165565Syar
885165565Syar	if [ -n "$_precmd" ]; then
886165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
887165565Syar		eval "$_precmd $rc_extra_args"
888165565Syar		_return=$?
889165565Syar
890165565Syar		# If precmd failed and force isn't set, request exit.
891165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
892165565Syar			return 1
893165565Syar		fi
894165565Syar	fi
895165565Syar
896165565Syar	check_required_after "$rc_arg" || return 1
897165565Syar
898165565Syar	return 0
899165565Syar}
900165565Syar
901165565Syar_run_rc_postcmd()
902165565Syar{
903165565Syar	if [ -n "$_postcmd" ]; then
904165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
905165565Syar		eval "$_postcmd $rc_extra_args"
906165565Syar		_return=$?
907165565Syar	fi
908165565Syar	return 0
909165565Syar}
910165565Syar
911165565Syar_run_rc_doit()
912165565Syar{
913165565Syar	debug "run_rc_command: doit: $*"
914165565Syar	eval "$@"
915165565Syar	_return=$?
916165565Syar
917165565Syar	# If command failed and force isn't set, request exit.
918165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
919165565Syar		return 1
920165565Syar	fi
921165565Syar
922165565Syar	return 0
923165565Syar}
924165565Syar
925165565Syar_run_rc_notrunning()
926165565Syar{
927165565Syar	local _pidmsg
928165565Syar
929165565Syar	if [ -n "$pidfile" ]; then
930165565Syar		_pidmsg=" (check $pidfile)."
931165565Syar	else
932165565Syar		_pidmsg=
933165565Syar	fi
934165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
935165565Syar}
936165565Syar
937165565Syar_run_rc_killcmd()
938165565Syar{
939165565Syar	local _cmd
940165565Syar
941165565Syar	_cmd="kill -$1 $rc_pid"
942165565Syar	if [ -n "$_user" ]; then
943165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
944165565Syar	fi
945165565Syar	echo "$_cmd"
946165565Syar}
947165565Syar
948165565Syar#
94978344Sobrien# run_rc_script file arg
95078344Sobrien#	Start the script `file' with `arg', and correctly handle the
951201038Sdougb#	return value from the script.
952201038Sdougb#	If `file' ends with `.sh', it's sourced into the current environment
953201038Sdougb#	when $rc_fast_and_loose is set, otherwise it is run as a child process.
954201038Sdougb#	If `file' appears to be a backup or scratch file, ignore it.
955201038Sdougb#	Otherwise if it is executable run as a child process.
95678344Sobrien#
95778344Sobrienrun_rc_script()
95878344Sobrien{
95978344Sobrien	_file=$1
96078344Sobrien	_arg=$2
96178344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
96278344Sobrien		err 3 'USAGE: run_rc_script file arg'
96378344Sobrien	fi
96478344Sobrien
96598186Sgordon	unset	name command command_args command_interpreter \
96698186Sgordon		extra_commands pidfile procname \
967197144Shrs		rcvar rcvars rcvars_obsolete required_dirs required_files \
968197144Shrs		required_vars
96998186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
97098186Sgordon
97178344Sobrien	case "$_file" in
972193118Sdougb	/etc/rc.d/*.sh)			# no longer allowed in the base
973193118Sdougb		warn "Ignoring old-style startup script $_file"
97478344Sobrien		;;
975153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
97698186Sgordon		warn "Ignoring scratch file $_file"
97798186Sgordon		;;
97878344Sobrien	*)				# run in subshell
97998186Sgordon		if [ -x $_file ]; then
98098186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
981146490Sschweikh				set $_arg; . $_file
98298186Sgordon			else
983130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
984130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
985184317Sthompsa				  trap "echo Script $_file running" 29
986146490Sschweikh				  set $_arg; . $_file )
98798186Sgordon			fi
98898186Sgordon		fi
98978344Sobrien		;;
99078344Sobrien	esac
99178344Sobrien}
99278344Sobrien
99378344Sobrien#
994157653Sflz# load_rc_config name
995157653Sflz#	Source in the configuration file for a given name.
99678344Sobrien#
99778344Sobrienload_rc_config()
99878344Sobrien{
999197144Shrs	local _name _var _defval _v _msg _new
1000157653Sflz	_name=$1
1001157653Sflz	if [ -z "$_name" ]; then
1002157653Sflz		err 3 'USAGE: load_rc_config name'
100378344Sobrien	fi
100478344Sobrien
1005126285Smtm	if ${_rc_conf_loaded:-false}; then
1006126285Smtm		:
1007126285Smtm	else
100898186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
100998186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
101098186Sgordon			. /etc/defaults/rc.conf
101198186Sgordon			source_rc_confs
101298186Sgordon		elif [ -r /etc/rc.conf ]; then
101398186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
101498186Sgordon			. /etc/rc.conf
101598186Sgordon		fi
1016126285Smtm		_rc_conf_loaded=true
101798186Sgordon	fi
1018161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
1019157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
1020161530Sflz		. /etc/rc.conf.d/"$_name"
1021157653Sflz	fi
1022179872Smtm
1023179872Smtm	# Old variable names support
1024179872Smtm	#
1025179872Smtm	[ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1026197144Shrs
1027197144Shrs	# Set defaults if defined.
1028197144Shrs	for _var in $rcvar $rcvars; do
1029197144Shrs		_defval=`eval echo "\\\$${_var}_defval"`
1030197144Shrs		if [ -n "$_defval" ]; then
1031197144Shrs			eval : \${$_var:=\$${_var}_defval}
1032197144Shrs		fi
1033197144Shrs	done
1034197144Shrs
1035197144Shrs	# check obsolete rc.conf variables
1036197144Shrs	for _var in $rcvars_obsolete; do
1037197144Shrs		_v=`eval echo \\$$_var`
1038197144Shrs		_msg=`eval echo \\$${_var}_obsolete_msg`
1039197144Shrs		_new=`eval echo \\$${_var}_newvar`
1040197144Shrs		case $_v in
1041197144Shrs		"")
1042197144Shrs			;;
1043197144Shrs		*)
1044197144Shrs			if [ -z "$_new" ]; then
1045197144Shrs				_msg="Ignored."
1046197144Shrs			else
1047197144Shrs				eval $_new=\"\$$_var\"
1048197144Shrs				if [ -z "$_msg" ]; then
1049197144Shrs					_msg="Use \$$_new instead."
1050197144Shrs				fi
1051197144Shrs			fi
1052197144Shrs			warn "\$$_var is obsolete.  $_msg"
1053197144Shrs			;;
1054197144Shrs		esac
1055197144Shrs	done
105678344Sobrien}
1057201036Sdougb
1058157473Sflz#
1059157653Sflz# load_rc_config_var name var
1060157653Sflz#	Read the rc.conf(5) var for name and set in the
1061157473Sflz#	current shell, using load_rc_config in a subshell to prevent
1062157473Sflz#	unwanted side effects from other variable assignments.
1063157473Sflz#
1064157473Sflzload_rc_config_var()
1065157473Sflz{
1066157473Sflz	if [ $# -ne 2 ]; then
1067157653Sflz		err 3 'USAGE: load_rc_config_var name var'
1068157473Sflz	fi
1069157473Sflz	eval $(eval '(
1070157473Sflz		load_rc_config '$1' >/dev/null;
1071157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1072157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
1073157473Sflz		fi
1074157473Sflz	)' )
1075157473Sflz}
107678344Sobrien
107778344Sobrien#
107878344Sobrien# rc_usage commands
107978344Sobrien#	Print a usage string for $0, with `commands' being a list of
108078344Sobrien#	valid commands.
108178344Sobrien#
108278344Sobrienrc_usage()
108378344Sobrien{
1084126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
108578344Sobrien
108678344Sobrien	_sep=
1087126286Smtm	for _elem; do
108878344Sobrien		echo -n 1>&2 "$_sep$_elem"
108978344Sobrien		_sep="|"
109078344Sobrien	done
109178344Sobrien	echo 1>&2 ")"
109278344Sobrien	exit 1
109378344Sobrien}
109478344Sobrien
109578344Sobrien#
109678344Sobrien# err exitval message
109778344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
109878344Sobrien#
109978344Sobrienerr()
110078344Sobrien{
110178344Sobrien	exitval=$1
110278344Sobrien	shift
110378344Sobrien
1104106643Sgordon	if [ -x /usr/bin/logger ]; then
1105106643Sgordon		logger "$0: ERROR: $*"
1106106643Sgordon	fi
1107106643Sgordon	echo 1>&2 "$0: ERROR: $*"
110878344Sobrien	exit $exitval
110978344Sobrien}
111078344Sobrien
111178344Sobrien#
111278344Sobrien# warn message
111378344Sobrien#	Display message to stderr and log to the syslog.
111478344Sobrien#
111578344Sobrienwarn()
111678344Sobrien{
1117106643Sgordon	if [ -x /usr/bin/logger ]; then
1118106643Sgordon		logger "$0: WARNING: $*"
1119106643Sgordon	fi
1120106643Sgordon	echo 1>&2 "$0: WARNING: $*"
112178344Sobrien}
112298186Sgordon
112398186Sgordon#
112498186Sgordon# info message
112598186Sgordon#	Display informational message to stdout and log to syslog.
112698186Sgordon#
112798186Sgordoninfo()
112898186Sgordon{
1129119170Smtm	case ${rc_info} in
1130119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1131119170Smtm		if [ -x /usr/bin/logger ]; then
1132119170Smtm			logger "$0: INFO: $*"
1133119170Smtm		fi
1134119170Smtm		echo "$0: INFO: $*"
1135119170Smtm		;;
1136119170Smtm	esac
113798186Sgordon}
113898186Sgordon
113998186Sgordon#
114098186Sgordon# debug message
1141106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
114298186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
114398186Sgordon#	function.
114498186Sgordon#
114598186Sgordondebug()
114698186Sgordon{
114798186Sgordon	case ${rc_debug} in
114898186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1149106700Sgordon		if [ -x /usr/bin/logger ]; then
1150162947Syar			logger "$0: DEBUG: $*"
1151106700Sgordon		fi
1152146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
115398186Sgordon		;;
115498186Sgordon	esac
115598186Sgordon}
115698186Sgordon
115798186Sgordon#
115898186Sgordon# backup_file action file cur backup
115998186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
116098186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
116198186Sgordon#
116298186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
116398186Sgordon#	which can be either YES or NO.
116498186Sgordon#
116598186Sgordon#	The `action' keyword can be one of the following:
116698186Sgordon#
116798186Sgordon#	add		`file' is now being backed up (and is possibly
116898186Sgordon#			being reentered into the backups system).  `cur'
116998186Sgordon#			is created and RCS files, if necessary, are
117098186Sgordon#			created as well.
117198186Sgordon#
117298186Sgordon#	update		`file' has changed and needs to be backed up.
117398186Sgordon#			If `cur' exists, it is copied to to `back' or
117498186Sgordon#			checked into RCS (if the repository file is old),
117598186Sgordon#			and then `file' is copied to `cur'.  Another RCS
117698186Sgordon#			check in done here if RCS is being used.
117798186Sgordon#
117898186Sgordon#	remove		`file' is no longer being tracked by the backups
117998186Sgordon#			system.  If RCS is not being used, `cur' is moved
118098186Sgordon#			to `back', otherwise an empty file is checked in,
118198186Sgordon#			and then `cur' is removed.
118298186Sgordon#
118398186Sgordon#
118498186Sgordonbackup_file()
118598186Sgordon{
118698186Sgordon	_action=$1
118798186Sgordon	_file=$2
118898186Sgordon	_cur=$3
118998186Sgordon	_back=$4
119098186Sgordon
119198186Sgordon	if checkyesno backup_uses_rcs; then
119298186Sgordon		_msg0="backup archive"
119398186Sgordon		_msg1="update"
119498186Sgordon
119598186Sgordon		# ensure that history file is not locked
119698186Sgordon		if [ -f $_cur,v ]; then
119798186Sgordon			rcs -q -u -U -M $_cur
119898186Sgordon		fi
119998186Sgordon
120098186Sgordon		# ensure after switching to rcs that the
120198186Sgordon		# current backup is not lost
120298186Sgordon		if [ -f $_cur ]; then
120398186Sgordon			# no archive, or current newer than archive
120498186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
120598186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
120698186Sgordon				rcs -q -kb -U $_cur
120798186Sgordon				co -q -f -u $_cur
120898186Sgordon			fi
120998186Sgordon		fi
121098186Sgordon
121198186Sgordon		case $_action in
121298186Sgordon		add|update)
121398186Sgordon			cp -p $_file $_cur
121498186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
121598186Sgordon			rcs -q -kb -U $_cur
121698186Sgordon			co -q -f -u $_cur
121798186Sgordon			chown root:wheel $_cur $_cur,v
121898186Sgordon			;;
121998186Sgordon		remove)
122098186Sgordon			cp /dev/null $_cur
122198186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
122298186Sgordon			rcs -q -kb -U $_cur
122398186Sgordon			chown root:wheel $_cur $_cur,v
122498186Sgordon			rm $_cur
122598186Sgordon			;;
122698186Sgordon		esac
122798186Sgordon	else
122898186Sgordon		case $_action in
122998186Sgordon		add|update)
123098186Sgordon			if [ -f $_cur ]; then
123198186Sgordon				cp -p $_cur $_back
123298186Sgordon			fi
123398186Sgordon			cp -p $_file $_cur
123498186Sgordon			chown root:wheel $_cur
123598186Sgordon			;;
123698186Sgordon		remove)
123798186Sgordon			mv -f $_cur $_back
123898186Sgordon			;;
123998186Sgordon		esac
124098186Sgordon	fi
124198186Sgordon}
1242119166Smtm
1243123344Smtm# make_symlink src link
1244123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1245123344Smtm#	directory in which link is to be created does not exist
1246123344Smtm#	a warning will be displayed and an error will be returned.
1247123344Smtm#	Returns 0 on sucess, 1 otherwise.
1248119166Smtm#
1249123344Smtmmake_symlink()
1250119166Smtm{
1251123344Smtm	local src link linkdir _me
1252123344Smtm	src="$1"
1253123344Smtm	link="$2"
1254123344Smtm	linkdir="`dirname $link`"
1255123344Smtm	_me="make_symlink()"
1256119166Smtm
1257123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1258123344Smtm		warn "$_me: requires two arguments."
1259119166Smtm		return 1
1260119166Smtm	fi
1261123344Smtm	if [ ! -d "$linkdir" ]; then
1262160667Syar		warn "$_me: the directory $linkdir does not exist."
1263119166Smtm		return 1
1264119166Smtm	fi
1265146490Sschweikh	if ! ln -sf $src $link; then
1266123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1267119166Smtm		return 1
1268119166Smtm	fi
1269119166Smtm	return 0
1270119166Smtm}
1271119166Smtm
1272119166Smtm# devfs_rulesets_from_file file
1273119166Smtm#	Reads a set of devfs commands from file, and creates
1274119166Smtm#	the specified rulesets with their rules. Returns non-zero
1275119166Smtm#	if there was an error.
1276119166Smtm#
1277119166Smtmdevfs_rulesets_from_file()
1278119166Smtm{
1279119166Smtm	local file _err _me
1280119166Smtm	file="$1"
1281119166Smtm	_me="devfs_rulesets_from_file"
1282119166Smtm	_err=0
1283119166Smtm
1284119166Smtm	if [ -z "$file" ]; then
1285119166Smtm		warn "$_me: you must specify a file"
1286119166Smtm		return 1
1287119166Smtm	fi
1288119166Smtm	if [ ! -e "$file" ]; then
1289119166Smtm		debug "$_me: no such file ($file)"
1290119166Smtm		return 0
1291119166Smtm	fi
1292119166Smtm	debug "reading rulesets from file ($file)"
1293119166Smtm	{ while read line
1294119166Smtm	do
1295119166Smtm		case $line in
1296119166Smtm		\#*)
1297119166Smtm			continue
1298119166Smtm			;;
1299119166Smtm		\[*\]*)
1300119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1301119166Smtm			if [ -z "$rulenum" ]; then
1302119166Smtm				warn "$_me: cannot extract rule number ($line)"
1303119166Smtm				_err=1
1304119166Smtm				break
1305119166Smtm			fi
1306119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1307119166Smtm			if [ -z "$rulename" ]; then
1308119166Smtm				warn "$_me: cannot extract rule name ($line)"
1309119166Smtm				_err=1
1310119166Smtm				break;
1311119166Smtm			fi
1312119166Smtm			eval $rulename=\$rulenum
1313119166Smtm			debug "found ruleset: $rulename=$rulenum"
1314146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1315119166Smtm				_err=1
1316119166Smtm				break
1317119166Smtm			fi
1318119166Smtm			;;
1319119166Smtm		*)
1320119166Smtm			rulecmd="${line%%"\#*"}"
1321119166Smtm			# evaluate the command incase it includes
1322119166Smtm			# other rules
1323119166Smtm			if [ -n "$rulecmd" ]; then
1324119166Smtm				debug "adding rule ($rulecmd)"
1325119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1326119166Smtm				then
1327119166Smtm					_err=1
1328119166Smtm					break
1329119166Smtm				fi
1330119166Smtm			fi
1331119166Smtm			;;
1332119166Smtm		esac
1333119166Smtm		if [ $_err -ne 0 ]; then
1334119166Smtm			debug "error in $_me"
1335119166Smtm			break
1336119166Smtm		fi
1337119166Smtm	done } < $file
1338119166Smtm	return $_err
1339119166Smtm}
1340119166Smtm
1341119166Smtm# devfs_init_rulesets
1342119166Smtm#	Initializes rulesets from configuration files. Returns
1343119166Smtm#	non-zero if there was an error.
1344119166Smtm#
1345119166Smtmdevfs_init_rulesets()
1346119166Smtm{
1347119166Smtm	local file _me
1348119166Smtm	_me="devfs_init_rulesets"
1349119166Smtm
1350119166Smtm	# Go through this only once
1351119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1352119166Smtm		debug "$_me: devfs rulesets already initialized"
1353119166Smtm		return
1354119166Smtm	fi
1355146490Sschweikh	for file in $devfs_rulesets; do
1356119166Smtm		devfs_rulesets_from_file $file || return 1
1357119166Smtm	done
1358119166Smtm	devfs_rulesets_init=1
1359119166Smtm	debug "$_me: devfs rulesets initialized"
1360119166Smtm	return 0
1361119166Smtm}
1362119166Smtm
1363119166Smtm# devfs_set_ruleset ruleset [dir]
1364151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1365119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1366119166Smtm#	Returns non-zero if it could not set it successfully.
1367119166Smtm#
1368119166Smtmdevfs_set_ruleset()
1369119166Smtm{
1370119166Smtm	local devdir rs _me
1371119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1372119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1373119166Smtm	_me="devfs_set_ruleset"
1374119166Smtm
1375119166Smtm	if [ -z "$rs" ]; then
1376119166Smtm		warn "$_me: you must specify a ruleset number"
1377119166Smtm		return 1
1378119166Smtm	fi
1379119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1380146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1381119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1382119166Smtm		return 1
1383119166Smtm	fi
1384119166Smtm	return 0
1385119166Smtm}
1386119166Smtm
1387119166Smtm# devfs_apply_ruleset ruleset [dir]
1388119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1389119166Smtm#	The ruleset argument must be a ruleset name as specified
1390119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1391119166Smtm#	if it could not apply the ruleset.
1392119166Smtm#
1393119166Smtmdevfs_apply_ruleset()
1394119166Smtm{
1395119166Smtm	local devdir rs _me
1396119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1397119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1398119166Smtm	_me="devfs_apply_ruleset"
1399119166Smtm
1400119166Smtm	if [ -z "$rs" ]; then
1401119166Smtm		warn "$_me: you must specify a ruleset"
1402119166Smtm		return 1
1403119166Smtm	fi
1404119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1405146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1406119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1407119166Smtm		return 1
1408119166Smtm	fi
1409119166Smtm	return 0
1410119166Smtm}
1411119166Smtm
1412119166Smtm# devfs_domount dir [ruleset]
1413119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1414119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1415119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1416119166Smtm#
1417119166Smtmdevfs_domount()
1418119166Smtm{
1419119166Smtm	local devdir rs _me
1420119166Smtm	devdir="$1"
1421119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1422119166Smtm	_me="devfs_domount()"
1423119166Smtm
1424119166Smtm	if [ -z "$devdir" ]; then
1425119166Smtm		warn "$_me: you must specify a mount-point"
1426119166Smtm		return 1
1427119166Smtm	fi
1428119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1429146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1430119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1431119166Smtm		return 1
1432119166Smtm	fi
1433119166Smtm	if [ -n "$rs" ]; then
1434119166Smtm		devfs_init_rulesets
1435119166Smtm		devfs_set_ruleset $rs $devdir
1436124797Scperciva		devfs -m $devdir rule applyset
1437119166Smtm	fi
1438119166Smtm	return 0
1439119166Smtm}
1440119166Smtm
1441119166Smtm# devfs_mount_jail dir [ruleset]
1442119166Smtm#	Mounts a devfs file system appropriate for jails
1443119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1444119166Smtm#	it names will be used instead.  If present, ruleset must
1445119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1446119166Smtm#	This function returns non-zero if an error occurs.
1447119166Smtm#
1448119166Smtmdevfs_mount_jail()
1449119166Smtm{
1450119166Smtm	local jdev rs _me
1451119166Smtm	jdev="$1"
1452119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1453119166Smtm	_me="devfs_mount_jail"
1454119166Smtm
1455119166Smtm	devfs_init_rulesets
1456146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1457119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1458119166Smtm		return 1
1459119166Smtm	fi
1460119166Smtm	return 0
1461119166Smtm}
1462127345Sbrooks
1463127345Sbrooks# Provide a function for normalizing the mounting of memory
1464127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1465127345Sbrooks# as close as possible between 5-current and 4-stable.
1466127345Sbrooks#   $1 = size
1467127345Sbrooks#   $2 = mount point
1468137451Skeramida#   $3 = (optional) extra mdmfs flags
1469146490Sschweikhmount_md()
1470146490Sschweikh{
1471127345Sbrooks	if [ -n "$3" ]; then
1472137451Skeramida		flags="$3"
1473127345Sbrooks	fi
1474149421Syar	/sbin/mdmfs $flags -s $1 md $2
1475127345Sbrooks}
1476131550Scperciva
1477159828Syar# Code common to scripts that need to load a kernel module
1478159828Syar# if it isn't in the kernel yet. Syntax:
1479160666Syar#   load_kld [-e regex] [-m module] file
1480159828Syar# where -e or -m chooses the way to check if the module
1481159828Syar# is already loaded:
1482160666Syar#   regex is egrep'd in the output from `kldstat -v',
1483160666Syar#   module is passed to `kldstat -m'.
1484160666Syar# The default way is as though `-m file' were specified.
1485159828Syarload_kld()
1486159828Syar{
1487159828Syar	local _loaded _mod _opt _re
1488159828Syar
1489159828Syar	while getopts "e:m:" _opt; do
1490159828Syar		case "$_opt" in
1491159828Syar		e) _re="$OPTARG" ;;
1492159828Syar		m) _mod="$OPTARG" ;;
1493160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1494159828Syar		esac
1495159828Syar	done
1496159828Syar	shift $(($OPTIND - 1))
1497160666Syar	if [ $# -ne 1 ]; then
1498160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1499160666Syar	fi
1500159828Syar	_mod=${_mod:-$1}
1501159828Syar	_loaded=false
1502159828Syar	if [ -n "$_re" ]; then
1503159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1504159828Syar			_loaded=true
1505159828Syar		fi
1506159828Syar	else
1507159828Syar		if kldstat -q -m "$_mod"; then
1508159828Syar			_loaded=true
1509159828Syar		fi
1510159828Syar	fi
1511159828Syar	if ! $_loaded; then
1512159828Syar		if ! kldload "$1"; then
1513159828Syar			warn "Unable to load kernel module $1"
1514159828Syar			return 1
1515160666Syar		else
1516160666Syar			info "$1 kernel module loaded."
1517159828Syar		fi
1518160666Syar	else
1519160666Syar		debug "load_kld: $1 kernel module already loaded."
1520159828Syar	fi
1521159828Syar	return 0
1522159828Syar}
1523159828Syar
1524149049Spjd# ltr str src dst
1525149049Spjd#	Change every $src in $str to $dst.
1526149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1527149049Spjd#	awk(1).
1528149049Spjdltr()
1529149049Spjd{
1530149049Spjd	local _str _src _dst _out _com
1531149049Spjd	_str=$1
1532149049Spjd	_src=$2
1533149049Spjd	_dst=$3
1534149049Spjd	_out=""
1535149049Spjd
1536149049Spjd	IFS=${_src}
1537149049Spjd	for _com in ${_str}; do
1538149049Spjd		if [ -z "${_out}" ]; then
1539149049Spjd			_out="${_com}"
1540149049Spjd		else
1541149049Spjd			_out="${_out}${_dst}${_com}"
1542149049Spjd		fi
1543149049Spjd	done
1544149049Spjd	echo "${_out}"
1545149049Spjd}
1546149049Spjd
1547149050Spjd# Creates a list of providers for GELI encryption.
1548149050Spjdgeli_make_list()
1549149050Spjd{
1550149050Spjd	local devices devices2
1551149050Spjd	local provider mountpoint type options rest
1552149050Spjd
1553149050Spjd	# Create list of GELI providers from fstab.
1554149050Spjd	while read provider mountpoint type options rest ; do
1555155570Sflz		case ":${options}" in
1556155570Sflz		:*noauto*)
1557155570Sflz			noauto=yes
1558155570Sflz			;;
1559155570Sflz		*)
1560155570Sflz			noauto=no
1561155570Sflz			;;
1562155570Sflz		esac
1563155570Sflz
1564149050Spjd		case ":${provider}" in
1565149050Spjd		:#*)
1566149050Spjd			continue
1567149050Spjd			;;
1568149050Spjd		*.eli)
1569149050Spjd			# Skip swap devices.
1570155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1571149050Spjd				continue
1572149050Spjd			fi
1573149050Spjd			devices="${devices} ${provider}"
1574149050Spjd			;;
1575149050Spjd		esac
1576149050Spjd	done < /etc/fstab
1577149050Spjd
1578149050Spjd	# Append providers from geli_devices.
1579149050Spjd	devices="${devices} ${geli_devices}"
1580149050Spjd
1581149050Spjd	for provider in ${devices}; do
1582149050Spjd		provider=${provider%.eli}
1583149050Spjd		provider=${provider#/dev/}
1584149050Spjd		devices2="${devices2} ${provider}"
1585149050Spjd	done
1586149050Spjd
1587149050Spjd	echo ${devices2}
1588149050Spjd}
1589149050Spjd
1590153027Sdougb# Find scripts in local_startup directories that use the old syntax
1591153027Sdougb#
1592153027Sdougbfind_local_scripts_old () {
1593153027Sdougb	zlist=''
1594153027Sdougb	slist=''
1595153027Sdougb	for dir in ${local_startup}; do
1596153027Sdougb		if [ -d "${dir}" ]; then
1597153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1598153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1599153027Sdougb				    continue
1600153027Sdougb				zlist="$zlist $file"
1601153027Sdougb			done
1602153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1603153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1604153027Sdougb				    continue
1605153027Sdougb				slist="$slist $file"
1606153027Sdougb			done
1607153027Sdougb		fi
1608153027Sdougb	done
1609153027Sdougb}
1610153027Sdougb
1611153027Sdougbfind_local_scripts_new () {
1612153027Sdougb	local_rc=''
1613153027Sdougb	for dir in ${local_startup}; do
1614153027Sdougb		if [ -d "${dir}" ]; then
1615153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1616153027Sdougb				case "$file" in
1617153027Sdougb				*.sample) ;;
1618153027Sdougb				*)	if [ -x "$file" ]; then
1619153027Sdougb						local_rc="${local_rc} ${file}"
1620153027Sdougb					fi
1621153027Sdougb					;;
1622153027Sdougb				esac
1623153027Sdougb			done
1624153027Sdougb		fi
1625153027Sdougb	done
1626153027Sdougb}
1627153027Sdougb
1628165565Syar# check_required_{before|after} command
1629165565Syar#	Check for things required by the command before and after its precmd,
1630165565Syar#	respectively.  The two separate functions are needed because some
1631165565Syar#	conditions should prevent precmd from being run while other things
1632165565Syar#	depend on precmd having already been run.
1633165565Syar#
1634165565Syarcheck_required_before()
1635165565Syar{
1636165565Syar	local _f
1637165565Syar
1638165565Syar	case "$1" in
1639165565Syar	start)
1640165565Syar		for _f in $required_vars; do
1641165565Syar			if ! checkyesno $_f; then
1642165565Syar				warn "\$${_f} is not enabled."
1643165565Syar				if [ -z "$rc_force" ]; then
1644165565Syar					return 1
1645165565Syar				fi
1646165565Syar			fi
1647165565Syar		done
1648165565Syar
1649165565Syar		for _f in $required_dirs; do
1650165565Syar			if [ ! -d "${_f}/." ]; then
1651165565Syar				warn "${_f} is not a directory."
1652165565Syar				if [ -z "$rc_force" ]; then
1653165565Syar					return 1
1654165565Syar				fi
1655165565Syar			fi
1656165565Syar		done
1657165565Syar
1658165565Syar		for _f in $required_files; do
1659165565Syar			if [ ! -r "${_f}" ]; then
1660165565Syar				warn "${_f} is not readable."
1661165565Syar				if [ -z "$rc_force" ]; then
1662165565Syar					return 1
1663165565Syar				fi
1664165565Syar			fi
1665165565Syar		done
1666165565Syar		;;
1667165565Syar	esac
1668165565Syar
1669165565Syar	return 0
1670165565Syar}
1671165565Syar
1672165565Syarcheck_required_after()
1673165565Syar{
1674165565Syar	local _f _args
1675165565Syar
1676165565Syar	case "$1" in
1677165565Syar	start)
1678165565Syar		for _f in $required_modules; do
1679165565Syar			case "${_f}" in
1680165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1681165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1682165565Syar				*)	_args="${_f}" ;;
1683165565Syar			esac
1684165565Syar			if ! load_kld ${_args}; then
1685165565Syar				if [ -z "$rc_force" ]; then
1686165565Syar					return 1
1687165565Syar				fi
1688165565Syar			fi
1689165565Syar		done
1690165565Syar		;;
1691165565Syar	esac
1692165565Syar
1693165565Syar	return 0
1694165565Syar}
1695165565Syar
1696131550Scpercivafi
1697157841Sflz
1698197144Shrs# _echoonce var msg mode
1699197144Shrs#	mode=0: Echo $msg if ${$var} is empty.
1700197144Shrs#	        After doing echo, a string is set to ${$var}.
1701197144Shrs#
1702197144Shrs#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
1703197144Shrs#
1704197144Shrs_echoonce()
1705197144Shrs{
1706197144Shrs	local _var _msg _mode
1707197144Shrs	_var=`eval echo \\$$1`
1708197144Shrs	_msg=$2
1709197144Shrs	_mode=$3
1710197144Shrs
1711197144Shrs	case $_mode in
1712197144Shrs	1)	[ -n "$_var" ] && echo "$_msg" ;;
1713197144Shrs	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1714197144Shrs	esac
1715197144Shrs}
1716197144Shrs
1717157841Sflz_rc_subr_loaded=:
1718