This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Patch] PR 31228, Fix close-on-exec race.


David Daney wrote:

+static void
+close_all_files(int lowfd, int exclude)
+{
+# if defined(HAVE_PROC_SELF_FD) && defined(HAVE_READDIR) \
+     && defined(HAVE_OPENDIR)
+  DIR *dir = opendir ("/proc/self/fd");
+  dirent *dep;
+  fd_list_elt *list_head = NULL;
+
+  while ((dep = readdir (dir)) != NULL)
+    {
+      if (dep->d_name[0] != '.')
+        {
+          int fd = atoi (dep->d_name);
+          if (fd >= lowfd && fd != exclude)
+            {
+#  ifdef HAVE_ALLOCA
+              fd_list_elt *elt = (fd_list_elt *)alloca(sizeof(fd_list_elt));
+#  else
+              fd_list_elt *elt = new fd_list_elt;
+#  endif
+              elt->next = list_head;
+              elt->fd = fd;
+            }
+        }
+    }
+  closedir (dir);
+  while (list_head != NULL)
+    {
+      close (list_head->fd);
+      list_head = list_head->next;
+      // Don't bother freeing list_head.  We are either going to exec
+      // or _exit so the memory will get cleaned up.
+    }

Well this is just a proof of concept. There should probably be some error checking in there too.


David Daney


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]