xref: /netbsd-src/share/man/man8/rc.subr.8 (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1.\" 	$NetBSD: rc.subr.8,v 1.21 2008/05/25 09:07:21 wiz Exp $
2.\"
3.\" Copyright (c) 2002-2004 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Luke Mewburn.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
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.\"
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.
29.\"
30.Dd May 25, 2008
31.Dt RC.SUBR 8
32.Os
33.Sh NAME
34.Nm rc.subr
35.Nd functions used by system shell scripts
36.Sh SYNOPSIS
37.Bl -item
38.It
39.Li . /etc/rc.subr
40.It
41.Ic backup_file Ar action Ar file Ar current Ar backup
42.It
43.Ic checkyesno Ar var
44.It
45.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
46.It
47.Ic check_process Ar procname Op Ar interpreter
48.It
49.Ic err Ar exitval Ar message
50.It
51.Ic load_rc_config Ar command
52.It
53.Ic load_rc_config_var Ar command Ar var
54.It
55.Ic mount_critical_filesystems Ar type
56.It
57.Ic rc_usage Ar command Op Ar ...
58.It
59.Ic reverse_list Ar item Op Ar ...
60.It
61.Ic run_rc_command Ar argument
62.It
63.Ic run_rc_script Ar file Ar argument
64.It
65.Ic stop_boot
66.It
67.Ic wait_for_pids Op Ar pid Op Ar ...
68.It
69.Ic warn Ar message
70.El
71.Sh DESCRIPTION
72.Nm
73contains commonly used shell script functions which are used by
74various scripts such as
75.Xr rc 8 ,
76and the periodic system services which are controlled by
77.Xr daily.conf 5 ,
78.Xr monthly.conf 5 ,
79.Xr security.conf 5 ,
80and
81.Xr weekly.conf 5 .
82.Pp
83The
84.Nm
85functions are accessed by sourcing
86.Pa /etc/rc.subr
87into the current shell.
88.Pp
89The following shell functions are available:
90.Bl -tag -width 4n
91.It Xo
92.Ic backup_file Ar action Ar file Ar current Ar backup
93.Xc
94Make a backup copy of
95.Ar file
96into
97.Ar current .
98If the
99.Xr rc.conf 5
100variable
101.Sy backup_uses_rcs
102is
103.Sq YES ,
104use
105.Xr rcs 1
106to archive the previous version of
107.Ar current ,
108otherwise save the previous version of
109.Ar current
110as
111.Ar backup .
112.Pp
113.Ar action
114may be one of the following:
115.Bl -tag -width remove
116.It Sy add
117.Ar file
118is now being backed up by or possibly re-entered into this backup mechanism.
119.Ar current
120is created, and if necessary, the
121.Xr rcs 1
122files are created as well.
123.It Sy update
124.Ar file
125has changed and needs to be backed up.
126If
127.Ar current
128exists, it is copied to
129.Ar backup
130or checked into
131.Xr rcs 1
132(if the repository file is old),
133and then
134.Ar file
135is copied to
136.Ar current .
137.It Sy remove
138.Ar file
139is no longer being tracked by this backup mechanism.
140If
141.Xr rcs 1
142is being used, an empty file is checked in and
143.Ar current
144is removed,
145otherwise
146.Ar current
147is moved to
148.Ar backup .
149.El
150.It Ic checkyesno Ar var
151Return 0 if
152.Ar var
153is defined to
154.Sq YES ,
155.Sq TRUE ,
156.Sq ON ,
157or
158.Sq 1 .
159Return 1 if
160.Ar var
161is defined to
162.Sq NO ,
163.Sq FALSE ,
164.Sq OFF ,
165or
166.Sq 0 .
167Otherwise, warn that
168.Ar var
169is not set correctly.
170The values are case insensitive.
171.Pp
172Note that the warning message shown by this function when
173.Ar var
174is not set references a manual page where the user can find more information.
175Its name is picked up from the
176.Sy rcvar_manpage
177variable.
178.It Xo
179.Ic check_pidfile
180.Ar pidfile
181.Ar procname
182.Op Ar interpreter
183.Xc
184Parses the first word of the first line of
185.Ar pidfile
186for a PID, and ensures that the process with that PID
187is running and its first argument matches
188.Ar procname .
189Prints the matching PID if successful, otherwise nothing.
190If
191.Ar interpreter
192is provided, parse the first line of
193.Ar procname ,
194ensure that the line is of the form
195.Dl #! interpreter [...]
196and use
197.Ar interpreter
198with its optional arguments and
199.Ar procname
200appended as the process string to search for.
201.It Ic check_process Ar procname Op Ar interpreter
202Prints the PIDs of any processes that are running with a first
203argument that matches
204.Ar procname .
205.Ar interpreter
206is handled as per
207.Ic check_pidfile .
208.It Ic err Ar exitval Ar message
209Display an error message to
210.Em stderr ,
211log it to the system log
212using
213.Xr logger 1 ,
214and
215.Cm exit
216with an exit value of
217.Ar exitval .
218The error message consists of the script name
219(from
220.Sy $0 ) ,
221followed by
222.Dq ": ERROR: " ,
223and then
224.Ar message .
225.It Ic load_rc_config Ar command
226Source in the
227.Xr rc.conf 5
228configuration files for
229.Ar command .
230First,
231.Pa /etc/rc.conf
232is sourced if it has not yet been read in.
233Then,
234.Pa /etc/rc.conf.d/ Ns Ar command
235is sourced if it is an existing file.
236The latter may also contain other variable assignments to override
237.Ic run_rc_command
238arguments defined by the calling script, to provide an easy
239mechanism for an administrator to override the behaviour of a given
240.Xr rc.d 8
241script without requiring the editing of that script.
242.It Ic load_rc_config_var Ar command Ar var
243Read the
244.Xr rc.conf 5
245variable
246.Ar var
247for
248.Ar command
249and set in the current shell, using
250.Ic load_rc_config
251in a sub-shell to prevent unwanted side effects from other variable
252assignments.
253.It Ic mount_critical_filesystems Ar type
254Go through a list of critical file systems,
255as found in the
256.Xr rc.conf 5
257variable
258.Sy critical_filesystems_ Ns Ar type ,
259mounting each one that
260is not currently mounted.
261.It Ic rc_usage Ar command Op Ar ...
262Print a usage message for
263.Sy $0 ,
264with
265.Ar commands
266being the list of valid arguments
267prefixed by
268.Dq "[fast|force|one]" .
269.It Ic reverse_list Ar item Op Ar ...
270Print the list of
271.Ar items
272in reverse order.
273.It Ic run_rc_command Ar argument
274Run the
275.Ar argument
276method for the current
277.Xr rc.d 8
278script, based on the settings of various shell variables.
279.Ic run_rc_command
280is extremely flexible, and allows fully functional
281.Xr rc.d 8
282scripts to be implemented in a small amount of shell code.
283.Pp
284.Ar argument
285is searched for in the list of supported commands, which may be one
286of:
287.Bl -tag -width restart -offset indent
288.It Sy start
289Start the service.
290This should check that the service is to be started as specified by
291.Xr rc.conf 5 .
292Also checks if the service is already running and refuses to start if
293it is.
294This latter check is not performed by standard
295.Nx
296scripts if the system is starting directly to multi-user mode, to
297speed up the boot process.
298.It Sy stop
299If the service is to be started as specified by
300.Xr rc.conf 5 ,
301stop the service.
302This should check that the service is running and complain if it's not.
303.It Sy restart
304Perform a
305.Sy stop
306then a
307.Sy start .
308Defaults to displaying the process ID of the program (if running).
309.It Sy rcvar
310Display which
311.Xr rc.conf 5
312variables are used to control the startup of the service (if any).
313.El
314.Pp
315If
316.Sy pidfile
317or
318.Sy procname
319is set, also support:
320.Bl -tag -width restart -offset indent
321.It Sy poll
322Wait for the command to exit.
323.It Sy status
324Show the status of the process.
325.El
326.Pp
327Other supported commands are listed in the optional variable
328.Sy extra_commands .
329.Pp
330.Ar argument
331may have one of the following prefixes which alters its operation:
332.Bl -tag -width "Prefix" -offset indent
333.It Sy fast
334Skip the check for an existing running process,
335and sets
336.Sy rc_fast=YES .
337.It Sy force
338Skip the checks for
339.Sy rcvar
340being set to yes,
341and sets
342.Sy rc_force=YES .
343This ignores
344.Ar argument Ns Sy _precmd
345returning non-zero, and ignores any of the
346.Sy required_*
347tests failing, and always returns a zero exit status.
348.It Sy one
349Skip the checks for
350.Sy rcvar
351being set to yes, but performs all the other prerequisite tests.
352.El
353.Pp
354.Ic run_rc_command
355uses the following shell variables to control its behaviour.
356Unless otherwise stated, these are optional.
357.Bl -tag -width procname -offset indent
358.It Sy name
359The name of this script.
360This is not optional.
361.It Sy rcvar
362The value of
363.Sy rcvar
364is checked with
365.Ic checkyesno
366to determine if this method should be run.
367.It Sy rcvar_manpage
368The manual page containing information about
369.Sy rcvar .
370It will be part of the warning message shown when
371.Sy rcvar
372is undefined.
373Defaults to
374.Xr rc.conf 5 .
375.It Sy command
376Full path to the command.
377Not required if
378.Ar argument Ns Sy _cmd
379is defined for each supported keyword.
380.It Sy command_args
381Optional arguments and/or shell directives for
382.Sy command .
383.It Sy command_interpreter
384.Sy command
385is started with
386.Dl #! command_interpreter [...]
387which results in its
388.Xr ps 1
389command being
390.Dl command_interpreter [...] command
391so use that string to find the PID(s) of the running command
392rather than
393.Ql command .
394.It Sy extra_commands
395Extra commands/keywords/arguments supported.
396.It Sy pidfile
397Path to pid file.
398Used to determine the PID(s) of the running command.
399If
400.Sy pidfile
401is set, use
402.Dl check_pidfile $pidfile $procname
403to find the PID.
404Otherwise, if
405.Sy command
406is set, use
407.Dl check_process $procname
408to find the PID.
409.It Sy procname
410Process name to check for.
411Defaults to the value of
412.Sy command .
413.It Sy required_dirs
414Check for the existence of the listed directories
415before running the default start method.
416.It Sy required_files
417Check for the readability of the listed files
418before running the default start method.
419.It Sy required_vars
420Perform
421.Ic checkyesno
422on each of the list variables
423before running the default start method.
424.It Sy ${name}_chdir
425Directory to
426.Ic cd
427to before running
428.Sy command ,
429if
430.Sy ${name}_chroot
431is not provided.
432.It Sy ${name}_chroot
433Directory to
434.Xr chroot 8
435to before running
436.Sy command .
437Only supported after
438.Pa /usr
439is mounted.
440.It Sy ${name}_env
441List of additional or modified environment variables to set
442when starting
443.Sy command .
444.It Sy ${name}_flags
445Arguments to call
446.Sy command
447with.
448This is usually set in
449.Xr rc.conf 5 ,
450and not in the
451.Xr rc.d 8
452script.
453The environment variable
454.Sq Ev flags
455can be used to override this.
456.It Sy ${name}_nice
457.Xr nice 1
458level to run
459.Sy command
460as.
461Only supported after
462.Pa /usr
463is mounted.
464.It Sy ${name}_user
465User to run
466.Sy command
467as, using
468.Xr chroot 8 .
469if
470.Sy ${name}_chroot
471is set, otherwise
472uses
473.Xr su 1 .
474Only supported after
475.Pa /usr
476is mounted.
477.It Sy ${name}_group
478Group to run the chrooted
479.Sy command
480as.
481.It Sy ${name}_groups
482Comma separated list of supplementary groups to run the chrooted
483.Sy command
484with.
485.It Ar argument Ns Sy _cmd
486Shell commands which override the default method for
487.Ar argument .
488.It Ar argument Ns Sy _precmd
489Shell commands to run just before running
490.Ar argument Ns Sy _cmd
491or the default method for
492.Ar argument .
493If this returns a non-zero exit code, the main method is not performed.
494If the default method is being executed, this check is performed after
495the
496.Sy required_*
497checks and process (non-)existence checks.
498.It Ar argument Ns Sy _postcmd
499Shell commands to run if running
500.Ar argument Ns Sy _cmd
501or the default method for
502.Ar argument
503returned a zero exit code.
504.It Sy sig_stop
505Signal to send the processes to stop in the default
506.Sy stop
507method.
508Defaults to
509.Dv SIGTERM .
510.It Sy sig_reload
511Signal to send the processes to reload in the default
512.Sy reload
513method.
514Defaults to
515.Dv SIGHUP .
516.El
517.Pp
518For a given method
519.Ar argument ,
520if
521.Ar argument Ns Sy _cmd
522is not defined, then a default method is provided by
523.Sy run_rc_command :
524.Bl -tag -width "argument" -offset indent
525.It Sy Argument
526.Sy Default method
527.It Sy start
528If
529.Sy command
530is not running and
531.Ic checkyesno Sy rcvar
532succeeds, start
533.Sy command .
534.It Sy stop
535Determine the PIDs of
536.Sy command
537with
538.Ic check_pidfile
539or
540.Ic check_process
541(as appropriate),
542.Ic kill Sy sig_stop
543those PIDs, and run
544.Ic wait_for_pids
545on those PIDs.
546.It Sy reload
547Similar to
548.Sy stop ,
549except that it uses
550.Sy sig_reload
551instead, and doesn't run
552.Ic wait_for_pids .
553.It Sy restart
554Runs the
555.Sy stop
556method, then the
557.Sy start
558method.
559.It Sy status
560Show the PID of
561.Sy command ,
562or some other script specific status operation.
563.It Sy poll
564Wait for
565.Sy command
566to exit.
567.It Sy rcvar
568Display which
569.Xr rc.conf 5
570variable is used (if any).
571This method always works, even if the appropriate
572.Xr rc.conf 5
573variable is set to
574.Sq NO .
575.El
576.Pp
577The following variables are available to the methods
578(such as
579.Ar argument Ns Sy _cmd )
580as well as after
581.Ic run_rc_command
582has completed:
583.Bl -tag -width "rc_flags" -offset indent
584.It Sy rc_arg
585Argument provided to
586.Sy run_rc_command ,
587after fast and force processing has been performed.
588.It Sy rc_flags
589Flags to start the default command with.
590Defaults to
591.Sy ${name}_flags ,
592unless overridden by the environment variable
593.Sq Ev flags .
594This variable may be changed by the
595.Ar argument Ns Sy _precmd
596method.
597.It Sy rc_pid
598PID of
599.Sy command
600(if appropriate).
601.It Sy rc_fast
602Not empty if
603.Dq fast
604prefix was used.
605.It Sy rc_force
606Not empty if
607.Dq force
608prefix was used.
609.El
610.It Ic run_rc_script Ar file Ar argument
611Start the script
612.Ar file
613with an argument of
614.Ar argument ,
615and handle the return value from the script.
616.Pp
617Various shell variables are unset before
618.Ar file
619is started:
620.Bd -ragged -offset indent
621.Sy name ,
622.Sy command ,
623.Sy command_args ,
624.Sy command_interpreter ,
625.Sy extra_commands ,
626.Sy pidfile ,
627.Sy rcvar ,
628.Sy required_dirs ,
629.Sy required_files ,
630.Sy required_vars ,
631.Ar argument Ns Sy _cmd ,
632.Ar argument Ns Sy _precmd .
633.Ar argument Ns Sy _postcmd .
634.Ed
635.Pp
636The startup behaviour of
637.Ar file
638depends upon the following checks:
639.Bl -enum
640.It
641If
642.Ar file
643ends in
644.Pa .sh ,
645it is sourced into the current shell.
646.It
647If
648.Ar file
649appears to be a backup or scratch file
650(e.g., with a suffix of
651.Sq ~ ,
652.Sq # ,
653.Sq .OLD ,
654or
655.Sq .orig ) ,
656ignore it.
657.It
658If
659.Ar file
660is not executable, ignore it.
661.It
662If the
663.Xr rc.conf 5
664variable
665.Sy rc_fast_and_loose
666is empty,
667source
668.Ar file
669in a sub shell,
670otherwise source
671.Ar file
672into the current shell.
673.El
674.It Ic stop_boot
675Prevent booting to multiuser mode.
676If the
677.Sy autoboot
678variable is
679.Sq yes ,
680then a
681.Sy SIGTERM
682signal is sent to the parent
683process (which is assumed to be
684.Xr rc 8 ) .
685Otherwise, the shell exits with status
686.Li 1 .
687.It Ic wait_for_pids Op Ar pid Op Ar ...
688Wait until all of the provided
689.Ar pids
690don't exist any more, printing the list of outstanding
691.Ar pids
692every two seconds.
693.It Ic warn Ar message
694Display a warning message to
695.Em stderr
696and log it to the system log
697using
698.Xr logger 1 .
699The warning message consists of the script name
700(from
701.Sy $0 ) ,
702followed by
703.Dq ": WARNING: " ,
704and then
705.Ar message .
706.El
707.Sh FILES
708.Bl -tag -width /etc/rc.subr -compact
709.It Pa /etc/rc.subr
710The
711.Nm
712file resides in
713.Pa /etc .
714.El
715.Sh SEE ALSO
716.Xr rc.conf 5 ,
717.Xr rc 8
718.Sh HISTORY
719.Nm
720appeared in
721.Nx 1.3 .
722The
723.Xr rc.d 8
724support functions appeared in
725.Nx 1.5 .
726