ia64/xen-unstable

changeset 16665:003542d9ab77

tapaio check return value from read()

In tools/blktap/drivers/tapaio.c there is a call to read(2) whose
return value is not checked. The attached patch attempts to do
something vaguely sensible in cases of error.

Fully comprehensive error handling in this area would be quite tough
to introduce now but at least with this change when things go wrong
you stand a chance of getting some information about what happened.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Dec 27 12:28:58 2007 +0000 (2007-12-27)
parents 9fe92a88912b
children d24f457fa1af
files tools/blktap/drivers/tapaio.c
line diff
     1.1 --- a/tools/blktap/drivers/tapaio.c	Thu Dec 27 12:27:34 2007 +0000
     1.2 +++ b/tools/blktap/drivers/tapaio.c	Thu Dec 27 12:28:58 2007 +0000
     1.3 @@ -30,6 +30,8 @@
     1.4  #include "tapaio.h"
     1.5  #include "tapdisk.h"
     1.6  #include <unistd.h>
     1.7 +#include <errno.h>
     1.8 +#include <string.h>
     1.9  
    1.10  /**
    1.11   * We used a kernel patch to return an fd associated with the AIO context
    1.12 @@ -149,8 +151,22 @@ tap_aio_get_events(tap_aio_context_t *ct
    1.13          if (!ctx->poll_in_thread)
    1.14                  nr_events = io_getevents(ctx->aio_ctx, 1,
    1.15                                           ctx->max_aio_events, ctx->aio_events, NULL);
    1.16 -        else
    1.17 -                read(ctx->completion_fd[0], &nr_events, sizeof(nr_events));
    1.18 +        else {
    1.19 +		int r;
    1.20 +		r = read(ctx->completion_fd[0], &nr_events, sizeof(nr_events));
    1.21 +		if (r < 0) {
    1.22 +			if (errno == EAGAIN || errno == EINTR)
    1.23 +				return 0;
    1.24 +			/* This is pretty bad, we'll probably spin */
    1.25 +			DPRINTF("Aargh, read completion_fd failed: %s",
    1.26 +				strerror(errno));
    1.27 +		} else if (r != sizeof(nr_events)) {
    1.28 +			/* Should never happen because sizeof(nr_events)
    1.29 +			 * fits in the guaranteed atomic pipe write size.
    1.30 +			 * Blundering on is slightly nicer than asserting */
    1.31 +			DPRINTF("Aargh, read completion_fd short read %d", r);
    1.32 +		}
    1.33 +	}
    1.34  
    1.35          return nr_events;
    1.36  }