[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#340531: libc6: freopen() function closes unrelated streams.



On Thu, Nov 24, 2005 at 07:36:11AM -0500, Thomas E. Dickey wrote:
> On Thu, Nov 24, 2005 at 12:46:41AM -0500, Daniel Jacobowitz wrote:
> > On Wed, Nov 23, 2005 at 05:59:59PM -0500, Thomas Dickey wrote:
> > > The call to freopen() is causing the stream for TRACE's output to
> > > close, so the last line in the output is the first TRACE shown in
> > > this chunk.  Putting an abort() before the last TRACE proves that
> > > the code is getting that far.
> > 
> > What does strace say is going on?
> 
> Oddly, I didn't think to try that.  It says that the fopen() for TRACE
> is getting file descriptor 0.  But fileno(stdin) says it also is 0.
> If stdin is closed, it's legal for another fopen() to get file descriptor
> 0 of course, but then stdin is no longer a valid stream and fileno() has
> to return a -1.

If stdin is closed _by the application_, then of course this is true. 
If it's closed before the application starts, then the standard library
has no way to know.  This is happening because vile is invoked with
stdin/stdout/stderr closed.

Glibc has code to fix this - but it's only invoked for setuid binaries. 
For non-setuid binaries that you expect to be run with file descriptors
closed, you need to first open 0/1/2 yourself, or not attempt to use
stdin/stdout/stderr.


-- 
Daniel Jacobowitz
CodeSourcery, LLC



Reply to: