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.
18 # 3. All advertising materials mentioning features or use of this software
19 # must display the following acknowledgement:
20 # This product includes software developed by the NetBSD
21 # Foundation, Inc. and its contributors.
22 # 4. Neither the name of The NetBSD Foundation nor the names of its
23 # contributors may be used to endorse or promote products derived
24 # from this software without specific prior written permission.
26 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 # POSSIBILITY OF SUCH DAMAGE.
39 # functions used by various rc scripts
42 : ${rcvar_manpage:='rc.conf(5)'}
43 : ${RC_PID:=$$}; export RC_PID
46 # Operating System dependent/independent variables
49 if [ -z "${_rc_subr_loaded}" ]; then
54 SYSCTL_N="${SYSCTL} -n"
55 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
56 OSTYPE=`${CMD_OSTYPE}`
58 IDCMD="if [ -x $ID ]; then $ID -un; fi"
60 JID=`$PS -p $$ -o jid=`
67 SYSCTL_W="${SYSCTL} -w"
75 # set_rcvar [var] [defval] [desc]
77 # Echo or define a rc.conf(5) variable name. Global variable
80 # If no argument is specified, echo "${name}_enable".
82 # If only a var is specified, echo "${var}_enable".
84 # If var and defval are specified, the ${var} is defined as
85 # rc.conf(5) variable and the default value is ${defvar}. An
86 # optional argument $desc can also be specified to add a
87 # description for that.
99 debug "rcvar_define: \$$1=$2 is added" \
100 " as a rc.conf(5) variable."
104 rcvars="${rcvars# } $_var"
105 eval ${_var}_defval=\"$2\"
107 # encode multiple lines of _desc
109 eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
111 eval ${_var}_desc=\"\${${_var}_desc#^^}\"
116 # set_rcvar_obsolete oldvar [newvar] [msg]
117 # Define obsolete variable.
118 # Global variable $rcvars_obsolete is used.
124 debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
126 rcvars_obsolete="${rcvars_obsolete# } $1"
127 eval ${1}_newvar=\"$2\"
129 eval ${_var}_obsolete_msg=\"$*\"
133 # force_depend script
134 # Force a service to start. Intended for use by services
135 # to resolve dependency issues. It is assumed the caller
136 # has check to make sure this call is necessary
137 # $1 - filename of script, in /etc/rc.d, to run
143 info "${name} depends on ${_depend}, which will be forced to start."
144 if ! /etc/rc.d/${_depend} forcestart; then
145 warn "Unable to force ${_depend}. It may already be running."
153 # Test $1 variable, and warn if not set to YES or NO.
154 # Return 0 if it's "yes" (et al), nonzero otherwise.
159 debug "checkyesno: $1 is set to $_value."
162 # "yes", "true", "on", or "1"
163 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
167 # "no", "false", "off", or "0"
168 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
172 warn "\$${1} is not set properly - see ${rcvar_manpage}."
180 # print the list in reverse order
186 _revlist="$_revfile $_revlist"
192 # If booting directly to multiuser or $always is enabled,
193 # send SIGTERM to the parent (/etc/rc) to abort the boot.
194 # Otherwise just exit.
201 # "yes", "true", "on", or "1"
202 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
209 if [ "$autoboot" = yes -o "$always" = true ]; then
210 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
217 # mount_critical_filesystems type
218 # Go through the list of critical filesystems as provided in
219 # the rc.conf(5) variable $critical_filesystems_${type}, checking
220 # each one to see if it is mounted, and if it is not, mounting it.
222 mount_critical_filesystems()
224 eval _fslist=\$critical_filesystems_${1}
225 for _fs in $_fslist; do
228 while read what _on on _type type; do
229 if [ $on = $_fs ]; then
236 mount $_fs >/dev/null 2>&1
243 # check_pidfile pidfile procname [interpreter]
244 # Parses the first line of pidfile for a PID, and ensures
245 # that the process is running and matches procname.
246 # Prints the matching PID upon success, nothing otherwise.
247 # interpreter is optional; see _find_processes() for details.
254 if [ -z "$_pidfile" -o -z "$_procname" ]; then
255 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
257 if [ ! -f $_pidfile ]; then
258 debug "pid file ($_pidfile): not readable."
261 read _pid _junk < $_pidfile
262 if [ -z "$_pid" ]; then
263 debug "pid file ($_pidfile): no pid in file."
266 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
270 # check_process procname [interpreter]
271 # Ensures that a process (or processes) named procname is running.
272 # Prints a list of matching PIDs.
273 # interpreter is optional; see _find_processes() for details.
279 if [ -z "$_procname" ]; then
280 err 3 'USAGE: check_process procname [interpreter]'
282 _find_processes $_procname ${_interpreter:-.} '-ax'
286 # _find_processes procname interpreter psargs
287 # Search for procname in the output of ps generated by psargs.
288 # Prints the PIDs of any matching processes, space separated.
290 # If interpreter == ".", check the following variations of procname
291 # against the first word of each command:
293 # `basename procname`
294 # `basename procname` + ":"
295 # "(" + `basename procname` + ")"
296 # "[" + `basename procname` + "]"
298 # If interpreter != ".", read the first line of procname, remove the
299 # leading #!, normalise whitespace, append procname, and attempt to
300 # match that against each command, either as is, or with extra words
301 # at the end. As an alternative, to deal with interpreted daemons
302 # using perl, the basename of the interpreter plus a colon is also
303 # tried as the prefix to procname.
307 if [ $# -ne 3 ]; then
308 err 3 'USAGE: _find_processes procname interpreter psargs'
315 if [ $_interpreter != "." ]; then # an interpreted script
316 _script=${_chroot}${_chroot:+"/"}$_procname
317 if [ -r $_script ]; then
318 read _interp < $_script # read interpreter name
321 _interp=${_interp#\#!} # strip #!
325 shift # drop env to get real name
328 if [ $_interpreter != $1 ]; then
329 warn "\$command_interpreter $_interpreter != $1"
333 warn "no shebang line in $_script"
338 warn "cannot read shebang line from $_script"
341 _interp="$* $_procname" # cleanup spaces, add _procname
344 _fp_match='case "$_argv" in
345 ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
346 else # a normal daemon
347 _procnamebn=${_procname##*/}
348 _fp_args='_arg0 _argv'
349 _fp_match='case "$_arg0" in
350 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
354 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
355 while read _npid _jid '"$_fp_args"'; do
357 if [ "$JID" -eq "$_jid" ];
358 then echo -n "$_pref$_npid";
365 # debug "in _find_processes: proccheck is ($_proccheck)."
370 # wait_for_pids pid [pid ...]
371 # spins until none of the pids exist
375 local _list _prefix _nlist _j
378 if [ -z "$_list" ]; then
385 if kill -0 $_j 2>/dev/null; then
386 _nlist="${_nlist}${_nlist:+ }$_j"
387 [ -n "$_prefix" ] && sleep 1
390 if [ -z "$_nlist" ]; then
394 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
396 pwait $_list 2>/dev/null
398 if [ -n "$_prefix" ]; then
405 # If rc_quiet is set (usually as a result of using faststart at
406 # boot time) check if rc_startmsgs is enabled.
410 if [ -n "$rc_quiet" ]; then
411 checkyesno rc_startmsgs
418 # run_rc_command argument
419 # Search for argument in the list of supported commands, which is:
420 # "start stop restart rcvar status poll ${extra_commands}"
421 # If there's a match, run ${argument}_cmd or the default method
424 # If argument has a given prefix, then change the operation as follows:
427 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
428 # force Set ${rcvar} to YES, and set rc_force=yes
429 # one Set ${rcvar} to YES
430 # quiet Don't output some diagnostics, and set rc_quiet=yes
432 # The following globals are used:
434 # Name Needed Purpose
435 # ---- ------ -------
436 # name y Name of script.
438 # command n Full path to command.
439 # Not needed if ${rc_arg}_cmd is set for
442 # command_args n Optional args/shell directives for command.
444 # command_interpreter n If not empty, command is interpreted, so
445 # call check_{pidfile,process}() appropriately.
447 # desc n Description of script.
449 # extra_commands n List of extra commands supported.
451 # pidfile n If set, use check_pidfile $pidfile $command,
452 # otherwise use check_process $command.
453 # In either case, only check if $command is set.
455 # procname n Process name to check for instead of $command.
457 # rcvar n This is checked with checkyesno to determine
458 # if the action should be run.
460 # ${name}_program n Full path to command.
461 # Meant to be used in /etc/rc.conf to override
464 # ${name}_chroot n Directory to chroot to before running ${command}
465 # Requires /usr to be mounted.
467 # ${name}_chdir n Directory to cd to before running ${command}
468 # (if not using ${name}_chroot).
470 # ${name}_flags n Arguments to call ${command} with.
471 # NOTE: $flags from the parent environment
472 # can be used to override this.
474 # ${name}_nice n Nice level to run ${command} at.
476 # ${name}_user n User to run ${command} as, using su(1) if not
477 # using ${name}_chroot.
478 # Requires /usr to be mounted.
480 # ${name}_group n Group to run chrooted ${command} as.
481 # Requires /usr to be mounted.
483 # ${name}_groups n Comma separated list of supplementary groups
484 # to run the chrooted ${command} with.
485 # Requires /usr to be mounted.
487 # ${rc_arg}_cmd n If set, use this as the method when invoked;
488 # Otherwise, use default command (see below)
490 # ${rc_arg}_precmd n If set, run just before performing the
491 # ${rc_arg}_cmd method in the default
492 # operation (i.e, after checking for required
493 # bits and process (non)existence).
494 # If this completes with a non-zero exit code,
495 # don't run ${rc_arg}_cmd.
497 # ${rc_arg}_postcmd n If set, run just after performing the
498 # ${rc_arg}_cmd method, if that method
499 # returned a zero exit code.
501 # required_dirs n If set, check for the existence of the given
502 # directories before running a (re)start command.
504 # required_files n If set, check for the readability of the given
505 # files before running a (re)start command.
507 # required_modules n If set, ensure the given kernel modules are
508 # loaded before running a (re)start command.
509 # The check and possible loads are actually
510 # done after start_precmd so that the modules
511 # aren't loaded in vain, should the precmd
512 # return a non-zero status to indicate a error.
513 # If a word in the list looks like "foo:bar",
514 # "foo" is the KLD file name and "bar" is the
515 # module name. If a word looks like "foo~bar",
516 # "foo" is the KLD file name and "bar" is a
517 # egrep(1) pattern matching the module name.
518 # Otherwise the module name is assumed to be
519 # the same as the KLD file name, which is most
520 # common. See load_kld().
522 # required_vars n If set, perform checkyesno on each of the
523 # listed variables before running the default
526 # Default behaviour for a given argument, if no override method is
529 # Argument Default behaviour
530 # -------- -----------------
531 # start if !running && checkyesno ${rcvar}
535 # rc_pid=$(check_pidfile $pidfile $command)
537 # rc_pid=$(check_process $command)
538 # kill $sig_stop $rc_pid
539 # wait_for_pids $rc_pid
540 # ($sig_stop defaults to TERM.)
542 # reload Similar to stop, except use $sig_reload instead,
543 # and doesn't wait_for_pids.
544 # $sig_reload defaults to HUP.
545 # Note that `reload' isn't provided by default,
546 # it should be enabled via $extra_commands.
548 # restart Run `stop' then `start'.
550 # status Show if ${command} is running, etc.
552 # poll Wait for ${command} to exit.
554 # rcvar Display what rc.conf variable is used (if any).
556 # Variables available to methods, and after run_rc_command() has
561 # rc_arg Argument to command, after fast/force/one processing
564 # rc_flags Flags to start the default command with.
565 # Defaults to ${name}_flags, unless overridden
566 # by $flags from the environment.
567 # This variable may be changed by the precmd method.
569 # rc_pid PID of command (if appropriate)
571 # rc_fast Not empty if "fast" was provided (q.v.)
573 # rc_force Not empty if "force" was provided (q.v.)
575 # rc_quiet Not empty if "quiet" was provided
582 if [ -z "$name" ]; then
583 err 3 'run_rc_command: $name is not set.'
586 # Don't repeat the first argument when passing additional command-
587 # line arguments to the command subroutines.
594 fast*) # "fast" prefix; don't check pid
595 rc_arg=${rc_arg#fast}
599 force*) # "force" prefix; always run
602 rc_arg=${rc_arg#${_rc_prefix}}
603 if [ -n "${rcvar}" ]; then
607 one*) # "one" prefix; set ${rcvar}=yes
609 rc_arg=${rc_arg#${_rc_prefix}}
610 if [ -n "${rcvar}" ]; then
614 quiet*) # "quiet" prefix; omit some messages
616 rc_arg=${rc_arg#${_rc_prefix}}
621 eval _override_command=\$${name}_program
622 command=${_override_command:-$command}
624 _keywords="start stop restart rcvar $extra_commands"
627 _procname=${procname:-${command}}
629 # setup pid check command
630 if [ -n "$_procname" ]; then
631 if [ -n "$pidfile" ]; then
632 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
634 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
636 if [ -n "$_pidcmd" ]; then
637 _keywords="${_keywords} status poll"
641 if [ -z "$rc_arg" ]; then
645 if [ -n "$flags" ]; then # allow override from environment
648 eval rc_flags=\$${name}_flags
650 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
651 _nice=\$${name}_nice _user=\$${name}_user \
652 _group=\$${name}_group _groups=\$${name}_groups
654 if [ -n "$_user" ]; then # unset $_user if running as that user
655 if [ "$_user" = "$(eval $IDCMD)" ]; then
660 eval $_pidcmd # determine the pid if necessary
662 for _elem in $_keywords; do
663 if [ "$_elem" != "$rc_arg" ]; then
666 # if ${rcvar} is set, $1 is not "rcvar"
667 # and ${rc_pid} is not set, then run
668 # checkyesno ${rcvar}
669 # and return if that failed
671 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a -z "${rc_pid}" ]; then
672 if ! checkyesno ${rcvar}; then
673 if [ -n "${rc_quiet}" ]; then
676 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
677 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
678 echo "instead of '${rc_arg}'."
683 # if there's a custom ${XXX_cmd},
684 # run that instead of the default
686 eval _cmd=\$${rc_arg}_cmd \
687 _precmd=\$${rc_arg}_precmd \
688 _postcmd=\$${rc_arg}_postcmd
690 if [ -n "$_cmd" ]; then
691 _run_rc_precmd || return 1
692 _run_rc_doit "$_cmd $rc_extra_args" || return 1
697 case "$rc_arg" in # default operations...
700 _run_rc_precmd || return 1
701 if [ -n "$rc_pid" ]; then
702 echo "${name} is running as pid $rc_pid."
704 echo "${name} is not running."
711 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
712 echo 1>&2 "${name} already running? (pid=$rc_pid)."
716 if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
717 warn "run_rc_command: cannot run $command"
721 if ! _run_rc_precmd; then
722 warn "failed precmd routine for ${name}"
726 # setup the full command to run
728 check_startmsgs && echo "Starting ${name}."
729 if [ -n "$_chroot" ]; then
731 ${_nice:+nice -n $_nice }\
732 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
733 $_chroot $command $rc_flags $command_args"
736 ${_chdir:+cd $_chdir && }\
737 $command $rc_flags $command_args"
738 if [ -n "$_user" ]; then
739 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
741 if [ -n "$_nice" ]; then
742 if [ -z "$_user" ]; then
743 _doit="sh -c \"$_doit\""
745 _doit="nice -n $_nice $_doit"
749 # run the full command
751 if ! _run_rc_doit "$_doit"; then
752 warn "failed to start ${name}"
756 # finally, run postcmd
762 if [ -z "$rc_pid" ]; then
763 [ -n "$rc_fast" ] && return 0
768 _run_rc_precmd || return 1
770 # send the signal to stop
772 echo "Stopping ${name}."
773 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
774 _run_rc_doit "$_doit" || return 1
776 # wait for the command to exit,
778 wait_for_pids $rc_pid
784 if [ -z "$rc_pid" ]; then
789 _run_rc_precmd || return 1
791 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
792 _run_rc_doit "$_doit" || return 1
798 # prevent restart being called more
799 # than once by any given script
801 if ${_rc_restart_done:-false}; then
804 _rc_restart_done=true
806 _run_rc_precmd || return 1
808 # run those in a subshell to keep global variables
809 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
810 ( run_rc_command ${_rc_prefix}start $rc_extra_args )
812 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
818 _run_rc_precmd || return 1
819 if [ -n "$rc_pid" ]; then
820 wait_for_pids $rc_pid
827 if [ -n "$desc" ]; then
833 # Get unique vars in $rcvar $rcvars
834 for _v in $rcvar $rcvars; do
836 $_v\ *|\ *$_v|*\ $_v\ *) ;;
843 if [ -z "$_v" ]; then
847 eval _desc=\$${_v}_desc
848 eval _defval=\$${_v}_defval
851 eval echo \"$_v=\\\"\$$_v\\\"\"
852 # decode multiple lines of _desc
853 while [ -n "$_desc" ]; do
856 echo "# $_h ${_desc%%^^*}"
861 echo "# $_h ${_desc}"
866 echo "# (default: \"$_defval\")"
879 echo 1>&2 "$0: unknown directive '$rc_arg'."
885 # Helper functions for run_rc_command: common code.
886 # They use such global variables besides the exported rc_* ones:
896 check_required_before "$rc_arg" || return 1
898 if [ -n "$_precmd" ]; then
899 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
900 eval "$_precmd $rc_extra_args"
903 # If precmd failed and force isn't set, request exit.
904 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
909 check_required_after "$rc_arg" || return 1
916 if [ -n "$_postcmd" ]; then
917 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
918 eval "$_postcmd $rc_extra_args"
926 debug "run_rc_command: doit: $*"
930 # If command failed and force isn't set, request exit.
931 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
942 if [ -n "$pidfile" ]; then
943 _pidmsg=" (check $pidfile)."
947 echo 1>&2 "${name} not running?${_pidmsg}"
954 _cmd="kill -$1 $rc_pid"
955 if [ -n "$_user" ]; then
956 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
962 # run_rc_script file arg
963 # Start the script `file' with `arg', and correctly handle the
964 # return value from the script. If `file' ends with `.sh', it's
965 # sourced into the current environment. If `file' appears to be
966 # a backup or scratch file, ignore it. Otherwise if it's
967 # executable run as a child process.
973 if [ -z "$_file" -o -z "$_arg" ]; then
974 err 3 'USAGE: run_rc_script file arg'
977 unset name command command_args command_interpreter \
978 extra_commands pidfile procname \
979 rcvar rcvars rcvars_obsolete required_dirs required_files \
981 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
984 /etc/rc.d/*.sh) # no longer allowed in the base
985 warn "Ignoring old-style startup script $_file"
987 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
988 warn "Ignoring scratch file $_file"
991 if [ -x $_file ]; then
992 if [ -n "$rc_fast_and_loose" ]; then
995 ( trap "echo Script $_file interrupted; kill -QUIT $$" 3
996 trap "echo Script $_file interrupted; exit 1" 2
997 trap "echo Script $_file running" 29
998 set $_arg; . $_file )
1006 # load_rc_config name
1007 # Source in the configuration file for a given name.
1011 local _name _var _defval _v _msg _new
1013 if [ -z "$_name" ]; then
1014 err 3 'USAGE: load_rc_config name'
1017 if ${_rc_conf_loaded:-false}; then
1020 if [ -r /etc/defaults/rc.conf ]; then
1021 debug "Sourcing /etc/defaults/rc.conf"
1022 . /etc/defaults/rc.conf
1024 elif [ -r /etc/rc.conf ]; then
1025 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1028 _rc_conf_loaded=true
1030 if [ -f /etc/rc.conf.d/"$_name" ]; then
1031 debug "Sourcing /etc/rc.conf.d/${_name}"
1032 . /etc/rc.conf.d/"$_name"
1035 # Old variable names support
1037 [ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1039 # Set defaults if defined.
1040 for _var in $rcvar $rcvars; do
1041 _defval=`eval echo "\\\$${_var}_defval"`
1042 if [ -n "$_defval" ]; then
1043 eval : \${$_var:=\$${_var}_defval}
1047 # check obsolete rc.conf variables
1048 for _var in $rcvars_obsolete; do
1049 _v=`eval echo \\$$_var`
1050 _msg=`eval echo \\$${_var}_obsolete_msg`
1051 _new=`eval echo \\$${_var}_newvar`
1056 if [ -z "$_new" ]; then
1059 eval $_new=\"\$$_var\"
1060 if [ -z "$_msg" ]; then
1061 _msg="Use \$$_new instead."
1064 warn "\$$_var is obsolete. $_msg"
1071 # load_rc_config_var name var
1072 # Read the rc.conf(5) var for name and set in the
1073 # current shell, using load_rc_config in a subshell to prevent
1074 # unwanted side effects from other variable assignments.
1076 load_rc_config_var()
1078 if [ $# -ne 2 ]; then
1079 err 3 'USAGE: load_rc_config_var name var'
1082 load_rc_config '$1' >/dev/null;
1083 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1084 echo '$2'=\'\''${'$2'}\'\'';
1091 # Print a usage string for $0, with `commands' being a list of
1096 echo -n 1>&2 "Usage: $0 [fast|force|one]("
1100 echo -n 1>&2 "$_sep$_elem"
1108 # err exitval message
1109 # Display message to stderr and log to the syslog, and exit with exitval.
1116 if [ -x /usr/bin/logger ]; then
1117 logger "$0: ERROR: $*"
1119 echo 1>&2 "$0: ERROR: $*"
1125 # Display message to stderr and log to the syslog.
1129 if [ -x /usr/bin/logger ]; then
1130 logger "$0: WARNING: $*"
1132 echo 1>&2 "$0: WARNING: $*"
1137 # Display informational message to stdout and log to syslog.
1142 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1143 if [ -x /usr/bin/logger ]; then
1144 logger "$0: INFO: $*"
1153 # If debugging is enabled in rc.conf output message to stderr.
1154 # BEWARE that you don't call any subroutine that itself calls this
1160 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1161 if [ -x /usr/bin/logger ]; then
1162 logger "$0: DEBUG: $*"
1164 echo 1>&2 "$0: DEBUG: $*"
1170 # backup_file action file cur backup
1171 # Make a backup copy of `file' into `cur', and save the previous
1172 # version of `cur' as `backup' or use rcs for archiving.
1174 # This routine checks the value of the backup_uses_rcs variable,
1175 # which can be either YES or NO.
1177 # The `action' keyword can be one of the following:
1179 # add `file' is now being backed up (and is possibly
1180 # being reentered into the backups system). `cur'
1181 # is created and RCS files, if necessary, are
1184 # update `file' has changed and needs to be backed up.
1185 # If `cur' exists, it is copied to to `back' or
1186 # checked into RCS (if the repository file is old),
1187 # and then `file' is copied to `cur'. Another RCS
1188 # check in done here if RCS is being used.
1190 # remove `file' is no longer being tracked by the backups
1191 # system. If RCS is not being used, `cur' is moved
1192 # to `back', otherwise an empty file is checked in,
1193 # and then `cur' is removed.
1203 if checkyesno backup_uses_rcs; then
1204 _msg0="backup archive"
1207 # ensure that history file is not locked
1208 if [ -f $_cur,v ]; then
1209 rcs -q -u -U -M $_cur
1212 # ensure after switching to rcs that the
1213 # current backup is not lost
1214 if [ -f $_cur ]; then
1215 # no archive, or current newer than archive
1216 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1217 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1226 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1229 chown root:wheel $_cur $_cur,v
1233 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1235 chown root:wheel $_cur $_cur,v
1242 if [ -f $_cur ]; then
1246 chown root:wheel $_cur
1255 # make_symlink src link
1256 # Make a symbolic link 'link' to src from basedir. If the
1257 # directory in which link is to be created does not exist
1258 # a warning will be displayed and an error will be returned.
1259 # Returns 0 on sucess, 1 otherwise.
1263 local src link linkdir _me
1266 linkdir="`dirname $link`"
1267 _me="make_symlink()"
1269 if [ -z "$src" -o -z "$link" ]; then
1270 warn "$_me: requires two arguments."
1273 if [ ! -d "$linkdir" ]; then
1274 warn "$_me: the directory $linkdir does not exist."
1277 if ! ln -sf $src $link; then
1278 warn "$_me: unable to make a symbolic link from $link to $src"
1284 # devfs_rulesets_from_file file
1285 # Reads a set of devfs commands from file, and creates
1286 # the specified rulesets with their rules. Returns non-zero
1287 # if there was an error.
1289 devfs_rulesets_from_file()
1293 _me="devfs_rulesets_from_file"
1296 if [ -z "$file" ]; then
1297 warn "$_me: you must specify a file"
1300 if [ ! -e "$file" ]; then
1301 debug "$_me: no such file ($file)"
1304 debug "reading rulesets from file ($file)"
1312 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1313 if [ -z "$rulenum" ]; then
1314 warn "$_me: cannot extract rule number ($line)"
1318 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1319 if [ -z "$rulename" ]; then
1320 warn "$_me: cannot extract rule name ($line)"
1324 eval $rulename=\$rulenum
1325 debug "found ruleset: $rulename=$rulenum"
1326 if ! /sbin/devfs rule -s $rulenum delset; then
1332 rulecmd="${line%%"\#*"}"
1333 # evaluate the command incase it includes
1335 if [ -n "$rulecmd" ]; then
1336 debug "adding rule ($rulecmd)"
1337 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1345 if [ $_err -ne 0 ]; then
1346 debug "error in $_me"
1353 # devfs_init_rulesets
1354 # Initializes rulesets from configuration files. Returns
1355 # non-zero if there was an error.
1357 devfs_init_rulesets()
1360 _me="devfs_init_rulesets"
1362 # Go through this only once
1363 if [ -n "$devfs_rulesets_init" ]; then
1364 debug "$_me: devfs rulesets already initialized"
1367 for file in $devfs_rulesets; do
1368 devfs_rulesets_from_file $file || return 1
1370 devfs_rulesets_init=1
1371 debug "$_me: devfs rulesets initialized"
1375 # devfs_set_ruleset ruleset [dir]
1376 # Sets the default ruleset of dir to ruleset. The ruleset argument
1377 # must be a ruleset name as specified in devfs.rules(5) file.
1378 # Returns non-zero if it could not set it successfully.
1383 [ -n "$1" ] && eval rs=\$$1 || rs=
1384 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1385 _me="devfs_set_ruleset"
1387 if [ -z "$rs" ]; then
1388 warn "$_me: you must specify a ruleset number"
1391 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1392 if ! /sbin/devfs $devdir ruleset $rs; then
1393 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1399 # devfs_apply_ruleset ruleset [dir]
1400 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1401 # The ruleset argument must be a ruleset name as specified
1402 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1403 # if it could not apply the ruleset.
1405 devfs_apply_ruleset()
1408 [ -n "$1" ] && eval rs=\$$1 || rs=
1409 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1410 _me="devfs_apply_ruleset"
1412 if [ -z "$rs" ]; then
1413 warn "$_me: you must specify a ruleset"
1416 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1417 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1418 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1424 # devfs_domount dir [ruleset]
1425 # Mount devfs on dir. If ruleset is specified it is set
1426 # on the mount-point. It must also be a ruleset name as specified
1427 # in a devfs.rules(5) file. Returns 0 on success.
1433 [ -n "$2" ] && rs=$2 || rs=
1434 _me="devfs_domount()"
1436 if [ -z "$devdir" ]; then
1437 warn "$_me: you must specify a mount-point"
1440 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1441 if ! mount -t devfs dev "$devdir"; then
1442 warn "$_me: Unable to mount devfs on $devdir"
1445 if [ -n "$rs" ]; then
1447 devfs_set_ruleset $rs $devdir
1448 devfs -m $devdir rule applyset
1453 # devfs_mount_jail dir [ruleset]
1454 # Mounts a devfs file system appropriate for jails
1455 # on the directory dir. If ruleset is specified, the ruleset
1456 # it names will be used instead. If present, ruleset must
1457 # be the name of a ruleset as defined in a devfs.rules(5) file.
1458 # This function returns non-zero if an error occurs.
1464 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1465 _me="devfs_mount_jail"
1468 if ! devfs_domount "$jdev" $rs; then
1469 warn "$_me: devfs was not mounted on $jdev"
1475 # Provide a function for normalizing the mounting of memory
1476 # filesystems. This should allow the rest of the code here to remain
1477 # as close as possible between 5-current and 4-stable.
1480 # $3 = (optional) extra mdmfs flags
1483 if [ -n "$3" ]; then
1486 /sbin/mdmfs $flags -s $1 md $2
1489 # Code common to scripts that need to load a kernel module
1490 # if it isn't in the kernel yet. Syntax:
1491 # load_kld [-e regex] [-m module] file
1492 # where -e or -m chooses the way to check if the module
1493 # is already loaded:
1494 # regex is egrep'd in the output from `kldstat -v',
1495 # module is passed to `kldstat -m'.
1496 # The default way is as though `-m file' were specified.
1499 local _loaded _mod _opt _re
1501 while getopts "e:m:" _opt; do
1504 m) _mod="$OPTARG" ;;
1505 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1508 shift $(($OPTIND - 1))
1509 if [ $# -ne 1 ]; then
1510 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1514 if [ -n "$_re" ]; then
1515 if kldstat -v | egrep -q -e "$_re"; then
1519 if kldstat -q -m "$_mod"; then
1524 if ! kldload "$1"; then
1525 warn "Unable to load kernel module $1"
1528 info "$1 kernel module loaded."
1531 debug "load_kld: $1 kernel module already loaded."
1537 # Change every $src in $str to $dst.
1538 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1542 local _str _src _dst _out _com
1549 for _com in ${_str}; do
1550 if [ -z "${_out}" ]; then
1553 _out="${_out}${_dst}${_com}"
1559 # Creates a list of providers for GELI encryption.
1562 local devices devices2
1563 local provider mountpoint type options rest
1565 # Create list of GELI providers from fstab.
1566 while read provider mountpoint type options rest ; do
1567 case ":${options}" in
1576 case ":${provider}" in
1581 # Skip swap devices.
1582 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1585 devices="${devices} ${provider}"
1590 # Append providers from geli_devices.
1591 devices="${devices} ${geli_devices}"
1593 for provider in ${devices}; do
1594 provider=${provider%.eli}
1595 provider=${provider#/dev/}
1596 devices2="${devices2} ${provider}"
1602 # Find scripts in local_startup directories that use the old syntax
1604 find_local_scripts_old () {
1607 for dir in ${local_startup}; do
1608 if [ -d "${dir}" ]; then
1609 for file in ${dir}/[0-9]*.sh; do
1610 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1612 zlist="$zlist $file"
1614 for file in ${dir}/[^0-9]*.sh; do
1615 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1617 slist="$slist $file"
1623 find_local_scripts_new () {
1625 for dir in ${local_startup}; do
1626 if [ -d "${dir}" ]; then
1627 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1630 *) if [ -x "$file" ]; then
1631 local_rc="${local_rc} ${file}"
1640 # check_required_{before|after} command
1641 # Check for things required by the command before and after its precmd,
1642 # respectively. The two separate functions are needed because some
1643 # conditions should prevent precmd from being run while other things
1644 # depend on precmd having already been run.
1646 check_required_before()
1652 for _f in $required_vars; do
1653 if ! checkyesno $_f; then
1654 warn "\$${_f} is not enabled."
1655 if [ -z "$rc_force" ]; then
1661 for _f in $required_dirs; do
1662 if [ ! -d "${_f}/." ]; then
1663 warn "${_f} is not a directory."
1664 if [ -z "$rc_force" ]; then
1670 for _f in $required_files; do
1671 if [ ! -r "${_f}" ]; then
1672 warn "${_f} is not readable."
1673 if [ -z "$rc_force" ]; then
1684 check_required_after()
1690 for _f in $required_modules; do
1692 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
1693 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
1696 if ! load_kld ${_args}; then
1697 if [ -z "$rc_force" ]; then