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
376 if [ -z "$_list" ]; then
383 if kill -0 $_j 2>/dev/null; then
384 _nlist="${_nlist}${_nlist:+ }$_j"
387 if [ -z "$_nlist" ]; then
391 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
395 if [ -n "$_prefix" ]; then
402 # If rc_quiet is set (usually as a result of using faststart at
403 # boot time) check if rc_startmsgs is enabled.
407 if [ -n "$rc_quiet" ]; then
408 checkyesno rc_startmsgs
415 # run_rc_command argument
416 # Search for argument in the list of supported commands, which is:
417 # "start stop restart rcvar status poll ${extra_commands}"
418 # If there's a match, run ${argument}_cmd or the default method
421 # If argument has a given prefix, then change the operation as follows:
424 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
425 # force Set ${rcvar} to YES, and set rc_force=yes
426 # one Set ${rcvar} to YES
427 # quiet Don't output some diagnostics, and set rc_quiet=yes
429 # The following globals are used:
431 # Name Needed Purpose
432 # ---- ------ -------
433 # name y Name of script.
435 # command n Full path to command.
436 # Not needed if ${rc_arg}_cmd is set for
439 # command_args n Optional args/shell directives for command.
441 # command_interpreter n If not empty, command is interpreted, so
442 # call check_{pidfile,process}() appropriately.
444 # desc n Description of script.
446 # extra_commands n List of extra commands supported.
448 # pidfile n If set, use check_pidfile $pidfile $command,
449 # otherwise use check_process $command.
450 # In either case, only check if $command is set.
452 # procname n Process name to check for instead of $command.
454 # rcvar n This is checked with checkyesno to determine
455 # if the action should be run.
457 # ${name}_program n Full path to command.
458 # Meant to be used in /etc/rc.conf to override
461 # ${name}_chroot n Directory to chroot to before running ${command}
462 # Requires /usr to be mounted.
464 # ${name}_chdir n Directory to cd to before running ${command}
465 # (if not using ${name}_chroot).
467 # ${name}_flags n Arguments to call ${command} with.
468 # NOTE: $flags from the parent environment
469 # can be used to override this.
471 # ${name}_nice n Nice level to run ${command} at.
473 # ${name}_user n User to run ${command} as, using su(1) if not
474 # using ${name}_chroot.
475 # Requires /usr to be mounted.
477 # ${name}_group n Group to run chrooted ${command} as.
478 # Requires /usr to be mounted.
480 # ${name}_groups n Comma separated list of supplementary groups
481 # to run the chrooted ${command} with.
482 # Requires /usr to be mounted.
484 # ${rc_arg}_cmd n If set, use this as the method when invoked;
485 # Otherwise, use default command (see below)
487 # ${rc_arg}_precmd n If set, run just before performing the
488 # ${rc_arg}_cmd method in the default
489 # operation (i.e, after checking for required
490 # bits and process (non)existence).
491 # If this completes with a non-zero exit code,
492 # don't run ${rc_arg}_cmd.
494 # ${rc_arg}_postcmd n If set, run just after performing the
495 # ${rc_arg}_cmd method, if that method
496 # returned a zero exit code.
498 # required_dirs n If set, check for the existence of the given
499 # directories before running a (re)start command.
501 # required_files n If set, check for the readability of the given
502 # files before running a (re)start command.
504 # required_modules n If set, ensure the given kernel modules are
505 # loaded before running a (re)start command.
506 # The check and possible loads are actually
507 # done after start_precmd so that the modules
508 # aren't loaded in vain, should the precmd
509 # return a non-zero status to indicate a error.
510 # If a word in the list looks like "foo:bar",
511 # "foo" is the KLD file name and "bar" is the
512 # module name. If a word looks like "foo~bar",
513 # "foo" is the KLD file name and "bar" is a
514 # egrep(1) pattern matching the module name.
515 # Otherwise the module name is assumed to be
516 # the same as the KLD file name, which is most
517 # common. See load_kld().
519 # required_vars n If set, perform checkyesno on each of the
520 # listed variables before running the default
523 # Default behaviour for a given argument, if no override method is
526 # Argument Default behaviour
527 # -------- -----------------
528 # start if !running && checkyesno ${rcvar}
532 # rc_pid=$(check_pidfile $pidfile $command)
534 # rc_pid=$(check_process $command)
535 # kill $sig_stop $rc_pid
536 # wait_for_pids $rc_pid
537 # ($sig_stop defaults to TERM.)
539 # reload Similar to stop, except use $sig_reload instead,
540 # and doesn't wait_for_pids.
541 # $sig_reload defaults to HUP.
542 # Note that `reload' isn't provided by default,
543 # it should be enabled via $extra_commands.
545 # restart Run `stop' then `start'.
547 # status Show if ${command} is running, etc.
549 # poll Wait for ${command} to exit.
551 # rcvar Display what rc.conf variable is used (if any).
553 # Variables available to methods, and after run_rc_command() has
558 # rc_arg Argument to command, after fast/force/one processing
561 # rc_flags Flags to start the default command with.
562 # Defaults to ${name}_flags, unless overridden
563 # by $flags from the environment.
564 # This variable may be changed by the precmd method.
566 # rc_pid PID of command (if appropriate)
568 # rc_fast Not empty if "fast" was provided (q.v.)
570 # rc_force Not empty if "force" was provided (q.v.)
572 # rc_quiet Not empty if "quiet" was provided
579 if [ -z "$name" ]; then
580 err 3 'run_rc_command: $name is not set.'
583 # Don't repeat the first argument when passing additional command-
584 # line arguments to the command subroutines.
591 fast*) # "fast" prefix; don't check pid
592 rc_arg=${rc_arg#fast}
596 force*) # "force" prefix; always run
599 rc_arg=${rc_arg#${_rc_prefix}}
600 if [ -n "${rcvar}" ]; then
604 one*) # "one" prefix; set ${rcvar}=yes
606 rc_arg=${rc_arg#${_rc_prefix}}
607 if [ -n "${rcvar}" ]; then
611 quiet*) # "quiet" prefix; omit some messages
613 rc_arg=${rc_arg#${_rc_prefix}}
618 eval _override_command=\$${name}_program
619 command=${_override_command:-$command}
621 _keywords="start stop restart rcvar $extra_commands"
624 _procname=${procname:-${command}}
626 # setup pid check command
627 if [ -n "$_procname" ]; then
628 if [ -n "$pidfile" ]; then
629 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
631 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
633 if [ -n "$_pidcmd" ]; then
634 _keywords="${_keywords} status poll"
638 if [ -z "$rc_arg" ]; then
642 if [ -n "$flags" ]; then # allow override from environment
645 eval rc_flags=\$${name}_flags
647 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
648 _nice=\$${name}_nice _user=\$${name}_user \
649 _group=\$${name}_group _groups=\$${name}_groups
651 if [ -n "$_user" ]; then # unset $_user if running as that user
652 if [ "$_user" = "$(eval $IDCMD)" ]; then
657 eval $_pidcmd # determine the pid if necessary
659 for _elem in $_keywords; do
660 if [ "$_elem" != "$rc_arg" ]; then
663 # if ${rcvar} is set, and $1 is not
665 # checkyesno ${rcvar}
666 # and return if that failed
668 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
669 if ! checkyesno ${rcvar}; then
670 if [ -n "${rc_quiet}" ]; then
673 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
674 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
675 echo "instead of '${rc_arg}'."
680 # if there's a custom ${XXX_cmd},
681 # run that instead of the default
683 eval _cmd=\$${rc_arg}_cmd \
684 _precmd=\$${rc_arg}_precmd \
685 _postcmd=\$${rc_arg}_postcmd
687 if [ -n "$_cmd" ]; then
688 _run_rc_precmd || return 1
689 _run_rc_doit "$_cmd $rc_extra_args" || return 1
694 case "$rc_arg" in # default operations...
697 _run_rc_precmd || return 1
698 if [ -n "$rc_pid" ]; then
699 echo "${name} is running as pid $rc_pid."
701 echo "${name} is not running."
708 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
709 echo 1>&2 "${name} already running? (pid=$rc_pid)."
713 if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
714 warn "run_rc_command: cannot run $command"
718 if ! _run_rc_precmd; then
719 warn "failed precmd routine for ${name}"
723 # setup the full command to run
725 check_startmsgs && echo "Starting ${name}."
726 if [ -n "$_chroot" ]; then
728 ${_nice:+nice -n $_nice }\
729 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
730 $_chroot $command $rc_flags $command_args"
733 ${_chdir:+cd $_chdir && }\
734 $command $rc_flags $command_args"
735 if [ -n "$_user" ]; then
736 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
738 if [ -n "$_nice" ]; then
739 if [ -z "$_user" ]; then
740 _doit="sh -c \"$_doit\""
742 _doit="nice -n $_nice $_doit"
746 # run the full command
748 if ! _run_rc_doit "$_doit"; then
749 warn "failed to start ${name}"
753 # finally, run postcmd
759 if [ -z "$rc_pid" ]; then
760 [ -n "$rc_fast" ] && return 0
765 _run_rc_precmd || return 1
767 # send the signal to stop
769 echo "Stopping ${name}."
770 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
771 _run_rc_doit "$_doit" || return 1
773 # wait for the command to exit,
775 wait_for_pids $rc_pid
781 if [ -z "$rc_pid" ]; then
786 _run_rc_precmd || return 1
788 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
789 _run_rc_doit "$_doit" || return 1
795 # prevent restart being called more
796 # than once by any given script
798 if ${_rc_restart_done:-false}; then
801 _rc_restart_done=true
803 _run_rc_precmd || return 1
805 # run those in a subshell to keep global variables
806 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
807 ( run_rc_command ${_rc_prefix}start $rc_extra_args )
809 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
815 _run_rc_precmd || return 1
816 if [ -n "$rc_pid" ]; then
817 wait_for_pids $rc_pid
824 if [ -n "$desc" ]; then
830 # Get unique vars in $rcvar $rcvars
831 for _v in $rcvar $rcvars; do
833 $_v\ *|\ *$_v|*\ $_v\ *) ;;
840 if [ -z "$_v" ]; then
844 eval _desc=\$${_v}_desc
845 eval _defval=\$${_v}_defval
848 eval echo \"$_v=\\\"\$$_v\\\"\"
849 # decode multiple lines of _desc
850 while [ -n "$_desc" ]; do
853 echo "# $_h ${_desc%%^^*}"
858 echo "# $_h ${_desc}"
863 echo "# (default: \"$_defval\")"
876 echo 1>&2 "$0: unknown directive '$rc_arg'."
882 # Helper functions for run_rc_command: common code.
883 # They use such global variables besides the exported rc_* ones:
893 check_required_before "$rc_arg" || return 1
895 if [ -n "$_precmd" ]; then
896 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
897 eval "$_precmd $rc_extra_args"
900 # If precmd failed and force isn't set, request exit.
901 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
906 check_required_after "$rc_arg" || return 1
913 if [ -n "$_postcmd" ]; then
914 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
915 eval "$_postcmd $rc_extra_args"
923 debug "run_rc_command: doit: $*"
927 # If command failed and force isn't set, request exit.
928 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
939 if [ -n "$pidfile" ]; then
940 _pidmsg=" (check $pidfile)."
944 echo 1>&2 "${name} not running?${_pidmsg}"
951 _cmd="kill -$1 $rc_pid"
952 if [ -n "$_user" ]; then
953 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
959 # run_rc_script file arg
960 # Start the script `file' with `arg', and correctly handle the
961 # return value from the script. If `file' ends with `.sh', it's
962 # sourced into the current environment. If `file' appears to be
963 # a backup or scratch file, ignore it. Otherwise if it's
964 # executable run as a child process.
970 if [ -z "$_file" -o -z "$_arg" ]; then
971 err 3 'USAGE: run_rc_script file arg'
974 unset name command command_args command_interpreter \
975 extra_commands pidfile procname \
976 rcvar rcvars rcvars_obsolete required_dirs required_files \
978 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
981 /etc/rc.d/*.sh) # no longer allowed in the base
982 warn "Ignoring old-style startup script $_file"
984 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
985 warn "Ignoring scratch file $_file"
988 if [ -x $_file ]; then
989 if [ -n "$rc_fast_and_loose" ]; then
992 ( trap "echo Script $_file interrupted; kill -QUIT $$" 3
993 trap "echo Script $_file interrupted; exit 1" 2
994 trap "echo Script $_file running" 29
995 set $_arg; . $_file )
1003 # load_rc_config name
1004 # Source in the configuration file for a given name.
1008 local _name _var _defval _v _msg _new
1010 if [ -z "$_name" ]; then
1011 err 3 'USAGE: load_rc_config name'
1014 if ${_rc_conf_loaded:-false}; then
1017 if [ -r /etc/defaults/rc.conf ]; then
1018 debug "Sourcing /etc/defaults/rc.conf"
1019 . /etc/defaults/rc.conf
1021 elif [ -r /etc/rc.conf ]; then
1022 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1025 _rc_conf_loaded=true
1027 if [ -f /etc/rc.conf.d/"$_name" ]; then
1028 debug "Sourcing /etc/rc.conf.d/${_name}"
1029 . /etc/rc.conf.d/"$_name"
1032 # Old variable names support
1034 [ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
1036 # Set defaults if defined.
1037 for _var in $rcvar $rcvars; do
1038 _defval=`eval echo "\\\$${_var}_defval"`
1039 if [ -n "$_defval" ]; then
1040 eval : \${$_var:=\$${_var}_defval}
1044 # check obsolete rc.conf variables
1045 for _var in $rcvars_obsolete; do
1046 _v=`eval echo \\$$_var`
1047 _msg=`eval echo \\$${_var}_obsolete_msg`
1048 _new=`eval echo \\$${_var}_newvar`
1053 if [ -z "$_new" ]; then
1056 eval $_new=\"\$$_var\"
1057 if [ -z "$_msg" ]; then
1058 _msg="Use \$$_new instead."
1061 warn "\$$_var is obsolete. $_msg"
1068 # load_rc_config_var name var
1069 # Read the rc.conf(5) var for name and set in the
1070 # current shell, using load_rc_config in a subshell to prevent
1071 # unwanted side effects from other variable assignments.
1073 load_rc_config_var()
1075 if [ $# -ne 2 ]; then
1076 err 3 'USAGE: load_rc_config_var name var'
1079 load_rc_config '$1' >/dev/null;
1080 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
1081 echo '$2'=\'\''${'$2'}\'\'';
1088 # Print a usage string for $0, with `commands' being a list of
1093 echo -n 1>&2 "Usage: $0 [fast|force|one]("
1097 echo -n 1>&2 "$_sep$_elem"
1105 # err exitval message
1106 # Display message to stderr and log to the syslog, and exit with exitval.
1113 if [ -x /usr/bin/logger ]; then
1114 logger "$0: ERROR: $*"
1116 echo 1>&2 "$0: ERROR: $*"
1122 # Display message to stderr and log to the syslog.
1126 if [ -x /usr/bin/logger ]; then
1127 logger "$0: WARNING: $*"
1129 echo 1>&2 "$0: WARNING: $*"
1134 # Display informational message to stdout and log to syslog.
1139 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1140 if [ -x /usr/bin/logger ]; then
1141 logger "$0: INFO: $*"
1150 # If debugging is enabled in rc.conf output message to stderr.
1151 # BEWARE that you don't call any subroutine that itself calls this
1157 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1158 if [ -x /usr/bin/logger ]; then
1159 logger "$0: DEBUG: $*"
1161 echo 1>&2 "$0: DEBUG: $*"
1167 # backup_file action file cur backup
1168 # Make a backup copy of `file' into `cur', and save the previous
1169 # version of `cur' as `backup' or use rcs for archiving.
1171 # This routine checks the value of the backup_uses_rcs variable,
1172 # which can be either YES or NO.
1174 # The `action' keyword can be one of the following:
1176 # add `file' is now being backed up (and is possibly
1177 # being reentered into the backups system). `cur'
1178 # is created and RCS files, if necessary, are
1181 # update `file' has changed and needs to be backed up.
1182 # If `cur' exists, it is copied to to `back' or
1183 # checked into RCS (if the repository file is old),
1184 # and then `file' is copied to `cur'. Another RCS
1185 # check in done here if RCS is being used.
1187 # remove `file' is no longer being tracked by the backups
1188 # system. If RCS is not being used, `cur' is moved
1189 # to `back', otherwise an empty file is checked in,
1190 # and then `cur' is removed.
1200 if checkyesno backup_uses_rcs; then
1201 _msg0="backup archive"
1204 # ensure that history file is not locked
1205 if [ -f $_cur,v ]; then
1206 rcs -q -u -U -M $_cur
1209 # ensure after switching to rcs that the
1210 # current backup is not lost
1211 if [ -f $_cur ]; then
1212 # no archive, or current newer than archive
1213 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1214 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1223 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1226 chown root:wheel $_cur $_cur,v
1230 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1232 chown root:wheel $_cur $_cur,v
1239 if [ -f $_cur ]; then
1243 chown root:wheel $_cur
1252 # make_symlink src link
1253 # Make a symbolic link 'link' to src from basedir. If the
1254 # directory in which link is to be created does not exist
1255 # a warning will be displayed and an error will be returned.
1256 # Returns 0 on sucess, 1 otherwise.
1260 local src link linkdir _me
1263 linkdir="`dirname $link`"
1264 _me="make_symlink()"
1266 if [ -z "$src" -o -z "$link" ]; then
1267 warn "$_me: requires two arguments."
1270 if [ ! -d "$linkdir" ]; then
1271 warn "$_me: the directory $linkdir does not exist."
1274 if ! ln -sf $src $link; then
1275 warn "$_me: unable to make a symbolic link from $link to $src"
1281 # devfs_rulesets_from_file file
1282 # Reads a set of devfs commands from file, and creates
1283 # the specified rulesets with their rules. Returns non-zero
1284 # if there was an error.
1286 devfs_rulesets_from_file()
1290 _me="devfs_rulesets_from_file"
1293 if [ -z "$file" ]; then
1294 warn "$_me: you must specify a file"
1297 if [ ! -e "$file" ]; then
1298 debug "$_me: no such file ($file)"
1301 debug "reading rulesets from file ($file)"
1309 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1310 if [ -z "$rulenum" ]; then
1311 warn "$_me: cannot extract rule number ($line)"
1315 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1316 if [ -z "$rulename" ]; then
1317 warn "$_me: cannot extract rule name ($line)"
1321 eval $rulename=\$rulenum
1322 debug "found ruleset: $rulename=$rulenum"
1323 if ! /sbin/devfs rule -s $rulenum delset; then
1329 rulecmd="${line%%"\#*"}"
1330 # evaluate the command incase it includes
1332 if [ -n "$rulecmd" ]; then
1333 debug "adding rule ($rulecmd)"
1334 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1342 if [ $_err -ne 0 ]; then
1343 debug "error in $_me"
1350 # devfs_init_rulesets
1351 # Initializes rulesets from configuration files. Returns
1352 # non-zero if there was an error.
1354 devfs_init_rulesets()
1357 _me="devfs_init_rulesets"
1359 # Go through this only once
1360 if [ -n "$devfs_rulesets_init" ]; then
1361 debug "$_me: devfs rulesets already initialized"
1364 for file in $devfs_rulesets; do
1365 devfs_rulesets_from_file $file || return 1
1367 devfs_rulesets_init=1
1368 debug "$_me: devfs rulesets initialized"
1372 # devfs_set_ruleset ruleset [dir]
1373 # Sets the default ruleset of dir to ruleset. The ruleset argument
1374 # must be a ruleset name as specified in devfs.rules(5) file.
1375 # Returns non-zero if it could not set it successfully.
1380 [ -n "$1" ] && eval rs=\$$1 || rs=
1381 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1382 _me="devfs_set_ruleset"
1384 if [ -z "$rs" ]; then
1385 warn "$_me: you must specify a ruleset number"
1388 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1389 if ! /sbin/devfs $devdir ruleset $rs; then
1390 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1396 # devfs_apply_ruleset ruleset [dir]
1397 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1398 # The ruleset argument must be a ruleset name as specified
1399 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1400 # if it could not apply the ruleset.
1402 devfs_apply_ruleset()
1405 [ -n "$1" ] && eval rs=\$$1 || rs=
1406 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1407 _me="devfs_apply_ruleset"
1409 if [ -z "$rs" ]; then
1410 warn "$_me: you must specify a ruleset"
1413 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1414 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1415 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1421 # devfs_domount dir [ruleset]
1422 # Mount devfs on dir. If ruleset is specified it is set
1423 # on the mount-point. It must also be a ruleset name as specified
1424 # in a devfs.rules(5) file. Returns 0 on success.
1430 [ -n "$2" ] && rs=$2 || rs=
1431 _me="devfs_domount()"
1433 if [ -z "$devdir" ]; then
1434 warn "$_me: you must specify a mount-point"
1437 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1438 if ! mount -t devfs dev "$devdir"; then
1439 warn "$_me: Unable to mount devfs on $devdir"
1442 if [ -n "$rs" ]; then
1444 devfs_set_ruleset $rs $devdir
1445 devfs -m $devdir rule applyset
1450 # devfs_mount_jail dir [ruleset]
1451 # Mounts a devfs file system appropriate for jails
1452 # on the directory dir. If ruleset is specified, the ruleset
1453 # it names will be used instead. If present, ruleset must
1454 # be the name of a ruleset as defined in a devfs.rules(5) file.
1455 # This function returns non-zero if an error occurs.
1461 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1462 _me="devfs_mount_jail"
1465 if ! devfs_domount "$jdev" $rs; then
1466 warn "$_me: devfs was not mounted on $jdev"
1472 # Provide a function for normalizing the mounting of memory
1473 # filesystems. This should allow the rest of the code here to remain
1474 # as close as possible between 5-current and 4-stable.
1477 # $3 = (optional) extra mdmfs flags
1480 if [ -n "$3" ]; then
1483 /sbin/mdmfs $flags -s $1 md $2
1486 # Code common to scripts that need to load a kernel module
1487 # if it isn't in the kernel yet. Syntax:
1488 # load_kld [-e regex] [-m module] file
1489 # where -e or -m chooses the way to check if the module
1490 # is already loaded:
1491 # regex is egrep'd in the output from `kldstat -v',
1492 # module is passed to `kldstat -m'.
1493 # The default way is as though `-m file' were specified.
1496 local _loaded _mod _opt _re
1498 while getopts "e:m:" _opt; do
1501 m) _mod="$OPTARG" ;;
1502 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1505 shift $(($OPTIND - 1))
1506 if [ $# -ne 1 ]; then
1507 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1511 if [ -n "$_re" ]; then
1512 if kldstat -v | egrep -q -e "$_re"; then
1516 if kldstat -q -m "$_mod"; then
1521 if ! kldload "$1"; then
1522 warn "Unable to load kernel module $1"
1525 info "$1 kernel module loaded."
1528 debug "load_kld: $1 kernel module already loaded."
1534 # Change every $src in $str to $dst.
1535 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1539 local _str _src _dst _out _com
1546 for _com in ${_str}; do
1547 if [ -z "${_out}" ]; then
1550 _out="${_out}${_dst}${_com}"
1556 # Creates a list of providers for GELI encryption.
1559 local devices devices2
1560 local provider mountpoint type options rest
1562 # Create list of GELI providers from fstab.
1563 while read provider mountpoint type options rest ; do
1564 case ":${options}" in
1573 case ":${provider}" in
1578 # Skip swap devices.
1579 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1582 devices="${devices} ${provider}"
1587 # Append providers from geli_devices.
1588 devices="${devices} ${geli_devices}"
1590 for provider in ${devices}; do
1591 provider=${provider%.eli}
1592 provider=${provider#/dev/}
1593 devices2="${devices2} ${provider}"
1599 # Find scripts in local_startup directories that use the old syntax
1601 find_local_scripts_old () {
1604 for dir in ${local_startup}; do
1605 if [ -d "${dir}" ]; then
1606 for file in ${dir}/[0-9]*.sh; do
1607 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1609 zlist="$zlist $file"
1611 for file in ${dir}/[^0-9]*.sh; do
1612 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1614 slist="$slist $file"
1620 find_local_scripts_new () {
1622 for dir in ${local_startup}; do
1623 if [ -d "${dir}" ]; then
1624 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1627 *) if [ -x "$file" ]; then
1628 local_rc="${local_rc} ${file}"
1637 # check_required_{before|after} command
1638 # Check for things required by the command before and after its precmd,
1639 # respectively. The two separate functions are needed because some
1640 # conditions should prevent precmd from being run while other things
1641 # depend on precmd having already been run.
1643 check_required_before()
1649 for _f in $required_vars; do
1650 if ! checkyesno $_f; then
1651 warn "\$${_f} is not enabled."
1652 if [ -z "$rc_force" ]; then
1658 for _f in $required_dirs; do
1659 if [ ! -d "${_f}/." ]; then
1660 warn "${_f} is not a directory."
1661 if [ -z "$rc_force" ]; then
1667 for _f in $required_files; do
1668 if [ ! -r "${_f}" ]; then
1669 warn "${_f} is not readable."
1670 if [ -z "$rc_force" ]; then
1681 check_required_after()
1687 for _f in $required_modules; do
1689 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
1690 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
1693 if ! load_kld ${_args}; then
1694 if [ -z "$rc_force" ]; then