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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 080D2BC6B for ; Mon, 17 Sep 2007 13:00:02 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CAA777UbAXQInemdsb2JhbACBWIw+CQo X-IronPort-AV: E=Sophos;i="4.20,264,1186351200"; d="scan'208";a="1299666" Received: from concorde.inria.fr ([192.93.2.39]) by mail2-smtp-roc.national.inria.fr with ESMTP; 17 Sep 2007 13:00:58 +0200 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l8HB0P9w029644 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Mon, 17 Sep 2007 13:00:28 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAJb67UZTkdxCn2dsb2JhbACBWIw+AgcEBgcIGA X-IronPort-AV: E=Sophos;i="4.20,264,1186351200"; d="scan'208";a="2805602" Received: from smtp-66.nebula.fi ([83.145.220.66]) by mail3-smtp-sop.national.inria.fr with ESMTP; 17 Sep 2007 13:00:55 +0200 Received: from will.iki.fi (xdsl-206-79.nblnetworks.fi [83.145.206.79]) by smtp-66.nebula.fi (Postfix) with ESMTP id 4548965E8C5; Mon, 17 Sep 2007 14:00:53 +0300 (EEST) Received: from [10.0.20.131] (fa-3-0-0.fw.exomi.com [217.169.64.99]) by will.iki.fi (Postfix) with ESMTP id 9353C10FF06; Mon, 17 Sep 2007 14:00:53 +0300 (EEST) In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: Caml List Content-Transfer-Encoding: 7bit From: Ville-Pertti Keinonen Subject: Re: [Caml-list] Closing all open file descriptors Date: Mon, 17 Sep 2007 14:00:53 +0300 To: Dave Benjamin X-Mailer: Apple Mail (2.752.3) X-Miltered: at concorde with ID 46EE5E49.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; descriptors:01 descriptors:01 stdin:01 stdout:01 stderr:01 libc:01 stdio:01 dup:01 stdout:01 stderr:01 dup:01 stdin:01 stdio:01 2007,:98 wrote:01 On Sep 14, 2007, at 1:56 AM, Dave Benjamin wrote: > for fd = 0 to 1024 do > try Unix.close (Obj.magic fd : Unix.file_descr) > with Unix.Unix_error _ -> () > done; > > Is there a better way? While I don't have a solution for your general problem (other than having your program launched by a known mechanism), I recommend that you never close the file descriptors for stdin, stdout and stderr. Subsequent opens will end up reusing those file descriptor numbers, but system libraries will still consider them valid (via libc). This can cause some very strange bugs. The safe way to detach stdio is to open /dev/null for writing, dup2 that over stdout and stderr, open /dev/null for reading and dup2 that over stdin (obviously you can close the newly opened descriptors), thus you never run anything without valid stdio file descriptors.