[email protected] | ead8c1f | 2012-05-30 14:26:13 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 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" | ||||
6 | |||||
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 7 | #include <errno.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 9 | #include <unistd.h> |
10 | |||||
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 13 | #include "base/logging.h" |
14 | |||||
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 15 | namespace { |
16 | |||||
17 | // We keep the file descriptor for /dev/urandom around so we don't need to | ||||
18 | // reopen it (which is expensive), and since we may not even be able to reopen | ||||
19 | // it if we are later put in a sandbox. This class wraps the file descriptor so | ||||
20 | // we can use LazyInstance to handle opening it on the first access. | ||||
21 | class URandomFd { | ||||
22 | public: | ||||
23 | URandomFd() { | ||||
24 | fd_ = open("/dev/urandom", O_RDONLY); | ||||
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 25 | DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno; |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 26 | } |
27 | |||||
28 | ~URandomFd() { | ||||
29 | close(fd_); | ||||
30 | } | ||||
31 | |||||
32 | int fd() const { return fd_; } | ||||
33 | |||||
34 | private: | ||||
35 | int fd_; | ||||
36 | }; | ||||
37 | |||||
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 38 | base::LazyInstance<URandomFd> g_urandom_fd = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 39 | |
40 | } // namespace | ||||
41 | |||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 42 | namespace base { |
43 | |||||
[email protected] | ba99012 | 2008-11-14 23:28:29 | [diff] [blame] | 44 | uint64 RandUint64() { |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 45 | uint64 number; |
46 | |||||
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 47 | int urandom_fd = g_urandom_fd.Pointer()->fd(); |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 48 | bool success = file_util::ReadFromFD(urandom_fd, |
49 | reinterpret_cast<char*>(&number), | ||||
50 | sizeof(number)); | ||||
51 | CHECK(success); | ||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 52 | |
53 | return number; | ||||
54 | } | ||||
55 | |||||
[email protected] | 1d87fad | 2010-03-04 20:18:55 | [diff] [blame] | 56 | int GetUrandomFD(void) { |
57 | return g_urandom_fd.Pointer()->fd(); | ||||
58 | } | ||||
[email protected] | ead8c1f | 2012-05-30 14:26:13 | [diff] [blame^] | 59 | |
60 | } // namespace base |