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