All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] check return value from read() and write() properly
Date: Wed, 06 Feb 2008 13:18:31 -0600	[thread overview]
Message-ID: <47AA0807.40707@codemonkey.ws> (raw)
In-Reply-To: <18329.60886.909252.289862@mariner.uk.xensource.com>

Ian Jackson wrote:
> The system calls read and write may return less than the whole amount
> requested for a number of reasons.  So the idioms
>    if (read(fd, &object, sizeof(object)) != sizeof(object)) goto fail;
> and even worse
>    if (read(fd, &object, sizeof(object)) < 0) goto fail;
> are wrong.  Additionally, read and write may sometimes return EINTR on
> some systems so interruption is not desired or expected a loop is
> needed.
>
> In the attached patch I introduce two new pairs of functions:
>  qemu_{read,write}      which are like read and write but never
>                           return partial answers unnecessarily
>                           and which never fail with EINTR
>  qemu_{read,write}_ok   which returns -1 for any failure or
>                           incomplete read, or +1 for success,
>                           reducing repetition at calling points
>   

There is already a unix_write function that serves this purpose.  I 
think a better approach would be to define unix_read/unix_write and 
remove the EAGAIN handling and instead only spin on EINTR.

I don't really like the _ok thing as it's not a very common idiom.

Regards,

Anthony Liguori

> I added these to osdep.c, and there are many calls in the block
> drivers, so osdep.o needs to be in BLOCK_OBJS as I do in my previous
> patch (getting rid of the duplicate definitions of qemu_malloc &c).
>
> The patch then uses these new functions whereever appropriate.  I
> think I have got each calling point correct but for obvious reasons I
> haven't done a thorough test.
>
> The resulting code is I think both smaller and more correct.  In most
> cases the correct behaviour was obvious.
>
> There was one nonobvious case: I removed unix_write from vl.c and
> replaced calls to it with calls to qemu_write.  unix_write looped on
> EAGAIN (though qemu_write doesn't) but I think this is wrong since
> that simply results in spinning if the fd is nonblocking and the write
> cannot complete immediately.  Callers with nonblocking fds have to
> cope with partial results and retry later.  Since unix_write doesn't
> do that I assume that its callers don't really have nonblocking fds;
> if they do then the old code is buggy and my new code is buggy too but
> in a different way.
>
> Also, the Makefile rule for dyngen$(EXESUF) referred to dyngen.c
> rather than dyngen.o, which appears to have been a mistake which I
> have fixed since I had to add osdep.o anyway.
>
> Regards,
> Ian.
>
>
>   

  parent reply	other threads:[~2008-02-06 19:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-25 14:10 [Qemu-devel] [PATCH] check return value from read() and write() properly Ian Jackson
2008-02-06 17:14 ` [Qemu-devel] " Ian Jackson
2008-02-06 19:18 ` Anthony Liguori [this message]
2008-02-07 10:32   ` [Qemu-devel] " Ian Jackson

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=47AA0807.40707@codemonkey.ws \
    --to=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.