blob: c90c59c6fc6eee010e4743503fc6715b5d85b09f [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
Josh Gaob14756a2018-02-02 14:38:04 -080075 if (packet->msg.data_length > sizeof(packet->data)) {
76 D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length);
77 return false;
78 }
79
Josh Gao395b86a2018-01-28 20:32:46 -080080 if (!ReadFdExactly(fd_.get(), &packet->data, packet->msg.data_length)) {
81 D("remote local: terminated (data)");
82 return false;
83 }
84
85 return true;
86}
87
88bool FdConnection::Write(apacket* packet) {
89 uint32_t length = packet->msg.data_length;
90
91 if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(amessage) + length)) {
92 D("remote local: write terminated");
93 return false;
94 }
95
96 return true;
97}
98
99void FdConnection::Close() {
100 adb_shutdown(fd_.get());
101 fd_.reset();
102}
103
Yabin Cui19bec5b2015-09-22 15:52:57 -0700104static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800105 unsigned command = p->msg.command;
106 int len = p->msg.data_length;
107 char cmd[9];
108 char arg0[12], arg1[12];
109 int n;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100110
111 for (n = 0; n < 4; n++) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800112 int b = (command >> (n * 8)) & 255;
113 if (b < 32 || b >= 127) break;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100114 cmd[n] = (char)b;
115 }
116 if (n == 4) {
117 cmd[4] = 0;
118 } else {
119 /* There is some non-ASCII name in the command, so dump
120 * the hexadecimal value instead */
121 snprintf(cmd, sizeof cmd, "%08x", command);
122 }
123
124 if (p->msg.arg0 < 256U)
125 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
126 else
127 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
128
129 if (p->msg.arg1 < 256U)
130 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
131 else
132 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
133
Josh Gao0d6aa992016-11-22 14:32:34 -0800134 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
135 func, cmd, arg0, arg1, len);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700136 result += dump_hex(p->data, len);
137 return result;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100138}
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100139
Josh Gao0d6aa992016-11-22 14:32:34 -0800140static int read_packet(int fd, const char* name, apacket** ppacket) {
Josh Gao9e09a972016-11-29 09:40:29 -0800141 ATRACE_NAME("read_packet");
Yabin Cuia96dabf2015-07-30 19:58:10 -0700142 char buff[8];
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100143 if (!name) {
144 snprintf(buff, sizeof buff, "fd=%d", fd);
145 name = buff;
146 }
Josh Gao0d6aa992016-11-22 14:32:34 -0800147 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
Yabin Cuia96dabf2015-07-30 19:58:10 -0700148 int len = sizeof(apacket*);
Josh Gao0d6aa992016-11-22 14:32:34 -0800149 while (len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700150 int r = adb_read(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800151 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152 len -= r;
Yabin Cuia96dabf2015-07-30 19:58:10 -0700153 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800154 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700155 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 -0800156 return -1;
157 }
158 }
159
Yabin Cui19bec5b2015-09-22 15:52:57 -0700160 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800161 return 0;
162}
163
Josh Gao0d6aa992016-11-22 14:32:34 -0800164static int write_packet(int fd, const char* name, apacket** ppacket) {
Josh Gao9e09a972016-11-29 09:40:29 -0800165 ATRACE_NAME("write_packet");
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100166 char buff[8];
167 if (!name) {
168 snprintf(buff, sizeof buff, "fd=%d", fd);
169 name = buff;
170 }
Yabin Cui19bec5b2015-09-22 15:52:57 -0700171 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Josh Gao0d6aa992016-11-22 14:32:34 -0800172 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
Yabin Cuia96dabf2015-07-30 19:58:10 -0700173 int len = sizeof(apacket*);
Josh Gao0d6aa992016-11-22 14:32:34 -0800174 while (len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700175 int r = adb_write(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800176 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177 len -= r;
178 p += r;
179 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700180 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 -0800181 return -1;
182 }
183 }
184 return 0;
185}
186
Josh Gao0d6aa992016-11-22 14:32:34 -0800187static void transport_socket_events(int fd, unsigned events, void* _t) {
188 atransport* t = reinterpret_cast<atransport*>(_t);
Yabin Cui815ad882015-09-02 17:44:28 -0700189 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
Josh Gao0d6aa992016-11-22 14:32:34 -0800190 if (events & FDE_READ) {
191 apacket* p = 0;
192 if (read_packet(fd, t->serial, &p)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700193 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 -0800194 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800195 handle_packet(p, (atransport*)_t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196 }
197 }
198}
199
Josh Gao67ac3792016-10-06 13:31:44 -0700200void send_packet(apacket* p, atransport* t) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayee7b44d2017-12-07 11:40:00 -0800202 // compute a checksum for connection/auth packets for compatibility reasons
203 if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
204 p->msg.data_check = 0;
205 } else {
206 p->msg.data_check = calculate_apacket_checksum(p);
207 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208
209 print_packet("send", p);
210
211 if (t == NULL) {
Josh Gao67ac3792016-10-06 13:31:44 -0700212 fatal("Transport is null");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800213 }
214
Josh Gao67ac3792016-10-06 13:31:44 -0700215 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800216 fatal_errno("cannot enqueue packet on transport socket");
217 }
218}
219
Yabin Cui4e222292015-08-31 11:50:24 -0700220// The transport is opened by transport_register_func before
221// the read_transport and write_transport threads are started.
222//
223// The read_transport thread issues a SYNC(1, token) message to let
224// the write_transport thread know to start things up. In the event
225// of transport IO failure, the read_transport thread will post a
226// SYNC(0,0) message to ensure shutdown.
227//
228// The transport will not actually be closed until both threads exit, but the threads
229// will kick the transport on their way out to disconnect the underlying device.
230//
231// read_transport thread reads data from a transport (representing a usb/tcp connection),
232// and makes the main thread call handle_packet().
Josh Gao7d405252016-02-12 14:31:15 -0800233static void read_transport_thread(void* _t) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800234 atransport* t = reinterpret_cast<atransport*>(_t);
235 apacket* p;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236
Josh Gao0d6aa992016-11-22 14:32:34 -0800237 adb_thread_setname(
238 android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport")));
239 D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd,
240 t->sync_token + 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241 p = get_apacket();
242 p->msg.command = A_SYNC;
243 p->msg.arg0 = 1;
244 p->msg.arg1 = ++(t->sync_token);
245 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao0d6aa992016-11-22 14:32:34 -0800246 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700248 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800249 goto oops;
250 }
251
Yabin Cui815ad882015-09-02 17:44:28 -0700252 D("%s: data pump started", t->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800253 for (;;) {
Josh Gao9e09a972016-11-29 09:40:29 -0800254 ATRACE_NAME("read_transport loop");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255 p = get_apacket();
256
Josh Gao9e09a972016-11-29 09:40:29 -0800257 {
258 ATRACE_NAME("read_transport read_remote");
Josh Gao395b86a2018-01-28 20:32:46 -0800259 if (!t->connection->Read(p)) {
Josh Gao9e09a972016-11-29 09:40:29 -0800260 D("%s: remote read failed for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 put_apacket(p);
Josh Gao9e09a972016-11-29 09:40:29 -0800262 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800263 }
Josh Gao395b86a2018-01-28 20:32:46 -0800264
265 if (!check_header(p, t)) {
266 D("%s: remote read: bad header", t->serial);
267 put_apacket(p);
268 break;
269 }
270
Yabin Cui3cf1b362017-03-10 16:01:01 -0800271#if ADB_HOST
272 if (p->msg.command == 0) {
Josh Gao30c42662018-01-29 18:09:20 -0800273 put_apacket(p);
Yabin Cui3cf1b362017-03-10 16:01:01 -0800274 continue;
275 }
276#endif
Josh Gao9e09a972016-11-29 09:40:29 -0800277 }
278
279 D("%s: received remote packet, sending to transport", t->serial);
280 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800281 put_apacket(p);
Josh Gao9e09a972016-11-29 09:40:29 -0800282 D("%s: failed to write apacket to transport", t->serial);
283 goto oops;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800284 }
285 }
286
Yabin Cui815ad882015-09-02 17:44:28 -0700287 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800288 p = get_apacket();
289 p->msg.command = A_SYNC;
290 p->msg.arg0 = 0;
291 p->msg.arg1 = 0;
292 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao0d6aa992016-11-22 14:32:34 -0800293 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800294 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700295 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800296 }
297
298oops:
Yabin Cui815ad882015-09-02 17:44:28 -0700299 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300 kick_transport(t);
301 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800302}
303
Yabin Cui4e222292015-08-31 11:50:24 -0700304// write_transport thread gets packets sent by the main thread (through send_packet()),
305// and writes to a transport (representing a usb/tcp connection).
Josh Gao7d405252016-02-12 14:31:15 -0800306static void write_transport_thread(void* _t) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800307 atransport* t = reinterpret_cast<atransport*>(_t);
308 apacket* p;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309 int active = 0;
310
Josh Gao0d6aa992016-11-22 14:32:34 -0800311 adb_thread_setname(
312 android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport")));
313 D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800314
Josh Gao0d6aa992016-11-22 14:32:34 -0800315 for (;;) {
Josh Gao9e09a972016-11-29 09:40:29 -0800316 ATRACE_NAME("write_transport loop");
Josh Gao0d6aa992016-11-22 14:32:34 -0800317 if (read_packet(t->fd, t->serial, &p)) {
318 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 -0800319 break;
320 }
Josh Gao9e09a972016-11-29 09:40:29 -0800321
Josh Gao0d6aa992016-11-22 14:32:34 -0800322 if (p->msg.command == A_SYNC) {
323 if (p->msg.arg0 == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700324 D("%s: transport SYNC offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800325 put_apacket(p);
326 break;
327 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800328 if (p->msg.arg1 == t->sync_token) {
Yabin Cui815ad882015-09-02 17:44:28 -0700329 D("%s: transport SYNC online", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330 active = 1;
331 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800332 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 -0800333 }
334 }
335 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800336 if (active) {
Yabin Cui815ad882015-09-02 17:44:28 -0700337 D("%s: transport got packet, sending to remote", t->serial);
Josh Gao9e09a972016-11-29 09:40:29 -0800338 ATRACE_NAME("write_transport write_remote");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800339 if (t->Write(p) != 0) {
340 D("%s: remote write failed for transport", t->serial);
341 put_apacket(p);
342 break;
343 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800344 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700345 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346 }
347 }
348
349 put_apacket(p);
350 }
351
Yabin Cui815ad882015-09-02 17:44:28 -0700352 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800353 kick_transport(t);
354 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800355}
356
Yabin Cui4d64fd82015-08-27 12:03:11 -0700357void kick_transport(atransport* t) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700358 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuid78ed222016-04-05 13:50:44 -0700359 // As kick_transport() can be called from threads without guarantee that t is valid,
360 // check if the transport is in transport_list first.
Josh Gaob39e4152017-08-16 16:57:01 -0700361 //
362 // TODO(jmgao): WTF? Is this actually true?
Yabin Cuid78ed222016-04-05 13:50:44 -0700363 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700364 t->Kick();
Yabin Cuid78ed222016-04-05 13:50:44 -0700365 }
Yabin Cui4d64fd82015-08-27 12:03:11 -0700366}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800367
368static int transport_registration_send = -1;
369static int transport_registration_recv = -1;
370static fdevent transport_registration_fde;
371
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800372#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800373
374/* this adds support required by the 'track-devices' service.
375 * this is used to send the content of "list_transport" to any
376 * number of client connections that want it through a single
377 * live TCP connection
378 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379struct device_tracker {
Josh Gao0d6aa992016-11-22 14:32:34 -0800380 asocket socket;
Josh Gao32124632017-08-14 18:57:54 -0700381 bool update_needed;
382 bool long_output;
Josh Gao0d6aa992016-11-22 14:32:34 -0800383 device_tracker* next;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800384};
385
386/* linked list of all device trackers */
Josh Gao0d6aa992016-11-22 14:32:34 -0800387static device_tracker* device_tracker_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388
Josh Gao0d6aa992016-11-22 14:32:34 -0800389static void device_tracker_remove(device_tracker* tracker) {
390 device_tracker** pnode = &device_tracker_list;
391 device_tracker* node = *pnode;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392
Josh Gaoaaa82f72017-08-17 13:50:51 -0700393 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800394 while (node) {
395 if (node == tracker) {
396 *pnode = node->next;
397 break;
398 }
399 pnode = &node->next;
Josh Gao0d6aa992016-11-22 14:32:34 -0800400 node = *pnode;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800401 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402}
403
Josh Gao0d6aa992016-11-22 14:32:34 -0800404static void device_tracker_close(asocket* socket) {
405 device_tracker* tracker = (device_tracker*)socket;
406 asocket* peer = socket->peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800407
Josh Gao0d6aa992016-11-22 14:32:34 -0800408 D("device tracker %p removed", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800409 if (peer) {
410 peer->peer = NULL;
411 peer->close(peer);
412 }
413 device_tracker_remove(tracker);
414 free(tracker);
415}
416
Josh Gao0d6aa992016-11-22 14:32:34 -0800417static int device_tracker_enqueue(asocket* socket, apacket* p) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418 /* you can't read from a device tracker, close immediately */
419 put_apacket(p);
420 device_tracker_close(socket);
421 return -1;
422}
423
Elliott Hughes88b4c852015-04-30 17:32:03 -0700424static int device_tracker_send(device_tracker* tracker, const std::string& string) {
425 apacket* p = get_apacket();
426 asocket* peer = tracker->socket.peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427
Elliott Hughes88b4c852015-04-30 17:32:03 -0700428 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
429 memcpy(&p->data[4], string.data(), string.size());
430 p->len = 4 + string.size();
431 return peer->enqueue(peer, p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800432}
433
Elliott Hughes88b4c852015-04-30 17:32:03 -0700434static void device_tracker_ready(asocket* socket) {
435 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800436
Elliott Hughes88b4c852015-04-30 17:32:03 -0700437 // We want to send the device list when the tracker connects
438 // for the first time, even if no update occurred.
Josh Gao32124632017-08-14 18:57:54 -0700439 if (tracker->update_needed) {
440 tracker->update_needed = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441
Josh Gao32124632017-08-14 18:57:54 -0700442 std::string transports = list_transports(tracker->long_output);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700443 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800444 }
445}
446
Josh Gao32124632017-08-14 18:57:54 -0700447asocket* create_device_tracker(bool long_output) {
Elliott Hughesd0269c92015-04-21 19:39:52 -0700448 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
449 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800450
Josh Gao0d6aa992016-11-22 14:32:34 -0800451 D("device tracker %p created", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800452
453 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao0d6aa992016-11-22 14:32:34 -0800454 tracker->socket.ready = device_tracker_ready;
455 tracker->socket.close = device_tracker_close;
Josh Gao32124632017-08-14 18:57:54 -0700456 tracker->update_needed = true;
457 tracker->long_output = long_output;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458
Josh Gao0d6aa992016-11-22 14:32:34 -0800459 tracker->next = device_tracker_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800460 device_tracker_list = tracker;
461
462 return &tracker->socket;
463}
464
Josh Gao1e3bf732017-05-03 22:37:10 -0700465// Check if all of the USB transports are connected.
466bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700467 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao1e3bf732017-05-03 22:37:10 -0700468 for (const auto& t : transport_list) {
469 if (!fn(t)) {
470 return false;
471 }
472 }
473 for (const auto& t : pending_list) {
474 if (!fn(t)) {
475 return false;
476 }
477 }
478 return true;
479}
480
Elliott Hughes88b4c852015-04-30 17:32:03 -0700481// Call this function each time the transport list has changed.
482void update_transports() {
Josh Gao1e3bf732017-05-03 22:37:10 -0700483 update_transport_status();
484
485 // Notify `adb track-devices` clients.
Elliott Hughes88b4c852015-04-30 17:32:03 -0700486 std::string transports = list_transports(false);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800487
Elliott Hughes88b4c852015-04-30 17:32:03 -0700488 device_tracker* tracker = device_tracker_list;
489 while (tracker != nullptr) {
490 device_tracker* next = tracker->next;
491 // This may destroy the tracker if the connection is closed.
492 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800493 tracker = next;
494 }
495}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700496
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800497#else
Elliott Hughes88b4c852015-04-30 17:32:03 -0700498
499void update_transports() {
500 // Nothing to do on the device side.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800501}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700502
Josh Gao0d6aa992016-11-22 14:32:34 -0800503#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800504
Josh Gao0d6aa992016-11-22 14:32:34 -0800505struct tmsg {
506 atransport* transport;
507 int action;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800508};
509
Josh Gao0d6aa992016-11-22 14:32:34 -0800510static int transport_read_action(int fd, struct tmsg* m) {
511 char* p = (char*)m;
512 int len = sizeof(*m);
513 int r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800514
Josh Gao0d6aa992016-11-22 14:32:34 -0800515 while (len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800516 r = adb_read(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800517 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800518 len -= r;
Josh Gao0d6aa992016-11-22 14:32:34 -0800519 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800520 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700521 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800522 return -1;
523 }
524 }
525 return 0;
526}
527
Josh Gao0d6aa992016-11-22 14:32:34 -0800528static int transport_write_action(int fd, struct tmsg* m) {
529 char* p = (char*)m;
530 int len = sizeof(*m);
531 int r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800532
Josh Gao0d6aa992016-11-22 14:32:34 -0800533 while (len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800534 r = adb_write(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800535 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800536 len -= r;
Josh Gao0d6aa992016-11-22 14:32:34 -0800537 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800538 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700539 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800540 return -1;
541 }
542 }
543 return 0;
544}
545
Josh Gao0d6aa992016-11-22 14:32:34 -0800546static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800547 tmsg m;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800548 int s[2];
Josh Gao0d6aa992016-11-22 14:32:34 -0800549 atransport* t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800550
Josh Gao0d6aa992016-11-22 14:32:34 -0800551 if (!(ev & FDE_READ)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800552 return;
553 }
554
Josh Gao0d6aa992016-11-22 14:32:34 -0800555 if (transport_read_action(_fd, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800556 fatal_errno("cannot read transport registration socket");
557 }
558
559 t = m.transport;
560
Dan Albertbe8e54b2015-05-18 13:06:53 -0700561 if (m.action == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700562 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800563
Josh Gao0d6aa992016-11-22 14:32:34 -0800564 /* IMPORTANT: the remove closes one half of the
565 ** socket pair. The close closes the other half.
566 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800567 fdevent_remove(&(t->transport_fde));
568 adb_close(t->fd);
569
Josh Gaoe7daf572016-09-21 12:37:10 -0700570 {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700571 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700572 transport_list.remove(t);
573 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800574
Josh Gao0d6aa992016-11-22 14:32:34 -0800575 if (t->product) free(t->product);
576 if (t->serial) free(t->serial);
577 if (t->model) free(t->model);
578 if (t->device) free(t->device);
579 if (t->devpath) free(t->devpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800580
Dan Albertecce5032015-05-18 16:46:31 -0700581 delete t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800582
583 update_transports();
584 return;
585 }
586
Mike Lockwoode45583f2009-08-08 12:37:44 -0400587 /* don't create transport threads for inaccessible devices */
Yabin Cui3cf1b362017-03-10 16:01:01 -0800588 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800589 /* initial references are the two threads */
Mike Lockwoode45583f2009-08-08 12:37:44 -0400590 t->ref_count = 2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800591
Dan Albertecce5032015-05-18 16:46:31 -0700592 if (adb_socketpair(s)) {
Mike Lockwoode45583f2009-08-08 12:37:44 -0400593 fatal_errno("cannot open transport socketpair");
594 }
595
Yabin Cui815ad882015-09-02 17:44:28 -0700596 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400597
598 t->transport_socket = s[0];
599 t->fd = s[1];
600
Josh Gao0d6aa992016-11-22 14:32:34 -0800601 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400602
603 fdevent_set(&(t->transport_fde), FDE_READ);
604
Josh Gao0f3312a2017-04-12 17:00:49 -0700605 std::thread(write_transport_thread, t).detach();
606 std::thread(read_transport_thread, t).detach();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800607 }
608
Josh Gaoe7daf572016-09-21 12:37:10 -0700609 {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700610 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700611 pending_list.remove(t);
612 transport_list.push_front(t);
613 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800614
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800615 update_transports();
616}
617
Josh Gao0d6aa992016-11-22 14:32:34 -0800618void init_transport_registration(void) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800619 int s[2];
620
Josh Gao0d6aa992016-11-22 14:32:34 -0800621 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800622 fatal_errno("cannot open transport registration socketpair");
623 }
Yabin Cui815ad882015-09-02 17:44:28 -0700624 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800625
626 transport_registration_send = s[0];
627 transport_registration_recv = s[1];
628
Josh Gao0d6aa992016-11-22 14:32:34 -0800629 fdevent_install(&transport_registration_fde, transport_registration_recv,
630 transport_registration_func, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800631
632 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao165460f2017-05-09 13:43:35 -0700633}
634
635void kick_all_transports() {
636 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gaoaaa82f72017-08-17 13:50:51 -0700637 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao165460f2017-05-09 13:43:35 -0700638 for (auto t : transport_list) {
639 t->Kick();
640 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800641}
642
643/* the fdevent select pump is single threaded */
Josh Gao0d6aa992016-11-22 14:32:34 -0800644static void register_transport(atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800645 tmsg m;
646 m.transport = transport;
647 m.action = 1;
Yabin Cui815ad882015-09-02 17:44:28 -0700648 D("transport: %s registered", transport->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800649 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800650 fatal_errno("cannot write transport registration socket\n");
651 }
652}
653
Josh Gao0d6aa992016-11-22 14:32:34 -0800654static void remove_transport(atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800655 tmsg m;
656 m.transport = transport;
657 m.action = 0;
Yabin Cui815ad882015-09-02 17:44:28 -0700658 D("transport: %s removed", transport->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800659 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800660 fatal_errno("cannot write transport registration socket\n");
661 }
662}
663
Yabin Cui4d64fd82015-08-27 12:03:11 -0700664static void transport_unref(atransport* t) {
665 CHECK(t != nullptr);
Josh Gaoe7daf572016-09-21 12:37:10 -0700666
Josh Gao4d299742017-09-13 13:40:57 -0700667 std::lock_guard<std::recursive_mutex> lock(transport_lock);
668 CHECK_GT(t->ref_count, 0u);
669 t->ref_count--;
670 if (t->ref_count == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700671 D("transport: %s unref (kicking and closing)", t->serial);
Josh Gao395b86a2018-01-28 20:32:46 -0800672 t->connection->Close();
Mike Lockwood01c2c302010-05-24 10:44:35 -0400673 remove_transport(t);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100674 } else {
Josh Gao4d299742017-09-13 13:40:57 -0700675 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400676 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800677}
678
Josh Gao0d6aa992016-11-22 14:32:34 -0800679static int qual_match(const char* to_test, const char* prefix, const char* qual,
680 bool sanitize_qual) {
681 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson27042382012-05-30 18:11:27 -0700682 return !qual || !*qual;
683
Josh Gao0d6aa992016-11-22 14:32:34 -0800684 if (!qual) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700685
686 if (prefix) {
687 while (*prefix) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800688 if (*prefix++ != *to_test++) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700689 }
690 }
691
692 while (*qual) {
693 char ch = *qual++;
Josh Gao0d6aa992016-11-22 14:32:34 -0800694 if (sanitize_qual && !isalnum(ch)) ch = '_';
695 if (ch != *to_test++) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700696 }
697
698 /* Everything matched so far. Return true if *to_test is a NUL. */
699 return !*to_test;
700}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800701
Josh Gaob39e4152017-08-16 16:57:01 -0700702atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
703 bool* is_ambiguous, std::string* error_out,
704 bool accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700705 atransport* result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800706
Josh Gaob39e4152017-08-16 16:57:01 -0700707 if (transport_id != 0) {
708 *error_out =
709 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
710 } else if (serial) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700711 *error_out = android::base::StringPrintf("device '%s' not found", serial);
712 } else if (type == kTransportLocal) {
713 *error_out = "no emulators found";
714 } else if (type == kTransportAny) {
715 *error_out = "no devices/emulators found";
716 } else {
717 *error_out = "no devices found";
718 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800719
Josh Gaoaaa82f72017-08-17 13:50:51 -0700720 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes67943d12015-10-07 14:55:10 -0700721 for (const auto& t : transport_list) {
Yabin Cui3cf1b362017-03-10 16:01:01 -0800722 if (t->GetConnectionState() == kCsNoPerm) {
Elliott Hughes70097492015-12-11 19:07:01 -0800723#if ADB_HOST
David Pursell6e5c7eb2015-12-02 15:14:31 -0800724 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes70097492015-12-11 19:07:01 -0800725#endif
Mike Lockwoodadc16b32009-08-08 13:53:16 -0400726 continue;
727 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400728
Josh Gaob39e4152017-08-16 16:57:01 -0700729 if (transport_id) {
730 if (t->id == transport_id) {
731 result = t;
732 break;
733 }
734 } else if (serial) {
David Pursellc929c6f2016-03-01 08:58:26 -0800735 if (t->MatchesTarget(serial)) {
Scott Anderson27042382012-05-30 18:11:27 -0700736 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700737 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700738 if (is_ambiguous) *is_ambiguous = true;
739 result = nullptr;
Scott Anderson27042382012-05-30 18:11:27 -0700740 break;
741 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800742 result = t;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700743 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800744 } else {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700745 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800746 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700747 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700748 if (is_ambiguous) *is_ambiguous = true;
749 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800750 break;
751 }
752 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700753 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800754 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700755 *error_out = "more than one emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700756 if (is_ambiguous) *is_ambiguous = true;
757 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800758 break;
759 }
760 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700761 } else if (type == kTransportAny) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800762 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700763 *error_out = "more than one device/emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700764 if (is_ambiguous) *is_ambiguous = true;
765 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800766 break;
767 }
768 result = t;
769 }
770 }
771 }
Josh Gaoe7daf572016-09-21 12:37:10 -0700772 lock.unlock();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800773
Elliott Hughes67943d12015-10-07 14:55:10 -0700774 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cui3cf1b362017-03-10 16:01:01 -0800775 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700776 *error_out = "device unauthorized.\n";
777 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
778 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
779 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
780 *error_out += "\n";
781 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
782 *error_out += "Otherwise check for a confirmation dialog on your device.";
783 result = nullptr;
784 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700785
Elliott Hughes67943d12015-10-07 14:55:10 -0700786 // Don't return offline devices; the caller can't do anything with them.
Yabin Cui3cf1b362017-03-10 16:01:01 -0800787 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700788 *error_out = "device offline";
789 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800790 }
791
792 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700793 *error_out = "success";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800794 }
795
796 return result;
797}
798
Yabin Cui3cf1b362017-03-10 16:01:01 -0800799int atransport::Write(apacket* p) {
Josh Gao395b86a2018-01-28 20:32:46 -0800800 return this->connection->Write(p) ? 0 : -1;
Yabin Cui3cf1b362017-03-10 16:01:01 -0800801}
802
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700803void atransport::Kick() {
804 if (!kicked_) {
Josh Gao395b86a2018-01-28 20:32:46 -0800805 D("kicking transport %s", this->serial);
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700806 kicked_ = true;
Josh Gao395b86a2018-01-28 20:32:46 -0800807 this->connection->Close();
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700808 }
809}
810
Yabin Cui3cf1b362017-03-10 16:01:01 -0800811ConnectionState atransport::GetConnectionState() const {
812 return connection_state_;
813}
814
815void atransport::SetConnectionState(ConnectionState state) {
816 check_main_thread();
817 connection_state_ = state;
818}
819
David Pursell6e5c7eb2015-12-02 15:14:31 -0800820const std::string atransport::connection_state_name() const {
Yabin Cui3cf1b362017-03-10 16:01:01 -0800821 ConnectionState state = GetConnectionState();
822 switch (state) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800823 case kCsOffline:
824 return "offline";
825 case kCsBootloader:
826 return "bootloader";
827 case kCsDevice:
828 return "device";
829 case kCsHost:
830 return "host";
831 case kCsRecovery:
832 return "recovery";
833 case kCsNoPerm:
834 return UsbNoPermissionsShortHelpText();
835 case kCsSideload:
836 return "sideload";
837 case kCsUnauthorized:
838 return "unauthorized";
839 default:
840 return "unknown";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800841 }
842}
843
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100844void atransport::update_version(int version, size_t payload) {
845 protocol_version = std::min(version, A_VERSION);
846 max_payload = std::min(payload, MAX_PAYLOAD);
847}
848
849int atransport::get_protocol_version() const {
850 return protocol_version;
851}
852
853size_t atransport::get_max_payload() const {
854 return max_payload;
855}
856
David Pursella07dbad2015-09-22 10:43:08 -0700857namespace {
David Pursell8da19a42015-08-31 10:42:13 -0700858
David Pursella07dbad2015-09-22 10:43:08 -0700859constexpr char kFeatureStringDelimiter = ',';
860
861} // namespace
Dan Albertbe8e54b2015-05-18 13:06:53 -0700862
863const FeatureSet& supported_features() {
David Pursella07dbad2015-09-22 10:43:08 -0700864 // Local static allocation to avoid global non-POD variables.
865 static const FeatureSet* features = new FeatureSet{
Josh Gao0d6aa992016-11-22 14:32:34 -0800866 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursell6d5fad32015-09-25 08:37:13 -0700867 // Increment ADB_SERVER_VERSION whenever the feature list changes to
868 // make sure that the adb client and server features stay in sync
869 // (http://b/24370690).
David Pursella07dbad2015-09-22 10:43:08 -0700870 };
871
872 return *features;
873}
874
875std::string FeatureSetToString(const FeatureSet& features) {
876 return android::base::Join(features, kFeatureStringDelimiter);
877}
878
879FeatureSet StringToFeatureSet(const std::string& features_string) {
David Pursell3d9072b2015-09-25 13:04:21 -0700880 if (features_string.empty()) {
881 return FeatureSet();
882 }
883
Josh Gao0d6aa992016-11-22 14:32:34 -0800884 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursella07dbad2015-09-22 10:43:08 -0700885 return FeatureSet(names.begin(), names.end());
Dan Albertbe8e54b2015-05-18 13:06:53 -0700886}
887
David Pursell22fc5e92015-09-30 13:35:42 -0700888bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800889 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell22fc5e92015-09-30 13:35:42 -0700890}
891
Dan Albertbe8e54b2015-05-18 13:06:53 -0700892bool atransport::has_feature(const std::string& feature) const {
893 return features_.count(feature) > 0;
894}
895
David Pursella07dbad2015-09-22 10:43:08 -0700896void atransport::SetFeatures(const std::string& features_string) {
897 features_ = StringToFeatureSet(features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700898}
899
Yabin Cui2d4c1982015-08-28 15:09:44 -0700900void atransport::AddDisconnect(adisconnect* disconnect) {
901 disconnects_.push_back(disconnect);
902}
903
904void atransport::RemoveDisconnect(adisconnect* disconnect) {
905 disconnects_.remove(disconnect);
906}
907
908void atransport::RunDisconnects() {
Elliott Hughes85952832015-10-07 15:59:35 -0700909 for (const auto& disconnect : disconnects_) {
Yabin Cui2d4c1982015-08-28 15:09:44 -0700910 disconnect->func(disconnect->opaque, this);
911 }
912 disconnects_.clear();
913}
914
David Pursellc929c6f2016-03-01 08:58:26 -0800915bool atransport::MatchesTarget(const std::string& target) const {
916 if (serial) {
917 if (target == serial) {
918 return true;
919 } else if (type == kTransportLocal) {
920 // Local transports can match [tcp:|udp:]<hostname>[:port].
921 const char* local_target_ptr = target.c_str();
922
923 // For fastboot compatibility, ignore protocol prefixes.
924 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao0d6aa992016-11-22 14:32:34 -0800925 android::base::StartsWith(target, "udp:")) {
David Pursellc929c6f2016-03-01 08:58:26 -0800926 local_target_ptr += 4;
927 }
928
929 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
930 std::string serial_host, error;
931 int serial_port = -1;
Josh Gao0d6aa992016-11-22 14:32:34 -0800932 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursellc929c6f2016-03-01 08:58:26 -0800933 // |target| may omit the port to default to ours.
934 std::string target_host;
935 int target_port = serial_port;
936 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
937 nullptr, &error) &&
Josh Gao0d6aa992016-11-22 14:32:34 -0800938 serial_host == target_host && serial_port == target_port) {
David Pursellc929c6f2016-03-01 08:58:26 -0800939 return true;
940 }
941 }
942 }
943 }
944
945 return (devpath && target == devpath) ||
946 qual_match(target.c_str(), "product:", product, false) ||
947 qual_match(target.c_str(), "model:", model, true) ||
948 qual_match(target.c_str(), "device:", device, false);
949}
950
Elliott Hughes88b4c852015-04-30 17:32:03 -0700951#if ADB_HOST
952
Josh Gaob39e4152017-08-16 16:57:01 -0700953// We use newline as our delimiter, make sure to never output it.
954static std::string sanitize(std::string str, bool alphanumeric) {
955 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
956 : [](const char c) { return c == '\n'; };
957 std::replace_if(str.begin(), str.end(), pred, '_');
958 return str;
959}
960
Josh Gao0d6aa992016-11-22 14:32:34 -0800961static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob39e4152017-08-16 16:57:01 -0700962 bool alphanumeric) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700963 if (value == nullptr || *value == '\0') {
Scott Anderson27042382012-05-30 18:11:27 -0700964 return;
Scott Anderson27042382012-05-30 18:11:27 -0700965 }
966
Elliott Hughes88b4c852015-04-30 17:32:03 -0700967 *result += ' ';
968 *result += key;
Josh Gaob39e4152017-08-16 16:57:01 -0700969 *result += sanitize(value, alphanumeric);
Scott Anderson27042382012-05-30 18:11:27 -0700970}
971
Josh Gao0d6aa992016-11-22 14:32:34 -0800972static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson27042382012-05-30 18:11:27 -0700973 const char* serial = t->serial;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700974 if (!serial || !serial[0]) {
Dan Alberta8c34142015-05-06 16:48:52 -0700975 serial = "(no serial number)";
Elliott Hughes88b4c852015-04-30 17:32:03 -0700976 }
Scott Anderson27042382012-05-30 18:11:27 -0700977
978 if (!long_listing) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700979 *result += serial;
980 *result += '\t';
981 *result += t->connection_state_name();
Scott Anderson27042382012-05-30 18:11:27 -0700982 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800983 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson27042382012-05-30 18:11:27 -0700984
Elliott Hughes88b4c852015-04-30 17:32:03 -0700985 append_transport_info(result, "", t->devpath, false);
986 append_transport_info(result, "product:", t->product, false);
987 append_transport_info(result, "model:", t->model, true);
988 append_transport_info(result, "device:", t->device, false);
Josh Gaob39e4152017-08-16 16:57:01 -0700989
990 // Put id at the end, so that anyone parsing the output here can always find it by scanning
991 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
992 *result += " transport_id:";
993 *result += std::to_string(t->id);
Scott Anderson27042382012-05-30 18:11:27 -0700994 }
Elliott Hughes88b4c852015-04-30 17:32:03 -0700995 *result += '\n';
Scott Anderson27042382012-05-30 18:11:27 -0700996}
997
Elliott Hughes88b4c852015-04-30 17:32:03 -0700998std::string list_transports(bool long_listing) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700999 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov4dd35012017-12-17 10:56:07 +00001000
1001 auto sorted_transport_list = transport_list;
1002 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
1003 if (x->type != y->type) {
1004 return x->type < y->type;
1005 }
1006 return strcmp(x->serial, y->serial) < 0;
1007 });
1008
1009 std::string result;
1010 for (const auto& t : sorted_transport_list) {
Elliott Hughes88b4c852015-04-30 17:32:03 -07001011 append_transport(t, &result, long_listing);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001012 }
Elliott Hughes88b4c852015-04-30 17:32:03 -07001013 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001014}
1015
Josh Gao4e562502016-10-27 14:01:08 -07001016void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001017 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao4e562502016-10-27 14:01:08 -07001018 for (auto& t : transport_list) {
1019 if (predicate(t)) {
1020 t->Kick();
1021 }
1022 }
1023}
1024
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001025/* hack for osx */
Dan Albertecce5032015-05-18 16:46:31 -07001026void close_usb_devices() {
Josh Gao4e562502016-10-27 14:01:08 -07001027 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001028}
Josh Gao0d6aa992016-11-22 14:32:34 -08001029#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001030
Josh Gao0d6aa992016-11-22 14:32:34 -08001031int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertecce5032015-05-18 16:46:31 -07001032 atransport* t = new atransport();
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001033
1034 if (!serial) {
Dan Albertecce5032015-05-18 16:46:31 -07001035 char buf[32];
1036 snprintf(buf, sizeof(buf), "T-%p", t);
1037 serial = buf;
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001038 }
Dan Albertecce5032015-05-18 16:46:31 -07001039
Yabin Cui815ad882015-09-02 17:44:28 -07001040 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001041 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertecce5032015-05-18 16:46:31 -07001042 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001043 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001044 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001045
Josh Gaoaaa82f72017-08-17 13:50:51 -07001046 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes85952832015-10-07 15:59:35 -07001047 for (const auto& transport : pending_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001048 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001049 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao0d6aa992016-11-22 14:32:34 -08001050 << " is already in pending_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -07001051 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001052 return -1;
1053 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001054 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001055
Elliott Hughes85952832015-10-07 15:59:35 -07001056 for (const auto& transport : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001057 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001058 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao0d6aa992016-11-22 14:32:34 -08001059 << " is already in transport_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -07001060 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001061 return -1;
1062 }
1063 }
1064
Dan Albertecce5032015-05-18 16:46:31 -07001065 pending_list.push_front(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001066 t->serial = strdup(serial);
Josh Gaoe7daf572016-09-21 12:37:10 -07001067
1068 lock.unlock();
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001069
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001070 register_transport(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001071 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001072}
1073
Mike Lockwood81ffe172009-10-11 23:04:18 -04001074#if ADB_HOST
Josh Gao0d6aa992016-11-22 14:32:34 -08001075atransport* find_transport(const char* serial) {
Dan Albertecce5032015-05-18 16:46:31 -07001076 atransport* result = nullptr;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001077
Josh Gaoaaa82f72017-08-17 13:50:51 -07001078 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -07001079 for (auto& t : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001080 if (t->serial && strcmp(serial, t->serial) == 0) {
1081 result = t;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001082 break;
1083 }
Dan Albertecce5032015-05-18 16:46:31 -07001084 }
Mike Lockwood81ffe172009-10-11 23:04:18 -04001085
Dan Albertecce5032015-05-18 16:46:31 -07001086 return result;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001087}
1088
Yabin Cui4d64fd82015-08-27 12:03:11 -07001089void kick_all_tcp_devices() {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001090 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -07001091 for (auto& t : transport_list) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001092 if (t->IsTcpDevice()) {
Yabin Cui4e222292015-08-31 11:50:24 -07001093 // Kicking breaks the read_transport thread of this transport out of any read, then
1094 // the read_transport thread will notify the main thread to make this transport
1095 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cui4d64fd82015-08-27 12:03:11 -07001096 // Finally, this transport will be closed and freed in the main thread.
Yabin Cuif2a9f9b2016-04-18 11:22:34 -07001097 t->Kick();
Mike Lockwood01c2c302010-05-24 10:44:35 -04001098 }
Dan Albertecce5032015-05-18 16:46:31 -07001099 }
Mike Lockwood01c2c302010-05-24 10:44:35 -04001100}
1101
Mike Lockwood81ffe172009-10-11 23:04:18 -04001102#endif
1103
Josh Gao0d6aa992016-11-22 14:32:34 -08001104void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1105 unsigned writeable) {
Yabin Cui3cf1b362017-03-10 16:01:01 -08001106 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertecce5032015-05-18 16:46:31 -07001107
Josh Gao0d6aa992016-11-22 14:32:34 -08001108 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cui3cf1b362017-03-10 16:01:01 -08001109 init_usb_transport(t, usb);
Josh Gao0d6aa992016-11-22 14:32:34 -08001110 if (serial) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001111 t->serial = strdup(serial);
1112 }
Dan Albertecce5032015-05-18 16:46:31 -07001113
1114 if (devpath) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001115 t->devpath = strdup(devpath);
1116 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001117
Josh Gaoe7daf572016-09-21 12:37:10 -07001118 {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001119 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -07001120 pending_list.push_front(t);
1121 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001122
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001123 register_transport(t);
1124}
1125
Dan Albert9a50f4c2015-05-18 16:43:57 -07001126// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao0d6aa992016-11-22 14:32:34 -08001127void unregister_usb_transport(usb_handle* usb) {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001128 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao395b86a2018-01-28 20:32:46 -08001129 transport_list.remove_if([usb](atransport* t) {
1130 if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) {
1131 return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
1132 }
1133 return false;
1134 });
Mike Lockwoode45583f2009-08-08 12:37:44 -04001135}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001136
Josh Gao67b683a2017-05-16 15:02:45 -07001137bool check_header(apacket* p, atransport* t) {
Josh Gao0d6aa992016-11-22 14:32:34 -08001138 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cui3cf1b362017-03-10 16:01:01 -08001139 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1140 << ", magic = " << p->msg.magic;
Josh Gao67b683a2017-05-16 15:02:45 -07001141 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001142 }
1143
Josh Gao0d6aa992016-11-22 14:32:34 -08001144 if (p->msg.data_length > t->get_max_payload()) {
1145 VLOG(RWX) << "check_header(): " << p->msg.data_length
1146 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao67b683a2017-05-16 15:02:45 -07001147 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001148 }
1149
Josh Gao67b683a2017-05-16 15:02:45 -07001150 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001151}
1152
Josh Gaoeac20582016-10-05 19:02:29 -07001153#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -07001154std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes801066a2016-06-29 17:42:01 -07001155 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1156
Josh Gao22cb70b2016-08-18 22:00:12 -07001157 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes801066a2016-06-29 17:42:01 -07001158 keys_.pop_front();
1159 return result;
1160}
Josh Gaoeac20582016-10-05 19:02:29 -07001161#endif