Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Closing all open file descriptors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-09-15 (14:11)
From: Markus E L <ls-ocaml-2006@m...>
Subject: Re: [Caml-list] Closing all open file descriptors

Oliver Bandel wrote:

> Zitat von Erik de Castro Lopo <>:
>> Oliver Bandel wrote:
>> > If you think you need all this for your daemon,
>> > I ask: why do you think you need it? If you write it, you have
>> > full control over all files you open
>> No, thats not right. I can write a program, that opens a bunch
>> of file descriptors, and then exec his program and his program
>> will inherit all open file descritors.
> Yes, that's correct.
> But you (as the programmer) have the control (by design)
> about how to handle that case.

Let's assume (as an example) I'm starting a demon / background process
by clicking on an icon in a desktop environment. Which file
descriptors are open when the demon starts executing? Would I be
expected to patch the desktop to ensure that they aren't? Or will I
have to rely on a rather undocumented bit of the desktop software's
behaviour (perhaps subject to change) to guarantee my demons security.

This sucks on so many levels. My position is, if there is something
you inherit or can create in your process there must also be a way to
browse/index/enumerate what you inherited/created. So, yes, there
should be a enumerate_my_open_descriptors() even in POSIX. If it it
isn't it has to be reinvented in a platform specific way on every
platfrom where this is possible (e.g. under Linux using
/proc/self/fd), to get a better programming environment.

> If you have all your open descriptors in a list,
> you can close them after a fork.

And if you inherit them unkknowingly, you're lost.

> In C, with fcntl, there is a possibility to close files
> on an exec automatically with the close-on-exec flag.

This is aboout what a parent does whom one possibly doesn't control.

Regards -- Markus