View Issue Details

IDProjectCategoryView StatusLast Update
00004801003.1(2008)/Issue 7System Interfacespublic2019-06-10 08:55
Reportereblake Assigned Toajosey  
PrioritynormalSeverityObjectionTypeError
Status ClosedResolutionAccepted As Marked 
NameEric Blake
OrganizationRed Hat
User Referenceebb.handles
Section2.5.1 Interaction of File Descriptors and Standard I/O Streams
Page Number491
Line Number16844
Interp StatusApproved
Final Accepted TextSee 0000480:0000894
Summary0000480: freopen, fclose effect on multiple file handles
DescriptionThe standard states that freopen always closes a handle and creates a
stream that is open to a new file description when describing the
interaction of freopen with handles, but later states that freopen
with a NULL filename argument may (but not must) reuse the existing
stream and underlying file description [XSH freopen lines 30885-89].

Here, I argue that the freopen description is intended, and that the
multiple-handle text should be fixed to be made consistent.

Also, the following example of two handles should be well-defined, but
is currently handled differently in Solaris than it is in glibc 2.14;
I argue that the glibc behavior is a bug in trying to implement 0000087
(that is, in trying to fix fclose() vs. ungetc() semantics in glibc bug
http://sourceware.org/bugzilla/show_bug.cgi?id=12724, glibc inadvertently
also broke fclose() to blindly reposition the file description even if
the stream was not otherwise used):

  const char buf[] = "hello world";
  int fd;
  int fd2;
  FILE *f;

  /* Prepare a seekable file. */
  fd = open (NAME, O_RDWR | O_CREAT | O_TRUNC, 0600);
  assert (0 <= fd);
  assert (write (fd, buf, sizeof buf) == sizeof buf);
  assert (lseek (fd, 1, SEEK_SET) == 1);

  /* Create an output stream visiting the file; when it is closed, all
     other file descriptors visiting the file must see the new file
     position. */
  fd2 = dup (fd);
  assert (0 <= fd2);
  f = fdopen (fd2, "w"); // POSIX requires that f be at position 1
  assert (f);
  assert(lseek(fd, 4, SEEK_SET) == 4); // fd is now the active handle
  assert (fclose (f) == 0); // The f/fd2 handle was never repositioned or made active
  assert (lseek (fd, 0, SEEK_CUR) == 4); // so fd should still be at 4

Solaris passed this test, but glibc 2.14 ended up repositioning the
file description to offset 1, which was then observable in fd and
causes the assertion to fail.

I think the intent of the standard is clear, but recent wording changes
to fclose() mean that we can make this more explicit.
Desired ActionAt line 16844 [XSH 2.5.1], change:

A handle which is a stream is considered to be closed when either an
fclose( ) or freopen( ) is executed on it (the result of freopen( ) is
a new stream, which cannot be a handle on the same open file description
as its previous value),

to:

A handle which is a stream is considered to be closed when either an
fclose( ), or freopen( ) with non-null filename, is executed on it (for
freopen( ) with a null filename, it is implementation-defined whether
a new handle is created or the existing one reused),

At line 16880, add a bullet for the second handle:

• If fclose( ) is the first (and only) action on the second handle, then
the offset of the file description shall not be changed from what it was
via the first handle.
Tagstc2-2008

Relationships

related to 0000087 Closedajosey fflush and ungetc 
related to 0000662 Closedajosey Clarify or add file descriptor preservation and atomicity requirements for freopen 

Activities

jim_pugsley

2011-07-28 16:27

manager   bugnote:0000894

Interpretation response
------------------------
The standard is unclear on this issue, and no conformance distinction can be made between alternative implementations based on this. This is being referred to the sponsor.

Rationale:
-------------
Standard is contradictory on whether freopen() can reuse a file description when passed a null filename.

Notes to the Editor (not part of this interpretation):
-------------------------------------------------------
Make the changes in the suggested action except do not make the changes suggested at line 16880.

ajosey

2011-08-10 15:19

manager   bugnote:0000927

Interpretation proposed 10 August 2011 for final 30 day review

ajosey

2011-09-12 15:21

manager   bugnote:0000959

Interpretation approved 12 Sept 2011

Issue History

Date Modified Username Field Change
2011-07-28 15:08 eblake New Issue
2011-07-28 15:08 eblake Status New => Under Review
2011-07-28 15:08 eblake Assigned To => ajosey
2011-07-28 15:08 eblake Name => Eric Blake
2011-07-28 15:08 eblake Organization => Red Hat
2011-07-28 15:08 eblake User Reference => ebb.handles
2011-07-28 15:08 eblake Section => 2.5.1 Interaction of File Descriptors and Standard I/O Streams
2011-07-28 15:08 eblake Page Number => 491
2011-07-28 15:08 eblake Line Number => 16844
2011-07-28 15:08 eblake Interp Status => ---
2011-07-28 16:22 jim_pugsley Interp Status --- => Pending
2011-07-28 16:22 jim_pugsley Status Under Review => Interpretation Required
2011-07-28 16:22 jim_pugsley Resolution Open => Accepted As Marked
2011-07-28 16:27 jim_pugsley Note Added: 0000894
2011-07-28 16:27 jim_pugsley Tag Attached: tc2-2008
2011-07-28 16:29 jim_pugsley Final Accepted Text => See 0000480:0000894
2011-07-28 16:31 eblake Relationship added related to 0000087
2011-08-10 15:19 ajosey Interp Status Pending => Proposed
2011-08-10 15:19 ajosey Note Added: 0000927
2011-09-12 15:21 ajosey Interp Status Proposed => Approved
2011-09-12 15:21 ajosey Note Added: 0000959
2013-02-21 18:04 eblake Relationship added related to 0000662
2019-06-10 08:55 agadmin Status Interpretation Required => Closed