rc revision 64400
1#!/bin/sh
2# $FreeBSD: head/etc/rc 64400 2000-08-08 13:30:27Z brian $
3#	From: @(#)rc	5.27 (Berkeley) 6/5/91
4
5# System startup script run by init on autoboot
6# or after single-user.
7# Output and error are redirected to console by init,
8# and the console is the controlling terminal.
9
10# Note that almost all of the user-configurable behavior is no longer in
11# this file, but rather in /etc/defaults/rc.conf.  Please check that file
12# first before contemplating any changes here.  If you do need to change
13# this file for some reason, we would like to know about it.
14
15stty status '^T'
16
17# Set shell to ignore SIGINT (2), but not children;
18# shell catches SIGQUIT (3) and returns to single user after fsck.
19#
20trap : 2
21trap : 3	# shouldn't be needed
22
23HOME=/
24PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
25export HOME PATH
26
27# BOOTP diskless boot.  We have to run the rc file early in order to
28# retarget various config files.
29#
30if [ -r /etc/rc.diskless1 ]; then
31	dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
32	if [ ${dlv:=0} != 0 ]; then
33		. /etc/rc.diskless1
34	fi
35fi
36
37# If there is a global system configuration file, suck it in.
38#
39if [ -r /etc/defaults/rc.conf ]; then
40	. /etc/defaults/rc.conf
41	source_rc_confs
42elif [ -r /etc/rc.conf ]; then
43	. /etc/rc.conf
44fi
45
46# Configure ccd devices.
47#
48if [ -r /etc/ccd.conf ]; then
49	ccdconfig -C
50fi
51
52case ${start_vinum} in
53[Yy][Ee][Ss])
54	vinum start
55	;;
56esac
57
58swapon -a
59
60case $1 in
61autoboot)
62	echo Automatic boot in progress...
63	fsck -p
64	case $? in
65	0)
66		;;
67	2)
68		exit 1
69		;;
70	4)
71		reboot
72		echo "reboot failed... help!"
73		exit 1
74		;;
75	8)
76		echo "Automatic file system check failed... help!"
77		exit 1
78		;;
79	12)
80		echo "Reboot interrupted"
81		exit 1
82		;;
83	130)
84		# interrupt before catcher installed
85		exit 1
86		;;
87	*)
88		echo "Unknown error in reboot"
89		exit 1
90		;;
91	esac
92	;;
93*)
94	echo Skipping disk checks ...
95	;;
96esac
97
98set -T
99trap "echo 'Reboot interrupted'; exit 1" 3
100
101# root normally must be read/write, but if this is a BOOTP NFS
102# diskless boot it does not have to be.
103#
104case ${root_rw_mount} in
105[Nn][Oo] | '')
106	;;
107*)
108	if ! mount -u -o rw / ; then
109		echo "Mounting root filesystem rw failed, startup aborted"
110		exit 1
111	fi
112	;;
113esac
114
115umount -a >/dev/null 2>&1
116
117# Mount everything except nfs filesystems.
118mount -a -t nonfs
119
120case $? in
1210)
122	;;
123*)
124	echo "Mounting /etc/fstab filesystems failed, startup aborted"
125	exit 1
126	;;
127esac
128
129# Run custom disk mounting function here
130#
131if [ -n "${diskless_mount}" -a -r "${diskless_mount}" ]; then
132		sh ${diskless_mount}
133fi
134
135# Recover some entropy so the rebooting /dev/random can reseed
136#
137case ${entropy_file} in
138[Nn][Oo] | '')
139	;;
140*)
141	if [ -f ${entropy_file} -a -r ${entropy_file} -a -w /dev/random ]; then
142		echo "Reading entropy file"
143		if ! cat ${entropy_file} 2>/dev/null >/dev/random; then
144			if ! kldstat -n randomdev >/dev/null 2>&1; then
145				kldload randomdev && \
146				    cat ${entropy_file} 2>/dev/null >/dev/random
147			fi
148		fi
149		if [ $? != 0 ]; then
150			echo "Writing entropy file to /dev/random failed"
151		fi
152		rm -f ${entropy_file}
153	fi
154	;;
155esac
156
157adjkerntz -i
158
159purgedir() {
160	local dir file
161
162	if [ $# -eq 0 ]; then
163		purgedir .
164	else
165		for dir
166		do
167		(
168			cd "$dir" && ls | while read file
169			do
170				[ -d "$file" ] && purgedir "$file"
171				[ -f "$file" ] && rm -f "$file"
172			done
173		)
174		done
175	fi
176}
177
178clean_var() {
179	if [ ! -f /var/run/clean_var ]; then
180		rm -rf /var/run/*
181		purgedir /var/spool/lock
182		rm -rf /var/spool/uucp/.Temp/*
183		# Keep a copy of the boot messages around
184		dmesg >/var/run/dmesg.boot
185		# And an initial utmp file
186		(cd /var/run && cp /dev/null utmp && chmod 644 utmp;)
187		>/var/run/clean_var
188	fi
189}
190
191if [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then
192	# network_pass1() *may* end up writing stuff to /var - we don't want to
193	# remove it immediately afterwards - *nor* to we want to fail to clean
194	# an nfs-mounted /var.
195	clean_var
196fi
197
198# Add additional swapfile, if configured.
199#
200case ${swapfile} in
201[Nn][Oo] | '')
202	;;
203*)
204	if [ -w "${swapfile}" -a -c /dev/vn0b ]; then
205		echo "Adding ${swapfile} as additional swap."
206		vnconfig /dev/vn0b ${swapfile} && swapon /dev/vn0b
207	fi
208	;;
209esac
210
211# Set sysctl variables as early as we can
212#
213if [ -r /etc/rc.sysctl ]; then
214	. /etc/rc.sysctl
215fi
216
217# Configure serial devices
218#
219if [ -r /etc/rc.serial ]; then
220	. /etc/rc.serial
221fi
222
223# Start up PC-card configuration
224#
225if [ -r /etc/rc.pccard ]; then
226	. /etc/rc.pccard
227fi
228
229# Start up the initial network configuration.
230#
231if [ -r /etc/rc.network ]; then
232	. /etc/rc.network	# We only need to do this once.
233	network_pass1
234fi
235
236case ${ipv6_enable} in
237[Yy][Ee][Ss])
238	if [ -r /etc/rc.network6 ]; then
239		. /etc/rc.network6	# We only need to do this once also.
240		network6_pass1
241	fi
242	;;
243esac
244
245# Mount NFS filesystems if present in /etc/fstab
246case "`mount -d -a -t nfs`" in
247*mount_nfs*)
248	echo -n "Mounting NFS file systems"
249	mount -a -t nfs
250	echo .
251	;;
252esac
253
254# Whack the pty perms back into shape.
255#
256chflags 0 /dev/tty[pqrsPQRS]*
257chmod 666 /dev/tty[pqrsPQRS]*
258chown root:wheel /dev/tty[pqrsPQRS]*
259
260# Clean up left-over files
261#
262clean_var			# If it hasn't already been done
263rm /var/run/clean_var
264
265# Clearing /tmp at boot-time seems to have a long tradition.  It doesn't
266# help in any way for long-living systems, and it might accidentally
267# clobber files you would rather like to have preserved after a crash
268# (if not using mfs /tmp anyway).
269#
270# See also the example of another cleanup policy in /etc/periodic/daily.
271#
272case ${clear_tmp_enable} in
273[Yy][Ee][Ss])
274	echo clearing /tmp
275	# prune quickly with one rm, then use find to clean up /tmp/[lq]*
276	# (not needed with mfs /tmp, but doesn't hurt there...)
277	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
278		find -d . ! -name . ! -name lost+found ! -name quota.user \
279		! -name quota.group -exec rm -rf -- {} \;)
280	;;
281esac
282
283# Remove X lock files, since they will prevent you from restarting X11
284# after a system crash.
285#
286rm -f /tmp/.X*-lock /tmp/.X11-unix/*
287
288# Snapshot any kernel -c changes back to disk here <someday>.
289# This has changed with ELF and /kernel.config.
290
291echo -n 'additional daemons:'
292
293# Start system logging and name service.  Named needs to start before syslogd
294# if you don't have a /etc/resolv.conf.
295#
296case ${syslogd_enable} in
297[Yy][Ee][Ss])
298	# Transitional symlink (for the next couple of years :) until all
299	# binaries have had a chance to move towards /var/run/log.
300	if [ ! -h /dev/log ]; then
301		# might complain for r/o root f/s
302		ln -sf /var/run/log /dev/log
303	fi
304
305	rm -f /var/run/log
306	echo -n ' syslogd';	syslogd ${syslogd_flags}
307	;;
308esac
309
310echo '.'
311
312# Enable dumpdev so that savecore can see it.
313# /var/crash should be a directory or a symbolic link
314# to the crash directory if core dumps are to be saved.
315#
316case ${dumpdev} in
317[Nn][Oo] | '')
318	;;
319*)
320	if [ -e "${dumpdev}" -a -d /var/crash ]; then
321		dumpon ${dumpdev}
322		echo -n checking for core dump...
323		savecore /var/crash
324	fi
325	;;
326esac
327
328if [ -n "${network_pass1_done}" ]; then
329	network_pass2
330fi
331
332# Enable/Check the quotas (must be after ypbind if using NIS)
333#
334case ${enable_quotas} in
335[Yy][Ee][Ss])
336	case ${check_quotas} in
337	[Yy][Ee][Ss])
338		echo -n 'checking quotas:'
339		quotacheck -a
340		echo ' done.'
341		;;
342	esac
343
344	echo -n 'enabling quotas:'
345	quotaon -a
346	echo ' done.'
347	;;
348esac
349
350if [ -n "${network_pass2_done}" ]; then
351	network_pass3
352fi
353
354# Build ps databases
355#
356dev_mkdb
357
358# Check the password temp/lock file
359#
360if [ -e /etc/ptmp ]; then
361	logger -s -p auth.err \
362	"password file may be incorrect -- /etc/ptmp exists"
363fi
364
365case ${accounting_enable} in
366[Yy][Ee][Ss])
367	if [ -d /var/account ]; then
368		echo 'turning on accounting'
369		if [ ! -e /var/account/acct ]; then
370			touch /var/account/acct
371		fi
372		accton /var/account/acct
373	fi
374	;;
375esac
376
377# Make shared lib searching a little faster.  Leave /usr/lib first if you
378# add your own entries or you may come to grief.
379#
380if [ -x /sbin/ldconfig ]; then
381	case `/usr/bin/objformat` in
382	elf)
383		_LDC=/usr/lib
384		for i in ${ldconfig_paths}; do
385			if [ -d "${i}" ]; then
386				_LDC="${_LDC} ${i}"
387			fi
388		done
389		echo 'setting ELF ldconfig path:' ${_LDC}
390		ldconfig -elf ${_LDC}
391		;;
392	esac
393
394	# Legacy aout support for i386 only
395	case `sysctl -n hw.machine` in
396	i386)
397		# Default the a.out ldconfig path.
398		: ${ldconfig_paths_aout=${ldconfig_paths}}
399		_LDC=/usr/lib/aout
400		for i in ${ldconfig_paths_aout}; do
401			if [ -d "${i}" ]; then
402				_LDC="${_LDC} ${i}"
403			fi
404		done
405		echo 'setting a.out ldconfig path:' ${_LDC}
406		ldconfig -aout ${_LDC}
407		;;
408	esac
409fi
410
411# Now start up miscellaneous daemons that don't belong anywhere else
412#
413echo -n starting standard daemons:
414case ${inetd_enable} in
415[Nn][Oo])
416	;;
417*)
418	echo -n ' inetd';	inetd ${inetd_flags}
419	;;
420esac
421
422case ${cron_enable} in
423[Nn][Oo])
424	;;
425*)
426	echo -n ' cron';	cron
427	;;
428esac
429
430case ${lpd_enable} in
431[Yy][Ee][Ss])
432	echo -n ' printer';	${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
433	;;
434esac
435
436case ${sendmail_enable} in
437[Yy][Ee][Ss])
438	if [ -r /etc/mail/sendmail.cf ]; then
439		echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
440	fi
441	;;
442esac
443
444case ${sshd_enable} in
445[Yy][Ee][Ss])
446	if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then
447		echo -n ' sshd';
448		${sshd_program:-/usr/sbin/sshd} ${sshd_flags}
449	fi
450	;;
451esac
452
453case ${usbd_enable} in
454[Yy][Ee][Ss])
455	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
456	;;
457esac
458
459echo '.'
460
461# Recover vi editor files.
462find /var/tmp/vi.recover ! -type f -a ! -type d -delete
463vibackup=`echo /var/tmp/vi.recover/vi.*`
464if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
465	echo 'Recovering vi editor sessions'
466	for i in /var/tmp/vi.recover/vi.*; do
467		# Only test files that are readable.
468		if [ ! -r "${i}" ]; then
469			continue
470		fi
471
472		# Unmodified nvi editor backup files either have the
473		# execute bit set or are zero length.  Delete them.
474		if [ -x "${i}" -o ! -s "${i}" ]; then
475			rm -f "${i}"
476		fi
477	done
478
479	# It is possible to get incomplete recovery files, if the editor
480	# crashes at the right time.
481	virecovery=`echo /var/tmp/vi.recover/recover.*`
482	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
483		for i in /var/tmp/vi.recover/recover.*; do
484			# Only test files that are readable.
485			if [ ! -r "${i}" ]; then
486				continue
487			fi
488
489			# Delete any recovery files that are zero length,
490			# corrupted, or that have no corresponding backup file.
491			# Else send mail to the user.
492			recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
493			if [ -n "${recfile}" -a -s "${recfile}" ]; then
494				sendmail -t < "${i}"
495			else
496				rm -f "${i}"
497			fi
498		done
499	fi
500fi
501
502# Make a bounds file for msgs(1) if there isn't one already
503# "Delete important files with symlink" security hole?
504#
505if [ -d /var/msgs -a ! -f /var/msgs/bounds ]; then
506	echo 0 > /var/msgs/bounds
507fi
508
509case ${update_motd} in
510[Nn][Oo] | '')
511	;;
512*)
513	if T=`mktemp /tmp/_motd.XXXXXX`; then
514		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
515		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
516		cmp -s ${T} /etc/motd || {
517			cp ${T} /etc/motd
518			chmod 644 /etc/motd
519		}
520		rm -f ${T}
521	fi
522	;;
523esac
524
525# Configure implementation specific stuff
526#
527arch=`uname -m`
528if [ -r /etc/rc.${arch} ]; then
529	. /etc/rc.${arch}
530fi
531
532# Run rc.devfs if readable to customize devfs
533#
534if [ -r /etc/rc.devfs ]; then
535	sh /etc/rc.devfs
536fi
537
538# Do traditional (but rather obsolete) rc.local file if it exists.  If you
539# use this file and want to make it programmatic, source /etc/defaults/rc.conf
540# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
541# shown below.  Please do not put local extensions into /etc/rc itself.
542# Use /etc/rc.local
543#
544# ---- rc.local ----
545#	if [ -r /etc/defaults/rc.conf ]; then
546#		. /etc/defaults/rc.conf
547#		source_rc_confs
548#	elif [ -r /etc/rc.conf ]; then
549#		. /etc/rc.conf
550#	fi
551#
552#	... additional startup conditionals ...
553# ---- rc.local ----
554#
555if [ -r /etc/rc.local ]; then
556	echo -n 'starting local daemons:'
557	sh /etc/rc.local
558	echo '.'
559fi
560
561# For each valid dir in $local_startup, search for init scripts matching *.sh
562#
563case ${local_startup} in
564[Nn][Oo] | '')
565	;;
566*)
567	echo -n 'Local package initialization:'
568	for dir in ${local_startup}; do
569		if [ -d "${dir}" ]; then
570			for script in ${dir}/*.sh; do
571				if [ -x "${script}" ]; then
572					(set -T
573					 trap 'exit 1' 2
574					 ${script} start)
575				fi
576			done
577		fi
578	done
579	echo .
580	;;
581esac
582
583if [ -n "${network_pass3_done}" ]; then
584	network_pass4
585fi
586
587# Raise kernel security level.  This should be done only after `fsck' has
588# repaired local file systems if you want the securelevel to be greater than 1.
589#
590case ${kern_securelevel_enable} in
591[Yy][Ee][Ss])
592	if [ "${kern_securelevel}" -ge 0 ]; then
593		echo 'Raising kernel security level'
594		sysctl -w kern.securelevel=${kern_securelevel}
595	fi
596	;;
597esac
598
599date
600exit 0
601