]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Introduce virRandomBytes
authorJohn Ferlan <jferlan@redhat.com>
Tue, 29 Mar 2016 22:15:33 +0000 (18:15 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Thu, 7 Apr 2016 00:27:09 +0000 (20:27 -0400)
Using the existing virUUIDGenerateRandomBytes, move API to virrandom.c
rename it to virRandomBytes and add it to libvirt_private.syms.

This will be used as a fallback for generating a domain master key.

src/libvirt_private.syms
src/util/virrandom.c
src/util/virrandom.h
src/util/viruuid.c

index 684f06cd4f16e768245b1d72aa0cf41c68475303..8bb78d95027877a27553c2e4638b8237fb6f54fb 100644 (file)
@@ -2094,6 +2094,7 @@ virProcessWait;
 # util/virrandom.h
 virRandom;
 virRandomBits;
+virRandomBytes;
 virRandomGenerateWWN;
 virRandomInt;
 
index 67a8bd05544046ad3af28a5f8cdc79062465d587..62a0e314e664ec2753644c807294e6c2f0e10737 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2015 Red Hat, Inc.
+ * Copyright (C) 2012-2016 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 #include <math.h>
 #include <strings.h>
 #include <time.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #include "virrandom.h"
 #include "virthread.h"
 #include "count-one-bits.h"
 #include "virutil.h"
 #include "virerror.h"
+#include "virfile.h"
 #include "virlog.h"
 #include "virstring.h"
 
@@ -156,11 +160,49 @@ uint32_t virRandomInt(uint32_t max)
 }
 
 
+/**
+ * virRandomBytes
+ * @buf: Pointer to location to store bytes
+ * @buflen: Number of bytes to store
+ *
+ * Generate a stream of random bytes from /dev/urandom
+ * into @buf of size @buflen
+ */
+int
+virRandomBytes(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;
+}
+
+
 #define QUMRANET_OUI "001a4a"
 #define VMWARE_OUI "000569"
 #define MICROSOFT_OUI "0050f2"
 #define XEN_OUI "00163e"
 
+
 int
 virRandomGenerateWWN(char **wwn,
                      const char *virt_type)
index 769df94c44293980e6ed15d6723394804465f116..f457d2de66cd2ed56917d61b8157ef784ce2ce47 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 Red Hat, Inc.
+ * Copyright (C) 2012, 2016 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -27,6 +27,8 @@
 uint64_t virRandomBits(int nbits);
 double virRandom(void);
 uint32_t virRandomInt(uint32_t max);
+int virRandomBytes(unsigned char *buf, size_t buflen)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 int virRandomGenerateWWN(char **wwn, const char *virt_type);
 
 #endif /* __VIR_RANDOM_H__ */
index 615d419e034cd0444fc37a446e778249e1de9fc7..1fcc95405b5b1fa7fe535762d629f5e7bb8bd7d3 100644 (file)
@@ -52,34 +52,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 = virRandomBytes(uuid, VIR_UUID_BUFLEN))) {
         char ebuf[1024];
         VIR_WARN("Falling back to pseudorandom UUID,"
                  " failed to generate random bytes: %s",