dbus-monitor: keep backwards compatibility
authorChengwei Yang <chengwei.yang@intel.com>
Wed, 9 Oct 2013 02:44:12 +0000 (10:44 +0800)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Wed, 9 Oct 2013 10:26:46 +0000 (11:26 +0100)
eavesdropping as a match rule key introduced in DBus 1.5.6, and the
privous implementation doesn't keep backwards compatibility with older
dbus-daemon.

And the reference dbus-daemon implementation just fail if unknwon key
found in match rule, this is undefined hehavior in DBus Sepcification.

Also there is a feature request for change this hehavior to
"ignore unknown key in match rule", See
https://bugs.freedesktop.org/show_bug.cgi?id=66114

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=66107
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
tools/dbus-monitor.c

index 1aa885f..ff8390d 100644 (file)
@@ -367,26 +367,45 @@ main (int argc, char *argv[])
 
   if (numFilters)
     {
+      size_t offset = 0;
       for (i = 0; i < j; i++)
         {
-          dbus_bus_add_match (connection, filters[i], &error);
-          if (dbus_error_is_set (&error))
+          dbus_bus_add_match (connection, filters[i] + offset, &error);
+          if (dbus_error_is_set (&error) && i == 0 && offset == 0)
+            {
+              /* We might be talking to a pre-1.5.6 dbus-daemon
+              * which wouldn't understand eavesdrop=true.
+              * If this works, carry on with offset > 0
+              * on the remaining iterations. */
+              offset = strlen (EAVESDROPPING_RULE) + 1;
+              dbus_error_free (&error);
+              dbus_bus_add_match (connection, filters[i] + offset, &error);
+            }
+
+         if (dbus_error_is_set (&error))
             {
               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
                        filters[i], error.message);
               dbus_error_free (&error);
               exit (1);
             }
-         free(filters[i]);
+          free(filters[i]);
         }
     }
   else
     {
       dbus_bus_add_match (connection,
-                         EAVESDROPPING_RULE,
-                         &error);
+                          EAVESDROPPING_RULE,
+                          &error);
       if (dbus_error_is_set (&error))
-        goto lose;
+        {
+          dbus_error_free (&error);
+          dbus_bus_add_match (connection,
+                              "",
+                              &error);
+          if (dbus_error_is_set (&error))
+            goto lose;
+        }
     }
 
   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {