Blame SOURCES/patch-2.7.6-crash-RLIMIT_NOFILE.patch

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