rc.subr revision 149050
1126288Smtm# $NetBSD: rc.subr,v 1.60 2003/07/26 05:13:47 lukem Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 149050 2005-08-14 18:02:22Z pjd $
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
46131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then
47131550Scperciva
48131550Scperciva_rc_subr_loaded="YES"
49131550Scperciva
5098186SgordonSYSCTL="/sbin/sysctl"
5198186SgordonSYSCTL_N="${SYSCTL} -n"
5298186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype"
53103018SgordonOSTYPE=`${CMD_OSTYPE}`
54124832SmtmID="/usr/bin/id"
55124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
5698186Sgordon
57103018Sgordoncase ${OSTYPE} in
5898186SgordonFreeBSD)
5998186Sgordon	SYSCTL_W="${SYSCTL}"
6098186Sgordon	;;
6198186SgordonNetBSD)
6298186Sgordon	SYSCTL_W="${SYSCTL} -w"
6398186Sgordon	;;
6498186Sgordonesac
6598186Sgordon
6698186Sgordon#
6778344Sobrien#	functions
6878344Sobrien#	---------
6978344Sobrien
7078344Sobrien#
7198186Sgordon# set_rcvar base_var
7298186Sgordon#	Set the variable name enabling a specific service.
7398186Sgordon#	FreeBSD uses ${service}_enable, while NetBSD uses
7498186Sgordon#	just the name of the service. For example:
7598186Sgordon#	FreeBSD: sendmail_enable="YES"
7698186Sgordon#	NetBSD : sendmail="YES"
7798186Sgordon#	$1 - if $name is not the base to work of off, specify
7898186Sgordon#	     a different one
7998186Sgordon#
8098186Sgordonset_rcvar()
8198186Sgordon{
8298186Sgordon	if [ -z "$1" ]; then
8398186Sgordon		base_var=${name}
8498186Sgordon	else
8598186Sgordon		base_var="$1"
8698186Sgordon	fi
8798186Sgordon
88103018Sgordon	case ${OSTYPE} in
8998186Sgordon	FreeBSD)
9098186Sgordon		echo ${base_var}_enable
9198186Sgordon		;;
9298186Sgordon	NetBSD)
9398186Sgordon		echo ${base_var}
9498186Sgordon		;;
9598186Sgordon	*)
9698186Sgordon		echo 'XXX'
9798186Sgordon		;;
9898186Sgordon	esac
9998186Sgordon}
10098186Sgordon
10198186Sgordon#
10298186Sgordon# force_depend script
10398186Sgordon#	Force a service to start. Intended for use by services
10498186Sgordon#	to resolve dependency issues. It is assumed the caller
10598186Sgordon#	has check to make sure this call is necessary
10698186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
10798186Sgordon#
10898186Sgordonforce_depend()
10998186Sgordon{
11098186Sgordon	_depend="$1"
11198186Sgordon
11298186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
113146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
11498186Sgordon		warn "Unable to force ${_depend}. It may already be running."
11598186Sgordon		return 1
11698186Sgordon	fi
11798186Sgordon	return 0
11898186Sgordon}
11998186Sgordon
12098186Sgordon#
12178344Sobrien# checkyesno var
12278344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
12378344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
12478344Sobrien#
12578344Sobriencheckyesno()
12678344Sobrien{
12778344Sobrien	eval _value=\$${1}
12898186Sgordon	debug "checkyesno: $1 is set to $_value."
12978344Sobrien	case $_value in
13078344Sobrien
13178344Sobrien		#	"yes", "true", "on", or "1"
13278344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
13378344Sobrien		return 0
13478344Sobrien		;;
13578344Sobrien
13678344Sobrien		#	"no", "false", "off", or "0"
13778344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
13878344Sobrien		return 1
13978344Sobrien		;;
14078344Sobrien	*)
141106643Sgordon		warn "\$${1} is not set properly - see rc.conf(5)."
14278344Sobrien		return 1
14378344Sobrien		;;
14478344Sobrien	esac
14578344Sobrien}
14678344Sobrien
14798186Sgordon# reverse_list list
14898186Sgordon#	print the list in reverse order
14978344Sobrien#
15098186Sgordonreverse_list()
15198186Sgordon{
15298186Sgordon	_revlist=
153126286Smtm	for _revfile; do
15498186Sgordon		_revlist="$_revfile $_revlist"
15598186Sgordon	done
15698186Sgordon	echo $_revlist
15798186Sgordon}
15898186Sgordon
15978344Sobrien#
16098186Sgordon# mount_critical_filesystems type
16198186Sgordon#	Go through the list of critical filesystems as provided in
16298186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
16398186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
16498186Sgordon#
16578344Sobrienmount_critical_filesystems()
16678344Sobrien{
16798186Sgordon	eval _fslist=\$critical_filesystems_${1}
16878344Sobrien	for _fs in $_fslist; do
16978344Sobrien		mount | (
170126285Smtm			_ismounted=false
17178344Sobrien			while read what _on on _type type; do
17278344Sobrien				if [ $on = $_fs ]; then
173126285Smtm					_ismounted=true
17478344Sobrien				fi
17578344Sobrien			done
176126285Smtm			if $_ismounted; then
177126285Smtm				:
178126285Smtm			else
17978344Sobrien				mount $_fs >/dev/null 2>&1
18078344Sobrien			fi
18198186Sgordon		)
18278344Sobrien	done
18378344Sobrien}
18478344Sobrien
18578344Sobrien#
18698186Sgordon# check_pidfile pidfile procname [interpreter]
18798186Sgordon#	Parses the first line of pidfile for a PID, and ensures
18878344Sobrien#	that the process is running and matches procname.
18998186Sgordon#	Prints the matching PID upon success, nothing otherwise.
19098186Sgordon#	interpreter is optional; see _find_processes() for details.
19178344Sobrien#
19278344Sobriencheck_pidfile()
19378344Sobrien{
19478344Sobrien	_pidfile=$1
19578344Sobrien	_procname=$2
19698186Sgordon	_interpreter=$3
19778344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
19898186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
19978344Sobrien	fi
20078344Sobrien	if [ ! -f $_pidfile ]; then
201131061Smtm		debug "pid file ($_pidfile): not readable."
20278344Sobrien		return
20378344Sobrien	fi
20478344Sobrien	read _pid _junk < $_pidfile
20578344Sobrien	if [ -z "$_pid" ]; then
206139949Skeramida		debug "pid file ($_pidfile): no pid in file."
20778344Sobrien		return
20878344Sobrien	fi
20998186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
21078344Sobrien}
21178344Sobrien
21278344Sobrien#
21398186Sgordon# check_process procname [interpreter]
21478344Sobrien#	Ensures that a process (or processes) named procname is running.
21598186Sgordon#	Prints a list of matching PIDs.
21698186Sgordon#	interpreter is optional; see _find_processes() for details.
21778344Sobrien#
21878344Sobriencheck_process()
21978344Sobrien{
22078344Sobrien	_procname=$1
22198186Sgordon	_interpreter=$2
22278344Sobrien	if [ -z "$_procname" ]; then
22398186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
22478344Sobrien	fi
22598186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
22698186Sgordon}
22798186Sgordon
22898186Sgordon#
22998186Sgordon# _find_processes procname interpreter psargs
23098186Sgordon#	Search for procname in the output of ps generated by psargs.
23198186Sgordon#	Prints the PIDs of any matching processes, space separated.
23298186Sgordon#
23398186Sgordon#	If interpreter == ".", check the following variations of procname
23498186Sgordon#	against the first word of each command:
23598186Sgordon#		procname
23698186Sgordon#		`basename procname`
23798186Sgordon#		`basename procname` + ":"
23898186Sgordon#		"(" + `basename procname` + ")"
23998186Sgordon#
24098186Sgordon#	If interpreter != ".", read the first line of procname, remove the
24198186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
24298186Sgordon#	match that against each command, either as is, or with extra words
24398186Sgordon#	at the end.
24498186Sgordon#
24598186Sgordon_find_processes()
24698186Sgordon{
24798186Sgordon	if [ $# -ne 3 ]; then
24898186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
24998186Sgordon	fi
25098186Sgordon	_procname=$1
25198186Sgordon	_interpreter=$2
25298186Sgordon	_psargs=$3
25398186Sgordon
25478344Sobrien	_pref=
25598186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
25698186Sgordon		read _interp < $_procname	# read interpreter name
25798186Sgordon		_interp=${_interp#\#!}		# strip #!
25898186Sgordon		set -- $_interp
25998186Sgordon		if [ $_interpreter != $1 ]; then
26098186Sgordon			warn "\$command_interpreter $_interpreter != $1"
26178344Sobrien		fi
26298186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
26398186Sgordon		_fp_args='_argv'
26498186Sgordon		_fp_match='case "$_argv" in
26598186Sgordon		    ${_interp}|"${_interp} "*)'
26698186Sgordon	else					# a normal daemon
26798186Sgordon		_procnamebn=${_procname##*/}
26898186Sgordon		_fp_args='_arg0 _argv'
26998186Sgordon		_fp_match='case "$_arg0" in
27098186Sgordon		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
27198186Sgordon	fi
27298186Sgordon
27398186Sgordon	_proccheck='
274126556Smtm		ps 2>/dev/null -o "pid,command" '"$_psargs"' |
27598186Sgordon		while read _npid '"$_fp_args"'; do
27698186Sgordon			case "$_npid" in
277146490Sschweikh			PID)
278146490Sschweikh				continue;;
279146490Sschweikh			esac; '"$_fp_match"'
280146490Sschweikh				echo -n "$_pref$_npid";
28198186Sgordon				_pref=" "
28298186Sgordon				;;
28398186Sgordon			esac
28498186Sgordon		done'
28598186Sgordon
286114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
28798186Sgordon	eval $_proccheck
28898186Sgordon}
28998186Sgordon
29098186Sgordon#
29198186Sgordon# wait_for_pids pid [pid ...]
29298186Sgordon#	spins until none of the pids exist
29398186Sgordon#
29498186Sgordonwait_for_pids()
29598186Sgordon{
296126286Smtm	_list="$@"
29798186Sgordon	if [ -z "$_list" ]; then
29898186Sgordon		return
29998186Sgordon	fi
30098186Sgordon	_prefix=
30198186Sgordon	while true; do
30298186Sgordon		_nlist="";
30398186Sgordon		for _j in $_list; do
30498186Sgordon			if kill -0 $_j 2>/dev/null; then
30598186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
30698186Sgordon			fi
30798186Sgordon		done
30898186Sgordon		if [ -z "$_nlist" ]; then
30998186Sgordon			break
31078344Sobrien		fi
31198186Sgordon		_list=$_nlist
31298186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
31398186Sgordon		_prefix=", "
31498186Sgordon		sleep 2
31578344Sobrien	done
31698186Sgordon	if [ -n "$_prefix" ]; then
31798186Sgordon		echo "."
31898186Sgordon	fi
31978344Sobrien}
32078344Sobrien
32178344Sobrien#
32298186Sgordon# run_rc_command argument
32398186Sgordon#	Search for argument in the list of supported commands, which is:
32498186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
32598186Sgordon#	If there's a match, run ${argument}_cmd or the default method
32698186Sgordon#	(see below).
32778344Sobrien#
32898186Sgordon#	If argument has a given prefix, then change the operation as follows:
32998186Sgordon#		Prefix	Operation
33078344Sobrien#		------	---------
33198186Sgordon#		fast	Skip the pid check, and set rc_fast=yes
33298186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
333126303Smtm#		one	Set ${rcvar} to YES
33478344Sobrien#
33578344Sobrien#	The following globals are used:
33678344Sobrien#
33798186Sgordon#	Name		Needed	Purpose
33898186Sgordon#	----		------	-------
33978344Sobrien#	name		y	Name of script.
34078344Sobrien#
34178344Sobrien#	command		n	Full path to command.
34298186Sgordon#				Not needed if ${rc_arg}_cmd is set for
34378344Sobrien#				each keyword.
34478344Sobrien#
34578344Sobrien#	command_args	n	Optional args/shell directives for command.
34678344Sobrien#
34798186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
34898186Sgordon#				call check_{pidfile,process}() appropriately.
34998186Sgordon#
35078344Sobrien#	extra_commands	n	List of extra commands supported.
35178344Sobrien#
35298186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
35398186Sgordon#				otherwise use check_process $command.
35498186Sgordon#				In either case, only check if $command is set.
35578344Sobrien#
35698186Sgordon#	procname	n	Process name to check for instead of $command.
35798186Sgordon#
35878344Sobrien#	rcvar		n	This is checked with checkyesno to determine
35978344Sobrien#				if the action should be run.
36078344Sobrien#
36178344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
36298186Sgordon#				Requires /usr to be mounted.
36378344Sobrien#
36478344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
36578344Sobrien#				(if not using ${name}_chroot).
36678344Sobrien#
36778344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
36878344Sobrien#				NOTE:	$flags from the parent environment
36978344Sobrien#					can be used to override this.
37078344Sobrien#
37178344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
37278344Sobrien#
37378344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
37478344Sobrien#				using ${name}_chroot.
37598186Sgordon#				Requires /usr to be mounted.
37678344Sobrien#
37778344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
37898186Sgordon#				Requires /usr to be mounted.
37978344Sobrien#
38098186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
38198186Sgordon#				to run the chrooted ${command} with.
38298186Sgordon#				Requires /usr to be mounted.
38378344Sobrien#
38498186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
38578344Sobrien#				Otherwise, use default command (see below)
38678344Sobrien#
38798186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
38898186Sgordon#				${rc_arg}_cmd method in the default
38998186Sgordon#				operation (i.e, after checking for required
39098186Sgordon#				bits and process (non)existence).
39178344Sobrien#				If this completes with a non-zero exit code,
39298186Sgordon#				don't run ${rc_arg}_cmd.
39378344Sobrien#
39498186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
39598186Sgordon#				${rc_arg}_cmd method, if that method
39698186Sgordon#				returned a zero exit code.
39798186Sgordon#
39878344Sobrien#	required_dirs	n	If set, check for the existence of the given
39978344Sobrien#				directories before running the default
40078344Sobrien#				(re)start command.
40178344Sobrien#
40278344Sobrien#	required_files	n	If set, check for the readability of the given
40378344Sobrien#				files before running the default (re)start
40478344Sobrien#				command.
40578344Sobrien#
40678344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
40778344Sobrien#				listed variables before running the default
40878344Sobrien#				(re)start command.
40978344Sobrien#
41098186Sgordon#	Default behaviour for a given argument, if no override method is
41198186Sgordon#	provided:
41278344Sobrien#
41398186Sgordon#	Argument	Default behaviour
41498186Sgordon#	--------	-----------------
41578344Sobrien#	start		if !running && checkyesno ${rcvar}
41678344Sobrien#				${command}
41778344Sobrien#
41878344Sobrien#	stop		if ${pidfile}
41998186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
42078344Sobrien#			else
42198186Sgordon#				rc_pid=$(check_process $command)
42298186Sgordon#			kill $sig_stop $rc_pid
42398186Sgordon#			wait_for_pids $rc_pid
42498186Sgordon#			($sig_stop defaults to TERM.)
42578344Sobrien#
42698186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
42798186Sgordon#			and doesn't wait_for_pids.
42878344Sobrien#			$sig_reload defaults to HUP.
42978344Sobrien#
43078344Sobrien#	restart		Run `stop' then `start'.
43178344Sobrien#
43298186Sgordon#	status		Show if ${command} is running, etc.
43378344Sobrien#
43498186Sgordon#	poll		Wait for ${command} to exit.
43598186Sgordon#
43698186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
43798186Sgordon#
43898186Sgordon#	Variables available to methods, and after run_rc_command() has
43998186Sgordon#	completed:
44098186Sgordon#
44198186Sgordon#	Variable	Purpose
44298186Sgordon#	--------	-------
443126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
44498186Sgordon#			performed
44598186Sgordon#
44698186Sgordon#	rc_flags	Flags to start the default command with.
44798186Sgordon#			Defaults to ${name}_flags, unless overridden
44898186Sgordon#			by $flags from the environment.
44998186Sgordon#			This variable may be changed by the precmd method.
45098186Sgordon#
45198186Sgordon#	rc_pid		PID of command (if appropriate)
45298186Sgordon#
45398186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
45498186Sgordon#
45598186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
45698186Sgordon#
45798186Sgordon#
45878344Sobrienrun_rc_command()
45978344Sobrien{
460116097Smtm	_return=0
46198186Sgordon	rc_arg=$1
46278344Sobrien	if [ -z "$name" ]; then
46398186Sgordon		err 3 'run_rc_command: $name is not set.'
46478344Sobrien	fi
46578344Sobrien
466132892Smtm	# Don't repeat the first argument when passing additional command-
467132892Smtm	# line arguments to the command subroutines.
468132892Smtm	#
469132892Smtm	shift 1
470132892Smtm	rc_extra_args="$*"
471132892Smtm
472126303Smtm	_rc_prefix=
47398186Sgordon	case "$rc_arg" in
47478344Sobrien	fast*)				# "fast" prefix; don't check pid
47598186Sgordon		rc_arg=${rc_arg#fast}
47698186Sgordon		rc_fast=yes
47778344Sobrien		;;
478126303Smtm	force*)				# "force prefix; always run
47998186Sgordon		rc_force=yes
480126303Smtm		_rc_prefix=force
481126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
48278344Sobrien		if [ -n "${rcvar}" ]; then
48378344Sobrien			eval ${rcvar}=YES
48478344Sobrien		fi
48578344Sobrien		;;
486126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
487126303Smtm		_rc_prefix=one
488126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
489126303Smtm		if [ -n "${rcvar}" ]; then
490126303Smtm			eval ${rcvar}=YES
491126303Smtm		fi
492126303Smtm		;;
49378344Sobrien	esac
49478344Sobrien
49598186Sgordon	eval _overide_command=\$${name}_program
49698186Sgordon	if [ -n "$_overide_command" ]; then
49798186Sgordon		command=$_overide_command
49898186Sgordon	fi
49998186Sgordon
50078344Sobrien	_keywords="start stop restart rcvar $extra_commands"
50198186Sgordon	rc_pid=
50278344Sobrien	_pidcmd=
50398186Sgordon	_procname=${procname:-${command}}
50498186Sgordon
505131135Smtm					# setup pid check command
506131135Smtm	if [ -n "$_procname" ]; then
50778344Sobrien		if [ -n "$pidfile" ]; then
50898186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
50998186Sgordon		else
51098186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
51178344Sobrien		fi
51278344Sobrien		if [ -n "$_pidcmd" ]; then
51398186Sgordon			_keywords="${_keywords} status poll"
51478344Sobrien		fi
51578344Sobrien	fi
51678344Sobrien
51798186Sgordon	if [ -z "$rc_arg" ]; then
51878344Sobrien		rc_usage "$_keywords"
51978344Sobrien	fi
52078344Sobrien
52178344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
52298186Sgordon		rc_flags=$flags
52378344Sobrien	else
52498186Sgordon		eval rc_flags=\$${name}_flags
52578344Sobrien	fi
52698186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
52798186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
52898186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
52978344Sobrien
53098186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
531124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
53298186Sgordon			unset _user
53398186Sgordon		fi
53498186Sgordon	fi
53598186Sgordon
53678344Sobrien					# if ${rcvar} is set, and $1 is not
53798186Sgordon					# "rcvar", then run
53878344Sobrien					#	checkyesno ${rcvar}
53978344Sobrien					# and return if that failed
54078344Sobrien					#
54198186Sgordon	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
54278344Sobrien		if ! checkyesno ${rcvar}; then
54378344Sobrien			return 0
54478344Sobrien		fi
54578344Sobrien	fi
54678344Sobrien
54778344Sobrien	eval $_pidcmd			# determine the pid if necessary
54878344Sobrien
54978344Sobrien	for _elem in $_keywords; do
55098186Sgordon		if [ "$_elem" != "$rc_arg" ]; then
55178344Sobrien			continue
55278344Sobrien		fi
55378344Sobrien
55478344Sobrien					# if there's a custom ${XXX_cmd},
55578344Sobrien					# run that instead of the default
55678344Sobrien					#
55798186Sgordon		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
55898186Sgordon		    _postcmd=\$${rc_arg}_postcmd
55978344Sobrien		if [ -n "$_cmd" ]; then
56078344Sobrien					# if the precmd failed and force
56178344Sobrien					# isn't set, exit
56278344Sobrien					#
563116097Smtm			if [ -n "$_precmd" ]; then
564116097Smtm				debug "run_rc_command: evaluating ${_precmd}()."
565132892Smtm				eval $_precmd $rc_extra_args
566116097Smtm				_return=$?
567116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
568116097Smtm				    return 1
56978344Sobrien			fi
57078344Sobrien
571116097Smtm			if [ -n "$_cmd" ]; then
572116097Smtm				debug "run_rc_command: evaluating ${_cmd}()."
573132892Smtm				eval $_cmd $rc_extra_args
574116097Smtm				_return=$?
575116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
576116097Smtm				    return 1
57798186Sgordon			fi
578109582Smtm
579116097Smtm			if [ -n "$_postcmd" ]; then
580116097Smtm				debug "run_rc_command: evaluating ${_postcmd}()."
581132892Smtm				 eval $_postcmd $rc_extra_args
582116097Smtm				_return=$?
583116097Smtm			fi
584116097Smtm			return $_return
58578344Sobrien		fi
58678344Sobrien
58798186Sgordon		case "$rc_arg" in	# default operations...
58878344Sobrien
58978344Sobrien		status)
59098186Sgordon			if [ -n "$rc_pid" ]; then
59198186Sgordon				echo "${name} is running as pid $rc_pid."
59278344Sobrien			else
59378344Sobrien				echo "${name} is not running."
59478344Sobrien				return 1
59578344Sobrien			fi
59678344Sobrien			;;
59778344Sobrien
59878344Sobrien		start)
599131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
60098186Sgordon				echo "${name} already running? (pid=$rc_pid)."
60178344Sobrien				exit 1
60278344Sobrien			fi
60378344Sobrien
604126287Smtm			if [ ! -x ${_chroot}${command} ]; then
60598186Sgordon				info "run_rc_command: cannot run ($command)."
60678344Sobrien				return 0
60778344Sobrien			fi
60878344Sobrien
60978344Sobrien					# check for required variables,
61078344Sobrien					# directories, and files
61178344Sobrien					#
61278344Sobrien			for _f in $required_vars; do
61378344Sobrien				if ! checkyesno $_f; then
61478344Sobrien					warn "\$${_f} is not set."
61598186Sgordon					if [ -z "$rc_force" ]; then
61678344Sobrien						return 1
61778344Sobrien					fi
61878344Sobrien				fi
61978344Sobrien			done
62078344Sobrien			for _f in $required_dirs; do
62178344Sobrien				if [ ! -d "${_f}/." ]; then
62278344Sobrien					warn "${_f} is not a directory."
62398186Sgordon					if [ -z "$rc_force" ]; then
62478344Sobrien						return 1
62578344Sobrien					fi
62678344Sobrien				fi
62778344Sobrien			done
62878344Sobrien			for _f in $required_files; do
62978344Sobrien				if [ ! -r "${_f}" ]; then
63078344Sobrien					warn "${_f} is not readable."
63198186Sgordon					if [ -z "$rc_force" ]; then
63278344Sobrien						return 1
63378344Sobrien					fi
63478344Sobrien				fi
63578344Sobrien			done
63678344Sobrien
63778344Sobrien					# if the precmd failed and force
63878344Sobrien					# isn't set, exit
63978344Sobrien					#
640116097Smtm			if [ -n "${_precmd}" ]; then
641116097Smtm				debug "run_rc_command: evaluating ${_precmd}()."
642116097Smtm				eval $_precmd
643116097Smtm				_return=$?
644116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
645116097Smtm				    return 1
64678344Sobrien			fi
64778344Sobrien
64878344Sobrien					# setup the command to run, and run it
64978344Sobrien					#
65078344Sobrien			echo "Starting ${name}."
65178344Sobrien			if [ -n "$_chroot" ]; then
65278344Sobrien				_doit="\
65378344Sobrien${_nice:+nice -n $_nice }\
65478344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
65598186Sgordon$_chroot $command $rc_flags $command_args"
65678344Sobrien			else
65778344Sobrien				_doit="\
65878344Sobrien${_chdir:+cd $_chdir; }\
65978344Sobrien${_nice:+nice -n $_nice }\
66098186Sgordon$command $rc_flags $command_args"
66198186Sgordon				if [ -n "$_user" ]; then
66298186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
66398186Sgordon				fi
66478344Sobrien			fi
66598186Sgordon
66698186Sgordon					# if the cmd failed and force
66798186Sgordon					# isn't set, exit
66898186Sgordon					#
66998186Sgordon			debug "run_rc_command: _doit: $_doit"
670116097Smtm			eval $_doit
671116097Smtm			_return=$?
672116097Smtm			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
67398186Sgordon
67498186Sgordon					# finally, run postcmd
67598186Sgordon					#
676116097Smtm			if [ -n "${_postcmd}" ]; then
677116097Smtm				debug "run_rc_command: evaluating ${_postcmd}()."
678116097Smtm				eval $_postcmd
679116097Smtm			fi
68078344Sobrien			;;
68178344Sobrien
68278344Sobrien		stop)
68398186Sgordon			if [ -z "$rc_pid" ]; then
684131135Smtm				[ -n "$rc_fast" ] && exit 0
68578344Sobrien				if [ -n "$pidfile" ]; then
68678344Sobrien					echo \
68778344Sobrien				    "${name} not running? (check $pidfile)."
68878344Sobrien				else
68978344Sobrien					echo "${name} not running?"
69078344Sobrien				fi
69178344Sobrien				exit 1
69278344Sobrien			fi
69378344Sobrien
69498186Sgordon					# if the precmd failed and force
69598186Sgordon					# isn't set, exit
69698186Sgordon					#
697117977Smtm			if [ -n "$_precmd" ]; then
698116097Smtm				eval $_precmd
699116097Smtm				_return=$?
700116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
701116097Smtm				    return 1
70278344Sobrien			fi
70398186Sgordon
70498186Sgordon					# send the signal to stop
70598186Sgordon					#
70678344Sobrien			echo "Stopping ${name}."
70798186Sgordon			_doit="kill -${sig_stop:-TERM} $rc_pid"
70898186Sgordon			if [ -n "$_user" ]; then
70998186Sgordon				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
71098186Sgordon			fi
71198186Sgordon
71298186Sgordon					# if the stop cmd failed and force
71398186Sgordon					# isn't set, exit
71498186Sgordon					#
715116097Smtm			eval $_doit
716116097Smtm			_return=$?
717116097Smtm			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
71898186Sgordon
71998186Sgordon					# wait for the command to exit,
72098186Sgordon					# and run postcmd.
72198186Sgordon			wait_for_pids $rc_pid
722116097Smtm			if [ -n "$_postcmd" ]; then
723116097Smtm				eval $_postcmd
724116097Smtm				_return=$?
725116097Smtm			fi
72678344Sobrien			;;
72778344Sobrien
72878344Sobrien		reload)
72998186Sgordon			if [ -z "$rc_pid" ]; then
73078344Sobrien				if [ -n "$pidfile" ]; then
73178344Sobrien					echo \
73278344Sobrien				    "${name} not running? (check $pidfile)."
73378344Sobrien				else
73478344Sobrien					echo "${name} not running?"
73578344Sobrien				fi
73678344Sobrien				exit 1
73778344Sobrien			fi
73878344Sobrien			echo "Reloading ${name} config files."
739116097Smtm			if [ -n "$_precmd" ]; then
740116097Smtm				eval $_precmd
741116097Smtm				_return=$?
742116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
743116097Smtm				    return 1
74478344Sobrien			fi
74598186Sgordon			_doit="kill -${sig_reload:-HUP} $rc_pid"
74698186Sgordon			if [ -n "$_user" ]; then
74798186Sgordon				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
74898186Sgordon			fi
749116097Smtm			eval $_doit
750116097Smtm			_return=$?
751116097Smtm			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
752116097Smtm			if [ -n "$_postcmd" ]; then
753116097Smtm				eval $_postcmd
754116097Smtm				_return=$?
75598186Sgordon			fi
75678344Sobrien			;;
75778344Sobrien
75878344Sobrien		restart)
759116097Smtm			if [ -n "$_precmd" ]; then
760132892Smtm				eval $_precmd $rc_extra_args
761116097Smtm				_return=$?
762116097Smtm				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
763116097Smtm				    return 1
76478344Sobrien			fi
76578344Sobrien					# prevent restart being called more
76678344Sobrien					# than once by any given script
76778344Sobrien					#
768126285Smtm			if ${_rc_restart_done:-false}; then
76978344Sobrien				return 0
77078344Sobrien			fi
771126285Smtm			_rc_restart_done=true
77278344Sobrien
773132892Smtm			( $0 ${_rc_prefix}stop $rc_extra_args )
774132892Smtm			$0 ${_rc_prefix}start $rc_extra_args
77598186Sgordon
776116097Smtm			if [ -n "$_postcmd" ]; then
777132892Smtm				eval $_postcmd $rc_extra_args
778116097Smtm				_return=$?
779116097Smtm			fi
78078344Sobrien			;;
78178344Sobrien
78298186Sgordon		poll)
78398186Sgordon			if [ -n "$rc_pid" ]; then
78498186Sgordon				wait_for_pids $rc_pid
78598186Sgordon			fi
78698186Sgordon			;;
78798186Sgordon
78878344Sobrien		rcvar)
78978344Sobrien			echo "# $name"
79078344Sobrien			if [ -n "$rcvar" ]; then
79178344Sobrien				if checkyesno ${rcvar}; then
79278344Sobrien					echo "\$${rcvar}=YES"
79378344Sobrien				else
79478344Sobrien					echo "\$${rcvar}=NO"
79578344Sobrien				fi
79678344Sobrien			fi
79778344Sobrien			;;
79878344Sobrien
79978344Sobrien		*)
80078344Sobrien			rc_usage "$_keywords"
80178344Sobrien			;;
80278344Sobrien
80378344Sobrien		esac
804116097Smtm		return $_return
80578344Sobrien	done
80678344Sobrien
80798186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
80878344Sobrien	rc_usage "$_keywords"
80978344Sobrien	exit 1
81078344Sobrien}
81178344Sobrien
81278344Sobrien#
81378344Sobrien# run_rc_script file arg
81478344Sobrien#	Start the script `file' with `arg', and correctly handle the
81578344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
81698186Sgordon#	sourced into the current environment.  If `file' appears to be
81798186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
81898186Sgordon#	executable run as a child process.
81978344Sobrien#
82078344Sobrienrun_rc_script()
82178344Sobrien{
82278344Sobrien	_file=$1
82378344Sobrien	_arg=$2
82478344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
82578344Sobrien		err 3 'USAGE: run_rc_script file arg'
82678344Sobrien	fi
82778344Sobrien
82898186Sgordon	unset	name command command_args command_interpreter \
82998186Sgordon		extra_commands pidfile procname \
83098186Sgordon		rcvar required_dirs required_files required_vars
83198186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
83298186Sgordon
83378344Sobrien	case "$_file" in
83478344Sobrien	*.sh)				# run in current shell
835146490Sschweikh		set $_arg; . $_file
83678344Sobrien		;;
837126288Smtm	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
83898186Sgordon		warn "Ignoring scratch file $_file"
83998186Sgordon		;;
84078344Sobrien	*)				# run in subshell
84198186Sgordon		if [ -x $_file ]; then
84298186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
843146490Sschweikh				set $_arg; . $_file
84498186Sgordon			else
845130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
846130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
847146490Sschweikh				  set $_arg; . $_file )
84898186Sgordon			fi
84998186Sgordon		fi
85078344Sobrien		;;
85178344Sobrien	esac
85278344Sobrien}
85378344Sobrien
85478344Sobrien#
85578344Sobrien# load_rc_config
85678344Sobrien#	Source in the configuration file for a given command.
85778344Sobrien#
85878344Sobrienload_rc_config()
85978344Sobrien{
86078344Sobrien	_command=$1
86178344Sobrien	if [ -z "$_command" ]; then
86278344Sobrien		err 3 'USAGE: load_rc_config command'
86378344Sobrien	fi
86478344Sobrien
865126285Smtm	if ${_rc_conf_loaded:-false}; then
866126285Smtm		:
867126285Smtm	else
86898186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
86998186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
87098186Sgordon			. /etc/defaults/rc.conf
87198186Sgordon			source_rc_confs
87298186Sgordon		elif [ -r /etc/rc.conf ]; then
87398186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
87498186Sgordon			. /etc/rc.conf
87598186Sgordon		fi
876126285Smtm		_rc_conf_loaded=true
87798186Sgordon	fi
87878344Sobrien	if [ -f /etc/rc.conf.d/"$_command" ]; then
87998186Sgordon		debug "Sourcing /etc/rc.conf.d/${_command}"
88078344Sobrien		. /etc/rc.conf.d/"$_command"
88178344Sobrien	fi
882101850Sgordon
883101850Sgordon	# XXX - Deprecated variable name support
884101850Sgordon	#
885103018Sgordon	case ${OSTYPE} in
886101850Sgordon	FreeBSD)
887146490Sschweikh		[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
888146490Sschweikh		[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
889146490Sschweikh		[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
890146490Sschweikh		[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
891146490Sschweikh		[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
892146490Sschweikh		[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
893146490Sschweikh		[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
894115950Smtm		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
895115950Smtm		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
896146490Sschweikh		;;
897101850Sgordon	esac
89878344Sobrien}
89978344Sobrien
90078344Sobrien#
90178344Sobrien# rc_usage commands
90278344Sobrien#	Print a usage string for $0, with `commands' being a list of
90378344Sobrien#	valid commands.
90478344Sobrien#
90578344Sobrienrc_usage()
90678344Sobrien{
907126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
90878344Sobrien
90978344Sobrien	_sep=
910126286Smtm	for _elem; do
91178344Sobrien		echo -n 1>&2 "$_sep$_elem"
91278344Sobrien		_sep="|"
91378344Sobrien	done
91478344Sobrien	echo 1>&2 ")"
91578344Sobrien	exit 1
91678344Sobrien}
91778344Sobrien
91878344Sobrien#
91978344Sobrien# err exitval message
92078344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
92178344Sobrien#
92278344Sobrienerr()
92378344Sobrien{
92478344Sobrien	exitval=$1
92578344Sobrien	shift
92678344Sobrien
927106643Sgordon	if [ -x /usr/bin/logger ]; then
928106643Sgordon		logger "$0: ERROR: $*"
929106643Sgordon	fi
930106643Sgordon	echo 1>&2 "$0: ERROR: $*"
93178344Sobrien	exit $exitval
93278344Sobrien}
93378344Sobrien
93478344Sobrien#
93578344Sobrien# warn message
93678344Sobrien#	Display message to stderr and log to the syslog.
93778344Sobrien#
93878344Sobrienwarn()
93978344Sobrien{
940106643Sgordon	if [ -x /usr/bin/logger ]; then
941106643Sgordon		logger "$0: WARNING: $*"
942106643Sgordon	fi
943106643Sgordon	echo 1>&2 "$0: WARNING: $*"
94478344Sobrien}
94598186Sgordon
94698186Sgordon#
94798186Sgordon# info message
94898186Sgordon#	Display informational message to stdout and log to syslog.
94998186Sgordon#
95098186Sgordoninfo()
95198186Sgordon{
952119170Smtm	case ${rc_info} in
953119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
954119170Smtm		if [ -x /usr/bin/logger ]; then
955119170Smtm			logger "$0: INFO: $*"
956119170Smtm		fi
957119170Smtm		echo "$0: INFO: $*"
958119170Smtm		;;
959119170Smtm	esac
96098186Sgordon}
96198186Sgordon
96298186Sgordon#
96398186Sgordon# debug message
964106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
96598186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
96698186Sgordon#	function.
96798186Sgordon#
96898186Sgordondebug()
96998186Sgordon{
97098186Sgordon	case ${rc_debug} in
97198186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
972106700Sgordon		if [ -x /usr/bin/logger ]; then
973106700Sgordon			logger "$0: INFO: $*"
974106700Sgordon		fi
975146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
97698186Sgordon		;;
97798186Sgordon	esac
97898186Sgordon}
97998186Sgordon
98098186Sgordon#
98198186Sgordon# backup_file action file cur backup
98298186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
98398186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
98498186Sgordon#
98598186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
98698186Sgordon#	which can be either YES or NO.
98798186Sgordon#
98898186Sgordon#	The `action' keyword can be one of the following:
98998186Sgordon#
99098186Sgordon#	add		`file' is now being backed up (and is possibly
99198186Sgordon#			being reentered into the backups system).  `cur'
99298186Sgordon#			is created and RCS files, if necessary, are
99398186Sgordon#			created as well.
99498186Sgordon#
99598186Sgordon#	update		`file' has changed and needs to be backed up.
99698186Sgordon#			If `cur' exists, it is copied to to `back' or
99798186Sgordon#			checked into RCS (if the repository file is old),
99898186Sgordon#			and then `file' is copied to `cur'.  Another RCS
99998186Sgordon#			check in done here if RCS is being used.
100098186Sgordon#
100198186Sgordon#	remove		`file' is no longer being tracked by the backups
100298186Sgordon#			system.  If RCS is not being used, `cur' is moved
100398186Sgordon#			to `back', otherwise an empty file is checked in,
100498186Sgordon#			and then `cur' is removed.
100598186Sgordon#
100698186Sgordon#
100798186Sgordonbackup_file()
100898186Sgordon{
100998186Sgordon	_action=$1
101098186Sgordon	_file=$2
101198186Sgordon	_cur=$3
101298186Sgordon	_back=$4
101398186Sgordon
101498186Sgordon	if checkyesno backup_uses_rcs; then
101598186Sgordon		_msg0="backup archive"
101698186Sgordon		_msg1="update"
101798186Sgordon
101898186Sgordon		# ensure that history file is not locked
101998186Sgordon		if [ -f $_cur,v ]; then
102098186Sgordon			rcs -q -u -U -M $_cur
102198186Sgordon		fi
102298186Sgordon
102398186Sgordon		# ensure after switching to rcs that the
102498186Sgordon		# current backup is not lost
102598186Sgordon		if [ -f $_cur ]; then
102698186Sgordon			# no archive, or current newer than archive
102798186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
102898186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
102998186Sgordon				rcs -q -kb -U $_cur
103098186Sgordon				co -q -f -u $_cur
103198186Sgordon			fi
103298186Sgordon		fi
103398186Sgordon
103498186Sgordon		case $_action in
103598186Sgordon		add|update)
103698186Sgordon			cp -p $_file $_cur
103798186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
103898186Sgordon			rcs -q -kb -U $_cur
103998186Sgordon			co -q -f -u $_cur
104098186Sgordon			chown root:wheel $_cur $_cur,v
104198186Sgordon			;;
104298186Sgordon		remove)
104398186Sgordon			cp /dev/null $_cur
104498186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
104598186Sgordon			rcs -q -kb -U $_cur
104698186Sgordon			chown root:wheel $_cur $_cur,v
104798186Sgordon			rm $_cur
104898186Sgordon			;;
104998186Sgordon		esac
105098186Sgordon	else
105198186Sgordon		case $_action in
105298186Sgordon		add|update)
105398186Sgordon			if [ -f $_cur ]; then
105498186Sgordon				cp -p $_cur $_back
105598186Sgordon			fi
105698186Sgordon			cp -p $_file $_cur
105798186Sgordon			chown root:wheel $_cur
105898186Sgordon			;;
105998186Sgordon		remove)
106098186Sgordon			mv -f $_cur $_back
106198186Sgordon			;;
106298186Sgordon		esac
106398186Sgordon	fi
106498186Sgordon}
1065119166Smtm
1066123344Smtm# make_symlink src link
1067123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1068123344Smtm#	directory in which link is to be created does not exist
1069123344Smtm#	a warning will be displayed and an error will be returned.
1070123344Smtm#	Returns 0 on sucess, 1 otherwise.
1071119166Smtm#
1072123344Smtmmake_symlink()
1073119166Smtm{
1074123344Smtm	local src link linkdir _me
1075123344Smtm	src="$1"
1076123344Smtm	link="$2"
1077123344Smtm	linkdir="`dirname $link`"
1078123344Smtm	_me="make_symlink()"
1079119166Smtm
1080123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1081123344Smtm		warn "$_me: requires two arguments."
1082119166Smtm		return 1
1083119166Smtm	fi
1084123344Smtm	if [ ! -d "$linkdir" ]; then
1085123344Smtm		warn "$_me: the directory $linkdir does not exist"
1086119166Smtm		return 1
1087119166Smtm	fi
1088146490Sschweikh	if ! ln -sf $src $link; then
1089123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1090119166Smtm		return 1
1091119166Smtm	fi
1092119166Smtm	return 0
1093119166Smtm}
1094119166Smtm
1095119166Smtm# devfs_rulesets_from_file file
1096119166Smtm#	Reads a set of devfs commands from file, and creates
1097119166Smtm#	the specified rulesets with their rules. Returns non-zero
1098119166Smtm#	if there was an error.
1099119166Smtm#
1100119166Smtmdevfs_rulesets_from_file()
1101119166Smtm{
1102119166Smtm	local file _err _me
1103119166Smtm	file="$1"
1104119166Smtm	_me="devfs_rulesets_from_file"
1105119166Smtm	_err=0
1106119166Smtm
1107119166Smtm	if [ -z "$file" ]; then
1108119166Smtm		warn "$_me: you must specify a file"
1109119166Smtm		return 1
1110119166Smtm	fi
1111119166Smtm	if [ ! -e "$file" ]; then
1112119166Smtm		debug "$_me: no such file ($file)"
1113119166Smtm		return 0
1114119166Smtm	fi
1115119166Smtm	debug "reading rulesets from file ($file)"
1116119166Smtm	{ while read line
1117119166Smtm	do
1118119166Smtm		case $line in
1119119166Smtm		\#*)
1120119166Smtm			continue
1121119166Smtm			;;
1122119166Smtm		\[*\]*)
1123119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1124119166Smtm			if [ -z "$rulenum" ]; then
1125119166Smtm				warn "$_me: cannot extract rule number ($line)"
1126119166Smtm				_err=1
1127119166Smtm				break
1128119166Smtm			fi
1129119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1130119166Smtm			if [ -z "$rulename" ]; then
1131119166Smtm				warn "$_me: cannot extract rule name ($line)"
1132119166Smtm				_err=1
1133119166Smtm				break;
1134119166Smtm			fi
1135119166Smtm			eval $rulename=\$rulenum
1136119166Smtm			debug "found ruleset: $rulename=$rulenum"
1137146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1138119166Smtm				_err=1
1139119166Smtm				break
1140119166Smtm			fi
1141119166Smtm			;;
1142119166Smtm		*)
1143119166Smtm			rulecmd="${line%%"\#*"}"
1144119166Smtm			# evaluate the command incase it includes
1145119166Smtm			# other rules
1146119166Smtm			if [ -n "$rulecmd" ]; then
1147119166Smtm				debug "adding rule ($rulecmd)"
1148119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1149119166Smtm				then
1150119166Smtm					_err=1
1151119166Smtm					break
1152119166Smtm				fi
1153119166Smtm			fi
1154119166Smtm			;;
1155119166Smtm		esac
1156119166Smtm		if [ $_err -ne 0 ]; then
1157119166Smtm			debug "error in $_me"
1158119166Smtm			break
1159119166Smtm		fi
1160119166Smtm	done } < $file
1161119166Smtm	return $_err
1162119166Smtm}
1163119166Smtm
1164119166Smtm# devfs_init_rulesets
1165119166Smtm#	Initializes rulesets from configuration files. Returns
1166119166Smtm#	non-zero if there was an error.
1167119166Smtm#
1168119166Smtmdevfs_init_rulesets()
1169119166Smtm{
1170119166Smtm	local file _me
1171119166Smtm	_me="devfs_init_rulesets"
1172119166Smtm
1173119166Smtm	# Go through this only once
1174119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1175119166Smtm		debug "$_me: devfs rulesets already initialized"
1176119166Smtm		return
1177119166Smtm	fi
1178146490Sschweikh	for file in $devfs_rulesets; do
1179119166Smtm		devfs_rulesets_from_file $file || return 1
1180119166Smtm	done
1181119166Smtm	devfs_rulesets_init=1
1182119166Smtm	debug "$_me: devfs rulesets initialized"
1183119166Smtm	return 0
1184119166Smtm}
1185119166Smtm
1186119166Smtm# devfs_set_ruleset ruleset [dir]
1187119166Smtm#	Sets the default ruleset of dir to ruleset. The ruleset arguement
1188119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1189119166Smtm#	Returns non-zero if it could not set it successfully.
1190119166Smtm#
1191119166Smtmdevfs_set_ruleset()
1192119166Smtm{
1193119166Smtm	local devdir rs _me
1194119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1195119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1196119166Smtm	_me="devfs_set_ruleset"
1197119166Smtm
1198119166Smtm	if [ -z "$rs" ]; then
1199119166Smtm		warn "$_me: you must specify a ruleset number"
1200119166Smtm		return 1
1201119166Smtm	fi
1202119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1203146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1204119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1205119166Smtm		return 1
1206119166Smtm	fi
1207119166Smtm	return 0
1208119166Smtm}
1209119166Smtm
1210119166Smtm# devfs_apply_ruleset ruleset [dir]
1211119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1212119166Smtm#	The ruleset argument must be a ruleset name as specified
1213119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1214119166Smtm#	if it could not apply the ruleset.
1215119166Smtm#
1216119166Smtmdevfs_apply_ruleset()
1217119166Smtm{
1218119166Smtm	local devdir rs _me
1219119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1220119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1221119166Smtm	_me="devfs_apply_ruleset"
1222119166Smtm
1223119166Smtm	if [ -z "$rs" ]; then
1224119166Smtm		warn "$_me: you must specify a ruleset"
1225119166Smtm		return 1
1226119166Smtm	fi
1227119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1228146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1229119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1230119166Smtm		return 1
1231119166Smtm	fi
1232119166Smtm	return 0
1233119166Smtm}
1234119166Smtm
1235119166Smtm# devfs_domount dir [ruleset]
1236119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1237119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1238119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1239119166Smtm#
1240119166Smtmdevfs_domount()
1241119166Smtm{
1242119166Smtm	local devdir rs _me
1243119166Smtm	devdir="$1"
1244119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1245119166Smtm	_me="devfs_domount()"
1246119166Smtm
1247119166Smtm	if [ -z "$devdir" ]; then
1248119166Smtm		warn "$_me: you must specify a mount-point"
1249119166Smtm		return 1
1250119166Smtm	fi
1251119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1252146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1253119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1254119166Smtm		return 1
1255119166Smtm	fi
1256119166Smtm	if [ -n "$rs" ]; then
1257119166Smtm		devfs_init_rulesets
1258119166Smtm		devfs_set_ruleset $rs $devdir
1259124797Scperciva		devfs -m $devdir rule applyset
1260119166Smtm	fi
1261119166Smtm	return 0
1262119166Smtm}
1263119166Smtm
1264119166Smtm# devfs_mount_jail dir [ruleset]
1265119166Smtm#	Mounts a devfs file system appropriate for jails
1266119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1267119166Smtm#	it names will be used instead.  If present, ruleset must
1268119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1269119166Smtm#	This function returns non-zero if an error occurs.
1270119166Smtm#
1271119166Smtmdevfs_mount_jail()
1272119166Smtm{
1273119166Smtm	local jdev rs _me
1274119166Smtm	jdev="$1"
1275119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1276119166Smtm	_me="devfs_mount_jail"
1277119166Smtm
1278119166Smtm	devfs_init_rulesets
1279146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1280119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1281119166Smtm		return 1
1282119166Smtm	fi
1283119166Smtm	return 0
1284119166Smtm}
1285127345Sbrooks
1286127345Sbrooks# Provide a function for normalizing the mounting of memory
1287127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1288127345Sbrooks# as close as possible between 5-current and 4-stable.
1289127345Sbrooks#   $1 = size
1290127345Sbrooks#   $2 = mount point
1291137451Skeramida#   $3 = (optional) extra mdmfs flags
1292146490Sschweikhmount_md()
1293146490Sschweikh{
1294127345Sbrooks	if [ -n "$3" ]; then
1295137451Skeramida		flags="$3"
1296127345Sbrooks	fi
1297137451Skeramida	/sbin/mdmfs $flags -s $1 -M md $2
1298127345Sbrooks}
1299131550Scperciva
1300149049Spjd# ltr str src dst
1301149049Spjd#	Change every $src in $str to $dst.
1302149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1303149049Spjd#	awk(1).
1304149049Spjdltr()
1305149049Spjd{
1306149049Spjd	local _str _src _dst _out _com
1307149049Spjd	_str=$1
1308149049Spjd	_src=$2
1309149049Spjd	_dst=$3
1310149049Spjd	_out=""
1311149049Spjd
1312149049Spjd	IFS=${_src}
1313149049Spjd	for _com in ${_str}; do
1314149049Spjd		if [ -z "${_out}" ]; then
1315149049Spjd			_out="${_com}"
1316149049Spjd		else
1317149049Spjd			_out="${_out}${_dst}${_com}"
1318149049Spjd		fi
1319149049Spjd	done
1320149049Spjd	echo "${_out}"
1321149049Spjd}
1322149049Spjd
1323149050Spjd# Creates a list of providers for GELI encryption.
1324149050Spjdgeli_make_list()
1325149050Spjd{
1326149050Spjd	local devices devices2
1327149050Spjd	local provider mountpoint type options rest
1328149050Spjd
1329149050Spjd	# Create list of GELI providers from fstab.
1330149050Spjd	while read provider mountpoint type options rest ; do
1331149050Spjd		case ":${provider}" in
1332149050Spjd		:#*)
1333149050Spjd			continue
1334149050Spjd			;;
1335149050Spjd		*.eli)
1336149050Spjd			# Skip swap devices.
1337149050Spjd			if [ "${type}" = "swap" -o "${options}" = "sw" ]; then
1338149050Spjd				continue
1339149050Spjd			fi
1340149050Spjd			devices="${devices} ${provider}"
1341149050Spjd			;;
1342149050Spjd		esac
1343149050Spjd	done < /etc/fstab
1344149050Spjd
1345149050Spjd	# Append providers from geli_devices.
1346149050Spjd	devices="${devices} ${geli_devices}"
1347149050Spjd
1348149050Spjd	for provider in ${devices}; do
1349149050Spjd		provider=${provider%.eli}
1350149050Spjd		provider=${provider#/dev/}
1351149050Spjd		devices2="${devices2} ${provider}"
1352149050Spjd	done
1353149050Spjd
1354149050Spjd	echo ${devices2}
1355149050Spjd}
1356149050Spjd
1357131550Scpercivafi
1358