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 JID=`ps -p $$ -o jid=`
58 IDCMD="if [ -x $ID ]; then $ID -un; fi"
65 SYSCTL_W="${SYSCTL} -w"
75 # Set the variable name enabling a specific service.
76 # FreeBSD uses ${service}_enable, while NetBSD uses
77 # just the name of the service. For example:
78 # FreeBSD: sendmail_enable="YES"
79 # NetBSD : sendmail="YES"
80 # $1 - if $name is not the base to work of off, specify
93 echo ${base_var}_enable
105 # force_depend script
106 # Force a service to start. Intended for use by services
107 # to resolve dependency issues. It is assumed the caller
108 # has check to make sure this call is necessary
109 # $1 - filename of script, in /etc/rc.d, to run
115 info "${name} depends on ${_depend}, which will be forced to start."
116 if ! /etc/rc.d/${_depend} forcestart; then
117 warn "Unable to force ${_depend}. It may already be running."
125 # Test $1 variable, and warn if not set to YES or NO.
126 # Return 0 if it's "yes" (et al), nonzero otherwise.
131 debug "checkyesno: $1 is set to $_value."
134 # "yes", "true", "on", or "1"
135 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
139 # "no", "false", "off", or "0"
140 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
144 warn "\$${1} is not set properly - see ${rcvar_manpage}."
152 # print the list in reverse order
158 _revlist="$_revfile $_revlist"
164 # mount_critical_filesystems type
165 # Go through the list of critical filesystems as provided in
166 # the rc.conf(5) variable $critical_filesystems_${type}, checking
167 # each one to see if it is mounted, and if it is not, mounting it.
169 mount_critical_filesystems()
171 eval _fslist=\$critical_filesystems_${1}
172 for _fs in $_fslist; do
175 while read what _on on _type type; do
176 if [ $on = $_fs ]; then
183 mount $_fs >/dev/null 2>&1
190 # check_pidfile pidfile procname [interpreter]
191 # Parses the first line of pidfile for a PID, and ensures
192 # that the process is running and matches procname.
193 # Prints the matching PID upon success, nothing otherwise.
194 # interpreter is optional; see _find_processes() for details.
201 if [ -z "$_pidfile" -o -z "$_procname" ]; then
202 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
204 if [ ! -f $_pidfile ]; then
205 debug "pid file ($_pidfile): not readable."
208 read _pid _junk < $_pidfile
209 if [ -z "$_pid" ]; then
210 debug "pid file ($_pidfile): no pid in file."
213 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
217 # check_process procname [interpreter]
218 # Ensures that a process (or processes) named procname is running.
219 # Prints a list of matching PIDs.
220 # interpreter is optional; see _find_processes() for details.
226 if [ -z "$_procname" ]; then
227 err 3 'USAGE: check_process procname [interpreter]'
229 _find_processes $_procname ${_interpreter:-.} '-ax'
233 # _find_processes procname interpreter psargs
234 # Search for procname in the output of ps generated by psargs.
235 # Prints the PIDs of any matching processes, space separated.
237 # If interpreter == ".", check the following variations of procname
238 # against the first word of each command:
240 # `basename procname`
241 # `basename procname` + ":"
242 # "(" + `basename procname` + ")"
243 # "[" + `basename procname` + "]"
245 # If interpreter != ".", read the first line of procname, remove the
246 # leading #!, normalise whitespace, append procname, and attempt to
247 # match that against each command, either as is, or with extra words
248 # at the end. As an alternative, to deal with interpreted daemons
249 # using perl, the basename of the interpreter plus a colon is also
250 # tried as the prefix to procname.
254 if [ $# -ne 3 ]; then
255 err 3 'USAGE: _find_processes procname interpreter psargs'
262 if [ $_interpreter != "." ]; then # an interpreted script
263 read _interp < $_procname # read interpreter name
264 _interp=${_interp#\#!} # strip #!
266 if [ $_interpreter != $1 ]; then
267 warn "\$command_interpreter $_interpreter != $1"
269 _interp="$* $_procname" # cleanup spaces, add _procname
272 _fp_match='case "$_argv" in
273 ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
274 else # a normal daemon
275 _procnamebn=${_procname##*/}
276 _fp_args='_arg0 _argv'
277 _fp_match='case "$_arg0" in
278 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
282 ps 2>/dev/null -o "pid,jid,command" '"$_psargs"' |
283 while read _npid _jid '"$_fp_args"'; do
288 if [ "$JID" -eq "$_jid" ];
289 then echo -n "$_pref$_npid";
296 # debug "in _find_processes: proccheck is ($_proccheck)."
301 # wait_for_pids pid [pid ...]
302 # spins until none of the pids exist
307 if [ -z "$_list" ]; then
314 if kill -0 $_j 2>/dev/null; then
315 _nlist="${_nlist}${_nlist:+ }$_j"
318 if [ -z "$_nlist" ]; then
322 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
326 if [ -n "$_prefix" ]; then
332 # run_rc_command argument
333 # Search for argument in the list of supported commands, which is:
334 # "start stop restart rcvar status poll ${extra_commands}"
335 # If there's a match, run ${argument}_cmd or the default method
338 # If argument has a given prefix, then change the operation as follows:
341 # fast Skip the pid check, and set rc_fast=yes
342 # force Set ${rcvar} to YES, and set rc_force=yes
343 # one Set ${rcvar} to YES
345 # The following globals are used:
347 # Name Needed Purpose
348 # ---- ------ -------
349 # name y Name of script.
351 # command n Full path to command.
352 # Not needed if ${rc_arg}_cmd is set for
355 # command_args n Optional args/shell directives for command.
357 # command_interpreter n If not empty, command is interpreted, so
358 # call check_{pidfile,process}() appropriately.
360 # extra_commands n List of extra commands supported.
362 # pidfile n If set, use check_pidfile $pidfile $command,
363 # otherwise use check_process $command.
364 # In either case, only check if $command is set.
366 # procname n Process name to check for instead of $command.
368 # rcvar n This is checked with checkyesno to determine
369 # if the action should be run.
371 # ${name}_program n Full path to command.
372 # Meant to be used in /etc/rc.conf to override
375 # ${name}_chroot n Directory to chroot to before running ${command}
376 # Requires /usr to be mounted.
378 # ${name}_chdir n Directory to cd to before running ${command}
379 # (if not using ${name}_chroot).
381 # ${name}_flags n Arguments to call ${command} with.
382 # NOTE: $flags from the parent environment
383 # can be used to override this.
385 # ${name}_nice n Nice level to run ${command} at.
387 # ${name}_user n User to run ${command} as, using su(1) if not
388 # using ${name}_chroot.
389 # Requires /usr to be mounted.
391 # ${name}_group n Group to run chrooted ${command} as.
392 # Requires /usr to be mounted.
394 # ${name}_groups n Comma separated list of supplementary groups
395 # to run the chrooted ${command} with.
396 # Requires /usr to be mounted.
398 # ${rc_arg}_cmd n If set, use this as the method when invoked;
399 # Otherwise, use default command (see below)
401 # ${rc_arg}_precmd n If set, run just before performing the
402 # ${rc_arg}_cmd method in the default
403 # operation (i.e, after checking for required
404 # bits and process (non)existence).
405 # If this completes with a non-zero exit code,
406 # don't run ${rc_arg}_cmd.
408 # ${rc_arg}_postcmd n If set, run just after performing the
409 # ${rc_arg}_cmd method, if that method
410 # returned a zero exit code.
412 # required_dirs n If set, check for the existence of the given
413 # directories before running the default
416 # required_files n If set, check for the readability of the given
417 # files before running the default (re)start
420 # required_vars n If set, perform checkyesno on each of the
421 # listed variables before running the default
424 # Default behaviour for a given argument, if no override method is
427 # Argument Default behaviour
428 # -------- -----------------
429 # start if !running && checkyesno ${rcvar}
433 # rc_pid=$(check_pidfile $pidfile $command)
435 # rc_pid=$(check_process $command)
436 # kill $sig_stop $rc_pid
437 # wait_for_pids $rc_pid
438 # ($sig_stop defaults to TERM.)
440 # reload Similar to stop, except use $sig_reload instead,
441 # and doesn't wait_for_pids.
442 # $sig_reload defaults to HUP.
443 # Note that `reload' isn't provided by default,
444 # it should be enabled via $extra_commands.
446 # restart Run `stop' then `start'.
448 # status Show if ${command} is running, etc.
450 # poll Wait for ${command} to exit.
452 # rcvar Display what rc.conf variable is used (if any).
454 # Variables available to methods, and after run_rc_command() has
459 # rc_arg Argument to command, after fast/force/one processing
462 # rc_flags Flags to start the default command with.
463 # Defaults to ${name}_flags, unless overridden
464 # by $flags from the environment.
465 # This variable may be changed by the precmd method.
467 # rc_pid PID of command (if appropriate)
469 # rc_fast Not empty if "fast" was provided (q.v.)
471 # rc_force Not empty if "force" was provided (q.v.)
478 if [ -z "$name" ]; then
479 err 3 'run_rc_command: $name is not set.'
482 # Don't repeat the first argument when passing additional command-
483 # line arguments to the command subroutines.
490 fast*) # "fast" prefix; don't check pid
491 rc_arg=${rc_arg#fast}
494 force*) # "force prefix; always run
497 rc_arg=${rc_arg#${_rc_prefix}}
498 if [ -n "${rcvar}" ]; then
502 one*) # "one" prefix; set ${rcvar}=yes
504 rc_arg=${rc_arg#${_rc_prefix}}
505 if [ -n "${rcvar}" ]; then
511 _keywords="start stop restart rcvar $extra_commands"
514 _procname=${procname:-${command}}
516 # setup pid check command
517 if [ -n "$_procname" ]; then
518 if [ -n "$pidfile" ]; then
519 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
521 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
523 if [ -n "$_pidcmd" ]; then
524 _keywords="${_keywords} status poll"
528 if [ -z "$rc_arg" ]; then
532 if [ -n "$flags" ]; then # allow override from environment
535 eval rc_flags=\$${name}_flags
537 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
538 _nice=\$${name}_nice _user=\$${name}_user \
539 _group=\$${name}_group _groups=\$${name}_groups
541 if [ -n "$_user" ]; then # unset $_user if running as that user
542 if [ "$_user" = "$(eval $IDCMD)" ]; then
547 # if ${rcvar} is set, and $1 is not
549 # checkyesno ${rcvar}
550 # and return if that failed
552 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
553 if ! checkyesno ${rcvar}; then
558 eval $_pidcmd # determine the pid if necessary
560 for _elem in $_keywords; do
561 if [ "$_elem" != "$rc_arg" ]; then
565 # if there's a custom ${XXX_cmd},
566 # run that instead of the default
568 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
569 _postcmd=\$${rc_arg}_postcmd
570 if [ -n "$_cmd" ]; then
571 # if the precmd failed and force
574 if [ -n "$_precmd" ]; then
575 debug "run_rc_command: evaluating ${_precmd}()."
576 eval $_precmd $rc_extra_args
578 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
582 if [ -n "$_cmd" ]; then
583 debug "run_rc_command: evaluating ${_cmd}()."
584 eval $_cmd $rc_extra_args
586 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
590 if [ -n "$_postcmd" ]; then
591 debug "run_rc_command: evaluating ${_postcmd}()."
592 eval $_postcmd $rc_extra_args
598 case "$rc_arg" in # default operations...
601 if [ -n "$rc_pid" ]; then
602 echo "${name} is running as pid $rc_pid."
604 echo "${name} is not running."
610 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
611 echo 1>&2 "${name} already running? (pid=$rc_pid)."
615 if [ ! -x ${_chroot}${command} ]; then
616 warn "run_rc_command: cannot run $command"
620 # check for required variables,
621 # directories, and files
623 for _f in $required_vars; do
624 if ! checkyesno $_f; then
625 warn "\$${_f} is not enabled."
626 if [ -z "$rc_force" ]; then
631 for _f in $required_dirs; do
632 if [ ! -d "${_f}/." ]; then
633 warn "${_f} is not a directory."
634 if [ -z "$rc_force" ]; then
639 for _f in $required_files; do
640 if [ ! -r "${_f}" ]; then
641 warn "${_f} is not readable."
642 if [ -z "$rc_force" ]; then
648 # if the precmd failed and force
651 if [ -n "${_precmd}" ]; then
652 debug "run_rc_command: evaluating ${_precmd}()."
655 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
659 # setup the full command to run
661 echo "Starting ${name}."
662 if [ -n "$_chroot" ]; then
664 ${_nice:+nice -n $_nice }\
665 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
666 $_chroot $command $rc_flags $command_args"
669 ${_chdir:+cd $_chdir; }\
670 ${_nice:+nice -n $_nice }\
671 $command $rc_flags $command_args"
672 if [ -n "$_user" ]; then
673 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
677 # run the full command;
678 # if the cmd failed and force
681 debug "run_rc_command: _doit: $_doit"
684 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
686 # finally, run postcmd
688 if [ -n "${_postcmd}" ]; then
689 debug "run_rc_command: evaluating ${_postcmd}()."
695 if [ -z "$rc_pid" ]; then
696 [ -n "$rc_fast" ] && return 0
697 if [ -n "$pidfile" ]; then
699 "${name} not running? (check $pidfile)."
701 echo 1>&2 "${name} not running?"
706 # if the precmd failed and force
709 if [ -n "$_precmd" ]; then
712 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
716 # send the signal to stop
718 echo "Stopping ${name}."
719 _doit="kill -${sig_stop:-TERM} $rc_pid"
720 if [ -n "$_user" ]; then
721 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
724 # if the stop cmd failed and force
729 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
731 # wait for the command to exit,
733 wait_for_pids $rc_pid
734 if [ -n "$_postcmd" ]; then
741 if [ -z "$rc_pid" ]; then
742 if [ -n "$pidfile" ]; then
744 "${name} not running? (check $pidfile)."
746 echo 1>&2 "${name} not running?"
750 echo "Reloading ${name} config files."
751 if [ -n "$_precmd" ]; then
754 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
757 _doit="kill -${sig_reload:-HUP} $rc_pid"
758 if [ -n "$_user" ]; then
759 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
763 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
764 if [ -n "$_postcmd" ]; then
771 if [ -n "$_precmd" ]; then
772 eval $_precmd $rc_extra_args
774 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
777 # prevent restart being called more
778 # than once by any given script
780 if ${_rc_restart_done:-false}; then
783 _rc_restart_done=true
785 # run stop in a subshell to keep variables for start
786 ( run_rc_command ${_rc_prefix}stop $rc_extra_args )
787 run_rc_command ${_rc_prefix}start $rc_extra_args
789 if [ -n "$_postcmd" ]; then
790 eval $_postcmd $rc_extra_args
796 if [ -n "$rc_pid" ]; then
797 wait_for_pids $rc_pid
803 if [ -n "$rcvar" ]; then
804 if checkyesno ${rcvar}; then
805 echo "\$${rcvar}=YES"
820 echo 1>&2 "$0: unknown directive '$rc_arg'."
826 # run_rc_script file arg
827 # Start the script `file' with `arg', and correctly handle the
828 # return value from the script. If `file' ends with `.sh', it's
829 # sourced into the current environment. If `file' appears to be
830 # a backup or scratch file, ignore it. Otherwise if it's
831 # executable run as a child process.
837 if [ -z "$_file" -o -z "$_arg" ]; then
838 err 3 'USAGE: run_rc_script file arg'
841 unset name command command_args command_interpreter \
842 extra_commands pidfile procname \
843 rcvar required_dirs required_files required_vars
844 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
847 /etc/rc.d/*.sh) # run in current shell
850 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
851 warn "Ignoring scratch file $_file"
854 if [ -x $_file ]; then
855 if [ -n "$rc_fast_and_loose" ]; then
858 ( trap "echo Script $_file interrupted; kill -QUIT $$" 3
859 trap "echo Script $_file interrupted; exit 1" 2
860 set $_arg; . $_file )
868 # load_rc_config name
869 # Source in the configuration file for a given name.
876 if [ -z "$_name" ]; then
877 err 3 'USAGE: load_rc_config name'
880 if ${_rc_conf_loaded:-false}; then
883 if [ -r /etc/defaults/rc.conf ]; then
884 debug "Sourcing /etc/defaults/rc.conf"
885 . /etc/defaults/rc.conf
887 elif [ -r /etc/rc.conf ]; then
888 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
894 eval _override_command=\$${name}_program
895 command=${command:+${_override_command:-$command}}
897 if [ -z "${command}" ]; then
898 _tmp=`/bin/realpath $0`
899 prefix=${_tmp%/etc/rc.d/*}/
901 prefix=${command%/*bin/*}/
903 if [ "${prefix}" = "/" -o "${prefix}" = "/usr/" ] ; then
906 etcdir="${prefix}etc"
910 if [ -f /etc/rc.conf.d/${_name} -a ${etcdir} != "/etc" ]; then
911 debug "Sourcing /etc/rc.conf.d/${_name}"
912 warn "Warning: /etc/rc.conf.d/${_name} is deprecated, please use ${etcdir}/rc.conf.d/${_name} instead."
913 if [ -f ${etcdir}/rc.conf.d/${_name} ]; then
914 warn "Warning: Both /etc/rc.conf.d/${_name} and ${etcdir}/rc.conf.d/${_name} exist."
916 . /etc/rc.conf.d/${_name}
919 if [ -f ${etcdir}/rc.conf.d/${_name} ]; then
920 debug "Sourcing ${etcdir}/rc.conf.d/${_name}"
921 . ${etcdir}/rc.conf.d/${_name}
924 # XXX - Deprecated variable name support
928 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
929 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
930 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
931 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
932 [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
933 [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
934 [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
935 [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
936 [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
942 # load_rc_config_var name var
943 # Read the rc.conf(5) var for name and set in the
944 # current shell, using load_rc_config in a subshell to prevent
945 # unwanted side effects from other variable assignments.
949 if [ $# -ne 2 ]; then
950 err 3 'USAGE: load_rc_config_var name var'
953 load_rc_config '$1' >/dev/null;
954 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
955 echo '$2'=\'\''${'$2'}\'\'';
962 # Print a usage string for $0, with `commands' being a list of
967 echo -n 1>&2 "Usage: $0 [fast|force|one]("
971 echo -n 1>&2 "$_sep$_elem"
979 # err exitval message
980 # Display message to stderr and log to the syslog, and exit with exitval.
987 if [ -x /usr/bin/logger ]; then
988 logger "$0: ERROR: $*"
990 echo 1>&2 "$0: ERROR: $*"
996 # Display message to stderr and log to the syslog.
1000 if [ -x /usr/bin/logger ]; then
1001 logger "$0: WARNING: $*"
1003 echo 1>&2 "$0: WARNING: $*"
1008 # Display informational message to stdout and log to syslog.
1013 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1014 if [ -x /usr/bin/logger ]; then
1015 logger "$0: INFO: $*"
1024 # If debugging is enabled in rc.conf output message to stderr.
1025 # BEWARE that you don't call any subroutine that itself calls this
1031 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1032 if [ -x /usr/bin/logger ]; then
1033 logger "$0: INFO: $*"
1035 echo 1>&2 "$0: DEBUG: $*"
1041 # backup_file action file cur backup
1042 # Make a backup copy of `file' into `cur', and save the previous
1043 # version of `cur' as `backup' or use rcs for archiving.
1045 # This routine checks the value of the backup_uses_rcs variable,
1046 # which can be either YES or NO.
1048 # The `action' keyword can be one of the following:
1050 # add `file' is now being backed up (and is possibly
1051 # being reentered into the backups system). `cur'
1052 # is created and RCS files, if necessary, are
1055 # update `file' has changed and needs to be backed up.
1056 # If `cur' exists, it is copied to to `back' or
1057 # checked into RCS (if the repository file is old),
1058 # and then `file' is copied to `cur'. Another RCS
1059 # check in done here if RCS is being used.
1061 # remove `file' is no longer being tracked by the backups
1062 # system. If RCS is not being used, `cur' is moved
1063 # to `back', otherwise an empty file is checked in,
1064 # and then `cur' is removed.
1074 if checkyesno backup_uses_rcs; then
1075 _msg0="backup archive"
1078 # ensure that history file is not locked
1079 if [ -f $_cur,v ]; then
1080 rcs -q -u -U -M $_cur
1083 # ensure after switching to rcs that the
1084 # current backup is not lost
1085 if [ -f $_cur ]; then
1086 # no archive, or current newer than archive
1087 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1088 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1097 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1100 chown root:wheel $_cur $_cur,v
1104 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1106 chown root:wheel $_cur $_cur,v
1113 if [ -f $_cur ]; then
1117 chown root:wheel $_cur
1126 # make_symlink src link
1127 # Make a symbolic link 'link' to src from basedir. If the
1128 # directory in which link is to be created does not exist
1129 # a warning will be displayed and an error will be returned.
1130 # Returns 0 on sucess, 1 otherwise.
1134 local src link linkdir _me
1137 linkdir="`dirname $link`"
1138 _me="make_symlink()"
1140 if [ -z "$src" -o -z "$link" ]; then
1141 warn "$_me: requires two arguments."
1144 if [ ! -d "$linkdir" ]; then
1145 warn "$_me: the directory $linkdir does not exist."
1148 if ! ln -sf $src $link; then
1149 warn "$_me: unable to make a symbolic link from $link to $src"
1155 # devfs_rulesets_from_file file
1156 # Reads a set of devfs commands from file, and creates
1157 # the specified rulesets with their rules. Returns non-zero
1158 # if there was an error.
1160 devfs_rulesets_from_file()
1164 _me="devfs_rulesets_from_file"
1167 if [ -z "$file" ]; then
1168 warn "$_me: you must specify a file"
1171 if [ ! -e "$file" ]; then
1172 debug "$_me: no such file ($file)"
1175 debug "reading rulesets from file ($file)"
1183 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1184 if [ -z "$rulenum" ]; then
1185 warn "$_me: cannot extract rule number ($line)"
1189 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1190 if [ -z "$rulename" ]; then
1191 warn "$_me: cannot extract rule name ($line)"
1195 eval $rulename=\$rulenum
1196 debug "found ruleset: $rulename=$rulenum"
1197 if ! /sbin/devfs rule -s $rulenum delset; then
1203 rulecmd="${line%%"\#*"}"
1204 # evaluate the command incase it includes
1206 if [ -n "$rulecmd" ]; then
1207 debug "adding rule ($rulecmd)"
1208 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1216 if [ $_err -ne 0 ]; then
1217 debug "error in $_me"
1224 # devfs_init_rulesets
1225 # Initializes rulesets from configuration files. Returns
1226 # non-zero if there was an error.
1228 devfs_init_rulesets()
1231 _me="devfs_init_rulesets"
1233 # Go through this only once
1234 if [ -n "$devfs_rulesets_init" ]; then
1235 debug "$_me: devfs rulesets already initialized"
1238 for file in $devfs_rulesets; do
1239 devfs_rulesets_from_file $file || return 1
1241 devfs_rulesets_init=1
1242 debug "$_me: devfs rulesets initialized"
1246 # devfs_set_ruleset ruleset [dir]
1247 # Sets the default ruleset of dir to ruleset. The ruleset argument
1248 # must be a ruleset name as specified in devfs.rules(5) file.
1249 # Returns non-zero if it could not set it successfully.
1254 [ -n "$1" ] && eval rs=\$$1 || rs=
1255 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1256 _me="devfs_set_ruleset"
1258 if [ -z "$rs" ]; then
1259 warn "$_me: you must specify a ruleset number"
1262 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1263 if ! /sbin/devfs $devdir ruleset $rs; then
1264 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1270 # devfs_apply_ruleset ruleset [dir]
1271 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1272 # The ruleset argument must be a ruleset name as specified
1273 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1274 # if it could not apply the ruleset.
1276 devfs_apply_ruleset()
1279 [ -n "$1" ] && eval rs=\$$1 || rs=
1280 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1281 _me="devfs_apply_ruleset"
1283 if [ -z "$rs" ]; then
1284 warn "$_me: you must specify a ruleset"
1287 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1288 if ! /sbin/devfs $devdir rule -s $rs applyset; then
1289 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1295 # devfs_domount dir [ruleset]
1296 # Mount devfs on dir. If ruleset is specified it is set
1297 # on the mount-point. It must also be a ruleset name as specified
1298 # in a devfs.rules(5) file. Returns 0 on success.
1304 [ -n "$2" ] && rs=$2 || rs=
1305 _me="devfs_domount()"
1307 if [ -z "$devdir" ]; then
1308 warn "$_me: you must specify a mount-point"
1311 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1312 if ! mount -t devfs dev "$devdir"; then
1313 warn "$_me: Unable to mount devfs on $devdir"
1316 if [ -n "$rs" ]; then
1318 devfs_set_ruleset $rs $devdir
1319 devfs -m $devdir rule applyset
1324 # devfs_mount_jail dir [ruleset]
1325 # Mounts a devfs file system appropriate for jails
1326 # on the directory dir. If ruleset is specified, the ruleset
1327 # it names will be used instead. If present, ruleset must
1328 # be the name of a ruleset as defined in a devfs.rules(5) file.
1329 # This function returns non-zero if an error occurs.
1335 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1336 _me="devfs_mount_jail"
1339 if ! devfs_domount "$jdev" $rs; then
1340 warn "$_me: devfs was not mounted on $jdev"
1346 # Provide a function for normalizing the mounting of memory
1347 # filesystems. This should allow the rest of the code here to remain
1348 # as close as possible between 5-current and 4-stable.
1351 # $3 = (optional) extra mdmfs flags
1354 if [ -n "$3" ]; then
1357 /sbin/mdmfs $flags -s $1 md $2
1360 # Code common to scripts that need to load a kernel module
1361 # if it isn't in the kernel yet. Syntax:
1362 # load_kld [-e regex] [-m module] file
1363 # where -e or -m chooses the way to check if the module
1364 # is already loaded:
1365 # regex is egrep'd in the output from `kldstat -v',
1366 # module is passed to `kldstat -m'.
1367 # The default way is as though `-m file' were specified.
1370 local _loaded _mod _opt _re
1372 while getopts "e:m:" _opt; do
1375 m) _mod="$OPTARG" ;;
1376 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1379 shift $(($OPTIND - 1))
1380 if [ $# -ne 1 ]; then
1381 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1385 if [ -n "$_re" ]; then
1386 if kldstat -v | egrep -q -e "$_re"; then
1390 if kldstat -q -m "$_mod"; then
1395 if ! kldload "$1"; then
1396 warn "Unable to load kernel module $1"
1399 info "$1 kernel module loaded."
1402 debug "load_kld: $1 kernel module already loaded."
1408 # Change every $src in $str to $dst.
1409 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1413 local _str _src _dst _out _com
1420 for _com in ${_str}; do
1421 if [ -z "${_out}" ]; then
1424 _out="${_out}${_dst}${_com}"
1430 # Creates a list of providers for GELI encryption.
1433 local devices devices2
1434 local provider mountpoint type options rest
1436 # Create list of GELI providers from fstab.
1437 while read provider mountpoint type options rest ; do
1438 case ":${options}" in
1447 case ":${provider}" in
1452 # Skip swap devices.
1453 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
1456 devices="${devices} ${provider}"
1461 # Append providers from geli_devices.
1462 devices="${devices} ${geli_devices}"
1464 for provider in ${devices}; do
1465 provider=${provider%.eli}
1466 provider=${provider#/dev/}
1467 devices2="${devices2} ${provider}"
1473 # Find scripts in local_startup directories that use the old syntax
1475 find_local_scripts_old () {
1478 for dir in ${local_startup}; do
1479 if [ -d "${dir}" ]; then
1480 for file in ${dir}/[0-9]*.sh; do
1481 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1483 zlist="$zlist $file"
1485 for file in ${dir}/[^0-9]*.sh; do
1486 grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
1488 slist="$slist $file"
1494 find_local_scripts_new () {
1496 for dir in ${local_startup}; do
1497 if [ -d "${dir}" ]; then
1498 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1501 *) if [ -x "$file" ]; then
1502 local_rc="${local_rc} ${file}"