Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 09 Aug 2013 11:59:48 +0200
From:      Mario Oshogbo <oshogbo@FreeBSD.org>
To:        freebsd-arch@freebsd.org
Subject:   closedir - change in the libc
Message-ID:  <5204BD94.7050800@FreeBSD.org>

next in thread | raw e-mail | index | archive | help
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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5204BD94.7050800>