Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: base/rand_util_posix.cc

Issue 144010: Cache the file descriptor for /dev/urandom to avoid needing to reopen it. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/rand_util.h" 5 #include "base/rand_util.h"
6 6
7 #include <errno.h>
7 #include <fcntl.h> 8 #include <fcntl.h>
8 #include <unistd.h> 9 #include <unistd.h>
9 10
10 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/lazy_instance.h"
11 #include "base/logging.h" 13 #include "base/logging.h"
12 14
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);
25 CHECK(fd_ >= 0) << "Cannot open /dev/urandom: " << errno;
26 }
27
28 ~URandomFd() {
29 close(fd_);
30 }
31
32 int fd() const { return fd_; }
33
34 private:
35 int fd_;
36 };
37
38 base::LazyInstance<URandomFd> g_urandom_fd(base::LINKER_INITIALIZED);
39
40 } // namespace
41
13 namespace base { 42 namespace base {
14 43
15 uint64 RandUint64() { 44 uint64 RandUint64() {
16 uint64 number; 45 uint64 number;
17 46
18 int urandom_fd = open("/dev/urandom", O_RDONLY); 47 int urandom_fd = g_urandom_fd.Pointer()->fd();
19 CHECK(urandom_fd >= 0);
20 bool success = file_util::ReadFromFD(urandom_fd, 48 bool success = file_util::ReadFromFD(urandom_fd,
21 reinterpret_cast<char*>(&number), 49 reinterpret_cast<char*>(&number),
22 sizeof(number)); 50 sizeof(number));
23 CHECK(success); 51 CHECK(success);
24 close(urandom_fd);
25 52
26 return number; 53 return number;
27 } 54 }
28 55
29 } // namespace base 56 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698