rc.subr revision 1.43
11.43Slukem# $NetBSD: rc.subr,v 1.43 2002/03/13 04:50:08 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.43Slukem#	${_arg}_postcmd	n	If set, run just after performing the main
2731.43Slukem#				action in the default command, if that action
2741.43Slukem#				returned a zero exit code.
2751.43Slukem#
2761.11Slukem#	required_dirs	n	If set, check for the existence of the given
2771.11Slukem#				directories before running the default
2781.11Slukem#				(re)start command.
2791.24Slukem#
2801.11Slukem#	required_files	n	If set, check for the readability of the given
2811.11Slukem#				files before running the default (re)start
2821.11Slukem#				command.
2831.24Slukem#
2841.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
2851.11Slukem#				listed variables before running the default
2861.11Slukem#				(re)start command.
2871.11Slukem#
2881.11Slukem#	Default commands for a given arg:
2891.24Slukem#
2901.11Slukem#	arg		default
2911.11Slukem#	---		-------
2921.11Slukem#	start		if !running && checkyesno ${rcvar}
2931.11Slukem#				${command}
2941.24Slukem#
2951.11Slukem#	stop		if ${pidfile}
2961.42Slukem#				_pid=$(check_pidfile $pidfile $command)
2971.11Slukem#			else
2981.42Slukem#				_pid=$(check_process $command)
2991.35Slukem#			kill $sig_stop $_pid
3001.35Slukem#			wait_for_pids $_pid
3011.35Slukem#			($sig_stop defaults to TERM.)
3021.24Slukem#
3031.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
3041.35Slukem#			and doesn't wait_for_pids.
3051.11Slukem#			$sig_reload defaults to HUP.
3061.24Slukem#
3071.11Slukem#	restart		Run `stop' then `start'.
3081.11Slukem#
3091.33Slukem#	status		Show if ${command} is running, etc.
3101.33Slukem#
3111.33Slukem#	poll		Wait for ${command} to exit.
3121.33Slukem#
3131.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
3141.33Slukem#
3151.33Slukem#
3161.24Slukem#
3171.11Slukemrun_rc_command()
3181.11Slukem{
3191.11Slukem	_arg=$1
3201.24Slukem	if [ -z "$name" ]; then
3211.30Slukem		err 3 'run_rc_command: $name is not set.'
3221.11Slukem	fi
3231.11Slukem
3241.11Slukem	case "$_arg" in
3251.24Slukem	fast*)				# "fast" prefix; don't check pid
3261.11Slukem		_arg=${_arg#fast}
3271.11Slukem		_rc_fast_run=YES
3281.11Slukem		;;
3291.24Slukem	force*)				# "force prefix; always start
3301.11Slukem		_arg=${_arg#force}
3311.24Slukem		_rc_force_run=YES
3321.29Slukem		if [ -n "${rcvar}" ]; then
3331.29Slukem			eval ${rcvar}=YES
3341.29Slukem		fi
3351.11Slukem		;;
3361.11Slukem	esac
3371.11Slukem
3381.16Slukem	_keywords="start stop restart rcvar $extra_commands"
3391.17Slukem	_pid=
3401.11Slukem	_pidcmd=
3411.42Slukem	_procname=${procname:-${command}}
3421.42Slukem
3431.24Slukem					# setup pid check command if not fast
3441.42Slukem	if [ -z "$_rc_fast_run" -a -n "$_procname" ]; then
3451.11Slukem		if [ -n "$pidfile" ]; then
3461.42Slukem			_pidcmd='_pid=$(check_pidfile '$pidfile' '$_procname')'
3471.42Slukem		else
3481.42Slukem			_pidcmd='_pid=$(check_process '$_procname')'
3491.11Slukem		fi
3501.11Slukem		if [ -n "$_pidcmd" ]; then
3511.33Slukem			_keywords="${_keywords} status poll"
3521.11Slukem		fi
3531.11Slukem	fi
3541.11Slukem
3551.11Slukem	if [ -z "$_arg" ]; then
3561.11Slukem		rc_usage "$_keywords"
3571.11Slukem	fi
3581.11Slukem
3591.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
3601.11Slukem		_flags=$flags
3611.11Slukem	else
3621.11Slukem		eval _flags=\$${name}_flags
3631.11Slukem	fi
3641.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
3651.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
3661.30Slukem	    _group=\$${name}_group	_groups=\$${name}_groups
3671.11Slukem
3681.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
3691.42Slukem		if [ "$_user" = "$(id -un)" ]; then
3701.42Slukem			unset _user
3711.42Slukem		fi
3721.42Slukem	fi
3731.42Slukem
3741.29Slukem					# if ${rcvar} is set, and $1 is not
3751.40Slukem					# "rcvar", then run
3761.29Slukem					#	checkyesno ${rcvar}
3771.29Slukem					# and return if that failed
3781.24Slukem					#
3791.30Slukem	# XXXX use case?
3801.40Slukem	if [ -n "${rcvar}" -a "$_arg" != "rcvar" ]; then
3811.24Slukem		if ! checkyesno ${rcvar}; then
3821.24Slukem			return 0
3831.24Slukem		fi
3841.24Slukem	fi
3851.24Slukem
3861.24Slukem	eval $_pidcmd			# determine the pid if necessary
3871.11Slukem
3881.11Slukem	for _elem in $_keywords; do
3891.11Slukem		if [ "$_elem" != "$_arg" ]; then
3901.11Slukem			continue
3911.11Slukem		fi
3921.11Slukem
3931.24Slukem					# if there's a custom ${XXX_cmd},
3941.24Slukem					# run that instead of the default
3951.24Slukem					#
3961.43Slukem		eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd \
3971.43Slukem		    _postcmd=\$${_arg}_postcmd
3981.11Slukem		if [ -n "$_cmd" ]; then
3991.24Slukem					# if the precmd failed and force
4001.24Slukem					# isn't set, exit
4011.24Slukem					#
4021.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4031.24Slukem				return 1
4041.24Slukem			fi
4051.24Slukem
4061.11Slukem			eval $_cmd
4071.11Slukem			return 0
4081.11Slukem		fi
4091.11Slukem
4101.24Slukem		case "$_arg" in		# default operations...
4111.11Slukem
4121.11Slukem		status)
4131.11Slukem			if [ -n "$_pid" ]; then
4141.11Slukem				echo "${name} is running as pid $_pid."
4151.11Slukem			else
4161.11Slukem				echo "${name} is not running."
4171.28Slukem				return 1
4181.11Slukem			fi
4191.11Slukem			;;
4201.11Slukem
4211.11Slukem		start)
4221.11Slukem			if [ -n "$_pid" ]; then
4231.11Slukem				echo "${name} already running? (pid=$_pid)."
4241.11Slukem				exit 1
4251.11Slukem			fi
4261.11Slukem
4271.24Slukem			if [ ! -x $command ]; then
4281.11Slukem				return 0
4291.11Slukem			fi
4301.11Slukem
4311.24Slukem					# check for required variables,
4321.24Slukem					# directories, and files
4331.24Slukem					#
4341.11Slukem			for _f in $required_vars; do
4351.11Slukem				if ! checkyesno $_f; then
4361.24Slukem					warn "\$${_f} is not set."
4371.24Slukem					if [ -z "$_rc_force_run" ]; then
4381.24Slukem						return 1
4391.24Slukem					fi
4401.11Slukem				fi
4411.11Slukem			done
4421.11Slukem			for _f in $required_dirs; do
4431.11Slukem				if [ ! -d "${_f}/." ]; then
4441.25Slukem					warn "${_f} is not a directory."
4451.24Slukem					if [ -z "$_rc_force_run" ]; then
4461.24Slukem						return 1
4471.24Slukem					fi
4481.11Slukem				fi
4491.11Slukem			done
4501.11Slukem			for _f in $required_files; do
4511.11Slukem				if [ ! -r "${_f}" ]; then
4521.25Slukem					warn "${_f} is not readable."
4531.24Slukem					if [ -z "$_rc_force_run" ]; then
4541.24Slukem						return 1
4551.24Slukem					fi
4561.11Slukem				fi
4571.11Slukem			done
4581.11Slukem
4591.24Slukem					# if the precmd failed and force
4601.24Slukem					# isn't set, exit
4611.24Slukem					#
4621.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4631.24Slukem				return 1
4641.24Slukem			fi
4651.24Slukem
4661.24Slukem					# setup the command to run, and run it
4671.29Slukem					#
4681.11Slukem			echo "Starting ${name}."
4691.22Slukem			if [ -n "$_chroot" ]; then
4701.22Slukem				_doit="\
4711.23Slukem${_nice:+nice -n $_nice }\
4721.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
4731.22Slukem$_chroot $command $_flags $command_args"
4741.22Slukem			else
4751.22Slukem				_doit="\
4761.18Slukem${_chdir:+cd $_chdir; }\
4771.18Slukem${_nice:+nice -n $_nice }\
4781.34Slukem$command $_flags $command_args"
4791.34Slukem				if [ -n "$_user" ]; then
4801.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
4811.34Slukem				fi
4821.22Slukem			fi
4831.43Slukem
4841.43Slukem					# if the cmd failed and force
4851.43Slukem					# isn't set, exit
4861.43Slukem					#
4871.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
4881.43Slukem				return 1
4891.43Slukem			fi
4901.43Slukem
4911.43Slukem					# finally, run postcmd
4921.43Slukem					#
4931.43Slukem			eval $_postcmd
4941.11Slukem			;;
4951.11Slukem
4961.11Slukem		stop)
4971.11Slukem			if [ -z "$_pid" ]; then
4981.24Slukem				if [ -n "$pidfile" ]; then
4991.24Slukem					echo \
5001.24Slukem				    "${name} not running? (check $pidfile)."
5011.24Slukem				else
5021.24Slukem					echo "${name} not running?"
5031.11Slukem				fi
5041.24Slukem				exit 1
5051.11Slukem			fi
5061.11Slukem
5071.43Slukem					# if the precmd failed and force
5081.43Slukem					# isn't set, exit
5091.43Slukem					#
5101.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5111.24Slukem				return 1
5121.24Slukem			fi
5131.43Slukem
5141.43Slukem					# send the signal to stop
5151.43Slukem					#
5161.11Slukem			echo "Stopping ${name}."
5171.34Slukem			_doit="kill -${sig_stop:-TERM} $_pid"
5181.34Slukem			if [ -n "$_user" ]; then
5191.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5201.34Slukem			fi
5211.43Slukem
5221.43Slukem					# if the stop cmd failed and force
5231.43Slukem					# isn't set, exit
5241.43Slukem					#
5251.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
5261.43Slukem				return 1
5271.43Slukem			fi
5281.43Slukem
5291.43Slukem					# wait for the command to exit,
5301.43Slukem					# and run postcmd.
5311.35Slukem			wait_for_pids $_pid
5321.43Slukem			eval $_postcmd
5331.11Slukem			;;
5341.11Slukem
5351.11Slukem		reload)
5361.11Slukem			if [ -z "$_pid" ]; then
5371.24Slukem				if [ -n "$pidfile" ]; then
5381.24Slukem					echo \
5391.11Slukem				    "${name} not running? (check $pidfile)."
5401.24Slukem				else
5411.24Slukem					echo "${name} not running?"
5421.11Slukem				fi
5431.24Slukem				exit 1
5441.11Slukem			fi
5451.11Slukem			echo "Reloading ${name} config files."
5461.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5471.24Slukem				return 1
5481.24Slukem			fi
5491.34Slukem			_doit="kill -${sig_reload:-HUP} $_pid"
5501.34Slukem			if [ -n "$_user" ]; then
5511.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5521.34Slukem			fi
5531.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
5541.43Slukem				return 1
5551.43Slukem			fi
5561.43Slukem			eval $_postcmd
5571.11Slukem			;;
5581.11Slukem
5591.11Slukem		restart)
5601.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5611.24Slukem				return 1
5621.11Slukem			fi
5631.29Slukem					# prevent restart being called more
5641.29Slukem					# than once by any given script
5651.29Slukem					#
5661.29Slukem			if [ -n "$_rc_restart_done" ]; then
5671.29Slukem				return 0
5681.29Slukem			fi
5691.29Slukem			_rc_restart_done=YES
5701.33Slukem
5711.27Slukem			( $0 ${_rc_force_run:+force}stop )
5721.27Slukem			$0 ${_rc_force_run:+force}start
5731.11Slukem
5741.43Slukem			eval $_postcmd
5751.33Slukem			;;
5761.33Slukem
5771.33Slukem		poll)
5781.33Slukem			if [ -n "$_pid" ]; then
5791.33Slukem				wait_for_pids $_pid
5801.33Slukem			fi
5811.11Slukem			;;
5821.11Slukem
5831.11Slukem		rcvar)
5841.11Slukem			echo "# $name"
5851.24Slukem			if [ -n "$rcvar" ]; then
5861.24Slukem				if checkyesno ${rcvar}; then
5871.24Slukem					echo "\$${rcvar}=YES"
5881.24Slukem				else
5891.24Slukem					echo "\$${rcvar}=NO"
5901.24Slukem				fi
5911.11Slukem			fi
5921.11Slukem			;;
5931.11Slukem
5941.11Slukem		*)
5951.11Slukem			rc_usage "$_keywords"
5961.11Slukem			;;
5971.11Slukem
5981.11Slukem		esac
5991.11Slukem		return 0
6001.11Slukem	done
6011.11Slukem
6021.11Slukem	echo 1>&2 "$0: unknown directive '$_arg'."
6031.11Slukem	rc_usage "$_keywords"
6041.11Slukem	exit 1
6051.11Slukem}
6061.11Slukem
6071.11Slukem#
6081.11Slukem# run_rc_script file arg
6091.11Slukem#	Start the script `file' with `arg', and correctly handle the
6101.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
6111.37Slukem#	sourced into the current environment.  If `file' appears to be
6121.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
6131.37Slukem#	executable run as a child process.
6141.17Slukem#
6151.11Slukemrun_rc_script()
6161.11Slukem{
6171.11Slukem	_file=$1
6181.11Slukem	_arg=$2
6191.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
6201.11Slukem		err 3 'USAGE: run_rc_script file arg'
6211.11Slukem	fi
6221.11Slukem
6231.42Slukem	unset	name command command_args extra_commands pidfile procname \
6241.42Slukem		rcvar required_dirs required_files required_vars
6251.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
6261.39Slukem
6271.39Slukem	case "$_file" in
6281.39Slukem	*.sh)				# run in current shell
6291.11Slukem		set $_arg ; . $_file
6301.39Slukem		;;
6311.39Slukem	*[~#]|*.OLD|*.orig)		# scratch file; skip
6321.39Slukem		warn "Ignoring scratch file $_file"
6331.39Slukem		;;
6341.39Slukem	*)				# run in subshell
6351.39Slukem		if [ -x $_file ]; then
6361.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
6371.39Slukem				set $_arg ; . $_file
6381.39Slukem			else
6391.37Slukem				( set $_arg ; . $_file )
6401.37Slukem			fi
6411.39Slukem		fi
6421.39Slukem		;;
6431.39Slukem	esac
6441.11Slukem}
6451.19Slukem
6461.19Slukem#
6471.19Slukem# load_rc_config
6481.19Slukem#	Source in the configuration file for a given command.
6491.19Slukem#
6501.19Slukemload_rc_config()
6511.19Slukem{
6521.19Slukem	_command=$1
6531.19Slukem	if [ -z "$_command" ]; then
6541.19Slukem		err 3 'USAGE: load_rc_config command'
6551.19Slukem	fi
6561.19Slukem
6571.30Slukem	if [ -z "$_rc_conf_loaded" ]; then
6581.30Slukem		. /etc/rc.conf
6591.30Slukem		_rc_conf_loaded=YES
6601.30Slukem	fi
6611.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
6621.20Sfvdl		. /etc/rc.conf.d/"$_command"
6631.20Sfvdl	fi
6641.19Slukem}
6651.19Slukem
6661.11Slukem
6671.11Slukem#
6681.11Slukem# rc_usage commands
6691.11Slukem#	Print a usage string for $0, with `commands' being a list of
6701.11Slukem#	valid commands.
6711.11Slukem#
6721.11Slukemrc_usage()
6731.11Slukem{
6741.11Slukem	echo -n 1>&2 "Usage: $0 [fast|force]("
6751.11Slukem
6761.11Slukem	_sep=
6771.11Slukem	for _elem in $*; do
6781.11Slukem		echo -n 1>&2 "$_sep$_elem"
6791.11Slukem		_sep="|"
6801.11Slukem	done
6811.11Slukem	echo 1>&2 ")"
6821.11Slukem	exit 1
6831.11Slukem}
6841.11Slukem
6851.11Slukem#
6861.11Slukem# err exitval message
6871.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
6881.11Slukem#
6891.11Slukemerr()
6901.11Slukem{
6911.11Slukem	exitval=$1
6921.11Slukem	shift
6931.11Slukem
6941.21Slukem	logger "$0: ERROR: $*"
6951.21Slukem	echo 1>&2 "$0: ERROR: $*"
6961.11Slukem	exit $exitval
6971.11Slukem}
6981.11Slukem
6991.11Slukem#
7001.11Slukem# warn message
7011.11Slukem#	Display message to stderr and log to the syslog.
7021.11Slukem#
7031.11Slukemwarn()
7041.11Slukem{
7051.21Slukem	logger "$0: WARNING: $*"
7061.21Slukem	echo 1>&2 "$0: WARNING: $*"
7071.31Satatat}
7081.31Satatat
7091.31Satatat#
7101.31Satatat# backup_file action file cur backup
7111.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
7121.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
7131.31Satatat#
7141.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
7151.31Satatat#	which can be either YES or NO.
7161.31Satatat#
7171.31Satatat#	The `action' keyword can be one of the following:
7181.31Satatat#
7191.31Satatat#	add		`file' is now being backed up (and is possibly
7201.31Satatat#			being reentered into the backups system).  `cur'
7211.31Satatat#			is created and RCS files, if necessary, are
7221.31Satatat#			created as well.
7231.31Satatat#
7241.31Satatat#	update		`file' has changed and needs to be backed up.
7251.31Satatat#			If `cur' exists, it is copied to to `back' or
7261.31Satatat#			checked into RCS (if the repository file is old),
7271.31Satatat#			and then `file' is copied to `cur'.  Another RCS
7281.31Satatat#			check in done here if RCS is being used.
7291.31Satatat#
7301.31Satatat#	remove		`file' is no longer being tracked by the backups
7311.31Satatat#			system.  If RCS is not being used, `cur' is moved
7321.31Satatat#			to `back', otherwise an empty file is checked in,
7331.31Satatat#			and then `cur' is removed.
7341.31Satatat#
7351.31Satatat#
7361.31Satatatbackup_file()
7371.31Satatat{
7381.31Satatat	_action=$1
7391.31Satatat	_file=$2
7401.31Satatat	_cur=$3
7411.31Satatat	_back=$4
7421.31Satatat
7431.31Satatat	if checkyesno backup_uses_rcs; then
7441.31Satatat		_msg0="backup archive"
7451.31Satatat		_msg1="update"
7461.31Satatat
7471.36Satatat		# ensure that history file is not locked
7481.36Satatat		if [ -f $_cur,v ]; then
7491.36Satatat			rcs -q -u -U -M $_cur
7501.36Satatat		fi
7511.36Satatat
7521.31Satatat		# ensure after switching to rcs that the
7531.31Satatat		# current backup is not lost
7541.31Satatat		if [ -f $_cur ]; then
7551.31Satatat			# no archive, or current newer than archive
7561.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
7571.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7581.36Satatat				rcs -q -kb -U $_cur
7591.31Satatat			fi
7601.31Satatat		fi
7611.31Satatat
7621.31Satatat		case $_action in
7631.31Satatat		add|update)
7641.31Satatat			cp -p $_file $_cur
7651.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7661.36Satatat			rcs -q -kb -U $_cur
7671.31Satatat			chown root:wheel $_cur $_cur,v
7681.31Satatat			;;
7691.31Satatat		remove)
7701.31Satatat			cp /dev/null $_cur
7711.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7721.36Satatat			rcs -q -kb -U $_cur
7731.31Satatat			chown root:wheel $_cur $_cur,v
7741.31Satatat			rm $_cur
7751.31Satatat			;;
7761.31Satatat		esac
7771.31Satatat	else
7781.31Satatat		case $_action in
7791.31Satatat		add|update)
7801.31Satatat			if [ -f $_cur ]; then
7811.31Satatat				cp -p $_cur $_back
7821.31Satatat			fi
7831.31Satatat			cp -p $_file $_cur
7841.31Satatat			chown root:wheel $_cur
7851.31Satatat			;;
7861.31Satatat		remove)
7871.31Satatat			mv -f $_cur $_back
7881.31Satatat			;;
7891.31Satatat		esac
7901.31Satatat	fi
7911.1Scjs}
792