1 # $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $
2 # $FreeBSD: src/etc/rc.subr,v 1.13 2003/06/09 17:31:06 mtm Exp $
4 # Copyright (c) 1997-2002 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
43 # Operating System dependent/independent variables
46 if [ "X$_file" = "X" ]; then
50 provide_list=`rcorder -p $_file`
53 SYSCTL_N="${SYSCTL} -n"
54 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
55 OSTYPE=`${CMD_OSTYPE}`
71 # Set the variable name enabling a specific service.
72 # FreeBSD uses ${service}_enable, while NetBSD uses
73 # just the name of the service. For example:
74 # FreeBSD: sendmail_enable="YES"
75 # NetBSD : sendmail="YES"
76 # $1 - if $name is not the base to work of off, specify
92 # $1 should be $rc_arg (start, stop, restart, reload, etc)
93 # $2 return value $RC_*
95 # Set the rcng_* variables associated with elements in provide_list
98 # Returns non-zero when early termination should occur, in which
99 # case the caller should return with a value of $? - 1
103 # Remember, plret is set to the early termination return code + 1,
104 # or 0 if we want to continue the operation.
106 for i in $provide_list; do
108 start$RC_RUNNING|restart$RC_RUNNING)
109 varsym -s rcng_$i=running
111 start$RC_FAILED|restart$RC_FAILED)
112 varsym -s rcng_$i=failed
114 start$RC_DISABLED|restart$RC_DISABLED|reload$RC_DISABLED)
115 varsym -s rcng_$i=disabled
117 start$RC_IRRELEVANT|restart$RC_IRRELEVANT|reload$RC_IRRELEVANT)
118 varsym -s rcng_$i=irrelevant
120 start$RC_CONFIGURED|restart$RC_CONFIGURED)
121 varsym -s rcng_$i=configured
124 varsym -s rcng_$i=disabled
127 varsym -s rcng_$i=irrelevant
130 varsym -s rcng_$i=stopped
139 # $1 should be $rc_arg (start, stop, restart, reload, etc)
140 # $2 return value $RC_*
141 # $3 $rc_force "" not to force, "anything" to force.
143 # The return code is 0 if early termination is not to occur, non-zero if
144 # it is to occur. When early termination is to occur the caller should
145 # return check_early_term()'s return code - 1. That is, early termination
146 # can occur with or without an error.
148 # The provide list will be adjusted when early termination occurs.
157 set_provide_list $1 $2
158 [ -z "$3" ] || return 0
162 set_provide_list $1 $2
163 [ -z "$3" ] || return 0
167 set_provide_list $1 $2
168 [ -z "$3" ] || return 0
178 set_provide_list $1 $2
179 [ -z "$3" ] || return 0
183 # adjust_return_code $1
185 # Convert the return code to an exit code of 0 (success) or 1 (failure)
189 if [ $1 = $RC_FAILED ]; then
196 # force_depend script
197 # Force a service to start. Intended for use by services
198 # to resolve dependency issues. It is assumed the caller
199 # has check to make sure this call is necessary
200 # $1 - filename of script, in /etc/rc.d, to run
206 info "${name} depends on ${_depend}, which will be forced to start."
207 if ! /etc/rc.d/${_depend} forcestart ; then
208 warn "Unable to force ${_depend}. It may already be running."
216 # Test $1 variable, and warn if not set to YES or NO.
217 # Return 0 if it's "yes" (et al), nonzero otherwise.
222 eval _value=\$${1%_enable}
226 eval _value=\$${1%_enable}_enable;
228 debug "checkyesno: $1 is set to $_value."
231 # "yes", "true", "on", or "1"
232 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
236 # "no", "false", "off", or "0"
237 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
241 warn "\$${1} is not set properly - see rc.conf(5)."
248 # print the list in reverse order
253 for _revfile in $*; do
254 _revlist="$_revfile $_revlist"
260 # mount_critical_filesystems type
261 # Go through the list of critical filesystems as provided in
262 # the rc.conf(5) variable $critical_filesystems_${type}, checking
263 # each one to see if it is mounted, and if it is not, mounting it.
265 mount_critical_filesystems()
267 eval _fslist=\$critical_filesystems_${1}
268 for _fs in $_fslist; do
271 while read what _on on _type type; do
272 if [ $on = $_fs ]; then
276 if [ $_ismounted = no ]; then
277 mount $_fs >/dev/null 2>&1
284 # check_pidfile pidfile procname [interpreter]
285 # Parses the first line of pidfile for a PID, and ensures
286 # that the process is running and matches procname.
287 # Prints the matching PID upon success, nothing otherwise.
288 # interpreter is optional; see _find_processes() for details.
295 if [ -z "$_pidfile" -o -z "$_procname" ]; then
296 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
298 if [ ! -f $_pidfile ]; then
299 debug "pid file {$_pidfile): not readable."
302 read _pid _junk < $_pidfile
303 if [ -z "$_pid" ]; then
304 debug "pid file {$_pidfile): no pid in file."
307 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
311 # check_process procname [interpreter]
312 # Ensures that a process (or processes) named procname is running.
313 # Prints a list of matching PIDs.
314 # interpreter is optional; see _find_processes() for details.
320 if [ -z "$_procname" ]; then
321 err 3 'USAGE: check_process procname [interpreter]'
323 _find_processes $_procname ${_interpreter:-.} '-ax'
327 # _find_processes procname interpreter psargs
328 # Search for procname in the output of ps generated by psargs.
329 # Prints the PIDs of any matching processes, space separated.
331 # If interpreter == ".", check the following variations of procname
332 # against the first word of each command:
334 # `basename procname`
335 # `basename procname` + ":"
336 # "(" + `basename procname` + ")"
338 # If interpreter != ".", read the first line of procname, remove the
339 # leading #!, normalise whitespace, append procname, and attempt to
340 # match that against each command, either as is, or with extra words
345 if [ $# -ne 3 ]; then
346 err 3 'USAGE: _find_processes procname interpreter psargs'
353 if [ $_interpreter != "." ]; then # an interpreted script
354 read _interp < $_procname # read interpreter name
355 _interp=${_interp#\#!} # strip #!
357 if [ $_interpreter != $1 ]; then
358 warn "\$command_interpreter $_interpreter != $1"
360 _interp="$* $_procname" # cleanup spaces, add _procname
361 _interpbn="$1" # the interpreter binary
363 _fp_match='case "$_argv" in
364 "${_interpbn##*/}: "$_procname*|${_interp}|"${_interp} "*)'
365 else # a normal daemon
366 _procnamebn=${_procname##*/}
367 _fp_args='_arg0 _argv'
368 _fp_match='case "$_arg0" in
369 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
373 ps -o "pid,command" '"$_psargs"' |
374 while read _npid '"$_fp_args"'; do
378 esac ; '"$_fp_match"'
379 echo -n "$_pref$_npid" ;
385 # debug "in _find_processes: proccheck is ($_proccheck)."
390 # wait_for_pids pid [pid ...]
391 # spins until none of the pids exist
396 if [ -z "$_list" ]; then
403 if kill -0 $_j 2>/dev/null; then
404 _nlist="${_nlist}${_nlist:+ }$_j"
407 if [ -z "$_nlist" ]; then
411 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
415 if [ -n "$_prefix" ]; then
421 # get_pidfile_from_conf string file
423 # Takes a string to search for in the specified file.
424 # Ignores lines with traditional comment characters.
428 # if get_pidfile_from_conf string file; then
429 # pidfile="$_pidfile_from_conf"
431 # pidfile='appropriate default'
434 get_pidfile_from_conf()
436 if [ -z "$1" -o -z "$2" ]; then
437 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
440 local string file line
442 string="$1" ; file="$2"
444 if [ ! -s "$file" ]; then
445 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
450 *[#\;]*${string}*) continue ;;
451 *${string}*) break ;;
455 if [ -n "$line" ]; then
457 _pidfile_from_conf="/${line%%[\"\;]*}"
464 # run_rc_command argument
465 # Search for argument in the list of supported commands, which is:
466 # "start stop restart rcvar status poll ${extra_commands}"
467 # If there's a match, run ${argument}_cmd or the default method
470 # If argument has a given prefix, then change the operation as follows:
473 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
474 # force Set ${rcvar} to YES, and set rc_force=yes
475 # one Set ${rcvar} to YES
476 # quiet Don't output some diagnostics, and set rc_quiet=yes
478 # The following globals are used:
480 # Name Needed Purpose
481 # ---- ------ -------
482 # provide_list (gen) list of keywords provided by current rcng file
484 # name y Name of script.
486 # command n Full path to command.
487 # Not needed if ${rc_arg}_cmd is set for
490 # command_args n Optional args/shell directives for command.
492 # command_interpreter n If not empty, command is interpreted, so
493 # call check_{pidfile,process}() appropriately.
495 # extra_commands n List of extra commands supported.
497 # pidfile n If set, use check_pidfile $pidfile $command,
498 # otherwise use check_process $command.
499 # In either case, only check if $command is set.
501 # procname n Process name to check for instead of $command.
503 # rcvar n This is checked with checkyesno to determine
504 # if the action should be run.
506 # ${name}_chroot n Directory to chroot to before running ${command}
507 # Requires /usr to be mounted.
509 # ${name}_chdir n Directory to cd to before running ${command}
510 # (if not using ${name}_chroot).
512 # ${name}_flags n Arguments to call ${command} with.
513 # NOTE: $flags from the parent environment
514 # can be used to override this.
516 # ${name}_nice n Nice level to run ${command} at.
518 # ${name}_user n User to run ${command} as, using su(1) if not
519 # using ${name}_chroot.
520 # Requires /usr to be mounted.
522 # ${name}_group n Group to run chrooted ${command} as.
523 # Requires /usr to be mounted.
525 # ${name}_groups n Comma separated list of supplementary groups
526 # to run the chrooted ${command} with.
527 # Requires /usr to be mounted.
529 # ${rc_arg}_cmd n If set, use this as the method when invoked;
530 # Otherwise, use default command (see below)
532 # ${rc_arg}_precmd n If set, run just before performing the
533 # ${rc_arg}_cmd method in the default
534 # operation (i.e, after checking for required
535 # bits and process (non)existence).
536 # If this completes with a non-zero exit code,
537 # don't run ${rc_arg}_cmd.
539 # ${rc_arg}_postcmd n If set, run just after performing the
540 # ${rc_arg}_cmd method, if that method
541 # returned a zero exit code.
543 # required_dirs n If set, check for the existence of the given
544 # directories before running the default
547 # required_files n If set, check for the readability of the given
548 # files before running the default (re)start
551 # required_vars n If set, perform checkyesno on each of the
552 # listed variables before running the default
555 # Default behaviour for a given argument, if no override method is
558 # Argument Default behaviour
559 # -------- -----------------
560 # start if !running && checkyesno ${rcvar}
564 # rc_pid=$(check_pidfile $pidfile $command)
566 # rc_pid=$(check_process $command)
567 # kill $sig_stop $rc_pid
568 # wait_for_pids $rc_pid
569 # ($sig_stop defaults to TERM.)
571 # reload Similar to stop, except use $sig_reload instead,
572 # and doesn't wait_for_pids.
573 # $sig_reload defaults to HUP.
575 # restart Run `stop' then `start'.
577 # status Show if ${command} is running, etc.
579 # poll Wait for ${command} to exit.
581 # rcvar Display what rc.conf variable is used (if any).
583 # Variables available to methods, and after run_rc_command() has
588 # rc_arg Argument to command, after fast/force/one processing
591 # rc_flags Flags to start the default command with.
592 # Defaults to ${name}_flags, unless overridden
593 # by $flags from the environment.
594 # This variable may be changed by the precmd method.
596 # rc_pid PID of command (if appropriate)
598 # rc_fast Not empty if "fast" was provided (q.v.)
600 # rc_force Not empty if "force" was provided (q.v.)
602 # rc_quiet Not empty if "quiet" was provided
610 fast*) # "fast" prefix; don't check pid
611 rc_arg=${rc_arg#fast}
613 force*) # "force" prefix; always start
614 rc_arg=${rc_arg#force}
616 one*) # "one" prefix; set ${rcvar}=yes
620 set_provide_list $rc_arg $RC_CONFIGURED
628 if [ -z "$name" ]; then
629 err 3 'run_rc_command: $name is not set.'
632 # Don't repeat the first argument when passing additional command-
633 # line arguments to the command subroutines.
639 fast*) # "fast" prefix; don't check pid
640 rc_arg=${rc_arg#fast}
644 force*) # "force" prefix; always start
645 rc_arg=${rc_arg#force}
647 if [ -n "${rcvar}" ]; then
651 one*) # "one" prefix; set ${rcvar}=yes
653 if [ -n "${rcvar}" ]; then
657 quiet*) # "quiet" prefix; omit some messages
659 rc_arg=${rc_arg#${_rc_prefix}}
664 eval _override_command=\$${name}_program
665 if [ -n "$_override_command" ]; then
666 command=$_override_command
669 _keywords="start stop restart rcvar $extra_commands"
672 _procname=${procname:-${command}}
674 # setup pid check command if not fast
675 if [ -z "$rc_fast" -a -n "$_procname" ]; then
676 if [ -n "$pidfile" ]; then
677 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
679 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
681 if [ -n "$_pidcmd" ]; then
682 _keywords="${_keywords} status poll"
686 if [ -z "$rc_arg" ]; then
687 rc_usage "$_keywords"
690 if [ -n "$flags" ]; then # allow override from environment
693 eval rc_flags=\$${name}_flags
695 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
696 _nice=\$${name}_nice _user=\$${name}_user \
697 _group=\$${name}_group _groups=\$${name}_groups
699 if [ -n "$_user" ]; then # unset $_user if running as that user
700 if [ "$_user" = "$(id -un)" ]; then
705 # if ${rcvar} is set, and $1 is not
707 # checkyesno ${rcvar}
708 # and return if that failed
710 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
711 if ! checkyesno ${rcvar}; then
712 set_provide_list $rc_arg $RC_DISABLED
717 eval $_pidcmd # determine the pid if necessary
719 for _elem in $_keywords; do
720 if [ "$_elem" != "$rc_arg" ]; then
724 # if there's a custom ${XXX_cmd},
725 # run that instead of the default
727 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
728 _postcmd=\$${rc_arg}_postcmd
729 if [ -n "$_cmd" ]; then
730 # if the precmd failed and force
733 if [ -n "$_precmd" ]; then
734 debug "run_rc_command: evaluating ${_precmd}()."
735 eval $_precmd $rc_extra_args
738 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
741 if [ -n "$_cmd" ]; then
742 debug "run_rc_command: evaluating ${_cmd}()."
743 eval $_cmd $rc_extra_args
745 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
748 if [ -n "$_postcmd" ]; then
749 debug "run_rc_command: evaluating ${_postcmd}()."
750 eval $_postcmd $rc_extra_args
752 check_early_term $rc_arg $_return "" || return $(($?-1))
754 set_provide_list $rc_arg $_return
755 adjust_return_code $_return
759 case "$rc_arg" in # default operations...
762 if [ -n "$rc_pid" ]; then
763 echo "${name} is running as pid $rc_pid."
765 echo "${name} is not running."
771 if [ -n "$rc_pid" ]; then
772 if [ -z "$rc_quiet" ]; then
773 echo "${name} already running? (pid=$rc_pid)."
778 if [ ! -x $command ]; then
779 info "run_rc_command: cannot run ($command)."
780 set_provide_list $rc_arg $RC_FAILED
781 adjust_return_code $RC_FAILED
785 # check for required variables,
786 # directories, and files
788 for _f in $required_vars; do
789 if ! checkyesno $_f; then
790 warn "\$${_f} is not set."
791 if [ -z "$rc_force" ]; then
792 set_provide_list $rc_arg $RC_FAILED
793 adjust_return_code $RC_FAILED
798 for _f in $required_dirs; do
799 if [ ! -d "${_f}/." ]; then
800 warn "${_f} is not a directory."
801 if [ -z "$rc_force" ]; then
802 set_provide_list $rc_arg $RC_FAILED
803 adjust_return_code $RC_FAILED
808 for _f in $required_files; do
809 if [ ! -r "${_f}" ]; then
810 warn "${_f} is not readable."
811 if [ -z "$rc_force" ]; then
812 set_provide_list $rc_arg $RC_FAILED
813 adjust_return_code $RC_FAILED
819 # if the precmd failed and force
822 if [ -n "${_precmd}" ]; then
823 debug "run_rc_command: evaluating ${_precmd}()."
826 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
829 # setup the command to run, and run it
831 echo "Starting ${name}."
832 if [ -n "$_chroot" ]; then
834 ${_nice:+nice -n $_nice }\
835 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
836 $_chroot $command $rc_flags $command_args"
839 ${_chdir:+cd $_chdir; }\
840 ${_nice:+nice -n $_nice }\
841 $command $rc_flags $command_args"
842 if [ -n "$_user" ]; then
843 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
847 # if the cmd failed and force
850 debug "run_rc_command: _doit: $_doit"
853 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
854 # finally, run postcmd
856 if [ -n "${_postcmd}" ]; then
857 debug "run_rc_command: evaluating ${_postcmd}()."
863 if [ -z "$rc_pid" ]; then
864 if [ -n "$pidfile" ]; then
866 "${name} not running? (check $pidfile)."
868 echo "${name} not running?"
870 set_provide_list $rc_arg $RC_STOPPED
874 # if the precmd failed and force
877 if [ -n "$_precmd" ]; then
880 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
883 # send the signal to stop
885 echo "Stopping ${name}."
886 _doit="kill -${sig_stop:-TERM} $rc_pid"
887 if [ -n "$_user" ]; then
888 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
891 # if the stop cmd failed and force
896 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
897 # wait for the command to exit,
900 wait_for_pids $rc_pid
901 if [ -n "$_postcmd" ]; then
908 if [ -z "$rc_pid" ]; then
909 if [ -n "$pidfile" ]; then
911 "${name} not running? (check $pidfile)."
913 echo "${name} not running?"
915 set_provide_list $rc_arg $RC_FAILED
918 echo "Reloading ${name} config files."
919 if [ -n "$_precmd" ]; then
922 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
924 _doit="kill -${sig_reload:-HUP} $rc_pid"
925 if [ -n "$_user" ]; then
926 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
930 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
931 if [ -n "$_postcmd" ]; then
938 if [ -n "$_precmd" ]; then
939 eval $_precmd $rc_extra_args
941 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
943 # prevent restart being called more
944 # than once by any given script
946 if [ -n "$_rc_restart_done" ]; then
951 ( $0 ${rc_force:+force}stop $rc_extra_args )
952 $0 ${rc_force:+force}start $rc_extra_args
955 if [ -n "$_postcmd" ]; then
956 eval $_postcmd $rc_extra_args
957 adjust_return_code $?
960 # Do not set_provide_list(), the start command above
961 # will have done it for us and we do not know the
962 # actual RC code to base a setting on here.
968 if [ -n "$rc_pid" ]; then
969 wait_for_pids $rc_pid
975 if [ -n "$rcvar" ]; then
976 if checkyesno ${rcvar}; then
977 echo "\$${rcvar}=YES"
985 rc_usage "$_keywords"
989 set_provide_list $rc_arg $_return
990 adjust_return_code $_return
994 echo 1>&2 "$0: unknown directive '$rc_arg'."
995 rc_usage "$_keywords"
1000 # run_rc_script file arg
1001 # Start the script `file' with `arg', and correctly handle the
1002 # return value from the script. If `file' ends with `.sh', it's
1003 # sourced into the current environment. If `file' appears to be
1004 # a backup or scratch file, ignore it. Otherwise if it's
1005 # executable run as a child process.
1011 if [ -z "$_file" -o -z "$_arg" ]; then
1012 err 3 'USAGE: run_rc_script file arg'
1015 trap "echo 'Reboot interrupted'; exit 1" 3
1017 unset name command command_args command_interpreter \
1018 extra_commands pidfile procname \
1019 rcvar required_dirs required_files required_vars
1020 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1023 *.sh) # run in current shell
1024 set $_arg ; . $_file
1026 *[~#]|*.OLD|*.orig) # scratch file; skip
1027 warn "Ignoring scratch file $_file"
1029 *) # run in subshell
1030 if [ -x $_file ]; then
1031 if [ -n "$rc_fast_and_loose" ]; then
1032 set $_arg ; . $_file
1034 ( trap "echo 'Reboot interrupted'; exit 1" 3
1035 set $_arg ; . $_file )
1043 # Change every $src in $str to $dst.
1044 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1048 local _str _src _dst _out _com
1055 for _com in ${_str}; do
1056 if [ -z "${_out}" ]; then
1059 _out="${_out}${_dst}${_com}"
1067 # Source in the configuration file for a given command.
1072 if [ -z "$_command" ]; then
1073 err 3 'USAGE: load_rc_config command'
1076 if [ -z "$_rc_conf_loaded" ]; then
1077 if [ -r /etc/defaults/rc.conf ]; then
1078 debug "Sourcing /etc/defaults/rc.conf"
1079 . /etc/defaults/rc.conf
1081 elif [ -r /etc/rc.conf ]; then
1082 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1087 if [ -f /etc/rc.conf.d/"$_command" ]; then
1088 debug "Sourcing /etc/rc.conf.d/${_command}"
1089 . /etc/rc.conf.d/"$_command"
1092 # XXX - Deprecated variable name support
1094 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
1095 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
1096 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
1097 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
1098 [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
1099 [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
1105 # Print a usage string for $0, with `commands' being a list of
1110 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
1114 echo -n 1>&2 "$_sep$_elem"
1122 # err exitval message
1123 # Display message to stderr and log to the syslog, and exit with exitval.
1130 if [ -x /usr/bin/logger ]; then
1131 logger "$0: ERROR: $*"
1133 echo 1>&2 "$0: ERROR: $*"
1139 # Display message to stderr and log to the syslog.
1143 if [ -x /usr/bin/logger ]; then
1144 logger "$0: WARNING: $*"
1146 echo 1>&2 "$0: WARNING: $*"
1151 # Display informational message to stdout and log to syslog.
1156 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1157 if [ -x /usr/bin/logger ]; then
1158 logger "$0: INFO: $*"
1167 # If debugging is enabled in rc.conf output message to stderr.
1168 # BEWARE that you don't call any subroutine that itself calls this
1174 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1175 if [ -x /usr/bin/logger ]; then
1176 logger "$0: INFO: $*"
1178 echo 1>&2 "$0: DEBUG: $*"
1184 # backup_file action file cur backup
1185 # Make a backup copy of `file' into `cur', and save the previous
1186 # version of `cur' as `backup' or use rcs for archiving.
1188 # This routine checks the value of the backup_uses_rcs variable,
1189 # which can be either YES or NO.
1191 # The `action' keyword can be one of the following:
1193 # add `file' is now being backed up (and is possibly
1194 # being reentered into the backups system). `cur'
1195 # is created and RCS files, if necessary, are
1198 # update `file' has changed and needs to be backed up.
1199 # If `cur' exists, it is copied to to `back' or
1200 # checked into RCS (if the repository file is old),
1201 # and then `file' is copied to `cur'. Another RCS
1202 # check in done here if RCS is being used.
1204 # remove `file' is no longer being tracked by the backups
1205 # system. If RCS is not being used, `cur' is moved
1206 # to `back', otherwise an empty file is checked in,
1207 # and then `cur' is removed.
1217 if checkyesno backup_uses_rcs; then
1218 _msg0="backup archive"
1221 # ensure that history file is not locked
1222 if [ -f $_cur,v ]; then
1223 rcs -q -u -U -M $_cur
1226 # ensure after switching to rcs that the
1227 # current backup is not lost
1228 if [ -f $_cur ]; then
1229 # no archive, or current newer than archive
1230 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1231 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1239 cp -p $_cpfile $_cur
1240 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1243 chown root:wheel $_cur $_cur,v
1247 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1249 chown root:wheel $_cur $_cur,v
1256 if [ -f $_cur ]; then
1259 cp -p $_cpfile $_cur
1260 chown root:wheel $_cur
1269 # devfs_mount_jail dir
1270 # Mounts a devfs file system appropriate for jails
1271 # on the directory dir.
1272 # This function returns non-zero if an error occurs.
1278 _me="devfs_mount_jail"
1280 if ! devfs_domount "$jdev" $rs; then
1281 warn "$_me: devfs was not mounted on $jdev"
1288 # Mount devfs on dir.
1289 # Returns 0 on success.
1295 _me="devfs_domount()"
1297 if [ -z "$devdir" ]; then
1298 warn "$_me: you must specify a mount-point"
1302 debug "$_me: mount-point is ($devdir)"
1303 if ! mount -t devfs devfs "$devdir"; then
1304 warn "$_me: Unable to mount devfs on $devdir"