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