rc.subr revision 1.108
11.108Salnsn# $NetBSD: rc.subr,v 1.108 2022/02/03 20:52:44 alnsn 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.108Salnsn# mount_critical_filesystems_zfs
2061.108Salnsn#	Go through the list of critical ZFS mountpoints as provided in
2071.108Salnsn#	the rc.conf(5) variable $critical_filesystems_zfs, checking
2081.108Salnsn#	each one to see if it is mounted, and if it is not, mounting it.
2091.108Salnsn#	It's not an error if file systems prefixed with "OPTIONAL:"
2101.108Salnsn#	aren't zfs mountpoints.
2111.108Salnsnmount_critical_filesystems_zfs()
2121.108Salnsn{
2131.108Salnsn	eval _fslist=\$critical_filesystems_zfs
2141.108Salnsn	_tab="	"
2151.108Salnsn	_mountcrit_es=0
2161.108Salnsn	for _fs in $_fslist; do
2171.108Salnsn		_optional=false
2181.108Salnsn		case "$_fs" in
2191.108Salnsn		OPTIONAL:*)
2201.108Salnsn			_optional=true
2211.108Salnsn			_fs="${_fs#*:}"
2221.108Salnsn			;;
2231.108Salnsn		esac
2241.108Salnsn
2251.108Salnsn		_dataset=`
2261.108Salnsn			zfs list -H -o mountpoint,name |
2271.108Salnsn			while read _line ; do
2281.108Salnsn				_dataset=''
2291.108Salnsn				case "$_line" in
2301.108Salnsn				"${_fs}${_tab}"*)
2311.108Salnsn					_dataset="${_line#*${_tab}}"
2321.108Salnsn					;;
2331.108Salnsn				esac
2341.108Salnsn				if [ -n "$_dataset" ]; then
2351.108Salnsn					case "$( zfs get -H -o value canmount $_dataset )" in
2361.108Salnsn					on)
2371.108Salnsn						echo -n "$_dataset"
2381.108Salnsn						break ;;
2391.108Salnsn					*) # noauto|off - dataset isn't supposed to be mounted
2401.108Salnsn						;;
2411.108Salnsn					esac
2421.108Salnsn				fi
2431.108Salnsn			done`
2441.108Salnsn
2451.108Salnsn		if [ -z "$_dataset" ]; then
2461.108Salnsn			if $_optional; then
2471.108Salnsn				# ignore this error
2481.108Salnsn				print_rc_metadata \
2491.108Salnsn				"note:Optional file system $_fs is not present"
2501.108Salnsn			else
2511.108Salnsn				printf >&2 "%s\n" "No suitable ZFS dataset found for mountpoint $_fs"
2521.108Salnsn				_mountcrit_es=1
2531.108Salnsn			fi
2541.108Salnsn		else
2551.108Salnsn			_mount_es=
2561.108Salnsn			case "$( zfs get -H -o value mounted $_dataset )" in
2571.108Salnsn			yes)
2581.108Salnsn				_mount_es=1
2591.108Salnsn				print_rc_metadata \
2601.108Salnsn				"note:File system $_fs was already mounted"
2611.108Salnsn				;;
2621.108Salnsn			esac
2631.108Salnsn
2641.108Salnsn			if [ -z "$_mount_es" ]; then
2651.108Salnsn				zfs mount "$_dataset" >/dev/null
2661.108Salnsn				_mount_es=$?
2671.108Salnsn			fi
2681.108Salnsn
2691.108Salnsn			if [ -n "$_mount_es" ]; then
2701.108Salnsn				_mountcrit_es="$_mount_es"
2711.108Salnsn			fi
2721.108Salnsn		fi
2731.108Salnsn	done
2741.108Salnsn	return $_mountcrit_es
2751.108Salnsn}
2761.108Salnsn
2771.108Salnsn#
2781.45Slukem# check_pidfile pidfile procname [interpreter]
2791.45Slukem#	Parses the first line of pidfile for a PID, and ensures
2801.11Slukem#	that the process is running and matches procname.
2811.45Slukem#	Prints the matching PID upon success, nothing otherwise.
2821.45Slukem#	interpreter is optional; see _find_processes() for details.
2831.11Slukem#
2841.11Slukemcheck_pidfile()
2851.11Slukem{
2861.11Slukem	_pidfile=$1
2871.11Slukem	_procname=$2
2881.45Slukem	_interpreter=$3
2891.103Skre	if [ -z "$_pidfile" ] || [ -z "$_procname" ]; then
2901.45Slukem		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
2911.11Slukem	fi
2921.11Slukem	if [ ! -f $_pidfile ]; then
2931.11Slukem		return
2941.11Slukem	fi
2951.11Slukem	read _pid _junk < $_pidfile
2961.11Slukem	if [ -z "$_pid" ]; then
2971.11Slukem		return
2981.11Slukem	fi
2991.45Slukem	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
3001.11Slukem}
3011.11Slukem
3021.11Slukem#
3031.45Slukem# check_process procname [interpreter]
3041.11Slukem#	Ensures that a process (or processes) named procname is running.
3051.45Slukem#	Prints a list of matching PIDs.
3061.45Slukem#	interpreter is optional; see _find_processes() for details.
3071.11Slukem#
3081.11Slukemcheck_process()
3091.11Slukem{
3101.11Slukem	_procname=$1
3111.45Slukem	_interpreter=$2
3121.11Slukem	if [ -z "$_procname" ]; then
3131.45Slukem		err 3 'USAGE: check_process procname [interpreter]'
3141.45Slukem	fi
3151.101Skre	_find_processes $_procname ${_interpreter:-.} '-A'
3161.45Slukem}
3171.45Slukem
3181.46Slukem#
3191.45Slukem# _find_processes procname interpreter psargs
3201.45Slukem#	Search for procname in the output of ps generated by psargs.
3211.45Slukem#	Prints the PIDs of any matching processes, space separated.
3221.45Slukem#
3231.45Slukem#	If interpreter == ".", check the following variations of procname
3241.45Slukem#	against the first word of each command:
3251.45Slukem#		procname
3261.45Slukem#		`basename procname`
3271.45Slukem#		`basename procname` + ":"
3281.45Slukem#		"(" + `basename procname` + ")"
3291.45Slukem#
3301.45Slukem#	If interpreter != ".", read the first line of procname, remove the
3311.45Slukem#	leading #!, normalise whitespace, append procname, and attempt to
3321.45Slukem#	match that against each command, either as is, or with extra words
3331.73Sdholland#	at the end.  As an alternative, to deal with interpreted daemons
3341.66She#	using perl, the basename of the interpreter plus a colon is also
3351.66She#	tried as the prefix to procname.
3361.45Slukem#
3371.45Slukem_find_processes()
3381.45Slukem{
3391.45Slukem	if [ $# -ne 3 ]; then
3401.45Slukem		err 3 'USAGE: _find_processes procname interpreter psargs'
3411.11Slukem	fi
3421.45Slukem	_procname=$1
3431.45Slukem	_interpreter=$2
3441.45Slukem	_psargs=$3
3451.45Slukem
3461.11Slukem	_pref=
3471.68Shubertf	_procnamebn=${_procname##*/}
3481.45Slukem	if [ $_interpreter != "." ]; then	# an interpreted script
3491.67Selad		read _interp < ${_chroot:-}/$_procname	# read interpreter name
3501.45Slukem		_interp=${_interp#\#!}		# strip #!
3511.45Slukem		set -- $_interp
3521.87Schristos		if [ $1 = "/usr/bin/env" ]; then
3531.87Schristos			shift
3541.87Schristos			set -- $(type $1)
3551.87Schristos			shift $(($# - 1))
3561.87Schristos			_interp="${1##*/} $_procname"
3571.87Schristos		else
3581.87Schristos			_interp="$* $_procname"
3591.87Schristos		fi
3601.45Slukem		if [ $_interpreter != $1 ]; then
3611.45Slukem			warn "\$command_interpreter $_interpreter != $1"
3621.45Slukem		fi
3631.66She		_interpbn=${1##*/}
3641.45Slukem		_fp_args='_argv'
3651.45Slukem		_fp_match='case "$_argv" in
3661.68Shubertf		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
3671.45Slukem	else					# a normal daemon
3681.45Slukem		_fp_args='_arg0 _argv'
3691.45Slukem		_fp_match='case "$_arg0" in
3701.45Slukem		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
3711.45Slukem	fi
3721.45Slukem
3731.45Slukem	_proccheck='
3741.102Schristos		ps -o "pid,args" '"$_psargs"' 2>&1 |
3751.45Slukem		while read _npid '"$_fp_args"'; do
3761.45Slukem			case "$_npid" in
3771.102Schristos			ps:|PID)
3781.45Slukem				continue ;;
3791.45Slukem			esac ; '"$_fp_match"'
3801.45Slukem				echo -n "$_pref$_npid" ;
3811.45Slukem				_pref=" "
3821.45Slukem				;;
3831.45Slukem			esac
3841.45Slukem		done'
3851.45Slukem
3861.45Slukem#echo 1>&2 "proccheck is :$_proccheck:"
3871.45Slukem	eval $_proccheck
3881.11Slukem}
3891.11Slukem
3901.11Slukem#
3911.104Schristos# kill_pids signal pid [pid ...]
3921.104Schristos#	kills the given pids with signal. 
3931.104Schristos#	returns the list of pids killed successfully.
3941.104Schristos#
3951.104Schristoskill_pids()
3961.104Schristos{
3971.104Schristos	local signal=$1
3981.104Schristos	shift
3991.105Sotis	local list="$*"
4001.104Schristos	local j=
4011.104Schristos	local nlist=
4021.104Schristos	for j in $list; do
4031.104Schristos		if kill -$signal $j 2>/dev/null; then
4041.104Schristos			nlist="${nlist}${nlist:+ }$j"
4051.104Schristos		fi
4061.104Schristos	done
4071.104Schristos	echo $nlist
4081.104Schristos}
4091.104Schristos
4101.104Schristos#
4111.33Slukem# wait_for_pids pid [pid ...]
4121.35Slukem#	spins until none of the pids exist
4131.104Schristos#	if _rc_kill_ntries is set and exceeded, it SIGKILLS the remaining
4141.104Schristos#	pids
4151.33Slukem#
4161.33Slukemwait_for_pids()
4171.33Slukem{
4181.104Schristos	local ntries=0
4191.104Schristos	local prefix=
4201.105Sotis	local nlist=
4211.105Sotis	local list="$*"
4221.104Schristos
4231.104Schristos	if [ -z "$list" ]; then
4241.33Slukem		return
4251.33Slukem	fi
4261.104Schristos
4271.35Slukem	while true; do
4281.105Sotis		nlist=$(kill_pids 0 $list)
4291.104Schristos		if [ -z "$nlist" ]; then
4301.33Slukem			break
4311.33Slukem		fi
4321.104Schristos		if [ "$list" != "$nlist" ]; then
4331.104Schristos			list=$nlist
4341.104Schristos			echo -n ${prefix:-"Waiting for PIDS: "}$list
4351.104Schristos			prefix=", "
4361.96Sroy		fi
4371.96Sroy		# We want this to be a tight loop for a fast exit
4381.96Sroy		sleep 0.05
4391.104Schristos		ntries=$((ntries + 1))
4401.104Schristos		if [ -n "${_rc_kill_ntries}" ]; then
4411.104Schristos			if [ ${ntries} -gt ${_rc_kill_ntries} ]; then
4421.104Schristos				kill_pids 9 $list > /dev/null
4431.104Schristos			fi
4441.104Schristos		fi
4451.33Slukem	done
4461.104Schristos	if [ -n "$prefix" ]; then
4471.35Slukem		echo "."
4481.35Slukem	fi
4491.33Slukem}
4501.33Slukem
4511.33Slukem#
4521.81Sjmmv# run_rc_command argument [parameters]
4531.46Slukem#	Search for argument in the list of supported commands, which is:
4541.33Slukem#		"start stop restart rcvar status poll ${extra_commands}"
4551.46Slukem#	If there's a match, run ${argument}_cmd or the default method
4561.81Sjmmv#	(see below), and pass the optional list of parameters to it.
4571.11Slukem#
4581.46Slukem#	If argument has a given prefix, then change the operation as follows:
4591.46Slukem#		Prefix	Operation
4601.11Slukem#		------	---------
4611.48Slukem#		fast	Skip the pid check, and set rc_fast=yes
4621.48Slukem#		force	Set ${rcvar} to YES, and set rc_force=yes
4631.61Slukem#		one	Set ${rcvar} to YES
4641.11Slukem#
4651.11Slukem#	The following globals are used:
4661.24Slukem#
4671.46Slukem#	Name		Needed	Purpose
4681.46Slukem#	----		------	-------
4691.11Slukem#	name		y	Name of script.
4701.24Slukem#
4711.11Slukem#	command		n	Full path to command.
4721.46Slukem#				Not needed if ${rc_arg}_cmd is set for
4731.11Slukem#				each keyword.
4741.24Slukem#
4751.11Slukem#	command_args	n	Optional args/shell directives for command.
4761.24Slukem#
4771.45Slukem#	command_interpreter n	If not empty, command is interpreted, so
4781.45Slukem#				call check_{pidfile,process}() appropriately.
4791.45Slukem#
4801.16Slukem#	extra_commands	n	List of extra commands supported.
4811.24Slukem#
4821.42Slukem#	pidfile		n	If set, use check_pidfile $pidfile $command,
4831.42Slukem#				otherwise use check_process $command.
4841.42Slukem#				In either case, only check if $command is set.
4851.42Slukem#
4861.42Slukem#	procname	n	Process name to check for instead of $command.
4871.24Slukem#
4881.24Slukem#	rcvar		n	This is checked with checkyesno to determine
4891.24Slukem#				if the action should be run.
4901.24Slukem#
4911.22Slukem#	${name}_chroot	n	Directory to chroot to before running ${command}
4921.42Slukem#				Requires /usr to be mounted.
4931.24Slukem#
4941.22Slukem#	${name}_chdir	n	Directory to cd to before running ${command}
4951.22Slukem#				(if not using ${name}_chroot).
4961.24Slukem#
4971.11Slukem#	${name}_flags	n	Arguments to call ${command} with.
4981.24Slukem#				NOTE:	$flags from the parent environment
4991.24Slukem#					can be used to override this.
5001.24Slukem#
5011.72She#	${name}_env	n	Additional environment variable settings
5021.72She#				for running ${command}
5031.72She#
5041.23Slukem#	${name}_nice	n	Nice level to run ${command} at.
5051.24Slukem#
5061.22Slukem#	${name}_user	n	User to run ${command} as, using su(1) if not
5071.22Slukem#				using ${name}_chroot.
5081.42Slukem#				Requires /usr to be mounted.
5091.24Slukem#
5101.22Slukem#	${name}_group	n	Group to run chrooted ${command} as.
5111.42Slukem#				Requires /usr to be mounted.
5121.24Slukem#
5131.32Slukem#	${name}_groups	n	Comma separated list of supplementary groups
5141.32Slukem#				to run the chrooted ${command} with.
5151.42Slukem#				Requires /usr to be mounted.
5161.24Slukem#
5171.46Slukem#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
5181.11Slukem#				Otherwise, use default command (see below)
5191.24Slukem#
5201.46Slukem#	${rc_arg}_precmd n	If set, run just before performing the
5211.46Slukem#				${rc_arg}_cmd method in the default
5221.46Slukem#				operation (i.e, after checking for required
5231.46Slukem#				bits and process (non)existence).
5241.11Slukem#				If this completes with a non-zero exit code,
5251.46Slukem#				don't run ${rc_arg}_cmd.
5261.24Slukem#
5271.46Slukem#	${rc_arg}_postcmd n	If set, run just after performing the
5281.46Slukem#				${rc_arg}_cmd method, if that method
5291.43Slukem#				returned a zero exit code.
5301.43Slukem#
5311.11Slukem#	required_dirs	n	If set, check for the existence of the given
5321.11Slukem#				directories before running the default
5331.11Slukem#				(re)start command.
5341.24Slukem#
5351.11Slukem#	required_files	n	If set, check for the readability of the given
5361.11Slukem#				files before running the default (re)start
5371.11Slukem#				command.
5381.24Slukem#
5391.11Slukem#	required_vars	n	If set, perform checkyesno on each of the
5401.11Slukem#				listed variables before running the default
5411.11Slukem#				(re)start command.
5421.11Slukem#
5431.46Slukem#	Default behaviour for a given argument, if no override method is
5441.46Slukem#	provided:
5451.24Slukem#
5461.46Slukem#	Argument	Default behaviour
5471.46Slukem#	--------	-----------------
5481.11Slukem#	start		if !running && checkyesno ${rcvar}
5491.11Slukem#				${command}
5501.24Slukem#
5511.11Slukem#	stop		if ${pidfile}
5521.46Slukem#				rc_pid=$(check_pidfile $pidfile $command)
5531.11Slukem#			else
5541.46Slukem#				rc_pid=$(check_process $command)
5551.46Slukem#			kill $sig_stop $rc_pid
5561.46Slukem#			wait_for_pids $rc_pid
5571.35Slukem#			($sig_stop defaults to TERM.)
5581.24Slukem#
5591.35Slukem#	reload		Similar to stop, except use $sig_reload instead,
5601.35Slukem#			and doesn't wait_for_pids.
5611.11Slukem#			$sig_reload defaults to HUP.
5621.24Slukem#
5631.11Slukem#	restart		Run `stop' then `start'.
5641.11Slukem#
5651.33Slukem#	status		Show if ${command} is running, etc.
5661.33Slukem#
5671.33Slukem#	poll		Wait for ${command} to exit.
5681.33Slukem#
5691.33Slukem#	rcvar		Display what rc.conf variable is used (if any).
5701.33Slukem#
5711.46Slukem#	Variables available to methods, and after run_rc_command() has
5721.46Slukem#	completed:
5731.46Slukem#
5741.46Slukem#	Variable	Purpose
5751.46Slukem#	--------	-------
5761.61Slukem#	rc_arg		Argument to command, after fast/force/one processing
5771.46Slukem#			performed
5781.46Slukem#
5791.46Slukem#	rc_flags	Flags to start the default command with.
5801.46Slukem#			Defaults to ${name}_flags, unless overridden
5811.46Slukem#			by $flags from the environment.
5821.46Slukem#			This variable may be changed by the precmd method.
5831.46Slukem#
5841.46Slukem#	rc_pid		PID of command (if appropriate)
5851.46Slukem#
5861.46Slukem#	rc_fast		Not empty if "fast" was provided (q.v.)
5871.46Slukem#
5881.46Slukem#	rc_force	Not empty if "force" was provided (q.v.)
5891.33Slukem#
5901.24Slukem#
5911.11Slukemrun_rc_command()
5921.11Slukem{
5931.46Slukem	rc_arg=$1
5941.24Slukem	if [ -z "$name" ]; then
5951.30Slukem		err 3 'run_rc_command: $name is not set.'
5961.11Slukem	fi
5971.11Slukem
5981.61Slukem	_rc_prefix=
5991.46Slukem	case "$rc_arg" in
6001.24Slukem	fast*)				# "fast" prefix; don't check pid
6011.46Slukem		rc_arg=${rc_arg#fast}
6021.48Slukem		rc_fast=yes
6031.11Slukem		;;
6041.61Slukem	force*)				# "force" prefix; always run
6051.48Slukem		rc_force=yes
6061.61Slukem		_rc_prefix=force
6071.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
6081.61Slukem		if [ -n "${rcvar}" ]; then
6091.61Slukem			eval ${rcvar}=YES
6101.61Slukem		fi
6111.61Slukem		;;
6121.61Slukem	one*)				# "one" prefix; set ${rcvar}=yes
6131.61Slukem		_rc_prefix=one
6141.61Slukem		rc_arg=${rc_arg#${_rc_prefix}}
6151.29Slukem		if [ -n "${rcvar}" ]; then
6161.29Slukem			eval ${rcvar}=YES
6171.29Slukem		fi
6181.11Slukem		;;
6191.11Slukem	esac
6201.11Slukem
6211.75Sreed	_keywords="start stop restart rcvar"
6221.75Sreed	if [ -n "$extra_commands" ]; then
6231.75Sreed		_keywords="${_keywords} ${extra_commands}"
6241.75Sreed	fi
6251.46Slukem	rc_pid=
6261.11Slukem	_pidcmd=
6271.42Slukem	_procname=${procname:-${command}}
6281.42Slukem
6291.24Slukem					# setup pid check command if not fast
6301.103Skre	if [ -z "$rc_fast" ] && [ -n "$_procname" ]; then
6311.11Slukem		if [ -n "$pidfile" ]; then
6321.46Slukem			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
6331.42Slukem		else
6341.46Slukem			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
6351.11Slukem		fi
6361.11Slukem		if [ -n "$_pidcmd" ]; then
6371.33Slukem			_keywords="${_keywords} status poll"
6381.11Slukem		fi
6391.11Slukem	fi
6401.11Slukem
6411.46Slukem	if [ -z "$rc_arg" ]; then
6421.11Slukem		rc_usage "$_keywords"
6431.11Slukem	fi
6441.81Sjmmv	shift	# remove $rc_arg from the positional parameters
6451.11Slukem
6461.17Slukem	if [ -n "$flags" ]; then	# allow override from environment
6471.46Slukem		rc_flags=$flags
6481.11Slukem	else
6491.46Slukem		eval rc_flags=\$${name}_flags
6501.11Slukem	fi
6511.30Slukem	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
6521.30Slukem	    _nice=\$${name}_nice	_user=\$${name}_user \
6531.72She	    _group=\$${name}_group	_groups=\$${name}_groups \
6541.72She	    _env=\"\$${name}_env\"
6551.11Slukem
6561.42Slukem	if [ -n "$_user" ]; then	# unset $_user if running as that user
6571.42Slukem		if [ "$_user" = "$(id -un)" ]; then
6581.42Slukem			unset _user
6591.42Slukem		fi
6601.42Slukem	fi
6611.42Slukem
6621.29Slukem					# if ${rcvar} is set, and $1 is not
6631.40Slukem					# "rcvar", then run
6641.29Slukem					#	checkyesno ${rcvar}
6651.74Ssalo					# and return if that failed or warn
6661.74Ssalo					# user and exit when interactive
6671.24Slukem					#
6681.103Skre	if [ -n "${rcvar}" ] && [ "$rc_arg" != "rcvar" ]; then
6691.24Slukem		if ! checkyesno ${rcvar}; then
6701.74Ssalo					# check whether interactive or not
6711.74Ssalo			if [ -n "$_run_rc_script" ]; then
6721.74Ssalo				return 0
6731.74Ssalo			fi
6741.74Ssalo			for _elem in $_keywords; do
6751.74Ssalo				if [ "$_elem" = "$rc_arg" ]; then
6761.88Sapb					cat 1>&2 <<EOF
6771.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}.
6781.88SapbUse the following if you wish to perform the operation:
6791.88Sapb  $0 one${rc_arg}
6801.88SapbEOF
6811.74Ssalo					exit 1
6821.74Ssalo				fi
6831.74Ssalo			done
6841.74Ssalo			echo 1>&2 "$0: unknown directive '$rc_arg'."
6851.74Ssalo			rc_usage "$_keywords"
6861.24Slukem		fi
6871.24Slukem	fi
6881.24Slukem
6891.24Slukem	eval $_pidcmd			# determine the pid if necessary
6901.11Slukem
6911.11Slukem	for _elem in $_keywords; do
6921.46Slukem		if [ "$_elem" != "$rc_arg" ]; then
6931.11Slukem			continue
6941.11Slukem		fi
6951.11Slukem
6961.24Slukem					# if there's a custom ${XXX_cmd},
6971.24Slukem					# run that instead of the default
6981.24Slukem					#
6991.46Slukem		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
7001.46Slukem		    _postcmd=\$${rc_arg}_postcmd
7011.11Slukem		if [ -n "$_cmd" ]; then
7021.24Slukem					# if the precmd failed and force
7031.24Slukem					# isn't set, exit
7041.24Slukem					#
7051.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7061.24Slukem				return 1
7071.24Slukem			fi
7081.24Slukem
7091.81Sjmmv			if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then
7101.44Slukem				return 1
7111.44Slukem			fi
7121.44Slukem			eval $_postcmd
7131.11Slukem			return 0
7141.11Slukem		fi
7151.11Slukem
7161.81Sjmmv		if [ ${#} -gt 0 ]; then
7171.81Sjmmv			err 1 "the $rc_arg command does not take any parameters"
7181.81Sjmmv		fi
7191.81Sjmmv
7201.46Slukem		case "$rc_arg" in	# default operations...
7211.11Slukem
7221.11Slukem		status)
7231.46Slukem			if [ -n "$rc_pid" ]; then
7241.46Slukem				echo "${name} is running as pid $rc_pid."
7251.11Slukem			else
7261.11Slukem				echo "${name} is not running."
7271.28Slukem				return 1
7281.11Slukem			fi
7291.11Slukem			;;
7301.11Slukem
7311.11Slukem		start)
7321.46Slukem			if [ -n "$rc_pid" ]; then
7331.63Slukem				echo 1>&2 "${name} already running? (pid=$rc_pid)."
7341.11Slukem				exit 1
7351.11Slukem			fi
7361.11Slukem
7371.57Slukem			if [ ! -x ${_chroot}${command} ]; then
7381.11Slukem				return 0
7391.11Slukem			fi
7401.11Slukem
7411.24Slukem					# check for required variables,
7421.24Slukem					# directories, and files
7431.24Slukem					#
7441.11Slukem			for _f in $required_vars; do
7451.11Slukem				if ! checkyesno $_f; then
7461.65Slukem					warn "\$${_f} is not enabled."
7471.46Slukem					if [ -z "$rc_force" ]; then
7481.24Slukem						return 1
7491.24Slukem					fi
7501.11Slukem				fi
7511.11Slukem			done
7521.11Slukem			for _f in $required_dirs; do
7531.11Slukem				if [ ! -d "${_f}/." ]; then
7541.25Slukem					warn "${_f} is not a directory."
7551.46Slukem					if [ -z "$rc_force" ]; then
7561.24Slukem						return 1
7571.24Slukem					fi
7581.11Slukem				fi
7591.11Slukem			done
7601.11Slukem			for _f in $required_files; do
7611.11Slukem				if [ ! -r "${_f}" ]; then
7621.25Slukem					warn "${_f} is not readable."
7631.46Slukem					if [ -z "$rc_force" ]; then
7641.24Slukem						return 1
7651.24Slukem					fi
7661.11Slukem				fi
7671.11Slukem			done
7681.11Slukem
7691.24Slukem					# if the precmd failed and force
7701.24Slukem					# isn't set, exit
7711.24Slukem					#
7721.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
7731.24Slukem				return 1
7741.24Slukem			fi
7751.24Slukem
7761.24Slukem					# setup the command to run, and run it
7771.29Slukem					#
7781.11Slukem			echo "Starting ${name}."
7791.22Slukem			if [ -n "$_chroot" ]; then
7801.22Slukem				_doit="\
7811.100Schristos$_env_clear_rc_vars $_env \
7821.23Slukem${_nice:+nice -n $_nice }\
7831.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
7841.46Slukem$_chroot $command $rc_flags $command_args"
7851.22Slukem			else
7861.22Slukem				_doit="\
7871.18Slukem${_chdir:+cd $_chdir; }\
7881.100Schristos$_env_clear_rc_vars $_env \
7891.18Slukem${_nice:+nice -n $_nice }\
7901.46Slukem$command $rc_flags $command_args"
7911.34Slukem				if [ -n "$_user" ]; then
7921.34Slukem				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
7931.34Slukem				fi
7941.22Slukem			fi
7951.43Slukem
7961.43Slukem					# if the cmd failed and force
7971.43Slukem					# isn't set, exit
7981.43Slukem					#
7991.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
8001.43Slukem				return 1
8011.43Slukem			fi
8021.43Slukem
8031.43Slukem					# finally, run postcmd
8041.43Slukem					#
8051.43Slukem			eval $_postcmd
8061.11Slukem			;;
8071.11Slukem
8081.11Slukem		stop)
8091.46Slukem			if [ -z "$rc_pid" ]; then
8101.24Slukem				if [ -n "$pidfile" ]; then
8111.63Slukem					echo 1>&2 \
8121.24Slukem				    "${name} not running? (check $pidfile)."
8131.24Slukem				else
8141.63Slukem					echo 1>&2 "${name} not running?"
8151.11Slukem				fi
8161.24Slukem				exit 1
8171.11Slukem			fi
8181.11Slukem
8191.43Slukem					# if the precmd failed and force
8201.43Slukem					# isn't set, exit
8211.43Slukem					#
8221.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
8231.24Slukem				return 1
8241.24Slukem			fi
8251.43Slukem
8261.43Slukem					# send the signal to stop
8271.43Slukem					#
8281.11Slukem			echo "Stopping ${name}."
8291.46Slukem			_doit="kill -${sig_stop:-TERM} $rc_pid"
8301.34Slukem			if [ -n "$_user" ]; then
8311.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
8321.34Slukem			fi
8331.43Slukem
8341.43Slukem					# if the stop cmd failed and force
8351.43Slukem					# isn't set, exit
8361.43Slukem					#
8371.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
8381.43Slukem				return 1
8391.43Slukem			fi
8401.43Slukem
8411.43Slukem					# wait for the command to exit,
8421.43Slukem					# and run postcmd.
8431.46Slukem			wait_for_pids $rc_pid
8441.43Slukem			eval $_postcmd
8451.11Slukem			;;
8461.11Slukem
8471.11Slukem		reload)
8481.46Slukem			if [ -z "$rc_pid" ]; then
8491.24Slukem				if [ -n "$pidfile" ]; then
8501.63Slukem					echo 1>&2 \
8511.11Slukem				    "${name} not running? (check $pidfile)."
8521.24Slukem				else
8531.63Slukem					echo 1>&2 "${name} not running?"
8541.11Slukem				fi
8551.24Slukem				exit 1
8561.11Slukem			fi
8571.11Slukem			echo "Reloading ${name} config files."
8581.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
8591.24Slukem				return 1
8601.24Slukem			fi
8611.46Slukem			_doit="kill -${sig_reload:-HUP} $rc_pid"
8621.34Slukem			if [ -n "$_user" ]; then
8631.34Slukem				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
8641.34Slukem			fi
8651.46Slukem			if ! eval $_doit && [ -z "$rc_force" ]; then
8661.43Slukem				return 1
8671.43Slukem			fi
8681.43Slukem			eval $_postcmd
8691.11Slukem			;;
8701.11Slukem
8711.11Slukem		restart)
8721.46Slukem			if ! eval $_precmd && [ -z "$rc_force" ]; then
8731.24Slukem				return 1
8741.11Slukem			fi
8751.29Slukem					# prevent restart being called more
8761.29Slukem					# than once by any given script
8771.29Slukem					#
8781.53Slukem			if ${_rc_restart_done:-false}; then
8791.29Slukem				return 0
8801.29Slukem			fi
8811.53Slukem			_rc_restart_done=true
8821.33Slukem
8831.61Slukem			( $0 ${_rc_prefix}stop )
8841.61Slukem			$0 ${_rc_prefix}start
8851.11Slukem
8861.43Slukem			eval $_postcmd
8871.33Slukem			;;
8881.33Slukem
8891.33Slukem		poll)
8901.46Slukem			if [ -n "$rc_pid" ]; then
8911.46Slukem				wait_for_pids $rc_pid
8921.33Slukem			fi
8931.11Slukem			;;
8941.11Slukem
8951.11Slukem		rcvar)
8961.11Slukem			echo "# $name"
8971.24Slukem			if [ -n "$rcvar" ]; then
8981.24Slukem				if checkyesno ${rcvar}; then
8991.106Suwe					echo "${rcvar}=YES"
9001.24Slukem				else
9011.106Suwe					echo "${rcvar}=NO"
9021.24Slukem				fi
9031.11Slukem			fi
9041.11Slukem			;;
9051.11Slukem
9061.11Slukem		*)
9071.11Slukem			rc_usage "$_keywords"
9081.11Slukem			;;
9091.11Slukem
9101.11Slukem		esac
9111.11Slukem		return 0
9121.11Slukem	done
9131.11Slukem
9141.46Slukem	echo 1>&2 "$0: unknown directive '$rc_arg'."
9151.11Slukem	rc_usage "$_keywords"
9161.11Slukem	exit 1
9171.11Slukem}
9181.11Slukem
9191.11Slukem#
9201.94Sapb# _have_rc_postprocessor
9211.94Sapb#	Test whether the current script is running in a context that
9221.94Sapb#	was invoked from /etc/rc with a postprocessor.
9231.94Sapb#
9241.94Sapb#	If the test fails, some variables may be unset to make
9251.94Sapb#	such tests more efficient in future.
9261.94Sapb#
9271.94Sapb_have_rc_postprocessor()
9281.94Sapb{
9291.94Sapb	# Cheap tests that fd and pid are set, fd is writable.
9301.97Sphx	[ -n "${_rc_pid}" ] || { unset _rc_pid; return 1; }
9311.97Sphx	[ -n "${_rc_postprocessor_fd}" ] || { unset _rc_pid; return 1; }
9321.97Sphx	eval ": >&${_rc_postprocessor_fd}" 2>/dev/null \
9331.94Sapb	|| { unset _rc_pid; return 1; }
9341.94Sapb
9351.94Sapb	return 0
9361.94Sapb}
9371.94Sapb
9381.94Sapb#
9391.11Slukem# run_rc_script file arg
9401.11Slukem#	Start the script `file' with `arg', and correctly handle the
9411.17Slukem#	return value from the script.  If `file' ends with `.sh', it's
9421.37Slukem#	sourced into the current environment.  If `file' appears to be
9431.37Slukem#	a backup or scratch file, ignore it.  Otherwise if it's
9441.37Slukem#	executable run as a child process.
9451.17Slukem#
9461.78Sapb#	If `file' contains "KEYWORD: interactive" and if we are
9471.94Sapb#	running inside /etc/rc with postprocessing, then the script's
9481.94Sapb#	stdout and stderr are redirected to $_rc_original_stdout_fd and
9491.78Sapb#	$_rc_original_stderr_fd, so the output will be displayed on the
9501.78Sapb#	console but not intercepted by /etc/rc's postprocessor.
9511.78Sapb#
9521.11Slukemrun_rc_script()
9531.11Slukem{
9541.11Slukem	_file=$1
9551.11Slukem	_arg=$2
9561.103Skre	if [ -z "$_file" ] || [ -z "$_arg" ]; then
9571.11Slukem		err 3 'USAGE: run_rc_script file arg'
9581.11Slukem	fi
9591.11Slukem
9601.74Ssalo	_run_rc_script=true
9611.74Ssalo
9621.45Slukem	unset	name command command_args command_interpreter \
9631.45Slukem		extra_commands pidfile procname \
9641.42Slukem		rcvar required_dirs required_files required_vars
9651.43Slukem	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
9661.39Slukem
9671.78Sapb	_must_redirect=false
9681.94Sapb	if _have_rc_postprocessor \
9691.78Sapb	    && _has_rcorder_keyword interactive $_file
9701.78Sapb	then
9711.78Sapb		_must_redirect=true
9721.78Sapb	fi
9731.78Sapb
9741.39Slukem	case "$_file" in
9751.39Slukem	*.sh)				# run in current shell
9761.78Sapb		if $_must_redirect; then
9771.78Sapb			print_rc_metadata \
9781.78Sapb			    "note:Output from ${_file} is not logged"
9791.80Sapb			no_rc_postprocess eval \
9801.80Sapb			    'set $_arg ; . $_file'
9811.78Sapb		else
9821.78Sapb			set $_arg ; . $_file
9831.78Sapb		fi
9841.39Slukem		;;
9851.60Slukem	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
9861.39Slukem		warn "Ignoring scratch file $_file"
9871.39Slukem		;;
9881.39Slukem	*)				# run in subshell
9891.78Sapb		if [ -x $_file ] && $_must_redirect; then
9901.78Sapb			print_rc_metadata \
9911.78Sapb			    "note:Output from ${_file} is not logged"
9921.78Sapb			if [ -n "$rc_fast_and_loose" ]; then
9931.80Sapb				no_rc_postprocess eval \
9941.80Sapb				    'set $_arg ; . $_file'
9951.78Sapb			else
9961.80Sapb				no_rc_postprocess eval \
9971.80Sapb				    '( set $_arg ; . $_file )'
9981.78Sapb			fi
9991.78Sapb		elif [ -x $_file ]; then
10001.39Slukem			if [ -n "$rc_fast_and_loose" ]; then
10011.39Slukem				set $_arg ; . $_file
10021.39Slukem			else
10031.37Slukem				( set $_arg ; . $_file )
10041.37Slukem			fi
10051.78Sapb		else
10061.78Sapb			warn "Ignoring non-executable file $_file"
10071.39Slukem		fi
10081.39Slukem		;;
10091.39Slukem	esac
10101.11Slukem}
10111.19Slukem
10121.19Slukem#
10131.65Slukem# load_rc_config command
10141.19Slukem#	Source in the configuration file for a given command.
10151.19Slukem#
10161.19Slukemload_rc_config()
10171.19Slukem{
10181.19Slukem	_command=$1
10191.19Slukem	if [ -z "$_command" ]; then
10201.19Slukem		err 3 'USAGE: load_rc_config command'
10211.19Slukem	fi
10221.19Slukem
10231.54Slukem	if ${_rc_conf_loaded:-false}; then
10241.54Slukem		:
10251.54Slukem	else
10261.30Slukem		. /etc/rc.conf
10271.53Slukem		_rc_conf_loaded=true
10281.30Slukem	fi
10291.20Sfvdl	if [ -f /etc/rc.conf.d/"$_command" ]; then
10301.20Sfvdl		. /etc/rc.conf.d/"$_command"
10311.20Sfvdl	fi
10321.19Slukem}
10331.19Slukem
10341.65Slukem#
10351.65Slukem# load_rc_config_var cmd var
10361.65Slukem#	Read the rc.conf(5) var for cmd and set in the
10371.65Slukem#	current shell, using load_rc_config in a subshell to prevent
10381.65Slukem#	unwanted side effects from other variable assignments.
10391.65Slukem#
10401.65Slukemload_rc_config_var()
10411.65Slukem{
10421.65Slukem	if [ $# -ne 2 ]; then
10431.65Slukem		err 3 'USAGE: load_rc_config_var cmd var'
10441.65Slukem	fi
10451.65Slukem	eval $(eval '(
10461.65Slukem		load_rc_config '$1' >/dev/null;
10471.103Skre		if [ -n "${'$2'}" ] || [ "${'$2'-UNSET}" != "UNSET" ]; then
10481.65Slukem			echo '$2'=\'\''${'$2'}\'\'';
10491.65Slukem		fi
10501.65Slukem	)' )
10511.65Slukem}
10521.11Slukem
10531.11Slukem#
10541.11Slukem# rc_usage commands
10551.11Slukem#	Print a usage string for $0, with `commands' being a list of
10561.11Slukem#	valid commands.
10571.11Slukem#
10581.11Slukemrc_usage()
10591.11Slukem{
10601.61Slukem	echo -n 1>&2 "Usage: $0 [fast|force|one]("
10611.11Slukem
10621.11Slukem	_sep=
10631.56Schristos	for _elem; do
10641.11Slukem		echo -n 1>&2 "$_sep$_elem"
10651.11Slukem		_sep="|"
10661.11Slukem	done
10671.11Slukem	echo 1>&2 ")"
10681.11Slukem	exit 1
10691.11Slukem}
10701.11Slukem
10711.11Slukem#
10721.11Slukem# err exitval message
10731.11Slukem#	Display message to stderr and log to the syslog, and exit with exitval.
10741.11Slukem#
10751.11Slukemerr()
10761.11Slukem{
10771.11Slukem	exitval=$1
10781.11Slukem	shift
10791.11Slukem
10801.51Sgrant	if [ -x /usr/bin/logger ]; then
10811.51Sgrant		logger "$0: ERROR: $*"
10821.51Sgrant	fi
10831.21Slukem	echo 1>&2 "$0: ERROR: $*"
10841.11Slukem	exit $exitval
10851.11Slukem}
10861.11Slukem
10871.11Slukem#
10881.11Slukem# warn message
10891.11Slukem#	Display message to stderr and log to the syslog.
10901.11Slukem#
10911.11Slukemwarn()
10921.11Slukem{
10931.51Sgrant	if [ -x /usr/bin/logger ]; then
10941.51Sgrant		logger "$0: WARNING: $*"
10951.51Sgrant	fi
10961.21Slukem	echo 1>&2 "$0: WARNING: $*"
10971.31Satatat}
10981.31Satatat
10991.31Satatat#
11001.31Satatat# backup_file action file cur backup
11011.31Satatat#	Make a backup copy of `file' into `cur', and save the previous
11021.31Satatat#	version of `cur' as `backup' or use rcs for archiving.
11031.31Satatat#
11041.31Satatat#	This routine checks the value of the backup_uses_rcs variable,
11051.31Satatat#	which can be either YES or NO.
11061.31Satatat#
11071.31Satatat#	The `action' keyword can be one of the following:
11081.31Satatat#
11091.31Satatat#	add		`file' is now being backed up (and is possibly
11101.31Satatat#			being reentered into the backups system).  `cur'
11111.31Satatat#			is created and RCS files, if necessary, are
11121.31Satatat#			created as well.
11131.31Satatat#
11141.31Satatat#	update		`file' has changed and needs to be backed up.
11151.31Satatat#			If `cur' exists, it is copied to to `back' or
11161.31Satatat#			checked into RCS (if the repository file is old),
11171.31Satatat#			and then `file' is copied to `cur'.  Another RCS
11181.31Satatat#			check in done here if RCS is being used.
11191.31Satatat#
11201.31Satatat#	remove		`file' is no longer being tracked by the backups
11211.31Satatat#			system.  If RCS is not being used, `cur' is moved
11221.31Satatat#			to `back', otherwise an empty file is checked in,
11231.31Satatat#			and then `cur' is removed.
11241.31Satatat#
11251.31Satatat#
11261.31Satatatbackup_file()
11271.31Satatat{
11281.31Satatat	_action=$1
11291.31Satatat	_file=$2
11301.31Satatat	_cur=$3
11311.31Satatat	_back=$4
11321.31Satatat
11331.31Satatat	if checkyesno backup_uses_rcs; then
11341.31Satatat		_msg0="backup archive"
11351.31Satatat		_msg1="update"
11361.31Satatat
11371.36Satatat		# ensure that history file is not locked
11381.36Satatat		if [ -f $_cur,v ]; then
11391.36Satatat			rcs -q -u -U -M $_cur
11401.36Satatat		fi
11411.36Satatat
11421.31Satatat		# ensure after switching to rcs that the
11431.31Satatat		# current backup is not lost
11441.31Satatat		if [ -f $_cur ]; then
11451.31Satatat			# no archive, or current newer than archive
11461.103Skre			if [ ! -f $_cur,v ] || [ $_cur -nt $_cur,v ]; then
11471.36Satatat				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
11481.36Satatat				rcs -q -kb -U $_cur
11491.49Slukem				co -q -f -u $_cur
11501.31Satatat			fi
11511.31Satatat		fi
11521.31Satatat
11531.31Satatat		case $_action in
11541.31Satatat		add|update)
11551.31Satatat			cp -p $_file $_cur
11561.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
11571.36Satatat			rcs -q -kb -U $_cur
11581.49Slukem			co -q -f -u $_cur
11591.31Satatat			chown root:wheel $_cur $_cur,v
11601.31Satatat			;;
11611.31Satatat		remove)
11621.31Satatat			cp /dev/null $_cur
11631.36Satatat			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
11641.36Satatat			rcs -q -kb -U $_cur
11651.31Satatat			chown root:wheel $_cur $_cur,v
11661.31Satatat			rm $_cur
11671.31Satatat			;;
11681.31Satatat		esac
11691.31Satatat	else
11701.31Satatat		case $_action in
11711.31Satatat		add|update)
11721.31Satatat			if [ -f $_cur ]; then
11731.31Satatat				cp -p $_cur $_back
11741.31Satatat			fi
11751.31Satatat			cp -p $_file $_cur
11761.31Satatat			chown root:wheel $_cur
11771.31Satatat			;;
11781.31Satatat		remove)
11791.31Satatat			mv -f $_cur $_back
11801.31Satatat			;;
11811.31Satatat		esac
11821.31Satatat	fi
11831.1Scjs}
11841.64Smycroft
11851.76Schristos#
11861.76Schristos# handle_fsck_error fsck_exit_code
11871.76Schristos#	Take action depending on the return code from fsck.
11881.76Schristos#
11891.76Schristoshandle_fsck_error()
11901.76Schristos{
11911.76Schristos	case $1 in
11921.76Schristos	0)	# OK
11931.76Schristos		return
11941.76Schristos		;;
11951.76Schristos	2)	# Needs re-run, still fs errors
11961.76Schristos		echo "File system still has errors; re-run fsck manually!"
11971.76Schristos		;;
11981.76Schristos	4)	# Root modified
11991.93Swiz		echo "Root file system was modified, rebooting ..."
12001.76Schristos		reboot -n
12011.76Schristos		echo "Reboot failed; help!"
12021.76Schristos		;;
12031.76Schristos	8)	# Check failed
12041.76Schristos		echo "Automatic file system check failed; help!"
12051.76Schristos		;;
12061.76Schristos	12)	# Got signal
12071.76Schristos		echo "Boot interrupted."
12081.76Schristos		;;
12091.76Schristos	*)
12101.76Schristos		echo "Unknown error $1; help!"
12111.76Schristos		;;
12121.76Schristos	esac
12131.76Schristos	stop_boot
12141.76Schristos}
12151.76Schristos
12161.78Sapb#
12171.78Sapb# _has_rcorder_keyword word file
12181.78Sapb#	Check whether a file contains a "# KEYWORD:" comment with a
12191.78Sapb#	specified keyword in the style used by rcorder(8).
12201.78Sapb#
12211.78Sapb_has_rcorder_keyword()
12221.78Sapb{
12231.78Sapb	local word="$1"
12241.78Sapb	local file="$2"
12251.78Sapb	local line
12261.78Sapb
12271.78Sapb	[ -r "$file" ] || return 1
12281.78Sapb	while read line; do
12291.78Sapb		case "${line} " in
12301.78Sapb		"# KEYWORD:"*[\ \	]"${word}"[\ \	]*)
12311.78Sapb			return 0
12321.78Sapb			;;
12331.78Sapb		"#"*)
12341.78Sapb			continue
12351.78Sapb			;;
12361.78Sapb		*[A-Za-z0-9]*)
12371.78Sapb			# give up at the first non-empty non-comment line
12381.78Sapb			return 1
12391.78Sapb			;;
12401.78Sapb		esac
12411.78Sapb	done <"$file"
12421.78Sapb	return 1
12431.78Sapb}
12441.78Sapb
12451.78Sapb#
12461.78Sapb# print_rc_metadata string
12471.78Sapb#	Print the specified string in such a way that the post-processor
12481.78Sapb#	inside /etc/rc will treat it as meta-data.
12491.78Sapb#
12501.78Sapb#	If we are not running inside /etc/rc, do nothing.
12511.78Sapb#
12521.78Sapb#	For public use by any rc.d script, the string must begin with
12531.78Sapb#	"note:", followed by arbitrary text.  The intent is that the text
12541.78Sapb#	will appear in a log file but not on the console.
12551.78Sapb#
12561.78Sapb#	For private use within /etc/rc, the string must contain a
12571.78Sapb#	keyword recognised by the rc_postprocess_metadata() function
12581.78Sapb#	defined in /etc/rc, followed by a colon, followed by one or more
12591.78Sapb#	colon-separated arguments associated with the keyword.
12601.78Sapb#
12611.78Sapbprint_rc_metadata()
12621.78Sapb{
12631.78Sapb	# _rc_postprocessor fd, if defined, is the fd to which we must
12641.78Sapb	# print, prefixing the output with $_rc_metadata_prefix.
12651.78Sapb	#
12661.94Sapb	if _have_rc_postprocessor; then
12671.88Sapb		command printf "%s%s\n" "$rc_metadata_prefix" "$1" \
12681.78Sapb			>&${_rc_postprocessor_fd}
12691.78Sapb	fi
12701.78Sapb}
12711.78Sapb
12721.78Sapb#
12731.88Sapb# _flush_rc_output
12741.88Sapb#	Arrange for output to be flushed, if we are running
12751.88Sapb#	inside /etc/rc with postprocessing.
12761.88Sapb#
12771.88Sapb_flush_rc_output()
12781.88Sapb{
12791.88Sapb	print_rc_metadata "nop"
12801.88Sapb}
12811.88Sapb
12821.88Sapb#
12831.88Sapb# print_rc_normal [-n] string
12841.78Sapb#	Print the specified string in such way that it is treated as
12851.78Sapb#	normal output, regardless of whether or not we are running
12861.78Sapb#	inside /etc/rc with post-processing.
12871.78Sapb#
12881.88Sapb#	If "-n" is specified in $1, then the string in $2 is printed
12891.88Sapb#	without a newline; otherwise, the string in $1 is printed
12901.88Sapb#	with a newline.
12911.88Sapb#
12921.88Sapb#	Intended use cases include:
12931.88Sapb#
12941.88Sapb#	o   An rc.d script can use ``print_rc_normal -n'' to print a
12951.88Sapb#	    partial line in such a way that it appears immediately
12961.88Sapb#	    instead of being buffered by rc(8)'s post-processor.
12971.88Sapb#
12981.88Sapb#	o   An rc.d script that is run via the no_rc_postprocess
12991.88Sapb#	    function (so most of its output is invisible to rc(8)'s
13001.88Sapb#	    post-processor) can use print_rc_normal to force some of its
13011.88Sapb#	    output to be seen by the post-processor.
13021.88Sapb#
13031.78Sapb#
13041.78Sapbprint_rc_normal()
13051.78Sapb{
13061.94Sapb	# print to stdout or _rc_postprocessor_fd, depending on
13071.94Sapb	# whether not we have an rc postprocessor.
13081.78Sapb	#
13091.94Sapb	local fd=1
13101.94Sapb	_have_rc_postprocessor && fd="${_rc_postprocessor_fd}"
13111.88Sapb	case "$1" in
13121.88Sapb	"-n")
13131.88Sapb		command printf "%s" "$2" >&${fd}
13141.88Sapb		_flush_rc_output
13151.88Sapb		;;
13161.88Sapb	*)
13171.88Sapb		command printf "%s\n" "$1" >&${fd}
13181.88Sapb		;;
13191.88Sapb	esac
13201.78Sapb}
13211.78Sapb
13221.78Sapb#
13231.78Sapb# no_rc_postprocess cmd...
13241.78Sapb#	Execute the specified command in such a way that its output
13251.78Sapb#	bypasses the post-processor that handles the output from
13261.78Sapb#	most commands that are run inside /etc/rc.  If we are not
13271.78Sapb#	inside /etc/rc, then just execute the command without special
13281.78Sapb#	treatment.
13291.78Sapb#
13301.78Sapb#	The intent is that interactive commands can be run via
13311.78Sapb#	no_rc_postprocess(), and their output will apear immediately
13321.78Sapb#	on the console instead of being hidden or delayed by the
13331.78Sapb#	post-processor.	 An unfortunate consequence of the output
13341.78Sapb#	bypassing the post-processor is that the output will not be
13351.78Sapb#	logged.
13361.78Sapb#
13371.78Sapbno_rc_postprocess()
13381.78Sapb{
13391.94Sapb	if _have_rc_postprocessor; then
13401.78Sapb		"$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
13411.78Sapb	else
13421.78Sapb		"$@"
13431.78Sapb	fi
13441.78Sapb}
13451.78Sapb
13461.78Sapb#
13471.78Sapb# twiddle
13481.78Sapb#	On each call, print a different one of "/", "-", "\\", "|",
13491.78Sapb#	followed by a backspace.  The most recently printed value is
13501.78Sapb#	saved in $_twiddle_state.
13511.78Sapb#
13521.78Sapb#	Output is to /dev/tty, so this function may be useful even inside
13531.78Sapb#	a script whose output is redirected.
13541.78Sapb#
13551.78Sapbtwiddle()
13561.78Sapb{
13571.78Sapb	case "$_twiddle_state" in
13581.78Sapb	'/')	_next='-' ;;
13591.78Sapb	'-')	_next='\' ;;
13601.78Sapb	'\')	_next='|' ;;
13611.78Sapb	*)	_next='/' ;;
13621.78Sapb	esac
13631.88Sapb	command printf "%s\b" "$_next" >/dev/tty
13641.78Sapb	_twiddle_state="$_next"
13651.78Sapb}
13661.78Sapb
13671.82Schristos#
13681.82Schristos# human_exit_code
13691.82Schristos#	Print the a human version of the exit code.
13701.82Schristos#
13711.82Schristoshuman_exit_code()
13721.82Schristos{
13731.83Schristos	if [ "$1" -lt 127 ]
13741.82Schristos	then
13751.82Schristos		echo "exited with code $1"
13761.85Schristos	elif [ "$(expr $1 % 256)" -eq 127 ]
13771.82Schristos	then
13781.84Schristos		# This cannot really happen because the shell will not
13791.84Schristos		# pass stopped job status out and the exit code is limited
13801.84Schristos		# to 8 bits. This code is here just for completeness.
13811.82Schristos		echo "stopped with signal $(expr $1 / 256)"
13821.82Schristos	else
13831.82Schristos		echo "terminated with signal $(expr $1 - 128)"
13841.82Schristos	fi
13851.82Schristos}
13861.86Sapb
13871.86Sapb#
13881.86Sapb# collapse_backslash_newline
13891.86Sapb#	Copy input to output, collapsing <backslash><newline>
13901.86Sapb#	to nothing, but leaving other backslashes alone.
13911.86Sapb#
13921.86Sapbcollapse_backslash_newline()
13931.86Sapb{
13941.86Sapb	local line
13951.86Sapb	while read -r line ; do
13961.86Sapb		case "$line" in
13971.86Sapb		*\\)
13981.86Sapb			# print it, without the backslash or newline
13991.88Sapb			command printf "%s" "${line%?}"
14001.86Sapb			;;
14011.86Sapb		*)
14021.86Sapb			# print it, with a newline
14031.88Sapb			command printf "%s\n" "${line}"
14041.86Sapb			;;
14051.86Sapb		esac
14061.86Sapb	done
14071.86Sapb}
14081.82Schristos
14091.92Sapb# Shell implementations of basename and dirname, usable before
14101.92Sapb# the /usr file system is mounted.
14111.92Sapb#
14121.92Sapbbasename()
14131.92Sapb{
14141.92Sapb	local file="$1"
14151.92Sapb	local suffix="$2"
14161.92Sapb	local base
14171.92Sapb
14181.92Sapb	base="${file##*/}"		# remove up to and including last '/'
14191.92Sapb	base="${base%${suffix}}"	# remove suffix, if any
14201.92Sapb	command printf "%s\n" "${base}"
14211.92Sapb}
14221.92Sapb
14231.92Sapbdirname()
14241.92Sapb{
14251.92Sapb	local file="$1"
14261.92Sapb	local dir
14271.92Sapb
14281.92Sapb	case "$file" in
14291.92Sapb	/*/*)	dir="${file%/*}" ;;	# common case: absolute path
14301.92Sapb	/*)	dir="/" ;;		# special case: name in root dir
14311.92Sapb	*/*)	dir="${file%/*}" ;;	# common case: relative path with '/'
14321.92Sapb	*)	dir="." ;;		# special case: name without '/'
14331.92Sapb	esac
14341.92Sapb	command printf "%s\n" "${dir}"
14351.92Sapb}
14361.92Sapb
14371.88Sapb# Override the normal "echo" and "printf" commands, so that
14381.88Sapb# partial lines printed by rc.d scripts appear immediately,
14391.88Sapb# instead of being buffered by rc(8)'s post-processor.
14401.88Sapb#
14411.88Sapb# Naive use of the echo or printf commands from rc.d scripts,
14421.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr,
14431.88Sapb# will call these functions.  To call the real echo and printf
14441.88Sapb# commands, use "command echo" or "command printf".
14451.88Sapb#
14461.103Skre# Avoid use of echo altogether as much as possible, printf works better
14471.103Skre#
14481.88Sapbecho()
14491.88Sapb{
14501.103Skre	local IFS=' ' NL='\n'	# not a literal newline...
14511.103Skre
14521.88Sapb	case "$1" in
14531.103Skre	-n)	NL=; shift;;
14541.88Sapb	esac
14551.103Skre
14561.103Skre	command printf "%s${NL}" "$*"
14571.103Skre
14581.103Skre	if test -z "${NL}"
14591.103Skre	then
14601.103Skre		_flush_rc_output
14611.103Skre	fi
14621.103Skre	return 0
14631.88Sapb}
14641.103Skre
14651.88Sapbprintf()
14661.88Sapb{
14671.88Sapb	command printf "$@"
14681.88Sapb	case "$1" in
14691.88Sapb	*'\n')	: ;;
14701.88Sapb	*)	_flush_rc_output ;;
14711.88Sapb	esac
14721.103Skre	return 0
14731.88Sapb}
14741.88Sapb
14751.98Schristoskat() {
14761.98Schristos	local i
14771.98Schristos	local v
14781.98Schristos	for i; do
14791.98Schristos		while read -r v; do
14801.98Schristos			v="${v%%#*}"
14811.98Schristos			if [ -z "$v" ]; then
14821.98Schristos				continue
14831.98Schristos			fi
14841.98Schristos			echo "$v"
14851.98Schristos		done < "$i"
14861.98Schristos	done
14871.98Schristos}
14881.98Schristos
14891.64Smycroft_rc_subr_loaded=:
1490