summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichal Privoznik <mprivozn@redhat.com>2018-05-29 08:58:46 +0200
committerMichal Privoznik <mprivozn@redhat.com>2018-06-05 10:31:19 +0200
commit8981c750b111c8a549a888d066f5e5c736787326 (patch)
tree085054810d2151ceb9504ee80a7ac9b36f7fd1e7
parenta6be5a4ce105975fd106f094411671d0d3b69445 (diff)
downloadlibvirt-8981c750b111c8a549a888d066f5e5c736787326.tar.gz
virRandomBytes: Report error
Instead of having each caller report error move it into the function. This way we can produce more accurate error messages too. Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
-rw-r--r--src/util/vircrypto.c6
-rw-r--r--src/util/virrandom.c18
2 files changed, 15 insertions, 9 deletions
diff --git a/src/util/vircrypto.c b/src/util/vircrypto.c
index 673e1648e8..e5f2319720 100644
--- a/src/util/vircrypto.c
+++ b/src/util/vircrypto.c
@@ -330,9 +330,9 @@ int
virCryptoGenerateRandom(unsigned char *buf,
size_t buflen)
{
+#if WITH_GNUTLS
int rv;
-#if WITH_GNUTLS
/* Generate the byte stream using gnutls_rnd() if possible */
if ((rv = gnutls_rnd(GNUTLS_RND_RANDOM, buf, buflen)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -344,10 +344,8 @@ virCryptoGenerateRandom(unsigned char *buf,
/* If we don't have gnutls_rnd(), we will generate a less cryptographically
* strong master buf from /dev/urandom.
*/
- if ((rv = virRandomBytes(buf, buflen)) < 0) {
- virReportSystemError(-rv, "%s", _("failed to generate byte stream"));
+ if (virRandomBytes(buf, buflen) < 0)
return -1;
- }
#endif
return 0;
diff --git a/src/util/virrandom.c b/src/util/virrandom.c
index 4283611065..230745d311 100644
--- a/src/util/virrandom.c
+++ b/src/util/virrandom.c
@@ -43,6 +43,8 @@
VIR_LOG_INIT("util.random");
+#define RANDOM_SOURCE "/dev/urandom"
+
/* The algorithm of virRandomBits relies on gnulib's guarantee that
* 'random_r' matches the POSIX requirements on 'random' of being
* evenly distributed among exactly [0, 2**31) (that is, we always get
@@ -107,7 +109,6 @@ uint64_t virRandomBits(int nbits)
if (virRandomInitialize() < 0) {
/* You're already hosed, so this particular non-random value
* isn't any worse. */
- VIR_WARN("random number generation is broken");
return 0;
}
@@ -165,10 +166,10 @@ uint32_t virRandomInt(uint32_t max)
* @buf: Pointer to location to store bytes
* @buflen: Number of bytes to store
*
- * Generate a stream of random bytes from /dev/urandom
+ * Generate a stream of random bytes from RANDOM_SOURCE
* into @buf of size @buflen
*
- * Returns 0 on success or -errno on failure
+ * Returns 0 on success or -1 (with error reported)
*/
int
virRandomBytes(unsigned char *buf,
@@ -176,13 +177,20 @@ virRandomBytes(unsigned char *buf,
{
int fd;
- if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
- return -errno;
+ if ((fd = open(RANDOM_SOURCE, O_RDONLY)) < 0) {
+ virReportSystemError(errno,
+ _("unable to open %s"),
+ RANDOM_SOURCE);
+ return -1;
+ }
while (buflen > 0) {
ssize_t n;
if ((n = saferead(fd, buf, buflen)) <= 0) {
+ virReportSystemError(errno,
+ _("unable to read from %s"),
+ RANDOM_SOURCE);
VIR_FORCE_CLOSE(fd);
return n < 0 ? -errno : -ENODATA;
}