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