From 8be69c0f73c43712190cdf9acb15166c951af245 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 9 Jun 2017 16:24:13 +0100 Subject: [PATCH 01/49] driver: Share bus_driver_get_conn_helper with other modules Now that we're starting to implement methods in more places, it makes sense to share this code. The Stats interface can already benefit. Signed-off-by: Simon McVittie --- bus/driver.c | 9 +-------- bus/driver.h | 14 ++++++++++++++ bus/stats.c | 40 +++++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/bus/driver.c b/bus/driver.c index d3ad878f..fa1b1b49 100644 --- a/bus/driver.c +++ b/bus/driver.c @@ -43,13 +43,6 @@ #include #include -typedef enum -{ - BUS_DRIVER_FOUND_SELF, - BUS_DRIVER_FOUND_PEER, - BUS_DRIVER_FOUND_ERROR, -} BusDriverFound; - static inline const char * nonnull (const char *maybe_null, const char *if_null) @@ -75,7 +68,7 @@ bus_driver_get_owner_of_name (DBusConnection *connection, return bus_service_get_primary_owners_connection (serv); } -static BusDriverFound +BusDriverFound bus_driver_get_conn_helper (DBusConnection *connection, DBusMessage *message, const char *what_we_want, diff --git a/bus/driver.h b/bus/driver.h index 2964b6d4..36a1db91 100644 --- a/bus/driver.h +++ b/bus/driver.h @@ -27,6 +27,13 @@ #include #include "connection.h" +typedef enum +{ + BUS_DRIVER_FOUND_SELF, + BUS_DRIVER_FOUND_PEER, + BUS_DRIVER_FOUND_ERROR, +} BusDriverFound; + void bus_driver_remove_connection (DBusConnection *connection); dbus_bool_t bus_driver_handle_message (DBusConnection *connection, BusTransaction *transaction, @@ -49,4 +56,11 @@ dbus_bool_t bus_driver_generate_introspect_string (DBusString *xml, dbus_bool_t canonical_path, DBusMessage *message); +BusDriverFound bus_driver_get_conn_helper (DBusConnection *connection, + DBusMessage *message, + const char *what_we_want, + const char **name_p, + DBusConnection **peer_conn_p, + DBusError *error); + #endif /* BUS_DRIVER_H */ diff --git a/bus/stats.c b/bus/stats.c index 065a09d5..15822556 100644 --- a/bus/stats.c +++ b/bus/stats.c @@ -120,37 +120,37 @@ bus_stats_handle_get_connection_stats (DBusConnection *caller_connection, DBusMessage *message, DBusError *error) { - const char *bus_name = NULL; - DBusString bus_name_str; + BusDriverFound found; DBusMessage *reply = NULL; DBusMessageIter iter, arr_iter; static dbus_uint32_t stats_serial = 0; dbus_uint32_t in_messages, in_bytes, in_fds, in_peak_bytes, in_peak_fds; dbus_uint32_t out_messages, out_bytes, out_fds, out_peak_bytes, out_peak_fds; - BusRegistry *registry; - BusService *service; DBusConnection *stats_connection; _DBUS_ASSERT_ERROR_IS_CLEAR (error); - registry = bus_connection_get_registry (caller_connection); - - if (! dbus_message_get_args (message, error, - DBUS_TYPE_STRING, &bus_name, - DBUS_TYPE_INVALID)) - return FALSE; + found = bus_driver_get_conn_helper (caller_connection, message, + "statistics", NULL, &stats_connection, + error); - _dbus_string_init_const (&bus_name_str, bus_name); - service = bus_registry_lookup (registry, &bus_name_str); - - if (service == NULL) + switch (found) { - dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER, - "Bus name '%s' has no owner", bus_name); - return FALSE; + case BUS_DRIVER_FOUND_SELF: + dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, + "GetConnectionStats is not meaningful for the " + "message bus \"%s\" itself", DBUS_SERVICE_DBUS); + goto failed; + + case BUS_DRIVER_FOUND_PEER: + break; + + case BUS_DRIVER_FOUND_ERROR: + /* fall through */ + default: + goto failed; } - stats_connection = bus_service_get_primary_owners_connection (service); _dbus_assert (stats_connection != NULL); reply = _dbus_asv_new_method_return (message, &iter, &arr_iter); @@ -212,10 +212,12 @@ bus_stats_handle_get_connection_stats (DBusConnection *caller_connection, return TRUE; oom: + BUS_SET_OOM (error); + /* fall through */ +failed: if (reply != NULL) dbus_message_unref (reply); - BUS_SET_OOM (error); return FALSE; } -- 2.11.0