1#!/bin/sh 2# 3# Copyright (c) 2000 The FreeBSD Project 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# 27# @(#)rc 5.27 (Berkeley) 6/5/91 28# $FreeBSD: src/etc/rc,v 1.212.2.51 2002/10/17 17:25:07 schweikh Exp $ 29# $DragonFly: src/etc/rc,v 1.2 2003/06/17 04:24:45 dillon Exp $ 30# 31 32# System startup script run by init on autoboot 33# or after single-user. 34# Output and error are redirected to console by init, 35# and the console is the controlling terminal. 36 37# Note that almost all of the user-configurable behavior is no longer in 38# this file, but rather in /etc/defaults/rc.conf. Please check that file 39# first before contemplating any changes here. If you do need to change 40# this file for some reason, we would like to know about it. 41 42stty status '^T' 43 44# Set shell to ignore SIGINT (2), but not children; 45# shell catches SIGQUIT (3) and returns to single user after fsck. 46# 47trap : 2 48trap : 3 # shouldn't be needed 49 50bootmode=$1 51 52HOME=/ 53PATH=/sbin:/bin:/usr/sbin:/usr/bin 54export HOME PATH 55 56# BOOTP diskless boot. We have to run the rc file early in order to 57# retarget various config files. 58# See /usr/share/examples/diskless/clone_root for details on how 59# to setup diskless on the client and the server. 60# 61if [ -r /etc/rc.diskless1 ]; then 62 dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 63 if [ ${dlv:=0} != 0 ]; then 64 . /etc/rc.diskless1 65 fi 66fi 67 68# If there is a global system configuration file, suck it in. 69# 70if [ -r /etc/defaults/rc.conf ]; then 71 . /etc/defaults/rc.conf 72 source_rc_confs 73elif [ -r /etc/rc.conf ]; then 74 . /etc/rc.conf 75fi 76 77chkdepend() { 78 svc=$1 79 svc_var=$2 80 dep=$3 81 dep_var=$4 82 83 eval svc_val=\${$svc_var} 84 eval dep_val=\${$dep_var} 85 86 case ${svc_val} in 87 [Yy][Ee][Ss]) 88 case ${dep_val} in 89 [Yy][Ee][Ss]) 90 ;; 91 *) 92 eval ${dep_var}="YES" 93 echo "DEPENDENCY NOTE: ${dep} will be enabled" \ 94 "to support ${svc}" 95 ;; 96 esac 97 ;; 98 esac 99} 100 101chkdepend amd amd_enable portmap portmap_enable 102chkdepend amd amd_enable NFS nfs_client_enable 103chkdepend NFS nfs_server_enable portmap portmap_enable 104chkdepend NIS nis_server_enable portmap portmap_enable 105chkdepend NIS nis_client_enable portmap portmap_enable 106 107# Enable dumpdev early so that a crash during the boot process can be caught. 108# 109case ${dumpdev} in 110[Nn][Oo] | '') 111 dumpdev='NO' 112 ;; 113*) 114 /sbin/dumpon -v ${dumpdev} 115 ;; 116esac 117 118# Configure ccd devices. 119# 120if [ -r /etc/ccd.conf ]; then 121 ccdconfig -C 122fi 123 124case ${start_vinum} in 125[Yy][Ee][Ss]) 126 vinum start 127 ;; 128esac 129 130swapon -a 131 132# Last chance to do things before potentially waiting for 133# operator to do fsck related tasks 134if [ -r /etc/rc.early ]; then 135 . /etc/rc.early 136fi 137 138case ${bootmode} in 139autoboot) 140 echo 'Automatic boot in progress...' 141 fsck -p 142 case $? in 143 0) 144 ;; 145 2) 146 exit 1 147 ;; 148 4) 149 reboot 150 echo 'Reboot failed... help!' 151 exit 1 152 ;; 153 8) 154 case ${fsck_y_enable} in 155 [Yy][Ee][Ss]) 156 echo 'File system preen failed, trying fsck -y . . .' 157 fsck -y 158 case $? in 159 0) 160 ;; 161 *) 162 echo 'Automatic file system check failed . . . help!' 163 exit 1 164 ;; 165 esac 166 ;; 167 *) 168 echo 'Automatic file system check failed . . . help!' 169 exit 1 170 ;; 171 esac 172 ;; 173 12) 174 echo 'Reboot interrupted' 175 exit 1 176 ;; 177 130) 178 # interrupt before catcher installed 179 exit 1 180 ;; 181 *) 182 echo 'Unknown error in reboot' 183 exit 1 184 ;; 185 esac 186 ;; 187*) 188 echo 'Skipping disk checks ...' 189 ;; 190esac 191 192set -T 193trap "echo 'Reboot interrupted'; exit 1" 3 194 195# root normally must be read/write, but if this is a BOOTP NFS 196# diskless boot it does not have to be. 197# 198case ${root_rw_mount} in 199[Nn][Oo] | '') 200 ;; 201*) 202 if ! mount -u -o rw /; then 203 echo 'Mounting root filesystem rw failed, startup aborted' 204 exit 1 205 fi 206 ;; 207esac 208 209umount -a >/dev/null 2>&1 210 211# If using diskless, run custom disk mounting function here 212# 213if [ -n "${diskless_mount}" -a -r "${diskless_mount}" ]; then 214 sh ${diskless_mount} 215else 216# otherwise mount everything except nfs filesystems. 217 mount -a -t nonfs 218fi 219 220case $? in 2210) 222 ;; 223*) 224 echo 'Mounting /etc/fstab filesystems failed, startup aborted' 225 exit 1 226 ;; 227esac 228 229adjkerntz -i 230 231purgedir() { 232 local dir file 233 234 if [ $# -eq 0 ]; then 235 purgedir . 236 else 237 for dir 238 do 239 ( 240 cd "$dir" && for file in .* * 241 do 242 [ ."$file" = .. -o ."$file" = ... ] && continue 243 if [ -d "$file" -a ! -L "$file" ]; then 244 purgedir "$file" 245 else 246 rm -f -- "$file" 247 fi 248 done 249 ) 250 done 251 fi 252} 253 254clean_var() { 255 if [ -d /var/run -a ! -f /var/run/clean_var ]; then 256 purgedir /var/run 257 # Keep a copy of the boot messages around 258 dmesg >/var/run/dmesg.boot 259 # And an initial utmp file 260 (cd /var/run && cp /dev/null utmp && chmod 644 utmp;) 261 >/var/run/clean_var 262 fi 263 if [ -d /var/spool/lock -a ! -f /var/spool/lock/clean_var ]; then 264 purgedir /var/spool/lock 265 >/var/spool/lock/clean_var 266 fi 267 rm -rf /var/spool/uucp/.Temp/* 268} 269 270# network_pass1() *may* end up writing stuff to /var - we don't want to 271# remove it immediately afterwards - *nor* do we want to fail to clean 272# an NFS-mounted /var. 273rm -f /var/run/clean_var /var/spool/lock/clean_var 274clean_var 275 276# Add additional swapfile, if configured. 277# 278case ${swapfile} in 279[Nn][Oo] | '') 280 ;; 281*) 282 if [ -w "${swapfile}" -a -c /dev/vn0b ]; then 283 echo "Adding ${swapfile} as additional swap" 284 vnconfig -e /dev/vn0b ${swapfile} swap 285 fi 286 ;; 287esac 288 289# Early pass to set the variables we can 290# 291if [ -r /etc/rc.sysctl ]; then 292 sh /etc/rc.sysctl first 293fi 294 295# Configure serial devices 296# 297if [ -r /etc/rc.serial ]; then 298 . /etc/rc.serial 299fi 300 301# Start up PC-card configuration 302# 303if [ -r /etc/rc.pccard ]; then 304 . /etc/rc.pccard 305fi 306 307# Start up the initial network configuration. 308# 309if [ -r /etc/rc.network ]; then 310 . /etc/rc.network # We only need to do this once. 311 network_pass1 312fi 313 314case ${ipv6_enable} in 315[Yy][Ee][Ss]) 316 if [ -r /etc/rc.network6 ]; then 317 . /etc/rc.network6 # We only need to do this once also. 318 network6_pass1 319 fi 320 ;; 321esac 322 323# Mount NFS filesystems if present in /etc/fstab 324# 325case "`mount -d -a -t nfs 2> /dev/null`" in 326*mount_nfs*) 327 echo -n 'Mounting NFS file systems:' 328 mount -a -t nfs 329 echo '.' 330 ;; 331esac 332 333# If we booted a special kernel remove the record so we will boot 334# the default kernel next time 335# 336rm -f /boot/nextboot.conf 337 338# Whack the pty perms back into shape. 339# 340if ls /dev/tty[pqrsPQRS]* > /dev/null 2>&1; then 341 chflags 0 /dev/tty[pqrsPQRS]* 342 chmod 666 /dev/tty[pqrsPQRS]* 343 chown root:wheel /dev/tty[pqrsPQRS]* 344fi 345 346# Clean up left-over files 347# 348clean_var # If it hasn't already been done 349rm /var/run/clean_var /var/spool/lock/clean_var 350 351# Clearing /tmp at boot-time seems to have a long tradition. It doesn't 352# help in any way for long-living systems, and it might accidentally 353# clobber files you would rather like to have preserved after a crash 354# (if not using mfs /tmp anyway). 355# 356# See also the example of another cleanup policy in /etc/periodic/daily. 357# 358case ${clear_tmp_enable} in 359[Yy][Ee][Ss]) 360 echo -n 'Clearing /tmp:' 361 # prune quickly with one rm, then use find to clean up /tmp/[lq]* 362 # (not needed with mfs /tmp, but doesn't hurt there...) 363 (cd /tmp && rm -rf [a-km-pr-zA-Z]* && 364 find -d . ! -name . ! -name lost+found ! -name quota.user \ 365 ! -name quota.group -exec rm -rf -- {} \;) 366 echo '.' 367 ;; 368esac 369 370# Remove X lock files, since they will prevent you from restarting X11 371# after a system crash. 372# 373rm -f /tmp/.X*-lock 374rm -fr /tmp/.X11-unix 375mkdir -m 1777 /tmp/.X11-unix 376 377# Snapshot any kernel -c changes back to disk here <someday>. 378# This has changed with ELF and /kernel.config. 379 380echo -n 'Additional daemons:' 381 382# Start system logging and name service. Named needs to start before syslogd 383# if you don't have a /etc/resolv.conf. 384# 385case ${syslogd_enable} in 386[Yy][Ee][Ss]) 387 # Transitional symlink (for the next couple of years :) until all 388 # binaries have had a chance to move towards /var/run/log. 389 if [ ! -L /dev/log ]; then 390 # might complain for r/o root f/s 391 ln -sf /var/run/log /dev/log 392 fi 393 394 rm -f /var/run/log 395 echo -n ' syslogd'; 396 ${syslogd_program:-/usr/sbin/syslogd} ${syslogd_flags} 397 ;; 398esac 399 400echo '.' 401 402# Build devices database 403# 404dev_mkdb 405 406# $dumpdir should be a directory or a symbolic link 407# to the crash directory if core dumps are to be saved. 408# 409if [ "${dumpdev}" != 'NO' ]; then 410 case ${dumpdir} in 411 '') 412 dumpdir='/var/crash' 413 ;; 414 [Nn][Oo]) 415 dumpdir='NO' 416 ;; 417 esac 418 419 if [ "${dumpdir}" != 'NO' ]; then 420 echo -n 'Checking for core dump: ' 421 /sbin/savecore ${savecore_flags} "${dumpdir}" 422 fi 423fi 424 425if [ -n "${network_pass1_done}" ]; then 426 network_pass2 427fi 428 429# Enable/Check the quotas (must be after ypbind if using NIS) 430# 431case ${enable_quotas} in 432[Yy][Ee][Ss]) 433 case ${check_quotas} in 434 [Yy][Ee][Ss]) 435 echo -n 'Checking quotas:' 436 quotacheck -a 437 echo ' done.' 438 ;; 439 esac 440 441 echo -n 'Enabling quotas:' 442 quotaon -a 443 echo ' done.' 444 ;; 445esac 446 447if [ -n "${network_pass2_done}" ]; then 448 network_pass3 449fi 450 451# Check the password temp/lock file 452# 453if [ -e /etc/ptmp ]; then 454 logger -s -p auth.err \ 455 "password file may be incorrect -- /etc/ptmp exists" 456fi 457 458case ${accounting_enable} in 459[Yy][Ee][Ss]) 460 if [ -d /var/account ]; then 461 echo 'Turning on accounting:' 462 if [ ! -e /var/account/acct ]; then 463 touch /var/account/acct 464 fi 465 accton /var/account/acct 466 fi 467 ;; 468esac 469 470# Make shared lib searching a little faster. Leave /usr/lib first if you 471# add your own entries or you may come to grief. 472# 473ldconfig="/sbin/ldconfig" 474case ${ldconfig_insecure} in 475[Yy][Ee][Ss]) 476 ldconfig="${ldconfig} -i" 477 ;; 478esac 479if [ -x /sbin/ldconfig ]; then 480 case `/usr/bin/objformat` in 481 elf) 482 _LDC=/usr/lib 483 for i in ${ldconfig_paths}; do 484 if [ -d "${i}" ]; then 485 _LDC="${_LDC} ${i}" 486 fi 487 done 488 echo 'ELF ldconfig path:' ${_LDC} 489 ${ldconfig} -elf ${_LDC} 490 ;; 491 esac 492 493 # Legacy aout support for i386 only 494 case `sysctl -n hw.machine_arch` in 495 i386) 496 # Default the a.out ldconfig path. 497 : ${ldconfig_paths_aout=${ldconfig_paths}} 498 _LDC=/usr/lib/aout 499 for i in ${ldconfig_paths_aout}; do 500 if [ -d "${i}" ]; then 501 _LDC="${_LDC} ${i}" 502 fi 503 done 504 echo 'a.out ldconfig path:' ${_LDC} 505 ${ldconfig} -aout ${_LDC} 506 ;; 507 esac 508fi 509 510# Now start up miscellaneous daemons that don't belong anywhere else 511# 512echo -n 'Starting standard daemons:' 513case ${inetd_enable} in 514[Nn][Oo]) 515 ;; 516*) 517 echo -n ' inetd'; ${inetd_program:-/usr/sbin/inetd} ${inetd_flags} 518 ;; 519esac 520 521case ${cron_enable} in 522[Nn][Oo]) 523 ;; 524*) 525 echo -n ' cron'; ${cron_program:-/usr/sbin/cron} ${cron_flags} 526 ;; 527esac 528 529case ${lpd_enable} in 530[Yy][Ee][Ss]) 531 echo -n ' printer'; ${lpd_program:-/usr/sbin/lpd} ${lpd_flags} 532 ;; 533esac 534 535case ${sshd_enable} in 536[Yy][Ee][Ss]) 537 if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then 538 echo -n ' sshd'; 539 ${sshd_program:-/usr/sbin/sshd} ${sshd_flags} 540 fi 541 ;; 542esac 543 544case ${usbd_enable} in 545[Yy][Ee][Ss]) 546 echo -n ' usbd'; /usr/sbin/usbd ${usbd_flags} 547 ;; 548esac 549 550case ${mta_start_script} in 551/*) 552 if [ -r ${mta_start_script} ]; then 553 sh ${mta_start_script} 554 fi 555 ;; 556esac 557 558echo '.' 559 560# Recover vi editor files. 561find /var/tmp/vi.recover ! -type f -a ! -type d -delete 562vibackup=`echo /var/tmp/vi.recover/vi.*` 563if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then 564 echo -n 'Recovering vi editor sessions:' 565 for i in /var/tmp/vi.recover/vi.*; do 566 # Only test files that are readable. 567 if [ ! -r "${i}" ]; then 568 continue 569 fi 570 571 # Unmodified nvi editor backup files either have the 572 # execute bit set or are zero length. Delete them. 573 if [ -x "${i}" -o ! -s "${i}" ]; then 574 rm -f "${i}" 575 fi 576 done 577 578 # It is possible to get incomplete recovery files, if the editor 579 # crashes at the right time. 580 virecovery=`echo /var/tmp/vi.recover/recover.*` 581 if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then 582 for i in /var/tmp/vi.recover/recover.*; do 583 # Only test files that are readable. 584 if [ ! -r "${i}" ]; then 585 continue 586 fi 587 588 # Delete any recovery files that are zero length, 589 # corrupted, or that have no corresponding backup file. 590 # Else send mail to the user. 591 recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"` 592 if [ -n "${recfile}" -a -s "${recfile}" ]; then 593 sendmail -t < "${i}" 594 else 595 rm -f "${i}" 596 fi 597 done 598 fi 599 echo '.' 600fi 601 602# Make a bounds file for msgs(1) if there isn't one already 603# 604if [ -d /var/msgs -a ! -f /var/msgs/bounds -a ! -L /var/msgs/bounds ]; then 605 echo 0 > /var/msgs/bounds 606fi 607 608case ${update_motd} in 609[Nn][Oo] | '') 610 ;; 611*) 612 if T=`mktemp /tmp/_motd.XXXXXX`; then 613 uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T} 614 awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T} 615 cmp -s ${T} /etc/motd || { 616 cp ${T} /etc/motd 617 chmod 644 /etc/motd 618 } 619 rm -f ${T} 620 fi 621 ;; 622esac 623 624# Configure implementation specific stuff 625# 626arch=`uname -p` 627if [ -r /etc/rc.${arch} ]; then 628 . /etc/rc.${arch} 629fi 630 631# Configure the system console 632# 633if [ -r /etc/rc.syscons ]; then 634 . /etc/rc.syscons 635fi 636 637echo -n 'Additional ABI support:' 638 639# Start the Linux binary compatibility if requested. 640# 641case ${linux_enable} in 642[Yy][Ee][Ss]) 643 echo -n ' linux' 644 if ! kldstat -v | grep -E 'linux(aout|elf)' > /dev/null; then 645 kldload linux > /dev/null 2>&1 646 fi 647 if [ -x /compat/linux/sbin/ldconfig ]; then 648 /compat/linux/sbin/ldconfig 649 fi 650 ;; 651esac 652 653# Start the SysVR4 binary emulation if requested. 654# 655case ${svr4_enable} in 656[Yy][Ee][Ss]) 657 echo -n ' svr4'; 658 kldload streams > /dev/null 2>&1 659 kldload svr4 > /dev/null 2>&1 660 ;; 661esac 662 663echo '.' 664 665# Do traditional (but rather obsolete) rc.local file if it exists. If you 666# use this file and want to make it programmatic, source /etc/defaults/rc.conf 667# in /etc/rc.local and add your custom variables to /etc/rc.conf, as 668# shown below. Please do not put local extensions into /etc/rc itself. 669# Use /etc/rc.local 670# 671# ---- rc.local ---- 672# if [ -r /etc/defaults/rc.conf ]; then 673# . /etc/defaults/rc.conf 674# source_rc_confs 675# elif [ -r /etc/rc.conf ]; then 676# . /etc/rc.conf 677# fi 678# 679# ... additional startup conditionals ... 680# ---- rc.local ---- 681# 682if [ -r /etc/rc.local ]; then 683 echo -n 'Starting local daemons:' 684 sh /etc/rc.local 685 echo '.' 686fi 687 688# For each valid dir in $local_startup, search for init scripts matching *.sh 689# 690case ${local_startup} in 691[Nn][Oo] | '') 692 ;; 693*) 694 echo -n 'Local package initialization:' 695 slist="" 696 if [ -z "${script_name_sep}" ]; then 697 script_name_sep=" " 698 fi 699 for dir in ${local_startup}; do 700 if [ -d "${dir}" ]; then 701 for script in ${dir}/*.sh; do 702 slist="${slist}${script_name_sep}${script}" 703 done 704 fi 705 done 706 script_save_sep="$IFS" 707 IFS="${script_name_sep}" 708 for script in ${slist}; do 709 if [ -x "${script}" ]; then 710 (set -T 711 trap 'exit 1' 2 712 ${script} start) 713 elif [ -f "${script}" -o -L "${script}" ]; then 714 echo -n " (skipping ${script##*/}, not executable)" 715 fi 716 done 717 IFS="${script_save_sep}" 718 echo '.' 719 ;; 720esac 721 722if [ -n "${network_pass3_done}" ]; then 723 network_pass4 724fi 725 726# Late pass to set variables we missed the first time 727# 728if [ -r /etc/rc.sysctl ]; then 729 sh /etc/rc.sysctl last 730fi 731 732# Raise kernel security level. This should be done only after `fsck' has 733# repaired local file systems if you want the securelevel to be greater than 1. 734# 735case ${kern_securelevel_enable} in 736[Yy][Ee][Ss]) 737 if [ "${kern_securelevel}" -ge 0 ]; then 738 echo 'Raising kernel security level: ' 739 sysctl kern.securelevel=${kern_securelevel} 740 fi 741 ;; 742esac 743 744echo '' 745 746date 747 748exit 0 749 750