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