[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH] tools/libxc: Implement writev_exact() in the same style as write_exact()
This implementation of writev_exact() will cope with an iovcnt greater than IOV_MAX because glibc will actually let this work anyway, and it is very useful not to have to work about this in the caller of writev_exact(). The caller is still required to ensure that the sum of iov_len's doesn't overflow a ssize_t. Promote the MAX() and MIN() macro definitions from xg_save_restore.h to xc_private.h Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx> CC: Ian Campbell <Ian.Campbell@xxxxxxxxxx> CC: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx> --- From strace()ing while writing into a pipe, I have observed quite a few cases where writev() will perform a partial overall write, but return with len exactly on an iov[] element boundary. However, I can't find any guarantee that a partial write won't end midway through an iov[] element, which is why the adjustment is present. --- tools/libxc/xc_private.c | 34 ++++++++++++++++++++++++++++++++++ tools/libxc/xc_private.h | 12 ++++++++++++ tools/libxc/xg_save_restore.h | 6 ------ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c index a3da614..b11d5e5 100644 --- a/tools/libxc/xc_private.c +++ b/tools/libxc/xc_private.c @@ -854,6 +854,40 @@ int write_exact(int fd, const void *data, size_t size) return 0; } +int writev_exact(int fd, struct iovec *iov, int iovcnt) +{ + int iov_idx = 0; + ssize_t len; + + while ( iov_idx < iovcnt ) + { + /* Skip over iov[] enties with 0 length. */ + while ( iov[iov_idx].iov_len == 0 ) + if ( ++iov_idx == iovcnt ) + return 0; + + len = writev(fd, &iov[iov_idx], MIN(iovcnt - iov_idx, IOV_MAX)); + + if ( (len == -1) && (errno == EINTR) ) + continue; + if ( len <= 0 ) + return -1; + + /* Check iov[] to see whether we had a partial or complete write. */ + while ( len > 0 && (iov_idx < iovcnt) ) + len -= iov[iov_idx++].iov_len; + + if ( len < 0 ) /* Partial write of iov[iov_idx - 1]. */ + { + iov_idx--; + iov[iov_idx].iov_base += iov[iov_idx].iov_len + len; + iov[iov_idx].iov_len = -len; + } + } + + return 0; +} + int xc_ffs8(uint8_t x) { int i; diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h index c7730f2..431ab5e 100644 --- a/tools/libxc/xc_private.h +++ b/tools/libxc/xc_private.h @@ -28,6 +28,7 @@ #include <sys/stat.h> #include <stdlib.h> #include <sys/ioctl.h> +#include <sys/uio.h> #include "xenctrl.h" #include "xenctrlosdep.h" @@ -343,6 +344,10 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu); /* Return 0 on success; -1 on error setting errno. */ 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); +int writev_exact(int fd, struct iovec *iov, int iovcnt); +/* Note - writev_exact() might modify iov. Whether it does so in practice + * depends on whether your system implementation of writev() returns from a + * partial write in the middle of an iov element. */ int xc_ffs8(uint8_t x); int xc_ffs16(uint16_t x); @@ -352,4 +357,11 @@ int xc_ffs64(uint64_t x); #define DOMPRINTF(fmt, args...) xc_dom_printf(dom->xch, fmt, ## args) #define DOMPRINTF_CALLED(xch) xc_dom_printf((xch), "%s: called", __FUNCTION__) +#ifndef MAX +#define MAX(_a, _b) ((_a) >= (_b) ? (_a) : (_b)) +#endif +#ifndef MIN +#define MIN(_a, _b) ((_a) <= (_b) ? (_a) : (_b)) +#endif + #endif /* __XC_PRIVATE_H__ */ diff --git a/tools/libxc/xg_save_restore.h b/tools/libxc/xg_save_restore.h index aa93c13..34019fa 100644 --- a/tools/libxc/xg_save_restore.h +++ b/tools/libxc/xg_save_restore.h @@ -393,9 +393,3 @@ static inline int get_platform_info(xc_interface *xch, uint32_t dom, memset(&(_p)->x32._f[0], (_v), sizeof((_p)->x32._f)); \ } while (0) -#ifndef MAX -#define MAX(_a, _b) ((_a) >= (_b) ? (_a) : (_b)) -#endif -#ifndef MIN -#define MIN(_a, _b) ((_a) <= (_b) ? (_a) : (_b)) -#endif -- 1.7.10.4 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |