[lib-fix] moved dbus_connection_get_unix_user from daemon to library
[platform/upstream/dbus.git] / bus / driver.c
index 425a3d5..0b6e7f0 100644 (file)
@@ -3,6 +3,7 @@
  *
  * 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
  *
 #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>
+#include <stdlib.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,
@@ -53,6 +107,11 @@ 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 = "";
@@ -102,6 +161,11 @@ 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,
@@ -145,6 +209,11 @@ 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,
@@ -392,12 +461,26 @@ 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);
 
@@ -411,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,
@@ -582,13 +669,27 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
   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
+  {
 
-  if (!bus_registry_acquire_service (registry, connection,
-                                     &service_name, flags,
-                                     &service_reply, transaction,
-                                     error))
-    goto out;
+         _dbus_string_init_const (&service_name, name);
+
+         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)
@@ -632,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))
@@ -645,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))
@@ -709,9 +819,29 @@ 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) || (inter_ret == -ENXIO))
+                               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);
@@ -826,7 +956,6 @@ bus_driver_handle_update_activation_environment (DBusConnection *connection,
   DBusMessageIter iter;
   DBusMessageIter dict_iter;
   DBusMessageIter dict_entry_iter;
-  int msg_type;
   int array_type;
   int key_type;
   DBusList *keys, *key_link;
@@ -841,9 +970,7 @@ bus_driver_handle_update_activation_environment (DBusConnection *connection,
   /* The message signature has already been checked for us,
    * so let's just assert it's right.
    */
-  msg_type = dbus_message_iter_get_arg_type (&iter);
-
-  _dbus_assert (msg_type == DBUS_TYPE_ARRAY);
+  _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
 
   dbus_message_iter_recurse (&iter, &dict_iter);
 
@@ -984,20 +1111,36 @@ 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))
-    {
-      bus_matchmaker_remove_rule (matchmaker, rule);
-      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;
+           }
+  }
 
   bus_match_rule_unref (rule);
 
@@ -1040,6 +1183,14 @@ 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.
    */
@@ -1047,10 +1198,15 @@ bus_driver_handle_remove_match (DBusConnection *connection,
                        message, error))
     goto failed;
 
-  matchmaker = bus_connection_get_matchmaker (connection);
+#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;
+         if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
+               goto failed;
+  }
 
   bus_match_rule_unref (rule);
 
@@ -1075,6 +1231,9 @@ bus_driver_handle_get_service_owner (DBusConnection *connection,
   BusRegistry *registry;
   BusService *service;
   DBusMessage *reply;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
+#endif
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -1088,35 +1247,59 @@ 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))
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
     {
-      /* 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)
+      int ret;
+
+      ret = kdbus_get_name_owner(connection, text, unique_name);
+      if(ret == 0)
+        base_name = unique_name;
+      else if((ret == -ENOENT) || (ret == -ENXIO)) //name has no owner
         {
-          /* FIXME - how is this error possible? */
-          dbus_set_error (error,
-                          DBUS_ERROR_FAILED,
-                          "Could not determine unique name for '%s'", text);
+          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_set_error (error, DBUS_ERROR_FAILED,
+                  "Could not determine unique name for '%s'", text);
           goto failed;
         }
-      _dbus_assert (*base_name == ':');
     }
-
+  else
+#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);
@@ -1183,7 +1366,11 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
       if (! _dbus_list_append (&base_names, dbus_service_name))
         goto oom;
     }
+#ifdef ENABLE_KDBUS_TRANSPORT
+  else if (!bus_context_is_kdbus(bus_transaction_get_context (transaction)) && (service == NULL))
+#else
   else if (service == NULL)
+#endif
     {
       dbus_set_error (error,
                       DBUS_ERROR_NAME_HAS_NO_OWNER,
@@ -1192,10 +1379,20 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
     }
   else
     {
-      if (!bus_service_list_queued_owners (service,
-                                           &base_names,
-                                           error))
-        goto failed;
+#ifdef ENABLE_KDBUS_TRANSPORT
+      if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+        {
+          if(!kdbus_list_queued (connection,  &base_names, text ,error))
+            goto failed;
+        }
+      else
+#endif
+        {
+          if (!bus_service_list_queued_owners (service,
+              &base_names,
+              error))
+            goto failed;
+        }
     }
 
   _dbus_assert (base_names != NULL);
@@ -1236,6 +1433,23 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
 
   dbus_message_unref (reply);
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+    {
+      link = _dbus_list_get_first_link (&base_names);
+      while (link != NULL)
+        {
+          DBusList *next = _dbus_list_get_next_link (&base_names, link);
+
+          if(link->data != NULL)
+            free(link->data);
+
+          _dbus_list_free_link (link);
+          link = next;
+        }
+    }
+#endif
+
   return TRUE;
 
  oom:
@@ -1245,10 +1459,8 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
   _DBUS_ASSERT_ERROR_IS_SET (error);
   if (reply)
     dbus_message_unref (reply);
-
   if (base_names)
     _dbus_list_clear (&base_names);
-
   return FALSE;
 }
 
@@ -1258,51 +1470,44 @@ 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))
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if (bus_context_is_kdbus (bus_transaction_get_context (transaction)))
     {
-      dbus_set_error (error,
-                      DBUS_ERROR_FAILED,
-                      "Could not determine UID for '%s'", service);
-      goto failed;
+      const char* name;
+
+      if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+        goto failed;
+      if (!kdbus_connection_get_unix_user (connection, name, &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;
@@ -1334,51 +1539,42 @@ 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;
-
-  _dbus_verbose ("asked for PID 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 PID 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;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if (bus_context_is_kdbus (bus_transaction_get_context (transaction)))
+    {
+      const char* name;
 
-  if (!dbus_connection_get_unix_process_id (conn, &pid))
+      if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+        goto failed;
+      if (!kdbus_connection_get_unix_process_id (connection, name, &pid, error))
+        goto failed;
+    }
+  else
+#endif
     {
-      dbus_set_error (error,
-                      DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
-                      "Could not determine PID for '%s'", service);
-      goto failed;
+      conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
+                                         error);
+      if (conn == NULL)
+        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;
@@ -1410,40 +1606,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;
   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);
-
-  _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_driver_get_conn_helper (connection, message,
+                                     "audit session data", &service, error);
 
-  conn = bus_service_get_primary_owners_connection (serv);
+  if (conn == NULL)
+    goto failed;
 
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
@@ -1485,60 +1662,145 @@ 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)
+#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
     {
-      dbus_set_error (error,
-                      DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
-                      "Could not determine security context for '%s'", service);
-      goto failed;
-    }
+      const char* name;
 
-  if (! bus_selinux_append_context (reply, context, error))
-    goto failed;
+      if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+        goto failed;
+      if (kdbus_connection_get_unix_process_id (connection, name, &ulong_val, error))
+        {
+          if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
+            goto oom;
+        }
+      else
+        goto failed;
 
-  if (! bus_transaction_send_from_driver (transaction, connection, reply))
+      if (kdbus_connection_get_unix_user (connection, name, &ulong_val, error))
+        {
+          if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
+            goto oom;
+        }
+      else
+        goto failed;
+   }
+#endif
+
+  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;
 
@@ -1547,8 +1809,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;
 }
 
@@ -1732,6 +1999,8 @@ static const MessageHandler dbus_message_handlers[] = {
     "",
     DBUS_TYPE_STRING_AS_STRING,
     bus_driver_handle_get_id },
+  { "GetConnectionCredentials", "s", "a{sv}",
+    bus_driver_handle_get_connection_credentials },
   { NULL, NULL, NULL, NULL }
 };
 
@@ -1743,6 +2012,14 @@ static const MessageHandler introspectable_message_handlers[] = {
   { 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;
@@ -1765,6 +2042,9 @@ static InterfaceHandler interface_handlers[] = {
     "      <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 }
 };
 
@@ -1918,7 +2198,7 @@ bus_driver_handle_message (DBusConnection *connection,
                           DBusMessage    *message,
                            DBusError      *error)
 {
-  const char *name, *sender, *interface;
+  const char *name, *interface;
   const InterfaceHandler *ih;
   const MessageHandler *mh;
   dbus_bool_t found_interface = FALSE;
@@ -1945,13 +2225,12 @@ bus_driver_handle_message (DBusConnection *connection,
   _dbus_assert (dbus_message_get_member (message) != NULL);
 
   name = dbus_message_get_member (message);
-  sender = dbus_message_get_sender (message);
 
-  _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);
+  _dbus_assert (dbus_message_get_sender (message) != NULL ||
+                strcmp (name, "Hello") == 0);
 
   for (ih = interface_handlers; ih->name != NULL; ih++)
     {
@@ -1997,7 +2276,6 @@ bus_driver_handle_message (DBusConnection *connection,
         }
     }
 
- unknown:
   _dbus_verbose ("No driver handler for message \"%s\"\n",
                  name);