diff mbox

[v3,06/35] osdep: add qemu_read_full() to read interrupt-safely

Message ID 57bbf61835e29be17e90937bf87c44140b64f958.1351582535.git.yamahata@valinux.co.jp
State New
Headers show

Commit Message

Isaku Yamahata Oct. 30, 2012, 8:32 a.m. UTC
This is read counter part of qemu_write_full().

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
---
 osdep.c       |   24 ++++++++++++++++++++++++
 qemu-common.h |    2 ++
 2 files changed, 26 insertions(+)
diff mbox

Patch

diff --git a/osdep.c b/osdep.c
index 3b25297..416ffe1 100644
--- a/osdep.c
+++ b/osdep.c
@@ -261,6 +261,30 @@  ssize_t qemu_write_full(int fd, const void *buf, size_t count)
     return total;
 }
 
+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;
+        }
+        if (ret == 0) {
+            break;
+        }
+
+        count -= ret;
+        buf += ret;
+        total += ret;
+    }
+
+    return total;
+}
+
 /*
  * Opens a socket with FD_CLOEXEC set
  */
diff --git a/qemu-common.h b/qemu-common.h
index b54612b..16128c5 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -214,6 +214,8 @@  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_read_full(int fd, void *buf, size_t count)
+    QEMU_WARN_UNUSED_RESULT;
 ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
     QEMU_WARN_UNUSED_RESULT;