rc.subr revision 179946
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 179946 2008-06-23 05:09:09Z mtm $
378344Sobrien#
4157473Sflz# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
578344Sobrien# All rights reserved.
678344Sobrien#
778344Sobrien# This code is derived from software contributed to The NetBSD Foundation
878344Sobrien# by Luke Mewburn.
978344Sobrien#
1078344Sobrien# Redistribution and use in source and binary forms, with or without
1178344Sobrien# modification, are permitted provided that the following conditions
1278344Sobrien# are met:
1378344Sobrien# 1. Redistributions of source code must retain the above copyright
1478344Sobrien#    notice, this list of conditions and the following disclaimer.
1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1678344Sobrien#    notice, this list of conditions and the following disclaimer in the
1778344Sobrien#    documentation and/or other materials provided with the distribution.
1878344Sobrien# 3. All advertising materials mentioning features or use of this software
1978344Sobrien#    must display the following acknowledgement:
2078344Sobrien#        This product includes software developed by the NetBSD
2178344Sobrien#        Foundation, Inc. and its contributors.
2278344Sobrien# 4. Neither the name of The NetBSD Foundation nor the names of its
2378344Sobrien#    contributors may be used to endorse or promote products derived
2478344Sobrien#    from this software without specific prior written permission.
2578344Sobrien#
2678344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2778344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2878344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2978344Sobrien# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3078344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3178344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3278344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3378344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3478344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3578344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3678344Sobrien# POSSIBILITY OF SUCH DAMAGE.
3778344Sobrien#
3878344Sobrien# rc.subr
3978344Sobrien#	functions used by various rc scripts
4078344Sobrien#
4178344Sobrien
42157473Sflz: ${rcvar_manpage:='rc.conf(5)'}
43169668Smtm: ${RC_PID:=$$}; export RC_PID
44157473Sflz
4578344Sobrien#
4698186Sgordon#	Operating System dependent/independent variables
4798186Sgordon#
4898186Sgordon
49131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then
50131550Scperciva
51131550Scperciva_rc_subr_loaded="YES"
52131550Scperciva
5398186SgordonSYSCTL="/sbin/sysctl"
5498186SgordonSYSCTL_N="${SYSCTL} -n"
5598186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype"
56103018SgordonOSTYPE=`${CMD_OSTYPE}`
57124832SmtmID="/usr/bin/id"
58124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi"
59161435SyarPS="/bin/ps -ww"
60161435SyarJID=`$PS -p $$ -o jid=`
6198186Sgordon
62103018Sgordoncase ${OSTYPE} in
6398186SgordonFreeBSD)
6498186Sgordon	SYSCTL_W="${SYSCTL}"
6598186Sgordon	;;
6698186SgordonNetBSD)
6798186Sgordon	SYSCTL_W="${SYSCTL} -w"
6898186Sgordon	;;
6998186Sgordonesac
7098186Sgordon
7198186Sgordon#
7278344Sobrien#	functions
7378344Sobrien#	---------
7478344Sobrien
7578344Sobrien#
7698186Sgordon# set_rcvar base_var
7798186Sgordon#	Set the variable name enabling a specific service.
7898186Sgordon#	FreeBSD uses ${service}_enable, while NetBSD uses
7998186Sgordon#	just the name of the service. For example:
8098186Sgordon#	FreeBSD: sendmail_enable="YES"
8198186Sgordon#	NetBSD : sendmail="YES"
8298186Sgordon#	$1 - if $name is not the base to work of off, specify
8398186Sgordon#	     a different one
8498186Sgordon#
8598186Sgordonset_rcvar()
8698186Sgordon{
8798186Sgordon	if [ -z "$1" ]; then
8898186Sgordon		base_var=${name}
8998186Sgordon	else
9098186Sgordon		base_var="$1"
9198186Sgordon	fi
9298186Sgordon
93103018Sgordon	case ${OSTYPE} in
9498186Sgordon	FreeBSD)
9598186Sgordon		echo ${base_var}_enable
9698186Sgordon		;;
9798186Sgordon	NetBSD)
9898186Sgordon		echo ${base_var}
9998186Sgordon		;;
10098186Sgordon	*)
10198186Sgordon		echo 'XXX'
10298186Sgordon		;;
10398186Sgordon	esac
10498186Sgordon}
10598186Sgordon
10698186Sgordon#
10798186Sgordon# force_depend script
10898186Sgordon#	Force a service to start. Intended for use by services
10998186Sgordon#	to resolve dependency issues. It is assumed the caller
11098186Sgordon#	has check to make sure this call is necessary
11198186Sgordon#	$1 - filename of script, in /etc/rc.d, to run
11298186Sgordon#
11398186Sgordonforce_depend()
11498186Sgordon{
11598186Sgordon	_depend="$1"
11698186Sgordon
11798186Sgordon	info "${name} depends on ${_depend}, which will be forced to start."
118146490Sschweikh	if ! /etc/rc.d/${_depend} forcestart; then
11998186Sgordon		warn "Unable to force ${_depend}. It may already be running."
12098186Sgordon		return 1
12198186Sgordon	fi
12298186Sgordon	return 0
12398186Sgordon}
12498186Sgordon
12598186Sgordon#
12678344Sobrien# checkyesno var
12778344Sobrien#	Test $1 variable, and warn if not set to YES or NO.
12878344Sobrien#	Return 0 if it's "yes" (et al), nonzero otherwise.
12978344Sobrien#
13078344Sobriencheckyesno()
13178344Sobrien{
13278344Sobrien	eval _value=\$${1}
13398186Sgordon	debug "checkyesno: $1 is set to $_value."
13478344Sobrien	case $_value in
13578344Sobrien
13678344Sobrien		#	"yes", "true", "on", or "1"
13778344Sobrien	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
13878344Sobrien		return 0
13978344Sobrien		;;
14078344Sobrien
14178344Sobrien		#	"no", "false", "off", or "0"
14278344Sobrien	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
14378344Sobrien		return 1
14478344Sobrien		;;
14578344Sobrien	*)
146157473Sflz		warn "\$${1} is not set properly - see ${rcvar_manpage}."
14778344Sobrien		return 1
14878344Sobrien		;;
14978344Sobrien	esac
15078344Sobrien}
15178344Sobrien
152157473Sflz#
15398186Sgordon# reverse_list list
15498186Sgordon#	print the list in reverse order
15578344Sobrien#
15698186Sgordonreverse_list()
15798186Sgordon{
15898186Sgordon	_revlist=
159126286Smtm	for _revfile; do
16098186Sgordon		_revlist="$_revfile $_revlist"
16198186Sgordon	done
16298186Sgordon	echo $_revlist
16398186Sgordon}
16498186Sgordon
165169668Smtm# stop_boot always
166169668Smtm#	If booting directly to multiuser or $always is enabled,
167169668Smtm#	send SIGTERM to the parent (/etc/rc) to abort the boot.
168169668Smtm#	Otherwise just exit.
16978344Sobrien#
170169668Smtmstop_boot()
171169668Smtm{
172169668Smtm	local always
173169668Smtm
174178776Smaxim	case $1 in
175178776Smaxim		#	"yes", "true", "on", or "1"
176178770Smtm        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
177169668Smtm		always=true
178178770Smtm		;;
179178770Smtm	*)
180169668Smtm		always=false
181178770Smtm		;;
182178775Smaxim	esac
183169668Smtm	if [ "$autoboot" = yes -o "$always" = true ]; then
184169668Smtm		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
185169668Smtm		kill -TERM ${RC_PID}
186169668Smtm	fi
187169668Smtm	exit 1
188169668Smtm}
189169668Smtm
190169668Smtm#
19198186Sgordon# mount_critical_filesystems type
19298186Sgordon#	Go through the list of critical filesystems as provided in
19398186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
19498186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
19598186Sgordon#
19678344Sobrienmount_critical_filesystems()
19778344Sobrien{
19898186Sgordon	eval _fslist=\$critical_filesystems_${1}
19978344Sobrien	for _fs in $_fslist; do
20078344Sobrien		mount | (
201126285Smtm			_ismounted=false
20278344Sobrien			while read what _on on _type type; do
20378344Sobrien				if [ $on = $_fs ]; then
204126285Smtm					_ismounted=true
20578344Sobrien				fi
20678344Sobrien			done
207126285Smtm			if $_ismounted; then
208126285Smtm				:
209126285Smtm			else
21078344Sobrien				mount $_fs >/dev/null 2>&1
21178344Sobrien			fi
21298186Sgordon		)
21378344Sobrien	done
21478344Sobrien}
21578344Sobrien
21678344Sobrien#
21798186Sgordon# check_pidfile pidfile procname [interpreter]
21898186Sgordon#	Parses the first line of pidfile for a PID, and ensures
21978344Sobrien#	that the process is running and matches procname.
22098186Sgordon#	Prints the matching PID upon success, nothing otherwise.
22198186Sgordon#	interpreter is optional; see _find_processes() for details.
22278344Sobrien#
22378344Sobriencheck_pidfile()
22478344Sobrien{
22578344Sobrien	_pidfile=$1
22678344Sobrien	_procname=$2
22798186Sgordon	_interpreter=$3
22878344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
22998186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
23078344Sobrien	fi
23178344Sobrien	if [ ! -f $_pidfile ]; then
232131061Smtm		debug "pid file ($_pidfile): not readable."
23378344Sobrien		return
23478344Sobrien	fi
23578344Sobrien	read _pid _junk < $_pidfile
23678344Sobrien	if [ -z "$_pid" ]; then
237139949Skeramida		debug "pid file ($_pidfile): no pid in file."
23878344Sobrien		return
23978344Sobrien	fi
24098186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
24178344Sobrien}
24278344Sobrien
24378344Sobrien#
24498186Sgordon# check_process procname [interpreter]
24578344Sobrien#	Ensures that a process (or processes) named procname is running.
24698186Sgordon#	Prints a list of matching PIDs.
24798186Sgordon#	interpreter is optional; see _find_processes() for details.
24878344Sobrien#
24978344Sobriencheck_process()
25078344Sobrien{
25178344Sobrien	_procname=$1
25298186Sgordon	_interpreter=$2
25378344Sobrien	if [ -z "$_procname" ]; then
25498186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
25578344Sobrien	fi
25698186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
25798186Sgordon}
25898186Sgordon
25998186Sgordon#
26098186Sgordon# _find_processes procname interpreter psargs
26198186Sgordon#	Search for procname in the output of ps generated by psargs.
26298186Sgordon#	Prints the PIDs of any matching processes, space separated.
26398186Sgordon#
26498186Sgordon#	If interpreter == ".", check the following variations of procname
26598186Sgordon#	against the first word of each command:
26698186Sgordon#		procname
26798186Sgordon#		`basename procname`
26898186Sgordon#		`basename procname` + ":"
26998186Sgordon#		"(" + `basename procname` + ")"
270155719Sceri#		"[" + `basename procname` + "]"
27198186Sgordon#
27298186Sgordon#	If interpreter != ".", read the first line of procname, remove the
27398186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
27498186Sgordon#	match that against each command, either as is, or with extra words
275157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
276157841Sflz#	using perl, the basename of the interpreter plus a colon is also
277157841Sflz#	tried as the prefix to procname.
27898186Sgordon#
27998186Sgordon_find_processes()
28098186Sgordon{
28198186Sgordon	if [ $# -ne 3 ]; then
28298186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
28398186Sgordon	fi
28498186Sgordon	_procname=$1
28598186Sgordon	_interpreter=$2
28698186Sgordon	_psargs=$3
28798186Sgordon
28878344Sobrien	_pref=
28998186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
290170282Syar		_script=${_chroot}${_chroot:+"/"}$_procname
291170282Syar		if [ -r $_script ]; then
292170282Syar			read _interp < $_script	# read interpreter name
293170282Syar			case "$_interp" in
294170282Syar			\#!*)
295170282Syar				_interp=${_interp#\#!}	# strip #!
296170282Syar				set -- $_interp
297170282Syar				case $1 in
298170282Syar				*/bin/env)
299170282Syar					shift	# drop env to get real name
300170282Syar					;;
301170282Syar				esac
302170282Syar				if [ $_interpreter != $1 ]; then
303170282Syar					warn "\$command_interpreter $_interpreter != $1"
304170282Syar				fi
305170282Syar				;;
306170282Syar			*)
307170282Syar				warn "no shebang line in $_script"
308170282Syar				set -- $_interpreter
309170282Syar				;;
310170282Syar			esac
311170282Syar		else
312170282Syar			warn "cannot read shebang line from $_script"
313170282Syar			set -- $_interpreter
31478344Sobrien		fi
31598186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
316157841Sflz		_interpbn=${1##*/}
31798186Sgordon		_fp_args='_argv'
31898186Sgordon		_fp_match='case "$_argv" in
319157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
32098186Sgordon	else					# a normal daemon
32198186Sgordon		_procnamebn=${_procname##*/}
32298186Sgordon		_fp_args='_arg0 _argv'
32398186Sgordon		_fp_match='case "$_arg0" in
324151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
32598186Sgordon	fi
32698186Sgordon
327161435Syar	_proccheck="\
328161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
329157657Sflz		while read _npid _jid '"$_fp_args"'; do
330161436Syar			'"$_fp_match"'
331157657Sflz				if [ "$JID" -eq "$_jid" ];
332157657Sflz				then echo -n "$_pref$_npid";
333157657Sflz				_pref=" ";
334157657Sflz				fi
33598186Sgordon				;;
33698186Sgordon			esac
33798186Sgordon		done'
33898186Sgordon
339114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
34098186Sgordon	eval $_proccheck
34198186Sgordon}
34298186Sgordon
34398186Sgordon#
34498186Sgordon# wait_for_pids pid [pid ...]
34598186Sgordon#	spins until none of the pids exist
34698186Sgordon#
34798186Sgordonwait_for_pids()
34898186Sgordon{
349126286Smtm	_list="$@"
35098186Sgordon	if [ -z "$_list" ]; then
35198186Sgordon		return
35298186Sgordon	fi
35398186Sgordon	_prefix=
35498186Sgordon	while true; do
35598186Sgordon		_nlist="";
35698186Sgordon		for _j in $_list; do
35798186Sgordon			if kill -0 $_j 2>/dev/null; then
35898186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
35998186Sgordon			fi
36098186Sgordon		done
36198186Sgordon		if [ -z "$_nlist" ]; then
36298186Sgordon			break
36378344Sobrien		fi
36498186Sgordon		_list=$_nlist
36598186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
36698186Sgordon		_prefix=", "
36798186Sgordon		sleep 2
36878344Sobrien	done
36998186Sgordon	if [ -n "$_prefix" ]; then
37098186Sgordon		echo "."
37198186Sgordon	fi
37278344Sobrien}
37378344Sobrien
37478344Sobrien#
37598186Sgordon# run_rc_command argument
37698186Sgordon#	Search for argument in the list of supported commands, which is:
37798186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
37898186Sgordon#	If there's a match, run ${argument}_cmd or the default method
37998186Sgordon#	(see below).
38078344Sobrien#
38198186Sgordon#	If argument has a given prefix, then change the operation as follows:
38298186Sgordon#		Prefix	Operation
38378344Sobrien#		------	---------
384175676Smtm#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
38598186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
386126303Smtm#		one	Set ${rcvar} to YES
387175676Smtm#		quiet	Don't output some diagnostics, and set rc_quiet=yes
38878344Sobrien#
38978344Sobrien#	The following globals are used:
39078344Sobrien#
39198186Sgordon#	Name		Needed	Purpose
39298186Sgordon#	----		------	-------
39378344Sobrien#	name		y	Name of script.
39478344Sobrien#
39578344Sobrien#	command		n	Full path to command.
39698186Sgordon#				Not needed if ${rc_arg}_cmd is set for
39778344Sobrien#				each keyword.
39878344Sobrien#
39978344Sobrien#	command_args	n	Optional args/shell directives for command.
40078344Sobrien#
40198186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
40298186Sgordon#				call check_{pidfile,process}() appropriately.
40398186Sgordon#
40478344Sobrien#	extra_commands	n	List of extra commands supported.
40578344Sobrien#
40698186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
40798186Sgordon#				otherwise use check_process $command.
40898186Sgordon#				In either case, only check if $command is set.
40978344Sobrien#
41098186Sgordon#	procname	n	Process name to check for instead of $command.
41198186Sgordon#
41278344Sobrien#	rcvar		n	This is checked with checkyesno to determine
41378344Sobrien#				if the action should be run.
41478344Sobrien#
415157653Sflz#	${name}_program	n	Full path to command.
416157653Sflz#				Meant to be used in /etc/rc.conf to override
417157653Sflz#				${command}.
418157653Sflz#
41978344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
42098186Sgordon#				Requires /usr to be mounted.
42178344Sobrien#
42278344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
42378344Sobrien#				(if not using ${name}_chroot).
42478344Sobrien#
42578344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
42678344Sobrien#				NOTE:	$flags from the parent environment
42778344Sobrien#					can be used to override this.
42878344Sobrien#
42978344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
43078344Sobrien#
43178344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
43278344Sobrien#				using ${name}_chroot.
43398186Sgordon#				Requires /usr to be mounted.
43478344Sobrien#
43578344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
43698186Sgordon#				Requires /usr to be mounted.
43778344Sobrien#
43898186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
43998186Sgordon#				to run the chrooted ${command} with.
44098186Sgordon#				Requires /usr to be mounted.
44178344Sobrien#
44298186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
44378344Sobrien#				Otherwise, use default command (see below)
44478344Sobrien#
44598186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
44698186Sgordon#				${rc_arg}_cmd method in the default
44798186Sgordon#				operation (i.e, after checking for required
44898186Sgordon#				bits and process (non)existence).
44978344Sobrien#				If this completes with a non-zero exit code,
45098186Sgordon#				don't run ${rc_arg}_cmd.
45178344Sobrien#
45298186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
45398186Sgordon#				${rc_arg}_cmd method, if that method
45498186Sgordon#				returned a zero exit code.
45598186Sgordon#
45678344Sobrien#	required_dirs	n	If set, check for the existence of the given
457165565Syar#				directories before running a (re)start command.
45878344Sobrien#
45978344Sobrien#	required_files	n	If set, check for the readability of the given
460165565Syar#				files before running a (re)start command.
46178344Sobrien#
462165565Syar#	required_modules n	If set, ensure the given kernel modules are
463165565Syar#				loaded before running a (re)start command.
464165565Syar#				The check and possible loads are actually
465165565Syar#				done after start_precmd so that the modules
466165565Syar#				aren't loaded in vain, should the precmd
467165565Syar#				return a non-zero status to indicate a error.
468165565Syar#				If a word in the list looks like "foo:bar",
469165565Syar#				"foo" is the KLD file name and "bar" is the
470165565Syar#				module name.  If a word looks like "foo~bar",
471165565Syar#				"foo" is the KLD file name and "bar" is a
472165565Syar#				egrep(1) pattern matching the module name.
473165565Syar#				Otherwise the module name is assumed to be
474165565Syar#				the same as the KLD file name, which is most
475165565Syar#				common.  See load_kld().
476165565Syar#
47778344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
47878344Sobrien#				listed variables before running the default
47978344Sobrien#				(re)start command.
48078344Sobrien#
48198186Sgordon#	Default behaviour for a given argument, if no override method is
48298186Sgordon#	provided:
48378344Sobrien#
48498186Sgordon#	Argument	Default behaviour
48598186Sgordon#	--------	-----------------
48678344Sobrien#	start		if !running && checkyesno ${rcvar}
48778344Sobrien#				${command}
48878344Sobrien#
48978344Sobrien#	stop		if ${pidfile}
49098186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
49178344Sobrien#			else
49298186Sgordon#				rc_pid=$(check_process $command)
49398186Sgordon#			kill $sig_stop $rc_pid
49498186Sgordon#			wait_for_pids $rc_pid
49598186Sgordon#			($sig_stop defaults to TERM.)
49678344Sobrien#
49798186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
49898186Sgordon#			and doesn't wait_for_pids.
49978344Sobrien#			$sig_reload defaults to HUP.
500151685Syar#			Note that `reload' isn't provided by default,
501151685Syar#			it should be enabled via $extra_commands.
50278344Sobrien#
50378344Sobrien#	restart		Run `stop' then `start'.
50478344Sobrien#
50598186Sgordon#	status		Show if ${command} is running, etc.
50678344Sobrien#
50798186Sgordon#	poll		Wait for ${command} to exit.
50898186Sgordon#
50998186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
51098186Sgordon#
51198186Sgordon#	Variables available to methods, and after run_rc_command() has
51298186Sgordon#	completed:
51398186Sgordon#
51498186Sgordon#	Variable	Purpose
51598186Sgordon#	--------	-------
516126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
51798186Sgordon#			performed
51898186Sgordon#
51998186Sgordon#	rc_flags	Flags to start the default command with.
52098186Sgordon#			Defaults to ${name}_flags, unless overridden
52198186Sgordon#			by $flags from the environment.
52298186Sgordon#			This variable may be changed by the precmd method.
52398186Sgordon#
52498186Sgordon#	rc_pid		PID of command (if appropriate)
52598186Sgordon#
52698186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
52798186Sgordon#
52898186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
52998186Sgordon#
530175676Smtm#	rc_quiet	Not empty if "quiet" was provided
53198186Sgordon#
532175676Smtm#
53378344Sobrienrun_rc_command()
53478344Sobrien{
535116097Smtm	_return=0
53698186Sgordon	rc_arg=$1
53778344Sobrien	if [ -z "$name" ]; then
53898186Sgordon		err 3 'run_rc_command: $name is not set.'
53978344Sobrien	fi
54078344Sobrien
541132892Smtm	# Don't repeat the first argument when passing additional command-
542132892Smtm	# line arguments to the command subroutines.
543132892Smtm	#
544132892Smtm	shift 1
545132892Smtm	rc_extra_args="$*"
546132892Smtm
547126303Smtm	_rc_prefix=
54898186Sgordon	case "$rc_arg" in
54978344Sobrien	fast*)				# "fast" prefix; don't check pid
55098186Sgordon		rc_arg=${rc_arg#fast}
55198186Sgordon		rc_fast=yes
552175676Smtm		rc_quiet=yes
55378344Sobrien		;;
554126303Smtm	force*)				# "force prefix; always run
55598186Sgordon		rc_force=yes
556126303Smtm		_rc_prefix=force
557126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
55878344Sobrien		if [ -n "${rcvar}" ]; then
55978344Sobrien			eval ${rcvar}=YES
56078344Sobrien		fi
56178344Sobrien		;;
562126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
563126303Smtm		_rc_prefix=one
564126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
565126303Smtm		if [ -n "${rcvar}" ]; then
566126303Smtm			eval ${rcvar}=YES
567126303Smtm		fi
568126303Smtm		;;
569175676Smtm	quiet*)				# "quiet" prefix; omit some messages
570175676Smtm		_rc_prefix=quiet
571175676Smtm		rc_arg=${rc_arg#${_rc_prefix}}
572175676Smtm		rc_quiet=yes
573175676Smtm		;;
57478344Sobrien	esac
57578344Sobrien
576161530Sflz	eval _override_command=\$${name}_program
577161530Sflz	command=${command:+${_override_command:-$command}}
578161530Sflz
57978344Sobrien	_keywords="start stop restart rcvar $extra_commands"
58098186Sgordon	rc_pid=
58178344Sobrien	_pidcmd=
58298186Sgordon	_procname=${procname:-${command}}
58398186Sgordon
584131135Smtm					# setup pid check command
585131135Smtm	if [ -n "$_procname" ]; then
58678344Sobrien		if [ -n "$pidfile" ]; then
58798186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
58898186Sgordon		else
58998186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
59078344Sobrien		fi
59178344Sobrien		if [ -n "$_pidcmd" ]; then
59298186Sgordon			_keywords="${_keywords} status poll"
59378344Sobrien		fi
59478344Sobrien	fi
59578344Sobrien
59698186Sgordon	if [ -z "$rc_arg" ]; then
597150796Syar		rc_usage $_keywords
59878344Sobrien	fi
59978344Sobrien
60078344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
60198186Sgordon		rc_flags=$flags
60278344Sobrien	else
60398186Sgordon		eval rc_flags=\$${name}_flags
60478344Sobrien	fi
60598186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
60698186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
60798186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
60878344Sobrien
60998186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
610124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
61198186Sgordon			unset _user
61298186Sgordon		fi
61398186Sgordon	fi
61498186Sgordon
615179870Smtm	eval $_pidcmd			# determine the pid if necessary
616179870Smtm
617179870Smtm	for _elem in $_keywords; do
618179870Smtm		if [ "$_elem" != "$rc_arg" ]; then
619179870Smtm			continue
620179870Smtm		fi
62178344Sobrien					# if ${rcvar} is set, and $1 is not
62298186Sgordon					# "rcvar", then run
62378344Sobrien					#	checkyesno ${rcvar}
62478344Sobrien					# and return if that failed
62578344Sobrien					#
626179870Smtm		if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
627179870Smtm			if ! checkyesno ${rcvar}; then
628179870Smtm				if [ -n "${rc_quiet}" ]; then
629179870Smtm					return 0
630179870Smtm				fi
631179870Smtm				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
632179870Smtm				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
633179870Smtm				echo "instead of '${rc_arg}'."
634175676Smtm				return 0
635175676Smtm			fi
63678344Sobrien		fi
63778344Sobrien
63878344Sobrien					# if there's a custom ${XXX_cmd},
63978344Sobrien					# run that instead of the default
64078344Sobrien					#
641165565Syar		eval _cmd=\$${rc_arg}_cmd \
642165565Syar		     _precmd=\$${rc_arg}_precmd \
643165565Syar		     _postcmd=\$${rc_arg}_postcmd
644165565Syar
64578344Sobrien		if [ -n "$_cmd" ]; then
646165565Syar			_run_rc_precmd || return 1
647165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
648165565Syar			_run_rc_postcmd
649116097Smtm			return $_return
65078344Sobrien		fi
65178344Sobrien
65298186Sgordon		case "$rc_arg" in	# default operations...
65378344Sobrien
65478344Sobrien		status)
655165565Syar			_run_rc_precmd || return 1
65698186Sgordon			if [ -n "$rc_pid" ]; then
65798186Sgordon				echo "${name} is running as pid $rc_pid."
65878344Sobrien			else
65978344Sobrien				echo "${name} is not running."
66078344Sobrien				return 1
66178344Sobrien			fi
662165565Syar			_run_rc_postcmd
66378344Sobrien			;;
66478344Sobrien
66578344Sobrien		start)
666131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
667157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
668153152Syar				return 1
66978344Sobrien			fi
67078344Sobrien
671167413Syar			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
672160667Syar				warn "run_rc_command: cannot run $command"
673153152Syar				return 1
67478344Sobrien			fi
67578344Sobrien
676179946Smtm			if ! _run_rc_precmd; then
677179946Smtm				warn "failed precmd routine for ${name}"
678179946Smtm				return 1
679179946Smtm			fi
68078344Sobrien
681160668Syar					# setup the full command to run
68278344Sobrien					#
683179946Smtm			[ -z "${rc_quiet}" ] && echo "Starting ${name}."
68478344Sobrien			if [ -n "$_chroot" ]; then
68578344Sobrien				_doit="\
68678344Sobrien${_nice:+nice -n $_nice }\
68778344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
68898186Sgordon$_chroot $command $rc_flags $command_args"
68978344Sobrien			else
69078344Sobrien				_doit="\
691161396Syar${_chdir:+cd $_chdir && }\
69298186Sgordon$command $rc_flags $command_args"
69398186Sgordon				if [ -n "$_user" ]; then
69498186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
69598186Sgordon				fi
696161396Syar				if [ -n "$_nice" ]; then
697161396Syar					if [ -z "$_user" ]; then
698161396Syar						_doit="sh -c \"$_doit\""
699161396Syar					fi	
700161396Syar					_doit="nice -n $_nice $_doit"
701161396Syar				fi
70278344Sobrien			fi
70398186Sgordon
704165565Syar					# run the full command
70598186Sgordon					#
706179946Smtm			if ! _run_rc_doit "$_doit"; then
707179946Smtm				warn "failed to start ${name}"
708179946Smtm				return 1
709179946Smtm			fi
71098186Sgordon
71198186Sgordon					# finally, run postcmd
71298186Sgordon					#
713165565Syar			_run_rc_postcmd
71478344Sobrien			;;
71578344Sobrien
71678344Sobrien		stop)
71798186Sgordon			if [ -z "$rc_pid" ]; then
718153152Syar				[ -n "$rc_fast" ] && return 0
719165565Syar				_run_rc_notrunning
720153152Syar				return 1
72178344Sobrien			fi
72278344Sobrien
723165565Syar			_run_rc_precmd || return 1
72498186Sgordon
72598186Sgordon					# send the signal to stop
72698186Sgordon					#
72778344Sobrien			echo "Stopping ${name}."
728165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
729165565Syar			_run_rc_doit "$_doit" || return 1
73098186Sgordon
73198186Sgordon					# wait for the command to exit,
73298186Sgordon					# and run postcmd.
73398186Sgordon			wait_for_pids $rc_pid
734165565Syar
735165565Syar			_run_rc_postcmd
73678344Sobrien			;;
73778344Sobrien
73878344Sobrien		reload)
73998186Sgordon			if [ -z "$rc_pid" ]; then
740165565Syar				_run_rc_notrunning
741153152Syar				return 1
74278344Sobrien			fi
743165565Syar
744165565Syar			_run_rc_precmd || return 1
745165565Syar
746165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
747165565Syar			_run_rc_doit "$_doit" || return 1
748165565Syar
749165565Syar			_run_rc_postcmd
75078344Sobrien			;;
75178344Sobrien
75278344Sobrien		restart)
75378344Sobrien					# prevent restart being called more
75478344Sobrien					# than once by any given script
75578344Sobrien					#
756126285Smtm			if ${_rc_restart_done:-false}; then
75778344Sobrien				return 0
75878344Sobrien			fi
759126285Smtm			_rc_restart_done=true
76078344Sobrien
761165565Syar			_run_rc_precmd || return 1
762165565Syar
763165565Syar			# run those in a subshell to keep global variables
764152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
765165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
766165565Syar			_return=$?
767165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
76898186Sgordon
769165565Syar			_run_rc_postcmd
77078344Sobrien			;;
77178344Sobrien
77298186Sgordon		poll)
773165565Syar			_run_rc_precmd || return 1
77498186Sgordon			if [ -n "$rc_pid" ]; then
77598186Sgordon				wait_for_pids $rc_pid
77698186Sgordon			fi
777165565Syar			_run_rc_postcmd
77898186Sgordon			;;
77998186Sgordon
78078344Sobrien		rcvar)
78178344Sobrien			echo "# $name"
78278344Sobrien			if [ -n "$rcvar" ]; then
78378344Sobrien				if checkyesno ${rcvar}; then
784164629Sflz					echo "${rcvar}=YES"
78578344Sobrien				else
786164629Sflz					echo "${rcvar}=NO"
78778344Sobrien				fi
78878344Sobrien			fi
78978344Sobrien			;;
79078344Sobrien
79178344Sobrien		*)
792150796Syar			rc_usage $_keywords
79378344Sobrien			;;
79478344Sobrien
79578344Sobrien		esac
796116097Smtm		return $_return
79778344Sobrien	done
79878344Sobrien
79998186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
800150796Syar	rc_usage $_keywords
801153152Syar	# not reached
80278344Sobrien}
80378344Sobrien
80478344Sobrien#
805165565Syar# Helper functions for run_rc_command: common code.
806165565Syar# They use such global variables besides the exported rc_* ones:
807165565Syar#
808165565Syar#	name	       R/W
809165565Syar#	------------------
810165565Syar#	_precmd		R
811165565Syar#	_postcmd	R
812165565Syar#	_return		W
813165565Syar#
814165565Syar_run_rc_precmd()
815165565Syar{
816165565Syar	check_required_before "$rc_arg" || return 1
817165565Syar
818165565Syar	if [ -n "$_precmd" ]; then
819165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
820165565Syar		eval "$_precmd $rc_extra_args"
821165565Syar		_return=$?
822165565Syar
823165565Syar		# If precmd failed and force isn't set, request exit.
824165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
825165565Syar			return 1
826165565Syar		fi
827165565Syar	fi
828165565Syar
829165565Syar	check_required_after "$rc_arg" || return 1
830165565Syar
831165565Syar	return 0
832165565Syar}
833165565Syar
834165565Syar_run_rc_postcmd()
835165565Syar{
836165565Syar	if [ -n "$_postcmd" ]; then
837165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
838165565Syar		eval "$_postcmd $rc_extra_args"
839165565Syar		_return=$?
840165565Syar	fi
841165565Syar	return 0
842165565Syar}
843165565Syar
844165565Syar_run_rc_doit()
845165565Syar{
846165565Syar	debug "run_rc_command: doit: $*"
847165565Syar	eval "$@"
848165565Syar	_return=$?
849165565Syar
850165565Syar	# If command failed and force isn't set, request exit.
851165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
852165565Syar		return 1
853165565Syar	fi
854165565Syar
855165565Syar	return 0
856165565Syar}
857165565Syar
858165565Syar_run_rc_notrunning()
859165565Syar{
860165565Syar	local _pidmsg
861165565Syar
862165565Syar	if [ -n "$pidfile" ]; then
863165565Syar		_pidmsg=" (check $pidfile)."
864165565Syar	else
865165565Syar		_pidmsg=
866165565Syar	fi
867165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
868165565Syar}
869165565Syar
870165565Syar_run_rc_killcmd()
871165565Syar{
872165565Syar	local _cmd
873165565Syar
874165565Syar	_cmd="kill -$1 $rc_pid"
875165565Syar	if [ -n "$_user" ]; then
876165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
877165565Syar	fi
878165565Syar	echo "$_cmd"
879165565Syar}
880165565Syar
881165565Syar#
88278344Sobrien# run_rc_script file arg
88378344Sobrien#	Start the script `file' with `arg', and correctly handle the
88478344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
88598186Sgordon#	sourced into the current environment.  If `file' appears to be
88698186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
88798186Sgordon#	executable run as a child process.
88878344Sobrien#
88978344Sobrienrun_rc_script()
89078344Sobrien{
89178344Sobrien	_file=$1
89278344Sobrien	_arg=$2
89378344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
89478344Sobrien		err 3 'USAGE: run_rc_script file arg'
89578344Sobrien	fi
89678344Sobrien
89798186Sgordon	unset	name command command_args command_interpreter \
89898186Sgordon		extra_commands pidfile procname \
89998186Sgordon		rcvar required_dirs required_files required_vars
90098186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
90198186Sgordon
90278344Sobrien	case "$_file" in
903153105Sdougb	/etc/rc.d/*.sh)			# run in current shell
904146490Sschweikh		set $_arg; . $_file
90578344Sobrien		;;
906153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
90798186Sgordon		warn "Ignoring scratch file $_file"
90898186Sgordon		;;
90978344Sobrien	*)				# run in subshell
91098186Sgordon		if [ -x $_file ]; then
91198186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
912146490Sschweikh				set $_arg; . $_file
91398186Sgordon			else
914130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
915130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
916146490Sschweikh				  set $_arg; . $_file )
91798186Sgordon			fi
91898186Sgordon		fi
91978344Sobrien		;;
92078344Sobrien	esac
92178344Sobrien}
92278344Sobrien
92378344Sobrien#
924157653Sflz# load_rc_config name
925157653Sflz#	Source in the configuration file for a given name.
92678344Sobrien#
92778344Sobrienload_rc_config()
92878344Sobrien{
929157653Sflz	_name=$1
930157653Sflz	if [ -z "$_name" ]; then
931157653Sflz		err 3 'USAGE: load_rc_config name'
93278344Sobrien	fi
93378344Sobrien
934126285Smtm	if ${_rc_conf_loaded:-false}; then
935126285Smtm		:
936126285Smtm	else
93798186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
93898186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
93998186Sgordon			. /etc/defaults/rc.conf
94098186Sgordon			source_rc_confs
94198186Sgordon		elif [ -r /etc/rc.conf ]; then
94298186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
94398186Sgordon			. /etc/rc.conf
94498186Sgordon		fi
945126285Smtm		_rc_conf_loaded=true
94698186Sgordon	fi
947161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
948157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
949161530Sflz		. /etc/rc.conf.d/"$_name"
950157653Sflz	fi
951179872Smtm
952179872Smtm	# Old variable names support
953179872Smtm	#
954179872Smtm	[ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
95578344Sobrien}
956157473Sflz  
957157473Sflz#
958157653Sflz# load_rc_config_var name var
959157653Sflz#	Read the rc.conf(5) var for name and set in the
960157473Sflz#	current shell, using load_rc_config in a subshell to prevent
961157473Sflz#	unwanted side effects from other variable assignments.
962157473Sflz#
963157473Sflzload_rc_config_var()
964157473Sflz{
965157473Sflz	if [ $# -ne 2 ]; then
966157653Sflz		err 3 'USAGE: load_rc_config_var name var'
967157473Sflz	fi
968157473Sflz	eval $(eval '(
969157473Sflz		load_rc_config '$1' >/dev/null;
970157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
971157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
972157473Sflz		fi
973157473Sflz	)' )
974157473Sflz}
97578344Sobrien
97678344Sobrien#
97778344Sobrien# rc_usage commands
97878344Sobrien#	Print a usage string for $0, with `commands' being a list of
97978344Sobrien#	valid commands.
98078344Sobrien#
98178344Sobrienrc_usage()
98278344Sobrien{
983126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
98478344Sobrien
98578344Sobrien	_sep=
986126286Smtm	for _elem; do
98778344Sobrien		echo -n 1>&2 "$_sep$_elem"
98878344Sobrien		_sep="|"
98978344Sobrien	done
99078344Sobrien	echo 1>&2 ")"
99178344Sobrien	exit 1
99278344Sobrien}
99378344Sobrien
99478344Sobrien#
99578344Sobrien# err exitval message
99678344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
99778344Sobrien#
99878344Sobrienerr()
99978344Sobrien{
100078344Sobrien	exitval=$1
100178344Sobrien	shift
100278344Sobrien
1003106643Sgordon	if [ -x /usr/bin/logger ]; then
1004106643Sgordon		logger "$0: ERROR: $*"
1005106643Sgordon	fi
1006106643Sgordon	echo 1>&2 "$0: ERROR: $*"
100778344Sobrien	exit $exitval
100878344Sobrien}
100978344Sobrien
101078344Sobrien#
101178344Sobrien# warn message
101278344Sobrien#	Display message to stderr and log to the syslog.
101378344Sobrien#
101478344Sobrienwarn()
101578344Sobrien{
1016106643Sgordon	if [ -x /usr/bin/logger ]; then
1017106643Sgordon		logger "$0: WARNING: $*"
1018106643Sgordon	fi
1019106643Sgordon	echo 1>&2 "$0: WARNING: $*"
102078344Sobrien}
102198186Sgordon
102298186Sgordon#
102398186Sgordon# info message
102498186Sgordon#	Display informational message to stdout and log to syslog.
102598186Sgordon#
102698186Sgordoninfo()
102798186Sgordon{
1028119170Smtm	case ${rc_info} in
1029119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1030119170Smtm		if [ -x /usr/bin/logger ]; then
1031119170Smtm			logger "$0: INFO: $*"
1032119170Smtm		fi
1033119170Smtm		echo "$0: INFO: $*"
1034119170Smtm		;;
1035119170Smtm	esac
103698186Sgordon}
103798186Sgordon
103898186Sgordon#
103998186Sgordon# debug message
1040106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
104198186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
104298186Sgordon#	function.
104398186Sgordon#
104498186Sgordondebug()
104598186Sgordon{
104698186Sgordon	case ${rc_debug} in
104798186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1048106700Sgordon		if [ -x /usr/bin/logger ]; then
1049162947Syar			logger "$0: DEBUG: $*"
1050106700Sgordon		fi
1051146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
105298186Sgordon		;;
105398186Sgordon	esac
105498186Sgordon}
105598186Sgordon
105698186Sgordon#
105798186Sgordon# backup_file action file cur backup
105898186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
105998186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
106098186Sgordon#
106198186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
106298186Sgordon#	which can be either YES or NO.
106398186Sgordon#
106498186Sgordon#	The `action' keyword can be one of the following:
106598186Sgordon#
106698186Sgordon#	add		`file' is now being backed up (and is possibly
106798186Sgordon#			being reentered into the backups system).  `cur'
106898186Sgordon#			is created and RCS files, if necessary, are
106998186Sgordon#			created as well.
107098186Sgordon#
107198186Sgordon#	update		`file' has changed and needs to be backed up.
107298186Sgordon#			If `cur' exists, it is copied to to `back' or
107398186Sgordon#			checked into RCS (if the repository file is old),
107498186Sgordon#			and then `file' is copied to `cur'.  Another RCS
107598186Sgordon#			check in done here if RCS is being used.
107698186Sgordon#
107798186Sgordon#	remove		`file' is no longer being tracked by the backups
107898186Sgordon#			system.  If RCS is not being used, `cur' is moved
107998186Sgordon#			to `back', otherwise an empty file is checked in,
108098186Sgordon#			and then `cur' is removed.
108198186Sgordon#
108298186Sgordon#
108398186Sgordonbackup_file()
108498186Sgordon{
108598186Sgordon	_action=$1
108698186Sgordon	_file=$2
108798186Sgordon	_cur=$3
108898186Sgordon	_back=$4
108998186Sgordon
109098186Sgordon	if checkyesno backup_uses_rcs; then
109198186Sgordon		_msg0="backup archive"
109298186Sgordon		_msg1="update"
109398186Sgordon
109498186Sgordon		# ensure that history file is not locked
109598186Sgordon		if [ -f $_cur,v ]; then
109698186Sgordon			rcs -q -u -U -M $_cur
109798186Sgordon		fi
109898186Sgordon
109998186Sgordon		# ensure after switching to rcs that the
110098186Sgordon		# current backup is not lost
110198186Sgordon		if [ -f $_cur ]; then
110298186Sgordon			# no archive, or current newer than archive
110398186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
110498186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
110598186Sgordon				rcs -q -kb -U $_cur
110698186Sgordon				co -q -f -u $_cur
110798186Sgordon			fi
110898186Sgordon		fi
110998186Sgordon
111098186Sgordon		case $_action in
111198186Sgordon		add|update)
111298186Sgordon			cp -p $_file $_cur
111398186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
111498186Sgordon			rcs -q -kb -U $_cur
111598186Sgordon			co -q -f -u $_cur
111698186Sgordon			chown root:wheel $_cur $_cur,v
111798186Sgordon			;;
111898186Sgordon		remove)
111998186Sgordon			cp /dev/null $_cur
112098186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
112198186Sgordon			rcs -q -kb -U $_cur
112298186Sgordon			chown root:wheel $_cur $_cur,v
112398186Sgordon			rm $_cur
112498186Sgordon			;;
112598186Sgordon		esac
112698186Sgordon	else
112798186Sgordon		case $_action in
112898186Sgordon		add|update)
112998186Sgordon			if [ -f $_cur ]; then
113098186Sgordon				cp -p $_cur $_back
113198186Sgordon			fi
113298186Sgordon			cp -p $_file $_cur
113398186Sgordon			chown root:wheel $_cur
113498186Sgordon			;;
113598186Sgordon		remove)
113698186Sgordon			mv -f $_cur $_back
113798186Sgordon			;;
113898186Sgordon		esac
113998186Sgordon	fi
114098186Sgordon}
1141119166Smtm
1142123344Smtm# make_symlink src link
1143123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1144123344Smtm#	directory in which link is to be created does not exist
1145123344Smtm#	a warning will be displayed and an error will be returned.
1146123344Smtm#	Returns 0 on sucess, 1 otherwise.
1147119166Smtm#
1148123344Smtmmake_symlink()
1149119166Smtm{
1150123344Smtm	local src link linkdir _me
1151123344Smtm	src="$1"
1152123344Smtm	link="$2"
1153123344Smtm	linkdir="`dirname $link`"
1154123344Smtm	_me="make_symlink()"
1155119166Smtm
1156123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1157123344Smtm		warn "$_me: requires two arguments."
1158119166Smtm		return 1
1159119166Smtm	fi
1160123344Smtm	if [ ! -d "$linkdir" ]; then
1161160667Syar		warn "$_me: the directory $linkdir does not exist."
1162119166Smtm		return 1
1163119166Smtm	fi
1164146490Sschweikh	if ! ln -sf $src $link; then
1165123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1166119166Smtm		return 1
1167119166Smtm	fi
1168119166Smtm	return 0
1169119166Smtm}
1170119166Smtm
1171119166Smtm# devfs_rulesets_from_file file
1172119166Smtm#	Reads a set of devfs commands from file, and creates
1173119166Smtm#	the specified rulesets with their rules. Returns non-zero
1174119166Smtm#	if there was an error.
1175119166Smtm#
1176119166Smtmdevfs_rulesets_from_file()
1177119166Smtm{
1178119166Smtm	local file _err _me
1179119166Smtm	file="$1"
1180119166Smtm	_me="devfs_rulesets_from_file"
1181119166Smtm	_err=0
1182119166Smtm
1183119166Smtm	if [ -z "$file" ]; then
1184119166Smtm		warn "$_me: you must specify a file"
1185119166Smtm		return 1
1186119166Smtm	fi
1187119166Smtm	if [ ! -e "$file" ]; then
1188119166Smtm		debug "$_me: no such file ($file)"
1189119166Smtm		return 0
1190119166Smtm	fi
1191119166Smtm	debug "reading rulesets from file ($file)"
1192119166Smtm	{ while read line
1193119166Smtm	do
1194119166Smtm		case $line in
1195119166Smtm		\#*)
1196119166Smtm			continue
1197119166Smtm			;;
1198119166Smtm		\[*\]*)
1199119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1200119166Smtm			if [ -z "$rulenum" ]; then
1201119166Smtm				warn "$_me: cannot extract rule number ($line)"
1202119166Smtm				_err=1
1203119166Smtm				break
1204119166Smtm			fi
1205119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1206119166Smtm			if [ -z "$rulename" ]; then
1207119166Smtm				warn "$_me: cannot extract rule name ($line)"
1208119166Smtm				_err=1
1209119166Smtm				break;
1210119166Smtm			fi
1211119166Smtm			eval $rulename=\$rulenum
1212119166Smtm			debug "found ruleset: $rulename=$rulenum"
1213146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1214119166Smtm				_err=1
1215119166Smtm				break
1216119166Smtm			fi
1217119166Smtm			;;
1218119166Smtm		*)
1219119166Smtm			rulecmd="${line%%"\#*"}"
1220119166Smtm			# evaluate the command incase it includes
1221119166Smtm			# other rules
1222119166Smtm			if [ -n "$rulecmd" ]; then
1223119166Smtm				debug "adding rule ($rulecmd)"
1224119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1225119166Smtm				then
1226119166Smtm					_err=1
1227119166Smtm					break
1228119166Smtm				fi
1229119166Smtm			fi
1230119166Smtm			;;
1231119166Smtm		esac
1232119166Smtm		if [ $_err -ne 0 ]; then
1233119166Smtm			debug "error in $_me"
1234119166Smtm			break
1235119166Smtm		fi
1236119166Smtm	done } < $file
1237119166Smtm	return $_err
1238119166Smtm}
1239119166Smtm
1240119166Smtm# devfs_init_rulesets
1241119166Smtm#	Initializes rulesets from configuration files. Returns
1242119166Smtm#	non-zero if there was an error.
1243119166Smtm#
1244119166Smtmdevfs_init_rulesets()
1245119166Smtm{
1246119166Smtm	local file _me
1247119166Smtm	_me="devfs_init_rulesets"
1248119166Smtm
1249119166Smtm	# Go through this only once
1250119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1251119166Smtm		debug "$_me: devfs rulesets already initialized"
1252119166Smtm		return
1253119166Smtm	fi
1254146490Sschweikh	for file in $devfs_rulesets; do
1255119166Smtm		devfs_rulesets_from_file $file || return 1
1256119166Smtm	done
1257119166Smtm	devfs_rulesets_init=1
1258119166Smtm	debug "$_me: devfs rulesets initialized"
1259119166Smtm	return 0
1260119166Smtm}
1261119166Smtm
1262119166Smtm# devfs_set_ruleset ruleset [dir]
1263151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1264119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1265119166Smtm#	Returns non-zero if it could not set it successfully.
1266119166Smtm#
1267119166Smtmdevfs_set_ruleset()
1268119166Smtm{
1269119166Smtm	local devdir rs _me
1270119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1271119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1272119166Smtm	_me="devfs_set_ruleset"
1273119166Smtm
1274119166Smtm	if [ -z "$rs" ]; then
1275119166Smtm		warn "$_me: you must specify a ruleset number"
1276119166Smtm		return 1
1277119166Smtm	fi
1278119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1279146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1280119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1281119166Smtm		return 1
1282119166Smtm	fi
1283119166Smtm	return 0
1284119166Smtm}
1285119166Smtm
1286119166Smtm# devfs_apply_ruleset ruleset [dir]
1287119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1288119166Smtm#	The ruleset argument must be a ruleset name as specified
1289119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1290119166Smtm#	if it could not apply the ruleset.
1291119166Smtm#
1292119166Smtmdevfs_apply_ruleset()
1293119166Smtm{
1294119166Smtm	local devdir rs _me
1295119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1296119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1297119166Smtm	_me="devfs_apply_ruleset"
1298119166Smtm
1299119166Smtm	if [ -z "$rs" ]; then
1300119166Smtm		warn "$_me: you must specify a ruleset"
1301119166Smtm		return 1
1302119166Smtm	fi
1303119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1304146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1305119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1306119166Smtm		return 1
1307119166Smtm	fi
1308119166Smtm	return 0
1309119166Smtm}
1310119166Smtm
1311119166Smtm# devfs_domount dir [ruleset]
1312119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1313119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1314119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1315119166Smtm#
1316119166Smtmdevfs_domount()
1317119166Smtm{
1318119166Smtm	local devdir rs _me
1319119166Smtm	devdir="$1"
1320119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1321119166Smtm	_me="devfs_domount()"
1322119166Smtm
1323119166Smtm	if [ -z "$devdir" ]; then
1324119166Smtm		warn "$_me: you must specify a mount-point"
1325119166Smtm		return 1
1326119166Smtm	fi
1327119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1328146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1329119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1330119166Smtm		return 1
1331119166Smtm	fi
1332119166Smtm	if [ -n "$rs" ]; then
1333119166Smtm		devfs_init_rulesets
1334119166Smtm		devfs_set_ruleset $rs $devdir
1335124797Scperciva		devfs -m $devdir rule applyset
1336119166Smtm	fi
1337119166Smtm	return 0
1338119166Smtm}
1339119166Smtm
1340119166Smtm# devfs_mount_jail dir [ruleset]
1341119166Smtm#	Mounts a devfs file system appropriate for jails
1342119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1343119166Smtm#	it names will be used instead.  If present, ruleset must
1344119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1345119166Smtm#	This function returns non-zero if an error occurs.
1346119166Smtm#
1347119166Smtmdevfs_mount_jail()
1348119166Smtm{
1349119166Smtm	local jdev rs _me
1350119166Smtm	jdev="$1"
1351119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1352119166Smtm	_me="devfs_mount_jail"
1353119166Smtm
1354119166Smtm	devfs_init_rulesets
1355146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1356119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1357119166Smtm		return 1
1358119166Smtm	fi
1359119166Smtm	return 0
1360119166Smtm}
1361127345Sbrooks
1362127345Sbrooks# Provide a function for normalizing the mounting of memory
1363127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1364127345Sbrooks# as close as possible between 5-current and 4-stable.
1365127345Sbrooks#   $1 = size
1366127345Sbrooks#   $2 = mount point
1367137451Skeramida#   $3 = (optional) extra mdmfs flags
1368146490Sschweikhmount_md()
1369146490Sschweikh{
1370127345Sbrooks	if [ -n "$3" ]; then
1371137451Skeramida		flags="$3"
1372127345Sbrooks	fi
1373149421Syar	/sbin/mdmfs $flags -s $1 md $2
1374127345Sbrooks}
1375131550Scperciva
1376159828Syar# Code common to scripts that need to load a kernel module
1377159828Syar# if it isn't in the kernel yet. Syntax:
1378160666Syar#   load_kld [-e regex] [-m module] file
1379159828Syar# where -e or -m chooses the way to check if the module
1380159828Syar# is already loaded:
1381160666Syar#   regex is egrep'd in the output from `kldstat -v',
1382160666Syar#   module is passed to `kldstat -m'.
1383160666Syar# The default way is as though `-m file' were specified.
1384159828Syarload_kld()
1385159828Syar{
1386159828Syar	local _loaded _mod _opt _re
1387159828Syar
1388159828Syar	while getopts "e:m:" _opt; do
1389159828Syar		case "$_opt" in
1390159828Syar		e) _re="$OPTARG" ;;
1391159828Syar		m) _mod="$OPTARG" ;;
1392160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1393159828Syar		esac
1394159828Syar	done
1395159828Syar	shift $(($OPTIND - 1))
1396160666Syar	if [ $# -ne 1 ]; then
1397160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1398160666Syar	fi
1399159828Syar	_mod=${_mod:-$1}
1400159828Syar	_loaded=false
1401159828Syar	if [ -n "$_re" ]; then
1402159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1403159828Syar			_loaded=true
1404159828Syar		fi
1405159828Syar	else
1406159828Syar		if kldstat -q -m "$_mod"; then
1407159828Syar			_loaded=true
1408159828Syar		fi
1409159828Syar	fi
1410159828Syar	if ! $_loaded; then
1411159828Syar		if ! kldload "$1"; then
1412159828Syar			warn "Unable to load kernel module $1"
1413159828Syar			return 1
1414160666Syar		else
1415160666Syar			info "$1 kernel module loaded."
1416159828Syar		fi
1417160666Syar	else
1418160666Syar		debug "load_kld: $1 kernel module already loaded."
1419159828Syar	fi
1420159828Syar	return 0
1421159828Syar}
1422159828Syar
1423149049Spjd# ltr str src dst
1424149049Spjd#	Change every $src in $str to $dst.
1425149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1426149049Spjd#	awk(1).
1427149049Spjdltr()
1428149049Spjd{
1429149049Spjd	local _str _src _dst _out _com
1430149049Spjd	_str=$1
1431149049Spjd	_src=$2
1432149049Spjd	_dst=$3
1433149049Spjd	_out=""
1434149049Spjd
1435149049Spjd	IFS=${_src}
1436149049Spjd	for _com in ${_str}; do
1437149049Spjd		if [ -z "${_out}" ]; then
1438149049Spjd			_out="${_com}"
1439149049Spjd		else
1440149049Spjd			_out="${_out}${_dst}${_com}"
1441149049Spjd		fi
1442149049Spjd	done
1443149049Spjd	echo "${_out}"
1444149049Spjd}
1445149049Spjd
1446149050Spjd# Creates a list of providers for GELI encryption.
1447149050Spjdgeli_make_list()
1448149050Spjd{
1449149050Spjd	local devices devices2
1450149050Spjd	local provider mountpoint type options rest
1451149050Spjd
1452149050Spjd	# Create list of GELI providers from fstab.
1453149050Spjd	while read provider mountpoint type options rest ; do
1454155570Sflz		case ":${options}" in
1455155570Sflz		:*noauto*)
1456155570Sflz			noauto=yes
1457155570Sflz			;;
1458155570Sflz		*)
1459155570Sflz			noauto=no
1460155570Sflz			;;
1461155570Sflz		esac
1462155570Sflz
1463149050Spjd		case ":${provider}" in
1464149050Spjd		:#*)
1465149050Spjd			continue
1466149050Spjd			;;
1467149050Spjd		*.eli)
1468149050Spjd			# Skip swap devices.
1469155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1470149050Spjd				continue
1471149050Spjd			fi
1472149050Spjd			devices="${devices} ${provider}"
1473149050Spjd			;;
1474149050Spjd		esac
1475149050Spjd	done < /etc/fstab
1476149050Spjd
1477149050Spjd	# Append providers from geli_devices.
1478149050Spjd	devices="${devices} ${geli_devices}"
1479149050Spjd
1480149050Spjd	for provider in ${devices}; do
1481149050Spjd		provider=${provider%.eli}
1482149050Spjd		provider=${provider#/dev/}
1483149050Spjd		devices2="${devices2} ${provider}"
1484149050Spjd	done
1485149050Spjd
1486149050Spjd	echo ${devices2}
1487149050Spjd}
1488149050Spjd
1489153027Sdougb# Find scripts in local_startup directories that use the old syntax
1490153027Sdougb#
1491153027Sdougbfind_local_scripts_old () {
1492153027Sdougb	zlist=''
1493153027Sdougb	slist=''
1494153027Sdougb	for dir in ${local_startup}; do
1495153027Sdougb		if [ -d "${dir}" ]; then
1496153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1497153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1498153027Sdougb				    continue
1499153027Sdougb				zlist="$zlist $file"
1500153027Sdougb			done
1501153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1502153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1503153027Sdougb				    continue
1504153027Sdougb				slist="$slist $file"
1505153027Sdougb			done
1506153027Sdougb		fi
1507153027Sdougb	done
1508153027Sdougb}
1509153027Sdougb
1510153027Sdougbfind_local_scripts_new () {
1511153027Sdougb	local_rc=''
1512153027Sdougb	for dir in ${local_startup}; do
1513153027Sdougb		if [ -d "${dir}" ]; then
1514153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1515153027Sdougb				case "$file" in
1516153027Sdougb				*.sample) ;;
1517153027Sdougb				*)	if [ -x "$file" ]; then
1518153027Sdougb						local_rc="${local_rc} ${file}"
1519153027Sdougb					fi
1520153027Sdougb					;;
1521153027Sdougb				esac
1522153027Sdougb			done
1523153027Sdougb		fi
1524153027Sdougb	done
1525153027Sdougb}
1526153027Sdougb
1527165565Syar# check_required_{before|after} command
1528165565Syar#	Check for things required by the command before and after its precmd,
1529165565Syar#	respectively.  The two separate functions are needed because some
1530165565Syar#	conditions should prevent precmd from being run while other things
1531165565Syar#	depend on precmd having already been run.
1532165565Syar#
1533165565Syarcheck_required_before()
1534165565Syar{
1535165565Syar	local _f
1536165565Syar
1537165565Syar	case "$1" in
1538165565Syar	start)
1539165565Syar		for _f in $required_vars; do
1540165565Syar			if ! checkyesno $_f; then
1541165565Syar				warn "\$${_f} is not enabled."
1542165565Syar				if [ -z "$rc_force" ]; then
1543165565Syar					return 1
1544165565Syar				fi
1545165565Syar			fi
1546165565Syar		done
1547165565Syar
1548165565Syar		for _f in $required_dirs; do
1549165565Syar			if [ ! -d "${_f}/." ]; then
1550165565Syar				warn "${_f} is not a directory."
1551165565Syar				if [ -z "$rc_force" ]; then
1552165565Syar					return 1
1553165565Syar				fi
1554165565Syar			fi
1555165565Syar		done
1556165565Syar
1557165565Syar		for _f in $required_files; do
1558165565Syar			if [ ! -r "${_f}" ]; then
1559165565Syar				warn "${_f} is not readable."
1560165565Syar				if [ -z "$rc_force" ]; then
1561165565Syar					return 1
1562165565Syar				fi
1563165565Syar			fi
1564165565Syar		done
1565165565Syar		;;
1566165565Syar	esac
1567165565Syar
1568165565Syar	return 0
1569165565Syar}
1570165565Syar
1571165565Syarcheck_required_after()
1572165565Syar{
1573165565Syar	local _f _args
1574165565Syar
1575165565Syar	case "$1" in
1576165565Syar	start)
1577165565Syar		for _f in $required_modules; do
1578165565Syar			case "${_f}" in
1579165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1580165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1581165565Syar				*)	_args="${_f}" ;;
1582165565Syar			esac
1583165565Syar			if ! load_kld ${_args}; then
1584165565Syar				if [ -z "$rc_force" ]; then
1585165565Syar					return 1
1586165565Syar				fi
1587165565Syar			fi
1588165565Syar		done
1589165565Syar		;;
1590165565Syar	esac
1591165565Syar
1592165565Syar	return 0
1593165565Syar}
1594165565Syar
1595131550Scpercivafi
1596157841Sflz
1597157841Sflz_rc_subr_loaded=:
1598