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.
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 set_rcvar Op Ar base
78 .Ic wait_for_pids Op Ar pid ...
86 contains commonly used shell script functions and variable
87 definitions which are used by various scripts such as
89 Scripts required by ports in
90 .Pa /usr/local/etc/rc.d
92 be rewritten to make use of it.
96 functions were mostly imported from
99 They are accessed by sourcing
101 into the current shell.
103 The following shell functions are available:
105 .It Ic backup_file Ar action file current backup
106 Make a backup copy of
118 to archive the previous version of
120 otherwise save the previous version of
128 may be one of the following:
129 .Bl -tag -width ".Cm remove"
132 is now being backed up by or possibly re-entered into this backup mechanism.
134 is created, and if necessary, the
136 files are created as well.
139 has changed and needs to be backed up.
142 exists, it is copied to
146 (if the repository file is old),
153 is no longer being tracked by this backup mechanism.
156 is being used, an empty file is checked in and
164 .It Ic checkyesno Ar var
183 is not set correctly.
184 The values are case insensitive.
187 should be a variable name, not its value;
189 will expand the variable by itself.
190 .It Ic check_pidfile Ar pidfile procname Op Ar interpreter
191 Parses the first word of the first line of
193 for a PID, and ensures that the process with that PID
194 is running and its first argument matches
196 Prints the matching PID if successful, otherwise nothing.
199 is provided, parse the first line of
201 ensure that the line is of the form:
203 .Dl "#! interpreter [...]"
207 with its optional arguments and
209 appended as the process string to search for.
210 .It Ic check_process Ar procname Op Ar interpreter
211 Prints the PIDs of any processes that are running with a first
212 argument that matches
217 .It Ic debug Ar message
218 Display a debugging message to
220 log it to the system log using
223 return to the caller.
224 The error message consists of the script name
231 This function is intended to be used by developers
232 as an aid to debugging scripts.
233 It can be turned on or off
238 .It Ic err Ar exitval message
239 Display an error message to
241 log it to the system log
246 with an exit value of
248 The error message consists of the script name
255 .It Ic force_depend Ar name
256 Output an advisory message and force the
263 component of the path to the script, usually
265 If the script fails for any reason it will output a warning
266 and return with a return value of 1.
269 .It Ic info Ar message
270 Display an informational message to
272 and log it to the system log using
274 The message consists of the script name
281 The display of this informational output can be
282 turned on or off by the
286 .It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
289 as a kernel module unless it is already loaded.
290 For the purpose of checking the module status,
291 either the exact module name can be specified using
295 regular expression matching the module name can be supplied via
297 By default, the module is assumed to have the same name as
299 which is not always the case.
300 .It Ic load_rc_config Ar name
301 Source in the configuration files for
305 is sourced if it has not yet been read in.
307 .Pa /etc/rc.conf.d/ Ns Ar name
308 is sourced if it is an existing file.
309 The latter may also contain other variable assignments to override
311 arguments defined by the calling script, to provide an easy
312 mechanism for an administrator to override the behaviour of a given
314 script without requiring the editing of that script.
315 .It Ic load_rc_config_var Ar name Ar var
322 and set in the current shell, using
324 in a sub-shell to prevent unwanted side effects from other variable
326 .It Ic mount_critical_filesystems Ar type
327 Go through a list of critical file systems,
331 .Va critical_filesystems_ Ns Ar type ,
332 mounting each one that
333 is not currently mounted.
334 .It Ic rc_usage Ar command ...
335 Print a usage message for
339 being the list of valid arguments
342 .Dq Bq Li fast | force | one | quiet .
344 .It Ic reverse_list Ar item ...
348 .It Ic run_rc_command Ar argument
351 method for the current
353 script, based on the settings of various shell variables.
355 is extremely flexible, and allows fully functional
357 scripts to be implemented in a small amount of shell code.
360 is searched for in the list of supported commands, which may be one
362 .Bl -tag -width ".Cm restart" -offset indent
365 This should check that the service is to be started as specified by
367 Also checks if the service is already running and refuses to start if
369 This latter check is not performed by standard
371 scripts if the system is starting directly to multi-user mode, to
372 speed up the boot process.
374 If the service is to be started as specified by
377 This should check that the service is running and complain if it is not.
383 Defaults to displaying the process ID of the program (if running).
387 variables are used to control the startup of the service (if any).
394 is set, also support:
395 .Bl -tag -width ".Cm restart" -offset indent
397 Wait for the command to exit.
399 Show the status of the process.
402 Other supported commands are listed in the optional variable
406 may have one of the following prefixes which alters its operation:
407 .Bl -tag -width ".Li force" -offset indent
409 Skip the check for an existing running process,
411 .Va rc_fast Ns = Ns Li YES .
418 .Va rc_force Ns = Ns Li YES .
420 .Ar argument Ns Va _precmd
421 returning non-zero, and ignores any of the
423 tests failing, and always returns a zero exit status.
429 but performs all the other prerequisite tests.
431 Inhibits some verbose diagnostics.
432 Currently, this includes messages
438 and errors about usage of services that are not enabled in
440 This prefix also sets
441 .Va rc_quiet Ns = Ns Li YES .
444 is not intended to completely mask all debug and warning messages,
445 but only certain small classes of them.
449 uses the following shell variables to control its behaviour.
450 Unless otherwise stated, these are optional.
451 .Bl -tag -width ".Va procname" -offset indent
453 The name of this script.
454 This is not optional.
460 to determine if this method should be run.
462 Full path to the command.
464 .Ar argument Ns Va _cmd
465 is defined for each supported keyword.
467 .Va ${name}_program .
469 Optional arguments and/or shell directives for
471 .It Va command_interpreter
475 .Dl "#! command_interpreter [...]"
481 .Dl "command_interpreter [...] command"
483 so use that string to find the PID(s) of the running command
486 .It Va extra_commands
487 Extra commands/keywords/arguments supported.
490 Used to determine the PID(s) of the running command.
495 .Dl "check_pidfile $pidfile $procname"
502 .Dl "check_process $procname"
506 Process name to check for.
507 Defaults to the value of
510 Check for the existence of the listed directories
514 .It Va required_files
515 Check for the readability of the listed files
519 .It Va required_modules
520 Ensure that the listed kernel modules are loaded
524 This is done after invoking the commands from
526 so that the missing modules are not loaded in vain
527 if the preliminary commands indicate a error condition.
528 A word in the list can have an optional
529 .Dq Li : Ns Ar modname
531 .Dq Li ~ Ns Ar pattern
537 parameter is passed to
543 option, respectively.
544 See the description of
546 in this document for details.
550 on each of the list variables
562 .It Va ${name}_chroot
574 This is usually set in
579 The environment variable
581 can be used to override this.
590 .It Va ${name}_program
591 Full path to the command.
594 if both are set, but has no effect if
599 should be set in the script while
617 Group to run the chrooted
620 .It Va ${name}_groups
621 Comma separated list of supplementary groups to run the chrooted
624 .It Ar argument Ns Va _cmd
625 Shell commands which override the default method for
627 .It Ar argument Ns Va _precmd
628 Shell commands to run just before running
629 .Ar argument Ns Va _cmd
630 or the default method for
632 If this returns a non-zero exit code, the main method is not performed.
633 If the default method is being executed, this check is performed after
636 checks and process (non-)existence checks.
637 .It Ar argument Ns Va _postcmd
638 Shell commands to run if running
639 .Ar argument Ns Va _cmd
640 or the default method for
642 returned a zero exit code.
644 Signal to send the processes to stop in the default
650 Signal to send the processes to reload in the default
660 .Ar argument Ns Va _cmd
661 is not defined, then a default method is provided by
663 .Bl -tag -width ".Sy Argument" -offset indent
670 .Ic checkyesno Va rcvar
674 Determine the PIDs of
690 instead, and does not run
692 Another difference from
696 is not provided by default.
697 It can be enabled via
701 .Dl "extra_commands=reload"
711 or some other script specific status operation.
719 variable is used (if any).
720 This method always works, even if the appropriate
726 The following variables are available to the methods
728 .Ar argument Ns Va _cmd )
732 .Bl -tag -width ".Va rc_flags" -offset indent
736 after fast and force processing has been performed.
738 Flags to start the default command with.
741 unless overridden by the environment variable
743 This variable may be changed by the
744 .Ar argument Ns Va _precmd
759 .It Ic run_rc_script Ar file argument
764 and handle the return value from the script.
766 Various shell variables are unset before
769 .Bd -ragged -offset indent
773 .Va command_interpreter ,
780 .Ar argument Ns Va _cmd ,
781 .Ar argument Ns Va _precmd .
782 .Ar argument Ns Va _postcmd .
785 The startup behaviour of
787 depends upon the following checks:
794 it is sourced into the current shell.
798 appears to be a backup or scratch file
799 (e.g., with a suffix of
807 is not executable, ignore it.
812 .Va rc_fast_and_loose
819 into the current shell.
821 .It Ic stop_boot Op Ar always
822 Prevent booting to multiuser mode.
828 .Ic checkyesno Ar always
829 indicates a truth value, then a
831 signal is sent to the parent
832 process, which is assumed to be
834 Otherwise, the shell exits with a non-zero status.
835 .It Ic set_rcvar Op Ar base
836 Set the variable name required to start a service.
839 a daemon is usually controlled by an
841 variable consisting of a daemon's name postfixed by the string
843 This is not the case in
845 When the following line is included in a script:
847 .Dl "rcvar=`set_rcvar`"
849 this function will use the value of the
851 variable, which should be defined by the calling script,
852 to construct the appropriate
857 argument is set it will use
861 .It Ic wait_for_pids Op Ar pid ...
862 Wait until all of the provided
864 do not exist any more, printing the list of outstanding
867 .It Ic warn Ar message
868 Display a warning message to
870 and log it to the system log
873 The warning message consists of the script name
877 .Dq Li ": WARNING: " ,
882 .Bl -tag -width ".Pa /etc/rc.subr" -compact
900 support functions appeared in