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 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 4EBA9BC69 for ; Fri, 14 Sep 2007 03:03:43 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAGZ66UbAXQImn2dsb2JhbACOFgEBAQEHBAYHCBiYMA X-IronPort-AV: E=Sophos;i="4.20,252,1186351200"; d="scan'208";a="742218" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 14 Sep 2007 03:04:11 +0200 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l8E13s1G006081 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 14 Sep 2007 03:03:55 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAACl76Uba1uFinmdsb2JhbACOFgEBAQEHBAYPGJgu X-IronPort-AV: E=Sophos;i="4.20,252,1186351200"; d="scan'208";a="16176672" Received: from smtp.syd.people.net.au ([218.214.225.98]) by mail4-smtp-sop.national.inria.fr with SMTP; 14 Sep 2007 03:04:08 +0200 Received: (qmail 21908 invoked from network); 14 Sep 2007 01:04:09 -0000 Received: from unknown (HELO hendrix.mega-nerd.net) (218.214.64.136) by smtp.syd.people.net.au with SMTP; 14 Sep 2007 01:04:09 -0000 Received: from hendrix (hendrix [192.168.200.99]) by hendrix.mega-nerd.net (Postfix) with SMTP id 64F3D30104 for ; Fri, 14 Sep 2007 11:04:03 +1000 (EST) Date: Fri, 14 Sep 2007 11:04:03 +1000 From: Erik de Castro Lopo To: caml-list@inria.fr Subject: Re: [Caml-list] Closing all open file descriptors Message-Id: <20070914110403.f1ffe94b.mle+ocaml@mega-nerd.com> In-Reply-To: References: Organization: Erik Conspiracy Secret Labs X-Mailer: Sylpheed 2.3.1 (GTK+ 2.10.11; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 46E9DDFA.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 descriptors:01 descriptors:01 ocaml:01 cloud:98 television:98 vcr:98 wrote:01 wrote:01 unix:01 unix:01 integer:01 caml-list:01 benjamin:01 descriptor:02 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). 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 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. Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- "I have long argued that the entertainment industry doesn't want people to have computers. Computers give users too much capability, too much flexibility, too much freedom. The entertainment industry wants users to sit back and consume things. They are trying to turn a computer into an Internet Entertainment Platform, along the lines of a television or VCR. This bill is a large step in that direction." -- Bruce Schneier