rc.subr revision 1.107
11.107Schristos# $NetBSD: rc.subr,v 1.107 2021/11/06 23:11:43 christos 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.107Schristosexport PATH=/sbin:/bin:/usr/sbin:/usr/bin
501.11Slukem#
511.11Slukem#	functions
521.11Slukem#	---------
531.1Scjs
541.5Slukem#
551.11Slukem# checkyesno var
561.95Sroy#	Test $1 variable.
571.95Sroy#	Return 0 if it's "yes" (et al), 1 if it's "no" (et al), 2 otherwise.
581.5Slukem#
591.95Sroycheckyesnox()
601.11Slukem{
611.11Slukem	eval _value=\$${1}
621.11Slukem	case $_value in
631.4Slukem
641.4Slukem		#	"yes", "true", "on", or "1"
651.4Slukem	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
661.4Slukem		return 0
671.3Slukem		;;
681.4Slukem
691.4Slukem		#	"no", "false", "off", or "0"
701.4Slukem	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
711.4Slukem		return 1
721.3Slukem		;;
731.3Slukem	*)
741.95Sroy		return 2
751.3Slukem		;;
761.3Slukem	esac
771.38Slukem}
781.38Slukem
791.65Slukem#
801.95Sroy# checkyesno var
811.95Sroy#	Test $1 variable, and warn if not set to YES or NO.
821.95Sroy#	Return 0 if it's "yes" (et al), nonzero otherwise.
831.95Sroy#
841.95Sroycheckyesno()
851.95Sroy{
861.95Sroy	local var
871.95Sroy
881.95Sroy	checkyesnox $1
891.95Sroy	var=$?
901.103Skre	case "${var}" in
911.103Skre	( 0 | 1 )	return $var;;
921.103Skre	esac
931.95Sroy	warn "\$${1} is not set properly - see ${rcvar_manpage}."
941.95Sroy	return 1
951.95Sroy}
961.95Sroy
971.95Sroy#
981.78Sapb# yesno_to_truefalse var
991.78Sapb#	Convert the value of a variable from any of the values
1001.78Sapb#	understood by checkyesno() to "true" or "false".
1011.78Sapb#
1021.78Sapbyesno_to_truefalse()
1031.78Sapb{
1041.78Sapb	local var=$1
1051.78Sapb	if checkyesno $var; then
1061.78Sapb		eval $var=true
1071.78Sapb		return 0
1081.78Sapb	else
1091.78Sapb		eval $var=false
1101.78Sapb		return 1
1111.78Sapb	fi
1121.78Sapb}
1131.78Sapb
1141.78Sapb#
1151.38Slukem# reverse_list list
1161.38Slukem#	print the list in reverse order
1171.38Slukem#
1181.38Slukemreverse_list()
1191.38Slukem{
1201.38Slukem	_revlist=
1211.56Schristos	for _revfile; do
1221.38Slukem		_revlist="$_revfile $_revlist"
1231.38Slukem	done
1241.38Slukem	echo $_revlist
1251.6Smellon}
1261.6Smellon
1271.6Smellon#
1281.69Sapb# If booting directly to multiuser, send SIGTERM to
1291.69Sapb# the parent (/etc/rc) to abort the boot.
1301.69Sapb# Otherwise just exit.
1311.69Sapb#
1321.69Sapbstop_boot()
1331.69Sapb{
1341.69Sapb	if [ "$autoboot" = yes ]; then
1351.69Sapb		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
1361.69Sapb		kill -TERM ${RC_PID}
1371.69Sapb	fi
1381.69Sapb	exit 1
1391.69Sapb}
1401.69Sapb
1411.69Sapb#
1421.47Slukem# mount_critical_filesystems type
1431.93Swiz#	Go through the list of critical file systems as provided in
1441.47Slukem#	the rc.conf(5) variable $critical_filesystems_${type}, checking
1451.47Slukem#	each one to see if it is mounted, and if it is not, mounting it.
1461.79Sapb#	It's not an error if file systems prefixed with "OPTIONAL:"
1471.79Sapb#	are not mentioned in /etc/fstab.
1481.6Smellon#
1491.11Slukemmount_critical_filesystems()
1501.11Slukem{
1511.47Slukem	eval _fslist=\$critical_filesystems_${1}
1521.79Sapb	_mountcrit_es=0
1531.17Slukem	for _fs in $_fslist; do
1541.79Sapb		_optional=false
1551.79Sapb		case "$_fs" in
1561.79Sapb		OPTIONAL:*)
1571.79Sapb			_optional=true
1581.79Sapb			_fs="${_fs#*:}"
1591.79Sapb			;;
1601.79Sapb		esac
1611.79Sapb		_ismounted=false
1621.79Sapb		# look for a line like "${fs} on * type *"
1631.79Sapb		# or "* on ${fs} type *" in the output from mount.
1641.79Sapb		case "${nl}$( mount )${nl}" in
1651.79Sapb		*" on ${_fs} type "*)
1661.79Sapb			_ismounted=true
1671.79Sapb			;;
1681.79Sapb		*"${nl}${_fs} on "*)
1691.79Sapb			_ismounted=true
1701.79Sapb			;;
1711.79Sapb		esac
1721.79Sapb		if $_ismounted; then
1731.79Sapb			print_rc_metadata \
1741.79Sapb			"note:File system ${_fs} was already mounted"
1751.79Sapb		else
1761.79Sapb			_mount_output=$( mount $_fs 2>&1 )
1771.79Sapb			_mount_es=$?
1781.79Sapb			case "$_mount_output" in
1791.79Sapb			*"${nl}"*)
1801.79Sapb				# multiple lines can't be good,
1811.79Sapb				# not even if $_optional is true
1821.79Sapb				;;
1831.89Sapb			*[uU]'nknown special file or file system'*)
1841.79Sapb				if $_optional; then
1851.79Sapb					# ignore this error
1861.79Sapb					print_rc_metadata \
1871.79Sapb			"note:Optional file system ${_fs} is not present"
1881.79Sapb					_mount_es=0
1891.79Sapb					_mount_output=""
1901.6Smellon				fi
1911.79Sapb				;;
1921.79Sapb			esac
1931.79Sapb			if [ -n "$_mount_output" ]; then
1941.79Sapb				printf >&2 "%s\n" "$_mount_output"
1951.79Sapb			fi
1961.79Sapb			if [ "$_mount_es" != 0 ]; then
1971.79Sapb				_mountcrit_es="$_mount_es"
1981.6Smellon			fi
1991.79Sapb		fi
2001.6Smellon	done
2011.79Sapb	return $_mountcrit_es
2021.7Scjs}
2031.7Scjs
2041.11Slukem#
2051.45Slukem# check_pidfile pidfile procname [interpreter]
2061.45Slukem#	Parses the first line of pidfile for a PID, and ensures
2071.11Slukem#	that the process is running and matches procname.
2081.45Slukem#	Prints the matching PID upon success, nothing otherwise.
2091.45Slukem#	interpreter is optional; see _find_processes() for details.
2101.11Slukem#
2111.11Slukemcheck_pidfile()
2121.11Slukem{
2131.11Slukem	_pidfile=$1
2141.11Slukem	_procname=$2
2151.45Slukem	_interpreter=$3
2161.103Skre	if [ -z "$_pidfile" ] || [ -z "$_procname" ]; then
2171.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
2181.11Slukem	fi
2191.11Slukem	if [ ! -f $_pidfile ]; then
2201.11Slukem		return
2211.11Slukem	fi
2221.11Slukem	read _pid _junk < $_pidfile
2231.11Slukem	if [ -z "$_pid" ]; then
2241.11Slukem		return
2251.11Slukem	fi
2261.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
2271.11Slukem}
2281.11Slukem
2291.11Slukem#
2301.45Slukem# check_process procname [interpreter]
2311.11Slukem#	Ensures that a process (or processes) named procname is running.
2321.45Slukem#	Prints a list of matching PIDs.
2331.45Slukem#	interpreter is optional; see _find_processes() for details.
2341.11Slukem#
2351.11Slukemcheck_process()
2361.11Slukem{
2371.11Slukem	_procname=$1
2381.45Slukem	_interpreter=$2
2391.11Slukem	if [ -z "$_procname" ]; then
2401.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
2411.45Slukem	fi
2421.101Skre	_find_processes $_procname ${_interpreter:-.} '-A'
2431.45Slukem}
2441.45Slukem
2451.46Slukem#
2461.45Slukem# _find_processes procname interpreter psargs
2471.45Slukem#	Search for procname in the output of ps generated by psargs.
2481.45Slukem#	Prints the PIDs of any matching processes, space separated.
2491.45Slukem#
2501.45Slukem#	If interpreter == ".", check the following variations of procname
2511.45Slukem#	against the first word of each command:
2521.45Slukem#		procname
2531.45Slukem#		`basename procname`
2541.45Slukem#		`basename procname` + ":"
2551.45Slukem#		"(" + `basename procname` + ")"
2561.45Slukem#
2571.45Slukem#	If interpreter != ".", read the first line of procname, remove the
2581.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
2591.45Slukem#	match that against each command, either as is, or with extra words
2601.73Sdholland#	at the end.  As an alternative, to deal with interpreted daemons
2611.66She#	using perl, the basename of the interpreter plus a colon is also
2621.66She#	tried as the prefix to procname.
2631.45Slukem#
2641.45Slukem_find_processes()
2651.45Slukem{
2661.45Slukem	if [ $# -ne 3 ]; then
2671.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
2681.11Slukem	fi
2691.45Slukem	_procname=$1
2701.45Slukem	_interpreter=$2
2711.45Slukem	_psargs=$3
2721.45Slukem
2731.11Slukem	_pref=
2741.68Shubertf	_procnamebn=${_procname##*/}
2751.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
2761.67Selad		read _interp < ${_chroot:-}/$_procname	# read interpreter name
2771.45Slukem		_interp=${_interp#\#!}		# strip #!
2781.45Slukem		set -- $_interp
2791.87Schristos		if [ $1 = "/usr/bin/env" ]; then
2801.87Schristos			shift
2811.87Schristos			set -- $(type $1)
2821.87Schristos			shift $(($# - 1))
2831.87Schristos			_interp="${1##*/} $_procname"
2841.87Schristos		else
2851.87Schristos			_interp="$* $_procname"
2861.87Schristos		fi
2871.45Slukem		if [ $_interpreter != $1 ]; then
2881.45Slukem			warn "\$command_interpreter $_interpreter != $1"
2891.45Slukem		fi
2901.66She		_interpbn=${1##*/}
2911.45Slukem		_fp_args='_argv'
2921.45Slukem		_fp_match='case "$_argv" in
2931.68Shubertf		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
2941.45Slukem	else					# a normal daemon
2951.45Slukem		_fp_args='_arg0 _argv'
2961.45Slukem		_fp_match='case "$_arg0" in
2971.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
2981.45Slukem	fi
2991.45Slukem
3001.45Slukem	_proccheck='
3011.102Schristos		ps -o "pid,args" '"$_psargs"' 2>&1 |
3021.45Slukem		while read _npid '"$_fp_args"'; do
3031.45Slukem			case "$_npid" in
3041.102Schristos			ps:|PID)
3051.45Slukem				continue ;;
3061.45Slukem			esac ; '"$_fp_match"'
3071.45Slukem				echo -n "$_pref$_npid" ;
3081.45Slukem				_pref=" "
3091.45Slukem				;;
3101.45Slukem			esac
3111.45Slukem		done'
3121.45Slukem
3131.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
3141.45Slukem	eval $_proccheck
3151.11Slukem}
3161.11Slukem
3171.11Slukem#
3181.104Schristos# kill_pids signal pid [pid ...]
3191.104Schristos#	kills the given pids with signal. 
3201.104Schristos#	returns the list of pids killed successfully.
3211.104Schristos#
3221.104Schristoskill_pids()
3231.104Schristos{
3241.104Schristos	local signal=$1
3251.104Schristos	shift
3261.105Sotis	local list="$*"
3271.104Schristos	local j=
3281.104Schristos	local nlist=
3291.104Schristos	for j in $list; do
3301.104Schristos		if kill -$signal $j 2>/dev/null; then
3311.104Schristos			nlist="${nlist}${nlist:+ }$j"
3321.104Schristos		fi
3331.104Schristos	done
3341.104Schristos	echo $nlist
3351.104Schristos}
3361.104Schristos
3371.104Schristos#
3381.33Slukem# wait_for_pids pid [pid ...]
3391.35Slukem#	spins until none of the pids exist
3401.104Schristos#	if _rc_kill_ntries is set and exceeded, it SIGKILLS the remaining
3411.104Schristos#	pids
3421.33Slukem#
3431.33Slukemwait_for_pids()
3441.33Slukem{
3451.104Schristos	local ntries=0
3461.104Schristos	local prefix=
3471.105Sotis	local nlist=
3481.105Sotis	local list="$*"
3491.104Schristos
3501.104Schristos	if [ -z "$list" ]; then
3511.33Slukem		return
3521.33Slukem	fi
3531.104Schristos
3541.35Slukem	while true; do
3551.105Sotis		nlist=$(kill_pids 0 $list)
3561.104Schristos		if [ -z "$nlist" ]; then
3571.33Slukem			break
3581.33Slukem		fi
3591.104Schristos		if [ "$list" != "$nlist" ]; then
3601.104Schristos			list=$nlist
3611.104Schristos			echo -n ${prefix:-"Waiting for PIDS: "}$list
3621.104Schristos			prefix=", "
3631.96Sroy		fi
3641.96Sroy		# We want this to be a tight loop for a fast exit
3651.96Sroy		sleep 0.05
3661.104Schristos		ntries=$((ntries + 1))
3671.104Schristos		if [ -n "${_rc_kill_ntries}" ]; then
3681.104Schristos			if [ ${ntries} -gt ${_rc_kill_ntries} ]; then
3691.104Schristos				kill_pids 9 $list > /dev/null
3701.104Schristos			fi
3711.104Schristos		fi
3721.33Slukem	done
3731.104Schristos	if [ -n "$prefix" ]; then
3741.35Slukem		echo "."
3751.35Slukem	fi
3761.33Slukem}
3771.33Slukem
3781.33Slukem#
3791.81Sjmmv# run_rc_command argument [parameters]
3801.46Slukem#	Search for argument in the list of supported commands, which is:
3811.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
3821.46Slukem#	If there's a match, run ${argument}_cmd or the default method
3831.81Sjmmv#	(see below), and pass the optional list of parameters to it.
3841.11Slukem#
3851.46Slukem#	If argument has a given prefix, then change the operation as follows:
3861.46Slukem#		Prefix	Operation
3871.11Slukem#		------	---------
3881.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
3891.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
3901.61Slukem#		one	Set ${rcvar} to YES
3911.11Slukem#
3921.11Slukem#	The following globals are used:
3931.24Slukem#
3941.46Slukem#	Name		Needed	Purpose
3951.46Slukem#	----		------	-------
3961.11Slukem#	name		y	Name of script.
3971.24Slukem#
3981.11Slukem#	command		n	Full path to command.
3991.46Slukem#				Not needed if ${rc_arg}_cmd is set for
4001.11Slukem#				each keyword.
4011.24Slukem#
4021.11Slukem#	command_args	n	Optional args/shell directives for command.
4031.24Slukem#
4041.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
4051.45Slukem#				call check_{pidfile,process}() appropriately.
4061.45Slukem#
4071.16Slukem#	extra_commands	n	List of extra commands supported.
4081.24Slukem#
4091.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
4101.42Slukem#				otherwise use check_process $command.
4111.42Slukem#				In either case, only check if $command is set.
4121.42Slukem#
4131.42Slukem#	procname	n	Process name to check for instead of $command.
4141.24Slukem#
4151.24Slukem#	rcvar		n	This is checked with checkyesno to determine
4161.24Slukem#				if the action should be run.
4171.24Slukem#
4181.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
4191.42Slukem#				Requires /usr to be mounted.
4201.24Slukem#
4211.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
4221.22Slukem#				(if not using ${name}_chroot).
4231.24Slukem#
4241.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
4251.24Slukem#				NOTE:	$flags from the parent environment
4261.24Slukem#					can be used to override this.
4271.24Slukem#
4281.72She#	${name}_env	n	Additional environment variable settings
4291.72She#				for running ${command}
4301.72She#
4311.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
4321.24Slukem#
4331.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
4341.22Slukem#				using ${name}_chroot.
4351.42Slukem#				Requires /usr to be mounted.
4361.24Slukem#
4371.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
4381.42Slukem#				Requires /usr to be mounted.
4391.24Slukem#
4401.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
4411.32Slukem#				to run the chrooted ${command} with.
4421.42Slukem#				Requires /usr to be mounted.
4431.24Slukem#
4441.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
4451.11Slukem#				Otherwise, use default command (see below)
4461.24Slukem#
4471.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
4481.46Slukem#				${rc_arg}_cmd method in the default
4491.46Slukem#				operation (i.e, after checking for required
4501.46Slukem#				bits and process (non)existence).
4511.11Slukem#				If this completes with a non-zero exit code,
4521.46Slukem#				don't run ${rc_arg}_cmd.
4531.24Slukem#
4541.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
4551.46Slukem#				${rc_arg}_cmd method, if that method
4561.43Slukem#				returned a zero exit code.
4571.43Slukem#
4581.11Slukem#	required_dirs	n	If set, check for the existence of the given
4591.11Slukem#				directories before running the default
4601.11Slukem#				(re)start command.
4611.24Slukem#
4621.11Slukem#	required_files	n	If set, check for the readability of the given
4631.11Slukem#				files before running the default (re)start
4641.11Slukem#				command.
4651.24Slukem#
4661.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
4671.11Slukem#				listed variables before running the default
4681.11Slukem#				(re)start command.
4691.11Slukem#
4701.46Slukem#	Default behaviour for a given argument, if no override method is
4711.46Slukem#	provided:
4721.24Slukem#
4731.46Slukem#	Argument	Default behaviour
4741.46Slukem#	--------	-----------------
4751.11Slukem#	start		if !running && checkyesno ${rcvar}
4761.11Slukem#				${command}
4771.24Slukem#
4781.11Slukem#	stop		if ${pidfile}
4791.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
4801.11Slukem#			else
4811.46Slukem#				rc_pid=$(check_process $command)
4821.46Slukem#			kill $sig_stop $rc_pid
4831.46Slukem#			wait_for_pids $rc_pid
4841.35Slukem#			($sig_stop defaults to TERM.)
4851.24Slukem#
4861.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
4871.35Slukem#			and doesn't wait_for_pids.
4881.11Slukem#			$sig_reload defaults to HUP.
4891.24Slukem#
4901.11Slukem#	restart		Run `stop' then `start'.
4911.11Slukem#
4921.33Slukem#	status		Show if ${command} is running, etc.
4931.33Slukem#
4941.33Slukem#	poll		Wait for ${command} to exit.
4951.33Slukem#
4961.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
4971.33Slukem#
4981.46Slukem#	Variables available to methods, and after run_rc_command() has
4991.46Slukem#	completed:
5001.46Slukem#
5011.46Slukem#	Variable	Purpose
5021.46Slukem#	--------	-------
5031.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
5041.46Slukem#			performed
5051.46Slukem#
5061.46Slukem#	rc_flags	Flags to start the default command with.
5071.46Slukem#			Defaults to ${name}_flags, unless overridden
5081.46Slukem#			by $flags from the environment.
5091.46Slukem#			This variable may be changed by the precmd method.
5101.46Slukem#
5111.46Slukem#	rc_pid		PID of command (if appropriate)
5121.46Slukem#
5131.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
5141.46Slukem#
5151.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
5161.33Slukem#
5171.24Slukem#
5181.11Slukemrun_rc_command()
5191.11Slukem{
5201.46Slukem	rc_arg=$1
5211.24Slukem	if [ -z "$name" ]; then
5221.30Slukem		err 3 'run_rc_command: $name is not set.'
5231.11Slukem	fi
5241.11Slukem
5251.61Slukem	_rc_prefix=
5261.46Slukem	case "$rc_arg" in
5271.24Slukem	fast*)				# "fast" prefix; don't check pid
5281.46Slukem		rc_arg=${rc_arg#fast}
5291.48Slukem		rc_fast=yes
5301.11Slukem		;;
5311.61Slukem	force*)				# "force" prefix; always run
5321.48Slukem		rc_force=yes
5331.61Slukem		_rc_prefix=force
5341.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
5351.61Slukem		if [ -n "${rcvar}" ]; then
5361.61Slukem			eval ${rcvar}=YES
5371.61Slukem		fi
5381.61Slukem		;;
5391.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
5401.61Slukem		_rc_prefix=one
5411.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
5421.29Slukem		if [ -n "${rcvar}" ]; then
5431.29Slukem			eval ${rcvar}=YES
5441.29Slukem		fi
5451.11Slukem		;;
5461.11Slukem	esac
5471.11Slukem
5481.75Sreed	_keywords="start stop restart rcvar"
5491.75Sreed	if [ -n "$extra_commands" ]; then
5501.75Sreed		_keywords="${_keywords} ${extra_commands}"
5511.75Sreed	fi
5521.46Slukem	rc_pid=
5531.11Slukem	_pidcmd=
5541.42Slukem	_procname=${procname:-${command}}
5551.42Slukem
5561.24Slukem					# setup pid check command if not fast
5571.103Skre	if [ -z "$rc_fast" ] && [ -n "$_procname" ]; then
5581.11Slukem		if [ -n "$pidfile" ]; then
5591.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
5601.42Slukem		else
5611.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
5621.11Slukem		fi
5631.11Slukem		if [ -n "$_pidcmd" ]; then
5641.33Slukem			_keywords="${_keywords} status poll"
5651.11Slukem		fi
5661.11Slukem	fi
5671.11Slukem
5681.46Slukem	if [ -z "$rc_arg" ]; then
5691.11Slukem		rc_usage "$_keywords"
5701.11Slukem	fi
5711.81Sjmmv	shift	# remove $rc_arg from the positional parameters
5721.11Slukem
5731.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
5741.46Slukem		rc_flags=$flags
5751.11Slukem	else
5761.46Slukem		eval rc_flags=\$${name}_flags
5771.11Slukem	fi
5781.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
5791.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
5801.72She	    _group=\$${name}_group	_groups=\$${name}_groups \
5811.72She	    _env=\"\$${name}_env\"
5821.11Slukem
5831.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
5841.42Slukem		if [ "$_user" = "$(id -un)" ]; then
5851.42Slukem			unset _user
5861.42Slukem		fi
5871.42Slukem	fi
5881.42Slukem
5891.29Slukem					# if ${rcvar} is set, and $1 is not
5901.40Slukem					# "rcvar", then run
5911.29Slukem					#	checkyesno ${rcvar}
5921.74Ssalo					# and return if that failed or warn
5931.74Ssalo					# user and exit when interactive
5941.24Slukem					#
5951.103Skre	if [ -n "${rcvar}" ] && [ "$rc_arg" != "rcvar" ]; then
5961.24Slukem		if ! checkyesno ${rcvar}; then
5971.74Ssalo					# check whether interactive or not
5981.74Ssalo			if [ -n "$_run_rc_script" ]; then
5991.74Ssalo				return 0
6001.74Ssalo			fi
6011.74Ssalo			for _elem in $_keywords; do
6021.74Ssalo				if [ "$_elem" = "$rc_arg" ]; then
6031.88Sapb					cat 1>&2 <<EOF
6041.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}.
6051.88SapbUse the following if you wish to perform the operation:
6061.88Sapb  $0 one${rc_arg}
6071.88SapbEOF
6081.74Ssalo					exit 1
6091.74Ssalo				fi
6101.74Ssalo			done
6111.74Ssalo			echo 1>&2 "$0: unknown directive '$rc_arg'."
6121.74Ssalo			rc_usage "$_keywords"
6131.24Slukem		fi
6141.24Slukem	fi
6151.24Slukem
6161.24Slukem	eval $_pidcmd			# determine the pid if necessary
6171.11Slukem
6181.11Slukem	for _elem in $_keywords; do
6191.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
6201.11Slukem			continue
6211.11Slukem		fi
6221.11Slukem
6231.24Slukem					# if there's a custom ${XXX_cmd},
6241.24Slukem					# run that instead of the default
6251.24Slukem					#
6261.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
6271.46Slukem		    _postcmd=\$${rc_arg}_postcmd
6281.11Slukem		if [ -n "$_cmd" ]; then
6291.24Slukem					# if the precmd failed and force
6301.24Slukem					# isn't set, exit
6311.24Slukem					#
6321.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
6331.24Slukem				return 1
6341.24Slukem			fi
6351.24Slukem
6361.81Sjmmv			if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then
6371.44Slukem				return 1
6381.44Slukem			fi
6391.44Slukem			eval $_postcmd
6401.11Slukem			return 0
6411.11Slukem		fi
6421.11Slukem
6431.81Sjmmv		if [ ${#} -gt 0 ]; then
6441.81Sjmmv			err 1 "the $rc_arg command does not take any parameters"
6451.81Sjmmv		fi
6461.81Sjmmv
6471.46Slukem		case "$rc_arg" in	# default operations...
6481.11Slukem
6491.11Slukem		status)
6501.46Slukem			if [ -n "$rc_pid" ]; then
6511.46Slukem				echo "${name} is running as pid $rc_pid."
6521.11Slukem			else
6531.11Slukem				echo "${name} is not running."
6541.28Slukem				return 1
6551.11Slukem			fi
6561.11Slukem			;;
6571.11Slukem
6581.11Slukem		start)
6591.46Slukem			if [ -n "$rc_pid" ]; then
6601.63Slukem				echo 1>&2 "${name} already running? (pid=$rc_pid)."
6611.11Slukem				exit 1
6621.11Slukem			fi
6631.11Slukem
6641.57Slukem			if [ ! -x ${_chroot}${command} ]; then
6651.11Slukem				return 0
6661.11Slukem			fi
6671.11Slukem
6681.24Slukem					# check for required variables,
6691.24Slukem					# directories, and files
6701.24Slukem					#
6711.11Slukem			for _f in $required_vars; do
6721.11Slukem				if ! checkyesno $_f; then
6731.65Slukem					warn "\$${_f} is not enabled."
6741.46Slukem					if [ -z "$rc_force" ]; then
6751.24Slukem						return 1
6761.24Slukem					fi
6771.11Slukem				fi
6781.11Slukem			done
6791.11Slukem			for _f in $required_dirs; do
6801.11Slukem				if [ ! -d "${_f}/." ]; then
6811.25Slukem					warn "${_f} is not a directory."
6821.46Slukem					if [ -z "$rc_force" ]; then
6831.24Slukem						return 1
6841.24Slukem					fi
6851.11Slukem				fi
6861.11Slukem			done
6871.11Slukem			for _f in $required_files; do
6881.11Slukem				if [ ! -r "${_f}" ]; then
6891.25Slukem					warn "${_f} is not readable."
6901.46Slukem					if [ -z "$rc_force" ]; then
6911.24Slukem						return 1
6921.24Slukem					fi
6931.11Slukem				fi
6941.11Slukem			done
6951.11Slukem
6961.24Slukem					# if the precmd failed and force
6971.24Slukem					# isn't set, exit
6981.24Slukem					#
6991.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7001.24Slukem				return 1
7011.24Slukem			fi
7021.24Slukem
7031.24Slukem					# setup the command to run, and run it
7041.29Slukem					#
7051.11Slukem			echo "Starting ${name}."
7061.22Slukem			if [ -n "$_chroot" ]; then
7071.22Slukem				_doit="\
7081.100Schristos$_env_clear_rc_vars $_env \
7091.23Slukem${_nice:+nice -n $_nice }\
7101.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
7111.46Slukem$_chroot $command $rc_flags $command_args"
7121.22Slukem			else
7131.22Slukem				_doit="\
7141.18Slukem${_chdir:+cd $_chdir; }\
7151.100Schristos$_env_clear_rc_vars $_env \
7161.18Slukem${_nice:+nice -n $_nice }\
7171.46Slukem$command $rc_flags $command_args"
7181.34Slukem				if [ -n "$_user" ]; then
7191.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
7201.34Slukem				fi
7211.22Slukem			fi
7221.43Slukem
7231.43Slukem					# if the cmd failed and force
7241.43Slukem					# isn't set, exit
7251.43Slukem					#
7261.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7271.43Slukem				return 1
7281.43Slukem			fi
7291.43Slukem
7301.43Slukem					# finally, run postcmd
7311.43Slukem					#
7321.43Slukem			eval $_postcmd
7331.11Slukem			;;
7341.11Slukem
7351.11Slukem		stop)
7361.46Slukem			if [ -z "$rc_pid" ]; then
7371.24Slukem				if [ -n "$pidfile" ]; then
7381.63Slukem					echo 1>&2 \
7391.24Slukem				    "${name} not running? (check $pidfile)."
7401.24Slukem				else
7411.63Slukem					echo 1>&2 "${name} not running?"
7421.11Slukem				fi
7431.24Slukem				exit 1
7441.11Slukem			fi
7451.11Slukem
7461.43Slukem					# if the precmd failed and force
7471.43Slukem					# isn't set, exit
7481.43Slukem					#
7491.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7501.24Slukem				return 1
7511.24Slukem			fi
7521.43Slukem
7531.43Slukem					# send the signal to stop
7541.43Slukem					#
7551.11Slukem			echo "Stopping ${name}."
7561.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
7571.34Slukem			if [ -n "$_user" ]; then
7581.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7591.34Slukem			fi
7601.43Slukem
7611.43Slukem					# if the stop cmd failed and force
7621.43Slukem					# isn't set, exit
7631.43Slukem					#
7641.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7651.43Slukem				return 1
7661.43Slukem			fi
7671.43Slukem
7681.43Slukem					# wait for the command to exit,
7691.43Slukem					# and run postcmd.
7701.46Slukem			wait_for_pids $rc_pid
7711.43Slukem			eval $_postcmd
7721.11Slukem			;;
7731.11Slukem
7741.11Slukem		reload)
7751.46Slukem			if [ -z "$rc_pid" ]; then
7761.24Slukem				if [ -n "$pidfile" ]; then
7771.63Slukem					echo 1>&2 \
7781.11Slukem				    "${name} not running? (check $pidfile)."
7791.24Slukem				else
7801.63Slukem					echo 1>&2 "${name} not running?"
7811.11Slukem				fi
7821.24Slukem				exit 1
7831.11Slukem			fi
7841.11Slukem			echo "Reloading ${name} config files."
7851.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7861.24Slukem				return 1
7871.24Slukem			fi
7881.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
7891.34Slukem			if [ -n "$_user" ]; then
7901.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
7911.34Slukem			fi
7921.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
7931.43Slukem				return 1
7941.43Slukem			fi
7951.43Slukem			eval $_postcmd
7961.11Slukem			;;
7971.11Slukem
7981.11Slukem		restart)
7991.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
8001.24Slukem				return 1
8011.11Slukem			fi
8021.29Slukem					# prevent restart being called more
8031.29Slukem					# than once by any given script
8041.29Slukem					#
8051.53Slukem			if ${_rc_restart_done:-false}; then
8061.29Slukem				return 0
8071.29Slukem			fi
8081.53Slukem			_rc_restart_done=true
8091.33Slukem
8101.61Slukem			( $0 ${_rc_prefix}stop )
8111.61Slukem			$0 ${_rc_prefix}start
8121.11Slukem
8131.43Slukem			eval $_postcmd
8141.33Slukem			;;
8151.33Slukem
8161.33Slukem		poll)
8171.46Slukem			if [ -n "$rc_pid" ]; then
8181.46Slukem				wait_for_pids $rc_pid
8191.33Slukem			fi
8201.11Slukem			;;
8211.11Slukem
8221.11Slukem		rcvar)
8231.11Slukem			echo "# $name"
8241.24Slukem			if [ -n "$rcvar" ]; then
8251.24Slukem				if checkyesno ${rcvar}; then
8261.106Suwe					echo "${rcvar}=YES"
8271.24Slukem				else
8281.106Suwe					echo "${rcvar}=NO"
8291.24Slukem				fi
8301.11Slukem			fi
8311.11Slukem			;;
8321.11Slukem
8331.11Slukem		*)
8341.11Slukem			rc_usage "$_keywords"
8351.11Slukem			;;
8361.11Slukem
8371.11Slukem		esac
8381.11Slukem		return 0
8391.11Slukem	done
8401.11Slukem
8411.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
8421.11Slukem	rc_usage "$_keywords"
8431.11Slukem	exit 1
8441.11Slukem}
8451.11Slukem
8461.11Slukem#
8471.94Sapb# _have_rc_postprocessor
8481.94Sapb#	Test whether the current script is running in a context that
8491.94Sapb#	was invoked from /etc/rc with a postprocessor.
8501.94Sapb#
8511.94Sapb#	If the test fails, some variables may be unset to make
8521.94Sapb#	such tests more efficient in future.
8531.94Sapb#
8541.94Sapb_have_rc_postprocessor()
8551.94Sapb{
8561.94Sapb	# Cheap tests that fd and pid are set, fd is writable.
8571.97Sphx	[ -n "${_rc_pid}" ] || { unset _rc_pid; return 1; }
8581.97Sphx	[ -n "${_rc_postprocessor_fd}" ] || { unset _rc_pid; return 1; }
8591.97Sphx	eval ": >&${_rc_postprocessor_fd}" 2>/dev/null \
8601.94Sapb	|| { unset _rc_pid; return 1; }
8611.94Sapb
8621.94Sapb	return 0
8631.94Sapb}
8641.94Sapb
8651.94Sapb#
8661.11Slukem# run_rc_script file arg
8671.11Slukem#	Start the script `file' with `arg', and correctly handle the
8681.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
8691.37Slukem#	sourced into the current environment.  If `file' appears to be
8701.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
8711.37Slukem#	executable run as a child process.
8721.17Slukem#
8731.78Sapb#	If `file' contains "KEYWORD: interactive" and if we are
8741.94Sapb#	running inside /etc/rc with postprocessing, then the script's
8751.94Sapb#	stdout and stderr are redirected to $_rc_original_stdout_fd and
8761.78Sapb#	$_rc_original_stderr_fd, so the output will be displayed on the
8771.78Sapb#	console but not intercepted by /etc/rc's postprocessor.
8781.78Sapb#
8791.11Slukemrun_rc_script()
8801.11Slukem{
8811.11Slukem	_file=$1
8821.11Slukem	_arg=$2
8831.103Skre	if [ -z "$_file" ] || [ -z "$_arg" ]; then
8841.11Slukem		err 3 'USAGE: run_rc_script file arg'
8851.11Slukem	fi
8861.11Slukem
8871.74Ssalo	_run_rc_script=true
8881.74Ssalo
8891.45Slukem	unset	name command command_args command_interpreter \
8901.45Slukem		extra_commands pidfile procname \
8911.42Slukem		rcvar required_dirs required_files required_vars
8921.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
8931.39Slukem
8941.78Sapb	_must_redirect=false
8951.94Sapb	if _have_rc_postprocessor \
8961.78Sapb	    && _has_rcorder_keyword interactive $_file
8971.78Sapb	then
8981.78Sapb		_must_redirect=true
8991.78Sapb	fi
9001.78Sapb
9011.39Slukem	case "$_file" in
9021.39Slukem	*.sh)				# run in current shell
9031.78Sapb		if $_must_redirect; then
9041.78Sapb			print_rc_metadata \
9051.78Sapb			    "note:Output from ${_file} is not logged"
9061.80Sapb			no_rc_postprocess eval \
9071.80Sapb			    'set $_arg ; . $_file'
9081.78Sapb		else
9091.78Sapb			set $_arg ; . $_file
9101.78Sapb		fi
9111.39Slukem		;;
9121.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
9131.39Slukem		warn "Ignoring scratch file $_file"
9141.39Slukem		;;
9151.39Slukem	*)				# run in subshell
9161.78Sapb		if [ -x $_file ] && $_must_redirect; then
9171.78Sapb			print_rc_metadata \
9181.78Sapb			    "note:Output from ${_file} is not logged"
9191.78Sapb			if [ -n "$rc_fast_and_loose" ]; then
9201.80Sapb				no_rc_postprocess eval \
9211.80Sapb				    'set $_arg ; . $_file'
9221.78Sapb			else
9231.80Sapb				no_rc_postprocess eval \
9241.80Sapb				    '( set $_arg ; . $_file )'
9251.78Sapb			fi
9261.78Sapb		elif [ -x $_file ]; then
9271.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
9281.39Slukem				set $_arg ; . $_file
9291.39Slukem			else
9301.37Slukem				( set $_arg ; . $_file )
9311.37Slukem			fi
9321.78Sapb		else
9331.78Sapb			warn "Ignoring non-executable file $_file"
9341.39Slukem		fi
9351.39Slukem		;;
9361.39Slukem	esac
9371.11Slukem}
9381.19Slukem
9391.19Slukem#
9401.65Slukem# load_rc_config command
9411.19Slukem#	Source in the configuration file for a given command.
9421.19Slukem#
9431.19Slukemload_rc_config()
9441.19Slukem{
9451.19Slukem	_command=$1
9461.19Slukem	if [ -z "$_command" ]; then
9471.19Slukem		err 3 'USAGE: load_rc_config command'
9481.19Slukem	fi
9491.19Slukem
9501.54Slukem	if ${_rc_conf_loaded:-false}; then
9511.54Slukem		:
9521.54Slukem	else
9531.30Slukem		. /etc/rc.conf
9541.53Slukem		_rc_conf_loaded=true
9551.30Slukem	fi
9561.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
9571.20Sfvdl		. /etc/rc.conf.d/"$_command"
9581.20Sfvdl	fi
9591.19Slukem}
9601.19Slukem
9611.65Slukem#
9621.65Slukem# load_rc_config_var cmd var
9631.65Slukem#	Read the rc.conf(5) var for cmd and set in the
9641.65Slukem#	current shell, using load_rc_config in a subshell to prevent
9651.65Slukem#	unwanted side effects from other variable assignments.
9661.65Slukem#
9671.65Slukemload_rc_config_var()
9681.65Slukem{
9691.65Slukem	if [ $# -ne 2 ]; then
9701.65Slukem		err 3 'USAGE: load_rc_config_var cmd var'
9711.65Slukem	fi
9721.65Slukem	eval $(eval '(
9731.65Slukem		load_rc_config '$1' >/dev/null;
9741.103Skre		if [ -n "${'$2'}" ] || [ "${'$2'-UNSET}" != "UNSET" ]; then
9751.65Slukem			echo '$2'=\'\''${'$2'}\'\'';
9761.65Slukem		fi
9771.65Slukem	)' )
9781.65Slukem}
9791.11Slukem
9801.11Slukem#
9811.11Slukem# rc_usage commands
9821.11Slukem#	Print a usage string for $0, with `commands' being a list of
9831.11Slukem#	valid commands.
9841.11Slukem#
9851.11Slukemrc_usage()
9861.11Slukem{
9871.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
9881.11Slukem
9891.11Slukem	_sep=
9901.56Schristos	for _elem; do
9911.11Slukem		echo -n 1>&2 "$_sep$_elem"
9921.11Slukem		_sep="|"
9931.11Slukem	done
9941.11Slukem	echo 1>&2 ")"
9951.11Slukem	exit 1
9961.11Slukem}
9971.11Slukem
9981.11Slukem#
9991.11Slukem# err exitval message
10001.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
10011.11Slukem#
10021.11Slukemerr()
10031.11Slukem{
10041.11Slukem	exitval=$1
10051.11Slukem	shift
10061.11Slukem
10071.51Sgrant	if [ -x /usr/bin/logger ]; then
10081.51Sgrant		logger "$0: ERROR: $*"
10091.51Sgrant	fi
10101.21Slukem	echo 1>&2 "$0: ERROR: $*"
10111.11Slukem	exit $exitval
10121.11Slukem}
10131.11Slukem
10141.11Slukem#
10151.11Slukem# warn message
10161.11Slukem#	Display message to stderr and log to the syslog.
10171.11Slukem#
10181.11Slukemwarn()
10191.11Slukem{
10201.51Sgrant	if [ -x /usr/bin/logger ]; then
10211.51Sgrant		logger "$0: WARNING: $*"
10221.51Sgrant	fi
10231.21Slukem	echo 1>&2 "$0: WARNING: $*"
10241.31Satatat}
10251.31Satatat
10261.31Satatat#
10271.31Satatat# backup_file action file cur backup
10281.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
10291.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
10301.31Satatat#
10311.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
10321.31Satatat#	which can be either YES or NO.
10331.31Satatat#
10341.31Satatat#	The `action' keyword can be one of the following:
10351.31Satatat#
10361.31Satatat#	add		`file' is now being backed up (and is possibly
10371.31Satatat#			being reentered into the backups system).  `cur'
10381.31Satatat#			is created and RCS files, if necessary, are
10391.31Satatat#			created as well.
10401.31Satatat#
10411.31Satatat#	update		`file' has changed and needs to be backed up.
10421.31Satatat#			If `cur' exists, it is copied to to `back' or
10431.31Satatat#			checked into RCS (if the repository file is old),
10441.31Satatat#			and then `file' is copied to `cur'.  Another RCS
10451.31Satatat#			check in done here if RCS is being used.
10461.31Satatat#
10471.31Satatat#	remove		`file' is no longer being tracked by the backups
10481.31Satatat#			system.  If RCS is not being used, `cur' is moved
10491.31Satatat#			to `back', otherwise an empty file is checked in,
10501.31Satatat#			and then `cur' is removed.
10511.31Satatat#
10521.31Satatat#
10531.31Satatatbackup_file()
10541.31Satatat{
10551.31Satatat	_action=$1
10561.31Satatat	_file=$2
10571.31Satatat	_cur=$3
10581.31Satatat	_back=$4
10591.31Satatat
10601.31Satatat	if checkyesno backup_uses_rcs; then
10611.31Satatat		_msg0="backup archive"
10621.31Satatat		_msg1="update"
10631.31Satatat
10641.36Satatat		# ensure that history file is not locked
10651.36Satatat		if [ -f $_cur,v ]; then
10661.36Satatat			rcs -q -u -U -M $_cur
10671.36Satatat		fi
10681.36Satatat
10691.31Satatat		# ensure after switching to rcs that the
10701.31Satatat		# current backup is not lost
10711.31Satatat		if [ -f $_cur ]; then
10721.31Satatat			# no archive, or current newer than archive
10731.103Skre			if [ ! -f $_cur,v ] || [ $_cur -nt $_cur,v ]; then
10741.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10751.36Satatat				rcs -q -kb -U $_cur
10761.49Slukem				co -q -f -u $_cur
10771.31Satatat			fi
10781.31Satatat		fi
10791.31Satatat
10801.31Satatat		case $_action in
10811.31Satatat		add|update)
10821.31Satatat			cp -p $_file $_cur
10831.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10841.36Satatat			rcs -q -kb -U $_cur
10851.49Slukem			co -q -f -u $_cur
10861.31Satatat			chown root:wheel $_cur $_cur,v
10871.31Satatat			;;
10881.31Satatat		remove)
10891.31Satatat			cp /dev/null $_cur
10901.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
10911.36Satatat			rcs -q -kb -U $_cur
10921.31Satatat			chown root:wheel $_cur $_cur,v
10931.31Satatat			rm $_cur
10941.31Satatat			;;
10951.31Satatat		esac
10961.31Satatat	else
10971.31Satatat		case $_action in
10981.31Satatat		add|update)
10991.31Satatat			if [ -f $_cur ]; then
11001.31Satatat				cp -p $_cur $_back
11011.31Satatat			fi
11021.31Satatat			cp -p $_file $_cur
11031.31Satatat			chown root:wheel $_cur
11041.31Satatat			;;
11051.31Satatat		remove)
11061.31Satatat			mv -f $_cur $_back
11071.31Satatat			;;
11081.31Satatat		esac
11091.31Satatat	fi
11101.1Scjs}
11111.64Smycroft
11121.76Schristos#
11131.76Schristos# handle_fsck_error fsck_exit_code
11141.76Schristos#	Take action depending on the return code from fsck.
11151.76Schristos#
11161.76Schristoshandle_fsck_error()
11171.76Schristos{
11181.76Schristos	case $1 in
11191.76Schristos	0)	# OK
11201.76Schristos		return
11211.76Schristos		;;
11221.76Schristos	2)	# Needs re-run, still fs errors
11231.76Schristos		echo "File system still has errors; re-run fsck manually!"
11241.76Schristos		;;
11251.76Schristos	4)	# Root modified
11261.93Swiz		echo "Root file system was modified, rebooting ..."
11271.76Schristos		reboot -n
11281.76Schristos		echo "Reboot failed; help!"
11291.76Schristos		;;
11301.76Schristos	8)	# Check failed
11311.76Schristos		echo "Automatic file system check failed; help!"
11321.76Schristos		;;
11331.76Schristos	12)	# Got signal
11341.76Schristos		echo "Boot interrupted."
11351.76Schristos		;;
11361.76Schristos	*)
11371.76Schristos		echo "Unknown error $1; help!"
11381.76Schristos		;;
11391.76Schristos	esac
11401.76Schristos	stop_boot
11411.76Schristos}
11421.76Schristos
11431.78Sapb#
11441.78Sapb# _has_rcorder_keyword word file
11451.78Sapb#	Check whether a file contains a "# KEYWORD:" comment with a
11461.78Sapb#	specified keyword in the style used by rcorder(8).
11471.78Sapb#
11481.78Sapb_has_rcorder_keyword()
11491.78Sapb{
11501.78Sapb	local word="$1"
11511.78Sapb	local file="$2"
11521.78Sapb	local line
11531.78Sapb
11541.78Sapb	[ -r "$file" ] || return 1
11551.78Sapb	while read line; do
11561.78Sapb		case "${line} " in
11571.78Sapb		"# KEYWORD:"*[\ \	]"${word}"[\ \	]*)
11581.78Sapb			return 0
11591.78Sapb			;;
11601.78Sapb		"#"*)
11611.78Sapb			continue
11621.78Sapb			;;
11631.78Sapb		*[A-Za-z0-9]*)
11641.78Sapb			# give up at the first non-empty non-comment line
11651.78Sapb			return 1
11661.78Sapb			;;
11671.78Sapb		esac
11681.78Sapb	done <"$file"
11691.78Sapb	return 1
11701.78Sapb}
11711.78Sapb
11721.78Sapb#
11731.78Sapb# print_rc_metadata string
11741.78Sapb#	Print the specified string in such a way that the post-processor
11751.78Sapb#	inside /etc/rc will treat it as meta-data.
11761.78Sapb#
11771.78Sapb#	If we are not running inside /etc/rc, do nothing.
11781.78Sapb#
11791.78Sapb#	For public use by any rc.d script, the string must begin with
11801.78Sapb#	"note:", followed by arbitrary text.  The intent is that the text
11811.78Sapb#	will appear in a log file but not on the console.
11821.78Sapb#
11831.78Sapb#	For private use within /etc/rc, the string must contain a
11841.78Sapb#	keyword recognised by the rc_postprocess_metadata() function
11851.78Sapb#	defined in /etc/rc, followed by a colon, followed by one or more
11861.78Sapb#	colon-separated arguments associated with the keyword.
11871.78Sapb#
11881.78Sapbprint_rc_metadata()
11891.78Sapb{
11901.78Sapb	# _rc_postprocessor fd, if defined, is the fd to which we must
11911.78Sapb	# print, prefixing the output with $_rc_metadata_prefix.
11921.78Sapb	#
11931.94Sapb	if _have_rc_postprocessor; then
11941.88Sapb		command printf "%s%s\n" "$rc_metadata_prefix" "$1" \
11951.78Sapb			>&${_rc_postprocessor_fd}
11961.78Sapb	fi
11971.78Sapb}
11981.78Sapb
11991.78Sapb#
12001.88Sapb# _flush_rc_output
12011.88Sapb#	Arrange for output to be flushed, if we are running
12021.88Sapb#	inside /etc/rc with postprocessing.
12031.88Sapb#
12041.88Sapb_flush_rc_output()
12051.88Sapb{
12061.88Sapb	print_rc_metadata "nop"
12071.88Sapb}
12081.88Sapb
12091.88Sapb#
12101.88Sapb# print_rc_normal [-n] string
12111.78Sapb#	Print the specified string in such way that it is treated as
12121.78Sapb#	normal output, regardless of whether or not we are running
12131.78Sapb#	inside /etc/rc with post-processing.
12141.78Sapb#
12151.88Sapb#	If "-n" is specified in $1, then the string in $2 is printed
12161.88Sapb#	without a newline; otherwise, the string in $1 is printed
12171.88Sapb#	with a newline.
12181.88Sapb#
12191.88Sapb#	Intended use cases include:
12201.88Sapb#
12211.88Sapb#	o   An rc.d script can use ``print_rc_normal -n'' to print a
12221.88Sapb#	    partial line in such a way that it appears immediately
12231.88Sapb#	    instead of being buffered by rc(8)'s post-processor.
12241.88Sapb#
12251.88Sapb#	o   An rc.d script that is run via the no_rc_postprocess
12261.88Sapb#	    function (so most of its output is invisible to rc(8)'s
12271.88Sapb#	    post-processor) can use print_rc_normal to force some of its
12281.88Sapb#	    output to be seen by the post-processor.
12291.88Sapb#
12301.78Sapb#
12311.78Sapbprint_rc_normal()
12321.78Sapb{
12331.94Sapb	# print to stdout or _rc_postprocessor_fd, depending on
12341.94Sapb	# whether not we have an rc postprocessor.
12351.78Sapb	#
12361.94Sapb	local fd=1
12371.94Sapb	_have_rc_postprocessor && fd="${_rc_postprocessor_fd}"
12381.88Sapb	case "$1" in
12391.88Sapb	"-n")
12401.88Sapb		command printf "%s" "$2" >&${fd}
12411.88Sapb		_flush_rc_output
12421.88Sapb		;;
12431.88Sapb	*)
12441.88Sapb		command printf "%s\n" "$1" >&${fd}
12451.88Sapb		;;
12461.88Sapb	esac
12471.78Sapb}
12481.78Sapb
12491.78Sapb#
12501.78Sapb# no_rc_postprocess cmd...
12511.78Sapb#	Execute the specified command in such a way that its output
12521.78Sapb#	bypasses the post-processor that handles the output from
12531.78Sapb#	most commands that are run inside /etc/rc.  If we are not
12541.78Sapb#	inside /etc/rc, then just execute the command without special
12551.78Sapb#	treatment.
12561.78Sapb#
12571.78Sapb#	The intent is that interactive commands can be run via
12581.78Sapb#	no_rc_postprocess(), and their output will apear immediately
12591.78Sapb#	on the console instead of being hidden or delayed by the
12601.78Sapb#	post-processor.	 An unfortunate consequence of the output
12611.78Sapb#	bypassing the post-processor is that the output will not be
12621.78Sapb#	logged.
12631.78Sapb#
12641.78Sapbno_rc_postprocess()
12651.78Sapb{
12661.94Sapb	if _have_rc_postprocessor; then
12671.78Sapb		"$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
12681.78Sapb	else
12691.78Sapb		"$@"
12701.78Sapb	fi
12711.78Sapb}
12721.78Sapb
12731.78Sapb#
12741.78Sapb# twiddle
12751.78Sapb#	On each call, print a different one of "/", "-", "\\", "|",
12761.78Sapb#	followed by a backspace.  The most recently printed value is
12771.78Sapb#	saved in $_twiddle_state.
12781.78Sapb#
12791.78Sapb#	Output is to /dev/tty, so this function may be useful even inside
12801.78Sapb#	a script whose output is redirected.
12811.78Sapb#
12821.78Sapbtwiddle()
12831.78Sapb{
12841.78Sapb	case "$_twiddle_state" in
12851.78Sapb	'/')	_next='-' ;;
12861.78Sapb	'-')	_next='\' ;;
12871.78Sapb	'\')	_next='|' ;;
12881.78Sapb	*)	_next='/' ;;
12891.78Sapb	esac
12901.88Sapb	command printf "%s\b" "$_next" >/dev/tty
12911.78Sapb	_twiddle_state="$_next"
12921.78Sapb}
12931.78Sapb
12941.82Schristos#
12951.82Schristos# human_exit_code
12961.82Schristos#	Print the a human version of the exit code.
12971.82Schristos#
12981.82Schristoshuman_exit_code()
12991.82Schristos{
13001.83Schristos	if [ "$1" -lt 127 ]
13011.82Schristos	then
13021.82Schristos		echo "exited with code $1"
13031.85Schristos	elif [ "$(expr $1 % 256)" -eq 127 ]
13041.82Schristos	then
13051.84Schristos		# This cannot really happen because the shell will not
13061.84Schristos		# pass stopped job status out and the exit code is limited
13071.84Schristos		# to 8 bits. This code is here just for completeness.
13081.82Schristos		echo "stopped with signal $(expr $1 / 256)"
13091.82Schristos	else
13101.82Schristos		echo "terminated with signal $(expr $1 - 128)"
13111.82Schristos	fi
13121.82Schristos}
13131.86Sapb
13141.86Sapb#
13151.86Sapb# collapse_backslash_newline
13161.86Sapb#	Copy input to output, collapsing <backslash><newline>
13171.86Sapb#	to nothing, but leaving other backslashes alone.
13181.86Sapb#
13191.86Sapbcollapse_backslash_newline()
13201.86Sapb{
13211.86Sapb	local line
13221.86Sapb	while read -r line ; do
13231.86Sapb		case "$line" in
13241.86Sapb		*\\)
13251.86Sapb			# print it, without the backslash or newline
13261.88Sapb			command printf "%s" "${line%?}"
13271.86Sapb			;;
13281.86Sapb		*)
13291.86Sapb			# print it, with a newline
13301.88Sapb			command printf "%s\n" "${line}"
13311.86Sapb			;;
13321.86Sapb		esac
13331.86Sapb	done
13341.86Sapb}
13351.82Schristos
13361.92Sapb# Shell implementations of basename and dirname, usable before
13371.92Sapb# the /usr file system is mounted.
13381.92Sapb#
13391.92Sapbbasename()
13401.92Sapb{
13411.92Sapb	local file="$1"
13421.92Sapb	local suffix="$2"
13431.92Sapb	local base
13441.92Sapb
13451.92Sapb	base="${file##*/}"		# remove up to and including last '/'
13461.92Sapb	base="${base%${suffix}}"	# remove suffix, if any
13471.92Sapb	command printf "%s\n" "${base}"
13481.92Sapb}
13491.92Sapb
13501.92Sapbdirname()
13511.92Sapb{
13521.92Sapb	local file="$1"
13531.92Sapb	local dir
13541.92Sapb
13551.92Sapb	case "$file" in
13561.92Sapb	/*/*)	dir="${file%/*}" ;;	# common case: absolute path
13571.92Sapb	/*)	dir="/" ;;		# special case: name in root dir
13581.92Sapb	*/*)	dir="${file%/*}" ;;	# common case: relative path with '/'
13591.92Sapb	*)	dir="." ;;		# special case: name without '/'
13601.92Sapb	esac
13611.92Sapb	command printf "%s\n" "${dir}"
13621.92Sapb}
13631.92Sapb
13641.88Sapb# Override the normal "echo" and "printf" commands, so that
13651.88Sapb# partial lines printed by rc.d scripts appear immediately,
13661.88Sapb# instead of being buffered by rc(8)'s post-processor.
13671.88Sapb#
13681.88Sapb# Naive use of the echo or printf commands from rc.d scripts,
13691.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr,
13701.88Sapb# will call these functions.  To call the real echo and printf
13711.88Sapb# commands, use "command echo" or "command printf".
13721.88Sapb#
13731.103Skre# Avoid use of echo altogether as much as possible, printf works better
13741.103Skre#
13751.88Sapbecho()
13761.88Sapb{
13771.103Skre	local IFS=' ' NL='\n'	# not a literal newline...
13781.103Skre
13791.88Sapb	case "$1" in
13801.103Skre	-n)	NL=; shift;;
13811.88Sapb	esac
13821.103Skre
13831.103Skre	command printf "%s${NL}" "$*"
13841.103Skre
13851.103Skre	if test -z "${NL}"
13861.103Skre	then
13871.103Skre		_flush_rc_output
13881.103Skre	fi
13891.103Skre	return 0
13901.88Sapb}
13911.103Skre
13921.88Sapbprintf()
13931.88Sapb{
13941.88Sapb	command printf "$@"
13951.88Sapb	case "$1" in
13961.88Sapb	*'\n')	: ;;
13971.88Sapb	*)	_flush_rc_output ;;
13981.88Sapb	esac
13991.103Skre	return 0
14001.88Sapb}
14011.88Sapb
14021.98Schristoskat() {
14031.98Schristos	local i
14041.98Schristos	local v
14051.98Schristos	for i; do
14061.98Schristos		while read -r v; do
14071.98Schristos			v="${v%%#*}"
14081.98Schristos			if [ -z "$v" ]; then
14091.98Schristos				continue
14101.98Schristos			fi
14111.98Schristos			echo "$v"
14121.98Schristos		done < "$i"
14131.98Schristos	done
14141.98Schristos}
14151.98Schristos
14161.64Smycroft_rc_subr_loaded=:
1417