rc revision 64520
1#!/bin/sh
2# $FreeBSD: head/etc/rc 64520 2000-08-11 03:26:30Z jdp $
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" && for file in .* *
169			do
170				[ ."$file" = .. -o ."$file" = ... ] && continue
171				[ -d "$file" -a ! -L "$file" ] &&
172					purgedir "$file"
173				[ -f "$file" ] && rm -f -- "$file"
174			done
175		)
176		done
177	fi
178}
179
180clean_var() {
181	if [ ! -f /var/run/clean_var ]; then
182		rm -rf /var/run/*
183		purgedir /var/spool/lock
184		rm -rf /var/spool/uucp/.Temp/*
185		# Keep a copy of the boot messages around
186		dmesg >/var/run/dmesg.boot
187		# And an initial utmp file
188		(cd /var/run && cp /dev/null utmp && chmod 644 utmp;)
189		>/var/run/clean_var
190	fi
191}
192
193if [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then
194	# network_pass1() *may* end up writing stuff to /var - we don't want to
195	# remove it immediately afterwards - *nor* to we want to fail to clean
196	# an nfs-mounted /var.
197	clean_var
198fi
199
200# Add additional swapfile, if configured.
201#
202case ${swapfile} in
203[Nn][Oo] | '')
204	;;
205*)
206	if [ -w "${swapfile}" -a -c /dev/vn0b ]; then
207		echo "Adding ${swapfile} as additional swap."
208		vnconfig /dev/vn0b ${swapfile} && swapon /dev/vn0b
209	fi
210	;;
211esac
212
213# Set sysctl variables as early as we can
214#
215if [ -r /etc/rc.sysctl ]; then
216	. /etc/rc.sysctl
217fi
218
219# Configure serial devices
220#
221if [ -r /etc/rc.serial ]; then
222	. /etc/rc.serial
223fi
224
225# Start up PC-card configuration
226#
227if [ -r /etc/rc.pccard ]; then
228	. /etc/rc.pccard
229fi
230
231# Start up the initial network configuration.
232#
233if [ -r /etc/rc.network ]; then
234	. /etc/rc.network	# We only need to do this once.
235	network_pass1
236fi
237
238case ${ipv6_enable} in
239[Yy][Ee][Ss])
240	if [ -r /etc/rc.network6 ]; then
241		. /etc/rc.network6	# We only need to do this once also.
242		network6_pass1
243	fi
244	;;
245esac
246
247# Mount NFS filesystems if present in /etc/fstab
248case "`mount -d -a -t nfs`" in
249*mount_nfs*)
250	echo -n "Mounting NFS file systems"
251	mount -a -t nfs
252	echo .
253	;;
254esac
255
256# Whack the pty perms back into shape.
257#
258chflags 0 /dev/tty[pqrsPQRS]*
259chmod 666 /dev/tty[pqrsPQRS]*
260chown root:wheel /dev/tty[pqrsPQRS]*
261
262# Clean up left-over files
263#
264clean_var			# If it hasn't already been done
265rm /var/run/clean_var
266
267# Clearing /tmp at boot-time seems to have a long tradition.  It doesn't
268# help in any way for long-living systems, and it might accidentally
269# clobber files you would rather like to have preserved after a crash
270# (if not using mfs /tmp anyway).
271#
272# See also the example of another cleanup policy in /etc/periodic/daily.
273#
274case ${clear_tmp_enable} in
275[Yy][Ee][Ss])
276	echo clearing /tmp
277	# prune quickly with one rm, then use find to clean up /tmp/[lq]*
278	# (not needed with mfs /tmp, but doesn't hurt there...)
279	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
280		find -d . ! -name . ! -name lost+found ! -name quota.user \
281		! -name quota.group -exec rm -rf -- {} \;)
282	;;
283esac
284
285# Remove X lock files, since they will prevent you from restarting X11
286# after a system crash.
287#
288rm -f /tmp/.X*-lock /tmp/.X11-unix/*
289
290# Snapshot any kernel -c changes back to disk here <someday>.
291# This has changed with ELF and /kernel.config.
292
293echo -n 'additional daemons:'
294
295# Start system logging and name service.  Named needs to start before syslogd
296# if you don't have a /etc/resolv.conf.
297#
298case ${syslogd_enable} in
299[Yy][Ee][Ss])
300	# Transitional symlink (for the next couple of years :) until all
301	# binaries have had a chance to move towards /var/run/log.
302	if [ ! -h /dev/log ]; then
303		# might complain for r/o root f/s
304		ln -sf /var/run/log /dev/log
305	fi
306
307	rm -f /var/run/log
308	echo -n ' syslogd';	syslogd ${syslogd_flags}
309	;;
310esac
311
312echo '.'
313
314# Enable dumpdev so that savecore can see it.
315# /var/crash should be a directory or a symbolic link
316# to the crash directory if core dumps are to be saved.
317#
318case ${dumpdev} in
319[Nn][Oo] | '')
320	;;
321*)
322	if [ -e "${dumpdev}" -a -d /var/crash ]; then
323		dumpon ${dumpdev}
324		echo -n checking for core dump...
325		savecore /var/crash
326	fi
327	;;
328esac
329
330if [ -n "${network_pass1_done}" ]; then
331	network_pass2
332fi
333
334# Enable/Check the quotas (must be after ypbind if using NIS)
335#
336case ${enable_quotas} in
337[Yy][Ee][Ss])
338	case ${check_quotas} in
339	[Yy][Ee][Ss])
340		echo -n 'checking quotas:'
341		quotacheck -a
342		echo ' done.'
343		;;
344	esac
345
346	echo -n 'enabling quotas:'
347	quotaon -a
348	echo ' done.'
349	;;
350esac
351
352if [ -n "${network_pass2_done}" ]; then
353	network_pass3
354fi
355
356# Build ps databases
357#
358dev_mkdb
359
360# Check the password temp/lock file
361#
362if [ -e /etc/ptmp ]; then
363	logger -s -p auth.err \
364	"password file may be incorrect -- /etc/ptmp exists"
365fi
366
367case ${accounting_enable} in
368[Yy][Ee][Ss])
369	if [ -d /var/account ]; then
370		echo 'turning on accounting'
371		if [ ! -e /var/account/acct ]; then
372			touch /var/account/acct
373		fi
374		accton /var/account/acct
375	fi
376	;;
377esac
378
379# Make shared lib searching a little faster.  Leave /usr/lib first if you
380# add your own entries or you may come to grief.
381#
382ldconfig="/sbin/ldconfig"
383case ${ldconfig_insecure} in
384[Yy][Ee][Ss])
385	ldconfig="${ldconfig} -i"
386	;;
387esac
388if [ -x /sbin/ldconfig ]; then
389	case `/usr/bin/objformat` in
390	elf)
391		_LDC=/usr/lib
392		for i in ${ldconfig_paths}; do
393			if [ -d "${i}" ]; then
394				_LDC="${_LDC} ${i}"
395			fi
396		done
397		echo 'setting ELF ldconfig path:' ${_LDC}
398		${ldconfig} -elf ${_LDC}
399		;;
400	esac
401
402	# Legacy aout support for i386 only
403	case `sysctl -n hw.machine` in
404	i386)
405		# Default the a.out ldconfig path.
406		: ${ldconfig_paths_aout=${ldconfig_paths}}
407		_LDC=/usr/lib/aout
408		for i in ${ldconfig_paths_aout}; do
409			if [ -d "${i}" ]; then
410				_LDC="${_LDC} ${i}"
411			fi
412		done
413		echo 'setting a.out ldconfig path:' ${_LDC}
414		${ldconfig} -aout ${_LDC}
415		;;
416	esac
417fi
418
419# Now start up miscellaneous daemons that don't belong anywhere else
420#
421echo -n starting standard daemons:
422case ${inetd_enable} in
423[Nn][Oo])
424	;;
425*)
426	echo -n ' inetd';	inetd ${inetd_flags}
427	;;
428esac
429
430case ${cron_enable} in
431[Nn][Oo])
432	;;
433*)
434	echo -n ' cron';	cron
435	;;
436esac
437
438case ${lpd_enable} in
439[Yy][Ee][Ss])
440	echo -n ' printer';	${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
441	;;
442esac
443
444case ${sendmail_enable} in
445[Yy][Ee][Ss])
446	if [ -r /etc/mail/sendmail.cf ]; then
447		echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
448	fi
449	;;
450esac
451
452case ${sshd_enable} in
453[Yy][Ee][Ss])
454	if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then
455		echo -n ' sshd';
456		${sshd_program:-/usr/sbin/sshd} ${sshd_flags}
457	fi
458	;;
459esac
460
461case ${usbd_enable} in
462[Yy][Ee][Ss])
463	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
464	;;
465esac
466
467echo '.'
468
469# Recover vi editor files.
470find /var/tmp/vi.recover ! -type f -a ! -type d -delete
471vibackup=`echo /var/tmp/vi.recover/vi.*`
472if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
473	echo 'Recovering vi editor sessions'
474	for i in /var/tmp/vi.recover/vi.*; do
475		# Only test files that are readable.
476		if [ ! -r "${i}" ]; then
477			continue
478		fi
479
480		# Unmodified nvi editor backup files either have the
481		# execute bit set or are zero length.  Delete them.
482		if [ -x "${i}" -o ! -s "${i}" ]; then
483			rm -f "${i}"
484		fi
485	done
486
487	# It is possible to get incomplete recovery files, if the editor
488	# crashes at the right time.
489	virecovery=`echo /var/tmp/vi.recover/recover.*`
490	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
491		for i in /var/tmp/vi.recover/recover.*; do
492			# Only test files that are readable.
493			if [ ! -r "${i}" ]; then
494				continue
495			fi
496
497			# Delete any recovery files that are zero length,
498			# corrupted, or that have no corresponding backup file.
499			# Else send mail to the user.
500			recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
501			if [ -n "${recfile}" -a -s "${recfile}" ]; then
502				sendmail -t < "${i}"
503			else
504				rm -f "${i}"
505			fi
506		done
507	fi
508fi
509
510# Make a bounds file for msgs(1) if there isn't one already
511# "Delete important files with symlink" security hole?
512#
513if [ -d /var/msgs -a ! -f /var/msgs/bounds ]; then
514	echo 0 > /var/msgs/bounds
515fi
516
517case ${update_motd} in
518[Nn][Oo] | '')
519	;;
520*)
521	if T=`mktemp /tmp/_motd.XXXXXX`; then
522		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
523		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
524		cmp -s ${T} /etc/motd || {
525			cp ${T} /etc/motd
526			chmod 644 /etc/motd
527		}
528		rm -f ${T}
529	fi
530	;;
531esac
532
533# Configure implementation specific stuff
534#
535arch=`uname -m`
536if [ -r /etc/rc.${arch} ]; then
537	. /etc/rc.${arch}
538fi
539
540# Run rc.devfs if readable to customize devfs
541#
542if [ -r /etc/rc.devfs ]; then
543	sh /etc/rc.devfs
544fi
545
546# Do traditional (but rather obsolete) rc.local file if it exists.  If you
547# use this file and want to make it programmatic, source /etc/defaults/rc.conf
548# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
549# shown below.  Please do not put local extensions into /etc/rc itself.
550# Use /etc/rc.local
551#
552# ---- rc.local ----
553#	if [ -r /etc/defaults/rc.conf ]; then
554#		. /etc/defaults/rc.conf
555#		source_rc_confs
556#	elif [ -r /etc/rc.conf ]; then
557#		. /etc/rc.conf
558#	fi
559#
560#	... additional startup conditionals ...
561# ---- rc.local ----
562#
563if [ -r /etc/rc.local ]; then
564	echo -n 'starting local daemons:'
565	sh /etc/rc.local
566	echo '.'
567fi
568
569# For each valid dir in $local_startup, search for init scripts matching *.sh
570#
571case ${local_startup} in
572[Nn][Oo] | '')
573	;;
574*)
575	echo -n 'Local package initialization:'
576	for dir in ${local_startup}; do
577		if [ -d "${dir}" ]; then
578			for script in ${dir}/*.sh; do
579				if [ -x "${script}" ]; then
580					(set -T
581					 trap 'exit 1' 2
582					 ${script} start)
583				fi
584			done
585		fi
586	done
587	echo .
588	;;
589esac
590
591if [ -n "${network_pass3_done}" ]; then
592	network_pass4
593fi
594
595# Raise kernel security level.  This should be done only after `fsck' has
596# repaired local file systems if you want the securelevel to be greater than 1.
597#
598case ${kern_securelevel_enable} in
599[Yy][Ee][Ss])
600	if [ "${kern_securelevel}" -ge 0 ]; then
601		echo 'Raising kernel security level'
602		sysctl -w kern.securelevel=${kern_securelevel}
603	fi
604	;;
605esac
606
607date
608exit 0
609