rc.subr revision 152519
1126288Smtm# $NetBSD: rc.subr,v 1.60 2003/07/26 05:13:47 lukem Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 152519 2005-11-16 10:45:19Z yar $
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
270151426Sjhb		    $_procname|$_procnamebn|${_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.
429151685Syar#			Note that `reload' isn't provided by default,
430151685Syar#			it should be enabled via $extra_commands.
43178344Sobrien#
43278344Sobrien#	restart		Run `stop' then `start'.
43378344Sobrien#
43498186Sgordon#	status		Show if ${command} is running, etc.
43578344Sobrien#
43698186Sgordon#	poll		Wait for ${command} to exit.
43798186Sgordon#
43898186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
43998186Sgordon#
44098186Sgordon#	Variables available to methods, and after run_rc_command() has
44198186Sgordon#	completed:
44298186Sgordon#
44398186Sgordon#	Variable	Purpose
44498186Sgordon#	--------	-------
445126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
44698186Sgordon#			performed
44798186Sgordon#
44898186Sgordon#	rc_flags	Flags to start the default command with.
44998186Sgordon#			Defaults to ${name}_flags, unless overridden
45098186Sgordon#			by $flags from the environment.
45198186Sgordon#			This variable may be changed by the precmd method.
45298186Sgordon#
45398186Sgordon#	rc_pid		PID of command (if appropriate)
45498186Sgordon#
45598186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
45698186Sgordon#
45798186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
45898186Sgordon#
45998186Sgordon#
46078344Sobrienrun_rc_command()
46178344Sobrien{
462116097Smtm	_return=0
46398186Sgordon	rc_arg=$1
46478344Sobrien	if [ -z "$name" ]; then
46598186Sgordon		err 3 'run_rc_command: $name is not set.'
46678344Sobrien	fi
46778344Sobrien
468132892Smtm	# Don't repeat the first argument when passing additional command-
469132892Smtm	# line arguments to the command subroutines.
470132892Smtm	#
471132892Smtm	shift 1
472132892Smtm	rc_extra_args="$*"
473132892Smtm
474126303Smtm	_rc_prefix=
47598186Sgordon	case "$rc_arg" in
47678344Sobrien	fast*)				# "fast" prefix; don't check pid
47798186Sgordon		rc_arg=${rc_arg#fast}
47898186Sgordon		rc_fast=yes
47978344Sobrien		;;
480126303Smtm	force*)				# "force prefix; always run
48198186Sgordon		rc_force=yes
482126303Smtm		_rc_prefix=force
483126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
48478344Sobrien		if [ -n "${rcvar}" ]; then
48578344Sobrien			eval ${rcvar}=YES
48678344Sobrien		fi
48778344Sobrien		;;
488126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
489126303Smtm		_rc_prefix=one
490126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
491126303Smtm		if [ -n "${rcvar}" ]; then
492126303Smtm			eval ${rcvar}=YES
493126303Smtm		fi
494126303Smtm		;;
49578344Sobrien	esac
49678344Sobrien
49798186Sgordon	eval _overide_command=\$${name}_program
498151687Syar	command=${command:+${_overide_command:-$command}}
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
518150796Syar		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
773152519Syar			# run stop in a subshell to keep variables for start
774152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
775152519Syar			run_rc_command ${_rc_prefix}start $rc_extra_args
77698186Sgordon
777116097Smtm			if [ -n "$_postcmd" ]; then
778132892Smtm				eval $_postcmd $rc_extra_args
779116097Smtm				_return=$?
780116097Smtm			fi
78178344Sobrien			;;
78278344Sobrien
78398186Sgordon		poll)
78498186Sgordon			if [ -n "$rc_pid" ]; then
78598186Sgordon				wait_for_pids $rc_pid
78698186Sgordon			fi
78798186Sgordon			;;
78898186Sgordon
78978344Sobrien		rcvar)
79078344Sobrien			echo "# $name"
79178344Sobrien			if [ -n "$rcvar" ]; then
79278344Sobrien				if checkyesno ${rcvar}; then
79378344Sobrien					echo "\$${rcvar}=YES"
79478344Sobrien				else
79578344Sobrien					echo "\$${rcvar}=NO"
79678344Sobrien				fi
79778344Sobrien			fi
79878344Sobrien			;;
79978344Sobrien
80078344Sobrien		*)
801150796Syar			rc_usage $_keywords
80278344Sobrien			;;
80378344Sobrien
80478344Sobrien		esac
805116097Smtm		return $_return
80678344Sobrien	done
80778344Sobrien
80898186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
809150796Syar	rc_usage $_keywords
81078344Sobrien	exit 1
81178344Sobrien}
81278344Sobrien
81378344Sobrien#
81478344Sobrien# run_rc_script file arg
81578344Sobrien#	Start the script `file' with `arg', and correctly handle the
81678344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
81798186Sgordon#	sourced into the current environment.  If `file' appears to be
81898186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
81998186Sgordon#	executable run as a child process.
82078344Sobrien#
82178344Sobrienrun_rc_script()
82278344Sobrien{
82378344Sobrien	_file=$1
82478344Sobrien	_arg=$2
82578344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
82678344Sobrien		err 3 'USAGE: run_rc_script file arg'
82778344Sobrien	fi
82878344Sobrien
82998186Sgordon	unset	name command command_args command_interpreter \
83098186Sgordon		extra_commands pidfile procname \
83198186Sgordon		rcvar required_dirs required_files required_vars
83298186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
83398186Sgordon
83478344Sobrien	case "$_file" in
83578344Sobrien	*.sh)				# run in current shell
836146490Sschweikh		set $_arg; . $_file
83778344Sobrien		;;
838126288Smtm	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
83998186Sgordon		warn "Ignoring scratch file $_file"
84098186Sgordon		;;
84178344Sobrien	*)				# run in subshell
84298186Sgordon		if [ -x $_file ]; then
84398186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
844146490Sschweikh				set $_arg; . $_file
84598186Sgordon			else
846130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
847130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
848146490Sschweikh				  set $_arg; . $_file )
84998186Sgordon			fi
85098186Sgordon		fi
85178344Sobrien		;;
85278344Sobrien	esac
85378344Sobrien}
85478344Sobrien
85578344Sobrien#
85678344Sobrien# load_rc_config
85778344Sobrien#	Source in the configuration file for a given command.
85878344Sobrien#
85978344Sobrienload_rc_config()
86078344Sobrien{
86178344Sobrien	_command=$1
86278344Sobrien	if [ -z "$_command" ]; then
86378344Sobrien		err 3 'USAGE: load_rc_config command'
86478344Sobrien	fi
86578344Sobrien
866126285Smtm	if ${_rc_conf_loaded:-false}; then
867126285Smtm		:
868126285Smtm	else
86998186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
87098186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
87198186Sgordon			. /etc/defaults/rc.conf
87298186Sgordon			source_rc_confs
87398186Sgordon		elif [ -r /etc/rc.conf ]; then
87498186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
87598186Sgordon			. /etc/rc.conf
87698186Sgordon		fi
877126285Smtm		_rc_conf_loaded=true
87898186Sgordon	fi
87978344Sobrien	if [ -f /etc/rc.conf.d/"$_command" ]; then
88098186Sgordon		debug "Sourcing /etc/rc.conf.d/${_command}"
88178344Sobrien		. /etc/rc.conf.d/"$_command"
88278344Sobrien	fi
883101850Sgordon
884101850Sgordon	# XXX - Deprecated variable name support
885101850Sgordon	#
886103018Sgordon	case ${OSTYPE} in
887101850Sgordon	FreeBSD)
888146490Sschweikh		[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
889146490Sschweikh		[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
890146490Sschweikh		[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
891146490Sschweikh		[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
892146490Sschweikh		[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
893146490Sschweikh		[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
894146490Sschweikh		[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
895115950Smtm		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
896115950Smtm		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
897146490Sschweikh		;;
898101850Sgordon	esac
89978344Sobrien}
90078344Sobrien
90178344Sobrien#
90278344Sobrien# rc_usage commands
90378344Sobrien#	Print a usage string for $0, with `commands' being a list of
90478344Sobrien#	valid commands.
90578344Sobrien#
90678344Sobrienrc_usage()
90778344Sobrien{
908126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
90978344Sobrien
91078344Sobrien	_sep=
911126286Smtm	for _elem; do
91278344Sobrien		echo -n 1>&2 "$_sep$_elem"
91378344Sobrien		_sep="|"
91478344Sobrien	done
91578344Sobrien	echo 1>&2 ")"
91678344Sobrien	exit 1
91778344Sobrien}
91878344Sobrien
91978344Sobrien#
92078344Sobrien# err exitval message
92178344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
92278344Sobrien#
92378344Sobrienerr()
92478344Sobrien{
92578344Sobrien	exitval=$1
92678344Sobrien	shift
92778344Sobrien
928106643Sgordon	if [ -x /usr/bin/logger ]; then
929106643Sgordon		logger "$0: ERROR: $*"
930106643Sgordon	fi
931106643Sgordon	echo 1>&2 "$0: ERROR: $*"
93278344Sobrien	exit $exitval
93378344Sobrien}
93478344Sobrien
93578344Sobrien#
93678344Sobrien# warn message
93778344Sobrien#	Display message to stderr and log to the syslog.
93878344Sobrien#
93978344Sobrienwarn()
94078344Sobrien{
941106643Sgordon	if [ -x /usr/bin/logger ]; then
942106643Sgordon		logger "$0: WARNING: $*"
943106643Sgordon	fi
944106643Sgordon	echo 1>&2 "$0: WARNING: $*"
94578344Sobrien}
94698186Sgordon
94798186Sgordon#
94898186Sgordon# info message
94998186Sgordon#	Display informational message to stdout and log to syslog.
95098186Sgordon#
95198186Sgordoninfo()
95298186Sgordon{
953119170Smtm	case ${rc_info} in
954119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
955119170Smtm		if [ -x /usr/bin/logger ]; then
956119170Smtm			logger "$0: INFO: $*"
957119170Smtm		fi
958119170Smtm		echo "$0: INFO: $*"
959119170Smtm		;;
960119170Smtm	esac
96198186Sgordon}
96298186Sgordon
96398186Sgordon#
96498186Sgordon# debug message
965106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
96698186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
96798186Sgordon#	function.
96898186Sgordon#
96998186Sgordondebug()
97098186Sgordon{
97198186Sgordon	case ${rc_debug} in
97298186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
973106700Sgordon		if [ -x /usr/bin/logger ]; then
974106700Sgordon			logger "$0: INFO: $*"
975106700Sgordon		fi
976146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
97798186Sgordon		;;
97898186Sgordon	esac
97998186Sgordon}
98098186Sgordon
98198186Sgordon#
98298186Sgordon# backup_file action file cur backup
98398186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
98498186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
98598186Sgordon#
98698186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
98798186Sgordon#	which can be either YES or NO.
98898186Sgordon#
98998186Sgordon#	The `action' keyword can be one of the following:
99098186Sgordon#
99198186Sgordon#	add		`file' is now being backed up (and is possibly
99298186Sgordon#			being reentered into the backups system).  `cur'
99398186Sgordon#			is created and RCS files, if necessary, are
99498186Sgordon#			created as well.
99598186Sgordon#
99698186Sgordon#	update		`file' has changed and needs to be backed up.
99798186Sgordon#			If `cur' exists, it is copied to to `back' or
99898186Sgordon#			checked into RCS (if the repository file is old),
99998186Sgordon#			and then `file' is copied to `cur'.  Another RCS
100098186Sgordon#			check in done here if RCS is being used.
100198186Sgordon#
100298186Sgordon#	remove		`file' is no longer being tracked by the backups
100398186Sgordon#			system.  If RCS is not being used, `cur' is moved
100498186Sgordon#			to `back', otherwise an empty file is checked in,
100598186Sgordon#			and then `cur' is removed.
100698186Sgordon#
100798186Sgordon#
100898186Sgordonbackup_file()
100998186Sgordon{
101098186Sgordon	_action=$1
101198186Sgordon	_file=$2
101298186Sgordon	_cur=$3
101398186Sgordon	_back=$4
101498186Sgordon
101598186Sgordon	if checkyesno backup_uses_rcs; then
101698186Sgordon		_msg0="backup archive"
101798186Sgordon		_msg1="update"
101898186Sgordon
101998186Sgordon		# ensure that history file is not locked
102098186Sgordon		if [ -f $_cur,v ]; then
102198186Sgordon			rcs -q -u -U -M $_cur
102298186Sgordon		fi
102398186Sgordon
102498186Sgordon		# ensure after switching to rcs that the
102598186Sgordon		# current backup is not lost
102698186Sgordon		if [ -f $_cur ]; then
102798186Sgordon			# no archive, or current newer than archive
102898186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
102998186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
103098186Sgordon				rcs -q -kb -U $_cur
103198186Sgordon				co -q -f -u $_cur
103298186Sgordon			fi
103398186Sgordon		fi
103498186Sgordon
103598186Sgordon		case $_action in
103698186Sgordon		add|update)
103798186Sgordon			cp -p $_file $_cur
103898186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
103998186Sgordon			rcs -q -kb -U $_cur
104098186Sgordon			co -q -f -u $_cur
104198186Sgordon			chown root:wheel $_cur $_cur,v
104298186Sgordon			;;
104398186Sgordon		remove)
104498186Sgordon			cp /dev/null $_cur
104598186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
104698186Sgordon			rcs -q -kb -U $_cur
104798186Sgordon			chown root:wheel $_cur $_cur,v
104898186Sgordon			rm $_cur
104998186Sgordon			;;
105098186Sgordon		esac
105198186Sgordon	else
105298186Sgordon		case $_action in
105398186Sgordon		add|update)
105498186Sgordon			if [ -f $_cur ]; then
105598186Sgordon				cp -p $_cur $_back
105698186Sgordon			fi
105798186Sgordon			cp -p $_file $_cur
105898186Sgordon			chown root:wheel $_cur
105998186Sgordon			;;
106098186Sgordon		remove)
106198186Sgordon			mv -f $_cur $_back
106298186Sgordon			;;
106398186Sgordon		esac
106498186Sgordon	fi
106598186Sgordon}
1066119166Smtm
1067123344Smtm# make_symlink src link
1068123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1069123344Smtm#	directory in which link is to be created does not exist
1070123344Smtm#	a warning will be displayed and an error will be returned.
1071123344Smtm#	Returns 0 on sucess, 1 otherwise.
1072119166Smtm#
1073123344Smtmmake_symlink()
1074119166Smtm{
1075123344Smtm	local src link linkdir _me
1076123344Smtm	src="$1"
1077123344Smtm	link="$2"
1078123344Smtm	linkdir="`dirname $link`"
1079123344Smtm	_me="make_symlink()"
1080119166Smtm
1081123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1082123344Smtm		warn "$_me: requires two arguments."
1083119166Smtm		return 1
1084119166Smtm	fi
1085123344Smtm	if [ ! -d "$linkdir" ]; then
1086123344Smtm		warn "$_me: the directory $linkdir does not exist"
1087119166Smtm		return 1
1088119166Smtm	fi
1089146490Sschweikh	if ! ln -sf $src $link; then
1090123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1091119166Smtm		return 1
1092119166Smtm	fi
1093119166Smtm	return 0
1094119166Smtm}
1095119166Smtm
1096119166Smtm# devfs_rulesets_from_file file
1097119166Smtm#	Reads a set of devfs commands from file, and creates
1098119166Smtm#	the specified rulesets with their rules. Returns non-zero
1099119166Smtm#	if there was an error.
1100119166Smtm#
1101119166Smtmdevfs_rulesets_from_file()
1102119166Smtm{
1103119166Smtm	local file _err _me
1104119166Smtm	file="$1"
1105119166Smtm	_me="devfs_rulesets_from_file"
1106119166Smtm	_err=0
1107119166Smtm
1108119166Smtm	if [ -z "$file" ]; then
1109119166Smtm		warn "$_me: you must specify a file"
1110119166Smtm		return 1
1111119166Smtm	fi
1112119166Smtm	if [ ! -e "$file" ]; then
1113119166Smtm		debug "$_me: no such file ($file)"
1114119166Smtm		return 0
1115119166Smtm	fi
1116119166Smtm	debug "reading rulesets from file ($file)"
1117119166Smtm	{ while read line
1118119166Smtm	do
1119119166Smtm		case $line in
1120119166Smtm		\#*)
1121119166Smtm			continue
1122119166Smtm			;;
1123119166Smtm		\[*\]*)
1124119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1125119166Smtm			if [ -z "$rulenum" ]; then
1126119166Smtm				warn "$_me: cannot extract rule number ($line)"
1127119166Smtm				_err=1
1128119166Smtm				break
1129119166Smtm			fi
1130119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1131119166Smtm			if [ -z "$rulename" ]; then
1132119166Smtm				warn "$_me: cannot extract rule name ($line)"
1133119166Smtm				_err=1
1134119166Smtm				break;
1135119166Smtm			fi
1136119166Smtm			eval $rulename=\$rulenum
1137119166Smtm			debug "found ruleset: $rulename=$rulenum"
1138146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1139119166Smtm				_err=1
1140119166Smtm				break
1141119166Smtm			fi
1142119166Smtm			;;
1143119166Smtm		*)
1144119166Smtm			rulecmd="${line%%"\#*"}"
1145119166Smtm			# evaluate the command incase it includes
1146119166Smtm			# other rules
1147119166Smtm			if [ -n "$rulecmd" ]; then
1148119166Smtm				debug "adding rule ($rulecmd)"
1149119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1150119166Smtm				then
1151119166Smtm					_err=1
1152119166Smtm					break
1153119166Smtm				fi
1154119166Smtm			fi
1155119166Smtm			;;
1156119166Smtm		esac
1157119166Smtm		if [ $_err -ne 0 ]; then
1158119166Smtm			debug "error in $_me"
1159119166Smtm			break
1160119166Smtm		fi
1161119166Smtm	done } < $file
1162119166Smtm	return $_err
1163119166Smtm}
1164119166Smtm
1165119166Smtm# devfs_init_rulesets
1166119166Smtm#	Initializes rulesets from configuration files. Returns
1167119166Smtm#	non-zero if there was an error.
1168119166Smtm#
1169119166Smtmdevfs_init_rulesets()
1170119166Smtm{
1171119166Smtm	local file _me
1172119166Smtm	_me="devfs_init_rulesets"
1173119166Smtm
1174119166Smtm	# Go through this only once
1175119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1176119166Smtm		debug "$_me: devfs rulesets already initialized"
1177119166Smtm		return
1178119166Smtm	fi
1179146490Sschweikh	for file in $devfs_rulesets; do
1180119166Smtm		devfs_rulesets_from_file $file || return 1
1181119166Smtm	done
1182119166Smtm	devfs_rulesets_init=1
1183119166Smtm	debug "$_me: devfs rulesets initialized"
1184119166Smtm	return 0
1185119166Smtm}
1186119166Smtm
1187119166Smtm# devfs_set_ruleset ruleset [dir]
1188151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1189119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1190119166Smtm#	Returns non-zero if it could not set it successfully.
1191119166Smtm#
1192119166Smtmdevfs_set_ruleset()
1193119166Smtm{
1194119166Smtm	local devdir rs _me
1195119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1196119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1197119166Smtm	_me="devfs_set_ruleset"
1198119166Smtm
1199119166Smtm	if [ -z "$rs" ]; then
1200119166Smtm		warn "$_me: you must specify a ruleset number"
1201119166Smtm		return 1
1202119166Smtm	fi
1203119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1204146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1205119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1206119166Smtm		return 1
1207119166Smtm	fi
1208119166Smtm	return 0
1209119166Smtm}
1210119166Smtm
1211119166Smtm# devfs_apply_ruleset ruleset [dir]
1212119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1213119166Smtm#	The ruleset argument must be a ruleset name as specified
1214119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1215119166Smtm#	if it could not apply the ruleset.
1216119166Smtm#
1217119166Smtmdevfs_apply_ruleset()
1218119166Smtm{
1219119166Smtm	local devdir rs _me
1220119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1221119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1222119166Smtm	_me="devfs_apply_ruleset"
1223119166Smtm
1224119166Smtm	if [ -z "$rs" ]; then
1225119166Smtm		warn "$_me: you must specify a ruleset"
1226119166Smtm		return 1
1227119166Smtm	fi
1228119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1229146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1230119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1231119166Smtm		return 1
1232119166Smtm	fi
1233119166Smtm	return 0
1234119166Smtm}
1235119166Smtm
1236119166Smtm# devfs_domount dir [ruleset]
1237119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1238119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1239119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1240119166Smtm#
1241119166Smtmdevfs_domount()
1242119166Smtm{
1243119166Smtm	local devdir rs _me
1244119166Smtm	devdir="$1"
1245119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1246119166Smtm	_me="devfs_domount()"
1247119166Smtm
1248119166Smtm	if [ -z "$devdir" ]; then
1249119166Smtm		warn "$_me: you must specify a mount-point"
1250119166Smtm		return 1
1251119166Smtm	fi
1252119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1253146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1254119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1255119166Smtm		return 1
1256119166Smtm	fi
1257119166Smtm	if [ -n "$rs" ]; then
1258119166Smtm		devfs_init_rulesets
1259119166Smtm		devfs_set_ruleset $rs $devdir
1260124797Scperciva		devfs -m $devdir rule applyset
1261119166Smtm	fi
1262119166Smtm	return 0
1263119166Smtm}
1264119166Smtm
1265119166Smtm# devfs_mount_jail dir [ruleset]
1266119166Smtm#	Mounts a devfs file system appropriate for jails
1267119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1268119166Smtm#	it names will be used instead.  If present, ruleset must
1269119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1270119166Smtm#	This function returns non-zero if an error occurs.
1271119166Smtm#
1272119166Smtmdevfs_mount_jail()
1273119166Smtm{
1274119166Smtm	local jdev rs _me
1275119166Smtm	jdev="$1"
1276119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1277119166Smtm	_me="devfs_mount_jail"
1278119166Smtm
1279119166Smtm	devfs_init_rulesets
1280146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1281119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1282119166Smtm		return 1
1283119166Smtm	fi
1284119166Smtm	return 0
1285119166Smtm}
1286127345Sbrooks
1287127345Sbrooks# Provide a function for normalizing the mounting of memory
1288127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1289127345Sbrooks# as close as possible between 5-current and 4-stable.
1290127345Sbrooks#   $1 = size
1291127345Sbrooks#   $2 = mount point
1292137451Skeramida#   $3 = (optional) extra mdmfs flags
1293146490Sschweikhmount_md()
1294146490Sschweikh{
1295127345Sbrooks	if [ -n "$3" ]; then
1296137451Skeramida		flags="$3"
1297127345Sbrooks	fi
1298149421Syar	/sbin/mdmfs $flags -s $1 md $2
1299127345Sbrooks}
1300131550Scperciva
1301149049Spjd# ltr str src dst
1302149049Spjd#	Change every $src in $str to $dst.
1303149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1304149049Spjd#	awk(1).
1305149049Spjdltr()
1306149049Spjd{
1307149049Spjd	local _str _src _dst _out _com
1308149049Spjd	_str=$1
1309149049Spjd	_src=$2
1310149049Spjd	_dst=$3
1311149049Spjd	_out=""
1312149049Spjd
1313149049Spjd	IFS=${_src}
1314149049Spjd	for _com in ${_str}; do
1315149049Spjd		if [ -z "${_out}" ]; then
1316149049Spjd			_out="${_com}"
1317149049Spjd		else
1318149049Spjd			_out="${_out}${_dst}${_com}"
1319149049Spjd		fi
1320149049Spjd	done
1321149049Spjd	echo "${_out}"
1322149049Spjd}
1323149049Spjd
1324149050Spjd# Creates a list of providers for GELI encryption.
1325149050Spjdgeli_make_list()
1326149050Spjd{
1327149050Spjd	local devices devices2
1328149050Spjd	local provider mountpoint type options rest
1329149050Spjd
1330149050Spjd	# Create list of GELI providers from fstab.
1331149050Spjd	while read provider mountpoint type options rest ; do
1332149050Spjd		case ":${provider}" in
1333149050Spjd		:#*)
1334149050Spjd			continue
1335149050Spjd			;;
1336149050Spjd		*.eli)
1337149050Spjd			# Skip swap devices.
1338149050Spjd			if [ "${type}" = "swap" -o "${options}" = "sw" ]; then
1339149050Spjd				continue
1340149050Spjd			fi
1341149050Spjd			devices="${devices} ${provider}"
1342149050Spjd			;;
1343149050Spjd		esac
1344149050Spjd	done < /etc/fstab
1345149050Spjd
1346149050Spjd	# Append providers from geli_devices.
1347149050Spjd	devices="${devices} ${geli_devices}"
1348149050Spjd
1349149050Spjd	for provider in ${devices}; do
1350149050Spjd		provider=${provider%.eli}
1351149050Spjd		provider=${provider#/dev/}
1352149050Spjd		devices2="${devices2} ${provider}"
1353149050Spjd	done
1354149050Spjd
1355149050Spjd	echo ${devices2}
1356149050Spjd}
1357149050Spjd
1358131550Scpercivafi
1359