James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/sync_socket.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <limits.h> |
| 10 | #include <stdio.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <sys/types.h> |
| 14 | |
| 15 | #if defined(OS_SOLARIS) |
| 16 | #include <sys/filio.h> |
| 17 | #endif |
| 18 | |
| 19 | #include "base/files/file_util.h" |
| 20 | #include "base/logging.h" |
| 21 | #include "base/threading/thread_restrictions.h" |
| 22 | |
| 23 | namespace base { |
| 24 | |
| 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); |
| 29 | |
| 30 | // Writes |length| of |buffer| into |handle|. Returns the number of bytes |
| 31 | // written or zero on error. |length| must be greater than 0. |
| 32 | size_t SendHelper(SyncSocket::Handle handle, |
| 33 | const void* buffer, |
| 34 | size_t length) { |
| 35 | DCHECK_GT(length, 0u); |
| 36 | DCHECK_LE(length, kMaxMessageLength); |
| 37 | DCHECK_NE(handle, SyncSocket::kInvalidHandle); |
| 38 | const char* charbuffer = static_cast<const char*>(buffer); |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 39 | return WriteFileDescriptor(handle, charbuffer, length) |
| 40 | ? static_cast<size_t>(length) |
| 41 | : 0; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool CloseHandle(SyncSocket::Handle handle) { |
| 45 | if (handle != SyncSocket::kInvalidHandle && close(handle) < 0) { |
| 46 | DPLOG(ERROR) << "close"; |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
| 56 | |
| 57 | SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 58 | |
| 59 | SyncSocket::~SyncSocket() { |
| 60 | Close(); |
| 61 | } |
| 62 | |
| 63 | // static |
| 64 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 65 | DCHECK_NE(socket_a, socket_b); |
| 66 | DCHECK_EQ(socket_a->handle_, kInvalidHandle); |
| 67 | DCHECK_EQ(socket_b->handle_, kInvalidHandle); |
| 68 | |
| 69 | #if defined(OS_MACOSX) |
| 70 | int nosigpipe = 1; |
| 71 | #endif // defined(OS_MACOSX) |
| 72 | |
| 73 | Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
| 74 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) { |
| 75 | CloseHandle(handles[0]); |
| 76 | CloseHandle(handles[1]); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | #if defined(OS_MACOSX) |
| 81 | // On OSX an attempt to read or write to a closed socket may generate a |
| 82 | // SIGPIPE rather than returning -1. setsockopt will shut this off. |
| 83 | if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE, |
| 84 | &nosigpipe, sizeof nosigpipe) || |
| 85 | 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE, |
| 86 | &nosigpipe, sizeof nosigpipe)) { |
| 87 | CloseHandle(handles[0]); |
| 88 | CloseHandle(handles[1]); |
| 89 | return false; |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | // Copy the handles out for successful return. |
| 94 | socket_a->handle_ = handles[0]; |
| 95 | socket_b->handle_ = handles[1]; |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // static |
| 101 | SyncSocket::Handle SyncSocket::UnwrapHandle( |
| 102 | const TransitDescriptor& descriptor) { |
| 103 | return descriptor.fd; |
| 104 | } |
| 105 | |
| 106 | bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle, |
| 107 | TransitDescriptor* descriptor) { |
| 108 | descriptor->fd = handle(); |
| 109 | descriptor->auto_close = false; |
| 110 | return descriptor->fd != kInvalidHandle; |
| 111 | } |
| 112 | |
| 113 | bool SyncSocket::Close() { |
| 114 | const bool retval = CloseHandle(handle_); |
| 115 | handle_ = kInvalidHandle; |
| 116 | return retval; |
| 117 | } |
| 118 | |
| 119 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
| 120 | ThreadRestrictions::AssertIOAllowed(); |
| 121 | return SendHelper(handle_, buffer, length); |
| 122 | } |
| 123 | |
| 124 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
| 125 | ThreadRestrictions::AssertIOAllowed(); |
| 126 | DCHECK_GT(length, 0u); |
| 127 | DCHECK_LE(length, kMaxMessageLength); |
| 128 | DCHECK_NE(handle_, kInvalidHandle); |
| 129 | char* charbuffer = static_cast<char*>(buffer); |
| 130 | if (ReadFromFD(handle_, charbuffer, length)) |
| 131 | return length; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 136 | size_t length, |
| 137 | TimeDelta timeout) { |
| 138 | ThreadRestrictions::AssertIOAllowed(); |
| 139 | DCHECK_GT(length, 0u); |
| 140 | DCHECK_LE(length, kMaxMessageLength); |
| 141 | DCHECK_NE(handle_, kInvalidHandle); |
| 142 | |
| 143 | // TODO(dalecurtis): There's an undiagnosed issue on OSX where we're seeing |
| 144 | // large numbers of open files which prevents select() from being used. In |
| 145 | // this case, the best we can do is Peek() to see if we can Receive() now or |
| 146 | // return a timeout error (0) if not. See http://crbug.com/314364. |
| 147 | if (handle_ >= FD_SETSIZE) |
| 148 | return Peek() < length ? 0 : Receive(buffer, length); |
| 149 | |
| 150 | // Only timeouts greater than zero and less than one second are allowed. |
| 151 | DCHECK_GT(timeout.InMicroseconds(), 0); |
| 152 | DCHECK_LT(timeout.InMicroseconds(), |
| 153 | base::TimeDelta::FromSeconds(1).InMicroseconds()); |
| 154 | |
| 155 | // Track the start time so we can reduce the timeout as data is read. |
| 156 | TimeTicks start_time = TimeTicks::Now(); |
| 157 | const TimeTicks finish_time = start_time + timeout; |
| 158 | |
| 159 | fd_set read_fds; |
| 160 | size_t bytes_read_total; |
| 161 | for (bytes_read_total = 0; |
| 162 | bytes_read_total < length && timeout.InMicroseconds() > 0; |
| 163 | timeout = finish_time - base::TimeTicks::Now()) { |
| 164 | FD_ZERO(&read_fds); |
| 165 | FD_SET(handle_, &read_fds); |
| 166 | |
| 167 | // Wait for data to become available. |
| 168 | struct timeval timeout_struct = |
| 169 | { 0, static_cast<suseconds_t>(timeout.InMicroseconds()) }; |
| 170 | const int select_result = |
| 171 | select(handle_ + 1, &read_fds, NULL, NULL, &timeout_struct); |
| 172 | // Handle EINTR manually since we need to update the timeout value. |
| 173 | if (select_result == -1 && errno == EINTR) |
| 174 | continue; |
| 175 | if (select_result <= 0) |
| 176 | return bytes_read_total; |
| 177 | |
| 178 | // select() only tells us that data is ready for reading, not how much. We |
| 179 | // must Peek() for the amount ready for reading to avoid blocking. |
| 180 | DCHECK(FD_ISSET(handle_, &read_fds)); |
| 181 | const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total); |
| 182 | |
| 183 | // There may be zero bytes to read if the socket at the other end closed. |
| 184 | if (!bytes_to_read) |
| 185 | return bytes_read_total; |
| 186 | |
| 187 | const size_t bytes_received = |
| 188 | Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read); |
| 189 | bytes_read_total += bytes_received; |
| 190 | if (bytes_received != bytes_to_read) |
| 191 | return bytes_read_total; |
| 192 | } |
| 193 | |
| 194 | return bytes_read_total; |
| 195 | } |
| 196 | |
| 197 | size_t SyncSocket::Peek() { |
| 198 | DCHECK_NE(handle_, kInvalidHandle); |
| 199 | int number_chars = 0; |
| 200 | if (ioctl(handle_, FIONREAD, &number_chars) == -1) { |
| 201 | // If there is an error in ioctl, signal that the channel would block. |
| 202 | return 0; |
| 203 | } |
| 204 | DCHECK_GE(number_chars, 0); |
| 205 | return number_chars; |
| 206 | } |
| 207 | |
| 208 | CancelableSyncSocket::CancelableSyncSocket() {} |
| 209 | CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 210 | : SyncSocket(handle) { |
| 211 | } |
| 212 | |
| 213 | bool CancelableSyncSocket::Shutdown() { |
| 214 | DCHECK_NE(handle_, kInvalidHandle); |
| 215 | return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0; |
| 216 | } |
| 217 | |
| 218 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
| 219 | DCHECK_GT(length, 0u); |
| 220 | DCHECK_LE(length, kMaxMessageLength); |
| 221 | DCHECK_NE(handle_, kInvalidHandle); |
| 222 | |
| 223 | const long flags = fcntl(handle_, F_GETFL, NULL); |
| 224 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 225 | // Set the socket to non-blocking mode for sending if its original mode |
| 226 | // is blocking. |
| 227 | fcntl(handle_, F_SETFL, flags | O_NONBLOCK); |
| 228 | } |
| 229 | |
| 230 | const size_t len = SendHelper(handle_, buffer, length); |
| 231 | |
| 232 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 233 | // Restore the original flags. |
| 234 | fcntl(handle_, F_SETFL, flags); |
| 235 | } |
| 236 | |
| 237 | return len; |
| 238 | } |
| 239 | |
| 240 | // static |
| 241 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 242 | CancelableSyncSocket* socket_b) { |
| 243 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 244 | } |
| 245 | |
| 246 | } // namespace base |