1 .\" $NetBSD: rc.subr.8,v 1.24 2009/09/11 19:48:52 wiz 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.
35 .Nd functions used by system shell scripts
41 .Ic backup_file Ar action Ar file Ar current Ar backup
45 .Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
47 .Ic check_process Ar procname Op Ar interpreter
49 .Ic err Ar exitval Ar message
51 .Ic load_rc_config Ar command
53 .Ic load_rc_config_var Ar command Ar var
55 .Ic mount_critical_filesystems Ar type
57 .Ic no_rc_postprocess Ar command Op Ar arguments
59 .Ic print_rc_metadata Ar string
61 .Ic print_rc_normal Ar string
63 .Ic rc_usage Ar command Op Ar ...
65 .Ic reverse_list Ar item Op Ar ...
67 .Ic run_rc_command Ar argument
69 .Ic run_rc_script Ar file Ar argument
75 .Ic wait_for_pids Op Ar pid Op Ar ...
79 .Ic yesno_to_truefalse Ar var
83 contains commonly used shell script functions which are used by
84 various scripts such as
86 and the periodic system services which are controlled by
95 functions are accessed by sourcing
97 into the current shell.
99 The following shell functions are available:
101 .It Ic backup_file Ar action Ar file Ar current Ar backup
102 Make a backup copy of
114 to archive the previous version of
116 otherwise save the previous version of
122 may be one of the following:
123 .Bl -tag -width remove
126 is now being backed up by or possibly re-entered into this backup mechanism.
128 is created, and if necessary, the
130 files are created as well.
133 has changed and needs to be backed up.
136 exists, it is copied to
140 (if the repository file is old),
147 is no longer being tracked by this backup mechanism.
150 is being used, an empty file is checked in and
158 .It Ic checkyesno Ar var
177 is not set correctly.
178 The values are case insensitive.
180 Note that the warning message shown by this function when
182 is not set references a manual page where the user can find more information.
183 Its name is picked up from the
186 .It Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
187 Parses the first word of the first line of
189 for a PID, and ensures that the process with that PID
190 is running and its first argument matches
192 Prints the matching PID if successful, otherwise nothing.
195 is provided, parse the first line of
197 ensure that the line is of the form
198 .Dl #! interpreter [...]
201 with its optional arguments and
203 appended as the process string to search for.
204 .It Ic check_process Ar procname Op Ar interpreter
205 Prints the PIDs of any processes that are running with a first
206 argument that matches
211 .It Ic err Ar exitval Ar message
212 Display an error message to
214 log it to the system log
219 with an exit value of
221 The error message consists of the script name
228 .It Ic load_rc_config Ar command
231 configuration files for
235 is sourced if it has not yet been read in.
237 .Pa /etc/rc.conf.d/ Ns Ar command
238 is sourced if it is an existing file.
239 The latter may also contain other variable assignments to override
241 arguments defined by the calling script, to provide an easy
242 mechanism for an administrator to override the behaviour of a given
244 script without requiring the editing of that script.
245 .It Ic load_rc_config_var Ar command Ar var
252 and set in the current shell, using
254 in a sub-shell to prevent unwanted side effects from other variable
256 .It Ic mount_critical_filesystems Ar type
257 Go through a list of critical file systems,
261 .Sy critical_filesystems_ Ns Ar type ,
262 mounting each one that
263 is not currently mounted.
264 .It Ic no_rc_postprocess Ar command Op Ar arguments
265 Execute the specified command with the specified arguments,
266 in such a way that its output bypasses the post-processor that
268 uses for most commands.
269 This implies that the output will not appear in the
271 file, and will appear on the console regardless of the
274 This is expected to be useful for interactive commands,
275 and this mechanism is automatically used by
277 when a script contains the
282 If invoked from a context that does not appear to be under the control of
284 then the command is executed without special treatment.
285 .It Ic print_rc_metadata Ar string
288 in such a way that it should be handled as meta-data by the
291 If invoked from a context that does not appear to be under the control of
299 script may invoke this function with an argument that begins with
301 followed by one line of arbitrary text;
302 the text will be logged by
304 but will not be displayed on the console.
306 The use of arguments that do not begin with
308 is reserved for internal use by
312 .It Ic print_rc_normal Ar string
315 in such a way that it should be handled as normal output by the
318 If invoked from a context that does not appear to be under the control of
322 is printed to standard output.
324 Ths intent is that a script that is run via the
325 .Fn no_rc_postprocess
326 function (so its output would ordinarily be invisible to the post-processor)
327 can nevertheless arrange for the post-processor to see things printed with
329 .It Ic rc_usage Ar command Op Ar ...
330 Print a usage message for
334 being the list of valid arguments
336 .Dq "[fast|force|one]" .
337 .It Ic reverse_list Ar item Op Ar ...
341 .It Ic run_rc_command Ar argument
344 method for the current
346 script, based on the settings of various shell variables.
348 is extremely flexible, and allows fully functional
350 scripts to be implemented in a small amount of shell code.
353 is searched for in the list of supported commands, which may be one
355 .Bl -tag -width restart -offset indent
358 This should check that the service is to be started as specified by
360 Also checks if the service is already running and refuses to start if
362 This latter check is not performed by standard
364 scripts if the system is starting directly to multi-user mode, to
365 speed up the boot process.
367 If the service is to be started as specified by
370 This should check that the service is running and complain if it's not.
376 Defaults to displaying the process ID of the program (if running).
380 variables are used to control the startup of the service (if any).
387 is set, also support:
388 .Bl -tag -width restart -offset indent
390 Wait for the command to exit.
392 Show the status of the process.
395 Other supported commands are listed in the optional variable
399 may have one of the following prefixes which alters its operation:
400 .Bl -tag -width "Prefix" -offset indent
402 Skip the check for an existing running process,
412 .Ar argument Ns Sy _precmd
413 returning non-zero, and ignores any of the
415 tests failing, and always returns a zero exit status.
419 being set to yes, but performs all the other prerequisite tests.
423 uses the following shell variables to control its behaviour.
424 Unless otherwise stated, these are optional.
425 .Bl -tag -width procname -offset indent
427 The name of this script.
428 This is not optional.
434 to determine if this method should be run.
436 The manual page containing information about
438 It will be part of the warning message shown when
444 Full path to the command.
446 .Ar argument Ns Sy _cmd
447 is defined for each supported keyword.
449 Optional arguments and/or shell directives for
451 .It Sy command_interpreter
454 .Dl #! command_interpreter [...]
458 .Dl command_interpreter [...] command
459 so use that string to find the PID(s) of the running command
462 .It Sy extra_commands
463 Extra commands/keywords/arguments supported.
466 Used to determine the PID(s) of the running command.
470 .Dl check_pidfile $pidfile $procname
475 .Dl check_process $procname
478 Process name to check for.
479 Defaults to the value of
482 Check for the existence of the listed directories
483 before running the default start method.
484 .It Sy required_files
485 Check for the readability of the listed files
486 before running the default start method.
490 on each of the list variables
491 before running the default start method.
500 .It Sy ${name}_chroot
509 List of additional or modified environment variables to set
516 This is usually set in
521 The environment variable
523 can be used to override this.
546 Group to run the chrooted
549 .It Sy ${name}_groups
550 Comma separated list of supplementary groups to run the chrooted
553 .It Ar argument Ns Sy _cmd
554 Shell commands which override the default method for
556 .It Ar argument Ns Sy _precmd
557 Shell commands to run just before running
558 .Ar argument Ns Sy _cmd
559 or the default method for
561 If this returns a non-zero exit code, the main method is not performed.
562 If the default method is being executed, this check is performed after
565 checks and process (non-)existence checks.
566 .It Ar argument Ns Sy _postcmd
567 Shell commands to run if running
568 .Ar argument Ns Sy _cmd
569 or the default method for
571 returned a zero exit code.
573 Signal to send the processes to stop in the default
579 Signal to send the processes to reload in the default
589 .Ar argument Ns Sy _cmd
590 is not defined, then a default method is provided by
592 .Bl -tag -width "argument" -offset indent
599 .Ic checkyesno Sy rcvar
603 Determine the PIDs of
619 instead, and doesn't run
630 or some other script specific status operation.
638 variable is used (if any).
639 This method always works, even if the appropriate
645 The following variables are available to the methods
647 .Ar argument Ns Sy _cmd )
651 .Bl -tag -width "rc_flags" -offset indent
655 after fast and force processing has been performed.
657 Flags to start the default command with.
660 unless overridden by the environment variable
662 This variable may be changed by the
663 .Ar argument Ns Sy _precmd
678 .It Ic run_rc_script Ar file Ar argument
683 and handle the return value from the script.
685 Various shell variables are unset before
688 .Bd -ragged -offset indent
692 .Sy command_interpreter ,
699 .Ar argument Ns Sy _cmd ,
700 .Ar argument Ns Sy _precmd .
701 .Ar argument Ns Sy _postcmd .
704 The startup behaviour of
706 depends upon the following checks:
713 it is sourced into the current shell.
717 appears to be a backup or scratch file
718 (e.g., with a suffix of
728 is not executable, ignore it.
733 .Sy rc_fast_and_loose
740 into the current shell.
748 then the command is executed using
749 .Ic no_rc_postprocess .
752 Prevent booting to multiuser mode.
759 signal is sent to the parent
760 process (which is assumed to be
762 Otherwise, the shell exits with status
765 Display one of the characters
766 .Sq \&/ , \&- , \&\e , \&| ,
767 followed by a backspace.
768 Repeated calls to this function will create the appearance of a spinning
769 symbol, as a different character is displayed on each call.
772 so this function may be useful even inside a script whose output
774 .It Ic wait_for_pids Op Ar pid Op Ar ...
775 Wait until all of the provided
777 don't exist any more, printing the list of outstanding
780 .It Ic warn Ar message
781 Display a warning message to
783 and log it to the system log
786 The warning message consists of the script name
793 .It Ic yesno_to_truefalse Ar var
794 Change the value of the specified variable from any of the
795 forms acceptable to the
803 .Bl -tag -width /etc/rc.subr -compact
819 support functions appeared in
823 post-processor appeared in