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