From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43576) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzAf3-00010r-Bv for qemu-devel@nongnu.org; Tue, 16 Jul 2013 15:11:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UzAf1-0006oA-08 for qemu-devel@nongnu.org; Tue, 16 Jul 2013 15:11:45 -0400 Received: from e35.co.us.ibm.com ([32.97.110.153]:60764) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzAf0-0006mo-PV for qemu-devel@nongnu.org; Tue, 16 Jul 2013 15:11:42 -0400 Received: from /spool/local by e35.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 16 Jul 2013 13:11:40 -0600 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by d03dlp02.boulder.ibm.com (Postfix) with ESMTP id 07C923E40030 for ; Tue, 16 Jul 2013 12:57:50 -0600 (MDT) Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r6GIvsQT043440 for ; Tue, 16 Jul 2013 12:57:57 -0600 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r6GIvqro004099 for ; Tue, 16 Jul 2013 12:57:52 -0600 From: Anthony Liguori In-Reply-To: <1373998781-29561-2-git-send-email-lersek@redhat.com> References: <1373998781-29561-1-git-send-email-lersek@redhat.com> <1373998781-29561-2-git-send-email-lersek@redhat.com> Date: Tue, 16 Jul 2013 13:57:46 -0500 Message-ID: <87ehaymixh.fsf@codemonkey.ws> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: [Qemu-devel] [PATCH 1/2] char: io_channel_send: don't lose written bytes List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Laszlo Ersek , Luiz Capitulino , qemu-devel@nongnu.org Laszlo Ersek writes: > The g_io_channel_write_chars() documentation states, > > bytes_written: The number of bytes written. This can be nonzero even if > the return value is not G_IO_STATUS_NORMAL. [...] > > io_channel_send() could lose such bytes before. > > Furthermore, the (status == G_IO_STATUS_EOF) condition used to evaluate to > constant false whenever it was reached. When that condition actually held, > it always led to -1 / EINVAL. This patch (almost) distinguishes > G_IO_STATUS_EOF only when no bytes have been written, and then treats it > as an error. Just for my own benefit, I always assume G_IO_STATUS_EOF cannot happen if bytes_written > 0. I see what you mean by the comment but do you have any reason to believe this happens in practice? Reviewed-by: Anthony Liguori Regards, Anthony Liguori > > Signed-off-by: Laszlo Ersek > --- > qemu-char.c | 41 +++++++++++++++++++---------------------- > 1 files changed, 19 insertions(+), 22 deletions(-) > > diff --git a/qemu-char.c b/qemu-char.c > index 800d6a6..c86ce4b 100644 > --- a/qemu-char.c > +++ b/qemu-char.c > @@ -720,35 +720,32 @@ static GIOChannel *io_channel_from_socket(int fd) > > static int io_channel_send(GIOChannel *fd, const void *buf, size_t len) > { > - GIOStatus status; > - size_t offset; > + size_t offset = 0; > + GIOStatus status = G_IO_STATUS_NORMAL; > > - offset = 0; > - while (offset < len) { > - gsize bytes_written; > + while (offset < len && status == G_IO_STATUS_NORMAL) { > + gsize bytes_written = 0; > > 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; > - } else { > - errno = EINVAL; > - } > - > - return -1; > - } else if (status == G_IO_STATUS_EOF) { > - break; > - } > - > offset += bytes_written; > } > > - return offset; > + if (offset > 0) { > + return offset; > + } > + switch (status) { > + case G_IO_STATUS_NORMAL: > + g_assert(len == 0); > + return 0; > + case G_IO_STATUS_AGAIN: > + errno = EAGAIN; > + return -1; > + default: > + break; > + } > + errno = EINVAL; > + return -1; > } > > #ifndef _WIN32 > -- > 1.7.1