rc.subr revision 1.96
11.96Sroy# $NetBSD: rc.subr,v 1.96 2014/10/07 19:09:45 roy 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.11Slukem#
401.11Slukem#	functions
411.11Slukem#	---------
421.1Scjs
431.5Slukem#
441.11Slukem# checkyesno var
451.95Sroy#	Test $1 variable.
461.95Sroy#	Return 0 if it's "yes" (et al), 1 if it's "no" (et al), 2 otherwise.
471.5Slukem#
481.95Sroycheckyesnox()
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.95Sroy		return 2
641.3Slukem		;;
651.3Slukem	esac
661.38Slukem}
671.38Slukem
681.65Slukem#
691.95Sroy# checkyesno var
701.95Sroy#	Test $1 variable, and warn if not set to YES or NO.
711.95Sroy#	Return 0 if it's "yes" (et al), nonzero otherwise.
721.95Sroy#
731.95Sroycheckyesno()
741.95Sroy{
751.95Sroy	local var
761.95Sroy
771.95Sroy	checkyesnox $1
781.95Sroy	var=$?
791.95Sroy	[ $var = 0 -o $var = 1 ] && return $var
801.95Sroy	warn "\$${1} is not set properly - see ${rcvar_manpage}."
811.95Sroy	return 1
821.95Sroy}
831.95Sroy
841.95Sroy#
851.78Sapb# yesno_to_truefalse var
861.78Sapb#	Convert the value of a variable from any of the values
871.78Sapb#	understood by checkyesno() to "true" or "false".
881.78Sapb#
891.78Sapbyesno_to_truefalse()
901.78Sapb{
911.78Sapb	local var=$1
921.78Sapb	if checkyesno $var; then
931.78Sapb		eval $var=true
941.78Sapb		return 0
951.78Sapb	else
961.78Sapb		eval $var=false
971.78Sapb		return 1
981.78Sapb	fi
991.78Sapb}
1001.78Sapb
1011.78Sapb#
1021.38Slukem# reverse_list list
1031.38Slukem#	print the list in reverse order
1041.38Slukem#
1051.38Slukemreverse_list()
1061.38Slukem{
1071.38Slukem	_revlist=
1081.56Schristos	for _revfile; do
1091.38Slukem		_revlist="$_revfile $_revlist"
1101.38Slukem	done
1111.38Slukem	echo $_revlist
1121.6Smellon}
1131.6Smellon
1141.6Smellon#
1151.69Sapb# If booting directly to multiuser, send SIGTERM to
1161.69Sapb# the parent (/etc/rc) to abort the boot.
1171.69Sapb# Otherwise just exit.
1181.69Sapb#
1191.69Sapbstop_boot()
1201.69Sapb{
1211.69Sapb	if [ "$autoboot" = yes ]; then
1221.69Sapb		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
1231.69Sapb		kill -TERM ${RC_PID}
1241.69Sapb	fi
1251.69Sapb	exit 1
1261.69Sapb}
1271.69Sapb
1281.69Sapb#
1291.47Slukem# mount_critical_filesystems type
1301.93Swiz#	Go through the list of critical file systems as provided in
1311.47Slukem#	the rc.conf(5) variable $critical_filesystems_${type}, checking
1321.47Slukem#	each one to see if it is mounted, and if it is not, mounting it.
1331.79Sapb#	It's not an error if file systems prefixed with "OPTIONAL:"
1341.79Sapb#	are not mentioned in /etc/fstab.
1351.6Smellon#
1361.11Slukemmount_critical_filesystems()
1371.11Slukem{
1381.47Slukem	eval _fslist=\$critical_filesystems_${1}
1391.79Sapb	_mountcrit_es=0
1401.17Slukem	for _fs in $_fslist; do
1411.79Sapb		_optional=false
1421.79Sapb		case "$_fs" in
1431.79Sapb		OPTIONAL:*)
1441.79Sapb			_optional=true
1451.79Sapb			_fs="${_fs#*:}"
1461.79Sapb			;;
1471.79Sapb		esac
1481.79Sapb		_ismounted=false
1491.79Sapb		# look for a line like "${fs} on * type *"
1501.79Sapb		# or "* on ${fs} type *" in the output from mount.
1511.79Sapb		case "${nl}$( mount )${nl}" in
1521.79Sapb		*" on ${_fs} type "*)
1531.79Sapb			_ismounted=true
1541.79Sapb			;;
1551.79Sapb		*"${nl}${_fs} on "*)
1561.79Sapb			_ismounted=true
1571.79Sapb			;;
1581.79Sapb		esac
1591.79Sapb		if $_ismounted; then
1601.79Sapb			print_rc_metadata \
1611.79Sapb			"note:File system ${_fs} was already mounted"
1621.79Sapb		else
1631.79Sapb			_mount_output=$( mount $_fs 2>&1 )
1641.79Sapb			_mount_es=$?
1651.79Sapb			case "$_mount_output" in
1661.79Sapb			*"${nl}"*)
1671.79Sapb				# multiple lines can't be good,
1681.79Sapb				# not even if $_optional is true
1691.79Sapb				;;
1701.89Sapb			*[uU]'nknown special file or file system'*)
1711.79Sapb				if $_optional; then
1721.79Sapb					# ignore this error
1731.79Sapb					print_rc_metadata \
1741.79Sapb			"note:Optional file system ${_fs} is not present"
1751.79Sapb					_mount_es=0
1761.79Sapb					_mount_output=""
1771.6Smellon				fi
1781.79Sapb				;;
1791.79Sapb			esac
1801.79Sapb			if [ -n "$_mount_output" ]; then
1811.79Sapb				printf >&2 "%s\n" "$_mount_output"
1821.79Sapb			fi
1831.79Sapb			if [ "$_mount_es" != 0 ]; then
1841.79Sapb				_mountcrit_es="$_mount_es"
1851.6Smellon			fi
1861.79Sapb		fi
1871.6Smellon	done
1881.79Sapb	return $_mountcrit_es
1891.7Scjs}
1901.7Scjs
1911.11Slukem#
1921.45Slukem# check_pidfile pidfile procname [interpreter]
1931.45Slukem#	Parses the first line of pidfile for a PID, and ensures
1941.11Slukem#	that the process is running and matches procname.
1951.45Slukem#	Prints the matching PID upon success, nothing otherwise.
1961.45Slukem#	interpreter is optional; see _find_processes() for details.
1971.11Slukem#
1981.11Slukemcheck_pidfile()
1991.11Slukem{
2001.11Slukem	_pidfile=$1
2011.11Slukem	_procname=$2
2021.45Slukem	_interpreter=$3
2031.11Slukem	if [ -z "$_pidfile" -o -z "$_procname" ]; then
2041.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
2051.11Slukem	fi
2061.11Slukem	if [ ! -f $_pidfile ]; then
2071.11Slukem		return
2081.11Slukem	fi
2091.11Slukem	read _pid _junk < $_pidfile
2101.11Slukem	if [ -z "$_pid" ]; then
2111.11Slukem		return
2121.11Slukem	fi
2131.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
2141.11Slukem}
2151.11Slukem
2161.11Slukem#
2171.45Slukem# check_process procname [interpreter]
2181.11Slukem#	Ensures that a process (or processes) named procname is running.
2191.45Slukem#	Prints a list of matching PIDs.
2201.45Slukem#	interpreter is optional; see _find_processes() for details.
2211.11Slukem#
2221.11Slukemcheck_process()
2231.11Slukem{
2241.11Slukem	_procname=$1
2251.45Slukem	_interpreter=$2
2261.11Slukem	if [ -z "$_procname" ]; then
2271.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
2281.45Slukem	fi
2291.45Slukem	_find_processes $_procname ${_interpreter:-.} '-ax'
2301.45Slukem}
2311.45Slukem
2321.46Slukem#
2331.45Slukem# _find_processes procname interpreter psargs
2341.45Slukem#	Search for procname in the output of ps generated by psargs.
2351.45Slukem#	Prints the PIDs of any matching processes, space separated.
2361.45Slukem#
2371.45Slukem#	If interpreter == ".", check the following variations of procname
2381.45Slukem#	against the first word of each command:
2391.45Slukem#		procname
2401.45Slukem#		`basename procname`
2411.45Slukem#		`basename procname` + ":"
2421.45Slukem#		"(" + `basename procname` + ")"
2431.45Slukem#
2441.45Slukem#	If interpreter != ".", read the first line of procname, remove the
2451.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
2461.45Slukem#	match that against each command, either as is, or with extra words
2471.73Sdholland#	at the end.  As an alternative, to deal with interpreted daemons
2481.66She#	using perl, the basename of the interpreter plus a colon is also
2491.66She#	tried as the prefix to procname.
2501.45Slukem#
2511.45Slukem_find_processes()
2521.45Slukem{
2531.45Slukem	if [ $# -ne 3 ]; then
2541.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
2551.11Slukem	fi
2561.45Slukem	_procname=$1
2571.45Slukem	_interpreter=$2
2581.45Slukem	_psargs=$3
2591.45Slukem
2601.11Slukem	_pref=
2611.68Shubertf	_procnamebn=${_procname##*/}
2621.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
2631.67Selad		read _interp < ${_chroot:-}/$_procname	# read interpreter name
2641.45Slukem		_interp=${_interp#\#!}		# strip #!
2651.45Slukem		set -- $_interp
2661.87Schristos		if [ $1 = "/usr/bin/env" ]; then
2671.87Schristos			shift
2681.87Schristos			set -- $(type $1)
2691.87Schristos			shift $(($# - 1))
2701.87Schristos			_interp="${1##*/} $_procname"
2711.87Schristos		else
2721.87Schristos			_interp="$* $_procname"
2731.87Schristos		fi
2741.45Slukem		if [ $_interpreter != $1 ]; then
2751.45Slukem			warn "\$command_interpreter $_interpreter != $1"
2761.45Slukem		fi
2771.66She		_interpbn=${1##*/}
2781.45Slukem		_fp_args='_argv'
2791.45Slukem		_fp_match='case "$_argv" in
2801.68Shubertf		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
2811.45Slukem	else					# a normal daemon
2821.45Slukem		_fp_args='_arg0 _argv'
2831.45Slukem		_fp_match='case "$_arg0" in
2841.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
2851.45Slukem	fi
2861.45Slukem
2871.45Slukem	_proccheck='
2881.46Slukem		ps -o "pid,command" '"$_psargs"' |
2891.45Slukem		while read _npid '"$_fp_args"'; do
2901.45Slukem			case "$_npid" in
2911.45Slukem			    PID)
2921.45Slukem				continue ;;
2931.45Slukem			esac ; '"$_fp_match"'
2941.45Slukem				echo -n "$_pref$_npid" ;
2951.45Slukem				_pref=" "
2961.45Slukem				;;
2971.45Slukem			esac
2981.45Slukem		done'
2991.45Slukem
3001.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
3011.45Slukem	eval $_proccheck
3021.11Slukem}
3031.11Slukem
3041.11Slukem#
3051.33Slukem# wait_for_pids pid [pid ...]
3061.35Slukem#	spins until none of the pids exist
3071.33Slukem#
3081.33Slukemwait_for_pids()
3091.33Slukem{
3101.56Schristos	_list="$@"
3111.33Slukem	if [ -z "$_list" ]; then
3121.33Slukem		return
3131.33Slukem	fi
3141.35Slukem	_prefix=
3151.35Slukem	while true; do
3161.33Slukem		_nlist="";
3171.33Slukem		for _j in $_list; do
3181.33Slukem			if kill -0 $_j 2>/dev/null; then
3191.33Slukem				_nlist="${_nlist}${_nlist:+ }$_j"
3201.33Slukem			fi
3211.33Slukem		done
3221.33Slukem		if [ -z "$_nlist" ]; then
3231.33Slukem			break
3241.33Slukem		fi
3251.96Sroy		if [ "$_list" != "$_nlist" ]; then
3261.96Sroy			_list=$_nlist
3271.96Sroy			echo -n ${_prefix:-"Waiting for PIDS: "}$_list
3281.96Sroy			_prefix=", "
3291.96Sroy		fi
3301.96Sroy		# We want this to be a tight loop for a fast exit
3311.96Sroy		sleep 0.05
3321.33Slukem	done
3331.35Slukem	if [ -n "$_prefix" ]; then
3341.35Slukem		echo "."
3351.35Slukem	fi
3361.33Slukem}
3371.33Slukem
3381.33Slukem#
3391.81Sjmmv# run_rc_command argument [parameters]
3401.46Slukem#	Search for argument in the list of supported commands, which is:
3411.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
3421.46Slukem#	If there's a match, run ${argument}_cmd or the default method
3431.81Sjmmv#	(see below), and pass the optional list of parameters to it.
3441.11Slukem#
3451.46Slukem#	If argument has a given prefix, then change the operation as follows:
3461.46Slukem#		Prefix	Operation
3471.11Slukem#		------	---------
3481.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
3491.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
3501.61Slukem#		one	Set ${rcvar} to YES
3511.11Slukem#
3521.11Slukem#	The following globals are used:
3531.24Slukem#
3541.46Slukem#	Name		Needed	Purpose
3551.46Slukem#	----		------	-------
3561.11Slukem#	name		y	Name of script.
3571.24Slukem#
3581.11Slukem#	command		n	Full path to command.
3591.46Slukem#				Not needed if ${rc_arg}_cmd is set for
3601.11Slukem#				each keyword.
3611.24Slukem#
3621.11Slukem#	command_args	n	Optional args/shell directives for command.
3631.24Slukem#
3641.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
3651.45Slukem#				call check_{pidfile,process}() appropriately.
3661.45Slukem#
3671.16Slukem#	extra_commands	n	List of extra commands supported.
3681.24Slukem#
3691.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
3701.42Slukem#				otherwise use check_process $command.
3711.42Slukem#				In either case, only check if $command is set.
3721.42Slukem#
3731.42Slukem#	procname	n	Process name to check for instead of $command.
3741.24Slukem#
3751.24Slukem#	rcvar		n	This is checked with checkyesno to determine
3761.24Slukem#				if the action should be run.
3771.24Slukem#
3781.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
3791.42Slukem#				Requires /usr to be mounted.
3801.24Slukem#
3811.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
3821.22Slukem#				(if not using ${name}_chroot).
3831.24Slukem#
3841.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
3851.24Slukem#				NOTE:	$flags from the parent environment
3861.24Slukem#					can be used to override this.
3871.24Slukem#
3881.72She#	${name}_env	n	Additional environment variable settings
3891.72She#				for running ${command}
3901.72She#
3911.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
3921.24Slukem#
3931.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
3941.22Slukem#				using ${name}_chroot.
3951.42Slukem#				Requires /usr to be mounted.
3961.24Slukem#
3971.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
3981.42Slukem#				Requires /usr to be mounted.
3991.24Slukem#
4001.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
4011.32Slukem#				to run the chrooted ${command} with.
4021.42Slukem#				Requires /usr to be mounted.
4031.24Slukem#
4041.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
4051.11Slukem#				Otherwise, use default command (see below)
4061.24Slukem#
4071.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
4081.46Slukem#				${rc_arg}_cmd method in the default
4091.46Slukem#				operation (i.e, after checking for required
4101.46Slukem#				bits and process (non)existence).
4111.11Slukem#				If this completes with a non-zero exit code,
4121.46Slukem#				don't run ${rc_arg}_cmd.
4131.24Slukem#
4141.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
4151.46Slukem#				${rc_arg}_cmd method, if that method
4161.43Slukem#				returned a zero exit code.
4171.43Slukem#
4181.11Slukem#	required_dirs	n	If set, check for the existence of the given
4191.11Slukem#				directories before running the default
4201.11Slukem#				(re)start command.
4211.24Slukem#
4221.11Slukem#	required_files	n	If set, check for the readability of the given
4231.11Slukem#				files before running the default (re)start
4241.11Slukem#				command.
4251.24Slukem#
4261.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
4271.11Slukem#				listed variables before running the default
4281.11Slukem#				(re)start command.
4291.11Slukem#
4301.46Slukem#	Default behaviour for a given argument, if no override method is
4311.46Slukem#	provided:
4321.24Slukem#
4331.46Slukem#	Argument	Default behaviour
4341.46Slukem#	--------	-----------------
4351.11Slukem#	start		if !running && checkyesno ${rcvar}
4361.11Slukem#				${command}
4371.24Slukem#
4381.11Slukem#	stop		if ${pidfile}
4391.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
4401.11Slukem#			else
4411.46Slukem#				rc_pid=$(check_process $command)
4421.46Slukem#			kill $sig_stop $rc_pid
4431.46Slukem#			wait_for_pids $rc_pid
4441.35Slukem#			($sig_stop defaults to TERM.)
4451.24Slukem#
4461.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
4471.35Slukem#			and doesn't wait_for_pids.
4481.11Slukem#			$sig_reload defaults to HUP.
4491.24Slukem#
4501.11Slukem#	restart		Run `stop' then `start'.
4511.11Slukem#
4521.33Slukem#	status		Show if ${command} is running, etc.
4531.33Slukem#
4541.33Slukem#	poll		Wait for ${command} to exit.
4551.33Slukem#
4561.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
4571.33Slukem#
4581.46Slukem#	Variables available to methods, and after run_rc_command() has
4591.46Slukem#	completed:
4601.46Slukem#
4611.46Slukem#	Variable	Purpose
4621.46Slukem#	--------	-------
4631.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
4641.46Slukem#			performed
4651.46Slukem#
4661.46Slukem#	rc_flags	Flags to start the default command with.
4671.46Slukem#			Defaults to ${name}_flags, unless overridden
4681.46Slukem#			by $flags from the environment.
4691.46Slukem#			This variable may be changed by the precmd method.
4701.46Slukem#
4711.46Slukem#	rc_pid		PID of command (if appropriate)
4721.46Slukem#
4731.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
4741.46Slukem#
4751.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
4761.33Slukem#
4771.24Slukem#
4781.11Slukemrun_rc_command()
4791.11Slukem{
4801.46Slukem	rc_arg=$1
4811.24Slukem	if [ -z "$name" ]; then
4821.30Slukem		err 3 'run_rc_command: $name is not set.'
4831.11Slukem	fi
4841.11Slukem
4851.61Slukem	_rc_prefix=
4861.46Slukem	case "$rc_arg" in
4871.24Slukem	fast*)				# "fast" prefix; don't check pid
4881.46Slukem		rc_arg=${rc_arg#fast}
4891.48Slukem		rc_fast=yes
4901.11Slukem		;;
4911.61Slukem	force*)				# "force" prefix; always run
4921.48Slukem		rc_force=yes
4931.61Slukem		_rc_prefix=force
4941.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
4951.61Slukem		if [ -n "${rcvar}" ]; then
4961.61Slukem			eval ${rcvar}=YES
4971.61Slukem		fi
4981.61Slukem		;;
4991.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
5001.61Slukem		_rc_prefix=one
5011.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
5021.29Slukem		if [ -n "${rcvar}" ]; then
5031.29Slukem			eval ${rcvar}=YES
5041.29Slukem		fi
5051.11Slukem		;;
5061.11Slukem	esac
5071.11Slukem
5081.75Sreed	_keywords="start stop restart rcvar"
5091.75Sreed	if [ -n "$extra_commands" ]; then
5101.75Sreed		_keywords="${_keywords} ${extra_commands}"
5111.75Sreed	fi
5121.46Slukem	rc_pid=
5131.11Slukem	_pidcmd=
5141.42Slukem	_procname=${procname:-${command}}
5151.42Slukem
5161.24Slukem					# setup pid check command if not fast
5171.46Slukem	if [ -z "$rc_fast" -a -n "$_procname" ]; then
5181.11Slukem		if [ -n "$pidfile" ]; then
5191.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
5201.42Slukem		else
5211.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
5221.11Slukem		fi
5231.11Slukem		if [ -n "$_pidcmd" ]; then
5241.33Slukem			_keywords="${_keywords} status poll"
5251.11Slukem		fi
5261.11Slukem	fi
5271.11Slukem
5281.46Slukem	if [ -z "$rc_arg" ]; then
5291.11Slukem		rc_usage "$_keywords"
5301.11Slukem	fi
5311.81Sjmmv	shift	# remove $rc_arg from the positional parameters
5321.11Slukem
5331.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
5341.46Slukem		rc_flags=$flags
5351.11Slukem	else
5361.46Slukem		eval rc_flags=\$${name}_flags
5371.11Slukem	fi
5381.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
5391.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
5401.72She	    _group=\$${name}_group	_groups=\$${name}_groups \
5411.72She	    _env=\"\$${name}_env\"
5421.11Slukem
5431.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
5441.42Slukem		if [ "$_user" = "$(id -un)" ]; then
5451.42Slukem			unset _user
5461.42Slukem		fi
5471.42Slukem	fi
5481.42Slukem
5491.29Slukem					# if ${rcvar} is set, and $1 is not
5501.40Slukem					# "rcvar", then run
5511.29Slukem					#	checkyesno ${rcvar}
5521.74Ssalo					# and return if that failed or warn
5531.74Ssalo					# user and exit when interactive
5541.24Slukem					#
5551.46Slukem	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
5561.24Slukem		if ! checkyesno ${rcvar}; then
5571.74Ssalo					# check whether interactive or not
5581.74Ssalo			if [ -n "$_run_rc_script" ]; then
5591.74Ssalo				return 0
5601.74Ssalo			fi
5611.74Ssalo			for _elem in $_keywords; do
5621.74Ssalo				if [ "$_elem" = "$rc_arg" ]; then
5631.88Sapb					cat 1>&2 <<EOF
5641.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}.
5651.88SapbUse the following if you wish to perform the operation:
5661.88Sapb  $0 one${rc_arg}
5671.88SapbEOF
5681.74Ssalo					exit 1
5691.74Ssalo				fi
5701.74Ssalo			done
5711.74Ssalo			echo 1>&2 "$0: unknown directive '$rc_arg'."
5721.74Ssalo			rc_usage "$_keywords"
5731.24Slukem		fi
5741.24Slukem	fi
5751.24Slukem
5761.24Slukem	eval $_pidcmd			# determine the pid if necessary
5771.11Slukem
5781.11Slukem	for _elem in $_keywords; do
5791.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
5801.11Slukem			continue
5811.11Slukem		fi
5821.11Slukem
5831.24Slukem					# if there's a custom ${XXX_cmd},
5841.24Slukem					# run that instead of the default
5851.24Slukem					#
5861.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
5871.46Slukem		    _postcmd=\$${rc_arg}_postcmd
5881.11Slukem		if [ -n "$_cmd" ]; then
5891.24Slukem					# if the precmd failed and force
5901.24Slukem					# isn't set, exit
5911.24Slukem					#
5921.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
5931.24Slukem				return 1
5941.24Slukem			fi
5951.24Slukem
5961.81Sjmmv			if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then
5971.44Slukem				return 1
5981.44Slukem			fi
5991.44Slukem			eval $_postcmd
6001.11Slukem			return 0
6011.11Slukem		fi
6021.11Slukem
6031.81Sjmmv		if [ ${#} -gt 0 ]; then
6041.81Sjmmv			err 1 "the $rc_arg command does not take any parameters"
6051.81Sjmmv		fi
6061.81Sjmmv
6071.46Slukem		case "$rc_arg" in	# default operations...
6081.11Slukem
6091.11Slukem		status)
6101.46Slukem			if [ -n "$rc_pid" ]; then
6111.46Slukem				echo "${name} is running as pid $rc_pid."
6121.11Slukem			else
6131.11Slukem				echo "${name} is not running."
6141.28Slukem				return 1
6151.11Slukem			fi
6161.11Slukem			;;
6171.11Slukem
6181.11Slukem		start)
6191.46Slukem			if [ -n "$rc_pid" ]; then
6201.63Slukem				echo 1>&2 "${name} already running? (pid=$rc_pid)."
6211.11Slukem				exit 1
6221.11Slukem			fi
6231.11Slukem
6241.57Slukem			if [ ! -x ${_chroot}${command} ]; then
6251.11Slukem				return 0
6261.11Slukem			fi
6271.11Slukem
6281.24Slukem					# check for required variables,
6291.24Slukem					# directories, and files
6301.24Slukem					#
6311.11Slukem			for _f in $required_vars; do
6321.11Slukem				if ! checkyesno $_f; then
6331.65Slukem					warn "\$${_f} is not enabled."
6341.46Slukem					if [ -z "$rc_force" ]; then
6351.24Slukem						return 1
6361.24Slukem					fi
6371.11Slukem				fi
6381.11Slukem			done
6391.11Slukem			for _f in $required_dirs; do
6401.11Slukem				if [ ! -d "${_f}/." ]; then
6411.25Slukem					warn "${_f} is not a directory."
6421.46Slukem					if [ -z "$rc_force" ]; then
6431.24Slukem						return 1
6441.24Slukem					fi
6451.11Slukem				fi
6461.11Slukem			done
6471.11Slukem			for _f in $required_files; do
6481.11Slukem				if [ ! -r "${_f}" ]; then
6491.25Slukem					warn "${_f} is not readable."
6501.46Slukem					if [ -z "$rc_force" ]; then
6511.24Slukem						return 1
6521.24Slukem					fi
6531.11Slukem				fi
6541.11Slukem			done
6551.11Slukem
6561.24Slukem					# if the precmd failed and force
6571.24Slukem					# isn't set, exit
6581.24Slukem					#
6591.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6601.24Slukem				return 1
6611.24Slukem			fi
6621.24Slukem
6631.24Slukem					# setup the command to run, and run it
6641.29Slukem					#
6651.11Slukem			echo "Starting ${name}."
6661.22Slukem			if [ -n "$_chroot" ]; then
6671.22Slukem				_doit="\
6681.72She${_env:+env $_env }\
6691.23Slukem${_nice:+nice -n $_nice }\
6701.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
6711.46Slukem$_chroot $command $rc_flags $command_args"
6721.22Slukem			else
6731.22Slukem				_doit="\
6741.18Slukem${_chdir:+cd $_chdir; }\
6751.72She${_env:+env $_env }\
6761.18Slukem${_nice:+nice -n $_nice }\
6771.46Slukem$command $rc_flags $command_args"
6781.34Slukem				if [ -n "$_user" ]; then
6791.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
6801.34Slukem				fi
6811.22Slukem			fi
6821.43Slukem
6831.43Slukem					# if the cmd failed and force
6841.43Slukem					# isn't set, exit
6851.43Slukem					#
6861.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
6871.43Slukem				return 1
6881.43Slukem			fi
6891.43Slukem
6901.43Slukem					# finally, run postcmd
6911.43Slukem					#
6921.43Slukem			eval $_postcmd
6931.11Slukem			;;
6941.11Slukem
6951.11Slukem		stop)
6961.46Slukem			if [ -z "$rc_pid" ]; then
6971.24Slukem				if [ -n "$pidfile" ]; then
6981.63Slukem					echo 1>&2 \
6991.24Slukem				    "${name} not running? (check $pidfile)."
7001.24Slukem				else
7011.63Slukem					echo 1>&2 "${name} not running?"
7021.11Slukem				fi
7031.24Slukem				exit 1
7041.11Slukem			fi
7051.11Slukem
7061.43Slukem					# if the precmd failed and force
7071.43Slukem					# isn't set, exit
7081.43Slukem					#
7091.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7101.24Slukem				return 1
7111.24Slukem			fi
7121.43Slukem
7131.43Slukem					# send the signal to stop
7141.43Slukem					#
7151.11Slukem			echo "Stopping ${name}."
7161.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
7171.34Slukem			if [ -n "$_user" ]; then
7181.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7191.34Slukem			fi
7201.43Slukem
7211.43Slukem					# if the stop cmd failed and force
7221.43Slukem					# isn't set, exit
7231.43Slukem					#
7241.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7251.43Slukem				return 1
7261.43Slukem			fi
7271.43Slukem
7281.43Slukem					# wait for the command to exit,
7291.43Slukem					# and run postcmd.
7301.46Slukem			wait_for_pids $rc_pid
7311.43Slukem			eval $_postcmd
7321.11Slukem			;;
7331.11Slukem
7341.11Slukem		reload)
7351.46Slukem			if [ -z "$rc_pid" ]; then
7361.24Slukem				if [ -n "$pidfile" ]; then
7371.63Slukem					echo 1>&2 \
7381.11Slukem				    "${name} not running? (check $pidfile)."
7391.24Slukem				else
7401.63Slukem					echo 1>&2 "${name} not running?"
7411.11Slukem				fi
7421.24Slukem				exit 1
7431.11Slukem			fi
7441.11Slukem			echo "Reloading ${name} config files."
7451.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7461.24Slukem				return 1
7471.24Slukem			fi
7481.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
7491.34Slukem			if [ -n "$_user" ]; then
7501.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7511.34Slukem			fi
7521.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7531.43Slukem				return 1
7541.43Slukem			fi
7551.43Slukem			eval $_postcmd
7561.11Slukem			;;
7571.11Slukem
7581.11Slukem		restart)
7591.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7601.24Slukem				return 1
7611.11Slukem			fi
7621.29Slukem					# prevent restart being called more
7631.29Slukem					# than once by any given script
7641.29Slukem					#
7651.53Slukem			if ${_rc_restart_done:-false}; then
7661.29Slukem				return 0
7671.29Slukem			fi
7681.53Slukem			_rc_restart_done=true
7691.33Slukem
7701.61Slukem			( $0 ${_rc_prefix}stop )
7711.61Slukem			$0 ${_rc_prefix}start
7721.11Slukem
7731.43Slukem			eval $_postcmd
7741.33Slukem			;;
7751.33Slukem
7761.33Slukem		poll)
7771.46Slukem			if [ -n "$rc_pid" ]; then
7781.46Slukem				wait_for_pids $rc_pid
7791.33Slukem			fi
7801.11Slukem			;;
7811.11Slukem
7821.11Slukem		rcvar)
7831.11Slukem			echo "# $name"
7841.24Slukem			if [ -n "$rcvar" ]; then
7851.24Slukem				if checkyesno ${rcvar}; then
7861.24Slukem					echo "\$${rcvar}=YES"
7871.24Slukem				else
7881.24Slukem					echo "\$${rcvar}=NO"
7891.24Slukem				fi
7901.11Slukem			fi
7911.11Slukem			;;
7921.11Slukem
7931.11Slukem		*)
7941.11Slukem			rc_usage "$_keywords"
7951.11Slukem			;;
7961.11Slukem
7971.11Slukem		esac
7981.11Slukem		return 0
7991.11Slukem	done
8001.11Slukem
8011.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
8021.11Slukem	rc_usage "$_keywords"
8031.11Slukem	exit 1
8041.11Slukem}
8051.11Slukem
8061.11Slukem#
8071.94Sapb# _have_rc_postprocessor
8081.94Sapb#	Test whether the current script is running in a context that
8091.94Sapb#	was invoked from /etc/rc with a postprocessor.
8101.94Sapb#
8111.94Sapb#	If the test fails, some variables may be unset to make
8121.94Sapb#	such tests more efficient in future.
8131.94Sapb#
8141.94Sapb_have_rc_postprocessor()
8151.94Sapb{
8161.94Sapb	# Cheap tests that fd and pid are set, fd is writable.
8171.94Sapb	[ -n "${_rc_postprocessor_fd}" ] || return 1
8181.94Sapb	[ -n "${_rc_pid}" ] || return 1
8191.94Sapb	eval ": >&${_rc_postprocessor_fd}" 2>/dev/null || return 1
8201.94Sapb
8211.94Sapb	# More expensive test that pid is running.
8221.94Sapb	# Unset _rc_pid if this fails.
8231.94Sapb	kill -0 "${_rc_pid}" 2>/dev/null \
8241.94Sapb	|| { unset _rc_pid; return 1; }
8251.94Sapb
8261.94Sapb	# More expensive test that pid appears to be
8271.94Sapb	# a shell running an rc script.
8281.94Sapb	# Unset _rc_pid if this fails.
8291.94Sapb	expr "$(ps -p "${_rc_pid}" -o command=)" : ".*sh .*/rc.*" >/dev/null \
8301.94Sapb	|| { unset _rc_pid; return 1; }
8311.94Sapb
8321.94Sapb	return 0
8331.94Sapb}
8341.94Sapb
8351.94Sapb#
8361.11Slukem# run_rc_script file arg
8371.11Slukem#	Start the script `file' with `arg', and correctly handle the
8381.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
8391.37Slukem#	sourced into the current environment.  If `file' appears to be
8401.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
8411.37Slukem#	executable run as a child process.
8421.17Slukem#
8431.78Sapb#	If `file' contains "KEYWORD: interactive" and if we are
8441.94Sapb#	running inside /etc/rc with postprocessing, then the script's
8451.94Sapb#	stdout and stderr are redirected to $_rc_original_stdout_fd and
8461.78Sapb#	$_rc_original_stderr_fd, so the output will be displayed on the
8471.78Sapb#	console but not intercepted by /etc/rc's postprocessor.
8481.78Sapb#
8491.11Slukemrun_rc_script()
8501.11Slukem{
8511.11Slukem	_file=$1
8521.11Slukem	_arg=$2
8531.11Slukem	if [ -z "$_file" -o -z "$_arg" ]; then
8541.11Slukem		err 3 'USAGE: run_rc_script file arg'
8551.11Slukem	fi
8561.11Slukem
8571.74Ssalo	_run_rc_script=true
8581.74Ssalo
8591.45Slukem	unset	name command command_args command_interpreter \
8601.45Slukem		extra_commands pidfile procname \
8611.42Slukem		rcvar required_dirs required_files required_vars
8621.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
8631.39Slukem
8641.78Sapb	_must_redirect=false
8651.94Sapb	if _have_rc_postprocessor \
8661.78Sapb	    && _has_rcorder_keyword interactive $_file
8671.78Sapb	then
8681.78Sapb		_must_redirect=true
8691.78Sapb	fi
8701.78Sapb
8711.39Slukem	case "$_file" in
8721.39Slukem	*.sh)				# run in current shell
8731.78Sapb		if $_must_redirect; then
8741.78Sapb			print_rc_metadata \
8751.78Sapb			    "note:Output from ${_file} is not logged"
8761.80Sapb			no_rc_postprocess eval \
8771.80Sapb			    'set $_arg ; . $_file'
8781.78Sapb		else
8791.78Sapb			set $_arg ; . $_file
8801.78Sapb		fi
8811.39Slukem		;;
8821.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
8831.39Slukem		warn "Ignoring scratch file $_file"
8841.39Slukem		;;
8851.39Slukem	*)				# run in subshell
8861.78Sapb		if [ -x $_file ] && $_must_redirect; then
8871.78Sapb			print_rc_metadata \
8881.78Sapb			    "note:Output from ${_file} is not logged"
8891.78Sapb			if [ -n "$rc_fast_and_loose" ]; then
8901.80Sapb				no_rc_postprocess eval \
8911.80Sapb				    'set $_arg ; . $_file'
8921.78Sapb			else
8931.80Sapb				no_rc_postprocess eval \
8941.80Sapb				    '( set $_arg ; . $_file )'
8951.78Sapb			fi
8961.78Sapb		elif [ -x $_file ]; then
8971.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
8981.39Slukem				set $_arg ; . $_file
8991.39Slukem			else
9001.37Slukem				( set $_arg ; . $_file )
9011.37Slukem			fi
9021.78Sapb		else
9031.78Sapb			warn "Ignoring non-executable file $_file"
9041.39Slukem		fi
9051.39Slukem		;;
9061.39Slukem	esac
9071.11Slukem}
9081.19Slukem
9091.19Slukem#
9101.65Slukem# load_rc_config command
9111.19Slukem#	Source in the configuration file for a given command.
9121.19Slukem#
9131.19Slukemload_rc_config()
9141.19Slukem{
9151.19Slukem	_command=$1
9161.19Slukem	if [ -z "$_command" ]; then
9171.19Slukem		err 3 'USAGE: load_rc_config command'
9181.19Slukem	fi
9191.19Slukem
9201.54Slukem	if ${_rc_conf_loaded:-false}; then
9211.54Slukem		:
9221.54Slukem	else
9231.30Slukem		. /etc/rc.conf
9241.53Slukem		_rc_conf_loaded=true
9251.30Slukem	fi
9261.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
9271.20Sfvdl		. /etc/rc.conf.d/"$_command"
9281.20Sfvdl	fi
9291.19Slukem}
9301.19Slukem
9311.65Slukem#
9321.65Slukem# load_rc_config_var cmd var
9331.65Slukem#	Read the rc.conf(5) var for cmd and set in the
9341.65Slukem#	current shell, using load_rc_config in a subshell to prevent
9351.65Slukem#	unwanted side effects from other variable assignments.
9361.65Slukem#
9371.65Slukemload_rc_config_var()
9381.65Slukem{
9391.65Slukem	if [ $# -ne 2 ]; then
9401.65Slukem		err 3 'USAGE: load_rc_config_var cmd var'
9411.65Slukem	fi
9421.65Slukem	eval $(eval '(
9431.65Slukem		load_rc_config '$1' >/dev/null;
9441.77Sapb		if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
9451.65Slukem			echo '$2'=\'\''${'$2'}\'\'';
9461.65Slukem		fi
9471.65Slukem	)' )
9481.65Slukem}
9491.11Slukem
9501.11Slukem#
9511.11Slukem# rc_usage commands
9521.11Slukem#	Print a usage string for $0, with `commands' being a list of
9531.11Slukem#	valid commands.
9541.11Slukem#
9551.11Slukemrc_usage()
9561.11Slukem{
9571.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
9581.11Slukem
9591.11Slukem	_sep=
9601.56Schristos	for _elem; do
9611.11Slukem		echo -n 1>&2 "$_sep$_elem"
9621.11Slukem		_sep="|"
9631.11Slukem	done
9641.11Slukem	echo 1>&2 ")"
9651.11Slukem	exit 1
9661.11Slukem}
9671.11Slukem
9681.11Slukem#
9691.11Slukem# err exitval message
9701.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
9711.11Slukem#
9721.11Slukemerr()
9731.11Slukem{
9741.11Slukem	exitval=$1
9751.11Slukem	shift
9761.11Slukem
9771.51Sgrant	if [ -x /usr/bin/logger ]; then
9781.51Sgrant		logger "$0: ERROR: $*"
9791.51Sgrant	fi
9801.21Slukem	echo 1>&2 "$0: ERROR: $*"
9811.11Slukem	exit $exitval
9821.11Slukem}
9831.11Slukem
9841.11Slukem#
9851.11Slukem# warn message
9861.11Slukem#	Display message to stderr and log to the syslog.
9871.11Slukem#
9881.11Slukemwarn()
9891.11Slukem{
9901.51Sgrant	if [ -x /usr/bin/logger ]; then
9911.51Sgrant		logger "$0: WARNING: $*"
9921.51Sgrant	fi
9931.21Slukem	echo 1>&2 "$0: WARNING: $*"
9941.31Satatat}
9951.31Satatat
9961.31Satatat#
9971.31Satatat# backup_file action file cur backup
9981.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
9991.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
10001.31Satatat#
10011.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
10021.31Satatat#	which can be either YES or NO.
10031.31Satatat#
10041.31Satatat#	The `action' keyword can be one of the following:
10051.31Satatat#
10061.31Satatat#	add		`file' is now being backed up (and is possibly
10071.31Satatat#			being reentered into the backups system).  `cur'
10081.31Satatat#			is created and RCS files, if necessary, are
10091.31Satatat#			created as well.
10101.31Satatat#
10111.31Satatat#	update		`file' has changed and needs to be backed up.
10121.31Satatat#			If `cur' exists, it is copied to to `back' or
10131.31Satatat#			checked into RCS (if the repository file is old),
10141.31Satatat#			and then `file' is copied to `cur'.  Another RCS
10151.31Satatat#			check in done here if RCS is being used.
10161.31Satatat#
10171.31Satatat#	remove		`file' is no longer being tracked by the backups
10181.31Satatat#			system.  If RCS is not being used, `cur' is moved
10191.31Satatat#			to `back', otherwise an empty file is checked in,
10201.31Satatat#			and then `cur' is removed.
10211.31Satatat#
10221.31Satatat#
10231.31Satatatbackup_file()
10241.31Satatat{
10251.31Satatat	_action=$1
10261.31Satatat	_file=$2
10271.31Satatat	_cur=$3
10281.31Satatat	_back=$4
10291.31Satatat
10301.31Satatat	if checkyesno backup_uses_rcs; then
10311.31Satatat		_msg0="backup archive"
10321.31Satatat		_msg1="update"
10331.31Satatat
10341.36Satatat		# ensure that history file is not locked
10351.36Satatat		if [ -f $_cur,v ]; then
10361.36Satatat			rcs -q -u -U -M $_cur
10371.36Satatat		fi
10381.36Satatat
10391.31Satatat		# ensure after switching to rcs that the
10401.31Satatat		# current backup is not lost
10411.31Satatat		if [ -f $_cur ]; then
10421.31Satatat			# no archive, or current newer than archive
10431.31Satatat			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
10441.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10451.36Satatat				rcs -q -kb -U $_cur
10461.49Slukem				co -q -f -u $_cur
10471.31Satatat			fi
10481.31Satatat		fi
10491.31Satatat
10501.31Satatat		case $_action in
10511.31Satatat		add|update)
10521.31Satatat			cp -p $_file $_cur
10531.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10541.36Satatat			rcs -q -kb -U $_cur
10551.49Slukem			co -q -f -u $_cur
10561.31Satatat			chown root:wheel $_cur $_cur,v
10571.31Satatat			;;
10581.31Satatat		remove)
10591.31Satatat			cp /dev/null $_cur
10601.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10611.36Satatat			rcs -q -kb -U $_cur
10621.31Satatat			chown root:wheel $_cur $_cur,v
10631.31Satatat			rm $_cur
10641.31Satatat			;;
10651.31Satatat		esac
10661.31Satatat	else
10671.31Satatat		case $_action in
10681.31Satatat		add|update)
10691.31Satatat			if [ -f $_cur ]; then
10701.31Satatat				cp -p $_cur $_back
10711.31Satatat			fi
10721.31Satatat			cp -p $_file $_cur
10731.31Satatat			chown root:wheel $_cur
10741.31Satatat			;;
10751.31Satatat		remove)
10761.31Satatat			mv -f $_cur $_back
10771.31Satatat			;;
10781.31Satatat		esac
10791.31Satatat	fi
10801.1Scjs}
10811.64Smycroft
10821.76Schristos#
10831.76Schristos# handle_fsck_error fsck_exit_code
10841.76Schristos#	Take action depending on the return code from fsck.
10851.76Schristos#
10861.76Schristoshandle_fsck_error()
10871.76Schristos{
10881.76Schristos	case $1 in
10891.76Schristos	0)	# OK
10901.76Schristos		return
10911.76Schristos		;;
10921.76Schristos	2)	# Needs re-run, still fs errors
10931.76Schristos		echo "File system still has errors; re-run fsck manually!"
10941.76Schristos		;;
10951.76Schristos	4)	# Root modified
10961.93Swiz		echo "Root file system was modified, rebooting ..."
10971.76Schristos		reboot -n
10981.76Schristos		echo "Reboot failed; help!"
10991.76Schristos		;;
11001.76Schristos	8)	# Check failed
11011.76Schristos		echo "Automatic file system check failed; help!"
11021.76Schristos		;;
11031.76Schristos	12)	# Got signal
11041.76Schristos		echo "Boot interrupted."
11051.76Schristos		;;
11061.76Schristos	*)
11071.76Schristos		echo "Unknown error $1; help!"
11081.76Schristos		;;
11091.76Schristos	esac
11101.76Schristos	stop_boot
11111.76Schristos}
11121.76Schristos
11131.78Sapb#
11141.78Sapb# _has_rcorder_keyword word file
11151.78Sapb#	Check whether a file contains a "# KEYWORD:" comment with a
11161.78Sapb#	specified keyword in the style used by rcorder(8).
11171.78Sapb#
11181.78Sapb_has_rcorder_keyword()
11191.78Sapb{
11201.78Sapb	local word="$1"
11211.78Sapb	local file="$2"
11221.78Sapb	local line
11231.78Sapb
11241.78Sapb	[ -r "$file" ] || return 1
11251.78Sapb	while read line; do
11261.78Sapb		case "${line} " in
11271.78Sapb		"# KEYWORD:"*[\ \	]"${word}"[\ \	]*)
11281.78Sapb			return 0
11291.78Sapb			;;
11301.78Sapb		"#"*)
11311.78Sapb			continue
11321.78Sapb			;;
11331.78Sapb		*[A-Za-z0-9]*)
11341.78Sapb			# give up at the first non-empty non-comment line
11351.78Sapb			return 1
11361.78Sapb			;;
11371.78Sapb		esac
11381.78Sapb	done <"$file"
11391.78Sapb	return 1
11401.78Sapb}
11411.78Sapb
11421.78Sapb#
11431.78Sapb# print_rc_metadata string
11441.78Sapb#	Print the specified string in such a way that the post-processor
11451.78Sapb#	inside /etc/rc will treat it as meta-data.
11461.78Sapb#
11471.78Sapb#	If we are not running inside /etc/rc, do nothing.
11481.78Sapb#
11491.78Sapb#	For public use by any rc.d script, the string must begin with
11501.78Sapb#	"note:", followed by arbitrary text.  The intent is that the text
11511.78Sapb#	will appear in a log file but not on the console.
11521.78Sapb#
11531.78Sapb#	For private use within /etc/rc, the string must contain a
11541.78Sapb#	keyword recognised by the rc_postprocess_metadata() function
11551.78Sapb#	defined in /etc/rc, followed by a colon, followed by one or more
11561.78Sapb#	colon-separated arguments associated with the keyword.
11571.78Sapb#
11581.78Sapbprint_rc_metadata()
11591.78Sapb{
11601.78Sapb	# _rc_postprocessor fd, if defined, is the fd to which we must
11611.78Sapb	# print, prefixing the output with $_rc_metadata_prefix.
11621.78Sapb	#
11631.94Sapb	if _have_rc_postprocessor; then
11641.88Sapb		command printf "%s%s\n" "$rc_metadata_prefix" "$1" \
11651.78Sapb			>&${_rc_postprocessor_fd}
11661.78Sapb	fi
11671.78Sapb}
11681.78Sapb
11691.78Sapb#
11701.88Sapb# _flush_rc_output
11711.88Sapb#	Arrange for output to be flushed, if we are running
11721.88Sapb#	inside /etc/rc with postprocessing.
11731.88Sapb#
11741.88Sapb_flush_rc_output()
11751.88Sapb{
11761.88Sapb	print_rc_metadata "nop"
11771.88Sapb}
11781.88Sapb
11791.88Sapb#
11801.88Sapb# print_rc_normal [-n] string
11811.78Sapb#	Print the specified string in such way that it is treated as
11821.78Sapb#	normal output, regardless of whether or not we are running
11831.78Sapb#	inside /etc/rc with post-processing.
11841.78Sapb#
11851.88Sapb#	If "-n" is specified in $1, then the string in $2 is printed
11861.88Sapb#	without a newline; otherwise, the string in $1 is printed
11871.88Sapb#	with a newline.
11881.88Sapb#
11891.88Sapb#	Intended use cases include:
11901.88Sapb#
11911.88Sapb#	o   An rc.d script can use ``print_rc_normal -n'' to print a
11921.88Sapb#	    partial line in such a way that it appears immediately
11931.88Sapb#	    instead of being buffered by rc(8)'s post-processor.
11941.88Sapb#
11951.88Sapb#	o   An rc.d script that is run via the no_rc_postprocess
11961.88Sapb#	    function (so most of its output is invisible to rc(8)'s
11971.88Sapb#	    post-processor) can use print_rc_normal to force some of its
11981.88Sapb#	    output to be seen by the post-processor.
11991.88Sapb#
12001.78Sapb#
12011.78Sapbprint_rc_normal()
12021.78Sapb{
12031.94Sapb	# print to stdout or _rc_postprocessor_fd, depending on
12041.94Sapb	# whether not we have an rc postprocessor.
12051.78Sapb	#
12061.94Sapb	local fd=1
12071.94Sapb	_have_rc_postprocessor && fd="${_rc_postprocessor_fd}"
12081.88Sapb	case "$1" in
12091.88Sapb	"-n")
12101.88Sapb		command printf "%s" "$2" >&${fd}
12111.88Sapb		_flush_rc_output
12121.88Sapb		;;
12131.88Sapb	*)
12141.88Sapb		command printf "%s\n" "$1" >&${fd}
12151.88Sapb		;;
12161.88Sapb	esac
12171.78Sapb}
12181.78Sapb
12191.78Sapb#
12201.78Sapb# no_rc_postprocess cmd...
12211.78Sapb#	Execute the specified command in such a way that its output
12221.78Sapb#	bypasses the post-processor that handles the output from
12231.78Sapb#	most commands that are run inside /etc/rc.  If we are not
12241.78Sapb#	inside /etc/rc, then just execute the command without special
12251.78Sapb#	treatment.
12261.78Sapb#
12271.78Sapb#	The intent is that interactive commands can be run via
12281.78Sapb#	no_rc_postprocess(), and their output will apear immediately
12291.78Sapb#	on the console instead of being hidden or delayed by the
12301.78Sapb#	post-processor.	 An unfortunate consequence of the output
12311.78Sapb#	bypassing the post-processor is that the output will not be
12321.78Sapb#	logged.
12331.78Sapb#
12341.78Sapbno_rc_postprocess()
12351.78Sapb{
12361.94Sapb	if _have_rc_postprocessor; then
12371.78Sapb		"$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
12381.78Sapb	else
12391.78Sapb		"$@"
12401.78Sapb	fi
12411.78Sapb}
12421.78Sapb
12431.78Sapb#
12441.78Sapb# twiddle
12451.78Sapb#	On each call, print a different one of "/", "-", "\\", "|",
12461.78Sapb#	followed by a backspace.  The most recently printed value is
12471.78Sapb#	saved in $_twiddle_state.
12481.78Sapb#
12491.78Sapb#	Output is to /dev/tty, so this function may be useful even inside
12501.78Sapb#	a script whose output is redirected.
12511.78Sapb#
12521.78Sapbtwiddle()
12531.78Sapb{
12541.78Sapb	case "$_twiddle_state" in
12551.78Sapb	'/')	_next='-' ;;
12561.78Sapb	'-')	_next='\' ;;
12571.78Sapb	'\')	_next='|' ;;
12581.78Sapb	*)	_next='/' ;;
12591.78Sapb	esac
12601.88Sapb	command printf "%s\b" "$_next" >/dev/tty
12611.78Sapb	_twiddle_state="$_next"
12621.78Sapb}
12631.78Sapb
12641.82Schristos#
12651.82Schristos# human_exit_code
12661.82Schristos#	Print the a human version of the exit code.
12671.82Schristos#
12681.82Schristoshuman_exit_code()
12691.82Schristos{
12701.83Schristos	if [ "$1" -lt 127 ]
12711.82Schristos	then
12721.82Schristos		echo "exited with code $1"
12731.85Schristos	elif [ "$(expr $1 % 256)" -eq 127 ]
12741.82Schristos	then
12751.84Schristos		# This cannot really happen because the shell will not
12761.84Schristos		# pass stopped job status out and the exit code is limited
12771.84Schristos		# to 8 bits. This code is here just for completeness.
12781.82Schristos		echo "stopped with signal $(expr $1 / 256)"
12791.82Schristos	else
12801.82Schristos		echo "terminated with signal $(expr $1 - 128)"
12811.82Schristos	fi
12821.82Schristos}
12831.86Sapb
12841.86Sapb#
12851.86Sapb# collapse_backslash_newline
12861.86Sapb#	Copy input to output, collapsing <backslash><newline>
12871.86Sapb#	to nothing, but leaving other backslashes alone.
12881.86Sapb#
12891.86Sapbcollapse_backslash_newline()
12901.86Sapb{
12911.86Sapb	local line
12921.86Sapb	while read -r line ; do
12931.86Sapb		case "$line" in
12941.86Sapb		*\\)
12951.86Sapb			# print it, without the backslash or newline
12961.88Sapb			command printf "%s" "${line%?}"
12971.86Sapb			;;
12981.86Sapb		*)
12991.86Sapb			# print it, with a newline
13001.88Sapb			command printf "%s\n" "${line}"
13011.86Sapb			;;
13021.86Sapb		esac
13031.86Sapb	done
13041.86Sapb}
13051.82Schristos
13061.92Sapb# Shell implementations of basename and dirname, usable before
13071.92Sapb# the /usr file system is mounted.
13081.92Sapb#
13091.92Sapbbasename()
13101.92Sapb{
13111.92Sapb	local file="$1"
13121.92Sapb	local suffix="$2"
13131.92Sapb	local base
13141.92Sapb
13151.92Sapb	base="${file##*/}"		# remove up to and including last '/'
13161.92Sapb	base="${base%${suffix}}"	# remove suffix, if any
13171.92Sapb	command printf "%s\n" "${base}"
13181.92Sapb}
13191.92Sapb
13201.92Sapbdirname()
13211.92Sapb{
13221.92Sapb	local file="$1"
13231.92Sapb	local dir
13241.92Sapb
13251.92Sapb	case "$file" in
13261.92Sapb	/*/*)	dir="${file%/*}" ;;	# common case: absolute path
13271.92Sapb	/*)	dir="/" ;;		# special case: name in root dir
13281.92Sapb	*/*)	dir="${file%/*}" ;;	# common case: relative path with '/'
13291.92Sapb	*)	dir="." ;;		# special case: name without '/'
13301.92Sapb	esac
13311.92Sapb	command printf "%s\n" "${dir}"
13321.92Sapb}
13331.92Sapb
13341.88Sapb# Override the normal "echo" and "printf" commands, so that
13351.88Sapb# partial lines printed by rc.d scripts appear immediately,
13361.88Sapb# instead of being buffered by rc(8)'s post-processor.
13371.88Sapb#
13381.88Sapb# Naive use of the echo or printf commands from rc.d scripts,
13391.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr,
13401.88Sapb# will call these functions.  To call the real echo and printf
13411.88Sapb# commands, use "command echo" or "command printf".
13421.88Sapb#
13431.88Sapbecho()
13441.88Sapb{
13451.88Sapb	command echo "$@"
13461.88Sapb	case "$1" in
13471.88Sapb	'-n')	_flush_rc_output ;;
13481.88Sapb	esac
13491.88Sapb}
13501.88Sapbprintf()
13511.88Sapb{
13521.88Sapb	command printf "$@"
13531.88Sapb	case "$1" in
13541.88Sapb	*'\n')	: ;;
13551.88Sapb	*)	_flush_rc_output ;;
13561.88Sapb	esac
13571.88Sapb}
13581.88Sapb
13591.64Smycroft_rc_subr_loaded=:
1360