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}`
76 # Set the variable name enabling a specific service.
77 # FreeBSD uses ${service}_enable, while NetBSD uses
78 # just the name of the service. For example:
79 # FreeBSD: sendmail_enable="YES"
80 # NetBSD : sendmail="YES"
81 # $1 - if $name is not the base to work of off, specify
97 # $1 should be $rc_arg (start, stop, restart, reload, etc)
98 # $2 return value $RC_*
100 # Set the rcng_* variables associated with elements in provide_list
101 # based on $1 and $2.
103 # Returns non-zero when early termination should occur, in which
104 # case the caller should return with a value of $? - 1
108 # Remember, plret is set to the early termination return code + 1,
109 # or 0 if we want to continue the operation.
111 for i in $provide_list; do
113 start$RC_RUNNING|restart$RC_RUNNING)
114 varsym -s rcng_$i=running
116 start$RC_FAILED|restart$RC_FAILED)
117 varsym -s rcng_$i=failed
119 start$RC_DISABLED|restart$RC_DISABLED|reload$RC_DISABLED)
120 varsym -s rcng_$i=disabled
122 start$RC_IRRELEVANT|restart$RC_IRRELEVANT|reload$RC_IRRELEVANT)
123 varsym -s rcng_$i=irrelevant
125 start$RC_CONFIGURED|restart$RC_CONFIGURED)
126 varsym -s rcng_$i=configured
129 varsym -s rcng_$i=disabled
132 varsym -s rcng_$i=irrelevant
135 varsym -s rcng_$i=stopped
144 # $1 should be $rc_arg (start, stop, restart, reload, etc)
145 # $2 return value $RC_*
146 # $3 $rc_force "" not to force, "anything" to force.
148 # The return code is 0 if early termination is not to occur, non-zero if
149 # it is to occur. When early termination is to occur the caller should
150 # return check_early_term()'s return code - 1. That is, early termination
151 # can occur with or without an error.
153 # The provide list will be adjusted when early termination occurs.
162 set_provide_list $1 $2
163 [ -z "$3" ] || return 0
167 set_provide_list $1 $2
168 [ -z "$3" ] || return 0
172 set_provide_list $1 $2
173 [ -z "$3" ] || return 0
183 set_provide_list $1 $2
184 [ -z "$3" ] || return 0
188 # adjust_return_code $1
190 # Convert the return code to an exit code of 0 (success) or 1 (failure)
194 if [ $1 = $RC_FAILED ]; then
201 # force_depend script
202 # Force a service to start. Intended for use by services
203 # to resolve dependency issues. It is assumed the caller
204 # has check to make sure this call is necessary
205 # $1 - filename of script, in /etc/rc.d, to run
211 info "${name} depends on ${_depend}, which will be forced to start."
212 if ! /etc/rc.d/${_depend} forcestart ; then
213 warn "Unable to force ${_depend}. It may already be running."
221 # Test $1 variable, and warn if not set to YES or NO.
222 # Return 0 if it's "yes" (et al), nonzero otherwise.
227 eval _value=\$${1%_enable}
231 eval _value=\$${1%_enable}_enable;
233 debug "checkyesno: $1 is set to $_value."
236 # "yes", "true", "on", or "1"
237 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
241 # "no", "false", "off", or "0"
242 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
246 warn "\$${1} is not set properly - see rc.conf(5)."
253 # print the list in reverse order
258 for _revfile in $*; do
259 _revlist="$_revfile $_revlist"
265 # mount_critical_filesystems type
266 # Go through the list of critical filesystems as provided in
267 # the rc.conf(5) variable $critical_filesystems_${type}, checking
268 # each one to see if it is mounted, and if it is not, mounting it.
270 mount_critical_filesystems()
272 eval _fslist=\$critical_filesystems_${1}
273 for _fs in $_fslist; do
276 while read what _on on _type type; do
277 if [ $on = $_fs ]; then
281 if [ $_ismounted = no ]; then
282 mount $_fs >/dev/null 2>&1
289 # check_pidfile pidfile procname [interpreter]
290 # Parses the first line of pidfile for a PID, and ensures
291 # that the process is running and matches procname.
292 # Prints the matching PID upon success, nothing otherwise.
293 # interpreter is optional; see _find_processes() for details.
300 if [ -z "$_pidfile" -o -z "$_procname" ]; then
301 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
303 if [ ! -f $_pidfile ]; then
304 debug "pid file {$_pidfile): not readable."
307 read _pid _junk < $_pidfile
308 if [ -z "$_pid" ]; then
309 debug "pid file {$_pidfile): no pid in file."
312 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
316 # check_process procname [interpreter]
317 # Ensures that a process (or processes) named procname is running.
318 # Prints a list of matching PIDs.
319 # interpreter is optional; see _find_processes() for details.
325 if [ -z "$_procname" ]; then
326 err 3 'USAGE: check_process procname [interpreter]'
328 _find_processes $_procname ${_interpreter:-.} '-ax'
332 # _find_processes procname interpreter psargs
333 # Search for procname in the output of ps generated by psargs.
334 # Prints the PIDs of any matching processes, space separated.
336 # If interpreter == ".", check the following variations of procname
337 # against the first word of each command:
339 # `basename procname`
340 # `basename procname` + ":"
341 # "(" + `basename procname` + ")"
343 # If interpreter != ".", read the first line of procname, remove the
344 # leading #!, normalise whitespace, append procname, and attempt to
345 # match that against each command, either as is, or with extra words
350 if [ $# -ne 3 ]; then
351 err 3 'USAGE: _find_processes procname interpreter psargs'
358 if [ $_interpreter != "." ]; then # an interpreted script
359 read _interp < $_procname # read interpreter name
360 _interp=${_interp#\#!} # strip #!
362 if [ $_interpreter != $1 ]; then
363 warn "\$command_interpreter $_interpreter != $1"
365 _interp="$* $_procname" # cleanup spaces, add _procname
366 _interpbn="$1" # the interpreter binary
368 _fp_match='case "$_argv" in
369 "${_interpbn##*/}: "$_procname*|${_interp}|"${_interp} "*)'
370 else # a normal daemon
371 _procnamebn=${_procname##*/}
372 _fp_args='_arg0 _argv'
373 _fp_match='case "$_arg0" in
374 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
378 ps -o "pid,command" '"$_psargs"' |
379 while read _npid '"$_fp_args"'; do
383 esac ; '"$_fp_match"'
384 echo -n "$_pref$_npid" ;
390 # debug "in _find_processes: proccheck is ($_proccheck)."
395 # wait_for_pids pid [pid ...]
396 # spins until none of the pids exist
401 if [ -z "$_list" ]; then
408 if kill -0 $_j 2>/dev/null; then
409 _nlist="${_nlist}${_nlist:+ }$_j"
412 if [ -z "$_nlist" ]; then
416 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
420 if [ -n "$_prefix" ]; then
426 # get_pidfile_from_conf string file
428 # Takes a string to search for in the specified file.
429 # Ignores lines with traditional comment characters.
433 # if get_pidfile_from_conf string file; then
434 # pidfile="$_pidfile_from_conf"
436 # pidfile='appropriate default'
439 get_pidfile_from_conf()
441 if [ -z "$1" -o -z "$2" ]; then
442 err 3 "USAGE: get_pidfile_from_conf string file ($name)"
445 local string file line
447 string="$1" ; file="$2"
449 if [ ! -s "$file" ]; then
450 err 3 "get_pidfile_from_conf: $file does not exist ($name)"
455 *[#\;]*${string}*) continue ;;
456 *${string}*) break ;;
460 if [ -n "$line" ]; then
462 _pidfile_from_conf="/${line%%[\"\;]*}"
470 # If rc_quiet is set (usually as a result of using faststart at
471 # boot time) check if rc_startmsgs is enabled.
475 if [ -n "$rc_quiet" ]; then
476 checkyesno rc_startmsgs
483 # run_rc_command argument
484 # Search for argument in the list of supported commands, which is:
485 # "start stop restart rcvar status poll ${extra_commands}"
486 # If there's a match, run ${argument}_cmd or the default method
489 # If argument has a given prefix, then change the operation as follows:
492 # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes
493 # force Set ${rcvar} to YES, and set rc_force=yes
494 # one Set ${rcvar} to YES
495 # quiet Don't output some diagnostics, and set rc_quiet=yes
497 # The following globals are used:
499 # Name Needed Purpose
500 # ---- ------ -------
501 # provide_list (gen) list of keywords provided by current rcng file
503 # name y Name of script.
505 # command n Full path to command.
506 # Not needed if ${rc_arg}_cmd is set for
509 # command_args n Optional args/shell directives for command.
511 # command_interpreter n If not empty, command is interpreted, so
512 # call check_{pidfile,process}() appropriately.
514 # extra_commands n List of extra commands supported.
516 # pidfile n If set, use check_pidfile $pidfile $command,
517 # otherwise use check_process $command.
518 # In either case, only check if $command is set.
520 # procname n Process name to check for instead of $command.
522 # rcvar n This is checked with checkyesno to determine
523 # if the action should be run.
525 # ${name}_chroot n Directory to chroot to before running ${command}
526 # Requires /usr to be mounted.
528 # ${name}_chdir n Directory to cd to before running ${command}
529 # (if not using ${name}_chroot).
531 # ${name}_flags n Arguments to call ${command} with.
532 # NOTE: $flags from the parent environment
533 # can be used to override this.
535 # ${name}_env n Environment variables to run ${command} with.
537 # ${name}_nice n Nice level to run ${command} at.
539 # ${name}_user n User to run ${command} as, using su(1) if not
540 # using ${name}_chroot.
541 # Requires /usr to be mounted.
543 # ${name}_group n Group to run chrooted ${command} as.
544 # Requires /usr to be mounted.
546 # ${name}_groups n Comma separated list of supplementary groups
547 # to run the chrooted ${command} with.
548 # Requires /usr to be mounted.
550 # ${name}_prepend n Command added before ${command}.
552 # ${rc_arg}_cmd n If set, use this as the method when invoked;
553 # Otherwise, use default command (see below)
555 # ${rc_arg}_precmd n If set, run just before performing the
556 # ${rc_arg}_cmd method in the default
557 # operation (i.e, after checking for required
558 # bits and process (non)existence).
559 # If this completes with a non-zero exit code,
560 # don't run ${rc_arg}_cmd.
562 # ${rc_arg}_postcmd n If set, run just after performing the
563 # ${rc_arg}_cmd method, if that method
564 # returned a zero exit code.
566 # required_dirs n If set, check for the existence of the given
567 # directories before running the default
570 # required_files n If set, check for the readability of the given
571 # files before running the default (re)start
574 # required_modules n If set, ensure the given kernel modules are
575 # loaded before running a (re)start command.
576 # The check and possible loads are actually
577 # done after start_precmd so that the modules
578 # aren't loaded in vain, should the precmd
579 # return a non-zero status to indicate a error.
580 # If a word in the list looks like "foo:bar",
581 # "foo" is the KLD file name and "bar" is the
582 # module name. If a word looks like "foo~bar",
583 # "foo" is the KLD file name and "bar" is a
584 # egrep(1) pattern matching the module name.
585 # Otherwise the module name is assumed to be
586 # the same as the KLD file name, which is most
587 # common. See load_kld().
589 # required_vars n If set, perform checkyesno on each of the
590 # listed variables before running the default
593 # Default behaviour for a given argument, if no override method is
596 # Argument Default behaviour
597 # -------- -----------------
598 # start if !running && checkyesno ${rcvar}
602 # rc_pid=$(check_pidfile $pidfile $command)
604 # rc_pid=$(check_process $command)
605 # kill $sig_stop $rc_pid
606 # wait_for_pids $rc_pid
607 # ($sig_stop defaults to TERM.)
609 # reload Similar to stop, except use $sig_reload instead,
610 # and doesn't wait_for_pids.
611 # $sig_reload defaults to HUP.
613 # restart Run `stop' then `start'.
615 # status Show if ${command} is running, etc.
617 # poll Wait for ${command} to exit.
619 # rcvar Display what rc.conf variable is used (if any).
621 # Variables available to methods, and after run_rc_command() has
626 # rc_arg Argument to command, after fast/force/one processing
629 # rc_flags Flags to start the default command with.
630 # Defaults to ${name}_flags, unless overridden
631 # by $flags from the environment.
632 # This variable may be changed by the precmd method.
634 # rc_pid PID of command (if appropriate)
636 # rc_fast Not empty if "fast" was provided (q.v.)
638 # rc_force Not empty if "force" was provided (q.v.)
640 # rc_quiet Not empty if "quiet" was provided
648 fast*) # "fast" prefix; don't check pid
649 rc_arg=${rc_arg#fast}
651 force*) # "force" prefix; always start
652 rc_arg=${rc_arg#force}
654 one*) # "one" prefix; set ${rcvar}=yes
658 set_provide_list $rc_arg $RC_CONFIGURED
666 if [ -z "$name" ]; then
667 err 3 'run_rc_command: $name is not set.'
670 # Don't repeat the first argument when passing additional command-
671 # line arguments to the command subroutines.
677 fast*) # "fast" prefix; don't check pid
678 rc_arg=${rc_arg#fast}
682 force*) # "force" prefix; always start
683 rc_arg=${rc_arg#force}
685 if [ -n "${rcvar}" ]; then
689 one*) # "one" prefix; set ${rcvar}=yes
691 if [ -n "${rcvar}" ]; then
695 quiet*) # "quiet" prefix; omit some messages
697 rc_arg=${rc_arg#${_rc_prefix}}
702 eval _override_command=\$${name}_program
703 if [ -n "$_override_command" ]; then
704 command=$_override_command
707 _keywords="start stop restart rcvar $extra_commands"
710 _procname=${procname:-${command}}
712 # setup pid check command if not fast
713 if [ -z "$rc_fast" -a -n "$_procname" ]; then
714 if [ -n "$pidfile" ]; then
715 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
717 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
719 if [ -n "$_pidcmd" ]; then
720 _keywords="${_keywords} status poll"
724 if [ -z "$rc_arg" ]; then
725 rc_usage "$_keywords"
728 if [ -n "$flags" ]; then # allow override from environment
731 eval rc_flags=\$${name}_flags
733 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
734 _nice=\$${name}_nice _user=\$${name}_user \
735 _group=\$${name}_group _groups=\$${name}_groups \
736 _env=\$${name}_env _prepend=\$${name}_prepend
738 if [ -n "$_user" ]; then # unset $_user if running as that user
739 if [ "$_user" = "$(id -un)" ]; then
744 # if ${rcvar} is set, and $1 is not
746 # checkyesno ${rcvar}
747 # and return if that failed
749 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
750 if ! checkyesno ${rcvar}; then
751 set_provide_list $rc_arg $RC_DISABLED
756 eval $_pidcmd # determine the pid if necessary
758 for _elem in $_keywords; do
759 if [ "$_elem" != "$rc_arg" ]; then
763 # if there's a custom ${XXX_cmd},
764 # run that instead of the default
766 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
767 _postcmd=\$${rc_arg}_postcmd
768 if [ -n "$_cmd" ]; then
769 # if the precmd failed and force
772 if [ -n "$_precmd" ]; then
773 debug "run_rc_command: evaluating ${_precmd}()."
774 eval $_precmd $rc_extra_args
777 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
780 if [ -n "$_cmd" ]; then
781 debug "run_rc_command: evaluating ${_cmd}()."
782 eval $_cmd $rc_extra_args
784 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
787 if [ -n "$_postcmd" ]; then
788 debug "run_rc_command: evaluating ${_postcmd}()."
789 eval $_postcmd $rc_extra_args
791 check_early_term $rc_arg $_return "" || return $(($?-1))
793 set_provide_list $rc_arg $_return
794 adjust_return_code $_return
798 case "$rc_arg" in # default operations...
801 if [ -n "$rc_pid" ]; then
802 echo "${name} is running as pid $rc_pid."
804 echo "${name} is not running."
810 if [ -n "$rc_pid" ]; then
811 if [ -z "$rc_quiet" ]; then
812 echo "${name} already running? (pid=$rc_pid)."
817 if [ ! -x $command ]; then
818 info "run_rc_command: cannot run ($command)."
819 set_provide_list $rc_arg $RC_FAILED
820 adjust_return_code $RC_FAILED
824 # check for required variables,
825 # directories, and files
827 for _f in $required_vars; do
828 if ! checkyesno $_f; then
829 warn "\$${_f} is not set."
830 if [ -z "$rc_force" ]; then
831 set_provide_list $rc_arg $RC_FAILED
832 adjust_return_code $RC_FAILED
837 for _f in $required_dirs; do
838 if [ ! -d "${_f}/." ]; then
839 warn "${_f} is not a directory."
840 if [ -z "$rc_force" ]; then
841 set_provide_list $rc_arg $RC_FAILED
842 adjust_return_code $RC_FAILED
847 for _f in $required_files; do
848 if [ ! -r "${_f}" ]; then
849 warn "${_f} is not readable."
850 if [ -z "$rc_force" ]; then
851 set_provide_list $rc_arg $RC_FAILED
852 adjust_return_code $RC_FAILED
857 for _f in $required_modules; do
859 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;;
860 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;;
863 if ! load_kld ${_args}; then
864 if [ -z "$rc_force" ]; then
865 set_provide_list $rc_arg $RC_FAILED
866 adjust_return_code $RC_FAILED
872 # if the precmd failed and force
875 if [ -n "${_precmd}" ]; then
876 debug "run_rc_command: evaluating ${_precmd}()."
879 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
882 # setup the command to run, and run it
884 echo "Starting ${name}."
885 if [ -n "$_chroot" ]; then
887 ${_nice:+nice -n $_nice }\
889 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
890 $_chroot $command $rc_flags $command_args"
893 ${_chdir:+cd $_chdir; }\
894 ${_nice:+nice -n $_nice }\
896 $command $rc_flags $command_args"
897 if [ -n "$_user" ]; then
898 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
900 if [ -n "$_prepend" ]; then
901 _doit="$_prepend $_doit"
905 # if the cmd failed and force
908 debug "run_rc_command: _doit: $_doit"
911 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
912 # finally, run postcmd
914 if [ -n "${_postcmd}" ]; then
915 debug "run_rc_command: evaluating ${_postcmd}()."
921 if [ -z "$rc_pid" ]; then
922 if [ -n "$pidfile" ]; then
924 "${name} not running? (check $pidfile)."
926 echo "${name} not running?"
928 set_provide_list $rc_arg $RC_STOPPED
932 # if the precmd failed and force
935 if [ -n "$_precmd" ]; then
938 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
941 # send the signal to stop
943 echo "Stopping ${name}."
944 _doit="kill -${sig_stop:-TERM} $rc_pid"
945 if [ -n "$_user" ]; then
946 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
949 # if the stop cmd failed and force
954 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
955 # wait for the command to exit,
958 wait_for_pids $rc_pid
959 if [ -n "$_postcmd" ]; then
966 if [ -z "$rc_pid" ]; then
967 if [ -n "$pidfile" ]; then
969 "${name} not running? (check $pidfile)."
971 echo "${name} not running?"
973 set_provide_list $rc_arg $RC_FAILED
976 echo "Reloading ${name} config files."
977 if [ -n "$_precmd" ]; then
980 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
982 _doit="kill -${sig_reload:-HUP} $rc_pid"
983 if [ -n "$_user" ]; then
984 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
988 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
989 if [ -n "$_postcmd" ]; then
996 if [ -n "$_precmd" ]; then
997 eval $_precmd $rc_extra_args
999 check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
1001 # prevent restart being called more
1002 # than once by any given script
1004 if [ -n "$_rc_restart_done" ]; then
1007 _rc_restart_done=YES
1009 ( $0 ${rc_force:+force}stop $rc_extra_args )
1010 $0 ${rc_force:+force}start $rc_extra_args
1013 if [ -n "$_postcmd" ]; then
1014 eval $_postcmd $rc_extra_args
1015 adjust_return_code $?
1018 # Do not set_provide_list(), the start command above
1019 # will have done it for us and we do not know the
1020 # actual RC code to base a setting on here.
1026 if [ -n "$rc_pid" ]; then
1027 wait_for_pids $rc_pid
1033 if [ -n "$rcvar" ]; then
1034 if checkyesno ${rcvar}; then
1035 echo "\$${rcvar}=YES"
1037 echo "\$${rcvar}=NO"
1043 rc_usage "$_keywords"
1047 set_provide_list $rc_arg $_return
1048 adjust_return_code $_return
1052 echo 1>&2 "$0: unknown directive '$rc_arg'."
1053 rc_usage "$_keywords"
1058 # Helper functions for run_rc_command: common code.
1059 # They use such global variables besides the exported rc_* ones:
1062 # ------------------
1069 check_required_before "$rc_arg" || return 1
1071 if [ -n "$_precmd" ]; then
1072 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
1073 eval "$_precmd $rc_extra_args"
1076 # If precmd failed and force isn't set, request exit.
1077 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1082 check_required_after "$rc_arg" || return 1
1089 if [ -n "$_postcmd" ]; then
1090 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
1091 eval "$_postcmd $rc_extra_args"
1099 debug "run_rc_command: doit: $*"
1103 # If command failed and force isn't set, request exit.
1104 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
1111 _run_rc_notrunning()
1115 if [ -n "$pidfile" ]; then
1116 _pidmsg=" (check $pidfile)."
1120 echo 1>&2 "${name} not running?${_pidmsg}"
1127 _cmd="kill -$1 $rc_pid"
1128 if [ -n "$_user" ]; then
1129 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
1135 # run_rc_script file arg
1136 # Start the script `file' with `arg', and correctly handle the
1137 # return value from the script. If `file' ends with `.sh', it's
1138 # sourced into the current environment. If `file' appears to be
1139 # a backup or scratch file, ignore it. Otherwise if it's
1140 # executable run as a child process.
1146 if [ -z "$_file" -o -z "$_arg" ]; then
1147 err 3 'USAGE: run_rc_script file arg'
1150 trap "echo 'Reboot interrupted'; exit 1" 3
1152 unset name command command_args command_interpreter \
1153 extra_commands pidfile procname \
1154 rcvar required_dirs required_files required_vars
1155 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1158 *.sh) # run in current shell
1159 set $_arg ; . $_file
1161 *[~#]|*.OLD|*.orig) # scratch file; skip
1162 warn "Ignoring scratch file $_file"
1164 *) # run in subshell
1165 if [ -x $_file ]; then
1166 if [ -n "$rc_fast_and_loose" ]; then
1167 set $_arg ; . $_file
1169 ( trap "echo 'Reboot interrupted'; exit 1" 3
1170 set $_arg ; . $_file )
1177 # Code common to scripts that need to load a kernel module
1178 # if it isn't in the kernel yet. Syntax:
1179 # load_kld [-e regex] [-m module] file
1180 # where -e or -m chooses the way to check if the module
1181 # is already loaded:
1182 # regex is egrep'd in the output from `kldstat -v',
1183 # module is passed to `kldstat -m'.
1184 # The default way is as though `-m file' were specified.
1187 local _loaded _mod _opt _re
1189 while getopts "e:m:" _opt; do
1192 m) _mod="$OPTARG" ;;
1193 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
1196 shift $(($OPTIND - 1))
1197 if [ $# -ne 1 ]; then
1198 err 3 'USAGE: load_kld [-e regex] [-m module] file'
1202 if [ -n "$_re" ]; then
1203 if kldstat -v | egrep -q -e "$_re"; then
1207 if kldstat -q -m "$_mod"; then
1212 if ! kldload "$1"; then
1213 warn "Unable to load kernel module $1"
1216 info "$1 kernel module loaded."
1219 debug "load_kld: $1 kernel module already loaded."
1226 # Change every $src in $str to $dst.
1227 # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
1231 local _str _src _dst _out _com
1238 for _com in ${_str}; do
1239 if [ -z "${_out}" ]; then
1242 _out="${_out}${_dst}${_com}"
1250 # Source in the configuration file for a given command.
1255 if [ -z "$_command" ]; then
1256 err 3 'USAGE: load_rc_config command'
1259 if [ -z "$_rc_conf_loaded" ]; then
1260 if [ -r /etc/defaults/rc.conf ]; then
1261 debug "Sourcing /etc/defaults/rc.conf"
1262 . /etc/defaults/rc.conf
1264 elif [ -r /etc/rc.conf ]; then
1265 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1270 if [ -f /etc/rc.conf.d/"$_command" ]; then
1271 debug "Sourcing /etc/rc.conf.d/${_command}"
1272 . /etc/rc.conf.d/"$_command"
1275 # XXX - Deprecated variable name support
1277 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
1278 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
1279 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
1280 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
1281 [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
1282 [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
1288 # Print a usage string for $0, with `commands' being a list of
1293 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
1297 echo -n 1>&2 "$_sep$_elem"
1305 # err exitval message
1306 # Display message to stderr and log to the syslog, and exit with exitval.
1313 if [ -x /usr/bin/logger ]; then
1314 logger "$0: ERROR: $*"
1316 echo 1>&2 "$0: ERROR: $*"
1322 # Display message to stderr and log to the syslog.
1326 if [ -x /usr/bin/logger ]; then
1327 logger "$0: WARNING: $*"
1329 echo 1>&2 "$0: WARNING: $*"
1334 # Display informational message to stdout and log to syslog.
1339 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1340 if [ -x /usr/bin/logger ]; then
1341 logger "$0: INFO: $*"
1350 # If debugging is enabled in rc.conf output message to stderr.
1351 # BEWARE that you don't call any subroutine that itself calls this
1357 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
1358 if [ -x /usr/bin/logger ]; then
1359 logger "$0: INFO: $*"
1361 echo 1>&2 "$0: DEBUG: $*"
1367 # backup_file action file cur backup
1368 # Make a backup copy of `file' into `cur', and save the previous
1369 # version of `cur' as `backup' or use rcs for archiving.
1371 # This routine checks the value of the backup_uses_rcs variable,
1372 # which can be either YES or NO.
1374 # The `action' keyword can be one of the following:
1376 # add `file' is now being backed up (and is possibly
1377 # being reentered into the backups system). `cur'
1378 # is created and RCS files, if necessary, are
1381 # update `file' has changed and needs to be backed up.
1382 # If `cur' exists, it is copied to to `back' or
1383 # checked into RCS (if the repository file is old),
1384 # and then `file' is copied to `cur'. Another RCS
1385 # check in done here if RCS is being used.
1387 # remove `file' is no longer being tracked by the backups
1388 # system. If RCS is not being used, `cur' is moved
1389 # to `back', otherwise an empty file is checked in,
1390 # and then `cur' is removed.
1400 if checkyesno backup_uses_rcs; then
1401 _msg0="backup archive"
1404 # ensure that history file is not locked
1405 if [ -f $_cur,v ]; then
1406 rcs -q -u -U -M $_cur
1409 # ensure after switching to rcs that the
1410 # current backup is not lost
1411 if [ -f $_cur ]; then
1412 # no archive, or current newer than archive
1413 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1414 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1422 cp -p $_cpfile $_cur
1423 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1426 chown root:wheel $_cur $_cur,v
1430 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1432 chown root:wheel $_cur $_cur,v
1439 if [ -f $_cur ]; then
1442 cp -p $_cpfile $_cur
1443 chown root:wheel $_cur
1452 # devfs_mount_jail dir
1453 # Mounts a devfs file system appropriate for jails
1454 # on the directory dir.
1455 # This function returns non-zero if an error occurs.
1461 _me="devfs_mount_jail"
1463 if ! devfs_domount "$jdev" $rs; then
1464 warn "$_me: devfs was not mounted on $jdev"
1471 # Mount devfs on dir.
1472 # Returns 0 on success.
1478 _me="devfs_domount()"
1480 if [ -z "$devdir" ]; then
1481 warn "$_me: you must specify a mount-point"
1485 debug "$_me: mount-point is ($devdir)"
1486 if ! mount -t devfs devfs "$devdir"; then
1487 warn "$_me: Unable to mount devfs on $devdir"
1494 # Find scripts in local_startup directories.
1495 find_local_scripts() {
1497 for dir in ${local_startup}; do
1498 if [ -d "${dir}" ]; then
1499 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
1502 *) if [ -x "$file" ]; then
1503 local_rc="${local_rc} ${file}"