*/
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;
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;
}
}
// 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)
// 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;
}
if (buf == MAP_FAILED)
{
_dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
+ ret_size = -1;
goto out;
}
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);
KDBUS_PART_FOREACH(item, msg, items)
{
- if (item->size <= KDBUS_ITEM_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;
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];
KDBUS_PART_FOREACH(item, msg, items)
{
- if (item->size <= KDBUS_ITEM_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)
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,