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: stable/9/share/man/man8/rc.subr.8 231676 2012-02-14 12:43:33Z des $
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 | quiet .
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.It Li quiet
431Inhibits some verbose diagnostics.
432Currently, this includes messages
433.Qq Starting ${name}
434(as checked by
435.Ic check_startmsgs
436inside
437.Nm )
438and errors about usage of services that are not enabled in
439.Xr rc.conf 5 .
440This prefix also sets
441.Va rc_quiet Ns = Ns Li YES .
442.Em Please, note:
443.Va rc_quiet
444is not intended to completely mask all debug and warning messages,
445but only certain small classes of them.
446.El
447.Pp
448.Ic run_rc_command
449uses the following shell variables to control its behaviour.
450Unless otherwise stated, these are optional.
451.Bl -tag -width ".Va procname" -offset indent
452.It Va name
453The name of this script.
454This is not optional.
455.It Va rcvar
456The value of
457.Va rcvar
458is checked with
459.Ic checkyesno
460to determine if this method should be run.
461.It Va command
462Full path to the command.
463Not required if
464.Ar argument Ns Va _cmd
465is defined for each supported keyword.
466Can be overridden by
467.Va ${name}_program .
468.It Va command_args
469Optional arguments and/or shell directives for
470.Va command .
471.It Va command_interpreter
472.Va command
473is started with:
474.Pp
475.Dl "#! command_interpreter [...]"
476.Pp
477which results in its
478.Xr ps 1
479command being:
480.Pp
481.Dl "command_interpreter [...] command"
482.Pp
483so use that string to find the PID(s) of the running command
484rather than
485.Va command .
486.It Va extra_commands
487Extra commands/keywords/arguments supported.
488.It Va pidfile
489Path to PID file.
490Used to determine the PID(s) of the running command.
491If
492.Va pidfile
493is set, use:
494.Pp
495.Dl "check_pidfile $pidfile $procname"
496.Pp
497to find the PID.
498Otherwise, if
499.Va command
500is set, use:
501.Pp
502.Dl "check_process $procname"
503.Pp
504to find the PID.
505.It Va procname
506Process name to check for.
507Defaults to the value of
508.Va command .
509.It Va required_dirs
510Check for the existence of the listed directories
511before running the
512.Cm start
513method.
514.It Va required_files
515Check for the readability of the listed files
516before running the
517.Cm start
518method.
519.It Va required_modules
520Ensure that the listed kernel modules are loaded
521before running the
522.Cm start
523method.
524This is done after invoking the commands from
525.Va start_precmd
526so that the missing modules are not loaded in vain
527if the preliminary commands indicate a error condition.
528A word in the list can have an optional
529.Dq Li : Ns Ar modname
530or
531.Dq Li ~ Ns Ar pattern
532suffix.
533The
534.Ar modname
535or
536.Ar pattern
537parameter is passed to
538.Ic load_kld
539through a
540.Fl m
541or
542.Fl e
543option, respectively.
544See the description of
545.Ic load_kld
546in this document for details.
547.It Va required_vars
548Perform
549.Ic checkyesno
550on each of the list variables
551before running the
552.Cm start
553method.
554.It Va ${name}_chdir
555Directory to
556.Ic cd
557to before running
558.Va command ,
559if
560.Va ${name}_chroot
561is not provided.
562.It Va ${name}_chroot
563Directory to
564.Xr chroot 8
565to before running
566.Va command .
567Only supported after
568.Pa /usr
569is mounted.
570.It Va ${name}_flags
571Arguments to call
572.Va command
573with.
574This is usually set in
575.Xr rc.conf 5 ,
576and not in the
577.Xr rc.d 8
578script.
579The environment variable
580.Sq Ev flags
581can be used to override this.
582.It Va ${name}_nice
583.Xr nice 1
584level to run
585.Va command
586as.
587Only supported after
588.Pa /usr
589is mounted.
590.It Va ${name}_program
591Full path to the command.
592Overrides
593.Va command
594if both are set, but has no effect if
595.Va command
596is unset.
597As a rule,
598.Va command
599should be set in the script while
600.Va ${name}_program
601should be set in
602.Xr rc.conf 5 .
603.It Va ${name}_user
604User to run
605.Va command
606as, using
607.Xr chroot 8
608if
609.Va ${name}_chroot
610is set, otherwise
611uses
612.Xr su 1 .
613Only supported after
614.Pa /usr
615is mounted.
616.It Va ${name}_group
617Group to run the chrooted
618.Va command
619as.
620.It Va ${name}_groups
621Comma separated list of supplementary groups to run the chrooted
622.Va command
623with.
624.It Ar argument Ns Va _cmd
625Shell commands which override the default method for
626.Ar argument .
627.It Ar argument Ns Va _precmd
628Shell commands to run just before running
629.Ar argument Ns Va _cmd
630or the default method for
631.Ar argument .
632If this returns a non-zero exit code, the main method is not performed.
633If the default method is being executed, this check is performed after
634the
635.Va required_*
636checks and process (non-)existence checks.
637.It Ar argument Ns Va _postcmd
638Shell commands to run if running
639.Ar argument Ns Va _cmd
640or the default method for
641.Ar argument
642returned a zero exit code.
643.It Va sig_stop
644Signal to send the processes to stop in the default
645.Cm stop
646method.
647Defaults to
648.Dv SIGTERM .
649.It Va sig_reload
650Signal to send the processes to reload in the default
651.Cm reload
652method.
653Defaults to
654.Dv SIGHUP .
655.El
656.Pp
657For a given method
658.Ar argument ,
659if
660.Ar argument Ns Va _cmd
661is not defined, then a default method is provided by
662.Ic run_rc_command :
663.Bl -tag -width ".Sy Argument" -offset indent
664.It Sy Argument
665.Sy Default method
666.It Cm start
667If
668.Va command
669is not running and
670.Ic checkyesno Va rcvar
671succeeds, start
672.Va command .
673.It Cm stop
674Determine the PIDs of
675.Va command
676with
677.Ic check_pidfile
678or
679.Ic check_process
680(as appropriate),
681.Ic kill Va sig_stop
682those PIDs, and run
683.Ic wait_for_pids
684on those PIDs.
685.It Cm reload
686Similar to
687.Cm stop ,
688except that it uses
689.Va sig_reload
690instead, and does not run
691.Ic wait_for_pids .
692Another difference from
693.Cm stop
694is that
695.Cm reload
696is not provided by default.
697It can be enabled via
698.Va extra_commands
699if appropriate:
700.Pp
701.Dl "extra_commands=reload"
702.It Cm restart
703Runs the
704.Cm stop
705method, then the
706.Cm start
707method.
708.It Cm status
709Show the PID of
710.Va command ,
711or some other script specific status operation.
712.It Cm poll
713Wait for
714.Va command
715to exit.
716.It Cm rcvar
717Display which
718.Xr rc.conf 5
719variable is used (if any).
720This method always works, even if the appropriate
721.Xr rc.conf 5
722variable is set to
723.Dq Li NO .
724.El
725.Pp
726The following variables are available to the methods
727(such as
728.Ar argument Ns Va _cmd )
729as well as after
730.Ic run_rc_command
731has completed:
732.Bl -tag -width ".Va rc_flags" -offset indent
733.It Va rc_arg
734Argument provided to
735.Ic run_rc_command ,
736after fast and force processing has been performed.
737.It Va rc_flags
738Flags to start the default command with.
739Defaults to
740.Va ${name}_flags ,
741unless overridden by the environment variable
742.Sq Ev flags .
743This variable may be changed by the
744.Ar argument Ns Va _precmd
745method.
746.It Va rc_pid
747PID of
748.Va command
749(if appropriate).
750.It Va rc_fast
751Not empty if
752.Dq Li fast
753prefix was used.
754.It Va rc_force
755Not empty if
756.Dq Li force
757prefix was used.
758.El
759.It Ic run_rc_script Ar file argument
760Start the script
761.Ar file
762with an argument of
763.Ar argument ,
764and handle the return value from the script.
765.Pp
766Various shell variables are unset before
767.Ar file
768is started:
769.Bd -ragged -offset indent
770.Va name ,
771.Va command ,
772.Va command_args ,
773.Va command_interpreter ,
774.Va extra_commands ,
775.Va pidfile ,
776.Va rcvar ,
777.Va required_dirs ,
778.Va required_files ,
779.Va required_vars ,
780.Ar argument Ns Va _cmd ,
781.Ar argument Ns Va _precmd .
782.Ar argument Ns Va _postcmd .
783.Ed
784.Pp
785The startup behaviour of
786.Ar file
787depends upon the following checks:
788.Bl -enum
789.It
790If
791.Ar file
792ends in
793.Pa .sh ,
794it is sourced into the current shell.
795.It
796If
797.Ar file
798appears to be a backup or scratch file
799(e.g., with a suffix of
800.Pa ~ , # , .OLD ,
801or
802.Pa .orig ) ,
803ignore it.
804.It
805If
806.Ar file
807is not executable, ignore it.
808.It
809If the
810.Xr rc.conf 5
811variable
812.Va rc_fast_and_loose
813is empty,
814source
815.Ar file
816in a sub shell,
817otherwise source
818.Ar file
819into the current shell.
820.El
821.It Ic stop_boot Op Ar always
822Prevent booting to multiuser mode.
823If the
824.Va autoboot
825variable is set to
826.Ql yes ,
827or
828.Ic checkyesno Ar always
829indicates a truth value, then a
830.Dv SIGTERM
831signal is sent to the parent
832process, which is assumed to be
833.Xr rc 8 .
834Otherwise, the shell exits with a non-zero status.
835.It Ic set_rcvar Op Ar base
836Set the variable name required to start a service.
837In
838.Fx
839a daemon is usually controlled by an
840.Xr rc.conf 5
841variable consisting of a daemon's name postfixed by the string
842.Dq Li "_enable" .
843This is not the case in
844.Nx .
845When the following line is included in a script:
846.Pp
847.Dl "rcvar=`set_rcvar`"
848.Pp
849this function will use the value of the
850.Va $name
851variable, which should be defined by the calling script,
852to construct the appropriate
853.Xr rc.conf 5
854knob.
855If the
856.Ar base
857argument is set it will use
858.Ar base
859instead of
860.Va $name .
861.It Ic wait_for_pids Op Ar pid ...
862Wait until all of the provided
863.Ar pids
864do not exist any more, printing the list of outstanding
865.Ar pids
866every two seconds.
867.It Ic warn Ar message
868Display a warning message to
869.Va stderr
870and log it to the system log
871using
872.Xr logger 1 .
873The warning message consists of the script name
874(from
875.Va $0 ) ,
876followed by
877.Dq Li ": WARNING: " ,
878and then
879.Ar message .
880.El
881.Sh FILES
882.Bl -tag -width ".Pa /etc/rc.subr" -compact
883.It Pa /etc/rc.subr
884The
885.Nm
886file resides in
887.Pa /etc .
888.El
889.Sh SEE ALSO
890.Xr rc.conf 5 ,
891.Xr rc 8
892.Sh HISTORY
893The
894.Nm
895script
896appeared in
897.Nx 1.3 .
898The
899.Xr rc.d 8
900support functions appeared in
901.Nx 1.5 .
902The
903.Nm
904script
905first appeared in
906.Fx 5.0 .
907