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