rc.subr revision 1.78
11.78Sapb# $NetBSD: rc.subr,v 1.78 2009/09/11 18:17:04 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#
181.11Slukem# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
191.11Slukem# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
201.11Slukem# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
211.11Slukem# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
221.11Slukem# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
231.11Slukem# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
241.11Slukem# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
251.11Slukem# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
261.11Slukem# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
271.11Slukem# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
281.11Slukem# POSSIBILITY OF SUCH DAMAGE.
291.11Slukem#
301.11Slukem# rc.subr
311.11Slukem#	functions used by various rc scripts
321.11Slukem#
331.11Slukem
341.62Sjmmv: ${rcvar_manpage:='rc.conf(5)'}
351.69Sapb: ${RC_PID:=$$} ; export RC_PID
361.78Sapbnl='
371.78Sapb' # a literal newline
381.62Sjmmv
391.11Slukem#
401.11Slukem#	functions
411.11Slukem#	---------
421.1Scjs
431.5Slukem#
441.11Slukem# checkyesno var
451.5Slukem#	Test $1 variable, and warn if not set to YES or NO.
461.11Slukem#	Return 0 if it's "yes" (et al), nonzero otherwise.
471.5Slukem#
481.11Slukemcheckyesno()
491.11Slukem{
501.11Slukem	eval _value=\$${1}
511.11Slukem	case $_value in
521.4Slukem
531.4Slukem		#	"yes", "true", "on", or "1"
541.4Slukem	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
551.4Slukem		return 0
561.3Slukem		;;
571.4Slukem
581.4Slukem		#	"no", "false", "off", or "0"
591.4Slukem	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
601.4Slukem		return 1
611.3Slukem		;;
621.3Slukem	*)
631.62Sjmmv		warn "\$${1} is not set properly - see ${rcvar_manpage}."
641.4Slukem		return 1
651.3Slukem		;;
661.3Slukem	esac
671.38Slukem}
681.38Slukem
691.65Slukem#
701.78Sapb# yesno_to_truefalse var
711.78Sapb#	Convert the value of a variable from any of the values
721.78Sapb#	understood by checkyesno() to "true" or "false".
731.78Sapb#
741.78Sapbyesno_to_truefalse()
751.78Sapb{
761.78Sapb	local var=$1
771.78Sapb	if checkyesno $var; then
781.78Sapb		eval $var=true
791.78Sapb		return 0
801.78Sapb	else
811.78Sapb		eval $var=false
821.78Sapb		return 1
831.78Sapb	fi
841.78Sapb}
851.78Sapb
861.78Sapb#
871.38Slukem# reverse_list list
881.38Slukem#	print the list in reverse order
891.38Slukem#
901.38Slukemreverse_list()
911.38Slukem{
921.38Slukem	_revlist=
931.56Schristos	for _revfile; do
941.38Slukem		_revlist="$_revfile $_revlist"
951.38Slukem	done
961.38Slukem	echo $_revlist
971.6Smellon}
981.6Smellon
991.6Smellon#
1001.69Sapb# If booting directly to multiuser, send SIGTERM to
1011.69Sapb# the parent (/etc/rc) to abort the boot.
1021.69Sapb# Otherwise just exit.
1031.69Sapb#
1041.69Sapbstop_boot()
1051.69Sapb{
1061.69Sapb	if [ "$autoboot" = yes ]; then
1071.69Sapb		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
1081.69Sapb		kill -TERM ${RC_PID}
1091.69Sapb	fi
1101.69Sapb	exit 1
1111.69Sapb}
1121.69Sapb
1131.69Sapb#
1141.47Slukem# mount_critical_filesystems type
1151.47Slukem#	Go through the list of critical filesystems as provided in
1161.47Slukem#	the rc.conf(5) variable $critical_filesystems_${type}, checking
1171.47Slukem#	each one to see if it is mounted, and if it is not, mounting it.
1181.6Smellon#
1191.11Slukemmount_critical_filesystems()
1201.11Slukem{
1211.47Slukem	eval _fslist=\$critical_filesystems_${1}
1221.17Slukem	for _fs in $_fslist; do
1231.6Smellon		mount | (
1241.53Slukem			_ismounted=false
1251.6Smellon			while read what _on on _type type; do
1261.17Slukem				if [ $on = $_fs ]; then
1271.53Slukem					_ismounted=true
1281.6Smellon				fi
1291.6Smellon			done
1301.53Slukem			if $_ismounted; then
1311.55Slukem				:
1321.55Slukem			else
1331.17Slukem				mount $_fs >/dev/null 2>&1
1341.6Smellon			fi
1351.46Slukem		)
1361.6Smellon	done
1371.7Scjs}
1381.7Scjs
1391.11Slukem#
1401.45Slukem# check_pidfile pidfile procname [interpreter]
1411.45Slukem#	Parses the first line of pidfile for a PID, and ensures
1421.11Slukem#	that the process is running and matches procname.
1431.45Slukem#	Prints the matching PID upon success, nothing otherwise.
1441.45Slukem#	interpreter is optional; see _find_processes() for details.
1451.11Slukem#
1461.11Slukemcheck_pidfile()
1471.11Slukem{
1481.11Slukem	_pidfile=$1
1491.11Slukem	_procname=$2
1501.45Slukem	_interpreter=$3
1511.11Slukem	if [ -z "$_pidfile" -o -z "$_procname" ]; then
1521.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
1531.11Slukem	fi
1541.11Slukem	if [ ! -f $_pidfile ]; then
1551.11Slukem		return
1561.11Slukem	fi
1571.11Slukem	read _pid _junk < $_pidfile
1581.11Slukem	if [ -z "$_pid" ]; then
1591.11Slukem		return
1601.11Slukem	fi
1611.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
1621.11Slukem}
1631.11Slukem
1641.11Slukem#
1651.45Slukem# check_process procname [interpreter]
1661.11Slukem#	Ensures that a process (or processes) named procname is running.
1671.45Slukem#	Prints a list of matching PIDs.
1681.45Slukem#	interpreter is optional; see _find_processes() for details.
1691.11Slukem#
1701.11Slukemcheck_process()
1711.11Slukem{
1721.11Slukem	_procname=$1
1731.45Slukem	_interpreter=$2
1741.11Slukem	if [ -z "$_procname" ]; then
1751.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
1761.45Slukem	fi
1771.45Slukem	_find_processes $_procname ${_interpreter:-.} '-ax'
1781.45Slukem}
1791.45Slukem
1801.46Slukem#
1811.45Slukem# _find_processes procname interpreter psargs
1821.45Slukem#	Search for procname in the output of ps generated by psargs.
1831.45Slukem#	Prints the PIDs of any matching processes, space separated.
1841.45Slukem#
1851.45Slukem#	If interpreter == ".", check the following variations of procname
1861.45Slukem#	against the first word of each command:
1871.45Slukem#		procname
1881.45Slukem#		`basename procname`
1891.45Slukem#		`basename procname` + ":"
1901.45Slukem#		"(" + `basename procname` + ")"
1911.45Slukem#
1921.45Slukem#	If interpreter != ".", read the first line of procname, remove the
1931.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
1941.45Slukem#	match that against each command, either as is, or with extra words
1951.73Sdholland#	at the end.  As an alternative, to deal with interpreted daemons
1961.66She#	using perl, the basename of the interpreter plus a colon is also
1971.66She#	tried as the prefix to procname.
1981.45Slukem#
1991.45Slukem_find_processes()
2001.45Slukem{
2011.45Slukem	if [ $# -ne 3 ]; then
2021.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
2031.11Slukem	fi
2041.45Slukem	_procname=$1
2051.45Slukem	_interpreter=$2
2061.45Slukem	_psargs=$3
2071.45Slukem
2081.11Slukem	_pref=
2091.68Shubertf	_procnamebn=${_procname##*/}
2101.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
2111.67Selad		read _interp < ${_chroot:-}/$_procname	# read interpreter name
2121.45Slukem		_interp=${_interp#\#!}		# strip #!
2131.45Slukem		set -- $_interp
2141.45Slukem		if [ $_interpreter != $1 ]; then
2151.45Slukem			warn "\$command_interpreter $_interpreter != $1"
2161.45Slukem		fi
2171.45Slukem		_interp="$* $_procname"		# cleanup spaces, add _procname
2181.66She		_interpbn=${1##*/}
2191.45Slukem		_fp_args='_argv'
2201.45Slukem		_fp_match='case "$_argv" in
2211.68Shubertf		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
2221.45Slukem	else					# a normal daemon
2231.45Slukem		_fp_args='_arg0 _argv'
2241.45Slukem		_fp_match='case "$_arg0" in
2251.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
2261.45Slukem	fi
2271.45Slukem
2281.45Slukem	_proccheck='
2291.46Slukem		ps -o "pid,command" '"$_psargs"' |
2301.45Slukem		while read _npid '"$_fp_args"'; do
2311.45Slukem			case "$_npid" in
2321.45Slukem			    PID)
2331.45Slukem				continue ;;
2341.45Slukem			esac ; '"$_fp_match"'
2351.45Slukem				echo -n "$_pref$_npid" ;
2361.45Slukem				_pref=" "
2371.45Slukem				;;
2381.45Slukem			esac
2391.45Slukem		done'
2401.45Slukem
2411.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
2421.45Slukem	eval $_proccheck
2431.11Slukem}
2441.11Slukem
2451.11Slukem#
2461.33Slukem# wait_for_pids pid [pid ...]
2471.35Slukem#	spins until none of the pids exist
2481.33Slukem#
2491.33Slukemwait_for_pids()
2501.33Slukem{
2511.56Schristos	_list="$@"
2521.33Slukem	if [ -z "$_list" ]; then
2531.33Slukem		return
2541.33Slukem	fi
2551.35Slukem	_prefix=
2561.35Slukem	while true; do
2571.33Slukem		_nlist="";
2581.33Slukem		for _j in $_list; do
2591.33Slukem			if kill -0 $_j 2>/dev/null; then
2601.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
2611.33Slukem			fi
2621.33Slukem		done
2631.33Slukem		if [ -z "$_nlist" ]; then
2641.33Slukem			break
2651.33Slukem		fi
2661.33Slukem		_list=$_nlist
2671.35Slukem		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
2681.35Slukem		_prefix=", "
2691.35Slukem		sleep 2
2701.33Slukem	done
2711.35Slukem	if [ -n "$_prefix" ]; then
2721.35Slukem		echo "."
2731.35Slukem	fi
2741.33Slukem}
2751.33Slukem
2761.33Slukem#
2771.46Slukem# run_rc_command argument
2781.46Slukem#	Search for argument in the list of supported commands, which is:
2791.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
2801.46Slukem#	If there's a match, run ${argument}_cmd or the default method
2811.46Slukem#	(see below).
2821.11Slukem#
2831.46Slukem#	If argument has a given prefix, then change the operation as follows:
2841.46Slukem#		Prefix	Operation
2851.11Slukem#		------	---------
2861.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
2871.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
2881.61Slukem#		one	Set ${rcvar} to YES
2891.11Slukem#
2901.11Slukem#	The following globals are used:
2911.24Slukem#
2921.46Slukem#	Name		Needed	Purpose
2931.46Slukem#	----		------	-------
2941.11Slukem#	name		y	Name of script.
2951.24Slukem#
2961.11Slukem#	command		n	Full path to command.
2971.46Slukem#				Not needed if ${rc_arg}_cmd is set for
2981.11Slukem#				each keyword.
2991.24Slukem#
3001.11Slukem#	command_args	n	Optional args/shell directives for command.
3011.24Slukem#
3021.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
3031.45Slukem#				call check_{pidfile,process}() appropriately.
3041.45Slukem#
3051.16Slukem#	extra_commands	n	List of extra commands supported.
3061.24Slukem#
3071.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
3081.42Slukem#				otherwise use check_process $command.
3091.42Slukem#				In either case, only check if $command is set.
3101.42Slukem#
3111.42Slukem#	procname	n	Process name to check for instead of $command.
3121.24Slukem#
3131.24Slukem#	rcvar		n	This is checked with checkyesno to determine
3141.24Slukem#				if the action should be run.
3151.24Slukem#
3161.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
3171.42Slukem#				Requires /usr to be mounted.
3181.24Slukem#
3191.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
3201.22Slukem#				(if not using ${name}_chroot).
3211.24Slukem#
3221.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
3231.24Slukem#				NOTE:	$flags from the parent environment
3241.24Slukem#					can be used to override this.
3251.24Slukem#
3261.72She#	${name}_env	n	Additional environment variable settings
3271.72She#				for running ${command}
3281.72She#
3291.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
3301.24Slukem#
3311.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
3321.22Slukem#				using ${name}_chroot.
3331.42Slukem#				Requires /usr to be mounted.
3341.24Slukem#
3351.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
3361.42Slukem#				Requires /usr to be mounted.
3371.24Slukem#
3381.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
3391.32Slukem#				to run the chrooted ${command} with.
3401.42Slukem#				Requires /usr to be mounted.
3411.24Slukem#
3421.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
3431.11Slukem#				Otherwise, use default command (see below)
3441.24Slukem#
3451.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
3461.46Slukem#				${rc_arg}_cmd method in the default
3471.46Slukem#				operation (i.e, after checking for required
3481.46Slukem#				bits and process (non)existence).
3491.11Slukem#				If this completes with a non-zero exit code,
3501.46Slukem#				don't run ${rc_arg}_cmd.
3511.24Slukem#
3521.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
3531.46Slukem#				${rc_arg}_cmd method, if that method
3541.43Slukem#				returned a zero exit code.
3551.43Slukem#
3561.11Slukem#	required_dirs	n	If set, check for the existence of the given
3571.11Slukem#				directories before running the default
3581.11Slukem#				(re)start command.
3591.24Slukem#
3601.11Slukem#	required_files	n	If set, check for the readability of the given
3611.11Slukem#				files before running the default (re)start
3621.11Slukem#				command.
3631.24Slukem#
3641.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
3651.11Slukem#				listed variables before running the default
3661.11Slukem#				(re)start command.
3671.11Slukem#
3681.46Slukem#	Default behaviour for a given argument, if no override method is
3691.46Slukem#	provided:
3701.24Slukem#
3711.46Slukem#	Argument	Default behaviour
3721.46Slukem#	--------	-----------------
3731.11Slukem#	start		if !running && checkyesno ${rcvar}
3741.11Slukem#				${command}
3751.24Slukem#
3761.11Slukem#	stop		if ${pidfile}
3771.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
3781.11Slukem#			else
3791.46Slukem#				rc_pid=$(check_process $command)
3801.46Slukem#			kill $sig_stop $rc_pid
3811.46Slukem#			wait_for_pids $rc_pid
3821.35Slukem#			($sig_stop defaults to TERM.)
3831.24Slukem#
3841.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
3851.35Slukem#			and doesn't wait_for_pids.
3861.11Slukem#			$sig_reload defaults to HUP.
3871.24Slukem#
3881.11Slukem#	restart		Run `stop' then `start'.
3891.11Slukem#
3901.33Slukem#	status		Show if ${command} is running, etc.
3911.33Slukem#
3921.33Slukem#	poll		Wait for ${command} to exit.
3931.33Slukem#
3941.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
3951.33Slukem#
3961.46Slukem#	Variables available to methods, and after run_rc_command() has
3971.46Slukem#	completed:
3981.46Slukem#
3991.46Slukem#	Variable	Purpose
4001.46Slukem#	--------	-------
4011.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
4021.46Slukem#			performed
4031.46Slukem#
4041.46Slukem#	rc_flags	Flags to start the default command with.
4051.46Slukem#			Defaults to ${name}_flags, unless overridden
4061.46Slukem#			by $flags from the environment.
4071.46Slukem#			This variable may be changed by the precmd method.
4081.46Slukem#
4091.46Slukem#	rc_pid		PID of command (if appropriate)
4101.46Slukem#
4111.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
4121.46Slukem#
4131.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
4141.33Slukem#
4151.24Slukem#
4161.11Slukemrun_rc_command()
4171.11Slukem{
4181.46Slukem	rc_arg=$1
4191.24Slukem	if [ -z "$name" ]; then
4201.30Slukem		err 3 'run_rc_command: $name is not set.'
4211.11Slukem	fi
4221.11Slukem
4231.61Slukem	_rc_prefix=
4241.46Slukem	case "$rc_arg" in
4251.24Slukem	fast*)				# "fast" prefix; don't check pid
4261.46Slukem		rc_arg=${rc_arg#fast}
4271.48Slukem		rc_fast=yes
4281.11Slukem		;;
4291.61Slukem	force*)				# "force" prefix; always run
4301.48Slukem		rc_force=yes
4311.61Slukem		_rc_prefix=force
4321.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
4331.61Slukem		if [ -n "${rcvar}" ]; then
4341.61Slukem			eval ${rcvar}=YES
4351.61Slukem		fi
4361.61Slukem		;;
4371.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
4381.61Slukem		_rc_prefix=one
4391.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
4401.29Slukem		if [ -n "${rcvar}" ]; then
4411.29Slukem			eval ${rcvar}=YES
4421.29Slukem		fi
4431.11Slukem		;;
4441.11Slukem	esac
4451.11Slukem
4461.75Sreed	_keywords="start stop restart rcvar"
4471.75Sreed	if [ -n "$extra_commands" ]; then
4481.75Sreed		_keywords="${_keywords} ${extra_commands}"
4491.75Sreed	fi
4501.46Slukem	rc_pid=
4511.11Slukem	_pidcmd=
4521.42Slukem	_procname=${procname:-${command}}
4531.42Slukem
4541.24Slukem					# setup pid check command if not fast
4551.46Slukem	if [ -z "$rc_fast" -a -n "$_procname" ]; then
4561.11Slukem		if [ -n "$pidfile" ]; then
4571.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
4581.42Slukem		else
4591.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
4601.11Slukem		fi
4611.11Slukem		if [ -n "$_pidcmd" ]; then
4621.33Slukem			_keywords="${_keywords} status poll"
4631.11Slukem		fi
4641.11Slukem	fi
4651.11Slukem
4661.46Slukem	if [ -z "$rc_arg" ]; then
4671.11Slukem		rc_usage "$_keywords"
4681.11Slukem	fi
4691.11Slukem
4701.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
4711.46Slukem		rc_flags=$flags
4721.11Slukem	else
4731.46Slukem		eval rc_flags=\$${name}_flags
4741.11Slukem	fi
4751.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
4761.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
4771.72She	    _group=\$${name}_group	_groups=\$${name}_groups \
4781.72She	    _env=\"\$${name}_env\"
4791.11Slukem
4801.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
4811.42Slukem		if [ "$_user" = "$(id -un)" ]; then
4821.42Slukem			unset _user
4831.42Slukem		fi
4841.42Slukem	fi
4851.42Slukem
4861.29Slukem					# if ${rcvar} is set, and $1 is not
4871.40Slukem					# "rcvar", then run
4881.29Slukem					#	checkyesno ${rcvar}
4891.74Ssalo					# and return if that failed or warn
4901.74Ssalo					# user and exit when interactive
4911.24Slukem					#
4921.46Slukem	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
4931.24Slukem		if ! checkyesno ${rcvar}; then
4941.74Ssalo					# check whether interactive or not
4951.74Ssalo			if [ -n "$_run_rc_script" ]; then
4961.74Ssalo				return 0
4971.74Ssalo			fi
4981.74Ssalo			for _elem in $_keywords; do
4991.74Ssalo				if [ "$_elem" = "$rc_arg" ]; then
5001.74Ssalo					echo 1>&2 "\$${rcvar} is not enabled - see ${rcvar_manpage}."
5011.74Ssalo					echo 1>&2 "Use the following if you wish to perform the operation:"
5021.74Ssalo					echo 1>&2 "  $0 one${rc_arg}"
5031.74Ssalo					exit 1
5041.74Ssalo				fi
5051.74Ssalo			done
5061.74Ssalo			echo 1>&2 "$0: unknown directive '$rc_arg'."
5071.74Ssalo			rc_usage "$_keywords"
5081.24Slukem		fi
5091.24Slukem	fi
5101.24Slukem
5111.24Slukem	eval $_pidcmd			# determine the pid if necessary
5121.11Slukem
5131.11Slukem	for _elem in $_keywords; do
5141.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
5151.11Slukem			continue
5161.11Slukem		fi
5171.11Slukem
5181.24Slukem					# if there's a custom ${XXX_cmd},
5191.24Slukem					# run that instead of the default
5201.24Slukem					#
5211.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
5221.46Slukem		    _postcmd=\$${rc_arg}_postcmd
5231.11Slukem		if [ -n "$_cmd" ]; then
5241.24Slukem					# if the precmd failed and force
5251.24Slukem					# isn't set, exit
5261.24Slukem					#
5271.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
5281.24Slukem				return 1
5291.24Slukem			fi
5301.24Slukem
5311.46Slukem			if ! eval $_cmd && [ -z "$rc_force" ]; then
5321.44Slukem				return 1
5331.44Slukem			fi
5341.44Slukem			eval $_postcmd
5351.11Slukem			return 0
5361.11Slukem		fi
5371.11Slukem
5381.46Slukem		case "$rc_arg" in	# default operations...
5391.11Slukem
5401.11Slukem		status)
5411.46Slukem			if [ -n "$rc_pid" ]; then
5421.46Slukem				echo "${name} is running as pid $rc_pid."
5431.11Slukem			else
5441.11Slukem				echo "${name} is not running."
5451.28Slukem				return 1
5461.11Slukem			fi
5471.11Slukem			;;
5481.11Slukem
5491.11Slukem		start)
5501.46Slukem			if [ -n "$rc_pid" ]; then
5511.63Slukem				echo 1>&2 "${name} already running? (pid=$rc_pid)."
5521.11Slukem				exit 1
5531.11Slukem			fi
5541.11Slukem
5551.57Slukem			if [ ! -x ${_chroot}${command} ]; then
5561.11Slukem				return 0
5571.11Slukem			fi
5581.11Slukem
5591.24Slukem					# check for required variables,
5601.24Slukem					# directories, and files
5611.24Slukem					#
5621.11Slukem			for _f in $required_vars; do
5631.11Slukem				if ! checkyesno $_f; then
5641.65Slukem					warn "\$${_f} is not enabled."
5651.46Slukem					if [ -z "$rc_force" ]; then
5661.24Slukem						return 1
5671.24Slukem					fi
5681.11Slukem				fi
5691.11Slukem			done
5701.11Slukem			for _f in $required_dirs; do
5711.11Slukem				if [ ! -d "${_f}/." ]; then
5721.25Slukem					warn "${_f} is not a directory."
5731.46Slukem					if [ -z "$rc_force" ]; then
5741.24Slukem						return 1
5751.24Slukem					fi
5761.11Slukem				fi
5771.11Slukem			done
5781.11Slukem			for _f in $required_files; do
5791.11Slukem				if [ ! -r "${_f}" ]; then
5801.25Slukem					warn "${_f} is not readable."
5811.46Slukem					if [ -z "$rc_force" ]; then
5821.24Slukem						return 1
5831.24Slukem					fi
5841.11Slukem				fi
5851.11Slukem			done
5861.11Slukem
5871.24Slukem					# if the precmd failed and force
5881.24Slukem					# isn't set, exit
5891.24Slukem					#
5901.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
5911.24Slukem				return 1
5921.24Slukem			fi
5931.24Slukem
5941.24Slukem					# setup the command to run, and run it
5951.29Slukem					#
5961.11Slukem			echo "Starting ${name}."
5971.22Slukem			if [ -n "$_chroot" ]; then
5981.22Slukem				_doit="\
5991.72She${_env:+env $_env }\
6001.23Slukem${_nice:+nice -n $_nice }\
6011.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
6021.46Slukem$_chroot $command $rc_flags $command_args"
6031.22Slukem			else
6041.22Slukem				_doit="\
6051.18Slukem${_chdir:+cd $_chdir; }\
6061.72She${_env:+env $_env }\
6071.18Slukem${_nice:+nice -n $_nice }\
6081.46Slukem$command $rc_flags $command_args"
6091.34Slukem				if [ -n "$_user" ]; then
6101.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
6111.34Slukem				fi
6121.22Slukem			fi
6131.43Slukem
6141.43Slukem					# if the cmd failed and force
6151.43Slukem					# isn't set, exit
6161.43Slukem					#
6171.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6181.43Slukem				return 1
6191.43Slukem			fi
6201.43Slukem
6211.43Slukem					# finally, run postcmd
6221.43Slukem					#
6231.43Slukem			eval $_postcmd
6241.11Slukem			;;
6251.11Slukem
6261.11Slukem		stop)
6271.46Slukem			if [ -z "$rc_pid" ]; then
6281.24Slukem				if [ -n "$pidfile" ]; then
6291.63Slukem					echo 1>&2 \
6301.24Slukem				    "${name} not running? (check $pidfile)."
6311.24Slukem				else
6321.63Slukem					echo 1>&2 "${name} not running?"
6331.11Slukem				fi
6341.24Slukem				exit 1
6351.11Slukem			fi
6361.11Slukem
6371.43Slukem					# if the precmd failed and force
6381.43Slukem					# isn't set, exit
6391.43Slukem					#
6401.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6411.24Slukem				return 1
6421.24Slukem			fi
6431.43Slukem
6441.43Slukem					# send the signal to stop
6451.43Slukem					#
6461.11Slukem			echo "Stopping ${name}."
6471.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
6481.34Slukem			if [ -n "$_user" ]; then
6491.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
6501.34Slukem			fi
6511.43Slukem
6521.43Slukem					# if the stop cmd failed and force
6531.43Slukem					# isn't set, exit
6541.43Slukem					#
6551.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6561.43Slukem				return 1
6571.43Slukem			fi
6581.43Slukem
6591.43Slukem					# wait for the command to exit,
6601.43Slukem					# and run postcmd.
6611.46Slukem			wait_for_pids $rc_pid
6621.43Slukem			eval $_postcmd
6631.11Slukem			;;
6641.11Slukem
6651.11Slukem		reload)
6661.46Slukem			if [ -z "$rc_pid" ]; then
6671.24Slukem				if [ -n "$pidfile" ]; then
6681.63Slukem					echo 1>&2 \
6691.11Slukem				    "${name} not running? (check $pidfile)."
6701.24Slukem				else
6711.63Slukem					echo 1>&2 "${name} not running?"
6721.11Slukem				fi
6731.24Slukem				exit 1
6741.11Slukem			fi
6751.11Slukem			echo "Reloading ${name} config files."
6761.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6771.24Slukem				return 1
6781.24Slukem			fi
6791.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
6801.34Slukem			if [ -n "$_user" ]; then
6811.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
6821.34Slukem			fi
6831.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6841.43Slukem				return 1
6851.43Slukem			fi
6861.43Slukem			eval $_postcmd
6871.11Slukem			;;
6881.11Slukem
6891.11Slukem		restart)
6901.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6911.24Slukem				return 1
6921.11Slukem			fi
6931.29Slukem					# prevent restart being called more
6941.29Slukem					# than once by any given script
6951.29Slukem					#
6961.53Slukem			if ${_rc_restart_done:-false}; then
6971.29Slukem				return 0
6981.29Slukem			fi
6991.53Slukem			_rc_restart_done=true
7001.33Slukem
7011.61Slukem			( $0 ${_rc_prefix}stop )
7021.61Slukem			$0 ${_rc_prefix}start
7031.11Slukem
7041.43Slukem			eval $_postcmd
7051.33Slukem			;;
7061.33Slukem
7071.33Slukem		poll)
7081.46Slukem			if [ -n "$rc_pid" ]; then
7091.46Slukem				wait_for_pids $rc_pid
7101.33Slukem			fi
7111.11Slukem			;;
7121.11Slukem
7131.11Slukem		rcvar)
7141.11Slukem			echo "# $name"
7151.24Slukem			if [ -n "$rcvar" ]; then
7161.24Slukem				if checkyesno ${rcvar}; then
7171.24Slukem					echo "\$${rcvar}=YES"
7181.24Slukem				else
7191.24Slukem					echo "\$${rcvar}=NO"
7201.24Slukem				fi
7211.11Slukem			fi
7221.11Slukem			;;
7231.11Slukem
7241.11Slukem		*)
7251.11Slukem			rc_usage "$_keywords"
7261.11Slukem			;;
7271.11Slukem
7281.11Slukem		esac
7291.11Slukem		return 0
7301.11Slukem	done
7311.11Slukem
7321.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
7331.11Slukem	rc_usage "$_keywords"
7341.11Slukem	exit 1
7351.11Slukem}
7361.11Slukem
7371.11Slukem#
7381.11Slukem# run_rc_script file arg
7391.11Slukem#	Start the script `file' with `arg', and correctly handle the
7401.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
7411.37Slukem#	sourced into the current environment.  If `file' appears to be
7421.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
7431.37Slukem#	executable run as a child process.
7441.17Slukem#
7451.78Sapb#	If `file' contains "KEYWORD: interactive" and if we are
7461.78Sapb#	running inside /etc/rc with postprocessing (as signified by
7471.78Sapb#	_rc_postprocessor_fd being defined) then the script's stdout
7481.78Sapb#	and stderr are redirected to $_rc_original_stdout_fd and
7491.78Sapb#	$_rc_original_stderr_fd, so the output will be displayed on the
7501.78Sapb#	console but not intercepted by /etc/rc's postprocessor.
7511.78Sapb#
7521.11Slukemrun_rc_script()
7531.11Slukem{
7541.11Slukem	_file=$1
7551.11Slukem	_arg=$2
7561.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
7571.11Slukem		err 3 'USAGE: run_rc_script file arg'
7581.11Slukem	fi
7591.11Slukem
7601.74Ssalo	_run_rc_script=true
7611.74Ssalo
7621.45Slukem	unset	name command command_args command_interpreter \
7631.45Slukem		extra_commands pidfile procname \
7641.42Slukem		rcvar required_dirs required_files required_vars
7651.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
7661.39Slukem
7671.78Sapb	_must_redirect=false
7681.78Sapb	if ! [ -n "${_rc_postprocessor_fd}" ] \
7691.78Sapb	    && _has_rcorder_keyword interactive $_file
7701.78Sapb	then
7711.78Sapb		_must_redirect=true
7721.78Sapb	fi
7731.78Sapb
7741.39Slukem	case "$_file" in
7751.39Slukem	*.sh)				# run in current shell
7761.78Sapb		if $_must_redirect; then
7771.78Sapb			print_rc_metadata \
7781.78Sapb			    "note:Output from ${_file} is not logged"
7791.78Sapb			set $_arg ; no_rc_postprocess . $_file
7801.78Sapb		else
7811.78Sapb			set $_arg ; . $_file
7821.78Sapb		fi
7831.39Slukem		;;
7841.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
7851.39Slukem		warn "Ignoring scratch file $_file"
7861.39Slukem		;;
7871.39Slukem	*)				# run in subshell
7881.78Sapb		if [ -x $_file ] && $_must_redirect; then
7891.78Sapb			print_rc_metadata \
7901.78Sapb			    "note:Output from ${_file} is not logged"
7911.78Sapb			if [ -n "$rc_fast_and_loose" ]; then
7921.78Sapb				set $_arg ; no_rc_postprocess . $_file
7931.78Sapb			else
7941.78Sapb				( set $_arg ; no_rc_postprocess . $_file )
7951.78Sapb			fi
7961.78Sapb		elif [ -x $_file ]; then
7971.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
7981.39Slukem				set $_arg ; . $_file
7991.39Slukem			else
8001.37Slukem				( set $_arg ; . $_file )
8011.37Slukem			fi
8021.78Sapb		else
8031.78Sapb			warn "Ignoring non-executable file $_file"
8041.39Slukem		fi
8051.39Slukem		;;
8061.39Slukem	esac
8071.11Slukem}
8081.19Slukem
8091.19Slukem#
8101.65Slukem# load_rc_config command
8111.19Slukem#	Source in the configuration file for a given command.
8121.19Slukem#
8131.19Slukemload_rc_config()
8141.19Slukem{
8151.19Slukem	_command=$1
8161.19Slukem	if [ -z "$_command" ]; then
8171.19Slukem		err 3 'USAGE: load_rc_config command'
8181.19Slukem	fi
8191.19Slukem
8201.54Slukem	if ${_rc_conf_loaded:-false}; then
8211.54Slukem		:
8221.54Slukem	else
8231.30Slukem		. /etc/rc.conf
8241.53Slukem		_rc_conf_loaded=true
8251.30Slukem	fi
8261.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
8271.20Sfvdl		. /etc/rc.conf.d/"$_command"
8281.20Sfvdl	fi
8291.19Slukem}
8301.19Slukem
8311.65Slukem#
8321.65Slukem# load_rc_config_var cmd var
8331.65Slukem#	Read the rc.conf(5) var for cmd and set in the
8341.65Slukem#	current shell, using load_rc_config in a subshell to prevent
8351.65Slukem#	unwanted side effects from other variable assignments.
8361.65Slukem#
8371.65Slukemload_rc_config_var()
8381.65Slukem{
8391.65Slukem	if [ $# -ne 2 ]; then
8401.65Slukem		err 3 'USAGE: load_rc_config_var cmd var'
8411.65Slukem	fi
8421.65Slukem	eval $(eval '(
8431.65Slukem		load_rc_config '$1' >/dev/null;
8441.77Sapb		if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
8451.65Slukem			echo '$2'=\'\''${'$2'}\'\'';
8461.65Slukem		fi
8471.65Slukem	)' )
8481.65Slukem}
8491.11Slukem
8501.11Slukem#
8511.11Slukem# rc_usage commands
8521.11Slukem#	Print a usage string for $0, with `commands' being a list of
8531.11Slukem#	valid commands.
8541.11Slukem#
8551.11Slukemrc_usage()
8561.11Slukem{
8571.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
8581.11Slukem
8591.11Slukem	_sep=
8601.56Schristos	for _elem; do
8611.11Slukem		echo -n 1>&2 "$_sep$_elem"
8621.11Slukem		_sep="|"
8631.11Slukem	done
8641.11Slukem	echo 1>&2 ")"
8651.11Slukem	exit 1
8661.11Slukem}
8671.11Slukem
8681.11Slukem#
8691.11Slukem# err exitval message
8701.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
8711.11Slukem#
8721.11Slukemerr()
8731.11Slukem{
8741.11Slukem	exitval=$1
8751.11Slukem	shift
8761.11Slukem
8771.51Sgrant	if [ -x /usr/bin/logger ]; then
8781.51Sgrant		logger "$0: ERROR: $*"
8791.51Sgrant	fi
8801.21Slukem	echo 1>&2 "$0: ERROR: $*"
8811.11Slukem	exit $exitval
8821.11Slukem}
8831.11Slukem
8841.11Slukem#
8851.11Slukem# warn message
8861.11Slukem#	Display message to stderr and log to the syslog.
8871.11Slukem#
8881.11Slukemwarn()
8891.11Slukem{
8901.51Sgrant	if [ -x /usr/bin/logger ]; then
8911.51Sgrant		logger "$0: WARNING: $*"
8921.51Sgrant	fi
8931.21Slukem	echo 1>&2 "$0: WARNING: $*"
8941.31Satatat}
8951.31Satatat
8961.31Satatat#
8971.31Satatat# backup_file action file cur backup
8981.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
8991.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
9001.31Satatat#
9011.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
9021.31Satatat#	which can be either YES or NO.
9031.31Satatat#
9041.31Satatat#	The `action' keyword can be one of the following:
9051.31Satatat#
9061.31Satatat#	add		`file' is now being backed up (and is possibly
9071.31Satatat#			being reentered into the backups system).  `cur'
9081.31Satatat#			is created and RCS files, if necessary, are
9091.31Satatat#			created as well.
9101.31Satatat#
9111.31Satatat#	update		`file' has changed and needs to be backed up.
9121.31Satatat#			If `cur' exists, it is copied to to `back' or
9131.31Satatat#			checked into RCS (if the repository file is old),
9141.31Satatat#			and then `file' is copied to `cur'.  Another RCS
9151.31Satatat#			check in done here if RCS is being used.
9161.31Satatat#
9171.31Satatat#	remove		`file' is no longer being tracked by the backups
9181.31Satatat#			system.  If RCS is not being used, `cur' is moved
9191.31Satatat#			to `back', otherwise an empty file is checked in,
9201.31Satatat#			and then `cur' is removed.
9211.31Satatat#
9221.31Satatat#
9231.31Satatatbackup_file()
9241.31Satatat{
9251.31Satatat	_action=$1
9261.31Satatat	_file=$2
9271.31Satatat	_cur=$3
9281.31Satatat	_back=$4
9291.31Satatat
9301.31Satatat	if checkyesno backup_uses_rcs; then
9311.31Satatat		_msg0="backup archive"
9321.31Satatat		_msg1="update"
9331.31Satatat
9341.36Satatat		# ensure that history file is not locked
9351.36Satatat		if [ -f $_cur,v ]; then
9361.36Satatat			rcs -q -u -U -M $_cur
9371.36Satatat		fi
9381.36Satatat
9391.31Satatat		# ensure after switching to rcs that the
9401.31Satatat		# current backup is not lost
9411.31Satatat		if [ -f $_cur ]; then
9421.31Satatat			# no archive, or current newer than archive
9431.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
9441.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
9451.36Satatat				rcs -q -kb -U $_cur
9461.49Slukem				co -q -f -u $_cur
9471.31Satatat			fi
9481.31Satatat		fi
9491.31Satatat
9501.31Satatat		case $_action in
9511.31Satatat		add|update)
9521.31Satatat			cp -p $_file $_cur
9531.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
9541.36Satatat			rcs -q -kb -U $_cur
9551.49Slukem			co -q -f -u $_cur
9561.31Satatat			chown root:wheel $_cur $_cur,v
9571.31Satatat			;;
9581.31Satatat		remove)
9591.31Satatat			cp /dev/null $_cur
9601.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
9611.36Satatat			rcs -q -kb -U $_cur
9621.31Satatat			chown root:wheel $_cur $_cur,v
9631.31Satatat			rm $_cur
9641.31Satatat			;;
9651.31Satatat		esac
9661.31Satatat	else
9671.31Satatat		case $_action in
9681.31Satatat		add|update)
9691.31Satatat			if [ -f $_cur ]; then
9701.31Satatat				cp -p $_cur $_back
9711.31Satatat			fi
9721.31Satatat			cp -p $_file $_cur
9731.31Satatat			chown root:wheel $_cur
9741.31Satatat			;;
9751.31Satatat		remove)
9761.31Satatat			mv -f $_cur $_back
9771.31Satatat			;;
9781.31Satatat		esac
9791.31Satatat	fi
9801.1Scjs}
9811.64Smycroft
9821.76Schristos#
9831.76Schristos# handle_fsck_error fsck_exit_code
9841.76Schristos#	Take action depending on the return code from fsck.
9851.76Schristos#
9861.76Schristoshandle_fsck_error()
9871.76Schristos{
9881.76Schristos	case $1 in
9891.76Schristos	0)	# OK
9901.76Schristos		return
9911.76Schristos		;;
9921.76Schristos	2)	# Needs re-run, still fs errors
9931.76Schristos		echo "File system still has errors; re-run fsck manually!"
9941.76Schristos		;;
9951.76Schristos	4)	# Root modified
9961.76Schristos		echo "Root filesystem was modified, rebooting ..."
9971.76Schristos		reboot -n
9981.76Schristos		echo "Reboot failed; help!"
9991.76Schristos		;;
10001.76Schristos	8)	# Check failed
10011.76Schristos		echo "Automatic file system check failed; help!"
10021.76Schristos		;;
10031.76Schristos	12)	# Got signal
10041.76Schristos		echo "Boot interrupted."
10051.76Schristos		;;
10061.76Schristos	*)
10071.76Schristos		echo "Unknown error $1; help!"
10081.76Schristos		;;
10091.76Schristos	esac
10101.76Schristos	stop_boot
10111.76Schristos}
10121.76Schristos
10131.78Sapb#
10141.78Sapb# _has_rcorder_keyword word file
10151.78Sapb#	Check whether a file contains a "# KEYWORD:" comment with a
10161.78Sapb#	specified keyword in the style used by rcorder(8).
10171.78Sapb#
10181.78Sapb_has_rcorder_keyword()
10191.78Sapb{
10201.78Sapb	local word="$1"
10211.78Sapb	local file="$2"
10221.78Sapb	local line
10231.78Sapb
10241.78Sapb	[ -r "$file" ] || return 1
10251.78Sapb	while read line; do
10261.78Sapb		case "${line} " in
10271.78Sapb		"# KEYWORD:"*[\ \	]"${word}"[\ \	]*)
10281.78Sapb			return 0
10291.78Sapb			;;
10301.78Sapb		"#"*)
10311.78Sapb			continue
10321.78Sapb			;;
10331.78Sapb		*[A-Za-z0-9]*)
10341.78Sapb			# give up at the first non-empty non-comment line
10351.78Sapb			return 1
10361.78Sapb			;;
10371.78Sapb		esac
10381.78Sapb	done <"$file"
10391.78Sapb	return 1
10401.78Sapb}
10411.78Sapb
10421.78Sapb#
10431.78Sapb# print_rc_metadata string
10441.78Sapb#	Print the specified string in such a way that the post-processor
10451.78Sapb#	inside /etc/rc will treat it as meta-data.
10461.78Sapb#
10471.78Sapb#	If we are not running inside /etc/rc, do nothing.
10481.78Sapb#
10491.78Sapb#	For public use by any rc.d script, the string must begin with
10501.78Sapb#	"note:", followed by arbitrary text.  The intent is that the text
10511.78Sapb#	will appear in a log file but not on the console.
10521.78Sapb#
10531.78Sapb#	For private use within /etc/rc, the string must contain a
10541.78Sapb#	keyword recognised by the rc_postprocess_metadata() function
10551.78Sapb#	defined in /etc/rc, followed by a colon, followed by one or more
10561.78Sapb#	colon-separated arguments associated with the keyword.
10571.78Sapb#
10581.78Sapbprint_rc_metadata()
10591.78Sapb{
10601.78Sapb	# _rc_postprocessor fd, if defined, is the fd to which we must
10611.78Sapb	# print, prefixing the output with $_rc_metadata_prefix.
10621.78Sapb	#
10631.78Sapb	if [ -n "$_rc_postprocessor_fd" ]; then
10641.78Sapb		printf "%s%s\n" "$rc_metadata_prefix" "$1" \
10651.78Sapb			>&${_rc_postprocessor_fd}
10661.78Sapb	fi
10671.78Sapb}
10681.78Sapb
10691.78Sapb#
10701.78Sapb# print_rc_normal string
10711.78Sapb#	Print the specified string in such way that it is treated as
10721.78Sapb#	normal output, regardless of whether or not we are running
10731.78Sapb#	inside /etc/rc with post-processing.
10741.78Sapb#
10751.78Sapb#	Ths intent is that a script that is run via the
10761.78Sapb#	no_rc_postprocess() function (so its output would ordinarily be
10771.78Sapb#	invisible to the post-processor) can nevertheless arrange for
10781.78Sapb#	the post-processor to see things printed with print_rc_normal().
10791.78Sapb#
10801.78Sapbprint_rc_normal()
10811.78Sapb{
10821.78Sapb	# If _rc_postprocessor_fd is defined, then it is the fd
10831.78Sapb	# to shich we must print; otherwise print to stdout.
10841.78Sapb	#
10851.78Sapb	printf "%s\n" "$1" >&${_rc_postprocessor_fd:-1}
10861.78Sapb}
10871.78Sapb
10881.78Sapb#
10891.78Sapb# no_rc_postprocess cmd...
10901.78Sapb#	Execute the specified command in such a way that its output
10911.78Sapb#	bypasses the post-processor that handles the output from
10921.78Sapb#	most commands that are run inside /etc/rc.  If we are not
10931.78Sapb#	inside /etc/rc, then just execute the command without special
10941.78Sapb#	treatment.
10951.78Sapb#
10961.78Sapb#	The intent is that interactive commands can be run via
10971.78Sapb#	no_rc_postprocess(), and their output will apear immediately
10981.78Sapb#	on the console instead of being hidden or delayed by the
10991.78Sapb#	post-processor.	 An unfortunate consequence of the output
11001.78Sapb#	bypassing the post-processor is that the output will not be
11011.78Sapb#	logged.
11021.78Sapb#
11031.78Sapbno_rc_postprocess()
11041.78Sapb{
11051.78Sapb	if [ -n "${_rc_postprocessor_fd}" ]; then
11061.78Sapb		"$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
11071.78Sapb	else
11081.78Sapb		"$@"
11091.78Sapb	fi
11101.78Sapb}
11111.78Sapb
11121.78Sapb#
11131.78Sapb# twiddle
11141.78Sapb#	On each call, print a different one of "/", "-", "\\", "|",
11151.78Sapb#	followed by a backspace.  The most recently printed value is
11161.78Sapb#	saved in $_twiddle_state.
11171.78Sapb#
11181.78Sapb#	Output is to /dev/tty, so this function may be useful even inside
11191.78Sapb#	a script whose output is redirected.
11201.78Sapb#
11211.78Sapbtwiddle()
11221.78Sapb{
11231.78Sapb	case "$_twiddle_state" in
11241.78Sapb	'/')	_next='-' ;;
11251.78Sapb	'-')	_next='\' ;;
11261.78Sapb	'\')	_next='|' ;;
11271.78Sapb	*)	_next='/' ;;
11281.78Sapb	esac
11291.78Sapb	printf "%s\b" "$_next" >/dev/tty
11301.78Sapb	_twiddle_state="$_next"
11311.78Sapb}
11321.78Sapb
11331.64Smycroft_rc_subr_loaded=:
1134