Skip to contentSkip to conversation

tools/libxc: Implement writev_exact() in the same style as write_exact()

PATCH Superseded Andrew Cooper 2 participants 5 messages Feb 19, 2015

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 tools/libxc: Implement writev_exact() in the same style as write_exact()

Implement a writev() wrapper which takes care of ensuring that partial writes are completed.

writev_exact() is slightly more flexible than writev() in that it will cope with an iovcnt greater than IOV_MAX, and will take care of correctly submitting POSIX-compliant writev() calls. The caller of writev_exact() is however still required to ensure that the sum of iov_len's does not overflow a ssize_t.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
v5:
* Rewrite commit message.
v4:
* Allow this to compile in a stubdom environment.
v3:
* Re-add adjustment for partial writes.
* Split min/max adjustment into separate patch.
v2:
* Remove adjustment for partial writes of a specific iov[] entry.

---
 tools/libxc/xc_private.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxc/xc_private.h |   14 ++++++++
 2 files changed, 93 insertions(+)
tools/libxc/xc_private.c +79 -0
index df6cd9b..35c514a 100644
@@ -860,6 +860,85 @@ int write_exact(int fd, const void *data, size_t size)
     return 0;
 }
 
+#if defined(__MINIOS__)
+/*
+ * MiniOS's libc doesn't know about writev(). Implement it as multiple write()s.
+ */
+int writev_exact(int fd, const struct iovec *iov, int iovcnt)
+{
+    int rc, i;
+
+    for ( i = 0; i < iovcnt; ++i )
+    {
+        rc = write_exact(fd, iov[i].iov_base, iov[i].iov_len);
+        if ( rc )
+            return rc;
+    }
+
+    return 0;
+}
+#else
+int writev_exact(int fd, const struct iovec *iov, int iovcnt)
+{
+    struct iovec *local_iov = NULL;
+    int rc = 0, iov_idx = 0, saved_errno = 0;
+    ssize_t len;
+
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+                goto out;
+
+        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
+        saved_errno = errno;
+
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }
+
+        /* Check iov[] to see whether we had a partial or complete write. */
+        while ( len > 0 && (iov_idx < iovcnt) )
+        {
+            if ( len >= iov[iov_idx].iov_len )
+                len -= iov[iov_idx++].iov_len;
+            else
+            {
+                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
+                 * element iov_idx and resubmit the rest. */
+                if ( !local_iov )
+                {
+                    local_iov = malloc(iovcnt * sizeof(*iov));
+                    if ( !local_iov )
+                    {
+                        saved_errno = ENOMEM;
+                        goto out;
+                    }
+
+                    iov = memcpy(local_iov, iov, iovcnt * sizeof(*iov));
+                }
+
+                local_iov[iov_idx].iov_base += len;
+                local_iov[iov_idx].iov_len  -= len;
+                break;
+            }
+        }
+    }
+
+    saved_errno = 0;
+
+ out:
+    free(local_iov);
+    errno = saved_errno;
+    return rc;
+}
+#endif
+
 int xc_ffs8(uint8_t x)
 {
     int i;
tools/libxc/xc_private.h +14 -0
index 45b8644..f74f7d7 100644
@@ -42,6 +42,19 @@
 #define VALGRIND_MAKE_MEM_UNDEFINED(addr, len) /* addr, len */
 #endif
 
+#if defined(__MINIOS__)
+/*
+ * MiniOS's libc doesn't know about sys/uio.h or writev().
+ * Declare enough of sys/uio.h to compile.
+ */
+struct iovec {
+    void *iov_base;
+    size_t iov_len;
+};
+#else
+#include <sys/uio.h>
+#endif
+
 #define DECLARE_HYPERCALL privcmd_hypercall_t hypercall
 #define DECLARE_DOMCTL struct xen_domctl domctl
 #define DECLARE_SYSCTL struct xen_sysctl sysctl
@@ -395,6 +408,7 @@ int xc_add_mmu_update(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, const struct iovec *iov, int iovcnt);
 
 int xc_ffs8(uint8_t x);
 int xc_ffs16(uint16_t x);
Signature · 1 line
1.7.10.4
#1 lore raw

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 892

Quoted 6 lines · Andrew Cooper
tools/libxc/xc_private.c +6 -0
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+                goto out;

Is this required for some reason or just an optimisation?

Quoted 11 lines
tools/libxc/xc_private.c +11 -0
+
+        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
+        saved_errno = errno;
+
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 913

Quoted 10 lines
tools/libxc/xc_private.c +10 -0
+
+        /* Check iov[] to see whether we had a partial or complete write. */
+        while ( len > 0 && (iov_idx < iovcnt) )
+        {
+            if ( len >= iov[iov_idx].iov_len )
+                len -= iov[iov_idx++].iov_len;
+            else
+            {
+                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
+                 * element iov_idx and resubmit the rest. */

I suppose we can't / don't want to just declare that the input is non-const and potentially corrupted?

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 920

Quoted 7 lines
tools/libxc/xc_private.c +7 -0
+                if ( !local_iov )
+                {
+                    local_iov = malloc(iovcnt * sizeof(*iov));
+                    if ( !local_iov )
+                    {
+                        saved_errno = ENOMEM;
+                        goto out;

What is rc at this point? I think it is 0, but I think you want it to be -1?

It might be better to drop the inialiser of rc and set it in the one or two places which would then need it (the goto out in the skip-0-length loop and just before the out label AFAICT).

#2 lore raw
Quoted 9 lines · Ian Campbell

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 892

Quoted 6 lines · Andrew Cooper
tools/libxc/xc_private.c +6 -0
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+                goto out;

Is this required for some reason or just an optimisation?

Experimentally, submitting a writev() with every iov of length 0 results in an EINVAL on CentOS 5.x

This causes a failure if a partial write adjustment happens and only iov entries of length 0 remain in the set.

Quoted 16 lines
Quoted 16 lines
tools/libxc/xc_private.c +15 -0
+
+        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
+        saved_errno = errno;
+
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }
+
+        /* Check iov[] to see whether we had a partial or complete write. */
+        while ( len > 0 && (iov_idx < iovcnt) )
+        {
Quoted 8 lines

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 913

Quoted 6 lines
tools/libxc/xc_private.c +6 -0
+            if ( len >= iov[iov_idx].iov_len )
+                len -= iov[iov_idx++].iov_len;
+            else
+            {
+                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
+                 * element iov_idx and resubmit the rest. */

I suppose we can't / don't want to just declare that the input is non-const and potentially corrupted?

That was the v1 implementation, and specifically objected to during review.

Quoted 8 lines

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 920

Quoted 8 lines
tools/libxc/xc_private.c +7 -0
+                if ( !local_iov )
+                {
+                    local_iov = malloc(iovcnt * sizeof(*iov));
+                    if ( !local_iov )
+                    {
+                        saved_errno = ENOMEM;
+                        goto out;
Quoted 8 lines

What is rc at this point? I think it is 0, but I think you want it to be -1?

It might be better to drop the inialiser of rc and set it in the one or two places which would then need it (the goto out in the skip-0-length loop and just before the out label AFAICT).

Hmm yes - I shall do.

For what it is worth, I can't find any way of provoking a partial write of an individual iov element, and I don't realistically expect this codepath to actually be used. POSIX however doesn't guarantee that it can't happen.

~Andrew

#3 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Ian Campbell
Quoted 6 lines · Andrew Cooper
tools/libxc/xc_private.c +6 -0
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+                goto out;
Quoted 4 lines
Quoted 1 line

Is this required for some reason or just an optimisation?

Experimentally, submitting a writev() with every iov of length 0 results in an EINVAL on CentOS 5.x

How exciting!

Quoted 2 lines

This causes a failure if a partial write adjustment happens and only iov entries of length 0 remain in the set.

If only entries of length 0 remain then isn't that a complete-write of the final non-empty entry?

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
tools/libxc/xc_private.c +15 -0
+
+        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
+        saved_errno = errno;
+
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }
+
+        /* Check iov[] to see whether we had a partial or complete write. */
+        while ( len > 0 && (iov_idx < iovcnt) )
+        {
Quoted 8 lines

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 913

Quoted 6 lines
tools/libxc/xc_private.c +6 -0
+            if ( len >= iov[iov_idx].iov_len )
+                len -= iov[iov_idx++].iov_len;
+            else
+            {
+                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
+                 * element iov_idx and resubmit the rest. */

I suppose we can't / don't want to just declare that the input is non-const and potentially corrupted?

Quoted 1 line

That was the v1 implementation, and specifically objected to during review.

OK.

Quoted 18 lines
Quoted 9 lines

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 920

Quoted 9 lines
tools/libxc/xc_private.c +7 -0
+                if ( !local_iov )
+                {
+                    local_iov = malloc(iovcnt * sizeof(*iov));
+                    if ( !local_iov )
+                    {
+                        saved_errno = ENOMEM;
+                        goto out;
Quoted 8 lines

What is rc at this point? I think it is 0, but I think you want it to be -1?

It might be better to drop the inialiser of rc and set it in the one or two places which would then need it (the goto out in the skip-0-length loop and just before the out label AFAICT).

Quoted 6 lines

Hmm yes - I shall do.

For what it is worth, I can't find any way of provoking a partial write of an individual iov element, and I don't realistically expect this codepath to actually be used. POSIX however doesn't guarantee that it can't happen.

Better safe than sorry then.

Ian.

#4 lore raw
Quoted 11 lines · Ian Campbell
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Ian Campbell

tools/libxc/xc_private.c int write_exact(int fd, const void *data, size_t size) · line 892

Quoted 6 lines · Andrew Cooper
tools/libxc/xc_private.c +6 -0
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+                goto out;

Is this required for some reason or just an optimisation?

Quoted 8 lines
Quoted 2 lines

Experimentally, submitting a writev() with every iov of length 0 results in an EINVAL on CentOS 5.x

How exciting!

Quoted 2 lines

This causes a failure if a partial write adjustment happens and only iov entries of length 0 remain in the set.

If only entries of length 0 remain then isn't that a complete-write of the final non-empty entry?

Hmm - I think you are right. This loop might now be redundant with the partial iov[] handling below. That bit of code did the hokey-cokey several times.

On the other hand, I seem to remember that it ended up like this very deliberately, and that I couldn't cover the edge case with only one loop. I think I am going to have to debug this back into existence again.

~Andrew