rc.subr revision 78344
178344Sobrien# $NetBSD: rc.subr,v 1.28 2000/11/06 00:08:30 lukem Exp $
278344Sobrien#
378344Sobrien# Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
478344Sobrien# All rights reserved.
578344Sobrien#
678344Sobrien# This code is derived from software contributed to The NetBSD Foundation
778344Sobrien# by Luke Mewburn.
878344Sobrien#
978344Sobrien# Redistribution and use in source and binary forms, with or without
1078344Sobrien# modification, are permitted provided that the following conditions
1178344Sobrien# are met:
1278344Sobrien# 1. Redistributions of source code must retain the above copyright
1378344Sobrien#    notice, this list of conditions and the following disclaimer.
1478344Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1578344Sobrien#    notice, this list of conditions and the following disclaimer in the
1678344Sobrien#    documentation and/or other materials provided with the distribution.
1778344Sobrien# 3. All advertising materials mentioning features or use of this software
1878344Sobrien#    must display the following acknowledgement:
1978344Sobrien#        This product includes software developed by the NetBSD
2078344Sobrien#        Foundation, Inc. and its contributors.
2178344Sobrien# 4. Neither the name of The NetBSD Foundation nor the names of its
2278344Sobrien#    contributors may be used to endorse or promote products derived
2378344Sobrien#    from this software without specific prior written permission.
2478344Sobrien#
2578344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2678344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2778344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2878344Sobrien# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2978344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3078344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3178344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3278344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3378344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3478344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3578344Sobrien# POSSIBILITY OF SUCH DAMAGE.
3678344Sobrien#
3778344Sobrien# rc.subr
3878344Sobrien#	functions used by various rc scripts
3978344Sobrien#
4078344Sobrien
4178344Sobrien#
4278344Sobrien#	functions
4378344Sobrien#	---------
4478344Sobrien
4578344Sobrien#
4678344Sobrien# checkyesno var
4778344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
4878344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
4978344Sobrien#
5078344Sobriencheckyesno()
5178344Sobrien{
5278344Sobrien	eval _value=\$${1}
5378344Sobrien	case $_value in
5478344Sobrien
5578344Sobrien		#	"yes", "true", "on", or "1"
5678344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
5778344Sobrien		return 0
5878344Sobrien		;;
5978344Sobrien
6078344Sobrien		#	"no", "false", "off", or "0"
6178344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
6278344Sobrien		return 1
6378344Sobrien		;;
6478344Sobrien	*)
6578344Sobrien		warn "\$${1} is not set properly."
6678344Sobrien		return 1
6778344Sobrien		;;
6878344Sobrien	esac
6978344Sobrien}
7078344Sobrien
7178344Sobrien#
7278344Sobrien# mount_critical_filesystems
7378344Sobrien#	Go through the list of critical filesystems, checking each one
7478344Sobrien#	to see if it is mounted, and if it is not, mounting it.
7578344Sobrien#
7678344Sobrienmount_critical_filesystems()
7778344Sobrien{
7878344Sobrien	if [ $1 = local ]; then
7978344Sobrien		_fslist=$critical_filesystems_beforenet
8078344Sobrien	else
8178344Sobrien		_fslist=$critical_filesystems
8278344Sobrien	fi
8378344Sobrien	for _fs in $_fslist; do
8478344Sobrien		mount | (
8578344Sobrien			_ismounted=no
8678344Sobrien			while read what _on on _type type; do
8778344Sobrien				if [ $on = $_fs ]; then
8878344Sobrien					_ismounted=yes
8978344Sobrien				fi
9078344Sobrien			done
9178344Sobrien			if [ $_ismounted = no ]; then 
9278344Sobrien				mount $_fs >/dev/null 2>&1
9378344Sobrien			fi
9478344Sobrien		)  
9578344Sobrien	done
9678344Sobrien}
9778344Sobrien
9878344Sobrien#
9978344Sobrien# check_pidfile pidfile procname
10078344Sobrien#	Parses the first line of pidfile for a pid, and ensures
10178344Sobrien#	that the process is running and matches procname.
10278344Sobrien#	Prints the matching pid upon success, nothing otherwise.
10378344Sobrien#
10478344Sobriencheck_pidfile()
10578344Sobrien{
10678344Sobrien	_pidfile=$1
10778344Sobrien	_procname=$2
10878344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
10978344Sobrien		err 3 'USAGE: check_pidfile pidfile procname'
11078344Sobrien	fi
11178344Sobrien	if [ ! -f $_pidfile ]; then
11278344Sobrien		return
11378344Sobrien	fi
11478344Sobrien	read _pid _junk < $_pidfile
11578344Sobrien	if [ -z "$_pid" ]; then
11678344Sobrien		return
11778344Sobrien	fi
11878344Sobrien	_procnamebn=${_procname##*/}
11978344Sobrien	ps -p $_pid -o 'pid,command' | while read _npid _arg0 _argv; do
12078344Sobrien		if [ "$_npid" = "PID" ]; then
12178344Sobrien			continue
12278344Sobrien		fi
12378344Sobrien		if [   "$_arg0" = "$_procname" \
12478344Sobrien		    -o "$_arg0" = "$_procnamebn" \
12578344Sobrien		    -o "$_arg0" = "${_procnamebn}:" \
12678344Sobrien		    -o "$_arg0" = "(${_procnamebn})" ]; then
12778344Sobrien			echo $_npid
12878344Sobrien			return
12978344Sobrien		fi
13078344Sobrien	done
13178344Sobrien}
13278344Sobrien
13378344Sobrien#
13478344Sobrien# check_process procname
13578344Sobrien#	Ensures that a process (or processes) named procname is running.
13678344Sobrien#	Prints a list of matching pids.
13778344Sobrien#
13878344Sobriencheck_process()
13978344Sobrien{
14078344Sobrien	_procname=$1
14178344Sobrien	if [ -z "$_procname" ]; then
14278344Sobrien		err 3 'USAGE: check_process procname'
14378344Sobrien	fi
14478344Sobrien	_procnamebn=${_procname##*/}
14578344Sobrien	_pref=
14678344Sobrien	ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do
14778344Sobrien		if [ "$_npid" = "PID" ]; then
14878344Sobrien			continue
14978344Sobrien		fi
15078344Sobrien		if [   "$_arg0" = "$_procname" \
15178344Sobrien		    -o "$_arg0" = "$_procnamebn" \
15278344Sobrien		    -o "$_arg0" = "${_procnamebn}:" \
15378344Sobrien		    -o "$_arg0" = "(${_procnamebn})" ]; then
15478344Sobrien			echo -n "$_pref$_npid"
15578344Sobrien			_pref=" "
15678344Sobrien		fi
15778344Sobrien	done
15878344Sobrien}
15978344Sobrien
16078344Sobrien#
16178344Sobrien# run_rc_command arg
16278344Sobrien#	Search for arg in the list of supported commands, which is:
16378344Sobrien#		"start stop restart rcvar status ${extra_commands}"
16478344Sobrien#	If there's a match, run ${arg}_cmd or the default command (see below).
16578344Sobrien#
16678344Sobrien#	If arg has a given prefix, then change the operation as follows:
16778344Sobrien#		prefix	operation
16878344Sobrien#		------	---------
16978344Sobrien#		fast	Skip the pid check.
17078344Sobrien#		force	Set ${rcvar} to YES.
17178344Sobrien#
17278344Sobrien#	The following globals are used:
17378344Sobrien#
17478344Sobrien#	name		needed	function
17578344Sobrien#	----		------	--------
17678344Sobrien#	name		y	Name of script.
17778344Sobrien#
17878344Sobrien#	command		n	Full path to command.
17978344Sobrien#				Not needed if ${arg}_cmd is set for
18078344Sobrien#				each keyword.
18178344Sobrien#
18278344Sobrien#	command_args	n	Optional args/shell directives for command.
18378344Sobrien#
18478344Sobrien#	extra_commands	n	List of extra commands supported.
18578344Sobrien#
18678344Sobrien#	pidfile		n	If set, use check_pidfile $pidfile, else if
18778344Sobrien#				$command is set, use check_process $command.
18878344Sobrien#
18978344Sobrien#	rcvar		n	This is checked with checkyesno to determine
19078344Sobrien#				if the action should be run.
19178344Sobrien#
19278344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
19378344Sobrien#
19478344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
19578344Sobrien#				(if not using ${name}_chroot).
19678344Sobrien#
19778344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
19878344Sobrien#				NOTE:	$flags from the parent environment
19978344Sobrien#					can be used to override this.
20078344Sobrien#
20178344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
20278344Sobrien#
20378344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
20478344Sobrien#				using ${name}_chroot.
20578344Sobrien#
20678344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
20778344Sobrien#
20878344Sobrien#	${name}_groups	n	Supplementary group list to run chrooted
20978344Sobrien#				${command} with.
21078344Sobrien#
21178344Sobrien#	${_arg}_cmd	n	If set, use this as the action when invoked;
21278344Sobrien#				$_arg is available to the action to use.
21378344Sobrien#				Otherwise, use default command (see below)
21478344Sobrien#
21578344Sobrien#	${_arg}_precmd	n	If set, run just before performing the main
21678344Sobrien#				action in the default command (i.e, after
21778344Sobrien#				checking for required bits and process
21878344Sobrien#				(non)existance).
21978344Sobrien#				If this completes with a non-zero exit code,
22078344Sobrien#				don't run ${_arg}_cmd.
22178344Sobrien#
22278344Sobrien#	required_dirs	n	If set, check for the existence of the given
22378344Sobrien#				directories before running the default
22478344Sobrien#				(re)start command.
22578344Sobrien#
22678344Sobrien#	required_files	n	If set, check for the readability of the given
22778344Sobrien#				files before running the default (re)start
22878344Sobrien#				command.
22978344Sobrien#
23078344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
23178344Sobrien#				listed variables before running the default
23278344Sobrien#				(re)start command.
23378344Sobrien#
23478344Sobrien#	Default commands for a given arg:
23578344Sobrien#
23678344Sobrien#	arg		default
23778344Sobrien#	---		-------
23878344Sobrien#	status		Show if ${command} is running, etc.
23978344Sobrien#
24078344Sobrien#	start		if !running && checkyesno ${rcvar}
24178344Sobrien#				${command}
24278344Sobrien#
24378344Sobrien#	stop		if ${pidfile}
24478344Sobrien#				kill $sig_stop `check_pidfile $pidfile`
24578344Sobrien#			else
24678344Sobrien#				kill $sig_stop `check_process $command`
24778344Sobrien#			$sig_stop defaults to TERM.
24878344Sobrien#
24978344Sobrien#	reload		As stop, except use $sig_reload instead.
25078344Sobrien#			$sig_reload defaults to HUP.
25178344Sobrien#
25278344Sobrien#	restart		Run `stop' then `start'.
25378344Sobrien#
25478344Sobrien#
25578344Sobrienrun_rc_command()
25678344Sobrien{
25778344Sobrien	_arg=$1
25878344Sobrien	if [ -z "$name" ]; then
25978344Sobrien		err 3 '$name is not set.'
26078344Sobrien	fi
26178344Sobrien
26278344Sobrien	case "$_arg" in
26378344Sobrien	fast*)				# "fast" prefix; don't check pid
26478344Sobrien		_arg=${_arg#fast}
26578344Sobrien		_rc_fast_run=YES
26678344Sobrien		;;
26778344Sobrien	force*)				# "force prefix; always start
26878344Sobrien		_arg=${_arg#force}
26978344Sobrien		_rc_force_run=YES
27078344Sobrien		if [ -n "${rcvar}" ]; then
27178344Sobrien			eval ${rcvar}=YES
27278344Sobrien		fi
27378344Sobrien		;;
27478344Sobrien	esac
27578344Sobrien
27678344Sobrien	_keywords="start stop restart rcvar $extra_commands"
27778344Sobrien	_pid=
27878344Sobrien	_pidcmd=
27978344Sobrien					# setup pid check command if not fast
28078344Sobrien	if [ -z "$_rc_fast_run" ]; then
28178344Sobrien		if [ -n "$pidfile" ]; then
28278344Sobrien			_pidcmd='_pid=`check_pidfile '$pidfile' '$command'`'
28378344Sobrien		elif [ -n "$command" ]; then
28478344Sobrien			_pidcmd='_pid=`check_process '$command'`'
28578344Sobrien		fi
28678344Sobrien		if [ -n "$_pidcmd" ]; then
28778344Sobrien			_keywords="${_keywords} status"
28878344Sobrien		fi
28978344Sobrien	fi
29078344Sobrien
29178344Sobrien	if [ -z "$_arg" ]; then
29278344Sobrien		rc_usage "$_keywords"
29378344Sobrien	fi
29478344Sobrien
29578344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
29678344Sobrien		_flags=$flags
29778344Sobrien	else
29878344Sobrien		eval _flags=\$${name}_flags
29978344Sobrien	fi
30078344Sobrien	eval _chdir=\$${name}_chdir
30178344Sobrien	eval _chroot=\$${name}_chroot
30278344Sobrien	eval _nice=\$${name}_nice
30378344Sobrien	eval _user=\$${name}_user
30478344Sobrien	eval _group=\$${name}_group
30578344Sobrien	eval _groups=\$${name}_groups
30678344Sobrien
30778344Sobrien					# if ${rcvar} is set, and $1 is not
30878344Sobrien					# "rcvar" or "status", then run
30978344Sobrien					#	checkyesno ${rcvar}
31078344Sobrien					# and return if that failed
31178344Sobrien					#
31278344Sobrien	if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then
31378344Sobrien		if ! checkyesno ${rcvar}; then
31478344Sobrien			return 0
31578344Sobrien		fi
31678344Sobrien	fi
31778344Sobrien
31878344Sobrien	eval $_pidcmd			# determine the pid if necessary
31978344Sobrien
32078344Sobrien	for _elem in $_keywords; do
32178344Sobrien		if [ "$_elem" != "$_arg" ]; then
32278344Sobrien			continue
32378344Sobrien		fi
32478344Sobrien
32578344Sobrien					# if there's a custom ${XXX_cmd},
32678344Sobrien					# run that instead of the default
32778344Sobrien					#
32878344Sobrien		eval _cmd=\$${_arg}_cmd
32978344Sobrien		eval _precmd=\$${_arg}_precmd
33078344Sobrien		if [ -n "$_cmd" ]; then
33178344Sobrien					# if the precmd failed and force
33278344Sobrien					# isn't set, exit
33378344Sobrien					#
33478344Sobrien			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
33578344Sobrien				return 1
33678344Sobrien			fi
33778344Sobrien
33878344Sobrien			eval $_cmd
33978344Sobrien			return 0
34078344Sobrien		fi
34178344Sobrien
34278344Sobrien		case "$_arg" in		# default operations...
34378344Sobrien
34478344Sobrien		status)
34578344Sobrien			if [ -n "$_pid" ]; then
34678344Sobrien				echo "${name} is running as pid $_pid."
34778344Sobrien			else
34878344Sobrien				echo "${name} is not running."
34978344Sobrien				return 1
35078344Sobrien			fi
35178344Sobrien			;;
35278344Sobrien
35378344Sobrien		start)
35478344Sobrien			if [ -n "$_pid" ]; then
35578344Sobrien				echo "${name} already running? (pid=$_pid)."
35678344Sobrien				exit 1
35778344Sobrien			fi
35878344Sobrien
35978344Sobrien			if [ ! -x $command ]; then
36078344Sobrien				return 0
36178344Sobrien			fi
36278344Sobrien
36378344Sobrien					# check for required variables,
36478344Sobrien					# directories, and files
36578344Sobrien					#
36678344Sobrien			for _f in $required_vars; do
36778344Sobrien				if ! checkyesno $_f; then
36878344Sobrien					warn "\$${_f} is not set."
36978344Sobrien					if [ -z "$_rc_force_run" ]; then
37078344Sobrien						return 1
37178344Sobrien					fi
37278344Sobrien				fi
37378344Sobrien			done
37478344Sobrien			for _f in $required_dirs; do
37578344Sobrien				if [ ! -d "${_f}/." ]; then
37678344Sobrien					warn "${_f} is not a directory."
37778344Sobrien					if [ -z "$_rc_force_run" ]; then
37878344Sobrien						return 1
37978344Sobrien					fi
38078344Sobrien				fi
38178344Sobrien			done
38278344Sobrien			for _f in $required_files; do
38378344Sobrien				if [ ! -r "${_f}" ]; then
38478344Sobrien					warn "${_f} is not readable."
38578344Sobrien					if [ -z "$_rc_force_run" ]; then
38678344Sobrien						return 1
38778344Sobrien					fi
38878344Sobrien				fi
38978344Sobrien			done
39078344Sobrien
39178344Sobrien					# if the precmd failed and force
39278344Sobrien					# isn't set, exit
39378344Sobrien					#
39478344Sobrien			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
39578344Sobrien				return 1
39678344Sobrien			fi
39778344Sobrien
39878344Sobrien
39978344Sobrien					# setup the command to run, and run it
40078344Sobrien					#
40178344Sobrien			echo "Starting ${name}."
40278344Sobrien			if [ -n "$_chroot" ]; then
40378344Sobrien				_doit="\
40478344Sobrien${_nice:+nice -n $_nice }\
40578344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
40678344Sobrien$_chroot $command $_flags $command_args"
40778344Sobrien			else
40878344Sobrien				_doit="\
40978344Sobrien${_user:+su -m $_user -c 'sh -c \"}\
41078344Sobrien${_chdir:+cd $_chdir; }\
41178344Sobrien${_nice:+nice -n $_nice }\
41278344Sobrien$command $_flags $command_args\
41378344Sobrien${_user:+\"'}"
41478344Sobrien			fi
41578344Sobrien			eval $_doit
41678344Sobrien			;;
41778344Sobrien
41878344Sobrien		stop)
41978344Sobrien			if [ -z "$_pid" ]; then
42078344Sobrien				if [ -n "$pidfile" ]; then
42178344Sobrien					echo \
42278344Sobrien				    "${name} not running? (check $pidfile)."
42378344Sobrien				else
42478344Sobrien					echo "${name} not running?"
42578344Sobrien				fi
42678344Sobrien				exit 1
42778344Sobrien			fi
42878344Sobrien
42978344Sobrien			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
43078344Sobrien				return 1
43178344Sobrien			fi
43278344Sobrien			echo "Stopping ${name}."
43378344Sobrien			_doit=\
43478344Sobrien"${_user:+su -m $_user -c '}kill -${sig_stop:-TERM} $_pid${_user:+'}"
43578344Sobrien			eval $_doit
43678344Sobrien			;;
43778344Sobrien
43878344Sobrien		reload)
43978344Sobrien			if [ -z "$_pid" ]; then
44078344Sobrien				if [ -n "$pidfile" ]; then
44178344Sobrien					echo \
44278344Sobrien				    "${name} not running? (check $pidfile)."
44378344Sobrien				else
44478344Sobrien					echo "${name} not running?"
44578344Sobrien				fi
44678344Sobrien				exit 1
44778344Sobrien			fi
44878344Sobrien			echo "Reloading ${name} config files."
44978344Sobrien			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
45078344Sobrien				return 1
45178344Sobrien			fi
45278344Sobrien			_doit=\
45378344Sobrien"${_user:+su -m $_user -c '}kill -${sig_reload:-HUP} $_pid${_user:+'}"
45478344Sobrien			eval $_doit
45578344Sobrien			;;
45678344Sobrien
45778344Sobrien		restart)
45878344Sobrien			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
45978344Sobrien				return 1
46078344Sobrien			fi
46178344Sobrien					# prevent restart being called more
46278344Sobrien					# than once by any given script
46378344Sobrien					#
46478344Sobrien			if [ -n "$_rc_restart_done" ]; then
46578344Sobrien				return 0
46678344Sobrien			fi
46778344Sobrien			_rc_restart_done=YES
46878344Sobrien			( $0 ${_rc_force_run:+force}stop )
46978344Sobrien			sleep 1
47078344Sobrien			$0 ${_rc_force_run:+force}start
47178344Sobrien
47278344Sobrien			;;
47378344Sobrien
47478344Sobrien		rcvar)
47578344Sobrien			echo "# $name"
47678344Sobrien			if [ -n "$rcvar" ]; then
47778344Sobrien				if checkyesno ${rcvar}; then
47878344Sobrien					echo "\$${rcvar}=YES"
47978344Sobrien				else
48078344Sobrien					echo "\$${rcvar}=NO"
48178344Sobrien				fi
48278344Sobrien			fi
48378344Sobrien			;;
48478344Sobrien
48578344Sobrien		*)
48678344Sobrien			rc_usage "$_keywords"
48778344Sobrien			;;
48878344Sobrien
48978344Sobrien		esac
49078344Sobrien		return 0
49178344Sobrien	done
49278344Sobrien
49378344Sobrien	echo 1>&2 "$0: unknown directive '$_arg'."
49478344Sobrien	rc_usage "$_keywords"
49578344Sobrien	exit 1
49678344Sobrien}
49778344Sobrien
49878344Sobrien#
49978344Sobrien# run_rc_script file arg
50078344Sobrien#	Start the script `file' with `arg', and correctly handle the
50178344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
50278344Sobrien#	sourced into the current environment.  Otherwise it's run as
50378344Sobrien#	a child process.
50478344Sobrien#
50578344Sobrien#	Note: because `.sh' files are sourced into the current environment
50678344Sobrien#	run_rc_command shouldn't be used because its difficult to ensure
50778344Sobrien#	that the global variable state before and after the sourcing of 
50878344Sobrien#	the .sh file won't adversely affect other scripts.
50978344Sobrien#
51078344Sobrienrun_rc_script()
51178344Sobrien{
51278344Sobrien	_file=$1
51378344Sobrien	_arg=$2
51478344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
51578344Sobrien		err 3 'USAGE: run_rc_script file arg'
51678344Sobrien	fi
51778344Sobrien
51878344Sobrien	case "$_file" in
51978344Sobrien	*.sh)				# run in current shell
52078344Sobrien		set $_arg ; . $_file
52178344Sobrien		;;
52278344Sobrien	*)				# run in subshell
52378344Sobrien		( set $_arg ; . $_file )
52478344Sobrien		;;
52578344Sobrien	esac
52678344Sobrien}
52778344Sobrien
52878344Sobrien#
52978344Sobrien# load_rc_config
53078344Sobrien#	Source in the configuration file for a given command.
53178344Sobrien#
53278344Sobrienload_rc_config()
53378344Sobrien{
53478344Sobrien	_command=$1
53578344Sobrien	if [ -z "$_command" ]; then
53678344Sobrien		err 3 'USAGE: load_rc_config command'
53778344Sobrien	fi
53878344Sobrien
53978344Sobrien	. /etc/rc.conf
54078344Sobrien	if [ -f /etc/rc.conf.d/"$_command" ]; then
54178344Sobrien		. /etc/rc.conf.d/"$_command"
54278344Sobrien	fi
54378344Sobrien}
54478344Sobrien
54578344Sobrien
54678344Sobrien#
54778344Sobrien# rc_usage commands
54878344Sobrien#	Print a usage string for $0, with `commands' being a list of
54978344Sobrien#	valid commands.
55078344Sobrien#
55178344Sobrienrc_usage()
55278344Sobrien{
55378344Sobrien	echo -n 1>&2 "Usage: $0 [fast|force]("
55478344Sobrien
55578344Sobrien	_sep=
55678344Sobrien	for _elem in $*; do
55778344Sobrien		echo -n 1>&2 "$_sep$_elem"
55878344Sobrien		_sep="|"
55978344Sobrien	done
56078344Sobrien	echo 1>&2 ")"
56178344Sobrien	exit 1
56278344Sobrien}
56378344Sobrien
56478344Sobrien#
56578344Sobrien# err exitval message
56678344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
56778344Sobrien#
56878344Sobrienerr()
56978344Sobrien{
57078344Sobrien	exitval=$1
57178344Sobrien	shift
57278344Sobrien
57378344Sobrien	logger "$0: ERROR: $*"
57478344Sobrien	echo 1>&2 "$0: ERROR: $*"
57578344Sobrien	exit $exitval
57678344Sobrien}
57778344Sobrien
57878344Sobrien#
57978344Sobrien# warn message
58078344Sobrien#	Display message to stderr and log to the syslog.
58178344Sobrien#
58278344Sobrienwarn()
58378344Sobrien{
58478344Sobrien	logger "$0: WARNING: $*"
58578344Sobrien	echo 1>&2 "$0: WARNING: $*"
58678344Sobrien}
587