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