rc revision 53314
1139749Simp#!/bin/sh
237785Smsmith# $FreeBSD: head/etc/rc 53314 1999-11-17 22:38:02Z ache $
337785Smsmith#	From: @(#)rc	5.27 (Berkeley) 6/5/91
437785Smsmith
537785Smsmith# System startup script run by init on autoboot
637785Smsmith# or after single-user.
737785Smsmith# Output and error are redirected to console by init,
837785Smsmith# and the console is the controlling terminal.
937785Smsmith
1037785Smsmith# Note that almost all of the user-configurable behavior is no longer in
1137785Smsmith# this file, but rather in /etc/defaults/rc.conf.  Please check that file
1237785Smsmith# first before contemplating any changes here.  If you do need to change
1337785Smsmith# this file for some reason, we would like to know about it.
1437785Smsmith
1537785Smsmithstty status '^T'
1637785Smsmith
1737785Smsmith# Set shell to ignore SIGINT (2), but not children;
1837785Smsmith# shell catches SIGQUIT (3) and returns to single user after fsck.
1937785Smsmith#
2037785Smsmithtrap : 2
2137785Smsmithtrap : 3	# shouldn't be needed
2237785Smsmith
2337785SmsmithHOME=/
2437785SmsmithPATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
2537785Smsmithexport HOME PATH
2637785Smsmith
2737785Smsmith# BOOTP diskless boot.  We have to run the rc file early in order to
2837785Smsmith# retarget various config files.
29119418Sobrien#
30119418Sobrienif [ -r /etc/rc.diskless1 ]; then
31119418Sobrien	dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
3237785Smsmith	if [ ${dlv:=0} != 0 ]; then
3337785Smsmith		. /etc/rc.diskless1
3437785Smsmith	fi
3537785Smsmithfi
3637785Smsmith
3737785Smsmith# If there is a global system configuration file, suck it in.
3858816Simp#
3958816Simpif [ -r /etc/defaults/rc.conf ]; then
4058816Simp	. /etc/defaults/rc.conf
4137785Smsmithelif [ -r /etc/rc.conf ]; then
4237785Smsmith	. /etc/rc.conf
4337785Smsmithfi
4437785Smsmith
4558816Simp# Configure ccd devices.
4658816Simp#
4737785Smsmithif [ -r /etc/ccd.conf ]; then
4837785Smsmith	ccdconfig -C
4938592Smsmithfi
5037785Smsmith
5137785Smsmithcase ${start_vinum} in
5258816Simp[Yy][Ee][Ss])
5358816Simp	vinum start
5458816Simp	;;
5558816Simpesac
5658816Simp
5758816Simpswapon -a
5837785Smsmith
5937785Smsmithcase $1 in
6037785Smsmithautoboot)
6137785Smsmith	echo Automatic reboot in progress...
6237785Smsmith	fsck -p
6337785Smsmith	case $? in
6472940Simp	0)
6558816Simp		;;
6637785Smsmith	2)
6737785Smsmith		exit 1
6837785Smsmith		;;
6937785Smsmith	4)
7037785Smsmith		reboot
7137785Smsmith		echo "reboot failed... help!"
7237785Smsmith		exit 1
73122024Simp		;;
74122024Simp	8)
75122024Simp		echo "Automatic file system check failed... help!"
76122024Simp		exit 1
77122024Simp		;;
78122024Simp	12)
7938592Smsmith		echo "Reboot interrupted"
80122024Simp		exit 1
81122024Simp		;;
82122024Simp	130)
8358816Simp		# interrupt before catcher installed
8437785Smsmith		exit 1
8537785Smsmith		;;
8637785Smsmith	*)
8737785Smsmith		echo "Unknown error in reboot"
8837785Smsmith		exit 1
89122024Simp		;;
90122024Simp	esac
9137785Smsmith	;;
9237785Smsmith*)
9337785Smsmith	echo Skipping disk checks ...
9437785Smsmith	;;
9537785Smsmithesac
9637785Smsmith
9737785Smsmithset -T
9837785Smsmithtrap "echo 'Reboot interrupted'; exit 1" 3
9971316Simp
10037785Smsmith# root normally must be read/write, but if this is a BOOTP NFS
101122024Simp# diskless boot it does not have to be.
102122024Simp#
103122024Simpcase ${root_rw_mount} in
104122024Simp[Nn][Oo] | '')
105122024Simp	;;
106122024Simp*)
107122024Simp	if ! mount -u -o rw / ; then
108122024Simp		echo "Mounting root filesystem rw failed, startup aborted"
109122024Simp		exit 1
110122024Simp	fi
111122024Simp	;;
112122024Simpesac
113122024Simp
114122024Simpumount -a >/dev/null 2>&1
115122024Simp
116122024Simp# Where/how would this get set?
117140925Simp#
118122024Simpcase ${early_nfs_mounts} in
119122024Simp[Yy][Ee][Ss])
12037785Smsmith	mount -a
121122024Simp	;;
12237785Smsmith*)
12337785Smsmith	mount -a -t nonfs
12437785Smsmith	;;
12537785Smsmithesac
126122024Simp
12737785Smsmithcase $? in
12837785Smsmith0)
129122024Simp	;;
130122024Simp*)
131122024Simp	echo "Mounting /etc/fstab filesystems failed, startup aborted"
13237785Smsmith	exit 1
13372940Simp	;;
134122024Simpesac
13572940Simp
13672940Simp# Run custom disk mounting function here
13737785Smsmith#
13837785Smsmithif [ -n "${diskless_mount}" -a -r "${diskless_mount}" ]; then
13937785Smsmith		sh ${diskless_mount}
140122024Simpfi
14137785Smsmith
14237785Smsmithadjkerntz -i
14337785Smsmith
14437785Smsmithclean_var() {
14537785Smsmith	if [ ! -f /var/run/clean_var ]; then
14637785Smsmith		rm -rf /var/run/*
14772940Simp		rm -f /var/spool/lock/*
14837785Smsmith		rm -rf /var/spool/uucp/.Temp/*
14937785Smsmith		# Keep a copy of the boot messages around
15037785Smsmith		dmesg >/var/run/dmesg.boot
151122024Simp		# And an initial utmp file
15237785Smsmith		(cd /var/run && cp /dev/null utmp && chmod 644 utmp;)
153122024Simp		>/var/run/clean_var
154122024Simp	fi
15537785Smsmith}
156122024Simp
157122024Simpif [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then
15837785Smsmith	# network_pass1() *may* end up writing stuff to /var - we don't want to
15937785Smsmith	# remove it immediately afterwards - *nor* to we want to fail to clean
160122024Simp	# an nfs-mounted /var.
161122024Simp	clean_var
162122024Simpfi
163122024Simp
164122024Simp# Add additional swapfile, if configured.
165122024Simp#
16637785Smsmithcase ${swapfile} in
16737785Smsmith[Nn][Oo] | '')
16837785Smsmith	;;
16937785Smsmith*)
17037785Smsmith	if [ -w "${swapfile}" -a -b /dev/vn0b ]; then
171122024Simp		echo "Adding ${swapfile} as additional swap."
17237785Smsmith		vnconfig /dev/vn0b ${swapfile} && swapon /dev/vn0b
17337785Smsmith	fi
17437785Smsmith	;;
17537785Smsmithesac
17637785Smsmith
17737785Smsmith# Set sysctl variables as early as we can
17837785Smsmith#
17937785Smsmithif [ -r /etc/rc.sysctl ]; then
18037785Smsmith	. /etc/rc.sysctl
18137785Smsmithfi
18237785Smsmith
18337785Smsmith# Configure serial devices
18472940Simp#
18537785Smsmithif [ -r /etc/rc.serial ]; then
186122024Simp	. /etc/rc.serial
18737785Smsmithfi
18837785Smsmith
18937785Smsmith# Start up PC-card configuration
19037785Smsmith#
19137785Smsmithif [ -r /etc/rc.pccard ]; then
19237785Smsmith	. /etc/rc.pccard
193104252Sbrooksfi
19437785Smsmith
19572940Simp# Start up the initial network configuration.
19672940Simp#
19772940Simpif [ -r /etc/rc.network ]; then
19837785Smsmith	. /etc/rc.network	# We only need to do this once.
199104252Sbrooks	network_pass1
200104252Sbrooksfi
20137785Smsmith
20237785Smsmith# Retest for early_nfs here?
20337785Smsmith#
20437785Smsmithecho -n "Mounting NFS file systems"
20537785Smsmithmount -a -t nfs
20637785Smsmithecho .
20737785Smsmith
20837785Smsmith# Whack the pty perms back into shape.
20937785Smsmith#
21037785Smsmithchflags 0 /dev/tty[pqrsPQRS]*
21137785Smsmithchmod 666 /dev/tty[pqrsPQRS]*
21237785Smsmithchown root:wheel /dev/tty[pqrsPQRS]*
21337785Smsmith
21472940Simp# Clean up left-over files
21537785Smsmith#
21637785Smsmithclean_var			# If it hasn't already been done
21737785Smsmithrm /var/run/clean_var
21872940Simp
219104252Sbrooks# Clearing /tmp at boot-time seems to have a long tradition.  It doesn't
22037785Smsmith# help in any way for long-living systems, and it might accidentally
22137785Smsmith# clobber files you would rather like to have preserved after a crash
22237785Smsmith# (if not using mfs /tmp anyway).
22337785Smsmith#
22437785Smsmith# See also the example of another cleanup policy in /etc/periodic/daily.
22537785Smsmith#
22637785Smsmithcase ${clear_tmp_enable} in
22737785Smsmith[Yy][Ee][Ss])
22837785Smsmith	echo clearing /tmp
22937785Smsmith	# prune quickly with one rm, then use find to clean up /tmp/[lq]*
23037785Smsmith	# (not needed with mfs /tmp, but doesn't hurt there...)
23137785Smsmith	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
23237785Smsmith		find -d . ! -name . ! -name lost+found ! -name quota.user \
23337785Smsmith		! -name quota.group -exec rm -rf -- {} \;)
23437785Smsmith	;;
23537785Smsmithesac
23638305Smsmith
23738305Smsmith# Remove X lock files, since they will prevent you from restarting X11
23837785Smsmith# after a system crash.
239122024Simp#
24038305Smsmithrm -f /tmp/.X*-lock /tmp/.X11-unix/*
24138305Smsmith
24272940Simp# Snapshot any kernel -c changes back to disk here <someday>.
24372940Simp# This has changed with ELF and /kernel.config.
24472940Simp
24572940Simpecho -n 'additional daemons:'
24672940Simp
24772940Simp# Start system logging and name service.  Named needs to start before syslogd
24837785Smsmith# if you don't have a /etc/resolv.conf.
24937785Smsmith#
25037785Smsmithcase ${syslogd_enable} in
25172940Simp[Yy][Ee][Ss])
252122024Simp	# Transitional symlink (for the next couple of years :) until all
25338305Smsmith	# binaries have had a chance to move towards /var/run/log.
25437785Smsmith	if [ ! -h /dev/log ]; then
25538305Smsmith		# might complain for r/o root f/s
25637785Smsmith		ln -sf /var/run/log /dev/log
25737785Smsmith	fi
25837785Smsmith
25937785Smsmith	rm -f /var/run/log
26037785Smsmith	echo -n ' syslogd';	syslogd ${syslogd_flags}
261122024Simp	;;
262122024Simpesac
263122024Simp
26437785Smsmithecho '.'
26537785Smsmith
26637785Smsmith# Enable dumpdev so that savecore can see it.
26737785Smsmith# /var/crash should be a directory or a symbolic link
26837785Smsmith# to the crash directory if core dumps are to be saved.
26937785Smsmith#
27037785Smsmithcase ${dumpdev} in
27137785Smsmith[Nn][Oo] | '')
27237785Smsmith	;;
27337785Smsmith*)
27437785Smsmith	if [ -e "${dumpdev}" -a -d /var/crash ]; then
27537785Smsmith		dumpon ${dumpdev}
27672940Simp		echo -n checking for core dump...
27772940Simp		savecore /var/crash
27837785Smsmith	fi
27937785Smsmith	;;
280104252Sbrooksesac
28137785Smsmith
28237785Smsmithif [ -n "${network_pass1_done}" ]; then
28337785Smsmith	network_pass2
28437785Smsmithfi
28537785Smsmith
28637785Smsmith# Enable/Check the quotas (must be after ypbind if using NIS)
28737785Smsmith#
28837785Smsmithcase ${enable_quotas} in
28937785Smsmith[Yy][Ee][Ss])
29037785Smsmith	case ${check_quotas} in
29137785Smsmith	[Yy][Ee][Ss])
29237785Smsmith		echo -n 'checking quotas:'
29337785Smsmith		quotacheck -a
29472940Simp		echo ' done.'
29572940Simp		;;
29637785Smsmith	esac
29737785Smsmith
298104252Sbrooks	echo -n 'enabling quotas:'
29937785Smsmith	quotaon -a
30037785Smsmith	echo ' done.'
30137785Smsmith	;;
30237785Smsmithesac
30337785Smsmith
30471316Simpif [ -n "${network_pass2_done}" ]; then
30558816Simp	network_pass3
30637785Smsmithfi
30758816Simp
30858816Simp# Build ps databases
30958816Simp#
31058816Simpkvm_mkdb
31137785Smsmithdev_mkdb
312122024Simp
31358816Simp# Check the password temp/lock file
314122024Simp#
31537785Smsmithif [ -e /etc/ptmp ]; then
31637785Smsmith	logger -s -p auth.err \
31758816Simp	"password file may be incorrect -- /etc/ptmp exists"
31858816Simpfi
31958816Simp
32058816Simpcase ${accounting_enable} in
32172940Simp[Yy][Ee][Ss])
32258816Simp	if [ -d /var/account ]; then
32372940Simp		echo 'turning on accounting'
32437785Smsmith		if [ ! -e /var/account/acct ]; then
32537785Smsmith			touch /var/account/acct
32658816Simp		fi
32772940Simp		accton /var/account/acct
32872940Simp	fi
32972940Simp	;;
33072940Simpesac
33172940Simp
33258816Simp# Make shared lib searching a little faster.  Leave /usr/lib first if you
33337785Smsmith# add your own entries or you may come to grief.
33437785Smsmith#
33572940Simpif [ -x /sbin/ldconfig ]; then
33672940Simp	case `/usr/bin/objformat` in
33772940Simp	elf)
33872940Simp		_LDC=/usr/lib
33972940Simp		for i in ${ldconfig_paths}; do
34072940Simp			if [ -d "${i}" ]; then
34158816Simp				_LDC="${_LDC} ${i}"
34237785Smsmith			fi
34372940Simp		done
34437785Smsmith		echo 'setting ELF ldconfig path:' ${_LDC}
34537785Smsmith		ldconfig -elf ${_LDC}
34637785Smsmith		;;
34737785Smsmith	esac
34858816Simp
34937785Smsmith	# Legacy aout support for i386 only
35037785Smsmith	case `sysctl -n hw.machine` in
35137785Smsmith	i386)
35237785Smsmith		# Default the a.out ldconfig path.
35337785Smsmith		: ${ldconfig_paths_aout=${ldconfig_paths}}
35437785Smsmith		_LDC=/usr/lib/aout
35537785Smsmith		for i in ${ldconfig_paths_aout}; do
35637785Smsmith			if [ -d "${i}" ]; then
35737785Smsmith				_LDC="${_LDC} ${i}"
35837785Smsmith			fi
35937785Smsmith		done
36037785Smsmith		echo 'setting a.out ldconfig path:' ${_LDC}
36137785Smsmith		ldconfig -aout ${_LDC}
36237785Smsmith		;;
36337785Smsmith	esac
36437785Smsmithfi
36537785Smsmith
36637785Smsmith# Now start up miscellaneous daemons that don't belong anywhere else
36758816Simp#
36837785Smsmithecho -n starting standard daemons:
36958816Simpcase ${inetd_enable} in
37058816Simp[Nn][Oo])
37158816Simp	;;
37258816Simp*)
37358816Simp	echo -n ' inetd';	inetd ${inetd_flags}
37472940Simp	;;
37558816Simpesac
37637785Smsmith
37737785Smsmithcase ${cron_enable} in
37858816Simp[Nn][Oo])
37958816Simp	;;
38037785Smsmith*)
38137785Smsmith	echo -n ' cron';	cron
38258816Simp	;;
38358816Simpesac
38437785Smsmith
38537785Smsmithcase ${lpd_enable} in
38637785Smsmith[Yy][Ee][Ss])
38737785Smsmith	echo -n ' printer';	${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
38837785Smsmith	;;
38937785Smsmithesac
39037785Smsmith
39137785Smsmithcase ${sendmail_enable} in
39237785Smsmith[Yy][Ee][Ss])
39337785Smsmith	if [ -r /etc/sendmail.cf ]; then
39437785Smsmith		echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
39537785Smsmith	fi
39637785Smsmith	;;
39737785Smsmithesac
39837785Smsmith
39937785Smsmithcase ${usbd_enable} in
40037785Smsmith[Yy][Ee][Ss])
40137785Smsmith	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
40237785Smsmith	;;
40358816Simpesac
40458816Simp
40537785Smsmithecho '.'
40658816Simp
40758816Simp# Recover vi editor files.
40858816Simpfind /var/tmp/vi.recover ! -type f -a ! -type d -delete
40958816Simpvibackup=`echo /var/tmp/vi.recover/vi.*`
41058816Simpif [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
41158816Simp	echo 'Recovering vi editor sessions'
41258816Simp	for i in ${vibackup}; do
41358816Simp		# Only test files that are readable.
41458816Simp		if [ ! -r "${i}" ]; then
41558816Simp			continue
41658816Simp		fi
41758816Simp
41858816Simp		# Unmodified nvi editor backup files either have the
41958816Simp		# execute bit set or are zero length.  Delete them.
42058816Simp		if [ -x "${i}" -o ! -s "${i}" ]; then
42158816Simp			rm -f ${i}
42258816Simp		fi
42358816Simp	done
42458816Simp
42558816Simp	# It is possible to get incomplete recovery files, if the editor
42658816Simp	# crashes at the right time.
42758816Simp	virecovery=`echo /var/tmp/vi.recover/recover.*`
42858816Simp	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
42958816Simp		for i in ${virecovery}; do
43058816Simp			# Only test files that are readable.
43158816Simp			if [ ! -r "${i}" ]; then
43258816Simp				continue
43337785Smsmith			fi
43437785Smsmith
43558816Simp			# Delete any recovery files that are zero length,
43658816Simp			# corrupted, or that have no corresponding backup file.
43758816Simp			# Else send mail to the user.
43858816Simp			recfile=`awk '/^X-vi-recover-path:/{print $2}' < ${i}`
43958816Simp			if [ -n "${recfile}" -a -s "${recfile}" ]; then
44037785Smsmith				sendmail -t < ${i}
44137785Smsmith			else
44237785Smsmith				rm -f ${i}
44337785Smsmith			fi
44458816Simp		done
44537785Smsmith	fi
44637785Smsmithfi
44758816Simp
44858816Simp# Make a bounds file for msgs(1) if there isn't one already
44958816Simp# "Delete important files with symlink" security hole?
45058816Simp#
45158816Simpif [ -d /var/msgs -a ! -f /var/msgs/bounds ]; then
45258816Simp	echo 0 > /var/msgs/bounds
45358816Simpfi
45458816Simp
45558816Simpcase ${update_motd} in
45658816Simp[Nn][Oo] | '')
45758816Simp	;;
45858816Simp*)
45958816Simp	if T=`mktemp /tmp/_motd.XXXXXX`; then
46058816Simp		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
46137785Smsmith		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
46237785Smsmith		cmp -s ${T} /etc/motd || {
46337785Smsmith			cp ${T} /etc/motd
46458816Simp			chmod 644 /etc/motd
46537785Smsmith		}
46637785Smsmith		rm -f ${T}
46758816Simp	fi
46858816Simp	;;
46958816Simpesac
470140926Simp
471140926Simp# Configure implementation specific stuff
47237785Smsmith#
47358816Simparch=`uname -m`
47458816Simpif [ -r /etc/rc.${arch} ]; then
47537785Smsmith	. /etc/rc.${arch}
47637785Smsmithfi
47737785Smsmith
47837785Smsmith# Run rc.devfs if readable to customize devfs
47937785Smsmith#
48037785Smsmithif [ -r /etc/rc.devfs ]; then
48172940Simp	sh /etc/rc.devfs
48237785Smsmithfi
483104252Sbrooks
48437785Smsmith# Do traditional (but rather obsolete) rc.local file if it exists.  If you
48537785Smsmith# use this file and want to make it programmatic, source /etc/defaults/rc.conf
48637785Smsmith# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
48737785Smsmith# shown below.  Please do not put local extensions into /etc/rc itself.
48837785Smsmith# Use /etc/rc.local
48958816Simp#
49037785Smsmith# ---- rc.local ----
49137785Smsmith#	if [ -r /etc/defaults/rc.conf ]; then
49237785Smsmith#		. /etc/defaults/rc.conf
49337785Smsmith#	fi
49437785Smsmith#
49558816Simp#	... additional startup conditionals ...
49637785Smsmith# ---- rc.local ----
49737785Smsmith#
49837785Smsmithif [ -r /etc/rc.local ]; then
49937785Smsmith	echo -n 'starting local daemons:'
50037785Smsmith	sh /etc/rc.local
50137785Smsmith	echo '.'
50237785Smsmithfi
50337785Smsmith
50458816Simp# For each valid dir in $local_startup, search for init scripts matching *.sh
50537785Smsmith#
50637785Smsmithcase ${local_startup} in
50737785Smsmith[Nn][Oo] | '')
50858816Simp	;;
50958816Simp*)
51058816Simp	echo -n 'Local package initialization:'
51158816Simp	for dir in ${local_startup}; do
51258816Simp		if [ -d "${dir}" ]; then
51358816Simp			for script in ${dir}/*.sh; do
51458816Simp				if [ -x "${script}" ]; then
51558816Simp					(set -T
51658816Simp					 trap 'exit 1' 2
51758816Simp					 ${script} start)
51858816Simp				fi
51958816Simp			done
52058816Simp		fi
52158816Simp	done
52258816Simp	echo .
52358816Simp	;;
52458816Simpesac
52558816Simp
52658816Simpif [ -n "${network_pass3_done}" ]; then
52758816Simp	network_pass4
52858816Simpfi
52958816Simp
53058816Simp# Raise kernel security level.  This should be done only after `fsck' has
53158816Simp# repaired local file systems if you want the securelevel to be greater than 1.
53258816Simp#
53358816Simpcase ${kern_securelevel_enable} in
53458816Simp[Yy][Ee][Ss])
53558816Simp	if [ "${kern_securelevel}" -ge 0 ]; then
53658816Simp		echo 'Raising kernel security level'
53758816Simp		sysctl -w kern.securelevel=${kern_securelevel}
53858816Simp	fi
53958816Simp	;;
54058816Simpesac
54158816Simp
54258816Simpdate
54358816Simpexit 0
54458816Simp