From owner-freebsd-arch@FreeBSD.ORG Mon Aug 12 20:27:38 2013 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id E7F8D4D8; Mon, 12 Aug 2013 20:27:37 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [IPv6:2001:470:1f11:75::1]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 58F082960; Mon, 12 Aug 2013 20:27:37 +0000 (UTC) Received: from jhbbsd.localnet (unknown [209.249.190.124]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 35AA9B93B; Mon, 12 Aug 2013 16:27:35 -0400 (EDT) From: John Baldwin To: freebsd-arch@freebsd.org Subject: Re: closedir - change in the libc Date: Mon, 12 Aug 2013 15:04:39 -0400 User-Agent: KMail/1.13.5 (FreeBSD/8.2-CBSD-20110714-p28; KDE/4.5.5; amd64; ; ) References: <5204BD94.7050800@FreeBSD.org> <20130809105934.GA32419@stack.nl> <20130810213844.GA1400@garage.freebsd.pl> In-Reply-To: <20130810213844.GA1400@garage.freebsd.pl> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Message-Id: <201308121504.39774.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Mon, 12 Aug 2013 16:27:35 -0400 (EDT) Cc: Jilles Tjoelker , Mario Oshogbo X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Aug 2013 20:27:38 -0000 On Saturday, August 10, 2013 5:38:44 pm Pawel Jakub Dawidek wrote: > On Fri, Aug 09, 2013 at 12:59:34PM +0200, Jilles Tjoelker wrote: > > On Fri, Aug 09, 2013 at 11:59:48AM +0200, Mario Oshogbo wrote: > > > I'm a student attending in GSoC'13 in project "Write new features for > > > Capsicum" [1] and I would like to propose a little change to the libc. > > > > > In the libc we have the function opendir(3) and fdopendir(3) which are > > > responsible for opening directories. We also have function closedir(3) > > > which is responsible for free the structure allocated by two previous > > > funcions. > > > > > The problem with the closedir(3) is that he close fd used as argument of > > > fdopendir(3). I think programmer should be able to make a choice if he > > > want to close this descriptor automatically or not. Of course we are > > > able to use for dup(2) function and save fd before calling fdopendir(3) > > > to prevent closing but I think this is the one syscall which we could save. > > > > > To support the propose I would like to give some example. I working now > > > on making the fts(3) more sandbox friendly which means I would like to > > > remove all fchdir(2) functions and operate only on fd. One of the steps > > > I perform is changing the opendir(3) to the fdopendir(3). I also must > > > remember the fd of currently opened directory after perform fclosedir. > > > If we have N of directories we must perform N additional dup(2) and N > > > additional close(2) functions to save this fd. > > > > > So I would like to propose function called fdclosedir which will free > > > DIR structure but will not close fd attached to DIR structure. This > > > function also could return this fd which make it useful with the > > > opendir(3) function, when we don't know the fd but we would like free > > > DIR structure and perform some operations only on fd. I attach diff file > > > with my proposal of change. > > > > While this looks sensible, the only advantage is cleaner code and less > > memory usage. Instead of additional dup()/close(), you could also keep > > the DIR around as long as you need the fd. [...] > > This change is useful, I needed it in unrelated work to Mariusz's. > I wanted to "borrow" my directory descriptor to some function, which > were doing fdopendir(3) on it and to make it possible to not leak memory > on return from this function I had to dup(2) this descriptor. > > I'd love to see fdclose() for similar reasons (see bin/75767, among > others). Perl has some really gross code that wants to control the exact close of stdio fd. Currently it writes -1 to the _file member of FILE, and I'd love to make it use an "official" API for this. I think fdclose() would work fine for this and I'd like to see it as long as it will work well with Perl (and let us remove the gross _file hack from Perl). My interest is in making _file a private part of the future ABI as part of extending it from a short to an int. -- John Baldwin