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