GaiaOAuthClient::Core::GetUserInfo() does not need to send or receive cookies.
[chromium-blink-merge.git] / base / rand_util_posix.cc
blob082d64923d5b9bf63890565bcf922eb361cc3ab3
1 // Copyright (c) 2012 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.
5 #include "base/rand_util.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <unistd.h>
11 #include "base/file_util.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
15 namespace {
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 DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno;
28 ~URandomFd() {
29 close(fd_);
32 int fd() const { return fd_; }
34 private:
35 int fd_;
38 base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER;
40 } // namespace
42 namespace base {
44 // NOTE: This function must be cryptographically secure. http://crbug.com/140076
45 uint64 RandUint64() {
46 uint64 number;
48 int urandom_fd = g_urandom_fd.Pointer()->fd();
49 bool success = ReadFromFD(urandom_fd, reinterpret_cast<char*>(&number),
50 sizeof(number));
51 CHECK(success);
53 return number;
56 int GetUrandomFD(void) {
57 return g_urandom_fd.Pointer()->fd();
60 } // namespace base