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