rc.subr revision 1.103
11.103Skre# $NetBSD: rc.subr,v 1.103 2018/09/23 23:02:39 kre Exp $
21.11Slukem#
31.88Sapb# Copyright (c) 1997-2011 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.99Schristos# RC variables to clear on start.
401.99Schristos_env_clear_rc_vars="
411.99SchristosRC_PID=
421.99Schristos_rc_pid=
431.99Schristos_rc_original_stdout_fd=
441.99Schristos_rc_original_stderr_fd=
451.99Schristos_rc_postprocessor_fd=
461.99Schristos"
471.99Schristos
481.11Slukem#
491.11Slukem#	functions
501.11Slukem#	---------
511.1Scjs
521.5Slukem#
531.11Slukem# checkyesno var
541.95Sroy#	Test $1 variable.
551.95Sroy#	Return 0 if it's "yes" (et al), 1 if it's "no" (et al), 2 otherwise.
561.5Slukem#
571.95Sroycheckyesnox()
581.11Slukem{
591.11Slukem	eval _value=\$${1}
601.11Slukem	case $_value in
611.4Slukem
621.4Slukem		#	"yes", "true", "on", or "1"
631.4Slukem	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
641.4Slukem		return 0
651.3Slukem		;;
661.4Slukem
671.4Slukem		#	"no", "false", "off", or "0"
681.4Slukem	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
691.4Slukem		return 1
701.3Slukem		;;
711.3Slukem	*)
721.95Sroy		return 2
731.3Slukem		;;
741.3Slukem	esac
751.38Slukem}
761.38Slukem
771.65Slukem#
781.95Sroy# checkyesno var
791.95Sroy#	Test $1 variable, and warn if not set to YES or NO.
801.95Sroy#	Return 0 if it's "yes" (et al), nonzero otherwise.
811.95Sroy#
821.95Sroycheckyesno()
831.95Sroy{
841.95Sroy	local var
851.95Sroy
861.95Sroy	checkyesnox $1
871.95Sroy	var=$?
881.103Skre	case "${var}" in
891.103Skre	( 0 | 1 )	return $var;;
901.103Skre	esac
911.95Sroy	warn "\$${1} is not set properly - see ${rcvar_manpage}."
921.95Sroy	return 1
931.95Sroy}
941.95Sroy
951.95Sroy#
961.78Sapb# yesno_to_truefalse var
971.78Sapb#	Convert the value of a variable from any of the values
981.78Sapb#	understood by checkyesno() to "true" or "false".
991.78Sapb#
1001.78Sapbyesno_to_truefalse()
1011.78Sapb{
1021.78Sapb	local var=$1
1031.78Sapb	if checkyesno $var; then
1041.78Sapb		eval $var=true
1051.78Sapb		return 0
1061.78Sapb	else
1071.78Sapb		eval $var=false
1081.78Sapb		return 1
1091.78Sapb	fi
1101.78Sapb}
1111.78Sapb
1121.78Sapb#
1131.38Slukem# reverse_list list
1141.38Slukem#	print the list in reverse order
1151.38Slukem#
1161.38Slukemreverse_list()
1171.38Slukem{
1181.38Slukem	_revlist=
1191.56Schristos	for _revfile; do
1201.38Slukem		_revlist="$_revfile $_revlist"
1211.38Slukem	done
1221.38Slukem	echo $_revlist
1231.6Smellon}
1241.6Smellon
1251.6Smellon#
1261.69Sapb# If booting directly to multiuser, send SIGTERM to
1271.69Sapb# the parent (/etc/rc) to abort the boot.
1281.69Sapb# Otherwise just exit.
1291.69Sapb#
1301.69Sapbstop_boot()
1311.69Sapb{
1321.69Sapb	if [ "$autoboot" = yes ]; then
1331.69Sapb		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
1341.69Sapb		kill -TERM ${RC_PID}
1351.69Sapb	fi
1361.69Sapb	exit 1
1371.69Sapb}
1381.69Sapb
1391.69Sapb#
1401.47Slukem# mount_critical_filesystems type
1411.93Swiz#	Go through the list of critical file systems as provided in
1421.47Slukem#	the rc.conf(5) variable $critical_filesystems_${type}, checking
1431.47Slukem#	each one to see if it is mounted, and if it is not, mounting it.
1441.79Sapb#	It's not an error if file systems prefixed with "OPTIONAL:"
1451.79Sapb#	are not mentioned in /etc/fstab.
1461.6Smellon#
1471.11Slukemmount_critical_filesystems()
1481.11Slukem{
1491.47Slukem	eval _fslist=\$critical_filesystems_${1}
1501.79Sapb	_mountcrit_es=0
1511.17Slukem	for _fs in $_fslist; do
1521.79Sapb		_optional=false
1531.79Sapb		case "$_fs" in
1541.79Sapb		OPTIONAL:*)
1551.79Sapb			_optional=true
1561.79Sapb			_fs="${_fs#*:}"
1571.79Sapb			;;
1581.79Sapb		esac
1591.79Sapb		_ismounted=false
1601.79Sapb		# look for a line like "${fs} on * type *"
1611.79Sapb		# or "* on ${fs} type *" in the output from mount.
1621.79Sapb		case "${nl}$( mount )${nl}" in
1631.79Sapb		*" on ${_fs} type "*)
1641.79Sapb			_ismounted=true
1651.79Sapb			;;
1661.79Sapb		*"${nl}${_fs} on "*)
1671.79Sapb			_ismounted=true
1681.79Sapb			;;
1691.79Sapb		esac
1701.79Sapb		if $_ismounted; then
1711.79Sapb			print_rc_metadata \
1721.79Sapb			"note:File system ${_fs} was already mounted"
1731.79Sapb		else
1741.79Sapb			_mount_output=$( mount $_fs 2>&1 )
1751.79Sapb			_mount_es=$?
1761.79Sapb			case "$_mount_output" in
1771.79Sapb			*"${nl}"*)
1781.79Sapb				# multiple lines can't be good,
1791.79Sapb				# not even if $_optional is true
1801.79Sapb				;;
1811.89Sapb			*[uU]'nknown special file or file system'*)
1821.79Sapb				if $_optional; then
1831.79Sapb					# ignore this error
1841.79Sapb					print_rc_metadata \
1851.79Sapb			"note:Optional file system ${_fs} is not present"
1861.79Sapb					_mount_es=0
1871.79Sapb					_mount_output=""
1881.6Smellon				fi
1891.79Sapb				;;
1901.79Sapb			esac
1911.79Sapb			if [ -n "$_mount_output" ]; then
1921.79Sapb				printf >&2 "%s\n" "$_mount_output"
1931.79Sapb			fi
1941.79Sapb			if [ "$_mount_es" != 0 ]; then
1951.79Sapb				_mountcrit_es="$_mount_es"
1961.6Smellon			fi
1971.79Sapb		fi
1981.6Smellon	done
1991.79Sapb	return $_mountcrit_es
2001.7Scjs}
2011.7Scjs
2021.11Slukem#
2031.45Slukem# check_pidfile pidfile procname [interpreter]
2041.45Slukem#	Parses the first line of pidfile for a PID, and ensures
2051.11Slukem#	that the process is running and matches procname.
2061.45Slukem#	Prints the matching PID upon success, nothing otherwise.
2071.45Slukem#	interpreter is optional; see _find_processes() for details.
2081.11Slukem#
2091.11Slukemcheck_pidfile()
2101.11Slukem{
2111.11Slukem	_pidfile=$1
2121.11Slukem	_procname=$2
2131.45Slukem	_interpreter=$3
2141.103Skre	if [ -z "$_pidfile" ] || [ -z "$_procname" ]; then
2151.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
2161.11Slukem	fi
2171.11Slukem	if [ ! -f $_pidfile ]; then
2181.11Slukem		return
2191.11Slukem	fi
2201.11Slukem	read _pid _junk < $_pidfile
2211.11Slukem	if [ -z "$_pid" ]; then
2221.11Slukem		return
2231.11Slukem	fi
2241.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
2251.11Slukem}
2261.11Slukem
2271.11Slukem#
2281.45Slukem# check_process procname [interpreter]
2291.11Slukem#	Ensures that a process (or processes) named procname is running.
2301.45Slukem#	Prints a list of matching PIDs.
2311.45Slukem#	interpreter is optional; see _find_processes() for details.
2321.11Slukem#
2331.11Slukemcheck_process()
2341.11Slukem{
2351.11Slukem	_procname=$1
2361.45Slukem	_interpreter=$2
2371.11Slukem	if [ -z "$_procname" ]; then
2381.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
2391.45Slukem	fi
2401.101Skre	_find_processes $_procname ${_interpreter:-.} '-A'
2411.45Slukem}
2421.45Slukem
2431.46Slukem#
2441.45Slukem# _find_processes procname interpreter psargs
2451.45Slukem#	Search for procname in the output of ps generated by psargs.
2461.45Slukem#	Prints the PIDs of any matching processes, space separated.
2471.45Slukem#
2481.45Slukem#	If interpreter == ".", check the following variations of procname
2491.45Slukem#	against the first word of each command:
2501.45Slukem#		procname
2511.45Slukem#		`basename procname`
2521.45Slukem#		`basename procname` + ":"
2531.45Slukem#		"(" + `basename procname` + ")"
2541.45Slukem#
2551.45Slukem#	If interpreter != ".", read the first line of procname, remove the
2561.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
2571.45Slukem#	match that against each command, either as is, or with extra words
2581.73Sdholland#	at the end.  As an alternative, to deal with interpreted daemons
2591.66She#	using perl, the basename of the interpreter plus a colon is also
2601.66She#	tried as the prefix to procname.
2611.45Slukem#
2621.45Slukem_find_processes()
2631.45Slukem{
2641.45Slukem	if [ $# -ne 3 ]; then
2651.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
2661.11Slukem	fi
2671.45Slukem	_procname=$1
2681.45Slukem	_interpreter=$2
2691.45Slukem	_psargs=$3
2701.45Slukem
2711.11Slukem	_pref=
2721.68Shubertf	_procnamebn=${_procname##*/}
2731.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
2741.67Selad		read _interp < ${_chroot:-}/$_procname	# read interpreter name
2751.45Slukem		_interp=${_interp#\#!}		# strip #!
2761.45Slukem		set -- $_interp
2771.87Schristos		if [ $1 = "/usr/bin/env" ]; then
2781.87Schristos			shift
2791.87Schristos			set -- $(type $1)
2801.87Schristos			shift $(($# - 1))
2811.87Schristos			_interp="${1##*/} $_procname"
2821.87Schristos		else
2831.87Schristos			_interp="$* $_procname"
2841.87Schristos		fi
2851.45Slukem		if [ $_interpreter != $1 ]; then
2861.45Slukem			warn "\$command_interpreter $_interpreter != $1"
2871.45Slukem		fi
2881.66She		_interpbn=${1##*/}
2891.45Slukem		_fp_args='_argv'
2901.45Slukem		_fp_match='case "$_argv" in
2911.68Shubertf		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
2921.45Slukem	else					# a normal daemon
2931.45Slukem		_fp_args='_arg0 _argv'
2941.45Slukem		_fp_match='case "$_arg0" in
2951.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
2961.45Slukem	fi
2971.45Slukem
2981.45Slukem	_proccheck='
2991.102Schristos		ps -o "pid,args" '"$_psargs"' 2>&1 |
3001.45Slukem		while read _npid '"$_fp_args"'; do
3011.45Slukem			case "$_npid" in
3021.102Schristos			ps:|PID)
3031.45Slukem				continue ;;
3041.45Slukem			esac ; '"$_fp_match"'
3051.45Slukem				echo -n "$_pref$_npid" ;
3061.45Slukem				_pref=" "
3071.45Slukem				;;
3081.45Slukem			esac
3091.45Slukem		done'
3101.45Slukem
3111.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
3121.45Slukem	eval $_proccheck
3131.11Slukem}
3141.11Slukem
3151.11Slukem#
3161.33Slukem# wait_for_pids pid [pid ...]
3171.35Slukem#	spins until none of the pids exist
3181.33Slukem#
3191.33Slukemwait_for_pids()
3201.33Slukem{
3211.56Schristos	_list="$@"
3221.33Slukem	if [ -z "$_list" ]; then
3231.33Slukem		return
3241.33Slukem	fi
3251.35Slukem	_prefix=
3261.35Slukem	while true; do
3271.33Slukem		_nlist="";
3281.33Slukem		for _j in $_list; do
3291.33Slukem			if kill -0 $_j 2>/dev/null; then
3301.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
3311.33Slukem			fi
3321.33Slukem		done
3331.33Slukem		if [ -z "$_nlist" ]; then
3341.33Slukem			break
3351.33Slukem		fi
3361.96Sroy		if [ "$_list" != "$_nlist" ]; then
3371.96Sroy			_list=$_nlist
3381.96Sroy			echo -n ${_prefix:-"Waiting for PIDS: "}$_list
3391.96Sroy			_prefix=", "
3401.96Sroy		fi
3411.96Sroy		# We want this to be a tight loop for a fast exit
3421.96Sroy		sleep 0.05
3431.33Slukem	done
3441.35Slukem	if [ -n "$_prefix" ]; then
3451.35Slukem		echo "."
3461.35Slukem	fi
3471.33Slukem}
3481.33Slukem
3491.33Slukem#
3501.81Sjmmv# run_rc_command argument [parameters]
3511.46Slukem#	Search for argument in the list of supported commands, which is:
3521.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
3531.46Slukem#	If there's a match, run ${argument}_cmd or the default method
3541.81Sjmmv#	(see below), and pass the optional list of parameters to it.
3551.11Slukem#
3561.46Slukem#	If argument has a given prefix, then change the operation as follows:
3571.46Slukem#		Prefix	Operation
3581.11Slukem#		------	---------
3591.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
3601.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
3611.61Slukem#		one	Set ${rcvar} to YES
3621.11Slukem#
3631.11Slukem#	The following globals are used:
3641.24Slukem#
3651.46Slukem#	Name		Needed	Purpose
3661.46Slukem#	----		------	-------
3671.11Slukem#	name		y	Name of script.
3681.24Slukem#
3691.11Slukem#	command		n	Full path to command.
3701.46Slukem#				Not needed if ${rc_arg}_cmd is set for
3711.11Slukem#				each keyword.
3721.24Slukem#
3731.11Slukem#	command_args	n	Optional args/shell directives for command.
3741.24Slukem#
3751.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
3761.45Slukem#				call check_{pidfile,process}() appropriately.
3771.45Slukem#
3781.16Slukem#	extra_commands	n	List of extra commands supported.
3791.24Slukem#
3801.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
3811.42Slukem#				otherwise use check_process $command.
3821.42Slukem#				In either case, only check if $command is set.
3831.42Slukem#
3841.42Slukem#	procname	n	Process name to check for instead of $command.
3851.24Slukem#
3861.24Slukem#	rcvar		n	This is checked with checkyesno to determine
3871.24Slukem#				if the action should be run.
3881.24Slukem#
3891.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
3901.42Slukem#				Requires /usr to be mounted.
3911.24Slukem#
3921.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
3931.22Slukem#				(if not using ${name}_chroot).
3941.24Slukem#
3951.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
3961.24Slukem#				NOTE:	$flags from the parent environment
3971.24Slukem#					can be used to override this.
3981.24Slukem#
3991.72She#	${name}_env	n	Additional environment variable settings
4001.72She#				for running ${command}
4011.72She#
4021.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
4031.24Slukem#
4041.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
4051.22Slukem#				using ${name}_chroot.
4061.42Slukem#				Requires /usr to be mounted.
4071.24Slukem#
4081.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
4091.42Slukem#				Requires /usr to be mounted.
4101.24Slukem#
4111.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
4121.32Slukem#				to run the chrooted ${command} with.
4131.42Slukem#				Requires /usr to be mounted.
4141.24Slukem#
4151.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
4161.11Slukem#				Otherwise, use default command (see below)
4171.24Slukem#
4181.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
4191.46Slukem#				${rc_arg}_cmd method in the default
4201.46Slukem#				operation (i.e, after checking for required
4211.46Slukem#				bits and process (non)existence).
4221.11Slukem#				If this completes with a non-zero exit code,
4231.46Slukem#				don't run ${rc_arg}_cmd.
4241.24Slukem#
4251.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
4261.46Slukem#				${rc_arg}_cmd method, if that method
4271.43Slukem#				returned a zero exit code.
4281.43Slukem#
4291.11Slukem#	required_dirs	n	If set, check for the existence of the given
4301.11Slukem#				directories before running the default
4311.11Slukem#				(re)start command.
4321.24Slukem#
4331.11Slukem#	required_files	n	If set, check for the readability of the given
4341.11Slukem#				files before running the default (re)start
4351.11Slukem#				command.
4361.24Slukem#
4371.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
4381.11Slukem#				listed variables before running the default
4391.11Slukem#				(re)start command.
4401.11Slukem#
4411.46Slukem#	Default behaviour for a given argument, if no override method is
4421.46Slukem#	provided:
4431.24Slukem#
4441.46Slukem#	Argument	Default behaviour
4451.46Slukem#	--------	-----------------
4461.11Slukem#	start		if !running && checkyesno ${rcvar}
4471.11Slukem#				${command}
4481.24Slukem#
4491.11Slukem#	stop		if ${pidfile}
4501.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
4511.11Slukem#			else
4521.46Slukem#				rc_pid=$(check_process $command)
4531.46Slukem#			kill $sig_stop $rc_pid
4541.46Slukem#			wait_for_pids $rc_pid
4551.35Slukem#			($sig_stop defaults to TERM.)
4561.24Slukem#
4571.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
4581.35Slukem#			and doesn't wait_for_pids.
4591.11Slukem#			$sig_reload defaults to HUP.
4601.24Slukem#
4611.11Slukem#	restart		Run `stop' then `start'.
4621.11Slukem#
4631.33Slukem#	status		Show if ${command} is running, etc.
4641.33Slukem#
4651.33Slukem#	poll		Wait for ${command} to exit.
4661.33Slukem#
4671.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
4681.33Slukem#
4691.46Slukem#	Variables available to methods, and after run_rc_command() has
4701.46Slukem#	completed:
4711.46Slukem#
4721.46Slukem#	Variable	Purpose
4731.46Slukem#	--------	-------
4741.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
4751.46Slukem#			performed
4761.46Slukem#
4771.46Slukem#	rc_flags	Flags to start the default command with.
4781.46Slukem#			Defaults to ${name}_flags, unless overridden
4791.46Slukem#			by $flags from the environment.
4801.46Slukem#			This variable may be changed by the precmd method.
4811.46Slukem#
4821.46Slukem#	rc_pid		PID of command (if appropriate)
4831.46Slukem#
4841.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
4851.46Slukem#
4861.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
4871.33Slukem#
4881.24Slukem#
4891.11Slukemrun_rc_command()
4901.11Slukem{
4911.46Slukem	rc_arg=$1
4921.24Slukem	if [ -z "$name" ]; then
4931.30Slukem		err 3 'run_rc_command: $name is not set.'
4941.11Slukem	fi
4951.11Slukem
4961.61Slukem	_rc_prefix=
4971.46Slukem	case "$rc_arg" in
4981.24Slukem	fast*)				# "fast" prefix; don't check pid
4991.46Slukem		rc_arg=${rc_arg#fast}
5001.48Slukem		rc_fast=yes
5011.11Slukem		;;
5021.61Slukem	force*)				# "force" prefix; always run
5031.48Slukem		rc_force=yes
5041.61Slukem		_rc_prefix=force
5051.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
5061.61Slukem		if [ -n "${rcvar}" ]; then
5071.61Slukem			eval ${rcvar}=YES
5081.61Slukem		fi
5091.61Slukem		;;
5101.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
5111.61Slukem		_rc_prefix=one
5121.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
5131.29Slukem		if [ -n "${rcvar}" ]; then
5141.29Slukem			eval ${rcvar}=YES
5151.29Slukem		fi
5161.11Slukem		;;
5171.11Slukem	esac
5181.11Slukem
5191.75Sreed	_keywords="start stop restart rcvar"
5201.75Sreed	if [ -n "$extra_commands" ]; then
5211.75Sreed		_keywords="${_keywords} ${extra_commands}"
5221.75Sreed	fi
5231.46Slukem	rc_pid=
5241.11Slukem	_pidcmd=
5251.42Slukem	_procname=${procname:-${command}}
5261.42Slukem
5271.24Slukem					# setup pid check command if not fast
5281.103Skre	if [ -z "$rc_fast" ] && [ -n "$_procname" ]; then
5291.11Slukem		if [ -n "$pidfile" ]; then
5301.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
5311.42Slukem		else
5321.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
5331.11Slukem		fi
5341.11Slukem		if [ -n "$_pidcmd" ]; then
5351.33Slukem			_keywords="${_keywords} status poll"
5361.11Slukem		fi
5371.11Slukem	fi
5381.11Slukem
5391.46Slukem	if [ -z "$rc_arg" ]; then
5401.11Slukem		rc_usage "$_keywords"
5411.11Slukem	fi
5421.81Sjmmv	shift	# remove $rc_arg from the positional parameters
5431.11Slukem
5441.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
5451.46Slukem		rc_flags=$flags
5461.11Slukem	else
5471.46Slukem		eval rc_flags=\$${name}_flags
5481.11Slukem	fi
5491.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
5501.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
5511.72She	    _group=\$${name}_group	_groups=\$${name}_groups \
5521.72She	    _env=\"\$${name}_env\"
5531.11Slukem
5541.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
5551.42Slukem		if [ "$_user" = "$(id -un)" ]; then
5561.42Slukem			unset _user
5571.42Slukem		fi
5581.42Slukem	fi
5591.42Slukem
5601.29Slukem					# if ${rcvar} is set, and $1 is not
5611.40Slukem					# "rcvar", then run
5621.29Slukem					#	checkyesno ${rcvar}
5631.74Ssalo					# and return if that failed or warn
5641.74Ssalo					# user and exit when interactive
5651.24Slukem					#
5661.103Skre	if [ -n "${rcvar}" ] && [ "$rc_arg" != "rcvar" ]; then
5671.24Slukem		if ! checkyesno ${rcvar}; then
5681.74Ssalo					# check whether interactive or not
5691.74Ssalo			if [ -n "$_run_rc_script" ]; then
5701.74Ssalo				return 0
5711.74Ssalo			fi
5721.74Ssalo			for _elem in $_keywords; do
5731.74Ssalo				if [ "$_elem" = "$rc_arg" ]; then
5741.88Sapb					cat 1>&2 <<EOF
5751.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}.
5761.88SapbUse the following if you wish to perform the operation:
5771.88Sapb  $0 one${rc_arg}
5781.88SapbEOF
5791.74Ssalo					exit 1
5801.74Ssalo				fi
5811.74Ssalo			done
5821.74Ssalo			echo 1>&2 "$0: unknown directive '$rc_arg'."
5831.74Ssalo			rc_usage "$_keywords"
5841.24Slukem		fi
5851.24Slukem	fi
5861.24Slukem
5871.24Slukem	eval $_pidcmd			# determine the pid if necessary
5881.11Slukem
5891.11Slukem	for _elem in $_keywords; do
5901.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
5911.11Slukem			continue
5921.11Slukem		fi
5931.11Slukem
5941.24Slukem					# if there's a custom ${XXX_cmd},
5951.24Slukem					# run that instead of the default
5961.24Slukem					#
5971.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
5981.46Slukem		    _postcmd=\$${rc_arg}_postcmd
5991.11Slukem		if [ -n "$_cmd" ]; then
6001.24Slukem					# if the precmd failed and force
6011.24Slukem					# isn't set, exit
6021.24Slukem					#
6031.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6041.24Slukem				return 1
6051.24Slukem			fi
6061.24Slukem
6071.81Sjmmv			if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then
6081.44Slukem				return 1
6091.44Slukem			fi
6101.44Slukem			eval $_postcmd
6111.11Slukem			return 0
6121.11Slukem		fi
6131.11Slukem
6141.81Sjmmv		if [ ${#} -gt 0 ]; then
6151.81Sjmmv			err 1 "the $rc_arg command does not take any parameters"
6161.81Sjmmv		fi
6171.81Sjmmv
6181.46Slukem		case "$rc_arg" in	# default operations...
6191.11Slukem
6201.11Slukem		status)
6211.46Slukem			if [ -n "$rc_pid" ]; then
6221.46Slukem				echo "${name} is running as pid $rc_pid."
6231.11Slukem			else
6241.11Slukem				echo "${name} is not running."
6251.28Slukem				return 1
6261.11Slukem			fi
6271.11Slukem			;;
6281.11Slukem
6291.11Slukem		start)
6301.46Slukem			if [ -n "$rc_pid" ]; then
6311.63Slukem				echo 1>&2 "${name} already running? (pid=$rc_pid)."
6321.11Slukem				exit 1
6331.11Slukem			fi
6341.11Slukem
6351.57Slukem			if [ ! -x ${_chroot}${command} ]; then
6361.11Slukem				return 0
6371.11Slukem			fi
6381.11Slukem
6391.24Slukem					# check for required variables,
6401.24Slukem					# directories, and files
6411.24Slukem					#
6421.11Slukem			for _f in $required_vars; do
6431.11Slukem				if ! checkyesno $_f; then
6441.65Slukem					warn "\$${_f} is not enabled."
6451.46Slukem					if [ -z "$rc_force" ]; then
6461.24Slukem						return 1
6471.24Slukem					fi
6481.11Slukem				fi
6491.11Slukem			done
6501.11Slukem			for _f in $required_dirs; do
6511.11Slukem				if [ ! -d "${_f}/." ]; then
6521.25Slukem					warn "${_f} is not a directory."
6531.46Slukem					if [ -z "$rc_force" ]; then
6541.24Slukem						return 1
6551.24Slukem					fi
6561.11Slukem				fi
6571.11Slukem			done
6581.11Slukem			for _f in $required_files; do
6591.11Slukem				if [ ! -r "${_f}" ]; then
6601.25Slukem					warn "${_f} is not readable."
6611.46Slukem					if [ -z "$rc_force" ]; then
6621.24Slukem						return 1
6631.24Slukem					fi
6641.11Slukem				fi
6651.11Slukem			done
6661.11Slukem
6671.24Slukem					# if the precmd failed and force
6681.24Slukem					# isn't set, exit
6691.24Slukem					#
6701.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6711.24Slukem				return 1
6721.24Slukem			fi
6731.24Slukem
6741.24Slukem					# setup the command to run, and run it
6751.29Slukem					#
6761.11Slukem			echo "Starting ${name}."
6771.22Slukem			if [ -n "$_chroot" ]; then
6781.22Slukem				_doit="\
6791.100Schristos$_env_clear_rc_vars $_env \
6801.23Slukem${_nice:+nice -n $_nice }\
6811.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
6821.46Slukem$_chroot $command $rc_flags $command_args"
6831.22Slukem			else
6841.22Slukem				_doit="\
6851.18Slukem${_chdir:+cd $_chdir; }\
6861.100Schristos$_env_clear_rc_vars $_env \
6871.18Slukem${_nice:+nice -n $_nice }\
6881.46Slukem$command $rc_flags $command_args"
6891.34Slukem				if [ -n "$_user" ]; then
6901.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
6911.34Slukem				fi
6921.22Slukem			fi
6931.43Slukem
6941.43Slukem					# if the cmd failed and force
6951.43Slukem					# isn't set, exit
6961.43Slukem					#
6971.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6981.43Slukem				return 1
6991.43Slukem			fi
7001.43Slukem
7011.43Slukem					# finally, run postcmd
7021.43Slukem					#
7031.43Slukem			eval $_postcmd
7041.11Slukem			;;
7051.11Slukem
7061.11Slukem		stop)
7071.46Slukem			if [ -z "$rc_pid" ]; then
7081.24Slukem				if [ -n "$pidfile" ]; then
7091.63Slukem					echo 1>&2 \
7101.24Slukem				    "${name} not running? (check $pidfile)."
7111.24Slukem				else
7121.63Slukem					echo 1>&2 "${name} not running?"
7131.11Slukem				fi
7141.24Slukem				exit 1
7151.11Slukem			fi
7161.11Slukem
7171.43Slukem					# if the precmd failed and force
7181.43Slukem					# isn't set, exit
7191.43Slukem					#
7201.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7211.24Slukem				return 1
7221.24Slukem			fi
7231.43Slukem
7241.43Slukem					# send the signal to stop
7251.43Slukem					#
7261.11Slukem			echo "Stopping ${name}."
7271.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
7281.34Slukem			if [ -n "$_user" ]; then
7291.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7301.34Slukem			fi
7311.43Slukem
7321.43Slukem					# if the stop cmd failed and force
7331.43Slukem					# isn't set, exit
7341.43Slukem					#
7351.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7361.43Slukem				return 1
7371.43Slukem			fi
7381.43Slukem
7391.43Slukem					# wait for the command to exit,
7401.43Slukem					# and run postcmd.
7411.46Slukem			wait_for_pids $rc_pid
7421.43Slukem			eval $_postcmd
7431.11Slukem			;;
7441.11Slukem
7451.11Slukem		reload)
7461.46Slukem			if [ -z "$rc_pid" ]; then
7471.24Slukem				if [ -n "$pidfile" ]; then
7481.63Slukem					echo 1>&2 \
7491.11Slukem				    "${name} not running? (check $pidfile)."
7501.24Slukem				else
7511.63Slukem					echo 1>&2 "${name} not running?"
7521.11Slukem				fi
7531.24Slukem				exit 1
7541.11Slukem			fi
7551.11Slukem			echo "Reloading ${name} config files."
7561.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7571.24Slukem				return 1
7581.24Slukem			fi
7591.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
7601.34Slukem			if [ -n "$_user" ]; then
7611.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7621.34Slukem			fi
7631.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7641.43Slukem				return 1
7651.43Slukem			fi
7661.43Slukem			eval $_postcmd
7671.11Slukem			;;
7681.11Slukem
7691.11Slukem		restart)
7701.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7711.24Slukem				return 1
7721.11Slukem			fi
7731.29Slukem					# prevent restart being called more
7741.29Slukem					# than once by any given script
7751.29Slukem					#
7761.53Slukem			if ${_rc_restart_done:-false}; then
7771.29Slukem				return 0
7781.29Slukem			fi
7791.53Slukem			_rc_restart_done=true
7801.33Slukem
7811.61Slukem			( $0 ${_rc_prefix}stop )
7821.61Slukem			$0 ${_rc_prefix}start
7831.11Slukem
7841.43Slukem			eval $_postcmd
7851.33Slukem			;;
7861.33Slukem
7871.33Slukem		poll)
7881.46Slukem			if [ -n "$rc_pid" ]; then
7891.46Slukem				wait_for_pids $rc_pid
7901.33Slukem			fi
7911.11Slukem			;;
7921.11Slukem
7931.11Slukem		rcvar)
7941.11Slukem			echo "# $name"
7951.24Slukem			if [ -n "$rcvar" ]; then
7961.24Slukem				if checkyesno ${rcvar}; then
7971.24Slukem					echo "\$${rcvar}=YES"
7981.24Slukem				else
7991.24Slukem					echo "\$${rcvar}=NO"
8001.24Slukem				fi
8011.11Slukem			fi
8021.11Slukem			;;
8031.11Slukem
8041.11Slukem		*)
8051.11Slukem			rc_usage "$_keywords"
8061.11Slukem			;;
8071.11Slukem
8081.11Slukem		esac
8091.11Slukem		return 0
8101.11Slukem	done
8111.11Slukem
8121.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
8131.11Slukem	rc_usage "$_keywords"
8141.11Slukem	exit 1
8151.11Slukem}
8161.11Slukem
8171.11Slukem#
8181.94Sapb# _have_rc_postprocessor
8191.94Sapb#	Test whether the current script is running in a context that
8201.94Sapb#	was invoked from /etc/rc with a postprocessor.
8211.94Sapb#
8221.94Sapb#	If the test fails, some variables may be unset to make
8231.94Sapb#	such tests more efficient in future.
8241.94Sapb#
8251.94Sapb_have_rc_postprocessor()
8261.94Sapb{
8271.94Sapb	# Cheap tests that fd and pid are set, fd is writable.
8281.97Sphx	[ -n "${_rc_pid}" ] || { unset _rc_pid; return 1; }
8291.97Sphx	[ -n "${_rc_postprocessor_fd}" ] || { unset _rc_pid; return 1; }
8301.97Sphx	eval ": >&${_rc_postprocessor_fd}" 2>/dev/null \
8311.94Sapb	|| { unset _rc_pid; return 1; }
8321.94Sapb
8331.94Sapb	return 0
8341.94Sapb}
8351.94Sapb
8361.94Sapb#
8371.11Slukem# run_rc_script file arg
8381.11Slukem#	Start the script `file' with `arg', and correctly handle the
8391.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
8401.37Slukem#	sourced into the current environment.  If `file' appears to be
8411.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
8421.37Slukem#	executable run as a child process.
8431.17Slukem#
8441.78Sapb#	If `file' contains "KEYWORD: interactive" and if we are
8451.94Sapb#	running inside /etc/rc with postprocessing, then the script's
8461.94Sapb#	stdout and stderr are redirected to $_rc_original_stdout_fd and
8471.78Sapb#	$_rc_original_stderr_fd, so the output will be displayed on the
8481.78Sapb#	console but not intercepted by /etc/rc's postprocessor.
8491.78Sapb#
8501.11Slukemrun_rc_script()
8511.11Slukem{
8521.11Slukem	_file=$1
8531.11Slukem	_arg=$2
8541.103Skre	if [ -z "$_file" ] || [ -z "$_arg" ]; then
8551.11Slukem		err 3 'USAGE: run_rc_script file arg'
8561.11Slukem	fi
8571.11Slukem
8581.74Ssalo	_run_rc_script=true
8591.74Ssalo
8601.45Slukem	unset	name command command_args command_interpreter \
8611.45Slukem		extra_commands pidfile procname \
8621.42Slukem		rcvar required_dirs required_files required_vars
8631.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
8641.39Slukem
8651.78Sapb	_must_redirect=false
8661.94Sapb	if _have_rc_postprocessor \
8671.78Sapb	    && _has_rcorder_keyword interactive $_file
8681.78Sapb	then
8691.78Sapb		_must_redirect=true
8701.78Sapb	fi
8711.78Sapb
8721.39Slukem	case "$_file" in
8731.39Slukem	*.sh)				# run in current shell
8741.78Sapb		if $_must_redirect; then
8751.78Sapb			print_rc_metadata \
8761.78Sapb			    "note:Output from ${_file} is not logged"
8771.80Sapb			no_rc_postprocess eval \
8781.80Sapb			    'set $_arg ; . $_file'
8791.78Sapb		else
8801.78Sapb			set $_arg ; . $_file
8811.78Sapb		fi
8821.39Slukem		;;
8831.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
8841.39Slukem		warn "Ignoring scratch file $_file"
8851.39Slukem		;;
8861.39Slukem	*)				# run in subshell
8871.78Sapb		if [ -x $_file ] && $_must_redirect; then
8881.78Sapb			print_rc_metadata \
8891.78Sapb			    "note:Output from ${_file} is not logged"
8901.78Sapb			if [ -n "$rc_fast_and_loose" ]; then
8911.80Sapb				no_rc_postprocess eval \
8921.80Sapb				    'set $_arg ; . $_file'
8931.78Sapb			else
8941.80Sapb				no_rc_postprocess eval \
8951.80Sapb				    '( set $_arg ; . $_file )'
8961.78Sapb			fi
8971.78Sapb		elif [ -x $_file ]; then
8981.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
8991.39Slukem				set $_arg ; . $_file
9001.39Slukem			else
9011.37Slukem				( set $_arg ; . $_file )
9021.37Slukem			fi
9031.78Sapb		else
9041.78Sapb			warn "Ignoring non-executable file $_file"
9051.39Slukem		fi
9061.39Slukem		;;
9071.39Slukem	esac
9081.11Slukem}
9091.19Slukem
9101.19Slukem#
9111.65Slukem# load_rc_config command
9121.19Slukem#	Source in the configuration file for a given command.
9131.19Slukem#
9141.19Slukemload_rc_config()
9151.19Slukem{
9161.19Slukem	_command=$1
9171.19Slukem	if [ -z "$_command" ]; then
9181.19Slukem		err 3 'USAGE: load_rc_config command'
9191.19Slukem	fi
9201.19Slukem
9211.54Slukem	if ${_rc_conf_loaded:-false}; then
9221.54Slukem		:
9231.54Slukem	else
9241.30Slukem		. /etc/rc.conf
9251.53Slukem		_rc_conf_loaded=true
9261.30Slukem	fi
9271.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
9281.20Sfvdl		. /etc/rc.conf.d/"$_command"
9291.20Sfvdl	fi
9301.19Slukem}
9311.19Slukem
9321.65Slukem#
9331.65Slukem# load_rc_config_var cmd var
9341.65Slukem#	Read the rc.conf(5) var for cmd and set in the
9351.65Slukem#	current shell, using load_rc_config in a subshell to prevent
9361.65Slukem#	unwanted side effects from other variable assignments.
9371.65Slukem#
9381.65Slukemload_rc_config_var()
9391.65Slukem{
9401.65Slukem	if [ $# -ne 2 ]; then
9411.65Slukem		err 3 'USAGE: load_rc_config_var cmd var'
9421.65Slukem	fi
9431.65Slukem	eval $(eval '(
9441.65Slukem		load_rc_config '$1' >/dev/null;
9451.103Skre		if [ -n "${'$2'}" ] || [ "${'$2'-UNSET}" != "UNSET" ]; then
9461.65Slukem			echo '$2'=\'\''${'$2'}\'\'';
9471.65Slukem		fi
9481.65Slukem	)' )
9491.65Slukem}
9501.11Slukem
9511.11Slukem#
9521.11Slukem# rc_usage commands
9531.11Slukem#	Print a usage string for $0, with `commands' being a list of
9541.11Slukem#	valid commands.
9551.11Slukem#
9561.11Slukemrc_usage()
9571.11Slukem{
9581.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
9591.11Slukem
9601.11Slukem	_sep=
9611.56Schristos	for _elem; do
9621.11Slukem		echo -n 1>&2 "$_sep$_elem"
9631.11Slukem		_sep="|"
9641.11Slukem	done
9651.11Slukem	echo 1>&2 ")"
9661.11Slukem	exit 1
9671.11Slukem}
9681.11Slukem
9691.11Slukem#
9701.11Slukem# err exitval message
9711.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
9721.11Slukem#
9731.11Slukemerr()
9741.11Slukem{
9751.11Slukem	exitval=$1
9761.11Slukem	shift
9771.11Slukem
9781.51Sgrant	if [ -x /usr/bin/logger ]; then
9791.51Sgrant		logger "$0: ERROR: $*"
9801.51Sgrant	fi
9811.21Slukem	echo 1>&2 "$0: ERROR: $*"
9821.11Slukem	exit $exitval
9831.11Slukem}
9841.11Slukem
9851.11Slukem#
9861.11Slukem# warn message
9871.11Slukem#	Display message to stderr and log to the syslog.
9881.11Slukem#
9891.11Slukemwarn()
9901.11Slukem{
9911.51Sgrant	if [ -x /usr/bin/logger ]; then
9921.51Sgrant		logger "$0: WARNING: $*"
9931.51Sgrant	fi
9941.21Slukem	echo 1>&2 "$0: WARNING: $*"
9951.31Satatat}
9961.31Satatat
9971.31Satatat#
9981.31Satatat# backup_file action file cur backup
9991.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
10001.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
10011.31Satatat#
10021.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
10031.31Satatat#	which can be either YES or NO.
10041.31Satatat#
10051.31Satatat#	The `action' keyword can be one of the following:
10061.31Satatat#
10071.31Satatat#	add		`file' is now being backed up (and is possibly
10081.31Satatat#			being reentered into the backups system).  `cur'
10091.31Satatat#			is created and RCS files, if necessary, are
10101.31Satatat#			created as well.
10111.31Satatat#
10121.31Satatat#	update		`file' has changed and needs to be backed up.
10131.31Satatat#			If `cur' exists, it is copied to to `back' or
10141.31Satatat#			checked into RCS (if the repository file is old),
10151.31Satatat#			and then `file' is copied to `cur'.  Another RCS
10161.31Satatat#			check in done here if RCS is being used.
10171.31Satatat#
10181.31Satatat#	remove		`file' is no longer being tracked by the backups
10191.31Satatat#			system.  If RCS is not being used, `cur' is moved
10201.31Satatat#			to `back', otherwise an empty file is checked in,
10211.31Satatat#			and then `cur' is removed.
10221.31Satatat#
10231.31Satatat#
10241.31Satatatbackup_file()
10251.31Satatat{
10261.31Satatat	_action=$1
10271.31Satatat	_file=$2
10281.31Satatat	_cur=$3
10291.31Satatat	_back=$4
10301.31Satatat
10311.31Satatat	if checkyesno backup_uses_rcs; then
10321.31Satatat		_msg0="backup archive"
10331.31Satatat		_msg1="update"
10341.31Satatat
10351.36Satatat		# ensure that history file is not locked
10361.36Satatat		if [ -f $_cur,v ]; then
10371.36Satatat			rcs -q -u -U -M $_cur
10381.36Satatat		fi
10391.36Satatat
10401.31Satatat		# ensure after switching to rcs that the
10411.31Satatat		# current backup is not lost
10421.31Satatat		if [ -f $_cur ]; then
10431.31Satatat			# no archive, or current newer than archive
10441.103Skre			if [ ! -f $_cur,v ] || [ $_cur -nt $_cur,v ]; then
10451.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10461.36Satatat				rcs -q -kb -U $_cur
10471.49Slukem				co -q -f -u $_cur
10481.31Satatat			fi
10491.31Satatat		fi
10501.31Satatat
10511.31Satatat		case $_action in
10521.31Satatat		add|update)
10531.31Satatat			cp -p $_file $_cur
10541.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10551.36Satatat			rcs -q -kb -U $_cur
10561.49Slukem			co -q -f -u $_cur
10571.31Satatat			chown root:wheel $_cur $_cur,v
10581.31Satatat			;;
10591.31Satatat		remove)
10601.31Satatat			cp /dev/null $_cur
10611.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10621.36Satatat			rcs -q -kb -U $_cur
10631.31Satatat			chown root:wheel $_cur $_cur,v
10641.31Satatat			rm $_cur
10651.31Satatat			;;
10661.31Satatat		esac
10671.31Satatat	else
10681.31Satatat		case $_action in
10691.31Satatat		add|update)
10701.31Satatat			if [ -f $_cur ]; then
10711.31Satatat				cp -p $_cur $_back
10721.31Satatat			fi
10731.31Satatat			cp -p $_file $_cur
10741.31Satatat			chown root:wheel $_cur
10751.31Satatat			;;
10761.31Satatat		remove)
10771.31Satatat			mv -f $_cur $_back
10781.31Satatat			;;
10791.31Satatat		esac
10801.31Satatat	fi
10811.1Scjs}
10821.64Smycroft
10831.76Schristos#
10841.76Schristos# handle_fsck_error fsck_exit_code
10851.76Schristos#	Take action depending on the return code from fsck.
10861.76Schristos#
10871.76Schristoshandle_fsck_error()
10881.76Schristos{
10891.76Schristos	case $1 in
10901.76Schristos	0)	# OK
10911.76Schristos		return
10921.76Schristos		;;
10931.76Schristos	2)	# Needs re-run, still fs errors
10941.76Schristos		echo "File system still has errors; re-run fsck manually!"
10951.76Schristos		;;
10961.76Schristos	4)	# Root modified
10971.93Swiz		echo "Root file system was modified, rebooting ..."
10981.76Schristos		reboot -n
10991.76Schristos		echo "Reboot failed; help!"
11001.76Schristos		;;
11011.76Schristos	8)	# Check failed
11021.76Schristos		echo "Automatic file system check failed; help!"
11031.76Schristos		;;
11041.76Schristos	12)	# Got signal
11051.76Schristos		echo "Boot interrupted."
11061.76Schristos		;;
11071.76Schristos	*)
11081.76Schristos		echo "Unknown error $1; help!"
11091.76Schristos		;;
11101.76Schristos	esac
11111.76Schristos	stop_boot
11121.76Schristos}
11131.76Schristos
11141.78Sapb#
11151.78Sapb# _has_rcorder_keyword word file
11161.78Sapb#	Check whether a file contains a "# KEYWORD:" comment with a
11171.78Sapb#	specified keyword in the style used by rcorder(8).
11181.78Sapb#
11191.78Sapb_has_rcorder_keyword()
11201.78Sapb{
11211.78Sapb	local word="$1"
11221.78Sapb	local file="$2"
11231.78Sapb	local line
11241.78Sapb
11251.78Sapb	[ -r "$file" ] || return 1
11261.78Sapb	while read line; do
11271.78Sapb		case "${line} " in
11281.78Sapb		"# KEYWORD:"*[\ \	]"${word}"[\ \	]*)
11291.78Sapb			return 0
11301.78Sapb			;;
11311.78Sapb		"#"*)
11321.78Sapb			continue
11331.78Sapb			;;
11341.78Sapb		*[A-Za-z0-9]*)
11351.78Sapb			# give up at the first non-empty non-comment line
11361.78Sapb			return 1
11371.78Sapb			;;
11381.78Sapb		esac
11391.78Sapb	done <"$file"
11401.78Sapb	return 1
11411.78Sapb}
11421.78Sapb
11431.78Sapb#
11441.78Sapb# print_rc_metadata string
11451.78Sapb#	Print the specified string in such a way that the post-processor
11461.78Sapb#	inside /etc/rc will treat it as meta-data.
11471.78Sapb#
11481.78Sapb#	If we are not running inside /etc/rc, do nothing.
11491.78Sapb#
11501.78Sapb#	For public use by any rc.d script, the string must begin with
11511.78Sapb#	"note:", followed by arbitrary text.  The intent is that the text
11521.78Sapb#	will appear in a log file but not on the console.
11531.78Sapb#
11541.78Sapb#	For private use within /etc/rc, the string must contain a
11551.78Sapb#	keyword recognised by the rc_postprocess_metadata() function
11561.78Sapb#	defined in /etc/rc, followed by a colon, followed by one or more
11571.78Sapb#	colon-separated arguments associated with the keyword.
11581.78Sapb#
11591.78Sapbprint_rc_metadata()
11601.78Sapb{
11611.78Sapb	# _rc_postprocessor fd, if defined, is the fd to which we must
11621.78Sapb	# print, prefixing the output with $_rc_metadata_prefix.
11631.78Sapb	#
11641.94Sapb	if _have_rc_postprocessor; then
11651.88Sapb		command printf "%s%s\n" "$rc_metadata_prefix" "$1" \
11661.78Sapb			>&${_rc_postprocessor_fd}
11671.78Sapb	fi
11681.78Sapb}
11691.78Sapb
11701.78Sapb#
11711.88Sapb# _flush_rc_output
11721.88Sapb#	Arrange for output to be flushed, if we are running
11731.88Sapb#	inside /etc/rc with postprocessing.
11741.88Sapb#
11751.88Sapb_flush_rc_output()
11761.88Sapb{
11771.88Sapb	print_rc_metadata "nop"
11781.88Sapb}
11791.88Sapb
11801.88Sapb#
11811.88Sapb# print_rc_normal [-n] string
11821.78Sapb#	Print the specified string in such way that it is treated as
11831.78Sapb#	normal output, regardless of whether or not we are running
11841.78Sapb#	inside /etc/rc with post-processing.
11851.78Sapb#
11861.88Sapb#	If "-n" is specified in $1, then the string in $2 is printed
11871.88Sapb#	without a newline; otherwise, the string in $1 is printed
11881.88Sapb#	with a newline.
11891.88Sapb#
11901.88Sapb#	Intended use cases include:
11911.88Sapb#
11921.88Sapb#	o   An rc.d script can use ``print_rc_normal -n'' to print a
11931.88Sapb#	    partial line in such a way that it appears immediately
11941.88Sapb#	    instead of being buffered by rc(8)'s post-processor.
11951.88Sapb#
11961.88Sapb#	o   An rc.d script that is run via the no_rc_postprocess
11971.88Sapb#	    function (so most of its output is invisible to rc(8)'s
11981.88Sapb#	    post-processor) can use print_rc_normal to force some of its
11991.88Sapb#	    output to be seen by the post-processor.
12001.88Sapb#
12011.78Sapb#
12021.78Sapbprint_rc_normal()
12031.78Sapb{
12041.94Sapb	# print to stdout or _rc_postprocessor_fd, depending on
12051.94Sapb	# whether not we have an rc postprocessor.
12061.78Sapb	#
12071.94Sapb	local fd=1
12081.94Sapb	_have_rc_postprocessor && fd="${_rc_postprocessor_fd}"
12091.88Sapb	case "$1" in
12101.88Sapb	"-n")
12111.88Sapb		command printf "%s" "$2" >&${fd}
12121.88Sapb		_flush_rc_output
12131.88Sapb		;;
12141.88Sapb	*)
12151.88Sapb		command printf "%s\n" "$1" >&${fd}
12161.88Sapb		;;
12171.88Sapb	esac
12181.78Sapb}
12191.78Sapb
12201.78Sapb#
12211.78Sapb# no_rc_postprocess cmd...
12221.78Sapb#	Execute the specified command in such a way that its output
12231.78Sapb#	bypasses the post-processor that handles the output from
12241.78Sapb#	most commands that are run inside /etc/rc.  If we are not
12251.78Sapb#	inside /etc/rc, then just execute the command without special
12261.78Sapb#	treatment.
12271.78Sapb#
12281.78Sapb#	The intent is that interactive commands can be run via
12291.78Sapb#	no_rc_postprocess(), and their output will apear immediately
12301.78Sapb#	on the console instead of being hidden or delayed by the
12311.78Sapb#	post-processor.	 An unfortunate consequence of the output
12321.78Sapb#	bypassing the post-processor is that the output will not be
12331.78Sapb#	logged.
12341.78Sapb#
12351.78Sapbno_rc_postprocess()
12361.78Sapb{
12371.94Sapb	if _have_rc_postprocessor; then
12381.78Sapb		"$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
12391.78Sapb	else
12401.78Sapb		"$@"
12411.78Sapb	fi
12421.78Sapb}
12431.78Sapb
12441.78Sapb#
12451.78Sapb# twiddle
12461.78Sapb#	On each call, print a different one of "/", "-", "\\", "|",
12471.78Sapb#	followed by a backspace.  The most recently printed value is
12481.78Sapb#	saved in $_twiddle_state.
12491.78Sapb#
12501.78Sapb#	Output is to /dev/tty, so this function may be useful even inside
12511.78Sapb#	a script whose output is redirected.
12521.78Sapb#
12531.78Sapbtwiddle()
12541.78Sapb{
12551.78Sapb	case "$_twiddle_state" in
12561.78Sapb	'/')	_next='-' ;;
12571.78Sapb	'-')	_next='\' ;;
12581.78Sapb	'\')	_next='|' ;;
12591.78Sapb	*)	_next='/' ;;
12601.78Sapb	esac
12611.88Sapb	command printf "%s\b" "$_next" >/dev/tty
12621.78Sapb	_twiddle_state="$_next"
12631.78Sapb}
12641.78Sapb
12651.82Schristos#
12661.82Schristos# human_exit_code
12671.82Schristos#	Print the a human version of the exit code.
12681.82Schristos#
12691.82Schristoshuman_exit_code()
12701.82Schristos{
12711.83Schristos	if [ "$1" -lt 127 ]
12721.82Schristos	then
12731.82Schristos		echo "exited with code $1"
12741.85Schristos	elif [ "$(expr $1 % 256)" -eq 127 ]
12751.82Schristos	then
12761.84Schristos		# This cannot really happen because the shell will not
12771.84Schristos		# pass stopped job status out and the exit code is limited
12781.84Schristos		# to 8 bits. This code is here just for completeness.
12791.82Schristos		echo "stopped with signal $(expr $1 / 256)"
12801.82Schristos	else
12811.82Schristos		echo "terminated with signal $(expr $1 - 128)"
12821.82Schristos	fi
12831.82Schristos}
12841.86Sapb
12851.86Sapb#
12861.86Sapb# collapse_backslash_newline
12871.86Sapb#	Copy input to output, collapsing <backslash><newline>
12881.86Sapb#	to nothing, but leaving other backslashes alone.
12891.86Sapb#
12901.86Sapbcollapse_backslash_newline()
12911.86Sapb{
12921.86Sapb	local line
12931.86Sapb	while read -r line ; do
12941.86Sapb		case "$line" in
12951.86Sapb		*\\)
12961.86Sapb			# print it, without the backslash or newline
12971.88Sapb			command printf "%s" "${line%?}"
12981.86Sapb			;;
12991.86Sapb		*)
13001.86Sapb			# print it, with a newline
13011.88Sapb			command printf "%s\n" "${line}"
13021.86Sapb			;;
13031.86Sapb		esac
13041.86Sapb	done
13051.86Sapb}
13061.82Schristos
13071.92Sapb# Shell implementations of basename and dirname, usable before
13081.92Sapb# the /usr file system is mounted.
13091.92Sapb#
13101.92Sapbbasename()
13111.92Sapb{
13121.92Sapb	local file="$1"
13131.92Sapb	local suffix="$2"
13141.92Sapb	local base
13151.92Sapb
13161.92Sapb	base="${file##*/}"		# remove up to and including last '/'
13171.92Sapb	base="${base%${suffix}}"	# remove suffix, if any
13181.92Sapb	command printf "%s\n" "${base}"
13191.92Sapb}
13201.92Sapb
13211.92Sapbdirname()
13221.92Sapb{
13231.92Sapb	local file="$1"
13241.92Sapb	local dir
13251.92Sapb
13261.92Sapb	case "$file" in
13271.92Sapb	/*/*)	dir="${file%/*}" ;;	# common case: absolute path
13281.92Sapb	/*)	dir="/" ;;		# special case: name in root dir
13291.92Sapb	*/*)	dir="${file%/*}" ;;	# common case: relative path with '/'
13301.92Sapb	*)	dir="." ;;		# special case: name without '/'
13311.92Sapb	esac
13321.92Sapb	command printf "%s\n" "${dir}"
13331.92Sapb}
13341.92Sapb
13351.88Sapb# Override the normal "echo" and "printf" commands, so that
13361.88Sapb# partial lines printed by rc.d scripts appear immediately,
13371.88Sapb# instead of being buffered by rc(8)'s post-processor.
13381.88Sapb#
13391.88Sapb# Naive use of the echo or printf commands from rc.d scripts,
13401.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr,
13411.88Sapb# will call these functions.  To call the real echo and printf
13421.88Sapb# commands, use "command echo" or "command printf".
13431.88Sapb#
13441.103Skre# Avoid use of echo altogether as much as possible, printf works better
13451.103Skre#
13461.88Sapbecho()
13471.88Sapb{
13481.103Skre	local IFS=' ' NL='\n'	# not a literal newline...
13491.103Skre
13501.88Sapb	case "$1" in
13511.103Skre	-n)	NL=; shift;;
13521.88Sapb	esac
13531.103Skre
13541.103Skre	command printf "%s${NL}" "$*"
13551.103Skre
13561.103Skre	if test -z "${NL}"
13571.103Skre	then
13581.103Skre		_flush_rc_output
13591.103Skre	fi
13601.103Skre	return 0
13611.88Sapb}
13621.103Skre
13631.88Sapbprintf()
13641.88Sapb{
13651.88Sapb	command printf "$@"
13661.88Sapb	case "$1" in
13671.88Sapb	*'\n')	: ;;
13681.88Sapb	*)	_flush_rc_output ;;
13691.88Sapb	esac
13701.103Skre	return 0
13711.88Sapb}
13721.88Sapb
13731.98Schristoskat() {
13741.98Schristos	local i
13751.98Schristos	local v
13761.98Schristos	for i; do
13771.98Schristos		while read -r v; do
13781.98Schristos			v="${v%%#*}"
13791.98Schristos			if [ -z "$v" ]; then
13801.98Schristos				continue
13811.98Schristos			fi
13821.98Schristos			echo "$v"
13831.98Schristos		done < "$i"
13841.98Schristos	done
13851.98Schristos}
13861.98Schristos
13871.64Smycroft_rc_subr_loaded=:
1388