[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 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 21 | #include "base/check_op.h" |
Lei Zhang | c9e8a16 | 2021-05-14 09:33:52 | [diff] [blame] | 22 | #include "base/containers/span.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 23 | #include "base/files/file_util.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 24 | #include "base/threading/scoped_blocking_call.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 25 | #include "build/build_config.h" |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 26 | |
| 27 | namespace base { |
| 28 | |
| 29 | namespace { |
| 30 | // To avoid users sending negative message lengths to Send/Receive |
| 31 | // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 32 | const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 33 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 34 | // Writes |length| of |buffer| into |handle|. Returns the number of bytes |
| 35 | // written or zero on error. |length| must be greater than 0. |
| 36 | size_t SendHelper(SyncSocket::Handle handle, |
| 37 | const void* buffer, |
| 38 | size_t length) { |
| 39 | DCHECK_GT(length, 0u); |
| 40 | DCHECK_LE(length, kMaxMessageLength); |
| 41 | DCHECK_NE(handle, SyncSocket::kInvalidHandle); |
Lei Zhang | c9e8a16 | 2021-05-14 09:33:52 | [diff] [blame] | 42 | return WriteFileDescriptor( |
| 43 | handle, make_span(static_cast<const uint8_t*>(buffer), length)) |
| 44 | ? length |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 45 | : 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 48 | } // namespace |
| 49 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 50 | // static |
| 51 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 52 | DCHECK_NE(socket_a, socket_b); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 53 | DCHECK(!socket_a->IsValid()); |
| 54 | DCHECK(!socket_b->IsValid()); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 55 | |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 56 | #if defined(OS_APPLE) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 57 | int nosigpipe = 1; |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 58 | #endif // defined(OS_APPLE) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 59 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 60 | ScopedHandle handles[2]; |
| 61 | |
| 62 | { |
| 63 | Handle raw_handles[2] = {kInvalidHandle, kInvalidHandle}; |
| 64 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, raw_handles) != 0) { |
| 65 | return false; |
| 66 | } |
| 67 | handles[0].reset(raw_handles[0]); |
| 68 | handles[1].reset(raw_handles[1]); |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 69 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 70 | |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 71 | #if defined(OS_APPLE) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 72 | // On OSX an attempt to read or write to a closed socket may generate a |
| 73 | // SIGPIPE rather than returning -1. setsockopt will shut this off. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 74 | if (0 != setsockopt(handles[0].get(), SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, |
| 75 | sizeof(nosigpipe)) || |
| 76 | 0 != setsockopt(handles[1].get(), SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, |
| 77 | sizeof(nosigpipe))) { |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 78 | return false; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 79 | } |
| 80 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 81 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 82 | // Copy the handles out for successful return. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 83 | socket_a->handle_ = std::move(handles[0]); |
| 84 | socket_b->handle_ = std::move(handles[1]); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 85 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 86 | return true; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 87 | } |
| 88 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 89 | void SyncSocket::Close() { |
| 90 | handle_.reset(); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 94 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 95 | return SendHelper(handle(), buffer, length); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 99 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 100 | DCHECK_GT(length, 0u); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 101 | DCHECK_LE(length, kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 102 | DCHECK(IsValid()); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 103 | char* charbuffer = static_cast<char*>(buffer); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 104 | if (ReadFromFD(handle(), charbuffer, length)) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 105 | return length; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 106 | return 0; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 109 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 110 | size_t length, |
| 111 | TimeDelta timeout) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 112 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 113 | DCHECK_GT(length, 0u); |
| 114 | DCHECK_LE(length, kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 115 | DCHECK(IsValid()); |
[email protected] | 1bc4379e | 2013-11-05 00:14:27 | [diff] [blame] | 116 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 117 | // Only timeouts greater than zero and less than one second are allowed. |
| 118 | DCHECK_GT(timeout.InMicroseconds(), 0); |
| 119 | DCHECK_LT(timeout.InMicroseconds(), |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 120 | TimeDelta::FromSeconds(1).InMicroseconds()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 121 | |
| 122 | // Track the start time so we can reduce the timeout as data is read. |
| 123 | TimeTicks start_time = TimeTicks::Now(); |
| 124 | const TimeTicks finish_time = start_time + timeout; |
| 125 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 126 | struct pollfd pollfd; |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 127 | pollfd.fd = handle(); |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 128 | pollfd.events = POLLIN; |
| 129 | pollfd.revents = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 130 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 131 | size_t bytes_read_total = 0; |
| 132 | while (bytes_read_total < length) { |
| 133 | const TimeDelta this_timeout = finish_time - TimeTicks::Now(); |
| 134 | const int timeout_ms = |
| 135 | static_cast<int>(this_timeout.InMillisecondsRoundedUp()); |
| 136 | if (timeout_ms <= 0) |
| 137 | break; |
| 138 | const int poll_result = poll(&pollfd, 1, timeout_ms); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 139 | // Handle EINTR manually since we need to update the timeout value. |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 140 | if (poll_result == -1 && errno == EINTR) |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 141 | continue; |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 142 | // Return if other type of error or a timeout. |
| 143 | if (poll_result <= 0) |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 144 | return bytes_read_total; |
| 145 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 146 | // 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] | 147 | // must Peek() for the amount ready for reading to avoid blocking. |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 148 | // At hang up (POLLHUP), the write end has been closed and there might still |
| 149 | // be data to be read. |
| 150 | // No special handling is needed for error (POLLERR); we can let any of the |
| 151 | // following operations fail and handle it there. |
| 152 | DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 153 | const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total); |
| 154 | |
| 155 | // There may be zero bytes to read if the socket at the other end closed. |
| 156 | if (!bytes_to_read) |
| 157 | return bytes_read_total; |
| 158 | |
| 159 | const size_t bytes_received = |
| 160 | Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read); |
| 161 | bytes_read_total += bytes_received; |
| 162 | if (bytes_received != bytes_to_read) |
| 163 | return bytes_read_total; |
| 164 | } |
| 165 | |
| 166 | return bytes_read_total; |
| 167 | } |
| 168 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 169 | size_t SyncSocket::Peek() { |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 170 | DCHECK(IsValid()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 171 | int number_chars = 0; |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 172 | if (ioctl(handle_.get(), FIONREAD, &number_chars) == -1) { |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 173 | // If there is an error in ioctl, signal that the channel would block. |
| 174 | return 0; |
| 175 | } |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 176 | DCHECK_GE(number_chars, 0); |
| 177 | return number_chars; |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 178 | } |
| 179 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 180 | bool SyncSocket::IsValid() const { |
| 181 | return handle_.is_valid(); |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 182 | } |
| 183 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 184 | SyncSocket::Handle SyncSocket::handle() const { |
| 185 | return handle_.get(); |
| 186 | } |
| 187 | |
| 188 | SyncSocket::Handle SyncSocket::Release() { |
| 189 | return handle_.release(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | bool CancelableSyncSocket::Shutdown() { |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 193 | DCHECK(IsValid()); |
| 194 | return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 197 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 198 | DCHECK_GT(length, 0u); |
| 199 | DCHECK_LE(length, kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 200 | DCHECK(IsValid()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 201 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 202 | const int flags = fcntl(handle(), F_GETFL); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 203 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 204 | // Set the socket to non-blocking mode for sending if its original mode |
| 205 | // is blocking. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 206 | fcntl(handle(), F_SETFL, flags | O_NONBLOCK); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 207 | } |
| 208 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 209 | const size_t len = SendHelper(handle(), buffer, length); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 210 | |
| 211 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 212 | // Restore the original flags. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 213 | fcntl(handle(), F_SETFL, flags); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return len; |
| 217 | } |
| 218 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 219 | // static |
| 220 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 221 | CancelableSyncSocket* socket_b) { |
| 222 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 223 | } |
| 224 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 225 | } // namespace base |