mirror of https://github.com/aria2/aria2
Comment getrandom code a bit
parent
ee63dff225
commit
c248d456d1
|
@ -109,6 +109,7 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len)
|
||||||
if (rv < -1) {
|
if (rv < -1) {
|
||||||
A2_LOG_ERROR(fmt("Failed to produce randomness: %d", errno));
|
A2_LOG_ERROR(fmt("Failed to produce randomness: %d", errno));
|
||||||
}
|
}
|
||||||
|
// getrandom is not supposed to fail, ever, so, we want to assert here.
|
||||||
assert(rv >= 0 && (size_t)rv == len);
|
assert(rv >= 0 && (size_t)rv == len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,17 +48,29 @@
|
||||||
int getrandom_linux(void *buf, size_t buflen) {
|
int getrandom_linux(void *buf, size_t buflen) {
|
||||||
int rv = 0;
|
int rv = 0;
|
||||||
uint8_t* p = buf;
|
uint8_t* p = buf;
|
||||||
|
|
||||||
|
/* Loop while we did not fully retrieve what the user asked for.
|
||||||
|
* This may happen in particular when a call was EINTRupted.
|
||||||
|
*/
|
||||||
while (buflen) {
|
while (buflen) {
|
||||||
int read;
|
int read;
|
||||||
#ifdef HAVE_GETRANDOM
|
#ifdef HAVE_GETRANDOM
|
||||||
|
/* libc already has support */
|
||||||
read = getrandom(p, buflen, 0);
|
read = getrandom(p, buflen, 0);
|
||||||
#else // HAVE_GETRANDOM
|
#else // HAVE_GETRANDOM
|
||||||
|
/* libc has no support, make the syscall ourselves */
|
||||||
read = syscall(SYS_getrandom, p, buflen, 0);
|
read = syscall(SYS_getrandom, p, buflen, 0);
|
||||||
/* Some libc impl. might mess this up */
|
/* Some libc impl. might mess -ERESTART up */
|
||||||
if (read == -EINTR || read == -ERESTART) {
|
if (read == -EINTR || read == -ERESTART) {
|
||||||
|
/* ERESTART, like EINTR, should restart the call, later, so handle both
|
||||||
|
* the same way.
|
||||||
|
*/
|
||||||
errno = EINTR;
|
errno = EINTR;
|
||||||
read = -1;
|
read = -1;
|
||||||
}
|
}
|
||||||
|
/* Some other non-interrupted error happened, put error code into errno and
|
||||||
|
* switch read to -1 (return value).
|
||||||
|
*/
|
||||||
if (read < -1) {
|
if (read < -1) {
|
||||||
errno = -read;
|
errno = -read;
|
||||||
read = -1;
|
read = -1;
|
||||||
|
@ -66,13 +78,19 @@ int getrandom_linux(void *buf, size_t buflen) {
|
||||||
#endif // HAVE_GETRANDOM
|
#endif // HAVE_GETRANDOM
|
||||||
if (read < 0) {
|
if (read < 0) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
|
/* Restart call */
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
/* Call failed, return -1, errno should be set up correctly at this
|
||||||
|
* point.
|
||||||
|
*/
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
/* We got some more randomness */
|
||||||
p += read;
|
p += read;
|
||||||
rv += read;
|
rv += read;
|
||||||
buflen -= read;
|
buflen -= read;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue