ce426f
--- glibc-2.17-c758a686/nscd/cache.c	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/nscd/cache.c	2015-05-13 13:45:57.259958374 -0400
ce426f
@@ -272,28 +272,38 @@
ce426f
       while (runp != NULL)
ce426f
 	{
ce426f
 #ifdef HAVE_INOTIFY
ce426f
-	  if (runp->inotify_descr == -1)
ce426f
+	  if (runp->inotify_descr[TRACED_FILE] == -1)
ce426f
 #endif
ce426f
 	    {
ce426f
 	      struct stat64 st;
ce426f
 
ce426f
 	      if (stat64 (runp->fname, &st) < 0)
ce426f
 		{
ce426f
+		  /* Print a diagnostic that the traced file was missing.
ce426f
+		     We must not disable tracing since the file might return
ce426f
+		     shortly and we want to reload it at the next pruning.
ce426f
+		     Disabling tracing here would go against the configuration
ce426f
+		     as specified by the user via check-files.  */
ce426f
 		  char buf[128];
ce426f
-		  /* We cannot stat() the file, disable file checking if the
ce426f
-		     file does not exist.  */
ce426f
-		  dbg_log (_("cannot stat() file `%s': %s"),
ce426f
+		  dbg_log (_("checking for monitored file `%s': %s"),
ce426f
 			   runp->fname, strerror_r (errno, buf, sizeof (buf)));
ce426f
-		  if (errno == ENOENT)
ce426f
-		    table->check_file = 0;
ce426f
 		}
ce426f
 	      else
ce426f
 		{
ce426f
-		  if (st.st_mtime != table->file_mtime)
ce426f
+		  /* This must be `!=` to catch cases where users turn the
ce426f
+		     clocks back and we still want to detect any time difference
ce426f
+		     in mtime.  */
ce426f
+		  if (st.st_mtime != runp->mtime)
ce426f
 		    {
ce426f
-		      /* The file changed.  Invalidate all entries.  */
ce426f
+		      dbg_log (_("monitored file `%s` changed (mtime)"),
ce426f
+			       runp->fname);
ce426f
+		      /* The file changed. Invalidate all entries.  */
ce426f
 		      now = LONG_MAX;
ce426f
-		      table->file_mtime = st.st_mtime;
ce426f
+		      runp->mtime = st.st_mtime;
ce426f
+#ifdef HAVE_INOTIFY
ce426f
+		      /* Attempt to install a watch on the file.  */
ce426f
+		      install_watches (runp);
ce426f
+#endif
ce426f
 		    }
ce426f
 		}
ce426f
 	    }
ce426f
--- glibc-2.17-c758a686/nscd/connections.c	2015-05-12 15:03:02.870274443 -0400
ce426f
+++ glibc-2.17-c758a686/nscd/connections.c	2015-05-13 13:45:57.259958374 -0400
ce426f
@@ -974,6 +974,44 @@
ce426f
     finish_drop_privileges ();
ce426f
 }
ce426f
 
ce426f
+#ifdef HAVE_INOTIFY
ce426f
+#define TRACED_FILE_MASK (IN_DELETE_SELF | IN_CLOSE_WRITE | IN_MOVE_SELF)
ce426f
+#define TRACED_DIR_MASK (IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MOVE_SELF)
ce426f
+void
ce426f
+install_watches (struct traced_file *finfo)
ce426f
+{
ce426f
+  /* Use inotify support if we have it.  */
ce426f
+  if (finfo->inotify_descr[TRACED_FILE] < 0)
ce426f
+    finfo->inotify_descr[TRACED_FILE] = inotify_add_watch (inotify_fd,
ce426f
+							   finfo->fname,
ce426f
+			 				   TRACED_FILE_MASK);
ce426f
+  if (finfo->inotify_descr[TRACED_FILE] < 0)
ce426f
+    {
ce426f
+      dbg_log (_("disabled inotify-based monitoring for file `%s': %s"),
ce426f
+		 finfo->fname, strerror (errno));
ce426f
+      return;
ce426f
+    }
ce426f
+  dbg_log (_("monitoring file `%s` (%d)"),
ce426f
+	   finfo->fname, finfo->inotify_descr[TRACED_FILE]);
ce426f
+  /* Additionally listen for events in the file's parent directory.
ce426f
+     We do this because the file to be watched might be
ce426f
+     deleted and then added back again.  When it is added back again
ce426f
+     we must re-add the watch.  We must also cover IN_MOVED_TO to
ce426f
+     detect a file being moved into the directory.  */
ce426f
+  if (finfo->inotify_descr[TRACED_DIR] < 0)
ce426f
+    finfo->inotify_descr[TRACED_DIR] = inotify_add_watch (inotify_fd,
ce426f
+							  finfo->dname,
ce426f
+							  TRACED_DIR_MASK);
ce426f
+  if (finfo->inotify_descr[TRACED_DIR] < 0)
ce426f
+    {
ce426f
+      dbg_log (_("disabled inotify-based monitoring for directory `%s': %s"),
ce426f
+		 finfo->fname, strerror (errno));
ce426f
+      return;
ce426f
+    }
ce426f
+  dbg_log (_("monitoring directory `%s` (%d)"),
ce426f
+	   finfo->dname, finfo->inotify_descr[TRACED_DIR]);
ce426f
+}
ce426f
+#endif
ce426f
 
ce426f
 void
ce426f
 register_traced_file (size_t dbidx, struct traced_file *finfo)
ce426f
@@ -982,30 +1020,24 @@
ce426f
     return;
ce426f
 
ce426f
   if (__builtin_expect (debug_level > 0, 0))
ce426f
-    dbg_log (_("register trace file %s for database %s"),
ce426f
-	     finfo->fname, dbnames[dbidx]);
ce426f
+    dbg_log (_("monitoring file `%s' for database `%s' (%d)"),
ce426f
+	     finfo->fname, dbnames[dbidx],
ce426f
+             finfo->inotify_descr[TRACED_DIR]);
ce426f
 
ce426f
 #ifdef HAVE_INOTIFY
ce426f
-  if (inotify_fd < 0
ce426f
-      || (finfo->inotify_descr = inotify_add_watch (inotify_fd, finfo->fname,
ce426f
-						    IN_DELETE_SELF
ce426f
-						    | IN_MODIFY)) < 0)
ce426f
+  install_watches (finfo);
ce426f
 #endif
ce426f
-    {
ce426f
-      /* We need the modification date of the file.  */
ce426f
-      struct stat64 st;
ce426f
-
ce426f
-      if (stat64 (finfo->fname, &st) < 0)
ce426f
-	{
ce426f
-	  /* We cannot stat() the file, disable file checking.  */
ce426f
-	  dbg_log (_("cannot stat() file `%s': %s"),
ce426f
-		   finfo->fname, strerror (errno));
ce426f
-	  return;
ce426f
-	}
ce426f
 
ce426f
-      finfo->inotify_descr = -1;
ce426f
-      finfo->mtime = st.st_mtime;
ce426f
+  struct stat64 st;
ce426f
+  if (stat64 (finfo->fname, &st) < 0)
ce426f
+    {
ce426f
+      /* We cannot stat() the file. Set mtime to zero and try again later.  */
ce426f
+      dbg_log (_("stat failed for file `%s'; will try again later: %s"),
ce426f
+               finfo->fname, strerror (errno));
ce426f
+      finfo->mtime = 0;
ce426f
     }
ce426f
+  else
ce426f
+    finfo->mtime = st.st_mtime;
ce426f
 
ce426f
   /* Queue up the file name.  */
ce426f
   finfo->next = dbs[dbidx].traced_files;
ce426f
@@ -1030,20 +1062,27 @@
ce426f
   for (number = pwddb; number < lastdb; ++number)
ce426f
     if (strcmp (key, dbnames[number]) == 0)
ce426f
       {
ce426f
-	if (number == hstdb)
ce426f
+	struct traced_file *runp = dbs[number].traced_files;
ce426f
+	while (runp != NULL)
ce426f
 	  {
ce426f
-	    struct traced_file *runp = dbs[hstdb].traced_files;
ce426f
-	    while (runp != NULL)
ce426f
-	      if (runp->call_res_init)
ce426f
-		{
ce426f
-		  res_init ();
ce426f
-		  break;
ce426f
-		}
ce426f
-	      else
ce426f
-		runp = runp->next;
ce426f
+	    /* Make sure we reload from file when checking mtime.  */
ce426f
+	    runp->mtime = 0;
ce426f
+#ifdef HAVE_INOTIFY
ce426f
+	    /* During an invalidation we try to reload the traced
ce426f
+	       file watches.  This allows the user to re-sync if
ce426f
+	       inotify events were lost.  Similar to what we do during
ce426f
+	       pruning.  */
ce426f
+	    install_watches (runp);
ce426f
+#endif
ce426f
+	    if (runp->call_res_init)
ce426f
+	      {
ce426f
+		res_init ();
ce426f
+		break;
ce426f
+	      }
ce426f
+	    runp = runp->next;
ce426f
 	  }
ce426f
 	break;
ce426f
-    }
ce426f
+      }
ce426f
 
ce426f
   if (number == lastdb)
ce426f
     {
ce426f
@@ -1871,6 +1910,234 @@
ce426f
 static time_t *starttime;
ce426f
 
ce426f
 
ce426f
+#ifdef HAVE_INOTIFY
ce426f
+/* Inotify event for changed file.  */
ce426f
+union __inev
ce426f
+{
ce426f
+  struct inotify_event i;
ce426f
+# ifndef PATH_MAX
ce426f
+#  define PATH_MAX 1024
ce426f
+# endif
ce426f
+  char buf[sizeof (struct inotify_event) + PATH_MAX];
ce426f
+};
ce426f
+
ce426f
+/* Returns 0 if the file is there otherwise -1.  */
ce426f
+int
ce426f
+check_file (struct traced_file *finfo)
ce426f
+{
ce426f
+  struct stat64 st;
ce426f
+  /* We could check mtime and if different re-add
ce426f
+     the watches, and invalidate the database, but we
ce426f
+     don't because we are called from inotify_check_files
ce426f
+     which should be doing that work.  If sufficient inotify
ce426f
+     events were lost then the next pruning or invalidation
ce426f
+     will do the stat and mtime check.  We don't do it here to
ce426f
+     keep the logic simple.  */
ce426f
+  if (stat64 (finfo->fname, &st) < 0)
ce426f
+    return -1;
ce426f
+  return 0;
ce426f
+}
ce426f
+
ce426f
+/* Process the inotify event in INEV. If the event matches any of the files
ce426f
+   registered with a database then mark that database as requiring its cache
ce426f
+   to be cleared. We indicate the cache needs clearing by setting
ce426f
+   TO_CLEAR[DBCNT] to true for the matching database.  */
ce426f
+static void
ce426f
+inotify_check_files (bool *to_clear, union __inev *inev)
ce426f
+{
ce426f
+  /* Check which of the files changed.  */
ce426f
+  for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
ce426f
+    {
ce426f
+      struct traced_file *finfo = dbs[dbcnt].traced_files;
ce426f
+
ce426f
+      while (finfo != NULL)
ce426f
+	{
ce426f
+	  /* The configuration file was moved or deleted.
ce426f
+	     We stop watching it at that point, and reinitialize.  */
ce426f
+	  if (finfo->inotify_descr[TRACED_FILE] == inev->i.wd
ce426f
+	      && ((inev->i.mask & IN_MOVE_SELF)
ce426f
+		  || (inev->i.mask & IN_DELETE_SELF)
ce426f
+		  || (inev->i.mask & IN_IGNORED)))
ce426f
+	    {
ce426f
+	      int ret;
ce426f
+	      bool moved = (inev->i.mask & IN_MOVE_SELF) != 0;
ce426f
+
ce426f
+	      if (check_file (finfo) == 0)
ce426f
+	        {
ce426f
+		  dbg_log (_("ignored inotify event for `%s` (file exists)"),
ce426f
+			   finfo->fname);
ce426f
+		  return;
ce426f
+		}
ce426f
+
ce426f
+	      dbg_log (_("monitored file `%s` was %s, removing watch"),
ce426f
+		       finfo->fname, moved ? "moved" : "deleted");
ce426f
+	      /* File was moved out, remove the watch.  Watches are
ce426f
+		 automatically removed when the file is deleted.  */
ce426f
+	      if (moved)
ce426f
+		{
ce426f
+		  ret = inotify_rm_watch (inotify_fd, inev->i.wd);
ce426f
+		  if (ret < 0)
ce426f
+		    dbg_log (_("failed to remove file watch `%s`: %s"),
ce426f
+			     finfo->fname, strerror (errno));
ce426f
+		}
ce426f
+	      finfo->inotify_descr[TRACED_FILE] = -1;
ce426f
+	      to_clear[dbcnt] = true;
ce426f
+	      if (finfo->call_res_init)
ce426f
+	        res_init ();
ce426f
+	      return;
ce426f
+	    }
ce426f
+	  /* The configuration file was open for writing and has just closed.
ce426f
+	     We reset the cache and reinitialize.  */
ce426f
+	  if (finfo->inotify_descr[TRACED_FILE] == inev->i.wd
ce426f
+	      && inev->i.mask & IN_CLOSE_WRITE)
ce426f
+	    {
ce426f
+	      /* Mark cache as needing to be cleared and reinitialize.  */
ce426f
+	      dbg_log (_("monitored file `%s` was written to"), finfo->fname);
ce426f
+	      to_clear[dbcnt] = true;
ce426f
+	      if (finfo->call_res_init)
ce426f
+	        res_init ();
ce426f
+	      return;
ce426f
+	    }
ce426f
+	  /* The parent directory was moved or deleted.  We trigger one last
ce426f
+	     invalidation.  At the next pruning or invalidation we may add
ce426f
+	     this watch back if the file is present again.  */
ce426f
+	  if (finfo->inotify_descr[TRACED_DIR] == inev->i.wd
ce426f
+	      && ((inev->i.mask & IN_DELETE_SELF)
ce426f
+		  || (inev->i.mask & IN_MOVE_SELF)
ce426f
+		  || (inev->i.mask & IN_IGNORED)))
ce426f
+	    {
ce426f
+	      bool moved = (inev->i.mask & IN_MOVE_SELF) != 0;
ce426f
+	      /* The directory watch may have already been removed
ce426f
+		 but we don't know so we just remove it again and
ce426f
+		 ignore the error.  Then we remove the file watch.
ce426f
+		 Note: watches are automatically removed for deleted
ce426f
+		 files.  */
ce426f
+	      if (moved)
ce426f
+		inotify_rm_watch (inotify_fd, inev->i.wd);
ce426f
+	      if (finfo->inotify_descr[TRACED_FILE] != -1)
ce426f
+		{
ce426f
+		  dbg_log (_("monitored parent directory `%s` was %s, removing watch on `%s`"),
ce426f
+			   finfo->dname, moved ? "moved" : "deleted", finfo->fname);
ce426f
+		  if (inotify_rm_watch (inotify_fd, finfo->inotify_descr[TRACED_FILE]) < 0)
ce426f
+		    dbg_log (_("failed to remove file watch `%s`: %s"),
ce426f
+			     finfo->dname, strerror (errno));
ce426f
+		}
ce426f
+	      finfo->inotify_descr[TRACED_FILE] = -1;
ce426f
+	      finfo->inotify_descr[TRACED_DIR] = -1;
ce426f
+	      to_clear[dbcnt] = true;
ce426f
+	      if (finfo->call_res_init)
ce426f
+	        res_init ();
ce426f
+	      /* Continue to the next entry since this might be the
ce426f
+		 parent directory for multiple registered files and
ce426f
+		 we want to remove watches for all registered files.  */
ce426f
+	      continue;
ce426f
+	    }
ce426f
+	  /* The parent directory had a create or moved to event.  */
ce426f
+	  if (finfo->inotify_descr[TRACED_DIR] == inev->i.wd
ce426f
+	      && ((inev->i.mask & IN_MOVED_TO)
ce426f
+		  || (inev->i.mask & IN_CREATE))
ce426f
+	      && strcmp (inev->i.name, finfo->sfname) == 0)
ce426f
+	    {
ce426f
+	      /* We detected a directory change.  We look for the creation
ce426f
+		 of the file we are tracking or the move of the same file
ce426f
+		 into the directory.  */
ce426f
+	      int ret;
ce426f
+	      dbg_log (_("monitored file `%s` was %s, adding watch"),
ce426f
+		       finfo->fname,
ce426f
+		       inev->i.mask & IN_CREATE ? "created" : "moved into place");
ce426f
+	      /* File was moved in or created.  Regenerate the watch.  */
ce426f
+	      if (finfo->inotify_descr[TRACED_FILE] != -1)
ce426f
+		inotify_rm_watch (inotify_fd,
ce426f
+				  finfo->inotify_descr[TRACED_FILE]);
ce426f
+
ce426f
+	      ret = inotify_add_watch (inotify_fd,
ce426f
+				       finfo->fname,
ce426f
+				       TRACED_FILE_MASK);
ce426f
+	      if (ret < 0)
ce426f
+		dbg_log (_("failed to add file watch `%s`: %s"),
ce426f
+			 finfo->fname, strerror (errno));
ce426f
+
ce426f
+	      finfo->inotify_descr[TRACED_FILE] = ret;
ce426f
+
ce426f
+	      /* The file is new or moved so mark cache as needing to
ce426f
+		 be cleared and reinitialize.  */
ce426f
+	      to_clear[dbcnt] = true;
ce426f
+	      if (finfo->call_res_init)
ce426f
+		res_init ();
ce426f
+
ce426f
+	      /* Done re-adding the watch.  Don't return, we may still
ce426f
+		 have other files in this same directory, same watch
ce426f
+		 descriptor, and need to process them.  */
ce426f
+	    }
ce426f
+	  /* Other events are ignored, and we move on to the next file.  */
ce426f
+	  finfo = finfo->next;
ce426f
+        }
ce426f
+    }
ce426f
+}
ce426f
+
ce426f
+/* If an entry in the array of booleans TO_CLEAR is TRUE then clear the cache
ce426f
+   for the associated database, otherwise do nothing. The TO_CLEAR array must
ce426f
+   have LASTDB entries.  */
ce426f
+static inline void
ce426f
+clear_db_cache (bool *to_clear)
ce426f
+{
ce426f
+  for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
ce426f
+    if (to_clear[dbcnt])
ce426f
+      {
ce426f
+	pthread_mutex_lock (&dbs[dbcnt].prune_lock);
ce426f
+	dbs[dbcnt].clear_cache = 1;
ce426f
+	pthread_mutex_unlock (&dbs[dbcnt].prune_lock);
ce426f
+	pthread_cond_signal (&dbs[dbcnt].prune_cond);
ce426f
+      }
ce426f
+}
ce426f
+
ce426f
+int
ce426f
+handle_inotify_events (void)
ce426f
+{
ce426f
+  bool to_clear[lastdb] = { false, };
ce426f
+  union __inev inev;
ce426f
+
ce426f
+  /* Read all inotify events for files registered via
ce426f
+     register_traced_file().  */
ce426f
+  while (1)
ce426f
+    {
ce426f
+      /* Potentially read multiple events into buf.  */
ce426f
+      ssize_t nb = TEMP_FAILURE_RETRY (read (inotify_fd,
ce426f
+					     &inev.buf,
ce426f
+					     sizeof (inev)));
ce426f
+      if (nb < (ssize_t) sizeof (struct inotify_event))
ce426f
+	{
ce426f
+	  /* Not even 1 event.  */
ce426f
+	  if (__glibc_unlikely (nb == -1 && errno != EAGAIN))
ce426f
+	    return -1;
ce426f
+	  /* Done reading events that are ready.  */
ce426f
+	  break;
ce426f
+	}
ce426f
+      /* Process all events.  The normal inotify interface delivers
ce426f
+	 complete events on a read and never a partial event.  */
ce426f
+      char *eptr = &inev.buf[0];
ce426f
+      ssize_t count;
ce426f
+      while (1)
ce426f
+	{
ce426f
+	  /* Check which of the files changed.  */
ce426f
+	  inotify_check_files (to_clear, &inev);
ce426f
+	  count = sizeof (struct inotify_event) + inev.i.len;
ce426f
+	  eptr += count;
ce426f
+	  nb -= count;
ce426f
+	  if (nb >= (ssize_t) sizeof (struct inotify_event))
ce426f
+	    memcpy (&inev, eptr, nb);
ce426f
+	  else
ce426f
+	    break;
ce426f
+	}
ce426f
+      continue;
ce426f
+    }
ce426f
+  /* Actually perform the cache clearing.  */
ce426f
+  clear_db_cache (to_clear);
ce426f
+  return 0;
ce426f
+}
ce426f
+
ce426f
+#endif
ce426f
+
ce426f
 static void
ce426f
 __attribute__ ((__noreturn__))
ce426f
 main_loop_poll (void)
ce426f
@@ -1975,72 +2242,21 @@
ce426f
 	    {
ce426f
 	      if (conns[1].revents != 0)
ce426f
 		{
ce426f
-		  bool to_clear[lastdb] = { false, };
ce426f
-		  union
ce426f
-		  {
ce426f
-# ifndef PATH_MAX
ce426f
-#  define PATH_MAX 1024
ce426f
-# endif
ce426f
-		    struct inotify_event i;
ce426f
-		    char buf[sizeof (struct inotify_event) + PATH_MAX];
ce426f
-		  } inev;
ce426f
-
ce426f
-		  while (1)
ce426f
-		    {
ce426f
-		      ssize_t nb = TEMP_FAILURE_RETRY (read (inotify_fd, &inev,
ce426f
-							     sizeof (inev)));
ce426f
-		      if (nb < (ssize_t) sizeof (struct inotify_event))
ce426f
-			{
ce426f
-			  if (__builtin_expect (nb == -1 && errno != EAGAIN,
ce426f
-						0))
ce426f
-			    {
ce426f
-			      /* Something went wrong when reading the inotify
ce426f
-				 data.  Better disable inotify.  */
ce426f
-			      dbg_log (_("\
ce426f
-disabled inotify after read error %d"),
ce426f
-				       errno);
ce426f
-			      conns[1].fd = -1;
ce426f
-			      firstfree = 1;
ce426f
-			      if (nused == 2)
ce426f
-				nused = 1;
ce426f
-			      close (inotify_fd);
ce426f
-			      inotify_fd = -1;
ce426f
-			    }
ce426f
-			  break;
ce426f
-			}
ce426f
-
ce426f
-		      /* Check which of the files changed.  */
ce426f
-		      for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
ce426f
-			{
ce426f
-			  struct traced_file *finfo = dbs[dbcnt].traced_files;
ce426f
-
ce426f
-			  while (finfo != NULL)
ce426f
-			    {
ce426f
-			      if (finfo->inotify_descr == inev.i.wd)
ce426f
-				{
ce426f
-				  to_clear[dbcnt] = true;
ce426f
-				  if (finfo->call_res_init)
ce426f
-				    res_init ();
ce426f
-				  goto next;
ce426f
-				}
ce426f
-
ce426f
-			      finfo = finfo->next;
ce426f
-			    }
ce426f
-			}
ce426f
-		    next:;
ce426f
-		    }
ce426f
-
ce426f
-		  /* Actually perform the cache clearing.  */
ce426f
-		  for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
ce426f
-		    if (to_clear[dbcnt])
ce426f
-		      {
ce426f
-			pthread_mutex_lock (&dbs[dbcnt].prune_lock);
ce426f
-			dbs[dbcnt].clear_cache = 1;
ce426f
-			pthread_mutex_unlock (&dbs[dbcnt].prune_lock);
ce426f
-			pthread_cond_signal (&dbs[dbcnt].prune_cond);
ce426f
-		      }
ce426f
-
ce426f
-		  --n;
ce426f
+                  int ret;
ce426f
+                  ret = handle_inotify_events ();
ce426f
+                  if (ret == -1)
ce426f
+                    {
ce426f
+                      /* Something went wrong when reading the inotify
ce426f
+                         data.  Better disable inotify.  */
ce426f
+                      dbg_log (_("disabled inotify-based monitoring after read error %d"), errno);
ce426f
+                      conns[1].fd = -1;
ce426f
+                      firstfree = 1;
ce426f
+                      if (nused == 2)
ce426f
+                        nused = 1;
ce426f
+                      close (inotify_fd);
ce426f
+                      inotify_fd = -1;
ce426f
+                    }
ce426f
+                  --n;
ce426f
 		}
ce426f
 
ce426f
 	      first = 2;
ce426f
@@ -2207,64 +2423,18 @@
ce426f
 # ifdef HAVE_INOTIFY
ce426f
 	else if (revs[cnt].data.fd == inotify_fd)
ce426f
 	  {
ce426f
-	    bool to_clear[lastdb] = { false, };
ce426f
-	    union
ce426f
-	    {
ce426f
-	      struct inotify_event i;
ce426f
-	      char buf[sizeof (struct inotify_event) + PATH_MAX];
ce426f
-	    } inev;
ce426f
-
ce426f
-	    while (1)
ce426f
+	    int ret;
ce426f
+	    ret = handle_inotify_events ();
ce426f
+	    if (ret == -1)
ce426f
 	      {
ce426f
-		ssize_t nb = TEMP_FAILURE_RETRY (read (inotify_fd, &inev,
ce426f
-						 sizeof (inev)));
ce426f
-		if (nb < (ssize_t) sizeof (struct inotify_event))
ce426f
-		  {
ce426f
-		    if (__builtin_expect (nb == -1 && errno != EAGAIN, 0))
ce426f
-		      {
ce426f
-			/* Something went wrong when reading the inotify
ce426f
-			   data.  Better disable inotify.  */
ce426f
-			dbg_log (_("disabled inotify after read error %d"),
ce426f
-				 errno);
ce426f
-			(void) epoll_ctl (efd, EPOLL_CTL_DEL, inotify_fd,
ce426f
-					  NULL);
ce426f
-			close (inotify_fd);
ce426f
-			inotify_fd = -1;
ce426f
-		      }
ce426f
-		    break;
ce426f
-		  }
ce426f
-
ce426f
-		/* Check which of the files changed.  */
ce426f
-		for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
ce426f
-		  {
ce426f
-		    struct traced_file *finfo = dbs[dbcnt].traced_files;
ce426f
-
ce426f
-		    while (finfo != NULL)
ce426f
-		      {
ce426f
-			if (finfo->inotify_descr == inev.i.wd)
ce426f
-			  {
ce426f
-			    to_clear[dbcnt] = true;
ce426f
-			    if (finfo->call_res_init)
ce426f
-			      res_init ();
ce426f
-			    goto next;
ce426f
-			  }
ce426f
-
ce426f
-			finfo = finfo->next;
ce426f
-		      }
ce426f
-		  }
ce426f
-	      next:;
ce426f
-	      }
ce426f
-
ce426f
-	    /* Actually perform the cache clearing.  */
ce426f
-	    for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
ce426f
-	      if (to_clear[dbcnt])
ce426f
-		{
ce426f
-		  pthread_mutex_lock (&dbs[dbcnt].prune_lock);
ce426f
-		  dbs[dbcnt].clear_cache = 1;
ce426f
-		  pthread_mutex_unlock (&dbs[dbcnt].prune_lock);
ce426f
-		  pthread_cond_signal (&dbs[dbcnt].prune_cond);
ce426f
-		}
ce426f
-	  }
ce426f
+		/* Something went wrong when reading the inotify
ce426f
+		   data.  Better disable inotify.  */
ce426f
+		dbg_log (_("disabled inotify-based monitoring after read error %d"), errno);
ce426f
+                close (inotify_fd);
ce426f
+                inotify_fd = -1;
ce426f
+                break;
ce426f
+              }
ce426f
+          }
ce426f
 # endif
ce426f
 # ifdef HAVE_NETLINK
ce426f
 	else if (revs[cnt].data.fd == nl_status_fd)
ce426f
@@ -2300,7 +2470,9 @@
ce426f
 	  no reply in too long of a time.  */
ce426f
       time_t laststart = now - ACCEPT_TIMEOUT;
ce426f
       assert (starttime[sock] == 0);
ce426f
+# ifdef HAVE_INOTIFY
ce426f
       assert (inotify_fd == -1 || starttime[inotify_fd] == 0);
ce426f
+# endif
ce426f
       assert (nl_status_fd == -1 || starttime[nl_status_fd] == 0);
ce426f
       for (int cnt = highest; cnt > STDERR_FILENO; --cnt)
ce426f
 	if (starttime[cnt] != 0 && starttime[cnt] < laststart)
ce426f
--- glibc-2.17-c758a686/nscd/nscd.h	2015-05-12 15:03:02.870274443 -0400
ce426f
+++ glibc-2.17-c758a686/nscd/nscd.h	2015-05-13 13:45:57.259958374 -0400
ce426f
@@ -61,17 +61,67 @@
ce426f
    80% of the thread stack size.  */
ce426f
 #define MAX_STACK_USE ((8 * NSCD_THREAD_STACKSIZE) / 10)
ce426f
 
ce426f
-
ce426f
-/* Registered filename used to fill database.  */
ce426f
+/* Records the file registered per database that when changed
ce426f
+   or modified requires invalidating the database.  */
ce426f
 struct traced_file
ce426f
 {
ce426f
+  /* Tracks the last modified time of the traced file.  */
ce426f
   time_t mtime;
ce426f
+  /* Support multiple registered files per database.  */
ce426f
   struct traced_file *next;
ce426f
   int call_res_init;
ce426f
-  int inotify_descr;
ce426f
+  /* Requires Inotify support to do anything useful.  */
ce426f
+#define TRACED_FILE	0
ce426f
+#define TRACED_DIR	1
ce426f
+  int inotify_descr[2];
ce426f
+# ifndef PATH_MAX
ce426f
+#  define PATH_MAX 1024
ce426f
+# endif
ce426f
+  /* The parent directory is used to scan for creation/deletion.  */
ce426f
+  char dname[PATH_MAX];
ce426f
+  /* Just the name of the file with no directory component.  */
ce426f
+  char *sfname;
ce426f
+  /* The full-path name of the registered file.  */
ce426f
   char fname[];
ce426f
 };
ce426f
 
ce426f
+/* Initialize a `struct traced_file`.  As input we need the name
ce426f
+   of the file, and if invalidation requires calling res_init.
ce426f
+   If CRINIT is 1 then res_init will be called after invalidation
ce426f
+   or if the traced file is changed in any way, otherwise it will
ce426f
+   not.  */
ce426f
+static inline void
ce426f
+init_traced_file(struct traced_file *file, const char *fname, int crinit)
ce426f
+{
ce426f
+   char *dname;
ce426f
+   file->mtime = 0;
ce426f
+   file->inotify_descr[TRACED_FILE] = -1;
ce426f
+   file->inotify_descr[TRACED_DIR] = -1;
ce426f
+   strcpy (file->fname, fname);
ce426f
+   /* Compute the parent directory name and store a copy.  The copy makes
ce426f
+      it much faster to add/remove watches while nscd is running instead
ce426f
+      of computing this over and over again in a temp buffer.  */
ce426f
+   file->dname[0] = '\0';
ce426f
+   dname = strrchr (fname, '/');
ce426f
+   if (dname != NULL)
ce426f
+     {
ce426f
+       size_t len = (size_t)(dname - fname);
ce426f
+       if (len > sizeof (file->dname))
ce426f
+	 abort ();
ce426f
+       strncpy (file->dname, file->fname, len);
ce426f
+       file->dname[len] = '\0';
ce426f
+     }
ce426f
+   /* The basename is the name just after the last forward slash.  */
ce426f
+   file->sfname = &dname[1];
ce426f
+   file->call_res_init = crinit;
ce426f
+}
ce426f
+
ce426f
+#define define_traced_file(id, filename) 			\
ce426f
+static union							\
ce426f
+{								\
ce426f
+  struct traced_file file;					\
ce426f
+  char buf[sizeof (struct traced_file) + sizeof (filename)];	\
ce426f
+} id##_traced_file;
ce426f
 
ce426f
 /* Structure describing dynamic part of one database.  */
ce426f
 struct database_dyn
ce426f
@@ -90,7 +140,6 @@
ce426f
   int propagate;
ce426f
   struct traced_file *traced_files;
ce426f
   const char *db_filename;
ce426f
-  time_t file_mtime;
ce426f
   size_t suggested_module;
ce426f
   size_t max_db_size;
ce426f
 
ce426f
@@ -216,6 +265,9 @@
ce426f
 /* connections.c */
ce426f
 extern void nscd_init (void);
ce426f
 extern void register_traced_file (size_t dbidx, struct traced_file *finfo);
ce426f
+#ifdef HAVE_INOTIFY
ce426f
+extern void install_watches (struct traced_file *finfo);
ce426f
+#endif
ce426f
 extern void close_sockets (void);
ce426f
 extern void start_threads (void) __attribute__ ((__noreturn__));
ce426f
 
ce426f
--- glibc-2.17-c758a686/nss/nss_db/db-init.c	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/nss/nss_db/db-init.c	2015-05-13 13:45:57.269958504 -0400
ce426f
@@ -22,35 +22,25 @@
ce426f
 #include <nscd/nscd.h>
ce426f
 #include <string.h>
ce426f
 
ce426f
-static union
ce426f
-{
ce426f
-  struct traced_file file;
ce426f
-  char buf[sizeof (struct traced_file) + sizeof (_PATH_VARDB "passwd.db")];
ce426f
-} pwd_traced_file;
ce426f
-
ce426f
-static union
ce426f
-{
ce426f
-  struct traced_file file;
ce426f
-  char buf[sizeof (struct traced_file) + sizeof (_PATH_VARDB "group.db")];
ce426f
-} grp_traced_file;
ce426f
+#define PWD_FILENAME (_PATH_VARDB "passwd.db")
ce426f
+define_traced_file (pwd, PWD_FILENAME);
ce426f
 
ce426f
-static union
ce426f
-{
ce426f
-  struct traced_file file;
ce426f
-  char buf[sizeof (struct traced_file) + sizeof (_PATH_VARDB "services.db")];
ce426f
-} serv_traced_file;
ce426f
+#define GRP_FILENAME (_PATH_VARDB "group.db")
ce426f
+define_traced_file (grp, GRP_FILENAME);
ce426f
 
ce426f
+#define SERV_FILENAME (_PATH_VARDB "services.db")
ce426f
+define_traced_file (serv, SERV_FILENAME);
ce426f
 
ce426f
 void
ce426f
 _nss_db_init (void (*cb) (size_t, struct traced_file *))
ce426f
 {
ce426f
-  strcpy (pwd_traced_file.file.fname,_PATH_VARDB  "passwd.db");
ce426f
+  init_traced_file (&pwd_traced_file.file, PWD_FILENAME, 0);
ce426f
   cb (pwddb, &pwd_traced_file.file);
ce426f
 
ce426f
-  strcpy (grp_traced_file.file.fname, _PATH_VARDB "group.db");
ce426f
+  init_traced_file (&grp_traced_file.file, GRP_FILENAME, 0);
ce426f
   cb (grpdb, &grp_traced_file.file);
ce426f
 
ce426f
-  strcpy (serv_traced_file.file.fname, _PATH_VARDB "services.db");
ce426f
+  init_traced_file (&serv_traced_file.file, SERV_FILENAME, 0);
ce426f
   cb (servdb, &serv_traced_file.file);
ce426f
 }
ce426f
 
ce426f
--- glibc-2.17-c758a686/nss/nss_files/files-init.c	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/nss/nss_files/files-init.c	2015-05-13 13:45:57.269958504 -0400
ce426f
@@ -18,43 +18,46 @@
ce426f
 
ce426f
 #ifdef USE_NSCD
ce426f
 
ce426f
+#include <string.h>
ce426f
 #include <nscd/nscd.h>
ce426f
 
ce426f
+#define PWD_FILENAME "/etc/passwd"
ce426f
+define_traced_file (pwd, PWD_FILENAME);
ce426f
 
ce426f
-#define TF(id, filename, ...)					\
ce426f
-static union							\
ce426f
-{								\
ce426f
-  struct traced_file file;					\
ce426f
-  char buf[sizeof (struct traced_file) + sizeof (filename)];	\
ce426f
-} id##_traced_file =						\
ce426f
-  {								\
ce426f
-    .file =							\
ce426f
-    {								\
ce426f
-      .fname = filename, ## __VA_ARGS__				\
ce426f
-    }								\
ce426f
-  }
ce426f
-
ce426f
-TF (pwd, "/etc/passwd");
ce426f
-TF (grp, "/etc/group");
ce426f
-TF (hst, "/etc/hosts");
ce426f
-TF (resolv, "/etc/resolv.conf", .call_res_init = 1);
ce426f
-TF (serv, "/etc/services");
ce426f
-TF (netgr, "/etc/netgroup");
ce426f
-
ce426f
-
ce426f
+#define GRP_FILENAME "/etc/group"
ce426f
+define_traced_file (grp, GRP_FILENAME);
ce426f
+ 
ce426f
+#define HST_FILENAME "/etc/hosts"
ce426f
+define_traced_file (hst, HST_FILENAME);
ce426f
+
ce426f
+#define RESOLV_FILENAME "/etc/resolv.conf"
ce426f
+define_traced_file (resolv, RESOLV_FILENAME);
ce426f
+
ce426f
+#define SERV_FILENAME "/etc/services"
ce426f
+define_traced_file (serv, SERV_FILENAME);
ce426f
+
ce426f
+#define NETGR_FILENAME "/etc/netgroup"
ce426f
+define_traced_file (netgr, NETGR_FILENAME);
ce426f
+ 
ce426f
 void
ce426f
 _nss_files_init (void (*cb) (size_t, struct traced_file *))
ce426f
 {
ce426f
+  init_traced_file (&pwd_traced_file.file, PWD_FILENAME, 0);
ce426f
   cb (pwddb, &pwd_traced_file.file);
ce426f
 
ce426f
+  init_traced_file (&grp_traced_file.file, GRP_FILENAME, 0);
ce426f
   cb (grpdb, &grp_traced_file.file);
ce426f
 
ce426f
+  init_traced_file (&hst_traced_file.file, HST_FILENAME, 0);
ce426f
   cb (hstdb, &hst_traced_file.file);
ce426f
 
ce426f
+  init_traced_file (&resolv_traced_file.file, RESOLV_FILENAME, 1);
ce426f
   cb (hstdb, &resolv_traced_file.file);
ce426f
 
ce426f
+  init_traced_file (&serv_traced_file.file, SERV_FILENAME, 0);
ce426f
   cb (servdb, &serv_traced_file.file);
ce426f
-
ce426f
+ 
ce426f
+  init_traced_file (&netgr_traced_file.file, NETGR_FILENAME, 0);
ce426f
   cb (netgrdb, &netgr_traced_file.file);
ce426f
 }
ce426f