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