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
43 # Operating System dependent/independent variables
46 if [ -n "${_rc_subr_loaded}" ]; then
52 if [ -z "$_file" ]; then
55 provide_list=`rcorder -p $_file`
58 SYSCTL_N="${SYSCTL} -n"
59 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
60 OSTYPE=`${CMD_OSTYPE}`
75 # List vars matching pattern.
79 set | { while read LINE; do
82 "$LINE"|*[!a-zA-Z0-9_]*) continue ;;
89 # Set the variable name enabling a specific service.
90 # FreeBSD uses ${service}_enable, while NetBSD uses
91 # just the name of the service. For example:
92 # FreeBSD: sendmail_enable="YES"
93 # NetBSD : sendmail="YES"
94 # $1 - if $name is not the base to work of off, specify
110 # $1 should be $rc_arg (start, stop, restart, reload, etc)
111 # $2 return value $RC_*
113 # Set the rcng_* variables associated with elements in provide_list
114 # based on $1 and $2.
116 # Returns non-zero when early termination should occur, in which
117 # case the caller should return with a value of $? - 1
121 # Remember, plret is set to the early termination return code + 1,
122 # or 0 if we want to continue the operation.
124 for i in $provide_list; do
126 start$RC_RUNNING|restart$RC_RUNNING)
127 varsym -s rcng_$i=running
129 start$RC_FAILED|restart$RC_FAILED)
130 varsym -s rcng_$i=failed
132 start$RC_DISABLED|restart$RC_DISABLED|reload$RC_DISABLED)
133 varsym -s rcng_$i=disabled
135 start$RC_IRRELEVANT|restart$RC_IRRELEVANT|reload$RC_IRRELEVANT)
136 varsym -s rcng_$i=irrelevant
138 start$RC_CONFIGURED|restart$RC_CONFIGURED)
139 varsym -s rcng_$i=configured
142 varsym -s rcng_$i=disabled
145 varsym -s rcng_$i=irrelevant
148 varsym -s rcng_$i=stopped
157 # $1 should be $rc_arg (start, stop, restart, reload, etc)
158 # $2 return value $RC_*
159 # $3 $rc_force "" not to force, "anything" to force.
161 # The return code is 0 if early termination is not to occur, non-zero if
162 # it is to occur. When early termination is to occur the caller should
163 # return check_early_term()'s return code - 1. That is, early termination
164 # can occur with or without an error.
166 # The provide list will be adjusted when early termination occurs.
175 set_provide_list $1 $2
176 [ -z "$3" ] || return 0
180 set_provide_list $1 $2
181 [ -z "$3" ] || return 0
185 set_provide_list $1 $2
186 [ -z "$3" ] || return 0
196 set_provide_list $1 $2
197 [ -z "$3" ] || return 0
201 # adjust_return_code $1
203 # Convert the return code to an exit code of 0 (success) or 1 (failure)
207 if [ $1 = $RC_FAILED ]; then
214 # force_depend script
215 # Force a service to start. Intended for use by services
216 # to resolve dependency issues. It is assumed the caller
217 # has check to make sure this call is necessary
218 # $1 - filename of script, in /etc/rc.d, to run
224 info "${name} depends on ${_depend}, which will be forced to start."
225 if ! /etc/rc.d/${_depend} forcestart ; then
226 warn "Unable to force ${_depend}. It may already be running."
234 # Test $1 variable, and warn if not set to YES or NO.
235 # Return 0 if it's "yes" (et al), nonzero otherwise.
240 eval _value=\$${1%_enable}
244 eval _value=\$${1%_enable}_enable;
246 debug "checkyesno: $1 is set to $_value."
249 # "yes", "true", "on", or "1"
250 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
254 # "no", "false", "off", or "0"
255 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
259 warn "\$${1} is not set properly - see rc.conf(5)."
266 # print the list in reverse order
271 for _revfile in $*; do
272 _revlist="$_revfile $_revlist"
278 # mount_critical_filesystems type
279 # Go through the list of critical filesystems as provided in
280 # the rc.conf(5) variable $critical_filesystems_${type}, checking
281 # each one to see if it is mounted, and if it is not, mounting it.
283 mount_critical_filesystems()
285 eval _fslist=\$critical_filesystems_${1}
286 for _fs in $_fslist; do
289 while read what _on on _type type; do
290 if [ $on = $_fs ]; then
294 if [ $_ismounted = no ]; then
295 mount $_fs >/dev/null 2>&1
302 # check_pidfile pidfile procname [interpreter]
303 # Parses the first line of pidfile for a PID, and ensures
304 # that the process is running and matches procname.
305 # Prints the matching PID upon success, nothing otherwise.
306 # interpreter is optional; see _find_processes() for details.
313 if [ -z "$_pidfile" -o -z "$_procname" ]; then
314 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
316 if [ ! -f $_pidfile ]; then
317 debug "pid file {$_pidfile): not readable."
320 read _pid _junk < $_pidfile
321 if [ -z "$_pid" ]; then
322 debug "pid file {$_pidfile): no pid in file."
325 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
329 # check_process procname [interpreter]
330 # Ensures that a process (or processes) named procname is running.
331 # Prints a list of matching PIDs.
332 # interpreter is optional; see _find_processes() for details.
338 if [ -z "$_procname" ]; then
339 err 3 'USAGE: check_process procname [interpreter]'
341 _find_processes $_procname ${_interpreter:-.} '-ax'
345 # _find_processes procname interpreter psargs
346 # Search for procname in the output of ps generated by psargs.
347 # Prints the PIDs of any matching processes, space separated.
349 # If interpreter == ".", check the following variations of procname
350 # against the first word of each command:
352 # `basename procname`
353 # `basename procname` + ":"
354 # "(" + `basename procname` + ")"
356 # If interpreter != ".", read the first line of procname, remove the
357 # leading #!, normalise whitespace, append procname, and attempt to
358 # match that against each command, either as is, or with extra words
363 if [ $# -ne 3 ]; then
364 err 3 'USAGE: _find_processes procname interpreter psargs'
371 if [ $_interpreter != "." ]; then # an interpreted script
372 read _interp < $_procname # read interpreter name
373 _interp=${_interp#\#!} # strip #!
375 if [ $_interpreter != $1 ]; then
376 warn "\$command_interpreter $_interpreter != $1"
378 _interp="$* $_procname" # cleanup spaces, add _procname
379 _interpbn="$1" # the interpreter binary
381 _fp_match='case "$_argv" in
382 "${_interpbn##*/}: "$_procname*|${_interp}|"${_interp} "*)'
383 else # a normal daemon
384 _procnamebn=${_procname##*/}
385 _fp_args='_arg0 _argv'
386 _fp_match='case "$_arg0" in
387 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
391 ps -o "pid,command" '"$_psargs"' |
392 while read _npid '"$_fp_args"'; do
396 esac ; '"$_fp_match"'
397 echo -n "$_pref$_npid" ;
403 # debug "in _find_processes: proccheck is ($_proccheck)."
408 # wait_for_pids pid [pid ...]
409 # spins until none of the pids exist
414 if [ -z "$_list" ]; then
421 if kill -0 $_j 2>/dev/null; then
422 _nlist="${_nlist}${_nlist:+ }$_j"
425 if [ -z "$_nlist" ]; then
429 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
433 if [ -n "$_prefix" ]; then
439 # get_pidfile_from_conf string file
441 # Takes a string to search for in the specified file.
442 # Ignores lines with traditional comment characters.
446 # if get_pidfile_from_conf string file; then
447 # pidfile="$_pidfile_from_conf"
449 # pidfile='appropriate default'
452 get_pidfile_from_conf()
454 if [ -z "$1" -o -z "$2" ]; then
455 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
458 local string file line
460 string="$1" ; file="$2"
462 if [ ! -s "$file" ]; then
463 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
468 *[#\;]*${string}*) continue ;;
469 *${string}*) break ;;
473 if [ -n "$line" ]; then
475 _pidfile_from_conf="/${line%%[\"\;]*}"
483 # If rc_quiet is set (usually as a result of using faststart at
484 # boot time) check if rc_startmsgs is enabled.
488 if [ -n "$rc_quiet" ]; then
489 checkyesno rc_startmsgs
496 # run_rc_command argument
497 # Search for argument in the list of supported commands, which is:
498 # "start stop restart rcvar status poll ${extra_commands}"
499 # If there's a match, run ${argument}_cmd or the default method
502 # If argument has a given prefix, then change the operation as follows:
505 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
506 # force Set ${rcvar} to YES, and set rc_force=yes
507 # one Set ${rcvar} to YES
508 # quiet Don't output some diagnostics, and set rc_quiet=yes
510 # The following globals are used:
512 # Name Needed Purpose
513 # ---- ------ -------
514 # provide_list (gen) list of keywords provided by current rcng file
516 # name y Name of script.
518 # command n Full path to command.
519 # Not needed if ${rc_arg}_cmd is set for
522 # command_args n Optional args/shell directives for command.
524 # command_interpreter n If not empty, command is interpreted, so
525 # call check_{pidfile,process}() appropriately.
527 # extra_commands n List of extra commands supported.
529 # pidfile n If set, use check_pidfile $pidfile $command,
530 # otherwise use check_process $command.
531 # In either case, only check if $command is set.
533 # procname n Process name to check for instead of $command.
535 # rcvar n This is checked with checkyesno to determine
536 # if the action should be run.
538 # ${name}_chroot n Directory to chroot to before running ${command}
539 # Requires /usr to be mounted.
541 # ${name}_chdir n Directory to cd to before running ${command}
542 # (if not using ${name}_chroot).
544 # ${name}_flags n Arguments to call ${command} with.
545 # NOTE: $flags from the parent environment
546 # can be used to override this.
548 # ${name}_env n Environment variables to run ${command} with.
550 # ${name}_nice n Nice level to run ${command} at.
552 # ${name}_user n User to run ${command} as, using su(1) if not
553 # using ${name}_chroot.
554 # Requires /usr to be mounted.
556 # ${name}_group n Group to run chrooted ${command} as.
557 # Requires /usr to be mounted.
559 # ${name}_groups n Comma separated list of supplementary groups
560 # to run the chrooted ${command} with.
561 # Requires /usr to be mounted.
563 # ${name}_prepend n Command added before ${command}.
565 # ${rc_arg}_cmd n If set, use this as the method when invoked;
566 # Otherwise, use default command (see below)
568 # ${rc_arg}_precmd n If set, run just before performing the
569 # ${rc_arg}_cmd method in the default
570 # operation (i.e, after checking for required
571 # bits and process (non)existence).
572 # If this completes with a non-zero exit code,
573 # don't run ${rc_arg}_cmd.
575 # ${rc_arg}_postcmd n If set, run just after performing the
576 # ${rc_arg}_cmd method, if that method
577 # returned a zero exit code.
579 # required_dirs n If set, check for the existence of the given
580 # directories before running the default
583 # required_files n If set, check for the readability of the given
584 # files before running the default (re)start
587 # required_modules n If set, ensure the given kernel modules are
588 # loaded before running a (re)start command.
589 # The check and possible loads are actually
590 # done after start_precmd so that the modules
591 # aren't loaded in vain, should the precmd
592 # return a non-zero status to indicate a error.
593 # If a word in the list looks like "foo:bar",
594 # "foo" is the KLD file name and "bar" is the
595 # module name. If a word looks like "foo~bar",
596 # "foo" is the KLD file name and "bar" is a
597 # egrep(1) pattern matching the module name.
598 # Otherwise the module name is assumed to be
599 # the same as the KLD file name, which is most
600 # common. See load_kld().
602 # required_vars n If set, perform checkyesno on each of the
603 # listed variables before running the default
606 # Default behaviour for a given argument, if no override method is
609 # Argument Default behaviour
610 # -------- -----------------
611 # start if !running && checkyesno ${rcvar}
615 # rc_pid=$(check_pidfile $pidfile $command)
617 # rc_pid=$(check_process $command)
618 # kill $sig_stop $rc_pid
619 # wait_for_pids $rc_pid
620 # ($sig_stop defaults to TERM.)
622 # reload Similar to stop, except use $sig_reload instead,
623 # and doesn't wait_for_pids.
624 # $sig_reload defaults to HUP.
626 # restart Run `stop' then `start'.
628 # status Show if ${command} is running, etc.
630 # poll Wait for ${command} to exit.
632 # rcvar Display what rc.conf variable is used (if any).
634 # Variables available to methods, and after run_rc_command() has
639 # rc_arg Argument to command, after fast/force/one processing
642 # rc_flags Flags to start the default command with.
643 # Defaults to ${name}_flags, unless overridden
644 # by $flags from the environment.
645 # This variable may be changed by the precmd method.
647 # rc_pid PID of command (if appropriate)
649 # rc_fast Not empty if "fast" was provided (q.v.)
651 # rc_force Not empty if "force" was provided (q.v.)
653 # rc_quiet Not empty if "quiet" was provided
661 fast*) # "fast" prefix; don't check pid
662 rc_arg=${rc_arg#fast}
664 force*) # "force" prefix; always start
665 rc_arg=${rc_arg#force}
667 one*) # "one" prefix; set ${rcvar}=yes
671 set_provide_list $rc_arg $RC_CONFIGURED
679 if [ -z "$name" ]; then
680 err 3 'run_rc_command: $name is not set.'
683 # Don't repeat the first argument when passing additional command-
684 # line arguments to the command subroutines.
690 fast*) # "fast" prefix; don't check pid
691 rc_arg=${rc_arg#fast}
695 force*) # "force" prefix; always start
696 rc_arg=${rc_arg#force}
698 if [ -n "${rcvar}" ]; then
702 one*) # "one" prefix; set ${rcvar}=yes
704 if [ -n "${rcvar}" ]; then
708 quiet*) # "quiet" prefix; omit some messages
710 rc_arg=${rc_arg#${_rc_prefix}}
715 eval _override_command=\$${name}_program
716 if [ -n "$_override_command" ]; then
717 command=$_override_command
720 _keywords="start stop restart rcvar $extra_commands"
723 _procname=${procname:-${command}}
725 # setup pid check command if not fast
726 if [ -z "$rc_fast" -a -n "$_procname" ]; then
727 if [ -n "$pidfile" ]; then
728 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
730 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
732 if [ -n "$_pidcmd" ]; then
733 _keywords="${_keywords} status poll"
737 if [ -z "$rc_arg" ]; then
738 rc_usage "$_keywords"
741 if [ -n "$flags" ]; then # allow override from environment
744 eval rc_flags=\$${name}_flags
746 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
747 _nice=\$${name}_nice _user=\$${name}_user \
748 _group=\$${name}_group _groups=\$${name}_groups \
749 _env=\$${name}_env _prepend=\$${name}_prepend
751 if [ -n "$_user" ]; then # unset $_user if running as that user
752 if [ "$_user" = "$(id -un)" ]; then
757 # if ${rcvar} is set, and $1 is not
759 # checkyesno ${rcvar}
760 # and return if that failed
762 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
763 if ! checkyesno ${rcvar}; then
764 set_provide_list $rc_arg $RC_DISABLED
769 eval $_pidcmd # determine the pid if necessary
771 for _elem in $_keywords; do
772 if [ "$_elem" != "$rc_arg" ]; then
776 # if there's a custom ${XXX_cmd},
777 # run that instead of the default
779 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
780 _postcmd=\$${rc_arg}_postcmd
781 if [ -n "$_cmd" ]; then
782 # if the precmd failed and force
785 if [ -n "$_precmd" ]; then
786 debug "run_rc_command: evaluating ${_precmd}()."
787 eval $_precmd $rc_extra_args
790 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
793 if [ -n "$_cmd" ]; then
794 debug "run_rc_command: evaluating ${_cmd}()."
795 eval $_cmd $rc_extra_args
797 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
800 if [ -n "$_postcmd" ]; then
801 debug "run_rc_command: evaluating ${_postcmd}()."
802 eval $_postcmd $rc_extra_args
804 check_early_term $rc_arg $_return "" || return $(($?-1))
806 set_provide_list $rc_arg $_return
807 adjust_return_code $_return
811 case "$rc_arg" in # default operations...
814 if [ -n "$rc_pid" ]; then
815 echo "${name} is running as pid $rc_pid."
817 echo "${name} is not running."
823 if [ -n "$rc_pid" ]; then
824 if [ -z "$rc_quiet" ]; then
825 echo "${name} already running? (pid=$rc_pid)."
830 if [ ! -x $command ]; then
831 info "run_rc_command: cannot run ($command)."
832 set_provide_list $rc_arg $RC_FAILED
833 adjust_return_code $RC_FAILED
837 # check for required variables,
838 # directories, and files
840 for _f in $required_vars; do
841 if ! checkyesno $_f; then
842 warn "\$${_f} is not set."
843 if [ -z "$rc_force" ]; then
844 set_provide_list $rc_arg $RC_FAILED
845 adjust_return_code $RC_FAILED
850 for _f in $required_dirs; do
851 if [ ! -d "${_f}/." ]; then
852 warn "${_f} is not a directory."
853 if [ -z "$rc_force" ]; then
854 set_provide_list $rc_arg $RC_FAILED
855 adjust_return_code $RC_FAILED
860 for _f in $required_files; do
861 if [ ! -r "${_f}" ]; then
862 warn "${_f} is not readable."
863 if [ -z "$rc_force" ]; then
864 set_provide_list $rc_arg $RC_FAILED
865 adjust_return_code $RC_FAILED
870 for _f in $required_modules; do
872 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
873 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
876 if ! load_kld ${_args}; then
877 if [ -z "$rc_force" ]; then
878 set_provide_list $rc_arg $RC_FAILED
879 adjust_return_code $RC_FAILED
885 # if the precmd failed and force
888 if [ -n "${_precmd}" ]; then
889 debug "run_rc_command: evaluating ${_precmd}()."
892 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
895 # setup the command to run, and run it
897 echo "Starting ${name}."
898 if [ -n "$_chroot" ]; then
900 ${_nice:+nice -n $_nice }\
902 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
903 $_chroot $command $rc_flags $command_args"
906 ${_chdir:+cd $_chdir; }\
907 ${_nice:+nice -n $_nice }\
909 $command $rc_flags $command_args"
910 if [ -n "$_user" ]; then
911 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
913 if [ -n "$_prepend" ]; then
914 _doit="$_prepend $_doit"
918 # if the cmd failed and force
921 debug "run_rc_command: _doit: $_doit"
924 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
925 # finally, run postcmd
927 if [ -n "${_postcmd}" ]; then
928 debug "run_rc_command: evaluating ${_postcmd}()."
934 if [ -z "$rc_pid" ]; then
935 if [ -n "$pidfile" ]; then
937 "${name} not running? (check $pidfile)."
939 echo "${name} not running?"
941 set_provide_list $rc_arg $RC_STOPPED
945 # if the precmd failed and force
948 if [ -n "$_precmd" ]; then
951 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
954 # send the signal to stop
956 echo "Stopping ${name}."
957 _doit="kill -${sig_stop:-TERM} $rc_pid"
958 if [ -n "$_user" ]; then
959 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
962 # if the stop cmd failed and force
967 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
968 # wait for the command to exit,
971 wait_for_pids $rc_pid
972 if [ -n "$_postcmd" ]; then
979 if [ -z "$rc_pid" ]; then
980 if [ -n "$pidfile" ]; then
982 "${name} not running? (check $pidfile)."
984 echo "${name} not running?"
986 set_provide_list $rc_arg $RC_FAILED
989 echo "Reloading ${name} config files."
990 if [ -n "$_precmd" ]; then
993 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
995 _doit="kill -${sig_reload:-HUP} $rc_pid"
996 if [ -n "$_user" ]; then
997 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
1001 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
1002 if [ -n "$_postcmd" ]; then
1009 if [ -n "$_precmd" ]; then
1010 eval $_precmd $rc_extra_args
1012 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
1014 # prevent restart being called more
1015 # than once by any given script
1017 if [ -n "$_rc_restart_done" ]; then
1020 _rc_restart_done=YES
1022 ( $0 ${rc_force:+force}stop $rc_extra_args )
1023 $0 ${rc_force:+force}start $rc_extra_args
1026 if [ -n "$_postcmd" ]; then
1027 eval $_postcmd $rc_extra_args
1028 adjust_return_code $?
1031 # Do not set_provide_list(), the start command above
1032 # will have done it for us and we do not know the
1033 # actual RC code to base a setting on here.
1039 if [ -n "$rc_pid" ]; then
1040 wait_for_pids $rc_pid
1046 if [ -n "$rcvar" ]; then
1047 if checkyesno ${rcvar}; then
1048 echo "\$${rcvar}=YES"
1050 echo "\$${rcvar}=NO"
1056 rc_usage "$_keywords"
1060 set_provide_list $rc_arg $_return
1061 adjust_return_code $_return
1065 echo 1>&2 "$0: unknown directive '$rc_arg'."
1066 rc_usage "$_keywords"
1071 # Helper functions for run_rc_command: common code.
1072 # They use such global variables besides the exported rc_* ones:
1075 # ------------------
1082 check_required_before "$rc_arg" || return 1
1084 if [ -n "$_precmd" ]; then
1085 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
1086 eval "$_precmd $rc_extra_args"
1089 # If precmd failed and force isn't set, request exit.
1090 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1095 check_required_after "$rc_arg" || return 1
1102 if [ -n "$_postcmd" ]; then
1103 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
1104 eval "$_postcmd $rc_extra_args"
1112 debug "run_rc_command: doit: $*"
1116 # If command failed and force isn't set, request exit.
1117 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1124 _run_rc_notrunning()
1128 if [ -n "$pidfile" ]; then
1129 _pidmsg=" (check $pidfile)."
1133 echo 1>&2 "${name} not running?${_pidmsg}"
1140 _cmd="kill -$1 $rc_pid"
1141 if [ -n "$_user" ]; then
1142 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
1148 # run_rc_script file arg
1149 # Start the script `file' with `arg', and correctly handle the
1150 # return value from the script. If `file' ends with `.sh', it's
1151 # sourced into the current environment. If `file' appears to be
1152 # a backup or scratch file, ignore it. Otherwise if it's
1153 # executable run as a child process.
1159 if [ -z "$_file" -o -z "$_arg" ]; then
1160 err 3 'USAGE: run_rc_script file arg'
1163 trap "echo 'Reboot interrupted'; exit 1" 3
1165 unset name command command_args command_interpreter \
1166 extra_commands pidfile procname \
1167 rcvar required_dirs required_files required_vars
1168 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1171 *.sh) # run in current shell
1172 set $_arg ; . $_file
1174 *[~#]|*.OLD|*.orig) # scratch file; skip
1175 warn "Ignoring scratch file $_file"
1177 *) # run in subshell
1178 if [ -x $_file ]; then
1179 if [ -n "$rc_fast_and_loose" ]; then
1180 set $_arg ; . $_file
1182 ( trap "echo 'Reboot interrupted'; exit 1" 3
1183 set $_arg ; . $_file )
1190 # Code common to scripts that need to load a kernel module
1191 # if it isn't in the kernel yet. Syntax:
1192 # load_kld [-e regex] [-m module] file
1193 # where -e or -m chooses the way to check if the module
1194 # is already loaded:
1195 # regex is egrep'd in the output from `kldstat -v',
1196 # module is passed to `kldstat -m'.
1197 # The default way is as though `-m file' were specified.
1200 local _loaded _mod _opt _re
1202 while getopts "e:m:" _opt; do
1205 m) _mod="$OPTARG" ;;
1206 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1209 shift $(($OPTIND - 1))
1210 if [ $# -ne 1 ]; then
1211 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1215 if [ -n "$_re" ]; then
1216 if kldstat -v | egrep -q -e "$_re"; then
1220 if kldstat -q -m "$_mod"; then
1225 if ! kldload "$1"; then
1226 warn "Unable to load kernel module $1"
1229 info "$1 kernel module loaded."
1232 debug "load_kld: $1 kernel module already loaded."
1239 # Change every $src in $str to $dst.
1240 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1244 local _str _src _dst _out _com
1251 for _com in ${_str}; do
1252 if [ -z "${_out}" ]; then
1255 _out="${_out}${_dst}${_com}"
1263 # Source in the configuration file for a given command.
1268 if [ -z "$_command" ]; then
1269 err 3 'USAGE: load_rc_config command'
1272 if [ -z "$_rc_conf_loaded" ]; then
1273 if [ -r /etc/defaults/rc.conf ]; then
1274 debug "Sourcing /etc/defaults/rc.conf"
1275 . /etc/defaults/rc.conf
1277 elif [ -r /etc/rc.conf ]; then
1278 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1283 if [ -f /etc/rc.conf.d/"$_command" ]; then
1284 debug "Sourcing /etc/rc.conf.d/${_command}"
1285 . /etc/rc.conf.d/"$_command"
1288 # XXX - Deprecated variable name support
1290 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
1291 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
1292 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
1293 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
1299 # Print a usage string for $0, with `commands' being a list of
1304 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
1308 echo -n 1>&2 "$_sep$_elem"
1316 # err exitval message
1317 # Display message to stderr and log to the syslog, and exit with exitval.
1324 if [ -x /usr/bin/logger ]; then
1325 logger "$0: ERROR: $*"
1327 echo 1>&2 "$0: ERROR: $*"
1333 # Display message to stderr and log to the syslog.
1337 if [ -x /usr/bin/logger ]; then
1338 logger "$0: WARNING: $*"
1340 echo 1>&2 "$0: WARNING: $*"
1345 # Display informational message to stdout and log to syslog.
1350 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1351 if [ -x /usr/bin/logger ]; then
1352 logger "$0: INFO: $*"
1361 # If debugging is enabled in rc.conf output message to stderr.
1362 # BEWARE that you don't call any subroutine that itself calls this
1368 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1369 if [ -x /usr/bin/logger ]; then
1370 logger "$0: INFO: $*"
1372 echo 1>&2 "$0: DEBUG: $*"
1378 # backup_file action file cur backup
1379 # Make a backup copy of `file' into `cur', and save the previous
1380 # version of `cur' as `backup' or use rcs for archiving.
1382 # This routine checks the value of the backup_uses_rcs variable,
1383 # which can be either YES or NO.
1385 # The `action' keyword can be one of the following:
1387 # add `file' is now being backed up (and is possibly
1388 # being reentered into the backups system). `cur'
1389 # is created and RCS files, if necessary, are
1392 # update `file' has changed and needs to be backed up.
1393 # If `cur' exists, it is copied to to `back' or
1394 # checked into RCS (if the repository file is old),
1395 # and then `file' is copied to `cur'. Another RCS
1396 # check in done here if RCS is being used.
1398 # remove `file' is no longer being tracked by the backups
1399 # system. If RCS is not being used, `cur' is moved
1400 # to `back', otherwise an empty file is checked in,
1401 # and then `cur' is removed.
1411 if checkyesno backup_uses_rcs; then
1412 _msg0="backup archive"
1415 # ensure that history file is not locked
1416 if [ -f $_cur,v ]; then
1417 rcs -q -u -U -M $_cur
1420 # ensure after switching to rcs that the
1421 # current backup is not lost
1422 if [ -f $_cur ]; then
1423 # no archive, or current newer than archive
1424 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1425 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1433 cp -p $_cpfile $_cur
1434 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1437 chown root:wheel $_cur $_cur,v
1441 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1443 chown root:wheel $_cur $_cur,v
1450 if [ -f $_cur ]; then
1453 cp -p $_cpfile $_cur
1454 chown root:wheel $_cur
1463 # devfs_mount_jail dir
1464 # Mounts a devfs file system appropriate for jails
1465 # on the directory dir.
1466 # This function returns non-zero if an error occurs.
1472 _me="devfs_mount_jail"
1474 if ! devfs_domount "$jdev" $rs; then
1475 warn "$_me: devfs was not mounted on $jdev"
1482 # Mount devfs on dir.
1483 # Returns 0 on success.
1489 _me="devfs_domount()"
1491 if [ -z "$devdir" ]; then
1492 warn "$_me: you must specify a mount-point"
1496 debug "$_me: mount-point is ($devdir)"
1497 if ! mount -t devfs devfs "$devdir"; then
1498 warn "$_me: Unable to mount devfs on $devdir"
1505 # Find scripts in local_startup directories.
1506 find_local_scripts() {
1508 for dir in ${local_startup}; do
1509 if [ -d "${dir}" ]; then
1510 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1513 *) if [ -x "$file" ]; then
1514 local_rc="${local_rc} ${file}"