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 Op Ar service
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 startmsg Oo Fl n Oc Ar message
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
110 Save the previous version of
118 may be one of the following:
119 .Bl -tag -width ".Cm remove"
122 is now being backed up by or possibly re-entered into this backup mechanism.
127 has changed and needs to be backed up.
130 exists, it is copied to
138 is no longer being tracked by this backup mechanism.
143 .It Ic checkyesno Ar var
162 is not set correctly.
163 The values are case insensitive.
166 should be a variable name, not its value;
168 will expand the variable by itself.
169 .It Ic check_pidfile Ar pidfile procname Op Ar interpreter
170 Parses the first word of the first line of
172 for a PID, and ensures that the process with that PID
173 is running and its first argument matches
175 Prints the matching PID if successful, otherwise nothing.
178 is provided, parse the first line of
180 ensure that the line is of the form:
182 .Dl "#! interpreter [...]"
186 with its optional arguments and
188 appended as the process string to search for.
189 .It Ic check_process Ar procname Op Ar interpreter
190 Prints the PIDs of any processes that are running with a first
191 argument that matches
196 .It Ic debug Ar message
197 Display a debugging message to
199 log it to the system log using
202 return to the caller.
203 The error message consists of the script name
210 This function is intended to be used by developers
211 as an aid to debugging scripts.
212 It can be turned on or off
217 .It Ic err Ar exitval message
218 Display an error message to
220 log it to the system log
225 with an exit value of
227 The error message consists of the script name
234 .It Ic force_depend Ar name
235 Output an advisory message and force the
242 component of the path to the script located at
244 (scripts stored in other locations such as
245 .Pa /usr/local/etc/rc.d
246 cannot be controlled with
249 If the script fails for any reason it will output a warning
250 and return with a return value of 1.
253 .It Ic info Ar message
254 Display an informational message to
256 and log it to the system log using
258 The message consists of the script name
265 The display of this informational output can be
266 turned on or off by the
270 .It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
273 as a kernel module unless it is already loaded.
274 For the purpose of checking the module status,
275 either the exact module name can be specified using
279 regular expression matching the module name can be supplied via
281 By default, the module is assumed to have the same name as
283 which is not always the case.
284 .It Ic load_rc_config Op Ar service
285 Source in the configuration file(s) for
290 only the global configuration file(s) will be loaded.
293 is sourced if it has not yet been read in.
295 .Pa /etc/rc.conf.d/ Ns Ar service
296 is sourced if it is an existing file.
297 The latter may also contain other variable assignments to override
299 arguments defined by the calling script, to provide an easy
300 mechanism for an administrator to override the behaviour of a given
302 script without requiring the editing of that script.
303 .It Ic load_rc_config_var Ar name Ar var
310 and set in the current shell, using
312 in a sub-shell to prevent unwanted side effects from other variable
314 .It Ic mount_critical_filesystems Ar type
315 Go through a list of critical file systems,
319 .Va critical_filesystems_ Ns Ar type ,
320 mounting each one that
321 is not currently mounted.
322 .It Ic rc_usage Ar command ...
323 Print a usage message for
327 being the list of valid arguments
330 .Dq Bq Li fast | force | one | quiet .
332 .It Ic reverse_list Ar item ...
336 .It Ic run_rc_command Ar argument
339 method for the current
341 script, based on the settings of various shell variables.
343 is extremely flexible, and allows fully functional
345 scripts to be implemented in a small amount of shell code.
348 is searched for in the list of supported commands, which may be one
350 .Bl -tag -width ".Cm restart" -offset indent
353 This should check that the service is to be started as specified by
355 Also checks if the service is already running and refuses to start if
357 This latter check is not performed by standard
359 scripts if the system is starting directly to multi-user mode, to
360 speed up the boot process.
362 If the service is to be started as specified by
365 This should check that the service is running and complain if it is not.
371 Defaults to displaying the process ID of the program (if running).
373 Return 0 if the service is enabled and 1 if it is not.
374 This command does not print anything.
378 variables are used to control the startup of the service (if any).
385 is set, also support:
386 .Bl -tag -width ".Cm restart" -offset indent
388 Wait for the command to exit.
390 Show the status of the process.
393 Other supported commands are listed in the optional variable
397 may have one of the following prefixes which alters its operation:
398 .Bl -tag -width ".Li force" -offset indent
400 Skip the check for an existing running process,
402 .Va rc_fast Ns = Ns Li YES .
409 .Va rc_force Ns = Ns Li YES .
411 .Ar argument Ns Va _precmd
412 returning non-zero, and ignores any of the
414 tests failing, and always returns a zero exit status.
420 but performs all the other prerequisite tests.
422 Inhibits some verbose diagnostics.
423 Currently, this includes messages
429 and errors about usage of services that are not enabled in
431 This prefix also sets
432 .Va rc_quiet Ns = Ns Li YES .
435 is not intended to completely mask all debug and warning messages,
436 but only certain small classes of them.
440 uses the following shell variables to control its behaviour.
441 Unless otherwise stated, these are optional.
442 .Bl -tag -width ".Va procname" -offset indent
444 The name of this script.
445 This is not optional.
451 to determine if this method should be run.
453 Full path to the command.
455 .Ar argument Ns Va _cmd
456 is defined for each supported keyword.
458 .Va ${name}_program .
460 Optional arguments and/or shell directives for
462 .It Va command_interpreter
466 .Dl "#! command_interpreter [...]"
472 .Dl "command_interpreter [...] command"
474 so use that string to find the PID(s) of the running command
477 .It Va extra_commands
478 Extra commands/keywords/arguments supported.
481 Used to determine the PID(s) of the running command.
486 .Dl "check_pidfile $pidfile $procname"
493 .Dl "check_process $procname"
497 Process name to check for.
498 Defaults to the value of
501 Check for the existence of the listed directories
505 The list is checked before running
507 .It Va required_files
508 Check for the readability of the listed files
512 The list is checked before running
514 .It Va required_modules
515 Ensure that the listed kernel modules are loaded
519 The list is checked after running
521 This is done after invoking the commands from
523 so that the missing modules are not loaded in vain
524 if the preliminary commands indicate a error condition.
525 A word in the list can have an optional
526 .Dq Li \&: Ns Ar modname
528 .Dq Li ~ Ns Ar pattern
534 parameter is passed to
540 option, respectively.
541 See the description of
543 in this document for details.
547 on each of the list variables
551 The list is checked after running
561 .It Va ${name}_chroot
570 A list of environment variables to run
573 Those variables will be passed as arguments to the
576 .Ar argument Ns Va _cmd
578 In that case the contents of
580 will be exported via the
584 which puts some limitations on the names of variables
585 (e.g., a variable name may not start with a digit).
586 .It Va ${name}_env_file
587 A file to source for environmental variables to run
591 all the variables which are being assigned in this file are going
592 to be exported into the environment of
607 This is usually set in
612 The environment variable
614 can be used to override this.
623 .It Va ${name}_limits
624 Resource limits to apply to
626 This will be passed as arguments to the
629 By default, the resource limits are based on the login class defined in
630 .Va ${name}_login_class .
631 .It Va ${name}_login_class
632 Login class to use with
636 .It Va ${name}_oomprotect
639 from being killed when swap space is exhausted.
642 is used, no child processes are protected.
645 protect all child processes.
646 .It Va ${name}_program
647 Full path to the command.
650 if both are set, but has no effect if
655 should be set in the script while
673 Group to run the chrooted
676 .It Va ${name}_groups
677 Comma separated list of supplementary groups to run the chrooted
680 .It Va ${name}_prepend
681 Commands to be prepended to
683 This is a generic version of
689 Command to be run prior to
691 .It Ar argument Ns Va _cmd
692 Shell commands which override the default method for
694 .It Ar argument Ns Va _precmd
695 Shell commands to run just before running
696 .Ar argument Ns Va _cmd
697 or the default method for
699 If this returns a non-zero exit code, the main method is not performed.
700 If the default method is being executed, this check is performed after
703 checks and process (non-)existence checks.
704 .It Ar argument Ns Va _postcmd
705 Shell commands to run if running
706 .Ar argument Ns Va _cmd
707 or the default method for
709 returned a zero exit code.
711 Signal to send the processes to stop in the default
717 Signal to send the processes to reload in the default
727 .Ar argument Ns Va _cmd
728 is not defined, then a default method is provided by
730 .Bl -tag -width ".Sy Argument" -offset indent
737 .Ic checkyesno Va rcvar
741 Determine the PIDs of
757 instead, and does not run
759 Another difference from
763 is not provided by default.
764 It can be enabled via
768 .Dl "extra_commands=reload"
778 or some other script specific status operation.
786 variable is used (if any).
787 This method always works, even if the appropriate
793 The following variables are available to the methods
795 .Ar argument Ns Va _cmd )
799 .Bl -tag -width ".Va rc_service" -offset indent
803 after fast and force processing has been performed.
805 Flags to start the default command with.
808 unless overridden by the environment variable
810 This variable may be changed by the
811 .Ar argument Ns Va _precmd
814 Path to the service script being executed, in case it needs to re-invoke itself.
828 .It Ic run_rc_script Ar file argument
833 and handle the return value from the script.
835 Various shell variables are unset before
838 .Bd -ragged -offset indent
842 .Va command_interpreter ,
849 .Ar argument Ns Va _cmd ,
850 .Ar argument Ns Va _precmd .
851 .Ar argument Ns Va _postcmd .
854 The startup behaviour of
856 depends upon the following checks:
863 it is sourced into the current shell.
867 appears to be a backup or scratch file
868 (e.g., with a suffix of
876 is not executable, ignore it.
881 .Va rc_fast_and_loose
888 into the current shell.
890 .It Ic startmsg Oo Fl n Oc Ar message
891 Display a start message to
893 It should be used instead of
895 The display of this output can be turned off if the
901 .It Ic stop_boot Op Ar always
902 Prevent booting to multiuser mode.
912 .Ic checkyesno Ar always
913 indicates a truth value, then a
915 signal is sent to the parent
916 process, which is assumed to be
918 Otherwise, the shell exits with a non-zero status.
919 .It Ic wait_for_pids Op Ar pid ...
920 Wait until all of the provided
922 do not exist any more, printing the list of outstanding
925 .It Ic warn Ar message
926 Display a warning message to
928 and log it to the system log
931 The warning message consists of the script name
935 .Dq Li ": WARNING: " ,
940 .Bl -tag -width ".Pa /etc/rc.subr" -compact
958 support functions appeared in