Sync usage with man page.
[netbsd-mini2440.git] / etc / rc
blobf4ad8a3c1dbd939bc5af80f166812afa4faaf7b8
1 #!/bin/sh
3 # $NetBSD: rc,v 1.163 2009/04/10 16:18:04 joerg Exp $
5 # rc --
6 # Run the scripts in /etc/rc.d with rcorder, and log output
7 # to /var/run/rc.log.
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.
13 export HOME=/
14 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
15 umask 022
17 if [ -e ./rc.subr ] ; then
18 . ./rc.subr # for testing
19 else
20 . /etc/rc.subr
22 . /etc/rc.conf
23 _rc_conf_loaded=true
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."
29 exit 1
32 if [ "$1" = autoboot ]; then
33 autoboot=yes
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.
41 trap '' INT QUIT
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"
66 # rc_real_work
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
69 # metadata.
71 # The body of this function is defined using (...), not {...}, to force
72 # it to run in a subshell.
74 rc_real_work()
76 stty status '^T'
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.
92 trap : INT
93 trap '_msg="Boot interrupted at $(date)";
94 print_rc_metadata "interrupted:${_msg}";
95 exit 1' QUIT
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
122 # override this.
124 print_rc_metadata "cmd-name:rcorder"
125 scripts=$(for rcd in ${rc_directories:-/etc/rc.d}; do
126 test -d ${rcd} && echo ${rcd}/*;
127 done)
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:$?"
142 done
144 print_rc_metadata "end:$(date)"
145 exit 0
149 # rc_postprocess
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.
178 rc_postprocess()
180 local line
181 local before after
182 local IFS=''
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
190 case "$line" in
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}"
206 esac
207 done
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
212 # here.
214 exit 1
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()
226 local line="$1"
227 rc_log_message "${line}"
228 if $rc_silent; then
229 eval "$rc_silent_cmd"
230 else
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()
244 local metadata="$1"
245 local keyword args
246 local msg
247 local IFS=':'
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#*:}"
255 set -- $args
257 case "$keyword" in
258 start)
259 # $args contains a date/time
260 rc_log_message "[$0 starting at $args]"
261 if ! $rc_silent; then
262 printf "%s\n" "$args"
265 cmd-name)
266 rc_log_message "[running $1]"
268 cmd-status)
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
276 printf "%s\n" "$msg"
279 # After the mountcritlocal script has finished, it's
280 # OK to flush the log to disk
281 case "$1" in
282 */mountcritlocal)
283 rc_log_flush OK
285 esac
287 note)
288 rc_log_message "[NOTE: $args]"
290 end)
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"
308 exit)
309 rc_log_message "[$0 exiting with status $1]"
310 exit $1
312 interrupted)
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}]"
321 esac
325 # rc_log_message string [...]
326 # write a message to the log file, or buffer it for later.
328 rc_log_message()
330 _rc_log_buffer="${_rc_log_buffer}${*}${nl}"
331 rc_log_flush
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
343 # truncating it.
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
350 # file.
352 rc_log_flush()
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
360 OK:*)
361 _rc_log_flush_ok=true
363 FORCE:*)
364 : OK just this once
366 *:true)
367 : OK
370 # it's too early in the boot sequence, so don't flush
371 return 1
373 esac
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}" ; } \
381 2>/dev/null
382 then
383 _rc_log_buffer=""
384 else
385 return 1
388 return 0
392 # Most of the action is in the rc_real_work() and rc_postprocess()
393 # functions.
395 rc_real_work "$@" 2>&1 | rc_postprocess
396 exit $?