1 # $NetBSD: rc.subr,v 1.28 2000/11/06 00:08:30 lukem Exp $
3 # Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
6 # This code is derived from software contributed to The NetBSD Foundation
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 # 3. All advertising materials mentioning features or use of this software
18 # must display the following acknowledgement:
19 # This product includes software developed by the NetBSD
20 # Foundation, Inc. and its contributors.
21 # 4. Neither the name of The NetBSD Foundation nor the names of its
22 # contributors may be used to endorse or promote products derived
23 # from this software without specific prior written permission.
25 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 # POSSIBILITY OF SUCH DAMAGE.
38 # functions used by various rc scripts
47 # Test $1 variable, and warn if not set to YES or NO.
48 # Return 0 if it's "yes" (et al), nonzero otherwise.
55 # "yes", "true", "on", or "1"
56 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
60 # "no", "false", "off", or "0"
61 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
65 warn "\$${1} is not set properly."
72 # mount_critical_filesystems
73 # Go through the list of critical filesystems, checking each one
74 # to see if it is mounted, and if it is not, mounting it.
76 mount_critical_filesystems()
78 if [ $1 = local ]; then
79 _fslist=$critical_filesystems_beforenet
81 _fslist=$critical_filesystems
83 for _fs in $_fslist; do
86 while read what _on on _type type; do
87 if [ $on = $_fs ]; then
91 if [ $_ismounted = no ]; then
92 mount $_fs >/dev/null 2>&1
99 # check_pidfile pidfile procname
100 # Parses the first line of pidfile for a pid, and ensures
101 # that the process is running and matches procname.
102 # Prints the matching pid upon success, nothing otherwise.
108 if [ -z "$_pidfile" -o -z "$_procname" ]; then
109 err 3 'USAGE: check_pidfile pidfile procname'
111 if [ ! -f $_pidfile ]; then
114 read _pid _junk < $_pidfile
115 if [ -z "$_pid" ]; then
118 _procnamebn=${_procname##*/}
119 ps -p $_pid -o 'pid,command' | while read _npid _arg0 _argv; do
120 if [ "$_npid" = "PID" ]; then
123 if [ "$_arg0" = "$_procname" \
124 -o "$_arg0" = "$_procnamebn" \
125 -o "$_arg0" = "${_procnamebn}:" \
126 -o "$_arg0" = "(${_procnamebn})" ]; then
134 # check_process procname
135 # Ensures that a process (or processes) named procname is running.
136 # Prints a list of matching pids.
141 if [ -z "$_procname" ]; then
142 err 3 'USAGE: check_process procname'
144 _procnamebn=${_procname##*/}
146 ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do
147 if [ "$_npid" = "PID" ]; then
150 if [ "$_arg0" = "$_procname" \
151 -o "$_arg0" = "$_procnamebn" \
152 -o "$_arg0" = "${_procnamebn}:" \
153 -o "$_arg0" = "(${_procnamebn})" ]; then
154 echo -n "$_pref$_npid"
162 # Search for arg in the list of supported commands, which is:
163 # "start stop restart rcvar status ${extra_commands}"
164 # If there's a match, run ${arg}_cmd or the default command (see below).
166 # If arg has a given prefix, then change the operation as follows:
169 # fast Skip the pid check.
170 # force Set ${rcvar} to YES.
172 # The following globals are used:
174 # name needed function
175 # ---- ------ --------
176 # name y Name of script.
178 # command n Full path to command.
179 # Not needed if ${arg}_cmd is set for
182 # command_args n Optional args/shell directives for command.
184 # extra_commands n List of extra commands supported.
186 # pidfile n If set, use check_pidfile $pidfile, else if
187 # $command is set, use check_process $command.
189 # rcvar n This is checked with checkyesno to determine
190 # if the action should be run.
192 # ${name}_chroot n Directory to chroot to before running ${command}
194 # ${name}_chdir n Directory to cd to before running ${command}
195 # (if not using ${name}_chroot).
197 # ${name}_flags n Arguments to call ${command} with.
198 # NOTE: $flags from the parent environment
199 # can be used to override this.
201 # ${name}_nice n Nice level to run ${command} at.
203 # ${name}_user n User to run ${command} as, using su(1) if not
204 # using ${name}_chroot.
206 # ${name}_group n Group to run chrooted ${command} as.
208 # ${name}_groups n Supplementary group list to run chrooted
211 # ${_arg}_cmd n If set, use this as the action when invoked;
212 # $_arg is available to the action to use.
213 # Otherwise, use default command (see below)
215 # ${_arg}_precmd n If set, run just before performing the main
216 # action in the default command (i.e, after
217 # checking for required bits and process
219 # If this completes with a non-zero exit code,
220 # don't run ${_arg}_cmd.
222 # required_dirs n If set, check for the existence of the given
223 # directories before running the default
226 # required_files n If set, check for the readability of the given
227 # files before running the default (re)start
230 # required_vars n If set, perform checkyesno on each of the
231 # listed variables before running the default
234 # Default commands for a given arg:
238 # status Show if ${command} is running, etc.
240 # start if !running && checkyesno ${rcvar}
244 # kill $sig_stop `check_pidfile $pidfile`
246 # kill $sig_stop `check_process $command`
247 # $sig_stop defaults to TERM.
249 # reload As stop, except use $sig_reload instead.
250 # $sig_reload defaults to HUP.
252 # restart Run `stop' then `start'.
258 if [ -z "$name" ]; then
259 err 3 '$name is not set.'
263 fast*) # "fast" prefix; don't check pid
267 force*) # "force prefix; always start
270 if [ -n "${rcvar}" ]; then
276 _keywords="start stop restart rcvar $extra_commands"
279 # setup pid check command if not fast
280 if [ -z "$_rc_fast_run" ]; then
281 if [ -n "$pidfile" ]; then
282 _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`'
283 elif [ -n "$command" ]; then
284 _pidcmd='_pid=`check_process '$command'`'
286 if [ -n "$_pidcmd" ]; then
287 _keywords="${_keywords} status"
291 if [ -z "$_arg" ]; then
292 rc_usage "$_keywords"
295 if [ -n "$flags" ]; then # allow override from environment
298 eval _flags=\$${name}_flags
300 eval _chdir=\$${name}_chdir
301 eval _chroot=\$${name}_chroot
302 eval _nice=\$${name}_nice
303 eval _user=\$${name}_user
304 eval _group=\$${name}_group
305 eval _groups=\$${name}_groups
307 # if ${rcvar} is set, and $1 is not
308 # "rcvar" or "status", then run
309 # checkyesno ${rcvar}
310 # and return if that failed
312 if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then
313 if ! checkyesno ${rcvar}; then
318 eval $_pidcmd # determine the pid if necessary
320 for _elem in $_keywords; do
321 if [ "$_elem" != "$_arg" ]; then
325 # if there's a custom ${XXX_cmd},
326 # run that instead of the default
328 eval _cmd=\$${_arg}_cmd
329 eval _precmd=\$${_arg}_precmd
330 if [ -n "$_cmd" ]; then
331 # if the precmd failed and force
334 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
342 case "$_arg" in # default operations...
345 if [ -n "$_pid" ]; then
346 echo "${name} is running as pid $_pid."
348 echo "${name} is not running."
354 if [ -n "$_pid" ]; then
355 echo "${name} already running? (pid=$_pid)."
359 if [ ! -x $command ]; then
363 # check for required variables,
364 # directories, and files
366 for _f in $required_vars; do
367 if ! checkyesno $_f; then
368 warn "\$${_f} is not set."
369 if [ -z "$_rc_force_run" ]; then
374 for _f in $required_dirs; do
375 if [ ! -d "${_f}/." ]; then
376 warn "${_f} is not a directory."
377 if [ -z "$_rc_force_run" ]; then
382 for _f in $required_files; do
383 if [ ! -r "${_f}" ]; then
384 warn "${_f} is not readable."
385 if [ -z "$_rc_force_run" ]; then
391 # if the precmd failed and force
394 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
399 # setup the command to run, and run it
401 echo "Starting ${name}."
402 if [ -n "$_chroot" ]; then
404 ${_nice:+nice -n $_nice }\
405 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
406 $_chroot $command $_flags $command_args"
409 ${_user:+su -m $_user -c 'sh -c \"}\
410 ${_chdir:+cd $_chdir; }\
411 ${_nice:+nice -n $_nice }\
412 $command $_flags $command_args\
419 if [ -z "$_pid" ]; then
420 if [ -n "$pidfile" ]; then
422 "${name} not running? (check $pidfile)."
424 echo "${name} not running?"
429 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
432 echo "Stopping ${name}."
434 "${_user:+su -m $_user -c '}kill -${sig_stop:-TERM} $_pid${_user:+'}"
439 if [ -z "$_pid" ]; then
440 if [ -n "$pidfile" ]; then
442 "${name} not running? (check $pidfile)."
444 echo "${name} not running?"
448 echo "Reloading ${name} config files."
449 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
453 "${_user:+su -m $_user -c '}kill -${sig_reload:-HUP} $_pid${_user:+'}"
458 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then
461 # prevent restart being called more
462 # than once by any given script
464 if [ -n "$_rc_restart_done" ]; then
468 ( $0 ${_rc_force_run:+force}stop )
470 $0 ${_rc_force_run:+force}start
476 if [ -n "$rcvar" ]; then
477 if checkyesno ${rcvar}; then
478 echo "\$${rcvar}=YES"
486 rc_usage "$_keywords"
493 echo 1>&2 "$0: unknown directive '$_arg'."
494 rc_usage "$_keywords"
499 # run_rc_script file arg
500 # Start the script `file' with `arg', and correctly handle the
501 # return value from the script. If `file' ends with `.sh', it's
502 # sourced into the current environment. Otherwise it's run as
505 # Note: because `.sh' files are sourced into the current environment
506 # run_rc_command shouldn't be used because its difficult to ensure
507 # that the global variable state before and after the sourcing of
508 # the .sh file won't adversely affect other scripts.
514 if [ -z "$_file" -o -z "$_arg" ]; then
515 err 3 'USAGE: run_rc_script file arg'
519 *.sh) # run in current shell
523 ( set $_arg ; . $_file )
530 # Source in the configuration file for a given command.
535 if [ -z "$_command" ]; then
536 err 3 'USAGE: load_rc_config command'
540 if [ -f /etc/rc.conf.d/"$_command" ]; then
541 . /etc/rc.conf.d/"$_command"
548 # Print a usage string for $0, with `commands' being a list of
553 echo -n 1>&2 "usage: $0 [fast|force]("
557 echo -n 1>&2 "$_sep$_elem"
565 # err exitval message
566 # Display message to stderr and log to the syslog, and exit with exitval.
573 logger "$0: ERROR: $*"
574 echo 1>&2 "$0: ERROR: $*"
580 # Display message to stderr and log to the syslog.
584 logger "$0: WARNING: $*"
585 echo 1>&2 "$0: WARNING: $*"