The result of dup3(fd, fd, O_CLOEXEC) is to set the O_CLOEXEC flag on fd.
The behaviour of dup2() is functionally the following:
1. Duplicate the file descriptor from file_table[oldfd].
2. If file_table[newfd] is in use, close it.
3. Install the duplicate file descriptor at file_table[newfd].
Step (2) could be considered a bit dubious, but the behaviour of
dup2(fd, fd) is a direct consequence of the chosen semantics.
-hpa
--