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 | */ |
| 16 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/un.h> |
| 21 | #include <unistd.h> |
| 22 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| 24 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include "base/logging.h" // For VLOG. |
Elliott Hughes | c2efd4d | 2018-10-25 13:14:55 -0700 | [diff] [blame] | 26 | #include "base/socket_peer_is_trusted.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 27 | #include "jdwp/jdwp_priv.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 28 | #include "thread-current-inl.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 30 | /* |
| 31 | * The JDWP <-> ADB transport protocol is explained in detail |
| 32 | * in system/core/adb/jdwp_service.c. Here's a summary. |
| 33 | * |
| 34 | * 1/ when the JDWP thread starts, it tries to connect to a Unix |
| 35 | * domain stream socket (@jdwp-control) that is opened by the |
| 36 | * ADB daemon. |
| 37 | * |
Josh Gao | 4b49bb7 | 2018-02-12 15:06:42 -0800 | [diff] [blame] | 38 | * 2/ it then sends the current process PID as an int32_t. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 39 | * |
| 40 | * 3/ then, it uses recvmsg to receive file descriptors from the |
| 41 | * daemon. each incoming file descriptor is a pass-through to |
| 42 | * a given JDWP debugger, that can be used to read the usual |
| 43 | * JDWP-handshake, etc... |
| 44 | */ |
| 45 | |
Tao Wu | 48fe794 | 2017-01-08 01:20:21 -0800 | [diff] [blame] | 46 | static constexpr char kJdwpControlName[] = "\0jdwp-control"; |
| 47 | static constexpr size_t kJdwpControlNameLen = sizeof(kJdwpControlName) - 1; |
| 48 | /* This timeout is for connect/send with control socket. In practice, the |
| 49 | * connect should never timeout since it's just connect to a local unix domain |
| 50 | * socket. But in case adb is buggy and doesn't respond to any connection, the |
| 51 | * connect will block. For send, actually it would never block since we only send |
| 52 | * several bytes and the kernel buffer is big enough to accept it. 10 seconds |
| 53 | * should be far enough. |
| 54 | */ |
| 55 | static constexpr int kControlSockSendTimeout = 10; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 56 | |
| 57 | namespace art { |
| 58 | |
| 59 | namespace JDWP { |
| 60 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 61 | using android::base::StringPrintf; |
| 62 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 63 | struct JdwpAdbState : public JdwpNetStateBase { |
| 64 | public: |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 65 | explicit JdwpAdbState(JdwpState* state) |
| 66 | : JdwpNetStateBase(state), |
| 67 | state_lock_("JdwpAdbState lock", kJdwpAdbStateLock) { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 68 | control_sock_ = -1; |
| 69 | shutting_down_ = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 71 | control_addr_.controlAddrUn.sun_family = AF_UNIX; |
| 72 | control_addr_len_ = sizeof(control_addr_.controlAddrUn.sun_family) + kJdwpControlNameLen; |
| 73 | memcpy(control_addr_.controlAddrUn.sun_path, kJdwpControlName, kJdwpControlNameLen); |
| 74 | } |
| 75 | |
| 76 | ~JdwpAdbState() { |
| 77 | if (clientSock != -1) { |
| 78 | shutdown(clientSock, SHUT_RDWR); |
| 79 | close(clientSock); |
| 80 | } |
| 81 | if (control_sock_ != -1) { |
| 82 | shutdown(control_sock_, SHUT_RDWR); |
| 83 | close(control_sock_); |
| 84 | } |
| 85 | } |
| 86 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 87 | bool Accept() override REQUIRES(!state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 89 | bool Establish(const JdwpOptions*) override { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 93 | void Shutdown() override REQUIRES(!state_lock_) { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 94 | int control_sock; |
| 95 | int local_clientSock; |
| 96 | { |
| 97 | MutexLock mu(Thread::Current(), state_lock_); |
| 98 | shutting_down_ = true; |
| 99 | control_sock = this->control_sock_; |
| 100 | local_clientSock = this->clientSock; |
| 101 | /* clear these out so it doesn't wake up and try to reuse them */ |
| 102 | this->control_sock_ = this->clientSock = -1; |
| 103 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 104 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 105 | if (local_clientSock != -1) { |
| 106 | shutdown(local_clientSock, SHUT_RDWR); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (control_sock != -1) { |
| 110 | shutdown(control_sock, SHUT_RDWR); |
| 111 | } |
| 112 | |
| 113 | WakePipe(); |
| 114 | } |
| 115 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 116 | bool ProcessIncoming() override REQUIRES(!state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 117 | |
| 118 | private: |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 119 | int ReceiveClientFd() REQUIRES(!state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 120 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 121 | bool IsDown() REQUIRES(!state_lock_) { |
| 122 | MutexLock mu(Thread::Current(), state_lock_); |
| 123 | return shutting_down_; |
| 124 | } |
| 125 | |
| 126 | int ControlSock() REQUIRES(!state_lock_) { |
| 127 | MutexLock mu(Thread::Current(), state_lock_); |
| 128 | if (shutting_down_) { |
| 129 | CHECK_EQ(control_sock_, -1); |
| 130 | } |
| 131 | return control_sock_; |
| 132 | } |
| 133 | |
| 134 | int control_sock_ GUARDED_BY(state_lock_); |
| 135 | bool shutting_down_ GUARDED_BY(state_lock_); |
| 136 | Mutex state_lock_; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 137 | |
| 138 | socklen_t control_addr_len_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 139 | union { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 140 | sockaddr_un controlAddrUn; |
| 141 | sockaddr controlAddrPlain; |
| 142 | } control_addr_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Do initial prep work, e.g. binding to ports and opening files. This |
| 147 | * runs in the main thread, before the JDWP thread starts, so it shouldn't |
| 148 | * do anything that might block forever. |
| 149 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 150 | bool InitAdbTransport(JdwpState* state, const JdwpOptions*) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 151 | VLOG(jdwp) << "ADB transport startup"; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 152 | state->netState = new JdwpAdbState(state); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 153 | return (state->netState != nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Receive a file descriptor from ADB. The fd can be used to communicate |
| 158 | * directly with a debugger or DDMS. |
| 159 | * |
| 160 | * Returns the file descriptor on success. On failure, returns -1 and |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 161 | * closes netState->control_sock_. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 162 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 163 | int JdwpAdbState::ReceiveClientFd() { |
| 164 | char dummy = '!'; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 165 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 166 | cmsghdr cm; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 167 | char buffer[CMSG_SPACE(sizeof(int))]; |
| 168 | } cm_un; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 169 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 170 | iovec iov; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 171 | iov.iov_base = &dummy; |
| 172 | iov.iov_len = 1; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 173 | |
| 174 | msghdr msg; |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 175 | msg.msg_name = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 176 | msg.msg_namelen = 0; |
| 177 | msg.msg_iov = &iov; |
| 178 | msg.msg_iovlen = 1; |
| 179 | msg.msg_flags = 0; |
| 180 | msg.msg_control = cm_un.buffer; |
| 181 | msg.msg_controllen = sizeof(cm_un.buffer); |
| 182 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 183 | cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 184 | cmsg->cmsg_len = msg.msg_controllen; |
| 185 | cmsg->cmsg_level = SOL_SOCKET; |
| 186 | cmsg->cmsg_type = SCM_RIGHTS; |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 187 | (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0] = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 188 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 189 | int rc = TEMP_FAILURE_RETRY(recvmsg(ControlSock(), &msg, 0)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 190 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 191 | if (rc <= 0) { |
| 192 | if (rc == -1) { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 193 | PLOG(WARNING) << "Receiving file descriptor from ADB failed (socket " << ControlSock() << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 194 | } |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 195 | MutexLock mu(Thread::Current(), state_lock_); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 196 | close(control_sock_); |
| 197 | control_sock_ = -1; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 198 | return -1; |
| 199 | } |
| 200 | |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 201 | return (reinterpret_cast<int*>(CMSG_DATA(cmsg)))[0]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Block forever, waiting for a debugger to connect to us. Called from the |
| 206 | * JDWP thread. |
| 207 | * |
| 208 | * This needs to un-block and return "false" if the VM is shutting down. It |
| 209 | * should return "true" when it successfully accepts a connection. |
| 210 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 211 | bool JdwpAdbState::Accept() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 212 | int retryCount = 0; |
| 213 | |
| 214 | /* first, ensure that we get a connection to the ADB daemon */ |
| 215 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 216 | retry: |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 217 | if (IsDown()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 221 | if (ControlSock() == -1) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 222 | int sleep_ms = 500; |
| 223 | const int sleep_max_ms = 2*1000; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 224 | |
Josh Gao | cbb65ae | 2017-03-20 11:33:34 -0700 | [diff] [blame] | 225 | int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 226 | if (sock < 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 227 | PLOG(ERROR) << "Could not create ADB control socket"; |
| 228 | return false; |
| 229 | } |
Tao Wu | 48fe794 | 2017-01-08 01:20:21 -0800 | [diff] [blame] | 230 | struct timeval timeout; |
| 231 | timeout.tv_sec = kControlSockSendTimeout; |
| 232 | timeout.tv_usec = 0; |
| 233 | setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 234 | { |
| 235 | MutexLock mu(Thread::Current(), state_lock_); |
| 236 | control_sock_ = sock; |
| 237 | if (shutting_down_) { |
| 238 | return false; |
| 239 | } |
| 240 | if (!MakePipe()) { |
| 241 | return false; |
| 242 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Josh Gao | 4b49bb7 | 2018-02-12 15:06:42 -0800 | [diff] [blame] | 245 | int32_t pid = getpid(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 246 | |
| 247 | for (;;) { |
| 248 | /* |
| 249 | * If adbd isn't running, because USB debugging was disabled or |
| 250 | * perhaps the system is restarting it for "adb root", the |
| 251 | * connect() will fail. We loop here forever waiting for it |
| 252 | * to come back. |
| 253 | * |
| 254 | * Waking up and polling every couple of seconds is generally a |
| 255 | * bad thing to do, but we only do this if the application is |
| 256 | * debuggable *and* adbd isn't running. Still, for the sake |
| 257 | * of battery life, we should consider timing out and giving |
| 258 | * up after a few minutes in case somebody ships an app with |
| 259 | * the debuggable flag set. |
| 260 | */ |
Josh Gao | cbb65ae | 2017-03-20 11:33:34 -0700 | [diff] [blame] | 261 | int ret = connect(ControlSock(), &control_addr_.controlAddrPlain, control_addr_len_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 262 | if (!ret) { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 263 | int control_sock = ControlSock(); |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 264 | #ifdef ART_TARGET_ANDROID |
Elliott Hughes | c2efd4d | 2018-10-25 13:14:55 -0700 | [diff] [blame] | 265 | if (control_sock < 0 || !art::SocketPeerIsTrusted(control_sock)) { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 266 | if (control_sock >= 0 && shutdown(control_sock, SHUT_RDWR)) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 267 | PLOG(ERROR) << "trouble shutting down socket"; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | /* now try to send our pid to the ADB daemon */ |
Josh Gao | 4b49bb7 | 2018-02-12 15:06:42 -0800 | [diff] [blame] | 274 | ret = TEMP_FAILURE_RETRY(send(control_sock, &pid, sizeof(pid), 0)); |
| 275 | if (ret == sizeof(pid)) { |
| 276 | VLOG(jdwp) << "PID " << pid << " sent to ADB"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | |
| 280 | PLOG(ERROR) << "Weird, can't send JDWP process pid to ADB"; |
| 281 | return false; |
| 282 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 283 | if (VLOG_IS_ON(jdwp)) { |
| 284 | PLOG(ERROR) << "Can't connect to ADB control socket"; |
| 285 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 286 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 287 | usleep(sleep_ms * 1000); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 288 | |
| 289 | sleep_ms += (sleep_ms >> 1); |
| 290 | if (sleep_ms > sleep_max_ms) { |
| 291 | sleep_ms = sleep_max_ms; |
| 292 | } |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 293 | if (IsDown()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 299 | VLOG(jdwp) << "trying to receive file descriptor from ADB"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 300 | /* now we can receive a client file descriptor */ |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 301 | int sock = ReceiveClientFd(); |
| 302 | { |
| 303 | MutexLock mu(Thread::Current(), state_lock_); |
| 304 | clientSock = sock; |
| 305 | if (shutting_down_) { |
| 306 | return false; // suppress logs and additional activity |
| 307 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 308 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 309 | if (clientSock == -1) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 310 | if (++retryCount > 5) { |
| 311 | LOG(ERROR) << "adb connection max retries exceeded"; |
| 312 | return false; |
| 313 | } |
| 314 | goto retry; |
| 315 | } else { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 316 | VLOG(jdwp) << "received file descriptor " << clientSock << " from ADB"; |
| 317 | SetAwaitingHandshake(true); |
| 318 | input_count_ = 0; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 324 | * Process incoming data. If no data is available, this will block until |
| 325 | * some arrives. |
| 326 | * |
| 327 | * If we get a full packet, handle it. |
| 328 | * |
| 329 | * To take some of the mystery out of life, we want to reject incoming |
| 330 | * connections if we already have a debugger attached. If we don't, the |
| 331 | * debugger will just mysteriously hang until it times out. We could just |
| 332 | * close the listen socket, but there's a good chance we won't be able to |
| 333 | * bind to the same port again, which would confuse utilities. |
| 334 | * |
| 335 | * Returns "false" on error (indicating that the connection has been severed), |
| 336 | * "true" if things are still okay. |
| 337 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 338 | bool JdwpAdbState::ProcessIncoming() { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 339 | int readCount; |
| 340 | |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 341 | CHECK_NE(clientSock, -1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 342 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 343 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 344 | /* read some more, looping until we have data */ |
| 345 | errno = 0; |
Andreas Gampe | 8351aac | 2018-09-10 12:37:49 -0700 | [diff] [blame] | 346 | while (true) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 347 | int selCount; |
| 348 | fd_set readfds; |
| 349 | int maxfd = -1; |
| 350 | int fd; |
| 351 | |
| 352 | FD_ZERO(&readfds); |
| 353 | |
| 354 | /* configure fds; note these may get zapped by another thread */ |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 355 | fd = ControlSock(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 356 | if (fd >= 0) { |
| 357 | FD_SET(fd, &readfds); |
| 358 | if (maxfd < fd) { |
| 359 | maxfd = fd; |
| 360 | } |
| 361 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 362 | fd = clientSock; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 363 | if (fd >= 0) { |
| 364 | FD_SET(fd, &readfds); |
| 365 | if (maxfd < fd) { |
| 366 | maxfd = fd; |
| 367 | } |
| 368 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 369 | fd = wake_pipe_[0]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 370 | if (fd >= 0) { |
| 371 | FD_SET(fd, &readfds); |
| 372 | if (maxfd < fd) { |
| 373 | maxfd = fd; |
| 374 | } |
| 375 | } else { |
| 376 | LOG(INFO) << "NOTE: entering select w/o wakepipe"; |
| 377 | } |
| 378 | |
| 379 | if (maxfd < 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 380 | VLOG(jdwp) << "+++ all fds are closed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Select blocks until it sees activity on the file descriptors. |
| 386 | * Closing the local file descriptor does not count as activity, |
| 387 | * so we can't rely on that to wake us up (it works for read() |
| 388 | * and accept(), but not select()). |
| 389 | * |
| 390 | * 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] | 391 | * EINTR, (2) open an additional fd ("wake pipe") and write to |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 392 | * it when it's time to exit, or (3) time out periodically and |
| 393 | * re-issue the select. We're currently using #2, as it's more |
| 394 | * reliable than #1 and generally better than #3. Wastes two fds. |
| 395 | */ |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 396 | selCount = select(maxfd + 1, &readfds, nullptr, nullptr, nullptr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 397 | if (selCount < 0) { |
| 398 | if (errno == EINTR) { |
| 399 | continue; |
| 400 | } |
| 401 | PLOG(ERROR) << "select failed"; |
| 402 | goto fail; |
| 403 | } |
| 404 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 405 | if (wake_pipe_[0] >= 0 && FD_ISSET(wake_pipe_[0], &readfds)) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 406 | VLOG(jdwp) << "Got wake-up signal, bailing out of select"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 407 | goto fail; |
| 408 | } |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 409 | int control_sock = ControlSock(); |
| 410 | if (control_sock >= 0 && FD_ISSET(control_sock, &readfds)) { |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 411 | int sock = ReceiveClientFd(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 412 | if (sock >= 0) { |
| 413 | LOG(INFO) << "Ignoring second debugger -- accepting and dropping"; |
| 414 | close(sock); |
| 415 | } else { |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 416 | CHECK_EQ(ControlSock(), -1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 417 | /* |
| 418 | * Remote side most likely went away, so our next read |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 419 | * on clientSock will fail and throw us out of the loop. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 420 | */ |
| 421 | } |
| 422 | } |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 423 | if (clientSock >= 0 && FD_ISSET(clientSock, &readfds)) { |
| 424 | readCount = read(clientSock, input_buffer_ + input_count_, sizeof(input_buffer_) - input_count_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 425 | if (readCount < 0) { |
| 426 | /* read failed */ |
| 427 | if (errno != EINTR) { |
| 428 | goto fail; |
| 429 | } |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 430 | VLOG(jdwp) << "+++ EINTR hit"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 431 | return true; |
| 432 | } else if (readCount == 0) { |
| 433 | /* EOF hit -- far end went away */ |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 434 | VLOG(jdwp) << "+++ peer disconnected"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 435 | goto fail; |
| 436 | } else { |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 442 | input_count_ += readCount; |
| 443 | if (!HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 444 | return true; /* still not there yet */ |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Special-case the initial handshake. For some bizarre reason we're |
| 450 | * expected to emulate bad tty settings by echoing the request back |
| 451 | * exactly as it was sent. Note the handshake is always initiated by |
| 452 | * the debugger, no matter who connects to whom. |
| 453 | * |
| 454 | * Other than this one case, the protocol [claims to be] stateless. |
| 455 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 456 | if (IsAwaitingHandshake()) { |
| 457 | if (memcmp(input_buffer_, kMagicHandshake, kMagicHandshakeLen) != 0) { |
| 458 | LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", input_buffer_); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 459 | goto fail; |
| 460 | } |
| 461 | |
| 462 | errno = 0; |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 463 | int cc = TEMP_FAILURE_RETRY(write(clientSock, input_buffer_, kMagicHandshakeLen)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 464 | if (cc != kMagicHandshakeLen) { |
| 465 | PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")"; |
| 466 | goto fail; |
| 467 | } |
| 468 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 469 | ConsumeBytes(kMagicHandshakeLen); |
| 470 | SetAwaitingHandshake(false); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 471 | VLOG(jdwp) << "+++ handshake complete"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 472 | return true; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Handle this packet. |
| 477 | */ |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 478 | return state_->HandlePacket(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 479 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 480 | fail: |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 481 | Close(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 482 | return false; |
| 483 | } |
| 484 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 485 | } // namespace JDWP |
| 486 | |
| 487 | } // namespace art |