blob: d6c84dac553071fc610dc6c5c43067a08f743171 [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 Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "transport.h"
21
Dan Albert76649012015-02-24 15:51:19 -080022#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Pirama Arumuga Nainar7eaef8a2016-09-06 13:34:42 -070028#include <condition_variable>
Josh Gao0cd3ae12016-09-21 12:37:10 -070029#include <mutex>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080030#include <thread>
Yabin Cuib74c6492016-04-29 16:53:52 -070031#include <vector>
32
Casey Dahlin13a269e2016-06-23 14:19:37 -070033#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080034#include <android-base/stringprintf.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070035#include <cutils/sockets.h>
Elliott Hughesab52c182015-05-01 17:04:38 -070036
Dan Albert76649012015-02-24 15:51:19 -080037#if !ADB_HOST
Elliott Hughesffdec182016-09-23 15:40:03 -070038#include <android-base/properties.h>
Dan Albert76649012015-02-24 15:51:19 -080039#endif
Dan Albert33134262015-03-19 15:21:08 -070040
41#include "adb.h"
42#include "adb_io.h"
Elliott Hughes381cfa92015-07-23 17:12:58 -070043#include "adb_utils.h"
Josh Gao4602adb2016-11-15 18:55:47 -080044#include "sysdeps/chrono.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#if ADB_HOST
Josh Gao9fe74262016-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 Gao0cd3ae12016-09-21 12:37:10 -070054static std::mutex& local_transports_lock = *new std::mutex();
Josh Gao9fe74262016-05-06 02:37:24 -070055
Stefan Hilzingerd9d1ca42010-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 Projectdd7bc332009-03-03 19:32:55 -080059 */
The Android Open Source Projectdd7bc332009-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{
Josh Gao36dadca2017-05-16 15:02:45 -070065 if (!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070066 D("remote local: read terminated (message)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067 return -1;
68 }
69
Josh Gao36dadca2017-05-16 15:02:45 -070070 if (!check_header(p, t)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070071 D("bad header: terminated (data)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 return -1;
73 }
74
Josh Gao36dadca2017-05-16 15:02:45 -070075 if (!ReadFdExactly(t->sfd, p->data, p->msg.data_length)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070076 D("remote local: terminated (data)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 return -1;
78 }
79
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 return 0;
81}
82
83static int remote_write(apacket *p, atransport *t)
84{
85 int length = p->msg.data_length;
86
Dan Albertcc731cc2015-02-24 21:26:58 -080087 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070088 D("remote local: write terminated");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 return -1;
90 }
91
92 return 0;
93}
94
Yabin Cuib74c6492016-04-29 16:53:52 -070095bool local_connect(int port) {
Elliott Hughes381cfa92015-07-23 17:12:58 -070096 std::string dummy;
Yabin Cuib74c6492016-04-29 16:53:52 -070097 return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +010098}
99
Casey Dahlin13a269e2016-06-23 14:19:37 -0700100void connect_device(const std::string& address, std::string* response) {
101 if (address.empty()) {
102 *response = "empty address";
103 return;
104 }
105
106 std::string serial;
107 std::string host;
108 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
109 if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) {
110 return;
111 }
112
113 std::string error;
114 int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error);
115 if (fd == -1) {
116 *response = android::base::StringPrintf("unable to connect to %s: %s",
117 serial.c_str(), error.c_str());
118 return;
119 }
120
121 D("client: connected %s remote on fd %d", serial.c_str(), fd);
122 close_on_exec(fd);
123 disable_tcp_nagle(fd);
124
125 // Send a TCP keepalive ping to the device every second so we can detect disconnects.
126 if (!set_tcp_keepalive(fd, 1)) {
127 D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
128 }
129
130 int ret = register_socket_transport(fd, serial.c_str(), port, 0);
131 if (ret < 0) {
132 adb_close(fd);
133 *response = android::base::StringPrintf("already connected to %s", serial.c_str());
134 } else {
135 *response = android::base::StringPrintf("connected to %s", serial.c_str());
136 }
137}
138
139
Elliott Hughes381cfa92015-07-23 17:12:58 -0700140int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
141 int fd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142
143#if ADB_HOST
Lingfeng Yang11979522016-10-06 12:22:55 -0700144 if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
145 find_emulator_transport_by_console_port(console_port) != nullptr) {
Yabin Cui0e2c1942015-07-31 14:10:54 -0700146 return -1;
147 }
148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 const char *host = getenv("ADBHOST");
150 if (host) {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700151 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 }
153#endif
154 if (fd < 0) {
Spencer Low5200c662015-07-30 23:07:55 -0700155 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 }
157
158 if (fd >= 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700159 D("client: connected on remote on fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 close_on_exec(fd);
161 disable_tcp_nagle(fd);
Lingfeng Yang11979522016-10-06 12:22:55 -0700162 std::string serial = getEmulatorSerialString(console_port);
Yabin Cui0e2c1942015-07-31 14:10:54 -0700163 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
164 return 0;
165 }
166 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
168 return -1;
169}
170
Yabin Cui661327e2015-08-11 13:40:42 -0700171#if ADB_HOST
Yabin Cuib74c6492016-04-29 16:53:52 -0700172
173static void PollAllLocalPortsForEmulator() {
174 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
175 int count = ADB_LOCAL_TRANSPORT_MAX;
176
177 // Try to connect to any number of running emulator instances.
178 for ( ; count > 0; count--, port += 2 ) {
179 local_connect(port);
180 }
181}
182
183// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
184constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
Josh Gao4602adb2016-11-15 18:55:47 -0800185constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s;
Yabin Cuib74c6492016-04-29 16:53:52 -0700186
187struct RetryPort {
188 int port;
189 uint32_t retry_count;
190};
191
192// Retry emulators just kicked.
193static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
194std::mutex &retry_ports_lock = *new std::mutex;
195std::condition_variable &retry_ports_cond = *new std::condition_variable;
196
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700197static void client_socket_thread(int) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700198 adb_thread_setname("client_socket_thread");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700199 D("transport: client_socket_thread() starting");
Yabin Cuib74c6492016-04-29 16:53:52 -0700200 PollAllLocalPortsForEmulator();
Yabin Cui0e2c1942015-07-31 14:10:54 -0700201 while (true) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700202 std::vector<RetryPort> ports;
203 // Collect retry ports.
204 {
205 std::unique_lock<std::mutex> lock(retry_ports_lock);
206 while (retry_ports.empty()) {
207 retry_ports_cond.wait(lock);
208 }
209 retry_ports.swap(ports);
Yabin Cui0e2c1942015-07-31 14:10:54 -0700210 }
Yabin Cuib74c6492016-04-29 16:53:52 -0700211 // Sleep here instead of the end of loop, because if we immediately try to reconnect
212 // the emulator just kicked, the adbd on the emulator may not have time to remove the
213 // just kicked transport.
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800214 std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL);
Yabin Cuib74c6492016-04-29 16:53:52 -0700215
216 // Try connecting retry ports.
217 std::vector<RetryPort> next_ports;
218 for (auto& port : ports) {
219 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
220 << port.retry_count;
221 if (local_connect(port.port)) {
222 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
223 continue;
224 }
225 if (--port.retry_count > 0) {
226 next_ports.push_back(port);
227 } else {
228 VLOG(TRANSPORT) << "stop retrying port " << port.port;
229 }
230 }
231
232 // Copy back left retry ports.
233 {
234 std::unique_lock<std::mutex> lock(retry_ports_lock);
235 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
236 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238}
239
Yabin Cui661327e2015-08-11 13:40:42 -0700240#else // ADB_HOST
241
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700242static void server_socket_thread(int port) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 int serverfd, fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700245 adb_thread_setname("server socket");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700246 D("transport: server_socket_thread() starting");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 serverfd = -1;
248 for(;;) {
249 if(serverfd == -1) {
Spencer Low5200c662015-07-30 23:07:55 -0700250 std::string error;
251 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 if(serverfd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700253 D("server: cannot bind socket yet: %s", error.c_str());
Josh Gao4602adb2016-11-15 18:55:47 -0800254 std::this_thread::sleep_for(1s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 continue;
256 }
257 close_on_exec(serverfd);
258 }
259
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700260 D("server: trying to get new connection from %d", port);
Josh Gao78e1eb12016-08-23 15:41:56 -0700261 fd = adb_socket_accept(serverfd, nullptr, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 if(fd >= 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700263 D("server: new connection on fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 close_on_exec(fd);
265 disable_tcp_nagle(fd);
Tao Wu043912e2016-10-21 15:43:57 -0700266 std::string serial = android::base::StringPrintf("host-%d", fd);
267 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0) {
Yabin Cui01401822016-05-17 15:14:25 -0700268 adb_close(fd);
269 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 }
271 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700272 D("transport: server_socket_thread() exiting");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273}
274
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800275/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800276/*
277 * Redefine open and write for qemu_pipe.h that contains inlined references
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200278 * to those routines. We will redefine them back after qemu_pipe.h inclusion.
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800279 */
280#undef open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200281#undef read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800282#undef write
283#define open adb_open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200284#define read adb_read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800285#define write adb_write
bohuf66a7ee2017-03-29 12:19:40 -0700286#include <qemu_pipe.h>
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800287#undef open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200288#undef read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800289#undef write
290#define open ___xxx_open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200291#define read ___xxx_read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800292#define write ___xxx_write
293
294/* A worker thread that monitors host connections, and registers a transport for
295 * every new host connection. This thread replaces server_socket_thread on
296 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
297 * pipe to communicate with adbd daemon inside the guest. This is done in order
298 * to provide more robust communication channel between ADB host and guest. The
299 * main issue with server_socket_thread approach is that it runs on top of TCP,
300 * and thus is sensitive to network disruptions. For instance, the
301 * ConnectionManager may decide to reset all network connections, in which case
302 * the connection between ADB host and guest will be lost. To make ADB traffic
303 * independent from the network, we use here 'adb' QEMUD service to transfer data
304 * between the host, and the guest. See external/qemu/android/adb-*.* that
305 * implements the emulator's side of the protocol. Another advantage of using
306 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
307 * anymore on network being set up.
308 * The guest side of the protocol contains the following phases:
309 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
310 * is opened, and it becomes clear whether or not emulator supports that
311 * protocol.
312 * - Wait for the ADB host to create connection with the guest. This is done by
313 * sending an 'accept' request to the adb QEMUD service, and waiting on
314 * response.
315 * - When new ADB host connection is accepted, the connection with adb QEMUD
316 * service is registered as the transport, and a 'start' request is sent to the
317 * adb QEMUD service, indicating that the guest is ready to receive messages.
318 * Note that the guest will ignore messages sent down from the emulator before
319 * the transport registration is completed. That's why we need to send the
320 * 'start' request after the transport is registered.
321 */
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700322static void qemu_socket_thread(int port) {
Josh Gaod9db09c2016-02-12 14:31:15 -0800323 /* 'accept' request to the adb QEMUD service. */
324 static const char _accept_req[] = "accept";
325 /* 'start' request to the adb QEMUD service. */
326 static const char _start_req[] = "start";
327 /* 'ok' reply from the adb QEMUD service. */
328 static const char _ok_resp[] = "ok";
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800329
bohu8ac1b042016-02-29 13:13:19 -0800330 int fd;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800331 char tmp[256];
332 char con_name[32];
333
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700334 adb_thread_setname("qemu socket");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700335 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800336
337 /* adb QEMUD service connection request. */
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200338 snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800339
340 /* Connect to the adb QEMUD service. */
341 fd = qemu_pipe_open(con_name);
342 if (fd < 0) {
343 /* This could be an older version of the emulator, that doesn't
344 * implement adb QEMUD service. Fall back to the old TCP way. */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700345 D("adb service is not available. Falling back to TCP socket.");
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700346 std::thread(server_socket_thread, port).detach();
Josh Gaod9db09c2016-02-12 14:31:15 -0800347 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800348 }
349
350 for(;;) {
351 /*
352 * Wait till the host creates a new connection.
353 */
354
355 /* Send the 'accept' request. */
bohu8ac1b042016-02-29 13:13:19 -0800356 if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800357 /* Wait for the response. In the response we expect 'ok' on success,
358 * or 'ko' on failure. */
bohu8ac1b042016-02-29 13:13:19 -0800359 if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700360 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800361 adb_close(fd);
362 } else {
363 /* Host is connected. Register the transport, and start the
364 * exchange. */
Prathmesh Prabhu251d46e2016-03-09 13:51:30 -0800365 std::string serial = android::base::StringPrintf("host-%d", fd);
Yabin Cui01401822016-05-17 15:14:25 -0700366 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
367 !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
bohu8ac1b042016-02-29 13:13:19 -0800368 adb_close(fd);
369 }
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800370 }
371
372 /* Prepare for accepting of the next ADB host connection. */
373 fd = qemu_pipe_open(con_name);
374 if (fd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700375 D("adb service become unavailable.");
Josh Gaod9db09c2016-02-12 14:31:15 -0800376 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800377 }
378 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700379 D("Unable to send the '%s' request to ADB service.", _accept_req);
Josh Gaod9db09c2016-02-12 14:31:15 -0800380 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800381 }
382 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700383 D("transport: qemu_socket_thread() exiting");
Josh Gaod9db09c2016-02-12 14:31:15 -0800384 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800385}
Nicolas Norvez717f1532017-06-06 13:38:20 -0700386
387// If adbd is running inside the emulator, it will normally use QEMUD pipe (aka
388// goldfish) as the transport. This can either be explicitly set by the
389// service.adb.transport property, or be inferred from ro.kernel.qemu that is
390// set to "1" for ranchu/goldfish.
391static bool use_qemu_goldfish() {
392 // Legacy way to detect if adbd should use the goldfish pipe is to check for
393 // ro.kernel.qemu, keep that behaviour for backward compatibility.
394 if (android::base::GetBoolProperty("ro.kernel.qemu", false)) {
395 return true;
396 }
397 // If service.adb.transport is present and is set to "goldfish", use the
398 // QEMUD pipe.
399 if (android::base::GetProperty("service.adb.transport", "") == "goldfish") {
400 return true;
401 }
402 return false;
403}
404
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800405#endif // !ADB_HOST
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800406
Mike Lockwoodff196702009-08-24 15:58:40 -0700407void local_init(int port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408{
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700409 void (*func)(int);
Yabin Cui661327e2015-08-11 13:40:42 -0700410 const char* debug_name = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800412#if ADB_HOST
Yabin Cui661327e2015-08-11 13:40:42 -0700413 func = client_socket_thread;
414 debug_name = "client";
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800415#else
Elliott Hughesffdec182016-09-23 15:40:03 -0700416 // For the adbd daemon in the system image we need to distinguish
417 // between the device, and the emulator.
Nicolas Norvez717f1532017-06-06 13:38:20 -0700418 func = use_qemu_goldfish() ? qemu_socket_thread : server_socket_thread;
Yabin Cui661327e2015-08-11 13:40:42 -0700419 debug_name = "server";
420#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700422 D("transport: local %s init", debug_name);
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700423 std::thread(func, port).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424}
425
426static void remote_kick(atransport *t)
427{
428 int fd = t->sfd;
429 t->sfd = -1;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400430 adb_shutdown(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 adb_close(fd);
432
433#if ADB_HOST
Yabin Cui661327e2015-08-11 13:40:42 -0700434 int nn;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700435 std::lock_guard<std::mutex> lock(local_transports_lock);
Yabin Cui661327e2015-08-11 13:40:42 -0700436 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
437 if (local_transports[nn] == t) {
438 local_transports[nn] = NULL;
439 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 }
442#endif
443}
444
445static void remote_close(atransport *t)
446{
Spencer Low3abd31d2015-06-09 12:32:17 -0700447 int fd = t->sfd;
448 if (fd != -1) {
449 t->sfd = -1;
450 adb_close(fd);
451 }
Yabin Cuib74c6492016-04-29 16:53:52 -0700452#if ADB_HOST
453 int local_port;
454 if (t->GetLocalPortForEmulator(&local_port)) {
455 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port;
456 std::unique_lock<std::mutex> lock(retry_ports_lock);
457 RetryPort port;
458 port.port = local_port;
459 port.retry_count = LOCAL_PORT_RETRY_COUNT;
460 retry_ports.push_back(port);
461 retry_ports_cond.notify_one();
462 }
463#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464}
465
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100466
467#if ADB_HOST
468/* Only call this function if you already hold local_transports_lock. */
Yabin Cuib74c6492016-04-29 16:53:52 -0700469static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100470{
471 int i;
472 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700473 int local_port;
474 if (local_transports[i] && local_transports[i]->GetLocalPortForEmulator(&local_port)) {
475 if (local_port == adb_port) {
476 return local_transports[i];
477 }
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100478 }
479 }
480 return NULL;
481}
482
Lingfeng Yang11979522016-10-06 12:22:55 -0700483std::string getEmulatorSerialString(int console_port)
484{
485 return android::base::StringPrintf("emulator-%d", console_port);
486}
487
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100488atransport* find_emulator_transport_by_adb_port(int adb_port)
489{
Josh Gao0cd3ae12016-09-21 12:37:10 -0700490 std::lock_guard<std::mutex> lock(local_transports_lock);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100491 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100492 return result;
493}
494
Lingfeng Yang11979522016-10-06 12:22:55 -0700495atransport* find_emulator_transport_by_console_port(int console_port)
496{
497 return find_transport(getEmulatorSerialString(console_port).c_str());
498}
499
500
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100501/* Only call this function if you already hold local_transports_lock. */
502int get_available_local_transport_index_locked()
503{
504 int i;
505 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
506 if (local_transports[i] == NULL) {
507 return i;
508 }
509 }
510 return -1;
511}
512
513int get_available_local_transport_index()
514{
Josh Gao0cd3ae12016-09-21 12:37:10 -0700515 std::lock_guard<std::mutex> lock(local_transports_lock);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100516 int result = get_available_local_transport_index_locked();
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100517 return result;
518}
519#endif
520
521int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522{
523 int fail = 0;
524
Yabin Cuia28918c2016-04-18 11:22:34 -0700525 t->SetKickFunction(remote_kick);
Yabin Cuib5e11412017-03-10 16:01:01 -0800526 t->SetWriteFunction(remote_write);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 t->close = remote_close;
528 t->read_from_remote = remote_read;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 t->sfd = s;
530 t->sync_token = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 t->type = kTransportLocal;
532
533#if ADB_HOST
Yabin Cui661327e2015-08-11 13:40:42 -0700534 if (local) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700535 std::lock_guard<std::mutex> lock(local_transports_lock);
536 t->SetLocalPortForEmulator(adb_port);
537 atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
538 int index = get_available_local_transport_index_locked();
539 if (existing_transport != NULL) {
540 D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
541 fail = -1;
542 } else if (index < 0) {
543 // Too many emulators.
544 D("cannot register more emulators. Maximum is %d", ADB_LOCAL_TRANSPORT_MAX);
545 fail = -1;
546 } else {
547 local_transports[index] = t;
548 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 }
550#endif
551 return fail;
552}