+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.
+ }