mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 1 | // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/rand_util.h" |
agl@chromium.org | ba637ac | 2010-03-05 05:18:55 +0900 | [diff] [blame] | 6 | #include "base/rand_util_c.h" |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 7 | |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 8 | #include <errno.h> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 9 | #include <fcntl.h> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 10 | #include <unistd.h> |
| 11 | |
phajdan.jr@chromium.org | 2372593 | 2009-04-23 21:38:08 +0900 | [diff] [blame] | 12 | #include "base/file_util.h" |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 14 | #include "base/logging.h" |
| 15 | |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // We keep the file descriptor for /dev/urandom around so we don't need to |
| 19 | // reopen it (which is expensive), and since we may not even be able to reopen |
| 20 | // it if we are later put in a sandbox. This class wraps the file descriptor so |
| 21 | // we can use LazyInstance to handle opening it on the first access. |
| 22 | class URandomFd { |
| 23 | public: |
| 24 | URandomFd() { |
| 25 | fd_ = open("/dev/urandom", O_RDONLY); |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame^] | 26 | DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno; |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ~URandomFd() { |
| 30 | close(fd_); |
| 31 | } |
| 32 | |
| 33 | int fd() const { return fd_; } |
| 34 | |
| 35 | private: |
| 36 | int fd_; |
| 37 | }; |
| 38 | |
| 39 | base::LazyInstance<URandomFd> g_urandom_fd(base::LINKER_INITIALIZED); |
| 40 | |
| 41 | } // namespace |
| 42 | |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 43 | namespace base { |
| 44 | |
deanm@chromium.org | 48c40ce | 2008-11-15 08:28:29 +0900 | [diff] [blame] | 45 | uint64 RandUint64() { |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 46 | uint64 number; |
| 47 | |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 48 | int urandom_fd = g_urandom_fd.Pointer()->fd(); |
phajdan.jr@chromium.org | 2372593 | 2009-04-23 21:38:08 +0900 | [diff] [blame] | 49 | bool success = file_util::ReadFromFD(urandom_fd, |
| 50 | reinterpret_cast<char*>(&number), |
| 51 | sizeof(number)); |
| 52 | CHECK(success); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 53 | |
| 54 | return number; |
| 55 | } |
| 56 | |
| 57 | } // namespace base |
agl@chromium.org | ba637ac | 2010-03-05 05:18:55 +0900 | [diff] [blame] | 58 | |
| 59 | int GetUrandomFD(void) { |
| 60 | return g_urandom_fd.Pointer()->fd(); |
| 61 | } |