xref: /netbsd-src/share/man/man8/rc.subr.8 (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1.\" 	$NetBSD: rc.subr.8,v 1.29 2011/08/11 22:52:46 apb Exp $
2.\"
3.\" Copyright (c) 2002-2011 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 August 11, 2011
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
38.It
39.Li . /etc/rc.subr
40.It
41.Ic backup_file Ar action Ar file Ar current Ar backup
42.It
43.Ic checkyesno Ar var
44.It
45.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
46.It
47.Ic check_process Ar procname Op Ar interpreter
48.It
49.Ic collapse_backslash_newline
50.It
51.Ic err Ar exitval Ar message
52.It
53.Ic load_rc_config Ar command
54.It
55.Ic load_rc_config_var Ar command Ar var
56.It
57.Ic mount_critical_filesystems Ar type
58.It
59.Ic no_rc_postprocess Ar command Op Ar arguments
60.It
61.Ic print_rc_metadata Ar string
62.It
63.Ic print_rc_normal Oo Fl n Oc Ar string
64.It
65.Ic rc_usage Ar command Op Ar ...
66.It
67.Ic reverse_list Ar item Op Ar ...
68.It
69.Ic run_rc_command Ar argument [parameters]
70.It
71.Ic run_rc_script Ar file Ar argument
72.It
73.Ic stop_boot
74.It
75.Ic twiddle
76.It
77.Ic wait_for_pids Op Ar pid Op Ar ...
78.It
79.Ic warn Ar message
80.It
81.Ic yesno_to_truefalse Ar var
82.El
83.Sh DESCRIPTION
84.Nm
85contains commonly used shell script functions which are used by
86various scripts such as
87.Xr rc 8 ,
88and the periodic system services which are controlled by
89.Xr daily.conf 5 ,
90.Xr monthly.conf 5 ,
91.Xr security.conf 5 ,
92and
93.Xr weekly.conf 5 .
94.Pp
95The
96.Nm
97functions 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 Ar file Ar current Ar backup
104Make a backup copy of
105.Ar file
106into
107.Ar current .
108If the
109.Xr rc.conf 5
110variable
111.Sy backup_uses_rcs
112is
113.Sq YES ,
114use
115.Xr rcs 1
116to archive the previous version of
117.Ar current ,
118otherwise save the previous version of
119.Ar current
120as
121.Ar backup .
122.Pp
123.Ar action
124may be one of the following:
125.Bl -tag -width remove
126.It Sy add
127.Ar file
128is now being backed up by or possibly re-entered into this backup mechanism.
129.Ar current
130is created, and if necessary, the
131.Xr rcs 1
132files are created as well.
133.It Sy update
134.Ar file
135has changed and needs to be backed up.
136If
137.Ar current
138exists, it is copied to
139.Ar backup
140or checked into
141.Xr rcs 1
142(if the repository file is old),
143and then
144.Ar file
145is copied to
146.Ar current .
147.It Sy remove
148.Ar file
149is no longer being tracked by this backup mechanism.
150If
151.Xr rcs 1
152is being used, an empty file is checked in and
153.Ar current
154is removed,
155otherwise
156.Ar current
157is moved to
158.Ar backup .
159.El
160.It Ic checkyesno Ar var
161Return 0 if
162.Ar var
163is defined to
164.Sq YES ,
165.Sq TRUE ,
166.Sq ON ,
167or
168.Sq 1 .
169Return 1 if
170.Ar var
171is defined to
172.Sq NO ,
173.Sq FALSE ,
174.Sq OFF ,
175or
176.Sq 0 .
177Otherwise, warn that
178.Ar var
179is not set correctly.
180The values are case insensitive.
181.Pp
182Note that the warning message shown by this function when
183.Ar var
184is not set references a manual page where the user can find more information.
185Its name is picked up from the
186.Sy rcvar_manpage
187variable.
188.It Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
189Parses the first word of the first line of
190.Ar pidfile
191for a PID, and ensures that the process with that PID
192is running and its first argument matches
193.Ar procname .
194Prints the matching PID if successful, otherwise nothing.
195If
196.Ar interpreter
197is provided, parse the first line of
198.Ar procname ,
199ensure that the line is of the form
200.Dl #! interpreter [...]
201and use
202.Ar interpreter
203with its optional arguments and
204.Ar procname
205appended as the process string to search for.
206.It Ic check_process Ar procname Op Ar interpreter
207Prints the PIDs of any processes that are running with a first
208argument that matches
209.Ar procname .
210.Ar interpreter
211is handled as per
212.Ic check_pidfile .
213.It Ic collapse_backslash_newline
214Copy input to output, collapsing
215.Ao backslash Ac Ns Ao newline Ac
216to nothing, but leaving other backslashes alone.
217.It Ic err Ar exitval Ar message
218Display an error message to
219.Em stderr ,
220log it to the system log
221using
222.Xr logger 1 ,
223and
224.Cm exit
225with an exit value of
226.Ar exitval .
227The error message consists of the script name
228(from
229.Sy $0 ) ,
230followed by
231.Dq ": ERROR: " ,
232and then
233.Ar message .
234.It Ic load_rc_config Ar command
235Source in the
236.Xr rc.conf 5
237configuration files for
238.Ar command .
239First,
240.Pa /etc/rc.conf
241is sourced if it has not yet been read in.
242Then,
243.Pa /etc/rc.conf.d/ Ns Ar command
244is sourced if it is an existing file.
245The latter may also contain other variable assignments to override
246.Ic run_rc_command
247arguments defined by the calling script, to provide an easy
248mechanism for an administrator to override the behaviour of a given
249.Xr rc.d 8
250script without requiring the editing of that script.
251.It Ic load_rc_config_var Ar command Ar var
252Read the
253.Xr rc.conf 5
254variable
255.Ar var
256for
257.Ar command
258and set in the current shell, using
259.Ic load_rc_config
260in a sub-shell to prevent unwanted side effects from other variable
261assignments.
262.It Ic mount_critical_filesystems Ar type
263Go through a list of critical file systems,
264as found in the
265.Xr rc.conf 5
266variable
267.Sy critical_filesystems_ Ns Ar type ,
268mounting each one that
269is not currently mounted.
270.It Ic no_rc_postprocess Ar command Op Ar arguments
271Execute the specified command with the specified arguments,
272in such a way that its output bypasses the post-processor that
273.Xr rc 8
274uses for most commands.
275This implies that the output will not appear in the
276.Pa /var/run/rc.log
277file, and will appear on the console regardless of the
278value of
279.Va rc_silent .
280This is expected to be useful for interactive commands,
281and this mechanism is automatically used by
282.Ic run_rc_command
283when a script contains the
284.Xr rcorder 8
285keyword
286.Dq interactive .
287.Pp
288If invoked from a context that does not appear to be under the control of
289.Xr rc 8 ,
290then the command is executed without special treatment.
291.It Ic print_rc_metadata Ar string
292Print the specified
293.Ar string
294in such a way that it should be handled as meta-data by the
295.Xr rc 8
296post-processor.
297If invoked from a context that does not appear to be under the control of
298.Xr rc 8 ,
299then the
300.Ar string
301is discarded.
302.Pp
303Any
304.Xr rc.d 8
305script may invoke this function with an argument that begins with
306.Dq note: ,
307followed by one line of arbitrary text;
308the text will be logged by
309.Xr rc 8
310but will not be displayed on the console.
311.Pp
312The use of arguments that do not begin with
313.Dq note:
314is reserved for internal use by
315.Xr rc 8
316and
317.Nm .
318.It Ic print_rc_normal Oo Fl n Oc Ar string
319Print the specified
320.Ar string
321in such a way that it should be handled as normal output by the
322.Xr rc 8
323post-processor.
324If invoked from a context that does not appear to be under the control of
325.Xr rc 8 ,
326then the
327.Ar string
328is printed to standard output.
329.Pp
330If the
331.Fl n
332flag is specified, then the string is printed without a newline.
333.Pp
334Intended use cases include:
335.Bl -bullet
336.It
337An rc.d script can use
338.Dq Sy print_rc_normal Fl n
339to print a partial line in such a way that it appears
340immediately instead of being buffered by
341.Xr rc 8 Ap s
342post-processor.
343.It
344An rc.d script that is run via the
345.Sy no_rc_postprocess
346function (so most of its output is invisible to
347.Xr rc 8 Ap s
348post-processor) can use
349.Sy print_rc_normal
350to force some of its output to be seen by the post-processor.
351.El
352.It Ic rc_usage Ar command Op Ar ...
353Print a usage message for
354.Sy $0 ,
355with
356.Ar commands
357being the list of valid arguments
358prefixed by
359.Dq "[fast|force|one]" .
360.It Ic reverse_list Ar item Op Ar ...
361Print the list of
362.Ar items
363in reverse order.
364.It Ic run_rc_command Ar argument Op Ar parameter ...
365Run the
366.Ar argument
367method for the current
368.Xr rc.d 8
369script, based on the settings of various shell variables.
370.Ic run_rc_command
371is extremely flexible, and allows fully functional
372.Xr rc.d 8
373scripts to be implemented in a small amount of shell code.
374The optional set of parameters is passed verbatim to the command, but not to its
375pre/post hooks.
376.Pp
377.Ar argument
378is searched for in the list of supported commands, which may be one
379of:
380.Bl -tag -width restart -offset indent
381.It Sy start
382Start the service.
383This should check that the service is to be started as specified by
384.Xr rc.conf 5 .
385Also checks if the service is already running and refuses to start if
386it is.
387This latter check is not performed by standard
388.Nx
389scripts if the system is starting directly to multi-user mode, to
390speed up the boot process.
391.It Sy stop
392If the service is to be started as specified by
393.Xr rc.conf 5 ,
394stop the service.
395This should check that the service is running and complain if it's not.
396.It Sy restart
397Perform a
398.Sy stop
399then a
400.Sy start .
401Defaults to displaying the process ID of the program (if running).
402.It Sy rcvar
403Display which
404.Xr rc.conf 5
405variables are used to control the startup of the service (if any).
406.El
407.Pp
408If
409.Sy pidfile
410or
411.Sy procname
412is set, also support:
413.Bl -tag -width restart -offset indent
414.It Sy poll
415Wait for the command to exit.
416.It Sy status
417Show the status of the process.
418.El
419.Pp
420Other supported commands are listed in the optional variable
421.Sy extra_commands .
422.Pp
423.Ar argument
424may have one of the following prefixes which alters its operation:
425.Bl -tag -width "Prefix" -offset indent
426.It Sy fast
427Skip the check for an existing running process,
428and sets
429.Sy rc_fast=YES .
430.It Sy force
431Skip the checks for
432.Sy rcvar
433being set to yes,
434and sets
435.Sy rc_force=YES .
436This ignores
437.Ar argument Ns Sy _precmd
438returning non-zero, and ignores any of the
439.Sy required_*
440tests failing, and always returns a zero exit status.
441.It Sy one
442Skip the checks for
443.Sy rcvar
444being set to yes, but performs all the other prerequisite tests.
445.El
446.Pp
447.Ic run_rc_command
448uses the following shell variables to control its behaviour.
449Unless otherwise stated, these are optional.
450.Bl -tag -width procname -offset indent
451.It Sy name
452The name of this script.
453This is not optional.
454.It Sy rcvar
455The value of
456.Sy rcvar
457is checked with
458.Ic checkyesno
459to determine if this method should be run.
460.It Sy rcvar_manpage
461The manual page containing information about
462.Sy rcvar .
463It will be part of the warning message shown when
464.Sy rcvar
465is undefined.
466Defaults to
467.Xr rc.conf 5 .
468.It Sy command
469Full path to the command.
470Not required if
471.Ar argument Ns Sy _cmd
472is defined for each supported keyword.
473.It Sy command_args
474Optional arguments and/or shell directives for
475.Sy command .
476.It Sy command_interpreter
477.Sy command
478is started with
479.Dl #! command_interpreter [...]
480which results in its
481.Xr ps 1
482command being
483.Dl command_interpreter [...] command
484so use that string to find the PID(s) of the running command
485rather than
486.Ql command .
487.It Sy extra_commands
488Extra commands/keywords/arguments supported.
489.It Sy pidfile
490Path to pid file.
491Used to determine the PID(s) of the running command.
492If
493.Sy pidfile
494is set, use
495.Dl check_pidfile $pidfile $procname
496to find the PID.
497Otherwise, if
498.Sy command
499is set, use
500.Dl check_process $procname
501to find the PID.
502.It Sy procname
503Process name to check for.
504Defaults to the value of
505.Sy command .
506.It Sy required_dirs
507Check for the existence of the listed directories
508before running the default start method.
509.It Sy required_files
510Check for the readability of the listed files
511before running the default start method.
512.It Sy required_vars
513Perform
514.Ic checkyesno
515on each of the list variables
516before running the default start method.
517.It Sy ${name}_chdir
518Directory to
519.Ic cd
520to before running
521.Sy command ,
522if
523.Sy ${name}_chroot
524is not provided.
525.It Sy ${name}_chroot
526Directory to
527.Xr chroot 8
528to before running
529.Sy command .
530Only supported after
531.Pa /usr
532is mounted.
533.It Sy ${name}_env
534List of additional or modified environment variables to set
535when starting
536.Sy command .
537.It Sy ${name}_flags
538Arguments to call
539.Sy command
540with.
541This is usually set in
542.Xr rc.conf 5 ,
543and not in the
544.Xr rc.d 8
545script.
546The environment variable
547.Sq Ev flags
548can be used to override this.
549.It Sy ${name}_nice
550.Xr nice 1
551level to run
552.Sy command
553as.
554Only supported after
555.Pa /usr
556is mounted.
557.It Sy ${name}_user
558User to run
559.Sy command
560as, using
561.Xr chroot 8 .
562if
563.Sy ${name}_chroot
564is set, otherwise
565uses
566.Xr su 1 .
567Only supported after
568.Pa /usr
569is mounted.
570.It Sy ${name}_group
571Group to run the chrooted
572.Sy command
573as.
574.It Sy ${name}_groups
575Comma separated list of supplementary groups to run the chrooted
576.Sy command
577with.
578.It Ar argument Ns Sy _cmd
579Shell commands which override the default method for
580.Ar argument .
581.It Ar argument Ns Sy _precmd
582Shell commands to run just before running
583.Ar argument Ns Sy _cmd
584or the default method for
585.Ar argument .
586If this returns a non-zero exit code, the main method is not performed.
587If the default method is being executed, this check is performed after
588the
589.Sy required_*
590checks and process (non-)existence checks.
591.It Ar argument Ns Sy _postcmd
592Shell commands to run if running
593.Ar argument Ns Sy _cmd
594or the default method for
595.Ar argument
596returned a zero exit code.
597.It Sy sig_stop
598Signal to send the processes to stop in the default
599.Sy stop
600method.
601Defaults to
602.Dv SIGTERM .
603.It Sy sig_reload
604Signal to send the processes to reload in the default
605.Sy reload
606method.
607Defaults to
608.Dv SIGHUP .
609.El
610.Pp
611For a given method
612.Ar argument ,
613if
614.Ar argument Ns Sy _cmd
615is not defined, then a default method is provided by
616.Sy run_rc_command :
617.Bl -tag -width "argument" -offset indent
618.It Sy Argument
619.Sy Default method
620.It Sy start
621If
622.Sy command
623is not running and
624.Ic checkyesno Sy rcvar
625succeeds, start
626.Sy command .
627.It Sy stop
628Determine the PIDs of
629.Sy command
630with
631.Ic check_pidfile
632or
633.Ic check_process
634(as appropriate),
635.Ic kill Sy sig_stop
636those PIDs, and run
637.Ic wait_for_pids
638on those PIDs.
639.It Sy reload
640Similar to
641.Sy stop ,
642except that it uses
643.Sy sig_reload
644instead, and doesn't run
645.Ic wait_for_pids .
646.It Sy restart
647Runs the
648.Sy stop
649method, then the
650.Sy start
651method.
652.It Sy status
653Show the PID of
654.Sy command ,
655or some other script specific status operation.
656.It Sy poll
657Wait for
658.Sy command
659to exit.
660.It Sy rcvar
661Display which
662.Xr rc.conf 5
663variable is used (if any).
664This method always works, even if the appropriate
665.Xr rc.conf 5
666variable is set to
667.Sq NO .
668.El
669.Pp
670The following variables are available to the methods
671(such as
672.Ar argument Ns Sy _cmd )
673as well as after
674.Ic run_rc_command
675has completed:
676.Bl -tag -width "rc_flags" -offset indent
677.It Sy rc_arg
678Argument provided to
679.Sy run_rc_command ,
680after fast and force processing has been performed.
681.It Sy rc_flags
682Flags to start the default command with.
683Defaults to
684.Sy ${name}_flags ,
685unless overridden by the environment variable
686.Sq Ev flags .
687This variable may be changed by the
688.Ar argument Ns Sy _precmd
689method.
690.It Sy rc_pid
691PID of
692.Sy command
693(if appropriate).
694.It Sy rc_fast
695Not empty if
696.Dq fast
697prefix was used.
698.It Sy rc_force
699Not empty if
700.Dq force
701prefix was used.
702.El
703.It Ic run_rc_script Ar file Ar argument
704Start the script
705.Ar file
706with an argument of
707.Ar argument ,
708and handle the return value from the script.
709.Pp
710Various shell variables are unset before
711.Ar file
712is started:
713.Bd -ragged -offset indent
714.Sy name ,
715.Sy command ,
716.Sy command_args ,
717.Sy command_interpreter ,
718.Sy extra_commands ,
719.Sy pidfile ,
720.Sy rcvar ,
721.Sy required_dirs ,
722.Sy required_files ,
723.Sy required_vars ,
724.Ar argument Ns Sy _cmd ,
725.Ar argument Ns Sy _precmd .
726.Ar argument Ns Sy _postcmd .
727.Ed
728.Pp
729The startup behaviour of
730.Ar file
731depends upon the following checks:
732.Bl -enum
733.It
734If
735.Ar file
736ends in
737.Pa .sh ,
738it is sourced into the current shell.
739.It
740If
741.Ar file
742appears to be a backup or scratch file
743(e.g., with a suffix of
744.Sq ~ ,
745.Sq # ,
746.Sq .OLD ,
747or
748.Sq .orig ) ,
749ignore it.
750.It
751If
752.Ar file
753is not executable, ignore it.
754.It
755If the
756.Xr rc.conf 5
757variable
758.Sy rc_fast_and_loose
759is empty,
760source
761.Ar file
762in a sub shell,
763otherwise source
764.Ar file
765into the current shell.
766.It
767If
768.Ar file
769contains the
770.Xr rcorder 8
771keyword
772.Dq interactive ,
773then the command is executed using
774.Ic no_rc_postprocess .
775.El
776.It Ic stop_boot
777Prevent booting to multiuser mode.
778If the
779.Sy autoboot
780variable is
781.Sq yes ,
782then a
783.Sy SIGTERM
784signal is sent to the parent
785process (which is assumed to be
786.Xr rc 8 ) .
787Otherwise, the shell exits with status
788.Li 1 .
789.It Ic twiddle
790Display one of the characters
791.Sq \&/ , \&- , \&\e , \&| ,
792followed by a backspace.
793Repeated calls to this function will create the appearance of a spinning
794symbol, as a different character is displayed on each call.
795Output is to
796.Pa /dev/tty ,
797so this function may be useful even inside a script whose output
798has been redirected.
799.It Ic wait_for_pids Op Ar pid Op Ar ...
800Wait until all of the provided
801.Ar pids
802don't exist any more, printing the list of outstanding
803.Ar pids
804every two seconds.
805.It Ic warn Ar message
806Display a warning message to
807.Em stderr
808and log it to the system log
809using
810.Xr logger 1 .
811The warning message consists of the script name
812(from
813.Sy $0 ) ,
814followed by
815.Dq ": WARNING: " ,
816and then
817.Ar message .
818.It Ic yesno_to_truefalse Ar var
819Change the value of the specified variable from any of the
820forms acceptable to the
821.Ic checkyesno
822function, to
823.Dq true
824or
825.Dq false .
826.El
827.Sh FILES
828.Bl -tag -width /etc/rc.subr -compact
829.It Pa /etc/rc.subr
830The
831.Nm
832file resides in
833.Pa /etc .
834.El
835.Sh SEE ALSO
836.Xr rc.conf 5 ,
837.Xr rc 8
838.Sh HISTORY
839.Nm
840appeared in
841.Nx 1.3 .
842The
843.Xr rc.d 8
844support functions appeared in
845.Nx 1.5 .
846Support for the
847.Xr rc 8
848post-processor appeared in
849.Nx 6.0 .
850