changeset 11940:6dd8c8815b1f

fts: avoid leaking fds * modules/fts (Depends-on): Add cloexec. * lib/fts.c (opendirat, diropen, fts_build): Set close-on-exec flag. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Wed, 02 Sep 2009 14:44:51 -0600
parents cd48f861a7a7
children 909daff94315
files ChangeLog lib/fts.c modules/fts
diffstat 3 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Sep 01 14:06:37 2009 -0600
+++ b/ChangeLog	Wed Sep 02 14:44:51 2009 -0600
@@ -1,5 +1,10 @@
 2009-09-02  Eric Blake  <ebb9@byu.net>
 
+	fts: avoid leaking fds
+	* modules/fts (Depends-on): Add cloexec.
+	* lib/fts.c (opendirat, diropen, fts_build): Set close-on-exec
+	flag.
+
 	fts: make directory fds more robust
 	* lib/fts.c (O_DIRECTORY): Let <fcntl.h> take care of this.
 	(opendirat): Specify O_DIRECTORY, and add fallbacks for safety.
--- a/lib/fts.c	Tue Sep 01 14:06:37 2009 -0600
+++ b/lib/fts.c	Wed Sep 02 14:44:51 2009 -0600
@@ -71,6 +71,9 @@
 # include "fcntl--.h"
 # include "dirent--.h"
 # include "unistd--.h"
+/* FIXME - use fcntl(F_DUPFD_CLOEXEC)/openat(O_CLOEXEC) once they are
+   supported.  */
+# include "cloexec.h"
 # include "same-inode.h"
 #endif
 
@@ -311,6 +314,7 @@
 
   if (new_fd < 0)
     return NULL;
+  set_cloexec_flag (new_fd, true);
   dirp = fdopendir (new_fd);
   if (dirp == NULL)
     {
@@ -362,9 +366,12 @@
   int open_flags = (O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
 		    | (ISSET (FTS_PHYSICAL) ? O_NOFOLLOW : 0));
 
-  return (ISSET (FTS_CWDFD)
-	  ? openat (sp->fts_cwd_fd, dir, open_flags)
-	  : open (dir, open_flags));
+  int fd = (ISSET (FTS_CWDFD)
+            ? openat (sp->fts_cwd_fd, dir, open_flags)
+            : open (dir, open_flags));
+  if (0 <= fd)
+    set_cloexec_flag (fd, true);
+  return fd;
 }
 
 FTS *
@@ -1305,7 +1312,10 @@
 	if (nlinks || type == BREAD) {
 		int dir_fd = dirfd(dirp);
 		if (ISSET(FTS_CWDFD) && 0 <= dir_fd)
-		  dir_fd = dup (dir_fd);
+		  {
+		    dir_fd = dup (dir_fd);
+		    set_cloexec_flag (dir_fd, true);
+		  }
 		if (dir_fd < 0 || fts_safe_changedir(sp, cur, dir_fd, NULL)) {
 			if (nlinks && type == BREAD)
 				cur->fts_errno = errno;
--- a/modules/fts	Tue Sep 01 14:06:37 2009 -0600
+++ b/modules/fts	Wed Sep 02 14:44:51 2009 -0600
@@ -8,6 +8,7 @@
 m4/fts.m4
 
 Depends-on:
+cloexec
 cycle-check
 d-ino
 d-type