rc.subr revision 1.44
11.44Slukem# $NetBSD: rc.subr,v 1.44 2002/03/13 06:58: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.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.44Slukem			if ! eval $_cmd && [ -z "$_rc_force_run" ]; then
4071.44Slukem				return 1
4081.44Slukem			fi
4091.44Slukem			eval $_postcmd
4101.11Slukem			return 0
4111.11Slukem		fi
4121.11Slukem
4131.24Slukem		case "$_arg" in		# default operations...
4141.11Slukem
4151.11Slukem		status)
4161.11Slukem			if [ -n "$_pid" ]; then
4171.11Slukem				echo "${name} is running as pid $_pid."
4181.11Slukem			else
4191.11Slukem				echo "${name} is not running."
4201.28Slukem				return 1
4211.11Slukem			fi
4221.11Slukem			;;
4231.11Slukem
4241.11Slukem		start)
4251.11Slukem			if [ -n "$_pid" ]; then
4261.11Slukem				echo "${name} already running? (pid=$_pid)."
4271.11Slukem				exit 1
4281.11Slukem			fi
4291.11Slukem
4301.24Slukem			if [ ! -x $command ]; then
4311.11Slukem				return 0
4321.11Slukem			fi
4331.11Slukem
4341.24Slukem					# check for required variables,
4351.24Slukem					# directories, and files
4361.24Slukem					#
4371.11Slukem			for _f in $required_vars; do
4381.11Slukem				if ! checkyesno $_f; then
4391.24Slukem					warn "\$${_f} is not set."
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_dirs; do
4461.11Slukem				if [ ! -d "${_f}/." ]; then
4471.25Slukem					warn "${_f} is not a directory."
4481.24Slukem					if [ -z "$_rc_force_run" ]; then
4491.24Slukem						return 1
4501.24Slukem					fi
4511.11Slukem				fi
4521.11Slukem			done
4531.11Slukem			for _f in $required_files; do
4541.11Slukem				if [ ! -r "${_f}" ]; then
4551.25Slukem					warn "${_f} is not readable."
4561.24Slukem					if [ -z "$_rc_force_run" ]; then
4571.24Slukem						return 1
4581.24Slukem					fi
4591.11Slukem				fi
4601.11Slukem			done
4611.11Slukem
4621.24Slukem					# if the precmd failed and force
4631.24Slukem					# isn't set, exit
4641.24Slukem					#
4651.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4661.24Slukem				return 1
4671.24Slukem			fi
4681.24Slukem
4691.24Slukem					# setup the command to run, and run it
4701.29Slukem					#
4711.11Slukem			echo "Starting ${name}."
4721.22Slukem			if [ -n "$_chroot" ]; then
4731.22Slukem				_doit="\
4741.23Slukem${_nice:+nice -n $_nice }\
4751.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
4761.22Slukem$_chroot $command $_flags $command_args"
4771.22Slukem			else
4781.22Slukem				_doit="\
4791.18Slukem${_chdir:+cd $_chdir; }\
4801.18Slukem${_nice:+nice -n $_nice }\
4811.34Slukem$command $_flags $command_args"
4821.34Slukem				if [ -n "$_user" ]; then
4831.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
4841.34Slukem				fi
4851.22Slukem			fi
4861.43Slukem
4871.43Slukem					# if the cmd failed and force
4881.43Slukem					# isn't set, exit
4891.43Slukem					#
4901.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
4911.43Slukem				return 1
4921.43Slukem			fi
4931.43Slukem
4941.43Slukem					# finally, run postcmd
4951.43Slukem					#
4961.43Slukem			eval $_postcmd
4971.11Slukem			;;
4981.11Slukem
4991.11Slukem		stop)
5001.11Slukem			if [ -z "$_pid" ]; then
5011.24Slukem				if [ -n "$pidfile" ]; then
5021.24Slukem					echo \
5031.24Slukem				    "${name} not running? (check $pidfile)."
5041.24Slukem				else
5051.24Slukem					echo "${name} not running?"
5061.11Slukem				fi
5071.24Slukem				exit 1
5081.11Slukem			fi
5091.11Slukem
5101.43Slukem					# if the precmd failed and force
5111.43Slukem					# isn't set, exit
5121.43Slukem					#
5131.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5141.24Slukem				return 1
5151.24Slukem			fi
5161.43Slukem
5171.43Slukem					# send the signal to stop
5181.43Slukem					#
5191.11Slukem			echo "Stopping ${name}."
5201.34Slukem			_doit="kill -${sig_stop:-TERM} $_pid"
5211.34Slukem			if [ -n "$_user" ]; then
5221.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5231.34Slukem			fi
5241.43Slukem
5251.43Slukem					# if the stop cmd failed and force
5261.43Slukem					# isn't set, exit
5271.43Slukem					#
5281.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
5291.43Slukem				return 1
5301.43Slukem			fi
5311.43Slukem
5321.43Slukem					# wait for the command to exit,
5331.43Slukem					# and run postcmd.
5341.35Slukem			wait_for_pids $_pid
5351.43Slukem			eval $_postcmd
5361.11Slukem			;;
5371.11Slukem
5381.11Slukem		reload)
5391.11Slukem			if [ -z "$_pid" ]; then
5401.24Slukem				if [ -n "$pidfile" ]; then
5411.24Slukem					echo \
5421.11Slukem				    "${name} not running? (check $pidfile)."
5431.24Slukem				else
5441.24Slukem					echo "${name} not running?"
5451.11Slukem				fi
5461.24Slukem				exit 1
5471.11Slukem			fi
5481.11Slukem			echo "Reloading ${name} config files."
5491.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5501.24Slukem				return 1
5511.24Slukem			fi
5521.34Slukem			_doit="kill -${sig_reload:-HUP} $_pid"
5531.34Slukem			if [ -n "$_user" ]; then
5541.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5551.34Slukem			fi
5561.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
5571.43Slukem				return 1
5581.43Slukem			fi
5591.43Slukem			eval $_postcmd
5601.11Slukem			;;
5611.11Slukem
5621.11Slukem		restart)
5631.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5641.24Slukem				return 1
5651.11Slukem			fi
5661.29Slukem					# prevent restart being called more
5671.29Slukem					# than once by any given script
5681.29Slukem					#
5691.29Slukem			if [ -n "$_rc_restart_done" ]; then
5701.29Slukem				return 0
5711.29Slukem			fi
5721.29Slukem			_rc_restart_done=YES
5731.33Slukem
5741.27Slukem			( $0 ${_rc_force_run:+force}stop )
5751.27Slukem			$0 ${_rc_force_run:+force}start
5761.11Slukem
5771.43Slukem			eval $_postcmd
5781.33Slukem			;;
5791.33Slukem
5801.33Slukem		poll)
5811.33Slukem			if [ -n "$_pid" ]; then
5821.33Slukem				wait_for_pids $_pid
5831.33Slukem			fi
5841.11Slukem			;;
5851.11Slukem
5861.11Slukem		rcvar)
5871.11Slukem			echo "# $name"
5881.24Slukem			if [ -n "$rcvar" ]; then
5891.24Slukem				if checkyesno ${rcvar}; then
5901.24Slukem					echo "\$${rcvar}=YES"
5911.24Slukem				else
5921.24Slukem					echo "\$${rcvar}=NO"
5931.24Slukem				fi
5941.11Slukem			fi
5951.11Slukem			;;
5961.11Slukem
5971.11Slukem		*)
5981.11Slukem			rc_usage "$_keywords"
5991.11Slukem			;;
6001.11Slukem
6011.11Slukem		esac
6021.11Slukem		return 0
6031.11Slukem	done
6041.11Slukem
6051.11Slukem	echo 1>&2 "$0: unknown directive '$_arg'."
6061.11Slukem	rc_usage "$_keywords"
6071.11Slukem	exit 1
6081.11Slukem}
6091.11Slukem
6101.11Slukem#
6111.11Slukem# run_rc_script file arg
6121.11Slukem#	Start the script `file' with `arg', and correctly handle the
6131.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
6141.37Slukem#	sourced into the current environment.  If `file' appears to be
6151.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
6161.37Slukem#	executable run as a child process.
6171.17Slukem#
6181.11Slukemrun_rc_script()
6191.11Slukem{
6201.11Slukem	_file=$1
6211.11Slukem	_arg=$2
6221.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
6231.11Slukem		err 3 'USAGE: run_rc_script file arg'
6241.11Slukem	fi
6251.11Slukem
6261.42Slukem	unset	name command command_args extra_commands pidfile procname \
6271.42Slukem		rcvar required_dirs required_files required_vars
6281.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
6291.39Slukem
6301.39Slukem	case "$_file" in
6311.39Slukem	*.sh)				# run in current shell
6321.11Slukem		set $_arg ; . $_file
6331.39Slukem		;;
6341.39Slukem	*[~#]|*.OLD|*.orig)		# scratch file; skip
6351.39Slukem		warn "Ignoring scratch file $_file"
6361.39Slukem		;;
6371.39Slukem	*)				# run in subshell
6381.39Slukem		if [ -x $_file ]; then
6391.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
6401.39Slukem				set $_arg ; . $_file
6411.39Slukem			else
6421.37Slukem				( set $_arg ; . $_file )
6431.37Slukem			fi
6441.39Slukem		fi
6451.39Slukem		;;
6461.39Slukem	esac
6471.11Slukem}
6481.19Slukem
6491.19Slukem#
6501.19Slukem# load_rc_config
6511.19Slukem#	Source in the configuration file for a given command.
6521.19Slukem#
6531.19Slukemload_rc_config()
6541.19Slukem{
6551.19Slukem	_command=$1
6561.19Slukem	if [ -z "$_command" ]; then
6571.19Slukem		err 3 'USAGE: load_rc_config command'
6581.19Slukem	fi
6591.19Slukem
6601.30Slukem	if [ -z "$_rc_conf_loaded" ]; then
6611.30Slukem		. /etc/rc.conf
6621.30Slukem		_rc_conf_loaded=YES
6631.30Slukem	fi
6641.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
6651.20Sfvdl		. /etc/rc.conf.d/"$_command"
6661.20Sfvdl	fi
6671.19Slukem}
6681.19Slukem
6691.11Slukem
6701.11Slukem#
6711.11Slukem# rc_usage commands
6721.11Slukem#	Print a usage string for $0, with `commands' being a list of
6731.11Slukem#	valid commands.
6741.11Slukem#
6751.11Slukemrc_usage()
6761.11Slukem{
6771.11Slukem	echo -n 1>&2 "Usage: $0 [fast|force]("
6781.11Slukem
6791.11Slukem	_sep=
6801.11Slukem	for _elem in $*; do
6811.11Slukem		echo -n 1>&2 "$_sep$_elem"
6821.11Slukem		_sep="|"
6831.11Slukem	done
6841.11Slukem	echo 1>&2 ")"
6851.11Slukem	exit 1
6861.11Slukem}
6871.11Slukem
6881.11Slukem#
6891.11Slukem# err exitval message
6901.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
6911.11Slukem#
6921.11Slukemerr()
6931.11Slukem{
6941.11Slukem	exitval=$1
6951.11Slukem	shift
6961.11Slukem
6971.21Slukem	logger "$0: ERROR: $*"
6981.21Slukem	echo 1>&2 "$0: ERROR: $*"
6991.11Slukem	exit $exitval
7001.11Slukem}
7011.11Slukem
7021.11Slukem#
7031.11Slukem# warn message
7041.11Slukem#	Display message to stderr and log to the syslog.
7051.11Slukem#
7061.11Slukemwarn()
7071.11Slukem{
7081.21Slukem	logger "$0: WARNING: $*"
7091.21Slukem	echo 1>&2 "$0: WARNING: $*"
7101.31Satatat}
7111.31Satatat
7121.31Satatat#
7131.31Satatat# backup_file action file cur backup
7141.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
7151.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
7161.31Satatat#
7171.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
7181.31Satatat#	which can be either YES or NO.
7191.31Satatat#
7201.31Satatat#	The `action' keyword can be one of the following:
7211.31Satatat#
7221.31Satatat#	add		`file' is now being backed up (and is possibly
7231.31Satatat#			being reentered into the backups system).  `cur'
7241.31Satatat#			is created and RCS files, if necessary, are
7251.31Satatat#			created as well.
7261.31Satatat#
7271.31Satatat#	update		`file' has changed and needs to be backed up.
7281.31Satatat#			If `cur' exists, it is copied to to `back' or
7291.31Satatat#			checked into RCS (if the repository file is old),
7301.31Satatat#			and then `file' is copied to `cur'.  Another RCS
7311.31Satatat#			check in done here if RCS is being used.
7321.31Satatat#
7331.31Satatat#	remove		`file' is no longer being tracked by the backups
7341.31Satatat#			system.  If RCS is not being used, `cur' is moved
7351.31Satatat#			to `back', otherwise an empty file is checked in,
7361.31Satatat#			and then `cur' is removed.
7371.31Satatat#
7381.31Satatat#
7391.31Satatatbackup_file()
7401.31Satatat{
7411.31Satatat	_action=$1
7421.31Satatat	_file=$2
7431.31Satatat	_cur=$3
7441.31Satatat	_back=$4
7451.31Satatat
7461.31Satatat	if checkyesno backup_uses_rcs; then
7471.31Satatat		_msg0="backup archive"
7481.31Satatat		_msg1="update"
7491.31Satatat
7501.36Satatat		# ensure that history file is not locked
7511.36Satatat		if [ -f $_cur,v ]; then
7521.36Satatat			rcs -q -u -U -M $_cur
7531.36Satatat		fi
7541.36Satatat
7551.31Satatat		# ensure after switching to rcs that the
7561.31Satatat		# current backup is not lost
7571.31Satatat		if [ -f $_cur ]; then
7581.31Satatat			# no archive, or current newer than archive
7591.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
7601.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7611.36Satatat				rcs -q -kb -U $_cur
7621.31Satatat			fi
7631.31Satatat		fi
7641.31Satatat
7651.31Satatat		case $_action in
7661.31Satatat		add|update)
7671.31Satatat			cp -p $_file $_cur
7681.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7691.36Satatat			rcs -q -kb -U $_cur
7701.31Satatat			chown root:wheel $_cur $_cur,v
7711.31Satatat			;;
7721.31Satatat		remove)
7731.31Satatat			cp /dev/null $_cur
7741.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
7751.36Satatat			rcs -q -kb -U $_cur
7761.31Satatat			chown root:wheel $_cur $_cur,v
7771.31Satatat			rm $_cur
7781.31Satatat			;;
7791.31Satatat		esac
7801.31Satatat	else
7811.31Satatat		case $_action in
7821.31Satatat		add|update)
7831.31Satatat			if [ -f $_cur ]; then
7841.31Satatat				cp -p $_cur $_back
7851.31Satatat			fi
7861.31Satatat			cp -p $_file $_cur
7871.31Satatat			chown root:wheel $_cur
7881.31Satatat			;;
7891.31Satatat		remove)
7901.31Satatat			mv -f $_cur $_back
7911.31Satatat			;;
7921.31Satatat		esac
7931.31Satatat	fi
7941.1Scjs}
795