aboutsummaryrefslogtreecommitdiff
path: root/libio/freopen.c
diff options
context:
space:
mode:
Diffstat (limited to 'libio/freopen.c')
-rw-r--r--libio/freopen.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/libio/freopen.c b/libio/freopen.c
index ad1c848877..980523af48 100644
--- a/libio/freopen.c
+++ b/libio/freopen.c
@@ -76,17 +76,31 @@ freopen (const char *filename, const char *mode, FILE *fp)
/* unbound stream orientation */
result->_mode = 0;
- if (fd != -1)
+ if (fd != -1 && _IO_fileno (result) != fd)
{
- __dup3 (_IO_fileno (result), fd,
- (result->_flags2 & _IO_FLAGS2_CLOEXEC) != 0
- ? O_CLOEXEC : 0);
+ /* At this point we have both file descriptors already allocated,
+ so __dup3 will not fail with EBADF, EINVAL, or EMFILE. But
+ we still need to check for EINVAL and, due Linux internal
+ implementation, EBUSY. It is because on how it internally opens
+ the file by splitting the buffer allocation operation and VFS
+ opening (a dup operation may run when a file is still pending
+ 'install' on VFS). */
+ if (__dup3 (_IO_fileno (result), fd,
+ (result->_flags2 & _IO_FLAGS2_CLOEXEC) != 0
+ ? O_CLOEXEC : 0) == -1)
+ {
+ _IO_file_close_it (result);
+ result = NULL;
+ goto end;
+ }
__close (_IO_fileno (result));
_IO_fileno (result) = fd;
}
}
else if (fd != -1)
__close (fd);
+
+end:
if (filename == NULL)
free ((char *) gfilename);