9dc0ea4
commit 61d7788b83b302207a67b82786f4fd79e3538f30
9dc0ea4
Author: Andreas Gruenbacher <agruen@gnu.org>
9dc0ea4
Date:   Thu Jun 27 11:10:43 2019 +0200
9dc0ea4
9dc0ea4
    Don't crash when RLIMIT_NOFILE is set to RLIM_INFINITY
9dc0ea4
    
9dc0ea4
    * src/safe.c (min_cached_fds): Define minimum number of cached dir file
9dc0ea4
    descriptors.
9dc0ea4
    (max_cached_fds): Change type to rlim_t to allow storing RLIM_INFINITY.
9dc0ea4
    (init_dirfd_cache): Set max_cached_fds to RLIM_INFINITY when RLIMIT_NOFILE is
9dc0ea4
    RLIM_INFINITY.  Set the initial hash table size to min_cached_fds, independent
9dc0ea4
    of RLIMIT_NOFILE: patches commonly only affect one or a few files, so a small
9dc0ea4
    hash table will usually suffice; if needed, the hash table will grow.
9dc0ea4
    (insert_cached_dirfd): Don't shrink the cache when max_cached_fds is
9dc0ea4
    RLIM_INFINITY.
9dc0ea4
9dc0ea4
diff --git a/src/safe.c b/src/safe.c
9dc0ea4
index 5a7202f..f147b0e 100644
9dc0ea4
--- a/src/safe.c
9dc0ea4
+++ b/src/safe.c
9dc0ea4
@@ -67,7 +67,8 @@ struct cached_dirfd {
9dc0ea4
 };
9dc0ea4
 
9dc0ea4
 static Hash_table *cached_dirfds = NULL;
9dc0ea4
-static size_t max_cached_fds;
9dc0ea4
+static rlim_t min_cached_fds = 8;
9dc0ea4
+static rlim_t max_cached_fds;
9dc0ea4
 LIST_HEAD (lru_list);
9dc0ea4
 
9dc0ea4
 static size_t hash_cached_dirfd (const void *entry, size_t table_size)
9dc0ea4
@@ -98,11 +99,17 @@ static void init_dirfd_cache (void)
9dc0ea4
 {
9dc0ea4
   struct rlimit nofile;
9dc0ea4
 
9dc0ea4
-  max_cached_fds = 8;
9dc0ea4
   if (getrlimit (RLIMIT_NOFILE, &nofile) == 0)
9dc0ea4
-    max_cached_fds = MAX (nofile.rlim_cur / 4, max_cached_fds);
9dc0ea4
+    {
9dc0ea4
+      if (nofile.rlim_cur == RLIM_INFINITY)
9dc0ea4
+        max_cached_fds = RLIM_INFINITY;
9dc0ea4
+      else
9dc0ea4
+	max_cached_fds = MAX (nofile.rlim_cur / 4, min_cached_fds);
9dc0ea4
+    }
9dc0ea4
+  else
9dc0ea4
+    max_cached_fds = min_cached_fds;
9dc0ea4
 
9dc0ea4
-  cached_dirfds = hash_initialize (max_cached_fds,
9dc0ea4
+  cached_dirfds = hash_initialize (min_cached_fds,
9dc0ea4
 				   NULL,
9dc0ea4
 				   hash_cached_dirfd,
9dc0ea4
 				   compare_cached_dirfds,
9dc0ea4
@@ -148,20 +155,23 @@ static void insert_cached_dirfd (struct cached_dirfd *entry, int keepfd)
9dc0ea4
   if (cached_dirfds == NULL)
9dc0ea4
     init_dirfd_cache ();
9dc0ea4
 
9dc0ea4
-  /* Trim off the least recently used entries */
9dc0ea4
-  while (hash_get_n_entries (cached_dirfds) >= max_cached_fds)
9dc0ea4
+  if (max_cached_fds != RLIM_INFINITY)
9dc0ea4
     {
9dc0ea4
-      struct cached_dirfd *last =
9dc0ea4
-	list_entry (lru_list.prev, struct cached_dirfd, lru_link);
9dc0ea4
-      if (&last->lru_link == &lru_list)
9dc0ea4
-	break;
9dc0ea4
-      if (last->fd == keepfd)
9dc0ea4
+      /* Trim off the least recently used entries */
9dc0ea4
+      while (hash_get_n_entries (cached_dirfds) >= max_cached_fds)
9dc0ea4
 	{
9dc0ea4
-	  last = list_entry (last->lru_link.prev, struct cached_dirfd, lru_link);
9dc0ea4
+	  struct cached_dirfd *last =
9dc0ea4
+	    list_entry (lru_list.prev, struct cached_dirfd, lru_link);
9dc0ea4
 	  if (&last->lru_link == &lru_list)
9dc0ea4
 	    break;
9dc0ea4
+	  if (last->fd == keepfd)
9dc0ea4
+	    {
9dc0ea4
+	      last = list_entry (last->lru_link.prev, struct cached_dirfd, lru_link);
9dc0ea4
+	      if (&last->lru_link == &lru_list)
9dc0ea4
+		break;
9dc0ea4
+	    }
9dc0ea4
+	  remove_cached_dirfd (last);
9dc0ea4
 	}
9dc0ea4
-      remove_cached_dirfd (last);
9dc0ea4
     }
9dc0ea4
 
9dc0ea4
   /* Only insert if the parent still exists. */