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