diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-01 21:37:31 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-01 21:37:31 +0000 |
commit | 157c61b07b3ce5e308db6230da9d93ff74a16ca3 (patch) | |
tree | 7a627dd3c11b684852e2a3ffe84bcfc079bf1316 /base/file_util_posix.cc | |
parent | 105414ec43286c5ba9953eaf66f147a9a1a6afd5 (diff) | |
download | chromium_src-157c61b07b3ce5e308db6230da9d93ff74a16ca3.zip chromium_src-157c61b07b3ce5e308db6230da9d93ff74a16ca3.tar.gz chromium_src-157c61b07b3ce5e308db6230da9d93ff74a16ca3.tar.bz2 |
POSIX: Add a macro for handling EINTR.
On POSIX systems, system calls can be interrupted by signals. In this
case, they'll return EINTR, indicating that the system call needs to
be restarted.
(The situation is a little more complicated than this with SA_RESTART,
but you can read man 7 signal if you like.)
The short of it is that you need to catch EINTR and restart the call
for these system calls:
* read, readv, write, writev, ioctl
* open() when dealing with a fifo
* wait*
* Anything socket based (send*, recv*, connect, accept etc)
* flock and lock control with fcntl
* mq_ functions which can block
* futex
* sem_wait (and timed wait)
* pause, sigsuspend, sigtimedwait, sigwaitinfo
* poll, epoll_wait, select and 'p' versions of the same
* msgrcv, msgsnd, semop, semtimedop
* close (although, on Linux, EINTR won't happen here)
* any sleep functions (careful, you need to handle this are restart
with different arguments)
We've been a little sloppy with this until now. This patch adds a
macro for dealing with this and corrects every case of these system
calls (that I found).
The macro is HANDLE_EINTR in base/eintr_wrapper.h. It's safe to
include on Windows and is a no-op there.
On POSIX, it uses GCC magic to return the correct type based on the
expression and restarts the system call if it throws EINTR.
And you can use it like:
HANDLE_EINTR(close(fd));
Or:
ssize_t bytes_read = HANDLE_EINTR(read(fd, buffer, len));
*BEWARE* that it will evaluate the argument multiple times, so this is
not safe:
HANDLE_EINTR(close(FireMissiles()));
http://groups.google.com/group/chromium-dev/browse_thread/thread/41a35b2a457d73a0
http://codereview.chromium.org/100225
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15102 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_posix.cc')
-rw-r--r-- | base/file_util_posix.cc | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index 1835d3a..06f3c1a 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -22,6 +22,7 @@ #include <fstream> #include "base/basictypes.h" +#include "base/eintr_wrapper.h" #include "base/file_path.h" #include "base/logging.h" #include "base/string_util.h" @@ -335,11 +336,11 @@ bool GetFileCreationLocalTime(const std::string& filename, 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 (bytes_read == 0 || errno != EINTR) + ssize_t bytes_read = + HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); + if (bytes_read <= 0) break; + total_read += bytes_read; } return total_read == bytes; } @@ -453,8 +454,8 @@ int ReadFile(const FilePath& filename, char* data, int size) { if (fd < 0) return -1; - int ret_value = read(fd, data, size); - close(fd); + int ret_value = HANDLE_EINTR(read(fd, data, size)); + HANDLE_EINTR(close(fd)); return ret_value; } @@ -466,17 +467,17 @@ int WriteFile(const FilePath& filename, const char* data, int size) { // Allow for partial writes ssize_t bytes_written_total = 0; do { - ssize_t bytes_written_partial = write(fd, - data + bytes_written_total, - size - bytes_written_total); + ssize_t bytes_written_partial = + HANDLE_EINTR(write(fd, data + bytes_written_total, + size - bytes_written_total)); if (bytes_written_partial < 0) { - close(fd); + HANDLE_EINTR(close(fd)); return -1; } bytes_written_total += bytes_written_partial; } while (bytes_written_total < size); - close(fd); + HANDLE_EINTR(close(fd)); return bytes_written_total; } |