rc revision 1.164
11.149Slukem#!/bin/sh
21.149Slukem#
31.164Sapb# $NetBSD: rc,v 1.164 2009/09/11 18:17:04 apb Exp $
41.149Slukem#
51.154Slukem# rc --
61.164Sapb#	Run the scripts in /etc/rc.d with rcorder, and log output
71.164Sapb#	to /var/run/rc.log.
81.149Slukem
91.154Slukem#	System startup script run by init(8) on autoboot or after single-user.
101.149Slukem#	Output and error are redirected to console by init, and the console
111.149Slukem#	is the controlling terminal.
121.1Scgd
131.149Slukemexport HOME=/
141.149Slukemexport PATH=/sbin:/bin:/usr/sbin:/usr/bin
151.157Slukemumask 022
161.1Scgd
171.164Sapbif [ -e ./rc.subr ] ; then
181.164Sapb	. ./rc.subr # for testing
191.164Sapbelse
201.164Sapb	. /etc/rc.subr
211.164Sapbfi
221.149Slukem. /etc/rc.conf
231.162Slukem_rc_conf_loaded=true
241.150Senami
251.164Sapb: ${RC_LOG_FILE:="/var/run/rc.log"}
261.164Sapb
271.150Senamiif ! checkyesno rc_configured; then
281.150Senami	echo "/etc/rc.conf is not configured.  Multiuser boot aborted."
291.150Senami	exit 1
301.150Senamifi
311.50Sthorpej
321.149Slukemif [ "$1" = autoboot ]; then
331.149Slukem	autoboot=yes
341.160Slukem	rc_fast=yes	# run_rc_command(): do fast booting
351.50Sthorpejfi
361.107Stron
371.164Sapb#
381.164Sapb# Completely ignore INT and QUIT at the outer level.  The rc_real_work()
391.164Sapb# function should do something different.
401.164Sapb#
411.164Sapbtrap '' INT QUIT
421.1Scgd
431.149Slukem#
441.164Sapb# This string will be used to mark lines of meta-data sent over the pipe
451.164Sapb# from the rc_real_work() function to the rc_postprocess() function.  Lines
461.164Sapb# not so marked are assumed to be output from rc.d scripts.
471.164Sapb#
481.164Sapb# This string is long and unique to ensure that it does not accidentally
491.164Sapb# appear in output from any rc.d script.  It must not contain any
501.164Sapb# characters that are special to glob expansion ('*', '?', '[', or ']').
511.164Sapb#
521.164Sapbrc_metadata_prefix="$0:$$:metadata:";
531.1Scgd
541.164Sapb# Child scripts may sometimes want to print directly to the original
551.164Sapb# stdout and stderr, bypassing the pipe to the postprocessor.  These
561.164Sapb# _rc_*_fd variables are private, shared with /etc/rc.subr, but not
571.164Sapb# intended to be used directly by child scripts.  (Child scripts
581.164Sapb# may use rc.subr's no_rc_postprocess function.)
591.164Sapb#
601.164Sapb_rc_original_stdout_fd=7; export _rc_original_stdout_fd
611.164Sapb_rc_original_stderr_fd=8; export _rc_original_stderr_fd
621.164Sapbeval "exec ${_rc_original_stdout_fd}>&1"
631.164Sapbeval "exec ${_rc_original_stderr_fd}>&2"
641.156Slukem
651.164Sapb#
661.164Sapb# rc_real_work
671.164Sapb#	Do the real work.  Output from this function will be piped into
681.164Sapb#	rc_postprocess(), and some of the output will be marked as
691.164Sapb#	metadata.
701.164Sapb#
711.164Sapb# The body of this function is defined using (...), not {...}, to force
721.164Sapb# it to run in a subshell.
731.164Sapb#
741.164Sapbrc_real_work()
751.164Sapb(
761.164Sapb	stty status '^T'
771.164Sapb
781.164Sapb	# print_rc_metadata() wants to be able to print to the pipe
791.164Sapb	# that goes to our postprocessor, even if its in a context
801.164Sapb	# with redirected output.
811.164Sapb	#
821.164Sapb	_rc_postprocessor_fd=9 ; export _rc_postprocessor_fd
831.164Sapb	eval "exec ${_rc_postprocessor_fd}>&1"
841.164Sapb
851.164Sapb	# Print a metadata line when we exit
861.164Sapb	#
871.164Sapb	trap 'es=$?; print_rc_metadata "exit:$es"; trap "" 0; exit $es' 0
881.164Sapb
891.164Sapb	#	Set shell to ignore SIGINT, but children will not ignore it.
901.164Sapb	#	Shell catches SIGQUIT and returns to single user.
911.164Sapb	#
921.164Sapb	trap : INT
931.164Sapb	trap '_msg="Boot interrupted at $(date)";
941.164Sapb	      print_rc_metadata "interrupted:${_msg}";
951.164Sapb	      exit 1' QUIT
961.164Sapb
971.164Sapb	print_rc_metadata "start:$(date)"
981.164Sapb
991.164Sapb	#
1001.164Sapb	# The stop_boot() function in rc.subr may kill $RC_PID.  We want
1011.164Sapb	# it to kill the subshell running this rc_real_work() function,
1021.164Sapb	# rather than killing the parent shell, because we want the
1031.164Sapb	# rc_postprocess() function to be able to log the error
1041.164Sapb	# without being killed itself.
1051.164Sapb	#
1061.164Sapb	# "$$" is the pid of the top-level shell, not the pid of the
1071.164Sapb	# subshell that's executing this function.  The command below
1081.164Sapb	# tentatively assumes that the parent of the "/bin/sh -c ..."
1091.164Sapb	# process will be the current subshell, and then uses "kill -0
1101.164Sapb	# ..." to check the result.  If the "/bin/sh -c ..." process
1111.164Sapb	# fails, or returns the pid of an ephemeral process that exits
1121.164Sapb	# before the "kill" command, then we fall back to using "$$".
1131.164Sapb	#
1141.164Sapb	RC_PID=$(/bin/sh -c 'ps -p $$ -o ppid=') || RC_PID=$$
1151.164Sapb	kill -0 $RC_PID >/dev/null 2>&1 || RC_PID=$$
1161.164Sapb
1171.164Sapb	#
1181.164Sapb	# Get a list of all rc.d scripts, and use rcorder to choose
1191.164Sapb	# what order to execute them.
1201.164Sapb	#
1211.164Sapb	# For testing, allow RC_FILES_OVERRIDE from the environment to
1221.164Sapb	# override this.
1231.164Sapb	#
1241.164Sapb	print_rc_metadata "cmd-name:rcorder"
1251.164Sapb	scripts=$(for rcd in ${rc_directories:-/etc/rc.d}; do
1261.164Sapb		test -d ${rcd} && echo ${rcd}/*;
1271.164Sapb	done)
1281.164Sapb	files=$(rcorder -s nostart ${rc_rcorder_flags} ${scripts})
1291.164Sapb	print_rc_metadata "cmd-status:rcorder:$?"
1301.164Sapb
1311.164Sapb	if [ -n "${RC_FILES_OVERRIDE}" ]; then
1321.164Sapb		files="${RC_FILES_OVERRIDE}"
1331.164Sapb	fi
1341.164Sapb
1351.164Sapb	#
1361.164Sapb	# Run the scripts in order.
1371.164Sapb	#
1381.164Sapb	for _rc_elem in $files; do
1391.164Sapb		print_rc_metadata "cmd-name:$_rc_elem"
1401.164Sapb		run_rc_script $_rc_elem start
1411.164Sapb		print_rc_metadata "cmd-status:$_rc_elem:$?"
1421.164Sapb	done
1431.164Sapb
1441.164Sapb	print_rc_metadata "end:$(date)"
1451.164Sapb	exit 0
1461.164Sapb)
1471.155Slukem
1481.164Sapb#
1491.164Sapb# rc_postprocess
1501.164Sapb#	Post-process the output from the rc_real_work() function.  For
1511.164Sapb#	each line of input, we have to decide whether to print the line
1521.164Sapb#	to the console, print a twiddle on the console, print a line to
1531.164Sapb#	the log, or some combination of these.
1541.164Sapb#
1551.164Sapb#	If rc_silent is true, then suppress most output, instead running
1561.164Sapb#	rc_silent_cmd (typically "twiddle") for each line.
1571.164Sapb#
1581.164Sapb# The body of this function is defined using (...), not {...}, to force
1591.164Sapb# it to run in a subshell.
1601.164Sapb#
1611.164Sapb# We have to deal with the following constraints:
1621.164Sapb#
1631.164Sapb#  * There may be no writable file systems early in the boot, so
1641.164Sapb#    any use of temporary files would be problematic.
1651.164Sapb#
1661.164Sapb#  * Scripts run during the boot may clear /tmp and/var/run, so even
1671.164Sapb#    if they are writable, using those directories too early may be
1681.164Sapb#    problematic.  We assume that it's safe to write to our log file
1691.164Sapb#    after the mountcritlocal script has run.
1701.164Sapb#
1711.164Sapb#  * /usr/bin/tee cannot be used because the /usr file system may not
1721.164Sapb#    be mounted early in the boot.
1731.164Sapb#
1741.164Sapb#  * All calls to the rc_log_message and rc_log_flush functions must be
1751.164Sapb#    from the same subshell, otherwise the use of a shell variable to
1761.164Sapb#    buffer log messages will fail.
1771.164Sapb#
1781.164Sapbrc_postprocess()
1791.164Sapb(
1801.164Sapb	local line
1811.164Sapb	local before after
1821.164Sapb	local IFS=''
1831.164Sapb
1841.164Sapb	# Try quite hard to flush the log to disk when we exit.
1851.164Sapb	trap 'es=$?; rc_log_flush FORCE; trap "" 0; exit $es' 0
1861.164Sapb
1871.164Sapb	yesno_to_truefalse rc_silent 2>/dev/null
1881.164Sapb
1891.164Sapb	while read -r line ; do
1901.164Sapb		case "$line" in
1911.164Sapb		"${rc_metadata_prefix}"*)
1921.164Sapb			after="${line#*"${rc_metadata_prefix}"}"
1931.164Sapb			rc_postprocess_metadata "${after}"
1941.164Sapb			;;
1951.164Sapb		*"${rc_metadata_prefix}"*)
1961.164Sapb			# magic string is present, but not at the start of
1971.164Sapb			# the line.  Treat it like two separate lines.
1981.164Sapb			before="${line%"${rc_metadata_prefix}"*}"
1991.164Sapb			rc_postprocess_plain_line "${before}"
2001.164Sapb			after="${line#*"${rc_metadata_prefix}"}"
2011.164Sapb			rc_postprocess_metadata "${after}"
2021.164Sapb			;;
2031.164Sapb		*)
2041.164Sapb			rc_postprocess_plain_line "${line}"
2051.164Sapb			;;
2061.164Sapb		esac
2071.164Sapb	done
2081.164Sapb
2091.164Sapb	# If we get here, then the rc_real_work() function must have
2101.164Sapb	# exited uncleanly.  A clean exit would have been accompanied by
2111.164Sapb	# a line of metadata that would have prevented us from getting
2121.164Sapb	# here.
2131.164Sapb	#
2141.164Sapb	exit 1
2151.164Sapb)
2161.164Sapb
2171.164Sapb#
2181.164Sapb# rc_postprocess_plain_line string
2191.164Sapb#	$1 is a string representing a line of output from one of the
2201.164Sapb#	rc.d scripts.  Append the line to the log, and also either
2211.164Sapb#	display the line on the console, or run $rc_silent_cmd,
2221.164Sapb#	depending on the value of $rc_silent.
2231.164Sapb#
2241.164Sapbrc_postprocess_plain_line()
2251.164Sapb{
2261.164Sapb	local line="$1"
2271.164Sapb	rc_log_message "${line}"
2281.164Sapb	if $rc_silent; then
2291.164Sapb		eval "$rc_silent_cmd"
2301.164Sapb	else
2311.164Sapb		printf "%s\n" "${line}"
2321.164Sapb	fi
2331.164Sapb}
2341.164Sapb
2351.164Sapb#
2361.164Sapb# rc_postprocess_metadata string
2371.164Sapb#	$1 is a string containing metadata from the rc_real_work()
2381.164Sapb#	function.  The rc_metadata_prefix marker should already
2391.164Sapb#	have been removed before the string is passed to this function.
2401.164Sapb#	Take appropriate action depending on the content of the string.
2411.164Sapb#
2421.164Sapbrc_postprocess_metadata()
2431.164Sapb{
2441.164Sapb	local metadata="$1"
2451.164Sapb	local keyword args
2461.164Sapb	local msg
2471.164Sapb	local IFS=':'
2481.164Sapb
2491.164Sapb	# given metadata="bleep:foo bar:baz",
2501.164Sapb	# set keyword="bleep", args="foo bar:baz",
2511.164Sapb	# $1="foo bar", $2="baz"
2521.164Sapb	#
2531.164Sapb	keyword="${metadata%%:*}"
2541.164Sapb	args="${metadata#*:}"
2551.164Sapb	set -- $args
2561.164Sapb
2571.164Sapb	case "$keyword" in
2581.164Sapb	start)
2591.164Sapb		# $args contains a date/time
2601.164Sapb		rc_log_message "[$0 starting at $args]"
2611.164Sapb		if ! $rc_silent; then
2621.164Sapb			printf "%s\n" "$args"
2631.164Sapb		fi
2641.164Sapb		;;
2651.164Sapb	cmd-name)
2661.164Sapb		rc_log_message "[running $1]"
2671.164Sapb		;;
2681.164Sapb	cmd-status)
2691.164Sapb		# $1 is a command name, $2 is the command's exit status.
2701.164Sapb		# If the command failed, report it, and add it to a list.
2711.164Sapb		if [ "$2" != 0 ]; then
2721.164Sapb			rc_failures="${rc_failures}${rc_failures:+ }$1"
2731.164Sapb			msg="$1 reported failure status $2"
2741.164Sapb			rc_log_message "$msg"
2751.164Sapb			if ! $rc_silent; then
2761.164Sapb				printf "%s\n" "$msg"
2771.164Sapb			fi
2781.164Sapb		fi
2791.164Sapb		# After the mountcritlocal script has finished, it's
2801.164Sapb		# OK to flush the log to disk
2811.164Sapb		case "$1" in
2821.164Sapb		*/mountcritlocal)
2831.164Sapb			rc_log_flush OK
2841.164Sapb			;;
2851.164Sapb		esac
2861.164Sapb		;;
2871.164Sapb	note)
2881.164Sapb		rc_log_message "[NOTE: $args]"
2891.164Sapb		;;
2901.164Sapb	end)
2911.164Sapb		#
2921.164Sapb		# If any scripts (or other commands) failed, report them.
2931.164Sapb		#
2941.164Sapb		if [ -n "$rc_failures" ]; then
2951.164Sapb			rc_log_message "[failures]"
2961.164Sapb			msg="The following components reported failures:"
2971.164Sapb			msg="${msg}${nl}$( echo "    ${rc_failures}" | fmt )"
2981.164Sapb			msg="${msg}${nl}See ${RC_LOG_FILE} for more information."
2991.164Sapb			rc_log_message "${msg}"
3001.164Sapb			printf "%s\n" "${msg}"
3011.164Sapb		fi
3021.164Sapb		#
3031.164Sapb		# Report the end date/time, even in silent mode
3041.164Sapb		#
3051.164Sapb		rc_log_message "[$0 finished at $args]"
3061.164Sapb		printf "%s\n" "$args"
3071.164Sapb		;;
3081.164Sapb	exit)
3091.164Sapb		rc_log_message "[$0 exiting with status $1]"
3101.164Sapb		exit $1
3111.164Sapb		;;
3121.164Sapb	interrupted)
3131.164Sapb		# $args is a human-readable message
3141.164Sapb		rc_log_message "$args"
3151.164Sapb		printf "%s\n" "$args"
3161.164Sapb		;;
3171.164Sapb	*)
3181.164Sapb		# an unrecognised line of metadata
3191.164Sapb		rc_log_message "[metadata:${metadata}]"
3201.164Sapb		;;
3211.164Sapb	esac
3221.164Sapb}
3231.164Sapb
3241.164Sapb#
3251.164Sapb# rc_log_message string [...]
3261.164Sapb#	write a message to the log file, or buffer it for later.
3271.164Sapb#
3281.164Sapbrc_log_message()
3291.164Sapb{
3301.164Sapb	_rc_log_buffer="${_rc_log_buffer}${*}${nl}"
3311.164Sapb	rc_log_flush
3321.164Sapb}
3331.1Scgd
3341.164Sapb#
3351.164Sapb# rc_log_flush [OK|FORCE]
3361.164Sapb#	save outstanding messages from $_rc_log_buffer to $RC_LOG_FILE.
3371.164Sapb#
3381.164Sapb# The log file is expected to reside in the /var/run directory, which
3391.164Sapb# may not be writable very early in the boot sequence, and which is
3401.164Sapb# erased a little later in the boot sequence.  We therefore avoid
3411.164Sapb# writing to the file until we believe it's safe to do so.  We also
3421.164Sapb# assume that it's reasonable to always append to the file, never
3431.164Sapb# truncating it.
3441.164Sapb#
3451.164Sapb# Optional argument $1 may be "OK" to report that writing to the log
3461.164Sapb# file is expected to be safe from now on, or "FORCE" to force writing
3471.164Sapb# to the log file even if it may be unsafe.
3481.164Sapb#
3491.164Sapb# Returns a non-zero status if messages could not be written to the
3501.164Sapb# file.
3511.164Sapb#
3521.164Sapbrc_log_flush()
3531.164Sapb{
3541.164Sapb	#
3551.164Sapb	# If $_rc_log_flush_ok is false, then it's probably too early to
3561.164Sapb	# write to the log file, so don't do it, unless $1 is "FORCE".
3571.164Sapb	#
3581.164Sapb	: ${_rc_log_flush_ok=false}
3591.164Sapb	case "$1:$_rc_log_flush_ok" in
3601.164Sapb	OK:*)
3611.164Sapb		_rc_log_flush_ok=true
3621.164Sapb		;;
3631.164Sapb	FORCE:*)
3641.164Sapb		: OK just this once
3651.164Sapb		;;
3661.164Sapb	*:true)
3671.164Sapb		: OK
3681.164Sapb		;;
3691.164Sapb	*)
3701.164Sapb		# it's too early in the boot sequence, so don't flush
3711.164Sapb		return 1
3721.164Sapb		;;
3731.164Sapb	esac
3741.164Sapb
3751.164Sapb	#
3761.164Sapb	# Now append the buffer to the file.  The buffer should already
3771.164Sapb	# contain a trailing newline, so don't add an extra newline.
3781.164Sapb	#
3791.164Sapb	if [ -n "$_rc_log_buffer" ]; then
3801.164Sapb		if { printf "%s" "${_rc_log_buffer}" >>"${RC_LOG_FILE}" ; } \
3811.164Sapb			2>/dev/null
3821.164Sapb		then
3831.164Sapb			_rc_log_buffer=""
3841.164Sapb		else
3851.164Sapb			return 1
3861.164Sapb		fi
3871.164Sapb	fi
3881.164Sapb	return 0
3891.164Sapb}
3901.164Sapb
3911.164Sapb#
3921.164Sapb# Most of the action is in the rc_real_work() and rc_postprocess()
3931.164Sapb# functions.
3941.164Sapb#
3951.164Sapbrc_real_work "$@" 2>&1 | rc_postprocess
3961.164Sapbexit $?
397