rc revision 50472
140123Sdes#!/bin/sh
250472Speter# $FreeBSD: head/etc/rc 50472 1999-08-27 23:37:10Z peter $
3709Swollman#	From: @(#)rc	5.27 (Berkeley) 6/5/91
437Srgrimes
537Srgrimes# System startup script run by init on autoboot
637Srgrimes# or after single-user.
737Srgrimes# Output and error are redirected to console by init,
837Srgrimes# and the console is the controlling terminal.
937Srgrimes
108460Sjkh# Note that almost all the user-configurable behavior is no longer in
1143951Sjkh# this file, but rather in /etc/defaults/rc.conf.  Please check this file
128460Sjkh# first before contemplating any changes here.
138460Sjkh
1437Srgrimesstty status '^T'
1537Srgrimes
1637Srgrimes# Set shell to ignore SIGINT (2), but not children;
1737Srgrimes# shell catches SIGQUIT (3) and returns to single user after fsck.
1837Srgrimestrap : 2
1937Srgrimestrap : 3	# shouldn't be needed
2037Srgrimes
2137SrgrimesHOME=/; export HOME
2220684SjoergPATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
2337Srgrimesexport PATH
2437Srgrimes
2543179Sdillon# BOOTP diskless boot.  We have to run the rc file early in order to
2643803Sdillon# retarget various config files.
2743179Sdillon#
2843803Sdillonif [ -f /etc/rc.diskless1 ]; then
2943375Sdillon	dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
3043375Sdillon	if [ ${dlv:=0} != 0 ]; then
3143803Sdillon		. /etc/rc.diskless1
3243179Sdillon	fi
3343179Sdillonfi
3443179Sdillon
3543219Speter# If there is a global system configuration file, suck it in.
3643219Speter#
3743849Sjkhif [ -f /etc/defaults/rc.conf ]; then
3843849Sjkh	. /etc/defaults/rc.conf
3943849Sjkhelif [ -f /etc/rc.conf ]; then
4043219Speter	. /etc/rc.conf
4143219Speterfi
4243219Speter
4315568Sasami# Configure ccd devices.
4443803Sdillonif [ -f /etc/ccd.conf ]; then
4515568Sasami	ccdconfig -C
4615568Sasamifi
4715568Sasami
4850357Ssheldonhif [ "${start_vinum}" = "YES" ]; then
4945239Sgrog	vinum start
5050357Ssheldonhelif [ -n "${vinum_drives}" ]; then
5150357Ssheldonh	vinum read ${vinum_drives}
5242741Sgrogfi
5342741Sgrog
5443803Sdillonswapon -a
553843Sdg
5650357Ssheldonhif [ "$1" = "autoboot" ]; then
5737Srgrimes	echo Automatic reboot in progress...
5837Srgrimes	fsck -p
5937Srgrimes	case $? in
6037Srgrimes	0)
6137Srgrimes		;;
6237Srgrimes	2)
6337Srgrimes		exit 1
6437Srgrimes		;;
6537Srgrimes	4)
6637Srgrimes		reboot
6737Srgrimes		echo "reboot failed... help!"
6837Srgrimes		exit 1
6937Srgrimes		;;
7037Srgrimes	8)
7137Srgrimes		echo "Automatic file system check failed... help!"
7237Srgrimes		exit 1
7337Srgrimes		;;
7437Srgrimes	12)
7537Srgrimes		echo "Reboot interrupted"
7637Srgrimes		exit 1
7737Srgrimes		;;
7837Srgrimes	130)
7937Srgrimes		# interrupt before catcher installed
8037Srgrimes		exit 1
8137Srgrimes		;;
8237Srgrimes	*)
8337Srgrimes		echo "Unknown error in reboot"
8437Srgrimes		exit 1
8537Srgrimes		;;
8637Srgrimes	esac
872164Sdgelse
882164Sdg	echo Skipping disk checks ...
8937Srgrimesfi
9037Srgrimes
9145222Scracauerset -T
9237Srgrimestrap "echo 'Reboot interrupted'; exit 1" 3
9337Srgrimes
9443197Sdillon# root normally must be read/write, but if this is a BOOTP NFS
9543197Sdillon# diskless boot it does not have to be.
9643197Sdillon#
971692Sphk
9850357Ssheldonhif [ "${root_rw_mount}" != "NO" ]; then
9943197Sdillon	mount -u -o rw /
10043803Sdillonfi
1018530Sdg
10243803Sdillonif [ $? != 0 ]; then
10343803Sdillon	echo "Filesystem mount failed, startup aborted"
10443803Sdillon	exit 1
10543803Sdillonfi
10643197Sdillon
10743803Sdillonumount -a >/dev/null 2>&1
10843197Sdillon
10950357Ssheldonhif [ "${early_nfs_mounts}" != "YES" ]; then
11043197Sdillon	mount -a -t nonfs
11143803Sdillonelse
11243803Sdillon	mount -a
1138530Sdgfi
11443803Sdillonif [ $? != 0 ]; then
11543803Sdillon	echo "Filesystem mount failed, startup aborted"
11643803Sdillon	exit 1
11743803Sdillonfi
11837Srgrimes
11943197Sdillon# Run custom disk mounting function here
12043197Sdillon#
12143197Sdillon
12250357Ssheldonhif [ -n "${diskless_mount}" ]; then
12350357Ssheldonh	if [ -f "${diskless_mount}" ]; then
12450357Ssheldonh		sh ${diskless_mount}
12543803Sdillon	fi
12643197Sdillonfi
12743197Sdillon
1284091Sacheadjkerntz -i
129872Sache
13038237Sbrianclean_var() {
13139384Sbrian	if [ ! -f /var/run/clean_var ]; then
13239384Sbrian		rm -rf /var/run/*
13339384Sbrian		rm -f /var/spool/lock/*
13439384Sbrian		rm -rf /var/spool/uucp/.Temp/*
13539384Sbrian		# Keep a copy of the boot messages around
13639384Sbrian		dmesg >/var/run/dmesg.boot
13739384Sbrian		# And an initial utmp file
13839384Sbrian		(cd /var/run && cp /dev/null utmp && chmod 644 utmp; )
13939384Sbrian		>/var/run/clean_var
14039384Sbrian	fi
14138237Sbrian}
14226450Sache
14338237Sbrianif [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then
14439384Sbrian	# network_pass1() *may* end up writing stuff to /var - we don't want to
14539384Sbrian	# remove it immediately afterwards - *nor* to we want to fail to clean
14639384Sbrian	# an nfs-mounted /var.
14739384Sbrian	clean_var
14838237Sbrianfi
14921197Sphk
15017767Sjkh# Add additional swapfile, if configured.
15150357Ssheldonhif [ "${swapfile}" != "NO" -a -w "${swapfile}" -a -b /dev/vn0b ]; then
15250357Ssheldonh	echo "Adding ${swapfile} as additional swap."
15350357Ssheldonh	vnconfig /dev/vn0b ${swapfile} && swapon /dev/vn0b
15417767Sjkhfi
15517767Sjkh
15645096Simp# set sysctl variables early as we can
15745096Simpif [ -f /etc/rc.sysctl ]; then
15845096Simp	. /etc/rc.sysctl
15945096Simpfi
16045096Simp
1611675Sache# configure serial devices
1627219Sjkhif [ -f /etc/rc.serial ]; then
1637293Sjkh	. /etc/rc.serial
1641675Sachefi
1651675Sache
16614596Snate# start up PC-card configuration
16714624Snateif [ -f /etc/rc.pccard ]; then
16814624Snate	. /etc/rc.pccard
16914596Snatefi
17014596Snate
17125184Sjkh# start up the initial network configuration.
17225184Sjkhif [ -f /etc/rc.network ]; then
17325184Sjkh	. /etc/rc.network	# We only need to do this once.
17425184Sjkh	network_pass1
1757460Sjkhfi
1767460Sjkh
17738456Sphkecho -n "Mounting NFS file systems"
17829319Sbrianmount -a -t nfs
17938456Sphkecho .
1807487Srgrimes
1817487Srgrimes# Whack the pty perms back into shape.
18249366Simpchflags 0 /dev/tty[pqrsPQRS]*
18320828Sjoergchmod 666 /dev/tty[pqrsPQRS]*
18444752Sdeschown root:wheel /dev/tty[pqrsPQRS]*
1857487Srgrimes
1867487Srgrimes# clean up left-over files
18738237Sbrianclean_var			# If it hasn't already been done
18838237Sbrianrm /var/run/clean_var
18938237Sbrian
19031192Ssteve#
19131192Ssteve# Clearing /tmp at boot-time seems to have a long tradition.  It doesn't
19231192Ssteve# help in any way for long-living systems, and it might accidentally
19331192Ssteve# clobber files you would rather like to have preserved after a crash
19431192Ssteve# (if not using mfs /tmp anyway).
19531192Ssteve#
19631192Ssteve# See also the example of another cleanup policy in /etc/periodic/daily.
19731192Ssteve#
19850357Ssheldonhif [ "${clear_tmp_enable}" = "YES" ]; then
19931192Ssteve	echo clearing /tmp
20031192Ssteve
20131192Ssteve	# prune quickly with one rm, then use find to clean up /tmp/[lq]*
20231192Ssteve	# (not needed with mfs /tmp, but doesn't hurt there...)
20331192Ssteve	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
20447661Sbrian	    find -d . ! -name . ! -name lost+found ! -name quota.user \
20547661Sbrian	    ! -name quota.group -exec rm -rf -- {} \;)
20631192Ssteve
20731192Sstevefi
20831192Ssteve
20938915Scracauer# Remove X lock files, since they will prevent you from restarting X11 
21038915Scracauer# after a system crash.
21138915Scracauerrm -f /tmp/.X*-lock /tmp/.X11-unix/*
21238915Scracauer
21342897Sjkh# snapshot any kernel -c changes back to disk here <someday>
21442897Sjkh# this has changed with ELF and /kernel.config.
2157487Srgrimes
21625339Sjkhecho -n 'additional daemons:'
2177487Srgrimes# start system logging and name service (named needs to start before syslogd
2187487Srgrimes# if you don't have a /etc/resolv.conf)
2197259Sjkh#
22050357Ssheldonhif [ "${syslogd_enable}" = "YES" ]; then
22124463Spst	# Transitional symlink (for the next couple of years :) until all
22224463Spst	# binaries had a chance to move towards /var/run/log.
22324463Spst	if [ ! -h /dev/log ] ; then
22424463Spst		# might complain for r/o root f/s
22524463Spst		ln -sf /var/run/log /dev/log
22624463Spst	fi
22724463Spst
22824463Spst	rm -f /var/run/log
22925339Sjkh	echo -n ' syslogd';		syslogd ${syslogd_flags}
23019226Sjoergfi
23125339Sjkhecho '.'
23225339Sjkh
23332340Sjoerg# enable dumpdev so that savecore can see it
23432340Sjoerg# /var/crash should be a directory or a symbolic link
23532340Sjoerg# to the crash directory if core dumps are to be saved.
23650357Ssheldonhif [ "${dumpdev}" != "NO" -a -e ${dumpdev} -a -d /var/crash ]; then
23732340Sjoerg	dumpon ${dumpdev}
23832340Sjoerg	echo -n checking for core dump...
23932340Sjoerg	savecore /var/crash
24032340Sjoergfi
24132340Sjoerg
24250357Ssheldonhif [ -n "${network_pass1_done}" ]; then
24339384Sbrian	network_pass2
244857Sdgfi
24537Srgrimes
24644818Sbillf# Enable/Check the quotas (must be after ypbind if using NIS)
24750357Ssheldonhif [ "${enable_quotas}" = "YES" ]; then
24844818Sbillf
24944818Sbillf    # Only check quotas if they have been previously enabled, and requested
25050357Ssheldonh    if [ "${check_quotas}" = "YES" ]; then
25144818Sbillf            echo -n 'checking quotas:'
25244818Sbillf            quotacheck -a
25344818Sbillf            echo ' done.'
25444818Sbillf    fi
25544818Sbillf
25644818Sbillf        echo -n 'enabling quotas:'
25744818Sbillf        quotaon -a
25844818Sbillf        echo ' done.'
2597238Sachefi
2607238Sache
26150357Ssheldonhif [ -n "${network_pass2_done}" ]; then
26239384Sbrian	network_pass3
2637477Sachefi
2647477Sache
2657238Sache
2667487Srgrimes# build ps databases
2677487Srgrimeskvm_mkdb 
2687487Srgrimesdev_mkdb
2697487Srgrimes
2707487Srgrimes# check the password temp/lock file
2717487Srgrimesif [ -f /etc/ptmp ]
2727487Srgrimesthen
2737487Srgrimes	logger -s -p auth.err \
2747487Srgrimes	"password file may be incorrect -- /etc/ptmp exists"
2757238Sachefi
2767238Sache
27750357Ssheldonhif [ "${accounting_enable}" = "YES" -a -d /var/account ]; then
27811992Sache	echo 'turning on accounting'
27911992Sache	if [ ! -e /var/account/acct ]; then
28025412Sjkh		touch /var/account/acct
28111992Sache	fi
28211992Sache	accton /var/account/acct
2837238Sachefi
2847238Sache
28527365Sjkh# Make shared lib searching a little faster.  Leave /usr/lib first if you
28627365Sjkh# add your own entries or you may come to grief.
28738512Sgpalmerif [ -x /sbin/ldconfig ]; then
28850357Ssheldonh	if [ "`/usr/bin/objformat`" = "elf" ]; then
28939329Sjdp		_LDC=/usr/lib
29050357Ssheldonh		for i in ${ldconfig_paths}; do
29150357Ssheldonh			if test -d ${i}; then
29250357Ssheldonh				_LDC="${_LDC} ${i}"
29339329Sjdp			fi
29439329Sjdp		done
29539329Sjdp		echo 'setting ELF ldconfig path:' ${_LDC}
29639329Sjdp		ldconfig -elf ${_LDC}
29739329Sjdp	fi
29839329Sjdp
29941648Sjb	# Legacy aout support for i386 only
30050357Ssheldonh	if [ "`sysctl -n hw.machine`" = "i386" ]; then
30143951Sjkh		# Default the a.out ldconfig path.
30241648Sjb		: ${ldconfig_paths_aout=${ldconfig_paths}}
30341648Sjb		_LDC=/usr/lib/aout
30450357Ssheldonh		for i in ${ldconfig_paths_aout}; do
30550357Ssheldonh			if test -d ${i}; then
30650357Ssheldonh				_LDC="${_LDC} ${i}"
30741648Sjb			fi
30841648Sjb		done
30941648Sjb		echo 'setting a.out ldconfig path:' ${_LDC}
31041648Sjb		ldconfig -aout ${_LDC}
31141648Sjb	fi
31238512Sgpalmerfi
3137296Sjkh
31417210Spst# Now start up miscellaneous daemons that don't belong anywhere else
31517210Spst#
31625339Sjkhecho -n starting standard daemons:
31750357Ssheldonhif [ "${inetd_enable}" != "NO" ]; then
31826727Spst	echo -n ' inetd';	inetd ${inetd_flags}
31926727Spstfi
32017210Spst
32150357Ssheldonhif [ "${cron_enable}" != "NO" ]; then
32226727Spst	echo -n ' cron';	cron
32326727Spstfi
32426727Spst
32550357Ssheldonhif [ "${lpd_enable}" = "YES" ]; then
32644668Sjfitz	echo -n ' printer';		${lpd_program} ${lpd_flags}
32717210Spstfi
32817210Spst
32950357Ssheldonhif [ "${sendmail_enable}" = "YES" -a -r /etc/sendmail.cf ]; then
33025530Sjkh	echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
33117161Spstfi
33217161Spst
33350357Ssheldonhif [ "${usbd_enable}" = "YES" ]; then
33442498Sn_hibma	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
33542498Sn_hibmafi
33642498Sn_hibma
33717161Spstecho '.'
33817161Spst
3397487Srgrimes# configure implementation specific stuff
3407487Srgrimesarch=`uname -m`
34125469Sandreasif [ -f /etc/rc.${arch} ]; then
34225469Sandreas	. /etc/rc.${arch}
34325469Sandreasfi
3447487Srgrimes
34516671Spst# Recover vi editor files.
34619314Spetervibackup=`echo /var/tmp/vi.recover/vi.*`
34750357Ssheldonhif [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
34816671Spst	echo 'Recovering vi editor sessions'
34950357Ssheldonh	for i in ${vibackup}; do
35019314Speter		# Only test files that are readable.
35150357Ssheldonh		if test ! -r ${i}; then
35219314Speter			continue
35319314Speter		fi
35419314Speter
35519314Speter		# Unmodified nvi editor backup files either have the
35619314Speter		# execute bit set or are zero length.  Delete them.
35750357Ssheldonh		if test -x ${i} -o ! -s ${i}; then
35850357Ssheldonh			rm -f ${i}
35919314Speter		fi
36016671Spst	done
36119314Speter
36219314Speter	# It is possible to get incomplete recovery files, if the editor
36319314Speter	# crashes at the right time.
36419314Speter	virecovery=`echo /var/tmp/vi.recover/recover.*`
36550357Ssheldonh	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
36650357Ssheldonh		for i in ${virecovery}; do
36719314Speter			# Only test files that are readable.
36850357Ssheldonh			if test ! -r ${i}; then
36919314Speter				continue
37019314Speter			fi
37119314Speter
37219314Speter			# Delete any recovery files that are zero length,
37319314Speter			# corrupted, or that have no corresponding backup file.
37419314Speter			# Else send mail to the user.
37550357Ssheldonh			recfile=`awk '/^X-vi-recover-path:/{print $2}' < ${i}`
37650357Ssheldonh			if test -n "${recfile}" -a -s "${recfile}"; then
37750357Ssheldonh				sendmail -t < ${i}
37819314Speter			else
37950357Ssheldonh				rm -f ${i}
38019314Speter			fi
38119314Speter		done
38219314Speter	fi
38316671Spstfi
38416671Spst
38537899Snectar# make a bounds file for msgs(1) if there isn't one already
38637899Snectarif [ ! -f /var/msgs/bounds ]; then
38737899Snectar	echo 0 > /var/msgs/bounds
38837899Snectarfi
38937899Snectar
39013071Sjkh# for each valid dir in $local_startup, search for init scripts matching *.sh
39150357Ssheldonhif [ "${local_startup}" != "NO" ]; then
39227365Sjkh	echo -n 'Local package initialization:'
39313071Sjkh	for dir in ${local_startup}; do
39413071Sjkh		[ -d ${dir} ] && for script in ${dir}/*.sh; do
39545244Scracauer			[ -x ${script} ] && \
39645244Scracauer				(set -T ; trap 'exit 1' 2 ; ${script} start)
39713071Sjkh		done
39810873Sjkh	done
39916391Sjkh	echo .
4007259Sjkhfi
40137Srgrimes
40250357Ssheldonhif [ "${update_motd}" != "NO" ]; then
40349451Speter	T=`mktemp /tmp/_motd.XXXXXX`
40449453Speter	if [ $? -eq 0 ]; then
40550357Ssheldonh		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
40650357Ssheldonh		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
40750357Ssheldonh		cmp -s ${T} /etc/motd || {
40850357Ssheldonh			cp ${T} /etc/motd
40949451Speter			chmod 644 /etc/motd
41049451Speter		}
41150357Ssheldonh		rm -f ${T}
41249451Speter	fi
41341704Sdillonfi
41441704Sdillon
41549452Speter# Run rc.devfs if present to customize devfs
41639162Ssos[ -f /etc/rc.devfs ] && sh /etc/rc.devfs
41739162Ssos
41841704Sdillon# Do traditional (but rather obsolete) rc.local file if it exists.  If you
41943951Sjkh# use this file and want to make it programmatic, source /etc/defaults/rc.conf
42043951Sjkh# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
42143951Sjkh# shown below.  Please do not put local extensions into /etc/rc itself.
42241704Sdillon# Use /etc/rc.local
42341704Sdillon#
42441704Sdillon# ---- rc.local  ----
42543951Sjkh#     if [ -f /etc/defaults/rc.conf ]; then
42643951Sjkh#             . /etc/defaults/rc.conf
42741704Sdillon#     fi
42843951Sjkh# 
42941704Sdillon#     ... additional startup conditionals ...
43043951Sjkh# ---- rc.local  ---- 
43143197Sdillon# 
43243797Sdillonif [ -f /etc/rc.local ]; then
43343197Sdillon	echo -n 'starting local daemons:'
43443797Sdillon        sh /etc/rc.local
43543197Sdillon	echo '.'
43641704Sdillonfi
43741704Sdillon
43837106Sjkoshy# Raise kernel security level.  This should be done only after `fsck' has
43937106Sjkoshy# repaired local file systems if you want the securelevel to be greater than 1.
44050357Ssheldonhif [ "${kern_securelevel_enable}" = "YES" -a "${kern_securelevel}" -ge 0 ]; 
44137106Sjkoshythen
44237106Sjkoshy	echo 'Raising kernel security level'
44337106Sjkoshy	sysctl -w kern.securelevel=${kern_securelevel}
44437106Sjkoshyfi
44537106Sjkoshy
44637Srgrimesdate
44737Srgrimesexit 0
448