[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-changelog] [xen-unstable] blktap2: Fix naked unchecked uses of read/write/chdir.
# HG changeset patch # User Keir Fraser <keir@xxxxxxx> # Date 1336757429 -3600 # Node ID 01d64a3dea717134d9387a1b39745bdc2eb8cf59 # Parent 84ae90427c54f275944683848344d1b17f5584f5 blktap2: Fix naked unchecked uses of read/write/chdir. These cause warnings under warn_unused_result, and for read/write we ought to deal with partial io results. Signed-off-by: Keir Fraser <keir@xxxxxxx> --- diff -r 84ae90427c54 -r 01d64a3dea71 tools/blktap2/drivers/tapdisk-diff.c --- a/tools/blktap2/drivers/tapdisk-diff.c Fri May 11 17:46:16 2012 +0100 +++ b/tools/blktap2/drivers/tapdisk-diff.c Fri May 11 18:30:29 2012 +0100 @@ -39,6 +39,7 @@ #include "tapdisk-vbd.h" #include "tapdisk-server.h" #include "tapdisk-disktype.h" +#include "tapdisk-utils.h" #include "libvhd.h" #define POLL_READ 0 @@ -170,7 +171,7 @@ tapdisk_stream_poll_clear(struct tapdisk { int dummy; - read(p->pipe[POLL_READ], &dummy, sizeof(dummy)); + read_exact(p->pipe[POLL_READ], &dummy, sizeof(dummy)); p->set = 0; } @@ -180,7 +181,7 @@ tapdisk_stream_poll_set(struct tapdisk_s int dummy = 0; if (!p->set) { - write(p->pipe[POLL_WRITE], &dummy, sizeof(dummy)); + write_exact(p->pipe[POLL_WRITE], &dummy, sizeof(dummy)); p->set = 1; } } diff -r 84ae90427c54 -r 01d64a3dea71 tools/blktap2/drivers/tapdisk-log.c --- a/tools/blktap2/drivers/tapdisk-log.c Fri May 11 17:46:16 2012 +0100 +++ b/tools/blktap2/drivers/tapdisk-log.c Fri May 11 18:30:29 2012 +0100 @@ -37,6 +37,7 @@ #include <sys/time.h> #include "tapdisk-log.h" +#include "tapdisk-utils.h" #define MAX_ENTRY_LEN 512 #define MAX_ERROR_MESSAGES 16 @@ -247,7 +248,7 @@ tlog_flush(void) wsize = ((size + 511) & (~511)); memset(tapdisk_log.buf + size, '\n', wsize - size); - write(fd, tapdisk_log.buf, wsize); + write_exact(fd, tapdisk_log.buf, wsize); tapdisk_log.p = tapdisk_log.buf; diff -r 84ae90427c54 -r 01d64a3dea71 tools/blktap2/drivers/tapdisk-queue.c --- a/tools/blktap2/drivers/tapdisk-queue.c Fri May 11 17:46:16 2012 +0100 +++ b/tools/blktap2/drivers/tapdisk-queue.c Fri May 11 18:30:29 2012 +0100 @@ -435,7 +435,7 @@ tapdisk_lio_ack_event(struct tqueue *que uint64_t val; if (lio->flags & LIO_FLAG_EVENTFD) - read(lio->event_fd, &val, sizeof(val)); + read_exact(lio->event_fd, &val, sizeof(val)); } static void diff -r 84ae90427c54 -r 01d64a3dea71 tools/blktap2/drivers/tapdisk-stream.c --- a/tools/blktap2/drivers/tapdisk-stream.c Fri May 11 17:46:16 2012 +0100 +++ b/tools/blktap2/drivers/tapdisk-stream.c Fri May 11 18:30:29 2012 +0100 @@ -38,6 +38,7 @@ #include "tapdisk-vbd.h" #include "tapdisk-server.h" #include "tapdisk-disktype.h" +#include "tapdisk-utils.h" #define POLL_READ 0 #define POLL_WRITE 1 @@ -145,7 +146,7 @@ tapdisk_stream_poll_clear(struct tapdisk { int dummy; - read(p->pipe[POLL_READ], &dummy, sizeof(dummy)); + read_exact(p->pipe[POLL_READ], &dummy, sizeof(dummy)); p->set = 0; } @@ -155,7 +156,7 @@ tapdisk_stream_poll_set(struct tapdisk_s int dummy = 0; if (!p->set) { - write(p->pipe[POLL_WRITE], &dummy, sizeof(dummy)); + write_exact(p->pipe[POLL_WRITE], &dummy, sizeof(dummy)); p->set = 1; } } @@ -203,7 +204,7 @@ tapdisk_stream_print_request(struct tapd { unsigned long idx = (unsigned long)tapdisk_stream_request_idx(s, sreq); char *buf = (char *)MMAP_VADDR(s->vbd->ring.vstart, idx, 0); - write(s->out_fd, buf, sreq->secs << SECTOR_SHIFT); + write_exact(s->out_fd, buf, sreq->secs << SECTOR_SHIFT); } static void diff -r 84ae90427c54 -r 01d64a3dea71 tools/blktap2/drivers/tapdisk-utils.c --- a/tools/blktap2/drivers/tapdisk-utils.c Fri May 11 17:46:16 2012 +0100 +++ b/tools/blktap2/drivers/tapdisk-utils.c Fri May 11 18:30:29 2012 +0100 @@ -175,3 +175,40 @@ int tapdisk_linux_version(void) } #endif +int read_exact(int fd, void *data, size_t size) +{ + size_t offset = 0; + ssize_t len; + + while ( offset < size ) + { + len = read(fd, (char *)data + offset, size - offset); + if ( (len == -1) && (errno == EINTR) ) + continue; + if ( len == 0 ) + errno = 0; + if ( len <= 0 ) + return -1; + offset += len; + } + + return 0; +} + +int write_exact(int fd, const void *data, size_t size) +{ + size_t offset = 0; + ssize_t len; + + while ( offset < size ) + { + len = write(fd, (const char *)data + offset, size - offset); + if ( (len == -1) && (errno == EINTR) ) + continue; + if ( len <= 0 ) + return -1; + offset += len; + } + + return 0; +} diff -r 84ae90427c54 -r 01d64a3dea71 tools/blktap2/drivers/tapdisk-utils.h --- a/tools/blktap2/drivers/tapdisk-utils.h Fri May 11 17:46:16 2012 +0100 +++ b/tools/blktap2/drivers/tapdisk-utils.h Fri May 11 18:30:29 2012 +0100 @@ -39,4 +39,7 @@ int tapdisk_namedup(char **, const char int tapdisk_get_image_size(int, uint64_t *, uint32_t *); int tapdisk_linux_version(void); +int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */ +int write_exact(int fd, const void *data, size_t size); + #endif diff -r 84ae90427c54 -r 01d64a3dea71 tools/blktap2/drivers/tapdisk2.c --- a/tools/blktap2/drivers/tapdisk2.c Fri May 11 17:46:16 2012 +0100 +++ b/tools/blktap2/drivers/tapdisk2.c Fri May 11 18:30:29 2012 +0100 @@ -79,7 +79,12 @@ main(int argc, char *argv[]) if (optind != argc) usage(argv[0], EINVAL); - chdir("/"); + if (chdir("/")) { + DPRINTF("failed to chdir(/): %d\n", errno); + err = 1; + goto out; + } + tapdisk_start_logging("tapdisk2"); err = tapdisk_server_init(); _______________________________________________ Xen-changelog mailing list Xen-changelog@xxxxxxxxxxxxx http://lists.xensource.com/xen-changelog
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |