xref: /freebsd/share/man/man8/rc.subr.8 (revision 0aa4a9fc859fd43343e2d7b5094a50d1ca0948eb)
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.Dd September 22, 2024
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 -compact
38.It
39.Ic .\& Pa /etc/rc.subr
40.Pp
41.It
42.Ic backup_file Ar action Ar file Ar current Ar backup
43.It
44.Ic checkyesno Ar var
45.It
46.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
47.It
48.Ic check_process Ar procname Op Ar interpreter
49.It
50.Ic DebugOn Ar tag ...
51.It
52.Ic DebugOff Ar tag ...
53.It
54.Ic debug Ar message
55.It
56.Ic dot Ar file ...
57.It
58.Ic err Ar exitval Ar message
59.It
60.Ic force_depend Ar name
61.It
62.Ic info Ar message
63.It
64.Ic is_verified Ar file
65.It
66.Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
67.It
68.Ic load_rc_config Oo Ar flag Oc Op Ar service
69.It
70.Ic load_rc_config_var Ar name Ar var
71.It
72.Ic mount_critical_filesystems Ar type
73.It
74.Ic rc_log Ar message
75.It
76.Ic rc_trace Ar level Ar message
77.It
78.Ic rc_usage Ar command ...
79.It
80.Ic reverse_list Ar item ...
81.It
82.Ic run_rc_command Ar argument
83.It
84.Ic run_rc_script Ar file Ar argument
85.It
86.Ic run_rc_scripts Oo options Oc Ar file ...
87.It
88.Ic safe_dot Ar file ...
89.It
90.Ic sdot Ar file ...
91.It
92.Ic startmsg Oo Fl n Oc Ar message
93.It
94.Ic vdot Ar file ...
95.It
96.Ic wait_for_pids Op Ar pid ...
97.It
98.Ic warn Ar message
99.El
100.Sh DESCRIPTION
101The
102.Nm
103script
104contains commonly used shell script functions and variable
105definitions which are used by various scripts such as
106.Xr rc 8 .
107Scripts required by ports in
108.Pa /usr/local/etc/rc.d
109will also eventually
110be rewritten to make use of it.
111.Pp
112The
113.Nm
114functions were mostly imported from
115.Nx .
116.Pp
117They are accessed by sourcing
118.Pa /etc/rc.subr
119into the current shell.
120.Pp
121The following shell functions are available:
122.Bl -tag -width 4n
123.It Ic backup_file Ar action file current backup
124Make a backup copy of
125.Ar file
126into
127.Ar current .
128Save the previous version of
129.Ar current
130as
131.Ar backup .
132.Pp
133The
134.Ar action
135argument
136may be one of the following:
137.Bl -tag -width "remove"
138.It Cm add
139.Ar file
140is now being backed up by or possibly re-entered into this backup mechanism.
141.Ar current
142is created.
143.It Cm update
144.Ar file
145has changed and needs to be backed up.
146If
147.Ar current
148exists, it is copied to
149.Ar backup
150and then
151.Ar file
152is copied to
153.Ar current .
154.It Cm remove
155.Ar file
156is no longer being tracked by this backup mechanism.
157.Ar current
158is moved to
159.Ar backup .
160.El
161.It Ic checkyesno Ar var
162Return 0 if
163.Ar var
164is defined to
165.Dq Li YES ,
166.Dq Li TRUE ,
167.Dq Li ON ,
168or
169.Ql 1 .
170Return 1 if
171.Ar var
172is defined to
173.Dq Li NO ,
174.Dq Li FALSE ,
175.Dq Li OFF ,
176or
177.Ql 0 .
178Otherwise, warn that
179.Ar var
180is not set correctly.
181The values are case insensitive.
182.Em Note :
183.Ar var
184should be a variable name, not its value;
185.Ic checkyesno
186will expand the variable by itself.
187.It Ic check_pidfile Ar pidfile procname Op Ar interpreter
188Parses the first word of the first line of
189.Ar pidfile
190for a PID, and ensures that the process with that PID
191is running and its first argument matches
192.Ar procname .
193Prints the matching PID if successful, otherwise nothing.
194If
195.Ar interpreter
196is provided, parse the first line of
197.Ar procname ,
198ensure that the line is of the form:
199.Pp
200.Dl "#! interpreter [...]"
201.Pp
202and use
203.Ar interpreter
204with its optional arguments and
205.Ar procname
206appended as the process string to search for.
207.It Ic check_process Ar procname Op Ar interpreter
208Prints the PIDs of any processes that are running with a first
209argument that matches
210.Ar procname .
211.Ar interpreter
212is handled as per
213.Ic check_pidfile .
214.It Ic DebugOn Ar tag ...
215Enable tracing if not already enabled,
216and any
217.Ar tag
218is found in
219.Va DEBUG_SH
220(a comma separated list of tags).
221.Pp
222Record the
223.Ar tag
224that caused it to be enabled in
225.Va DEBUG_ON ,
226set
227.Va DEBUG_DO
228empty and
229.Va DEBUG_SKIP
230to
231.Ql \&: .
232.Pp
233See
234.Xr debug.sh 8
235for more details.
236.It Ic DebugOff Ar tag ...
237Disable tracing if enabled and any
238.Ar tag
239matches
240.Va DEBUG_ON ,
241which means it was the reason tracing was enabled.
242.Pp
243Set
244.Va DEBUG_DO
245to
246.Ql \&: ,
247and
248.Va DEBUG_ON ,
249.Va DEBUG_SKIP
250empty.
251.It Ic debug Ar message
252Display a debugging message to
253.Va stderr ,
254log it to the system log using
255.Xr logger 1 ,
256and
257return to the caller.
258The error message consists of the script name
259(from
260.Va $0 ) ,
261followed by
262.Dq Li ": DEBUG: " ,
263and then
264.Ar message .
265This function is intended to be used by developers
266as an aid to debugging scripts.
267It can be turned on or off
268by the
269.Xr rc.conf 5
270variable
271.Va rc_debug .
272.It Ic dot Ar file ...
273For reading in unverified files.
274.Pp
275Ensure shell
276.Li verify
277option is off.
278This option is only meaningful when
279.Xr mac_veriexec 4
280is active.
281.Pp
282Read each
283.Ar file
284if it exists.
285.Pp
286Restore previous state of the
287.Li verify
288option.
289.It Ic err Ar exitval message
290Display an error message to
291.Va stderr ,
292log it to the system log
293using
294.Xr logger 1 ,
295and
296.Ic exit
297with an exit value of
298.Ar exitval .
299The error message consists of the script name
300(from
301.Va $0 ) ,
302followed by
303.Dq Li ": ERROR: " ,
304and then
305.Ar message .
306.It Ic force_depend Ar name
307Output an advisory message and force the
308.Ar name
309service to start.
310The
311.Ar name
312argument is the
313.Xr basename 1
314component of the path to the script located at
315.Pa /etc/rc.d
316(scripts stored in other locations such as
317.Pa /usr/local/etc/rc.d
318cannot be controlled with
319.Ic force_depend
320currently).
321If the script fails for any reason it will output a warning
322and return with a return value of 1.
323If it was successful
324it will return 0.
325.It Ic is_verified Ar file
326If
327.Xr veriexec 8
328does not exist, or
329.Xr mac_veriexec 4
330is not active, just return success.
331Otherwise use
332.Xr veriexec 8
333to check if
334.Ar file
335is verified.
336If not verified the return code will be 80 (EAUTH).
337.It Ic info Ar message
338Display an informational message to
339.Va stdout ,
340and log it to the system log using
341.Xr logger 1 .
342The message consists of the script name
343(from
344.Va $0 ) ,
345followed by
346.Dq Li ": INFO: " ,
347and then
348.Ar message .
349The display of this informational output can be
350turned on or off by the
351.Xr rc.conf 5
352variable
353.Va rc_info .
354.It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file
355Load
356.Ar file
357as a kernel module unless it is already loaded.
358For the purpose of checking the module status,
359either the exact module name can be specified using
360.Fl m ,
361or an
362.Xr egrep 1
363regular expression matching the module name can be supplied via
364.Fl e .
365By default, the module is assumed to have the same name as
366.Ar file ,
367which is not always the case.
368.It Ic load_rc_config Oo Ar flag Oc Op Ar service
369Source in the configuration file(s) for
370.Ar service .
371If no
372.Ar service
373is specified,
374only the global configuration file(s) will be loaded.
375First,
376.Pa /etc/rc.conf
377is sourced if it has not yet been read in.
378Then,
379.Pa /etc/rc.conf.d/ Ns Ar service
380is sourced if it is an existing file.
381The latter may also contain other variable assignments to override
382.Ic run_rc_command
383arguments defined by the calling script, to provide an easy
384mechanism for an administrator to override the behaviour of a given
385.Xr rc.d 8
386script without requiring the editing of that script.
387.Pp
388The function named by
389.Va load_rc_config_reader
390(default is
391.Ic dot )
392is used to read configuration unless
393.Ar flag
394is:
395.Bl -tag -width Ds
396.It Fl s
397use
398.Ic sdot
399to read configuration,
400because we want verified configuration or
401to use
402.Ic safe_dot
403to read an unverified configuration.
404.It Fl v
405use
406.Ic vdot
407to read in configuration only if it is verified.
408.El
409.Pp
410.Ic DebugOn
411will be called with tags derived from
412.Ar name
413to enable tracing if any appear in
414.Va DEBUG_SH .
415.It Ic load_rc_config_var Ar name Ar var
416Read the
417.Xr rc.conf 5
418variable
419.Ar var
420for
421.Ar name
422and set in the current shell, using
423.Ic load_rc_config
424in a sub-shell to prevent unwanted side effects from other variable
425assignments.
426.It Ic mount_critical_filesystems Ar type
427Go through a list of critical file systems,
428as found in the
429.Xr rc.conf 5
430variable
431.Va critical_filesystems_ Ns Ar type ,
432mounting each one that
433is not currently mounted.
434.It Ic rc_log Ar message
435Output
436.Ar message
437with a timestamp, which is both human readable and
438easily parsed for post processing, using:
439.Bd -literal -offset indent
440date "+@ %s [%Y-%m-%d %H:%M:%S %Z] $*"
441.Ed
442.It Ic rc_trace Ar level Ar message
443If the file
444.Pa /etc/rc.conf.d/rc_trace
445exists and is not empty attempt to set
446.Va RC_LEVEL
447based on its content.
448If the file is empty or does not contain
449a value for
450.Va RC_LEVEL ,
451set it to
452.Li 0 .
453.Pp
454If
455.Ar level
456is greater than or equal to
457.Va RC_LEVEL
458pass
459.Ar message
460to
461.Ic rc_log .
462.It Ic rc_usage Ar command ...
463Print a usage message for
464.Va $0 ,
465with
466.Ar commands
467being the list of valid arguments
468prefixed by
469.Sm off
470.Dq Bq Li fast | force | one | quiet .
471.Sm on
472.It Ic reverse_list Ar item ...
473Print the list of
474.Ar items
475in reverse order.
476.It Ic run_rc_command Ar argument
477Run the
478.Ar argument
479method for the current
480.Xr rc.d 8
481script, based on the settings of various shell variables.
482.Ic run_rc_command
483is extremely flexible, and allows fully functional
484.Xr rc.d 8
485scripts to be implemented in a small amount of shell code.
486.Pp
487.Ar argument
488is searched for in the list of supported commands, which may be one
489of:
490.Bl -tag -width "restart" -offset indent
491.It Cm start
492Start the service.
493This should check that the service is to be started as specified by
494.Xr rc.conf 5 .
495Also checks if the service is already running and refuses to start if
496it is.
497This latter check is not performed by standard
498.Fx
499scripts if the system is starting directly to multi-user mode, to
500speed up the boot process.
501.It Cm stop
502If the service is to be started as specified by
503.Xr rc.conf 5 ,
504stop the service.
505This should check that the service is running and complain if it is not.
506.It Cm restart
507Perform a
508.Cm stop
509then a
510.Cm start .
511Defaults to displaying the process ID of the program (if running).
512.It Cm enabled
513Return 0 if the service is enabled and 1 if it is not.
514This command does not print anything.
515.It Cm rcvar
516Display which
517.Xr rc.conf 5
518variables are used to control the startup of the service (if any).
519.El
520.Pp
521If
522.Va pidfile
523or
524.Va procname
525is set, also support:
526.Bl -tag -width "status" -offset indent
527.It Cm poll
528Wait for the command to exit.
529.It Cm status
530Show the status of the process.
531.El
532.Pp
533Other supported commands are listed in the optional variable
534.Va extra_commands .
535.Pp
536.Ar argument
537may have one of the following prefixes which alters its operation:
538.Bl -tag -width "force" -offset indent
539.It Li fast
540Skip the check for an existing running process,
541and sets
542.Va rc_fast Ns = Ns Li YES .
543.It Li force
544Skip the checks for
545.Va rcvar
546being set to
547.Dq Li YES ,
548and sets
549.Va rc_force Ns = Ns Li YES .
550This ignores
551.Ar argument Ns Va _precmd
552returning non-zero, and ignores any of the
553.Va required_*
554tests failing, and always returns a zero exit status.
555.It Li one
556Skip the checks for
557.Va rcvar
558being set to
559.Dq Li YES ,
560but performs all the other prerequisite tests.
561.It Li quiet
562Inhibits some verbose diagnostics.
563Currently, this includes messages
564.Qq Starting ${name}
565(as checked by
566.Ic check_startmsgs
567inside
568.Nm )
569and errors about usage of services that are not enabled in
570.Xr rc.conf 5 .
571This prefix also sets
572.Va rc_quiet Ns = Ns Li YES .
573.Em Note :
574.Va rc_quiet
575is not intended to completely mask all debug and warning messages,
576but only certain small classes of them.
577.El
578.Pp
579.Ic run_rc_command
580uses the following shell variables to control its behaviour.
581Unless otherwise stated, these are optional.
582.Bl -tag -width "procname" -offset indent
583.It Va name
584The name of this script.
585This is not optional.
586.It Va rcvar
587The value of
588.Va rcvar
589is checked with
590.Ic checkyesno
591to determine if this method should be run.
592.It Va command
593Full path to the command.
594Not required if
595.Ar argument Ns Va _cmd
596is defined for each supported keyword.
597Can be overridden by
598.Va ${name}_program .
599.It Va command_args
600Optional arguments and/or shell directives for
601.Va command .
602.It Va command_interpreter
603.Va command
604is started with:
605.Pp
606.Dl "#! command_interpreter [...]"
607.Pp
608which results in its
609.Xr ps 1
610command being:
611.Pp
612.Dl "command_interpreter [...] command"
613.Pp
614so use that string to find the PID(s) of the running command
615rather than
616.Va command .
617.It Va extra_commands
618Extra commands/keywords/arguments supported.
619.It Va pidfile
620Path to PID file.
621Used to determine the PID(s) of the running command.
622If
623.Va pidfile
624is set, use:
625.Pp
626.Dl "check_pidfile $pidfile $procname"
627.Pp
628to find the PID.
629Otherwise, if
630.Va command
631is set, use:
632.Pp
633.Dl "check_process $procname"
634.Pp
635to find the PID.
636.It Va procname
637Process name to check for.
638Defaults to the value of
639.Va command .
640.It Va required_dirs
641Check for the existence of the listed directories
642before running the
643.Cm start
644method.
645The list is checked before running
646.Va start_precmd .
647.It Va required_files
648Check for the readability of the listed files
649before running the
650.Cm start
651method.
652The list is checked before running
653.Va start_precmd .
654.It Va required_modules
655Ensure that the listed kernel modules are loaded
656before running the
657.Cm start
658method.
659The list is checked after running
660.Va start_precmd .
661This is done after invoking the commands from
662.Va start_precmd
663so that the missing modules are not loaded in vain
664if the preliminary commands indicate a error condition.
665A word in the list can have an optional
666.Dq Li \&: Ns Ar modname
667or
668.Dq Li ~ Ns Ar pattern
669suffix.
670The
671.Ar modname
672or
673.Ar pattern
674parameter is passed to
675.Ic load_kld
676through a
677.Fl m
678or
679.Fl e
680option, respectively.
681See the description of
682.Ic load_kld
683in this document for details.
684.It Va required_vars
685Perform
686.Ic checkyesno
687on each of the list variables
688before running the
689.Cm start
690method.
691The list is checked after running
692.Va start_precmd .
693.It Va ${name}_chdir
694Directory to
695.Ic cd
696to before running
697.Va command ,
698if
699.Va ${name}_chroot
700is not provided.
701.It Va ${name}_chroot
702Directory to
703.Xr chroot 8
704to before running
705.Va command .
706Only supported after
707.Pa /usr
708is mounted.
709.It Va ${name}_env
710A list of environment variables to run
711.Va command
712with.
713Those variables will be passed as arguments to the
714.Xr env 1
715utility unless
716.Ar argument Ns Va _cmd
717is defined.
718In that case the contents of
719.Va ${name}_env
720will be exported via the
721.Xr export 1
722builtin of
723.Xr sh 1 ,
724which puts some limitations on the names of variables
725(e.g., a variable name may not start with a digit).
726.It Va ${name}_env_file
727A file to source for environmental variables to run
728.Va command
729with.
730.Em Note :
731all the variables which are being assigned in this file are going
732to be exported into the environment of
733.Va command .
734.It Va ${name}_fib
735FIB
736.Pa Routing Table
737number to run
738.Va command
739with.
740See
741.Xr setfib 1
742for more details.
743.It Va ${name}_flags
744Arguments to call
745.Va command
746with.
747This is usually set in
748.Xr rc.conf 5 ,
749and not in the
750.Xr rc.d 8
751script.
752The environment variable
753.Sq Ev flags
754can be used to override this.
755.It Va ${name}_nice
756.Xr nice 1
757level to run
758.Va command
759as.
760Only supported after
761.Pa /usr
762is mounted.
763.It Va ${name}_limits
764Resource limits to apply to
765.Va command .
766This will be passed as arguments to the
767.Xr limits 1
768utility.
769By default, the resource limits are based on the login class defined in
770.Va ${name}_login_class .
771.It Va ${name}_login_class
772Login class to use with
773.Va ${name}_limits .
774Defaults to
775.Dq Li daemon .
776.It Va ${name}_offcmd
777Shell commands to run during start if a service is not enabled.
778.It Va ${name}_oomprotect
779.Xr protect 1
780.Va command
781from being killed when swap space is exhausted.
782If
783.Dq Li YES
784is used, no child processes are protected.
785If
786.Dq Li ALL ,
787protect all child processes.
788.It Va ${name}_program
789Full path to the command.
790Overrides
791.Va command
792if both are set, but has no effect if
793.Va command
794is unset.
795As a rule,
796.Va command
797should be set in the script while
798.Va ${name}_program
799should be set in
800.Xr rc.conf 5 .
801.It Va ${name}_user
802User to run
803.Va command
804as, using
805.Xr chroot 8
806if
807.Va ${name}_chroot
808is set, otherwise
809uses
810.Xr su 1 .
811Only supported after
812.Pa /usr
813is mounted.
814.It Va ${name}_group
815Group to run the chrooted
816.Va command
817as.
818.It Va ${name}_groups
819Comma separated list of supplementary groups to run the chrooted
820.Va command
821with.
822.It Va ${name}_prepend
823Commands to be prepended to
824.Va command .
825This is a generic version of
826.Va ${name}_env ,
827.Va ${name}_fib ,
828or
829.Va ${name}_nice .
830.It Va ${name}_setup
831Optional command to be run during
832.Cm start ,
833.Cm restart ,
834and
835.Cm reload
836prior to the respective
837.Ar argument Ns Va _precmd .
838If the command fails for any reason it will output a warning,
839but execution will continue.
840.It Ar argument Ns Va _cmd
841Shell commands which override the default method for
842.Ar argument .
843.It Ar argument Ns Va _precmd
844Shell commands to run just before running
845.Ar argument Ns Va _cmd
846or the default method for
847.Ar argument .
848If this returns a non-zero exit code, the main method is not performed.
849If the default method is being executed, this check is performed after
850the
851.Va required_*
852checks and process (non-)existence checks.
853.It Ar argument Ns Va _postcmd
854Shell commands to run if running
855.Ar argument Ns Va _cmd
856or the default method for
857.Ar argument
858returned a zero exit code.
859.It Va sig_stop
860Signal to send the processes to stop in the default
861.Cm stop
862method.
863Defaults to
864.Dv SIGTERM .
865.It Va sig_reload
866Signal to send the processes to reload in the default
867.Cm reload
868method.
869Defaults to
870.Dv SIGHUP .
871.El
872.Pp
873For a given method
874.Ar argument ,
875if
876.Ar argument Ns Va _cmd
877is not defined, then a default method is provided by
878.Ic run_rc_command :
879.Bl -column "Argument" "Default Method" -offset indent
880.It Sy Argument Ta Sy Default method
881.It Cm start Ta
882If
883.Va command
884is not running and
885.Ic checkyesno Va rcvar
886succeeds, start
887.Va command .
888.It Cm stop Ta
889Determine the PIDs of
890.Va command
891with
892.Ic check_pidfile
893or
894.Ic check_process
895(as appropriate),
896.Ic kill Va sig_stop
897those PIDs, and run
898.Ic wait_for_pids
899on those PIDs.
900.It Cm reload Ta
901Similar to
902.Cm stop ,
903except that it uses
904.Va sig_reload
905instead, and does not run
906.Ic wait_for_pids .
907Another difference from
908.Cm stop
909is that
910.Cm reload
911is not provided by default.
912It can be enabled via
913.Va extra_commands
914if appropriate:
915.Pp
916.Dl "extra_commands=reload"
917.It Cm restart Ta
918Runs the
919.Cm stop
920method, then the
921.Cm start
922method.
923.It Cm status Ta
924Show the PID of
925.Va command ,
926or some other script specific status operation.
927.It Cm poll Ta
928Wait for
929.Va command
930to exit.
931.It Cm rcvar Ta
932Display which
933.Xr rc.conf 5
934variable is used (if any).
935This method always works, even if the appropriate
936.Xr rc.conf 5
937variable is set to
938.Dq Li NO .
939.El
940.Pp
941The following variables are available to the methods
942(such as
943.Ar argument Ns Va _cmd )
944as well as after
945.Ic run_rc_command
946has completed:
947.Bl -tag -width "rc_service" -offset indent
948.It Va rc_arg
949Argument provided to
950.Ic run_rc_command ,
951after fast and force processing has been performed.
952.It Va rc_flags
953Flags to start the default command with.
954Defaults to
955.Va ${name}_flags ,
956unless overridden by the environment variable
957.Sq Ev flags .
958This variable may be changed by the
959.Ar argument Ns Va _precmd
960method.
961.It Va rc_service
962Path to the service script being executed, in case it needs to re-invoke itself.
963.It Va rc_pid
964PID of
965.Va command
966(if appropriate).
967.It Va rc_fast
968Not empty if
969.Dq Li fast
970prefix was used.
971.It Va rc_force
972Not empty if
973.Dq Li force
974prefix was used.
975.El
976.It Ic run_rc_script Ar file argument
977Start the script
978.Ar file
979with an argument of
980.Ar argument ,
981and handle the return value from the script.
982.Pp
983Various shell variables are unset before
984.Ar file
985is started:
986.Bd -ragged -offset indent
987.Va name ,
988.Va command ,
989.Va command_args ,
990.Va command_interpreter ,
991.Va extra_commands ,
992.Va pidfile ,
993.Va rcvar ,
994.Va required_dirs ,
995.Va required_files ,
996.Va required_vars ,
997.Ar argument Ns Va _cmd ,
998.Ar argument Ns Va _precmd .
999.Ar argument Ns Va _postcmd .
1000.Ed
1001.Pp
1002Call
1003.Ic rc_trace
1004to indicate that
1005.Ar file
1006is to be run.
1007.Pp
1008However, if
1009.Ic is_verified Ar file
1010fails, just return.
1011.Pp
1012.Ic DebugOn
1013will be called with tags derrived from
1014.Va name
1015and
1016.Va rc_arg
1017to enable tracing if any of those tags appear in
1018.Va DEBUG_SH .
1019.Pp
1020The startup behaviour of
1021.Ar file
1022depends upon the following checks:
1023.Bl -enum
1024.It
1025If
1026.Ar file
1027ends in
1028.Pa .sh ,
1029it is sourced into the current shell.
1030.It
1031If
1032.Ar file
1033appears to be a backup or scratch file
1034(e.g., with a suffix of
1035.Pa ~ , # , .OLD ,
1036or
1037.Pa .orig ) ,
1038ignore it.
1039.It
1040If
1041.Ar file
1042is not executable, ignore it.
1043.It
1044If the
1045.Xr rc.conf 5
1046variable
1047.Va rc_fast_and_loose
1048is empty,
1049source
1050.Ar file
1051in a sub shell,
1052otherwise source
1053.Ar file
1054into the current shell.
1055.El
1056.It Ic run_rc_scripts Oo options Oc file ...
1057Call
1058.Ic run_rc_script
1059for each
1060.Ar file ,
1061unless it is already recorded as having been run.
1062.Pp
1063The
1064.Ar options
1065are:
1066.Bl -tag -width "--break break"
1067.It Ic --arg Ar arg
1068Pass
1069.Ar arg
1070to
1071.Ic run_rc_script ,
1072default is
1073.Ar _boot
1074set by
1075.Xr rc 8 .
1076.It Ic --break Ar break
1077Stop processing if any
1078.Ar file
1079matches any
1080.Ar break
1081.El
1082.It Ic safe_dot Ar file ...
1083Used by
1084.Ic sdot
1085when
1086.Xr mac_veriexec 4
1087is active and
1088.Ar file
1089is not verified.
1090.Pp
1091This function limits the input from
1092.Ar file
1093to simple variable assignments with any
1094non-alphanumeric characters replaced with
1095.Ql _ .
1096.It Ic sdot Ar file ...
1097For reading in configuration files.
1098Skip files that do not exist or are empty.
1099Try using
1100.Ic vdot
1101and if that fails (the file is unverified)
1102fall back to using
1103.Ic safe_dot .
1104.It Ic startmsg Oo Fl n Oc Ar message
1105Display a start message to
1106.Va stdout .
1107It should be used instead of
1108.Xr echo 1 .
1109The display of this output can be turned off if the
1110.Xr rc.conf 5
1111variable
1112.Va rc_startmsgs
1113is set to
1114.Dq Li NO .
1115.It Ic stop_boot Op Ar always
1116Prevent booting to multiuser mode.
1117If the
1118.Va autoboot
1119variable is set to
1120.Ql yes
1121(see
1122.Xr rc 8
1123to learn more about
1124.Va autoboot ) ,
1125or
1126.Ic checkyesno Ar always
1127indicates a truth value, then a
1128.Dv SIGTERM
1129signal is sent to the parent
1130process, which is assumed to be
1131.Xr rc 8 .
1132Otherwise, the shell exits with a non-zero status.
1133.It Ic vdot Ar file ...
1134For reading in only verified files.
1135.Pp
1136Ensure shell
1137.Li verify
1138option is on.
1139This option is only meaningful when
1140.Xr mac_veriexec 4
1141is active,
1142otherwise this function is effectively the same as
1143.Ic dot .
1144.Pp
1145Read in each
1146.Ar file
1147if it exists and
1148.Ic is_verfied Ar file
1149is successful, otherwise set return code to 80 (EAUTH).
1150.Pp
1151Restore previous state of the
1152.Li verify
1153option.
1154.It Ic wait_for_pids Op Ar pid ...
1155Wait until all of the provided
1156.Ar pids
1157do not exist any more, printing the list of outstanding
1158.Ar pids
1159every two seconds.
1160.It Ic warn Ar message
1161Display a warning message to
1162.Va stderr
1163and log it to the system log
1164using
1165.Xr logger 1 .
1166The warning message consists of the script name
1167(from
1168.Va $0 ) ,
1169followed by
1170.Dq Li ": WARNING: " ,
1171and then
1172.Ar message .
1173.El
1174.Sh FILES
1175.Bl -tag -width "/etc/rc.subr" -compact
1176.It Pa /etc/rc.subr
1177The
1178.Nm
1179file resides in
1180.Pa /etc .
1181.El
1182.Sh SEE ALSO
1183.Xr rc.conf 5 ,
1184.Xr debug.sh 8 ,
1185.Xr rc 8
1186.Sh HISTORY
1187The
1188.Nm
1189script
1190appeared in
1191.Nx 1.3 .
1192The
1193.Xr rc.d 8
1194support functions appeared in
1195.Nx 1.5 .
1196The
1197.Nm
1198script
1199first appeared in
1200.Fx 5.0 .
1201