xref: /freebsd/share/man/man8/rc.subr.8 (revision 3157ba21)
1.\" 	$NetBSD: rc.subr.8,v 1.12 2004/01/06 00:52:24 lukem 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.\" $FreeBSD$
31.\"
32.Dd May 18, 2007
33.Dt RC.SUBR 8
34.Os
35.Sh NAME
36.Nm rc.subr
37.Nd functions used by system shell scripts
38.Sh SYNOPSIS
39.Bl -item -compact
40.It
41.Ic .\& Pa /etc/rc.subr
42.Pp
43.It
44.Ic backup_file Ar action Ar file Ar current Ar backup
45.It
46.Ic checkyesno Ar var
47.It
48.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
49.It
50.Ic check_process Ar procname Op Ar interpreter
51.It
52.Ic debug Ar message
53.It
54.Ic err Ar exitval Ar message
55.It
56.Ic force_depend Ar name
57.It
58.Ic info Ar message
59.It
60.Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
61.It
62.Ic load_rc_config Ar name
63.It
64.Ic load_rc_config_var Ar name Ar var
65.It
66.Ic mount_critical_filesystems Ar type
67.It
68.Ic rc_usage Ar command ...
69.It
70.Ic reverse_list Ar item ...
71.It
72.Ic run_rc_command Ar argument
73.It
74.Ic run_rc_script Ar file Ar argument
75.It
76.Ic set_rcvar Op Ar base
77.It
78.Ic wait_for_pids Op Ar pid ...
79.It
80.Ic warn Ar message
81.El
82.Sh DESCRIPTION
83The
84.Nm
85script
86contains commonly used shell script functions and variable
87definitions which are used by various scripts such as
88.Xr rc 8 .
89Scripts required by ports in
90.Pa /usr/local/etc/rc.d
91will also eventually
92be rewritten to make use of it.
93.Pp
94The
95.Nm
96functions were mostly imported from
97.Nx .
98.Pp
99They are accessed by sourcing
100.Pa /etc/rc.subr
101into the current shell.
102.Pp
103The following shell functions are available:
104.Bl -tag -width 4n
105.It Ic backup_file Ar action file current backup
106Make a backup copy of
107.Ar file
108into
109.Ar current .
110If the
111.Xr rc.conf 5
112variable
113.Va backup_uses_rcs
114is
115.Dq Li YES ,
116use
117.Xr rcs 1
118to archive the previous version of
119.Ar current ,
120otherwise save the previous version of
121.Ar current
122as
123.Ar backup .
124.Pp
125The
126.Ar action
127argument
128may be one of the following:
129.Bl -tag -width ".Cm remove"
130.It Cm add
131.Ar file
132is now being backed up by or possibly re-entered into this backup mechanism.
133.Ar current
134is created, and if necessary, the
135.Xr rcs 1
136files are created as well.
137.It Cm update
138.Ar file
139has changed and needs to be backed up.
140If
141.Ar current
142exists, it is copied to
143.Ar backup
144or checked into
145.Xr rcs 1
146(if the repository file is old),
147and then
148.Ar file
149is copied to
150.Ar current .
151.It Cm remove
152.Ar file
153is no longer being tracked by this backup mechanism.
154If
155.Xr rcs 1
156is being used, an empty file is checked in and
157.Ar current
158is removed,
159otherwise
160.Ar current
161is moved to
162.Ar backup .
163.El
164.It Ic checkyesno Ar var
165Return 0 if
166.Ar var
167is defined to
168.Dq Li YES ,
169.Dq Li TRUE ,
170.Dq Li ON ,
171or
172.Ql 1 .
173Return 1 if
174.Ar var
175is defined to
176.Dq Li NO ,
177.Dq Li FALSE ,
178.Dq Li OFF ,
179or
180.Ql 0 .
181Otherwise, warn that
182.Ar var
183is not set correctly.
184The values are case insensitive.
185.Sy Note :
186.Ar var
187should be a variable name, not its value;
188.Ic checkyesno
189will expand the variable by itself.
190.It Ic check_pidfile Ar pidfile procname Op Ar interpreter
191Parses the first word of the first line of
192.Ar pidfile
193for a PID, and ensures that the process with that PID
194is running and its first argument matches
195.Ar procname .
196Prints the matching PID if successful, otherwise nothing.
197If
198.Ar interpreter
199is provided, parse the first line of
200.Ar procname ,
201ensure that the line is of the form:
202.Pp
203.Dl "#! interpreter [...]"
204.Pp
205and use
206.Ar interpreter
207with its optional arguments and
208.Ar procname
209appended as the process string to search for.
210.It Ic check_process Ar procname Op Ar interpreter
211Prints the PIDs of any processes that are running with a first
212argument that matches
213.Ar procname .
214.Ar interpreter
215is handled as per
216.Ic check_pidfile .
217.It Ic debug Ar message
218Display a debugging message to
219.Va stderr ,
220log it to the system log using
221.Xr logger 1 ,
222and
223return to the caller.
224The error message consists of the script name
225(from
226.Va $0 ) ,
227followed by
228.Dq Li ": DEBUG: " ,
229and then
230.Ar message .
231This function is intended to be used by developers
232as an aid to debugging scripts.
233It can be turned on or off
234by the
235.Xr rc.conf 5
236variable
237.Va rc_debug .
238.It Ic err Ar exitval message
239Display an error message to
240.Va stderr ,
241log it to the system log
242using
243.Xr logger 1 ,
244and
245.Ic exit
246with an exit value of
247.Ar exitval .
248The error message consists of the script name
249(from
250.Va $0 ) ,
251followed by
252.Dq Li ": ERROR: " ,
253and then
254.Ar message .
255.It Ic force_depend Ar name
256Output an advisory message and force the
257.Ar name
258service to start.
259The
260.Ar name
261argument is the
262.Xr basename 1
263component of the path to the script, usually
264.Pa /etc/rc.d/name .
265If the script fails for any reason it will output a warning
266and return with a return value of 1.
267If it was successful
268it will return 0.
269.It Ic info Ar message
270Display an informational message to
271.Va stdout ,
272and log it to the system log using
273.Xr logger 1 .
274The message consists of the script name
275(from
276.Va $0 ) ,
277followed by
278.Dq Li ": INFO: " ,
279and then
280.Ar message .
281The display of this informational output can be
282turned on or off by the
283.Xr rc.conf 5
284variable
285.Va rc_info .
286.It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
287Load
288.Ar file
289as a kernel module unless it is already loaded.
290For the purpose of checking the module status,
291either the exact module name can be specified using
292.Fl m ,
293or an
294.Xr egrep 1
295regular expression matching the module name can be supplied via
296.Fl e .
297By default, the module is assumed to have the same name as
298.Ar file ,
299which is not always the case.
300.It Ic load_rc_config Ar name
301Source in the configuration files for
302.Ar name .
303First,
304.Pa /etc/rc.conf
305is sourced if it has not yet been read in.
306Then,
307.Pa /etc/rc.conf.d/ Ns Ar name
308is sourced if it is an existing file.
309The latter may also contain other variable assignments to override
310.Ic run_rc_command
311arguments defined by the calling script, to provide an easy
312mechanism for an administrator to override the behaviour of a given
313.Xr rc.d 8
314script without requiring the editing of that script.
315.It Ic load_rc_config_var Ar name Ar var
316Read the
317.Xr rc.conf 5
318variable
319.Ar var
320for
321.Ar name
322and set in the current shell, using
323.Ic load_rc_config
324in a sub-shell to prevent unwanted side effects from other variable
325assignments.
326.It Ic mount_critical_filesystems Ar type
327Go through a list of critical file systems,
328as found in the
329.Xr rc.conf 5
330variable
331.Va critical_filesystems_ Ns Ar type ,
332mounting each one that
333is not currently mounted.
334.It Ic rc_usage Ar command ...
335Print a usage message for
336.Va $0 ,
337with
338.Ar commands
339being the list of valid arguments
340prefixed by
341.Sm off
342.Dq Bq Li fast | force | one .
343.Sm on
344.It Ic reverse_list Ar item ...
345Print the list of
346.Ar items
347in reverse order.
348.It Ic run_rc_command Ar argument
349Run the
350.Ar argument
351method for the current
352.Xr rc.d 8
353script, based on the settings of various shell variables.
354.Ic run_rc_command
355is extremely flexible, and allows fully functional
356.Xr rc.d 8
357scripts to be implemented in a small amount of shell code.
358.Pp
359.Ar argument
360is searched for in the list of supported commands, which may be one
361of:
362.Bl -tag -width ".Cm restart" -offset indent
363.It Cm start
364Start the service.
365This should check that the service is to be started as specified by
366.Xr rc.conf 5 .
367Also checks if the service is already running and refuses to start if
368it is.
369This latter check is not performed by standard
370.Fx
371scripts if the system is starting directly to multi-user mode, to
372speed up the boot process.
373.It Cm stop
374If the service is to be started as specified by
375.Xr rc.conf 5 ,
376stop the service.
377This should check that the service is running and complain if it is not.
378.It Cm restart
379Perform a
380.Cm stop
381then a
382.Cm start .
383Defaults to displaying the process ID of the program (if running).
384.It Cm rcvar
385Display which
386.Xr rc.conf 5
387variables are used to control the startup of the service (if any).
388.El
389.Pp
390If
391.Va pidfile
392or
393.Va procname
394is set, also support:
395.Bl -tag -width ".Cm restart" -offset indent
396.It Cm poll
397Wait for the command to exit.
398.It Cm status
399Show the status of the process.
400.El
401.Pp
402Other supported commands are listed in the optional variable
403.Va extra_commands .
404.Pp
405.Ar argument
406may have one of the following prefixes which alters its operation:
407.Bl -tag -width ".Li force" -offset indent
408.It Li fast
409Skip the check for an existing running process,
410and sets
411.Va rc_fast Ns = Ns Li YES .
412.It Li force
413Skip the checks for
414.Va rcvar
415being set to
416.Dq Li YES ,
417and sets
418.Va rc_force Ns = Ns Li YES .
419This ignores
420.Ar argument Ns Va _precmd
421returning non-zero, and ignores any of the
422.Va required_*
423tests failing, and always returns a zero exit status.
424.It Li one
425Skip the checks for
426.Va rcvar
427being set to
428.Dq Li YES ,
429but performs all the other prerequisite tests.
430.El
431.Pp
432.Ic run_rc_command
433uses the following shell variables to control its behaviour.
434Unless otherwise stated, these are optional.
435.Bl -tag -width ".Va procname" -offset indent
436.It Va name
437The name of this script.
438This is not optional.
439.It Va rcvar
440The value of
441.Va rcvar
442is checked with
443.Ic checkyesno
444to determine if this method should be run.
445.It Va command
446Full path to the command.
447Not required if
448.Ar argument Ns Va _cmd
449is defined for each supported keyword.
450Can be overridden by
451.Va ${name}_program .
452.It Va command_args
453Optional arguments and/or shell directives for
454.Va command .
455.It Va command_interpreter
456.Va command
457is started with:
458.Pp
459.Dl "#! command_interpreter [...]"
460.Pp
461which results in its
462.Xr ps 1
463command being:
464.Pp
465.Dl "command_interpreter [...] command"
466.Pp
467so use that string to find the PID(s) of the running command
468rather than
469.Va command .
470.It Va extra_commands
471Extra commands/keywords/arguments supported.
472.It Va pidfile
473Path to PID file.
474Used to determine the PID(s) of the running command.
475If
476.Va pidfile
477is set, use:
478.Pp
479.Dl "check_pidfile $pidfile $procname"
480.Pp
481to find the PID.
482Otherwise, if
483.Va command
484is set, use:
485.Pp
486.Dl "check_process $procname"
487.Pp
488to find the PID.
489.It Va procname
490Process name to check for.
491Defaults to the value of
492.Va command .
493.It Va required_dirs
494Check for the existence of the listed directories
495before running the
496.Cm start
497method.
498.It Va required_files
499Check for the readability of the listed files
500before running the
501.Cm start
502method.
503.It Va required_modules
504Ensure that the listed kernel modules are loaded
505before running the
506.Cm start
507method.
508This is done after invoking the commands from
509.Va start_precmd
510so that the missing modules are not loaded in vain
511if the preliminary commands indicate a error condition.
512A word in the list can have an optional
513.Dq Li : Ns Ar modname
514or
515.Dq Li ~ Ns Ar pattern
516suffix.
517The
518.Ar modname
519or
520.Ar pattern
521parameter is passed to
522.Ic load_kld
523through a
524.Fl m
525or
526.Fl e
527option, respectively.
528See the description of
529.Ic load_kld
530in this document for details.
531.It Va required_vars
532Perform
533.Ic checkyesno
534on each of the list variables
535before running the
536.Cm start
537method.
538.It Va ${name}_chdir
539Directory to
540.Ic cd
541to before running
542.Va command ,
543if
544.Va ${name}_chroot
545is not provided.
546.It Va ${name}_chroot
547Directory to
548.Xr chroot 8
549to before running
550.Va command .
551Only supported after
552.Pa /usr
553is mounted.
554.It Va ${name}_flags
555Arguments to call
556.Va command
557with.
558This is usually set in
559.Xr rc.conf 5 ,
560and not in the
561.Xr rc.d 8
562script.
563The environment variable
564.Sq Ev flags
565can be used to override this.
566.It Va ${name}_nice
567.Xr nice 1
568level to run
569.Va command
570as.
571Only supported after
572.Pa /usr
573is mounted.
574.It Va ${name}_program
575Full path to the command.
576Overrides
577.Va command
578if both are set, but has no effect if
579.Va command
580is unset.
581As a rule,
582.Va command
583should be set in the script while
584.Va ${name}_program
585should be set in
586.Xr rc.conf 5 .
587.It Va ${name}_user
588User to run
589.Va command
590as, using
591.Xr chroot 8
592if
593.Va ${name}_chroot
594is set, otherwise
595uses
596.Xr su 1 .
597Only supported after
598.Pa /usr
599is mounted.
600.It Va ${name}_group
601Group to run the chrooted
602.Va command
603as.
604.It Va ${name}_groups
605Comma separated list of supplementary groups to run the chrooted
606.Va command
607with.
608.It Ar argument Ns Va _cmd
609Shell commands which override the default method for
610.Ar argument .
611.It Ar argument Ns Va _precmd
612Shell commands to run just before running
613.Ar argument Ns Va _cmd
614or the default method for
615.Ar argument .
616If this returns a non-zero exit code, the main method is not performed.
617If the default method is being executed, this check is performed after
618the
619.Va required_*
620checks and process (non-)existence checks.
621.It Ar argument Ns Va _postcmd
622Shell commands to run if running
623.Ar argument Ns Va _cmd
624or the default method for
625.Ar argument
626returned a zero exit code.
627.It Va sig_stop
628Signal to send the processes to stop in the default
629.Cm stop
630method.
631Defaults to
632.Dv SIGTERM .
633.It Va sig_reload
634Signal to send the processes to reload in the default
635.Cm reload
636method.
637Defaults to
638.Dv SIGHUP .
639.El
640.Pp
641For a given method
642.Ar argument ,
643if
644.Ar argument Ns Va _cmd
645is not defined, then a default method is provided by
646.Ic run_rc_command :
647.Bl -tag -width ".Sy Argument" -offset indent
648.It Sy Argument
649.Sy Default method
650.It Cm start
651If
652.Va command
653is not running and
654.Ic checkyesno Va rcvar
655succeeds, start
656.Va command .
657.It Cm stop
658Determine the PIDs of
659.Va command
660with
661.Ic check_pidfile
662or
663.Ic check_process
664(as appropriate),
665.Ic kill Va sig_stop
666those PIDs, and run
667.Ic wait_for_pids
668on those PIDs.
669.It Cm reload
670Similar to
671.Cm stop ,
672except that it uses
673.Va sig_reload
674instead, and does not run
675.Ic wait_for_pids .
676Another difference from
677.Cm stop
678is that
679.Cm reload
680is not provided by default.
681It can be enabled via
682.Va extra_commands
683if appropriate:
684.Pp
685.Dl "extra_commands=reload"
686.It Cm restart
687Runs the
688.Cm stop
689method, then the
690.Cm start
691method.
692.It Cm status
693Show the PID of
694.Va command ,
695or some other script specific status operation.
696.It Cm poll
697Wait for
698.Va command
699to exit.
700.It Cm rcvar
701Display which
702.Xr rc.conf 5
703variable is used (if any).
704This method always works, even if the appropriate
705.Xr rc.conf 5
706variable is set to
707.Dq Li NO .
708.El
709.Pp
710The following variables are available to the methods
711(such as
712.Ar argument Ns Va _cmd )
713as well as after
714.Ic run_rc_command
715has completed:
716.Bl -tag -width ".Va rc_flags" -offset indent
717.It Va rc_arg
718Argument provided to
719.Ic run_rc_command ,
720after fast and force processing has been performed.
721.It Va rc_flags
722Flags to start the default command with.
723Defaults to
724.Va ${name}_flags ,
725unless overridden by the environment variable
726.Sq Ev flags .
727This variable may be changed by the
728.Ar argument Ns Va _precmd
729method.
730.It Va rc_pid
731PID of
732.Va command
733(if appropriate).
734.It Va rc_fast
735Not empty if
736.Dq Li fast
737prefix was used.
738.It Va rc_force
739Not empty if
740.Dq Li force
741prefix was used.
742.El
743.It Ic run_rc_script Ar file argument
744Start the script
745.Ar file
746with an argument of
747.Ar argument ,
748and handle the return value from the script.
749.Pp
750Various shell variables are unset before
751.Ar file
752is started:
753.Bd -ragged -offset indent
754.Va name ,
755.Va command ,
756.Va command_args ,
757.Va command_interpreter ,
758.Va extra_commands ,
759.Va pidfile ,
760.Va rcvar ,
761.Va required_dirs ,
762.Va required_files ,
763.Va required_vars ,
764.Ar argument Ns Va _cmd ,
765.Ar argument Ns Va _precmd .
766.Ar argument Ns Va _postcmd .
767.Ed
768.Pp
769The startup behaviour of
770.Ar file
771depends upon the following checks:
772.Bl -enum
773.It
774If
775.Ar file
776ends in
777.Pa .sh ,
778it is sourced into the current shell.
779.It
780If
781.Ar file
782appears to be a backup or scratch file
783(e.g., with a suffix of
784.Pa ~ , # , .OLD ,
785or
786.Pa .orig ) ,
787ignore it.
788.It
789If
790.Ar file
791is not executable, ignore it.
792.It
793If the
794.Xr rc.conf 5
795variable
796.Va rc_fast_and_loose
797is empty,
798source
799.Ar file
800in a sub shell,
801otherwise source
802.Ar file
803into the current shell.
804.El
805.It Ic stop_boot Op Ar always
806Prevent booting to multiuser mode.
807If the
808.Va autoboot
809variable is set to
810.Ql yes ,
811or
812.Ic checkyesno Ar always
813indicates a truth value, then a
814.Dv SIGTERM
815signal is sent to the parent
816process, which is assumed to be
817.Xr rc 8 .
818Otherwise, the shell exits with a non-zero status.
819.It Ic set_rcvar Op Ar base
820Set the variable name required to start a service.
821In
822.Fx
823a daemon is usually controlled by an
824.Xr rc.conf 5
825variable consisting of a daemon's name postfixed by the string
826.Dq Li "_enable" .
827This is not the case in
828.Nx .
829When the following line is included in a script:
830.Pp
831.Dl "rcvar=`set_rcvar`"
832.Pp
833this function will use the value of the
834.Va $name
835variable, which should be defined by the calling script,
836to construct the appropriate
837.Xr rc.conf 5
838knob.
839If the
840.Ar base
841argument is set it will use
842.Ar base
843instead of
844.Va $name .
845.It Ic wait_for_pids Op Ar pid ...
846Wait until all of the provided
847.Ar pids
848do not exist any more, printing the list of outstanding
849.Ar pids
850every two seconds.
851.It Ic warn Ar message
852Display a warning message to
853.Va stderr
854and log it to the system log
855using
856.Xr logger 1 .
857The warning message consists of the script name
858(from
859.Va $0 ) ,
860followed by
861.Dq Li ": WARNING: " ,
862and then
863.Ar message .
864.El
865.Sh FILES
866.Bl -tag -width ".Pa /etc/rc.subr" -compact
867.It Pa /etc/rc.subr
868The
869.Nm
870file resides in
871.Pa /etc .
872.El
873.Sh SEE ALSO
874.Xr rc.conf 5 ,
875.Xr rc 8
876.Sh HISTORY
877The
878.Nm
879script
880appeared in
881.Nx 1.3 .
882The
883.Xr rc.d 8
884support functions appeared in
885.Nx 1.5 .
886The
887.Nm
888script
889first appeared in
890.Fx 5.0 .
891