summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-23 12:38:08 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-23 12:38:08 +0000
commit4530149ff00f8970e3b06f6580c109ee74753694 (patch)
tree7541528bc1add772963ad8b889f9dd7dd6eb2b71
parentb2edf0a95849836ab55c65a3fe0ed89a0e4616ea (diff)
downloadchromium_src-4530149ff00f8970e3b06f6580c109ee74753694.zip
chromium_src-4530149ff00f8970e3b06f6580c109ee74753694.tar.gz
chromium_src-4530149ff00f8970e3b06f6580c109ee74753694.tar.bz2
Protect RandUint64 against EINTR.
Review URL: http://codereview.chromium.org/77022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14297 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/file_util.h7
-rw-r--r--base/file_util_posix.cc12
-rw-r--r--base/rand_util_posix.cc8
3 files changed, 24 insertions, 3 deletions
diff --git a/base/file_util.h b/base/file_util.h
index 6728a0d..102dd4b 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -222,6 +222,13 @@ bool ReadFileToString(const FilePath& path, std::string* contents);
// Deprecated version.
bool ReadFileToString(const std::wstring& path, std::string* contents);
+#if defined(OS_POSIX)
+// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
+// in |buffer|. This function is protected against EINTR and partial reads.
+// Returns true iff |bytes| bytes have been successfuly read from |fd|.
+bool ReadFromFD(int fd, char* buffer, size_t bytes);
+#endif // defined(OS_POSIX)
+
#if defined(OS_WIN)
// Resolve Windows shortcut (.LNK file)
// Argument path specifies a valid LNK file. On success, return true and put
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 6a36c62..f571c5e 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -316,6 +316,18 @@ bool GetFileCreationLocalTime(const std::string& filename,
}
#endif
+bool ReadFromFD(int fd, char* buffer, size_t bytes) {
+ size_t total_read = 0;
+ while (total_read < bytes) {
+ ssize_t bytes_read = read(fd, buffer + total_read, bytes - total_read);
+ if (bytes_read > 0)
+ total_read += bytes_read;
+ else if (errno != EINTR || bytes_read == 0)
+ break;
+ }
+ return total_read == bytes;
+}
+
// Creates and opens a temporary file in |directory|, returning the
// file descriptor. |path| is set to the temporary file path.
// Note TODO(erikkay) comment in header for BlahFileName() calls; the
diff --git a/base/rand_util_posix.cc b/base/rand_util_posix.cc
index e0b4257..b4f3ac2 100644
--- a/base/rand_util_posix.cc
+++ b/base/rand_util_posix.cc
@@ -5,9 +5,9 @@
#include "base/rand_util.h"
#include <fcntl.h>
-#include <sys/stat.h>
#include <unistd.h>
+#include "base/file_util.h"
#include "base/logging.h"
namespace base {
@@ -17,8 +17,10 @@ uint64 RandUint64() {
int urandom_fd = open("/dev/urandom", O_RDONLY);
CHECK(urandom_fd >= 0);
- ssize_t bytes_read = read(urandom_fd, &number, sizeof(number));
- CHECK(bytes_read == sizeof(number));
+ bool success = file_util::ReadFromFD(urandom_fd,
+ reinterpret_cast<char*>(&number),
+ sizeof(number));
+ CHECK(success);
close(urandom_fd);
return number;