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.
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.
44 .Nd functions used by system shell scripts
48 .Ic .\& Pa /etc/rc.subr
51 .Ic backup_file Ar action Ar file Ar current Ar backup
55 .Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
57 .Ic check_process Ar procname Op Ar interpreter
61 .Ic err Ar exitval Ar message
63 .Ic force_depend Ar name
67 .Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
69 .Ic load_rc_config Ar name
71 .Ic load_rc_config_var Ar name Ar var
73 .Ic mount_critical_filesystems Ar type
75 .Ic rc_usage Ar command ...
77 .Ic reverse_list Ar item ...
79 .Ic run_rc_command Ar argument
81 .Ic run_rc_script Ar file Ar argument
83 .Ic set_rcvar Op Ar base
85 .Ic wait_for_pids Op Ar pid ...
93 contains commonly used shell script functions and variable
94 definitions which are used by various scripts such as
96 Scripts required by ports in
97 .Pa /usr/local/etc/rc.d
99 be rewritten to make use of it.
103 functions were mostly imported from
105 and it is intended that they remain synced between the
107 With that in mind there are several variable
108 definitions that can help in this regard.
112 Its value will be either
116 depending on which OS it is running on.
122 The path and argument list to display only the
125 .Ar name Ns = Ns Ar value
128 The path and argument to write or modify
135 functions are accessed by sourcing
137 into the current shell.
139 The following shell functions are available:
141 .It Ic backup_file Ar action file current backup
142 Make a backup copy of
154 to archive the previous version of
156 otherwise save the previous version of
164 may be one of the following:
165 .Bl -tag -width ".Cm remove"
168 is now being backed up by or possibly re-entered into this backup mechanism.
170 is created, and if necessary, the
172 files are created as well.
175 has changed and needs to be backed up.
178 exists, it is copied to
182 (if the repository file is old),
189 is no longer being tracked by this backup mechanism.
192 is being used, an empty file is checked in and
200 .It Ic checkyesno Ar var
219 is not set correctly.
220 The values are case insensitive.
223 should be a variable name, not its value;
225 will expand the variable by itself.
226 .It Ic check_pidfile Ar pidfile procname Op Ar interpreter
227 Parses the first word of the first line of
229 for a PID, and ensures that the process with that PID
230 is running and its first argument matches
232 Prints the matching PID if successful, otherwise nothing.
235 is provided, parse the first line of
237 ensure that the line is of the form:
239 .Dl "#! interpreter [...]"
243 with its optional arguments and
245 appended as the process string to search for.
246 .It Ic check_process Ar procname Op Ar interpreter
247 Prints the PIDs of any processes that are running with a first
248 argument that matches
253 .It Ic debug Ar message
254 Display a debugging message to
256 log it to the system log using
259 return to the caller.
260 The error message consists of the script name
267 This function is intended to be used by developers
268 as an aid to debugging scripts.
269 It can be turned on or off
274 .It Ic err Ar exitval message
275 Display an error message to
277 log it to the system log
282 with an exit value of
284 The error message consists of the script name
291 .It Ic force_depend name
292 Output an advisory message and force the
299 component of the path to the script, usually
301 If the script fails for any reason it will output a warning
302 and return with a return value of 1.
305 .It Ic info Ar message
306 Display an informational message to
308 and log it to the system log using
310 The message consists of the script name
317 The display of this informational output can be
318 turned on or off by the
322 .It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
325 as a kernel module unless it is already loaded.
326 For the purpose of checking the module status,
327 either the exact module name can be specified using
331 regular expression matching the module name can be supplied via
333 By default, the module is assumed to have the same name as
335 which is not always the case.
336 .It Ic load_rc_config Ar name
337 Source in the configuration files for
341 is sourced if it has not yet been read in.
343 .Pa /etc/rc.conf.d/ Ns Ar name
344 is sourced if it is an existing file.
345 The latter may also contain other variable assignments to override
347 arguments defined by the calling script, to provide an easy
348 mechanism for an administrator to override the behaviour of a given
350 script without requiring the editing of that script.
351 .It Ic load_rc_config_var Ar name Ar var
358 and set in the current shell, using
360 in a sub-shell to prevent unwanted side effects from other variable
362 .It Ic mount_critical_filesystems Ar type
363 Go through a list of critical file systems,
367 .Va critical_filesystems_ Ns Ar type ,
368 mounting each one that
369 is not currently mounted.
370 .It Ic rc_usage Ar command ...
371 Print a usage message for
375 being the list of valid arguments
378 .Dq Bq Li fast | force | one .
380 .It Ic reverse_list Ar item ...
384 .It Ic run_rc_command Ar argument
387 method for the current
389 script, based on the settings of various shell variables.
391 is extremely flexible, and allows fully functional
393 scripts to be implemented in a small amount of shell code.
396 is searched for in the list of supported commands, which may be one
398 .Bl -tag -width ".Cm restart" -offset indent
401 This should check that the service is to be started as specified by
403 Also checks if the service is already running and refuses to start if
405 This latter check is not performed by standard
407 scripts if the system is starting directly to multi-user mode, to
408 speed up the boot process.
410 If the service is to be started as specified by
413 This should check that the service is running and complain if it is not.
419 Defaults to displaying the process ID of the program (if running).
423 variables are used to control the startup of the service (if any).
430 is set, also support:
431 .Bl -tag -width ".Cm restart" -offset indent
433 Wait for the command to exit.
435 Show the status of the process.
438 Other supported commands are listed in the optional variable
442 may have one of the following prefixes which alters its operation:
443 .Bl -tag -width ".Li force" -offset indent
445 Skip the check for an existing running process,
447 .Va rc_fast Ns = Ns Li YES .
454 .Va rc_force Ns = Ns Li YES .
456 .Ar argument Ns Va _precmd
457 returning non-zero, and ignores any of the
459 tests failing, and always returns a zero exit status.
465 but performs all the other prerequisite tests.
469 uses the following shell variables to control its behaviour.
470 Unless otherwise stated, these are optional.
471 .Bl -tag -width ".Va procname" -offset indent
473 The name of this script.
474 This is not optional.
480 to determine if this method should be run.
482 Full path to the command.
484 .Ar argument Ns Va _cmd
485 is defined for each supported keyword.
487 .Va ${name}_program .
489 Optional arguments and/or shell directives for
491 .It Va command_interpreter
495 .Dl "#! command_interpreter [...]"
501 .Dl "command_interpreter [...] command"
503 so use that string to find the PID(s) of the running command
506 .It Va extra_commands
507 Extra commands/keywords/arguments supported.
510 Used to determine the PID(s) of the running command.
515 .Dl "check_pidfile $pidfile $procname"
522 .Dl "check_process $procname"
526 Process name to check for.
527 Defaults to the value of
530 Check for the existence of the listed directories
534 .It Va required_files
535 Check for the readability of the listed files
539 .It Va required_modules
540 Ensure that the listed kernel modules are loaded
544 This is done after invoking the commands from
546 so that the missing modules are not loaded in vain
547 if the preliminary commands indicate a error condition.
548 A word in the list can have an optional
549 .Dq Li : Ns Ar modname
551 .Dq Li ~ Ns Ar pattern
557 parameter is passed to
563 option, respectively.
564 See the description of
566 in this document for details.
570 on each of the list variables
582 .It Va ${name}_chroot
594 This is usually set in
599 The environment variable
601 can be used to override this.
610 .It Va ${name}_program
611 Full path to the command.
614 if both are set, but has no effect if
619 should be set in the script while
637 Group to run the chrooted
640 .It Va ${name}_groups
641 Comma separated list of supplementary groups to run the chrooted
644 .It Ar argument Ns Va _cmd
645 Shell commands which override the default method for
647 .It Ar argument Ns Va _precmd
648 Shell commands to run just before running
649 .Ar argument Ns Va _cmd
650 or the default method for
652 If this returns a non-zero exit code, the main method is not performed.
653 If the default method is being executed, this check is performed after
656 checks and process (non-)existence checks.
657 .It Ar argument Ns Va _postcmd
658 Shell commands to run if running
659 .Ar argument Ns Va _cmd
660 or the default method for
662 returned a zero exit code.
664 Signal to send the processes to stop in the default
670 Signal to send the processes to reload in the default
680 .Ar argument Ns Va _cmd
681 is not defined, then a default method is provided by
683 .Bl -tag -width ".Sy Argument" -offset indent
690 .Ic checkyesno Va rcvar
694 Determine the PIDs of
710 instead, and does not run
712 Another difference from
716 is not provided by default.
717 It can be enabled via
721 .Dl "extra_commands=reload"
731 or some other script specific status operation.
739 variable is used (if any).
740 This method always works, even if the appropriate
746 The following variables are available to the methods
748 .Ar argument Ns Va _cmd )
752 .Bl -tag -width ".Va rc_flags" -offset indent
756 after fast and force processing has been performed.
758 Flags to start the default command with.
761 unless overridden by the environment variable
763 This variable may be changed by the
764 .Ar argument Ns Va _precmd
779 .It Ic run_rc_script Ar file argument
784 and handle the return value from the script.
786 Various shell variables are unset before
789 .Bd -ragged -offset indent
793 .Va command_interpreter ,
800 .Ar argument Ns Va _cmd ,
801 .Ar argument Ns Va _precmd .
802 .Ar argument Ns Va _postcmd .
805 The startup behaviour of
807 depends upon the following checks:
814 it is sourced into the current shell.
818 appears to be a backup or scratch file
819 (e.g., with a suffix of
827 is not executable, ignore it.
832 .Va rc_fast_and_loose
839 into the current shell.
841 .It Ic stop_boot Op Ar always
842 Prevent booting to multiuser mode.
848 .Ic checkyesno Ar always
849 indicates a truth value, then a
851 signal is sent to the parent
852 process, which is assumed to be
854 Otherwise, the shell exits with a non-zero status.
855 .It Ic set_rcvar Op Ar base
856 Set the variable name required to start a service.
859 a daemon is usually controlled by an
861 variable consisting of a daemon's name postfixed by the string
863 This is not the case in
865 When the following line is included in a script:
867 .Dl "rcvar=`set_rcvar`"
869 this function will use the value of the
871 variable, which should be defined by the calling script,
872 to construct the appropriate
877 argument is set it will use
881 .It Ic wait_for_pids Op Ar pid ...
882 Wait until all of the provided
884 do not exist any more, printing the list of outstanding
887 .It Ic warn Ar message
888 Display a warning message to
890 and log it to the system log
893 The warning message consists of the script name
897 .Dq Li ": WARNING: " ,
902 .Bl -tag -width ".Pa /etc/rc.subr" -compact
920 support functions appeared in