rc.subr revision 165565
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 165565 2006-12-27 13:15:33Z yar $
378344Sobrien#
4157473Sflz# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
578344Sobrien# All rights reserved.
678344Sobrien#
778344Sobrien# This code is derived from software contributed to The NetBSD Foundation
878344Sobrien# by Luke Mewburn.
978344Sobrien#
1078344Sobrien# Redistribution and use in source and binary forms, with or without
1178344Sobrien# modification, are permitted provided that the following conditions
1278344Sobrien# are met:
1378344Sobrien# 1. Redistributions of source code must retain the above copyright
1478344Sobrien#    notice, this list of conditions and the following disclaimer.
1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1678344Sobrien#    notice, this list of conditions and the following disclaimer in the
1778344Sobrien#    documentation and/or other materials provided with the distribution.
1878344Sobrien# 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
42157473Sflz: ${rcvar_manpage:='rc.conf(5)'}
43157473Sflz
4478344Sobrien#
4598186Sgordon#	Operating System dependent/independent variables
4698186Sgordon#
4798186Sgordon
48131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then
49131550Scperciva
50131550Scperciva_rc_subr_loaded="YES"
51131550Scperciva
5298186SgordonSYSCTL="/sbin/sysctl"
5398186SgordonSYSCTL_N="${SYSCTL} -n"
5498186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype"
55103018SgordonOSTYPE=`${CMD_OSTYPE}`
56124832SmtmID="/usr/bin/id"
57124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
58161435SyarPS="/bin/ps -ww"
59161435SyarJID=`$PS -p $$ -o jid=`
6098186Sgordon
61103018Sgordoncase ${OSTYPE} in
6298186SgordonFreeBSD)
6398186Sgordon	SYSCTL_W="${SYSCTL}"
6498186Sgordon	;;
6598186SgordonNetBSD)
6698186Sgordon	SYSCTL_W="${SYSCTL} -w"
6798186Sgordon	;;
6898186Sgordonesac
6998186Sgordon
7098186Sgordon#
7178344Sobrien#	functions
7278344Sobrien#	---------
7378344Sobrien
7478344Sobrien#
7598186Sgordon# set_rcvar base_var
7698186Sgordon#	Set the variable name enabling a specific service.
7798186Sgordon#	FreeBSD uses ${service}_enable, while NetBSD uses
7898186Sgordon#	just the name of the service. For example:
7998186Sgordon#	FreeBSD: sendmail_enable="YES"
8098186Sgordon#	NetBSD : sendmail="YES"
8198186Sgordon#	$1 - if $name is not the base to work of off, specify
8298186Sgordon#	     a different one
8398186Sgordon#
8498186Sgordonset_rcvar()
8598186Sgordon{
8698186Sgordon	if [ -z "$1" ]; then
8798186Sgordon		base_var=${name}
8898186Sgordon	else
8998186Sgordon		base_var="$1"
9098186Sgordon	fi
9198186Sgordon
92103018Sgordon	case ${OSTYPE} in
9398186Sgordon	FreeBSD)
9498186Sgordon		echo ${base_var}_enable
9598186Sgordon		;;
9698186Sgordon	NetBSD)
9798186Sgordon		echo ${base_var}
9898186Sgordon		;;
9998186Sgordon	*)
10098186Sgordon		echo 'XXX'
10198186Sgordon		;;
10298186Sgordon	esac
10398186Sgordon}
10498186Sgordon
10598186Sgordon#
10698186Sgordon# force_depend script
10798186Sgordon#	Force a service to start. Intended for use by services
10898186Sgordon#	to resolve dependency issues. It is assumed the caller
10998186Sgordon#	has check to make sure this call is necessary
11098186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
11198186Sgordon#
11298186Sgordonforce_depend()
11398186Sgordon{
11498186Sgordon	_depend="$1"
11598186Sgordon
11698186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
117146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
11898186Sgordon		warn "Unable to force ${_depend}. It may already be running."
11998186Sgordon		return 1
12098186Sgordon	fi
12198186Sgordon	return 0
12298186Sgordon}
12398186Sgordon
12498186Sgordon#
12578344Sobrien# checkyesno var
12678344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
12778344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
12878344Sobrien#
12978344Sobriencheckyesno()
13078344Sobrien{
13178344Sobrien	eval _value=\$${1}
13298186Sgordon	debug "checkyesno: $1 is set to $_value."
13378344Sobrien	case $_value in
13478344Sobrien
13578344Sobrien		#	"yes", "true", "on", or "1"
13678344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
13778344Sobrien		return 0
13878344Sobrien		;;
13978344Sobrien
14078344Sobrien		#	"no", "false", "off", or "0"
14178344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
14278344Sobrien		return 1
14378344Sobrien		;;
14478344Sobrien	*)
145157473Sflz		warn "\$${1} is not set properly - see ${rcvar_manpage}."
14678344Sobrien		return 1
14778344Sobrien		;;
14878344Sobrien	esac
14978344Sobrien}
15078344Sobrien
151157473Sflz#
15298186Sgordon# reverse_list list
15398186Sgordon#	print the list in reverse order
15478344Sobrien#
15598186Sgordonreverse_list()
15698186Sgordon{
15798186Sgordon	_revlist=
158126286Smtm	for _revfile; do
15998186Sgordon		_revlist="$_revfile $_revlist"
16098186Sgordon	done
16198186Sgordon	echo $_revlist
16298186Sgordon}
16398186Sgordon
16478344Sobrien#
16598186Sgordon# mount_critical_filesystems type
16698186Sgordon#	Go through the list of critical filesystems as provided in
16798186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
16898186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
16998186Sgordon#
17078344Sobrienmount_critical_filesystems()
17178344Sobrien{
17298186Sgordon	eval _fslist=\$critical_filesystems_${1}
17378344Sobrien	for _fs in $_fslist; do
17478344Sobrien		mount | (
175126285Smtm			_ismounted=false
17678344Sobrien			while read what _on on _type type; do
17778344Sobrien				if [ $on = $_fs ]; then
178126285Smtm					_ismounted=true
17978344Sobrien				fi
18078344Sobrien			done
181126285Smtm			if $_ismounted; then
182126285Smtm				:
183126285Smtm			else
18478344Sobrien				mount $_fs >/dev/null 2>&1
18578344Sobrien			fi
18698186Sgordon		)
18778344Sobrien	done
18878344Sobrien}
18978344Sobrien
19078344Sobrien#
19198186Sgordon# check_pidfile pidfile procname [interpreter]
19298186Sgordon#	Parses the first line of pidfile for a PID, and ensures
19378344Sobrien#	that the process is running and matches procname.
19498186Sgordon#	Prints the matching PID upon success, nothing otherwise.
19598186Sgordon#	interpreter is optional; see _find_processes() for details.
19678344Sobrien#
19778344Sobriencheck_pidfile()
19878344Sobrien{
19978344Sobrien	_pidfile=$1
20078344Sobrien	_procname=$2
20198186Sgordon	_interpreter=$3
20278344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
20398186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
20478344Sobrien	fi
20578344Sobrien	if [ ! -f $_pidfile ]; then
206131061Smtm		debug "pid file ($_pidfile): not readable."
20778344Sobrien		return
20878344Sobrien	fi
20978344Sobrien	read _pid _junk < $_pidfile
21078344Sobrien	if [ -z "$_pid" ]; then
211139949Skeramida		debug "pid file ($_pidfile): no pid in file."
21278344Sobrien		return
21378344Sobrien	fi
21498186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
21578344Sobrien}
21678344Sobrien
21778344Sobrien#
21898186Sgordon# check_process procname [interpreter]
21978344Sobrien#	Ensures that a process (or processes) named procname is running.
22098186Sgordon#	Prints a list of matching PIDs.
22198186Sgordon#	interpreter is optional; see _find_processes() for details.
22278344Sobrien#
22378344Sobriencheck_process()
22478344Sobrien{
22578344Sobrien	_procname=$1
22698186Sgordon	_interpreter=$2
22778344Sobrien	if [ -z "$_procname" ]; then
22898186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
22978344Sobrien	fi
23098186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
23198186Sgordon}
23298186Sgordon
23398186Sgordon#
23498186Sgordon# _find_processes procname interpreter psargs
23598186Sgordon#	Search for procname in the output of ps generated by psargs.
23698186Sgordon#	Prints the PIDs of any matching processes, space separated.
23798186Sgordon#
23898186Sgordon#	If interpreter == ".", check the following variations of procname
23998186Sgordon#	against the first word of each command:
24098186Sgordon#		procname
24198186Sgordon#		`basename procname`
24298186Sgordon#		`basename procname` + ":"
24398186Sgordon#		"(" + `basename procname` + ")"
244155719Sceri#		"[" + `basename procname` + "]"
24598186Sgordon#
24698186Sgordon#	If interpreter != ".", read the first line of procname, remove the
24798186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
24898186Sgordon#	match that against each command, either as is, or with extra words
249157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
250157841Sflz#	using perl, the basename of the interpreter plus a colon is also
251157841Sflz#	tried as the prefix to procname.
25298186Sgordon#
25398186Sgordon_find_processes()
25498186Sgordon{
25598186Sgordon	if [ $# -ne 3 ]; then
25698186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
25798186Sgordon	fi
25898186Sgordon	_procname=$1
25998186Sgordon	_interpreter=$2
26098186Sgordon	_psargs=$3
26198186Sgordon
26278344Sobrien	_pref=
26398186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
264164640Sflz		read _interp < ${_chroot:-}/$_procname	# read interpreter name
26598186Sgordon		_interp=${_interp#\#!}		# strip #!
26698186Sgordon		set -- $_interp
26798186Sgordon		if [ $_interpreter != $1 ]; then
26898186Sgordon			warn "\$command_interpreter $_interpreter != $1"
26978344Sobrien		fi
27098186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
271157841Sflz		_interpbn=${1##*/}
27298186Sgordon		_fp_args='_argv'
27398186Sgordon		_fp_match='case "$_argv" in
274157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
27598186Sgordon	else					# a normal daemon
27698186Sgordon		_procnamebn=${_procname##*/}
27798186Sgordon		_fp_args='_arg0 _argv'
27898186Sgordon		_fp_match='case "$_arg0" in
279151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
28098186Sgordon	fi
28198186Sgordon
282161435Syar	_proccheck="\
283161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
284157657Sflz		while read _npid _jid '"$_fp_args"'; do
285161436Syar			'"$_fp_match"'
286157657Sflz				if [ "$JID" -eq "$_jid" ];
287157657Sflz				then echo -n "$_pref$_npid";
288157657Sflz				_pref=" ";
289157657Sflz				fi
29098186Sgordon				;;
29198186Sgordon			esac
29298186Sgordon		done'
29398186Sgordon
294114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
29598186Sgordon	eval $_proccheck
29698186Sgordon}
29798186Sgordon
29898186Sgordon#
29998186Sgordon# wait_for_pids pid [pid ...]
30098186Sgordon#	spins until none of the pids exist
30198186Sgordon#
30298186Sgordonwait_for_pids()
30398186Sgordon{
304126286Smtm	_list="$@"
30598186Sgordon	if [ -z "$_list" ]; then
30698186Sgordon		return
30798186Sgordon	fi
30898186Sgordon	_prefix=
30998186Sgordon	while true; do
31098186Sgordon		_nlist="";
31198186Sgordon		for _j in $_list; do
31298186Sgordon			if kill -0 $_j 2>/dev/null; then
31398186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
31498186Sgordon			fi
31598186Sgordon		done
31698186Sgordon		if [ -z "$_nlist" ]; then
31798186Sgordon			break
31878344Sobrien		fi
31998186Sgordon		_list=$_nlist
32098186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
32198186Sgordon		_prefix=", "
32298186Sgordon		sleep 2
32378344Sobrien	done
32498186Sgordon	if [ -n "$_prefix" ]; then
32598186Sgordon		echo "."
32698186Sgordon	fi
32778344Sobrien}
32878344Sobrien
32978344Sobrien#
33098186Sgordon# run_rc_command argument
33198186Sgordon#	Search for argument in the list of supported commands, which is:
33298186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
33398186Sgordon#	If there's a match, run ${argument}_cmd or the default method
33498186Sgordon#	(see below).
33578344Sobrien#
33698186Sgordon#	If argument has a given prefix, then change the operation as follows:
33798186Sgordon#		Prefix	Operation
33878344Sobrien#		------	---------
33998186Sgordon#		fast	Skip the pid check, and set rc_fast=yes
34098186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
341126303Smtm#		one	Set ${rcvar} to YES
34278344Sobrien#
34378344Sobrien#	The following globals are used:
34478344Sobrien#
34598186Sgordon#	Name		Needed	Purpose
34698186Sgordon#	----		------	-------
34778344Sobrien#	name		y	Name of script.
34878344Sobrien#
34978344Sobrien#	command		n	Full path to command.
35098186Sgordon#				Not needed if ${rc_arg}_cmd is set for
35178344Sobrien#				each keyword.
35278344Sobrien#
35378344Sobrien#	command_args	n	Optional args/shell directives for command.
35478344Sobrien#
35598186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
35698186Sgordon#				call check_{pidfile,process}() appropriately.
35798186Sgordon#
35878344Sobrien#	extra_commands	n	List of extra commands supported.
35978344Sobrien#
36098186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
36198186Sgordon#				otherwise use check_process $command.
36298186Sgordon#				In either case, only check if $command is set.
36378344Sobrien#
36498186Sgordon#	procname	n	Process name to check for instead of $command.
36598186Sgordon#
36678344Sobrien#	rcvar		n	This is checked with checkyesno to determine
36778344Sobrien#				if the action should be run.
36878344Sobrien#
369157653Sflz#	${name}_program	n	Full path to command.
370157653Sflz#				Meant to be used in /etc/rc.conf to override
371157653Sflz#				${command}.
372157653Sflz#
37378344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
37498186Sgordon#				Requires /usr to be mounted.
37578344Sobrien#
37678344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
37778344Sobrien#				(if not using ${name}_chroot).
37878344Sobrien#
37978344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
38078344Sobrien#				NOTE:	$flags from the parent environment
38178344Sobrien#					can be used to override this.
38278344Sobrien#
38378344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
38478344Sobrien#
38578344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
38678344Sobrien#				using ${name}_chroot.
38798186Sgordon#				Requires /usr to be mounted.
38878344Sobrien#
38978344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
39098186Sgordon#				Requires /usr to be mounted.
39178344Sobrien#
39298186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
39398186Sgordon#				to run the chrooted ${command} with.
39498186Sgordon#				Requires /usr to be mounted.
39578344Sobrien#
39698186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
39778344Sobrien#				Otherwise, use default command (see below)
39878344Sobrien#
39998186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
40098186Sgordon#				${rc_arg}_cmd method in the default
40198186Sgordon#				operation (i.e, after checking for required
40298186Sgordon#				bits and process (non)existence).
40378344Sobrien#				If this completes with a non-zero exit code,
40498186Sgordon#				don't run ${rc_arg}_cmd.
40578344Sobrien#
40698186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
40798186Sgordon#				${rc_arg}_cmd method, if that method
40898186Sgordon#				returned a zero exit code.
40998186Sgordon#
41078344Sobrien#	required_dirs	n	If set, check for the existence of the given
411165565Syar#				directories before running a (re)start command.
41278344Sobrien#
41378344Sobrien#	required_files	n	If set, check for the readability of the given
414165565Syar#				files before running a (re)start command.
41578344Sobrien#
416165565Syar#	required_modules n	If set, ensure the given kernel modules are
417165565Syar#				loaded before running a (re)start command.
418165565Syar#				The check and possible loads are actually
419165565Syar#				done after start_precmd so that the modules
420165565Syar#				aren't loaded in vain, should the precmd
421165565Syar#				return a non-zero status to indicate a error.
422165565Syar#				If a word in the list looks like "foo:bar",
423165565Syar#				"foo" is the KLD file name and "bar" is the
424165565Syar#				module name.  If a word looks like "foo~bar",
425165565Syar#				"foo" is the KLD file name and "bar" is a
426165565Syar#				egrep(1) pattern matching the module name.
427165565Syar#				Otherwise the module name is assumed to be
428165565Syar#				the same as the KLD file name, which is most
429165565Syar#				common.  See load_kld().
430165565Syar#
43178344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
43278344Sobrien#				listed variables before running the default
43378344Sobrien#				(re)start command.
43478344Sobrien#
43598186Sgordon#	Default behaviour for a given argument, if no override method is
43698186Sgordon#	provided:
43778344Sobrien#
43898186Sgordon#	Argument	Default behaviour
43998186Sgordon#	--------	-----------------
44078344Sobrien#	start		if !running && checkyesno ${rcvar}
44178344Sobrien#				${command}
44278344Sobrien#
44378344Sobrien#	stop		if ${pidfile}
44498186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
44578344Sobrien#			else
44698186Sgordon#				rc_pid=$(check_process $command)
44798186Sgordon#			kill $sig_stop $rc_pid
44898186Sgordon#			wait_for_pids $rc_pid
44998186Sgordon#			($sig_stop defaults to TERM.)
45078344Sobrien#
45198186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
45298186Sgordon#			and doesn't wait_for_pids.
45378344Sobrien#			$sig_reload defaults to HUP.
454151685Syar#			Note that `reload' isn't provided by default,
455151685Syar#			it should be enabled via $extra_commands.
45678344Sobrien#
45778344Sobrien#	restart		Run `stop' then `start'.
45878344Sobrien#
45998186Sgordon#	status		Show if ${command} is running, etc.
46078344Sobrien#
46198186Sgordon#	poll		Wait for ${command} to exit.
46298186Sgordon#
46398186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
46498186Sgordon#
46598186Sgordon#	Variables available to methods, and after run_rc_command() has
46698186Sgordon#	completed:
46798186Sgordon#
46898186Sgordon#	Variable	Purpose
46998186Sgordon#	--------	-------
470126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
47198186Sgordon#			performed
47298186Sgordon#
47398186Sgordon#	rc_flags	Flags to start the default command with.
47498186Sgordon#			Defaults to ${name}_flags, unless overridden
47598186Sgordon#			by $flags from the environment.
47698186Sgordon#			This variable may be changed by the precmd method.
47798186Sgordon#
47898186Sgordon#	rc_pid		PID of command (if appropriate)
47998186Sgordon#
48098186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
48198186Sgordon#
48298186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
48398186Sgordon#
48498186Sgordon#
48578344Sobrienrun_rc_command()
48678344Sobrien{
487116097Smtm	_return=0
48898186Sgordon	rc_arg=$1
48978344Sobrien	if [ -z "$name" ]; then
49098186Sgordon		err 3 'run_rc_command: $name is not set.'
49178344Sobrien	fi
49278344Sobrien
493132892Smtm	# Don't repeat the first argument when passing additional command-
494132892Smtm	# line arguments to the command subroutines.
495132892Smtm	#
496132892Smtm	shift 1
497132892Smtm	rc_extra_args="$*"
498132892Smtm
499126303Smtm	_rc_prefix=
50098186Sgordon	case "$rc_arg" in
50178344Sobrien	fast*)				# "fast" prefix; don't check pid
50298186Sgordon		rc_arg=${rc_arg#fast}
50398186Sgordon		rc_fast=yes
50478344Sobrien		;;
505126303Smtm	force*)				# "force prefix; always run
50698186Sgordon		rc_force=yes
507126303Smtm		_rc_prefix=force
508126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
50978344Sobrien		if [ -n "${rcvar}" ]; then
51078344Sobrien			eval ${rcvar}=YES
51178344Sobrien		fi
51278344Sobrien		;;
513126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
514126303Smtm		_rc_prefix=one
515126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
516126303Smtm		if [ -n "${rcvar}" ]; then
517126303Smtm			eval ${rcvar}=YES
518126303Smtm		fi
519126303Smtm		;;
52078344Sobrien	esac
52178344Sobrien
522161530Sflz	eval _override_command=\$${name}_program
523161530Sflz	command=${command:+${_override_command:-$command}}
524161530Sflz
52578344Sobrien	_keywords="start stop restart rcvar $extra_commands"
52698186Sgordon	rc_pid=
52778344Sobrien	_pidcmd=
52898186Sgordon	_procname=${procname:-${command}}
52998186Sgordon
530131135Smtm					# setup pid check command
531131135Smtm	if [ -n "$_procname" ]; then
53278344Sobrien		if [ -n "$pidfile" ]; then
53398186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
53498186Sgordon		else
53598186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
53678344Sobrien		fi
53778344Sobrien		if [ -n "$_pidcmd" ]; then
53898186Sgordon			_keywords="${_keywords} status poll"
53978344Sobrien		fi
54078344Sobrien	fi
54178344Sobrien
54298186Sgordon	if [ -z "$rc_arg" ]; then
543150796Syar		rc_usage $_keywords
54478344Sobrien	fi
54578344Sobrien
54678344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
54798186Sgordon		rc_flags=$flags
54878344Sobrien	else
54998186Sgordon		eval rc_flags=\$${name}_flags
55078344Sobrien	fi
55198186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
55298186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
55398186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
55478344Sobrien
55598186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
556124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
55798186Sgordon			unset _user
55898186Sgordon		fi
55998186Sgordon	fi
56098186Sgordon
56178344Sobrien					# if ${rcvar} is set, and $1 is not
56298186Sgordon					# "rcvar", then run
56378344Sobrien					#	checkyesno ${rcvar}
56478344Sobrien					# and return if that failed
56578344Sobrien					#
56698186Sgordon	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
56778344Sobrien		if ! checkyesno ${rcvar}; then
56878344Sobrien			return 0
56978344Sobrien		fi
57078344Sobrien	fi
57178344Sobrien
57278344Sobrien	eval $_pidcmd			# determine the pid if necessary
57378344Sobrien
57478344Sobrien	for _elem in $_keywords; do
57598186Sgordon		if [ "$_elem" != "$rc_arg" ]; then
57678344Sobrien			continue
57778344Sobrien		fi
57878344Sobrien					# if there's a custom ${XXX_cmd},
57978344Sobrien					# run that instead of the default
58078344Sobrien					#
581165565Syar		eval _cmd=\$${rc_arg}_cmd \
582165565Syar		     _precmd=\$${rc_arg}_precmd \
583165565Syar		     _postcmd=\$${rc_arg}_postcmd
584165565Syar
58578344Sobrien		if [ -n "$_cmd" ]; then
586165565Syar			_run_rc_precmd || return 1
587165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
588165565Syar			_run_rc_postcmd
589116097Smtm			return $_return
59078344Sobrien		fi
59178344Sobrien
59298186Sgordon		case "$rc_arg" in	# default operations...
59378344Sobrien
59478344Sobrien		status)
595165565Syar			_run_rc_precmd || return 1
59698186Sgordon			if [ -n "$rc_pid" ]; then
59798186Sgordon				echo "${name} is running as pid $rc_pid."
59878344Sobrien			else
59978344Sobrien				echo "${name} is not running."
60078344Sobrien				return 1
60178344Sobrien			fi
602165565Syar			_run_rc_postcmd
60378344Sobrien			;;
60478344Sobrien
60578344Sobrien		start)
606131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
607157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
608153152Syar				return 1
60978344Sobrien			fi
61078344Sobrien
611126287Smtm			if [ ! -x ${_chroot}${command} ]; then
612160667Syar				warn "run_rc_command: cannot run $command"
613153152Syar				return 1
61478344Sobrien			fi
61578344Sobrien
616165565Syar			_run_rc_precmd || return 1
61778344Sobrien
618160668Syar					# setup the full command to run
61978344Sobrien					#
62078344Sobrien			echo "Starting ${name}."
62178344Sobrien			if [ -n "$_chroot" ]; then
62278344Sobrien				_doit="\
62378344Sobrien${_nice:+nice -n $_nice }\
62478344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
62598186Sgordon$_chroot $command $rc_flags $command_args"
62678344Sobrien			else
62778344Sobrien				_doit="\
628161396Syar${_chdir:+cd $_chdir && }\
62998186Sgordon$command $rc_flags $command_args"
63098186Sgordon				if [ -n "$_user" ]; then
63198186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
63298186Sgordon				fi
633161396Syar				if [ -n "$_nice" ]; then
634161396Syar					if [ -z "$_user" ]; then
635161396Syar						_doit="sh -c \"$_doit\""
636161396Syar					fi	
637161396Syar					_doit="nice -n $_nice $_doit"
638161396Syar				fi
63978344Sobrien			fi
64098186Sgordon
641165565Syar					# run the full command
64298186Sgordon					#
643165565Syar			_run_rc_doit "$_doit" || return 1
64498186Sgordon
64598186Sgordon					# finally, run postcmd
64698186Sgordon					#
647165565Syar			_run_rc_postcmd
64878344Sobrien			;;
64978344Sobrien
65078344Sobrien		stop)
65198186Sgordon			if [ -z "$rc_pid" ]; then
652153152Syar				[ -n "$rc_fast" ] && return 0
653165565Syar				_run_rc_notrunning
654153152Syar				return 1
65578344Sobrien			fi
65678344Sobrien
657165565Syar			_run_rc_precmd || return 1
65898186Sgordon
65998186Sgordon					# send the signal to stop
66098186Sgordon					#
66178344Sobrien			echo "Stopping ${name}."
662165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
663165565Syar			_run_rc_doit "$_doit" || return 1
66498186Sgordon
66598186Sgordon					# wait for the command to exit,
66698186Sgordon					# and run postcmd.
66798186Sgordon			wait_for_pids $rc_pid
668165565Syar
669165565Syar			_run_rc_postcmd
67078344Sobrien			;;
67178344Sobrien
67278344Sobrien		reload)
67398186Sgordon			if [ -z "$rc_pid" ]; then
674165565Syar				_run_rc_notrunning
675153152Syar				return 1
67678344Sobrien			fi
677165565Syar
678165565Syar			_run_rc_precmd || return 1
679165565Syar
680165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
681165565Syar			_run_rc_doit "$_doit" || return 1
682165565Syar
683165565Syar			_run_rc_postcmd
68478344Sobrien			;;
68578344Sobrien
68678344Sobrien		restart)
68778344Sobrien					# prevent restart being called more
68878344Sobrien					# than once by any given script
68978344Sobrien					#
690126285Smtm			if ${_rc_restart_done:-false}; then
69178344Sobrien				return 0
69278344Sobrien			fi
693126285Smtm			_rc_restart_done=true
69478344Sobrien
695165565Syar			_run_rc_precmd || return 1
696165565Syar
697165565Syar			# run those in a subshell to keep global variables
698152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
699165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
700165565Syar			_return=$?
701165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
70298186Sgordon
703165565Syar			_run_rc_postcmd
70478344Sobrien			;;
70578344Sobrien
70698186Sgordon		poll)
707165565Syar			_run_rc_precmd || return 1
70898186Sgordon			if [ -n "$rc_pid" ]; then
70998186Sgordon				wait_for_pids $rc_pid
71098186Sgordon			fi
711165565Syar			_run_rc_postcmd
71298186Sgordon			;;
71398186Sgordon
71478344Sobrien		rcvar)
71578344Sobrien			echo "# $name"
71678344Sobrien			if [ -n "$rcvar" ]; then
71778344Sobrien				if checkyesno ${rcvar}; then
718164629Sflz					echo "${rcvar}=YES"
71978344Sobrien				else
720164629Sflz					echo "${rcvar}=NO"
72178344Sobrien				fi
72278344Sobrien			fi
72378344Sobrien			;;
72478344Sobrien
72578344Sobrien		*)
726150796Syar			rc_usage $_keywords
72778344Sobrien			;;
72878344Sobrien
72978344Sobrien		esac
730116097Smtm		return $_return
73178344Sobrien	done
73278344Sobrien
73398186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
734150796Syar	rc_usage $_keywords
735153152Syar	# not reached
73678344Sobrien}
73778344Sobrien
73878344Sobrien#
739165565Syar# Helper functions for run_rc_command: common code.
740165565Syar# They use such global variables besides the exported rc_* ones:
741165565Syar#
742165565Syar#	name	       R/W
743165565Syar#	------------------
744165565Syar#	_precmd		R
745165565Syar#	_postcmd	R
746165565Syar#	_return		W
747165565Syar#
748165565Syar_run_rc_precmd()
749165565Syar{
750165565Syar	check_required_before "$rc_arg" || return 1
751165565Syar
752165565Syar	if [ -n "$_precmd" ]; then
753165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
754165565Syar		eval "$_precmd $rc_extra_args"
755165565Syar		_return=$?
756165565Syar
757165565Syar		# If precmd failed and force isn't set, request exit.
758165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
759165565Syar			return 1
760165565Syar		fi
761165565Syar	fi
762165565Syar
763165565Syar	check_required_after "$rc_arg" || return 1
764165565Syar
765165565Syar	return 0
766165565Syar}
767165565Syar
768165565Syar_run_rc_postcmd()
769165565Syar{
770165565Syar	if [ -n "$_postcmd" ]; then
771165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
772165565Syar		eval "$_postcmd $rc_extra_args"
773165565Syar		_return=$?
774165565Syar	fi
775165565Syar	return 0
776165565Syar}
777165565Syar
778165565Syar_run_rc_doit()
779165565Syar{
780165565Syar	debug "run_rc_command: doit: $*"
781165565Syar	eval "$@"
782165565Syar	_return=$?
783165565Syar
784165565Syar	# If command failed and force isn't set, request exit.
785165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
786165565Syar		return 1
787165565Syar	fi
788165565Syar
789165565Syar	return 0
790165565Syar}
791165565Syar
792165565Syar_run_rc_notrunning()
793165565Syar{
794165565Syar	local _pidmsg
795165565Syar
796165565Syar	if [ -n "$pidfile" ]; then
797165565Syar		_pidmsg=" (check $pidfile)."
798165565Syar	else
799165565Syar		_pidmsg=
800165565Syar	fi
801165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
802165565Syar}
803165565Syar
804165565Syar_run_rc_killcmd()
805165565Syar{
806165565Syar	local _cmd
807165565Syar
808165565Syar	_cmd="kill -$1 $rc_pid"
809165565Syar	if [ -n "$_user" ]; then
810165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
811165565Syar	fi
812165565Syar	echo "$_cmd"
813165565Syar}
814165565Syar
815165565Syar#
81678344Sobrien# run_rc_script file arg
81778344Sobrien#	Start the script `file' with `arg', and correctly handle the
81878344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
81998186Sgordon#	sourced into the current environment.  If `file' appears to be
82098186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
82198186Sgordon#	executable run as a child process.
82278344Sobrien#
82378344Sobrienrun_rc_script()
82478344Sobrien{
82578344Sobrien	_file=$1
82678344Sobrien	_arg=$2
82778344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
82878344Sobrien		err 3 'USAGE: run_rc_script file arg'
82978344Sobrien	fi
83078344Sobrien
83198186Sgordon	unset	name command command_args command_interpreter \
83298186Sgordon		extra_commands pidfile procname \
83398186Sgordon		rcvar required_dirs required_files required_vars
83498186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
83598186Sgordon
83678344Sobrien	case "$_file" in
837153105Sdougb	/etc/rc.d/*.sh)			# run in current shell
838146490Sschweikh		set $_arg; . $_file
83978344Sobrien		;;
840153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
84198186Sgordon		warn "Ignoring scratch file $_file"
84298186Sgordon		;;
84378344Sobrien	*)				# run in subshell
84498186Sgordon		if [ -x $_file ]; then
84598186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
846146490Sschweikh				set $_arg; . $_file
84798186Sgordon			else
848130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
849130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
850146490Sschweikh				  set $_arg; . $_file )
85198186Sgordon			fi
85298186Sgordon		fi
85378344Sobrien		;;
85478344Sobrien	esac
85578344Sobrien}
85678344Sobrien
85778344Sobrien#
858157653Sflz# load_rc_config name
859157653Sflz#	Source in the configuration file for a given name.
86078344Sobrien#
86178344Sobrienload_rc_config()
86278344Sobrien{
863157653Sflz	_name=$1
864157653Sflz	if [ -z "$_name" ]; then
865157653Sflz		err 3 'USAGE: load_rc_config name'
86678344Sobrien	fi
86778344Sobrien
868126285Smtm	if ${_rc_conf_loaded:-false}; then
869126285Smtm		:
870126285Smtm	else
87198186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
87298186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
87398186Sgordon			. /etc/defaults/rc.conf
87498186Sgordon			source_rc_confs
87598186Sgordon		elif [ -r /etc/rc.conf ]; then
87698186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
87798186Sgordon			. /etc/rc.conf
87898186Sgordon		fi
879126285Smtm		_rc_conf_loaded=true
88098186Sgordon	fi
881161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
882157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
883161530Sflz		. /etc/rc.conf.d/"$_name"
884157653Sflz	fi
885157653Sflz
886101850Sgordon	# XXX - Deprecated variable name support
887101850Sgordon	#
888103018Sgordon	case ${OSTYPE} in
889101850Sgordon	FreeBSD)
890146490Sschweikh		[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
891146490Sschweikh		[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
892146490Sschweikh		[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
893146490Sschweikh		[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
894146490Sschweikh		[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
895146490Sschweikh		[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
896146490Sschweikh		[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
897115950Smtm		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
898115950Smtm		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
899146490Sschweikh		;;
900101850Sgordon	esac
90178344Sobrien}
902157473Sflz  
903157473Sflz#
904157653Sflz# load_rc_config_var name var
905157653Sflz#	Read the rc.conf(5) var for name and set in the
906157473Sflz#	current shell, using load_rc_config in a subshell to prevent
907157473Sflz#	unwanted side effects from other variable assignments.
908157473Sflz#
909157473Sflzload_rc_config_var()
910157473Sflz{
911157473Sflz	if [ $# -ne 2 ]; then
912157653Sflz		err 3 'USAGE: load_rc_config_var name var'
913157473Sflz	fi
914157473Sflz	eval $(eval '(
915157473Sflz		load_rc_config '$1' >/dev/null;
916157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
917157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
918157473Sflz		fi
919157473Sflz	)' )
920157473Sflz}
92178344Sobrien
92278344Sobrien#
92378344Sobrien# rc_usage commands
92478344Sobrien#	Print a usage string for $0, with `commands' being a list of
92578344Sobrien#	valid commands.
92678344Sobrien#
92778344Sobrienrc_usage()
92878344Sobrien{
929126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
93078344Sobrien
93178344Sobrien	_sep=
932126286Smtm	for _elem; do
93378344Sobrien		echo -n 1>&2 "$_sep$_elem"
93478344Sobrien		_sep="|"
93578344Sobrien	done
93678344Sobrien	echo 1>&2 ")"
93778344Sobrien	exit 1
93878344Sobrien}
93978344Sobrien
94078344Sobrien#
94178344Sobrien# err exitval message
94278344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
94378344Sobrien#
94478344Sobrienerr()
94578344Sobrien{
94678344Sobrien	exitval=$1
94778344Sobrien	shift
94878344Sobrien
949106643Sgordon	if [ -x /usr/bin/logger ]; then
950106643Sgordon		logger "$0: ERROR: $*"
951106643Sgordon	fi
952106643Sgordon	echo 1>&2 "$0: ERROR: $*"
95378344Sobrien	exit $exitval
95478344Sobrien}
95578344Sobrien
95678344Sobrien#
95778344Sobrien# warn message
95878344Sobrien#	Display message to stderr and log to the syslog.
95978344Sobrien#
96078344Sobrienwarn()
96178344Sobrien{
962106643Sgordon	if [ -x /usr/bin/logger ]; then
963106643Sgordon		logger "$0: WARNING: $*"
964106643Sgordon	fi
965106643Sgordon	echo 1>&2 "$0: WARNING: $*"
96678344Sobrien}
96798186Sgordon
96898186Sgordon#
96998186Sgordon# info message
97098186Sgordon#	Display informational message to stdout and log to syslog.
97198186Sgordon#
97298186Sgordoninfo()
97398186Sgordon{
974119170Smtm	case ${rc_info} in
975119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
976119170Smtm		if [ -x /usr/bin/logger ]; then
977119170Smtm			logger "$0: INFO: $*"
978119170Smtm		fi
979119170Smtm		echo "$0: INFO: $*"
980119170Smtm		;;
981119170Smtm	esac
98298186Sgordon}
98398186Sgordon
98498186Sgordon#
98598186Sgordon# debug message
986106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
98798186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
98898186Sgordon#	function.
98998186Sgordon#
99098186Sgordondebug()
99198186Sgordon{
99298186Sgordon	case ${rc_debug} in
99398186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
994106700Sgordon		if [ -x /usr/bin/logger ]; then
995162947Syar			logger "$0: DEBUG: $*"
996106700Sgordon		fi
997146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
99898186Sgordon		;;
99998186Sgordon	esac
100098186Sgordon}
100198186Sgordon
100298186Sgordon#
100398186Sgordon# backup_file action file cur backup
100498186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
100598186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
100698186Sgordon#
100798186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
100898186Sgordon#	which can be either YES or NO.
100998186Sgordon#
101098186Sgordon#	The `action' keyword can be one of the following:
101198186Sgordon#
101298186Sgordon#	add		`file' is now being backed up (and is possibly
101398186Sgordon#			being reentered into the backups system).  `cur'
101498186Sgordon#			is created and RCS files, if necessary, are
101598186Sgordon#			created as well.
101698186Sgordon#
101798186Sgordon#	update		`file' has changed and needs to be backed up.
101898186Sgordon#			If `cur' exists, it is copied to to `back' or
101998186Sgordon#			checked into RCS (if the repository file is old),
102098186Sgordon#			and then `file' is copied to `cur'.  Another RCS
102198186Sgordon#			check in done here if RCS is being used.
102298186Sgordon#
102398186Sgordon#	remove		`file' is no longer being tracked by the backups
102498186Sgordon#			system.  If RCS is not being used, `cur' is moved
102598186Sgordon#			to `back', otherwise an empty file is checked in,
102698186Sgordon#			and then `cur' is removed.
102798186Sgordon#
102898186Sgordon#
102998186Sgordonbackup_file()
103098186Sgordon{
103198186Sgordon	_action=$1
103298186Sgordon	_file=$2
103398186Sgordon	_cur=$3
103498186Sgordon	_back=$4
103598186Sgordon
103698186Sgordon	if checkyesno backup_uses_rcs; then
103798186Sgordon		_msg0="backup archive"
103898186Sgordon		_msg1="update"
103998186Sgordon
104098186Sgordon		# ensure that history file is not locked
104198186Sgordon		if [ -f $_cur,v ]; then
104298186Sgordon			rcs -q -u -U -M $_cur
104398186Sgordon		fi
104498186Sgordon
104598186Sgordon		# ensure after switching to rcs that the
104698186Sgordon		# current backup is not lost
104798186Sgordon		if [ -f $_cur ]; then
104898186Sgordon			# no archive, or current newer than archive
104998186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
105098186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
105198186Sgordon				rcs -q -kb -U $_cur
105298186Sgordon				co -q -f -u $_cur
105398186Sgordon			fi
105498186Sgordon		fi
105598186Sgordon
105698186Sgordon		case $_action in
105798186Sgordon		add|update)
105898186Sgordon			cp -p $_file $_cur
105998186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
106098186Sgordon			rcs -q -kb -U $_cur
106198186Sgordon			co -q -f -u $_cur
106298186Sgordon			chown root:wheel $_cur $_cur,v
106398186Sgordon			;;
106498186Sgordon		remove)
106598186Sgordon			cp /dev/null $_cur
106698186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
106798186Sgordon			rcs -q -kb -U $_cur
106898186Sgordon			chown root:wheel $_cur $_cur,v
106998186Sgordon			rm $_cur
107098186Sgordon			;;
107198186Sgordon		esac
107298186Sgordon	else
107398186Sgordon		case $_action in
107498186Sgordon		add|update)
107598186Sgordon			if [ -f $_cur ]; then
107698186Sgordon				cp -p $_cur $_back
107798186Sgordon			fi
107898186Sgordon			cp -p $_file $_cur
107998186Sgordon			chown root:wheel $_cur
108098186Sgordon			;;
108198186Sgordon		remove)
108298186Sgordon			mv -f $_cur $_back
108398186Sgordon			;;
108498186Sgordon		esac
108598186Sgordon	fi
108698186Sgordon}
1087119166Smtm
1088123344Smtm# make_symlink src link
1089123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1090123344Smtm#	directory in which link is to be created does not exist
1091123344Smtm#	a warning will be displayed and an error will be returned.
1092123344Smtm#	Returns 0 on sucess, 1 otherwise.
1093119166Smtm#
1094123344Smtmmake_symlink()
1095119166Smtm{
1096123344Smtm	local src link linkdir _me
1097123344Smtm	src="$1"
1098123344Smtm	link="$2"
1099123344Smtm	linkdir="`dirname $link`"
1100123344Smtm	_me="make_symlink()"
1101119166Smtm
1102123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1103123344Smtm		warn "$_me: requires two arguments."
1104119166Smtm		return 1
1105119166Smtm	fi
1106123344Smtm	if [ ! -d "$linkdir" ]; then
1107160667Syar		warn "$_me: the directory $linkdir does not exist."
1108119166Smtm		return 1
1109119166Smtm	fi
1110146490Sschweikh	if ! ln -sf $src $link; then
1111123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1112119166Smtm		return 1
1113119166Smtm	fi
1114119166Smtm	return 0
1115119166Smtm}
1116119166Smtm
1117119166Smtm# devfs_rulesets_from_file file
1118119166Smtm#	Reads a set of devfs commands from file, and creates
1119119166Smtm#	the specified rulesets with their rules. Returns non-zero
1120119166Smtm#	if there was an error.
1121119166Smtm#
1122119166Smtmdevfs_rulesets_from_file()
1123119166Smtm{
1124119166Smtm	local file _err _me
1125119166Smtm	file="$1"
1126119166Smtm	_me="devfs_rulesets_from_file"
1127119166Smtm	_err=0
1128119166Smtm
1129119166Smtm	if [ -z "$file" ]; then
1130119166Smtm		warn "$_me: you must specify a file"
1131119166Smtm		return 1
1132119166Smtm	fi
1133119166Smtm	if [ ! -e "$file" ]; then
1134119166Smtm		debug "$_me: no such file ($file)"
1135119166Smtm		return 0
1136119166Smtm	fi
1137119166Smtm	debug "reading rulesets from file ($file)"
1138119166Smtm	{ while read line
1139119166Smtm	do
1140119166Smtm		case $line in
1141119166Smtm		\#*)
1142119166Smtm			continue
1143119166Smtm			;;
1144119166Smtm		\[*\]*)
1145119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1146119166Smtm			if [ -z "$rulenum" ]; then
1147119166Smtm				warn "$_me: cannot extract rule number ($line)"
1148119166Smtm				_err=1
1149119166Smtm				break
1150119166Smtm			fi
1151119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1152119166Smtm			if [ -z "$rulename" ]; then
1153119166Smtm				warn "$_me: cannot extract rule name ($line)"
1154119166Smtm				_err=1
1155119166Smtm				break;
1156119166Smtm			fi
1157119166Smtm			eval $rulename=\$rulenum
1158119166Smtm			debug "found ruleset: $rulename=$rulenum"
1159146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1160119166Smtm				_err=1
1161119166Smtm				break
1162119166Smtm			fi
1163119166Smtm			;;
1164119166Smtm		*)
1165119166Smtm			rulecmd="${line%%"\#*"}"
1166119166Smtm			# evaluate the command incase it includes
1167119166Smtm			# other rules
1168119166Smtm			if [ -n "$rulecmd" ]; then
1169119166Smtm				debug "adding rule ($rulecmd)"
1170119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1171119166Smtm				then
1172119166Smtm					_err=1
1173119166Smtm					break
1174119166Smtm				fi
1175119166Smtm			fi
1176119166Smtm			;;
1177119166Smtm		esac
1178119166Smtm		if [ $_err -ne 0 ]; then
1179119166Smtm			debug "error in $_me"
1180119166Smtm			break
1181119166Smtm		fi
1182119166Smtm	done } < $file
1183119166Smtm	return $_err
1184119166Smtm}
1185119166Smtm
1186119166Smtm# devfs_init_rulesets
1187119166Smtm#	Initializes rulesets from configuration files. Returns
1188119166Smtm#	non-zero if there was an error.
1189119166Smtm#
1190119166Smtmdevfs_init_rulesets()
1191119166Smtm{
1192119166Smtm	local file _me
1193119166Smtm	_me="devfs_init_rulesets"
1194119166Smtm
1195119166Smtm	# Go through this only once
1196119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1197119166Smtm		debug "$_me: devfs rulesets already initialized"
1198119166Smtm		return
1199119166Smtm	fi
1200146490Sschweikh	for file in $devfs_rulesets; do
1201119166Smtm		devfs_rulesets_from_file $file || return 1
1202119166Smtm	done
1203119166Smtm	devfs_rulesets_init=1
1204119166Smtm	debug "$_me: devfs rulesets initialized"
1205119166Smtm	return 0
1206119166Smtm}
1207119166Smtm
1208119166Smtm# devfs_set_ruleset ruleset [dir]
1209151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1210119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1211119166Smtm#	Returns non-zero if it could not set it successfully.
1212119166Smtm#
1213119166Smtmdevfs_set_ruleset()
1214119166Smtm{
1215119166Smtm	local devdir rs _me
1216119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1217119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1218119166Smtm	_me="devfs_set_ruleset"
1219119166Smtm
1220119166Smtm	if [ -z "$rs" ]; then
1221119166Smtm		warn "$_me: you must specify a ruleset number"
1222119166Smtm		return 1
1223119166Smtm	fi
1224119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1225146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1226119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1227119166Smtm		return 1
1228119166Smtm	fi
1229119166Smtm	return 0
1230119166Smtm}
1231119166Smtm
1232119166Smtm# devfs_apply_ruleset ruleset [dir]
1233119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1234119166Smtm#	The ruleset argument must be a ruleset name as specified
1235119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1236119166Smtm#	if it could not apply the ruleset.
1237119166Smtm#
1238119166Smtmdevfs_apply_ruleset()
1239119166Smtm{
1240119166Smtm	local devdir rs _me
1241119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1242119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1243119166Smtm	_me="devfs_apply_ruleset"
1244119166Smtm
1245119166Smtm	if [ -z "$rs" ]; then
1246119166Smtm		warn "$_me: you must specify a ruleset"
1247119166Smtm		return 1
1248119166Smtm	fi
1249119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1250146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1251119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1252119166Smtm		return 1
1253119166Smtm	fi
1254119166Smtm	return 0
1255119166Smtm}
1256119166Smtm
1257119166Smtm# devfs_domount dir [ruleset]
1258119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1259119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1260119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1261119166Smtm#
1262119166Smtmdevfs_domount()
1263119166Smtm{
1264119166Smtm	local devdir rs _me
1265119166Smtm	devdir="$1"
1266119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1267119166Smtm	_me="devfs_domount()"
1268119166Smtm
1269119166Smtm	if [ -z "$devdir" ]; then
1270119166Smtm		warn "$_me: you must specify a mount-point"
1271119166Smtm		return 1
1272119166Smtm	fi
1273119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1274146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1275119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1276119166Smtm		return 1
1277119166Smtm	fi
1278119166Smtm	if [ -n "$rs" ]; then
1279119166Smtm		devfs_init_rulesets
1280119166Smtm		devfs_set_ruleset $rs $devdir
1281124797Scperciva		devfs -m $devdir rule applyset
1282119166Smtm	fi
1283119166Smtm	return 0
1284119166Smtm}
1285119166Smtm
1286119166Smtm# devfs_mount_jail dir [ruleset]
1287119166Smtm#	Mounts a devfs file system appropriate for jails
1288119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1289119166Smtm#	it names will be used instead.  If present, ruleset must
1290119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1291119166Smtm#	This function returns non-zero if an error occurs.
1292119166Smtm#
1293119166Smtmdevfs_mount_jail()
1294119166Smtm{
1295119166Smtm	local jdev rs _me
1296119166Smtm	jdev="$1"
1297119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1298119166Smtm	_me="devfs_mount_jail"
1299119166Smtm
1300119166Smtm	devfs_init_rulesets
1301146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1302119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1303119166Smtm		return 1
1304119166Smtm	fi
1305119166Smtm	return 0
1306119166Smtm}
1307127345Sbrooks
1308127345Sbrooks# Provide a function for normalizing the mounting of memory
1309127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1310127345Sbrooks# as close as possible between 5-current and 4-stable.
1311127345Sbrooks#   $1 = size
1312127345Sbrooks#   $2 = mount point
1313137451Skeramida#   $3 = (optional) extra mdmfs flags
1314146490Sschweikhmount_md()
1315146490Sschweikh{
1316127345Sbrooks	if [ -n "$3" ]; then
1317137451Skeramida		flags="$3"
1318127345Sbrooks	fi
1319149421Syar	/sbin/mdmfs $flags -s $1 md $2
1320127345Sbrooks}
1321131550Scperciva
1322159828Syar# Code common to scripts that need to load a kernel module
1323159828Syar# if it isn't in the kernel yet. Syntax:
1324160666Syar#   load_kld [-e regex] [-m module] file
1325159828Syar# where -e or -m chooses the way to check if the module
1326159828Syar# is already loaded:
1327160666Syar#   regex is egrep'd in the output from `kldstat -v',
1328160666Syar#   module is passed to `kldstat -m'.
1329160666Syar# The default way is as though `-m file' were specified.
1330159828Syarload_kld()
1331159828Syar{
1332159828Syar	local _loaded _mod _opt _re
1333159828Syar
1334159828Syar	while getopts "e:m:" _opt; do
1335159828Syar		case "$_opt" in
1336159828Syar		e) _re="$OPTARG" ;;
1337159828Syar		m) _mod="$OPTARG" ;;
1338160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1339159828Syar		esac
1340159828Syar	done
1341159828Syar	shift $(($OPTIND - 1))
1342160666Syar	if [ $# -ne 1 ]; then
1343160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1344160666Syar	fi
1345159828Syar	_mod=${_mod:-$1}
1346159828Syar	_loaded=false
1347159828Syar	if [ -n "$_re" ]; then
1348159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1349159828Syar			_loaded=true
1350159828Syar		fi
1351159828Syar	else
1352159828Syar		if kldstat -q -m "$_mod"; then
1353159828Syar			_loaded=true
1354159828Syar		fi
1355159828Syar	fi
1356159828Syar	if ! $_loaded; then
1357159828Syar		if ! kldload "$1"; then
1358159828Syar			warn "Unable to load kernel module $1"
1359159828Syar			return 1
1360160666Syar		else
1361160666Syar			info "$1 kernel module loaded."
1362159828Syar		fi
1363160666Syar	else
1364160666Syar		debug "load_kld: $1 kernel module already loaded."
1365159828Syar	fi
1366159828Syar	return 0
1367159828Syar}
1368159828Syar
1369149049Spjd# ltr str src dst
1370149049Spjd#	Change every $src in $str to $dst.
1371149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1372149049Spjd#	awk(1).
1373149049Spjdltr()
1374149049Spjd{
1375149049Spjd	local _str _src _dst _out _com
1376149049Spjd	_str=$1
1377149049Spjd	_src=$2
1378149049Spjd	_dst=$3
1379149049Spjd	_out=""
1380149049Spjd
1381149049Spjd	IFS=${_src}
1382149049Spjd	for _com in ${_str}; do
1383149049Spjd		if [ -z "${_out}" ]; then
1384149049Spjd			_out="${_com}"
1385149049Spjd		else
1386149049Spjd			_out="${_out}${_dst}${_com}"
1387149049Spjd		fi
1388149049Spjd	done
1389149049Spjd	echo "${_out}"
1390149049Spjd}
1391149049Spjd
1392149050Spjd# Creates a list of providers for GELI encryption.
1393149050Spjdgeli_make_list()
1394149050Spjd{
1395149050Spjd	local devices devices2
1396149050Spjd	local provider mountpoint type options rest
1397149050Spjd
1398149050Spjd	# Create list of GELI providers from fstab.
1399149050Spjd	while read provider mountpoint type options rest ; do
1400155570Sflz		case ":${options}" in
1401155570Sflz		:*noauto*)
1402155570Sflz			noauto=yes
1403155570Sflz			;;
1404155570Sflz		*)
1405155570Sflz			noauto=no
1406155570Sflz			;;
1407155570Sflz		esac
1408155570Sflz
1409149050Spjd		case ":${provider}" in
1410149050Spjd		:#*)
1411149050Spjd			continue
1412149050Spjd			;;
1413149050Spjd		*.eli)
1414149050Spjd			# Skip swap devices.
1415155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1416149050Spjd				continue
1417149050Spjd			fi
1418149050Spjd			devices="${devices} ${provider}"
1419149050Spjd			;;
1420149050Spjd		esac
1421149050Spjd	done < /etc/fstab
1422149050Spjd
1423149050Spjd	# Append providers from geli_devices.
1424149050Spjd	devices="${devices} ${geli_devices}"
1425149050Spjd
1426149050Spjd	for provider in ${devices}; do
1427149050Spjd		provider=${provider%.eli}
1428149050Spjd		provider=${provider#/dev/}
1429149050Spjd		devices2="${devices2} ${provider}"
1430149050Spjd	done
1431149050Spjd
1432149050Spjd	echo ${devices2}
1433149050Spjd}
1434149050Spjd
1435153027Sdougb# Find scripts in local_startup directories that use the old syntax
1436153027Sdougb#
1437153027Sdougbfind_local_scripts_old () {
1438153027Sdougb	zlist=''
1439153027Sdougb	slist=''
1440153027Sdougb	for dir in ${local_startup}; do
1441153027Sdougb		if [ -d "${dir}" ]; then
1442153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1443153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1444153027Sdougb				    continue
1445153027Sdougb				zlist="$zlist $file"
1446153027Sdougb			done
1447153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1448153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1449153027Sdougb				    continue
1450153027Sdougb				slist="$slist $file"
1451153027Sdougb			done
1452153027Sdougb		fi
1453153027Sdougb	done
1454153027Sdougb}
1455153027Sdougb
1456153027Sdougbfind_local_scripts_new () {
1457153027Sdougb	local_rc=''
1458153027Sdougb	for dir in ${local_startup}; do
1459153027Sdougb		if [ -d "${dir}" ]; then
1460153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1461153027Sdougb				case "$file" in
1462153027Sdougb				*.sample) ;;
1463153027Sdougb				*)	if [ -x "$file" ]; then
1464153027Sdougb						local_rc="${local_rc} ${file}"
1465153027Sdougb					fi
1466153027Sdougb					;;
1467153027Sdougb				esac
1468153027Sdougb			done
1469153027Sdougb		fi
1470153027Sdougb	done
1471153027Sdougb}
1472153027Sdougb
1473165565Syar# check_required_{before|after} command
1474165565Syar#	Check for things required by the command before and after its precmd,
1475165565Syar#	respectively.  The two separate functions are needed because some
1476165565Syar#	conditions should prevent precmd from being run while other things
1477165565Syar#	depend on precmd having already been run.
1478165565Syar#
1479165565Syarcheck_required_before()
1480165565Syar{
1481165565Syar	local _f
1482165565Syar
1483165565Syar	case "$1" in
1484165565Syar	start)
1485165565Syar		for _f in $required_vars; do
1486165565Syar			if ! checkyesno $_f; then
1487165565Syar				warn "\$${_f} is not enabled."
1488165565Syar				if [ -z "$rc_force" ]; then
1489165565Syar					return 1
1490165565Syar				fi
1491165565Syar			fi
1492165565Syar		done
1493165565Syar
1494165565Syar		for _f in $required_dirs; do
1495165565Syar			if [ ! -d "${_f}/." ]; then
1496165565Syar				warn "${_f} is not a directory."
1497165565Syar				if [ -z "$rc_force" ]; then
1498165565Syar					return 1
1499165565Syar				fi
1500165565Syar			fi
1501165565Syar		done
1502165565Syar
1503165565Syar		for _f in $required_files; do
1504165565Syar			if [ ! -r "${_f}" ]; then
1505165565Syar				warn "${_f} is not readable."
1506165565Syar				if [ -z "$rc_force" ]; then
1507165565Syar					return 1
1508165565Syar				fi
1509165565Syar			fi
1510165565Syar		done
1511165565Syar		;;
1512165565Syar	esac
1513165565Syar
1514165565Syar	return 0
1515165565Syar}
1516165565Syar
1517165565Syarcheck_required_after()
1518165565Syar{
1519165565Syar	local _f _args
1520165565Syar
1521165565Syar	case "$1" in
1522165565Syar	start)
1523165565Syar		for _f in $required_modules; do
1524165565Syar			case "${_f}" in
1525165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1526165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1527165565Syar				*)	_args="${_f}" ;;
1528165565Syar			esac
1529165565Syar			if ! load_kld ${_args}; then
1530165565Syar				if [ -z "$rc_force" ]; then
1531165565Syar					return 1
1532165565Syar				fi
1533165565Syar			fi
1534165565Syar		done
1535165565Syar		;;
1536165565Syar	esac
1537165565Syar
1538165565Syar	return 0
1539165565Syar}
1540165565Syar
1541131550Scpercivafi
1542157841Sflz
1543157841Sflz_rc_subr_loaded=:
1544