/* If we don't have gnutls_rnd(), we will generate a less cryptographically
* strong master buf from /dev/urandom.
*/
- if ((ret = virRandomBytes(buf, nbytes))) {
- virReportSystemError(ret, "%s", _("failed to generate byte stream"));
+ if ((ret = virRandomBytes(buf, nbytes)) < 0) {
+ virReportSystemError(-ret, "%s", _("failed to generate byte stream"));
VIR_FREE(buf);
return NULL;
}
* Generate a stream of random bytes from /dev/urandom
* into @buf of size @buflen
*
- * Returns 0 on success or an errno on failure
+ * Returns 0 on success or -errno on failure
*/
int
virRandomBytes(unsigned char *buf,
int fd;
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
- return errno;
+ return -errno;
while (buflen > 0) {
ssize_t n;
if (errno == EINTR)
continue;
VIR_FORCE_CLOSE(fd);
- return n < 0 ? errno : ENODATA;
+ return n < 0 ? -errno : -ENODATA;
}
buf += n;
if (uuid == NULL)
return -1;
- if ((err = virRandomBytes(uuid, VIR_UUID_BUFLEN))) {
+ if ((err = virRandomBytes(uuid, VIR_UUID_BUFLEN)) < 0) {
char ebuf[1024];
VIR_WARN("Falling back to pseudorandom UUID,"
" failed to generate random bytes: %s",
- virStrerror(err, ebuf, sizeof(ebuf)));
+ virStrerror(-err, ebuf, sizeof(ebuf)));
err = virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
}
VIR_ALLOC_N(iv, ivlen) < 0)
goto cleanup;
- if (virRandomBytes(enckey, enckeylen) ||
- virRandomBytes(iv, ivlen)) {
+ if (virRandomBytes(enckey, enckeylen) < 0 ||
+ virRandomBytes(iv, ivlen) < 0) {
fprintf(stderr, "Failed to generate random bytes\n");
goto cleanup;
}