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 variables matching glob pattern.
73 # Localize 'set' option below.
75 local IFS=$'\n' line varname
77 # Disable path expansion in unquoted 'for' parameters below.
80 for line in $(set); do
84 "$line"|*[!a-zA-Z0-9_]*)
94 # set_rcvar [var] [defval] [desc]
96 # Echo or define a rc.conf(5) variable name. Global variable
99 # If no argument is specified, echo "${name}_enable".
101 # If only a var is specified, echo "${var}_enable".
103 # If var and defval are specified, the ${var} is defined as
104 # rc.conf(5) variable and the default value is ${defvar}. An
105 # optional argument $desc can also be specified to add a
106 # description for that.
113 0) echo ${name}_enable ;;
114 1) echo ${1}_enable ;;
116 debug "set_rcvar: \$$1=$2 is added" \
117 " as a rc.conf(5) variable."
119 rcvars="${rcvars# } $_var"
120 eval ${_var}_defval=\"$2\"
122 eval ${_var}_desc=\"$*\"
127 # set_rcvar_obsolete oldvar [newvar] [msg]
128 # Define obsolete variable.
129 # Global variable $rcvars_obsolete is used.
135 debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
137 rcvars_obsolete="${rcvars_obsolete# } $1"
138 eval ${1}_newvar=\"$2\"
140 eval ${_var}_obsolete_msg=\"$*\"
144 # force_depend script [rcvar]
145 # Force a service to start. Intended for use by services
146 # to resolve dependency issues.
147 # $1 - filename of script, in /etc/rc.d, to run
148 # $2 - name of the script's rcvar (minus the _enable)
152 local _depend _dep_rcvar
155 _dep_rcvar="${2:-$1}_enable"
157 [ -n "$rc_fast" ] && ! checkyesno always_force_depends &&
158 checkyesno $_dep_rcvar && return 0
160 /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0
162 info "${name} depends on ${_depend}, which will be forced to start."
163 if ! /etc/rc.d/${_depend} forcestart; then
164 warn "Unable to force ${_depend}. It may already be running."
171 # Test $1 variable, and warn if not set to YES or NO.
172 # Return 0 if it's "yes" (et al), nonzero otherwise.
177 debug "checkyesno: $1 is set to $_value."
180 # "yes", "true", "on", or "1"
181 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
185 # "no", "false", "off", or "0"
186 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
190 warn "\$${1} is not set properly - see rc.conf(5)."
198 # print the list in reverse order
204 _revlist="$_revfile $_revlist"
210 # If booting directly to multiuser or $always is enabled,
211 # send SIGTERM to the parent (/etc/rc) to abort the boot.
212 # Otherwise just exit.
219 # "yes", "true", "on", or "1"
220 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
227 if [ "$autoboot" = yes -o "$always" = true ]; then
228 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
235 # mount_critical_filesystems type
236 # Go through the list of critical filesystems as provided in
237 # the rc.conf(5) variable $critical_filesystems_${type}, checking
238 # each one to see if it is mounted, and if it is not, mounting it.
240 mount_critical_filesystems()
242 eval _fslist=\$critical_filesystems_${1}
243 for _fs in $_fslist; do
246 while read what _on on _type type; do
247 if [ $on = $_fs ]; then
254 mount $_fs >/dev/null 2>&1
261 # check_pidfile pidfile procname [interpreter]
262 # Parses the first line of pidfile for a PID, and ensures
263 # that the process is running and matches procname.
264 # Prints the matching PID upon success, nothing otherwise.
265 # interpreter is optional; see _find_processes() for details.
272 if [ -z "$_pidfile" -o -z "$_procname" ]; then
273 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
275 if [ ! -f $_pidfile ]; then
276 debug "pid file ($_pidfile): not readable."
279 read _pid _junk < $_pidfile
280 if [ -z "$_pid" ]; then
281 debug "pid file ($_pidfile): no pid in file."
284 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
288 # check_process procname [interpreter]
289 # Ensures that a process (or processes) named procname is running.
290 # Prints a list of matching PIDs.
291 # interpreter is optional; see _find_processes() for details.
297 if [ -z "$_procname" ]; then
298 err 3 'USAGE: check_process procname [interpreter]'
300 _find_processes $_procname ${_interpreter:-.} '-ax'
304 # _find_processes procname interpreter psargs
305 # Search for procname in the output of ps generated by psargs.
306 # Prints the PIDs of any matching processes, space separated.
308 # If interpreter == ".", check the following variations of procname
309 # against the first word of each command:
311 # `basename procname`
312 # `basename procname` + ":"
313 # "(" + `basename procname` + ")"
314 # "[" + `basename procname` + "]"
316 # If interpreter != ".", read the first line of procname, remove the
317 # leading #!, normalise whitespace, append procname, and attempt to
318 # match that against each command, either as is, or with extra words
319 # at the end. As an alternative, to deal with interpreted daemons
320 # using perl, the basename of the interpreter plus a colon is also
321 # tried as the prefix to procname.
325 if [ $# -ne 3 ]; then
326 err 3 'USAGE: _find_processes procname interpreter psargs'
333 if [ $_interpreter != "." ]; then # an interpreted script
334 _script="${_chroot}${_chroot:+/}$_procname"
335 if [ -r "$_script" ]; then
336 read _interp < $_script # read interpreter name
339 _interp=${_interp#\#!} # strip #!
343 shift # drop env to get real name
346 if [ $_interpreter != $1 ]; then
347 warn "\$command_interpreter $_interpreter != $1"
351 warn "no shebang line in $_script"
356 warn "cannot read shebang line from $_script"
359 _interp="$* $_procname" # cleanup spaces, add _procname
362 _fp_match='case "$_argv" in
363 ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
364 else # a normal daemon
365 _procnamebn=${_procname##*/}
366 _fp_args='_arg0 _argv'
367 _fp_match='case "$_arg0" in
368 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
372 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
373 while read _npid _jid '"$_fp_args"'; do
375 if [ "$JID" -eq "$_jid" ];
376 then echo -n "$_pref$_npid";
383 # debug "in _find_processes: proccheck is ($_proccheck)."
387 # sort_lite [-b] [-n] [-k POS] [-t SEP]
388 # A lite version of sort(1) (supporting a few options) that can be used
389 # before the real sort(1) is available (e.g., in scripts that run prior
390 # to mountcritremote). Requires only shell built-in functionality.
394 local funcname=sort_lite
395 local sort_sep="$IFS" sort_ignore_leading_space=
396 local sort_field=0 sort_strict_fields= sort_numeric=
397 local nitems=0 skip_leading=0 trim=
400 while getopts bnk:t: flag; do
402 b) sort_ignore_leading_space=1 ;;
403 n) sort_numeric=1 sort_ignore_leading_space=1 ;;
404 k) sort_field="${OPTARG%%,*}" ;; # only up to first comma
405 # NB: Unlike sort(1) only one POS allowed
406 t) sort_sep="$OPTARG"
407 if [ ${#sort_sep} -gt 1 ]; then
408 echo "$funcname: multi-character tab \`$sort_sep'" >&2
416 shift $(( $OPTIND - 1 ))
418 # Create transformation pattern to trim leading text if desired
419 case "$sort_field" in
420 ""|[!0-9]*|*[!0-9.]*)
421 echo "$funcname: invalid sort field \`$sort_field'" >&2
425 skip_leading=${sort_field#*.} sort_field=${sort_field%%.*}
426 while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do
427 trim="$trim?" skip_leading=$(( $skip_leading - 1 ))
431 # Copy input to series of local numbered variables
432 # NB: IFS of NULL preserves leading whitespace
434 while IFS= read -r LINE || [ "$LINE" ]; do
435 nitems=$(( $nitems + 1 ))
436 local src_$nitems="$LINE"
440 # Sort numbered locals using insertion sort
442 local curitem curitem_orig curitem_mod curitem_haskey
443 local dest dest_orig dest_mod dest_haskey
446 while [ $i -le $nitems ]; do
447 curitem_haskey=1 # Assume sort field (-k POS) exists
448 eval curitem=\"\$src_$i\"
449 curitem_mod="$curitem" # for modified comparison
450 curitem_orig="$curitem" # for original comparison
452 # Trim leading whitespace if desired
453 if [ "$sort_ignore_leading_space" ]; then
454 while case "$curitem_orig" in
455 [$IFS]*) : ;; *) false; esac
457 curitem_orig="${curitem_orig#?}"
459 curitem_mod="$curitem_orig"
462 # Shift modified comparison value if sort field (-k POS) is > 1
464 while [ $n -gt 1 ]; do
465 case "$curitem_mod" in
467 # Cut text up-to (and incl.) first separator
468 curitem_mod="${curitem_mod#*[$sort_sep]}"
470 # Skip NULLs unless strict field splitting
471 [ "$sort_strict_fields" ] ||
472 [ "${curitem_mod%%[$sort_sep]*}" ] ||
477 # Asked for a field that doesn't exist
478 curitem_haskey= break
483 # Trim trailing words if sort field >= 1
484 [ $sort_field -ge 1 -a "$sort_numeric" ] &&
485 curitem_mod="${curitem_mod%%[$sort_sep]*}"
487 # Apply optional trim (-k POS.TRIM) to cut leading characters
488 curitem_mod="${curitem_mod#$trim}"
490 # Determine the type of modified comparison to use initially
491 # NB: Prefer numerical if requested but fallback to standard
492 case "$curitem_mod" in
493 ""|[!0-9]*) # NULL or begins with non-number
495 [ "$sort_numeric" ] && curitem_mod=0
498 if [ "$sort_numeric" ]; then
500 curitem_mod="${curitem_mod%%[!0-9]*}"
501 # NB: trailing non-digits removed
502 # otherwise numeric comparison fails
508 # If first time through, short-circuit below position-search
509 if [ $i -le 1 ]; then
516 # Find appropriate element position
520 dest_haskey=$curitem_haskey
521 eval dest=\"\$dest_$d\"
522 dest_mod="$dest" # for modified comparison
523 dest_orig="$dest" # for original comparison
525 # Trim leading whitespace if desired
526 if [ "$sort_ignore_leading_space" ]; then
527 while case "$dest_orig" in
528 [$IFS]*) : ;; *) false; esac
530 dest_orig="${dest_orig#?}"
532 dest_mod="$dest_orig"
535 # Shift modified value if sort field (-k POS) is > 1
537 while [ $n -gt 1 ]; do
540 # Cut text up-to (and incl.) 1st sep
541 dest_mod="${dest_mod#*[$sort_sep]}"
543 # Skip NULLs unless strict fields
544 [ "$sort_strict_fields" ] ||
545 [ "${dest_mod%%[$sort_sep]*}" ] ||
550 # Asked for a field that doesn't exist
556 # Trim trailing words if sort field >= 1
557 [ $sort_field -ge 1 -a "$sort_numeric" ] &&
558 dest_mod="${dest_mod%%[$sort_sep]*}"
560 # Apply optional trim (-k POS.TRIM), cut leading chars
561 dest_mod="${dest_mod#$trim}"
563 # Determine type of modified comparison to use
564 # NB: Prefer numerical if requested, fallback to std
566 ""|[!0-9]*) # NULL or begins with non-number
568 [ "$sort_numeric" ] && dest_mod=0
571 if [ "$sort_numeric" ]; then
573 dest_mod="${dest_mod%%[!0-9]*}"
574 # NB: kill trailing non-digits
575 # for numeric comparison safety
581 # Break if we've found the proper element position
582 if [ "$curitem_haskey" -a "$dest_haskey" ]; then
583 if [ "$dest_mod" = "$curitem_mod" ]; then
584 [ "$dest_orig" ">" "$curitem_orig" ] &&
586 elif [ "$dest_mod" $gt "$curitem_mod" ] \
592 [ "$dest_orig" ">" "$curitem_orig" ] && break
595 # Break if we've hit the end
596 [ $d -ge $i ] && break
601 # Shift remaining positions forward, making room for new item
603 while [ $n -ge $d ]; do
604 # Shift destination item forward one placement
605 eval dest_$(( $n + 1 ))=\"\$dest_$n\"
610 if [ $i -eq 1 ]; then
611 local dest_1="$curitem"
613 local dest_$d="$curitem"
619 # Print sorted results
621 while [ $d -le $nitems ]; do
622 eval echo \"\$dest_$d\"
628 # wait_for_pids pid [pid ...]
629 # spins until none of the pids exist
633 local _list _prefix _nlist _j
636 if [ -z "$_list" ]; then
643 if kill -0 $_j 2>/dev/null; then
644 _nlist="${_nlist}${_nlist:+ }$_j"
645 [ -n "$_prefix" ] && sleep 1
648 if [ -z "$_nlist" ]; then
652 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
654 pwait $_list 2>/dev/null
656 if [ -n "$_prefix" ]; then
662 # get_pidfile_from_conf string file
664 # Takes a string to search for in the specified file.
665 # Ignores lines with traditional comment characters.
669 # if get_pidfile_from_conf string file; then
670 # pidfile="$_pidfile_from_conf"
672 # pidfile='appropriate default'
675 get_pidfile_from_conf()
677 if [ -z "$1" -o -z "$2" ]; then
678 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
681 local string file line
683 string="$1" ; file="$2"
685 if [ ! -s "$file" ]; then
686 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
691 *[#\;]*${string}*) continue ;;
692 *${string}*) break ;;
696 if [ -n "$line" ]; then
698 _pidfile_from_conf="/${line%%[\"\;]*}"
706 # If rc_quiet is set (usually as a result of using faststart at
707 # boot time) check if rc_startmsgs is enabled.
711 if [ -n "$rc_quiet" ]; then
712 checkyesno rc_startmsgs
720 # Preferred method to use when displaying start messages in lieu of echo.
724 check_startmsgs && echo "$@"
728 # run_rc_command argument
729 # Search for argument in the list of supported commands, which is:
730 # "start stop restart rcvar status poll ${extra_commands}"
731 # If there's a match, run ${argument}_cmd or the default method
734 # If argument has a given prefix, then change the operation as follows:
737 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
738 # force Set ${rcvar} to YES, and set rc_force=yes
739 # one Set ${rcvar} to YES
740 # quiet Don't output some diagnostics, and set rc_quiet=yes
742 # The following globals are used:
744 # Name Needed Purpose
745 # ---- ------ -------
746 # name y Name of script.
748 # command n Full path to command.
749 # Not needed if ${rc_arg}_cmd is set for
752 # command_args n Optional args/shell directives for command.
754 # command_interpreter n If not empty, command is interpreted, so
755 # call check_{pidfile,process}() appropriately.
757 # desc n Description of script.
759 # extra_commands n List of extra commands supported.
761 # pidfile n If set, use check_pidfile $pidfile $command,
762 # otherwise use check_process $command.
763 # In either case, only check if $command is set.
765 # procname n Process name to check for instead of $command.
767 # rcvar n This is checked with checkyesno to determine
768 # if the action should be run.
770 # ${name}_program n Full path to command.
771 # Meant to be used in /etc/rc.conf to override
774 # ${name}_chroot n Directory to chroot to before running ${command}
775 # Requires /usr to be mounted.
777 # ${name}_chdir n Directory to cd to before running ${command}
778 # (if not using ${name}_chroot).
780 # ${name}_flags n Arguments to call ${command} with.
781 # NOTE: $flags from the parent environment
782 # can be used to override this.
784 # ${name}_env n Environment variables to run ${command} with.
786 # ${name}_env_file n File to source variables to run ${command} with.
788 # ${name}_fib n Routing table number to run ${command} with.
790 # ${name}_nice n Nice level to run ${command} at.
792 # ${name}_oomprotect n Don't kill ${command} when swap space is exhausted.
794 # ${name}_umask n The file creation mask to run ${command} with.
796 # ${name}_user n User to run ${command} as, using su(1) if not
797 # using ${name}_chroot.
798 # Requires /usr to be mounted.
800 # ${name}_group n Group to run chrooted ${command} as.
801 # Requires /usr to be mounted.
803 # ${name}_groups n Comma separated list of supplementary groups
804 # to run the chrooted ${command} with.
805 # Requires /usr to be mounted.
807 # ${name}_prepend n Command added before ${command}.
809 # ${name}_setup n Command executed before ${command}.
811 # ${name}_login_class n Login class to use, else "daemon".
813 # ${name}_limits n limits(1) to apply to ${command}.
815 # ${rc_arg}_cmd n If set, use this as the method when invoked;
816 # Otherwise, use default command (see below)
818 # ${rc_arg}_precmd n If set, run just before performing the
819 # ${rc_arg}_cmd method in the default
820 # operation (i.e, after checking for required
821 # bits and process (non)existence).
822 # If this completes with a non-zero exit code,
823 # don't run ${rc_arg}_cmd.
825 # ${rc_arg}_postcmd n If set, run just after performing the
826 # ${rc_arg}_cmd method, if that method
827 # returned a zero exit code.
829 # required_dirs n If set, check for the existence of the given
830 # directories before running a (re)start command.
832 # required_files n If set, check for the readability of the given
833 # files before running a (re)start command.
835 # required_modules n If set, ensure the given kernel modules are
836 # loaded before running a (re)start command.
837 # The check and possible loads are actually
838 # done after start_precmd so that the modules
839 # aren't loaded in vain, should the precmd
840 # return a non-zero status to indicate a error.
841 # If a word in the list looks like "foo:bar",
842 # "foo" is the KLD file name and "bar" is the
843 # module name. If a word looks like "foo~bar",
844 # "foo" is the KLD file name and "bar" is a
845 # egrep(1) pattern matching the module name.
846 # Otherwise the module name is assumed to be
847 # the same as the KLD file name, which is most
848 # common. See load_kld().
850 # required_vars n If set, perform checkyesno on each of the
851 # listed variables before running the default
854 # Default behaviour for a given argument, if no override method is
857 # Argument Default behaviour
858 # -------- -----------------
859 # start if !running && checkyesno ${rcvar}
863 # rc_pid=$(check_pidfile $pidfile $command)
865 # rc_pid=$(check_process $command)
866 # kill $sig_stop $rc_pid
867 # wait_for_pids $rc_pid
868 # ($sig_stop defaults to TERM.)
870 # reload Similar to stop, except use $sig_reload instead,
871 # and don't wait_for_pids.
872 # $sig_reload defaults to HUP.
873 # Note that `reload' isn't provided by default,
874 # it should be enabled via $extra_commands.
876 # restart Run `stop' then `start'.
878 # status Show if ${command} is running, etc.
880 # poll Wait for ${command} to exit.
882 # rcvar Display what rc.conf variable is used (if any).
884 # enabled Return true if the service is enabled.
886 # describe Show the service's description
888 # extracommands Show the service's extra commands
890 # Variables available to methods, and after run_rc_command() has
895 # rc_arg Argument to command, after fast/force/one processing
898 # rc_flags Flags to start the default command with.
899 # Defaults to ${name}_flags, unless overridden
900 # by $flags from the environment.
901 # This variable may be changed by the precmd method.
903 # rc_service Path to the service being executed, in case the service
904 # needs to re-invoke itself.
906 # rc_pid PID of command (if appropriate)
908 # rc_fast Not empty if "fast" was provided (q.v.)
910 # rc_force Not empty if "force" was provided (q.v.)
912 # rc_quiet Not empty if "quiet" was provided
919 if [ -z "$name" ]; then
920 err 3 'run_rc_command: $name is not set.'
923 # Don't repeat the first argument when passing additional command-
924 # line arguments to the command subroutines.
931 fast*) # "fast" prefix; don't check pid
932 rc_arg=${rc_arg#fast}
936 force*) # "force" prefix; always run
939 rc_arg=${rc_arg#${_rc_prefix}}
940 if [ -n "${rcvar}" ]; then
944 one*) # "one" prefix; set ${rcvar}=yes
946 rc_arg=${rc_arg#${_rc_prefix}}
947 if [ -n "${rcvar}" ]; then
951 quiet*) # "quiet" prefix; omit some messages
953 rc_arg=${rc_arg#${_rc_prefix}}
958 eval _override_command=\$${name}_program
959 command=${_override_command:-$command}
961 _keywords="start stop restart rcvar enable disable delete enabled describe extracommands $extra_commands"
964 _procname=${procname:-${command}}
966 # setup pid check command
967 if [ -n "$_procname" ]; then
968 if [ -n "$pidfile" ]; then
969 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
971 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
973 _keywords="${_keywords} status poll"
975 if [ ! -z "${status_cmd}" ]
977 _keywords="${_keywords} status"
981 if [ -z "$rc_arg" ]; then
985 if [ "$rc_arg" = "enabled" ] ; then
990 if [ -n "$flags" ]; then # allow override from environment
993 eval rc_flags=\$${name}_flags
995 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
996 _nice=\$${name}_nice _user=\$${name}_user \
997 _group=\$${name}_group _groups=\$${name}_groups \
998 _fib=\$${name}_fib _env=\$${name}_env \
999 _prepend=\$${name}_prepend _login_class=\${${name}_login_class:-daemon} \
1000 _limits=\$${name}_limits _oomprotect=\$${name}_oomprotect \
1001 _setup=\$${name}_setup _env_file=\$${name}_env_file \
1002 _umask=\$${name}_umask
1004 if [ -n "$_env_file" ] && [ -r "${_env_file}" ]; then # load env from file
1010 if [ -n "$_user" ]; then # unset $_user if running as that user
1011 if [ "$_user" = "$(eval $IDCMD)" ]; then
1016 [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary
1018 for _elem in $_keywords; do
1019 if [ "$_elem" != "$rc_arg" ]; then
1022 # if ${rcvar} is set, $1 is not "rcvar", "describe",
1023 # "enable" or "delete", and ${rc_pid} is not set, run:
1024 # checkyesno ${rcvar}
1025 # and return if that failed
1027 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" \
1028 -a "$rc_arg" != "delete" -a "$rc_arg" != "enable" \
1029 -a "$rc_arg" != "describe" ] ||
1030 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then
1031 if ! checkyesno ${rcvar}; then
1032 if [ -n "${rc_quiet}" ]; then
1035 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
1036 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
1037 echo "instead of '${rc_arg}'."
1042 if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then
1043 if [ -z "$rc_quiet" ]; then
1044 echo 1>&2 "${name} already running? " \
1050 # if there's a custom ${XXX_cmd},
1051 # run that instead of the default
1053 eval _cmd=\$${rc_arg}_cmd \
1054 _precmd=\$${rc_arg}_precmd \
1055 _postcmd=\$${rc_arg}_postcmd
1057 if [ -n "$_cmd" ]; then
1058 if [ -n "$_env" ]; then
1059 eval "export -- $_env"
1061 _run_rc_precmd || return 1
1062 _run_rc_doit "$_cmd $rc_extra_args" || return 1
1067 case "$rc_arg" in # default operations...
1070 if [ -n "$desc" ]; then
1076 echo "$extra_commands"
1080 _out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=YES") &&
1081 echo "$name enabled in ${_out%%:*}"
1085 _out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=NO") &&
1086 echo "$name disabled in ${_out%%:*}"
1091 for _file in $(sysrc -lEs "$name"); do
1092 _out=$(sysrc -Fif $_file "$rcvar") && _files="$_files $_file"
1094 /usr/sbin/sysrc -x "$rcvar" && echo "$rcvar deleted in ${_files# }"
1095 # delete file in rc.conf.d if desired and empty.
1096 checkyesno service_delete_empty || _files=
1097 for _file in $_files; do
1098 [ "$_file" = "${_file#*/rc.conf.d/}" ] && continue
1099 [ $(/usr/bin/stat -f%z $_file) -gt 0 ] && continue
1100 /bin/rm "$_file" && echo "Empty file $_file removed"
1105 _run_rc_precmd || return 1
1106 if [ -n "$rc_pid" ]; then
1107 echo "${name} is running as pid $rc_pid."
1109 echo "${name} is not running."
1116 if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then
1117 warn "run_rc_command: cannot run $command"
1121 if ! _run_rc_precmd; then
1122 warn "failed precmd routine for ${name}"
1126 # setup the full command to run
1128 startmsg "Starting ${name}."
1129 if [ -n "$_chroot" ]; then
1132 ${_nice:+nice -n $_nice }\
1133 ${_fib:+setfib -F $_fib }\
1134 ${_env:+env $_env }\
1135 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
1136 $_chroot $command $rc_flags $command_args"
1138 _cd="${_chdir:+cd $_chdir && }"
1140 ${_fib:+setfib -F $_fib }\
1141 ${_env:+env $_env }\
1142 $command $rc_flags $command_args"
1143 if [ -n "$_user" ]; then
1144 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
1146 if [ -n "$_nice" ]; then
1147 if [ -z "$_user" ]; then
1148 _doit="sh -c \"$_doit\""
1150 _doit="nice -n $_nice $_doit"
1152 if [ -n "$_prepend" ]; then
1153 _doit="$_prepend $_doit"
1157 if [ -n "$_setup" ]; then
1158 if ! _run_rc_doit "$_setup"; then
1159 warn "failed to setup ${name}"
1163 # Prepend default limits
1164 _doit="$_cd limits -C $_login_class $_limits $_doit"
1166 # run the full command
1168 if ! _run_rc_doit "$_doit"; then
1169 warn "failed to start ${name}"
1173 # finally, run postcmd
1179 if [ -z "$rc_pid" ]; then
1180 [ -n "$rc_fast" ] && return 0
1185 _run_rc_precmd || return 1
1187 # send the signal to stop
1189 echo "Stopping ${name}."
1190 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
1191 _run_rc_doit "$_doit" || return 1
1193 # wait for the command to exit,
1195 wait_for_pids $rc_pid
1201 if [ -z "$rc_pid" ]; then
1206 _run_rc_precmd || return 1
1208 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
1209 _run_rc_doit "$_doit" || return 1
1215 # prevent restart being called more
1216 # than once by any given script
1218 if ${_rc_restart_done:-false}; then
1221 _rc_restart_done=true
1223 _run_rc_precmd || return 1
1225 # run those in a subshell to keep global variables
1226 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
1227 ( run_rc_command ${_rc_prefix}start $rc_extra_args )
1229 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
1235 _run_rc_precmd || return 1
1236 if [ -n "$rc_pid" ]; then
1237 wait_for_pids $rc_pid
1244 if [ -n "$desc" ]; then
1250 # Get unique vars in $rcvar $rcvars
1251 for _v in $rcvar $rcvars; do
1253 $_v\ *|\ *$_v|*\ $_v\ *) ;;
1254 *) v="${v# } $_v" ;;
1258 # Display variables.
1260 if [ -z "$_v" ]; then
1264 eval _desc=\$${_v}_desc
1265 eval _defval=\$${_v}_defval
1268 eval echo \"$_v=\\\"\$$_v\\\"\"
1269 # decode multiple lines of _desc
1270 while [ -n "$_desc" ]; do
1273 echo "# $_h ${_desc%%^^*}"
1278 echo "# $_h ${_desc}"
1283 echo "# (default: \"$_defval\")"
1294 # Apply protect(1) to the PID if ${name}_oomprotect is set.
1297 # We cannot use protect(1) inside jails.
1298 if [ -n "$_oomprotect" ] && [ -f "${PROTECT}" ] &&
1299 [ "$(sysctl -n security.jail.jailed)" -eq 0 ]; then
1300 [ -z "${rc_pid}" ] && eval $_pidcmd
1301 case $_oomprotect in
1303 ${PROTECT} -d -i -p ${rc_pid}
1306 ${PROTECT} -p ${rc_pid}
1316 echo 1>&2 "$0: unknown directive '$rc_arg'."
1322 # Helper functions for run_rc_command: common code.
1323 # They use such global variables besides the exported rc_* ones:
1326 # ------------------
1333 check_required_before "$rc_arg" || return 1
1335 if [ -n "$_precmd" ]; then
1336 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
1337 eval "$_precmd $rc_extra_args"
1340 # If precmd failed and force isn't set, request exit.
1341 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1346 check_required_after "$rc_arg" || return 1
1353 if [ -n "$_postcmd" ]; then
1354 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
1355 eval "$_postcmd $rc_extra_args"
1365 debug "run_rc_command: doit: $*"
1367 ${_umask:+umask ${_umask}}
1372 # If command failed and force isn't set, request exit.
1373 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1380 _run_rc_notrunning()
1384 if [ -n "$pidfile" ]; then
1385 _pidmsg=" (check $pidfile)."
1389 echo 1>&2 "${name} not running?${_pidmsg}"
1396 _cmd="kill -$1 $rc_pid"
1397 if [ -n "$_user" ]; then
1398 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
1404 # run_rc_script file arg
1405 # Start the script `file' with `arg', and correctly handle the
1406 # return value from the script.
1407 # If `file' ends with `.sh' and lives in /etc/rc.d, ignore it as it's
1408 # an old-style startup file.
1409 # If `file' ends with `.sh' and does not live in /etc/rc.d, it's sourced
1410 # into the current environment if $rc_fast_and_loose is set; otherwise
1411 # it is run as a child process.
1412 # If `file' appears to be a backup or scratch file, ignore it.
1413 # Otherwise if it is executable run as a child process.
1419 if [ -z "$_file" -o -z "$_arg" ]; then
1420 err 3 'USAGE: run_rc_script file arg'
1423 unset name command command_args command_interpreter \
1424 extra_commands pidfile procname \
1425 rcvar rcvars rcvars_obsolete required_dirs required_files \
1427 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1431 /etc/rc.d/*.sh) # no longer allowed in the base
1432 warn "Ignoring old-style startup script $_file"
1434 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
1435 warn "Ignoring scratch file $_file"
1437 *) # run in subshell
1438 if [ -x $_file ]; then
1439 if [ -n "$rc_boottrace" ]; then
1440 boottrace_fn "$_file" "$_arg"
1441 elif [ -n "$rc_fast_and_loose" ]; then
1444 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3
1445 trap "echo Script $_file interrupted >&2 ; exit 1" 2
1446 trap "echo Script $_file running >&2" 29
1447 set $_arg; . $_file )
1460 if [ -n "$rc_fast_and_loose" ]; then
1461 boottrace_sysctl "$_file start"
1463 boottrace_sysctl "$_file done"
1465 $boottrace_cmd "$_file" "$_arg"
1471 ${SYSCTL} kern.boottrace.boottrace="$1"
1475 # load_rc_config [service]
1476 # Source in the configuration file(s) for a given service.
1477 # If no service is specified, only the global configuration
1478 # file(s) will be loaded.
1482 local _name _rcvar_val _var _defval _v _msg _new _d
1485 if ${_rc_conf_loaded:-false}; then
1488 if [ -r /etc/defaults/rc.conf ]; then
1489 debug "Sourcing /etc/defaults/rc.conf"
1490 . /etc/defaults/rc.conf
1492 elif [ -r /etc/rc.conf ]; then
1493 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1496 _rc_conf_loaded=true
1499 # If a service name was specified, attempt to load
1500 # service-specific configuration
1501 if [ -n "$_name" ] ; then
1502 for _d in /etc ${local_startup}; do
1504 if [ -f ${_d}/rc.conf.d/"$_name" ]; then
1505 debug "Sourcing ${_d}/rc.conf.d/$_name"
1506 . ${_d}/rc.conf.d/"$_name"
1507 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then
1509 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do
1510 if [ -f "$_rc" ] ; then
1511 debug "Sourcing $_rc"
1519 # Set defaults if defined.
1520 for _var in $rcvar $rcvars; do
1521 eval _defval=\$${_var}_defval
1522 if [ -n "$_defval" ]; then
1523 eval : \${$_var:=\$${_var}_defval}
1527 # check obsolete rc.conf variables
1528 for _var in $rcvars_obsolete; do
1530 eval _msg=\$${_var}_obsolete_msg
1531 eval _new=\$${_var}_newvar
1536 if [ -z "$_new" ]; then
1539 eval $_new=\"\$$_var\"
1540 if [ -z "$_msg" ]; then
1541 _msg="Use \$$_new instead."
1544 warn "\$$_var is obsolete. $_msg"
1551 # load_rc_config_var name var
1552 # Read the rc.conf(5) var for name and set in the
1553 # current shell, using load_rc_config in a subshell to prevent
1554 # unwanted side effects from other variable assignments.
1556 load_rc_config_var()
1558 if [ $# -ne 2 ]; then
1559 err 3 'USAGE: load_rc_config_var name var'
1562 load_rc_config '$1' >/dev/null;
1563 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1564 echo '$2'=\'\''${'$2'}\'\'';
1571 # Print a usage string for $0, with `commands' being a list of
1576 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
1580 echo -n 1>&2 "$_sep$_elem"
1588 # err exitval message
1589 # Display message to stderr and log to the syslog, and exit with exitval.
1596 if [ -x /usr/bin/logger ]; then
1597 logger "$0: ERROR: $*"
1599 echo 1>&2 "$0: ERROR: $*"
1605 # Display message to stderr and log to the syslog.
1609 if [ -x /usr/bin/logger ]; then
1610 logger "$0: WARNING: $*"
1612 echo 1>&2 "$0: WARNING: $*"
1617 # Display informational message to stdout and log to syslog.
1622 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1623 if [ -x /usr/bin/logger ]; then
1624 logger "$0: INFO: $*"
1633 # If debugging is enabled in rc.conf output message to stderr.
1634 # BEWARE that you don't call any subroutine that itself calls this
1640 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1641 if [ -x /usr/bin/logger ]; then
1642 logger "$0: DEBUG: $*"
1644 echo 1>&2 "$0: DEBUG: $*"
1650 # backup_file action file cur backup
1651 # Make a backup copy of `file' into `cur', and save the previous
1652 # version of `cur' as `backup'.
1654 # The `action' keyword can be one of the following:
1656 # add `file' is now being backed up (and is possibly
1657 # being reentered into the backups system). `cur'
1660 # update `file' has changed and needs to be backed up.
1661 # If `cur' exists, it is copied to `back'
1662 # and then `file' is copied to `cur'.
1664 # remove `file' is no longer being tracked by the backups
1665 # system. `cur' is moved `back'.
1677 if [ -f $_cur ]; then
1681 chown root:wheel $_cur
1689 # make_symlink src link
1690 # Make a symbolic link 'link' to src from basedir. If the
1691 # directory in which link is to be created does not exist
1692 # a warning will be displayed and an error will be returned.
1693 # Returns 0 on success, 1 otherwise.
1697 local src link linkdir _me
1700 linkdir="`dirname $link`"
1701 _me="make_symlink()"
1703 if [ -z "$src" -o -z "$link" ]; then
1704 warn "$_me: requires two arguments."
1707 if [ ! -d "$linkdir" ]; then
1708 warn "$_me: the directory $linkdir does not exist."
1711 if ! ln -sf $src $link; then
1712 warn "$_me: unable to make a symbolic link from $link to $src"
1718 # devfs_rulesets_from_file file
1719 # Reads a set of devfs commands from file, and creates
1720 # the specified rulesets with their rules. Returns non-zero
1721 # if there was an error.
1723 devfs_rulesets_from_file()
1725 local file _err _me _opts
1727 _me="devfs_rulesets_from_file"
1730 if [ -z "$file" ]; then
1731 warn "$_me: you must specify a file"
1734 if [ ! -e "$file" ]; then
1735 debug "$_me: no such file ($file)"
1739 # Disable globbing so that the rule patterns are not expanded
1740 # by accident with matching filesystem entries.
1743 debug "reading rulesets from file ($file)"
1751 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1752 if [ -z "$rulenum" ]; then
1753 warn "$_me: cannot extract rule number ($line)"
1757 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1758 if [ -z "$rulename" ]; then
1759 warn "$_me: cannot extract rule name ($line)"
1763 eval $rulename=\$rulenum
1764 debug "found ruleset: $rulename=$rulenum"
1765 if ! /sbin/devfs rule -s $rulenum delset; then
1771 rulecmd="${line%%"\#*"}"
1772 # evaluate the command incase it includes
1774 if [ -n "$rulecmd" ]; then
1775 debug "adding rule ($rulecmd)"
1776 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1784 if [ $_err -ne 0 ]; then
1785 debug "error in $_me"
1789 case $_opts in *f*) ;; *) set +f ;; esac
1793 # devfs_init_rulesets
1794 # Initializes rulesets from configuration files. Returns
1795 # non-zero if there was an error.
1797 devfs_init_rulesets()
1800 _me="devfs_init_rulesets"
1802 # Go through this only once
1803 if [ -n "$devfs_rulesets_init" ]; then
1804 debug "$_me: devfs rulesets already initialized"
1807 for file in $devfs_rulesets; do
1808 if ! devfs_rulesets_from_file $file; then
1809 warn "$_me: could not read rules from $file"
1813 devfs_rulesets_init=1
1814 debug "$_me: devfs rulesets initialized"
1818 # devfs_set_ruleset ruleset [dir]
1819 # Sets the default ruleset of dir to ruleset. The ruleset argument
1820 # must be a ruleset name as specified in devfs.rules(5) file.
1821 # Returns non-zero if it could not set it successfully.
1826 [ -n "$1" ] && eval rs=\$$1 || rs=
1827 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1828 _me="devfs_set_ruleset"
1830 if [ -z "$rs" ]; then
1831 warn "$_me: you must specify a ruleset number"
1834 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1835 if ! /sbin/devfs $devdir ruleset $rs; then
1836 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1842 # devfs_apply_ruleset ruleset [dir]
1843 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1844 # The ruleset argument must be a ruleset name as specified
1845 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1846 # if it could not apply the ruleset.
1848 devfs_apply_ruleset()
1851 [ -n "$1" ] && eval rs=\$$1 || rs=
1852 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1853 _me="devfs_apply_ruleset"
1855 if [ -z "$rs" ]; then
1856 warn "$_me: you must specify a ruleset"
1859 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1860 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1861 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1867 # devfs_domount dir [ruleset]
1868 # Mount devfs on dir. If ruleset is specified it is set
1869 # on the mount-point. It must also be a ruleset name as specified
1870 # in a devfs.rules(5) file. Returns 0 on success.
1876 [ -n "$2" ] && rs=$2 || rs=
1877 _me="devfs_domount()"
1879 if [ -z "$devdir" ]; then
1880 warn "$_me: you must specify a mount-point"
1883 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1884 if ! mount -t devfs dev "$devdir"; then
1885 warn "$_me: Unable to mount devfs on $devdir"
1888 if [ -n "$rs" ]; then
1890 devfs_set_ruleset $rs $devdir
1891 devfs -m $devdir rule applyset
1896 # Provide a function for normalizing the mounting of memory
1897 # filesystems. This should allow the rest of the code here to remain
1898 # as close as possible between 5-current and 4-stable.
1901 # $3 = (optional) extra mdmfs flags
1904 if [ -n "$3" ]; then
1907 /sbin/mdmfs $flags -s $1 ${mfs_type} $2
1910 # Code common to scripts that need to load a kernel module
1911 # if it isn't in the kernel yet. Syntax:
1912 # load_kld [-e regex] [-m module] file
1913 # where -e or -m chooses the way to check if the module
1914 # is already loaded:
1915 # regex is egrep'd in the output from `kldstat -v',
1916 # module is passed to `kldstat -m'.
1917 # The default way is as though `-m file' were specified.
1920 local _loaded _mod _opt _re
1922 while getopts "e:m:" _opt; do
1925 m) _mod="$OPTARG" ;;
1926 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1929 shift $(($OPTIND - 1))
1930 if [ $# -ne 1 ]; then
1931 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1935 if [ -n "$_re" ]; then
1936 if kldstat -v | egrep -q -e "$_re"; then
1940 if kldstat -q -m "$_mod"; then
1945 if ! kldload "$1"; then
1946 warn "Unable to load kernel module $1"
1949 info "$1 kernel module loaded."
1952 debug "load_kld: $1 kernel module already loaded."
1957 # ltr str src dst [var]
1958 # Change every $src in $str to $dst.
1959 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1960 # awk(1). If var is non-NULL, set it to the result.
1963 local _str _src _dst _out _com _var
1971 for _com in ${_str}; do
1972 if [ -z "${_out}" ]; then
1975 _out="${_out}${_dst}${_com}"
1978 if [ -n "${_var}" ]; then
1979 setvar "${_var}" "${_out}"
1985 # Creates a list of providers for GELI encryption.
1988 local devices devices2
1989 local provider mountpoint type options rest
1991 # Create list of GELI providers from fstab.
1992 while read provider mountpoint type options rest ; do
1993 case ":${options}" in
2002 case ":${provider}" in
2007 # Skip swap devices.
2008 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
2011 devices="${devices} ${provider}"
2016 # Append providers from geli_devices.
2017 devices="${devices} ${geli_devices}"
2019 for provider in ${devices}; do
2020 provider=${provider%.eli}
2021 provider=${provider#/dev/}
2022 devices2="${devices2} ${provider}"
2028 # Originally, root mount hold had to be released before mounting
2029 # the root filesystem. This delayed the boot, so it was changed
2030 # to only wait if the root device isn't readily available. This
2031 # can result in rc scripts executing before all the devices - such
2032 # as graid(8), or USB disks - can be accessed. This function can
2033 # be used to explicitly wait for root mount holds to be released.
2036 local wait waited holders
2040 holders="$(sysctl -n vfs.root_mount_hold)"
2041 if [ -z "${holders}" ]; then
2044 if [ ${waited} -eq 0 ]; then
2045 echo -n "Waiting ${root_hold_delay}s" \
2046 "for the root mount holders: ${holders}"
2050 if [ ${waited} -ge ${root_hold_delay} ]; then
2055 waited=$(($waited + 1))
2059 # Find scripts in local_startup directories that use the old syntax
2061 find_local_scripts_old() {
2064 for dir in ${local_startup}; do
2065 if [ -d "${dir}" ]; then
2066 for file in ${dir}/[0-9]*.sh; do
2067 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
2069 zlist="$zlist $file"
2071 for file in ${dir}/[!0-9]*.sh; do
2072 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
2074 slist="$slist $file"
2080 find_local_scripts_new() {
2082 for dir in ${local_startup}; do
2083 if [ -d "${dir}" ]; then
2084 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
2087 *) if [ -x "$file" ]; then
2088 local_rc="${local_rc} ${file}"
2097 # check_required_{before|after} command
2098 # Check for things required by the command before and after its precmd,
2099 # respectively. The two separate functions are needed because some
2100 # conditions should prevent precmd from being run while other things
2101 # depend on precmd having already been run.
2103 check_required_before()
2109 for _f in $required_vars; do
2110 if ! checkyesno $_f; then
2111 warn "\$${_f} is not enabled."
2112 if [ -z "$rc_force" ]; then
2118 for _f in $required_dirs; do
2119 if [ ! -d "${_f}/." ]; then
2120 warn "${_f} is not a directory."
2121 if [ -z "$rc_force" ]; then
2127 for _f in $required_files; do
2128 if [ ! -r "${_f}" ]; then
2129 warn "${_f} is not readable."
2130 if [ -z "$rc_force" ]; then
2141 check_required_after()
2147 for _f in $required_modules; do
2149 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
2150 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
2153 if ! load_kld ${_args}; then
2154 if [ -z "$rc_force" ]; then
2166 # Return true if security.jail.$mib exists and set to 1.
2173 if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then
2181 # check_kern_features mib
2182 # Return existence of kern.features.* sysctl MIB as true or
2183 # false. The result will be cached in $_rc_cache_kern_features_
2184 # namespace. "0" means the kern.features.X exists.
2186 check_kern_features()
2190 [ -n "$1" ] || return 1;
2191 eval _v=\$_rc_cache_kern_features_$1
2192 [ -n "$_v" ] && return "$_v";
2194 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then
2195 eval _rc_cache_kern_features_$1=0
2198 eval _rc_cache_kern_features_$1=1
2203 # check_namevarlist var
2204 # Return "0" if ${name}_var is reserved in rc.subr.
2206 _rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend setup"
2211 for _v in $_rc_namevarlist; do
2220 # _echoonce var msg mode
2221 # mode=0: Echo $msg if ${$var} is empty.
2222 # After doing echo, a string is set to ${$var}.
2224 # mode=1: Echo $msg if ${$var} is a string with non-zero length.
2228 local _var _msg _mode
2234 1) [ -n "$_var" ] && echo "$_msg" ;;
2235 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
2239 # If the loader env variable rc.debug is set, turn on debugging. rc.conf will
2240 # still override this, but /etc/defaults/rc.conf can't unconditionally set this
2241 # since it would undo what we've done here.
2242 if kenv -q rc.debug > /dev/null ; then
2246 boottrace_cmd=`command -v boottrace`
2247 if [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1" ]; then