Protect WebURLLoaderImpl::Context while receiving responses.
[chromium-blink-merge.git] / base / sync_socket_posix.cc
blob257916df3357a21962a4f6b9fe7561a09c286d50
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/sync_socket.h"
7 #include <errno.h>
8 #include <limits.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <sys/ioctl.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
15 #if defined(OS_SOLARIS)
16 #include <sys/filio.h>
17 #endif
19 #include "base/file_util.h"
20 #include "base/logging.h"
23 namespace base {
25 namespace {
26 // To avoid users sending negative message lengths to Send/Receive
27 // we clamp message lengths, which are size_t, to no more than INT_MAX.
28 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
30 } // namespace
32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
34 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
36 SyncSocket::~SyncSocket() {
37 Close();
40 // static
41 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
42 DCHECK(socket_a != socket_b);
43 DCHECK(socket_a->handle_ == kInvalidHandle);
44 DCHECK(socket_b->handle_ == kInvalidHandle);
46 #if defined(OS_MACOSX)
47 int nosigpipe = 1;
48 #endif // defined(OS_MACOSX)
50 Handle handles[2] = { kInvalidHandle, kInvalidHandle };
51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0)
52 goto cleanup;
54 #if defined(OS_MACOSX)
55 // On OSX an attempt to read or write to a closed socket may generate a
56 // SIGPIPE rather than returning -1. setsockopt will shut this off.
57 if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE,
58 &nosigpipe, sizeof nosigpipe) ||
59 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE,
60 &nosigpipe, sizeof nosigpipe)) {
61 goto cleanup;
63 #endif
65 // Copy the handles out for successful return.
66 socket_a->handle_ = handles[0];
67 socket_b->handle_ = handles[1];
69 return true;
71 cleanup:
72 if (handles[0] != kInvalidHandle) {
73 if (HANDLE_EINTR(close(handles[0])) < 0)
74 DPLOG(ERROR) << "close";
76 if (handles[1] != kInvalidHandle) {
77 if (HANDLE_EINTR(close(handles[1])) < 0)
78 DPLOG(ERROR) << "close";
81 return false;
84 bool SyncSocket::Close() {
85 if (handle_ == kInvalidHandle) {
86 return false;
88 int retval = HANDLE_EINTR(close(handle_));
89 if (retval < 0)
90 DPLOG(ERROR) << "close";
91 handle_ = kInvalidHandle;
92 return (retval == 0);
95 size_t SyncSocket::Send(const void* buffer, size_t length) {
96 DCHECK_LE(length, kMaxMessageLength);
97 const char* charbuffer = static_cast<const char*>(buffer);
98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length);
100 return (len == -1) ? 0 : static_cast<size_t>(len);
103 size_t SyncSocket::Receive(void* buffer, size_t length) {
104 DCHECK_LE(length, kMaxMessageLength);
105 char* charbuffer = static_cast<char*>(buffer);
106 if (file_util::ReadFromFD(handle_, charbuffer, length))
107 return length;
108 return 0;
111 size_t SyncSocket::Peek() {
112 int number_chars;
113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) {
114 // If there is an error in ioctl, signal that the channel would block.
115 return 0;
117 return (size_t) number_chars;
120 CancelableSyncSocket::CancelableSyncSocket() {}
121 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
122 : SyncSocket(handle) {
125 bool CancelableSyncSocket::Shutdown() {
126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0;
129 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
130 long flags = 0;
131 flags = fcntl(handle_, F_GETFL, NULL);
132 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
133 // Set the socket to non-blocking mode for sending if its original mode
134 // is blocking.
135 fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
138 size_t len = SyncSocket::Send(buffer, length);
140 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
141 // Restore the original flags.
142 fcntl(handle_, F_SETFL, flags);
145 return len;
148 // static
149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
150 CancelableSyncSocket* socket_b) {
151 return SyncSocket::CreatePair(socket_a, socket_b);
154 } // namespace base