rc.subr revision 1.69
11.69Sapb# $NetBSD: rc.subr,v 1.69 2007/04/06 14:20:08 apb Exp $
21.11Slukem#
31.65Slukem# Copyright (c) 1997-2004 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.62Sjmmv: ${rcvar_manpage:='rc.conf(5)'}
421.69Sapb: ${RC_PID:=$$} ; export RC_PID
431.62Sjmmv
441.11Slukem#
451.11Slukem#	functions
461.11Slukem#	---------
471.1Scjs
481.5Slukem#
491.11Slukem# checkyesno var
501.5Slukem#	Test $1 variable, and warn if not set to YES or NO.
511.11Slukem#	Return 0 if it's "yes" (et al), nonzero otherwise.
521.5Slukem#
531.11Slukemcheckyesno()
541.11Slukem{
551.11Slukem	eval _value=\$${1}
561.11Slukem	case $_value in
571.4Slukem
581.4Slukem		#	"yes", "true", "on", or "1"
591.4Slukem	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
601.4Slukem		return 0
611.3Slukem		;;
621.4Slukem
631.4Slukem		#	"no", "false", "off", or "0"
641.4Slukem	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
651.4Slukem		return 1
661.3Slukem		;;
671.3Slukem	*)
681.62Sjmmv		warn "\$${1} is not set properly - see ${rcvar_manpage}."
691.4Slukem		return 1
701.3Slukem		;;
711.3Slukem	esac
721.38Slukem}
731.38Slukem
741.65Slukem#
751.38Slukem# reverse_list list
761.38Slukem#	print the list in reverse order
771.38Slukem#
781.38Slukemreverse_list()
791.38Slukem{
801.38Slukem	_revlist=
811.56Schristos	for _revfile; do
821.38Slukem		_revlist="$_revfile $_revlist"
831.38Slukem	done
841.38Slukem	echo $_revlist
851.6Smellon}
861.6Smellon
871.6Smellon#
881.69Sapb# If booting directly to multiuser, send SIGTERM to
891.69Sapb# the parent (/etc/rc) to abort the boot.
901.69Sapb# Otherwise just exit.
911.69Sapb#
921.69Sapbstop_boot()
931.69Sapb{
941.69Sapb	if [ "$autoboot" = yes ]; then
951.69Sapb		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
961.69Sapb		kill -TERM ${RC_PID}
971.69Sapb	fi
981.69Sapb	exit 1
991.69Sapb}
1001.69Sapb
1011.69Sapb#
1021.47Slukem# mount_critical_filesystems type
1031.47Slukem#	Go through the list of critical filesystems as provided in
1041.47Slukem#	the rc.conf(5) variable $critical_filesystems_${type}, checking
1051.47Slukem#	each one to see if it is mounted, and if it is not, mounting it.
1061.6Smellon#
1071.11Slukemmount_critical_filesystems()
1081.11Slukem{
1091.47Slukem	eval _fslist=\$critical_filesystems_${1}
1101.17Slukem	for _fs in $_fslist; do
1111.6Smellon		mount | (
1121.53Slukem			_ismounted=false
1131.6Smellon			while read what _on on _type type; do
1141.17Slukem				if [ $on = $_fs ]; then
1151.53Slukem					_ismounted=true
1161.6Smellon				fi
1171.6Smellon			done
1181.53Slukem			if $_ismounted; then
1191.55Slukem				:
1201.55Slukem			else
1211.17Slukem				mount $_fs >/dev/null 2>&1
1221.6Smellon			fi
1231.46Slukem		)
1241.6Smellon	done
1251.7Scjs}
1261.7Scjs
1271.11Slukem#
1281.45Slukem# check_pidfile pidfile procname [interpreter]
1291.45Slukem#	Parses the first line of pidfile for a PID, and ensures
1301.11Slukem#	that the process is running and matches procname.
1311.45Slukem#	Prints the matching PID upon success, nothing otherwise.
1321.45Slukem#	interpreter is optional; see _find_processes() for details.
1331.11Slukem#
1341.11Slukemcheck_pidfile()
1351.11Slukem{
1361.11Slukem	_pidfile=$1
1371.11Slukem	_procname=$2
1381.45Slukem	_interpreter=$3
1391.11Slukem	if [ -z "$_pidfile" -o -z "$_procname" ]; then
1401.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
1411.11Slukem	fi
1421.11Slukem	if [ ! -f $_pidfile ]; then
1431.11Slukem		return
1441.11Slukem	fi
1451.11Slukem	read _pid _junk < $_pidfile
1461.11Slukem	if [ -z "$_pid" ]; then
1471.11Slukem		return
1481.11Slukem	fi
1491.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
1501.11Slukem}
1511.11Slukem
1521.11Slukem#
1531.45Slukem# check_process procname [interpreter]
1541.11Slukem#	Ensures that a process (or processes) named procname is running.
1551.45Slukem#	Prints a list of matching PIDs.
1561.45Slukem#	interpreter is optional; see _find_processes() for details.
1571.11Slukem#
1581.11Slukemcheck_process()
1591.11Slukem{
1601.11Slukem	_procname=$1
1611.45Slukem	_interpreter=$2
1621.11Slukem	if [ -z "$_procname" ]; then
1631.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
1641.45Slukem	fi
1651.45Slukem	_find_processes $_procname ${_interpreter:-.} '-ax'
1661.45Slukem}
1671.45Slukem
1681.46Slukem#
1691.45Slukem# _find_processes procname interpreter psargs
1701.45Slukem#	Search for procname in the output of ps generated by psargs.
1711.45Slukem#	Prints the PIDs of any matching processes, space separated.
1721.45Slukem#
1731.45Slukem#	If interpreter == ".", check the following variations of procname
1741.45Slukem#	against the first word of each command:
1751.45Slukem#		procname
1761.45Slukem#		`basename procname`
1771.45Slukem#		`basename procname` + ":"
1781.45Slukem#		"(" + `basename procname` + ")"
1791.45Slukem#
1801.45Slukem#	If interpreter != ".", read the first line of procname, remove the
1811.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
1821.45Slukem#	match that against each command, either as is, or with extra words
1831.66She#	at the end.  As an alternative, to deal with interpreted deaemons
1841.66She#	using perl, the basename of the interpreter plus a colon is also
1851.66She#	tried as the prefix to procname.
1861.45Slukem#
1871.45Slukem_find_processes()
1881.45Slukem{
1891.45Slukem	if [ $# -ne 3 ]; then
1901.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
1911.11Slukem	fi
1921.45Slukem	_procname=$1
1931.45Slukem	_interpreter=$2
1941.45Slukem	_psargs=$3
1951.45Slukem
1961.11Slukem	_pref=
1971.68Shubertf	_procnamebn=${_procname##*/}
1981.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
1991.67Selad		read _interp < ${_chroot:-}/$_procname	# read interpreter name
2001.45Slukem		_interp=${_interp#\#!}		# strip #!
2011.45Slukem		set -- $_interp
2021.45Slukem		if [ $_interpreter != $1 ]; then
2031.45Slukem			warn "\$command_interpreter $_interpreter != $1"
2041.45Slukem		fi
2051.45Slukem		_interp="$* $_procname"		# cleanup spaces, add _procname
2061.66She		_interpbn=${1##*/}
2071.45Slukem		_fp_args='_argv'
2081.45Slukem		_fp_match='case "$_argv" in
2091.68Shubertf		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
2101.45Slukem	else					# a normal daemon
2111.45Slukem		_fp_args='_arg0 _argv'
2121.45Slukem		_fp_match='case "$_arg0" in
2131.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
2141.45Slukem	fi
2151.45Slukem
2161.45Slukem	_proccheck='
2171.46Slukem		ps -o "pid,command" '"$_psargs"' |
2181.45Slukem		while read _npid '"$_fp_args"'; do
2191.45Slukem			case "$_npid" in
2201.45Slukem			    PID)
2211.45Slukem				continue ;;
2221.45Slukem			esac ; '"$_fp_match"'
2231.45Slukem				echo -n "$_pref$_npid" ;
2241.45Slukem				_pref=" "
2251.45Slukem				;;
2261.45Slukem			esac
2271.45Slukem		done'
2281.45Slukem
2291.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
2301.45Slukem	eval $_proccheck
2311.11Slukem}
2321.11Slukem
2331.11Slukem#
2341.33Slukem# wait_for_pids pid [pid ...]
2351.35Slukem#	spins until none of the pids exist
2361.33Slukem#
2371.33Slukemwait_for_pids()
2381.33Slukem{
2391.56Schristos	_list="$@"
2401.33Slukem	if [ -z "$_list" ]; then
2411.33Slukem		return
2421.33Slukem	fi
2431.35Slukem	_prefix=
2441.35Slukem	while true; do
2451.33Slukem		_nlist="";
2461.33Slukem		for _j in $_list; do
2471.33Slukem			if kill -0 $_j 2>/dev/null; then
2481.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
2491.33Slukem			fi
2501.33Slukem		done
2511.33Slukem		if [ -z "$_nlist" ]; then
2521.33Slukem			break
2531.33Slukem		fi
2541.33Slukem		_list=$_nlist
2551.35Slukem		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
2561.35Slukem		_prefix=", "
2571.35Slukem		sleep 2
2581.33Slukem	done
2591.35Slukem	if [ -n "$_prefix" ]; then
2601.35Slukem		echo "."
2611.35Slukem	fi
2621.33Slukem}
2631.33Slukem
2641.33Slukem#
2651.46Slukem# run_rc_command argument
2661.46Slukem#	Search for argument in the list of supported commands, which is:
2671.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
2681.46Slukem#	If there's a match, run ${argument}_cmd or the default method
2691.46Slukem#	(see below).
2701.11Slukem#
2711.46Slukem#	If argument has a given prefix, then change the operation as follows:
2721.46Slukem#		Prefix	Operation
2731.11Slukem#		------	---------
2741.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
2751.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
2761.61Slukem#		one	Set ${rcvar} to YES
2771.11Slukem#
2781.11Slukem#	The following globals are used:
2791.24Slukem#
2801.46Slukem#	Name		Needed	Purpose
2811.46Slukem#	----		------	-------
2821.11Slukem#	name		y	Name of script.
2831.24Slukem#
2841.11Slukem#	command		n	Full path to command.
2851.46Slukem#				Not needed if ${rc_arg}_cmd is set for
2861.11Slukem#				each keyword.
2871.24Slukem#
2881.11Slukem#	command_args	n	Optional args/shell directives for command.
2891.24Slukem#
2901.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
2911.45Slukem#				call check_{pidfile,process}() appropriately.
2921.45Slukem#
2931.16Slukem#	extra_commands	n	List of extra commands supported.
2941.24Slukem#
2951.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
2961.42Slukem#				otherwise use check_process $command.
2971.42Slukem#				In either case, only check if $command is set.
2981.42Slukem#
2991.42Slukem#	procname	n	Process name to check for instead of $command.
3001.24Slukem#
3011.24Slukem#	rcvar		n	This is checked with checkyesno to determine
3021.24Slukem#				if the action should be run.
3031.24Slukem#
3041.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
3051.42Slukem#				Requires /usr to be mounted.
3061.24Slukem#
3071.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
3081.22Slukem#				(if not using ${name}_chroot).
3091.24Slukem#
3101.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
3111.24Slukem#				NOTE:	$flags from the parent environment
3121.24Slukem#					can be used to override this.
3131.24Slukem#
3141.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
3151.24Slukem#
3161.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
3171.22Slukem#				using ${name}_chroot.
3181.42Slukem#				Requires /usr to be mounted.
3191.24Slukem#
3201.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
3211.42Slukem#				Requires /usr to be mounted.
3221.24Slukem#
3231.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
3241.32Slukem#				to run the chrooted ${command} with.
3251.42Slukem#				Requires /usr to be mounted.
3261.24Slukem#
3271.50Satatat#	${name}_systrace n	Flags passed to systrace(1) if it is used.
3281.50Satatat#				Setting this variable enables systracing
3291.50Satatat# 				of the given program.  The use of "-a" is
3301.50Satatat#				recommended so that the boot process is not
3311.50Satatat#				stalled.  In order to pass no flags to
3321.50Satatat#				systrace, set this variable to "--".
3331.50Satatat#
3341.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
3351.11Slukem#				Otherwise, use default command (see below)
3361.24Slukem#
3371.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
3381.46Slukem#				${rc_arg}_cmd method in the default
3391.46Slukem#				operation (i.e, after checking for required
3401.46Slukem#				bits and process (non)existence).
3411.11Slukem#				If this completes with a non-zero exit code,
3421.46Slukem#				don't run ${rc_arg}_cmd.
3431.24Slukem#
3441.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
3451.46Slukem#				${rc_arg}_cmd method, if that method
3461.43Slukem#				returned a zero exit code.
3471.43Slukem#
3481.11Slukem#	required_dirs	n	If set, check for the existence of the given
3491.11Slukem#				directories before running the default
3501.11Slukem#				(re)start command.
3511.24Slukem#
3521.11Slukem#	required_files	n	If set, check for the readability of the given
3531.11Slukem#				files before running the default (re)start
3541.11Slukem#				command.
3551.24Slukem#
3561.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
3571.11Slukem#				listed variables before running the default
3581.11Slukem#				(re)start command.
3591.11Slukem#
3601.46Slukem#	Default behaviour for a given argument, if no override method is
3611.46Slukem#	provided:
3621.24Slukem#
3631.46Slukem#	Argument	Default behaviour
3641.46Slukem#	--------	-----------------
3651.11Slukem#	start		if !running && checkyesno ${rcvar}
3661.11Slukem#				${command}
3671.24Slukem#
3681.11Slukem#	stop		if ${pidfile}
3691.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
3701.11Slukem#			else
3711.46Slukem#				rc_pid=$(check_process $command)
3721.46Slukem#			kill $sig_stop $rc_pid
3731.46Slukem#			wait_for_pids $rc_pid
3741.35Slukem#			($sig_stop defaults to TERM.)
3751.24Slukem#
3761.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
3771.35Slukem#			and doesn't wait_for_pids.
3781.11Slukem#			$sig_reload defaults to HUP.
3791.24Slukem#
3801.11Slukem#	restart		Run `stop' then `start'.
3811.11Slukem#
3821.33Slukem#	status		Show if ${command} is running, etc.
3831.33Slukem#
3841.33Slukem#	poll		Wait for ${command} to exit.
3851.33Slukem#
3861.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
3871.33Slukem#
3881.46Slukem#	Variables available to methods, and after run_rc_command() has
3891.46Slukem#	completed:
3901.46Slukem#
3911.46Slukem#	Variable	Purpose
3921.46Slukem#	--------	-------
3931.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
3941.46Slukem#			performed
3951.46Slukem#
3961.46Slukem#	rc_flags	Flags to start the default command with.
3971.46Slukem#			Defaults to ${name}_flags, unless overridden
3981.46Slukem#			by $flags from the environment.
3991.46Slukem#			This variable may be changed by the precmd method.
4001.46Slukem#
4011.46Slukem#	rc_pid		PID of command (if appropriate)
4021.46Slukem#
4031.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
4041.46Slukem#
4051.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
4061.33Slukem#
4071.24Slukem#
4081.11Slukemrun_rc_command()
4091.11Slukem{
4101.46Slukem	rc_arg=$1
4111.24Slukem	if [ -z "$name" ]; then
4121.30Slukem		err 3 'run_rc_command: $name is not set.'
4131.11Slukem	fi
4141.11Slukem
4151.61Slukem	_rc_prefix=
4161.46Slukem	case "$rc_arg" in
4171.24Slukem	fast*)				# "fast" prefix; don't check pid
4181.46Slukem		rc_arg=${rc_arg#fast}
4191.48Slukem		rc_fast=yes
4201.11Slukem		;;
4211.61Slukem	force*)				# "force" prefix; always run
4221.48Slukem		rc_force=yes
4231.61Slukem		_rc_prefix=force
4241.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
4251.61Slukem		if [ -n "${rcvar}" ]; then
4261.61Slukem			eval ${rcvar}=YES
4271.61Slukem		fi
4281.61Slukem		;;
4291.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
4301.61Slukem		_rc_prefix=one
4311.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
4321.29Slukem		if [ -n "${rcvar}" ]; then
4331.29Slukem			eval ${rcvar}=YES
4341.29Slukem		fi
4351.11Slukem		;;
4361.11Slukem	esac
4371.11Slukem
4381.16Slukem	_keywords="start stop restart rcvar $extra_commands"
4391.46Slukem	rc_pid=
4401.11Slukem	_pidcmd=
4411.42Slukem	_procname=${procname:-${command}}
4421.42Slukem
4431.24Slukem					# setup pid check command if not fast
4441.46Slukem	if [ -z "$rc_fast" -a -n "$_procname" ]; then
4451.11Slukem		if [ -n "$pidfile" ]; then
4461.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
4471.42Slukem		else
4481.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
4491.11Slukem		fi
4501.11Slukem		if [ -n "$_pidcmd" ]; then
4511.33Slukem			_keywords="${_keywords} status poll"
4521.11Slukem		fi
4531.11Slukem	fi
4541.11Slukem
4551.46Slukem	if [ -z "$rc_arg" ]; then
4561.11Slukem		rc_usage "$_keywords"
4571.11Slukem	fi
4581.11Slukem
4591.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
4601.46Slukem		rc_flags=$flags
4611.11Slukem	else
4621.46Slukem		eval rc_flags=\$${name}_flags
4631.11Slukem	fi
4641.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
4651.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
4661.50Satatat	    _group=\$${name}_group	_groups=\$${name}_groups \
4671.50Satatat	    _systrace=\$${name}_systrace
4681.11Slukem
4691.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
4701.42Slukem		if [ "$_user" = "$(id -un)" ]; then
4711.42Slukem			unset _user
4721.42Slukem		fi
4731.42Slukem	fi
4741.42Slukem
4751.29Slukem					# if ${rcvar} is set, and $1 is not
4761.40Slukem					# "rcvar", then run
4771.29Slukem					#	checkyesno ${rcvar}
4781.29Slukem					# and return if that failed
4791.24Slukem					#
4801.46Slukem	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
4811.24Slukem		if ! checkyesno ${rcvar}; then
4821.24Slukem			return 0
4831.24Slukem		fi
4841.24Slukem	fi
4851.24Slukem
4861.24Slukem	eval $_pidcmd			# determine the pid if necessary
4871.11Slukem
4881.11Slukem	for _elem in $_keywords; do
4891.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
4901.11Slukem			continue
4911.11Slukem		fi
4921.11Slukem
4931.24Slukem					# if there's a custom ${XXX_cmd},
4941.24Slukem					# run that instead of the default
4951.24Slukem					#
4961.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
4971.46Slukem		    _postcmd=\$${rc_arg}_postcmd
4981.11Slukem		if [ -n "$_cmd" ]; then
4991.24Slukem					# if the precmd failed and force
5001.24Slukem					# isn't set, exit
5011.24Slukem					#
5021.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
5031.24Slukem				return 1
5041.24Slukem			fi
5051.24Slukem
5061.46Slukem			if ! eval $_cmd && [ -z "$rc_force" ]; then
5071.44Slukem				return 1
5081.44Slukem			fi
5091.44Slukem			eval $_postcmd
5101.11Slukem			return 0
5111.11Slukem		fi
5121.11Slukem
5131.46Slukem		case "$rc_arg" in	# default operations...
5141.11Slukem
5151.11Slukem		status)
5161.46Slukem			if [ -n "$rc_pid" ]; then
5171.46Slukem				echo "${name} is running as pid $rc_pid."
5181.11Slukem			else
5191.11Slukem				echo "${name} is not running."
5201.28Slukem				return 1
5211.11Slukem			fi
5221.11Slukem			;;
5231.11Slukem
5241.11Slukem		start)
5251.46Slukem			if [ -n "$rc_pid" ]; then
5261.63Slukem				echo 1>&2 "${name} already running? (pid=$rc_pid)."
5271.11Slukem				exit 1
5281.11Slukem			fi
5291.11Slukem
5301.57Slukem			if [ ! -x ${_chroot}${command} ]; then
5311.11Slukem				return 0
5321.11Slukem			fi
5331.11Slukem
5341.24Slukem					# check for required variables,
5351.24Slukem					# directories, and files
5361.24Slukem					#
5371.11Slukem			for _f in $required_vars; do
5381.11Slukem				if ! checkyesno $_f; then
5391.65Slukem					warn "\$${_f} is not enabled."
5401.46Slukem					if [ -z "$rc_force" ]; then
5411.24Slukem						return 1
5421.24Slukem					fi
5431.11Slukem				fi
5441.11Slukem			done
5451.11Slukem			for _f in $required_dirs; do
5461.11Slukem				if [ ! -d "${_f}/." ]; then
5471.25Slukem					warn "${_f} is not a directory."
5481.46Slukem					if [ -z "$rc_force" ]; then
5491.24Slukem						return 1
5501.24Slukem					fi
5511.11Slukem				fi
5521.11Slukem			done
5531.11Slukem			for _f in $required_files; do
5541.11Slukem				if [ ! -r "${_f}" ]; then
5551.25Slukem					warn "${_f} is not readable."
5561.46Slukem					if [ -z "$rc_force" ]; then
5571.24Slukem						return 1
5581.24Slukem					fi
5591.11Slukem				fi
5601.11Slukem			done
5611.11Slukem
5621.24Slukem					# if the precmd failed and force
5631.24Slukem					# isn't set, exit
5641.24Slukem					#
5651.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
5661.24Slukem				return 1
5671.24Slukem			fi
5681.24Slukem
5691.24Slukem					# setup the command to run, and run it
5701.29Slukem					#
5711.11Slukem			echo "Starting ${name}."
5721.22Slukem			if [ -n "$_chroot" ]; then
5731.22Slukem				_doit="\
5741.23Slukem${_nice:+nice -n $_nice }\
5751.50Satatat${_systrace:+systrace $_systrace }\
5761.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
5771.46Slukem$_chroot $command $rc_flags $command_args"
5781.22Slukem			else
5791.22Slukem				_doit="\
5801.18Slukem${_chdir:+cd $_chdir; }\
5811.18Slukem${_nice:+nice -n $_nice }\
5821.50Satatat${_systrace:+systrace $_systrace }\
5831.46Slukem$command $rc_flags $command_args"
5841.34Slukem				if [ -n "$_user" ]; then
5851.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
5861.34Slukem				fi
5871.22Slukem			fi
5881.43Slukem
5891.43Slukem					# if the cmd failed and force
5901.43Slukem					# isn't set, exit
5911.43Slukem					#
5921.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
5931.43Slukem				return 1
5941.43Slukem			fi
5951.43Slukem
5961.43Slukem					# finally, run postcmd
5971.43Slukem					#
5981.43Slukem			eval $_postcmd
5991.11Slukem			;;
6001.11Slukem
6011.11Slukem		stop)
6021.46Slukem			if [ -z "$rc_pid" ]; then
6031.24Slukem				if [ -n "$pidfile" ]; then
6041.63Slukem					echo 1>&2 \
6051.24Slukem				    "${name} not running? (check $pidfile)."
6061.24Slukem				else
6071.63Slukem					echo 1>&2 "${name} not running?"
6081.11Slukem				fi
6091.24Slukem				exit 1
6101.11Slukem			fi
6111.11Slukem
6121.43Slukem					# if the precmd failed and force
6131.43Slukem					# isn't set, exit
6141.43Slukem					#
6151.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6161.24Slukem				return 1
6171.24Slukem			fi
6181.43Slukem
6191.43Slukem					# send the signal to stop
6201.43Slukem					#
6211.11Slukem			echo "Stopping ${name}."
6221.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
6231.34Slukem			if [ -n "$_user" ]; then
6241.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
6251.34Slukem			fi
6261.43Slukem
6271.43Slukem					# if the stop cmd failed and force
6281.43Slukem					# isn't set, exit
6291.43Slukem					#
6301.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6311.43Slukem				return 1
6321.43Slukem			fi
6331.43Slukem
6341.43Slukem					# wait for the command to exit,
6351.43Slukem					# and run postcmd.
6361.46Slukem			wait_for_pids $rc_pid
6371.43Slukem			eval $_postcmd
6381.11Slukem			;;
6391.11Slukem
6401.11Slukem		reload)
6411.46Slukem			if [ -z "$rc_pid" ]; then
6421.24Slukem				if [ -n "$pidfile" ]; then
6431.63Slukem					echo 1>&2 \
6441.11Slukem				    "${name} not running? (check $pidfile)."
6451.24Slukem				else
6461.63Slukem					echo 1>&2 "${name} not running?"
6471.11Slukem				fi
6481.24Slukem				exit 1
6491.11Slukem			fi
6501.11Slukem			echo "Reloading ${name} config files."
6511.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6521.24Slukem				return 1
6531.24Slukem			fi
6541.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
6551.34Slukem			if [ -n "$_user" ]; then
6561.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
6571.34Slukem			fi
6581.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6591.43Slukem				return 1
6601.43Slukem			fi
6611.43Slukem			eval $_postcmd
6621.11Slukem			;;
6631.11Slukem
6641.11Slukem		restart)
6651.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6661.24Slukem				return 1
6671.11Slukem			fi
6681.29Slukem					# prevent restart being called more
6691.29Slukem					# than once by any given script
6701.29Slukem					#
6711.53Slukem			if ${_rc_restart_done:-false}; then
6721.29Slukem				return 0
6731.29Slukem			fi
6741.53Slukem			_rc_restart_done=true
6751.33Slukem
6761.61Slukem			( $0 ${_rc_prefix}stop )
6771.61Slukem			$0 ${_rc_prefix}start
6781.11Slukem
6791.43Slukem			eval $_postcmd
6801.33Slukem			;;
6811.33Slukem
6821.33Slukem		poll)
6831.46Slukem			if [ -n "$rc_pid" ]; then
6841.46Slukem				wait_for_pids $rc_pid
6851.33Slukem			fi
6861.11Slukem			;;
6871.11Slukem
6881.11Slukem		rcvar)
6891.11Slukem			echo "# $name"
6901.24Slukem			if [ -n "$rcvar" ]; then
6911.24Slukem				if checkyesno ${rcvar}; then
6921.24Slukem					echo "\$${rcvar}=YES"
6931.24Slukem				else
6941.24Slukem					echo "\$${rcvar}=NO"
6951.24Slukem				fi
6961.11Slukem			fi
6971.11Slukem			;;
6981.11Slukem
6991.11Slukem		*)
7001.11Slukem			rc_usage "$_keywords"
7011.11Slukem			;;
7021.11Slukem
7031.11Slukem		esac
7041.11Slukem		return 0
7051.11Slukem	done
7061.11Slukem
7071.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
7081.11Slukem	rc_usage "$_keywords"
7091.11Slukem	exit 1
7101.11Slukem}
7111.11Slukem
7121.11Slukem#
7131.11Slukem# run_rc_script file arg
7141.11Slukem#	Start the script `file' with `arg', and correctly handle the
7151.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
7161.37Slukem#	sourced into the current environment.  If `file' appears to be
7171.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
7181.37Slukem#	executable run as a child process.
7191.17Slukem#
7201.11Slukemrun_rc_script()
7211.11Slukem{
7221.11Slukem	_file=$1
7231.11Slukem	_arg=$2
7241.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
7251.11Slukem		err 3 'USAGE: run_rc_script file arg'
7261.11Slukem	fi
7271.11Slukem
7281.45Slukem	unset	name command command_args command_interpreter \
7291.45Slukem		extra_commands pidfile procname \
7301.42Slukem		rcvar required_dirs required_files required_vars
7311.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
7321.39Slukem
7331.39Slukem	case "$_file" in
7341.39Slukem	*.sh)				# run in current shell
7351.11Slukem		set $_arg ; . $_file
7361.39Slukem		;;
7371.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
7381.39Slukem		warn "Ignoring scratch file $_file"
7391.39Slukem		;;
7401.39Slukem	*)				# run in subshell
7411.39Slukem		if [ -x $_file ]; then
7421.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
7431.39Slukem				set $_arg ; . $_file
7441.39Slukem			else
7451.37Slukem				( set $_arg ; . $_file )
7461.37Slukem			fi
7471.39Slukem		fi
7481.39Slukem		;;
7491.39Slukem	esac
7501.11Slukem}
7511.19Slukem
7521.19Slukem#
7531.65Slukem# load_rc_config command
7541.19Slukem#	Source in the configuration file for a given command.
7551.19Slukem#
7561.19Slukemload_rc_config()
7571.19Slukem{
7581.19Slukem	_command=$1
7591.19Slukem	if [ -z "$_command" ]; then
7601.19Slukem		err 3 'USAGE: load_rc_config command'
7611.19Slukem	fi
7621.19Slukem
7631.54Slukem	if ${_rc_conf_loaded:-false}; then
7641.54Slukem		:
7651.54Slukem	else
7661.30Slukem		. /etc/rc.conf
7671.53Slukem		_rc_conf_loaded=true
7681.30Slukem	fi
7691.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
7701.20Sfvdl		. /etc/rc.conf.d/"$_command"
7711.20Sfvdl	fi
7721.19Slukem}
7731.19Slukem
7741.65Slukem#
7751.65Slukem# load_rc_config_var cmd var
7761.65Slukem#	Read the rc.conf(5) var for cmd and set in the
7771.65Slukem#	current shell, using load_rc_config in a subshell to prevent
7781.65Slukem#	unwanted side effects from other variable assignments.
7791.65Slukem#
7801.65Slukemload_rc_config_var()
7811.65Slukem{
7821.65Slukem	if [ $# -ne 2 ]; then
7831.65Slukem		err 3 'USAGE: load_rc_config_var cmd var'
7841.65Slukem	fi
7851.65Slukem	eval $(eval '(
7861.65Slukem		load_rc_config '$1' >/dev/null;
7871.65Slukem                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
7881.65Slukem			echo '$2'=\'\''${'$2'}\'\'';
7891.65Slukem		fi
7901.65Slukem	)' )
7911.65Slukem}
7921.11Slukem
7931.11Slukem#
7941.11Slukem# rc_usage commands
7951.11Slukem#	Print a usage string for $0, with `commands' being a list of
7961.11Slukem#	valid commands.
7971.11Slukem#
7981.11Slukemrc_usage()
7991.11Slukem{
8001.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
8011.11Slukem
8021.11Slukem	_sep=
8031.56Schristos	for _elem; do
8041.11Slukem		echo -n 1>&2 "$_sep$_elem"
8051.11Slukem		_sep="|"
8061.11Slukem	done
8071.11Slukem	echo 1>&2 ")"
8081.11Slukem	exit 1
8091.11Slukem}
8101.11Slukem
8111.11Slukem#
8121.11Slukem# err exitval message
8131.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
8141.11Slukem#
8151.11Slukemerr()
8161.11Slukem{
8171.11Slukem	exitval=$1
8181.11Slukem	shift
8191.11Slukem
8201.51Sgrant	if [ -x /usr/bin/logger ]; then
8211.51Sgrant		logger "$0: ERROR: $*"
8221.51Sgrant	fi
8231.21Slukem	echo 1>&2 "$0: ERROR: $*"
8241.11Slukem	exit $exitval
8251.11Slukem}
8261.11Slukem
8271.11Slukem#
8281.11Slukem# warn message
8291.11Slukem#	Display message to stderr and log to the syslog.
8301.11Slukem#
8311.11Slukemwarn()
8321.11Slukem{
8331.51Sgrant	if [ -x /usr/bin/logger ]; then
8341.51Sgrant		logger "$0: WARNING: $*"
8351.51Sgrant	fi
8361.21Slukem	echo 1>&2 "$0: WARNING: $*"
8371.31Satatat}
8381.31Satatat
8391.31Satatat#
8401.31Satatat# backup_file action file cur backup
8411.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
8421.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
8431.31Satatat#
8441.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
8451.31Satatat#	which can be either YES or NO.
8461.31Satatat#
8471.31Satatat#	The `action' keyword can be one of the following:
8481.31Satatat#
8491.31Satatat#	add		`file' is now being backed up (and is possibly
8501.31Satatat#			being reentered into the backups system).  `cur'
8511.31Satatat#			is created and RCS files, if necessary, are
8521.31Satatat#			created as well.
8531.31Satatat#
8541.31Satatat#	update		`file' has changed and needs to be backed up.
8551.31Satatat#			If `cur' exists, it is copied to to `back' or
8561.31Satatat#			checked into RCS (if the repository file is old),
8571.31Satatat#			and then `file' is copied to `cur'.  Another RCS
8581.31Satatat#			check in done here if RCS is being used.
8591.31Satatat#
8601.31Satatat#	remove		`file' is no longer being tracked by the backups
8611.31Satatat#			system.  If RCS is not being used, `cur' is moved
8621.31Satatat#			to `back', otherwise an empty file is checked in,
8631.31Satatat#			and then `cur' is removed.
8641.31Satatat#
8651.31Satatat#
8661.31Satatatbackup_file()
8671.31Satatat{
8681.31Satatat	_action=$1
8691.31Satatat	_file=$2
8701.31Satatat	_cur=$3
8711.31Satatat	_back=$4
8721.31Satatat
8731.31Satatat	if checkyesno backup_uses_rcs; then
8741.31Satatat		_msg0="backup archive"
8751.31Satatat		_msg1="update"
8761.31Satatat
8771.36Satatat		# ensure that history file is not locked
8781.36Satatat		if [ -f $_cur,v ]; then
8791.36Satatat			rcs -q -u -U -M $_cur
8801.36Satatat		fi
8811.36Satatat
8821.31Satatat		# ensure after switching to rcs that the
8831.31Satatat		# current backup is not lost
8841.31Satatat		if [ -f $_cur ]; then
8851.31Satatat			# no archive, or current newer than archive
8861.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
8871.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8881.36Satatat				rcs -q -kb -U $_cur
8891.49Slukem				co -q -f -u $_cur
8901.31Satatat			fi
8911.31Satatat		fi
8921.31Satatat
8931.31Satatat		case $_action in
8941.31Satatat		add|update)
8951.31Satatat			cp -p $_file $_cur
8961.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
8971.36Satatat			rcs -q -kb -U $_cur
8981.49Slukem			co -q -f -u $_cur
8991.31Satatat			chown root:wheel $_cur $_cur,v
9001.31Satatat			;;
9011.31Satatat		remove)
9021.31Satatat			cp /dev/null $_cur
9031.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
9041.36Satatat			rcs -q -kb -U $_cur
9051.31Satatat			chown root:wheel $_cur $_cur,v
9061.31Satatat			rm $_cur
9071.31Satatat			;;
9081.31Satatat		esac
9091.31Satatat	else
9101.31Satatat		case $_action in
9111.31Satatat		add|update)
9121.31Satatat			if [ -f $_cur ]; then
9131.31Satatat				cp -p $_cur $_back
9141.31Satatat			fi
9151.31Satatat			cp -p $_file $_cur
9161.31Satatat			chown root:wheel $_cur
9171.31Satatat			;;
9181.31Satatat		remove)
9191.31Satatat			mv -f $_cur $_back
9201.31Satatat			;;
9211.31Satatat		esac
9221.31Satatat	fi
9231.1Scjs}
9241.64Smycroft
9251.64Smycroft_rc_subr_loaded=:
926