rc.subr revision 1.105
11.105Sotis# $NetBSD: rc.subr,v 1.105 2020/09/17 20:29:03 otis 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.104Schristos_rc_kill_ntries=
471.99Schristos"
481.99Schristos
491.11Slukem#
501.11Slukem#	functions
511.11Slukem#	---------
521.1Scjs
531.5Slukem#
541.11Slukem# checkyesno var
551.95Sroy#	Test $1 variable.
561.95Sroy#	Return 0 if it's "yes" (et al), 1 if it's "no" (et al), 2 otherwise.
571.5Slukem#
581.95Sroycheckyesnox()
591.11Slukem{
601.11Slukem	eval _value=\$${1}
611.11Slukem	case $_value in
621.4Slukem
631.4Slukem		#	"yes", "true", "on", or "1"
641.4Slukem	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
651.4Slukem		return 0
661.3Slukem		;;
671.4Slukem
681.4Slukem		#	"no", "false", "off", or "0"
691.4Slukem	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
701.4Slukem		return 1
711.3Slukem		;;
721.3Slukem	*)
731.95Sroy		return 2
741.3Slukem		;;
751.3Slukem	esac
761.38Slukem}
771.38Slukem
781.65Slukem#
791.95Sroy# checkyesno var
801.95Sroy#	Test $1 variable, and warn if not set to YES or NO.
811.95Sroy#	Return 0 if it's "yes" (et al), nonzero otherwise.
821.95Sroy#
831.95Sroycheckyesno()
841.95Sroy{
851.95Sroy	local var
861.95Sroy
871.95Sroy	checkyesnox $1
881.95Sroy	var=$?
891.103Skre	case "${var}" in
901.103Skre	( 0 | 1 )	return $var;;
911.103Skre	esac
921.95Sroy	warn "\$${1} is not set properly - see ${rcvar_manpage}."
931.95Sroy	return 1
941.95Sroy}
951.95Sroy
961.95Sroy#
971.78Sapb# yesno_to_truefalse var
981.78Sapb#	Convert the value of a variable from any of the values
991.78Sapb#	understood by checkyesno() to "true" or "false".
1001.78Sapb#
1011.78Sapbyesno_to_truefalse()
1021.78Sapb{
1031.78Sapb	local var=$1
1041.78Sapb	if checkyesno $var; then
1051.78Sapb		eval $var=true
1061.78Sapb		return 0
1071.78Sapb	else
1081.78Sapb		eval $var=false
1091.78Sapb		return 1
1101.78Sapb	fi
1111.78Sapb}
1121.78Sapb
1131.78Sapb#
1141.38Slukem# reverse_list list
1151.38Slukem#	print the list in reverse order
1161.38Slukem#
1171.38Slukemreverse_list()
1181.38Slukem{
1191.38Slukem	_revlist=
1201.56Schristos	for _revfile; do
1211.38Slukem		_revlist="$_revfile $_revlist"
1221.38Slukem	done
1231.38Slukem	echo $_revlist
1241.6Smellon}
1251.6Smellon
1261.6Smellon#
1271.69Sapb# If booting directly to multiuser, send SIGTERM to
1281.69Sapb# the parent (/etc/rc) to abort the boot.
1291.69Sapb# Otherwise just exit.
1301.69Sapb#
1311.69Sapbstop_boot()
1321.69Sapb{
1331.69Sapb	if [ "$autoboot" = yes ]; then
1341.69Sapb		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
1351.69Sapb		kill -TERM ${RC_PID}
1361.69Sapb	fi
1371.69Sapb	exit 1
1381.69Sapb}
1391.69Sapb
1401.69Sapb#
1411.47Slukem# mount_critical_filesystems type
1421.93Swiz#	Go through the list of critical file systems as provided in
1431.47Slukem#	the rc.conf(5) variable $critical_filesystems_${type}, checking
1441.47Slukem#	each one to see if it is mounted, and if it is not, mounting it.
1451.79Sapb#	It's not an error if file systems prefixed with "OPTIONAL:"
1461.79Sapb#	are not mentioned in /etc/fstab.
1471.6Smellon#
1481.11Slukemmount_critical_filesystems()
1491.11Slukem{
1501.47Slukem	eval _fslist=\$critical_filesystems_${1}
1511.79Sapb	_mountcrit_es=0
1521.17Slukem	for _fs in $_fslist; do
1531.79Sapb		_optional=false
1541.79Sapb		case "$_fs" in
1551.79Sapb		OPTIONAL:*)
1561.79Sapb			_optional=true
1571.79Sapb			_fs="${_fs#*:}"
1581.79Sapb			;;
1591.79Sapb		esac
1601.79Sapb		_ismounted=false
1611.79Sapb		# look for a line like "${fs} on * type *"
1621.79Sapb		# or "* on ${fs} type *" in the output from mount.
1631.79Sapb		case "${nl}$( mount )${nl}" in
1641.79Sapb		*" on ${_fs} type "*)
1651.79Sapb			_ismounted=true
1661.79Sapb			;;
1671.79Sapb		*"${nl}${_fs} on "*)
1681.79Sapb			_ismounted=true
1691.79Sapb			;;
1701.79Sapb		esac
1711.79Sapb		if $_ismounted; then
1721.79Sapb			print_rc_metadata \
1731.79Sapb			"note:File system ${_fs} was already mounted"
1741.79Sapb		else
1751.79Sapb			_mount_output=$( mount $_fs 2>&1 )
1761.79Sapb			_mount_es=$?
1771.79Sapb			case "$_mount_output" in
1781.79Sapb			*"${nl}"*)
1791.79Sapb				# multiple lines can't be good,
1801.79Sapb				# not even if $_optional is true
1811.79Sapb				;;
1821.89Sapb			*[uU]'nknown special file or file system'*)
1831.79Sapb				if $_optional; then
1841.79Sapb					# ignore this error
1851.79Sapb					print_rc_metadata \
1861.79Sapb			"note:Optional file system ${_fs} is not present"
1871.79Sapb					_mount_es=0
1881.79Sapb					_mount_output=""
1891.6Smellon				fi
1901.79Sapb				;;
1911.79Sapb			esac
1921.79Sapb			if [ -n "$_mount_output" ]; then
1931.79Sapb				printf >&2 "%s\n" "$_mount_output"
1941.79Sapb			fi
1951.79Sapb			if [ "$_mount_es" != 0 ]; then
1961.79Sapb				_mountcrit_es="$_mount_es"
1971.6Smellon			fi
1981.79Sapb		fi
1991.6Smellon	done
2001.79Sapb	return $_mountcrit_es
2011.7Scjs}
2021.7Scjs
2031.11Slukem#
2041.45Slukem# check_pidfile pidfile procname [interpreter]
2051.45Slukem#	Parses the first line of pidfile for a PID, and ensures
2061.11Slukem#	that the process is running and matches procname.
2071.45Slukem#	Prints the matching PID upon success, nothing otherwise.
2081.45Slukem#	interpreter is optional; see _find_processes() for details.
2091.11Slukem#
2101.11Slukemcheck_pidfile()
2111.11Slukem{
2121.11Slukem	_pidfile=$1
2131.11Slukem	_procname=$2
2141.45Slukem	_interpreter=$3
2151.103Skre	if [ -z "$_pidfile" ] || [ -z "$_procname" ]; then
2161.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
2171.11Slukem	fi
2181.11Slukem	if [ ! -f $_pidfile ]; then
2191.11Slukem		return
2201.11Slukem	fi
2211.11Slukem	read _pid _junk < $_pidfile
2221.11Slukem	if [ -z "$_pid" ]; then
2231.11Slukem		return
2241.11Slukem	fi
2251.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
2261.11Slukem}
2271.11Slukem
2281.11Slukem#
2291.45Slukem# check_process procname [interpreter]
2301.11Slukem#	Ensures that a process (or processes) named procname is running.
2311.45Slukem#	Prints a list of matching PIDs.
2321.45Slukem#	interpreter is optional; see _find_processes() for details.
2331.11Slukem#
2341.11Slukemcheck_process()
2351.11Slukem{
2361.11Slukem	_procname=$1
2371.45Slukem	_interpreter=$2
2381.11Slukem	if [ -z "$_procname" ]; then
2391.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
2401.45Slukem	fi
2411.101Skre	_find_processes $_procname ${_interpreter:-.} '-A'
2421.45Slukem}
2431.45Slukem
2441.46Slukem#
2451.45Slukem# _find_processes procname interpreter psargs
2461.45Slukem#	Search for procname in the output of ps generated by psargs.
2471.45Slukem#	Prints the PIDs of any matching processes, space separated.
2481.45Slukem#
2491.45Slukem#	If interpreter == ".", check the following variations of procname
2501.45Slukem#	against the first word of each command:
2511.45Slukem#		procname
2521.45Slukem#		`basename procname`
2531.45Slukem#		`basename procname` + ":"
2541.45Slukem#		"(" + `basename procname` + ")"
2551.45Slukem#
2561.45Slukem#	If interpreter != ".", read the first line of procname, remove the
2571.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
2581.45Slukem#	match that against each command, either as is, or with extra words
2591.73Sdholland#	at the end.  As an alternative, to deal with interpreted daemons
2601.66She#	using perl, the basename of the interpreter plus a colon is also
2611.66She#	tried as the prefix to procname.
2621.45Slukem#
2631.45Slukem_find_processes()
2641.45Slukem{
2651.45Slukem	if [ $# -ne 3 ]; then
2661.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
2671.11Slukem	fi
2681.45Slukem	_procname=$1
2691.45Slukem	_interpreter=$2
2701.45Slukem	_psargs=$3
2711.45Slukem
2721.11Slukem	_pref=
2731.68Shubertf	_procnamebn=${_procname##*/}
2741.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
2751.67Selad		read _interp < ${_chroot:-}/$_procname	# read interpreter name
2761.45Slukem		_interp=${_interp#\#!}		# strip #!
2771.45Slukem		set -- $_interp
2781.87Schristos		if [ $1 = "/usr/bin/env" ]; then
2791.87Schristos			shift
2801.87Schristos			set -- $(type $1)
2811.87Schristos			shift $(($# - 1))
2821.87Schristos			_interp="${1##*/} $_procname"
2831.87Schristos		else
2841.87Schristos			_interp="$* $_procname"
2851.87Schristos		fi
2861.45Slukem		if [ $_interpreter != $1 ]; then
2871.45Slukem			warn "\$command_interpreter $_interpreter != $1"
2881.45Slukem		fi
2891.66She		_interpbn=${1##*/}
2901.45Slukem		_fp_args='_argv'
2911.45Slukem		_fp_match='case "$_argv" in
2921.68Shubertf		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
2931.45Slukem	else					# a normal daemon
2941.45Slukem		_fp_args='_arg0 _argv'
2951.45Slukem		_fp_match='case "$_arg0" in
2961.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
2971.45Slukem	fi
2981.45Slukem
2991.45Slukem	_proccheck='
3001.102Schristos		ps -o "pid,args" '"$_psargs"' 2>&1 |
3011.45Slukem		while read _npid '"$_fp_args"'; do
3021.45Slukem			case "$_npid" in
3031.102Schristos			ps:|PID)
3041.45Slukem				continue ;;
3051.45Slukem			esac ; '"$_fp_match"'
3061.45Slukem				echo -n "$_pref$_npid" ;
3071.45Slukem				_pref=" "
3081.45Slukem				;;
3091.45Slukem			esac
3101.45Slukem		done'
3111.45Slukem
3121.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
3131.45Slukem	eval $_proccheck
3141.11Slukem}
3151.11Slukem
3161.11Slukem#
3171.104Schristos# kill_pids signal pid [pid ...]
3181.104Schristos#	kills the given pids with signal. 
3191.104Schristos#	returns the list of pids killed successfully.
3201.104Schristos#
3211.104Schristoskill_pids()
3221.104Schristos{
3231.104Schristos	local signal=$1
3241.104Schristos	shift
3251.105Sotis	local list="$*"
3261.104Schristos	local j=
3271.104Schristos	local nlist=
3281.104Schristos	for j in $list; do
3291.104Schristos		if kill -$signal $j 2>/dev/null; then
3301.104Schristos			nlist="${nlist}${nlist:+ }$j"
3311.104Schristos		fi
3321.104Schristos	done
3331.104Schristos	echo $nlist
3341.104Schristos}
3351.104Schristos
3361.104Schristos#
3371.33Slukem# wait_for_pids pid [pid ...]
3381.35Slukem#	spins until none of the pids exist
3391.104Schristos#	if _rc_kill_ntries is set and exceeded, it SIGKILLS the remaining
3401.104Schristos#	pids
3411.33Slukem#
3421.33Slukemwait_for_pids()
3431.33Slukem{
3441.104Schristos	local ntries=0
3451.104Schristos	local prefix=
3461.105Sotis	local nlist=
3471.105Sotis	local list="$*"
3481.104Schristos
3491.104Schristos	if [ -z "$list" ]; then
3501.33Slukem		return
3511.33Slukem	fi
3521.104Schristos
3531.35Slukem	while true; do
3541.105Sotis		nlist=$(kill_pids 0 $list)
3551.104Schristos		if [ -z "$nlist" ]; then
3561.33Slukem			break
3571.33Slukem		fi
3581.104Schristos		if [ "$list" != "$nlist" ]; then
3591.104Schristos			list=$nlist
3601.104Schristos			echo -n ${prefix:-"Waiting for PIDS: "}$list
3611.104Schristos			prefix=", "
3621.96Sroy		fi
3631.96Sroy		# We want this to be a tight loop for a fast exit
3641.96Sroy		sleep 0.05
3651.104Schristos		ntries=$((ntries + 1))
3661.104Schristos		if [ -n "${_rc_kill_ntries}" ]; then
3671.104Schristos			if [ ${ntries} -gt ${_rc_kill_ntries} ]; then
3681.104Schristos				kill_pids 9 $list > /dev/null
3691.104Schristos			fi
3701.104Schristos		fi
3711.33Slukem	done
3721.104Schristos	if [ -n "$prefix" ]; then
3731.35Slukem		echo "."
3741.35Slukem	fi
3751.33Slukem}
3761.33Slukem
3771.33Slukem#
3781.81Sjmmv# run_rc_command argument [parameters]
3791.46Slukem#	Search for argument in the list of supported commands, which is:
3801.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
3811.46Slukem#	If there's a match, run ${argument}_cmd or the default method
3821.81Sjmmv#	(see below), and pass the optional list of parameters to it.
3831.11Slukem#
3841.46Slukem#	If argument has a given prefix, then change the operation as follows:
3851.46Slukem#		Prefix	Operation
3861.11Slukem#		------	---------
3871.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
3881.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
3891.61Slukem#		one	Set ${rcvar} to YES
3901.11Slukem#
3911.11Slukem#	The following globals are used:
3921.24Slukem#
3931.46Slukem#	Name		Needed	Purpose
3941.46Slukem#	----		------	-------
3951.11Slukem#	name		y	Name of script.
3961.24Slukem#
3971.11Slukem#	command		n	Full path to command.
3981.46Slukem#				Not needed if ${rc_arg}_cmd is set for
3991.11Slukem#				each keyword.
4001.24Slukem#
4011.11Slukem#	command_args	n	Optional args/shell directives for command.
4021.24Slukem#
4031.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
4041.45Slukem#				call check_{pidfile,process}() appropriately.
4051.45Slukem#
4061.16Slukem#	extra_commands	n	List of extra commands supported.
4071.24Slukem#
4081.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
4091.42Slukem#				otherwise use check_process $command.
4101.42Slukem#				In either case, only check if $command is set.
4111.42Slukem#
4121.42Slukem#	procname	n	Process name to check for instead of $command.
4131.24Slukem#
4141.24Slukem#	rcvar		n	This is checked with checkyesno to determine
4151.24Slukem#				if the action should be run.
4161.24Slukem#
4171.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
4181.42Slukem#				Requires /usr to be mounted.
4191.24Slukem#
4201.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
4211.22Slukem#				(if not using ${name}_chroot).
4221.24Slukem#
4231.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
4241.24Slukem#				NOTE:	$flags from the parent environment
4251.24Slukem#					can be used to override this.
4261.24Slukem#
4271.72She#	${name}_env	n	Additional environment variable settings
4281.72She#				for running ${command}
4291.72She#
4301.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
4311.24Slukem#
4321.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
4331.22Slukem#				using ${name}_chroot.
4341.42Slukem#				Requires /usr to be mounted.
4351.24Slukem#
4361.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
4371.42Slukem#				Requires /usr to be mounted.
4381.24Slukem#
4391.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
4401.32Slukem#				to run the chrooted ${command} with.
4411.42Slukem#				Requires /usr to be mounted.
4421.24Slukem#
4431.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
4441.11Slukem#				Otherwise, use default command (see below)
4451.24Slukem#
4461.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
4471.46Slukem#				${rc_arg}_cmd method in the default
4481.46Slukem#				operation (i.e, after checking for required
4491.46Slukem#				bits and process (non)existence).
4501.11Slukem#				If this completes with a non-zero exit code,
4511.46Slukem#				don't run ${rc_arg}_cmd.
4521.24Slukem#
4531.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
4541.46Slukem#				${rc_arg}_cmd method, if that method
4551.43Slukem#				returned a zero exit code.
4561.43Slukem#
4571.11Slukem#	required_dirs	n	If set, check for the existence of the given
4581.11Slukem#				directories before running the default
4591.11Slukem#				(re)start command.
4601.24Slukem#
4611.11Slukem#	required_files	n	If set, check for the readability of the given
4621.11Slukem#				files before running the default (re)start
4631.11Slukem#				command.
4641.24Slukem#
4651.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
4661.11Slukem#				listed variables before running the default
4671.11Slukem#				(re)start command.
4681.11Slukem#
4691.46Slukem#	Default behaviour for a given argument, if no override method is
4701.46Slukem#	provided:
4711.24Slukem#
4721.46Slukem#	Argument	Default behaviour
4731.46Slukem#	--------	-----------------
4741.11Slukem#	start		if !running && checkyesno ${rcvar}
4751.11Slukem#				${command}
4761.24Slukem#
4771.11Slukem#	stop		if ${pidfile}
4781.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
4791.11Slukem#			else
4801.46Slukem#				rc_pid=$(check_process $command)
4811.46Slukem#			kill $sig_stop $rc_pid
4821.46Slukem#			wait_for_pids $rc_pid
4831.35Slukem#			($sig_stop defaults to TERM.)
4841.24Slukem#
4851.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
4861.35Slukem#			and doesn't wait_for_pids.
4871.11Slukem#			$sig_reload defaults to HUP.
4881.24Slukem#
4891.11Slukem#	restart		Run `stop' then `start'.
4901.11Slukem#
4911.33Slukem#	status		Show if ${command} is running, etc.
4921.33Slukem#
4931.33Slukem#	poll		Wait for ${command} to exit.
4941.33Slukem#
4951.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
4961.33Slukem#
4971.46Slukem#	Variables available to methods, and after run_rc_command() has
4981.46Slukem#	completed:
4991.46Slukem#
5001.46Slukem#	Variable	Purpose
5011.46Slukem#	--------	-------
5021.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
5031.46Slukem#			performed
5041.46Slukem#
5051.46Slukem#	rc_flags	Flags to start the default command with.
5061.46Slukem#			Defaults to ${name}_flags, unless overridden
5071.46Slukem#			by $flags from the environment.
5081.46Slukem#			This variable may be changed by the precmd method.
5091.46Slukem#
5101.46Slukem#	rc_pid		PID of command (if appropriate)
5111.46Slukem#
5121.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
5131.46Slukem#
5141.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
5151.33Slukem#
5161.24Slukem#
5171.11Slukemrun_rc_command()
5181.11Slukem{
5191.46Slukem	rc_arg=$1
5201.24Slukem	if [ -z "$name" ]; then
5211.30Slukem		err 3 'run_rc_command: $name is not set.'
5221.11Slukem	fi
5231.11Slukem
5241.61Slukem	_rc_prefix=
5251.46Slukem	case "$rc_arg" in
5261.24Slukem	fast*)				# "fast" prefix; don't check pid
5271.46Slukem		rc_arg=${rc_arg#fast}
5281.48Slukem		rc_fast=yes
5291.11Slukem		;;
5301.61Slukem	force*)				# "force" prefix; always run
5311.48Slukem		rc_force=yes
5321.61Slukem		_rc_prefix=force
5331.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
5341.61Slukem		if [ -n "${rcvar}" ]; then
5351.61Slukem			eval ${rcvar}=YES
5361.61Slukem		fi
5371.61Slukem		;;
5381.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
5391.61Slukem		_rc_prefix=one
5401.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
5411.29Slukem		if [ -n "${rcvar}" ]; then
5421.29Slukem			eval ${rcvar}=YES
5431.29Slukem		fi
5441.11Slukem		;;
5451.11Slukem	esac
5461.11Slukem
5471.75Sreed	_keywords="start stop restart rcvar"
5481.75Sreed	if [ -n "$extra_commands" ]; then
5491.75Sreed		_keywords="${_keywords} ${extra_commands}"
5501.75Sreed	fi
5511.46Slukem	rc_pid=
5521.11Slukem	_pidcmd=
5531.42Slukem	_procname=${procname:-${command}}
5541.42Slukem
5551.24Slukem					# setup pid check command if not fast
5561.103Skre	if [ -z "$rc_fast" ] && [ -n "$_procname" ]; then
5571.11Slukem		if [ -n "$pidfile" ]; then
5581.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
5591.42Slukem		else
5601.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
5611.11Slukem		fi
5621.11Slukem		if [ -n "$_pidcmd" ]; then
5631.33Slukem			_keywords="${_keywords} status poll"
5641.11Slukem		fi
5651.11Slukem	fi
5661.11Slukem
5671.46Slukem	if [ -z "$rc_arg" ]; then
5681.11Slukem		rc_usage "$_keywords"
5691.11Slukem	fi
5701.81Sjmmv	shift	# remove $rc_arg from the positional parameters
5711.11Slukem
5721.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
5731.46Slukem		rc_flags=$flags
5741.11Slukem	else
5751.46Slukem		eval rc_flags=\$${name}_flags
5761.11Slukem	fi
5771.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
5781.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
5791.72She	    _group=\$${name}_group	_groups=\$${name}_groups \
5801.72She	    _env=\"\$${name}_env\"
5811.11Slukem
5821.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
5831.42Slukem		if [ "$_user" = "$(id -un)" ]; then
5841.42Slukem			unset _user
5851.42Slukem		fi
5861.42Slukem	fi
5871.42Slukem
5881.29Slukem					# if ${rcvar} is set, and $1 is not
5891.40Slukem					# "rcvar", then run
5901.29Slukem					#	checkyesno ${rcvar}
5911.74Ssalo					# and return if that failed or warn
5921.74Ssalo					# user and exit when interactive
5931.24Slukem					#
5941.103Skre	if [ -n "${rcvar}" ] && [ "$rc_arg" != "rcvar" ]; then
5951.24Slukem		if ! checkyesno ${rcvar}; then
5961.74Ssalo					# check whether interactive or not
5971.74Ssalo			if [ -n "$_run_rc_script" ]; then
5981.74Ssalo				return 0
5991.74Ssalo			fi
6001.74Ssalo			for _elem in $_keywords; do
6011.74Ssalo				if [ "$_elem" = "$rc_arg" ]; then
6021.88Sapb					cat 1>&2 <<EOF
6031.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}.
6041.88SapbUse the following if you wish to perform the operation:
6051.88Sapb  $0 one${rc_arg}
6061.88SapbEOF
6071.74Ssalo					exit 1
6081.74Ssalo				fi
6091.74Ssalo			done
6101.74Ssalo			echo 1>&2 "$0: unknown directive '$rc_arg'."
6111.74Ssalo			rc_usage "$_keywords"
6121.24Slukem		fi
6131.24Slukem	fi
6141.24Slukem
6151.24Slukem	eval $_pidcmd			# determine the pid if necessary
6161.11Slukem
6171.11Slukem	for _elem in $_keywords; do
6181.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
6191.11Slukem			continue
6201.11Slukem		fi
6211.11Slukem
6221.24Slukem					# if there's a custom ${XXX_cmd},
6231.24Slukem					# run that instead of the default
6241.24Slukem					#
6251.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
6261.46Slukem		    _postcmd=\$${rc_arg}_postcmd
6271.11Slukem		if [ -n "$_cmd" ]; then
6281.24Slukem					# if the precmd failed and force
6291.24Slukem					# isn't set, exit
6301.24Slukem					#
6311.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6321.24Slukem				return 1
6331.24Slukem			fi
6341.24Slukem
6351.81Sjmmv			if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then
6361.44Slukem				return 1
6371.44Slukem			fi
6381.44Slukem			eval $_postcmd
6391.11Slukem			return 0
6401.11Slukem		fi
6411.11Slukem
6421.81Sjmmv		if [ ${#} -gt 0 ]; then
6431.81Sjmmv			err 1 "the $rc_arg command does not take any parameters"
6441.81Sjmmv		fi
6451.81Sjmmv
6461.46Slukem		case "$rc_arg" in	# default operations...
6471.11Slukem
6481.11Slukem		status)
6491.46Slukem			if [ -n "$rc_pid" ]; then
6501.46Slukem				echo "${name} is running as pid $rc_pid."
6511.11Slukem			else
6521.11Slukem				echo "${name} is not running."
6531.28Slukem				return 1
6541.11Slukem			fi
6551.11Slukem			;;
6561.11Slukem
6571.11Slukem		start)
6581.46Slukem			if [ -n "$rc_pid" ]; then
6591.63Slukem				echo 1>&2 "${name} already running? (pid=$rc_pid)."
6601.11Slukem				exit 1
6611.11Slukem			fi
6621.11Slukem
6631.57Slukem			if [ ! -x ${_chroot}${command} ]; then
6641.11Slukem				return 0
6651.11Slukem			fi
6661.11Slukem
6671.24Slukem					# check for required variables,
6681.24Slukem					# directories, and files
6691.24Slukem					#
6701.11Slukem			for _f in $required_vars; do
6711.11Slukem				if ! checkyesno $_f; then
6721.65Slukem					warn "\$${_f} is not enabled."
6731.46Slukem					if [ -z "$rc_force" ]; then
6741.24Slukem						return 1
6751.24Slukem					fi
6761.11Slukem				fi
6771.11Slukem			done
6781.11Slukem			for _f in $required_dirs; do
6791.11Slukem				if [ ! -d "${_f}/." ]; then
6801.25Slukem					warn "${_f} is not a directory."
6811.46Slukem					if [ -z "$rc_force" ]; then
6821.24Slukem						return 1
6831.24Slukem					fi
6841.11Slukem				fi
6851.11Slukem			done
6861.11Slukem			for _f in $required_files; do
6871.11Slukem				if [ ! -r "${_f}" ]; then
6881.25Slukem					warn "${_f} is not readable."
6891.46Slukem					if [ -z "$rc_force" ]; then
6901.24Slukem						return 1
6911.24Slukem					fi
6921.11Slukem				fi
6931.11Slukem			done
6941.11Slukem
6951.24Slukem					# if the precmd failed and force
6961.24Slukem					# isn't set, exit
6971.24Slukem					#
6981.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6991.24Slukem				return 1
7001.24Slukem			fi
7011.24Slukem
7021.24Slukem					# setup the command to run, and run it
7031.29Slukem					#
7041.11Slukem			echo "Starting ${name}."
7051.22Slukem			if [ -n "$_chroot" ]; then
7061.22Slukem				_doit="\
7071.100Schristos$_env_clear_rc_vars $_env \
7081.23Slukem${_nice:+nice -n $_nice }\
7091.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
7101.46Slukem$_chroot $command $rc_flags $command_args"
7111.22Slukem			else
7121.22Slukem				_doit="\
7131.18Slukem${_chdir:+cd $_chdir; }\
7141.100Schristos$_env_clear_rc_vars $_env \
7151.18Slukem${_nice:+nice -n $_nice }\
7161.46Slukem$command $rc_flags $command_args"
7171.34Slukem				if [ -n "$_user" ]; then
7181.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
7191.34Slukem				fi
7201.22Slukem			fi
7211.43Slukem
7221.43Slukem					# if the cmd failed and force
7231.43Slukem					# isn't set, exit
7241.43Slukem					#
7251.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7261.43Slukem				return 1
7271.43Slukem			fi
7281.43Slukem
7291.43Slukem					# finally, run postcmd
7301.43Slukem					#
7311.43Slukem			eval $_postcmd
7321.11Slukem			;;
7331.11Slukem
7341.11Slukem		stop)
7351.46Slukem			if [ -z "$rc_pid" ]; then
7361.24Slukem				if [ -n "$pidfile" ]; then
7371.63Slukem					echo 1>&2 \
7381.24Slukem				    "${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
7451.43Slukem					# if the precmd failed and force
7461.43Slukem					# isn't set, exit
7471.43Slukem					#
7481.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7491.24Slukem				return 1
7501.24Slukem			fi
7511.43Slukem
7521.43Slukem					# send the signal to stop
7531.43Slukem					#
7541.11Slukem			echo "Stopping ${name}."
7551.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
7561.34Slukem			if [ -n "$_user" ]; then
7571.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7581.34Slukem			fi
7591.43Slukem
7601.43Slukem					# if the stop cmd failed and force
7611.43Slukem					# isn't set, exit
7621.43Slukem					#
7631.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7641.43Slukem				return 1
7651.43Slukem			fi
7661.43Slukem
7671.43Slukem					# wait for the command to exit,
7681.43Slukem					# and run postcmd.
7691.46Slukem			wait_for_pids $rc_pid
7701.43Slukem			eval $_postcmd
7711.11Slukem			;;
7721.11Slukem
7731.11Slukem		reload)
7741.46Slukem			if [ -z "$rc_pid" ]; then
7751.24Slukem				if [ -n "$pidfile" ]; then
7761.63Slukem					echo 1>&2 \
7771.11Slukem				    "${name} not running? (check $pidfile)."
7781.24Slukem				else
7791.63Slukem					echo 1>&2 "${name} not running?"
7801.11Slukem				fi
7811.24Slukem				exit 1
7821.11Slukem			fi
7831.11Slukem			echo "Reloading ${name} config files."
7841.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7851.24Slukem				return 1
7861.24Slukem			fi
7871.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
7881.34Slukem			if [ -n "$_user" ]; then
7891.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7901.34Slukem			fi
7911.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7921.43Slukem				return 1
7931.43Slukem			fi
7941.43Slukem			eval $_postcmd
7951.11Slukem			;;
7961.11Slukem
7971.11Slukem		restart)
7981.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7991.24Slukem				return 1
8001.11Slukem			fi
8011.29Slukem					# prevent restart being called more
8021.29Slukem					# than once by any given script
8031.29Slukem					#
8041.53Slukem			if ${_rc_restart_done:-false}; then
8051.29Slukem				return 0
8061.29Slukem			fi
8071.53Slukem			_rc_restart_done=true
8081.33Slukem
8091.61Slukem			( $0 ${_rc_prefix}stop )
8101.61Slukem			$0 ${_rc_prefix}start
8111.11Slukem
8121.43Slukem			eval $_postcmd
8131.33Slukem			;;
8141.33Slukem
8151.33Slukem		poll)
8161.46Slukem			if [ -n "$rc_pid" ]; then
8171.46Slukem				wait_for_pids $rc_pid
8181.33Slukem			fi
8191.11Slukem			;;
8201.11Slukem
8211.11Slukem		rcvar)
8221.11Slukem			echo "# $name"
8231.24Slukem			if [ -n "$rcvar" ]; then
8241.24Slukem				if checkyesno ${rcvar}; then
8251.24Slukem					echo "\$${rcvar}=YES"
8261.24Slukem				else
8271.24Slukem					echo "\$${rcvar}=NO"
8281.24Slukem				fi
8291.11Slukem			fi
8301.11Slukem			;;
8311.11Slukem
8321.11Slukem		*)
8331.11Slukem			rc_usage "$_keywords"
8341.11Slukem			;;
8351.11Slukem
8361.11Slukem		esac
8371.11Slukem		return 0
8381.11Slukem	done
8391.11Slukem
8401.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
8411.11Slukem	rc_usage "$_keywords"
8421.11Slukem	exit 1
8431.11Slukem}
8441.11Slukem
8451.11Slukem#
8461.94Sapb# _have_rc_postprocessor
8471.94Sapb#	Test whether the current script is running in a context that
8481.94Sapb#	was invoked from /etc/rc with a postprocessor.
8491.94Sapb#
8501.94Sapb#	If the test fails, some variables may be unset to make
8511.94Sapb#	such tests more efficient in future.
8521.94Sapb#
8531.94Sapb_have_rc_postprocessor()
8541.94Sapb{
8551.94Sapb	# Cheap tests that fd and pid are set, fd is writable.
8561.97Sphx	[ -n "${_rc_pid}" ] || { unset _rc_pid; return 1; }
8571.97Sphx	[ -n "${_rc_postprocessor_fd}" ] || { unset _rc_pid; return 1; }
8581.97Sphx	eval ": >&${_rc_postprocessor_fd}" 2>/dev/null \
8591.94Sapb	|| { unset _rc_pid; return 1; }
8601.94Sapb
8611.94Sapb	return 0
8621.94Sapb}
8631.94Sapb
8641.94Sapb#
8651.11Slukem# run_rc_script file arg
8661.11Slukem#	Start the script `file' with `arg', and correctly handle the
8671.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
8681.37Slukem#	sourced into the current environment.  If `file' appears to be
8691.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
8701.37Slukem#	executable run as a child process.
8711.17Slukem#
8721.78Sapb#	If `file' contains "KEYWORD: interactive" and if we are
8731.94Sapb#	running inside /etc/rc with postprocessing, then the script's
8741.94Sapb#	stdout and stderr are redirected to $_rc_original_stdout_fd and
8751.78Sapb#	$_rc_original_stderr_fd, so the output will be displayed on the
8761.78Sapb#	console but not intercepted by /etc/rc's postprocessor.
8771.78Sapb#
8781.11Slukemrun_rc_script()
8791.11Slukem{
8801.11Slukem	_file=$1
8811.11Slukem	_arg=$2
8821.103Skre	if [ -z "$_file" ] || [ -z "$_arg" ]; then
8831.11Slukem		err 3 'USAGE: run_rc_script file arg'
8841.11Slukem	fi
8851.11Slukem
8861.74Ssalo	_run_rc_script=true
8871.74Ssalo
8881.45Slukem	unset	name command command_args command_interpreter \
8891.45Slukem		extra_commands pidfile procname \
8901.42Slukem		rcvar required_dirs required_files required_vars
8911.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
8921.39Slukem
8931.78Sapb	_must_redirect=false
8941.94Sapb	if _have_rc_postprocessor \
8951.78Sapb	    && _has_rcorder_keyword interactive $_file
8961.78Sapb	then
8971.78Sapb		_must_redirect=true
8981.78Sapb	fi
8991.78Sapb
9001.39Slukem	case "$_file" in
9011.39Slukem	*.sh)				# run in current shell
9021.78Sapb		if $_must_redirect; then
9031.78Sapb			print_rc_metadata \
9041.78Sapb			    "note:Output from ${_file} is not logged"
9051.80Sapb			no_rc_postprocess eval \
9061.80Sapb			    'set $_arg ; . $_file'
9071.78Sapb		else
9081.78Sapb			set $_arg ; . $_file
9091.78Sapb		fi
9101.39Slukem		;;
9111.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
9121.39Slukem		warn "Ignoring scratch file $_file"
9131.39Slukem		;;
9141.39Slukem	*)				# run in subshell
9151.78Sapb		if [ -x $_file ] && $_must_redirect; then
9161.78Sapb			print_rc_metadata \
9171.78Sapb			    "note:Output from ${_file} is not logged"
9181.78Sapb			if [ -n "$rc_fast_and_loose" ]; then
9191.80Sapb				no_rc_postprocess eval \
9201.80Sapb				    'set $_arg ; . $_file'
9211.78Sapb			else
9221.80Sapb				no_rc_postprocess eval \
9231.80Sapb				    '( set $_arg ; . $_file )'
9241.78Sapb			fi
9251.78Sapb		elif [ -x $_file ]; then
9261.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
9271.39Slukem				set $_arg ; . $_file
9281.39Slukem			else
9291.37Slukem				( set $_arg ; . $_file )
9301.37Slukem			fi
9311.78Sapb		else
9321.78Sapb			warn "Ignoring non-executable file $_file"
9331.39Slukem		fi
9341.39Slukem		;;
9351.39Slukem	esac
9361.11Slukem}
9371.19Slukem
9381.19Slukem#
9391.65Slukem# load_rc_config command
9401.19Slukem#	Source in the configuration file for a given command.
9411.19Slukem#
9421.19Slukemload_rc_config()
9431.19Slukem{
9441.19Slukem	_command=$1
9451.19Slukem	if [ -z "$_command" ]; then
9461.19Slukem		err 3 'USAGE: load_rc_config command'
9471.19Slukem	fi
9481.19Slukem
9491.54Slukem	if ${_rc_conf_loaded:-false}; then
9501.54Slukem		:
9511.54Slukem	else
9521.30Slukem		. /etc/rc.conf
9531.53Slukem		_rc_conf_loaded=true
9541.30Slukem	fi
9551.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
9561.20Sfvdl		. /etc/rc.conf.d/"$_command"
9571.20Sfvdl	fi
9581.19Slukem}
9591.19Slukem
9601.65Slukem#
9611.65Slukem# load_rc_config_var cmd var
9621.65Slukem#	Read the rc.conf(5) var for cmd and set in the
9631.65Slukem#	current shell, using load_rc_config in a subshell to prevent
9641.65Slukem#	unwanted side effects from other variable assignments.
9651.65Slukem#
9661.65Slukemload_rc_config_var()
9671.65Slukem{
9681.65Slukem	if [ $# -ne 2 ]; then
9691.65Slukem		err 3 'USAGE: load_rc_config_var cmd var'
9701.65Slukem	fi
9711.65Slukem	eval $(eval '(
9721.65Slukem		load_rc_config '$1' >/dev/null;
9731.103Skre		if [ -n "${'$2'}" ] || [ "${'$2'-UNSET}" != "UNSET" ]; then
9741.65Slukem			echo '$2'=\'\''${'$2'}\'\'';
9751.65Slukem		fi
9761.65Slukem	)' )
9771.65Slukem}
9781.11Slukem
9791.11Slukem#
9801.11Slukem# rc_usage commands
9811.11Slukem#	Print a usage string for $0, with `commands' being a list of
9821.11Slukem#	valid commands.
9831.11Slukem#
9841.11Slukemrc_usage()
9851.11Slukem{
9861.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
9871.11Slukem
9881.11Slukem	_sep=
9891.56Schristos	for _elem; do
9901.11Slukem		echo -n 1>&2 "$_sep$_elem"
9911.11Slukem		_sep="|"
9921.11Slukem	done
9931.11Slukem	echo 1>&2 ")"
9941.11Slukem	exit 1
9951.11Slukem}
9961.11Slukem
9971.11Slukem#
9981.11Slukem# err exitval message
9991.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
10001.11Slukem#
10011.11Slukemerr()
10021.11Slukem{
10031.11Slukem	exitval=$1
10041.11Slukem	shift
10051.11Slukem
10061.51Sgrant	if [ -x /usr/bin/logger ]; then
10071.51Sgrant		logger "$0: ERROR: $*"
10081.51Sgrant	fi
10091.21Slukem	echo 1>&2 "$0: ERROR: $*"
10101.11Slukem	exit $exitval
10111.11Slukem}
10121.11Slukem
10131.11Slukem#
10141.11Slukem# warn message
10151.11Slukem#	Display message to stderr and log to the syslog.
10161.11Slukem#
10171.11Slukemwarn()
10181.11Slukem{
10191.51Sgrant	if [ -x /usr/bin/logger ]; then
10201.51Sgrant		logger "$0: WARNING: $*"
10211.51Sgrant	fi
10221.21Slukem	echo 1>&2 "$0: WARNING: $*"
10231.31Satatat}
10241.31Satatat
10251.31Satatat#
10261.31Satatat# backup_file action file cur backup
10271.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
10281.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
10291.31Satatat#
10301.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
10311.31Satatat#	which can be either YES or NO.
10321.31Satatat#
10331.31Satatat#	The `action' keyword can be one of the following:
10341.31Satatat#
10351.31Satatat#	add		`file' is now being backed up (and is possibly
10361.31Satatat#			being reentered into the backups system).  `cur'
10371.31Satatat#			is created and RCS files, if necessary, are
10381.31Satatat#			created as well.
10391.31Satatat#
10401.31Satatat#	update		`file' has changed and needs to be backed up.
10411.31Satatat#			If `cur' exists, it is copied to to `back' or
10421.31Satatat#			checked into RCS (if the repository file is old),
10431.31Satatat#			and then `file' is copied to `cur'.  Another RCS
10441.31Satatat#			check in done here if RCS is being used.
10451.31Satatat#
10461.31Satatat#	remove		`file' is no longer being tracked by the backups
10471.31Satatat#			system.  If RCS is not being used, `cur' is moved
10481.31Satatat#			to `back', otherwise an empty file is checked in,
10491.31Satatat#			and then `cur' is removed.
10501.31Satatat#
10511.31Satatat#
10521.31Satatatbackup_file()
10531.31Satatat{
10541.31Satatat	_action=$1
10551.31Satatat	_file=$2
10561.31Satatat	_cur=$3
10571.31Satatat	_back=$4
10581.31Satatat
10591.31Satatat	if checkyesno backup_uses_rcs; then
10601.31Satatat		_msg0="backup archive"
10611.31Satatat		_msg1="update"
10621.31Satatat
10631.36Satatat		# ensure that history file is not locked
10641.36Satatat		if [ -f $_cur,v ]; then
10651.36Satatat			rcs -q -u -U -M $_cur
10661.36Satatat		fi
10671.36Satatat
10681.31Satatat		# ensure after switching to rcs that the
10691.31Satatat		# current backup is not lost
10701.31Satatat		if [ -f $_cur ]; then
10711.31Satatat			# no archive, or current newer than archive
10721.103Skre			if [ ! -f $_cur,v ] || [ $_cur -nt $_cur,v ]; then
10731.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10741.36Satatat				rcs -q -kb -U $_cur
10751.49Slukem				co -q -f -u $_cur
10761.31Satatat			fi
10771.31Satatat		fi
10781.31Satatat
10791.31Satatat		case $_action in
10801.31Satatat		add|update)
10811.31Satatat			cp -p $_file $_cur
10821.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10831.36Satatat			rcs -q -kb -U $_cur
10841.49Slukem			co -q -f -u $_cur
10851.31Satatat			chown root:wheel $_cur $_cur,v
10861.31Satatat			;;
10871.31Satatat		remove)
10881.31Satatat			cp /dev/null $_cur
10891.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10901.36Satatat			rcs -q -kb -U $_cur
10911.31Satatat			chown root:wheel $_cur $_cur,v
10921.31Satatat			rm $_cur
10931.31Satatat			;;
10941.31Satatat		esac
10951.31Satatat	else
10961.31Satatat		case $_action in
10971.31Satatat		add|update)
10981.31Satatat			if [ -f $_cur ]; then
10991.31Satatat				cp -p $_cur $_back
11001.31Satatat			fi
11011.31Satatat			cp -p $_file $_cur
11021.31Satatat			chown root:wheel $_cur
11031.31Satatat			;;
11041.31Satatat		remove)
11051.31Satatat			mv -f $_cur $_back
11061.31Satatat			;;
11071.31Satatat		esac
11081.31Satatat	fi
11091.1Scjs}
11101.64Smycroft
11111.76Schristos#
11121.76Schristos# handle_fsck_error fsck_exit_code
11131.76Schristos#	Take action depending on the return code from fsck.
11141.76Schristos#
11151.76Schristoshandle_fsck_error()
11161.76Schristos{
11171.76Schristos	case $1 in
11181.76Schristos	0)	# OK
11191.76Schristos		return
11201.76Schristos		;;
11211.76Schristos	2)	# Needs re-run, still fs errors
11221.76Schristos		echo "File system still has errors; re-run fsck manually!"
11231.76Schristos		;;
11241.76Schristos	4)	# Root modified
11251.93Swiz		echo "Root file system was modified, rebooting ..."
11261.76Schristos		reboot -n
11271.76Schristos		echo "Reboot failed; help!"
11281.76Schristos		;;
11291.76Schristos	8)	# Check failed
11301.76Schristos		echo "Automatic file system check failed; help!"
11311.76Schristos		;;
11321.76Schristos	12)	# Got signal
11331.76Schristos		echo "Boot interrupted."
11341.76Schristos		;;
11351.76Schristos	*)
11361.76Schristos		echo "Unknown error $1; help!"
11371.76Schristos		;;
11381.76Schristos	esac
11391.76Schristos	stop_boot
11401.76Schristos}
11411.76Schristos
11421.78Sapb#
11431.78Sapb# _has_rcorder_keyword word file
11441.78Sapb#	Check whether a file contains a "# KEYWORD:" comment with a
11451.78Sapb#	specified keyword in the style used by rcorder(8).
11461.78Sapb#
11471.78Sapb_has_rcorder_keyword()
11481.78Sapb{
11491.78Sapb	local word="$1"
11501.78Sapb	local file="$2"
11511.78Sapb	local line
11521.78Sapb
11531.78Sapb	[ -r "$file" ] || return 1
11541.78Sapb	while read line; do
11551.78Sapb		case "${line} " in
11561.78Sapb		"# KEYWORD:"*[\ \	]"${word}"[\ \	]*)
11571.78Sapb			return 0
11581.78Sapb			;;
11591.78Sapb		"#"*)
11601.78Sapb			continue
11611.78Sapb			;;
11621.78Sapb		*[A-Za-z0-9]*)
11631.78Sapb			# give up at the first non-empty non-comment line
11641.78Sapb			return 1
11651.78Sapb			;;
11661.78Sapb		esac
11671.78Sapb	done <"$file"
11681.78Sapb	return 1
11691.78Sapb}
11701.78Sapb
11711.78Sapb#
11721.78Sapb# print_rc_metadata string
11731.78Sapb#	Print the specified string in such a way that the post-processor
11741.78Sapb#	inside /etc/rc will treat it as meta-data.
11751.78Sapb#
11761.78Sapb#	If we are not running inside /etc/rc, do nothing.
11771.78Sapb#
11781.78Sapb#	For public use by any rc.d script, the string must begin with
11791.78Sapb#	"note:", followed by arbitrary text.  The intent is that the text
11801.78Sapb#	will appear in a log file but not on the console.
11811.78Sapb#
11821.78Sapb#	For private use within /etc/rc, the string must contain a
11831.78Sapb#	keyword recognised by the rc_postprocess_metadata() function
11841.78Sapb#	defined in /etc/rc, followed by a colon, followed by one or more
11851.78Sapb#	colon-separated arguments associated with the keyword.
11861.78Sapb#
11871.78Sapbprint_rc_metadata()
11881.78Sapb{
11891.78Sapb	# _rc_postprocessor fd, if defined, is the fd to which we must
11901.78Sapb	# print, prefixing the output with $_rc_metadata_prefix.
11911.78Sapb	#
11921.94Sapb	if _have_rc_postprocessor; then
11931.88Sapb		command printf "%s%s\n" "$rc_metadata_prefix" "$1" \
11941.78Sapb			>&${_rc_postprocessor_fd}
11951.78Sapb	fi
11961.78Sapb}
11971.78Sapb
11981.78Sapb#
11991.88Sapb# _flush_rc_output
12001.88Sapb#	Arrange for output to be flushed, if we are running
12011.88Sapb#	inside /etc/rc with postprocessing.
12021.88Sapb#
12031.88Sapb_flush_rc_output()
12041.88Sapb{
12051.88Sapb	print_rc_metadata "nop"
12061.88Sapb}
12071.88Sapb
12081.88Sapb#
12091.88Sapb# print_rc_normal [-n] string
12101.78Sapb#	Print the specified string in such way that it is treated as
12111.78Sapb#	normal output, regardless of whether or not we are running
12121.78Sapb#	inside /etc/rc with post-processing.
12131.78Sapb#
12141.88Sapb#	If "-n" is specified in $1, then the string in $2 is printed
12151.88Sapb#	without a newline; otherwise, the string in $1 is printed
12161.88Sapb#	with a newline.
12171.88Sapb#
12181.88Sapb#	Intended use cases include:
12191.88Sapb#
12201.88Sapb#	o   An rc.d script can use ``print_rc_normal -n'' to print a
12211.88Sapb#	    partial line in such a way that it appears immediately
12221.88Sapb#	    instead of being buffered by rc(8)'s post-processor.
12231.88Sapb#
12241.88Sapb#	o   An rc.d script that is run via the no_rc_postprocess
12251.88Sapb#	    function (so most of its output is invisible to rc(8)'s
12261.88Sapb#	    post-processor) can use print_rc_normal to force some of its
12271.88Sapb#	    output to be seen by the post-processor.
12281.88Sapb#
12291.78Sapb#
12301.78Sapbprint_rc_normal()
12311.78Sapb{
12321.94Sapb	# print to stdout or _rc_postprocessor_fd, depending on
12331.94Sapb	# whether not we have an rc postprocessor.
12341.78Sapb	#
12351.94Sapb	local fd=1
12361.94Sapb	_have_rc_postprocessor && fd="${_rc_postprocessor_fd}"
12371.88Sapb	case "$1" in
12381.88Sapb	"-n")
12391.88Sapb		command printf "%s" "$2" >&${fd}
12401.88Sapb		_flush_rc_output
12411.88Sapb		;;
12421.88Sapb	*)
12431.88Sapb		command printf "%s\n" "$1" >&${fd}
12441.88Sapb		;;
12451.88Sapb	esac
12461.78Sapb}
12471.78Sapb
12481.78Sapb#
12491.78Sapb# no_rc_postprocess cmd...
12501.78Sapb#	Execute the specified command in such a way that its output
12511.78Sapb#	bypasses the post-processor that handles the output from
12521.78Sapb#	most commands that are run inside /etc/rc.  If we are not
12531.78Sapb#	inside /etc/rc, then just execute the command without special
12541.78Sapb#	treatment.
12551.78Sapb#
12561.78Sapb#	The intent is that interactive commands can be run via
12571.78Sapb#	no_rc_postprocess(), and their output will apear immediately
12581.78Sapb#	on the console instead of being hidden or delayed by the
12591.78Sapb#	post-processor.	 An unfortunate consequence of the output
12601.78Sapb#	bypassing the post-processor is that the output will not be
12611.78Sapb#	logged.
12621.78Sapb#
12631.78Sapbno_rc_postprocess()
12641.78Sapb{
12651.94Sapb	if _have_rc_postprocessor; then
12661.78Sapb		"$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
12671.78Sapb	else
12681.78Sapb		"$@"
12691.78Sapb	fi
12701.78Sapb}
12711.78Sapb
12721.78Sapb#
12731.78Sapb# twiddle
12741.78Sapb#	On each call, print a different one of "/", "-", "\\", "|",
12751.78Sapb#	followed by a backspace.  The most recently printed value is
12761.78Sapb#	saved in $_twiddle_state.
12771.78Sapb#
12781.78Sapb#	Output is to /dev/tty, so this function may be useful even inside
12791.78Sapb#	a script whose output is redirected.
12801.78Sapb#
12811.78Sapbtwiddle()
12821.78Sapb{
12831.78Sapb	case "$_twiddle_state" in
12841.78Sapb	'/')	_next='-' ;;
12851.78Sapb	'-')	_next='\' ;;
12861.78Sapb	'\')	_next='|' ;;
12871.78Sapb	*)	_next='/' ;;
12881.78Sapb	esac
12891.88Sapb	command printf "%s\b" "$_next" >/dev/tty
12901.78Sapb	_twiddle_state="$_next"
12911.78Sapb}
12921.78Sapb
12931.82Schristos#
12941.82Schristos# human_exit_code
12951.82Schristos#	Print the a human version of the exit code.
12961.82Schristos#
12971.82Schristoshuman_exit_code()
12981.82Schristos{
12991.83Schristos	if [ "$1" -lt 127 ]
13001.82Schristos	then
13011.82Schristos		echo "exited with code $1"
13021.85Schristos	elif [ "$(expr $1 % 256)" -eq 127 ]
13031.82Schristos	then
13041.84Schristos		# This cannot really happen because the shell will not
13051.84Schristos		# pass stopped job status out and the exit code is limited
13061.84Schristos		# to 8 bits. This code is here just for completeness.
13071.82Schristos		echo "stopped with signal $(expr $1 / 256)"
13081.82Schristos	else
13091.82Schristos		echo "terminated with signal $(expr $1 - 128)"
13101.82Schristos	fi
13111.82Schristos}
13121.86Sapb
13131.86Sapb#
13141.86Sapb# collapse_backslash_newline
13151.86Sapb#	Copy input to output, collapsing <backslash><newline>
13161.86Sapb#	to nothing, but leaving other backslashes alone.
13171.86Sapb#
13181.86Sapbcollapse_backslash_newline()
13191.86Sapb{
13201.86Sapb	local line
13211.86Sapb	while read -r line ; do
13221.86Sapb		case "$line" in
13231.86Sapb		*\\)
13241.86Sapb			# print it, without the backslash or newline
13251.88Sapb			command printf "%s" "${line%?}"
13261.86Sapb			;;
13271.86Sapb		*)
13281.86Sapb			# print it, with a newline
13291.88Sapb			command printf "%s\n" "${line}"
13301.86Sapb			;;
13311.86Sapb		esac
13321.86Sapb	done
13331.86Sapb}
13341.82Schristos
13351.92Sapb# Shell implementations of basename and dirname, usable before
13361.92Sapb# the /usr file system is mounted.
13371.92Sapb#
13381.92Sapbbasename()
13391.92Sapb{
13401.92Sapb	local file="$1"
13411.92Sapb	local suffix="$2"
13421.92Sapb	local base
13431.92Sapb
13441.92Sapb	base="${file##*/}"		# remove up to and including last '/'
13451.92Sapb	base="${base%${suffix}}"	# remove suffix, if any
13461.92Sapb	command printf "%s\n" "${base}"
13471.92Sapb}
13481.92Sapb
13491.92Sapbdirname()
13501.92Sapb{
13511.92Sapb	local file="$1"
13521.92Sapb	local dir
13531.92Sapb
13541.92Sapb	case "$file" in
13551.92Sapb	/*/*)	dir="${file%/*}" ;;	# common case: absolute path
13561.92Sapb	/*)	dir="/" ;;		# special case: name in root dir
13571.92Sapb	*/*)	dir="${file%/*}" ;;	# common case: relative path with '/'
13581.92Sapb	*)	dir="." ;;		# special case: name without '/'
13591.92Sapb	esac
13601.92Sapb	command printf "%s\n" "${dir}"
13611.92Sapb}
13621.92Sapb
13631.88Sapb# Override the normal "echo" and "printf" commands, so that
13641.88Sapb# partial lines printed by rc.d scripts appear immediately,
13651.88Sapb# instead of being buffered by rc(8)'s post-processor.
13661.88Sapb#
13671.88Sapb# Naive use of the echo or printf commands from rc.d scripts,
13681.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr,
13691.88Sapb# will call these functions.  To call the real echo and printf
13701.88Sapb# commands, use "command echo" or "command printf".
13711.88Sapb#
13721.103Skre# Avoid use of echo altogether as much as possible, printf works better
13731.103Skre#
13741.88Sapbecho()
13751.88Sapb{
13761.103Skre	local IFS=' ' NL='\n'	# not a literal newline...
13771.103Skre
13781.88Sapb	case "$1" in
13791.103Skre	-n)	NL=; shift;;
13801.88Sapb	esac
13811.103Skre
13821.103Skre	command printf "%s${NL}" "$*"
13831.103Skre
13841.103Skre	if test -z "${NL}"
13851.103Skre	then
13861.103Skre		_flush_rc_output
13871.103Skre	fi
13881.103Skre	return 0
13891.88Sapb}
13901.103Skre
13911.88Sapbprintf()
13921.88Sapb{
13931.88Sapb	command printf "$@"
13941.88Sapb	case "$1" in
13951.88Sapb	*'\n')	: ;;
13961.88Sapb	*)	_flush_rc_output ;;
13971.88Sapb	esac
13981.103Skre	return 0
13991.88Sapb}
14001.88Sapb
14011.98Schristoskat() {
14021.98Schristos	local i
14031.98Schristos	local v
14041.98Schristos	for i; do
14051.98Schristos		while read -r v; do
14061.98Schristos			v="${v%%#*}"
14071.98Schristos			if [ -z "$v" ]; then
14081.98Schristos				continue
14091.98Schristos			fi
14101.98Schristos			echo "$v"
14111.98Schristos		done < "$i"
14121.98Schristos	done
14131.98Schristos}
14141.98Schristos
14151.64Smycroft_rc_subr_loaded=:
1416