xref: /dflybsd-src/share/man/man8/rc.subr.8 (revision c4be1ed54eff4542e7a8605007f73b381092df37)
1.\" 	$NetBSD: rc.subr.8,v 1.9 2002/07/08 16:14:55 atatat Exp $
2.\"	$FreeBSD: src/share/man/man8/rc.subr.8,v 1.3 2003/04/22 05:13:55 dougb Exp $
3.\" Copyright (c) 2002 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.Dd August 6, 2014
38.Dt RC.SUBR 8
39.Os
40.Sh NAME
41.Nm rc.subr
42.Nd functions used by system shell scripts
43.Sh SYNOPSIS
44.Bl -item
45.It
46.Li . /etc/rc.subr
47.It
48.Ic backup_file Ar action Ar file Ar current Ar backup
49.It
50.Ic checkyesno Ar var
51.It
52.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
53.It
54.Ic check_process Ar procname Op Ar interpreter
55.It
56.Ic debug Ar message
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 load_rc_config Ar command
65.It
66.Ic mount_critical_filesystems Ar type
67.It
68.Ic rc_usage Ar command Op Ar ...
69.It
70.Ic reverse_list Ar item Op Ar ...
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 Op Ar ...
79.It
80.Ic warn Ar message
81.El
82.Sh DESCRIPTION
83.Nm
84contains commonly used shell script functions and variable
85definitions which are used by various scripts such as
86.Xr rc 8 .
87.Pp
88The
89.Nm
90functions were mostly imported from
91.Nx
92and it is intended that they remain synced between the
93two projects. With that in mind there are several variable
94definitions that can help in this regard. They are:
95.Bl -tag -width 4n
96.It Ic OSTYPE
97Its value will be either
98.Dx Ns ,
99.Fx Ns ,
100or
101.Nx ,
102depending on which OS it is running on.
103.It Ic SYSCTL
104The path to the
105.Xr sysctl 8
106command.
107.It Ic SYSCTL_N
108The path and argument list to display only the
109.Xr sysctl 8
110values instead of a name=value pair.
111.It Ic SYSCTL_W
112The path and argument to write or modify
113.Xr sysctl 8
114values.
115.El
116.Pp
117The
118.Nm
119functions are accessed by sourcing
120.Pa /etc/rc.subr
121into the current shell.
122.Pp
123The following shell functions are available:
124.Bl -tag -width 4n
125.It Xo
126.Ic backup_file Ar action Ar file Ar current Ar backup
127.Xc
128Make a backup copy of
129.Ar file
130into
131.Ar current .
132If the
133.Xr rc.conf 5
134variable
135.Sy backup_uses_rcs
136is
137.Sq YES ,
138use
139.Xr rcs 1
140to archive the previous version of
141.Ar current ,
142otherwise save the previous version of
143.Ar current
144as
145.Ar backup .
146.Pp
147.Ar action
148may be one of the following:
149.Bl -tag -width remove
150.It Sy add
151.Ar file
152is now being backed up by or possibly re-entered into this backup mechanism.
153.Ar current
154is created, and if necessary, the
155.Xr rcs 1
156files are created as well.
157.It Sy update
158.Ar file
159has changed and needs to be backed up.
160If
161.Ar current
162exists, it is copied to
163.Ar backup
164or checked into
165.Xr rcs 1
166(if the repository file is old),
167and then
168.Ar file
169is copied to
170.Ar current .
171.It Sy remove
172.Ar file
173is no longer being tracked by this backup mechanism.
174If
175.Xr rcs 1
176is being used, an empty file is checked in and
177.Ar current
178is removed,
179otherwise
180.Ar current
181is moved to
182.Ar backup .
183.El
184.It Ic checkyesno Ar var
185Return 0 if
186.Ar var
187is defined to
188.Sq YES ,
189.Sq TRUE ,
190.Sq ON ,
191or
192.Sq 1 .
193Return 1 if
194.Ar var
195is defined to
196.Sq NO ,
197.Sq FALSE ,
198.Sq OFF ,
199or
200.Sq 0 .
201Otherwise, warn that
202.Ar var
203is not set correctly.
204The values are case insensitive.
205.It Xo
206.Ic check_pidfile
207.Ar pidfile
208.Ar procname
209.Op Ar interpreter
210.Xc
211Parses the first word of the first line of
212.Ar pidfile
213for a PID, and ensures that the process with that PID
214is running and its first argument matches
215.Ar procname .
216Prints the matching PID if successful, otherwise nothing.
217If
218.Ar interpreter
219is provided, parse the first line of
220.Ar procname ,
221ensure that the line is of the form
222.Dl #! interpreter [...]
223and use
224.Ar interpreter
225with its optional arguments and
226.Ar procname
227appended as the process string to search for.
228.It Ic check_process Ar procname Op Ar interpreter
229Prints the PIDs of any processes that are running with a first
230argument that matches
231.Ar procname .
232.Ar interpreter
233is handled as per
234.Ic check_pidfile .
235.It Ic debug Ar message
236Display a debugging message to
237.Em stderr ,
238log it to the system log using
239.Xr logger 1 ,
240and
241return to the caller.
242The error message consists of the script name
243(from
244.Sy $0 ) ,
245followed by
246.Dq ": DEBUG: " ,
247and then
248.Ar message .
249This function is intended to be used by developers
250as an aid to debugging scripts. It can be turned on or off
251by the
252.Xr rc.conf 5
253variable
254.Va rc_debug .
255.It Ic err Ar exitval Ar message
256Display an error message to
257.Em stderr ,
258log it to the system log
259using
260.Xr logger 1 ,
261and
262.Cm exit
263with an exit value of
264.Ar exitval .
265The error message consists of the script name
266(from
267.Sy $0 ) ,
268followed by
269.Dq ": ERROR: " ,
270and then
271.Ar message .
272.It Ic force_depend name
273Output an advisory message and force the
274.Ar name
275service to start. The
276.Ar name
277argument is the
278.Xr basename 1 ,
279component of the path to the script, usually
280.Em /etc/rc.d/name .
281If the script fails for any reason it will output a warning
282and return with a return value of 1. If it was successful
283it will return 0.
284.It Ic info Ar message
285Display an informational message to
286.Em stdout ,
287and log it to the system log using
288.Xr logger 1 .
289The message consists of the script name
290(from
291.Sy $0 ) ,
292followed by
293.Dq ": INFO: " ,
294and then
295.Ar message .
296The display of this informational output can be
297turned on or off by the
298.Xr rc.conf 5
299variable
300.Va rc_info .
301.It Ic load_rc_config Ar command
302Source in the configuration files for
303.Ar command .
304First,
305.Pa /etc/rc.conf
306is sourced if it has not yet been read in.
307Then,
308.Pa /etc/rc.conf.d/ Ns Ar command
309is sourced if it is an existing file.
310The latter may also contain other variable assignments to override
311.Ic run_rc_command
312arguments defined by the calling script, to provide an easy
313mechanism for an administrator to override the behaviour of a given
314.Xr rc.d 8
315script without requiring the editing of that script.
316.It Ic mount_critical_filesystems Ar type
317Go through a list of critical file systems,
318as found in the
319.Xr rc.conf 5
320variable
321.Sy critical_filesystems_ Ns Ar type ,
322mounting each one that
323is not currently mounted.
324.It Ic rc_usage Ar command Op Ar ...
325Print a usage message for
326.Sy $0 ,
327with
328.Ar commands
329being the list of valid arguments
330prefixed by
331.Sm off
332.Dq Bq Li fast | force | one | quiet .
333.Sm on
334.It Ic reverse_list Ar item Op Ar ...
335Print the list of
336.Ar items
337in reverse order.
338.It Ic run_rc_command Ar argument
339Run the
340.Ar argument
341method for the current
342.Xr rc.d 8
343script, based on the settings of various shell variables.
344.Ic run_rc_command
345is extremely flexible, and allows fully functional
346.Xr rc.d 8
347scripts to be implemented in a small amount of shell code.
348.Pp
349.Ar argument
350is searched for in the list of supported commands, which may be one
351of:
352.Dl start stop restart rcvar
353as well as any word listed in the optional variable
354.Sy extra_commands .
355If
356.Sy pidfile
357or
358.Sy procname
359is set, also allow:
360.Dl status poll
361.Pp
362.Ar argument
363may have one of the following prefixes which alters its operation:
364.Bl -tag -width "Prefix" -offset indent -compact
365.It Sy Prefix
366.Sy Operation
367.It Li fast
368Skip the check for an existing running process,
369and sets
370.Sy rc_fast=YES .
371.It Li force
372Skip the checks for
373.Sy rcvar
374being set to yes,
375and sets
376.Sy rc_force=YES .
377This ignores
378.Ar argument Ns Sy _precmd
379returning non-zero, and ignores any of the
380.Sy required_*
381tests failing .
382.It Li one
383Skip the checks for
384.Sy rcvar
385being set to yes, but performs all the other prerequisite tests.
386.It Li quiet
387Inhibits some verbose diagnostics.
388Currently, this includes messages
389.Qq Starting ${name}
390.\"(as checked by
391.\".Ic check_startmsgs
392.\"inside
393.\".Nm )
394and errors about usage of services that are not enabled in
395.Xr rc.conf 5 .
396This prefix also sets
397.Va rc_quiet Ns = Ns Li YES .
398.Em Please, note:
399.Va rc_quiet
400is not intended to completely mask all debug and warning messages,
401but only certain small classes of them.
402.El
403.Pp
404.Ic run_rc_command
405uses the following shell variables to control its behaviour.
406Unless otherwise stated, these are optional.
407.Bl -tag -width procname -offset indent
408.It Sy name
409The name of this script.
410This is not optional.
411.It Sy rcvar
412The value of
413.Sy rcvar
414is checked with
415.Ic checkyesno
416to determine if this method should be run.
417.It Sy command
418Full path to the command.
419Not required if
420.Ar argument Ns Sy _cmd
421is defined for each supported keyword.
422.It Sy command_args
423Optional arguments and/or shell directives for
424.Sy command .
425.It Sy command_interpreter
426.Sy command
427is started with
428.Dl #! command_interpreter [...]
429which results in its
430.Xr ps 1
431command being
432.Dl command_interpreter [...] command
433so use that string to find the PID(s) of the running command
434rather than
435.Ql command .
436.It Sy extra_commands
437Extra commands/keywords/arguments supported.
438.It Sy pidfile
439Path to pid file.
440Used to determine the PID(s) of the running command.
441If
442.Sy pidfile
443is set, use
444.Dl check_pidfile $pidfile $procname
445to find the PID.
446Otherwise, if
447.Sy command
448is set, use
449.Dl check_process $procname
450to find the PID.
451.It Sy procname
452Process name to check for.
453Defaults to the value of
454.Sy command .
455.It Sy required_dirs
456Check for the existence of the listed directories
457before running the default start method.
458.It Sy required_files
459Check for the readability of the listed files
460before running the default start method.
461.It Sy required_vars
462Perform
463.Ic checkyesno
464on each of the list variables
465before running the default start method.
466.It Sy ${name}_chdir
467Directory to
468.Ic cd
469to before running
470.Sy command ,
471if
472.Sy ${name}_chroot
473is not provided.
474.It Sy ${name}_chroot
475Directory to
476.Xr chroot 8
477to before running
478.Sy command .
479Only supported after
480.Pa /usr
481is mounted.
482.It Sy ${name}_flags
483Arguments to call
484.Sy command
485with.
486This is usually set in
487.Xr rc.conf 5 ,
488and not in the
489.Xr rc.d 8
490script.
491The environment variable
492.Sq Ev flags
493can be used to override this.
494.It Sy ${name}_nice
495.Xr nice 1
496level to run
497.Sy command
498as.
499Only supported after
500.Pa /usr
501is mounted.
502.It Sy ${name}_user
503User to run
504.Sy command
505as, using
506.Xr chroot 8 .
507if
508.Sy ${name}_chroot
509is set, otherwise
510uses
511.Xr su 1 .
512Only supported after
513.Pa /usr
514is mounted.
515.It Sy ${name}_group
516Group to run the chrooted
517.Sy command
518as.
519.It Sy ${name}_groups
520Comma separated list of supplementary groups to run the chrooted
521.Sy command
522with.
523.It Ar argument Ns Sy _cmd
524Shell commands which override the default method for
525.Ar argument .
526.It Ar argument Ns Sy _precmd
527Shell commands to run just before running
528.Ar argument Ns Sy _cmd
529or the default method for
530.Ar argument .
531If this returns a non-zero exit code, the main method is not performed.
532If the default method is being executed, this check is performed after
533the
534.Sy required_*
535checks and process (non-)existence checks.
536.It Ar argument Ns Sy _postcmd
537Shell commands to run if running
538.Ar argument Ns Sy _cmd
539or the default method for
540.Ar argument
541returned a zero exit code.
542.It Sy sig_stop
543Signal to send the processes to stop in the default
544.Sy stop
545method.
546Defaults to
547.Dv SIGTERM .
548.It Sy sig_reload
549Signal to send the processes to reload in the default
550.Sy reload
551method.
552Defaults to
553.Dv SIGHUP .
554.El
555.Pp
556For a given method
557.Ar argument ,
558if
559.Ar argument Ns Sy _cmd
560is not defined, then a default method is provided by
561.Sy run_rc_command :
562.Bl -tag -width "argument" -offset indent
563.It Sy Argument
564.Sy Default method
565.It Sy start
566If
567.Sy command
568is not running and
569.Ic checkyesno Sy rcvar
570succeeds, start
571.Sy command .
572.It Sy stop
573Determine the PIDs of
574.Sy command
575with
576.Ic check_pidfile
577or
578.Ic check_process
579(as appropriate),
580.Ic kill Sy sig_stop
581those PIDs, and run
582.Ic wait_for_pids
583on those PIDs.
584.It Sy reload
585Similar to
586.Sy stop ,
587except that it uses
588.Sy sig_reload
589instead, and doesn't run
590.Ic wait_for_pids .
591.It Sy restart
592Runs the
593.Sy stop
594method, then the
595.Sy start
596method.
597.It Sy status
598Show the PID of
599.Sy command ,
600or some other script specific status operation.
601.It Sy poll
602Wait for
603.Sy command
604to exit.
605.It Sy rcvar
606Display which
607.Xr rc.conf 5
608variable is used (if any).
609This method always works, even if the appropriate
610.Xr rc.conf 5
611variable is set to
612.Sq NO .
613.El
614.Pp
615The following variables are available to the methods
616(such as
617.Ar argument Ns Sy _cmd )
618as well as after
619.Ic run_rc_command
620has completed:
621.Bl -tag -width "rc_flags" -offset indent
622.It Sy rc_arg
623Argument provided to
624.Sy run_rc_command ,
625after fast and force processing has been performed.
626.It Sy rc_flags
627Flags to start the default command with.
628Defaults to
629.Sy ${name}_flags ,
630unless overridden by the environment variable
631.Sq Ev flags .
632This variable may be changed by the
633.Ar argument Ns Sy _precmd
634method.
635.It Sy rc_pid
636PID of
637.Sy command
638(if appropriate).
639.It Sy rc_fast
640Not empty if
641.Dq fast
642prefix was used.
643.It Sy rc_force
644Not empty if
645.Dq force
646prefix was used.
647.El
648.It Ic run_rc_script Ar file Ar argument
649Start the script
650.Ar file
651with an argument of
652.Ar argument ,
653and handle the return value from the script.
654.Pp
655Various shell variables are unset before
656.Ar file
657is started:
658.Bd -ragged -offset indent
659.Sy name ,
660.Sy command ,
661.Sy command_args ,
662.Sy command_interpreter ,
663.Sy extra_commands ,
664.Sy pidfile ,
665.Sy rcvar ,
666.Sy required_dirs ,
667.Sy required_files ,
668.Sy required_vars ,
669.Ar argument Ns Sy _cmd ,
670.Ar argument Ns Sy _precmd .
671.Ar argument Ns Sy _postcmd .
672.Ed
673.Pp
674The startup behaviour of
675.Ar file
676depends upon the following checks:
677.Bl -enum
678.It
679If
680.Ar file
681ends in
682.Pa .sh ,
683it is sourced into the current shell.
684.It
685If
686.Ar file
687appears to be a backup or scratch file
688(e.g., with a suffix of
689.Sq ~ ,
690.Sq # ,
691.Sq .OLD ,
692or
693.Sq .orig ) ,
694ignore it.
695.It
696If
697.Ar file
698is not executable, ignore it.
699.It
700If the
701.Xr rc.conf 5
702variable
703.Sy rc_fast_and_loose
704is empty,
705source
706.Ar file
707in a sub shell,
708otherwise source
709.Ar file
710into the current shell.
711.El
712.It Ic set_rcvar Op Ar base
713Set the variable name required to start a service. In
714.Dx
715a daemon is usually controlled by an
716.Xr rc.conf 5
717variable consisting of a daemon's name optionally postfixed by the string
718.Sy "_enable" .
719When the following line is included in a script
720.Pp
721.Dl rcvar=`set_rcvar`
722.Pp
723This function will use the value of the
724.Sy $name
725variable, which should be defined by the calling script, to construct the appropriate
726.Xr rc.conf 5
727knob. If the
728.Ar base
729argument is set it will use
730.Ar base
731instead of
732.Sy $name .
733.It Ic wait_for_pids Op Ar pid Op Ar ...
734Wait until all of the provided
735.Ar pids
736don't exist any more, printing the list of outstanding
737.Ar pids
738every two seconds.
739.It Ic warn Ar message
740Display a warning message to
741.Em stderr
742and log it to the system log
743using
744.Xr logger 1 .
745The warning message consists of the script name
746(from
747.Sy $0 ) ,
748followed by
749.Dq ": WARNING: " ,
750and then
751.Ar message .
752.El
753.Sh FILES
754.Bl -tag -width /etc/rc.subr -compact
755.It Pa /etc/rc.subr
756The
757.Nm
758file resides in
759.Pa /etc .
760.El
761.Sh SEE ALSO
762.Xr rc.conf 5 ,
763.Xr rc 8
764.Sh HISTORY
765.Nm
766appeared in
767.Nx 1.3 .
768The
769.Xr rc.d 8
770support functions appeared in
771.Nx 1.5 .
772.Nm
773first appeared in
774.Fx 5.0 .
775