[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 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> |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 9 | #include <limits.h> |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 10 | #include <poll.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include <stddef.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 12 | #include <stdio.h> |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 13 | #include <sys/ioctl.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 14 | #include <sys/socket.h> |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 15 | #include <sys/types.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 16 | |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 17 | #if defined(OS_SOLARIS) |
| 18 | #include <sys/filio.h> |
| 19 | #endif |
| 20 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 21 | #include "base/files/file_util.h" |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 22 | #include "base/logging.h" |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 23 | #include "base/threading/thread_restrictions.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 24 | #include "build/build_config.h" |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 25 | |
| 26 | namespace base { |
| 27 | |
| 28 | namespace { |
| 29 | // To avoid users sending negative message lengths to Send/Receive |
| 30 | // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 31 | const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 32 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 33 | // Writes |length| of |buffer| into |handle|. Returns the number of bytes |
| 34 | // written or zero on error. |length| must be greater than 0. |
| 35 | size_t SendHelper(SyncSocket::Handle handle, |
| 36 | const void* buffer, |
| 37 | size_t length) { |
| 38 | DCHECK_GT(length, 0u); |
| 39 | DCHECK_LE(length, kMaxMessageLength); |
| 40 | DCHECK_NE(handle, SyncSocket::kInvalidHandle); |
| 41 | const char* charbuffer = static_cast<const char*>(buffer); |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 42 | return WriteFileDescriptor(handle, charbuffer, length) |
| 43 | ? static_cast<size_t>(length) |
| 44 | : 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 47 | bool CloseHandle(SyncSocket::Handle handle) { |
| 48 | if (handle != SyncSocket::kInvalidHandle && close(handle) < 0) { |
| 49 | DPLOG(ERROR) << "close"; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 56 | } // namespace |
| 57 | |
[email protected] | 5895ee1 | 2011-12-22 19:33:27 | [diff] [blame] | 58 | const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
| 59 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 60 | SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 61 | |
| 62 | SyncSocket::~SyncSocket() { |
| 63 | Close(); |
| 64 | } |
| 65 | |
| 66 | // static |
| 67 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 68 | DCHECK_NE(socket_a, socket_b); |
| 69 | DCHECK_EQ(socket_a->handle_, kInvalidHandle); |
| 70 | DCHECK_EQ(socket_b->handle_, kInvalidHandle); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 71 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 72 | #if defined(OS_MACOSX) |
| 73 | int nosigpipe = 1; |
| 74 | #endif // defined(OS_MACOSX) |
| 75 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 76 | Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 77 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) { |
| 78 | CloseHandle(handles[0]); |
| 79 | CloseHandle(handles[1]); |
| 80 | return false; |
| 81 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 82 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 83 | #if defined(OS_MACOSX) |
| 84 | // On OSX an attempt to read or write to a closed socket may generate a |
| 85 | // SIGPIPE rather than returning -1. setsockopt will shut this off. |
| 86 | if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE, |
| 87 | &nosigpipe, sizeof nosigpipe) || |
| 88 | 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE, |
| 89 | &nosigpipe, sizeof nosigpipe)) { |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 90 | CloseHandle(handles[0]); |
| 91 | CloseHandle(handles[1]); |
| 92 | return false; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 93 | } |
| 94 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 95 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 96 | // Copy the handles out for successful return. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 97 | socket_a->handle_ = handles[0]; |
| 98 | socket_b->handle_ = handles[1]; |
| 99 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 100 | return true; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 101 | } |
| 102 | |
burnik | 3d67005 | 2014-09-08 06:58:22 | [diff] [blame] | 103 | // static |
| 104 | SyncSocket::Handle SyncSocket::UnwrapHandle( |
| 105 | const TransitDescriptor& descriptor) { |
| 106 | return descriptor.fd; |
| 107 | } |
| 108 | |
| 109 | bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle, |
| 110 | TransitDescriptor* descriptor) { |
| 111 | descriptor->fd = handle(); |
| 112 | descriptor->auto_close = false; |
| 113 | return descriptor->fd != kInvalidHandle; |
| 114 | } |
| 115 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 116 | bool SyncSocket::Close() { |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 117 | const bool retval = CloseHandle(handle_); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 118 | handle_ = kInvalidHandle; |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 119 | return retval; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
Francois Doray | 66bdfd8 | 2017-10-20 13:50:37 | [diff] [blame] | 123 | AssertBlockingAllowed(); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 124 | return SendHelper(handle_, buffer, length); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
Francois Doray | 66bdfd8 | 2017-10-20 13:50:37 | [diff] [blame] | 128 | AssertBlockingAllowed(); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 129 | DCHECK_GT(length, 0u); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 130 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 131 | DCHECK_NE(handle_, kInvalidHandle); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 132 | char* charbuffer = static_cast<char*>(buffer); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 133 | if (ReadFromFD(handle_, charbuffer, length)) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 134 | return length; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 135 | return 0; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 138 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 139 | size_t length, |
| 140 | TimeDelta timeout) { |
Francois Doray | 66bdfd8 | 2017-10-20 13:50:37 | [diff] [blame] | 141 | AssertBlockingAllowed(); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 142 | DCHECK_GT(length, 0u); |
| 143 | DCHECK_LE(length, kMaxMessageLength); |
| 144 | DCHECK_NE(handle_, kInvalidHandle); |
[email protected] | 1bc4379e | 2013-11-05 00:14:27 | [diff] [blame] | 145 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 146 | // Only timeouts greater than zero and less than one second are allowed. |
| 147 | DCHECK_GT(timeout.InMicroseconds(), 0); |
| 148 | DCHECK_LT(timeout.InMicroseconds(), |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 149 | TimeDelta::FromSeconds(1).InMicroseconds()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 150 | |
| 151 | // Track the start time so we can reduce the timeout as data is read. |
| 152 | TimeTicks start_time = TimeTicks::Now(); |
| 153 | const TimeTicks finish_time = start_time + timeout; |
| 154 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 155 | struct pollfd pollfd; |
| 156 | pollfd.fd = handle_; |
| 157 | pollfd.events = POLLIN; |
| 158 | pollfd.revents = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 159 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 160 | size_t bytes_read_total = 0; |
| 161 | while (bytes_read_total < length) { |
| 162 | const TimeDelta this_timeout = finish_time - TimeTicks::Now(); |
| 163 | const int timeout_ms = |
| 164 | static_cast<int>(this_timeout.InMillisecondsRoundedUp()); |
| 165 | if (timeout_ms <= 0) |
| 166 | break; |
| 167 | const int poll_result = poll(&pollfd, 1, timeout_ms); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 168 | // Handle EINTR manually since we need to update the timeout value. |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 169 | if (poll_result == -1 && errno == EINTR) |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 170 | continue; |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 171 | // Return if other type of error or a timeout. |
| 172 | if (poll_result <= 0) |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 173 | return bytes_read_total; |
| 174 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 175 | // poll() only tells us that data is ready for reading, not how much. We |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 176 | // must Peek() for the amount ready for reading to avoid blocking. |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 177 | // At hang up (POLLHUP), the write end has been closed and there might still |
| 178 | // be data to be read. |
| 179 | // No special handling is needed for error (POLLERR); we can let any of the |
| 180 | // following operations fail and handle it there. |
| 181 | DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 182 | const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total); |
| 183 | |
| 184 | // There may be zero bytes to read if the socket at the other end closed. |
| 185 | if (!bytes_to_read) |
| 186 | return bytes_read_total; |
| 187 | |
| 188 | const size_t bytes_received = |
| 189 | Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read); |
| 190 | bytes_read_total += bytes_received; |
| 191 | if (bytes_received != bytes_to_read) |
| 192 | return bytes_read_total; |
| 193 | } |
| 194 | |
| 195 | return bytes_read_total; |
| 196 | } |
| 197 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 198 | size_t SyncSocket::Peek() { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 199 | DCHECK_NE(handle_, kInvalidHandle); |
| 200 | int number_chars = 0; |
| 201 | if (ioctl(handle_, FIONREAD, &number_chars) == -1) { |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 202 | // If there is an error in ioctl, signal that the channel would block. |
| 203 | return 0; |
| 204 | } |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 205 | DCHECK_GE(number_chars, 0); |
| 206 | return number_chars; |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 207 | } |
| 208 | |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 209 | SyncSocket::Handle SyncSocket::Release() { |
| 210 | Handle r = handle_; |
| 211 | handle_ = kInvalidHandle; |
| 212 | return r; |
| 213 | } |
| 214 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 215 | CancelableSyncSocket::CancelableSyncSocket() = default; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 216 | CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 217 | : SyncSocket(handle) { |
| 218 | } |
| 219 | |
| 220 | bool CancelableSyncSocket::Shutdown() { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 221 | DCHECK_NE(handle_, kInvalidHandle); |
| 222 | return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 225 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 226 | DCHECK_GT(length, 0u); |
| 227 | DCHECK_LE(length, kMaxMessageLength); |
| 228 | DCHECK_NE(handle_, kInvalidHandle); |
| 229 | |
tfarina | 5967e5a | 2016-03-09 14:40:13 | [diff] [blame] | 230 | const int flags = fcntl(handle_, F_GETFL); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 231 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 232 | // Set the socket to non-blocking mode for sending if its original mode |
| 233 | // is blocking. |
| 234 | fcntl(handle_, F_SETFL, flags | O_NONBLOCK); |
| 235 | } |
| 236 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 237 | const size_t len = SendHelper(handle_, buffer, length); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 238 | |
| 239 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 240 | // Restore the original flags. |
| 241 | fcntl(handle_, F_SETFL, flags); |
| 242 | } |
| 243 | |
| 244 | return len; |
| 245 | } |
| 246 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 247 | // static |
| 248 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 249 | CancelableSyncSocket* socket_b) { |
| 250 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 251 | } |
| 252 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 253 | } // namespace base |