blob: 5acaaece691ea1854aeeec57224771c5eefdc93c [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 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
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albertb302d122015-02-24 15:51:19 -080018
Dan Albertdb6fe642015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albertb302d122015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert4895c522015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albertb302d122015-02-24 15:51:19 -080023#include <errno.h>
Josh Gaob39e4152017-08-16 16:57:01 -070024#include <inttypes.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080028#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029
Spencer Low28bc2cb2015-11-07 18:51:54 -080030#include <algorithm>
Dan Albertecce5032015-05-18 16:46:31 -070031#include <list>
Josh Gaoe7daf572016-09-21 12:37:10 -070032#include <mutex>
Josh Gao0f3312a2017-04-12 17:00:49 -070033#include <thread>
Dan Albertecce5032015-05-18 16:46:31 -070034
Elliott Hughesf55ead92015-12-04 22:00:26 -080035#include <android-base/logging.h>
David Pursellc929c6f2016-03-01 08:58:26 -080036#include <android-base/parsenetaddress.h>
Yabin Cui3cf1b362017-03-10 16:01:01 -080037#include <android-base/quick_exit.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
Josh Gaob39e4152017-08-16 16:57:01 -070040#include <android-base/thread_annotations.h>
Elliott Hughes88b4c852015-04-30 17:32:03 -070041
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#include "adb.h"
Elliott Hughes801066a2016-06-29 17:42:01 -070043#include "adb_auth.h"
Josh Gao395b86a2018-01-28 20:32:46 -080044#include "adb_io.h"
Josh Gao9e09a972016-11-29 09:40:29 -080045#include "adb_trace.h"
Elliott Hughes88b4c852015-04-30 17:32:03 -070046#include "adb_utils.h"
Elliott Hughes70097492015-12-11 19:07:01 -080047#include "diagnose_usb.h"
Yabin Cui3cf1b362017-03-10 16:01:01 -080048#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080049
50static void transport_unref(atransport *t);
51
Josh Gaob39e4152017-08-16 16:57:01 -070052// TODO: unordered_map<TransportId, atransport*>
Josh Gaoe3a87d02015-11-11 17:56:12 -080053static auto& transport_list = *new std::list<atransport*>();
54static auto& pending_list = *new std::list<atransport*>();
Benoit Goby3f9f9ce2013-03-29 18:22:36 -070055
Josh Gaoaaa82f72017-08-17 13:50:51 -070056static auto& transport_lock = *new std::recursive_mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057
Todd Kennedyaff9c672015-11-10 00:03:25 +000058const char* const kFeatureShell2 = "shell_v2";
59const char* const kFeatureCmd = "cmd";
Josh Gaoa2cf3752016-12-05 17:11:34 -080060const char* const kFeatureStat2 = "stat_v2";
Josh Gao210b63f2017-02-22 17:07:01 -080061const char* const kFeatureLibusb = "libusb";
Dan Albert27983bc2017-05-23 14:30:00 -070062const char* const kFeaturePushSync = "push_sync";
Todd Kennedyaff9c672015-11-10 00:03:25 +000063
Josh Gaob39e4152017-08-16 16:57:01 -070064TransportId NextTransportId() {
65 static std::atomic<TransportId> next(1);
66 return next++;
67}
68
Josh Gao395b86a2018-01-28 20:32:46 -080069bool FdConnection::Read(apacket* packet) {
70 if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) {
71 D("remote local: read terminated (message)");
72 return false;
73 }
74
75 if (!ReadFdExactly(fd_.get(), &packet->data, packet->msg.data_length)) {
76 D("remote local: terminated (data)");
77 return false;
78 }
79
80 return true;
81}
82
83bool FdConnection::Write(apacket* packet) {
84 uint32_t length = packet->msg.data_length;
85
86 if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(amessage) + length)) {
87 D("remote local: write terminated");
88 return false;
89 }
90
91 return true;
92}
93
94void FdConnection::Close() {
95 adb_shutdown(fd_.get());
96 fd_.reset();
97}
98
Yabin Cui19bec5b2015-09-22 15:52:57 -070099static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800100 unsigned command = p->msg.command;
101 int len = p->msg.data_length;
102 char cmd[9];
103 char arg0[12], arg1[12];
104 int n;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100105
106 for (n = 0; n < 4; n++) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800107 int b = (command >> (n * 8)) & 255;
108 if (b < 32 || b >= 127) break;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100109 cmd[n] = (char)b;
110 }
111 if (n == 4) {
112 cmd[4] = 0;
113 } else {
114 /* There is some non-ASCII name in the command, so dump
115 * the hexadecimal value instead */
116 snprintf(cmd, sizeof cmd, "%08x", command);
117 }
118
119 if (p->msg.arg0 < 256U)
120 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
121 else
122 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
123
124 if (p->msg.arg1 < 256U)
125 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
126 else
127 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
128
Josh Gao0d6aa992016-11-22 14:32:34 -0800129 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
130 func, cmd, arg0, arg1, len);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700131 result += dump_hex(p->data, len);
132 return result;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100133}
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100134
Josh Gao0d6aa992016-11-22 14:32:34 -0800135static int read_packet(int fd, const char* name, apacket** ppacket) {
Josh Gao9e09a972016-11-29 09:40:29 -0800136 ATRACE_NAME("read_packet");
Yabin Cuia96dabf2015-07-30 19:58:10 -0700137 char buff[8];
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100138 if (!name) {
139 snprintf(buff, sizeof buff, "fd=%d", fd);
140 name = buff;
141 }
Josh Gao0d6aa992016-11-22 14:32:34 -0800142 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
Yabin Cuia96dabf2015-07-30 19:58:10 -0700143 int len = sizeof(apacket*);
Josh Gao0d6aa992016-11-22 14:32:34 -0800144 while (len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700145 int r = adb_read(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800146 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800147 len -= r;
Yabin Cuia96dabf2015-07-30 19:58:10 -0700148 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800149 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700150 D("%s: read_packet (fd=%d), error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800151 return -1;
152 }
153 }
154
Yabin Cui19bec5b2015-09-22 15:52:57 -0700155 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800156 return 0;
157}
158
Josh Gao0d6aa992016-11-22 14:32:34 -0800159static int write_packet(int fd, const char* name, apacket** ppacket) {
Josh Gao9e09a972016-11-29 09:40:29 -0800160 ATRACE_NAME("write_packet");
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100161 char buff[8];
162 if (!name) {
163 snprintf(buff, sizeof buff, "fd=%d", fd);
164 name = buff;
165 }
Yabin Cui19bec5b2015-09-22 15:52:57 -0700166 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Josh Gao0d6aa992016-11-22 14:32:34 -0800167 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
Yabin Cuia96dabf2015-07-30 19:58:10 -0700168 int len = sizeof(apacket*);
Josh Gao0d6aa992016-11-22 14:32:34 -0800169 while (len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700170 int r = adb_write(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800171 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800172 len -= r;
173 p += r;
174 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700175 D("%s: write_packet (fd=%d) error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800176 return -1;
177 }
178 }
179 return 0;
180}
181
Josh Gao0d6aa992016-11-22 14:32:34 -0800182static void transport_socket_events(int fd, unsigned events, void* _t) {
183 atransport* t = reinterpret_cast<atransport*>(_t);
Yabin Cui815ad882015-09-02 17:44:28 -0700184 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
Josh Gao0d6aa992016-11-22 14:32:34 -0800185 if (events & FDE_READ) {
186 apacket* p = 0;
187 if (read_packet(fd, t->serial, &p)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700188 D("%s: failed to read packet from transport socket on fd %d", t->serial, fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800189 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800190 handle_packet(p, (atransport*)_t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191 }
192 }
193}
194
Josh Gao67ac3792016-10-06 13:31:44 -0700195void send_packet(apacket* p, atransport* t) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayee7b44d2017-12-07 11:40:00 -0800197 // compute a checksum for connection/auth packets for compatibility reasons
198 if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
199 p->msg.data_check = 0;
200 } else {
201 p->msg.data_check = calculate_apacket_checksum(p);
202 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800203
204 print_packet("send", p);
205
206 if (t == NULL) {
Josh Gao67ac3792016-10-06 13:31:44 -0700207 fatal("Transport is null");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208 }
209
Josh Gao67ac3792016-10-06 13:31:44 -0700210 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800211 fatal_errno("cannot enqueue packet on transport socket");
212 }
213}
214
Yabin Cui4e222292015-08-31 11:50:24 -0700215// The transport is opened by transport_register_func before
216// the read_transport and write_transport threads are started.
217//
218// The read_transport thread issues a SYNC(1, token) message to let
219// the write_transport thread know to start things up. In the event
220// of transport IO failure, the read_transport thread will post a
221// SYNC(0,0) message to ensure shutdown.
222//
223// The transport will not actually be closed until both threads exit, but the threads
224// will kick the transport on their way out to disconnect the underlying device.
225//
226// read_transport thread reads data from a transport (representing a usb/tcp connection),
227// and makes the main thread call handle_packet().
Josh Gao7d405252016-02-12 14:31:15 -0800228static void read_transport_thread(void* _t) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800229 atransport* t = reinterpret_cast<atransport*>(_t);
230 apacket* p;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800231
Josh Gao0d6aa992016-11-22 14:32:34 -0800232 adb_thread_setname(
233 android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport")));
234 D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd,
235 t->sync_token + 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236 p = get_apacket();
237 p->msg.command = A_SYNC;
238 p->msg.arg0 = 1;
239 p->msg.arg1 = ++(t->sync_token);
240 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao0d6aa992016-11-22 14:32:34 -0800241 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800242 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700243 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800244 goto oops;
245 }
246
Yabin Cui815ad882015-09-02 17:44:28 -0700247 D("%s: data pump started", t->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800248 for (;;) {
Josh Gao9e09a972016-11-29 09:40:29 -0800249 ATRACE_NAME("read_transport loop");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250 p = get_apacket();
251
Josh Gao9e09a972016-11-29 09:40:29 -0800252 {
253 ATRACE_NAME("read_transport read_remote");
Josh Gao395b86a2018-01-28 20:32:46 -0800254 if (!t->connection->Read(p)) {
Josh Gao9e09a972016-11-29 09:40:29 -0800255 D("%s: remote read failed for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800256 put_apacket(p);
Josh Gao9e09a972016-11-29 09:40:29 -0800257 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 }
Josh Gao395b86a2018-01-28 20:32:46 -0800259
260 if (!check_header(p, t)) {
261 D("%s: remote read: bad header", t->serial);
262 put_apacket(p);
263 break;
264 }
265
Yabin Cui3cf1b362017-03-10 16:01:01 -0800266#if ADB_HOST
267 if (p->msg.command == 0) {
Josh Gao30c42662018-01-29 18:09:20 -0800268 put_apacket(p);
Yabin Cui3cf1b362017-03-10 16:01:01 -0800269 continue;
270 }
271#endif
Josh Gao9e09a972016-11-29 09:40:29 -0800272 }
273
274 D("%s: received remote packet, sending to transport", t->serial);
275 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 put_apacket(p);
Josh Gao9e09a972016-11-29 09:40:29 -0800277 D("%s: failed to write apacket to transport", t->serial);
278 goto oops;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279 }
280 }
281
Yabin Cui815ad882015-09-02 17:44:28 -0700282 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283 p = get_apacket();
284 p->msg.command = A_SYNC;
285 p->msg.arg0 = 0;
286 p->msg.arg1 = 0;
287 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao0d6aa992016-11-22 14:32:34 -0800288 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800289 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700290 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291 }
292
293oops:
Yabin Cui815ad882015-09-02 17:44:28 -0700294 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800295 kick_transport(t);
296 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297}
298
Yabin Cui4e222292015-08-31 11:50:24 -0700299// write_transport thread gets packets sent by the main thread (through send_packet()),
300// and writes to a transport (representing a usb/tcp connection).
Josh Gao7d405252016-02-12 14:31:15 -0800301static void write_transport_thread(void* _t) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800302 atransport* t = reinterpret_cast<atransport*>(_t);
303 apacket* p;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800304 int active = 0;
305
Josh Gao0d6aa992016-11-22 14:32:34 -0800306 adb_thread_setname(
307 android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport")));
308 D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309
Josh Gao0d6aa992016-11-22 14:32:34 -0800310 for (;;) {
Josh Gao9e09a972016-11-29 09:40:29 -0800311 ATRACE_NAME("write_transport loop");
Josh Gao0d6aa992016-11-22 14:32:34 -0800312 if (read_packet(t->fd, t->serial, &p)) {
313 D("%s: failed to read apacket from transport on fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800314 break;
315 }
Josh Gao9e09a972016-11-29 09:40:29 -0800316
Josh Gao0d6aa992016-11-22 14:32:34 -0800317 if (p->msg.command == A_SYNC) {
318 if (p->msg.arg0 == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700319 D("%s: transport SYNC offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800320 put_apacket(p);
321 break;
322 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800323 if (p->msg.arg1 == t->sync_token) {
Yabin Cui815ad882015-09-02 17:44:28 -0700324 D("%s: transport SYNC online", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800325 active = 1;
326 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800327 D("%s: transport ignoring SYNC %d != %d", t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800328 }
329 }
330 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800331 if (active) {
Yabin Cui815ad882015-09-02 17:44:28 -0700332 D("%s: transport got packet, sending to remote", t->serial);
Josh Gao9e09a972016-11-29 09:40:29 -0800333 ATRACE_NAME("write_transport write_remote");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800334 if (t->Write(p) != 0) {
335 D("%s: remote write failed for transport", t->serial);
336 put_apacket(p);
337 break;
338 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800339 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700340 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800341 }
342 }
343
344 put_apacket(p);
345 }
346
Yabin Cui815ad882015-09-02 17:44:28 -0700347 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800348 kick_transport(t);
349 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800350}
351
Yabin Cui4d64fd82015-08-27 12:03:11 -0700352void kick_transport(atransport* t) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700353 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuid78ed222016-04-05 13:50:44 -0700354 // As kick_transport() can be called from threads without guarantee that t is valid,
355 // check if the transport is in transport_list first.
Josh Gaob39e4152017-08-16 16:57:01 -0700356 //
357 // TODO(jmgao): WTF? Is this actually true?
Yabin Cuid78ed222016-04-05 13:50:44 -0700358 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700359 t->Kick();
Yabin Cuid78ed222016-04-05 13:50:44 -0700360 }
Yabin Cui4d64fd82015-08-27 12:03:11 -0700361}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800362
363static int transport_registration_send = -1;
364static int transport_registration_recv = -1;
365static fdevent transport_registration_fde;
366
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800367#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368
369/* this adds support required by the 'track-devices' service.
370 * this is used to send the content of "list_transport" to any
371 * number of client connections that want it through a single
372 * live TCP connection
373 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800374struct device_tracker {
Josh Gao0d6aa992016-11-22 14:32:34 -0800375 asocket socket;
Josh Gao32124632017-08-14 18:57:54 -0700376 bool update_needed;
377 bool long_output;
Josh Gao0d6aa992016-11-22 14:32:34 -0800378 device_tracker* next;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379};
380
381/* linked list of all device trackers */
Josh Gao0d6aa992016-11-22 14:32:34 -0800382static device_tracker* device_tracker_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383
Josh Gao0d6aa992016-11-22 14:32:34 -0800384static void device_tracker_remove(device_tracker* tracker) {
385 device_tracker** pnode = &device_tracker_list;
386 device_tracker* node = *pnode;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800387
Josh Gaoaaa82f72017-08-17 13:50:51 -0700388 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800389 while (node) {
390 if (node == tracker) {
391 *pnode = node->next;
392 break;
393 }
394 pnode = &node->next;
Josh Gao0d6aa992016-11-22 14:32:34 -0800395 node = *pnode;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800397}
398
Josh Gao0d6aa992016-11-22 14:32:34 -0800399static void device_tracker_close(asocket* socket) {
400 device_tracker* tracker = (device_tracker*)socket;
401 asocket* peer = socket->peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402
Josh Gao0d6aa992016-11-22 14:32:34 -0800403 D("device tracker %p removed", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404 if (peer) {
405 peer->peer = NULL;
406 peer->close(peer);
407 }
408 device_tracker_remove(tracker);
409 free(tracker);
410}
411
Josh Gao0d6aa992016-11-22 14:32:34 -0800412static int device_tracker_enqueue(asocket* socket, apacket* p) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800413 /* you can't read from a device tracker, close immediately */
414 put_apacket(p);
415 device_tracker_close(socket);
416 return -1;
417}
418
Elliott Hughes88b4c852015-04-30 17:32:03 -0700419static int device_tracker_send(device_tracker* tracker, const std::string& string) {
420 apacket* p = get_apacket();
421 asocket* peer = tracker->socket.peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800422
Elliott Hughes88b4c852015-04-30 17:32:03 -0700423 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
424 memcpy(&p->data[4], string.data(), string.size());
425 p->len = 4 + string.size();
426 return peer->enqueue(peer, p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427}
428
Elliott Hughes88b4c852015-04-30 17:32:03 -0700429static void device_tracker_ready(asocket* socket) {
430 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800431
Elliott Hughes88b4c852015-04-30 17:32:03 -0700432 // We want to send the device list when the tracker connects
433 // for the first time, even if no update occurred.
Josh Gao32124632017-08-14 18:57:54 -0700434 if (tracker->update_needed) {
435 tracker->update_needed = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800436
Josh Gao32124632017-08-14 18:57:54 -0700437 std::string transports = list_transports(tracker->long_output);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700438 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800439 }
440}
441
Josh Gao32124632017-08-14 18:57:54 -0700442asocket* create_device_tracker(bool long_output) {
Elliott Hughesd0269c92015-04-21 19:39:52 -0700443 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
444 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800445
Josh Gao0d6aa992016-11-22 14:32:34 -0800446 D("device tracker %p created", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800447
448 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao0d6aa992016-11-22 14:32:34 -0800449 tracker->socket.ready = device_tracker_ready;
450 tracker->socket.close = device_tracker_close;
Josh Gao32124632017-08-14 18:57:54 -0700451 tracker->update_needed = true;
452 tracker->long_output = long_output;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800453
Josh Gao0d6aa992016-11-22 14:32:34 -0800454 tracker->next = device_tracker_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800455 device_tracker_list = tracker;
456
457 return &tracker->socket;
458}
459
Josh Gao1e3bf732017-05-03 22:37:10 -0700460// Check if all of the USB transports are connected.
461bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700462 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao1e3bf732017-05-03 22:37:10 -0700463 for (const auto& t : transport_list) {
464 if (!fn(t)) {
465 return false;
466 }
467 }
468 for (const auto& t : pending_list) {
469 if (!fn(t)) {
470 return false;
471 }
472 }
473 return true;
474}
475
Elliott Hughes88b4c852015-04-30 17:32:03 -0700476// Call this function each time the transport list has changed.
477void update_transports() {
Josh Gao1e3bf732017-05-03 22:37:10 -0700478 update_transport_status();
479
480 // Notify `adb track-devices` clients.
Elliott Hughes88b4c852015-04-30 17:32:03 -0700481 std::string transports = list_transports(false);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482
Elliott Hughes88b4c852015-04-30 17:32:03 -0700483 device_tracker* tracker = device_tracker_list;
484 while (tracker != nullptr) {
485 device_tracker* next = tracker->next;
486 // This may destroy the tracker if the connection is closed.
487 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800488 tracker = next;
489 }
490}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700491
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800492#else
Elliott Hughes88b4c852015-04-30 17:32:03 -0700493
494void update_transports() {
495 // Nothing to do on the device side.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800496}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700497
Josh Gao0d6aa992016-11-22 14:32:34 -0800498#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499
Josh Gao0d6aa992016-11-22 14:32:34 -0800500struct tmsg {
501 atransport* transport;
502 int action;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800503};
504
Josh Gao0d6aa992016-11-22 14:32:34 -0800505static int transport_read_action(int fd, struct tmsg* m) {
506 char* p = (char*)m;
507 int len = sizeof(*m);
508 int r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800509
Josh Gao0d6aa992016-11-22 14:32:34 -0800510 while (len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800511 r = adb_read(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800512 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800513 len -= r;
Josh Gao0d6aa992016-11-22 14:32:34 -0800514 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800515 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700516 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800517 return -1;
518 }
519 }
520 return 0;
521}
522
Josh Gao0d6aa992016-11-22 14:32:34 -0800523static int transport_write_action(int fd, struct tmsg* m) {
524 char* p = (char*)m;
525 int len = sizeof(*m);
526 int r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800527
Josh Gao0d6aa992016-11-22 14:32:34 -0800528 while (len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800529 r = adb_write(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800530 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800531 len -= r;
Josh Gao0d6aa992016-11-22 14:32:34 -0800532 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800533 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700534 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800535 return -1;
536 }
537 }
538 return 0;
539}
540
Josh Gao0d6aa992016-11-22 14:32:34 -0800541static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800542 tmsg m;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800543 int s[2];
Josh Gao0d6aa992016-11-22 14:32:34 -0800544 atransport* t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800545
Josh Gao0d6aa992016-11-22 14:32:34 -0800546 if (!(ev & FDE_READ)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800547 return;
548 }
549
Josh Gao0d6aa992016-11-22 14:32:34 -0800550 if (transport_read_action(_fd, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800551 fatal_errno("cannot read transport registration socket");
552 }
553
554 t = m.transport;
555
Dan Albertbe8e54b2015-05-18 13:06:53 -0700556 if (m.action == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700557 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800558
Josh Gao0d6aa992016-11-22 14:32:34 -0800559 /* IMPORTANT: the remove closes one half of the
560 ** socket pair. The close closes the other half.
561 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800562 fdevent_remove(&(t->transport_fde));
563 adb_close(t->fd);
564
Josh Gaoe7daf572016-09-21 12:37:10 -0700565 {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700566 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700567 transport_list.remove(t);
568 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800569
Josh Gao0d6aa992016-11-22 14:32:34 -0800570 if (t->product) free(t->product);
571 if (t->serial) free(t->serial);
572 if (t->model) free(t->model);
573 if (t->device) free(t->device);
574 if (t->devpath) free(t->devpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800575
Dan Albertecce5032015-05-18 16:46:31 -0700576 delete t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800577
578 update_transports();
579 return;
580 }
581
Mike Lockwoode45583f2009-08-08 12:37:44 -0400582 /* don't create transport threads for inaccessible devices */
Yabin Cui3cf1b362017-03-10 16:01:01 -0800583 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800584 /* initial references are the two threads */
Mike Lockwoode45583f2009-08-08 12:37:44 -0400585 t->ref_count = 2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800586
Dan Albertecce5032015-05-18 16:46:31 -0700587 if (adb_socketpair(s)) {
Mike Lockwoode45583f2009-08-08 12:37:44 -0400588 fatal_errno("cannot open transport socketpair");
589 }
590
Yabin Cui815ad882015-09-02 17:44:28 -0700591 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400592
593 t->transport_socket = s[0];
594 t->fd = s[1];
595
Josh Gao0d6aa992016-11-22 14:32:34 -0800596 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400597
598 fdevent_set(&(t->transport_fde), FDE_READ);
599
Josh Gao0f3312a2017-04-12 17:00:49 -0700600 std::thread(write_transport_thread, t).detach();
601 std::thread(read_transport_thread, t).detach();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800602 }
603
Josh Gaoe7daf572016-09-21 12:37:10 -0700604 {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700605 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700606 pending_list.remove(t);
607 transport_list.push_front(t);
608 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800609
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800610 update_transports();
611}
612
Josh Gao0d6aa992016-11-22 14:32:34 -0800613void init_transport_registration(void) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800614 int s[2];
615
Josh Gao0d6aa992016-11-22 14:32:34 -0800616 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800617 fatal_errno("cannot open transport registration socketpair");
618 }
Yabin Cui815ad882015-09-02 17:44:28 -0700619 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800620
621 transport_registration_send = s[0];
622 transport_registration_recv = s[1];
623
Josh Gao0d6aa992016-11-22 14:32:34 -0800624 fdevent_install(&transport_registration_fde, transport_registration_recv,
625 transport_registration_func, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800626
627 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao165460f2017-05-09 13:43:35 -0700628}
629
630void kick_all_transports() {
631 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gaoaaa82f72017-08-17 13:50:51 -0700632 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao165460f2017-05-09 13:43:35 -0700633 for (auto t : transport_list) {
634 t->Kick();
635 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800636}
637
638/* the fdevent select pump is single threaded */
Josh Gao0d6aa992016-11-22 14:32:34 -0800639static void register_transport(atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800640 tmsg m;
641 m.transport = transport;
642 m.action = 1;
Yabin Cui815ad882015-09-02 17:44:28 -0700643 D("transport: %s registered", transport->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800644 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800645 fatal_errno("cannot write transport registration socket\n");
646 }
647}
648
Josh Gao0d6aa992016-11-22 14:32:34 -0800649static void remove_transport(atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800650 tmsg m;
651 m.transport = transport;
652 m.action = 0;
Yabin Cui815ad882015-09-02 17:44:28 -0700653 D("transport: %s removed", transport->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800654 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800655 fatal_errno("cannot write transport registration socket\n");
656 }
657}
658
Yabin Cui4d64fd82015-08-27 12:03:11 -0700659static void transport_unref(atransport* t) {
660 CHECK(t != nullptr);
Josh Gaoe7daf572016-09-21 12:37:10 -0700661
Josh Gao4d299742017-09-13 13:40:57 -0700662 std::lock_guard<std::recursive_mutex> lock(transport_lock);
663 CHECK_GT(t->ref_count, 0u);
664 t->ref_count--;
665 if (t->ref_count == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700666 D("transport: %s unref (kicking and closing)", t->serial);
Josh Gao395b86a2018-01-28 20:32:46 -0800667 t->connection->Close();
Mike Lockwood01c2c302010-05-24 10:44:35 -0400668 remove_transport(t);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100669 } else {
Josh Gao4d299742017-09-13 13:40:57 -0700670 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400671 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800672}
673
Josh Gao0d6aa992016-11-22 14:32:34 -0800674static int qual_match(const char* to_test, const char* prefix, const char* qual,
675 bool sanitize_qual) {
676 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson27042382012-05-30 18:11:27 -0700677 return !qual || !*qual;
678
Josh Gao0d6aa992016-11-22 14:32:34 -0800679 if (!qual) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700680
681 if (prefix) {
682 while (*prefix) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800683 if (*prefix++ != *to_test++) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700684 }
685 }
686
687 while (*qual) {
688 char ch = *qual++;
Josh Gao0d6aa992016-11-22 14:32:34 -0800689 if (sanitize_qual && !isalnum(ch)) ch = '_';
690 if (ch != *to_test++) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700691 }
692
693 /* Everything matched so far. Return true if *to_test is a NUL. */
694 return !*to_test;
695}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800696
Josh Gaob39e4152017-08-16 16:57:01 -0700697atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
698 bool* is_ambiguous, std::string* error_out,
699 bool accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700700 atransport* result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800701
Josh Gaob39e4152017-08-16 16:57:01 -0700702 if (transport_id != 0) {
703 *error_out =
704 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
705 } else if (serial) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700706 *error_out = android::base::StringPrintf("device '%s' not found", serial);
707 } else if (type == kTransportLocal) {
708 *error_out = "no emulators found";
709 } else if (type == kTransportAny) {
710 *error_out = "no devices/emulators found";
711 } else {
712 *error_out = "no devices found";
713 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800714
Josh Gaoaaa82f72017-08-17 13:50:51 -0700715 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes67943d12015-10-07 14:55:10 -0700716 for (const auto& t : transport_list) {
Yabin Cui3cf1b362017-03-10 16:01:01 -0800717 if (t->GetConnectionState() == kCsNoPerm) {
Elliott Hughes70097492015-12-11 19:07:01 -0800718#if ADB_HOST
David Pursell6e5c7eb2015-12-02 15:14:31 -0800719 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes70097492015-12-11 19:07:01 -0800720#endif
Mike Lockwoodadc16b32009-08-08 13:53:16 -0400721 continue;
722 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400723
Josh Gaob39e4152017-08-16 16:57:01 -0700724 if (transport_id) {
725 if (t->id == transport_id) {
726 result = t;
727 break;
728 }
729 } else if (serial) {
David Pursellc929c6f2016-03-01 08:58:26 -0800730 if (t->MatchesTarget(serial)) {
Scott Anderson27042382012-05-30 18:11:27 -0700731 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700732 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700733 if (is_ambiguous) *is_ambiguous = true;
734 result = nullptr;
Scott Anderson27042382012-05-30 18:11:27 -0700735 break;
736 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800737 result = t;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700738 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800739 } else {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700740 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800741 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700742 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700743 if (is_ambiguous) *is_ambiguous = true;
744 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800745 break;
746 }
747 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700748 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800749 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700750 *error_out = "more than one emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700751 if (is_ambiguous) *is_ambiguous = true;
752 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800753 break;
754 }
755 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700756 } else if (type == kTransportAny) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800757 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700758 *error_out = "more than one device/emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700759 if (is_ambiguous) *is_ambiguous = true;
760 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800761 break;
762 }
763 result = t;
764 }
765 }
766 }
Josh Gaoe7daf572016-09-21 12:37:10 -0700767 lock.unlock();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800768
Elliott Hughes67943d12015-10-07 14:55:10 -0700769 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cui3cf1b362017-03-10 16:01:01 -0800770 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700771 *error_out = "device unauthorized.\n";
772 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
773 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
774 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
775 *error_out += "\n";
776 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
777 *error_out += "Otherwise check for a confirmation dialog on your device.";
778 result = nullptr;
779 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700780
Elliott Hughes67943d12015-10-07 14:55:10 -0700781 // Don't return offline devices; the caller can't do anything with them.
Yabin Cui3cf1b362017-03-10 16:01:01 -0800782 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700783 *error_out = "device offline";
784 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800785 }
786
787 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700788 *error_out = "success";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800789 }
790
791 return result;
792}
793
Yabin Cui3cf1b362017-03-10 16:01:01 -0800794int atransport::Write(apacket* p) {
Josh Gao395b86a2018-01-28 20:32:46 -0800795 return this->connection->Write(p) ? 0 : -1;
Yabin Cui3cf1b362017-03-10 16:01:01 -0800796}
797
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700798void atransport::Kick() {
799 if (!kicked_) {
Josh Gao395b86a2018-01-28 20:32:46 -0800800 D("kicking transport %s", this->serial);
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700801 kicked_ = true;
Josh Gao395b86a2018-01-28 20:32:46 -0800802 this->connection->Close();
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700803 }
804}
805
Yabin Cui3cf1b362017-03-10 16:01:01 -0800806ConnectionState atransport::GetConnectionState() const {
807 return connection_state_;
808}
809
810void atransport::SetConnectionState(ConnectionState state) {
811 check_main_thread();
812 connection_state_ = state;
813}
814
David Pursell6e5c7eb2015-12-02 15:14:31 -0800815const std::string atransport::connection_state_name() const {
Yabin Cui3cf1b362017-03-10 16:01:01 -0800816 ConnectionState state = GetConnectionState();
817 switch (state) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800818 case kCsOffline:
819 return "offline";
820 case kCsBootloader:
821 return "bootloader";
822 case kCsDevice:
823 return "device";
824 case kCsHost:
825 return "host";
826 case kCsRecovery:
827 return "recovery";
828 case kCsNoPerm:
829 return UsbNoPermissionsShortHelpText();
830 case kCsSideload:
831 return "sideload";
832 case kCsUnauthorized:
833 return "unauthorized";
834 default:
835 return "unknown";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800836 }
837}
838
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100839void atransport::update_version(int version, size_t payload) {
840 protocol_version = std::min(version, A_VERSION);
841 max_payload = std::min(payload, MAX_PAYLOAD);
842}
843
844int atransport::get_protocol_version() const {
845 return protocol_version;
846}
847
848size_t atransport::get_max_payload() const {
849 return max_payload;
850}
851
David Pursella07dbad2015-09-22 10:43:08 -0700852namespace {
David Pursell8da19a42015-08-31 10:42:13 -0700853
David Pursella07dbad2015-09-22 10:43:08 -0700854constexpr char kFeatureStringDelimiter = ',';
855
856} // namespace
Dan Albertbe8e54b2015-05-18 13:06:53 -0700857
858const FeatureSet& supported_features() {
David Pursella07dbad2015-09-22 10:43:08 -0700859 // Local static allocation to avoid global non-POD variables.
860 static const FeatureSet* features = new FeatureSet{
Josh Gao0d6aa992016-11-22 14:32:34 -0800861 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursell6d5fad32015-09-25 08:37:13 -0700862 // Increment ADB_SERVER_VERSION whenever the feature list changes to
863 // make sure that the adb client and server features stay in sync
864 // (http://b/24370690).
David Pursella07dbad2015-09-22 10:43:08 -0700865 };
866
867 return *features;
868}
869
870std::string FeatureSetToString(const FeatureSet& features) {
871 return android::base::Join(features, kFeatureStringDelimiter);
872}
873
874FeatureSet StringToFeatureSet(const std::string& features_string) {
David Pursell3d9072b2015-09-25 13:04:21 -0700875 if (features_string.empty()) {
876 return FeatureSet();
877 }
878
Josh Gao0d6aa992016-11-22 14:32:34 -0800879 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursella07dbad2015-09-22 10:43:08 -0700880 return FeatureSet(names.begin(), names.end());
Dan Albertbe8e54b2015-05-18 13:06:53 -0700881}
882
David Pursell22fc5e92015-09-30 13:35:42 -0700883bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800884 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell22fc5e92015-09-30 13:35:42 -0700885}
886
Dan Albertbe8e54b2015-05-18 13:06:53 -0700887bool atransport::has_feature(const std::string& feature) const {
888 return features_.count(feature) > 0;
889}
890
David Pursella07dbad2015-09-22 10:43:08 -0700891void atransport::SetFeatures(const std::string& features_string) {
892 features_ = StringToFeatureSet(features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700893}
894
Yabin Cui2d4c1982015-08-28 15:09:44 -0700895void atransport::AddDisconnect(adisconnect* disconnect) {
896 disconnects_.push_back(disconnect);
897}
898
899void atransport::RemoveDisconnect(adisconnect* disconnect) {
900 disconnects_.remove(disconnect);
901}
902
903void atransport::RunDisconnects() {
Elliott Hughes85952832015-10-07 15:59:35 -0700904 for (const auto& disconnect : disconnects_) {
Yabin Cui2d4c1982015-08-28 15:09:44 -0700905 disconnect->func(disconnect->opaque, this);
906 }
907 disconnects_.clear();
908}
909
David Pursellc929c6f2016-03-01 08:58:26 -0800910bool atransport::MatchesTarget(const std::string& target) const {
911 if (serial) {
912 if (target == serial) {
913 return true;
914 } else if (type == kTransportLocal) {
915 // Local transports can match [tcp:|udp:]<hostname>[:port].
916 const char* local_target_ptr = target.c_str();
917
918 // For fastboot compatibility, ignore protocol prefixes.
919 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao0d6aa992016-11-22 14:32:34 -0800920 android::base::StartsWith(target, "udp:")) {
David Pursellc929c6f2016-03-01 08:58:26 -0800921 local_target_ptr += 4;
922 }
923
924 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
925 std::string serial_host, error;
926 int serial_port = -1;
Josh Gao0d6aa992016-11-22 14:32:34 -0800927 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursellc929c6f2016-03-01 08:58:26 -0800928 // |target| may omit the port to default to ours.
929 std::string target_host;
930 int target_port = serial_port;
931 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
932 nullptr, &error) &&
Josh Gao0d6aa992016-11-22 14:32:34 -0800933 serial_host == target_host && serial_port == target_port) {
David Pursellc929c6f2016-03-01 08:58:26 -0800934 return true;
935 }
936 }
937 }
938 }
939
940 return (devpath && target == devpath) ||
941 qual_match(target.c_str(), "product:", product, false) ||
942 qual_match(target.c_str(), "model:", model, true) ||
943 qual_match(target.c_str(), "device:", device, false);
944}
945
Elliott Hughes88b4c852015-04-30 17:32:03 -0700946#if ADB_HOST
947
Josh Gaob39e4152017-08-16 16:57:01 -0700948// We use newline as our delimiter, make sure to never output it.
949static std::string sanitize(std::string str, bool alphanumeric) {
950 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
951 : [](const char c) { return c == '\n'; };
952 std::replace_if(str.begin(), str.end(), pred, '_');
953 return str;
954}
955
Josh Gao0d6aa992016-11-22 14:32:34 -0800956static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob39e4152017-08-16 16:57:01 -0700957 bool alphanumeric) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700958 if (value == nullptr || *value == '\0') {
Scott Anderson27042382012-05-30 18:11:27 -0700959 return;
Scott Anderson27042382012-05-30 18:11:27 -0700960 }
961
Elliott Hughes88b4c852015-04-30 17:32:03 -0700962 *result += ' ';
963 *result += key;
Josh Gaob39e4152017-08-16 16:57:01 -0700964 *result += sanitize(value, alphanumeric);
Scott Anderson27042382012-05-30 18:11:27 -0700965}
966
Josh Gao0d6aa992016-11-22 14:32:34 -0800967static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson27042382012-05-30 18:11:27 -0700968 const char* serial = t->serial;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700969 if (!serial || !serial[0]) {
Dan Alberta8c34142015-05-06 16:48:52 -0700970 serial = "(no serial number)";
Elliott Hughes88b4c852015-04-30 17:32:03 -0700971 }
Scott Anderson27042382012-05-30 18:11:27 -0700972
973 if (!long_listing) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700974 *result += serial;
975 *result += '\t';
976 *result += t->connection_state_name();
Scott Anderson27042382012-05-30 18:11:27 -0700977 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800978 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson27042382012-05-30 18:11:27 -0700979
Elliott Hughes88b4c852015-04-30 17:32:03 -0700980 append_transport_info(result, "", t->devpath, false);
981 append_transport_info(result, "product:", t->product, false);
982 append_transport_info(result, "model:", t->model, true);
983 append_transport_info(result, "device:", t->device, false);
Josh Gaob39e4152017-08-16 16:57:01 -0700984
985 // Put id at the end, so that anyone parsing the output here can always find it by scanning
986 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
987 *result += " transport_id:";
988 *result += std::to_string(t->id);
Scott Anderson27042382012-05-30 18:11:27 -0700989 }
Elliott Hughes88b4c852015-04-30 17:32:03 -0700990 *result += '\n';
Scott Anderson27042382012-05-30 18:11:27 -0700991}
992
Elliott Hughes88b4c852015-04-30 17:32:03 -0700993std::string list_transports(bool long_listing) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700994 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov4dd35012017-12-17 10:56:07 +0000995
996 auto sorted_transport_list = transport_list;
997 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
998 if (x->type != y->type) {
999 return x->type < y->type;
1000 }
1001 return strcmp(x->serial, y->serial) < 0;
1002 });
1003
1004 std::string result;
1005 for (const auto& t : sorted_transport_list) {
Elliott Hughes88b4c852015-04-30 17:32:03 -07001006 append_transport(t, &result, long_listing);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001007 }
Elliott Hughes88b4c852015-04-30 17:32:03 -07001008 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001009}
1010
Josh Gao4e562502016-10-27 14:01:08 -07001011void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001012 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao4e562502016-10-27 14:01:08 -07001013 for (auto& t : transport_list) {
1014 if (predicate(t)) {
1015 t->Kick();
1016 }
1017 }
1018}
1019
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001020/* hack for osx */
Dan Albertecce5032015-05-18 16:46:31 -07001021void close_usb_devices() {
Josh Gao4e562502016-10-27 14:01:08 -07001022 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001023}
Josh Gao0d6aa992016-11-22 14:32:34 -08001024#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001025
Josh Gao0d6aa992016-11-22 14:32:34 -08001026int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertecce5032015-05-18 16:46:31 -07001027 atransport* t = new atransport();
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001028
1029 if (!serial) {
Dan Albertecce5032015-05-18 16:46:31 -07001030 char buf[32];
1031 snprintf(buf, sizeof(buf), "T-%p", t);
1032 serial = buf;
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001033 }
Dan Albertecce5032015-05-18 16:46:31 -07001034
Yabin Cui815ad882015-09-02 17:44:28 -07001035 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001036 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertecce5032015-05-18 16:46:31 -07001037 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001038 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001039 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001040
Josh Gaoaaa82f72017-08-17 13:50:51 -07001041 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes85952832015-10-07 15:59:35 -07001042 for (const auto& transport : pending_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001043 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001044 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao0d6aa992016-11-22 14:32:34 -08001045 << " is already in pending_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -07001046 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001047 return -1;
1048 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001049 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001050
Elliott Hughes85952832015-10-07 15:59:35 -07001051 for (const auto& transport : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001052 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001053 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao0d6aa992016-11-22 14:32:34 -08001054 << " is already in transport_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -07001055 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001056 return -1;
1057 }
1058 }
1059
Dan Albertecce5032015-05-18 16:46:31 -07001060 pending_list.push_front(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001061 t->serial = strdup(serial);
Josh Gaoe7daf572016-09-21 12:37:10 -07001062
1063 lock.unlock();
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001064
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001065 register_transport(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001066 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001067}
1068
Mike Lockwood81ffe172009-10-11 23:04:18 -04001069#if ADB_HOST
Josh Gao0d6aa992016-11-22 14:32:34 -08001070atransport* find_transport(const char* serial) {
Dan Albertecce5032015-05-18 16:46:31 -07001071 atransport* result = nullptr;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001072
Josh Gaoaaa82f72017-08-17 13:50:51 -07001073 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -07001074 for (auto& t : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001075 if (t->serial && strcmp(serial, t->serial) == 0) {
1076 result = t;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001077 break;
1078 }
Dan Albertecce5032015-05-18 16:46:31 -07001079 }
Mike Lockwood81ffe172009-10-11 23:04:18 -04001080
Dan Albertecce5032015-05-18 16:46:31 -07001081 return result;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001082}
1083
Yabin Cui4d64fd82015-08-27 12:03:11 -07001084void kick_all_tcp_devices() {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001085 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -07001086 for (auto& t : transport_list) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001087 if (t->IsTcpDevice()) {
Yabin Cui4e222292015-08-31 11:50:24 -07001088 // Kicking breaks the read_transport thread of this transport out of any read, then
1089 // the read_transport thread will notify the main thread to make this transport
1090 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cui4d64fd82015-08-27 12:03:11 -07001091 // Finally, this transport will be closed and freed in the main thread.
Yabin Cuif2a9f9b2016-04-18 11:22:34 -07001092 t->Kick();
Mike Lockwood01c2c302010-05-24 10:44:35 -04001093 }
Dan Albertecce5032015-05-18 16:46:31 -07001094 }
Mike Lockwood01c2c302010-05-24 10:44:35 -04001095}
1096
Mike Lockwood81ffe172009-10-11 23:04:18 -04001097#endif
1098
Josh Gao0d6aa992016-11-22 14:32:34 -08001099void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1100 unsigned writeable) {
Yabin Cui3cf1b362017-03-10 16:01:01 -08001101 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertecce5032015-05-18 16:46:31 -07001102
Josh Gao0d6aa992016-11-22 14:32:34 -08001103 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cui3cf1b362017-03-10 16:01:01 -08001104 init_usb_transport(t, usb);
Josh Gao0d6aa992016-11-22 14:32:34 -08001105 if (serial) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001106 t->serial = strdup(serial);
1107 }
Dan Albertecce5032015-05-18 16:46:31 -07001108
1109 if (devpath) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001110 t->devpath = strdup(devpath);
1111 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001112
Josh Gaoe7daf572016-09-21 12:37:10 -07001113 {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001114 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -07001115 pending_list.push_front(t);
1116 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001117
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001118 register_transport(t);
1119}
1120
Dan Albert9a50f4c2015-05-18 16:43:57 -07001121// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao0d6aa992016-11-22 14:32:34 -08001122void unregister_usb_transport(usb_handle* usb) {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001123 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao395b86a2018-01-28 20:32:46 -08001124 transport_list.remove_if([usb](atransport* t) {
1125 if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) {
1126 return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
1127 }
1128 return false;
1129 });
Mike Lockwoode45583f2009-08-08 12:37:44 -04001130}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001131
Josh Gao67b683a2017-05-16 15:02:45 -07001132bool check_header(apacket* p, atransport* t) {
Josh Gao0d6aa992016-11-22 14:32:34 -08001133 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cui3cf1b362017-03-10 16:01:01 -08001134 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1135 << ", magic = " << p->msg.magic;
Josh Gao67b683a2017-05-16 15:02:45 -07001136 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001137 }
1138
Josh Gao0d6aa992016-11-22 14:32:34 -08001139 if (p->msg.data_length > t->get_max_payload()) {
1140 VLOG(RWX) << "check_header(): " << p->msg.data_length
1141 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao67b683a2017-05-16 15:02:45 -07001142 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001143 }
1144
Josh Gao67b683a2017-05-16 15:02:45 -07001145 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001146}
1147
Josh Gaoeac20582016-10-05 19:02:29 -07001148#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -07001149std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes801066a2016-06-29 17:42:01 -07001150 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1151
Josh Gao22cb70b2016-08-18 22:00:12 -07001152 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes801066a2016-06-29 17:42:01 -07001153 keys_.pop_front();
1154 return result;
1155}
Josh Gaoeac20582016-10-05 19:02:29 -07001156#endif