net/local: correct the return length of sendmsg

Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2021-08-20 19:34:55 +08:00 committed by Xiang Xiao
parent 68d6dbf86f
commit efaf72a1b1

View File

@ -70,38 +70,35 @@ static const uint8_t g_preamble[LOCAL_PREAMBLE_SIZE] =
* len Length of data to send * len Length of data to send
* *
* Returned Value: * Returned Value:
* Zero is returned on success; a negated errno value is returned on any * On success, the number of bytes written are returned (zero indicates
* failure. * nothing was written). On any failure, a negated errno value is returned
* *
****************************************************************************/ ****************************************************************************/
static int local_fifo_write(FAR struct file *filep, FAR const uint8_t *buf, static int local_fifo_write(FAR struct file *filep, FAR const uint8_t *buf,
size_t len) size_t len)
{ {
ssize_t nwritten; ssize_t nwritten = 0;
ssize_t ret;
while (len > 0) while (len != nwritten)
{ {
nwritten = file_write(filep, buf, len); ret = file_write(filep, buf + nwritten, len - nwritten);
if (nwritten < 0) if (ret < 0)
{ {
if (nwritten != -EINTR) if (ret != -EINTR)
{ {
nerr("ERROR: file_write failed: %zd\n", nwritten); nerr("ERROR: file_write failed: %zd\n", ret);
return (int)nwritten; break;
} }
ninfo("Ignoring signal\n"); continue;
}
else
{
DEBUGASSERT(nwritten > 0 && nwritten <= len);
len -= nwritten;
buf += nwritten;
}
} }
return OK; nwritten += ret;
}
return nwritten;
} }
/**************************************************************************** /****************************************************************************
@ -139,8 +136,11 @@ int local_send_packet(FAR struct file *filep, FAR const struct iovec *buf,
/* Send the packet preamble */ /* Send the packet preamble */
ret = local_fifo_write(filep, g_preamble, LOCAL_PREAMBLE_SIZE); ret = local_fifo_write(filep, g_preamble, LOCAL_PREAMBLE_SIZE);
if (ret == OK) if (ret != LOCAL_PREAMBLE_SIZE)
{ {
return ret;
}
/* Send the packet length */ /* Send the packet length */
for (len16 = 0, iov = buf; iov != end; iov++) for (len16 = 0, iov = buf; iov != end; iov++)
@ -150,12 +150,11 @@ int local_send_packet(FAR struct file *filep, FAR const struct iovec *buf,
ret = local_fifo_write(filep, (FAR const uint8_t *)&len16, ret = local_fifo_write(filep, (FAR const uint8_t *)&len16,
sizeof(uint16_t)); sizeof(uint16_t));
if (ret != OK) if (ret != sizeof(uint16_t))
{ {
return ret; return ret;
} }
} }
}
for (len16 = 0, iov = buf; iov != end; iov++) for (len16 = 0, iov = buf; iov != end; iov++)
{ {
@ -165,10 +164,17 @@ int local_send_packet(FAR struct file *filep, FAR const struct iovec *buf,
break; break;
} }
len16 += iov->iov_len; if (ret > 0)
{
len16 += ret;
if (ret != iov->iov_len)
{
break;
}
}
} }
return (ret == OK) ? len16 : ret; return (len16 > 0) ? len16 : ret;
} }
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */ #endif /* CONFIG_NET && CONFIG_NET_LOCAL */