rc.subr revision 222996
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 222996 2011-06-11 21:40:37Z 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#
1978344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2078344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2178344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2278344Sobrien# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2378344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2478344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2578344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2678344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2778344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2878344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2978344Sobrien# POSSIBILITY OF SUCH DAMAGE.
3078344Sobrien#
3178344Sobrien# rc.subr
3278344Sobrien#	functions used by various rc scripts
3378344Sobrien#
3478344Sobrien
35157473Sflz: ${rcvar_manpage:='rc.conf(5)'}
36169668Smtm: ${RC_PID:=$$}; export RC_PID
37157473Sflz
3878344Sobrien#
3998186Sgordon#	Operating System dependent/independent variables
4098186Sgordon#
4198186Sgordon
42131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then
43131550Scperciva
44131550Scperciva_rc_subr_loaded="YES"
45131550Scperciva
4698186SgordonSYSCTL="/sbin/sysctl"
4798186SgordonSYSCTL_N="${SYSCTL} -n"
48202988SemasteSYSCTL_W="${SYSCTL}"
49124832SmtmID="/usr/bin/id"
50124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
51161435SyarPS="/bin/ps -ww"
52161435SyarJID=`$PS -p $$ -o jid=`
5398186Sgordon
5498186Sgordon#
5578344Sobrien#	functions
5678344Sobrien#	---------
5778344Sobrien
58197144Shrs# set_rcvar [var] [defval] [desc]
5978344Sobrien#
60197144Shrs#	Echo or define a rc.conf(5) variable name.  Global variable
61197144Shrs#	$rcvars is used.
6298186Sgordon#
63197144Shrs#	If no argument is specified, echo "${name}_enable".
64197144Shrs#
65197144Shrs#	If only a var is specified, echo "${var}_enable".
66197144Shrs#
67197144Shrs#	If var and defval are specified, the ${var} is defined as
68197144Shrs#	rc.conf(5) variable and the default value is ${defvar}.  An
69197144Shrs#	optional argument $desc can also be specified to add a
70197144Shrs#	description for that.
71197144Shrs#
7298186Sgordonset_rcvar()
7398186Sgordon{
74197144Shrs	case $# in
75197144Shrs	0)
76197144Shrs		echo ${name}_enable
7798186Sgordon		;;
78197144Shrs	1)
79197144Shrs		echo ${1}_enable
8098186Sgordon		;;
8198186Sgordon	*)
82197144Shrs		debug "rcvar_define: \$$1=$2 is added" \
83197144Shrs		    " as a rc.conf(5) variable."
84197144Shrs
85197144Shrs		local _var
86197144Shrs		_var=$1
87197144Shrs		rcvars="${rcvars# } $_var"
88197144Shrs		eval ${_var}_defval=\"$2\"
89197144Shrs		shift 2
90197144Shrs		# encode multiple lines of _desc
91197144Shrs		for l in "$@"; do
92197144Shrs			eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
93197144Shrs		done
94197144Shrs		eval ${_var}_desc=\"\${${_var}_desc#^^}\"
9598186Sgordon		;;
9698186Sgordon	esac
9798186Sgordon}
9898186Sgordon
99197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg]
100197144Shrs#	Define obsolete variable.
101197144Shrs#	Global variable $rcvars_obsolete is used.
10298186Sgordon#
103197144Shrsset_rcvar_obsolete()
104197144Shrs{
105197144Shrs	local _var
106197144Shrs	_var=$1
107197144Shrs	debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
108197144Shrs
109197144Shrs	rcvars_obsolete="${rcvars_obsolete# } $1"
110197144Shrs	eval ${1}_newvar=\"$2\"
111197144Shrs	shift 2
112197144Shrs	eval ${_var}_obsolete_msg=\"$*\"
113197144Shrs}
114197144Shrs
115197144Shrs#
11698186Sgordon# force_depend script
11798186Sgordon#	Force a service to start. Intended for use by services
11898186Sgordon#	to resolve dependency issues. It is assumed the caller
11998186Sgordon#	has check to make sure this call is necessary
12098186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
12198186Sgordon#
12298186Sgordonforce_depend()
12398186Sgordon{
12498186Sgordon	_depend="$1"
12598186Sgordon
12698186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
127146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
12898186Sgordon		warn "Unable to force ${_depend}. It may already be running."
12998186Sgordon		return 1
13098186Sgordon	fi
13198186Sgordon	return 0
13298186Sgordon}
13398186Sgordon
13498186Sgordon#
13578344Sobrien# checkyesno var
13678344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
13778344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
13878344Sobrien#
13978344Sobriencheckyesno()
14078344Sobrien{
14178344Sobrien	eval _value=\$${1}
14298186Sgordon	debug "checkyesno: $1 is set to $_value."
14378344Sobrien	case $_value in
14478344Sobrien
14578344Sobrien		#	"yes", "true", "on", or "1"
14678344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
14778344Sobrien		return 0
14878344Sobrien		;;
14978344Sobrien
15078344Sobrien		#	"no", "false", "off", or "0"
15178344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
15278344Sobrien		return 1
15378344Sobrien		;;
15478344Sobrien	*)
155157473Sflz		warn "\$${1} is not set properly - see ${rcvar_manpage}."
15678344Sobrien		return 1
15778344Sobrien		;;
15878344Sobrien	esac
15978344Sobrien}
16078344Sobrien
161157473Sflz#
16298186Sgordon# reverse_list list
16398186Sgordon#	print the list in reverse order
16478344Sobrien#
16598186Sgordonreverse_list()
16698186Sgordon{
16798186Sgordon	_revlist=
168126286Smtm	for _revfile; do
16998186Sgordon		_revlist="$_revfile $_revlist"
17098186Sgordon	done
17198186Sgordon	echo $_revlist
17298186Sgordon}
17398186Sgordon
174169668Smtm# stop_boot always
175169668Smtm#	If booting directly to multiuser or $always is enabled,
176169668Smtm#	send SIGTERM to the parent (/etc/rc) to abort the boot.
177169668Smtm#	Otherwise just exit.
17878344Sobrien#
179169668Smtmstop_boot()
180169668Smtm{
181169668Smtm	local always
182169668Smtm
183178776Smaxim	case $1 in
184178776Smaxim		#	"yes", "true", "on", or "1"
185178770Smtm        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
186169668Smtm		always=true
187178770Smtm		;;
188178770Smtm	*)
189169668Smtm		always=false
190178770Smtm		;;
191178775Smaxim	esac
192169668Smtm	if [ "$autoboot" = yes -o "$always" = true ]; then
193169668Smtm		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
194169668Smtm		kill -TERM ${RC_PID}
195169668Smtm	fi
196169668Smtm	exit 1
197169668Smtm}
198169668Smtm
199169668Smtm#
20098186Sgordon# mount_critical_filesystems type
20198186Sgordon#	Go through the list of critical filesystems as provided in
20298186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
20398186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
20498186Sgordon#
20578344Sobrienmount_critical_filesystems()
20678344Sobrien{
20798186Sgordon	eval _fslist=\$critical_filesystems_${1}
20878344Sobrien	for _fs in $_fslist; do
20978344Sobrien		mount | (
210126285Smtm			_ismounted=false
21178344Sobrien			while read what _on on _type type; do
21278344Sobrien				if [ $on = $_fs ]; then
213126285Smtm					_ismounted=true
21478344Sobrien				fi
21578344Sobrien			done
216126285Smtm			if $_ismounted; then
217126285Smtm				:
218126285Smtm			else
21978344Sobrien				mount $_fs >/dev/null 2>&1
22078344Sobrien			fi
22198186Sgordon		)
22278344Sobrien	done
22378344Sobrien}
22478344Sobrien
22578344Sobrien#
22698186Sgordon# check_pidfile pidfile procname [interpreter]
22798186Sgordon#	Parses the first line of pidfile for a PID, and ensures
22878344Sobrien#	that the process is running and matches procname.
22998186Sgordon#	Prints the matching PID upon success, nothing otherwise.
23098186Sgordon#	interpreter is optional; see _find_processes() for details.
23178344Sobrien#
23278344Sobriencheck_pidfile()
23378344Sobrien{
23478344Sobrien	_pidfile=$1
23578344Sobrien	_procname=$2
23698186Sgordon	_interpreter=$3
23778344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
23898186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
23978344Sobrien	fi
24078344Sobrien	if [ ! -f $_pidfile ]; then
241131061Smtm		debug "pid file ($_pidfile): not readable."
24278344Sobrien		return
24378344Sobrien	fi
24478344Sobrien	read _pid _junk < $_pidfile
24578344Sobrien	if [ -z "$_pid" ]; then
246139949Skeramida		debug "pid file ($_pidfile): no pid in file."
24778344Sobrien		return
24878344Sobrien	fi
24998186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
25078344Sobrien}
25178344Sobrien
25278344Sobrien#
25398186Sgordon# check_process procname [interpreter]
25478344Sobrien#	Ensures that a process (or processes) named procname is running.
25598186Sgordon#	Prints a list of matching PIDs.
25698186Sgordon#	interpreter is optional; see _find_processes() for details.
25778344Sobrien#
25878344Sobriencheck_process()
25978344Sobrien{
26078344Sobrien	_procname=$1
26198186Sgordon	_interpreter=$2
26278344Sobrien	if [ -z "$_procname" ]; then
26398186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
26478344Sobrien	fi
26598186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
26698186Sgordon}
26798186Sgordon
26898186Sgordon#
26998186Sgordon# _find_processes procname interpreter psargs
27098186Sgordon#	Search for procname in the output of ps generated by psargs.
27198186Sgordon#	Prints the PIDs of any matching processes, space separated.
27298186Sgordon#
27398186Sgordon#	If interpreter == ".", check the following variations of procname
27498186Sgordon#	against the first word of each command:
27598186Sgordon#		procname
27698186Sgordon#		`basename procname`
27798186Sgordon#		`basename procname` + ":"
27898186Sgordon#		"(" + `basename procname` + ")"
279155719Sceri#		"[" + `basename procname` + "]"
28098186Sgordon#
28198186Sgordon#	If interpreter != ".", read the first line of procname, remove the
28298186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
28398186Sgordon#	match that against each command, either as is, or with extra words
284157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
285157841Sflz#	using perl, the basename of the interpreter plus a colon is also
286157841Sflz#	tried as the prefix to procname.
28798186Sgordon#
28898186Sgordon_find_processes()
28998186Sgordon{
29098186Sgordon	if [ $# -ne 3 ]; then
29198186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
29298186Sgordon	fi
29398186Sgordon	_procname=$1
29498186Sgordon	_interpreter=$2
29598186Sgordon	_psargs=$3
29698186Sgordon
29778344Sobrien	_pref=
29898186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
299170282Syar		_script=${_chroot}${_chroot:+"/"}$_procname
300170282Syar		if [ -r $_script ]; then
301170282Syar			read _interp < $_script	# read interpreter name
302170282Syar			case "$_interp" in
303170282Syar			\#!*)
304170282Syar				_interp=${_interp#\#!}	# strip #!
305170282Syar				set -- $_interp
306170282Syar				case $1 in
307170282Syar				*/bin/env)
308170282Syar					shift	# drop env to get real name
309170282Syar					;;
310170282Syar				esac
311170282Syar				if [ $_interpreter != $1 ]; then
312170282Syar					warn "\$command_interpreter $_interpreter != $1"
313170282Syar				fi
314170282Syar				;;
315170282Syar			*)
316170282Syar				warn "no shebang line in $_script"
317170282Syar				set -- $_interpreter
318170282Syar				;;
319170282Syar			esac
320170282Syar		else
321170282Syar			warn "cannot read shebang line from $_script"
322170282Syar			set -- $_interpreter
32378344Sobrien		fi
32498186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
325157841Sflz		_interpbn=${1##*/}
32698186Sgordon		_fp_args='_argv'
32798186Sgordon		_fp_match='case "$_argv" in
328157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
32998186Sgordon	else					# a normal daemon
33098186Sgordon		_procnamebn=${_procname##*/}
33198186Sgordon		_fp_args='_arg0 _argv'
33298186Sgordon		_fp_match='case "$_arg0" in
333151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
33498186Sgordon	fi
33598186Sgordon
336161435Syar	_proccheck="\
337161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
338157657Sflz		while read _npid _jid '"$_fp_args"'; do
339161436Syar			'"$_fp_match"'
340157657Sflz				if [ "$JID" -eq "$_jid" ];
341157657Sflz				then echo -n "$_pref$_npid";
342157657Sflz				_pref=" ";
343157657Sflz				fi
34498186Sgordon				;;
34598186Sgordon			esac
34698186Sgordon		done'
34798186Sgordon
348114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
34998186Sgordon	eval $_proccheck
35098186Sgordon}
35198186Sgordon
35298186Sgordon#
35398186Sgordon# wait_for_pids pid [pid ...]
35498186Sgordon#	spins until none of the pids exist
35598186Sgordon#
35698186Sgordonwait_for_pids()
35798186Sgordon{
358206248Sdougb	local _list _prefix _nlist _j
359206248Sdougb
360126286Smtm	_list="$@"
36198186Sgordon	if [ -z "$_list" ]; then
36298186Sgordon		return
36398186Sgordon	fi
36498186Sgordon	_prefix=
36598186Sgordon	while true; do
36698186Sgordon		_nlist="";
36798186Sgordon		for _j in $_list; do
36898186Sgordon			if kill -0 $_j 2>/dev/null; then
36998186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
370206248Sdougb				[ -n "$_prefix" ] && sleep 1
37198186Sgordon			fi
37298186Sgordon		done
37398186Sgordon		if [ -z "$_nlist" ]; then
37498186Sgordon			break
37578344Sobrien		fi
37698186Sgordon		_list=$_nlist
37798186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
37898186Sgordon		_prefix=", "
379206248Sdougb		pwait $_list 2>/dev/null
38078344Sobrien	done
38198186Sgordon	if [ -n "$_prefix" ]; then
38298186Sgordon		echo "."
38398186Sgordon	fi
38478344Sobrien}
38578344Sobrien
38678344Sobrien#
387220962Sdougb# get_pidfile_from_conf string file
388220962Sdougb#
389220962Sdougb#	Takes a string to search for in the specified file.
390220962Sdougb#	Ignores lines with traditional comment characters.
391220962Sdougb#
392220962Sdougb# Example:
393220962Sdougb#
394220962Sdougb# if get_pidfile_from_conf string file; then
395220962Sdougb#	pidfile="$_pidfile_from_conf"
396220962Sdougb# else
397220962Sdougb#	pidfile='appropriate default'
398220962Sdougb# fi
399220962Sdougb#
400220962Sdougbget_pidfile_from_conf()
401220962Sdougb{
402220963Sdougb	if [ -z "$1" -o -z "$2" ]; then
403220963Sdougb		err 3 "USAGE: get_pidfile_from_conf string file ($name)"
404220963Sdougb	fi
405220963Sdougb
406220962Sdougb	local string file line
407220962Sdougb
408220962Sdougb	string="$1" ; file="$2"
409220962Sdougb
410220963Sdougb	if [ ! -s "$file" ]; then
411220963Sdougb		err 3 "get_pidfile_from_conf: $file does not exist ($name)"
412220962Sdougb	fi
413220962Sdougb
414220962Sdougb	while read line; do
415220962Sdougb		case "$line" in
416220962Sdougb		*[#\;]*${string}*)	continue ;;
417220962Sdougb		*${string}*)		break ;;
418220962Sdougb		esac
419220962Sdougb	done < $file
420220962Sdougb
421220962Sdougb	if [ -n "$line" ]; then
422220962Sdougb		line=${line#*/}
423220962Sdougb		_pidfile_from_conf="/${line%%[\"\;]*}"
424220962Sdougb	else
425220962Sdougb		return 1
426220962Sdougb	fi
427220962Sdougb}
428220962Sdougb
429220962Sdougb#
430197947Sdougb# check_startmsgs
431197947Sdougb#	If rc_quiet is set (usually as a result of using faststart at
432197947Sdougb#	boot time) check if rc_startmsgs is enabled.
433197947Sdougb#
434197947Sdougbcheck_startmsgs()
435197947Sdougb{
436197947Sdougb	if [ -n "$rc_quiet" ]; then
437197947Sdougb		checkyesno rc_startmsgs
438197947Sdougb	else
439197947Sdougb		return 0
440197947Sdougb	fi
441197947Sdougb}
442197947Sdougb
443197947Sdougb#
44498186Sgordon# run_rc_command argument
44598186Sgordon#	Search for argument in the list of supported commands, which is:
44698186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
44798186Sgordon#	If there's a match, run ${argument}_cmd or the default method
44898186Sgordon#	(see below).
44978344Sobrien#
45098186Sgordon#	If argument has a given prefix, then change the operation as follows:
45198186Sgordon#		Prefix	Operation
45278344Sobrien#		------	---------
453175676Smtm#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
45498186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
455126303Smtm#		one	Set ${rcvar} to YES
456175676Smtm#		quiet	Don't output some diagnostics, and set rc_quiet=yes
45778344Sobrien#
45878344Sobrien#	The following globals are used:
45978344Sobrien#
46098186Sgordon#	Name		Needed	Purpose
46198186Sgordon#	----		------	-------
46278344Sobrien#	name		y	Name of script.
46378344Sobrien#
46478344Sobrien#	command		n	Full path to command.
46598186Sgordon#				Not needed if ${rc_arg}_cmd is set for
46678344Sobrien#				each keyword.
46778344Sobrien#
46878344Sobrien#	command_args	n	Optional args/shell directives for command.
46978344Sobrien#
47098186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
47198186Sgordon#				call check_{pidfile,process}() appropriately.
47298186Sgordon#
473197144Shrs#	desc		n	Description of script.
474197144Shrs#
47578344Sobrien#	extra_commands	n	List of extra commands supported.
47678344Sobrien#
47798186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
47898186Sgordon#				otherwise use check_process $command.
47998186Sgordon#				In either case, only check if $command is set.
48078344Sobrien#
48198186Sgordon#	procname	n	Process name to check for instead of $command.
48298186Sgordon#
48378344Sobrien#	rcvar		n	This is checked with checkyesno to determine
48478344Sobrien#				if the action should be run.
48578344Sobrien#
486157653Sflz#	${name}_program	n	Full path to command.
487157653Sflz#				Meant to be used in /etc/rc.conf to override
488157653Sflz#				${command}.
489157653Sflz#
49078344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
49198186Sgordon#				Requires /usr to be mounted.
49278344Sobrien#
49378344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
49478344Sobrien#				(if not using ${name}_chroot).
49578344Sobrien#
49678344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
49778344Sobrien#				NOTE:	$flags from the parent environment
49878344Sobrien#					can be used to override this.
49978344Sobrien#
50078344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
50178344Sobrien#
50278344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
50378344Sobrien#				using ${name}_chroot.
50498186Sgordon#				Requires /usr to be mounted.
50578344Sobrien#
50678344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
50798186Sgordon#				Requires /usr to be mounted.
50878344Sobrien#
50998186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
51098186Sgordon#				to run the chrooted ${command} with.
51198186Sgordon#				Requires /usr to be mounted.
51278344Sobrien#
51398186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
51478344Sobrien#				Otherwise, use default command (see below)
51578344Sobrien#
51698186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
51798186Sgordon#				${rc_arg}_cmd method in the default
51898186Sgordon#				operation (i.e, after checking for required
51998186Sgordon#				bits and process (non)existence).
52078344Sobrien#				If this completes with a non-zero exit code,
52198186Sgordon#				don't run ${rc_arg}_cmd.
52278344Sobrien#
52398186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
52498186Sgordon#				${rc_arg}_cmd method, if that method
52598186Sgordon#				returned a zero exit code.
52698186Sgordon#
52778344Sobrien#	required_dirs	n	If set, check for the existence of the given
528165565Syar#				directories before running a (re)start command.
52978344Sobrien#
53078344Sobrien#	required_files	n	If set, check for the readability of the given
531165565Syar#				files before running a (re)start command.
53278344Sobrien#
533165565Syar#	required_modules n	If set, ensure the given kernel modules are
534165565Syar#				loaded before running a (re)start command.
535165565Syar#				The check and possible loads are actually
536165565Syar#				done after start_precmd so that the modules
537165565Syar#				aren't loaded in vain, should the precmd
538165565Syar#				return a non-zero status to indicate a error.
539165565Syar#				If a word in the list looks like "foo:bar",
540165565Syar#				"foo" is the KLD file name and "bar" is the
541165565Syar#				module name.  If a word looks like "foo~bar",
542165565Syar#				"foo" is the KLD file name and "bar" is a
543165565Syar#				egrep(1) pattern matching the module name.
544165565Syar#				Otherwise the module name is assumed to be
545165565Syar#				the same as the KLD file name, which is most
546165565Syar#				common.  See load_kld().
547165565Syar#
54878344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
54978344Sobrien#				listed variables before running the default
55078344Sobrien#				(re)start command.
55178344Sobrien#
55298186Sgordon#	Default behaviour for a given argument, if no override method is
55398186Sgordon#	provided:
55478344Sobrien#
55598186Sgordon#	Argument	Default behaviour
55698186Sgordon#	--------	-----------------
55778344Sobrien#	start		if !running && checkyesno ${rcvar}
55878344Sobrien#				${command}
55978344Sobrien#
56078344Sobrien#	stop		if ${pidfile}
56198186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
56278344Sobrien#			else
56398186Sgordon#				rc_pid=$(check_process $command)
56498186Sgordon#			kill $sig_stop $rc_pid
56598186Sgordon#			wait_for_pids $rc_pid
56698186Sgordon#			($sig_stop defaults to TERM.)
56778344Sobrien#
56898186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
56998186Sgordon#			and doesn't wait_for_pids.
57078344Sobrien#			$sig_reload defaults to HUP.
571151685Syar#			Note that `reload' isn't provided by default,
572151685Syar#			it should be enabled via $extra_commands.
57378344Sobrien#
57478344Sobrien#	restart		Run `stop' then `start'.
57578344Sobrien#
57698186Sgordon#	status		Show if ${command} is running, etc.
57778344Sobrien#
57898186Sgordon#	poll		Wait for ${command} to exit.
57998186Sgordon#
58098186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
58198186Sgordon#
58298186Sgordon#	Variables available to methods, and after run_rc_command() has
58398186Sgordon#	completed:
58498186Sgordon#
58598186Sgordon#	Variable	Purpose
58698186Sgordon#	--------	-------
587126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
58898186Sgordon#			performed
58998186Sgordon#
59098186Sgordon#	rc_flags	Flags to start the default command with.
59198186Sgordon#			Defaults to ${name}_flags, unless overridden
59298186Sgordon#			by $flags from the environment.
59398186Sgordon#			This variable may be changed by the precmd method.
59498186Sgordon#
59598186Sgordon#	rc_pid		PID of command (if appropriate)
59698186Sgordon#
59798186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
59898186Sgordon#
59998186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
60098186Sgordon#
601175676Smtm#	rc_quiet	Not empty if "quiet" was provided
60298186Sgordon#
603175676Smtm#
60478344Sobrienrun_rc_command()
60578344Sobrien{
606116097Smtm	_return=0
60798186Sgordon	rc_arg=$1
60878344Sobrien	if [ -z "$name" ]; then
60998186Sgordon		err 3 'run_rc_command: $name is not set.'
61078344Sobrien	fi
61178344Sobrien
612132892Smtm	# Don't repeat the first argument when passing additional command-
613132892Smtm	# line arguments to the command subroutines.
614132892Smtm	#
615132892Smtm	shift 1
616132892Smtm	rc_extra_args="$*"
617132892Smtm
618126303Smtm	_rc_prefix=
61998186Sgordon	case "$rc_arg" in
62078344Sobrien	fast*)				# "fast" prefix; don't check pid
62198186Sgordon		rc_arg=${rc_arg#fast}
62298186Sgordon		rc_fast=yes
623175676Smtm		rc_quiet=yes
62478344Sobrien		;;
625198216Sed	force*)				# "force" prefix; always run
62698186Sgordon		rc_force=yes
627126303Smtm		_rc_prefix=force
628126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
62978344Sobrien		if [ -n "${rcvar}" ]; then
63078344Sobrien			eval ${rcvar}=YES
63178344Sobrien		fi
63278344Sobrien		;;
633126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
634126303Smtm		_rc_prefix=one
635126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
636126303Smtm		if [ -n "${rcvar}" ]; then
637126303Smtm			eval ${rcvar}=YES
638126303Smtm		fi
639126303Smtm		;;
640175676Smtm	quiet*)				# "quiet" prefix; omit some messages
641175676Smtm		_rc_prefix=quiet
642175676Smtm		rc_arg=${rc_arg#${_rc_prefix}}
643175676Smtm		rc_quiet=yes
644175676Smtm		;;
64578344Sobrien	esac
64678344Sobrien
647161530Sflz	eval _override_command=\$${name}_program
648198162Sdougb	command=${_override_command:-$command}
649161530Sflz
65078344Sobrien	_keywords="start stop restart rcvar $extra_commands"
65198186Sgordon	rc_pid=
65278344Sobrien	_pidcmd=
65398186Sgordon	_procname=${procname:-${command}}
65498186Sgordon
655131135Smtm					# setup pid check command
656131135Smtm	if [ -n "$_procname" ]; then
65778344Sobrien		if [ -n "$pidfile" ]; then
65898186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
65998186Sgordon		else
66098186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
66178344Sobrien		fi
66278344Sobrien		if [ -n "$_pidcmd" ]; then
66398186Sgordon			_keywords="${_keywords} status poll"
66478344Sobrien		fi
66578344Sobrien	fi
66678344Sobrien
66798186Sgordon	if [ -z "$rc_arg" ]; then
668150796Syar		rc_usage $_keywords
66978344Sobrien	fi
67078344Sobrien
67178344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
67298186Sgordon		rc_flags=$flags
67378344Sobrien	else
67498186Sgordon		eval rc_flags=\$${name}_flags
67578344Sobrien	fi
67698186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
67798186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
67898186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
67978344Sobrien
68098186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
681124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
68298186Sgordon			unset _user
68398186Sgordon		fi
68498186Sgordon	fi
68598186Sgordon
686179870Smtm	eval $_pidcmd			# determine the pid if necessary
687179870Smtm
688179870Smtm	for _elem in $_keywords; do
689179870Smtm		if [ "$_elem" != "$rc_arg" ]; then
690179870Smtm			continue
691179870Smtm		fi
692206686Sdougb					# if ${rcvar} is set, $1 is not "rcvar"
693206686Sdougb					# and ${rc_pid} is not set, then run
69478344Sobrien					#	checkyesno ${rcvar}
69578344Sobrien					# and return if that failed
69678344Sobrien					#
697220760Sdougb		if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] ||
698220760Sdougb		    [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then
699179870Smtm			if ! checkyesno ${rcvar}; then
700179870Smtm				if [ -n "${rc_quiet}" ]; then
701179870Smtm					return 0
702179870Smtm				fi
703179870Smtm				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
704179870Smtm				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
705179870Smtm				echo "instead of '${rc_arg}'."
706175676Smtm				return 0
707175676Smtm			fi
70878344Sobrien		fi
70978344Sobrien
71078344Sobrien					# if there's a custom ${XXX_cmd},
71178344Sobrien					# run that instead of the default
71278344Sobrien					#
713165565Syar		eval _cmd=\$${rc_arg}_cmd \
714165565Syar		     _precmd=\$${rc_arg}_precmd \
715165565Syar		     _postcmd=\$${rc_arg}_postcmd
716165565Syar
71778344Sobrien		if [ -n "$_cmd" ]; then
718165565Syar			_run_rc_precmd || return 1
719165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
720165565Syar			_run_rc_postcmd
721116097Smtm			return $_return
72278344Sobrien		fi
72378344Sobrien
72498186Sgordon		case "$rc_arg" in	# default operations...
72578344Sobrien
72678344Sobrien		status)
727165565Syar			_run_rc_precmd || return 1
72898186Sgordon			if [ -n "$rc_pid" ]; then
72998186Sgordon				echo "${name} is running as pid $rc_pid."
73078344Sobrien			else
73178344Sobrien				echo "${name} is not running."
73278344Sobrien				return 1
73378344Sobrien			fi
734165565Syar			_run_rc_postcmd
73578344Sobrien			;;
73678344Sobrien
73778344Sobrien		start)
738131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
739157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
740153152Syar				return 1
74178344Sobrien			fi
74278344Sobrien
743167413Syar			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
744160667Syar				warn "run_rc_command: cannot run $command"
745153152Syar				return 1
74678344Sobrien			fi
74778344Sobrien
748179946Smtm			if ! _run_rc_precmd; then
749179946Smtm				warn "failed precmd routine for ${name}"
750179946Smtm				return 1
751179946Smtm			fi
75278344Sobrien
753160668Syar					# setup the full command to run
75478344Sobrien					#
755197947Sdougb			check_startmsgs && echo "Starting ${name}."
75678344Sobrien			if [ -n "$_chroot" ]; then
75778344Sobrien				_doit="\
75878344Sobrien${_nice:+nice -n $_nice }\
75978344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
76098186Sgordon$_chroot $command $rc_flags $command_args"
76178344Sobrien			else
76278344Sobrien				_doit="\
763161396Syar${_chdir:+cd $_chdir && }\
76498186Sgordon$command $rc_flags $command_args"
76598186Sgordon				if [ -n "$_user" ]; then
76698186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
76798186Sgordon				fi
768161396Syar				if [ -n "$_nice" ]; then
769161396Syar					if [ -z "$_user" ]; then
770161396Syar						_doit="sh -c \"$_doit\""
771201036Sdougb					fi
772161396Syar					_doit="nice -n $_nice $_doit"
773161396Syar				fi
77478344Sobrien			fi
77598186Sgordon
776165565Syar					# run the full command
77798186Sgordon					#
778179946Smtm			if ! _run_rc_doit "$_doit"; then
779179946Smtm				warn "failed to start ${name}"
780179946Smtm				return 1
781179946Smtm			fi
78298186Sgordon
78398186Sgordon					# finally, run postcmd
78498186Sgordon					#
785165565Syar			_run_rc_postcmd
78678344Sobrien			;;
78778344Sobrien
78878344Sobrien		stop)
78998186Sgordon			if [ -z "$rc_pid" ]; then
790153152Syar				[ -n "$rc_fast" ] && return 0
791165565Syar				_run_rc_notrunning
792153152Syar				return 1
79378344Sobrien			fi
79478344Sobrien
795165565Syar			_run_rc_precmd || return 1
79698186Sgordon
79798186Sgordon					# send the signal to stop
79898186Sgordon					#
79978344Sobrien			echo "Stopping ${name}."
800165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
801165565Syar			_run_rc_doit "$_doit" || return 1
80298186Sgordon
80398186Sgordon					# wait for the command to exit,
80498186Sgordon					# and run postcmd.
80598186Sgordon			wait_for_pids $rc_pid
806165565Syar
807165565Syar			_run_rc_postcmd
80878344Sobrien			;;
80978344Sobrien
81078344Sobrien		reload)
81198186Sgordon			if [ -z "$rc_pid" ]; then
812165565Syar				_run_rc_notrunning
813153152Syar				return 1
81478344Sobrien			fi
815165565Syar
816165565Syar			_run_rc_precmd || return 1
817165565Syar
818165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
819165565Syar			_run_rc_doit "$_doit" || return 1
820165565Syar
821165565Syar			_run_rc_postcmd
82278344Sobrien			;;
82378344Sobrien
82478344Sobrien		restart)
82578344Sobrien					# prevent restart being called more
82678344Sobrien					# than once by any given script
82778344Sobrien					#
828126285Smtm			if ${_rc_restart_done:-false}; then
82978344Sobrien				return 0
83078344Sobrien			fi
831126285Smtm			_rc_restart_done=true
83278344Sobrien
833165565Syar			_run_rc_precmd || return 1
834165565Syar
835165565Syar			# run those in a subshell to keep global variables
836152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
837165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
838165565Syar			_return=$?
839165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
84098186Sgordon
841165565Syar			_run_rc_postcmd
84278344Sobrien			;;
84378344Sobrien
84498186Sgordon		poll)
845165565Syar			_run_rc_precmd || return 1
84698186Sgordon			if [ -n "$rc_pid" ]; then
84798186Sgordon				wait_for_pids $rc_pid
84898186Sgordon			fi
849165565Syar			_run_rc_postcmd
85098186Sgordon			;;
85198186Sgordon
85278344Sobrien		rcvar)
853197144Shrs			echo -n "# $name"
854197144Shrs			if [ -n "$desc" ]; then
855197144Shrs				echo " : $desc"
856197144Shrs			else
857197144Shrs				echo ""
858197144Shrs			fi
859197144Shrs			echo "#"
860197144Shrs			# Get unique vars in $rcvar $rcvars
861197144Shrs			for _v in $rcvar $rcvars; do
862197144Shrs				case $v in
863197144Shrs				$_v\ *|\ *$_v|*\ $_v\ *) ;;
864197144Shrs				*)	v="${v# } $_v" ;;
865197144Shrs				esac
866197144Shrs			done
867197144Shrs
868197144Shrs			# Display variables.
869197144Shrs			for _v in $v; do
870197144Shrs				if [ -z "$_v" ]; then
871197144Shrs					continue
87278344Sobrien				fi
873197144Shrs
874197144Shrs				eval _desc=\$${_v}_desc
875197144Shrs				eval _defval=\$${_v}_defval
876197144Shrs				_h="-"
877197144Shrs
878197144Shrs				eval echo \"$_v=\\\"\$$_v\\\"\"
879197144Shrs				# decode multiple lines of _desc
880197144Shrs				while [ -n "$_desc" ]; do
881197144Shrs					case $_desc in
882197144Shrs					*^^*)
883197144Shrs						echo "# $_h ${_desc%%^^*}"
884197144Shrs						_desc=${_desc#*^^}
885197144Shrs						_h=" "
886197144Shrs						;;
887197144Shrs					*)
888197144Shrs						echo "# $_h ${_desc}"
889197144Shrs						break
890197144Shrs						;;
891197144Shrs					esac
892197144Shrs				done
893197144Shrs				echo "#   (default: \"$_defval\")"
894197144Shrs			done
895197144Shrs			echo ""
89678344Sobrien			;;
89778344Sobrien
89878344Sobrien		*)
899150796Syar			rc_usage $_keywords
90078344Sobrien			;;
90178344Sobrien
90278344Sobrien		esac
903116097Smtm		return $_return
90478344Sobrien	done
90578344Sobrien
90698186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
907150796Syar	rc_usage $_keywords
908153152Syar	# not reached
90978344Sobrien}
91078344Sobrien
91178344Sobrien#
912165565Syar# Helper functions for run_rc_command: common code.
913165565Syar# They use such global variables besides the exported rc_* ones:
914165565Syar#
915165565Syar#	name	       R/W
916165565Syar#	------------------
917165565Syar#	_precmd		R
918165565Syar#	_postcmd	R
919165565Syar#	_return		W
920165565Syar#
921165565Syar_run_rc_precmd()
922165565Syar{
923165565Syar	check_required_before "$rc_arg" || return 1
924165565Syar
925165565Syar	if [ -n "$_precmd" ]; then
926165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
927165565Syar		eval "$_precmd $rc_extra_args"
928165565Syar		_return=$?
929165565Syar
930165565Syar		# If precmd failed and force isn't set, request exit.
931165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
932165565Syar			return 1
933165565Syar		fi
934165565Syar	fi
935165565Syar
936165565Syar	check_required_after "$rc_arg" || return 1
937165565Syar
938165565Syar	return 0
939165565Syar}
940165565Syar
941165565Syar_run_rc_postcmd()
942165565Syar{
943165565Syar	if [ -n "$_postcmd" ]; then
944165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
945165565Syar		eval "$_postcmd $rc_extra_args"
946165565Syar		_return=$?
947165565Syar	fi
948165565Syar	return 0
949165565Syar}
950165565Syar
951165565Syar_run_rc_doit()
952165565Syar{
953165565Syar	debug "run_rc_command: doit: $*"
954165565Syar	eval "$@"
955165565Syar	_return=$?
956165565Syar
957165565Syar	# If command failed and force isn't set, request exit.
958165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
959165565Syar		return 1
960165565Syar	fi
961165565Syar
962165565Syar	return 0
963165565Syar}
964165565Syar
965165565Syar_run_rc_notrunning()
966165565Syar{
967165565Syar	local _pidmsg
968165565Syar
969165565Syar	if [ -n "$pidfile" ]; then
970165565Syar		_pidmsg=" (check $pidfile)."
971165565Syar	else
972165565Syar		_pidmsg=
973165565Syar	fi
974165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
975165565Syar}
976165565Syar
977165565Syar_run_rc_killcmd()
978165565Syar{
979165565Syar	local _cmd
980165565Syar
981165565Syar	_cmd="kill -$1 $rc_pid"
982165565Syar	if [ -n "$_user" ]; then
983165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
984165565Syar	fi
985165565Syar	echo "$_cmd"
986165565Syar}
987165565Syar
988165565Syar#
98978344Sobrien# run_rc_script file arg
99078344Sobrien#	Start the script `file' with `arg', and correctly handle the
991201038Sdougb#	return value from the script.
992201038Sdougb#	If `file' ends with `.sh', it's sourced into the current environment
993201038Sdougb#	when $rc_fast_and_loose is set, otherwise it is run as a child process.
994201038Sdougb#	If `file' appears to be a backup or scratch file, ignore it.
995201038Sdougb#	Otherwise if it is executable run as a child process.
99678344Sobrien#
99778344Sobrienrun_rc_script()
99878344Sobrien{
99978344Sobrien	_file=$1
100078344Sobrien	_arg=$2
100178344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
100278344Sobrien		err 3 'USAGE: run_rc_script file arg'
100378344Sobrien	fi
100478344Sobrien
100598186Sgordon	unset	name command command_args command_interpreter \
100698186Sgordon		extra_commands pidfile procname \
1007197144Shrs		rcvar rcvars rcvars_obsolete required_dirs required_files \
1008197144Shrs		required_vars
100998186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
101098186Sgordon
101178344Sobrien	case "$_file" in
1012193118Sdougb	/etc/rc.d/*.sh)			# no longer allowed in the base
1013193118Sdougb		warn "Ignoring old-style startup script $_file"
101478344Sobrien		;;
1015153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
101698186Sgordon		warn "Ignoring scratch file $_file"
101798186Sgordon		;;
101878344Sobrien	*)				# run in subshell
101998186Sgordon		if [ -x $_file ]; then
102098186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
1021146490Sschweikh				set $_arg; . $_file
102298186Sgordon			else
1023130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
1024130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
1025184317Sthompsa				  trap "echo Script $_file running" 29
1026146490Sschweikh				  set $_arg; . $_file )
102798186Sgordon			fi
102898186Sgordon		fi
102978344Sobrien		;;
103078344Sobrien	esac
103178344Sobrien}
103278344Sobrien
103378344Sobrien#
1034157653Sflz# load_rc_config name
1035157653Sflz#	Source in the configuration file for a given name.
103678344Sobrien#
103778344Sobrienload_rc_config()
103878344Sobrien{
1039197144Shrs	local _name _var _defval _v _msg _new
1040157653Sflz	_name=$1
1041157653Sflz	if [ -z "$_name" ]; then
1042157653Sflz		err 3 'USAGE: load_rc_config name'
104378344Sobrien	fi
104478344Sobrien
1045219612Sdougb	if ${_rc_conf_loaded:-false}; then
1046219612Sdougb		:
1047219612Sdougb	else
104898186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
104998186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
105098186Sgordon			. /etc/defaults/rc.conf
105198186Sgordon			source_rc_confs
105298186Sgordon		elif [ -r /etc/rc.conf ]; then
105398186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
105498186Sgordon			. /etc/rc.conf
105598186Sgordon		fi
1056126285Smtm		_rc_conf_loaded=true
105798186Sgordon	fi
1058161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
1059157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
1060161530Sflz		. /etc/rc.conf.d/"$_name"
1061157653Sflz	fi
1062179872Smtm
1063197144Shrs	# Set defaults if defined.
1064197144Shrs	for _var in $rcvar $rcvars; do
1065197144Shrs		_defval=`eval echo "\\\$${_var}_defval"`
1066197144Shrs		if [ -n "$_defval" ]; then
1067197144Shrs			eval : \${$_var:=\$${_var}_defval}
1068197144Shrs		fi
1069197144Shrs	done
1070197144Shrs
1071197144Shrs	# check obsolete rc.conf variables
1072197144Shrs	for _var in $rcvars_obsolete; do
1073197144Shrs		_v=`eval echo \\$$_var`
1074197144Shrs		_msg=`eval echo \\$${_var}_obsolete_msg`
1075197144Shrs		_new=`eval echo \\$${_var}_newvar`
1076197144Shrs		case $_v in
1077197144Shrs		"")
1078197144Shrs			;;
1079197144Shrs		*)
1080197144Shrs			if [ -z "$_new" ]; then
1081197144Shrs				_msg="Ignored."
1082197144Shrs			else
1083197144Shrs				eval $_new=\"\$$_var\"
1084197144Shrs				if [ -z "$_msg" ]; then
1085197144Shrs					_msg="Use \$$_new instead."
1086197144Shrs				fi
1087197144Shrs			fi
1088197144Shrs			warn "\$$_var is obsolete.  $_msg"
1089197144Shrs			;;
1090197144Shrs		esac
1091197144Shrs	done
109278344Sobrien}
1093201036Sdougb
1094157473Sflz#
1095157653Sflz# load_rc_config_var name var
1096157653Sflz#	Read the rc.conf(5) var for name and set in the
1097157473Sflz#	current shell, using load_rc_config in a subshell to prevent
1098157473Sflz#	unwanted side effects from other variable assignments.
1099157473Sflz#
1100157473Sflzload_rc_config_var()
1101157473Sflz{
1102157473Sflz	if [ $# -ne 2 ]; then
1103157653Sflz		err 3 'USAGE: load_rc_config_var name var'
1104157473Sflz	fi
1105157473Sflz	eval $(eval '(
1106157473Sflz		load_rc_config '$1' >/dev/null;
1107157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1108157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
1109157473Sflz		fi
1110157473Sflz	)' )
1111157473Sflz}
111278344Sobrien
111378344Sobrien#
111478344Sobrien# rc_usage commands
111578344Sobrien#	Print a usage string for $0, with `commands' being a list of
111678344Sobrien#	valid commands.
111778344Sobrien#
111878344Sobrienrc_usage()
111978344Sobrien{
1120126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
112178344Sobrien
112278344Sobrien	_sep=
1123126286Smtm	for _elem; do
112478344Sobrien		echo -n 1>&2 "$_sep$_elem"
112578344Sobrien		_sep="|"
112678344Sobrien	done
112778344Sobrien	echo 1>&2 ")"
112878344Sobrien	exit 1
112978344Sobrien}
113078344Sobrien
113178344Sobrien#
113278344Sobrien# err exitval message
113378344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
113478344Sobrien#
113578344Sobrienerr()
113678344Sobrien{
113778344Sobrien	exitval=$1
113878344Sobrien	shift
113978344Sobrien
1140106643Sgordon	if [ -x /usr/bin/logger ]; then
1141106643Sgordon		logger "$0: ERROR: $*"
1142106643Sgordon	fi
1143106643Sgordon	echo 1>&2 "$0: ERROR: $*"
114478344Sobrien	exit $exitval
114578344Sobrien}
114678344Sobrien
114778344Sobrien#
114878344Sobrien# warn message
114978344Sobrien#	Display message to stderr and log to the syslog.
115078344Sobrien#
115178344Sobrienwarn()
115278344Sobrien{
1153106643Sgordon	if [ -x /usr/bin/logger ]; then
1154106643Sgordon		logger "$0: WARNING: $*"
1155106643Sgordon	fi
1156106643Sgordon	echo 1>&2 "$0: WARNING: $*"
115778344Sobrien}
115898186Sgordon
115998186Sgordon#
116098186Sgordon# info message
116198186Sgordon#	Display informational message to stdout and log to syslog.
116298186Sgordon#
116398186Sgordoninfo()
116498186Sgordon{
1165119170Smtm	case ${rc_info} in
1166119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1167119170Smtm		if [ -x /usr/bin/logger ]; then
1168119170Smtm			logger "$0: INFO: $*"
1169119170Smtm		fi
1170119170Smtm		echo "$0: INFO: $*"
1171119170Smtm		;;
1172119170Smtm	esac
117398186Sgordon}
117498186Sgordon
117598186Sgordon#
117698186Sgordon# debug message
1177106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
117898186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
117998186Sgordon#	function.
118098186Sgordon#
118198186Sgordondebug()
118298186Sgordon{
118398186Sgordon	case ${rc_debug} in
118498186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1185106700Sgordon		if [ -x /usr/bin/logger ]; then
1186162947Syar			logger "$0: DEBUG: $*"
1187106700Sgordon		fi
1188146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
118998186Sgordon		;;
119098186Sgordon	esac
119198186Sgordon}
119298186Sgordon
119398186Sgordon#
119498186Sgordon# backup_file action file cur backup
119598186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
119698186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
119798186Sgordon#
119898186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
119998186Sgordon#	which can be either YES or NO.
120098186Sgordon#
120198186Sgordon#	The `action' keyword can be one of the following:
120298186Sgordon#
120398186Sgordon#	add		`file' is now being backed up (and is possibly
120498186Sgordon#			being reentered into the backups system).  `cur'
120598186Sgordon#			is created and RCS files, if necessary, are
120698186Sgordon#			created as well.
120798186Sgordon#
120898186Sgordon#	update		`file' has changed and needs to be backed up.
120998186Sgordon#			If `cur' exists, it is copied to to `back' or
121098186Sgordon#			checked into RCS (if the repository file is old),
121198186Sgordon#			and then `file' is copied to `cur'.  Another RCS
121298186Sgordon#			check in done here if RCS is being used.
121398186Sgordon#
121498186Sgordon#	remove		`file' is no longer being tracked by the backups
121598186Sgordon#			system.  If RCS is not being used, `cur' is moved
121698186Sgordon#			to `back', otherwise an empty file is checked in,
121798186Sgordon#			and then `cur' is removed.
121898186Sgordon#
121998186Sgordon#
122098186Sgordonbackup_file()
122198186Sgordon{
122298186Sgordon	_action=$1
122398186Sgordon	_file=$2
122498186Sgordon	_cur=$3
122598186Sgordon	_back=$4
122698186Sgordon
122798186Sgordon	if checkyesno backup_uses_rcs; then
122898186Sgordon		_msg0="backup archive"
122998186Sgordon		_msg1="update"
123098186Sgordon
123198186Sgordon		# ensure that history file is not locked
123298186Sgordon		if [ -f $_cur,v ]; then
123398186Sgordon			rcs -q -u -U -M $_cur
123498186Sgordon		fi
123598186Sgordon
123698186Sgordon		# ensure after switching to rcs that the
123798186Sgordon		# current backup is not lost
123898186Sgordon		if [ -f $_cur ]; then
123998186Sgordon			# no archive, or current newer than archive
124098186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
124198186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
124298186Sgordon				rcs -q -kb -U $_cur
124398186Sgordon				co -q -f -u $_cur
124498186Sgordon			fi
124598186Sgordon		fi
124698186Sgordon
124798186Sgordon		case $_action in
124898186Sgordon		add|update)
124998186Sgordon			cp -p $_file $_cur
125098186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
125198186Sgordon			rcs -q -kb -U $_cur
125298186Sgordon			co -q -f -u $_cur
125398186Sgordon			chown root:wheel $_cur $_cur,v
125498186Sgordon			;;
125598186Sgordon		remove)
125698186Sgordon			cp /dev/null $_cur
125798186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
125898186Sgordon			rcs -q -kb -U $_cur
125998186Sgordon			chown root:wheel $_cur $_cur,v
126098186Sgordon			rm $_cur
126198186Sgordon			;;
126298186Sgordon		esac
126398186Sgordon	else
126498186Sgordon		case $_action in
126598186Sgordon		add|update)
126698186Sgordon			if [ -f $_cur ]; then
126798186Sgordon				cp -p $_cur $_back
126898186Sgordon			fi
126998186Sgordon			cp -p $_file $_cur
127098186Sgordon			chown root:wheel $_cur
127198186Sgordon			;;
127298186Sgordon		remove)
127398186Sgordon			mv -f $_cur $_back
127498186Sgordon			;;
127598186Sgordon		esac
127698186Sgordon	fi
127798186Sgordon}
1278119166Smtm
1279123344Smtm# make_symlink src link
1280123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1281123344Smtm#	directory in which link is to be created does not exist
1282123344Smtm#	a warning will be displayed and an error will be returned.
1283123344Smtm#	Returns 0 on sucess, 1 otherwise.
1284119166Smtm#
1285123344Smtmmake_symlink()
1286119166Smtm{
1287123344Smtm	local src link linkdir _me
1288123344Smtm	src="$1"
1289123344Smtm	link="$2"
1290123344Smtm	linkdir="`dirname $link`"
1291123344Smtm	_me="make_symlink()"
1292119166Smtm
1293123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1294123344Smtm		warn "$_me: requires two arguments."
1295119166Smtm		return 1
1296119166Smtm	fi
1297123344Smtm	if [ ! -d "$linkdir" ]; then
1298160667Syar		warn "$_me: the directory $linkdir does not exist."
1299119166Smtm		return 1
1300119166Smtm	fi
1301146490Sschweikh	if ! ln -sf $src $link; then
1302123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1303119166Smtm		return 1
1304119166Smtm	fi
1305119166Smtm	return 0
1306119166Smtm}
1307119166Smtm
1308119166Smtm# devfs_rulesets_from_file file
1309119166Smtm#	Reads a set of devfs commands from file, and creates
1310119166Smtm#	the specified rulesets with their rules. Returns non-zero
1311119166Smtm#	if there was an error.
1312119166Smtm#
1313119166Smtmdevfs_rulesets_from_file()
1314119166Smtm{
1315119166Smtm	local file _err _me
1316119166Smtm	file="$1"
1317119166Smtm	_me="devfs_rulesets_from_file"
1318119166Smtm	_err=0
1319119166Smtm
1320119166Smtm	if [ -z "$file" ]; then
1321119166Smtm		warn "$_me: you must specify a file"
1322119166Smtm		return 1
1323119166Smtm	fi
1324119166Smtm	if [ ! -e "$file" ]; then
1325119166Smtm		debug "$_me: no such file ($file)"
1326119166Smtm		return 0
1327119166Smtm	fi
1328119166Smtm	debug "reading rulesets from file ($file)"
1329119166Smtm	{ while read line
1330119166Smtm	do
1331119166Smtm		case $line in
1332119166Smtm		\#*)
1333119166Smtm			continue
1334119166Smtm			;;
1335119166Smtm		\[*\]*)
1336119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1337119166Smtm			if [ -z "$rulenum" ]; then
1338119166Smtm				warn "$_me: cannot extract rule number ($line)"
1339119166Smtm				_err=1
1340119166Smtm				break
1341119166Smtm			fi
1342119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1343119166Smtm			if [ -z "$rulename" ]; then
1344119166Smtm				warn "$_me: cannot extract rule name ($line)"
1345119166Smtm				_err=1
1346119166Smtm				break;
1347119166Smtm			fi
1348119166Smtm			eval $rulename=\$rulenum
1349119166Smtm			debug "found ruleset: $rulename=$rulenum"
1350146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1351119166Smtm				_err=1
1352119166Smtm				break
1353119166Smtm			fi
1354119166Smtm			;;
1355119166Smtm		*)
1356119166Smtm			rulecmd="${line%%"\#*"}"
1357119166Smtm			# evaluate the command incase it includes
1358119166Smtm			# other rules
1359119166Smtm			if [ -n "$rulecmd" ]; then
1360119166Smtm				debug "adding rule ($rulecmd)"
1361119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1362119166Smtm				then
1363119166Smtm					_err=1
1364119166Smtm					break
1365119166Smtm				fi
1366119166Smtm			fi
1367119166Smtm			;;
1368119166Smtm		esac
1369119166Smtm		if [ $_err -ne 0 ]; then
1370119166Smtm			debug "error in $_me"
1371119166Smtm			break
1372119166Smtm		fi
1373119166Smtm	done } < $file
1374119166Smtm	return $_err
1375119166Smtm}
1376119166Smtm
1377119166Smtm# devfs_init_rulesets
1378119166Smtm#	Initializes rulesets from configuration files. Returns
1379119166Smtm#	non-zero if there was an error.
1380119166Smtm#
1381119166Smtmdevfs_init_rulesets()
1382119166Smtm{
1383119166Smtm	local file _me
1384119166Smtm	_me="devfs_init_rulesets"
1385119166Smtm
1386119166Smtm	# Go through this only once
1387119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1388119166Smtm		debug "$_me: devfs rulesets already initialized"
1389119166Smtm		return
1390119166Smtm	fi
1391146490Sschweikh	for file in $devfs_rulesets; do
1392217090Sjh		if ! devfs_rulesets_from_file $file; then
1393217090Sjh			warn "$_me: could not read rules from $file"
1394217090Sjh			return 1
1395217090Sjh		fi
1396119166Smtm	done
1397119166Smtm	devfs_rulesets_init=1
1398119166Smtm	debug "$_me: devfs rulesets initialized"
1399119166Smtm	return 0
1400119166Smtm}
1401119166Smtm
1402119166Smtm# devfs_set_ruleset ruleset [dir]
1403151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1404119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1405119166Smtm#	Returns non-zero if it could not set it successfully.
1406119166Smtm#
1407119166Smtmdevfs_set_ruleset()
1408119166Smtm{
1409119166Smtm	local devdir rs _me
1410119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1411119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1412119166Smtm	_me="devfs_set_ruleset"
1413119166Smtm
1414119166Smtm	if [ -z "$rs" ]; then
1415119166Smtm		warn "$_me: you must specify a ruleset number"
1416119166Smtm		return 1
1417119166Smtm	fi
1418119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1419146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1420119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1421119166Smtm		return 1
1422119166Smtm	fi
1423119166Smtm	return 0
1424119166Smtm}
1425119166Smtm
1426119166Smtm# devfs_apply_ruleset ruleset [dir]
1427119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1428119166Smtm#	The ruleset argument must be a ruleset name as specified
1429119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1430119166Smtm#	if it could not apply the ruleset.
1431119166Smtm#
1432119166Smtmdevfs_apply_ruleset()
1433119166Smtm{
1434119166Smtm	local devdir rs _me
1435119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1436119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1437119166Smtm	_me="devfs_apply_ruleset"
1438119166Smtm
1439119166Smtm	if [ -z "$rs" ]; then
1440119166Smtm		warn "$_me: you must specify a ruleset"
1441119166Smtm		return 1
1442119166Smtm	fi
1443119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1444146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1445119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1446119166Smtm		return 1
1447119166Smtm	fi
1448119166Smtm	return 0
1449119166Smtm}
1450119166Smtm
1451119166Smtm# devfs_domount dir [ruleset]
1452119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1453119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1454119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1455119166Smtm#
1456119166Smtmdevfs_domount()
1457119166Smtm{
1458119166Smtm	local devdir rs _me
1459119166Smtm	devdir="$1"
1460119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1461119166Smtm	_me="devfs_domount()"
1462119166Smtm
1463119166Smtm	if [ -z "$devdir" ]; then
1464119166Smtm		warn "$_me: you must specify a mount-point"
1465119166Smtm		return 1
1466119166Smtm	fi
1467119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1468146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1469119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1470119166Smtm		return 1
1471119166Smtm	fi
1472119166Smtm	if [ -n "$rs" ]; then
1473119166Smtm		devfs_init_rulesets
1474119166Smtm		devfs_set_ruleset $rs $devdir
1475124797Scperciva		devfs -m $devdir rule applyset
1476119166Smtm	fi
1477119166Smtm	return 0
1478119166Smtm}
1479119166Smtm
1480119166Smtm# devfs_mount_jail dir [ruleset]
1481119166Smtm#	Mounts a devfs file system appropriate for jails
1482119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1483119166Smtm#	it names will be used instead.  If present, ruleset must
1484119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1485119166Smtm#	This function returns non-zero if an error occurs.
1486119166Smtm#
1487119166Smtmdevfs_mount_jail()
1488119166Smtm{
1489119166Smtm	local jdev rs _me
1490119166Smtm	jdev="$1"
1491119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1492119166Smtm	_me="devfs_mount_jail"
1493119166Smtm
1494119166Smtm	devfs_init_rulesets
1495146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1496119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1497119166Smtm		return 1
1498119166Smtm	fi
1499119166Smtm	return 0
1500119166Smtm}
1501127345Sbrooks
1502127345Sbrooks# Provide a function for normalizing the mounting of memory
1503127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1504127345Sbrooks# as close as possible between 5-current and 4-stable.
1505127345Sbrooks#   $1 = size
1506127345Sbrooks#   $2 = mount point
1507137451Skeramida#   $3 = (optional) extra mdmfs flags
1508146490Sschweikhmount_md()
1509146490Sschweikh{
1510127345Sbrooks	if [ -n "$3" ]; then
1511137451Skeramida		flags="$3"
1512127345Sbrooks	fi
1513149421Syar	/sbin/mdmfs $flags -s $1 md $2
1514127345Sbrooks}
1515131550Scperciva
1516159828Syar# Code common to scripts that need to load a kernel module
1517159828Syar# if it isn't in the kernel yet. Syntax:
1518160666Syar#   load_kld [-e regex] [-m module] file
1519159828Syar# where -e or -m chooses the way to check if the module
1520159828Syar# is already loaded:
1521160666Syar#   regex is egrep'd in the output from `kldstat -v',
1522160666Syar#   module is passed to `kldstat -m'.
1523160666Syar# The default way is as though `-m file' were specified.
1524159828Syarload_kld()
1525159828Syar{
1526159828Syar	local _loaded _mod _opt _re
1527159828Syar
1528159828Syar	while getopts "e:m:" _opt; do
1529159828Syar		case "$_opt" in
1530159828Syar		e) _re="$OPTARG" ;;
1531159828Syar		m) _mod="$OPTARG" ;;
1532160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1533159828Syar		esac
1534159828Syar	done
1535159828Syar	shift $(($OPTIND - 1))
1536160666Syar	if [ $# -ne 1 ]; then
1537160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1538160666Syar	fi
1539159828Syar	_mod=${_mod:-$1}
1540159828Syar	_loaded=false
1541159828Syar	if [ -n "$_re" ]; then
1542159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1543159828Syar			_loaded=true
1544159828Syar		fi
1545159828Syar	else
1546159828Syar		if kldstat -q -m "$_mod"; then
1547159828Syar			_loaded=true
1548159828Syar		fi
1549159828Syar	fi
1550159828Syar	if ! $_loaded; then
1551159828Syar		if ! kldload "$1"; then
1552159828Syar			warn "Unable to load kernel module $1"
1553159828Syar			return 1
1554160666Syar		else
1555160666Syar			info "$1 kernel module loaded."
1556159828Syar		fi
1557160666Syar	else
1558160666Syar		debug "load_kld: $1 kernel module already loaded."
1559159828Syar	fi
1560159828Syar	return 0
1561159828Syar}
1562159828Syar
1563149049Spjd# ltr str src dst
1564149049Spjd#	Change every $src in $str to $dst.
1565149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1566149049Spjd#	awk(1).
1567149049Spjdltr()
1568149049Spjd{
1569149049Spjd	local _str _src _dst _out _com
1570149049Spjd	_str=$1
1571149049Spjd	_src=$2
1572149049Spjd	_dst=$3
1573149049Spjd	_out=""
1574149049Spjd
1575149049Spjd	IFS=${_src}
1576149049Spjd	for _com in ${_str}; do
1577149049Spjd		if [ -z "${_out}" ]; then
1578149049Spjd			_out="${_com}"
1579149049Spjd		else
1580149049Spjd			_out="${_out}${_dst}${_com}"
1581149049Spjd		fi
1582149049Spjd	done
1583149049Spjd	echo "${_out}"
1584149049Spjd}
1585149049Spjd
1586149050Spjd# Creates a list of providers for GELI encryption.
1587149050Spjdgeli_make_list()
1588149050Spjd{
1589149050Spjd	local devices devices2
1590149050Spjd	local provider mountpoint type options rest
1591149050Spjd
1592149050Spjd	# Create list of GELI providers from fstab.
1593149050Spjd	while read provider mountpoint type options rest ; do
1594155570Sflz		case ":${options}" in
1595155570Sflz		:*noauto*)
1596155570Sflz			noauto=yes
1597155570Sflz			;;
1598155570Sflz		*)
1599155570Sflz			noauto=no
1600155570Sflz			;;
1601155570Sflz		esac
1602155570Sflz
1603149050Spjd		case ":${provider}" in
1604149050Spjd		:#*)
1605149050Spjd			continue
1606149050Spjd			;;
1607149050Spjd		*.eli)
1608149050Spjd			# Skip swap devices.
1609155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1610149050Spjd				continue
1611149050Spjd			fi
1612149050Spjd			devices="${devices} ${provider}"
1613149050Spjd			;;
1614149050Spjd		esac
1615149050Spjd	done < /etc/fstab
1616149050Spjd
1617149050Spjd	# Append providers from geli_devices.
1618149050Spjd	devices="${devices} ${geli_devices}"
1619149050Spjd
1620149050Spjd	for provider in ${devices}; do
1621149050Spjd		provider=${provider%.eli}
1622149050Spjd		provider=${provider#/dev/}
1623149050Spjd		devices2="${devices2} ${provider}"
1624149050Spjd	done
1625149050Spjd
1626149050Spjd	echo ${devices2}
1627149050Spjd}
1628149050Spjd
1629153027Sdougb# Find scripts in local_startup directories that use the old syntax
1630153027Sdougb#
1631153027Sdougbfind_local_scripts_old () {
1632153027Sdougb	zlist=''
1633153027Sdougb	slist=''
1634153027Sdougb	for dir in ${local_startup}; do
1635153027Sdougb		if [ -d "${dir}" ]; then
1636153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1637153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1638153027Sdougb				    continue
1639153027Sdougb				zlist="$zlist $file"
1640153027Sdougb			done
1641153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1642153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1643153027Sdougb				    continue
1644153027Sdougb				slist="$slist $file"
1645153027Sdougb			done
1646153027Sdougb		fi
1647153027Sdougb	done
1648153027Sdougb}
1649153027Sdougb
1650153027Sdougbfind_local_scripts_new () {
1651153027Sdougb	local_rc=''
1652153027Sdougb	for dir in ${local_startup}; do
1653153027Sdougb		if [ -d "${dir}" ]; then
1654153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1655153027Sdougb				case "$file" in
1656153027Sdougb				*.sample) ;;
1657153027Sdougb				*)	if [ -x "$file" ]; then
1658153027Sdougb						local_rc="${local_rc} ${file}"
1659153027Sdougb					fi
1660153027Sdougb					;;
1661153027Sdougb				esac
1662153027Sdougb			done
1663153027Sdougb		fi
1664153027Sdougb	done
1665153027Sdougb}
1666153027Sdougb
1667165565Syar# check_required_{before|after} command
1668165565Syar#	Check for things required by the command before and after its precmd,
1669165565Syar#	respectively.  The two separate functions are needed because some
1670165565Syar#	conditions should prevent precmd from being run while other things
1671165565Syar#	depend on precmd having already been run.
1672165565Syar#
1673165565Syarcheck_required_before()
1674165565Syar{
1675165565Syar	local _f
1676165565Syar
1677165565Syar	case "$1" in
1678165565Syar	start)
1679165565Syar		for _f in $required_vars; do
1680165565Syar			if ! checkyesno $_f; then
1681165565Syar				warn "\$${_f} is not enabled."
1682165565Syar				if [ -z "$rc_force" ]; then
1683165565Syar					return 1
1684165565Syar				fi
1685165565Syar			fi
1686165565Syar		done
1687165565Syar
1688165565Syar		for _f in $required_dirs; do
1689165565Syar			if [ ! -d "${_f}/." ]; then
1690165565Syar				warn "${_f} is not a directory."
1691165565Syar				if [ -z "$rc_force" ]; then
1692165565Syar					return 1
1693165565Syar				fi
1694165565Syar			fi
1695165565Syar		done
1696165565Syar
1697165565Syar		for _f in $required_files; do
1698165565Syar			if [ ! -r "${_f}" ]; then
1699165565Syar				warn "${_f} is not readable."
1700165565Syar				if [ -z "$rc_force" ]; then
1701165565Syar					return 1
1702165565Syar				fi
1703165565Syar			fi
1704165565Syar		done
1705165565Syar		;;
1706165565Syar	esac
1707165565Syar
1708165565Syar	return 0
1709165565Syar}
1710165565Syar
1711165565Syarcheck_required_after()
1712165565Syar{
1713165565Syar	local _f _args
1714165565Syar
1715165565Syar	case "$1" in
1716165565Syar	start)
1717165565Syar		for _f in $required_modules; do
1718165565Syar			case "${_f}" in
1719165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1720165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1721165565Syar				*)	_args="${_f}" ;;
1722165565Syar			esac
1723165565Syar			if ! load_kld ${_args}; then
1724165565Syar				if [ -z "$rc_force" ]; then
1725165565Syar					return 1
1726165565Syar				fi
1727165565Syar			fi
1728165565Syar		done
1729165565Syar		;;
1730165565Syar	esac
1731165565Syar
1732165565Syar	return 0
1733165565Syar}
1734165565Syar
1735131550Scpercivafi
1736157841Sflz
1737222996Shrs# check_kern_features mib
1738222996Shrs#	Return existence of kern.features.* sysctl MIB as true or
1739222996Shrs#	false.  The result will be cached in $_rc_cache_kern_features_
1740222996Shrs#	namespace.  "0" means the kern.features.X exists.
1741222996Shrs
1742222996Shrscheck_kern_features()
1743222996Shrs{
1744222996Shrs	local _v
1745222996Shrs
1746222996Shrs	[ -n "$1" ] || return 1;
1747222996Shrs	_v=`eval echo "\\$_rc_cache_kern_features_$1"`
1748222996Shrs	[ -n "$_v" ] && return "$_v";
1749222996Shrs
1750222996Shrs	if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then
1751222996Shrs		eval _rc_cache_kern_features_$1=0
1752222996Shrs		return 0
1753222996Shrs	else
1754222996Shrs		eval _rc_cache_kern_features_$1=1
1755222996Shrs		return 1
1756222996Shrs	fi
1757222996Shrs}
1758222996Shrs
1759197144Shrs# _echoonce var msg mode
1760197144Shrs#	mode=0: Echo $msg if ${$var} is empty.
1761197144Shrs#	        After doing echo, a string is set to ${$var}.
1762197144Shrs#
1763197144Shrs#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
1764197144Shrs#
1765197144Shrs_echoonce()
1766197144Shrs{
1767197144Shrs	local _var _msg _mode
1768197144Shrs	_var=`eval echo \\$$1`
1769197144Shrs	_msg=$2
1770197144Shrs	_mode=$3
1771197144Shrs
1772197144Shrs	case $_mode in
1773197144Shrs	1)	[ -n "$_var" ] && echo "$_msg" ;;
1774197144Shrs	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1775197144Shrs	esac
1776197144Shrs}
1777197144Shrs
1778157841Sflz_rc_subr_loaded=:
1779