qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: owasserm@redhat.com, quintela@redhat.com
Subject: [Qemu-devel] [PATCH 5/6] iov: handle partial writes from sendmsg and recvmsg
Date: Wed, 27 Mar 2013 17:36:31 +0100	[thread overview]
Message-ID: <1364402192-18169-6-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1364402192-18169-1-git-send-email-pbonzini@redhat.com>

Partial writes can still happen in sendmsg and recvmsg, if a
signal is received in the middle of a write.  To handle this,
retry the operation with a new offset/bytes pair.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
        I suggest using "git show -b" to review this one...

 util/iov.c | 102 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 55 insertions(+), 47 deletions(-)

diff --git a/util/iov.c b/util/iov.c
index f14ff0b..0ecfc4c 100644
--- a/util/iov.c
+++ b/util/iov.c
@@ -144,63 +144,71 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
                       size_t offset, size_t bytes,
                       bool do_send)
 {
+    ssize_t total = 0;
     ssize_t ret;
     size_t orig_len, tail;
     unsigned niov;
 
-    if (bytes == 0) {
-        /* Catch the do-nothing case early, as otherwise we will pass an
-         * empty iovec to sendmsg/recvmsg(), and not all implementations
-         * accept this.
-         */
-        return 0;
-    }
-
-    /* Find the start position, skipping `offset' bytes:
-     * first, skip all full-sized vector elements, */
-    for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
-        offset -= iov[niov].iov_len;
-    }
+    while (bytes > 0) {
+        /* Find the start position, skipping `offset' bytes:
+         * first, skip all full-sized vector elements, */
+        for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
+            offset -= iov[niov].iov_len;
+        }
 
-    /* niov == iov_cnt would only be valid if bytes == 0, which
-     * we already ruled out above.  */
-    assert(niov < iov_cnt);
-    iov += niov;
-    iov_cnt -= niov;
-
-    if (offset) {
-        /* second, skip `offset' bytes from the (now) first element,
-         * undo it on exit */
-        iov[0].iov_base += offset;
-        iov[0].iov_len -= offset;
-    }
-    /* Find the end position skipping `bytes' bytes: */
-    /* first, skip all full-sized elements */
-    tail = bytes;
-    for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
-        tail -= iov[niov].iov_len;
-    }
-    if (tail) {
-        /* second, fixup the last element, and remember the original
-         * length */
+        /* niov == iov_cnt would only be valid if bytes == 0, which
+         * we already ruled out in the loop condition.  */
         assert(niov < iov_cnt);
-        assert(iov[niov].iov_len > tail);
-        orig_len = iov[niov].iov_len;
-        iov[niov++].iov_len = tail;
-    }
+        iov += niov;
+        iov_cnt -= niov;
+
+        if (offset) {
+            /* second, skip `offset' bytes from the (now) first element,
+             * undo it on exit */
+            iov[0].iov_base += offset;
+            iov[0].iov_len -= offset;
+        }
+        /* Find the end position skipping `bytes' bytes: */
+        /* first, skip all full-sized elements */
+        tail = bytes;
+        for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
+            tail -= iov[niov].iov_len;
+        }
+        if (tail) {
+            /* second, fixup the last element, and remember the original
+             * length */
+            assert(niov < iov_cnt);
+            assert(iov[niov].iov_len > tail);
+            orig_len = iov[niov].iov_len;
+            iov[niov++].iov_len = tail;
+        }
 
-    ret = do_send_recv(sockfd, iov, niov, do_send);
+        ret = do_send_recv(sockfd, iov, niov, do_send);
 
-    /* Undo the changes above */
-    if (tail) {
-        iov[niov-1].iov_len = orig_len;
-    }
-    if (offset) {
-        iov[0].iov_base -= offset;
-        iov[0].iov_len += offset;
+        /* Undo the changes above before checking for errors */
+        if (tail) {
+            iov[niov-1].iov_len = orig_len;
+        }
+        if (offset) {
+            iov[0].iov_base -= offset;
+            iov[0].iov_len += offset;
+        }
+
+        if (ret < 0) {
+            assert(errno != EINTR);
+            if (errno == EAGAIN && total > 0) {
+                return total;
+            }
+            return -1;
+        }
+
+        /* Prepare for the next iteration */
+        offset += ret;
+        total += ret;
+        bytes -= ret;
     }
 
-    return ret;
+    return total;
 }
 
 
-- 
1.8.1.4

  parent reply	other threads:[~2013-03-27 16:44 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-27 16:36 [Qemu-devel] [PATCH 0/6] migration: followups for writev patches Paolo Bonzini
2013-03-27 16:36 ` [Qemu-devel] [PATCH 1/6] qemu-file: drop socket_put_buffer Paolo Bonzini
2013-04-09 13:20   ` Juan Quintela
2013-03-27 16:36 ` [Qemu-devel] [PATCH 2/6] iov: reorganize iov_send_recv, part 1 Paolo Bonzini
2013-04-09 13:18   ` Juan Quintela
2013-03-27 16:36 ` [Qemu-devel] [PATCH 3/6] iov: reorganize iov_send_recv, part 2 Paolo Bonzini
2013-04-09 13:23   ` Juan Quintela
2013-03-27 16:36 ` [Qemu-devel] [PATCH 4/6] iov: reorganize iov_send_recv, part 3 Paolo Bonzini
2013-04-09 13:24   ` Juan Quintela
2013-03-27 16:36 ` Paolo Bonzini [this message]
2013-04-09 13:27   ` [Qemu-devel] [PATCH 5/6] iov: handle partial writes from sendmsg and recvmsg Juan Quintela
2013-03-27 16:36 ` [Qemu-devel] [PATCH 6/6] qemu-file: do not use stdio for qemu_fdopen Paolo Bonzini
2013-04-09 13:30   ` Juan Quintela
2013-04-16 21:09   ` Anthony Liguori
2013-04-03  8:08 ` [Qemu-devel] [PATCH 0/6] migration: followups for writev patches Orit Wasserman
2013-04-16 15:41   ` Anthony Liguori
  -- strict thread matches above, loose matches on Subject: below --
2013-04-17  9:46 [Qemu-devel] [PULL (rebased) " Paolo Bonzini
2013-04-17  9:46 ` [Qemu-devel] [PATCH 5/6] iov: handle partial writes from sendmsg and recvmsg Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1364402192-18169-6-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=owasserm@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).