Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 17 | #include <arpa/inet.h> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include <netdb.h> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <netinet/tcp.h> |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 30 | #include "base/stringprintf.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 31 | #include "jdwp/jdwp_priv.h" |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
| 35 | namespace JDWP { |
| 36 | |
Roland Levillain | 3cbad24 | 2016-01-25 19:17:14 +0000 | [diff] [blame] | 37 | static constexpr uint16_t kBasePort = 8000; |
| 38 | static constexpr uint16_t kMaxPort = 8040; |
| 39 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 40 | /* |
| 41 | * JDWP network state. |
| 42 | * |
| 43 | * We only talk to one debugger at a time. |
| 44 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 45 | struct JdwpSocketState : public JdwpNetStateBase { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 46 | uint16_t listenPort; |
| 47 | int listenSock; /* listen for connection from debugger */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 48 | |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame] | 49 | explicit JdwpSocketState(JdwpState* state) |
| 50 | : JdwpNetStateBase(state), |
| 51 | listenPort(0U), |
| 52 | listenSock(-1), |
| 53 | remote_port_(0U) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 54 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 55 | |
| 56 | virtual bool Accept(); |
| 57 | virtual bool Establish(const JdwpOptions*); |
| 58 | virtual void Shutdown(); |
| 59 | virtual bool ProcessIncoming(); |
| 60 | |
| 61 | private: |
| 62 | in_addr remote_addr_; |
| 63 | uint16_t remote_port_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 66 | static JdwpSocketState* SocketStartup(JdwpState* state, uint16_t port, bool probe); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Set up some stuff for transport=dt_socket. |
| 70 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 71 | bool InitSocketTransport(JdwpState* state, const JdwpOptions* options) { |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 72 | uint16_t port = options->port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 74 | if (options->server) { |
| 75 | if (options->port != 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 76 | /* try only the specified port */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 77 | state->netState = SocketStartup(state, port, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 78 | } else { |
| 79 | /* scan through a range of ports, binding to the first available */ |
| 80 | for (port = kBasePort; port <= kMaxPort; port++) { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 81 | state->netState = SocketStartup(state, port, true); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 82 | if (state->netState != nullptr) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 87 | if (state->netState == nullptr) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 88 | LOG(ERROR) << "JDWP net startup failed (req port=" << options->port << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | } else { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 92 | state->netState = SocketStartup(state, 0, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 95 | if (options->suspend) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 96 | LOG(INFO) << "JDWP will wait for debugger on port " << port; |
| 97 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 98 | LOG(INFO) << "JDWP will " << (options->server ? "listen" : "connect") << " on port " << port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 105 | * Initialize JDWP stuff. |
| 106 | * |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 107 | * Allocates a new state structure. If "port" is non-zero, this also |
| 108 | * tries to bind to a listen port. If "port" is zero, we assume |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 109 | * we're preparing for an outbound connection, and return without binding |
| 110 | * to anything. |
| 111 | * |
| 112 | * This may be called several times if we're probing for a port. |
| 113 | * |
| 114 | * Returns 0 on success. |
| 115 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 116 | static JdwpSocketState* SocketStartup(JdwpState* state, uint16_t port, bool probe) { |
| 117 | JdwpSocketState* netState = new JdwpSocketState(state); |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 118 | if (port == 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 119 | return netState; |
| 120 | } |
| 121 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 122 | netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 123 | if (netState->listenSock < 0) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame^] | 124 | PLOG(probe ? ::android::base::ERROR : ::android::base::FATAL) << "Socket create failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 125 | goto fail; |
| 126 | } |
| 127 | |
| 128 | /* allow immediate re-use */ |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 129 | { |
| 130 | int one = 1; |
| 131 | if (setsockopt(netState->listenSock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame^] | 132 | PLOG(probe ? ::android::base::ERROR : ::android::base::FATAL) |
| 133 | << "setsockopt(SO_REUSEADDR) failed"; |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 134 | goto fail; |
| 135 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 139 | sockaddr_in addrInet; |
| 140 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 141 | } addr; |
| 142 | addr.addrInet.sin_family = AF_INET; |
| 143 | addr.addrInet.sin_port = htons(port); |
| 144 | inet_aton("127.0.0.1", &addr.addrInet.sin_addr); |
| 145 | |
| 146 | if (bind(netState->listenSock, &addr.addrPlain, sizeof(addr)) != 0) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame^] | 147 | PLOG(probe ? ::android::base::ERROR : ::android::base::FATAL) |
| 148 | << "Attempt to bind to port " << port << " failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 149 | goto fail; |
| 150 | } |
| 151 | |
| 152 | netState->listenPort = port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 153 | |
| 154 | if (listen(netState->listenSock, 5) != 0) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame^] | 155 | PLOG(probe ? ::android::base::ERROR : ::android::base::FATAL) << "Listen failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 156 | goto fail; |
| 157 | } |
| 158 | |
| 159 | return netState; |
| 160 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 161 | fail: |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 162 | netState->Shutdown(); |
| 163 | delete netState; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 164 | return nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Shut down JDWP listener. Don't free state. |
| 169 | * |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 170 | * This may be called from a non-JDWP thread as part of shutting the |
| 171 | * JDWP thread down. |
| 172 | * |
| 173 | * (This is currently called several times during startup as we probe |
| 174 | * for an open port.) |
| 175 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 176 | void JdwpSocketState::Shutdown() { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 177 | int local_listenSock = this->listenSock; |
| 178 | int local_clientSock = this->clientSock; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 179 | |
| 180 | /* clear these out so it doesn't wake up and try to reuse them */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 181 | this->listenSock = this->clientSock = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 182 | |
| 183 | /* "shutdown" dislodges blocking read() and accept() calls */ |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 184 | if (local_listenSock != -1) { |
| 185 | shutdown(local_listenSock, SHUT_RDWR); |
| 186 | close(local_listenSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 187 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 188 | if (local_clientSock != -1) { |
| 189 | shutdown(local_clientSock, SHUT_RDWR); |
| 190 | close(local_clientSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 193 | WakePipe(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 197 | * Disable the TCP Nagle algorithm, which delays transmission of outbound |
| 198 | * packets until the previous transmissions have been acked. JDWP does a |
| 199 | * lot of back-and-forth with small packets, so this may help. |
| 200 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 201 | static int SetNoDelay(int fd) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 202 | int on = 1; |
| 203 | int cc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 204 | CHECK_EQ(cc, 0); |
| 205 | return cc; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Accept a connection. This will block waiting for somebody to show up. |
| 210 | * If that's not desirable, use checkConnection() to make sure something |
| 211 | * is pending. |
| 212 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 213 | bool JdwpSocketState::Accept() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 214 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 215 | sockaddr_in addrInet; |
| 216 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 217 | } addr; |
| 218 | socklen_t addrlen; |
| 219 | int sock; |
| 220 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 221 | if (listenSock < 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 222 | return false; /* you're not listening! */ |
| 223 | } |
| 224 | |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 225 | CHECK_EQ(clientSock, -1); /* must not already be talking */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 226 | |
| 227 | addrlen = sizeof(addr); |
| 228 | do { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 229 | sock = accept(listenSock, &addr.addrPlain, &addrlen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 230 | if (sock < 0 && errno != EINTR) { |
| 231 | // When we call shutdown() on the socket, accept() returns with |
| 232 | // EINVAL. Don't gripe about it. |
| 233 | if (errno == EINVAL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 234 | if (VLOG_IS_ON(jdwp)) { |
| 235 | PLOG(ERROR) << "accept failed"; |
| 236 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 237 | } else { |
| 238 | PLOG(ERROR) << "accept failed"; |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | } while (sock < 0); |
| 243 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 244 | remote_addr_ = addr.addrInet.sin_addr; |
| 245 | remote_port_ = ntohs(addr.addrInet.sin_port); |
| 246 | VLOG(jdwp) << "+++ accepted connection from " << inet_ntoa(remote_addr_) << ":" << remote_port_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 247 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 248 | clientSock = sock; |
| 249 | SetAwaitingHandshake(true); |
| 250 | input_count_ = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 251 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 252 | VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket"; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 253 | SetNoDelay(clientSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 254 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 255 | if (!MakePipe()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Create a connection to a waiting debugger. |
| 264 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 265 | bool JdwpSocketState::Establish(const JdwpOptions* options) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 266 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 267 | sockaddr_in addrInet; |
| 268 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 269 | } addr; |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 270 | hostent* pEntry; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 271 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 272 | CHECK(!options->server); |
| 273 | CHECK(!options->host.empty()); |
| 274 | CHECK_NE(options->port, 0); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * Start by resolving the host name. |
| 278 | */ |
Yabin Cui | ec17f98 | 2014-11-13 10:29:25 -0800 | [diff] [blame] | 279 | #if defined(__linux__) |
Roland Levillain | d817560 | 2016-01-26 10:22:14 +0000 | [diff] [blame] | 280 | // Initial size of the work buffer used in gethostbyname_r. |
| 281 | // |
| 282 | // The call to gethostbyname_r below requires a user-allocated buffer, |
| 283 | // the size of which depends on the system. The initial implementation |
| 284 | // used to use a 128-byte buffer, but that was not enough on some |
| 285 | // systems (maybe because of IPv6), causing failures in JDWP host |
| 286 | // testing; thus it was increased to 256. |
| 287 | // |
| 288 | // However, we should not use a fixed size: gethostbyname_r's |
| 289 | // documentation states that if the work buffer is too small (i.e. if |
| 290 | // gethostbyname_r returns `ERANGE`), then the function should be |
| 291 | // called again with a bigger buffer. Which we do now, starting with |
| 292 | // an initial 256-byte buffer, and doubling it until gethostbyname_r |
| 293 | // accepts this size. |
| 294 | static constexpr size_t kInitialAuxBufSize = 256; |
| 295 | |
Roland Levillain | 3cbad24 | 2016-01-25 19:17:14 +0000 | [diff] [blame] | 296 | std::vector<char> auxBuf(kInitialAuxBufSize); |
Roland Levillain | d817560 | 2016-01-26 10:22:14 +0000 | [diff] [blame] | 297 | hostent he; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 298 | int error; |
Roland Levillain | 3cbad24 | 2016-01-25 19:17:14 +0000 | [diff] [blame] | 299 | int cc; |
| 300 | while ((cc = gethostbyname_r( |
| 301 | options->host.c_str(), &he, auxBuf.data(), auxBuf.size(), &pEntry, &error)) |
| 302 | == ERANGE) { |
| 303 | // The work buffer `auxBuf` is too small; enlarge it. |
| 304 | auxBuf.resize(auxBuf.size() * 2); |
| 305 | } |
| 306 | if (cc != 0 || pEntry == nullptr) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 307 | LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | #else |
| 311 | h_errno = 0; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 312 | pEntry = gethostbyname(options->host.c_str()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 313 | if (pEntry == nullptr) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 314 | PLOG(WARNING) << "gethostbyname('" << options->host << "') failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | #endif |
| 318 | |
| 319 | /* copy it out ASAP to minimize risk of multithreaded annoyances */ |
| 320 | memcpy(&addr.addrInet.sin_addr, pEntry->h_addr, pEntry->h_length); |
| 321 | addr.addrInet.sin_family = pEntry->h_addrtype; |
| 322 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 323 | addr.addrInet.sin_port = htons(options->port); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 324 | |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 325 | LOG(INFO) << "Connecting out to " << inet_ntoa(addr.addrInet.sin_addr) << ":" |
| 326 | << ntohs(addr.addrInet.sin_port); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * Create a socket. |
| 330 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 331 | clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 332 | if (clientSock < 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 333 | PLOG(ERROR) << "Unable to create socket"; |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Try to connect. |
| 339 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 340 | if (connect(clientSock, &addr.addrPlain, sizeof(addr)) != 0) { |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 341 | PLOG(ERROR) << "Unable to connect to " << inet_ntoa(addr.addrInet.sin_addr) << ":" |
| 342 | << ntohs(addr.addrInet.sin_port); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 343 | close(clientSock); |
| 344 | clientSock = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 345 | return false; |
| 346 | } |
| 347 | |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 348 | LOG(INFO) << "Connection established to " << options->host << " (" |
| 349 | << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")"; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 350 | SetAwaitingHandshake(true); |
| 351 | input_count_ = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 352 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 353 | SetNoDelay(clientSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 354 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 355 | if (!MakePipe()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 356 | return false; |
| 357 | } |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 363 | * Process incoming data. If no data is available, this will block until |
| 364 | * some arrives. |
| 365 | * |
| 366 | * If we get a full packet, handle it. |
| 367 | * |
| 368 | * To take some of the mystery out of life, we want to reject incoming |
| 369 | * connections if we already have a debugger attached. If we don't, the |
| 370 | * debugger will just mysteriously hang until it times out. We could just |
| 371 | * close the listen socket, but there's a good chance we won't be able to |
| 372 | * bind to the same port again, which would confuse utilities. |
| 373 | * |
| 374 | * Returns "false" on error (indicating that the connection has been severed), |
| 375 | * "true" if things are still okay. |
| 376 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 377 | bool JdwpSocketState::ProcessIncoming() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 378 | int readCount; |
| 379 | |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 380 | CHECK_NE(clientSock, -1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 381 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 382 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 383 | /* read some more, looping until we have data */ |
| 384 | errno = 0; |
| 385 | while (1) { |
| 386 | int selCount; |
| 387 | fd_set readfds; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 388 | int maxfd = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 389 | int fd; |
| 390 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 391 | FD_ZERO(&readfds); |
| 392 | |
| 393 | /* configure fds; note these may get zapped by another thread */ |
| 394 | fd = listenSock; |
| 395 | if (fd >= 0) { |
| 396 | FD_SET(fd, &readfds); |
| 397 | if (maxfd < fd) { |
| 398 | maxfd = fd; |
| 399 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 400 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 401 | fd = clientSock; |
| 402 | if (fd >= 0) { |
| 403 | FD_SET(fd, &readfds); |
| 404 | if (maxfd < fd) { |
| 405 | maxfd = fd; |
| 406 | } |
| 407 | } |
| 408 | fd = wake_pipe_[0]; |
| 409 | if (fd >= 0) { |
| 410 | FD_SET(fd, &readfds); |
| 411 | if (maxfd < fd) { |
| 412 | maxfd = fd; |
| 413 | } |
| 414 | } else { |
| 415 | LOG(INFO) << "NOTE: entering select w/o wakepipe"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (maxfd < 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 419 | VLOG(jdwp) << "+++ all fds are closed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 423 | /* |
| 424 | * Select blocks until it sees activity on the file descriptors. |
| 425 | * Closing the local file descriptor does not count as activity, |
| 426 | * so we can't rely on that to wake us up (it works for read() |
| 427 | * and accept(), but not select()). |
| 428 | * |
| 429 | * We can do one of three things: (1) send a signal and catch |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 430 | * EINTR, (2) open an additional fd ("wake pipe") and write to |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 431 | * it when it's time to exit, or (3) time out periodically and |
| 432 | * re-issue the select. We're currently using #2, as it's more |
| 433 | * reliable than #1 and generally better than #3. Wastes two fds. |
| 434 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 435 | selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 436 | if (selCount < 0) { |
| 437 | if (errno == EINTR) { |
| 438 | continue; |
| 439 | } |
| 440 | PLOG(ERROR) << "select failed"; |
| 441 | goto fail; |
| 442 | } |
| 443 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 444 | if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) { |
| 445 | if (listenSock >= 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 446 | LOG(ERROR) << "Exit wake set, but not exiting?"; |
| 447 | } else { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 448 | VLOG(jdwp) << "Got wake-up signal, bailing out of select"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 449 | } |
| 450 | goto fail; |
| 451 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 452 | if (listenSock >= 0 && FD_ISSET(listenSock, &readfds)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 453 | LOG(INFO) << "Ignoring second debugger -- accepting and dropping"; |
| 454 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 455 | sockaddr_in addrInet; |
| 456 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 457 | } addr; |
| 458 | socklen_t addrlen; |
| 459 | int tmpSock; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 460 | tmpSock = accept(listenSock, &addr.addrPlain, &addrlen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 461 | if (tmpSock < 0) { |
| 462 | LOG(INFO) << "Weird -- accept failed"; |
| 463 | } else { |
| 464 | close(tmpSock); |
| 465 | } |
| 466 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 467 | if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) { |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 468 | readCount = |
| 469 | read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 470 | if (readCount < 0) { |
| 471 | /* read failed */ |
| 472 | if (errno != EINTR) { |
| 473 | goto fail; |
| 474 | } |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 475 | VLOG(jdwp) << "+++ EINTR hit"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 476 | return true; |
| 477 | } else if (readCount == 0) { |
| 478 | /* EOF hit -- far end went away */ |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 479 | VLOG(jdwp) << "+++ peer disconnected"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 480 | goto fail; |
| 481 | } else { |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 487 | input_count_ += readCount; |
| 488 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 489 | return true; /* still not there yet */ |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Special-case the initial handshake. For some bizarre reason we're |
| 495 | * expected to emulate bad tty settings by echoing the request back |
| 496 | * exactly as it was sent. Note the handshake is always initiated by |
| 497 | * the debugger, no matter who connects to whom. |
| 498 | * |
| 499 | * Other than this one case, the protocol [claims to be] stateless. |
| 500 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 501 | if (IsAwaitingHandshake()) { |
| 502 | if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) { |
| 503 | LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 504 | goto fail; |
| 505 | } |
| 506 | |
| 507 | errno = 0; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 508 | int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 509 | if (cc != kMagicHandshakeLen) { |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 510 | PLOG(ERROR) << "Failed writing handshake bytes (" |
| 511 | << cc << " of " << kMagicHandshakeLen << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 512 | goto fail; |
| 513 | } |
| 514 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 515 | ConsumeBytes(kMagicHandshakeLen); |
| 516 | SetAwaitingHandshake(false); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 517 | VLOG(jdwp) << "+++ handshake complete"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 518 | return true; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Handle this packet. |
| 523 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 524 | return state_->HandlePacket(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 525 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 526 | fail: |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 527 | Close(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 531 | } // namespace JDWP |
| 532 | |
| 533 | } // namespace art |