blob: d918cc7ac5fd3e1803ed473a45ed1a2c06f6a1e4 [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"
Yabin Cuif401ead2016-04-29 16:53:52 -070020#include "sysdeps/condition_variable.h"
21#include "sysdeps/mutex.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070022#include "transport.h"
23
Dan Albertb302d122015-02-24 15:51:19 -080024#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080028#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029
Yabin Cuif401ead2016-04-29 16:53:52 -070030#include <vector>
31
Elliott Hughesf55ead92015-12-04 22:00:26 -080032#include <android-base/stringprintf.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070033#include <cutils/sockets.h>
Elliott Hughesfb596842015-05-01 17:04:38 -070034
Dan Albertb302d122015-02-24 15:51:19 -080035#if !ADB_HOST
36#include "cutils/properties.h"
37#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070038
39#include "adb.h"
40#include "adb_io.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070041#include "adb_utils.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043#if ADB_HOST
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010044/* we keep a list of opened transports. The atransport struct knows to which
45 * local transport it is connected. The list is used to detect when we're
46 * trying to connect twice to a given local transport.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047 */
David 'Digit' Turnerb0f0a462014-03-13 11:21:58 +010048#define ADB_LOCAL_TRANSPORT_MAX 64
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080049
50ADB_MUTEX_DEFINE( local_transports_lock );
51
52static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
53#endif /* ADB_HOST */
54
55static int remote_read(apacket *p, atransport *t)
56{
Dan Albert66a91b02015-02-24 21:26:58 -080057 if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
Yabin Cui815ad882015-09-02 17:44:28 -070058 D("remote local: read terminated (message)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059 return -1;
60 }
61
Tamas Berghammera1c60c02015-07-13 19:12:28 +010062 if(check_header(p, t)) {
Yabin Cui815ad882015-09-02 17:44:28 -070063 D("bad header: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064 return -1;
65 }
66
Dan Albert66a91b02015-02-24 21:26:58 -080067 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
Yabin Cui815ad882015-09-02 17:44:28 -070068 D("remote local: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069 return -1;
70 }
71
72 if(check_data(p)) {
Yabin Cui815ad882015-09-02 17:44:28 -070073 D("bad data: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074 return -1;
75 }
76
77 return 0;
78}
79
80static int remote_write(apacket *p, atransport *t)
81{
82 int length = p->msg.data_length;
83
Dan Albert66a91b02015-02-24 21:26:58 -080084 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
Yabin Cui815ad882015-09-02 17:44:28 -070085 D("remote local: write terminated");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080086 return -1;
87 }
88
89 return 0;
90}
91
Yabin Cuif401ead2016-04-29 16:53:52 -070092bool local_connect(int port) {
Elliott Hughes43df1092015-07-23 17:12:58 -070093 std::string dummy;
Yabin Cuif401ead2016-04-29 16:53:52 -070094 return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010095}
96
Elliott Hughes43df1092015-07-23 17:12:58 -070097int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
98 int fd = -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099
100#if ADB_HOST
Yabin Cuid802bcf2015-07-31 14:10:54 -0700101 if (find_emulator_transport_by_adb_port(adb_port) != nullptr) {
102 return -1;
103 }
104
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800105 const char *host = getenv("ADBHOST");
106 if (host) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700107 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108 }
109#endif
110 if (fd < 0) {
Spencer Low753d4852015-07-30 23:07:55 -0700111 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 }
113
114 if (fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700115 D("client: connected on remote on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800116 close_on_exec(fd);
117 disable_tcp_nagle(fd);
Elliott Hughesfb596842015-05-01 17:04:38 -0700118 std::string serial = android::base::StringPrintf("emulator-%d", console_port);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700119 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
120 return 0;
121 }
122 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800123 }
124 return -1;
125}
126
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700127#if ADB_HOST
Yabin Cuif401ead2016-04-29 16:53:52 -0700128
129static void PollAllLocalPortsForEmulator() {
130 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
131 int count = ADB_LOCAL_TRANSPORT_MAX;
132
133 // Try to connect to any number of running emulator instances.
134 for ( ; count > 0; count--, port += 2 ) {
135 local_connect(port);
136 }
137}
138
139// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
140constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
141constexpr uint32_t LOCAL_PORT_RETRY_INTERVAL_IN_MS = 1000;
142
143struct RetryPort {
144 int port;
145 uint32_t retry_count;
146};
147
148// Retry emulators just kicked.
149static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
150std::mutex &retry_ports_lock = *new std::mutex;
151std::condition_variable &retry_ports_cond = *new std::condition_variable;
152
Josh Gao7d405252016-02-12 14:31:15 -0800153static void client_socket_thread(void* x) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700154 adb_thread_setname("client_socket_thread");
Yabin Cui815ad882015-09-02 17:44:28 -0700155 D("transport: client_socket_thread() starting");
Yabin Cuif401ead2016-04-29 16:53:52 -0700156 PollAllLocalPortsForEmulator();
Yabin Cuid802bcf2015-07-31 14:10:54 -0700157 while (true) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700158 std::vector<RetryPort> ports;
159 // Collect retry ports.
160 {
161 std::unique_lock<std::mutex> lock(retry_ports_lock);
162 while (retry_ports.empty()) {
163 retry_ports_cond.wait(lock);
164 }
165 retry_ports.swap(ports);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700166 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700167 // Sleep here instead of the end of loop, because if we immediately try to reconnect
168 // the emulator just kicked, the adbd on the emulator may not have time to remove the
169 // just kicked transport.
170 adb_sleep_ms(LOCAL_PORT_RETRY_INTERVAL_IN_MS);
171
172 // Try connecting retry ports.
173 std::vector<RetryPort> next_ports;
174 for (auto& port : ports) {
175 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
176 << port.retry_count;
177 if (local_connect(port.port)) {
178 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
179 continue;
180 }
181 if (--port.retry_count > 0) {
182 next_ports.push_back(port);
183 } else {
184 VLOG(TRANSPORT) << "stop retrying port " << port.port;
185 }
186 }
187
188 // Copy back left retry ports.
189 {
190 std::unique_lock<std::mutex> lock(retry_ports_lock);
191 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
192 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800193 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194}
195
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700196#else // ADB_HOST
197
Josh Gao7d405252016-02-12 14:31:15 -0800198static void server_socket_thread(void* arg) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 int serverfd, fd;
Erik Kline3c8d44d2015-12-01 17:27:59 +0900200 sockaddr_storage ss;
201 sockaddr *addrp = reinterpret_cast<sockaddr*>(&ss);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800202 socklen_t alen;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800203 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800204
Siva Velusamy2669cf92015-08-28 16:37:29 -0700205 adb_thread_setname("server socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700206 D("transport: server_socket_thread() starting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800207 serverfd = -1;
208 for(;;) {
209 if(serverfd == -1) {
Spencer Low753d4852015-07-30 23:07:55 -0700210 std::string error;
211 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800212 if(serverfd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700213 D("server: cannot bind socket yet: %s", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800214 adb_sleep_ms(1000);
215 continue;
216 }
217 close_on_exec(serverfd);
218 }
219
Erik Kline3c8d44d2015-12-01 17:27:59 +0900220 alen = sizeof(ss);
Yabin Cui815ad882015-09-02 17:44:28 -0700221 D("server: trying to get new connection from %d", port);
Erik Kline3c8d44d2015-12-01 17:27:59 +0900222 fd = adb_socket_accept(serverfd, addrp, &alen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223 if(fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700224 D("server: new connection on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800225 close_on_exec(fd);
226 disable_tcp_nagle(fd);
Yabin Cuic7221782016-05-17 15:14:25 -0700227 if (register_socket_transport(fd, "host", port, 1) != 0) {
228 adb_close(fd);
229 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800230 }
231 }
Yabin Cui815ad882015-09-02 17:44:28 -0700232 D("transport: server_socket_thread() exiting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233}
234
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800235/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800236/*
237 * Redefine open and write for qemu_pipe.h that contains inlined references
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200238 * to those routines. We will redefine them back after qemu_pipe.h inclusion.
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800239 */
240#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200241#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800242#undef write
243#define open adb_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200244#define read adb_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800245#define write adb_write
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200246#include <system/qemu_pipe.h>
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800247#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200248#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800249#undef write
250#define open ___xxx_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200251#define read ___xxx_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800252#define write ___xxx_write
253
254/* A worker thread that monitors host connections, and registers a transport for
255 * every new host connection. This thread replaces server_socket_thread on
256 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
257 * pipe to communicate with adbd daemon inside the guest. This is done in order
258 * to provide more robust communication channel between ADB host and guest. The
259 * main issue with server_socket_thread approach is that it runs on top of TCP,
260 * and thus is sensitive to network disruptions. For instance, the
261 * ConnectionManager may decide to reset all network connections, in which case
262 * the connection between ADB host and guest will be lost. To make ADB traffic
263 * independent from the network, we use here 'adb' QEMUD service to transfer data
264 * between the host, and the guest. See external/qemu/android/adb-*.* that
265 * implements the emulator's side of the protocol. Another advantage of using
266 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
267 * anymore on network being set up.
268 * The guest side of the protocol contains the following phases:
269 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
270 * is opened, and it becomes clear whether or not emulator supports that
271 * protocol.
272 * - Wait for the ADB host to create connection with the guest. This is done by
273 * sending an 'accept' request to the adb QEMUD service, and waiting on
274 * response.
275 * - When new ADB host connection is accepted, the connection with adb QEMUD
276 * service is registered as the transport, and a 'start' request is sent to the
277 * adb QEMUD service, indicating that the guest is ready to receive messages.
278 * Note that the guest will ignore messages sent down from the emulator before
279 * the transport registration is completed. That's why we need to send the
280 * 'start' request after the transport is registered.
281 */
Josh Gao7d405252016-02-12 14:31:15 -0800282static void qemu_socket_thread(void* arg) {
283 /* 'accept' request to the adb QEMUD service. */
284 static const char _accept_req[] = "accept";
285 /* 'start' request to the adb QEMUD service. */
286 static const char _start_req[] = "start";
287 /* 'ok' reply from the adb QEMUD service. */
288 static const char _ok_resp[] = "ok";
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800289
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800290 const int port = (int) (uintptr_t) arg;
bohu82a28342016-02-29 13:13:19 -0800291 int fd;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800292 char tmp[256];
293 char con_name[32];
294
Siva Velusamy2669cf92015-08-28 16:37:29 -0700295 adb_thread_setname("qemu socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700296 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800297
298 /* adb QEMUD service connection request. */
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200299 snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800300
301 /* Connect to the adb QEMUD service. */
302 fd = qemu_pipe_open(con_name);
303 if (fd < 0) {
304 /* This could be an older version of the emulator, that doesn't
305 * implement adb QEMUD service. Fall back to the old TCP way. */
Yabin Cui815ad882015-09-02 17:44:28 -0700306 D("adb service is not available. Falling back to TCP socket.");
Elliott Hughesf2517142015-05-05 13:41:21 -0700307 adb_thread_create(server_socket_thread, arg);
Josh Gao7d405252016-02-12 14:31:15 -0800308 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800309 }
310
311 for(;;) {
312 /*
313 * Wait till the host creates a new connection.
314 */
315
316 /* Send the 'accept' request. */
bohu82a28342016-02-29 13:13:19 -0800317 if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800318 /* Wait for the response. In the response we expect 'ok' on success,
319 * or 'ko' on failure. */
bohu82a28342016-02-29 13:13:19 -0800320 if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700321 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800322 adb_close(fd);
323 } else {
324 /* Host is connected. Register the transport, and start the
325 * exchange. */
Prathmesh Prabhu07778a02016-03-09 13:51:30 -0800326 std::string serial = android::base::StringPrintf("host-%d", fd);
Yabin Cuic7221782016-05-17 15:14:25 -0700327 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
328 !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
bohu82a28342016-02-29 13:13:19 -0800329 adb_close(fd);
330 }
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800331 }
332
333 /* Prepare for accepting of the next ADB host connection. */
334 fd = qemu_pipe_open(con_name);
335 if (fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700336 D("adb service become unavailable.");
Josh Gao7d405252016-02-12 14:31:15 -0800337 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800338 }
339 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700340 D("Unable to send the '%s' request to ADB service.", _accept_req);
Josh Gao7d405252016-02-12 14:31:15 -0800341 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800342 }
343 }
Yabin Cui815ad882015-09-02 17:44:28 -0700344 D("transport: qemu_socket_thread() exiting");
Josh Gao7d405252016-02-12 14:31:15 -0800345 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800346}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800347#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800348
Mike Lockwood26b88e32009-08-24 15:58:40 -0700349void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800350{
Josh Gao7d405252016-02-12 14:31:15 -0800351 adb_thread_func_t func;
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700352 const char* debug_name = "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800353
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800354#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700355 func = client_socket_thread;
356 debug_name = "client";
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800357#else
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700358 /* For the adbd daemon in the system image we need to distinguish
359 * between the device, and the emulator. */
360 char is_qemu[PROPERTY_VALUE_MAX];
361 property_get("ro.kernel.qemu", is_qemu, "");
362 if (!strcmp(is_qemu, "1")) {
363 /* Running inside the emulator: use QEMUD pipe as the transport. */
364 func = qemu_socket_thread;
365 } else {
366 /* Running inside the device: use TCP socket as the transport. */
367 func = server_socket_thread;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700369 debug_name = "server";
370#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371
Yabin Cui815ad882015-09-02 17:44:28 -0700372 D("transport: local %s init", debug_name);
Elliott Hughesf2517142015-05-05 13:41:21 -0700373 if (!adb_thread_create(func, (void *) (uintptr_t) port)) {
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700374 fatal_errno("cannot create local socket %s thread", debug_name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800375 }
376}
377
378static void remote_kick(atransport *t)
379{
380 int fd = t->sfd;
381 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400382 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383 adb_close(fd);
384
385#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700386 int nn;
387 adb_mutex_lock( &local_transports_lock );
388 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
389 if (local_transports[nn] == t) {
390 local_transports[nn] = NULL;
391 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800393 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700394 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800395#endif
396}
397
398static void remote_close(atransport *t)
399{
Spencer Low6f38e232015-06-09 12:32:17 -0700400 int fd = t->sfd;
401 if (fd != -1) {
402 t->sfd = -1;
403 adb_close(fd);
404 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700405#if ADB_HOST
406 int local_port;
407 if (t->GetLocalPortForEmulator(&local_port)) {
408 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port;
409 std::unique_lock<std::mutex> lock(retry_ports_lock);
410 RetryPort port;
411 port.port = local_port;
412 port.retry_count = LOCAL_PORT_RETRY_COUNT;
413 retry_ports.push_back(port);
414 retry_ports_cond.notify_one();
415 }
416#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417}
418
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100419
420#if ADB_HOST
421/* Only call this function if you already hold local_transports_lock. */
Yabin Cuif401ead2016-04-29 16:53:52 -0700422static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100423{
424 int i;
425 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700426 int local_port;
427 if (local_transports[i] && local_transports[i]->GetLocalPortForEmulator(&local_port)) {
428 if (local_port == adb_port) {
429 return local_transports[i];
430 }
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100431 }
432 }
433 return NULL;
434}
435
436atransport* find_emulator_transport_by_adb_port(int adb_port)
437{
438 adb_mutex_lock( &local_transports_lock );
439 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
440 adb_mutex_unlock( &local_transports_lock );
441 return result;
442}
443
444/* Only call this function if you already hold local_transports_lock. */
445int get_available_local_transport_index_locked()
446{
447 int i;
448 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
449 if (local_transports[i] == NULL) {
450 return i;
451 }
452 }
453 return -1;
454}
455
456int get_available_local_transport_index()
457{
458 adb_mutex_lock( &local_transports_lock );
459 int result = get_available_local_transport_index_locked();
460 adb_mutex_unlock( &local_transports_lock );
461 return result;
462}
463#endif
464
465int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800466{
467 int fail = 0;
468
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700469 t->SetKickFunction(remote_kick);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800470 t->close = remote_close;
471 t->read_from_remote = remote_read;
472 t->write_to_remote = remote_write;
473 t->sfd = s;
474 t->sync_token = 1;
Dan Albert9a50f4c2015-05-18 16:43:57 -0700475 t->connection_state = kCsOffline;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800476 t->type = kTransportLocal;
477
478#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700479 if (local) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800480 adb_mutex_lock( &local_transports_lock );
481 {
Yabin Cuif401ead2016-04-29 16:53:52 -0700482 t->SetLocalPortForEmulator(adb_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100483 atransport* existing_transport =
484 find_emulator_transport_by_adb_port_locked(adb_port);
485 int index = get_available_local_transport_index_locked();
486 if (existing_transport != NULL) {
Yabin Cui815ad882015-09-02 17:44:28 -0700487 D("local transport for port %d already registered (%p)?",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100488 adb_port, existing_transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800489 fail = -1;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100490 } else if (index < 0) {
491 // Too many emulators.
Yabin Cui815ad882015-09-02 17:44:28 -0700492 D("cannot register more emulators. Maximum is %d",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100493 ADB_LOCAL_TRANSPORT_MAX);
494 fail = -1;
495 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800496 local_transports[index] = t;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100497 }
498 }
499 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800500 }
501#endif
502 return fail;
503}