[daemon-dev][lib-opt] Fix in freeing memory, cleanup, comments, renames
[platform/upstream/dbus.git] / bus / connection.c
index 3c43200..1f2c955 100644 (file)
@@ -1,9 +1,10 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* connection.c  Client connections
  *
  * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2013  Samsung Electronics
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
+
+#include <config.h>
 #include "connection.h"
 #include "dispatch.h"
-#include "loop.h"
 #include "policy.h"
 #include "services.h"
 #include "utils.h"
+#include "signals.h"
+#include "expirelist.h"
+#include "selinux.h"
 #include <dbus/dbus-list.h>
+#include <dbus/dbus-hash.h>
+#include <dbus/dbus-timeout.h>
+
+/* Trim executed commands to this length; we want to keep logs readable */
+#define MAX_LOG_COMMAND_LEN 50
 
 static void bus_connection_remove_transactions (DBusConnection *connection);
 
+typedef struct
+{
+  BusExpireItem expire_item;
+
+  DBusConnection *will_get_reply;
+  DBusConnection *will_send_reply;
+
+  dbus_uint32_t reply_serial;
+  
+} BusPendingReply;
+
 struct BusConnections
 {
   int refcount;
-  DBusList *list; /**< List of all the connections */
+  DBusList *completed;  /**< List of all completed connections */
+  int n_completed;      /**< Length of completed list */
+  DBusList *incomplete; /**< List of all not-yet-active connections */
+  int n_incomplete;     /**< Length of incomplete list */
   BusContext *context;
+  DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
+  DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
+  int stamp;                   /**< Incrementing number */
+  BusExpireList *pending_replies; /**< List of pending replies */
+
+#ifdef DBUS_ENABLE_STATS
+  int total_match_rules;
+  int peak_match_rules;
+  int peak_match_rules_per_conn;
+
+  int total_bus_names;
+  int peak_bus_names;
+  int peak_bus_names_per_conn;
+#endif
 };
 
-static int connection_data_slot = -1;
-static int connection_data_slot_refcount = 0;
+static dbus_int32_t connection_data_slot = -1;
 
 typedef struct
 {
   BusConnections *connections;
+  DBusList *link_in_connection_list;
   DBusConnection *connection;
   DBusList *services_owned;
+  int n_services_owned;
+  DBusList *match_rules;
+  int n_match_rules;
   char *name;
   DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
   DBusMessage *oom_message;
   DBusPreallocatedSend *oom_preallocated;
-  unsigned long *group_ids;
-  int n_group_ids;
-  BusPolicy *policy;
+  BusClientPolicy *policy;
+
+  char *cached_loginfo_string;
+  BusSELinuxID *selinux_id;
+
+  long connection_tv_sec;  /**< Time when we connected (seconds component) */
+  long connection_tv_usec; /**< Time when we connected (microsec component) */
+  int stamp;               /**< connections->stamp last time we were traversed */
+
+#ifdef DBUS_ENABLE_STATS
+  int peak_match_rules;
+  int peak_bus_names;
+#endif
 } BusConnectionData;
 
+static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
+                                              DBusList      *link,
+                                              void          *data);
+
+static void bus_connection_drop_pending_replies (BusConnections  *connections,
+                                                 DBusConnection  *connection);
+
+static dbus_bool_t expire_incomplete_timeout (void *data);
+
 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
 
-static dbus_bool_t
-connection_data_slot_ref (void)
+static DBusLoop*
+connection_get_loop (DBusConnection *connection)
 {
-  if (connection_data_slot < 0)
-    {
-      connection_data_slot = dbus_connection_allocate_data_slot ();
-      
-      if (connection_data_slot < 0)
-        return FALSE;
+  BusConnectionData *d;
 
-      _dbus_assert (connection_data_slot_refcount == 0);
-    }  
+  d = BUS_CONNECTION_DATA (connection);
+
+  return bus_context_get_loop (d->connections->context);
+}
 
-  connection_data_slot_refcount += 1;
 
-  return TRUE;
+static int
+get_connections_for_uid (BusConnections *connections,
+                         dbus_uid_t      uid)
+{
+  void *val;
+  int current_count;
+
+  /* val is NULL is 0 when it isn't in the hash yet */
+  
+  val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
+                                       uid);
+
+  current_count = _DBUS_POINTER_TO_INT (val);
 
+  return current_count;
 }
 
-static void
-connection_data_slot_unref (void)
+static dbus_bool_t
+adjust_connections_for_uid (BusConnections *connections,
+                            dbus_uid_t      uid,
+                            int             adjustment)
 {
-  _dbus_assert (connection_data_slot_refcount > 0);
+  int current_count;
 
-  connection_data_slot_refcount -= 1;
+  current_count = get_connections_for_uid (connections, uid);
+
+  _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
+                 ": was %d adjustment %d making %d\n",
+                 uid, current_count, adjustment, current_count + adjustment);
+  
+  _dbus_assert (current_count >= 0);
   
-  if (connection_data_slot_refcount == 0)
+  current_count += adjustment;
+
+  _dbus_assert (current_count >= 0);
+
+  if (current_count == 0)
     {
-      dbus_connection_free_data_slot (connection_data_slot);
-      connection_data_slot = -1;
+      _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
+      return TRUE;
+    }
+  else
+    {
+      dbus_bool_t retval;
+      
+      retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
+                                              uid, _DBUS_INT_TO_POINTER (current_count));
+
+      /* only positive adjustment can fail as otherwise
+       * a hash entry should already exist
+       */
+      _dbus_assert (adjustment > 0 ||
+                    (adjustment <= 0 && retval));
+
+      return retval;
     }
 }
 
@@ -94,22 +190,35 @@ bus_connection_disconnected (DBusConnection *connection)
 {
   BusConnectionData *d;
   BusService *service;
-
+  BusMatchmaker *matchmaker;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  dbus_bool_t is_phantom = FALSE;
+#endif
+  
   d = BUS_CONNECTION_DATA (connection);
   _dbus_assert (d != NULL);
 
   _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
                  d->name ? d->name : "(inactive)");
+
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(d->connections->context) && (strcmp(bus_connection_get_name(connection), ":1.1")))
+      is_phantom = TRUE;
+#endif
+
+  /* Delete our match rules */
+  if (d->n_match_rules > 0)
+    {
+      matchmaker = bus_context_get_matchmaker (d->connections->context);
+      bus_matchmaker_disconnected (matchmaker, connection);
+    }
   
-  /* Drop any service ownership. FIXME Unfortunately, this requires
+  /* Drop any service ownership. Unfortunately, this requires
    * memory allocation and there doesn't seem to be a good way to
    * handle it other than sleeping; we can't "fail" the operation of
    * disconnecting a client, and preallocating a broadcast "service is
    * now gone" message for every client-service pair seems kind of
-   * involved. Probably we need to do that though, and also
-   * extend BusTransaction to be able to revert generic
-   * stuff, not just sending a message (so we can e.g. revert
-   * removal of service owners).
+   * involved.
    */
   while ((service = _dbus_list_get_last (&d->services_owned)))
     {
@@ -120,25 +229,27 @@ bus_connection_disconnected (DBusConnection *connection)
       
       dbus_error_init (&error);
         
-      transaction = NULL;
-      while (transaction == NULL)
-        {
-          transaction = bus_transaction_new (d->connections->context);
-          bus_wait_for_memory ();
-        }
+      while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
+        _dbus_wait_for_memory ();
         
       if (!bus_service_remove_owner (service, connection,
                                      transaction, &error))
         {
+          _DBUS_ASSERT_ERROR_IS_SET (&error);
+          
           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
             {
               dbus_error_free (&error);
               bus_transaction_cancel_and_free (transaction);
-              bus_wait_for_memory ();
+              _dbus_wait_for_memory ();
               goto retry;
             }
           else
-            _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
+            {
+              _dbus_verbose ("Failed to remove service owner: %s %s\n",
+                             error.name, error.message);
+              _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
+            }
         }
         
       bus_transaction_execute_and_free (transaction);
@@ -161,97 +272,128 @@ bus_connection_disconnected (DBusConnection *connection)
   
   dbus_connection_set_unix_user_function (connection,
                                           NULL, NULL, NULL);
+  dbus_connection_set_windows_user_function (connection,
+                                             NULL, NULL, NULL);
+  
+  dbus_connection_set_dispatch_status_function (connection,
+                                                NULL, NULL, NULL);
   
   bus_connection_remove_transactions (connection);
 
-  _dbus_list_remove (&d->connections->list, connection);
+  if (d->link_in_connection_list != NULL)
+    {
+      if (d->name != NULL)
+        {
+          unsigned long uid;
+          
+          _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
+          d->link_in_connection_list = NULL;
+          d->connections->n_completed -= 1;
+
+          if (dbus_connection_get_unix_user (connection, &uid))
+            {
+              if (!adjust_connections_for_uid (d->connections,
+                                               uid, -1))
+                _dbus_assert_not_reached ("adjusting downward should never fail");
+            }
+        }
+      else
+        {
+          _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
+          d->link_in_connection_list = NULL;
+          d->connections->n_incomplete -= 1;
+        }
+      
+      _dbus_assert (d->connections->n_incomplete >= 0);
+      _dbus_assert (d->connections->n_completed >= 0);
+    }
 
+  bus_connection_drop_pending_replies (d->connections, connection);
+  
   /* frees "d" as side effect */
   dbus_connection_set_data (connection,
                             connection_data_slot,
                             NULL, NULL);
 
-  dbus_connection_unref (connection);
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(is_phantom)
+      dbus_connection_unref_phantom(connection);
+  else
+#endif
+      dbus_connection_unref (connection);
 }
 
 static dbus_bool_t
-connection_watch_callback (DBusWatch     *watch,
-                           unsigned int   condition,
-                           void          *data)
+add_connection_watch (DBusWatch      *watch,
+                      void           *data)
 {
   DBusConnection *connection = data;
-  dbus_bool_t retval;
-
-  dbus_connection_ref (connection);
-  
-  retval = dbus_connection_handle_watch (connection, watch, condition);
 
-  bus_connection_dispatch_all_messages (connection);
-  
-  dbus_connection_unref (connection);
-
-  return retval;
-}
-
-static dbus_bool_t
-add_connection_watch (DBusWatch      *watch,
-                      DBusConnection *connection)
-{
-  return bus_loop_add_watch (watch, connection_watch_callback, connection,
-                             NULL);
+  return _dbus_loop_add_watch (connection_get_loop (connection), watch);
 }
 
 static void
 remove_connection_watch (DBusWatch      *watch,
-                         DBusConnection *connection)
+                         void           *data)
 {
-  bus_loop_remove_watch (watch, connection_watch_callback, connection);
+  DBusConnection *connection = data;
+  
+  _dbus_loop_remove_watch (connection_get_loop (connection), watch);
 }
 
 static void
-connection_timeout_callback (DBusTimeout   *timeout,
-                             void          *data)
+toggle_connection_watch (DBusWatch      *watch,
+                         void           *data)
 {
   DBusConnection *connection = data;
 
-  dbus_connection_ref (connection);
-
-  /* can return FALSE on OOM but we just let it fire again later */
-  dbus_timeout_handle (timeout);
-
-  bus_connection_dispatch_all_messages (connection);
-  
-  dbus_connection_unref (connection);
+  _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
 }
 
 static dbus_bool_t
 add_connection_timeout (DBusTimeout    *timeout,
-                        DBusConnection *connection)
+                        void           *data)
 {
-  return bus_loop_add_timeout (timeout, connection_timeout_callback, connection, NULL);
+  DBusConnection *connection = data;
+  
+  return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
 }
 
 static void
 remove_connection_timeout (DBusTimeout    *timeout,
-                           DBusConnection *connection)
+                           void           *data)
 {
-  bus_loop_remove_timeout (timeout, connection_timeout_callback, connection);
+  DBusConnection *connection = data;
+  
+  _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
+}
+
+static void
+dispatch_status_function (DBusConnection    *connection,
+                          DBusDispatchStatus new_status,
+                          void              *data)
+{
+  DBusLoop *loop = data;
+  
+  if (new_status != DBUS_DISPATCH_COMPLETE)
+    {
+      while (!_dbus_loop_queue_dispatch (loop, connection))
+        _dbus_wait_for_memory ();
+    }
 }
 
 static dbus_bool_t
-allow_user_function (DBusConnection *connection,
-                     unsigned long   uid,
-                     void           *data)
+allow_unix_user_function (DBusConnection *connection,
+                          unsigned long   uid,
+                          void           *data)
 {
   BusConnectionData *d;
     
   d = BUS_CONNECTION_DATA (connection);
 
   _dbus_assert (d != NULL);
-
-  return TRUE; /* FIXME - this is just until we can parse a config file */
   
-  return bus_context_allow_user (d->connections->context, uid);
+  return bus_context_allow_unix_user (d->connections->context, uid);
 }
 
 static void
@@ -261,6 +403,7 @@ free_connection_data (void *data)
 
   /* services_owned should be NULL since we should be disconnected */
   _dbus_assert (d->services_owned == NULL);
+  _dbus_assert (d->n_services_owned == 0);
   /* similarly */
   _dbus_assert (d->transaction_messages == NULL);
 
@@ -271,9 +414,12 @@ free_connection_data (void *data)
     dbus_message_unref (d->oom_message);
 
   if (d->policy)
-    bus_policy_unref (d->policy);
+    bus_client_policy_unref (d->policy);
+
+  if (d->selinux_id)
+    bus_selinux_id_unref (d->selinux_id);
   
-  dbus_free (d->group_ids);
+  dbus_free (d->cached_loginfo_string);
   
   dbus_free (d->name);
   
@@ -285,27 +431,63 @@ bus_connections_new (BusContext *context)
 {
   BusConnections *connections;
 
-  if (!connection_data_slot_ref ())
-    return NULL;
+  if (!dbus_connection_allocate_data_slot (&connection_data_slot))
+    goto failed_0;
 
   connections = dbus_new0 (BusConnections, 1);
   if (connections == NULL)
-    {
-      connection_data_slot_unref ();
-      return NULL;
-    }
+    goto failed_1;
+
+  connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
+                                                         NULL, NULL);
+  if (connections->completed_by_user == NULL)
+    goto failed_2;
+
+  connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
+                                                   expire_incomplete_timeout,
+                                                   connections, NULL);
+  if (connections->expire_timeout == NULL)
+    goto failed_3;
+
+  _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
+
+  connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
+                                                      bus_context_get_reply_timeout (context),
+                                                      bus_pending_reply_expired,
+                                                      connections);
+  if (connections->pending_replies == NULL)
+    goto failed_4;
+  
+  if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
+                               connections->expire_timeout))
+    goto failed_5;
   
   connections->refcount = 1;
   connections->context = context;
   
   return connections;
+
+ failed_5:
+  bus_expire_list_free (connections->pending_replies);
+ failed_4:
+  _dbus_timeout_unref (connections->expire_timeout);
+ failed_3:
+  _dbus_hash_table_unref (connections->completed_by_user);
+ failed_2:
+  dbus_free (connections);
+ failed_1:
+  dbus_connection_free_data_slot (&connection_data_slot);
+ failed_0:
+  return NULL;
 }
 
-void
+BusConnections *
 bus_connections_ref (BusConnections *connections)
 {
   _dbus_assert (connections->refcount > 0);
   connections->refcount += 1;
+
+  return connections;
 }
 
 void
@@ -315,32 +497,120 @@ bus_connections_unref (BusConnections *connections)
   connections->refcount -= 1;
   if (connections->refcount == 0)
     {
-      while (connections->list != NULL)
+      /* drop all incomplete */
+      while (connections->incomplete != NULL)
+        {
+          DBusConnection *connection;
+
+          connection = connections->incomplete->data;
+
+          dbus_connection_ref (connection);
+          dbus_connection_close (connection);
+          bus_connection_disconnected (connection);
+          dbus_connection_unref (connection);
+        }
+
+      _dbus_assert (connections->n_incomplete == 0);
+      
+      /* drop all real connections */
+      while (connections->completed != NULL)
         {
           DBusConnection *connection;
 
-          connection = connections->list->data;
+          connection = connections->completed->data;
 
           dbus_connection_ref (connection);
-          dbus_connection_disconnect (connection);
+          dbus_connection_close (connection);
           bus_connection_disconnected (connection);
           dbus_connection_unref (connection);
         }
+
+      _dbus_assert (connections->n_completed == 0);
+
+      bus_expire_list_free (connections->pending_replies);
+      
+      _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
+                                 connections->expire_timeout);
       
-      _dbus_list_clear (&connections->list);
+      _dbus_timeout_unref (connections->expire_timeout);
+      
+      _dbus_hash_table_unref (connections->completed_by_user);
       
       dbus_free (connections);
 
-      connection_data_slot_unref ();
+      dbus_connection_free_data_slot (&connection_data_slot);
+    }
+}
+
+/* Used for logging */
+static dbus_bool_t
+cache_peer_loginfo_string (BusConnectionData *d, 
+                           DBusConnection    *connection)
+{
+  DBusString loginfo_buf;
+  unsigned long uid;
+  unsigned long pid;
+  char *windows_sid;
+  dbus_bool_t prev_added;
+
+  if (!_dbus_string_init (&loginfo_buf))
+    return FALSE;
+  
+  prev_added = FALSE;
+  if (dbus_connection_get_unix_user (connection, &uid))
+    {
+      if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
+        goto oom;
+      else
+        prev_added = TRUE;
+    }
+
+  if (dbus_connection_get_unix_process_id (connection, &pid))
+    {
+      if (prev_added)
+        {
+          if (!_dbus_string_append_byte (&loginfo_buf, ' '))
+            goto oom;
+        }
+      if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
+        goto oom;
+      /* Ignore errors here; we may not have permissions to read the
+       * proc file. */
+      _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
+      if (!_dbus_string_append_byte (&loginfo_buf, '"'))
+        goto oom;
+    }
+
+  if (dbus_connection_get_windows_user (connection, &windows_sid))
+    {
+      dbus_bool_t did_append;
+      did_append = _dbus_string_append_printf (&loginfo_buf,
+                                               "sid=\"%s\" ", windows_sid);
+      dbus_free (windows_sid);
+      if (!did_append)
+        goto oom;
     }
+
+  if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
+    goto oom;
+
+  _dbus_string_free (&loginfo_buf); 
+
+  return TRUE;
+oom:
+   _dbus_string_free (&loginfo_buf);
+   return FALSE;
 }
 
 dbus_bool_t
 bus_connections_setup_connection (BusConnections *connections,
                                   DBusConnection *connection)
 {
+
   BusConnectionData *d;
   dbus_bool_t retval;
+  DBusError error;
+
   
   d = dbus_new0 (BusConnectionData, 1);
   
@@ -349,7 +619,10 @@ bus_connections_setup_connection (BusConnections *connections,
 
   d->connections = connections;
   d->connection = connection;
-
+  
+  _dbus_get_monotonic_time (&d->connection_tv_sec,
+                            &d->connection_tv_usec);
+  
   _dbus_assert (connection_data_slot >= 0);
   
   if (!dbus_connection_set_data (connection,
@@ -360,52 +633,110 @@ bus_connections_setup_connection (BusConnections *connections,
       return FALSE;
     }
 
+  dbus_connection_set_route_peer_messages (connection, TRUE);
+  
   retval = FALSE;
 
-  d->n_group_ids = 0;
-  d->group_ids = NULL;
-  
+  dbus_error_init (&error);
+  d->selinux_id = bus_selinux_init_connection_id (connection,
+                                                  &error);
+  if (dbus_error_is_set (&error))
+    {
+      /* This is a bit bogus because we pretend all errors
+       * are OOM; this is done because we know that in bus.c
+       * an OOM error disconnects the connection, which is
+       * the same thing we want on any other error.
+       */
+      dbus_error_free (&error);
+      goto out;
+    }
+
   if (!dbus_connection_set_watch_functions (connection,
-                                            (DBusAddWatchFunction) add_connection_watch,
-                                            (DBusRemoveWatchFunction) remove_connection_watch,
-                                            NULL,
+                                            add_connection_watch,
+                                            remove_connection_watch,
+                                            toggle_connection_watch,
                                             connection,
                                             NULL))
     goto out;
   
   if (!dbus_connection_set_timeout_functions (connection,
-                                              (DBusAddTimeoutFunction) add_connection_timeout,
-                                              (DBusRemoveTimeoutFunction) remove_connection_timeout,
+                                              add_connection_timeout,
+                                              remove_connection_timeout,
                                               NULL,
                                               connection, NULL))
     goto out;
 
-
+  /* For now we don't need to set a Windows user function because
+   * there are no policies in the config file controlling what
+   * Windows users can connect. The default 'same user that owns the
+   * bus can connect' behavior of DBusConnection is fine on Windows.
+   */
   dbus_connection_set_unix_user_function (connection,
-                                          allow_user_function,
+                                          allow_unix_user_function,
                                           NULL, NULL);
+
+  dbus_connection_set_dispatch_status_function (connection,
+                                                dispatch_status_function,
+                                                bus_context_get_loop (connections->context),
+                                                NULL);
+
+  d->link_in_connection_list = _dbus_list_alloc_link (connection);
+  if (d->link_in_connection_list == NULL)
+    goto out;
   
   /* Setup the connection with the dispatcher */
   if (!bus_dispatch_add_connection (connection))
     goto out;
-  
-  if (!_dbus_list_append (&connections->list, connection))
+
+  if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
     {
-      bus_dispatch_remove_connection (connection);
-      goto out;
+      if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
+        {
+          bus_dispatch_remove_connection (connection);
+          goto out;
+        }
     }
+
+  _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
+  connections->n_incomplete += 1;
   
   dbus_connection_ref (connection);
+
+  /* Note that we might disconnect ourselves here, but it only takes
+   * effect on return to the main loop. We call this to free up
+   * expired connections if possible, and to queue the timeout for our
+   * own expiration.
+   */
+  bus_connections_expire_incomplete (connections);
+  
+  /* And we might also disconnect ourselves here, but again it
+   * only takes effect on return to main loop.
+   */
+  if (connections->n_incomplete >
+      bus_context_get_max_incomplete_connections (connections->context))
+    {
+      _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
+      
+      _dbus_assert (connections->incomplete != NULL);
+      /* Disconnect the oldest unauthenticated connection.  FIXME
+       * would it be more secure to drop a *random* connection?  This
+       * algorithm seems to mean that if someone can create new
+       * connections quickly enough, they can keep anyone else from
+       * completing authentication. But random may or may not really
+       * help with that, a more elaborate solution might be required.
+       */
+      dbus_connection_close (connections->incomplete->data);
+    }
+  
   retval = TRUE;
 
  out:
   if (!retval)
     {
-      if (!dbus_connection_set_data (connection,
-                                     connection_data_slot,
-                                     NULL, NULL))
-        _dbus_assert_not_reached ("failed to set connection data to null");
-        
+      if (d->selinux_id)
+        bus_selinux_id_unref (d->selinux_id);
+      d->selinux_id = NULL;
+      
       if (!dbus_connection_set_watch_functions (connection,
                                                 NULL, NULL, NULL,
                                                 connection,
@@ -420,76 +751,172 @@ bus_connections_setup_connection (BusConnections *connections,
 
       dbus_connection_set_unix_user_function (connection,
                                               NULL, NULL, NULL);
+
+      dbus_connection_set_windows_user_function (connection,
+                                                 NULL, NULL, NULL);
+      
+      dbus_connection_set_dispatch_status_function (connection,
+                                                    NULL, NULL, NULL);
+
+      if (d->link_in_connection_list != NULL)
+        {
+          _dbus_assert (d->link_in_connection_list->next == NULL);
+          _dbus_assert (d->link_in_connection_list->prev == NULL);
+          _dbus_list_free_link (d->link_in_connection_list);
+          d->link_in_connection_list = NULL;
+        }
+      
+      if (!dbus_connection_set_data (connection,
+                                     connection_data_slot,
+                                     NULL, NULL))
+        _dbus_assert_not_reached ("failed to set connection data to null");
+
+      /* "d" has now been freed */
     }
   
   return retval;
 }
 
-dbus_bool_t
-bus_connection_get_groups  (DBusConnection       *connection,
-                            const unsigned long **groups,
-                            int                  *n_groups)
-{
-  BusConnectionData *d;
-    
-  d = BUS_CONNECTION_DATA (connection);
-
-  _dbus_assert (d != NULL);
-
-  *groups = NULL;
-  *n_groups = 0;
+void
+bus_connections_expire_incomplete (BusConnections *connections)
+{    
+  int next_interval;
 
-  /* we do a lazy lookup on groups a user is in for two reasons:
-   * 1) we can't do it on connection setup since the user
-   * hasn't authenticated and 2) it might be expensive
-   * and we don't need to do it if there are no group-based
-   * rules in the config file
-   */
+  next_interval = -1;
   
-  if (d->n_group_ids == 0)
+  if (connections->incomplete != NULL)
     {
-      unsigned long uid;
+      long tv_sec, tv_usec;
+      DBusList *link;
+      int auth_timeout;
       
-      if (dbus_connection_get_unix_user (connection, &uid))
+      _dbus_get_monotonic_time (&tv_sec, &tv_usec);
+      auth_timeout = bus_context_get_auth_timeout (connections->context);
+  
+      link = _dbus_list_get_first_link (&connections->incomplete);
+      while (link != NULL)
         {
-          if (!_dbus_get_groups (uid, &d->group_ids, &d->n_group_ids))
+          DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
+          DBusConnection *connection;
+          BusConnectionData *d;
+          double elapsed;
+      
+          connection = link->data;
+      
+          d = BUS_CONNECTION_DATA (connection);
+      
+          _dbus_assert (d != NULL);
+      
+          elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
+                                                d->connection_tv_usec,
+                                                tv_sec, tv_usec);
+
+          if (elapsed >= (double) auth_timeout)
+            {
+              _dbus_verbose ("Timing out authentication for connection %p\n", connection);
+              dbus_connection_close (connection);
+            }
+          else
             {
-              _dbus_verbose ("Did not get any groups for UID %lu\n",
-                             uid);
-              return FALSE;
+              /* We can end the loop, since the connections are in oldest-first order */
+              next_interval = ((double)auth_timeout) - elapsed;
+              _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
+                             connection, next_interval);
+          
+              break;
             }
+      
+          link = next;
         }
     }
 
-  *groups = d->group_ids;
-  *n_groups = d->n_group_ids;
+  bus_expire_timeout_set_interval (connections->expire_timeout,
+                                   next_interval);
+}
+
+static dbus_bool_t
+expire_incomplete_timeout (void *data)
+{
+  BusConnections *connections = data;
+
+  _dbus_verbose ("Running\n");
+  
+  /* note that this may remove the timeout */
+  bus_connections_expire_incomplete (connections);
 
   return TRUE;
 }
 
 dbus_bool_t
-bus_connection_is_in_group (DBusConnection *connection,
-                            unsigned long   gid)
+bus_connection_get_unix_groups  (DBusConnection   *connection,
+                                 unsigned long   **groups,
+                                 int              *n_groups,
+                                 DBusError        *error)
+{
+  unsigned long uid;
+
+  *groups = NULL;
+  *n_groups = 0;
+
+  if (dbus_connection_get_unix_user (connection, &uid))
+    {
+      if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
+        {
+          _dbus_verbose ("Did not get any groups for UID %lu\n",
+                         uid);
+          return FALSE;
+        }
+      else
+        {
+          _dbus_verbose ("Got %d groups for UID %lu\n",
+                         *n_groups, uid);
+          return TRUE;
+        }
+    }
+  else
+    return TRUE; /* successfully got 0 groups */
+}
+
+dbus_bool_t
+bus_connection_is_in_unix_group (DBusConnection *connection,
+                                 unsigned long   gid)
 {
   int i;
-  const unsigned long *group_ids;
+  unsigned long *group_ids;
   int n_group_ids;
 
-  if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
+  if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
+                                       NULL))
     return FALSE;
 
   i = 0;
   while (i < n_group_ids)
     {
       if (group_ids[i] == gid)
-        return TRUE;
+        {
+          dbus_free (group_ids);
+          return TRUE;
+        }
       ++i;
     }
 
+  dbus_free (group_ids);
   return FALSE;
 }
 
-BusPolicy*
+const char *
+bus_connection_get_loginfo (DBusConnection        *connection)
+{
+  BusConnectionData *d;
+    
+  d = BUS_CONNECTION_DATA (connection);
+
+  if (!bus_connection_is_active (connection))
+    return "inactive";
+  return d->cached_loginfo_string;  
+}
+
+BusClientPolicy*
 bus_connection_get_policy (DBusConnection *connection)
 {
   BusConnectionData *d;
@@ -497,32 +924,72 @@ bus_connection_get_policy (DBusConnection *connection)
   d = BUS_CONNECTION_DATA (connection);
 
   _dbus_assert (d != NULL);
-
-  if (!dbus_connection_get_is_authenticated (connection))
-    {
-      _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
-      return NULL;
-    }
+  _dbus_assert (d->policy != NULL);
   
-  /* We do lazy creation of the policy because
-   * it can only be done post-authentication.
-   */
-  if (d->policy == NULL)
-    {
-      d->policy =
-        bus_context_create_connection_policy (d->connections->context,
-                                              connection);
-
-      /* we may have a NULL policy on OOM or error getting list of
-       * groups for a user. In the latter case we don't handle it so
-       * well currently, just keep pretending we're out of memory,
-       * which is kind of bizarre.
-       */
-    }
-
   return d->policy;
 }
 
+static dbus_bool_t
+foreach_active (BusConnections               *connections,
+                BusConnectionForeachFunction  function,
+                void                         *data)
+{
+  DBusList *link;
+  
+  link = _dbus_list_get_first_link (&connections->completed);
+  while (link != NULL)
+    {
+      DBusConnection *connection = link->data;
+      DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
+
+      if (!(* function) (connection, data))
+        return FALSE;
+      
+      link = next;
+    }
+
+  return TRUE;
+}
+
+static dbus_bool_t
+foreach_inactive (BusConnections               *connections,
+                  BusConnectionForeachFunction  function,
+                  void                         *data)
+{
+  DBusList *link;
+  
+  link = _dbus_list_get_first_link (&connections->incomplete);
+  while (link != NULL)
+    {
+      DBusConnection *connection = link->data;
+      DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
+
+      if (!(* function) (connection, data))
+        return FALSE;
+      
+      link = next;
+    }
+
+  return TRUE;
+}
+
+/**
+ * Calls function on each active connection; if the function returns
+ * #FALSE, stops iterating. Active connections are authenticated
+ * and have sent a Hello message.
+ *
+ * @param connections the connections object
+ * @param function the function
+ * @param data data to pass to it as a second arg
+ */
+void
+bus_connections_foreach_active (BusConnections               *connections,
+                                BusConnectionForeachFunction  function,
+                                void                         *data)
+{
+  foreach_active (connections, function, data);
+}
+
 /**
  * Calls function on each connection; if the function returns
  * #FALSE, stops iterating.
@@ -534,22 +1001,35 @@ bus_connection_get_policy (DBusConnection *connection)
 void
 bus_connections_foreach (BusConnections               *connections,
                          BusConnectionForeachFunction  function,
-                       void                          *data)
+                         void                         *data)
 {
-  DBusList *link;
-  
-  link = _dbus_list_get_first_link (&connections->list);
-  while (link != NULL)
-    {
-      DBusConnection *connection = link->data;
-      DBusList *next = _dbus_list_get_next_link (&connections->list, link);
+  if (!foreach_active (connections, function, data))
+    return;
 
-      if (!(* function) (connection, data))
-        break;
-      
-      link = next;
-    }
+  foreach_inactive (connections, function, data);
+}
+
+#ifdef ENABLE_KDBUS_TRANSPORT
+DBusConnection*
+bus_connections_find_conn_by_name(BusConnections *connections, const char* name)
+{
+    DBusList *link;
+
+    link = _dbus_list_get_first_link (&connections->completed);
+    while (link != NULL)
+      {
+        DBusConnection *connection = link->data;
+        DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
+
+        if (!strcmp(bus_connection_get_name(connection), name))
+          return connection;
+
+        link = next;
+      }
+
+    return NULL;
 }
+#endif
 
 BusContext*
 bus_connections_get_context (BusConnections *connections)
@@ -557,6 +1037,40 @@ bus_connections_get_context (BusConnections *connections)
   return connections->context;
 }
 
+/*
+ * This is used to avoid covering the same connection twice when
+ * traversing connections. Note that it assumes we will
+ * bus_connection_mark_stamp() each connection at least once per
+ * INT_MAX increments of the global stamp, or wraparound would break
+ * things.
+ */
+void
+bus_connections_increment_stamp (BusConnections *connections)
+{
+  connections->stamp += 1;
+}
+
+/* Mark connection with current stamp, return TRUE if it
+ * didn't already have that stamp
+ */
+dbus_bool_t
+bus_connection_mark_stamp (DBusConnection *connection)
+{
+  BusConnectionData *d;
+  
+  d = BUS_CONNECTION_DATA (connection);
+  
+  _dbus_assert (d != NULL);
+
+  if (d->stamp == d->connections->stamp)
+    return FALSE;
+  else
+    {
+      d->stamp = d->connections->stamp;
+      return TRUE;
+    }
+}
+
 BusContext*
 bus_connection_get_context (DBusConnection *connection)
 {
@@ -590,174 +1104,852 @@ bus_connection_get_registry (DBusConnection *connection)
 
   _dbus_assert (d != NULL);
 
-  return bus_context_get_registry (d->connections->context);
+  return bus_context_get_registry (d->connections->context);
+}
+
+BusActivation*
+bus_connection_get_activation (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+
+  _dbus_assert (d != NULL);
+
+  return bus_context_get_activation (d->connections->context);
+}
+
+BusMatchmaker*
+bus_connection_get_matchmaker (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+
+  _dbus_assert (d != NULL);
+
+  return bus_context_get_matchmaker (d->connections->context);
+}
+
+BusSELinuxID*
+bus_connection_get_selinux_id (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+
+  _dbus_assert (d != NULL);
+
+  return d->selinux_id;
+}
+
+/**
+ * Checks whether the connection is registered with the message bus.
+ *
+ * @param connection the connection
+ * @returns #TRUE if we're an active message bus participant
+ */
+dbus_bool_t
+bus_connection_is_active (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  
+  return d != NULL && d->name != NULL;
+}
+
+dbus_bool_t
+bus_connection_preallocate_oom_error (DBusConnection *connection)
+{
+  DBusMessage *message;
+  DBusPreallocatedSend *preallocated;
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);  
+
+  _dbus_assert (d != NULL);
+
+  if (d->oom_preallocated != NULL)
+    return TRUE;
+  
+  preallocated = dbus_connection_preallocate_send (connection);
+  if (preallocated == NULL)
+    return FALSE;
+
+  message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
+
+  if (message == NULL)
+    {
+      dbus_connection_free_preallocated_send (connection, preallocated);
+      return FALSE;
+    }
+
+  /* d->name may be NULL, but that is OK */
+  if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
+      !dbus_message_set_destination (message, d->name) ||
+      !dbus_message_set_sender (message,
+                                DBUS_SERVICE_DBUS))
+    {
+      dbus_connection_free_preallocated_send (connection, preallocated);
+      dbus_message_unref (message);
+      return FALSE;
+    }
+  
+  /* set reply serial to placeholder value just so space is already allocated
+   * for it.
+   */
+  if (!dbus_message_set_reply_serial (message, 14))
+    {
+      dbus_connection_free_preallocated_send (connection, preallocated);
+      dbus_message_unref (message);
+      return FALSE;
+    }
+
+  d->oom_message = message;
+  d->oom_preallocated = preallocated;
+  
+  return TRUE;
+}
+
+void
+bus_connection_send_oom_error (DBusConnection *connection,
+                               DBusMessage    *in_reply_to)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);  
+
+  _dbus_assert (d != NULL);  
+  _dbus_assert (d->oom_message != NULL);
+
+  /* should always succeed since we set it to a placeholder earlier */
+  if (!dbus_message_set_reply_serial (d->oom_message,
+                                      dbus_message_get_serial (in_reply_to)))
+    _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
+
+  _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
+  
+  dbus_connection_send_preallocated (connection, d->oom_preallocated,
+                                     d->oom_message, NULL);
+
+  dbus_message_unref (d->oom_message);
+  d->oom_message = NULL;
+  d->oom_preallocated = NULL;
+}
+
+#ifdef DBUS_ENABLE_STATS
+static void
+update_peak (int *peak,
+             int n)
+{
+  if (*peak < n)
+    *peak = n;
+}
+#endif
+
+void
+bus_connection_add_match_rule_link (DBusConnection *connection,
+                                    DBusList       *link)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+
+  _dbus_list_append_link (&d->match_rules, link);
+
+  d->n_match_rules += 1;
+
+#ifdef DBUS_ENABLE_STATS
+  update_peak (&d->peak_match_rules, d->n_match_rules);
+  update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
+
+  d->connections->total_match_rules += 1;
+  update_peak (&d->connections->peak_match_rules,
+               d->connections->total_match_rules);
+#endif
+}
+
+dbus_bool_t
+bus_connection_add_match_rule (DBusConnection *connection,
+                               BusMatchRule   *rule)
+{
+    DBusList *link;
+
+  link = _dbus_list_alloc_link (rule);
+
+  if (link == NULL)
+    return FALSE;
+
+  bus_connection_add_match_rule_link (connection, link);
+
+  return TRUE;
+}
+
+void
+bus_connection_remove_match_rule (DBusConnection *connection,
+                                  BusMatchRule   *rule)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+
+  _dbus_list_remove_last (&d->match_rules, rule);
+
+  d->n_match_rules -= 1;
+  _dbus_assert (d->n_match_rules >= 0);
+
+#ifdef DBUS_ENABLE_STATS
+  d->connections->total_match_rules -= 1;
+#endif
+}
+
+int
+bus_connection_get_n_match_rules (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+  
+  return d->n_match_rules;
+}
+
+void
+bus_connection_add_owned_service_link (DBusConnection *connection,
+                                       DBusList       *link)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+
+  _dbus_list_append_link (&d->services_owned, link);
+
+  d->n_services_owned += 1;
+
+#ifdef DBUS_ENABLE_STATS
+  update_peak (&d->peak_bus_names, d->n_services_owned);
+  update_peak (&d->connections->peak_bus_names_per_conn,
+               d->n_services_owned);
+
+  d->connections->total_bus_names += 1;
+  update_peak (&d->connections->peak_bus_names,
+               d->connections->total_bus_names);
+#endif
+}
+
+dbus_bool_t
+bus_connection_add_owned_service (DBusConnection *connection,
+                                  BusService     *service)
+{
+  DBusList *link;
+
+  link = _dbus_list_alloc_link (service);
+
+  if (link == NULL)
+    return FALSE;
+
+  bus_connection_add_owned_service_link (connection, link);
+
+  return TRUE;
+}
+
+void
+bus_connection_remove_owned_service (DBusConnection *connection,
+                                     BusService     *service)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+
+  _dbus_list_remove_last (&d->services_owned, service);
+
+  d->n_services_owned -= 1;
+  _dbus_assert (d->n_services_owned >= 0);
+
+#ifdef DBUS_ENABLE_STATS
+  d->connections->total_bus_names -= 1;
+#endif
+}
+
+int
+bus_connection_get_n_services_owned (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+  
+  return d->n_services_owned;
+}
+
+#ifdef ENABLE_KDBUS_TRANSPORT
+DBusList**
+bus_connection_get_services_owned (DBusConnection *connection)
+{
+    BusConnectionData *d;
+    d = BUS_CONNECTION_DATA (connection);
+    _dbus_assert (d != NULL);
+
+    return &d->services_owned;
+}
+#endif
+
+dbus_bool_t
+bus_connection_complete (DBusConnection   *connection,
+                        const DBusString *name,
+                         DBusError        *error)
+{
+  BusConnectionData *d;
+  unsigned long uid;
+  
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+  _dbus_assert (d->name == NULL);
+  _dbus_assert (d->policy == NULL);
+
+  _dbus_assert (!bus_connection_is_active (connection));
+  
+  if (!_dbus_string_copy_data (name, &d->name))
+    {
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
+
+  _dbus_assert (d->name != NULL);
+  
+  _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
+
+  d->policy = bus_context_create_client_policy (d->connections->context,
+                                                connection,
+                                                error);
+
+  /* we may have a NULL policy on OOM or error getting list of
+   * groups for a user. In the latter case we don't handle it so
+   * well currently, as it will just keep failing over and over.
+   */
+
+  if (d->policy == NULL)
+    {
+      _dbus_verbose ("Failed to create security policy for connection %p\n",
+                     connection);
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      dbus_free (d->name);
+      d->name = NULL;
+      return FALSE;
+    }
+  
+  if (dbus_connection_get_unix_user (connection, &uid))
+    {
+      if (!adjust_connections_for_uid (d->connections,
+                                       uid, 1))
+        goto fail;
+    }
+
+  /* Create and cache a string which holds information about the 
+   * peer process; used for logging purposes.
+   */
+  if (!cache_peer_loginfo_string (d, connection))
+    goto fail;
+
+  /* Now the connection is active, move it between lists */
+  _dbus_list_unlink (&d->connections->incomplete,
+                     d->link_in_connection_list);
+  d->connections->n_incomplete -= 1;
+  _dbus_list_append_link (&d->connections->completed,
+                          d->link_in_connection_list);
+  d->connections->n_completed += 1;
+
+  _dbus_assert (d->connections->n_incomplete >= 0);
+  _dbus_assert (d->connections->n_completed > 0);
+
+  /* See if we can remove the timeout */
+  bus_connections_expire_incomplete (d->connections);
+
+  _dbus_assert (bus_connection_is_active (connection));
+  
+  return TRUE;
+fail:
+  BUS_SET_OOM (error);
+  dbus_free (d->name);
+  d->name = NULL;
+  if (d->policy)
+    bus_client_policy_unref (d->policy);
+  d->policy = NULL;
+  return FALSE;
+}
+
+const char *
+bus_connection_get_name (DBusConnection *connection)
+{
+  BusConnectionData *d;
+  
+  d = BUS_CONNECTION_DATA (connection);
+  _dbus_assert (d != NULL);
+  
+  return d->name;
+}
+
+/**
+ * Check whether completing the passed-in connection would
+ * exceed limits, and if so set error and return #FALSE
+ */
+dbus_bool_t
+bus_connections_check_limits (BusConnections  *connections,
+                              DBusConnection  *requesting_completion,
+                              DBusError       *error)
+{
+  unsigned long uid;
+
+  if (connections->n_completed >=
+      bus_context_get_max_completed_connections (connections->context))
+    {
+      dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
+                      "The maximum number of active connections has been reached");
+      return FALSE;
+    }
+  
+  if (dbus_connection_get_unix_user (requesting_completion, &uid))
+    {
+      if (get_connections_for_uid (connections, uid) >=
+          bus_context_get_max_connections_per_user (connections->context))
+        {
+          dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
+                          "The maximum number of active connections for UID %lu has been reached",
+                          uid);
+          return FALSE;
+        }
+    }
+  
+  return TRUE;
+}
+
+static void
+bus_pending_reply_free (BusPendingReply *pending)
+{
+  _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
+                 pending,
+                 pending->will_send_reply,
+                 pending->will_get_reply,
+                 pending->reply_serial);
+
+  dbus_free (pending);
+}
+
+static dbus_bool_t
+bus_pending_reply_send_no_reply (BusConnections  *connections,
+                                 BusTransaction  *transaction,
+                                 BusPendingReply *pending)
+{
+  DBusMessage *message;
+  DBusMessageIter iter;
+  dbus_bool_t retval;
+  const char *errmsg;
+
+  retval = FALSE;
+  
+  message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
+  if (message == NULL)
+    return FALSE;
+  
+  dbus_message_set_no_reply (message, TRUE);
+  
+  if (!dbus_message_set_reply_serial (message,
+                                      pending->reply_serial))
+    goto out;
+
+  if (!dbus_message_set_error_name (message,
+                                    DBUS_ERROR_NO_REPLY))
+    goto out;
+
+  errmsg = "Message did not receive a reply (timeout by message bus)";
+  dbus_message_iter_init_append (message, &iter);
+  if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
+    goto out;
+    
+  if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
+                                         message))
+    goto out;
+
+  retval = TRUE;
+
+ out:
+  dbus_message_unref (message);
+  return retval;
+}
+
+static dbus_bool_t
+bus_pending_reply_expired (BusExpireList *list,
+                           DBusList      *link,
+                           void          *data)
+{
+  BusPendingReply *pending = link->data;
+  BusConnections *connections = data;
+  BusTransaction *transaction;
+  
+  /* No reply is forthcoming. So nuke it if we can. If not,
+   * leave it in the list to try expiring again later when we
+   * get more memory.
+   */
+
+  _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
+                 pending,
+                 pending->will_send_reply,
+                 pending->will_get_reply,
+                 pending->reply_serial);
+  
+  transaction = bus_transaction_new (connections->context);
+  if (transaction == NULL)
+    return FALSE;
+  
+  if (!bus_pending_reply_send_no_reply (connections,
+                                        transaction,
+                                        pending))
+    {
+      bus_transaction_cancel_and_free (transaction);
+      return FALSE;
+    }
+
+  bus_expire_list_remove_link (connections->pending_replies, link);
+
+  bus_pending_reply_free (pending);
+  bus_transaction_execute_and_free (transaction);
+
+  return TRUE;
+}
+
+static void
+bus_connection_drop_pending_replies (BusConnections  *connections,
+                                     DBusConnection  *connection)
+{
+  /* The DBusConnection is almost 100% finalized here, so you can't
+   * do anything with it except check for pointer equality
+   */
+  DBusList *link;
+
+  _dbus_verbose ("Dropping pending replies that involve connection %p\n",
+                 connection);
+  
+  link = bus_expire_list_get_first_link (connections->pending_replies);
+  while (link != NULL)
+    {
+      DBusList *next;
+      BusPendingReply *pending;
+
+      next = bus_expire_list_get_next_link (connections->pending_replies,
+                                            link);
+      pending = link->data;
+
+      if (pending->will_get_reply == connection)
+        {
+          /* We don't need to track this pending reply anymore */
+
+          _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
+                         pending,
+                         pending->will_send_reply,
+                         pending->will_get_reply,
+                         pending->reply_serial);
+          
+          bus_expire_list_remove_link (connections->pending_replies,
+                                       link);
+          bus_pending_reply_free (pending);
+        }
+      else if (pending->will_send_reply == connection)
+        {
+          /* The reply isn't going to be sent, so set things
+           * up so it will be expired right away
+           */
+          _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
+                         pending,
+                         pending->will_send_reply,
+                         pending->will_get_reply,
+                         pending->reply_serial);
+          
+          pending->will_send_reply = NULL;
+          pending->expire_item.added_tv_sec = 0;
+          pending->expire_item.added_tv_usec = 0;
+
+          bus_expire_list_recheck_immediately (connections->pending_replies);
+        }
+      
+      link = next;
+    }
 }
 
-BusActivation*
-bus_connection_get_activation (DBusConnection *connection)
+
+typedef struct
 {
-  BusConnectionData *d;
+  BusPendingReply *pending;
+  BusConnections  *connections;
+} CancelPendingReplyData;
 
-  d = BUS_CONNECTION_DATA (connection);
+static void
+cancel_pending_reply (void *data)
+{
+  CancelPendingReplyData *d = data;
 
-  _dbus_assert (d != NULL);
+  _dbus_verbose ("d = %p\n", d);
+  
+  if (!bus_expire_list_remove (d->connections->pending_replies,
+                               &d->pending->expire_item))
+    _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
 
-  return bus_context_get_activation (d->connections->context);
+  bus_pending_reply_free (d->pending); /* since it's been cancelled */
 }
 
-/**
- * Checks whether the connection is registered with the message bus.
- *
- * @param connection the connection
- * @returns #TRUE if we're an active message bus participant
- */
-dbus_bool_t
-bus_connection_is_active (DBusConnection *connection)
+static void
+cancel_pending_reply_data_free (void *data)
 {
-  BusConnectionData *d;
+  CancelPendingReplyData *d = data;
 
-  d = BUS_CONNECTION_DATA (connection);
+  _dbus_verbose ("d = %p\n", d);
   
-  return d != NULL && d->name != NULL;
+  /* d->pending should be either freed or still
+   * in the list of pending replies (owned by someone
+   * else)
+   */
+  
+  dbus_free (d);
 }
 
+/*
+ * Record that a reply is allowed; return TRUE on success.
+ */
 dbus_bool_t
-bus_connection_preallocate_oom_error (DBusConnection *connection)
+bus_connections_expect_reply (BusConnections  *connections,
+                              BusTransaction  *transaction,
+                              DBusConnection  *will_get_reply,
+                              DBusConnection  *will_send_reply,
+                              DBusMessage     *reply_to_this,
+                              DBusError       *error)
 {
-  DBusMessage *message;
-  DBusPreallocatedSend *preallocated;
-  BusConnectionData *d;
+  BusPendingReply *pending;
+  dbus_uint32_t reply_serial;
+  DBusList *link;
+  CancelPendingReplyData *cprd;
+  int count;
 
-  d = BUS_CONNECTION_DATA (connection);  
+  _dbus_assert (will_get_reply != NULL);
+  _dbus_assert (will_send_reply != NULL);
+  _dbus_assert (reply_to_this != NULL);
+  
+  if (dbus_message_get_no_reply (reply_to_this))
+    return TRUE; /* we won't allow a reply, since client doesn't care for one. */
+  
+  reply_serial = dbus_message_get_serial (reply_to_this);
 
-  _dbus_assert (d != NULL);
+  link = bus_expire_list_get_first_link (connections->pending_replies);
+  count = 0;
+  while (link != NULL)
+    {
+      pending = link->data;
 
-  if (d->oom_preallocated != NULL)
-    return TRUE;
+      if (pending->reply_serial == reply_serial &&
+          pending->will_get_reply == will_get_reply &&
+          pending->will_send_reply == will_send_reply)
+        {
+          dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
+                          "Message has the same reply serial as a currently-outstanding existing method call");
+          return FALSE;
+        }
+      
+      link = bus_expire_list_get_next_link (connections->pending_replies,
+                                            link);
+      if (pending->will_get_reply == will_get_reply)
+        ++count;
+    }
   
-  preallocated = dbus_connection_preallocate_send (connection);
-  if (preallocated == NULL)
-    return FALSE;
+  if (count >=
+      bus_context_get_max_replies_per_connection (connections->context))
+    {
+      dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
+                     "The maximum number of pending replies per connection has been reached");
+      return FALSE;
+    }
 
-  /* d->name may be NULL, but that should be OK */
-  message = dbus_message_new (d->name,
-                              DBUS_ERROR_NO_MEMORY);
-  if (message == NULL)
+  pending = dbus_new0 (BusPendingReply, 1);
+  if (pending == NULL)
     {
-      dbus_connection_free_preallocated_send (connection, preallocated);
+      BUS_SET_OOM (error);
       return FALSE;
     }
 
-  dbus_message_set_is_error (message, TRUE);
+#ifdef DBUS_ENABLE_VERBOSE_MODE
+  /* so we can see a not-yet-added pending reply */
+  pending->expire_item.added_tv_sec = 1;
+  pending->expire_item.added_tv_usec = 1;
+#endif
 
-  if (!dbus_message_set_sender (message,
-                                DBUS_SERVICE_DBUS))
+  pending->will_get_reply = will_get_reply;
+  pending->will_send_reply = will_send_reply;
+  pending->reply_serial = reply_serial;
+  
+  cprd = dbus_new0 (CancelPendingReplyData, 1);
+  if (cprd == NULL)
     {
-      dbus_connection_free_preallocated_send (connection, preallocated);
-      dbus_message_unref (message);
+      BUS_SET_OOM (error);
+      bus_pending_reply_free (pending);
       return FALSE;
     }
   
-  /* set reply serial to placeholder value just so space is already allocated
-   * for it.
-   */
-  if (!dbus_message_set_reply_serial (message, 14))
+  if (!bus_expire_list_add (connections->pending_replies,
+                            &pending->expire_item))
     {
-      dbus_connection_free_preallocated_send (connection, preallocated);
-      dbus_message_unref (message);
+      BUS_SET_OOM (error);
+      dbus_free (cprd);
+      bus_pending_reply_free (pending);
       return FALSE;
     }
 
-  d->oom_message = message;
-  d->oom_preallocated = preallocated;
+  if (!bus_transaction_add_cancel_hook (transaction,
+                                        cancel_pending_reply,
+                                        cprd,
+                                        cancel_pending_reply_data_free))
+    {
+      BUS_SET_OOM (error);
+      bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
+      dbus_free (cprd);
+      bus_pending_reply_free (pending);
+      return FALSE;
+    }
+                                        
+  cprd->pending = pending;
+  cprd->connections = connections;
+  
+  _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
+                            &pending->expire_item.added_tv_usec);
+
+  _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
+                 pending,
+                 pending->will_send_reply,
+                 pending->will_get_reply,
+                 pending->reply_serial);
   
   return TRUE;
 }
 
-void
-bus_connection_send_oom_error (DBusConnection *connection,
-                               DBusMessage    *in_reply_to)
+typedef struct
 {
-  BusConnectionData *d;
+  DBusList        *link;
+  BusConnections  *connections;
+} CheckPendingReplyData;
 
-  d = BUS_CONNECTION_DATA (connection);  
+static void
+cancel_check_pending_reply (void *data)
+{
+  CheckPendingReplyData *d = data;
 
-  _dbus_assert (d != NULL);  
-  _dbus_assert (d->oom_message != NULL);
+  _dbus_verbose ("d = %p\n",d);
 
-  /* should always succeed since we set it to a placeholder earlier */
-  if (!dbus_message_set_reply_serial (d->oom_message,
-                                      dbus_message_get_serial (in_reply_to)))
-    _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
+  bus_expire_list_add_link (d->connections->pending_replies,
+                            d->link);
+  d->link = NULL;
+}
 
-  _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
-  
-  dbus_connection_send_preallocated (connection, d->oom_preallocated,
-                                     d->oom_message, NULL);
+static void
+check_pending_reply_data_free (void *data)
+{
+  CheckPendingReplyData *d = data;
 
-  dbus_message_unref (d->oom_message);
-  d->oom_message = NULL;
-  d->oom_preallocated = NULL;
+  _dbus_verbose ("d = %p\n",d);
+  
+  if (d->link != NULL)
+    {
+      BusPendingReply *pending = d->link->data;
+      
+      _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
+                                                    &pending->expire_item));
+      
+      bus_pending_reply_free (pending);
+      _dbus_list_free_link (d->link);
+    }
+  
+  dbus_free (d);
 }
 
+/*
+ * Check whether a reply is allowed, remove BusPendingReply
+ * if so, return TRUE if so.
+ */
 dbus_bool_t
-bus_connection_add_owned_service (DBusConnection *connection,
-                                  BusService     *service)
+bus_connections_check_reply (BusConnections *connections,
+                             BusTransaction *transaction,
+                             DBusConnection *sending_reply,
+                             DBusConnection *receiving_reply,
+                             DBusMessage    *reply,
+                             DBusError      *error)
 {
-  BusConnectionData *d;
-
-  d = BUS_CONNECTION_DATA (connection);
-  _dbus_assert (d != NULL);
+  CheckPendingReplyData *cprd;
+  DBusList *link;
+  dbus_uint32_t reply_serial;
+  
+  _dbus_assert (sending_reply != NULL);
+  _dbus_assert (receiving_reply != NULL);
 
-  if (!_dbus_list_append (&d->services_owned,
-                          service))
-    return FALSE;
+  reply_serial = dbus_message_get_reply_serial (reply);
 
-  return TRUE;
-}
+  link = bus_expire_list_get_first_link (connections->pending_replies);
+  while (link != NULL)
+    {
+      BusPendingReply *pending = link->data;
 
-void
-bus_connection_remove_owned_service (DBusConnection *connection,
-                                     BusService     *service)
-{
-  BusConnectionData *d;
+      if (pending->reply_serial == reply_serial &&
+          pending->will_get_reply == receiving_reply &&
+          pending->will_send_reply == sending_reply)
+        {
+          _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
+          break;
+        }
+      
+      link = bus_expire_list_get_next_link (connections->pending_replies,
+                                            link);
+    }
 
-  d = BUS_CONNECTION_DATA (connection);
-  _dbus_assert (d != NULL);
+  if (link == NULL)
+    {
+      _dbus_verbose ("No pending reply expected\n");
 
-  _dbus_list_remove_last (&d->services_owned, service);
-}
+      return FALSE;
+    }
 
-dbus_bool_t
-bus_connection_set_name (DBusConnection   *connection,
-                        const DBusString *name)
-{
-  BusConnectionData *d;
+  cprd = dbus_new0 (CheckPendingReplyData, 1);
+  if (cprd == NULL)
+    {
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
   
-  d = BUS_CONNECTION_DATA (connection);
-  _dbus_assert (d != NULL);
-  _dbus_assert (d->name == NULL);
-
-  if (!_dbus_string_copy_data (name, &d->name))
-    return FALSE;
+  if (!bus_transaction_add_cancel_hook (transaction,
+                                        cancel_check_pending_reply,
+                                        cprd,
+                                        check_pending_reply_data_free))
+    {
+      BUS_SET_OOM (error);
+      dbus_free (cprd);
+      return FALSE;
+    }
 
-  _dbus_assert (d->name != NULL);
+  cprd->link = link;
+  cprd->connections = connections;
   
-  _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
+  bus_expire_list_unlink (connections->pending_replies,
+                          link);
   
+  _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
+
   return TRUE;
 }
 
-const char *
-bus_connection_get_name (DBusConnection *connection)
-{
-  BusConnectionData *d;
-  
-  d = BUS_CONNECTION_DATA (connection);
-  _dbus_assert (d != NULL);
-  
-  return d->name;
-}
+/*
+ * Transactions
+ *
+ * Note that this is fairly fragile; in particular, don't try to use
+ * one transaction across any main loop iterations.
+ */
 
 typedef struct
 {
@@ -766,10 +1958,18 @@ typedef struct
   DBusPreallocatedSend *preallocated;
 } MessageToSend;
 
+typedef struct
+{
+  BusTransactionCancelFunction cancel_function;
+  DBusFreeFunction free_data_function;
+  void *data;
+} CancelHook;
+
 struct BusTransaction
 {
   DBusList *connections;
   BusContext *context;
+  DBusList *cancel_hooks;
 };
 
 static void
@@ -785,6 +1985,39 @@ message_to_send_free (DBusConnection *connection,
   dbus_free (to_send);
 }
 
+static void
+cancel_hook_cancel (void *element,
+                    void *data)
+{
+  CancelHook *ch = element;
+
+  _dbus_verbose ("Running transaction cancel hook\n");
+  
+  if (ch->cancel_function)
+    (* ch->cancel_function) (ch->data);  
+}
+
+static void
+cancel_hook_free (void *element,
+                  void *data)
+{
+  CancelHook *ch = element;
+
+  if (ch->free_data_function)
+    (* ch->free_data_function) (ch->data);
+
+  dbus_free (ch);
+}
+
+static void
+free_cancel_hooks (BusTransaction *transaction)
+{
+  _dbus_list_foreach (&transaction->cancel_hooks,
+                      cancel_hook_free, NULL);
+  
+  _dbus_list_clear (&transaction->cancel_hooks);
+}
+
 BusTransaction*
 bus_transaction_new (BusContext *context)
 {
@@ -812,16 +2045,68 @@ bus_transaction_get_connections (BusTransaction  *transaction)
 }
 
 dbus_bool_t
-bus_transaction_send_message (BusTransaction *transaction,
-                              DBusConnection *connection,
-                              DBusMessage    *message)
+bus_transaction_send_from_driver (BusTransaction *transaction,
+                                  DBusConnection *connection,
+                                  DBusMessage    *message)
+{
+  /* We have to set the sender to the driver, and have
+   * to check security policy since it was not done in
+   * dispatch.c
+   */
+  _dbus_verbose ("Sending %s %s %s from driver\n",
+                 dbus_message_get_interface (message) ?
+                 dbus_message_get_interface (message) : "(no interface)",
+                 dbus_message_get_member (message) ?
+                 dbus_message_get_member (message) : "(no member)",
+                 dbus_message_get_error_name (message) ?
+                 dbus_message_get_error_name (message) : "(no error name)");
+                 
+  if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
+    return FALSE;
+
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* we can't set destination on the basis of connection when on kdbus*/
+#endif
+    if (bus_connection_is_active (connection))
+    {
+      if (!dbus_message_set_destination (message,
+                                         bus_connection_get_name (connection)))
+        return FALSE;
+    }
+  
+  /* bus driver never wants a reply */
+  dbus_message_set_no_reply (message, TRUE);
+  
+  /* If security policy doesn't allow the message, we silently
+   * eat it; the driver doesn't care about getting a reply.
+   */
+  if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
+                                          transaction,
+                                          NULL, connection, connection, message, NULL))
+    return TRUE;
+
+  return bus_transaction_send (transaction, connection, message);
+}
+
+dbus_bool_t
+bus_transaction_send (BusTransaction *transaction,
+                      DBusConnection *connection,
+                      DBusMessage    *message)
 {
   MessageToSend *to_send;
   BusConnectionData *d;
   DBusList *link;
 
-  _dbus_verbose ("  trying to add message %s to transaction%s\n",
-                 dbus_message_get_name (message),
+  _dbus_verbose ("  trying to add %s interface=%s member=%s error=%s to transaction%s\n",
+                 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
+                 dbus_message_get_reply_serial (message) != 0 ? "reply" :
+                 "message",
+                 dbus_message_get_interface (message) ?
+                 dbus_message_get_interface (message) : "(unset)",
+                 dbus_message_get_member (message) ?
+                 dbus_message_get_member (message) : "(unset)",
+                 dbus_message_get_error_name (message) ?
+                 dbus_message_get_error_name (message) : "(unset)",
                  dbus_connection_get_is_connected (connection) ?
                  "" : " (disconnected)");
 
@@ -931,6 +2216,11 @@ bus_transaction_cancel_and_free (BusTransaction *transaction)
 
   _dbus_assert (transaction->connections == NULL);
 
+  _dbus_list_foreach (&transaction->cancel_hooks,
+                      cancel_hook_cancel, NULL);
+
+  free_cancel_hooks (transaction);
+  
   dbus_free (transaction);
 }
 
@@ -987,6 +2277,8 @@ bus_transaction_execute_and_free (BusTransaction *transaction)
 
   _dbus_assert (transaction->connections == NULL);
 
+  free_cancel_hooks (transaction);
+  
   dbus_free (transaction);
 }
 
@@ -1024,14 +2316,16 @@ bus_transaction_send_error_reply (BusTransaction  *transaction,
   _dbus_assert (error != NULL);
   _DBUS_ASSERT_ERROR_IS_SET (error);
   
-  reply = dbus_message_new_error_reply (in_reply_to,
-                                        error->name,
-                                        error->message);
+  _dbus_verbose ("Sending error reply %s \"%s\"\n",
+                 error->name, error->message);
+
+  reply = dbus_message_new_error (in_reply_to,
+                                  error->name,
+                                  error->message);
   if (reply == NULL)
     return FALSE;
 
-  if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS) ||
-      !bus_transaction_send_message (transaction, connection, reply))
+  if (!bus_transaction_send_from_driver (transaction, connection, reply))
     {
       dbus_message_unref (reply);
       return FALSE;
@@ -1041,3 +2335,102 @@ bus_transaction_send_error_reply (BusTransaction  *transaction,
   
   return TRUE;
 }
+
+dbus_bool_t
+bus_transaction_add_cancel_hook (BusTransaction               *transaction,
+                                 BusTransactionCancelFunction  cancel_function,
+                                 void                         *data,
+                                 DBusFreeFunction              free_data_function)
+{
+  CancelHook *ch;
+
+  ch = dbus_new (CancelHook, 1);
+  if (ch == NULL)
+    return FALSE;
+
+  _dbus_verbose ("     adding cancel hook function = %p data = %p\n",
+                 cancel_function, data);
+  
+  ch->cancel_function = cancel_function;
+  ch->data = data;
+  ch->free_data_function = free_data_function;
+
+  /* It's important that the hooks get run in reverse order that they
+   * were added
+   */
+  if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
+    {
+      dbus_free (ch);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+#ifdef DBUS_ENABLE_STATS
+int
+bus_connections_get_n_active (BusConnections *connections)
+{
+  return connections->n_completed;
+}
+
+int
+bus_connections_get_n_incomplete (BusConnections *connections)
+{
+  return connections->n_incomplete;
+}
+
+int
+bus_connections_get_total_match_rules (BusConnections *connections)
+{
+  return connections->total_match_rules;
+}
+
+int
+bus_connections_get_peak_match_rules (BusConnections *connections)
+{
+  return connections->peak_match_rules;
+}
+
+int
+bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
+{
+  return connections->peak_match_rules_per_conn;
+}
+
+int
+bus_connections_get_total_bus_names (BusConnections *connections)
+{
+  return connections->total_bus_names;
+}
+
+int
+bus_connections_get_peak_bus_names (BusConnections *connections)
+{
+  return connections->peak_bus_names;
+}
+
+int
+bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
+{
+  return connections->peak_bus_names_per_conn;
+}
+
+int
+bus_connection_get_peak_match_rules (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  return d->peak_match_rules;
+}
+
+int
+bus_connection_get_peak_bus_names (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  return d->peak_bus_names;
+}
+#endif /* DBUS_ENABLE_STATS */