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.
221 .It Ic check_pidfile Ar pidfile procname Op Ar interpreter
222 Parses the first word of the first line of
224 for a PID, and ensures that the process with that PID
225 is running and its first argument matches
227 Prints the matching PID if successful, otherwise nothing.
230 is provided, parse the first line of
232 ensure that the line is of the form:
234 .Dl "#! interpreter [...]"
238 with its optional arguments and
240 appended as the process string to search for.
241 .It Ic check_process Ar procname Op Ar interpreter
242 Prints the PIDs of any processes that are running with a first
243 argument that matches
248 .It Ic debug Ar message
249 Display a debugging message to
251 log it to the system log using
254 return to the caller.
255 The error message consists of the script name
262 This function is intended to be used by developers
263 as an aid to debugging scripts.
264 It can be turned on or off
269 .It Ic err Ar exitval message
270 Display an error message to
272 log it to the system log
277 with an exit value of
279 The error message consists of the script name
286 .It Ic force_depend name
287 Output an advisory message and force the
294 component of the path to the script, usually
296 If the script fails for any reason it will output a warning
297 and return with a return value of 1.
300 .It Ic info Ar message
301 Display an informational message to
303 and log it to the system log using
305 The message consists of the script name
312 The display of this informational output can be
313 turned on or off by the
317 .It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
320 as a kernel module unless it is already loaded.
321 For the purpose of checking the module status,
322 either the exact module name can be specified using
326 regular expression matching the module name can be supplied via
328 By default, the module is assumed to have the same name as
330 which is not always the case.
331 .It Ic load_rc_config Ar name
332 Source in the configuration files for
336 is sourced if it has not yet been read in.
338 .Pa /etc/rc.conf.d/ Ns Ar name
339 is sourced if it is an existing file.
340 The latter may also contain other variable assignments to override
342 arguments defined by the calling script, to provide an easy
343 mechanism for an administrator to override the behaviour of a given
345 script without requiring the editing of that script.
346 .It Ic load_rc_config_var Ar name Ar var
353 and set in the current shell, using
355 in a sub-shell to prevent unwanted side effects from other variable
357 .It Ic mount_critical_filesystems Ar type
358 Go through a list of critical file systems,
362 .Va critical_filesystems_ Ns Ar type ,
363 mounting each one that
364 is not currently mounted.
365 .It Ic rc_usage Ar command ...
366 Print a usage message for
370 being the list of valid arguments
373 .Dq Bq Li fast | force | one .
375 .It Ic reverse_list Ar item ...
379 .It Ic run_rc_command Ar argument
382 method for the current
384 script, based on the settings of various shell variables.
386 is extremely flexible, and allows fully functional
388 scripts to be implemented in a small amount of shell code.
391 is searched for in the list of supported commands, which may be one
393 .Bl -tag -width ".Cm restart" -offset indent
396 This should check that the service is to be started as specified by
398 Also checks if the service is already running and refuses to start if
400 This latter check is not performed by standard
402 scripts if the system is starting directly to multi-user mode, to
403 speed up the boot process.
405 If the service is to be started as specified by
408 This should check that the service is running and complain if it is not.
414 Defaults to displaying the process ID of the program (if running).
418 variables are used to control the startup of the service (if any).
425 is set, also support:
426 .Bl -tag -width ".Cm restart" -offset indent
428 Wait for the command to exit.
430 Show the status of the process.
433 Other supported commands are listed in the optional variable
437 may have one of the following prefixes which alters its operation:
438 .Bl -tag -width ".Li force" -offset indent
440 Skip the check for an existing running process,
442 .Va rc_fast Ns = Ns Li YES .
449 .Va rc_force Ns = Ns Li YES .
451 .Ar argument Ns Va _precmd
452 returning non-zero, and ignores any of the
454 tests failing, and always returns a zero exit status.
460 but performs all the other prerequisite tests.
464 uses the following shell variables to control its behaviour.
465 Unless otherwise stated, these are optional.
466 .Bl -tag -width ".Va procname" -offset indent
468 The name of this script.
469 This is not optional.
475 to determine if this method should be run.
477 Full path to the command.
479 .Ar argument Ns Va _cmd
480 is defined for each supported keyword.
482 .Va ${name}_program .
484 Optional arguments and/or shell directives for
486 .It Va command_interpreter
490 .Dl "#! command_interpreter [...]"
496 .Dl "command_interpreter [...] command"
498 so use that string to find the PID(s) of the running command
501 .It Va extra_commands
502 Extra commands/keywords/arguments supported.
505 Used to determine the PID(s) of the running command.
510 .Dl "check_pidfile $pidfile $procname"
517 .Dl "check_process $procname"
521 Process name to check for.
522 Defaults to the value of
525 Check for the existence of the listed directories
529 .It Va required_files
530 Check for the readability of the listed files
534 .It Va required_modules
535 Ensure that the listed kernel modules are loaded
539 This is done after invoking the commands from
541 so that the missing modules are not loaded in vain
542 if the preliminary commands indicate a error condition.
543 A word in the list can have an optional
544 .Dq Li : Ns Ar modname
546 .Dq Li ~ Ns Ar pattern
552 parameter is passed to
558 option, respectively.
559 See the description of
561 in this document for details.
565 on each of the list variables
577 .It Va ${name}_chroot
589 This is usually set in
594 The environment variable
596 can be used to override this.
605 .It Va ${name}_program
606 Full path to the command.
609 if both are set, but has no effect if
614 should be set in the script while
632 Group to run the chrooted
635 .It Va ${name}_groups
636 Comma separated list of supplementary groups to run the chrooted
639 .It Ar argument Ns Va _cmd
640 Shell commands which override the default method for
642 .It Ar argument Ns Va _precmd
643 Shell commands to run just before running
644 .Ar argument Ns Va _cmd
645 or the default method for
647 If this returns a non-zero exit code, the main method is not performed.
648 If the default method is being executed, this check is performed after
651 checks and process (non-)existence checks.
652 .It Ar argument Ns Va _postcmd
653 Shell commands to run if running
654 .Ar argument Ns Va _cmd
655 or the default method for
657 returned a zero exit code.
659 Signal to send the processes to stop in the default
665 Signal to send the processes to reload in the default
675 .Ar argument Ns Va _cmd
676 is not defined, then a default method is provided by
678 .Bl -tag -width ".Sy Argument" -offset indent
685 .Ic checkyesno Va rcvar
689 Determine the PIDs of
705 instead, and does not run
707 Another difference from
711 is not provided by default.
712 It can be enabled via
716 .Dl "extra_commands=reload"
726 or some other script specific status operation.
734 variable is used (if any).
735 This method always works, even if the appropriate
741 The following variables are available to the methods
743 .Ar argument Ns Va _cmd )
747 .Bl -tag -width ".Va rc_flags" -offset indent
751 after fast and force processing has been performed.
753 Flags to start the default command with.
756 unless overridden by the environment variable
758 This variable may be changed by the
759 .Ar argument Ns Va _precmd
774 .It Ic run_rc_script Ar file argument
779 and handle the return value from the script.
781 Various shell variables are unset before
784 .Bd -ragged -offset indent
788 .Va command_interpreter ,
795 .Ar argument Ns Va _cmd ,
796 .Ar argument Ns Va _precmd .
797 .Ar argument Ns Va _postcmd .
800 The startup behaviour of
802 depends upon the following checks:
809 it is sourced into the current shell.
813 appears to be a backup or scratch file
814 (e.g., with a suffix of
822 is not executable, ignore it.
827 .Va rc_fast_and_loose
834 into the current shell.
836 .It Ic set_rcvar Op Ar base
837 Set the variable name required to start a service.
840 a daemon is usually controlled by an
842 variable consisting of a daemon's name postfixed by the string
844 This is not the case in
846 When the following line is included in a script:
848 .Dl "rcvar=`set_rcvar`"
850 this function will use the value of the
852 variable, which should be defined by the calling script,
853 to construct the appropriate
858 argument is set it will use
862 .It Ic wait_for_pids Op Ar pid ...
863 Wait until all of the provided
865 do not exist any more, printing the list of outstanding
868 .It Ic warn Ar message
869 Display a warning message to
871 and log it to the system log
874 The warning message consists of the script name
878 .Dq Li ": WARNING: " ,
883 .Bl -tag -width ".Pa /etc/rc.subr" -compact
901 support functions appeared in