2003-10-14 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / connection.c
index 146e376..1e56242 100644 (file)
 #include "policy.h"
 #include "services.h"
 #include "utils.h"
+#include "signals.h"
+#include "expirelist.h"
 #include <dbus/dbus-list.h>
 #include <dbus/dbus-hash.h>
+#include <dbus/dbus-timeout.h>
 
 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;
@@ -39,10 +53,12 @@ struct BusConnections
   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 */
 };
 
-static int connection_data_slot = -1;
-static int connection_data_slot_refcount = 0;
+static dbus_int32_t connection_data_slot = -1;
 
 typedef struct
 {
@@ -50,47 +66,30 @@ typedef struct
   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;
   BusClientPolicy *policy;
-} BusConnectionData;
 
-#define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
-
-static dbus_bool_t
-connection_data_slot_ref (void)
-{
-  if (connection_data_slot < 0)
-    {
-      connection_data_slot = dbus_connection_allocate_data_slot ();
-      
-      if (connection_data_slot < 0)
-        return FALSE;
-
-      _dbus_assert (connection_data_slot_refcount == 0);
-    }  
-
-  connection_data_slot_refcount += 1;
+  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 */
+} BusConnectionData;
 
-  return TRUE;
+static void bus_pending_reply_expired (BusExpireList *list,
+                                       DBusList      *link,
+                                       void          *data);
 
-}
+static void bus_connection_drop_pending_replies (BusConnections  *connections,
+                                                 DBusConnection  *connection);
 
-static void
-connection_data_slot_unref (void)
-{
-  _dbus_assert (connection_data_slot_refcount > 0);
+static dbus_bool_t expire_incomplete_timeout (void *data);
 
-  connection_data_slot_refcount -= 1;
-  
-  if (connection_data_slot_refcount == 0)
-    {
-      dbus_connection_free_data_slot (connection_data_slot);
-      connection_data_slot = -1;
-    }
-}
+#define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
 
 static DBusLoop*
 connection_get_loop (DBusConnection *connection)
@@ -166,22 +165,27 @@ bus_connection_disconnected (DBusConnection *connection)
 {
   BusConnectionData *d;
   BusService *service;
-
+  BusMatchmaker *matchmaker;
+  
   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)");
+
+  /* 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
    * 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. Probably we need to do that though.
    */
   while ((service = _dbus_list_get_last (&d->services_owned)))
     {
@@ -272,12 +276,14 @@ bus_connection_disconnected (DBusConnection *connection)
       _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);
 }
 
@@ -287,9 +293,9 @@ connection_watch_callback (DBusWatch     *watch,
                            void          *data)
 {
  /* FIXME this can be done in dbus-mainloop.c
-   * if the code in activation.c for the babysitter
-   * watch handler is fixed.
-   */
+  * if the code in activation.c for the babysitter
+  * watch handler is fixed.
+  */
   
 #if 0
   _dbus_verbose ("Calling handle_watch\n");
@@ -383,6 +389,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);
 
@@ -400,34 +407,68 @@ free_connection_data (void *data)
   dbus_free (d);
 }
 
+static void
+call_timeout_callback (DBusTimeout   *timeout,
+                       void          *data)
+{
+  /* can return FALSE on OOM but we just let it fire again later */
+  dbus_timeout_handle (timeout);
+}
+
 BusConnections*
 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_ULONG,
                                                          NULL, NULL);
   if (connections->completed_by_user == NULL)
-    {
-      dbus_free (connections);
-      connection_data_slot_unref ();
-      return 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,
+                               call_timeout_callback, NULL, NULL))
+    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
@@ -469,16 +510,25 @@ bus_connections_unref (BusConnections *connections)
           dbus_connection_ref (connection);
           dbus_connection_disconnect (connection);
           bus_connection_disconnected (connection);
-          dbus_connection_unref (connection);          
+          dbus_connection_unref (connection);
         }
 
       _dbus_assert (connections->n_completed == 0);
 
+      _dbus_assert (connections->pending_replies->n_items == 0);
+      bus_expire_list_free (connections->pending_replies);
+      
+      _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
+                                 connections->expire_timeout,
+                                 call_timeout_callback, NULL);
+      
+      _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);
     }
 }
 
@@ -496,7 +546,10 @@ bus_connections_setup_connection (BusConnections *connections,
 
   d->connections = connections;
   d->connection = connection;
-
+  
+  _dbus_get_current_time (&d->connection_tv_sec,
+                          &d->connection_tv_usec);
+  
   _dbus_assert (connection_data_slot >= 0);
   
   if (!dbus_connection_set_data (connection,
@@ -556,7 +609,14 @@ bus_connections_setup_connection (BusConnections *connections,
   dbus_connection_ref (connection);
 
   /* Note that we might disconnect ourselves here, but it only takes
-   * effect on return to the main loop.
+   * 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))
@@ -596,27 +656,101 @@ bus_connections_setup_connection (BusConnections *connections,
 
       dbus_connection_set_dispatch_status_function (connection,
                                                     NULL, NULL, NULL);
-      
-      if (!dbus_connection_set_data (connection,
-                                     connection_data_slot,
-                                     NULL, NULL))
-        _dbus_assert_not_reached ("failed to set connection data to 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;
 }
 
+void
+bus_connections_expire_incomplete (BusConnections *connections)
+{    
+  int next_interval;
+
+  next_interval = -1;
+  
+  if (connections->incomplete != NULL)
+    {
+      long tv_sec, tv_usec;
+      DBusList *link;
+      int auth_timeout;
+      
+      _dbus_get_current_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)
+        {
+          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_disconnect (connection);
+            }
+          else
+            {
+              /* 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;
+        }
+    }
+
+  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 %s\n", _DBUS_FUNCTION_NAME);
+  
+  /* note that this may remove the timeout */
+  bus_connections_expire_incomplete (connections);
+
+  return TRUE;
+}
+
 dbus_bool_t
 bus_connection_get_groups  (DBusConnection   *connection,
                             unsigned long   **groups,
-                            int              *n_groups)
+                            int              *n_groups,
+                            DBusError        *error)
 {
   BusConnectionData *d;
   unsigned long uid;
@@ -635,8 +769,9 @@ bus_connection_get_groups  (DBusConnection   *connection,
     {
       if (!_dbus_user_database_get_groups (user_database,
                                            uid, groups, n_groups,
-                                           NULL))
+                                           error))
         {
+          _DBUS_ASSERT_ERROR_IS_SET (error);
           _dbus_verbose ("Did not get any groups for UID %lu\n",
                          uid);
           return FALSE;
@@ -660,7 +795,8 @@ bus_connection_is_in_group (DBusConnection *connection,
   unsigned long *group_ids;
   int n_group_ids;
 
-  if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
+  if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids,
+                                  NULL))
     return FALSE;
 
   i = 0;
@@ -686,29 +822,8 @@ 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_client_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;
 }
 
@@ -798,6 +913,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)
 {
@@ -846,6 +995,18 @@ bus_connection_get_activation (DBusConnection *connection)
   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);
+}
+
 /**
  * Checks whether the connection is registered with the message bus.
  *
@@ -880,19 +1041,19 @@ bus_connection_preallocate_oom_error (DBusConnection *connection)
   if (preallocated == NULL)
     return FALSE;
 
-  /* d->name may be NULL, but that is OK */
-  message = dbus_message_new (DBUS_ERROR_NO_MEMORY,
-                              d->name);
+  message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
+
   if (message == NULL)
     {
       dbus_connection_free_preallocated_send (connection, preallocated);
       return FALSE;
     }
 
-  dbus_message_set_is_error (message, TRUE);
-
-  if (!dbus_message_set_sender (message,
-                                DBUS_SERVICE_DBUS))
+  /* 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_ORG_FREEDESKTOP_DBUS))
     {
       dbus_connection_free_preallocated_send (connection, preallocated);
       dbus_message_unref (message);
@@ -942,6 +1103,62 @@ bus_connection_send_oom_error (DBusConnection *connection,
 }
 
 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;
+}
+
+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);
+}
+
+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)
 {
@@ -951,6 +1168,8 @@ bus_connection_add_owned_service_link (DBusConnection *connection,
   _dbus_assert (d != NULL);
 
   _dbus_list_append_link (&d->services_owned, link);
+
+  d->n_services_owned += 1;
 }
 
 dbus_bool_t
@@ -979,11 +1198,26 @@ bus_connection_remove_owned_service (DBusConnection *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);
+}
+
+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;
 }
 
 dbus_bool_t
-bus_connection_set_name (DBusConnection   *connection,
-                        const DBusString *name)
+bus_connection_complete (DBusConnection   *connection,
+                        const DBusString *name,
+                         DBusError        *error)
 {
   BusConnectionData *d;
   unsigned long uid;
@@ -991,19 +1225,45 @@ bus_connection_set_name (DBusConnection   *connection,
   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))
-    return FALSE;
+    {
+      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))
         {
+          BUS_SET_OOM (error);
           dbus_free (d->name);
           d->name = NULL;
           return FALSE;
@@ -1020,6 +1280,11 @@ bus_connection_set_name (DBusConnection   *connection,
 
   _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;
 }
@@ -1075,6 +1340,347 @@ bus_connections_check_limits (BusConnections  *connections,
   return TRUE;
 }
 
+static dbus_bool_t
+bus_pending_reply_send_no_reply (BusConnections  *connections,
+                                 BusTransaction  *transaction,
+                                 BusPendingReply *pending)
+{
+  DBusMessage *message;
+  DBusMessageIter iter;
+  dbus_bool_t retval;
+
+  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;
+  
+  dbus_message_append_iter_init (message, &iter);
+  if (!dbus_message_iter_append_string (&iter, "Message did not receive a reply (timeout by message bus)"))
+    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 void
+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.
+   */
+  transaction = bus_transaction_new (connections->context);
+  if (transaction == NULL)
+    return;
+  
+  if (bus_pending_reply_send_no_reply (connections,
+                                       transaction,
+                                       pending))
+    {
+      _dbus_list_remove_link (&connections->pending_replies->items,
+                              link);
+      dbus_free (pending);
+      bus_transaction_cancel_and_free (transaction);
+    }
+
+  bus_transaction_execute_and_free (transaction);
+}
+
+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;
+  
+  link = _dbus_list_get_first_link (&connections->pending_replies->items);
+  while (link != NULL)
+    {
+      DBusList *next;
+      BusPendingReply *pending;
+
+      next = _dbus_list_get_next_link (&connections->pending_replies->items,
+                                       link);
+      pending = link->data;
+
+      if (pending->will_get_reply == connection)
+        {
+          /* We don't need to track this pending reply anymore */
+
+          _dbus_list_remove_link (&connections->pending_replies->items,
+                                  link);
+          dbus_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
+           */
+
+          pending->will_send_reply = NULL;
+          pending->expire_item.added_tv_sec = 0;
+          pending->expire_item.added_tv_usec = 0;
+
+          bus_expire_timeout_set_interval (connections->pending_replies->timeout,
+                                           0);
+        }
+      
+      link = next;
+    }
+}
+
+
+typedef struct
+{
+  BusPendingReply *pending;
+  BusConnections  *connections;
+} CancelPendingReplyData;
+
+static void
+cancel_pending_reply (void *data)
+{
+  CancelPendingReplyData *d = data;
+
+  if (!_dbus_list_remove (&d->connections->pending_replies->items,
+                          d->pending))
+    _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
+
+  dbus_free (d->pending); /* since it's been cancelled */
+}
+
+static void
+cancel_pending_reply_data_free (void *data)
+{
+  CancelPendingReplyData *d = data;
+
+  /* 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_connections_expect_reply (BusConnections  *connections,
+                              BusTransaction  *transaction,
+                              DBusConnection  *will_get_reply,
+                              DBusConnection  *will_send_reply,
+                              DBusMessage     *reply_to_this,
+                              DBusError       *error)
+{
+  BusPendingReply *pending;
+  dbus_uint32_t reply_serial;
+  DBusList *link;
+  CancelPendingReplyData *cprd;
+
+  _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);
+
+  link = _dbus_list_get_first_link (&connections->pending_replies->items);
+  while (link != NULL)
+    {
+      pending = link->data;
+
+      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 = _dbus_list_get_next_link (&connections->pending_replies->items,
+                                       link);
+    }
+  
+  pending = dbus_new0 (BusPendingReply, 1);
+  if (pending == NULL)
+    {
+      BUS_SET_OOM (error);
+      return FALSE;
+    }
+
+  cprd = dbus_new0 (CancelPendingReplyData, 1);
+  if (cprd == NULL)
+    {
+      BUS_SET_OOM (error);
+      dbus_free (pending);
+      return FALSE;
+    }
+  
+  if (!_dbus_list_prepend (&connections->pending_replies->items,
+                           pending))
+    {
+      BUS_SET_OOM (error);
+      dbus_free (cprd);
+      dbus_free (pending);
+      return FALSE;
+    }
+
+  if (!bus_transaction_add_cancel_hook (transaction,
+                                        cancel_pending_reply,
+                                        cprd,
+                                        cancel_pending_reply_data_free))
+    {
+      BUS_SET_OOM (error);
+      _dbus_list_remove (&connections->pending_replies->items, pending);
+      dbus_free (cprd);
+      dbus_free (pending);
+      return FALSE;
+    }
+                                        
+  cprd->pending = pending;
+  cprd->connections = connections;
+  
+  _dbus_get_current_time (&pending->expire_item.added_tv_sec,
+                          &pending->expire_item.added_tv_usec);
+
+  pending->will_get_reply = will_get_reply;
+  pending->will_send_reply = will_send_reply;
+  pending->reply_serial = reply_serial;
+
+  return TRUE;
+}
+
+typedef struct
+{
+  DBusList        *link;
+  BusConnections  *connections;
+} CheckPendingReplyData;
+
+static void
+cancel_check_pending_reply (void *data)
+{
+  CheckPendingReplyData *d = data;
+
+  _dbus_list_prepend_link (&d->connections->pending_replies->items,
+                           d->link);
+  d->link = NULL;
+}
+
+static void
+check_pending_reply_data_free (void *data)
+{
+  CheckPendingReplyData *d = data;
+
+  if (d->link != NULL)
+    {
+      BusPendingReply *pending = d->link->data;
+
+      dbus_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_connections_check_reply (BusConnections *connections,
+                             BusTransaction *transaction,
+                             DBusConnection *sending_reply,
+                             DBusConnection *receiving_reply,
+                             DBusMessage    *reply,
+                             DBusError      *error)
+{
+  CheckPendingReplyData *cprd;
+  DBusList *link;
+  dbus_uint32_t reply_serial;
+  
+  _dbus_assert (sending_reply != NULL);
+  _dbus_assert (receiving_reply != NULL);
+
+  reply_serial = dbus_message_get_reply_serial (reply);
+
+  link = _dbus_list_get_first_link (&connections->pending_replies->items);
+  while (link != NULL)
+    {
+      BusPendingReply *pending = link->data;
+
+      if (pending->reply_serial == reply_serial &&
+          pending->will_get_reply == receiving_reply &&
+          pending->will_send_reply == sending_reply)
+        {
+          _dbus_verbose ("Found pending reply\n");
+          break;
+        }
+      
+      link = _dbus_list_get_next_link (&connections->pending_replies->items,
+                                       link);
+    }
+
+  if (link == NULL)
+    {
+      _dbus_verbose ("No pending reply expected\n");
+
+      return FALSE;
+    }
+
+  cprd = dbus_new0 (CheckPendingReplyData, 1);
+  if (cprd == NULL)
+    {
+      BUS_SET_OOM (error);
+      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;
+    }
+
+  cprd->link = link;
+  cprd->connections = connections;
+  
+  _dbus_list_remove_link (&connections->pending_replies->items,
+                          link);
+
+  return TRUE;
+}
 
 /*
  * Transactions
@@ -1185,17 +1791,26 @@ bus_transaction_send_from_driver (BusTransaction *transaction,
    * to check security policy since it was not done in
    * dispatch.c
    */
-  _dbus_verbose ("Sending %s from driver\n",
-                 dbus_message_get_name (message));
-  
-  if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
+  _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_ORG_FREEDESKTOP_DBUS))
     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),
-                                          NULL, connection, message, NULL))
+                                          transaction,
+                                          NULL, connection, connection, message, NULL))
     return TRUE;
 
   return bus_transaction_send (transaction, connection, message);
@@ -1210,11 +1825,16 @@ bus_transaction_send (BusTransaction *transaction,
   BusConnectionData *d;
   DBusList *link;
 
-  _dbus_verbose ("  trying to add %s %s to transaction%s\n",
-                 dbus_message_get_is_error (message) ? "error" :
+  _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_name (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)");
 
@@ -1424,9 +2044,12 @@ 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;