[daemon-dev][daemon-fix] starting services by direct message (autostart) and some...
[platform/upstream/dbus.git] / dbus / dbus-transport-kdbus.c
index 0dd011a..6a33241 100644 (file)
 
 #include "dbus-transport.h"
 #include "dbus-transport-kdbus.h"
-#include <dbus/dbus-transport-protected.h>
+//#include <dbus/dbus-transport-protected.h>
+#include "dbus-transport-protected.h"
 #include "dbus-connection-internal.h"
 #include "kdbus.h"
 #include "dbus-watch.h"
 #include "dbus-errors.h"
 #include "dbus-bus.h"
+#include "kdbus-common.h"
 #include <linux/types.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <openssl/md5.h>
 
-#define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
-#define KDBUS_PART_HEADER_SIZE offsetof(struct kdbus_item, data)
-#define KDBUS_ITEM_SIZE(s) KDBUS_ALIGN8((s) + KDBUS_PART_HEADER_SIZE)
-
-#define KDBUS_PART_NEXT(part) \
-       (typeof(part))(((uint8_t *)part) + KDBUS_ALIGN8((part)->size))
 #define KDBUS_PART_FOREACH(part, head, first)                          \
        for (part = (head)->first;                                      \
             (uint8_t *)(part) < (uint8_t *)(head) + (head)->size;      \
@@ -45,6 +41,7 @@
 #define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used
 
 #define KDBUS_MSG_DECODE_DEBUG 0
+//#define DBUS_AUTHENTICATION
 
 #define ITER_APPEND_STR(string) \
 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))   \
@@ -96,6 +93,7 @@ struct DBusTransportSocket
                                          *   Kdbus kernel module. 
                                          */
   __u64 bloom_size;                                            /**<  bloom filter field size */
+  char* sender;                         /**< uniqe name of the sender */
 };
 
 static dbus_bool_t
@@ -129,7 +127,7 @@ static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection*
        return TRUE;
 }
 
-static dbus_bool_t reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection)
+static int reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection)
 {
        DBusMessage *errMessage;
        char* error_msg = "";
@@ -144,34 +142,47 @@ static dbus_bool_t reply_with_error(char* error_type, const char* template, cons
 
        errMessage = generate_local_error_message(dbus_message_get_serial(message), error_type, error_msg);
        if(errMessage == NULL)
-               return FALSE;
+               return -1;
        if (add_message_to_received(errMessage, connection))
-               return TRUE;
+               return 0;
 
-       return FALSE;
+       return -1;
 }
 
-static dbus_bool_t reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection)
+static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection)
 {
        DBusMessageIter args;
        DBusMessage *reply;
 
        reply = dbus_message_new_method_return(message);
        if(reply == NULL)
-               return FALSE;
+               return -1;
        dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
     dbus_message_iter_init_append(reply, &args);
     if (!dbus_message_iter_append_basic(&args, data_type, pData))
     {
        dbus_message_unref(reply);
-        return FALSE;
+        return -1;
     }
     if(add_message_to_received(reply, connection))
-       return TRUE;
+       return 0;
 
-    return FALSE;
+    return -1;
 }
 
+/*
+static int reply_ack(DBusMessage *message, DBusConnection* connection)
+{
+       DBusMessage *reply;
+
+       reply = dbus_message_new_method_return(message);
+       if(reply == NULL)
+               return -1;
+    if(add_message_to_received(reply, connection))
+       return 0;
+    return -1;
+}*/
+
 /**
  * Retrieves file descriptor to memory pool from kdbus module.
  * It is then used for bulk data sending.
@@ -364,6 +375,7 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
         if(body_size)
         {
             _dbus_verbose("body attaching\n");
+           item = KDBUS_PART_NEXT(item);
            MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(body), body_size);
         }
     }
@@ -381,7 +393,7 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
                item = KDBUS_PART_NEXT(item);
                item->type = KDBUS_MSG_DST_NAME;
                item->size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
-               strcpy(item->str, name);
+               memcpy(item->str, name, item->size - KDBUS_PART_HEADER_SIZE);
        }
        else if (dst_id == KDBUS_DST_ID_BROADCAST)
        {
@@ -396,18 +408,21 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
        {
                if(errno == EINTR)
                        goto again;
-               if((errno == ESRCH) || (errno == ENXIO) || (errno = EADDRNOTAVAIL))  //when recipient is not available on the bus
+               else if(errno == ENXIO) //no such id on the bus
+               {
+            if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
+                goto out;
+               }
+        else if((errno == ESRCH) || (errno = EADDRNOTAVAIL))  //when well known name is not available on the bus
                {
                        if(autostart)
                        {
-                               //todo start service here, otherwise
-                               if(reply_with_error(DBUS_ERROR_SERVICE_UNKNOWN, "The name %s was not provided by any .service files", dbus_message_get_destination(message), message, transport->base.connection))
+                               if(!reply_with_error(DBUS_ERROR_SERVICE_UNKNOWN, "The name %s was not provided by any .service files", dbus_message_get_destination(message), message, transport->base.connection))
                                        goto out;
                        }
                        else
-                               if(reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
-                                       goto out;
-
+                   if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
+                       goto out;
                }
                _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
                ret_size = -1;
@@ -419,15 +434,6 @@ out:
     return ret_size;
 }
 
-struct nameInfo
-{
-       __u64 uniqueId;
-       __u64 userId;
-       __u64 processId;
-       __u32 sec_label_len;
-       char *sec_label;
-};
-
 /**
  * Performs kdbus query of id of the given name
  *
@@ -436,7 +442,7 @@ struct nameInfo
  * @param ownerID place to store id of the name
  * @return 0 on success, -errno if failed
  */
-static int kdbus_NameQuery(char* name, int fd, struct nameInfo* pInfo)
+int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
 {
        struct kdbus_cmd_name_info *msg;
        struct kdbus_item *item;
@@ -468,14 +474,16 @@ static int kdbus_NameQuery(char* name, int fd, struct nameInfo* pInfo)
        item = msg->items;
        item->type = KDBUS_NAME_INFO_ITEM_NAME;
        item->size = item_size;
-       strcpy(item->str, name);
+       memcpy(item->str, name, strlen(name) + 1);
 
        again:
-       ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg);
+       ret = ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_NAME_QUERY, msg);
        if (ret < 0)
        {
                if(errno == EINTR)
                        goto again;
+               if(errno == EAGAIN)
+                               goto again;
                else if(ret == -ENOBUFS)
                {
                        msg = realloc(msg, msg->size);  //prepare memory
@@ -490,20 +498,20 @@ static int kdbus_NameQuery(char* name, int fd, struct nameInfo* pInfo)
                pInfo->uniqueId = msg->id;
                pInfo->userId = msg->creds.uid;
                pInfo->processId = msg->creds.pid;
-_dbus_verbose ("I'm alive 1\n");
                item = msg->items;
                while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
                {
                        if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
                        {
-                               pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
+                           pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
                                if(pInfo->sec_label_len != 0)
+                               {
                                        pInfo->sec_label = malloc(pInfo->sec_label_len);
-                               if(pInfo->sec_label == NULL)
-                                       ret = -1;
-                               else
-                                       memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
-                                       
+                                       if(pInfo->sec_label == NULL)
+                                               ret = -1;
+                                       else
+                                               memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
+                               }
                                break;
                        }
                        item = KDBUS_PART_NEXT(item);
@@ -515,181 +523,523 @@ _dbus_verbose ("I'm alive 1\n");
 }
 
 /**
- * Handles messages sent to bus daemon - "org.freedesktop.DBus" and translates them to appropriate
- * kdbus ioctl commands. Than translate kdbus reply into dbus message and put it into recived messages queue.
- *
- * !!! Not all methods are handled !!! Doubt if it is even possible.
- * If method is not handled, returns error reply org.freedesktop.DBus.Error.UnknownMethod
- *
- * Handled methods:
- * - GetNameOwner
- * - NameHasOwner
- * - ListNames
+ * Kdbus part of dbus_bus_register.
+ * Shouldn't be used separately because it needs to be surrounded
+ * by other functions as it is done in dbus_bus_register.
  *
- * Not handled methods:
- * - ListActivatableNames
- * - StartServiceByName
- * - UpdateActivationEnvironment
- * - GetConnectionUnixUser
- * - GetId
+ * @param name place to store unique name given by bus
+ * @param connection the connection
+ * @param error place to store errors
+ * @returns #TRUE on success
  */
-static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
+static dbus_bool_t bus_register_kdbus(char* name, DBusTransportSocket* transportS)
 {
-       int inter_ret;
-       struct nameInfo info;
-       dbus_bool_t ret_value;
-
-       if(!strcmp(dbus_message_get_member(message), "GetNameOwner"))  //returns id of the well known name
-       {
-               char* name = NULL;
+       struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
 
-               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
-               if(inter_ret == 0) //unique id of the name
-               {
-                       char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
-                       const char* pString = unique_name;
+       hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
+                          KDBUS_HELLO_ATTACH_COMM |
+                          KDBUS_HELLO_ATTACH_EXE |
+                          KDBUS_HELLO_ATTACH_CMDLINE |
+                          KDBUS_HELLO_ATTACH_CAPS |
+                          KDBUS_HELLO_ATTACH_CGROUP |
+                          KDBUS_HELLO_ATTACH_SECLABEL |
+                          KDBUS_HELLO_ATTACH_AUDIT*/;
+       hello.size = sizeof(struct kdbus_cmd_hello);
+       hello.pool_size = RECEIVE_POOL_SIZE;
 
-                       sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
-                       _dbus_verbose("Unique name discovered:%s\n", unique_name);
-                       ret_value = reply_1_data(message, DBUS_TYPE_STRING, &pString, transport->connection);
-               }
-               else if(inter_ret == -ENOENT)  //name has no owner
-                       return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get owner of name '%s': no such name", name, message, transport->connection);
-               else
-               {
-                       _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
-                       ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine unique name for '%s'", name, message, transport->connection);
-               }
-       }
-       else if(!strcmp(dbus_message_get_member(message), "NameHasOwner"))   //returns if name is currently registered on the bus
+       if (ioctl(transportS->fd, KDBUS_CMD_HELLO, &hello))
        {
-               char* name = NULL;
-               dbus_bool_t result;
-
-               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
-               if((inter_ret == 0) || (inter_ret == -ENOENT))
-               {
-                       result = (inter_ret == 0) ? TRUE : FALSE;
-                       ret_value = reply_1_data(message, DBUS_TYPE_BOOLEAN, &result, transport->connection);
-               }
-               else
-               {
-                       _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
-                       ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name, message, transport->connection);
-               }
+               _dbus_verbose ("Failed to send hello: %m, %d",errno);
+               return FALSE;
        }
-       else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixUser"))
-       {
-               char* name = NULL;
 
-               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
-               if(inter_ret == 0) //name found
-               {
-                       _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
-                       ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.userId, transport->connection);
-               }
-               else if(inter_ret == -ENOENT)  //name has no owner
-                       return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get UID of name '%s': no such name", name, message, transport->connection);
-               else
-               {
-                       _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
-                       ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name, message, transport->connection);
-               }
-       }
-       else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixProcessID"))
-       {
-               char* name = NULL;
+       sprintf(name, "%llu", (unsigned long long)hello.id);
+       _dbus_verbose("-- Our peer ID is: %s\n", name);
+       transportS->bloom_size = hello.bloom_size;
 
-               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
-               if(inter_ret == 0) //name found
-                       ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.processId, transport->connection);
-               else if(inter_ret == -ENOENT)  //name has no owner
-                       return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get PID of name '%s': no such name", name, message, transport->connection);
-               else
-               {
-                       _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
-                       ret_value = reply_with_error(DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,"Could not determine PID for '%s'", name, message, transport->connection);
-               }
-       }
-       else if(!strcmp(dbus_message_get_member(message), "ListNames"))  //return all well known names on he bus
+       transportS->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, transportS->fd, 0);
+       if (transportS->kdbus_mmap_ptr == MAP_FAILED)
        {
-               struct kdbus_cmd_names* pCmd;
-               uint64_t cmd_size;
+               _dbus_verbose("Error when mmap: %m, %d",errno);
+               return FALSE;
+       }
 
-               cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
-               pCmd = malloc(cmd_size);
-               if(pCmd == NULL)
-                       goto out;
-               pCmd->size = cmd_size;
+       return TRUE;
+}
 
-  again:
-               cmd_size = 0;
-               if(ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_NAME_LIST, pCmd))
+/**
+ * Seeks key in rule string, and duplicates value of the key into pValue.
+ * If value is "org.freedesktop.DBus" it is indicated by returning -1, because it
+ * needs to be handled in different manner.
+ * Value is duplicated from rule string to newly allocated memory pointe by pValue,
+ * so it must be freed after use.
+ *
+ * @param rule rule to look through
+ * @param key key to look for
+ * @param pValue pointer to value of the key found
+ * @return length of the value string, 0 means not found, -1 means "org.freedesktop.DBus"
+ */
+static int parse_match_key(const char *rule, const char* key, char** pValue)
+{
+       const char* pBegin;
+       const char* pValueEnd;
+       int value_length = 0;
+
+       pBegin = strstr(rule, key);
+       if(pBegin)
+       {
+               pBegin += strlen(key);
+               pValueEnd = strchr(pBegin, '\'');
+               if(pValueEnd)
                {
-                       if(errno == EINTR)
-                               goto again;
-                       if(errno == ENOBUFS)                    //buffer to small to put all names into it
-                               cmd_size = pCmd->size;          //here kernel tells how much memory it needs
-                       else
+                       value_length = pValueEnd - pBegin;
+                       *pValue = strndup(pBegin, value_length);
+                       if(*pValue)
                        {
-                               _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
-                               goto out;
+                               if(strcmp(*pValue, "org.freedesktop.DBus") == 0)
+                                       value_length = -1;
+                               _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
                        }
                }
-               if(cmd_size)  //kernel needs more memory
-               {
-                       pCmd = realloc(pCmd, cmd_size);  //prepare memory
-                       if(pCmd == NULL)
-                               return FALSE;
-                       goto again;                                             //and try again
-               }
-               else
-               {
-                       DBusMessage *reply;
-                       DBusMessageIter args;
-                       struct kdbus_cmd_name* pCmd_name;
-                       char* pName;
+       }
+       return value_length;
+}
 
-                       reply = dbus_message_new_method_return(message);
-                       if(reply == NULL)
-                               goto out;
-                       dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
-                       dbus_message_iter_init_append(reply, &args);
+/**
+ * Adds a match rule to match broadcast messages going through the message bus.
+ * Do no affect messages addressed directly.
+ *
+ * The "rule" argument is the string form of a match rule.
+ *
+ * In kdbus function do not blocks.
+ *
+ * Upper function returns nothing because of blocking issues
+ * so there is no point to return true or false here.
+ *
+ * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
+ * Current mapping:
+ * interface match key mapped to bloom
+ * sender match key mapped to src_name
+ * also handled org.freedesktop.dbus members: NameOwnerChanged, NameLost, NameAcquired
+ *
+ * @param connection connection to the message bus
+ * @param rule textual form of match rule
+ * @param error location to store any errors - may be NULL
+ */
+dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
+{
+       struct kdbus_cmd_match* pCmd_match;
+       struct kdbus_item *pItem;
+       __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
+       uint64_t size;
+       unsigned int kernel_item = 0;
+       int name_size;
+       char* pName = NULL;
+       char* pInterface = NULL;
+       dbus_bool_t ret_value = FALSE;
+       DBusTransportSocket* transportS = (DBusTransportSocket*)transport;
 
-                       for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
-                       {
-                               pName = pCmd_name->name;
-                               if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pName))
-                               {
-                                       dbus_message_unref(reply);
-                                       goto out;
-                               }
-                       }
+       /*parsing rule and calculating size of command*/
+       size = sizeof(struct kdbus_cmd_match);
 
-                       if(add_message_to_received(reply, transport->connection))
-                       {
-                               free(pCmd);
-                               return TRUE;
-                       }
-               }
-out:
-               if(pCmd)
-                       free(pCmd);
-               return FALSE;
+       if(strstr(rule, "member='NameOwnerChanged'"))
+       {
+               kernel_item = ~0;
+               size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;  /*std DBus: 3 name related items plus 2 id related items*/
        }
-       else if(!strcmp(dbus_message_get_member(message), "GetId"))
+       else if(strstr(rule, "member='NameChanged'"))
        {
-               char* path;
+               kernel_item = KDBUS_MATCH_NAME_CHANGE;
+               size += KDBUS_ITEM_SIZE(1);
+       }
+       else if(strstr(rule, "member='NameLost'"))
+       {
+               kernel_item = KDBUS_MATCH_NAME_REMOVE;
+               size += KDBUS_ITEM_SIZE(1);
+       }
+       else if(strstr(rule, "member='NameAcquired'"))
+       {
+               kernel_item = KDBUS_MATCH_NAME_ADD;
+               size += KDBUS_ITEM_SIZE(1);
+       }
+
+       name_size = parse_match_key(rule, "interface='", &pInterface);
+       if((name_size == -1) && (kernel_item == 0))   //means org.freedesktop.DBus without specified member
+       {
+               kernel_item = ~0;
+               size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;  /* 3 above name related items plus 2 id related items*/
+       }
+       else if(name_size > 0)                  /*actual size is not important for interface because bloom size is defined by bus*/
+               size += KDBUS_PART_HEADER_SIZE + transportS->bloom_size;
+
+       name_size = parse_match_key(rule, "sender='", &pName);
+       if((name_size == -1) && (kernel_item == 0))  //means org.freedesktop.DBus without specified name - same as interface few line above
+       {
+               kernel_item = ~0;
+               size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /* 3 above     name related items plus 2 id related items*/
+       }
+       else if(name_size > 0)
+       {
+               if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
+               {
+                       src_id = strtoull(&pName[3], NULL, 10);
+                       free(pName);
+                       pName = NULL;
+               }
+               else
+                       size += KDBUS_ITEM_SIZE(name_size + 1);  //well known name
+       }
+
+       pCmd_match = alloca(size);
+       if(pCmd_match == NULL)
+               goto out;
+
+       pCmd_match->id = id;
+       pCmd_match->cookie = id;
+       pCmd_match->size = size;
+
+       pItem = pCmd_match->items;
+       if(kernel_item == ~0)  //all signals from kernel
+       {
+               pCmd_match->src_id = 0;
+               pItem->type = KDBUS_MATCH_NAME_CHANGE;
+               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+               pItem = KDBUS_PART_NEXT(pItem);
+               pItem->type = KDBUS_MATCH_NAME_ADD;
+               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+               pItem = KDBUS_PART_NEXT(pItem);
+               pItem->type = KDBUS_MATCH_NAME_REMOVE;
+               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+               pItem = KDBUS_PART_NEXT(pItem);
+               pItem->type = KDBUS_MATCH_ID_ADD;
+               pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
+               pItem = KDBUS_PART_NEXT(pItem);
+               pItem->type = KDBUS_MATCH_ID_REMOVE;
+               pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
+       }
+       else if(kernel_item) //only one item
+       {
+               pCmd_match->src_id = 0;
+               pItem->type = kernel_item;
+               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+       }
+       else
+       {
+               pCmd_match->src_id = src_id;
+               if(pName)
+               {
+                       pItem->type = KDBUS_MATCH_SRC_NAME;
+                       pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
+                       memcpy(pItem->str, pName, strlen(pName) + 1);
+                       pItem = KDBUS_PART_NEXT(pItem);
+               }
+
+               if(pInterface)
+               {
+                       pItem->type = KDBUS_MATCH_BLOOM;
+                       pItem->size = KDBUS_PART_HEADER_SIZE + transportS->bloom_size;
+                       strncpy(pItem->data, pInterface, transportS->bloom_size);
+               }
+       }
+
+       if(ioctl(transportS->fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
+               _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
+       else
+       {
+               _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
+               ret_value = TRUE;
+       }
+
+out:
+       if(pName)
+               free(pName);
+       if(pInterface)
+               free(pInterface);
+       return ret_value;
+}
+
+/**
+ * Opposing to dbus, in kdbus removes all match rules with given
+ * cookie, which now is equal to uniqe id.
+ *
+ * In kdbus this function will not block
+ *
+ * @param connection connection to the message bus
+ * @param error location to store any errors - may be NULL
+ */
+dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
+{
+       struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
+       DBusTransportSocket* transportS = (DBusTransportSocket*) transport;
+
+       cmd.cookie = id;
+       cmd.id = id;
+       cmd.size = sizeof(struct kdbus_cmd_match);
+
+       if(ioctl(transportS->fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
+       {
+               _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
+               return FALSE;
+       }
+       else
+       {
+               _dbus_verbose("Match rule removed correctly.\n");
+               return TRUE;
+       }
+}
+
+/**
+ * Handles messages sent to bus daemon - "org.freedesktop.DBus" and translates them to appropriate
+ * kdbus ioctl commands. Than translate kdbus reply into dbus message and put it into recived messages queue.
+ *
+ * !!! Not all methods are handled !!! Doubt if it is even possible.
+ * If method is not handled, returns error reply org.freedesktop.DBus.Error.UnknownMethod
+ *
+ * Handled methods:
+ * - GetNameOwner
+ * - NameHasOwner
+ * - ListNames
+ *
+ * Not handled methods:
+ * - ListActivatableNames
+ * - StartServiceByName
+ * - UpdateActivationEnvironment
+ * - GetConnectionUnixUser
+ * - GetId
+ */
+static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
+{
+#ifdef DBUS_SERVICES_IN_LIB
+       int inter_ret;
+       struct nameInfo info;
+#endif
+       int ret_value = -1;
+
+       if(!strcmp(dbus_message_get_member(message), "Hello"))
+       {
+               char* sender = NULL;
+               char* name = NULL;
+
+               name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + 1);
+               if(name == NULL)
+                       return -1;
+               if(!bus_register_kdbus(name, (DBusTransportSocket*)transport))
+                       goto outH1;
+               if(!register_kdbus_policy(name, ((DBusTransportSocket*)transport)->fd))
+                       goto outH1;
+
+               sender = malloc (strlen(name) + 4);
+               if(!sender)
+                       goto outH1;
+               sprintf(sender, ":1.%s", name);
+               ((DBusTransportSocket*)transport)->sender = sender;
+
+               if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
+                       return 0;  //todo why we cannot free name after sending reply, shouldn't we?
+               else
+                       free(sender);
+
+       outH1:
+               free(name);
+       }
+#ifdef DBUS_SERVICES_IN_LIB
+       else if(!strcmp(dbus_message_get_member(message), "RequestName"))
+       {
+               char* name;
+               int flags;
+               int result;
+
+               if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
+                       return -1;
+
+               if(!register_kdbus_policy(name, ((DBusTransportSocket*)transport)->fd))
+                       return -1;
+
+               result = request_kdbus_name(((DBusTransportSocket*)transport)->fd, name, flags, 0);
+               if(result == -EPERM)
+                       return reply_with_error(DBUS_ERROR_ACCESS_DENIED,
+                                         "Connection is not allowed to own the service \"%s\" due to security policies in the configuration file",
+                                         name, message, transport->connection);
+               else if(result < 0)
+                       return reply_with_error(DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name, message, transport->connection);
+               else
+                       return reply_1_data(message, DBUS_TYPE_UINT32, &result, transport->connection);
+       }
+       else if(!strcmp(dbus_message_get_member(message), "AddMatch"))
+       {
+               char* rule;
+
+               if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &rule, DBUS_TYPE_INVALID))
+                       return -1;
+
+               if(!add_match_kdbus(transport, 0, rule))
+                       return -1;
+
+               return reply_ack(message,transport->connection);
+       }
+       else if(!strcmp(dbus_message_get_member(message), "RemoveMatch"))
+       {
+               if(!remove_match_kdbus(transport, 0))
+                       return -1;
+               return reply_ack(message, transport->connection);
+       }
+       else if(!strcmp(dbus_message_get_member(message), "GetNameOwner"))  //returns id of the well known name
+       {
+               char* name = NULL;
+
+               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
+               if(inter_ret == 0) //unique id of the name
+               {
+                       char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
+                       const char* pString = unique_name;
+
+                       sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
+                       _dbus_verbose("Unique name discovered:%s\n", unique_name);
+                       ret_value = reply_1_data(message, DBUS_TYPE_STRING, &pString, transport->connection);
+               }
+               else if(inter_ret == -ENOENT)  //name has no owner
+                       return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get owner of name '%s': no such name", name, message, transport->connection);
+               else
+               {
+                       _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
+                       ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine unique name for '%s'", name, message, transport->connection);
+               }
+       }
+       else if(!strcmp(dbus_message_get_member(message), "NameHasOwner"))   //returns if name is currently registered on the bus
+       {
+               char* name = NULL;
+               dbus_bool_t result;
+
+               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
+               if((inter_ret == 0) || (inter_ret == -ENOENT))
+               {
+                       result = (inter_ret == 0) ? TRUE : FALSE;
+                       ret_value = reply_1_data(message, DBUS_TYPE_BOOLEAN, &result, transport->connection);
+               }
+               else
+               {
+                       _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
+                       ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name, message, transport->connection);
+               }
+       }
+       else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixUser"))
+       {
+               char* name = NULL;
+
+               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
+               if(inter_ret == 0) //name found
+               {
+                       _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
+                       ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.userId, transport->connection);
+               }
+               else if(inter_ret == -ENOENT)  //name has no owner
+                       return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get UID of name '%s': no such name", name, message, transport->connection);
+               else
+               {
+                       _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
+                       ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name, message, transport->connection);
+               }
+       }
+       else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixProcessID"))
+       {
+               char* name = NULL;
+
+               dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
+               if(inter_ret == 0) //name found
+                       ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.processId, transport->connection);
+               else if(inter_ret == -ENOENT)  //name has no owner
+                       return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get PID of name '%s': no such name", name, message, transport->connection);
+               else
+               {
+                       _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
+                       ret_value = reply_with_error(DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,"Could not determine PID for '%s'", name, message, transport->connection);
+               }
+       }
+       else if(!strcmp(dbus_message_get_member(message), "ListNames"))  //return all well known names on he bus
+       {
+               struct kdbus_cmd_names* pCmd;
+               uint64_t cmd_size;
+
+               cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
+               pCmd = malloc(cmd_size);
+               if(pCmd == NULL)
+                       goto out;
+               pCmd->size = cmd_size;
+
+  again:
+               cmd_size = 0;
+               if(ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_NAME_LIST, pCmd))
+               {
+                       if(errno == EINTR)
+                               goto again;
+                       if(errno == ENOBUFS)                    //buffer to small to put all names into it
+                               cmd_size = pCmd->size;          //here kernel tells how much memory it needs
+                       else
+                       {
+                               _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
+                               goto out;
+                       }
+               }
+               if(cmd_size)  //kernel needs more memory
+               {
+                       pCmd = realloc(pCmd, cmd_size);  //prepare memory
+                       if(pCmd == NULL)
+                               return -1;
+                       goto again;                                             //and try again
+               }
+               else
+               {
+                       DBusMessage *reply;
+                       DBusMessageIter iter, sub;
+                       struct kdbus_cmd_name* pCmd_name;
+                       char* pName;
+
+                       reply = dbus_message_new_method_return(message);
+                       if(reply == NULL)
+                               goto out;
+                       dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
+                       dbus_message_iter_init_append(reply, &iter);
+                       if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &sub))
+                       {
+                               dbus_message_unref(reply);
+                               goto out;
+                       }
+                       for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
+                       {
+                               pName = pCmd_name->name;
+                               if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &pName))
+                               {
+                                       dbus_message_unref(reply);
+                                       goto out;
+                               }
+                       }
+
+                       if (!dbus_message_iter_close_container (&iter, &sub))
+                       {
+                               dbus_message_unref (reply);
+                               goto out;
+                       }
+
+                       if(add_message_to_received(reply, transport->connection))
+                               ret_value = 0;
+               }
+out:
+               if(pCmd)
+                       free(pCmd);
+               return ret_value;
+       }
+       else if(!strcmp(dbus_message_get_member(message), "GetId"))
+       {
+               char* path;
                char uuid[DBUS_UUID_LENGTH_BYTES];
                struct stat stats;
                MD5_CTX md5;
                DBusString binary, encoded;
 
-               ret_value = FALSE;
                path = &transport->address[11]; //start of kdbus bus path
                if(stat(path, &stats) < -1)
                {
@@ -716,19 +1066,21 @@ out:
        outgid:
                return ret_value;
        }
-       else if(!strcmp(dbus_message_get_member(message), "GetAdtAuditSessionData"))
+#endif
+/*     else if(!strcmp(dbus_message_get_member(message), "GetAdtAuditSessionData"))  //todo to be implemented if needed and possible
        {
                char* name = NULL;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
                return reply_with_error(DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN, "Could not determine audit session data for '%s'", name, message, transport->connection);
-       }
+       }*/
+#ifdef DBUS_SERVICES_IN_LIB
        else if(!strcmp(dbus_message_get_member(message), "GetConnectionSELinuxSecurityContext"))
        {
                char* name = NULL;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
                if(inter_ret == -ENOENT)  //name has no owner
                        return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get security context of name '%s': no such name", name, message, transport->connection);
                else if(inter_ret < 0)
@@ -737,7 +1089,6 @@ out:
                {
                        DBusMessage *reply;
 
-                       ret_value = FALSE;
                        reply = dbus_message_new_method_return(message);
                        if(reply != NULL)
                        {
@@ -745,32 +1096,18 @@ out:
                                if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
                                        dbus_message_unref(reply);
                                else if(add_message_to_received(reply, transport->connection))
-                                       ret_value = TRUE;
+                                       ret_value = 0;
                        }
                }
        }
+#endif
        else
-               return reply_with_error(DBUS_ERROR_UNKNOWN_METHOD, NULL, (char*)dbus_message_get_member(message), message, transport->connection);
-/*     else if(!strcmp(dbus_message_get_member(message), "ListActivatableNames"))  //todo
-       {
-
-       }
-       else if(!strcmp(dbus_message_get_member(message), "StartServiceByName"))
-       {
-
-       }
-       else if(!strcmp(dbus_message_get_member(message), "UpdateActivationEnvironment"))
-       {
-
-       }
-       else if(!strcmp(dbus_message_get_member(message), "ReloadConfig"))
-       {
-
-       }
-       */
+               return 1;  //send to daemon
 
+#ifdef DBUS_SERVICES_IN_LIB
        if(info.sec_label)
                free(info.sec_label);
+#endif
        return ret_value;
 }
 
@@ -854,6 +1191,7 @@ static int put_message_into_data(DBusMessage *message, char* data)
     const DBusString *body;
     int size;
 
+    dbus_message_set_serial(message, 1);
     dbus_message_lock (message);
     _dbus_message_get_network_data (message, &header, &body);
     ret_size = _dbus_string_get_length(header);
@@ -862,7 +1200,6 @@ static int put_message_into_data(DBusMessage *message, char* data)
        size = _dbus_string_get_length(body);
        memcpy(data, _dbus_string_get_const_data(body), size);
        ret_size += size;
-       dbus_message_unref(message);
 
        return ret_size;
 }
@@ -1343,7 +1680,7 @@ check_write_watch (DBusTransport *transport)
   _dbus_transport_ref (transport);
 
 #ifdef DBUS_AUTHENTICATION
-  if (_dbus_transport_get_is_authenticated (transport))
+  if (_dbus_transport_try_to_authenticate (transport))
 #endif
     needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
 #ifdef DBUS_AUTHENTICATION
@@ -1401,7 +1738,7 @@ check_read_watch (DBusTransport *transport)
   _dbus_transport_ref (transport);
 
 #ifdef DBUS_AUTHENTICATION
-  if (_dbus_transport_get_is_authenticated (transport))
+  if (_dbus_transport_try_to_authenticate (transport))
 #endif
     need_read_watch =
       (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
@@ -1462,12 +1799,13 @@ read_data_into_auth (DBusTransport *transport,
   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
   DBusString *buffer;
   int bytes_read;
+  int *fds, n_fds;
 
   *oom = FALSE;
 
   _dbus_auth_get_buffer (transport->auth, &buffer);
 
-  bytes_read = kdbus_read_message(socket_transport, buffer);
+  bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
 
   _dbus_auth_return_buffer (transport->auth, buffer,
                             bytes_read > 0 ? bytes_read : 0);
@@ -1507,11 +1845,38 @@ read_data_into_auth (DBusTransport *transport,
     }
 }
 
+static int kdbus_send_auth (DBusTransport *transport,  const DBusString *buffer)
+{
+       int len;
+       int bytes_written = -1;
+       struct kdbus_msg *msg;
+       struct kdbus_item *item;
+
+       len = _dbus_string_get_length (buffer);
+//     data = _dbus_string_get_const_data_len (buffer, 0, len);
+
+       msg = kdbus_init_msg(NULL, 1, 0, FALSE, 0, (DBusTransportSocket*)transport);
+       item = msg->items;
+       MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data_len (buffer, 0, len), len);
+
+    again:
+    if(ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_MSG_SEND, msg))
+    {
+        if(errno == EINTR)
+            goto again;
+        _dbus_verbose ("Error writing auth: %d, %m\n", errno);
+    }
+    else
+        bytes_written = len;
+
+       return bytes_written;
+}
+
 /* Return value is whether we successfully wrote any bytes */
 static dbus_bool_t
 write_data_from_auth (DBusTransport *transport)
 {
-  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
+//  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
   int bytes_written;
   const DBusString *buffer;
 
@@ -1519,9 +1884,7 @@ write_data_from_auth (DBusTransport *transport)
                                      &buffer))
     return FALSE;
 
-  bytes_written = _dbus_write_socket (socket_transport->fd,
-                                      buffer,
-                                      0, _dbus_string_get_length (buffer));
+  bytes_written = kdbus_send_auth (transport, buffer);
 
   if (bytes_written > 0)
     {
@@ -1617,7 +1980,7 @@ do_authentication (DBusTransport *transport,
 
   oom = FALSE;
 
-  orig_auth_state = _dbus_transport_get_is_authenticated (transport);
+  orig_auth_state = _dbus_transport_try_to_authenticate (transport);
 
   /* This is essential to avoid the check_write_watch() at the end,
    * we don't want to add a write watch in do_iteration before
@@ -1632,7 +1995,7 @@ do_authentication (DBusTransport *transport,
 
   _dbus_transport_ref (transport);
 
-  while (!_dbus_transport_get_is_authenticated (transport) &&
+   while (!_dbus_transport_try_to_authenticate (transport) &&
          _dbus_transport_get_is_connected (transport))
     {
       if (!exchange_credentials (transport, do_reading, do_writing))
@@ -1689,7 +2052,7 @@ do_authentication (DBusTransport *transport,
 
  out:
   if (auth_completed)
-    *auth_completed = (orig_auth_state != _dbus_transport_get_is_authenticated (transport));
+    *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
 
   check_read_watch (transport);
   check_write_watch (transport);
@@ -1711,7 +2074,7 @@ do_writing (DBusTransport *transport)
 
 #ifdef DBUS_AUTHENTICATION
        /* No messages without authentication! */
-       if (!_dbus_transport_get_is_authenticated (transport))
+       if (!_dbus_transport_try_to_authenticate (transport))
     {
                _dbus_verbose ("Not authenticated, not writing anything\n");
                return TRUE;
@@ -1743,7 +2106,12 @@ do_writing (DBusTransport *transport)
 
                message = _dbus_connection_get_message_to_send (transport->connection);
                _dbus_assert (message != NULL);
-               dbus_message_lock (message);
+               if(dbus_message_get_sender(message) == NULL)  //needed for daemon
+               {
+            dbus_message_unlock(message);
+            dbus_message_set_sender(message, socket_transport->sender);
+            dbus_message_lock (message);
+               }
                _dbus_message_get_network_data (message, &header, &body);
                total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
                pDestination = dbus_message_get_destination(message);
@@ -1752,11 +2120,23 @@ do_writing (DBusTransport *transport)
                {
                        if(!strcmp(pDestination, "org.freedesktop.DBus"))
                        {
-                               if(emulateOrgFreedesktopDBus(transport, message))
-                                       bytes_written = total_bytes_to_write;
-                               else
-                                       bytes_written = -1;
-                               goto written;
+                               if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
+                               {
+                                       int ret;
+
+                                       ret = emulateOrgFreedesktopDBus(transport, message);
+                                       if(ret < 0)
+                                       {
+                                               bytes_written = -1;
+                                               goto written;
+                                       }
+                                       else if(ret == 0)
+                                       {
+                                               bytes_written = total_bytes_to_write;
+                                               goto written;
+                                       }
+                                       //else send to "daemon" as to normal recipient
+                               }
                        }
                }
                if (_dbus_auth_needs_encoding (transport->auth))
@@ -1853,7 +2233,7 @@ do_reading (DBusTransport *transport)
 
 #ifdef DBUS_AUTHENTICATION
   /* No messages without authentication! */
-  if (!_dbus_transport_get_is_authenticated (transport))
+  if (!_dbus_transport_try_to_authenticate (transport))
     return TRUE;
 #endif
 
@@ -2092,10 +2472,12 @@ socket_disconnect (DBusTransport *transport)
 }
 
 static dbus_bool_t
-socket_connection_set (DBusTransport *transport)
+kdbus_connection_set (DBusTransport *transport)
 {
   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
 
+  dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus
+
   _dbus_watch_set_handler (socket_transport->write_watch,
                            _dbus_connection_handle_watch,
                            transport->connection, NULL);
@@ -2122,7 +2504,7 @@ socket_connection_set (DBusTransport *transport)
   return TRUE;
 }
 
-/**
+/**  original dbus copy-pasted
  * @todo We need to have a way to wake up the select sleep if
  * a new iteration request comes in with a flag (read/write) that
  * we're not currently serving. Otherwise a call that just reads
@@ -2156,7 +2538,7 @@ kdbus_do_iteration (DBusTransport *transport,
    poll_fd.fd = socket_transport->fd;
    poll_fd.events = 0;
 
-   if (_dbus_transport_get_is_authenticated (transport))
+   if (_dbus_transport_try_to_authenticate (transport))
    {
       /* This is kind of a hack; if we have stuff to write, then try
        * to avoid the poll. This is probably about a 5% speedup on an
@@ -2300,7 +2682,7 @@ static const DBusTransportVTable kdbus_vtable = {
   socket_finalize,
   socket_handle_watch,
   socket_disconnect,
-  socket_connection_set,
+  kdbus_connection_set,
   kdbus_do_iteration,
   socket_live_messages_changed,
   socket_get_socket_fd
@@ -2390,594 +2772,123 @@ _dbus_transport_new_for_socket_kdbus (int     fd,
  *
  * @param path the path to UNIX domain socket
  * @param error return location for error code
- * @returns connection file descriptor or -1 on error
- */
-static int _dbus_connect_kdbus (const char *path, DBusError *error)
-{
-       int fd;
-
-       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-       _dbus_verbose ("connecting to kdbus bus %s\n", path);
-
-       fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
-       if (fd < 0)
-               dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
-
-       return fd;
-}
-
-/**
- * maps memory pool for messages received by the kdbus transport
- *
- * @param transport transport
- * @returns #TRUE on success, otherwise FALSE
- */
-static dbus_bool_t kdbus_mmap(DBusTransport* transport)
-{
-       DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
-
-       socket_transport->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, socket_transport->fd, 0);
-       if (socket_transport->kdbus_mmap_ptr == MAP_FAILED)
-               return FALSE;
-
-       return TRUE;
-}
-
-/**
- * Creates a new transport for kdbus.
- * This creates a client-side of a transport.
- *
- * @param path the path to the bus.
- * @param error address where an error can be returned.
- * @returns a new transport, or #NULL on failure.
- */
-static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
-{
-       int fd;
-       DBusTransport *transport;
-       DBusString address;
-
-       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
-       if (!_dbus_string_init (&address))
-    {
-               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-               return NULL;
-    }
-
-       fd = -1;
-
-       if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
-    {
-               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-               goto failed_0;
-    }
-
-       fd = _dbus_connect_kdbus (path, error);
-       if (fd < 0)
-    {
-               _DBUS_ASSERT_ERROR_IS_SET (error);
-               goto failed_0;
-    }
-
-       _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
-
-       transport = _dbus_transport_new_for_socket_kdbus (fd, &address);
-       if (transport == NULL)
-    {
-               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-               goto failed_1;
-    }
-
-       _dbus_string_free (&address);
-
-       return transport;
-
-       failed_1:
-               _dbus_close_socket (fd, NULL);
-       failed_0:
-               _dbus_string_free (&address);
-       return NULL;
-}
-
-
-/**
- * Opens kdbus transport if method from address entry is kdbus
- *
- * @param entry the address entry to try opening
- * @param transport_p return location for the opened transport
- * @param error error to be set
- * @returns result of the attempt
- */
-DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry  *entry,
-                                                          DBusTransport    **transport_p,
-                                                          DBusError         *error)
-{
-       const char *method;
-
-       method = dbus_address_entry_get_method (entry);
-       _dbus_assert (method != NULL);
-
-       if (strcmp (method, "kdbus") == 0)
-    {
-               const char *path = dbus_address_entry_get_value (entry, "path");
-
-               if (path == NULL)
-        {
-                       _dbus_set_bad_address (error, "kdbus", "path", NULL);
-                       return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
-        }
-
-        *transport_p = _dbus_transport_new_for_kdbus (path, error);
-
-        if (*transport_p == NULL)
-        {
-               _DBUS_ASSERT_ERROR_IS_SET (error);
-               return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
-        }
-        else
-        {
-               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-               return DBUS_TRANSPORT_OPEN_OK;
-        }
-    }
-       else
-    {
-               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-               return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
-    }
-}
-
-static struct kdbus_policy *make_policy_name(const char *name)
-{
-       struct kdbus_policy *p;
-       __u64 size;
-
-       size = offsetof(struct kdbus_policy, name) + strlen(name) + 1;
-       p = malloc(size);
-       if (!p)
-               return NULL;
-       memset(p, 0, size);
-       p->size = size;
-       p->type = KDBUS_POLICY_NAME;
-       strcpy(p->name, name);
-
-       return p;
-}
-
-static struct kdbus_policy *make_policy_access(__u64 type, __u64 bits, __u64 id)
-{
-       struct kdbus_policy *p;
-       __u64 size = sizeof(*p);
-
-       p = malloc(size);
-       if (!p)
-               return NULL;
-
-       memset(p, 0, size);
-       p->size = size;
-       p->type = KDBUS_POLICY_ACCESS;
-       p->access.type = type;
-       p->access.bits = bits;
-       p->access.id = id;
-
-       return p;
-}
-
-static void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_policy *policy, __u64 max_size)
-{
-       struct kdbus_policy *dst = (struct kdbus_policy *) ((char *) cmd_policy + cmd_policy->size);
-
-       if (cmd_policy->size + policy->size > max_size)
-               return;
-
-       memcpy(dst, policy, policy->size);
-       cmd_policy->size += KDBUS_ALIGN8(policy->size);
-       free(policy);
-}
-
-/**
- * Registers kdbus policy for given connection.
- *
- * Policy sets rights of the name (unique or well known) on the bus. Without policy it is
- * not possible to send or receive messages. It must be set separately for unique id and
- * well known name of the connection. It is set after registering on the bus, but before
- * requesting for name. The policy is valid for the given name, not for the connection.
- *
- * Name of the policy equals name on the bus.
- *
- * @param name name of the policy = name of the connection
- * @param connection the connection
- * @param error place to store errors
- *
- * @returns #TRUE on success
- */
-dbus_bool_t bus_register_policy_kdbus(const char* name, DBusConnection *connection, DBusError *error)
-{
-       struct kdbus_cmd_policy *cmd_policy;
-       struct kdbus_policy *policy;
-       int size = 0xffff;
-       int fd;
-
-       if(!dbus_connection_get_socket(connection, &fd))
-       {
-               dbus_set_error (error, "Failed to get fd for registering policy", NULL);
-               return FALSE;
-       }
-
-       cmd_policy = alloca(size);
-       memset(cmd_policy, 0, size);
-
-       policy = (struct kdbus_policy *) cmd_policy->policies;
-       cmd_policy->size = offsetof(struct kdbus_cmd_policy, policies);
-
-       policy = make_policy_name(name);
-       append_policy(cmd_policy, policy, size);
-
-       policy = make_policy_access(KDBUS_POLICY_ACCESS_USER, KDBUS_POLICY_OWN, getuid());
-       append_policy(cmd_policy, policy, size);
-
-       policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_RECV, 0);
-       append_policy(cmd_policy, policy, size);
-
-       policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_SEND, 0);
-       append_policy(cmd_policy, policy, size);
-
-       if (ioctl(fd, KDBUS_CMD_EP_POLICY_SET, cmd_policy) < 0)
-       {
-               dbus_set_error(error,_dbus_error_from_errno (errno), "Error setting EP policy: %s", _dbus_strerror (errno));
-               return FALSE;
-       }
-
-       _dbus_verbose("Policy %s set correctly\n", name);
-       return TRUE;
-}
-
-/**
- * Kdbus part of dbus_bus_register.
- * Shouldn't be used separately because it needs to be surrounded
- * by other functions as it is done in dbus_bus_register.
- *
- * @param name place to store unique name given by bus
- * @param connection the connection
- * @param error place to store errors
- * @returns #TRUE on success
- */
-dbus_bool_t bus_register_kdbus(char* name, DBusConnection *connection, DBusError *error)
-{
-       struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
-       int fd;
-
-       hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
-                          KDBUS_HELLO_ATTACH_COMM |
-                          KDBUS_HELLO_ATTACH_EXE |
-                          KDBUS_HELLO_ATTACH_CMDLINE |
-                          KDBUS_HELLO_ATTACH_CAPS |
-                          KDBUS_HELLO_ATTACH_CGROUP |
-                          KDBUS_HELLO_ATTACH_SECLABEL |
-                          KDBUS_HELLO_ATTACH_AUDIT*/;
-       hello.size = sizeof(struct kdbus_cmd_hello);
-       hello.pool_size = RECEIVE_POOL_SIZE;
-
-       if(!dbus_connection_get_socket(connection, &fd))
-       {
-               dbus_set_error (error, "failed to get fd for bus registration", NULL);
-               return FALSE;
-       }
-       if (ioctl(fd, KDBUS_CMD_HELLO, &hello))
-       {
-               dbus_set_error(error,_dbus_error_from_errno (errno), "Failed to send hello: %s", _dbus_strerror (errno));
-               return FALSE;
-       }
-
-       sprintf(name, "%llu", (unsigned long long)hello.id);
-       _dbus_verbose("-- Our peer ID is: %s\n", name);
-       ((DBusTransportSocket*)dbus_connection_get_transport(connection))->bloom_size = hello.bloom_size;
-
-       if(!kdbus_mmap(dbus_connection_get_transport(connection)))
-       {
-               dbus_set_error(error,_dbus_error_from_errno (errno), "Error when mmap: %s", _dbus_strerror (errno));
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
-/**
- * kdbus version of dbus_bus_request_name.
- *
- * Asks the bus to assign the given name to this connection.
- *
- * Use same flags as original dbus version with one exception below.
- * Result flag #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER is currently
- * never returned by kdbus, instead DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
- * is returned by kdbus.
- *
- * @param connection the connection
- * @param name the name to request
- * @param flags flags
- * @param error location to store the error
- * @returns a result code, -1 if error is set
- */
-int bus_request_name_kdbus(DBusConnection *connection, const char *name, const uint64_t flags, DBusError *error)
-{
-       struct kdbus_cmd_name *cmd_name;
-       int fd;
-       uint64_t size = sizeof(*cmd_name) + strlen(name) + 1;
-       uint64_t flags_kdbus = 0;
-
-       cmd_name = alloca(size);
-
-       memset(cmd_name, 0, size);
-       strcpy(cmd_name->name, name);
-       cmd_name->size = size;
-
-       if(flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
-               flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
-       if(!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
-               flags_kdbus |= KDBUS_NAME_QUEUE;
-       if(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
-               flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
-
-       cmd_name->conn_flags = flags_kdbus;
-
-       if(!dbus_connection_get_socket(connection, &fd))
-       {
-               dbus_set_error (error, "failed to get fd for name request", NULL);
-               return -1;
-       }
-
-       _dbus_verbose("Request name - flags sent: 0x%llx       !!!!!!!!!\n", cmd_name->conn_flags);
-
-       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-       if (ioctl(fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name))
-       {
-               dbus_set_error(error,_dbus_error_from_errno (errno), "error acquiring name: %s", _dbus_strerror (errno));
-               if(errno == EEXIST)
-                       return DBUS_REQUEST_NAME_REPLY_EXISTS;
-               return -1;
-       }
-
-       _dbus_verbose("Request name - received flag: 0x%llx       !!!!!!!!!\n", cmd_name->conn_flags);
-
-       if(cmd_name->conn_flags & KDBUS_NAME_IN_QUEUE)
-               return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
-       else
-               return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
-       /*todo now 1 code is never returned -  DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
-        * because kdbus never returns it now
-        */
-}
-
-/**
- * Checks if the connection's transport is kdbus on the basis of its address
- *
- * @param pointer to the connection
- * @returns TRUE if kdbus transport, otherwise FALSE
+ * @returns connection file descriptor or -1 on error
  */
-dbus_bool_t dbus_transport_is_kdbus(DBusConnection *connection)
+static int _dbus_connect_kdbus (const char *path, DBusError *error)
 {
-       const char* address = _dbus_connection_get_address(connection);
+       int fd;
 
-       if(address)
-               if(address == strstr(address, "kdbus:path="))
-                               return TRUE;
-       return FALSE;
-}
+       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+       _dbus_verbose ("connecting to kdbus bus %s\n", path);
 
-/**
- * Seeks key in rule string, and duplicates value of the key into pValue.
- * If value is "org.freedesktop.DBus" it is indicated by returning -1, because it
- * needs to be handled in different manner.
- * Value is duplicated from rule string to newly allocated memory pointe by pValue,
- * so it must be freed after use.
- *
- * @param rule rule to look through
- * @param key key to look for
- * @param pValue pointer to value of the key found
- * @return length of the value string, 0 means not found, -1 means "org.freedesktop.DBus"
- */
-static int parse_match_key(const char *rule, const char* key, char** pValue)
-{
-       const char* pBegin;
-       const char* pValueEnd;
-       int value_length = 0;
+       fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
+       if (fd < 0)
+               dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
 
-       pBegin = strstr(rule, key);
-       if(pBegin)
-       {
-               pBegin += strlen(key);
-               pValueEnd = strchr(pBegin, '\'');
-               if(pValueEnd)
-               {
-                       value_length = pValueEnd - pBegin;
-                       *pValue = strndup(pBegin, value_length);
-                       if(*pValue)
-                       {
-                               if(strcmp(*pValue, "org.freedesktop.DBus") == 0)
-                                       value_length = -1;
-                               _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
-                       }
-               }
-       }
-       return value_length;
+       return fd;
 }
 
 /**
- * Adds a match rule to match broadcast messages going through the message bus.
- * Do no affect messages addressed directly.
- *
- * The "rule" argument is the string form of a match rule.
- *
- * In kdbus function do not blocks.
- *
- * Upper function returns nothing because of blocking issues
- * so there is no point to return true or false here.
- *
- * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
- * Current mapping:
- * interface match key mapped to bloom
- * sender match key mapped to src_name
- * also handled org.freedesktop.dbus members: NameOwnerChanged, NameLost, NameAcquired
+ * Creates a new transport for kdbus.
+ * This creates a client-side of a transport.
  *
- * @param connection connection to the message bus
- * @param rule textual form of match rule
- * @param error location to store any errors - may be NULL
+ * @param path the path to the bus.
+ * @param error address where an error can be returned.
+ * @returns a new transport, or #NULL on failure.
  */
-void dbus_bus_add_match_kdbus (DBusConnection *connection, const char *rule, DBusError *error)
+static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
 {
-       struct kdbus_cmd_match* pCmd_match;
-       struct kdbus_item *pItem;
        int fd;
-       __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
-       uint64_t size;
-       unsigned int kernel_item = 0;
-       int name_size;
-       char* pName = NULL;
-       char* pInterface = NULL;
-       __u64 bloom_size = ((DBusTransportSocket*)dbus_connection_get_transport(connection))->bloom_size;
-
-       dbus_connection_get_socket(connection, &fd);
+       DBusTransport *transport;
+       DBusString address;
 
-       /*parsing rule and calculating size of command*/
-       size = sizeof(struct kdbus_cmd_match);
+       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-       if(strstr(rule, "member='NameOwnerChanged'"))
-       {
-               kernel_item = KDBUS_MATCH_NAME_CHANGE;
-               size += KDBUS_ITEM_SIZE(1);
-       }
-       else if(strstr(rule, "member='NameLost'"))
-       {
-               kernel_item = KDBUS_MATCH_NAME_REMOVE;
-               size += KDBUS_ITEM_SIZE(1);
-       }
-       else if(strstr(rule, "member='NameAcquired'"))
-       {
-               kernel_item = KDBUS_MATCH_NAME_ADD;
-               size += KDBUS_ITEM_SIZE(1);
-       }
+       if (!_dbus_string_init (&address))
+    {
+               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+               return NULL;
+    }
 
-       name_size = parse_match_key(rule, "interface='", &pInterface);
-       if((name_size == -1) && (kernel_item == 0))   //means org.freedesktop.DBus without specified member
-       {
-               kernel_item = ~0;
-               size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;  /* 3 above name related items plus 2 id related items*/
-       }
-       else if(name_size > 0)                  /*actual size is not important for interface because bloom size is defined by bus*/
-               size += KDBUS_PART_HEADER_SIZE + bloom_size;
+       fd = -1;
 
-       name_size = parse_match_key(rule, "sender='", &pName);
-       if((name_size == -1) && (kernel_item == 0))  //means org.freedesktop.DBus without specified name - same as interface few line above
-       {
-               kernel_item = ~0;
-               size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /* 3 above     name related items plus 2 id related items*/
-       }
-       else if(name_size > 0)
-       {
-               if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
-               {
-                       src_id = strtoull(&pName[3], NULL, 10);
-                       free(pName);
-                       pName = NULL;
-               }
-               else
-                       size += KDBUS_ITEM_SIZE(name_size + 1);  //well known name
-       }
+       if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
+    {
+               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+               goto failed_0;
+    }
 
-       pCmd_match = alloca(size);
-       if(pCmd_match == NULL)
-               goto out;
+       fd = _dbus_connect_kdbus (path, error);
+       if (fd < 0)
+    {
+               _DBUS_ASSERT_ERROR_IS_SET (error);
+               goto failed_0;
+    }
 
-       pCmd_match->id = 0;
-       pCmd_match->size = size;
-       pCmd_match->cookie = strtoull(dbus_bus_get_unique_name(connection), NULL , 10);
+       _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
 
-       pItem = pCmd_match->items;
-       if(kernel_item == ~0)  //all signals from kernel
-       {
-               pCmd_match->src_id = 0;
-               pItem->type = KDBUS_MATCH_NAME_CHANGE;
-               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
-               pItem = KDBUS_PART_NEXT(pItem);
-               pItem->type = KDBUS_MATCH_NAME_ADD;
-               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
-               pItem = KDBUS_PART_NEXT(pItem);
-               pItem->type = KDBUS_MATCH_NAME_REMOVE;
-               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
-               pItem = KDBUS_PART_NEXT(pItem);
-               pItem->type = KDBUS_MATCH_ID_ADD;
-               pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
-               pItem = KDBUS_PART_NEXT(pItem);
-               pItem->type = KDBUS_MATCH_ID_REMOVE;
-               pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
-       }
-       else if(kernel_item) //only one item
-       {
-               pCmd_match->src_id = 0;
-               pItem->type = kernel_item;
-               pItem->size = KDBUS_PART_HEADER_SIZE + 1;
-       }
-       else
-       {
-               pCmd_match->src_id = src_id;
-               if(pName)
-               {
-                       pItem->type = KDBUS_MATCH_SRC_NAME;
-                       pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
-                       strcpy(pItem->str, pName);
-                       pItem = KDBUS_PART_NEXT(pItem);
-               }
+       transport = _dbus_transport_new_for_socket_kdbus (fd, &address);
+       if (transport == NULL)
+    {
+               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+               goto failed_1;
+    }
 
-               if(pInterface)
-               {
-                       pItem->type = KDBUS_MATCH_BLOOM;
-                       pItem->size = KDBUS_PART_HEADER_SIZE + bloom_size;
-                       strncpy(pItem->data, pInterface, bloom_size);
-               }
-       }
+       _dbus_string_free (&address);
 
-       if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
-       {
-               if(error)
-                       dbus_set_error(error,_dbus_error_from_errno (errno), "error adding match: %s", _dbus_strerror (errno));
-               _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
-       }
-       else
-               _dbus_verbose("Added match bus rule %s\n", rule);
+       return transport;
 
-out:
-       if(pName)
-               free(pName);
-       if(pInterface)
-               free(pInterface);
+       failed_1:
+               _dbus_close_socket (fd, NULL);
+       failed_0:
+               _dbus_string_free (&address);
+       return NULL;
 }
 
+
 /**
- * Opposing to dbus, in kdbus removes all match rules with given
- * cookie, which now is equal to uniqe id.
- *
- * In kdbus this function will not block
+ * Opens kdbus transport if method from address entry is kdbus
  *
- * @param connection connection to the message bus
- * @param error location to store any errors - may be NULL
+ * @param entry the address entry to try opening
+ * @param transport_p return location for the opened transport
+ * @param error error to be set
+ * @returns result of the attempt
  */
-void dbus_bus_remove_match_kdbus (DBusConnection *connection, DBusError *error)
+DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry  *entry,
+                                                          DBusTransport    **transport_p,
+                                                          DBusError         *error)
 {
-       struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
-       int fd;
+       const char *method;
 
-       dbus_connection_get_socket(connection, &fd);
-       cmd.cookie = strtoull(dbus_bus_get_unique_name(connection), NULL , 10);
-       cmd.id = cmd.cookie;
-       cmd.size = sizeof(struct kdbus_cmd_match);
+       method = dbus_address_entry_get_method (entry);
+       _dbus_assert (method != NULL);
 
-       if(ioctl(fd, KDBUS_CMD_MATCH_ADD, &cmd))
-       {
-               if(error)
-                       dbus_set_error(error,_dbus_error_from_errno (errno), "error removing match: %s", _dbus_strerror (errno));
-               _dbus_verbose("Failed removing match rule; error: %d, %m\n", errno);
-       }
+       if (strcmp (method, "kdbus") == 0)
+    {
+               const char *path = dbus_address_entry_get_value (entry, "path");
+
+               if (path == NULL)
+        {
+                       _dbus_set_bad_address (error, "kdbus", "path", NULL);
+                       return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
+        }
+
+        *transport_p = _dbus_transport_new_for_kdbus (path, error);
+
+        if (*transport_p == NULL)
+        {
+               _DBUS_ASSERT_ERROR_IS_SET (error);
+               return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
+        }
+        else
+        {
+               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+               return DBUS_TRANSPORT_OPEN_OK;
+        }
+    }
        else
-               _dbus_verbose("Match rule removed correctly.\n");
+    {
+               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+               return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
+    }
 }