add qemu_send_full and qemu_recv_full

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2011-09-17 16:27:59 +02:00
parent 3799ce4ab6
commit 993295fedc
2 changed files with 71 additions and 0 deletions

67
osdep.c
View File

@ -166,3 +166,70 @@ int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
return ret;
}
/*
* A variant of send(2) which handles partial write.
*
* Return the number of bytes transferred, which is only
* smaller than `count' if there is an error.
*
* This function won't work with non-blocking fd's.
* Any of the possibilities with non-bloking fd's is bad:
* - return a short write (then name is wrong)
* - busy wait adding (errno == EAGAIN) to the loop
*/
ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
{
ssize_t ret = 0;
ssize_t total = 0;
while (count) {
ret = send(fd, buf, count, flags);
if (ret < 0) {
if (errno == EINTR) {
continue;
}
break;
}
count -= ret;
buf += ret;
total += ret;
}
return total;
}
/*
* A variant of recv(2) which handles partial write.
*
* Return the number of bytes transferred, which is only
* smaller than `count' if there is an error.
*
* This function won't work with non-blocking fd's.
* Any of the possibilities with non-bloking fd's is bad:
* - return a short write (then name is wrong)
* - busy wait adding (errno == EAGAIN) to the loop
*/
ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
{
ssize_t ret = 0;
ssize_t total = 0;
while (count) {
ret = qemu_recv(fd, buf, count, flags);
if (ret <= 0) {
if (ret < 0 && errno == EINTR) {
continue;
}
break;
}
count -= ret;
buf += ret;
total += ret;
}
return total;
}

View File

@ -173,6 +173,10 @@ void *qemu_oom_check(void *ptr);
int qemu_open(const char *name, int flags, ...);
ssize_t qemu_write_full(int fd, const void *buf, size_t count)
QEMU_WARN_UNUSED_RESULT;
ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
QEMU_WARN_UNUSED_RESULT;
ssize_t qemu_recv_full(int fd, const void *buf, size_t count, int flags)
QEMU_WARN_UNUSED_RESULT;
void qemu_set_cloexec(int fd);
#ifndef _WIN32