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