rc.subr revision 169668
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
298186Sgordon# $FreeBSD: head/etc/rc.subr 169668 2007-05-18 12:04:41Z 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
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
286167413Syar						# read interpreter name
287167413Syar		read _interp < ${_chroot}${_chroot:+"/"}$_procname
28898186Sgordon		_interp=${_interp#\#!}		# strip #!
28998186Sgordon		set -- $_interp
290165684Syar		case $1 in
291165684Syar		*/bin/env)
292165684Syar			shift			# drop env to get real name
293165684Syar			;;
294165684Syar		esac
29598186Sgordon		if [ $_interpreter != $1 ]; then
29698186Sgordon			warn "\$command_interpreter $_interpreter != $1"
29778344Sobrien		fi
29898186Sgordon		_interp="$* $_procname"		# cleanup spaces, add _procname
299157841Sflz		_interpbn=${1##*/}
30098186Sgordon		_fp_args='_argv'
30198186Sgordon		_fp_match='case "$_argv" in
302157841Sflz		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
30398186Sgordon	else					# a normal daemon
30498186Sgordon		_procnamebn=${_procname##*/}
30598186Sgordon		_fp_args='_arg0 _argv'
30698186Sgordon		_fp_match='case "$_arg0" in
307151426Sjhb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
30898186Sgordon	fi
30998186Sgordon
310161435Syar	_proccheck="\
311161436Syar		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
312157657Sflz		while read _npid _jid '"$_fp_args"'; do
313161436Syar			'"$_fp_match"'
314157657Sflz				if [ "$JID" -eq "$_jid" ];
315157657Sflz				then echo -n "$_pref$_npid";
316157657Sflz				_pref=" ";
317157657Sflz				fi
31898186Sgordon				;;
31998186Sgordon			esac
32098186Sgordon		done'
32198186Sgordon
322114272Smtm#	debug "in _find_processes: proccheck is ($_proccheck)."
32398186Sgordon	eval $_proccheck
32498186Sgordon}
32598186Sgordon
32698186Sgordon#
32798186Sgordon# wait_for_pids pid [pid ...]
32898186Sgordon#	spins until none of the pids exist
32998186Sgordon#
33098186Sgordonwait_for_pids()
33198186Sgordon{
332126286Smtm	_list="$@"
33398186Sgordon	if [ -z "$_list" ]; then
33498186Sgordon		return
33598186Sgordon	fi
33698186Sgordon	_prefix=
33798186Sgordon	while true; do
33898186Sgordon		_nlist="";
33998186Sgordon		for _j in $_list; do
34098186Sgordon			if kill -0 $_j 2>/dev/null; then
34198186Sgordon				_nlist="${_nlist}${_nlist:+ }$_j"
34298186Sgordon			fi
34398186Sgordon		done
34498186Sgordon		if [ -z "$_nlist" ]; then
34598186Sgordon			break
34678344Sobrien		fi
34798186Sgordon		_list=$_nlist
34898186Sgordon		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
34998186Sgordon		_prefix=", "
35098186Sgordon		sleep 2
35178344Sobrien	done
35298186Sgordon	if [ -n "$_prefix" ]; then
35398186Sgordon		echo "."
35498186Sgordon	fi
35578344Sobrien}
35678344Sobrien
35778344Sobrien#
35898186Sgordon# run_rc_command argument
35998186Sgordon#	Search for argument in the list of supported commands, which is:
36098186Sgordon#		"start stop restart rcvar status poll ${extra_commands}"
36198186Sgordon#	If there's a match, run ${argument}_cmd or the default method
36298186Sgordon#	(see below).
36378344Sobrien#
36498186Sgordon#	If argument has a given prefix, then change the operation as follows:
36598186Sgordon#		Prefix	Operation
36678344Sobrien#		------	---------
36798186Sgordon#		fast	Skip the pid check, and set rc_fast=yes
36898186Sgordon#		force	Set ${rcvar} to YES, and set rc_force=yes
369126303Smtm#		one	Set ${rcvar} to YES
37078344Sobrien#
37178344Sobrien#	The following globals are used:
37278344Sobrien#
37398186Sgordon#	Name		Needed	Purpose
37498186Sgordon#	----		------	-------
37578344Sobrien#	name		y	Name of script.
37678344Sobrien#
37778344Sobrien#	command		n	Full path to command.
37898186Sgordon#				Not needed if ${rc_arg}_cmd is set for
37978344Sobrien#				each keyword.
38078344Sobrien#
38178344Sobrien#	command_args	n	Optional args/shell directives for command.
38278344Sobrien#
38398186Sgordon#	command_interpreter n	If not empty, command is interpreted, so
38498186Sgordon#				call check_{pidfile,process}() appropriately.
38598186Sgordon#
38678344Sobrien#	extra_commands	n	List of extra commands supported.
38778344Sobrien#
38898186Sgordon#	pidfile		n	If set, use check_pidfile $pidfile $command,
38998186Sgordon#				otherwise use check_process $command.
39098186Sgordon#				In either case, only check if $command is set.
39178344Sobrien#
39298186Sgordon#	procname	n	Process name to check for instead of $command.
39398186Sgordon#
39478344Sobrien#	rcvar		n	This is checked with checkyesno to determine
39578344Sobrien#				if the action should be run.
39678344Sobrien#
397157653Sflz#	${name}_program	n	Full path to command.
398157653Sflz#				Meant to be used in /etc/rc.conf to override
399157653Sflz#				${command}.
400157653Sflz#
40178344Sobrien#	${name}_chroot	n	Directory to chroot to before running ${command}
40298186Sgordon#				Requires /usr to be mounted.
40378344Sobrien#
40478344Sobrien#	${name}_chdir	n	Directory to cd to before running ${command}
40578344Sobrien#				(if not using ${name}_chroot).
40678344Sobrien#
40778344Sobrien#	${name}_flags	n	Arguments to call ${command} with.
40878344Sobrien#				NOTE:	$flags from the parent environment
40978344Sobrien#					can be used to override this.
41078344Sobrien#
41178344Sobrien#	${name}_nice	n	Nice level to run ${command} at.
41278344Sobrien#
41378344Sobrien#	${name}_user	n	User to run ${command} as, using su(1) if not
41478344Sobrien#				using ${name}_chroot.
41598186Sgordon#				Requires /usr to be mounted.
41678344Sobrien#
41778344Sobrien#	${name}_group	n	Group to run chrooted ${command} as.
41898186Sgordon#				Requires /usr to be mounted.
41978344Sobrien#
42098186Sgordon#	${name}_groups	n	Comma separated list of supplementary groups
42198186Sgordon#				to run the chrooted ${command} with.
42298186Sgordon#				Requires /usr to be mounted.
42378344Sobrien#
42498186Sgordon#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
42578344Sobrien#				Otherwise, use default command (see below)
42678344Sobrien#
42798186Sgordon#	${rc_arg}_precmd n	If set, run just before performing the
42898186Sgordon#				${rc_arg}_cmd method in the default
42998186Sgordon#				operation (i.e, after checking for required
43098186Sgordon#				bits and process (non)existence).
43178344Sobrien#				If this completes with a non-zero exit code,
43298186Sgordon#				don't run ${rc_arg}_cmd.
43378344Sobrien#
43498186Sgordon#	${rc_arg}_postcmd n	If set, run just after performing the
43598186Sgordon#				${rc_arg}_cmd method, if that method
43698186Sgordon#				returned a zero exit code.
43798186Sgordon#
43878344Sobrien#	required_dirs	n	If set, check for the existence of the given
439165565Syar#				directories before running a (re)start command.
44078344Sobrien#
44178344Sobrien#	required_files	n	If set, check for the readability of the given
442165565Syar#				files before running a (re)start command.
44378344Sobrien#
444165565Syar#	required_modules n	If set, ensure the given kernel modules are
445165565Syar#				loaded before running a (re)start command.
446165565Syar#				The check and possible loads are actually
447165565Syar#				done after start_precmd so that the modules
448165565Syar#				aren't loaded in vain, should the precmd
449165565Syar#				return a non-zero status to indicate a error.
450165565Syar#				If a word in the list looks like "foo:bar",
451165565Syar#				"foo" is the KLD file name and "bar" is the
452165565Syar#				module name.  If a word looks like "foo~bar",
453165565Syar#				"foo" is the KLD file name and "bar" is a
454165565Syar#				egrep(1) pattern matching the module name.
455165565Syar#				Otherwise the module name is assumed to be
456165565Syar#				the same as the KLD file name, which is most
457165565Syar#				common.  See load_kld().
458165565Syar#
45978344Sobrien#	required_vars	n	If set, perform checkyesno on each of the
46078344Sobrien#				listed variables before running the default
46178344Sobrien#				(re)start command.
46278344Sobrien#
46398186Sgordon#	Default behaviour for a given argument, if no override method is
46498186Sgordon#	provided:
46578344Sobrien#
46698186Sgordon#	Argument	Default behaviour
46798186Sgordon#	--------	-----------------
46878344Sobrien#	start		if !running && checkyesno ${rcvar}
46978344Sobrien#				${command}
47078344Sobrien#
47178344Sobrien#	stop		if ${pidfile}
47298186Sgordon#				rc_pid=$(check_pidfile $pidfile $command)
47378344Sobrien#			else
47498186Sgordon#				rc_pid=$(check_process $command)
47598186Sgordon#			kill $sig_stop $rc_pid
47698186Sgordon#			wait_for_pids $rc_pid
47798186Sgordon#			($sig_stop defaults to TERM.)
47878344Sobrien#
47998186Sgordon#	reload		Similar to stop, except use $sig_reload instead,
48098186Sgordon#			and doesn't wait_for_pids.
48178344Sobrien#			$sig_reload defaults to HUP.
482151685Syar#			Note that `reload' isn't provided by default,
483151685Syar#			it should be enabled via $extra_commands.
48478344Sobrien#
48578344Sobrien#	restart		Run `stop' then `start'.
48678344Sobrien#
48798186Sgordon#	status		Show if ${command} is running, etc.
48878344Sobrien#
48998186Sgordon#	poll		Wait for ${command} to exit.
49098186Sgordon#
49198186Sgordon#	rcvar		Display what rc.conf variable is used (if any).
49298186Sgordon#
49398186Sgordon#	Variables available to methods, and after run_rc_command() has
49498186Sgordon#	completed:
49598186Sgordon#
49698186Sgordon#	Variable	Purpose
49798186Sgordon#	--------	-------
498126303Smtm#	rc_arg		Argument to command, after fast/force/one processing
49998186Sgordon#			performed
50098186Sgordon#
50198186Sgordon#	rc_flags	Flags to start the default command with.
50298186Sgordon#			Defaults to ${name}_flags, unless overridden
50398186Sgordon#			by $flags from the environment.
50498186Sgordon#			This variable may be changed by the precmd method.
50598186Sgordon#
50698186Sgordon#	rc_pid		PID of command (if appropriate)
50798186Sgordon#
50898186Sgordon#	rc_fast		Not empty if "fast" was provided (q.v.)
50998186Sgordon#
51098186Sgordon#	rc_force	Not empty if "force" was provided (q.v.)
51198186Sgordon#
51298186Sgordon#
51378344Sobrienrun_rc_command()
51478344Sobrien{
515116097Smtm	_return=0
51698186Sgordon	rc_arg=$1
51778344Sobrien	if [ -z "$name" ]; then
51898186Sgordon		err 3 'run_rc_command: $name is not set.'
51978344Sobrien	fi
52078344Sobrien
521132892Smtm	# Don't repeat the first argument when passing additional command-
522132892Smtm	# line arguments to the command subroutines.
523132892Smtm	#
524132892Smtm	shift 1
525132892Smtm	rc_extra_args="$*"
526132892Smtm
527126303Smtm	_rc_prefix=
52898186Sgordon	case "$rc_arg" in
52978344Sobrien	fast*)				# "fast" prefix; don't check pid
53098186Sgordon		rc_arg=${rc_arg#fast}
53198186Sgordon		rc_fast=yes
53278344Sobrien		;;
533126303Smtm	force*)				# "force prefix; always run
53498186Sgordon		rc_force=yes
535126303Smtm		_rc_prefix=force
536126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
53778344Sobrien		if [ -n "${rcvar}" ]; then
53878344Sobrien			eval ${rcvar}=YES
53978344Sobrien		fi
54078344Sobrien		;;
541126303Smtm	one*)				# "one" prefix; set ${rcvar}=yes
542126303Smtm		_rc_prefix=one
543126303Smtm		rc_arg=${rc_arg#${_rc_prefix}}
544126303Smtm		if [ -n "${rcvar}" ]; then
545126303Smtm			eval ${rcvar}=YES
546126303Smtm		fi
547126303Smtm		;;
54878344Sobrien	esac
54978344Sobrien
550161530Sflz	eval _override_command=\$${name}_program
551161530Sflz	command=${command:+${_override_command:-$command}}
552161530Sflz
55378344Sobrien	_keywords="start stop restart rcvar $extra_commands"
55498186Sgordon	rc_pid=
55578344Sobrien	_pidcmd=
55698186Sgordon	_procname=${procname:-${command}}
55798186Sgordon
558131135Smtm					# setup pid check command
559131135Smtm	if [ -n "$_procname" ]; then
56078344Sobrien		if [ -n "$pidfile" ]; then
56198186Sgordon			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
56298186Sgordon		else
56398186Sgordon			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
56478344Sobrien		fi
56578344Sobrien		if [ -n "$_pidcmd" ]; then
56698186Sgordon			_keywords="${_keywords} status poll"
56778344Sobrien		fi
56878344Sobrien	fi
56978344Sobrien
57098186Sgordon	if [ -z "$rc_arg" ]; then
571150796Syar		rc_usage $_keywords
57278344Sobrien	fi
57378344Sobrien
57478344Sobrien	if [ -n "$flags" ]; then	# allow override from environment
57598186Sgordon		rc_flags=$flags
57678344Sobrien	else
57798186Sgordon		eval rc_flags=\$${name}_flags
57878344Sobrien	fi
57998186Sgordon	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
58098186Sgordon	    _nice=\$${name}_nice	_user=\$${name}_user \
58198186Sgordon	    _group=\$${name}_group	_groups=\$${name}_groups
58278344Sobrien
58398186Sgordon	if [ -n "$_user" ]; then	# unset $_user if running as that user
584124832Smtm		if [ "$_user" = "$(eval $IDCMD)" ]; then
58598186Sgordon			unset _user
58698186Sgordon		fi
58798186Sgordon	fi
58898186Sgordon
58978344Sobrien					# if ${rcvar} is set, and $1 is not
59098186Sgordon					# "rcvar", then run
59178344Sobrien					#	checkyesno ${rcvar}
59278344Sobrien					# and return if that failed
59378344Sobrien					#
59498186Sgordon	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
59578344Sobrien		if ! checkyesno ${rcvar}; then
59678344Sobrien			return 0
59778344Sobrien		fi
59878344Sobrien	fi
59978344Sobrien
60078344Sobrien	eval $_pidcmd			# determine the pid if necessary
60178344Sobrien
60278344Sobrien	for _elem in $_keywords; do
60398186Sgordon		if [ "$_elem" != "$rc_arg" ]; then
60478344Sobrien			continue
60578344Sobrien		fi
60678344Sobrien					# if there's a custom ${XXX_cmd},
60778344Sobrien					# run that instead of the default
60878344Sobrien					#
609165565Syar		eval _cmd=\$${rc_arg}_cmd \
610165565Syar		     _precmd=\$${rc_arg}_precmd \
611165565Syar		     _postcmd=\$${rc_arg}_postcmd
612165565Syar
61378344Sobrien		if [ -n "$_cmd" ]; then
614165565Syar			_run_rc_precmd || return 1
615165565Syar			_run_rc_doit "$_cmd $rc_extra_args" || return 1
616165565Syar			_run_rc_postcmd
617116097Smtm			return $_return
61878344Sobrien		fi
61978344Sobrien
62098186Sgordon		case "$rc_arg" in	# default operations...
62178344Sobrien
62278344Sobrien		status)
623165565Syar			_run_rc_precmd || return 1
62498186Sgordon			if [ -n "$rc_pid" ]; then
62598186Sgordon				echo "${name} is running as pid $rc_pid."
62678344Sobrien			else
62778344Sobrien				echo "${name} is not running."
62878344Sobrien				return 1
62978344Sobrien			fi
630165565Syar			_run_rc_postcmd
63178344Sobrien			;;
63278344Sobrien
63378344Sobrien		start)
634131135Smtm			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
635157473Sflz				echo 1>&2 "${name} already running? (pid=$rc_pid)."
636153152Syar				return 1
63778344Sobrien			fi
63878344Sobrien
639167413Syar			if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
640160667Syar				warn "run_rc_command: cannot run $command"
641153152Syar				return 1
64278344Sobrien			fi
64378344Sobrien
644165565Syar			_run_rc_precmd || return 1
64578344Sobrien
646160668Syar					# setup the full command to run
64778344Sobrien					#
64878344Sobrien			echo "Starting ${name}."
64978344Sobrien			if [ -n "$_chroot" ]; then
65078344Sobrien				_doit="\
65178344Sobrien${_nice:+nice -n $_nice }\
65278344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
65398186Sgordon$_chroot $command $rc_flags $command_args"
65478344Sobrien			else
65578344Sobrien				_doit="\
656161396Syar${_chdir:+cd $_chdir && }\
65798186Sgordon$command $rc_flags $command_args"
65898186Sgordon				if [ -n "$_user" ]; then
65998186Sgordon				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
66098186Sgordon				fi
661161396Syar				if [ -n "$_nice" ]; then
662161396Syar					if [ -z "$_user" ]; then
663161396Syar						_doit="sh -c \"$_doit\""
664161396Syar					fi	
665161396Syar					_doit="nice -n $_nice $_doit"
666161396Syar				fi
66778344Sobrien			fi
66898186Sgordon
669165565Syar					# run the full command
67098186Sgordon					#
671165565Syar			_run_rc_doit "$_doit" || return 1
67298186Sgordon
67398186Sgordon					# finally, run postcmd
67498186Sgordon					#
675165565Syar			_run_rc_postcmd
67678344Sobrien			;;
67778344Sobrien
67878344Sobrien		stop)
67998186Sgordon			if [ -z "$rc_pid" ]; then
680153152Syar				[ -n "$rc_fast" ] && return 0
681165565Syar				_run_rc_notrunning
682153152Syar				return 1
68378344Sobrien			fi
68478344Sobrien
685165565Syar			_run_rc_precmd || return 1
68698186Sgordon
68798186Sgordon					# send the signal to stop
68898186Sgordon					#
68978344Sobrien			echo "Stopping ${name}."
690165565Syar			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
691165565Syar			_run_rc_doit "$_doit" || return 1
69298186Sgordon
69398186Sgordon					# wait for the command to exit,
69498186Sgordon					# and run postcmd.
69598186Sgordon			wait_for_pids $rc_pid
696165565Syar
697165565Syar			_run_rc_postcmd
69878344Sobrien			;;
69978344Sobrien
70078344Sobrien		reload)
70198186Sgordon			if [ -z "$rc_pid" ]; then
702165565Syar				_run_rc_notrunning
703153152Syar				return 1
70478344Sobrien			fi
705165565Syar
706165565Syar			_run_rc_precmd || return 1
707165565Syar
708165565Syar			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
709165565Syar			_run_rc_doit "$_doit" || return 1
710165565Syar
711165565Syar			_run_rc_postcmd
71278344Sobrien			;;
71378344Sobrien
71478344Sobrien		restart)
71578344Sobrien					# prevent restart being called more
71678344Sobrien					# than once by any given script
71778344Sobrien					#
718126285Smtm			if ${_rc_restart_done:-false}; then
71978344Sobrien				return 0
72078344Sobrien			fi
721126285Smtm			_rc_restart_done=true
72278344Sobrien
723165565Syar			_run_rc_precmd || return 1
724165565Syar
725165565Syar			# run those in a subshell to keep global variables
726152519Syar			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
727165565Syar			( run_rc_command ${_rc_prefix}start $rc_extra_args )
728165565Syar			_return=$?
729165565Syar			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
73098186Sgordon
731165565Syar			_run_rc_postcmd
73278344Sobrien			;;
73378344Sobrien
73498186Sgordon		poll)
735165565Syar			_run_rc_precmd || return 1
73698186Sgordon			if [ -n "$rc_pid" ]; then
73798186Sgordon				wait_for_pids $rc_pid
73898186Sgordon			fi
739165565Syar			_run_rc_postcmd
74098186Sgordon			;;
74198186Sgordon
74278344Sobrien		rcvar)
74378344Sobrien			echo "# $name"
74478344Sobrien			if [ -n "$rcvar" ]; then
74578344Sobrien				if checkyesno ${rcvar}; then
746164629Sflz					echo "${rcvar}=YES"
74778344Sobrien				else
748164629Sflz					echo "${rcvar}=NO"
74978344Sobrien				fi
75078344Sobrien			fi
75178344Sobrien			;;
75278344Sobrien
75378344Sobrien		*)
754150796Syar			rc_usage $_keywords
75578344Sobrien			;;
75678344Sobrien
75778344Sobrien		esac
758116097Smtm		return $_return
75978344Sobrien	done
76078344Sobrien
76198186Sgordon	echo 1>&2 "$0: unknown directive '$rc_arg'."
762150796Syar	rc_usage $_keywords
763153152Syar	# not reached
76478344Sobrien}
76578344Sobrien
76678344Sobrien#
767165565Syar# Helper functions for run_rc_command: common code.
768165565Syar# They use such global variables besides the exported rc_* ones:
769165565Syar#
770165565Syar#	name	       R/W
771165565Syar#	------------------
772165565Syar#	_precmd		R
773165565Syar#	_postcmd	R
774165565Syar#	_return		W
775165565Syar#
776165565Syar_run_rc_precmd()
777165565Syar{
778165565Syar	check_required_before "$rc_arg" || return 1
779165565Syar
780165565Syar	if [ -n "$_precmd" ]; then
781165565Syar		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
782165565Syar		eval "$_precmd $rc_extra_args"
783165565Syar		_return=$?
784165565Syar
785165565Syar		# If precmd failed and force isn't set, request exit.
786165565Syar		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
787165565Syar			return 1
788165565Syar		fi
789165565Syar	fi
790165565Syar
791165565Syar	check_required_after "$rc_arg" || return 1
792165565Syar
793165565Syar	return 0
794165565Syar}
795165565Syar
796165565Syar_run_rc_postcmd()
797165565Syar{
798165565Syar	if [ -n "$_postcmd" ]; then
799165565Syar		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
800165565Syar		eval "$_postcmd $rc_extra_args"
801165565Syar		_return=$?
802165565Syar	fi
803165565Syar	return 0
804165565Syar}
805165565Syar
806165565Syar_run_rc_doit()
807165565Syar{
808165565Syar	debug "run_rc_command: doit: $*"
809165565Syar	eval "$@"
810165565Syar	_return=$?
811165565Syar
812165565Syar	# If command failed and force isn't set, request exit.
813165565Syar	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
814165565Syar		return 1
815165565Syar	fi
816165565Syar
817165565Syar	return 0
818165565Syar}
819165565Syar
820165565Syar_run_rc_notrunning()
821165565Syar{
822165565Syar	local _pidmsg
823165565Syar
824165565Syar	if [ -n "$pidfile" ]; then
825165565Syar		_pidmsg=" (check $pidfile)."
826165565Syar	else
827165565Syar		_pidmsg=
828165565Syar	fi
829165565Syar	echo 1>&2 "${name} not running?${_pidmsg}"
830165565Syar}
831165565Syar
832165565Syar_run_rc_killcmd()
833165565Syar{
834165565Syar	local _cmd
835165565Syar
836165565Syar	_cmd="kill -$1 $rc_pid"
837165565Syar	if [ -n "$_user" ]; then
838165565Syar		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
839165565Syar	fi
840165565Syar	echo "$_cmd"
841165565Syar}
842165565Syar
843165565Syar#
84478344Sobrien# run_rc_script file arg
84578344Sobrien#	Start the script `file' with `arg', and correctly handle the
84678344Sobrien#	return value from the script.  If `file' ends with `.sh', it's
84798186Sgordon#	sourced into the current environment.  If `file' appears to be
84898186Sgordon#	a backup or scratch file, ignore it.  Otherwise if it's
84998186Sgordon#	executable run as a child process.
85078344Sobrien#
85178344Sobrienrun_rc_script()
85278344Sobrien{
85378344Sobrien	_file=$1
85478344Sobrien	_arg=$2
85578344Sobrien	if [ -z "$_file" -o -z "$_arg" ]; then
85678344Sobrien		err 3 'USAGE: run_rc_script file arg'
85778344Sobrien	fi
85878344Sobrien
85998186Sgordon	unset	name command command_args command_interpreter \
86098186Sgordon		extra_commands pidfile procname \
86198186Sgordon		rcvar required_dirs required_files required_vars
86298186Sgordon	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
86398186Sgordon
86478344Sobrien	case "$_file" in
865153105Sdougb	/etc/rc.d/*.sh)			# run in current shell
866146490Sschweikh		set $_arg; . $_file
86778344Sobrien		;;
868153105Sdougb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
86998186Sgordon		warn "Ignoring scratch file $_file"
87098186Sgordon		;;
87178344Sobrien	*)				# run in subshell
87298186Sgordon		if [ -x $_file ]; then
87398186Sgordon			if [ -n "$rc_fast_and_loose" ]; then
874146490Sschweikh				set $_arg; . $_file
87598186Sgordon			else
876130161Smtm				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
877130161Smtm				  trap "echo Script $_file interrupted; exit 1" 2
878146490Sschweikh				  set $_arg; . $_file )
87998186Sgordon			fi
88098186Sgordon		fi
88178344Sobrien		;;
88278344Sobrien	esac
88378344Sobrien}
88478344Sobrien
88578344Sobrien#
886157653Sflz# load_rc_config name
887157653Sflz#	Source in the configuration file for a given name.
88878344Sobrien#
88978344Sobrienload_rc_config()
89078344Sobrien{
891157653Sflz	_name=$1
892157653Sflz	if [ -z "$_name" ]; then
893157653Sflz		err 3 'USAGE: load_rc_config name'
89478344Sobrien	fi
89578344Sobrien
896126285Smtm	if ${_rc_conf_loaded:-false}; then
897126285Smtm		:
898126285Smtm	else
89998186Sgordon		if [ -r /etc/defaults/rc.conf ]; then
90098186Sgordon			debug "Sourcing /etc/defaults/rc.conf"
90198186Sgordon			. /etc/defaults/rc.conf
90298186Sgordon			source_rc_confs
90398186Sgordon		elif [ -r /etc/rc.conf ]; then
90498186Sgordon			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
90598186Sgordon			. /etc/rc.conf
90698186Sgordon		fi
907126285Smtm		_rc_conf_loaded=true
90898186Sgordon	fi
909161530Sflz	if [ -f /etc/rc.conf.d/"$_name" ]; then
910157653Sflz		debug "Sourcing /etc/rc.conf.d/${_name}"
911161530Sflz		. /etc/rc.conf.d/"$_name"
912157653Sflz	fi
913157653Sflz
914101850Sgordon	# XXX - Deprecated variable name support
915101850Sgordon	#
916103018Sgordon	case ${OSTYPE} in
917101850Sgordon	FreeBSD)
918146490Sschweikh		[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
919146490Sschweikh		[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
920146490Sschweikh		[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
921146490Sschweikh		[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
922146490Sschweikh		[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
923146490Sschweikh		[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
924146490Sschweikh		[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
925115950Smtm		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
926115950Smtm		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
927146490Sschweikh		;;
928101850Sgordon	esac
92978344Sobrien}
930157473Sflz  
931157473Sflz#
932157653Sflz# load_rc_config_var name var
933157653Sflz#	Read the rc.conf(5) var for name and set in the
934157473Sflz#	current shell, using load_rc_config in a subshell to prevent
935157473Sflz#	unwanted side effects from other variable assignments.
936157473Sflz#
937157473Sflzload_rc_config_var()
938157473Sflz{
939157473Sflz	if [ $# -ne 2 ]; then
940157653Sflz		err 3 'USAGE: load_rc_config_var name var'
941157473Sflz	fi
942157473Sflz	eval $(eval '(
943157473Sflz		load_rc_config '$1' >/dev/null;
944157473Sflz                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
945157473Sflz			echo '$2'=\'\''${'$2'}\'\'';
946157473Sflz		fi
947157473Sflz	)' )
948157473Sflz}
94978344Sobrien
95078344Sobrien#
95178344Sobrien# rc_usage commands
95278344Sobrien#	Print a usage string for $0, with `commands' being a list of
95378344Sobrien#	valid commands.
95478344Sobrien#
95578344Sobrienrc_usage()
95678344Sobrien{
957126303Smtm	echo -n 1>&2 "Usage: $0 [fast|force|one]("
95878344Sobrien
95978344Sobrien	_sep=
960126286Smtm	for _elem; do
96178344Sobrien		echo -n 1>&2 "$_sep$_elem"
96278344Sobrien		_sep="|"
96378344Sobrien	done
96478344Sobrien	echo 1>&2 ")"
96578344Sobrien	exit 1
96678344Sobrien}
96778344Sobrien
96878344Sobrien#
96978344Sobrien# err exitval message
97078344Sobrien#	Display message to stderr and log to the syslog, and exit with exitval.
97178344Sobrien#
97278344Sobrienerr()
97378344Sobrien{
97478344Sobrien	exitval=$1
97578344Sobrien	shift
97678344Sobrien
977106643Sgordon	if [ -x /usr/bin/logger ]; then
978106643Sgordon		logger "$0: ERROR: $*"
979106643Sgordon	fi
980106643Sgordon	echo 1>&2 "$0: ERROR: $*"
98178344Sobrien	exit $exitval
98278344Sobrien}
98378344Sobrien
98478344Sobrien#
98578344Sobrien# warn message
98678344Sobrien#	Display message to stderr and log to the syslog.
98778344Sobrien#
98878344Sobrienwarn()
98978344Sobrien{
990106643Sgordon	if [ -x /usr/bin/logger ]; then
991106643Sgordon		logger "$0: WARNING: $*"
992106643Sgordon	fi
993106643Sgordon	echo 1>&2 "$0: WARNING: $*"
99478344Sobrien}
99598186Sgordon
99698186Sgordon#
99798186Sgordon# info message
99898186Sgordon#	Display informational message to stdout and log to syslog.
99998186Sgordon#
100098186Sgordoninfo()
100198186Sgordon{
1002119170Smtm	case ${rc_info} in
1003119170Smtm	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1004119170Smtm		if [ -x /usr/bin/logger ]; then
1005119170Smtm			logger "$0: INFO: $*"
1006119170Smtm		fi
1007119170Smtm		echo "$0: INFO: $*"
1008119170Smtm		;;
1009119170Smtm	esac
101098186Sgordon}
101198186Sgordon
101298186Sgordon#
101398186Sgordon# debug message
1014106643Sgordon#	If debugging is enabled in rc.conf output message to stderr.
101598186Sgordon#	BEWARE that you don't call any subroutine that itself calls this
101698186Sgordon#	function.
101798186Sgordon#
101898186Sgordondebug()
101998186Sgordon{
102098186Sgordon	case ${rc_debug} in
102198186Sgordon	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1022106700Sgordon		if [ -x /usr/bin/logger ]; then
1023162947Syar			logger "$0: DEBUG: $*"
1024106700Sgordon		fi
1025146490Sschweikh		echo 1>&2 "$0: DEBUG: $*"
102698186Sgordon		;;
102798186Sgordon	esac
102898186Sgordon}
102998186Sgordon
103098186Sgordon#
103198186Sgordon# backup_file action file cur backup
103298186Sgordon#	Make a backup copy of `file' into `cur', and save the previous
103398186Sgordon#	version of `cur' as `backup' or use rcs for archiving.
103498186Sgordon#
103598186Sgordon#	This routine checks the value of the backup_uses_rcs variable,
103698186Sgordon#	which can be either YES or NO.
103798186Sgordon#
103898186Sgordon#	The `action' keyword can be one of the following:
103998186Sgordon#
104098186Sgordon#	add		`file' is now being backed up (and is possibly
104198186Sgordon#			being reentered into the backups system).  `cur'
104298186Sgordon#			is created and RCS files, if necessary, are
104398186Sgordon#			created as well.
104498186Sgordon#
104598186Sgordon#	update		`file' has changed and needs to be backed up.
104698186Sgordon#			If `cur' exists, it is copied to to `back' or
104798186Sgordon#			checked into RCS (if the repository file is old),
104898186Sgordon#			and then `file' is copied to `cur'.  Another RCS
104998186Sgordon#			check in done here if RCS is being used.
105098186Sgordon#
105198186Sgordon#	remove		`file' is no longer being tracked by the backups
105298186Sgordon#			system.  If RCS is not being used, `cur' is moved
105398186Sgordon#			to `back', otherwise an empty file is checked in,
105498186Sgordon#			and then `cur' is removed.
105598186Sgordon#
105698186Sgordon#
105798186Sgordonbackup_file()
105898186Sgordon{
105998186Sgordon	_action=$1
106098186Sgordon	_file=$2
106198186Sgordon	_cur=$3
106298186Sgordon	_back=$4
106398186Sgordon
106498186Sgordon	if checkyesno backup_uses_rcs; then
106598186Sgordon		_msg0="backup archive"
106698186Sgordon		_msg1="update"
106798186Sgordon
106898186Sgordon		# ensure that history file is not locked
106998186Sgordon		if [ -f $_cur,v ]; then
107098186Sgordon			rcs -q -u -U -M $_cur
107198186Sgordon		fi
107298186Sgordon
107398186Sgordon		# ensure after switching to rcs that the
107498186Sgordon		# current backup is not lost
107598186Sgordon		if [ -f $_cur ]; then
107698186Sgordon			# no archive, or current newer than archive
107798186Sgordon			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
107898186Sgordon				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
107998186Sgordon				rcs -q -kb -U $_cur
108098186Sgordon				co -q -f -u $_cur
108198186Sgordon			fi
108298186Sgordon		fi
108398186Sgordon
108498186Sgordon		case $_action in
108598186Sgordon		add|update)
108698186Sgordon			cp -p $_file $_cur
108798186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
108898186Sgordon			rcs -q -kb -U $_cur
108998186Sgordon			co -q -f -u $_cur
109098186Sgordon			chown root:wheel $_cur $_cur,v
109198186Sgordon			;;
109298186Sgordon		remove)
109398186Sgordon			cp /dev/null $_cur
109498186Sgordon			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
109598186Sgordon			rcs -q -kb -U $_cur
109698186Sgordon			chown root:wheel $_cur $_cur,v
109798186Sgordon			rm $_cur
109898186Sgordon			;;
109998186Sgordon		esac
110098186Sgordon	else
110198186Sgordon		case $_action in
110298186Sgordon		add|update)
110398186Sgordon			if [ -f $_cur ]; then
110498186Sgordon				cp -p $_cur $_back
110598186Sgordon			fi
110698186Sgordon			cp -p $_file $_cur
110798186Sgordon			chown root:wheel $_cur
110898186Sgordon			;;
110998186Sgordon		remove)
111098186Sgordon			mv -f $_cur $_back
111198186Sgordon			;;
111298186Sgordon		esac
111398186Sgordon	fi
111498186Sgordon}
1115119166Smtm
1116123344Smtm# make_symlink src link
1117123344Smtm#	Make a symbolic link 'link' to src from basedir. If the
1118123344Smtm#	directory in which link is to be created does not exist
1119123344Smtm#	a warning will be displayed and an error will be returned.
1120123344Smtm#	Returns 0 on sucess, 1 otherwise.
1121119166Smtm#
1122123344Smtmmake_symlink()
1123119166Smtm{
1124123344Smtm	local src link linkdir _me
1125123344Smtm	src="$1"
1126123344Smtm	link="$2"
1127123344Smtm	linkdir="`dirname $link`"
1128123344Smtm	_me="make_symlink()"
1129119166Smtm
1130123344Smtm	if [ -z "$src" -o -z "$link" ]; then
1131123344Smtm		warn "$_me: requires two arguments."
1132119166Smtm		return 1
1133119166Smtm	fi
1134123344Smtm	if [ ! -d "$linkdir" ]; then
1135160667Syar		warn "$_me: the directory $linkdir does not exist."
1136119166Smtm		return 1
1137119166Smtm	fi
1138146490Sschweikh	if ! ln -sf $src $link; then
1139123344Smtm		warn "$_me: unable to make a symbolic link from $link to $src"
1140119166Smtm		return 1
1141119166Smtm	fi
1142119166Smtm	return 0
1143119166Smtm}
1144119166Smtm
1145119166Smtm# devfs_rulesets_from_file file
1146119166Smtm#	Reads a set of devfs commands from file, and creates
1147119166Smtm#	the specified rulesets with their rules. Returns non-zero
1148119166Smtm#	if there was an error.
1149119166Smtm#
1150119166Smtmdevfs_rulesets_from_file()
1151119166Smtm{
1152119166Smtm	local file _err _me
1153119166Smtm	file="$1"
1154119166Smtm	_me="devfs_rulesets_from_file"
1155119166Smtm	_err=0
1156119166Smtm
1157119166Smtm	if [ -z "$file" ]; then
1158119166Smtm		warn "$_me: you must specify a file"
1159119166Smtm		return 1
1160119166Smtm	fi
1161119166Smtm	if [ ! -e "$file" ]; then
1162119166Smtm		debug "$_me: no such file ($file)"
1163119166Smtm		return 0
1164119166Smtm	fi
1165119166Smtm	debug "reading rulesets from file ($file)"
1166119166Smtm	{ while read line
1167119166Smtm	do
1168119166Smtm		case $line in
1169119166Smtm		\#*)
1170119166Smtm			continue
1171119166Smtm			;;
1172119166Smtm		\[*\]*)
1173119166Smtm			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1174119166Smtm			if [ -z "$rulenum" ]; then
1175119166Smtm				warn "$_me: cannot extract rule number ($line)"
1176119166Smtm				_err=1
1177119166Smtm				break
1178119166Smtm			fi
1179119166Smtm			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1180119166Smtm			if [ -z "$rulename" ]; then
1181119166Smtm				warn "$_me: cannot extract rule name ($line)"
1182119166Smtm				_err=1
1183119166Smtm				break;
1184119166Smtm			fi
1185119166Smtm			eval $rulename=\$rulenum
1186119166Smtm			debug "found ruleset: $rulename=$rulenum"
1187146490Sschweikh			if ! /sbin/devfs rule -s $rulenum delset; then
1188119166Smtm				_err=1
1189119166Smtm				break
1190119166Smtm			fi
1191119166Smtm			;;
1192119166Smtm		*)
1193119166Smtm			rulecmd="${line%%"\#*"}"
1194119166Smtm			# evaluate the command incase it includes
1195119166Smtm			# other rules
1196119166Smtm			if [ -n "$rulecmd" ]; then
1197119166Smtm				debug "adding rule ($rulecmd)"
1198119166Smtm				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1199119166Smtm				then
1200119166Smtm					_err=1
1201119166Smtm					break
1202119166Smtm				fi
1203119166Smtm			fi
1204119166Smtm			;;
1205119166Smtm		esac
1206119166Smtm		if [ $_err -ne 0 ]; then
1207119166Smtm			debug "error in $_me"
1208119166Smtm			break
1209119166Smtm		fi
1210119166Smtm	done } < $file
1211119166Smtm	return $_err
1212119166Smtm}
1213119166Smtm
1214119166Smtm# devfs_init_rulesets
1215119166Smtm#	Initializes rulesets from configuration files. Returns
1216119166Smtm#	non-zero if there was an error.
1217119166Smtm#
1218119166Smtmdevfs_init_rulesets()
1219119166Smtm{
1220119166Smtm	local file _me
1221119166Smtm	_me="devfs_init_rulesets"
1222119166Smtm
1223119166Smtm	# Go through this only once
1224119166Smtm	if [ -n "$devfs_rulesets_init" ]; then
1225119166Smtm		debug "$_me: devfs rulesets already initialized"
1226119166Smtm		return
1227119166Smtm	fi
1228146490Sschweikh	for file in $devfs_rulesets; do
1229119166Smtm		devfs_rulesets_from_file $file || return 1
1230119166Smtm	done
1231119166Smtm	devfs_rulesets_init=1
1232119166Smtm	debug "$_me: devfs rulesets initialized"
1233119166Smtm	return 0
1234119166Smtm}
1235119166Smtm
1236119166Smtm# devfs_set_ruleset ruleset [dir]
1237151619Smaxim#	Sets the default ruleset of dir to ruleset. The ruleset argument
1238119166Smtm#	must be a ruleset name as specified in devfs.rules(5) file.
1239119166Smtm#	Returns non-zero if it could not set it successfully.
1240119166Smtm#
1241119166Smtmdevfs_set_ruleset()
1242119166Smtm{
1243119166Smtm	local devdir rs _me
1244119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1245119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1246119166Smtm	_me="devfs_set_ruleset"
1247119166Smtm
1248119166Smtm	if [ -z "$rs" ]; then
1249119166Smtm		warn "$_me: you must specify a ruleset number"
1250119166Smtm		return 1
1251119166Smtm	fi
1252119166Smtm	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1253146490Sschweikh	if ! /sbin/devfs $devdir ruleset $rs; then
1254119166Smtm		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1255119166Smtm		return 1
1256119166Smtm	fi
1257119166Smtm	return 0
1258119166Smtm}
1259119166Smtm
1260119166Smtm# devfs_apply_ruleset ruleset [dir]
1261119166Smtm#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1262119166Smtm#	The ruleset argument must be a ruleset name as specified
1263119166Smtm#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1264119166Smtm#	if it could not apply the ruleset.
1265119166Smtm#
1266119166Smtmdevfs_apply_ruleset()
1267119166Smtm{
1268119166Smtm	local devdir rs _me
1269119166Smtm	[ -n "$1" ] && eval rs=\$$1 || rs=
1270119166Smtm	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1271119166Smtm	_me="devfs_apply_ruleset"
1272119166Smtm
1273119166Smtm	if [ -z "$rs" ]; then
1274119166Smtm		warn "$_me: you must specify a ruleset"
1275119166Smtm		return 1
1276119166Smtm	fi
1277119166Smtm	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1278146490Sschweikh	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1279119166Smtm		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1280119166Smtm		return 1
1281119166Smtm	fi
1282119166Smtm	return 0
1283119166Smtm}
1284119166Smtm
1285119166Smtm# devfs_domount dir [ruleset]
1286119166Smtm#	Mount devfs on dir. If ruleset is specified it is set
1287119166Smtm#	on the mount-point. It must also be a ruleset name as specified
1288119166Smtm#	in a devfs.rules(5) file. Returns 0 on success.
1289119166Smtm#
1290119166Smtmdevfs_domount()
1291119166Smtm{
1292119166Smtm	local devdir rs _me
1293119166Smtm	devdir="$1"
1294119166Smtm	[ -n "$2" ] && rs=$2 || rs=
1295119166Smtm	_me="devfs_domount()"
1296119166Smtm
1297119166Smtm	if [ -z "$devdir" ]; then
1298119166Smtm		warn "$_me: you must specify a mount-point"
1299119166Smtm		return 1
1300119166Smtm	fi
1301119166Smtm	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1302146490Sschweikh	if ! mount -t devfs dev "$devdir"; then
1303119166Smtm		warn "$_me: Unable to mount devfs on $devdir"
1304119166Smtm		return 1
1305119166Smtm	fi
1306119166Smtm	if [ -n "$rs" ]; then
1307119166Smtm		devfs_init_rulesets
1308119166Smtm		devfs_set_ruleset $rs $devdir
1309124797Scperciva		devfs -m $devdir rule applyset
1310119166Smtm	fi
1311119166Smtm	return 0
1312119166Smtm}
1313119166Smtm
1314119166Smtm# devfs_mount_jail dir [ruleset]
1315119166Smtm#	Mounts a devfs file system appropriate for jails
1316119166Smtm#	on the directory dir. If ruleset is specified, the ruleset
1317119166Smtm#	it names will be used instead.  If present, ruleset must
1318119166Smtm#	be the name of a ruleset as defined in a devfs.rules(5) file.
1319119166Smtm#	This function returns non-zero if an error occurs.
1320119166Smtm#
1321119166Smtmdevfs_mount_jail()
1322119166Smtm{
1323119166Smtm	local jdev rs _me
1324119166Smtm	jdev="$1"
1325119166Smtm	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1326119166Smtm	_me="devfs_mount_jail"
1327119166Smtm
1328119166Smtm	devfs_init_rulesets
1329146490Sschweikh	if ! devfs_domount "$jdev" $rs; then
1330119166Smtm		warn "$_me: devfs was not mounted on $jdev"
1331119166Smtm		return 1
1332119166Smtm	fi
1333119166Smtm	return 0
1334119166Smtm}
1335127345Sbrooks
1336127345Sbrooks# Provide a function for normalizing the mounting of memory
1337127345Sbrooks# filesystems.  This should allow the rest of the code here to remain
1338127345Sbrooks# as close as possible between 5-current and 4-stable.
1339127345Sbrooks#   $1 = size
1340127345Sbrooks#   $2 = mount point
1341137451Skeramida#   $3 = (optional) extra mdmfs flags
1342146490Sschweikhmount_md()
1343146490Sschweikh{
1344127345Sbrooks	if [ -n "$3" ]; then
1345137451Skeramida		flags="$3"
1346127345Sbrooks	fi
1347149421Syar	/sbin/mdmfs $flags -s $1 md $2
1348127345Sbrooks}
1349131550Scperciva
1350159828Syar# Code common to scripts that need to load a kernel module
1351159828Syar# if it isn't in the kernel yet. Syntax:
1352160666Syar#   load_kld [-e regex] [-m module] file
1353159828Syar# where -e or -m chooses the way to check if the module
1354159828Syar# is already loaded:
1355160666Syar#   regex is egrep'd in the output from `kldstat -v',
1356160666Syar#   module is passed to `kldstat -m'.
1357160666Syar# The default way is as though `-m file' were specified.
1358159828Syarload_kld()
1359159828Syar{
1360159828Syar	local _loaded _mod _opt _re
1361159828Syar
1362159828Syar	while getopts "e:m:" _opt; do
1363159828Syar		case "$_opt" in
1364159828Syar		e) _re="$OPTARG" ;;
1365159828Syar		m) _mod="$OPTARG" ;;
1366160666Syar		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1367159828Syar		esac
1368159828Syar	done
1369159828Syar	shift $(($OPTIND - 1))
1370160666Syar	if [ $# -ne 1 ]; then
1371160666Syar		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1372160666Syar	fi
1373159828Syar	_mod=${_mod:-$1}
1374159828Syar	_loaded=false
1375159828Syar	if [ -n "$_re" ]; then
1376159828Syar		if kldstat -v | egrep -q -e "$_re"; then
1377159828Syar			_loaded=true
1378159828Syar		fi
1379159828Syar	else
1380159828Syar		if kldstat -q -m "$_mod"; then
1381159828Syar			_loaded=true
1382159828Syar		fi
1383159828Syar	fi
1384159828Syar	if ! $_loaded; then
1385159828Syar		if ! kldload "$1"; then
1386159828Syar			warn "Unable to load kernel module $1"
1387159828Syar			return 1
1388160666Syar		else
1389160666Syar			info "$1 kernel module loaded."
1390159828Syar		fi
1391160666Syar	else
1392160666Syar		debug "load_kld: $1 kernel module already loaded."
1393159828Syar	fi
1394159828Syar	return 0
1395159828Syar}
1396159828Syar
1397149049Spjd# ltr str src dst
1398149049Spjd#	Change every $src in $str to $dst.
1399149049Spjd#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1400149049Spjd#	awk(1).
1401149049Spjdltr()
1402149049Spjd{
1403149049Spjd	local _str _src _dst _out _com
1404149049Spjd	_str=$1
1405149049Spjd	_src=$2
1406149049Spjd	_dst=$3
1407149049Spjd	_out=""
1408149049Spjd
1409149049Spjd	IFS=${_src}
1410149049Spjd	for _com in ${_str}; do
1411149049Spjd		if [ -z "${_out}" ]; then
1412149049Spjd			_out="${_com}"
1413149049Spjd		else
1414149049Spjd			_out="${_out}${_dst}${_com}"
1415149049Spjd		fi
1416149049Spjd	done
1417149049Spjd	echo "${_out}"
1418149049Spjd}
1419149049Spjd
1420149050Spjd# Creates a list of providers for GELI encryption.
1421149050Spjdgeli_make_list()
1422149050Spjd{
1423149050Spjd	local devices devices2
1424149050Spjd	local provider mountpoint type options rest
1425149050Spjd
1426149050Spjd	# Create list of GELI providers from fstab.
1427149050Spjd	while read provider mountpoint type options rest ; do
1428155570Sflz		case ":${options}" in
1429155570Sflz		:*noauto*)
1430155570Sflz			noauto=yes
1431155570Sflz			;;
1432155570Sflz		*)
1433155570Sflz			noauto=no
1434155570Sflz			;;
1435155570Sflz		esac
1436155570Sflz
1437149050Spjd		case ":${provider}" in
1438149050Spjd		:#*)
1439149050Spjd			continue
1440149050Spjd			;;
1441149050Spjd		*.eli)
1442149050Spjd			# Skip swap devices.
1443155570Sflz			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1444149050Spjd				continue
1445149050Spjd			fi
1446149050Spjd			devices="${devices} ${provider}"
1447149050Spjd			;;
1448149050Spjd		esac
1449149050Spjd	done < /etc/fstab
1450149050Spjd
1451149050Spjd	# Append providers from geli_devices.
1452149050Spjd	devices="${devices} ${geli_devices}"
1453149050Spjd
1454149050Spjd	for provider in ${devices}; do
1455149050Spjd		provider=${provider%.eli}
1456149050Spjd		provider=${provider#/dev/}
1457149050Spjd		devices2="${devices2} ${provider}"
1458149050Spjd	done
1459149050Spjd
1460149050Spjd	echo ${devices2}
1461149050Spjd}
1462149050Spjd
1463153027Sdougb# Find scripts in local_startup directories that use the old syntax
1464153027Sdougb#
1465153027Sdougbfind_local_scripts_old () {
1466153027Sdougb	zlist=''
1467153027Sdougb	slist=''
1468153027Sdougb	for dir in ${local_startup}; do
1469153027Sdougb		if [ -d "${dir}" ]; then
1470153027Sdougb			for file in ${dir}/[0-9]*.sh; do
1471153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1472153027Sdougb				    continue
1473153027Sdougb				zlist="$zlist $file"
1474153027Sdougb			done
1475153027Sdougb			for file in ${dir}/[^0-9]*.sh; do
1476153027Sdougb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1477153027Sdougb				    continue
1478153027Sdougb				slist="$slist $file"
1479153027Sdougb			done
1480153027Sdougb		fi
1481153027Sdougb	done
1482153027Sdougb}
1483153027Sdougb
1484153027Sdougbfind_local_scripts_new () {
1485153027Sdougb	local_rc=''
1486153027Sdougb	for dir in ${local_startup}; do
1487153027Sdougb		if [ -d "${dir}" ]; then
1488153297Sdougb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1489153027Sdougb				case "$file" in
1490153027Sdougb				*.sample) ;;
1491153027Sdougb				*)	if [ -x "$file" ]; then
1492153027Sdougb						local_rc="${local_rc} ${file}"
1493153027Sdougb					fi
1494153027Sdougb					;;
1495153027Sdougb				esac
1496153027Sdougb			done
1497153027Sdougb		fi
1498153027Sdougb	done
1499153027Sdougb}
1500153027Sdougb
1501165565Syar# check_required_{before|after} command
1502165565Syar#	Check for things required by the command before and after its precmd,
1503165565Syar#	respectively.  The two separate functions are needed because some
1504165565Syar#	conditions should prevent precmd from being run while other things
1505165565Syar#	depend on precmd having already been run.
1506165565Syar#
1507165565Syarcheck_required_before()
1508165565Syar{
1509165565Syar	local _f
1510165565Syar
1511165565Syar	case "$1" in
1512165565Syar	start)
1513165565Syar		for _f in $required_vars; do
1514165565Syar			if ! checkyesno $_f; then
1515165565Syar				warn "\$${_f} is not enabled."
1516165565Syar				if [ -z "$rc_force" ]; then
1517165565Syar					return 1
1518165565Syar				fi
1519165565Syar			fi
1520165565Syar		done
1521165565Syar
1522165565Syar		for _f in $required_dirs; do
1523165565Syar			if [ ! -d "${_f}/." ]; then
1524165565Syar				warn "${_f} is not a directory."
1525165565Syar				if [ -z "$rc_force" ]; then
1526165565Syar					return 1
1527165565Syar				fi
1528165565Syar			fi
1529165565Syar		done
1530165565Syar
1531165565Syar		for _f in $required_files; do
1532165565Syar			if [ ! -r "${_f}" ]; then
1533165565Syar				warn "${_f} is not readable."
1534165565Syar				if [ -z "$rc_force" ]; then
1535165565Syar					return 1
1536165565Syar				fi
1537165565Syar			fi
1538165565Syar		done
1539165565Syar		;;
1540165565Syar	esac
1541165565Syar
1542165565Syar	return 0
1543165565Syar}
1544165565Syar
1545165565Syarcheck_required_after()
1546165565Syar{
1547165565Syar	local _f _args
1548165565Syar
1549165565Syar	case "$1" in
1550165565Syar	start)
1551165565Syar		for _f in $required_modules; do
1552165565Syar			case "${_f}" in
1553165565Syar				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1554165565Syar				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1555165565Syar				*)	_args="${_f}" ;;
1556165565Syar			esac
1557165565Syar			if ! load_kld ${_args}; then
1558165565Syar				if [ -z "$rc_force" ]; then
1559165565Syar					return 1
1560165565Syar				fi
1561165565Syar			fi
1562165565Syar		done
1563165565Syar		;;
1564165565Syar	esac
1565165565Syar
1566165565Syar	return 0
1567165565Syar}
1568165565Syar
1569131550Scpercivafi
1570157841Sflz
1571157841Sflz_rc_subr_loaded=:
1572