Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[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 | |
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" |
[email protected] | 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" |
[email protected] | 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 | |
[email protected] | 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 | |
[email protected] | 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); |
[email protected] | 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; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 43 | } // namespace |
| 44 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 45 | // static |
| 46 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
[email protected] | 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()); |
[email protected] | 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) |
[email protected] | 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) |
[email protected] | 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]); |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 64 | } |
[email protected] | 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) |
[email protected] | 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))) { |
[email protected] | 1da08e7 | 2013-11-01 19:58:12 | [diff] [blame] | 73 | return false; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 74 | } |
| 75 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 76 | |
[email protected] | 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]); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 80 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 81 | return true; |
[email protected] | 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(); |
[email protected] | 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); |
[email protected] | 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::Receive(span<uint8_t> buffer) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 94 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 95 | CHECK_LE(buffer.size(), kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 96 | DCHECK(IsValid()); |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 97 | if (ReadFromFD(handle(), as_writable_chars(buffer))) { |
| 98 | return buffer.size(); |
Austin Sullivan | 49f9744 | 2024-01-08 20:26:27 | [diff] [blame] | 99 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 100 | return 0; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 101 | } |
| 102 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 103 | size_t SyncSocket::ReceiveWithTimeout(span<uint8_t> buffer, TimeDelta timeout) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 104 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 105 | CHECK_LE(buffer.size(), kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 106 | DCHECK(IsValid()); |
[email protected] | 1bc4379e | 2013-11-05 00:14:27 | [diff] [blame] | 107 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 108 | // Only timeouts greater than zero and less than one second are allowed. |
| 109 | DCHECK_GT(timeout.InMicroseconds(), 0); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 110 | DCHECK_LT(timeout.InMicroseconds(), Seconds(1).InMicroseconds()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 111 | |
| 112 | // Track the start time so we can reduce the timeout as data is read. |
| 113 | TimeTicks start_time = TimeTicks::Now(); |
| 114 | const TimeTicks finish_time = start_time + timeout; |
| 115 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 116 | struct pollfd pollfd; |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 117 | pollfd.fd = handle(); |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 118 | pollfd.events = POLLIN; |
| 119 | pollfd.revents = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 120 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 121 | size_t bytes_read_total = 0; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 122 | while (!buffer.empty()) { |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 123 | const TimeDelta this_timeout = finish_time - TimeTicks::Now(); |
| 124 | const int timeout_ms = |
| 125 | static_cast<int>(this_timeout.InMillisecondsRoundedUp()); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 126 | if (timeout_ms <= 0) { |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 127 | break; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 128 | } |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 129 | const int poll_result = poll(&pollfd, 1, timeout_ms); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 130 | // Handle EINTR manually since we need to update the timeout value. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 131 | if (poll_result == -1 && errno == EINTR) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 132 | continue; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 133 | } |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 134 | // Return if other type of error or a timeout. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 135 | if (poll_result <= 0) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 136 | return bytes_read_total; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 137 | } |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 138 | |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 139 | // 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] | 140 | // must Peek() for the amount ready for reading to avoid blocking. |
grunell | b464b3e | 2017-05-23 11:15:26 | [diff] [blame] | 141 | // At hang up (POLLHUP), the write end has been closed and there might still |
| 142 | // be data to be read. |
| 143 | // No special handling is needed for error (POLLERR); we can let any of the |
| 144 | // following operations fail and handle it there. |
| 145 | DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 146 | const size_t bytes_to_read = std::min(Peek(), buffer.size()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 147 | |
| 148 | // There may be zero bytes to read if the socket at the other end closed. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 149 | if (!bytes_to_read) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 150 | return bytes_read_total; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 151 | } |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 152 | |
Tom Sepez | e9d9e04 | 2025-02-26 23:04:02 | [diff] [blame] | 153 | const size_t bytes_received = Receive(buffer.first(bytes_to_read)); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 154 | bytes_read_total += bytes_received; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 155 | buffer = buffer.subspan(bytes_received); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 156 | if (bytes_received != bytes_to_read) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 157 | return bytes_read_total; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 158 | } |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return bytes_read_total; |
| 162 | } |
| 163 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 164 | size_t SyncSocket::Peek() { |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 165 | DCHECK(IsValid()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 166 | int number_chars = 0; |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 167 | if (ioctl(handle_.get(), FIONREAD, &number_chars) == -1) { |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 168 | // If there is an error in ioctl, signal that the channel would block. |
| 169 | return 0; |
| 170 | } |
Peter Kasting | a0b914dc | 2022-07-14 18:43:19 | [diff] [blame] | 171 | return checked_cast<size_t>(number_chars); |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 172 | } |
| 173 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 174 | bool SyncSocket::IsValid() const { |
| 175 | return handle_.is_valid(); |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 176 | } |
| 177 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 178 | SyncSocket::Handle SyncSocket::handle() const { |
| 179 | return handle_.get(); |
| 180 | } |
| 181 | |
| 182 | SyncSocket::Handle SyncSocket::Release() { |
| 183 | return handle_.release(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool CancelableSyncSocket::Shutdown() { |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 187 | DCHECK(IsValid()); |
| 188 | return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 189 | } |
| 190 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 191 | size_t CancelableSyncSocket::Send(span<const uint8_t> data) { |
| 192 | CHECK_LE(data.size(), kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 193 | DCHECK(IsValid()); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 194 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 195 | const int flags = fcntl(handle(), F_GETFL); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 196 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 197 | // Set the socket to non-blocking mode for sending if its original mode |
| 198 | // is blocking. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 199 | fcntl(handle(), F_SETFL, flags | O_NONBLOCK); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 200 | } |
| 201 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 202 | const size_t len = SendHelper(handle(), data); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 203 | |
| 204 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 205 | // Restore the original flags. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 206 | fcntl(handle(), F_SETFL, flags); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return len; |
| 210 | } |
| 211 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 212 | // static |
| 213 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 214 | CancelableSyncSocket* socket_b) { |
| 215 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 216 | } |
| 217 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 218 | } // namespace base |