1 # $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
4 # Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
7 # This code is derived from software contributed to The NetBSD Foundation
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
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.
19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
32 # functions used by various rc scripts
35 : ${RC_PID:=$$}; export RC_PID
38 # Operating System dependent/independent variables
41 if [ -n "${_rc_subr_loaded}" ]; then
48 SYSCTL_N="${SYSCTL} -n"
50 PROTECT="/usr/bin/protect"
52 IDCMD="if [ -x $ID ]; then $ID -un; fi"
55 # rc_service provides the path to the service script that we are executing.
56 # This is not being set here in an execution context, necessarily, so it's
57 # really just a reasonable guess, and it will get overwritten later if
58 # we are executing from some other means than direct execution by service(8)
59 # or manual invocation of the service script. The prime example of this is
60 # during system startup, all rc scripts will be invoked via /etc/rc, so
61 # run_rc_script will overwrite rc_service with the file being sourced.
69 # List vars matching pattern.
73 set | { while read LINE; do
76 "$LINE"|*[!a-zA-Z0-9_]*) continue ;;
82 # set_rcvar [var] [defval] [desc]
84 # Echo or define a rc.conf(5) variable name. Global variable
87 # If no argument is specified, echo "${name}_enable".
89 # If only a var is specified, echo "${var}_enable".
91 # If var and defval are specified, the ${var} is defined as
92 # rc.conf(5) variable and the default value is ${defvar}. An
93 # optional argument $desc can also be specified to add a
94 # description for that.
101 0) echo ${name}_enable ;;
102 1) echo ${1}_enable ;;
104 debug "set_rcvar: \$$1=$2 is added" \
105 " as a rc.conf(5) variable."
107 rcvars="${rcvars# } $_var"
108 eval ${_var}_defval=\"$2\"
110 eval ${_var}_desc=\"$*\"
115 # set_rcvar_obsolete oldvar [newvar] [msg]
116 # Define obsolete variable.
117 # Global variable $rcvars_obsolete is used.
123 debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
125 rcvars_obsolete="${rcvars_obsolete# } $1"
126 eval ${1}_newvar=\"$2\"
128 eval ${_var}_obsolete_msg=\"$*\"
132 # force_depend script [rcvar]
133 # Force a service to start. Intended for use by services
134 # to resolve dependency issues.
135 # $1 - filename of script, in /etc/rc.d, to run
136 # $2 - name of the script's rcvar (minus the _enable)
140 local _depend _dep_rcvar
143 _dep_rcvar="${2:-$1}_enable"
145 [ -n "$rc_fast" ] && ! checkyesno always_force_depends &&
146 checkyesno $_dep_rcvar && return 0
148 /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0
150 info "${name} depends on ${_depend}, which will be forced to start."
151 if ! /etc/rc.d/${_depend} forcestart; then
152 warn "Unable to force ${_depend}. It may already be running."
159 # Test $1 variable, and warn if not set to YES or NO.
160 # Return 0 if it's "yes" (et al), nonzero otherwise.
165 debug "checkyesno: $1 is set to $_value."
168 # "yes", "true", "on", or "1"
169 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
173 # "no", "false", "off", or "0"
174 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
178 warn "\$${1} is not set properly - see rc.conf(5)."
186 # print the list in reverse order
192 _revlist="$_revfile $_revlist"
198 # If booting directly to multiuser or $always is enabled,
199 # send SIGTERM to the parent (/etc/rc) to abort the boot.
200 # Otherwise just exit.
207 # "yes", "true", "on", or "1"
208 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
215 if [ "$autoboot" = yes -o "$always" = true ]; then
216 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
223 # mount_critical_filesystems type
224 # Go through the list of critical filesystems as provided in
225 # the rc.conf(5) variable $critical_filesystems_${type}, checking
226 # each one to see if it is mounted, and if it is not, mounting it.
228 mount_critical_filesystems()
230 eval _fslist=\$critical_filesystems_${1}
231 for _fs in $_fslist; do
234 while read what _on on _type type; do
235 if [ $on = $_fs ]; then
242 mount $_fs >/dev/null 2>&1
249 # check_pidfile pidfile procname [interpreter]
250 # Parses the first line of pidfile for a PID, and ensures
251 # that the process is running and matches procname.
252 # Prints the matching PID upon success, nothing otherwise.
253 # interpreter is optional; see _find_processes() for details.
260 if [ -z "$_pidfile" -o -z "$_procname" ]; then
261 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
263 if [ ! -f $_pidfile ]; then
264 debug "pid file ($_pidfile): not readable."
267 read _pid _junk < $_pidfile
268 if [ -z "$_pid" ]; then
269 debug "pid file ($_pidfile): no pid in file."
272 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
276 # check_process procname [interpreter]
277 # Ensures that a process (or processes) named procname is running.
278 # Prints a list of matching PIDs.
279 # interpreter is optional; see _find_processes() for details.
285 if [ -z "$_procname" ]; then
286 err 3 'USAGE: check_process procname [interpreter]'
288 _find_processes $_procname ${_interpreter:-.} '-ax'
292 # _find_processes procname interpreter psargs
293 # Search for procname in the output of ps generated by psargs.
294 # Prints the PIDs of any matching processes, space separated.
296 # If interpreter == ".", check the following variations of procname
297 # against the first word of each command:
299 # `basename procname`
300 # `basename procname` + ":"
301 # "(" + `basename procname` + ")"
302 # "[" + `basename procname` + "]"
304 # If interpreter != ".", read the first line of procname, remove the
305 # leading #!, normalise whitespace, append procname, and attempt to
306 # match that against each command, either as is, or with extra words
307 # at the end. As an alternative, to deal with interpreted daemons
308 # using perl, the basename of the interpreter plus a colon is also
309 # tried as the prefix to procname.
313 if [ $# -ne 3 ]; then
314 err 3 'USAGE: _find_processes procname interpreter psargs'
321 if [ $_interpreter != "." ]; then # an interpreted script
322 _script="${_chroot}${_chroot:+/}$_procname"
323 if [ -r "$_script" ]; then
324 read _interp < $_script # read interpreter name
327 _interp=${_interp#\#!} # strip #!
331 shift # drop env to get real name
334 if [ $_interpreter != $1 ]; then
335 warn "\$command_interpreter $_interpreter != $1"
339 warn "no shebang line in $_script"
344 warn "cannot read shebang line from $_script"
347 _interp="$* $_procname" # cleanup spaces, add _procname
350 _fp_match='case "$_argv" in
351 ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
352 else # a normal daemon
353 _procnamebn=${_procname##*/}
354 _fp_args='_arg0 _argv'
355 _fp_match='case "$_arg0" in
356 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
360 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
361 while read _npid _jid '"$_fp_args"'; do
363 if [ "$JID" -eq "$_jid" ];
364 then echo -n "$_pref$_npid";
371 # debug "in _find_processes: proccheck is ($_proccheck)."
375 # sort_lite [-b] [-n] [-k POS] [-t SEP]
376 # A lite version of sort(1) (supporting a few options) that can be used
377 # before the real sort(1) is available (e.g., in scripts that run prior
378 # to mountcritremote). Requires only shell built-in functionality.
382 local funcname=sort_lite
383 local sort_sep="$IFS" sort_ignore_leading_space=
384 local sort_field=0 sort_strict_fields= sort_numeric=
385 local nitems=0 skip_leading=0 trim=
388 while getopts bnk:t: flag; do
390 b) sort_ignore_leading_space=1 ;;
391 n) sort_numeric=1 sort_ignore_leading_space=1 ;;
392 k) sort_field="${OPTARG%%,*}" ;; # only up to first comma
393 # NB: Unlike sort(1) only one POS allowed
394 t) sort_sep="$OPTARG"
395 if [ ${#sort_sep} -gt 1 ]; then
396 echo "$funcname: multi-character tab \`$sort_sep'" >&2
404 shift $(( $OPTIND - 1 ))
406 # Create transformation pattern to trim leading text if desired
407 case "$sort_field" in
408 ""|[!0-9]*|*[!0-9.]*)
409 echo "$funcname: invalid sort field \`$sort_field'" >&2
413 skip_leading=${sort_field#*.} sort_field=${sort_field%%.*}
414 while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do
415 trim="$trim?" skip_leading=$(( $skip_leading - 1 ))
419 # Copy input to series of local numbered variables
420 # NB: IFS of NULL preserves leading whitespace
422 while IFS= read -r LINE || [ "$LINE" ]; do
423 nitems=$(( $nitems + 1 ))
424 local src_$nitems="$LINE"
428 # Sort numbered locals using insertion sort
430 local curitem curitem_orig curitem_mod curitem_haskey
431 local dest dest_orig dest_mod dest_haskey
434 while [ $i -le $nitems ]; do
435 curitem_haskey=1 # Assume sort field (-k POS) exists
436 eval curitem=\"\$src_$i\"
437 curitem_mod="$curitem" # for modified comparison
438 curitem_orig="$curitem" # for original comparison
440 # Trim leading whitespace if desired
441 if [ "$sort_ignore_leading_space" ]; then
442 while case "$curitem_orig" in
443 [$IFS]*) : ;; *) false; esac
445 curitem_orig="${curitem_orig#?}"
447 curitem_mod="$curitem_orig"
450 # Shift modified comparison value if sort field (-k POS) is > 1
452 while [ $n -gt 1 ]; do
453 case "$curitem_mod" in
455 # Cut text up-to (and incl.) first separator
456 curitem_mod="${curitem_mod#*[$sort_sep]}"
458 # Skip NULLs unless strict field splitting
459 [ "$sort_strict_fields" ] ||
460 [ "${curitem_mod%%[$sort_sep]*}" ] ||
465 # Asked for a field that doesn't exist
466 curitem_haskey= break
471 # Trim trailing words if sort field >= 1
472 [ $sort_field -ge 1 -a "$sort_numeric" ] &&
473 curitem_mod="${curitem_mod%%[$sort_sep]*}"
475 # Apply optional trim (-k POS.TRIM) to cut leading characters
476 curitem_mod="${curitem_mod#$trim}"
478 # Determine the type of modified comparison to use initially
479 # NB: Prefer numerical if requested but fallback to standard
480 case "$curitem_mod" in
481 ""|[!0-9]*) # NULL or begins with non-number
483 [ "$sort_numeric" ] && curitem_mod=0
486 if [ "$sort_numeric" ]; then
488 curitem_mod="${curitem_mod%%[!0-9]*}"
489 # NB: trailing non-digits removed
490 # otherwise numeric comparison fails
496 # If first time through, short-circuit below position-search
497 if [ $i -le 1 ]; then
504 # Find appropriate element position
508 dest_haskey=$curitem_haskey
509 eval dest=\"\$dest_$d\"
510 dest_mod="$dest" # for modified comparison
511 dest_orig="$dest" # for original comparison
513 # Trim leading whitespace if desired
514 if [ "$sort_ignore_leading_space" ]; then
515 while case "$dest_orig" in
516 [$IFS]*) : ;; *) false; esac
518 dest_orig="${dest_orig#?}"
520 dest_mod="$dest_orig"
523 # Shift modified value if sort field (-k POS) is > 1
525 while [ $n -gt 1 ]; do
528 # Cut text up-to (and incl.) 1st sep
529 dest_mod="${dest_mod#*[$sort_sep]}"
531 # Skip NULLs unless strict fields
532 [ "$sort_strict_fields" ] ||
533 [ "${dest_mod%%[$sort_sep]*}" ] ||
538 # Asked for a field that doesn't exist
544 # Trim trailing words if sort field >= 1
545 [ $sort_field -ge 1 -a "$sort_numeric" ] &&
546 dest_mod="${dest_mod%%[$sort_sep]*}"
548 # Apply optional trim (-k POS.TRIM), cut leading chars
549 dest_mod="${dest_mod#$trim}"
551 # Determine type of modified comparison to use
552 # NB: Prefer numerical if requested, fallback to std
554 ""|[!0-9]*) # NULL or begins with non-number
556 [ "$sort_numeric" ] && dest_mod=0
559 if [ "$sort_numeric" ]; then
561 dest_mod="${dest_mod%%[!0-9]*}"
562 # NB: kill trailing non-digits
563 # for numeric comparison safety
569 # Break if we've found the proper element position
570 if [ "$curitem_haskey" -a "$dest_haskey" ]; then
571 if [ "$dest_mod" = "$curitem_mod" ]; then
572 [ "$dest_orig" ">" "$curitem_orig" ] &&
574 elif [ "$dest_mod" $gt "$curitem_mod" ] \
580 [ "$dest_orig" ">" "$curitem_orig" ] && break
583 # Break if we've hit the end
584 [ $d -ge $i ] && break
589 # Shift remaining positions forward, making room for new item
591 while [ $n -ge $d ]; do
592 # Shift destination item forward one placement
593 eval dest_$(( $n + 1 ))=\"\$dest_$n\"
598 if [ $i -eq 1 ]; then
599 local dest_1="$curitem"
601 local dest_$d="$curitem"
607 # Print sorted results
609 while [ $d -le $nitems ]; do
610 eval echo \"\$dest_$d\"
616 # wait_for_pids pid [pid ...]
617 # spins until none of the pids exist
621 local _list _prefix _nlist _j
624 if [ -z "$_list" ]; then
631 if kill -0 $_j 2>/dev/null; then
632 _nlist="${_nlist}${_nlist:+ }$_j"
633 [ -n "$_prefix" ] && sleep 1
636 if [ -z "$_nlist" ]; then
640 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
642 pwait $_list 2>/dev/null
644 if [ -n "$_prefix" ]; then
650 # get_pidfile_from_conf string file
652 # Takes a string to search for in the specified file.
653 # Ignores lines with traditional comment characters.
657 # if get_pidfile_from_conf string file; then
658 # pidfile="$_pidfile_from_conf"
660 # pidfile='appropriate default'
663 get_pidfile_from_conf()
665 if [ -z "$1" -o -z "$2" ]; then
666 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
669 local string file line
671 string="$1" ; file="$2"
673 if [ ! -s "$file" ]; then
674 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
679 *[#\;]*${string}*) continue ;;
680 *${string}*) break ;;
684 if [ -n "$line" ]; then
686 _pidfile_from_conf="/${line%%[\"\;]*}"
694 # If rc_quiet is set (usually as a result of using faststart at
695 # boot time) check if rc_startmsgs is enabled.
699 if [ -n "$rc_quiet" ]; then
700 checkyesno rc_startmsgs
707 # run_rc_command argument
708 # Search for argument in the list of supported commands, which is:
709 # "start stop restart rcvar status poll ${extra_commands}"
710 # If there's a match, run ${argument}_cmd or the default method
713 # If argument has a given prefix, then change the operation as follows:
716 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
717 # force Set ${rcvar} to YES, and set rc_force=yes
718 # one Set ${rcvar} to YES
719 # quiet Don't output some diagnostics, and set rc_quiet=yes
721 # The following globals are used:
723 # Name Needed Purpose
724 # ---- ------ -------
725 # name y Name of script.
727 # command n Full path to command.
728 # Not needed if ${rc_arg}_cmd is set for
731 # command_args n Optional args/shell directives for command.
733 # command_interpreter n If not empty, command is interpreted, so
734 # call check_{pidfile,process}() appropriately.
736 # desc n Description of script.
738 # extra_commands n List of extra commands supported.
740 # pidfile n If set, use check_pidfile $pidfile $command,
741 # otherwise use check_process $command.
742 # In either case, only check if $command is set.
744 # procname n Process name to check for instead of $command.
746 # rcvar n This is checked with checkyesno to determine
747 # if the action should be run.
749 # ${name}_program n Full path to command.
750 # Meant to be used in /etc/rc.conf to override
753 # ${name}_chroot n Directory to chroot to before running ${command}
754 # Requires /usr to be mounted.
756 # ${name}_chdir n Directory to cd to before running ${command}
757 # (if not using ${name}_chroot).
759 # ${name}_flags n Arguments to call ${command} with.
760 # NOTE: $flags from the parent environment
761 # can be used to override this.
763 # ${name}_env n Environment variables to run ${command} with.
765 # ${name}_fib n Routing table number to run ${command} with.
767 # ${name}_nice n Nice level to run ${command} at.
769 # ${name}_oomprotect n Don't kill ${command} when swap space is exhausted.
771 # ${name}_user n User to run ${command} as, using su(1) if not
772 # using ${name}_chroot.
773 # Requires /usr to be mounted.
775 # ${name}_group n Group to run chrooted ${command} as.
776 # Requires /usr to be mounted.
778 # ${name}_groups n Comma separated list of supplementary groups
779 # to run the chrooted ${command} with.
780 # Requires /usr to be mounted.
782 # ${name}_prepend n Command added before ${command}.
784 # ${name}_login_class n Login class to use, else "daemon".
786 # ${rc_arg}_cmd n If set, use this as the method when invoked;
787 # Otherwise, use default command (see below)
789 # ${rc_arg}_precmd n If set, run just before performing the
790 # ${rc_arg}_cmd method in the default
791 # operation (i.e, after checking for required
792 # bits and process (non)existence).
793 # If this completes with a non-zero exit code,
794 # don't run ${rc_arg}_cmd.
796 # ${rc_arg}_postcmd n If set, run just after performing the
797 # ${rc_arg}_cmd method, if that method
798 # returned a zero exit code.
800 # required_dirs n If set, check for the existence of the given
801 # directories before running a (re)start command.
803 # required_files n If set, check for the readability of the given
804 # files before running a (re)start command.
806 # required_modules n If set, ensure the given kernel modules are
807 # loaded before running a (re)start command.
808 # The check and possible loads are actually
809 # done after start_precmd so that the modules
810 # aren't loaded in vain, should the precmd
811 # return a non-zero status to indicate a error.
812 # If a word in the list looks like "foo:bar",
813 # "foo" is the KLD file name and "bar" is the
814 # module name. If a word looks like "foo~bar",
815 # "foo" is the KLD file name and "bar" is a
816 # egrep(1) pattern matching the module name.
817 # Otherwise the module name is assumed to be
818 # the same as the KLD file name, which is most
819 # common. See load_kld().
821 # required_vars n If set, perform checkyesno on each of the
822 # listed variables before running the default
825 # Default behaviour for a given argument, if no override method is
828 # Argument Default behaviour
829 # -------- -----------------
830 # start if !running && checkyesno ${rcvar}
834 # rc_pid=$(check_pidfile $pidfile $command)
836 # rc_pid=$(check_process $command)
837 # kill $sig_stop $rc_pid
838 # wait_for_pids $rc_pid
839 # ($sig_stop defaults to TERM.)
841 # reload Similar to stop, except use $sig_reload instead,
842 # and doesn't wait_for_pids.
843 # $sig_reload defaults to HUP.
844 # Note that `reload' isn't provided by default,
845 # it should be enabled via $extra_commands.
847 # restart Run `stop' then `start'.
849 # status Show if ${command} is running, etc.
851 # poll Wait for ${command} to exit.
853 # rcvar Display what rc.conf variable is used (if any).
855 # enabled Return true if the service is enabled.
857 # describe Show the service's description
859 # extracommands Show the service's extra commands
861 # Variables available to methods, and after run_rc_command() has
866 # rc_arg Argument to command, after fast/force/one processing
869 # rc_flags Flags to start the default command with.
870 # Defaults to ${name}_flags, unless overridden
871 # by $flags from the environment.
872 # This variable may be changed by the precmd method.
874 # rc_service Path to the service being executed, in case the service
875 # needs to re-invoke itself.
877 # rc_pid PID of command (if appropriate)
879 # rc_fast Not empty if "fast" was provided (q.v.)
881 # rc_force Not empty if "force" was provided (q.v.)
883 # rc_quiet Not empty if "quiet" was provided
890 if [ -z "$name" ]; then
891 err 3 'run_rc_command: $name is not set.'
894 # Don't repeat the first argument when passing additional command-
895 # line arguments to the command subroutines.
902 fast*) # "fast" prefix; don't check pid
903 rc_arg=${rc_arg#fast}
907 force*) # "force" prefix; always run
910 rc_arg=${rc_arg#${_rc_prefix}}
911 if [ -n "${rcvar}" ]; then
915 one*) # "one" prefix; set ${rcvar}=yes
917 rc_arg=${rc_arg#${_rc_prefix}}
918 if [ -n "${rcvar}" ]; then
922 quiet*) # "quiet" prefix; omit some messages
924 rc_arg=${rc_arg#${_rc_prefix}}
929 eval _override_command=\$${name}_program
930 command=${_override_command:-$command}
932 _keywords="start stop restart rcvar enabled describe extracommands $extra_commands"
935 _procname=${procname:-${command}}
937 # setup pid check command
938 if [ -n "$_procname" ]; then
939 if [ -n "$pidfile" ]; then
940 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
942 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
944 _keywords="${_keywords} status poll"
947 if [ -z "$rc_arg" ]; then
951 if [ "$rc_arg" = "enabled" ] ; then
956 if [ -n "$flags" ]; then # allow override from environment
959 eval rc_flags=\$${name}_flags
961 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
962 _nice=\$${name}_nice _user=\$${name}_user \
963 _group=\$${name}_group _groups=\$${name}_groups \
964 _fib=\$${name}_fib _env=\$${name}_env \
965 _prepend=\$${name}_prepend _login_class=\${${name}_login_class:-daemon} \
966 _oomprotect=\$${name}_oomprotect
968 if [ -n "$_user" ]; then # unset $_user if running as that user
969 if [ "$_user" = "$(eval $IDCMD)" ]; then
974 [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary
976 for _elem in $_keywords; do
977 if [ "$_elem" != "$rc_arg" ]; then
980 # if ${rcvar} is set, $1 is not "rcvar" and not "describe"
981 # and ${rc_pid} is not set, then run
982 # checkyesno ${rcvar}
983 # and return if that failed
985 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" \
986 -a "$rc_arg" != "describe" ] ||
987 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then
988 if ! checkyesno ${rcvar}; then
989 if [ -n "${rc_quiet}" ]; then
992 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
993 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
994 echo "instead of '${rc_arg}'."
999 if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then
1000 if [ -z "$rc_quiet" ]; then
1001 echo 1>&2 "${name} already running? " \
1007 # if there's a custom ${XXX_cmd},
1008 # run that instead of the default
1010 eval _cmd=\$${rc_arg}_cmd \
1011 _precmd=\$${rc_arg}_precmd \
1012 _postcmd=\$${rc_arg}_postcmd
1014 if [ -n "$_cmd" ]; then
1015 _run_rc_precmd || return 1
1016 _run_rc_doit "$_cmd $rc_extra_args" || return 1
1021 case "$rc_arg" in # default operations...
1024 if [ -n "$desc" ]; then
1030 echo "$extra_commands"
1034 _run_rc_precmd || return 1
1035 if [ -n "$rc_pid" ]; then
1036 echo "${name} is running as pid $rc_pid."
1038 echo "${name} is not running."
1045 if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then
1046 warn "run_rc_command: cannot run $command"
1050 if ! _run_rc_precmd; then
1051 warn "failed precmd routine for ${name}"
1055 # setup the full command to run
1057 check_startmsgs && echo "Starting ${name}."
1058 if [ -n "$_chroot" ]; then
1061 ${_nice:+nice -n $_nice }\
1062 ${_fib:+setfib -F $_fib }\
1063 ${_env:+env $_env }\
1064 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
1065 $_chroot $command $rc_flags $command_args"
1067 _cd="${_chdir:+cd $_chdir && }"
1069 ${_fib:+setfib -F $_fib }\
1070 ${_env:+env $_env }\
1071 $command $rc_flags $command_args"
1072 if [ -n "$_user" ]; then
1073 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
1075 if [ -n "$_nice" ]; then
1076 if [ -z "$_user" ]; then
1077 _doit="sh -c \"$_doit\""
1079 _doit="nice -n $_nice $_doit"
1081 if [ -n "$_prepend" ]; then
1082 _doit="$_prepend $_doit"
1086 # Prepend default limits
1087 _doit="$_cd limits -C $_login_class $_doit"
1089 # run the full command
1091 if ! _run_rc_doit "$_doit"; then
1092 warn "failed to start ${name}"
1096 # finally, run postcmd
1102 if [ -z "$rc_pid" ]; then
1103 [ -n "$rc_fast" ] && return 0
1108 _run_rc_precmd || return 1
1110 # send the signal to stop
1112 echo "Stopping ${name}."
1113 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
1114 _run_rc_doit "$_doit" || return 1
1116 # wait for the command to exit,
1118 wait_for_pids $rc_pid
1124 if [ -z "$rc_pid" ]; then
1129 _run_rc_precmd || return 1
1131 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
1132 _run_rc_doit "$_doit" || return 1
1138 # prevent restart being called more
1139 # than once by any given script
1141 if ${_rc_restart_done:-false}; then
1144 _rc_restart_done=true
1146 _run_rc_precmd || return 1
1148 # run those in a subshell to keep global variables
1149 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
1150 ( run_rc_command ${_rc_prefix}start $rc_extra_args )
1152 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
1158 _run_rc_precmd || return 1
1159 if [ -n "$rc_pid" ]; then
1160 wait_for_pids $rc_pid
1167 if [ -n "$desc" ]; then
1173 # Get unique vars in $rcvar $rcvars
1174 for _v in $rcvar $rcvars; do
1176 $_v\ *|\ *$_v|*\ $_v\ *) ;;
1177 *) v="${v# } $_v" ;;
1181 # Display variables.
1183 if [ -z "$_v" ]; then
1187 eval _desc=\$${_v}_desc
1188 eval _defval=\$${_v}_defval
1191 eval echo \"$_v=\\\"\$$_v\\\"\"
1192 # decode multiple lines of _desc
1193 while [ -n "$_desc" ]; do
1196 echo "# $_h ${_desc%%^^*}"
1201 echo "# $_h ${_desc}"
1206 echo "# (default: \"$_defval\")"
1217 # Apply protect(1) to the PID if ${name}_oomprotect is set.
1220 # We cannot use protect(1) inside jails.
1221 if [ -n "$_oomprotect" ] && [ -f "${PROTECT}" ] &&
1222 [ "$(sysctl -n security.jail.jailed)" -eq 0 ]; then
1223 pid=$(check_process $command)
1224 case $_oomprotect in
1226 ${PROTECT} -i -p ${pid}
1229 ${PROTECT} -p ${pid}
1239 echo 1>&2 "$0: unknown directive '$rc_arg'."
1245 # Helper functions for run_rc_command: common code.
1246 # They use such global variables besides the exported rc_* ones:
1249 # ------------------
1256 check_required_before "$rc_arg" || return 1
1258 if [ -n "$_precmd" ]; then
1259 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
1260 eval "$_precmd $rc_extra_args"
1263 # If precmd failed and force isn't set, request exit.
1264 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1269 check_required_after "$rc_arg" || return 1
1276 if [ -n "$_postcmd" ]; then
1277 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
1278 eval "$_postcmd $rc_extra_args"
1286 debug "run_rc_command: doit: $*"
1290 # If command failed and force isn't set, request exit.
1291 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1298 _run_rc_notrunning()
1302 if [ -n "$pidfile" ]; then
1303 _pidmsg=" (check $pidfile)."
1307 echo 1>&2 "${name} not running?${_pidmsg}"
1314 _cmd="kill -$1 $rc_pid"
1315 if [ -n "$_user" ]; then
1316 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
1322 # run_rc_script file arg
1323 # Start the script `file' with `arg', and correctly handle the
1324 # return value from the script.
1325 # If `file' ends with `.sh' and lives in /etc/rc.d, ignore it as it's
1326 # an old-style startup file.
1327 # If `file' ends with `.sh' and does not live in /etc/rc.d, it's sourced
1328 # into the current environment if $rc_fast_and_loose is set; otherwise
1329 # it is run as a child process.
1330 # If `file' appears to be a backup or scratch file, ignore it.
1331 # Otherwise if it is executable run as a child process.
1337 if [ -z "$_file" -o -z "$_arg" ]; then
1338 err 3 'USAGE: run_rc_script file arg'
1341 unset name command command_args command_interpreter \
1342 extra_commands pidfile procname \
1343 rcvar rcvars rcvars_obsolete required_dirs required_files \
1345 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1349 /etc/rc.d/*.sh) # no longer allowed in the base
1350 warn "Ignoring old-style startup script $_file"
1352 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
1353 warn "Ignoring scratch file $_file"
1355 *) # run in subshell
1356 if [ -x $_file ]; then
1357 if [ -n "$rc_fast_and_loose" ]; then
1360 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3
1361 trap "echo Script $_file interrupted >&2 ; exit 1" 2
1362 trap "echo Script $_file running >&2" 29
1363 set $_arg; . $_file )
1371 # load_rc_config [service]
1372 # Source in the configuration file(s) for a given service.
1373 # If no service is specified, only the global configuration
1374 # file(s) will be loaded.
1378 local _name _rcvar_val _var _defval _v _msg _new _d
1381 if ${_rc_conf_loaded:-false}; then
1384 if [ -r /etc/defaults/rc.conf ]; then
1385 debug "Sourcing /etc/defaults/rc.conf"
1386 . /etc/defaults/rc.conf
1388 elif [ -r /etc/rc.conf ]; then
1389 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1392 _rc_conf_loaded=true
1395 # If a service name was specified, attempt to load
1396 # service-specific configuration
1397 if [ -n "$_name" ] ; then
1398 for _d in /etc ${local_startup}; do
1400 if [ -f ${_d}/rc.conf.d/"$_name" ]; then
1401 debug "Sourcing ${_d}/rc.conf.d/$_name"
1402 . ${_d}/rc.conf.d/"$_name"
1403 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then
1405 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do
1406 if [ -f "$_rc" ] ; then
1407 debug "Sourcing $_rc"
1415 # Set defaults if defined.
1416 for _var in $rcvar $rcvars; do
1417 eval _defval=\$${_var}_defval
1418 if [ -n "$_defval" ]; then
1419 eval : \${$_var:=\$${_var}_defval}
1423 # check obsolete rc.conf variables
1424 for _var in $rcvars_obsolete; do
1426 eval _msg=\$${_var}_obsolete_msg
1427 eval _new=\$${_var}_newvar
1432 if [ -z "$_new" ]; then
1435 eval $_new=\"\$$_var\"
1436 if [ -z "$_msg" ]; then
1437 _msg="Use \$$_new instead."
1440 warn "\$$_var is obsolete. $_msg"
1447 # load_rc_config_var name var
1448 # Read the rc.conf(5) var for name and set in the
1449 # current shell, using load_rc_config in a subshell to prevent
1450 # unwanted side effects from other variable assignments.
1452 load_rc_config_var()
1454 if [ $# -ne 2 ]; then
1455 err 3 'USAGE: load_rc_config_var name var'
1458 load_rc_config '$1' >/dev/null;
1459 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1460 echo '$2'=\'\''${'$2'}\'\'';
1467 # Print a usage string for $0, with `commands' being a list of
1472 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
1476 echo -n 1>&2 "$_sep$_elem"
1484 # err exitval message
1485 # Display message to stderr and log to the syslog, and exit with exitval.
1492 if [ -x /usr/bin/logger ]; then
1493 logger "$0: ERROR: $*"
1495 echo 1>&2 "$0: ERROR: $*"
1501 # Display message to stderr and log to the syslog.
1505 if [ -x /usr/bin/logger ]; then
1506 logger "$0: WARNING: $*"
1508 echo 1>&2 "$0: WARNING: $*"
1513 # Display informational message to stdout and log to syslog.
1518 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1519 if [ -x /usr/bin/logger ]; then
1520 logger "$0: INFO: $*"
1529 # If debugging is enabled in rc.conf output message to stderr.
1530 # BEWARE that you don't call any subroutine that itself calls this
1536 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1537 if [ -x /usr/bin/logger ]; then
1538 logger "$0: DEBUG: $*"
1540 echo 1>&2 "$0: DEBUG: $*"
1546 # backup_file action file cur backup
1547 # Make a backup copy of `file' into `cur', and save the previous
1548 # version of `cur' as `backup' or use rcs for archiving.
1550 # This routine checks the value of the backup_uses_rcs variable,
1551 # which can be either YES or NO.
1553 # The `action' keyword can be one of the following:
1555 # add `file' is now being backed up (and is possibly
1556 # being reentered into the backups system). `cur'
1557 # is created and RCS files, if necessary, are
1560 # update `file' has changed and needs to be backed up.
1561 # If `cur' exists, it is copied to to `back' or
1562 # checked into RCS (if the repository file is old),
1563 # and then `file' is copied to `cur'. Another RCS
1564 # check in done here if RCS is being used.
1566 # remove `file' is no longer being tracked by the backups
1567 # system. If RCS is not being used, `cur' is moved
1568 # to `back', otherwise an empty file is checked in,
1569 # and then `cur' is removed.
1579 if checkyesno backup_uses_rcs; then
1580 _msg0="backup archive"
1583 # ensure that history file is not locked
1584 if [ -f $_cur,v ]; then
1585 rcs -q -u -U -M $_cur
1588 # ensure after switching to rcs that the
1589 # current backup is not lost
1590 if [ -f $_cur ]; then
1591 # no archive, or current newer than archive
1592 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1593 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1602 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1605 chown root:wheel $_cur $_cur,v
1609 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1611 chown root:wheel $_cur $_cur,v
1618 if [ -f $_cur ]; then
1622 chown root:wheel $_cur
1631 # make_symlink src link
1632 # Make a symbolic link 'link' to src from basedir. If the
1633 # directory in which link is to be created does not exist
1634 # a warning will be displayed and an error will be returned.
1635 # Returns 0 on success, 1 otherwise.
1639 local src link linkdir _me
1642 linkdir="`dirname $link`"
1643 _me="make_symlink()"
1645 if [ -z "$src" -o -z "$link" ]; then
1646 warn "$_me: requires two arguments."
1649 if [ ! -d "$linkdir" ]; then
1650 warn "$_me: the directory $linkdir does not exist."
1653 if ! ln -sf $src $link; then
1654 warn "$_me: unable to make a symbolic link from $link to $src"
1660 # devfs_rulesets_from_file file
1661 # Reads a set of devfs commands from file, and creates
1662 # the specified rulesets with their rules. Returns non-zero
1663 # if there was an error.
1665 devfs_rulesets_from_file()
1667 local file _err _me _opts
1669 _me="devfs_rulesets_from_file"
1672 if [ -z "$file" ]; then
1673 warn "$_me: you must specify a file"
1676 if [ ! -e "$file" ]; then
1677 debug "$_me: no such file ($file)"
1681 # Disable globbing so that the rule patterns are not expanded
1682 # by accident with matching filesystem entries.
1685 debug "reading rulesets from file ($file)"
1693 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1694 if [ -z "$rulenum" ]; then
1695 warn "$_me: cannot extract rule number ($line)"
1699 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1700 if [ -z "$rulename" ]; then
1701 warn "$_me: cannot extract rule name ($line)"
1705 eval $rulename=\$rulenum
1706 debug "found ruleset: $rulename=$rulenum"
1707 if ! /sbin/devfs rule -s $rulenum delset; then
1713 rulecmd="${line%%"\#*"}"
1714 # evaluate the command incase it includes
1716 if [ -n "$rulecmd" ]; then
1717 debug "adding rule ($rulecmd)"
1718 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1726 if [ $_err -ne 0 ]; then
1727 debug "error in $_me"
1731 case $_opts in *f*) ;; *) set +f ;; esac
1735 # devfs_init_rulesets
1736 # Initializes rulesets from configuration files. Returns
1737 # non-zero if there was an error.
1739 devfs_init_rulesets()
1742 _me="devfs_init_rulesets"
1744 # Go through this only once
1745 if [ -n "$devfs_rulesets_init" ]; then
1746 debug "$_me: devfs rulesets already initialized"
1749 for file in $devfs_rulesets; do
1750 if ! devfs_rulesets_from_file $file; then
1751 warn "$_me: could not read rules from $file"
1755 devfs_rulesets_init=1
1756 debug "$_me: devfs rulesets initialized"
1760 # devfs_set_ruleset ruleset [dir]
1761 # Sets the default ruleset of dir to ruleset. The ruleset argument
1762 # must be a ruleset name as specified in devfs.rules(5) file.
1763 # Returns non-zero if it could not set it successfully.
1768 [ -n "$1" ] && eval rs=\$$1 || rs=
1769 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1770 _me="devfs_set_ruleset"
1772 if [ -z "$rs" ]; then
1773 warn "$_me: you must specify a ruleset number"
1776 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1777 if ! /sbin/devfs $devdir ruleset $rs; then
1778 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1784 # devfs_apply_ruleset ruleset [dir]
1785 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1786 # The ruleset argument must be a ruleset name as specified
1787 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1788 # if it could not apply the ruleset.
1790 devfs_apply_ruleset()
1793 [ -n "$1" ] && eval rs=\$$1 || rs=
1794 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1795 _me="devfs_apply_ruleset"
1797 if [ -z "$rs" ]; then
1798 warn "$_me: you must specify a ruleset"
1801 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1802 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1803 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1809 # devfs_domount dir [ruleset]
1810 # Mount devfs on dir. If ruleset is specified it is set
1811 # on the mount-point. It must also be a ruleset name as specified
1812 # in a devfs.rules(5) file. Returns 0 on success.
1818 [ -n "$2" ] && rs=$2 || rs=
1819 _me="devfs_domount()"
1821 if [ -z "$devdir" ]; then
1822 warn "$_me: you must specify a mount-point"
1825 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1826 if ! mount -t devfs dev "$devdir"; then
1827 warn "$_me: Unable to mount devfs on $devdir"
1830 if [ -n "$rs" ]; then
1832 devfs_set_ruleset $rs $devdir
1833 devfs -m $devdir rule applyset
1838 # Provide a function for normalizing the mounting of memory
1839 # filesystems. This should allow the rest of the code here to remain
1840 # as close as possible between 5-current and 4-stable.
1843 # $3 = (optional) extra mdmfs flags
1846 if [ -n "$3" ]; then
1849 /sbin/mdmfs $flags -s $1 ${mfs_type} $2
1852 # Code common to scripts that need to load a kernel module
1853 # if it isn't in the kernel yet. Syntax:
1854 # load_kld [-e regex] [-m module] file
1855 # where -e or -m chooses the way to check if the module
1856 # is already loaded:
1857 # regex is egrep'd in the output from `kldstat -v',
1858 # module is passed to `kldstat -m'.
1859 # The default way is as though `-m file' were specified.
1862 local _loaded _mod _opt _re
1864 while getopts "e:m:" _opt; do
1867 m) _mod="$OPTARG" ;;
1868 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1871 shift $(($OPTIND - 1))
1872 if [ $# -ne 1 ]; then
1873 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1877 if [ -n "$_re" ]; then
1878 if kldstat -v | egrep -q -e "$_re"; then
1882 if kldstat -q -m "$_mod"; then
1887 if ! kldload "$1"; then
1888 warn "Unable to load kernel module $1"
1891 info "$1 kernel module loaded."
1894 debug "load_kld: $1 kernel module already loaded."
1899 # ltr str src dst [var]
1900 # Change every $src in $str to $dst.
1901 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1902 # awk(1). If var is non-NULL, set it to the result.
1905 local _str _src _dst _out _com _var
1913 for _com in ${_str}; do
1914 if [ -z "${_out}" ]; then
1917 _out="${_out}${_dst}${_com}"
1920 if [ -n "${_var}" ]; then
1921 setvar "${_var}" "${_out}"
1927 # Creates a list of providers for GELI encryption.
1930 local devices devices2
1931 local provider mountpoint type options rest
1933 # Create list of GELI providers from fstab.
1934 while read provider mountpoint type options rest ; do
1935 case ":${options}" in
1944 case ":${provider}" in
1949 # Skip swap devices.
1950 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1953 devices="${devices} ${provider}"
1958 # Append providers from geli_devices.
1959 devices="${devices} ${geli_devices}"
1961 for provider in ${devices}; do
1962 provider=${provider%.eli}
1963 provider=${provider#/dev/}
1964 devices2="${devices2} ${provider}"
1970 # Originally, root mount hold had to be released before mounting
1971 # the root filesystem. This delayed the boot, so it was changed
1972 # to only wait if the root device isn't readily available. This
1973 # can result in rc scripts executing before all the devices - such
1974 # as graid(8), or USB disks - can be accessed. This function can
1975 # be used to explicitly wait for root mount holds to be released.
1978 local wait waited holders
1982 holders="$(sysctl -n vfs.root_mount_hold)"
1983 if [ -z "${holders}" ]; then
1986 if [ ${waited} -eq 0 ]; then
1987 echo -n "Waiting ${root_hold_delay}s" \
1988 "for the root mount holders: ${holders}"
1992 if [ ${waited} -ge ${root_hold_delay} ]; then
1997 waited=$(($waited + 1))
2001 # Find scripts in local_startup directories that use the old syntax
2003 find_local_scripts_old() {
2006 for dir in ${local_startup}; do
2007 if [ -d "${dir}" ]; then
2008 for file in ${dir}/[0-9]*.sh; do
2009 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
2011 zlist="$zlist $file"
2013 for file in ${dir}/[!0-9]*.sh; do
2014 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
2016 slist="$slist $file"
2022 find_local_scripts_new() {
2024 for dir in ${local_startup}; do
2025 if [ -d "${dir}" ]; then
2026 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
2029 *) if [ -x "$file" ]; then
2030 local_rc="${local_rc} ${file}"
2039 # check_required_{before|after} command
2040 # Check for things required by the command before and after its precmd,
2041 # respectively. The two separate functions are needed because some
2042 # conditions should prevent precmd from being run while other things
2043 # depend on precmd having already been run.
2045 check_required_before()
2051 for _f in $required_vars; do
2052 if ! checkyesno $_f; then
2053 warn "\$${_f} is not enabled."
2054 if [ -z "$rc_force" ]; then
2060 for _f in $required_dirs; do
2061 if [ ! -d "${_f}/." ]; then
2062 warn "${_f} is not a directory."
2063 if [ -z "$rc_force" ]; then
2069 for _f in $required_files; do
2070 if [ ! -r "${_f}" ]; then
2071 warn "${_f} is not readable."
2072 if [ -z "$rc_force" ]; then
2083 check_required_after()
2089 for _f in $required_modules; do
2091 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
2092 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
2095 if ! load_kld ${_args}; then
2096 if [ -z "$rc_force" ]; then
2108 # Return true if security.jail.$mib exists and set to 1.
2115 if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then
2123 # check_kern_features mib
2124 # Return existence of kern.features.* sysctl MIB as true or
2125 # false. The result will be cached in $_rc_cache_kern_features_
2126 # namespace. "0" means the kern.features.X exists.
2128 check_kern_features()
2132 [ -n "$1" ] || return 1;
2133 eval _v=\$_rc_cache_kern_features_$1
2134 [ -n "$_v" ] && return "$_v";
2136 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then
2137 eval _rc_cache_kern_features_$1=0
2140 eval _rc_cache_kern_features_$1=1
2145 # check_namevarlist var
2146 # Return "0" if ${name}_var is reserved in rc.subr.
2148 _rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend"
2153 for _v in $_rc_namevarlist; do
2162 # _echoonce var msg mode
2163 # mode=0: Echo $msg if ${$var} is empty.
2164 # After doing echo, a string is set to ${$var}.
2166 # mode=1: Echo $msg if ${$var} is a string with non-zero length.
2170 local _var _msg _mode
2176 1) [ -n "$_var" ] && echo "$_msg" ;;
2177 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
2181 # If the loader env variable rc.debug is set, turn on debugging. rc.conf will
2182 # still override this, but /etc/defaults/rc.conf can't unconditionally set this
2183 # since it would undo what we've done here.
2184 if kenv -q rc.debug > /dev/null ; then