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