]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
bus: Don't crash if inotify fails, even under DBUS_FATAL_WARNINGS=1
authorSimon McVittie <smcv@collabora.com>
Fri, 18 Aug 2023 16:15:32 +0000 (17:15 +0100)
committerSimon McVittie <smcv@collabora.com>
Fri, 18 Aug 2023 18:56:59 +0000 (18:56 +0000)
When running tests, we use DBUS_FATAL_WARNINGS=1 to make the tests fail
on internal errors. Failing to set up inotify is not really an internal
error: it's more like an environmental error, which can occur for
reasons outside our control.

Instead, log using bus_context_log(), which never crashes the process
and always just logs a warning.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Resolves: https://gitlab.freedesktop.org/dbus/dbus/-/issues/473

bus/dir-watch-inotify.c

index c8462c74f8cd21240381ad58840e155fcc53fff5..77b2d5a927fe3336ee18fe825b6f9395beca4f43 100644 (file)
@@ -95,13 +95,18 @@ _handle_inotify_watch (DBusWatch *passed_watch, unsigned int flags, void *data)
 #include <stdio.h>
 
 static void
-_set_watched_dirs_internal (DBusList **directories)
+_set_watched_dirs_internal (BusContext *context,
+                            DBusList **directories)
 {
   int new_wds[MAX_DIRS_TO_WATCH];
   char *new_dirs[MAX_DIRS_TO_WATCH];
   DBusList *link;
   int i, j, wd;
 
+  /* Callers must provide a context, except during shutdown, at which
+   * point the list of directories must be empty. */
+  _dbus_assert (context != NULL || *directories == NULL);
+
   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
     {
       new_wds[i] = -1;
@@ -116,9 +121,11 @@ _set_watched_dirs_internal (DBusList **directories)
       link = _dbus_list_get_next_link (directories, link);
     }
 
-  if (link != NULL)
+  if (link != NULL && context != NULL)
     {
-      _dbus_warn ("Too many directories to watch them all, only watching first %d.", MAX_DIRS_TO_WATCH);
+      bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
+                       "Too many directories to watch them all, only watching first %d.",
+                       MAX_DIRS_TO_WATCH);
     }
 
   /* Look for directories in both the old and new sets, if
@@ -164,7 +171,12 @@ _set_watched_dirs_internal (DBusList **directories)
               /* Not all service directories need to exist. */
               if (errno != ENOENT)
                 {
-                  _dbus_warn ("Cannot setup inotify for '%s'; error '%s'", new_dirs[i], _dbus_strerror (errno));
+                  /* We only have context == NULL during shutdown, at which
+                   * point we are not adding directories */
+                  _dbus_assert (context != NULL);
+                  bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
+                                   "Cannot set up inotify for '%s': %s",
+                                   new_dirs[i], _dbus_strerror (errno));
                   goto out;
                 }
               else
@@ -208,7 +220,7 @@ _shutdown_inotify (void *data)
   if (inotify_fd == -1)
     return;
 
-  _set_watched_dirs_internal (&empty);
+  _set_watched_dirs_internal (NULL, &empty);
 
   if (watch != NULL)
     {
@@ -243,7 +255,9 @@ _init_inotify (BusContext *context)
 #endif
       if (inotify_fd < 0)
         {
-          _dbus_warn ("Cannot initialize inotify: %s", _dbus_strerror (errno));
+          bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
+                           "Cannot initialize inotify: %s",
+                           _dbus_strerror (errno));
           goto out;
         }
 
@@ -259,13 +273,15 @@ _init_inotify (BusContext *context)
 
       if (watch == NULL)
         {
-          _dbus_warn ("Unable to create inotify watch");
+          bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
+                           "Unable to create inotify watch");
           goto out;
         }
 
       if (!_dbus_loop_add_watch (loop, watch))
         {
-          _dbus_warn ("Unable to add reload watch to main loop");
+          bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
+                           "Unable to add reload watch to main loop");
           _dbus_watch_unref (watch);
           watch = NULL;
           goto out;
@@ -273,7 +289,8 @@ _init_inotify (BusContext *context)
 
       if (!_dbus_register_shutdown_func (_shutdown_inotify, NULL))
       {
-          _dbus_warn ("Unable to register shutdown func");
+          bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
+                           "Unable to register shutdown func");
           _dbus_watch_unref (watch);
           watch = NULL;
           goto out;
@@ -292,5 +309,5 @@ bus_set_watched_dirs (BusContext *context, DBusList **directories)
   if (!_init_inotify (context))
     return;
 
-  _set_watched_dirs_internal (directories);
+  _set_watched_dirs_internal (context, directories);
 }