rc.subr revision 170282
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 170282 2007-06-04 11:39:35Z yar $
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
174169668Smtm	if [ -n "$1" ] && checkyesno $1; then
175169668Smtm		always=true
176169668Smtm	else
177169668Smtm		always=false
178169668Smtm	fi
179169668Smtm	if [ "$autoboot" = yes -o "$always" = true ]; then
180169668Smtm		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
181169668Smtm		kill -TERM ${RC_PID}
182169668Smtm	fi
183169668Smtm	exit 1
184169668Smtm}
185169668Smtm
186169668Smtm#
18798186Sgordon# mount_critical_filesystems type
18898186Sgordon#	Go through the list of critical filesystems as provided in
18998186Sgordon#	the rc.conf(5) variable $critical_filesystems_${type}, checking
19098186Sgordon#	each one to see if it is mounted, and if it is not, mounting it.
19198186Sgordon#
19278344Sobrienmount_critical_filesystems()
19378344Sobrien{
19498186Sgordon	eval _fslist=\$critical_filesystems_${1}
19578344Sobrien	for _fs in $_fslist; do
19678344Sobrien		mount | (
197126285Smtm			_ismounted=false
19878344Sobrien			while read what _on on _type type; do
19978344Sobrien				if [ $on = $_fs ]; then
200126285Smtm					_ismounted=true
20178344Sobrien				fi
20278344Sobrien			done
203126285Smtm			if $_ismounted; then
204126285Smtm				:
205126285Smtm			else
20678344Sobrien				mount $_fs >/dev/null 2>&1
20778344Sobrien			fi
20898186Sgordon		)
20978344Sobrien	done
21078344Sobrien}
21178344Sobrien
21278344Sobrien#
21398186Sgordon# check_pidfile pidfile procname [interpreter]
21498186Sgordon#	Parses the first line of pidfile for a PID, and ensures
21578344Sobrien#	that the process is running and matches procname.
21698186Sgordon#	Prints the matching PID upon success, nothing otherwise.
21798186Sgordon#	interpreter is optional; see _find_processes() for details.
21878344Sobrien#
21978344Sobriencheck_pidfile()
22078344Sobrien{
22178344Sobrien	_pidfile=$1
22278344Sobrien	_procname=$2
22398186Sgordon	_interpreter=$3
22478344Sobrien	if [ -z "$_pidfile" -o -z "$_procname" ]; then
22598186Sgordon		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
22678344Sobrien	fi
22778344Sobrien	if [ ! -f $_pidfile ]; then
228131061Smtm		debug "pid file ($_pidfile): not readable."
22978344Sobrien		return
23078344Sobrien	fi
23178344Sobrien	read _pid _junk < $_pidfile
23278344Sobrien	if [ -z "$_pid" ]; then
233139949Skeramida		debug "pid file ($_pidfile): no pid in file."
23478344Sobrien		return
23578344Sobrien	fi
23698186Sgordon	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
23778344Sobrien}
23878344Sobrien
23978344Sobrien#
24098186Sgordon# check_process procname [interpreter]
24178344Sobrien#	Ensures that a process (or processes) named procname is running.
24298186Sgordon#	Prints a list of matching PIDs.
24398186Sgordon#	interpreter is optional; see _find_processes() for details.
24478344Sobrien#
24578344Sobriencheck_process()
24678344Sobrien{
24778344Sobrien	_procname=$1
24898186Sgordon	_interpreter=$2
24978344Sobrien	if [ -z "$_procname" ]; then
25098186Sgordon		err 3 'USAGE: check_process procname [interpreter]'
25178344Sobrien	fi
25298186Sgordon	_find_processes $_procname ${_interpreter:-.} '-ax'
25398186Sgordon}
25498186Sgordon
25598186Sgordon#
25698186Sgordon# _find_processes procname interpreter psargs
25798186Sgordon#	Search for procname in the output of ps generated by psargs.
25898186Sgordon#	Prints the PIDs of any matching processes, space separated.
25998186Sgordon#
26098186Sgordon#	If interpreter == ".", check the following variations of procname
26198186Sgordon#	against the first word of each command:
26298186Sgordon#		procname
26398186Sgordon#		`basename procname`
26498186Sgordon#		`basename procname` + ":"
26598186Sgordon#		"(" + `basename procname` + ")"
266155719Sceri#		"[" + `basename procname` + "]"
26798186Sgordon#
26898186Sgordon#	If interpreter != ".", read the first line of procname, remove the
26998186Sgordon#	leading #!, normalise whitespace, append procname, and attempt to
27098186Sgordon#	match that against each command, either as is, or with extra words
271157841Sflz#	at the end.  As an alternative, to deal with interpreted daemons
272157841Sflz#	using perl, the basename of the interpreter plus a colon is also
273157841Sflz#	tried as the prefix to procname.
27498186Sgordon#
27598186Sgordon_find_processes()
27698186Sgordon{
27798186Sgordon	if [ $# -ne 3 ]; then
27898186Sgordon		err 3 'USAGE: _find_processes procname interpreter psargs'
27998186Sgordon	fi
28098186Sgordon	_procname=$1
28198186Sgordon	_interpreter=$2
28298186Sgordon	_psargs=$3
28398186Sgordon
28478344Sobrien	_pref=
28598186Sgordon	if [ $_interpreter != "." ]; then	# an interpreted script
286170282Syar		_script=${_chroot}${_chroot:+"/"}$_procname
287170282Syar		if [ -r $_script ]; then
288170282Syar			read _interp < $_script	# read interpreter name
289170282Syar			case "$_interp" in
290170282Syar			\#!*)
291170282Syar				_interp=${_interp#\#!}	# strip #!
292170282Syar				set -- $_interp
293170282Syar				case $1 in
294170282Syar				*/bin/env)
295170282Syar					shift	# drop env to get real name
296170282Syar					;;
297170282Syar				esac
298170282Syar				if [ $_interpreter != $1 ]; then
299170282Syar					warn "\$command_interpreter $_interpreter != $1"
300170282Syar				fi
301170282Syar				;;
302170282Syar			*)
303170282Syar				warn "no shebang line in $_script"
304170282Syar				set -- $_interpreter
305170282Syar				;;
306170282Syar			esac
307170282Syar		else
308170282Syar			warn "cannot read shebang line from $_script"
309170282Syar			set -- $_interpreter
31078344Sobrien		fi
31198186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
312157841Sflz		_interpbn=${1##*/}
31398186Sgordon		_fp_args='_argv'
31498186Sgordon		_fp_match='case "$_argv" in
315157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
31698186Sgordon	else					# a normal daemon
31798186Sgordon		_procnamebn=${_procname##*/}
31898186Sgordon		_fp_args='_arg0 _argv'
31998186Sgordon		_fp_match='case "$_arg0" in
320151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
32198186Sgordon	fi
32298186Sgordon
323161435Syar	_proccheck="\
324161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
325157657Sflz		while read _npid _jid '"$_fp_args"'; do
326161436Syar			'"$_fp_match"'
327157657Sflz				if [ "$JID" -eq "$_jid" ];
328157657Sflz				then echo -n "$_pref$_npid";
329157657Sflz				_pref=" ";
330157657Sflz				fi
33198186Sgordon				;;
33298186Sgordon			esac
33398186Sgordon		done'
33498186Sgordon
335114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
33698186Sgordon	eval $_proccheck
33798186Sgordon}
33898186Sgordon
33998186Sgordon#
34098186Sgordon# wait_for_pids pid [pid ...]
34198186Sgordon#	spins until none of the pids exist
34298186Sgordon#
34398186Sgordonwait_for_pids()
34498186Sgordon{
345126286Smtm	_list="$@"
34698186Sgordon	if [ -z "$_list" ]; then
34798186Sgordon		return
34898186Sgordon	fi
34998186Sgordon	_prefix=
35098186Sgordon	while true; do
35198186Sgordon		_nlist="";
35298186Sgordon		for _j in $_list; do
35398186Sgordon			if kill -0 $_j 2>/dev/null; then
35498186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
35598186Sgordon			fi
35698186Sgordon		done
35798186Sgordon		if [ -z "$_nlist" ]; then
35898186Sgordon			break
35978344Sobrien		fi
36098186Sgordon		_list=$_nlist
36198186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
36298186Sgordon		_prefix=", "
36398186Sgordon		sleep 2
36478344Sobrien	done
36598186Sgordon	if [ -n "$_prefix" ]; then
36698186Sgordon		echo "."
36798186Sgordon	fi
36878344Sobrien}
36978344Sobrien
37078344Sobrien#
37198186Sgordon# run_rc_command argument
37298186Sgordon#	Search for argument in the list of supported commands, which is:
37398186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
37498186Sgordon#	If there's a match, run ${argument}_cmd or the default method
37598186Sgordon#	(see below).
37678344Sobrien#
37798186Sgordon#	If argument has a given prefix, then change the operation as follows:
37898186Sgordon#		Prefix	Operation
37978344Sobrien#		------	---------
38098186Sgordon#		fast	Skip the pid check, and set rc_fast=yes
38198186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
382126303Smtm#		one	Set ${rcvar} to YES
38378344Sobrien#
38478344Sobrien#	The following globals are used:
38578344Sobrien#
38698186Sgordon#	Name		Needed	Purpose
38798186Sgordon#	----		------	-------
38878344Sobrien#	name		y	Name of script.
38978344Sobrien#
39078344Sobrien#	command		n	Full path to command.
39198186Sgordon#				Not needed if ${rc_arg}_cmd is set for
39278344Sobrien#				each keyword.
39378344Sobrien#
39478344Sobrien#	command_args	n	Optional args/shell directives for command.
39578344Sobrien#
39698186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
39798186Sgordon#				call check_{pidfile,process}() appropriately.
39898186Sgordon#
39978344Sobrien#	extra_commands	n	List of extra commands supported.
40078344Sobrien#
40198186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
40298186Sgordon#				otherwise use check_process $command.
40398186Sgordon#				In either case, only check if $command is set.
40478344Sobrien#
40598186Sgordon#	procname	n	Process name to check for instead of $command.
40698186Sgordon#
40778344Sobrien#	rcvar		n	This is checked with checkyesno to determine
40878344Sobrien#				if the action should be run.
40978344Sobrien#
410157653Sflz#	${name}_program	n	Full path to command.
411157653Sflz#				Meant to be used in /etc/rc.conf to override
412157653Sflz#				${command}.
413157653Sflz#
41478344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
41598186Sgordon#				Requires /usr to be mounted.
41678344Sobrien#
41778344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
41878344Sobrien#				(if not using ${name}_chroot).
41978344Sobrien#
42078344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
42178344Sobrien#				NOTE:	$flags from the parent environment
42278344Sobrien#					can be used to override this.
42378344Sobrien#
42478344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
42578344Sobrien#
42678344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
42778344Sobrien#				using ${name}_chroot.
42898186Sgordon#				Requires /usr to be mounted.
42978344Sobrien#
43078344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
43198186Sgordon#				Requires /usr to be mounted.
43278344Sobrien#
43398186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
43498186Sgordon#				to run the chrooted ${command} with.
43598186Sgordon#				Requires /usr to be mounted.
43678344Sobrien#
43798186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
43878344Sobrien#				Otherwise, use default command (see below)
43978344Sobrien#
44098186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
44198186Sgordon#				${rc_arg}_cmd method in the default
44298186Sgordon#				operation (i.e, after checking for required
44398186Sgordon#				bits and process (non)existence).
44478344Sobrien#				If this completes with a non-zero exit code,
44598186Sgordon#				don't run ${rc_arg}_cmd.
44678344Sobrien#
44798186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
44898186Sgordon#				${rc_arg}_cmd method, if that method
44998186Sgordon#				returned a zero exit code.
45098186Sgordon#
45178344Sobrien#	required_dirs	n	If set, check for the existence of the given
452165565Syar#				directories before running a (re)start command.
45378344Sobrien#
45478344Sobrien#	required_files	n	If set, check for the readability of the given
455165565Syar#				files before running a (re)start command.
45678344Sobrien#
457165565Syar#	required_modules n	If set, ensure the given kernel modules are
458165565Syar#				loaded before running a (re)start command.
459165565Syar#				The check and possible loads are actually
460165565Syar#				done after start_precmd so that the modules
461165565Syar#				aren't loaded in vain, should the precmd
462165565Syar#				return a non-zero status to indicate a error.
463165565Syar#				If a word in the list looks like "foo:bar",
464165565Syar#				"foo" is the KLD file name and "bar" is the
465165565Syar#				module name.  If a word looks like "foo~bar",
466165565Syar#				"foo" is the KLD file name and "bar" is a
467165565Syar#				egrep(1) pattern matching the module name.
468165565Syar#				Otherwise the module name is assumed to be
469165565Syar#				the same as the KLD file name, which is most
470165565Syar#				common.  See load_kld().
471165565Syar#
47278344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
47378344Sobrien#				listed variables before running the default
47478344Sobrien#				(re)start command.
47578344Sobrien#
47698186Sgordon#	Default behaviour for a given argument, if no override method is
47798186Sgordon#	provided:
47878344Sobrien#
47998186Sgordon#	Argument	Default behaviour
48098186Sgordon#	--------	-----------------
48178344Sobrien#	start		if !running && checkyesno ${rcvar}
48278344Sobrien#				${command}
48378344Sobrien#
48478344Sobrien#	stop		if ${pidfile}
48598186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
48678344Sobrien#			else
48798186Sgordon#				rc_pid=$(check_process $command)
48898186Sgordon#			kill $sig_stop $rc_pid
48998186Sgordon#			wait_for_pids $rc_pid
49098186Sgordon#			($sig_stop defaults to TERM.)
49178344Sobrien#
49298186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
49398186Sgordon#			and doesn't wait_for_pids.
49478344Sobrien#			$sig_reload defaults to HUP.
495151685Syar#			Note that `reload' isn't provided by default,
496151685Syar#			it should be enabled via $extra_commands.
49778344Sobrien#
49878344Sobrien#	restart		Run `stop' then `start'.
49978344Sobrien#
50098186Sgordon#	status		Show if ${command} is running, etc.
50178344Sobrien#
50298186Sgordon#	poll		Wait for ${command} to exit.
50398186Sgordon#
50498186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
50598186Sgordon#
50698186Sgordon#	Variables available to methods, and after run_rc_command() has
50798186Sgordon#	completed:
50898186Sgordon#
50998186Sgordon#	Variable	Purpose
51098186Sgordon#	--------	-------
511126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
51298186Sgordon#			performed
51398186Sgordon#
51498186Sgordon#	rc_flags	Flags to start the default command with.
51598186Sgordon#			Defaults to ${name}_flags, unless overridden
51698186Sgordon#			by $flags from the environment.
51798186Sgordon#			This variable may be changed by the precmd method.
51898186Sgordon#
51998186Sgordon#	rc_pid		PID of command (if appropriate)
52098186Sgordon#
52198186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
52298186Sgordon#
52398186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
52498186Sgordon#
52598186Sgordon#
52678344Sobrienrun_rc_command()
52778344Sobrien{
528116097Smtm	_return=0
52998186Sgordon	rc_arg=$1
53078344Sobrien	if [ -z "$name" ]; then
53198186Sgordon		err 3 'run_rc_command: $name is not set.'
53278344Sobrien	fi
53378344Sobrien
534132892Smtm	# Don't repeat the first argument when passing additional command-
535132892Smtm	# line arguments to the command subroutines.
536132892Smtm	#
537132892Smtm	shift 1
538132892Smtm	rc_extra_args="$*"
539132892Smtm
540126303Smtm	_rc_prefix=
54198186Sgordon	case "$rc_arg" in
54278344Sobrien	fast*)				# "fast" prefix; don't check pid
54398186Sgordon		rc_arg=${rc_arg#fast}
54498186Sgordon		rc_fast=yes
54578344Sobrien		;;
546126303Smtm	force*)				# "force prefix; always run
54798186Sgordon		rc_force=yes
548126303Smtm		_rc_prefix=force
549126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
55078344Sobrien		if [ -n "${rcvar}" ]; then
55178344Sobrien			eval ${rcvar}=YES
55278344Sobrien		fi
55378344Sobrien		;;
554126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
555126303Smtm		_rc_prefix=one
556126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
557126303Smtm		if [ -n "${rcvar}" ]; then
558126303Smtm			eval ${rcvar}=YES
559126303Smtm		fi
560126303Smtm		;;
56178344Sobrien	esac
56278344Sobrien
563161530Sflz	eval _override_command=\$${name}_program
564161530Sflz	command=${command:+${_override_command:-$command}}
565161530Sflz
56678344Sobrien	_keywords="start stop restart rcvar $extra_commands"
56798186Sgordon	rc_pid=
56878344Sobrien	_pidcmd=
56998186Sgordon	_procname=${procname:-${command}}
57098186Sgordon
571131135Smtm					# setup pid check command
572131135Smtm	if [ -n "$_procname" ]; then
57378344Sobrien		if [ -n "$pidfile" ]; then
57498186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
57598186Sgordon		else
57698186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
57778344Sobrien		fi
57878344Sobrien		if [ -n "$_pidcmd" ]; then
57998186Sgordon			_keywords="${_keywords} status poll"
58078344Sobrien		fi
58178344Sobrien	fi
58278344Sobrien
58398186Sgordon	if [ -z "$rc_arg" ]; then
584150796Syar		rc_usage $_keywords
58578344Sobrien	fi
58678344Sobrien
58778344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
58898186Sgordon		rc_flags=$flags
58978344Sobrien	else
59098186Sgordon		eval rc_flags=\$${name}_flags
59178344Sobrien	fi
59298186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
59398186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
59498186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
59578344Sobrien
59698186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
597124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
59898186Sgordon			unset _user
59998186Sgordon		fi
60098186Sgordon	fi
60198186Sgordon
60278344Sobrien					# if ${rcvar} is set, and $1 is not
60398186Sgordon					# "rcvar", then run
60478344Sobrien					#	checkyesno ${rcvar}
60578344Sobrien					# and return if that failed
60678344Sobrien					#
60798186Sgordon	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
60878344Sobrien		if ! checkyesno ${rcvar}; then
60978344Sobrien			return 0
61078344Sobrien		fi
61178344Sobrien	fi
61278344Sobrien
61378344Sobrien	eval $_pidcmd			# determine the pid if necessary
61478344Sobrien
61578344Sobrien	for _elem in $_keywords; do
61698186Sgordon		if [ "$_elem" != "$rc_arg" ]; then
61778344Sobrien			continue
61878344Sobrien		fi
61978344Sobrien					# if there's a custom ${XXX_cmd},
62078344Sobrien					# run that instead of the default
62178344Sobrien					#
622165565Syar		eval _cmd=\$${rc_arg}_cmd \
623165565Syar		     _precmd=\$${rc_arg}_precmd \
624165565Syar		     _postcmd=\$${rc_arg}_postcmd
625165565Syar
62678344Sobrien		if [ -n "$_cmd" ]; then
627165565Syar			_run_rc_precmd || return 1
628165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
629165565Syar			_run_rc_postcmd
630116097Smtm			return $_return
63178344Sobrien		fi
63278344Sobrien
63398186Sgordon		case "$rc_arg" in	# default operations...
63478344Sobrien
63578344Sobrien		status)
636165565Syar			_run_rc_precmd || return 1
63798186Sgordon			if [ -n "$rc_pid" ]; then
63898186Sgordon				echo "${name} is running as pid $rc_pid."
63978344Sobrien			else
64078344Sobrien				echo "${name} is not running."
64178344Sobrien				return 1
64278344Sobrien			fi
643165565Syar			_run_rc_postcmd
64478344Sobrien			;;
64578344Sobrien
64678344Sobrien		start)
647131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
648157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
649153152Syar				return 1
65078344Sobrien			fi
65178344Sobrien
652167413Syar			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
653160667Syar				warn "run_rc_command: cannot run $command"
654153152Syar				return 1
65578344Sobrien			fi
65678344Sobrien
657165565Syar			_run_rc_precmd || return 1
65878344Sobrien
659160668Syar					# setup the full command to run
66078344Sobrien					#
66178344Sobrien			echo "Starting ${name}."
66278344Sobrien			if [ -n "$_chroot" ]; then
66378344Sobrien				_doit="\
66478344Sobrien${_nice:+nice -n $_nice }\
66578344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
66698186Sgordon$_chroot $command $rc_flags $command_args"
66778344Sobrien			else
66878344Sobrien				_doit="\
669161396Syar${_chdir:+cd $_chdir && }\
67098186Sgordon$command $rc_flags $command_args"
67198186Sgordon				if [ -n "$_user" ]; then
67298186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
67398186Sgordon				fi
674161396Syar				if [ -n "$_nice" ]; then
675161396Syar					if [ -z "$_user" ]; then
676161396Syar						_doit="sh -c \"$_doit\""
677161396Syar					fi	
678161396Syar					_doit="nice -n $_nice $_doit"
679161396Syar				fi
68078344Sobrien			fi
68198186Sgordon
682165565Syar					# run the full command
68398186Sgordon					#
684165565Syar			_run_rc_doit "$_doit" || return 1
68598186Sgordon
68698186Sgordon					# finally, run postcmd
68798186Sgordon					#
688165565Syar			_run_rc_postcmd
68978344Sobrien			;;
69078344Sobrien
69178344Sobrien		stop)
69298186Sgordon			if [ -z "$rc_pid" ]; then
693153152Syar				[ -n "$rc_fast" ] && return 0
694165565Syar				_run_rc_notrunning
695153152Syar				return 1
69678344Sobrien			fi
69778344Sobrien
698165565Syar			_run_rc_precmd || return 1
69998186Sgordon
70098186Sgordon					# send the signal to stop
70198186Sgordon					#
70278344Sobrien			echo "Stopping ${name}."
703165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
704165565Syar			_run_rc_doit "$_doit" || return 1
70598186Sgordon
70698186Sgordon					# wait for the command to exit,
70798186Sgordon					# and run postcmd.
70898186Sgordon			wait_for_pids $rc_pid
709165565Syar
710165565Syar			_run_rc_postcmd
71178344Sobrien			;;
71278344Sobrien
71378344Sobrien		reload)
71498186Sgordon			if [ -z "$rc_pid" ]; then
715165565Syar				_run_rc_notrunning
716153152Syar				return 1
71778344Sobrien			fi
718165565Syar
719165565Syar			_run_rc_precmd || return 1
720165565Syar
721165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
722165565Syar			_run_rc_doit "$_doit" || return 1
723165565Syar
724165565Syar			_run_rc_postcmd
72578344Sobrien			;;
72678344Sobrien
72778344Sobrien		restart)
72878344Sobrien					# prevent restart being called more
72978344Sobrien					# than once by any given script
73078344Sobrien					#
731126285Smtm			if ${_rc_restart_done:-false}; then
73278344Sobrien				return 0
73378344Sobrien			fi
734126285Smtm			_rc_restart_done=true
73578344Sobrien
736165565Syar			_run_rc_precmd || return 1
737165565Syar
738165565Syar			# run those in a subshell to keep global variables
739152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
740165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
741165565Syar			_return=$?
742165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
74398186Sgordon
744165565Syar			_run_rc_postcmd
74578344Sobrien			;;
74678344Sobrien
74798186Sgordon		poll)
748165565Syar			_run_rc_precmd || return 1
74998186Sgordon			if [ -n "$rc_pid" ]; then
75098186Sgordon				wait_for_pids $rc_pid
75198186Sgordon			fi
752165565Syar			_run_rc_postcmd
75398186Sgordon			;;
75498186Sgordon
75578344Sobrien		rcvar)
75678344Sobrien			echo "# $name"
75778344Sobrien			if [ -n "$rcvar" ]; then
75878344Sobrien				if checkyesno ${rcvar}; then
759164629Sflz					echo "${rcvar}=YES"
76078344Sobrien				else
761164629Sflz					echo "${rcvar}=NO"
76278344Sobrien				fi
76378344Sobrien			fi
76478344Sobrien			;;
76578344Sobrien
76678344Sobrien		*)
767150796Syar			rc_usage $_keywords
76878344Sobrien			;;
76978344Sobrien
77078344Sobrien		esac
771116097Smtm		return $_return
77278344Sobrien	done
77378344Sobrien
77498186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
775150796Syar	rc_usage $_keywords
776153152Syar	# not reached
77778344Sobrien}
77878344Sobrien
77978344Sobrien#
780165565Syar# Helper functions for run_rc_command: common code.
781165565Syar# They use such global variables besides the exported rc_* ones:
782165565Syar#
783165565Syar#	name	       R/W
784165565Syar#	------------------
785165565Syar#	_precmd		R
786165565Syar#	_postcmd	R
787165565Syar#	_return		W
788165565Syar#
789165565Syar_run_rc_precmd()
790165565Syar{
791165565Syar	check_required_before "$rc_arg" || return 1
792165565Syar
793165565Syar	if [ -n "$_precmd" ]; then
794165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
795165565Syar		eval "$_precmd $rc_extra_args"
796165565Syar		_return=$?
797165565Syar
798165565Syar		# If precmd failed and force isn't set, request exit.
799165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
800165565Syar			return 1
801165565Syar		fi
802165565Syar	fi
803165565Syar
804165565Syar	check_required_after "$rc_arg" || return 1
805165565Syar
806165565Syar	return 0
807165565Syar}
808165565Syar
809165565Syar_run_rc_postcmd()
810165565Syar{
811165565Syar	if [ -n "$_postcmd" ]; then
812165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
813165565Syar		eval "$_postcmd $rc_extra_args"
814165565Syar		_return=$?
815165565Syar	fi
816165565Syar	return 0
817165565Syar}
818165565Syar
819165565Syar_run_rc_doit()
820165565Syar{
821165565Syar	debug "run_rc_command: doit: $*"
822165565Syar	eval "$@"
823165565Syar	_return=$?
824165565Syar
825165565Syar	# If command failed and force isn't set, request exit.
826165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
827165565Syar		return 1
828165565Syar	fi
829165565Syar
830165565Syar	return 0
831165565Syar}
832165565Syar
833165565Syar_run_rc_notrunning()
834165565Syar{
835165565Syar	local _pidmsg
836165565Syar
837165565Syar	if [ -n "$pidfile" ]; then
838165565Syar		_pidmsg=" (check $pidfile)."
839165565Syar	else
840165565Syar		_pidmsg=
841165565Syar	fi
842165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
843165565Syar}
844165565Syar
845165565Syar_run_rc_killcmd()
846165565Syar{
847165565Syar	local _cmd
848165565Syar
849165565Syar	_cmd="kill -$1 $rc_pid"
850165565Syar	if [ -n "$_user" ]; then
851165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
852165565Syar	fi
853165565Syar	echo "$_cmd"
854165565Syar}
855165565Syar
856165565Syar#
85778344Sobrien# run_rc_script file arg
85878344Sobrien#	Start the script `file' with `arg', and correctly handle the
85978344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
86098186Sgordon#	sourced into the current environment.  If `file' appears to be
86198186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
86298186Sgordon#	executable run as a child process.
86378344Sobrien#
86478344Sobrienrun_rc_script()
86578344Sobrien{
86678344Sobrien	_file=$1
86778344Sobrien	_arg=$2
86878344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
86978344Sobrien		err 3 'USAGE: run_rc_script file arg'
87078344Sobrien	fi
87178344Sobrien
87298186Sgordon	unset	name command command_args command_interpreter \
87398186Sgordon		extra_commands pidfile procname \
87498186Sgordon		rcvar required_dirs required_files required_vars
87598186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
87698186Sgordon
87778344Sobrien	case "$_file" in
878153105Sdougb	/etc/rc.d/*.sh)			# run in current shell
879146490Sschweikh		set $_arg; . $_file
88078344Sobrien		;;
881153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
88298186Sgordon		warn "Ignoring scratch file $_file"
88398186Sgordon		;;
88478344Sobrien	*)				# run in subshell
88598186Sgordon		if [ -x $_file ]; then
88698186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
887146490Sschweikh				set $_arg; . $_file
88898186Sgordon			else
889130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
890130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
891146490Sschweikh				  set $_arg; . $_file )
89298186Sgordon			fi
89398186Sgordon		fi
89478344Sobrien		;;
89578344Sobrien	esac
89678344Sobrien}
89778344Sobrien
89878344Sobrien#
899157653Sflz# load_rc_config name
900157653Sflz#	Source in the configuration file for a given name.
90178344Sobrien#
90278344Sobrienload_rc_config()
90378344Sobrien{
904157653Sflz	_name=$1
905157653Sflz	if [ -z "$_name" ]; then
906157653Sflz		err 3 'USAGE: load_rc_config name'
90778344Sobrien	fi
90878344Sobrien
909126285Smtm	if ${_rc_conf_loaded:-false}; then
910126285Smtm		:
911126285Smtm	else
91298186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
91398186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
91498186Sgordon			. /etc/defaults/rc.conf
91598186Sgordon			source_rc_confs
91698186Sgordon		elif [ -r /etc/rc.conf ]; then
91798186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
91898186Sgordon			. /etc/rc.conf
91998186Sgordon		fi
920126285Smtm		_rc_conf_loaded=true
92198186Sgordon	fi
922161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
923157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
924161530Sflz		. /etc/rc.conf.d/"$_name"
925157653Sflz	fi
926157653Sflz
927101850Sgordon	# XXX - Deprecated variable name support
928101850Sgordon	#
929103018Sgordon	case ${OSTYPE} in
930101850Sgordon	FreeBSD)
931146490Sschweikh		[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
932146490Sschweikh		[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
933146490Sschweikh		[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
934146490Sschweikh		[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
935146490Sschweikh		[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
936146490Sschweikh		[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
937146490Sschweikh		[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
938115950Smtm		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
939115950Smtm		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
940146490Sschweikh		;;
941101850Sgordon	esac
94278344Sobrien}
943157473Sflz  
944157473Sflz#
945157653Sflz# load_rc_config_var name var
946157653Sflz#	Read the rc.conf(5) var for name and set in the
947157473Sflz#	current shell, using load_rc_config in a subshell to prevent
948157473Sflz#	unwanted side effects from other variable assignments.
949157473Sflz#
950157473Sflzload_rc_config_var()
951157473Sflz{
952157473Sflz	if [ $# -ne 2 ]; then
953157653Sflz		err 3 'USAGE: load_rc_config_var name var'
954157473Sflz	fi
955157473Sflz	eval $(eval '(
956157473Sflz		load_rc_config '$1' >/dev/null;
957157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
958157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
959157473Sflz		fi
960157473Sflz	)' )
961157473Sflz}
96278344Sobrien
96378344Sobrien#
96478344Sobrien# rc_usage commands
96578344Sobrien#	Print a usage string for $0, with `commands' being a list of
96678344Sobrien#	valid commands.
96778344Sobrien#
96878344Sobrienrc_usage()
96978344Sobrien{
970126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
97178344Sobrien
97278344Sobrien	_sep=
973126286Smtm	for _elem; do
97478344Sobrien		echo -n 1>&2 "$_sep$_elem"
97578344Sobrien		_sep="|"
97678344Sobrien	done
97778344Sobrien	echo 1>&2 ")"
97878344Sobrien	exit 1
97978344Sobrien}
98078344Sobrien
98178344Sobrien#
98278344Sobrien# err exitval message
98378344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
98478344Sobrien#
98578344Sobrienerr()
98678344Sobrien{
98778344Sobrien	exitval=$1
98878344Sobrien	shift
98978344Sobrien
990106643Sgordon	if [ -x /usr/bin/logger ]; then
991106643Sgordon		logger "$0: ERROR: $*"
992106643Sgordon	fi
993106643Sgordon	echo 1>&2 "$0: ERROR: $*"
99478344Sobrien	exit $exitval
99578344Sobrien}
99678344Sobrien
99778344Sobrien#
99878344Sobrien# warn message
99978344Sobrien#	Display message to stderr and log to the syslog.
100078344Sobrien#
100178344Sobrienwarn()
100278344Sobrien{
1003106643Sgordon	if [ -x /usr/bin/logger ]; then
1004106643Sgordon		logger "$0: WARNING: $*"
1005106643Sgordon	fi
1006106643Sgordon	echo 1>&2 "$0: WARNING: $*"
100778344Sobrien}
100898186Sgordon
100998186Sgordon#
101098186Sgordon# info message
101198186Sgordon#	Display informational message to stdout and log to syslog.
101298186Sgordon#
101398186Sgordoninfo()
101498186Sgordon{
1015119170Smtm	case ${rc_info} in
1016119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1017119170Smtm		if [ -x /usr/bin/logger ]; then
1018119170Smtm			logger "$0: INFO: $*"
1019119170Smtm		fi
1020119170Smtm		echo "$0: INFO: $*"
1021119170Smtm		;;
1022119170Smtm	esac
102398186Sgordon}
102498186Sgordon
102598186Sgordon#
102698186Sgordon# debug message
1027106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
102898186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
102998186Sgordon#	function.
103098186Sgordon#
103198186Sgordondebug()
103298186Sgordon{
103398186Sgordon	case ${rc_debug} in
103498186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1035106700Sgordon		if [ -x /usr/bin/logger ]; then
1036162947Syar			logger "$0: DEBUG: $*"
1037106700Sgordon		fi
1038146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
103998186Sgordon		;;
104098186Sgordon	esac
104198186Sgordon}
104298186Sgordon
104398186Sgordon#
104498186Sgordon# backup_file action file cur backup
104598186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
104698186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
104798186Sgordon#
104898186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
104998186Sgordon#	which can be either YES or NO.
105098186Sgordon#
105198186Sgordon#	The `action' keyword can be one of the following:
105298186Sgordon#
105398186Sgordon#	add		`file' is now being backed up (and is possibly
105498186Sgordon#			being reentered into the backups system).  `cur'
105598186Sgordon#			is created and RCS files, if necessary, are
105698186Sgordon#			created as well.
105798186Sgordon#
105898186Sgordon#	update		`file' has changed and needs to be backed up.
105998186Sgordon#			If `cur' exists, it is copied to to `back' or
106098186Sgordon#			checked into RCS (if the repository file is old),
106198186Sgordon#			and then `file' is copied to `cur'.  Another RCS
106298186Sgordon#			check in done here if RCS is being used.
106398186Sgordon#
106498186Sgordon#	remove		`file' is no longer being tracked by the backups
106598186Sgordon#			system.  If RCS is not being used, `cur' is moved
106698186Sgordon#			to `back', otherwise an empty file is checked in,
106798186Sgordon#			and then `cur' is removed.
106898186Sgordon#
106998186Sgordon#
107098186Sgordonbackup_file()
107198186Sgordon{
107298186Sgordon	_action=$1
107398186Sgordon	_file=$2
107498186Sgordon	_cur=$3
107598186Sgordon	_back=$4
107698186Sgordon
107798186Sgordon	if checkyesno backup_uses_rcs; then
107898186Sgordon		_msg0="backup archive"
107998186Sgordon		_msg1="update"
108098186Sgordon
108198186Sgordon		# ensure that history file is not locked
108298186Sgordon		if [ -f $_cur,v ]; then
108398186Sgordon			rcs -q -u -U -M $_cur
108498186Sgordon		fi
108598186Sgordon
108698186Sgordon		# ensure after switching to rcs that the
108798186Sgordon		# current backup is not lost
108898186Sgordon		if [ -f $_cur ]; then
108998186Sgordon			# no archive, or current newer than archive
109098186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
109198186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
109298186Sgordon				rcs -q -kb -U $_cur
109398186Sgordon				co -q -f -u $_cur
109498186Sgordon			fi
109598186Sgordon		fi
109698186Sgordon
109798186Sgordon		case $_action in
109898186Sgordon		add|update)
109998186Sgordon			cp -p $_file $_cur
110098186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
110198186Sgordon			rcs -q -kb -U $_cur
110298186Sgordon			co -q -f -u $_cur
110398186Sgordon			chown root:wheel $_cur $_cur,v
110498186Sgordon			;;
110598186Sgordon		remove)
110698186Sgordon			cp /dev/null $_cur
110798186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
110898186Sgordon			rcs -q -kb -U $_cur
110998186Sgordon			chown root:wheel $_cur $_cur,v
111098186Sgordon			rm $_cur
111198186Sgordon			;;
111298186Sgordon		esac
111398186Sgordon	else
111498186Sgordon		case $_action in
111598186Sgordon		add|update)
111698186Sgordon			if [ -f $_cur ]; then
111798186Sgordon				cp -p $_cur $_back
111898186Sgordon			fi
111998186Sgordon			cp -p $_file $_cur
112098186Sgordon			chown root:wheel $_cur
112198186Sgordon			;;
112298186Sgordon		remove)
112398186Sgordon			mv -f $_cur $_back
112498186Sgordon			;;
112598186Sgordon		esac
112698186Sgordon	fi
112798186Sgordon}
1128119166Smtm
1129123344Smtm# make_symlink src link
1130123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1131123344Smtm#	directory in which link is to be created does not exist
1132123344Smtm#	a warning will be displayed and an error will be returned.
1133123344Smtm#	Returns 0 on sucess, 1 otherwise.
1134119166Smtm#
1135123344Smtmmake_symlink()
1136119166Smtm{
1137123344Smtm	local src link linkdir _me
1138123344Smtm	src="$1"
1139123344Smtm	link="$2"
1140123344Smtm	linkdir="`dirname $link`"
1141123344Smtm	_me="make_symlink()"
1142119166Smtm
1143123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1144123344Smtm		warn "$_me: requires two arguments."
1145119166Smtm		return 1
1146119166Smtm	fi
1147123344Smtm	if [ ! -d "$linkdir" ]; then
1148160667Syar		warn "$_me: the directory $linkdir does not exist."
1149119166Smtm		return 1
1150119166Smtm	fi
1151146490Sschweikh	if ! ln -sf $src $link; then
1152123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1153119166Smtm		return 1
1154119166Smtm	fi
1155119166Smtm	return 0
1156119166Smtm}
1157119166Smtm
1158119166Smtm# devfs_rulesets_from_file file
1159119166Smtm#	Reads a set of devfs commands from file, and creates
1160119166Smtm#	the specified rulesets with their rules. Returns non-zero
1161119166Smtm#	if there was an error.
1162119166Smtm#
1163119166Smtmdevfs_rulesets_from_file()
1164119166Smtm{
1165119166Smtm	local file _err _me
1166119166Smtm	file="$1"
1167119166Smtm	_me="devfs_rulesets_from_file"
1168119166Smtm	_err=0
1169119166Smtm
1170119166Smtm	if [ -z "$file" ]; then
1171119166Smtm		warn "$_me: you must specify a file"
1172119166Smtm		return 1
1173119166Smtm	fi
1174119166Smtm	if [ ! -e "$file" ]; then
1175119166Smtm		debug "$_me: no such file ($file)"
1176119166Smtm		return 0
1177119166Smtm	fi
1178119166Smtm	debug "reading rulesets from file ($file)"
1179119166Smtm	{ while read line
1180119166Smtm	do
1181119166Smtm		case $line in
1182119166Smtm		\#*)
1183119166Smtm			continue
1184119166Smtm			;;
1185119166Smtm		\[*\]*)
1186119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1187119166Smtm			if [ -z "$rulenum" ]; then
1188119166Smtm				warn "$_me: cannot extract rule number ($line)"
1189119166Smtm				_err=1
1190119166Smtm				break
1191119166Smtm			fi
1192119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1193119166Smtm			if [ -z "$rulename" ]; then
1194119166Smtm				warn "$_me: cannot extract rule name ($line)"
1195119166Smtm				_err=1
1196119166Smtm				break;
1197119166Smtm			fi
1198119166Smtm			eval $rulename=\$rulenum
1199119166Smtm			debug "found ruleset: $rulename=$rulenum"
1200146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1201119166Smtm				_err=1
1202119166Smtm				break
1203119166Smtm			fi
1204119166Smtm			;;
1205119166Smtm		*)
1206119166Smtm			rulecmd="${line%%"\#*"}"
1207119166Smtm			# evaluate the command incase it includes
1208119166Smtm			# other rules
1209119166Smtm			if [ -n "$rulecmd" ]; then
1210119166Smtm				debug "adding rule ($rulecmd)"
1211119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1212119166Smtm				then
1213119166Smtm					_err=1
1214119166Smtm					break
1215119166Smtm				fi
1216119166Smtm			fi
1217119166Smtm			;;
1218119166Smtm		esac
1219119166Smtm		if [ $_err -ne 0 ]; then
1220119166Smtm			debug "error in $_me"
1221119166Smtm			break
1222119166Smtm		fi
1223119166Smtm	done } < $file
1224119166Smtm	return $_err
1225119166Smtm}
1226119166Smtm
1227119166Smtm# devfs_init_rulesets
1228119166Smtm#	Initializes rulesets from configuration files. Returns
1229119166Smtm#	non-zero if there was an error.
1230119166Smtm#
1231119166Smtmdevfs_init_rulesets()
1232119166Smtm{
1233119166Smtm	local file _me
1234119166Smtm	_me="devfs_init_rulesets"
1235119166Smtm
1236119166Smtm	# Go through this only once
1237119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1238119166Smtm		debug "$_me: devfs rulesets already initialized"
1239119166Smtm		return
1240119166Smtm	fi
1241146490Sschweikh	for file in $devfs_rulesets; do
1242119166Smtm		devfs_rulesets_from_file $file || return 1
1243119166Smtm	done
1244119166Smtm	devfs_rulesets_init=1
1245119166Smtm	debug "$_me: devfs rulesets initialized"
1246119166Smtm	return 0
1247119166Smtm}
1248119166Smtm
1249119166Smtm# devfs_set_ruleset ruleset [dir]
1250151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1251119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1252119166Smtm#	Returns non-zero if it could not set it successfully.
1253119166Smtm#
1254119166Smtmdevfs_set_ruleset()
1255119166Smtm{
1256119166Smtm	local devdir rs _me
1257119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1258119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1259119166Smtm	_me="devfs_set_ruleset"
1260119166Smtm
1261119166Smtm	if [ -z "$rs" ]; then
1262119166Smtm		warn "$_me: you must specify a ruleset number"
1263119166Smtm		return 1
1264119166Smtm	fi
1265119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1266146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1267119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1268119166Smtm		return 1
1269119166Smtm	fi
1270119166Smtm	return 0
1271119166Smtm}
1272119166Smtm
1273119166Smtm# devfs_apply_ruleset ruleset [dir]
1274119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1275119166Smtm#	The ruleset argument must be a ruleset name as specified
1276119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1277119166Smtm#	if it could not apply the ruleset.
1278119166Smtm#
1279119166Smtmdevfs_apply_ruleset()
1280119166Smtm{
1281119166Smtm	local devdir rs _me
1282119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1283119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1284119166Smtm	_me="devfs_apply_ruleset"
1285119166Smtm
1286119166Smtm	if [ -z "$rs" ]; then
1287119166Smtm		warn "$_me: you must specify a ruleset"
1288119166Smtm		return 1
1289119166Smtm	fi
1290119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1291146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1292119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1293119166Smtm		return 1
1294119166Smtm	fi
1295119166Smtm	return 0
1296119166Smtm}
1297119166Smtm
1298119166Smtm# devfs_domount dir [ruleset]
1299119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1300119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1301119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1302119166Smtm#
1303119166Smtmdevfs_domount()
1304119166Smtm{
1305119166Smtm	local devdir rs _me
1306119166Smtm	devdir="$1"
1307119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1308119166Smtm	_me="devfs_domount()"
1309119166Smtm
1310119166Smtm	if [ -z "$devdir" ]; then
1311119166Smtm		warn "$_me: you must specify a mount-point"
1312119166Smtm		return 1
1313119166Smtm	fi
1314119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1315146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1316119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1317119166Smtm		return 1
1318119166Smtm	fi
1319119166Smtm	if [ -n "$rs" ]; then
1320119166Smtm		devfs_init_rulesets
1321119166Smtm		devfs_set_ruleset $rs $devdir
1322124797Scperciva		devfs -m $devdir rule applyset
1323119166Smtm	fi
1324119166Smtm	return 0
1325119166Smtm}
1326119166Smtm
1327119166Smtm# devfs_mount_jail dir [ruleset]
1328119166Smtm#	Mounts a devfs file system appropriate for jails
1329119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1330119166Smtm#	it names will be used instead.  If present, ruleset must
1331119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1332119166Smtm#	This function returns non-zero if an error occurs.
1333119166Smtm#
1334119166Smtmdevfs_mount_jail()
1335119166Smtm{
1336119166Smtm	local jdev rs _me
1337119166Smtm	jdev="$1"
1338119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1339119166Smtm	_me="devfs_mount_jail"
1340119166Smtm
1341119166Smtm	devfs_init_rulesets
1342146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1343119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1344119166Smtm		return 1
1345119166Smtm	fi
1346119166Smtm	return 0
1347119166Smtm}
1348127345Sbrooks
1349127345Sbrooks# Provide a function for normalizing the mounting of memory
1350127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1351127345Sbrooks# as close as possible between 5-current and 4-stable.
1352127345Sbrooks#   $1 = size
1353127345Sbrooks#   $2 = mount point
1354137451Skeramida#   $3 = (optional) extra mdmfs flags
1355146490Sschweikhmount_md()
1356146490Sschweikh{
1357127345Sbrooks	if [ -n "$3" ]; then
1358137451Skeramida		flags="$3"
1359127345Sbrooks	fi
1360149421Syar	/sbin/mdmfs $flags -s $1 md $2
1361127345Sbrooks}
1362131550Scperciva
1363159828Syar# Code common to scripts that need to load a kernel module
1364159828Syar# if it isn't in the kernel yet. Syntax:
1365160666Syar#   load_kld [-e regex] [-m module] file
1366159828Syar# where -e or -m chooses the way to check if the module
1367159828Syar# is already loaded:
1368160666Syar#   regex is egrep'd in the output from `kldstat -v',
1369160666Syar#   module is passed to `kldstat -m'.
1370160666Syar# The default way is as though `-m file' were specified.
1371159828Syarload_kld()
1372159828Syar{
1373159828Syar	local _loaded _mod _opt _re
1374159828Syar
1375159828Syar	while getopts "e:m:" _opt; do
1376159828Syar		case "$_opt" in
1377159828Syar		e) _re="$OPTARG" ;;
1378159828Syar		m) _mod="$OPTARG" ;;
1379160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1380159828Syar		esac
1381159828Syar	done
1382159828Syar	shift $(($OPTIND - 1))
1383160666Syar	if [ $# -ne 1 ]; then
1384160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1385160666Syar	fi
1386159828Syar	_mod=${_mod:-$1}
1387159828Syar	_loaded=false
1388159828Syar	if [ -n "$_re" ]; then
1389159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1390159828Syar			_loaded=true
1391159828Syar		fi
1392159828Syar	else
1393159828Syar		if kldstat -q -m "$_mod"; then
1394159828Syar			_loaded=true
1395159828Syar		fi
1396159828Syar	fi
1397159828Syar	if ! $_loaded; then
1398159828Syar		if ! kldload "$1"; then
1399159828Syar			warn "Unable to load kernel module $1"
1400159828Syar			return 1
1401160666Syar		else
1402160666Syar			info "$1 kernel module loaded."
1403159828Syar		fi
1404160666Syar	else
1405160666Syar		debug "load_kld: $1 kernel module already loaded."
1406159828Syar	fi
1407159828Syar	return 0
1408159828Syar}
1409159828Syar
1410149049Spjd# ltr str src dst
1411149049Spjd#	Change every $src in $str to $dst.
1412149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1413149049Spjd#	awk(1).
1414149049Spjdltr()
1415149049Spjd{
1416149049Spjd	local _str _src _dst _out _com
1417149049Spjd	_str=$1
1418149049Spjd	_src=$2
1419149049Spjd	_dst=$3
1420149049Spjd	_out=""
1421149049Spjd
1422149049Spjd	IFS=${_src}
1423149049Spjd	for _com in ${_str}; do
1424149049Spjd		if [ -z "${_out}" ]; then
1425149049Spjd			_out="${_com}"
1426149049Spjd		else
1427149049Spjd			_out="${_out}${_dst}${_com}"
1428149049Spjd		fi
1429149049Spjd	done
1430149049Spjd	echo "${_out}"
1431149049Spjd}
1432149049Spjd
1433149050Spjd# Creates a list of providers for GELI encryption.
1434149050Spjdgeli_make_list()
1435149050Spjd{
1436149050Spjd	local devices devices2
1437149050Spjd	local provider mountpoint type options rest
1438149050Spjd
1439149050Spjd	# Create list of GELI providers from fstab.
1440149050Spjd	while read provider mountpoint type options rest ; do
1441155570Sflz		case ":${options}" in
1442155570Sflz		:*noauto*)
1443155570Sflz			noauto=yes
1444155570Sflz			;;
1445155570Sflz		*)
1446155570Sflz			noauto=no
1447155570Sflz			;;
1448155570Sflz		esac
1449155570Sflz
1450149050Spjd		case ":${provider}" in
1451149050Spjd		:#*)
1452149050Spjd			continue
1453149050Spjd			;;
1454149050Spjd		*.eli)
1455149050Spjd			# Skip swap devices.
1456155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1457149050Spjd				continue
1458149050Spjd			fi
1459149050Spjd			devices="${devices} ${provider}"
1460149050Spjd			;;
1461149050Spjd		esac
1462149050Spjd	done < /etc/fstab
1463149050Spjd
1464149050Spjd	# Append providers from geli_devices.
1465149050Spjd	devices="${devices} ${geli_devices}"
1466149050Spjd
1467149050Spjd	for provider in ${devices}; do
1468149050Spjd		provider=${provider%.eli}
1469149050Spjd		provider=${provider#/dev/}
1470149050Spjd		devices2="${devices2} ${provider}"
1471149050Spjd	done
1472149050Spjd
1473149050Spjd	echo ${devices2}
1474149050Spjd}
1475149050Spjd
1476153027Sdougb# Find scripts in local_startup directories that use the old syntax
1477153027Sdougb#
1478153027Sdougbfind_local_scripts_old () {
1479153027Sdougb	zlist=''
1480153027Sdougb	slist=''
1481153027Sdougb	for dir in ${local_startup}; do
1482153027Sdougb		if [ -d "${dir}" ]; then
1483153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1484153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1485153027Sdougb				    continue
1486153027Sdougb				zlist="$zlist $file"
1487153027Sdougb			done
1488153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1489153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1490153027Sdougb				    continue
1491153027Sdougb				slist="$slist $file"
1492153027Sdougb			done
1493153027Sdougb		fi
1494153027Sdougb	done
1495153027Sdougb}
1496153027Sdougb
1497153027Sdougbfind_local_scripts_new () {
1498153027Sdougb	local_rc=''
1499153027Sdougb	for dir in ${local_startup}; do
1500153027Sdougb		if [ -d "${dir}" ]; then
1501153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1502153027Sdougb				case "$file" in
1503153027Sdougb				*.sample) ;;
1504153027Sdougb				*)	if [ -x "$file" ]; then
1505153027Sdougb						local_rc="${local_rc} ${file}"
1506153027Sdougb					fi
1507153027Sdougb					;;
1508153027Sdougb				esac
1509153027Sdougb			done
1510153027Sdougb		fi
1511153027Sdougb	done
1512153027Sdougb}
1513153027Sdougb
1514165565Syar# check_required_{before|after} command
1515165565Syar#	Check for things required by the command before and after its precmd,
1516165565Syar#	respectively.  The two separate functions are needed because some
1517165565Syar#	conditions should prevent precmd from being run while other things
1518165565Syar#	depend on precmd having already been run.
1519165565Syar#
1520165565Syarcheck_required_before()
1521165565Syar{
1522165565Syar	local _f
1523165565Syar
1524165565Syar	case "$1" in
1525165565Syar	start)
1526165565Syar		for _f in $required_vars; do
1527165565Syar			if ! checkyesno $_f; then
1528165565Syar				warn "\$${_f} is not enabled."
1529165565Syar				if [ -z "$rc_force" ]; then
1530165565Syar					return 1
1531165565Syar				fi
1532165565Syar			fi
1533165565Syar		done
1534165565Syar
1535165565Syar		for _f in $required_dirs; do
1536165565Syar			if [ ! -d "${_f}/." ]; then
1537165565Syar				warn "${_f} is not a directory."
1538165565Syar				if [ -z "$rc_force" ]; then
1539165565Syar					return 1
1540165565Syar				fi
1541165565Syar			fi
1542165565Syar		done
1543165565Syar
1544165565Syar		for _f in $required_files; do
1545165565Syar			if [ ! -r "${_f}" ]; then
1546165565Syar				warn "${_f} is not readable."
1547165565Syar				if [ -z "$rc_force" ]; then
1548165565Syar					return 1
1549165565Syar				fi
1550165565Syar			fi
1551165565Syar		done
1552165565Syar		;;
1553165565Syar	esac
1554165565Syar
1555165565Syar	return 0
1556165565Syar}
1557165565Syar
1558165565Syarcheck_required_after()
1559165565Syar{
1560165565Syar	local _f _args
1561165565Syar
1562165565Syar	case "$1" in
1563165565Syar	start)
1564165565Syar		for _f in $required_modules; do
1565165565Syar			case "${_f}" in
1566165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1567165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1568165565Syar				*)	_args="${_f}" ;;
1569165565Syar			esac
1570165565Syar			if ! load_kld ${_args}; then
1571165565Syar				if [ -z "$rc_force" ]; then
1572165565Syar					return 1
1573165565Syar				fi
1574165565Syar			fi
1575165565Syar		done
1576165565Syar		;;
1577165565Syar	esac
1578165565Syar
1579165565Syar	return 0
1580165565Syar}
1581165565Syar
1582131550Scpercivafi
1583157841Sflz
1584157841Sflz_rc_subr_loaded=:
1585