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