1 .\" $NetBSD: rc.subr.8,v 1.31 2012/12/18 13:14:34 wiz Exp $
3 .\" Copyright (c) 2002-2011 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.
35 .Nd functions used by system shell scripts
41 .Ic backup_file Ar action Ar file Ar current Ar backup
43 .Ic basename Ar file Op Ar suffix
47 .Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
49 .Ic check_process Ar procname Op Ar interpreter
51 .Ic collapse_backslash_newline
55 .Ic err Ar exitval Ar message
57 .Ic load_rc_config Ar command
59 .Ic load_rc_config_var Ar command Ar var
61 .Ic mount_critical_filesystems Ar type
63 .Ic no_rc_postprocess Ar command Op Ar arguments
65 .Ic print_rc_metadata Ar string
67 .Ic print_rc_normal Oo Fl n Oc Ar string
69 .Ic rc_usage Ar command Op Ar ...
71 .Ic reverse_list Ar item Op Ar ...
73 .Ic run_rc_command Ar argument [parameters]
75 .Ic run_rc_script Ar file Ar argument
81 .Ic wait_for_pids Op Ar pid Op Ar ...
85 .Ic yesno_to_truefalse Ar var
89 contains commonly used shell script functions which are used by
90 various scripts such as
92 and the periodic system services which are controlled by
101 functions are accessed by sourcing
103 into the current shell.
105 The following shell functions are available:
107 .It Ic backup_file Ar action Ar file Ar current Ar backup
108 Make a backup copy of
120 to archive the previous version of
122 otherwise save the previous version of
128 may be one of the following:
129 .Bl -tag -width 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 .Ic basename Ar file Op Ar suffix
167 except implemented using shell built-in commands, and usable before the
169 direcory is available.
170 .It Ic checkyesno Ar var
189 is not set correctly.
190 The values are case insensitive.
192 Note that the warning message shown by this function when
194 is not set references a manual page where the user can find more information.
195 Its name is picked up from the
198 .It Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
199 Parses the first word of the first line of
201 for a PID, and ensures that the process with that PID
202 is running and its first argument matches
204 Prints the matching PID if successful, otherwise nothing.
207 is provided, parse the first line of
209 ensure that the line is of the form
210 .Dl #! interpreter [...]
213 with its optional arguments and
215 appended as the process string to search for.
216 .It Ic check_process Ar procname Op Ar interpreter
217 Prints the PIDs of any processes that are running with a first
218 argument that matches
223 .It Ic collapse_backslash_newline
224 Copy input to output, collapsing
225 .Ao backslash Ac Ns Ao newline Ac
226 to nothing, but leaving other backslashes alone.
230 except implemented using shell built-in commands, and usable before the
232 direcory is available.
233 .It Ic err Ar exitval Ar message
234 Display an error message to
236 log it to the system log
241 with an exit value of
243 The error message consists of the script name
250 .It Ic load_rc_config Ar command
253 configuration files for
257 is sourced if it has not yet been read in.
259 .Pa /etc/rc.conf.d/ Ns Ar command
260 is sourced if it is an existing file.
261 The latter may also contain other variable assignments to override
263 arguments defined by the calling script, to provide an easy
264 mechanism for an administrator to override the behaviour of a given
266 script without requiring the editing of that script.
267 .It Ic load_rc_config_var Ar command Ar var
274 and set in the current shell, using
276 in a sub-shell to prevent unwanted side effects from other variable
278 .It Ic mount_critical_filesystems Ar type
279 Go through a list of critical file systems,
283 .Sy critical_filesystems_ Ns Ar type ,
284 mounting each one that
285 is not currently mounted.
286 .It Ic no_rc_postprocess Ar command Op Ar arguments
287 Execute the specified command with the specified arguments,
288 in such a way that its output bypasses the post-processor that
290 uses for most commands.
291 This implies that the output will not appear in the
293 file, and will appear on the console regardless of the
296 This is expected to be useful for interactive commands,
297 and this mechanism is automatically used by
299 when a script contains the
304 If invoked from a context that does not appear to be under the control of
306 then the command is executed without special treatment.
307 .It Ic print_rc_metadata Ar string
310 in such a way that it should be handled as meta-data by the
313 If invoked from a context that does not appear to be under the control of
321 script may invoke this function with an argument that begins with
323 followed by one line of arbitrary text;
324 the text will be logged by
326 but will not be displayed on the console.
328 The use of arguments that do not begin with
330 is reserved for internal use by
334 .It Ic print_rc_normal Oo Fl n Oc Ar string
337 in such a way that it should be handled as normal output by the
340 If invoked from a context that does not appear to be under the control of
344 is printed to standard output.
348 flag is specified, then the string is printed without a newline.
350 Intended use cases include:
353 An rc.d script can use
354 .Dq Sy print_rc_normal Fl n
355 to print a partial line in such a way that it appears
356 immediately instead of being buffered by
360 An rc.d script that is run via the
361 .Sy no_rc_postprocess
362 function (so most of its output is invisible to
364 post-processor) can use
366 to force some of its output to be seen by the post-processor.
368 .It Ic rc_usage Ar command Op Ar ...
369 Print a usage message for
373 being the list of valid arguments
375 .Dq "[fast|force|one]" .
376 .It Ic reverse_list Ar item Op Ar ...
380 .It Ic run_rc_command Ar argument Op Ar parameter ...
383 method for the current
385 script, based on the settings of various shell variables.
387 is extremely flexible, and allows fully functional
389 scripts to be implemented in a small amount of shell code.
390 The optional set of parameters is passed verbatim to the command, but not to its
394 is searched for in the list of supported commands, which may be one
396 .Bl -tag -width restart -offset indent
399 This should check that the service is to be started as specified by
401 Also checks if the service is already running and refuses to start if
403 This latter check is not performed by standard
405 scripts if the system is starting directly to multi-user mode, to
406 speed up the boot process.
408 If the service is to be started as specified by
411 This should check that the service is running and complain if it's not.
417 Defaults to displaying the process ID of the program (if running).
421 variables are used to control the startup of the service (if any).
428 is set, also support:
429 .Bl -tag -width restart -offset indent
431 Wait for the command to exit.
433 Show the status of the process.
436 Other supported commands are listed in the optional variable
440 may have one of the following prefixes which alters its operation:
441 .Bl -tag -width "Prefix" -offset indent
443 Skip the check for an existing running process,
453 .Ar argument Ns Sy _precmd
454 returning non-zero, and ignores any of the
456 tests failing, and always returns a zero exit status.
460 being set to yes, 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 procname -offset indent
468 The name of this script.
469 This is not optional.
475 to determine if this method should be run.
477 The manual page containing information about
479 It will be part of the warning message shown when
485 Full path to the command.
487 .Ar argument Ns Sy _cmd
488 is defined for each supported keyword.
490 Optional arguments and/or shell directives for
492 .It Sy command_interpreter
495 .Dl #! command_interpreter [...]
499 .Dl command_interpreter [...] command
500 so use that string to find the PID(s) of the running command
503 .It Sy extra_commands
504 Extra commands/keywords/arguments supported.
507 Used to determine the PID(s) of the running command.
511 .Dl check_pidfile $pidfile $procname
516 .Dl check_process $procname
519 Process name to check for.
520 Defaults to the value of
523 Check for the existence of the listed directories
524 before running the default start method.
525 .It Sy required_files
526 Check for the readability of the listed files
527 before running the default start method.
531 on each of the list variables
532 before running the default start method.
541 .It Sy ${name}_chroot
550 List of additional or modified environment variables to set
557 This is usually set in
562 The environment variable
564 can be used to override this.
587 Group to run the chrooted
590 .It Sy ${name}_groups
591 Comma separated list of supplementary groups to run the chrooted
594 .It Ar argument Ns Sy _cmd
595 Shell commands which override the default method for
597 .It Ar argument Ns Sy _precmd
598 Shell commands to run just before running
599 .Ar argument Ns Sy _cmd
600 or the default method for
602 If this returns a non-zero exit code, the main method is not performed.
603 If the default method is being executed, this check is performed after
606 checks and process (non-)existence checks.
607 .It Ar argument Ns Sy _postcmd
608 Shell commands to run if running
609 .Ar argument Ns Sy _cmd
610 or the default method for
612 returned a zero exit code.
614 Signal to send the processes to stop in the default
620 Signal to send the processes to reload in the default
630 .Ar argument Ns Sy _cmd
631 is not defined, then a default method is provided by
633 .Bl -tag -width "argument" -offset indent
640 .Ic checkyesno Sy rcvar
644 Determine the PIDs of
660 instead, and doesn't run
671 or some other script specific status operation.
679 variable is used (if any).
680 This method always works, even if the appropriate
686 The following variables are available to the methods
688 .Ar argument Ns Sy _cmd )
692 .Bl -tag -width "rc_flags" -offset indent
696 after fast and force processing has been performed.
698 Flags to start the default command with.
701 unless overridden by the environment variable
703 This variable may be changed by the
704 .Ar argument Ns Sy _precmd
719 .It Ic run_rc_script Ar file Ar argument
724 and handle the return value from the script.
726 Various shell variables are unset before
729 .Bd -ragged -offset indent
733 .Sy command_interpreter ,
740 .Ar argument Ns Sy _cmd ,
741 .Ar argument Ns Sy _precmd .
742 .Ar argument Ns Sy _postcmd .
745 The startup behaviour of
747 depends upon the following checks:
754 it is sourced into the current shell.
758 appears to be a backup or scratch file
759 (e.g., with a suffix of
769 is not executable, ignore it.
774 .Sy rc_fast_and_loose
781 into the current shell.
789 then the command is executed using
790 .Ic no_rc_postprocess .
793 Prevent booting to multiuser mode.
800 signal is sent to the parent
801 process (which is assumed to be
803 Otherwise, the shell exits with status
806 Display one of the characters
807 .Sq \&/ , \&- , \&\e , \&| ,
808 followed by a backspace.
809 Repeated calls to this function will create the appearance of a spinning
810 symbol, as a different character is displayed on each call.
813 so this function may be useful even inside a script whose output
815 .It Ic wait_for_pids Op Ar pid Op Ar ...
816 Wait until all of the provided
818 don't exist any more, printing the list of outstanding
821 .It Ic warn Ar message
822 Display a warning message to
824 and log it to the system log
827 The warning message consists of the script name
834 .It Ic yesno_to_truefalse Ar var
835 Change the value of the specified variable from any of the
836 forms acceptable to the
844 .Bl -tag -width /etc/rc.subr -compact
860 support functions appeared in
864 post-processor appeared in