glibc/manual
Adhemerval Zanella f1a67a2c78 libio: Avoid dup already opened file descriptor [BZ#21393]
As described in BZ#21398 (close as dup of 21393) report current
freopen implementation fails when one tries to freopen STDIN_FILENO,
STDOUT_FILENO, or STDERR_FILENO.  Although on bug report the
discussion leads to argue if a close followed by a freopen on the
standard file is a valid operation, the underlying issue is not
really the check for dup3 returned value, but rather calling it
if the returned file descriptor is equal as the input one.

So for a quality of implementation this patch avoid calling dup3
for the aforementioned case.  It also adds a dup3 error case check
for the two possible failures, with one being Linux only: EINTR and
EBUSY.  The EBUSY issue is better explained on this stackoverflow
thread [1], but in a short it is due the internal Linux
implementation which allows a race condition window for dup2 due
the logic dissociation of file descriptor allocation and actual
VFS 'install' operation.  For both outliers failures all allocated
memory is freed and a NULL FILE* is returned.

With this patch the example on BZ#21398 is now actually possible
(I used as the testcase for the bug report).  Checked on
x86_64-linux-gnu.

	[BZ #21393]
	* libio/freopen.c (freopen): Avoid dup already opened file descriptor
	and add a check for dup3 failure.
	* libio/freopen64.c (freopen64): Likewise.
	* libio/tst-freopen.c (do_test): Rename to do_test_basic and use
	libsupport.
	(do_test_bz21398): New test.
	* manual/stdio.texi (freopen): Add documentation of EBUSY failure.

[1] http://stackoverflow.com/questions/23440216/race-condition-when-using-dup2
2017-05-22 18:13:35 -03:00
..
examples Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
Makefile Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
argp.texi Manual typos: The Basic Program/System Interface 2016-10-06 12:19:25 +05:30
arith.texi Add fromfp functions. 2016-12-31 00:40:59 +00:00
charset.texi manual: fix typos in character set handling 2016-06-16 00:45:37 -04:00
check-safety.sh Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
conf.texi manual: Convert @tables of annotated @items to @vtables. 2017-05-18 00:44:06 -07:00
contrib.texi Fix a typo in the manual. 2017-02-08 01:34:12 -08:00
creature.texi Add support for testing __STDC_WANT_IEC_60559_TYPES_EXT__ 2017-05-09 11:40:28 -03:00
crypt.texi manual: Fix up invalid header and standards syntax. 2017-05-18 00:39:48 -07:00
ctype.texi manual: fix typos in the character handling chapter 2016-06-16 00:37:44 -04:00
debug.texi Manual typos: Debugging support 2016-10-06 12:25:38 +05:30
dir .. 2005-11-21 15:45:19 +00:00
errno.texi manual: Convert errno @comments to new @errno macro. 2017-05-19 22:47:53 -07:00
fdl-1.3.texi BZ#13738: Switch manual to FDL 1.3. 2012-02-24 12:58:10 -08:00
filesys.texi manual: Fix up invalid header and standards syntax. 2017-05-18 00:39:48 -07:00
freemanuals.texi Update to canonical freemanuals.texi file. 2013-09-24 14:06:56 -07:00
getopt.texi getopt: remove USE_NONOPTION_FLAGS 2017-04-07 07:45:53 -04:00
header.texi Use Texinfo macros to refer to the GNU C Library within the manual. 2012-02-28 14:44:20 +00:00
install-plain.texi BZ #15941: Fix INSTALL file regeneration failure with makeinfo 5.x 2013-12-05 09:58:20 +05:30
install.texi Assume that accept4 is always available and works 2017-04-19 07:44:48 +02:00
intro.texi manual: fix typo in the introduction 2016-05-19 23:22:59 -04:00
io.texi Clean up glibc manual references to "GNU system" (bug 6911). 2012-03-08 01:27:38 +00:00
ipc.texi manual/ipc.texi: Fix AC-safety notes. 2014-04-08 17:12:15 -04:00
job.texi Manual typos: Job Control 2016-10-06 12:20:28 +05:30
lang.texi manual: Convert @tables of annotated @items to @vtables. 2017-05-18 00:44:06 -07:00
lgpl-2.1.texi Use canonical FSF .texi files for LGPL and FDL texts. 2011-06-06 16:16:55 -07:00
libc-texinfo.sh Make shebang interpreter directives consistent 2016-01-07 04:03:21 -05:00
libc.texinfo Update copyright dates not handled by scripts/update-copyrights. 2017-01-01 00:26:24 +00:00
libcbook.texi
libdl.texi * manual/libdl.texi: New. 2014-01-31 23:23:59 -02:00
libm-err-tab.pl Do not hardcode list of libm functions in libm-err-tab.pl. 2017-02-06 17:55:59 +00:00
llio.texi manual: Add preadv and pwritev documentation 2017-05-04 17:44:12 -03:00
locale.texi manual: Fix up invalid header and standards syntax. 2017-05-18 00:39:48 -07:00
macros.texi manual: Convert errno @comments to new @errno macro. 2017-05-19 22:47:53 -07:00
maint.texi Manual typos: Library Maintenance 2016-10-06 12:29:11 +05:30
math.texi Do not hardcode list of libm functions in libm-err-tab.pl. 2017-02-06 17:55:59 +00:00
memory.texi manual: Document replacing malloc [BZ #20424] 2017-04-21 10:28:37 +02:00
message.texi manual: fix typos in the message chapter 2016-06-16 01:14:32 -04:00
nss.texi manual: Convert @tables of variables to @vtables. 2016-12-21 01:36:58 -08:00
nsswitch.texi
pattern.texi manual: Convert @tables of annotated @items to @vtables. 2017-05-18 00:44:06 -07:00
pipe.texi * manual/pipe.texi: Document MTASC-safety properties. 2014-01-31 23:37:28 -02:00
platform.texi PowerPC: Extend Program Priority Register support 2015-08-19 17:43:26 -03:00
probes.texi User manual documentation for tunables 2016-12-31 23:49:24 +05:30
process.texi manual: Convert @tables of variables to @vtables. 2016-12-21 01:36:58 -08:00
resource.texi manual: Convert @tables of annotated @items to @vtables. 2017-05-18 00:44:06 -07:00
search.texi manual: Convert @tables of variables to @vtables. 2016-12-21 01:36:58 -08:00
setjmp.texi Manual typos: Non-Local Exits 2016-10-06 12:17:29 +05:30
signal.texi manual: Convert @tables of variables to @vtables. 2016-12-21 01:36:58 -08:00
socket.texi manual: Convert @tables of annotated @items to @vtables. 2017-05-18 00:44:06 -07:00
startup.texi Manual typos: The Basic Program/System Interface 2016-10-06 12:19:25 +05:30
stdio-fp.c
stdio.texi libio: Avoid dup already opened file descriptor [BZ#21393] 2017-05-22 18:13:35 -03:00
string.texi New string function explicit_bzero (from OpenBSD). 2016-12-16 16:21:54 -05:00
summary.awk Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sysinfo.texi manual: Convert @tables of variables to @vtables. 2016-12-21 01:36:58 -08:00
syslog.texi manual: Convert @tables of variables to @vtables. 2016-12-21 01:36:58 -08:00
terminal.texi manual: Convert @tables of variables to @vtables. 2016-12-21 01:36:58 -08:00
texinfo.tex Update miscellaneous files from upstream sources. 2016-12-21 16:05:55 +00:00
texis.awk Correct close statement. 2001-05-18 13:01:32 +00:00
threads.texi Manual typos: POSIX Threads 2016-10-06 12:26:06 +05:30
time.texi manual: Fix up invalid header and standards syntax. 2017-05-18 00:39:48 -07:00
tsort.awk Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
tunables.texi User manual documentation for tunables 2016-12-31 23:49:24 +05:30
users.texi manual: Fix up invalid header and standards syntax. 2017-05-18 00:39:48 -07:00
xtract-typefun.awk Make shebang interpreter directives consistent 2016-01-07 04:03:21 -05:00