rc revision 64449
1#!/bin/sh
2# $FreeBSD: head/etc/rc 64449 2000-08-09 09:23:30Z 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" && 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#
382if [ -x /sbin/ldconfig ]; then
383	case `/usr/bin/objformat` in
384	elf)
385		_LDC=/usr/lib
386		for i in ${ldconfig_paths}; do
387			if [ -d "${i}" ]; then
388				_LDC="${_LDC} ${i}"
389			fi
390		done
391		echo 'setting ELF ldconfig path:' ${_LDC}
392		ldconfig -elf ${_LDC}
393		;;
394	esac
395
396	# Legacy aout support for i386 only
397	case `sysctl -n hw.machine` in
398	i386)
399		# Default the a.out ldconfig path.
400		: ${ldconfig_paths_aout=${ldconfig_paths}}
401		_LDC=/usr/lib/aout
402		for i in ${ldconfig_paths_aout}; do
403			if [ -d "${i}" ]; then
404				_LDC="${_LDC} ${i}"
405			fi
406		done
407		echo 'setting a.out ldconfig path:' ${_LDC}
408		ldconfig -aout ${_LDC}
409		;;
410	esac
411fi
412
413# Now start up miscellaneous daemons that don't belong anywhere else
414#
415echo -n starting standard daemons:
416case ${inetd_enable} in
417[Nn][Oo])
418	;;
419*)
420	echo -n ' inetd';	inetd ${inetd_flags}
421	;;
422esac
423
424case ${cron_enable} in
425[Nn][Oo])
426	;;
427*)
428	echo -n ' cron';	cron
429	;;
430esac
431
432case ${lpd_enable} in
433[Yy][Ee][Ss])
434	echo -n ' printer';	${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
435	;;
436esac
437
438case ${sendmail_enable} in
439[Yy][Ee][Ss])
440	if [ -r /etc/mail/sendmail.cf ]; then
441		echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
442	fi
443	;;
444esac
445
446case ${sshd_enable} in
447[Yy][Ee][Ss])
448	if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then
449		echo -n ' sshd';
450		${sshd_program:-/usr/sbin/sshd} ${sshd_flags}
451	fi
452	;;
453esac
454
455case ${usbd_enable} in
456[Yy][Ee][Ss])
457	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
458	;;
459esac
460
461echo '.'
462
463# Recover vi editor files.
464find /var/tmp/vi.recover ! -type f -a ! -type d -delete
465vibackup=`echo /var/tmp/vi.recover/vi.*`
466if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
467	echo 'Recovering vi editor sessions'
468	for i in /var/tmp/vi.recover/vi.*; do
469		# Only test files that are readable.
470		if [ ! -r "${i}" ]; then
471			continue
472		fi
473
474		# Unmodified nvi editor backup files either have the
475		# execute bit set or are zero length.  Delete them.
476		if [ -x "${i}" -o ! -s "${i}" ]; then
477			rm -f "${i}"
478		fi
479	done
480
481	# It is possible to get incomplete recovery files, if the editor
482	# crashes at the right time.
483	virecovery=`echo /var/tmp/vi.recover/recover.*`
484	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
485		for i in /var/tmp/vi.recover/recover.*; do
486			# Only test files that are readable.
487			if [ ! -r "${i}" ]; then
488				continue
489			fi
490
491			# Delete any recovery files that are zero length,
492			# corrupted, or that have no corresponding backup file.
493			# Else send mail to the user.
494			recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
495			if [ -n "${recfile}" -a -s "${recfile}" ]; then
496				sendmail -t < "${i}"
497			else
498				rm -f "${i}"
499			fi
500		done
501	fi
502fi
503
504# Make a bounds file for msgs(1) if there isn't one already
505# "Delete important files with symlink" security hole?
506#
507if [ -d /var/msgs -a ! -f /var/msgs/bounds ]; then
508	echo 0 > /var/msgs/bounds
509fi
510
511case ${update_motd} in
512[Nn][Oo] | '')
513	;;
514*)
515	if T=`mktemp /tmp/_motd.XXXXXX`; then
516		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
517		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
518		cmp -s ${T} /etc/motd || {
519			cp ${T} /etc/motd
520			chmod 644 /etc/motd
521		}
522		rm -f ${T}
523	fi
524	;;
525esac
526
527# Configure implementation specific stuff
528#
529arch=`uname -m`
530if [ -r /etc/rc.${arch} ]; then
531	. /etc/rc.${arch}
532fi
533
534# Run rc.devfs if readable to customize devfs
535#
536if [ -r /etc/rc.devfs ]; then
537	sh /etc/rc.devfs
538fi
539
540# Do traditional (but rather obsolete) rc.local file if it exists.  If you
541# use this file and want to make it programmatic, source /etc/defaults/rc.conf
542# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
543# shown below.  Please do not put local extensions into /etc/rc itself.
544# Use /etc/rc.local
545#
546# ---- rc.local ----
547#	if [ -r /etc/defaults/rc.conf ]; then
548#		. /etc/defaults/rc.conf
549#		source_rc_confs
550#	elif [ -r /etc/rc.conf ]; then
551#		. /etc/rc.conf
552#	fi
553#
554#	... additional startup conditionals ...
555# ---- rc.local ----
556#
557if [ -r /etc/rc.local ]; then
558	echo -n 'starting local daemons:'
559	sh /etc/rc.local
560	echo '.'
561fi
562
563# For each valid dir in $local_startup, search for init scripts matching *.sh
564#
565case ${local_startup} in
566[Nn][Oo] | '')
567	;;
568*)
569	echo -n 'Local package initialization:'
570	for dir in ${local_startup}; do
571		if [ -d "${dir}" ]; then
572			for script in ${dir}/*.sh; do
573				if [ -x "${script}" ]; then
574					(set -T
575					 trap 'exit 1' 2
576					 ${script} start)
577				fi
578			done
579		fi
580	done
581	echo .
582	;;
583esac
584
585if [ -n "${network_pass3_done}" ]; then
586	network_pass4
587fi
588
589# Raise kernel security level.  This should be done only after `fsck' has
590# repaired local file systems if you want the securelevel to be greater than 1.
591#
592case ${kern_securelevel_enable} in
593[Yy][Ee][Ss])
594	if [ "${kern_securelevel}" -ge 0 ]; then
595		echo 'Raising kernel security level'
596		sysctl -w kern.securelevel=${kern_securelevel}
597	fi
598	;;
599esac
600
601date
602exit 0
603