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