3 # $NetBSD: rc,v 1.163 2009/04/10 16:18:04 joerg Exp $
6 # Run the scripts in /etc/rc.d with rcorder, and log output
9 # System startup script run by init(8) on autoboot or after single-user.
10 # Output and error are redirected to console by init, and the console
11 # is the controlling terminal.
14 export PATH
=/sbin
:/bin
:/usr
/sbin
:/usr
/bin
17 if [ -e .
/rc.subr
] ; then
18 . .
/rc.subr
# for testing
25 : ${RC_LOG_FILE:="/var/run/rc.log"}
27 if ! checkyesno rc_configured
; then
28 echo "/etc/rc.conf is not configured. Multiuser boot aborted."
32 if [ "$1" = autoboot
]; then
34 rc_fast
=yes # run_rc_command(): do fast booting
38 # Completely ignore INT and QUIT at the outer level. The rc_real_work()
39 # function should do something different.
44 # This string will be used to mark lines of meta-data sent over the pipe
45 # from the rc_real_work() function to the rc_postprocess() function. Lines
46 # not so marked are assumed to be output from rc.d scripts.
48 # This string is long and unique to ensure that it does not accidentally
49 # appear in output from any rc.d script. It must not contain any
50 # characters that are special to glob expansion ('*', '?', '[', or ']').
52 rc_metadata_prefix
="$0:$$:metadata:";
54 # Child scripts may sometimes want to print directly to the original
55 # stdout and stderr, bypassing the pipe to the postprocessor. These
56 # _rc_*_fd variables are private, shared with /etc/rc.subr, but not
57 # intended to be used directly by child scripts. (Child scripts
58 # may use rc.subr's no_rc_postprocess function.)
60 _rc_original_stdout_fd
=7; export _rc_original_stdout_fd
61 _rc_original_stderr_fd
=8; export _rc_original_stderr_fd
62 eval "exec ${_rc_original_stdout_fd}>&1"
63 eval "exec ${_rc_original_stderr_fd}>&2"
67 # Do the real work. Output from this function will be piped into
68 # rc_postprocess(), and some of the output will be marked as
71 # The body of this function is defined using (...), not {...}, to force
72 # it to run in a subshell.
78 # print_rc_metadata() wants to be able to print to the pipe
79 # that goes to our postprocessor, even if its in a context
80 # with redirected output.
82 _rc_postprocessor_fd
=9 ; export _rc_postprocessor_fd
83 eval "exec ${_rc_postprocessor_fd}>&1"
85 # Print a metadata line when we exit
87 trap 'es=$?; print_rc_metadata "exit:$es"; trap "" 0; exit $es' 0
89 # Set shell to ignore SIGINT, but children will not ignore it.
90 # Shell catches SIGQUIT and returns to single user.
93 trap '_msg="Boot interrupted at $(date)";
94 print_rc_metadata "interrupted:${_msg}";
97 print_rc_metadata
"start:$(date)"
100 # The stop_boot() function in rc.subr may kill $RC_PID. We want
101 # it to kill the subshell running this rc_real_work() function,
102 # rather than killing the parent shell, because we want the
103 # rc_postprocess() function to be able to log the error
104 # without being killed itself.
106 # "$$" is the pid of the top-level shell, not the pid of the
107 # subshell that's executing this function. The command below
108 # tentatively assumes that the parent of the "/bin/sh -c ..."
109 # process will be the current subshell, and then uses "kill -0
110 # ..." to check the result. If the "/bin/sh -c ..." process
111 # fails, or returns the pid of an ephemeral process that exits
112 # before the "kill" command, then we fall back to using "$$".
114 RC_PID
=$
(/bin
/sh
-c 'ps -p $$ -o ppid=') || RC_PID
=$$
115 kill -0 $RC_PID >/dev
/null
2>&1 || RC_PID
=$$
118 # Get a list of all rc.d scripts, and use rcorder to choose
119 # what order to execute them.
121 # For testing, allow RC_FILES_OVERRIDE from the environment to
124 print_rc_metadata
"cmd-name:rcorder"
125 scripts
=$
(for rcd
in ${rc_directories:-/etc/rc.d}; do
126 test -d ${rcd} && echo ${rcd}/*;
128 files
=$
(rcorder
-s nostart
${rc_rcorder_flags} ${scripts})
129 print_rc_metadata
"cmd-status:rcorder:$?"
131 if [ -n "${RC_FILES_OVERRIDE}" ]; then
132 files
="${RC_FILES_OVERRIDE}"
136 # Run the scripts in order.
138 for _rc_elem
in $files; do
139 print_rc_metadata
"cmd-name:$_rc_elem"
140 run_rc_script
$_rc_elem start
141 print_rc_metadata
"cmd-status:$_rc_elem:$?"
144 print_rc_metadata
"end:$(date)"
150 # Post-process the output from the rc_real_work() function. For
151 # each line of input, we have to decide whether to print the line
152 # to the console, print a twiddle on the console, print a line to
153 # the log, or some combination of these.
155 # If rc_silent is true, then suppress most output, instead running
156 # rc_silent_cmd (typically "twiddle") for each line.
158 # The body of this function is defined using (...), not {...}, to force
159 # it to run in a subshell.
161 # We have to deal with the following constraints:
163 # * There may be no writable file systems early in the boot, so
164 # any use of temporary files would be problematic.
166 # * Scripts run during the boot may clear /tmp and/var/run, so even
167 # if they are writable, using those directories too early may be
168 # problematic. We assume that it's safe to write to our log file
169 # after the mountcritlocal script has run.
171 # * /usr/bin/tee cannot be used because the /usr file system may not
172 # be mounted early in the boot.
174 # * All calls to the rc_log_message and rc_log_flush functions must be
175 # from the same subshell, otherwise the use of a shell variable to
176 # buffer log messages will fail.
184 # Try quite hard to flush the log to disk when we exit.
185 trap 'es=$?; rc_log_flush FORCE; trap "" 0; exit $es' 0
187 yesno_to_truefalse rc_silent
2>/dev
/null
189 while read -r line
; do
191 "${rc_metadata_prefix}"*)
192 after
="${line#*"${rc_metadata_prefix}"}"
193 rc_postprocess_metadata
"${after}"
195 *"${rc_metadata_prefix}"*)
196 # magic string is present, but not at the start of
197 # the line. Treat it like two separate lines.
198 before
="${line%"${rc_metadata_prefix}"*}"
199 rc_postprocess_plain_line
"${before}"
200 after
="${line#*"${rc_metadata_prefix}"}"
201 rc_postprocess_metadata
"${after}"
204 rc_postprocess_plain_line
"${line}"
209 # If we get here, then the rc_real_work() function must have
210 # exited uncleanly. A clean exit would have been accompanied by
211 # a line of metadata that would have prevented us from getting
218 # rc_postprocess_plain_line string
219 # $1 is a string representing a line of output from one of the
220 # rc.d scripts. Append the line to the log, and also either
221 # display the line on the console, or run $rc_silent_cmd,
222 # depending on the value of $rc_silent.
224 rc_postprocess_plain_line
()
227 rc_log_message
"${line}"
229 eval "$rc_silent_cmd"
231 printf "%s\n" "${line}"
236 # rc_postprocess_metadata string
237 # $1 is a string containing metadata from the rc_real_work()
238 # function. The rc_metadata_prefix marker should already
239 # have been removed before the string is passed to this function.
240 # Take appropriate action depending on the content of the string.
242 rc_postprocess_metadata
()
249 # given metadata="bleep:foo bar:baz",
250 # set keyword="bleep", args="foo bar:baz",
251 # $1="foo bar", $2="baz"
253 keyword
="${metadata%%:*}"
254 args
="${metadata#*:}"
259 # $args contains a date/time
260 rc_log_message
"[$0 starting at $args]"
261 if ! $rc_silent; then
262 printf "%s\n" "$args"
266 rc_log_message
"[running $1]"
269 # $1 is a command name, $2 is the command's exit status.
270 # If the command failed, report it, and add it to a list.
271 if [ "$2" != 0 ]; then
272 rc_failures
="${rc_failures}${rc_failures:+ }$1"
273 msg
="$1 reported failure status $2"
274 rc_log_message
"$msg"
275 if ! $rc_silent; then
279 # After the mountcritlocal script has finished, it's
280 # OK to flush the log to disk
288 rc_log_message
"[NOTE: $args]"
292 # If any scripts (or other commands) failed, report them.
294 if [ -n "$rc_failures" ]; then
295 rc_log_message
"[failures]"
296 msg
="The following components reported failures:"
297 msg
="${msg}${nl}$( echo " ${rc_failures}" |
fmt )"
298 msg="${msg}${nl}See ${RC_LOG_FILE} for more information.
"
299 rc_log_message "${msg}"
300 printf "%s
\n" "${msg}"
303 # Report the end date/time, even in silent mode
305 rc_log_message "[$0 finished
at $args]"
306 printf "%s
\n" "$args"
309 rc_log_message "[$0 exiting with status
$1]"
313 # $args is a human-readable message
314 rc_log_message "$args"
315 printf "%s
\n" "$args"
318 # an unrecognised line of metadata
319 rc_log_message "[metadata
:${metadata}]"
325 # rc_log_message string [...]
326 # write a message to the log file, or buffer it for later.
330 _rc_log_buffer="${_rc_log_buffer}${*}${nl}"
335 # rc_log_flush [OK|FORCE]
336 # save outstanding messages from $_rc_log_buffer to $RC_LOG_FILE.
338 # The log file is expected to reside in the /var/run directory, which
339 # may not be writable very early in the boot sequence, and which is
340 # erased a little later in the boot sequence. We therefore avoid
341 # writing to the file until we believe it's safe to do so. We also
342 # assume that it's reasonable to always append to the file, never
345 # Optional argument $1 may be "OK
" to report that writing to the log
346 # file is expected to be safe from now on, or "FORCE
" to force writing
347 # to the log file even if it may be unsafe.
349 # Returns a non-zero status if messages could not be written to the
355 # If $_rc_log_flush_ok is false, then it's probably too early to
356 # write to the log file, so don't do it, unless $1 is "FORCE
".
358 : ${_rc_log_flush_ok=false}
359 case "$1:$_rc_log_flush_ok" in
361 _rc_log_flush_ok=true
370 # it's too early in the boot sequence, so don't flush
376 # Now append the buffer to the file. The buffer should already
377 # contain a trailing newline, so don't add an extra newline.
379 if [ -n "$_rc_log_buffer" ]; then
380 if { printf "%s
" "${_rc_log_buffer}" >>"${RC_LOG_FILE}" ; } \
392 # Most of the action is in the rc_real_work() and rc_postprocess()
395 rc_real_work
"$@" 2>&1 | rc_postprocess