[Qemu-devel] [PATCH] Add qemu_read_full

2010-11-24 Thread M. Mohan Kumar
Add qemu_read_full function

Signed-off-by: M. Mohan Kumar mo...@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




Re: [Qemu-devel] [PATCH] Add qemu_read_full

2010-11-24 Thread Stefan Hajnoczi
On Wed, Nov 24, 2010 at 5:18 PM, M. Mohan Kumar mo...@in.ibm.com wrote:
 Add qemu_read_full function

 Signed-off-by: M. Mohan Kumar mo...@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