Bugzilla will be upgraded to version 5.0. The upgrade date is tentatively scheduled for 2 December 2018, pending final testing and feedback.
Bug 615284 - wrong redirections with SIGPIPE traps
wrong redirections with SIGPIPE traps
Status: CLOSED ERRATA
Product: Red Hat Enterprise Linux 5
Classification: Red Hat
Component: ksh (Show other bugs)
5.5
All Linux
high Severity high
: rc
: ---
Assigned To: Michal Hlavinka
qe-baseos-tools
: ZStream
Depends On:
Blocks: 590060 616684 675128
  Show dependency treegraph
 
Reported: 2010-07-16 07:12 EDT by Tomas Smetana
Modified: 2012-02-21 00:49 EST (History)
4 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
When a ksh script contained the "trap" command to capture a "SIGPIPE" signal, sending this signal via the built-in "echo" command could cause its output to be incorrectly added to the redirected output of an external command. With this update, ksh now flushes the output buffer before redirecting any output streams.
Story Points: ---
Clone Of:
: 616684 (view as bug list)
Environment:
Last Closed: 2012-02-21 00:49:56 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
patch to fix this (482 bytes, patch)
2010-07-21 03:49 EDT, Michal Hlavinka
no flags Details | Diff


External Trackers
Tracker ID Priority Status Summary Last Updated
Red Hat Product Errata RHBA-2012:0159 normal SHIPPED_LIVE ksh bug fix and enhancement update 2012-02-20 09:53:47 EST

  None (edit)
Description Tomas Smetana 2010-07-16 07:12:09 EDT
Description of problem:
In a ksh script, when SIGPIPE trap action is specified and SIGPIPE occurs by the output of the built-in echo command, the output appears in a redirect file of a external command.

Version-Release number of selected component (if applicable):
ksh-20100202-1.el5

How reproducible:
Always

Steps to Reproduce:

Assume there is a huge ascii file /tmp/huge-text which takes more than one second to cat to terminal.

Run the following scripts with

ssh localhost /path/to/the/script.ksh

Steps to reproduce:

Case 1:
-------

the script.ksh is:

#!/bin/ksh
killparent()
{
	sleep 1
	/bin/kill -9 $1
}
trap "/bin/echo trap > /dev/null" 13
killparent ${PPID} &
/bin/cat /tmp/huge-text
print "echo1"
/bin/echo "echo2" > /tmp/echo2
trap 13

After running the script with ssh print the contents of the resulting file:
# cat /tmp/echo2

Actual results:
echo1
echo2

Expected results:
echo2

Case 2:
-------

the script.ksh is

#!/bin/ksh
killparent()
{
	sleep 1
	/bin/kill -9 $1
}
trap "/bin/echo trap >> /tmp/trap" 13
killparent ${PPID} &
/bin/cat /tmp/huge-text
echo "echo1"
/bin/echo "echo2"
trap 13

After running the script with ssh print the contents of the resulting file:
# cat /tmp/trap

Actual results:
echo1
trap
trap

Expected results:
trap
trap

Additional information:
Seems that the output of the echo builtin command is buffered and the buffer is flushed upon forking a child process.  However when the flush fails the content of the buffer is not erased and then appears in where it should not.

Maybe the ksh should either delete the buffer after flush (even unsucessful), not to buffer the echo output or not to buffer the redirection output.
Comment 1 Michal Hlavinka 2010-07-16 07:34:07 EDT
I can reproduce this. 
/bin/cat /tmp/huge-text from the reproducer can be replaced with /usr/bin/yes
Comment 2 Michal Hlavinka 2010-07-21 03:49:56 EDT
Created attachment 433326 [details]
patch to fix this
Comment 5 RHEL Product and Program Management 2010-08-09 14:40:51 EDT
This request was evaluated by Red Hat Product Management for
inclusion in the current release of Red Hat Enterprise Linux.
Because the affected component is not scheduled to be updated in the
current release, Red Hat is unfortunately unable to address this
request at this time. Red Hat invites you to ask your support
representative to propose this request, if appropriate and relevant,
in the next release of Red Hat Enterprise Linux.
Comment 10 Michal Hlavinka 2011-02-03 05:31:35 EST
note: attached patch works fine without any regressions, but when combined with patch for bug #601555 (which works fine too) it no longer fixes second reproducer and output is:
echo1
trap

working on it to find out more and prepare better patch
Comment 15 Martin Prpič 2011-03-16 11:54:02 EDT
    Technical note added. If any revisions are required, please edit the "Technical Notes" field
    accordingly. All revisions will be proofread by the Engineering Content Services team.
    
    New Contents:
When a ksh script contained the "trap" command to capture a "SIGPIPE" signal, sending this signal via the built-in "echo" command could cause its output to be incorrectly added to the redirected output of an external command. With this update, ksh now flushes the output buffer before redirecting any output streams.
Comment 18 errata-xmlrpc 2012-02-21 00:49:56 EST
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

http://rhn.redhat.com/errata/RHBA-2012-0159.html

Note You need to log in before you can comment on or make changes to this bug.