# $FabBSD$ # $OpenBSD: rc,v 1.318 2008/07/09 20:23:47 djm Exp $ # System startup script run by init on autoboot # or after single-user. # Output and error are redirected to console by init, # and the console is the controlling terminal. # Subroutines (have to come first). # Strip comments (and leading/trailing whitespace if IFS is set) # from a file and spew to stdout stripcom() { local _file="$1" local _line { while read _line ; do _line=${_line%%#*} # strip comments test -z "$_line" && continue echo $_line done } < $_file } # Update resource limits when sysctl changes # Usage: update_limit -X loginconf_name update_limit() { local _fl="$1" # ulimit flag local _lc="$2" # login.conf name local _new _suf for _suf in "" -cur -max; do _new=`getcap -f /etc/login.conf -s ${_lc}${_suf} daemon 2>/dev/null` if [ X"$_new" != X"" ]; then if [ X"$_new" = X"infinity" ]; then _new=unlimited fi case "$_suf" in -cur) ulimit -S $_fl $_new ;; -max) ulimit -H $_fl $_new ;; *) ulimit $_fl $_new return ;; esac fi done } sysctl_conf() { test -s /etc/sysctl.conf || return # delete comments and blank lines set -- `stripcom /etc/sysctl.conf` while [ $# -ge 1 ] ; do sysctl $1 # update limits if needed case $1 in kern.maxproc=*) update_limit -p maxproc ;; kern.maxfiles=*) update_limit -n openfiles ;; esac shift done } wsconsctl_conf() { local save_IFS="$IFS" test -x /sbin/wsconsctl -a -s /etc/wsconsctl.conf || return # delete comments and blank lines IFS=" " set -- `stripcom /etc/wsconsctl.conf` IFS="$save_IFS" while [ $# -ge 1 ] ; do eval /sbin/wsconsctl $1 shift done } random_seed() { if [ -f /var/db/host.random -a "X$random_seed_done" = "X" ]; then dd if=/var/db/host.random of=/dev/urandom bs=1024 count=64 \ > /dev/null 2>&1 dd if=/var/db/host.random of=/dev/arandom bs=1024 count=64 \ > /dev/null 2>&1 # reset seed file, so that if a shutdown-less reboot occurs, # the next seed is not a repeat dd if=/dev/urandom of=/var/db/host.random bs=1024 count=64 \ > /dev/null 2>&1 random_seed_done=1 fi } fill_baddynamic() { local _service="$1" local _sysctl="net.inet.${_service}.baddynamic" local _name _port _srv _junk _ban local _i=0 grep "/${_service}" /etc/services | { IFS=" /" while read _name _port _srv _junk; do [ "x${_srv}" = "x${_service}" ] || continue; if [ "x${_ban}" = "x" ]; then _ban="+${_port}" else _ban="${_ban},+${_port}" fi # Flush before argv gets too long if [ $((++_i)) -gt 128 ]; then sysctl ${_sysctl}=${_ban} >/dev/null _ban="" _i=0 fi done; if [ "x${_ban}" != "x" ]; then sysctl ${_sysctl}=${_ban} >/dev/null fi } } # End subroutines stty status '^T' # Set shell to ignore SIGINT (2), but not children; # shell catches SIGQUIT (3) and returns to single user after fsck. trap : 2 trap : 3 # shouldn't be needed HOME=/; export HOME PATH=/sbin:/bin:/usr/sbin:/usr/bin export PATH if [ X"$1" = X"shutdown" ]; then dd if=/dev/urandom of=/var/db/host.random bs=1024 count=64 >/dev/null 2>&1 chmod 600 /var/db/host.random >/dev/null 2>&1 if [ $? -eq 0 -a -f /etc/rc.shutdown ]; then echo /etc/rc.shutdown in progress... . /etc/rc.shutdown echo /etc/rc.shutdown complete. # bring carp interfaces down gracefully ifconfig -a|grep '^carp.*:'|cut -f1 -d:|while read if do ifconfig $if down done if [ X"${powerdown}" = X"YES" ]; then exit 2 fi else echo single user: not running /etc/rc.shutdown fi exit 0 fi # Configure ccd devices. if [ -f /etc/ccd.conf ]; then ccdconfig -C fi # Configure raid devices. #for dev in 0 1 2 3; do # if [ -f /etc/raid$dev.conf ]; then # raidctl -c /etc/raid$dev.conf raid$dev # fi #done # Check parity on raid devices. #raidctl -P all swapctl -A -t blk if [ -e /fastboot ]; then echo "Fast boot: skipping disk checks." elif [ X"$1" = X"autoboot" ]; then echo "Automatic boot in progress: starting file system checks." fsck -p case $? in 0) ;; 2) exit 1 ;; 4) echo "Rebooting..." reboot echo "Reboot failed; help!" exit 1 ;; 8) echo "Automatic file system check failed; help!" exit 1 ;; 12) echo "Boot interrupted." exit 1 ;; 130) # interrupt before catcher installed exit 1 ;; *) echo "Unknown error; help!" exit 1 ;; esac fi trap "echo 'Boot interrupted.'; exit 1" 3 umount -a >/dev/null 2>&1 mount -a -t nonfs,vnd mount -uw / # root on nfs requires this, others aren't hurt rm -f /fastboot # XXX (root now writeable) random_seed # pick up option configuration . /etc/rc.conf # set flags on ttys. (do early, in case they use tty for SLIP in netstart) echo 'setting tty flags' ttyflags -a if [ -f /sbin/kbd -a -f /etc/kbdtype ]; then kbd `cat /etc/kbdtype` fi wsconsctl_conf # Fill net.inet.(tcp|udp).baddynamic lists from /etc/services fill_baddynamic udp fill_baddynamic tcp sysctl_conf # set hostname, turn on network echo 'starting network' ifconfig -g carp carpdemote 128 if [ -f /etc/resolv.conf.save ]; then mv /etc/resolv.conf.save /etc/resolv.conf touch /etc/resolv.conf fi . /etc/netstart mount -s /usr >/dev/null 2>&1 mount -s /var >/dev/null 2>&1 # if there's no /var/db/host.random, make one through /dev/urandom if [ ! -f /var/db/host.random ]; then dd if=/dev/urandom of=/var/db/host.random bs=1024 count=64 \ >/dev/null 2>&1 chmod 600 /var/db/host.random >/dev/null 2>&1 else # Try to read seed if it was not initially present (e.g. /var on NFS) random_seed fi # clean up left-over files rm -f /etc/nologin rm -f /var/spool/lock/LCK.* rm -f /var/spool/uucp/STST/* (cd /var/run && { rm -rf -- *; install -c -m 664 -g utmp /dev/null utmp; }) # save a copy of the boot messages dmesg >/var/run/dmesg.boot echo 'starting system logger' rm -f /dev/log if [ -d /var/empty ]; then rm -f /var/empty/dev/log mkdir -p -m 0555 /var/empty/dev syslogd_flags="${syslogd_flags} -a /var/empty/dev/log" fi syslogd ${syslogd_flags} if [ X"${ipsec}" != X"NO" ]; then if [ -f ${ipsec_rules} ]; then ipsecctl -f ${ipsec_rules} fi fi echo -n 'starting initial daemons:' if [ X"${portmap}" = X"YES" ]; then echo -n ' portmap'; portmap fi if [ X"${nfs_server}" = X"YES" -a -s /etc/exports -a \ `sed -e '/^#/d' < /etc/exports | wc -l` -ne 0 ]; then rm -f /var/db/mountdtab echo -n > /var/db/mountdtab echo -n ' mountd'; mountd echo -n ' nfsd'; nfsd ${nfsd_flags} if [ X"${lockd}" = X"YES" ]; then echo -n ' rpc.lockd'; rpc.lockd echo -n ' rpc.statd'; rpc.statd fi fi if [ X"${amd}" = X"YES" -a -e ${amd_master} ]; then echo -n ' amd' (cd /etc/amd; amd -l syslog -x error,noinfo,nostats -p \ -a ${amd_dir} `cat ${amd_master}` > /var/run/amd.pid ) fi # run rdate before timed/ntpd if [ X"${rdate_flags}" != X"NO" ]; then echo -n ' rdate'; rdate -s ${rdate_flags} fi if [ X"${ldattach_flags}" != X"NO" -a -n "${ldattach_flags}" ]; then echo -n ' ldattach'; ldattach ${ldattach_flags} fi echo '.' mount -a swapctl -A -t noblk # /var/crash should be a directory or a symbolic link # to the crash directory if core dumps are to be saved. if [ -d /var/crash ]; then savecore ${savecore_flags} /var/crash fi # build ps databases echo -n 'building ps databases:' echo -n " kvm" kvm_mkdb echo -n " dev" dev_mkdb echo "." chmod 666 /dev/tty[pqrstuvwxyzPQRST]* chown root:wheel /dev/tty[pqrstuvwxyzPQRST]* # check the password temp/lock file if [ -f /etc/ptmp ]; then logger -s -p auth.err \ 'password file may be incorrect -- /etc/ptmp exists' fi echo clearing /tmp # prune quickly with one rm, then use find to clean up /tmp/[lq]* # (not needed with mfs /tmp, but doesn't hurt there...) (cd /tmp && rm -rf [a-km-pr-zA-Z]* && find . ! -name . ! -name lost+found \ -execdir rm -rf -- {} \; -type d -prune) [ -f /etc/rc.securelevel ] && . /etc/rc.securelevel if [ X"${securelevel}" != X"" ]; then echo -n 'setting kernel security level: ' sysctl kern.securelevel=${securelevel} fi # patch /etc/motd if [ ! -f /etc/motd ]; then install -c -o root -g wheel -m 664 /dev/null /etc/motd fi T=`mktemp /tmp/_motd.XXXXXXXXXX` if [ $? -eq 0 ]; then sysctl -n kern.version | sed 1q > $T echo "" >> $T sed '1,/^$/d' < /etc/motd >> $T cmp -s $T /etc/motd || cp $T /etc/motd rm -f $T fi if [ -f /sbin/ldconfig ]; then echo 'creating runtime link editor directory cache.' if [ -d /usr/local/lib ]; then shlib_dirs="/usr/local/lib $shlib_dirs" fi ldconfig $shlib_dirs fi if [ -x /usr/libexec/vi.recover ]; then echo 'preserving editor files.'; /usr/libexec/vi.recover fi if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then echo -n "ssh-keygen: generating new DSA host key... " if /usr/bin/ssh-keygen -q -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''; then echo done. else echo failed. fi fi if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then echo -n "ssh-keygen: generating new RSA host key... " if /usr/bin/ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''; then echo done. else echo failed. fi fi if [ ! -f /etc/ssh/ssh_host_key ]; then echo -n "ssh-keygen: generating new RSA1 host key... " if /usr/bin/ssh-keygen -q -t rsa1 -f /etc/ssh/ssh_host_key -N ''; then echo done. else echo failed. fi fi echo -n starting network daemons: if [ X"${snmpd_flags}" != X"NO" ]; then echo -n ' snmpd'; /usr/sbin/snmpd $snmpd_flags fi if [ X"${ifstated_flags}" != X"NO" ]; then echo -n ' ifstated'; ifstated $ifstated_flags fi if [ X"${lpd_flags}" != X"NO" ]; then echo -n ' lpd'; lpd ${lpd_flags} fi if [ X"${ftpd_flags}" != X"NO" ]; then echo -n ' ftpd'; /usr/libexec/ftpd ${ftpd_flags} fi if [ X"${inetd}" = X"YES" -a -e /etc/inetd.conf ]; then echo -n ' inetd'; inetd fi if [ X"${sshd_flags}" != X"NO" ]; then echo -n ' sshd'; /usr/sbin/sshd ${sshd_flags}; fi echo '.' [ -f /etc/rc.local ] && . /etc/rc.local echo -n standard daemons: if [ X"${apmd_flags}" != X"NO" -a -x /usr/sbin/apmd ]; then echo -n ' apmd'; /usr/sbin/apmd ${apmd_flags} fi if [ X"${sensorsd_flags}" != X"NO" ]; then echo -n ' sensorsd'; /usr/sbin/sensorsd ${sensorsd_flags} fi if [ X"${hotplugd_flags}" != X"NO" -a -x /usr/sbin/hotplugd ]; then echo -n ' hotplugd'; /usr/sbin/hotplugd ${hotplugd_flags} fi if [ X"${watchdogd_flags}" != X"NO" -a -x /usr/sbin/watchdogd ]; then echo -n ' watchdogd'; /usr/sbin/watchdogd ${watchdogd_flags} fi echo -n ' cron'; cron # disable carp interlock ifconfig -g carp -carpdemote 128 echo '.' if [ -e "/var/db/cnc.timings" ]; then cncctl -C else echo "WARNING: CNC timings were not calibrated yet; see cncctl(8)." fi date exit 0