diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2015-10-06 18:10:40 -0700 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2015-10-06 18:10:40 -0700 |
commit | 598a1d8dd7f621bb75a7f0537c5a63ee93e85a4c (patch) | |
tree | f6847421ec14b63158c05a4f357587c7dacf5959 | |
parent | 64aaea3978533a6643850f6ed05a9afe7f4dc7be (diff) | |
download | infinitytier-598a1d8dd7f621bb75a7f0537c5a63ee93e85a4c.tar.gz infinitytier-598a1d8dd7f621bb75a7f0537c5a63ee93e85a4c.zip |
Try reopening /dev/urandom if there is a problem.
-rw-r--r-- | node/Utils.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/node/Utils.cpp b/node/Utils.cpp index 3c6dee32..6c5d8c7d 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -181,7 +181,7 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes) #ifdef __UNIX_LIKE__ - static char randomBuf[65536]; + static char randomBuf[131072]; static unsigned int randomPtr = sizeof(randomBuf); static int devURandomFd = -1; static Mutex globalLock; @@ -199,10 +199,16 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes) for(unsigned int i=0;i<bytes;++i) { if (randomPtr >= sizeof(randomBuf)) { - if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) { - fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to read from /dev/urandom\n"); - exit(1); - return; + for(;;) { + if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) { + ::close(devURandomFd); + devURandomFd = ::open("/dev/urandom",O_RDONLY); + if (devURandomFd <= 0) { + fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n"); + exit(1); + return; + } + } else break; } randomPtr = 0; } |