Added NameList method handling, small improvemnets and cleanups
[platform/upstream/dbus.git] / dbus / dbus-transport-kdbus.c
index d07cda1..a8fccc1 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <limits.h>
 
 #define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
 #define KDBUS_PART_HEADER_SIZE offsetof(struct kdbus_item, data)
        for (part = (head)->first;                                      \
             (uint8_t *)(part) < (uint8_t *)(head) + (head)->size;      \
             part = KDBUS_PART_NEXT(part))
-#define RECEIVE_POOL_SIZE (50 * 1024LU * 1024LU)  //todo pool size to be decided
-#define MEMFD_POOL_SIZE        DBUS_MAXIMUM_MESSAGE_LENGTH
+#define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU)
 #define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used
 
-#define KDBUS_DECODE_DEBUG 1
+#define KDBUS_MSG_DECODE_DEBUG 0
 
 
 /**
@@ -149,7 +149,7 @@ static int kdbus_init_memfd(DBusTransportSocket* socket_transport)
        return 0;
 }
 
-static struct kdbus_msg* kdbus_init_msg(DBusTransportSocket *socket_transport, const char* name, __u64 dst_id, uint64_t body_size, dbus_bool_t use_memfd)
+static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t body_size, dbus_bool_t use_memfd, int fds_count)
 {
     struct kdbus_msg* msg;
     uint64_t msg_size;
@@ -157,14 +157,16 @@ static struct kdbus_msg* kdbus_init_msg(DBusTransportSocket *socket_transport, c
     msg_size = sizeof(struct kdbus_msg);
 
     if(use_memfd == TRUE)  // bulk data - memfd - encoded and plain
-    {
         msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
-    else {
+    else {
         msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
        if(body_size)
                msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
     }
 
+    if(fds_count)
+       msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
+
     if (name)
        msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
     else if (dst_id == KDBUS_DST_ID_BROADCAST)
@@ -179,18 +181,12 @@ static struct kdbus_msg* kdbus_init_msg(DBusTransportSocket *socket_transport, c
 
     memset(msg, 0, msg_size);
     msg->size = msg_size;
-    msg->src_id = strtoll(dbus_bus_get_unique_name(socket_transport->base.connection), NULL , 10);
     msg->payload_type = KDBUS_PAYLOAD_DBUS1;
     msg->dst_id = name ? 0 : dst_id;
 
     return msg;
 }
 
-/*
- * 
- *
- * 
- */
 static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message, dbus_bool_t encoded)
 {
     struct kdbus_msg *msg;
@@ -208,8 +204,6 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
     unsigned fds_count;
     char *buf;
 
-    _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);  //todo or to remove
-
     // determine name and destination id
     if((name = dbus_message_get_destination(message)))
     {
@@ -235,12 +229,14 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
 
     // check if message size is big enough to use memfd kdbus transport
     use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE;
-
     if(use_memfd) kdbus_init_memfd(transport);
     
+    _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
+
     // init basic message fields
-    msg = kdbus_init_msg(transport, name, dst_id, body_size, use_memfd); //todo add fds
+    msg = kdbus_init_msg(name, dst_id, body_size, use_memfd, fds_count);
     msg->cookie = dbus_message_get_serial(message);
+    msg->src_id = strtoll(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
     
     // build message contents
     item = msg->items;
@@ -251,39 +247,38 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
            ret = ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0);
            if (ret < 0) 
            {
-               _dbus_verbose("memfd sealing failed: \n");
-               goto out;
+                       _dbus_verbose("memfd sealing failed: \n");
+                       goto out;
            }
 
            buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
            if (buf == MAP_FAILED) 
            {
-               _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
-               goto out;
+                       _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
+                       goto out;
            }
 
-            if(encoded)                           
-               memcpy(buf, &transport->encoded_outgoing, ret_size);
-            else
-            {
-               memcpy(buf, _dbus_string_get_const_data(header), header_size);
-               if(body_size) {
-                       buf+=header_size;
-                       memcpy(buf, _dbus_string_get_const_data(body),  body_size);
-                       buf-=header_size;
+               if(encoded)
+                       memcpy(buf, &transport->encoded_outgoing, ret_size);
+               else
+               {
+                       memcpy(buf, _dbus_string_get_const_data(header), header_size);
+                       if(body_size) {
+                               buf+=header_size;
+                               memcpy(buf, _dbus_string_get_const_data(body),  body_size);
+                               buf-=header_size;
+                       }
                }
 
-            }
-           
-           munmap(buf, ret_size);
+               munmap(buf, ret_size);
 
-            // seal data - kdbus module needs it
-           ret = ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1);
-           if (ret < 0) {
-               _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
-                               ret_size = -1;
-                               goto out;
-           }
+               // seal data - kdbus module needs it
+               ret = ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1);
+               if (ret < 0) {
+                       _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
+                       ret_size = -1;
+                       goto out;
+               }
 
            item->type = KDBUS_MSG_PAYLOAD_MEMFD;
                item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
@@ -309,7 +304,7 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
 
         if(body_size)
         {
-                _dbus_verbose("body attaching\n");
+            _dbus_verbose("body attaching\n");
                item = KDBUS_PART_NEXT(item);
                item->type = KDBUS_MSG_PAYLOAD_VEC;
                item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
@@ -318,9 +313,12 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
         }
     }
 
-    if(fds_count)  //todo
+    if(fds_count)
     {
-
+       item = KDBUS_PART_NEXT(item);
+       item->type = KDBUS_MSG_FDS;
+       item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
+       memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
     }
 
        if (name)
@@ -348,7 +346,7 @@ static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message,
                        DBusMessage *errMessage = NULL;
                        dbus_uint32_t replySerial;
 
-                       errMessage = generate_local_error_message(msg->cookie, DBUS_ERROR_SERVICE_UNKNOWN, NULL);
+                       errMessage = generate_local_error_message(msg->cookie, DBUS_ERROR_SERVICE_UNKNOWN, (char*)dbus_message_get_destination(message));
                        if(errMessage == NULL)
                        {
                                ret_size = -1;
@@ -426,7 +424,7 @@ static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessa
                {
                        DBusMessage *reply;
                        DBusMessageIter args;
-                       char unique_name[(unsigned int)(sizeof(ret)*2.5 + 4)];
+                       char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + 4)]; //+3 prefix ":1." and +1 closing NULL
                        const char* pString = unique_name;
 
                        sprintf(unique_name, ":1.%lld", (long long int)ret);
@@ -434,6 +432,7 @@ static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessa
                        reply = dbus_message_new_method_return(message);
                        if(reply == NULL)
                                return FALSE;
+                       dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
                    dbus_message_iter_init_append(reply, &args);
                    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pString))
                        return FALSE;
@@ -445,7 +444,7 @@ static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessa
                        DBusMessage *errMessage;
                        dbus_uint32_t replySerial;
 
-                       errMessage = generate_local_error_message(1, DBUS_ERROR_NAME_HAS_NO_OWNER, NULL);
+                       errMessage = generate_local_error_message(1, DBUS_ERROR_NAME_HAS_NO_OWNER, name);
                        if(errMessage == NULL)
                                return FALSE;
                        replySerial = dbus_message_get_reply_serial(message);
@@ -473,12 +472,76 @@ static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessa
                reply = dbus_message_new_method_return(message);
                if(reply == NULL)
                        return FALSE;
+               dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
                dbus_message_iter_init_append(reply, &args);
                if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &result))
                        return FALSE;
                if(add_message_to_received(reply, transport))
                        return TRUE;
        }
+       else if(!strcmp(dbus_message_get_member(message), "ListNames"))
+       {
+               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(fd, KDBUS_CMD_NAME_LIST, pCmd))
+               {
+                       if(errno == EINTR)
+                               goto again;
+                       if(errno == ENOBUFS)
+                               cmd_size = pCmd->size;
+                       else
+                       {
+                               _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
+                               goto out;
+                       }
+               }
+               if(cmd_size)
+               {
+                       pCmd = realloc(pCmd, cmd_size);
+                       if(pCmd == NULL)
+                               return FALSE;
+                       goto again;
+               }
+               else
+               {
+                       DBusMessage *reply;
+                       DBusMessageIter args;
+                       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, &args);
+
+                       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))
+                               goto out;
+                       }
+
+                       if(add_message_to_received(reply, transport))
+                       {
+                               free(pCmd);
+                               return TRUE;
+                       }
+               }
+
+out:
+               if(pCmd)
+                       free(pCmd);
+       }
        else  //temporarily we send that info but methods below should be implemented
        {
                DBusMessage *reply;
@@ -493,11 +556,7 @@ static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessa
                if(add_message_to_received(reply, transport))
                        return TRUE;
        }
-       /*else if(!strcmp(dbus_message_get_member(message), "ListNames"))
-       {
-               //todo
-       }
-       else if(!strcmp(dbus_message_get_member(message), "ListActivatableNames"))
+/*     else if(!strcmp(dbus_message_get_member(message), "ListActivatableNames"))
        {
                //todo
        }
@@ -521,7 +580,7 @@ static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessa
        return FALSE;
 }
 
-#if KDBUS_DECODE_DEBUG == 1
+#if KDBUS_MSG_DECODE_DEBUG == 1
 static char *msg_id(uint64_t id, char *buf)
 {
        if (id == 0)
@@ -605,23 +664,22 @@ static int put_message_into_data(DBusMessage *message, char* data)
        return ret_size;
 }
 
-static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportSocket* socket_transport)
+static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportSocket* socket_transport, int* fds, int* n_fds)
 {
        const struct kdbus_item *item = msg->items;
        int ret_size = 0;
        DBusMessage *message = NULL;
        DBusMessageIter args;
-       char dbus_name[(unsigned int)(sizeof(item->name_change.new_id)*2.5 + 4)];
-       const char* pDBusName = dbus_name;
-       const char* dbus = "org.freedesktop.DBus";
        const char* emptyString = "";
-        const char* pString = NULL;
-        void* mmap_ptr;
-#if KDBUS_DECODE_DEBUG == 1
+    const char* pString = NULL;
+       char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + 4)];  //+3 prefix ":1." and +1 closing NULL
+       const char* pDBusName = dbus_name;
+    void* mmap_ptr;
+#if KDBUS_MSG_DECODE_DEBUG == 1
        char buf[32];
 #endif
 
-#if KDBUS_DECODE_DEBUG == 1
+#if KDBUS_MSG_DECODE_DEBUG == 1
        _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
                enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
                (unsigned long long) msg->flags,
@@ -629,14 +687,15 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
 #endif
 
-        mmap_ptr = socket_transport->kdbus_mmap_ptr;
+       *n_fds = 0;
+       mmap_ptr = socket_transport->kdbus_mmap_ptr;
 
        KDBUS_PART_FOREACH(item, msg, items)
        {
                if (item->size <= KDBUS_PART_HEADER_SIZE)
                {
                        _dbus_verbose("  +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
-                       continue;  //todo to be discovered and rewritten
+                       break;  //??? continue (because dbus will find error) or break
                }
 
                switch (item->type)
@@ -665,7 +724,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
 
                                _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
                                
-                               buf = mmap(NULL, MEMFD_POOL_SIZE, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
+                               buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
 
                                if (buf == MAP_FAILED) 
                                {
@@ -677,15 +736,27 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                data += size;
                                ret_size += size;
 
-                               munmap(buf, MEMFD_POOL_SIZE);
+                               munmap(buf, size);
 
-                                _dbus_verbose("  +%s (%llu bytes) off=%llu size=%llu\n",
+                _dbus_verbose("  +%s (%llu bytes) off=%llu size=%llu\n",
                                           enum_MSG(item->type), item->size,
                                           (unsigned long long)item->vec.offset,
                                           (unsigned long long)item->vec.size);
                        break;
                        }
-#if KDBUS_DECODE_DEBUG == 1
+
+                       case KDBUS_MSG_FDS:
+                       {
+                               int i;
+
+                               *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
+                               memcpy(fds, item->fds, *n_fds * sizeof(int));
+                   for (i = 0; i < *n_fds; i++)
+                     _dbus_fd_set_close_on_exec(fds[i]);
+                       break;
+                       }
+
+#if KDBUS_MSG_DECODE_DEBUG == 1
                        case KDBUS_MSG_SRC_CREDS:
                                _dbus_verbose("  +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
                                        enum_MSG(item->type), item->size,
@@ -706,7 +777,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
 
                        case KDBUS_MSG_SRC_CMDLINE:
                        case KDBUS_MSG_SRC_NAMES: {
-                               size_t size = item->size - KDBUS_PART_HEADER_SIZE;
+                               __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
                                const char *str = item->str;
                                int count = 0;
 
@@ -789,7 +860,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                        item->name_change.new_id, item->name_change.flags);
 
                                message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
-                                           dbus, // interface name of the signal
+                                           DBUS_INTERFACE_DBUS, // interface name of the signal
                                            "NameOwnerChanged"); // name of the signal
                                if(message == NULL)
                                {
@@ -817,7 +888,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                goto out;
                                }
 
-                               dbus_message_set_sender(message, dbus);
+                               dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
                                dbus_message_set_serial(message, 1);
 
                                ret_size = put_message_into_data(message, data);
@@ -830,7 +901,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                        item->name_change.new_id, item->name_change.flags);
 
                                message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
-                                           dbus, // interface name of the signal
+                                           DBUS_INTERFACE_DBUS, // interface name of the signal
                                            "NameOwnerChanged"); // name of the signal
                                if(message == NULL)
                                {
@@ -858,7 +929,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                goto out;
                                }
 
-                               dbus_message_set_sender(message, dbus);
+                               dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
                                dbus_message_set_serial(message, 1);
 
                                ret_size = put_message_into_data(message, data);
@@ -871,7 +942,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                        item->name_change.new_id, item->name_change.flags);
 
                                message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
-                                           dbus, // interface name of the signal
+                                           DBUS_INTERFACE_DBUS, // interface name of the signal
                                            "NameOwnerChanged"); // name of the signal
                                if(message == NULL)
                                {
@@ -894,14 +965,14 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                goto out;
                                }
                            sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
-                           _dbus_verbose ("New id: %s\n", pDBusName);  //todo to be removed
+                           _dbus_verbose ("New id: %s\n", pDBusName);
                            if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
                                {
                                        ret_size = -1;
                                goto out;
                                }
 
-                               dbus_message_set_sender(message, dbus);
+                               dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
                                dbus_message_set_serial(message, 1);
 
                                ret_size = put_message_into_data(message, data);
@@ -914,7 +985,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                           (unsigned long long) item->id_change.flags);
 
                                message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
-                                           dbus, // interface name of the signal
+                                           DBUS_INTERFACE_DBUS, // interface name of the signal
                                            "NameOwnerChanged"); // name of the signal
                                if(message == NULL)
                                {
@@ -940,7 +1011,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                goto out;
                                }
 
-                               dbus_message_set_sender(message, dbus);
+                               dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
                                dbus_message_set_serial(message, 1);
 
                                ret_size = put_message_into_data(message, data);
@@ -953,7 +1024,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                           (unsigned long long) item->id_change.flags);
 
                                message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
-                                           dbus, // interface name of the signal
+                                           DBUS_INTERFACE_DBUS, // interface name of the signal
                                            "NameOwnerChanged"); // name of the signal
                                if(message == NULL)
                                {
@@ -979,12 +1050,12 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                goto out;
                                }
 
-                               dbus_message_set_sender(message, dbus);
+                               dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
                                dbus_message_set_serial(message, 1);
 
                                ret_size = put_message_into_data(message, data);
                        break;
-#if KDBUS_DECODE_DEBUG == 1
+#if KDBUS_MSG_DECODE_DEBUG == 1
                        default:
                                _dbus_verbose("  +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
                        break;
@@ -992,7 +1063,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                }
        }
 
-#if KDBUS_DECODE_DEBUG == 1
+#if KDBUS_MSG_DECODE_DEBUG == 1
 
        if ((char *)item - ((char *)msg + msg->size) >= 8)
                _dbus_verbose("invalid padding at end of message\n");
@@ -1004,7 +1075,7 @@ out:
        return ret_size;
 }
 
-static int kdbus_read_message(DBusTransportSocket *socket_transport, DBusString *buffer)
+static int kdbus_read_message(DBusTransportSocket *socket_transport, DBusString *buffer, int* fds, int* n_fds)
 {
        int ret_size;
        uint64_t __attribute__ ((__aligned__(8))) offset;
@@ -1031,7 +1102,7 @@ static int kdbus_read_message(DBusTransportSocket *socket_transport, DBusString
 
        msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
 
-       ret_size = kdbus_decode_msg(msg, data, socket_transport);
+       ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
        _dbus_string_set_length (buffer, ret_size);
 
        again2:
@@ -1217,6 +1288,7 @@ do_io_error (DBusTransport *transport)
   _dbus_transport_unref (transport);
 }
 
+#ifdef DBUS_AUTHENTICATION
 /* return value is whether we successfully read any new data. */
 static dbus_bool_t
 read_data_into_auth (DBusTransport *transport,
@@ -1400,7 +1472,6 @@ do_authentication (DBusTransport *transport,
     {
       if (!exchange_credentials (transport, do_reading, do_writing))
         {
-          /* OOM */
           oom = TRUE;
           goto out;
         }
@@ -1464,6 +1535,7 @@ do_authentication (DBusTransport *transport,
   else
     return TRUE;
 }
+#endif
 
 /* returns false on oom */
 static dbus_bool_t
@@ -1551,14 +1623,14 @@ do_writing (DBusTransport *transport)
 
                        total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
                        if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
-                               return -E2BIG;  //todo to be changed because large messages are now sent through memfd
+                               return -E2BIG;
 
                        bytes_written = kdbus_write_msg(socket_transport, message, TRUE);
         }
                else
                {
                        if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
-                               return -E2BIG;   //todo to be changed because large messages are now sent through memfd
+                               return -E2BIG;
 
                        bytes_written = kdbus_write_msg(socket_transport, message, FALSE);
                }
@@ -1618,8 +1690,8 @@ do_reading (DBusTransport *transport)
   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
   DBusString *buffer;
   int bytes_read;
-//  int total;
   dbus_bool_t oom;
+  int *fds, n_fds;
 
   _dbus_verbose ("fd = %d\n",socket_transport->fd);
 
@@ -1629,20 +1701,11 @@ do_reading (DBusTransport *transport)
 
   oom = FALSE;
 
-//  total = 0;
-
  again:
 
   /* See if we've exceeded max messages and need to disable reading */
   check_read_watch (transport);
 
-/*  if (total > socket_transport->max_bytes_read_per_iteration)
-    {
-      _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
-                     total, socket_transport->max_bytes_read_per_iteration);
-      goto out;
-    }*/
-
   _dbus_assert (socket_transport->read_watch != NULL ||
                 transport->disconnected);
 
@@ -1652,8 +1715,51 @@ do_reading (DBusTransport *transport)
   if (!dbus_watch_get_enabled (socket_transport->read_watch))
     return TRUE;
 
+  if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
+  {
+      _dbus_verbose ("Out of memory reading file descriptors\n");
+      oom = TRUE;
+      goto out;
+  }
+  _dbus_message_loader_get_buffer (transport->loader, &buffer);
+
   if (_dbus_auth_needs_decoding (transport->auth))
   {
+         bytes_read = kdbus_read_message(socket_transport,  &socket_transport->encoded_incoming, fds, &n_fds);
+
+      _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) == bytes_read);
+
+      if (bytes_read > 0)
+      {
+          if (!_dbus_auth_decode_data (transport->auth,
+                                       &socket_transport->encoded_incoming,
+                                       buffer))
+          {
+              _dbus_verbose ("Out of memory decoding incoming data\n");
+              _dbus_message_loader_return_buffer (transport->loader,
+                                              buffer,
+                                              _dbus_string_get_length (buffer));
+              oom = TRUE;
+              goto out;
+          }
+
+          _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
+          _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
+      }
+  }
+  else
+         bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
+
+  if (bytes_read >= 0 && n_fds > 0)
+    _dbus_verbose("Read %i unix fds\n", n_fds);
+
+  _dbus_message_loader_return_buffer (transport->loader,
+                                      buffer,
+                                      bytes_read < 0 ? 0 : _dbus_string_get_length (buffer));
+  _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
+
+ /* if (_dbus_auth_needs_decoding (transport->auth))
+  {
          bytes_read = kdbus_read_message(socket_transport,  &socket_transport->encoded_incoming);
 
       _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
@@ -1686,13 +1792,27 @@ do_reading (DBusTransport *transport)
   }
   else
   {
-      _dbus_message_loader_get_buffer (transport->loader,
-                                       &buffer);
-      bytes_read = kdbus_read_message(socket_transport, buffer);
+      int *fds, n_fds;
+
+      if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
+      {
+          _dbus_verbose ("Out of memory reading file descriptors\n");
+          _dbus_message_loader_return_buffer (transport->loader, buffer, 0);
+          oom = TRUE;
+          goto out;
+      }
+         _dbus_message_loader_get_buffer (transport->loader, &buffer);
+
+      bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
+
+      if (bytes_read >= 0 && n_fds > 0)
+        _dbus_verbose("Read %i unix fds\n", n_fds);
+
+      _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
       _dbus_message_loader_return_buffer (transport->loader,
                                           buffer,
                                           bytes_read < 0 ? 0 : bytes_read);
-  }
+  }*/
 
   if (bytes_read < 0)
     {
@@ -1791,11 +1911,14 @@ socket_handle_watch (DBusTransport *transport,
   if (watch == socket_transport->read_watch &&
       (flags & DBUS_WATCH_READABLE))
     {
+#ifdef DBUS_AUTHENTICATION
       dbus_bool_t auth_finished;
+#endif
 #if 1
       _dbus_verbose ("handling read watch %p flags = %x\n",
                      watch, flags);
 #endif
+#ifdef DBUS_AUTHENTICATION
       if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
         return FALSE;
 
@@ -1807,16 +1930,19 @@ socket_handle_watch (DBusTransport *transport,
        */
       if (!auth_finished)
        {
+#endif
          if (!do_reading (transport))
            {
              _dbus_verbose ("no memory to read\n");
              return FALSE;
            }
+#ifdef DBUS_AUTHENTICATION
        }
       else
         {
           _dbus_verbose ("Not reading anything since we just completed the authentication\n");
         }
+#endif
     }
   else if (watch == socket_transport->write_watch &&
            (flags & DBUS_WATCH_WRITABLE))
@@ -1825,9 +1951,10 @@ socket_handle_watch (DBusTransport *transport,
       _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
                      _dbus_connection_has_messages_to_send_unlocked (transport->connection));
 #endif
+#ifdef DBUS_AUTHENTICATION
       if (!do_authentication (transport, FALSE, TRUE, NULL))
         return FALSE;
-
+#endif
       if (!do_writing (transport))
         {
           _dbus_verbose ("no memory to write\n");
@@ -2029,17 +2156,20 @@ kdbus_do_iteration (DBusTransport *transport,
             {
               dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
               dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
-             dbus_bool_t authentication_completed;
+#ifdef DBUS_AUTHENTICATION
+              dbus_bool_t authentication_completed;
+#endif
 
               _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
                              need_read, need_write);
+#ifdef DBUS_AUTHENTICATION
               do_authentication (transport, need_read, need_write,
                                 &authentication_completed);
 
              /* See comment in socket_handle_watch. */
              if (authentication_completed)
                 goto out;
-
+#endif
               if (need_read && (flags & DBUS_ITERATION_DO_READING))
                 do_reading (transport);
               if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
@@ -2142,7 +2272,7 @@ _dbus_transport_new_for_socket_kdbus (int fd,
 
   /* These values should probably be tunable or something. */
   socket_transport->max_bytes_read_per_iteration = RECEIVE_POOL_SIZE;
-  socket_transport->max_bytes_written_per_iteration = MEMFD_POOL_SIZE;
+  socket_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
 
   socket_transport->kdbus_mmap_ptr = NULL;
   socket_transport->memfd = -1;
@@ -2399,14 +2529,14 @@ dbus_bool_t bus_register_kdbus(char* name, DBusConnection *connection, DBusError
        int fd;
 
        memset(&hello, 0, sizeof(hello));
-       hello.conn_flags = KDBUS_HELLO_ACCEPT_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;
+                          KDBUS_HELLO_ATTACH_AUDIT*/;
        hello.size = sizeof(struct kdbus_cmd_hello);
        hello.pool_size = RECEIVE_POOL_SIZE;
 
@@ -2516,7 +2646,7 @@ static int parse_match_key(const char *rule, const char* key, char** pValue)
                        {
                                if(strcmp(*pValue, "org.freedesktop.DBus") == 0)
                                        value_length = -1;
-                               _dbus_verbose ("founf for key: %s value:'%s'\n", key, *pValue);
+                               _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
                        }
                }
        }