News for hackers. Stuff that matters.
Re:POSIX close(2) is broken by FrankBooth on Saturday December 17, 2011 @06:19AM (#3364029)
From the man page:

  If  newfd was open, any errors that would have been reported at close(2) time are
  lost.  A careful programmer will not use dup2() or dup3() without closing newfd
  first.
Re:POSIX close(2) is broken by saurik on Saturday December 17, 2011 @06:59AM (#3364078)
Which is actually "interesting" advice, as it cannot be followed in a multithreaded program without insanely extensive process-wide locks ;P (as any call to open() between close() and dup2() has an irritatingly high probability of grabbing whatever file descriptor you just freed up). (Of course, most usages of dup2() are in contexts just after a fork() or at the beginning of main(), so I guess this could often be practical; it still seems unfortunate.)
Re:POSIX close(2) is broken by finnw on Saturday December 17, 2011 @08:57AM (#3364229)
Maybe this can still be worked around by copying the target fd first so it can be closed after fdup()ing the source fd, e.g.

// Assume x and y are open fds and we want to replace y with a copy of x

z = open("/dev/null", O_RDONLY); // Guarantees no other thread is using z

dup2(y, z);

dup2(x, y);

close(z);

Re:POSIX close(2) is broken by saurik on Saturday December 17, 2011 @02:50PM (#3364957)
Hah! (You don't actually need that open("/dev/null") trick, though: you can just use dup(). ;P)
Re:POSIX close(2) is broken by cperciva on Saturday December 17, 2011 @06:31AM (#3364048)
Maybe your man page says that, but POSIX says

    The dup2() function may fail if:
    [EIO]
        An I/O error occurred while attempting to close fildes2.