1 # $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem 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
47 SYSCTL_N="${SYSCTL} -n"
48 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
50 case `${CMD_OSTYPE}` in
55 SYSCTL_W="${SYSCTL} -w"
65 # Set the variable name enabling a specific service.
66 # FreeBSD uses ${service}_enable, while NetBSD uses
67 # just the name of the service. For example:
68 # FreeBSD: sendmail_enable="YES"
69 # NetBSD : sendmail="YES"
70 # $1 - if $name is not the base to work of off, specify
81 case `${CMD_OSTYPE}` in
83 echo ${base_var}_enable
96 # Force a service to start. Intended for use by services
97 # to resolve dependency issues. It is assumed the caller
98 # has check to make sure this call is necessary
99 # $1 - filename of script, in /etc/rc.d, to run
105 info "${name} depends on ${_depend}, which will be forced to start."
106 if ! /etc/rc.d/${_depend} forcestart ; then
107 warn "Unable to force ${_depend}. It may already be running."
115 # Test $1 variable, and warn if not set to YES or NO.
116 # Return 0 if it's "yes" (et al), nonzero otherwise.
121 debug "checkyesno: $1 is set to $_value."
124 # "yes", "true", "on", or "1"
125 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
129 # "no", "false", "off", or "0"
130 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
134 warn "\$${1} is not set properly."
141 # print the list in reverse order
146 for _revfile in $*; do
147 _revlist="$_revfile $_revlist"
153 # mount_critical_filesystems type
154 # Go through the list of critical filesystems as provided in
155 # the rc.conf(5) variable $critical_filesystems_${type}, checking
156 # each one to see if it is mounted, and if it is not, mounting it.
158 mount_critical_filesystems()
160 eval _fslist=\$critical_filesystems_${1}
161 for _fs in $_fslist; do
164 while read what _on on _type type; do
165 if [ $on = $_fs ]; then
169 if [ $_ismounted = no ]; then
170 mount $_fs >/dev/null 2>&1
177 # check_pidfile pidfile procname [interpreter]
178 # Parses the first line of pidfile for a PID, and ensures
179 # that the process is running and matches procname.
180 # Prints the matching PID upon success, nothing otherwise.
181 # interpreter is optional; see _find_processes() for details.
188 if [ -z "$_pidfile" -o -z "$_procname" ]; then
189 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
191 if [ ! -f $_pidfile ]; then
192 debug "pid file {$_pidfile): not readable."
195 read _pid _junk < $_pidfile
196 if [ -z "$_pid" ]; then
197 debug "pid file {$_pidfile): no pid in file."
200 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
204 # check_process procname [interpreter]
205 # Ensures that a process (or processes) named procname is running.
206 # Prints a list of matching PIDs.
207 # interpreter is optional; see _find_processes() for details.
213 if [ -z "$_procname" ]; then
214 err 3 'USAGE: check_process procname [interpreter]'
216 _find_processes $_procname ${_interpreter:-.} '-ax'
220 # _find_processes procname interpreter psargs
221 # Search for procname in the output of ps generated by psargs.
222 # Prints the PIDs of any matching processes, space separated.
224 # If interpreter == ".", check the following variations of procname
225 # against the first word of each command:
227 # `basename procname`
228 # `basename procname` + ":"
229 # "(" + `basename procname` + ")"
231 # If interpreter != ".", read the first line of procname, remove the
232 # leading #!, normalise whitespace, append procname, and attempt to
233 # match that against each command, either as is, or with extra words
238 if [ $# -ne 3 ]; then
239 err 3 'USAGE: _find_processes procname interpreter psargs'
246 if [ $_interpreter != "." ]; then # an interpreted script
247 read _interp < $_procname # read interpreter name
248 _interp=${_interp#\#!} # strip #!
250 if [ $_interpreter != $1 ]; then
251 warn "\$command_interpreter $_interpreter != $1"
253 _interp="$* $_procname" # cleanup spaces, add _procname
255 _fp_match='case "$_argv" in
256 ${_interp}|"${_interp} "*)'
257 else # a normal daemon
258 _procnamebn=${_procname##*/}
259 _fp_args='_arg0 _argv'
260 _fp_match='case "$_arg0" in
261 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
265 ps -o "pid,command" '"$_psargs"' |
266 while read _npid '"$_fp_args"'; do
270 esac ; '"$_fp_match"'
271 echo -n "$_pref$_npid" ;
277 debug "in _find_processes: proccheck is ($_proccheck)."
282 # wait_for_pids pid [pid ...]
283 # spins until none of the pids exist
288 if [ -z "$_list" ]; then
295 if kill -0 $_j 2>/dev/null; then
296 _nlist="${_nlist}${_nlist:+ }$_j"
299 if [ -z "$_nlist" ]; then
303 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
307 if [ -n "$_prefix" ]; then
313 # run_rc_command argument
314 # Search for argument in the list of supported commands, which is:
315 # "start stop restart rcvar status poll ${extra_commands}"
316 # If there's a match, run ${argument}_cmd or the default method
319 # If argument has a given prefix, then change the operation as follows:
322 # fast Skip the pid check, and set rc_fast=yes
323 # force Set ${rcvar} to YES, and set rc_force=yes
325 # The following globals are used:
327 # Name Needed Purpose
328 # ---- ------ -------
329 # name y Name of script.
331 # command n Full path to command.
332 # Not needed if ${rc_arg}_cmd is set for
335 # command_args n Optional args/shell directives for command.
337 # command_interpreter n If not empty, command is interpreted, so
338 # call check_{pidfile,process}() appropriately.
340 # extra_commands n List of extra commands supported.
342 # pidfile n If set, use check_pidfile $pidfile $command,
343 # otherwise use check_process $command.
344 # In either case, only check if $command is set.
346 # procname n Process name to check for instead of $command.
348 # rcvar n This is checked with checkyesno to determine
349 # if the action should be run.
351 # ${name}_chroot n Directory to chroot to before running ${command}
352 # Requires /usr to be mounted.
354 # ${name}_chdir n Directory to cd to before running ${command}
355 # (if not using ${name}_chroot).
357 # ${name}_flags n Arguments to call ${command} with.
358 # NOTE: $flags from the parent environment
359 # can be used to override this.
361 # ${name}_nice n Nice level to run ${command} at.
363 # ${name}_user n User to run ${command} as, using su(1) if not
364 # using ${name}_chroot.
365 # Requires /usr to be mounted.
367 # ${name}_group n Group to run chrooted ${command} as.
368 # Requires /usr to be mounted.
370 # ${name}_groups n Comma separated list of supplementary groups
371 # to run the chrooted ${command} with.
372 # Requires /usr to be mounted.
374 # ${rc_arg}_cmd n If set, use this as the method when invoked;
375 # Otherwise, use default command (see below)
377 # ${rc_arg}_precmd n If set, run just before performing the
378 # ${rc_arg}_cmd method in the default
379 # operation (i.e, after checking for required
380 # bits and process (non)existence).
381 # If this completes with a non-zero exit code,
382 # don't run ${rc_arg}_cmd.
384 # ${rc_arg}_postcmd n If set, run just after performing the
385 # ${rc_arg}_cmd method, if that method
386 # returned a zero exit code.
388 # required_dirs n If set, check for the existence of the given
389 # directories before running the default
392 # required_files n If set, check for the readability of the given
393 # files before running the default (re)start
396 # required_vars n If set, perform checkyesno on each of the
397 # listed variables before running the default
400 # Default behaviour for a given argument, if no override method is
403 # Argument Default behaviour
404 # -------- -----------------
405 # start if !running && checkyesno ${rcvar}
409 # rc_pid=$(check_pidfile $pidfile $command)
411 # rc_pid=$(check_process $command)
412 # kill $sig_stop $rc_pid
413 # wait_for_pids $rc_pid
414 # ($sig_stop defaults to TERM.)
416 # reload Similar to stop, except use $sig_reload instead,
417 # and doesn't wait_for_pids.
418 # $sig_reload defaults to HUP.
420 # restart Run `stop' then `start'.
422 # status Show if ${command} is running, etc.
424 # poll Wait for ${command} to exit.
426 # rcvar Display what rc.conf variable is used (if any).
428 # Variables available to methods, and after run_rc_command() has
433 # rc_arg Argument to command, after fast/force processing
436 # rc_flags Flags to start the default command with.
437 # Defaults to ${name}_flags, unless overridden
438 # by $flags from the environment.
439 # This variable may be changed by the precmd method.
441 # rc_pid PID of command (if appropriate)
443 # rc_fast Not empty if "fast" was provided (q.v.)
445 # rc_force Not empty if "force" was provided (q.v.)
451 if [ -z "$name" ]; then
452 err 3 'run_rc_command: $name is not set.'
456 fast*) # "fast" prefix; don't check pid
457 rc_arg=${rc_arg#fast}
460 force*) # "force prefix; always start
461 rc_arg=${rc_arg#force}
463 if [ -n "${rcvar}" ]; then
469 eval _overide_command=\$${name}_program
470 if [ -n "$_overide_command" ]; then
471 command=$_overide_command
474 _keywords="start stop restart rcvar $extra_commands"
477 _procname=${procname:-${command}}
479 # setup pid check command if not fast
480 if [ -z "$rc_fast" -a -n "$_procname" ]; then
481 if [ -n "$pidfile" ]; then
482 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
484 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
486 if [ -n "$_pidcmd" ]; then
487 _keywords="${_keywords} status poll"
491 if [ -z "$rc_arg" ]; then
492 rc_usage "$_keywords"
495 if [ -n "$flags" ]; then # allow override from environment
498 eval rc_flags=\$${name}_flags
500 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
501 _nice=\$${name}_nice _user=\$${name}_user \
502 _group=\$${name}_group _groups=\$${name}_groups
504 if [ -n "$_user" ]; then # unset $_user if running as that user
505 if [ "$_user" = "$(id -un)" ]; then
510 # if ${rcvar} is set, and $1 is not
512 # checkyesno ${rcvar}
513 # and return if that failed
515 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
516 if ! checkyesno ${rcvar}; then
521 eval $_pidcmd # determine the pid if necessary
523 for _elem in $_keywords; do
524 if [ "$_elem" != "$rc_arg" ]; then
528 # if there's a custom ${XXX_cmd},
529 # run that instead of the default
531 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
532 _postcmd=\$${rc_arg}_postcmd
533 if [ -n "$_cmd" ]; then
534 debug "run_rc_command: using XXX_cmd functions."
535 # if the precmd failed and force
538 if ! eval $_precmd && [ -z "$rc_force" ]; then
542 if ! eval $_cmd && [ -z "$rc_force" ]; then
549 case "$rc_arg" in # default operations...
552 if [ -n "$rc_pid" ]; then
553 echo "${name} is running as pid $rc_pid."
555 echo "${name} is not running."
561 if [ -n "$rc_pid" ]; then
562 echo "${name} already running? (pid=$rc_pid)."
566 if [ ! -x $command ]; then
567 info "run_rc_command: cannot run ($command)."
571 # check for required variables,
572 # directories, and files
574 for _f in $required_vars; do
575 if ! checkyesno $_f; then
576 warn "\$${_f} is not set."
577 if [ -z "$rc_force" ]; then
582 for _f in $required_dirs; do
583 if [ ! -d "${_f}/." ]; then
584 warn "${_f} is not a directory."
585 if [ -z "$rc_force" ]; then
590 for _f in $required_files; do
591 if [ ! -r "${_f}" ]; then
592 warn "${_f} is not readable."
593 if [ -z "$rc_force" ]; then
599 # if the precmd failed and force
602 if ! eval $_precmd && [ -z "$rc_force" ]; then
606 # setup the command to run, and run it
608 echo "Starting ${name}."
609 if [ -n "$_chroot" ]; then
611 ${_nice:+nice -n $_nice }\
612 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
613 $_chroot $command $rc_flags $command_args"
616 ${_chdir:+cd $_chdir; }\
617 ${_nice:+nice -n $_nice }\
618 $command $rc_flags $command_args"
619 if [ -n "$_user" ]; then
620 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
624 # if the cmd failed and force
627 debug "run_rc_command: _doit: $_doit"
628 if ! eval $_doit && [ -z "$rc_force" ]; then
632 # finally, run postcmd
638 if [ -z "$rc_pid" ]; then
639 if [ -n "$pidfile" ]; then
641 "${name} not running? (check $pidfile)."
643 echo "${name} not running?"
648 # if the precmd failed and force
651 if ! eval $_precmd && [ -z "$rc_force" ]; then
655 # send the signal to stop
657 echo "Stopping ${name}."
658 _doit="kill -${sig_stop:-TERM} $rc_pid"
659 if [ -n "$_user" ]; then
660 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
663 # if the stop cmd failed and force
666 if ! eval $_doit && [ -z "$rc_force" ]; then
670 # wait for the command to exit,
672 wait_for_pids $rc_pid
677 if [ -z "$rc_pid" ]; then
678 if [ -n "$pidfile" ]; then
680 "${name} not running? (check $pidfile)."
682 echo "${name} not running?"
686 echo "Reloading ${name} config files."
687 if ! eval $_precmd && [ -z "$rc_force" ]; then
690 _doit="kill -${sig_reload:-HUP} $rc_pid"
691 if [ -n "$_user" ]; then
692 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
694 if ! eval $_doit && [ -z "$rc_force" ]; then
701 if ! eval $_precmd && [ -z "$rc_force" ]; then
704 # prevent restart being called more
705 # than once by any given script
707 if [ -n "$_rc_restart_done" ]; then
712 ( $0 ${rc_force:+force}stop )
713 $0 ${rc_force:+force}start
719 if [ -n "$rc_pid" ]; then
720 wait_for_pids $rc_pid
726 if [ -n "$rcvar" ]; then
727 if checkyesno ${rcvar}; then
728 echo "\$${rcvar}=YES"
736 rc_usage "$_keywords"
743 echo 1>&2 "$0: unknown directive '$rc_arg'."
744 rc_usage "$_keywords"
749 # run_rc_script file arg
750 # Start the script `file' with `arg', and correctly handle the
751 # return value from the script. If `file' ends with `.sh', it's
752 # sourced into the current environment. If `file' appears to be
753 # a backup or scratch file, ignore it. Otherwise if it's
754 # executable run as a child process.
760 if [ -z "$_file" -o -z "$_arg" ]; then
761 err 3 'USAGE: run_rc_script file arg'
764 trap "echo 'Reboot interrupted'; exit 1" 3
766 unset name command command_args command_interpreter \
767 extra_commands pidfile procname \
768 rcvar required_dirs required_files required_vars
769 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
772 *.sh) # run in current shell
775 *[~#]|*.OLD|*.orig) # scratch file; skip
776 warn "Ignoring scratch file $_file"
779 if [ -x $_file ]; then
780 if [ -n "$rc_fast_and_loose" ]; then
783 ( trap "echo 'Reboot interrupted'; exit 1" 3
784 set $_arg ; . $_file )
793 # Source in the configuration file for a given command.
798 if [ -z "$_command" ]; then
799 err 3 'USAGE: load_rc_config command'
802 if [ -z "$_rc_conf_loaded" ]; then
803 if [ -r /etc/defaults/rc.conf ]; then
804 debug "Sourcing /etc/defaults/rc.conf"
805 . /etc/defaults/rc.conf
807 elif [ -r /etc/rc.conf ]; then
808 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
813 if [ -f /etc/rc.conf.d/"$_command" ]; then
814 debug "Sourcing /etc/rc.conf.d/${_command}"
815 . /etc/rc.conf.d/"$_command"
822 # Print a usage string for $0, with `commands' being a list of
827 echo -n 1>&2 "usage: $0 [fast|force]("
831 echo -n 1>&2 "$_sep$_elem"
839 # _echo prefix message
840 # Display message preceded by "$prefix:". Log to syslog as well.
841 # XXX - syslogd may not be listening (especially if this subroutine
842 # is called at boot before syslogd has had a chance to startup).
846 [ -x /usr/bin/logger ] && /usr/bin/logger "$0: $1: $2"
851 # err exitval message
852 # Display message to stderr and log to the syslog, and exit with exitval.
859 _echo 1>&2 "ERROR" "$*"
865 # Display message to stderr and log to the syslog.
869 _echo 1>&2 "WARNING" "$*"
874 # Display informational message to stdout and log to syslog.
883 # If debugging is enabled in rc.conf output message to stderr and syslog.
884 # BEWARE that you don't call any subroutine that itself calls this
889 # This subroutine is provided as a convenience to script writers, who
890 # should enable debugging in /etc/rc.conf.
892 [ -f /etc/rc.conf ] && . /etc/rc.conf
895 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
896 _echo 1>&2 "DEBUG" "$*"
903 # backup_file action file cur backup
904 # Make a backup copy of `file' into `cur', and save the previous
905 # version of `cur' as `backup' or use rcs for archiving.
907 # This routine checks the value of the backup_uses_rcs variable,
908 # which can be either YES or NO.
910 # The `action' keyword can be one of the following:
912 # add `file' is now being backed up (and is possibly
913 # being reentered into the backups system). `cur'
914 # is created and RCS files, if necessary, are
917 # update `file' has changed and needs to be backed up.
918 # If `cur' exists, it is copied to to `back' or
919 # checked into RCS (if the repository file is old),
920 # and then `file' is copied to `cur'. Another RCS
921 # check in done here if RCS is being used.
923 # remove `file' is no longer being tracked by the backups
924 # system. If RCS is not being used, `cur' is moved
925 # to `back', otherwise an empty file is checked in,
926 # and then `cur' is removed.
936 if checkyesno backup_uses_rcs; then
937 _msg0="backup archive"
940 # ensure that history file is not locked
941 if [ -f $_cur,v ]; then
942 rcs -q -u -U -M $_cur
945 # ensure after switching to rcs that the
946 # current backup is not lost
947 if [ -f $_cur ]; then
948 # no archive, or current newer than archive
949 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
950 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
959 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
962 chown root:wheel $_cur $_cur,v
966 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
968 chown root:wheel $_cur $_cur,v
975 if [ -f $_cur ]; then
979 chown root:wheel $_cur