From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 7BAD3BC69 for ; Fri, 14 Sep 2007 08:36:47 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAK7I6UbAXQImn2dsb2JhbACOEgEBAQEHBAYHCBg X-IronPort-AV: E=Sophos;i="4.20,253,1186351200"; d="scan'208";a="1180743" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 14 Sep 2007 08:37:17 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l8E6b0rl023621 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 14 Sep 2007 08:37:00 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CACbJ6UZA9hw0/2dsb2JhbAA X-IronPort-AV: E=Sophos;i="4.20,253,1186351200"; d="scan'208";a="750733" Received: from tenhost.net ([64.246.28.52]) by mail1-smtp-roc.national.inria.fr with SMTP; 14 Sep 2007 08:37:16 +0200 Received: (qmail 7671 invoked from network); 14 Sep 2007 01:08:46 -0500 Received: from ip72-208-52-84.ph.ph.cox.net (HELO ?192.168.1.99?) (72.208.52.84) by tenhost.net with SMTP; 14 Sep 2007 01:08:46 -0500 Message-ID: <46EA2BC8.7040508@ramenlabs.com> Date: Thu, 13 Sep 2007 23:35:52 -0700 From: Dave Benjamin User-Agent: Mozilla-Thunderbird 2.0.0.4 (X11/20070828) MIME-Version: 1.0 To: Erik de Castro Lopo Cc: caml-list@inria.fr Subject: Re: [Caml-list] Closing all open file descriptors References: <20070914110403.f1ffe94b.mle+ocaml@mega-nerd.com> In-Reply-To: <20070914110403.f1ffe94b.mle+ocaml@mega-nerd.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 46EA2C0C.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; descriptors:01 descriptors:01 ocaml:01 stdio:01 ocaml:01 cloud:98 wrote:01 wrote:01 unix:01 unix:01 integer:01 caml-list:01 benjamin:01 benjamin:01 modules:02 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