This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: stderr vs. STDERR_FILENO
Mark Mitchell <mark@codesourcery.com> 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 (7.19.5.4p4 - "[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.
zw