1 # $NetBSD: rc.subr,v 1.88 2011/08/11 22:52:47 apb Exp $
3 # Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
6 # This code is derived from software contributed to The NetBSD Foundation
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
31 # functions used by various rc scripts
34 : ${rcvar_manpage:='rc.conf(5)'}
35 : ${RC_PID:=$$} ; export RC_PID
45 # Test $1 variable, and warn if not set to YES or NO.
46 # Return 0 if it's "yes" (et al), nonzero otherwise.
53 # "yes", "true", "on", or "1"
54 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
58 # "no", "false", "off", or "0"
59 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
63 warn "\$${1} is not set properly - see ${rcvar_manpage}."
70 # yesno_to_truefalse var
71 # Convert the value of a variable from any of the values
72 # understood by checkyesno() to "true" or "false".
77 if checkyesno $var; then
88 # print the list in reverse order
94 _revlist="$_revfile $_revlist"
100 # If booting directly to multiuser, send SIGTERM to
101 # the parent (/etc/rc) to abort the boot.
102 # Otherwise just exit.
106 if [ "$autoboot" = yes ]; then
107 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
114 # mount_critical_filesystems type
115 # Go through the list of critical filesystems as provided in
116 # the rc.conf(5) variable $critical_filesystems_${type}, checking
117 # each one to see if it is mounted, and if it is not, mounting it.
118 # It's not an error if file systems prefixed with "OPTIONAL:"
119 # are not mentioned in /etc/fstab.
121 mount_critical_filesystems()
123 eval _fslist=\$critical_filesystems_${1}
125 for _fs in $_fslist; do
134 # look for a line like "${fs} on * type *"
135 # or "* on ${fs} type *" in the output from mount.
136 case "${nl}$( mount )${nl}" in
137 *" on ${_fs} type "*)
146 "note:File system ${_fs} was already mounted"
148 _mount_output=$( mount $_fs 2>&1 )
150 case "$_mount_output" in
152 # multiple lines can't be good,
153 # not even if $_optional is true
155 *'unknown special file or file system'*)
159 "note:Optional file system ${_fs} is not present"
165 if [ -n "$_mount_output" ]; then
166 printf >&2 "%s\n" "$_mount_output"
168 if [ "$_mount_es" != 0 ]; then
169 _mountcrit_es="$_mount_es"
173 return $_mountcrit_es
177 # check_pidfile pidfile procname [interpreter]
178 # Parses the first line of pidfile for a PID, and ensures
179 # that the process is running and matches procname.
180 # Prints the matching PID upon success, nothing otherwise.
181 # interpreter is optional; see _find_processes() for details.
188 if [ -z "$_pidfile" -o -z "$_procname" ]; then
189 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
191 if [ ! -f $_pidfile ]; then
194 read _pid _junk < $_pidfile
195 if [ -z "$_pid" ]; then
198 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
202 # check_process procname [interpreter]
203 # Ensures that a process (or processes) named procname is running.
204 # Prints a list of matching PIDs.
205 # interpreter is optional; see _find_processes() for details.
211 if [ -z "$_procname" ]; then
212 err 3 'USAGE: check_process procname [interpreter]'
214 _find_processes $_procname ${_interpreter:-.} '-ax'
218 # _find_processes procname interpreter psargs
219 # Search for procname in the output of ps generated by psargs.
220 # Prints the PIDs of any matching processes, space separated.
222 # If interpreter == ".", check the following variations of procname
223 # against the first word of each command:
225 # `basename procname`
226 # `basename procname` + ":"
227 # "(" + `basename procname` + ")"
229 # If interpreter != ".", read the first line of procname, remove the
230 # leading #!, normalise whitespace, append procname, and attempt to
231 # match that against each command, either as is, or with extra words
232 # at the end. As an alternative, to deal with interpreted daemons
233 # using perl, the basename of the interpreter plus a colon is also
234 # tried as the prefix to procname.
238 if [ $# -ne 3 ]; then
239 err 3 'USAGE: _find_processes procname interpreter psargs'
246 _procnamebn=${_procname##*/}
247 if [ $_interpreter != "." ]; then # an interpreted script
248 read _interp < ${_chroot:-}/$_procname # read interpreter name
249 _interp=${_interp#\#!} # strip #!
251 if [ $1 = "/usr/bin/env" ]; then
255 _interp="${1##*/} $_procname"
257 _interp="$* $_procname"
259 if [ $_interpreter != $1 ]; then
260 warn "\$command_interpreter $_interpreter != $1"
264 _fp_match='case "$_argv" in
265 ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
266 else # a normal daemon
267 _fp_args='_arg0 _argv'
268 _fp_match='case "$_arg0" in
269 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
273 ps -o "pid,command" '"$_psargs"' |
274 while read _npid '"$_fp_args"'; do
278 esac ; '"$_fp_match"'
279 echo -n "$_pref$_npid" ;
285 #echo 1>&2 "proccheck is :$_proccheck:"
290 # wait_for_pids pid [pid ...]
291 # spins until none of the pids exist
296 if [ -z "$_list" ]; then
303 if kill -0 $_j 2>/dev/null; then
304 _nlist="${_nlist}${_nlist:+ }$_j"
307 if [ -z "$_nlist" ]; then
311 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
315 if [ -n "$_prefix" ]; then
321 # run_rc_command argument [parameters]
322 # Search for argument in the list of supported commands, which is:
323 # "start stop restart rcvar status poll ${extra_commands}"
324 # If there's a match, run ${argument}_cmd or the default method
325 # (see below), and pass the optional list of parameters to it.
327 # If argument has a given prefix, then change the operation as follows:
330 # fast Skip the pid check, and set rc_fast=yes
331 # force Set ${rcvar} to YES, and set rc_force=yes
332 # one Set ${rcvar} to YES
334 # The following globals are used:
336 # Name Needed Purpose
337 # ---- ------ -------
338 # name y Name of script.
340 # command n Full path to command.
341 # Not needed if ${rc_arg}_cmd is set for
344 # command_args n Optional args/shell directives for command.
346 # command_interpreter n If not empty, command is interpreted, so
347 # call check_{pidfile,process}() appropriately.
349 # extra_commands n List of extra commands supported.
351 # pidfile n If set, use check_pidfile $pidfile $command,
352 # otherwise use check_process $command.
353 # In either case, only check if $command is set.
355 # procname n Process name to check for instead of $command.
357 # rcvar n This is checked with checkyesno to determine
358 # if the action should be run.
360 # ${name}_chroot n Directory to chroot to before running ${command}
361 # Requires /usr to be mounted.
363 # ${name}_chdir n Directory to cd to before running ${command}
364 # (if not using ${name}_chroot).
366 # ${name}_flags n Arguments to call ${command} with.
367 # NOTE: $flags from the parent environment
368 # can be used to override this.
370 # ${name}_env n Additional environment variable settings
371 # for running ${command}
373 # ${name}_nice n Nice level to run ${command} at.
375 # ${name}_user n User to run ${command} as, using su(1) if not
376 # using ${name}_chroot.
377 # Requires /usr to be mounted.
379 # ${name}_group n Group to run chrooted ${command} as.
380 # Requires /usr to be mounted.
382 # ${name}_groups n Comma separated list of supplementary groups
383 # to run the chrooted ${command} with.
384 # Requires /usr to be mounted.
386 # ${rc_arg}_cmd n If set, use this as the method when invoked;
387 # Otherwise, use default command (see below)
389 # ${rc_arg}_precmd n If set, run just before performing the
390 # ${rc_arg}_cmd method in the default
391 # operation (i.e, after checking for required
392 # bits and process (non)existence).
393 # If this completes with a non-zero exit code,
394 # don't run ${rc_arg}_cmd.
396 # ${rc_arg}_postcmd n If set, run just after performing the
397 # ${rc_arg}_cmd method, if that method
398 # returned a zero exit code.
400 # required_dirs n If set, check for the existence of the given
401 # directories before running the default
404 # required_files n If set, check for the readability of the given
405 # files before running the default (re)start
408 # required_vars n If set, perform checkyesno on each of the
409 # listed variables before running the default
412 # Default behaviour for a given argument, if no override method is
415 # Argument Default behaviour
416 # -------- -----------------
417 # start if !running && checkyesno ${rcvar}
421 # rc_pid=$(check_pidfile $pidfile $command)
423 # rc_pid=$(check_process $command)
424 # kill $sig_stop $rc_pid
425 # wait_for_pids $rc_pid
426 # ($sig_stop defaults to TERM.)
428 # reload Similar to stop, except use $sig_reload instead,
429 # and doesn't wait_for_pids.
430 # $sig_reload defaults to HUP.
432 # restart Run `stop' then `start'.
434 # status Show if ${command} is running, etc.
436 # poll Wait for ${command} to exit.
438 # rcvar Display what rc.conf variable is used (if any).
440 # Variables available to methods, and after run_rc_command() has
445 # rc_arg Argument to command, after fast/force/one processing
448 # rc_flags Flags to start the default command with.
449 # Defaults to ${name}_flags, unless overridden
450 # by $flags from the environment.
451 # This variable may be changed by the precmd method.
453 # rc_pid PID of command (if appropriate)
455 # rc_fast Not empty if "fast" was provided (q.v.)
457 # rc_force Not empty if "force" was provided (q.v.)
463 if [ -z "$name" ]; then
464 err 3 'run_rc_command: $name is not set.'
469 fast*) # "fast" prefix; don't check pid
470 rc_arg=${rc_arg#fast}
473 force*) # "force" prefix; always run
476 rc_arg=${rc_arg#${_rc_prefix}}
477 if [ -n "${rcvar}" ]; then
481 one*) # "one" prefix; set ${rcvar}=yes
483 rc_arg=${rc_arg#${_rc_prefix}}
484 if [ -n "${rcvar}" ]; then
490 _keywords="start stop restart rcvar"
491 if [ -n "$extra_commands" ]; then
492 _keywords="${_keywords} ${extra_commands}"
496 _procname=${procname:-${command}}
498 # setup pid check command if not fast
499 if [ -z "$rc_fast" -a -n "$_procname" ]; then
500 if [ -n "$pidfile" ]; then
501 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
503 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
505 if [ -n "$_pidcmd" ]; then
506 _keywords="${_keywords} status poll"
510 if [ -z "$rc_arg" ]; then
511 rc_usage "$_keywords"
513 shift # remove $rc_arg from the positional parameters
515 if [ -n "$flags" ]; then # allow override from environment
518 eval rc_flags=\$${name}_flags
520 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
521 _nice=\$${name}_nice _user=\$${name}_user \
522 _group=\$${name}_group _groups=\$${name}_groups \
523 _env=\"\$${name}_env\"
525 if [ -n "$_user" ]; then # unset $_user if running as that user
526 if [ "$_user" = "$(id -un)" ]; then
531 # if ${rcvar} is set, and $1 is not
533 # checkyesno ${rcvar}
534 # and return if that failed or warn
535 # user and exit when interactive
537 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
538 if ! checkyesno ${rcvar}; then
539 # check whether interactive or not
540 if [ -n "$_run_rc_script" ]; then
543 for _elem in $_keywords; do
544 if [ "$_elem" = "$rc_arg" ]; then
546 \$${rcvar} is not enabled - see ${rcvar_manpage}.
547 Use the following if you wish to perform the operation:
553 echo 1>&2 "$0: unknown directive '$rc_arg'."
554 rc_usage "$_keywords"
558 eval $_pidcmd # determine the pid if necessary
560 for _elem in $_keywords; do
561 if [ "$_elem" != "$rc_arg" ]; then
565 # if there's a custom ${XXX_cmd},
566 # run that instead of the default
568 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
569 _postcmd=\$${rc_arg}_postcmd
570 if [ -n "$_cmd" ]; then
571 # if the precmd failed and force
574 if ! eval $_precmd && [ -z "$rc_force" ]; then
578 if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then
585 if [ ${#} -gt 0 ]; then
586 err 1 "the $rc_arg command does not take any parameters"
589 case "$rc_arg" in # default operations...
592 if [ -n "$rc_pid" ]; then
593 echo "${name} is running as pid $rc_pid."
595 echo "${name} is not running."
601 if [ -n "$rc_pid" ]; then
602 echo 1>&2 "${name} already running? (pid=$rc_pid)."
606 if [ ! -x ${_chroot}${command} ]; then
610 # check for required variables,
611 # directories, and files
613 for _f in $required_vars; do
614 if ! checkyesno $_f; then
615 warn "\$${_f} is not enabled."
616 if [ -z "$rc_force" ]; then
621 for _f in $required_dirs; do
622 if [ ! -d "${_f}/." ]; then
623 warn "${_f} is not a directory."
624 if [ -z "$rc_force" ]; then
629 for _f in $required_files; do
630 if [ ! -r "${_f}" ]; then
631 warn "${_f} is not readable."
632 if [ -z "$rc_force" ]; then
638 # if the precmd failed and force
641 if ! eval $_precmd && [ -z "$rc_force" ]; then
645 # setup the command to run, and run it
647 echo "Starting ${name}."
648 if [ -n "$_chroot" ]; then
651 ${_nice:+nice -n $_nice }\
652 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
653 $_chroot $command $rc_flags $command_args"
656 ${_chdir:+cd $_chdir; }\
658 ${_nice:+nice -n $_nice }\
659 $command $rc_flags $command_args"
660 if [ -n "$_user" ]; then
661 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
665 # if the cmd failed and force
668 if ! eval $_doit && [ -z "$rc_force" ]; then
672 # finally, run postcmd
678 if [ -z "$rc_pid" ]; then
679 if [ -n "$pidfile" ]; then
681 "${name} not running? (check $pidfile)."
683 echo 1>&2 "${name} not running?"
688 # if the precmd failed and force
691 if ! eval $_precmd && [ -z "$rc_force" ]; then
695 # send the signal to stop
697 echo "Stopping ${name}."
698 _doit="kill -${sig_stop:-TERM} $rc_pid"
699 if [ -n "$_user" ]; then
700 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
703 # if the stop cmd failed and force
706 if ! eval $_doit && [ -z "$rc_force" ]; then
710 # wait for the command to exit,
712 wait_for_pids $rc_pid
717 if [ -z "$rc_pid" ]; then
718 if [ -n "$pidfile" ]; then
720 "${name} not running? (check $pidfile)."
722 echo 1>&2 "${name} not running?"
726 echo "Reloading ${name} config files."
727 if ! eval $_precmd && [ -z "$rc_force" ]; then
730 _doit="kill -${sig_reload:-HUP} $rc_pid"
731 if [ -n "$_user" ]; then
732 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
734 if ! eval $_doit && [ -z "$rc_force" ]; then
741 if ! eval $_precmd && [ -z "$rc_force" ]; then
744 # prevent restart being called more
745 # than once by any given script
747 if ${_rc_restart_done:-false}; then
750 _rc_restart_done=true
752 ( $0 ${_rc_prefix}stop )
753 $0 ${_rc_prefix}start
759 if [ -n "$rc_pid" ]; then
760 wait_for_pids $rc_pid
766 if [ -n "$rcvar" ]; then
767 if checkyesno ${rcvar}; then
768 echo "\$${rcvar}=YES"
776 rc_usage "$_keywords"
783 echo 1>&2 "$0: unknown directive '$rc_arg'."
784 rc_usage "$_keywords"
789 # run_rc_script file arg
790 # Start the script `file' with `arg', and correctly handle the
791 # return value from the script. If `file' ends with `.sh', it's
792 # sourced into the current environment. If `file' appears to be
793 # a backup or scratch file, ignore it. Otherwise if it's
794 # executable run as a child process.
796 # If `file' contains "KEYWORD: interactive" and if we are
797 # running inside /etc/rc with postprocessing (as signified by
798 # _rc_postprocessor_fd being defined) then the script's stdout
799 # and stderr are redirected to $_rc_original_stdout_fd and
800 # $_rc_original_stderr_fd, so the output will be displayed on the
801 # console but not intercepted by /etc/rc's postprocessor.
807 if [ -z "$_file" -o -z "$_arg" ]; then
808 err 3 'USAGE: run_rc_script file arg'
813 unset name command command_args command_interpreter \
814 extra_commands pidfile procname \
815 rcvar required_dirs required_files required_vars
816 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
819 if [ -n "${_rc_postprocessor_fd}" ] \
820 && _has_rcorder_keyword interactive $_file
826 *.sh) # run in current shell
827 if $_must_redirect; then
829 "note:Output from ${_file} is not logged"
830 no_rc_postprocess eval \
831 'set $_arg ; . $_file'
836 *[~#]|*.OLD|*.orig|*,v) # scratch file; skip
837 warn "Ignoring scratch file $_file"
840 if [ -x $_file ] && $_must_redirect; then
842 "note:Output from ${_file} is not logged"
843 if [ -n "$rc_fast_and_loose" ]; then
844 no_rc_postprocess eval \
845 'set $_arg ; . $_file'
847 no_rc_postprocess eval \
848 '( set $_arg ; . $_file )'
850 elif [ -x $_file ]; then
851 if [ -n "$rc_fast_and_loose" ]; then
854 ( set $_arg ; . $_file )
857 warn "Ignoring non-executable file $_file"
864 # load_rc_config command
865 # Source in the configuration file for a given command.
870 if [ -z "$_command" ]; then
871 err 3 'USAGE: load_rc_config command'
874 if ${_rc_conf_loaded:-false}; then
880 if [ -f /etc/rc.conf.d/"$_command" ]; then
881 . /etc/rc.conf.d/"$_command"
886 # load_rc_config_var cmd var
887 # Read the rc.conf(5) var for cmd and set in the
888 # current shell, using load_rc_config in a subshell to prevent
889 # unwanted side effects from other variable assignments.
893 if [ $# -ne 2 ]; then
894 err 3 'USAGE: load_rc_config_var cmd var'
897 load_rc_config '$1' >/dev/null;
898 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
899 echo '$2'=\'\''${'$2'}\'\'';
906 # Print a usage string for $0, with `commands' being a list of
911 echo -n 1>&2 "Usage: $0 [fast|force|one]("
915 echo -n 1>&2 "$_sep$_elem"
923 # err exitval message
924 # Display message to stderr and log to the syslog, and exit with exitval.
931 if [ -x /usr/bin/logger ]; then
932 logger "$0: ERROR: $*"
934 echo 1>&2 "$0: ERROR: $*"
940 # Display message to stderr and log to the syslog.
944 if [ -x /usr/bin/logger ]; then
945 logger "$0: WARNING: $*"
947 echo 1>&2 "$0: WARNING: $*"
951 # backup_file action file cur backup
952 # Make a backup copy of `file' into `cur', and save the previous
953 # version of `cur' as `backup' or use rcs for archiving.
955 # This routine checks the value of the backup_uses_rcs variable,
956 # which can be either YES or NO.
958 # The `action' keyword can be one of the following:
960 # add `file' is now being backed up (and is possibly
961 # being reentered into the backups system). `cur'
962 # is created and RCS files, if necessary, are
965 # update `file' has changed and needs to be backed up.
966 # If `cur' exists, it is copied to to `back' or
967 # checked into RCS (if the repository file is old),
968 # and then `file' is copied to `cur'. Another RCS
969 # check in done here if RCS is being used.
971 # remove `file' is no longer being tracked by the backups
972 # system. If RCS is not being used, `cur' is moved
973 # to `back', otherwise an empty file is checked in,
974 # and then `cur' is removed.
984 if checkyesno backup_uses_rcs; then
985 _msg0="backup archive"
988 # ensure that history file is not locked
989 if [ -f $_cur,v ]; then
990 rcs -q -u -U -M $_cur
993 # ensure after switching to rcs that the
994 # current backup is not lost
995 if [ -f $_cur ]; then
996 # no archive, or current newer than archive
997 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
998 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1007 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1010 chown root:wheel $_cur $_cur,v
1014 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1016 chown root:wheel $_cur $_cur,v
1023 if [ -f $_cur ]; then
1027 chown root:wheel $_cur
1037 # handle_fsck_error fsck_exit_code
1038 # Take action depending on the return code from fsck.
1046 2) # Needs re-run, still fs errors
1047 echo "File system still has errors; re-run fsck manually!"
1050 echo "Root filesystem was modified, rebooting ..."
1052 echo "Reboot failed; help!"
1055 echo "Automatic file system check failed; help!"
1058 echo "Boot interrupted."
1061 echo "Unknown error $1; help!"
1068 # _has_rcorder_keyword word file
1069 # Check whether a file contains a "# KEYWORD:" comment with a
1070 # specified keyword in the style used by rcorder(8).
1072 _has_rcorder_keyword()
1078 [ -r "$file" ] || return 1
1081 "# KEYWORD:"*[\ \ ]"${word}"[\ \ ]*)
1088 # give up at the first non-empty non-comment line
1097 # print_rc_metadata string
1098 # Print the specified string in such a way that the post-processor
1099 # inside /etc/rc will treat it as meta-data.
1101 # If we are not running inside /etc/rc, do nothing.
1103 # For public use by any rc.d script, the string must begin with
1104 # "note:", followed by arbitrary text. The intent is that the text
1105 # will appear in a log file but not on the console.
1107 # For private use within /etc/rc, the string must contain a
1108 # keyword recognised by the rc_postprocess_metadata() function
1109 # defined in /etc/rc, followed by a colon, followed by one or more
1110 # colon-separated arguments associated with the keyword.
1114 # _rc_postprocessor fd, if defined, is the fd to which we must
1115 # print, prefixing the output with $_rc_metadata_prefix.
1117 if [ -n "$_rc_postprocessor_fd" ]; then
1118 command printf "%s%s\n" "$rc_metadata_prefix" "$1" \
1119 >&${_rc_postprocessor_fd}
1125 # Arrange for output to be flushed, if we are running
1126 # inside /etc/rc with postprocessing.
1130 print_rc_metadata "nop"
1134 # print_rc_normal [-n] string
1135 # Print the specified string in such way that it is treated as
1136 # normal output, regardless of whether or not we are running
1137 # inside /etc/rc with post-processing.
1139 # If "-n" is specified in $1, then the string in $2 is printed
1140 # without a newline; otherwise, the string in $1 is printed
1143 # Intended use cases include:
1145 # o An rc.d script can use ``print_rc_normal -n'' to print a
1146 # partial line in such a way that it appears immediately
1147 # instead of being buffered by rc(8)'s post-processor.
1149 # o An rc.d script that is run via the no_rc_postprocess
1150 # function (so most of its output is invisible to rc(8)'s
1151 # post-processor) can use print_rc_normal to force some of its
1152 # output to be seen by the post-processor.
1157 # If _rc_postprocessor_fd is defined, then it is the fd
1158 # to which we must print; otherwise print to stdout.
1160 local fd="${_rc_postprocessor_fd:-1}"
1163 command printf "%s" "$2" >&${fd}
1167 command printf "%s\n" "$1" >&${fd}
1173 # no_rc_postprocess cmd...
1174 # Execute the specified command in such a way that its output
1175 # bypasses the post-processor that handles the output from
1176 # most commands that are run inside /etc/rc. If we are not
1177 # inside /etc/rc, then just execute the command without special
1180 # The intent is that interactive commands can be run via
1181 # no_rc_postprocess(), and their output will apear immediately
1182 # on the console instead of being hidden or delayed by the
1183 # post-processor. An unfortunate consequence of the output
1184 # bypassing the post-processor is that the output will not be
1189 if [ -n "${_rc_postprocessor_fd}" ]; then
1190 "$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
1198 # On each call, print a different one of "/", "-", "\\", "|",
1199 # followed by a backspace. The most recently printed value is
1200 # saved in $_twiddle_state.
1202 # Output is to /dev/tty, so this function may be useful even inside
1203 # a script whose output is redirected.
1207 case "$_twiddle_state" in
1213 command printf "%s\b" "$_next" >/dev/tty
1214 _twiddle_state="$_next"
1219 # Print the a human version of the exit code.
1225 echo "exited with code $1"
1226 elif [ "$(expr $1 % 256)" -eq 127 ]
1228 # This cannot really happen because the shell will not
1229 # pass stopped job status out and the exit code is limited
1230 # to 8 bits. This code is here just for completeness.
1231 echo "stopped with signal $(expr $1 / 256)"
1233 echo "terminated with signal $(expr $1 - 128)"
1238 # collapse_backslash_newline
1239 # Copy input to output, collapsing <backslash><newline>
1240 # to nothing, but leaving other backslashes alone.
1242 collapse_backslash_newline()
1245 while read -r line ; do
1248 # print it, without the backslash or newline
1249 command printf "%s" "${line%?}"
1252 # print it, with a newline
1253 command printf "%s\n" "${line}"
1259 # Override the normal "echo" and "printf" commands, so that
1260 # partial lines printed by rc.d scripts appear immediately,
1261 # instead of being buffered by rc(8)'s post-processor.
1263 # Naive use of the echo or printf commands from rc.d scripts,
1264 # elsewhere in rc.subr, or anything else that sources rc.subr,
1265 # will call these functions. To call the real echo and printf
1266 # commands, use "command echo" or "command printf".
1272 '-n') _flush_rc_output ;;
1280 *) _flush_rc_output ;;