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(a)redhat.com>
---
src/util/vircrypto.c | 6 ++----
src/util/virrandom.c | 18 +++++++++++++-----
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 ea55fe654d..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 an -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;
}
--
2.16.1