driver: Share bus_driver_get_conn_helper with other modules
authorSimon McVittie <smcv@collabora.com>
Fri, 9 Jun 2017 15:24:13 +0000 (16:24 +0100)
committerSimon McVittie <smcv@collabora.com>
Thu, 29 Jun 2017 14:42:20 +0000 (15:42 +0100)
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 <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=101567

bus/driver.c
bus/driver.h
bus/stats.c

index 6534382..c51a4f9 100644 (file)
 #include <dbus/dbus-marshal-validate.h>
 #include <string.h>
 
-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,
index 2964b6d..36a1db9 100644 (file)
 #include <dbus/dbus.h>
 #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 */
index 065a09d..1582255 100644 (file)
@@ -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;
 }