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