1 .\" $NetBSD: rc.subr.8,v 1.12 2004/01/06 00:52:24 lukem Exp $
3 .\" Copyright (c) 2002-2004 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
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.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
32 .Dd September 23, 2012
37 .Nd functions used by system shell scripts
41 .Ic .\& Pa /etc/rc.subr
44 .Ic backup_file Ar action Ar file Ar current Ar backup
48 .Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
50 .Ic check_process Ar procname Op Ar interpreter
54 .Ic err Ar exitval Ar message
56 .Ic force_depend Ar name
60 .Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
62 .Ic load_rc_config Ar name
64 .Ic load_rc_config_var Ar name Ar var
66 .Ic mount_critical_filesystems Ar type
68 .Ic rc_usage Ar command ...
70 .Ic reverse_list Ar item ...
72 .Ic run_rc_command Ar argument
74 .Ic run_rc_script Ar file Ar argument
76 .Ic wait_for_pids Op Ar pid ...
84 contains commonly used shell script functions and variable
85 definitions which are used by various scripts such as
87 Scripts required by ports in
88 .Pa /usr/local/etc/rc.d
90 be rewritten to make use of it.
94 functions were mostly imported from
97 They are accessed by sourcing
99 into the current shell.
101 The following shell functions are available:
103 .It Ic backup_file Ar action file current backup
104 Make a backup copy of
116 to archive the previous version of
118 otherwise save the previous version of
126 may be one of the following:
127 .Bl -tag -width ".Cm remove"
130 is now being backed up by or possibly re-entered into this backup mechanism.
132 is created, and if necessary, the
134 files are created as well.
137 has changed and needs to be backed up.
140 exists, it is copied to
144 (if the repository file is old),
151 is no longer being tracked by this backup mechanism.
154 is being used, an empty file is checked in and
162 .It Ic checkyesno Ar var
181 is not set correctly.
182 The values are case insensitive.
185 should be a variable name, not its value;
187 will expand the variable by itself.
188 .It Ic check_pidfile Ar pidfile procname Op Ar interpreter
189 Parses the first word of the first line of
191 for a PID, and ensures that the process with that PID
192 is running and its first argument matches
194 Prints the matching PID if successful, otherwise nothing.
197 is provided, parse the first line of
199 ensure that the line is of the form:
201 .Dl "#! interpreter [...]"
205 with its optional arguments and
207 appended as the process string to search for.
208 .It Ic check_process Ar procname Op Ar interpreter
209 Prints the PIDs of any processes that are running with a first
210 argument that matches
215 .It Ic debug Ar message
216 Display a debugging message to
218 log it to the system log using
221 return to the caller.
222 The error message consists of the script name
229 This function is intended to be used by developers
230 as an aid to debugging scripts.
231 It can be turned on or off
236 .It Ic err Ar exitval message
237 Display an error message to
239 log it to the system log
244 with an exit value of
246 The error message consists of the script name
253 .It Ic force_depend Ar name
254 Output an advisory message and force the
261 component of the path to the script, usually
263 If the script fails for any reason it will output a warning
264 and return with a return value of 1.
267 .It Ic info Ar message
268 Display an informational message to
270 and log it to the system log using
272 The message consists of the script name
279 The display of this informational output can be
280 turned on or off by the
284 .It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
287 as a kernel module unless it is already loaded.
288 For the purpose of checking the module status,
289 either the exact module name can be specified using
293 regular expression matching the module name can be supplied via
295 By default, the module is assumed to have the same name as
297 which is not always the case.
298 .It Ic load_rc_config Ar name
299 Source in the configuration files for
303 is sourced if it has not yet been read in.
305 .Pa /etc/rc.conf.d/ Ns Ar name
306 is sourced if it is an existing file.
307 The latter may also contain other variable assignments to override
309 arguments defined by the calling script, to provide an easy
310 mechanism for an administrator to override the behaviour of a given
312 script without requiring the editing of that script.
313 .It Ic load_rc_config_var Ar name Ar var
320 and set in the current shell, using
322 in a sub-shell to prevent unwanted side effects from other variable
324 .It Ic mount_critical_filesystems Ar type
325 Go through a list of critical file systems,
329 .Va critical_filesystems_ Ns Ar type ,
330 mounting each one that
331 is not currently mounted.
332 .It Ic rc_usage Ar command ...
333 Print a usage message for
337 being the list of valid arguments
340 .Dq Bq Li fast | force | one | quiet .
342 .It Ic reverse_list Ar item ...
346 .It Ic run_rc_command Ar argument
349 method for the current
351 script, based on the settings of various shell variables.
353 is extremely flexible, and allows fully functional
355 scripts to be implemented in a small amount of shell code.
358 is searched for in the list of supported commands, which may be one
360 .Bl -tag -width ".Cm restart" -offset indent
363 This should check that the service is to be started as specified by
365 Also checks if the service is already running and refuses to start if
367 This latter check is not performed by standard
369 scripts if the system is starting directly to multi-user mode, to
370 speed up the boot process.
372 If the service is to be started as specified by
375 This should check that the service is running and complain if it is not.
381 Defaults to displaying the process ID of the program (if running).
383 Return 0 if the service is enabled and 1 if it is not.
384 This command does not print anything.
388 variables are used to control the startup of the service (if any).
395 is set, also support:
396 .Bl -tag -width ".Cm restart" -offset indent
398 Wait for the command to exit.
400 Show the status of the process.
403 Other supported commands are listed in the optional variable
407 may have one of the following prefixes which alters its operation:
408 .Bl -tag -width ".Li force" -offset indent
410 Skip the check for an existing running process,
412 .Va rc_fast Ns = Ns Li YES .
419 .Va rc_force Ns = Ns Li YES .
421 .Ar argument Ns Va _precmd
422 returning non-zero, and ignores any of the
424 tests failing, and always returns a zero exit status.
430 but performs all the other prerequisite tests.
432 Inhibits some verbose diagnostics.
433 Currently, this includes messages
439 and errors about usage of services that are not enabled in
441 This prefix also sets
442 .Va rc_quiet Ns = Ns Li YES .
445 is not intended to completely mask all debug and warning messages,
446 but only certain small classes of them.
450 uses the following shell variables to control its behaviour.
451 Unless otherwise stated, these are optional.
452 .Bl -tag -width ".Va procname" -offset indent
454 The name of this script.
455 This is not optional.
461 to determine if this method should be run.
463 Full path to the command.
465 .Ar argument Ns Va _cmd
466 is defined for each supported keyword.
468 .Va ${name}_program .
470 Optional arguments and/or shell directives for
472 .It Va command_interpreter
476 .Dl "#! command_interpreter [...]"
482 .Dl "command_interpreter [...] command"
484 so use that string to find the PID(s) of the running command
487 .It Va extra_commands
488 Extra commands/keywords/arguments supported.
491 Used to determine the PID(s) of the running command.
496 .Dl "check_pidfile $pidfile $procname"
503 .Dl "check_process $procname"
507 Process name to check for.
508 Defaults to the value of
511 Check for the existence of the listed directories
515 .It Va required_files
516 Check for the readability of the listed files
520 .It Va required_modules
521 Ensure that the listed kernel modules are loaded
525 This is done after invoking the commands from
527 so that the missing modules are not loaded in vain
528 if the preliminary commands indicate a error condition.
529 A word in the list can have an optional
530 .Dq Li : Ns Ar modname
532 .Dq Li ~ Ns Ar pattern
538 parameter is passed to
544 option, respectively.
545 See the description of
547 in this document for details.
551 on each of the list variables
563 .It Va ${name}_chroot
575 This is usually set in
580 The environment variable
582 can be used to override this.
591 .It Va ${name}_program
592 Full path to the command.
595 if both are set, but has no effect if
600 should be set in the script while
618 Group to run the chrooted
621 .It Va ${name}_groups
622 Comma separated list of supplementary groups to run the chrooted
625 .It Ar argument Ns Va _cmd
626 Shell commands which override the default method for
628 .It Ar argument Ns Va _precmd
629 Shell commands to run just before running
630 .Ar argument Ns Va _cmd
631 or the default method for
633 If this returns a non-zero exit code, the main method is not performed.
634 If the default method is being executed, this check is performed after
637 checks and process (non-)existence checks.
638 .It Ar argument Ns Va _postcmd
639 Shell commands to run if running
640 .Ar argument Ns Va _cmd
641 or the default method for
643 returned a zero exit code.
645 Signal to send the processes to stop in the default
651 Signal to send the processes to reload in the default
661 .Ar argument Ns Va _cmd
662 is not defined, then a default method is provided by
664 .Bl -tag -width ".Sy Argument" -offset indent
671 .Ic checkyesno Va rcvar
675 Determine the PIDs of
691 instead, and does not run
693 Another difference from
697 is not provided by default.
698 It can be enabled via
702 .Dl "extra_commands=reload"
712 or some other script specific status operation.
720 variable is used (if any).
721 This method always works, even if the appropriate
727 The following variables are available to the methods
729 .Ar argument Ns Va _cmd )
733 .Bl -tag -width ".Va rc_flags" -offset indent
737 after fast and force processing has been performed.
739 Flags to start the default command with.
742 unless overridden by the environment variable
744 This variable may be changed by the
745 .Ar argument Ns Va _precmd
760 .It Ic run_rc_script Ar file argument
765 and handle the return value from the script.
767 Various shell variables are unset before
770 .Bd -ragged -offset indent
774 .Va command_interpreter ,
781 .Ar argument Ns Va _cmd ,
782 .Ar argument Ns Va _precmd .
783 .Ar argument Ns Va _postcmd .
786 The startup behaviour of
788 depends upon the following checks:
795 it is sourced into the current shell.
799 appears to be a backup or scratch file
800 (e.g., with a suffix of
808 is not executable, ignore it.
813 .Va rc_fast_and_loose
820 into the current shell.
822 .It Ic stop_boot Op Ar always
823 Prevent booting to multiuser mode.
829 .Ic checkyesno Ar always
830 indicates a truth value, then a
832 signal is sent to the parent
833 process, which is assumed to be
835 Otherwise, the shell exits with a non-zero status.
836 .It Ic wait_for_pids Op Ar pid ...
837 Wait until all of the provided
839 do not exist any more, printing the list of outstanding
842 .It Ic warn Ar message
843 Display a warning message to
845 and log it to the system log
848 The warning message consists of the script name
852 .Dq Li ": WARNING: " ,
857 .Bl -tag -width ".Pa /etc/rc.subr" -compact
875 support functions appeared in