| 6 |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <limits.h> |
| 10 | #include <poll.h> |
| 11 | #include <stddef.h> |
| 12 | #include <stdio.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <sys/types.h> |
| 16 |
| 17 | #if defined(OS_SOLARIS) |
| 18 | #include <sys/filio.h> |
| 19 | #endif |
| 20 |
| 21 | #include "base/files/file_util.h" |
| 22 | #include "base/logging.h" |
| 23 | #include "base/threading/scoped_blocking_call.h" |
| 24 | #include "build/build_config.h" |
| 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 |
| 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); |
| 42 | return WriteFileDescriptor(handle, charbuffer, length) |
| 43 | ? static_cast<size_t>(length) |
| 44 | : 0; |
| 45 | } |
| 46 |
| 47 | } // namespace |
| 48 |
| 49 | // static |
| 50 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 51 | DCHECK_NE(socket_a, socket_b); |
| 52 | DCHECK(!socket_a->IsValid()); |
| 53 | DCHECK(!socket_b->IsValid()); |
| 54 |
| 55 | #if defined(OS_MACOSX) |
| 56 | int nosigpipe = 1; |
| 57 | #endif // defined(OS_MACOSX) |
| 58 |
| 59 | ScopedHandle handles[2]; |
| 60 |
| 61 | { |
| 62 | Handle raw_handles[2] = {kInvalidHandle, kInvalidHandle}; |
| 63 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, raw_handles) != 0) { |
| 64 | return false; |
| 65 | } |
| 66 | handles[0].reset(raw_handles[0]); |
| 67 | handles[1].reset(raw_handles[1]); |
| 68 | } |
| 69 |
| 70 | #if defined(OS_MACOSX) |
| 71 | // On OSX an attempt to read or write to a closed socket may generate a |
| 72 | // SIGPIPE rather than returning -1. setsockopt will shut this off. |
| 73 | if (0 != setsockopt(handles[0].get(), SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, |
| 74 | sizeof(nosigpipe)) || |
| 75 | 0 != setsockopt(handles[1].get(), SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, |
| 76 | sizeof(nosigpipe))) { |
| 77 | return false; |
| 78 | } |
| 79 | #endif |
| 80 |
| 81 | // Copy the handles out for successful return. |
| 82 | socket_a->handle_ = std::move(handles[0]); |
| 83 | socket_b->handle_ = std::move(handles[1]); |
| 84 |
| 85 | return true; |
| 86 | } |
| 87 |
| 88 | void SyncSocket::Close() { |
| 89 | handle_.reset(); |
| 90 | } |
| 91 |
| 92 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
| 93 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 94 | return SendHelper(handle(), buffer, length); |
| 95 | } |
| 96 |
| 97 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
| 98 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 99 | DCHECK_GT(length, 0u); |
| 100 | DCHECK_LE(length, kMaxMessageLength); |
| 101 | DCHECK(IsValid()); |
| 102 | char* charbuffer = static_cast<char*>(buffer); |
| 103 | if (ReadFromFD(handle(), charbuffer, length)) |
| 104 | return length; |
| 105 | return 0; |
| 106 | } |
| 107 |
| 108 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 109 | size_t length, |
| 110 | TimeDelta timeout) { |
| 111 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 112 | DCHECK_GT(length, 0u); |
| 113 | DCHECK_LE(length, kMaxMessageLength); |
| 114 | DCHECK(IsValid()); |
| 115 |
| 116 | // Only timeouts greater than zero and less than one second are allowed. |
| 117 | DCHECK_GT(timeout.InMicroseconds(), 0); |
| 118 | DCHECK_LT(timeout.InMicroseconds(), |
| 119 | TimeDelta::FromSeconds(1).InMicroseconds()); |
| 120 |
| 121 | // Track the start time so we can reduce the timeout as data is read. |
| 122 | TimeTicks start_time = TimeTicks::Now(); |
| 123 | const TimeTicks finish_time = start_time + timeout; |
| 124 |
| 125 | struct pollfd pollfd; |
| 126 | pollfd.fd = handle(); |
| 127 | pollfd.events = POLLIN; |
| 128 | pollfd.revents = 0; |
| 129 |
| 130 | size_t bytes_read_total = 0; |
| 131 | while (bytes_read_total < length) { |
| 132 | const TimeDelta this_timeout = finish_time - TimeTicks::Now(); |
| 133 | const int timeout_ms = |
| 134 | static_cast<int>(this_timeout.InMillisecondsRoundedUp()); |
| 135 | if (timeout_ms <= 0) |
| 136 | break; |
| 137 | const int poll_result = poll(&pollfd, 1, timeout_ms); |
| 138 | // Handle EINTR manually since we need to update the timeout value. |
| 139 | if (poll_result == -1 && errno == EINTR) |
| 140 | continue; |
| 141 | // Return if other type of error or a timeout. |
| 142 | if (poll_result <= 0) |
| 143 | return bytes_read_total; |
| 144 |
| 145 | // poll() only tells us that data is ready for reading, not how much. We |
| 146 | // must Peek() for the amount ready for reading to avoid blocking. |
| 147 | // At hang up (POLLHUP), the write end has been closed and there might still |
| 148 | // be data to be read. |
| 149 | // No special handling is needed for error (POLLERR); we can let any of the |
| 150 | // following operations fail and handle it there. |
| 151 | DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents; |
| 152 | const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total); |
| 153 |
| 154 | // There may be zero bytes to read if the socket at the other end closed. |
| 155 | if (!bytes_to_read) |
| 156 | return bytes_read_total; |
| 157 |
| 158 | const size_t bytes_received = |
| 159 | Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read); |
| 160 | bytes_read_total += bytes_received; |
| 161 | if (bytes_received != bytes_to_read) |
| 162 | return bytes_read_total; |
| 163 | } |
| 164 |
| 165 | return bytes_read_total; |
| 166 | } |
| 167 |
| 168 | size_t SyncSocket::Peek() { |
| 169 | DCHECK(IsValid()); |
| 170 | int number_chars = 0; |
| 171 | if (ioctl(handle_.get(), FIONREAD, &number_chars) == -1) { |
| 172 | // If there is an error in ioctl, signal that the channel would block. |
| 173 | return 0; |
| 174 | } |
| 175 | DCHECK_GE(number_chars, 0); |
| 176 | return number_chars; |
| 177 | } |
| 178 |
| 179 | bool SyncSocket::IsValid() const { |
| 180 | return handle_.is_valid(); |
| 181 | } |
| 182 |
| 183 | SyncSocket::Handle SyncSocket::handle() const { |
| 184 | return handle_.get(); |
| 185 | } |
| 186 |
| 187 | SyncSocket::Handle SyncSocket::Release() { |
| 188 | return handle_.release(); |
| 189 | } |
| 190 |
| 191 | bool CancelableSyncSocket::Shutdown() { |
| 192 | DCHECK(IsValid()); |
| 193 | return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
| 194 | } |
| 195 |
| 196 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
| 197 | DCHECK_GT(length, 0u); |
| 198 | DCHECK_LE(length, kMaxMessageLength); |
| 199 | DCHECK(IsValid()); |
| 200 |
| 201 | const int flags = fcntl(handle(), F_GETFL); |
| 202 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 203 | // Set the socket to non-blocking mode for sending if its original mode |
| 204 | // is blocking. |
| 205 | fcntl(handle(), F_SETFL, flags | O_NONBLOCK); |
| 206 | } |
| 207 |
| 208 | const size_t len = SendHelper(handle(), buffer, length); |
| 209 |
| 210 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 211 | // Restore the original flags. |
| 212 | fcntl(handle(), F_SETFL, flags); |
| 213 | } |
| 214 |
| 215 | return len; |
| 216 | } |
| 217 |
| 218 | // static |
| 219 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 220 | CancelableSyncSocket* socket_b) { |
| 221 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 222 | } |
| 223 |
| 224 | } // namespace base |
| 225 |