rc.subr revision 1.45
11.45Slukem# $NetBSD: rc.subr,v 1.45 2002/03/21 12:21:00 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.45Slukem# check_pidfile pidfile procname [interpreter]
1121.45Slukem#	Parses the first line of pidfile for a PID, and ensures
1131.11Slukem#	that the process is running and matches procname.
1141.45Slukem#	Prints the matching PID upon success, nothing otherwise.
1151.45Slukem#	interpreter is optional; see _find_processes() for details.
1161.11Slukem#
1171.11Slukemcheck_pidfile()
1181.11Slukem{
1191.11Slukem	_pidfile=$1
1201.11Slukem	_procname=$2
1211.45Slukem	_interpreter=$3
1221.11Slukem	if [ -z "$_pidfile" -o -z "$_procname" ]; then
1231.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
1241.11Slukem	fi
1251.11Slukem	if [ ! -f $_pidfile ]; then
1261.11Slukem		return
1271.11Slukem	fi
1281.11Slukem	read _pid _junk < $_pidfile
1291.11Slukem	if [ -z "$_pid" ]; then
1301.11Slukem		return
1311.11Slukem	fi
1321.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
1331.11Slukem}
1341.11Slukem
1351.11Slukem#
1361.45Slukem# check_process procname [interpreter]
1371.11Slukem#	Ensures that a process (or processes) named procname is running.
1381.45Slukem#	Prints a list of matching PIDs.
1391.45Slukem#	interpreter is optional; see _find_processes() for details.
1401.11Slukem#
1411.11Slukemcheck_process()
1421.11Slukem{
1431.11Slukem	_procname=$1
1441.45Slukem	_interpreter=$2
1451.11Slukem	if [ -z "$_procname" ]; then
1461.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
1471.45Slukem	fi
1481.45Slukem	_find_processes $_procname ${_interpreter:-.} '-ax'
1491.45Slukem}
1501.45Slukem
1511.45Slukem# 
1521.45Slukem# _find_processes procname interpreter psargs
1531.45Slukem#	Search for procname in the output of ps generated by psargs.
1541.45Slukem#	Prints the PIDs of any matching processes, space separated.
1551.45Slukem#
1561.45Slukem#	If interpreter == ".", check the following variations of procname
1571.45Slukem#	against the first word of each command:
1581.45Slukem#		procname
1591.45Slukem#		`basename procname`
1601.45Slukem#		`basename procname` + ":"
1611.45Slukem#		"(" + `basename procname` + ")"
1621.45Slukem#
1631.45Slukem#	If interpreter != ".", read the first line of procname, remove the
1641.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
1651.45Slukem#	match that against each command, either as is, or with extra words
1661.45Slukem#	at the end.
1671.45Slukem#
1681.45Slukem_find_processes()
1691.45Slukem{
1701.45Slukem	if [ $# -ne 3 ]; then
1711.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
1721.11Slukem	fi
1731.45Slukem	_procname=$1
1741.45Slukem	_interpreter=$2
1751.45Slukem	_psargs=$3
1761.45Slukem
1771.11Slukem	_pref=
1781.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
1791.45Slukem		read _interp < $_procname	# read interpreter name
1801.45Slukem		_interp=${_interp#\#!}		# strip #!
1811.45Slukem		set -- $_interp
1821.45Slukem		if [ $_interpreter != $1 ]; then
1831.45Slukem			warn "\$command_interpreter $_interpreter != $1"
1841.45Slukem		fi
1851.45Slukem		_interp="$* $_procname"		# cleanup spaces, add _procname
1861.45Slukem		_fp_args='_argv'
1871.45Slukem		_fp_match='case "$_argv" in
1881.45Slukem		    ${_interp}|"${_interp} "*)'
1891.45Slukem	else					# a normal daemon
1901.45Slukem		_procnamebn=${_procname##*/}
1911.45Slukem		_fp_args='_arg0 _argv'
1921.45Slukem		_fp_match='case "$_arg0" in
1931.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
1941.45Slukem	fi
1951.45Slukem
1961.45Slukem	_proccheck='
1971.45Slukem		ps -o "pid,command" '"$_psargs"' | 
1981.45Slukem		while read _npid '"$_fp_args"'; do
1991.45Slukem			case "$_npid" in
2001.45Slukem			    PID)
2011.45Slukem				continue ;;
2021.45Slukem			esac ; '"$_fp_match"'
2031.45Slukem				echo -n "$_pref$_npid" ;
2041.45Slukem				_pref=" "
2051.45Slukem				;;
2061.45Slukem			esac
2071.45Slukem		done'
2081.45Slukem
2091.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
2101.45Slukem	eval $_proccheck
2111.11Slukem}
2121.11Slukem
2131.11Slukem#
2141.33Slukem# wait_for_pids pid [pid ...]
2151.35Slukem#	spins until none of the pids exist
2161.33Slukem#
2171.33Slukemwait_for_pids()
2181.33Slukem{
2191.33Slukem	_list=$*
2201.33Slukem	if [ -z "$_list" ]; then
2211.33Slukem		return
2221.33Slukem	fi
2231.35Slukem	_prefix=
2241.35Slukem	while true; do
2251.33Slukem		_nlist="";
2261.33Slukem		for _j in $_list; do
2271.33Slukem			if kill -0 $_j 2>/dev/null; then
2281.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
2291.33Slukem			fi
2301.33Slukem		done
2311.33Slukem		if [ -z "$_nlist" ]; then
2321.33Slukem			break
2331.33Slukem		fi
2341.33Slukem		_list=$_nlist
2351.35Slukem		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
2361.35Slukem		_prefix=", "
2371.35Slukem		sleep 2
2381.33Slukem	done
2391.35Slukem	if [ -n "$_prefix" ]; then
2401.35Slukem		echo "."
2411.35Slukem	fi
2421.33Slukem}
2431.33Slukem
2441.33Slukem#
2451.16Slukem# run_rc_command arg
2461.16Slukem#	Search for arg in the list of supported commands, which is:
2471.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
2481.11Slukem#	If there's a match, run ${arg}_cmd or the default command (see below).
2491.11Slukem#
2501.16Slukem#	If arg has a given prefix, then change the operation as follows:
2511.11Slukem#		prefix	operation
2521.11Slukem#		------	---------
2531.11Slukem#		fast	Skip the pid check.
2541.11Slukem#		force	Set ${rcvar} to YES.
2551.11Slukem#
2561.11Slukem#	The following globals are used:
2571.24Slukem#
2581.11Slukem#	name		needed	function
2591.11Slukem#	----		------	--------
2601.11Slukem#	name		y	Name of script.
2611.24Slukem#
2621.11Slukem#	command		n	Full path to command.
2631.11Slukem#				Not needed if ${arg}_cmd is set for
2641.11Slukem#				each keyword.
2651.24Slukem#
2661.11Slukem#	command_args	n	Optional args/shell directives for command.
2671.24Slukem#
2681.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
2691.45Slukem#				call check_{pidfile,process}() appropriately.
2701.45Slukem#
2711.16Slukem#	extra_commands	n	List of extra commands supported.
2721.24Slukem#
2731.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
2741.42Slukem#				otherwise use check_process $command.
2751.42Slukem#				In either case, only check if $command is set.
2761.42Slukem#
2771.42Slukem#	procname	n	Process name to check for instead of $command.
2781.24Slukem#
2791.24Slukem#	rcvar		n	This is checked with checkyesno to determine
2801.24Slukem#				if the action should be run.
2811.24Slukem#
2821.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
2831.42Slukem#				Requires /usr to be mounted.
2841.24Slukem#
2851.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
2861.22Slukem#				(if not using ${name}_chroot).
2871.24Slukem#
2881.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
2891.24Slukem#				NOTE:	$flags from the parent environment
2901.24Slukem#					can be used to override this.
2911.24Slukem#
2921.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
2931.24Slukem#
2941.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
2951.22Slukem#				using ${name}_chroot.
2961.42Slukem#				Requires /usr to be mounted.
2971.24Slukem#
2981.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
2991.42Slukem#				Requires /usr to be mounted.
3001.24Slukem#
3011.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
3021.32Slukem#				to run the chrooted ${command} with.
3031.42Slukem#				Requires /usr to be mounted.
3041.24Slukem#
3051.11Slukem#	${_arg}_cmd	n	If set, use this as the action when invoked;
3061.11Slukem#				$_arg is available to the action to use.
3071.11Slukem#				Otherwise, use default command (see below)
3081.24Slukem#
3091.11Slukem#	${_arg}_precmd	n	If set, run just before performing the main
3101.11Slukem#				action in the default command (i.e, after
3111.11Slukem#				checking for required bits and process
3121.41Slukem#				(non)existence).
3131.11Slukem#				If this completes with a non-zero exit code,
3141.11Slukem#				don't run ${_arg}_cmd.
3151.24Slukem#
3161.43Slukem#	${_arg}_postcmd	n	If set, run just after performing the main
3171.43Slukem#				action in the default command, if that action
3181.43Slukem#				returned a zero exit code.
3191.43Slukem#
3201.11Slukem#	required_dirs	n	If set, check for the existence of the given
3211.11Slukem#				directories before running the default
3221.11Slukem#				(re)start command.
3231.24Slukem#
3241.11Slukem#	required_files	n	If set, check for the readability of the given
3251.11Slukem#				files before running the default (re)start
3261.11Slukem#				command.
3271.24Slukem#
3281.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
3291.11Slukem#				listed variables before running the default
3301.11Slukem#				(re)start command.
3311.11Slukem#
3321.11Slukem#	Default commands for a given arg:
3331.24Slukem#
3341.11Slukem#	arg		default
3351.11Slukem#	---		-------
3361.11Slukem#	start		if !running && checkyesno ${rcvar}
3371.11Slukem#				${command}
3381.24Slukem#
3391.11Slukem#	stop		if ${pidfile}
3401.42Slukem#				_pid=$(check_pidfile $pidfile $command)
3411.11Slukem#			else
3421.42Slukem#				_pid=$(check_process $command)
3431.35Slukem#			kill $sig_stop $_pid
3441.35Slukem#			wait_for_pids $_pid
3451.35Slukem#			($sig_stop defaults to TERM.)
3461.24Slukem#
3471.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
3481.35Slukem#			and doesn't wait_for_pids.
3491.11Slukem#			$sig_reload defaults to HUP.
3501.24Slukem#
3511.11Slukem#	restart		Run `stop' then `start'.
3521.11Slukem#
3531.33Slukem#	status		Show if ${command} is running, etc.
3541.33Slukem#
3551.33Slukem#	poll		Wait for ${command} to exit.
3561.33Slukem#
3571.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
3581.33Slukem#
3591.33Slukem#
3601.24Slukem#
3611.11Slukemrun_rc_command()
3621.11Slukem{
3631.11Slukem	_arg=$1
3641.24Slukem	if [ -z "$name" ]; then
3651.30Slukem		err 3 'run_rc_command: $name is not set.'
3661.11Slukem	fi
3671.11Slukem
3681.11Slukem	case "$_arg" in
3691.24Slukem	fast*)				# "fast" prefix; don't check pid
3701.11Slukem		_arg=${_arg#fast}
3711.11Slukem		_rc_fast_run=YES
3721.11Slukem		;;
3731.24Slukem	force*)				# "force prefix; always start
3741.11Slukem		_arg=${_arg#force}
3751.24Slukem		_rc_force_run=YES
3761.29Slukem		if [ -n "${rcvar}" ]; then
3771.29Slukem			eval ${rcvar}=YES
3781.29Slukem		fi
3791.11Slukem		;;
3801.11Slukem	esac
3811.11Slukem
3821.16Slukem	_keywords="start stop restart rcvar $extra_commands"
3831.17Slukem	_pid=
3841.11Slukem	_pidcmd=
3851.42Slukem	_procname=${procname:-${command}}
3861.42Slukem
3871.24Slukem					# setup pid check command if not fast
3881.42Slukem	if [ -z "$_rc_fast_run" -a -n "$_procname" ]; then
3891.11Slukem		if [ -n "$pidfile" ]; then
3901.45Slukem			_pidcmd='_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
3911.42Slukem		else
3921.45Slukem			_pidcmd='_pid=$(check_process '"$_procname $command_interpreter"')'
3931.11Slukem		fi
3941.11Slukem		if [ -n "$_pidcmd" ]; then
3951.33Slukem			_keywords="${_keywords} status poll"
3961.11Slukem		fi
3971.11Slukem	fi
3981.11Slukem
3991.11Slukem	if [ -z "$_arg" ]; then
4001.11Slukem		rc_usage "$_keywords"
4011.11Slukem	fi
4021.11Slukem
4031.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
4041.11Slukem		_flags=$flags
4051.11Slukem	else
4061.11Slukem		eval _flags=\$${name}_flags
4071.11Slukem	fi
4081.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
4091.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
4101.30Slukem	    _group=\$${name}_group	_groups=\$${name}_groups
4111.11Slukem
4121.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
4131.42Slukem		if [ "$_user" = "$(id -un)" ]; then
4141.42Slukem			unset _user
4151.42Slukem		fi
4161.42Slukem	fi
4171.42Slukem
4181.29Slukem					# if ${rcvar} is set, and $1 is not
4191.40Slukem					# "rcvar", then run
4201.29Slukem					#	checkyesno ${rcvar}
4211.29Slukem					# and return if that failed
4221.24Slukem					#
4231.40Slukem	if [ -n "${rcvar}" -a "$_arg" != "rcvar" ]; then
4241.24Slukem		if ! checkyesno ${rcvar}; then
4251.24Slukem			return 0
4261.24Slukem		fi
4271.24Slukem	fi
4281.24Slukem
4291.24Slukem	eval $_pidcmd			# determine the pid if necessary
4301.11Slukem
4311.11Slukem	for _elem in $_keywords; do
4321.11Slukem		if [ "$_elem" != "$_arg" ]; then
4331.11Slukem			continue
4341.11Slukem		fi
4351.11Slukem
4361.24Slukem					# if there's a custom ${XXX_cmd},
4371.24Slukem					# run that instead of the default
4381.24Slukem					#
4391.43Slukem		eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd \
4401.43Slukem		    _postcmd=\$${_arg}_postcmd
4411.11Slukem		if [ -n "$_cmd" ]; then
4421.24Slukem					# if the precmd failed and force
4431.24Slukem					# isn't set, exit
4441.24Slukem					#
4451.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
4461.24Slukem				return 1
4471.24Slukem			fi
4481.24Slukem
4491.44Slukem			if ! eval $_cmd && [ -z "$_rc_force_run" ]; then
4501.44Slukem				return 1
4511.44Slukem			fi
4521.44Slukem			eval $_postcmd
4531.11Slukem			return 0
4541.11Slukem		fi
4551.11Slukem
4561.24Slukem		case "$_arg" in		# default operations...
4571.11Slukem
4581.11Slukem		status)
4591.11Slukem			if [ -n "$_pid" ]; then
4601.11Slukem				echo "${name} is running as pid $_pid."
4611.11Slukem			else
4621.11Slukem				echo "${name} is not running."
4631.28Slukem				return 1
4641.11Slukem			fi
4651.11Slukem			;;
4661.11Slukem
4671.11Slukem		start)
4681.11Slukem			if [ -n "$_pid" ]; then
4691.11Slukem				echo "${name} already running? (pid=$_pid)."
4701.11Slukem				exit 1
4711.11Slukem			fi
4721.11Slukem
4731.24Slukem			if [ ! -x $command ]; then
4741.11Slukem				return 0
4751.11Slukem			fi
4761.11Slukem
4771.24Slukem					# check for required variables,
4781.24Slukem					# directories, and files
4791.24Slukem					#
4801.11Slukem			for _f in $required_vars; do
4811.11Slukem				if ! checkyesno $_f; then
4821.24Slukem					warn "\$${_f} is not set."
4831.24Slukem					if [ -z "$_rc_force_run" ]; then
4841.24Slukem						return 1
4851.24Slukem					fi
4861.11Slukem				fi
4871.11Slukem			done
4881.11Slukem			for _f in $required_dirs; do
4891.11Slukem				if [ ! -d "${_f}/." ]; then
4901.25Slukem					warn "${_f} is not a directory."
4911.24Slukem					if [ -z "$_rc_force_run" ]; then
4921.24Slukem						return 1
4931.24Slukem					fi
4941.11Slukem				fi
4951.11Slukem			done
4961.11Slukem			for _f in $required_files; do
4971.11Slukem				if [ ! -r "${_f}" ]; then
4981.25Slukem					warn "${_f} is not readable."
4991.24Slukem					if [ -z "$_rc_force_run" ]; then
5001.24Slukem						return 1
5011.24Slukem					fi
5021.11Slukem				fi
5031.11Slukem			done
5041.11Slukem
5051.24Slukem					# if the precmd failed and force
5061.24Slukem					# isn't set, exit
5071.24Slukem					#
5081.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5091.24Slukem				return 1
5101.24Slukem			fi
5111.24Slukem
5121.24Slukem					# setup the command to run, and run it
5131.29Slukem					#
5141.11Slukem			echo "Starting ${name}."
5151.22Slukem			if [ -n "$_chroot" ]; then
5161.22Slukem				_doit="\
5171.23Slukem${_nice:+nice -n $_nice }\
5181.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
5191.22Slukem$_chroot $command $_flags $command_args"
5201.22Slukem			else
5211.22Slukem				_doit="\
5221.18Slukem${_chdir:+cd $_chdir; }\
5231.18Slukem${_nice:+nice -n $_nice }\
5241.34Slukem$command $_flags $command_args"
5251.34Slukem				if [ -n "$_user" ]; then
5261.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
5271.34Slukem				fi
5281.22Slukem			fi
5291.43Slukem
5301.43Slukem					# if the cmd failed and force
5311.43Slukem					# isn't set, exit
5321.43Slukem					#
5331.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
5341.43Slukem				return 1
5351.43Slukem			fi
5361.43Slukem
5371.43Slukem					# finally, run postcmd
5381.43Slukem					#
5391.43Slukem			eval $_postcmd
5401.11Slukem			;;
5411.11Slukem
5421.11Slukem		stop)
5431.11Slukem			if [ -z "$_pid" ]; then
5441.24Slukem				if [ -n "$pidfile" ]; then
5451.24Slukem					echo \
5461.24Slukem				    "${name} not running? (check $pidfile)."
5471.24Slukem				else
5481.24Slukem					echo "${name} not running?"
5491.11Slukem				fi
5501.24Slukem				exit 1
5511.11Slukem			fi
5521.11Slukem
5531.43Slukem					# if the precmd failed and force
5541.43Slukem					# isn't set, exit
5551.43Slukem					#
5561.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5571.24Slukem				return 1
5581.24Slukem			fi
5591.43Slukem
5601.43Slukem					# send the signal to stop
5611.43Slukem					#
5621.11Slukem			echo "Stopping ${name}."
5631.34Slukem			_doit="kill -${sig_stop:-TERM} $_pid"
5641.34Slukem			if [ -n "$_user" ]; then
5651.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5661.34Slukem			fi
5671.43Slukem
5681.43Slukem					# if the stop cmd failed and force
5691.43Slukem					# isn't set, exit
5701.43Slukem					#
5711.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
5721.43Slukem				return 1
5731.43Slukem			fi
5741.43Slukem
5751.43Slukem					# wait for the command to exit,
5761.43Slukem					# and run postcmd.
5771.35Slukem			wait_for_pids $_pid
5781.43Slukem			eval $_postcmd
5791.11Slukem			;;
5801.11Slukem
5811.11Slukem		reload)
5821.11Slukem			if [ -z "$_pid" ]; then
5831.24Slukem				if [ -n "$pidfile" ]; then
5841.24Slukem					echo \
5851.11Slukem				    "${name} not running? (check $pidfile)."
5861.24Slukem				else
5871.24Slukem					echo "${name} not running?"
5881.11Slukem				fi
5891.24Slukem				exit 1
5901.11Slukem			fi
5911.11Slukem			echo "Reloading ${name} config files."
5921.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
5931.24Slukem				return 1
5941.24Slukem			fi
5951.34Slukem			_doit="kill -${sig_reload:-HUP} $_pid"
5961.34Slukem			if [ -n "$_user" ]; then
5971.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
5981.34Slukem			fi
5991.43Slukem			if ! eval $_doit && [ -z "$_rc_force_run" ]; then
6001.43Slukem				return 1
6011.43Slukem			fi
6021.43Slukem			eval $_postcmd
6031.11Slukem			;;
6041.11Slukem
6051.11Slukem		restart)
6061.24Slukem			if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
6071.24Slukem				return 1
6081.11Slukem			fi
6091.29Slukem					# prevent restart being called more
6101.29Slukem					# than once by any given script
6111.29Slukem					#
6121.29Slukem			if [ -n "$_rc_restart_done" ]; then
6131.29Slukem				return 0
6141.29Slukem			fi
6151.29Slukem			_rc_restart_done=YES
6161.33Slukem
6171.27Slukem			( $0 ${_rc_force_run:+force}stop )
6181.27Slukem			$0 ${_rc_force_run:+force}start
6191.11Slukem
6201.43Slukem			eval $_postcmd
6211.33Slukem			;;
6221.33Slukem
6231.33Slukem		poll)
6241.33Slukem			if [ -n "$_pid" ]; then
6251.33Slukem				wait_for_pids $_pid
6261.33Slukem			fi
6271.11Slukem			;;
6281.11Slukem
6291.11Slukem		rcvar)
6301.11Slukem			echo "# $name"
6311.24Slukem			if [ -n "$rcvar" ]; then
6321.24Slukem				if checkyesno ${rcvar}; then
6331.24Slukem					echo "\$${rcvar}=YES"
6341.24Slukem				else
6351.24Slukem					echo "\$${rcvar}=NO"
6361.24Slukem				fi
6371.11Slukem			fi
6381.11Slukem			;;
6391.11Slukem
6401.11Slukem		*)
6411.11Slukem			rc_usage "$_keywords"
6421.11Slukem			;;
6431.11Slukem
6441.11Slukem		esac
6451.11Slukem		return 0
6461.11Slukem	done
6471.11Slukem
6481.11Slukem	echo 1>&2 "$0: unknown directive '$_arg'."
6491.11Slukem	rc_usage "$_keywords"
6501.11Slukem	exit 1
6511.11Slukem}
6521.11Slukem
6531.11Slukem#
6541.11Slukem# run_rc_script file arg
6551.11Slukem#	Start the script `file' with `arg', and correctly handle the
6561.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
6571.37Slukem#	sourced into the current environment.  If `file' appears to be
6581.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
6591.37Slukem#	executable run as a child process.
6601.17Slukem#
6611.11Slukemrun_rc_script()
6621.11Slukem{
6631.11Slukem	_file=$1
6641.11Slukem	_arg=$2
6651.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
6661.11Slukem		err 3 'USAGE: run_rc_script file arg'
6671.11Slukem	fi
6681.11Slukem
6691.45Slukem	unset	name command command_args command_interpreter \
6701.45Slukem		extra_commands pidfile procname \
6711.42Slukem		rcvar required_dirs required_files required_vars
6721.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
6731.39Slukem
6741.39Slukem	case "$_file" in
6751.39Slukem	*.sh)				# run in current shell
6761.11Slukem		set $_arg ; . $_file
6771.39Slukem		;;
6781.39Slukem	*[~#]|*.OLD|*.orig)		# scratch file; skip
6791.39Slukem		warn "Ignoring scratch file $_file"
6801.39Slukem		;;
6811.39Slukem	*)				# run in subshell
6821.39Slukem		if [ -x $_file ]; then
6831.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
6841.39Slukem				set $_arg ; . $_file
6851.39Slukem			else
6861.37Slukem				( set $_arg ; . $_file )
6871.37Slukem			fi
6881.39Slukem		fi
6891.39Slukem		;;
6901.39Slukem	esac
6911.11Slukem}
6921.19Slukem
6931.19Slukem#
6941.19Slukem# load_rc_config
6951.19Slukem#	Source in the configuration file for a given command.
6961.19Slukem#
6971.19Slukemload_rc_config()
6981.19Slukem{
6991.19Slukem	_command=$1
7001.19Slukem	if [ -z "$_command" ]; then
7011.19Slukem		err 3 'USAGE: load_rc_config command'
7021.19Slukem	fi
7031.19Slukem
7041.30Slukem	if [ -z "$_rc_conf_loaded" ]; then
7051.30Slukem		. /etc/rc.conf
7061.30Slukem		_rc_conf_loaded=YES
7071.30Slukem	fi
7081.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
7091.20Sfvdl		. /etc/rc.conf.d/"$_command"
7101.20Sfvdl	fi
7111.19Slukem}
7121.19Slukem
7131.11Slukem
7141.11Slukem#
7151.11Slukem# rc_usage commands
7161.11Slukem#	Print a usage string for $0, with `commands' being a list of
7171.11Slukem#	valid commands.
7181.11Slukem#
7191.11Slukemrc_usage()
7201.11Slukem{
7211.11Slukem	echo -n 1>&2 "Usage: $0 [fast|force]("
7221.11Slukem
7231.11Slukem	_sep=
7241.11Slukem	for _elem in $*; do
7251.11Slukem		echo -n 1>&2 "$_sep$_elem"
7261.11Slukem		_sep="|"
7271.11Slukem	done
7281.11Slukem	echo 1>&2 ")"
7291.11Slukem	exit 1
7301.11Slukem}
7311.11Slukem
7321.11Slukem#
7331.11Slukem# err exitval message
7341.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
7351.11Slukem#
7361.11Slukemerr()
7371.11Slukem{
7381.11Slukem	exitval=$1
7391.11Slukem	shift
7401.11Slukem
7411.21Slukem	logger "$0: ERROR: $*"
7421.21Slukem	echo 1>&2 "$0: ERROR: $*"
7431.11Slukem	exit $exitval
7441.11Slukem}
7451.11Slukem
7461.11Slukem#
7471.11Slukem# warn message
7481.11Slukem#	Display message to stderr and log to the syslog.
7491.11Slukem#
7501.11Slukemwarn()
7511.11Slukem{
7521.21Slukem	logger "$0: WARNING: $*"
7531.21Slukem	echo 1>&2 "$0: WARNING: $*"
7541.31Satatat}
7551.31Satatat
7561.31Satatat#
7571.31Satatat# backup_file action file cur backup
7581.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
7591.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
7601.31Satatat#
7611.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
7621.31Satatat#	which can be either YES or NO.
7631.31Satatat#
7641.31Satatat#	The `action' keyword can be one of the following:
7651.31Satatat#
7661.31Satatat#	add		`file' is now being backed up (and is possibly
7671.31Satatat#			being reentered into the backups system).  `cur'
7681.31Satatat#			is created and RCS files, if necessary, are
7691.31Satatat#			created as well.
7701.31Satatat#
7711.31Satatat#	update		`file' has changed and needs to be backed up.
7721.31Satatat#			If `cur' exists, it is copied to to `back' or
7731.31Satatat#			checked into RCS (if the repository file is old),
7741.31Satatat#			and then `file' is copied to `cur'.  Another RCS
7751.31Satatat#			check in done here if RCS is being used.
7761.31Satatat#
7771.31Satatat#	remove		`file' is no longer being tracked by the backups
7781.31Satatat#			system.  If RCS is not being used, `cur' is moved
7791.31Satatat#			to `back', otherwise an empty file is checked in,
7801.31Satatat#			and then `cur' is removed.
7811.31Satatat#
7821.31Satatat#
7831.31Satatatbackup_file()
7841.31Satatat{
7851.31Satatat	_action=$1
7861.31Satatat	_file=$2
7871.31Satatat	_cur=$3
7881.31Satatat	_back=$4
7891.31Satatat
7901.31Satatat	if checkyesno backup_uses_rcs; then
7911.31Satatat		_msg0="backup archive"
7921.31Satatat		_msg1="update"
7931.31Satatat
7941.36Satatat		# ensure that history file is not locked
7951.36Satatat		if [ -f $_cur,v ]; then
7961.36Satatat			rcs -q -u -U -M $_cur
7971.36Satatat		fi
7981.36Satatat
7991.31Satatat		# ensure after switching to rcs that the
8001.31Satatat		# current backup is not lost
8011.31Satatat		if [ -f $_cur ]; then
8021.31Satatat			# no archive, or current newer than archive
8031.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
8041.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8051.36Satatat				rcs -q -kb -U $_cur
8061.31Satatat			fi
8071.31Satatat		fi
8081.31Satatat
8091.31Satatat		case $_action in
8101.31Satatat		add|update)
8111.31Satatat			cp -p $_file $_cur
8121.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8131.36Satatat			rcs -q -kb -U $_cur
8141.31Satatat			chown root:wheel $_cur $_cur,v
8151.31Satatat			;;
8161.31Satatat		remove)
8171.31Satatat			cp /dev/null $_cur
8181.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8191.36Satatat			rcs -q -kb -U $_cur
8201.31Satatat			chown root:wheel $_cur $_cur,v
8211.31Satatat			rm $_cur
8221.31Satatat			;;
8231.31Satatat		esac
8241.31Satatat	else
8251.31Satatat		case $_action in
8261.31Satatat		add|update)
8271.31Satatat			if [ -f $_cur ]; then
8281.31Satatat				cp -p $_cur $_back
8291.31Satatat			fi
8301.31Satatat			cp -p $_file $_cur
8311.31Satatat			chown root:wheel $_cur
8321.31Satatat			;;
8331.31Satatat		remove)
8341.31Satatat			mv -f $_cur $_back
8351.31Satatat			;;
8361.31Satatat		esac
8371.31Satatat	fi
8381.1Scjs}
839