rc.subr revision 219612
1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
2# $FreeBSD: head/etc/rc.subr 219612 2011-03-13 16:27:36Z dougb $
3#
4# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Luke Mewburn.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31# rc.subr
32#	functions used by various rc scripts
33#
34
35: ${rcvar_manpage:='rc.conf(5)'}
36: ${RC_PID:=$$}; export RC_PID
37
38#
39#	Operating System dependent/independent variables
40#
41
42if [ -z "${_rc_subr_loaded}" ]; then
43
44_rc_subr_loaded="YES"
45
46SYSCTL="/sbin/sysctl"
47SYSCTL_N="${SYSCTL} -n"
48SYSCTL_W="${SYSCTL}"
49ID="/usr/bin/id"
50IDCMD="if [ -x $ID ]; then $ID -un; fi"
51PS="/bin/ps -ww"
52JID=`$PS -p $$ -o jid=`
53
54#
55#	functions
56#	---------
57
58# set_rcvar [var] [defval] [desc]
59#
60#	Echo or define a rc.conf(5) variable name.  Global variable
61#	$rcvars is used.
62#
63#	If no argument is specified, echo "${name}_enable".
64#
65#	If only a var is specified, echo "${var}_enable".
66#
67#	If var and defval are specified, the ${var} is defined as
68#	rc.conf(5) variable and the default value is ${defvar}.  An
69#	optional argument $desc can also be specified to add a
70#	description for that.
71#
72set_rcvar()
73{
74	case $# in
75	0)
76		echo ${name}_enable
77		;;
78	1)
79		echo ${1}_enable
80		;;
81	*)
82		debug "rcvar_define: \$$1=$2 is added" \
83		    " as a rc.conf(5) variable."
84
85		local _var
86		_var=$1
87		rcvars="${rcvars# } $_var"
88		eval ${_var}_defval=\"$2\"
89		shift 2
90		# encode multiple lines of _desc
91		for l in "$@"; do
92			eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
93		done
94		eval ${_var}_desc=\"\${${_var}_desc#^^}\"
95		;;
96	esac
97}
98
99# set_rcvar_obsolete oldvar [newvar] [msg]
100#	Define obsolete variable.
101#	Global variable $rcvars_obsolete is used.
102#
103set_rcvar_obsolete()
104{
105	local _var
106	_var=$1
107	debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
108
109	rcvars_obsolete="${rcvars_obsolete# } $1"
110	eval ${1}_newvar=\"$2\"
111	shift 2
112	eval ${_var}_obsolete_msg=\"$*\"
113}
114
115#
116# force_depend script
117#	Force a service to start. Intended for use by services
118#	to resolve dependency issues. It is assumed the caller
119#	has check to make sure this call is necessary
120#	$1 - filename of script, in /etc/rc.d, to run
121#
122force_depend()
123{
124	_depend="$1"
125
126	info "${name} depends on ${_depend}, which will be forced to start."
127	if ! /etc/rc.d/${_depend} forcestart; then
128		warn "Unable to force ${_depend}. It may already be running."
129		return 1
130	fi
131	return 0
132}
133
134#
135# checkyesno var
136#	Test $1 variable, and warn if not set to YES or NO.
137#	Return 0 if it's "yes" (et al), nonzero otherwise.
138#
139checkyesno()
140{
141	eval _value=\$${1}
142	debug "checkyesno: $1 is set to $_value."
143	case $_value in
144
145		#	"yes", "true", "on", or "1"
146	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
147		return 0
148		;;
149
150		#	"no", "false", "off", or "0"
151	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
152		return 1
153		;;
154	*)
155		warn "\$${1} is not set properly - see ${rcvar_manpage}."
156		return 1
157		;;
158	esac
159}
160
161#
162# reverse_list list
163#	print the list in reverse order
164#
165reverse_list()
166{
167	_revlist=
168	for _revfile; do
169		_revlist="$_revfile $_revlist"
170	done
171	echo $_revlist
172}
173
174# stop_boot always
175#	If booting directly to multiuser or $always is enabled,
176#	send SIGTERM to the parent (/etc/rc) to abort the boot.
177#	Otherwise just exit.
178#
179stop_boot()
180{
181	local always
182
183	case $1 in
184		#	"yes", "true", "on", or "1"
185        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
186		always=true
187		;;
188	*)
189		always=false
190		;;
191	esac
192	if [ "$autoboot" = yes -o "$always" = true ]; then
193		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
194		kill -TERM ${RC_PID}
195	fi
196	exit 1
197}
198
199#
200# mount_critical_filesystems type
201#	Go through the list of critical filesystems as provided in
202#	the rc.conf(5) variable $critical_filesystems_${type}, checking
203#	each one to see if it is mounted, and if it is not, mounting it.
204#
205mount_critical_filesystems()
206{
207	eval _fslist=\$critical_filesystems_${1}
208	for _fs in $_fslist; do
209		mount | (
210			_ismounted=false
211			while read what _on on _type type; do
212				if [ $on = $_fs ]; then
213					_ismounted=true
214				fi
215			done
216			if $_ismounted; then
217				:
218			else
219				mount $_fs >/dev/null 2>&1
220			fi
221		)
222	done
223}
224
225#
226# check_pidfile pidfile procname [interpreter]
227#	Parses the first line of pidfile for a PID, and ensures
228#	that the process is running and matches procname.
229#	Prints the matching PID upon success, nothing otherwise.
230#	interpreter is optional; see _find_processes() for details.
231#
232check_pidfile()
233{
234	_pidfile=$1
235	_procname=$2
236	_interpreter=$3
237	if [ -z "$_pidfile" -o -z "$_procname" ]; then
238		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
239	fi
240	if [ ! -f $_pidfile ]; then
241		debug "pid file ($_pidfile): not readable."
242		return
243	fi
244	read _pid _junk < $_pidfile
245	if [ -z "$_pid" ]; then
246		debug "pid file ($_pidfile): no pid in file."
247		return
248	fi
249	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
250}
251
252#
253# check_process procname [interpreter]
254#	Ensures that a process (or processes) named procname is running.
255#	Prints a list of matching PIDs.
256#	interpreter is optional; see _find_processes() for details.
257#
258check_process()
259{
260	_procname=$1
261	_interpreter=$2
262	if [ -z "$_procname" ]; then
263		err 3 'USAGE: check_process procname [interpreter]'
264	fi
265	_find_processes $_procname ${_interpreter:-.} '-ax'
266}
267
268#
269# _find_processes procname interpreter psargs
270#	Search for procname in the output of ps generated by psargs.
271#	Prints the PIDs of any matching processes, space separated.
272#
273#	If interpreter == ".", check the following variations of procname
274#	against the first word of each command:
275#		procname
276#		`basename procname`
277#		`basename procname` + ":"
278#		"(" + `basename procname` + ")"
279#		"[" + `basename procname` + "]"
280#
281#	If interpreter != ".", read the first line of procname, remove the
282#	leading #!, normalise whitespace, append procname, and attempt to
283#	match that against each command, either as is, or with extra words
284#	at the end.  As an alternative, to deal with interpreted daemons
285#	using perl, the basename of the interpreter plus a colon is also
286#	tried as the prefix to procname.
287#
288_find_processes()
289{
290	if [ $# -ne 3 ]; then
291		err 3 'USAGE: _find_processes procname interpreter psargs'
292	fi
293	_procname=$1
294	_interpreter=$2
295	_psargs=$3
296
297	_pref=
298	if [ $_interpreter != "." ]; then	# an interpreted script
299		_script=${_chroot}${_chroot:+"/"}$_procname
300		if [ -r $_script ]; then
301			read _interp < $_script	# read interpreter name
302			case "$_interp" in
303			\#!*)
304				_interp=${_interp#\#!}	# strip #!
305				set -- $_interp
306				case $1 in
307				*/bin/env)
308					shift	# drop env to get real name
309					;;
310				esac
311				if [ $_interpreter != $1 ]; then
312					warn "\$command_interpreter $_interpreter != $1"
313				fi
314				;;
315			*)
316				warn "no shebang line in $_script"
317				set -- $_interpreter
318				;;
319			esac
320		else
321			warn "cannot read shebang line from $_script"
322			set -- $_interpreter
323		fi
324		_interp="$* $_procname"		# cleanup spaces, add _procname
325		_interpbn=${1##*/}
326		_fp_args='_argv'
327		_fp_match='case "$_argv" in
328		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
329	else					# a normal daemon
330		_procnamebn=${_procname##*/}
331		_fp_args='_arg0 _argv'
332		_fp_match='case "$_arg0" in
333		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
334	fi
335
336	_proccheck="\
337		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
338		while read _npid _jid '"$_fp_args"'; do
339			'"$_fp_match"'
340				if [ "$JID" -eq "$_jid" ];
341				then echo -n "$_pref$_npid";
342				_pref=" ";
343				fi
344				;;
345			esac
346		done'
347
348#	debug "in _find_processes: proccheck is ($_proccheck)."
349	eval $_proccheck
350}
351
352#
353# wait_for_pids pid [pid ...]
354#	spins until none of the pids exist
355#
356wait_for_pids()
357{
358	local _list _prefix _nlist _j
359
360	_list="$@"
361	if [ -z "$_list" ]; then
362		return
363	fi
364	_prefix=
365	while true; do
366		_nlist="";
367		for _j in $_list; do
368			if kill -0 $_j 2>/dev/null; then
369				_nlist="${_nlist}${_nlist:+ }$_j"
370				[ -n "$_prefix" ] && sleep 1
371			fi
372		done
373		if [ -z "$_nlist" ]; then
374			break
375		fi
376		_list=$_nlist
377		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
378		_prefix=", "
379		pwait $_list 2>/dev/null
380	done
381	if [ -n "$_prefix" ]; then
382		echo "."
383	fi
384}
385
386#
387# check_startmsgs
388#	If rc_quiet is set (usually as a result of using faststart at
389#	boot time) check if rc_startmsgs is enabled.
390#
391check_startmsgs()
392{
393	if [ -n "$rc_quiet" ]; then
394		checkyesno rc_startmsgs
395	else
396		return 0
397	fi
398}
399
400#
401# run_rc_command argument
402#	Search for argument in the list of supported commands, which is:
403#		"start stop restart rcvar status poll ${extra_commands}"
404#	If there's a match, run ${argument}_cmd or the default method
405#	(see below).
406#
407#	If argument has a given prefix, then change the operation as follows:
408#		Prefix	Operation
409#		------	---------
410#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
411#		force	Set ${rcvar} to YES, and set rc_force=yes
412#		one	Set ${rcvar} to YES
413#		quiet	Don't output some diagnostics, and set rc_quiet=yes
414#
415#	The following globals are used:
416#
417#	Name		Needed	Purpose
418#	----		------	-------
419#	name		y	Name of script.
420#
421#	command		n	Full path to command.
422#				Not needed if ${rc_arg}_cmd is set for
423#				each keyword.
424#
425#	command_args	n	Optional args/shell directives for command.
426#
427#	command_interpreter n	If not empty, command is interpreted, so
428#				call check_{pidfile,process}() appropriately.
429#
430#	desc		n	Description of script.
431#
432#	extra_commands	n	List of extra commands supported.
433#
434#	pidfile		n	If set, use check_pidfile $pidfile $command,
435#				otherwise use check_process $command.
436#				In either case, only check if $command is set.
437#
438#	procname	n	Process name to check for instead of $command.
439#
440#	rcvar		n	This is checked with checkyesno to determine
441#				if the action should be run.
442#
443#	${name}_program	n	Full path to command.
444#				Meant to be used in /etc/rc.conf to override
445#				${command}.
446#
447#	${name}_chroot	n	Directory to chroot to before running ${command}
448#				Requires /usr to be mounted.
449#
450#	${name}_chdir	n	Directory to cd to before running ${command}
451#				(if not using ${name}_chroot).
452#
453#	${name}_flags	n	Arguments to call ${command} with.
454#				NOTE:	$flags from the parent environment
455#					can be used to override this.
456#
457#	${name}_nice	n	Nice level to run ${command} at.
458#
459#	${name}_user	n	User to run ${command} as, using su(1) if not
460#				using ${name}_chroot.
461#				Requires /usr to be mounted.
462#
463#	${name}_group	n	Group to run chrooted ${command} as.
464#				Requires /usr to be mounted.
465#
466#	${name}_groups	n	Comma separated list of supplementary groups
467#				to run the chrooted ${command} with.
468#				Requires /usr to be mounted.
469#
470#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
471#				Otherwise, use default command (see below)
472#
473#	${rc_arg}_precmd n	If set, run just before performing the
474#				${rc_arg}_cmd method in the default
475#				operation (i.e, after checking for required
476#				bits and process (non)existence).
477#				If this completes with a non-zero exit code,
478#				don't run ${rc_arg}_cmd.
479#
480#	${rc_arg}_postcmd n	If set, run just after performing the
481#				${rc_arg}_cmd method, if that method
482#				returned a zero exit code.
483#
484#	required_dirs	n	If set, check for the existence of the given
485#				directories before running a (re)start command.
486#
487#	required_files	n	If set, check for the readability of the given
488#				files before running a (re)start command.
489#
490#	required_modules n	If set, ensure the given kernel modules are
491#				loaded before running a (re)start command.
492#				The check and possible loads are actually
493#				done after start_precmd so that the modules
494#				aren't loaded in vain, should the precmd
495#				return a non-zero status to indicate a error.
496#				If a word in the list looks like "foo:bar",
497#				"foo" is the KLD file name and "bar" is the
498#				module name.  If a word looks like "foo~bar",
499#				"foo" is the KLD file name and "bar" is a
500#				egrep(1) pattern matching the module name.
501#				Otherwise the module name is assumed to be
502#				the same as the KLD file name, which is most
503#				common.  See load_kld().
504#
505#	required_vars	n	If set, perform checkyesno on each of the
506#				listed variables before running the default
507#				(re)start command.
508#
509#	Default behaviour for a given argument, if no override method is
510#	provided:
511#
512#	Argument	Default behaviour
513#	--------	-----------------
514#	start		if !running && checkyesno ${rcvar}
515#				${command}
516#
517#	stop		if ${pidfile}
518#				rc_pid=$(check_pidfile $pidfile $command)
519#			else
520#				rc_pid=$(check_process $command)
521#			kill $sig_stop $rc_pid
522#			wait_for_pids $rc_pid
523#			($sig_stop defaults to TERM.)
524#
525#	reload		Similar to stop, except use $sig_reload instead,
526#			and doesn't wait_for_pids.
527#			$sig_reload defaults to HUP.
528#			Note that `reload' isn't provided by default,
529#			it should be enabled via $extra_commands.
530#
531#	restart		Run `stop' then `start'.
532#
533#	status		Show if ${command} is running, etc.
534#
535#	poll		Wait for ${command} to exit.
536#
537#	rcvar		Display what rc.conf variable is used (if any).
538#
539#	Variables available to methods, and after run_rc_command() has
540#	completed:
541#
542#	Variable	Purpose
543#	--------	-------
544#	rc_arg		Argument to command, after fast/force/one processing
545#			performed
546#
547#	rc_flags	Flags to start the default command with.
548#			Defaults to ${name}_flags, unless overridden
549#			by $flags from the environment.
550#			This variable may be changed by the precmd method.
551#
552#	rc_pid		PID of command (if appropriate)
553#
554#	rc_fast		Not empty if "fast" was provided (q.v.)
555#
556#	rc_force	Not empty if "force" was provided (q.v.)
557#
558#	rc_quiet	Not empty if "quiet" was provided
559#
560#
561run_rc_command()
562{
563	_return=0
564	rc_arg=$1
565	if [ -z "$name" ]; then
566		err 3 'run_rc_command: $name is not set.'
567	fi
568
569	# Don't repeat the first argument when passing additional command-
570	# line arguments to the command subroutines.
571	#
572	shift 1
573	rc_extra_args="$*"
574
575	_rc_prefix=
576	case "$rc_arg" in
577	fast*)				# "fast" prefix; don't check pid
578		rc_arg=${rc_arg#fast}
579		rc_fast=yes
580		rc_quiet=yes
581		;;
582	force*)				# "force" prefix; always run
583		rc_force=yes
584		_rc_prefix=force
585		rc_arg=${rc_arg#${_rc_prefix}}
586		if [ -n "${rcvar}" ]; then
587			eval ${rcvar}=YES
588		fi
589		;;
590	one*)				# "one" prefix; set ${rcvar}=yes
591		_rc_prefix=one
592		rc_arg=${rc_arg#${_rc_prefix}}
593		if [ -n "${rcvar}" ]; then
594			eval ${rcvar}=YES
595		fi
596		;;
597	quiet*)				# "quiet" prefix; omit some messages
598		_rc_prefix=quiet
599		rc_arg=${rc_arg#${_rc_prefix}}
600		rc_quiet=yes
601		;;
602	esac
603
604	eval _override_command=\$${name}_program
605	command=${_override_command:-$command}
606
607	_keywords="start stop restart rcvar $extra_commands"
608	rc_pid=
609	_pidcmd=
610	_procname=${procname:-${command}}
611
612					# setup pid check command
613	if [ -n "$_procname" ]; then
614		if [ -n "$pidfile" ]; then
615			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
616		else
617			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
618		fi
619		if [ -n "$_pidcmd" ]; then
620			_keywords="${_keywords} status poll"
621		fi
622	fi
623
624	if [ -z "$rc_arg" ]; then
625		rc_usage $_keywords
626	fi
627
628	if [ -n "$flags" ]; then	# allow override from environment
629		rc_flags=$flags
630	else
631		eval rc_flags=\$${name}_flags
632	fi
633	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
634	    _nice=\$${name}_nice	_user=\$${name}_user \
635	    _group=\$${name}_group	_groups=\$${name}_groups
636
637	if [ -n "$_user" ]; then	# unset $_user if running as that user
638		if [ "$_user" = "$(eval $IDCMD)" ]; then
639			unset _user
640		fi
641	fi
642
643	eval $_pidcmd			# determine the pid if necessary
644
645	for _elem in $_keywords; do
646		if [ "$_elem" != "$rc_arg" ]; then
647			continue
648		fi
649					# if ${rcvar} is set, $1 is not "rcvar"
650					# and ${rc_pid} is not set, then run
651					#	checkyesno ${rcvar}
652					# and return if that failed
653					#
654		if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a -z "${rc_pid}" ]; then
655			if ! checkyesno ${rcvar}; then
656				if [ -n "${rc_quiet}" ]; then
657					return 0
658				fi
659				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
660				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
661				echo "instead of '${rc_arg}'."
662				return 0
663			fi
664		fi
665
666					# if there's a custom ${XXX_cmd},
667					# run that instead of the default
668					#
669		eval _cmd=\$${rc_arg}_cmd \
670		     _precmd=\$${rc_arg}_precmd \
671		     _postcmd=\$${rc_arg}_postcmd
672
673		if [ -n "$_cmd" ]; then
674			_run_rc_precmd || return 1
675			_run_rc_doit "$_cmd $rc_extra_args" || return 1
676			_run_rc_postcmd
677			return $_return
678		fi
679
680		case "$rc_arg" in	# default operations...
681
682		status)
683			_run_rc_precmd || return 1
684			if [ -n "$rc_pid" ]; then
685				echo "${name} is running as pid $rc_pid."
686			else
687				echo "${name} is not running."
688				return 1
689			fi
690			_run_rc_postcmd
691			;;
692
693		start)
694			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
695				echo 1>&2 "${name} already running? (pid=$rc_pid)."
696				return 1
697			fi
698
699			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
700				warn "run_rc_command: cannot run $command"
701				return 1
702			fi
703
704			if ! _run_rc_precmd; then
705				warn "failed precmd routine for ${name}"
706				return 1
707			fi
708
709					# setup the full command to run
710					#
711			check_startmsgs && echo "Starting ${name}."
712			if [ -n "$_chroot" ]; then
713				_doit="\
714${_nice:+nice -n $_nice }\
715chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
716$_chroot $command $rc_flags $command_args"
717			else
718				_doit="\
719${_chdir:+cd $_chdir && }\
720$command $rc_flags $command_args"
721				if [ -n "$_user" ]; then
722				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
723				fi
724				if [ -n "$_nice" ]; then
725					if [ -z "$_user" ]; then
726						_doit="sh -c \"$_doit\""
727					fi
728					_doit="nice -n $_nice $_doit"
729				fi
730			fi
731
732					# run the full command
733					#
734			if ! _run_rc_doit "$_doit"; then
735				warn "failed to start ${name}"
736				return 1
737			fi
738
739					# finally, run postcmd
740					#
741			_run_rc_postcmd
742			;;
743
744		stop)
745			if [ -z "$rc_pid" ]; then
746				[ -n "$rc_fast" ] && return 0
747				_run_rc_notrunning
748				return 1
749			fi
750
751			_run_rc_precmd || return 1
752
753					# send the signal to stop
754					#
755			echo "Stopping ${name}."
756			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
757			_run_rc_doit "$_doit" || return 1
758
759					# wait for the command to exit,
760					# and run postcmd.
761			wait_for_pids $rc_pid
762
763			_run_rc_postcmd
764			;;
765
766		reload)
767			if [ -z "$rc_pid" ]; then
768				_run_rc_notrunning
769				return 1
770			fi
771
772			_run_rc_precmd || return 1
773
774			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
775			_run_rc_doit "$_doit" || return 1
776
777			_run_rc_postcmd
778			;;
779
780		restart)
781					# prevent restart being called more
782					# than once by any given script
783					#
784			if ${_rc_restart_done:-false}; then
785				return 0
786			fi
787			_rc_restart_done=true
788
789			_run_rc_precmd || return 1
790
791			# run those in a subshell to keep global variables
792			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
793			( run_rc_command ${_rc_prefix}start $rc_extra_args )
794			_return=$?
795			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
796
797			_run_rc_postcmd
798			;;
799
800		poll)
801			_run_rc_precmd || return 1
802			if [ -n "$rc_pid" ]; then
803				wait_for_pids $rc_pid
804			fi
805			_run_rc_postcmd
806			;;
807
808		rcvar)
809			echo -n "# $name"
810			if [ -n "$desc" ]; then
811				echo " : $desc"
812			else
813				echo ""
814			fi
815			echo "#"
816			# Get unique vars in $rcvar $rcvars
817			for _v in $rcvar $rcvars; do
818				case $v in
819				$_v\ *|\ *$_v|*\ $_v\ *) ;;
820				*)	v="${v# } $_v" ;;
821				esac
822			done
823
824			# Display variables.
825			for _v in $v; do
826				if [ -z "$_v" ]; then
827					continue
828				fi
829
830				eval _desc=\$${_v}_desc
831				eval _defval=\$${_v}_defval
832				_h="-"
833
834				eval echo \"$_v=\\\"\$$_v\\\"\"
835				# decode multiple lines of _desc
836				while [ -n "$_desc" ]; do
837					case $_desc in
838					*^^*)
839						echo "# $_h ${_desc%%^^*}"
840						_desc=${_desc#*^^}
841						_h=" "
842						;;
843					*)
844						echo "# $_h ${_desc}"
845						break
846						;;
847					esac
848				done
849				echo "#   (default: \"$_defval\")"
850			done
851			echo ""
852			;;
853
854		*)
855			rc_usage $_keywords
856			;;
857
858		esac
859		return $_return
860	done
861
862	echo 1>&2 "$0: unknown directive '$rc_arg'."
863	rc_usage $_keywords
864	# not reached
865}
866
867#
868# Helper functions for run_rc_command: common code.
869# They use such global variables besides the exported rc_* ones:
870#
871#	name	       R/W
872#	------------------
873#	_precmd		R
874#	_postcmd	R
875#	_return		W
876#
877_run_rc_precmd()
878{
879	check_required_before "$rc_arg" || return 1
880
881	if [ -n "$_precmd" ]; then
882		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
883		eval "$_precmd $rc_extra_args"
884		_return=$?
885
886		# If precmd failed and force isn't set, request exit.
887		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
888			return 1
889		fi
890	fi
891
892	check_required_after "$rc_arg" || return 1
893
894	return 0
895}
896
897_run_rc_postcmd()
898{
899	if [ -n "$_postcmd" ]; then
900		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
901		eval "$_postcmd $rc_extra_args"
902		_return=$?
903	fi
904	return 0
905}
906
907_run_rc_doit()
908{
909	debug "run_rc_command: doit: $*"
910	eval "$@"
911	_return=$?
912
913	# If command failed and force isn't set, request exit.
914	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
915		return 1
916	fi
917
918	return 0
919}
920
921_run_rc_notrunning()
922{
923	local _pidmsg
924
925	if [ -n "$pidfile" ]; then
926		_pidmsg=" (check $pidfile)."
927	else
928		_pidmsg=
929	fi
930	echo 1>&2 "${name} not running?${_pidmsg}"
931}
932
933_run_rc_killcmd()
934{
935	local _cmd
936
937	_cmd="kill -$1 $rc_pid"
938	if [ -n "$_user" ]; then
939		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
940	fi
941	echo "$_cmd"
942}
943
944#
945# run_rc_script file arg
946#	Start the script `file' with `arg', and correctly handle the
947#	return value from the script.
948#	If `file' ends with `.sh', it's sourced into the current environment
949#	when $rc_fast_and_loose is set, otherwise it is run as a child process.
950#	If `file' appears to be a backup or scratch file, ignore it.
951#	Otherwise if it is executable run as a child process.
952#
953run_rc_script()
954{
955	_file=$1
956	_arg=$2
957	if [ -z "$_file" -o -z "$_arg" ]; then
958		err 3 'USAGE: run_rc_script file arg'
959	fi
960
961	unset	name command command_args command_interpreter \
962		extra_commands pidfile procname \
963		rcvar rcvars rcvars_obsolete required_dirs required_files \
964		required_vars
965	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
966
967	case "$_file" in
968	/etc/rc.d/*.sh)			# no longer allowed in the base
969		warn "Ignoring old-style startup script $_file"
970		;;
971	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
972		warn "Ignoring scratch file $_file"
973		;;
974	*)				# run in subshell
975		if [ -x $_file ]; then
976			if [ -n "$rc_fast_and_loose" ]; then
977				set $_arg; . $_file
978			else
979				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
980				  trap "echo Script $_file interrupted; exit 1" 2
981				  trap "echo Script $_file running" 29
982				  set $_arg; . $_file )
983			fi
984		fi
985		;;
986	esac
987}
988
989#
990# load_rc_config name
991#	Source in the configuration file for a given name.
992#
993load_rc_config()
994{
995	local _name _var _defval _v _msg _new
996	_name=$1
997	if [ -z "$_name" ]; then
998		err 3 'USAGE: load_rc_config name'
999	fi
1000
1001	if ${_rc_conf_loaded:-false}; then
1002		:
1003	else
1004		if [ -r /etc/defaults/rc.conf ]; then
1005			debug "Sourcing /etc/defaults/rc.conf"
1006			. /etc/defaults/rc.conf
1007			source_rc_confs
1008		elif [ -r /etc/rc.conf ]; then
1009			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1010			. /etc/rc.conf
1011		fi
1012		_rc_conf_loaded=true
1013	fi
1014	if [ -f /etc/rc.conf.d/"$_name" ]; then
1015		debug "Sourcing /etc/rc.conf.d/${_name}"
1016		. /etc/rc.conf.d/"$_name"
1017	fi
1018
1019	# Old variable name support -- Remove before 9.0-RELEASE
1020	#
1021	[ -n "$enable_quotas" ] && err 1 "enable_quotas is deprecated, use quota_enable"
1022
1023	# Set defaults if defined.
1024	for _var in $rcvar $rcvars; do
1025		_defval=`eval echo "\\\$${_var}_defval"`
1026		if [ -n "$_defval" ]; then
1027			eval : \${$_var:=\$${_var}_defval}
1028		fi
1029	done
1030
1031	# check obsolete rc.conf variables
1032	for _var in $rcvars_obsolete; do
1033		_v=`eval echo \\$$_var`
1034		_msg=`eval echo \\$${_var}_obsolete_msg`
1035		_new=`eval echo \\$${_var}_newvar`
1036		case $_v in
1037		"")
1038			;;
1039		*)
1040			if [ -z "$_new" ]; then
1041				_msg="Ignored."
1042			else
1043				eval $_new=\"\$$_var\"
1044				if [ -z "$_msg" ]; then
1045					_msg="Use \$$_new instead."
1046				fi
1047			fi
1048			warn "\$$_var is obsolete.  $_msg"
1049			;;
1050		esac
1051	done
1052}
1053
1054#
1055# load_rc_config_var name var
1056#	Read the rc.conf(5) var for name and set in the
1057#	current shell, using load_rc_config in a subshell to prevent
1058#	unwanted side effects from other variable assignments.
1059#
1060load_rc_config_var()
1061{
1062	if [ $# -ne 2 ]; then
1063		err 3 'USAGE: load_rc_config_var name var'
1064	fi
1065	eval $(eval '(
1066		load_rc_config '$1' >/dev/null;
1067                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1068			echo '$2'=\'\''${'$2'}\'\'';
1069		fi
1070	)' )
1071}
1072
1073#
1074# rc_usage commands
1075#	Print a usage string for $0, with `commands' being a list of
1076#	valid commands.
1077#
1078rc_usage()
1079{
1080	echo -n 1>&2 "Usage: $0 [fast|force|one]("
1081
1082	_sep=
1083	for _elem; do
1084		echo -n 1>&2 "$_sep$_elem"
1085		_sep="|"
1086	done
1087	echo 1>&2 ")"
1088	exit 1
1089}
1090
1091#
1092# err exitval message
1093#	Display message to stderr and log to the syslog, and exit with exitval.
1094#
1095err()
1096{
1097	exitval=$1
1098	shift
1099
1100	if [ -x /usr/bin/logger ]; then
1101		logger "$0: ERROR: $*"
1102	fi
1103	echo 1>&2 "$0: ERROR: $*"
1104	exit $exitval
1105}
1106
1107#
1108# warn message
1109#	Display message to stderr and log to the syslog.
1110#
1111warn()
1112{
1113	if [ -x /usr/bin/logger ]; then
1114		logger "$0: WARNING: $*"
1115	fi
1116	echo 1>&2 "$0: WARNING: $*"
1117}
1118
1119#
1120# info message
1121#	Display informational message to stdout and log to syslog.
1122#
1123info()
1124{
1125	case ${rc_info} in
1126	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1127		if [ -x /usr/bin/logger ]; then
1128			logger "$0: INFO: $*"
1129		fi
1130		echo "$0: INFO: $*"
1131		;;
1132	esac
1133}
1134
1135#
1136# debug message
1137#	If debugging is enabled in rc.conf output message to stderr.
1138#	BEWARE that you don't call any subroutine that itself calls this
1139#	function.
1140#
1141debug()
1142{
1143	case ${rc_debug} in
1144	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1145		if [ -x /usr/bin/logger ]; then
1146			logger "$0: DEBUG: $*"
1147		fi
1148		echo 1>&2 "$0: DEBUG: $*"
1149		;;
1150	esac
1151}
1152
1153#
1154# backup_file action file cur backup
1155#	Make a backup copy of `file' into `cur', and save the previous
1156#	version of `cur' as `backup' or use rcs for archiving.
1157#
1158#	This routine checks the value of the backup_uses_rcs variable,
1159#	which can be either YES or NO.
1160#
1161#	The `action' keyword can be one of the following:
1162#
1163#	add		`file' is now being backed up (and is possibly
1164#			being reentered into the backups system).  `cur'
1165#			is created and RCS files, if necessary, are
1166#			created as well.
1167#
1168#	update		`file' has changed and needs to be backed up.
1169#			If `cur' exists, it is copied to to `back' or
1170#			checked into RCS (if the repository file is old),
1171#			and then `file' is copied to `cur'.  Another RCS
1172#			check in done here if RCS is being used.
1173#
1174#	remove		`file' is no longer being tracked by the backups
1175#			system.  If RCS is not being used, `cur' is moved
1176#			to `back', otherwise an empty file is checked in,
1177#			and then `cur' is removed.
1178#
1179#
1180backup_file()
1181{
1182	_action=$1
1183	_file=$2
1184	_cur=$3
1185	_back=$4
1186
1187	if checkyesno backup_uses_rcs; then
1188		_msg0="backup archive"
1189		_msg1="update"
1190
1191		# ensure that history file is not locked
1192		if [ -f $_cur,v ]; then
1193			rcs -q -u -U -M $_cur
1194		fi
1195
1196		# ensure after switching to rcs that the
1197		# current backup is not lost
1198		if [ -f $_cur ]; then
1199			# no archive, or current newer than archive
1200			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1201				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1202				rcs -q -kb -U $_cur
1203				co -q -f -u $_cur
1204			fi
1205		fi
1206
1207		case $_action in
1208		add|update)
1209			cp -p $_file $_cur
1210			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1211			rcs -q -kb -U $_cur
1212			co -q -f -u $_cur
1213			chown root:wheel $_cur $_cur,v
1214			;;
1215		remove)
1216			cp /dev/null $_cur
1217			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1218			rcs -q -kb -U $_cur
1219			chown root:wheel $_cur $_cur,v
1220			rm $_cur
1221			;;
1222		esac
1223	else
1224		case $_action in
1225		add|update)
1226			if [ -f $_cur ]; then
1227				cp -p $_cur $_back
1228			fi
1229			cp -p $_file $_cur
1230			chown root:wheel $_cur
1231			;;
1232		remove)
1233			mv -f $_cur $_back
1234			;;
1235		esac
1236	fi
1237}
1238
1239# make_symlink src link
1240#	Make a symbolic link 'link' to src from basedir. If the
1241#	directory in which link is to be created does not exist
1242#	a warning will be displayed and an error will be returned.
1243#	Returns 0 on sucess, 1 otherwise.
1244#
1245make_symlink()
1246{
1247	local src link linkdir _me
1248	src="$1"
1249	link="$2"
1250	linkdir="`dirname $link`"
1251	_me="make_symlink()"
1252
1253	if [ -z "$src" -o -z "$link" ]; then
1254		warn "$_me: requires two arguments."
1255		return 1
1256	fi
1257	if [ ! -d "$linkdir" ]; then
1258		warn "$_me: the directory $linkdir does not exist."
1259		return 1
1260	fi
1261	if ! ln -sf $src $link; then
1262		warn "$_me: unable to make a symbolic link from $link to $src"
1263		return 1
1264	fi
1265	return 0
1266}
1267
1268# devfs_rulesets_from_file file
1269#	Reads a set of devfs commands from file, and creates
1270#	the specified rulesets with their rules. Returns non-zero
1271#	if there was an error.
1272#
1273devfs_rulesets_from_file()
1274{
1275	local file _err _me
1276	file="$1"
1277	_me="devfs_rulesets_from_file"
1278	_err=0
1279
1280	if [ -z "$file" ]; then
1281		warn "$_me: you must specify a file"
1282		return 1
1283	fi
1284	if [ ! -e "$file" ]; then
1285		debug "$_me: no such file ($file)"
1286		return 0
1287	fi
1288	debug "reading rulesets from file ($file)"
1289	{ while read line
1290	do
1291		case $line in
1292		\#*)
1293			continue
1294			;;
1295		\[*\]*)
1296			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1297			if [ -z "$rulenum" ]; then
1298				warn "$_me: cannot extract rule number ($line)"
1299				_err=1
1300				break
1301			fi
1302			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1303			if [ -z "$rulename" ]; then
1304				warn "$_me: cannot extract rule name ($line)"
1305				_err=1
1306				break;
1307			fi
1308			eval $rulename=\$rulenum
1309			debug "found ruleset: $rulename=$rulenum"
1310			if ! /sbin/devfs rule -s $rulenum delset; then
1311				_err=1
1312				break
1313			fi
1314			;;
1315		*)
1316			rulecmd="${line%%"\#*"}"
1317			# evaluate the command incase it includes
1318			# other rules
1319			if [ -n "$rulecmd" ]; then
1320				debug "adding rule ($rulecmd)"
1321				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1322				then
1323					_err=1
1324					break
1325				fi
1326			fi
1327			;;
1328		esac
1329		if [ $_err -ne 0 ]; then
1330			debug "error in $_me"
1331			break
1332		fi
1333	done } < $file
1334	return $_err
1335}
1336
1337# devfs_init_rulesets
1338#	Initializes rulesets from configuration files. Returns
1339#	non-zero if there was an error.
1340#
1341devfs_init_rulesets()
1342{
1343	local file _me
1344	_me="devfs_init_rulesets"
1345
1346	# Go through this only once
1347	if [ -n "$devfs_rulesets_init" ]; then
1348		debug "$_me: devfs rulesets already initialized"
1349		return
1350	fi
1351	for file in $devfs_rulesets; do
1352		if ! devfs_rulesets_from_file $file; then
1353			warn "$_me: could not read rules from $file"
1354			return 1
1355		fi
1356	done
1357	devfs_rulesets_init=1
1358	debug "$_me: devfs rulesets initialized"
1359	return 0
1360}
1361
1362# devfs_set_ruleset ruleset [dir]
1363#	Sets the default ruleset of dir to ruleset. The ruleset argument
1364#	must be a ruleset name as specified in devfs.rules(5) file.
1365#	Returns non-zero if it could not set it successfully.
1366#
1367devfs_set_ruleset()
1368{
1369	local devdir rs _me
1370	[ -n "$1" ] && eval rs=\$$1 || rs=
1371	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1372	_me="devfs_set_ruleset"
1373
1374	if [ -z "$rs" ]; then
1375		warn "$_me: you must specify a ruleset number"
1376		return 1
1377	fi
1378	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1379	if ! /sbin/devfs $devdir ruleset $rs; then
1380		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1381		return 1
1382	fi
1383	return 0
1384}
1385
1386# devfs_apply_ruleset ruleset [dir]
1387#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1388#	The ruleset argument must be a ruleset name as specified
1389#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1390#	if it could not apply the ruleset.
1391#
1392devfs_apply_ruleset()
1393{
1394	local devdir rs _me
1395	[ -n "$1" ] && eval rs=\$$1 || rs=
1396	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1397	_me="devfs_apply_ruleset"
1398
1399	if [ -z "$rs" ]; then
1400		warn "$_me: you must specify a ruleset"
1401		return 1
1402	fi
1403	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1404	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1405		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1406		return 1
1407	fi
1408	return 0
1409}
1410
1411# devfs_domount dir [ruleset]
1412#	Mount devfs on dir. If ruleset is specified it is set
1413#	on the mount-point. It must also be a ruleset name as specified
1414#	in a devfs.rules(5) file. Returns 0 on success.
1415#
1416devfs_domount()
1417{
1418	local devdir rs _me
1419	devdir="$1"
1420	[ -n "$2" ] && rs=$2 || rs=
1421	_me="devfs_domount()"
1422
1423	if [ -z "$devdir" ]; then
1424		warn "$_me: you must specify a mount-point"
1425		return 1
1426	fi
1427	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1428	if ! mount -t devfs dev "$devdir"; then
1429		warn "$_me: Unable to mount devfs on $devdir"
1430		return 1
1431	fi
1432	if [ -n "$rs" ]; then
1433		devfs_init_rulesets
1434		devfs_set_ruleset $rs $devdir
1435		devfs -m $devdir rule applyset
1436	fi
1437	return 0
1438}
1439
1440# devfs_mount_jail dir [ruleset]
1441#	Mounts a devfs file system appropriate for jails
1442#	on the directory dir. If ruleset is specified, the ruleset
1443#	it names will be used instead.  If present, ruleset must
1444#	be the name of a ruleset as defined in a devfs.rules(5) file.
1445#	This function returns non-zero if an error occurs.
1446#
1447devfs_mount_jail()
1448{
1449	local jdev rs _me
1450	jdev="$1"
1451	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1452	_me="devfs_mount_jail"
1453
1454	devfs_init_rulesets
1455	if ! devfs_domount "$jdev" $rs; then
1456		warn "$_me: devfs was not mounted on $jdev"
1457		return 1
1458	fi
1459	return 0
1460}
1461
1462# Provide a function for normalizing the mounting of memory
1463# filesystems.  This should allow the rest of the code here to remain
1464# as close as possible between 5-current and 4-stable.
1465#   $1 = size
1466#   $2 = mount point
1467#   $3 = (optional) extra mdmfs flags
1468mount_md()
1469{
1470	if [ -n "$3" ]; then
1471		flags="$3"
1472	fi
1473	/sbin/mdmfs $flags -s $1 md $2
1474}
1475
1476# Code common to scripts that need to load a kernel module
1477# if it isn't in the kernel yet. Syntax:
1478#   load_kld [-e regex] [-m module] file
1479# where -e or -m chooses the way to check if the module
1480# is already loaded:
1481#   regex is egrep'd in the output from `kldstat -v',
1482#   module is passed to `kldstat -m'.
1483# The default way is as though `-m file' were specified.
1484load_kld()
1485{
1486	local _loaded _mod _opt _re
1487
1488	while getopts "e:m:" _opt; do
1489		case "$_opt" in
1490		e) _re="$OPTARG" ;;
1491		m) _mod="$OPTARG" ;;
1492		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1493		esac
1494	done
1495	shift $(($OPTIND - 1))
1496	if [ $# -ne 1 ]; then
1497		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1498	fi
1499	_mod=${_mod:-$1}
1500	_loaded=false
1501	if [ -n "$_re" ]; then
1502		if kldstat -v | egrep -q -e "$_re"; then
1503			_loaded=true
1504		fi
1505	else
1506		if kldstat -q -m "$_mod"; then
1507			_loaded=true
1508		fi
1509	fi
1510	if ! $_loaded; then
1511		if ! kldload "$1"; then
1512			warn "Unable to load kernel module $1"
1513			return 1
1514		else
1515			info "$1 kernel module loaded."
1516		fi
1517	else
1518		debug "load_kld: $1 kernel module already loaded."
1519	fi
1520	return 0
1521}
1522
1523# ltr str src dst
1524#	Change every $src in $str to $dst.
1525#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1526#	awk(1).
1527ltr()
1528{
1529	local _str _src _dst _out _com
1530	_str=$1
1531	_src=$2
1532	_dst=$3
1533	_out=""
1534
1535	IFS=${_src}
1536	for _com in ${_str}; do
1537		if [ -z "${_out}" ]; then
1538			_out="${_com}"
1539		else
1540			_out="${_out}${_dst}${_com}"
1541		fi
1542	done
1543	echo "${_out}"
1544}
1545
1546# Creates a list of providers for GELI encryption.
1547geli_make_list()
1548{
1549	local devices devices2
1550	local provider mountpoint type options rest
1551
1552	# Create list of GELI providers from fstab.
1553	while read provider mountpoint type options rest ; do
1554		case ":${options}" in
1555		:*noauto*)
1556			noauto=yes
1557			;;
1558		*)
1559			noauto=no
1560			;;
1561		esac
1562
1563		case ":${provider}" in
1564		:#*)
1565			continue
1566			;;
1567		*.eli)
1568			# Skip swap devices.
1569			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1570				continue
1571			fi
1572			devices="${devices} ${provider}"
1573			;;
1574		esac
1575	done < /etc/fstab
1576
1577	# Append providers from geli_devices.
1578	devices="${devices} ${geli_devices}"
1579
1580	for provider in ${devices}; do
1581		provider=${provider%.eli}
1582		provider=${provider#/dev/}
1583		devices2="${devices2} ${provider}"
1584	done
1585
1586	echo ${devices2}
1587}
1588
1589# Find scripts in local_startup directories that use the old syntax
1590#
1591find_local_scripts_old () {
1592	zlist=''
1593	slist=''
1594	for dir in ${local_startup}; do
1595		if [ -d "${dir}" ]; then
1596			for file in ${dir}/[0-9]*.sh; do
1597				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1598				    continue
1599				zlist="$zlist $file"
1600			done
1601			for file in ${dir}/[^0-9]*.sh; do
1602				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1603				    continue
1604				slist="$slist $file"
1605			done
1606		fi
1607	done
1608}
1609
1610find_local_scripts_new () {
1611	local_rc=''
1612	for dir in ${local_startup}; do
1613		if [ -d "${dir}" ]; then
1614			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1615				case "$file" in
1616				*.sample) ;;
1617				*)	if [ -x "$file" ]; then
1618						local_rc="${local_rc} ${file}"
1619					fi
1620					;;
1621				esac
1622			done
1623		fi
1624	done
1625}
1626
1627# check_required_{before|after} command
1628#	Check for things required by the command before and after its precmd,
1629#	respectively.  The two separate functions are needed because some
1630#	conditions should prevent precmd from being run while other things
1631#	depend on precmd having already been run.
1632#
1633check_required_before()
1634{
1635	local _f
1636
1637	case "$1" in
1638	start)
1639		for _f in $required_vars; do
1640			if ! checkyesno $_f; then
1641				warn "\$${_f} is not enabled."
1642				if [ -z "$rc_force" ]; then
1643					return 1
1644				fi
1645			fi
1646		done
1647
1648		for _f in $required_dirs; do
1649			if [ ! -d "${_f}/." ]; then
1650				warn "${_f} is not a directory."
1651				if [ -z "$rc_force" ]; then
1652					return 1
1653				fi
1654			fi
1655		done
1656
1657		for _f in $required_files; do
1658			if [ ! -r "${_f}" ]; then
1659				warn "${_f} is not readable."
1660				if [ -z "$rc_force" ]; then
1661					return 1
1662				fi
1663			fi
1664		done
1665		;;
1666	esac
1667
1668	return 0
1669}
1670
1671check_required_after()
1672{
1673	local _f _args
1674
1675	case "$1" in
1676	start)
1677		for _f in $required_modules; do
1678			case "${_f}" in
1679				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1680				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1681				*)	_args="${_f}" ;;
1682			esac
1683			if ! load_kld ${_args}; then
1684				if [ -z "$rc_force" ]; then
1685					return 1
1686				fi
1687			fi
1688		done
1689		;;
1690	esac
1691
1692	return 0
1693}
1694
1695fi
1696
1697# _echoonce var msg mode
1698#	mode=0: Echo $msg if ${$var} is empty.
1699#	        After doing echo, a string is set to ${$var}.
1700#
1701#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
1702#
1703_echoonce()
1704{
1705	local _var _msg _mode
1706	_var=`eval echo \\$$1`
1707	_msg=$2
1708	_mode=$3
1709
1710	case $_mode in
1711	1)	[ -n "$_var" ] && echo "$_msg" ;;
1712	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1713	esac
1714}
1715
1716_rc_subr_loaded=:
1717