* [Qemu-devel] [PATCH] Add qemu_read_full
@ 2010-11-24 17:18 M. Mohan Kumar
2010-11-24 19:21 ` Stefan Hajnoczi
0 siblings, 1 reply; 2+ messages in thread
From: M. Mohan Kumar @ 2010-11-24 17:18 UTC (permalink / raw)
To: qemu-devel@nongnu.org
Add qemu_read_full function
Signed-off-by: M. Mohan Kumar <mohan@in.ibm.com>
---
osdep.c | 29 +++++++++++++++++++++++++++++
qemu-common.h | 2 ++
2 files changed, 31 insertions(+), 0 deletions(-)
diff --git a/osdep.c b/osdep.c
index 327583b..7046b32 100644
--- a/osdep.c
+++ b/osdep.c
@@ -127,6 +127,35 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)
}
/*
+ * A variant of read(2) which handles interrupted read.
+ * Simlar to qemu_write_full function
+ *
+ * Return the number of bytes read.
+ *
+ */
+ssize_t qemu_read_full(int fd, void *buf, size_t count)
+{
+ ssize_t ret = 0;
+ ssize_t total = 0;
+
+ while (count) {
+ ret = read(fd, buf, count);
+ if (ret < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ break;
+ }
+
+ count -= ret;
+ buf += ret;
+ total += ret;
+ }
+
+ return total;
+}
+
+/*
* Opens a socket with FD_CLOEXEC set
*/
int qemu_socket(int domain, int type, int protocol)
diff --git a/qemu-common.h b/qemu-common.h
index b3957f1..0f60e08 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -190,6 +190,8 @@ void qemu_mutex_unlock_iothread(void);
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_read_full(int fd, void *buf, size_t count)
+ QEMU_WARN_UNUSED_RESULT;
void qemu_set_cloexec(int fd);
#ifndef _WIN32
--
1.7.0.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [PATCH] Add qemu_read_full
2010-11-24 17:18 [Qemu-devel] [PATCH] Add qemu_read_full M. Mohan Kumar
@ 2010-11-24 19:21 ` Stefan Hajnoczi
0 siblings, 0 replies; 2+ messages in thread
From: Stefan Hajnoczi @ 2010-11-24 19:21 UTC (permalink / raw)
To: M. Mohan Kumar; +Cc: qemu-devel@nongnu.org
On Wed, Nov 24, 2010 at 5:18 PM, M. Mohan Kumar <mohan@in.ibm.com> wrote:
> Add qemu_read_full function
>
> Signed-off-by: M. Mohan Kumar <mohan@in.ibm.com>
> ---
> osdep.c | 29 +++++++++++++++++++++++++++++
> qemu-common.h | 2 ++
> 2 files changed, 31 insertions(+), 0 deletions(-)
>
> diff --git a/osdep.c b/osdep.c
> index 327583b..7046b32 100644
> --- a/osdep.c
> +++ b/osdep.c
> @@ -127,6 +127,35 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)
> }
>
> /*
> + * A variant of read(2) which handles interrupted read.
> + * Simlar to qemu_write_full function
> + *
> + * Return the number of bytes read.
> + *
> + */
Two points worth noting in the comment (stolen from qemu_write_full):
1. "This function don't work with non-blocking fd's."
2. "Set errno if fewer than `count' bytes are read." Normal QEMU
style would be to return -errno, so it's important to point out this
exception to the rule.
> +ssize_t qemu_read_full(int fd, void *buf, size_t count)
> +{
> + ssize_t ret = 0;
> + ssize_t total = 0;
> +
> + while (count) {
> + ret = read(fd, buf, count);
> + if (ret < 0) {
> + if (errno == EINTR) {
> + continue;
> + }
> + break;
> + }
> +
> + count -= ret;
> + buf += ret;
> + total += ret;
> + }
> +
> + return total;
> +}
On EOF read(2) will return 0. Right now we don't handle this case and
go into an infinite loop.
Stefan
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2010-11-24 19:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-24 17:18 [Qemu-devel] [PATCH] Add qemu_read_full M. Mohan Kumar
2010-11-24 19:21 ` Stefan Hajnoczi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).