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

David Daney ddaney@avtrex.com
Wed Mar 21 17:39:00 GMT 2007


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



More information about the Java-patches mailing list