This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] libio: Avoid dup already opened file descriptor [BZ#21393]
On Mon, May 22, 2017 at 4:04 PM, Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
> On Monday 22 May 2017 10:55 PM, Adhemerval Zanella wrote:
>> If the operation fails, a null pointer is returned; otherwise,
>> -@code{freopen} returns @var{stream}.
>> +@code{freopen} returns @var{stream}. Also for Linux, due internal dup3
>> +usage, it might fail with errno set to EBUSY during a race condition
>> +with @code{open} and @code{dup}.
>
> On Linux, @code{freopen} may fail and set @code{errno} to @{EBUSY} when
> the kernel structure for the old file descriptor was not initialized
> completely before @code{freopen} was called.
+ "This can only happen in multi-threaded programs, when two threads
race to allocate the same file descriptor number. To avoid the
possibility of this race, do not use @code{close} to close the
underlying file descriptor for a @code{FILE}; either use
@code{freopen} while the file is still open, or use @code{open} and
then @code{dup2} to install the new file descriptor."
zw