Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / bus / driver.c
index bdf5afb..af85eb0 100644 (file)
@@ -3,9 +3,10 @@
  *
  * Copyright (C) 2003 CodeFactory AB
  * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
+ * Copyright (C) 2013  Samsung Electronics
  *
  * 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
  * the Free Software Foundation; either version 2 of the License, or
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * 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 "activation.h"
 #include "connection.h"
 #include "driver.h"
 #include "services.h"
 #include "selinux.h"
 #include "signals.h"
+#include "stats.h"
 #include "utils.h"
+
+#include <dbus/dbus-asv-util.h>
 #include <dbus/dbus-string.h>
 #include <dbus/dbus-internals.h>
+#include <dbus/dbus-message.h>
 #include <dbus/dbus-marshal-recursive.h>
 #include <string.h>
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+#include "kdbus-d.h"
+#include <stdio.h>
+#include <errno.h>
+#include <limits.h>
+#endif
+
+static DBusConnection *
+bus_driver_get_conn_helper (DBusConnection  *connection,
+                            DBusMessage     *message,
+                            const char      *what_we_want,
+                            const char     **name_p,
+                            DBusError       *error)
+{
+  const char *name;
+  BusRegistry *registry;
+  BusService *serv;
+  DBusString str;
+  DBusConnection *conn;
+
+  if (!dbus_message_get_args (message, error,
+                              DBUS_TYPE_STRING, &name,
+                              DBUS_TYPE_INVALID))
+    return NULL;
+
+  _dbus_assert (name != NULL);
+  _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
+
+  registry = bus_connection_get_registry (connection);
+  _dbus_string_init_const (&str, name);
+  serv = bus_registry_lookup (registry, &str);
+
+  if (serv == NULL)
+    {
+      dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
+                      "Could not get %s of name '%s': no such name",
+                      what_we_want, name);
+      return NULL;
+    }
+
+  conn = bus_service_get_primary_owners_connection (serv);
+  _dbus_assert (conn != NULL);
+
+  if (name_p != NULL)
+    *name_p = name;
+
+  return conn;
+}
+
 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
                                                     DBusMessage    *hello_message,
                                                     BusTransaction *transaction,
@@ -51,24 +106,29 @@ bus_driver_send_service_owner_changed (const char     *service_name,
   dbus_bool_t retval;
   const char *null_service;
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+         return TRUE;
+#endif
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   null_service = "";
   _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
-                 service_name, 
-                 old_owner ? old_owner : null_service, 
+                 service_name,
+                 old_owner ? old_owner : null_service,
                  new_owner ? new_owner : null_service);
 
   message = dbus_message_new_signal (DBUS_PATH_DBUS,
                                      DBUS_INTERFACE_DBUS,
                                      "NameOwnerChanged");
-  
+
   if (message == NULL)
     {
       BUS_SET_OOM (error);
       return FALSE;
     }
-  
+
   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
     goto oom;
 
@@ -80,7 +140,7 @@ bus_driver_send_service_owner_changed (const char     *service_name,
     goto oom;
 
   _dbus_assert (dbus_message_has_signature (message, "sss"));
-  
+
   retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
   dbus_message_unref (message);
 
@@ -100,18 +160,23 @@ bus_driver_send_service_lost (DBusConnection *connection,
 {
   DBusMessage *message;
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+         return TRUE;
+#endif
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   message = dbus_message_new_signal (DBUS_PATH_DBUS,
                                      DBUS_INTERFACE_DBUS,
                                      "NameLost");
-  
+
   if (message == NULL)
     {
       BUS_SET_OOM (error);
       return FALSE;
     }
-  
+
   if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
       !dbus_message_append_args (message,
                                  DBUS_TYPE_STRING, &service_name,
@@ -143,8 +208,13 @@ bus_driver_send_service_acquired (DBusConnection *connection,
 {
   DBusMessage *message;
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+         return TRUE;
+#endif
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   message = dbus_message_new_signal (DBUS_PATH_DBUS,
                                      DBUS_INTERFACE_DBUS,
                                      "NameAcquired");
@@ -154,7 +224,7 @@ bus_driver_send_service_acquired (DBusConnection *connection,
       BUS_SET_OOM (error);
       return FALSE;
     }
-  
+
   if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
       !dbus_message_append_args (message,
                                  DBUS_TYPE_STRING, &service_name,
@@ -192,9 +262,9 @@ create_unique_client_name (BusRegistry *registry,
   static int next_major_number = 0;
   static int next_minor_number = 0;
   int len;
-  
+
   len = _dbus_string_get_length (str);
-  
+
   while (TRUE)
     {
       /* start out with 1-0, go to 1-1, 1-2, 1-3,
@@ -212,21 +282,21 @@ create_unique_client_name (BusRegistry *registry,
       _dbus_assert (next_minor_number >= 0);
 
       /* appname:MAJOR-MINOR */
-      
+
       if (!_dbus_string_append (str, ":"))
         return FALSE;
-      
+
       if (!_dbus_string_append_int (str, next_major_number))
         return FALSE;
 
       if (!_dbus_string_append (str, "."))
         return FALSE;
-      
+
       if (!_dbus_string_append_int (str, next_minor_number))
         return FALSE;
 
       next_minor_number += 1;
-      
+
       /* Check if a client with the name exists */
       if (bus_registry_lookup (registry, str) == NULL)
        break;
@@ -273,7 +343,7 @@ bus_driver_handle_hello (DBusConnection *connection,
       _DBUS_ASSERT_ERROR_IS_SET (error);
       return FALSE;
     }
-  
+
   if (!_dbus_string_init (&unique_name))
     {
       BUS_SET_OOM (error);
@@ -283,7 +353,7 @@ bus_driver_handle_hello (DBusConnection *connection,
   retval = FALSE;
 
   registry = bus_connection_get_registry (connection);
-  
+
   if (!create_unique_client_name (registry, &unique_name))
     {
       BUS_SET_OOM (error);
@@ -295,14 +365,14 @@ bus_driver_handle_hello (DBusConnection *connection,
       _DBUS_ASSERT_ERROR_IS_SET (error);
       goto out_0;
     }
-  
+
   if (!dbus_message_set_sender (message,
                                 bus_connection_get_name (connection)))
     {
       BUS_SET_OOM (error);
       goto out_0;
     }
-  
+
   if (!bus_driver_send_welcome_message (connection, message, transaction, error))
     goto out_0;
 
@@ -311,10 +381,10 @@ bus_driver_handle_hello (DBusConnection *connection,
                                  &unique_name, connection, 0, transaction, error);
   if (service == NULL)
     goto out_0;
-  
+
   _dbus_assert (bus_connection_is_active (connection));
   retval = TRUE;
-  
+
  out_0:
   _dbus_string_free (&unique_name);
   return retval;
@@ -330,17 +400,17 @@ bus_driver_send_welcome_message (DBusConnection *connection,
   const char *name;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   name = bus_connection_get_name (connection);
   _dbus_assert (name != NULL);
-  
+
   welcome = dbus_message_new_method_return (hello_message);
   if (welcome == NULL)
     {
       BUS_SET_OOM (error);
       return FALSE;
     }
-  
+
   if (!dbus_message_append_args (welcome,
                                  DBUS_TYPE_STRING, &name,
                                  DBUS_TYPE_INVALID))
@@ -351,7 +421,7 @@ bus_driver_send_welcome_message (DBusConnection *connection,
     }
 
   _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
-  
+
   if (!bus_transaction_send_from_driver (transaction, connection, welcome))
     {
       dbus_message_unref (welcome);
@@ -380,9 +450,9 @@ bus_driver_handle_list_services (DBusConnection *connection,
   DBusMessageIter sub;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   registry = bus_connection_get_registry (connection);
-  
+
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     {
@@ -390,15 +460,30 @@ bus_driver_handle_list_services (DBusConnection *connection,
       return FALSE;
     }
 
-  if (!bus_registry_list_services (registry, &services, &len))
-    {
-      dbus_message_unref (reply);
-      BUS_SET_OOM (error);
-      return FALSE;
-    }
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         if(!kdbus_list_services (connection, &services, &len))
+           {
+             dbus_message_unref (reply);
+             BUS_SET_OOM (error);
+             return FALSE;
+           }
+  }
+  else
+#endif
+  {
+
+      if (!bus_registry_list_services (registry, &services, &len))
+        {
+          dbus_message_unref (reply);
+          BUS_SET_OOM (error);
+          return FALSE;
+        }
+  }
 
   dbus_message_iter_init_append (reply, &iter);
-  
+
   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
                                          DBUS_TYPE_STRING_AS_STRING,
                                          &sub))
@@ -409,7 +494,11 @@ bus_driver_handle_list_services (DBusConnection *connection,
       return FALSE;
     }
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //not needed for kdbus, we got it from kdbus_list_services
+#endif
   {
+
     /* Include the bus driver in the list */
     const char *v_STRING = DBUS_SERVICE_DBUS;
     if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
@@ -421,7 +510,7 @@ bus_driver_handle_list_services (DBusConnection *connection,
         return FALSE;
       }
   }
-  
+
   i = 0;
   while (i < len)
     {
@@ -444,7 +533,7 @@ bus_driver_handle_list_services (DBusConnection *connection,
       BUS_SET_OOM (error);
       return FALSE;
     }
-  
+
   if (!bus_transaction_send_from_driver (transaction, connection, reply))
     {
       dbus_message_unref (reply);
@@ -566,28 +655,42 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
   BusRegistry *registry;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   registry = bus_connection_get_registry (connection);
-  
+
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &name,
                               DBUS_TYPE_UINT32, &flags,
                               DBUS_TYPE_INVALID))
     return FALSE;
-  
+
   _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
-  
+
   retval = FALSE;
   reply = NULL;
 
-  _dbus_string_init_const (&service_name, name);
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         if (!bus_registry_acquire_kdbus_service (registry, connection,
+                                                                                message,
+                                            &service_reply, transaction,
+                                            error))
+           goto out;
+  }
+  else
+#endif
+  {
+
+         _dbus_string_init_const (&service_name, name);
+
+         if (!bus_registry_acquire_service (registry, connection,
+                                            &service_name, flags,
+                                            &service_reply, transaction,
+                                            error))
+           goto out;
+  }
 
-  if (!bus_registry_acquire_service (registry, connection,
-                                     &service_name, flags,
-                                     &service_reply, transaction,
-                                     error))
-    goto out;
-  
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     {
@@ -608,12 +711,12 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
     }
 
   retval = TRUE;
-  
+
  out:
   if (reply)
     dbus_message_unref (reply);
   return retval;
-} 
+}
 
 static dbus_bool_t
 bus_driver_handle_release_service (DBusConnection *connection,
@@ -630,8 +733,6 @@ bus_driver_handle_release_service (DBusConnection *connection,
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  registry = bus_connection_get_registry (connection);
-
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &name,
                               DBUS_TYPE_INVALID))
@@ -643,7 +744,18 @@ bus_driver_handle_release_service (DBusConnection *connection,
   reply = NULL;
 
   _dbus_string_init_const (&service_name, name);
+  registry = bus_connection_get_registry (connection);
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         if (!bus_registry_release_service_kdbus (dbus_message_get_sender(message), connection,
+                                            &service_name, &service_reply,
+                                            transaction, error))
+             goto out;
+  }
+  else
+#endif
   if (!bus_registry_release_service (registry, connection,
                                      &service_name, &service_reply,
                                      transaction, error))
@@ -691,9 +803,9 @@ bus_driver_handle_service_exists (DBusConnection *connection,
   BusRegistry *registry;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   registry = bus_connection_get_registry (connection);
-  
+
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &name,
                               DBUS_TYPE_INVALID))
@@ -707,11 +819,31 @@ bus_driver_handle_service_exists (DBusConnection *connection,
     }
   else
     {
-      _dbus_string_init_const (&service_name, name);
-      service = bus_registry_lookup (registry, &service_name);
-      service_exists = service != NULL;
+#ifdef ENABLE_KDBUS_TRANSPORT
+         if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+         {
+                 int inter_ret;
+                 struct nameInfo info;
+
+                 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
+                       if((inter_ret == 0) || (inter_ret == -ENOENT))
+                               service_exists = (inter_ret == 0) ? TRUE : FALSE;
+                       else
+                       {
+                               _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
+                               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
+                               service_exists = FALSE;
+                       }
+         }
+         else
+#endif
+         {
+             _dbus_string_init_const (&service_name, name);
+             service = bus_registry_lookup (registry, &service_name);
+             service_exists = service != NULL;
+         }
     }
-  
+
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     {
@@ -734,7 +866,7 @@ bus_driver_handle_service_exists (DBusConnection *connection,
     }
 
   retval = TRUE;
-  
+
  out:
   if (reply)
     dbus_message_unref (reply);
@@ -754,9 +886,9 @@ bus_driver_handle_activate_service (DBusConnection *connection,
   BusActivation *activation;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   activation = bus_connection_get_activation (connection);
-  
+
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &name,
                               DBUS_TYPE_UINT32, &flags,
@@ -778,7 +910,7 @@ bus_driver_handle_activate_service (DBusConnection *connection,
     }
 
   retval = TRUE;
-  
+
  out:
   return retval;
 }
@@ -791,6 +923,9 @@ send_ack_reply (DBusConnection *connection,
 {
   DBusMessage *reply;
 
+  if (dbus_message_get_no_reply (message))
+    return TRUE;
+
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     {
@@ -806,11 +941,135 @@ send_ack_reply (DBusConnection *connection,
     }
 
   dbus_message_unref (reply);
-  
+
   return TRUE;
 }
 
 static dbus_bool_t
+bus_driver_handle_update_activation_environment (DBusConnection *connection,
+                                                 BusTransaction *transaction,
+                                                 DBusMessage    *message,
+                                                 DBusError      *error)
+{
+  dbus_bool_t retval;
+  BusActivation *activation;
+  DBusMessageIter iter;
+  DBusMessageIter dict_iter;
+  DBusMessageIter dict_entry_iter;
+  int array_type;
+  int key_type;
+  DBusList *keys, *key_link;
+  DBusList *values, *value_link;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
+  activation = bus_connection_get_activation (connection);
+
+  dbus_message_iter_init (message, &iter);
+
+  /* The message signature has already been checked for us,
+   * so let's just assert it's right.
+   */
+  _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
+
+  dbus_message_iter_recurse (&iter, &dict_iter);
+
+  retval = FALSE;
+
+  /* Then loop through the sent dictionary, add the location of
+   * the environment keys and values to lists. The result will
+   * be in reverse order, so we don't have to constantly search
+   * for the end of the list in a loop.
+   */
+  keys = NULL;
+  values = NULL;
+  while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
+    {
+      dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
+
+      while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
+        {
+          char *key;
+          char *value;
+          int value_type;
+
+          dbus_message_iter_get_basic (&dict_entry_iter, &key);
+          dbus_message_iter_next (&dict_entry_iter);
+
+          value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
+
+          if (value_type != DBUS_TYPE_STRING)
+            break;
+
+          dbus_message_iter_get_basic (&dict_entry_iter, &value);
+
+          if (!_dbus_list_append (&keys, key))
+            {
+              BUS_SET_OOM (error);
+              break;
+            }
+
+          if (!_dbus_list_append (&values, value))
+            {
+              BUS_SET_OOM (error);
+              break;
+            }
+
+          dbus_message_iter_next (&dict_entry_iter);
+        }
+
+      if (key_type != DBUS_TYPE_INVALID)
+        break;
+
+      dbus_message_iter_next (&dict_iter);
+    }
+
+  if (array_type != DBUS_TYPE_INVALID)
+    goto out;
+
+  _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
+
+  key_link = keys;
+  value_link = values;
+  while (key_link != NULL)
+  {
+      const char *key;
+      const char *value;
+
+      key = key_link->data;
+      value = value_link->data;
+
+      if (!bus_activation_set_environment_variable (activation,
+                                                    key, value, error))
+      {
+          _DBUS_ASSERT_ERROR_IS_SET (error);
+          _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
+          break;
+      }
+      key_link = _dbus_list_get_next_link (&keys, key_link);
+      value_link = _dbus_list_get_next_link (&values, value_link);
+  }
+
+  /* FIXME: We can fail early having set only some of the environment variables,
+   * (because of OOM failure).  It's sort of hard to fix and it doesn't really
+   * matter, so we're punting for now.
+   */
+  if (key_link != NULL)
+    goto out;
+
+  if (!send_ack_reply (connection, transaction,
+                       message, error))
+    goto out;
+
+  retval = TRUE;
+
+ out:
+  _dbus_list_clear (&keys);
+  _dbus_list_clear (&values);
+  return retval;
+}
+
+static dbus_bool_t
 bus_driver_handle_add_match (DBusConnection *connection,
                              BusTransaction *transaction,
                              DBusMessage    *message,
@@ -820,7 +1079,7 @@ bus_driver_handle_add_match (DBusConnection *connection,
   const char *text;
   DBusString str;
   BusMatchmaker *matchmaker;
-  
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   text = NULL;
@@ -837,7 +1096,7 @@ bus_driver_handle_add_match (DBusConnection *connection,
                       "(inactive)");
       goto failed;
     }
-  
+
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &text,
                               DBUS_TYPE_INVALID))
@@ -852,23 +1111,39 @@ bus_driver_handle_add_match (DBusConnection *connection,
   if (rule == NULL)
     goto failed;
 
-  matchmaker = bus_connection_get_matchmaker (connection);
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
 
-  if (!bus_matchmaker_add_rule (matchmaker, rule))
-    {
-      BUS_SET_OOM (error);
-      goto failed;
-    }
+         if (!kdbus_add_match_rule (connection, message, text, error))
+             goto failed;
+
+         if (!send_ack_reply (connection, transaction,
+                              message, error))
+             goto failed;
+  }
+  else
+#endif
+  {
+
+         matchmaker = bus_connection_get_matchmaker (connection);
+
+         if (!bus_matchmaker_add_rule (matchmaker, rule))
+           {
+             BUS_SET_OOM (error);
+             goto failed;
+           }
+
+         if (!send_ack_reply (connection, transaction,
+                              message, error))
+           {
+             bus_matchmaker_remove_rule (matchmaker, rule);
+             goto failed;
+           }
+  }
 
-  if (!send_ack_reply (connection, transaction,
-                       message, error))
-    {
-      bus_matchmaker_remove_rule (matchmaker, rule);
-      goto failed;
-    }
-  
   bus_match_rule_unref (rule);
-  
+
   return TRUE;
 
  failed:
@@ -888,12 +1163,12 @@ bus_driver_handle_remove_match (DBusConnection *connection,
   const char *text;
   DBusString str;
   BusMatchmaker *matchmaker;
-  
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   text = NULL;
   rule = NULL;
-  
+
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &text,
                               DBUS_TYPE_INVALID))
@@ -908,20 +1183,33 @@ bus_driver_handle_remove_match (DBusConnection *connection,
   if (rule == NULL)
     goto failed;
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         if(!kdbus_remove_match(connection, message, error))
+                 goto failed;
+  }
+#endif
+
   /* Send the ack before we remove the rule, since the ack is undone
    * on transaction cancel, but rule removal isn't.
    */
   if (!send_ack_reply (connection, transaction,
                        message, error))
     goto failed;
-  
-  matchmaker = bus_connection_get_matchmaker (connection);
 
-  if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
-    goto failed;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+#endif
+  {
+         matchmaker = bus_connection_get_matchmaker (connection);
+
+         if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
+               goto failed;
+  }
 
   bus_match_rule_unref (rule);
-  
+
   return TRUE;
 
  failed:
@@ -943,7 +1231,7 @@ bus_driver_handle_get_service_owner (DBusConnection *connection,
   BusRegistry *registry;
   BusService *service;
   DBusMessage *reply;
-  
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   registry = bus_connection_get_registry (connection);
@@ -956,46 +1244,77 @@ bus_driver_handle_get_service_owner (DBusConnection *connection,
                               DBUS_TYPE_INVALID))
       goto failed;
 
-  _dbus_string_init_const (&str, text);
-  service = bus_registry_lookup (registry, &str);
-  if (service == NULL &&
-      _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
-    {
-      /* ORG_FREEDESKTOP_DBUS owns itself */
-      base_name = DBUS_SERVICE_DBUS;
-    }
-  else if (service == NULL)
-    {
-      dbus_set_error (error, 
-                      DBUS_ERROR_NAME_HAS_NO_OWNER,
-                      "Could not get owner of name '%s': no such name", text);
-      goto failed;
-    }
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         int ret;
+         struct nameInfo info;
+         char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
+
+         ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
+               if(ret == 0) //unique id of the name
+               {
+                       sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
+                       _dbus_verbose("Unique name discovered:%s\n", unique_name);
+                       base_name = unique_name;
+               }
+               else if(ret == -ENOENT)  //name has no owner
+               {
+                         dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
+                                                         "Could not get owner of name '%s': no such name", text);
+                         goto failed;
+               }
+               else
+               {
+                       _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
+                       dbus_set_error (error, DBUS_ERROR_FAILED,
+                                                         "Could not determine unique name for '%s'", text);
+                       goto failed;
+               }
+  }
   else
-    {
-      base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
-      if (base_name == NULL)
-        {
-          /* FIXME - how is this error possible? */
-          dbus_set_error (error,
-                          DBUS_ERROR_FAILED,
-                          "Could not determine unique name for '%s'", text);
-          goto failed;
-        }
-      _dbus_assert (*base_name == ':');      
-    }
-
+#endif
+    {
+         _dbus_string_init_const (&str, text);
+         service = bus_registry_lookup (registry, &str);
+         if (service == NULL &&
+                 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
+               {
+                 /* ORG_FREEDESKTOP_DBUS owns itself */
+                 base_name = DBUS_SERVICE_DBUS;
+               }
+         else if (service == NULL)
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_NAME_HAS_NO_OWNER,
+                                                 "Could not get owner of name '%s': no such name", text);
+                 goto failed;
+               }
+         else
+               {
+                 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
+                 if (base_name == NULL)
+                       {
+                         /* FIXME - how is this error possible? */
+                         dbus_set_error (error,
+                                                         DBUS_ERROR_FAILED,
+                                                         "Could not determine unique name for '%s'", text);
+                         goto failed;
+                       }
+                 _dbus_assert (*base_name == ':');
+               }
+  }
   _dbus_assert (base_name != NULL);
 
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     goto oom;
 
-  if (! dbus_message_append_args (reply, 
+  if (! dbus_message_append_args (reply,
                                  DBUS_TYPE_STRING, &base_name,
                                  DBUS_TYPE_INVALID))
     goto oom;
-  
+
   if (! bus_transaction_send_from_driver (transaction, connection, reply))
     goto oom;
 
@@ -1028,7 +1347,7 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
   DBusMessage *reply;
   DBusMessageIter iter, array_iter;
   char *dbus_service_name = DBUS_SERVICE_DBUS;
-  
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   registry = bus_connection_get_registry (connection);
@@ -1053,14 +1372,14 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
     }
   else if (service == NULL)
     {
-      dbus_set_error (error, 
+      dbus_set_error (error,
                       DBUS_ERROR_NAME_HAS_NO_OWNER,
                       "Could not get owners of name '%s': no such name", text);
       goto failed;
     }
   else
     {
-      if (!bus_service_list_queued_owners (service, 
+      if (!bus_service_list_queued_owners (service,
                                            &base_names,
                                            error))
         goto failed;
@@ -1078,7 +1397,7 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
                                          DBUS_TYPE_STRING_AS_STRING,
                                          &array_iter))
     goto oom;
-  
+
   link = _dbus_list_get_first_link (&base_names);
   while (link != NULL)
     {
@@ -1086,8 +1405,8 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
 
       _dbus_assert (link->data != NULL);
       uname = (char *)link->data;
-    
-      if (!dbus_message_iter_append_basic (&array_iter, 
+
+      if (!dbus_message_iter_append_basic (&array_iter,
                                            DBUS_TYPE_STRING,
                                            &uname))
         goto oom;
@@ -1097,8 +1416,8 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
 
   if (! dbus_message_iter_close_container (&iter, &array_iter))
     goto oom;
-                                    
+
+
   if (! bus_transaction_send_from_driver (transaction, connection, reply))
     goto oom;
 
@@ -1126,52 +1445,41 @@ bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
                                             DBusMessage    *message,
                                             DBusError      *error)
 {
-  const char *service;
-  DBusString str;
-  BusRegistry *registry;
-  BusService *serv;
   DBusConnection *conn;
   DBusMessage *reply;
   unsigned long uid;
   dbus_uint32_t uid32;
+  const char *service;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  registry = bus_connection_get_registry (connection);
-
-  service = NULL;
-  reply = NULL;
-
-  if (! dbus_message_get_args (message, error,
-                              DBUS_TYPE_STRING, &service,
-                              DBUS_TYPE_INVALID))
-      goto failed;
-
-  _dbus_verbose ("asked for UID of connection %s\n", service);
-
-  _dbus_string_init_const (&str, service);
-  serv = bus_registry_lookup (registry, &str);
-  if (serv == NULL)
-    {
-      dbus_set_error (error, 
-                     DBUS_ERROR_NAME_HAS_NO_OWNER,
-                     "Could not get UID of name '%s': no such name", service);
-      goto failed;
-    }
-
-  conn = bus_service_get_primary_owners_connection (serv);
-
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     goto oom;
 
-  if (!dbus_connection_get_unix_user (conn, &uid))
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_FAILED,
-                      "Could not determine UID for '%s'", service);
-      goto failed;
-    }
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
+                 goto failed;
+  }
+  else
+#endif
+  {
+         conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
+                                     error);
+
+         if (conn == NULL)
+                 goto failed;
+
+         if (!dbus_connection_get_unix_user (conn, &uid))
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_FAILED,
+                                                 "Could not determine UID for '%s'", service);
+                 goto failed;
+               }
+  }
 
   uid32 = uid;
   if (! dbus_message_append_args (reply,
@@ -1202,52 +1510,43 @@ bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
                                                  DBusMessage    *message,
                                                  DBusError      *error)
 {
-  const char *service;
-  DBusString str;
-  BusRegistry *registry;
-  BusService *serv;
   DBusConnection *conn;
   DBusMessage *reply;
   unsigned long pid;
   dbus_uint32_t pid32;
+  const char *service;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  registry = bus_connection_get_registry (connection);
-
-  service = NULL;
-  reply = NULL;
-
-  if (! dbus_message_get_args (message, error,
-                              DBUS_TYPE_STRING, &service,
-                              DBUS_TYPE_INVALID))
-      goto failed;
+  reply = dbus_message_new_method_return (message);
+  if (reply == NULL)
+    goto oom;
 
-  _dbus_verbose ("asked for PID of connection %s\n", service);
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
+                 goto failed;
+  }
+  else
+#endif
+  {
+         conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
+                                                                                error);
 
-  _dbus_string_init_const (&str, service);
-  serv = bus_registry_lookup (registry, &str);
-  if (serv == NULL)
-    {
-      dbus_set_error (error, 
-                     DBUS_ERROR_NAME_HAS_NO_OWNER,
-                     "Could not get PID of name '%s': no such name", service);
-      goto failed;
-    }
+         if (conn == NULL)
+               goto failed;
 
-  conn = bus_service_get_primary_owners_connection (serv);
 
-  reply = dbus_message_new_method_return (message);
-  if (reply == NULL)
-    goto oom;
 
-  if (!dbus_connection_get_unix_process_id (conn, &pid))
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
-                      "Could not determine PID for '%s'", service);
-      goto failed;
-    }
+         if (!dbus_connection_get_unix_process_id (conn, &pid))
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
+                                                 "Could not determine PID for '%s'", service);
+                 goto failed;
+               }
+  }
 
   pid32 = pid;
   if (! dbus_message_append_args (reply,
@@ -1278,40 +1577,21 @@ bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
                                              DBusMessage    *message,
                                              DBusError      *error)
 {
-  const char *service;
-  DBusString str;
-  BusRegistry *registry;
-  BusService *serv;
   DBusConnection *conn;
   DBusMessage *reply;
-  char *data = NULL;
+  void *data = NULL;
   dbus_uint32_t data_size;
+  const char *service;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  registry = bus_connection_get_registry (connection);
-
-  service = NULL;
   reply = NULL;
 
-  if (! dbus_message_get_args (message, error,
-                              DBUS_TYPE_STRING, &service,
-                              DBUS_TYPE_INVALID))
-      goto failed;
-
-  _dbus_verbose ("asked for audit session data for connection %s\n", service);
+  conn = bus_driver_get_conn_helper (connection, message,
+                                     "audit session data", &service, error);
 
-  _dbus_string_init_const (&str, service);
-  serv = bus_registry_lookup (registry, &str);
-  if (serv == NULL)
-    {
-      dbus_set_error (error, 
-                     DBUS_ERROR_NAME_HAS_NO_OWNER,
-                     "Could not get audit session data for name '%s': no such name", service);
-      goto failed;
-    }
-
-  conn = bus_service_get_primary_owners_connection (serv);
+  if (conn == NULL)
+    goto failed;
 
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
@@ -1353,60 +1633,141 @@ bus_driver_handle_get_connection_selinux_security_context (DBusConnection *conne
                                                           DBusMessage    *message,
                                                           DBusError      *error)
 {
-  const char *service;
-  DBusString str;
-  BusRegistry *registry;
-  BusService *serv;
   DBusConnection *conn;
   DBusMessage *reply;
   BusSELinuxID *context;
+  const char *service;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  registry = bus_connection_get_registry (connection);
+  reply = dbus_message_new_method_return (message);
+  if (reply == NULL)
+    goto oom;
 
-  service = NULL;
-  reply = NULL;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+         if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
+                 goto failed;
+  }
+  else
+#endif
+  {
+         conn = bus_driver_get_conn_helper (connection, message, "security context",
+                                                                                &service, error);
+
+         if (conn == NULL)
+               goto failed;
+
+         context = bus_connection_get_selinux_id (conn);
+         if (!context)
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
+                                                 "Could not determine security context for '%s'", service);
+                 goto failed;
+               }
+
+         if (! bus_selinux_append_context (reply, context, error))
+               goto failed;
+  }
 
-  if (! dbus_message_get_args (message, error,
-                              DBUS_TYPE_STRING, &service,
-                              DBUS_TYPE_INVALID))
-      goto failed;
+  if (! bus_transaction_send_from_driver (transaction, connection, reply))
+    goto oom;
 
-  _dbus_verbose ("asked for security context of connection %s\n", service);
+  dbus_message_unref (reply);
 
-  _dbus_string_init_const (&str, service);
-  serv = bus_registry_lookup (registry, &str);
-  if (serv == NULL)
-    {
-      dbus_set_error (error, 
-                     DBUS_ERROR_NAME_HAS_NO_OWNER,
-                     "Could not get security context of name '%s': no such name", service);
-      goto failed;
-    }
+  return TRUE;
 
-  conn = bus_service_get_primary_owners_connection (serv);
+ oom:
+  BUS_SET_OOM (error);
 
-  reply = dbus_message_new_method_return (message);
+ failed:
+  _DBUS_ASSERT_ERROR_IS_SET (error);
+  if (reply)
+    dbus_message_unref (reply);
+  return FALSE;
+}
+
+static dbus_bool_t
+bus_driver_handle_get_connection_credentials (DBusConnection *connection,
+                                              BusTransaction *transaction,
+                                              DBusMessage    *message,
+                                              DBusError      *error)
+{
+  DBusConnection *conn;
+  DBusMessage *reply;
+  DBusMessageIter reply_iter;
+  DBusMessageIter array_iter;
+  unsigned long ulong_val;
+  const char *service;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
+  reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
   if (reply == NULL)
     goto oom;
 
-  context = bus_connection_get_selinux_id (conn);
-  if (!context)
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
-                      "Could not determine security context for '%s'", service);
-      goto failed;
-    }
-
-  if (! bus_selinux_append_context (reply, context, error))
-    goto failed;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+  {
+#endif
+         conn = bus_driver_get_conn_helper (connection, message, "credentials",
+                                                                                &service, error);
+
+         if (conn == NULL)
+               goto failed;
+
+         /* we can't represent > 32-bit pids; if your system needs them, please
+          * add ProcessID64 to the spec or something */
+         if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
+                 ulong_val <= _DBUS_UINT32_MAX)
+               {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
+                       goto oom;
+               }
+
+         /* we can't represent > 32-bit uids; if your system needs them, please
+          * add UnixUserID64 to the spec or something */
+         if (dbus_connection_get_unix_user (conn, &ulong_val) &&
+                 ulong_val <= _DBUS_UINT32_MAX)
+               {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
+                       goto oom;
+               }
+#ifdef ENABLE_KDBUS_TRANSPORT
+  }
+  else
+  {
+         if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
+         {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
+                       goto oom;
+         }
+         else
+                 goto failed;
+
+         if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
+         {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
+                       goto oom;
+         }
+         else
+                 goto failed;
+  }
+#endif
 
-  if (! bus_transaction_send_from_driver (transaction, connection, reply))
+  if (!_dbus_asv_close (&reply_iter, &array_iter))
     goto oom;
 
-  dbus_message_unref (reply);
+  if (! bus_transaction_send_from_driver (transaction, connection, reply))
+    {
+      /* this time we don't want to close the iterator again, so just
+       * get rid of the message */
+      dbus_message_unref (reply);
+      reply = NULL;
+      goto oom;
+    }
 
   return TRUE;
 
@@ -1415,8 +1776,13 @@ bus_driver_handle_get_connection_selinux_security_context (DBusConnection *conne
 
  failed:
   _DBUS_ASSERT_ERROR_IS_SET (error);
+
   if (reply)
-    dbus_message_unref (reply);
+    {
+      _dbus_asv_abandon (&reply_iter, &array_iter);
+      dbus_message_unref (reply);
+    }
+
   return FALSE;
 }
 
@@ -1432,7 +1798,7 @@ bus_driver_handle_reload_config (DBusConnection *connection,
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   reply = NULL;
-  
+
   context = bus_connection_get_context (connection);
   if (!bus_context_reload_config (context, error))
     goto failed;
@@ -1477,7 +1843,7 @@ bus_driver_handle_get_id (DBusConnection *connection,
     }
 
   reply = NULL;
-  
+
   context = bus_connection_get_context (connection);
   if (!bus_context_get_id (context, &uuid))
     goto oom;
@@ -1491,32 +1857,28 @@ bus_driver_handle_get_id (DBusConnection *connection,
                                  DBUS_TYPE_STRING, &v_STRING,
                                  DBUS_TYPE_INVALID))
     goto oom;
-  
+
   _dbus_assert (dbus_message_has_signature (reply, "s"));
-  
+
   if (! bus_transaction_send_from_driver (transaction, connection, reply))
     goto oom;
 
-  _dbus_string_free (&uuid);  
+  _dbus_string_free (&uuid);
   dbus_message_unref (reply);
   return TRUE;
 
  oom:
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
+
   BUS_SET_OOM (error);
 
   if (reply)
     dbus_message_unref (reply);
-  _dbus_string_free (&uuid);  
+  _dbus_string_free (&uuid);
   return FALSE;
 }
 
-/* For speed it might be useful to sort this in order of
- * frequency of use (but doesn't matter with only a few items
- * anyhow)
- */
-struct
+typedef struct
 {
   const char *name;
   const char *in_args;
@@ -1525,11 +1887,17 @@ struct
                            BusTransaction *transaction,
                            DBusMessage    *message,
                            DBusError      *error);
-} message_handlers[] = {
+} MessageHandler;
+
+/* For speed it might be useful to sort this in order of
+ * frequency of use (but doesn't matter with only a few items
+ * anyhow)
+ */
+static const MessageHandler dbus_message_handlers[] = {
   { "Hello",
     "",
     DBUS_TYPE_STRING_AS_STRING,
-    bus_driver_handle_hello },  
+    bus_driver_handle_hello },
   { "RequestName",
     DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
     DBUS_TYPE_UINT32_AS_STRING,
@@ -1542,6 +1910,10 @@ struct
     DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
     DBUS_TYPE_UINT32_AS_STRING,
     bus_driver_handle_activate_service },
+  { "UpdateActivationEnvironment",
+    DBUS_TYPE_ARRAY_AS_STRING DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
+    "",
+    bus_driver_handle_update_activation_environment },
   { "NameHasOwner",
     DBUS_TYPE_STRING_AS_STRING,
     DBUS_TYPE_BOOLEAN_AS_STRING,
@@ -1593,7 +1965,54 @@ struct
   { "GetId",
     "",
     DBUS_TYPE_STRING_AS_STRING,
-    bus_driver_handle_get_id }
+    bus_driver_handle_get_id },
+  { "GetConnectionCredentials", "s", "a{sv}",
+    bus_driver_handle_get_connection_credentials },
+  { NULL, NULL, NULL, NULL }
+};
+
+static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
+    BusTransaction *, DBusMessage *, DBusError *);
+
+static const MessageHandler introspectable_message_handlers[] = {
+  { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
+  { NULL, NULL, NULL, NULL }
+};
+
+#ifdef DBUS_ENABLE_STATS
+static const MessageHandler stats_message_handlers[] = {
+  { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
+  { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
+  { NULL, NULL, NULL, NULL }
+};
+#endif
+
+typedef struct {
+  const char *name;
+  const MessageHandler *message_handlers;
+  const char *extra_introspection;
+} InterfaceHandler;
+
+/* These should ideally be sorted by frequency of use, although it
+ * probably doesn't matter with this few items */
+static InterfaceHandler interface_handlers[] = {
+  { DBUS_INTERFACE_DBUS, dbus_message_handlers,
+    "    <signal name=\"NameOwnerChanged\">\n"
+    "      <arg type=\"s\"/>\n"
+    "      <arg type=\"s\"/>\n"
+    "      <arg type=\"s\"/>\n"
+    "    </signal>\n"
+    "    <signal name=\"NameLost\">\n"
+    "      <arg type=\"s\"/>\n"
+    "    </signal>\n"
+    "    <signal name=\"NameAcquired\">\n"
+    "      <arg type=\"s\"/>\n"
+    "    </signal>\n" },
+  { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
+#ifdef DBUS_ENABLE_STATS
+  { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
+#endif
+  { NULL, NULL, NULL }
 };
 
 static dbus_bool_t
@@ -1604,10 +2023,10 @@ write_args_for_direction (DBusString *xml,
   DBusTypeReader typereader;
   DBusString sigstr;
   int current_type;
-  
+
   _dbus_string_init_const (&sigstr, signature);
   _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
-      
+
   while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
     {
       const DBusString *subsig;
@@ -1634,87 +2053,44 @@ write_args_for_direction (DBusString *xml,
 dbus_bool_t
 bus_driver_generate_introspect_string (DBusString *xml)
 {
-  int i;
+  const InterfaceHandler *ih;
+  const MessageHandler *mh;
 
   if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
     return FALSE;
   if (!_dbus_string_append (xml, "<node>\n"))
     return FALSE;
-  if (!_dbus_string_append_printf (xml, "  <interface name=\"%s\">\n", DBUS_INTERFACE_INTROSPECTABLE))
-    return FALSE;
-  if (!_dbus_string_append (xml, "    <method name=\"Introspect\">\n"))
-    return FALSE;
-  if (!_dbus_string_append_printf (xml, "      <arg name=\"data\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING))
-    return FALSE;
-  if (!_dbus_string_append (xml, "    </method>\n"))
-    return FALSE;
-  if (!_dbus_string_append (xml, "  </interface>\n"))
-    return FALSE;
 
-  if (!_dbus_string_append_printf (xml, "  <interface name=\"%s\">\n",
-                                   DBUS_INTERFACE_DBUS))
-    return FALSE;
-
-  i = 0;
-  while (i < _DBUS_N_ELEMENTS (message_handlers))
+  for (ih = interface_handlers; ih->name != NULL; ih++)
     {
-         
-      if (!_dbus_string_append_printf (xml, "    <method name=\"%s\">\n",
-                                       message_handlers[i].name))
+      if (!_dbus_string_append_printf (xml, "  <interface name=\"%s\">\n",
+                                       ih->name))
         return FALSE;
 
-      if (!write_args_for_direction (xml, message_handlers[i].in_args, TRUE))
-       return FALSE;
-
-      if (!write_args_for_direction (xml, message_handlers[i].out_args, FALSE))
-       return FALSE;
-
-      if (!_dbus_string_append (xml, "    </method>\n"))
-       return FALSE;
-      
-      ++i;
-    }
-
-  if (!_dbus_string_append_printf (xml, "    <signal name=\"NameOwnerChanged\">\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "      <arg type=\"s\"/>\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "      <arg type=\"s\"/>\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "      <arg type=\"s\"/>\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "    </signal>\n"))
-    return FALSE;
-
+      for (mh = ih->message_handlers; mh->name != NULL; mh++)
+        {
+          if (!_dbus_string_append_printf (xml, "    <method name=\"%s\">\n",
+                                           mh->name))
+            return FALSE;
 
+          if (!write_args_for_direction (xml, mh->in_args, TRUE))
+            return FALSE;
 
-  if (!_dbus_string_append_printf (xml, "    <signal name=\"NameLost\">\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "      <arg type=\"s\"/>\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "    </signal>\n"))
-    return FALSE;
+          if (!write_args_for_direction (xml, mh->out_args, FALSE))
+            return FALSE;
 
+          if (!_dbus_string_append (xml, "    </method>\n"))
+            return FALSE;
+        }
 
+      if (ih->extra_introspection != NULL &&
+          !_dbus_string_append (xml, ih->extra_introspection))
+        return FALSE;
 
-  if (!_dbus_string_append_printf (xml, "    <signal name=\"NameAcquired\">\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "      <arg type=\"s\"/>\n"))
-    return FALSE;
-  
-  if (!_dbus_string_append_printf (xml, "    </signal>\n"))
-    return FALSE;
+      if (!_dbus_string_append (xml, "  </interface>\n"))
+        return FALSE;
+    }
 
-  if (!_dbus_string_append (xml, "  </interface>\n"))
-    return FALSE;
-  
   if (!_dbus_string_append (xml, "</node>\n"))
     return FALSE;
 
@@ -1732,7 +2108,7 @@ bus_driver_handle_introspect (DBusConnection *connection,
   const char *v_STRING;
 
   _dbus_verbose ("Introspect() on bus driver\n");
-  
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   reply = NULL;
@@ -1779,7 +2155,7 @@ bus_driver_handle_introspect (DBusConnection *connection,
     dbus_message_unref (reply);
 
   _dbus_string_free (&xml);
-  
+
   return FALSE;
 }
 
@@ -1789,68 +2165,70 @@ bus_driver_handle_message (DBusConnection *connection,
                           DBusMessage    *message,
                            DBusError      *error)
 {
-  const char *name, *sender, *interface;
-  int i;
+  const char *name, *interface;
+  const InterfaceHandler *ih;
+  const MessageHandler *mh;
+  dbus_bool_t found_interface = FALSE;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
+  if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
+    {
+      BusContext *context;
+
+      context = bus_connection_get_context (connection);
+      return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
+    }
+
   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
     {
       _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
       return TRUE; /* we just ignore this */
     }
 
-  if (dbus_message_is_method_call (message,
-                                   DBUS_INTERFACE_INTROSPECTABLE,
-                                   "Introspect"))
-    return bus_driver_handle_introspect (connection, transaction, message, error);
-  
+  /* may be NULL, which means "any interface will do" */
   interface = dbus_message_get_interface (message);
-  if (interface == NULL)
-    interface = DBUS_INTERFACE_DBUS;
-  
+
   _dbus_assert (dbus_message_get_member (message) != NULL);
-  
+
   name = dbus_message_get_member (message);
-  sender = dbus_message_get_sender (message);
-  
-  if (strcmp (interface,
-              DBUS_INTERFACE_DBUS) != 0)
-    {
-      _dbus_verbose ("Driver got message to unknown interface \"%s\"\n",
-                     interface);
-      goto unknown;
-    }
-  
-  _dbus_verbose ("Driver got a method call: %s\n",
-                dbus_message_get_member (message));
-  
+
+  _dbus_verbose ("Driver got a method call: %s\n", name);
+
   /* security checks should have kept this from getting here */
-  _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
-  
-  i = 0;
-  while (i < _DBUS_N_ELEMENTS (message_handlers))
+  _dbus_assert (dbus_message_get_sender (message) != NULL ||
+                strcmp (name, "Hello") == 0);
+
+  for (ih = interface_handlers; ih->name != NULL; ih++)
     {
-      if (strcmp (message_handlers[i].name, name) == 0)
+      if (interface != NULL && strcmp (interface, ih->name) != 0)
+        continue;
+
+      found_interface = TRUE;
+
+      for (mh = ih->message_handlers; mh->name != NULL; mh++)
         {
+          if (strcmp (mh->name, name) != 0)
+            continue;
+
           _dbus_verbose ("Found driver handler for %s\n", name);
 
-          if (!dbus_message_has_signature (message, message_handlers[i].in_args))
+          if (!dbus_message_has_signature (message, mh->in_args))
             {
               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
               _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
                              name, dbus_message_get_signature (message),
-                             message_handlers[i].in_args);
-              
+                             mh->in_args);
+
               dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                               "Call to %s has wrong args (%s, expected %s)\n",
                               name, dbus_message_get_signature (message),
-                              message_handlers[i].in_args);
+                              mh->in_args);
               _DBUS_ASSERT_ERROR_IS_SET (error);
               return FALSE;
             }
-          
-          if ((* message_handlers[i].handler) (connection, transaction, message, error))
+
+          if ((* mh->handler) (connection, transaction, message, error))
             {
               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
               _dbus_verbose ("Driver handler succeeded\n");
@@ -1863,18 +2241,15 @@ bus_driver_handle_message (DBusConnection *connection,
               return FALSE;
             }
         }
-      
-      ++i;
     }
 
- unknown:
   _dbus_verbose ("No driver handler for message \"%s\"\n",
                  name);
 
-  dbus_set_error (error, DBUS_ERROR_UNKNOWN_METHOD,
+  dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
                   "%s does not understand message %s",
                   DBUS_SERVICE_DBUS, name);
-  
+
   return FALSE;
 }