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