[libvirt] [PATCH v2 2/4] util: Introduce virGenerateRandomBytes

Daniel P. Berrange berrange at redhat.com
Mon Apr 4 09:36:36 UTC 2016


On Tue, Mar 29, 2016 at 07:11:34PM -0400, John Ferlan wrote:
> Using the existing virUUIDGenerateRandomBytes, move API to virutil.c
> and add it to libvirt_private.syms.
> 
> This will be used as a fallback for generating a domain master key.
> 
> Signed-off-by: John Ferlan <jferlan at redhat.com>
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virutil.c       | 36 ++++++++++++++++++++++++++++++++++++
>  src/util/virutil.h       |  3 +++
>  src/util/viruuid.c       | 30 +-----------------------------
>  4 files changed, 41 insertions(+), 29 deletions(-)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 7c44047..3d54c39 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -2422,6 +2422,7 @@ virEnumToString;
>  virFindFCHostCapableVport;
>  virFindSCSIHostByPCI;
>  virFormatIntDecimal;
> +virGenerateRandomBytes;
>  virGetDeviceID;
>  virGetDeviceUnprivSGIO;
>  virGetEnvAllowSUID;
> diff --git a/src/util/virutil.c b/src/util/virutil.c
> index b401f8d..c55f6f6 100644
> --- a/src/util/virutil.c
> +++ b/src/util/virutil.c
> @@ -2669,3 +2669,39 @@ virMemoryMaxValue(bool capped)
>      else
>          return LLONG_MAX;
>  }
> +
> +
> +/**
> + * virGenerateRandomBytes
> + * @buf: Pointer to location to store bytes
> + * @buflen: Number of bytes to store
> + *
> + * Generate a stream of random bytes into @buf of size @buflen
> + */
> +int
> +virGenerateRandomBytes(unsigned char *buf,
> +                       size_t buflen)
> +{
> +    int fd;
> +
> +    if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
> +        return errno;
> +
> +    while (buflen > 0) {
> +        ssize_t n;
> +
> +        if ((n = read(fd, buf, buflen)) <= 0) {
> +            if (errno == EINTR)
> +                continue;
> +            VIR_FORCE_CLOSE(fd);
> +            return n < 0 ? errno : ENODATA;
> +        }
> +
> +        buf += n;
> +        buflen -= n;
> +    }
> +
> +    VIR_FORCE_CLOSE(fd);
> +
> +    return 0;
> +}
> diff --git a/src/util/virutil.h b/src/util/virutil.h
> index b121de0..a398b38 100644
> --- a/src/util/virutil.h
> +++ b/src/util/virutil.h
> @@ -254,6 +254,9 @@ unsigned long long virMemoryLimitTruncate(unsigned long long value);
>  bool virMemoryLimitIsSet(unsigned long long value);
>  unsigned long long virMemoryMaxValue(bool ulong);
>  
> +int virGenerateRandomBytes(unsigned char *buf, size_t buflen)
> +    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
> +

Please call this   virRandomBytes() and put it in virrandom.{c,h}

ACK if you make that change before applying.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list