rc revision 61065
1#!/bin/sh
2# $FreeBSD: head/etc/rc 61065 2000-05-29 12:31:26Z asmodai $
3#	From: @(#)rc	5.27 (Berkeley) 6/5/91
4
5# System startup script run by init on autoboot
6# or after single-user.
7# Output and error are redirected to console by init,
8# and the console is the controlling terminal.
9
10# Note that almost all of the user-configurable behavior is no longer in
11# this file, but rather in /etc/defaults/rc.conf.  Please check that file
12# first before contemplating any changes here.  If you do need to change
13# this file for some reason, we would like to know about it.
14
15stty status '^T'
16
17# Set shell to ignore SIGINT (2), but not children;
18# shell catches SIGQUIT (3) and returns to single user after fsck.
19#
20trap : 2
21trap : 3	# shouldn't be needed
22
23HOME=/
24PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
25export HOME PATH
26
27# BOOTP diskless boot.  We have to run the rc file early in order to
28# retarget various config files.
29#
30if [ -r /etc/rc.diskless1 ]; then
31	dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
32	if [ ${dlv:=0} != 0 ]; then
33		. /etc/rc.diskless1
34	fi
35fi
36
37# If there is a global system configuration file, suck it in.
38#
39if [ -r /etc/defaults/rc.conf ]; then
40	. /etc/defaults/rc.conf
41	source_rc_confs
42elif [ -r /etc/rc.conf ]; then
43	. /etc/rc.conf
44fi
45
46# Configure ccd devices.
47#
48if [ -r /etc/ccd.conf ]; then
49	ccdconfig -C
50fi
51
52case ${start_vinum} in
53[Yy][Ee][Ss])
54	vinum start
55	;;
56esac
57
58swapon -a
59
60case $1 in
61autoboot)
62	echo Automatic boot in progress...
63	fsck -p
64	case $? in
65	0)
66		;;
67	2)
68		exit 1
69		;;
70	4)
71		reboot
72		echo "reboot failed... help!"
73		exit 1
74		;;
75	8)
76		echo "Automatic file system check failed... help!"
77		exit 1
78		;;
79	12)
80		echo "Reboot interrupted"
81		exit 1
82		;;
83	130)
84		# interrupt before catcher installed
85		exit 1
86		;;
87	*)
88		echo "Unknown error in reboot"
89		exit 1
90		;;
91	esac
92	;;
93*)
94	echo Skipping disk checks ...
95	;;
96esac
97
98set -T
99trap "echo 'Reboot interrupted'; exit 1" 3
100
101# root normally must be read/write, but if this is a BOOTP NFS
102# diskless boot it does not have to be.
103#
104case ${root_rw_mount} in
105[Nn][Oo] | '')
106	;;
107*)
108	if ! mount -u -o rw / ; then
109		echo "Mounting root filesystem rw failed, startup aborted"
110		exit 1
111	fi
112	;;
113esac
114
115umount -a >/dev/null 2>&1
116
117# Mount everything except nfs filesystems.
118mount -a -t nonfs
119
120case $? in
1210)
122	;;
123*)
124	echo "Mounting /etc/fstab filesystems failed, startup aborted"
125	exit 1
126	;;
127esac
128
129# Run custom disk mounting function here
130#
131if [ -n "${diskless_mount}" -a -r "${diskless_mount}" ]; then
132		sh ${diskless_mount}
133fi
134
135adjkerntz -i
136
137clean_var() {
138	if [ ! -f /var/run/clean_var ]; then
139		rm -rf /var/run/*
140		rm -f /var/spool/lock/*
141		rm -rf /var/spool/uucp/.Temp/*
142		# Keep a copy of the boot messages around
143		dmesg >/var/run/dmesg.boot
144		# And an initial utmp file
145		(cd /var/run && cp /dev/null utmp && chmod 644 utmp;)
146		>/var/run/clean_var
147	fi
148}
149
150if [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then
151	# network_pass1() *may* end up writing stuff to /var - we don't want to
152	# remove it immediately afterwards - *nor* to we want to fail to clean
153	# an nfs-mounted /var.
154	clean_var
155fi
156
157# Add additional swapfile, if configured.
158#
159case ${swapfile} in
160[Nn][Oo] | '')
161	;;
162*)
163	if [ -w "${swapfile}" -a -c /dev/vn0b ]; then
164		echo "Adding ${swapfile} as additional swap."
165		vnconfig /dev/vn0b ${swapfile} && swapon /dev/vn0b
166	fi
167	;;
168esac
169
170# Set sysctl variables as early as we can
171#
172if [ -r /etc/rc.sysctl ]; then
173	. /etc/rc.sysctl
174fi
175
176# Configure serial devices
177#
178if [ -r /etc/rc.serial ]; then
179	. /etc/rc.serial
180fi
181
182# Start up PC-card configuration
183#
184if [ -r /etc/rc.pccard ]; then
185	. /etc/rc.pccard
186fi
187
188# Start up the initial network configuration.
189#
190if [ -r /etc/rc.network ]; then
191	. /etc/rc.network	# We only need to do this once.
192	network_pass1
193fi
194
195case ${ipv6_enable} in
196[Yy][Ee][Ss])
197	if [ -r /etc/rc.network6 ]; then
198		. /etc/rc.network6	# We only need to do this once also.
199		network6_pass1
200	fi
201	;;
202esac
203
204# Mount NFS filesystems if present in /etc/fstab
205case "`mount -d -a -t nfs`" in
206*mount_nfs*)
207	echo -n "Mounting NFS file systems"
208	mount -a -t nfs
209	echo .
210	;;
211esac
212
213# Whack the pty perms back into shape.
214#
215chflags 0 /dev/tty[pqrsPQRS]*
216chmod 666 /dev/tty[pqrsPQRS]*
217chown root:wheel /dev/tty[pqrsPQRS]*
218
219# Clean up left-over files
220#
221clean_var			# If it hasn't already been done
222rm /var/run/clean_var
223
224# Clearing /tmp at boot-time seems to have a long tradition.  It doesn't
225# help in any way for long-living systems, and it might accidentally
226# clobber files you would rather like to have preserved after a crash
227# (if not using mfs /tmp anyway).
228#
229# See also the example of another cleanup policy in /etc/periodic/daily.
230#
231case ${clear_tmp_enable} in
232[Yy][Ee][Ss])
233	echo clearing /tmp
234	# prune quickly with one rm, then use find to clean up /tmp/[lq]*
235	# (not needed with mfs /tmp, but doesn't hurt there...)
236	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
237		find -d . ! -name . ! -name lost+found ! -name quota.user \
238		! -name quota.group -exec rm -rf -- {} \;)
239	;;
240esac
241
242# Remove X lock files, since they will prevent you from restarting X11
243# after a system crash.
244#
245rm -f /tmp/.X*-lock /tmp/.X11-unix/*
246
247# Snapshot any kernel -c changes back to disk here <someday>.
248# This has changed with ELF and /kernel.config.
249
250echo -n 'additional daemons:'
251
252# Start system logging and name service.  Named needs to start before syslogd
253# if you don't have a /etc/resolv.conf.
254#
255case ${syslogd_enable} in
256[Yy][Ee][Ss])
257	# Transitional symlink (for the next couple of years :) until all
258	# binaries have had a chance to move towards /var/run/log.
259	if [ ! -h /dev/log ]; then
260		# might complain for r/o root f/s
261		ln -sf /var/run/log /dev/log
262	fi
263
264	rm -f /var/run/log
265	echo -n ' syslogd';	syslogd ${syslogd_flags}
266	;;
267esac
268
269echo '.'
270
271# Enable dumpdev so that savecore can see it.
272# /var/crash should be a directory or a symbolic link
273# to the crash directory if core dumps are to be saved.
274#
275case ${dumpdev} in
276[Nn][Oo] | '')
277	;;
278*)
279	if [ -e "${dumpdev}" -a -d /var/crash ]; then
280		dumpon ${dumpdev}
281		echo -n checking for core dump...
282		savecore /var/crash
283	fi
284	;;
285esac
286
287if [ -n "${network_pass1_done}" ]; then
288	network_pass2
289fi
290
291# Enable/Check the quotas (must be after ypbind if using NIS)
292#
293case ${enable_quotas} in
294[Yy][Ee][Ss])
295	case ${check_quotas} in
296	[Yy][Ee][Ss])
297		echo -n 'checking quotas:'
298		quotacheck -a
299		echo ' done.'
300		;;
301	esac
302
303	echo -n 'enabling quotas:'
304	quotaon -a
305	echo ' done.'
306	;;
307esac
308
309if [ -n "${network_pass2_done}" ]; then
310	network_pass3
311fi
312
313# Build ps databases
314#
315dev_mkdb
316
317# Check the password temp/lock file
318#
319if [ -e /etc/ptmp ]; then
320	logger -s -p auth.err \
321	"password file may be incorrect -- /etc/ptmp exists"
322fi
323
324case ${accounting_enable} in
325[Yy][Ee][Ss])
326	if [ -d /var/account ]; then
327		echo 'turning on accounting'
328		if [ ! -e /var/account/acct ]; then
329			touch /var/account/acct
330		fi
331		accton /var/account/acct
332	fi
333	;;
334esac
335
336# Make shared lib searching a little faster.  Leave /usr/lib first if you
337# add your own entries or you may come to grief.
338#
339if [ -x /sbin/ldconfig ]; then
340	case `/usr/bin/objformat` in
341	elf)
342		_LDC=/usr/lib
343		for i in ${ldconfig_paths}; do
344			if [ -d "${i}" ]; then
345				_LDC="${_LDC} ${i}"
346			fi
347		done
348		echo 'setting ELF ldconfig path:' ${_LDC}
349		ldconfig -elf ${_LDC}
350		;;
351	esac
352
353	# Legacy aout support for i386 only
354	case `sysctl -n hw.machine` in
355	i386)
356		# Default the a.out ldconfig path.
357		: ${ldconfig_paths_aout=${ldconfig_paths}}
358		_LDC=/usr/lib/aout
359		for i in ${ldconfig_paths_aout}; do
360			if [ -d "${i}" ]; then
361				_LDC="${_LDC} ${i}"
362			fi
363		done
364		echo 'setting a.out ldconfig path:' ${_LDC}
365		ldconfig -aout ${_LDC}
366		;;
367	esac
368fi
369
370# Now start up miscellaneous daemons that don't belong anywhere else
371#
372echo -n starting standard daemons:
373case ${inetd_enable} in
374[Nn][Oo])
375	;;
376*)
377	echo -n ' inetd';	inetd ${inetd_flags}
378	;;
379esac
380
381case ${cron_enable} in
382[Nn][Oo])
383	;;
384*)
385	echo -n ' cron';	cron
386	;;
387esac
388
389case ${lpd_enable} in
390[Yy][Ee][Ss])
391	echo -n ' printer';	${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
392	;;
393esac
394
395case ${sendmail_enable} in
396[Yy][Ee][Ss])
397	if [ -r /etc/mail/sendmail.cf ]; then
398		echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
399	fi
400	;;
401esac
402
403case ${sshd_enable} in
404[Yy][Ee][Ss])
405	if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then
406		echo -n ' sshd';
407		${sshd_program:-/usr/sbin/sshd} ${sshd_flags}
408	fi
409	;;
410esac
411
412case ${usbd_enable} in
413[Yy][Ee][Ss])
414	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
415	;;
416esac
417
418echo '.'
419
420# Recover vi editor files.
421find /var/tmp/vi.recover ! -type f -a ! -type d -delete
422vibackup=`echo /var/tmp/vi.recover/vi.*`
423if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
424	echo 'Recovering vi editor sessions'
425	for i in /var/tmp/vi.recover/vi.*; do
426		# Only test files that are readable.
427		if [ ! -r "${i}" ]; then
428			continue
429		fi
430
431		# Unmodified nvi editor backup files either have the
432		# execute bit set or are zero length.  Delete them.
433		if [ -x "${i}" -o ! -s "${i}" ]; then
434			rm -f "${i}"
435		fi
436	done
437
438	# It is possible to get incomplete recovery files, if the editor
439	# crashes at the right time.
440	virecovery=`echo /var/tmp/vi.recover/recover.*`
441	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
442		for i in /var/tmp/vi.recover/recover.*; do
443			# Only test files that are readable.
444			if [ ! -r "${i}" ]; then
445				continue
446			fi
447
448			# Delete any recovery files that are zero length,
449			# corrupted, or that have no corresponding backup file.
450			# Else send mail to the user.
451			recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
452			if [ -n "${recfile}" -a -s "${recfile}" ]; then
453				sendmail -t < "${i}"
454			else
455				rm -f "${i}"
456			fi
457		done
458	fi
459fi
460
461# Make a bounds file for msgs(1) if there isn't one already
462# "Delete important files with symlink" security hole?
463#
464if [ -d /var/msgs -a ! -f /var/msgs/bounds ]; then
465	echo 0 > /var/msgs/bounds
466fi
467
468case ${update_motd} in
469[Nn][Oo] | '')
470	;;
471*)
472	if T=`mktemp /tmp/_motd.XXXXXX`; then
473		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
474		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
475		cmp -s ${T} /etc/motd || {
476			cp ${T} /etc/motd
477			chmod 644 /etc/motd
478		}
479		rm -f ${T}
480	fi
481	;;
482esac
483
484# Configure implementation specific stuff
485#
486arch=`uname -m`
487if [ -r /etc/rc.${arch} ]; then
488	. /etc/rc.${arch}
489fi
490
491# Run rc.devfs if readable to customize devfs
492#
493if [ -r /etc/rc.devfs ]; then
494	sh /etc/rc.devfs
495fi
496
497# Do traditional (but rather obsolete) rc.local file if it exists.  If you
498# use this file and want to make it programmatic, source /etc/defaults/rc.conf
499# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
500# shown below.  Please do not put local extensions into /etc/rc itself.
501# Use /etc/rc.local
502#
503# ---- rc.local ----
504#	if [ -r /etc/defaults/rc.conf ]; then
505#		. /etc/defaults/rc.conf
506#		source_rc_confs
507#	elif [ -r /etc/rc.conf ]; then
508#		. /etc/rc.conf
509#	fi
510#
511#	... additional startup conditionals ...
512# ---- rc.local ----
513#
514if [ -r /etc/rc.local ]; then
515	echo -n 'starting local daemons:'
516	sh /etc/rc.local
517	echo '.'
518fi
519
520# For each valid dir in $local_startup, search for init scripts matching *.sh
521#
522case ${local_startup} in
523[Nn][Oo] | '')
524	;;
525*)
526	echo -n 'Local package initialization:'
527	for dir in ${local_startup}; do
528		if [ -d "${dir}" ]; then
529			for script in ${dir}/*.sh; do
530				if [ -x "${script}" ]; then
531					(set -T
532					 trap 'exit 1' 2
533					 ${script} start)
534				fi
535			done
536		fi
537	done
538	echo .
539	;;
540esac
541
542if [ -n "${network_pass3_done}" ]; then
543	network_pass4
544fi
545
546# Raise kernel security level.  This should be done only after `fsck' has
547# repaired local file systems if you want the securelevel to be greater than 1.
548#
549case ${kern_securelevel_enable} in
550[Yy][Ee][Ss])
551	if [ "${kern_securelevel}" -ge 0 ]; then
552		echo 'Raising kernel security level'
553		sysctl -w kern.securelevel=${kern_securelevel}
554	fi
555	;;
556esac
557
558date
559exit 0
560