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