blob: 9ae129751762f92d99dec73e415d6c1d85262482 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albert76649012015-02-24 15:51:19 -080018
Dan Albert33134262015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert055f1aa2015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <errno.h>
Josh Gaob122b172017-08-16 16:57:01 -070024#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080028#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Spencer Low363af562015-11-07 18:51:54 -080030#include <algorithm>
Dan Albertc7915a32015-05-18 16:46:31 -070031#include <list>
Josh Gao0cd3ae12016-09-21 12:37:10 -070032#include <mutex>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070033#include <thread>
Dan Albertc7915a32015-05-18 16:46:31 -070034
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/logging.h>
David Pursell3f902aa2016-03-01 08:58:26 -080036#include <android-base/parsenetaddress.h>
Yabin Cuib5e11412017-03-10 16:01:01 -080037#include <android-base/quick_exit.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
Josh Gaob122b172017-08-16 16:57:01 -070040#include <android-base/thread_annotations.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070041
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include "adb.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070043#include "adb_auth.h"
Josh Gaob800d882018-01-28 20:32:46 -080044#include "adb_io.h"
Josh Gaocfe72e22016-11-29 09:40:29 -080045#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070046#include "adb_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080047#include "diagnose_usb.h"
Yabin Cuib5e11412017-03-10 16:01:01 -080048#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50static void transport_unref(atransport *t);
51
Josh Gaob122b172017-08-16 16:57:01 -070052// TODO: unordered_map<TransportId, atransport*>
Josh Gaob7b1edf2015-11-11 17:56:12 -080053static auto& transport_list = *new std::list<atransport*>();
54static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070055
Josh Gao1db71af2017-08-17 13:50:51 -070056static auto& transport_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Todd Kennedy51c05ec2015-11-10 00:03:25 +000058const char* const kFeatureShell2 = "shell_v2";
59const char* const kFeatureCmd = "cmd";
Josh Gao5a1e3fd2016-12-05 17:11:34 -080060const char* const kFeatureStat2 = "stat_v2";
Josh Gao5d1756c2017-02-22 17:07:01 -080061const char* const kFeatureLibusb = "libusb";
Dan Albert5176df82017-05-23 14:30:00 -070062const char* const kFeaturePushSync = "push_sync";
Todd Kennedy51c05ec2015-11-10 00:03:25 +000063
Josh Gaob122b172017-08-16 16:57:01 -070064TransportId NextTransportId() {
65 static std::atomic<TransportId> next(1);
66 return next++;
67}
68
Josh Gaob800d882018-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 Gao5caaebd2018-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 Gaob800d882018-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 Cuiaed3c612015-09-22 15:52:57 -0700104static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao1290fbf2016-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' Turner730ff3b2011-01-06 14:11:07 +0100110
111 for (n = 0; n < 4; n++) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800112 int b = (command >> (n * 8)) & 255;
113 if (b < 32 || b >= 127) break;
David 'Digit' Turner730ff3b2011-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 Gao1290fbf2016-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 Cuiaed3c612015-09-22 15:52:57 -0700136 result += dump_hex(p->data, len);
137 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100138}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100139
Josh Gao1290fbf2016-11-22 14:32:34 -0800140static int read_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800141 ATRACE_NAME("read_packet");
Yabin Cui62641292015-07-30 19:58:10 -0700142 char buff[8];
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100143 if (!name) {
144 snprintf(buff, sizeof buff, "fd=%d", fd);
145 name = buff;
146 }
Josh Gao1290fbf2016-11-22 14:32:34 -0800147 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700148 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800149 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700150 int r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800151 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 len -= r;
Yabin Cui62641292015-07-30 19:58:10 -0700153 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 } else {
Yabin Cui7a3f8d62015-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 Projectdd7bc332009-03-03 19:32:55 -0800156 return -1;
157 }
158 }
159
Yabin Cuiaed3c612015-09-22 15:52:57 -0700160 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 return 0;
162}
163
Josh Gao1290fbf2016-11-22 14:32:34 -0800164static int write_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800165 ATRACE_NAME("write_packet");
David 'Digit' Turner730ff3b2011-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 Cuiaed3c612015-09-22 15:52:57 -0700171 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Josh Gao1290fbf2016-11-22 14:32:34 -0800172 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700173 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800174 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700175 int r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800176 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 len -= r;
178 p += r;
179 } else {
Yabin Cui7a3f8d62015-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 Projectdd7bc332009-03-03 19:32:55 -0800181 return -1;
182 }
183 }
184 return 0;
185}
186
Josh Gao1290fbf2016-11-22 14:32:34 -0800187static void transport_socket_events(int fd, unsigned events, void* _t) {
188 atransport* t = reinterpret_cast<atransport*>(_t);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700189 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
Josh Gao1290fbf2016-11-22 14:32:34 -0800190 if (events & FDE_READ) {
191 apacket* p = 0;
192 if (read_packet(fd, t->serial, &p)) {
Yabin Cui7a3f8d62015-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 Projectdd7bc332009-03-03 19:32:55 -0800194 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800195 handle_packet(p, (atransport*)_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
197 }
198}
199
Josh Gao06d61d42016-10-06 13:31:44 -0700200void send_packet(apacket* p, atransport* t) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayde471942017-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 Projectdd7bc332009-03-03 19:32:55 -0800208
209 print_packet("send", p);
210
211 if (t == NULL) {
Josh Gao06d61d42016-10-06 13:31:44 -0700212 fatal("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 }
214
Josh Gao06d61d42016-10-06 13:31:44 -0700215 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 fatal_errno("cannot enqueue packet on transport socket");
217 }
218}
219
Yabin Cuid6ab3c22015-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 Gaob5fea142016-02-12 14:31:15 -0800233static void read_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800234 atransport* t = reinterpret_cast<atransport*>(_t);
235 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236
Josh Gao1290fbf2016-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 Projectdd7bc332009-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 Gao1290fbf2016-11-22 14:32:34 -0800246 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700248 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 goto oops;
250 }
251
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700252 D("%s: data pump started", t->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800253 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800254 ATRACE_NAME("read_transport loop");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 p = get_apacket();
256
Josh Gaocfe72e22016-11-29 09:40:29 -0800257 {
258 ATRACE_NAME("read_transport read_remote");
Josh Gaob800d882018-01-28 20:32:46 -0800259 if (!t->connection->Read(p)) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800260 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800262 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 }
Josh Gaob800d882018-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 Cuib5e11412017-03-10 16:01:01 -0800271#if ADB_HOST
272 if (p->msg.command == 0) {
Josh Gaofb413a22018-01-29 18:09:20 -0800273 put_apacket(p);
Yabin Cuib5e11412017-03-10 16:01:01 -0800274 continue;
275 }
276#endif
Josh Gaocfe72e22016-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 Projectdd7bc332009-03-03 19:32:55 -0800281 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800282 D("%s: failed to write apacket to transport", t->serial);
283 goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 }
285 }
286
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700287 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-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 Gao1290fbf2016-11-22 14:32:34 -0800293 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700295 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 }
297
298oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700299 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 kick_transport(t);
301 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302}
303
Yabin Cuid6ab3c22015-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 Gaob5fea142016-02-12 14:31:15 -0800306static void write_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800307 atransport* t = reinterpret_cast<atransport*>(_t);
308 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 int active = 0;
310
Josh Gao1290fbf2016-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 Projectdd7bc332009-03-03 19:32:55 -0800314
Josh Gao1290fbf2016-11-22 14:32:34 -0800315 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800316 ATRACE_NAME("write_transport loop");
Josh Gao1290fbf2016-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 Projectdd7bc332009-03-03 19:32:55 -0800319 break;
320 }
Josh Gaocfe72e22016-11-29 09:40:29 -0800321
Josh Gao1290fbf2016-11-22 14:32:34 -0800322 if (p->msg.command == A_SYNC) {
323 if (p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700324 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 put_apacket(p);
326 break;
327 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800328 if (p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700329 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 active = 1;
331 } else {
Josh Gao1290fbf2016-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 Projectdd7bc332009-03-03 19:32:55 -0800333 }
334 }
335 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800336 if (active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700337 D("%s: transport got packet, sending to remote", t->serial);
Josh Gaocfe72e22016-11-29 09:40:29 -0800338 ATRACE_NAME("write_transport write_remote");
Yabin Cuib5e11412017-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 Projectdd7bc332009-03-03 19:32:55 -0800344 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700345 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 }
347 }
348
349 put_apacket(p);
350 }
351
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700352 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 kick_transport(t);
354 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355}
356
Yabin Cuif4b99282015-08-27 12:03:11 -0700357void kick_transport(atransport* t) {
Josh Gao1db71af2017-08-17 13:50:51 -0700358 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui1f4ec192016-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 Gaob122b172017-08-16 16:57:01 -0700361 //
362 // TODO(jmgao): WTF? Is this actually true?
Yabin Cui1f4ec192016-04-05 13:50:44 -0700363 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700364 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700365 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700366}
The Android Open Source Projectdd7bc332009-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 Projectdd7bc332009-03-03 19:32:55 -0800372#if ADB_HOST
The Android Open Source Projectdd7bc332009-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 Projectdd7bc332009-03-03 19:32:55 -0800379struct device_tracker {
Josh Gao1290fbf2016-11-22 14:32:34 -0800380 asocket socket;
Josh Gaoe0361d12018-02-12 17:24:00 -0800381 bool update_needed = false;
382 bool long_output = false;
383 device_tracker* next = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384};
385
386/* linked list of all device trackers */
Josh Gao1290fbf2016-11-22 14:32:34 -0800387static device_tracker* device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388
Josh Gao1290fbf2016-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 Projectdd7bc332009-03-03 19:32:55 -0800392
Josh Gao1db71af2017-08-17 13:50:51 -0700393 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 while (node) {
395 if (node == tracker) {
396 *pnode = node->next;
397 break;
398 }
399 pnode = &node->next;
Josh Gao1290fbf2016-11-22 14:32:34 -0800400 node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402}
403
Josh Gao1290fbf2016-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 Projectdd7bc332009-03-03 19:32:55 -0800407
Josh Gao1290fbf2016-11-22 14:32:34 -0800408 D("device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 if (peer) {
410 peer->peer = NULL;
411 peer->close(peer);
412 }
413 device_tracker_remove(tracker);
Josh Gaoe0361d12018-02-12 17:24:00 -0800414 delete tracker;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415}
416
Josh Gao27cb7dc2018-02-01 13:17:50 -0800417static int device_tracker_enqueue(asocket* socket, std::string) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 /* you can't read from a device tracker, close immediately */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 device_tracker_close(socket);
420 return -1;
421}
422
Elliott Hughese67f1f82015-04-30 17:32:03 -0700423static int device_tracker_send(device_tracker* tracker, const std::string& string) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700424 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425
Josh Gao27cb7dc2018-02-01 13:17:50 -0800426 std::string data;
427 data.resize(4 + string.size());
428 char buf[5];
429 snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size()));
430 memcpy(&data[0], buf, 4);
431 memcpy(&data[4], string.data(), string.size());
432 return peer->enqueue(peer, std::move(data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433}
434
Elliott Hughese67f1f82015-04-30 17:32:03 -0700435static void device_tracker_ready(asocket* socket) {
436 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437
Elliott Hughese67f1f82015-04-30 17:32:03 -0700438 // We want to send the device list when the tracker connects
439 // for the first time, even if no update occurred.
Josh Gaob0c18022017-08-14 18:57:54 -0700440 if (tracker->update_needed) {
441 tracker->update_needed = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442
Josh Gaob0c18022017-08-14 18:57:54 -0700443 std::string transports = list_transports(tracker->long_output);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700444 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 }
446}
447
Josh Gaob0c18022017-08-14 18:57:54 -0700448asocket* create_device_tracker(bool long_output) {
Josh Gaoe0361d12018-02-12 17:24:00 -0800449 device_tracker* tracker = new device_tracker();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700450 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451
Josh Gao1290fbf2016-11-22 14:32:34 -0800452 D("device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453
454 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao1290fbf2016-11-22 14:32:34 -0800455 tracker->socket.ready = device_tracker_ready;
456 tracker->socket.close = device_tracker_close;
Josh Gaob0c18022017-08-14 18:57:54 -0700457 tracker->update_needed = true;
458 tracker->long_output = long_output;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459
Josh Gao1290fbf2016-11-22 14:32:34 -0800460 tracker->next = device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 device_tracker_list = tracker;
462
463 return &tracker->socket;
464}
465
Josh Gaofd713e52017-05-03 22:37:10 -0700466// Check if all of the USB transports are connected.
467bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gao1db71af2017-08-17 13:50:51 -0700468 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaofd713e52017-05-03 22:37:10 -0700469 for (const auto& t : transport_list) {
470 if (!fn(t)) {
471 return false;
472 }
473 }
474 for (const auto& t : pending_list) {
475 if (!fn(t)) {
476 return false;
477 }
478 }
479 return true;
480}
481
Elliott Hughese67f1f82015-04-30 17:32:03 -0700482// Call this function each time the transport list has changed.
483void update_transports() {
Josh Gaofd713e52017-05-03 22:37:10 -0700484 update_transport_status();
485
486 // Notify `adb track-devices` clients.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700487 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488
Elliott Hughese67f1f82015-04-30 17:32:03 -0700489 device_tracker* tracker = device_tracker_list;
490 while (tracker != nullptr) {
491 device_tracker* next = tracker->next;
492 // This may destroy the tracker if the connection is closed.
493 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 tracker = next;
495 }
496}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700497
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700499
500void update_transports() {
501 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700503
Josh Gao1290fbf2016-11-22 14:32:34 -0800504#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505
Josh Gao1290fbf2016-11-22 14:32:34 -0800506struct tmsg {
507 atransport* transport;
508 int action;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509};
510
Josh Gao1290fbf2016-11-22 14:32:34 -0800511static int transport_read_action(int fd, struct tmsg* m) {
512 char* p = (char*)m;
513 int len = sizeof(*m);
514 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515
Josh Gao1290fbf2016-11-22 14:32:34 -0800516 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800518 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800520 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700522 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 return -1;
524 }
525 }
526 return 0;
527}
528
Josh Gao1290fbf2016-11-22 14:32:34 -0800529static int transport_write_action(int fd, struct tmsg* m) {
530 char* p = (char*)m;
531 int len = sizeof(*m);
532 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533
Josh Gao1290fbf2016-11-22 14:32:34 -0800534 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800536 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800538 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700540 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 return -1;
542 }
543 }
544 return 0;
545}
546
Josh Gao1290fbf2016-11-22 14:32:34 -0800547static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 int s[2];
Josh Gao1290fbf2016-11-22 14:32:34 -0800550 atransport* t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551
Josh Gao1290fbf2016-11-22 14:32:34 -0800552 if (!(ev & FDE_READ)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 return;
554 }
555
Josh Gao1290fbf2016-11-22 14:32:34 -0800556 if (transport_read_action(_fd, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 fatal_errno("cannot read transport registration socket");
558 }
559
560 t = m.transport;
561
Dan Albert1792c232015-05-18 13:06:53 -0700562 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700563 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564
Josh Gao1290fbf2016-11-22 14:32:34 -0800565 /* IMPORTANT: the remove closes one half of the
566 ** socket pair. The close closes the other half.
567 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 fdevent_remove(&(t->transport_fde));
569 adb_close(t->fd);
570
Josh Gao0cd3ae12016-09-21 12:37:10 -0700571 {
Josh Gao1db71af2017-08-17 13:50:51 -0700572 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700573 transport_list.remove(t);
574 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575
Josh Gao1290fbf2016-11-22 14:32:34 -0800576 if (t->product) free(t->product);
577 if (t->serial) free(t->serial);
578 if (t->model) free(t->model);
579 if (t->device) free(t->device);
580 if (t->devpath) free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581
Dan Albertc7915a32015-05-18 16:46:31 -0700582 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583
584 update_transports();
585 return;
586 }
587
Mike Lockwood0927bf92009-08-08 12:37:44 -0400588 /* don't create transport threads for inaccessible devices */
Yabin Cuib5e11412017-03-10 16:01:01 -0800589 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400591 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592
Dan Albertc7915a32015-05-18 16:46:31 -0700593 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400594 fatal_errno("cannot open transport socketpair");
595 }
596
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700597 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400598
599 t->transport_socket = s[0];
600 t->fd = s[1];
601
Josh Gao1290fbf2016-11-22 14:32:34 -0800602 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400603
604 fdevent_set(&(t->transport_fde), FDE_READ);
605
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700606 std::thread(write_transport_thread, t).detach();
607 std::thread(read_transport_thread, t).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 }
609
Josh Gao0cd3ae12016-09-21 12:37:10 -0700610 {
Josh Gao1db71af2017-08-17 13:50:51 -0700611 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700612 pending_list.remove(t);
613 transport_list.push_front(t);
614 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616 update_transports();
617}
618
Josh Gao1290fbf2016-11-22 14:32:34 -0800619void init_transport_registration(void) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 int s[2];
621
Josh Gao1290fbf2016-11-22 14:32:34 -0800622 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 fatal_errno("cannot open transport registration socketpair");
624 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700625 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626
627 transport_registration_send = s[0];
628 transport_registration_recv = s[1];
629
Josh Gao1290fbf2016-11-22 14:32:34 -0800630 fdevent_install(&transport_registration_fde, transport_registration_recv,
631 transport_registration_func, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632
633 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao01b7bc42017-05-09 13:43:35 -0700634}
635
636void kick_all_transports() {
637 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gao1db71af2017-08-17 13:50:51 -0700638 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao01b7bc42017-05-09 13:43:35 -0700639 for (auto t : transport_list) {
640 t->Kick();
641 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642}
643
644/* the fdevent select pump is single threaded */
Josh Gao1290fbf2016-11-22 14:32:34 -0800645static void register_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 tmsg m;
647 m.transport = transport;
648 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700649 D("transport: %s registered", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800650 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651 fatal_errno("cannot write transport registration socket\n");
652 }
653}
654
Josh Gao1290fbf2016-11-22 14:32:34 -0800655static void remove_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 tmsg m;
657 m.transport = transport;
658 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700659 D("transport: %s removed", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800660 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 fatal_errno("cannot write transport registration socket\n");
662 }
663}
664
Yabin Cuif4b99282015-08-27 12:03:11 -0700665static void transport_unref(atransport* t) {
666 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700667
Josh Gaoe48ecce2017-09-13 13:40:57 -0700668 std::lock_guard<std::recursive_mutex> lock(transport_lock);
669 CHECK_GT(t->ref_count, 0u);
670 t->ref_count--;
671 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700672 D("transport: %s unref (kicking and closing)", t->serial);
Josh Gaob800d882018-01-28 20:32:46 -0800673 t->connection->Close();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400674 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100675 } else {
Josh Gaoe48ecce2017-09-13 13:40:57 -0700676 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400677 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678}
679
Josh Gao1290fbf2016-11-22 14:32:34 -0800680static int qual_match(const char* to_test, const char* prefix, const char* qual,
681 bool sanitize_qual) {
682 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700683 return !qual || !*qual;
684
Josh Gao1290fbf2016-11-22 14:32:34 -0800685 if (!qual) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700686
687 if (prefix) {
688 while (*prefix) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800689 if (*prefix++ != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700690 }
691 }
692
693 while (*qual) {
694 char ch = *qual++;
Josh Gao1290fbf2016-11-22 14:32:34 -0800695 if (sanitize_qual && !isalnum(ch)) ch = '_';
696 if (ch != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700697 }
698
699 /* Everything matched so far. Return true if *to_test is a NUL. */
700 return !*to_test;
701}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702
Josh Gaob122b172017-08-16 16:57:01 -0700703atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
704 bool* is_ambiguous, std::string* error_out,
705 bool accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700706 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707
Josh Gaob122b172017-08-16 16:57:01 -0700708 if (transport_id != 0) {
709 *error_out =
710 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
711 } else if (serial) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700712 *error_out = android::base::StringPrintf("device '%s' not found", serial);
713 } else if (type == kTransportLocal) {
714 *error_out = "no emulators found";
715 } else if (type == kTransportAny) {
716 *error_out = "no devices/emulators found";
717 } else {
718 *error_out = "no devices found";
719 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720
Josh Gao1db71af2017-08-17 13:50:51 -0700721 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700722 for (const auto& t : transport_list) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800723 if (t->GetConnectionState() == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800724#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800725 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800726#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400727 continue;
728 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400729
Josh Gaob122b172017-08-16 16:57:01 -0700730 if (transport_id) {
731 if (t->id == transport_id) {
732 result = t;
733 break;
734 }
735 } else if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800736 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700737 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700738 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700739 if (is_ambiguous) *is_ambiguous = true;
740 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700741 break;
742 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700744 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700746 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700748 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700749 if (is_ambiguous) *is_ambiguous = true;
750 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751 break;
752 }
753 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700754 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700756 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700757 if (is_ambiguous) *is_ambiguous = true;
758 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759 break;
760 }
761 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700762 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700764 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700765 if (is_ambiguous) *is_ambiguous = true;
766 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800767 break;
768 }
769 result = t;
770 }
771 }
772 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700773 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774
Elliott Hughes8d28e192015-10-07 14:55:10 -0700775 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800776 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700777 *error_out = "device unauthorized.\n";
778 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
779 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
780 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
781 *error_out += "\n";
782 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
783 *error_out += "Otherwise check for a confirmation dialog on your device.";
784 result = nullptr;
785 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800786
Elliott Hughes8d28e192015-10-07 14:55:10 -0700787 // Don't return offline devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800788 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700789 *error_out = "device offline";
790 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 }
792
793 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700794 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795 }
796
797 return result;
798}
799
Yabin Cuib5e11412017-03-10 16:01:01 -0800800int atransport::Write(apacket* p) {
Josh Gaob800d882018-01-28 20:32:46 -0800801 return this->connection->Write(p) ? 0 : -1;
Yabin Cuib5e11412017-03-10 16:01:01 -0800802}
803
Yabin Cui7f274902016-04-18 11:22:34 -0700804void atransport::Kick() {
805 if (!kicked_) {
Josh Gaob800d882018-01-28 20:32:46 -0800806 D("kicking transport %s", this->serial);
Yabin Cui7f274902016-04-18 11:22:34 -0700807 kicked_ = true;
Josh Gaob800d882018-01-28 20:32:46 -0800808 this->connection->Close();
Yabin Cui7f274902016-04-18 11:22:34 -0700809 }
810}
811
Yabin Cuib5e11412017-03-10 16:01:01 -0800812ConnectionState atransport::GetConnectionState() const {
813 return connection_state_;
814}
815
816void atransport::SetConnectionState(ConnectionState state) {
817 check_main_thread();
818 connection_state_ = state;
819}
820
David Purselld2acbd12015-12-02 15:14:31 -0800821const std::string atransport::connection_state_name() const {
Yabin Cuib5e11412017-03-10 16:01:01 -0800822 ConnectionState state = GetConnectionState();
823 switch (state) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800824 case kCsOffline:
825 return "offline";
826 case kCsBootloader:
827 return "bootloader";
828 case kCsDevice:
829 return "device";
830 case kCsHost:
831 return "host";
832 case kCsRecovery:
833 return "recovery";
834 case kCsNoPerm:
835 return UsbNoPermissionsShortHelpText();
836 case kCsSideload:
837 return "sideload";
838 case kCsUnauthorized:
839 return "unauthorized";
840 default:
841 return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800842 }
843}
844
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100845void atransport::update_version(int version, size_t payload) {
846 protocol_version = std::min(version, A_VERSION);
847 max_payload = std::min(payload, MAX_PAYLOAD);
848}
849
850int atransport::get_protocol_version() const {
851 return protocol_version;
852}
853
854size_t atransport::get_max_payload() const {
855 return max_payload;
856}
857
David Pursell4e2fd362015-09-22 10:43:08 -0700858namespace {
David Pursell0955c662015-08-31 10:42:13 -0700859
David Pursell4e2fd362015-09-22 10:43:08 -0700860constexpr char kFeatureStringDelimiter = ',';
861
862} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700863
864const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700865 // Local static allocation to avoid global non-POD variables.
866 static const FeatureSet* features = new FeatureSet{
Josh Gao1290fbf2016-11-22 14:32:34 -0800867 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursellbbe3d212015-09-25 08:37:13 -0700868 // Increment ADB_SERVER_VERSION whenever the feature list changes to
869 // make sure that the adb client and server features stay in sync
870 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700871 };
872
873 return *features;
874}
875
876std::string FeatureSetToString(const FeatureSet& features) {
877 return android::base::Join(features, kFeatureStringDelimiter);
878}
879
880FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700881 if (features_string.empty()) {
882 return FeatureSet();
883 }
884
Josh Gao1290fbf2016-11-22 14:32:34 -0800885 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursell4e2fd362015-09-22 10:43:08 -0700886 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700887}
888
David Pursell70ef7b42015-09-30 13:35:42 -0700889bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800890 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell70ef7b42015-09-30 13:35:42 -0700891}
892
Dan Albert1792c232015-05-18 13:06:53 -0700893bool atransport::has_feature(const std::string& feature) const {
894 return features_.count(feature) > 0;
895}
896
David Pursell4e2fd362015-09-22 10:43:08 -0700897void atransport::SetFeatures(const std::string& features_string) {
898 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700899}
900
Yabin Cuib3298242015-08-28 15:09:44 -0700901void atransport::AddDisconnect(adisconnect* disconnect) {
902 disconnects_.push_back(disconnect);
903}
904
905void atransport::RemoveDisconnect(adisconnect* disconnect) {
906 disconnects_.remove(disconnect);
907}
908
909void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700910 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700911 disconnect->func(disconnect->opaque, this);
912 }
913 disconnects_.clear();
914}
915
David Pursell3f902aa2016-03-01 08:58:26 -0800916bool atransport::MatchesTarget(const std::string& target) const {
917 if (serial) {
918 if (target == serial) {
919 return true;
920 } else if (type == kTransportLocal) {
921 // Local transports can match [tcp:|udp:]<hostname>[:port].
922 const char* local_target_ptr = target.c_str();
923
924 // For fastboot compatibility, ignore protocol prefixes.
925 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao1290fbf2016-11-22 14:32:34 -0800926 android::base::StartsWith(target, "udp:")) {
David Pursell3f902aa2016-03-01 08:58:26 -0800927 local_target_ptr += 4;
928 }
929
930 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
931 std::string serial_host, error;
932 int serial_port = -1;
Josh Gao1290fbf2016-11-22 14:32:34 -0800933 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursell3f902aa2016-03-01 08:58:26 -0800934 // |target| may omit the port to default to ours.
935 std::string target_host;
936 int target_port = serial_port;
937 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
938 nullptr, &error) &&
Josh Gao1290fbf2016-11-22 14:32:34 -0800939 serial_host == target_host && serial_port == target_port) {
David Pursell3f902aa2016-03-01 08:58:26 -0800940 return true;
941 }
942 }
943 }
944 }
945
946 return (devpath && target == devpath) ||
947 qual_match(target.c_str(), "product:", product, false) ||
948 qual_match(target.c_str(), "model:", model, true) ||
949 qual_match(target.c_str(), "device:", device, false);
950}
951
Elliott Hughese67f1f82015-04-30 17:32:03 -0700952#if ADB_HOST
953
Josh Gaob122b172017-08-16 16:57:01 -0700954// We use newline as our delimiter, make sure to never output it.
955static std::string sanitize(std::string str, bool alphanumeric) {
956 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
957 : [](const char c) { return c == '\n'; };
958 std::replace_if(str.begin(), str.end(), pred, '_');
959 return str;
960}
961
Josh Gao1290fbf2016-11-22 14:32:34 -0800962static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob122b172017-08-16 16:57:01 -0700963 bool alphanumeric) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700964 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700965 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700966 }
967
Elliott Hughese67f1f82015-04-30 17:32:03 -0700968 *result += ' ';
969 *result += key;
Josh Gaob122b172017-08-16 16:57:01 -0700970 *result += sanitize(value, alphanumeric);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700971}
972
Josh Gao1290fbf2016-11-22 14:32:34 -0800973static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700974 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700975 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700976 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700977 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700978
979 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700980 *result += serial;
981 *result += '\t';
982 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700983 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800984 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700985
Elliott Hughese67f1f82015-04-30 17:32:03 -0700986 append_transport_info(result, "", t->devpath, false);
987 append_transport_info(result, "product:", t->product, false);
988 append_transport_info(result, "model:", t->model, true);
989 append_transport_info(result, "device:", t->device, false);
Josh Gaob122b172017-08-16 16:57:01 -0700990
991 // Put id at the end, so that anyone parsing the output here can always find it by scanning
992 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
993 *result += " transport_id:";
994 *result += std::to_string(t->id);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700995 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700996 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700997}
998
Elliott Hughese67f1f82015-04-30 17:32:03 -0700999std::string list_transports(bool long_listing) {
Josh Gao1db71af2017-08-17 13:50:51 -07001000 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov04398a92017-12-17 10:56:07 +00001001
1002 auto sorted_transport_list = transport_list;
1003 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
1004 if (x->type != y->type) {
1005 return x->type < y->type;
1006 }
1007 return strcmp(x->serial, y->serial) < 0;
1008 });
1009
1010 std::string result;
1011 for (const auto& t : sorted_transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -07001012 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001013 }
Elliott Hughese67f1f82015-04-30 17:32:03 -07001014 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015}
1016
Josh Gao22d2b3e2016-10-27 14:01:08 -07001017void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gao1db71af2017-08-17 13:50:51 -07001018 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao22d2b3e2016-10-27 14:01:08 -07001019 for (auto& t : transport_list) {
1020 if (predicate(t)) {
1021 t->Kick();
1022 }
1023 }
1024}
1025
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001026/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -07001027void close_usb_devices() {
Josh Gao22d2b3e2016-10-27 14:01:08 -07001028 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029}
Josh Gao1290fbf2016-11-22 14:32:34 -08001030#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001031
Josh Gao1290fbf2016-11-22 14:32:34 -08001032int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertc7915a32015-05-18 16:46:31 -07001033 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +01001034
1035 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001036 char buf[32];
1037 snprintf(buf, sizeof(buf), "T-%p", t);
1038 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +01001039 }
Dan Albertc7915a32015-05-18 16:46:31 -07001040
Yabin Cui7a3f8d62015-09-02 17:44:28 -07001041 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001042 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -07001043 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001044 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001045 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001046
Josh Gao1db71af2017-08-17 13:50:51 -07001047 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -07001048 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001049 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001050 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001051 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001052 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001053 return -1;
1054 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001055 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001056
Elliott Hughes65fe2512015-10-07 15:59:35 -07001057 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001058 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001059 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001060 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001061 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001062 return -1;
1063 }
1064 }
1065
Dan Albertc7915a32015-05-18 16:46:31 -07001066 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001067 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001068
1069 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -07001070
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001071 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001072 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001073}
1074
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001075#if ADB_HOST
Josh Gao1290fbf2016-11-22 14:32:34 -08001076atransport* find_transport(const char* serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001077 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001078
Josh Gao1db71af2017-08-17 13:50:51 -07001079 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001080 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001081 if (t->serial && strcmp(serial, t->serial) == 0) {
1082 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001083 break;
1084 }
Dan Albertc7915a32015-05-18 16:46:31 -07001085 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001086
Dan Albertc7915a32015-05-18 16:46:31 -07001087 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001088}
1089
Yabin Cuif4b99282015-08-27 12:03:11 -07001090void kick_all_tcp_devices() {
Josh Gao1db71af2017-08-17 13:50:51 -07001091 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001092 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001093 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -07001094 // Kicking breaks the read_transport thread of this transport out of any read, then
1095 // the read_transport thread will notify the main thread to make this transport
1096 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001097 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -07001098 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001099 }
Dan Albertc7915a32015-05-18 16:46:31 -07001100 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001101}
1102
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001103#endif
1104
Josh Gao1290fbf2016-11-22 14:32:34 -08001105void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1106 unsigned writeable) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001107 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertc7915a32015-05-18 16:46:31 -07001108
Josh Gao1290fbf2016-11-22 14:32:34 -08001109 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cuib5e11412017-03-10 16:01:01 -08001110 init_usb_transport(t, usb);
Josh Gao1290fbf2016-11-22 14:32:34 -08001111 if (serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001112 t->serial = strdup(serial);
1113 }
Dan Albertc7915a32015-05-18 16:46:31 -07001114
1115 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001116 t->devpath = strdup(devpath);
1117 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001118
Josh Gao0cd3ae12016-09-21 12:37:10 -07001119 {
Josh Gao1db71af2017-08-17 13:50:51 -07001120 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001121 pending_list.push_front(t);
1122 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124 register_transport(t);
1125}
1126
Dan Albertdcd78a12015-05-18 16:43:57 -07001127// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao1290fbf2016-11-22 14:32:34 -08001128void unregister_usb_transport(usb_handle* usb) {
Josh Gao1db71af2017-08-17 13:50:51 -07001129 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaob800d882018-01-28 20:32:46 -08001130 transport_list.remove_if([usb](atransport* t) {
1131 if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) {
1132 return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
1133 }
1134 return false;
1135 });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001136}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001137
Josh Gao36dadca2017-05-16 15:02:45 -07001138bool check_header(apacket* p, atransport* t) {
Josh Gao1290fbf2016-11-22 14:32:34 -08001139 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001140 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1141 << ", magic = " << p->msg.magic;
Josh Gao36dadca2017-05-16 15:02:45 -07001142 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001143 }
1144
Josh Gao1290fbf2016-11-22 14:32:34 -08001145 if (p->msg.data_length > t->get_max_payload()) {
1146 VLOG(RWX) << "check_header(): " << p->msg.data_length
1147 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao36dadca2017-05-16 15:02:45 -07001148 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149 }
1150
Josh Gao36dadca2017-05-16 15:02:45 -07001151 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001152}
1153
Josh Gao3bd28792016-10-05 19:02:29 -07001154#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001155std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001156 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1157
Josh Gao2e671202016-08-18 22:00:12 -07001158 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001159 keys_.pop_front();
1160 return result;
1161}
Josh Gao3bd28792016-10-05 19:02:29 -07001162#endif