about summary refs log tree commit diff
path: root/nscd/nscd_gethst_r.c
diff options
context:
space:
mode:
Diffstat (limited to 'nscd/nscd_gethst_r.c')
-rw-r--r--nscd/nscd_gethst_r.c39
1 files changed, 24 insertions, 15 deletions
diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c
index 42327dd317..407be1441f 100644
--- a/nscd/nscd_gethst_r.c
+++ b/nscd/nscd_gethst_r.c
@@ -27,7 +27,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <arpa/nameser.h>
-#include <sys/mman.h>
 #include <not-cancel.h>
 
 #include "nscd-client.h"
@@ -94,7 +93,6 @@ libc_locked_map_ptr (map_handle);
    handling.  */
 libc_freeres_fn (gr_map_free)
 {
-
   if (map_handle.mapped != NO_MAPPING)
     free (map_handle.mapped);
 }
@@ -106,22 +104,24 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
 	       struct hostent *resultbuf, char *buffer, size_t buflen,
 	       struct hostent **result, int *h_errnop)
 {
+  int gc_cycle;
+  int nretries = 0;
+
+  /* If the mapping is available, try to search there instead of
+     communicating with the nscd.  */
+  struct mapped_database *mapped;
+  mapped = __nscd_get_map_ref (GETFDHST, "hosts", &map_handle, &gc_cycle);
+
+ retry:;
   const hst_response_header *hst_resp = NULL;
   const char *h_name = NULL;
   const uint32_t *aliases_len = NULL;
   const char *addr_list = NULL;
   size_t addr_list_len = 0;
   int retval = -1;
-  int gc_cycle;
   const char *recend = (const char *) ~UINTMAX_C (0);
   int sock = -1;
-
-  /* If the mapping is available, try to search there instead of
-     communicating with the nscd.  */
-  struct mapped_database *mapped = __nscd_get_map_ref (GETFDHST, "hosts",
-						       &map_handle, &gc_cycle);
- retry:
-  if (mapped != MAP_FAILED)
+  if (mapped != NO_MAPPING)
     {
       const struct datahead *found = __nscd_cache_search (type, key, keylen,
 							  mapped);
@@ -355,11 +355,20 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
   if (sock != -1)
     close_not_cancel_no_status (sock);
  out:
-  if (__nscd_drop_map_ref (mapped, gc_cycle) != 0)
-    /* When we come here this means there has been a GC cycle while we
-       were looking for the data.  This means the data might have been
-       inconsistent.  Retry.  */
-    goto retry;
+  if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1)
+    {
+      /* When we come here this means there has been a GC cycle while we
+	 were looking for the data.  This means the data might have been
+	 inconsistent.  Retry if possible.  */
+      if ((gc_cycle & 1) != 0 || ++nretries == 5)
+	{
+	  /* nscd is just running gc now.  Disable using the mapping.  */
+	  __nscd_unmap (mapped);
+	  mapped = NO_MAPPING;
+	}
+
+      goto retry;
+    }
 
   return retval;
 }