2009-03-04 03:32:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-22 22:52:57 +00:00
|
|
|
#define TRACE_TAG TRANSPORT
|
2015-03-19 22:21:08 +00:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "transport.h"
|
|
|
|
|
2015-02-24 23:51:19 +00:00
|
|
|
#include <errno.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-02-24 23:51:19 +00:00
|
|
|
#include <sys/types.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2016-09-06 20:34:42 +00:00
|
|
|
#include <condition_variable>
|
2016-09-21 19:37:10 +00:00
|
|
|
#include <mutex>
|
2016-11-15 20:37:32 +00:00
|
|
|
#include <thread>
|
2016-04-29 23:53:52 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-06-23 21:19:37 +00:00
|
|
|
#include <android-base/parsenetaddress.h>
|
2015-12-05 06:00:26 +00:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-07-24 00:12:58 +00:00
|
|
|
#include <cutils/sockets.h>
|
2015-05-02 00:04:38 +00:00
|
|
|
|
2015-02-24 23:51:19 +00:00
|
|
|
#if !ADB_HOST
|
2016-09-23 22:40:03 +00:00
|
|
|
#include <android-base/properties.h>
|
2015-02-24 23:51:19 +00:00
|
|
|
#endif
|
2015-03-19 22:21:08 +00:00
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
#include "adb_io.h"
|
2015-07-24 00:12:58 +00:00
|
|
|
#include "adb_utils.h"
|
2016-11-16 02:55:47 +00:00
|
|
|
#include "sysdeps/chrono.h"
|
2009-03-04 03:32:55 +00:00
|
|
|
|
|
|
|
#if ADB_HOST
|
2016-05-06 09:37:24 +00:00
|
|
|
|
|
|
|
// Android Wear has been using port 5601 in all of its documentation/tooling,
|
|
|
|
// but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX].
|
|
|
|
// Avoid stomping on their port by limiting the number of emulators that can be
|
|
|
|
// connected.
|
|
|
|
#define ADB_LOCAL_TRANSPORT_MAX 16
|
|
|
|
|
2016-09-21 19:37:10 +00:00
|
|
|
static std::mutex& local_transports_lock = *new std::mutex();
|
2016-05-06 09:37:24 +00:00
|
|
|
|
2010-04-26 09:17:43 +00:00
|
|
|
/* we keep a list of opened transports. The atransport struct knows to which
|
|
|
|
* local transport it is connected. The list is used to detect when we're
|
|
|
|
* trying to connect twice to a given local transport.
|
2009-03-04 03:32:55 +00:00
|
|
|
*/
|
|
|
|
static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
|
|
|
|
#endif /* ADB_HOST */
|
|
|
|
|
|
|
|
static int remote_read(apacket *p, atransport *t)
|
|
|
|
{
|
2015-02-25 05:26:58 +00:00
|
|
|
if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote local: read terminated (message)");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:12:28 +00:00
|
|
|
if(check_header(p, t)) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("bad header: terminated (data)");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-25 05:26:58 +00:00
|
|
|
if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote local: terminated (data)");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(check_data(p)) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("bad data: terminated (data)");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int remote_write(apacket *p, atransport *t)
|
|
|
|
{
|
|
|
|
int length = p->msg.data_length;
|
|
|
|
|
2015-02-25 05:26:58 +00:00
|
|
|
if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote local: write terminated");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-29 23:53:52 +00:00
|
|
|
bool local_connect(int port) {
|
2015-07-24 00:12:58 +00:00
|
|
|
std::string dummy;
|
2016-04-29 23:53:52 +00:00
|
|
|
return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
|
2010-04-26 09:17:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 21:19:37 +00:00
|
|
|
void connect_device(const std::string& address, std::string* response) {
|
|
|
|
if (address.empty()) {
|
|
|
|
*response = "empty address";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string serial;
|
|
|
|
std::string host;
|
|
|
|
int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
|
|
|
|
if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string error;
|
|
|
|
int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error);
|
|
|
|
if (fd == -1) {
|
|
|
|
*response = android::base::StringPrintf("unable to connect to %s: %s",
|
|
|
|
serial.c_str(), error.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
D("client: connected %s remote on fd %d", serial.c_str(), fd);
|
|
|
|
close_on_exec(fd);
|
|
|
|
disable_tcp_nagle(fd);
|
|
|
|
|
|
|
|
// Send a TCP keepalive ping to the device every second so we can detect disconnects.
|
|
|
|
if (!set_tcp_keepalive(fd, 1)) {
|
|
|
|
D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = register_socket_transport(fd, serial.c_str(), port, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
adb_close(fd);
|
|
|
|
*response = android::base::StringPrintf("already connected to %s", serial.c_str());
|
|
|
|
} else {
|
|
|
|
*response = android::base::StringPrintf("connected to %s", serial.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-24 00:12:58 +00:00
|
|
|
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
|
|
|
|
int fd = -1;
|
2009-03-04 03:32:55 +00:00
|
|
|
|
|
|
|
#if ADB_HOST
|
2016-10-06 19:22:55 +00:00
|
|
|
if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
|
|
|
|
find_emulator_transport_by_console_port(console_port) != nullptr) {
|
2015-07-31 21:10:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:32:55 +00:00
|
|
|
const char *host = getenv("ADBHOST");
|
|
|
|
if (host) {
|
2015-07-24 00:12:58 +00:00
|
|
|
fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (fd < 0) {
|
2015-07-31 06:07:55 +00:00
|
|
|
fd = network_loopback_client(adb_port, SOCK_STREAM, error);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fd >= 0) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("client: connected on remote on fd %d", fd);
|
2009-03-04 03:32:55 +00:00
|
|
|
close_on_exec(fd);
|
|
|
|
disable_tcp_nagle(fd);
|
2016-10-06 19:22:55 +00:00
|
|
|
std::string serial = getEmulatorSerialString(console_port);
|
2015-07-31 21:10:54 +00:00
|
|
|
if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
adb_close(fd);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:40:42 +00:00
|
|
|
#if ADB_HOST
|
2016-04-29 23:53:52 +00:00
|
|
|
|
|
|
|
static void PollAllLocalPortsForEmulator() {
|
|
|
|
int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
|
|
|
|
int count = ADB_LOCAL_TRANSPORT_MAX;
|
|
|
|
|
|
|
|
// Try to connect to any number of running emulator instances.
|
|
|
|
for ( ; count > 0; count--, port += 2 ) {
|
|
|
|
local_connect(port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
|
|
|
|
constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
|
2016-11-16 02:55:47 +00:00
|
|
|
constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s;
|
2016-04-29 23:53:52 +00:00
|
|
|
|
|
|
|
struct RetryPort {
|
|
|
|
int port;
|
|
|
|
uint32_t retry_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Retry emulators just kicked.
|
|
|
|
static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
|
|
|
|
std::mutex &retry_ports_lock = *new std::mutex;
|
|
|
|
std::condition_variable &retry_ports_cond = *new std::condition_variable;
|
|
|
|
|
2017-04-13 00:00:49 +00:00
|
|
|
static void client_socket_thread(int) {
|
2015-08-28 23:37:29 +00:00
|
|
|
adb_thread_setname("client_socket_thread");
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: client_socket_thread() starting");
|
2016-04-29 23:53:52 +00:00
|
|
|
PollAllLocalPortsForEmulator();
|
2015-07-31 21:10:54 +00:00
|
|
|
while (true) {
|
2016-04-29 23:53:52 +00:00
|
|
|
std::vector<RetryPort> ports;
|
|
|
|
// Collect retry ports.
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(retry_ports_lock);
|
|
|
|
while (retry_ports.empty()) {
|
|
|
|
retry_ports_cond.wait(lock);
|
|
|
|
}
|
|
|
|
retry_ports.swap(ports);
|
|
|
|
}
|
|
|
|
// Sleep here instead of the end of loop, because if we immediately try to reconnect
|
|
|
|
// the emulator just kicked, the adbd on the emulator may not have time to remove the
|
|
|
|
// just kicked transport.
|
2016-11-15 20:37:32 +00:00
|
|
|
std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL);
|
2016-04-29 23:53:52 +00:00
|
|
|
|
|
|
|
// Try connecting retry ports.
|
|
|
|
std::vector<RetryPort> next_ports;
|
|
|
|
for (auto& port : ports) {
|
|
|
|
VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
|
|
|
|
<< port.retry_count;
|
|
|
|
if (local_connect(port.port)) {
|
|
|
|
VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (--port.retry_count > 0) {
|
|
|
|
next_ports.push_back(port);
|
|
|
|
} else {
|
|
|
|
VLOG(TRANSPORT) << "stop retrying port " << port.port;
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 21:10:54 +00:00
|
|
|
|
2016-04-29 23:53:52 +00:00
|
|
|
// Copy back left retry ports.
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(retry_ports_lock);
|
|
|
|
retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
|
2015-07-31 21:10:54 +00:00
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:40:42 +00:00
|
|
|
#else // ADB_HOST
|
|
|
|
|
2017-04-13 00:00:49 +00:00
|
|
|
static void server_socket_thread(int port) {
|
2009-03-04 03:32:55 +00:00
|
|
|
int serverfd, fd;
|
|
|
|
|
2015-08-28 23:37:29 +00:00
|
|
|
adb_thread_setname("server socket");
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: server_socket_thread() starting");
|
2009-03-04 03:32:55 +00:00
|
|
|
serverfd = -1;
|
|
|
|
for(;;) {
|
|
|
|
if(serverfd == -1) {
|
2015-07-31 06:07:55 +00:00
|
|
|
std::string error;
|
|
|
|
serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
|
2009-03-04 03:32:55 +00:00
|
|
|
if(serverfd < 0) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("server: cannot bind socket yet: %s", error.c_str());
|
2016-11-16 02:55:47 +00:00
|
|
|
std::this_thread::sleep_for(1s);
|
2009-03-04 03:32:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
close_on_exec(serverfd);
|
|
|
|
}
|
|
|
|
|
2015-09-03 00:44:28 +00:00
|
|
|
D("server: trying to get new connection from %d", port);
|
2016-08-23 22:41:56 +00:00
|
|
|
fd = adb_socket_accept(serverfd, nullptr, nullptr);
|
2009-03-04 03:32:55 +00:00
|
|
|
if(fd >= 0) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("server: new connection on fd %d", fd);
|
2009-03-04 03:32:55 +00:00
|
|
|
close_on_exec(fd);
|
|
|
|
disable_tcp_nagle(fd);
|
2016-10-21 22:43:57 +00:00
|
|
|
std::string serial = android::base::StringPrintf("host-%d", fd);
|
|
|
|
if (register_socket_transport(fd, serial.c_str(), port, 1) != 0) {
|
2016-05-17 22:14:25 +00:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: server_socket_thread() exiting");
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:19:29 +00:00
|
|
|
/* This is relevant only for ADB daemon running inside the emulator. */
|
2011-12-09 23:49:47 +00:00
|
|
|
/*
|
|
|
|
* Redefine open and write for qemu_pipe.h that contains inlined references
|
2016-06-16 15:05:52 +00:00
|
|
|
* to those routines. We will redefine them back after qemu_pipe.h inclusion.
|
2011-12-09 23:49:47 +00:00
|
|
|
*/
|
|
|
|
#undef open
|
2016-06-16 15:05:52 +00:00
|
|
|
#undef read
|
2011-12-09 23:49:47 +00:00
|
|
|
#undef write
|
|
|
|
#define open adb_open
|
2016-06-16 15:05:52 +00:00
|
|
|
#define read adb_read
|
2011-12-09 23:49:47 +00:00
|
|
|
#define write adb_write
|
2016-06-16 15:05:52 +00:00
|
|
|
#include <system/qemu_pipe.h>
|
2011-12-09 23:49:47 +00:00
|
|
|
#undef open
|
2016-06-16 15:05:52 +00:00
|
|
|
#undef read
|
2011-12-09 23:49:47 +00:00
|
|
|
#undef write
|
|
|
|
#define open ___xxx_open
|
2016-06-16 15:05:52 +00:00
|
|
|
#define read ___xxx_read
|
2011-12-09 23:49:47 +00:00
|
|
|
#define write ___xxx_write
|
|
|
|
|
|
|
|
/* A worker thread that monitors host connections, and registers a transport for
|
|
|
|
* every new host connection. This thread replaces server_socket_thread on
|
|
|
|
* condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
|
|
|
|
* pipe to communicate with adbd daemon inside the guest. This is done in order
|
|
|
|
* to provide more robust communication channel between ADB host and guest. The
|
|
|
|
* main issue with server_socket_thread approach is that it runs on top of TCP,
|
|
|
|
* and thus is sensitive to network disruptions. For instance, the
|
|
|
|
* ConnectionManager may decide to reset all network connections, in which case
|
|
|
|
* the connection between ADB host and guest will be lost. To make ADB traffic
|
|
|
|
* independent from the network, we use here 'adb' QEMUD service to transfer data
|
|
|
|
* between the host, and the guest. See external/qemu/android/adb-*.* that
|
|
|
|
* implements the emulator's side of the protocol. Another advantage of using
|
|
|
|
* QEMUD approach is that ADB will be up much sooner, since it doesn't depend
|
|
|
|
* anymore on network being set up.
|
|
|
|
* The guest side of the protocol contains the following phases:
|
|
|
|
* - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
|
|
|
|
* is opened, and it becomes clear whether or not emulator supports that
|
|
|
|
* protocol.
|
|
|
|
* - Wait for the ADB host to create connection with the guest. This is done by
|
|
|
|
* sending an 'accept' request to the adb QEMUD service, and waiting on
|
|
|
|
* response.
|
|
|
|
* - When new ADB host connection is accepted, the connection with adb QEMUD
|
|
|
|
* service is registered as the transport, and a 'start' request is sent to the
|
|
|
|
* adb QEMUD service, indicating that the guest is ready to receive messages.
|
|
|
|
* Note that the guest will ignore messages sent down from the emulator before
|
|
|
|
* the transport registration is completed. That's why we need to send the
|
|
|
|
* 'start' request after the transport is registered.
|
|
|
|
*/
|
2017-04-13 00:00:49 +00:00
|
|
|
static void qemu_socket_thread(int port) {
|
2016-02-12 22:31:15 +00:00
|
|
|
/* 'accept' request to the adb QEMUD service. */
|
|
|
|
static const char _accept_req[] = "accept";
|
|
|
|
/* 'start' request to the adb QEMUD service. */
|
|
|
|
static const char _start_req[] = "start";
|
|
|
|
/* 'ok' reply from the adb QEMUD service. */
|
|
|
|
static const char _ok_resp[] = "ok";
|
2011-12-09 23:49:47 +00:00
|
|
|
|
2016-02-29 21:13:19 +00:00
|
|
|
int fd;
|
2011-12-09 23:49:47 +00:00
|
|
|
char tmp[256];
|
|
|
|
char con_name[32];
|
|
|
|
|
2015-08-28 23:37:29 +00:00
|
|
|
adb_thread_setname("qemu socket");
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: qemu_socket_thread() starting");
|
2011-12-09 23:49:47 +00:00
|
|
|
|
|
|
|
/* adb QEMUD service connection request. */
|
2016-06-16 15:05:52 +00:00
|
|
|
snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
|
2011-12-09 23:49:47 +00:00
|
|
|
|
|
|
|
/* Connect to the adb QEMUD service. */
|
|
|
|
fd = qemu_pipe_open(con_name);
|
|
|
|
if (fd < 0) {
|
|
|
|
/* This could be an older version of the emulator, that doesn't
|
|
|
|
* implement adb QEMUD service. Fall back to the old TCP way. */
|
2015-09-03 00:44:28 +00:00
|
|
|
D("adb service is not available. Falling back to TCP socket.");
|
2017-04-13 00:00:49 +00:00
|
|
|
std::thread(server_socket_thread, port).detach();
|
2016-02-12 22:31:15 +00:00
|
|
|
return;
|
2011-12-09 23:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
/*
|
|
|
|
* Wait till the host creates a new connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Send the 'accept' request. */
|
2016-02-29 21:13:19 +00:00
|
|
|
if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
|
2011-12-09 23:49:47 +00:00
|
|
|
/* Wait for the response. In the response we expect 'ok' on success,
|
|
|
|
* or 'ko' on failure. */
|
2016-02-29 21:13:19 +00:00
|
|
|
if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("Accepting ADB host connection has failed.");
|
2011-12-09 23:49:47 +00:00
|
|
|
adb_close(fd);
|
|
|
|
} else {
|
|
|
|
/* Host is connected. Register the transport, and start the
|
|
|
|
* exchange. */
|
2016-03-09 21:51:30 +00:00
|
|
|
std::string serial = android::base::StringPrintf("host-%d", fd);
|
2016-05-17 22:14:25 +00:00
|
|
|
if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
|
|
|
|
!WriteFdExactly(fd, _start_req, strlen(_start_req))) {
|
2016-02-29 21:13:19 +00:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
2011-12-09 23:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare for accepting of the next ADB host connection. */
|
|
|
|
fd = qemu_pipe_open(con_name);
|
|
|
|
if (fd < 0) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("adb service become unavailable.");
|
2016-02-12 22:31:15 +00:00
|
|
|
return;
|
2011-12-09 23:49:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("Unable to send the '%s' request to ADB service.", _accept_req);
|
2016-02-12 22:31:15 +00:00
|
|
|
return;
|
2011-12-09 23:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: qemu_socket_thread() exiting");
|
2016-02-12 22:31:15 +00:00
|
|
|
return;
|
2011-12-09 23:49:47 +00:00
|
|
|
}
|
2011-12-13 20:19:29 +00:00
|
|
|
#endif // !ADB_HOST
|
2011-12-09 23:49:47 +00:00
|
|
|
|
2009-08-24 22:58:40 +00:00
|
|
|
void local_init(int port)
|
2009-03-04 03:32:55 +00:00
|
|
|
{
|
2017-04-13 00:00:49 +00:00
|
|
|
void (*func)(int);
|
2015-08-11 20:40:42 +00:00
|
|
|
const char* debug_name = "";
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2011-12-13 20:19:29 +00:00
|
|
|
#if ADB_HOST
|
2015-08-11 20:40:42 +00:00
|
|
|
func = client_socket_thread;
|
|
|
|
debug_name = "client";
|
2011-12-13 20:19:29 +00:00
|
|
|
#else
|
2016-09-23 22:40:03 +00:00
|
|
|
// For the adbd daemon in the system image we need to distinguish
|
|
|
|
// between the device, and the emulator.
|
|
|
|
if (android::base::GetBoolProperty("ro.kernel.qemu", false)) {
|
|
|
|
// Running inside the emulator: use QEMUD pipe as the transport.
|
2015-08-11 20:40:42 +00:00
|
|
|
func = qemu_socket_thread;
|
|
|
|
} else {
|
2016-09-23 22:40:03 +00:00
|
|
|
// Running inside the device: use TCP socket as the transport.
|
2015-08-11 20:40:42 +00:00
|
|
|
func = server_socket_thread;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2015-08-11 20:40:42 +00:00
|
|
|
debug_name = "server";
|
|
|
|
#endif // !ADB_HOST
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: local %s init", debug_name);
|
2017-04-13 00:00:49 +00:00
|
|
|
std::thread(func, port).detach();
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remote_kick(atransport *t)
|
|
|
|
{
|
|
|
|
int fd = t->sfd;
|
|
|
|
t->sfd = -1;
|
2009-10-12 03:04:18 +00:00
|
|
|
adb_shutdown(fd);
|
2009-03-04 03:32:55 +00:00
|
|
|
adb_close(fd);
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2015-08-11 20:40:42 +00:00
|
|
|
int nn;
|
2016-09-21 19:37:10 +00:00
|
|
|
std::lock_guard<std::mutex> lock(local_transports_lock);
|
2015-08-11 20:40:42 +00:00
|
|
|
for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
|
|
|
|
if (local_transports[nn] == t) {
|
|
|
|
local_transports[nn] = NULL;
|
|
|
|
break;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remote_close(atransport *t)
|
|
|
|
{
|
adb server: don't close stale fd when TCP transport is closed
I think this fixes a scary bug that could be on all host platforms.
When running 'adb unroot' with an emulator, the connection to the
emulator is dropped (as expected). I noticed that the adb.log showed:
_fh_from_int: 1168: 5280 | _fh_from_int: invalid fd 106 passed to adb_close
Background: Every transport has a socketpair (two bidirectional sockets
connected to each other to form one 'pipe') that are used as follows:
* When adb wants to write to a transport, it writes to
t->transport_socket (half of the socketpair). An input thread reads from
t->fd (the other half of the socketpair) and writes the data to the
underlying transport (TCP, USB).
* An output thread reads from the underlying transport (TCP, USB) and
writes the data to t->fd. The main thread runs fdevent_loop() which
reads from t->transport_socket and processes the packets (that really
came from the underlying transport).
So t->fd and t->transport_socket are just an intermediate pipe between
transport agnostic code in adb and the underlying transport (TCP, USB).
Here's what I think is going on:
1. When the TCP transport is closed (such as when running adb unroot),
adb server's output thread notices this (adb_read() returns zero), and
it writes a special packet to t->fd.
2. The main thread processes the special packet by writing the special
packet to the input thread.
3. input_thread() sees the special packet, so it breaks out of a read
loop and calls transport_unref() which calls transport_unref_locked().
4. transport_unref_locked() calls t->close() which is a function pointer
that points to transport_local.cpp: remote_close() which calls
adb_close(t->fd). <----- ****THIS IS THE BUG****
I think this is a (very old) typo and it should instead be
adb_close(t->sfd) (the transport’s actual TCP socket) because it does
not make sense for the particular transport mechanism (TCP, USB) to be
messing with a socket of the socketpair of the transport agnostic code
(t->fd).
5. transport_unref_locked() calls remove_transport() which writes an
action to another special socketpair.
6. The action is read and eventually transport_registration_func() is
called and it calls adb_close(t->fd). But t->fd was already
(erroneously) closed in #4 above!! Anyway, this causes the adb.log
output.
The fix is to fix the typo changing t->fd to t->sfd and adding some
resiliency around whether the socket has already been closed (probably
by remote_kick()).
I tested this by putting a new adbd on an emulator, a new adb on Linux
and Windows and running the adb unroot scenario and checking adb.log. I
also ran test_adb.py (which doesn't totally work without problems with
an emulator, but I'll leave that to another day.)
Change-Id: I188b6c74917a3d721c150fd17ed0f2b63a2178c3
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-06-09 19:32:17 +00:00
|
|
|
int fd = t->sfd;
|
|
|
|
if (fd != -1) {
|
|
|
|
t->sfd = -1;
|
|
|
|
adb_close(fd);
|
|
|
|
}
|
2016-04-29 23:53:52 +00:00
|
|
|
#if ADB_HOST
|
|
|
|
int local_port;
|
|
|
|
if (t->GetLocalPortForEmulator(&local_port)) {
|
|
|
|
VLOG(TRANSPORT) << "remote_close, local_port = " << local_port;
|
|
|
|
std::unique_lock<std::mutex> lock(retry_ports_lock);
|
|
|
|
RetryPort port;
|
|
|
|
port.port = local_port;
|
|
|
|
port.retry_count = LOCAL_PORT_RETRY_COUNT;
|
|
|
|
retry_ports.push_back(port);
|
|
|
|
retry_ports_cond.notify_one();
|
|
|
|
}
|
|
|
|
#endif
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2010-04-26 09:17:43 +00:00
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
/* Only call this function if you already hold local_transports_lock. */
|
2016-04-29 23:53:52 +00:00
|
|
|
static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
|
2010-04-26 09:17:43 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
|
2016-04-29 23:53:52 +00:00
|
|
|
int local_port;
|
|
|
|
if (local_transports[i] && local_transports[i]->GetLocalPortForEmulator(&local_port)) {
|
|
|
|
if (local_port == adb_port) {
|
|
|
|
return local_transports[i];
|
|
|
|
}
|
2010-04-26 09:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:22:55 +00:00
|
|
|
std::string getEmulatorSerialString(int console_port)
|
|
|
|
{
|
|
|
|
return android::base::StringPrintf("emulator-%d", console_port);
|
|
|
|
}
|
|
|
|
|
2010-04-26 09:17:43 +00:00
|
|
|
atransport* find_emulator_transport_by_adb_port(int adb_port)
|
|
|
|
{
|
2016-09-21 19:37:10 +00:00
|
|
|
std::lock_guard<std::mutex> lock(local_transports_lock);
|
2010-04-26 09:17:43 +00:00
|
|
|
atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:22:55 +00:00
|
|
|
atransport* find_emulator_transport_by_console_port(int console_port)
|
|
|
|
{
|
|
|
|
return find_transport(getEmulatorSerialString(console_port).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-26 09:17:43 +00:00
|
|
|
/* Only call this function if you already hold local_transports_lock. */
|
|
|
|
int get_available_local_transport_index_locked()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
|
|
|
|
if (local_transports[i] == NULL) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_available_local_transport_index()
|
|
|
|
{
|
2016-09-21 19:37:10 +00:00
|
|
|
std::lock_guard<std::mutex> lock(local_transports_lock);
|
2010-04-26 09:17:43 +00:00
|
|
|
int result = get_available_local_transport_index_locked();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int init_socket_transport(atransport *t, int s, int adb_port, int local)
|
2009-03-04 03:32:55 +00:00
|
|
|
{
|
|
|
|
int fail = 0;
|
|
|
|
|
2016-04-18 18:22:34 +00:00
|
|
|
t->SetKickFunction(remote_kick);
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
t->SetWriteFunction(remote_write);
|
2009-03-04 03:32:55 +00:00
|
|
|
t->close = remote_close;
|
|
|
|
t->read_from_remote = remote_read;
|
|
|
|
t->sfd = s;
|
|
|
|
t->sync_token = 1;
|
|
|
|
t->type = kTransportLocal;
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2015-08-11 20:40:42 +00:00
|
|
|
if (local) {
|
2016-09-21 19:37:10 +00:00
|
|
|
std::lock_guard<std::mutex> lock(local_transports_lock);
|
|
|
|
t->SetLocalPortForEmulator(adb_port);
|
|
|
|
atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
|
|
|
|
int index = get_available_local_transport_index_locked();
|
|
|
|
if (existing_transport != NULL) {
|
|
|
|
D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
|
|
|
|
fail = -1;
|
|
|
|
} else if (index < 0) {
|
|
|
|
// Too many emulators.
|
|
|
|
D("cannot register more emulators. Maximum is %d", ADB_LOCAL_TRANSPORT_MAX);
|
|
|
|
fail = -1;
|
|
|
|
} else {
|
|
|
|
local_transports[index] = t;
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return fail;
|
|
|
|
}
|