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