1 .\" $NetBSD: rc.subr.8,v 1.9 2002/07/08 16:14:55 atatat Exp $
2 .\" $FreeBSD: src/share/man/man8/rc.subr.8,v 1.3 2003/04/22 05:13:55 dougb Exp $
3 .\" $DragonFly: src/share/man/man8/rc.subr.8,v 1.1 2003/08/01 04:36:57 rob Exp $
4 .\" Copyright (c) 2002 The NetBSD Foundation, Inc.
5 .\" All rights reserved.
7 .\" This code is derived from software contributed to The NetBSD Foundation
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
18 .\" 3. All advertising materials mentioning features or use of this software
19 .\" must display the following acknowledgement:
20 .\" This product includes software developed by the NetBSD
21 .\" Foundation, Inc. and its contributors.
22 .\" 4. Neither the name of The NetBSD Foundation nor the names of its
23 .\" contributors may be used to endorse or promote products derived
24 .\" from this software without specific prior written permission.
26 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 .\" POSSIBILITY OF SUCH DAMAGE.
43 .Nd functions used by system shell scripts
49 .Ic backup_file Ar action Ar file Ar current Ar backup
53 .Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
55 .Ic check_process Ar procname Op Ar interpreter
59 .Ic err Ar exitval Ar message
61 .Ic force_depend Ar name
65 .Ic load_rc_config Ar command
67 .Ic mount_critical_filesystems Ar type
69 .Ic rc_usage Ar command Op Ar ...
71 .Ic reverse_list Ar item Op Ar ...
73 .Ic run_rc_command Ar argument
75 .Ic run_rc_script Ar file Ar argument
77 .Ic set_rcvar Op Ar base
79 .Ic wait_for_pids Op Ar pid Op Ar ...
85 contains commonly used shell script functions and variable
86 definitions which are used by various scripts such as
88 Scripts required by ports in
89 .Pa /usr/local/etc/rc.d
91 be rewritten to make use of it.
95 functions were mostly imported from
97 and it is intended that they remain synced between the
98 two projects. With that in mind there are several variable
99 definitions that can help in this regard. They are:
102 Its value will be either
106 depending on which OS it is running on.
112 The path and argument list to display only the
114 values instead of a name=value pair.
116 The path and argument to write or modify
123 functions are accessed by sourcing
125 into the current shell.
127 The following shell functions are available:
130 .Ic backup_file Ar action Ar file Ar current Ar backup
132 Make a backup copy of
144 to archive the previous version of
146 otherwise save the previous version of
152 may be one of the following:
153 .Bl -tag -width remove
156 is now being backed up by or possibly re-entered into this backup mechanism.
158 is created, and if necessary, the
160 files are created as well.
163 has changed and needs to be backed up.
166 exists, it is copied to
170 (if the repository file is old),
177 is no longer being tracked by this backup mechanism.
180 is being used, an empty file is checked in and
188 .It Ic checkyesno Ar var
207 is not set correctly.
208 The values are case insensitive.
215 Parses the first word of the first line of
217 for a PID, and ensures that the process with that PID
218 is running and its first argument matches
220 Prints the matching PID if successfull, otherwise nothing.
223 is provided, parse the first line of
225 ensure that the line is of the form
226 .Dl #! interpreter [...]
229 with its optional arguments and
231 appended as the process string to search for.
232 .It Ic check_process Ar procname Op Ar interpreter
233 Prints the PIDs of any processes that are running with a first
234 argument that matches
239 .It Ic debug Ar message
240 Display a debugging message to
242 log it to the system log using
245 return to the caller.
246 The error message consists of the script name
253 This function is intended to be used by developers
254 as an aid to debugging scripts. It can be turned on or off
259 .It Ic err Ar exitval Ar message
260 Display an error message to
262 log it to the system log
267 with an exit value of
269 The error message consists of the script name
276 .It Ic force_depend name
277 Output an advisory message and force the
279 service to start. The
283 component of the path to the script, usually
285 If the script fails for any reason it will output a warning
286 and return with a return value of 1. If it was successful
288 .It Ic info Ar message
289 Display an informational message to
291 and log it to the system log using
293 The message consists of the script name
300 The display of this informational output can be
301 turned on or off by the
305 .It Ic load_rc_config Ar command
306 Source in the configuration files for
310 is sourced if it has not yet been read in.
312 .Pa /etc/rc.conf.d/ Ns Ar command
313 is sourced if it is an existing file.
314 The latter may also contain other variable assignments to override
316 arguments defined by the calling script, to provide an easy
317 mechanism for an administrator to override the behaviour of a given
319 script without requiring the editing of that script.
320 .It Ic mount_critical_filesystems Ar type
321 Go through a list of critical file systems,
325 .Sy critical_filesystems_ Ns Ar type ,
326 mounting each one that
327 is not currently mounted.
328 .It Ic rc_usage Ar command Op Ar ...
329 Print a usage message for
333 being the list of valid arguments
336 .It Ic reverse_list Ar item Op Ar ...
340 .It Ic run_rc_command Ar argument
343 method for the current
345 script, based on the settings of various shell variables.
347 is extremely flexible, and allows fully functional
349 scripts to be implemented in a small amount of shell code.
352 is searched for in the list of supported commands, which may be one
354 .Dl start stop restart rcvar
355 as well as any word listed in the optional variable
365 may have one of the following prefixes which alters its operation:
366 .Bl -tag -width "Prefix" -offset indent -compact
370 Skip the check for an existing running process,
380 .Ar argument Ns Sy _precmd
381 returning non-zero, and ignores any of the
387 uses the following shell variables to control its behaviour.
388 Unless otherwise stated, these are optional.
389 .Bl -tag -width procname -offset indent
391 The name of this script.
392 This is not optional.
398 to determine if this method should be run.
400 Full path to the command.
402 .Ar argument Ns Sy _cmd
403 is defined for each supported keyword.
405 Optional arguments and/or shell directives for
407 .It Sy command_interpreter
410 .Dl #! command_interpreter [...]
414 .Dl command_interpreter [...] command
415 so use that string to find the PID(s) of the running command
418 .It Sy extra_commands
419 Extra commands/keywords/arguments supported.
422 Used to determine the PID(s) of the running command.
426 .Dl check_pidfile $pidfile $procname
431 .Dl check_process $procname
434 Process name to check for.
435 Defaults to the value of
438 Check for the existence of the listed directories
439 before running the default start method.
440 .It Sy required_files
441 Check for the readability of the listed files
442 before running the default start method.
446 on each of the list variables
447 before running the default start method.
456 .It Sy ${name}_chroot
468 This is usually set in
473 The environment variable
475 can be used to override this.
498 Group to run the chrooted
501 .It Sy ${name}_groups
502 Comma separated list of supplementary groups to run the chrooted
505 .It Ar argument Ns Sy _cmd
506 Shell commands which override the default method for
508 .It Ar argument Ns Sy _precmd
509 Shell commands to run just before running
510 .Ar argument Ns Sy _cmd
511 or the default method for
513 If this returns a non-zero exit code, the main method is not performed.
514 If the default method is being executed, this check is performed after
517 checks and process (non-)existence checks.
518 .It Ar argument Ns Sy _postcmd
519 Shell commands to run if running
520 .Ar argument Ns Sy _cmd
521 or the default method for
523 returned a zero exit code.
525 Signal to send the processes to stop in the default
531 Signal to send the processes to reload in the default
541 .Ar argument Ns Sy _cmd
542 is not defined, then a default method is provided by
544 .Bl -tag -width "argument" -offset indent
551 .Ic checkyesno Sy rcvar
555 Determine the PIDs of
571 instead, and doesn't run
582 or some other script specific status operation.
590 variable is used (if any).
591 This method always works, even if the appropriate
597 The following variables are available to the methods
599 .Ar argument Ns Sy _cmd )
603 .Bl -tag -width "rc_flags" -offset indent
607 after fast and force processing has been performed.
609 Flags to start the default command with.
612 unless overridden by the environment variable
614 This variable may be changed by the
615 .Ar argument Ns Sy _precmd
630 .It Ic run_rc_script Ar file Ar argument
635 and handle the return value from the script.
637 Various shell variables are unset before
640 .Bd -ragged -offset indent
644 .Sy command_interpreter ,
651 .Ar argument Ns Sy _cmd ,
652 .Ar argument Ns Sy _precmd .
653 .Ar argument Ns Sy _postcmd .
656 The startup behaviour of
658 depends upon the following checks:
665 it is sourced into the current shell.
669 appears to be a backup or scratch file
670 (e.g., with a suffix of
680 is not executable, ignore it.
685 .Sy rc_fast_and_loose
692 into the current shell.
694 .It Ic set_rcvar Op Ar base
695 Set the variable name required to start a service. In
697 a daemon is usually controlled by an
699 variable consisting of a daemon's name postfixed by the string
701 This is not the case in
703 When the following line is included in a script
705 .Dl rcvar=`set_rcvar`
707 This function will use the value of the
709 variable, which should be defined by the calling script, to construct the appropriate
713 argument is set it will use
717 .It Ic wait_for_pids Op Ar pid Op Ar ...
718 Wait until all of the provided
720 don't exist any more, printing the list of outstanding
723 .It Ic warn Ar message
724 Display a warning message to
726 and log it to the system log
729 The warning message consists of the script name
738 .Bl -tag -width /etc/rc.subr -compact
754 support functions appeared in