Fix inotify shutdown
authorColin Walters <walters@verbum.org>
Tue, 2 Feb 2010 17:37:17 +0000 (12:37 -0500)
committerColin Walters <walters@verbum.org>
Wed, 3 Feb 2010 17:05:28 +0000 (12:05 -0500)
We were incorrectly passing NULL for a DBusList when the usage expected
is a pointer to a NULL DBusList pointer.  Also during dbus_shutdown
we need to actually close the inotify fd, and remove our watch.

Move the shutdown handler out of bus.c and into inotify where we
can do all of this cleanly.
(cherry picked from commit 90fe96b1875350f86a4a773d4a0a22009950dd4d)

bus/bus.c
bus/dir-watch-inotify.c

index c748353..eefdaeb 100644 (file)
--- a/bus/bus.c
+++ b/bus/bus.c
@@ -562,12 +562,6 @@ process_config_postinit (BusContext      *context,
   return TRUE;
 }
 
-static void
-bus_shutdown_all_directory_watches (void *data)
-{
-  bus_set_watched_dirs ((BusContext *) data, NULL);
-}
-
 BusContext*
 bus_context_new (const DBusString *config_file,
                  ForceForkSetting  force_fork,
@@ -599,8 +593,6 @@ bus_context_new (const DBusString *config_file,
 
   _dbus_generate_uuid (&context->uuid);
 
-  _dbus_register_shutdown_func (bus_shutdown_all_directory_watches, context);
-
   if (!_dbus_string_copy_data (config_file, &context->config_file))
     {
       BUS_SET_OOM (error);
index d00549a..f457ea8 100644 (file)
@@ -91,59 +91,16 @@ _handle_inotify_watch (DBusWatch *passed_watch, unsigned int flags, void *data)
   return TRUE;
 }
 
-static int
-_init_inotify (BusContext *context)
-{
-  int ret = 0;
-
-  if (inotify_fd == -1) {
-#ifdef HAVE_INOTIFY_INIT1
-     inotify_fd = inotify_init1 (IN_CLOEXEC);
-#else
-     inotify_fd = inotify_init ();
-#endif
-     if (inotify_fd <= 0) {
-      _dbus_warn ("Cannot initialize inotify\n");
-      goto out;
-     } 
-     loop = bus_context_get_loop (context);
-
-     watch = _dbus_watch_new (inotify_fd, DBUS_WATCH_READABLE, TRUE,
-                              _handle_inotify_watch, NULL, NULL);
-
-     if (watch == NULL)
-       {
-         _dbus_warn ("Unable to create inotify watch\n");
-         goto out;
-       }
-
-     if (!_dbus_loop_add_watch (loop, watch, _inotify_watch_callback,
-                                NULL, NULL))
-       {
-         _dbus_warn ("Unable to add reload watch to main loop");
-        _dbus_watch_unref (watch);
-        watch = NULL;
-         goto out;
-       }
-  }
+#include <stdio.h>
 
-  ret = 1;
-
-out:
-  return ret;
-}
-
-void
-bus_set_watched_dirs (BusContext *context, DBusList **directories)
+static void
+_set_watched_dirs_internal (DBusList **directories)
 {
   int new_wds[MAX_DIRS_TO_WATCH];
   char *new_dirs[MAX_DIRS_TO_WATCH];
   DBusList *link;
   int i, j, wd;
 
-  if (!_init_inotify (context))
-    goto out;
-
   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
     {
       new_wds[i] = -1;
@@ -225,3 +182,82 @@ bus_set_watched_dirs (BusContext *context, DBusList **directories)
 
  out:;
 }
+
+#include <stdio.h>
+static void
+_shutdown_inotify (void *data)
+{
+  DBusList *empty = NULL;
+
+  if (inotify_fd == -1)
+    return;
+
+  _set_watched_dirs_internal (&empty);
+
+  close (inotify_fd);
+  inotify_fd = -1;
+  if (watch != NULL)
+    {
+      _dbus_loop_remove_watch (loop, watch, _inotify_watch_callback, NULL);
+      _dbus_watch_unref (watch);
+      _dbus_loop_unref (loop);
+    }
+  watch = NULL;
+  loop = NULL;
+}
+
+static int
+_init_inotify (BusContext *context)
+{
+  int ret = 0;
+
+  if (inotify_fd == -1)
+    {
+#ifdef HAVE_INOTIFY_INIT1
+      inotify_fd = inotify_init1 (IN_CLOEXEC);
+#else
+      inotify_fd = inotify_init ();
+#endif
+      if (inotify_fd <= 0)
+        {
+          _dbus_warn ("Cannot initialize inotify\n");
+          goto out;
+        }
+      loop = bus_context_get_loop (context);
+      _dbus_loop_ref (loop);
+
+      watch = _dbus_watch_new (inotify_fd, DBUS_WATCH_READABLE, TRUE,
+                               _handle_inotify_watch, NULL, NULL);
+
+      if (watch == NULL)
+        {
+          _dbus_warn ("Unable to create inotify watch\n");
+          goto out;
+        }
+
+      if (!_dbus_loop_add_watch (loop, watch, _inotify_watch_callback,
+                                 NULL, NULL))
+        {
+          _dbus_warn ("Unable to add reload watch to main loop");
+          _dbus_watch_unref (watch);
+          watch = NULL;
+          goto out;
+        }
+
+      _dbus_register_shutdown_func (_shutdown_inotify, NULL);
+    }
+
+  ret = 1;
+
+out:
+  return ret;
+}
+
+void
+bus_set_watched_dirs (BusContext *context, DBusList **directories)
+{
+  if (!_init_inotify (context))
+    return;
+
+  _set_watched_dirs_internal (directories);
+}