mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-26 23:55:23 +00:00
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>
This commit is contained in:
parent
a6be5a4ce1
commit
8981c750b1
@ -330,9 +330,9 @@ int
|
|||||||
virCryptoGenerateRandom(unsigned char *buf,
|
virCryptoGenerateRandom(unsigned char *buf,
|
||||||
size_t buflen)
|
size_t buflen)
|
||||||
{
|
{
|
||||||
|
#if WITH_GNUTLS
|
||||||
int rv;
|
int rv;
|
||||||
|
|
||||||
#if WITH_GNUTLS
|
|
||||||
/* Generate the byte stream using gnutls_rnd() if possible */
|
/* Generate the byte stream using gnutls_rnd() if possible */
|
||||||
if ((rv = gnutls_rnd(GNUTLS_RND_RANDOM, buf, buflen)) < 0) {
|
if ((rv = gnutls_rnd(GNUTLS_RND_RANDOM, buf, buflen)) < 0) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
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
|
/* If we don't have gnutls_rnd(), we will generate a less cryptographically
|
||||||
* strong master buf from /dev/urandom.
|
* strong master buf from /dev/urandom.
|
||||||
*/
|
*/
|
||||||
if ((rv = virRandomBytes(buf, buflen)) < 0) {
|
if (virRandomBytes(buf, buflen) < 0)
|
||||||
virReportSystemError(-rv, "%s", _("failed to generate byte stream"));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -43,6 +43,8 @@
|
|||||||
|
|
||||||
VIR_LOG_INIT("util.random");
|
VIR_LOG_INIT("util.random");
|
||||||
|
|
||||||
|
#define RANDOM_SOURCE "/dev/urandom"
|
||||||
|
|
||||||
/* The algorithm of virRandomBits relies on gnulib's guarantee that
|
/* The algorithm of virRandomBits relies on gnulib's guarantee that
|
||||||
* 'random_r' matches the POSIX requirements on 'random' of being
|
* 'random_r' matches the POSIX requirements on 'random' of being
|
||||||
* evenly distributed among exactly [0, 2**31) (that is, we always get
|
* evenly distributed among exactly [0, 2**31) (that is, we always get
|
||||||
@ -107,7 +109,6 @@ uint64_t virRandomBits(int nbits)
|
|||||||
if (virRandomInitialize() < 0) {
|
if (virRandomInitialize() < 0) {
|
||||||
/* You're already hosed, so this particular non-random value
|
/* You're already hosed, so this particular non-random value
|
||||||
* isn't any worse. */
|
* isn't any worse. */
|
||||||
VIR_WARN("random number generation is broken");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,10 +166,10 @@ uint32_t virRandomInt(uint32_t max)
|
|||||||
* @buf: Pointer to location to store bytes
|
* @buf: Pointer to location to store bytes
|
||||||
* @buflen: Number of bytes to store
|
* @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
|
* into @buf of size @buflen
|
||||||
*
|
*
|
||||||
* Returns 0 on success or -errno on failure
|
* Returns 0 on success or -1 (with error reported)
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
virRandomBytes(unsigned char *buf,
|
virRandomBytes(unsigned char *buf,
|
||||||
@ -176,13 +177,20 @@ virRandomBytes(unsigned char *buf,
|
|||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
|
if ((fd = open(RANDOM_SOURCE, O_RDONLY)) < 0) {
|
||||||
return -errno;
|
virReportSystemError(errno,
|
||||||
|
_("unable to open %s"),
|
||||||
|
RANDOM_SOURCE);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
while (buflen > 0) {
|
while (buflen > 0) {
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
|
|
||||||
if ((n = saferead(fd, buf, buflen)) <= 0) {
|
if ((n = saferead(fd, buf, buflen)) <= 0) {
|
||||||
|
virReportSystemError(errno,
|
||||||
|
_("unable to read from %s"),
|
||||||
|
RANDOM_SOURCE);
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
return n < 0 ? -errno : -ENODATA;
|
return n < 0 ? -errno : -ENODATA;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user