crypto: Do not fail for EINTR during qcrypto_random_bytes

We can always get EINTR for read; /dev/urandom is no exception.

Rearrange the order of tests for likelihood; allow degenerate buflen==0
case to perform a no-op zero-length read.  This means that the normal
success path is a straight line with a single test for success.

Reviewed-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2019-03-13 20:47:32 -07:00
parent 14a356f475
commit 25fb26e4f4
1 changed files with 15 additions and 21 deletions

View File

@ -65,29 +65,23 @@ int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED,
"Unable to read random bytes"); "Unable to read random bytes");
return -1; return -1;
} }
return 0;
#else #else
int ret = -1; while (1) {
int got; ssize_t got = read(fd, buf, buflen);
if (likely(got == buflen)) {
while (buflen > 0) { return 0;
got = read(fd, buf, buflen); }
if (got < 0) { if (got > 0) {
error_setg_errno(errp, errno, buflen -= got;
"Unable to read random bytes"); buf += got;
goto cleanup; } else if (got == 0) {
} else if (!got) { error_setg(errp, "Unexpected EOF reading random bytes");
error_setg(errp, return -1;
"Unexpected EOF reading random bytes"); } else if (errno != EINTR) {
goto cleanup; error_setg_errno(errp, errno, "Unable to read random bytes");
return -1;
} }
buflen -= got;
buf += got;
} }
ret = 0;
cleanup:
return ret;
#endif #endif
return 0;
} }