rc.subr revision 1.42
11.42Slukem# $NetBSD: rc.subr,v 1.42 2002/02/25 12:49:34 lukem Exp $
21.11Slukem#
31.41Slukem# Copyright (c) 1997-2002 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.38Slukem}
701.38Slukem
711.38Slukem# reverse_list list
721.38Slukem#	print the list in reverse order
731.38Slukem#
741.38Slukemreverse_list()
751.38Slukem{
761.38Slukem	_revlist=
771.38Slukem	for _revfile in $*; do
781.38Slukem		_revlist="$_revfile $_revlist"
791.38Slukem	done
801.38Slukem	echo $_revlist
811.6Smellon}
821.6Smellon
831.6Smellon#
841.6Smellon# mount_critical_filesystems
851.6Smellon#	Go through the list of critical filesystems, checking each one
861.6Smellon#	to see if it is mounted, and if it is not, mounting it.
871.6Smellon#
881.11Slukemmount_critical_filesystems()
891.11Slukem{
901.10Sdrochner	if [ $1 = local ]; then
911.10Sdrochner		_fslist=$critical_filesystems_beforenet
921.10Sdrochner	else
931.10Sdrochner		_fslist=$critical_filesystems
941.10Sdrochner	fi
951.17Slukem	for _fs in $_fslist; do
961.6Smellon		mount | (
971.17Slukem			_ismounted=no
981.6Smellon			while read what _on on _type type; do
991.17Slukem				if [ $on = $_fs ]; then
1001.17Slukem					_ismounted=yes
1011.6Smellon				fi
1021.6Smellon			done
1031.17Slukem			if [ $_ismounted = no ]; then 
1041.17Slukem				mount $_fs >/dev/null 2>&1
1051.6Smellon			fi
1061.6Smellon		)  
1071.6Smellon	done
1081.7Scjs}
1091.7Scjs
1101.11Slukem#
1111.11Slukem# check_pidfile pidfile procname
1121.11Slukem#	Parses the first line of pidfile for a pid, and ensures
1131.11Slukem#	that the process is running and matches procname.
1141.11Slukem#	Prints the matching pid upon success, nothing otherwise.
1151.11Slukem#
1161.11Slukemcheck_pidfile()
1171.11Slukem{
1181.11Slukem	_pidfile=$1
1191.11Slukem	_procname=$2
1201.11Slukem	if [ -z "$_pidfile" -o -z "$_procname" ]; then
1211.11Slukem		err 3 'USAGE: check_pidfile pidfile procname'
1221.11Slukem	fi
1231.11Slukem	if [ ! -f $_pidfile ]; then
1241.11Slukem		return
1251.11Slukem	fi
1261.11Slukem	read _pid _junk < $_pidfile
1271.11Slukem	if [ -z "$_pid" ]; then
1281.11Slukem		return
1291.11Slukem	fi
1301.26Slukem	_procnamebn=${_procname##*/}
1311.11Slukem	ps -p $_pid -o 'pid,command' | while read _npid _arg0 _argv; do
1321.30Slukem		case "$_npid" in
1331.30Slukem		    PID)
1341.30Slukem			continue ;;
1351.30Slukem		esac
1361.30Slukem		case "$_arg0" in
1371.30Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")
1381.11Slukem			echo $_npid
1391.11Slukem			return
1401.30Slukem			;;
1411.30Slukem		esac
1421.11Slukem	done
1431.11Slukem}
1441.11Slukem
1451.11Slukem#
1461.11Slukem# check_process procname
1471.11Slukem#	Ensures that a process (or processes) named procname is running.
1481.11Slukem#	Prints a list of matching pids.
1491.11Slukem#
1501.11Slukemcheck_process()
1511.11Slukem{
1521.11Slukem	_procname=$1
1531.11Slukem	if [ -z "$_procname" ]; then
1541.11Slukem		err 3 'USAGE: check_process procname'
1551.11Slukem	fi
1561.26Slukem	_procnamebn=${_procname##*/}
1571.11Slukem	_pref=
1581.11Slukem	ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do
1591.30Slukem		case "$_npid" in
1601.30Slukem		    PID)
1611.30Slukem			continue ;;
1621.30Slukem		esac
1631.30Slukem		case "$_arg0" in
1641.30Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")
1651.11Slukem			echo -n "$_pref$_npid"
1661.11Slukem			_pref=" "
1671.30Slukem			;;
1681.30Slukem		esac
1691.11Slukem	done
1701.11Slukem}
1711.11Slukem
1721.11Slukem#
1731.33Slukem# wait_for_pids pid [pid ...]
1741.35Slukem#	spins until none of the pids exist
1751.33Slukem#
1761.33Slukemwait_for_pids()
1771.33Slukem{
1781.33Slukem	_list=$*
1791.33Slukem	if [ -z "$_list" ]; then
1801.33Slukem		return
1811.33Slukem	fi
1821.35Slukem	_prefix=
1831.35Slukem	while true; do
1841.33Slukem		_nlist="";
1851.33Slukem		for _j in $_list; do
1861.33Slukem			if kill -0 $_j 2>/dev/null; then
1871.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
1881.33Slukem			fi
1891.33Slukem		done
1901.33Slukem		if [ -z "$_nlist" ]; then
1911.33Slukem			break
1921.33Slukem		fi
1931.33Slukem		_list=$_nlist
1941.35Slukem		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
1951.35Slukem		_prefix=", "
1961.35Slukem		sleep 2
1971.33Slukem	done
1981.35Slukem	if [ -n "$_prefix" ]; then
1991.35Slukem		echo "."
2001.35Slukem	fi
2011.33Slukem}
2021.33Slukem
2031.33Slukem#
2041.16Slukem# run_rc_command arg
2051.16Slukem#	Search for arg in the list of supported commands, which is:
2061.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
2071.11Slukem#	If there's a match, run ${arg}_cmd or the default command (see below).
2081.11Slukem#
2091.16Slukem#	If arg has a given prefix, then change the operation as follows:
2101.11Slukem#		prefix	operation
2111.11Slukem#		------	---------
2121.11Slukem#		fast	Skip the pid check.
2131.11Slukem#		force	Set ${rcvar} to YES.
2141.11Slukem#
2151.11Slukem#	The following globals are used:
2161.24Slukem#
2171.11Slukem#	name		needed	function
2181.11Slukem#	----		------	--------
2191.11Slukem#	name		y	Name of script.
2201.24Slukem#
2211.11Slukem#	command		n	Full path to command.
2221.11Slukem#				Not needed if ${arg}_cmd is set for
2231.11Slukem#				each keyword.
2241.24Slukem#
2251.11Slukem#	command_args	n	Optional args/shell directives for command.
2261.24Slukem#
2271.16Slukem#	extra_commands	n	List of extra commands supported.
2281.24Slukem#
2291.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
2301.42Slukem#				otherwise use check_process $command.
2311.42Slukem#				In either case, only check if $command is set.
2321.42Slukem#
2331.42Slukem#	procname	n	Process name to check for instead of $command.
2341.24Slukem#
2351.24Slukem#	rcvar		n	This is checked with checkyesno to determine
2361.24Slukem#				if the action should be run.
2371.24Slukem#
2381.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
2391.42Slukem#				Requires /usr to be mounted.
2401.24Slukem#
2411.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
2421.22Slukem#				(if not using ${name}_chroot).
2431.24Slukem#
2441.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
2451.24Slukem#				NOTE:	$flags from the parent environment
2461.24Slukem#					can be used to override this.
2471.24Slukem#
2481.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
2491.24Slukem#
2501.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
2511.22Slukem#				using ${name}_chroot.
2521.42Slukem#				Requires /usr to be mounted.
2531.24Slukem#
2541.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
2551.42Slukem#				Requires /usr to be mounted.
2561.24Slukem#
2571.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
2581.32Slukem#				to run the chrooted ${command} with.
2591.42Slukem#				Requires /usr to be mounted.
2601.24Slukem#
2611.11Slukem#	${_arg}_cmd	n	If set, use this as the action when invoked;
2621.11Slukem#				$_arg is available to the action to use.
2631.11Slukem#				Otherwise, use default command (see below)
2641.24Slukem#
2651.11Slukem#	${_arg}_precmd	n	If set, run just before performing the main
2661.11Slukem#				action in the default command (i.e, after
2671.11Slukem#				checking for required bits and process
2681.41Slukem#				(non)existence).
2691.11Slukem#				If this completes with a non-zero exit code,
2701.11Slukem#				don't run ${_arg}_cmd.
2711.24Slukem#
2721.11Slukem#	required_dirs	n	If set, check for the existence of the given
2731.11Slukem#				directories before running the default
2741.11Slukem#				(re)start command.
2751.24Slukem#
2761.11Slukem#	required_files	n	If set, check for the readability of the given
2771.11Slukem#				files before running the default (re)start
2781.11Slukem#				command.
2791.24Slukem#
2801.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
2811.11Slukem#				listed variables before running the default
2821.11Slukem#				(re)start command.
2831.11Slukem#
2841.11Slukem#	Default commands for a given arg:
2851.24Slukem#
2861.11Slukem#	arg		default
2871.11Slukem#	---		-------
2881.11Slukem#	start		if !running && checkyesno ${rcvar}
2891.11Slukem#				${command}
2901.24Slukem#
2911.11Slukem#	stop		if ${pidfile}
2921.42Slukem#				_pid=$(check_pidfile $pidfile $command)
2931.11Slukem#			else
2941.42Slukem#				_pid=$(check_process $command)
2951.35Slukem#			kill $sig_stop $_pid
2961.35Slukem#			wait_for_pids $_pid
2971.35Slukem#			($sig_stop defaults to TERM.)
2981.24Slukem#
2991.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
3001.35Slukem#			and doesn't wait_for_pids.
3011.11Slukem#			$sig_reload defaults to HUP.
3021.24Slukem#
3031.11Slukem#	restart		Run `stop' then `start'.
3041.11Slukem#
3051.33Slukem#	status		Show if ${command} is running, etc.
3061.33Slukem#
3071.33Slukem#	poll		Wait for ${command} to exit.
3081.33Slukem#
3091.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
3101.33Slukem#
3111.33Slukem#
3121.24Slukem#
3131.11Slukemrun_rc_command()
3141.11Slukem{
3151.11Slukem	_arg=$1
3161.24Slukem	if [ -z "$name" ]; then
3171.30Slukem		err 3 'run_rc_command: $name is not set.'
3181.11Slukem	fi
3191.11Slukem
3201.11Slukem	case "$_arg" in
3211.24Slukem	fast*)				# "fast" prefix; don't check pid
3221.11Slukem		_arg=${_arg#fast}
3231.11Slukem		_rc_fast_run=YES
3241.11Slukem		;;
3251.24Slukem	force*)				# "force prefix; always start
3261.11Slukem		_arg=${_arg#force}
3271.24Slukem		_rc_force_run=YES
3281.29Slukem		if [ -n "${rcvar}" ]; then
3291.29Slukem			eval ${rcvar}=YES
3301.29Slukem		fi
3311.11Slukem		;;
3321.11Slukem	esac
3331.11Slukem
3341.16Slukem	_keywords="start stop restart rcvar $extra_commands"
3351.17Slukem	_pid=
3361.11Slukem	_pidcmd=
3371.42Slukem	_procname=${procname:-${command}}
3381.42Slukem
3391.24Slukem					# setup pid check command if not fast
3401.42Slukem	if [ -z "$_rc_fast_run" -a -n "$_procname" ]; then
3411.11Slukem		if [ -n "$pidfile" ]; then
3421.42Slukem			_pidcmd='_pid=$(check_pidfile '$pidfile' '$_procname')'
3431.42Slukem		else
3441.42Slukem			_pidcmd='_pid=$(check_process '$_procname')'
3451.11Slukem		fi
3461.11Slukem		if [ -n "$_pidcmd" ]; then
3471.33Slukem			_keywords="${_keywords} status poll"
3481.11Slukem		fi
3491.11Slukem	fi
3501.11Slukem
3511.11Slukem	if [ -z "$_arg" ]; then
3521.11Slukem		rc_usage "$_keywords"
3531.11Slukem	fi
3541.11Slukem
3551.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
3561.11Slukem		_flags=$flags
3571.11Slukem	else
3581.11Slukem		eval _flags=\$${name}_flags
3591.11Slukem	fi
3601.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
3611.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
3621.30Slukem	    _group=\$${name}_group	_groups=\$${name}_groups
3631.11Slukem
3641.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
3651.42Slukem		if [ "$_user" = "$(id -un)" ]; then
3661.42Slukem			unset _user
3671.42Slukem		fi
3681.42Slukem	fi
3691.42Slukem
3701.29Slukem					# if ${rcvar} is set, and $1 is not
3711.40Slukem					# "rcvar", then run
3721.29Slukem					#	checkyesno ${rcvar}
3731.29Slukem					# and return if that failed
3741.24Slukem					#
3751.30Slukem	# XXXX use case?
3761.40Slukem	if [ -n "${rcvar}" -a "$_arg" != "rcvar" ]; then
3771.24Slukem		if ! checkyesno ${rcvar}; then
3781.24Slukem			return 0
3791.24Slukem		fi
3801.24Slukem	fi
3811.24Slukem
3821.24Slukem	eval $_pidcmd			# determine the pid if necessary
3831.11Slukem
3841.11Slukem	for _elem in $_keywords; do
3851.11Slukem		if [ "$_elem" != "$_arg" ]; then
3861.11Slukem			continue
3871.11Slukem		fi
3881.11Slukem
3891.24Slukem					# if there's a custom ${XXX_cmd},
3901.24Slukem					# run that instead of the default
3911.24Slukem					#
3921.30Slukem		eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd
3931.11Slukem		if [ -n "$_cmd" ]; then
3941.24Slukem					# if the precmd failed and force
3951.24Slukem					# isn't set, exit
3961.24Slukem					#
3971.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
3981.24Slukem				return 1
3991.24Slukem			fi
4001.24Slukem
4011.11Slukem			eval $_cmd
4021.11Slukem			return 0
4031.11Slukem		fi
4041.11Slukem
4051.24Slukem		case "$_arg" in		# default operations...
4061.11Slukem
4071.11Slukem		status)
4081.11Slukem			if [ -n "$_pid" ]; then
4091.11Slukem				echo "${name} is running as pid $_pid."
4101.11Slukem			else
4111.11Slukem				echo "${name} is not running."
4121.28Slukem				return 1
4131.11Slukem			fi
4141.11Slukem			;;
4151.11Slukem
4161.11Slukem		start)
4171.11Slukem			if [ -n "$_pid" ]; then
4181.11Slukem				echo "${name} already running? (pid=$_pid)."
4191.11Slukem				exit 1
4201.11Slukem			fi
4211.11Slukem
4221.24Slukem			if [ ! -x $command ]; then
4231.11Slukem				return 0
4241.11Slukem			fi
4251.11Slukem
4261.24Slukem					# check for required variables,
4271.24Slukem					# directories, and files
4281.24Slukem					#
4291.11Slukem			for _f in $required_vars; do
4301.11Slukem				if ! checkyesno $_f; then
4311.24Slukem					warn "\$${_f} is not set."
4321.24Slukem					if [ -z "$_rc_force_run" ]; then
4331.24Slukem						return 1
4341.24Slukem					fi
4351.11Slukem				fi
4361.11Slukem			done
4371.11Slukem			for _f in $required_dirs; do
4381.11Slukem				if [ ! -d "${_f}/." ]; then
4391.25Slukem					warn "${_f} is not a directory."
4401.24Slukem					if [ -z "$_rc_force_run" ]; then
4411.24Slukem						return 1
4421.24Slukem					fi
4431.11Slukem				fi
4441.11Slukem			done
4451.11Slukem			for _f in $required_files; do
4461.11Slukem				if [ ! -r "${_f}" ]; then
4471.25Slukem					warn "${_f} is not readable."
4481.24Slukem					if [ -z "$_rc_force_run" ]; then
4491.24Slukem						return 1
4501.24Slukem					fi
4511.11Slukem				fi
4521.11Slukem			done
4531.11Slukem
4541.24Slukem					# if the precmd failed and force
4551.24Slukem					# isn't set, exit
4561.24Slukem					#
4571.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4581.24Slukem				return 1
4591.24Slukem			fi
4601.24Slukem
4611.24Slukem
4621.24Slukem					# setup the command to run, and run it
4631.29Slukem					#
4641.11Slukem			echo "Starting ${name}."
4651.22Slukem			if [ -n "$_chroot" ]; then
4661.22Slukem				_doit="\
4671.23Slukem${_nice:+nice -n $_nice }\
4681.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
4691.22Slukem$_chroot $command $_flags $command_args"
4701.22Slukem			else
4711.22Slukem				_doit="\
4721.18Slukem${_chdir:+cd $_chdir; }\
4731.18Slukem${_nice:+nice -n $_nice }\
4741.34Slukem$command $_flags $command_args"
4751.34Slukem				if [ -n "$_user" ]; then
4761.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
4771.34Slukem				fi
4781.22Slukem			fi
4791.18Slukem			eval $_doit
4801.11Slukem			;;
4811.11Slukem
4821.11Slukem		stop)
4831.11Slukem			if [ -z "$_pid" ]; then
4841.24Slukem				if [ -n "$pidfile" ]; then
4851.24Slukem					echo \
4861.24Slukem				    "${name} not running? (check $pidfile)."
4871.24Slukem				else
4881.24Slukem					echo "${name} not running?"
4891.11Slukem				fi
4901.24Slukem				exit 1
4911.11Slukem			fi
4921.11Slukem
4931.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4941.24Slukem				return 1
4951.24Slukem			fi
4961.11Slukem			echo "Stopping ${name}."
4971.34Slukem			_doit="kill -${sig_stop:-TERM} $_pid"
4981.34Slukem			if [ -n "$_user" ]; then
4991.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5001.34Slukem			fi
5011.18Slukem			eval $_doit
5021.35Slukem			wait_for_pids $_pid
5031.11Slukem			;;
5041.11Slukem
5051.11Slukem		reload)
5061.11Slukem			if [ -z "$_pid" ]; then
5071.24Slukem				if [ -n "$pidfile" ]; then
5081.24Slukem					echo \
5091.11Slukem				    "${name} not running? (check $pidfile)."
5101.24Slukem				else
5111.24Slukem					echo "${name} not running?"
5121.11Slukem				fi
5131.24Slukem				exit 1
5141.11Slukem			fi
5151.11Slukem			echo "Reloading ${name} config files."
5161.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5171.24Slukem				return 1
5181.24Slukem			fi
5191.34Slukem			_doit="kill -${sig_reload:-HUP} $_pid"
5201.34Slukem			if [ -n "$_user" ]; then
5211.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5221.34Slukem			fi
5231.18Slukem			eval $_doit
5241.11Slukem			;;
5251.11Slukem
5261.11Slukem		restart)
5271.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5281.24Slukem				return 1
5291.11Slukem			fi
5301.29Slukem					# prevent restart being called more
5311.29Slukem					# than once by any given script
5321.29Slukem					#
5331.29Slukem			if [ -n "$_rc_restart_done" ]; then
5341.29Slukem				return 0
5351.29Slukem			fi
5361.29Slukem			_rc_restart_done=YES
5371.33Slukem
5381.27Slukem			( $0 ${_rc_force_run:+force}stop )
5391.27Slukem			$0 ${_rc_force_run:+force}start
5401.11Slukem
5411.33Slukem			;;
5421.33Slukem
5431.33Slukem		poll)
5441.33Slukem			if [ -n "$_pid" ]; then
5451.33Slukem				wait_for_pids $_pid
5461.33Slukem			fi
5471.11Slukem			;;
5481.11Slukem
5491.11Slukem		rcvar)
5501.11Slukem			echo "# $name"
5511.24Slukem			if [ -n "$rcvar" ]; then
5521.24Slukem				if checkyesno ${rcvar}; then
5531.24Slukem					echo "\$${rcvar}=YES"
5541.24Slukem				else
5551.24Slukem					echo "\$${rcvar}=NO"
5561.24Slukem				fi
5571.11Slukem			fi
5581.11Slukem			;;
5591.11Slukem
5601.11Slukem		*)
5611.11Slukem			rc_usage "$_keywords"
5621.11Slukem			;;
5631.11Slukem
5641.11Slukem		esac
5651.11Slukem		return 0
5661.11Slukem	done
5671.11Slukem
5681.11Slukem	echo 1>&2 "$0: unknown directive '$_arg'."
5691.11Slukem	rc_usage "$_keywords"
5701.11Slukem	exit 1
5711.11Slukem}
5721.11Slukem
5731.11Slukem#
5741.11Slukem# run_rc_script file arg
5751.11Slukem#	Start the script `file' with `arg', and correctly handle the
5761.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
5771.37Slukem#	sourced into the current environment.  If `file' appears to be
5781.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
5791.37Slukem#	executable run as a child process.
5801.17Slukem#
5811.11Slukemrun_rc_script()
5821.11Slukem{
5831.11Slukem	_file=$1
5841.11Slukem	_arg=$2
5851.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
5861.11Slukem		err 3 'USAGE: run_rc_script file arg'
5871.11Slukem	fi
5881.11Slukem
5891.42Slukem	unset	name command command_args extra_commands pidfile procname \
5901.42Slukem		rcvar required_dirs required_files required_vars
5911.39Slukem	eval unset ${_arg}_cmd ${_arg}_precmd
5921.39Slukem
5931.39Slukem	case "$_file" in
5941.39Slukem	*.sh)				# run in current shell
5951.11Slukem		set $_arg ; . $_file
5961.39Slukem		;;
5971.39Slukem	*[~#]|*.OLD|*.orig)		# scratch file; skip
5981.39Slukem		warn "Ignoring scratch file $_file"
5991.39Slukem		;;
6001.39Slukem	*)				# run in subshell
6011.39Slukem		if [ -x $_file ]; then
6021.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
6031.39Slukem				set $_arg ; . $_file
6041.39Slukem			else
6051.37Slukem				( set $_arg ; . $_file )
6061.37Slukem			fi
6071.39Slukem		fi
6081.39Slukem		;;
6091.39Slukem	esac
6101.11Slukem}
6111.19Slukem
6121.19Slukem#
6131.19Slukem# load_rc_config
6141.19Slukem#	Source in the configuration file for a given command.
6151.19Slukem#
6161.19Slukemload_rc_config()
6171.19Slukem{
6181.19Slukem	_command=$1
6191.19Slukem	if [ -z "$_command" ]; then
6201.19Slukem		err 3 'USAGE: load_rc_config command'
6211.19Slukem	fi
6221.19Slukem
6231.30Slukem	if [ -z "$_rc_conf_loaded" ]; then
6241.30Slukem		. /etc/rc.conf
6251.30Slukem		_rc_conf_loaded=YES
6261.30Slukem	fi
6271.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
6281.20Sfvdl		. /etc/rc.conf.d/"$_command"
6291.20Sfvdl	fi
6301.19Slukem}
6311.19Slukem
6321.11Slukem
6331.11Slukem#
6341.11Slukem# rc_usage commands
6351.11Slukem#	Print a usage string for $0, with `commands' being a list of
6361.11Slukem#	valid commands.
6371.11Slukem#
6381.11Slukemrc_usage()
6391.11Slukem{
6401.11Slukem	echo -n 1>&2 "Usage: $0 [fast|force]("
6411.11Slukem
6421.11Slukem	_sep=
6431.11Slukem	for _elem in $*; do
6441.11Slukem		echo -n 1>&2 "$_sep$_elem"
6451.11Slukem		_sep="|"
6461.11Slukem	done
6471.11Slukem	echo 1>&2 ")"
6481.11Slukem	exit 1
6491.11Slukem}
6501.11Slukem
6511.11Slukem#
6521.11Slukem# err exitval message
6531.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
6541.11Slukem#
6551.11Slukemerr()
6561.11Slukem{
6571.11Slukem	exitval=$1
6581.11Slukem	shift
6591.11Slukem
6601.21Slukem	logger "$0: ERROR: $*"
6611.21Slukem	echo 1>&2 "$0: ERROR: $*"
6621.11Slukem	exit $exitval
6631.11Slukem}
6641.11Slukem
6651.11Slukem#
6661.11Slukem# warn message
6671.11Slukem#	Display message to stderr and log to the syslog.
6681.11Slukem#
6691.11Slukemwarn()
6701.11Slukem{
6711.21Slukem	logger "$0: WARNING: $*"
6721.21Slukem	echo 1>&2 "$0: WARNING: $*"
6731.31Satatat}
6741.31Satatat
6751.31Satatat#
6761.31Satatat# backup_file action file cur backup
6771.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
6781.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
6791.31Satatat#
6801.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
6811.31Satatat#	which can be either YES or NO.
6821.31Satatat#
6831.31Satatat#	The `action' keyword can be one of the following:
6841.31Satatat#
6851.31Satatat#	add		`file' is now being backed up (and is possibly
6861.31Satatat#			being reentered into the backups system).  `cur'
6871.31Satatat#			is created and RCS files, if necessary, are
6881.31Satatat#			created as well.
6891.31Satatat#
6901.31Satatat#	update		`file' has changed and needs to be backed up.
6911.31Satatat#			If `cur' exists, it is copied to to `back' or
6921.31Satatat#			checked into RCS (if the repository file is old),
6931.31Satatat#			and then `file' is copied to `cur'.  Another RCS
6941.31Satatat#			check in done here if RCS is being used.
6951.31Satatat#
6961.31Satatat#	remove		`file' is no longer being tracked by the backups
6971.31Satatat#			system.  If RCS is not being used, `cur' is moved
6981.31Satatat#			to `back', otherwise an empty file is checked in,
6991.31Satatat#			and then `cur' is removed.
7001.31Satatat#
7011.31Satatat#
7021.31Satatatbackup_file()
7031.31Satatat{
7041.31Satatat	_action=$1
7051.31Satatat	_file=$2
7061.31Satatat	_cur=$3
7071.31Satatat	_back=$4
7081.31Satatat
7091.31Satatat	if checkyesno backup_uses_rcs; then
7101.31Satatat		_msg0="backup archive"
7111.31Satatat		_msg1="update"
7121.31Satatat
7131.36Satatat		# ensure that history file is not locked
7141.36Satatat		if [ -f $_cur,v ]; then
7151.36Satatat			rcs -q -u -U -M $_cur
7161.36Satatat		fi
7171.36Satatat
7181.31Satatat		# ensure after switching to rcs that the
7191.31Satatat		# current backup is not lost
7201.31Satatat		if [ -f $_cur ]; then
7211.31Satatat			# no archive, or current newer than archive
7221.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
7231.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7241.36Satatat				rcs -q -kb -U $_cur
7251.31Satatat			fi
7261.31Satatat		fi
7271.31Satatat
7281.31Satatat		case $_action in
7291.31Satatat		add|update)
7301.31Satatat			cp -p $_file $_cur
7311.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7321.36Satatat			rcs -q -kb -U $_cur
7331.31Satatat			chown root:wheel $_cur $_cur,v
7341.31Satatat			;;
7351.31Satatat		remove)
7361.31Satatat			cp /dev/null $_cur
7371.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7381.36Satatat			rcs -q -kb -U $_cur
7391.31Satatat			chown root:wheel $_cur $_cur,v
7401.31Satatat			rm $_cur
7411.31Satatat			;;
7421.31Satatat		esac
7431.31Satatat	else
7441.31Satatat		case $_action in
7451.31Satatat		add|update)
7461.31Satatat			if [ -f $_cur ]; then
7471.31Satatat				cp -p $_cur $_back
7481.31Satatat			fi
7491.31Satatat			cp -p $_file $_cur
7501.31Satatat			chown root:wheel $_cur
7511.31Satatat			;;
7521.31Satatat		remove)
7531.31Satatat			mv -f $_cur $_back
7541.31Satatat			;;
7551.31Satatat		esac
7561.31Satatat	fi
7571.1Scjs}
758