rc.subr revision 161396
198186Sgordon# $NetBSD: rc.subr,v 1.66 2006/04/01 10:05:50 he Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 161396 2006-08-17 08:04:20Z yar $
378344Sobrien#
498186Sgordon# 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
4278344Sobrien: ${rcvar_manpage:='rc.conf(5)'}
4398186Sgordon
4498186Sgordon#
4598186Sgordon#	Operating System dependent/independent variables
4698186Sgordon#
4798186Sgordon
4898186Sgordonif [ -z "${_rc_subr_loaded}" ]; then
49103018Sgordon
5098186Sgordon_rc_subr_loaded="YES"
51103018Sgordon
5298186SgordonSYSCTL="/sbin/sysctl"
5398186SgordonSYSCTL_N="${SYSCTL} -n"
5498186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype"
5598186SgordonOSTYPE=`${CMD_OSTYPE}`
5698186SgordonID="/usr/bin/id"
5798186SgordonJID=`ps -p $$ -o jid=`
5898186SgordonIDCMD="if [ -x $ID ]; then $ID -un; fi"
5998186Sgordon
6098186Sgordoncase ${OSTYPE} in
6178344SobrienFreeBSD)
6278344Sobrien	SYSCTL_W="${SYSCTL}"
6378344Sobrien	;;
6478344SobrienNetBSD)
6598186Sgordon	SYSCTL_W="${SYSCTL} -w"
6698186Sgordon	;;
6798186Sgordonesac
6898186Sgordon
6998186Sgordon#
7098186Sgordon#	functions
7198186Sgordon#	---------
7298186Sgordon
7398186Sgordon#
7498186Sgordon# set_rcvar base_var
7598186Sgordon#	Set the variable name enabling a specific service.
7698186Sgordon#	FreeBSD uses ${service}_enable, while NetBSD uses
7798186Sgordon#	just the name of the service. For example:
7898186Sgordon#	FreeBSD: sendmail_enable="YES"
7998186Sgordon#	NetBSD : sendmail="YES"
8098186Sgordon#	$1 - if $name is not the base to work of off, specify
8198186Sgordon#	     a different one
82103018Sgordon#
8398186Sgordonset_rcvar()
8498186Sgordon{
8598186Sgordon	if [ -z "$1" ]; then
8698186Sgordon		base_var=${name}
8798186Sgordon	else
8898186Sgordon		base_var="$1"
8998186Sgordon	fi
9098186Sgordon
9198186Sgordon	case ${OSTYPE} in
9298186Sgordon	FreeBSD)
9398186Sgordon		echo ${base_var}_enable
9498186Sgordon		;;
9598186Sgordon	NetBSD)
9698186Sgordon		echo ${base_var}
9798186Sgordon		;;
9898186Sgordon	*)
9998186Sgordon		echo 'XXX'
10098186Sgordon		;;
10198186Sgordon	esac
10298186Sgordon}
10398186Sgordon
10498186Sgordon#
10598186Sgordon# force_depend script
10698186Sgordon#	Force a service to start. Intended for use by services
10798186Sgordon#	to resolve dependency issues. It is assumed the caller
10898186Sgordon#	has check to make sure this call is necessary
10998186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
11098186Sgordon#
11198186Sgordonforce_depend()
11298186Sgordon{
11398186Sgordon	_depend="$1"
11498186Sgordon
11578344Sobrien	info "${name} depends on ${_depend}, which will be forced to start."
11678344Sobrien	if ! /etc/rc.d/${_depend} forcestart; then
11778344Sobrien		warn "Unable to force ${_depend}. It may already be running."
11878344Sobrien		return 1
11978344Sobrien	fi
12078344Sobrien	return 0
12178344Sobrien}
12298186Sgordon
12378344Sobrien#
12478344Sobrien# checkyesno var
12578344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
12678344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
12778344Sobrien#
12878344Sobriencheckyesno()
12978344Sobrien{
13078344Sobrien	eval _value=\$${1}
13178344Sobrien	debug "checkyesno: $1 is set to $_value."
13278344Sobrien	case $_value in
13378344Sobrien
13478344Sobrien		#	"yes", "true", "on", or "1"
135106643Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
13678344Sobrien		return 0
13778344Sobrien		;;
13878344Sobrien
13978344Sobrien		#	"no", "false", "off", or "0"
14078344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
14198186Sgordon		return 1
14298186Sgordon		;;
14378344Sobrien	*)
14498186Sgordon		warn "\$${1} is not set properly - see ${rcvar_manpage}."
14598186Sgordon		return 1
14698186Sgordon		;;
14798186Sgordon	esac
14898186Sgordon}
14998186Sgordon
15098186Sgordon#
15198186Sgordon# reverse_list list
15298186Sgordon#	print the list in reverse order
15378344Sobrien#
15498186Sgordonreverse_list()
15598186Sgordon{
15698186Sgordon	_revlist=
15798186Sgordon	for _revfile; do
15898186Sgordon		_revlist="$_revfile $_revlist"
15978344Sobrien	done
16078344Sobrien	echo $_revlist
16198186Sgordon}
16278344Sobrien
16378344Sobrien#
16478344Sobrien# mount_critical_filesystems type
16578344Sobrien#	Go through the list of critical filesystems as provided in
16678344Sobrien#	the rc.conf(5) variable $critical_filesystems_${type}, checking
16778344Sobrien#	each one to see if it is mounted, and if it is not, mounting it.
16878344Sobrien#
16978344Sobrienmount_critical_filesystems()
17098186Sgordon{
17178344Sobrien	eval _fslist=\$critical_filesystems_${1}
17278344Sobrien	for _fs in $_fslist; do
17398186Sgordon		mount | (
17478344Sobrien			_ismounted=false
17578344Sobrien			while read what _on on _type type; do
17678344Sobrien				if [ $on = $_fs ]; then
17778344Sobrien					_ismounted=true
17898186Sgordon				fi
17998186Sgordon			done
18078344Sobrien			if $_ismounted; then
18198186Sgordon				:
18298186Sgordon			else
18378344Sobrien				mount $_fs >/dev/null 2>&1
18478344Sobrien			fi
18578344Sobrien		)
18678344Sobrien	done
18778344Sobrien}
18898186Sgordon
18978344Sobrien#
19098186Sgordon# check_pidfile pidfile procname [interpreter]
19178344Sobrien#	Parses the first line of pidfile for a PID, and ensures
19278344Sobrien#	that the process is running and matches procname.
19398186Sgordon#	Prints the matching PID upon success, nothing otherwise.
19478344Sobrien#	interpreter is optional; see _find_processes() for details.
19578344Sobrien#
19678344Sobriencheck_pidfile()
19778344Sobrien{
19898186Sgordon	_pidfile=$1
19978344Sobrien	_procname=$2
20078344Sobrien	_interpreter=$3
20198186Sgordon	if [ -z "$_pidfile" -o -z "$_procname" ]; then
20278344Sobrien		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
20378344Sobrien	fi
20478344Sobrien	if [ ! -f $_pidfile ]; then
20598186Sgordon		debug "pid file ($_pidfile): not readable."
20678344Sobrien		return
20798186Sgordon	fi
20898186Sgordon	read _pid _junk < $_pidfile
20978344Sobrien	if [ -z "$_pid" ]; then
21078344Sobrien		debug "pid file ($_pidfile): no pid in file."
21178344Sobrien		return
21278344Sobrien	fi
21398186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
21478344Sobrien}
21598186Sgordon
21678344Sobrien#
21798186Sgordon# check_process procname [interpreter]
21898186Sgordon#	Ensures that a process (or processes) named procname is running.
21998186Sgordon#	Prints a list of matching PIDs.
22098186Sgordon#	interpreter is optional; see _find_processes() for details.
22198186Sgordon#
22298186Sgordoncheck_process()
22398186Sgordon{
22498186Sgordon	_procname=$1
22598186Sgordon	_interpreter=$2
22698186Sgordon	if [ -z "$_procname" ]; then
22798186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
22898186Sgordon	fi
22998186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
23098186Sgordon}
23198186Sgordon
23298186Sgordon#
23398186Sgordon# _find_processes procname interpreter psargs
23498186Sgordon#	Search for procname in the output of ps generated by psargs.
23598186Sgordon#	Prints the PIDs of any matching processes, space separated.
23698186Sgordon#
23798186Sgordon#	If interpreter == ".", check the following variations of procname
23898186Sgordon#	against the first word of each command:
23998186Sgordon#		procname
24098186Sgordon#		`basename procname`
24198186Sgordon#		`basename procname` + ":"
24298186Sgordon#		"(" + `basename procname` + ")"
24398186Sgordon#		"[" + `basename procname` + "]"
24498186Sgordon#
24598186Sgordon#	If interpreter != ".", read the first line of procname, remove the
24678344Sobrien#	leading #!, normalise whitespace, append procname, and attempt to
24798186Sgordon#	match that against each command, either as is, or with extra words
24898186Sgordon#	at the end.  As an alternative, to deal with interpreted daemons
24998186Sgordon#	using perl, the basename of the interpreter plus a colon is also
25098186Sgordon#	tried as the prefix to procname.
25198186Sgordon#
25298186Sgordon_find_processes()
25378344Sobrien{
25498186Sgordon	if [ $# -ne 3 ]; then
25598186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
25698186Sgordon	fi
25798186Sgordon	_procname=$1
25898186Sgordon	_interpreter=$2
25998186Sgordon	_psargs=$3
26098186Sgordon
26198186Sgordon	_pref=
26298186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
26398186Sgordon		read _interp < $_procname	# read interpreter name
26498186Sgordon		_interp=${_interp#\#!}		# strip #!
26598186Sgordon		set -- $_interp
26698186Sgordon		if [ $_interpreter != $1 ]; then
26798186Sgordon			warn "\$command_interpreter $_interpreter != $1"
26898186Sgordon		fi
26998186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
27098186Sgordon		_interpbn=${1##*/}
27198186Sgordon		_fp_args='_argv'
27298186Sgordon		_fp_match='case "$_argv" in
27398186Sgordon		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
27498186Sgordon	else					# a normal daemon
27598186Sgordon		_procnamebn=${_procname##*/}
27698186Sgordon		_fp_args='_arg0 _argv'
27798186Sgordon		_fp_match='case "$_arg0" in
278114272Smtm		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
27998186Sgordon	fi
28098186Sgordon
28198186Sgordon	_proccheck='
28298186Sgordon		ps 2>/dev/null -o "pid,jid,command" '"$_psargs"' |
28398186Sgordon		while read _npid _jid '"$_fp_args"'; do
28498186Sgordon			case "$_npid" in
28598186Sgordon			PID)
28698186Sgordon				continue;;
28798186Sgordon			esac; '"$_fp_match"'
28898186Sgordon				if [ "$JID" -eq "$_jid" ];
28998186Sgordon				then echo -n "$_pref$_npid";
29098186Sgordon				_pref=" ";
29198186Sgordon				fi
29298186Sgordon				;;
29398186Sgordon			esac
29498186Sgordon		done'
29598186Sgordon
29698186Sgordon#	debug "in _find_processes: proccheck is ($_proccheck)."
29798186Sgordon	eval $_proccheck
29898186Sgordon}
29998186Sgordon
30098186Sgordon#
30198186Sgordon# wait_for_pids pid [pid ...]
30278344Sobrien#	spins until none of the pids exist
30398186Sgordon#
30498186Sgordonwait_for_pids()
30598186Sgordon{
30698186Sgordon	_list="$@"
30778344Sobrien	if [ -z "$_list" ]; then
30898186Sgordon		return
30998186Sgordon	fi
31098186Sgordon	_prefix=
31178344Sobrien	while true; do
31278344Sobrien		_nlist="";
31378344Sobrien		for _j in $_list; do
31498186Sgordon			if kill -0 $_j 2>/dev/null; then
31598186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
31698186Sgordon			fi
31798186Sgordon		done
31898186Sgordon		if [ -z "$_nlist" ]; then
31978344Sobrien			break
32098186Sgordon		fi
32198186Sgordon		_list=$_nlist
32278344Sobrien		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
32398186Sgordon		_prefix=", "
32498186Sgordon		sleep 2
32578344Sobrien	done
32678344Sobrien	if [ -n "$_prefix" ]; then
32778344Sobrien		echo "."
32898186Sgordon	fi
32998186Sgordon}
33078344Sobrien
33178344Sobrien#
33278344Sobrien# run_rc_command argument
33398186Sgordon#	Search for argument in the list of supported commands, which is:
33478344Sobrien#		"start stop restart rcvar status poll ${extra_commands}"
33578344Sobrien#	If there's a match, run ${argument}_cmd or the default method
33678344Sobrien#	(see below).
33778344Sobrien#
33898186Sgordon#	If argument has a given prefix, then change the operation as follows:
33998186Sgordon#		Prefix	Operation
34098186Sgordon#		------	---------
34178344Sobrien#		fast	Skip the pid check, and set rc_fast=yes
34278344Sobrien#		force	Set ${rcvar} to YES, and set rc_force=yes
34398186Sgordon#		one	Set ${rcvar} to YES
34498186Sgordon#
34598186Sgordon#	The following globals are used:
34678344Sobrien#
34798186Sgordon#	Name		Needed	Purpose
34898186Sgordon#	----		------	-------
34978344Sobrien#	name		y	Name of script.
35078344Sobrien#
35178344Sobrien#	command		n	Full path to command.
35278344Sobrien#				Not needed if ${rc_arg}_cmd is set for
35398186Sgordon#				each keyword.
35478344Sobrien#
35578344Sobrien#	command_args	n	Optional args/shell directives for command.
35678344Sobrien#
35778344Sobrien#	command_interpreter n	If not empty, command is interpreted, so
35878344Sobrien#				call check_{pidfile,process}() appropriately.
35978344Sobrien#
36078344Sobrien#	extra_commands	n	List of extra commands supported.
36178344Sobrien#
36278344Sobrien#	pidfile		n	If set, use check_pidfile $pidfile $command,
36378344Sobrien#				otherwise use check_process $command.
36478344Sobrien#				In either case, only check if $command is set.
36578344Sobrien#
36698186Sgordon#	procname	n	Process name to check for instead of $command.
36778344Sobrien#
36878344Sobrien#	rcvar		n	This is checked with checkyesno to determine
36998186Sgordon#				if the action should be run.
37078344Sobrien#
37198186Sgordon#	${name}_program	n	Full path to command.
37298186Sgordon#				Meant to be used in /etc/rc.conf to override
37398186Sgordon#				${command}.
37478344Sobrien#
37598186Sgordon#	${name}_chroot	n	Directory to chroot to before running ${command}
37678344Sobrien#				Requires /usr to be mounted.
37778344Sobrien#
37898186Sgordon#	${name}_chdir	n	Directory to cd to before running ${command}
37998186Sgordon#				(if not using ${name}_chroot).
38098186Sgordon#
38198186Sgordon#	${name}_flags	n	Arguments to call ${command} with.
38278344Sobrien#				NOTE:	$flags from the parent environment
38398186Sgordon#					can be used to override this.
38478344Sobrien#
38598186Sgordon#	${name}_nice	n	Nice level to run ${command} at.
38698186Sgordon#
38798186Sgordon#	${name}_user	n	User to run ${command} as, using su(1) if not
38898186Sgordon#				using ${name}_chroot.
38978344Sobrien#				Requires /usr to be mounted.
39078344Sobrien#
39178344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
39278344Sobrien#				Requires /usr to be mounted.
39378344Sobrien#
39478344Sobrien#	${name}_groups	n	Comma separated list of supplementary groups
39578344Sobrien#				to run the chrooted ${command} with.
39678344Sobrien#				Requires /usr to be mounted.
39778344Sobrien#
39878344Sobrien#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
39978344Sobrien#				Otherwise, use default command (see below)
40078344Sobrien#
40198186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
40298186Sgordon#				${rc_arg}_cmd method in the default
40378344Sobrien#				operation (i.e, after checking for required
40498186Sgordon#				bits and process (non)existence).
40598186Sgordon#				If this completes with a non-zero exit code,
40678344Sobrien#				don't run ${rc_arg}_cmd.
40778344Sobrien#
40878344Sobrien#	${rc_arg}_postcmd n	If set, run just after performing the
40978344Sobrien#				${rc_arg}_cmd method, if that method
41098186Sgordon#				returned a zero exit code.
41178344Sobrien#
41298186Sgordon#	required_dirs	n	If set, check for the existence of the given
41398186Sgordon#				directories before running the default
41498186Sgordon#				(re)start command.
41598186Sgordon#
41678344Sobrien#	required_files	n	If set, check for the readability of the given
41798186Sgordon#				files before running the default (re)start
41898186Sgordon#				command.
41978344Sobrien#
42078344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
42178344Sobrien#				listed variables before running the default
42278344Sobrien#				(re)start command.
42398186Sgordon#
42478344Sobrien#	Default behaviour for a given argument, if no override method is
42598186Sgordon#	provided:
42698186Sgordon#
42798186Sgordon#	Argument	Default behaviour
42898186Sgordon#	--------	-----------------
42998186Sgordon#	start		if !running && checkyesno ${rcvar}
43098186Sgordon#				${command}
43198186Sgordon#
43298186Sgordon#	stop		if ${pidfile}
43398186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
43498186Sgordon#			else
43598186Sgordon#				rc_pid=$(check_process $command)
43698186Sgordon#			kill $sig_stop $rc_pid
43798186Sgordon#			wait_for_pids $rc_pid
43898186Sgordon#			($sig_stop defaults to TERM.)
43998186Sgordon#
44098186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
44198186Sgordon#			and doesn't wait_for_pids.
44298186Sgordon#			$sig_reload defaults to HUP.
44398186Sgordon#			Note that `reload' isn't provided by default,
44498186Sgordon#			it should be enabled via $extra_commands.
44598186Sgordon#
44698186Sgordon#	restart		Run `stop' then `start'.
44798186Sgordon#
44898186Sgordon#	status		Show if ${command} is running, etc.
44978344Sobrien#
45078344Sobrien#	poll		Wait for ${command} to exit.
45198186Sgordon#
45278344Sobrien#	rcvar		Display what rc.conf variable is used (if any).
45398186Sgordon#
45478344Sobrien#	Variables available to methods, and after run_rc_command() has
45578344Sobrien#	completed:
45698186Sgordon#
45778344Sobrien#	Variable	Purpose
45898186Sgordon#	--------	-------
45998186Sgordon#	rc_arg		Argument to command, after fast/force/one processing
46078344Sobrien#			performed
46178344Sobrien#
46298186Sgordon#	rc_flags	Flags to start the default command with.
46398186Sgordon#			Defaults to ${name}_flags, unless overridden
46478344Sobrien#			by $flags from the environment.
46578344Sobrien#			This variable may be changed by the precmd method.
46678344Sobrien#
46778344Sobrien#	rc_pid		PID of command (if appropriate)
46878344Sobrien#
46978344Sobrien#	rc_fast		Not empty if "fast" was provided (q.v.)
47098186Sgordon#
47198186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
47298186Sgordon#
47398186Sgordon#
47498186Sgordonrun_rc_command()
47578344Sobrien{
47698186Sgordon	_return=0
47778344Sobrien	rc_arg=$1
47898186Sgordon	if [ -z "$name" ]; then
47998186Sgordon		err 3 'run_rc_command: $name is not set.'
48078344Sobrien	fi
48198186Sgordon
48278344Sobrien	# Don't repeat the first argument when passing additional command-
48398186Sgordon	# line arguments to the command subroutines.
48498186Sgordon	#
48598186Sgordon	shift 1
48678344Sobrien	rc_extra_args="$*"
48778344Sobrien
48898186Sgordon	_rc_prefix=
48978344Sobrien	case "$rc_arg" in
49078344Sobrien	fast*)				# "fast" prefix; don't check pid
49178344Sobrien		rc_arg=${rc_arg#fast}
49298186Sgordon		rc_fast=yes
49378344Sobrien		;;
49478344Sobrien	force*)				# "force prefix; always run
49578344Sobrien		rc_force=yes
49678344Sobrien		_rc_prefix=force
49798186Sgordon		rc_arg=${rc_arg#${_rc_prefix}}
49878344Sobrien		if [ -n "${rcvar}" ]; then
49998186Sgordon			eval ${rcvar}=YES
50078344Sobrien		fi
50198186Sgordon		;;
50298186Sgordon	one*)				# "one" prefix; set ${rcvar}=yes
50398186Sgordon		_rc_prefix=one
50478344Sobrien		rc_arg=${rc_arg#${_rc_prefix}}
50598186Sgordon		if [ -n "${rcvar}" ]; then
50698186Sgordon			eval ${rcvar}=YES
50798186Sgordon		fi
50898186Sgordon		;;
50998186Sgordon	esac
51098186Sgordon
51178344Sobrien	_keywords="start stop restart rcvar $extra_commands"
51298186Sgordon	rc_pid=
51378344Sobrien	_pidcmd=
51478344Sobrien	_procname=${procname:-${command}}
51578344Sobrien
51698186Sgordon					# setup pid check command
51778344Sobrien	if [ -n "$_procname" ]; then
51878344Sobrien		if [ -n "$pidfile" ]; then
51978344Sobrien			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
52078344Sobrien		else
52178344Sobrien			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
52278344Sobrien		fi
52378344Sobrien		if [ -n "$_pidcmd" ]; then
52478344Sobrien			_keywords="${_keywords} status poll"
52598186Sgordon		fi
52678344Sobrien	fi
52778344Sobrien
52878344Sobrien	if [ -z "$rc_arg" ]; then
52978344Sobrien		rc_usage $_keywords
53078344Sobrien	fi
53178344Sobrien
53298186Sgordon	if [ -n "$flags" ]; then	# allow override from environment
53398186Sgordon		rc_flags=$flags
53478344Sobrien	else
53578344Sobrien		eval rc_flags=\$${name}_flags
53678344Sobrien	fi
53778344Sobrien	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
538109582Smtm	    _nice=\$${name}_nice	_user=\$${name}_user \
539109582Smtm	    _group=\$${name}_group	_groups=\$${name}_groups
54098186Sgordon
54178344Sobrien	if [ -n "$_user" ]; then	# unset $_user if running as that user
54278344Sobrien		if [ "$_user" = "$(eval $IDCMD)" ]; then
54378344Sobrien			unset _user
544109582Smtm		fi
545109582Smtm	fi
54698186Sgordon
54798186Sgordon					# if ${rcvar} is set, and $1 is not
54898186Sgordon					# "rcvar", then run
549109582Smtm					#	checkyesno ${rcvar}
550109582Smtm					# and return if that failed
551109582Smtm					#
55298186Sgordon	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
55378344Sobrien		if ! checkyesno ${rcvar}; then
55478344Sobrien			return 0
55578344Sobrien		fi
55698186Sgordon	fi
55778344Sobrien
55878344Sobrien	eval $_pidcmd			# determine the pid if necessary
55998186Sgordon
56098186Sgordon	for _elem in $_keywords; do
56178344Sobrien		if [ "$_elem" != "$rc_arg" ]; then
56278344Sobrien			continue
56378344Sobrien		fi
56478344Sobrien
56578344Sobrien					# if there's a custom ${XXX_cmd},
56678344Sobrien					# run that instead of the default
56778344Sobrien					#
56898186Sgordon		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
56998186Sgordon		    _postcmd=\$${rc_arg}_postcmd
57078344Sobrien		if [ -n "$_cmd" ]; then
57178344Sobrien					# if the precmd failed and force
57278344Sobrien					# isn't set, exit
57378344Sobrien					#
57498186Sgordon			if [ -n "$_precmd" ]; then
57578344Sobrien				debug "run_rc_command: evaluating ${_precmd}()."
57678344Sobrien				eval $_precmd $rc_extra_args
57778344Sobrien				_return=$?
57878344Sobrien				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
57978344Sobrien				    return 1
58078344Sobrien			fi
58178344Sobrien
58278344Sobrien			if [ -n "$_cmd" ]; then
58378344Sobrien				debug "run_rc_command: evaluating ${_cmd}()."
58498186Sgordon				eval $_cmd $rc_extra_args
58578344Sobrien				_return=$?
58678344Sobrien				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
58778344Sobrien				    return 1
58878344Sobrien			fi
58978344Sobrien
59078344Sobrien			if [ -n "$_postcmd" ]; then
59178344Sobrien				debug "run_rc_command: evaluating ${_postcmd}()."
59298186Sgordon				 eval $_postcmd $rc_extra_args
59378344Sobrien				_return=$?
59478344Sobrien			fi
59578344Sobrien			return $_return
59678344Sobrien		fi
59778344Sobrien
59878344Sobrien		case "$rc_arg" in	# default operations...
59978344Sobrien
60098186Sgordon		status)
60178344Sobrien			if [ -n "$rc_pid" ]; then
60278344Sobrien				echo "${name} is running as pid $rc_pid."
60378344Sobrien			else
60478344Sobrien				echo "${name} is not running."
60578344Sobrien				return 1
60678344Sobrien			fi
60778344Sobrien			;;
60878344Sobrien
609109582Smtm		start)
610109582Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
61198186Sgordon				echo 1>&2 "${name} already running? (pid=$rc_pid)."
61278344Sobrien				return 1
61378344Sobrien			fi
61478344Sobrien
61578344Sobrien			if [ ! -x ${_chroot}${command} ]; then
61678344Sobrien				warn "run_rc_command: cannot run $command"
61778344Sobrien				return 1
61878344Sobrien			fi
61978344Sobrien
62078344Sobrien					# check for required variables,
62178344Sobrien					# directories, and files
62298186Sgordon					#
62378344Sobrien			for _f in $required_vars; do
62478344Sobrien				if ! checkyesno $_f; then
62578344Sobrien					warn "\$${_f} is not enabled."
62678344Sobrien					if [ -z "$rc_force" ]; then
62798186Sgordon						return 1
62898186Sgordon					fi
62998186Sgordon				fi
63098186Sgordon			done
63178344Sobrien			for _f in $required_dirs; do
63298186Sgordon				if [ ! -d "${_f}/." ]; then
63398186Sgordon					warn "${_f} is not a directory."
63498186Sgordon					if [ -z "$rc_force" ]; then
63598186Sgordon						return 1
63698186Sgordon					fi
63798186Sgordon				fi
63898186Sgordon			done
63998186Sgordon			for _f in $required_files; do
64098186Sgordon				if [ ! -r "${_f}" ]; then
64198186Sgordon					warn "${_f} is not readable."
64298186Sgordon					if [ -z "$rc_force" ]; then
643109582Smtm						return 1
644109582Smtm					fi
64598186Sgordon				fi
64678344Sobrien			done
64778344Sobrien
64878344Sobrien					# if the precmd failed and force
64998186Sgordon					# isn't set, exit
65078344Sobrien					#
65178344Sobrien			if [ -n "${_precmd}" ]; then
65278344Sobrien				debug "run_rc_command: evaluating ${_precmd}()."
65378344Sobrien				eval $_precmd
65478344Sobrien				_return=$?
65578344Sobrien				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
65678344Sobrien				    return 1
65778344Sobrien			fi
65878344Sobrien
65998186Sgordon					# setup the full command to run
66098186Sgordon					#
66198186Sgordon			echo "Starting ${name}."
66298186Sgordon			if [ -n "$_chroot" ]; then
66378344Sobrien				_doit="\
66478344Sobrien${_nice:+nice -n $_nice }\
66598186Sgordonchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
66698186Sgordon$_chroot $command $rc_flags $command_args"
66798186Sgordon			else
66878344Sobrien				_doit="\
66998186Sgordon${_chdir:+cd $_chdir && }\
67098186Sgordon$command $rc_flags $command_args"
67198186Sgordon				if [ -n "$_user" ]; then
67298186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
67398186Sgordon				fi
67498186Sgordon				if [ -n "$_nice" ]; then
67598186Sgordon					if [ -z "$_user" ]; then
67698186Sgordon						_doit="sh -c \"$_doit\""
67798186Sgordon					fi	
67898186Sgordon					_doit="nice -n $_nice $_doit"
67998186Sgordon				fi
68098186Sgordon			fi
68198186Sgordon
68298186Sgordon					# run the full command;
68398186Sgordon					# if the cmd failed and force
68498186Sgordon					# isn't set, exit
68578344Sobrien					#
68678344Sobrien			debug "run_rc_command: _doit: $_doit"
68778344Sobrien			eval $_doit
68898186Sgordon			_return=$?
68978344Sobrien			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
69078344Sobrien
69178344Sobrien					# finally, run postcmd
69278344Sobrien					#
69378344Sobrien			if [ -n "${_postcmd}" ]; then
69478344Sobrien				debug "run_rc_command: evaluating ${_postcmd}()."
69578344Sobrien				eval $_postcmd
69678344Sobrien			fi
69778344Sobrien			;;
69898186Sgordon
69978344Sobrien		stop)
70078344Sobrien			if [ -z "$rc_pid" ]; then
70198186Sgordon				[ -n "$rc_fast" ] && return 0
70298186Sgordon				if [ -n "$pidfile" ]; then
70398186Sgordon					echo 1>&2 \
70498186Sgordon				    "${name} not running? (check $pidfile)."
70598186Sgordon				else
70698186Sgordon					echo 1>&2 "${name} not running?"
70798186Sgordon				fi
70898186Sgordon				return 1
70978344Sobrien			fi
71078344Sobrien
71178344Sobrien					# if the precmd failed and force
71298186Sgordon					# isn't set, exit
71378344Sobrien					#
71478344Sobrien			if [ -n "$_precmd" ]; then
71578344Sobrien				eval $_precmd
71678344Sobrien				_return=$?
71778344Sobrien				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
71878344Sobrien				    return 1
71978344Sobrien			fi
72078344Sobrien
72178344Sobrien					# send the signal to stop
72278344Sobrien					#
72398186Sgordon			echo "Stopping ${name}."
72498186Sgordon			_doit="kill -${sig_stop:-TERM} $rc_pid"
72598186Sgordon			if [ -n "$_user" ]; then
72698186Sgordon				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
72778344Sobrien			fi
72878344Sobrien
72998186Sgordon					# if the stop cmd failed and force
73098186Sgordon					# isn't set, exit
73198186Sgordon					#
73298186Sgordon			eval $_doit
73398186Sgordon			_return=$?
73498186Sgordon			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
73578344Sobrien
73678344Sobrien					# wait for the command to exit,
73778344Sobrien					# and run postcmd.
73878344Sobrien			wait_for_pids $rc_pid
73978344Sobrien			if [ -n "$_postcmd" ]; then
74078344Sobrien				eval $_postcmd
74178344Sobrien				_return=$?
74278344Sobrien			fi
74378344Sobrien			;;
74478344Sobrien
74578344Sobrien		reload)
74678344Sobrien			if [ -z "$rc_pid" ]; then
74778344Sobrien				if [ -n "$pidfile" ]; then
74878344Sobrien					echo 1>&2 \
74978344Sobrien				    "${name} not running? (check $pidfile)."
75078344Sobrien				else
75178344Sobrien					echo 1>&2 "${name} not running?"
75278344Sobrien				fi
75378344Sobrien				return 1
75498186Sgordon			fi
75578344Sobrien			echo "Reloading ${name} config files."
75678344Sobrien			if [ -n "$_precmd" ]; then
75778344Sobrien				eval $_precmd
75878344Sobrien				_return=$?
75978344Sobrien				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
76078344Sobrien				    return 1
76178344Sobrien			fi
76278344Sobrien			_doit="kill -${sig_reload:-HUP} $rc_pid"
76398186Sgordon			if [ -n "$_user" ]; then
76498186Sgordon				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
76598186Sgordon			fi
76678344Sobrien			eval $_doit
76778344Sobrien			_return=$?
76878344Sobrien			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
76978344Sobrien			if [ -n "$_postcmd" ]; then
77078344Sobrien				eval $_postcmd
77178344Sobrien				_return=$?
77278344Sobrien			fi
77378344Sobrien			;;
77478344Sobrien
77598186Sgordon		restart)
77698186Sgordon			if [ -n "$_precmd" ]; then
77798186Sgordon				eval $_precmd $rc_extra_args
77898186Sgordon				_return=$?
77998186Sgordon				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
78098186Sgordon				    return 1
78198186Sgordon			fi
78278344Sobrien					# prevent restart being called more
78378344Sobrien					# than once by any given script
78478344Sobrien					#
78578344Sobrien			if ${_rc_restart_done:-false}; then
78698186Sgordon				return 0
78798186Sgordon			fi
78898186Sgordon			_rc_restart_done=true
78978344Sobrien
79098186Sgordon			# run stop in a subshell to keep variables for start
79198186Sgordon			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
79298186Sgordon			run_rc_command ${_rc_prefix}start $rc_extra_args
79398186Sgordon
79498186Sgordon			if [ -n "$_postcmd" ]; then
79598186Sgordon				eval $_postcmd $rc_extra_args
79698186Sgordon				_return=$?
79798186Sgordon			fi
79878344Sobrien			;;
79978344Sobrien
80078344Sobrien		poll)
80178344Sobrien			if [ -n "$rc_pid" ]; then
80278344Sobrien				wait_for_pids $rc_pid
80378344Sobrien			fi
80478344Sobrien			;;
80578344Sobrien
80678344Sobrien		rcvar)
80778344Sobrien			echo "# $name"
80878344Sobrien			if [ -n "$rcvar" ]; then
80978344Sobrien				if checkyesno ${rcvar}; then
81078344Sobrien					echo "\$${rcvar}=YES"
81178344Sobrien				else
81278344Sobrien					echo "\$${rcvar}=NO"
81398186Sgordon				fi
81498186Sgordon			fi
81598186Sgordon			;;
81698186Sgordon
81798186Sgordon		*)
81898186Sgordon			rc_usage $_keywords
81998186Sgordon			;;
82098186Sgordon
82198186Sgordon		esac
82298186Sgordon		return $_return
82398186Sgordon	done
82478344Sobrien
82598186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
82678344Sobrien	rc_usage $_keywords
82778344Sobrien	# not reached
828101850Sgordon}
829101850Sgordon
830101850Sgordon#
831103018Sgordon# run_rc_script file arg
832101850Sgordon#	Start the script `file' with `arg', and correctly handle the
833101850Sgordon#	return value from the script.  If `file' ends with `.sh', it's
834101850Sgordon#	sourced into the current environment.  If `file' appears to be
835101850Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
836101850Sgordon#	executable run as a child process.
837101850Sgordon#
838101850Sgordonrun_rc_script()
839101850Sgordon{
840101850Sgordon	_file=$1
841101850Sgordon	_arg=$2
842101850Sgordon	if [ -z "$_file" -o -z "$_arg" ]; then
84378344Sobrien		err 3 'USAGE: run_rc_script file arg'
84478344Sobrien	fi
84578344Sobrien
84678344Sobrien	unset	name command command_args command_interpreter \
84778344Sobrien		extra_commands pidfile procname \
84878344Sobrien		rcvar required_dirs required_files required_vars
84978344Sobrien	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
85078344Sobrien
85178344Sobrien	case "$_file" in
852106643Sgordon	/etc/rc.d/*.sh)			# run in current shell
85378344Sobrien		set $_arg; . $_file
85478344Sobrien		;;
85578344Sobrien	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
85678344Sobrien		warn "Ignoring scratch file $_file"
85778344Sobrien		;;
85878344Sobrien	*)				# run in subshell
85978344Sobrien		if [ -x $_file ]; then
86078344Sobrien			if [ -n "$rc_fast_and_loose" ]; then
86178344Sobrien				set $_arg; . $_file
86278344Sobrien			else
86378344Sobrien				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
86478344Sobrien				  trap "echo Script $_file interrupted; exit 1" 2
86578344Sobrien				  set $_arg; . $_file )
86678344Sobrien			fi
86778344Sobrien		fi
86878344Sobrien		;;
86978344Sobrien	esac
87078344Sobrien}
87178344Sobrien
872106643Sgordon#
873106643Sgordon# load_rc_config name
874106643Sgordon#	Source in the configuration file for a given name.
875106643Sgordon#
87678344Sobrienload_rc_config()
87778344Sobrien{
87878344Sobrien	local _tmp
87978344Sobrien
88078344Sobrien	_name=$1
88178344Sobrien	if [ -z "$_name" ]; then
88278344Sobrien		err 3 'USAGE: load_rc_config name'
88378344Sobrien	fi
88478344Sobrien
885106643Sgordon	if ${_rc_conf_loaded:-false}; then
886106643Sgordon		:
887106643Sgordon	else
888106643Sgordon		if [ -r /etc/defaults/rc.conf ]; then
88978344Sobrien			debug "Sourcing /etc/defaults/rc.conf"
89098186Sgordon			. /etc/defaults/rc.conf
89198186Sgordon			source_rc_confs
89298186Sgordon		elif [ -r /etc/rc.conf ]; then
89398186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
89498186Sgordon			. /etc/rc.conf
89598186Sgordon		fi
89698186Sgordon		_rc_conf_loaded=true
897106643Sgordon	fi
898106643Sgordon
899106643Sgordon	eval _override_command=\$${name}_program
900106643Sgordon	command=${command:+${_override_command:-$command}}
90198186Sgordon	
90298186Sgordon	if [ -z "${command}" ]; then
90398186Sgordon		_tmp=`/bin/realpath $0`
90498186Sgordon		prefix=${_tmp%/etc/rc.d/*}/
905106643Sgordon	else
90698186Sgordon		prefix=${command%/*bin/*}/
90798186Sgordon	fi
90898186Sgordon	if [ "${prefix}" = "/" -o "${prefix}" = "/usr/" ] ; then
90998186Sgordon		etcdir="/etc"
91098186Sgordon	else
91198186Sgordon		etcdir="${prefix}etc"
91298186Sgordon	fi
913106700Sgordon
914106700Sgordon	# XXX - Deprecated
915106700Sgordon	if [ -f /etc/rc.conf.d/${_name} -a ${etcdir} != "/etc" ]; then
916106643Sgordon		debug "Sourcing /etc/rc.conf.d/${_name}"
91798186Sgordon		warn "Warning: /etc/rc.conf.d/${_name} is deprecated, please use ${etcdir}/rc.conf.d/${_name} instead."
91898186Sgordon		if [ -f ${etcdir}/rc.conf.d/${_name} ]; then
91998186Sgordon			warn "Warning: Both /etc/rc.conf.d/${_name} and ${etcdir}/rc.conf.d/${_name} exist."
92098186Sgordon		fi
92198186Sgordon		. /etc/rc.conf.d/${_name}
92298186Sgordon	fi
92398186Sgordon
92498186Sgordon	if [ -f ${etcdir}/rc.conf.d/${_name} ]; then
92598186Sgordon		debug "Sourcing ${etcdir}/rc.conf.d/${_name}"
92698186Sgordon		. ${etcdir}/rc.conf.d/${_name}
92798186Sgordon	fi
92898186Sgordon
92998186Sgordon	# XXX - Deprecated variable name support
93098186Sgordon	#
93198186Sgordon	case ${OSTYPE} in
93298186Sgordon	FreeBSD)
93398186Sgordon		[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
93498186Sgordon		[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
93598186Sgordon		[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
93698186Sgordon		[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
93798186Sgordon		[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
93898186Sgordon		[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
93998186Sgordon		[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
94098186Sgordon		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
94198186Sgordon		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
94298186Sgordon		;;
94398186Sgordon	esac
94498186Sgordon}
94598186Sgordon  
94698186Sgordon#
94798186Sgordon# load_rc_config_var name var
94898186Sgordon#	Read the rc.conf(5) var for name and set in the
94998186Sgordon#	current shell, using load_rc_config in a subshell to prevent
95098186Sgordon#	unwanted side effects from other variable assignments.
95198186Sgordon#
95298186Sgordonload_rc_config_var()
95398186Sgordon{
95498186Sgordon	if [ $# -ne 2 ]; then
95598186Sgordon		err 3 'USAGE: load_rc_config_var name var'
95698186Sgordon	fi
95798186Sgordon	eval $(eval '(
95898186Sgordon		load_rc_config '$1' >/dev/null;
95998186Sgordon                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
96098186Sgordon			echo '$2'=\'\''${'$2'}\'\'';
96198186Sgordon		fi
96298186Sgordon	)' )
96398186Sgordon}
96498186Sgordon
96598186Sgordon#
96698186Sgordon# rc_usage commands
96798186Sgordon#	Print a usage string for $0, with `commands' being a list of
96898186Sgordon#	valid commands.
96998186Sgordon#
97098186Sgordonrc_usage()
97198186Sgordon{
97298186Sgordon	echo -n 1>&2 "Usage: $0 [fast|force|one]("
97398186Sgordon
97498186Sgordon	_sep=
97598186Sgordon	for _elem; do
97698186Sgordon		echo -n 1>&2 "$_sep$_elem"
97798186Sgordon		_sep="|"
97898186Sgordon	done
97998186Sgordon	echo 1>&2 ")"
98098186Sgordon	exit 1
98198186Sgordon}
98298186Sgordon
98398186Sgordon#
98498186Sgordon# err exitval message
98598186Sgordon#	Display message to stderr and log to the syslog, and exit with exitval.
98698186Sgordon#
98798186Sgordonerr()
98898186Sgordon{
98998186Sgordon	exitval=$1
99098186Sgordon	shift
99198186Sgordon
99298186Sgordon	if [ -x /usr/bin/logger ]; then
99398186Sgordon		logger "$0: ERROR: $*"
99498186Sgordon	fi
99598186Sgordon	echo 1>&2 "$0: ERROR: $*"
99698186Sgordon	exit $exitval
99798186Sgordon}
99898186Sgordon
99998186Sgordon#
100098186Sgordon# warn message
100198186Sgordon#	Display message to stderr and log to the syslog.
100298186Sgordon#
100398186Sgordonwarn()
100498186Sgordon{
100598186Sgordon	if [ -x /usr/bin/logger ]; then
1006		logger "$0: WARNING: $*"
1007	fi
1008	echo 1>&2 "$0: WARNING: $*"
1009}
1010
1011#
1012# info message
1013#	Display informational message to stdout and log to syslog.
1014#
1015info()
1016{
1017	case ${rc_info} in
1018	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1019		if [ -x /usr/bin/logger ]; then
1020			logger "$0: INFO: $*"
1021		fi
1022		echo "$0: INFO: $*"
1023		;;
1024	esac
1025}
1026
1027#
1028# debug message
1029#	If debugging is enabled in rc.conf output message to stderr.
1030#	BEWARE that you don't call any subroutine that itself calls this
1031#	function.
1032#
1033debug()
1034{
1035	case ${rc_debug} in
1036	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1037		if [ -x /usr/bin/logger ]; then
1038			logger "$0: INFO: $*"
1039		fi
1040		echo 1>&2 "$0: DEBUG: $*"
1041		;;
1042	esac
1043}
1044
1045#
1046# backup_file action file cur backup
1047#	Make a backup copy of `file' into `cur', and save the previous
1048#	version of `cur' as `backup' or use rcs for archiving.
1049#
1050#	This routine checks the value of the backup_uses_rcs variable,
1051#	which can be either YES or NO.
1052#
1053#	The `action' keyword can be one of the following:
1054#
1055#	add		`file' is now being backed up (and is possibly
1056#			being reentered into the backups system).  `cur'
1057#			is created and RCS files, if necessary, are
1058#			created as well.
1059#
1060#	update		`file' has changed and needs to be backed up.
1061#			If `cur' exists, it is copied to to `back' or
1062#			checked into RCS (if the repository file is old),
1063#			and then `file' is copied to `cur'.  Another RCS
1064#			check in done here if RCS is being used.
1065#
1066#	remove		`file' is no longer being tracked by the backups
1067#			system.  If RCS is not being used, `cur' is moved
1068#			to `back', otherwise an empty file is checked in,
1069#			and then `cur' is removed.
1070#
1071#
1072backup_file()
1073{
1074	_action=$1
1075	_file=$2
1076	_cur=$3
1077	_back=$4
1078
1079	if checkyesno backup_uses_rcs; then
1080		_msg0="backup archive"
1081		_msg1="update"
1082
1083		# ensure that history file is not locked
1084		if [ -f $_cur,v ]; then
1085			rcs -q -u -U -M $_cur
1086		fi
1087
1088		# ensure after switching to rcs that the
1089		# current backup is not lost
1090		if [ -f $_cur ]; then
1091			# no archive, or current newer than archive
1092			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1093				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1094				rcs -q -kb -U $_cur
1095				co -q -f -u $_cur
1096			fi
1097		fi
1098
1099		case $_action in
1100		add|update)
1101			cp -p $_file $_cur
1102			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1103			rcs -q -kb -U $_cur
1104			co -q -f -u $_cur
1105			chown root:wheel $_cur $_cur,v
1106			;;
1107		remove)
1108			cp /dev/null $_cur
1109			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1110			rcs -q -kb -U $_cur
1111			chown root:wheel $_cur $_cur,v
1112			rm $_cur
1113			;;
1114		esac
1115	else
1116		case $_action in
1117		add|update)
1118			if [ -f $_cur ]; then
1119				cp -p $_cur $_back
1120			fi
1121			cp -p $_file $_cur
1122			chown root:wheel $_cur
1123			;;
1124		remove)
1125			mv -f $_cur $_back
1126			;;
1127		esac
1128	fi
1129}
1130
1131# make_symlink src link
1132#	Make a symbolic link 'link' to src from basedir. If the
1133#	directory in which link is to be created does not exist
1134#	a warning will be displayed and an error will be returned.
1135#	Returns 0 on sucess, 1 otherwise.
1136#
1137make_symlink()
1138{
1139	local src link linkdir _me
1140	src="$1"
1141	link="$2"
1142	linkdir="`dirname $link`"
1143	_me="make_symlink()"
1144
1145	if [ -z "$src" -o -z "$link" ]; then
1146		warn "$_me: requires two arguments."
1147		return 1
1148	fi
1149	if [ ! -d "$linkdir" ]; then
1150		warn "$_me: the directory $linkdir does not exist."
1151		return 1
1152	fi
1153	if ! ln -sf $src $link; then
1154		warn "$_me: unable to make a symbolic link from $link to $src"
1155		return 1
1156	fi
1157	return 0
1158}
1159
1160# devfs_rulesets_from_file file
1161#	Reads a set of devfs commands from file, and creates
1162#	the specified rulesets with their rules. Returns non-zero
1163#	if there was an error.
1164#
1165devfs_rulesets_from_file()
1166{
1167	local file _err _me
1168	file="$1"
1169	_me="devfs_rulesets_from_file"
1170	_err=0
1171
1172	if [ -z "$file" ]; then
1173		warn "$_me: you must specify a file"
1174		return 1
1175	fi
1176	if [ ! -e "$file" ]; then
1177		debug "$_me: no such file ($file)"
1178		return 0
1179	fi
1180	debug "reading rulesets from file ($file)"
1181	{ while read line
1182	do
1183		case $line in
1184		\#*)
1185			continue
1186			;;
1187		\[*\]*)
1188			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1189			if [ -z "$rulenum" ]; then
1190				warn "$_me: cannot extract rule number ($line)"
1191				_err=1
1192				break
1193			fi
1194			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1195			if [ -z "$rulename" ]; then
1196				warn "$_me: cannot extract rule name ($line)"
1197				_err=1
1198				break;
1199			fi
1200			eval $rulename=\$rulenum
1201			debug "found ruleset: $rulename=$rulenum"
1202			if ! /sbin/devfs rule -s $rulenum delset; then
1203				_err=1
1204				break
1205			fi
1206			;;
1207		*)
1208			rulecmd="${line%%"\#*"}"
1209			# evaluate the command incase it includes
1210			# other rules
1211			if [ -n "$rulecmd" ]; then
1212				debug "adding rule ($rulecmd)"
1213				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1214				then
1215					_err=1
1216					break
1217				fi
1218			fi
1219			;;
1220		esac
1221		if [ $_err -ne 0 ]; then
1222			debug "error in $_me"
1223			break
1224		fi
1225	done } < $file
1226	return $_err
1227}
1228
1229# devfs_init_rulesets
1230#	Initializes rulesets from configuration files. Returns
1231#	non-zero if there was an error.
1232#
1233devfs_init_rulesets()
1234{
1235	local file _me
1236	_me="devfs_init_rulesets"
1237
1238	# Go through this only once
1239	if [ -n "$devfs_rulesets_init" ]; then
1240		debug "$_me: devfs rulesets already initialized"
1241		return
1242	fi
1243	for file in $devfs_rulesets; do
1244		devfs_rulesets_from_file $file || return 1
1245	done
1246	devfs_rulesets_init=1
1247	debug "$_me: devfs rulesets initialized"
1248	return 0
1249}
1250
1251# devfs_set_ruleset ruleset [dir]
1252#	Sets the default ruleset of dir to ruleset. The ruleset argument
1253#	must be a ruleset name as specified in devfs.rules(5) file.
1254#	Returns non-zero if it could not set it successfully.
1255#
1256devfs_set_ruleset()
1257{
1258	local devdir rs _me
1259	[ -n "$1" ] && eval rs=\$$1 || rs=
1260	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1261	_me="devfs_set_ruleset"
1262
1263	if [ -z "$rs" ]; then
1264		warn "$_me: you must specify a ruleset number"
1265		return 1
1266	fi
1267	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1268	if ! /sbin/devfs $devdir ruleset $rs; then
1269		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1270		return 1
1271	fi
1272	return 0
1273}
1274
1275# devfs_apply_ruleset ruleset [dir]
1276#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1277#	The ruleset argument must be a ruleset name as specified
1278#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1279#	if it could not apply the ruleset.
1280#
1281devfs_apply_ruleset()
1282{
1283	local devdir rs _me
1284	[ -n "$1" ] && eval rs=\$$1 || rs=
1285	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1286	_me="devfs_apply_ruleset"
1287
1288	if [ -z "$rs" ]; then
1289		warn "$_me: you must specify a ruleset"
1290		return 1
1291	fi
1292	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1293	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1294		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1295		return 1
1296	fi
1297	return 0
1298}
1299
1300# devfs_domount dir [ruleset]
1301#	Mount devfs on dir. If ruleset is specified it is set
1302#	on the mount-point. It must also be a ruleset name as specified
1303#	in a devfs.rules(5) file. Returns 0 on success.
1304#
1305devfs_domount()
1306{
1307	local devdir rs _me
1308	devdir="$1"
1309	[ -n "$2" ] && rs=$2 || rs=
1310	_me="devfs_domount()"
1311
1312	if [ -z "$devdir" ]; then
1313		warn "$_me: you must specify a mount-point"
1314		return 1
1315	fi
1316	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1317	if ! mount -t devfs dev "$devdir"; then
1318		warn "$_me: Unable to mount devfs on $devdir"
1319		return 1
1320	fi
1321	if [ -n "$rs" ]; then
1322		devfs_init_rulesets
1323		devfs_set_ruleset $rs $devdir
1324		devfs -m $devdir rule applyset
1325	fi
1326	return 0
1327}
1328
1329# devfs_mount_jail dir [ruleset]
1330#	Mounts a devfs file system appropriate for jails
1331#	on the directory dir. If ruleset is specified, the ruleset
1332#	it names will be used instead.  If present, ruleset must
1333#	be the name of a ruleset as defined in a devfs.rules(5) file.
1334#	This function returns non-zero if an error occurs.
1335#
1336devfs_mount_jail()
1337{
1338	local jdev rs _me
1339	jdev="$1"
1340	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1341	_me="devfs_mount_jail"
1342
1343	devfs_init_rulesets
1344	if ! devfs_domount "$jdev" $rs; then
1345		warn "$_me: devfs was not mounted on $jdev"
1346		return 1
1347	fi
1348	return 0
1349}
1350
1351# Provide a function for normalizing the mounting of memory
1352# filesystems.  This should allow the rest of the code here to remain
1353# as close as possible between 5-current and 4-stable.
1354#   $1 = size
1355#   $2 = mount point
1356#   $3 = (optional) extra mdmfs flags
1357mount_md()
1358{
1359	if [ -n "$3" ]; then
1360		flags="$3"
1361	fi
1362	/sbin/mdmfs $flags -s $1 md $2
1363}
1364
1365# Code common to scripts that need to load a kernel module
1366# if it isn't in the kernel yet. Syntax:
1367#   load_kld [-e regex] [-m module] file
1368# where -e or -m chooses the way to check if the module
1369# is already loaded:
1370#   regex is egrep'd in the output from `kldstat -v',
1371#   module is passed to `kldstat -m'.
1372# The default way is as though `-m file' were specified.
1373load_kld()
1374{
1375	local _loaded _mod _opt _re
1376
1377	while getopts "e:m:" _opt; do
1378		case "$_opt" in
1379		e) _re="$OPTARG" ;;
1380		m) _mod="$OPTARG" ;;
1381		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1382		esac
1383	done
1384	shift $(($OPTIND - 1))
1385	if [ $# -ne 1 ]; then
1386		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1387	fi
1388	_mod=${_mod:-$1}
1389	_loaded=false
1390	if [ -n "$_re" ]; then
1391		if kldstat -v | egrep -q -e "$_re"; then
1392			_loaded=true
1393		fi
1394	else
1395		if kldstat -q -m "$_mod"; then
1396			_loaded=true
1397		fi
1398	fi
1399	if ! $_loaded; then
1400		if ! kldload "$1"; then
1401			warn "Unable to load kernel module $1"
1402			return 1
1403		else
1404			info "$1 kernel module loaded."
1405		fi
1406	else
1407		debug "load_kld: $1 kernel module already loaded."
1408	fi
1409	return 0
1410}
1411
1412# ltr str src dst
1413#	Change every $src in $str to $dst.
1414#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1415#	awk(1).
1416ltr()
1417{
1418	local _str _src _dst _out _com
1419	_str=$1
1420	_src=$2
1421	_dst=$3
1422	_out=""
1423
1424	IFS=${_src}
1425	for _com in ${_str}; do
1426		if [ -z "${_out}" ]; then
1427			_out="${_com}"
1428		else
1429			_out="${_out}${_dst}${_com}"
1430		fi
1431	done
1432	echo "${_out}"
1433}
1434
1435# Creates a list of providers for GELI encryption.
1436geli_make_list()
1437{
1438	local devices devices2
1439	local provider mountpoint type options rest
1440
1441	# Create list of GELI providers from fstab.
1442	while read provider mountpoint type options rest ; do
1443		case ":${options}" in
1444		:*noauto*)
1445			noauto=yes
1446			;;
1447		*)
1448			noauto=no
1449			;;
1450		esac
1451
1452		case ":${provider}" in
1453		:#*)
1454			continue
1455			;;
1456		*.eli)
1457			# Skip swap devices.
1458			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1459				continue
1460			fi
1461			devices="${devices} ${provider}"
1462			;;
1463		esac
1464	done < /etc/fstab
1465
1466	# Append providers from geli_devices.
1467	devices="${devices} ${geli_devices}"
1468
1469	for provider in ${devices}; do
1470		provider=${provider%.eli}
1471		provider=${provider#/dev/}
1472		devices2="${devices2} ${provider}"
1473	done
1474
1475	echo ${devices2}
1476}
1477
1478# Find scripts in local_startup directories that use the old syntax
1479#
1480find_local_scripts_old () {
1481	zlist=''
1482	slist=''
1483	for dir in ${local_startup}; do
1484		if [ -d "${dir}" ]; then
1485			for file in ${dir}/[0-9]*.sh; do
1486				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1487				    continue
1488				zlist="$zlist $file"
1489			done
1490			for file in ${dir}/[^0-9]*.sh; do
1491				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1492				    continue
1493				slist="$slist $file"
1494			done
1495		fi
1496	done
1497}
1498
1499find_local_scripts_new () {
1500	local_rc=''
1501	for dir in ${local_startup}; do
1502		if [ -d "${dir}" ]; then
1503			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1504				case "$file" in
1505				*.sample) ;;
1506				*)	if [ -x "$file" ]; then
1507						local_rc="${local_rc} ${file}"
1508					fi
1509					;;
1510				esac
1511			done
1512		fi
1513	done
1514}
1515
1516fi
1517
1518_rc_subr_loaded=:
1519