[lib-fix] Fixed return value of kdbus_write_msg
[platform/upstream/dbus.git] / dbus / dbus-transport-kdbus.c
index 690c32c..67c85f3 100644 (file)
@@ -85,7 +85,7 @@ if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))   \
 
 #define MSG_ITEM_BUILD_VEC(data, datasize)                                    \
        item->type = KDBUS_ITEM_PAYLOAD_VEC;                                    \
-        item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);                \
+        item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec);                \
         item->vec.address = (unsigned long) data;                              \
         item->vec.size = datasize;
 
@@ -285,26 +285,26 @@ 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_PART_SIZE(sizeof(struct kdbus_memfd));
+        msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
     else
       {
-        msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));  //header is a must
+        msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));  //header is a must
         while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
           {
-            msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));
+            msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
             body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
           }
         if(body_size)
-          msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));
+          msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
       }
 
     if(fds_count)
-       msg_size += KDBUS_PART_SIZE(sizeof(int)*fds_count);
+       msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
 
     if (name)
-       msg_size += KDBUS_PART_SIZE(strlen(name) + 1);
+       msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
     else if (dst_id == KDBUS_DST_ID_BROADCAST)
-       msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size;
+       msg_size += KDBUS_ITEM_HEADER_SIZE + transport->bloom_size;
 
     msg = malloc(msg_size);
     if (!msg)
@@ -315,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);
 
@@ -337,7 +337,7 @@ static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t
  */
 static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message, const char* destination)
 {
-  struct kdbus_msg *msg;
+  struct kdbus_msg *msg = NULL;
   struct kdbus_item *item;
   uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
   const DBusString *header;
@@ -356,7 +356,14 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
       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 */
         {
+          errno = 0;
           dst_id = strtoull(&destination[3], NULL, 10);
+          if(errno)
+          {
+            _dbus_verbose("error: unique name is not a number: %s (%m)\n", destination);
+            ret_size = -1;
+            goto out;
+          }
           destination = NULL;
         }
     }
@@ -369,14 +376,22 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
   // check whether we can and should use memfd
   if((dst_id != KDBUS_DST_ID_BROADCAST) && (ret_size > MEMFD_SIZE_THRESHOLD))
     {
-      use_memfd = TRUE;
-      kdbus_init_memfd(transport);
+      if(kdbus_init_memfd(transport) == 0)
+        {
+          use_memfd = TRUE;
+        }
     }
 
   _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
 
   // init basic message fields
   msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport);
+  if(msg == NULL)
+    {
+      _dbus_verbose("Can't alloc memory for new message\n");
+      ret_size = -1;
+      goto out;
+    }
   msg->cookie = dbus_message_get_serial(message);
   autostart = dbus_message_get_auto_start (message);
   if(!autostart)
@@ -385,13 +400,14 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
   // build message contents
   item = msg->items;
 
-  if(use_memfd)
+  if(use_memfd == TRUE)
     {
       char *buf;
 
       if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0)
         {
           _dbus_verbose("memfd sealing failed: \n");
+          ret_size = -1;
           goto out;
         }
 
@@ -399,6 +415,7 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
       if (buf == MAP_FAILED)
         {
           _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
+          ret_size = -1;
           goto out;
         }
 
@@ -419,7 +436,7 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
       }
 
       item->type = KDBUS_ITEM_PAYLOAD_MEMFD;
-      item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
+      item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_memfd);
       item->memfd.size = ret_size;
       item->memfd.fd = transport->memfd;
     }
@@ -454,7 +471,7 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
     {
       item = KDBUS_PART_NEXT(item);
       item->type = KDBUS_ITEM_FDS;
-      item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
+      item->size = KDBUS_ITEM_HEADER_SIZE + (sizeof(int) * fds_count);
       memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
     }
 
@@ -462,14 +479,14 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
     {
       item = KDBUS_PART_NEXT(item);
       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);
+      item->size = KDBUS_ITEM_HEADER_SIZE + strlen(destination) + 1;
+      memcpy(item->str, destination, item->size - KDBUS_ITEM_HEADER_SIZE);
     }
   else if (dst_id == KDBUS_DST_ID_BROADCAST)
     {
       item = KDBUS_PART_NEXT(item);
       item->type = KDBUS_ITEM_BLOOM;
-      item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
+      item->size = KDBUS_ITEM_HEADER_SIZE + transport->bloom_size;
       strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
     }
 
@@ -480,25 +497,30 @@ static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message,
         goto again;
       else if(errno == ENXIO) //no such id on the bus
         {
+          ret_size = 0;
           if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
-            goto out;
+              goto out;
+
         }
       else if((errno == ESRCH) || (errno = EADDRNOTAVAIL))  //when well known name is not available on the bus
         {
+          ret_size = 0;
           if(autostart)
             {
               if(!reply_with_error(DBUS_ERROR_SERVICE_UNKNOWN, "The name %s was not provided by any .service files", dbus_message_get_destination(message), message, transport->base.connection))
-                goto out;
+                  goto out;
             }
           else
             if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
-              goto out;
+                goto out;
         }
+      else
+        ret_size = -1;
       _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
-      ret_size = -1;
     }
   out:
-  free(msg);
+  if(msg)
+    free(msg);
   if(use_memfd)
     close(transport->memfd);
 
@@ -518,6 +540,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 |
@@ -657,8 +680,7 @@ 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);
 
@@ -703,7 +725,7 @@ static int kdbus_message_size(const struct kdbus_msg* msg)
 
        KDBUS_PART_FOREACH(item, msg, items)
        {
-               if (item->size <= KDBUS_PART_HEADER_SIZE)
+               if (item->size < KDBUS_ITEM_HEADER_SIZE)
                {
                        _dbus_verbose("  +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
                        return -1;
@@ -743,8 +765,8 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
        DBusMessage *message = NULL;
        DBusMessageIter args;
        const char* emptyString = "";
-    const char* pString = NULL;
-       char dbus_name[(unsigned int)(snprintf((char*)pString, 0, ":1.%llu0", ULLONG_MAX))];
+       const char* pString = NULL;
+       char dbus_name[128];
        const char* pDBusName = dbus_name;
 #if KDBUS_MSG_DECODE_DEBUG == 1
        char buf[32];
@@ -762,10 +784,11 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
 
        KDBUS_PART_FOREACH(item, msg, items)
        {
-               if (item->size <= KDBUS_PART_HEADER_SIZE)
+               if (item->size < KDBUS_ITEM_HEADER_SIZE)
                {
                        _dbus_verbose("  +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
-                       break;  //??? continue (because dbus will find error) or break
+                       ret_size = -1;
+                       break;
                }
 
                switch (item->type)
@@ -813,7 +836,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                        {
                                int i;
 
-                               *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
+                               *n_fds = (item->size - KDBUS_ITEM_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]);
@@ -841,7 +864,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
 
                        case KDBUS_ITEM_CMDLINE:
                        case KDBUS_ITEM_NAME: {
-                               __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
+                               __u64 size = item->size - KDBUS_ITEM_HEADER_SIZE;
                                const char *str = item->str;
                                int count = 0;
 
@@ -871,10 +894,10 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
 
                                _dbus_verbose("  +%s (%llu bytes) len=%llu bytes)\n",
                                           enum_MSG(item->type), item->size,
-                                          (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
+                                          (unsigned long long)item->size - KDBUS_ITEM_HEADER_SIZE);
 
                                cap = item->data32;
-                               n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
+                               n = (item->size - KDBUS_ITEM_HEADER_SIZE) / 4 / sizeof(uint32_t);
 
                                _dbus_verbose("    CapInh=");
                                for (i = 0; i < n; i++)
@@ -932,10 +955,11 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                        break;
 
                        case KDBUS_ITEM_NAME_ADD:
-                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+                               _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)
@@ -957,10 +981,11 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                        break;
 
                        case KDBUS_ITEM_NAME_REMOVE:
-                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+                               _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)
@@ -982,10 +1007,11 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo
                        break;
 
                        case KDBUS_ITEM_NAME_CHANGE:
-                               _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+                               _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)
@@ -1368,6 +1394,12 @@ do_writing (DBusTransport *transport)
               goto out;
             }
         }
+      else if (bytes_written == 0)
+        {
+           _dbus_verbose ("Destination is not available\n");
+           _dbus_connection_message_sent_unlocked (transport->connection,
+                   message);
+        }
       else
         {
           _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,