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