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 [ -z "${_rc_subr_loaded}" ]; then
46 SYSCTL_N="${SYSCTL} -n"
49 IDCMD="if [ -x $ID ]; then $ID -un; fi"
51 JID=`$PS -p $$ -o jid=`
57 # set_rcvar [var] [defval] [desc]
59 # Echo or define a rc.conf(5) variable name. Global variable
62 # If no argument is specified, echo "${name}_enable".
64 # If only a var is specified, echo "${var}_enable".
66 # If var and defval are specified, the ${var} is defined as
67 # rc.conf(5) variable and the default value is ${defvar}. An
68 # optional argument $desc can also be specified to add a
69 # description for that.
81 debug "rcvar_define: \$$1=$2 is added" \
82 " as a rc.conf(5) variable."
86 rcvars="${rcvars# } $_var"
87 eval ${_var}_defval=\"$2\"
89 # encode multiple lines of _desc
91 eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
93 eval ${_var}_desc=\"\${${_var}_desc#^^}\"
98 # set_rcvar_obsolete oldvar [newvar] [msg]
99 # Define obsolete variable.
100 # Global variable $rcvars_obsolete is used.
106 debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
108 rcvars_obsolete="${rcvars_obsolete# } $1"
109 eval ${1}_newvar=\"$2\"
111 eval ${_var}_obsolete_msg=\"$*\"
115 # force_depend script [rcvar]
116 # Force a service to start. Intended for use by services
117 # to resolve dependency issues.
118 # $1 - filename of script, in /etc/rc.d, to run
119 # $2 - name of the script's rcvar (minus the _enable)
123 local _depend _dep_rcvar
126 _dep_rcvar="${2:-$1}_enable"
128 [ -n "$rc_fast" ] && ! checkyesno always_force_depends &&
129 checkyesno $_dep_rcvar && return 0
131 /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0
133 info "${name} depends on ${_depend}, which will be forced to start."
134 if ! /etc/rc.d/${_depend} forcestart; then
135 warn "Unable to force ${_depend}. It may already be running."
142 # Test $1 variable, and warn if not set to YES or NO.
143 # Return 0 if it's "yes" (et al), nonzero otherwise.
148 debug "checkyesno: $1 is set to $_value."
151 # "yes", "true", "on", or "1"
152 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
156 # "no", "false", "off", or "0"
157 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
161 warn "\$${1} is not set properly - see rc.conf(5)."
169 # print the list in reverse order
175 _revlist="$_revfile $_revlist"
181 # If booting directly to multiuser or $always is enabled,
182 # send SIGTERM to the parent (/etc/rc) to abort the boot.
183 # Otherwise just exit.
190 # "yes", "true", "on", or "1"
191 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
198 if [ "$autoboot" = yes -o "$always" = true ]; then
199 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
206 # mount_critical_filesystems type
207 # Go through the list of critical filesystems as provided in
208 # the rc.conf(5) variable $critical_filesystems_${type}, checking
209 # each one to see if it is mounted, and if it is not, mounting it.
211 mount_critical_filesystems()
213 eval _fslist=\$critical_filesystems_${1}
214 for _fs in $_fslist; do
217 while read what _on on _type type; do
218 if [ $on = $_fs ]; then
225 mount $_fs >/dev/null 2>&1
232 # check_pidfile pidfile procname [interpreter]
233 # Parses the first line of pidfile for a PID, and ensures
234 # that the process is running and matches procname.
235 # Prints the matching PID upon success, nothing otherwise.
236 # interpreter is optional; see _find_processes() for details.
243 if [ -z "$_pidfile" -o -z "$_procname" ]; then
244 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
246 if [ ! -f $_pidfile ]; then
247 debug "pid file ($_pidfile): not readable."
250 read _pid _junk < $_pidfile
251 if [ -z "$_pid" ]; then
252 debug "pid file ($_pidfile): no pid in file."
255 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
259 # check_process procname [interpreter]
260 # Ensures that a process (or processes) named procname is running.
261 # Prints a list of matching PIDs.
262 # interpreter is optional; see _find_processes() for details.
268 if [ -z "$_procname" ]; then
269 err 3 'USAGE: check_process procname [interpreter]'
271 _find_processes $_procname ${_interpreter:-.} '-ax'
275 # _find_processes procname interpreter psargs
276 # Search for procname in the output of ps generated by psargs.
277 # Prints the PIDs of any matching processes, space separated.
279 # If interpreter == ".", check the following variations of procname
280 # against the first word of each command:
282 # `basename procname`
283 # `basename procname` + ":"
284 # "(" + `basename procname` + ")"
285 # "[" + `basename procname` + "]"
287 # If interpreter != ".", read the first line of procname, remove the
288 # leading #!, normalise whitespace, append procname, and attempt to
289 # match that against each command, either as is, or with extra words
290 # at the end. As an alternative, to deal with interpreted daemons
291 # using perl, the basename of the interpreter plus a colon is also
292 # tried as the prefix to procname.
296 if [ $# -ne 3 ]; then
297 err 3 'USAGE: _find_processes procname interpreter psargs'
304 if [ $_interpreter != "." ]; then # an interpreted script
305 _script="${_chroot}${_chroot:+/}$_procname"
306 if [ -r "$_script" ]; then
307 read _interp < $_script # read interpreter name
310 _interp=${_interp#\#!} # strip #!
314 shift # drop env to get real name
317 if [ $_interpreter != $1 ]; then
318 warn "\$command_interpreter $_interpreter != $1"
322 warn "no shebang line in $_script"
327 warn "cannot read shebang line from $_script"
330 _interp="$* $_procname" # cleanup spaces, add _procname
333 _fp_match='case "$_argv" in
334 ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
335 else # a normal daemon
336 _procnamebn=${_procname##*/}
337 _fp_args='_arg0 _argv'
338 _fp_match='case "$_arg0" in
339 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
343 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
344 while read _npid _jid '"$_fp_args"'; do
346 if [ "$JID" -eq "$_jid" ];
347 then echo -n "$_pref$_npid";
354 # debug "in _find_processes: proccheck is ($_proccheck)."
359 # wait_for_pids pid [pid ...]
360 # spins until none of the pids exist
364 local _list _prefix _nlist _j
367 if [ -z "$_list" ]; then
374 if kill -0 $_j 2>/dev/null; then
375 _nlist="${_nlist}${_nlist:+ }$_j"
376 [ -n "$_prefix" ] && sleep 1
379 if [ -z "$_nlist" ]; then
383 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
385 pwait $_list 2>/dev/null
387 if [ -n "$_prefix" ]; then
393 # get_pidfile_from_conf string file
395 # Takes a string to search for in the specified file.
396 # Ignores lines with traditional comment characters.
400 # if get_pidfile_from_conf string file; then
401 # pidfile="$_pidfile_from_conf"
403 # pidfile='appropriate default'
406 get_pidfile_from_conf()
408 if [ -z "$1" -o -z "$2" ]; then
409 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
412 local string file line
414 string="$1" ; file="$2"
416 if [ ! -s "$file" ]; then
417 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
422 *[#\;]*${string}*) continue ;;
423 *${string}*) break ;;
427 if [ -n "$line" ]; then
429 _pidfile_from_conf="/${line%%[\"\;]*}"
437 # If rc_quiet is set (usually as a result of using faststart at
438 # boot time) check if rc_startmsgs is enabled.
442 if [ -n "$rc_quiet" ]; then
443 checkyesno rc_startmsgs
450 # run_rc_command argument
451 # Search for argument in the list of supported commands, which is:
452 # "start stop restart rcvar status poll ${extra_commands}"
453 # If there's a match, run ${argument}_cmd or the default method
456 # If argument has a given prefix, then change the operation as follows:
459 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
460 # force Set ${rcvar} to YES, and set rc_force=yes
461 # one Set ${rcvar} to YES
462 # quiet Don't output some diagnostics, and set rc_quiet=yes
464 # The following globals are used:
466 # Name Needed Purpose
467 # ---- ------ -------
468 # name y Name of script.
470 # command n Full path to command.
471 # Not needed if ${rc_arg}_cmd is set for
474 # command_args n Optional args/shell directives for command.
476 # command_interpreter n If not empty, command is interpreted, so
477 # call check_{pidfile,process}() appropriately.
479 # desc n Description of script.
481 # extra_commands n List of extra commands supported.
483 # pidfile n If set, use check_pidfile $pidfile $command,
484 # otherwise use check_process $command.
485 # In either case, only check if $command is set.
487 # procname n Process name to check for instead of $command.
489 # rcvar n This is checked with checkyesno to determine
490 # if the action should be run.
492 # ${name}_program n Full path to command.
493 # Meant to be used in /etc/rc.conf to override
496 # ${name}_chroot n Directory to chroot to before running ${command}
497 # Requires /usr to be mounted.
499 # ${name}_chdir n Directory to cd to before running ${command}
500 # (if not using ${name}_chroot).
502 # ${name}_flags n Arguments to call ${command} with.
503 # NOTE: $flags from the parent environment
504 # can be used to override this.
506 # ${name}_nice n Nice level to run ${command} at.
508 # ${name}_user n User to run ${command} as, using su(1) if not
509 # using ${name}_chroot.
510 # Requires /usr to be mounted.
512 # ${name}_group n Group to run chrooted ${command} as.
513 # Requires /usr to be mounted.
515 # ${name}_groups n Comma separated list of supplementary groups
516 # to run the chrooted ${command} with.
517 # Requires /usr to be mounted.
519 # ${rc_arg}_cmd n If set, use this as the method when invoked;
520 # Otherwise, use default command (see below)
522 # ${rc_arg}_precmd n If set, run just before performing the
523 # ${rc_arg}_cmd method in the default
524 # operation (i.e, after checking for required
525 # bits and process (non)existence).
526 # If this completes with a non-zero exit code,
527 # don't run ${rc_arg}_cmd.
529 # ${rc_arg}_postcmd n If set, run just after performing the
530 # ${rc_arg}_cmd method, if that method
531 # returned a zero exit code.
533 # required_dirs n If set, check for the existence of the given
534 # directories before running a (re)start command.
536 # required_files n If set, check for the readability of the given
537 # files before running a (re)start command.
539 # required_modules n If set, ensure the given kernel modules are
540 # loaded before running a (re)start command.
541 # The check and possible loads are actually
542 # done after start_precmd so that the modules
543 # aren't loaded in vain, should the precmd
544 # return a non-zero status to indicate a error.
545 # If a word in the list looks like "foo:bar",
546 # "foo" is the KLD file name and "bar" is the
547 # module name. If a word looks like "foo~bar",
548 # "foo" is the KLD file name and "bar" is a
549 # egrep(1) pattern matching the module name.
550 # Otherwise the module name is assumed to be
551 # the same as the KLD file name, which is most
552 # common. See load_kld().
554 # required_vars n If set, perform checkyesno on each of the
555 # listed variables before running the default
558 # Default behaviour for a given argument, if no override method is
561 # Argument Default behaviour
562 # -------- -----------------
563 # start if !running && checkyesno ${rcvar}
567 # rc_pid=$(check_pidfile $pidfile $command)
569 # rc_pid=$(check_process $command)
570 # kill $sig_stop $rc_pid
571 # wait_for_pids $rc_pid
572 # ($sig_stop defaults to TERM.)
574 # reload Similar to stop, except use $sig_reload instead,
575 # and doesn't wait_for_pids.
576 # $sig_reload defaults to HUP.
577 # Note that `reload' isn't provided by default,
578 # it should be enabled via $extra_commands.
580 # restart Run `stop' then `start'.
582 # status Show if ${command} is running, etc.
584 # poll Wait for ${command} to exit.
586 # rcvar Display what rc.conf variable is used (if any).
588 # Variables available to methods, and after run_rc_command() has
593 # rc_arg Argument to command, after fast/force/one processing
596 # rc_flags Flags to start the default command with.
597 # Defaults to ${name}_flags, unless overridden
598 # by $flags from the environment.
599 # This variable may be changed by the precmd method.
601 # rc_pid PID of command (if appropriate)
603 # rc_fast Not empty if "fast" was provided (q.v.)
605 # rc_force Not empty if "force" was provided (q.v.)
607 # rc_quiet Not empty if "quiet" was provided
614 if [ -z "$name" ]; then
615 err 3 'run_rc_command: $name is not set.'
618 # Don't repeat the first argument when passing additional command-
619 # line arguments to the command subroutines.
626 fast*) # "fast" prefix; don't check pid
627 rc_arg=${rc_arg#fast}
631 force*) # "force" prefix; always run
634 rc_arg=${rc_arg#${_rc_prefix}}
635 if [ -n "${rcvar}" ]; then
639 one*) # "one" prefix; set ${rcvar}=yes
641 rc_arg=${rc_arg#${_rc_prefix}}
642 if [ -n "${rcvar}" ]; then
646 quiet*) # "quiet" prefix; omit some messages
648 rc_arg=${rc_arg#${_rc_prefix}}
653 eval _override_command=\$${name}_program
654 command=${_override_command:-$command}
656 _keywords="start stop restart rcvar $extra_commands"
659 _procname=${procname:-${command}}
661 # setup pid check command
662 if [ -n "$_procname" ]; then
663 if [ -n "$pidfile" ]; then
664 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
666 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
668 if [ -n "$_pidcmd" ]; then
669 _keywords="${_keywords} status poll"
673 if [ -z "$rc_arg" ]; then
677 if [ -n "$flags" ]; then # allow override from environment
680 eval rc_flags=\$${name}_flags
682 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
683 _nice=\$${name}_nice _user=\$${name}_user \
684 _group=\$${name}_group _groups=\$${name}_groups
686 if [ -n "$_user" ]; then # unset $_user if running as that user
687 if [ "$_user" = "$(eval $IDCMD)" ]; then
692 [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary
694 for _elem in $_keywords; do
695 if [ "$_elem" != "$rc_arg" ]; then
698 # if ${rcvar} is set, $1 is not "rcvar"
699 # and ${rc_pid} is not set, then run
700 # checkyesno ${rcvar}
701 # and return if that failed
703 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] ||
704 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then
705 if ! checkyesno ${rcvar}; then
706 if [ -n "${rc_quiet}" ]; then
709 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
710 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
711 echo "instead of '${rc_arg}'."
716 # if there's a custom ${XXX_cmd},
717 # run that instead of the default
719 eval _cmd=\$${rc_arg}_cmd \
720 _precmd=\$${rc_arg}_precmd \
721 _postcmd=\$${rc_arg}_postcmd
723 if [ -n "$_cmd" ]; then
724 _run_rc_precmd || return 1
725 _run_rc_doit "$_cmd $rc_extra_args" || return 1
730 case "$rc_arg" in # default operations...
733 _run_rc_precmd || return 1
734 if [ -n "$rc_pid" ]; then
735 echo "${name} is running as pid $rc_pid."
737 echo "${name} is not running."
744 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
745 echo 1>&2 "${name} already running? (pid=$rc_pid)."
749 if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then
750 warn "run_rc_command: cannot run $command"
754 if ! _run_rc_precmd; then
755 warn "failed precmd routine for ${name}"
759 # setup the full command to run
761 check_startmsgs && echo "Starting ${name}."
762 if [ -n "$_chroot" ]; then
764 ${_nice:+nice -n $_nice }\
765 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
766 $_chroot $command $rc_flags $command_args"
769 ${_chdir:+cd $_chdir && }\
770 $command $rc_flags $command_args"
771 if [ -n "$_user" ]; then
772 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
774 if [ -n "$_nice" ]; then
775 if [ -z "$_user" ]; then
776 _doit="sh -c \"$_doit\""
778 _doit="nice -n $_nice $_doit"
782 # run the full command
784 if ! _run_rc_doit "$_doit"; then
785 warn "failed to start ${name}"
789 # finally, run postcmd
795 if [ -z "$rc_pid" ]; then
796 [ -n "$rc_fast" ] && return 0
801 _run_rc_precmd || return 1
803 # send the signal to stop
805 echo "Stopping ${name}."
806 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
807 _run_rc_doit "$_doit" || return 1
809 # wait for the command to exit,
811 wait_for_pids $rc_pid
817 if [ -z "$rc_pid" ]; then
822 _run_rc_precmd || return 1
824 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
825 _run_rc_doit "$_doit" || return 1
831 # prevent restart being called more
832 # than once by any given script
834 if ${_rc_restart_done:-false}; then
837 _rc_restart_done=true
839 _run_rc_precmd || return 1
841 # run those in a subshell to keep global variables
842 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
843 ( run_rc_command ${_rc_prefix}start $rc_extra_args )
845 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
851 _run_rc_precmd || return 1
852 if [ -n "$rc_pid" ]; then
853 wait_for_pids $rc_pid
860 if [ -n "$desc" ]; then
866 # Get unique vars in $rcvar $rcvars
867 for _v in $rcvar $rcvars; do
869 $_v\ *|\ *$_v|*\ $_v\ *) ;;
876 if [ -z "$_v" ]; then
880 eval _desc=\$${_v}_desc
881 eval _defval=\$${_v}_defval
884 eval echo \"$_v=\\\"\$$_v\\\"\"
885 # decode multiple lines of _desc
886 while [ -n "$_desc" ]; do
889 echo "# $_h ${_desc%%^^*}"
894 echo "# $_h ${_desc}"
899 echo "# (default: \"$_defval\")"
912 echo 1>&2 "$0: unknown directive '$rc_arg'."
918 # Helper functions for run_rc_command: common code.
919 # They use such global variables besides the exported rc_* ones:
929 check_required_before "$rc_arg" || return 1
931 if [ -n "$_precmd" ]; then
932 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
933 eval "$_precmd $rc_extra_args"
936 # If precmd failed and force isn't set, request exit.
937 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
942 check_required_after "$rc_arg" || return 1
949 if [ -n "$_postcmd" ]; then
950 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
951 eval "$_postcmd $rc_extra_args"
959 debug "run_rc_command: doit: $*"
963 # If command failed and force isn't set, request exit.
964 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
975 if [ -n "$pidfile" ]; then
976 _pidmsg=" (check $pidfile)."
980 echo 1>&2 "${name} not running?${_pidmsg}"
987 _cmd="kill -$1 $rc_pid"
988 if [ -n "$_user" ]; then
989 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
995 # run_rc_script file arg
996 # Start the script `file' with `arg', and correctly handle the
997 # return value from the script.
998 # If `file' ends with `.sh', it's sourced into the current environment
999 # when $rc_fast_and_loose is set, otherwise it is run as a child process.
1000 # If `file' appears to be a backup or scratch file, ignore it.
1001 # Otherwise if it is executable run as a child process.
1007 if [ -z "$_file" -o -z "$_arg" ]; then
1008 err 3 'USAGE: run_rc_script file arg'
1011 unset name command command_args command_interpreter \
1012 extra_commands pidfile procname \
1013 rcvar rcvars rcvars_obsolete required_dirs required_files \
1015 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1018 /etc/rc.d/*.sh) # no longer allowed in the base
1019 warn "Ignoring old-style startup script $_file"
1021 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
1022 warn "Ignoring scratch file $_file"
1024 *) # run in subshell
1025 if [ -x $_file ]; then
1026 if [ -n "$rc_fast_and_loose" ]; then
1029 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3
1030 trap "echo Script $_file interrupted >&2 ; exit 1" 2
1031 trap "echo Script $_file running >&2" 29
1032 set $_arg; . $_file )
1040 # load_rc_config name
1041 # Source in the configuration file for a given name.
1045 local _name _var _defval _v _msg _new
1047 if [ -z "$_name" ]; then
1048 err 3 'USAGE: load_rc_config name'
1051 if ${_rc_conf_loaded:-false}; then
1054 if [ -r /etc/defaults/rc.conf ]; then
1055 debug "Sourcing /etc/defaults/rc.conf"
1056 . /etc/defaults/rc.conf
1058 elif [ -r /etc/rc.conf ]; then
1059 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1062 _rc_conf_loaded=true
1064 if [ -f /etc/rc.conf.d/"$_name" ]; then
1065 debug "Sourcing /etc/rc.conf.d/${_name}"
1066 . /etc/rc.conf.d/"$_name"
1069 # Old variable names support
1071 [ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1073 # Set defaults if defined.
1074 for _var in $rcvar $rcvars; do
1075 eval _defval=\$${_var}_defval
1076 if [ -n "$_defval" ]; then
1077 eval : \${$_var:=\$${_var}_defval}
1081 # check obsolete rc.conf variables
1082 for _var in $rcvars_obsolete; do
1084 eval _msg=\$${_var}_obsolete_msg
1085 eval _new=\$${_var}_newvar
1090 if [ -z "$_new" ]; then
1093 eval $_new=\"\$$_var\"
1094 if [ -z "$_msg" ]; then
1095 _msg="Use \$$_new instead."
1098 warn "\$$_var is obsolete. $_msg"
1105 # load_rc_config_var name var
1106 # Read the rc.conf(5) var for name and set in the
1107 # current shell, using load_rc_config in a subshell to prevent
1108 # unwanted side effects from other variable assignments.
1110 load_rc_config_var()
1112 if [ $# -ne 2 ]; then
1113 err 3 'USAGE: load_rc_config_var name var'
1116 load_rc_config '$1' >/dev/null;
1117 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1118 echo '$2'=\'\''${'$2'}\'\'';
1125 # Print a usage string for $0, with `commands' being a list of
1130 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
1134 echo -n 1>&2 "$_sep$_elem"
1142 # err exitval message
1143 # Display message to stderr and log to the syslog, and exit with exitval.
1150 if [ -x /usr/bin/logger ]; then
1151 logger "$0: ERROR: $*"
1153 echo 1>&2 "$0: ERROR: $*"
1159 # Display message to stderr and log to the syslog.
1163 if [ -x /usr/bin/logger ]; then
1164 logger "$0: WARNING: $*"
1166 echo 1>&2 "$0: WARNING: $*"
1171 # Display informational message to stdout and log to syslog.
1176 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1177 if [ -x /usr/bin/logger ]; then
1178 logger "$0: INFO: $*"
1187 # If debugging is enabled in rc.conf output message to stderr.
1188 # BEWARE that you don't call any subroutine that itself calls this
1194 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1195 if [ -x /usr/bin/logger ]; then
1196 logger "$0: DEBUG: $*"
1198 echo 1>&2 "$0: DEBUG: $*"
1204 # backup_file action file cur backup
1205 # Make a backup copy of `file' into `cur', and save the previous
1206 # version of `cur' as `backup' or use rcs for archiving.
1208 # This routine checks the value of the backup_uses_rcs variable,
1209 # which can be either YES or NO.
1211 # The `action' keyword can be one of the following:
1213 # add `file' is now being backed up (and is possibly
1214 # being reentered into the backups system). `cur'
1215 # is created and RCS files, if necessary, are
1218 # update `file' has changed and needs to be backed up.
1219 # If `cur' exists, it is copied to to `back' or
1220 # checked into RCS (if the repository file is old),
1221 # and then `file' is copied to `cur'. Another RCS
1222 # check in done here if RCS is being used.
1224 # remove `file' is no longer being tracked by the backups
1225 # system. If RCS is not being used, `cur' is moved
1226 # to `back', otherwise an empty file is checked in,
1227 # and then `cur' is removed.
1237 if checkyesno backup_uses_rcs; then
1238 _msg0="backup archive"
1241 # ensure that history file is not locked
1242 if [ -f $_cur,v ]; then
1243 rcs -q -u -U -M $_cur
1246 # ensure after switching to rcs that the
1247 # current backup is not lost
1248 if [ -f $_cur ]; then
1249 # no archive, or current newer than archive
1250 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1251 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1260 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1263 chown root:wheel $_cur $_cur,v
1267 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1269 chown root:wheel $_cur $_cur,v
1276 if [ -f $_cur ]; then
1280 chown root:wheel $_cur
1289 # make_symlink src link
1290 # Make a symbolic link 'link' to src from basedir. If the
1291 # directory in which link is to be created does not exist
1292 # a warning will be displayed and an error will be returned.
1293 # Returns 0 on sucess, 1 otherwise.
1297 local src link linkdir _me
1300 linkdir="`dirname $link`"
1301 _me="make_symlink()"
1303 if [ -z "$src" -o -z "$link" ]; then
1304 warn "$_me: requires two arguments."
1307 if [ ! -d "$linkdir" ]; then
1308 warn "$_me: the directory $linkdir does not exist."
1311 if ! ln -sf $src $link; then
1312 warn "$_me: unable to make a symbolic link from $link to $src"
1318 # devfs_rulesets_from_file file
1319 # Reads a set of devfs commands from file, and creates
1320 # the specified rulesets with their rules. Returns non-zero
1321 # if there was an error.
1323 devfs_rulesets_from_file()
1325 local file _err _me _opts
1327 _me="devfs_rulesets_from_file"
1330 if [ -z "$file" ]; then
1331 warn "$_me: you must specify a file"
1334 if [ ! -e "$file" ]; then
1335 debug "$_me: no such file ($file)"
1339 # Disable globbing so that the rule patterns are not expanded
1340 # by accident with matching filesystem entries.
1343 debug "reading rulesets from file ($file)"
1351 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1352 if [ -z "$rulenum" ]; then
1353 warn "$_me: cannot extract rule number ($line)"
1357 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1358 if [ -z "$rulename" ]; then
1359 warn "$_me: cannot extract rule name ($line)"
1363 eval $rulename=\$rulenum
1364 debug "found ruleset: $rulename=$rulenum"
1365 if ! /sbin/devfs rule -s $rulenum delset; then
1371 rulecmd="${line%%"\#*"}"
1372 # evaluate the command incase it includes
1374 if [ -n "$rulecmd" ]; then
1375 debug "adding rule ($rulecmd)"
1376 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1384 if [ $_err -ne 0 ]; then
1385 debug "error in $_me"
1389 case $_opts in *f*) ;; *) set +f ;; esac
1393 # devfs_init_rulesets
1394 # Initializes rulesets from configuration files. Returns
1395 # non-zero if there was an error.
1397 devfs_init_rulesets()
1400 _me="devfs_init_rulesets"
1402 # Go through this only once
1403 if [ -n "$devfs_rulesets_init" ]; then
1404 debug "$_me: devfs rulesets already initialized"
1407 for file in $devfs_rulesets; do
1408 if ! devfs_rulesets_from_file $file; then
1409 warn "$_me: could not read rules from $file"
1413 devfs_rulesets_init=1
1414 debug "$_me: devfs rulesets initialized"
1418 # devfs_set_ruleset ruleset [dir]
1419 # Sets the default ruleset of dir to ruleset. The ruleset argument
1420 # must be a ruleset name as specified in devfs.rules(5) file.
1421 # Returns non-zero if it could not set it successfully.
1426 [ -n "$1" ] && eval rs=\$$1 || rs=
1427 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1428 _me="devfs_set_ruleset"
1430 if [ -z "$rs" ]; then
1431 warn "$_me: you must specify a ruleset number"
1434 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1435 if ! /sbin/devfs $devdir ruleset $rs; then
1436 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1442 # devfs_apply_ruleset ruleset [dir]
1443 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1444 # The ruleset argument must be a ruleset name as specified
1445 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1446 # if it could not apply the ruleset.
1448 devfs_apply_ruleset()
1451 [ -n "$1" ] && eval rs=\$$1 || rs=
1452 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1453 _me="devfs_apply_ruleset"
1455 if [ -z "$rs" ]; then
1456 warn "$_me: you must specify a ruleset"
1459 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1460 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1461 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1467 # devfs_domount dir [ruleset]
1468 # Mount devfs on dir. If ruleset is specified it is set
1469 # on the mount-point. It must also be a ruleset name as specified
1470 # in a devfs.rules(5) file. Returns 0 on success.
1476 [ -n "$2" ] && rs=$2 || rs=
1477 _me="devfs_domount()"
1479 if [ -z "$devdir" ]; then
1480 warn "$_me: you must specify a mount-point"
1483 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1484 if ! mount -t devfs dev "$devdir"; then
1485 warn "$_me: Unable to mount devfs on $devdir"
1488 if [ -n "$rs" ]; then
1490 devfs_set_ruleset $rs $devdir
1491 devfs -m $devdir rule applyset
1496 # devfs_mount_jail dir [ruleset]
1497 # Mounts a devfs file system appropriate for jails
1498 # on the directory dir. If ruleset is specified, the ruleset
1499 # it names will be used instead. If present, ruleset must
1500 # be the name of a ruleset as defined in a devfs.rules(5) file.
1501 # This function returns non-zero if an error occurs.
1507 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1508 _me="devfs_mount_jail"
1511 if ! devfs_domount "$jdev" $rs; then
1512 warn "$_me: devfs was not mounted on $jdev"
1518 # Provide a function for normalizing the mounting of memory
1519 # filesystems. This should allow the rest of the code here to remain
1520 # as close as possible between 5-current and 4-stable.
1523 # $3 = (optional) extra mdmfs flags
1526 if [ -n "$3" ]; then
1529 /sbin/mdmfs $flags -s $1 md $2
1532 # Code common to scripts that need to load a kernel module
1533 # if it isn't in the kernel yet. Syntax:
1534 # load_kld [-e regex] [-m module] file
1535 # where -e or -m chooses the way to check if the module
1536 # is already loaded:
1537 # regex is egrep'd in the output from `kldstat -v',
1538 # module is passed to `kldstat -m'.
1539 # The default way is as though `-m file' were specified.
1542 local _loaded _mod _opt _re
1544 while getopts "e:m:" _opt; do
1547 m) _mod="$OPTARG" ;;
1548 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1551 shift $(($OPTIND - 1))
1552 if [ $# -ne 1 ]; then
1553 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1557 if [ -n "$_re" ]; then
1558 if kldstat -v | egrep -q -e "$_re"; then
1562 if kldstat -q -m "$_mod"; then
1567 if ! kldload "$1"; then
1568 warn "Unable to load kernel module $1"
1571 info "$1 kernel module loaded."
1574 debug "load_kld: $1 kernel module already loaded."
1580 # Change every $src in $str to $dst.
1581 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1585 local _str _src _dst _out _com
1592 for _com in ${_str}; do
1593 if [ -z "${_out}" ]; then
1596 _out="${_out}${_dst}${_com}"
1602 # Creates a list of providers for GELI encryption.
1605 local devices devices2
1606 local provider mountpoint type options rest
1608 # Create list of GELI providers from fstab.
1609 while read provider mountpoint type options rest ; do
1610 case ":${options}" in
1619 case ":${provider}" in
1624 # Skip swap devices.
1625 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1628 devices="${devices} ${provider}"
1633 # Append providers from geli_devices.
1634 devices="${devices} ${geli_devices}"
1636 for provider in ${devices}; do
1637 provider=${provider%.eli}
1638 provider=${provider#/dev/}
1639 devices2="${devices2} ${provider}"
1645 # Find scripts in local_startup directories that use the old syntax
1647 find_local_scripts_old () {
1650 for dir in ${local_startup}; do
1651 if [ -d "${dir}" ]; then
1652 for file in ${dir}/[0-9]*.sh; do
1653 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1655 zlist="$zlist $file"
1657 for file in ${dir}/[^0-9]*.sh; do
1658 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1660 slist="$slist $file"
1666 find_local_scripts_new () {
1668 for dir in ${local_startup}; do
1669 if [ -d "${dir}" ]; then
1670 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1673 *) if [ -x "$file" ]; then
1674 local_rc="${local_rc} ${file}"
1683 # check_required_{before|after} command
1684 # Check for things required by the command before and after its precmd,
1685 # respectively. The two separate functions are needed because some
1686 # conditions should prevent precmd from being run while other things
1687 # depend on precmd having already been run.
1689 check_required_before()
1695 for _f in $required_vars; do
1696 if ! checkyesno $_f; then
1697 warn "\$${_f} is not enabled."
1698 if [ -z "$rc_force" ]; then
1704 for _f in $required_dirs; do
1705 if [ ! -d "${_f}/." ]; then
1706 warn "${_f} is not a directory."
1707 if [ -z "$rc_force" ]; then
1713 for _f in $required_files; do
1714 if [ ! -r "${_f}" ]; then
1715 warn "${_f} is not readable."
1716 if [ -z "$rc_force" ]; then
1727 check_required_after()
1733 for _f in $required_modules; do
1735 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
1736 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
1739 if ! load_kld ${_args}; then
1740 if [ -z "$rc_force" ]; then