[OT] figuring out which files have F_CLOEXEC

Nicholas Clark nick at ccl4.org
Fri Mar 27 16:55:19 GMT 2009


On Fri, Mar 27, 2009 at 04:51:36PM +0000, Aaron Crane wrote:
> Peter Corlett writes:
> > open() with ">&" does dup(2) under the hood. Without testing, I'd
> > have assumed that when the new filehandle is closed, it won't close
> > the duped file handle. I can't recall whether the close-on-exec flag
> > is passed through on duping though.
> 
> Unfortunately, dup(2) (and dup2(2)) give you a new fd that doesn't
> share fd flags with the original.  (That's the thing that distinguishes
> fd flags (of which FD_CLOEXEC is probably the only one) from other fd
> metadata like seek position and O_* mode.)
> 
> It's a tricky problem, and even if it can be done, I don't think it
> can be done easily.  I think you'd need to be able to swap in a
> replacement fd for a filehandle without closing the original fd, or
> some such; and I can't see a way of doing that.

Whereas it's trivial to do it cleanly in C... :-)

> Nick, what would be bad in your situation about fork/execing a script
> which prints out all file descriptors that were open when it started?

Nothing that I can think of. That's a sick trick, that I think will work.

Nicholas Clark


More information about the london.pm mailing list