Proposal: a revoke() system call

Robert Watson rwatson at FreeBSD.org
Mon Jul 7 23:23:02 UTC 2008


On Mon, 7 Jul 2008, Sergey Babkin wrote:

> This leaves a small race window between fd is checked and read() is 
> executed. If in the meantime another thread does close() (and sets 
> mystructure.fd to -1), and the third thread does open() then the result of 
> this open would use the same fd number as our old fd (since now it's likely 
> to be the lowest available number), then read() would happen on a completely 
> wrong file. And yes, it does happen in real world. The best workaround I've 
> come up with is a small pause between setting mystructure.fd = -1 and 
> calling close().
>
> The point of proposal is to do a close() without freeing the file 
> descriptor.

Which can be accomplished by calling dup2(2) to replace the file descriptor 
with another file descriptor, perhaps one to /dev/null.  It would be worth 
carefully reviewing the implementation of dup2(2) to make sure that the 
close->replace there is atomic with respect to other threads simultaneously 
allocating file descriptors, such as with pipe(2).  This won't cancel existing 
I/Os, but per discussion, I/O cancelation is a very complicated issue.

Robert N M Watson
Computer Laboratory
University of Cambridge


More information about the freebsd-arch mailing list