Commit 28150275 authored by davidben's avatar davidben Committed by Commit bot

Try to tolerate running on contexts with SIGPIPE enabled.

Although Chromium wisely disables this ancient UNIX bug, the net stack is part
of Cronet which may be used in other consumers. Rather than modify the global
signal dispositions (bad manners for a library) or propagate this requirement
to the callers (also bad manners, if sound), make an attempt to suppress the
signal on a per-socket basis.

Unfortunately, iOS/OSX and Linux kindly have different APIs for this, so we
need to touch two places.

BUG=467132

Review-Url: https://codereview.chromium.org/2034433002
Cr-Commit-Position: refs/heads/master@{#398121}
parent 6bf0bb56
......@@ -14,6 +14,10 @@
#include "net/base/winsock_init.h"
#endif
#if defined(OS_MACOSX)
#include <unistd.h>
#endif
namespace net {
SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) {
......@@ -31,7 +35,20 @@ SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) {
}
return result;
#else // OS_WIN
return ::socket(family, type, protocol);
SocketDescriptor result = ::socket(family, type, protocol);
#if defined(OS_MACOSX)
// Disable SIGPIPE on this socket. Although Chromium globally disables
// SIGPIPE, the net stack may be used in other consumers which do not do
// this. SO_NOSIGPIPE is a Mac-only API. On Linux, it is a flag on send.
if (result != kInvalidSocket) {
int value = 1;
if (setsockopt(result, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value))) {
close(result);
return kInvalidSocket;
}
}
#endif
return result;
#endif // OS_WIN
}
......
......@@ -14,6 +14,7 @@
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
......@@ -441,7 +442,15 @@ void SocketPosix::ReadCompleted() {
}
int SocketPosix::DoWrite(IOBuffer* buf, int buf_len) {
#if defined(OS_LINUX) || defined(OS_ANDROID)
// Disable SIGPIPE for this write. Although Chromium globally disables
// SIGPIPE, the net stack may be used in other consumers which do not do
// this. MSG_NOSIGNAL is a Linux-only API. On OS X, this is a setsockopt on
// socket creation.
int rv = HANDLE_EINTR(send(socket_fd_, buf->data(), buf_len, MSG_NOSIGNAL));
#else
int rv = HANDLE_EINTR(write(socket_fd_, buf->data(), buf_len));
#endif
return rv >= 0 ? rv : MapSystemError(errno);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment