From owner-freebsd-arch@FreeBSD.ORG Fri Aug 9 09:59:59 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 995BFA85; Fri, 9 Aug 2013 09:59:59 +0000 (UTC) (envelope-from oshogbo.vx@gmail.com) Received: from mail-bk0-x22f.google.com (mail-bk0-x22f.google.com [IPv6:2a00:1450:4008:c01::22f]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id DCE262BAD; Fri, 9 Aug 2013 09:59:58 +0000 (UTC) Received: by mail-bk0-f47.google.com with SMTP id mx12so1005779bkb.34 for ; Fri, 09 Aug 2013 02:59:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :content-type; bh=z2QS8AdY/0vJRM0zOFbJ29tKd3zAyEEfy6mrt8+bzBM=; b=jGSMUfpnFM1EQGU2ARe6YlaIX5CFpEcUQqSwXilLng0k90AOTdv/V/J6rpkXJ99AW+ 7XBOW5ZUYzRgQAJhwT8YqNlY/L7WSzA8brVY84BA39sEu/tHBHv5dCtk+7Yu1RkhrbZp nVI+IE1NHACM5cORu+nYPd/HeK4YBAdmnMhruh0brp55DLpb6JgmvdpX4PIJV8BH15x+ g/cUSEWPHyxICpEpdhvxoPCbFXDrKXtI/qbOGaCUCS2PIYbKu7d+1rS/IEugU+w/KBg2 seP9RVUa3rnKmRc2lOY5xEdqRMEXLwVHG2jpr+oibn4Xl3k7r8a0Jr3qjsk3TTUR809/ glOQ== X-Received: by 10.205.9.8 with SMTP id ou8mr2136143bkb.76.1376042396949; Fri, 09 Aug 2013 02:59:56 -0700 (PDT) Received: from [192.168.0.195] (58.wheelsystems.com. [83.12.187.58]) by mx.google.com with ESMTPSA id et15sm3296800bkc.0.2013.08.09.02.59.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 02:59:55 -0700 (PDT) Sender: Mario Oshogbo Message-ID: <5204BD94.7050800@FreeBSD.org> Date: Fri, 09 Aug 2013 11:59:48 +0200 From: Mario Oshogbo User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 MIME-Version: 1.0 To: freebsd-arch@freebsd.org Subject: closedir - change in the libc X-Enigmail-Version: 1.5.2 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="KjmkLWTW566TBTRP5Q5VAkAlRDJmGKQq1" 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: Fri, 09 Aug 2013 09:59:59 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --KjmkLWTW566TBTRP5Q5VAkAlRDJmGKQq1 Content-Type: multipart/mixed; boundary="------------070702080808080009040308" This is a multi-part message in MIME format. --------------070702080808080009040308 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, 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 sav= e. 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. Chears, oshogbo [1] https://wiki.freebsd.org/SummerOfCode2013/CapsicumFeatures --------------070702080808080009040308 Content-Type: text/x-patch; name="fdclosedir.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="fdclosedir.patch" --- lib/libc/gen/closedir.c 2013-08-07 11:20:34.000000000 +0200 +++ lib/libc/gen/closedir.c 2013-08-07 11:21:09.000000000 +0200 @@ -49,7 +49,7 @@ * close a directory. */ int -closedir(DIR *dirp) +fdclosedir(DIR *dirp) { int fd; =20 @@ -65,5 +65,12 @@ _pthread_mutex_destroy(&dirp->dd_lock); } free((void *)dirp); - return(_close(fd)); + return (fd); +} + +int +closedir(DIR *dirp) +{ + + return (_close(fdclosedir(dirp))); } --- lib/libc/gen/directory.3 2013-08-07 11:21:48.000000000 +0200 +++ lib/libc/gen/directory.3 2013-08-07 11:25:20.000000000 +0200 @@ -40,6 +40,7 @@ .Nm seekdir , .Nm rewinddir , .Nm closedir , +.Nm fdclosedir , .Nm dirfd .Nd directory operations .Sh LIBRARY @@ -64,6 +65,8 @@ .Ft int .Fn closedir "DIR *dirp" .Ft int +.Fn fdclosedir "DIR *dirp" +.Ft int .Fn dirfd "DIR *dirp" .Sh DESCRIPTION The @@ -208,6 +211,14 @@ is set to indicate the error. .Pp The +.Fn fdclosedir +function +is equivalent to the +.Fn closedir +function except that this function returns directory file descriptor +instead of closing it. +.Pp +The .Fn dirfd function returns the integer file descriptor associated with the named @@ -252,6 +263,9 @@ .Fn fdopendir function appeared in .Fx 8.0 . +.Fn fdclosedir +function appeared in +.Fx 10.0 . .Sh BUGS The invalidation of .Fn telldir --- include/dirent.h 2013-08-07 11:19:41.000000000 +0200 +++ include/dirent.h 2013-08-07 11:19:10.000000000 +0200 @@ -79,6 +79,7 @@ #endif #if __BSD_VISIBLE DIR *__opendir2(const char *, int); +int fdclosedir(DIR *); int getdents(int, char *, int); int getdirentries(int, char *, int, long *); #endif --------------070702080808080009040308-- --KjmkLWTW566TBTRP5Q5VAkAlRDJmGKQq1 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iQIcBAEBAgAGBQJSBL2ZAAoJECRkYejLhN/ji6gP/RR3PLfrS7hdq6cFMbIcsBDV 0F2ZDZDQKMK2iAgSotDh5vP4MyMDSN9170zpn6TtCBB+HD/5JAufOr0bEmDh6Rqh /PuAzkif1O/a/fiZ659FfLkKa9YDmY9lNhvzliPI9ZGWwx5RBA2fqfdVn6rpFpZT 39LnZqfqBcdQUijfzdOM0XPBp0FbiHyHnrK6LzkfdpMWFmZT1OA0XLMAEWudRcbD lm59K+8ZGib9MF2DcH+sQ4Oebb+BtLYdwxCU27Bt6gQ8JEzSgsCzGW9XJSfkpE0F NXiINUAUCaqE9RtXAEl9VMcXvgBYtzaV9No3ZWpmF+BuEe4fS/Mzmg9vWoLT/em7 cGvAnXbA/47hgEnxAQlmZ61lw7giroU1mqMaHU2c7fFwC8nCEyHUiLoixFZmPb5u ILwn8YVtNfyCj+5fiEkwRzFJRKjaQW80u8O2umQabp0axNcE+C05kH7Tzvzxvb7m hyjAAOgBxzsxWIGMYPAsca2hBU+9/3n74l16DzgeEjdNRB3yaWSWOB4sa9y/X6V0 Afyp9z/cDZRBJVV2ONW42+YeqyMpsAp7SjSg+C+TsDDFFrjghE2ImiXFOIm2E/fT ePPV+OPpedaJPUB5yx11SUGEqLyYAs+jnTQMSMD/ont8CuXDMDj8HosMid2XnCfV rJHgrAUyWE1W49gwPGCT =dnQh -----END PGP SIGNATURE----- --KjmkLWTW566TBTRP5Q5VAkAlRDJmGKQq1--