1 1.149 lukem #!/bin/sh 2 1.149 lukem # 3 1.176 thorpej # $NetBSD: rc,v 1.176 2026/08/20 14:43:31 thorpej Exp $ 4 1.149 lukem # 5 1.154 lukem # rc -- 6 1.164 apb # Run the scripts in /etc/rc.d with rcorder, and log output 7 1.164 apb # to /var/run/rc.log. 8 1.149 lukem 9 1.154 lukem # System startup script run by init(8) on autoboot or after single-user. 10 1.149 lukem # Output and error are redirected to console by init, and the console 11 1.149 lukem # is the controlling terminal. 12 1.1 cgd 13 1.149 lukem export HOME=/ 14 1.149 lukem export PATH=/sbin:/bin:/usr/sbin:/usr/bin 15 1.157 lukem umask 022 16 1.1 cgd 17 1.164 apb if [ -e ./rc.subr ] ; then 18 1.164 apb . ./rc.subr # for testing 19 1.164 apb else 20 1.164 apb . /etc/rc.subr 21 1.164 apb fi 22 1.149 lukem . /etc/rc.conf 23 1.162 lukem _rc_conf_loaded=true 24 1.150 enami 25 1.164 apb : ${RC_LOG_FILE:="/var/run/rc.log"} 26 1.164 apb 27 1.166 apb # rc.subr redefines echo and printf. Undo that here. 28 1.166 apb unset echo ; unalias echo 29 1.166 apb unset printf ; unalias printf 30 1.166 apb 31 1.150 enami if ! checkyesno rc_configured; then 32 1.150 enami echo "/etc/rc.conf is not configured. Multiuser boot aborted." 33 1.150 enami exit 1 34 1.150 enami fi 35 1.50 thorpej 36 1.149 lukem if [ "$1" = autoboot ]; then 37 1.149 lukem autoboot=yes 38 1.160 lukem rc_fast=yes # run_rc_command(): do fast booting 39 1.50 thorpej fi 40 1.107 tron 41 1.164 apb # 42 1.164 apb # Completely ignore INT and QUIT at the outer level. The rc_real_work() 43 1.164 apb # function should do something different. 44 1.164 apb # 45 1.164 apb trap '' INT QUIT 46 1.1 cgd 47 1.149 lukem # 48 1.164 apb # This string will be used to mark lines of meta-data sent over the pipe 49 1.164 apb # from the rc_real_work() function to the rc_postprocess() function. Lines 50 1.164 apb # not so marked are assumed to be output from rc.d scripts. 51 1.164 apb # 52 1.164 apb # This string is long and unique to ensure that it does not accidentally 53 1.164 apb # appear in output from any rc.d script. It must not contain any 54 1.164 apb # characters that are special to glob expansion ('*', '?', '[', or ']'). 55 1.164 apb # 56 1.164 apb rc_metadata_prefix="$0:$$:metadata:"; 57 1.1 cgd 58 1.164 apb # Child scripts may sometimes want to print directly to the original 59 1.164 apb # stdout and stderr, bypassing the pipe to the postprocessor. These 60 1.164 apb # _rc_*_fd variables are private, shared with /etc/rc.subr, but not 61 1.164 apb # intended to be used directly by child scripts. (Child scripts 62 1.164 apb # may use rc.subr's no_rc_postprocess function.) 63 1.164 apb # 64 1.164 apb _rc_original_stdout_fd=7; export _rc_original_stdout_fd 65 1.164 apb _rc_original_stderr_fd=8; export _rc_original_stderr_fd 66 1.164 apb eval "exec ${_rc_original_stdout_fd}>&1" 67 1.164 apb eval "exec ${_rc_original_stderr_fd}>&2" 68 1.172 christos fdflags -s +cloexec 7 8 69 1.156 lukem 70 1.164 apb # 71 1.164 apb # rc_real_work 72 1.164 apb # Do the real work. Output from this function will be piped into 73 1.164 apb # rc_postprocess(), and some of the output will be marked as 74 1.164 apb # metadata. 75 1.164 apb # 76 1.164 apb # The body of this function is defined using (...), not {...}, to force 77 1.164 apb # it to run in a subshell. 78 1.164 apb # 79 1.164 apb rc_real_work() 80 1.164 apb ( 81 1.164 apb stty status '^T' 82 1.164 apb 83 1.164 apb # print_rc_metadata() wants to be able to print to the pipe 84 1.164 apb # that goes to our postprocessor, even if its in a context 85 1.164 apb # with redirected output. 86 1.164 apb # 87 1.164 apb _rc_postprocessor_fd=9 ; export _rc_postprocessor_fd 88 1.169 apb _rc_pid=$$ ; export _rc_pid 89 1.164 apb eval "exec ${_rc_postprocessor_fd}>&1" 90 1.173 christos fdflags -s +cloexec 9 91 1.164 apb 92 1.164 apb # Print a metadata line when we exit 93 1.164 apb # 94 1.164 apb trap 'es=$?; print_rc_metadata "exit:$es"; trap "" 0; exit $es' 0 95 1.164 apb 96 1.164 apb # Set shell to ignore SIGINT, but children will not ignore it. 97 1.164 apb # Shell catches SIGQUIT and returns to single user. 98 1.164 apb # 99 1.164 apb trap : INT 100 1.164 apb trap '_msg="Boot interrupted at $(date)"; 101 1.164 apb print_rc_metadata "interrupted:${_msg}"; 102 1.164 apb exit 1' QUIT 103 1.164 apb 104 1.164 apb print_rc_metadata "start:$(date)" 105 1.164 apb 106 1.164 apb # 107 1.164 apb # The stop_boot() function in rc.subr may kill $RC_PID. We want 108 1.164 apb # it to kill the subshell running this rc_real_work() function, 109 1.164 apb # rather than killing the parent shell, because we want the 110 1.164 apb # rc_postprocess() function to be able to log the error 111 1.164 apb # without being killed itself. 112 1.164 apb # 113 1.164 apb # "$$" is the pid of the top-level shell, not the pid of the 114 1.164 apb # subshell that's executing this function. The command below 115 1.164 apb # tentatively assumes that the parent of the "/bin/sh -c ..." 116 1.164 apb # process will be the current subshell, and then uses "kill -0 117 1.164 apb # ..." to check the result. If the "/bin/sh -c ..." process 118 1.164 apb # fails, or returns the pid of an ephemeral process that exits 119 1.164 apb # before the "kill" command, then we fall back to using "$$". 120 1.164 apb # 121 1.164 apb RC_PID=$(/bin/sh -c 'ps -p $$ -o ppid=') || RC_PID=$$ 122 1.164 apb kill -0 $RC_PID >/dev/null 2>&1 || RC_PID=$$ 123 1.164 apb 124 1.164 apb # 125 1.168 apb # As long as process $RC_PID is still running, send a "nop" 126 1.168 apb # metadata message to the postprocessor every few seconds. 127 1.168 apb # This should help flush partial lines that may appear when 128 1.168 apb # rc.d scripts that are NOT marked with "KEYWORD: interactive" 129 1.168 apb # nevertheless attempt to print prompts and wait for input. 130 1.168 apb # 131 1.168 apb ( 132 1.170 apb # First detach from tty, to avoid intercepting SIGINFO. 133 1.170 apb eval "exec ${_rc_original_stdout_fd}<&-" 134 1.170 apb eval "exec ${_rc_original_stderr_fd}<&-" 135 1.170 apb exec </dev/null >/dev/null 2>&1 136 1.168 apb while kill -0 $RC_PID ; do 137 1.168 apb print_rc_metadata "nop" 138 1.168 apb sleep 3 139 1.168 apb done 140 1.168 apb ) & 141 1.168 apb 142 1.168 apb # 143 1.176 thorpej # Get a list of all rc.d scripts and the order in which to 144 1.176 thorpej # execute them. 145 1.176 thorpej # 146 1.176 thorpej if ! _rc_order_cache_is_valid; then 147 1.176 thorpej _rc_update_rcorder_cache=YES 148 1.176 thorpej _rc_order_build_list 149 1.176 thorpej else 150 1.176 thorpej _rc_update_rcorder_cache=NO 151 1.176 thorpej fi 152 1.176 thorpej 153 1.176 thorpej # 154 1.176 thorpej # $_rc_ordered_script_list now contains the rc.d scripts 155 1.176 thorpej # to process. 156 1.176 thorpej # 157 1.176 thorpej files="$_rc_ordered_script_list" 158 1.176 thorpej 159 1.164 apb # 160 1.164 apb # For testing, allow RC_FILES_OVERRIDE from the environment to 161 1.164 apb # override this. 162 1.164 apb # 163 1.164 apb if [ -n "${RC_FILES_OVERRIDE}" ]; then 164 1.176 thorpej _rc_update_rcorder_cache=NO 165 1.164 apb files="${RC_FILES_OVERRIDE}" 166 1.164 apb fi 167 1.164 apb 168 1.164 apb # 169 1.164 apb # Run the scripts in order. 170 1.164 apb # 171 1.164 apb for _rc_elem in $files; do 172 1.164 apb print_rc_metadata "cmd-name:$_rc_elem" 173 1.176 thorpej run_rc_script "$_rc_elem" start 174 1.164 apb print_rc_metadata "cmd-status:$_rc_elem:$?" 175 1.164 apb done 176 1.164 apb 177 1.176 thorpej if [ "$_rc_update_rcorder_cache" = YES ]; then 178 1.176 thorpej _rc_order_save_cache 179 1.176 thorpej fi 180 1.176 thorpej 181 1.164 apb print_rc_metadata "end:$(date)" 182 1.164 apb exit 0 183 1.164 apb ) 184 1.155 lukem 185 1.164 apb # 186 1.164 apb # rc_postprocess 187 1.164 apb # Post-process the output from the rc_real_work() function. For 188 1.164 apb # each line of input, we have to decide whether to print the line 189 1.164 apb # to the console, print a twiddle on the console, print a line to 190 1.164 apb # the log, or some combination of these. 191 1.164 apb # 192 1.164 apb # If rc_silent is true, then suppress most output, instead running 193 1.164 apb # rc_silent_cmd (typically "twiddle") for each line. 194 1.164 apb # 195 1.164 apb # The body of this function is defined using (...), not {...}, to force 196 1.164 apb # it to run in a subshell. 197 1.164 apb # 198 1.164 apb # We have to deal with the following constraints: 199 1.164 apb # 200 1.164 apb # * There may be no writable file systems early in the boot, so 201 1.164 apb # any use of temporary files would be problematic. 202 1.164 apb # 203 1.164 apb # * Scripts run during the boot may clear /tmp and/var/run, so even 204 1.164 apb # if they are writable, using those directories too early may be 205 1.164 apb # problematic. We assume that it's safe to write to our log file 206 1.175 martin # after the CRITLOCALMOUNTED script has run. 207 1.164 apb # 208 1.164 apb # * /usr/bin/tee cannot be used because the /usr file system may not 209 1.164 apb # be mounted early in the boot. 210 1.164 apb # 211 1.164 apb # * All calls to the rc_log_message and rc_log_flush functions must be 212 1.164 apb # from the same subshell, otherwise the use of a shell variable to 213 1.164 apb # buffer log messages will fail. 214 1.164 apb # 215 1.164 apb rc_postprocess() 216 1.164 apb ( 217 1.164 apb local line 218 1.164 apb local before after 219 1.164 apb local IFS='' 220 1.164 apb 221 1.164 apb # Try quite hard to flush the log to disk when we exit. 222 1.164 apb trap 'es=$?; rc_log_flush FORCE; trap "" 0; exit $es' 0 223 1.164 apb 224 1.164 apb yesno_to_truefalse rc_silent 2>/dev/null 225 1.164 apb 226 1.164 apb while read -r line ; do 227 1.164 apb case "$line" in 228 1.164 apb "${rc_metadata_prefix}"*) 229 1.164 apb after="${line#*"${rc_metadata_prefix}"}" 230 1.164 apb rc_postprocess_metadata "${after}" 231 1.164 apb ;; 232 1.164 apb *"${rc_metadata_prefix}"*) 233 1.164 apb # magic string is present, but not at the start of 234 1.166 apb # the line. Treat it as a partial line of 235 1.166 apb # ordinary data, followed by a line of metadata. 236 1.164 apb before="${line%"${rc_metadata_prefix}"*}" 237 1.166 apb rc_postprocess_partial_line "${before}" 238 1.164 apb after="${line#*"${rc_metadata_prefix}"}" 239 1.164 apb rc_postprocess_metadata "${after}" 240 1.164 apb ;; 241 1.164 apb *) 242 1.164 apb rc_postprocess_plain_line "${line}" 243 1.164 apb ;; 244 1.164 apb esac 245 1.164 apb done 246 1.164 apb 247 1.164 apb # If we get here, then the rc_real_work() function must have 248 1.164 apb # exited uncleanly. A clean exit would have been accompanied by 249 1.164 apb # a line of metadata that would have prevented us from getting 250 1.164 apb # here. 251 1.164 apb # 252 1.164 apb exit 1 253 1.164 apb ) 254 1.164 apb 255 1.164 apb # 256 1.164 apb # rc_postprocess_plain_line string 257 1.164 apb # $1 is a string representing a line of output from one of the 258 1.164 apb # rc.d scripts. Append the line to the log, and also either 259 1.164 apb # display the line on the console, or run $rc_silent_cmd, 260 1.164 apb # depending on the value of $rc_silent. 261 1.164 apb # 262 1.164 apb rc_postprocess_plain_line() 263 1.164 apb { 264 1.164 apb local line="$1" 265 1.164 apb rc_log_message "${line}" 266 1.164 apb if $rc_silent; then 267 1.164 apb eval "$rc_silent_cmd" 268 1.164 apb else 269 1.164 apb printf "%s\n" "${line}" 270 1.164 apb fi 271 1.164 apb } 272 1.164 apb 273 1.164 apb # 274 1.166 apb # rc_postprocess_partial_line string 275 1.166 apb # This is just like rc_postprocess_plain_line, except that 276 1.166 apb # a newline is not appended to the string. 277 1.166 apb # 278 1.166 apb rc_postprocess_partial_line() 279 1.166 apb { 280 1.166 apb local line="$1" 281 1.166 apb rc_log_message_n "${line}" 282 1.166 apb if $rc_silent; then 283 1.166 apb eval "$rc_silent_cmd" 284 1.166 apb else 285 1.166 apb printf "%s" "${line}" 286 1.166 apb fi 287 1.166 apb } 288 1.166 apb 289 1.166 apb # 290 1.164 apb # rc_postprocess_metadata string 291 1.164 apb # $1 is a string containing metadata from the rc_real_work() 292 1.164 apb # function. The rc_metadata_prefix marker should already 293 1.164 apb # have been removed before the string is passed to this function. 294 1.164 apb # Take appropriate action depending on the content of the string. 295 1.164 apb # 296 1.164 apb rc_postprocess_metadata() 297 1.164 apb { 298 1.164 apb local metadata="$1" 299 1.164 apb local keyword args 300 1.164 apb local msg 301 1.164 apb local IFS=':' 302 1.164 apb 303 1.164 apb # given metadata="bleep:foo bar:baz", 304 1.164 apb # set keyword="bleep", args="foo bar:baz", 305 1.164 apb # $1="foo bar", $2="baz" 306 1.164 apb # 307 1.164 apb keyword="${metadata%%:*}" 308 1.164 apb args="${metadata#*:}" 309 1.164 apb set -- $args 310 1.164 apb 311 1.164 apb case "$keyword" in 312 1.164 apb start) 313 1.167 apb # Marks the start of the entire /etc/rc script. 314 1.167 apb # $args contains a date/time. 315 1.164 apb rc_log_message "[$0 starting at $args]" 316 1.164 apb if ! $rc_silent; then 317 1.164 apb printf "%s\n" "$args" 318 1.164 apb fi 319 1.164 apb ;; 320 1.164 apb cmd-name) 321 1.167 apb # Marks the start of a child script (usually one of 322 1.167 apb # the /etc/rc.d/* scripts). 323 1.164 apb rc_log_message "[running $1]" 324 1.164 apb ;; 325 1.164 apb cmd-status) 326 1.167 apb # Marks the end of a child script. 327 1.164 apb # $1 is a command name, $2 is the command's exit status. 328 1.164 apb # If the command failed, report it, and add it to a list. 329 1.164 apb if [ "$2" != 0 ]; then 330 1.164 apb rc_failures="${rc_failures}${rc_failures:+ }$1" 331 1.165 christos msg="$1 $(human_exit_code $2)" 332 1.164 apb rc_log_message "$msg" 333 1.164 apb if ! $rc_silent; then 334 1.164 apb printf "%s\n" "$msg" 335 1.164 apb fi 336 1.164 apb fi 337 1.175 martin # After the CRITLOCALMOUNTED script has finished, it's 338 1.164 apb # OK to flush the log to disk 339 1.164 apb case "$1" in 340 1.175 martin */CRITLOCALMOUNTED) 341 1.164 apb rc_log_flush OK 342 1.164 apb ;; 343 1.164 apb esac 344 1.164 apb ;; 345 1.166 apb nop) 346 1.166 apb # Do nothing. 347 1.167 apb # This has the side effect of flushing partial lines, 348 1.167 apb # and the echo() and printf() functions in rc.subr take 349 1.167 apb # advantage of this. 350 1.166 apb ;; 351 1.164 apb note) 352 1.167 apb # Unlike most metadata messages, which should be used 353 1.167 apb # only by /etc/rc and rc.subr, the "note" message may be 354 1.167 apb # used directly by /etc.rc.d/* and similar scripts. 355 1.167 apb # It adds a note to the log file, without displaying 356 1.167 apb # it to stdout. 357 1.164 apb rc_log_message "[NOTE: $args]" 358 1.164 apb ;; 359 1.164 apb end) 360 1.167 apb # Marks the end of processing, after the last child script. 361 1.167 apb # If any child scripts (or other commands) failed, report them. 362 1.164 apb # 363 1.164 apb if [ -n "$rc_failures" ]; then 364 1.164 apb rc_log_message "[failures]" 365 1.164 apb msg="The following components reported failures:" 366 1.164 apb msg="${msg}${nl}$( echo " ${rc_failures}" | fmt )" 367 1.164 apb msg="${msg}${nl}See ${RC_LOG_FILE} for more information." 368 1.164 apb rc_log_message "${msg}" 369 1.164 apb printf "%s\n" "${msg}" 370 1.164 apb fi 371 1.164 apb # 372 1.164 apb # Report the end date/time, even in silent mode 373 1.164 apb # 374 1.164 apb rc_log_message "[$0 finished at $args]" 375 1.164 apb printf "%s\n" "$args" 376 1.164 apb ;; 377 1.164 apb exit) 378 1.167 apb # Marks an exit from the rc_real_work() function. 379 1.167 apb # This may be a normal or abnormal exit. 380 1.167 apb # 381 1.164 apb rc_log_message "[$0 exiting with status $1]" 382 1.164 apb exit $1 383 1.164 apb ;; 384 1.164 apb interrupted) 385 1.167 apb # Marks an interrupt trapped by the rc_real_work() function. 386 1.167 apb # $args is a human-readable message. 387 1.164 apb rc_log_message "$args" 388 1.164 apb printf "%s\n" "$args" 389 1.164 apb ;; 390 1.164 apb *) 391 1.164 apb # an unrecognised line of metadata 392 1.164 apb rc_log_message "[metadata:${metadata}]" 393 1.164 apb ;; 394 1.164 apb esac 395 1.164 apb } 396 1.164 apb 397 1.164 apb # 398 1.164 apb # rc_log_message string [...] 399 1.166 apb # Write a message to the log file, or buffer it for later. 400 1.166 apb # This function appends a newline to the message. 401 1.164 apb # 402 1.164 apb rc_log_message() 403 1.164 apb { 404 1.164 apb _rc_log_buffer="${_rc_log_buffer}${*}${nl}" 405 1.164 apb rc_log_flush 406 1.164 apb } 407 1.1 cgd 408 1.164 apb # 409 1.166 apb # rc_log_message_n string [...] 410 1.166 apb # Just like rc_log_message, except without appending a newline. 411 1.166 apb # 412 1.166 apb rc_log_message_n() 413 1.166 apb { 414 1.166 apb _rc_log_buffer="${_rc_log_buffer}${*}" 415 1.166 apb rc_log_flush 416 1.166 apb } 417 1.166 apb 418 1.166 apb # 419 1.164 apb # rc_log_flush [OK|FORCE] 420 1.164 apb # save outstanding messages from $_rc_log_buffer to $RC_LOG_FILE. 421 1.164 apb # 422 1.164 apb # The log file is expected to reside in the /var/run directory, which 423 1.164 apb # may not be writable very early in the boot sequence, and which is 424 1.164 apb # erased a little later in the boot sequence. We therefore avoid 425 1.164 apb # writing to the file until we believe it's safe to do so. We also 426 1.164 apb # assume that it's reasonable to always append to the file, never 427 1.164 apb # truncating it. 428 1.164 apb # 429 1.164 apb # Optional argument $1 may be "OK" to report that writing to the log 430 1.164 apb # file is expected to be safe from now on, or "FORCE" to force writing 431 1.164 apb # to the log file even if it may be unsafe. 432 1.164 apb # 433 1.164 apb # Returns a non-zero status if messages could not be written to the 434 1.164 apb # file. 435 1.164 apb # 436 1.164 apb rc_log_flush() 437 1.164 apb { 438 1.164 apb # 439 1.164 apb # If $_rc_log_flush_ok is false, then it's probably too early to 440 1.164 apb # write to the log file, so don't do it, unless $1 is "FORCE". 441 1.164 apb # 442 1.164 apb : ${_rc_log_flush_ok=false} 443 1.164 apb case "$1:$_rc_log_flush_ok" in 444 1.164 apb OK:*) 445 1.164 apb _rc_log_flush_ok=true 446 1.164 apb ;; 447 1.164 apb FORCE:*) 448 1.164 apb : OK just this once 449 1.164 apb ;; 450 1.164 apb *:true) 451 1.164 apb : OK 452 1.164 apb ;; 453 1.164 apb *) 454 1.164 apb # it's too early in the boot sequence, so don't flush 455 1.164 apb return 1 456 1.164 apb ;; 457 1.164 apb esac 458 1.164 apb 459 1.164 apb # 460 1.164 apb # Now append the buffer to the file. The buffer should already 461 1.164 apb # contain a trailing newline, so don't add an extra newline. 462 1.164 apb # 463 1.164 apb if [ -n "$_rc_log_buffer" ]; then 464 1.164 apb if { printf "%s" "${_rc_log_buffer}" >>"${RC_LOG_FILE}" ; } \ 465 1.164 apb 2>/dev/null 466 1.164 apb then 467 1.164 apb _rc_log_buffer="" 468 1.164 apb else 469 1.164 apb return 1 470 1.164 apb fi 471 1.164 apb fi 472 1.164 apb return 0 473 1.164 apb } 474 1.164 apb 475 1.164 apb # 476 1.164 apb # Most of the action is in the rc_real_work() and rc_postprocess() 477 1.164 apb # functions. 478 1.164 apb # 479 1.164 apb rc_real_work "$@" 2>&1 | rc_postprocess 480 1.164 apb exit $? 481