[libvirt] [PATCH 1/4] Introduce new API for generating random numbers

Eric Blake eblake at redhat.com
Wed Jan 25 18:20:23 UTC 2012


On 01/25/2012 09:38 AM, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange" <berrange at redhat.com>
> 
> The old virRandom() API was not generating good random numbers.
> Replace it with a new API virRandomBits which instead of being
> told the upper limit, gets told the number of bits of randomness
> required.
> 
> * src/util/virrandom.c, src/util/virrandom.h: Add virRandomBits,
>   and move virRandomInitialize
> * src/util/util.h, src/util/util.c: Delete virRandom and
>   virRandomInitialize
> * src/libvirt.c, src/security/security_selinux.c,
>   src/test/test_driver.c, src/util/iohelper.c: Update for
>   changes from virRandom to virRandomBits
> * src/storage/storage_backend_iscsi.c: Remove bogus call
>   to virRandomInitialize & convert to virRandomBits
> ---

> +++ b/src/storage/storage_backend_iscsi.c

> -
> -    snprintf(temp_ifacename, sizeof(temp_ifacename), "libvirt-iface-%08x",
> -             virRandom(1024 * 1024 * 1024));
> +    snprintf(temp_ifacename, sizeof(temp_ifacename), "libvirt-iface-%08llx",
> +             (unsigned long long)virRandomBits(30));

Hmm - the cast is indeed necessary if we don't want to rely on PRIx64,
thanks to the change in return type as we switch to the new function;
knowing we only have 30 bits, we could get away with a smaller cast, as in:
 "%08x", (unsigned)virRandomBits(30)

but this is not enough of an issue to be worth changing.

> +
> +
> +uint64_t virRandomBits(int nbits)
> +{

You lost my documentation comments from my proposal:

/* Return an evenly distributed random number between [0,2^nbits), where
nbits must be in the range (0,64]. */

Also, the tests failed to compile.  Squash this in, and then you have my
ACK:

diff --git i/tests/testutils.c w/tests/testutils.c
index acdfdc1..fccea17 100644
--- i/tests/testutils.c
+++ w/tests/testutils.c
@@ -1,7 +1,7 @@
 /*
  * testutils.c: basic test utils
  *
- * Copyright (C) 2005-2011 Red Hat, Inc.
+ * Copyright (C) 2005-2012 Red Hat, Inc.
  *
  * See COPYING.LIB for the License of this software
  *
@@ -34,6 +34,7 @@
 #include "buf.h"
 #include "logging.h"
 #include "command.h"
+#include "virrandom.h"

 #if TEST_OOM_TRACE
 # include <execinfo.h>

-- 
Eric Blake   eblake at redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120125/64e10586/attachment-0001.sig>


More information about the libvir-list mailing list