rc revision 55511
112795Swpaul#!/bin/sh
212795Swpaul# $FreeBSD: head/etc/rc 55511 2000-01-06 14:27:37Z peter $
312795Swpaul#	From: @(#)rc	5.27 (Berkeley) 6/5/91
412795Swpaul
512795Swpaul# System startup script run by init on autoboot
612795Swpaul# or after single-user.
712795Swpaul# Output and error are redirected to console by init,
8100441Scharnier# and the console is the controlling terminal.
912795Swpaul
1012795Swpaul# Note that almost all of the user-configurable behavior is no longer in
1112795Swpaul# this file, but rather in /etc/defaults/rc.conf.  Please check that file
12100441Scharnier# first before contemplating any changes here.  If you do need to change
1312795Swpaul# this file for some reason, we would like to know about it.
1412795Swpaul
1512795Swpaulstty status '^T'
16100441Scharnier
1712795Swpaul# Set shell to ignore SIGINT (2), but not children;
1812795Swpaul# shell catches SIGQUIT (3) and returns to single user after fsck.
1912795Swpaul#
20100441Scharniertrap : 2
2112795Swpaultrap : 3	# shouldn't be needed
2212795Swpaul
2312795SwpaulHOME=/
24100441ScharnierPATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
2512795Swpaulexport HOME PATH
2612795Swpaul
2712795Swpaul# BOOTP diskless boot.  We have to run the rc file early in order to
2812795Swpaul# retarget various config files.
2912795Swpaul#
30100441Scharnierif [ -r /etc/rc.diskless1 ]; then
3112795Swpaul	dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
32146833Sstefanf	if [ ${dlv:=0} != 0 ]; then
3312795Swpaul		. /etc/rc.diskless1
3412795Swpaul	fi
3527935Scharnierfi
3612795Swpaul
37100441Scharnier# If there is a global system configuration file, suck it in.
38100441Scharnier#
39100441Scharnierif [ -r /etc/defaults/rc.conf ]; then
4012795Swpaul	. /etc/defaults/rc.conf
4112795Swpaulelif [ -r /etc/rc.conf ]; then
4212795Swpaul	. /etc/rc.conf
4312795Swpaulfi
4427935Scharnier
4512795Swpaul# Configure ccd devices.
4612795Swpaul#
4712795Swpaulif [ -r /etc/ccd.conf ]; then
4812795Swpaul	ccdconfig -C
4912795Swpaulfi
5012795Swpaul
5112795Swpaulcase ${start_vinum} in
5212795Swpaul[Yy][Ee][Ss])
5312795Swpaul	vinum start
5412795Swpaul	;;
5512795Swpaulesac
5612795Swpaul
57141614Salfredswapon -a
5812795Swpaul
59141614Salfredcase $1 in
6012795Swpaulautoboot)
61141614Salfred	echo Automatic reboot in progress...
6212795Swpaul	fsck -p
6312795Swpaul	case $? in
6412795Swpaul	0)
6512795Swpaul		;;
6692921Simp	2)
6792921Simp		exit 1
6892921Simp		;;
6912795Swpaul	4)
7012795Swpaul		reboot
7112795Swpaul		echo "reboot failed... help!"
7212795Swpaul		exit 1
7312795Swpaul		;;
7412795Swpaul	8)
7512795Swpaul		echo "Automatic file system check failed... help!"
7612795Swpaul		exit 1
7712795Swpaul		;;
7812795Swpaul	12)
7912795Swpaul		echo "Reboot interrupted"
8012795Swpaul		exit 1
8112795Swpaul		;;
8212795Swpaul	130)
8312795Swpaul		# interrupt before catcher installed
8412795Swpaul		exit 1
8517142Sjkh		;;
8612795Swpaul	*)
8712795Swpaul		echo "Unknown error in reboot"
8812795Swpaul		exit 1
8912795Swpaul		;;
9012795Swpaul	esac
9112795Swpaul	;;
9212795Swpaul*)
9312795Swpaul	echo Skipping disk checks ...
9412795Swpaul	;;
9512795Swpaulesac
9612795Swpaul
9712795Swpaulset -T
9812795Swpaultrap "echo 'Reboot interrupted'; exit 1" 3
9912795Swpaul
10012795Swpaul# root normally must be read/write, but if this is a BOOTP NFS
10112795Swpaul# diskless boot it does not have to be.
10212795Swpaul#
10312795Swpaulcase ${root_rw_mount} in
10412795Swpaul[Nn][Oo] | '')
10512795Swpaul	;;
10612795Swpaul*)
10712795Swpaul	if ! mount -u -o rw / ; then
10812795Swpaul		echo "Mounting root filesystem rw failed, startup aborted"
10912795Swpaul		exit 1
11012795Swpaul	fi
11112795Swpaul	;;
11212795Swpaulesac
11312795Swpaul
11412795Swpaulumount -a >/dev/null 2>&1
11527935Scharnier
11612795Swpaul# Early NFS mounts and clean up of /var/db/mounttab.
11712795Swpaul#
11812795Swpaulcase ${early_nfs_mounts} in
11912795Swpaul[Yy][Ee][Ss])
12012795Swpaul	case $1 in
121141614Salfred	autoboot)
12212795Swpaul		if [ -f /var/db/mounttab ]; then
12312795Swpaul			rpc.umntall -k
12412795Swpaul		fi
12512795Swpaul		;;
12612795Swpaul	esac
12712795Swpaul	mount -a
12812795Swpaul	;;
12912795Swpaul*)
13012795Swpaul	mount -a -t nonfs
13112795Swpaul	;;
13212795Swpaulesac
13312795Swpaul
13412795Swpaulcase $? in
13512795Swpaul0)
136100441Scharnier	;;
13712795Swpaul*)
13812795Swpaul	echo "Mounting /etc/fstab filesystems failed, startup aborted"
13912795Swpaul	exit 1
14012795Swpaul	;;
14112795Swpaulesac
14212795Swpaul
14312795Swpaul# Run custom disk mounting function here
14412795Swpaul#
14512795Swpaulif [ -n "${diskless_mount}" -a -r "${diskless_mount}" ]; then
14612795Swpaul		sh ${diskless_mount}
14712795Swpaulfi
14812795Swpaul
14912795Swpauladjkerntz -i
15017142Sjkh
15112795Swpaulclean_var() {
15212795Swpaul	if [ ! -f /var/run/clean_var ]; then
15312795Swpaul		rm -rf /var/run/*
15412795Swpaul		rm -f /var/spool/lock/*
15512795Swpaul		rm -rf /var/spool/uucp/.Temp/*
15612795Swpaul		# Keep a copy of the boot messages around
15712795Swpaul		dmesg >/var/run/dmesg.boot
15812795Swpaul		# And an initial utmp file
159141614Salfred		(cd /var/run && cp /dev/null utmp && chmod 644 utmp;)
16012795Swpaul		>/var/run/clean_var
16112795Swpaul	fi
16212795Swpaul}
16312795Swpaul
16412795Swpaulif [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then
16512795Swpaul	# network_pass1() *may* end up writing stuff to /var - we don't want to
16612795Swpaul	# remove it immediately afterwards - *nor* to we want to fail to clean
16712795Swpaul	# an nfs-mounted /var.
16812795Swpaul	clean_var
169141614Salfredfi
17012795Swpaul
17112795Swpaul# Add additional swapfile, if configured.
17212795Swpaul#
17312795Swpaulcase ${swapfile} in
17412795Swpaul[Nn][Oo] | '')
17512795Swpaul	;;
176*)
177	if [ -w "${swapfile}" -a -b /dev/vn0b ]; then
178		echo "Adding ${swapfile} as additional swap."
179		vnconfig /dev/vn0b ${swapfile} && swapon /dev/vn0b
180	fi
181	;;
182esac
183
184# Set sysctl variables as early as we can
185#
186if [ -r /etc/rc.sysctl ]; then
187	. /etc/rc.sysctl
188fi
189
190# Configure serial devices
191#
192if [ -r /etc/rc.serial ]; then
193	. /etc/rc.serial
194fi
195
196# Start up PC-card configuration
197#
198if [ -r /etc/rc.pccard ]; then
199	. /etc/rc.pccard
200fi
201
202# Start up the initial network configuration.
203#
204if [ -r /etc/rc.network ]; then
205	. /etc/rc.network	# We only need to do this once.
206	network_pass1
207fi
208
209# Only mount NFS file systems if $early_nfs_mounts is false.
210# If there is a /var/db/mounttab, clean it up with rpc.umntall.
211#
212echo -n "Mounting NFS file systems"
213case ${early_nfs_mounts} in
214[Yy][Ee][Ss])
215	;;
216*)
217	case $1 in
218	autoboot)
219		case ${nfs_client_enable} in
220		[Yy][Ee][Ss])
221			if [ -f /var/db/mounttab ]; then
222				rpc.umntall -k
223			fi
224			;;
225		esac
226		;;
227	esac
228	mount -a -t nfs
229	;;
230esac
231echo .
232
233# Whack the pty perms back into shape.
234#
235chflags 0 /dev/tty[pqrsPQRS]*
236chmod 666 /dev/tty[pqrsPQRS]*
237chown root:wheel /dev/tty[pqrsPQRS]*
238
239# Clean up left-over files
240#
241clean_var			# If it hasn't already been done
242rm /var/run/clean_var
243
244# Clearing /tmp at boot-time seems to have a long tradition.  It doesn't
245# help in any way for long-living systems, and it might accidentally
246# clobber files you would rather like to have preserved after a crash
247# (if not using mfs /tmp anyway).
248#
249# See also the example of another cleanup policy in /etc/periodic/daily.
250#
251case ${clear_tmp_enable} in
252[Yy][Ee][Ss])
253	echo clearing /tmp
254	# prune quickly with one rm, then use find to clean up /tmp/[lq]*
255	# (not needed with mfs /tmp, but doesn't hurt there...)
256	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
257		find -d . ! -name . ! -name lost+found ! -name quota.user \
258		! -name quota.group -exec rm -rf -- {} \;)
259	;;
260esac
261
262# Remove X lock files, since they will prevent you from restarting X11
263# after a system crash.
264#
265rm -f /tmp/.X*-lock /tmp/.X11-unix/*
266
267# Snapshot any kernel -c changes back to disk here <someday>.
268# This has changed with ELF and /kernel.config.
269
270echo -n 'additional daemons:'
271
272# Start system logging and name service.  Named needs to start before syslogd
273# if you don't have a /etc/resolv.conf.
274#
275case ${syslogd_enable} in
276[Yy][Ee][Ss])
277	# Transitional symlink (for the next couple of years :) until all
278	# binaries have had a chance to move towards /var/run/log.
279	if [ ! -h /dev/log ]; then
280		# might complain for r/o root f/s
281		ln -sf /var/run/log /dev/log
282	fi
283
284	rm -f /var/run/log
285	echo -n ' syslogd';	syslogd ${syslogd_flags}
286	;;
287esac
288
289echo '.'
290
291# Enable dumpdev so that savecore can see it.
292# /var/crash should be a directory or a symbolic link
293# to the crash directory if core dumps are to be saved.
294#
295case ${dumpdev} in
296[Nn][Oo] | '')
297	;;
298*)
299	if [ -e "${dumpdev}" -a -d /var/crash ]; then
300		dumpon ${dumpdev}
301		echo -n checking for core dump...
302		savecore /var/crash
303	fi
304	;;
305esac
306
307if [ -n "${network_pass1_done}" ]; then
308	network_pass2
309fi
310
311# Enable/Check the quotas (must be after ypbind if using NIS)
312#
313case ${enable_quotas} in
314[Yy][Ee][Ss])
315	case ${check_quotas} in
316	[Yy][Ee][Ss])
317		echo -n 'checking quotas:'
318		quotacheck -a
319		echo ' done.'
320		;;
321	esac
322
323	echo -n 'enabling quotas:'
324	quotaon -a
325	echo ' done.'
326	;;
327esac
328
329if [ -n "${network_pass2_done}" ]; then
330	network_pass3
331fi
332
333# Build ps databases
334#
335dev_mkdb
336
337# Check the password temp/lock file
338#
339if [ -e /etc/ptmp ]; then
340	logger -s -p auth.err \
341	"password file may be incorrect -- /etc/ptmp exists"
342fi
343
344case ${accounting_enable} in
345[Yy][Ee][Ss])
346	if [ -d /var/account ]; then
347		echo 'turning on accounting'
348		if [ ! -e /var/account/acct ]; then
349			touch /var/account/acct
350		fi
351		accton /var/account/acct
352	fi
353	;;
354esac
355
356# Make shared lib searching a little faster.  Leave /usr/lib first if you
357# add your own entries or you may come to grief.
358#
359if [ -x /sbin/ldconfig ]; then
360	case `/usr/bin/objformat` in
361	elf)
362		_LDC=/usr/lib
363		for i in ${ldconfig_paths}; do
364			if [ -d "${i}" ]; then
365				_LDC="${_LDC} ${i}"
366			fi
367		done
368		echo 'setting ELF ldconfig path:' ${_LDC}
369		ldconfig -elf ${_LDC}
370		;;
371	esac
372
373	# Legacy aout support for i386 only
374	case `sysctl -n hw.machine` in
375	i386)
376		# Default the a.out ldconfig path.
377		: ${ldconfig_paths_aout=${ldconfig_paths}}
378		_LDC=/usr/lib/aout
379		for i in ${ldconfig_paths_aout}; do
380			if [ -d "${i}" ]; then
381				_LDC="${_LDC} ${i}"
382			fi
383		done
384		echo 'setting a.out ldconfig path:' ${_LDC}
385		ldconfig -aout ${_LDC}
386		;;
387	esac
388fi
389
390# Now start up miscellaneous daemons that don't belong anywhere else
391#
392echo -n starting standard daemons:
393case ${inetd_enable} in
394[Nn][Oo])
395	;;
396*)
397	echo -n ' inetd';	inetd ${inetd_flags}
398	;;
399esac
400
401case ${cron_enable} in
402[Nn][Oo])
403	;;
404*)
405	echo -n ' cron';	cron
406	;;
407esac
408
409case ${lpd_enable} in
410[Yy][Ee][Ss])
411	echo -n ' printer';	${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
412	;;
413esac
414
415case ${sendmail_enable} in
416[Yy][Ee][Ss])
417	if [ -r /etc/mail/sendmail.cf ]; then
418		echo -n ' sendmail';	/usr/sbin/sendmail ${sendmail_flags}
419	fi
420	;;
421esac
422
423case ${usbd_enable} in
424[Yy][Ee][Ss])
425	echo -n ' usbd';	/usr/sbin/usbd ${usbd_flags}
426	;;
427esac
428
429echo '.'
430
431# Recover vi editor files.
432find /var/tmp/vi.recover ! -type f -a ! -type d -delete
433vibackup=`echo /var/tmp/vi.recover/vi.*`
434if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
435	echo 'Recovering vi editor sessions'
436	for i in /var/tmp/vi.recover/vi.*; do
437		# Only test files that are readable.
438		if [ ! -r "${i}" ]; then
439			continue
440		fi
441
442		# Unmodified nvi editor backup files either have the
443		# execute bit set or are zero length.  Delete them.
444		if [ -x "${i}" -o ! -s "${i}" ]; then
445			rm -f "${i}"
446		fi
447	done
448
449	# It is possible to get incomplete recovery files, if the editor
450	# crashes at the right time.
451	virecovery=`echo /var/tmp/vi.recover/recover.*`
452	if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
453		for i in /var/tmp/vi.recover/recover.*; do
454			# Only test files that are readable.
455			if [ ! -r "${i}" ]; then
456				continue
457			fi
458
459			# Delete any recovery files that are zero length,
460			# corrupted, or that have no corresponding backup file.
461			# Else send mail to the user.
462			recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
463			if [ -n "${recfile}" -a -s "${recfile}" ]; then
464				sendmail -t < "${i}"
465			else
466				rm -f "${i}"
467			fi
468		done
469	fi
470fi
471
472# Make a bounds file for msgs(1) if there isn't one already
473# "Delete important files with symlink" security hole?
474#
475if [ -d /var/msgs -a ! -f /var/msgs/bounds ]; then
476	echo 0 > /var/msgs/bounds
477fi
478
479case ${update_motd} in
480[Nn][Oo] | '')
481	;;
482*)
483	if T=`mktemp /tmp/_motd.XXXXXX`; then
484		uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
485		awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
486		cmp -s ${T} /etc/motd || {
487			cp ${T} /etc/motd
488			chmod 644 /etc/motd
489		}
490		rm -f ${T}
491	fi
492	;;
493esac
494
495# Configure implementation specific stuff
496#
497arch=`uname -m`
498if [ -r /etc/rc.${arch} ]; then
499	. /etc/rc.${arch}
500fi
501
502# Run rc.devfs if readable to customize devfs
503#
504if [ -r /etc/rc.devfs ]; then
505	sh /etc/rc.devfs
506fi
507
508# Do traditional (but rather obsolete) rc.local file if it exists.  If you
509# use this file and want to make it programmatic, source /etc/defaults/rc.conf
510# in /etc/rc.local and add your custom variables to /etc/rc.conf, as
511# shown below.  Please do not put local extensions into /etc/rc itself.
512# Use /etc/rc.local
513#
514# ---- rc.local ----
515#	if [ -r /etc/defaults/rc.conf ]; then
516#		. /etc/defaults/rc.conf
517#	fi
518#
519#	... additional startup conditionals ...
520# ---- rc.local ----
521#
522if [ -r /etc/rc.local ]; then
523	echo -n 'starting local daemons:'
524	sh /etc/rc.local
525	echo '.'
526fi
527
528# For each valid dir in $local_startup, search for init scripts matching *.sh
529#
530case ${local_startup} in
531[Nn][Oo] | '')
532	;;
533*)
534	echo -n 'Local package initialization:'
535	for dir in ${local_startup}; do
536		if [ -d "${dir}" ]; then
537			for script in ${dir}/*.sh; do
538				if [ -x "${script}" ]; then
539					(set -T
540					 trap 'exit 1' 2
541					 ${script} start)
542				fi
543			done
544		fi
545	done
546	echo .
547	;;
548esac
549
550if [ -n "${network_pass3_done}" ]; then
551	network_pass4
552fi
553
554# Raise kernel security level.  This should be done only after `fsck' has
555# repaired local file systems if you want the securelevel to be greater than 1.
556#
557case ${kern_securelevel_enable} in
558[Yy][Ee][Ss])
559	if [ "${kern_securelevel}" -ge 0 ]; then
560		echo 'Raising kernel security level'
561		sysctl -w kern.securelevel=${kern_securelevel}
562	fi
563	;;
564esac
565
566date
567exit 0
568