delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/01/23/16:34:34

X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: Eric Blake <ebb9 AT byu DOT net>
Subject: Re: bug with built-in commands in bash when redirecting output
Date: Tue, 23 Jan 2007 21:33:08 +0000 (UTC)
Lines: 46
Message-ID: <loom.20070123T221146-200@post.gmane.org>
References: <45B638BE DOT 80003 AT acm DOT org> <loom DOT 20070123T193447-734 AT post DOT gmane DOT org>
Mime-Version: 1.0
User-Agent: Loom/3.14 (http://gmane.org/)
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Eric Blake <ebb9 <at> byu.net> writes:

> 
> I am suspecting a potential cygwin bug in fork().  Using the builtin echo, 
the 
> sequence is that bash creates the pipe, then forks, the forked child sees 
that 
> stdout is text mode, then rearranges fd's around so that the pipe is now 
> attached to stdout, all before any output occurs - but by then the damage is 
> done, since stdout remains in text mode.  Using /bin/echo is immune, because 
> the fork is followed by an exec() that starts from scratch, determines that 
> stdout is a pipe, and defaults back to binary mode.

The problem is that after a fork, there is no way for stdio to know when you 
have replaced the fd underlying one of std{in,out,err} unless you tell it.  On 
Linux, this isn't too bad of a problem (sure, if stdout is line-buffered, and 
you then replace the fd with a pipe, you have a slight performance drop when 
using stdout in the fork, but no one has really complained about this before).  
But on cygwin, when line endings are at stake, it matters.  A cooler fix would 
be coordinating between cygwin and newlib, such that any dup() that changes the 
underlying fd of an open stream will make the stream auto-detect the change.  
But I don't know if such a change is worth the effort; at any rate, my reading 
of POSIX is that the use of a stream (including stdout) after clobbering the 
underlying fd is in the realm of undefined territory.

> 
> I'll have to do further investigation, to see whether it is really bash's or 
> cygwin's fault (so far all I've done is look at straces), and even if it is 
> cygwin's fault, whether bash can work around it until a cygwin patch is 
> available.

So, I retract that it is a cygwin bug, and instead claim it is an upstream bash 
bug.  It is solved by doing freopen(NULL,"w",stdout) after replacing the fd, 
since that is the mechanism for telling stdio that it should please choose the 
correct text or binary mode based on the new underlying fd.  And while I don't 
know if upstream will accept this patch (after all, on Linux, no one seems to 
mind if stdout is slightly out-of-sync with reality after replacing the fd 
underneath it, and freopen(NULL) is not universally supported), I will 
certainly be releasing a patched version of cygwin bash in the near future.  I 
still hope to convert to a cygport build setup, and figure out how to make 
interactive exported SHELLOPTS play nicer with non-interactive shells, so it 
may be a few more days before I can release this patch.

-- 
Eric Blake



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019