xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wen Congyang <wency@cn.fujitsu.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>,
	Xen-devel <xen-devel@lists.xen.org>
Cc: Ian Jackson <Ian.Jackson@eu.citrix.com>,
	Ian Campbell <Ian.Campbell@citrix.com>
Subject: Re: [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
Date: Fri, 18 Jul 2014 09:14:07 +0800	[thread overview]
Message-ID: <53C874DF.2040903@cn.fujitsu.com> (raw)
In-Reply-To: <1405521126-17035-2-git-send-email-andrew.cooper3@citrix.com>

At 07/16/2014 10:32 PM, Andrew Cooper Wrote:
> 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.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> CC: Ian Campbell <Ian.Campbell@citrix.com>
> CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
> 
> ---
> 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 |   60 ++++++++++++++++++++++++++++++++++++++++++++++
>  tools/libxc/xc_private.h |    2 ++
>  2 files changed, 62 insertions(+)
> 
> diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
> index 1c214dd..0941b06 100644
> --- a/tools/libxc/xc_private.c
> +++ b/tools/libxc/xc_private.c
> @@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
>      return 0;
>  }
>  
> +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;

set saved_errn to 0 before 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;
> +}
> +
>  int xc_ffs8(uint8_t x)
>  {
>      int i;
> diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
> index ce7e81e..1f06166 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,7 @@ 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, const struct iovec *iov, int iovcnt);
>  
>  int xc_ffs8(uint8_t x);
>  int xc_ffs16(uint16_t x);
> 

  reply	other threads:[~2014-07-18  1:14 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-16 14:32 [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Andrew Cooper
2014-07-16 14:32 ` [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact() Andrew Cooper
2014-07-18  1:14   ` Wen Congyang [this message]
2014-07-18  9:20     ` Andrew Cooper
2014-07-18  9:31       ` [PATCH v4 " Andrew Cooper
2014-07-18  9:53       ` [Patch v3 " Ian Campbell
2014-07-18 10:05         ` Wen Congyang
2014-07-18 12:32           ` Andrew Cooper
2014-07-17 14:15 ` [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Ian Campbell
2014-07-18 12:43   ` Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53C874DF.2040903@cn.fujitsu.com \
    --to=wency@cn.fujitsu.com \
    --cc=Ian.Campbell@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).