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 : ${rcvar_manpage:='rc.conf(5)'}
36 : ${RC_PID:=$$}; export RC_PID
39 # Operating System dependent/independent variables
42 if [ -z "${_rc_subr_loaded}" ]; then
47 SYSCTL_N="${SYSCTL} -n"
50 IDCMD="if [ -x $ID ]; then $ID -un; fi"
52 JID=`$PS -p $$ -o jid=`
58 # set_rcvar [var] [defval] [desc]
60 # Echo or define a rc.conf(5) variable name. Global variable
63 # If no argument is specified, echo "${name}_enable".
65 # If only a var is specified, echo "${var}_enable".
67 # If var and defval are specified, the ${var} is defined as
68 # rc.conf(5) variable and the default value is ${defvar}. An
69 # optional argument $desc can also be specified to add a
70 # description for that.
82 debug "rcvar_define: \$$1=$2 is added" \
83 " as a rc.conf(5) variable."
87 rcvars="${rcvars# } $_var"
88 eval ${_var}_defval=\"$2\"
90 # encode multiple lines of _desc
92 eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
94 eval ${_var}_desc=\"\${${_var}_desc#^^}\"
99 # set_rcvar_obsolete oldvar [newvar] [msg]
100 # Define obsolete variable.
101 # Global variable $rcvars_obsolete is used.
107 debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
109 rcvars_obsolete="${rcvars_obsolete# } $1"
110 eval ${1}_newvar=\"$2\"
112 eval ${_var}_obsolete_msg=\"$*\"
116 # force_depend script
117 # Force a service to start. Intended for use by services
118 # to resolve dependency issues. It is assumed the caller
119 # has check to make sure this call is necessary
120 # $1 - filename of script, in /etc/rc.d, to run
126 info "${name} depends on ${_depend}, which will be forced to start."
127 if ! /etc/rc.d/${_depend} forcestart; then
128 warn "Unable to force ${_depend}. It may already be running."
136 # Test $1 variable, and warn if not set to YES or NO.
137 # Return 0 if it's "yes" (et al), nonzero otherwise.
142 debug "checkyesno: $1 is set to $_value."
145 # "yes", "true", "on", or "1"
146 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
150 # "no", "false", "off", or "0"
151 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
155 warn "\$${1} is not set properly - see ${rcvar_manpage}."
163 # print the list in reverse order
169 _revlist="$_revfile $_revlist"
175 # If booting directly to multiuser or $always is enabled,
176 # send SIGTERM to the parent (/etc/rc) to abort the boot.
177 # Otherwise just exit.
184 # "yes", "true", "on", or "1"
185 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
192 if [ "$autoboot" = yes -o "$always" = true ]; then
193 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
200 # mount_critical_filesystems type
201 # Go through the list of critical filesystems as provided in
202 # the rc.conf(5) variable $critical_filesystems_${type}, checking
203 # each one to see if it is mounted, and if it is not, mounting it.
205 mount_critical_filesystems()
207 eval _fslist=\$critical_filesystems_${1}
208 for _fs in $_fslist; do
211 while read what _on on _type type; do
212 if [ $on = $_fs ]; then
219 mount $_fs >/dev/null 2>&1
226 # check_pidfile pidfile procname [interpreter]
227 # Parses the first line of pidfile for a PID, and ensures
228 # that the process is running and matches procname.
229 # Prints the matching PID upon success, nothing otherwise.
230 # interpreter is optional; see _find_processes() for details.
237 if [ -z "$_pidfile" -o -z "$_procname" ]; then
238 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
240 if [ ! -f $_pidfile ]; then
241 debug "pid file ($_pidfile): not readable."
244 read _pid _junk < $_pidfile
245 if [ -z "$_pid" ]; then
246 debug "pid file ($_pidfile): no pid in file."
249 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
253 # check_process procname [interpreter]
254 # Ensures that a process (or processes) named procname is running.
255 # Prints a list of matching PIDs.
256 # interpreter is optional; see _find_processes() for details.
262 if [ -z "$_procname" ]; then
263 err 3 'USAGE: check_process procname [interpreter]'
265 _find_processes $_procname ${_interpreter:-.} '-ax'
269 # _find_processes procname interpreter psargs
270 # Search for procname in the output of ps generated by psargs.
271 # Prints the PIDs of any matching processes, space separated.
273 # If interpreter == ".", check the following variations of procname
274 # against the first word of each command:
276 # `basename procname`
277 # `basename procname` + ":"
278 # "(" + `basename procname` + ")"
279 # "[" + `basename procname` + "]"
281 # If interpreter != ".", read the first line of procname, remove the
282 # leading #!, normalise whitespace, append procname, and attempt to
283 # match that against each command, either as is, or with extra words
284 # at the end. As an alternative, to deal with interpreted daemons
285 # using perl, the basename of the interpreter plus a colon is also
286 # tried as the prefix to procname.
290 if [ $# -ne 3 ]; then
291 err 3 'USAGE: _find_processes procname interpreter psargs'
298 if [ $_interpreter != "." ]; then # an interpreted script
299 _script=${_chroot}${_chroot:+"/"}$_procname
300 if [ -r $_script ]; then
301 read _interp < $_script # read interpreter name
304 _interp=${_interp#\#!} # strip #!
308 shift # drop env to get real name
311 if [ $_interpreter != $1 ]; then
312 warn "\$command_interpreter $_interpreter != $1"
316 warn "no shebang line in $_script"
321 warn "cannot read shebang line from $_script"
324 _interp="$* $_procname" # cleanup spaces, add _procname
327 _fp_match='case "$_argv" in
328 ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
329 else # a normal daemon
330 _procnamebn=${_procname##*/}
331 _fp_args='_arg0 _argv'
332 _fp_match='case "$_arg0" in
333 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
337 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
338 while read _npid _jid '"$_fp_args"'; do
340 if [ "$JID" -eq "$_jid" ];
341 then echo -n "$_pref$_npid";
348 # debug "in _find_processes: proccheck is ($_proccheck)."
353 # wait_for_pids pid [pid ...]
354 # spins until none of the pids exist
358 local _list _prefix _nlist _j
361 if [ -z "$_list" ]; then
368 if kill -0 $_j 2>/dev/null; then
369 _nlist="${_nlist}${_nlist:+ }$_j"
370 [ -n "$_prefix" ] && sleep 1
373 if [ -z "$_nlist" ]; then
377 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
379 pwait $_list 2>/dev/null
381 if [ -n "$_prefix" ]; then
387 # get_pidfile_from_conf string file
389 # Takes a string to search for in the specified file.
390 # Ignores lines with traditional comment characters.
394 # if get_pidfile_from_conf string file; then
395 # pidfile="$_pidfile_from_conf"
397 # pidfile='appropriate default'
400 get_pidfile_from_conf()
402 if [ -z "$1" -o -z "$2" ]; then
403 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
406 local string file line
408 string="$1" ; file="$2"
410 if [ ! -s "$file" ]; then
411 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
416 *[#\;]*${string}*) continue ;;
417 *${string}*) break ;;
421 if [ -n "$line" ]; then
423 _pidfile_from_conf="/${line%%[\"\;]*}"
431 # If rc_quiet is set (usually as a result of using faststart at
432 # boot time) check if rc_startmsgs is enabled.
436 if [ -n "$rc_quiet" ]; then
437 checkyesno rc_startmsgs
444 # run_rc_command argument
445 # Search for argument in the list of supported commands, which is:
446 # "start stop restart rcvar status poll ${extra_commands}"
447 # If there's a match, run ${argument}_cmd or the default method
450 # If argument has a given prefix, then change the operation as follows:
453 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
454 # force Set ${rcvar} to YES, and set rc_force=yes
455 # one Set ${rcvar} to YES
456 # quiet Don't output some diagnostics, and set rc_quiet=yes
458 # The following globals are used:
460 # Name Needed Purpose
461 # ---- ------ -------
462 # name y Name of script.
464 # command n Full path to command.
465 # Not needed if ${rc_arg}_cmd is set for
468 # command_args n Optional args/shell directives for command.
470 # command_interpreter n If not empty, command is interpreted, so
471 # call check_{pidfile,process}() appropriately.
473 # desc n Description of script.
475 # extra_commands n List of extra commands supported.
477 # pidfile n If set, use check_pidfile $pidfile $command,
478 # otherwise use check_process $command.
479 # In either case, only check if $command is set.
481 # procname n Process name to check for instead of $command.
483 # rcvar n This is checked with checkyesno to determine
484 # if the action should be run.
486 # ${name}_program n Full path to command.
487 # Meant to be used in /etc/rc.conf to override
490 # ${name}_chroot n Directory to chroot to before running ${command}
491 # Requires /usr to be mounted.
493 # ${name}_chdir n Directory to cd to before running ${command}
494 # (if not using ${name}_chroot).
496 # ${name}_flags n Arguments to call ${command} with.
497 # NOTE: $flags from the parent environment
498 # can be used to override this.
500 # ${name}_nice n Nice level to run ${command} at.
502 # ${name}_user n User to run ${command} as, using su(1) if not
503 # using ${name}_chroot.
504 # Requires /usr to be mounted.
506 # ${name}_group n Group to run chrooted ${command} as.
507 # Requires /usr to be mounted.
509 # ${name}_groups n Comma separated list of supplementary groups
510 # to run the chrooted ${command} with.
511 # Requires /usr to be mounted.
513 # ${rc_arg}_cmd n If set, use this as the method when invoked;
514 # Otherwise, use default command (see below)
516 # ${rc_arg}_precmd n If set, run just before performing the
517 # ${rc_arg}_cmd method in the default
518 # operation (i.e, after checking for required
519 # bits and process (non)existence).
520 # If this completes with a non-zero exit code,
521 # don't run ${rc_arg}_cmd.
523 # ${rc_arg}_postcmd n If set, run just after performing the
524 # ${rc_arg}_cmd method, if that method
525 # returned a zero exit code.
527 # required_dirs n If set, check for the existence of the given
528 # directories before running a (re)start command.
530 # required_files n If set, check for the readability of the given
531 # files before running a (re)start command.
533 # required_modules n If set, ensure the given kernel modules are
534 # loaded before running a (re)start command.
535 # The check and possible loads are actually
536 # done after start_precmd so that the modules
537 # aren't loaded in vain, should the precmd
538 # return a non-zero status to indicate a error.
539 # If a word in the list looks like "foo:bar",
540 # "foo" is the KLD file name and "bar" is the
541 # module name. If a word looks like "foo~bar",
542 # "foo" is the KLD file name and "bar" is a
543 # egrep(1) pattern matching the module name.
544 # Otherwise the module name is assumed to be
545 # the same as the KLD file name, which is most
546 # common. See load_kld().
548 # required_vars n If set, perform checkyesno on each of the
549 # listed variables before running the default
552 # Default behaviour for a given argument, if no override method is
555 # Argument Default behaviour
556 # -------- -----------------
557 # start if !running && checkyesno ${rcvar}
561 # rc_pid=$(check_pidfile $pidfile $command)
563 # rc_pid=$(check_process $command)
564 # kill $sig_stop $rc_pid
565 # wait_for_pids $rc_pid
566 # ($sig_stop defaults to TERM.)
568 # reload Similar to stop, except use $sig_reload instead,
569 # and doesn't wait_for_pids.
570 # $sig_reload defaults to HUP.
571 # Note that `reload' isn't provided by default,
572 # it should be enabled via $extra_commands.
574 # restart Run `stop' then `start'.
576 # status Show if ${command} is running, etc.
578 # poll Wait for ${command} to exit.
580 # rcvar Display what rc.conf variable is used (if any).
582 # Variables available to methods, and after run_rc_command() has
587 # rc_arg Argument to command, after fast/force/one processing
590 # rc_flags Flags to start the default command with.
591 # Defaults to ${name}_flags, unless overridden
592 # by $flags from the environment.
593 # This variable may be changed by the precmd method.
595 # rc_pid PID of command (if appropriate)
597 # rc_fast Not empty if "fast" was provided (q.v.)
599 # rc_force Not empty if "force" was provided (q.v.)
601 # rc_quiet Not empty if "quiet" was provided
608 if [ -z "$name" ]; then
609 err 3 'run_rc_command: $name is not set.'
612 # Don't repeat the first argument when passing additional command-
613 # line arguments to the command subroutines.
620 fast*) # "fast" prefix; don't check pid
621 rc_arg=${rc_arg#fast}
625 force*) # "force" prefix; always run
628 rc_arg=${rc_arg#${_rc_prefix}}
629 if [ -n "${rcvar}" ]; then
633 one*) # "one" prefix; set ${rcvar}=yes
635 rc_arg=${rc_arg#${_rc_prefix}}
636 if [ -n "${rcvar}" ]; then
640 quiet*) # "quiet" prefix; omit some messages
642 rc_arg=${rc_arg#${_rc_prefix}}
647 eval _override_command=\$${name}_program
648 command=${_override_command:-$command}
650 _keywords="start stop restart rcvar $extra_commands"
653 _procname=${procname:-${command}}
655 # setup pid check command
656 if [ -n "$_procname" ]; then
657 if [ -n "$pidfile" ]; then
658 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
660 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
662 if [ -n "$_pidcmd" ]; then
663 _keywords="${_keywords} status poll"
667 if [ -z "$rc_arg" ]; then
671 if [ -n "$flags" ]; then # allow override from environment
674 eval rc_flags=\$${name}_flags
676 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
677 _nice=\$${name}_nice _user=\$${name}_user \
678 _group=\$${name}_group _groups=\$${name}_groups
680 if [ -n "$_user" ]; then # unset $_user if running as that user
681 if [ "$_user" = "$(eval $IDCMD)" ]; then
686 eval $_pidcmd # determine the pid if necessary
688 for _elem in $_keywords; do
689 if [ "$_elem" != "$rc_arg" ]; then
692 # if ${rcvar} is set, $1 is not "rcvar"
693 # and ${rc_pid} is not set, then run
694 # checkyesno ${rcvar}
695 # and return if that failed
697 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] ||
698 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then
699 if ! checkyesno ${rcvar}; then
700 if [ -n "${rc_quiet}" ]; then
703 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
704 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
705 echo "instead of '${rc_arg}'."
710 # if there's a custom ${XXX_cmd},
711 # run that instead of the default
713 eval _cmd=\$${rc_arg}_cmd \
714 _precmd=\$${rc_arg}_precmd \
715 _postcmd=\$${rc_arg}_postcmd
717 if [ -n "$_cmd" ]; then
718 _run_rc_precmd || return 1
719 _run_rc_doit "$_cmd $rc_extra_args" || return 1
724 case "$rc_arg" in # default operations...
727 _run_rc_precmd || return 1
728 if [ -n "$rc_pid" ]; then
729 echo "${name} is running as pid $rc_pid."
731 echo "${name} is not running."
738 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
739 echo 1>&2 "${name} already running? (pid=$rc_pid)."
743 if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
744 warn "run_rc_command: cannot run $command"
748 if ! _run_rc_precmd; then
749 warn "failed precmd routine for ${name}"
753 # setup the full command to run
755 check_startmsgs && echo "Starting ${name}."
756 if [ -n "$_chroot" ]; then
758 ${_nice:+nice -n $_nice }\
759 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
760 $_chroot $command $rc_flags $command_args"
763 ${_chdir:+cd $_chdir && }\
764 $command $rc_flags $command_args"
765 if [ -n "$_user" ]; then
766 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
768 if [ -n "$_nice" ]; then
769 if [ -z "$_user" ]; then
770 _doit="sh -c \"$_doit\""
772 _doit="nice -n $_nice $_doit"
776 # run the full command
778 if ! _run_rc_doit "$_doit"; then
779 warn "failed to start ${name}"
783 # finally, run postcmd
789 if [ -z "$rc_pid" ]; then
790 [ -n "$rc_fast" ] && return 0
795 _run_rc_precmd || return 1
797 # send the signal to stop
799 echo "Stopping ${name}."
800 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
801 _run_rc_doit "$_doit" || return 1
803 # wait for the command to exit,
805 wait_for_pids $rc_pid
811 if [ -z "$rc_pid" ]; then
816 _run_rc_precmd || return 1
818 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
819 _run_rc_doit "$_doit" || return 1
825 # prevent restart being called more
826 # than once by any given script
828 if ${_rc_restart_done:-false}; then
831 _rc_restart_done=true
833 _run_rc_precmd || return 1
835 # run those in a subshell to keep global variables
836 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
837 ( run_rc_command ${_rc_prefix}start $rc_extra_args )
839 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
845 _run_rc_precmd || return 1
846 if [ -n "$rc_pid" ]; then
847 wait_for_pids $rc_pid
854 if [ -n "$desc" ]; then
860 # Get unique vars in $rcvar $rcvars
861 for _v in $rcvar $rcvars; do
863 $_v\ *|\ *$_v|*\ $_v\ *) ;;
870 if [ -z "$_v" ]; then
874 eval _desc=\$${_v}_desc
875 eval _defval=\$${_v}_defval
878 eval echo \"$_v=\\\"\$$_v\\\"\"
879 # decode multiple lines of _desc
880 while [ -n "$_desc" ]; do
883 echo "# $_h ${_desc%%^^*}"
888 echo "# $_h ${_desc}"
893 echo "# (default: \"$_defval\")"
906 echo 1>&2 "$0: unknown directive '$rc_arg'."
912 # Helper functions for run_rc_command: common code.
913 # They use such global variables besides the exported rc_* ones:
923 check_required_before "$rc_arg" || return 1
925 if [ -n "$_precmd" ]; then
926 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
927 eval "$_precmd $rc_extra_args"
930 # If precmd failed and force isn't set, request exit.
931 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
936 check_required_after "$rc_arg" || return 1
943 if [ -n "$_postcmd" ]; then
944 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
945 eval "$_postcmd $rc_extra_args"
953 debug "run_rc_command: doit: $*"
957 # If command failed and force isn't set, request exit.
958 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
969 if [ -n "$pidfile" ]; then
970 _pidmsg=" (check $pidfile)."
974 echo 1>&2 "${name} not running?${_pidmsg}"
981 _cmd="kill -$1 $rc_pid"
982 if [ -n "$_user" ]; then
983 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
989 # run_rc_script file arg
990 # Start the script `file' with `arg', and correctly handle the
991 # return value from the script.
992 # If `file' ends with `.sh', it's sourced into the current environment
993 # when $rc_fast_and_loose is set, otherwise it is run as a child process.
994 # If `file' appears to be a backup or scratch file, ignore it.
995 # Otherwise if it is executable run as a child process.
1001 if [ -z "$_file" -o -z "$_arg" ]; then
1002 err 3 'USAGE: run_rc_script file arg'
1005 unset name command command_args command_interpreter \
1006 extra_commands pidfile procname \
1007 rcvar rcvars rcvars_obsolete required_dirs required_files \
1009 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1012 /etc/rc.d/*.sh) # no longer allowed in the base
1013 warn "Ignoring old-style startup script $_file"
1015 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
1016 warn "Ignoring scratch file $_file"
1018 *) # run in subshell
1019 if [ -x $_file ]; then
1020 if [ -n "$rc_fast_and_loose" ]; then
1023 ( trap "echo Script $_file interrupted; kill -QUIT $$" 3
1024 trap "echo Script $_file interrupted; exit 1" 2
1025 trap "echo Script $_file running" 29
1026 set $_arg; . $_file )
1034 # load_rc_config name
1035 # Source in the configuration file for a given name.
1039 local _name _var _defval _v _msg _new
1041 if [ -z "$_name" ]; then
1042 err 3 'USAGE: load_rc_config name'
1045 if ${_rc_conf_loaded:-false}; then
1048 if [ -r /etc/defaults/rc.conf ]; then
1049 debug "Sourcing /etc/defaults/rc.conf"
1050 . /etc/defaults/rc.conf
1052 elif [ -r /etc/rc.conf ]; then
1053 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1056 _rc_conf_loaded=true
1058 if [ -f /etc/rc.conf.d/"$_name" ]; then
1059 debug "Sourcing /etc/rc.conf.d/${_name}"
1060 . /etc/rc.conf.d/"$_name"
1063 # Set defaults if defined.
1064 for _var in $rcvar $rcvars; do
1065 eval _defval=\$${_var}_defval
1066 if [ -n "$_defval" ]; then
1067 eval : \${$_var:=\$${_var}_defval}
1071 # check obsolete rc.conf variables
1072 for _var in $rcvars_obsolete; do
1074 eval _msg=\$${_var}_obsolete_msg
1075 eval _new=\$${_var}_newvar
1080 if [ -z "$_new" ]; then
1083 eval $_new=\"\$$_var\"
1084 if [ -z "$_msg" ]; then
1085 _msg="Use \$$_new instead."
1088 warn "\$$_var is obsolete. $_msg"
1095 # load_rc_config_var name var
1096 # Read the rc.conf(5) var for name and set in the
1097 # current shell, using load_rc_config in a subshell to prevent
1098 # unwanted side effects from other variable assignments.
1100 load_rc_config_var()
1102 if [ $# -ne 2 ]; then
1103 err 3 'USAGE: load_rc_config_var name var'
1106 load_rc_config '$1' >/dev/null;
1107 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1108 echo '$2'=\'\''${'$2'}\'\'';
1115 # Print a usage string for $0, with `commands' being a list of
1120 echo -n 1>&2 "Usage: $0 [fast|force|one]("
1124 echo -n 1>&2 "$_sep$_elem"
1132 # err exitval message
1133 # Display message to stderr and log to the syslog, and exit with exitval.
1140 if [ -x /usr/bin/logger ]; then
1141 logger "$0: ERROR: $*"
1143 echo 1>&2 "$0: ERROR: $*"
1149 # Display message to stderr and log to the syslog.
1153 if [ -x /usr/bin/logger ]; then
1154 logger "$0: WARNING: $*"
1156 echo 1>&2 "$0: WARNING: $*"
1161 # Display informational message to stdout and log to syslog.
1166 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1167 if [ -x /usr/bin/logger ]; then
1168 logger "$0: INFO: $*"
1177 # If debugging is enabled in rc.conf output message to stderr.
1178 # BEWARE that you don't call any subroutine that itself calls this
1184 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1185 if [ -x /usr/bin/logger ]; then
1186 logger "$0: DEBUG: $*"
1188 echo 1>&2 "$0: DEBUG: $*"
1194 # backup_file action file cur backup
1195 # Make a backup copy of `file' into `cur', and save the previous
1196 # version of `cur' as `backup' or use rcs for archiving.
1198 # This routine checks the value of the backup_uses_rcs variable,
1199 # which can be either YES or NO.
1201 # The `action' keyword can be one of the following:
1203 # add `file' is now being backed up (and is possibly
1204 # being reentered into the backups system). `cur'
1205 # is created and RCS files, if necessary, are
1208 # update `file' has changed and needs to be backed up.
1209 # If `cur' exists, it is copied to to `back' or
1210 # checked into RCS (if the repository file is old),
1211 # and then `file' is copied to `cur'. Another RCS
1212 # check in done here if RCS is being used.
1214 # remove `file' is no longer being tracked by the backups
1215 # system. If RCS is not being used, `cur' is moved
1216 # to `back', otherwise an empty file is checked in,
1217 # and then `cur' is removed.
1227 if checkyesno backup_uses_rcs; then
1228 _msg0="backup archive"
1231 # ensure that history file is not locked
1232 if [ -f $_cur,v ]; then
1233 rcs -q -u -U -M $_cur
1236 # ensure after switching to rcs that the
1237 # current backup is not lost
1238 if [ -f $_cur ]; then
1239 # no archive, or current newer than archive
1240 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1241 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1250 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1253 chown root:wheel $_cur $_cur,v
1257 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1259 chown root:wheel $_cur $_cur,v
1266 if [ -f $_cur ]; then
1270 chown root:wheel $_cur
1279 # make_symlink src link
1280 # Make a symbolic link 'link' to src from basedir. If the
1281 # directory in which link is to be created does not exist
1282 # a warning will be displayed and an error will be returned.
1283 # Returns 0 on sucess, 1 otherwise.
1287 local src link linkdir _me
1290 linkdir="`dirname $link`"
1291 _me="make_symlink()"
1293 if [ -z "$src" -o -z "$link" ]; then
1294 warn "$_me: requires two arguments."
1297 if [ ! -d "$linkdir" ]; then
1298 warn "$_me: the directory $linkdir does not exist."
1301 if ! ln -sf $src $link; then
1302 warn "$_me: unable to make a symbolic link from $link to $src"
1308 # devfs_rulesets_from_file file
1309 # Reads a set of devfs commands from file, and creates
1310 # the specified rulesets with their rules. Returns non-zero
1311 # if there was an error.
1313 devfs_rulesets_from_file()
1317 _me="devfs_rulesets_from_file"
1320 if [ -z "$file" ]; then
1321 warn "$_me: you must specify a file"
1324 if [ ! -e "$file" ]; then
1325 debug "$_me: no such file ($file)"
1328 debug "reading rulesets from file ($file)"
1336 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1337 if [ -z "$rulenum" ]; then
1338 warn "$_me: cannot extract rule number ($line)"
1342 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1343 if [ -z "$rulename" ]; then
1344 warn "$_me: cannot extract rule name ($line)"
1348 eval $rulename=\$rulenum
1349 debug "found ruleset: $rulename=$rulenum"
1350 if ! /sbin/devfs rule -s $rulenum delset; then
1356 rulecmd="${line%%"\#*"}"
1357 # evaluate the command incase it includes
1359 if [ -n "$rulecmd" ]; then
1360 debug "adding rule ($rulecmd)"
1361 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1369 if [ $_err -ne 0 ]; then
1370 debug "error in $_me"
1377 # devfs_init_rulesets
1378 # Initializes rulesets from configuration files. Returns
1379 # non-zero if there was an error.
1381 devfs_init_rulesets()
1384 _me="devfs_init_rulesets"
1386 # Go through this only once
1387 if [ -n "$devfs_rulesets_init" ]; then
1388 debug "$_me: devfs rulesets already initialized"
1391 for file in $devfs_rulesets; do
1392 if ! devfs_rulesets_from_file $file; then
1393 warn "$_me: could not read rules from $file"
1397 devfs_rulesets_init=1
1398 debug "$_me: devfs rulesets initialized"
1402 # devfs_set_ruleset ruleset [dir]
1403 # Sets the default ruleset of dir to ruleset. The ruleset argument
1404 # must be a ruleset name as specified in devfs.rules(5) file.
1405 # Returns non-zero if it could not set it successfully.
1410 [ -n "$1" ] && eval rs=\$$1 || rs=
1411 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1412 _me="devfs_set_ruleset"
1414 if [ -z "$rs" ]; then
1415 warn "$_me: you must specify a ruleset number"
1418 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1419 if ! /sbin/devfs $devdir ruleset $rs; then
1420 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1426 # devfs_apply_ruleset ruleset [dir]
1427 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1428 # The ruleset argument must be a ruleset name as specified
1429 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1430 # if it could not apply the ruleset.
1432 devfs_apply_ruleset()
1435 [ -n "$1" ] && eval rs=\$$1 || rs=
1436 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1437 _me="devfs_apply_ruleset"
1439 if [ -z "$rs" ]; then
1440 warn "$_me: you must specify a ruleset"
1443 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1444 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1445 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1451 # devfs_domount dir [ruleset]
1452 # Mount devfs on dir. If ruleset is specified it is set
1453 # on the mount-point. It must also be a ruleset name as specified
1454 # in a devfs.rules(5) file. Returns 0 on success.
1460 [ -n "$2" ] && rs=$2 || rs=
1461 _me="devfs_domount()"
1463 if [ -z "$devdir" ]; then
1464 warn "$_me: you must specify a mount-point"
1467 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1468 if ! mount -t devfs dev "$devdir"; then
1469 warn "$_me: Unable to mount devfs on $devdir"
1472 if [ -n "$rs" ]; then
1474 devfs_set_ruleset $rs $devdir
1475 devfs -m $devdir rule applyset
1480 # devfs_mount_jail dir [ruleset]
1481 # Mounts a devfs file system appropriate for jails
1482 # on the directory dir. If ruleset is specified, the ruleset
1483 # it names will be used instead. If present, ruleset must
1484 # be the name of a ruleset as defined in a devfs.rules(5) file.
1485 # This function returns non-zero if an error occurs.
1491 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1492 _me="devfs_mount_jail"
1495 if ! devfs_domount "$jdev" $rs; then
1496 warn "$_me: devfs was not mounted on $jdev"
1502 # Provide a function for normalizing the mounting of memory
1503 # filesystems. This should allow the rest of the code here to remain
1504 # as close as possible between 5-current and 4-stable.
1507 # $3 = (optional) extra mdmfs flags
1510 if [ -n "$3" ]; then
1513 /sbin/mdmfs $flags -s $1 md $2
1516 # Code common to scripts that need to load a kernel module
1517 # if it isn't in the kernel yet. Syntax:
1518 # load_kld [-e regex] [-m module] file
1519 # where -e or -m chooses the way to check if the module
1520 # is already loaded:
1521 # regex is egrep'd in the output from `kldstat -v',
1522 # module is passed to `kldstat -m'.
1523 # The default way is as though `-m file' were specified.
1526 local _loaded _mod _opt _re
1528 while getopts "e:m:" _opt; do
1531 m) _mod="$OPTARG" ;;
1532 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1535 shift $(($OPTIND - 1))
1536 if [ $# -ne 1 ]; then
1537 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1541 if [ -n "$_re" ]; then
1542 if kldstat -v | egrep -q -e "$_re"; then
1546 if kldstat -q -m "$_mod"; then
1551 if ! kldload "$1"; then
1552 warn "Unable to load kernel module $1"
1555 info "$1 kernel module loaded."
1558 debug "load_kld: $1 kernel module already loaded."
1564 # Change every $src in $str to $dst.
1565 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1569 local _str _src _dst _out _com
1576 for _com in ${_str}; do
1577 if [ -z "${_out}" ]; then
1580 _out="${_out}${_dst}${_com}"
1586 # Creates a list of providers for GELI encryption.
1589 local devices devices2
1590 local provider mountpoint type options rest
1592 # Create list of GELI providers from fstab.
1593 while read provider mountpoint type options rest ; do
1594 case ":${options}" in
1603 case ":${provider}" in
1608 # Skip swap devices.
1609 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1612 devices="${devices} ${provider}"
1617 # Append providers from geli_devices.
1618 devices="${devices} ${geli_devices}"
1620 for provider in ${devices}; do
1621 provider=${provider%.eli}
1622 provider=${provider#/dev/}
1623 devices2="${devices2} ${provider}"
1629 # Find scripts in local_startup directories that use the old syntax
1631 find_local_scripts_old () {
1634 for dir in ${local_startup}; do
1635 if [ -d "${dir}" ]; then
1636 for file in ${dir}/[0-9]*.sh; do
1637 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1639 zlist="$zlist $file"
1641 for file in ${dir}/[^0-9]*.sh; do
1642 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1644 slist="$slist $file"
1650 find_local_scripts_new () {
1652 for dir in ${local_startup}; do
1653 if [ -d "${dir}" ]; then
1654 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1657 *) if [ -x "$file" ]; then
1658 local_rc="${local_rc} ${file}"
1667 # check_required_{before|after} command
1668 # Check for things required by the command before and after its precmd,
1669 # respectively. The two separate functions are needed because some
1670 # conditions should prevent precmd from being run while other things
1671 # depend on precmd having already been run.
1673 check_required_before()
1679 for _f in $required_vars; do
1680 if ! checkyesno $_f; then
1681 warn "\$${_f} is not enabled."
1682 if [ -z "$rc_force" ]; then
1688 for _f in $required_dirs; do
1689 if [ ! -d "${_f}/." ]; then
1690 warn "${_f} is not a directory."
1691 if [ -z "$rc_force" ]; then
1697 for _f in $required_files; do
1698 if [ ! -r "${_f}" ]; then
1699 warn "${_f} is not readable."
1700 if [ -z "$rc_force" ]; then
1711 check_required_after()
1717 for _f in $required_modules; do
1719 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
1720 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
1723 if ! load_kld ${_args}; then
1724 if [ -z "$rc_force" ]; then
1735 # check_kern_features mib
1736 # Return existence of kern.features.* sysctl MIB as true or
1737 # false. The result will be cached in $_rc_cache_kern_features_
1738 # namespace. "0" means the kern.features.X exists.
1740 check_kern_features()
1744 [ -n "$1" ] || return 1;
1745 eval _v=\$_rc_cache_kern_features_$1
1746 [ -n "$_v" ] && return "$_v";
1748 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then
1749 eval _rc_cache_kern_features_$1=0
1752 eval _rc_cache_kern_features_$1=1
1757 # _echoonce var msg mode
1758 # mode=0: Echo $msg if ${$var} is empty.
1759 # After doing echo, a string is set to ${$var}.
1761 # mode=1: Echo $msg if ${$var} is a string with non-zero length.
1765 local _var _msg _mode
1771 1) [ -n "$_var" ] && echo "$_msg" ;;
1772 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
1776 fi # [ -z "${_rc_subr_loaded}" ]