[PATCH v3 1/5] osdep: make readv_writev() work with partial read/write

marcandre.lureau@redhat.com posted 5 patches 3 years, 4 months ago
Maintainers: "Daniel P. Berrangé" <berrange@redhat.com>
There is a newer version of this series
[PATCH v3 1/5] osdep: make readv_writev() work with partial read/write
Posted by marcandre.lureau@redhat.com 3 years, 4 months ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

With a pipe or other reasons, read/write may return less than the
requested bytes. This happens with the test-io-channel-command test on
Windows. glib spawn code uses a binary pipe of 4096 bytes, and the first
read returns that much (although more are requested), for some unclear
reason...

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 util/osdep.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/util/osdep.c b/util/osdep.c
index 60fcbbaebe..b85715a743 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -544,12 +544,17 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
 {
     unsigned i = 0;
     ssize_t ret = 0;
+    ssize_t off = 0;
     while (i < iov_cnt) {
         ssize_t r = do_write
-            ? write(fd, iov[i].iov_base, iov[i].iov_len)
-            : read(fd, iov[i].iov_base, iov[i].iov_len);
+            ? write(fd, iov[i].iov_base + off, iov[i].iov_len - off)
+            : read(fd, iov[i].iov_base + off, iov[i].iov_len - off);
         if (r > 0) {
             ret += r;
+            off += r;
+            if (off < iov[i].iov_len) {
+                continue;
+            }
         } else if (!r) {
             break;
         } else if (errno == EINTR) {
@@ -562,6 +567,7 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
             }
             break;
         }
+        off = 0;
         i++;
     }
     return ret;
-- 
2.37.3


Re: [PATCH v3 1/5] osdep: make readv_writev() work with partial read/write
Posted by Daniel P. Berrangé 3 years, 4 months ago
On Thu, Oct 06, 2022 at 12:12:18PM +0400, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> With a pipe or other reasons, read/write may return less than the
> requested bytes. This happens with the test-io-channel-command test on
> Windows. glib spawn code uses a binary pipe of 4096 bytes, and the first
> read returns that much (although more are requested), for some unclear
> reason...

Wow, lucky this function isn't too widely used historically!

> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  util/osdep.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

> 
> diff --git a/util/osdep.c b/util/osdep.c
> index 60fcbbaebe..b85715a743 100644
> --- a/util/osdep.c
> +++ b/util/osdep.c

There's a comment just above here that says

   helper function for iov_send_recv

which is not true, as iov_send_recv just calls send/recv instead,
again in a loop with the exact same logic flaw.  Care to fix the
comment and also fix iov_send_recv in the same way ?

> @@ -544,12 +544,17 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
>  {
>      unsigned i = 0;
>      ssize_t ret = 0;
> +    ssize_t off = 0;
>      while (i < iov_cnt) {
>          ssize_t r = do_write
> -            ? write(fd, iov[i].iov_base, iov[i].iov_len)
> -            : read(fd, iov[i].iov_base, iov[i].iov_len);
> +            ? write(fd, iov[i].iov_base + off, iov[i].iov_len - off)
> +            : read(fd, iov[i].iov_base + off, iov[i].iov_len - off);
>          if (r > 0) {
>              ret += r;
> +            off += r;
> +            if (off < iov[i].iov_len) {
> +                continue;
> +            }
>          } else if (!r) {
>              break;
>          } else if (errno == EINTR) {
> @@ -562,6 +567,7 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
>              }
>              break;
>          }
> +        off = 0;
>          i++;
>      }
>      return ret;
> -- 
> 2.37.3
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|