rc revision 44668
140123Sdes#!/bin/sh
244668Sjfitz#	$Id: rc,v 1.179 1999/02/13 05:30:49 jkh Exp $
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
4843803Sdillonif [ -n "$vinum_drives" ]; then
4943454Sgrog	vinum read $vinum_drives
5042741Sgrogfi
5142741Sgrog
5243803Sdillonswapon -a
533843Sdg
5443803Sdillonif [ $1x = autobootx ]; then
5537Srgrimes	echo Automatic reboot in progress...
5637Srgrimes	fsck -p
5737Srgrimes	case $? in
5837Srgrimes	0)
5937Srgrimes		;;
6037Srgrimes	2)
6137Srgrimes		exit 1
6237Srgrimes		;;
6337Srgrimes	4)
6437Srgrimes		reboot
6537Srgrimes		echo "reboot failed... help!"
6637Srgrimes		exit 1
6737Srgrimes		;;
6837Srgrimes	8)
6937Srgrimes		echo "Automatic file system check failed... help!"
7037Srgrimes		exit 1
7137Srgrimes		;;
7237Srgrimes	12)
7337Srgrimes		echo "Reboot interrupted"
7437Srgrimes		exit 1
7537Srgrimes		;;
7637Srgrimes	130)
7737Srgrimes		# interrupt before catcher installed
7837Srgrimes		exit 1
7937Srgrimes		;;
8037Srgrimes	*)
8137Srgrimes		echo "Unknown error in reboot"
8237Srgrimes		exit 1
8337Srgrimes		;;
8437Srgrimes	esac
852164Sdgelse
862164Sdg	echo Skipping disk checks ...
8737Srgrimesfi
8837Srgrimes
8937Srgrimestrap "echo 'Reboot interrupted'; exit 1" 3
9037Srgrimes
9143197Sdillon# root normally must be read/write, but if this is a BOOTP NFS
9243197Sdillon# diskless boot it does not have to be.
9343197Sdillon#
941692Sphk
9543803Sdillonif [ "X$root_rw_mount" != "XNO" ]; then
9643197Sdillon	mount -u -o rw /
9743803Sdillonfi
988530Sdg
9943803Sdillonif [ $? != 0 ]; then
10043803Sdillon	echo "Filesystem mount failed, startup aborted"
10143803Sdillon	exit 1
10243803Sdillonfi
10343197Sdillon
10443803Sdillonumount -a >/dev/null 2>&1
10543197Sdillon
10643803Sdillonif [ "X$early_nfs_mounts" != "XYES" ]; then
10743197Sdillon	mount -a -t nonfs
10843803Sdillonelse
10943803Sdillon	mount -a
1108530Sdgfi
11143803Sdillonif [ $? != 0 ]; then
11243803Sdillon	echo "Filesystem mount failed, startup aborted"
11343803Sdillon	exit 1
11443803Sdillonfi
11537Srgrimes
11643197Sdillon# Run custom disk mounting function here
11743197Sdillon#
11843197Sdillon
11943803Sdillonif [ "X$diskless_mount" != "X" ]; then
12043803Sdillon	if [ -f $diskless_mount ]; then
12143803Sdillon		sh $diskless_mount
12243803Sdillon	fi
12343197Sdillonfi
12443197Sdillon
1254091Sacheadjkerntz -i
126872Sache
12738237Sbrianclean_var() {
12839384Sbrian	if [ ! -f /var/run/clean_var ]; then
12939384Sbrian		rm -rf /var/run/*
13039384Sbrian		rm -f /var/spool/lock/*
13139384Sbrian		rm -rf /var/spool/uucp/.Temp/*
13239384Sbrian		# Keep a copy of the boot messages around
13339384Sbrian		dmesg >/var/run/dmesg.boot
13439384Sbrian		# And an initial utmp file
13539384Sbrian		(cd /var/run && cp /dev/null utmp && chmod 644 utmp; )
13639384Sbrian		>/var/run/clean_var
13739384Sbrian	fi
13838237Sbrian}
13926450Sache
14038237Sbrianif [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then
14139384Sbrian	# network_pass1() *may* end up writing stuff to /var - we don't want to
14239384Sbrian	# remove it immediately afterwards - *nor* to we want to fail to clean
14339384Sbrian	# an nfs-mounted /var.
14439384Sbrian	clean_var
14538237Sbrianfi
14621197Sphk
14717767Sjkh# Add additional swapfile, if configured.
14819363Sjoergif [ "x$swapfile" != "xNO" -a -w "$swapfile" -a -b /dev/vn0b ]; then
14917767Sjkh	echo "Adding $swapfile as additional swap."
15025184Sjkh	vnconfig /dev/vn0b $swapfile && swapon /dev/vn0b
15117767Sjkhfi
15217767Sjkh
1531675Sache# configure serial devices
1547219Sjkhif [ -f /etc/rc.serial ]; then
1557293Sjkh	. /etc/rc.serial
1561675Sachefi
1571675Sache
15814596Snate# start up PC-card configuration
15914624Snateif [ -f /etc/rc.pccard ]; then
16014624Snate	. /etc/rc.pccard
16114596Snatefi
16214596Snate
16325184Sjkh# start up the initial network configuration.
16425184Sjkhif [ -f /etc/rc.network ]; then
16525184Sjkh	. /etc/rc.network	# We only need to do this once.
16625184Sjkh	network_pass1
1677460Sjkhfi
1687460Sjkh
16938456Sphkecho -n "Mounting NFS file systems"
17029319Sbrianmount -a -t nfs
17138456Sphkecho .
1727487Srgrimes
1737487Srgrimes# Whack the pty perms back into shape.
17420828Sjoergchmod 666 /dev/tty[pqrsPQRS]*
1757487Srgrimes
1767487Srgrimes# clean up left-over files
17738237Sbrianclean_var			# If it hasn't already been done
17838237Sbrianrm /var/run/clean_var
17938237Sbrian
18031192Ssteve#
18131192Ssteve# Clearing /tmp at boot-time seems to have a long tradition.  It doesn't
18231192Ssteve# help in any way for long-living systems, and it might accidentally
18331192Ssteve# clobber files you would rather like to have preserved after a crash
18431192Ssteve# (if not using mfs /tmp anyway).
18531192Ssteve#
18631192Ssteve# See also the example of another cleanup policy in /etc/periodic/daily.
18731192Ssteve#
18831192Ssteveif [ "X${clear_tmp_enable}" = X"YES" ]; then
18931192Ssteve	echo clearing /tmp
19031192Ssteve
19131192Ssteve	# prune quickly with one rm, then use find to clean up /tmp/[lq]*
19231192Ssteve	# (not needed with mfs /tmp, but doesn't hurt there...)
19331192Ssteve	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
19439384Sbrian		find -d . ! -name . ! -name lost+found ! -name quotas -exec rm -rf -- {} \;)
19531192Ssteve
19631192Sstevefi
19731192Ssteve
19838915Scracauer# Remove X lock files, since they will prevent you from restarting X11 
19938915Scracauer# after a system crash.
20038915Scracauerrm -f /tmp/.X*-lock /tmp/.X11-unix/*
20138915Scracauer
20242897Sjkh# snapshot any kernel -c changes back to disk here <someday>
20342897Sjkh# this has changed with ELF and /kernel.config.
2047487Srgrimes
20525339Sjkhecho -n 'additional daemons:'
2067487Srgrimes# start system logging and name service (named needs to start before syslogd
2077487Srgrimes# if you don't have a /etc/resolv.conf)
2087259Sjkh#
20925412Sjkhif [ "X${syslogd_enable}" = X"YES" ]; then
21024463Spst	# Transitional symlink (for the next couple of years :) until all
21124463Spst	# binaries had a chance to move towards /var/run/log.
21224463Spst	if [ ! -h /dev/log ] ; then
21324463Spst		# might complain for r/o root f/s
21424463Spst		ln -sf /var/run/log /dev/log
21524463Spst	fi
21624463Spst
21724463Spst	rm -f /var/run/log
21825339Sjkh	echo -n ' syslogd';		syslogd ${syslogd_flags}
21919226Sjoergfi
22025339Sjkhecho '.'
22125339Sjkh
22232340Sjoerg# enable dumpdev so that savecore can see it
22332340Sjoerg# /var/crash should be a directory or a symbolic link
22432340Sjoerg# to the crash directory if core dumps are to be saved.
22532340Sjoergif [ "X${dumpdev}" != X"NO" -a -e ${dumpdev} -a -d /var/crash ]; then
22632340Sjoerg	dumpon ${dumpdev}
22732340Sjoerg	echo -n checking for core dump...
22832340Sjoerg	savecore /var/crash
22932340Sjoergfi
23032340Sjoerg
23125184Sjkhif [ -n "$network_pass1_done" ]; then
23239384Sbrian	network_pass2
233857Sdgfi
23437Srgrimes
23518812Speter# Check the quotas (must be after ypbind if using NIS)
23618812Speterif [ "X${check_quotas}" = X"YES" ]; then
23718812Speter	echo -n 'checking quotas:'
23818812Speter	quotacheck -a
23918812Speter	echo ' done.'
24018812Speter	quotaon -a
2417238Sachefi
2427238Sache
24325184Sjkhif [ -n "$network_pass2_done" ]; then
24439384Sbrian	network_pass3
2457477Sachefi
2467477Sache
2477238Sache
2487487Srgrimes# build ps databases
2497487Srgrimeskvm_mkdb 
2507487Srgrimesdev_mkdb
2517487Srgrimes
2527487Srgrimes# check the password temp/lock file
2537487Srgrimesif [ -f /etc/ptmp ]
2547487Srgrimesthen
2557487Srgrimes	logger -s -p auth.err \
2567487Srgrimes	"password file may be incorrect -- /etc/ptmp exists"
2577238Sachefi
2587238Sache
25925184Sjkhif [ "X${accounting_enable}" = X"YES" -a -d /var/account ]; then
26011992Sache	echo 'turning on accounting'
26111992Sache	if [ ! -e /var/account/acct ]; then
26225412Sjkh		touch /var/account/acct
26311992Sache	fi
26411992Sache	accton /var/account/acct
2657238Sachefi
2667238Sache
26727365Sjkh# Make shared lib searching a little faster.  Leave /usr/lib first if you
26827365Sjkh# add your own entries or you may come to grief.
26938512Sgpalmerif [ -x /sbin/ldconfig ]; then
27039329Sjdp	if [ X"`/usr/bin/objformat`" = X"elf" ]; then
27139329Sjdp		_LDC=/usr/lib
27239329Sjdp		for i in $ldconfig_paths; do
27339329Sjdp			if test -d $i; then
27439329Sjdp				_LDC="${_LDC} $i"
27539329Sjdp			fi
27639329Sjdp		done
27739329Sjdp		echo 'setting ELF ldconfig path:' ${_LDC}
27839329Sjdp		ldconfig -elf ${_LDC}
27939329Sjdp	fi
28039329Sjdp
28141648Sjb	# Legacy aout support for i386 only
28241676Sjb	if [ X"`sysctl -n hw.machine`" = X"i386" ]; then
28343951Sjkh		# Default the a.out ldconfig path.
28441648Sjb		: ${ldconfig_paths_aout=${ldconfig_paths}}
28541648Sjb		_LDC=/usr/lib/aout
28641648Sjb		for i in $ldconfig_paths_aout; do
28741648Sjb			if test -d $i; then
28841648Sjb				_LDC="${_LDC} $i"
28941648Sjb			fi
29041648Sjb		done
29141648Sjb		echo 'setting a.out ldconfig path:' ${_LDC}
29241648Sjb		ldconfig -aout ${_LDC}
29341648Sjb	fi
29438512Sgpalmerfi
2957296Sjkh
29617210Spst# Now start up miscellaneous daemons that don't belong anywhere else
29717210Spst#
29825339Sjkhecho -n starting standard daemons:
29929343Sbrianif [ "X${inetd_enable}" != X"NO" ]; then
30026727Spst	echo -n ' inetd';	inetd ${inetd_flags}
30126727Spstfi
30217210Spst
30329343Sbrianif [ "X${cron_enable}" != X"NO" ]; then
30426727Spst	echo -n ' cron';	cron
30526727Spstfi
30626727Spst
30725184Sjkhif [ "X${lpd_enable}" = X"YES" ]; then
30844668Sjfitz	echo -n ' printer';		${lpd_program} ${lpd_flags}
30917210Spstfi
31017210Spst
31125184Sjkhif [ "X${sendmail_enable}" = X"YES" -a -r /etc/sendmail.cf ]; then
31225530Sjkh	echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
31317161Spstfi
31417161Spst
31542498Sn_hibmaif [ "X${usbd_enable}" = X"YES" ]; then
31642498Sn_hibma	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
31742498Sn_hibmafi
31842498Sn_hibma
31917161Spstecho '.'
32017161Spst
3217487Srgrimes# configure implementation specific stuff
3227487Srgrimesarch=`uname -m`
32325469Sandreasif [ -f /etc/rc.${arch} ]; then
32425469Sandreas	. /etc/rc.${arch}
32525469Sandreasfi
3267487Srgrimes
32716671Spst# Recover vi editor files.
32819314Spetervibackup=`echo /var/tmp/vi.recover/vi.*`
32919314Speterif [ "$vibackup" != '/var/tmp/vi.recover/vi.*' ]; then
33016671Spst	echo 'Recovering vi editor sessions'
33119314Speter	for i in $vibackup; do
33219314Speter		# Only test files that are readable.
33319314Speter		if test ! -r $i; then
33419314Speter			continue
33519314Speter		fi
33619314Speter
33719314Speter		# Unmodified nvi editor backup files either have the
33819314Speter		# execute bit set or are zero length.  Delete them.
33919314Speter		if test -x $i -o ! -s $i; then
34019314Speter			rm -f $i
34119314Speter		fi
34216671Spst	done
34319314Speter
34419314Speter	# It is possible to get incomplete recovery files, if the editor
34519314Speter	# crashes at the right time.
34619314Speter	virecovery=`echo /var/tmp/vi.recover/recover.*`
34719314Speter	if [ "$virecovery" != "/var/tmp/vi.recover/recover.*" ]; then
34819314Speter		for i in $virecovery; do
34919314Speter			# Only test files that are readable.
35019314Speter			if test ! -r $i; then
35119314Speter				continue
35219314Speter			fi
35319314Speter
35419314Speter			# Delete any recovery files that are zero length,
35519314Speter			# corrupted, or that have no corresponding backup file.
35619314Speter			# Else send mail to the user.
35719314Speter			recfile=`awk '/^X-vi-recover-path:/{print $2}' < $i`
35819314Speter			if test -n "$recfile" -a -s "$recfile"; then
35919314Speter				sendmail -t < $i
36019314Speter			else
36119314Speter				rm -f $i
36219314Speter			fi
36319314Speter		done
36419314Speter	fi
36516671Spstfi
36616671Spst
36737899Snectar# make a bounds file for msgs(1) if there isn't one already
36837899Snectarif [ ! -f /var/msgs/bounds ]; then
36937899Snectar	echo 0 > /var/msgs/bounds
37037899Snectarfi
37137899Snectar
37213071Sjkh# for each valid dir in $local_startup, search for init scripts matching *.sh
37313071Sjkhif [ "X${local_startup}" != X"NO" ]; then
37427365Sjkh	echo -n 'Local package initialization:'
37513071Sjkh	for dir in ${local_startup}; do
37613071Sjkh		[ -d ${dir} ] && for script in ${dir}/*.sh; do
37739824Scracauer			[ -x ${script} ] && \
37839824Scracauer				(trap 'exit 1' 2 ; ${script} start ; echo -n)
37913071Sjkh		done
38010873Sjkh	done
38116391Sjkh	echo .
3827259Sjkhfi
38337Srgrimes
38441704Sdillonif [ "X${update_motd}" != X"NO" ]; then
38541704Sdillon	T=/tmp/_motd
38641704Sdillon	rm -f $T
38741704Sdillon	uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > $T
38841704Sdillon	awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> $T
38941704Sdillon	cp $T /etc/motd
39041704Sdillon	chmod 644 /etc/motd
39141704Sdillon	rm -f $T
39241704Sdillonfi
39341704Sdillon
39439162Ssos# Run rc.devfs if present to customify devfs
39539162Ssos[ -f /etc/rc.devfs ] && sh /etc/rc.devfs
39639162Ssos
39741704Sdillon# Do traditional (but rather obsolete) rc.local file if it exists.  If you
39843951Sjkh# use this file and want to make it programmatic, source /etc/defaults/rc.conf
39943951Sjkh# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
40043951Sjkh# shown below.  Please do not put local extensions into /etc/rc itself.
40141704Sdillon# Use /etc/rc.local
40241704Sdillon#
40341704Sdillon# ---- rc.local  ----
40443951Sjkh#     if [ -f /etc/defaults/rc.conf ]; then
40543951Sjkh#             . /etc/defaults/rc.conf
40641704Sdillon#     fi
40743951Sjkh# 
40841704Sdillon#     ... additional startup conditionals ...
40943951Sjkh# ---- rc.local  ---- 
41043197Sdillon# 
41143797Sdillonif [ -f /etc/rc.local ]; then
41243197Sdillon	echo -n 'starting local daemons:'
41343797Sdillon        sh /etc/rc.local
41443197Sdillon	echo '.'
41541704Sdillonfi
41641704Sdillon
41737106Sjkoshy# Raise kernel security level.  This should be done only after `fsck' has
41837106Sjkoshy# repaired local file systems if you want the securelevel to be greater than 1.
41939030Sjraynardif [ "X${kern_securelevel_enable}" = X"YES" -a "${kern_securelevel}" -ge 0 ]; 
42037106Sjkoshythen
42137106Sjkoshy	echo 'Raising kernel security level'
42237106Sjkoshy	sysctl -w kern.securelevel=${kern_securelevel}
42337106Sjkoshyfi
42437106Sjkoshy
42537Srgrimesdate
42637Srgrimesexit 0
427