blob: cbdde24ae68ef4d4e428e1367dba0bfef1bf6171 [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
Brian Carlstrom93ba8932013-07-17 21:31:49 -070049 explicit JdwpSocketState(JdwpState* state) : JdwpNetStateBase(state) {
Elliott Hughes74847412012-06-20 18:10:21 -070050 listenPort = 0;
51 listenSock = -1;
Elliott Hughes74847412012-06-20 18:10:21 -070052 }
Elliott Hughes5d10a872013-04-17 19:26:43 -070053
54 virtual bool Accept();
55 virtual bool Establish(const JdwpOptions*);
56 virtual void Shutdown();
57 virtual bool ProcessIncoming();
58
59 private:
60 in_addr remote_addr_;
61 uint16_t remote_port_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070062};
63
Elliott Hughes5d10a872013-04-17 19:26:43 -070064static JdwpSocketState* SocketStartup(JdwpState* state, uint16_t port, bool probe);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070065
66/*
67 * Set up some stuff for transport=dt_socket.
68 */
Elliott Hughes5d10a872013-04-17 19:26:43 -070069bool InitSocketTransport(JdwpState* state, const JdwpOptions* options) {
Elliott Hughes6d8dd472012-01-17 18:27:41 -080070 uint16_t port = options->port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070071
Elliott Hughes376a7a02011-10-24 18:35:55 -070072 if (options->server) {
73 if (options->port != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070074 /* try only the specified port */
Elliott Hughes5d10a872013-04-17 19:26:43 -070075 state->netState = SocketStartup(state, port, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070076 } else {
77 /* scan through a range of ports, binding to the first available */
78 for (port = kBasePort; port <= kMaxPort; port++) {
Elliott Hughes5d10a872013-04-17 19:26:43 -070079 state->netState = SocketStartup(state, port, true);
Sebastien Hertz7d955652014-10-22 10:57:10 +020080 if (state->netState != nullptr) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070081 break;
82 }
83 }
84 }
Sebastien Hertz7d955652014-10-22 10:57:10 +020085 if (state->netState == nullptr) {
Elliott Hughes376a7a02011-10-24 18:35:55 -070086 LOG(ERROR) << "JDWP net startup failed (req port=" << options->port << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -070087 return false;
88 }
89 } else {
Elliott Hughes5d10a872013-04-17 19:26:43 -070090 state->netState = SocketStartup(state, 0, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091 }
92
Elliott Hughes376a7a02011-10-24 18:35:55 -070093 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070094 LOG(INFO) << "JDWP will wait for debugger on port " << port;
95 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -070096 LOG(INFO) << "JDWP will " << (options->server ? "listen" : "connect") << " on port " << port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070097 }
98
99 return true;
100}
101
102/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700103 * Initialize JDWP stuff.
104 *
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800105 * Allocates a new state structure. If "port" is non-zero, this also
106 * tries to bind to a listen port. If "port" is zero, we assume
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700107 * we're preparing for an outbound connection, and return without binding
108 * to anything.
109 *
110 * This may be called several times if we're probing for a port.
111 *
112 * Returns 0 on success.
113 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700114static JdwpSocketState* SocketStartup(JdwpState* state, uint16_t port, bool probe) {
115 JdwpSocketState* netState = new JdwpSocketState(state);
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800116 if (port == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117 return netState;
118 }
119
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120 netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
121 if (netState->listenSock < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800122 PLOG(probe ? ERROR : FATAL) << "Socket create failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 goto fail;
124 }
125
126 /* allow immediate re-use */
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800127 {
128 int one = 1;
129 if (setsockopt(netState->listenSock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
130 PLOG(probe ? ERROR : FATAL) << "setsockopt(SO_REUSEADDR) failed";
131 goto fail;
132 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 }
134
135 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700136 sockaddr_in addrInet;
137 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700138 } addr;
139 addr.addrInet.sin_family = AF_INET;
140 addr.addrInet.sin_port = htons(port);
141 inet_aton("127.0.0.1", &addr.addrInet.sin_addr);
142
143 if (bind(netState->listenSock, &addr.addrPlain, sizeof(addr)) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800144 PLOG(probe ? ERROR : FATAL) << "Attempt to bind to port " << port << " failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145 goto fail;
146 }
147
148 netState->listenPort = port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700149
150 if (listen(netState->listenSock, 5) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800151 PLOG(probe ? ERROR : FATAL) << "Listen failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152 goto fail;
153 }
154
155 return netState;
156
Elliott Hughesa21039c2012-06-21 12:09:25 -0700157 fail:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700158 netState->Shutdown();
159 delete netState;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200160 return nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161}
162
163/*
164 * Shut down JDWP listener. Don't free state.
165 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166 * This may be called from a non-JDWP thread as part of shutting the
167 * JDWP thread down.
168 *
169 * (This is currently called several times during startup as we probe
170 * for an open port.)
171 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700172void JdwpSocketState::Shutdown() {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800173 int local_listenSock = this->listenSock;
174 int local_clientSock = this->clientSock;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700175
176 /* clear these out so it doesn't wake up and try to reuse them */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700177 this->listenSock = this->clientSock = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700178
179 /* "shutdown" dislodges blocking read() and accept() calls */
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800180 if (local_listenSock != -1) {
181 shutdown(local_listenSock, SHUT_RDWR);
182 close(local_listenSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700183 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800184 if (local_clientSock != -1) {
185 shutdown(local_clientSock, SHUT_RDWR);
186 close(local_clientSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187 }
188
Elliott Hughes5d10a872013-04-17 19:26:43 -0700189 WakePipe();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190}
191
192/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193 * Disable the TCP Nagle algorithm, which delays transmission of outbound
194 * packets until the previous transmissions have been acked. JDWP does a
195 * lot of back-and-forth with small packets, so this may help.
196 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700197static int SetNoDelay(int fd) {
Elliott Hughes74847412012-06-20 18:10:21 -0700198 int on = 1;
199 int cc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
200 CHECK_EQ(cc, 0);
201 return cc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202}
203
204/*
205 * Accept a connection. This will block waiting for somebody to show up.
206 * If that's not desirable, use checkConnection() to make sure something
207 * is pending.
208 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700209bool JdwpSocketState::Accept() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700211 sockaddr_in addrInet;
212 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213 } addr;
214 socklen_t addrlen;
215 int sock;
216
Elliott Hughes5d10a872013-04-17 19:26:43 -0700217 if (listenSock < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218 return false; /* you're not listening! */
219 }
220
Brian Carlstrom42748892013-07-18 18:04:08 -0700221 CHECK_EQ(clientSock, -1); /* must not already be talking */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222
223 addrlen = sizeof(addr);
224 do {
Elliott Hughes5d10a872013-04-17 19:26:43 -0700225 sock = accept(listenSock, &addr.addrPlain, &addrlen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 if (sock < 0 && errno != EINTR) {
227 // When we call shutdown() on the socket, accept() returns with
228 // EINVAL. Don't gripe about it.
229 if (errno == EINVAL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800230 if (VLOG_IS_ON(jdwp)) {
231 PLOG(ERROR) << "accept failed";
232 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700233 } else {
234 PLOG(ERROR) << "accept failed";
235 return false;
236 }
237 }
238 } while (sock < 0);
239
Elliott Hughes5d10a872013-04-17 19:26:43 -0700240 remote_addr_ = addr.addrInet.sin_addr;
241 remote_port_ = ntohs(addr.addrInet.sin_port);
242 VLOG(jdwp) << "+++ accepted connection from " << inet_ntoa(remote_addr_) << ":" << remote_port_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243
Elliott Hughes5d10a872013-04-17 19:26:43 -0700244 clientSock = sock;
245 SetAwaitingHandshake(true);
246 input_count_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800248 VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket";
Elliott Hughes5d10a872013-04-17 19:26:43 -0700249 SetNoDelay(clientSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700250
Elliott Hughes5d10a872013-04-17 19:26:43 -0700251 if (!MakePipe()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700252 return false;
253 }
254
255 return true;
256}
257
258/*
259 * Create a connection to a waiting debugger.
260 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700261bool JdwpSocketState::Establish(const JdwpOptions* options) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700263 sockaddr_in addrInet;
264 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265 } addr;
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700266 hostent* pEntry;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700267
Elliott Hughesa21039c2012-06-21 12:09:25 -0700268 CHECK(!options->server);
269 CHECK(!options->host.empty());
270 CHECK_NE(options->port, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700271
272 /*
273 * Start by resolving the host name.
274 */
Yabin Cuiec17f982014-11-13 10:29:25 -0800275#if defined(__linux__)
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700276 hostent he;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700277 char auxBuf[128];
278 int error;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700279 int cc = gethostbyname_r(options->host.c_str(), &he, auxBuf, sizeof(auxBuf), &pEntry, &error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280 if (cc != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700281 LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 return false;
283 }
284#else
285 h_errno = 0;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700286 pEntry = gethostbyname(options->host.c_str());
Sebastien Hertz7d955652014-10-22 10:57:10 +0200287 if (pEntry == nullptr) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700288 PLOG(WARNING) << "gethostbyname('" << options->host << "') failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700289 return false;
290 }
291#endif
292
293 /* copy it out ASAP to minimize risk of multithreaded annoyances */
294 memcpy(&addr.addrInet.sin_addr, pEntry->h_addr, pEntry->h_length);
295 addr.addrInet.sin_family = pEntry->h_addrtype;
296
Elliott Hughesa21039c2012-06-21 12:09:25 -0700297 addr.addrInet.sin_port = htons(options->port);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700298
299 LOG(INFO) << "Connecting out to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
300
301 /*
302 * Create a socket.
303 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700304 clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
305 if (clientSock < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306 PLOG(ERROR) << "Unable to create socket";
307 return false;
308 }
309
310 /*
311 * Try to connect.
312 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700313 if (connect(clientSock, &addr.addrPlain, sizeof(addr)) != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314 PLOG(ERROR) << "Unable to connect to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port);
Elliott Hughes5d10a872013-04-17 19:26:43 -0700315 close(clientSock);
316 clientSock = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700317 return false;
318 }
319
Elliott Hughesa21039c2012-06-21 12:09:25 -0700320 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 -0700321 SetAwaitingHandshake(true);
322 input_count_ = 0;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323
Elliott Hughes5d10a872013-04-17 19:26:43 -0700324 SetNoDelay(clientSock);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325
Elliott Hughes5d10a872013-04-17 19:26:43 -0700326 if (!MakePipe()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700327 return false;
328 }
329
330 return true;
331}
332
333/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700334 * Process incoming data. If no data is available, this will block until
335 * some arrives.
336 *
337 * If we get a full packet, handle it.
338 *
339 * To take some of the mystery out of life, we want to reject incoming
340 * connections if we already have a debugger attached. If we don't, the
341 * debugger will just mysteriously hang until it times out. We could just
342 * close the listen socket, but there's a good chance we won't be able to
343 * bind to the same port again, which would confuse utilities.
344 *
345 * Returns "false" on error (indicating that the connection has been severed),
346 * "true" if things are still okay.
347 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700348bool JdwpSocketState::ProcessIncoming() {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349 int readCount;
350
Brian Carlstrom42748892013-07-18 18:04:08 -0700351 CHECK_NE(clientSock, -1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700352
Elliott Hughes5d10a872013-04-17 19:26:43 -0700353 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354 /* read some more, looping until we have data */
355 errno = 0;
356 while (1) {
357 int selCount;
358 fd_set readfds;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700359 int maxfd = -1;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700360 int fd;
361
Elliott Hughes5d10a872013-04-17 19:26:43 -0700362 FD_ZERO(&readfds);
363
364 /* configure fds; note these may get zapped by another thread */
365 fd = listenSock;
366 if (fd >= 0) {
367 FD_SET(fd, &readfds);
368 if (maxfd < fd) {
369 maxfd = fd;
370 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700372 fd = clientSock;
373 if (fd >= 0) {
374 FD_SET(fd, &readfds);
375 if (maxfd < fd) {
376 maxfd = fd;
377 }
378 }
379 fd = wake_pipe_[0];
380 if (fd >= 0) {
381 FD_SET(fd, &readfds);
382 if (maxfd < fd) {
383 maxfd = fd;
384 }
385 } else {
386 LOG(INFO) << "NOTE: entering select w/o wakepipe";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700387 }
388
389 if (maxfd < 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800390 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700391 return false;
392 }
393
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700394 /*
395 * Select blocks until it sees activity on the file descriptors.
396 * Closing the local file descriptor does not count as activity,
397 * so we can't rely on that to wake us up (it works for read()
398 * and accept(), but not select()).
399 *
400 * We can do one of three things: (1) send a signal and catch
Elliott Hughes5d10a872013-04-17 19:26:43 -0700401 * EINTR, (2) open an additional fd ("wake pipe") and write to
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700402 * it when it's time to exit, or (3) time out periodically and
403 * re-issue the select. We're currently using #2, as it's more
404 * reliable than #1 and generally better than #3. Wastes two fds.
405 */
Sebastien Hertz7d955652014-10-22 10:57:10 +0200406 selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700407 if (selCount < 0) {
408 if (errno == EINTR) {
409 continue;
410 }
411 PLOG(ERROR) << "select failed";
412 goto fail;
413 }
414
Elliott Hughes5d10a872013-04-17 19:26:43 -0700415 if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) {
416 if (listenSock >= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417 LOG(ERROR) << "Exit wake set, but not exiting?";
418 } else {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700419 VLOG(jdwp) << "Got wake-up signal, bailing out of select";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420 }
421 goto fail;
422 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700423 if (listenSock >= 0 && FD_ISSET(listenSock, &readfds)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424 LOG(INFO) << "Ignoring second debugger -- accepting and dropping";
425 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700426 sockaddr_in addrInet;
427 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428 } addr;
429 socklen_t addrlen;
430 int tmpSock;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700431 tmpSock = accept(listenSock, &addr.addrPlain, &addrlen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700432 if (tmpSock < 0) {
433 LOG(INFO) << "Weird -- accept failed";
434 } else {
435 close(tmpSock);
436 }
437 }
Elliott Hughes5d10a872013-04-17 19:26:43 -0700438 if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) {
439 readCount = read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 if (readCount < 0) {
441 /* read failed */
442 if (errno != EINTR) {
443 goto fail;
444 }
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700445 VLOG(jdwp) << "+++ EINTR hit";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700446 return true;
447 } else if (readCount == 0) {
448 /* EOF hit -- far end went away */
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700449 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700450 goto fail;
451 } else {
452 break;
453 }
454 }
455 }
456
Elliott Hughes5d10a872013-04-17 19:26:43 -0700457 input_count_ += readCount;
458 if (!HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700459 return true; /* still not there yet */
460 }
461 }
462
463 /*
464 * Special-case the initial handshake. For some bizarre reason we're
465 * expected to emulate bad tty settings by echoing the request back
466 * exactly as it was sent. Note the handshake is always initiated by
467 * the debugger, no matter who connects to whom.
468 *
469 * Other than this one case, the protocol [claims to be] stateless.
470 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700471 if (IsAwaitingHandshake()) {
472 if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) {
473 LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474 goto fail;
475 }
476
477 errno = 0;
Elliott Hughes5d10a872013-04-17 19:26:43 -0700478 int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 if (cc != kMagicHandshakeLen) {
480 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
481 goto fail;
482 }
483
Elliott Hughes5d10a872013-04-17 19:26:43 -0700484 ConsumeBytes(kMagicHandshakeLen);
485 SetAwaitingHandshake(false);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800486 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700487 return true;
488 }
489
490 /*
491 * Handle this packet.
492 */
Elliott Hughes5d10a872013-04-17 19:26:43 -0700493 return state_->HandlePacket();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700494
Elliott Hughesa21039c2012-06-21 12:09:25 -0700495 fail:
Elliott Hughes5d10a872013-04-17 19:26:43 -0700496 Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497 return false;
498}
499
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500} // namespace JDWP
501
502} // namespace art