(Wangle) Shrink Core to 128 bytes
[folly.git] / folly / Random.cpp
index 06b3d5aea0b20a7f60e4f52d8b3b407119357957..fcebfde9f208ba8df0a0c5eaf6d18bea4b97ae5f 100644 (file)
@@ -31,9 +31,11 @@ namespace folly {
 namespace {
 
 void readRandomDevice(void* data, size_t size) {
-  // Keep it open for the duration of the program
-  static File randomDevice("/dev/urandom");
-  PCHECK(readFull(randomDevice.fd(), data, size) == size);
+  // Keep the random device open for the duration of the program.
+  static int randomFd = ::open("/dev/urandom", O_RDONLY);
+  PCHECK(randomFd >= 0);
+  auto bytesRead = readFull(randomFd, data, size);
+  PCHECK(bytesRead >= 0 && size_t(bytesRead) == size);
 }
 
 class BufferedRandomDevice {
@@ -90,11 +92,11 @@ void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) {
   ptr_ += size;
 }
 
-ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
 
 }  // namespace
 
 void Random::secureRandom(void* data, size_t size) {
+  static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
   bufferedRandomDevice->get(data, size);
 }