caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dave Benjamin <dave@ramenlabs.com>
To: Erik de Castro Lopo <mle+ocaml@mega-nerd.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Closing all open file descriptors
Date: Thu, 13 Sep 2007 23:35:52 -0700	[thread overview]
Message-ID: <46EA2BC8.7040508@ramenlabs.com> (raw)
In-Reply-To: <20070914110403.f1ffe94b.mle+ocaml@mega-nerd.com>

Erik de Castro Lopo wrote:
> Dave Benjamin wrote:
> 
>> I'm writing a daemon and I would like to be able to close all open file 
>> descriptors. As far as I can tell, there is no way to determine the 
>> maximum number of file descriptors available, and neither is there a way 
>> to get a file descriptor from an integer. The best I've come up with is 
>> the following:
>>
>>    for fd = 0 to 1024 do
>>      try Unix.close (Obj.magic fd : Unix.file_descr)
>>      with Unix.Unix_error _ -> ()
>>    done;
> 
> I remember doing this in a daemon I wrote years ago in C but didn't
> do it for a daemon I wrote recently in Ocaml (much nicer for daemons
> than *any* other language).

Did your program "daemonize" at all, then? Or did you use daemontools?

> My curiosity was piqued, so I did a bit of a search and found the
> rational for this here:
> 
>     http://cloud9.hedgee.com/scribbles/daemon
> 
> in particular the fghack tool which prevents a daemon that doesn't
> close all file descriptors from going into the background :
> 
>     http://cr.yp.to/daemontools/fghack.html

I never really thought all that much about closing file descriptors 
until recently, when I discovered that Apache leaks a bunch of 
descriptors to its modules, one of which is the listening socket which 
can prevent Apache from restarting. So, I've been reading Advanced 
Programming in the UNIX Environment to try to fill in the many gaps in 
my understanding of what's going on here.

I can see how it might be annoying to try to make a daemon run on stdio 
instead, and not being able to keep the file handles open... but it 
seems like daemons could be written to work either way if designed with 
some care.

> One possible solution to this would be a function written in C and
> wrapped in Ocaml that does the same as the Obj.magic hack above.

Yeah, the whole operation could be done in a C function too, and 
probably wouldn't be much more code to write.

Thanks for the interesting links,
Dave


  reply	other threads:[~2007-09-14  6:36 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-13 22:56 Dave Benjamin
2007-09-14  1:04 ` [Caml-list] " Erik de Castro Lopo
2007-09-14  6:35   ` Dave Benjamin [this message]
2007-09-14  6:48     ` Erik de Castro Lopo
2007-09-14  7:32       ` Dave Benjamin
2007-09-14  6:33 ` David Allsopp
2007-09-14  6:41   ` Dave Benjamin
2007-09-14 10:54     ` Andre Nathan
2007-09-14 10:00   ` Mattias Engdegård
2007-09-14 20:31     ` Dave Benjamin
2007-09-14 21:52       ` Oliver Bandel
2007-09-14 22:12         ` Markus E L
2007-09-15  9:15           ` Oliver Bandel
2007-09-15  9:26             ` Erik de Castro Lopo
2007-09-15 10:43               ` Oliver Bandel
2007-09-15 11:36                 ` Mattias Engdegård
2007-09-15 11:57                   ` Oliver Bandel
2007-09-15 14:27                     ` Markus E L
2007-09-15 12:16                   ` Oliver Bandel
2007-09-15 14:29                     ` Markus E L
2007-09-15 18:04                       ` skaller
2007-09-15 14:17                   ` Markus E L
2007-09-15 14:16                 ` Markus E L
2007-09-15 15:58                   ` Eric Cooper
2007-09-15 16:17                     ` Markus E L
2007-09-15 18:33                       ` skaller
2007-09-15 17:44                   ` skaller
2007-09-14 10:10 ` Gerd Stolpmann
2007-09-14 11:56 ` Oliver Bandel
2007-09-17 11:00 ` Ville-Pertti Keinonen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=46EA2BC8.7040508@ramenlabs.com \
    --to=dave@ramenlabs.com \
    --cc=caml-list@inria.fr \
    --cc=mle+ocaml@mega-nerd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).