Skip to contentSkip to conversation

Add qemu_read_full

PATCH In review M. Mohan Kumar 2 participants 2 messages Nov 24, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add qemu_read_full

Add qemu_read_full function

---
 osdep.c       |   29 +++++++++++++++++++++++++++++
 qemu-common.h |    2 ++
 2 files changed, 31 insertions(+), 0 deletions(-)
osdep.c +29 -0
index 327583b..7046b32 100644
@@ -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)
qemu-common.h +2 -0
index b3957f1..0f60e08 100644
@@ -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
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 13 lines · M. Mohan Kumar

Add qemu_read_full function

---
 osdep.c       |   29 +++++++++++++++++++++++++++++ qemu-common.h |    2 ++

 2 files changed, 31 insertions(+), 0 deletions(-)
osdep.c +0 -0
index 327583b..7046b32 100644

osdep.c ssize_t qemu_write_full(int fd, const void *buf, size_t count) · line 135

Quoted 9 lines
osdep.c +6 -0
@@ -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.

Quoted 11 lines
osdep.c +11 -0
+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;
+            }

osdep.c ssize_t qemu_write_full(int fd, const void *buf, size_t count) · line 156

Quoted 10 lines
osdep.c +10 -0
+            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