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"
49 OSTYPE=`${CMD_OSTYPE}`
56 SYSCTL_W="${SYSCTL} -w"
66 # Set the variable name enabling a specific service.
67 # FreeBSD uses ${service}_enable, while NetBSD uses
68 # just the name of the service. For example:
69 # FreeBSD: sendmail_enable="YES"
70 # NetBSD : sendmail="YES"
71 # $1 - if $name is not the base to work of off, specify
84 echo ${base_var}_enable
97 # Force a service to start. Intended for use by services
98 # to resolve dependency issues. It is assumed the caller
99 # has check to make sure this call is necessary
100 # $1 - filename of script, in /etc/rc.d, to run
106 info "${name} depends on ${_depend}, which will be forced to start."
107 if ! /etc/rc.d/${_depend} forcestart ; then
108 warn "Unable to force ${_depend}. It may already be running."
116 # Test $1 variable, and warn if not set to YES or NO.
117 # Return 0 if it's "yes" (et al), nonzero otherwise.
122 debug "checkyesno: $1 is set to $_value."
125 # "yes", "true", "on", or "1"
126 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
130 # "no", "false", "off", or "0"
131 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
135 warn "\$${1} is not set properly - see rc.conf(5)."
142 # print the list in reverse order
147 for _revfile in $*; do
148 _revlist="$_revfile $_revlist"
154 # mount_critical_filesystems type
155 # Go through the list of critical filesystems as provided in
156 # the rc.conf(5) variable $critical_filesystems_${type}, checking
157 # each one to see if it is mounted, and if it is not, mounting it.
159 mount_critical_filesystems()
161 eval _fslist=\$critical_filesystems_${1}
162 for _fs in $_fslist; do
165 while read what _on on _type type; do
166 if [ $on = $_fs ]; then
170 if [ $_ismounted = no ]; then
171 mount $_fs >/dev/null 2>&1
178 # check_pidfile pidfile procname [interpreter]
179 # Parses the first line of pidfile for a PID, and ensures
180 # that the process is running and matches procname.
181 # Prints the matching PID upon success, nothing otherwise.
182 # interpreter is optional; see _find_processes() for details.
189 if [ -z "$_pidfile" -o -z "$_procname" ]; then
190 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
192 if [ ! -f $_pidfile ]; then
193 debug "pid file {$_pidfile): not readable."
196 read _pid _junk < $_pidfile
197 if [ -z "$_pid" ]; then
198 debug "pid file {$_pidfile): no pid in file."
201 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
205 # check_process procname [interpreter]
206 # Ensures that a process (or processes) named procname is running.
207 # Prints a list of matching PIDs.
208 # interpreter is optional; see _find_processes() for details.
214 if [ -z "$_procname" ]; then
215 err 3 'USAGE: check_process procname [interpreter]'
217 _find_processes $_procname ${_interpreter:-.} '-ax'
221 # _find_processes procname interpreter psargs
222 # Search for procname in the output of ps generated by psargs.
223 # Prints the PIDs of any matching processes, space separated.
225 # If interpreter == ".", check the following variations of procname
226 # against the first word of each command:
228 # `basename procname`
229 # `basename procname` + ":"
230 # "(" + `basename procname` + ")"
232 # If interpreter != ".", read the first line of procname, remove the
233 # leading #!, normalise whitespace, append procname, and attempt to
234 # match that against each command, either as is, or with extra words
239 if [ $# -ne 3 ]; then
240 err 3 'USAGE: _find_processes procname interpreter psargs'
247 if [ $_interpreter != "." ]; then # an interpreted script
248 read _interp < $_procname # read interpreter name
249 _interp=${_interp#\#!} # strip #!
251 if [ $_interpreter != $1 ]; then
252 warn "\$command_interpreter $_interpreter != $1"
254 _interp="$* $_procname" # cleanup spaces, add _procname
256 _fp_match='case "$_argv" in
257 ${_interp}|"${_interp} "*)'
258 else # a normal daemon
259 _procnamebn=${_procname##*/}
260 _fp_args='_arg0 _argv'
261 _fp_match='case "$_arg0" in
262 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
266 ps -o "pid,command" '"$_psargs"' |
267 while read _npid '"$_fp_args"'; do
271 esac ; '"$_fp_match"'
272 echo -n "$_pref$_npid" ;
278 debug "in _find_processes: proccheck is ($_proccheck)."
283 # wait_for_pids pid [pid ...]
284 # spins until none of the pids exist
289 if [ -z "$_list" ]; then
296 if kill -0 $_j 2>/dev/null; then
297 _nlist="${_nlist}${_nlist:+ }$_j"
300 if [ -z "$_nlist" ]; then
304 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
308 if [ -n "$_prefix" ]; then
314 # run_rc_command argument
315 # Search for argument in the list of supported commands, which is:
316 # "start stop restart rcvar status poll ${extra_commands}"
317 # If there's a match, run ${argument}_cmd or the default method
320 # If argument has a given prefix, then change the operation as follows:
323 # fast Skip the pid check, and set rc_fast=yes
324 # force Set ${rcvar} to YES, and set rc_force=yes
326 # The following globals are used:
328 # Name Needed Purpose
329 # ---- ------ -------
330 # name y Name of script.
332 # command n Full path to command.
333 # Not needed if ${rc_arg}_cmd is set for
336 # command_args n Optional args/shell directives for command.
338 # command_interpreter n If not empty, command is interpreted, so
339 # call check_{pidfile,process}() appropriately.
341 # extra_commands n List of extra commands supported.
343 # pidfile n If set, use check_pidfile $pidfile $command,
344 # otherwise use check_process $command.
345 # In either case, only check if $command is set.
347 # procname n Process name to check for instead of $command.
349 # rcvar n This is checked with checkyesno to determine
350 # if the action should be run.
352 # ${name}_chroot n Directory to chroot to before running ${command}
353 # Requires /usr to be mounted.
355 # ${name}_chdir n Directory to cd to before running ${command}
356 # (if not using ${name}_chroot).
358 # ${name}_flags n Arguments to call ${command} with.
359 # NOTE: $flags from the parent environment
360 # can be used to override this.
362 # ${name}_nice n Nice level to run ${command} at.
364 # ${name}_user n User to run ${command} as, using su(1) if not
365 # using ${name}_chroot.
366 # Requires /usr to be mounted.
368 # ${name}_group n Group to run chrooted ${command} as.
369 # Requires /usr to be mounted.
371 # ${name}_groups n Comma separated list of supplementary groups
372 # to run the chrooted ${command} with.
373 # Requires /usr to be mounted.
375 # ${rc_arg}_cmd n If set, use this as the method when invoked;
376 # Otherwise, use default command (see below)
378 # ${rc_arg}_precmd n If set, run just before performing the
379 # ${rc_arg}_cmd method in the default
380 # operation (i.e, after checking for required
381 # bits and process (non)existence).
382 # If this completes with a non-zero exit code,
383 # don't run ${rc_arg}_cmd.
385 # ${rc_arg}_postcmd n If set, run just after performing the
386 # ${rc_arg}_cmd method, if that method
387 # returned a zero exit code.
389 # required_dirs n If set, check for the existence of the given
390 # directories before running the default
393 # required_files n If set, check for the readability of the given
394 # files before running the default (re)start
397 # required_vars n If set, perform checkyesno on each of the
398 # listed variables before running the default
401 # Default behaviour for a given argument, if no override method is
404 # Argument Default behaviour
405 # -------- -----------------
406 # start if !running && checkyesno ${rcvar}
410 # rc_pid=$(check_pidfile $pidfile $command)
412 # rc_pid=$(check_process $command)
413 # kill $sig_stop $rc_pid
414 # wait_for_pids $rc_pid
415 # ($sig_stop defaults to TERM.)
417 # reload Similar to stop, except use $sig_reload instead,
418 # and doesn't wait_for_pids.
419 # $sig_reload defaults to HUP.
421 # restart Run `stop' then `start'.
423 # status Show if ${command} is running, etc.
425 # poll Wait for ${command} to exit.
427 # rcvar Display what rc.conf variable is used (if any).
429 # Variables available to methods, and after run_rc_command() has
434 # rc_arg Argument to command, after fast/force processing
437 # rc_flags Flags to start the default command with.
438 # Defaults to ${name}_flags, unless overridden
439 # by $flags from the environment.
440 # This variable may be changed by the precmd method.
442 # rc_pid PID of command (if appropriate)
444 # rc_fast Not empty if "fast" was provided (q.v.)
446 # rc_force Not empty if "force" was provided (q.v.)
452 if [ -z "$name" ]; then
453 err 3 'run_rc_command: $name is not set.'
457 fast*) # "fast" prefix; don't check pid
458 rc_arg=${rc_arg#fast}
461 force*) # "force prefix; always start
462 rc_arg=${rc_arg#force}
464 if [ -n "${rcvar}" ]; then
470 eval _overide_command=\$${name}_program
471 if [ -n "$_overide_command" ]; then
472 command=$_overide_command
475 _keywords="start stop restart rcvar $extra_commands"
478 _procname=${procname:-${command}}
480 # setup pid check command if not fast
481 if [ -z "$rc_fast" -a -n "$_procname" ]; then
482 if [ -n "$pidfile" ]; then
483 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
485 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
487 if [ -n "$_pidcmd" ]; then
488 _keywords="${_keywords} status poll"
492 if [ -z "$rc_arg" ]; then
493 rc_usage "$_keywords"
496 if [ -n "$flags" ]; then # allow override from environment
499 eval rc_flags=\$${name}_flags
501 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
502 _nice=\$${name}_nice _user=\$${name}_user \
503 _group=\$${name}_group _groups=\$${name}_groups
505 if [ -n "$_user" ]; then # unset $_user if running as that user
506 if [ "$_user" = "$(id -un)" ]; then
511 # if ${rcvar} is set, and $1 is not
513 # checkyesno ${rcvar}
514 # and return if that failed
516 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
517 if ! checkyesno ${rcvar}; then
522 eval $_pidcmd # determine the pid if necessary
524 for _elem in $_keywords; do
525 if [ "$_elem" != "$rc_arg" ]; then
529 # if there's a custom ${XXX_cmd},
530 # run that instead of the default
532 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
533 _postcmd=\$${rc_arg}_postcmd
534 if [ -n "$_cmd" ]; then
535 debug "run_rc_command: using XXX_cmd functions."
536 # if the precmd failed and force
539 if ! eval $_precmd && [ -z "$rc_force" ]; then
543 if ! eval $_cmd && [ -z "$rc_force" ]; then
550 case "$rc_arg" in # default operations...
553 if [ -n "$rc_pid" ]; then
554 echo "${name} is running as pid $rc_pid."
556 echo "${name} is not running."
562 if [ -n "$rc_pid" ]; then
563 echo "${name} already running? (pid=$rc_pid)."
567 if [ ! -x $command ]; then
568 info "run_rc_command: cannot run ($command)."
572 # check for required variables,
573 # directories, and files
575 for _f in $required_vars; do
576 if ! checkyesno $_f; then
577 warn "\$${_f} is not set."
578 if [ -z "$rc_force" ]; then
583 for _f in $required_dirs; do
584 if [ ! -d "${_f}/." ]; then
585 warn "${_f} is not a directory."
586 if [ -z "$rc_force" ]; then
591 for _f in $required_files; do
592 if [ ! -r "${_f}" ]; then
593 warn "${_f} is not readable."
594 if [ -z "$rc_force" ]; then
600 # if the precmd failed and force
603 if ! eval $_precmd && [ -z "$rc_force" ]; then
607 # setup the command to run, and run it
609 echo "Starting ${name}."
610 if [ -n "$_chroot" ]; then
612 ${_nice:+nice -n $_nice }\
613 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
614 $_chroot $command $rc_flags $command_args"
617 ${_chdir:+cd $_chdir; }\
618 ${_nice:+nice -n $_nice }\
619 $command $rc_flags $command_args"
620 if [ -n "$_user" ]; then
621 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
625 # if the cmd failed and force
628 debug "run_rc_command: _doit: $_doit"
629 if ! eval $_doit && [ -z "$rc_force" ]; then
633 # finally, run postcmd
639 if [ -z "$rc_pid" ]; then
640 if [ -n "$pidfile" ]; then
642 "${name} not running? (check $pidfile)."
644 echo "${name} not running?"
649 # if the precmd failed and force
652 if ! eval $_precmd && [ -z "$rc_force" ]; then
656 # send the signal to stop
658 echo "Stopping ${name}."
659 _doit="kill -${sig_stop:-TERM} $rc_pid"
660 if [ -n "$_user" ]; then
661 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
664 # if the stop cmd failed and force
667 if ! eval $_doit && [ -z "$rc_force" ]; then
671 # wait for the command to exit,
673 wait_for_pids $rc_pid
678 if [ -z "$rc_pid" ]; then
679 if [ -n "$pidfile" ]; then
681 "${name} not running? (check $pidfile)."
683 echo "${name} not running?"
687 echo "Reloading ${name} config files."
688 if ! eval $_precmd && [ -z "$rc_force" ]; then
691 _doit="kill -${sig_reload:-HUP} $rc_pid"
692 if [ -n "$_user" ]; then
693 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
695 if ! eval $_doit && [ -z "$rc_force" ]; then
702 if ! eval $_precmd && [ -z "$rc_force" ]; then
705 # prevent restart being called more
706 # than once by any given script
708 if [ -n "$_rc_restart_done" ]; then
713 ( $0 ${rc_force:+force}stop )
714 $0 ${rc_force:+force}start
720 if [ -n "$rc_pid" ]; then
721 wait_for_pids $rc_pid
727 if [ -n "$rcvar" ]; then
728 if checkyesno ${rcvar}; then
729 echo "\$${rcvar}=YES"
737 rc_usage "$_keywords"
744 echo 1>&2 "$0: unknown directive '$rc_arg'."
745 rc_usage "$_keywords"
750 # run_rc_script file arg
751 # Start the script `file' with `arg', and correctly handle the
752 # return value from the script. If `file' ends with `.sh', it's
753 # sourced into the current environment. If `file' appears to be
754 # a backup or scratch file, ignore it. Otherwise if it's
755 # executable run as a child process.
761 if [ -z "$_file" -o -z "$_arg" ]; then
762 err 3 'USAGE: run_rc_script file arg'
765 trap "echo 'Reboot interrupted'; exit 1" 3
767 unset name command command_args command_interpreter \
768 extra_commands pidfile procname \
769 rcvar required_dirs required_files required_vars
770 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
773 *.sh) # run in current shell
776 *[~#]|*.OLD|*.orig) # scratch file; skip
777 warn "Ignoring scratch file $_file"
780 if [ -x $_file ]; then
781 if [ -n "$rc_fast_and_loose" ]; then
784 ( trap "echo 'Reboot interrupted'; exit 1" 3
785 set $_arg ; . $_file )
794 # Source in the configuration file for a given command.
799 if [ -z "$_command" ]; then
800 err 3 'USAGE: load_rc_config command'
803 if [ -z "$_rc_conf_loaded" ]; then
804 if [ -r /etc/defaults/rc.conf ]; then
805 debug "Sourcing /etc/defaults/rc.conf"
806 . /etc/defaults/rc.conf
808 elif [ -r /etc/rc.conf ]; then
809 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
814 if [ -f /etc/rc.conf.d/"$_command" ]; then
815 debug "Sourcing /etc/rc.conf.d/${_command}"
816 . /etc/rc.conf.d/"$_command"
819 # XXX - Deprecated variable name support
823 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
824 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
825 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
826 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
827 [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
828 [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
829 [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
837 # Print a usage string for $0, with `commands' being a list of
842 echo -n 1>&2 "Usage: $0 [fast|force]("
846 echo -n 1>&2 "$_sep$_elem"
854 # err exitval message
855 # Display message to stderr and log to the syslog, and exit with exitval.
862 if [ -x /usr/bin/logger ]; then
863 logger "$0: ERROR: $*"
865 echo 1>&2 "$0: ERROR: $*"
871 # Display message to stderr and log to the syslog.
875 if [ -x /usr/bin/logger ]; then
876 logger "$0: WARNING: $*"
878 echo 1>&2 "$0: WARNING: $*"
883 # Display informational message to stdout and log to syslog.
887 if [ -x /usr/bin/logger ]; then
888 logger "$0: INFO: $*"
895 # If debugging is enabled in rc.conf output message to stderr.
896 # BEWARE that you don't call any subroutine that itself calls this
902 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
903 if [ -x /usr/bin/logger ]; then
904 logger "$0: INFO: $*"
906 echo 1>&2 "$0: DEBUG: $*"
912 # backup_file action file cur backup
913 # Make a backup copy of `file' into `cur', and save the previous
914 # version of `cur' as `backup' or use rcs for archiving.
916 # This routine checks the value of the backup_uses_rcs variable,
917 # which can be either YES or NO.
919 # The `action' keyword can be one of the following:
921 # add `file' is now being backed up (and is possibly
922 # being reentered into the backups system). `cur'
923 # is created and RCS files, if necessary, are
926 # update `file' has changed and needs to be backed up.
927 # If `cur' exists, it is copied to to `back' or
928 # checked into RCS (if the repository file is old),
929 # and then `file' is copied to `cur'. Another RCS
930 # check in done here if RCS is being used.
932 # remove `file' is no longer being tracked by the backups
933 # system. If RCS is not being used, `cur' is moved
934 # to `back', otherwise an empty file is checked in,
935 # and then `cur' is removed.
945 if checkyesno backup_uses_rcs; then
946 _msg0="backup archive"
949 # ensure that history file is not locked
950 if [ -f $_cur,v ]; then
951 rcs -q -u -U -M $_cur
954 # ensure after switching to rcs that the
955 # current backup is not lost
956 if [ -f $_cur ]; then
957 # no archive, or current newer than archive
958 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
959 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
968 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
971 chown root:wheel $_cur $_cur,v
975 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
977 chown root:wheel $_cur $_cur,v
984 if [ -f $_cur ]; then
988 chown root:wheel $_cur