rc.subr revision 1.37
11.37Slukem# $NetBSD: rc.subr,v 1.37 2001/06/18 06:53:45 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.30Slukem		case "$_npid" in
1211.30Slukem		    PID)
1221.30Slukem			continue ;;
1231.30Slukem		esac
1241.30Slukem		case "$_arg0" in
1251.30Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")
1261.11Slukem			echo $_npid
1271.11Slukem			return
1281.30Slukem			;;
1291.30Slukem		esac
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.30Slukem		case "$_npid" in
1481.30Slukem		    PID)
1491.30Slukem			continue ;;
1501.30Slukem		esac
1511.30Slukem		case "$_arg0" in
1521.30Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")
1531.11Slukem			echo -n "$_pref$_npid"
1541.11Slukem			_pref=" "
1551.30Slukem			;;
1561.30Slukem		esac
1571.11Slukem	done
1581.11Slukem}
1591.11Slukem
1601.11Slukem#
1611.33Slukem# wait_for_pids pid [pid ...]
1621.35Slukem#	spins until none of the pids exist
1631.33Slukem#
1641.33Slukemwait_for_pids()
1651.33Slukem{
1661.33Slukem	_list=$*
1671.33Slukem	if [ -z "$_list" ]; then
1681.33Slukem		return
1691.33Slukem	fi
1701.35Slukem	_prefix=
1711.35Slukem	while true; do
1721.33Slukem		_nlist="";
1731.33Slukem		for _j in $_list; do
1741.33Slukem			if kill -0 $_j 2>/dev/null; then
1751.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
1761.33Slukem			fi
1771.33Slukem		done
1781.33Slukem		if [ -z "$_nlist" ]; then
1791.33Slukem			break
1801.33Slukem		fi
1811.33Slukem		_list=$_nlist
1821.35Slukem		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
1831.35Slukem		_prefix=", "
1841.35Slukem		sleep 2
1851.33Slukem	done
1861.35Slukem	if [ -n "$_prefix" ]; then
1871.35Slukem		echo "."
1881.35Slukem	fi
1891.33Slukem}
1901.33Slukem
1911.33Slukem#
1921.16Slukem# run_rc_command arg
1931.16Slukem#	Search for arg in the list of supported commands, which is:
1941.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
1951.11Slukem#	If there's a match, run ${arg}_cmd or the default command (see below).
1961.11Slukem#
1971.16Slukem#	If arg has a given prefix, then change the operation as follows:
1981.11Slukem#		prefix	operation
1991.11Slukem#		------	---------
2001.11Slukem#		fast	Skip the pid check.
2011.11Slukem#		force	Set ${rcvar} to YES.
2021.11Slukem#
2031.11Slukem#	The following globals are used:
2041.24Slukem#
2051.11Slukem#	name		needed	function
2061.11Slukem#	----		------	--------
2071.11Slukem#	name		y	Name of script.
2081.24Slukem#
2091.11Slukem#	command		n	Full path to command.
2101.11Slukem#				Not needed if ${arg}_cmd is set for
2111.11Slukem#				each keyword.
2121.24Slukem#
2131.11Slukem#	command_args	n	Optional args/shell directives for command.
2141.24Slukem#
2151.16Slukem#	extra_commands	n	List of extra commands supported.
2161.24Slukem#
2171.11Slukem#	pidfile		n	If set, use check_pidfile $pidfile, else if
2181.11Slukem#				$command is set, use check_process $command.
2191.24Slukem#
2201.24Slukem#	rcvar		n	This is checked with checkyesno to determine
2211.24Slukem#				if the action should be run.
2221.24Slukem#
2231.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
2241.24Slukem#
2251.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
2261.22Slukem#				(if not using ${name}_chroot).
2271.24Slukem#
2281.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
2291.24Slukem#				NOTE:	$flags from the parent environment
2301.24Slukem#					can be used to override this.
2311.24Slukem#
2321.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
2331.24Slukem#
2341.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
2351.22Slukem#				using ${name}_chroot.
2361.24Slukem#
2371.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
2381.24Slukem#
2391.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
2401.32Slukem#				to run the chrooted ${command} with.
2411.24Slukem#
2421.11Slukem#	${_arg}_cmd	n	If set, use this as the action when invoked;
2431.11Slukem#				$_arg is available to the action to use.
2441.11Slukem#				Otherwise, use default command (see below)
2451.24Slukem#
2461.11Slukem#	${_arg}_precmd	n	If set, run just before performing the main
2471.11Slukem#				action in the default command (i.e, after
2481.11Slukem#				checking for required bits and process
2491.11Slukem#				(non)existance).
2501.11Slukem#				If this completes with a non-zero exit code,
2511.11Slukem#				don't run ${_arg}_cmd.
2521.24Slukem#
2531.11Slukem#	required_dirs	n	If set, check for the existence of the given
2541.11Slukem#				directories before running the default
2551.11Slukem#				(re)start command.
2561.24Slukem#
2571.11Slukem#	required_files	n	If set, check for the readability of the given
2581.11Slukem#				files before running the default (re)start
2591.11Slukem#				command.
2601.24Slukem#
2611.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
2621.11Slukem#				listed variables before running the default
2631.11Slukem#				(re)start command.
2641.11Slukem#
2651.11Slukem#	Default commands for a given arg:
2661.24Slukem#
2671.11Slukem#	arg		default
2681.11Slukem#	---		-------
2691.11Slukem#	start		if !running && checkyesno ${rcvar}
2701.11Slukem#				${command}
2711.24Slukem#
2721.11Slukem#	stop		if ${pidfile}
2731.35Slukem#				_pid=`check_pidfile $pidfile`
2741.11Slukem#			else
2751.35Slukem#				_pid=`check_process $command`
2761.35Slukem#			kill $sig_stop $_pid
2771.35Slukem#			wait_for_pids $_pid
2781.35Slukem#			($sig_stop defaults to TERM.)
2791.24Slukem#
2801.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
2811.35Slukem#			and doesn't wait_for_pids.
2821.11Slukem#			$sig_reload defaults to HUP.
2831.24Slukem#
2841.11Slukem#	restart		Run `stop' then `start'.
2851.11Slukem#
2861.33Slukem#	status		Show if ${command} is running, etc.
2871.33Slukem#
2881.33Slukem#	poll		Wait for ${command} to exit.
2891.33Slukem#
2901.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
2911.33Slukem#
2921.33Slukem#
2931.24Slukem#
2941.11Slukemrun_rc_command()
2951.11Slukem{
2961.11Slukem	_arg=$1
2971.24Slukem	if [ -z "$name" ]; then
2981.30Slukem		err 3 'run_rc_command: $name is not set.'
2991.11Slukem	fi
3001.11Slukem
3011.11Slukem	case "$_arg" in
3021.24Slukem	fast*)				# "fast" prefix; don't check pid
3031.11Slukem		_arg=${_arg#fast}
3041.11Slukem		_rc_fast_run=YES
3051.11Slukem		;;
3061.24Slukem	force*)				# "force prefix; always start
3071.11Slukem		_arg=${_arg#force}
3081.24Slukem		_rc_force_run=YES
3091.29Slukem		if [ -n "${rcvar}" ]; then
3101.29Slukem			eval ${rcvar}=YES
3111.29Slukem		fi
3121.11Slukem		;;
3131.11Slukem	esac
3141.11Slukem
3151.16Slukem	_keywords="start stop restart rcvar $extra_commands"
3161.17Slukem	_pid=
3171.11Slukem	_pidcmd=
3181.24Slukem					# setup pid check command if not fast
3191.11Slukem	if [ -z "$_rc_fast_run" ]; then
3201.11Slukem		if [ -n "$pidfile" ]; then
3211.11Slukem			_pidcmd='_pid=`check_pidfile '$pidfile' '$command'`'
3221.11Slukem		elif [ -n "$command" ]; then
3231.11Slukem			_pidcmd='_pid=`check_process '$command'`'
3241.11Slukem		fi
3251.11Slukem		if [ -n "$_pidcmd" ]; then
3261.33Slukem			_keywords="${_keywords} status poll"
3271.11Slukem		fi
3281.11Slukem	fi
3291.11Slukem
3301.11Slukem	if [ -z "$_arg" ]; then
3311.11Slukem		rc_usage "$_keywords"
3321.11Slukem	fi
3331.11Slukem
3341.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
3351.11Slukem		_flags=$flags
3361.11Slukem	else
3371.11Slukem		eval _flags=\$${name}_flags
3381.11Slukem	fi
3391.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
3401.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
3411.30Slukem	    _group=\$${name}_group	_groups=\$${name}_groups
3421.11Slukem
3431.29Slukem					# if ${rcvar} is set, and $1 is not
3441.29Slukem					# "rcvar" or "status", then run
3451.29Slukem					#	checkyesno ${rcvar}
3461.29Slukem					# and return if that failed
3471.24Slukem					#
3481.30Slukem	# XXXX use case?
3491.29Slukem	if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then
3501.24Slukem		if ! checkyesno ${rcvar}; then
3511.24Slukem			return 0
3521.24Slukem		fi
3531.24Slukem	fi
3541.24Slukem
3551.24Slukem	eval $_pidcmd			# determine the pid if necessary
3561.11Slukem
3571.11Slukem	for _elem in $_keywords; do
3581.11Slukem		if [ "$_elem" != "$_arg" ]; then
3591.11Slukem			continue
3601.11Slukem		fi
3611.11Slukem
3621.24Slukem					# if there's a custom ${XXX_cmd},
3631.24Slukem					# run that instead of the default
3641.24Slukem					#
3651.30Slukem		eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd
3661.11Slukem		if [ -n "$_cmd" ]; then
3671.24Slukem					# if the precmd failed and force
3681.24Slukem					# isn't set, exit
3691.24Slukem					#
3701.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
3711.24Slukem				return 1
3721.24Slukem			fi
3731.24Slukem
3741.11Slukem			eval $_cmd
3751.11Slukem			return 0
3761.11Slukem		fi
3771.11Slukem
3781.24Slukem		case "$_arg" in		# default operations...
3791.11Slukem
3801.11Slukem		status)
3811.11Slukem			if [ -n "$_pid" ]; then
3821.11Slukem				echo "${name} is running as pid $_pid."
3831.11Slukem			else
3841.11Slukem				echo "${name} is not running."
3851.28Slukem				return 1
3861.11Slukem			fi
3871.11Slukem			;;
3881.11Slukem
3891.11Slukem		start)
3901.11Slukem			if [ -n "$_pid" ]; then
3911.11Slukem				echo "${name} already running? (pid=$_pid)."
3921.11Slukem				exit 1
3931.11Slukem			fi
3941.11Slukem
3951.24Slukem			if [ ! -x $command ]; then
3961.11Slukem				return 0
3971.11Slukem			fi
3981.11Slukem
3991.24Slukem					# check for required variables,
4001.24Slukem					# directories, and files
4011.24Slukem					#
4021.11Slukem			for _f in $required_vars; do
4031.11Slukem				if ! checkyesno $_f; then
4041.24Slukem					warn "\$${_f} is not set."
4051.24Slukem					if [ -z "$_rc_force_run" ]; then
4061.24Slukem						return 1
4071.24Slukem					fi
4081.11Slukem				fi
4091.11Slukem			done
4101.11Slukem			for _f in $required_dirs; do
4111.11Slukem				if [ ! -d "${_f}/." ]; then
4121.25Slukem					warn "${_f} is not a directory."
4131.24Slukem					if [ -z "$_rc_force_run" ]; then
4141.24Slukem						return 1
4151.24Slukem					fi
4161.11Slukem				fi
4171.11Slukem			done
4181.11Slukem			for _f in $required_files; do
4191.11Slukem				if [ ! -r "${_f}" ]; then
4201.25Slukem					warn "${_f} is not readable."
4211.24Slukem					if [ -z "$_rc_force_run" ]; then
4221.24Slukem						return 1
4231.24Slukem					fi
4241.11Slukem				fi
4251.11Slukem			done
4261.11Slukem
4271.24Slukem					# if the precmd failed and force
4281.24Slukem					# isn't set, exit
4291.24Slukem					#
4301.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4311.24Slukem				return 1
4321.24Slukem			fi
4331.24Slukem
4341.24Slukem
4351.24Slukem					# setup the command to run, and run it
4361.29Slukem					#
4371.11Slukem			echo "Starting ${name}."
4381.22Slukem			if [ -n "$_chroot" ]; then
4391.22Slukem				_doit="\
4401.23Slukem${_nice:+nice -n $_nice }\
4411.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
4421.22Slukem$_chroot $command $_flags $command_args"
4431.22Slukem			else
4441.22Slukem				_doit="\
4451.18Slukem${_chdir:+cd $_chdir; }\
4461.18Slukem${_nice:+nice -n $_nice }\
4471.34Slukem$command $_flags $command_args"
4481.34Slukem				if [ -n "$_user" ]; then
4491.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
4501.34Slukem				fi
4511.22Slukem			fi
4521.18Slukem			eval $_doit
4531.11Slukem			;;
4541.11Slukem
4551.11Slukem		stop)
4561.11Slukem			if [ -z "$_pid" ]; then
4571.24Slukem				if [ -n "$pidfile" ]; then
4581.24Slukem					echo \
4591.24Slukem				    "${name} not running? (check $pidfile)."
4601.24Slukem				else
4611.24Slukem					echo "${name} not running?"
4621.11Slukem				fi
4631.24Slukem				exit 1
4641.11Slukem			fi
4651.11Slukem
4661.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4671.24Slukem				return 1
4681.24Slukem			fi
4691.11Slukem			echo "Stopping ${name}."
4701.34Slukem			_doit="kill -${sig_stop:-TERM} $_pid"
4711.34Slukem			if [ -n "$_user" ]; then
4721.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
4731.34Slukem			fi
4741.18Slukem			eval $_doit
4751.35Slukem			wait_for_pids $_pid
4761.11Slukem			;;
4771.11Slukem
4781.11Slukem		reload)
4791.11Slukem			if [ -z "$_pid" ]; then
4801.24Slukem				if [ -n "$pidfile" ]; then
4811.24Slukem					echo \
4821.11Slukem				    "${name} not running? (check $pidfile)."
4831.24Slukem				else
4841.24Slukem					echo "${name} not running?"
4851.11Slukem				fi
4861.24Slukem				exit 1
4871.11Slukem			fi
4881.11Slukem			echo "Reloading ${name} config files."
4891.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4901.24Slukem				return 1
4911.24Slukem			fi
4921.34Slukem			_doit="kill -${sig_reload:-HUP} $_pid"
4931.34Slukem			if [ -n "$_user" ]; then
4941.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
4951.34Slukem			fi
4961.18Slukem			eval $_doit
4971.11Slukem			;;
4981.11Slukem
4991.11Slukem		restart)
5001.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5011.24Slukem				return 1
5021.11Slukem			fi
5031.29Slukem					# prevent restart being called more
5041.29Slukem					# than once by any given script
5051.29Slukem					#
5061.29Slukem			if [ -n "$_rc_restart_done" ]; then
5071.29Slukem				return 0
5081.29Slukem			fi
5091.29Slukem			_rc_restart_done=YES
5101.33Slukem
5111.27Slukem			( $0 ${_rc_force_run:+force}stop )
5121.27Slukem			$0 ${_rc_force_run:+force}start
5131.11Slukem
5141.33Slukem			;;
5151.33Slukem
5161.33Slukem		poll)
5171.33Slukem			if [ -n "$_pid" ]; then
5181.33Slukem				wait_for_pids $_pid
5191.33Slukem			fi
5201.11Slukem			;;
5211.11Slukem
5221.11Slukem		rcvar)
5231.11Slukem			echo "# $name"
5241.24Slukem			if [ -n "$rcvar" ]; then
5251.24Slukem				if checkyesno ${rcvar}; then
5261.24Slukem					echo "\$${rcvar}=YES"
5271.24Slukem				else
5281.24Slukem					echo "\$${rcvar}=NO"
5291.24Slukem				fi
5301.11Slukem			fi
5311.11Slukem			;;
5321.11Slukem
5331.11Slukem		*)
5341.11Slukem			rc_usage "$_keywords"
5351.11Slukem			;;
5361.11Slukem
5371.11Slukem		esac
5381.11Slukem		return 0
5391.11Slukem	done
5401.11Slukem
5411.11Slukem	echo 1>&2 "$0: unknown directive '$_arg'."
5421.11Slukem	rc_usage "$_keywords"
5431.11Slukem	exit 1
5441.11Slukem}
5451.11Slukem
5461.11Slukem#
5471.11Slukem# run_rc_script file arg
5481.11Slukem#	Start the script `file' with `arg', and correctly handle the
5491.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
5501.37Slukem#	sourced into the current environment.  If `file' appears to be
5511.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
5521.37Slukem#	executable run as a child process.
5531.17Slukem#
5541.11Slukem#	Note: because `.sh' files are sourced into the current environment
5551.37Slukem#	run_rc_command() shouldn't be used because its difficult to ensure
5561.11Slukem#	that the global variable state before and after the sourcing of 
5571.11Slukem#	the .sh file won't adversely affect other scripts.
5581.11Slukem#
5591.11Slukemrun_rc_script()
5601.11Slukem{
5611.11Slukem	_file=$1
5621.11Slukem	_arg=$2
5631.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
5641.11Slukem		err 3 'USAGE: run_rc_script file arg'
5651.11Slukem	fi
5661.11Slukem
5671.30Slukem	if [ -n "$rc_fast_and_loose" ]; then
5681.30Slukem		unset name command command_args extra_commands pidfile rcvar
5691.30Slukem		unset required_dirs required_files required_vars
5701.30Slukem		eval unset ${_arg}_cmd ${_arg}_precmd
5711.11Slukem		set $_arg ; . $_file
5721.30Slukem	else
5731.30Slukem		case "$_file" in
5741.30Slukem		*.sh)				# run in current shell
5751.30Slukem			set $_arg ; . $_file
5761.30Slukem			;;
5771.37Slukem		*[~#]|*.OLD|*.orig)		# scratch file; skip
5781.37Slukem			warn "Ignoring scratch file $_file"
5791.37Slukem			;;
5801.30Slukem		*)				# run in subshell
5811.37Slukem			if [ -x $_file ]; then
5821.37Slukem				( set $_arg ; . $_file )
5831.37Slukem			fi
5841.30Slukem			;;
5851.30Slukem		esac
5861.30Slukem	fi
5871.11Slukem}
5881.19Slukem
5891.19Slukem#
5901.19Slukem# load_rc_config
5911.19Slukem#	Source in the configuration file for a given command.
5921.19Slukem#
5931.19Slukemload_rc_config()
5941.19Slukem{
5951.19Slukem	_command=$1
5961.19Slukem	if [ -z "$_command" ]; then
5971.19Slukem		err 3 'USAGE: load_rc_config command'
5981.19Slukem	fi
5991.19Slukem
6001.30Slukem	if [ -z "$_rc_conf_loaded" ]; then
6011.30Slukem		. /etc/rc.conf
6021.30Slukem		_rc_conf_loaded=YES
6031.30Slukem	fi
6041.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
6051.20Sfvdl		. /etc/rc.conf.d/"$_command"
6061.20Sfvdl	fi
6071.19Slukem}
6081.19Slukem
6091.11Slukem
6101.11Slukem#
6111.11Slukem# rc_usage commands
6121.11Slukem#	Print a usage string for $0, with `commands' being a list of
6131.11Slukem#	valid commands.
6141.11Slukem#
6151.11Slukemrc_usage()
6161.11Slukem{
6171.11Slukem	echo -n 1>&2 "Usage: $0 [fast|force]("
6181.11Slukem
6191.11Slukem	_sep=
6201.11Slukem	for _elem in $*; do
6211.11Slukem		echo -n 1>&2 "$_sep$_elem"
6221.11Slukem		_sep="|"
6231.11Slukem	done
6241.11Slukem	echo 1>&2 ")"
6251.11Slukem	exit 1
6261.11Slukem}
6271.11Slukem
6281.11Slukem#
6291.11Slukem# err exitval message
6301.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
6311.11Slukem#
6321.11Slukemerr()
6331.11Slukem{
6341.11Slukem	exitval=$1
6351.11Slukem	shift
6361.11Slukem
6371.21Slukem	logger "$0: ERROR: $*"
6381.21Slukem	echo 1>&2 "$0: ERROR: $*"
6391.11Slukem	exit $exitval
6401.11Slukem}
6411.11Slukem
6421.11Slukem#
6431.11Slukem# warn message
6441.11Slukem#	Display message to stderr and log to the syslog.
6451.11Slukem#
6461.11Slukemwarn()
6471.11Slukem{
6481.21Slukem	logger "$0: WARNING: $*"
6491.21Slukem	echo 1>&2 "$0: WARNING: $*"
6501.31Satatat}
6511.31Satatat
6521.31Satatat#
6531.31Satatat# backup_file action file cur backup
6541.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
6551.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
6561.31Satatat#
6571.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
6581.31Satatat#	which can be either YES or NO.
6591.31Satatat#
6601.31Satatat#	The `action' keyword can be one of the following:
6611.31Satatat#
6621.31Satatat#	add		`file' is now being backed up (and is possibly
6631.31Satatat#			being reentered into the backups system).  `cur'
6641.31Satatat#			is created and RCS files, if necessary, are
6651.31Satatat#			created as well.
6661.31Satatat#
6671.31Satatat#	update		`file' has changed and needs to be backed up.
6681.31Satatat#			If `cur' exists, it is copied to to `back' or
6691.31Satatat#			checked into RCS (if the repository file is old),
6701.31Satatat#			and then `file' is copied to `cur'.  Another RCS
6711.31Satatat#			check in done here if RCS is being used.
6721.31Satatat#
6731.31Satatat#	remove		`file' is no longer being tracked by the backups
6741.31Satatat#			system.  If RCS is not being used, `cur' is moved
6751.31Satatat#			to `back', otherwise an empty file is checked in,
6761.31Satatat#			and then `cur' is removed.
6771.31Satatat#
6781.31Satatat#
6791.31Satatatbackup_file()
6801.31Satatat{
6811.31Satatat	_action=$1
6821.31Satatat	_file=$2
6831.31Satatat	_cur=$3
6841.31Satatat	_back=$4
6851.31Satatat
6861.31Satatat	if checkyesno backup_uses_rcs; then
6871.31Satatat		_msg0="backup archive"
6881.31Satatat		_msg1="update"
6891.31Satatat
6901.36Satatat		# ensure that history file is not locked
6911.36Satatat		if [ -f $_cur,v ]; then
6921.36Satatat			rcs -q -u -U -M $_cur
6931.36Satatat		fi
6941.36Satatat
6951.31Satatat		# ensure after switching to rcs that the
6961.31Satatat		# current backup is not lost
6971.31Satatat		if [ -f $_cur ]; then
6981.31Satatat			# no archive, or current newer than archive
6991.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
7001.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7011.36Satatat				rcs -q -kb -U $_cur
7021.31Satatat			fi
7031.31Satatat		fi
7041.31Satatat
7051.31Satatat		case $_action in
7061.31Satatat		add|update)
7071.31Satatat			cp -p $_file $_cur
7081.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7091.36Satatat			rcs -q -kb -U $_cur
7101.31Satatat			chown root:wheel $_cur $_cur,v
7111.31Satatat			;;
7121.31Satatat		remove)
7131.31Satatat			cp /dev/null $_cur
7141.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7151.36Satatat			rcs -q -kb -U $_cur
7161.31Satatat			chown root:wheel $_cur $_cur,v
7171.31Satatat			rm $_cur
7181.31Satatat			;;
7191.31Satatat		esac
7201.31Satatat	else
7211.31Satatat		case $_action in
7221.31Satatat		add|update)
7231.31Satatat			if [ -f $_cur ]; then
7241.31Satatat				cp -p $_cur $_back
7251.31Satatat			fi
7261.31Satatat			cp -p $_file $_cur
7271.31Satatat			chown root:wheel $_cur
7281.31Satatat			;;
7291.31Satatat		remove)
7301.31Satatat			mv -f $_cur $_back
7311.31Satatat			;;
7321.31Satatat		esac
7331.31Satatat	fi
7341.1Scjs}
735