NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: misc/46546: powerd script fails due to spurious environment variable
The following reply was made to PR misc/46546; it has been noted by GNATS.
From: Alan Barrett <apb%cequrux.com@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc:
Subject: Re: misc/46546: powerd script fails due to spurious environment
variable
Date: Fri, 25 Jul 2014 18:39:35 +0200
--bAmEntskrkuBymla
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Disposition: inline
Please try the attached patch, which attempts to notice when a process that
was started under control of /etc/rc no longer has access to /etc/rc's output
postprocessor (which will be the case for daemons launched from rc.d scripts).
--apb (Alan Barrett)
--bAmEntskrkuBymla
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="pr46546.patch"
Index: etc/rc
===================================================================
--- etc/rc 9 Apr 2014 12:45:05 -0000 1.168
+++ etc/rc 25 Jul 2014 12:35:33 -0000
@@ -84,6 +84,7 @@ rc_real_work()
# with redirected output.
#
_rc_postprocessor_fd=9 ; export _rc_postprocessor_fd
+ _rc_pid=$$ ; export _rc_pid
eval "exec ${_rc_postprocessor_fd}>&1"
# Print a metadata line when we exit
Index: etc/rc.subr
===================================================================
--- etc/rc.subr 17 Dec 2012 18:17:27 -0000 1.92
+++ etc/rc.subr 25 Jul 2014 14:45:13 -0000
@@ -786,6 +786,35 @@ $command $rc_flags $command_args"
}
#
+# _have_rc_postprocessor
+# Test whether the current script is running in a context that
+# was invoked from /etc/rc with a postprocessor.
+#
+# If the test fails, some variables may be unset to make
+# such tests more efficient in future.
+#
+_have_rc_postprocessor()
+{
+ # Cheap tests that fd and pid are set, fd is writable.
+ [ -n "${_rc_postprocessor_fd}" ] || return 1
+ [ -n "${_rc_pid}" ] || return 1
+ eval ": >&${_rc_postprocessor_fd}" 2>/dev/null || return 1
+
+ # More expensive test that pid is running.
+ # Unset _rc_pid if this fails.
+ kill -0 "${_rc_pid}" 2>/dev/null \
+ || { unset _rc_pid; return 1; }
+
+ # More expensive test that pid appears to be
+ # a shell running an rc script.
+ # Unset _rc_pid if this fails.
+ expr "$(ps -p "${_rc_pid}" -o command=)" : ".*sh .*/rc.*" >/dev/null \
+ || { unset _rc_pid; return 1; }
+
+ return 0
+}
+
+#
# run_rc_script file arg
# Start the script `file' with `arg', and correctly handle the
# return value from the script. If `file' ends with `.sh', it's
@@ -794,9 +823,8 @@ $command $rc_flags $command_args"
# executable run as a child process.
#
# If `file' contains "KEYWORD: interactive" and if we are
-# running inside /etc/rc with postprocessing (as signified by
-# _rc_postprocessor_fd being defined) then the script's stdout
-# and stderr are redirected to $_rc_original_stdout_fd and
+# running inside /etc/rc with postprocessing, then the script's
+# stdout and stderr are redirected to $_rc_original_stdout_fd and
# $_rc_original_stderr_fd, so the output will be displayed on the
# console but not intercepted by /etc/rc's postprocessor.
#
@@ -816,7 +844,7 @@ run_rc_script()
eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
_must_redirect=false
- if [ -n "${_rc_postprocessor_fd}" ] \
+ if _have_rc_postprocessor \
&& _has_rcorder_keyword interactive $_file
then
_must_redirect=true
@@ -1114,7 +1142,7 @@ print_rc_metadata()
# _rc_postprocessor fd, if defined, is the fd to which we must
# print, prefixing the output with $_rc_metadata_prefix.
#
- if [ -n "$_rc_postprocessor_fd" ]; then
+ if _have_rc_postprocessor; then
command printf "%s%s\n" "$rc_metadata_prefix" "$1" \
>&${_rc_postprocessor_fd}
fi
@@ -1154,10 +1182,11 @@ _flush_rc_output()
#
print_rc_normal()
{
- # If _rc_postprocessor_fd is defined, then it is the fd
- # to which we must print; otherwise print to stdout.
+ # print to stdout or _rc_postprocessor_fd, depending on
+ # whether not we have an rc postprocessor.
#
- local fd="${_rc_postprocessor_fd:-1}"
+ local fd=1
+ _have_rc_postprocessor && fd="${_rc_postprocessor_fd}"
case "$1" in
"-n")
command printf "%s" "$2" >&${fd}
@@ -1186,7 +1215,7 @@ print_rc_normal()
#
no_rc_postprocess()
{
- if [ -n "${_rc_postprocessor_fd}" ]; then
+ if _have_rc_postprocessor; then
"$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd}
else
"$@"
--bAmEntskrkuBymla--
Home |
Main Index |
Thread Index |
Old Index