1 # $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $
2 # $FreeBSD: head/etc/rc.subr 275359 2014-12-01 12:17:42Z des $
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
42 : ${RC_PID:=$$}; export RC_PID
45 # Operating System dependent/independent variables
48 if [ -z "$_file" ]; then
51 provide_list=`rcorder -p $_file`
53 if [ -n "${_rc_subr_loaded}" ]; then
60 SYSCTL_N="${SYSCTL} -n"
62 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
63 OSTYPE=`${CMD_OSTYPE}`
77 # List vars matching pattern.
81 set | { while read LINE; do
84 "$LINE"|*[!a-zA-Z0-9_]*) continue ;;
91 # Set the variable name enabling a specific service.
92 # FreeBSD uses ${service}_enable, while NetBSD uses
93 # just the name of the service. For example:
94 # FreeBSD: sendmail_enable="YES"
95 # NetBSD : sendmail="YES"
96 # $1 - if $name is not the base to work of off, specify
112 # $1 should be $rc_arg (start, stop, restart, reload, etc)
113 # $2 return value $RC_*
115 # Set the rcng_* variables associated with elements in provide_list
116 # based on $1 and $2.
118 # Returns non-zero when early termination should occur, in which
119 # case the caller should return with a value of $? - 1
123 # Remember, plret is set to the early termination return code + 1,
124 # or 0 if we want to continue the operation.
126 for i in $provide_list; do
128 start$RC_RUNNING|restart$RC_RUNNING)
129 varsym -s rcng_$i=running
131 start$RC_FAILED|restart$RC_FAILED)
132 varsym -s rcng_$i=failed
134 start$RC_DISABLED|restart$RC_DISABLED|reload$RC_DISABLED)
135 varsym -s rcng_$i=disabled
137 start$RC_IRRELEVANT|restart$RC_IRRELEVANT|reload$RC_IRRELEVANT)
138 varsym -s rcng_$i=irrelevant
140 start$RC_CONFIGURED|restart$RC_CONFIGURED)
141 varsym -s rcng_$i=configured
144 varsym -s rcng_$i=disabled
147 varsym -s rcng_$i=irrelevant
150 varsym -s rcng_$i=stopped
159 # $1 should be $rc_arg (start, stop, restart, reload, etc)
160 # $2 return value $RC_*
161 # $3 $rc_force "" not to force, "anything" to force.
163 # The return code is 0 if early termination is not to occur, non-zero if
164 # it is to occur. When early termination is to occur the caller should
165 # return check_early_term()'s return code - 1. That is, early termination
166 # can occur with or without an error.
168 # The provide list will be adjusted when early termination occurs.
177 set_provide_list $1 $2
178 [ -z "$3" ] || return 0
182 set_provide_list $1 $2
183 [ -z "$3" ] || return 0
187 set_provide_list $1 $2
188 [ -z "$3" ] || return 0
198 set_provide_list $1 $2
199 [ -z "$3" ] || return 0
203 # adjust_return_code $1
205 # Convert the return code to an exit code of 0 (success) or 1 (failure)
209 if [ $1 = $RC_FAILED ]; then
216 # force_depend script
217 # Force a service to start. Intended for use by services
218 # to resolve dependency issues. It is assumed the caller
219 # has check to make sure this call is necessary
220 # $1 - filename of script, in /etc/rc.d, to run
226 info "${name} depends on ${_depend}, which will be forced to start."
227 if ! /etc/rc.d/${_depend} forcestart ; then
228 warn "Unable to force ${_depend}. It may already be running."
236 # Test $1 variable, and warn if not set to YES or NO.
237 # Return 0 if it's "yes" (et al), nonzero otherwise.
242 eval _value=\$${1%_enable}
246 eval _value=\$${1%_enable}_enable;
248 debug "checkyesno: $1 is set to $_value."
251 # "yes", "true", "on", or "1"
252 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
256 # "no", "false", "off", or "0"
257 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
261 warn "\$${1} is not set properly - see rc.conf(5)."
269 # print the list in reverse order
274 for _revfile in $*; do
275 _revlist="$_revfile $_revlist"
282 # If booting directly to multiuser or $always is true,
283 # send SIGTERM to the parent (/etc/rc) to abort the boot.
284 # Otherwise just exit.
291 # "yes", "true", "on", or "1"
292 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
299 if [ "$autoboot" = yes -o "$always" = true ]; then
300 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
307 # mount_critical_filesystems type
308 # Go through the list of critical filesystems as provided in
309 # the rc.conf(5) variable $critical_filesystems_${type}, checking
310 # each one to see if it is mounted, and if it is not, mounting it.
312 mount_critical_filesystems()
314 eval _fslist=\$critical_filesystems_${1}
315 for _fs in $_fslist; do
318 while read what _on on _type type; do
319 if [ $on = $_fs ]; then
323 if [ $_ismounted = no ]; then
324 mount $_fs >/dev/null 2>&1
331 # check_pidfile pidfile procname [interpreter]
332 # Parses the first line of pidfile for a PID, and ensures
333 # that the process is running and matches procname.
334 # Prints the matching PID upon success, nothing otherwise.
335 # interpreter is optional; see _find_processes() for details.
342 if [ -z "$_pidfile" -o -z "$_procname" ]; then
343 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
345 if [ ! -f $_pidfile ]; then
346 debug "pid file {$_pidfile): not readable."
349 read _pid _junk < $_pidfile
350 if [ -z "$_pid" ]; then
351 debug "pid file {$_pidfile): no pid in file."
354 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
358 # check_process procname [interpreter]
359 # Ensures that a process (or processes) named procname is running.
360 # Prints a list of matching PIDs.
361 # interpreter is optional; see _find_processes() for details.
367 if [ -z "$_procname" ]; then
368 err 3 'USAGE: check_process procname [interpreter]'
370 _find_processes $_procname ${_interpreter:-.} '-ax'
374 # _find_processes procname interpreter psargs
375 # Search for procname in the output of ps generated by psargs.
376 # Prints the PIDs of any matching processes, space separated.
378 # If interpreter == ".", check the following variations of procname
379 # against the first word of each command:
381 # `basename procname`
382 # `basename procname` + ":"
383 # "(" + `basename procname` + ")"
385 # If interpreter != ".", read the first line of procname, remove the
386 # leading #!, normalise whitespace, append procname, and attempt to
387 # match that against each command, either as is, or with extra words
392 if [ $# -ne 3 ]; then
393 err 3 'USAGE: _find_processes procname interpreter psargs'
400 if [ $_interpreter != "." ]; then # an interpreted script
401 read _interp < $_procname # read interpreter name
402 _interp=${_interp#\#!} # strip #!
404 if [ $_interpreter != $1 ]; then
405 warn "\$command_interpreter $_interpreter != $1"
407 _interp="$* $_procname" # cleanup spaces, add _procname
408 _interpbn="$1" # the interpreter binary
410 _fp_match='case "$_argv" in
411 "${_interpbn##*/}: "$_procname*|${_interp}|"${_interp} "*)'
412 else # a normal daemon
413 _procnamebn=${_procname##*/}
414 _fp_args='_arg0 _argv'
415 _fp_match='case "$_arg0" in
416 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
420 ps -o "pid,command" '"$_psargs"' |
421 while read _npid '"$_fp_args"'; do
425 esac ; '"$_fp_match"'
426 echo -n "$_pref$_npid" ;
432 # debug "in _find_processes: proccheck is ($_proccheck)."
437 # wait_for_pids pid [pid ...]
438 # spins until none of the pids exist
443 if [ -z "$_list" ]; then
450 if kill -0 $_j 2>/dev/null; then
451 _nlist="${_nlist}${_nlist:+ }$_j"
454 if [ -z "$_nlist" ]; then
458 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
462 if [ -n "$_prefix" ]; then
468 # get_pidfile_from_conf string file
470 # Takes a string to search for in the specified file.
471 # Ignores lines with traditional comment characters.
475 # if get_pidfile_from_conf string file; then
476 # pidfile="$_pidfile_from_conf"
478 # pidfile='appropriate default'
481 get_pidfile_from_conf()
483 if [ -z "$1" -o -z "$2" ]; then
484 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
487 local string file line
489 string="$1" ; file="$2"
491 if [ ! -s "$file" ]; then
492 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
497 *[#\;]*${string}*) continue ;;
498 *${string}*) break ;;
502 if [ -n "$line" ]; then
504 _pidfile_from_conf="/${line%%[\"\;]*}"
512 # If rc_quiet is set (usually as a result of using faststart at
513 # boot time) check if rc_startmsgs is enabled.
517 if [ -n "$rc_quiet" ]; then
518 checkyesno rc_startmsgs
525 # run_rc_command argument
526 # Search for argument in the list of supported commands, which is:
527 # "start stop restart rcvar status poll ${extra_commands}"
528 # If there's a match, run ${argument}_cmd or the default method
531 # If argument has a given prefix, then change the operation as follows:
534 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
535 # force Set ${rcvar} to YES, and set rc_force=yes
536 # one Set ${rcvar} to YES
537 # quiet Don't output some diagnostics, and set rc_quiet=yes
539 # The following globals are used:
541 # Name Needed Purpose
542 # ---- ------ -------
543 # provide_list (gen) list of keywords provided by current rcng file
545 # name y Name of script.
547 # command n Full path to command.
548 # Not needed if ${rc_arg}_cmd is set for
551 # command_args n Optional args/shell directives for command.
553 # command_interpreter n If not empty, command is interpreted, so
554 # call check_{pidfile,process}() appropriately.
556 # extra_commands n List of extra commands supported.
558 # pidfile n If set, use check_pidfile $pidfile $command,
559 # otherwise use check_process $command.
560 # In either case, only check if $command is set.
562 # procname n Process name to check for instead of $command.
564 # rcvar n This is checked with checkyesno to determine
565 # if the action should be run.
567 # ${name}_chroot n Directory to chroot to before running ${command}
568 # Requires /usr to be mounted.
570 # ${name}_chdir n Directory to cd to before running ${command}
571 # (if not using ${name}_chroot).
573 # ${name}_flags n Arguments to call ${command} with.
574 # NOTE: $flags from the parent environment
575 # can be used to override this.
577 # ${name}_env n Environment variables to run ${command} with.
579 # ${name}_nice n Nice level to run ${command} at.
581 # ${name}_user n User to run ${command} as, using su(1) if not
582 # using ${name}_chroot.
583 # Requires /usr to be mounted.
585 # ${name}_group n Group to run chrooted ${command} as.
586 # Requires /usr to be mounted.
588 # ${name}_groups n Comma separated list of supplementary groups
589 # to run the chrooted ${command} with.
590 # Requires /usr to be mounted.
592 # ${name}_prepend n Command added before ${command}.
594 # ${rc_arg}_cmd n If set, use this as the method when invoked;
595 # Otherwise, use default command (see below)
597 # ${rc_arg}_precmd n If set, run just before performing the
598 # ${rc_arg}_cmd method in the default
599 # operation (i.e, after checking for required
600 # bits and process (non)existence).
601 # If this completes with a non-zero exit code,
602 # don't run ${rc_arg}_cmd.
604 # ${rc_arg}_postcmd n If set, run just after performing the
605 # ${rc_arg}_cmd method, if that method
606 # returned a zero exit code.
608 # required_dirs n If set, check for the existence of the given
609 # directories before running the default
612 # required_files n If set, check for the readability of the given
613 # files before running the default (re)start
616 # required_modules n If set, ensure the given kernel modules are
617 # loaded before running a (re)start command.
618 # The check and possible loads are actually
619 # done after start_precmd so that the modules
620 # aren't loaded in vain, should the precmd
621 # return a non-zero status to indicate an error.
622 # If a word in the list looks like "foo:bar",
623 # "foo" is the KLD file name and "bar" is the
624 # module name. If a word looks like "foo~bar",
625 # "foo" is the KLD file name and "bar" is a
626 # egrep(1) pattern matching the module name.
627 # Otherwise the module name is assumed to be
628 # the same as the KLD file name, which is most
629 # common. See load_kld().
631 # required_vars n If set, perform checkyesno on each of the
632 # listed variables before running the default
635 # Default behaviour for a given argument, if no override method is
638 # Argument Default behaviour
639 # -------- -----------------
640 # start if !running && checkyesno ${rcvar}
644 # rc_pid=$(check_pidfile $pidfile $command)
646 # rc_pid=$(check_process $command)
647 # kill $sig_stop $rc_pid
648 # wait_for_pids $rc_pid
649 # ($sig_stop defaults to TERM.)
651 # reload Similar to stop, except use $sig_reload instead,
652 # and doesn't wait_for_pids.
653 # $sig_reload defaults to HUP.
655 # restart Run `stop' then `start'.
657 # status Show if ${command} is running, etc.
659 # poll Wait for ${command} to exit.
661 # rcvar Display what rc.conf variable is used (if any).
663 # Variables available to methods, and after run_rc_command() has
668 # rc_arg Argument to command, after fast/force/one processing
671 # rc_flags Flags to start the default command with.
672 # Defaults to ${name}_flags, unless overridden
673 # by $flags from the environment.
674 # This variable may be changed by the precmd method.
676 # rc_pid PID of command (if appropriate)
678 # rc_fast Not empty if "fast" was provided (q.v.)
680 # rc_force Not empty if "force" was provided (q.v.)
682 # rc_quiet Not empty if "quiet" was provided
690 fast*) # "fast" prefix; don't check pid
691 rc_arg=${rc_arg#fast}
693 force*) # "force" prefix; always start
694 rc_arg=${rc_arg#force}
696 one*) # "one" prefix; set ${rcvar}=yes
700 set_provide_list $rc_arg $RC_CONFIGURED
708 if [ -z "$name" ]; then
709 err 3 'run_rc_command: $name is not set.'
712 # Don't repeat the first argument when passing additional command-
713 # line arguments to the command subroutines.
719 fast*) # "fast" prefix; don't check pid
720 rc_arg=${rc_arg#fast}
724 force*) # "force" prefix; always start
725 rc_arg=${rc_arg#force}
727 if [ -n "${rcvar}" ]; then
731 one*) # "one" prefix; set ${rcvar}=yes
733 if [ -n "${rcvar}" ]; then
737 quiet*) # "quiet" prefix; omit some messages
739 rc_arg=${rc_arg#${_rc_prefix}}
744 eval _override_command=\$${name}_program
745 if [ -n "$_override_command" ]; then
746 command=$_override_command
749 _keywords="start stop restart rcvar $extra_commands"
752 _procname=${procname:-${command}}
754 # setup pid check command if not fast
755 if [ -z "$rc_fast" -a -n "$_procname" ]; then
756 if [ -n "$pidfile" ]; then
757 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
759 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
761 if [ -n "$_pidcmd" ]; then
762 _keywords="${_keywords} status poll"
766 if [ -z "$rc_arg" ]; then
767 rc_usage "$_keywords"
770 if [ -n "$flags" ]; then # allow override from environment
773 eval rc_flags=\$${name}_flags
775 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
776 _nice=\$${name}_nice _user=\$${name}_user \
777 _group=\$${name}_group _groups=\$${name}_groups \
778 _env=\$${name}_env _prepend=\$${name}_prepend
780 if [ -n "$_user" ]; then # unset $_user if running as that user
781 if [ "$_user" = "$(id -un)" ]; then
786 # if ${rcvar} is set, and $1 is not
788 # checkyesno ${rcvar}
789 # and return if that failed
791 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
792 if ! checkyesno ${rcvar}; then
793 set_provide_list $rc_arg $RC_DISABLED
798 eval $_pidcmd # determine the pid if necessary
800 for _elem in $_keywords; do
801 if [ "$_elem" != "$rc_arg" ]; then
805 # if there's a custom ${XXX_cmd},
806 # run that instead of the default
808 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
809 _postcmd=\$${rc_arg}_postcmd
810 if [ -n "$_cmd" ]; then
811 # if the precmd failed and force
814 if [ -n "$_precmd" ]; then
815 debug "run_rc_command: evaluating ${_precmd}()."
816 eval $_precmd $rc_extra_args
819 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
822 if [ -n "$_cmd" ]; then
823 debug "run_rc_command: evaluating ${_cmd}()."
824 eval $_cmd $rc_extra_args
826 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
829 if [ -n "$_postcmd" ]; then
830 debug "run_rc_command: evaluating ${_postcmd}()."
831 eval $_postcmd $rc_extra_args
833 check_early_term $rc_arg $_return "" || return $(($?-1))
835 set_provide_list $rc_arg $_return
836 adjust_return_code $_return
840 case "$rc_arg" in # default operations...
843 if [ -n "$rc_pid" ]; then
844 echo "${name} is running as pid $rc_pid."
846 echo "${name} is not running."
852 if [ -n "$rc_pid" ]; then
853 if [ -z "$rc_quiet" ]; then
854 echo "${name} already running? (pid=$rc_pid)."
859 if [ ! -x $command ]; then
860 info "run_rc_command: cannot run ($command)."
861 set_provide_list $rc_arg $RC_FAILED
862 adjust_return_code $RC_FAILED
866 # check for required variables,
867 # directories, and files
869 for _f in $required_vars; do
870 if ! checkyesno $_f; then
871 warn "\$${_f} is not set."
872 if [ -z "$rc_force" ]; then
873 set_provide_list $rc_arg $RC_FAILED
874 adjust_return_code $RC_FAILED
879 for _f in $required_dirs; do
880 if [ ! -d "${_f}/." ]; then
881 warn "${_f} is not a directory."
882 if [ -z "$rc_force" ]; then
883 set_provide_list $rc_arg $RC_FAILED
884 adjust_return_code $RC_FAILED
889 for _f in $required_files; do
890 if [ ! -r "${_f}" ]; then
891 warn "${_f} is not readable."
892 if [ -z "$rc_force" ]; then
893 set_provide_list $rc_arg $RC_FAILED
894 adjust_return_code $RC_FAILED
899 for _f in $required_modules; do
901 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
902 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
905 if ! load_kld ${_args}; then
906 if [ -z "$rc_force" ]; then
907 set_provide_list $rc_arg $RC_FAILED
908 adjust_return_code $RC_FAILED
914 # if the precmd failed and force
917 if [ -n "${_precmd}" ]; then
918 debug "run_rc_command: evaluating ${_precmd}()."
921 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
924 # setup the command to run, and run it
926 echo "Starting ${name}."
927 if [ -n "$_chroot" ]; then
929 ${_nice:+nice -n $_nice }\
931 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
932 $_chroot $command $rc_flags $command_args"
935 ${_chdir:+cd $_chdir; }\
936 ${_nice:+nice -n $_nice }\
938 $command $rc_flags $command_args"
939 if [ -n "$_user" ]; then
940 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
942 if [ -n "$_prepend" ]; then
943 _doit="$_prepend $_doit"
947 # if the cmd failed and force
950 debug "run_rc_command: _doit: $_doit"
953 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
954 # finally, run postcmd
956 if [ -n "${_postcmd}" ]; then
957 debug "run_rc_command: evaluating ${_postcmd}()."
963 if [ -z "$rc_pid" ]; then
964 if [ -n "$pidfile" ]; then
966 "${name} not running? (check $pidfile)."
968 echo "${name} not running?"
970 set_provide_list $rc_arg $RC_STOPPED
974 # if the precmd failed and force
977 if [ -n "$_precmd" ]; then
980 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
983 # send the signal to stop
985 echo "Stopping ${name}."
986 _doit="kill -${sig_stop:-TERM} $rc_pid"
987 if [ -n "$_user" ]; then
988 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
991 # if the stop cmd failed and force
996 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
997 # wait for the command to exit,
1000 wait_for_pids $rc_pid
1001 if [ -n "$_postcmd" ]; then
1008 if [ -z "$rc_pid" ]; then
1009 if [ -n "$pidfile" ]; then
1011 "${name} not running? (check $pidfile)."
1013 echo "${name} not running?"
1015 set_provide_list $rc_arg $RC_FAILED
1018 echo "Reloading ${name} config files."
1019 if [ -n "$_precmd" ]; then
1022 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
1024 _doit="kill -${sig_reload:-HUP} $rc_pid"
1025 if [ -n "$_user" ]; then
1026 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
1030 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
1031 if [ -n "$_postcmd" ]; then
1038 if [ -n "$_precmd" ]; then
1039 eval $_precmd $rc_extra_args
1041 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
1043 # prevent restart being called more
1044 # than once by any given script
1046 if [ -n "$_rc_restart_done" ]; then
1049 _rc_restart_done=YES
1051 ( $0 ${rc_force:+force}stop $rc_extra_args )
1052 $0 ${rc_force:+force}start $rc_extra_args
1055 if [ -n "$_postcmd" ]; then
1056 eval $_postcmd $rc_extra_args
1057 adjust_return_code $?
1060 # Do not set_provide_list(), the start command above
1061 # will have done it for us and we do not know the
1062 # actual RC code to base a setting on here.
1068 if [ -n "$rc_pid" ]; then
1069 wait_for_pids $rc_pid
1075 if [ -n "$rcvar" ]; then
1076 if checkyesno ${rcvar}; then
1077 echo "\$${rcvar}=YES"
1079 echo "\$${rcvar}=NO"
1085 rc_usage "$_keywords"
1089 set_provide_list $rc_arg $_return
1090 adjust_return_code $_return
1094 echo 1>&2 "$0: unknown directive '$rc_arg'."
1095 rc_usage "$_keywords"
1100 # Helper functions for run_rc_command: common code.
1101 # They use such global variables besides the exported rc_* ones:
1104 # ------------------
1111 check_required_before "$rc_arg" || return 1
1113 if [ -n "$_precmd" ]; then
1114 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
1115 eval "$_precmd $rc_extra_args"
1118 # If precmd failed and force isn't set, request exit.
1119 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1124 check_required_after "$rc_arg" || return 1
1131 if [ -n "$_postcmd" ]; then
1132 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
1133 eval "$_postcmd $rc_extra_args"
1141 debug "run_rc_command: doit: $*"
1145 # If command failed and force isn't set, request exit.
1146 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1153 _run_rc_notrunning()
1157 if [ -n "$pidfile" ]; then
1158 _pidmsg=" (check $pidfile)."
1162 echo 1>&2 "${name} not running?${_pidmsg}"
1169 _cmd="kill -$1 $rc_pid"
1170 if [ -n "$_user" ]; then
1171 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
1177 # run_rc_script file arg
1178 # Start the script `file' with `arg', and correctly handle the
1179 # return value from the script. If `file' ends with `.sh', it's
1180 # sourced into the current environment. If `file' appears to be
1181 # a backup or scratch file, ignore it. Otherwise if it's
1182 # executable run as a child process.
1188 if [ -z "$_file" -o -z "$_arg" ]; then
1189 err 3 'USAGE: run_rc_script file arg'
1192 trap "echo 'Reboot interrupted'; exit 1" 3
1194 unset name command command_args command_interpreter \
1195 extra_commands pidfile procname \
1196 rcvar required_dirs required_files required_vars
1197 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1200 *.sh) # run in current shell
1201 set $_arg ; . $_file
1203 *[~#]|*.OLD|*.orig) # scratch file; skip
1204 warn "Ignoring scratch file $_file"
1206 *) # run in subshell
1207 if [ -x $_file ]; then
1208 if [ -n "$rc_fast_and_loose" ]; then
1209 set $_arg ; . $_file
1211 ( trap "echo 'Reboot interrupted'; exit 1" 3
1212 set $_arg ; . $_file )
1219 # Code common to scripts that need to load a kernel module
1220 # if it isn't in the kernel yet. Syntax:
1221 # load_kld [-e regex] [-m module] file
1222 # where -e or -m chooses the way to check if the module
1223 # is already loaded:
1224 # regex is egrep'd in the output from `kldstat -v',
1225 # module is passed to `kldstat -m'.
1226 # The default way is as though `-m file' were specified.
1229 local _loaded _mod _opt _re
1231 while getopts "e:m:" _opt; do
1234 m) _mod="$OPTARG" ;;
1235 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1238 shift $(($OPTIND - 1))
1239 if [ $# -ne 1 ]; then
1240 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1244 if [ -n "$_re" ]; then
1245 if kldstat -v | egrep -q -e "$_re"; then
1249 if kldstat -q -m "$_mod"; then
1254 if ! kldload "$1"; then
1255 warn "Unable to load kernel module $1"
1258 info "$1 kernel module loaded."
1261 debug "load_kld: $1 kernel module already loaded."
1268 # Change every $src in $str to $dst.
1269 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1273 local _str _src _dst _out _com
1280 for _com in ${_str}; do
1281 if [ -z "${_out}" ]; then
1284 _out="${_out}${_dst}${_com}"
1292 # Source in the configuration file for a given command.
1297 if [ -z "$_command" ]; then
1298 err 3 'USAGE: load_rc_config command'
1301 if [ -z "$_rc_conf_loaded" ]; then
1302 if [ -r /etc/defaults/rc.conf ]; then
1303 debug "Sourcing /etc/defaults/rc.conf"
1304 . /etc/defaults/rc.conf
1306 elif [ -r /etc/rc.conf ]; then
1307 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1312 if [ -f /etc/rc.conf.d/"$_command" ]; then
1313 debug "Sourcing /etc/rc.conf.d/${_command}"
1314 . /etc/rc.conf.d/"$_command"
1317 # XXX - Deprecated variable name support
1319 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
1320 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
1321 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
1322 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
1328 # Print a usage string for $0, with `commands' being a list of
1333 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
1337 echo -n 1>&2 "$_sep$_elem"
1345 # err exitval message
1346 # Display message to stderr and log to the syslog, and exit with exitval.
1353 if [ -x /usr/bin/logger ]; then
1354 logger "$0: ERROR: $*"
1356 echo 1>&2 "$0: ERROR: $*"
1362 # Display message to stderr and log to the syslog.
1366 if [ -x /usr/bin/logger ]; then
1367 logger "$0: WARNING: $*"
1369 echo 1>&2 "$0: WARNING: $*"
1374 # Display informational message to stdout and log to syslog.
1379 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1380 if [ -x /usr/bin/logger ]; then
1381 logger "$0: INFO: $*"
1390 # If debugging is enabled in rc.conf output message to stderr.
1391 # BEWARE that you don't call any subroutine that itself calls this
1397 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1398 if [ -x /usr/bin/logger ]; then
1399 logger "$0: INFO: $*"
1401 echo 1>&2 "$0: DEBUG: $*"
1407 # backup_file action file cur backup
1408 # Make a backup copy of `file' into `cur', and save the previous
1409 # version of `cur' as `backup' or use rcs for archiving.
1411 # This routine checks the value of the backup_uses_rcs variable,
1412 # which can be either YES or NO.
1414 # The `action' keyword can be one of the following:
1416 # add `file' is now being backed up (and is possibly
1417 # being reentered into the backups system). `cur'
1418 # is created and RCS files, if necessary, are
1421 # update `file' has changed and needs to be backed up.
1422 # If `cur' exists, it is copied to to `back' or
1423 # checked into RCS (if the repository file is old),
1424 # and then `file' is copied to `cur'. Another RCS
1425 # check in done here if RCS is being used.
1427 # remove `file' is no longer being tracked by the backups
1428 # system. If RCS is not being used, `cur' is moved
1429 # to `back', otherwise an empty file is checked in,
1430 # and then `cur' is removed.
1440 if checkyesno backup_uses_rcs; then
1441 _msg0="backup archive"
1444 # ensure that history file is not locked
1445 if [ -f $_cur,v ]; then
1446 rcs -q -u -U -M $_cur
1449 # ensure after switching to rcs that the
1450 # current backup is not lost
1451 if [ -f $_cur ]; then
1452 # no archive, or current newer than archive
1453 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1454 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1462 cp -p $_cpfile $_cur
1463 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1466 chown root:wheel $_cur $_cur,v
1470 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1472 chown root:wheel $_cur $_cur,v
1479 if [ -f $_cur ]; then
1482 cp -p $_cpfile $_cur
1483 chown root:wheel $_cur
1492 # devfs_mount_jail dir
1493 # Mounts a devfs file system appropriate for jails
1494 # on the directory dir.
1495 # This function returns non-zero if an error occurs.
1501 _me="devfs_mount_jail"
1503 if ! devfs_domount "$jdev" $rs; then
1504 warn "$_me: devfs was not mounted on $jdev"
1511 # Mount devfs on dir.
1512 # Returns 0 on success.
1518 _me="devfs_domount()"
1520 if [ -z "$devdir" ]; then
1521 warn "$_me: you must specify a mount-point"
1525 debug "$_me: mount-point is ($devdir)"
1526 if ! mount -t devfs devfs "$devdir"; then
1527 warn "$_me: Unable to mount devfs on $devdir"
1534 # Find scripts in local_startup directories.
1535 find_local_scripts() {
1537 for dir in ${local_startup}; do
1538 if [ -d "${dir}" ]; then
1539 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1542 *) if [ -x "$file" ]; then
1543 local_rc="${local_rc} ${file}"