blob: 12b98ba3b933da27579d4f075e75d3615cd0a80a [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 Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "transport.h"
21
Dan Albertb302d122015-02-24 15:51:19 -080022#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080026#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027
Pirama Arumuga Nainarbd4d4e12016-09-06 13:34:42 -070028#include <condition_variable>
Josh Gaoe7daf572016-09-21 12:37:10 -070029#include <mutex>
Elliott Hughes73925982016-11-15 12:37:32 -080030#include <thread>
Yabin Cuif401ead2016-04-29 16:53:52 -070031#include <vector>
32
Casey Dahlin3122cdf2016-06-23 14:19:37 -070033#include <android-base/parsenetaddress.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080034#include <android-base/stringprintf.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070035#include <cutils/sockets.h>
Elliott Hughesfb596842015-05-01 17:04:38 -070036
Dan Albertb302d122015-02-24 15:51:19 -080037#if !ADB_HOST
Elliott Hughes8b249d22016-09-23 15:40:03 -070038#include <android-base/properties.h>
Dan Albertb302d122015-02-24 15:51:19 -080039#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070040
41#include "adb.h"
42#include "adb_io.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070043#include "adb_utils.h"
Josh Gao70267e42016-11-15 18:55:47 -080044#include "sysdeps/chrono.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046#if ADB_HOST
Josh Gao89513a52016-05-06 02:37:24 -070047
48// Android Wear has been using port 5601 in all of its documentation/tooling,
49// but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX].
50// Avoid stomping on their port by limiting the number of emulators that can be
51// connected.
52#define ADB_LOCAL_TRANSPORT_MAX 16
53
Josh Gaoe7daf572016-09-21 12:37:10 -070054static std::mutex& local_transports_lock = *new std::mutex();
Josh Gao89513a52016-05-06 02:37:24 -070055
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010056/* we keep a list of opened transports. The atransport struct knows to which
57 * local transport it is connected. The list is used to detect when we're
58 * trying to connect twice to a given local transport.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
61#endif /* ADB_HOST */
62
63static int remote_read(apacket *p, atransport *t)
64{
Dan Albert66a91b02015-02-24 21:26:58 -080065 if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
Yabin Cui815ad882015-09-02 17:44:28 -070066 D("remote local: read terminated (message)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067 return -1;
68 }
69
Tamas Berghammera1c60c02015-07-13 19:12:28 +010070 if(check_header(p, t)) {
Yabin Cui815ad882015-09-02 17:44:28 -070071 D("bad header: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080072 return -1;
73 }
74
Dan Albert66a91b02015-02-24 21:26:58 -080075 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
Yabin Cui815ad882015-09-02 17:44:28 -070076 D("remote local: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080077 return -1;
78 }
79
80 if(check_data(p)) {
Yabin Cui815ad882015-09-02 17:44:28 -070081 D("bad data: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082 return -1;
83 }
84
85 return 0;
86}
87
88static int remote_write(apacket *p, atransport *t)
89{
90 int length = p->msg.data_length;
91
Dan Albert66a91b02015-02-24 21:26:58 -080092 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
Yabin Cui815ad882015-09-02 17:44:28 -070093 D("remote local: write terminated");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080094 return -1;
95 }
96
97 return 0;
98}
99
Yabin Cuif401ead2016-04-29 16:53:52 -0700100bool local_connect(int port) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700101 std::string dummy;
Yabin Cuif401ead2016-04-29 16:53:52 -0700102 return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100103}
104
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700105void connect_device(const std::string& address, std::string* response) {
106 if (address.empty()) {
107 *response = "empty address";
108 return;
109 }
110
111 std::string serial;
112 std::string host;
113 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
114 if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) {
115 return;
116 }
117
118 std::string error;
119 int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error);
120 if (fd == -1) {
121 *response = android::base::StringPrintf("unable to connect to %s: %s",
122 serial.c_str(), error.c_str());
123 return;
124 }
125
126 D("client: connected %s remote on fd %d", serial.c_str(), fd);
127 close_on_exec(fd);
128 disable_tcp_nagle(fd);
129
130 // Send a TCP keepalive ping to the device every second so we can detect disconnects.
131 if (!set_tcp_keepalive(fd, 1)) {
132 D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
133 }
134
135 int ret = register_socket_transport(fd, serial.c_str(), port, 0);
136 if (ret < 0) {
137 adb_close(fd);
138 *response = android::base::StringPrintf("already connected to %s", serial.c_str());
139 } else {
140 *response = android::base::StringPrintf("connected to %s", serial.c_str());
141 }
142}
143
144
Elliott Hughes43df1092015-07-23 17:12:58 -0700145int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
146 int fd = -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800147
148#if ADB_HOST
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700149 if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
150 find_emulator_transport_by_console_port(console_port) != nullptr) {
Yabin Cuid802bcf2015-07-31 14:10:54 -0700151 return -1;
152 }
153
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800154 const char *host = getenv("ADBHOST");
155 if (host) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700156 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800157 }
158#endif
159 if (fd < 0) {
Spencer Low753d4852015-07-30 23:07:55 -0700160 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800161 }
162
163 if (fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700164 D("client: connected on remote on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800165 close_on_exec(fd);
166 disable_tcp_nagle(fd);
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700167 std::string serial = getEmulatorSerialString(console_port);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700168 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
169 return 0;
170 }
171 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800172 }
173 return -1;
174}
175
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700176#if ADB_HOST
Yabin Cuif401ead2016-04-29 16:53:52 -0700177
178static void PollAllLocalPortsForEmulator() {
179 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
180 int count = ADB_LOCAL_TRANSPORT_MAX;
181
182 // Try to connect to any number of running emulator instances.
183 for ( ; count > 0; count--, port += 2 ) {
184 local_connect(port);
185 }
186}
187
188// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
189constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
Josh Gao70267e42016-11-15 18:55:47 -0800190constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s;
Yabin Cuif401ead2016-04-29 16:53:52 -0700191
192struct RetryPort {
193 int port;
194 uint32_t retry_count;
195};
196
197// Retry emulators just kicked.
198static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
199std::mutex &retry_ports_lock = *new std::mutex;
200std::condition_variable &retry_ports_cond = *new std::condition_variable;
201
Josh Gao382ea6e2016-02-12 14:31:15 -0800202static void client_socket_thread(void* x) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700203 adb_thread_setname("client_socket_thread");
Yabin Cui815ad882015-09-02 17:44:28 -0700204 D("transport: client_socket_thread() starting");
Yabin Cuif401ead2016-04-29 16:53:52 -0700205 PollAllLocalPortsForEmulator();
Yabin Cuid802bcf2015-07-31 14:10:54 -0700206 while (true) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700207 std::vector<RetryPort> ports;
208 // Collect retry ports.
209 {
210 std::unique_lock<std::mutex> lock(retry_ports_lock);
211 while (retry_ports.empty()) {
212 retry_ports_cond.wait(lock);
213 }
214 retry_ports.swap(ports);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700215 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700216 // Sleep here instead of the end of loop, because if we immediately try to reconnect
217 // the emulator just kicked, the adbd on the emulator may not have time to remove the
218 // just kicked transport.
Elliott Hughes73925982016-11-15 12:37:32 -0800219 std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL);
Yabin Cuif401ead2016-04-29 16:53:52 -0700220
221 // Try connecting retry ports.
222 std::vector<RetryPort> next_ports;
223 for (auto& port : ports) {
224 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
225 << port.retry_count;
226 if (local_connect(port.port)) {
227 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
228 continue;
229 }
230 if (--port.retry_count > 0) {
231 next_ports.push_back(port);
232 } else {
233 VLOG(TRANSPORT) << "stop retrying port " << port.port;
234 }
235 }
236
237 // Copy back left retry ports.
238 {
239 std::unique_lock<std::mutex> lock(retry_ports_lock);
240 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
241 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800242 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243}
244
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700245#else // ADB_HOST
246
Josh Gao382ea6e2016-02-12 14:31:15 -0800247static void server_socket_thread(void* arg) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800248 int serverfd, fd;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800249 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250
Siva Velusamy2669cf92015-08-28 16:37:29 -0700251 adb_thread_setname("server socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700252 D("transport: server_socket_thread() starting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800253 serverfd = -1;
254 for(;;) {
255 if(serverfd == -1) {
Spencer Low753d4852015-07-30 23:07:55 -0700256 std::string error;
257 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 if(serverfd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700259 D("server: cannot bind socket yet: %s", error.c_str());
Josh Gao70267e42016-11-15 18:55:47 -0800260 std::this_thread::sleep_for(1s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 continue;
262 }
263 close_on_exec(serverfd);
264 }
265
Yabin Cui815ad882015-09-02 17:44:28 -0700266 D("server: trying to get new connection from %d", port);
Josh Gao782d8d42016-08-23 15:41:56 -0700267 fd = adb_socket_accept(serverfd, nullptr, nullptr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800268 if(fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700269 D("server: new connection on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800270 close_on_exec(fd);
271 disable_tcp_nagle(fd);
Tao Wu1b29a3f2016-10-21 15:43:57 -0700272 std::string serial = android::base::StringPrintf("host-%d", fd);
273 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0) {
Yabin Cuic7221782016-05-17 15:14:25 -0700274 adb_close(fd);
275 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 }
277 }
Yabin Cui815ad882015-09-02 17:44:28 -0700278 D("transport: server_socket_thread() exiting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279}
280
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800281/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800282/*
283 * Redefine open and write for qemu_pipe.h that contains inlined references
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200284 * to those routines. We will redefine them back after qemu_pipe.h inclusion.
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800285 */
286#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200287#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800288#undef write
289#define open adb_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200290#define read adb_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800291#define write adb_write
bohu90a18d22017-03-29 12:19:40 -0700292#include <qemu_pipe.h>
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800293#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200294#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800295#undef write
296#define open ___xxx_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200297#define read ___xxx_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800298#define write ___xxx_write
299
300/* A worker thread that monitors host connections, and registers a transport for
301 * every new host connection. This thread replaces server_socket_thread on
302 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
303 * pipe to communicate with adbd daemon inside the guest. This is done in order
304 * to provide more robust communication channel between ADB host and guest. The
305 * main issue with server_socket_thread approach is that it runs on top of TCP,
306 * and thus is sensitive to network disruptions. For instance, the
307 * ConnectionManager may decide to reset all network connections, in which case
308 * the connection between ADB host and guest will be lost. To make ADB traffic
309 * independent from the network, we use here 'adb' QEMUD service to transfer data
310 * between the host, and the guest. See external/qemu/android/adb-*.* that
311 * implements the emulator's side of the protocol. Another advantage of using
312 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
313 * anymore on network being set up.
314 * The guest side of the protocol contains the following phases:
315 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
316 * is opened, and it becomes clear whether or not emulator supports that
317 * protocol.
318 * - Wait for the ADB host to create connection with the guest. This is done by
319 * sending an 'accept' request to the adb QEMUD service, and waiting on
320 * response.
321 * - When new ADB host connection is accepted, the connection with adb QEMUD
322 * service is registered as the transport, and a 'start' request is sent to the
323 * adb QEMUD service, indicating that the guest is ready to receive messages.
324 * Note that the guest will ignore messages sent down from the emulator before
325 * the transport registration is completed. That's why we need to send the
326 * 'start' request after the transport is registered.
327 */
Josh Gao382ea6e2016-02-12 14:31:15 -0800328static void qemu_socket_thread(void* arg) {
329 /* 'accept' request to the adb QEMUD service. */
330 static const char _accept_req[] = "accept";
331 /* 'start' request to the adb QEMUD service. */
332 static const char _start_req[] = "start";
333 /* 'ok' reply from the adb QEMUD service. */
334 static const char _ok_resp[] = "ok";
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800335
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800336 const int port = (int) (uintptr_t) arg;
bohuf4ae8fa2016-02-29 13:13:19 -0800337 int fd;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800338 char tmp[256];
339 char con_name[32];
340
Siva Velusamy2669cf92015-08-28 16:37:29 -0700341 adb_thread_setname("qemu socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700342 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800343
344 /* adb QEMUD service connection request. */
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200345 snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800346
347 /* Connect to the adb QEMUD service. */
348 fd = qemu_pipe_open(con_name);
349 if (fd < 0) {
350 /* This could be an older version of the emulator, that doesn't
351 * implement adb QEMUD service. Fall back to the old TCP way. */
Yabin Cui815ad882015-09-02 17:44:28 -0700352 D("adb service is not available. Falling back to TCP socket.");
Elliott Hughesf2517142015-05-05 13:41:21 -0700353 adb_thread_create(server_socket_thread, arg);
Josh Gao382ea6e2016-02-12 14:31:15 -0800354 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800355 }
356
357 for(;;) {
358 /*
359 * Wait till the host creates a new connection.
360 */
361
362 /* Send the 'accept' request. */
bohuf4ae8fa2016-02-29 13:13:19 -0800363 if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800364 /* Wait for the response. In the response we expect 'ok' on success,
365 * or 'ko' on failure. */
bohuf4ae8fa2016-02-29 13:13:19 -0800366 if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700367 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800368 adb_close(fd);
369 } else {
370 /* Host is connected. Register the transport, and start the
371 * exchange. */
Prathmesh Prabhuefa49792016-03-09 13:51:30 -0800372 std::string serial = android::base::StringPrintf("host-%d", fd);
Yabin Cuic7221782016-05-17 15:14:25 -0700373 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
374 !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
bohuf4ae8fa2016-02-29 13:13:19 -0800375 adb_close(fd);
376 }
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800377 }
378
379 /* Prepare for accepting of the next ADB host connection. */
380 fd = qemu_pipe_open(con_name);
381 if (fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700382 D("adb service become unavailable.");
Josh Gao382ea6e2016-02-12 14:31:15 -0800383 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800384 }
385 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700386 D("Unable to send the '%s' request to ADB service.", _accept_req);
Josh Gao382ea6e2016-02-12 14:31:15 -0800387 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800388 }
389 }
Yabin Cui815ad882015-09-02 17:44:28 -0700390 D("transport: qemu_socket_thread() exiting");
Josh Gao382ea6e2016-02-12 14:31:15 -0800391 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800392}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800393#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800394
Mike Lockwood26b88e32009-08-24 15:58:40 -0700395void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396{
Josh Gao382ea6e2016-02-12 14:31:15 -0800397 adb_thread_func_t func;
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700398 const char* debug_name = "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800399
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800400#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700401 func = client_socket_thread;
402 debug_name = "client";
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800403#else
Elliott Hughes8b249d22016-09-23 15:40:03 -0700404 // For the adbd daemon in the system image we need to distinguish
405 // between the device, and the emulator.
406 if (android::base::GetBoolProperty("ro.kernel.qemu", false)) {
407 // Running inside the emulator: use QEMUD pipe as the transport.
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700408 func = qemu_socket_thread;
409 } else {
Elliott Hughes8b249d22016-09-23 15:40:03 -0700410 // Running inside the device: use TCP socket as the transport.
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700411 func = server_socket_thread;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800412 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700413 debug_name = "server";
414#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800415
Yabin Cui815ad882015-09-02 17:44:28 -0700416 D("transport: local %s init", debug_name);
Elliott Hughesf2517142015-05-05 13:41:21 -0700417 if (!adb_thread_create(func, (void *) (uintptr_t) port)) {
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700418 fatal_errno("cannot create local socket %s thread", debug_name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800419 }
420}
421
422static void remote_kick(atransport *t)
423{
424 int fd = t->sfd;
425 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400426 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427 adb_close(fd);
428
429#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700430 int nn;
Josh Gaoe7daf572016-09-21 12:37:10 -0700431 std::lock_guard<std::mutex> lock(local_transports_lock);
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700432 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
433 if (local_transports[nn] == t) {
434 local_transports[nn] = NULL;
435 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800436 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800437 }
438#endif
439}
440
441static void remote_close(atransport *t)
442{
Spencer Low6f38e232015-06-09 12:32:17 -0700443 int fd = t->sfd;
444 if (fd != -1) {
445 t->sfd = -1;
446 adb_close(fd);
447 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700448#if ADB_HOST
449 int local_port;
450 if (t->GetLocalPortForEmulator(&local_port)) {
451 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port;
452 std::unique_lock<std::mutex> lock(retry_ports_lock);
453 RetryPort port;
454 port.port = local_port;
455 port.retry_count = LOCAL_PORT_RETRY_COUNT;
456 retry_ports.push_back(port);
457 retry_ports_cond.notify_one();
458 }
459#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800460}
461
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100462
463#if ADB_HOST
464/* Only call this function if you already hold local_transports_lock. */
Yabin Cuif401ead2016-04-29 16:53:52 -0700465static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100466{
467 int i;
468 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700469 int local_port;
470 if (local_transports[i] && local_transports[i]->GetLocalPortForEmulator(&local_port)) {
471 if (local_port == adb_port) {
472 return local_transports[i];
473 }
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100474 }
475 }
476 return NULL;
477}
478
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700479std::string getEmulatorSerialString(int console_port)
480{
481 return android::base::StringPrintf("emulator-%d", console_port);
482}
483
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100484atransport* find_emulator_transport_by_adb_port(int adb_port)
485{
Josh Gaoe7daf572016-09-21 12:37:10 -0700486 std::lock_guard<std::mutex> lock(local_transports_lock);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100487 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100488 return result;
489}
490
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700491atransport* find_emulator_transport_by_console_port(int console_port)
492{
493 return find_transport(getEmulatorSerialString(console_port).c_str());
494}
495
496
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100497/* Only call this function if you already hold local_transports_lock. */
498int get_available_local_transport_index_locked()
499{
500 int i;
501 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
502 if (local_transports[i] == NULL) {
503 return i;
504 }
505 }
506 return -1;
507}
508
509int get_available_local_transport_index()
510{
Josh Gaoe7daf572016-09-21 12:37:10 -0700511 std::lock_guard<std::mutex> lock(local_transports_lock);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100512 int result = get_available_local_transport_index_locked();
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100513 return result;
514}
515#endif
516
517int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800518{
519 int fail = 0;
520
Yabin Cuiefc525a2016-04-18 11:22:34 -0700521 t->SetKickFunction(remote_kick);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800522 t->close = remote_close;
523 t->read_from_remote = remote_read;
524 t->write_to_remote = remote_write;
525 t->sfd = s;
526 t->sync_token = 1;
Dan Albert9a50f4c2015-05-18 16:43:57 -0700527 t->connection_state = kCsOffline;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800528 t->type = kTransportLocal;
529
530#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700531 if (local) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700532 std::lock_guard<std::mutex> lock(local_transports_lock);
533 t->SetLocalPortForEmulator(adb_port);
534 atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
535 int index = get_available_local_transport_index_locked();
536 if (existing_transport != NULL) {
537 D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
538 fail = -1;
539 } else if (index < 0) {
540 // Too many emulators.
541 D("cannot register more emulators. Maximum is %d", ADB_LOCAL_TRANSPORT_MAX);
542 fail = -1;
543 } else {
544 local_transports[index] = t;
545 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800546 }
547#endif
548 return fail;
549}