blob: 4fb6df19f1d86665afe8b5278709d617935faed4 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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 Hughes872d4ec2011-10-21 17:07:15 -070016
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include <arpa/inet.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070018#include <errno.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include <netdb.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070020#include <netinet/in.h>
21#include <netinet/tcp.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#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 Hughese222ee02012-12-13 14:41:43 -080030#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031#include "jdwp/jdwp_priv.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070032
33#define kBasePort 8000
34#define kMaxPort 8040
35
Elliott Hughes872d4ec2011-10-21 17:07:15 -070036namespace art {
37
38namespace JDWP {
39
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040/*
41 * JDWP network state.
42 *
43 * We only talk to one debugger at a time.
44 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070045struct JdwpSocketState : public JdwpNetStateBase {
Elliott Hughes74847412012-06-20 18:10:21 -070046 uint16_t listenPort;
47 int listenSock; /* listen for connection from debugger */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070048
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020049 explicit JdwpSocketState(JdwpState* state)
50 : JdwpNetStateBase(state),
51 listenPort(0U),
52 listenSock(-1),
53 remote_port_(0U) {
Elliott Hughes74847412012-06-20 18:10:21 -070054 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070055
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 Hughes872d4ec2011-10-21 17:07:15 -070064};
65
Elliott Hughes5d10a872013-04-17 19:26:43 -070066static JdwpSocketState* SocketStartup(JdwpState* state, uint16_t port, bool probe);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070067
68/*
69 * Set up some stuff for transport=dt_socket.
70 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070071bool InitSocketTransport(JdwpState* state, const JdwpOptions* options) {
Elliott Hughes6d8dd472012-01-17 18:27:41 -080072 uint16_t port = options->port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070073
Elliott Hughes376a7a02011-10-24 18:35:55 -070074 if (options->server) {
75 if (options->port != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070076 /* try only the specified port */
Elliott Hughes5d10a872013-04-17 19:26:43 -070077 state->netState = SocketStartup(state, port, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 } else {
79 /* scan through a range of ports, binding to the first available */
80 for (port = kBasePort; port <= kMaxPort; port++) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070081 state->netState = SocketStartup(state, port, true);
Sebastien Hertz7d955652014-10-22 10:57:10 +020082 if (state->netState != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070083 break;
84 }
85 }
86 }
Sebastien Hertz7d955652014-10-22 10:57:10 +020087 if (state->netState == nullptr) {
Elliott Hughes376a7a02011-10-24 18:35:55 -070088 LOG(ERROR) << "JDWP net startup failed (req port=" << options->port << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089 return false;
90 }
91 } else {
Elliott Hughes5d10a872013-04-17 19:26:43 -070092 state->netState = SocketStartup(state, 0, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070093 }
94
Elliott Hughes376a7a02011-10-24 18:35:55 -070095 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070096 LOG(INFO) << "JDWP will wait for debugger on port " << port;
97 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -070098 LOG(INFO) << "JDWP will " << (options->server ? "listen" : "connect") << " on port " << port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070099 }
100
101 return true;
102}
103
104/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700105 * Initialize JDWP stuff.
106 *
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800107 * 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 Hughes872d4ec2011-10-21 17:07:15 -0700109 * 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 Hughes5d10a872013-04-17 19:26:43 -0700116static JdwpSocketState* SocketStartup(JdwpState* state, uint16_t port, bool probe) {
117 JdwpSocketState* netState = new JdwpSocketState(state);
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800118 if (port == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119 return netState;
120 }
121
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
123 if (netState->listenSock < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800124 PLOG(probe ? ERROR : FATAL) << "Socket create failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 goto fail;
126 }
127
128 /* allow immediate re-use */
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800129 {
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 Hughes872d4ec2011-10-21 17:07:15 -0700135 }
136
137 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700138 sockaddr_in addrInet;
139 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140 } 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 Hughes3d30d9b2011-12-07 17:35:48 -0800146 PLOG(probe ? ERROR : FATAL) << "Attempt to bind to port " << port << " failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147 goto fail;
148 }
149
150 netState->listenPort = port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151
152 if (listen(netState->listenSock, 5) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800153 PLOG(probe ? ERROR : FATAL) << "Listen failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154 goto fail;
155 }
156
157 return netState;
158
Elliott Hughesa21039c2012-06-21 12:09:25 -0700159 fail:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700160 netState->Shutdown();
161 delete netState;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200162 return nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700163}
164
165/*
166 * Shut down JDWP listener. Don't free state.
167 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168 * 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 Hughes5d10a872013-04-17 19:26:43 -0700174void JdwpSocketState::Shutdown() {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800175 int local_listenSock = this->listenSock;
176 int local_clientSock = this->clientSock;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700177
178 /* clear these out so it doesn't wake up and try to reuse them */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700179 this->listenSock = this->clientSock = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180
181 /* "shutdown" dislodges blocking read() and accept() calls */
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800182 if (local_listenSock != -1) {
183 shutdown(local_listenSock, SHUT_RDWR);
184 close(local_listenSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800186 if (local_clientSock != -1) {
187 shutdown(local_clientSock, SHUT_RDWR);
188 close(local_clientSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189 }
190
Elliott Hughes5d10a872013-04-17 19:26:43 -0700191 WakePipe();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700192}
193
194/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700195 * 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 Hughes5d10a872013-04-17 19:26:43 -0700199static int SetNoDelay(int fd) {
Elliott Hughes74847412012-06-20 18:10:21 -0700200 int on = 1;
201 int cc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
202 CHECK_EQ(cc, 0);
203 return cc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700204}
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 Hughes5d10a872013-04-17 19:26:43 -0700211bool JdwpSocketState::Accept() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700213 sockaddr_in addrInet;
214 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 } addr;
216 socklen_t addrlen;
217 int sock;
218
Elliott Hughes5d10a872013-04-17 19:26:43 -0700219 if (listenSock < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700220 return false; /* you're not listening! */
221 }
222
Brian Carlstrom42748892013-07-18 18:04:08 -0700223 CHECK_EQ(clientSock, -1); /* must not already be talking */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224
225 addrlen = sizeof(addr);
226 do {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700227 sock = accept(listenSock, &addr.addrPlain, &addrlen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 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 Hughes4dd9b4d2011-12-12 18:29:24 -0800232 if (VLOG_IS_ON(jdwp)) {
233 PLOG(ERROR) << "accept failed";
234 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235 } else {
236 PLOG(ERROR) << "accept failed";
237 return false;
238 }
239 }
240 } while (sock < 0);
241
Elliott Hughes5d10a872013-04-17 19:26:43 -0700242 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 Hughes872d4ec2011-10-21 17:07:15 -0700245
Elliott Hughes5d10a872013-04-17 19:26:43 -0700246 clientSock = sock;
247 SetAwaitingHandshake(true);
248 input_count_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800250 VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700251 SetNoDelay(clientSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700252
Elliott Hughes5d10a872013-04-17 19:26:43 -0700253 if (!MakePipe()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700254 return false;
255 }
256
257 return true;
258}
259
260/*
261 * Create a connection to a waiting debugger.
262 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700263bool JdwpSocketState::Establish(const JdwpOptions* options) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700264 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700265 sockaddr_in addrInet;
266 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700267 } addr;
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700268 hostent* pEntry;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269
Elliott Hughesa21039c2012-06-21 12:09:25 -0700270 CHECK(!options->server);
271 CHECK(!options->host.empty());
272 CHECK_NE(options->port, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700273
274 /*
275 * Start by resolving the host name.
276 */
Yabin Cuiec17f982014-11-13 10:29:25 -0800277#if defined(__linux__)
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700278 hostent he;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700279 char auxBuf[128];
280 int error;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700281 int cc = gethostbyname_r(options->host.c_str(), &he, auxBuf, sizeof(auxBuf), &pEntry, &error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 if (cc != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700283 LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284 return false;
285 }
286#else
287 h_errno = 0;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700288 pEntry = gethostbyname(options->host.c_str());
Sebastien Hertz7d955652014-10-22 10:57:10 +0200289 if (pEntry == nullptr) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700290 PLOG(WARNING) << "gethostbyname('" << options->host << "') failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700291 return false;
292 }
293#endif
294
295 /* copy it out ASAP to minimize risk of multithreaded annoyances */
296 memcpy(&addr.addrInet.sin_addr, pEntry->h_addr, pEntry->h_length);
297 addr.addrInet.sin_family = pEntry->h_addrtype;
298
Elliott Hughesa21039c2012-06-21 12:09:25 -0700299 addr.addrInet.sin_port = htons(options->port);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700300
301 LOG(INFO) << "Connecting out to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
302
303 /*
304 * Create a socket.
305 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700306 clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
307 if (clientSock < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308 PLOG(ERROR) << "Unable to create socket";
309 return false;
310 }
311
312 /*
313 * Try to connect.
314 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700315 if (connect(clientSock, &addr.addrPlain, sizeof(addr)) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700316 PLOG(ERROR) << "Unable to connect to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700317 close(clientSock);
318 clientSock = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319 return false;
320 }
321
Elliott Hughesa21039c2012-06-21 12:09:25 -0700322 LOG(INFO) << "Connection established to " << options->host << " (" << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700323 SetAwaitingHandshake(true);
324 input_count_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325
Elliott Hughes5d10a872013-04-17 19:26:43 -0700326 SetNoDelay(clientSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700327
Elliott Hughes5d10a872013-04-17 19:26:43 -0700328 if (!MakePipe()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700329 return false;
330 }
331
332 return true;
333}
334
335/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 * Process incoming data. If no data is available, this will block until
337 * some arrives.
338 *
339 * If we get a full packet, handle it.
340 *
341 * To take some of the mystery out of life, we want to reject incoming
342 * connections if we already have a debugger attached. If we don't, the
343 * debugger will just mysteriously hang until it times out. We could just
344 * close the listen socket, but there's a good chance we won't be able to
345 * bind to the same port again, which would confuse utilities.
346 *
347 * Returns "false" on error (indicating that the connection has been severed),
348 * "true" if things are still okay.
349 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700350bool JdwpSocketState::ProcessIncoming() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351 int readCount;
352
Brian Carlstrom42748892013-07-18 18:04:08 -0700353 CHECK_NE(clientSock, -1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354
Elliott Hughes5d10a872013-04-17 19:26:43 -0700355 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 /* read some more, looping until we have data */
357 errno = 0;
358 while (1) {
359 int selCount;
360 fd_set readfds;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700361 int maxfd = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700362 int fd;
363
Elliott Hughes5d10a872013-04-17 19:26:43 -0700364 FD_ZERO(&readfds);
365
366 /* configure fds; note these may get zapped by another thread */
367 fd = listenSock;
368 if (fd >= 0) {
369 FD_SET(fd, &readfds);
370 if (maxfd < fd) {
371 maxfd = fd;
372 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700373 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700374 fd = clientSock;
375 if (fd >= 0) {
376 FD_SET(fd, &readfds);
377 if (maxfd < fd) {
378 maxfd = fd;
379 }
380 }
381 fd = wake_pipe_[0];
382 if (fd >= 0) {
383 FD_SET(fd, &readfds);
384 if (maxfd < fd) {
385 maxfd = fd;
386 }
387 } else {
388 LOG(INFO) << "NOTE: entering select w/o wakepipe";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700389 }
390
391 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800392 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700393 return false;
394 }
395
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700396 /*
397 * Select blocks until it sees activity on the file descriptors.
398 * Closing the local file descriptor does not count as activity,
399 * so we can't rely on that to wake us up (it works for read()
400 * and accept(), but not select()).
401 *
402 * We can do one of three things: (1) send a signal and catch
Elliott Hughes5d10a872013-04-17 19:26:43 -0700403 * EINTR, (2) open an additional fd ("wake pipe") and write to
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404 * it when it's time to exit, or (3) time out periodically and
405 * re-issue the select. We're currently using #2, as it's more
406 * reliable than #1 and generally better than #3. Wastes two fds.
407 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200408 selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700409 if (selCount < 0) {
410 if (errno == EINTR) {
411 continue;
412 }
413 PLOG(ERROR) << "select failed";
414 goto fail;
415 }
416
Elliott Hughes5d10a872013-04-17 19:26:43 -0700417 if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) {
418 if (listenSock >= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700419 LOG(ERROR) << "Exit wake set, but not exiting?";
420 } else {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700421 VLOG(jdwp) << "Got wake-up signal, bailing out of select";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700422 }
423 goto fail;
424 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700425 if (listenSock >= 0 && FD_ISSET(listenSock, &readfds)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
427 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700428 sockaddr_in addrInet;
429 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430 } addr;
431 socklen_t addrlen;
432 int tmpSock;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700433 tmpSock = accept(listenSock, &addr.addrPlain, &addrlen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434 if (tmpSock < 0) {
435 LOG(INFO) << "Weird -- accept failed";
436 } else {
437 close(tmpSock);
438 }
439 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700440 if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) {
441 readCount = read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442 if (readCount < 0) {
443 /* read failed */
444 if (errno != EINTR) {
445 goto fail;
446 }
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700447 VLOG(jdwp) << "+++ EINTR hit";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700448 return true;
449 } else if (readCount == 0) {
450 /* EOF hit -- far end went away */
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700451 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700452 goto fail;
453 } else {
454 break;
455 }
456 }
457 }
458
Elliott Hughes5d10a872013-04-17 19:26:43 -0700459 input_count_ += readCount;
460 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700461 return true; /* still not there yet */
462 }
463 }
464
465 /*
466 * Special-case the initial handshake. For some bizarre reason we're
467 * expected to emulate bad tty settings by echoing the request back
468 * exactly as it was sent. Note the handshake is always initiated by
469 * the debugger, no matter who connects to whom.
470 *
471 * Other than this one case, the protocol [claims to be] stateless.
472 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700473 if (IsAwaitingHandshake()) {
474 if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) {
475 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700476 goto fail;
477 }
478
479 errno = 0;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700480 int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700481 if (cc != kMagicHandshakeLen) {
482 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
483 goto fail;
484 }
485
Elliott Hughes5d10a872013-04-17 19:26:43 -0700486 ConsumeBytes(kMagicHandshakeLen);
487 SetAwaitingHandshake(false);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800488 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700489 return true;
490 }
491
492 /*
493 * Handle this packet.
494 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700495 return state_->HandlePacket();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700496
Elliott Hughesa21039c2012-06-21 12:09:25 -0700497 fail:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700498 Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700499 return false;
500}
501
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502} // namespace JDWP
503
504} // namespace art