rc revision 67445
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 67445 2000-10-22 19:10:13Z phk $
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 2>/dev/null
207				( for i in /etc /var/run ; do
208					cd $i ; ls -al ; cat *
209				done ) | /bin/dd of=/dev/random bs=8k 2>/dev/null
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 device name databases if we are not using DEVFS
380#
381if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then
382	rm /var/run/dev.db
383else
384	dev_mkdb
385fi
386
387# Enable dumpdev so that savecore can see it.
388# /var/crash should be a directory or a symbolic link
389# to the crash directory if core dumps are to be saved.
390#
391case ${dumpdev} in
392[Nn][Oo] | '')
393	;;
394*)
395	if [ -e "${dumpdev}" -a -d /var/crash ]; then
396		dumpon -v ${dumpdev}
397		echo -n checking for core dump...
398		savecore /var/crash
399	fi
400	;;
401esac
402
403if [ -n "${network_pass1_done}" ]; then
404	network_pass2
405fi
406
407# Enable/Check the quotas (must be after ypbind if using NIS)
408#
409case ${enable_quotas} in
410[Yy][Ee][Ss])
411	case ${check_quotas} in
412	[Yy][Ee][Ss])
413		echo -n 'checking quotas:'
414		quotacheck -a
415		echo ' done.'
416		;;
417	esac
418
419	echo -n 'enabling quotas:'
420	quotaon -a
421	echo ' done.'
422	;;
423esac
424
425if [ -n "${network_pass2_done}" ]; then
426	network_pass3
427fi
428
429# Check the password temp/lock file
430#
431if [ -e /etc/ptmp ]; then
432	logger -s -p auth.err \
433	"password file may be incorrect -- /etc/ptmp exists"
434fi
435
436case ${accounting_enable} in
437[Yy][Ee][Ss])
438	if [ -d /var/account ]; then
439		echo 'turning on accounting'
440		if [ ! -e /var/account/acct ]; then
441			touch /var/account/acct
442		fi
443		accton /var/account/acct
444	fi
445	;;
446esac
447
448# Make shared lib searching a little faster.  Leave /usr/lib first if you
449# add your own entries or you may come to grief.
450#
451ldconfig="/sbin/ldconfig"
452case ${ldconfig_insecure} in
453[Yy][Ee][Ss])
454	ldconfig="${ldconfig} -i"
455	;;
456esac
457if [ -x /sbin/ldconfig ]; then
458	case `/usr/bin/objformat` in
459	elf)
460		_LDC=/usr/lib
461		for i in ${ldconfig_paths}; do
462			if [ -d "${i}" ]; then
463				_LDC="${_LDC} ${i}"
464			fi
465		done
466		echo 'setting ELF ldconfig path:' ${_LDC}
467		${ldconfig} -elf ${_LDC}
468		;;
469	esac
470
471	# Legacy aout support for i386 only
472	case `sysctl -n hw.machine` in
473	i386)
474		# Default the a.out ldconfig path.
475		: ${ldconfig_paths_aout=${ldconfig_paths}}
476		_LDC=/usr/lib/aout
477		for i in ${ldconfig_paths_aout}; do
478			if [ -d "${i}" ]; then
479				_LDC="${_LDC} ${i}"
480			fi
481		done
482		echo 'setting a.out ldconfig path:' ${_LDC}
483		${ldconfig} -aout ${_LDC}
484		;;
485	esac
486fi
487
488# Now start up miscellaneous daemons that don't belong anywhere else
489#
490echo -n starting standard daemons:
491case ${inetd_enable} in
492[Nn][Oo])
493	;;
494*)
495	echo -n ' inetd';	inetd ${inetd_flags}
496	;;
497esac
498
499case ${cron_enable} in
500[Nn][Oo])
501	;;
502*)
503	echo -n ' cron';	cron
504	;;
505esac
506
507case ${lpd_enable} in
508[Yy][Ee][Ss])
509	echo -n ' printer';	${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
510	;;
511esac
512
513case ${sendmail_enable} in
514[Yy][Ee][Ss])
515	if [ -r /etc/mail/sendmail.cf ]; then
516		echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
517	fi
518	;;
519esac
520
521case ${sshd_enable} in
522[Yy][Ee][Ss])
523	if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then
524		echo -n ' sshd';
525		${sshd_program:-/usr/sbin/sshd} ${sshd_flags}
526	fi
527	;;
528esac
529
530case ${usbd_enable} in
531[Yy][Ee][Ss])
532	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
533	;;
534esac
535
536echo '.'
537
538# Recover vi editor files.
539find /var/tmp/vi.recover ! -type f -a ! -type d -delete
540vibackup=`echo /var/tmp/vi.recover/vi.*`
541if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
542	echo 'Recovering vi editor sessions'
543	for i in /var/tmp/vi.recover/vi.*; do
544		# Only test files that are readable.
545		if [ ! -r "${i}" ]; then
546			continue
547		fi
548
549		# Unmodified nvi editor backup files either have the
550		# execute bit set or are zero length.  Delete them.
551		if [ -x "${i}" -o ! -s "${i}" ]; then
552			rm -f "${i}"
553		fi
554	done
555
556	# It is possible to get incomplete recovery files, if the editor
557	# crashes at the right time.
558	virecovery=`echo /var/tmp/vi.recover/recover.*`
559	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
560		for i in /var/tmp/vi.recover/recover.*; do
561			# Only test files that are readable.
562			if [ ! -r "${i}" ]; then
563				continue
564			fi
565
566			# Delete any recovery files that are zero length,
567			# corrupted, or that have no corresponding backup file.
568			# Else send mail to the user.
569			recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
570			if [ -n "${recfile}" -a -s "${recfile}" ]; then
571				sendmail -t < "${i}"
572			else
573				rm -f "${i}"
574			fi
575		done
576	fi
577fi
578
579# Make a bounds file for msgs(1) if there isn't one already
580# "Delete important files with symlink" security hole?
581#
582if [ -d /var/msgs -a ! -f /var/msgs/bounds ]; then
583	echo 0 > /var/msgs/bounds
584fi
585
586case ${update_motd} in
587[Nn][Oo] | '')
588	;;
589*)
590	if T=`mktemp /tmp/_motd.XXXXXX`; then
591		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
592		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
593		cmp -s ${T} /etc/motd || {
594			cp ${T} /etc/motd
595			chmod 644 /etc/motd
596		}
597		rm -f ${T}
598	fi
599	;;
600esac
601
602# Configure implementation specific stuff
603#
604arch=`uname -m`
605if [ -r /etc/rc.${arch} ]; then
606	. /etc/rc.${arch}
607fi
608
609# Run rc.devfs if readable to customize devfs
610#
611if [ -r /etc/rc.devfs ]; then
612	sh /etc/rc.devfs
613fi
614
615echo -n additional ABI support:
616
617# Start the Linux binary compatibility if requested.
618#
619case ${linux_enable} in
620[Yy][Ee][Ss])
621	echo -n ' linux'
622	if ! kldstat -v | grep -E 'linux(aout|elf)' > /dev/null; then
623		kldload linux > /dev/null 2>&1
624	fi
625	if [ -x /compat/linux/sbin/ldconfig ]; then
626		/compat/linux/sbin/ldconfig
627	fi
628	;;
629esac
630
631# Start the SysVR4 binary emulation if requested.
632#
633case ${svr4_enable} in
634[Yy][Ee][Ss])
635	echo -n ' svr4';	kldload svr4 > /dev/null 2>&1
636	;;
637esac
638
639echo .
640
641# Do traditional (but rather obsolete) rc.local file if it exists.  If you
642# use this file and want to make it programmatic, source /etc/defaults/rc.conf
643# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
644# shown below.  Please do not put local extensions into /etc/rc itself.
645# Use /etc/rc.local
646#
647# ---- rc.local ----
648#	if [ -r /etc/defaults/rc.conf ]; then
649#		. /etc/defaults/rc.conf
650#		source_rc_confs
651#	elif [ -r /etc/rc.conf ]; then
652#		. /etc/rc.conf
653#	fi
654#
655#	... additional startup conditionals ...
656# ---- rc.local ----
657#
658if [ -r /etc/rc.local ]; then
659	echo -n 'starting local daemons:'
660	sh /etc/rc.local
661	echo '.'
662fi
663
664# For each valid dir in $local_startup, search for init scripts matching *.sh
665#
666case ${local_startup} in
667[Nn][Oo] | '')
668	;;
669*)
670	echo -n 'Local package initialization:'
671	for dir in ${local_startup}; do
672		if [ -d "${dir}" ]; then
673			for script in ${dir}/*.sh; do
674				if [ -x "${script}" ]; then
675					(set -T
676					 trap 'exit 1' 2
677					 ${script} start)
678				fi
679			done
680		fi
681	done
682	echo .
683	;;
684esac
685
686if [ -n "${network_pass3_done}" ]; then
687	network_pass4
688fi
689
690# Raise kernel security level.  This should be done only after `fsck' has
691# repaired local file systems if you want the securelevel to be greater than 1.
692#
693case ${kern_securelevel_enable} in
694[Yy][Ee][Ss])
695	if [ "${kern_securelevel}" -ge 0 ]; then
696		echo 'Raising kernel security level'
697		sysctl -w kern.securelevel=${kern_securelevel}
698	fi
699	;;
700esac
701
702date
703exit 0
704