1 # $NetBSD: rc.subr,v 1.66 2006/04/01 10:05:50 he 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)'}
45 # Operating System dependent/independent variables
48 if [ -z "${_rc_subr_loaded}" ]; then
53 SYSCTL_N="${SYSCTL} -n"
54 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
55 OSTYPE=`${CMD_OSTYPE}`
57 IDCMD="if [ -x $ID ]; then $ID -un; fi"
59 JID=`$PS -p $$ -o jid=`
66 SYSCTL_W="${SYSCTL} -w"
76 # Set the variable name enabling a specific service.
77 # FreeBSD uses ${service}_enable, while NetBSD uses
78 # just the name of the service. For example:
79 # FreeBSD: sendmail_enable="YES"
80 # NetBSD : sendmail="YES"
81 # $1 - if $name is not the base to work of off, specify
94 echo ${base_var}_enable
106 # force_depend script
107 # Force a service to start. Intended for use by services
108 # to resolve dependency issues. It is assumed the caller
109 # has check to make sure this call is necessary
110 # $1 - filename of script, in /etc/rc.d, to run
116 info "${name} depends on ${_depend}, which will be forced to start."
117 if ! /etc/rc.d/${_depend} forcestart; then
118 warn "Unable to force ${_depend}. It may already be running."
126 # Test $1 variable, and warn if not set to YES or NO.
127 # Return 0 if it's "yes" (et al), nonzero otherwise.
132 debug "checkyesno: $1 is set to $_value."
135 # "yes", "true", "on", or "1"
136 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
140 # "no", "false", "off", or "0"
141 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
145 warn "\$${1} is not set properly - see ${rcvar_manpage}."
153 # print the list in reverse order
159 _revlist="$_revfile $_revlist"
165 # mount_critical_filesystems type
166 # Go through the list of critical filesystems as provided in
167 # the rc.conf(5) variable $critical_filesystems_${type}, checking
168 # each one to see if it is mounted, and if it is not, mounting it.
170 mount_critical_filesystems()
172 eval _fslist=\$critical_filesystems_${1}
173 for _fs in $_fslist; do
176 while read what _on on _type type; do
177 if [ $on = $_fs ]; then
184 mount $_fs >/dev/null 2>&1
191 # check_pidfile pidfile procname [interpreter]
192 # Parses the first line of pidfile for a PID, and ensures
193 # that the process is running and matches procname.
194 # Prints the matching PID upon success, nothing otherwise.
195 # interpreter is optional; see _find_processes() for details.
202 if [ -z "$_pidfile" -o -z "$_procname" ]; then
203 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
205 if [ ! -f $_pidfile ]; then
206 debug "pid file ($_pidfile): not readable."
209 read _pid _junk < $_pidfile
210 if [ -z "$_pid" ]; then
211 debug "pid file ($_pidfile): no pid in file."
214 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
218 # check_process procname [interpreter]
219 # Ensures that a process (or processes) named procname is running.
220 # Prints a list of matching PIDs.
221 # interpreter is optional; see _find_processes() for details.
227 if [ -z "$_procname" ]; then
228 err 3 'USAGE: check_process procname [interpreter]'
230 _find_processes $_procname ${_interpreter:-.} '-ax'
234 # _find_processes procname interpreter psargs
235 # Search for procname in the output of ps generated by psargs.
236 # Prints the PIDs of any matching processes, space separated.
238 # If interpreter == ".", check the following variations of procname
239 # against the first word of each command:
241 # `basename procname`
242 # `basename procname` + ":"
243 # "(" + `basename procname` + ")"
244 # "[" + `basename procname` + "]"
246 # If interpreter != ".", read the first line of procname, remove the
247 # leading #!, normalise whitespace, append procname, and attempt to
248 # match that against each command, either as is, or with extra words
249 # at the end. As an alternative, to deal with interpreted daemons
250 # using perl, the basename of the interpreter plus a colon is also
251 # tried as the prefix to procname.
255 if [ $# -ne 3 ]; then
256 err 3 'USAGE: _find_processes procname interpreter psargs'
263 if [ $_interpreter != "." ]; then # an interpreted script
264 read _interp < $_procname # read interpreter name
265 _interp=${_interp#\#!} # strip #!
267 if [ $_interpreter != $1 ]; then
268 warn "\$command_interpreter $_interpreter != $1"
270 _interp="$* $_procname" # cleanup spaces, add _procname
273 _fp_match='case "$_argv" in
274 ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
275 else # a normal daemon
276 _procnamebn=${_procname##*/}
277 _fp_args='_arg0 _argv'
278 _fp_match='case "$_arg0" in
279 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
283 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
284 while read _npid _jid '"$_fp_args"'; do
286 if [ "$JID" -eq "$_jid" ];
287 then echo -n "$_pref$_npid";
294 # debug "in _find_processes: proccheck is ($_proccheck)."
299 # wait_for_pids pid [pid ...]
300 # spins until none of the pids exist
305 if [ -z "$_list" ]; then
312 if kill -0 $_j 2>/dev/null; then
313 _nlist="${_nlist}${_nlist:+ }$_j"
316 if [ -z "$_nlist" ]; then
320 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
324 if [ -n "$_prefix" ]; then
330 # run_rc_command argument
331 # Search for argument in the list of supported commands, which is:
332 # "start stop restart rcvar status poll ${extra_commands}"
333 # If there's a match, run ${argument}_cmd or the default method
336 # If argument has a given prefix, then change the operation as follows:
339 # fast Skip the pid check, and set rc_fast=yes
340 # force Set ${rcvar} to YES, and set rc_force=yes
341 # one Set ${rcvar} to YES
343 # The following globals are used:
345 # Name Needed Purpose
346 # ---- ------ -------
347 # name y Name of script.
349 # command n Full path to command.
350 # Not needed if ${rc_arg}_cmd is set for
353 # command_args n Optional args/shell directives for command.
355 # command_interpreter n If not empty, command is interpreted, so
356 # call check_{pidfile,process}() appropriately.
358 # extra_commands n List of extra commands supported.
360 # pidfile n If set, use check_pidfile $pidfile $command,
361 # otherwise use check_process $command.
362 # In either case, only check if $command is set.
364 # procname n Process name to check for instead of $command.
366 # rcvar n This is checked with checkyesno to determine
367 # if the action should be run.
369 # ${name}_program n Full path to command.
370 # Meant to be used in /etc/rc.conf to override
373 # ${name}_chroot n Directory to chroot to before running ${command}
374 # Requires /usr to be mounted.
376 # ${name}_chdir n Directory to cd to before running ${command}
377 # (if not using ${name}_chroot).
379 # ${name}_flags n Arguments to call ${command} with.
380 # NOTE: $flags from the parent environment
381 # can be used to override this.
383 # ${name}_nice n Nice level to run ${command} at.
385 # ${name}_user n User to run ${command} as, using su(1) if not
386 # using ${name}_chroot.
387 # Requires /usr to be mounted.
389 # ${name}_group n Group to run chrooted ${command} as.
390 # Requires /usr to be mounted.
392 # ${name}_groups n Comma separated list of supplementary groups
393 # to run the chrooted ${command} with.
394 # Requires /usr to be mounted.
396 # ${rc_arg}_cmd n If set, use this as the method when invoked;
397 # Otherwise, use default command (see below)
399 # ${rc_arg}_precmd n If set, run just before performing the
400 # ${rc_arg}_cmd method in the default
401 # operation (i.e, after checking for required
402 # bits and process (non)existence).
403 # If this completes with a non-zero exit code,
404 # don't run ${rc_arg}_cmd.
406 # ${rc_arg}_postcmd n If set, run just after performing the
407 # ${rc_arg}_cmd method, if that method
408 # returned a zero exit code.
410 # required_dirs n If set, check for the existence of the given
411 # directories before running the default
414 # required_files n If set, check for the readability of the given
415 # files before running the default (re)start
418 # required_vars n If set, perform checkyesno on each of the
419 # listed variables before running the default
422 # Default behaviour for a given argument, if no override method is
425 # Argument Default behaviour
426 # -------- -----------------
427 # start if !running && checkyesno ${rcvar}
431 # rc_pid=$(check_pidfile $pidfile $command)
433 # rc_pid=$(check_process $command)
434 # kill $sig_stop $rc_pid
435 # wait_for_pids $rc_pid
436 # ($sig_stop defaults to TERM.)
438 # reload Similar to stop, except use $sig_reload instead,
439 # and doesn't wait_for_pids.
440 # $sig_reload defaults to HUP.
441 # Note that `reload' isn't provided by default,
442 # it should be enabled via $extra_commands.
444 # restart Run `stop' then `start'.
446 # status Show if ${command} is running, etc.
448 # poll Wait for ${command} to exit.
450 # rcvar Display what rc.conf variable is used (if any).
452 # Variables available to methods, and after run_rc_command() has
457 # rc_arg Argument to command, after fast/force/one processing
460 # rc_flags Flags to start the default command with.
461 # Defaults to ${name}_flags, unless overridden
462 # by $flags from the environment.
463 # This variable may be changed by the precmd method.
465 # rc_pid PID of command (if appropriate)
467 # rc_fast Not empty if "fast" was provided (q.v.)
469 # rc_force Not empty if "force" was provided (q.v.)
476 if [ -z "$name" ]; then
477 err 3 'run_rc_command: $name is not set.'
480 # Don't repeat the first argument when passing additional command-
481 # line arguments to the command subroutines.
488 fast*) # "fast" prefix; don't check pid
489 rc_arg=${rc_arg#fast}
492 force*) # "force prefix; always run
495 rc_arg=${rc_arg#${_rc_prefix}}
496 if [ -n "${rcvar}" ]; then
500 one*) # "one" prefix; set ${rcvar}=yes
502 rc_arg=${rc_arg#${_rc_prefix}}
503 if [ -n "${rcvar}" ]; then
509 eval _override_command=\$${name}_program
510 command=${command:+${_override_command:-$command}}
512 _keywords="start stop restart rcvar $extra_commands"
515 _procname=${procname:-${command}}
517 # setup pid check command
518 if [ -n "$_procname" ]; then
519 if [ -n "$pidfile" ]; then
520 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
522 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
524 if [ -n "$_pidcmd" ]; then
525 _keywords="${_keywords} status poll"
529 if [ -z "$rc_arg" ]; then
533 if [ -n "$flags" ]; then # allow override from environment
536 eval rc_flags=\$${name}_flags
538 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
539 _nice=\$${name}_nice _user=\$${name}_user \
540 _group=\$${name}_group _groups=\$${name}_groups
542 if [ -n "$_user" ]; then # unset $_user if running as that user
543 if [ "$_user" = "$(eval $IDCMD)" ]; then
548 # if ${rcvar} is set, and $1 is not
550 # checkyesno ${rcvar}
551 # and return if that failed
553 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
554 if ! checkyesno ${rcvar}; then
559 eval $_pidcmd # determine the pid if necessary
561 for _elem in $_keywords; do
562 if [ "$_elem" != "$rc_arg" ]; then
566 # if there's a custom ${XXX_cmd},
567 # run that instead of the default
569 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
570 _postcmd=\$${rc_arg}_postcmd
571 if [ -n "$_cmd" ]; then
572 # if the precmd failed and force
575 if [ -n "$_precmd" ]; then
576 debug "run_rc_command: evaluating ${_precmd}()."
577 eval $_precmd $rc_extra_args
579 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
583 if [ -n "$_cmd" ]; then
584 debug "run_rc_command: evaluating ${_cmd}()."
585 eval $_cmd $rc_extra_args
587 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
591 if [ -n "$_postcmd" ]; then
592 debug "run_rc_command: evaluating ${_postcmd}()."
593 eval $_postcmd $rc_extra_args
599 case "$rc_arg" in # default operations...
602 if [ -n "$rc_pid" ]; then
603 echo "${name} is running as pid $rc_pid."
605 echo "${name} is not running."
611 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
612 echo 1>&2 "${name} already running? (pid=$rc_pid)."
616 if [ ! -x ${_chroot}${command} ]; then
617 warn "run_rc_command: cannot run $command"
621 # check for required variables,
622 # directories, and files
624 for _f in $required_vars; do
625 if ! checkyesno $_f; then
626 warn "\$${_f} is not enabled."
627 if [ -z "$rc_force" ]; then
632 for _f in $required_dirs; do
633 if [ ! -d "${_f}/." ]; then
634 warn "${_f} is not a directory."
635 if [ -z "$rc_force" ]; then
640 for _f in $required_files; do
641 if [ ! -r "${_f}" ]; then
642 warn "${_f} is not readable."
643 if [ -z "$rc_force" ]; then
649 # if the precmd failed and force
652 if [ -n "${_precmd}" ]; then
653 debug "run_rc_command: evaluating ${_precmd}()."
656 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
660 # setup the full command to run
662 echo "Starting ${name}."
663 if [ -n "$_chroot" ]; then
665 ${_nice:+nice -n $_nice }\
666 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
667 $_chroot $command $rc_flags $command_args"
670 ${_chdir:+cd $_chdir && }\
671 $command $rc_flags $command_args"
672 if [ -n "$_user" ]; then
673 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
675 if [ -n "$_nice" ]; then
676 if [ -z "$_user" ]; then
677 _doit="sh -c \"$_doit\""
679 _doit="nice -n $_nice $_doit"
683 # run the full command;
684 # if the cmd failed and force
687 debug "run_rc_command: _doit: $_doit"
690 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
692 # finally, run postcmd
694 if [ -n "${_postcmd}" ]; then
695 debug "run_rc_command: evaluating ${_postcmd}()."
701 if [ -z "$rc_pid" ]; then
702 [ -n "$rc_fast" ] && return 0
703 if [ -n "$pidfile" ]; then
705 "${name} not running? (check $pidfile)."
707 echo 1>&2 "${name} not running?"
712 # if the precmd failed and force
715 if [ -n "$_precmd" ]; then
718 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
722 # send the signal to stop
724 echo "Stopping ${name}."
725 _doit="kill -${sig_stop:-TERM} $rc_pid"
726 if [ -n "$_user" ]; then
727 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
730 # if the stop cmd failed and force
735 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
737 # wait for the command to exit,
739 wait_for_pids $rc_pid
740 if [ -n "$_postcmd" ]; then
747 if [ -z "$rc_pid" ]; then
748 if [ -n "$pidfile" ]; then
750 "${name} not running? (check $pidfile)."
752 echo 1>&2 "${name} not running?"
756 echo "Reloading ${name} config files."
757 if [ -n "$_precmd" ]; then
760 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
763 _doit="kill -${sig_reload:-HUP} $rc_pid"
764 if [ -n "$_user" ]; then
765 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
769 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
770 if [ -n "$_postcmd" ]; then
777 if [ -n "$_precmd" ]; then
778 eval $_precmd $rc_extra_args
780 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
783 # prevent restart being called more
784 # than once by any given script
786 if ${_rc_restart_done:-false}; then
789 _rc_restart_done=true
791 # run stop in a subshell to keep variables for start
792 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
793 run_rc_command ${_rc_prefix}start $rc_extra_args
795 if [ -n "$_postcmd" ]; then
796 eval $_postcmd $rc_extra_args
802 if [ -n "$rc_pid" ]; then
803 wait_for_pids $rc_pid
809 if [ -n "$rcvar" ]; then
810 if checkyesno ${rcvar}; then
811 echo "\$${rcvar}=YES"
826 echo 1>&2 "$0: unknown directive '$rc_arg'."
832 # run_rc_script file arg
833 # Start the script `file' with `arg', and correctly handle the
834 # return value from the script. If `file' ends with `.sh', it's
835 # sourced into the current environment. If `file' appears to be
836 # a backup or scratch file, ignore it. Otherwise if it's
837 # executable run as a child process.
843 if [ -z "$_file" -o -z "$_arg" ]; then
844 err 3 'USAGE: run_rc_script file arg'
847 unset name command command_args command_interpreter \
848 extra_commands pidfile procname \
849 rcvar required_dirs required_files required_vars
850 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
853 /etc/rc.d/*.sh) # run in current shell
856 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
857 warn "Ignoring scratch file $_file"
860 if [ -x $_file ]; then
861 if [ -n "$rc_fast_and_loose" ]; then
864 ( trap "echo Script $_file interrupted; kill -QUIT $$" 3
865 trap "echo Script $_file interrupted; exit 1" 2
866 set $_arg; . $_file )
874 # load_rc_config name
875 # Source in the configuration file for a given name.
880 if [ -z "$_name" ]; then
881 err 3 'USAGE: load_rc_config name'
884 if ${_rc_conf_loaded:-false}; then
887 if [ -r /etc/defaults/rc.conf ]; then
888 debug "Sourcing /etc/defaults/rc.conf"
889 . /etc/defaults/rc.conf
891 elif [ -r /etc/rc.conf ]; then
892 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
897 if [ -f /etc/rc.conf.d/"$_name" ]; then
898 debug "Sourcing /etc/rc.conf.d/${_name}"
899 . /etc/rc.conf.d/"$_name"
902 # XXX - Deprecated variable name support
906 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
907 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
908 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
909 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
910 [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
911 [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
912 [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
913 [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
914 [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
920 # load_rc_config_var name var
921 # Read the rc.conf(5) var for name and set in the
922 # current shell, using load_rc_config in a subshell to prevent
923 # unwanted side effects from other variable assignments.
927 if [ $# -ne 2 ]; then
928 err 3 'USAGE: load_rc_config_var name var'
931 load_rc_config '$1' >/dev/null;
932 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
933 echo '$2'=\'\''${'$2'}\'\'';
940 # Print a usage string for $0, with `commands' being a list of
945 echo -n 1>&2 "Usage: $0 [fast|force|one]("
949 echo -n 1>&2 "$_sep$_elem"
957 # err exitval message
958 # Display message to stderr and log to the syslog, and exit with exitval.
965 if [ -x /usr/bin/logger ]; then
966 logger "$0: ERROR: $*"
968 echo 1>&2 "$0: ERROR: $*"
974 # Display message to stderr and log to the syslog.
978 if [ -x /usr/bin/logger ]; then
979 logger "$0: WARNING: $*"
981 echo 1>&2 "$0: WARNING: $*"
986 # Display informational message to stdout and log to syslog.
991 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
992 if [ -x /usr/bin/logger ]; then
993 logger "$0: INFO: $*"
1002 # If debugging is enabled in rc.conf output message to stderr.
1003 # BEWARE that you don't call any subroutine that itself calls this
1009 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1010 if [ -x /usr/bin/logger ]; then
1011 logger "$0: DEBUG: $*"
1013 echo 1>&2 "$0: DEBUG: $*"
1019 # backup_file action file cur backup
1020 # Make a backup copy of `file' into `cur', and save the previous
1021 # version of `cur' as `backup' or use rcs for archiving.
1023 # This routine checks the value of the backup_uses_rcs variable,
1024 # which can be either YES or NO.
1026 # The `action' keyword can be one of the following:
1028 # add `file' is now being backed up (and is possibly
1029 # being reentered into the backups system). `cur'
1030 # is created and RCS files, if necessary, are
1033 # update `file' has changed and needs to be backed up.
1034 # If `cur' exists, it is copied to to `back' or
1035 # checked into RCS (if the repository file is old),
1036 # and then `file' is copied to `cur'. Another RCS
1037 # check in done here if RCS is being used.
1039 # remove `file' is no longer being tracked by the backups
1040 # system. If RCS is not being used, `cur' is moved
1041 # to `back', otherwise an empty file is checked in,
1042 # and then `cur' is removed.
1052 if checkyesno backup_uses_rcs; then
1053 _msg0="backup archive"
1056 # ensure that history file is not locked
1057 if [ -f $_cur,v ]; then
1058 rcs -q -u -U -M $_cur
1061 # ensure after switching to rcs that the
1062 # current backup is not lost
1063 if [ -f $_cur ]; then
1064 # no archive, or current newer than archive
1065 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1066 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1075 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1078 chown root:wheel $_cur $_cur,v
1082 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1084 chown root:wheel $_cur $_cur,v
1091 if [ -f $_cur ]; then
1095 chown root:wheel $_cur
1104 # make_symlink src link
1105 # Make a symbolic link 'link' to src from basedir. If the
1106 # directory in which link is to be created does not exist
1107 # a warning will be displayed and an error will be returned.
1108 # Returns 0 on sucess, 1 otherwise.
1112 local src link linkdir _me
1115 linkdir="`dirname $link`"
1116 _me="make_symlink()"
1118 if [ -z "$src" -o -z "$link" ]; then
1119 warn "$_me: requires two arguments."
1122 if [ ! -d "$linkdir" ]; then
1123 warn "$_me: the directory $linkdir does not exist."
1126 if ! ln -sf $src $link; then
1127 warn "$_me: unable to make a symbolic link from $link to $src"
1133 # devfs_rulesets_from_file file
1134 # Reads a set of devfs commands from file, and creates
1135 # the specified rulesets with their rules. Returns non-zero
1136 # if there was an error.
1138 devfs_rulesets_from_file()
1142 _me="devfs_rulesets_from_file"
1145 if [ -z "$file" ]; then
1146 warn "$_me: you must specify a file"
1149 if [ ! -e "$file" ]; then
1150 debug "$_me: no such file ($file)"
1153 debug "reading rulesets from file ($file)"
1161 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1162 if [ -z "$rulenum" ]; then
1163 warn "$_me: cannot extract rule number ($line)"
1167 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1168 if [ -z "$rulename" ]; then
1169 warn "$_me: cannot extract rule name ($line)"
1173 eval $rulename=\$rulenum
1174 debug "found ruleset: $rulename=$rulenum"
1175 if ! /sbin/devfs rule -s $rulenum delset; then
1181 rulecmd="${line%%"\#*"}"
1182 # evaluate the command incase it includes
1184 if [ -n "$rulecmd" ]; then
1185 debug "adding rule ($rulecmd)"
1186 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1194 if [ $_err -ne 0 ]; then
1195 debug "error in $_me"
1202 # devfs_init_rulesets
1203 # Initializes rulesets from configuration files. Returns
1204 # non-zero if there was an error.
1206 devfs_init_rulesets()
1209 _me="devfs_init_rulesets"
1211 # Go through this only once
1212 if [ -n "$devfs_rulesets_init" ]; then
1213 debug "$_me: devfs rulesets already initialized"
1216 for file in $devfs_rulesets; do
1217 devfs_rulesets_from_file $file || return 1
1219 devfs_rulesets_init=1
1220 debug "$_me: devfs rulesets initialized"
1224 # devfs_set_ruleset ruleset [dir]
1225 # Sets the default ruleset of dir to ruleset. The ruleset argument
1226 # must be a ruleset name as specified in devfs.rules(5) file.
1227 # Returns non-zero if it could not set it successfully.
1232 [ -n "$1" ] && eval rs=\$$1 || rs=
1233 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1234 _me="devfs_set_ruleset"
1236 if [ -z "$rs" ]; then
1237 warn "$_me: you must specify a ruleset number"
1240 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1241 if ! /sbin/devfs $devdir ruleset $rs; then
1242 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1248 # devfs_apply_ruleset ruleset [dir]
1249 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1250 # The ruleset argument must be a ruleset name as specified
1251 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1252 # if it could not apply the ruleset.
1254 devfs_apply_ruleset()
1257 [ -n "$1" ] && eval rs=\$$1 || rs=
1258 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1259 _me="devfs_apply_ruleset"
1261 if [ -z "$rs" ]; then
1262 warn "$_me: you must specify a ruleset"
1265 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1266 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1267 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1273 # devfs_domount dir [ruleset]
1274 # Mount devfs on dir. If ruleset is specified it is set
1275 # on the mount-point. It must also be a ruleset name as specified
1276 # in a devfs.rules(5) file. Returns 0 on success.
1282 [ -n "$2" ] && rs=$2 || rs=
1283 _me="devfs_domount()"
1285 if [ -z "$devdir" ]; then
1286 warn "$_me: you must specify a mount-point"
1289 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1290 if ! mount -t devfs dev "$devdir"; then
1291 warn "$_me: Unable to mount devfs on $devdir"
1294 if [ -n "$rs" ]; then
1296 devfs_set_ruleset $rs $devdir
1297 devfs -m $devdir rule applyset
1302 # devfs_mount_jail dir [ruleset]
1303 # Mounts a devfs file system appropriate for jails
1304 # on the directory dir. If ruleset is specified, the ruleset
1305 # it names will be used instead. If present, ruleset must
1306 # be the name of a ruleset as defined in a devfs.rules(5) file.
1307 # This function returns non-zero if an error occurs.
1313 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1314 _me="devfs_mount_jail"
1317 if ! devfs_domount "$jdev" $rs; then
1318 warn "$_me: devfs was not mounted on $jdev"
1324 # Provide a function for normalizing the mounting of memory
1325 # filesystems. This should allow the rest of the code here to remain
1326 # as close as possible between 5-current and 4-stable.
1329 # $3 = (optional) extra mdmfs flags
1332 if [ -n "$3" ]; then
1335 /sbin/mdmfs $flags -s $1 md $2
1338 # Code common to scripts that need to load a kernel module
1339 # if it isn't in the kernel yet. Syntax:
1340 # load_kld [-e regex] [-m module] file
1341 # where -e or -m chooses the way to check if the module
1342 # is already loaded:
1343 # regex is egrep'd in the output from `kldstat -v',
1344 # module is passed to `kldstat -m'.
1345 # The default way is as though `-m file' were specified.
1348 local _loaded _mod _opt _re
1350 while getopts "e:m:" _opt; do
1353 m) _mod="$OPTARG" ;;
1354 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1357 shift $(($OPTIND - 1))
1358 if [ $# -ne 1 ]; then
1359 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1363 if [ -n "$_re" ]; then
1364 if kldstat -v | egrep -q -e "$_re"; then
1368 if kldstat -q -m "$_mod"; then
1373 if ! kldload "$1"; then
1374 warn "Unable to load kernel module $1"
1377 info "$1 kernel module loaded."
1380 debug "load_kld: $1 kernel module already loaded."
1386 # Change every $src in $str to $dst.
1387 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1391 local _str _src _dst _out _com
1398 for _com in ${_str}; do
1399 if [ -z "${_out}" ]; then
1402 _out="${_out}${_dst}${_com}"
1408 # Creates a list of providers for GELI encryption.
1411 local devices devices2
1412 local provider mountpoint type options rest
1414 # Create list of GELI providers from fstab.
1415 while read provider mountpoint type options rest ; do
1416 case ":${options}" in
1425 case ":${provider}" in
1430 # Skip swap devices.
1431 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1434 devices="${devices} ${provider}"
1439 # Append providers from geli_devices.
1440 devices="${devices} ${geli_devices}"
1442 for provider in ${devices}; do
1443 provider=${provider%.eli}
1444 provider=${provider#/dev/}
1445 devices2="${devices2} ${provider}"
1451 # Find scripts in local_startup directories that use the old syntax
1453 find_local_scripts_old () {
1456 for dir in ${local_startup}; do
1457 if [ -d "${dir}" ]; then
1458 for file in ${dir}/[0-9]*.sh; do
1459 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1461 zlist="$zlist $file"
1463 for file in ${dir}/[^0-9]*.sh; do
1464 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1466 slist="$slist $file"
1472 find_local_scripts_new () {
1474 for dir in ${local_startup}; do
1475 if [ -d "${dir}" ]; then
1476 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1479 *) if [ -x "$file" ]; then
1480 local_rc="${local_rc} ${file}"