Skip to contentSkip to conversation

dup2() vs dup3() inconsistency when

DISCUSSION Bodo Eggert 2 participants 3 messages Oct 10, 2008 7 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw
Quoted 10 lines · Michael Kerrisk

Well, as long as we are fixing the dup3() interface in the way that Al and Ulrich have suggested, what about another fix:

give an error if newfd is already open, thus forcing the user to do an explicit close

?

This silent close in dup2() is an implementation blemish. Why not eliminate it?

I think it might be usefull:
Thread B does some logging to fd 42
Thread A switches the logfile by creating a new file, writing a header and then does dup3(fd, 42, O_WRONLY|O_APPEND|O_CLOEXEC); close(fd);

(Off cause this is not yet implemented, O_RDONLY would give some problems,
O_CLOEXEC alone might be better done while open()ing the file, ... but you get the idea.)

BTW: I think dup3(fd, -1, flags) should use the file descriptor dup() would return. Or should there be a dupf(fd, flags) syscall instead?

#8 lore raw
Quoted 10 lines · Bodo Eggert
Quoted 8 lines · Michael Kerrisk

Well, as long as we are fixing the dup3() interface in the way that Al and Ulrich have suggested, what about another fix:

give an error if newfd is already open, thus forcing the user to do an explicit close

?

Quoted 7 lines
Quoted 2 lines

This silent close in dup2() is an implementation blemish. Why not eliminate it?

I think it might be usefull:
Thread B does some logging to fd 42
Thread A switches the logfile by creating a new file, writing a header and then does dup3(fd, 42, O_WRONLY|O_APPEND|O_CLOEXEC); close(fd);

I don't know the details of the kernel locks here, so perhaps this is a naive question: but, as things stand is there not the potential for some nasty race if one thread is writing to fd 42 at the same time as another thread does a dup2(fd, 42)?

Quoted 7 lines

(Off cause this is not yet implemented, O_RDONLY would give some problems,
O_CLOEXEC alone might be better done while open()ing the file, ... but you get the idea.)

BTW: I think dup3(fd, -1, flags) should use the file descriptor dup() would return. Or should there be a dupf(fd, flags) syscall instead?

If one did this, maybe it would be better to have an extra flag that
said: "use the first free file descriptor >= newfd", thus giving the more general functionality like fcntl(F_DUPFD).

Signature · 5 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
git://git.kernel.org/pub/scm/docs/man-pages/man-pages.git
man-pages online: http://www.kernel.org/doc/man-pages/online_pages.html
Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html
#9 lore raw
Quoted 19 lines · Michael Kerrisk
Quoted 10 lines · Bodo Eggert
Quoted 8 lines · Michael Kerrisk

Well, as long as we are fixing the dup3() interface in the way that Al and Ulrich have suggested, what about another fix:

give an error if newfd is already open, thus forcing the user to do an explicit close

?

Quoted 7 lines
Quoted 2 lines

This silent close in dup2() is an implementation blemish. Why not eliminate it?

I think it might be usefull:
Thread B does some logging to fd 42
Thread A switches the logfile by creating a new file, writing a header and then does dup3(fd, 42, O_WRONLY|O_APPEND|O_CLOEXEC); close(fd);

Quoted 4 lines

I don't know the details of the kernel locks here, so perhaps this is a naive question: but, as things stand is there not the potential for some nasty race if one thread is writing to fd 42 at the same time as another thread does a dup2(fd, 42)?

I strongly hope there would not be any ...

Quoted 11 lines
Quoted 7 lines

(Off cause this is not yet implemented, O_RDONLY would give some problems,
O_CLOEXEC alone might be better done while open()ing the file, ... but you get the idea.)

BTW: I think dup3(fd, -1, flags) should use the file descriptor dup() would return. Or should there be a dupf(fd, flags) syscall instead?

If one did this, maybe it would be better to have an extra flag that
said: "use the first free file descriptor >= newfd", thus giving the more general functionality like fcntl(F_DUPFD).

I think you are right. I thought about something similar, too, but was distracted enough to not think about the connection.

Signature · 2 lines
Funny quotes:
10. Nothing is fool proof to a talented fool.