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