From patchwork Wed Mar 27 16:36:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 231752 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0955A2C008C for ; Thu, 28 Mar 2013 03:45:07 +1100 (EST) Received: from localhost ([::1]:34326 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtTC-0005gx-UC for incoming@patchwork.ozlabs.org; Wed, 27 Mar 2013 12:45:02 -0400 Received: from eggs.gnu.org ([208.118.235.92]:48382) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtSl-0005a7-4M for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:44:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UKtLE-0002vF-NT for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:36:56 -0400 Received: from mail-ea0-x22f.google.com ([2a00:1450:4013:c01::22f]:64443) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtLE-0002uT-Co for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:36:48 -0400 Received: by mail-ea0-f175.google.com with SMTP id r16so821409ead.34 for ; Wed, 27 Mar 2013 09:36:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=Dh5bQM1ysV6foAJXKLxLNg3EjrdQXHZY3NiCGeiqaTU=; b=qZQ17kBiMmkjmWF3KrbA4iuxCq89NF8FzO/efwarkwsvHusScgPPCwM5nrx3ETHywP qXS1MsEvGPdfeeHSQQaYWaIXc7X+HVOWxO78ccc3xDYZxxYPg0kibaVxIfqIPO2PIWIc R5hONTIPJiLO2Hh1wFYxetoLH9WlUFKtAwtYFNQHHGVVMn19f2KO/KW1KgnoGG8cro+5 T8Vd9YeocbVC01cEUcWHoL96zC9ZltDCNKI9f2CGiPjndpmDtegOWTAsmjHOyRykZsks W8ywAQ3M0+G2BX7dfb/o61nQIm/a8Dw9vF59pE5X/dg5XMZbjuMuF3K49LWIUAXPEHyF KWRQ== X-Received: by 10.14.183.198 with SMTP id q46mr49102480eem.1.1364402207431; Wed, 27 Mar 2013 09:36:47 -0700 (PDT) Received: from playground.lan (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPS id d47sm32366397eem.9.2013.03.27.09.36.45 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 27 Mar 2013 09:36:46 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 27 Mar 2013 17:36:31 +0100 Message-Id: <1364402192-18169-6-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1364402192-18169-1-git-send-email-pbonzini@redhat.com> References: <1364402192-18169-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::22f Cc: owasserm@redhat.com, quintela@redhat.com Subject: [Qemu-devel] [PATCH 5/6] iov: handle partial writes from sendmsg and recvmsg X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Reviewed-by: Juan Quintela --- 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; }