From 0fb626ef53c5d2e5138092d2388587f6b907b6f7 Mon Sep 17 00:00:00 2001 From: Cosimo Alfarano Date: Tue, 12 Jul 2011 17:40:34 +0100 Subject: [PATCH 1/3] Add eavesdrop=true when supported Bug https://bugs.freedesktop.org/show_bug.cgi?id=39140 --- bustle-dbus-monitor.c | 91 +++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 80 insertions(+), 11 deletions(-) diff --git a/bustle-dbus-monitor.c b/bustle-dbus-monitor.c index 0ebaded..3b9326d 100644 --- a/bustle-dbus-monitor.c +++ b/bustle-dbus-monitor.c @@ -67,6 +67,15 @@ gettimeofday (struct timeval *__p, #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu" #define TRAP_NULL_STRING(str) ((str) ? (str) : "") +#define EAVESDROPPING_RULE "eavesdrop=true" + +inline static void +oom (const char *doing) +{ + fprintf (stderr, "OOM while %s\n", doing); + exit (1); +} + static void print_name_owner_changed (struct timeval *t, const char *name, @@ -315,6 +324,7 @@ main (int argc, char *argv[]) DBusError error = DBUS_ERROR_INIT; DBusBusType type = DBUS_BUS_SESSION; DBusHandleMessageFunction filter_func = profile_filter_func; + dbus_bool_t supports_eavesdrop_keyword = TRUE; int i = 0, j = 0, numFilters = 0; char **filters = NULL; @@ -357,41 +367,100 @@ main (int argc, char *argv[]) get_well_known_names (connection); + /* check support for eavesdrop=true in filter */ + dbus_bus_add_match (connection, EAVESDROPPING_RULE, &error); + if (dbus_error_is_set (&error) && + strcmp (error.name, DBUS_ERROR_MATCH_RULE_INVALID) == 0) + { + /* make it re-usable */ + dbus_error_free (&error); + dbus_error_init (&error); + + /* using a dbus version witout this feature, too bad, but still OK */ + supports_eavesdrop_keyword = FALSE; + } + else if (dbus_error_is_set (&error)) /* but not RULE_INVALID */ + { + fprintf (stderr, "Failed to set rule \"%s\": %s\n", + EAVESDROPPING_RULE, error.message); + dbus_error_free (&error); + exit (1); + } + else /* we support it! cleanup and go! */ + { + dbus_bus_remove_match (connection, EAVESDROPPING_RULE, &error); + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "Failed to removed rule \"%s\": %s\n", + EAVESDROPPING_RULE, error.message); + dbus_error_free (&error); + exit (1); + } + } + if (numFilters) { for (i = 0; i < j; i++) { - dbus_bus_add_match (connection, filters[i], &error); + char *filter = filters[i]; /* by default we use the user supplied + filter */ + + if (supports_eavesdrop_keyword) + { + /* prepend eavesdrop=true to the filter rule, amending the user + * supplied rule. User can disable it using "eavesdrop=false" */ + unsigned int eavesdrop_filter_len; + + eavesdrop_filter_len = strlen (EAVESDROPPING_RULE) + 1 + \ + strlen (filters[i]) + 1; + + filter = malloc (eavesdrop_filter_len * sizeof(char *)); + if (filter == NULL) + oom ("creating rule with eavesdrop=true"); + + snprintf(filter, eavesdrop_filter_len, "%s,%s", + EAVESDROPPING_RULE, filters[i]); + } + + dbus_bus_add_match (connection, filter, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "Failed to setup match \"%s\": %s\n", - filters[i], error.message); + filter, error.message); dbus_error_free (&error); exit (1); } - free(filters[i]); + + free (filters[i]); + if (supports_eavesdrop_keyword) + free (filter); } } else { dbus_bus_add_match (connection, - "type='signal'", - &error); + supports_eavesdrop_keyword ? + EAVESDROPPING_RULE ",type='signal'" : "type='signal'", + &error); if (dbus_error_is_set (&error)) goto lose; dbus_bus_add_match (connection, - "type='method_call'", - &error); + supports_eavesdrop_keyword ? + EAVESDROPPING_RULE ",type='method_call'" : "type='method_call'", + &error); if (dbus_error_is_set (&error)) goto lose; dbus_bus_add_match (connection, - "type='method_return'", - &error); + supports_eavesdrop_keyword ? + EAVESDROPPING_RULE ",type='method_return'" : + "type='method_return'", + &error); if (dbus_error_is_set (&error)) goto lose; dbus_bus_add_match (connection, - "type='error'", - &error); + supports_eavesdrop_keyword ? + EAVESDROPPING_RULE ",type='error'" : "type='error'", + &error); if (dbus_error_is_set (&error)) goto lose; } -- 1.7.5.4