rc revision 1.169
11.149Slukem#!/bin/sh 21.149Slukem# 31.169Sapb# $NetBSD: rc,v 1.169 2014/07/27 07:46:46 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.166Sapb# rc.subr redefines echo and printf. Undo that here. 281.166Sapbunset echo ; unalias echo 291.166Sapbunset printf ; unalias printf 301.166Sapb 311.150Senamiif ! checkyesno rc_configured; then 321.150Senami echo "/etc/rc.conf is not configured. Multiuser boot aborted." 331.150Senami exit 1 341.150Senamifi 351.50Sthorpej 361.149Slukemif [ "$1" = autoboot ]; then 371.149Slukem autoboot=yes 381.160Slukem rc_fast=yes # run_rc_command(): do fast booting 391.50Sthorpejfi 401.107Stron 411.164Sapb# 421.164Sapb# Completely ignore INT and QUIT at the outer level. The rc_real_work() 431.164Sapb# function should do something different. 441.164Sapb# 451.164Sapbtrap '' INT QUIT 461.1Scgd 471.149Slukem# 481.164Sapb# This string will be used to mark lines of meta-data sent over the pipe 491.164Sapb# from the rc_real_work() function to the rc_postprocess() function. Lines 501.164Sapb# not so marked are assumed to be output from rc.d scripts. 511.164Sapb# 521.164Sapb# This string is long and unique to ensure that it does not accidentally 531.164Sapb# appear in output from any rc.d script. It must not contain any 541.164Sapb# characters that are special to glob expansion ('*', '?', '[', or ']'). 551.164Sapb# 561.164Sapbrc_metadata_prefix="$0:$$:metadata:"; 571.1Scgd 581.164Sapb# Child scripts may sometimes want to print directly to the original 591.164Sapb# stdout and stderr, bypassing the pipe to the postprocessor. These 601.164Sapb# _rc_*_fd variables are private, shared with /etc/rc.subr, but not 611.164Sapb# intended to be used directly by child scripts. (Child scripts 621.164Sapb# may use rc.subr's no_rc_postprocess function.) 631.164Sapb# 641.164Sapb_rc_original_stdout_fd=7; export _rc_original_stdout_fd 651.164Sapb_rc_original_stderr_fd=8; export _rc_original_stderr_fd 661.164Sapbeval "exec ${_rc_original_stdout_fd}>&1" 671.164Sapbeval "exec ${_rc_original_stderr_fd}>&2" 681.156Slukem 691.164Sapb# 701.164Sapb# rc_real_work 711.164Sapb# Do the real work. Output from this function will be piped into 721.164Sapb# rc_postprocess(), and some of the output will be marked as 731.164Sapb# metadata. 741.164Sapb# 751.164Sapb# The body of this function is defined using (...), not {...}, to force 761.164Sapb# it to run in a subshell. 771.164Sapb# 781.164Sapbrc_real_work() 791.164Sapb( 801.164Sapb stty status '^T' 811.164Sapb 821.164Sapb # print_rc_metadata() wants to be able to print to the pipe 831.164Sapb # that goes to our postprocessor, even if its in a context 841.164Sapb # with redirected output. 851.164Sapb # 861.164Sapb _rc_postprocessor_fd=9 ; export _rc_postprocessor_fd 871.169Sapb _rc_pid=$$ ; export _rc_pid 881.164Sapb eval "exec ${_rc_postprocessor_fd}>&1" 891.164Sapb 901.164Sapb # Print a metadata line when we exit 911.164Sapb # 921.164Sapb trap 'es=$?; print_rc_metadata "exit:$es"; trap "" 0; exit $es' 0 931.164Sapb 941.164Sapb # Set shell to ignore SIGINT, but children will not ignore it. 951.164Sapb # Shell catches SIGQUIT and returns to single user. 961.164Sapb # 971.164Sapb trap : INT 981.164Sapb trap '_msg="Boot interrupted at $(date)"; 991.164Sapb print_rc_metadata "interrupted:${_msg}"; 1001.164Sapb exit 1' QUIT 1011.164Sapb 1021.164Sapb print_rc_metadata "start:$(date)" 1031.164Sapb 1041.164Sapb # 1051.164Sapb # The stop_boot() function in rc.subr may kill $RC_PID. We want 1061.164Sapb # it to kill the subshell running this rc_real_work() function, 1071.164Sapb # rather than killing the parent shell, because we want the 1081.164Sapb # rc_postprocess() function to be able to log the error 1091.164Sapb # without being killed itself. 1101.164Sapb # 1111.164Sapb # "$$" is the pid of the top-level shell, not the pid of the 1121.164Sapb # subshell that's executing this function. The command below 1131.164Sapb # tentatively assumes that the parent of the "/bin/sh -c ..." 1141.164Sapb # process will be the current subshell, and then uses "kill -0 1151.164Sapb # ..." to check the result. If the "/bin/sh -c ..." process 1161.164Sapb # fails, or returns the pid of an ephemeral process that exits 1171.164Sapb # before the "kill" command, then we fall back to using "$$". 1181.164Sapb # 1191.164Sapb RC_PID=$(/bin/sh -c 'ps -p $$ -o ppid=') || RC_PID=$$ 1201.164Sapb kill -0 $RC_PID >/dev/null 2>&1 || RC_PID=$$ 1211.164Sapb 1221.164Sapb # 1231.168Sapb # As long as process $RC_PID is still running, send a "nop" 1241.168Sapb # metadata message to the postprocessor every few seconds. 1251.168Sapb # This should help flush partial lines that may appear when 1261.168Sapb # rc.d scripts that are NOT marked with "KEYWORD: interactive" 1271.168Sapb # nevertheless attempt to print prompts and wait for input. 1281.168Sapb # 1291.168Sapb ( 1301.168Sapb while kill -0 $RC_PID ; do 1311.168Sapb print_rc_metadata "nop" 1321.168Sapb sleep 3 1331.168Sapb done 1341.168Sapb ) & 1351.168Sapb 1361.168Sapb # 1371.164Sapb # Get a list of all rc.d scripts, and use rcorder to choose 1381.164Sapb # what order to execute them. 1391.164Sapb # 1401.164Sapb # For testing, allow RC_FILES_OVERRIDE from the environment to 1411.164Sapb # override this. 1421.164Sapb # 1431.164Sapb print_rc_metadata "cmd-name:rcorder" 1441.164Sapb scripts=$(for rcd in ${rc_directories:-/etc/rc.d}; do 1451.164Sapb test -d ${rcd} && echo ${rcd}/*; 1461.164Sapb done) 1471.164Sapb files=$(rcorder -s nostart ${rc_rcorder_flags} ${scripts}) 1481.164Sapb print_rc_metadata "cmd-status:rcorder:$?" 1491.164Sapb 1501.164Sapb if [ -n "${RC_FILES_OVERRIDE}" ]; then 1511.164Sapb files="${RC_FILES_OVERRIDE}" 1521.164Sapb fi 1531.164Sapb 1541.164Sapb # 1551.164Sapb # Run the scripts in order. 1561.164Sapb # 1571.164Sapb for _rc_elem in $files; do 1581.164Sapb print_rc_metadata "cmd-name:$_rc_elem" 1591.164Sapb run_rc_script $_rc_elem start 1601.164Sapb print_rc_metadata "cmd-status:$_rc_elem:$?" 1611.164Sapb done 1621.164Sapb 1631.164Sapb print_rc_metadata "end:$(date)" 1641.164Sapb exit 0 1651.164Sapb) 1661.155Slukem 1671.164Sapb# 1681.164Sapb# rc_postprocess 1691.164Sapb# Post-process the output from the rc_real_work() function. For 1701.164Sapb# each line of input, we have to decide whether to print the line 1711.164Sapb# to the console, print a twiddle on the console, print a line to 1721.164Sapb# the log, or some combination of these. 1731.164Sapb# 1741.164Sapb# If rc_silent is true, then suppress most output, instead running 1751.164Sapb# rc_silent_cmd (typically "twiddle") for each line. 1761.164Sapb# 1771.164Sapb# The body of this function is defined using (...), not {...}, to force 1781.164Sapb# it to run in a subshell. 1791.164Sapb# 1801.164Sapb# We have to deal with the following constraints: 1811.164Sapb# 1821.164Sapb# * There may be no writable file systems early in the boot, so 1831.164Sapb# any use of temporary files would be problematic. 1841.164Sapb# 1851.164Sapb# * Scripts run during the boot may clear /tmp and/var/run, so even 1861.164Sapb# if they are writable, using those directories too early may be 1871.164Sapb# problematic. We assume that it's safe to write to our log file 1881.164Sapb# after the mountcritlocal script has run. 1891.164Sapb# 1901.164Sapb# * /usr/bin/tee cannot be used because the /usr file system may not 1911.164Sapb# be mounted early in the boot. 1921.164Sapb# 1931.164Sapb# * All calls to the rc_log_message and rc_log_flush functions must be 1941.164Sapb# from the same subshell, otherwise the use of a shell variable to 1951.164Sapb# buffer log messages will fail. 1961.164Sapb# 1971.164Sapbrc_postprocess() 1981.164Sapb( 1991.164Sapb local line 2001.164Sapb local before after 2011.164Sapb local IFS='' 2021.164Sapb 2031.164Sapb # Try quite hard to flush the log to disk when we exit. 2041.164Sapb trap 'es=$?; rc_log_flush FORCE; trap "" 0; exit $es' 0 2051.164Sapb 2061.164Sapb yesno_to_truefalse rc_silent 2>/dev/null 2071.164Sapb 2081.164Sapb while read -r line ; do 2091.164Sapb case "$line" in 2101.164Sapb "${rc_metadata_prefix}"*) 2111.164Sapb after="${line#*"${rc_metadata_prefix}"}" 2121.164Sapb rc_postprocess_metadata "${after}" 2131.164Sapb ;; 2141.164Sapb *"${rc_metadata_prefix}"*) 2151.164Sapb # magic string is present, but not at the start of 2161.166Sapb # the line. Treat it as a partial line of 2171.166Sapb # ordinary data, followed by a line of metadata. 2181.164Sapb before="${line%"${rc_metadata_prefix}"*}" 2191.166Sapb rc_postprocess_partial_line "${before}" 2201.164Sapb after="${line#*"${rc_metadata_prefix}"}" 2211.164Sapb rc_postprocess_metadata "${after}" 2221.164Sapb ;; 2231.164Sapb *) 2241.164Sapb rc_postprocess_plain_line "${line}" 2251.164Sapb ;; 2261.164Sapb esac 2271.164Sapb done 2281.164Sapb 2291.164Sapb # If we get here, then the rc_real_work() function must have 2301.164Sapb # exited uncleanly. A clean exit would have been accompanied by 2311.164Sapb # a line of metadata that would have prevented us from getting 2321.164Sapb # here. 2331.164Sapb # 2341.164Sapb exit 1 2351.164Sapb) 2361.164Sapb 2371.164Sapb# 2381.164Sapb# rc_postprocess_plain_line string 2391.164Sapb# $1 is a string representing a line of output from one of the 2401.164Sapb# rc.d scripts. Append the line to the log, and also either 2411.164Sapb# display the line on the console, or run $rc_silent_cmd, 2421.164Sapb# depending on the value of $rc_silent. 2431.164Sapb# 2441.164Sapbrc_postprocess_plain_line() 2451.164Sapb{ 2461.164Sapb local line="$1" 2471.164Sapb rc_log_message "${line}" 2481.164Sapb if $rc_silent; then 2491.164Sapb eval "$rc_silent_cmd" 2501.164Sapb else 2511.164Sapb printf "%s\n" "${line}" 2521.164Sapb fi 2531.164Sapb} 2541.164Sapb 2551.164Sapb# 2561.166Sapb# rc_postprocess_partial_line string 2571.166Sapb# This is just like rc_postprocess_plain_line, except that 2581.166Sapb# a newline is not appended to the string. 2591.166Sapb# 2601.166Sapbrc_postprocess_partial_line() 2611.166Sapb{ 2621.166Sapb local line="$1" 2631.166Sapb rc_log_message_n "${line}" 2641.166Sapb if $rc_silent; then 2651.166Sapb eval "$rc_silent_cmd" 2661.166Sapb else 2671.166Sapb printf "%s" "${line}" 2681.166Sapb fi 2691.166Sapb} 2701.166Sapb 2711.166Sapb# 2721.164Sapb# rc_postprocess_metadata string 2731.164Sapb# $1 is a string containing metadata from the rc_real_work() 2741.164Sapb# function. The rc_metadata_prefix marker should already 2751.164Sapb# have been removed before the string is passed to this function. 2761.164Sapb# Take appropriate action depending on the content of the string. 2771.164Sapb# 2781.164Sapbrc_postprocess_metadata() 2791.164Sapb{ 2801.164Sapb local metadata="$1" 2811.164Sapb local keyword args 2821.164Sapb local msg 2831.164Sapb local IFS=':' 2841.164Sapb 2851.164Sapb # given metadata="bleep:foo bar:baz", 2861.164Sapb # set keyword="bleep", args="foo bar:baz", 2871.164Sapb # $1="foo bar", $2="baz" 2881.164Sapb # 2891.164Sapb keyword="${metadata%%:*}" 2901.164Sapb args="${metadata#*:}" 2911.164Sapb set -- $args 2921.164Sapb 2931.164Sapb case "$keyword" in 2941.164Sapb start) 2951.167Sapb # Marks the start of the entire /etc/rc script. 2961.167Sapb # $args contains a date/time. 2971.164Sapb rc_log_message "[$0 starting at $args]" 2981.164Sapb if ! $rc_silent; then 2991.164Sapb printf "%s\n" "$args" 3001.164Sapb fi 3011.164Sapb ;; 3021.164Sapb cmd-name) 3031.167Sapb # Marks the start of a child script (usually one of 3041.167Sapb # the /etc/rc.d/* scripts). 3051.164Sapb rc_log_message "[running $1]" 3061.164Sapb ;; 3071.164Sapb cmd-status) 3081.167Sapb # Marks the end of a child script. 3091.164Sapb # $1 is a command name, $2 is the command's exit status. 3101.164Sapb # If the command failed, report it, and add it to a list. 3111.164Sapb if [ "$2" != 0 ]; then 3121.164Sapb rc_failures="${rc_failures}${rc_failures:+ }$1" 3131.165Schristos msg="$1 $(human_exit_code $2)" 3141.164Sapb rc_log_message "$msg" 3151.164Sapb if ! $rc_silent; then 3161.164Sapb printf "%s\n" "$msg" 3171.164Sapb fi 3181.164Sapb fi 3191.164Sapb # After the mountcritlocal script has finished, it's 3201.164Sapb # OK to flush the log to disk 3211.164Sapb case "$1" in 3221.164Sapb */mountcritlocal) 3231.164Sapb rc_log_flush OK 3241.164Sapb ;; 3251.164Sapb esac 3261.164Sapb ;; 3271.166Sapb nop) 3281.166Sapb # Do nothing. 3291.167Sapb # This has the side effect of flushing partial lines, 3301.167Sapb # and the echo() and printf() functions in rc.subr take 3311.167Sapb # advantage of this. 3321.166Sapb ;; 3331.164Sapb note) 3341.167Sapb # Unlike most metadata messages, which should be used 3351.167Sapb # only by /etc/rc and rc.subr, the "note" message may be 3361.167Sapb # used directly by /etc.rc.d/* and similar scripts. 3371.167Sapb # It adds a note to the log file, without displaying 3381.167Sapb # it to stdout. 3391.164Sapb rc_log_message "[NOTE: $args]" 3401.164Sapb ;; 3411.164Sapb end) 3421.167Sapb # Marks the end of processing, after the last child script. 3431.167Sapb # If any child scripts (or other commands) failed, report them. 3441.164Sapb # 3451.164Sapb if [ -n "$rc_failures" ]; then 3461.164Sapb rc_log_message "[failures]" 3471.164Sapb msg="The following components reported failures:" 3481.164Sapb msg="${msg}${nl}$( echo " ${rc_failures}" | fmt )" 3491.164Sapb msg="${msg}${nl}See ${RC_LOG_FILE} for more information." 3501.164Sapb rc_log_message "${msg}" 3511.164Sapb printf "%s\n" "${msg}" 3521.164Sapb fi 3531.164Sapb # 3541.164Sapb # Report the end date/time, even in silent mode 3551.164Sapb # 3561.164Sapb rc_log_message "[$0 finished at $args]" 3571.164Sapb printf "%s\n" "$args" 3581.164Sapb ;; 3591.164Sapb exit) 3601.167Sapb # Marks an exit from the rc_real_work() function. 3611.167Sapb # This may be a normal or abnormal exit. 3621.167Sapb # 3631.164Sapb rc_log_message "[$0 exiting with status $1]" 3641.164Sapb exit $1 3651.164Sapb ;; 3661.164Sapb interrupted) 3671.167Sapb # Marks an interrupt trapped by the rc_real_work() function. 3681.167Sapb # $args is a human-readable message. 3691.164Sapb rc_log_message "$args" 3701.164Sapb printf "%s\n" "$args" 3711.164Sapb ;; 3721.164Sapb *) 3731.164Sapb # an unrecognised line of metadata 3741.164Sapb rc_log_message "[metadata:${metadata}]" 3751.164Sapb ;; 3761.164Sapb esac 3771.164Sapb} 3781.164Sapb 3791.164Sapb# 3801.164Sapb# rc_log_message string [...] 3811.166Sapb# Write a message to the log file, or buffer it for later. 3821.166Sapb# This function appends a newline to the message. 3831.164Sapb# 3841.164Sapbrc_log_message() 3851.164Sapb{ 3861.164Sapb _rc_log_buffer="${_rc_log_buffer}${*}${nl}" 3871.164Sapb rc_log_flush 3881.164Sapb} 3891.1Scgd 3901.164Sapb# 3911.166Sapb# rc_log_message_n string [...] 3921.166Sapb# Just like rc_log_message, except without appending a newline. 3931.166Sapb# 3941.166Sapbrc_log_message_n() 3951.166Sapb{ 3961.166Sapb _rc_log_buffer="${_rc_log_buffer}${*}" 3971.166Sapb rc_log_flush 3981.166Sapb} 3991.166Sapb 4001.166Sapb# 4011.164Sapb# rc_log_flush [OK|FORCE] 4021.164Sapb# save outstanding messages from $_rc_log_buffer to $RC_LOG_FILE. 4031.164Sapb# 4041.164Sapb# The log file is expected to reside in the /var/run directory, which 4051.164Sapb# may not be writable very early in the boot sequence, and which is 4061.164Sapb# erased a little later in the boot sequence. We therefore avoid 4071.164Sapb# writing to the file until we believe it's safe to do so. We also 4081.164Sapb# assume that it's reasonable to always append to the file, never 4091.164Sapb# truncating it. 4101.164Sapb# 4111.164Sapb# Optional argument $1 may be "OK" to report that writing to the log 4121.164Sapb# file is expected to be safe from now on, or "FORCE" to force writing 4131.164Sapb# to the log file even if it may be unsafe. 4141.164Sapb# 4151.164Sapb# Returns a non-zero status if messages could not be written to the 4161.164Sapb# file. 4171.164Sapb# 4181.164Sapbrc_log_flush() 4191.164Sapb{ 4201.164Sapb # 4211.164Sapb # If $_rc_log_flush_ok is false, then it's probably too early to 4221.164Sapb # write to the log file, so don't do it, unless $1 is "FORCE". 4231.164Sapb # 4241.164Sapb : ${_rc_log_flush_ok=false} 4251.164Sapb case "$1:$_rc_log_flush_ok" in 4261.164Sapb OK:*) 4271.164Sapb _rc_log_flush_ok=true 4281.164Sapb ;; 4291.164Sapb FORCE:*) 4301.164Sapb : OK just this once 4311.164Sapb ;; 4321.164Sapb *:true) 4331.164Sapb : OK 4341.164Sapb ;; 4351.164Sapb *) 4361.164Sapb # it's too early in the boot sequence, so don't flush 4371.164Sapb return 1 4381.164Sapb ;; 4391.164Sapb esac 4401.164Sapb 4411.164Sapb # 4421.164Sapb # Now append the buffer to the file. The buffer should already 4431.164Sapb # contain a trailing newline, so don't add an extra newline. 4441.164Sapb # 4451.164Sapb if [ -n "$_rc_log_buffer" ]; then 4461.164Sapb if { printf "%s" "${_rc_log_buffer}" >>"${RC_LOG_FILE}" ; } \ 4471.164Sapb 2>/dev/null 4481.164Sapb then 4491.164Sapb _rc_log_buffer="" 4501.164Sapb else 4511.164Sapb return 1 4521.164Sapb fi 4531.164Sapb fi 4541.164Sapb return 0 4551.164Sapb} 4561.164Sapb 4571.164Sapb# 4581.164Sapb# Most of the action is in the rc_real_work() and rc_postprocess() 4591.164Sapb# functions. 4601.164Sapb# 4611.164Sapbrc_real_work "$@" 2>&1 | rc_postprocess 4621.164Sapbexit $? 463