[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> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 10 | #include <stdio.h> |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 11 | #include <sys/ioctl.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 12 | #include <sys/socket.h> |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 13 | #include <sys/types.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 14 | |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 15 | #if defined(OS_SOLARIS) |
| 16 | #include <sys/filio.h> |
| 17 | #endif |
| 18 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 19 | #include "base/file_util.h" |
| 20 | #include "base/logging.h" |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 21 | #include "base/threading/thread_restrictions.h" |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 22 | |
| 23 | namespace base { |
| 24 | |
| 25 | namespace { |
| 26 | // To avoid users sending negative message lengths to Send/Receive |
| 27 | // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 28 | const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 29 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 30 | // Writes |length| of |buffer| into |handle|. Returns the number of bytes |
| 31 | // written or zero on error. |length| must be greater than 0. |
| 32 | size_t SendHelper(SyncSocket::Handle handle, |
| 33 | const void* buffer, |
| 34 | size_t length) { |
| 35 | DCHECK_GT(length, 0u); |
| 36 | DCHECK_LE(length, kMaxMessageLength); |
| 37 | DCHECK_NE(handle, SyncSocket::kInvalidHandle); |
| 38 | const char* charbuffer = static_cast<const char*>(buffer); |
| 39 | const int len = file_util::WriteFileDescriptor(handle, charbuffer, length); |
| 40 | return len < 0 ? 0 : static_cast<size_t>(len); |
| 41 | } |
| 42 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 43 | } // namespace |
| 44 | |
[email protected] | 5895ee1 | 2011-12-22 19:33:27 | [diff] [blame] | 45 | const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
| 46 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 47 | SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 48 | |
| 49 | SyncSocket::~SyncSocket() { |
| 50 | Close(); |
| 51 | } |
| 52 | |
| 53 | // static |
| 54 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 55 | DCHECK_NE(socket_a, socket_b); |
| 56 | DCHECK_EQ(socket_a->handle_, kInvalidHandle); |
| 57 | DCHECK_EQ(socket_b->handle_, kInvalidHandle); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 58 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 59 | #if defined(OS_MACOSX) |
| 60 | int nosigpipe = 1; |
| 61 | #endif // defined(OS_MACOSX) |
| 62 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 63 | Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
| 64 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 65 | goto cleanup; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 66 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 67 | #if defined(OS_MACOSX) |
| 68 | // On OSX an attempt to read or write to a closed socket may generate a |
| 69 | // SIGPIPE rather than returning -1. setsockopt will shut this off. |
| 70 | if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE, |
| 71 | &nosigpipe, sizeof nosigpipe) || |
| 72 | 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE, |
| 73 | &nosigpipe, sizeof nosigpipe)) { |
| 74 | goto cleanup; |
| 75 | } |
| 76 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 77 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 78 | // Copy the handles out for successful return. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 79 | socket_a->handle_ = handles[0]; |
| 80 | socket_b->handle_ = handles[1]; |
| 81 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 82 | return true; |
| 83 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 84 | cleanup: |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 85 | if (handles[0] != kInvalidHandle) { |
| 86 | if (HANDLE_EINTR(close(handles[0])) < 0) |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 87 | DPLOG(ERROR) << "close"; |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 88 | } |
| 89 | if (handles[1] != kInvalidHandle) { |
| 90 | if (HANDLE_EINTR(close(handles[1])) < 0) |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 91 | DPLOG(ERROR) << "close"; |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 92 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 93 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
| 97 | bool SyncSocket::Close() { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 98 | if (handle_ == kInvalidHandle) |
| 99 | return true; |
| 100 | const int retval = HANDLE_EINTR(close(handle_)); |
| 101 | DPLOG_IF(ERROR, retval < 0) << "close"; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 102 | handle_ = kInvalidHandle; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 103 | return retval == 0; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 107 | ThreadRestrictions::AssertIOAllowed(); |
| 108 | return SendHelper(handle_, buffer, length); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 112 | ThreadRestrictions::AssertIOAllowed(); |
| 113 | DCHECK_GT(length, 0u); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 114 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 115 | DCHECK_NE(handle_, kInvalidHandle); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 116 | char* charbuffer = static_cast<char*>(buffer); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 117 | if (file_util::ReadFromFD(handle_, charbuffer, length)) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 118 | return length; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 119 | return 0; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 122 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 123 | size_t length, |
| 124 | TimeDelta timeout) { |
| 125 | ThreadRestrictions::AssertIOAllowed(); |
| 126 | DCHECK_GT(length, 0u); |
| 127 | DCHECK_LE(length, kMaxMessageLength); |
| 128 | DCHECK_NE(handle_, kInvalidHandle); |
| 129 | DCHECK_LT(handle_, FD_SETSIZE); |
| 130 | |
| 131 | // Only timeouts greater than zero and less than one second are allowed. |
| 132 | DCHECK_GT(timeout.InMicroseconds(), 0); |
| 133 | DCHECK_LT(timeout.InMicroseconds(), |
| 134 | base::TimeDelta::FromSeconds(1).InMicroseconds()); |
| 135 | |
| 136 | // Track the start time so we can reduce the timeout as data is read. |
| 137 | TimeTicks start_time = TimeTicks::Now(); |
| 138 | const TimeTicks finish_time = start_time + timeout; |
| 139 | |
| 140 | fd_set read_fds; |
| 141 | size_t bytes_read_total; |
| 142 | for (bytes_read_total = 0; |
| 143 | bytes_read_total < length && timeout.InMicroseconds() > 0; |
| 144 | timeout = finish_time - base::TimeTicks::Now()) { |
| 145 | FD_ZERO(&read_fds); |
| 146 | FD_SET(handle_, &read_fds); |
| 147 | |
| 148 | // Wait for data to become available. |
| 149 | struct timeval timeout_struct = |
| 150 | { 0, static_cast<suseconds_t>(timeout.InMicroseconds()) }; |
| 151 | const int select_result = |
| 152 | select(handle_ + 1, &read_fds, NULL, NULL, &timeout_struct); |
| 153 | // Handle EINTR manually since we need to update the timeout value. |
| 154 | if (select_result == -1 && errno == EINTR) |
| 155 | continue; |
| 156 | if (select_result <= 0) |
| 157 | return bytes_read_total; |
| 158 | |
| 159 | // select() only tells us that data is ready for reading, not how much. We |
| 160 | // must Peek() for the amount ready for reading to avoid blocking. |
| 161 | DCHECK(FD_ISSET(handle_, &read_fds)); |
| 162 | const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total); |
| 163 | |
| 164 | // There may be zero bytes to read if the socket at the other end closed. |
| 165 | if (!bytes_to_read) |
| 166 | return bytes_read_total; |
| 167 | |
| 168 | const size_t bytes_received = |
| 169 | Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read); |
| 170 | bytes_read_total += bytes_received; |
| 171 | if (bytes_received != bytes_to_read) |
| 172 | return bytes_read_total; |
| 173 | } |
| 174 | |
| 175 | return bytes_read_total; |
| 176 | } |
| 177 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 178 | size_t SyncSocket::Peek() { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 179 | DCHECK_NE(handle_, kInvalidHandle); |
| 180 | int number_chars = 0; |
| 181 | if (ioctl(handle_, FIONREAD, &number_chars) == -1) { |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 182 | // If there is an error in ioctl, signal that the channel would block. |
| 183 | return 0; |
| 184 | } |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 185 | DCHECK_GE(number_chars, 0); |
| 186 | return number_chars; |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 189 | CancelableSyncSocket::CancelableSyncSocket() {} |
| 190 | CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 191 | : SyncSocket(handle) { |
| 192 | } |
| 193 | |
| 194 | bool CancelableSyncSocket::Shutdown() { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 195 | DCHECK_NE(handle_, kInvalidHandle); |
| 196 | return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 199 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 200 | DCHECK_GT(length, 0u); |
| 201 | DCHECK_LE(length, kMaxMessageLength); |
| 202 | DCHECK_NE(handle_, kInvalidHandle); |
| 203 | |
| 204 | const long flags = fcntl(handle_, F_GETFL, NULL); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 205 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 206 | // Set the socket to non-blocking mode for sending if its original mode |
| 207 | // is blocking. |
| 208 | fcntl(handle_, F_SETFL, flags | O_NONBLOCK); |
| 209 | } |
| 210 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame^] | 211 | const size_t len = SendHelper(handle_, buffer, length); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 212 | |
| 213 | if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 214 | // Restore the original flags. |
| 215 | fcntl(handle_, F_SETFL, flags); |
| 216 | } |
| 217 | |
| 218 | return len; |
| 219 | } |
| 220 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 221 | // static |
| 222 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 223 | CancelableSyncSocket* socket_b) { |
| 224 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 225 | } |
| 226 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 227 | } // namespace base |