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 | |
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')
-rw-r--r-- | base/debug_util_posix.cc | 5 | ||||
-rw-r--r-- | base/directory_watcher_inotify.cc | 24 | ||||
-rw-r--r-- | base/eintr_wrapper.h | 33 | ||||
-rw-r--r-- | base/file_descriptor_shuffle.cc | 20 | ||||
-rw-r--r-- | base/file_util_linux.cc | 11 | ||||
-rw-r--r-- | base/file_util_posix.cc | 23 | ||||
-rw-r--r-- | base/message_pump_glib.cc | 5 | ||||
-rw-r--r-- | base/message_pump_libevent.cc | 5 | ||||
-rw-r--r-- | base/process_util_linux.cc | 3 | ||||
-rw-r--r-- | base/process_util_mac.mm | 3 | ||||
-rw-r--r-- | base/process_util_posix.cc | 40 | ||||
-rw-r--r-- | base/process_util_unittest.cc | 22 |
12 files changed, 104 insertions, 90 deletions
diff --git a/base/debug_util_posix.cc b/base/debug_util_posix.cc index ced59c8..34cc45c 100644 --- a/base/debug_util_posix.cc +++ b/base/debug_util_posix.cc @@ -15,6 +15,7 @@ #include <unistd.h> #include "base/basictypes.h" +#include "base/eintr_wrapper.h" #include "base/logging.h" #include "base/scoped_ptr.h" #include "base/string_piece.h" @@ -85,8 +86,8 @@ bool DebugUtil::BeingDebugged() { // This simplifies and speeds up things considerably. char buf[1024]; - ssize_t num_read = read(status_fd, buf, sizeof(buf)); - close(status_fd); + ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); + HANDLE_EINTR(close(status_fd)); if (num_read <= 0) return false; diff --git a/base/directory_watcher_inotify.cc b/base/directory_watcher_inotify.cc index a17e1e4..86e7bd9 100644 --- a/base/directory_watcher_inotify.cc +++ b/base/directory_watcher_inotify.cc @@ -16,6 +16,7 @@ #include <utility> #include <vector> +#include "base/eintr_wrapper.h" #include "base/file_path.h" #include "base/hash_tables.h" #include "base/lock.h" @@ -100,11 +101,10 @@ class InotifyReaderTask : public Task { FD_SET(shutdown_fd_, &rfds); // Wait until some inotify events are available. - int select_result = select(std::max(inotify_fd_, shutdown_fd_) + 1, - &rfds, NULL, NULL, NULL); + int select_result = + HANDLE_EINTR(select(std::max(inotify_fd_, shutdown_fd_) + 1, + &rfds, NULL, NULL, NULL)); if (select_result < 0) { - if (errno == EINTR) - continue; DLOG(WARNING) << "select failed: " << strerror(errno); return; } @@ -114,7 +114,8 @@ class InotifyReaderTask : public Task { // Adjust buffer size to current event queue size. int buffer_size; - int ioctl_result = ioctl(inotify_fd_, FIONREAD, &buffer_size); + int ioctl_result = HANDLE_EINTR(ioctl(inotify_fd_, FIONREAD, + &buffer_size)); if (ioctl_result != 0) { DLOG(WARNING) << "ioctl failed: " << strerror(errno); @@ -123,10 +124,8 @@ class InotifyReaderTask : public Task { std::vector<char> buffer(buffer_size); - ssize_t bytes_read; - do { - bytes_read = read(inotify_fd_, &buffer[0], buffer_size); - } while (bytes_read < 0 && errno == EINTR); + ssize_t bytes_read = HANDLE_EINTR(read(inotify_fd_, &buffer[0], + buffer_size)); if (bytes_read < 0) { DLOG(WARNING) << "read from inotify fd failed: " << strerror(errno); @@ -188,12 +187,7 @@ InotifyReader::~InotifyReader() { if (valid_) { // Write to the self-pipe so that the select call in InotifyReaderTask // returns. - ssize_t bytes_written; - do { - bytes_written = write(shutdown_pipe_[1], "", 1); - if (bytes_written == 0) - continue; - } while (bytes_written == -1 && errno == EINTR); + HANDLE_EINTR(write(shutdown_pipe_[1], "", 1)); thread_.Stop(); } if (inotify_fd_ >= 0) diff --git a/base/eintr_wrapper.h b/base/eintr_wrapper.h new file mode 100644 index 0000000..a3fb1e4 --- /dev/null +++ b/base/eintr_wrapper.h @@ -0,0 +1,33 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This provides a wrapper around system calls which may be interrupted by a +// signal and return EINTR. See man 7 signal. +// +// On Windows, this wrapper macro does nothing. + +#ifndef BASE_EINTR_WRAPPER_H_ +#define BASE_EINTR_WRAPPER_H_ + +#include "build/build_config.h" + +#if defined(OS_POSIX) + +#include <errno.h> + +#define HANDLE_EINTR(x) ({ \ + typeof(x) __eintr_result__; \ + do { \ + __eintr_result__ = x; \ + } while (__eintr_result__ == -1 && errno == EINTR); \ + __eintr_result__;\ +}) + +#else + +#define HANDLE_EINTR(x) x + +#endif // OS_POSIX + +#endif // !BASE_EINTR_WRAPPER_H_ diff --git a/base/file_descriptor_shuffle.cc b/base/file_descriptor_shuffle.cc index 1426155..28447c9 100644 --- a/base/file_descriptor_shuffle.cc +++ b/base/file_descriptor_shuffle.cc @@ -7,6 +7,7 @@ #include <errno.h> #include <unistd.h> +#include "base/eintr_wrapper.h" #include "base/logging.h" namespace base { @@ -64,29 +65,16 @@ bool PerformInjectiveMultimap(const InjectiveMultimap& m_in, } bool FileDescriptorTableInjection::Duplicate(int* result, int fd) { - do { - *result = dup(fd); - } while(*result == -1 && errno == EINTR); - + *result = HANDLE_EINTR(dup(fd)); return *result >= 0; } bool FileDescriptorTableInjection::Move(int src, int dest) { - int result; - - do { - result = dup2(src, dest); - } while (result == -1 && errno == EINTR); - - return result != -1; + return HANDLE_EINTR(dup2(src, dest)) != -1; } void FileDescriptorTableInjection::Close(int fd) { - int result; - - do { - result = close(fd); - } while (result == -1 && errno == EINTR); + HANDLE_EINTR(close(fd)); } } // namespace base diff --git a/base/file_util_linux.cc b/base/file_util_linux.cc index d595fe1..0f9795e 100644 --- a/base/file_util_linux.cc +++ b/base/file_util_linux.cc @@ -9,6 +9,7 @@ #include <string> #include <vector> +#include "base/eintr_wrapper.h" #include "base/file_path.h" #include "base/string_util.h" @@ -44,7 +45,7 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) { bool result = true; while (result) { - ssize_t bytes_read = read(infile, &buffer[0], buffer.size()); + ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); if (bytes_read < 0) { result = false; break; @@ -54,10 +55,10 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) { // Allow for partial writes ssize_t bytes_written_per_read = 0; do { - ssize_t bytes_written_partial = write( + ssize_t bytes_written_partial = HANDLE_EINTR(write( outfile, &buffer[bytes_written_per_read], - bytes_read - bytes_written_per_read); + bytes_read - bytes_written_per_read)); if (bytes_written_partial < 0) { result = false; break; @@ -66,9 +67,9 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) { } while (bytes_written_per_read < bytes_read); } - if (close(infile) < 0) + if (HANDLE_EINTR(close(infile)) < 0) result = false; - if (close(outfile) < 0) + if (HANDLE_EINTR(close(outfile)) < 0) result = false; return result; 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; } diff --git a/base/message_pump_glib.cc b/base/message_pump_glib.cc index c18bfd3..da40f26 100644 --- a/base/message_pump_glib.cc +++ b/base/message_pump_glib.cc @@ -7,6 +7,7 @@ #include <fcntl.h> #include <math.h> +#include "base/eintr_wrapper.h" #include "base/lazy_instance.h" #include "base/logging.h" #include "base/platform_thread.h" @@ -177,7 +178,7 @@ void MessagePumpForUI::HandleDispatch() { // poll will tell us whether there was data, so this read shouldn't block. if (wakeup_gpollfd_.revents & G_IO_IN) { char msg; - if (read(wakeup_pipe_read_, &msg, 1) != 1 || msg != '!') { + if (HANDLE_EINTR(read(wakeup_pipe_read_, &msg, 1)) != 1 || msg != '!') { NOTREACHED() << "Error reading from the wakeup pipe."; } } @@ -222,7 +223,7 @@ void MessagePumpForUI::ScheduleWork() { // variables as we would then need locks all over. This ensures that if // we are sleeping in a poll that we will wake up. char msg = '!'; - if (write(wakeup_pipe_write_, &msg, 1) != 1) { + if (HANDLE_EINTR(write(wakeup_pipe_write_, &msg, 1)) != 1) { NOTREACHED() << "Could not write to the UI message loop wakeup pipe!"; } } diff --git a/base/message_pump_libevent.cc b/base/message_pump_libevent.cc index f2681f3..a940081 100644 --- a/base/message_pump_libevent.cc +++ b/base/message_pump_libevent.cc @@ -7,6 +7,7 @@ #include <errno.h> #include <fcntl.h> +#include "eintr_wrapper.h" #include "base/logging.h" #include "base/scoped_nsautorelease_pool.h" #include "base/scoped_ptr.h" @@ -87,7 +88,7 @@ void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) { // Remove and discard the wakeup byte. char buf; - int nread = read(socket, &buf, 1); + int nread = HANDLE_EINTR(read(socket, &buf, 1)); DCHECK_EQ(nread, 1); // Tell libevent to break out of inner loop. event_base_loopbreak(that->event_base_); @@ -272,7 +273,7 @@ void MessagePumpLibevent::Quit() { void MessagePumpLibevent::ScheduleWork() { // Tell libevent (in a threadsafe way) that it should break out of its loop. char buf = 0; - int nwrite = write(wakeup_pipe_in_, &buf, 1); + int nwrite = HANDLE_EINTR(write(wakeup_pipe_in_, &buf, 1)); DCHECK(nwrite == 1 || errno == EAGAIN) << "[nwrite:" << nwrite << "] [errno:" << errno << "]"; } diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index f8e8a04..b542e6f 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -12,6 +12,7 @@ #include <sys/types.h> #include <sys/wait.h> +#include "base/eintr_wrapper.h" #include "base/file_util.h" #include "base/logging.h" #include "base/string_tokenizer.h" @@ -55,7 +56,7 @@ bool LaunchApp(const std::vector<std::string>& argv, exit(127); } else { if (wait) - waitpid(pid, 0, 0); + HANDLE_EINTR(waitpid(pid, 0, 0)); if (process_handle) *process_handle = pid; diff --git a/base/process_util_mac.mm b/base/process_util_mac.mm index f489bd0..bcdaffe 100644 --- a/base/process_util_mac.mm +++ b/base/process_util_mac.mm @@ -14,6 +14,7 @@ #include <string> +#include "base/eintr_wrapper.h" #include "base/logging.h" #include "base/string_util.h" #include "base/time.h" @@ -76,7 +77,7 @@ bool LaunchApp(const std::vector<std::string>& argv, retval = false; } else { if (wait) - waitpid(pid, 0, 0); + HANDLE_EINTR(waitpid(pid, 0, 0)); if (process_handle) *process_handle = pid; diff --git a/base/process_util_posix.cc b/base/process_util_posix.cc index c0e792b..e66576a 100644 --- a/base/process_util_posix.cc +++ b/base/process_util_posix.cc @@ -17,6 +17,7 @@ #include <set> #include "base/basictypes.h" +#include "base/eintr_wrapper.h" #include "base/logging.h" #include "base/platform_thread.h" #include "base/process_util.h" @@ -69,7 +70,7 @@ bool KillProcess(ProcessHandle process_id, int exit_code, bool wait) { int tries = 60; // The process may not end immediately due to pending I/O while (tries-- > 0) { - int pid = waitpid(process_id, NULL, WNOHANG); + int pid = HANDLE_EINTR(waitpid(process_id, NULL, WNOHANG)); if (pid == process_id) break; @@ -139,10 +140,7 @@ void CloseSuperfluousFds(const base::InjectiveMultimap& saved_mapping) { if (saved_fds.find(fd) != saved_fds.end()) continue; - int result; - do { - result = close(fd); - } while (result == -1 && errno == EINTR); + HANDLE_EINTR(close(fd)); } return; } @@ -161,10 +159,7 @@ void CloseSuperfluousFds(const base::InjectiveMultimap& saved_mapping) { if (saved_fds.find(fd) != saved_fds.end()) continue; - int result; - do { - result = close(fd); - } while (result == -1 && errno == EINTR); + HANDLE_EINTR(close(fd)); } } @@ -226,7 +221,7 @@ void RaiseProcessToHighPriority() { bool DidProcessCrash(bool* child_exited, ProcessHandle handle) { int status; - const int result = waitpid(handle, &status, WNOHANG); + const int result = HANDLE_EINTR(waitpid(handle, &status, WNOHANG)); if (result == -1) { LOG(ERROR) << "waitpid failed pid:" << handle << " errno:" << errno; if (child_exited) @@ -262,11 +257,9 @@ bool DidProcessCrash(bool* child_exited, ProcessHandle handle) { bool WaitForExitCode(ProcessHandle handle, int* exit_code) { int status; - while (waitpid(handle, &status, 0) == -1) { - if (errno != EINTR) { - NOTREACHED(); - return false; - } + if (HANDLE_EINTR(waitpid(handle, &status, 0)) == -1) { + NOTREACHED(); + return false; } if (WIFEXITED(status)) { @@ -305,7 +298,7 @@ int WaitpidWithTimeout(ProcessHandle handle, int wait_milliseconds, // This function is used primarily for unit tests, if we want to use it in // the application itself it would probably be best to examine other routes. int status = -1; - pid_t ret_pid = waitpid(handle, &status, WNOHANG); + pid_t ret_pid = HANDLE_EINTR(waitpid(handle, &status, WNOHANG)); static const int64 kQuarterSecondInMicroseconds = kMicrosecondsPerSecond/4; // If the process hasn't exited yet, then sleep and try again. @@ -325,7 +318,7 @@ int WaitpidWithTimeout(ProcessHandle handle, int wait_milliseconds, // usleep() will return 0 and set errno to EINTR on receipt of a signal // such as SIGCHLD. usleep(sleep_time_usecs); - ret_pid = waitpid(handle, &status, WNOHANG); + ret_pid = HANDLE_EINTR(waitpid(handle, &status, WNOHANG)); } if (success) @@ -340,7 +333,7 @@ bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) { bool waitpid_success; int status; if (wait_milliseconds == base::kNoTimeout) - waitpid_success = (waitpid(handle, &status, 0) != -1); + waitpid_success = (HANDLE_EINTR(waitpid(handle, &status, 0)) != -1); else status = WaitpidWithTimeout(handle, wait_milliseconds, &waitpid_success); if (status != -1) { @@ -463,16 +456,13 @@ bool GetAppOutput(const CommandLine& cl, std::string* output) { char buffer[256]; std::string buf_output; - ssize_t bytes_read = 0; while (true) { - bytes_read = read(pipe_fd[0], buffer, sizeof(buffer)); - if (bytes_read == 0) - break; - if (bytes_read == -1 && errno != EINTR) + ssize_t bytes_read = + HANDLE_EINTR(read(pipe_fd[0], buffer, sizeof(buffer))); + if (bytes_read <= 0) break; - if (bytes_read > 0) - buf_output.append(buffer, bytes_read); + buf_output.append(buffer, bytes_read); } output->swap(buf_output); close(pipe_fd[0]); diff --git a/base/process_util_unittest.cc b/base/process_util_unittest.cc index 76b705d..30a2c07 100644 --- a/base/process_util_unittest.cc +++ b/base/process_util_unittest.cc @@ -5,6 +5,7 @@ #define _CRT_SECURE_NO_WARNINGS #include "base/command_line.h" +#include "base/eintr_wrapper.h" #include "base/file_path.h" #include "base/multiprocess_test.h" #include "base/path_service.h" @@ -191,7 +192,7 @@ MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { int max_files = GetMaxFilesOpenInProcess(); for (int i = STDERR_FILENO + 1; i < max_files; i++) { if (i != kChildPipe) { - if (close(i) != -1) { + if (HANDLE_EINTR(close(i)) != -1) { LOG(WARNING) << "Leaked FD " << i; num_open_files += 1; } @@ -206,9 +207,10 @@ MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { #endif // defined(OS_LINUX) num_open_files -= expected_num_open_fds; - int written = write(write_pipe, &num_open_files, sizeof(num_open_files)); + int written = HANDLE_EINTR(write(write_pipe, &num_open_files, + sizeof(num_open_files))); DCHECK_EQ(static_cast<size_t>(written), sizeof(num_open_files)); - close(write_pipe); + HANDLE_EINTR(close(write_pipe)); return 0; } @@ -233,12 +235,12 @@ TEST_F(ProcessUtilTest, FDRemapping) { fd_mapping_vec, false); ASSERT_NE(static_cast<ProcessHandle>(NULL), handle); - close(pipe_write_fd); + HANDLE_EINTR(close(pipe_write_fd)); // Read number of open files in client process from pipe; int num_open_files = -1; - ssize_t bytes_read = read(pipe_read_fd, &num_open_files, - sizeof(num_open_files)); + ssize_t bytes_read = + HANDLE_EINTR(read(pipe_read_fd, &num_open_files, sizeof(num_open_files))); ASSERT_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files))); // Make sure 0 fds are leaked to the client. @@ -246,10 +248,10 @@ TEST_F(ProcessUtilTest, FDRemapping) { EXPECT_TRUE(WaitForSingleProcess(handle, 1000)); base::CloseProcessHandle(handle); - close(fds[0]); - close(sockets[0]); - close(sockets[1]); - close(dev_null); + HANDLE_EINTR(close(fds[0])); + HANDLE_EINTR(close(sockets[0])); + HANDLE_EINTR(close(sockets[1])); + HANDLE_EINTR(close(dev_null)); } TEST_F(ProcessUtilTest, GetAppOutput) { |