rc.subr revision 1.29
11.29Slukem# $NetBSD: rc.subr,v 1.29 2000/11/17 03:47:43 lukem Exp $
21.11Slukem#
31.11Slukem# Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
41.11Slukem# All rights reserved.
51.11Slukem#
61.11Slukem# This code is derived from software contributed to The NetBSD Foundation
71.11Slukem# by Luke Mewburn.
81.11Slukem#
91.11Slukem# Redistribution and use in source and binary forms, with or without
101.11Slukem# modification, are permitted provided that the following conditions
111.11Slukem# are met:
121.11Slukem# 1. Redistributions of source code must retain the above copyright
131.11Slukem#    notice, this list of conditions and the following disclaimer.
141.11Slukem# 2. Redistributions in binary form must reproduce the above copyright
151.11Slukem#    notice, this list of conditions and the following disclaimer in the
161.11Slukem#    documentation and/or other materials provided with the distribution.
171.11Slukem# 3. All advertising materials mentioning features or use of this software
181.11Slukem#    must display the following acknowledgement:
191.11Slukem#        This product includes software developed by the NetBSD
201.11Slukem#        Foundation, Inc. and its contributors.
211.11Slukem# 4. Neither the name of The NetBSD Foundation nor the names of its
221.11Slukem#    contributors may be used to endorse or promote products derived
231.11Slukem#    from this software without specific prior written permission.
241.11Slukem#
251.11Slukem# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
261.11Slukem# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.11Slukem# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.11Slukem# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
291.11Slukem# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.11Slukem# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.11Slukem# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.11Slukem# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.11Slukem# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.11Slukem# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.11Slukem# POSSIBILITY OF SUCH DAMAGE.
361.11Slukem#
371.11Slukem# rc.subr
381.11Slukem#	functions used by various rc scripts
391.11Slukem#
401.11Slukem
411.11Slukem#
421.11Slukem#	functions
431.11Slukem#	---------
441.1Scjs
451.5Slukem#
461.11Slukem# checkyesno var
471.5Slukem#	Test $1 variable, and warn if not set to YES or NO.
481.11Slukem#	Return 0 if it's "yes" (et al), nonzero otherwise.
491.5Slukem#
501.11Slukemcheckyesno()
511.11Slukem{
521.11Slukem	eval _value=\$${1}
531.11Slukem	case $_value in
541.4Slukem
551.4Slukem		#	"yes", "true", "on", or "1"
561.4Slukem	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
571.4Slukem		return 0
581.3Slukem		;;
591.4Slukem
601.4Slukem		#	"no", "false", "off", or "0"
611.4Slukem	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
621.4Slukem		return 1
631.3Slukem		;;
641.3Slukem	*)
651.11Slukem		warn "\$${1} is not set properly."
661.4Slukem		return 1
671.3Slukem		;;
681.3Slukem	esac
691.6Smellon}
701.6Smellon
711.6Smellon#
721.6Smellon# mount_critical_filesystems
731.6Smellon#	Go through the list of critical filesystems, checking each one
741.6Smellon#	to see if it is mounted, and if it is not, mounting it.
751.6Smellon#
761.11Slukemmount_critical_filesystems()
771.11Slukem{
781.10Sdrochner	if [ $1 = local ]; then
791.10Sdrochner		_fslist=$critical_filesystems_beforenet
801.10Sdrochner	else
811.10Sdrochner		_fslist=$critical_filesystems
821.10Sdrochner	fi
831.17Slukem	for _fs in $_fslist; do
841.6Smellon		mount | (
851.17Slukem			_ismounted=no
861.6Smellon			while read what _on on _type type; do
871.17Slukem				if [ $on = $_fs ]; then
881.17Slukem					_ismounted=yes
891.6Smellon				fi
901.6Smellon			done
911.17Slukem			if [ $_ismounted = no ]; then 
921.17Slukem				mount $_fs >/dev/null 2>&1
931.6Smellon			fi
941.6Smellon		)  
951.6Smellon	done
961.7Scjs}
971.7Scjs
981.11Slukem#
991.11Slukem# check_pidfile pidfile procname
1001.11Slukem#	Parses the first line of pidfile for a pid, and ensures
1011.11Slukem#	that the process is running and matches procname.
1021.11Slukem#	Prints the matching pid upon success, nothing otherwise.
1031.11Slukem#
1041.11Slukemcheck_pidfile()
1051.11Slukem{
1061.11Slukem	_pidfile=$1
1071.11Slukem	_procname=$2
1081.11Slukem	if [ -z "$_pidfile" -o -z "$_procname" ]; then
1091.11Slukem		err 3 'USAGE: check_pidfile pidfile procname'
1101.11Slukem	fi
1111.11Slukem	if [ ! -f $_pidfile ]; then
1121.11Slukem		return
1131.11Slukem	fi
1141.11Slukem	read _pid _junk < $_pidfile
1151.11Slukem	if [ -z "$_pid" ]; then
1161.11Slukem		return
1171.11Slukem	fi
1181.26Slukem	_procnamebn=${_procname##*/}
1191.11Slukem	ps -p $_pid -o 'pid,command' | while read _npid _arg0 _argv; do
1201.11Slukem		if [ "$_npid" = "PID" ]; then
1211.11Slukem			continue
1221.11Slukem		fi
1231.15Slukem		if [   "$_arg0" = "$_procname" \
1241.15Slukem		    -o "$_arg0" = "$_procnamebn" \
1251.15Slukem		    -o "$_arg0" = "${_procnamebn}:" \
1261.15Slukem		    -o "$_arg0" = "(${_procnamebn})" ]; then
1271.11Slukem			echo $_npid
1281.11Slukem			return
1291.11Slukem		fi
1301.11Slukem	done
1311.11Slukem}
1321.11Slukem
1331.11Slukem#
1341.11Slukem# check_process procname
1351.11Slukem#	Ensures that a process (or processes) named procname is running.
1361.11Slukem#	Prints a list of matching pids.
1371.11Slukem#
1381.11Slukemcheck_process()
1391.11Slukem{
1401.11Slukem	_procname=$1
1411.11Slukem	if [ -z "$_procname" ]; then
1421.11Slukem		err 3 'USAGE: check_process procname'
1431.11Slukem	fi
1441.26Slukem	_procnamebn=${_procname##*/}
1451.11Slukem	_pref=
1461.11Slukem	ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do
1471.11Slukem		if [ "$_npid" = "PID" ]; then
1481.11Slukem			continue
1491.11Slukem		fi
1501.15Slukem		if [   "$_arg0" = "$_procname" \
1511.15Slukem		    -o "$_arg0" = "$_procnamebn" \
1521.15Slukem		    -o "$_arg0" = "${_procnamebn}:" \
1531.15Slukem		    -o "$_arg0" = "(${_procnamebn})" ]; then
1541.11Slukem			echo -n "$_pref$_npid"
1551.11Slukem			_pref=" "
1561.11Slukem		fi
1571.11Slukem	done
1581.11Slukem}
1591.11Slukem
1601.11Slukem#
1611.16Slukem# run_rc_command arg
1621.16Slukem#	Search for arg in the list of supported commands, which is:
1631.16Slukem#		"start stop restart rcvar status ${extra_commands}"
1641.11Slukem#	If there's a match, run ${arg}_cmd or the default command (see below).
1651.11Slukem#
1661.16Slukem#	If arg has a given prefix, then change the operation as follows:
1671.11Slukem#		prefix	operation
1681.11Slukem#		------	---------
1691.11Slukem#		fast	Skip the pid check.
1701.11Slukem#		force	Set ${rcvar} to YES.
1711.11Slukem#
1721.11Slukem#	The following globals are used:
1731.24Slukem#
1741.11Slukem#	name		needed	function
1751.11Slukem#	----		------	--------
1761.11Slukem#	name		y	Name of script.
1771.24Slukem#
1781.11Slukem#	command		n	Full path to command.
1791.11Slukem#				Not needed if ${arg}_cmd is set for
1801.11Slukem#				each keyword.
1811.24Slukem#
1821.11Slukem#	command_args	n	Optional args/shell directives for command.
1831.24Slukem#
1841.16Slukem#	extra_commands	n	List of extra commands supported.
1851.24Slukem#
1861.11Slukem#	pidfile		n	If set, use check_pidfile $pidfile, else if
1871.11Slukem#				$command is set, use check_process $command.
1881.24Slukem#
1891.24Slukem#	rcvar		n	This is checked with checkyesno to determine
1901.24Slukem#				if the action should be run.
1911.24Slukem#
1921.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
1931.24Slukem#
1941.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
1951.22Slukem#				(if not using ${name}_chroot).
1961.24Slukem#
1971.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
1981.24Slukem#				NOTE:	$flags from the parent environment
1991.24Slukem#					can be used to override this.
2001.24Slukem#
2011.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
2021.24Slukem#
2031.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
2041.22Slukem#				using ${name}_chroot.
2051.24Slukem#
2061.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
2071.24Slukem#
2081.24Slukem#	${name}_groups	n	Supplementary group list to run chrooted
2091.24Slukem#				${command} with.
2101.24Slukem#
2111.11Slukem#	${_arg}_cmd	n	If set, use this as the action when invoked;
2121.11Slukem#				$_arg is available to the action to use.
2131.11Slukem#				Otherwise, use default command (see below)
2141.24Slukem#
2151.11Slukem#	${_arg}_precmd	n	If set, run just before performing the main
2161.11Slukem#				action in the default command (i.e, after
2171.11Slukem#				checking for required bits and process
2181.11Slukem#				(non)existance).
2191.11Slukem#				If this completes with a non-zero exit code,
2201.11Slukem#				don't run ${_arg}_cmd.
2211.24Slukem#
2221.11Slukem#	required_dirs	n	If set, check for the existence of the given
2231.11Slukem#				directories before running the default
2241.11Slukem#				(re)start command.
2251.24Slukem#
2261.11Slukem#	required_files	n	If set, check for the readability of the given
2271.11Slukem#				files before running the default (re)start
2281.11Slukem#				command.
2291.24Slukem#
2301.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
2311.11Slukem#				listed variables before running the default
2321.11Slukem#				(re)start command.
2331.11Slukem#
2341.11Slukem#	Default commands for a given arg:
2351.24Slukem#
2361.11Slukem#	arg		default
2371.11Slukem#	---		-------
2381.11Slukem#	status		Show if ${command} is running, etc.
2391.24Slukem#
2401.11Slukem#	start		if !running && checkyesno ${rcvar}
2411.11Slukem#				${command}
2421.24Slukem#
2431.11Slukem#	stop		if ${pidfile}
2441.11Slukem#				kill $sig_stop `check_pidfile $pidfile`
2451.11Slukem#			else
2461.11Slukem#				kill $sig_stop `check_process $command`
2471.11Slukem#			$sig_stop defaults to TERM.
2481.24Slukem#
2491.11Slukem#	reload		As stop, except use $sig_reload instead.
2501.11Slukem#			$sig_reload defaults to HUP.
2511.24Slukem#
2521.11Slukem#	restart		Run `stop' then `start'.
2531.11Slukem#
2541.24Slukem#
2551.11Slukemrun_rc_command()
2561.11Slukem{
2571.11Slukem	_arg=$1
2581.24Slukem	if [ -z "$name" ]; then
2591.24Slukem		err 3 '$name is not set.'
2601.11Slukem	fi
2611.11Slukem
2621.11Slukem	case "$_arg" in
2631.24Slukem	fast*)				# "fast" prefix; don't check pid
2641.11Slukem		_arg=${_arg#fast}
2651.11Slukem		_rc_fast_run=YES
2661.11Slukem		;;
2671.24Slukem	force*)				# "force prefix; always start
2681.11Slukem		_arg=${_arg#force}
2691.24Slukem		_rc_force_run=YES
2701.29Slukem		if [ -n "${rcvar}" ]; then
2711.29Slukem			eval ${rcvar}=YES
2721.29Slukem		fi
2731.11Slukem		;;
2741.11Slukem	esac
2751.11Slukem
2761.16Slukem	_keywords="start stop restart rcvar $extra_commands"
2771.17Slukem	_pid=
2781.11Slukem	_pidcmd=
2791.24Slukem					# setup pid check command if not fast
2801.11Slukem	if [ -z "$_rc_fast_run" ]; then
2811.11Slukem		if [ -n "$pidfile" ]; then
2821.11Slukem			_pidcmd='_pid=`check_pidfile '$pidfile' '$command'`'
2831.11Slukem		elif [ -n "$command" ]; then
2841.11Slukem			_pidcmd='_pid=`check_process '$command'`'
2851.11Slukem		fi
2861.11Slukem		if [ -n "$_pidcmd" ]; then
2871.11Slukem			_keywords="${_keywords} status"
2881.11Slukem		fi
2891.11Slukem	fi
2901.11Slukem
2911.11Slukem	if [ -z "$_arg" ]; then
2921.11Slukem		rc_usage "$_keywords"
2931.11Slukem	fi
2941.11Slukem
2951.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
2961.11Slukem		_flags=$flags
2971.11Slukem	else
2981.11Slukem		eval _flags=\$${name}_flags
2991.11Slukem	fi
3001.18Slukem	eval _chdir=\$${name}_chdir
3011.22Slukem	eval _chroot=\$${name}_chroot
3021.18Slukem	eval _nice=\$${name}_nice
3031.18Slukem	eval _user=\$${name}_user
3041.22Slukem	eval _group=\$${name}_group
3051.22Slukem	eval _groups=\$${name}_groups
3061.11Slukem
3071.29Slukem					# if ${rcvar} is set, and $1 is not
3081.29Slukem					# "rcvar" or "status", then run
3091.29Slukem					#	checkyesno ${rcvar}
3101.29Slukem					# and return if that failed
3111.24Slukem					#
3121.29Slukem	if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then
3131.24Slukem		if ! checkyesno ${rcvar}; then
3141.24Slukem			return 0
3151.24Slukem		fi
3161.24Slukem	fi
3171.24Slukem
3181.24Slukem	eval $_pidcmd			# determine the pid if necessary
3191.11Slukem
3201.11Slukem	for _elem in $_keywords; do
3211.11Slukem		if [ "$_elem" != "$_arg" ]; then
3221.11Slukem			continue
3231.11Slukem		fi
3241.11Slukem
3251.24Slukem					# if there's a custom ${XXX_cmd},
3261.24Slukem					# run that instead of the default
3271.24Slukem					#
3281.11Slukem		eval _cmd=\$${_arg}_cmd
3291.11Slukem		eval _precmd=\$${_arg}_precmd
3301.11Slukem		if [ -n "$_cmd" ]; then
3311.24Slukem					# if the precmd failed and force
3321.24Slukem					# isn't set, exit
3331.24Slukem					#
3341.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
3351.24Slukem				return 1
3361.24Slukem			fi
3371.24Slukem
3381.11Slukem			eval $_cmd
3391.11Slukem			return 0
3401.11Slukem		fi
3411.11Slukem
3421.24Slukem		case "$_arg" in		# default operations...
3431.11Slukem
3441.11Slukem		status)
3451.11Slukem			if [ -n "$_pid" ]; then
3461.11Slukem				echo "${name} is running as pid $_pid."
3471.11Slukem			else
3481.11Slukem				echo "${name} is not running."
3491.28Slukem				return 1
3501.11Slukem			fi
3511.11Slukem			;;
3521.11Slukem
3531.11Slukem		start)
3541.11Slukem			if [ -n "$_pid" ]; then
3551.11Slukem				echo "${name} already running? (pid=$_pid)."
3561.11Slukem				exit 1
3571.11Slukem			fi
3581.11Slukem
3591.24Slukem			if [ ! -x $command ]; then
3601.11Slukem				return 0
3611.11Slukem			fi
3621.11Slukem
3631.24Slukem					# check for required variables,
3641.24Slukem					# directories, and files
3651.24Slukem					#
3661.11Slukem			for _f in $required_vars; do
3671.11Slukem				if ! checkyesno $_f; then
3681.24Slukem					warn "\$${_f} is not set."
3691.24Slukem					if [ -z "$_rc_force_run" ]; then
3701.24Slukem						return 1
3711.24Slukem					fi
3721.11Slukem				fi
3731.11Slukem			done
3741.11Slukem			for _f in $required_dirs; do
3751.11Slukem				if [ ! -d "${_f}/." ]; then
3761.25Slukem					warn "${_f} is not a directory."
3771.24Slukem					if [ -z "$_rc_force_run" ]; then
3781.24Slukem						return 1
3791.24Slukem					fi
3801.11Slukem				fi
3811.11Slukem			done
3821.11Slukem			for _f in $required_files; do
3831.11Slukem				if [ ! -r "${_f}" ]; then
3841.25Slukem					warn "${_f} is not readable."
3851.24Slukem					if [ -z "$_rc_force_run" ]; then
3861.24Slukem						return 1
3871.24Slukem					fi
3881.11Slukem				fi
3891.11Slukem			done
3901.11Slukem
3911.24Slukem					# if the precmd failed and force
3921.24Slukem					# isn't set, exit
3931.24Slukem					#
3941.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
3951.24Slukem				return 1
3961.24Slukem			fi
3971.24Slukem
3981.24Slukem
3991.24Slukem					# setup the command to run, and run it
4001.29Slukem					#
4011.11Slukem			echo "Starting ${name}."
4021.22Slukem			if [ -n "$_chroot" ]; then
4031.22Slukem				_doit="\
4041.23Slukem${_nice:+nice -n $_nice }\
4051.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
4061.22Slukem$_chroot $command $_flags $command_args"
4071.22Slukem			else
4081.22Slukem				_doit="\
4091.18Slukem${_user:+su -m $_user -c 'sh -c \"}\
4101.18Slukem${_chdir:+cd $_chdir; }\
4111.18Slukem${_nice:+nice -n $_nice }\
4121.18Slukem$command $_flags $command_args\
4131.18Slukem${_user:+\"'}"
4141.22Slukem			fi
4151.18Slukem			eval $_doit
4161.11Slukem			;;
4171.11Slukem
4181.11Slukem		stop)
4191.11Slukem			if [ -z "$_pid" ]; then
4201.24Slukem				if [ -n "$pidfile" ]; then
4211.24Slukem					echo \
4221.24Slukem				    "${name} not running? (check $pidfile)."
4231.24Slukem				else
4241.24Slukem					echo "${name} not running?"
4251.11Slukem				fi
4261.24Slukem				exit 1
4271.11Slukem			fi
4281.11Slukem
4291.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4301.24Slukem				return 1
4311.24Slukem			fi
4321.11Slukem			echo "Stopping ${name}."
4331.18Slukem			_doit=\
4341.18Slukem"${_user:+su -m $_user -c '}kill -${sig_stop:-TERM} $_pid${_user:+'}"
4351.18Slukem			eval $_doit
4361.11Slukem			;;
4371.11Slukem
4381.11Slukem		reload)
4391.11Slukem			if [ -z "$_pid" ]; then
4401.24Slukem				if [ -n "$pidfile" ]; then
4411.24Slukem					echo \
4421.11Slukem				    "${name} not running? (check $pidfile)."
4431.24Slukem				else
4441.24Slukem					echo "${name} not running?"
4451.11Slukem				fi
4461.24Slukem				exit 1
4471.11Slukem			fi
4481.11Slukem			echo "Reloading ${name} config files."
4491.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4501.24Slukem				return 1
4511.24Slukem			fi
4521.18Slukem			_doit=\
4531.18Slukem"${_user:+su -m $_user -c '}kill -${sig_reload:-HUP} $_pid${_user:+'}"
4541.18Slukem			eval $_doit
4551.11Slukem			;;
4561.11Slukem
4571.11Slukem		restart)
4581.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4591.24Slukem				return 1
4601.11Slukem			fi
4611.29Slukem					# prevent restart being called more
4621.29Slukem					# than once by any given script
4631.29Slukem					#
4641.29Slukem			if [ -n "$_rc_restart_done" ]; then
4651.29Slukem				return 0
4661.29Slukem			fi
4671.29Slukem			_rc_restart_done=YES
4681.27Slukem			( $0 ${_rc_force_run:+force}stop )
4691.11Slukem			sleep 1
4701.27Slukem			$0 ${_rc_force_run:+force}start
4711.11Slukem
4721.11Slukem			;;
4731.11Slukem
4741.11Slukem		rcvar)
4751.11Slukem			echo "# $name"
4761.24Slukem			if [ -n "$rcvar" ]; then
4771.24Slukem				if checkyesno ${rcvar}; then
4781.24Slukem					echo "\$${rcvar}=YES"
4791.24Slukem				else
4801.24Slukem					echo "\$${rcvar}=NO"
4811.24Slukem				fi
4821.11Slukem			fi
4831.11Slukem			;;
4841.11Slukem
4851.11Slukem		*)
4861.11Slukem			rc_usage "$_keywords"
4871.11Slukem			;;
4881.11Slukem
4891.11Slukem		esac
4901.11Slukem		return 0
4911.11Slukem	done
4921.11Slukem
4931.11Slukem	echo 1>&2 "$0: unknown directive '$_arg'."
4941.11Slukem	rc_usage "$_keywords"
4951.11Slukem	exit 1
4961.11Slukem}
4971.11Slukem
4981.11Slukem#
4991.11Slukem# run_rc_script file arg
5001.11Slukem#	Start the script `file' with `arg', and correctly handle the
5011.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
5021.17Slukem#	sourced into the current environment.  Otherwise it's run as
5031.11Slukem#	a child process.
5041.17Slukem#
5051.11Slukem#	Note: because `.sh' files are sourced into the current environment
5061.11Slukem#	run_rc_command shouldn't be used because its difficult to ensure
5071.11Slukem#	that the global variable state before and after the sourcing of 
5081.11Slukem#	the .sh file won't adversely affect other scripts.
5091.11Slukem#
5101.11Slukemrun_rc_script()
5111.11Slukem{
5121.11Slukem	_file=$1
5131.11Slukem	_arg=$2
5141.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
5151.11Slukem		err 3 'USAGE: run_rc_script file arg'
5161.11Slukem	fi
5171.11Slukem
5181.11Slukem	case "$_file" in
5191.17Slukem	*.sh)				# run in current shell
5201.11Slukem		set $_arg ; . $_file
5211.11Slukem		;;
5221.17Slukem	*)				# run in subshell
5231.11Slukem		( set $_arg ; . $_file )
5241.11Slukem		;;
5251.11Slukem	esac
5261.11Slukem}
5271.19Slukem
5281.19Slukem#
5291.19Slukem# load_rc_config
5301.19Slukem#	Source in the configuration file for a given command.
5311.19Slukem#
5321.19Slukemload_rc_config()
5331.19Slukem{
5341.19Slukem	_command=$1
5351.19Slukem	if [ -z "$_command" ]; then
5361.19Slukem		err 3 'USAGE: load_rc_config command'
5371.19Slukem	fi
5381.19Slukem
5391.19Slukem	. /etc/rc.conf
5401.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
5411.20Sfvdl		. /etc/rc.conf.d/"$_command"
5421.20Sfvdl	fi
5431.19Slukem}
5441.19Slukem
5451.11Slukem
5461.11Slukem#
5471.11Slukem# rc_usage commands
5481.11Slukem#	Print a usage string for $0, with `commands' being a list of
5491.11Slukem#	valid commands.
5501.11Slukem#
5511.11Slukemrc_usage()
5521.11Slukem{
5531.11Slukem	echo -n 1>&2 "Usage: $0 [fast|force]("
5541.11Slukem
5551.11Slukem	_sep=
5561.11Slukem	for _elem in $*; do
5571.11Slukem		echo -n 1>&2 "$_sep$_elem"
5581.11Slukem		_sep="|"
5591.11Slukem	done
5601.11Slukem	echo 1>&2 ")"
5611.11Slukem	exit 1
5621.11Slukem}
5631.11Slukem
5641.11Slukem#
5651.11Slukem# err exitval message
5661.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
5671.11Slukem#
5681.11Slukemerr()
5691.11Slukem{
5701.11Slukem	exitval=$1
5711.11Slukem	shift
5721.11Slukem
5731.21Slukem	logger "$0: ERROR: $*"
5741.21Slukem	echo 1>&2 "$0: ERROR: $*"
5751.11Slukem	exit $exitval
5761.11Slukem}
5771.11Slukem
5781.11Slukem#
5791.11Slukem# warn message
5801.11Slukem#	Display message to stderr and log to the syslog.
5811.11Slukem#
5821.11Slukemwarn()
5831.11Slukem{
5841.21Slukem	logger "$0: WARNING: $*"
5851.21Slukem	echo 1>&2 "$0: WARNING: $*"
5861.1Scjs}
587