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"
77 # Set the variable name enabling a specific service.
78 # FreeBSD uses ${service}_enable, while NetBSD uses
79 # just the name of the service. For example:
80 # FreeBSD: sendmail_enable="YES"
81 # NetBSD : sendmail="YES"
82 # $1 - if $name is not the base to work of off, specify
95 echo ${base_var}_enable
107 # force_depend script
108 # Force a service to start. Intended for use by services
109 # to resolve dependency issues. It is assumed the caller
110 # has check to make sure this call is necessary
111 # $1 - filename of script, in /etc/rc.d, to run
117 info "${name} depends on ${_depend}, which will be forced to start."
118 if ! /etc/rc.d/${_depend} forcestart; then
119 warn "Unable to force ${_depend}. It may already be running."
127 # Test $1 variable, and warn if not set to YES or NO.
128 # Return 0 if it's "yes" (et al), nonzero otherwise.
133 debug "checkyesno: $1 is set to $_value."
136 # "yes", "true", "on", or "1"
137 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
141 # "no", "false", "off", or "0"
142 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
146 warn "\$${1} is not set properly - see ${rcvar_manpage}."
154 # print the list in reverse order
160 _revlist="$_revfile $_revlist"
166 # If booting directly to multiuser or $always is enabled,
167 # send SIGTERM to the parent (/etc/rc) to abort the boot.
168 # Otherwise just exit.
175 # "yes", "true", "on", or "1"
176 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
183 if [ "$autoboot" = yes -o "$always" = true ]; then
184 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
191 # mount_critical_filesystems type
192 # Go through the list of critical filesystems as provided in
193 # the rc.conf(5) variable $critical_filesystems_${type}, checking
194 # each one to see if it is mounted, and if it is not, mounting it.
196 mount_critical_filesystems()
198 eval _fslist=\$critical_filesystems_${1}
199 for _fs in $_fslist; do
202 while read what _on on _type type; do
203 if [ $on = $_fs ]; then
210 mount $_fs >/dev/null 2>&1
217 # check_pidfile pidfile procname [interpreter]
218 # Parses the first line of pidfile for a PID, and ensures
219 # that the process is running and matches procname.
220 # Prints the matching PID upon success, nothing otherwise.
221 # interpreter is optional; see _find_processes() for details.
228 if [ -z "$_pidfile" -o -z "$_procname" ]; then
229 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
231 if [ ! -f $_pidfile ]; then
232 debug "pid file ($_pidfile): not readable."
235 read _pid _junk < $_pidfile
236 if [ -z "$_pid" ]; then
237 debug "pid file ($_pidfile): no pid in file."
240 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
244 # check_process procname [interpreter]
245 # Ensures that a process (or processes) named procname is running.
246 # Prints a list of matching PIDs.
247 # interpreter is optional; see _find_processes() for details.
253 if [ -z "$_procname" ]; then
254 err 3 'USAGE: check_process procname [interpreter]'
256 _find_processes $_procname ${_interpreter:-.} '-ax'
260 # _find_processes procname interpreter psargs
261 # Search for procname in the output of ps generated by psargs.
262 # Prints the PIDs of any matching processes, space separated.
264 # If interpreter == ".", check the following variations of procname
265 # against the first word of each command:
267 # `basename procname`
268 # `basename procname` + ":"
269 # "(" + `basename procname` + ")"
270 # "[" + `basename procname` + "]"
272 # If interpreter != ".", read the first line of procname, remove the
273 # leading #!, normalise whitespace, append procname, and attempt to
274 # match that against each command, either as is, or with extra words
275 # at the end. As an alternative, to deal with interpreted daemons
276 # using perl, the basename of the interpreter plus a colon is also
277 # tried as the prefix to procname.
281 if [ $# -ne 3 ]; then
282 err 3 'USAGE: _find_processes procname interpreter psargs'
289 if [ $_interpreter != "." ]; then # an interpreted script
290 _script=${_chroot}${_chroot:+"/"}$_procname
291 if [ -r $_script ]; then
292 read _interp < $_script # read interpreter name
295 _interp=${_interp#\#!} # strip #!
299 shift # drop env to get real name
302 if [ $_interpreter != $1 ]; then
303 warn "\$command_interpreter $_interpreter != $1"
307 warn "no shebang line in $_script"
312 warn "cannot read shebang line from $_script"
315 _interp="$* $_procname" # cleanup spaces, add _procname
318 _fp_match='case "$_argv" in
319 ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
320 else # a normal daemon
321 _procnamebn=${_procname##*/}
322 _fp_args='_arg0 _argv'
323 _fp_match='case "$_arg0" in
324 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
328 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
329 while read _npid _jid '"$_fp_args"'; do
331 if [ "$JID" -eq "$_jid" ];
332 then echo -n "$_pref$_npid";
339 # debug "in _find_processes: proccheck is ($_proccheck)."
344 # wait_for_pids pid [pid ...]
345 # spins until none of the pids exist
350 if [ -z "$_list" ]; then
357 if kill -0 $_j 2>/dev/null; then
358 _nlist="${_nlist}${_nlist:+ }$_j"
361 if [ -z "$_nlist" ]; then
365 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
369 if [ -n "$_prefix" ]; then
375 # run_rc_command argument
376 # Search for argument in the list of supported commands, which is:
377 # "start stop restart rcvar status poll ${extra_commands}"
378 # If there's a match, run ${argument}_cmd or the default method
381 # If argument has a given prefix, then change the operation as follows:
384 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
385 # force Set ${rcvar} to YES, and set rc_force=yes
386 # one Set ${rcvar} to YES
387 # quiet Don't output some diagnostics, and set rc_quiet=yes
389 # The following globals are used:
391 # Name Needed Purpose
392 # ---- ------ -------
393 # name y Name of script.
395 # command n Full path to command.
396 # Not needed if ${rc_arg}_cmd is set for
399 # command_args n Optional args/shell directives for command.
401 # command_interpreter n If not empty, command is interpreted, so
402 # call check_{pidfile,process}() appropriately.
404 # extra_commands n List of extra commands supported.
406 # pidfile n If set, use check_pidfile $pidfile $command,
407 # otherwise use check_process $command.
408 # In either case, only check if $command is set.
410 # procname n Process name to check for instead of $command.
412 # rcvar n This is checked with checkyesno to determine
413 # if the action should be run.
415 # ${name}_program n Full path to command.
416 # Meant to be used in /etc/rc.conf to override
419 # ${name}_chroot n Directory to chroot to before running ${command}
420 # Requires /usr to be mounted.
422 # ${name}_chdir n Directory to cd to before running ${command}
423 # (if not using ${name}_chroot).
425 # ${name}_flags n Arguments to call ${command} with.
426 # NOTE: $flags from the parent environment
427 # can be used to override this.
429 # ${name}_nice n Nice level to run ${command} at.
431 # ${name}_user n User to run ${command} as, using su(1) if not
432 # using ${name}_chroot.
433 # Requires /usr to be mounted.
435 # ${name}_group n Group to run chrooted ${command} as.
436 # Requires /usr to be mounted.
438 # ${name}_groups n Comma separated list of supplementary groups
439 # to run the chrooted ${command} with.
440 # Requires /usr to be mounted.
442 # ${rc_arg}_cmd n If set, use this as the method when invoked;
443 # Otherwise, use default command (see below)
445 # ${rc_arg}_precmd n If set, run just before performing the
446 # ${rc_arg}_cmd method in the default
447 # operation (i.e, after checking for required
448 # bits and process (non)existence).
449 # If this completes with a non-zero exit code,
450 # don't run ${rc_arg}_cmd.
452 # ${rc_arg}_postcmd n If set, run just after performing the
453 # ${rc_arg}_cmd method, if that method
454 # returned a zero exit code.
456 # required_dirs n If set, check for the existence of the given
457 # directories before running a (re)start command.
459 # required_files n If set, check for the readability of the given
460 # files before running a (re)start command.
462 # required_modules n If set, ensure the given kernel modules are
463 # loaded before running a (re)start command.
464 # The check and possible loads are actually
465 # done after start_precmd so that the modules
466 # aren't loaded in vain, should the precmd
467 # return a non-zero status to indicate a error.
468 # If a word in the list looks like "foo:bar",
469 # "foo" is the KLD file name and "bar" is the
470 # module name. If a word looks like "foo~bar",
471 # "foo" is the KLD file name and "bar" is a
472 # egrep(1) pattern matching the module name.
473 # Otherwise the module name is assumed to be
474 # the same as the KLD file name, which is most
475 # common. See load_kld().
477 # required_vars n If set, perform checkyesno on each of the
478 # listed variables before running the default
481 # Default behaviour for a given argument, if no override method is
484 # Argument Default behaviour
485 # -------- -----------------
486 # start if !running && checkyesno ${rcvar}
490 # rc_pid=$(check_pidfile $pidfile $command)
492 # rc_pid=$(check_process $command)
493 # kill $sig_stop $rc_pid
494 # wait_for_pids $rc_pid
495 # ($sig_stop defaults to TERM.)
497 # reload Similar to stop, except use $sig_reload instead,
498 # and doesn't wait_for_pids.
499 # $sig_reload defaults to HUP.
500 # Note that `reload' isn't provided by default,
501 # it should be enabled via $extra_commands.
503 # restart Run `stop' then `start'.
505 # status Show if ${command} is running, etc.
507 # poll Wait for ${command} to exit.
509 # rcvar Display what rc.conf variable is used (if any).
511 # Variables available to methods, and after run_rc_command() has
516 # rc_arg Argument to command, after fast/force/one processing
519 # rc_flags Flags to start the default command with.
520 # Defaults to ${name}_flags, unless overridden
521 # by $flags from the environment.
522 # This variable may be changed by the precmd method.
524 # rc_pid PID of command (if appropriate)
526 # rc_fast Not empty if "fast" was provided (q.v.)
528 # rc_force Not empty if "force" was provided (q.v.)
530 # rc_quiet Not empty if "quiet" was provided
537 if [ -z "$name" ]; then
538 err 3 'run_rc_command: $name is not set.'
541 # Don't repeat the first argument when passing additional command-
542 # line arguments to the command subroutines.
549 fast*) # "fast" prefix; don't check pid
550 rc_arg=${rc_arg#fast}
554 force*) # "force prefix; always run
557 rc_arg=${rc_arg#${_rc_prefix}}
558 if [ -n "${rcvar}" ]; then
562 one*) # "one" prefix; set ${rcvar}=yes
564 rc_arg=${rc_arg#${_rc_prefix}}
565 if [ -n "${rcvar}" ]; then
569 quiet*) # "quiet" prefix; omit some messages
571 rc_arg=${rc_arg#${_rc_prefix}}
576 eval _override_command=\$${name}_program
577 command=${command:+${_override_command:-$command}}
579 _keywords="start stop restart rcvar $extra_commands"
582 _procname=${procname:-${command}}
584 # setup pid check command
585 if [ -n "$_procname" ]; then
586 if [ -n "$pidfile" ]; then
587 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
589 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
591 if [ -n "$_pidcmd" ]; then
592 _keywords="${_keywords} status poll"
596 if [ -z "$rc_arg" ]; then
600 if [ -n "$flags" ]; then # allow override from environment
603 eval rc_flags=\$${name}_flags
605 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
606 _nice=\$${name}_nice _user=\$${name}_user \
607 _group=\$${name}_group _groups=\$${name}_groups
609 if [ -n "$_user" ]; then # unset $_user if running as that user
610 if [ "$_user" = "$(eval $IDCMD)" ]; then
615 eval $_pidcmd # determine the pid if necessary
617 for _elem in $_keywords; do
618 if [ "$_elem" != "$rc_arg" ]; then
621 # if ${rcvar} is set, and $1 is not
623 # checkyesno ${rcvar}
624 # and return if that failed
626 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
627 if ! checkyesno ${rcvar}; then
628 if [ -n "${rc_quiet}" ]; then
631 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
632 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
633 echo "instead of '${rc_arg}'."
638 # if there's a custom ${XXX_cmd},
639 # run that instead of the default
641 eval _cmd=\$${rc_arg}_cmd \
642 _precmd=\$${rc_arg}_precmd \
643 _postcmd=\$${rc_arg}_postcmd
645 if [ -n "$_cmd" ]; then
646 _run_rc_precmd || return 1
647 _run_rc_doit "$_cmd $rc_extra_args" || return 1
652 case "$rc_arg" in # default operations...
655 _run_rc_precmd || return 1
656 if [ -n "$rc_pid" ]; then
657 echo "${name} is running as pid $rc_pid."
659 echo "${name} is not running."
666 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
667 echo 1>&2 "${name} already running? (pid=$rc_pid)."
671 if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then
672 warn "run_rc_command: cannot run $command"
676 if ! _run_rc_precmd; then
677 warn "failed precmd routine for ${name}"
681 # setup the full command to run
684 if [ -n "${rc_quiet}" ]; then
685 if ! checkyesno rc_startmsgs; then
686 unset _show_startmsgs
689 [ -n "$_show_startmsgs" ] && echo "Starting ${name}."
690 if [ -n "$_chroot" ]; then
692 ${_nice:+nice -n $_nice }\
693 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
694 $_chroot $command $rc_flags $command_args"
697 ${_chdir:+cd $_chdir && }\
698 $command $rc_flags $command_args"
699 if [ -n "$_user" ]; then
700 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
702 if [ -n "$_nice" ]; then
703 if [ -z "$_user" ]; then
704 _doit="sh -c \"$_doit\""
706 _doit="nice -n $_nice $_doit"
710 # run the full command
712 if ! _run_rc_doit "$_doit"; then
713 warn "failed to start ${name}"
717 # finally, run postcmd
723 if [ -z "$rc_pid" ]; then
724 [ -n "$rc_fast" ] && return 0
729 _run_rc_precmd || return 1
731 # send the signal to stop
733 echo "Stopping ${name}."
734 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
735 _run_rc_doit "$_doit" || return 1
737 # wait for the command to exit,
739 wait_for_pids $rc_pid
745 if [ -z "$rc_pid" ]; then
750 _run_rc_precmd || return 1
752 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
753 _run_rc_doit "$_doit" || return 1
759 # prevent restart being called more
760 # than once by any given script
762 if ${_rc_restart_done:-false}; then
765 _rc_restart_done=true
767 _run_rc_precmd || return 1
769 # run those in a subshell to keep global variables
770 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
771 ( run_rc_command ${_rc_prefix}start $rc_extra_args )
773 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
779 _run_rc_precmd || return 1
780 if [ -n "$rc_pid" ]; then
781 wait_for_pids $rc_pid
788 if [ -n "$rcvar" ]; then
789 if checkyesno ${rcvar}; then
805 echo 1>&2 "$0: unknown directive '$rc_arg'."
811 # Helper functions for run_rc_command: common code.
812 # They use such global variables besides the exported rc_* ones:
822 check_required_before "$rc_arg" || return 1
824 if [ -n "$_precmd" ]; then
825 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
826 eval "$_precmd $rc_extra_args"
829 # If precmd failed and force isn't set, request exit.
830 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
835 check_required_after "$rc_arg" || return 1
842 if [ -n "$_postcmd" ]; then
843 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
844 eval "$_postcmd $rc_extra_args"
852 debug "run_rc_command: doit: $*"
856 # If command failed and force isn't set, request exit.
857 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
868 if [ -n "$pidfile" ]; then
869 _pidmsg=" (check $pidfile)."
873 echo 1>&2 "${name} not running?${_pidmsg}"
880 _cmd="kill -$1 $rc_pid"
881 if [ -n "$_user" ]; then
882 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
888 # run_rc_script file arg
889 # Start the script `file' with `arg', and correctly handle the
890 # return value from the script. If `file' ends with `.sh', it's
891 # sourced into the current environment. If `file' appears to be
892 # a backup or scratch file, ignore it. Otherwise if it's
893 # executable run as a child process.
899 if [ -z "$_file" -o -z "$_arg" ]; then
900 err 3 'USAGE: run_rc_script file arg'
903 unset name command command_args command_interpreter \
904 extra_commands pidfile procname \
905 rcvar required_dirs required_files required_vars
906 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
909 /etc/rc.d/*.sh) # no longer allowed in the base
910 warn "Ignoring old-style startup script $_file"
912 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
913 warn "Ignoring scratch file $_file"
916 if [ -x $_file ]; then
917 if [ -n "$rc_fast_and_loose" ]; then
920 ( trap "echo Script $_file interrupted; kill -QUIT $$" 3
921 trap "echo Script $_file interrupted; exit 1" 2
922 trap "echo Script $_file running" 29
923 set $_arg; . $_file )
931 # load_rc_config name
932 # Source in the configuration file for a given name.
937 if [ -z "$_name" ]; then
938 err 3 'USAGE: load_rc_config name'
941 if ${_rc_conf_loaded:-false}; then
944 if [ -r /etc/defaults/rc.conf ]; then
945 debug "Sourcing /etc/defaults/rc.conf"
946 . /etc/defaults/rc.conf
948 elif [ -r /etc/rc.conf ]; then
949 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
954 if [ -f /etc/rc.conf.d/"$_name" ]; then
955 debug "Sourcing /etc/rc.conf.d/${_name}"
956 . /etc/rc.conf.d/"$_name"
959 # Old variable names support
961 [ -n "$enable_quotas" ] && quota_enable="$enable_quotas"
965 # load_rc_config_var name var
966 # Read the rc.conf(5) var for name and set in the
967 # current shell, using load_rc_config in a subshell to prevent
968 # unwanted side effects from other variable assignments.
972 if [ $# -ne 2 ]; then
973 err 3 'USAGE: load_rc_config_var name var'
976 load_rc_config '$1' >/dev/null;
977 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
978 echo '$2'=\'\''${'$2'}\'\'';
985 # Print a usage string for $0, with `commands' being a list of
990 echo -n 1>&2 "Usage: $0 [fast|force|one]("
994 echo -n 1>&2 "$_sep$_elem"
1002 # err exitval message
1003 # Display message to stderr and log to the syslog, and exit with exitval.
1010 if [ -x /usr/bin/logger ]; then
1011 logger "$0: ERROR: $*"
1013 echo 1>&2 "$0: ERROR: $*"
1019 # Display message to stderr and log to the syslog.
1023 if [ -x /usr/bin/logger ]; then
1024 logger "$0: WARNING: $*"
1026 echo 1>&2 "$0: WARNING: $*"
1031 # Display informational message to stdout and log to syslog.
1036 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1037 if [ -x /usr/bin/logger ]; then
1038 logger "$0: INFO: $*"
1047 # If debugging is enabled in rc.conf output message to stderr.
1048 # BEWARE that you don't call any subroutine that itself calls this
1054 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1055 if [ -x /usr/bin/logger ]; then
1056 logger "$0: DEBUG: $*"
1058 echo 1>&2 "$0: DEBUG: $*"
1064 # backup_file action file cur backup
1065 # Make a backup copy of `file' into `cur', and save the previous
1066 # version of `cur' as `backup' or use rcs for archiving.
1068 # This routine checks the value of the backup_uses_rcs variable,
1069 # which can be either YES or NO.
1071 # The `action' keyword can be one of the following:
1073 # add `file' is now being backed up (and is possibly
1074 # being reentered into the backups system). `cur'
1075 # is created and RCS files, if necessary, are
1078 # update `file' has changed and needs to be backed up.
1079 # If `cur' exists, it is copied to to `back' or
1080 # checked into RCS (if the repository file is old),
1081 # and then `file' is copied to `cur'. Another RCS
1082 # check in done here if RCS is being used.
1084 # remove `file' is no longer being tracked by the backups
1085 # system. If RCS is not being used, `cur' is moved
1086 # to `back', otherwise an empty file is checked in,
1087 # and then `cur' is removed.
1097 if checkyesno backup_uses_rcs; then
1098 _msg0="backup archive"
1101 # ensure that history file is not locked
1102 if [ -f $_cur,v ]; then
1103 rcs -q -u -U -M $_cur
1106 # ensure after switching to rcs that the
1107 # current backup is not lost
1108 if [ -f $_cur ]; then
1109 # no archive, or current newer than archive
1110 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1111 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1120 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1123 chown root:wheel $_cur $_cur,v
1127 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1129 chown root:wheel $_cur $_cur,v
1136 if [ -f $_cur ]; then
1140 chown root:wheel $_cur
1149 # make_symlink src link
1150 # Make a symbolic link 'link' to src from basedir. If the
1151 # directory in which link is to be created does not exist
1152 # a warning will be displayed and an error will be returned.
1153 # Returns 0 on sucess, 1 otherwise.
1157 local src link linkdir _me
1160 linkdir="`dirname $link`"
1161 _me="make_symlink()"
1163 if [ -z "$src" -o -z "$link" ]; then
1164 warn "$_me: requires two arguments."
1167 if [ ! -d "$linkdir" ]; then
1168 warn "$_me: the directory $linkdir does not exist."
1171 if ! ln -sf $src $link; then
1172 warn "$_me: unable to make a symbolic link from $link to $src"
1178 # devfs_rulesets_from_file file
1179 # Reads a set of devfs commands from file, and creates
1180 # the specified rulesets with their rules. Returns non-zero
1181 # if there was an error.
1183 devfs_rulesets_from_file()
1187 _me="devfs_rulesets_from_file"
1190 if [ -z "$file" ]; then
1191 warn "$_me: you must specify a file"
1194 if [ ! -e "$file" ]; then
1195 debug "$_me: no such file ($file)"
1198 debug "reading rulesets from file ($file)"
1206 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1207 if [ -z "$rulenum" ]; then
1208 warn "$_me: cannot extract rule number ($line)"
1212 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1213 if [ -z "$rulename" ]; then
1214 warn "$_me: cannot extract rule name ($line)"
1218 eval $rulename=\$rulenum
1219 debug "found ruleset: $rulename=$rulenum"
1220 if ! /sbin/devfs rule -s $rulenum delset; then
1226 rulecmd="${line%%"\#*"}"
1227 # evaluate the command incase it includes
1229 if [ -n "$rulecmd" ]; then
1230 debug "adding rule ($rulecmd)"
1231 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1239 if [ $_err -ne 0 ]; then
1240 debug "error in $_me"
1247 # devfs_init_rulesets
1248 # Initializes rulesets from configuration files. Returns
1249 # non-zero if there was an error.
1251 devfs_init_rulesets()
1254 _me="devfs_init_rulesets"
1256 # Go through this only once
1257 if [ -n "$devfs_rulesets_init" ]; then
1258 debug "$_me: devfs rulesets already initialized"
1261 for file in $devfs_rulesets; do
1262 devfs_rulesets_from_file $file || return 1
1264 devfs_rulesets_init=1
1265 debug "$_me: devfs rulesets initialized"
1269 # devfs_set_ruleset ruleset [dir]
1270 # Sets the default ruleset of dir to ruleset. The ruleset argument
1271 # must be a ruleset name as specified in devfs.rules(5) file.
1272 # Returns non-zero if it could not set it successfully.
1277 [ -n "$1" ] && eval rs=\$$1 || rs=
1278 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1279 _me="devfs_set_ruleset"
1281 if [ -z "$rs" ]; then
1282 warn "$_me: you must specify a ruleset number"
1285 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1286 if ! /sbin/devfs $devdir ruleset $rs; then
1287 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1293 # devfs_apply_ruleset ruleset [dir]
1294 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1295 # The ruleset argument must be a ruleset name as specified
1296 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1297 # if it could not apply the ruleset.
1299 devfs_apply_ruleset()
1302 [ -n "$1" ] && eval rs=\$$1 || rs=
1303 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1304 _me="devfs_apply_ruleset"
1306 if [ -z "$rs" ]; then
1307 warn "$_me: you must specify a ruleset"
1310 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1311 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1312 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1318 # devfs_domount dir [ruleset]
1319 # Mount devfs on dir. If ruleset is specified it is set
1320 # on the mount-point. It must also be a ruleset name as specified
1321 # in a devfs.rules(5) file. Returns 0 on success.
1327 [ -n "$2" ] && rs=$2 || rs=
1328 _me="devfs_domount()"
1330 if [ -z "$devdir" ]; then
1331 warn "$_me: you must specify a mount-point"
1334 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1335 if ! mount -t devfs dev "$devdir"; then
1336 warn "$_me: Unable to mount devfs on $devdir"
1339 if [ -n "$rs" ]; then
1341 devfs_set_ruleset $rs $devdir
1342 devfs -m $devdir rule applyset
1347 # devfs_mount_jail dir [ruleset]
1348 # Mounts a devfs file system appropriate for jails
1349 # on the directory dir. If ruleset is specified, the ruleset
1350 # it names will be used instead. If present, ruleset must
1351 # be the name of a ruleset as defined in a devfs.rules(5) file.
1352 # This function returns non-zero if an error occurs.
1358 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1359 _me="devfs_mount_jail"
1362 if ! devfs_domount "$jdev" $rs; then
1363 warn "$_me: devfs was not mounted on $jdev"
1369 # Provide a function for normalizing the mounting of memory
1370 # filesystems. This should allow the rest of the code here to remain
1371 # as close as possible between 5-current and 4-stable.
1374 # $3 = (optional) extra mdmfs flags
1377 if [ -n "$3" ]; then
1380 /sbin/mdmfs $flags -s $1 md $2
1383 # Code common to scripts that need to load a kernel module
1384 # if it isn't in the kernel yet. Syntax:
1385 # load_kld [-e regex] [-m module] file
1386 # where -e or -m chooses the way to check if the module
1387 # is already loaded:
1388 # regex is egrep'd in the output from `kldstat -v',
1389 # module is passed to `kldstat -m'.
1390 # The default way is as though `-m file' were specified.
1393 local _loaded _mod _opt _re
1395 while getopts "e:m:" _opt; do
1398 m) _mod="$OPTARG" ;;
1399 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1402 shift $(($OPTIND - 1))
1403 if [ $# -ne 1 ]; then
1404 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1408 if [ -n "$_re" ]; then
1409 if kldstat -v | egrep -q -e "$_re"; then
1413 if kldstat -q -m "$_mod"; then
1418 if ! kldload "$1"; then
1419 warn "Unable to load kernel module $1"
1422 info "$1 kernel module loaded."
1425 debug "load_kld: $1 kernel module already loaded."
1431 # Change every $src in $str to $dst.
1432 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1436 local _str _src _dst _out _com
1443 for _com in ${_str}; do
1444 if [ -z "${_out}" ]; then
1447 _out="${_out}${_dst}${_com}"
1453 # Creates a list of providers for GELI encryption.
1456 local devices devices2
1457 local provider mountpoint type options rest
1459 # Create list of GELI providers from fstab.
1460 while read provider mountpoint type options rest ; do
1461 case ":${options}" in
1470 case ":${provider}" in
1475 # Skip swap devices.
1476 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1479 devices="${devices} ${provider}"
1484 # Append providers from geli_devices.
1485 devices="${devices} ${geli_devices}"
1487 for provider in ${devices}; do
1488 provider=${provider%.eli}
1489 provider=${provider#/dev/}
1490 devices2="${devices2} ${provider}"
1496 # Find scripts in local_startup directories that use the old syntax
1498 find_local_scripts_old () {
1501 for dir in ${local_startup}; do
1502 if [ -d "${dir}" ]; then
1503 for file in ${dir}/[0-9]*.sh; do
1504 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1506 zlist="$zlist $file"
1508 for file in ${dir}/[^0-9]*.sh; do
1509 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1511 slist="$slist $file"
1517 find_local_scripts_new () {
1519 for dir in ${local_startup}; do
1520 if [ -d "${dir}" ]; then
1521 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1524 *) if [ -x "$file" ]; then
1525 local_rc="${local_rc} ${file}"
1534 # check_required_{before|after} command
1535 # Check for things required by the command before and after its precmd,
1536 # respectively. The two separate functions are needed because some
1537 # conditions should prevent precmd from being run while other things
1538 # depend on precmd having already been run.
1540 check_required_before()
1546 for _f in $required_vars; do
1547 if ! checkyesno $_f; then
1548 warn "\$${_f} is not enabled."
1549 if [ -z "$rc_force" ]; then
1555 for _f in $required_dirs; do
1556 if [ ! -d "${_f}/." ]; then
1557 warn "${_f} is not a directory."
1558 if [ -z "$rc_force" ]; then
1564 for _f in $required_files; do
1565 if [ ! -r "${_f}" ]; then
1566 warn "${_f} is not readable."
1567 if [ -z "$rc_force" ]; then
1578 check_required_after()
1584 for _f in $required_modules; do
1586 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
1587 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
1590 if ! load_kld ${_args}; then
1591 if [ -z "$rc_force" ]; then