From 005e631eff03cd6c027cbd3a452d9f13c7f88366 Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Fri, 25 Sep 2009 17:36:32 +0100 Subject: [PATCH] Add GetConnectionMatchRules in the bus driver --- bus/driver.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ bus/signals.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- bus/signals.h | 4 ++ 3 files changed, 170 insertions(+), 5 deletions(-) diff --git a/bus/driver.c b/bus/driver.c index 4a6a324..1d8227f 100644 --- a/bus/driver.c +++ b/bus/driver.c @@ -1400,6 +1400,86 @@ bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection, } static dbus_bool_t +bus_driver_handle_get_connection_match_rules (DBusConnection *connection, + BusTransaction *transaction, + DBusMessage *message, + DBusError *error) +{ + const char *service; + DBusString str; + BusRegistry *registry; + BusService *serv; + DBusConnection *conn; + DBusMessage *reply; + unsigned long pid; + dbus_uint32_t pid32; + BusContext *context; + BusMatchmaker *matchmaker; + DBusString rule_text; + char *reply_str; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + registry = bus_connection_get_registry (connection); + + service = NULL; + reply = NULL; + + if (! dbus_message_get_args (message, error, + DBUS_TYPE_STRING, &service, + DBUS_TYPE_INVALID)) + goto failed; + + _dbus_verbose ("asked for PID of connection %s\n", service); + + _dbus_string_init_const (&str, service); + serv = bus_registry_lookup (registry, &str); + if (serv == NULL) + { + dbus_set_error (error, + DBUS_ERROR_NAME_HAS_NO_OWNER, + "Could not get PID of name '%s': no such name", service); + goto failed; + } + + conn = bus_service_get_primary_owners_connection (serv); + + reply = dbus_message_new_method_return (message); + if (reply == NULL) + goto oom; + + + context = bus_transaction_get_context (transaction); + matchmaker = bus_context_get_matchmaker (context); + + bus_match_rule_dump (matchmaker, &rule_text, conn); + + reply_str = _dbus_string_get_data (&rule_text); + + if (! dbus_message_append_args (reply, + DBUS_TYPE_STRING, + &reply_str, + DBUS_TYPE_INVALID)) + goto oom; + + if (! bus_transaction_send_from_driver (transaction, connection, reply)) + goto oom; + + dbus_message_unref (reply); + + return TRUE; + + oom: + BUS_SET_OOM (error); + + failed: + _DBUS_ASSERT_ERROR_IS_SET (error); + if (reply) + dbus_message_unref (reply); + return FALSE; +} + +static dbus_bool_t bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection, BusTransaction *transaction, DBusMessage *message, @@ -1709,6 +1789,10 @@ static struct DBUS_TYPE_STRING_AS_STRING, DBUS_TYPE_UINT32_AS_STRING, bus_driver_handle_get_connection_unix_process_id }, + { "GetConnectionMatchRules", + DBUS_TYPE_STRING_AS_STRING, + DBUS_TYPE_STRING_AS_STRING, + bus_driver_handle_get_connection_match_rules }, { "GetAdtAuditSessionData", DBUS_TYPE_STRING_AS_STRING, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, diff --git a/bus/signals.c b/bus/signals.c index dbfbb5c..46edc5f 100644 --- a/bus/signals.c +++ b/bus/signals.c @@ -113,7 +113,6 @@ bus_match_rule_unref (BusMatchRule *rule) } } -#ifdef DBUS_ENABLE_VERBOSE_MODE /* Note this function does not do escaping, so it's only * good for debug spew at the moment */ @@ -133,9 +132,36 @@ match_rule_to_string (BusMatchRule *rule) if (rule->flags & BUS_MATCH_MESSAGE_TYPE) { - /* FIXME make type readable */ - if (!_dbus_string_append_printf (&str, "type='%d'", rule->message_type)) - goto nomem; + if (rule->message_type == DBUS_MESSAGE_TYPE_INVALID) + { + if (!_dbus_string_append_printf (&str, "type='INVALID'")) + goto nomem; + } + else if (rule->message_type == DBUS_MESSAGE_TYPE_METHOD_CALL) + { + if (!_dbus_string_append_printf (&str, "type='method_call'")) + goto nomem; + } + else if (rule->message_type == DBUS_MESSAGE_TYPE_METHOD_RETURN) + { + if (!_dbus_string_append_printf (&str, "type='method_return'")) + goto nomem; + } + else if (rule->message_type == DBUS_MESSAGE_TYPE_ERROR) + { + if (!_dbus_string_append_printf (&str, "type='error'")) + goto nomem; + } + else if (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL) + { + if (!_dbus_string_append_printf (&str, "type='signal'")) + goto nomem; + } + else + { + if (!_dbus_string_append_printf (&str, "type='%d'", rule->message_type)) + goto nomem; + } } if (rule->flags & BUS_MATCH_INTERFACE) @@ -245,7 +271,6 @@ match_rule_to_string (BusMatchRule *rule) return s; } } -#endif /* DBUS_ENABLE_VERBOSE_MODE */ dbus_bool_t bus_match_rule_set_message_type (BusMatchRule *rule, @@ -1332,6 +1357,58 @@ match_rule_equal (BusMatchRule *a, return TRUE; } +void +bus_match_rule_dump (BusMatchmaker *matchmaker, DBusString *rule_text_out, + DBusConnection *conn_filter) +{ + int i; + _dbus_string_init (rule_text_out); + + for (i = 0 ; i < DBUS_NUM_MESSAGE_TYPES ; i++) + { + DBusHashIter iter; + DBusList *list; + DBusList *link; + + _dbus_hash_iter_init (matchmaker->rules_by_type[i].rules_by_iface, &iter); + while (_dbus_hash_iter_next (&iter)) + { + list = *((DBusList **)_dbus_hash_iter_get_value (&iter)); + link = _dbus_list_get_first_link (&list); + while (link != NULL) + { + DBusList *next = _dbus_list_get_next_link (&list, link); + BusMatchRule *rule = link->data; + + if (conn_filter == NULL || rule->matches_go_to == conn_filter) + { + char *s = match_rule_to_string (rule); + _dbus_string_append (rule_text_out, s); + _dbus_string_append (rule_text_out, "\n"); + } + + link = next; + } + } + list = matchmaker->rules_by_type[i].rules_without_iface; + link = _dbus_list_get_first_link (&list); + while (link != NULL) + { + DBusList *next = _dbus_list_get_next_link (&list, link); + BusMatchRule *rule = link->data; + + if (conn_filter == NULL || rule->matches_go_to == conn_filter) + { + char *s = match_rule_to_string (rule); + _dbus_string_append (rule_text_out, s); + _dbus_string_append (rule_text_out, "\n"); + } + + link = next; + } + } +} + static void bus_matchmaker_remove_rule_link (DBusList **rules, DBusList *link) diff --git a/bus/signals.h b/bus/signals.h index eeb1d2d..7fbc036 100644 --- a/bus/signals.h +++ b/bus/signals.h @@ -65,6 +65,10 @@ BusMatchRule* bus_match_rule_parse (DBusConnection *matches_go_to, const DBusString *rule_text, DBusError *error); +void bus_match_rule_dump (BusMatchmaker *matchmaker, + DBusString *rule_text_out, + DBusConnection *conn_filter); + BusMatchmaker* bus_matchmaker_new (void); BusMatchmaker* bus_matchmaker_ref (BusMatchmaker *matchmaker); void bus_matchmaker_unref (BusMatchmaker *matchmaker); -- 1.6.4.3