[lib-fix] moved dbus_connection_get_unix_user from daemon to library
[platform/upstream/dbus.git] / bus / kdbus-d.c
index 3e247be..a0975fd 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
-
 #include <dbus/dbus-connection-internal.h>
 #include "kdbus-d.h"
-#define KDBUS_FOR_SBB
 #include <dbus/kdbus.h>
 #include <dbus/dbus-bus.h>
 #include "dispatch.h"
-#include <dbus/kdbus-common.h>
 #include <dbus/dbus-transport.h>
 #include <dbus/dbus-transport-kdbus.h>
 #include "connection.h"
@@ -127,7 +124,7 @@ dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rul
   /*parsing rule and calculating size of command*/
   size = sizeof(struct kdbus_cmd_match);
   if(parse_match_key(rule, "interface='", &pInterface))       /*actual size is not important for interface because bloom size is defined by bus*/
-    size += KDBUS_PART_HEADER_SIZE + bloom_size;
+    size += KDBUS_ITEM_HEADER_SIZE + bloom_size;
   name_size = parse_match_key(rule, "sender='", &pName);
   if(name_size)
   {
@@ -154,14 +151,14 @@ dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rul
   if(pName)
   {
     pItem->type = KDBUS_MATCH_SRC_NAME;
-    pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
+    pItem->size = KDBUS_ITEM_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 + bloom_size;
+    pItem->size = KDBUS_ITEM_HEADER_SIZE + bloom_size;
     strncpy(pItem->data, pInterface, bloom_size);
   }
 
@@ -212,159 +209,106 @@ dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
   }
 }
 
-/**
- * Performs kdbus query of id of the given name
- *
- * @param name name to query for
- * @param transport transport
- * @param pInfo nameInfo structure address to store info about the name
- * @return 0 on success, -errno if failed
+/*
+ * Creates kdbus bus of given type.
  */
-int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
+char* make_kdbus_bus(DBusBusType type, const char* address, DBusError *error)
 {
-  struct kdbus_cmd_name_info *msg;
+  // TODO Function alloca() used. In upstream there was a patch proposing to
+  // replace alloca() with malloc() to assure memory alignment. If there will be
+  // suggestion to use malloc instead of alloca this function has to be modified
+  struct kdbus_cmd_make *bus_make;
   struct kdbus_item *item;
-  uint64_t size;
-  int ret;
-  uint64_t item_size;
-  int fd;
-
-  pInfo->sec_label_len = 0;
-  pInfo->sec_label = NULL;
-
-  if(!_dbus_transport_get_socket_fd(transport, &fd))
-    return -EPERM;
-
-  item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
-  item_size = (item_size < 56) ? 56 : item_size;  //at least 56 bytes are needed by kernel to place info about name, otherwise error
-  size = sizeof(struct kdbus_cmd_name_info) + item_size;
-
-  msg = malloc(size);
-  if (!msg)
-  {
-    _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
-    return -errno;
-  }
-
-  memset(msg, 0, size);
-  msg->size = size;
-    if((name[0] == ':') && (name[1] == '1') && (name[2] == '.'))  /* if name starts with ":1." it is a unique name and should be send as number */
-      msg->id = strtoull(&name[3], NULL, 10);
-    else
-      msg->id = 0;
+  __u64 name_size, bus_make_size;
+  int fdc, ret;
+  char *addr_value = NULL;
+  char *bus = NULL;
+  char *name = NULL;
+
+  if(type == DBUS_BUS_SYSTEM)
+    name_size = snprintf(name, 0, "%u-kdbus-%s", getuid(), "system") + 1;
+  else if(type == DBUS_BUS_SESSION)
+    name_size = snprintf(name, 0, "%u-kdbus", getuid()) + 1;
+  else
+    name_size = snprintf(name, 0, "%u-kdbus-%u", getuid(), getpid()) + 1;
 
-  item = msg->items;
-  item->type = KDBUS_NAME_INFO_ITEM_NAME;
-  item->size = item_size;
-  memcpy(item->str, name, strlen(name) + 1);
+  name = alloca(name_size);
+  if (!name)
+    {
+      return NULL;
+    }
 
-  again:
-  ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg);
-  if (ret < 0)
-  {
-    if(errno == EINTR)
-      goto again;
-    if(errno == EAGAIN)
-        goto again;
-    else if(ret == -ENOBUFS)
+  bus_make_size = sizeof(struct kdbus_cmd_make) + KDBUS_ITEM_SIZE(name_size) + KDBUS_ITEM_SIZE(sizeof(__u64));
+  bus_make = alloca(bus_make_size);
+  if (!bus_make)
     {
-      msg = realloc(msg, msg->size);  //prepare memory
-      if(msg != NULL)
-        goto again;
+      return NULL;
     }
-    pInfo->uniqueId = 0;
-    ret = -errno;
-  }
+
+  bus_make->size = bus_make_size;
+#ifdef POLICY_TO_KDBUS
+  bus_make->flags = KDBUS_MAKE_ACCESS_WORLD;
+#else
+  bus_make->flags = KDBUS_MAKE_ACCESS_WORLD | KDBUS_MAKE_POLICY_OPEN;
+#endif
+  item = bus_make->items;
+
+  item->type = KDBUS_ITEM_MAKE_NAME;
+  item->size = KDBUS_ITEM_HEADER_SIZE + name_size;
+  if(type == DBUS_BUS_SYSTEM)
+    sprintf(name, "%u-kdbus-%s", getuid(), "system");
+  else if(type == DBUS_BUS_SESSION)
+    sprintf(name, "%u-kdbus", getuid());
   else
-  {
-    pInfo->uniqueId = msg->id;
-    pInfo->userId = msg->creds.uid;
-    pInfo->processId = msg->creds.pid;
-    item = msg->items;
-    while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
+    sprintf(name, "%u-kdbus-%u", getuid(), getpid());
+  memcpy((bus_make->items)->str, name, name_size);
+
+  item = KDBUS_PART_NEXT(item);
+  item->type = KDBUS_ITEM_BLOOM_SIZE;
+  item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
+  item->data64[0] = 64;
+
+#ifdef KDBUS_FOR_SBB
+  addr_value = strchr(address, ':') + 1;
+  if(*addr_value)
     {
-      if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
-      {
-          pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
-        if(pInfo->sec_label_len != 0)
+      if(!strcmp(addr_value, "sbb"))
+        bus_make->flags |= KDBUS_MAKE_SBB_OFFSET;
+      else
         {
-          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);
+          dbus_set_error_const(error, DBUS_ERROR_BAD_ADDRESS, "Invalid address parameter.");
+          return NULL;
         }
-        break;
-      }
-      item = KDBUS_PART_NEXT(item);
     }
-  }
+#endif
 
-  free(msg);
-  return ret;
-}
-
-/*
- * Creates kdbus bus of given type.
- */
-char* make_kdbus_bus(DBusBusType type, const char* address, DBusError *error)
-{
-    struct {
-        struct kdbus_cmd_bus_make head;
-        uint64_t n_size;
-        uint64_t n_type;
-        char name[64];
-    } __attribute__ ((__aligned__(8))) bus_make;
-
-    int fdc, ret;
-    char *bus;
-    char *addr_value;
-
-    _dbus_verbose("Opening /dev/kdbus/control\n");
-    fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
-    if (fdc < 0)
+  _dbus_verbose("Opening /dev/kdbus/control\n");
+  fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
+  if (fdc < 0)
     {
-        _dbus_verbose("--- error %d (%m)\n", fdc);
-        dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
-        return NULL;
+      _dbus_verbose("--- error %d (%m)\n", fdc);
+      dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
+      return NULL;
     }
 
-    addr_value = strchr(address, ':') + 1;
-
-    memset(&bus_make, 0, sizeof(bus_make));
-    bus_make.head.bloom_size = 64;
-    bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
-    if(!strcmp(addr_value, "sbb"))
-      bus_make.head.flags |= KDBUS_MAKE_SBB_OFFSET;
-
-    if(type == DBUS_BUS_SYSTEM)
-        snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
-    else if(type == DBUS_BUS_SESSION)
-        snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
-    else
-        snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid());
-
-    bus_make.n_type = KDBUS_MAKE_NAME;
-    bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
-    bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
-
-    _dbus_verbose("Creating bus '%s'\n", bus_make.name);
-    ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
-    if (ret)
+  _dbus_verbose("Creating bus '%s'\n", (bus_make->items[0]).str);
+  ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, bus_make);
+  if (ret)
     {
-        _dbus_verbose("--- error %d (%m)\n", ret);
-        dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)", bus_make.name, fdc);
-        return NULL;
+      _dbus_verbose("--- error %d (%m)\n", errno);
+      dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)",
+          (bus_make->items[0]).str, errno);
+      return NULL;
     }
 
-    if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
+  if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", (bus_make->items[0]).str) < 0)
     {
-        BUS_SET_OOM (error);
-        return NULL;
+      BUS_SET_OOM (error);
+      return NULL;
     }
 
-    _dbus_verbose("Return value '%s'\n", bus);
-       return bus;
+  _dbus_verbose("Return value '%s'\n", bus);
+  return bus;
 }
 
 /*
@@ -382,7 +326,6 @@ static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusConnection* connection)
   uint64_t size;
   int fd;
   DBusTransport *transport;
-  const char* unique_name;
 
   transport = dbus_connection_get_transport(connection);
 
@@ -402,28 +345,28 @@ static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusConnection* connection)
       return FALSE;
     }
 
-  unique_name = dbus_bus_get_unique_name(connection);
-
-  pCmd_match->id = strtoull(&unique_name[3], NULL, 10);
+  pCmd_match->id = 0;
   pCmd_match->cookie = 1;
   pCmd_match->size = size;
+  pCmd_match->src_id = 0;
 
   pItem = pCmd_match->items;
-  pCmd_match->src_id = 0;
   pItem->type = KDBUS_MATCH_NAME_CHANGE;
-  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_NAME_ADD;
-  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_NAME_REMOVE;
-  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_ID_ADD;
-  pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
+  pItem->id = KDBUS_MATCH_SRC_ID_ANY;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_ID_REMOVE;
-  pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
+  pItem->id = KDBUS_MATCH_SRC_ID_ANY;
 
   if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
     {
@@ -479,8 +422,10 @@ dbus_bool_t register_daemon_name(DBusConnection* connection)
     BusTransaction *transaction;
 
     _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
+#ifdef POLICY_TO_KDBUS
     if(!register_kdbus_policy(DBUS_SERVICE_DBUS, dbus_connection_get_transport(connection), geteuid()))
       return FALSE;
+#endif
 
     if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
        return FALSE;
@@ -501,8 +446,12 @@ dbus_bool_t register_daemon_name(DBusConnection* connection)
     retval = TRUE;
 
 out:
-       bus_transaction_cancel_and_free(transaction);
-    return retval;
+  if(retval)
+    bus_transaction_execute_and_free(transaction);
+  else
+    bus_transaction_cancel_and_free(transaction);
+
+  return retval;
 }
 
 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
@@ -529,77 +478,85 @@ dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *s
 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
 {
        int fd;
-       struct kdbus_cmd_names* pCmd;
-       __u64 cmd_size;
+       struct kdbus_cmd_name_list __attribute__ ((__aligned__(8))) cmd;
+       struct kdbus_name_list *name_list;
+       struct kdbus_cmd_name *name;
+       DBusTransport *transport = dbus_connection_get_transport(connection);
        dbus_bool_t ret_val = FALSE;
        char** list;
        int list_len = 0;
        int i = 0;
        int j;
 
-       cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
-       pCmd = malloc(cmd_size);
-       if(pCmd == NULL)
-               goto out;
-       pCmd->size = cmd_size;
+       if(!_dbus_transport_get_socket_fd(transport, &fd))
+         return FALSE;
 
-       _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+  cmd.flags = KDBUS_NAME_LIST_NAMES | KDBUS_NAME_LIST_UNIQUE;
 
 again:
-       cmd_size = 0;
-       if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
+       if(ioctl(fd, KDBUS_CMD_NAME_LIST, &cmd))
        {
                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 FALSE;
-               goto again;                                             //and try again
+               }
        }
-       else
-       {
-               struct kdbus_cmd_name* pCmd_name;
 
-               for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
-                       list_len++;
+       name_list = (struct kdbus_name_list *)((char*)dbus_transport_get_pool_pointer(transport) + cmd.offset);
 
-               list = malloc(sizeof(char*) * (list_len + 1));
-               if(list == NULL)
-                       goto out;
+  for (name = name_list->names; (uint8_t *)(name) < (uint8_t *)(name_list) + name_list->size; name = KDBUS_PART_NEXT(name))
+    list_len++;
 
-               for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
-               {
-                       list[i] = strdup(pCmd_name->name);
-                       if(list[i] == NULL)
-                       {
-                               for(j=0; j<i; j++)
-                                       free(list[j]);
-                               free(list);
-                               goto out;
-                       }
-                       _dbus_verbose ("Name %d: %s\n", i, list[i]);
-                       ++i;
-               }
-               list[i] = NULL;
-       }
+  _dbus_verbose ("List len: %d\n", list_len);
+
+  list = malloc(sizeof(char*) * (list_len + 1));
+  if(list == NULL)
+    goto out;
+
+  for (name = name_list->names; (uint8_t *)(name) < (uint8_t *)(name_list) + name_list->size; name = KDBUS_PART_NEXT(name))
+  {
+      if(name->size > sizeof(struct kdbus_cmd_name) )
+      {
+        list[i] = strdup(name->name);
+        if(list[i] == NULL)
+          goto out;
+      }
+      else
+      {
+        list[i] = malloc(snprintf(list[i], 0, ":1.%llu0", (unsigned long long)name->id));
+        if(list[i] == NULL)
+          goto out;
+        sprintf(list[i], ":1.%llu", (unsigned long long int)name->id);
+      }
+    _dbus_verbose ("Name %d: %s\n", i, list[i]);
+    ++i;
+  }
 
+  list[i] = NULL;
        *array_len = list_len;
        *listp = list;
        ret_val = TRUE;
 
 out:
-       if(pCmd)
-               free(pCmd);
+  if (ioctl(fd, KDBUS_CMD_FREE, &cmd.offset) < 0)
+  {
+    if(errno == EINTR)
+      goto out;
+    _dbus_verbose("kdbus error freeing pool: %d (%m)\n", errno);
+    ret_val = FALSE;
+  }
+  if(ret_val == FALSE)
+    {
+      for(j=0; j<i; j++)
+        free(list[j]);
+      free(list);
+      *array_len = 0;
+      *listp = NULL;
+    }
+
        return ret_val;
 }
 
@@ -612,39 +569,38 @@ dbus_bool_t kdbus_list_queued (DBusConnection *connection, DBusList  **return_li
                                const char *name, DBusError  *error)
 {
   int fd;
-  struct kdbus_cmd_names* pCmd;
-  __u64 cmd_size;
   dbus_bool_t ret_val = FALSE;
   int name_length;
+  struct kdbus_cmd_conn_info *pCmd;
+  __u64 cmd_size;
+  DBusTransport *transport = dbus_connection_get_transport(connection);
+  struct kdbus_name_list *name_list;
+  struct kdbus_cmd_name *owner;
 
   _dbus_assert (*return_list == NULL);
 
   name_length = strlen(name) + 1;
-  cmd_size = sizeof(struct kdbus_cmd_names) + sizeof(struct kdbus_cmd_name) + name_length;
-  pCmd = malloc(cmd_size);
+  cmd_size = sizeof(struct kdbus_cmd_conn_info) + name_length;
+  pCmd = alloca(cmd_size);
   if(pCmd == NULL)
     goto out;
   pCmd->size = cmd_size;
-  pCmd->names[0].id = 0;
-  pCmd->names[0].size =  sizeof(struct kdbus_cmd_name) + name_length;
-  memcpy(pCmd->names[0].name, name, name_length);
-  _dbus_verbose ("Asking for queued owners of %s\n", pCmd->names[0].name);
+  pCmd->id = 0;
+  memcpy(pCmd->name, name, name_length);
 
-  _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+  _dbus_verbose ("Asking for queued owners of %s\n", pCmd->name);
+
+  _dbus_transport_get_socket_fd(transport, &fd);
 
   again:
-  cmd_size = 0;
   if(ioctl(fd, KDBUS_CMD_NAME_LIST_QUEUED, 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 if(errno == ENOENT)
+      else if(errno == ESRCH)
         {
           dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
                       "Could not get owners of name '%s': no such name", name);
-          free(pCmd);
           return FALSE;
         }
       else
@@ -653,36 +609,32 @@ dbus_bool_t kdbus_list_queued (DBusConnection *connection, DBusList  **return_li
           goto out;
         }
     }
-  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
-    {
-      struct kdbus_cmd_name* pCmd_name;
 
-      for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
-        {
-          char *uname = NULL;
+  name_list = (struct kdbus_name_list *)((char*)dbus_transport_get_pool_pointer(transport) + pCmd->offset);
 
-          _dbus_verbose ("Got queued owner id: %llu\n", (unsigned long long)pCmd_name->id);
-          uname = malloc(snprintf(uname, 0, ":1.%llu0", (unsigned long long)pCmd_name->id));
-          if(uname == NULL)
-            goto out;
-          sprintf(uname, ":1.%llu", (unsigned long long int)pCmd_name->id);
-          if (!_dbus_list_append (return_list, uname))
-            goto out;
-        }
+  for (owner = name_list->names; (uint8_t *)(owner) < (uint8_t *)(name_list) + name_list->size; owner = KDBUS_PART_NEXT(owner))
+    {
+      char *uname = NULL;
+
+      _dbus_verbose ("Got queued owner id: %llu\n", (unsigned long long)owner->id);
+      uname = malloc(snprintf(uname, 0, ":1.%llu0", (unsigned long long)owner->id));
+      if(uname == NULL)
+        goto out;
+      sprintf(uname, ":1.%llu", (unsigned long long int)owner->id);
+      if (!_dbus_list_append (return_list, uname))
+        goto out;
     }
 
   ret_val = TRUE;
 
   out:
-  if(pCmd)
-    free(pCmd);
+  if (ioctl(fd, KDBUS_CMD_FREE, &pCmd->offset) < 0)
+  {
+    if(errno == EINTR)
+      goto out;
+    _dbus_verbose("kdbus error freeing pool: %d (%m)\n", errno);
+    ret_val = FALSE;
+  }
   if(ret_val == FALSE)
     {
       DBusList *link;
@@ -720,7 +672,7 @@ dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* messa
 
        if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
        {
-             dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
+             dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%llu, %s",
                              sender_id, _dbus_strerror_from_errno ());
              return FALSE;
        }
@@ -741,7 +693,7 @@ dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message
 
        if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
        {
-             dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
+             dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%llu", sender_id);
              return FALSE;
        }
 
@@ -759,71 +711,13 @@ int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* own
     sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
     _dbus_verbose("Unique name discovered:%s\n", owner);
   }
-  else if(ret != -ENOENT)
-    _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
-
-  return ret;
-}
-
-/*
- *  Asks kdbus for uid of the owner of the name given in the message
- */
-dbus_bool_t kdbus_get_unix_user(DBusConnection* connection, const char* name, unsigned long* uid, DBusError* error)
-{
-  struct nameInfo info;
-  int inter_ret;
-  dbus_bool_t ret = FALSE;
-
-  inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
-  if(inter_ret == 0) //name found
-  {
-    _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
-    *uid = info.userId;
-    return TRUE;
-  }
-  else if(inter_ret == -ENOENT)  //name has no owner
-    {
-      _dbus_verbose ("Name %s has no owner.\n", name);
-      dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
-    }
-
-  else
-  {
-    _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
-    dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
-  }
+  else if((ret != -ENOENT) && (ret != -ENXIO))
+    _dbus_verbose("kdbus error sending name query: err %d (%m)\n", ret);
 
   return ret;
 }
 
 /*
- *  Asks kdbus for pid of the owner of the name given in the message
- */
-dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, const char* name, unsigned long* pid, DBusError* error)
-{
-       struct nameInfo info;
-       int inter_ret;
-       dbus_bool_t ret = FALSE;
-
-       inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
-       if(inter_ret == 0) //name found
-       {
-               _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
-               *pid = info.processId;
-               return TRUE;
-       }
-       else if(inter_ret == -ENOENT)  //name has no owner
-               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
-       else
-       {
-               _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
-               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
-       }
-
-       return ret;
-}
-
-/*
  *  Asks kdbus for selinux_security_context of the owner of the name given in the message
  */
 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
@@ -835,7 +729,7 @@ dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* c
 
        dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
        inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
-       if(inter_ret == -ENOENT)  //name has no owner
+       if((inter_ret == -ENOENT) || (inter_ret == -ENXIO)) //name has no owner
                dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
        else if(inter_ret < 0)
        {
@@ -855,57 +749,6 @@ dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* c
        return ret;
 }
 
-/**
- * Gets the UNIX user ID of the connection from kdbus, if known. Returns #TRUE if
- * the uid is filled in.  Always returns #FALSE on non-UNIX platforms
- * for now., though in theory someone could hook Windows to NIS or
- * something.  Always returns #FALSE prior to authenticating the
- * connection.
- *
- * The UID of is only read by bus daemon from kdbus. You can not
- * call this function from client side of the connection.
- *
- * You can ask the bus to tell you the UID of another connection though
- * if you like; this is done with dbus_bus_get_unix_user().
- *
- * @param connection the connection
- * @param uid return location for the user ID
- * @returns #TRUE if uid is filled in with a valid user ID
- */
-dbus_bool_t
-dbus_connection_get_unix_user (DBusConnection *connection,
-                               unsigned long  *uid)
-{
-  _dbus_return_val_if_fail (connection != NULL, FALSE);
-  _dbus_return_val_if_fail (uid != NULL, FALSE);
-
-  if(bus_context_is_kdbus(bus_connection_get_context (connection)))
-    return kdbus_get_unix_user(connection, bus_connection_get_name(connection), uid, NULL);
-
-  return dbus_connection_get_unix_user_dbus(connection, uid);
-}
-
-/**
- * Gets the process ID of the connection if any.
- * Returns #TRUE if the pid is filled in.
- *
- * @param connection the connection
- * @param pid return location for the process ID
- * @returns #TRUE if uid is filled in with a valid process ID
- */
-dbus_bool_t
-dbus_connection_get_unix_process_id (DBusConnection *connection,
-             unsigned long  *pid)
-{
-  _dbus_return_val_if_fail (connection != NULL, FALSE);
-  _dbus_return_val_if_fail (pid != NULL, FALSE);
-
-  if(bus_context_is_kdbus(bus_connection_get_context (connection)))
-    return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);
-
-  return dbus_connection_get_unix_process_id_dbus(connection, pid);
-}
-
 /*
  * Create connection structure for given name. It is needed to control starters - activatable services
  * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
@@ -928,6 +771,7 @@ DBusConnection* create_phantom_connection(DBusConnection* connection, const char
         dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
         goto out;
     }
+    dbus_bus_set_unique_name(phantom_connection, name);
     if(!bus_connection_complete(phantom_connection, &Sname, error))
     {
         bus_connection_disconnected(phantom_connection);
@@ -953,7 +797,6 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
     BusTransaction *transaction;
     DBusString name;
     DBusTransport* transport;
-    unsigned long int euid;
 
     transaction = bus_transaction_new (bus_connection_get_context(connection));
     if (transaction == NULL)
@@ -963,7 +806,6 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
         return FALSE;
 
     transport = dbus_connection_get_transport(connection);
-    euid = geteuid();
 
     if(!_dbus_transport_get_socket_fd (transport, &fd))
       return FALSE;
@@ -972,16 +814,18 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
 
     for(i=0; i<len; i++)
     {
-        if(!register_kdbus_policy(services[i], transport, euid))
+#ifdef POLICY_TO_KDBUS
+        if(!register_kdbus_policy(services[i], transport, geteuid()))
           goto out;
+#endif
 
-        if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
+        if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME) , 0) < 0)
             goto out;
 
         if(!_dbus_string_append(&name, services[i]))
                goto out;
         if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
-                       (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER), transaction, NULL))
+                       (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME), transaction, NULL))
                goto out;
         if(!_dbus_string_set_length(&name, 0))
                goto out;
@@ -996,8 +840,12 @@ out:
     }
     dbus_free_string_array (services);
     _dbus_string_free(&name);
+  if(retval)
+    bus_transaction_execute_and_free(transaction);
+  else
     bus_transaction_cancel_and_free(transaction);
-    return retval;
+
+  return retval;
 }
 
 /*
@@ -1053,8 +901,12 @@ dbus_bool_t update_kdbus_starters(DBusConnection* connection)
     retval = TRUE;
 
 out:
-       bus_transaction_cancel_and_free(transaction);
-    return retval;
+  if(retval)
+    bus_transaction_execute_and_free(transaction);
+  else
+    bus_transaction_cancel_and_free(transaction);
+
+  return retval;
 }
 
 /*