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