This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: stderr vs. STDERR_FILENO

Mark Mitchell <> writes:

> Andreas Schwab wrote:
>>stderr freopen'd means it is still attached to fd 2.
> Typical implementations do a close, followed by an open.
> In threaded code, some other thread might grab the fd between the
> close and the open.

An extra careful implementation would do an open first, then dup2 the
file descriptor over the old one and close the extra descriptor.
(The dup2 is only necessary to ensure that fds 0,1,2 retain their
special meaning - other files could just swap out the 'fd' field of
the FILE structure and close the old file).

I note that glibc does not do this, and there is wording in C99 which
could be read to disallow it ( - "[freopen] first attempts
to close any file that is associated with the specified stream") so
Mark's concern is valid.

For what it's worth I think this is primarily a documentation issue -

    The default terminate handler is __gnu_cxx::__verbose_terminate_handler, 
    which will attempt to write to stderr.  If your application closes
    stderr or redirects it to an inappropriate location for error
    messages, you should provide your own terminate handler.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]