Add "eavesdrop=true" as constant match rule for dbus-monitor
authorCosimo Alfarano <cosimo.alfarano@collabora.co.uk>
Thu, 7 Jul 2011 15:10:54 +0000 (16:10 +0100)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Mon, 11 Jul 2011 16:03:54 +0000 (17:03 +0100)
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=37890
Bug-NB: NB#269748
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
tools/dbus-monitor.c

index 5edb5c0..ba9f9cc 100644 (file)
@@ -35,6 +35,8 @@
 
 #include "dbus-print-message.h"
 
+#define EAVESDROPPING_RULE "eavesdrop=true"
+
 #ifdef DBUS_WIN
 
 /* gettimeofday is not defined on windows */
@@ -76,6 +78,13 @@ gettimeofday (struct timeval *__p,
 }
 #endif
 
+inline static void
+oom (const char *doing)
+{
+  fprintf (stderr, "OOM while %s\n", doing);
+  exit (1);
+}
+
 static DBusHandlerResult
 monitor_filter_func (DBusConnection     *connection,
                     DBusMessage        *message,
@@ -299,11 +308,21 @@ main (int argc, char *argv[])
       else if (arg[0] == '-')
        usage (argv[0], 1);
       else {
-       numFilters++;
-       filters = (char **)realloc(filters, numFilters * sizeof(char *));
-       filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
-       snprintf(filters[j], strlen(arg) + 1, "%s", arg);
-       j++;
+          unsigned int filter_len;
+          numFilters++;
+          /* Prepend a rule (and a comma) to enable the monitor to eavesdrop.
+           * Prepending allows the user to add eavesdrop=false at command line
+           * in order to disable eavesdropping when needed */
+          filter_len = strlen (EAVESDROPPING_RULE) + 1 + strlen (arg) + 1;
+
+          filters = (char **) realloc (filters, numFilters * sizeof (char *));
+          if (filters == NULL)
+            oom ("adding a new filter slot");
+          filters[j] = (char *) malloc (filter_len * sizeof (char *));
+          if (filters[j] == NULL)
+            oom ("adding a new filter");
+          snprintf (filters[j], filter_len, "%s,%s", EAVESDROPPING_RULE, arg);
+          j++;
       }
     }