rc.subr revision 124832
198186Sgordon# $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 124832 2004-01-22 08:46:03Z mtm $
378344Sobrien#
498186Sgordon# Copyright (c) 1997-2002 The NetBSD Foundation, Inc.
578344Sobrien# All rights reserved.
678344Sobrien#
778344Sobrien# This code is derived from software contributed to The NetBSD Foundation
878344Sobrien# by Luke Mewburn.
978344Sobrien#
1078344Sobrien# Redistribution and use in source and binary forms, with or without
1178344Sobrien# modification, are permitted provided that the following conditions
1278344Sobrien# are met:
1378344Sobrien# 1. Redistributions of source code must retain the above copyright
1478344Sobrien#    notice, this list of conditions and the following disclaimer.
1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1678344Sobrien#    notice, this list of conditions and the following disclaimer in the
1778344Sobrien#    documentation and/or other materials provided with the distribution.
1878344Sobrien# 3. All advertising materials mentioning features or use of this software
1978344Sobrien#    must display the following acknowledgement:
2078344Sobrien#        This product includes software developed by the NetBSD
2178344Sobrien#        Foundation, Inc. and its contributors.
2278344Sobrien# 4. Neither the name of The NetBSD Foundation nor the names of its
2378344Sobrien#    contributors may be used to endorse or promote products derived
2478344Sobrien#    from this software without specific prior written permission.
2578344Sobrien#
2678344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2778344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2878344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2978344Sobrien# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3078344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3178344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3278344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3378344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3478344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3578344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3678344Sobrien# POSSIBILITY OF SUCH DAMAGE.
3778344Sobrien#
3878344Sobrien# rc.subr
3978344Sobrien#	functions used by various rc scripts
4078344Sobrien#
4178344Sobrien
4278344Sobrien#
4398186Sgordon#	Operating System dependent/independent variables
4498186Sgordon#
4598186Sgordon
4698186SgordonSYSCTL="/sbin/sysctl"
4798186SgordonSYSCTL_N="${SYSCTL} -n"
4898186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype"
49103018SgordonOSTYPE=`${CMD_OSTYPE}`
50124832SmtmID="/usr/bin/id"
51124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
5298186Sgordon
53103018Sgordoncase ${OSTYPE} in
5498186SgordonFreeBSD)
5598186Sgordon	SYSCTL_W="${SYSCTL}"
5698186Sgordon	;;
5798186SgordonNetBSD)
5898186Sgordon	SYSCTL_W="${SYSCTL} -w"
5998186Sgordon	;;
6098186Sgordonesac
6198186Sgordon
6298186Sgordon#
6378344Sobrien#	functions
6478344Sobrien#	---------
6578344Sobrien
6678344Sobrien#
6798186Sgordon# set_rcvar base_var
6898186Sgordon#	Set the variable name enabling a specific service.
6998186Sgordon#	FreeBSD uses ${service}_enable, while NetBSD uses
7098186Sgordon#	just the name of the service. For example:
7198186Sgordon#	FreeBSD: sendmail_enable="YES"
7298186Sgordon#	NetBSD : sendmail="YES"
7398186Sgordon#	$1 - if $name is not the base to work of off, specify
7498186Sgordon#	     a different one
7598186Sgordon#
7698186Sgordonset_rcvar()
7798186Sgordon{
7898186Sgordon	if [ -z "$1" ]; then
7998186Sgordon		base_var=${name}
8098186Sgordon	else
8198186Sgordon		base_var="$1"
8298186Sgordon	fi
8398186Sgordon
84103018Sgordon	case ${OSTYPE} in
8598186Sgordon	FreeBSD)
8698186Sgordon		echo ${base_var}_enable
8798186Sgordon		;;
8898186Sgordon	NetBSD)
8998186Sgordon		echo ${base_var}
9098186Sgordon		;;
9198186Sgordon	*)
9298186Sgordon		echo 'XXX'
9398186Sgordon		;;
9498186Sgordon	esac
9598186Sgordon}
9698186Sgordon
9798186Sgordon#
9898186Sgordon# force_depend script
9998186Sgordon#	Force a service to start. Intended for use by services
10098186Sgordon#	to resolve dependency issues. It is assumed the caller
10198186Sgordon#	has check to make sure this call is necessary
10298186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
10398186Sgordon#
10498186Sgordonforce_depend()
10598186Sgordon{
10698186Sgordon	_depend="$1"
10798186Sgordon
10898186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
10998186Sgordon	if ! /etc/rc.d/${_depend} forcestart ; then
11098186Sgordon		warn "Unable to force ${_depend}. It may already be running."
11198186Sgordon		return 1
11298186Sgordon	fi
11398186Sgordon	return 0
11498186Sgordon}
11598186Sgordon
11698186Sgordon#
11778344Sobrien# checkyesno var
11878344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
11978344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
12078344Sobrien#
12178344Sobriencheckyesno()
12278344Sobrien{
12378344Sobrien	eval _value=\$${1}
12498186Sgordon	debug "checkyesno: $1 is set to $_value."
12578344Sobrien	case $_value in
12678344Sobrien
12778344Sobrien		#	"yes", "true", "on", or "1"
12878344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
12978344Sobrien		return 0
13078344Sobrien		;;
13178344Sobrien
13278344Sobrien		#	"no", "false", "off", or "0"
13378344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
13478344Sobrien		return 1
13578344Sobrien		;;
13678344Sobrien	*)
137106643Sgordon		warn "\$${1} is not set properly - see rc.conf(5)."
13878344Sobrien		return 1
13978344Sobrien		;;
14078344Sobrien	esac
14178344Sobrien}
14278344Sobrien
14398186Sgordon# reverse_list list
14498186Sgordon#	print the list in reverse order
14578344Sobrien#
14698186Sgordonreverse_list()
14798186Sgordon{
14898186Sgordon	_revlist=
14998186Sgordon	for _revfile in $*; do
15098186Sgordon		_revlist="$_revfile $_revlist"
15198186Sgordon	done
15298186Sgordon	echo $_revlist
15398186Sgordon}
15498186Sgordon
15578344Sobrien#
15698186Sgordon# mount_critical_filesystems type
15798186Sgordon#	Go through the list of critical filesystems as provided in
15898186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
15998186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
16098186Sgordon#
16178344Sobrienmount_critical_filesystems()
16278344Sobrien{
16398186Sgordon	eval _fslist=\$critical_filesystems_${1}
16478344Sobrien	for _fs in $_fslist; do
16578344Sobrien		mount | (
16678344Sobrien			_ismounted=no
16778344Sobrien			while read what _on on _type type; do
16878344Sobrien				if [ $on = $_fs ]; then
16978344Sobrien					_ismounted=yes
17078344Sobrien				fi
17178344Sobrien			done
17298186Sgordon			if [ $_ismounted = no ]; then
17378344Sobrien				mount $_fs >/dev/null 2>&1
17478344Sobrien			fi
17598186Sgordon		)
17678344Sobrien	done
17778344Sobrien}
17878344Sobrien
17978344Sobrien#
18098186Sgordon# check_pidfile pidfile procname [interpreter]
18198186Sgordon#	Parses the first line of pidfile for a PID, and ensures
18278344Sobrien#	that the process is running and matches procname.
18398186Sgordon#	Prints the matching PID upon success, nothing otherwise.
18498186Sgordon#	interpreter is optional; see _find_processes() for details.
18578344Sobrien#
18678344Sobriencheck_pidfile()
18778344Sobrien{
18878344Sobrien	_pidfile=$1
18978344Sobrien	_procname=$2
19098186Sgordon	_interpreter=$3
19178344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
19298186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
19378344Sobrien	fi
19478344Sobrien	if [ ! -f $_pidfile ]; then
19598186Sgordon		debug "pid file {$_pidfile): not readable."
19678344Sobrien		return
19778344Sobrien	fi
19878344Sobrien	read _pid _junk < $_pidfile
19978344Sobrien	if [ -z "$_pid" ]; then
20098186Sgordon		debug "pid file {$_pidfile): no pid in file."
20178344Sobrien		return
20278344Sobrien	fi
20398186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
20478344Sobrien}
20578344Sobrien
20678344Sobrien#
20798186Sgordon# check_process procname [interpreter]
20878344Sobrien#	Ensures that a process (or processes) named procname is running.
20998186Sgordon#	Prints a list of matching PIDs.
21098186Sgordon#	interpreter is optional; see _find_processes() for details.
21178344Sobrien#
21278344Sobriencheck_process()
21378344Sobrien{
21478344Sobrien	_procname=$1
21598186Sgordon	_interpreter=$2
21678344Sobrien	if [ -z "$_procname" ]; then
21798186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
21878344Sobrien	fi
21998186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
22098186Sgordon}
22198186Sgordon
22298186Sgordon#
22398186Sgordon# _find_processes procname interpreter psargs
22498186Sgordon#	Search for procname in the output of ps generated by psargs.
22598186Sgordon#	Prints the PIDs of any matching processes, space separated.
22698186Sgordon#
22798186Sgordon#	If interpreter == ".", check the following variations of procname
22898186Sgordon#	against the first word of each command:
22998186Sgordon#		procname
23098186Sgordon#		`basename procname`
23198186Sgordon#		`basename procname` + ":"
23298186Sgordon#		"(" + `basename procname` + ")"
23398186Sgordon#
23498186Sgordon#	If interpreter != ".", read the first line of procname, remove the
23598186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
23698186Sgordon#	match that against each command, either as is, or with extra words
23798186Sgordon#	at the end.
23898186Sgordon#
23998186Sgordon_find_processes()
24098186Sgordon{
24198186Sgordon	if [ $# -ne 3 ]; then
24298186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
24398186Sgordon	fi
24498186Sgordon	_procname=$1
24598186Sgordon	_interpreter=$2
24698186Sgordon	_psargs=$3
24798186Sgordon
24878344Sobrien	_pref=
24998186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
25098186Sgordon		read _interp < $_procname	# read interpreter name
25198186Sgordon		_interp=${_interp#\#!}		# strip #!
25298186Sgordon		set -- $_interp
25398186Sgordon		if [ $_interpreter != $1 ]; then
25498186Sgordon			warn "\$command_interpreter $_interpreter != $1"
25578344Sobrien		fi
25698186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
25798186Sgordon		_fp_args='_argv'
25898186Sgordon		_fp_match='case "$_argv" in
25998186Sgordon		    ${_interp}|"${_interp} "*)'
26098186Sgordon	else					# a normal daemon
26198186Sgordon		_procnamebn=${_procname##*/}
26298186Sgordon		_fp_args='_arg0 _argv'
26398186Sgordon		_fp_match='case "$_arg0" in
26498186Sgordon		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
26598186Sgordon	fi
26698186Sgordon
26798186Sgordon	_proccheck='
26898186Sgordon		ps -o "pid,command" '"$_psargs"' |
26998186Sgordon		while read _npid '"$_fp_args"'; do
27098186Sgordon			case "$_npid" in
27198186Sgordon			    PID)
27298186Sgordon				continue ;;
27398186Sgordon			esac ; '"$_fp_match"'
27498186Sgordon				echo -n "$_pref$_npid" ;
27598186Sgordon				_pref=" "
27698186Sgordon				;;
27798186Sgordon			esac
27898186Sgordon		done'
27998186Sgordon
280114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
28198186Sgordon	eval $_proccheck
28298186Sgordon}
28398186Sgordon
28498186Sgordon#
28598186Sgordon# wait_for_pids pid [pid ...]
28698186Sgordon#	spins until none of the pids exist
28798186Sgordon#
28898186Sgordonwait_for_pids()
28998186Sgordon{
29098186Sgordon	_list=$*
29198186Sgordon	if [ -z "$_list" ]; then
29298186Sgordon		return
29398186Sgordon	fi
29498186Sgordon	_prefix=
29598186Sgordon	while true; do
29698186Sgordon		_nlist="";
29798186Sgordon		for _j in $_list; do
29898186Sgordon			if kill -0 $_j 2>/dev/null; then
29998186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
30098186Sgordon			fi
30198186Sgordon		done
30298186Sgordon		if [ -z "$_nlist" ]; then
30398186Sgordon			break
30478344Sobrien		fi
30598186Sgordon		_list=$_nlist
30698186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
30798186Sgordon		_prefix=", "
30898186Sgordon		sleep 2
30978344Sobrien	done
31098186Sgordon	if [ -n "$_prefix" ]; then
31198186Sgordon		echo "."
31298186Sgordon	fi
31378344Sobrien}
31478344Sobrien
31578344Sobrien#
31698186Sgordon# run_rc_command argument
31798186Sgordon#	Search for argument in the list of supported commands, which is:
31898186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
31998186Sgordon#	If there's a match, run ${argument}_cmd or the default method
32098186Sgordon#	(see below).
32178344Sobrien#
32298186Sgordon#	If argument has a given prefix, then change the operation as follows:
32398186Sgordon#		Prefix	Operation
32478344Sobrien#		------	---------
32598186Sgordon#		fast	Skip the pid check, and set rc_fast=yes
32698186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
32778344Sobrien#
32878344Sobrien#	The following globals are used:
32978344Sobrien#
33098186Sgordon#	Name		Needed	Purpose
33198186Sgordon#	----		------	-------
33278344Sobrien#	name		y	Name of script.
33378344Sobrien#
33478344Sobrien#	command		n	Full path to command.
33598186Sgordon#				Not needed if ${rc_arg}_cmd is set for
33678344Sobrien#				each keyword.
33778344Sobrien#
33878344Sobrien#	command_args	n	Optional args/shell directives for command.
33978344Sobrien#
34098186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
34198186Sgordon#				call check_{pidfile,process}() appropriately.
34298186Sgordon#
34378344Sobrien#	extra_commands	n	List of extra commands supported.
34478344Sobrien#
34598186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
34698186Sgordon#				otherwise use check_process $command.
34798186Sgordon#				In either case, only check if $command is set.
34878344Sobrien#
34998186Sgordon#	procname	n	Process name to check for instead of $command.
35098186Sgordon#
35178344Sobrien#	rcvar		n	This is checked with checkyesno to determine
35278344Sobrien#				if the action should be run.
35378344Sobrien#
35478344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
35598186Sgordon#				Requires /usr to be mounted.
35678344Sobrien#
35778344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
35878344Sobrien#				(if not using ${name}_chroot).
35978344Sobrien#
36078344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
36178344Sobrien#				NOTE:	$flags from the parent environment
36278344Sobrien#					can be used to override this.
36378344Sobrien#
36478344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
36578344Sobrien#
36678344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
36778344Sobrien#				using ${name}_chroot.
36898186Sgordon#				Requires /usr to be mounted.
36978344Sobrien#
37078344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
37198186Sgordon#				Requires /usr to be mounted.
37278344Sobrien#
37398186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
37498186Sgordon#				to run the chrooted ${command} with.
37598186Sgordon#				Requires /usr to be mounted.
37678344Sobrien#
37798186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
37878344Sobrien#				Otherwise, use default command (see below)
37978344Sobrien#
38098186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
38198186Sgordon#				${rc_arg}_cmd method in the default
38298186Sgordon#				operation (i.e, after checking for required
38398186Sgordon#				bits and process (non)existence).
38478344Sobrien#				If this completes with a non-zero exit code,
38598186Sgordon#				don't run ${rc_arg}_cmd.
38678344Sobrien#
38798186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
38898186Sgordon#				${rc_arg}_cmd method, if that method
38998186Sgordon#				returned a zero exit code.
39098186Sgordon#
39178344Sobrien#	required_dirs	n	If set, check for the existence of the given
39278344Sobrien#				directories before running the default
39378344Sobrien#				(re)start command.
39478344Sobrien#
39578344Sobrien#	required_files	n	If set, check for the readability of the given
39678344Sobrien#				files before running the default (re)start
39778344Sobrien#				command.
39878344Sobrien#
39978344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
40078344Sobrien#				listed variables before running the default
40178344Sobrien#				(re)start command.
40278344Sobrien#
40398186Sgordon#	Default behaviour for a given argument, if no override method is
40498186Sgordon#	provided:
40578344Sobrien#
40698186Sgordon#	Argument	Default behaviour
40798186Sgordon#	--------	-----------------
40878344Sobrien#	start		if !running && checkyesno ${rcvar}
40978344Sobrien#				${command}
41078344Sobrien#
41178344Sobrien#	stop		if ${pidfile}
41298186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
41378344Sobrien#			else
41498186Sgordon#				rc_pid=$(check_process $command)
41598186Sgordon#			kill $sig_stop $rc_pid
41698186Sgordon#			wait_for_pids $rc_pid
41798186Sgordon#			($sig_stop defaults to TERM.)
41878344Sobrien#
41998186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
42098186Sgordon#			and doesn't wait_for_pids.
42178344Sobrien#			$sig_reload defaults to HUP.
42278344Sobrien#
42378344Sobrien#	restart		Run `stop' then `start'.
42478344Sobrien#
42598186Sgordon#	status		Show if ${command} is running, etc.
42678344Sobrien#
42798186Sgordon#	poll		Wait for ${command} to exit.
42898186Sgordon#
42998186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
43098186Sgordon#
43198186Sgordon#	Variables available to methods, and after run_rc_command() has
43298186Sgordon#	completed:
43398186Sgordon#
43498186Sgordon#	Variable	Purpose
43598186Sgordon#	--------	-------
43698186Sgordon#	rc_arg		Argument to command, after fast/force processing
43798186Sgordon#			performed
43898186Sgordon#
43998186Sgordon#	rc_flags	Flags to start the default command with.
44098186Sgordon#			Defaults to ${name}_flags, unless overridden
44198186Sgordon#			by $flags from the environment.
44298186Sgordon#			This variable may be changed by the precmd method.
44398186Sgordon#
44498186Sgordon#	rc_pid		PID of command (if appropriate)
44598186Sgordon#
44698186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
44798186Sgordon#
44898186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
44998186Sgordon#
45098186Sgordon#
45178344Sobrienrun_rc_command()
45278344Sobrien{
453116097Smtm	_return=0
45498186Sgordon	rc_arg=$1
45578344Sobrien	if [ -z "$name" ]; then
45698186Sgordon		err 3 'run_rc_command: $name is not set.'
45778344Sobrien	fi
45878344Sobrien
45998186Sgordon	case "$rc_arg" in
46078344Sobrien	fast*)				# "fast" prefix; don't check pid
46198186Sgordon		rc_arg=${rc_arg#fast}
46298186Sgordon		rc_fast=yes
46378344Sobrien		;;
46478344Sobrien	force*)				# "force prefix; always start
46598186Sgordon		rc_arg=${rc_arg#force}
46698186Sgordon		rc_force=yes
46778344Sobrien		if [ -n "${rcvar}" ]; then
46878344Sobrien			eval ${rcvar}=YES
46978344Sobrien		fi
47078344Sobrien		;;
47178344Sobrien	esac
47278344Sobrien
47398186Sgordon	eval _overide_command=\$${name}_program
47498186Sgordon	if [ -n "$_overide_command" ]; then
47598186Sgordon		command=$_overide_command
47698186Sgordon	fi
47798186Sgordon
47878344Sobrien	_keywords="start stop restart rcvar $extra_commands"
47998186Sgordon	rc_pid=
48078344Sobrien	_pidcmd=
48198186Sgordon	_procname=${procname:-${command}}
48298186Sgordon
48378344Sobrien					# setup pid check command if not fast
48498186Sgordon	if [ -z "$rc_fast" -a -n "$_procname" ]; then
48578344Sobrien		if [ -n "$pidfile" ]; then
48698186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
48798186Sgordon		else
48898186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
48978344Sobrien		fi
49078344Sobrien		if [ -n "$_pidcmd" ]; then
49198186Sgordon			_keywords="${_keywords} status poll"
49278344Sobrien		fi
49378344Sobrien	fi
49478344Sobrien
49598186Sgordon	if [ -z "$rc_arg" ]; then
49678344Sobrien		rc_usage "$_keywords"
49778344Sobrien	fi
49878344Sobrien
49978344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
50098186Sgordon		rc_flags=$flags
50178344Sobrien	else
50298186Sgordon		eval rc_flags=\$${name}_flags
50378344Sobrien	fi
50498186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
50598186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
50698186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
50778344Sobrien
50898186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
509124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
51098186Sgordon			unset _user
51198186Sgordon		fi
51298186Sgordon	fi
51398186Sgordon
51478344Sobrien					# if ${rcvar} is set, and $1 is not
51598186Sgordon					# "rcvar", then run
51678344Sobrien					#	checkyesno ${rcvar}
51778344Sobrien					# and return if that failed
51878344Sobrien					#
51998186Sgordon	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
52078344Sobrien		if ! checkyesno ${rcvar}; then
52178344Sobrien			return 0
52278344Sobrien		fi
52378344Sobrien	fi
52478344Sobrien
52578344Sobrien	eval $_pidcmd			# determine the pid if necessary
52678344Sobrien
52778344Sobrien	for _elem in $_keywords; do
52898186Sgordon		if [ "$_elem" != "$rc_arg" ]; then
52978344Sobrien			continue
53078344Sobrien		fi
53178344Sobrien
53278344Sobrien					# if there's a custom ${XXX_cmd},
53378344Sobrien					# run that instead of the default
53478344Sobrien					#
53598186Sgordon		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
53698186Sgordon		    _postcmd=\$${rc_arg}_postcmd
53778344Sobrien		if [ -n "$_cmd" ]; then
53878344Sobrien					# if the precmd failed and force
53978344Sobrien					# isn't set, exit
54078344Sobrien					#
541116097Smtm			if [ -n "$_precmd" ]; then
542116097Smtm				debug "run_rc_command: evaluating ${_precmd}()."
543116097Smtm				eval $_precmd
544116097Smtm				_return=$?
545116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
546116097Smtm				    return 1
54778344Sobrien			fi
54878344Sobrien
549116097Smtm			if [ -n "$_cmd" ]; then
550116097Smtm				debug "run_rc_command: evaluating ${_cmd}()."
551116097Smtm				eval $_cmd
552116097Smtm				_return=$?
553116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
554116097Smtm				    return 1
55598186Sgordon			fi
556109582Smtm
557116097Smtm			if [ -n "$_postcmd" ]; then
558116097Smtm				debug "run_rc_command: evaluating ${_postcmd}()."
559116097Smtm				 eval $_postcmd
560116097Smtm				_return=$?
561116097Smtm			fi
562116097Smtm			return $_return
56378344Sobrien		fi
56478344Sobrien
56598186Sgordon		case "$rc_arg" in	# default operations...
56678344Sobrien
56778344Sobrien		status)
56898186Sgordon			if [ -n "$rc_pid" ]; then
56998186Sgordon				echo "${name} is running as pid $rc_pid."
57078344Sobrien			else
57178344Sobrien				echo "${name} is not running."
57278344Sobrien				return 1
57378344Sobrien			fi
57478344Sobrien			;;
57578344Sobrien
57678344Sobrien		start)
57798186Sgordon			if [ -n "$rc_pid" ]; then
57898186Sgordon				echo "${name} already running? (pid=$rc_pid)."
57978344Sobrien				exit 1
58078344Sobrien			fi
58178344Sobrien
58278344Sobrien			if [ ! -x $command ]; then
58398186Sgordon				info "run_rc_command: cannot run ($command)."
58478344Sobrien				return 0
58578344Sobrien			fi
58678344Sobrien
58778344Sobrien					# check for required variables,
58878344Sobrien					# directories, and files
58978344Sobrien					#
59078344Sobrien			for _f in $required_vars; do
59178344Sobrien				if ! checkyesno $_f; then
59278344Sobrien					warn "\$${_f} is not set."
59398186Sgordon					if [ -z "$rc_force" ]; then
59478344Sobrien						return 1
59578344Sobrien					fi
59678344Sobrien				fi
59778344Sobrien			done
59878344Sobrien			for _f in $required_dirs; do
59978344Sobrien				if [ ! -d "${_f}/." ]; then
60078344Sobrien					warn "${_f} is not a directory."
60198186Sgordon					if [ -z "$rc_force" ]; then
60278344Sobrien						return 1
60378344Sobrien					fi
60478344Sobrien				fi
60578344Sobrien			done
60678344Sobrien			for _f in $required_files; do
60778344Sobrien				if [ ! -r "${_f}" ]; then
60878344Sobrien					warn "${_f} is not readable."
60998186Sgordon					if [ -z "$rc_force" ]; then
61078344Sobrien						return 1
61178344Sobrien					fi
61278344Sobrien				fi
61378344Sobrien			done
61478344Sobrien
61578344Sobrien					# if the precmd failed and force
61678344Sobrien					# isn't set, exit
61778344Sobrien					#
618116097Smtm			if [ -n "${_precmd}" ]; then
619116097Smtm				debug "run_rc_command: evaluating ${_precmd}()."
620116097Smtm				eval $_precmd
621116097Smtm				_return=$?
622116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
623116097Smtm				    return 1
62478344Sobrien			fi
62578344Sobrien
62678344Sobrien					# setup the command to run, and run it
62778344Sobrien					#
62878344Sobrien			echo "Starting ${name}."
62978344Sobrien			if [ -n "$_chroot" ]; then
63078344Sobrien				_doit="\
63178344Sobrien${_nice:+nice -n $_nice }\
63278344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
63398186Sgordon$_chroot $command $rc_flags $command_args"
63478344Sobrien			else
63578344Sobrien				_doit="\
63678344Sobrien${_chdir:+cd $_chdir; }\
63778344Sobrien${_nice:+nice -n $_nice }\
63898186Sgordon$command $rc_flags $command_args"
63998186Sgordon				if [ -n "$_user" ]; then
64098186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
64198186Sgordon				fi
64278344Sobrien			fi
64398186Sgordon
64498186Sgordon					# if the cmd failed and force
64598186Sgordon					# isn't set, exit
64698186Sgordon					#
64798186Sgordon			debug "run_rc_command: _doit: $_doit"
648116097Smtm			eval $_doit
649116097Smtm			_return=$?
650116097Smtm			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
65198186Sgordon
65298186Sgordon					# finally, run postcmd
65398186Sgordon					#
654116097Smtm			if [ -n "${_postcmd}" ]; then
655116097Smtm				debug "run_rc_command: evaluating ${_postcmd}()."
656116097Smtm				eval $_postcmd
657116097Smtm			fi
65878344Sobrien			;;
65978344Sobrien
66078344Sobrien		stop)
66198186Sgordon			if [ -z "$rc_pid" ]; then
66278344Sobrien				if [ -n "$pidfile" ]; then
66378344Sobrien					echo \
66478344Sobrien				    "${name} not running? (check $pidfile)."
66578344Sobrien				else
66678344Sobrien					echo "${name} not running?"
66778344Sobrien				fi
66878344Sobrien				exit 1
66978344Sobrien			fi
67078344Sobrien
67198186Sgordon					# if the precmd failed and force
67298186Sgordon					# isn't set, exit
67398186Sgordon					#
674117977Smtm			if [ -n "$_precmd" ]; then
675116097Smtm				eval $_precmd
676116097Smtm				_return=$?
677116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
678116097Smtm				    return 1
67978344Sobrien			fi
68098186Sgordon
68198186Sgordon					# send the signal to stop
68298186Sgordon					#
68378344Sobrien			echo "Stopping ${name}."
68498186Sgordon			_doit="kill -${sig_stop:-TERM} $rc_pid"
68598186Sgordon			if [ -n "$_user" ]; then
68698186Sgordon				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
68798186Sgordon			fi
68898186Sgordon
68998186Sgordon					# if the stop cmd failed and force
69098186Sgordon					# isn't set, exit
69198186Sgordon					#
692116097Smtm			eval $_doit
693116097Smtm			_return=$?
694116097Smtm			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
69598186Sgordon
69698186Sgordon					# wait for the command to exit,
69798186Sgordon					# and run postcmd.
69898186Sgordon			wait_for_pids $rc_pid
699116097Smtm			if [ -n "$_postcmd" ]; then
700116097Smtm				eval $_postcmd
701116097Smtm				_return=$?
702116097Smtm			fi
70378344Sobrien			;;
70478344Sobrien
70578344Sobrien		reload)
70698186Sgordon			if [ -z "$rc_pid" ]; then
70778344Sobrien				if [ -n "$pidfile" ]; then
70878344Sobrien					echo \
70978344Sobrien				    "${name} not running? (check $pidfile)."
71078344Sobrien				else
71178344Sobrien					echo "${name} not running?"
71278344Sobrien				fi
71378344Sobrien				exit 1
71478344Sobrien			fi
71578344Sobrien			echo "Reloading ${name} config files."
716116097Smtm			if [ -n "$_precmd" ]; then
717116097Smtm				eval $_precmd
718116097Smtm				_return=$?
719116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
720116097Smtm				    return 1
72178344Sobrien			fi
72298186Sgordon			_doit="kill -${sig_reload:-HUP} $rc_pid"
72398186Sgordon			if [ -n "$_user" ]; then
72498186Sgordon				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
72598186Sgordon			fi
726116097Smtm			eval $_doit
727116097Smtm			_return=$?
728116097Smtm			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
729116097Smtm			if [ -n "$_postcmd" ]; then
730116097Smtm				eval $_postcmd
731116097Smtm				_return=$?
73298186Sgordon			fi
73378344Sobrien			;;
73478344Sobrien
73578344Sobrien		restart)
736116097Smtm			if [ -n "$_precmd" ]; then
737116097Smtm				eval $_precmd
738116097Smtm				_return=$?
739116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
740116097Smtm				    return 1
74178344Sobrien			fi
74278344Sobrien					# prevent restart being called more
74378344Sobrien					# than once by any given script
74478344Sobrien					#
74578344Sobrien			if [ -n "$_rc_restart_done" ]; then
74678344Sobrien				return 0
74778344Sobrien			fi
74878344Sobrien			_rc_restart_done=YES
74978344Sobrien
75098186Sgordon			( $0 ${rc_force:+force}stop )
75198186Sgordon			$0 ${rc_force:+force}start
75298186Sgordon
753116097Smtm			if [ -n "$_postcmd" ]; then
754116097Smtm				eval $_postcmd
755116097Smtm				_return=$?
756116097Smtm			fi
75778344Sobrien			;;
75878344Sobrien
75998186Sgordon		poll)
76098186Sgordon			if [ -n "$rc_pid" ]; then
76198186Sgordon				wait_for_pids $rc_pid
76298186Sgordon			fi
76398186Sgordon			;;
76498186Sgordon
76578344Sobrien		rcvar)
76678344Sobrien			echo "# $name"
76778344Sobrien			if [ -n "$rcvar" ]; then
76878344Sobrien				if checkyesno ${rcvar}; then
76978344Sobrien					echo "\$${rcvar}=YES"
77078344Sobrien				else
77178344Sobrien					echo "\$${rcvar}=NO"
77278344Sobrien				fi
77378344Sobrien			fi
77478344Sobrien			;;
77578344Sobrien
77678344Sobrien		*)
77778344Sobrien			rc_usage "$_keywords"
77878344Sobrien			;;
77978344Sobrien
78078344Sobrien		esac
781116097Smtm		return $_return
78278344Sobrien	done
78378344Sobrien
78498186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
78578344Sobrien	rc_usage "$_keywords"
78678344Sobrien	exit 1
78778344Sobrien}
78878344Sobrien
78978344Sobrien#
79078344Sobrien# run_rc_script file arg
79178344Sobrien#	Start the script `file' with `arg', and correctly handle the
79278344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
79398186Sgordon#	sourced into the current environment.  If `file' appears to be
79498186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
79598186Sgordon#	executable run as a child process.
79678344Sobrien#
79778344Sobrienrun_rc_script()
79878344Sobrien{
79978344Sobrien	_file=$1
80078344Sobrien	_arg=$2
80178344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
80278344Sobrien		err 3 'USAGE: run_rc_script file arg'
80378344Sobrien	fi
80478344Sobrien
80598186Sgordon	trap "echo 'Reboot interrupted'; exit 1" 3
80698186Sgordon
80798186Sgordon	unset	name command command_args command_interpreter \
80898186Sgordon		extra_commands pidfile procname \
80998186Sgordon		rcvar required_dirs required_files required_vars
81098186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
81198186Sgordon
81278344Sobrien	case "$_file" in
81378344Sobrien	*.sh)				# run in current shell
81478344Sobrien		set $_arg ; . $_file
81578344Sobrien		;;
81698186Sgordon	*[~#]|*.OLD|*.orig)		# scratch file; skip
81798186Sgordon		warn "Ignoring scratch file $_file"
81898186Sgordon		;;
81978344Sobrien	*)				# run in subshell
82098186Sgordon		if [ -x $_file ]; then
82198186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
82298186Sgordon				set $_arg ; . $_file
82398186Sgordon			else
82498186Sgordon				( trap "echo 'Reboot interrupted'; exit 1" 3
82598186Sgordon				  set $_arg ; . $_file )
82698186Sgordon			fi
82798186Sgordon		fi
82878344Sobrien		;;
82978344Sobrien	esac
83078344Sobrien}
83178344Sobrien
83278344Sobrien#
83378344Sobrien# load_rc_config
83478344Sobrien#	Source in the configuration file for a given command.
83578344Sobrien#
83678344Sobrienload_rc_config()
83778344Sobrien{
83878344Sobrien	_command=$1
83978344Sobrien	if [ -z "$_command" ]; then
84078344Sobrien		err 3 'USAGE: load_rc_config command'
84178344Sobrien	fi
84278344Sobrien
84398186Sgordon	if [ -z "$_rc_conf_loaded" ]; then
84498186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
84598186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
84698186Sgordon			. /etc/defaults/rc.conf
84798186Sgordon			source_rc_confs
84898186Sgordon		elif [ -r /etc/rc.conf ]; then
84998186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
85098186Sgordon			. /etc/rc.conf
85198186Sgordon		fi
85298186Sgordon		_rc_conf_loaded=YES
85398186Sgordon	fi
85478344Sobrien	if [ -f /etc/rc.conf.d/"$_command" ]; then
85598186Sgordon		debug "Sourcing /etc/rc.conf.d/${_command}"
85678344Sobrien		. /etc/rc.conf.d/"$_command"
85778344Sobrien	fi
858101850Sgordon
859101850Sgordon	# XXX - Deprecated variable name support
860101850Sgordon	#
861103018Sgordon	case ${OSTYPE} in
862101850Sgordon	FreeBSD)
863101850Sgordon        	[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
864101850Sgordon        	[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
865101850Sgordon        	[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
866101850Sgordon        	[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
867101850Sgordon        	[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
868101850Sgordon        	[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
869101850Sgordon        	[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
870115950Smtm		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
871115950Smtm		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
872101850Sgordon        	;;
873101850Sgordon	esac
874101850Sgordon
87578344Sobrien}
87678344Sobrien
87778344Sobrien#
87878344Sobrien# rc_usage commands
87978344Sobrien#	Print a usage string for $0, with `commands' being a list of
88078344Sobrien#	valid commands.
88178344Sobrien#
88278344Sobrienrc_usage()
88378344Sobrien{
884106643Sgordon	echo -n 1>&2 "Usage: $0 [fast|force]("
88578344Sobrien
88678344Sobrien	_sep=
88778344Sobrien	for _elem in $*; do
88878344Sobrien		echo -n 1>&2 "$_sep$_elem"
88978344Sobrien		_sep="|"
89078344Sobrien	done
89178344Sobrien	echo 1>&2 ")"
89278344Sobrien	exit 1
89378344Sobrien}
89478344Sobrien
89578344Sobrien#
89678344Sobrien# err exitval message
89778344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
89878344Sobrien#
89978344Sobrienerr()
90078344Sobrien{
90178344Sobrien	exitval=$1
90278344Sobrien	shift
90378344Sobrien
904106643Sgordon	if [ -x /usr/bin/logger ]; then
905106643Sgordon		logger "$0: ERROR: $*"
906106643Sgordon	fi
907106643Sgordon	echo 1>&2 "$0: ERROR: $*"
90878344Sobrien	exit $exitval
90978344Sobrien}
91078344Sobrien
91178344Sobrien#
91278344Sobrien# warn message
91378344Sobrien#	Display message to stderr and log to the syslog.
91478344Sobrien#
91578344Sobrienwarn()
91678344Sobrien{
917106643Sgordon	if [ -x /usr/bin/logger ]; then
918106643Sgordon		logger "$0: WARNING: $*"
919106643Sgordon	fi
920106643Sgordon	echo 1>&2 "$0: WARNING: $*"
92178344Sobrien}
92298186Sgordon
92398186Sgordon#
92498186Sgordon# info message
92598186Sgordon#	Display informational message to stdout and log to syslog.
92698186Sgordon#
92798186Sgordoninfo()
92898186Sgordon{
929119170Smtm	case ${rc_info} in
930119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
931119170Smtm		if [ -x /usr/bin/logger ]; then
932119170Smtm			logger "$0: INFO: $*"
933119170Smtm		fi
934119170Smtm		echo "$0: INFO: $*"
935119170Smtm		;;
936119170Smtm	esac
93798186Sgordon}
93898186Sgordon
93998186Sgordon#
94098186Sgordon# debug message
941106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
94298186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
94398186Sgordon#	function.
94498186Sgordon#
94598186Sgordondebug()
94698186Sgordon{
94798186Sgordon	case ${rc_debug} in
94898186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
949106700Sgordon		if [ -x /usr/bin/logger ]; then
950106700Sgordon			logger "$0: INFO: $*"
951106700Sgordon		fi
952106643Sgordon        	echo 1>&2 "$0: DEBUG: $*"
95398186Sgordon		;;
95498186Sgordon	esac
95598186Sgordon}
95698186Sgordon
95798186Sgordon#
95898186Sgordon# backup_file action file cur backup
95998186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
96098186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
96198186Sgordon#
96298186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
96398186Sgordon#	which can be either YES or NO.
96498186Sgordon#
96598186Sgordon#	The `action' keyword can be one of the following:
96698186Sgordon#
96798186Sgordon#	add		`file' is now being backed up (and is possibly
96898186Sgordon#			being reentered into the backups system).  `cur'
96998186Sgordon#			is created and RCS files, if necessary, are
97098186Sgordon#			created as well.
97198186Sgordon#
97298186Sgordon#	update		`file' has changed and needs to be backed up.
97398186Sgordon#			If `cur' exists, it is copied to to `back' or
97498186Sgordon#			checked into RCS (if the repository file is old),
97598186Sgordon#			and then `file' is copied to `cur'.  Another RCS
97698186Sgordon#			check in done here if RCS is being used.
97798186Sgordon#
97898186Sgordon#	remove		`file' is no longer being tracked by the backups
97998186Sgordon#			system.  If RCS is not being used, `cur' is moved
98098186Sgordon#			to `back', otherwise an empty file is checked in,
98198186Sgordon#			and then `cur' is removed.
98298186Sgordon#
98398186Sgordon#
98498186Sgordonbackup_file()
98598186Sgordon{
98698186Sgordon	_action=$1
98798186Sgordon	_file=$2
98898186Sgordon	_cur=$3
98998186Sgordon	_back=$4
99098186Sgordon
99198186Sgordon	if checkyesno backup_uses_rcs; then
99298186Sgordon		_msg0="backup archive"
99398186Sgordon		_msg1="update"
99498186Sgordon
99598186Sgordon		# ensure that history file is not locked
99698186Sgordon		if [ -f $_cur,v ]; then
99798186Sgordon			rcs -q -u -U -M $_cur
99898186Sgordon		fi
99998186Sgordon
100098186Sgordon		# ensure after switching to rcs that the
100198186Sgordon		# current backup is not lost
100298186Sgordon		if [ -f $_cur ]; then
100398186Sgordon			# no archive, or current newer than archive
100498186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
100598186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
100698186Sgordon				rcs -q -kb -U $_cur
100798186Sgordon				co -q -f -u $_cur
100898186Sgordon			fi
100998186Sgordon		fi
101098186Sgordon
101198186Sgordon		case $_action in
101298186Sgordon		add|update)
101398186Sgordon			cp -p $_file $_cur
101498186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
101598186Sgordon			rcs -q -kb -U $_cur
101698186Sgordon			co -q -f -u $_cur
101798186Sgordon			chown root:wheel $_cur $_cur,v
101898186Sgordon			;;
101998186Sgordon		remove)
102098186Sgordon			cp /dev/null $_cur
102198186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
102298186Sgordon			rcs -q -kb -U $_cur
102398186Sgordon			chown root:wheel $_cur $_cur,v
102498186Sgordon			rm $_cur
102598186Sgordon			;;
102698186Sgordon		esac
102798186Sgordon	else
102898186Sgordon		case $_action in
102998186Sgordon		add|update)
103098186Sgordon			if [ -f $_cur ]; then
103198186Sgordon				cp -p $_cur $_back
103298186Sgordon			fi
103398186Sgordon			cp -p $_file $_cur
103498186Sgordon			chown root:wheel $_cur
103598186Sgordon			;;
103698186Sgordon		remove)
103798186Sgordon			mv -f $_cur $_back
103898186Sgordon			;;
103998186Sgordon		esac
104098186Sgordon	fi
104198186Sgordon}
1042119166Smtm
1043123344Smtm# make_symlink src link
1044123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1045123344Smtm#	directory in which link is to be created does not exist
1046123344Smtm#	a warning will be displayed and an error will be returned.
1047123344Smtm#	Returns 0 on sucess, 1 otherwise.
1048119166Smtm#
1049123344Smtmmake_symlink()
1050119166Smtm{
1051123344Smtm	local src link linkdir _me
1052123344Smtm	src="$1"
1053123344Smtm	link="$2"
1054123344Smtm	linkdir="`dirname $link`"
1055123344Smtm	_me="make_symlink()"
1056119166Smtm
1057123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1058123344Smtm		warn "$_me: requires two arguments."
1059119166Smtm		return 1
1060119166Smtm	fi
1061123344Smtm	if [ ! -d "$linkdir" ]; then
1062123344Smtm		warn "$_me: the directory $linkdir does not exist"
1063119166Smtm		return 1
1064119166Smtm	fi
1065119166Smtm	if ! ln -sf $src $link ; then
1066123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1067119166Smtm		return 1
1068119166Smtm	fi
1069119166Smtm	return 0
1070119166Smtm}
1071119166Smtm
1072119166Smtm# devfs_rulesets_from_file file
1073119166Smtm#	Reads a set of devfs commands from file, and creates
1074119166Smtm#	the specified rulesets with their rules. Returns non-zero
1075119166Smtm#	if there was an error.
1076119166Smtm#
1077119166Smtmdevfs_rulesets_from_file()
1078119166Smtm{
1079119166Smtm	local file _err _me
1080119166Smtm	file="$1"
1081119166Smtm	_me="devfs_rulesets_from_file"
1082119166Smtm	_err=0
1083119166Smtm
1084119166Smtm	if [ -z "$file" ]; then
1085119166Smtm		warn "$_me: you must specify a file"
1086119166Smtm		return 1
1087119166Smtm	fi
1088119166Smtm	if [ ! -e "$file" ]; then
1089119166Smtm		debug "$_me: no such file ($file)"
1090119166Smtm		return 0
1091119166Smtm	fi
1092119166Smtm	debug "reading rulesets from file ($file)"
1093119166Smtm	{ while read line
1094119166Smtm	do
1095119166Smtm		case $line in
1096119166Smtm		\#*)
1097119166Smtm			continue
1098119166Smtm			;;
1099119166Smtm		\[*\]*)
1100119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1101119166Smtm			if [ -z "$rulenum" ]; then
1102119166Smtm				warn "$_me: cannot extract rule number ($line)"
1103119166Smtm				_err=1
1104119166Smtm				break
1105119166Smtm			fi
1106119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1107119166Smtm			if [ -z "$rulename" ]; then
1108119166Smtm				warn "$_me: cannot extract rule name ($line)"
1109119166Smtm				_err=1
1110119166Smtm				break;
1111119166Smtm			fi
1112119166Smtm			eval $rulename=\$rulenum
1113119166Smtm			debug "found ruleset: $rulename=$rulenum"
1114119166Smtm			if ! /sbin/devfs rule -s $rulenum delset ; then
1115119166Smtm				_err=1
1116119166Smtm				break
1117119166Smtm			fi
1118119166Smtm			;;
1119119166Smtm		*)
1120119166Smtm			rulecmd="${line%%"\#*"}"
1121119166Smtm			# evaluate the command incase it includes
1122119166Smtm			# other rules
1123119166Smtm			if [ -n "$rulecmd" ]; then
1124119166Smtm				debug "adding rule ($rulecmd)"
1125119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1126119166Smtm				then
1127119166Smtm					_err=1
1128119166Smtm					break
1129119166Smtm				fi
1130119166Smtm			fi
1131119166Smtm			;;
1132119166Smtm		esac
1133119166Smtm		if [ $_err -ne 0 ]; then
1134119166Smtm			debug "error in $_me"
1135119166Smtm			break
1136119166Smtm		fi
1137119166Smtm	done } < $file
1138119166Smtm	return $_err
1139119166Smtm}
1140119166Smtm
1141119166Smtm# devfs_init_rulesets
1142119166Smtm#	Initializes rulesets from configuration files. Returns
1143119166Smtm#	non-zero if there was an error.
1144119166Smtm#
1145119166Smtmdevfs_init_rulesets()
1146119166Smtm{
1147119166Smtm	local file _me
1148119166Smtm	_me="devfs_init_rulesets"
1149119166Smtm
1150119166Smtm	# Go through this only once
1151119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1152119166Smtm		debug "$_me: devfs rulesets already initialized"
1153119166Smtm		return
1154119166Smtm	fi
1155119166Smtm	for file in $devfs_rulesets ; do
1156119166Smtm		devfs_rulesets_from_file $file || return 1
1157119166Smtm	done
1158119166Smtm	devfs_rulesets_init=1
1159119166Smtm	debug "$_me: devfs rulesets initialized"
1160119166Smtm	return 0
1161119166Smtm}
1162119166Smtm
1163119166Smtm# devfs_set_ruleset ruleset [dir]
1164119166Smtm#	Sets the default ruleset of dir to ruleset. The ruleset arguement
1165119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1166119166Smtm#	Returns non-zero if it could not set it successfully.
1167119166Smtm#
1168119166Smtmdevfs_set_ruleset()
1169119166Smtm{
1170119166Smtm	local devdir rs _me
1171119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1172119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1173119166Smtm	_me="devfs_set_ruleset"
1174119166Smtm
1175119166Smtm	if [ -z "$rs" ]; then
1176119166Smtm		warn "$_me: you must specify a ruleset number"
1177119166Smtm		return 1
1178119166Smtm	fi
1179119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1180119166Smtm	if ! /sbin/devfs $devdir ruleset $rs ; then
1181119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1182119166Smtm		return 1
1183119166Smtm	fi
1184119166Smtm	return 0
1185119166Smtm}
1186119166Smtm
1187119166Smtm# devfs_apply_ruleset ruleset [dir]
1188119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1189119166Smtm#	The ruleset argument must be a ruleset name as specified
1190119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1191119166Smtm#	if it could not apply the ruleset.
1192119166Smtm#
1193119166Smtmdevfs_apply_ruleset()
1194119166Smtm{
1195119166Smtm	local devdir rs _me
1196119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1197119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1198119166Smtm	_me="devfs_apply_ruleset"
1199119166Smtm
1200119166Smtm	if [ -z "$rs" ]; then
1201119166Smtm		warn "$_me: you must specify a ruleset"
1202119166Smtm		return 1
1203119166Smtm	fi
1204119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1205119166Smtm	if ! /sbin/devfs $devdir rule -s $rs applyset ; then
1206119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1207119166Smtm		return 1
1208119166Smtm	fi
1209119166Smtm	return 0
1210119166Smtm}
1211119166Smtm
1212119166Smtm# devfs_domount dir [ruleset]
1213119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1214119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1215119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1216119166Smtm#
1217119166Smtmdevfs_domount()
1218119166Smtm{
1219119166Smtm	local devdir rs _me
1220119166Smtm	devdir="$1"
1221119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1222119166Smtm	_me="devfs_domount()"
1223119166Smtm
1224119166Smtm	if [ -z "$devdir" ]; then
1225119166Smtm		warn "$_me: you must specify a mount-point"
1226119166Smtm		return 1
1227119166Smtm	fi
1228119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1229119166Smtm	if ! mount -t devfs dev "$devdir" ; then
1230119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1231119166Smtm		return 1
1232119166Smtm	fi
1233119166Smtm	if [ -n "$rs" ]; then
1234119166Smtm		devfs_init_rulesets
1235119166Smtm		devfs_set_ruleset $rs $devdir
1236124797Scperciva		devfs -m $devdir rule applyset
1237119166Smtm	fi
1238119166Smtm	return 0
1239119166Smtm}
1240119166Smtm
1241119166Smtm# devfs_mount_jail dir [ruleset]
1242119166Smtm#	Mounts a devfs file system appropriate for jails
1243119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1244119166Smtm#	it names will be used instead.  If present, ruleset must
1245119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1246119166Smtm#	This function returns non-zero if an error occurs.
1247119166Smtm#
1248119166Smtmdevfs_mount_jail()
1249119166Smtm{
1250119166Smtm	local jdev rs _me
1251119166Smtm	jdev="$1"
1252119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1253119166Smtm	_me="devfs_mount_jail"
1254119166Smtm
1255119166Smtm	devfs_init_rulesets
1256119166Smtm	if ! devfs_domount "$jdev" $rs ; then
1257119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1258119166Smtm		return 1
1259119166Smtm	fi
1260119166Smtm	return 0
1261119166Smtm}
1262