From patchwork Fri Mar 29 16:39:50 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 232460 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 279212C00C9 for ; Sat, 30 Mar 2013 03:40:38 +1100 (EST) Received: from localhost ([::1]:43222 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ULcM0-0008Km-1n for incoming@patchwork.ozlabs.org; Fri, 29 Mar 2013 12:40:36 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54655) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ULcLd-0008FH-47 for qemu-devel@nongnu.org; Fri, 29 Mar 2013 12:40:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ULcLb-000239-3j for qemu-devel@nongnu.org; Fri, 29 Mar 2013 12:40:13 -0400 Received: from e23smtp02.au.ibm.com ([202.81.31.144]:51225) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ULcLa-00021G-JC for qemu-devel@nongnu.org; Fri, 29 Mar 2013 12:40:11 -0400 Received: from /spool/local by e23smtp02.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sat, 30 Mar 2013 02:32:49 +1000 Received: from d23dlp02.au.ibm.com (202.81.31.213) by e23smtp02.au.ibm.com (202.81.31.208) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Sat, 30 Mar 2013 02:32:47 +1000 Received: from d23relay05.au.ibm.com (d23relay05.au.ibm.com [9.190.235.152]) by d23dlp02.au.ibm.com (Postfix) with ESMTP id 68B1F2BB0023 for ; Sat, 30 Mar 2013 03:39:55 +1100 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay05.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r2TGQec33867022 for ; Sat, 30 Mar 2013 03:26:42 +1100 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r2TGdqM1029355 for ; Sat, 30 Mar 2013 03:39:53 +1100 Received: from titi.austin.rr.com (sig-9-49-145-124.mts.ibm.com [9.49.145.124]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r2TGdo4I029349; Sat, 30 Mar 2013 03:39:51 +1100 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Fri, 29 Mar 2013 11:39:50 -0500 Message-Id: <1364575190-731-1-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.8.0 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13032916-5490-0000-0000-0000033292FE X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.144 Cc: Amit Shah , Anthony Liguori Subject: [Qemu-devel] [PATCH] qemu-char: rewrite io_channel_send_all and drop the '_all' suffix 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 The current code is oddly written and have equally odd semantics. Despite the '_all' suffix, upon EAGAIN the result will be a partial write but instead of returning the partial write, we return EAGAIN. Change the behavior to write as much as we can until we get an EAGAIN returning a partial write if we do. Reported-by: Amit Shah Signed-off-by: Anthony Liguori Tested-by: Amit Shah --- qemu-char.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index dee623e..7acbf53 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -727,33 +727,37 @@ static GIOChannel *io_channel_from_socket(int fd) return chan; } -static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1) +static int io_channel_send(GIOChannel *fd, const void *buf, size_t len) { GIOStatus status; - gsize bytes_written; - int len; - const uint8_t *buf = _buf; + size_t offset; - len = len1; - while (len > 0) { - status = g_io_channel_write_chars(fd, (const gchar *)buf, len, + offset = 0; + while (offset < len) { + gsize bytes_written; + + status = g_io_channel_write_chars(fd, buf + offset, len - offset, &bytes_written, NULL); if (status != G_IO_STATUS_NORMAL) { if (status == G_IO_STATUS_AGAIN) { + /* If we've written any data, return a partial write. */ + if (offset) { + break; + } errno = EAGAIN; - return -1; } else { errno = EINVAL; - return -1; } + + return -1; } else if (status == G_IO_STATUS_EOF) { break; - } else { - buf += bytes_written; - len -= bytes_written; } + + offset += bytes_written; } - return len1 - len; + + return offset; } #ifndef _WIN32 @@ -770,7 +774,7 @@ static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) { FDCharDriver *s = chr->opaque; - return io_channel_send_all(s->fd_out, buf, len); + return io_channel_send(s->fd_out, buf, len); } static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) @@ -1088,7 +1092,7 @@ static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) pty_chr_update_read_handler(chr); return 0; } - return io_channel_send_all(s->fd, buf, len); + return io_channel_send(s->fd, buf, len); } static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond) @@ -2347,7 +2351,7 @@ static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) { TCPCharDriver *s = chr->opaque; if (s->connected) { - return io_channel_send_all(s->chan, buf, len); + return io_channel_send(s->chan, buf, len); } else { /* XXX: indicate an error ? */ return len;