rc.subr revision 1.61
11.61Slukem# $NetBSD: rc.subr,v 1.61 2004/01/06 00:52:24 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.52Sabs		warn "\$${1} is not set properly - see rc.conf(5)."
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.56Schristos	for _revfile; do
781.38Slukem		_revlist="$_revfile $_revlist"
791.38Slukem	done
801.38Slukem	echo $_revlist
811.6Smellon}
821.6Smellon
831.6Smellon#
841.47Slukem# mount_critical_filesystems type
851.47Slukem#	Go through the list of critical filesystems as provided in
861.47Slukem#	the rc.conf(5) variable $critical_filesystems_${type}, checking
871.47Slukem#	each one to see if it is mounted, and if it is not, mounting it.
881.6Smellon#
891.11Slukemmount_critical_filesystems()
901.11Slukem{
911.47Slukem	eval _fslist=\$critical_filesystems_${1}
921.17Slukem	for _fs in $_fslist; do
931.6Smellon		mount | (
941.53Slukem			_ismounted=false
951.6Smellon			while read what _on on _type type; do
961.17Slukem				if [ $on = $_fs ]; then
971.53Slukem					_ismounted=true
981.6Smellon				fi
991.6Smellon			done
1001.53Slukem			if $_ismounted; then
1011.55Slukem				:
1021.55Slukem			else
1031.17Slukem				mount $_fs >/dev/null 2>&1
1041.6Smellon			fi
1051.46Slukem		)
1061.6Smellon	done
1071.7Scjs}
1081.7Scjs
1091.11Slukem#
1101.45Slukem# check_pidfile pidfile procname [interpreter]
1111.45Slukem#	Parses the first line of pidfile for a PID, and ensures
1121.11Slukem#	that the process is running and matches procname.
1131.45Slukem#	Prints the matching PID upon success, nothing otherwise.
1141.45Slukem#	interpreter is optional; see _find_processes() for details.
1151.11Slukem#
1161.11Slukemcheck_pidfile()
1171.11Slukem{
1181.11Slukem	_pidfile=$1
1191.11Slukem	_procname=$2
1201.45Slukem	_interpreter=$3
1211.11Slukem	if [ -z "$_pidfile" -o -z "$_procname" ]; then
1221.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
1231.11Slukem	fi
1241.11Slukem	if [ ! -f $_pidfile ]; then
1251.11Slukem		return
1261.11Slukem	fi
1271.11Slukem	read _pid _junk < $_pidfile
1281.11Slukem	if [ -z "$_pid" ]; then
1291.11Slukem		return
1301.11Slukem	fi
1311.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
1321.11Slukem}
1331.11Slukem
1341.11Slukem#
1351.45Slukem# check_process procname [interpreter]
1361.11Slukem#	Ensures that a process (or processes) named procname is running.
1371.45Slukem#	Prints a list of matching PIDs.
1381.45Slukem#	interpreter is optional; see _find_processes() for details.
1391.11Slukem#
1401.11Slukemcheck_process()
1411.11Slukem{
1421.11Slukem	_procname=$1
1431.45Slukem	_interpreter=$2
1441.11Slukem	if [ -z "$_procname" ]; then
1451.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
1461.45Slukem	fi
1471.45Slukem	_find_processes $_procname ${_interpreter:-.} '-ax'
1481.45Slukem}
1491.45Slukem
1501.46Slukem#
1511.45Slukem# _find_processes procname interpreter psargs
1521.45Slukem#	Search for procname in the output of ps generated by psargs.
1531.45Slukem#	Prints the PIDs of any matching processes, space separated.
1541.45Slukem#
1551.45Slukem#	If interpreter == ".", check the following variations of procname
1561.45Slukem#	against the first word of each command:
1571.45Slukem#		procname
1581.45Slukem#		`basename procname`
1591.45Slukem#		`basename procname` + ":"
1601.45Slukem#		"(" + `basename procname` + ")"
1611.45Slukem#
1621.45Slukem#	If interpreter != ".", read the first line of procname, remove the
1631.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
1641.45Slukem#	match that against each command, either as is, or with extra words
1651.45Slukem#	at the end.
1661.45Slukem#
1671.45Slukem_find_processes()
1681.45Slukem{
1691.45Slukem	if [ $# -ne 3 ]; then
1701.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
1711.11Slukem	fi
1721.45Slukem	_procname=$1
1731.45Slukem	_interpreter=$2
1741.45Slukem	_psargs=$3
1751.45Slukem
1761.11Slukem	_pref=
1771.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
1781.45Slukem		read _interp < $_procname	# read interpreter name
1791.45Slukem		_interp=${_interp#\#!}		# strip #!
1801.45Slukem		set -- $_interp
1811.45Slukem		if [ $_interpreter != $1 ]; then
1821.45Slukem			warn "\$command_interpreter $_interpreter != $1"
1831.45Slukem		fi
1841.45Slukem		_interp="$* $_procname"		# cleanup spaces, add _procname
1851.45Slukem		_fp_args='_argv'
1861.45Slukem		_fp_match='case "$_argv" in
1871.45Slukem		    ${_interp}|"${_interp} "*)'
1881.45Slukem	else					# a normal daemon
1891.45Slukem		_procnamebn=${_procname##*/}
1901.45Slukem		_fp_args='_arg0 _argv'
1911.45Slukem		_fp_match='case "$_arg0" in
1921.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
1931.45Slukem	fi
1941.45Slukem
1951.45Slukem	_proccheck='
1961.46Slukem		ps -o "pid,command" '"$_psargs"' |
1971.45Slukem		while read _npid '"$_fp_args"'; do
1981.45Slukem			case "$_npid" in
1991.45Slukem			    PID)
2001.45Slukem				continue ;;
2011.45Slukem			esac ; '"$_fp_match"'
2021.45Slukem				echo -n "$_pref$_npid" ;
2031.45Slukem				_pref=" "
2041.45Slukem				;;
2051.45Slukem			esac
2061.45Slukem		done'
2071.45Slukem
2081.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
2091.45Slukem	eval $_proccheck
2101.11Slukem}
2111.11Slukem
2121.11Slukem#
2131.33Slukem# wait_for_pids pid [pid ...]
2141.35Slukem#	spins until none of the pids exist
2151.33Slukem#
2161.33Slukemwait_for_pids()
2171.33Slukem{
2181.56Schristos	_list="$@"
2191.33Slukem	if [ -z "$_list" ]; then
2201.33Slukem		return
2211.33Slukem	fi
2221.35Slukem	_prefix=
2231.35Slukem	while true; do
2241.33Slukem		_nlist="";
2251.33Slukem		for _j in $_list; do
2261.33Slukem			if kill -0 $_j 2>/dev/null; then
2271.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
2281.33Slukem			fi
2291.33Slukem		done
2301.33Slukem		if [ -z "$_nlist" ]; then
2311.33Slukem			break
2321.33Slukem		fi
2331.33Slukem		_list=$_nlist
2341.35Slukem		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
2351.35Slukem		_prefix=", "
2361.35Slukem		sleep 2
2371.33Slukem	done
2381.35Slukem	if [ -n "$_prefix" ]; then
2391.35Slukem		echo "."
2401.35Slukem	fi
2411.33Slukem}
2421.33Slukem
2431.33Slukem#
2441.46Slukem# run_rc_command argument
2451.46Slukem#	Search for argument in the list of supported commands, which is:
2461.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
2471.46Slukem#	If there's a match, run ${argument}_cmd or the default method
2481.46Slukem#	(see below).
2491.11Slukem#
2501.46Slukem#	If argument has a given prefix, then change the operation as follows:
2511.46Slukem#		Prefix	Operation
2521.11Slukem#		------	---------
2531.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
2541.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
2551.61Slukem#		one	Set ${rcvar} to YES
2561.11Slukem#
2571.11Slukem#	The following globals are used:
2581.24Slukem#
2591.46Slukem#	Name		Needed	Purpose
2601.46Slukem#	----		------	-------
2611.11Slukem#	name		y	Name of script.
2621.24Slukem#
2631.11Slukem#	command		n	Full path to command.
2641.46Slukem#				Not needed if ${rc_arg}_cmd is set for
2651.11Slukem#				each keyword.
2661.24Slukem#
2671.11Slukem#	command_args	n	Optional args/shell directives for command.
2681.24Slukem#
2691.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
2701.45Slukem#				call check_{pidfile,process}() appropriately.
2711.45Slukem#
2721.16Slukem#	extra_commands	n	List of extra commands supported.
2731.24Slukem#
2741.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
2751.42Slukem#				otherwise use check_process $command.
2761.42Slukem#				In either case, only check if $command is set.
2771.42Slukem#
2781.42Slukem#	procname	n	Process name to check for instead of $command.
2791.24Slukem#
2801.24Slukem#	rcvar		n	This is checked with checkyesno to determine
2811.24Slukem#				if the action should be run.
2821.24Slukem#
2831.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
2841.42Slukem#				Requires /usr to be mounted.
2851.24Slukem#
2861.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
2871.22Slukem#				(if not using ${name}_chroot).
2881.24Slukem#
2891.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
2901.24Slukem#				NOTE:	$flags from the parent environment
2911.24Slukem#					can be used to override this.
2921.24Slukem#
2931.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
2941.24Slukem#
2951.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
2961.22Slukem#				using ${name}_chroot.
2971.42Slukem#				Requires /usr to be mounted.
2981.24Slukem#
2991.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
3001.42Slukem#				Requires /usr to be mounted.
3011.24Slukem#
3021.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
3031.32Slukem#				to run the chrooted ${command} with.
3041.42Slukem#				Requires /usr to be mounted.
3051.24Slukem#
3061.50Satatat#	${name}_systrace n	Flags passed to systrace(1) if it is used.
3071.50Satatat#				Setting this variable enables systracing
3081.50Satatat# 				of the given program.  The use of "-a" is
3091.50Satatat#				recommended so that the boot process is not
3101.50Satatat#				stalled.  In order to pass no flags to
3111.50Satatat#				systrace, set this variable to "--".
3121.50Satatat#
3131.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
3141.11Slukem#				Otherwise, use default command (see below)
3151.24Slukem#
3161.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
3171.46Slukem#				${rc_arg}_cmd method in the default
3181.46Slukem#				operation (i.e, after checking for required
3191.46Slukem#				bits and process (non)existence).
3201.11Slukem#				If this completes with a non-zero exit code,
3211.46Slukem#				don't run ${rc_arg}_cmd.
3221.24Slukem#
3231.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
3241.46Slukem#				${rc_arg}_cmd method, if that method
3251.43Slukem#				returned a zero exit code.
3261.43Slukem#
3271.11Slukem#	required_dirs	n	If set, check for the existence of the given
3281.11Slukem#				directories before running the default
3291.11Slukem#				(re)start command.
3301.24Slukem#
3311.11Slukem#	required_files	n	If set, check for the readability of the given
3321.11Slukem#				files before running the default (re)start
3331.11Slukem#				command.
3341.24Slukem#
3351.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
3361.11Slukem#				listed variables before running the default
3371.11Slukem#				(re)start command.
3381.11Slukem#
3391.46Slukem#	Default behaviour for a given argument, if no override method is
3401.46Slukem#	provided:
3411.24Slukem#
3421.46Slukem#	Argument	Default behaviour
3431.46Slukem#	--------	-----------------
3441.11Slukem#	start		if !running && checkyesno ${rcvar}
3451.11Slukem#				${command}
3461.24Slukem#
3471.11Slukem#	stop		if ${pidfile}
3481.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
3491.11Slukem#			else
3501.46Slukem#				rc_pid=$(check_process $command)
3511.46Slukem#			kill $sig_stop $rc_pid
3521.46Slukem#			wait_for_pids $rc_pid
3531.35Slukem#			($sig_stop defaults to TERM.)
3541.24Slukem#
3551.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
3561.35Slukem#			and doesn't wait_for_pids.
3571.11Slukem#			$sig_reload defaults to HUP.
3581.24Slukem#
3591.11Slukem#	restart		Run `stop' then `start'.
3601.11Slukem#
3611.33Slukem#	status		Show if ${command} is running, etc.
3621.33Slukem#
3631.33Slukem#	poll		Wait for ${command} to exit.
3641.33Slukem#
3651.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
3661.33Slukem#
3671.46Slukem#	Variables available to methods, and after run_rc_command() has
3681.46Slukem#	completed:
3691.46Slukem#
3701.46Slukem#	Variable	Purpose
3711.46Slukem#	--------	-------
3721.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
3731.46Slukem#			performed
3741.46Slukem#
3751.46Slukem#	rc_flags	Flags to start the default command with.
3761.46Slukem#			Defaults to ${name}_flags, unless overridden
3771.46Slukem#			by $flags from the environment.
3781.46Slukem#			This variable may be changed by the precmd method.
3791.46Slukem#
3801.46Slukem#	rc_pid		PID of command (if appropriate)
3811.46Slukem#
3821.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
3831.46Slukem#
3841.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
3851.33Slukem#
3861.24Slukem#
3871.11Slukemrun_rc_command()
3881.11Slukem{
3891.46Slukem	rc_arg=$1
3901.24Slukem	if [ -z "$name" ]; then
3911.30Slukem		err 3 'run_rc_command: $name is not set.'
3921.11Slukem	fi
3931.11Slukem
3941.61Slukem	_rc_prefix=
3951.46Slukem	case "$rc_arg" in
3961.24Slukem	fast*)				# "fast" prefix; don't check pid
3971.46Slukem		rc_arg=${rc_arg#fast}
3981.48Slukem		rc_fast=yes
3991.11Slukem		;;
4001.61Slukem	force*)				# "force" prefix; always run
4011.48Slukem		rc_force=yes
4021.61Slukem		_rc_prefix=force
4031.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
4041.61Slukem		if [ -n "${rcvar}" ]; then
4051.61Slukem			eval ${rcvar}=YES
4061.61Slukem		fi
4071.61Slukem		;;
4081.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
4091.61Slukem		_rc_prefix=one
4101.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
4111.29Slukem		if [ -n "${rcvar}" ]; then
4121.29Slukem			eval ${rcvar}=YES
4131.29Slukem		fi
4141.11Slukem		;;
4151.11Slukem	esac
4161.11Slukem
4171.16Slukem	_keywords="start stop restart rcvar $extra_commands"
4181.46Slukem	rc_pid=
4191.11Slukem	_pidcmd=
4201.42Slukem	_procname=${procname:-${command}}
4211.42Slukem
4221.24Slukem					# setup pid check command if not fast
4231.46Slukem	if [ -z "$rc_fast" -a -n "$_procname" ]; then
4241.11Slukem		if [ -n "$pidfile" ]; then
4251.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
4261.42Slukem		else
4271.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
4281.11Slukem		fi
4291.11Slukem		if [ -n "$_pidcmd" ]; then
4301.33Slukem			_keywords="${_keywords} status poll"
4311.11Slukem		fi
4321.11Slukem	fi
4331.11Slukem
4341.46Slukem	if [ -z "$rc_arg" ]; then
4351.11Slukem		rc_usage "$_keywords"
4361.11Slukem	fi
4371.11Slukem
4381.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
4391.46Slukem		rc_flags=$flags
4401.11Slukem	else
4411.46Slukem		eval rc_flags=\$${name}_flags
4421.11Slukem	fi
4431.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
4441.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
4451.50Satatat	    _group=\$${name}_group	_groups=\$${name}_groups \
4461.50Satatat	    _systrace=\$${name}_systrace
4471.11Slukem
4481.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
4491.42Slukem		if [ "$_user" = "$(id -un)" ]; then
4501.42Slukem			unset _user
4511.42Slukem		fi
4521.42Slukem	fi
4531.42Slukem
4541.29Slukem					# if ${rcvar} is set, and $1 is not
4551.40Slukem					# "rcvar", then run
4561.29Slukem					#	checkyesno ${rcvar}
4571.29Slukem					# and return if that failed
4581.24Slukem					#
4591.46Slukem	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
4601.24Slukem		if ! checkyesno ${rcvar}; then
4611.24Slukem			return 0
4621.24Slukem		fi
4631.24Slukem	fi
4641.24Slukem
4651.24Slukem	eval $_pidcmd			# determine the pid if necessary
4661.11Slukem
4671.11Slukem	for _elem in $_keywords; do
4681.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
4691.11Slukem			continue
4701.11Slukem		fi
4711.11Slukem
4721.24Slukem					# if there's a custom ${XXX_cmd},
4731.24Slukem					# run that instead of the default
4741.24Slukem					#
4751.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
4761.46Slukem		    _postcmd=\$${rc_arg}_postcmd
4771.11Slukem		if [ -n "$_cmd" ]; then
4781.24Slukem					# if the precmd failed and force
4791.24Slukem					# isn't set, exit
4801.24Slukem					#
4811.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
4821.24Slukem				return 1
4831.24Slukem			fi
4841.24Slukem
4851.46Slukem			if ! eval $_cmd && [ -z "$rc_force" ]; then
4861.44Slukem				return 1
4871.44Slukem			fi
4881.44Slukem			eval $_postcmd
4891.11Slukem			return 0
4901.11Slukem		fi
4911.11Slukem
4921.46Slukem		case "$rc_arg" in	# default operations...
4931.11Slukem
4941.11Slukem		status)
4951.46Slukem			if [ -n "$rc_pid" ]; then
4961.46Slukem				echo "${name} is running as pid $rc_pid."
4971.11Slukem			else
4981.11Slukem				echo "${name} is not running."
4991.28Slukem				return 1
5001.11Slukem			fi
5011.11Slukem			;;
5021.11Slukem
5031.11Slukem		start)
5041.46Slukem			if [ -n "$rc_pid" ]; then
5051.46Slukem				echo "${name} already running? (pid=$rc_pid)."
5061.11Slukem				exit 1
5071.11Slukem			fi
5081.11Slukem
5091.57Slukem			if [ ! -x ${_chroot}${command} ]; then
5101.11Slukem				return 0
5111.11Slukem			fi
5121.11Slukem
5131.24Slukem					# check for required variables,
5141.24Slukem					# directories, and files
5151.24Slukem					#
5161.11Slukem			for _f in $required_vars; do
5171.11Slukem				if ! checkyesno $_f; then
5181.24Slukem					warn "\$${_f} is not set."
5191.46Slukem					if [ -z "$rc_force" ]; then
5201.24Slukem						return 1
5211.24Slukem					fi
5221.11Slukem				fi
5231.11Slukem			done
5241.11Slukem			for _f in $required_dirs; do
5251.11Slukem				if [ ! -d "${_f}/." ]; then
5261.25Slukem					warn "${_f} is not a directory."
5271.46Slukem					if [ -z "$rc_force" ]; then
5281.24Slukem						return 1
5291.24Slukem					fi
5301.11Slukem				fi
5311.11Slukem			done
5321.11Slukem			for _f in $required_files; do
5331.11Slukem				if [ ! -r "${_f}" ]; then
5341.25Slukem					warn "${_f} is not readable."
5351.46Slukem					if [ -z "$rc_force" ]; then
5361.24Slukem						return 1
5371.24Slukem					fi
5381.11Slukem				fi
5391.11Slukem			done
5401.11Slukem
5411.24Slukem					# if the precmd failed and force
5421.24Slukem					# isn't set, exit
5431.24Slukem					#
5441.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
5451.24Slukem				return 1
5461.24Slukem			fi
5471.24Slukem
5481.24Slukem					# setup the command to run, and run it
5491.29Slukem					#
5501.11Slukem			echo "Starting ${name}."
5511.22Slukem			if [ -n "$_chroot" ]; then
5521.22Slukem				_doit="\
5531.23Slukem${_nice:+nice -n $_nice }\
5541.50Satatat${_systrace:+systrace $_systrace }\
5551.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
5561.46Slukem$_chroot $command $rc_flags $command_args"
5571.22Slukem			else
5581.22Slukem				_doit="\
5591.18Slukem${_chdir:+cd $_chdir; }\
5601.18Slukem${_nice:+nice -n $_nice }\
5611.50Satatat${_systrace:+systrace $_systrace }\
5621.46Slukem$command $rc_flags $command_args"
5631.34Slukem				if [ -n "$_user" ]; then
5641.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
5651.34Slukem				fi
5661.22Slukem			fi
5671.43Slukem
5681.43Slukem					# if the cmd failed and force
5691.43Slukem					# isn't set, exit
5701.43Slukem					#
5711.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
5721.43Slukem				return 1
5731.43Slukem			fi
5741.43Slukem
5751.43Slukem					# finally, run postcmd
5761.43Slukem					#
5771.43Slukem			eval $_postcmd
5781.11Slukem			;;
5791.11Slukem
5801.11Slukem		stop)
5811.46Slukem			if [ -z "$rc_pid" ]; then
5821.24Slukem				if [ -n "$pidfile" ]; then
5831.24Slukem					echo \
5841.24Slukem				    "${name} not running? (check $pidfile)."
5851.24Slukem				else
5861.24Slukem					echo "${name} not running?"
5871.11Slukem				fi
5881.24Slukem				exit 1
5891.11Slukem			fi
5901.11Slukem
5911.43Slukem					# if the precmd failed and force
5921.43Slukem					# isn't set, exit
5931.43Slukem					#
5941.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
5951.24Slukem				return 1
5961.24Slukem			fi
5971.43Slukem
5981.43Slukem					# send the signal to stop
5991.43Slukem					#
6001.11Slukem			echo "Stopping ${name}."
6011.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
6021.34Slukem			if [ -n "$_user" ]; then
6031.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
6041.34Slukem			fi
6051.43Slukem
6061.43Slukem					# if the stop cmd failed and force
6071.43Slukem					# isn't set, exit
6081.43Slukem					#
6091.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6101.43Slukem				return 1
6111.43Slukem			fi
6121.43Slukem
6131.43Slukem					# wait for the command to exit,
6141.43Slukem					# and run postcmd.
6151.46Slukem			wait_for_pids $rc_pid
6161.43Slukem			eval $_postcmd
6171.11Slukem			;;
6181.11Slukem
6191.11Slukem		reload)
6201.46Slukem			if [ -z "$rc_pid" ]; then
6211.24Slukem				if [ -n "$pidfile" ]; then
6221.24Slukem					echo \
6231.11Slukem				    "${name} not running? (check $pidfile)."
6241.24Slukem				else
6251.24Slukem					echo "${name} not running?"
6261.11Slukem				fi
6271.24Slukem				exit 1
6281.11Slukem			fi
6291.11Slukem			echo "Reloading ${name} config files."
6301.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6311.24Slukem				return 1
6321.24Slukem			fi
6331.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
6341.34Slukem			if [ -n "$_user" ]; then
6351.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
6361.34Slukem			fi
6371.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6381.43Slukem				return 1
6391.43Slukem			fi
6401.43Slukem			eval $_postcmd
6411.11Slukem			;;
6421.11Slukem
6431.11Slukem		restart)
6441.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6451.24Slukem				return 1
6461.11Slukem			fi
6471.29Slukem					# prevent restart being called more
6481.29Slukem					# than once by any given script
6491.29Slukem					#
6501.53Slukem			if ${_rc_restart_done:-false}; then
6511.29Slukem				return 0
6521.29Slukem			fi
6531.53Slukem			_rc_restart_done=true
6541.33Slukem
6551.61Slukem			( $0 ${_rc_prefix}stop )
6561.61Slukem			$0 ${_rc_prefix}start
6571.11Slukem
6581.43Slukem			eval $_postcmd
6591.33Slukem			;;
6601.33Slukem
6611.33Slukem		poll)
6621.46Slukem			if [ -n "$rc_pid" ]; then
6631.46Slukem				wait_for_pids $rc_pid
6641.33Slukem			fi
6651.11Slukem			;;
6661.11Slukem
6671.11Slukem		rcvar)
6681.11Slukem			echo "# $name"
6691.24Slukem			if [ -n "$rcvar" ]; then
6701.24Slukem				if checkyesno ${rcvar}; then
6711.24Slukem					echo "\$${rcvar}=YES"
6721.24Slukem				else
6731.24Slukem					echo "\$${rcvar}=NO"
6741.24Slukem				fi
6751.11Slukem			fi
6761.11Slukem			;;
6771.11Slukem
6781.11Slukem		*)
6791.11Slukem			rc_usage "$_keywords"
6801.11Slukem			;;
6811.11Slukem
6821.11Slukem		esac
6831.11Slukem		return 0
6841.11Slukem	done
6851.11Slukem
6861.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
6871.11Slukem	rc_usage "$_keywords"
6881.11Slukem	exit 1
6891.11Slukem}
6901.11Slukem
6911.11Slukem#
6921.11Slukem# run_rc_script file arg
6931.11Slukem#	Start the script `file' with `arg', and correctly handle the
6941.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
6951.37Slukem#	sourced into the current environment.  If `file' appears to be
6961.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
6971.37Slukem#	executable run as a child process.
6981.17Slukem#
6991.11Slukemrun_rc_script()
7001.11Slukem{
7011.11Slukem	_file=$1
7021.11Slukem	_arg=$2
7031.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
7041.11Slukem		err 3 'USAGE: run_rc_script file arg'
7051.11Slukem	fi
7061.11Slukem
7071.45Slukem	unset	name command command_args command_interpreter \
7081.45Slukem		extra_commands pidfile procname \
7091.42Slukem		rcvar required_dirs required_files required_vars
7101.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
7111.39Slukem
7121.39Slukem	case "$_file" in
7131.39Slukem	*.sh)				# run in current shell
7141.11Slukem		set $_arg ; . $_file
7151.39Slukem		;;
7161.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
7171.39Slukem		warn "Ignoring scratch file $_file"
7181.39Slukem		;;
7191.39Slukem	*)				# run in subshell
7201.39Slukem		if [ -x $_file ]; then
7211.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
7221.39Slukem				set $_arg ; . $_file
7231.39Slukem			else
7241.37Slukem				( set $_arg ; . $_file )
7251.37Slukem			fi
7261.39Slukem		fi
7271.39Slukem		;;
7281.39Slukem	esac
7291.11Slukem}
7301.19Slukem
7311.19Slukem#
7321.19Slukem# load_rc_config
7331.19Slukem#	Source in the configuration file for a given command.
7341.19Slukem#
7351.19Slukemload_rc_config()
7361.19Slukem{
7371.19Slukem	_command=$1
7381.19Slukem	if [ -z "$_command" ]; then
7391.19Slukem		err 3 'USAGE: load_rc_config command'
7401.19Slukem	fi
7411.19Slukem
7421.54Slukem	if ${_rc_conf_loaded:-false}; then
7431.54Slukem		:
7441.54Slukem	else
7451.30Slukem		. /etc/rc.conf
7461.53Slukem		_rc_conf_loaded=true
7471.30Slukem	fi
7481.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
7491.20Sfvdl		. /etc/rc.conf.d/"$_command"
7501.20Sfvdl	fi
7511.19Slukem}
7521.19Slukem
7531.11Slukem
7541.11Slukem#
7551.11Slukem# rc_usage commands
7561.11Slukem#	Print a usage string for $0, with `commands' being a list of
7571.11Slukem#	valid commands.
7581.11Slukem#
7591.11Slukemrc_usage()
7601.11Slukem{
7611.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
7621.11Slukem
7631.11Slukem	_sep=
7641.56Schristos	for _elem; do
7651.11Slukem		echo -n 1>&2 "$_sep$_elem"
7661.11Slukem		_sep="|"
7671.11Slukem	done
7681.11Slukem	echo 1>&2 ")"
7691.11Slukem	exit 1
7701.11Slukem}
7711.11Slukem
7721.11Slukem#
7731.11Slukem# err exitval message
7741.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
7751.11Slukem#
7761.11Slukemerr()
7771.11Slukem{
7781.11Slukem	exitval=$1
7791.11Slukem	shift
7801.11Slukem
7811.51Sgrant	if [ -x /usr/bin/logger ]; then
7821.51Sgrant		logger "$0: ERROR: $*"
7831.51Sgrant	fi
7841.21Slukem	echo 1>&2 "$0: ERROR: $*"
7851.11Slukem	exit $exitval
7861.11Slukem}
7871.11Slukem
7881.11Slukem#
7891.11Slukem# warn message
7901.11Slukem#	Display message to stderr and log to the syslog.
7911.11Slukem#
7921.11Slukemwarn()
7931.11Slukem{
7941.51Sgrant	if [ -x /usr/bin/logger ]; then
7951.51Sgrant		logger "$0: WARNING: $*"
7961.51Sgrant	fi
7971.21Slukem	echo 1>&2 "$0: WARNING: $*"
7981.31Satatat}
7991.31Satatat
8001.31Satatat#
8011.31Satatat# backup_file action file cur backup
8021.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
8031.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
8041.31Satatat#
8051.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
8061.31Satatat#	which can be either YES or NO.
8071.31Satatat#
8081.31Satatat#	The `action' keyword can be one of the following:
8091.31Satatat#
8101.31Satatat#	add		`file' is now being backed up (and is possibly
8111.31Satatat#			being reentered into the backups system).  `cur'
8121.31Satatat#			is created and RCS files, if necessary, are
8131.31Satatat#			created as well.
8141.31Satatat#
8151.31Satatat#	update		`file' has changed and needs to be backed up.
8161.31Satatat#			If `cur' exists, it is copied to to `back' or
8171.31Satatat#			checked into RCS (if the repository file is old),
8181.31Satatat#			and then `file' is copied to `cur'.  Another RCS
8191.31Satatat#			check in done here if RCS is being used.
8201.31Satatat#
8211.31Satatat#	remove		`file' is no longer being tracked by the backups
8221.31Satatat#			system.  If RCS is not being used, `cur' is moved
8231.31Satatat#			to `back', otherwise an empty file is checked in,
8241.31Satatat#			and then `cur' is removed.
8251.31Satatat#
8261.31Satatat#
8271.31Satatatbackup_file()
8281.31Satatat{
8291.31Satatat	_action=$1
8301.31Satatat	_file=$2
8311.31Satatat	_cur=$3
8321.31Satatat	_back=$4
8331.31Satatat
8341.31Satatat	if checkyesno backup_uses_rcs; then
8351.31Satatat		_msg0="backup archive"
8361.31Satatat		_msg1="update"
8371.31Satatat
8381.36Satatat		# ensure that history file is not locked
8391.36Satatat		if [ -f $_cur,v ]; then
8401.36Satatat			rcs -q -u -U -M $_cur
8411.36Satatat		fi
8421.36Satatat
8431.31Satatat		# ensure after switching to rcs that the
8441.31Satatat		# current backup is not lost
8451.31Satatat		if [ -f $_cur ]; then
8461.31Satatat			# no archive, or current newer than archive
8471.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
8481.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8491.36Satatat				rcs -q -kb -U $_cur
8501.49Slukem				co -q -f -u $_cur
8511.31Satatat			fi
8521.31Satatat		fi
8531.31Satatat
8541.31Satatat		case $_action in
8551.31Satatat		add|update)
8561.31Satatat			cp -p $_file $_cur
8571.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8581.36Satatat			rcs -q -kb -U $_cur
8591.49Slukem			co -q -f -u $_cur
8601.31Satatat			chown root:wheel $_cur $_cur,v
8611.31Satatat			;;
8621.31Satatat		remove)
8631.31Satatat			cp /dev/null $_cur
8641.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8651.36Satatat			rcs -q -kb -U $_cur
8661.31Satatat			chown root:wheel $_cur $_cur,v
8671.31Satatat			rm $_cur
8681.31Satatat			;;
8691.31Satatat		esac
8701.31Satatat	else
8711.31Satatat		case $_action in
8721.31Satatat		add|update)
8731.31Satatat			if [ -f $_cur ]; then
8741.31Satatat				cp -p $_cur $_back
8751.31Satatat			fi
8761.31Satatat			cp -p $_file $_cur
8771.31Satatat			chown root:wheel $_cur
8781.31Satatat			;;
8791.31Satatat		remove)
8801.31Satatat			mv -f $_cur $_back
8811.31Satatat			;;
8821.31Satatat		esac
8831.31Satatat	fi
8841.1Scjs}
885