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