]> git.proxmox.com Git - qemu.git/commitdiff
char: introduce a blocking version of qemu_chr_fe_write
authorAnthony Liguori <aliguori@us.ibm.com>
Tue, 26 Mar 2013 15:04:17 +0000 (10:04 -0500)
committerAnthony Liguori <aliguori@us.ibm.com>
Tue, 26 Mar 2013 15:08:07 +0000 (10:08 -0500)
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
include/char/char.h
qemu-char.c

index 0326b2a47b4782f2d6e1e52135441eb6ee32c347..5c3a7a57564d3c4aa1be0cc95de2fa28d5689c40 100644 (file)
@@ -169,6 +169,21 @@ int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
  */
 int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len);
 
+/**
+ * @qemu_chr_fe_write_all:
+ *
+ * Write data to a character backend from the front end.  This function will
+ * send data from the front end to the back end.  Unlike @qemu_chr_fe_write,
+ * this function will block if the back end cannot consume all of the data
+ * attempted to be written.
+ *
+ * @buf the data
+ * @len the number of bytes to send
+ *
+ * Returns: the number of bytes consumed
+ */
+int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len);
+
 /**
  * @qemu_chr_fe_ioctl:
  *
index 4e011df3ecca3dad4852065a94a2994eec6ac08a..936150fa73d6568b291f3a1f26cf7ed558c30280 100644 (file)
@@ -140,6 +140,33 @@ int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
     return s->chr_write(s, buf, len);
 }
 
+int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
+{
+    int offset = 0;
+    int res;
+
+    while (offset < len) {
+        do {
+            res = s->chr_write(s, buf + offset, len - offset);
+            if (res == -1 && errno == EAGAIN) {
+                g_usleep(100);
+            }
+        } while (res == -1 && errno == EAGAIN);
+
+        if (res == 0) {
+            break;
+        }
+
+        if (res < 0) {
+            return res;
+        }
+
+        offset += res;
+    }
+
+    return offset;
+}
+
 int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
 {
     if (!s->chr_ioctl)