rc.subr revision 165565
1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
2# $FreeBSD: head/etc/rc.subr 165565 2006-12-27 13:15:33Z yar $
3#
4# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Luke Mewburn.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18# 3. All advertising materials mentioning features or use of this software
19#    must display the following acknowledgement:
20#        This product includes software developed by the NetBSD
21#        Foundation, Inc. and its contributors.
22# 4. Neither the name of The NetBSD Foundation nor the names of its
23#    contributors may be used to endorse or promote products derived
24#    from this software without specific prior written permission.
25#
26# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36# POSSIBILITY OF SUCH DAMAGE.
37#
38# rc.subr
39#	functions used by various rc scripts
40#
41
42: ${rcvar_manpage:='rc.conf(5)'}
43
44#
45#	Operating System dependent/independent variables
46#
47
48if [ -z "${_rc_subr_loaded}" ]; then
49
50_rc_subr_loaded="YES"
51
52SYSCTL="/sbin/sysctl"
53SYSCTL_N="${SYSCTL} -n"
54CMD_OSTYPE="${SYSCTL_N} kern.ostype"
55OSTYPE=`${CMD_OSTYPE}`
56ID="/usr/bin/id"
57IDCMD="if [ -x $ID ]; then $ID -un; fi"
58PS="/bin/ps -ww"
59JID=`$PS -p $$ -o jid=`
60
61case ${OSTYPE} in
62FreeBSD)
63	SYSCTL_W="${SYSCTL}"
64	;;
65NetBSD)
66	SYSCTL_W="${SYSCTL} -w"
67	;;
68esac
69
70#
71#	functions
72#	---------
73
74#
75# set_rcvar base_var
76#	Set the variable name enabling a specific service.
77#	FreeBSD uses ${service}_enable, while NetBSD uses
78#	just the name of the service. For example:
79#	FreeBSD: sendmail_enable="YES"
80#	NetBSD : sendmail="YES"
81#	$1 - if $name is not the base to work of off, specify
82#	     a different one
83#
84set_rcvar()
85{
86	if [ -z "$1" ]; then
87		base_var=${name}
88	else
89		base_var="$1"
90	fi
91
92	case ${OSTYPE} in
93	FreeBSD)
94		echo ${base_var}_enable
95		;;
96	NetBSD)
97		echo ${base_var}
98		;;
99	*)
100		echo 'XXX'
101		;;
102	esac
103}
104
105#
106# force_depend script
107#	Force a service to start. Intended for use by services
108#	to resolve dependency issues. It is assumed the caller
109#	has check to make sure this call is necessary
110#	$1 - filename of script, in /etc/rc.d, to run
111#
112force_depend()
113{
114	_depend="$1"
115
116	info "${name} depends on ${_depend}, which will be forced to start."
117	if ! /etc/rc.d/${_depend} forcestart; then
118		warn "Unable to force ${_depend}. It may already be running."
119		return 1
120	fi
121	return 0
122}
123
124#
125# checkyesno var
126#	Test $1 variable, and warn if not set to YES or NO.
127#	Return 0 if it's "yes" (et al), nonzero otherwise.
128#
129checkyesno()
130{
131	eval _value=\$${1}
132	debug "checkyesno: $1 is set to $_value."
133	case $_value in
134
135		#	"yes", "true", "on", or "1"
136	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
137		return 0
138		;;
139
140		#	"no", "false", "off", or "0"
141	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
142		return 1
143		;;
144	*)
145		warn "\$${1} is not set properly - see ${rcvar_manpage}."
146		return 1
147		;;
148	esac
149}
150
151#
152# reverse_list list
153#	print the list in reverse order
154#
155reverse_list()
156{
157	_revlist=
158	for _revfile; do
159		_revlist="$_revfile $_revlist"
160	done
161	echo $_revlist
162}
163
164#
165# mount_critical_filesystems type
166#	Go through the list of critical filesystems as provided in
167#	the rc.conf(5) variable $critical_filesystems_${type}, checking
168#	each one to see if it is mounted, and if it is not, mounting it.
169#
170mount_critical_filesystems()
171{
172	eval _fslist=\$critical_filesystems_${1}
173	for _fs in $_fslist; do
174		mount | (
175			_ismounted=false
176			while read what _on on _type type; do
177				if [ $on = $_fs ]; then
178					_ismounted=true
179				fi
180			done
181			if $_ismounted; then
182				:
183			else
184				mount $_fs >/dev/null 2>&1
185			fi
186		)
187	done
188}
189
190#
191# check_pidfile pidfile procname [interpreter]
192#	Parses the first line of pidfile for a PID, and ensures
193#	that the process is running and matches procname.
194#	Prints the matching PID upon success, nothing otherwise.
195#	interpreter is optional; see _find_processes() for details.
196#
197check_pidfile()
198{
199	_pidfile=$1
200	_procname=$2
201	_interpreter=$3
202	if [ -z "$_pidfile" -o -z "$_procname" ]; then
203		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
204	fi
205	if [ ! -f $_pidfile ]; then
206		debug "pid file ($_pidfile): not readable."
207		return
208	fi
209	read _pid _junk < $_pidfile
210	if [ -z "$_pid" ]; then
211		debug "pid file ($_pidfile): no pid in file."
212		return
213	fi
214	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
215}
216
217#
218# check_process procname [interpreter]
219#	Ensures that a process (or processes) named procname is running.
220#	Prints a list of matching PIDs.
221#	interpreter is optional; see _find_processes() for details.
222#
223check_process()
224{
225	_procname=$1
226	_interpreter=$2
227	if [ -z "$_procname" ]; then
228		err 3 'USAGE: check_process procname [interpreter]'
229	fi
230	_find_processes $_procname ${_interpreter:-.} '-ax'
231}
232
233#
234# _find_processes procname interpreter psargs
235#	Search for procname in the output of ps generated by psargs.
236#	Prints the PIDs of any matching processes, space separated.
237#
238#	If interpreter == ".", check the following variations of procname
239#	against the first word of each command:
240#		procname
241#		`basename procname`
242#		`basename procname` + ":"
243#		"(" + `basename procname` + ")"
244#		"[" + `basename procname` + "]"
245#
246#	If interpreter != ".", read the first line of procname, remove the
247#	leading #!, normalise whitespace, append procname, and attempt to
248#	match that against each command, either as is, or with extra words
249#	at the end.  As an alternative, to deal with interpreted daemons
250#	using perl, the basename of the interpreter plus a colon is also
251#	tried as the prefix to procname.
252#
253_find_processes()
254{
255	if [ $# -ne 3 ]; then
256		err 3 'USAGE: _find_processes procname interpreter psargs'
257	fi
258	_procname=$1
259	_interpreter=$2
260	_psargs=$3
261
262	_pref=
263	if [ $_interpreter != "." ]; then	# an interpreted script
264		read _interp < ${_chroot:-}/$_procname	# read interpreter name
265		_interp=${_interp#\#!}		# strip #!
266		set -- $_interp
267		if [ $_interpreter != $1 ]; then
268			warn "\$command_interpreter $_interpreter != $1"
269		fi
270		_interp="$* $_procname"		# cleanup spaces, add _procname
271		_interpbn=${1##*/}
272		_fp_args='_argv'
273		_fp_match='case "$_argv" in
274		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
275	else					# a normal daemon
276		_procnamebn=${_procname##*/}
277		_fp_args='_arg0 _argv'
278		_fp_match='case "$_arg0" in
279		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
280	fi
281
282	_proccheck="\
283		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
284		while read _npid _jid '"$_fp_args"'; do
285			'"$_fp_match"'
286				if [ "$JID" -eq "$_jid" ];
287				then echo -n "$_pref$_npid";
288				_pref=" ";
289				fi
290				;;
291			esac
292		done'
293
294#	debug "in _find_processes: proccheck is ($_proccheck)."
295	eval $_proccheck
296}
297
298#
299# wait_for_pids pid [pid ...]
300#	spins until none of the pids exist
301#
302wait_for_pids()
303{
304	_list="$@"
305	if [ -z "$_list" ]; then
306		return
307	fi
308	_prefix=
309	while true; do
310		_nlist="";
311		for _j in $_list; do
312			if kill -0 $_j 2>/dev/null; then
313				_nlist="${_nlist}${_nlist:+ }$_j"
314			fi
315		done
316		if [ -z "$_nlist" ]; then
317			break
318		fi
319		_list=$_nlist
320		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
321		_prefix=", "
322		sleep 2
323	done
324	if [ -n "$_prefix" ]; then
325		echo "."
326	fi
327}
328
329#
330# run_rc_command argument
331#	Search for argument in the list of supported commands, which is:
332#		"start stop restart rcvar status poll ${extra_commands}"
333#	If there's a match, run ${argument}_cmd or the default method
334#	(see below).
335#
336#	If argument has a given prefix, then change the operation as follows:
337#		Prefix	Operation
338#		------	---------
339#		fast	Skip the pid check, and set rc_fast=yes
340#		force	Set ${rcvar} to YES, and set rc_force=yes
341#		one	Set ${rcvar} to YES
342#
343#	The following globals are used:
344#
345#	Name		Needed	Purpose
346#	----		------	-------
347#	name		y	Name of script.
348#
349#	command		n	Full path to command.
350#				Not needed if ${rc_arg}_cmd is set for
351#				each keyword.
352#
353#	command_args	n	Optional args/shell directives for command.
354#
355#	command_interpreter n	If not empty, command is interpreted, so
356#				call check_{pidfile,process}() appropriately.
357#
358#	extra_commands	n	List of extra commands supported.
359#
360#	pidfile		n	If set, use check_pidfile $pidfile $command,
361#				otherwise use check_process $command.
362#				In either case, only check if $command is set.
363#
364#	procname	n	Process name to check for instead of $command.
365#
366#	rcvar		n	This is checked with checkyesno to determine
367#				if the action should be run.
368#
369#	${name}_program	n	Full path to command.
370#				Meant to be used in /etc/rc.conf to override
371#				${command}.
372#
373#	${name}_chroot	n	Directory to chroot to before running ${command}
374#				Requires /usr to be mounted.
375#
376#	${name}_chdir	n	Directory to cd to before running ${command}
377#				(if not using ${name}_chroot).
378#
379#	${name}_flags	n	Arguments to call ${command} with.
380#				NOTE:	$flags from the parent environment
381#					can be used to override this.
382#
383#	${name}_nice	n	Nice level to run ${command} at.
384#
385#	${name}_user	n	User to run ${command} as, using su(1) if not
386#				using ${name}_chroot.
387#				Requires /usr to be mounted.
388#
389#	${name}_group	n	Group to run chrooted ${command} as.
390#				Requires /usr to be mounted.
391#
392#	${name}_groups	n	Comma separated list of supplementary groups
393#				to run the chrooted ${command} with.
394#				Requires /usr to be mounted.
395#
396#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
397#				Otherwise, use default command (see below)
398#
399#	${rc_arg}_precmd n	If set, run just before performing the
400#				${rc_arg}_cmd method in the default
401#				operation (i.e, after checking for required
402#				bits and process (non)existence).
403#				If this completes with a non-zero exit code,
404#				don't run ${rc_arg}_cmd.
405#
406#	${rc_arg}_postcmd n	If set, run just after performing the
407#				${rc_arg}_cmd method, if that method
408#				returned a zero exit code.
409#
410#	required_dirs	n	If set, check for the existence of the given
411#				directories before running a (re)start command.
412#
413#	required_files	n	If set, check for the readability of the given
414#				files before running a (re)start command.
415#
416#	required_modules n	If set, ensure the given kernel modules are
417#				loaded before running a (re)start command.
418#				The check and possible loads are actually
419#				done after start_precmd so that the modules
420#				aren't loaded in vain, should the precmd
421#				return a non-zero status to indicate a error.
422#				If a word in the list looks like "foo:bar",
423#				"foo" is the KLD file name and "bar" is the
424#				module name.  If a word looks like "foo~bar",
425#				"foo" is the KLD file name and "bar" is a
426#				egrep(1) pattern matching the module name.
427#				Otherwise the module name is assumed to be
428#				the same as the KLD file name, which is most
429#				common.  See load_kld().
430#
431#	required_vars	n	If set, perform checkyesno on each of the
432#				listed variables before running the default
433#				(re)start command.
434#
435#	Default behaviour for a given argument, if no override method is
436#	provided:
437#
438#	Argument	Default behaviour
439#	--------	-----------------
440#	start		if !running && checkyesno ${rcvar}
441#				${command}
442#
443#	stop		if ${pidfile}
444#				rc_pid=$(check_pidfile $pidfile $command)
445#			else
446#				rc_pid=$(check_process $command)
447#			kill $sig_stop $rc_pid
448#			wait_for_pids $rc_pid
449#			($sig_stop defaults to TERM.)
450#
451#	reload		Similar to stop, except use $sig_reload instead,
452#			and doesn't wait_for_pids.
453#			$sig_reload defaults to HUP.
454#			Note that `reload' isn't provided by default,
455#			it should be enabled via $extra_commands.
456#
457#	restart		Run `stop' then `start'.
458#
459#	status		Show if ${command} is running, etc.
460#
461#	poll		Wait for ${command} to exit.
462#
463#	rcvar		Display what rc.conf variable is used (if any).
464#
465#	Variables available to methods, and after run_rc_command() has
466#	completed:
467#
468#	Variable	Purpose
469#	--------	-------
470#	rc_arg		Argument to command, after fast/force/one processing
471#			performed
472#
473#	rc_flags	Flags to start the default command with.
474#			Defaults to ${name}_flags, unless overridden
475#			by $flags from the environment.
476#			This variable may be changed by the precmd method.
477#
478#	rc_pid		PID of command (if appropriate)
479#
480#	rc_fast		Not empty if "fast" was provided (q.v.)
481#
482#	rc_force	Not empty if "force" was provided (q.v.)
483#
484#
485run_rc_command()
486{
487	_return=0
488	rc_arg=$1
489	if [ -z "$name" ]; then
490		err 3 'run_rc_command: $name is not set.'
491	fi
492
493	# Don't repeat the first argument when passing additional command-
494	# line arguments to the command subroutines.
495	#
496	shift 1
497	rc_extra_args="$*"
498
499	_rc_prefix=
500	case "$rc_arg" in
501	fast*)				# "fast" prefix; don't check pid
502		rc_arg=${rc_arg#fast}
503		rc_fast=yes
504		;;
505	force*)				# "force prefix; always run
506		rc_force=yes
507		_rc_prefix=force
508		rc_arg=${rc_arg#${_rc_prefix}}
509		if [ -n "${rcvar}" ]; then
510			eval ${rcvar}=YES
511		fi
512		;;
513	one*)				# "one" prefix; set ${rcvar}=yes
514		_rc_prefix=one
515		rc_arg=${rc_arg#${_rc_prefix}}
516		if [ -n "${rcvar}" ]; then
517			eval ${rcvar}=YES
518		fi
519		;;
520	esac
521
522	eval _override_command=\$${name}_program
523	command=${command:+${_override_command:-$command}}
524
525	_keywords="start stop restart rcvar $extra_commands"
526	rc_pid=
527	_pidcmd=
528	_procname=${procname:-${command}}
529
530					# setup pid check command
531	if [ -n "$_procname" ]; then
532		if [ -n "$pidfile" ]; then
533			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
534		else
535			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
536		fi
537		if [ -n "$_pidcmd" ]; then
538			_keywords="${_keywords} status poll"
539		fi
540	fi
541
542	if [ -z "$rc_arg" ]; then
543		rc_usage $_keywords
544	fi
545
546	if [ -n "$flags" ]; then	# allow override from environment
547		rc_flags=$flags
548	else
549		eval rc_flags=\$${name}_flags
550	fi
551	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
552	    _nice=\$${name}_nice	_user=\$${name}_user \
553	    _group=\$${name}_group	_groups=\$${name}_groups
554
555	if [ -n "$_user" ]; then	# unset $_user if running as that user
556		if [ "$_user" = "$(eval $IDCMD)" ]; then
557			unset _user
558		fi
559	fi
560
561					# if ${rcvar} is set, and $1 is not
562					# "rcvar", then run
563					#	checkyesno ${rcvar}
564					# and return if that failed
565					#
566	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
567		if ! checkyesno ${rcvar}; then
568			return 0
569		fi
570	fi
571
572	eval $_pidcmd			# determine the pid if necessary
573
574	for _elem in $_keywords; do
575		if [ "$_elem" != "$rc_arg" ]; then
576			continue
577		fi
578					# if there's a custom ${XXX_cmd},
579					# run that instead of the default
580					#
581		eval _cmd=\$${rc_arg}_cmd \
582		     _precmd=\$${rc_arg}_precmd \
583		     _postcmd=\$${rc_arg}_postcmd
584
585		if [ -n "$_cmd" ]; then
586			_run_rc_precmd || return 1
587			_run_rc_doit "$_cmd $rc_extra_args" || return 1
588			_run_rc_postcmd
589			return $_return
590		fi
591
592		case "$rc_arg" in	# default operations...
593
594		status)
595			_run_rc_precmd || return 1
596			if [ -n "$rc_pid" ]; then
597				echo "${name} is running as pid $rc_pid."
598			else
599				echo "${name} is not running."
600				return 1
601			fi
602			_run_rc_postcmd
603			;;
604
605		start)
606			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
607				echo 1>&2 "${name} already running? (pid=$rc_pid)."
608				return 1
609			fi
610
611			if [ ! -x ${_chroot}${command} ]; then
612				warn "run_rc_command: cannot run $command"
613				return 1
614			fi
615
616			_run_rc_precmd || return 1
617
618					# setup the full command to run
619					#
620			echo "Starting ${name}."
621			if [ -n "$_chroot" ]; then
622				_doit="\
623${_nice:+nice -n $_nice }\
624chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
625$_chroot $command $rc_flags $command_args"
626			else
627				_doit="\
628${_chdir:+cd $_chdir && }\
629$command $rc_flags $command_args"
630				if [ -n "$_user" ]; then
631				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
632				fi
633				if [ -n "$_nice" ]; then
634					if [ -z "$_user" ]; then
635						_doit="sh -c \"$_doit\""
636					fi	
637					_doit="nice -n $_nice $_doit"
638				fi
639			fi
640
641					# run the full command
642					#
643			_run_rc_doit "$_doit" || return 1
644
645					# finally, run postcmd
646					#
647			_run_rc_postcmd
648			;;
649
650		stop)
651			if [ -z "$rc_pid" ]; then
652				[ -n "$rc_fast" ] && return 0
653				_run_rc_notrunning
654				return 1
655			fi
656
657			_run_rc_precmd || return 1
658
659					# send the signal to stop
660					#
661			echo "Stopping ${name}."
662			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
663			_run_rc_doit "$_doit" || return 1
664
665					# wait for the command to exit,
666					# and run postcmd.
667			wait_for_pids $rc_pid
668
669			_run_rc_postcmd
670			;;
671
672		reload)
673			if [ -z "$rc_pid" ]; then
674				_run_rc_notrunning
675				return 1
676			fi
677
678			_run_rc_precmd || return 1
679
680			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
681			_run_rc_doit "$_doit" || return 1
682
683			_run_rc_postcmd
684			;;
685
686		restart)
687					# prevent restart being called more
688					# than once by any given script
689					#
690			if ${_rc_restart_done:-false}; then
691				return 0
692			fi
693			_rc_restart_done=true
694
695			_run_rc_precmd || return 1
696
697			# run those in a subshell to keep global variables
698			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
699			( run_rc_command ${_rc_prefix}start $rc_extra_args )
700			_return=$?
701			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
702
703			_run_rc_postcmd
704			;;
705
706		poll)
707			_run_rc_precmd || return 1
708			if [ -n "$rc_pid" ]; then
709				wait_for_pids $rc_pid
710			fi
711			_run_rc_postcmd
712			;;
713
714		rcvar)
715			echo "# $name"
716			if [ -n "$rcvar" ]; then
717				if checkyesno ${rcvar}; then
718					echo "${rcvar}=YES"
719				else
720					echo "${rcvar}=NO"
721				fi
722			fi
723			;;
724
725		*)
726			rc_usage $_keywords
727			;;
728
729		esac
730		return $_return
731	done
732
733	echo 1>&2 "$0: unknown directive '$rc_arg'."
734	rc_usage $_keywords
735	# not reached
736}
737
738#
739# Helper functions for run_rc_command: common code.
740# They use such global variables besides the exported rc_* ones:
741#
742#	name	       R/W
743#	------------------
744#	_precmd		R
745#	_postcmd	R
746#	_return		W
747#
748_run_rc_precmd()
749{
750	check_required_before "$rc_arg" || return 1
751
752	if [ -n "$_precmd" ]; then
753		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
754		eval "$_precmd $rc_extra_args"
755		_return=$?
756
757		# If precmd failed and force isn't set, request exit.
758		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
759			return 1
760		fi
761	fi
762
763	check_required_after "$rc_arg" || return 1
764
765	return 0
766}
767
768_run_rc_postcmd()
769{
770	if [ -n "$_postcmd" ]; then
771		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
772		eval "$_postcmd $rc_extra_args"
773		_return=$?
774	fi
775	return 0
776}
777
778_run_rc_doit()
779{
780	debug "run_rc_command: doit: $*"
781	eval "$@"
782	_return=$?
783
784	# If command failed and force isn't set, request exit.
785	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
786		return 1
787	fi
788
789	return 0
790}
791
792_run_rc_notrunning()
793{
794	local _pidmsg
795
796	if [ -n "$pidfile" ]; then
797		_pidmsg=" (check $pidfile)."
798	else
799		_pidmsg=
800	fi
801	echo 1>&2 "${name} not running?${_pidmsg}"
802}
803
804_run_rc_killcmd()
805{
806	local _cmd
807
808	_cmd="kill -$1 $rc_pid"
809	if [ -n "$_user" ]; then
810		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
811	fi
812	echo "$_cmd"
813}
814
815#
816# run_rc_script file arg
817#	Start the script `file' with `arg', and correctly handle the
818#	return value from the script.  If `file' ends with `.sh', it's
819#	sourced into the current environment.  If `file' appears to be
820#	a backup or scratch file, ignore it.  Otherwise if it's
821#	executable run as a child process.
822#
823run_rc_script()
824{
825	_file=$1
826	_arg=$2
827	if [ -z "$_file" -o -z "$_arg" ]; then
828		err 3 'USAGE: run_rc_script file arg'
829	fi
830
831	unset	name command command_args command_interpreter \
832		extra_commands pidfile procname \
833		rcvar required_dirs required_files required_vars
834	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
835
836	case "$_file" in
837	/etc/rc.d/*.sh)			# run in current shell
838		set $_arg; . $_file
839		;;
840	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
841		warn "Ignoring scratch file $_file"
842		;;
843	*)				# run in subshell
844		if [ -x $_file ]; then
845			if [ -n "$rc_fast_and_loose" ]; then
846				set $_arg; . $_file
847			else
848				( trap "echo Script $_file interrupted; kill -QUIT $$" 3
849				  trap "echo Script $_file interrupted; exit 1" 2
850				  set $_arg; . $_file )
851			fi
852		fi
853		;;
854	esac
855}
856
857#
858# load_rc_config name
859#	Source in the configuration file for a given name.
860#
861load_rc_config()
862{
863	_name=$1
864	if [ -z "$_name" ]; then
865		err 3 'USAGE: load_rc_config name'
866	fi
867
868	if ${_rc_conf_loaded:-false}; then
869		:
870	else
871		if [ -r /etc/defaults/rc.conf ]; then
872			debug "Sourcing /etc/defaults/rc.conf"
873			. /etc/defaults/rc.conf
874			source_rc_confs
875		elif [ -r /etc/rc.conf ]; then
876			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
877			. /etc/rc.conf
878		fi
879		_rc_conf_loaded=true
880	fi
881	if [ -f /etc/rc.conf.d/"$_name" ]; then
882		debug "Sourcing /etc/rc.conf.d/${_name}"
883		. /etc/rc.conf.d/"$_name"
884	fi
885
886	# XXX - Deprecated variable name support
887	#
888	case ${OSTYPE} in
889	FreeBSD)
890		[ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
891		[ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
892		[ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
893		[ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
894		[ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
895		[ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
896		[ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
897		[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
898		[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
899		;;
900	esac
901}
902  
903#
904# load_rc_config_var name var
905#	Read the rc.conf(5) var for name and set in the
906#	current shell, using load_rc_config in a subshell to prevent
907#	unwanted side effects from other variable assignments.
908#
909load_rc_config_var()
910{
911	if [ $# -ne 2 ]; then
912		err 3 'USAGE: load_rc_config_var name var'
913	fi
914	eval $(eval '(
915		load_rc_config '$1' >/dev/null;
916                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
917			echo '$2'=\'\''${'$2'}\'\'';
918		fi
919	)' )
920}
921
922#
923# rc_usage commands
924#	Print a usage string for $0, with `commands' being a list of
925#	valid commands.
926#
927rc_usage()
928{
929	echo -n 1>&2 "Usage: $0 [fast|force|one]("
930
931	_sep=
932	for _elem; do
933		echo -n 1>&2 "$_sep$_elem"
934		_sep="|"
935	done
936	echo 1>&2 ")"
937	exit 1
938}
939
940#
941# err exitval message
942#	Display message to stderr and log to the syslog, and exit with exitval.
943#
944err()
945{
946	exitval=$1
947	shift
948
949	if [ -x /usr/bin/logger ]; then
950		logger "$0: ERROR: $*"
951	fi
952	echo 1>&2 "$0: ERROR: $*"
953	exit $exitval
954}
955
956#
957# warn message
958#	Display message to stderr and log to the syslog.
959#
960warn()
961{
962	if [ -x /usr/bin/logger ]; then
963		logger "$0: WARNING: $*"
964	fi
965	echo 1>&2 "$0: WARNING: $*"
966}
967
968#
969# info message
970#	Display informational message to stdout and log to syslog.
971#
972info()
973{
974	case ${rc_info} in
975	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
976		if [ -x /usr/bin/logger ]; then
977			logger "$0: INFO: $*"
978		fi
979		echo "$0: INFO: $*"
980		;;
981	esac
982}
983
984#
985# debug message
986#	If debugging is enabled in rc.conf output message to stderr.
987#	BEWARE that you don't call any subroutine that itself calls this
988#	function.
989#
990debug()
991{
992	case ${rc_debug} in
993	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
994		if [ -x /usr/bin/logger ]; then
995			logger "$0: DEBUG: $*"
996		fi
997		echo 1>&2 "$0: DEBUG: $*"
998		;;
999	esac
1000}
1001
1002#
1003# backup_file action file cur backup
1004#	Make a backup copy of `file' into `cur', and save the previous
1005#	version of `cur' as `backup' or use rcs for archiving.
1006#
1007#	This routine checks the value of the backup_uses_rcs variable,
1008#	which can be either YES or NO.
1009#
1010#	The `action' keyword can be one of the following:
1011#
1012#	add		`file' is now being backed up (and is possibly
1013#			being reentered into the backups system).  `cur'
1014#			is created and RCS files, if necessary, are
1015#			created as well.
1016#
1017#	update		`file' has changed and needs to be backed up.
1018#			If `cur' exists, it is copied to to `back' or
1019#			checked into RCS (if the repository file is old),
1020#			and then `file' is copied to `cur'.  Another RCS
1021#			check in done here if RCS is being used.
1022#
1023#	remove		`file' is no longer being tracked by the backups
1024#			system.  If RCS is not being used, `cur' is moved
1025#			to `back', otherwise an empty file is checked in,
1026#			and then `cur' is removed.
1027#
1028#
1029backup_file()
1030{
1031	_action=$1
1032	_file=$2
1033	_cur=$3
1034	_back=$4
1035
1036	if checkyesno backup_uses_rcs; then
1037		_msg0="backup archive"
1038		_msg1="update"
1039
1040		# ensure that history file is not locked
1041		if [ -f $_cur,v ]; then
1042			rcs -q -u -U -M $_cur
1043		fi
1044
1045		# ensure after switching to rcs that the
1046		# current backup is not lost
1047		if [ -f $_cur ]; then
1048			# no archive, or current newer than archive
1049			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1050				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1051				rcs -q -kb -U $_cur
1052				co -q -f -u $_cur
1053			fi
1054		fi
1055
1056		case $_action in
1057		add|update)
1058			cp -p $_file $_cur
1059			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1060			rcs -q -kb -U $_cur
1061			co -q -f -u $_cur
1062			chown root:wheel $_cur $_cur,v
1063			;;
1064		remove)
1065			cp /dev/null $_cur
1066			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1067			rcs -q -kb -U $_cur
1068			chown root:wheel $_cur $_cur,v
1069			rm $_cur
1070			;;
1071		esac
1072	else
1073		case $_action in
1074		add|update)
1075			if [ -f $_cur ]; then
1076				cp -p $_cur $_back
1077			fi
1078			cp -p $_file $_cur
1079			chown root:wheel $_cur
1080			;;
1081		remove)
1082			mv -f $_cur $_back
1083			;;
1084		esac
1085	fi
1086}
1087
1088# make_symlink src link
1089#	Make a symbolic link 'link' to src from basedir. If the
1090#	directory in which link is to be created does not exist
1091#	a warning will be displayed and an error will be returned.
1092#	Returns 0 on sucess, 1 otherwise.
1093#
1094make_symlink()
1095{
1096	local src link linkdir _me
1097	src="$1"
1098	link="$2"
1099	linkdir="`dirname $link`"
1100	_me="make_symlink()"
1101
1102	if [ -z "$src" -o -z "$link" ]; then
1103		warn "$_me: requires two arguments."
1104		return 1
1105	fi
1106	if [ ! -d "$linkdir" ]; then
1107		warn "$_me: the directory $linkdir does not exist."
1108		return 1
1109	fi
1110	if ! ln -sf $src $link; then
1111		warn "$_me: unable to make a symbolic link from $link to $src"
1112		return 1
1113	fi
1114	return 0
1115}
1116
1117# devfs_rulesets_from_file file
1118#	Reads a set of devfs commands from file, and creates
1119#	the specified rulesets with their rules. Returns non-zero
1120#	if there was an error.
1121#
1122devfs_rulesets_from_file()
1123{
1124	local file _err _me
1125	file="$1"
1126	_me="devfs_rulesets_from_file"
1127	_err=0
1128
1129	if [ -z "$file" ]; then
1130		warn "$_me: you must specify a file"
1131		return 1
1132	fi
1133	if [ ! -e "$file" ]; then
1134		debug "$_me: no such file ($file)"
1135		return 0
1136	fi
1137	debug "reading rulesets from file ($file)"
1138	{ while read line
1139	do
1140		case $line in
1141		\#*)
1142			continue
1143			;;
1144		\[*\]*)
1145			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1146			if [ -z "$rulenum" ]; then
1147				warn "$_me: cannot extract rule number ($line)"
1148				_err=1
1149				break
1150			fi
1151			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1152			if [ -z "$rulename" ]; then
1153				warn "$_me: cannot extract rule name ($line)"
1154				_err=1
1155				break;
1156			fi
1157			eval $rulename=\$rulenum
1158			debug "found ruleset: $rulename=$rulenum"
1159			if ! /sbin/devfs rule -s $rulenum delset; then
1160				_err=1
1161				break
1162			fi
1163			;;
1164		*)
1165			rulecmd="${line%%"\#*"}"
1166			# evaluate the command incase it includes
1167			# other rules
1168			if [ -n "$rulecmd" ]; then
1169				debug "adding rule ($rulecmd)"
1170				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1171				then
1172					_err=1
1173					break
1174				fi
1175			fi
1176			;;
1177		esac
1178		if [ $_err -ne 0 ]; then
1179			debug "error in $_me"
1180			break
1181		fi
1182	done } < $file
1183	return $_err
1184}
1185
1186# devfs_init_rulesets
1187#	Initializes rulesets from configuration files. Returns
1188#	non-zero if there was an error.
1189#
1190devfs_init_rulesets()
1191{
1192	local file _me
1193	_me="devfs_init_rulesets"
1194
1195	# Go through this only once
1196	if [ -n "$devfs_rulesets_init" ]; then
1197		debug "$_me: devfs rulesets already initialized"
1198		return
1199	fi
1200	for file in $devfs_rulesets; do
1201		devfs_rulesets_from_file $file || return 1
1202	done
1203	devfs_rulesets_init=1
1204	debug "$_me: devfs rulesets initialized"
1205	return 0
1206}
1207
1208# devfs_set_ruleset ruleset [dir]
1209#	Sets the default ruleset of dir to ruleset. The ruleset argument
1210#	must be a ruleset name as specified in devfs.rules(5) file.
1211#	Returns non-zero if it could not set it successfully.
1212#
1213devfs_set_ruleset()
1214{
1215	local devdir rs _me
1216	[ -n "$1" ] && eval rs=\$$1 || rs=
1217	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1218	_me="devfs_set_ruleset"
1219
1220	if [ -z "$rs" ]; then
1221		warn "$_me: you must specify a ruleset number"
1222		return 1
1223	fi
1224	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1225	if ! /sbin/devfs $devdir ruleset $rs; then
1226		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1227		return 1
1228	fi
1229	return 0
1230}
1231
1232# devfs_apply_ruleset ruleset [dir]
1233#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
1234#	The ruleset argument must be a ruleset name as specified
1235#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
1236#	if it could not apply the ruleset.
1237#
1238devfs_apply_ruleset()
1239{
1240	local devdir rs _me
1241	[ -n "$1" ] && eval rs=\$$1 || rs=
1242	[ -n "$2" ] && devdir="-m "$2"" || devdir=
1243	_me="devfs_apply_ruleset"
1244
1245	if [ -z "$rs" ]; then
1246		warn "$_me: you must specify a ruleset"
1247		return 1
1248	fi
1249	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1250	if ! /sbin/devfs $devdir rule -s $rs applyset; then
1251		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1252		return 1
1253	fi
1254	return 0
1255}
1256
1257# devfs_domount dir [ruleset]
1258#	Mount devfs on dir. If ruleset is specified it is set
1259#	on the mount-point. It must also be a ruleset name as specified
1260#	in a devfs.rules(5) file. Returns 0 on success.
1261#
1262devfs_domount()
1263{
1264	local devdir rs _me
1265	devdir="$1"
1266	[ -n "$2" ] && rs=$2 || rs=
1267	_me="devfs_domount()"
1268
1269	if [ -z "$devdir" ]; then
1270		warn "$_me: you must specify a mount-point"
1271		return 1
1272	fi
1273	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1274	if ! mount -t devfs dev "$devdir"; then
1275		warn "$_me: Unable to mount devfs on $devdir"
1276		return 1
1277	fi
1278	if [ -n "$rs" ]; then
1279		devfs_init_rulesets
1280		devfs_set_ruleset $rs $devdir
1281		devfs -m $devdir rule applyset
1282	fi
1283	return 0
1284}
1285
1286# devfs_mount_jail dir [ruleset]
1287#	Mounts a devfs file system appropriate for jails
1288#	on the directory dir. If ruleset is specified, the ruleset
1289#	it names will be used instead.  If present, ruleset must
1290#	be the name of a ruleset as defined in a devfs.rules(5) file.
1291#	This function returns non-zero if an error occurs.
1292#
1293devfs_mount_jail()
1294{
1295	local jdev rs _me
1296	jdev="$1"
1297	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1298	_me="devfs_mount_jail"
1299
1300	devfs_init_rulesets
1301	if ! devfs_domount "$jdev" $rs; then
1302		warn "$_me: devfs was not mounted on $jdev"
1303		return 1
1304	fi
1305	return 0
1306}
1307
1308# Provide a function for normalizing the mounting of memory
1309# filesystems.  This should allow the rest of the code here to remain
1310# as close as possible between 5-current and 4-stable.
1311#   $1 = size
1312#   $2 = mount point
1313#   $3 = (optional) extra mdmfs flags
1314mount_md()
1315{
1316	if [ -n "$3" ]; then
1317		flags="$3"
1318	fi
1319	/sbin/mdmfs $flags -s $1 md $2
1320}
1321
1322# Code common to scripts that need to load a kernel module
1323# if it isn't in the kernel yet. Syntax:
1324#   load_kld [-e regex] [-m module] file
1325# where -e or -m chooses the way to check if the module
1326# is already loaded:
1327#   regex is egrep'd in the output from `kldstat -v',
1328#   module is passed to `kldstat -m'.
1329# The default way is as though `-m file' were specified.
1330load_kld()
1331{
1332	local _loaded _mod _opt _re
1333
1334	while getopts "e:m:" _opt; do
1335		case "$_opt" in
1336		e) _re="$OPTARG" ;;
1337		m) _mod="$OPTARG" ;;
1338		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1339		esac
1340	done
1341	shift $(($OPTIND - 1))
1342	if [ $# -ne 1 ]; then
1343		err 3 'USAGE: load_kld [-e regex] [-m module] file'
1344	fi
1345	_mod=${_mod:-$1}
1346	_loaded=false
1347	if [ -n "$_re" ]; then
1348		if kldstat -v | egrep -q -e "$_re"; then
1349			_loaded=true
1350		fi
1351	else
1352		if kldstat -q -m "$_mod"; then
1353			_loaded=true
1354		fi
1355	fi
1356	if ! $_loaded; then
1357		if ! kldload "$1"; then
1358			warn "Unable to load kernel module $1"
1359			return 1
1360		else
1361			info "$1 kernel module loaded."
1362		fi
1363	else
1364		debug "load_kld: $1 kernel module already loaded."
1365	fi
1366	return 0
1367}
1368
1369# ltr str src dst
1370#	Change every $src in $str to $dst.
1371#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1372#	awk(1).
1373ltr()
1374{
1375	local _str _src _dst _out _com
1376	_str=$1
1377	_src=$2
1378	_dst=$3
1379	_out=""
1380
1381	IFS=${_src}
1382	for _com in ${_str}; do
1383		if [ -z "${_out}" ]; then
1384			_out="${_com}"
1385		else
1386			_out="${_out}${_dst}${_com}"
1387		fi
1388	done
1389	echo "${_out}"
1390}
1391
1392# Creates a list of providers for GELI encryption.
1393geli_make_list()
1394{
1395	local devices devices2
1396	local provider mountpoint type options rest
1397
1398	# Create list of GELI providers from fstab.
1399	while read provider mountpoint type options rest ; do
1400		case ":${options}" in
1401		:*noauto*)
1402			noauto=yes
1403			;;
1404		*)
1405			noauto=no
1406			;;
1407		esac
1408
1409		case ":${provider}" in
1410		:#*)
1411			continue
1412			;;
1413		*.eli)
1414			# Skip swap devices.
1415			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1416				continue
1417			fi
1418			devices="${devices} ${provider}"
1419			;;
1420		esac
1421	done < /etc/fstab
1422
1423	# Append providers from geli_devices.
1424	devices="${devices} ${geli_devices}"
1425
1426	for provider in ${devices}; do
1427		provider=${provider%.eli}
1428		provider=${provider#/dev/}
1429		devices2="${devices2} ${provider}"
1430	done
1431
1432	echo ${devices2}
1433}
1434
1435# Find scripts in local_startup directories that use the old syntax
1436#
1437find_local_scripts_old () {
1438	zlist=''
1439	slist=''
1440	for dir in ${local_startup}; do
1441		if [ -d "${dir}" ]; then
1442			for file in ${dir}/[0-9]*.sh; do
1443				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1444				    continue
1445				zlist="$zlist $file"
1446			done
1447			for file in ${dir}/[^0-9]*.sh; do
1448				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1449				    continue
1450				slist="$slist $file"
1451			done
1452		fi
1453	done
1454}
1455
1456find_local_scripts_new () {
1457	local_rc=''
1458	for dir in ${local_startup}; do
1459		if [ -d "${dir}" ]; then
1460			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1461				case "$file" in
1462				*.sample) ;;
1463				*)	if [ -x "$file" ]; then
1464						local_rc="${local_rc} ${file}"
1465					fi
1466					;;
1467				esac
1468			done
1469		fi
1470	done
1471}
1472
1473# check_required_{before|after} command
1474#	Check for things required by the command before and after its precmd,
1475#	respectively.  The two separate functions are needed because some
1476#	conditions should prevent precmd from being run while other things
1477#	depend on precmd having already been run.
1478#
1479check_required_before()
1480{
1481	local _f
1482
1483	case "$1" in
1484	start)
1485		for _f in $required_vars; do
1486			if ! checkyesno $_f; then
1487				warn "\$${_f} is not enabled."
1488				if [ -z "$rc_force" ]; then
1489					return 1
1490				fi
1491			fi
1492		done
1493
1494		for _f in $required_dirs; do
1495			if [ ! -d "${_f}/." ]; then
1496				warn "${_f} is not a directory."
1497				if [ -z "$rc_force" ]; then
1498					return 1
1499				fi
1500			fi
1501		done
1502
1503		for _f in $required_files; do
1504			if [ ! -r "${_f}" ]; then
1505				warn "${_f} is not readable."
1506				if [ -z "$rc_force" ]; then
1507					return 1
1508				fi
1509			fi
1510		done
1511		;;
1512	esac
1513
1514	return 0
1515}
1516
1517check_required_after()
1518{
1519	local _f _args
1520
1521	case "$1" in
1522	start)
1523		for _f in $required_modules; do
1524			case "${_f}" in
1525				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
1526				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
1527				*)	_args="${_f}" ;;
1528			esac
1529			if ! load_kld ${_args}; then
1530				if [ -z "$rc_force" ]; then
1531					return 1
1532				fi
1533			fi
1534		done
1535		;;
1536	esac
1537
1538	return 0
1539}
1540
1541fi
1542
1543_rc_subr_loaded=:
1544