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 | |
| 33 | #define kBasePort 8000 |
| 34 | #define kMaxPort 8040 |
| 35 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 36 | namespace art { |
| 37 | |
| 38 | namespace JDWP { |
| 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) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 124 | PLOG(probe ? ERROR : 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) { |
| 132 | PLOG(probe ? ERROR : FATAL) << "setsockopt(SO_REUSEADDR) failed"; |
| 133 | goto fail; |
| 134 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 138 | sockaddr_in addrInet; |
| 139 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 140 | } addr; |
| 141 | addr.addrInet.sin_family = AF_INET; |
| 142 | addr.addrInet.sin_port = htons(port); |
| 143 | inet_aton("127.0.0.1", &addr.addrInet.sin_addr); |
| 144 | |
| 145 | if (bind(netState->listenSock, &addr.addrPlain, sizeof(addr)) != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 146 | PLOG(probe ? ERROR : FATAL) << "Attempt to bind to port " << port << " failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 147 | goto fail; |
| 148 | } |
| 149 | |
| 150 | netState->listenPort = port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 151 | |
| 152 | if (listen(netState->listenSock, 5) != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 153 | PLOG(probe ? ERROR : FATAL) << "Listen failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 154 | goto fail; |
| 155 | } |
| 156 | |
| 157 | return netState; |
| 158 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 159 | fail: |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 160 | netState->Shutdown(); |
| 161 | delete netState; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 162 | return nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Shut down JDWP listener. Don't free state. |
| 167 | * |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 168 | * This may be called from a non-JDWP thread as part of shutting the |
| 169 | * JDWP thread down. |
| 170 | * |
| 171 | * (This is currently called several times during startup as we probe |
| 172 | * for an open port.) |
| 173 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 174 | void JdwpSocketState::Shutdown() { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 175 | int local_listenSock = this->listenSock; |
| 176 | int local_clientSock = this->clientSock; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 177 | |
| 178 | /* 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] | 179 | this->listenSock = this->clientSock = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 180 | |
| 181 | /* "shutdown" dislodges blocking read() and accept() calls */ |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 182 | if (local_listenSock != -1) { |
| 183 | shutdown(local_listenSock, SHUT_RDWR); |
| 184 | close(local_listenSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 185 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 186 | if (local_clientSock != -1) { |
| 187 | shutdown(local_clientSock, SHUT_RDWR); |
| 188 | close(local_clientSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 191 | WakePipe(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 195 | * Disable the TCP Nagle algorithm, which delays transmission of outbound |
| 196 | * packets until the previous transmissions have been acked. JDWP does a |
| 197 | * lot of back-and-forth with small packets, so this may help. |
| 198 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 199 | static int SetNoDelay(int fd) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 200 | int on = 1; |
| 201 | int cc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 202 | CHECK_EQ(cc, 0); |
| 203 | return cc; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Accept a connection. This will block waiting for somebody to show up. |
| 208 | * If that's not desirable, use checkConnection() to make sure something |
| 209 | * is pending. |
| 210 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 211 | bool JdwpSocketState::Accept() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 212 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 213 | sockaddr_in addrInet; |
| 214 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 215 | } addr; |
| 216 | socklen_t addrlen; |
| 217 | int sock; |
| 218 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 219 | if (listenSock < 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 220 | return false; /* you're not listening! */ |
| 221 | } |
| 222 | |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 223 | CHECK_EQ(clientSock, -1); /* must not already be talking */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 224 | |
| 225 | addrlen = sizeof(addr); |
| 226 | do { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 227 | sock = accept(listenSock, &addr.addrPlain, &addrlen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 228 | if (sock < 0 && errno != EINTR) { |
| 229 | // When we call shutdown() on the socket, accept() returns with |
| 230 | // EINVAL. Don't gripe about it. |
| 231 | if (errno == EINVAL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 232 | if (VLOG_IS_ON(jdwp)) { |
| 233 | PLOG(ERROR) << "accept failed"; |
| 234 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 235 | } else { |
| 236 | PLOG(ERROR) << "accept failed"; |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | } while (sock < 0); |
| 241 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 242 | remote_addr_ = addr.addrInet.sin_addr; |
| 243 | remote_port_ = ntohs(addr.addrInet.sin_port); |
| 244 | VLOG(jdwp) << "+++ accepted connection from " << inet_ntoa(remote_addr_) << ":" << remote_port_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 245 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 246 | clientSock = sock; |
| 247 | SetAwaitingHandshake(true); |
| 248 | input_count_ = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 249 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 250 | VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket"; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 251 | SetNoDelay(clientSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 253 | if (!MakePipe()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Create a connection to a waiting debugger. |
| 262 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 263 | bool JdwpSocketState::Establish(const JdwpOptions* options) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 264 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 265 | sockaddr_in addrInet; |
| 266 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 267 | } addr; |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 268 | hostent* pEntry; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 269 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 270 | CHECK(!options->server); |
| 271 | CHECK(!options->host.empty()); |
| 272 | CHECK_NE(options->port, 0); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * Start by resolving the host name. |
| 276 | */ |
Yabin Cui | ec17f98 | 2014-11-13 10:29:25 -0800 | [diff] [blame] | 277 | #if defined(__linux__) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 278 | hostent he; |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 279 | // The size of the work buffer used in the gethostbyname_r call |
| 280 | // below. It used to be 128, but this was not enough on some |
| 281 | // configurations (maybe because of IPv6?), causing failures in JDWP |
| 282 | // host testing; thus it was increased to 256. |
| 283 | static constexpr size_t kAuxBufSize = 256; |
| 284 | char auxBuf[kAuxBufSize]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 285 | int error; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 286 | int cc = gethostbyname_r(options->host.c_str(), &he, auxBuf, sizeof(auxBuf), &pEntry, &error); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 287 | if (cc != 0) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 288 | LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | #else |
| 292 | h_errno = 0; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 293 | pEntry = gethostbyname(options->host.c_str()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 294 | if (pEntry == nullptr) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 295 | PLOG(WARNING) << "gethostbyname('" << options->host << "') failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | /* copy it out ASAP to minimize risk of multithreaded annoyances */ |
| 301 | memcpy(&addr.addrInet.sin_addr, pEntry->h_addr, pEntry->h_length); |
| 302 | addr.addrInet.sin_family = pEntry->h_addrtype; |
| 303 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 304 | addr.addrInet.sin_port = htons(options->port); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 305 | |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 306 | LOG(INFO) << "Connecting out to " << inet_ntoa(addr.addrInet.sin_addr) << ":" |
| 307 | << ntohs(addr.addrInet.sin_port); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Create a socket. |
| 311 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 312 | clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 313 | if (clientSock < 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 314 | PLOG(ERROR) << "Unable to create socket"; |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Try to connect. |
| 320 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 321 | if (connect(clientSock, &addr.addrPlain, sizeof(addr)) != 0) { |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 322 | PLOG(ERROR) << "Unable to connect to " << inet_ntoa(addr.addrInet.sin_addr) << ":" |
| 323 | << ntohs(addr.addrInet.sin_port); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 324 | close(clientSock); |
| 325 | clientSock = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 329 | LOG(INFO) << "Connection established to " << options->host << " (" |
| 330 | << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")"; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 331 | SetAwaitingHandshake(true); |
| 332 | input_count_ = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 333 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 334 | SetNoDelay(clientSock); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 335 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 336 | if (!MakePipe()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 344 | * Process incoming data. If no data is available, this will block until |
| 345 | * some arrives. |
| 346 | * |
| 347 | * If we get a full packet, handle it. |
| 348 | * |
| 349 | * To take some of the mystery out of life, we want to reject incoming |
| 350 | * connections if we already have a debugger attached. If we don't, the |
| 351 | * debugger will just mysteriously hang until it times out. We could just |
| 352 | * close the listen socket, but there's a good chance we won't be able to |
| 353 | * bind to the same port again, which would confuse utilities. |
| 354 | * |
| 355 | * Returns "false" on error (indicating that the connection has been severed), |
| 356 | * "true" if things are still okay. |
| 357 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 358 | bool JdwpSocketState::ProcessIncoming() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 359 | int readCount; |
| 360 | |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 361 | CHECK_NE(clientSock, -1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 362 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 363 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 364 | /* read some more, looping until we have data */ |
| 365 | errno = 0; |
| 366 | while (1) { |
| 367 | int selCount; |
| 368 | fd_set readfds; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 369 | int maxfd = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 370 | int fd; |
| 371 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 372 | FD_ZERO(&readfds); |
| 373 | |
| 374 | /* configure fds; note these may get zapped by another thread */ |
| 375 | fd = listenSock; |
| 376 | if (fd >= 0) { |
| 377 | FD_SET(fd, &readfds); |
| 378 | if (maxfd < fd) { |
| 379 | maxfd = fd; |
| 380 | } |
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 | fd = clientSock; |
| 383 | if (fd >= 0) { |
| 384 | FD_SET(fd, &readfds); |
| 385 | if (maxfd < fd) { |
| 386 | maxfd = fd; |
| 387 | } |
| 388 | } |
| 389 | fd = wake_pipe_[0]; |
| 390 | if (fd >= 0) { |
| 391 | FD_SET(fd, &readfds); |
| 392 | if (maxfd < fd) { |
| 393 | maxfd = fd; |
| 394 | } |
| 395 | } else { |
| 396 | LOG(INFO) << "NOTE: entering select w/o wakepipe"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | if (maxfd < 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 400 | VLOG(jdwp) << "+++ all fds are closed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 401 | return false; |
| 402 | } |
| 403 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 404 | /* |
| 405 | * Select blocks until it sees activity on the file descriptors. |
| 406 | * Closing the local file descriptor does not count as activity, |
| 407 | * so we can't rely on that to wake us up (it works for read() |
| 408 | * and accept(), but not select()). |
| 409 | * |
| 410 | * 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] | 411 | * EINTR, (2) open an additional fd ("wake pipe") and write to |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 412 | * it when it's time to exit, or (3) time out periodically and |
| 413 | * re-issue the select. We're currently using #2, as it's more |
| 414 | * reliable than #1 and generally better than #3. Wastes two fds. |
| 415 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 416 | selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 417 | if (selCount < 0) { |
| 418 | if (errno == EINTR) { |
| 419 | continue; |
| 420 | } |
| 421 | PLOG(ERROR) << "select failed"; |
| 422 | goto fail; |
| 423 | } |
| 424 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 425 | if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) { |
| 426 | if (listenSock >= 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 427 | LOG(ERROR) << "Exit wake set, but not exiting?"; |
| 428 | } else { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 429 | VLOG(jdwp) << "Got wake-up signal, bailing out of select"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 430 | } |
| 431 | goto fail; |
| 432 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 433 | if (listenSock >= 0 && FD_ISSET(listenSock, &readfds)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 434 | LOG(INFO) << "Ignoring second debugger -- accepting and dropping"; |
| 435 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 436 | sockaddr_in addrInet; |
| 437 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 438 | } addr; |
| 439 | socklen_t addrlen; |
| 440 | int tmpSock; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 441 | tmpSock = accept(listenSock, &addr.addrPlain, &addrlen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 442 | if (tmpSock < 0) { |
| 443 | LOG(INFO) << "Weird -- accept failed"; |
| 444 | } else { |
| 445 | close(tmpSock); |
| 446 | } |
| 447 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 448 | if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) { |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 449 | readCount = |
| 450 | read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 451 | if (readCount < 0) { |
| 452 | /* read failed */ |
| 453 | if (errno != EINTR) { |
| 454 | goto fail; |
| 455 | } |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 456 | VLOG(jdwp) << "+++ EINTR hit"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 457 | return true; |
| 458 | } else if (readCount == 0) { |
| 459 | /* EOF hit -- far end went away */ |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 460 | VLOG(jdwp) << "+++ peer disconnected"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 461 | goto fail; |
| 462 | } else { |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 468 | input_count_ += readCount; |
| 469 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 470 | return true; /* still not there yet */ |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Special-case the initial handshake. For some bizarre reason we're |
| 476 | * expected to emulate bad tty settings by echoing the request back |
| 477 | * exactly as it was sent. Note the handshake is always initiated by |
| 478 | * the debugger, no matter who connects to whom. |
| 479 | * |
| 480 | * Other than this one case, the protocol [claims to be] stateless. |
| 481 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 482 | if (IsAwaitingHandshake()) { |
| 483 | if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) { |
| 484 | LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 485 | goto fail; |
| 486 | } |
| 487 | |
| 488 | errno = 0; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 489 | int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 490 | if (cc != kMagicHandshakeLen) { |
Roland Levillain | 15f9b27 | 2016-01-21 14:01:10 +0000 | [diff] [blame] | 491 | PLOG(ERROR) << "Failed writing handshake bytes (" |
| 492 | << cc << " of " << kMagicHandshakeLen << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 493 | goto fail; |
| 494 | } |
| 495 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 496 | ConsumeBytes(kMagicHandshakeLen); |
| 497 | SetAwaitingHandshake(false); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 498 | VLOG(jdwp) << "+++ handshake complete"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Handle this packet. |
| 504 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 505 | return state_->HandlePacket(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 506 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 507 | fail: |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 508 | Close(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 512 | } // namespace JDWP |
| 513 | |
| 514 | } // namespace art |