Changes in kdbus transport arising from changing of kdus.h
[platform/upstream/dbus.git] / dbus / dbus-transport-kdbus.c
index 824f822..902116d 100644 (file)
 #include <limits.h>
 #include <sys/stat.h>
 
-#define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU) //size of the memory area for received non-memfd messages
-#define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used to send (if it is not broadcast)
-//todo add compilation-time check if MEMFD_SIZE_THERSHOLD is lower than max payload vector size defined in kdbus.h
+/**
+ * @defgroup DBusTransportKdbus DBusTransport implementations for kdbus
+ * @ingroup  DBusInternals
+ * @brief Implementation details of DBusTransport on kdbus
+ *
+ * @{
+ */
+
+/** Size of the memory area for received non-memfd messages. */
+#define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU)
+
+/** Over this memfd is used to send (if it is not broadcast). */
+#define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU)
+
+/** Define max bytes read or written in one iteration.
+* This is to avoid blocking on reading or writing for too long. It is checked after each message is sent or received,
+* so if message is bigger than MAX_BYTES_PER_ITERATION it will be handled in one iteration, but sending/writing
+* will break after that message.
+**/
+#define MAX_BYTES_PER_ITERATION 16384
+
+#if (MEMFD_SIZE_THRESHOLD > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
+  #error  Memfd size threshold higher than max kdbus message payload vector size
+#endif
 
+/** Enables verbosing more information about kdbus message.
+ *  Works only if DBUS_VERBOSE=1 is used.
+ */
 #define KDBUS_MSG_DECODE_DEBUG 0
 
 #define ITER_APPEND_STR(string) \
@@ -60,7 +84,7 @@ if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))   \
 }\
 
 #define MSG_ITEM_BUILD_VEC(data, datasize)                                    \
-       item->type = KDBUS_MSG_PAYLOAD_VEC;                                     \
+       item->type = KDBUS_ITEM_PAYLOAD_VEC;                                    \
         item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);                \
         item->vec.address = (unsigned long) data;                              \
         item->vec.size = datasize;
@@ -112,6 +136,17 @@ __u64 dbus_transport_get_bloom_size(DBusTransport* transport)
 }
 
 /**
+ *  Gets pointer to the memory pool, wher received messages are
+ *  placed and some ioctls return their info
+ *  @param transport transport
+ *  @returns pointer to the pool
+ */
+void* dbus_transport_get_pool_pointer(DBusTransport* transport)
+{
+  return ((DBusTransportKdbus*)transport)->kdbus_mmap_ptr;
+}
+
+/**
  * Puts locally generated message into received messages queue
  * @param message message that will be added
  * @param connection connection to which message will be added
@@ -136,7 +171,7 @@ static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection*
 /**
  * Generates local error message as a reply to message given as parameter
  * and adds generated error message to received messages queue.
- * @param error_Type type of error, preferably DBUS_ERROR_(...)
+ * @param error_type type of error, preferably DBUS_ERROR_(...)
  * @param template Template of error description. It can has formatting
  *       characters to print object string into it. Can be NULL.
  * @param object String to print into error description. Can be NULL.
@@ -250,24 +285,24 @@ static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t
     msg_size = sizeof(struct kdbus_msg);
 
     if(use_memfd == TRUE)  // bulk data - memfd
-        msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
+        msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_memfd));
     else
       {
-        msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));  //header is a must
+        msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));  //header is a must
         while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
           {
-            msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
+            msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));
             body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
           }
         if(body_size)
-          msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
+          msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));
       }
 
     if(fds_count)
-       msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
+       msg_size += KDBUS_PART_SIZE(sizeof(int)*fds_count);
 
     if (name)
-       msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
+       msg_size += KDBUS_PART_SIZE(strlen(name) + 1);
     else if (dst_id == KDBUS_DST_ID_BROADCAST)
        msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size;
 
@@ -280,7 +315,7 @@ static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t
 
     memset(msg, 0, msg_size);
     msg->size = msg_size;
-    msg->payload_type = KDBUS_PAYLOAD_DBUS1;
+    msg->payload_type = KDBUS_PAYLOAD_DBUS;
     msg->dst_id = name ? 0 : dst_id;
     msg->src_id = strtoull(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
 
@@ -318,7 +353,7 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
   // determine destination and destination id
   if(destination)
     {
-      dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
+      dst_id = KDBUS_DST_ID_NAME;
       if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.'))  /* if name starts with ":1." it is a unique name and should be send as number */
         {
           dst_id = strtoull(&destination[3], NULL, 10);
@@ -383,7 +418,7 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
           goto out;
       }
 
-      item->type = KDBUS_MSG_PAYLOAD_MEMFD;
+      item->type = KDBUS_ITEM_PAYLOAD_MEMFD;
       item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
       item->memfd.size = ret_size;
       item->memfd.fd = transport->memfd;
@@ -418,7 +453,7 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
   if(fds_count)
     {
       item = KDBUS_PART_NEXT(item);
-      item->type = KDBUS_MSG_FDS;
+      item->type = KDBUS_ITEM_FDS;
       item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
       memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
     }
@@ -426,14 +461,14 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
   if (destination)
     {
       item = KDBUS_PART_NEXT(item);
-      item->type = KDBUS_MSG_DST_NAME;
+      item->type = KDBUS_ITEM_DST_NAME;
       item->size = KDBUS_PART_HEADER_SIZE + strlen(destination) + 1;
       memcpy(item->str, destination, item->size - KDBUS_PART_HEADER_SIZE);
     }
   else if (dst_id == KDBUS_DST_ID_BROADCAST)
     {
       item = KDBUS_PART_NEXT(item);
-      item->type = KDBUS_MSG_BLOOM;
+      item->type = KDBUS_ITEM_BLOOM;
       item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
       strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
     }
@@ -483,6 +518,7 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
 static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS)
 {
        struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
+       memset(&hello, 0, sizeof(hello));
 
        hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
                           KDBUS_HELLO_ATTACH_COMM |
@@ -527,33 +563,39 @@ static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS
  *                     0 if Hello message was handled correctly,
  *                     -1 if Hello message was not handle correctly.
  */
-static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
+static int capture_hello_message(DBusTransport *transport, const char* destination, DBusMessage *message)
 {
-  if(!strcmp(dbus_message_get_member(message), "Hello"))
+  if(!strcmp(destination, DBUS_SERVICE_DBUS))
     {
-      char* name = NULL;
-
-      name = malloc(snprintf(name, 0, ":1.%llu0", ULLONG_MAX));
-      if(name == NULL)
-        return -1;
-      strcpy(name, ":1.");
-      if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport))
-        goto out;
-      if(!register_kdbus_policy(&name[3], transport, geteuid()))
-        goto out;
+      if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
+        {
+          if(!strcmp(dbus_message_get_member(message), "Hello"))
+            {
+              char* name = NULL;
 
-      ((DBusTransportKdbus*)transport)->sender = name;
+              name = malloc(snprintf(name, 0, ":1.%llu0", ULLONG_MAX));
+              if(name == NULL)
+                return -1;
+              strcpy(name, ":1.");
+              if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport))
+                goto out;
+#ifdef POLICY_TO_KDBUS
+              if(!register_kdbus_policy(&name[3], transport, geteuid()))
+                goto out;
+#endif
+              ((DBusTransportKdbus*)transport)->sender = name;
 
-      if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
-        return 0;
+              if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
+                return 0;  //on success we can not free name
 
-    out:
-      free(name);
+              out:
+              free(name);
+              return -1;
+            }
+        }
     }
-  else
-    return 1;  //send to daemon
 
-  return -1;
+  return 1;  //send message to daemon
 }
 
 #if KDBUS_MSG_DECODE_DEBUG == 1
@@ -586,38 +628,37 @@ struct kdbus_enum_table {
        }
 const char *enum_MSG(long long id);
 TABLE(MSG) = {
-       ENUM(_KDBUS_MSG_NULL),
-       ENUM(KDBUS_MSG_PAYLOAD_VEC),
-       ENUM(KDBUS_MSG_PAYLOAD_OFF),
-       ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
-       ENUM(KDBUS_MSG_FDS),
-       ENUM(KDBUS_MSG_BLOOM),
-       ENUM(KDBUS_MSG_DST_NAME),
-       ENUM(KDBUS_MSG_SRC_CREDS),
-       ENUM(KDBUS_MSG_SRC_PID_COMM),
-       ENUM(KDBUS_MSG_SRC_TID_COMM),
-       ENUM(KDBUS_MSG_SRC_EXE),
-       ENUM(KDBUS_MSG_SRC_CMDLINE),
-       ENUM(KDBUS_MSG_SRC_CGROUP),
-       ENUM(KDBUS_MSG_SRC_CAPS),
-       ENUM(KDBUS_MSG_SRC_SECLABEL),
-       ENUM(KDBUS_MSG_SRC_AUDIT),
-       ENUM(KDBUS_MSG_SRC_NAMES),
-       ENUM(KDBUS_MSG_TIMESTAMP),
-       ENUM(KDBUS_MSG_NAME_ADD),
-       ENUM(KDBUS_MSG_NAME_REMOVE),
-       ENUM(KDBUS_MSG_NAME_CHANGE),
-       ENUM(KDBUS_MSG_ID_ADD),
-       ENUM(KDBUS_MSG_ID_REMOVE),
-       ENUM(KDBUS_MSG_REPLY_TIMEOUT),
-       ENUM(KDBUS_MSG_REPLY_DEAD),
+       ENUM(_KDBUS_ITEM_NULL),
+       ENUM(KDBUS_ITEM_PAYLOAD_VEC),
+       ENUM(KDBUS_ITEM_PAYLOAD_OFF),
+       ENUM(KDBUS_ITEM_PAYLOAD_MEMFD),
+       ENUM(KDBUS_ITEM_FDS),
+       ENUM(KDBUS_ITEM_BLOOM),
+       ENUM(KDBUS_ITEM_DST_NAME),
+       ENUM(KDBUS_ITEM_CREDS),
+       ENUM(KDBUS_ITEM_PID_COMM),
+       ENUM(KDBUS_ITEM_TID_COMM),
+       ENUM(KDBUS_ITEM_EXE),
+       ENUM(KDBUS_ITEM_CMDLINE),
+       ENUM(KDBUS_ITEM_CGROUP),
+       ENUM(KDBUS_ITEM_CAPS),
+       ENUM(KDBUS_ITEM_SECLABEL),
+       ENUM(KDBUS_ITEM_AUDIT),
+       ENUM(KDBUS_ITEM_NAME),
+       ENUM(KDBUS_ITEM_TIMESTAMP),
+       ENUM(KDBUS_ITEM_NAME_ADD),
+       ENUM(KDBUS_ITEM_NAME_REMOVE),
+       ENUM(KDBUS_ITEM_NAME_CHANGE),
+       ENUM(KDBUS_ITEM_ID_ADD),
+       ENUM(KDBUS_ITEM_ID_REMOVE),
+       ENUM(KDBUS_ITEM_REPLY_TIMEOUT),
+       ENUM(KDBUS_ITEM_REPLY_DEAD),
 };
 LOOKUP(MSG);
 const char *enum_PAYLOAD(long long id);
 TABLE(PAYLOAD) = {
        ENUM(KDBUS_PAYLOAD_KERNEL),
-       ENUM(KDBUS_PAYLOAD_DBUS1),
-       ENUM(KDBUS_PAYLOAD_GVARIANT),
+       ENUM(KDBUS_PAYLOAD_DBUS),
 };
 LOOKUP(PAYLOAD);
 
@@ -669,10 +710,10 @@ static int kdbus_message_size(const struct kdbus_msg* msg)
                }
                switch (item->type)
                {
-                       case KDBUS_MSG_PAYLOAD_OFF:
+                       case KDBUS_ITEM_PAYLOAD_OFF:
                                ret_size += item->vec.size;
                                break;
-                       case KDBUS_MSG_PAYLOAD_MEMFD:
+                       case KDBUS_ITEM_PAYLOAD_MEMFD:
                                ret_size += item->memfd.size;
                                break;
                        default:
@@ -729,7 +770,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
 
                switch (item->type)
                {
-                       case KDBUS_MSG_PAYLOAD_OFF:
+                       case KDBUS_ITEM_PAYLOAD_OFF:
                                memcpy(data, (char *)kdbus_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size);
                                data += item->vec.size;
                                ret_size += item->vec.size;
@@ -740,7 +781,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                        (unsigned long long)item->vec.size);
                        break;
 
-                       case KDBUS_MSG_PAYLOAD_MEMFD:
+                       case KDBUS_ITEM_PAYLOAD_MEMFD:
                        {
                                char *buf;
                                uint64_t size;
@@ -768,7 +809,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                        break;
                        }
 
-                       case KDBUS_MSG_FDS:
+                       case KDBUS_ITEM_FDS:
                        {
                                int i;
 
@@ -780,7 +821,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                        }
 
 #if KDBUS_MSG_DECODE_DEBUG == 1
-                       case KDBUS_MSG_SRC_CREDS:
+                       case KDBUS_ITEM_CREDS:
                                _dbus_verbose("  +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
                                        enum_MSG(item->type), item->size,
                                        item->creds.uid, item->creds.gid,
@@ -788,18 +829,18 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                        item->creds.starttime);
                        break;
 
-                       case KDBUS_MSG_SRC_PID_COMM:
-                       case KDBUS_MSG_SRC_TID_COMM:
-                       case KDBUS_MSG_SRC_EXE:
-                       case KDBUS_MSG_SRC_CGROUP:
-                       case KDBUS_MSG_SRC_SECLABEL:
-                       case KDBUS_MSG_DST_NAME:
+                       case KDBUS_ITEM_PID_COMM:
+                       case KDBUS_ITEM_TID_COMM:
+                       case KDBUS_ITEM_EXE:
+                       case KDBUS_ITEM_CGROUP:
+                       case KDBUS_ITEM_SECLABEL:
+                       case KDBUS_ITEM_DST_NAME:
                                _dbus_verbose("  +%s (%llu bytes) '%s' (%zu)\n",
                                           enum_MSG(item->type), item->size, item->str, strlen(item->str));
                                break;
 
-                       case KDBUS_MSG_SRC_CMDLINE:
-                       case KDBUS_MSG_SRC_NAMES: {
+                       case KDBUS_ITEM_CMDLINE:
+                       case KDBUS_ITEM_NAME: {
                                __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
                                const char *str = item->str;
                                int count = 0;
@@ -816,14 +857,14 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                break;
                        }
 
-                       case KDBUS_MSG_SRC_AUDIT:
+                       case KDBUS_ITEM_AUDIT:
                                _dbus_verbose("  +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
                                           enum_MSG(item->type), item->size,
                                           (unsigned long long)item->data64[0],
                                           (unsigned long long)item->data64[1]);
                                break;
 
-                       case KDBUS_MSG_SRC_CAPS: {
+                       case KDBUS_ITEM_CAPS: {
                                int n;
                                const uint32_t *cap;
                                int i;
@@ -854,7 +895,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                break;
                        }
 
-                       case KDBUS_MSG_TIMESTAMP:
+                       case KDBUS_ITEM_TIMESTAMP:
                                _dbus_verbose("  +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
                                           enum_MSG(item->type), item->size,
                                           (unsigned long long)item->timestamp.realtime_ns,
@@ -862,7 +903,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                break;
 #endif
 
-                       case KDBUS_MSG_REPLY_TIMEOUT:
+                       case KDBUS_ITEM_REPLY_TIMEOUT:
                                _dbus_verbose("  +%s (%llu bytes) cookie=%llu\n",
                                           enum_MSG(item->type), item->size, msg->cookie_reply);
 
@@ -876,7 +917,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                ret_size = put_message_into_data(message, data);
                        break;
 
-                       case KDBUS_MSG_REPLY_DEAD:
+                       case KDBUS_ITEM_REPLY_DEAD:
                                _dbus_verbose("  +%s (%llu bytes) cookie=%llu\n",
                                           enum_MSG(item->type), item->size, msg->cookie_reply);
 
@@ -890,11 +931,12 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                ret_size = put_message_into_data(message, data);
                        break;
 
-                       case KDBUS_MSG_NAME_ADD:
-                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+                       case KDBUS_ITEM_NAME_ADD:
+                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, old flags=0x%llx, new flags=0x%llx\n",
                                        enum_MSG(item->type), (unsigned long long) item->size,
                                        item->name_change.name, item->name_change.old_id,
-                                       item->name_change.new_id, item->name_change.flags);
+                                       item->name_change.new_id, item->name_change.old_flags,
+                                       item->name_change.new_flags);
 
                                message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
                                if(message == NULL)
@@ -915,11 +957,12 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                ret_size = put_message_into_data(message, data);
                        break;
 
-                       case KDBUS_MSG_NAME_REMOVE:
-                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+                       case KDBUS_ITEM_NAME_REMOVE:
+                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, old flags=0x%llx, new flags=0x%llx\n",
                                        enum_MSG(item->type), (unsigned long long) item->size,
                                        item->name_change.name, item->name_change.old_id,
-                                       item->name_change.new_id, item->name_change.flags);
+                                       item->name_change.new_id, item->name_change.old_flags,
+                                       item->name_change.new_flags);
 
                                message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal
                                if(message == NULL)
@@ -940,11 +983,12 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                ret_size = put_message_into_data(message, data);
                        break;
 
-                       case KDBUS_MSG_NAME_CHANGE:
-                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+                       case KDBUS_ITEM_NAME_CHANGE:
+                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, old flags=0x%llx, new flags=0x%llx\n",
                                        enum_MSG(item->type), (unsigned long long) item->size,
                                        item->name_change.name, item->name_change.old_id,
-                                       item->name_change.new_id, item->name_change.flags);
+                                       item->name_change.new_id, item->name_change.old_flags,
+                                       item->name_change.new_flags);
 
                                message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
                                if(message == NULL)
@@ -967,7 +1011,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                ret_size = put_message_into_data(message, data);
                        break;
 
-                       case KDBUS_MSG_ID_ADD:
+                       case KDBUS_ITEM_ID_ADD:
                                _dbus_verbose("  +%s (%llu bytes) id=%llu flags=%llu\n",
                                           enum_MSG(item->type), (unsigned long long) item->size,
                                           (unsigned long long) item->id_change.id,
@@ -990,7 +1034,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                                ret_size = put_message_into_data(message, data);
                        break;
 
-                       case KDBUS_MSG_ID_REMOVE:
+                       case KDBUS_ITEM_ID_REMOVE:
                                _dbus_verbose("  +%s (%llu bytes) id=%llu flags=%llu\n",
                                           enum_MSG(item->type), (unsigned long long) item->size,
                                           (unsigned long long) item->id_change.id,
@@ -1094,7 +1138,7 @@ static int kdbus_read_message(DBusTransportKdbus *kdbus_transport, DBusString *b
        }
 
        again2:
-       if (ioctl(kdbus_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
+       if (ioctl(kdbus_transport->fd, KDBUS_CMD_FREE, &offset) < 0)
        {
                if(errno == EINTR)
                        goto again2;
@@ -1291,26 +1335,20 @@ do_writing (DBusTransport *transport)
 
       if(pDestination)
         {
-          if(!strcmp(pDestination, DBUS_SERVICE_DBUS))
+          int ret;
+
+          ret = capture_hello_message(transport, pDestination, message);
+          if(ret < 0)  //error
             {
-              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
-                }
+              bytes_written = -1;
+              goto written;
             }
+          else if(ret == 0)  //hello message captured and handled correctly
+            {
+              bytes_written = total_bytes_to_write;
+              goto written;
+            }
+          //else send as regular message
         }
 
       bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination);
@@ -1540,7 +1578,8 @@ kdbus_handle_watch (DBusTransport *transport,
 }
 
 /**
- * Copy-paste from socket transport renamed to kdbus_transport.
+ * Copy-paste from socket transport, but socket_transport renamed to kdbus_transport
+ * and _dbus_close_socket replaced with close().
  */
 static void
 kdbus_disconnect (DBusTransport *transport)
@@ -1551,7 +1590,13 @@ kdbus_disconnect (DBusTransport *transport)
 
   free_watches (transport);
 
-  _dbus_close_socket (kdbus_transport->fd, NULL);
+  again:
+   if (close (kdbus_transport->fd) < 0)
+     {
+       if (errno == EINTR)
+         goto again;
+     }
+
   kdbus_transport->fd = -1;
 }
 
@@ -1623,60 +1668,39 @@ kdbus_do_iteration (DBusTransport *transport,
                  kdbus_transport->write_watch,
                  kdbus_transport->fd);
 
-  /* the passed in DO_READING/DO_WRITING flags indicate whether to
-   * read/write messages, but regardless of those we may need to block
-   * for reading/writing to do auth.  But if we do reading for auth,
-   * we don't want to read any messages yet if not given DO_READING.
-   */
-
    poll_fd.fd = kdbus_transport->fd;
    poll_fd.events = 0;
 
-   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
-       * echo client/server.
-       *
-       * If both reading and writing were requested, we want to avoid this
-       * since it could have funky effects:
-       *   - both ends spinning waiting for the other one to read
-       *     data so they can finish writing
-       *   - prioritizing all writing ahead of reading
-       */
-      if ((flags & DBUS_ITERATION_DO_WRITING) &&
-          !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
-          !transport->disconnected &&
-          _dbus_connection_has_messages_to_send_unlocked (transport->connection))
-      {
-         do_writing (transport);
-
-         if (transport->disconnected ||
-              !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
-            goto out;
-      }
-
-      /* If we get here, we decided to do the poll() after all */
-      _dbus_assert (kdbus_transport->read_watch);
-      if (flags & DBUS_ITERATION_DO_READING)
-            poll_fd.events |= _DBUS_POLLIN;
-
-      _dbus_assert (kdbus_transport->write_watch);
-      if (flags & DBUS_ITERATION_DO_WRITING)
-         poll_fd.events |= _DBUS_POLLOUT;
-   }
-   else
-   {
-      DBusAuthState auth_state;
-
-      auth_state = _dbus_auth_do_work (transport->auth);
-
-      if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
-            poll_fd.events |= _DBUS_POLLIN;
-
-      if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
-            poll_fd.events |= _DBUS_POLLOUT;
-   }
+   /* 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
+    * echo client/server.
+    *
+    * If both reading and writing were requested, we want to avoid this
+    * since it could have funky effects:
+    *   - both ends spinning waiting for the other one to read
+    *     data so they can finish writing
+    *   - prioritizing all writing ahead of reading
+    */
+   if ((flags & DBUS_ITERATION_DO_WRITING) &&
+       !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
+       !transport->disconnected &&
+       _dbus_connection_has_messages_to_send_unlocked (transport->connection))
+     {
+       do_writing (transport);
+
+       if (transport->disconnected ||
+           !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
+         goto out;
+     }
+
+   /* If we get here, we decided to do the poll() after all */
+   _dbus_assert (kdbus_transport->read_watch);
+   if (flags & DBUS_ITERATION_DO_READING)
+     poll_fd.events |= _DBUS_POLLIN;
+
+   _dbus_assert (kdbus_transport->write_watch);
+   if (flags & DBUS_ITERATION_DO_WRITING)
+     poll_fd.events |= _DBUS_POLLOUT;
 
    if (poll_fd.events)
    {
@@ -1700,10 +1724,7 @@ kdbus_do_iteration (DBusTransport *transport,
       poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
 
       if (poll_res < 0 && _dbus_get_is_errno_eintr ())
-      {
-         _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
-        goto again;
-      }
+        goto again;
 
       if (flags & DBUS_ITERATION_BLOCK)
       {
@@ -1803,7 +1824,7 @@ static const DBusTransportVTable kdbus_vtable = {
  * @returns the new transport, or #NULL if no memory.
  */
 static DBusTransport*
-_dbus_transport_new_kdbus_transport (int fd, const DBusString *address)
+new_kdbus_transport (int fd, const DBusString *address)
 {
        DBusTransportKdbus *kdbus_transport;
 
@@ -1833,8 +1854,8 @@ _dbus_transport_new_kdbus_transport (int fd, const DBusString *address)
   kdbus_transport->fd = fd;
 
   /* These values should probably be tunable or something. */
-  kdbus_transport->max_bytes_read_per_iteration = 16384;
-  kdbus_transport->max_bytes_written_per_iteration = 16384;
+  kdbus_transport->max_bytes_read_per_iteration = MAX_BYTES_PER_ITERATION;
+  kdbus_transport->max_bytes_written_per_iteration = MAX_BYTES_PER_ITERATION;
 
   kdbus_transport->kdbus_mmap_ptr = NULL;
   kdbus_transport->memfd = -1;
@@ -1911,7 +1932,7 @@ static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError
 
        _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
 
-       transport = _dbus_transport_new_kdbus_transport (fd, &address);
+       transport = new_kdbus_transport (fd, &address);
        if (transport == NULL)
     {
                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
@@ -1923,10 +1944,15 @@ static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError
        return transport;
 
        failed_1:
-               _dbus_close_socket (fd, NULL);
-       failed_0:
-               _dbus_string_free (&address);
-       return NULL;
+  again:
+   if (close (fd) < 0)
+     {
+       if (errno == EINTR)
+         goto again;
+     }
+  failed_0:
+       _dbus_string_free (&address);
+  return NULL;
 }
 
 
@@ -1976,3 +2002,5 @@ DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry  *entry,
                return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
     }
 }
+
+/** @} */