summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authoragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-01 21:37:31 +0000
committeragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-01 21:37:31 +0000
commit157c61b07b3ce5e308db6230da9d93ff74a16ca3 (patch)
tree7a627dd3c11b684852e2a3ffe84bcfc079bf1316 /chrome/browser
parent105414ec43286c5ba9953eaf66f147a9a1a6afd5 (diff)
downloadchromium_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 'chrome/browser')
-rw-r--r--chrome/browser/debugger/devtools_remote_listen_socket.cc3
-rw-r--r--chrome/browser/debugger/devtools_remote_listen_socket_unittest.cc21
-rw-r--r--chrome/browser/process_singleton_linux.cc4
-rw-r--r--chrome/browser/renderer_host/render_widget_helper.cc5
4 files changed, 16 insertions, 17 deletions
diff --git a/chrome/browser/debugger/devtools_remote_listen_socket.cc b/chrome/browser/debugger/devtools_remote_listen_socket.cc
index 11d38dc..5b7bb21 100644
--- a/chrome/browser/debugger/devtools_remote_listen_socket.cc
+++ b/chrome/browser/debugger/devtools_remote_listen_socket.cc
@@ -21,6 +21,7 @@
#include "third_party/libevent/event.h"
#endif
+#include "base/eintr_wrapper.h"
#include "base/string_util.h"
#include "chrome/browser/debugger/devtools_remote.h"
#include "chrome/browser/debugger/devtools_remote_message.h"
@@ -103,7 +104,7 @@ void DevToolsRemoteListenSocket::Read() {
char buf[kReadBufSize];
int len;
do {
- len = recv(socket_, buf, kReadBufSize, 0);
+ len = HANDLE_EINTR(recv(socket_, buf, kReadBufSize, 0));
if (len == SOCKET_ERROR) {
#if defined(OS_WIN)
int err = WSAGetLastError();
diff --git a/chrome/browser/debugger/devtools_remote_listen_socket_unittest.cc b/chrome/browser/debugger/devtools_remote_listen_socket_unittest.cc
index 8a78e9f..efa0642 100644
--- a/chrome/browser/debugger/devtools_remote_listen_socket_unittest.cc
+++ b/chrome/browser/debugger/devtools_remote_listen_socket_unittest.cc
@@ -6,6 +6,7 @@
#include <fcntl.h>
+#include "base/eintr_wrapper.h"
#include "net/base/net_util.h"
#include "testing/platform_test.h"
@@ -79,8 +80,9 @@ void DevToolsRemoteListenSocketTester::SetUp() {
client.sin_family = AF_INET;
client.sin_addr.s_addr = inet_addr(kLoopback);
client.sin_port = htons(kTestPort);
- int ret = connect(test_socket_,
- reinterpret_cast<sockaddr*>(&client), sizeof(client));
+ int ret = HANDLE_EINTR(connect(test_socket_,
+ reinterpret_cast<sockaddr*>(&client),
+ sizeof(client)));
ASSERT_NE(ret, SOCKET_ERROR);
net::SetNonBlocking(test_socket_);
@@ -93,7 +95,7 @@ void DevToolsRemoteListenSocketTester::TearDown() {
#if defined(OS_WIN)
closesocket(test_socket_);
#elif defined(OS_POSIX)
- close(test_socket_);
+ HANDLE_EINTR(close(test_socket_));
#endif
ASSERT_TRUE(NextAction(kDefaultTimeoutMs));
ASSERT_EQ(ACTION_CLOSE, last_action_.type());
@@ -177,7 +179,7 @@ int DevToolsRemoteListenSocketTester::ClearTestSocket() {
int len_ret = 0;
int time_out = 0;
do {
- int len = recv(test_socket_, buf, kReadBufSize, 0);
+ int len = HANDLE_EINTR(recv(test_socket_, buf, kReadBufSize, 0));
#if defined(OS_WIN)
if (len == SOCKET_ERROR) {
int err = WSAGetLastError();
@@ -248,7 +250,7 @@ void DevToolsRemoteListenSocketTester::HandleMessage(
bool DevToolsRemoteListenSocketTester::Send(SOCKET sock,
const std::string& str) {
int len = static_cast<int>(str.length());
- int send_len = send(sock, str.data(), len, 0);
+ int send_len = HANDLE_EINTR(send(sock, str.data(), len, 0));
if (send_len == SOCKET_ERROR) {
LOG(ERROR) << "send failed: " << errno;
return false;
@@ -317,14 +319,7 @@ void DevToolsRemoteListenSocketTester::TestServerSend() {
PlatformThread::Sleep(10); // sleep for 10ms
const int buf_len = 200;
char buf[buf_len+1];
- int recv_len;
- do {
- recv_len = recv(test_socket_, buf, buf_len, 0);
-#if defined(OS_POSIX)
- } while (recv_len == SOCKET_ERROR && errno == EINTR);
-#else
- } while (false);
-#endif
+ int recv_len = HANDLE_EINTR(recv(test_socket_, buf, buf_len, 0));
ASSERT_NE(recv_len, SOCKET_ERROR);
buf[recv_len] = 0;
ASSERT_STREQ(buf, kChromeDevToolsHandshake);
diff --git a/chrome/browser/process_singleton_linux.cc b/chrome/browser/process_singleton_linux.cc
index 4fba373..fcd64259 100644
--- a/chrome/browser/process_singleton_linux.cc
+++ b/chrome/browser/process_singleton_linux.cc
@@ -9,6 +9,7 @@
#include <sys/socket.h>
#include <sys/un.h>
+#include "base/eintr_wrapper.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "chrome/common/chrome_constants.h"
@@ -25,7 +26,8 @@ bool ProcessSingleton::NotifyOtherProcess() {
sockaddr_un addr;
SetupSocket(&sock, &addr);
- if (connect(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0 &&
+ if (HANDLE_EINTR(connect(sock, reinterpret_cast<sockaddr*>(&addr),
+ sizeof(addr))) < 0 &&
(errno == ENOENT || errno == ECONNREFUSED)) {
return false; // Tell the caller there's nobody to notify.
}
diff --git a/chrome/browser/renderer_host/render_widget_helper.cc b/chrome/browser/renderer_host/render_widget_helper.cc
index 4432352..d738a75 100644
--- a/chrome/browser/renderer_host/render_widget_helper.cc
+++ b/chrome/browser/renderer_host/render_widget_helper.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/renderer_host/render_widget_helper.h"
+#include "base/eintr_wrapper.h"
#include "base/thread.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/renderer_host/render_process_host.h"
@@ -311,7 +312,7 @@ void RenderWidgetHelper::FreeTransportDIB(TransportDIB::Id dib_id) {
i = allocated_dibs_.find(dib_id);
if (i != allocated_dibs_.end()) {
- close(i->second);
+ HANDLE_EINTR(close(i->second));
allocated_dibs_.erase(i);
} else {
DLOG(WARNING) << "Renderer asked us to free unknown transport DIB";
@@ -321,7 +322,7 @@ void RenderWidgetHelper::FreeTransportDIB(TransportDIB::Id dib_id) {
void RenderWidgetHelper::ClearAllocatedDIBs() {
for (std::map<TransportDIB::Id, int>::iterator
i = allocated_dibs_.begin(); i != allocated_dibs_.end(); ++i) {
- close(i->second);
+ HANDLE_EINTR(close(i->second));
}
allocated_dibs_.clear();