Merge branch 'vendor/DHCPCD'
[dragonfly.git] / share / man / man8 / rc.8
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Portions of this manual page are Copyrighted by
5 .\"     The NetBSD Foundation.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\"    notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in the
14 .\"    documentation and/or other materials provided with the distribution.
15 .\" 3. Neither the name of the University nor the names of its contributors
16 .\"    may be used to endorse or promote products derived from this software
17 .\"    without specific prior written permission.
18 .\"
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 .\" SUCH DAMAGE.
30 .\"
31 .\"     @(#)rc.8        8.2 (Berkeley) 12/11/93
32 .\" $FreeBSD: src/share/man/man8/rc.8,v 1.22 2002/12/12 17:25:58 ru Exp $
33 .\"
34 .Dd July 20, 2018
35 .Dt RC 8
36 .Os
37 .Sh NAME
38 .Nm rc
39 .Nd command scripts for auto-reboot and daemon startup
40 .Sh SYNOPSIS
41 .Nm
42 .Nm rc.conf
43 .Nm rc.conf.local
44 .Nm rc.d/
45 .Nm rc.firewall
46 .Nm rc.local
47 .Nm rc.shutdown
48 .Nm rc.shutdown.local
49 .Nm rc.subr
50 .Sh DESCRIPTION
51 The
52 .Nm
53 utility is the command script which controls the automatic boot process
54 after being called by
55 .Xr init 8 .
56 The
57 .Nm rc.local
58 and
59 .Nm rc.shutdown.local
60 scripts contains commands which are pertinent only to a specific site.
61 Typically, scripts in
62 .Pa /usr/local/etc/rc.d/
63 is used instead of
64 .Nm rc.local
65 and
66 .Nm rc.shutdown.local
67 these days but if you want to use them it is still supported.
68 In this case, they should source
69 .Pa /etc/rc.conf
70 and contain additional custom startup and shutdown code for your system.
71 The best way to handle
72 .Nm rc.local
73 and
74 .Nm rc.shutdown.local ,
75 however, is to separate them out into
76 .Nm rc.d/
77 style scripts and place them under
78 .Pa /usr/local/etc/rc.d/ .
79 The
80 .Nm rc.conf
81 file contains the global system configuration information referenced
82 by the startup scripts, while
83 .Nm rc.conf.local
84 contains the local system configuration.
85 See
86 .Xr rc.conf 5
87 for more information.
88 .Pp
89 The
90 .Nm rc.d/
91 directories contain scripts which will be automatically
92 executed at boot time and shutdown time.
93 .Ss Operation of Nm
94 .Bl -enum
95 .It
96 Source
97 .Pa /etc/rc.subr
98 to load various
99 .Xr rc.subr 8
100 shell functions to use.
101 .It
102 If autobooting, set
103 .Va autoboot Ns = Ns Li yes
104 and enable a flag
105 .Pq Va rc_fast Ns = Ns Li yes ,
106 which prevents the
107 .Nm rc.d/
108 scripts from performing the check for already running processes
109 (thus speeding up the boot process).
110 This
111 .Va rc_fast Ns = Ns Li yes
112 speedup will not occur when
113 .Nm
114 is started up after exiting the single-user shell.
115 .It
116 If the file
117 .Pa /firstboot
118 does not exist, add
119 .Dq Li firstboot
120 to the list of KEYWORDS to skip in
121 .Xr rcorder 8 .
122 .It
123 Invoke
124 .Xr rcorder 8
125 to order the files in
126 .Pa /etc/rc.d/
127 that do not have a
128 .Dq Li nostart
129 keyword (refer to
130 .Xr rcorder 8 Ns 's
131 .Fl s
132 flag),
133 and assign the result to a variable.
134 .It
135 Call each script in turn using
136 .Fn run_rc_script
137 (from
138 .Xr rc.subr 8 ) ,
139 which sets
140 .Va $1
141 to
142 .Dq Li start ,
143 and sources the script in a subshell.
144 If the script has a
145 .Pa .sh
146 suffix then it is sourced directly into the current shell.
147 .It
148 If the file
149 .Pa /firstboot
150 exists, delete it.
151 .El
152 .Ss Operation of Nm rc.shutdown
153 .Bl -enum
154 .It
155 Source
156 .Pa /etc/rc.subr
157 to load various
158 .Xr rc.subr 8
159 shell functions to use.
160 .It
161 Invoke
162 .Xr rcorder 8
163 to order the files in
164 .Pa /etc/rc.d/
165 that have a
166 .Dq Li shutdown
167 keyword (refer to
168 .Xr rcorder 8 Ns 's
169 .Fl k
170 flag),
171 reverse that order, and assign the result to a variable.
172 .It
173 Call each script in turn using
174 .Fn run_rc_script
175 (from
176 .Xr rc.subr 8 ) ,
177 which sets
178 .Va $1
179 to
180 .Dq Li stop ,
181 and sources the script in a subshell.
182 If the script has a
183 .Pa .sh
184 suffix then it is sourced directly into the current shell.
185 .El
186 .Ss Contents of Nm rc.d/
187 .Nm rc.d/
188 is located in
189 .Pa /etc/rc.d/ .
190 The following file naming conventions are currently used in
191 .Nm rc.d/ :
192 .Bl -tag -width ".Pa ALLUPPERCASE" -offset indent
193 .It Pa ALLUPPERCASE
194 Scripts that are
195 .Dq placeholders
196 to ensure that certain operations are performed before others.
197 In order of startup, these are:
198 .Bl -tag -width ".Pa NETWORKING"
199 .It Pa NETWORKING
200 Ensure basic network services are running, including general
201 network configuration
202 .Pq Pa netif , routing , network_ipv6 , ppp .
203 .It Pa SERVERS
204 Ensure basic services (such as
205 .Pa NETWORKING
206 and
207 .Pa syslogd )
208 exist for services that start early (such as
209 .Pa named ) ,
210 because they are required by
211 .Pa DAEMON
212 below.
213 .It Pa DAEMON
214 Check-point before all general purpose daemons such as
215 .Pa dhcpd , ftpd
216 and
217 .Pa lpd .
218 .It Pa LOGIN
219 Check-point before user login services
220 .Pa ( inetd
221 and
222 .Pa sshd ) ,
223 as well as services which might run commands as users
224 .Pa ( cron , jail
225 and
226 .Pa sendmail ) .
227 .El
228 .It Pa foo.sh
229 Scripts that are to be sourced into the current shell rather than a subshell
230 have a
231 .Pa .sh
232 suffix.
233 Extreme care must be taken in using this, as the startup sequence will
234 terminate if the script terminates.
235 .It Pa bar
236 Scripts that are sourced in a subshell.
237 These can stop the boot if necessary with the following shell
238 commands:
239 .Bd -literal -offset indent
240 if [ "$autoboot" = yes ]; then
241         kill -TERM $$
242 fi
243 exit 1
244 .Ed
245 .Pp
246 Note that this should be used extremely sparingly!
247 .El
248 .Pp
249 Each script should contain
250 .Xr rcorder 8
251 keywords, especially an appropriate
252 .Dq Li PROVIDE
253 entry, and if necessary
254 .Dq Li REQUIRE
255 and
256 .Dq Li BEFORE
257 keywords.
258 .Pp
259 Each script is expected to support at least the following arguments, which
260 are automatically supported if it uses the
261 .Fn run_rc_command
262 function:
263 .Bl -tag -width ".Cm restart" -offset indent
264 .It Cm start
265 Start the service.
266 This should check that the service is to be started as specified by
267 .Xr rc.conf 5 .
268 Also checks if the service is already running and refuses to start if
269 it is.
270 This latter check is not performed by standard
271 .Dx
272 scripts if the system is starting directly to multi-user mode, to
273 speed up the boot process.
274 If
275 .Cm faststart
276 is given, skip the PID check.
277 If
278 .Cm forcestart
279 is given, ignore the
280 .Xr rc.conf 5
281 check and start anyway.
282 .It Cm stop
283 If the service is to be started as specified by
284 .Xr rc.conf 5 ,
285 stop the service.
286 This should check that the service is running and complain if it is not.
287 If
288 .Cm forcestop
289 is given, ignore the
290 .Xr rc.conf 5
291 check and attempt to stop.
292 .It Cm restart
293 Perform a
294 .Cm stop
295 then a
296 .Cm start .
297 .It Cm reload
298 Send
299 .Va $sig_reload
300 (defaults to
301 .Dv HUP )
302 to the program(s) associated with the service.
303 Note that not all scripts enable the
304 .Cm reload
305 command.
306 .It Cm status
307 If the script starts a process (rather than performing a one-off
308 operation), show the status of the process.
309 Otherwise it is not necessary to support this argument.
310 Defaults to displaying the process ID of the program (if running).
311 .It Cm poll
312 If the script starts a process (rather than performing a one-off
313 operation), wait for the command to exit.
314 Otherwise it is not necessary to support this argument.
315 .It Cm rcvar
316 Display which
317 .Xr rc.conf 5
318 variables are used to control the startup of the service (if any).
319 .El
320 .Pp
321 If a script must implement additional commands it can list them in
322 the
323 .Va extra_commands
324 variable, and define their actions in a variable constructed from
325 the command name (see the
326 .Sx EXAMPLES
327 section).
328 .Pp
329 The following key points apply to old-style scripts in
330 .Pa /usr/local/etc/rc.d/ :
331 .Bl -bullet
332 .It
333 Scripts are only executed if their
334 .Xr basename 1
335 matches the shell globbing pattern
336 .Pa *.sh ,
337 and they are executable.
338 Any other files or directories present within the directory are silently
339 ignored.
340 .It
341 When a script is executed at boot time, it is passed the string
342 .Dq Li start
343 as its only argument.
344 At shutdown time, it is passed the string
345 .Dq Li stop
346 as its only argument.
347 All
348 .Nm rc.d/
349 scripts are expected to handle these arguments appropriately.
350 If no action needs to be taken at a given time
351 (either boot time or shutdown time),
352 the script should exit successfully and without producing an error message.
353 .It
354 The scripts within each directory are executed in lexicographical order.
355 If a specific order is required,
356 numbers may be used as a prefix to the existing filenames,
357 so for example
358 .Pa 100.foo
359 would be executed before
360 .Pa 200.bar ;
361 without the numeric prefixes the opposite would be true.
362 .It
363 The output from each script is traditionally a space character,
364 followed by the name of the software package being started or shut down,
365 .Em without
366 a trailing newline character (see the
367 .Sx EXAMPLES
368 section).
369 .El
370 .Sh SCRIPTS OF INTEREST
371 When an automatic reboot is in progress,
372 .Nm
373 is invoked with the argument
374 .Cm autoboot .
375 One of the scripts run from
376 .Pa /etc/rc.d/
377 is
378 .Pa /etc/rc.d/fsck .
379 This script runs
380 .Xr fsck 8
381 with option
382 .Fl p
383 to
384 .Dq preen
385 all
386 .Xr UFS 5
387 file systems of minor inconsistencies resulting
388 from the last system shutdown.
389 If preening fails further action depends on the
390 .Xr rc.conf 5
391 variable
392 .Va fsck_y_enable :
393 if the value is
394 .Dq NO
395 (default)
396 .Nm
397 exits, if value is
398 .Dq YES ,
399 .Xr fsck 8
400 is run with option
401 .Fl y ,
402 if this also fails
403 .Nm
404 exits.
405 If
406 .Cm autoboot
407 is not set, when going from single-user to multi-user mode for example,
408 the script does not do anything.
409 .Pp
410 The
411 .Pa /etc/rc.d/localdaemons
412 script can execute scripts from multiple
413 .Nm rc.d/
414 directories.
415 The default locations are
416 .Pa /usr/local/etc/rc.d/
417 and
418 .Pa /usr/pkg/etc/rc.d/ ,
419 but these may be overridden with the
420 .Va local_startup
421 .Xr rc.conf 5
422 variable.
423 .Pp
424 The
425 .Pa /etc/rc.d/serial
426 script is used to set any special configurations for serial devices.
427 .Pp
428 The
429 .Pa /etc/rc.d/{net*,routing}
430 scripts are used to start the network.
431 The network is started in several passes.
432 The first pass,
433 .Pa /etc/rc.d/netif ,
434 configures the network
435 interfaces.
436 The
437 .Pa /etc/rc.d/routing
438 script starts routing and sets routing options.
439 The
440 .Pa /etc/rc.d/netoptions
441 script sets additional networking options.
442 Finally, the
443 .Pa /etc/rc.d/network_ipv6
444 script configures IPv6 interfaces and options.
445 .Pp
446 The
447 .Nm rc.firewall
448 script is used to configure rules for the
449 .Xr ipfw 4
450 kernel based firewall
451 service.
452 It has several possible options:
453 .Pp
454 .Bl -tag -width ".Ar filename" -compact -offset indent
455 .It Cm open
456 will allow anyone in
457 .It Cm client
458 will try to protect just this machine
459 .It Cm simple
460 will try to protect a whole network
461 .It Cm closed
462 totally disables IP services except via
463 .Pa lo0
464 interface
465 .It Cm UNKNOWN
466 disables the loading of firewall rules
467 .It Ar filename
468 will load the rules in the given filename (full path required).
469 .El
470 .Pp
471 Most daemons, including network related daemons, have their own script in
472 .Pa /etc/rc.d/ ,
473 which can be used to start, stop, and check the status of the service.
474 .Pp
475 Any architecture specific scripts, such as
476 specifically check that they are on that architecture
477 before starting the daemon.
478 .Pp
479 Following tradition, all startup files reside in
480 .Pa /etc .
481 .Sh FILES
482 .Bl -tag -compact -width ".Pa /etc/rc.shutdown.local"
483 .It Pa /etc/rc
484 .It Pa /etc/rc.conf
485 .It Pa /etc/rc.conf.local
486 .It Pa /etc/rc.d/
487 .It Pa /etc/rc.firewall
488 .It Pa /etc/rc.local
489 .It Pa /etc/rc.shutdown
490 .It Pa /etc/rc.shutdown.local
491 .It Pa /etc/rc.subr
492 .El
493 .Sh EXAMPLES
494 The following is a minimal
495 .Nm rc.d/
496 style script.
497 Most scripts require little more than the following.
498 .Bd -literal -offset indent
499 #!/bin/sh
500 #
501
502 # PROVIDE: foo
503 # REQUIRE: bar_service_required_to_precede_foo
504 # BEFORE:  baz_service_requiring_foo_to_precede_it
505
506 \&. /etc/rc.subr
507
508 name="foo"
509 rcvar=`set_rcvar`
510 command="/usr/local/bin/foo"
511
512 load_rc_config $name
513 run_rc_command "$1"
514 .Ed
515 .Pp
516 Certain scripts may want to provide enhanced functionality.
517 The user may access this functionality through additional commands.
518 The script may list and define as many commands at it needs.
519 .Bd -literal -offset indent
520 #!/bin/sh
521 #
522
523 # PROVIDE: foo
524 # REQUIRE: bar_service_required_to_precede_foo
525 # BEFORE:  baz_service_requiring_foo_to_precede_it
526
527 \&. /etc/rc.subr
528
529 name="foo"
530 rcvar=`set_rcvar`
531 command="/usr/local/bin/foo"
532 extra_commands="nop hello"
533 hello_cmd="echo Hello World."
534 nop_cmd="do_nop"
535
536 do_nop()
537 {
538         echo "I do nothing."
539 }
540
541 load_rc_config $name
542 run_rc_command "$1"
543 .Ed
544 .Pp
545 The following is a simple, hypothetical example of an old-style
546 .Pa /usr/local/etc/rc.d/
547 script,
548 which would start a daemon at boot time,
549 and kill it at shutdown time.
550 .Bd -literal -offset indent
551 #!/bin/sh -
552 #
553 #    initialization/shutdown script for foobar package
554
555 case "$1" in
556 start)
557         /usr/local/sbin/foo -d && echo -n ' foo'
558         ;;
559 stop)
560         kill `cat /var/run/foo.pid` && echo -n ' foo'
561         ;;
562 *)
563         echo "unknown option: $1 - should be 'start' or 'stop'" >&2
564         ;;
565 esac
566 .Ed
567 .Pp
568 As all processes are killed by
569 .Xr init 8
570 at shutdown, the explicit
571 .Xr kill 1
572 is unnecessary, but is often included.
573 .Sh SEE ALSO
574 .Xr kill 1 ,
575 .Xr ipfw 4 ,
576 .Xr rc.conf 5 ,
577 .Xr init 8 ,
578 .Xr rcorder 8 ,
579 .Xr rcrun 8 ,
580 .Xr rc.subr 8 ,
581 .Xr reboot 8 ,
582 .Xr savecore 8
583 .Sh HISTORY
584 The
585 .Nm
586 utility appeared in
587 .Bx 4.0 .
588 The
589 .Nm rc.d/
590 facility was implemented in
591 .Nx 1.5
592 and appeared in
593 .Dx 1.0 .