Commit 43fa5b90 authored by Keir Fraser's avatar Keir Fraser

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: default avatarKeir Fraser <keir@xen.org>
parent e1ce2cbe
......@@ -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_stream_poll *p)
{
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_stream_poll *p)
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;
}
}
......
......@@ -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;
......
......@@ -435,7 +435,7 @@ tapdisk_lio_ack_event(struct tqueue *queue)
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
......
......@@ -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_stream_poll *p)
{
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_stream_poll *p)
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 tapdisk_stream *s,
{
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
......
......@@ -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;
}
......@@ -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
......@@ -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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment