Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/sync_socket.h" | 5 #include "base/sync_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | |
| 8 #include <limits.h> | 9 #include <limits.h> |
| 9 #include <fcntl.h> | |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 #include <sys/ioctl.h> | 11 #include <sys/ioctl.h> |
| 12 #include <sys/socket.h> | 12 #include <sys/socket.h> |
| 13 #include <sys/types.h> | 13 #include <sys/types.h> |
| 14 | 14 |
| 15 #if defined(OS_SOLARIS) | 15 #if defined(OS_SOLARIS) |
| 16 #include <sys/filio.h> | 16 #include <sys/filio.h> |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
| 20 #include "base/logging.h" | 20 #include "base/logging.h" |
| 21 | 21 #include "base/threading/thread_restrictions.h" |
| 22 | 22 |
| 23 namespace base { | 23 namespace base { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 // To avoid users sending negative message lengths to Send/Receive | 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. | 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); | 28 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; | 32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
| 33 | 33 |
| 34 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} | 34 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 35 | 35 |
| 36 SyncSocket::~SyncSocket() { | 36 SyncSocket::~SyncSocket() { |
| 37 Close(); | 37 Close(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 // static | 40 // static |
| 41 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { | 41 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 42 DCHECK(socket_a != socket_b); | 42 DCHECK_NE(socket_a, socket_b); |
| 43 DCHECK(socket_a->handle_ == kInvalidHandle); | 43 DCHECK_EQ(socket_a->handle_, kInvalidHandle); |
| 44 DCHECK(socket_b->handle_ == kInvalidHandle); | 44 DCHECK_EQ(socket_b->handle_, kInvalidHandle); |
| 45 | 45 |
| 46 #if defined(OS_MACOSX) | 46 #if defined(OS_MACOSX) |
| 47 int nosigpipe = 1; | 47 int nosigpipe = 1; |
| 48 #endif // defined(OS_MACOSX) | 48 #endif // defined(OS_MACOSX) |
| 49 | 49 |
| 50 Handle handles[2] = { kInvalidHandle, kInvalidHandle }; | 50 Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
| 51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) | 51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) |
| 52 goto cleanup; | 52 goto cleanup; |
| 53 | 53 |
| 54 #if defined(OS_MACOSX) | 54 #if defined(OS_MACOSX) |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 75 } | 75 } |
| 76 if (handles[1] != kInvalidHandle) { | 76 if (handles[1] != kInvalidHandle) { |
| 77 if (HANDLE_EINTR(close(handles[1])) < 0) | 77 if (HANDLE_EINTR(close(handles[1])) < 0) |
| 78 DPLOG(ERROR) << "close"; | 78 DPLOG(ERROR) << "close"; |
| 79 } | 79 } |
| 80 | 80 |
| 81 return false; | 81 return false; |
| 82 } | 82 } |
| 83 | 83 |
| 84 bool SyncSocket::Close() { | 84 bool SyncSocket::Close() { |
| 85 if (handle_ == kInvalidHandle) { | 85 if (handle_ == kInvalidHandle) |
| 86 return false; | 86 return true; |
| 87 } | 87 const int retval = HANDLE_EINTR(close(handle_)); |
| 88 int retval = HANDLE_EINTR(close(handle_)); | 88 DPLOG_IF(ERROR, retval < 0) << "close"; |
| 89 if (retval < 0) | |
| 90 DPLOG(ERROR) << "close"; | |
| 91 handle_ = kInvalidHandle; | 89 handle_ = kInvalidHandle; |
| 92 return (retval == 0); | 90 return retval == 0; |
| 93 } | 91 } |
| 94 | 92 |
| 95 size_t SyncSocket::Send(const void* buffer, size_t length) { | 93 size_t SyncSocket::Send(const void* buffer, size_t length) { |
| 94 ThreadRestrictions::AssertIOAllowed(); | |
| 95 DCHECK_GT(length, 0u); | |
| 96 DCHECK_LE(length, kMaxMessageLength); | 96 DCHECK_LE(length, kMaxMessageLength); |
| 97 DCHECK_NE(handle_, kInvalidHandle); | |
| 97 const char* charbuffer = static_cast<const char*>(buffer); | 98 const char* charbuffer = static_cast<const char*>(buffer); |
| 98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); | 99 const int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); |
| 99 | 100 return len < 0 ? 0 : static_cast<size_t>(len); |
| 100 return (len == -1) ? 0 : static_cast<size_t>(len); | |
| 101 } | 101 } |
| 102 | 102 |
| 103 size_t SyncSocket::Receive(void* buffer, size_t length) { | 103 size_t SyncSocket::Receive(void* buffer, size_t length) { |
| 104 ThreadRestrictions::AssertIOAllowed(); | |
| 105 DCHECK_GT(length, 0u); | |
| 104 DCHECK_LE(length, kMaxMessageLength); | 106 DCHECK_LE(length, kMaxMessageLength); |
| 107 DCHECK_NE(handle_, kInvalidHandle); | |
| 105 char* charbuffer = static_cast<char*>(buffer); | 108 char* charbuffer = static_cast<char*>(buffer); |
| 106 if (file_util::ReadFromFD(handle_, charbuffer, length)) | 109 if (file_util::ReadFromFD(handle_, charbuffer, length)) |
| 107 return length; | 110 return length; |
| 108 return 0; | 111 return 0; |
| 109 } | 112 } |
| 110 | 113 |
| 114 size_t SyncSocket::ReceiveWithTimeout(void* buffer, | |
| 115 size_t length, | |
| 116 TimeDelta timeout) { | |
| 117 ThreadRestrictions::AssertIOAllowed(); | |
| 118 DCHECK_GT(length, 0u); | |
| 119 DCHECK_LE(length, kMaxMessageLength); | |
| 120 DCHECK_NE(handle_, kInvalidHandle); | |
| 121 DCHECK_LT(handle_, FD_SETSIZE); | |
| 122 | |
| 123 // Only timeouts greater than zero and less than one second are allowed. | |
| 124 DCHECK_GT(timeout.InMicroseconds(), 0); | |
| 125 DCHECK_LT(timeout.InMicroseconds(), | |
| 126 base::TimeDelta::FromSeconds(1).InMicroseconds()); | |
| 127 | |
| 128 // Track the start time so we can reduce the timeout as data is read. | |
| 129 TimeTicks start_time = TimeTicks::Now(); | |
| 130 const TimeTicks finish_time = start_time + timeout; | |
| 131 | |
| 132 fd_set read_fds; | |
| 133 size_t bytes_read_total; | |
| 134 for (bytes_read_total = 0; bytes_read_total < length && timeout > TimeDelta(); | |
|
jar (doing other things)
2013/10/14 15:31:07
nit: This seems close to safe... but I'm wary that
DaleCurtis
2013/10/14 19:25:13
Should be fine, but I'll change it to .InMicroseco
| |
| 135 timeout = finish_time - base::TimeTicks::Now()) { | |
| 136 FD_ZERO(&read_fds); | |
| 137 FD_SET(handle_, &read_fds); | |
| 138 | |
| 139 // Wait for data to become available. | |
| 140 struct timeval timeout_struct = { 0, timeout.InMicroseconds() }; | |
|
jar (doing other things)
2013/10/14 15:31:07
nit: (really just a comment) I'm wary that with Ti
DaleCurtis
2013/10/14 19:25:13
Done.
| |
| 141 const int select_result = | |
| 142 select(handle_ + 1, &read_fds, NULL, NULL, &timeout_struct); | |
| 143 // Handle EINTR manually since we need to update the timeout value. | |
| 144 if (select_result == -1 && errno == EINTR) | |
| 145 continue; | |
| 146 if (select_result <= 0) | |
| 147 return bytes_read_total; | |
| 148 | |
| 149 // select() only tells us that data is ready for reading, not how much. We | |
| 150 // must Peek() for the amount ready for reading to avoid blocking. | |
| 151 DCHECK(FD_ISSET(handle_, &read_fds)); | |
| 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) | |
|
jar (doing other things)
2013/10/14 15:31:07
When does this happen?
DaleCurtis
2013/10/14 19:25:13
If the read() call underlying file_util::ReadFromF
| |
| 162 return bytes_read_total; | |
| 163 } | |
| 164 | |
| 165 return bytes_read_total; | |
| 166 } | |
| 167 | |
| 111 size_t SyncSocket::Peek() { | 168 size_t SyncSocket::Peek() { |
| 112 int number_chars; | 169 DCHECK_NE(handle_, kInvalidHandle); |
| 113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { | 170 int number_chars = 0; |
| 171 if (ioctl(handle_, FIONREAD, &number_chars) == -1) { | |
| 114 // If there is an error in ioctl, signal that the channel would block. | 172 // If there is an error in ioctl, signal that the channel would block. |
| 115 return 0; | 173 return 0; |
| 116 } | 174 } |
| 117 return (size_t) number_chars; | 175 DCHECK_GE(number_chars, 0); |
| 176 return number_chars; | |
| 118 } | 177 } |
| 119 | 178 |
| 120 CancelableSyncSocket::CancelableSyncSocket() {} | 179 CancelableSyncSocket::CancelableSyncSocket() {} |
| 121 CancelableSyncSocket::CancelableSyncSocket(Handle handle) | 180 CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 122 : SyncSocket(handle) { | 181 : SyncSocket(handle) { |
| 123 } | 182 } |
| 124 | 183 |
| 125 bool CancelableSyncSocket::Shutdown() { | 184 bool CancelableSyncSocket::Shutdown() { |
| 185 DCHECK_NE(handle_, kInvalidHandle); | |
| 126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; | 186 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
| 127 } | 187 } |
| 128 | 188 |
| 129 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { | 189 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
| 190 DCHECK_GT(length, 0u); | |
| 191 DCHECK_LE(length, kMaxMessageLength); | |
| 192 DCHECK_NE(handle_, kInvalidHandle); | |
| 193 | |
| 130 long flags = 0; | 194 long flags = 0; |
| 131 flags = fcntl(handle_, F_GETFL, NULL); | 195 flags = fcntl(handle_, F_GETFL, NULL); |
| 132 if (flags != -1 && (flags & O_NONBLOCK) == 0) { | 196 if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 133 // Set the socket to non-blocking mode for sending if its original mode | 197 // Set the socket to non-blocking mode for sending if its original mode |
| 134 // is blocking. | 198 // is blocking. |
| 135 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); | 199 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); |
| 136 } | 200 } |
| 137 | 201 |
| 138 size_t len = SyncSocket::Send(buffer, length); | 202 size_t len = SyncSocket::Send(buffer, length); |
| 139 | 203 |
| 140 if (flags != -1 && (flags & O_NONBLOCK) == 0) { | 204 if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
| 141 // Restore the original flags. | 205 // Restore the original flags. |
| 142 fcntl(handle_, F_SETFL, flags); | 206 fcntl(handle_, F_SETFL, flags); |
| 143 } | 207 } |
| 144 | 208 |
| 145 return len; | 209 return len; |
| 146 } | 210 } |
| 147 | 211 |
| 148 // static | 212 // static |
| 149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 213 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 150 CancelableSyncSocket* socket_b) { | 214 CancelableSyncSocket* socket_b) { |
| 151 return SyncSocket::CreatePair(socket_a, socket_b); | 215 return SyncSocket::CreatePair(socket_a, socket_b); |
| 152 } | 216 } |
| 153 | 217 |
| 154 } // namespace base | 218 } // namespace base |
| OLD | NEW |