This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/682] stdout and file descriptor mismatch after a freopen()

------- Additional Comments From gotom at debian dot or dot jp  2005-02-14 15:47 -------
I don't understand how to implement your proposal: "A correct behavior
would be to reserve file descriptors 0, 1 and 2 for stdin, stdout and
stderr at the fopen/fclose/freopen level".

BTW, looking through the discussion in bug 670, I think the following
interpretation of freopen() can provide your request:

 - interpret freopen() that freopen(path, mode, fp) assigns the same
   fd number which is previously used in fp (using dup2()).

This is known as BSD freopen() behavior.

However, I know this behavior has problem because there's race
condition between fd=open(path) and dup2(fd, fp->fd) - the fd
specified in the 2nd argument of dup2() is possibly used for the other
purpose when signal handler is called or concurrent thread is running
at the same time.

In order to fix this race condition, the 2nd argument of dup2() should
be the original fp->fd - but unfortunatelly standards say freopen()
should always close the fp in first, so we can't avoid this problem.

I marked it as invalid until you propose the correct implementation.

           What    |Removed                     |Added
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]