rc.subr revision 197790
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 197790 2009-10-05 20:11:33Z hrs $
378344Sobrien#
4157473Sflz# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
578344Sobrien# All rights reserved.
678344Sobrien#
778344Sobrien# This code is derived from software contributed to The NetBSD Foundation
878344Sobrien# by Luke Mewburn.
978344Sobrien#
1078344Sobrien# Redistribution and use in source and binary forms, with or without
1178344Sobrien# modification, are permitted provided that the following conditions
1278344Sobrien# are met:
1378344Sobrien# 1. Redistributions of source code must retain the above copyright
1478344Sobrien#    notice, this list of conditions and the following disclaimer.
1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1678344Sobrien#    notice, this list of conditions and the following disclaimer in the
1778344Sobrien#    documentation and/or other materials provided with the distribution.
1878344Sobrien# 3. All advertising materials mentioning features or use of this software
1978344Sobrien#    must display the following acknowledgement:
2078344Sobrien#        This product includes software developed by the NetBSD
2178344Sobrien#        Foundation, Inc. and its contributors.
2278344Sobrien# 4. Neither the name of The NetBSD Foundation nor the names of its
2378344Sobrien#    contributors may be used to endorse or promote products derived
2478344Sobrien#    from this software without specific prior written permission.
2578344Sobrien#
2678344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2778344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2878344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2978344Sobrien# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3078344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3178344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3278344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3378344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3478344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3578344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3678344Sobrien# POSSIBILITY OF SUCH DAMAGE.
3778344Sobrien#
3878344Sobrien# rc.subr
3978344Sobrien#	functions used by various rc scripts
4078344Sobrien#
4178344Sobrien
42157473Sflz: ${rcvar_manpage:='rc.conf(5)'}
43169668Smtm: ${RC_PID:=$$}; export RC_PID
44157473Sflz
4578344Sobrien#
4698186Sgordon#	Operating System dependent/independent variables
4798186Sgordon#
4898186Sgordon
49131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then
50131550Scperciva
51131550Scperciva_rc_subr_loaded="YES"
52131550Scperciva
5398186SgordonSYSCTL="/sbin/sysctl"
5498186SgordonSYSCTL_N="${SYSCTL} -n"
5598186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype"
56103018SgordonOSTYPE=`${CMD_OSTYPE}`
57124832SmtmID="/usr/bin/id"
58124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
59161435SyarPS="/bin/ps -ww"
60161435SyarJID=`$PS -p $$ -o jid=`
6198186Sgordon
62103018Sgordoncase ${OSTYPE} in
6398186SgordonFreeBSD)
6498186Sgordon	SYSCTL_W="${SYSCTL}"
6598186Sgordon	;;
6698186SgordonNetBSD)
6798186Sgordon	SYSCTL_W="${SYSCTL} -w"
6898186Sgordon	;;
6998186Sgordonesac
7098186Sgordon
7198186Sgordon#
7278344Sobrien#	functions
7378344Sobrien#	---------
7478344Sobrien
75197144Shrs# set_rcvar [var] [defval] [desc]
7678344Sobrien#
77197144Shrs#	Echo or define a rc.conf(5) variable name.  Global variable
78197144Shrs#	$rcvars is used.
7998186Sgordon#
80197144Shrs#	If no argument is specified, echo "${name}_enable".
81197144Shrs#
82197144Shrs#	If only a var is specified, echo "${var}_enable".
83197144Shrs#
84197144Shrs#	If var and defval are specified, the ${var} is defined as
85197144Shrs#	rc.conf(5) variable and the default value is ${defvar}.  An
86197144Shrs#	optional argument $desc can also be specified to add a
87197144Shrs#	description for that.
88197144Shrs#
8998186Sgordonset_rcvar()
9098186Sgordon{
91197144Shrs	case $# in
92197144Shrs	0)
93197144Shrs		echo ${name}_enable
9498186Sgordon		;;
95197144Shrs	1)
96197144Shrs		echo ${1}_enable
9798186Sgordon		;;
9898186Sgordon	*)
99197144Shrs		debug "rcvar_define: \$$1=$2 is added" \
100197144Shrs		    " as a rc.conf(5) variable."
101197144Shrs
102197144Shrs		local _var
103197144Shrs		_var=$1
104197144Shrs		rcvars="${rcvars# } $_var"
105197144Shrs		eval ${_var}_defval=\"$2\"
106197144Shrs		shift 2
107197144Shrs		# encode multiple lines of _desc
108197144Shrs		for l in "$@"; do
109197144Shrs			eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
110197144Shrs		done
111197144Shrs		eval ${_var}_desc=\"\${${_var}_desc#^^}\"
11298186Sgordon		;;
11398186Sgordon	esac
11498186Sgordon}
11598186Sgordon
116197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg]
117197144Shrs#	Define obsolete variable.
118197144Shrs#	Global variable $rcvars_obsolete is used.
11998186Sgordon#
120197144Shrsset_rcvar_obsolete()
121197144Shrs{
122197144Shrs	local _var
123197144Shrs	_var=$1
124197144Shrs	debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
125197144Shrs
126197144Shrs	rcvars_obsolete="${rcvars_obsolete# } $1"
127197144Shrs	eval ${1}_newvar=\"$2\"
128197144Shrs	shift 2
129197144Shrs	eval ${_var}_obsolete_msg=\"$*\"
130197144Shrs}
131197144Shrs
132197144Shrs#
13398186Sgordon# force_depend script
13498186Sgordon#	Force a service to start. Intended for use by services
13598186Sgordon#	to resolve dependency issues. It is assumed the caller
13698186Sgordon#	has check to make sure this call is necessary
13798186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
13898186Sgordon#
13998186Sgordonforce_depend()
14098186Sgordon{
14198186Sgordon	_depend="$1"
14298186Sgordon
14398186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
144146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
14598186Sgordon		warn "Unable to force ${_depend}. It may already be running."
14698186Sgordon		return 1
14798186Sgordon	fi
14898186Sgordon	return 0
14998186Sgordon}
15098186Sgordon
15198186Sgordon#
15278344Sobrien# checkyesno var
15378344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
15478344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
15578344Sobrien#
15678344Sobriencheckyesno()
15778344Sobrien{
15878344Sobrien	eval _value=\$${1}
15998186Sgordon	debug "checkyesno: $1 is set to $_value."
16078344Sobrien	case $_value in
16178344Sobrien
16278344Sobrien		#	"yes", "true", "on", or "1"
16378344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
16478344Sobrien		return 0
16578344Sobrien		;;
16678344Sobrien
16778344Sobrien		#	"no", "false", "off", or "0"
16878344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
16978344Sobrien		return 1
17078344Sobrien		;;
17178344Sobrien	*)
172157473Sflz		warn "\$${1} is not set properly - see ${rcvar_manpage}."
17378344Sobrien		return 1
17478344Sobrien		;;
17578344Sobrien	esac
17678344Sobrien}
17778344Sobrien
178157473Sflz#
17998186Sgordon# reverse_list list
18098186Sgordon#	print the list in reverse order
18178344Sobrien#
18298186Sgordonreverse_list()
18398186Sgordon{
18498186Sgordon	_revlist=
185126286Smtm	for _revfile; do
18698186Sgordon		_revlist="$_revfile $_revlist"
18798186Sgordon	done
18898186Sgordon	echo $_revlist
18998186Sgordon}
19098186Sgordon
191169668Smtm# stop_boot always
192169668Smtm#	If booting directly to multiuser or $always is enabled,
193169668Smtm#	send SIGTERM to the parent (/etc/rc) to abort the boot.
194169668Smtm#	Otherwise just exit.
19578344Sobrien#
196169668Smtmstop_boot()
197169668Smtm{
198169668Smtm	local always
199169668Smtm
200178776Smaxim	case $1 in
201178776Smaxim		#	"yes", "true", "on", or "1"
202178770Smtm        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
203169668Smtm		always=true
204178770Smtm		;;
205178770Smtm	*)
206169668Smtm		always=false
207178770Smtm		;;
208178775Smaxim	esac
209169668Smtm	if [ "$autoboot" = yes -o "$always" = true ]; then
210169668Smtm		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
211169668Smtm		kill -TERM ${RC_PID}
212169668Smtm	fi
213169668Smtm	exit 1
214169668Smtm}
215169668Smtm
216169668Smtm#
21798186Sgordon# mount_critical_filesystems type
21898186Sgordon#	Go through the list of critical filesystems as provided in
21998186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
22098186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
22198186Sgordon#
22278344Sobrienmount_critical_filesystems()
22378344Sobrien{
22498186Sgordon	eval _fslist=\$critical_filesystems_${1}
22578344Sobrien	for _fs in $_fslist; do
22678344Sobrien		mount | (
227126285Smtm			_ismounted=false
22878344Sobrien			while read what _on on _type type; do
22978344Sobrien				if [ $on = $_fs ]; then
230126285Smtm					_ismounted=true
23178344Sobrien				fi
23278344Sobrien			done
233126285Smtm			if $_ismounted; then
234126285Smtm				:
235126285Smtm			else
23678344Sobrien				mount $_fs >/dev/null 2>&1
23778344Sobrien			fi
23898186Sgordon		)
23978344Sobrien	done
24078344Sobrien}
24178344Sobrien
24278344Sobrien#
24398186Sgordon# check_pidfile pidfile procname [interpreter]
24498186Sgordon#	Parses the first line of pidfile for a PID, and ensures
24578344Sobrien#	that the process is running and matches procname.
24698186Sgordon#	Prints the matching PID upon success, nothing otherwise.
24798186Sgordon#	interpreter is optional; see _find_processes() for details.
24878344Sobrien#
24978344Sobriencheck_pidfile()
25078344Sobrien{
25178344Sobrien	_pidfile=$1
25278344Sobrien	_procname=$2
25398186Sgordon	_interpreter=$3
25478344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
25598186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
25678344Sobrien	fi
25778344Sobrien	if [ ! -f $_pidfile ]; then
258131061Smtm		debug "pid file ($_pidfile): not readable."
25978344Sobrien		return
26078344Sobrien	fi
26178344Sobrien	read _pid _junk < $_pidfile
26278344Sobrien	if [ -z "$_pid" ]; then
263139949Skeramida		debug "pid file ($_pidfile): no pid in file."
26478344Sobrien		return
26578344Sobrien	fi
26698186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
26778344Sobrien}
26878344Sobrien
26978344Sobrien#
27098186Sgordon# check_process procname [interpreter]
27178344Sobrien#	Ensures that a process (or processes) named procname is running.
27298186Sgordon#	Prints a list of matching PIDs.
27398186Sgordon#	interpreter is optional; see _find_processes() for details.
27478344Sobrien#
27578344Sobriencheck_process()
27678344Sobrien{
27778344Sobrien	_procname=$1
27898186Sgordon	_interpreter=$2
27978344Sobrien	if [ -z "$_procname" ]; then
28098186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
28178344Sobrien	fi
28298186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
28398186Sgordon}
28498186Sgordon
28598186Sgordon#
28698186Sgordon# _find_processes procname interpreter psargs
28798186Sgordon#	Search for procname in the output of ps generated by psargs.
28898186Sgordon#	Prints the PIDs of any matching processes, space separated.
28998186Sgordon#
29098186Sgordon#	If interpreter == ".", check the following variations of procname
29198186Sgordon#	against the first word of each command:
29298186Sgordon#		procname
29398186Sgordon#		`basename procname`
29498186Sgordon#		`basename procname` + ":"
29598186Sgordon#		"(" + `basename procname` + ")"
296155719Sceri#		"[" + `basename procname` + "]"
29798186Sgordon#
29898186Sgordon#	If interpreter != ".", read the first line of procname, remove the
29998186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
30098186Sgordon#	match that against each command, either as is, or with extra words
301157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
302157841Sflz#	using perl, the basename of the interpreter plus a colon is also
303157841Sflz#	tried as the prefix to procname.
30498186Sgordon#
30598186Sgordon_find_processes()
30698186Sgordon{
30798186Sgordon	if [ $# -ne 3 ]; then
30898186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
30998186Sgordon	fi
31098186Sgordon	_procname=$1
31198186Sgordon	_interpreter=$2
31298186Sgordon	_psargs=$3
31398186Sgordon
31478344Sobrien	_pref=
31598186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
316170282Syar		_script=${_chroot}${_chroot:+"/"}$_procname
317170282Syar		if [ -r $_script ]; then
318170282Syar			read _interp < $_script	# read interpreter name
319170282Syar			case "$_interp" in
320170282Syar			\#!*)
321170282Syar				_interp=${_interp#\#!}	# strip #!
322170282Syar				set -- $_interp
323170282Syar				case $1 in
324170282Syar				*/bin/env)
325170282Syar					shift	# drop env to get real name
326170282Syar					;;
327170282Syar				esac
328170282Syar				if [ $_interpreter != $1 ]; then
329170282Syar					warn "\$command_interpreter $_interpreter != $1"
330170282Syar				fi
331170282Syar				;;
332170282Syar			*)
333170282Syar				warn "no shebang line in $_script"
334170282Syar				set -- $_interpreter
335170282Syar				;;
336170282Syar			esac
337170282Syar		else
338170282Syar			warn "cannot read shebang line from $_script"
339170282Syar			set -- $_interpreter
34078344Sobrien		fi
34198186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
342157841Sflz		_interpbn=${1##*/}
34398186Sgordon		_fp_args='_argv'
34498186Sgordon		_fp_match='case "$_argv" in
345157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
34698186Sgordon	else					# a normal daemon
34798186Sgordon		_procnamebn=${_procname##*/}
34898186Sgordon		_fp_args='_arg0 _argv'
34998186Sgordon		_fp_match='case "$_arg0" in
350151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
35198186Sgordon	fi
35298186Sgordon
353161435Syar	_proccheck="\
354161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
355157657Sflz		while read _npid _jid '"$_fp_args"'; do
356161436Syar			'"$_fp_match"'
357157657Sflz				if [ "$JID" -eq "$_jid" ];
358157657Sflz				then echo -n "$_pref$_npid";
359157657Sflz				_pref=" ";
360157657Sflz				fi
36198186Sgordon				;;
36298186Sgordon			esac
36398186Sgordon		done'
36498186Sgordon
365114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
36698186Sgordon	eval $_proccheck
36798186Sgordon}
36898186Sgordon
36998186Sgordon#
37098186Sgordon# wait_for_pids pid [pid ...]
37198186Sgordon#	spins until none of the pids exist
37298186Sgordon#
37398186Sgordonwait_for_pids()
37498186Sgordon{
375126286Smtm	_list="$@"
37698186Sgordon	if [ -z "$_list" ]; then
37798186Sgordon		return
37898186Sgordon	fi
37998186Sgordon	_prefix=
38098186Sgordon	while true; do
38198186Sgordon		_nlist="";
38298186Sgordon		for _j in $_list; do
38398186Sgordon			if kill -0 $_j 2>/dev/null; then
38498186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
38598186Sgordon			fi
38698186Sgordon		done
38798186Sgordon		if [ -z "$_nlist" ]; then
38898186Sgordon			break
38978344Sobrien		fi
39098186Sgordon		_list=$_nlist
39198186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
39298186Sgordon		_prefix=", "
39398186Sgordon		sleep 2
39478344Sobrien	done
39598186Sgordon	if [ -n "$_prefix" ]; then
39698186Sgordon		echo "."
39798186Sgordon	fi
39878344Sobrien}
39978344Sobrien
40078344Sobrien#
40198186Sgordon# run_rc_command argument
40298186Sgordon#	Search for argument in the list of supported commands, which is:
40398186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
40498186Sgordon#	If there's a match, run ${argument}_cmd or the default method
40598186Sgordon#	(see below).
40678344Sobrien#
40798186Sgordon#	If argument has a given prefix, then change the operation as follows:
40898186Sgordon#		Prefix	Operation
40978344Sobrien#		------	---------
410175676Smtm#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
41198186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
412126303Smtm#		one	Set ${rcvar} to YES
413175676Smtm#		quiet	Don't output some diagnostics, and set rc_quiet=yes
41478344Sobrien#
41578344Sobrien#	The following globals are used:
41678344Sobrien#
41798186Sgordon#	Name		Needed	Purpose
41898186Sgordon#	----		------	-------
41978344Sobrien#	name		y	Name of script.
42078344Sobrien#
42178344Sobrien#	command		n	Full path to command.
42298186Sgordon#				Not needed if ${rc_arg}_cmd is set for
42378344Sobrien#				each keyword.
42478344Sobrien#
42578344Sobrien#	command_args	n	Optional args/shell directives for command.
42678344Sobrien#
42798186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
42898186Sgordon#				call check_{pidfile,process}() appropriately.
42998186Sgordon#
430197144Shrs#	desc		n	Description of script.
431197144Shrs#
43278344Sobrien#	extra_commands	n	List of extra commands supported.
43378344Sobrien#
43498186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
43598186Sgordon#				otherwise use check_process $command.
43698186Sgordon#				In either case, only check if $command is set.
43778344Sobrien#
43898186Sgordon#	procname	n	Process name to check for instead of $command.
43998186Sgordon#
44078344Sobrien#	rcvar		n	This is checked with checkyesno to determine
44178344Sobrien#				if the action should be run.
44278344Sobrien#
443157653Sflz#	${name}_program	n	Full path to command.
444157653Sflz#				Meant to be used in /etc/rc.conf to override
445157653Sflz#				${command}.
446157653Sflz#
44778344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
44898186Sgordon#				Requires /usr to be mounted.
44978344Sobrien#
45078344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
45178344Sobrien#				(if not using ${name}_chroot).
45278344Sobrien#
45378344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
45478344Sobrien#				NOTE:	$flags from the parent environment
45578344Sobrien#					can be used to override this.
45678344Sobrien#
45778344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
45878344Sobrien#
45978344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
46078344Sobrien#				using ${name}_chroot.
46198186Sgordon#				Requires /usr to be mounted.
46278344Sobrien#
46378344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
46498186Sgordon#				Requires /usr to be mounted.
46578344Sobrien#
46698186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
46798186Sgordon#				to run the chrooted ${command} with.
46898186Sgordon#				Requires /usr to be mounted.
46978344Sobrien#
47098186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
47178344Sobrien#				Otherwise, use default command (see below)
47278344Sobrien#
47398186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
47498186Sgordon#				${rc_arg}_cmd method in the default
47598186Sgordon#				operation (i.e, after checking for required
47698186Sgordon#				bits and process (non)existence).
47778344Sobrien#				If this completes with a non-zero exit code,
47898186Sgordon#				don't run ${rc_arg}_cmd.
47978344Sobrien#
48098186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
48198186Sgordon#				${rc_arg}_cmd method, if that method
48298186Sgordon#				returned a zero exit code.
48398186Sgordon#
48478344Sobrien#	required_dirs	n	If set, check for the existence of the given
485165565Syar#				directories before running a (re)start command.
48678344Sobrien#
48778344Sobrien#	required_files	n	If set, check for the readability of the given
488165565Syar#				files before running a (re)start command.
48978344Sobrien#
490165565Syar#	required_modules n	If set, ensure the given kernel modules are
491165565Syar#				loaded before running a (re)start command.
492165565Syar#				The check and possible loads are actually
493165565Syar#				done after start_precmd so that the modules
494165565Syar#				aren't loaded in vain, should the precmd
495165565Syar#				return a non-zero status to indicate a error.
496165565Syar#				If a word in the list looks like "foo:bar",
497165565Syar#				"foo" is the KLD file name and "bar" is the
498165565Syar#				module name.  If a word looks like "foo~bar",
499165565Syar#				"foo" is the KLD file name and "bar" is a
500165565Syar#				egrep(1) pattern matching the module name.
501165565Syar#				Otherwise the module name is assumed to be
502165565Syar#				the same as the KLD file name, which is most
503165565Syar#				common.  See load_kld().
504165565Syar#
50578344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
50678344Sobrien#				listed variables before running the default
50778344Sobrien#				(re)start command.
50878344Sobrien#
50998186Sgordon#	Default behaviour for a given argument, if no override method is
51098186Sgordon#	provided:
51178344Sobrien#
51298186Sgordon#	Argument	Default behaviour
51398186Sgordon#	--------	-----------------
51478344Sobrien#	start		if !running && checkyesno ${rcvar}
51578344Sobrien#				${command}
51678344Sobrien#
51778344Sobrien#	stop		if ${pidfile}
51898186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
51978344Sobrien#			else
52098186Sgordon#				rc_pid=$(check_process $command)
52198186Sgordon#			kill $sig_stop $rc_pid
52298186Sgordon#			wait_for_pids $rc_pid
52398186Sgordon#			($sig_stop defaults to TERM.)
52478344Sobrien#
52598186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
52698186Sgordon#			and doesn't wait_for_pids.
52778344Sobrien#			$sig_reload defaults to HUP.
528151685Syar#			Note that `reload' isn't provided by default,
529151685Syar#			it should be enabled via $extra_commands.
53078344Sobrien#
53178344Sobrien#	restart		Run `stop' then `start'.
53278344Sobrien#
53398186Sgordon#	status		Show if ${command} is running, etc.
53478344Sobrien#
53598186Sgordon#	poll		Wait for ${command} to exit.
53698186Sgordon#
53798186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
53898186Sgordon#
53998186Sgordon#	Variables available to methods, and after run_rc_command() has
54098186Sgordon#	completed:
54198186Sgordon#
54298186Sgordon#	Variable	Purpose
54398186Sgordon#	--------	-------
544126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
54598186Sgordon#			performed
54698186Sgordon#
54798186Sgordon#	rc_flags	Flags to start the default command with.
54898186Sgordon#			Defaults to ${name}_flags, unless overridden
54998186Sgordon#			by $flags from the environment.
55098186Sgordon#			This variable may be changed by the precmd method.
55198186Sgordon#
55298186Sgordon#	rc_pid		PID of command (if appropriate)
55398186Sgordon#
55498186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
55598186Sgordon#
55698186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
55798186Sgordon#
558175676Smtm#	rc_quiet	Not empty if "quiet" was provided
55998186Sgordon#
560175676Smtm#
56178344Sobrienrun_rc_command()
56278344Sobrien{
563116097Smtm	_return=0
56498186Sgordon	rc_arg=$1
56578344Sobrien	if [ -z "$name" ]; then
56698186Sgordon		err 3 'run_rc_command: $name is not set.'
56778344Sobrien	fi
56878344Sobrien
569132892Smtm	# Don't repeat the first argument when passing additional command-
570132892Smtm	# line arguments to the command subroutines.
571132892Smtm	#
572132892Smtm	shift 1
573132892Smtm	rc_extra_args="$*"
574132892Smtm
575126303Smtm	_rc_prefix=
57698186Sgordon	case "$rc_arg" in
57778344Sobrien	fast*)				# "fast" prefix; don't check pid
57898186Sgordon		rc_arg=${rc_arg#fast}
57998186Sgordon		rc_fast=yes
580175676Smtm		rc_quiet=yes
58178344Sobrien		;;
582126303Smtm	force*)				# "force prefix; always run
58398186Sgordon		rc_force=yes
584126303Smtm		_rc_prefix=force
585126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
58678344Sobrien		if [ -n "${rcvar}" ]; then
58778344Sobrien			eval ${rcvar}=YES
58878344Sobrien		fi
58978344Sobrien		;;
590126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
591126303Smtm		_rc_prefix=one
592126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
593126303Smtm		if [ -n "${rcvar}" ]; then
594126303Smtm			eval ${rcvar}=YES
595126303Smtm		fi
596126303Smtm		;;
597175676Smtm	quiet*)				# "quiet" prefix; omit some messages
598175676Smtm		_rc_prefix=quiet
599175676Smtm		rc_arg=${rc_arg#${_rc_prefix}}
600175676Smtm		rc_quiet=yes
601175676Smtm		;;
60278344Sobrien	esac
60378344Sobrien
604161530Sflz	eval _override_command=\$${name}_program
605197790Shrs	command=${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					#
711197297Sdougb			_show_startmsgs=1
712197297Sdougb			if [ -n "${rc_quiet}" ]; then
713197297Sdougb				if ! checkyesno rc_startmsgs; then
714197297Sdougb					unset _show_startmsgs
715197297Sdougb				fi
716197297Sdougb			fi
717197297Sdougb			[ -n "$_show_startmsgs" ] && echo "Starting ${name}."
71878344Sobrien			if [ -n "$_chroot" ]; then
71978344Sobrien				_doit="\
72078344Sobrien${_nice:+nice -n $_nice }\
72178344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
72298186Sgordon$_chroot $command $rc_flags $command_args"
72378344Sobrien			else
72478344Sobrien				_doit="\
725161396Syar${_chdir:+cd $_chdir && }\
72698186Sgordon$command $rc_flags $command_args"
72798186Sgordon				if [ -n "$_user" ]; then
72898186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
72998186Sgordon				fi
730161396Syar				if [ -n "$_nice" ]; then
731161396Syar					if [ -z "$_user" ]; then
732161396Syar						_doit="sh -c \"$_doit\""
733161396Syar					fi	
734161396Syar					_doit="nice -n $_nice $_doit"
735161396Syar				fi
73678344Sobrien			fi
73798186Sgordon
738165565Syar					# run the full command
73998186Sgordon					#
740179946Smtm			if ! _run_rc_doit "$_doit"; then
741179946Smtm				warn "failed to start ${name}"
742179946Smtm				return 1
743179946Smtm			fi
74498186Sgordon
74598186Sgordon					# finally, run postcmd
74698186Sgordon					#
747165565Syar			_run_rc_postcmd
74878344Sobrien			;;
74978344Sobrien
75078344Sobrien		stop)
75198186Sgordon			if [ -z "$rc_pid" ]; then
752153152Syar				[ -n "$rc_fast" ] && return 0
753165565Syar				_run_rc_notrunning
754153152Syar				return 1
75578344Sobrien			fi
75678344Sobrien
757165565Syar			_run_rc_precmd || return 1
75898186Sgordon
75998186Sgordon					# send the signal to stop
76098186Sgordon					#
76178344Sobrien			echo "Stopping ${name}."
762165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
763165565Syar			_run_rc_doit "$_doit" || return 1
76498186Sgordon
76598186Sgordon					# wait for the command to exit,
76698186Sgordon					# and run postcmd.
76798186Sgordon			wait_for_pids $rc_pid
768165565Syar
769165565Syar			_run_rc_postcmd
77078344Sobrien			;;
77178344Sobrien
77278344Sobrien		reload)
77398186Sgordon			if [ -z "$rc_pid" ]; then
774165565Syar				_run_rc_notrunning
775153152Syar				return 1
77678344Sobrien			fi
777165565Syar
778165565Syar			_run_rc_precmd || return 1
779165565Syar
780165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
781165565Syar			_run_rc_doit "$_doit" || return 1
782165565Syar
783165565Syar			_run_rc_postcmd
78478344Sobrien			;;
78578344Sobrien
78678344Sobrien		restart)
78778344Sobrien					# prevent restart being called more
78878344Sobrien					# than once by any given script
78978344Sobrien					#
790126285Smtm			if ${_rc_restart_done:-false}; then
79178344Sobrien				return 0
79278344Sobrien			fi
793126285Smtm			_rc_restart_done=true
79478344Sobrien
795165565Syar			_run_rc_precmd || return 1
796165565Syar
797165565Syar			# run those in a subshell to keep global variables
798152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
799165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
800165565Syar			_return=$?
801165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
80298186Sgordon
803165565Syar			_run_rc_postcmd
80478344Sobrien			;;
80578344Sobrien
80698186Sgordon		poll)
807165565Syar			_run_rc_precmd || return 1
80898186Sgordon			if [ -n "$rc_pid" ]; then
80998186Sgordon				wait_for_pids $rc_pid
81098186Sgordon			fi
811165565Syar			_run_rc_postcmd
81298186Sgordon			;;
81398186Sgordon
81478344Sobrien		rcvar)
815197144Shrs			echo -n "# $name"
816197144Shrs			if [ -n "$desc" ]; then
817197144Shrs				echo " : $desc"
818197144Shrs			else
819197144Shrs				echo ""
820197144Shrs			fi
821197144Shrs			echo "#"
822197144Shrs			# Get unique vars in $rcvar $rcvars
823197144Shrs			for _v in $rcvar $rcvars; do
824197144Shrs				case $v in
825197144Shrs				$_v\ *|\ *$_v|*\ $_v\ *) ;;
826197144Shrs				*)	v="${v# } $_v" ;;
827197144Shrs				esac
828197144Shrs			done
829197144Shrs
830197144Shrs			# Display variables.
831197144Shrs			for _v in $v; do
832197144Shrs				if [ -z "$_v" ]; then
833197144Shrs					continue
83478344Sobrien				fi
835197144Shrs
836197144Shrs				eval _desc=\$${_v}_desc
837197144Shrs				eval _defval=\$${_v}_defval
838197144Shrs				_h="-"
839197144Shrs
840197144Shrs				eval echo \"$_v=\\\"\$$_v\\\"\"
841197144Shrs				# decode multiple lines of _desc
842197144Shrs				while [ -n "$_desc" ]; do
843197144Shrs					case $_desc in
844197144Shrs					*^^*)
845197144Shrs						echo "# $_h ${_desc%%^^*}"
846197144Shrs						_desc=${_desc#*^^}
847197144Shrs						_h=" "
848197144Shrs						;;
849197144Shrs					*)
850197144Shrs						echo "# $_h ${_desc}"
851197144Shrs						break
852197144Shrs						;;
853197144Shrs					esac
854197144Shrs				done
855197144Shrs				echo "#   (default: \"$_defval\")"
856197144Shrs			done
857197144Shrs			echo ""
85878344Sobrien			;;
85978344Sobrien
86078344Sobrien		*)
861150796Syar			rc_usage $_keywords
86278344Sobrien			;;
86378344Sobrien
86478344Sobrien		esac
865116097Smtm		return $_return
86678344Sobrien	done
86778344Sobrien
86898186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
869150796Syar	rc_usage $_keywords
870153152Syar	# not reached
87178344Sobrien}
87278344Sobrien
87378344Sobrien#
874165565Syar# Helper functions for run_rc_command: common code.
875165565Syar# They use such global variables besides the exported rc_* ones:
876165565Syar#
877165565Syar#	name	       R/W
878165565Syar#	------------------
879165565Syar#	_precmd		R
880165565Syar#	_postcmd	R
881165565Syar#	_return		W
882165565Syar#
883165565Syar_run_rc_precmd()
884165565Syar{
885165565Syar	check_required_before "$rc_arg" || return 1
886165565Syar
887165565Syar	if [ -n "$_precmd" ]; then
888165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
889165565Syar		eval "$_precmd $rc_extra_args"
890165565Syar		_return=$?
891165565Syar
892165565Syar		# If precmd failed and force isn't set, request exit.
893165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
894165565Syar			return 1
895165565Syar		fi
896165565Syar	fi
897165565Syar
898165565Syar	check_required_after "$rc_arg" || return 1
899165565Syar
900165565Syar	return 0
901165565Syar}
902165565Syar
903165565Syar_run_rc_postcmd()
904165565Syar{
905165565Syar	if [ -n "$_postcmd" ]; then
906165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
907165565Syar		eval "$_postcmd $rc_extra_args"
908165565Syar		_return=$?
909165565Syar	fi
910165565Syar	return 0
911165565Syar}
912165565Syar
913165565Syar_run_rc_doit()
914165565Syar{
915165565Syar	debug "run_rc_command: doit: $*"
916165565Syar	eval "$@"
917165565Syar	_return=$?
918165565Syar
919165565Syar	# If command failed and force isn't set, request exit.
920165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
921165565Syar		return 1
922165565Syar	fi
923165565Syar
924165565Syar	return 0
925165565Syar}
926165565Syar
927165565Syar_run_rc_notrunning()
928165565Syar{
929165565Syar	local _pidmsg
930165565Syar
931165565Syar	if [ -n "$pidfile" ]; then
932165565Syar		_pidmsg=" (check $pidfile)."
933165565Syar	else
934165565Syar		_pidmsg=
935165565Syar	fi
936165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
937165565Syar}
938165565Syar
939165565Syar_run_rc_killcmd()
940165565Syar{
941165565Syar	local _cmd
942165565Syar
943165565Syar	_cmd="kill -$1 $rc_pid"
944165565Syar	if [ -n "$_user" ]; then
945165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
946165565Syar	fi
947165565Syar	echo "$_cmd"
948165565Syar}
949165565Syar
950165565Syar#
95178344Sobrien# run_rc_script file arg
95278344Sobrien#	Start the script `file' with `arg', and correctly handle the
95378344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
95498186Sgordon#	sourced into the current environment.  If `file' appears to be
95598186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
95698186Sgordon#	executable run as a child process.
95778344Sobrien#
95878344Sobrienrun_rc_script()
95978344Sobrien{
96078344Sobrien	_file=$1
96178344Sobrien	_arg=$2
96278344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
96378344Sobrien		err 3 'USAGE: run_rc_script file arg'
96478344Sobrien	fi
96578344Sobrien
96698186Sgordon	unset	name command command_args command_interpreter \
96798186Sgordon		extra_commands pidfile procname \
968197144Shrs		rcvar rcvars rcvars_obsolete required_dirs required_files \
969197144Shrs		required_vars
97098186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
97198186Sgordon
97278344Sobrien	case "$_file" in
973193118Sdougb	/etc/rc.d/*.sh)			# no longer allowed in the base
974193118Sdougb		warn "Ignoring old-style startup script $_file"
97578344Sobrien		;;
976153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
97798186Sgordon		warn "Ignoring scratch file $_file"
97898186Sgordon		;;
97978344Sobrien	*)				# run in subshell
98098186Sgordon		if [ -x $_file ]; then
98198186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
982146490Sschweikh				set $_arg; . $_file
98398186Sgordon			else
984130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
985130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
986184317Sthompsa				  trap "echo Script $_file running" 29
987146490Sschweikh				  set $_arg; . $_file )
98898186Sgordon			fi
98998186Sgordon		fi
99078344Sobrien		;;
99178344Sobrien	esac
99278344Sobrien}
99378344Sobrien
99478344Sobrien#
995157653Sflz# load_rc_config name
996157653Sflz#	Source in the configuration file for a given name.
99778344Sobrien#
99878344Sobrienload_rc_config()
99978344Sobrien{
1000197144Shrs	local _name _var _defval _v _msg _new
1001157653Sflz	_name=$1
1002157653Sflz	if [ -z "$_name" ]; then
1003157653Sflz		err 3 'USAGE: load_rc_config name'
100478344Sobrien	fi
100578344Sobrien
1006126285Smtm	if ${_rc_conf_loaded:-false}; then
1007126285Smtm		:
1008126285Smtm	else
100998186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
101098186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
101198186Sgordon			. /etc/defaults/rc.conf
101298186Sgordon			source_rc_confs
101398186Sgordon		elif [ -r /etc/rc.conf ]; then
101498186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
101598186Sgordon			. /etc/rc.conf
101698186Sgordon		fi
1017126285Smtm		_rc_conf_loaded=true
101898186Sgordon	fi
1019161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
1020157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
1021161530Sflz		. /etc/rc.conf.d/"$_name"
1022157653Sflz	fi
1023179872Smtm
1024179872Smtm	# Old variable names support
1025179872Smtm	#
1026179872Smtm	[ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1027197144Shrs
1028197144Shrs	# Set defaults if defined.
1029197144Shrs	for _var in $rcvar $rcvars; do
1030197144Shrs		_defval=`eval echo "\\\$${_var}_defval"`
1031197144Shrs		if [ -n "$_defval" ]; then
1032197144Shrs			eval : \${$_var:=\$${_var}_defval}
1033197144Shrs		fi
1034197144Shrs	done
1035197144Shrs
1036197144Shrs	# check obsolete rc.conf variables
1037197144Shrs	for _var in $rcvars_obsolete; do
1038197144Shrs		_v=`eval echo \\$$_var`
1039197144Shrs		_msg=`eval echo \\$${_var}_obsolete_msg`
1040197144Shrs		_new=`eval echo \\$${_var}_newvar`
1041197144Shrs		case $_v in
1042197144Shrs		"")
1043197144Shrs			;;
1044197144Shrs		*)
1045197144Shrs			if [ -z "$_new" ]; then
1046197144Shrs				_msg="Ignored."
1047197144Shrs			else
1048197144Shrs				eval $_new=\"\$$_var\"
1049197144Shrs				if [ -z "$_msg" ]; then
1050197144Shrs					_msg="Use \$$_new instead."
1051197144Shrs				fi
1052197144Shrs			fi
1053197144Shrs			warn "\$$_var is obsolete.  $_msg"
1054197144Shrs			;;
1055197144Shrs		esac
1056197144Shrs	done
105778344Sobrien}
1058157473Sflz  
1059157473Sflz#
1060157653Sflz# load_rc_config_var name var
1061157653Sflz#	Read the rc.conf(5) var for name and set in the
1062157473Sflz#	current shell, using load_rc_config in a subshell to prevent
1063157473Sflz#	unwanted side effects from other variable assignments.
1064157473Sflz#
1065157473Sflzload_rc_config_var()
1066157473Sflz{
1067157473Sflz	if [ $# -ne 2 ]; then
1068157653Sflz		err 3 'USAGE: load_rc_config_var name var'
1069157473Sflz	fi
1070157473Sflz	eval $(eval '(
1071157473Sflz		load_rc_config '$1' >/dev/null;
1072157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1073157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
1074157473Sflz		fi
1075157473Sflz	)' )
1076157473Sflz}
107778344Sobrien
107878344Sobrien#
107978344Sobrien# rc_usage commands
108078344Sobrien#	Print a usage string for $0, with `commands' being a list of
108178344Sobrien#	valid commands.
108278344Sobrien#
108378344Sobrienrc_usage()
108478344Sobrien{
1085126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
108678344Sobrien
108778344Sobrien	_sep=
1088126286Smtm	for _elem; do
108978344Sobrien		echo -n 1>&2 "$_sep$_elem"
109078344Sobrien		_sep="|"
109178344Sobrien	done
109278344Sobrien	echo 1>&2 ")"
109378344Sobrien	exit 1
109478344Sobrien}
109578344Sobrien
109678344Sobrien#
109778344Sobrien# err exitval message
109878344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
109978344Sobrien#
110078344Sobrienerr()
110178344Sobrien{
110278344Sobrien	exitval=$1
110378344Sobrien	shift
110478344Sobrien
1105106643Sgordon	if [ -x /usr/bin/logger ]; then
1106106643Sgordon		logger "$0: ERROR: $*"
1107106643Sgordon	fi
1108106643Sgordon	echo 1>&2 "$0: ERROR: $*"
110978344Sobrien	exit $exitval
111078344Sobrien}
111178344Sobrien
111278344Sobrien#
111378344Sobrien# warn message
111478344Sobrien#	Display message to stderr and log to the syslog.
111578344Sobrien#
111678344Sobrienwarn()
111778344Sobrien{
1118106643Sgordon	if [ -x /usr/bin/logger ]; then
1119106643Sgordon		logger "$0: WARNING: $*"
1120106643Sgordon	fi
1121106643Sgordon	echo 1>&2 "$0: WARNING: $*"
112278344Sobrien}
112398186Sgordon
112498186Sgordon#
112598186Sgordon# info message
112698186Sgordon#	Display informational message to stdout and log to syslog.
112798186Sgordon#
112898186Sgordoninfo()
112998186Sgordon{
1130119170Smtm	case ${rc_info} in
1131119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1132119170Smtm		if [ -x /usr/bin/logger ]; then
1133119170Smtm			logger "$0: INFO: $*"
1134119170Smtm		fi
1135119170Smtm		echo "$0: INFO: $*"
1136119170Smtm		;;
1137119170Smtm	esac
113898186Sgordon}
113998186Sgordon
114098186Sgordon#
114198186Sgordon# debug message
1142106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
114398186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
114498186Sgordon#	function.
114598186Sgordon#
114698186Sgordondebug()
114798186Sgordon{
114898186Sgordon	case ${rc_debug} in
114998186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1150106700Sgordon		if [ -x /usr/bin/logger ]; then
1151162947Syar			logger "$0: DEBUG: $*"
1152106700Sgordon		fi
1153146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
115498186Sgordon		;;
115598186Sgordon	esac
115698186Sgordon}
115798186Sgordon
115898186Sgordon#
115998186Sgordon# backup_file action file cur backup
116098186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
116198186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
116298186Sgordon#
116398186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
116498186Sgordon#	which can be either YES or NO.
116598186Sgordon#
116698186Sgordon#	The `action' keyword can be one of the following:
116798186Sgordon#
116898186Sgordon#	add		`file' is now being backed up (and is possibly
116998186Sgordon#			being reentered into the backups system).  `cur'
117098186Sgordon#			is created and RCS files, if necessary, are
117198186Sgordon#			created as well.
117298186Sgordon#
117398186Sgordon#	update		`file' has changed and needs to be backed up.
117498186Sgordon#			If `cur' exists, it is copied to to `back' or
117598186Sgordon#			checked into RCS (if the repository file is old),
117698186Sgordon#			and then `file' is copied to `cur'.  Another RCS
117798186Sgordon#			check in done here if RCS is being used.
117898186Sgordon#
117998186Sgordon#	remove		`file' is no longer being tracked by the backups
118098186Sgordon#			system.  If RCS is not being used, `cur' is moved
118198186Sgordon#			to `back', otherwise an empty file is checked in,
118298186Sgordon#			and then `cur' is removed.
118398186Sgordon#
118498186Sgordon#
118598186Sgordonbackup_file()
118698186Sgordon{
118798186Sgordon	_action=$1
118898186Sgordon	_file=$2
118998186Sgordon	_cur=$3
119098186Sgordon	_back=$4
119198186Sgordon
119298186Sgordon	if checkyesno backup_uses_rcs; then
119398186Sgordon		_msg0="backup archive"
119498186Sgordon		_msg1="update"
119598186Sgordon
119698186Sgordon		# ensure that history file is not locked
119798186Sgordon		if [ -f $_cur,v ]; then
119898186Sgordon			rcs -q -u -U -M $_cur
119998186Sgordon		fi
120098186Sgordon
120198186Sgordon		# ensure after switching to rcs that the
120298186Sgordon		# current backup is not lost
120398186Sgordon		if [ -f $_cur ]; then
120498186Sgordon			# no archive, or current newer than archive
120598186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
120698186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
120798186Sgordon				rcs -q -kb -U $_cur
120898186Sgordon				co -q -f -u $_cur
120998186Sgordon			fi
121098186Sgordon		fi
121198186Sgordon
121298186Sgordon		case $_action in
121398186Sgordon		add|update)
121498186Sgordon			cp -p $_file $_cur
121598186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
121698186Sgordon			rcs -q -kb -U $_cur
121798186Sgordon			co -q -f -u $_cur
121898186Sgordon			chown root:wheel $_cur $_cur,v
121998186Sgordon			;;
122098186Sgordon		remove)
122198186Sgordon			cp /dev/null $_cur
122298186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
122398186Sgordon			rcs -q -kb -U $_cur
122498186Sgordon			chown root:wheel $_cur $_cur,v
122598186Sgordon			rm $_cur
122698186Sgordon			;;
122798186Sgordon		esac
122898186Sgordon	else
122998186Sgordon		case $_action in
123098186Sgordon		add|update)
123198186Sgordon			if [ -f $_cur ]; then
123298186Sgordon				cp -p $_cur $_back
123398186Sgordon			fi
123498186Sgordon			cp -p $_file $_cur
123598186Sgordon			chown root:wheel $_cur
123698186Sgordon			;;
123798186Sgordon		remove)
123898186Sgordon			mv -f $_cur $_back
123998186Sgordon			;;
124098186Sgordon		esac
124198186Sgordon	fi
124298186Sgordon}
1243119166Smtm
1244123344Smtm# make_symlink src link
1245123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1246123344Smtm#	directory in which link is to be created does not exist
1247123344Smtm#	a warning will be displayed and an error will be returned.
1248123344Smtm#	Returns 0 on sucess, 1 otherwise.
1249119166Smtm#
1250123344Smtmmake_symlink()
1251119166Smtm{
1252123344Smtm	local src link linkdir _me
1253123344Smtm	src="$1"
1254123344Smtm	link="$2"
1255123344Smtm	linkdir="`dirname $link`"
1256123344Smtm	_me="make_symlink()"
1257119166Smtm
1258123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1259123344Smtm		warn "$_me: requires two arguments."
1260119166Smtm		return 1
1261119166Smtm	fi
1262123344Smtm	if [ ! -d "$linkdir" ]; then
1263160667Syar		warn "$_me: the directory $linkdir does not exist."
1264119166Smtm		return 1
1265119166Smtm	fi
1266146490Sschweikh	if ! ln -sf $src $link; then
1267123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1268119166Smtm		return 1
1269119166Smtm	fi
1270119166Smtm	return 0
1271119166Smtm}
1272119166Smtm
1273119166Smtm# devfs_rulesets_from_file file
1274119166Smtm#	Reads a set of devfs commands from file, and creates
1275119166Smtm#	the specified rulesets with their rules. Returns non-zero
1276119166Smtm#	if there was an error.
1277119166Smtm#
1278119166Smtmdevfs_rulesets_from_file()
1279119166Smtm{
1280119166Smtm	local file _err _me
1281119166Smtm	file="$1"
1282119166Smtm	_me="devfs_rulesets_from_file"
1283119166Smtm	_err=0
1284119166Smtm
1285119166Smtm	if [ -z "$file" ]; then
1286119166Smtm		warn "$_me: you must specify a file"
1287119166Smtm		return 1
1288119166Smtm	fi
1289119166Smtm	if [ ! -e "$file" ]; then
1290119166Smtm		debug "$_me: no such file ($file)"
1291119166Smtm		return 0
1292119166Smtm	fi
1293119166Smtm	debug "reading rulesets from file ($file)"
1294119166Smtm	{ while read line
1295119166Smtm	do
1296119166Smtm		case $line in
1297119166Smtm		\#*)
1298119166Smtm			continue
1299119166Smtm			;;
1300119166Smtm		\[*\]*)
1301119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1302119166Smtm			if [ -z "$rulenum" ]; then
1303119166Smtm				warn "$_me: cannot extract rule number ($line)"
1304119166Smtm				_err=1
1305119166Smtm				break
1306119166Smtm			fi
1307119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1308119166Smtm			if [ -z "$rulename" ]; then
1309119166Smtm				warn "$_me: cannot extract rule name ($line)"
1310119166Smtm				_err=1
1311119166Smtm				break;
1312119166Smtm			fi
1313119166Smtm			eval $rulename=\$rulenum
1314119166Smtm			debug "found ruleset: $rulename=$rulenum"
1315146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1316119166Smtm				_err=1
1317119166Smtm				break
1318119166Smtm			fi
1319119166Smtm			;;
1320119166Smtm		*)
1321119166Smtm			rulecmd="${line%%"\#*"}"
1322119166Smtm			# evaluate the command incase it includes
1323119166Smtm			# other rules
1324119166Smtm			if [ -n "$rulecmd" ]; then
1325119166Smtm				debug "adding rule ($rulecmd)"
1326119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1327119166Smtm				then
1328119166Smtm					_err=1
1329119166Smtm					break
1330119166Smtm				fi
1331119166Smtm			fi
1332119166Smtm			;;
1333119166Smtm		esac
1334119166Smtm		if [ $_err -ne 0 ]; then
1335119166Smtm			debug "error in $_me"
1336119166Smtm			break
1337119166Smtm		fi
1338119166Smtm	done } < $file
1339119166Smtm	return $_err
1340119166Smtm}
1341119166Smtm
1342119166Smtm# devfs_init_rulesets
1343119166Smtm#	Initializes rulesets from configuration files. Returns
1344119166Smtm#	non-zero if there was an error.
1345119166Smtm#
1346119166Smtmdevfs_init_rulesets()
1347119166Smtm{
1348119166Smtm	local file _me
1349119166Smtm	_me="devfs_init_rulesets"
1350119166Smtm
1351119166Smtm	# Go through this only once
1352119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1353119166Smtm		debug "$_me: devfs rulesets already initialized"
1354119166Smtm		return
1355119166Smtm	fi
1356146490Sschweikh	for file in $devfs_rulesets; do
1357119166Smtm		devfs_rulesets_from_file $file || return 1
1358119166Smtm	done
1359119166Smtm	devfs_rulesets_init=1
1360119166Smtm	debug "$_me: devfs rulesets initialized"
1361119166Smtm	return 0
1362119166Smtm}
1363119166Smtm
1364119166Smtm# devfs_set_ruleset ruleset [dir]
1365151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1366119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1367119166Smtm#	Returns non-zero if it could not set it successfully.
1368119166Smtm#
1369119166Smtmdevfs_set_ruleset()
1370119166Smtm{
1371119166Smtm	local devdir rs _me
1372119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1373119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1374119166Smtm	_me="devfs_set_ruleset"
1375119166Smtm
1376119166Smtm	if [ -z "$rs" ]; then
1377119166Smtm		warn "$_me: you must specify a ruleset number"
1378119166Smtm		return 1
1379119166Smtm	fi
1380119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1381146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1382119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1383119166Smtm		return 1
1384119166Smtm	fi
1385119166Smtm	return 0
1386119166Smtm}
1387119166Smtm
1388119166Smtm# devfs_apply_ruleset ruleset [dir]
1389119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1390119166Smtm#	The ruleset argument must be a ruleset name as specified
1391119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1392119166Smtm#	if it could not apply the ruleset.
1393119166Smtm#
1394119166Smtmdevfs_apply_ruleset()
1395119166Smtm{
1396119166Smtm	local devdir rs _me
1397119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1398119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1399119166Smtm	_me="devfs_apply_ruleset"
1400119166Smtm
1401119166Smtm	if [ -z "$rs" ]; then
1402119166Smtm		warn "$_me: you must specify a ruleset"
1403119166Smtm		return 1
1404119166Smtm	fi
1405119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1406146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1407119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1408119166Smtm		return 1
1409119166Smtm	fi
1410119166Smtm	return 0
1411119166Smtm}
1412119166Smtm
1413119166Smtm# devfs_domount dir [ruleset]
1414119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1415119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1416119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1417119166Smtm#
1418119166Smtmdevfs_domount()
1419119166Smtm{
1420119166Smtm	local devdir rs _me
1421119166Smtm	devdir="$1"
1422119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1423119166Smtm	_me="devfs_domount()"
1424119166Smtm
1425119166Smtm	if [ -z "$devdir" ]; then
1426119166Smtm		warn "$_me: you must specify a mount-point"
1427119166Smtm		return 1
1428119166Smtm	fi
1429119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1430146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1431119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1432119166Smtm		return 1
1433119166Smtm	fi
1434119166Smtm	if [ -n "$rs" ]; then
1435119166Smtm		devfs_init_rulesets
1436119166Smtm		devfs_set_ruleset $rs $devdir
1437124797Scperciva		devfs -m $devdir rule applyset
1438119166Smtm	fi
1439119166Smtm	return 0
1440119166Smtm}
1441119166Smtm
1442119166Smtm# devfs_mount_jail dir [ruleset]
1443119166Smtm#	Mounts a devfs file system appropriate for jails
1444119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1445119166Smtm#	it names will be used instead.  If present, ruleset must
1446119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1447119166Smtm#	This function returns non-zero if an error occurs.
1448119166Smtm#
1449119166Smtmdevfs_mount_jail()
1450119166Smtm{
1451119166Smtm	local jdev rs _me
1452119166Smtm	jdev="$1"
1453119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1454119166Smtm	_me="devfs_mount_jail"
1455119166Smtm
1456119166Smtm	devfs_init_rulesets
1457146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1458119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1459119166Smtm		return 1
1460119166Smtm	fi
1461119166Smtm	return 0
1462119166Smtm}
1463127345Sbrooks
1464127345Sbrooks# Provide a function for normalizing the mounting of memory
1465127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1466127345Sbrooks# as close as possible between 5-current and 4-stable.
1467127345Sbrooks#   $1 = size
1468127345Sbrooks#   $2 = mount point
1469137451Skeramida#   $3 = (optional) extra mdmfs flags
1470146490Sschweikhmount_md()
1471146490Sschweikh{
1472127345Sbrooks	if [ -n "$3" ]; then
1473137451Skeramida		flags="$3"
1474127345Sbrooks	fi
1475149421Syar	/sbin/mdmfs $flags -s $1 md $2
1476127345Sbrooks}
1477131550Scperciva
1478159828Syar# Code common to scripts that need to load a kernel module
1479159828Syar# if it isn't in the kernel yet. Syntax:
1480160666Syar#   load_kld [-e regex] [-m module] file
1481159828Syar# where -e or -m chooses the way to check if the module
1482159828Syar# is already loaded:
1483160666Syar#   regex is egrep'd in the output from `kldstat -v',
1484160666Syar#   module is passed to `kldstat -m'.
1485160666Syar# The default way is as though `-m file' were specified.
1486159828Syarload_kld()
1487159828Syar{
1488159828Syar	local _loaded _mod _opt _re
1489159828Syar
1490159828Syar	while getopts "e:m:" _opt; do
1491159828Syar		case "$_opt" in
1492159828Syar		e) _re="$OPTARG" ;;
1493159828Syar		m) _mod="$OPTARG" ;;
1494160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1495159828Syar		esac
1496159828Syar	done
1497159828Syar	shift $(($OPTIND - 1))
1498160666Syar	if [ $# -ne 1 ]; then
1499160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1500160666Syar	fi
1501159828Syar	_mod=${_mod:-$1}
1502159828Syar	_loaded=false
1503159828Syar	if [ -n "$_re" ]; then
1504159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1505159828Syar			_loaded=true
1506159828Syar		fi
1507159828Syar	else
1508159828Syar		if kldstat -q -m "$_mod"; then
1509159828Syar			_loaded=true
1510159828Syar		fi
1511159828Syar	fi
1512159828Syar	if ! $_loaded; then
1513159828Syar		if ! kldload "$1"; then
1514159828Syar			warn "Unable to load kernel module $1"
1515159828Syar			return 1
1516160666Syar		else
1517160666Syar			info "$1 kernel module loaded."
1518159828Syar		fi
1519160666Syar	else
1520160666Syar		debug "load_kld: $1 kernel module already loaded."
1521159828Syar	fi
1522159828Syar	return 0
1523159828Syar}
1524159828Syar
1525149049Spjd# ltr str src dst
1526149049Spjd#	Change every $src in $str to $dst.
1527149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1528149049Spjd#	awk(1).
1529149049Spjdltr()
1530149049Spjd{
1531149049Spjd	local _str _src _dst _out _com
1532149049Spjd	_str=$1
1533149049Spjd	_src=$2
1534149049Spjd	_dst=$3
1535149049Spjd	_out=""
1536149049Spjd
1537149049Spjd	IFS=${_src}
1538149049Spjd	for _com in ${_str}; do
1539149049Spjd		if [ -z "${_out}" ]; then
1540149049Spjd			_out="${_com}"
1541149049Spjd		else
1542149049Spjd			_out="${_out}${_dst}${_com}"
1543149049Spjd		fi
1544149049Spjd	done
1545149049Spjd	echo "${_out}"
1546149049Spjd}
1547149049Spjd
1548149050Spjd# Creates a list of providers for GELI encryption.
1549149050Spjdgeli_make_list()
1550149050Spjd{
1551149050Spjd	local devices devices2
1552149050Spjd	local provider mountpoint type options rest
1553149050Spjd
1554149050Spjd	# Create list of GELI providers from fstab.
1555149050Spjd	while read provider mountpoint type options rest ; do
1556155570Sflz		case ":${options}" in
1557155570Sflz		:*noauto*)
1558155570Sflz			noauto=yes
1559155570Sflz			;;
1560155570Sflz		*)
1561155570Sflz			noauto=no
1562155570Sflz			;;
1563155570Sflz		esac
1564155570Sflz
1565149050Spjd		case ":${provider}" in
1566149050Spjd		:#*)
1567149050Spjd			continue
1568149050Spjd			;;
1569149050Spjd		*.eli)
1570149050Spjd			# Skip swap devices.
1571155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1572149050Spjd				continue
1573149050Spjd			fi
1574149050Spjd			devices="${devices} ${provider}"
1575149050Spjd			;;
1576149050Spjd		esac
1577149050Spjd	done < /etc/fstab
1578149050Spjd
1579149050Spjd	# Append providers from geli_devices.
1580149050Spjd	devices="${devices} ${geli_devices}"
1581149050Spjd
1582149050Spjd	for provider in ${devices}; do
1583149050Spjd		provider=${provider%.eli}
1584149050Spjd		provider=${provider#/dev/}
1585149050Spjd		devices2="${devices2} ${provider}"
1586149050Spjd	done
1587149050Spjd
1588149050Spjd	echo ${devices2}
1589149050Spjd}
1590149050Spjd
1591153027Sdougb# Find scripts in local_startup directories that use the old syntax
1592153027Sdougb#
1593153027Sdougbfind_local_scripts_old () {
1594153027Sdougb	zlist=''
1595153027Sdougb	slist=''
1596153027Sdougb	for dir in ${local_startup}; do
1597153027Sdougb		if [ -d "${dir}" ]; then
1598153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1599153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1600153027Sdougb				    continue
1601153027Sdougb				zlist="$zlist $file"
1602153027Sdougb			done
1603153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1604153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1605153027Sdougb				    continue
1606153027Sdougb				slist="$slist $file"
1607153027Sdougb			done
1608153027Sdougb		fi
1609153027Sdougb	done
1610153027Sdougb}
1611153027Sdougb
1612153027Sdougbfind_local_scripts_new () {
1613153027Sdougb	local_rc=''
1614153027Sdougb	for dir in ${local_startup}; do
1615153027Sdougb		if [ -d "${dir}" ]; then
1616153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1617153027Sdougb				case "$file" in
1618153027Sdougb				*.sample) ;;
1619153027Sdougb				*)	if [ -x "$file" ]; then
1620153027Sdougb						local_rc="${local_rc} ${file}"
1621153027Sdougb					fi
1622153027Sdougb					;;
1623153027Sdougb				esac
1624153027Sdougb			done
1625153027Sdougb		fi
1626153027Sdougb	done
1627153027Sdougb}
1628153027Sdougb
1629165565Syar# check_required_{before|after} command
1630165565Syar#	Check for things required by the command before and after its precmd,
1631165565Syar#	respectively.  The two separate functions are needed because some
1632165565Syar#	conditions should prevent precmd from being run while other things
1633165565Syar#	depend on precmd having already been run.
1634165565Syar#
1635165565Syarcheck_required_before()
1636165565Syar{
1637165565Syar	local _f
1638165565Syar
1639165565Syar	case "$1" in
1640165565Syar	start)
1641165565Syar		for _f in $required_vars; do
1642165565Syar			if ! checkyesno $_f; then
1643165565Syar				warn "\$${_f} is not enabled."
1644165565Syar				if [ -z "$rc_force" ]; then
1645165565Syar					return 1
1646165565Syar				fi
1647165565Syar			fi
1648165565Syar		done
1649165565Syar
1650165565Syar		for _f in $required_dirs; do
1651165565Syar			if [ ! -d "${_f}/." ]; then
1652165565Syar				warn "${_f} is not a directory."
1653165565Syar				if [ -z "$rc_force" ]; then
1654165565Syar					return 1
1655165565Syar				fi
1656165565Syar			fi
1657165565Syar		done
1658165565Syar
1659165565Syar		for _f in $required_files; do
1660165565Syar			if [ ! -r "${_f}" ]; then
1661165565Syar				warn "${_f} is not readable."
1662165565Syar				if [ -z "$rc_force" ]; then
1663165565Syar					return 1
1664165565Syar				fi
1665165565Syar			fi
1666165565Syar		done
1667165565Syar		;;
1668165565Syar	esac
1669165565Syar
1670165565Syar	return 0
1671165565Syar}
1672165565Syar
1673165565Syarcheck_required_after()
1674165565Syar{
1675165565Syar	local _f _args
1676165565Syar
1677165565Syar	case "$1" in
1678165565Syar	start)
1679165565Syar		for _f in $required_modules; do
1680165565Syar			case "${_f}" in
1681165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1682165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1683165565Syar				*)	_args="${_f}" ;;
1684165565Syar			esac
1685165565Syar			if ! load_kld ${_args}; then
1686165565Syar				if [ -z "$rc_force" ]; then
1687165565Syar					return 1
1688165565Syar				fi
1689165565Syar			fi
1690165565Syar		done
1691165565Syar		;;
1692165565Syar	esac
1693165565Syar
1694165565Syar	return 0
1695165565Syar}
1696165565Syar
1697131550Scpercivafi
1698157841Sflz
1699197144Shrs# _echoonce var msg mode
1700197144Shrs#	mode=0: Echo $msg if ${$var} is empty.
1701197144Shrs#	        After doing echo, a string is set to ${$var}.
1702197144Shrs#
1703197144Shrs#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
1704197144Shrs#
1705197144Shrs_echoonce()
1706197144Shrs{
1707197144Shrs	local _var _msg _mode
1708197144Shrs	_var=`eval echo \\$$1`
1709197144Shrs	_msg=$2
1710197144Shrs	_mode=$3
1711197144Shrs
1712197144Shrs	case $_mode in
1713197144Shrs	1)	[ -n "$_var" ] && echo "$_msg" ;;
1714197144Shrs	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1715197144Shrs	esac
1716197144Shrs}
1717197144Shrs
1718157841Sflz_rc_subr_loaded=:
1719