mirror of https://gitee.com/openkylin/qemu.git
iov: handle partial writes from sendmsg and recvmsg
Partial writes can still happen in sendmsg and recvmsg, if a signal is received in the middle of a write. To handle this, retry the operation with a new offset/bytes pair. Reviewed-by: Juan Quintela <quintela@redhat.com> Reviewed-by: Orit Wassermann <owasserm@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
f48869ad28
commit
83f75c26e8
util
30
util/iov.c
30
util/iov.c
|
@ -144,18 +144,12 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
|
||||||
size_t offset, size_t bytes,
|
size_t offset, size_t bytes,
|
||||||
bool do_send)
|
bool do_send)
|
||||||
{
|
{
|
||||||
|
ssize_t total = 0;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
size_t orig_len, tail;
|
size_t orig_len, tail;
|
||||||
unsigned niov;
|
unsigned niov;
|
||||||
|
|
||||||
if (bytes == 0) {
|
while (bytes > 0) {
|
||||||
/* Catch the do-nothing case early, as otherwise we will pass an
|
|
||||||
* empty iovec to sendmsg/recvmsg(), and not all implementations
|
|
||||||
* accept this.
|
|
||||||
*/
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Find the start position, skipping `offset' bytes:
|
/* Find the start position, skipping `offset' bytes:
|
||||||
* first, skip all full-sized vector elements, */
|
* first, skip all full-sized vector elements, */
|
||||||
for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
|
for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
|
||||||
|
@ -163,7 +157,7 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* niov == iov_cnt would only be valid if bytes == 0, which
|
/* niov == iov_cnt would only be valid if bytes == 0, which
|
||||||
* we already ruled out above. */
|
* we already ruled out in the loop condition. */
|
||||||
assert(niov < iov_cnt);
|
assert(niov < iov_cnt);
|
||||||
iov += niov;
|
iov += niov;
|
||||||
iov_cnt -= niov;
|
iov_cnt -= niov;
|
||||||
|
@ -191,7 +185,7 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
|
||||||
|
|
||||||
ret = do_send_recv(sockfd, iov, niov, do_send);
|
ret = do_send_recv(sockfd, iov, niov, do_send);
|
||||||
|
|
||||||
/* Undo the changes above */
|
/* Undo the changes above before checking for errors */
|
||||||
if (tail) {
|
if (tail) {
|
||||||
iov[niov-1].iov_len = orig_len;
|
iov[niov-1].iov_len = orig_len;
|
||||||
}
|
}
|
||||||
|
@ -200,7 +194,21 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
|
||||||
iov[0].iov_len += offset;
|
iov[0].iov_len += offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
if (ret < 0) {
|
||||||
|
assert(errno != EINTR);
|
||||||
|
if (errno == EAGAIN && total > 0) {
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Prepare for the next iteration */
|
||||||
|
offset += ret;
|
||||||
|
total += ret;
|
||||||
|
bytes -= ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue