[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

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



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 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;
+
 /**
  * VIR_ASSIGN_IS_OVERFLOW:
  * @rvalue: value that is checked (evaluated twice)
diff --git a/src/util/viruuid.c b/src/util/viruuid.c
index 615d419..e2d9fbf 100644
--- a/src/util/viruuid.c
+++ b/src/util/viruuid.c
@@ -53,34 +53,6 @@ VIR_LOG_INIT("util.uuid");
 static unsigned char host_uuid[VIR_UUID_BUFLEN];
 
 static int
-virUUIDGenerateRandomBytes(unsigned char *buf,
-                           int buflen)
-{
-    int fd;
-
-    if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
-        return errno;
-
-    while (buflen > 0) {
-        int 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;
-}
-
-static int
 virUUIDGeneratePseudoRandomBytes(unsigned char *buf,
                                  int buflen)
 {
@@ -108,7 +80,7 @@ virUUIDGenerate(unsigned char *uuid)
     if (uuid == NULL)
         return -1;
 
-    if ((err = virUUIDGenerateRandomBytes(uuid, VIR_UUID_BUFLEN))) {
+    if ((err = virGenerateRandomBytes(uuid, VIR_UUID_BUFLEN))) {
         char ebuf[1024];
         VIR_WARN("Falling back to pseudorandom UUID,"
                  " failed to generate random bytes: %s",
-- 
2.5.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]