2009-03-04 04:32:55 +01: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-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG ADB
|
2015-03-19 23:21:08 +01:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "adb.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2015-03-09 05:12:08 +01:00
|
|
|
#include <stdarg.h>
|
2012-05-25 22:55:46 +02:00
|
|
|
#include <stddef.h>
|
2015-03-09 05:12:08 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
2009-05-26 00:17:55 +02:00
|
|
|
#include <sys/time.h>
|
2015-03-09 05:12:08 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
2016-11-15 21:37:32 +01:00
|
|
|
#include <chrono>
|
2017-05-04 07:37:10 +02:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2015-03-09 05:12:08 +01:00
|
|
|
#include <string>
|
2020-02-27 01:39:20 +01:00
|
|
|
#include <string_view>
|
2016-11-15 21:37:32 +01:00
|
|
|
#include <thread>
|
2015-03-19 21:25:27 +01:00
|
|
|
#include <vector>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-01-27 17:52:53 +01:00
|
|
|
#include <android-base/errors.h>
|
2016-09-01 00:07:18 +02:00
|
|
|
#include <android-base/file.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/macros.h>
|
2016-01-21 17:40:59 +01:00
|
|
|
#include <android-base/parsenetaddress.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2018-02-28 00:49:23 +01:00
|
|
|
#include <build/version.h>
|
2018-08-29 23:58:02 +02:00
|
|
|
#include <platform_tools_version.h>
|
2015-04-20 17:09:20 +02:00
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#include "adb_auth.h"
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_io.h"
|
2015-02-19 03:03:26 +01:00
|
|
|
#include "adb_listeners.h"
|
2017-06-15 17:35:24 +02:00
|
|
|
#include "adb_unique_fd.h"
|
2015-07-18 21:21:30 +02:00
|
|
|
#include "adb_utils.h"
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
#include "adb_wifi.h"
|
2017-05-04 07:37:10 +02:00
|
|
|
#include "sysdeps/chrono.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "transport.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#if !ADB_HOST
|
2013-02-28 23:12:58 +01:00
|
|
|
#include <sys/capability.h>
|
2012-08-15 06:00:22 +02:00
|
|
|
#include <sys/mount.h>
|
2016-09-24 00:40:03 +02:00
|
|
|
#include <android-base/properties.h>
|
2016-11-15 21:37:32 +01:00
|
|
|
using namespace std::chrono_literals;
|
2020-02-27 01:39:20 +01:00
|
|
|
|
|
|
|
#include "daemon/logging.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
2020-03-28 02:09:56 +01:00
|
|
|
#if ADB_HOST
|
|
|
|
#include "client/usb.h"
|
|
|
|
#endif
|
|
|
|
|
2015-08-12 17:32:10 +02:00
|
|
|
std::string adb_version() {
|
|
|
|
// Don't change the format of this --- it's parsed by ddmlib.
|
2017-03-31 00:08:28 +02:00
|
|
|
return android::base::StringPrintf(
|
|
|
|
"Android Debug Bridge version %d.%d.%d\n"
|
2018-08-29 23:58:02 +02:00
|
|
|
"Version %s-%s\n"
|
2017-03-31 00:08:28 +02:00
|
|
|
"Installed as %s\n",
|
2018-01-02 21:01:43 +01:00
|
|
|
ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
|
2018-08-29 23:58:02 +02:00
|
|
|
PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str(),
|
|
|
|
android::base::GetExecutablePath().c_str());
|
2015-08-12 17:32:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-06 22:31:44 +02:00
|
|
|
uint32_t calculate_apacket_checksum(const apacket* p) {
|
|
|
|
uint32_t sum = 0;
|
2018-02-06 03:49:10 +01:00
|
|
|
for (size_t i = 0; i < p->msg.data_length; ++i) {
|
|
|
|
sum += static_cast<uint8_t>(p->payload[i]);
|
2016-10-06 22:31:44 +02:00
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
apacket* get_apacket(void)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2018-02-06 03:49:10 +01:00
|
|
|
apacket* p = new apacket();
|
2015-02-26 02:51:28 +01:00
|
|
|
if (p == nullptr) {
|
2018-10-19 22:59:44 +02:00
|
|
|
LOG(FATAL) << "failed to allocate an apacket";
|
2015-02-26 02:51:28 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 03:49:10 +01:00
|
|
|
memset(&p->msg, 0, sizeof(p->msg));
|
2009-03-04 04:32:55 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_apacket(apacket *p)
|
|
|
|
{
|
2018-02-06 03:49:10 +01:00
|
|
|
delete p;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
void handle_online(atransport *t)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("adb: online");
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
t->online = 1;
|
2018-04-17 23:25:04 +02:00
|
|
|
t->SetConnectionEstablished(true);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_offline(atransport *t)
|
|
|
|
{
|
2018-02-16 22:24:58 +01:00
|
|
|
if (t->GetConnectionState() == kCsOffline) {
|
|
|
|
LOG(INFO) << t->serial_name() << ": already offline";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << t->serial_name() << ": offline";
|
|
|
|
|
|
|
|
t->SetConnectionState(kCsOffline);
|
|
|
|
|
|
|
|
// Close the associated usb
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
t->online = 0;
|
2015-08-26 20:18:42 +02:00
|
|
|
|
2015-08-29 00:09:44 +02:00
|
|
|
// This is necessary to avoid a race condition that occurred when a transport closes
|
2015-08-26 20:18:42 +02:00
|
|
|
// while a client socket is still active.
|
|
|
|
close_all_sockets(t);
|
|
|
|
|
2015-08-29 00:09:44 +02:00
|
|
|
t->RunDisconnects();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#if DEBUG_PACKETS
|
2009-03-04 04:32:55 +01:00
|
|
|
#define DUMPMAX 32
|
|
|
|
void print_packet(const char *label, apacket *p)
|
|
|
|
{
|
2018-02-06 03:49:10 +01:00
|
|
|
const char* tag;
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
switch(p->msg.command){
|
|
|
|
case A_SYNC: tag = "SYNC"; break;
|
|
|
|
case A_CNXN: tag = "CNXN" ; break;
|
|
|
|
case A_OPEN: tag = "OPEN"; break;
|
|
|
|
case A_OKAY: tag = "OKAY"; break;
|
|
|
|
case A_CLSE: tag = "CLSE"; break;
|
|
|
|
case A_WRTE: tag = "WRTE"; break;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
case A_AUTH: tag = "AUTH"; break;
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
case A_STLS:
|
|
|
|
tag = "ATLS";
|
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
default: tag = "????"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: %s %08x %08x %04x \"",
|
|
|
|
label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
|
|
|
|
count = p->msg.data_length;
|
2018-02-06 03:49:10 +01:00
|
|
|
const char* x = p->payload.data();
|
|
|
|
if (count > DUMPMAX) {
|
2009-03-04 04:32:55 +01:00
|
|
|
count = DUMPMAX;
|
|
|
|
tag = "\n";
|
|
|
|
} else {
|
|
|
|
tag = "\"\n";
|
|
|
|
}
|
2018-02-06 03:49:10 +01:00
|
|
|
while (count-- > 0) {
|
|
|
|
if ((*x >= ' ') && (*x < 127)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fputc(*x, stderr);
|
|
|
|
} else {
|
|
|
|
fputc('.', stderr);
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
fputs(tag, stderr);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void send_ready(unsigned local, unsigned remote, atransport *t)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Calling send_ready");
|
2009-03-04 04:32:55 +01:00
|
|
|
apacket *p = get_apacket();
|
|
|
|
p->msg.command = A_OKAY;
|
|
|
|
p->msg.arg0 = local;
|
|
|
|
p->msg.arg1 = remote;
|
|
|
|
send_packet(p, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_close(unsigned local, unsigned remote, atransport *t)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Calling send_close");
|
2009-03-04 04:32:55 +01:00
|
|
|
apacket *p = get_apacket();
|
|
|
|
p->msg.command = A_CLSE;
|
|
|
|
p->msg.arg0 = local;
|
|
|
|
p->msg.arg1 = remote;
|
|
|
|
send_packet(p, t);
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
std::string get_connection_string() {
|
|
|
|
std::vector<std::string> connection_properties;
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
|
|
|
static const char* cnxn_props[] = {
|
2012-05-25 23:10:02 +02:00
|
|
|
"ro.product.name",
|
|
|
|
"ro.product.model",
|
|
|
|
"ro.product.device",
|
|
|
|
};
|
2015-05-18 22:06:53 +02:00
|
|
|
|
2016-09-24 00:40:03 +02:00
|
|
|
for (const auto& prop : cnxn_props) {
|
|
|
|
std::string value = std::string(prop) + "=" + android::base::GetProperty(prop, "");
|
|
|
|
connection_properties.push_back(value);
|
2012-05-25 23:10:02 +02:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-18 22:06:53 +02:00
|
|
|
|
|
|
|
connection_properties.push_back(android::base::StringPrintf(
|
2015-09-22 19:43:08 +02:00
|
|
|
"features=%s", FeatureSetToString(supported_features()).c_str()));
|
2015-05-18 22:06:53 +02:00
|
|
|
|
|
|
|
return android::base::StringPrintf(
|
|
|
|
"%s::%s", adb_device_banner,
|
|
|
|
android::base::Join(connection_properties, ';').c_str());
|
2012-05-25 23:10:02 +02:00
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
void send_tls_request(atransport* t) {
|
|
|
|
D("Calling send_tls_request");
|
|
|
|
apacket* p = get_apacket();
|
|
|
|
p->msg.command = A_STLS;
|
|
|
|
p->msg.arg0 = A_STLS_VERSION;
|
|
|
|
p->msg.data_length = 0;
|
|
|
|
send_packet(p, t);
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
void send_connect(atransport* t) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Calling send_connect");
|
2015-05-18 22:06:53 +02:00
|
|
|
apacket* cp = get_apacket();
|
2009-03-04 04:32:55 +01:00
|
|
|
cp->msg.command = A_CNXN;
|
2017-12-07 20:40:00 +01:00
|
|
|
// Send the max supported version, but because the transport is
|
|
|
|
// initialized to A_VERSION_MIN, this will be compatible with every
|
|
|
|
// device.
|
|
|
|
cp->msg.arg0 = A_VERSION;
|
2015-07-13 20:12:28 +02:00
|
|
|
cp->msg.arg1 = t->get_max_payload();
|
2015-05-18 22:06:53 +02:00
|
|
|
|
|
|
|
std::string connection_str = get_connection_string();
|
|
|
|
// Connect and auth packets are limited to MAX_PAYLOAD_V1 because we don't
|
|
|
|
// yet know how much data the other size is willing to accept.
|
|
|
|
if (connection_str.length() > MAX_PAYLOAD_V1) {
|
|
|
|
LOG(FATAL) << "Connection banner is too long (length = "
|
|
|
|
<< connection_str.length() << ")";
|
|
|
|
}
|
|
|
|
|
2018-03-08 01:52:28 +01:00
|
|
|
cp->payload.assign(connection_str.begin(), connection_str.end());
|
2018-02-06 03:49:10 +01:00
|
|
|
cp->msg.data_length = cp->payload.size();
|
2015-05-18 22:06:53 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
send_packet(cp, t);
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
void parse_banner(const std::string& banner, atransport* t) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("parse_banner: %s", banner.c_str());
|
2015-04-25 08:02:00 +02:00
|
|
|
|
|
|
|
// The format is something like:
|
|
|
|
// "device::ro.product.name=x;ro.product.model=y;ro.product.device=z;".
|
|
|
|
std::vector<std::string> pieces = android::base::Split(banner, ":");
|
|
|
|
|
2015-09-25 22:04:21 +02:00
|
|
|
// Reset the features list or else if the server sends no features we may
|
|
|
|
// keep the existing feature set (http://b/24405971).
|
|
|
|
t->SetFeatures("");
|
|
|
|
|
2015-04-25 08:02:00 +02:00
|
|
|
if (pieces.size() > 2) {
|
|
|
|
const std::string& props = pieces[2];
|
2015-10-08 00:59:35 +02:00
|
|
|
for (const auto& prop : android::base::Split(props, ";")) {
|
2015-04-25 08:02:00 +02:00
|
|
|
// The list of properties was traditionally ;-terminated rather than ;-separated.
|
|
|
|
if (prop.empty()) continue;
|
|
|
|
|
|
|
|
std::vector<std::string> key_value = android::base::Split(prop, "=");
|
|
|
|
if (key_value.size() != 2) continue;
|
|
|
|
|
|
|
|
const std::string& key = key_value[0];
|
|
|
|
const std::string& value = key_value[1];
|
|
|
|
if (key == "ro.product.name") {
|
2018-07-19 06:18:27 +02:00
|
|
|
t->product = value;
|
2015-04-25 08:02:00 +02:00
|
|
|
} else if (key == "ro.product.model") {
|
2018-07-19 06:18:27 +02:00
|
|
|
t->model = value;
|
2015-04-25 08:02:00 +02:00
|
|
|
} else if (key == "ro.product.device") {
|
2018-07-19 06:18:27 +02:00
|
|
|
t->device = value;
|
2015-05-18 22:06:53 +02:00
|
|
|
} else if (key == "features") {
|
2015-09-22 19:43:08 +02:00
|
|
|
t->SetFeatures(value);
|
2012-05-25 23:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-25 08:02:00 +02:00
|
|
|
const std::string& type = pieces[0];
|
|
|
|
if (type == "bootloader") {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("setting connection_state to kCsBootloader");
|
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 01:01:01 +01:00
|
|
|
t->SetConnectionState(kCsBootloader);
|
2015-04-25 08:02:00 +02:00
|
|
|
} else if (type == "device") {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("setting connection_state to kCsDevice");
|
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 01:01:01 +01:00
|
|
|
t->SetConnectionState(kCsDevice);
|
2015-04-25 08:02:00 +02:00
|
|
|
} else if (type == "recovery") {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("setting connection_state to kCsRecovery");
|
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 01:01:01 +01:00
|
|
|
t->SetConnectionState(kCsRecovery);
|
2015-04-25 08:02:00 +02:00
|
|
|
} else if (type == "sideload") {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("setting connection_state to kCsSideload");
|
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 01:01:01 +01:00
|
|
|
t->SetConnectionState(kCsSideload);
|
2019-04-08 08:24:03 +02:00
|
|
|
} else if (type == "rescue") {
|
|
|
|
D("setting connection_state to kCsRescue");
|
|
|
|
t->SetConnectionState(kCsRescue);
|
2015-04-30 07:37:25 +02:00
|
|
|
} else {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("setting connection_state to kCsHost");
|
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 01:01:01 +01:00
|
|
|
t->SetConnectionState(kCsHost);
|
2012-01-09 23:54:53 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
static void handle_new_connection(atransport* t, apacket* p) {
|
2018-02-16 22:24:58 +01:00
|
|
|
handle_offline(t);
|
2015-05-18 22:06:53 +02:00
|
|
|
|
|
|
|
t->update_version(p->msg.arg0, p->msg.arg1);
|
2018-03-08 01:52:28 +01:00
|
|
|
std::string banner(p->payload.begin(), p->payload.end());
|
|
|
|
parse_banner(banner, t);
|
2015-05-18 22:06:53 +02:00
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
handle_online(t);
|
|
|
|
#else
|
2020-02-27 01:39:20 +01:00
|
|
|
ADB_LOG(Connection) << "received CNXN: version=" << p->msg.arg0 << ", maxdata = " << p->msg.arg1
|
|
|
|
<< ", banner = '" << banner << "'";
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
if (t->use_tls) {
|
|
|
|
// We still handshake in TLS mode. If auth_required is disabled,
|
|
|
|
// we'll just not verify the client's certificate. This should be the
|
|
|
|
// first packet the client receives to indicate the new protocol.
|
|
|
|
send_tls_request(t);
|
|
|
|
} else if (!auth_required) {
|
2019-10-22 21:30:39 +02:00
|
|
|
LOG(INFO) << "authentication not required";
|
2015-05-18 22:06:53 +02:00
|
|
|
handle_online(t);
|
|
|
|
send_connect(t);
|
|
|
|
} else {
|
|
|
|
send_auth_request(t);
|
|
|
|
}
|
|
|
|
#endif
|
2017-05-04 07:37:10 +02:00
|
|
|
|
|
|
|
update_transports();
|
2015-05-18 22:06:53 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
void handle_packet(apacket *p, atransport *t)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("handle_packet() %c%c%c%c", ((char*) (&(p->msg.command)))[0],
|
2010-06-16 15:11:28 +02:00
|
|
|
((char*) (&(p->msg.command)))[1],
|
|
|
|
((char*) (&(p->msg.command)))[2],
|
|
|
|
((char*) (&(p->msg.command)))[3]);
|
2009-03-04 04:32:55 +01:00
|
|
|
print_packet("recv", p);
|
2018-02-06 03:49:10 +01:00
|
|
|
CHECK_EQ(p->payload.size(), p->msg.data_length);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
switch(p->msg.command){
|
2015-05-18 22:06:53 +02:00
|
|
|
case A_CNXN: // CONNECT(version, maxdata, "system-id-string")
|
|
|
|
handle_new_connection(t, p);
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
break;
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
case A_STLS: // TLS(version, "")
|
|
|
|
t->use_tls = true;
|
|
|
|
#if ADB_HOST
|
|
|
|
send_tls_request(t);
|
|
|
|
adb_auth_tls_handshake(t);
|
|
|
|
#else
|
|
|
|
adbd_auth_tls_handshake(t);
|
|
|
|
#endif
|
|
|
|
break;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
|
|
|
|
case A_AUTH:
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
// All AUTH commands are ignored in TLS mode
|
|
|
|
if (t->use_tls) {
|
|
|
|
break;
|
|
|
|
}
|
2016-10-06 04:02:29 +02:00
|
|
|
switch (p->msg.arg0) {
|
|
|
|
#if ADB_HOST
|
|
|
|
case ADB_AUTH_TOKEN:
|
2018-05-05 01:04:49 +02:00
|
|
|
if (t->GetConnectionState() != kCsAuthorizing) {
|
|
|
|
t->SetConnectionState(kCsAuthorizing);
|
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 01:01:01 +01:00
|
|
|
}
|
2018-02-06 03:49:10 +01:00
|
|
|
send_auth_response(p->payload.data(), p->msg.data_length, t);
|
2016-10-06 04:02:29 +02:00
|
|
|
break;
|
|
|
|
#else
|
2018-03-08 01:52:28 +01:00
|
|
|
case ADB_AUTH_SIGNATURE: {
|
|
|
|
// TODO: Switch to string_view.
|
|
|
|
std::string signature(p->payload.begin(), p->payload.end());
|
2019-04-26 03:33:35 +02:00
|
|
|
std::string auth_key;
|
|
|
|
if (adbd_auth_verify(t->token, sizeof(t->token), signature, &auth_key)) {
|
2016-10-06 04:02:29 +02:00
|
|
|
adbd_auth_verified(t);
|
|
|
|
t->failed_auth_attempts = 0;
|
2019-04-26 03:33:35 +02:00
|
|
|
t->auth_key = auth_key;
|
|
|
|
adbd_notify_framework_connected_key(t);
|
2016-10-06 04:02:29 +02:00
|
|
|
} else {
|
2016-11-15 21:37:32 +01:00
|
|
|
if (t->failed_auth_attempts++ > 256) std::this_thread::sleep_for(1s);
|
2016-10-06 04:02:29 +02:00
|
|
|
send_auth_request(t);
|
|
|
|
}
|
|
|
|
break;
|
2018-03-08 01:52:28 +01:00
|
|
|
}
|
2016-10-06 04:02:29 +02:00
|
|
|
|
|
|
|
case ADB_AUTH_RSAPUBLICKEY:
|
2019-04-26 03:33:35 +02:00
|
|
|
t->auth_key = std::string(p->payload.data());
|
|
|
|
adbd_auth_confirm_key(t);
|
2016-10-06 04:02:29 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
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 01:01:01 +01:00
|
|
|
t->SetConnectionState(kCsOffline);
|
2016-10-06 04:02:29 +02:00
|
|
|
handle_offline(t);
|
|
|
|
break;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case A_OPEN: /* OPEN(local-id, 0, "destination") */
|
2013-12-13 14:09:44 +01:00
|
|
|
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
|
2018-12-13 23:21:00 +01:00
|
|
|
std::string_view address(p->payload.begin(), p->payload.size());
|
|
|
|
|
2018-12-27 20:16:27 +01:00
|
|
|
// Historically, we received service names as a char*, and stopped at the first NUL
|
|
|
|
// byte. The client sent strings with null termination, which post-string_view, start
|
|
|
|
// being interpreted as part of the string, unless we explicitly strip them.
|
|
|
|
address = StripTrailingNulls(address);
|
|
|
|
|
2018-12-13 23:21:00 +01:00
|
|
|
asocket* s = create_local_service_socket(address, t);
|
2016-06-15 23:46:56 +02:00
|
|
|
if (s == nullptr) {
|
2009-03-04 04:32:55 +01:00
|
|
|
send_close(0, p->msg.arg0, t);
|
|
|
|
} else {
|
|
|
|
s->peer = create_remote_socket(p->msg.arg0, t);
|
|
|
|
s->peer->peer = s;
|
|
|
|
send_ready(s->id, s->peer->id, t);
|
|
|
|
s->ready(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_OKAY: /* READY(local-id, remote-id, "") */
|
2013-12-13 14:09:44 +01:00
|
|
|
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
|
2016-06-15 23:46:56 +02:00
|
|
|
asocket* s = find_local_socket(p->msg.arg1, 0);
|
|
|
|
if (s) {
|
2018-07-14 03:15:16 +02:00
|
|
|
if(s->peer == nullptr) {
|
2013-12-13 14:09:44 +01:00
|
|
|
/* On first READY message, create the connection. */
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer = create_remote_socket(p->msg.arg0, t);
|
|
|
|
s->peer->peer = s;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->ready(s);
|
|
|
|
} else if (s->peer->id == p->msg.arg0) {
|
|
|
|
/* Other READY messages must use the same local-id */
|
|
|
|
s->ready(s);
|
|
|
|
} else {
|
2018-07-19 06:18:27 +02:00
|
|
|
D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s", p->msg.arg0,
|
|
|
|
p->msg.arg1, s->peer->id, p->msg.arg1, t->serial.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-29 21:25:33 +02:00
|
|
|
} else {
|
|
|
|
// When receiving A_OKAY from device for A_OPEN request, the host server may
|
|
|
|
// have closed the local socket because of client disconnection. Then we need
|
|
|
|
// to send A_CLSE back to device to close the service on device.
|
|
|
|
send_close(p->msg.arg1, p->msg.arg0, t);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-13 14:09:44 +01:00
|
|
|
case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
|
|
|
|
if (t->online && p->msg.arg1 != 0) {
|
2016-06-15 23:46:56 +02:00
|
|
|
asocket* s = find_local_socket(p->msg.arg1, p->msg.arg0);
|
|
|
|
if (s) {
|
2013-12-13 14:09:44 +01:00
|
|
|
/* According to protocol.txt, p->msg.arg0 might be 0 to indicate
|
|
|
|
* a failed OPEN only. However, due to a bug in previous ADB
|
|
|
|
* versions, CLOSE(0, remote-id, "") was also used for normal
|
|
|
|
* CLOSE() operations.
|
|
|
|
*
|
|
|
|
* This is bad because it means a compromised adbd could
|
|
|
|
* send packets to close connections between the host and
|
|
|
|
* other devices. To avoid this, only allow this if the local
|
|
|
|
* socket has a peer on the same transport.
|
|
|
|
*/
|
|
|
|
if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
|
2018-07-19 06:18:27 +02:00
|
|
|
D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s", p->msg.arg1,
|
|
|
|
t->serial.c_str(), s->peer->transport->serial.c_str());
|
2013-12-13 14:09:44 +01:00
|
|
|
} else {
|
|
|
|
s->close(s);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-13 14:09:44 +01:00
|
|
|
case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
|
|
|
|
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
|
2016-06-15 23:46:56 +02:00
|
|
|
asocket* s = find_local_socket(p->msg.arg1, p->msg.arg0);
|
|
|
|
if (s) {
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned rid = p->msg.arg0;
|
2018-02-06 03:49:10 +01:00
|
|
|
if (s->enqueue(s, std::move(p->payload)) == 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Enqueue the socket");
|
2009-03-04 04:32:55 +01:00
|
|
|
send_ready(s->id, rid, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("handle_packet: what is %08x?!\n", p->msg.command);
|
|
|
|
}
|
|
|
|
|
|
|
|
put_apacket(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2012-12-07 03:18:12 +01:00
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2015-09-17 01:40:06 +02:00
|
|
|
// Try to make a handle non-inheritable and if there is an error, don't output
|
|
|
|
// any error info, but leave GetLastError() for the caller to read. This is
|
|
|
|
// convenient if the caller is expecting that this may fail and they'd like to
|
|
|
|
// ignore such a failure.
|
|
|
|
static bool _try_make_handle_noninheritable(HANDLE h) {
|
2015-08-27 03:46:09 +02:00
|
|
|
if (h != INVALID_HANDLE_VALUE && h != NULL) {
|
2015-09-17 01:40:06 +02:00
|
|
|
return SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to make a handle non-inheritable with the expectation that this should
|
|
|
|
// succeed, so if this fails, output error info.
|
|
|
|
static bool _make_handle_noninheritable(HANDLE h) {
|
|
|
|
if (!_try_make_handle_noninheritable(h)) {
|
|
|
|
// Show the handle value to give us a clue in case we have problems
|
|
|
|
// with pseudo-handle values.
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: cannot make handle 0x%p non-inheritable: %s\n", h,
|
|
|
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
2015-09-17 01:40:06 +02:00
|
|
|
return false;
|
2015-08-27 03:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create anonymous pipe, preventing inheritance of the read pipe and setting
|
|
|
|
// security of the write pipe to sa.
|
|
|
|
static bool _create_anonymous_pipe(unique_handle* pipe_read_out,
|
|
|
|
unique_handle* pipe_write_out,
|
|
|
|
SECURITY_ATTRIBUTES* sa) {
|
|
|
|
HANDLE pipe_read_raw = NULL;
|
|
|
|
HANDLE pipe_write_raw = NULL;
|
|
|
|
if (!CreatePipe(&pipe_read_raw, &pipe_write_raw, sa, 0)) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: CreatePipe failed: %s\n",
|
2016-01-27 17:52:53 +01:00
|
|
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
2015-08-27 03:46:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_handle pipe_read(pipe_read_raw);
|
|
|
|
pipe_read_raw = NULL;
|
|
|
|
unique_handle pipe_write(pipe_write_raw);
|
|
|
|
pipe_write_raw = NULL;
|
|
|
|
|
|
|
|
if (!_make_handle_noninheritable(pipe_read.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pipe_read_out = std::move(pipe_read);
|
|
|
|
*pipe_write_out = std::move(pipe_write);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-22 02:23:51 +01:00
|
|
|
// Read from a pipe (that we take ownership of) and write the result to stdout/stderr. Return on
|
|
|
|
// error or when the pipe is closed. Internally makes inheritable handles, so this should not be
|
|
|
|
// called if subprocesses may be started concurrently.
|
2015-08-27 03:46:09 +02:00
|
|
|
static unsigned _redirect_pipe_thread(HANDLE h, DWORD nStdHandle) {
|
|
|
|
// Take ownership of the HANDLE and close when we're done.
|
|
|
|
unique_handle read_pipe(h);
|
2015-11-22 02:23:51 +01:00
|
|
|
const char* output_name = nStdHandle == STD_OUTPUT_HANDLE ? "stdout" : "stderr";
|
|
|
|
const int original_fd = fileno(nStdHandle == STD_OUTPUT_HANDLE ? stdout : stderr);
|
|
|
|
std::unique_ptr<FILE, decltype(&fclose)> stream(nullptr, fclose);
|
|
|
|
|
|
|
|
if (original_fd == -1) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: failed to get file descriptor for %s: %s\n", output_name,
|
|
|
|
strerror(errno));
|
2015-11-22 02:23:51 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If fileno() is -2, stdout/stderr is not associated with an output stream, so we should read,
|
|
|
|
// but don't write. Otherwise, make a FILE* identical to stdout/stderr except that it is in
|
|
|
|
// binary mode with no CR/LR translation since we're reading raw.
|
|
|
|
if (original_fd >= 0) {
|
|
|
|
// This internally makes a duplicate file handle that is inheritable, so callers should not
|
|
|
|
// call this function if subprocesses may be started concurrently.
|
|
|
|
const int fd = dup(original_fd);
|
|
|
|
if (fd == -1) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: failed to duplicate file descriptor for %s: %s\n", output_name,
|
2015-11-22 02:23:51 +01:00
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that although we call fdopen() below with a binary flag, it may not adhere to that
|
|
|
|
// flag, so we have to set the mode manually.
|
|
|
|
if (_setmode(fd, _O_BINARY) == -1) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: failed to set binary mode for duplicate of %s: %s\n", output_name,
|
2015-11-22 02:23:51 +01:00
|
|
|
strerror(errno));
|
|
|
|
unix_close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.reset(fdopen(fd, "wb"));
|
|
|
|
if (stream.get() == nullptr) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: failed to open duplicate stream for %s: %s\n", output_name,
|
2015-11-22 02:23:51 +01:00
|
|
|
strerror(errno));
|
|
|
|
unix_close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unbuffer the stream because it will be buffered by default and we want subprocess output
|
|
|
|
// to be shown immediately.
|
|
|
|
if (setvbuf(stream.get(), NULL, _IONBF, 0) == -1) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: failed to unbuffer %s: %s\n", output_name, strerror(errno));
|
2015-11-22 02:23:51 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fd will be closed when stream is closed.
|
|
|
|
}
|
2015-08-27 03:46:09 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
char buf[64 * 1024];
|
|
|
|
DWORD bytes_read = 0;
|
|
|
|
if (!ReadFile(read_pipe.get(), buf, sizeof(buf), &bytes_read, NULL)) {
|
|
|
|
const DWORD err = GetLastError();
|
|
|
|
// ERROR_BROKEN_PIPE is expected when the subprocess closes
|
|
|
|
// the other end of the pipe.
|
|
|
|
if (err == ERROR_BROKEN_PIPE) {
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
} else {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: failed to read from %s: %s\n", output_name,
|
2016-01-27 17:52:53 +01:00
|
|
|
android::base::SystemErrorCodeToString(err).c_str());
|
2015-08-27 03:46:09 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 02:23:51 +01:00
|
|
|
// Don't try to write if our stdout/stderr was not setup by the parent process.
|
|
|
|
if (stream) {
|
|
|
|
// fwrite() actually calls adb_fwrite() which can write UTF-8 to the console.
|
|
|
|
const size_t bytes_written = fwrite(buf, 1, bytes_read, stream.get());
|
2015-08-27 03:46:09 +02:00
|
|
|
if (bytes_written != bytes_read) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: error: only wrote %zu of %lu bytes to %s\n", bytes_written,
|
|
|
|
bytes_read, output_name);
|
2015-08-27 03:46:09 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned __stdcall _redirect_stdout_thread(HANDLE h) {
|
2015-08-29 01:37:29 +02:00
|
|
|
adb_thread_setname("stdout redirect");
|
2015-08-27 03:46:09 +02:00
|
|
|
return _redirect_pipe_thread(h, STD_OUTPUT_HANDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned __stdcall _redirect_stderr_thread(HANDLE h) {
|
2015-08-29 01:37:29 +02:00
|
|
|
adb_thread_setname("stderr redirect");
|
2015-08-27 03:46:09 +02:00
|
|
|
return _redirect_pipe_thread(h, STD_ERROR_HANDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-06-15 17:35:24 +02:00
|
|
|
static void ReportServerStartupFailure(pid_t pid) {
|
|
|
|
fprintf(stderr, "ADB server didn't ACK\n");
|
|
|
|
fprintf(stderr, "Full server startup log: %s\n", GetLogFilePath().c_str());
|
|
|
|
fprintf(stderr, "Server had pid: %d\n", pid);
|
|
|
|
|
2018-12-13 01:12:28 +01:00
|
|
|
android::base::unique_fd fd(unix_open(GetLogFilePath(), O_RDONLY));
|
2017-06-15 17:35:24 +02:00
|
|
|
if (fd == -1) return;
|
|
|
|
|
|
|
|
// Let's not show more than 128KiB of log...
|
2018-09-03 04:19:39 +02:00
|
|
|
unix_lseek(fd, -128 * 1024, SEEK_END);
|
2017-06-15 17:35:24 +02:00
|
|
|
std::string content;
|
|
|
|
if (!android::base::ReadFdToString(fd, &content)) return;
|
|
|
|
|
|
|
|
std::string header = android::base::StringPrintf("--- adb starting (pid %d) ---", pid);
|
|
|
|
std::vector<std::string> lines = android::base::Split(content, "\n");
|
|
|
|
int i = lines.size() - 1;
|
|
|
|
while (i >= 0 && lines[i] != header) --i;
|
|
|
|
while (static_cast<size_t>(i) < lines.size()) fprintf(stderr, "%s\n", lines[i++].c_str());
|
|
|
|
}
|
|
|
|
|
2016-08-26 01:00:22 +02:00
|
|
|
int launch_server(const std::string& socket_spec) {
|
2014-11-11 18:24:11 +01:00
|
|
|
#if defined(_WIN32)
|
2009-03-04 04:32:55 +01:00
|
|
|
/* we need to start the server in the background */
|
|
|
|
/* we create a PIPE that will be used to wait for the server's "OK" */
|
|
|
|
/* message since the pipe handles must be inheritable, we use a */
|
|
|
|
/* security attribute */
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
// Redirect stdin to Windows /dev/null. If we instead pass an original
|
|
|
|
// stdin/stdout/stderr handle and it is a console handle, when the adb
|
|
|
|
// server starts up, the C Runtime will see a console handle for a process
|
|
|
|
// that isn't connected to a console and it will configure
|
|
|
|
// stdin/stdout/stderr to be closed. At that point, freopen() could be used
|
|
|
|
// to reopen stderr/out, but it would take more massaging to fixup the file
|
|
|
|
// descriptor number that freopen() uses. It's simplest to avoid all of this
|
|
|
|
// complexity by just redirecting stdin to `nul' and then the C Runtime acts
|
|
|
|
// as expected.
|
|
|
|
unique_handle nul_read(CreateFileW(L"nul", GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL));
|
|
|
|
if (nul_read.get() == INVALID_HANDLE_VALUE) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: CreateFileW 'nul' failed: %s\n",
|
2016-01-27 17:52:53 +01:00
|
|
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
2015-05-21 08:17:26 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-22 02:23:51 +01:00
|
|
|
// Create pipes with non-inheritable read handle, inheritable write handle. We need to connect
|
|
|
|
// the subprocess to pipes instead of just letting the subprocess inherit our existing
|
|
|
|
// stdout/stderr handles because a DETACHED_PROCESS cannot write to a console that it is not
|
|
|
|
// attached to.
|
2015-08-27 03:46:09 +02:00
|
|
|
unique_handle ack_read, ack_write;
|
|
|
|
if (!_create_anonymous_pipe(&ack_read, &ack_write, &sa)) {
|
2015-05-21 08:17:26 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-27 03:46:09 +02:00
|
|
|
unique_handle stdout_read, stdout_write;
|
|
|
|
if (!_create_anonymous_pipe(&stdout_read, &stdout_write, &sa)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
unique_handle stderr_read, stderr_write;
|
|
|
|
if (!_create_anonymous_pipe(&stderr_read, &stderr_write, &sa)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-29 02:18:50 +01:00
|
|
|
/* Some programs want to launch an adb command and collect its output by
|
|
|
|
* calling CreateProcess with inheritable stdout/stderr handles, then
|
|
|
|
* using read() to get its output. When this happens, the stdout/stderr
|
|
|
|
* handles passed to the adb client process will also be inheritable.
|
|
|
|
* When starting the adb server here, care must be taken to reset them
|
|
|
|
* to non-inheritable.
|
|
|
|
* Otherwise, something bad happens: even if the adb command completes,
|
|
|
|
* the calling process is stuck while read()-ing from the stdout/stderr
|
|
|
|
* descriptors, because they're connected to corresponding handles in the
|
|
|
|
* adb server process (even if the latter never uses/writes to them).
|
2015-08-27 03:46:09 +02:00
|
|
|
* Note that even if we don't pass these handles in the STARTUPINFO struct,
|
|
|
|
* if they're marked inheritable, they're still inherited, requiring us to
|
|
|
|
* deal with this.
|
|
|
|
*
|
|
|
|
* If we're still having problems with inheriting random handles in the
|
|
|
|
* future, consider using PROC_THREAD_ATTRIBUTE_HANDLE_LIST to explicitly
|
|
|
|
* specify which handles should be inherited: http://blogs.msdn.com/b/oldnewthing/archive/2011/12/16/10248328.aspx
|
2015-09-17 01:40:06 +02:00
|
|
|
*
|
|
|
|
* Older versions of Windows return console pseudo-handles that cannot be
|
|
|
|
* made non-inheritable, so ignore those failures.
|
2012-11-29 02:18:50 +01:00
|
|
|
*/
|
2015-09-17 01:40:06 +02:00
|
|
|
_try_make_handle_noninheritable(GetStdHandle(STD_INPUT_HANDLE));
|
|
|
|
_try_make_handle_noninheritable(GetStdHandle(STD_OUTPUT_HANDLE));
|
|
|
|
_try_make_handle_noninheritable(GetStdHandle(STD_ERROR_HANDLE));
|
2012-11-29 02:18:50 +01:00
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
STARTUPINFOW startup;
|
2009-03-04 04:32:55 +01:00
|
|
|
ZeroMemory( &startup, sizeof(startup) );
|
|
|
|
startup.cb = sizeof(startup);
|
2015-08-27 03:46:09 +02:00
|
|
|
startup.hStdInput = nul_read.get();
|
|
|
|
startup.hStdOutput = stdout_write.get();
|
|
|
|
startup.hStdError = stderr_write.get();
|
2009-03-04 04:32:55 +01:00
|
|
|
startup.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
|
2015-08-09 00:07:07 +02:00
|
|
|
// Verify that the pipe_write handle value can be passed on the command line
|
|
|
|
// as %d and that the rest of adb code can pass it around in an int.
|
2015-08-27 03:46:09 +02:00
|
|
|
const int ack_write_as_int = cast_handle_to_int(ack_write.get());
|
|
|
|
if (cast_int_to_handle(ack_write_as_int) != ack_write.get()) {
|
2015-08-09 00:07:07 +02:00
|
|
|
// If this fires, either handle values are larger than 32-bits or else
|
|
|
|
// there is a bug in our casting.
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: cannot fit pipe handle value into 32-bits: 0x%p\n", ack_write.get());
|
2015-08-09 00:07:07 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
// get path of current program
|
|
|
|
WCHAR program_path[MAX_PATH];
|
|
|
|
const DWORD module_result = GetModuleFileNameW(NULL, program_path,
|
|
|
|
arraysize(program_path));
|
|
|
|
if ((module_result >= arraysize(program_path)) || (module_result == 0)) {
|
|
|
|
// String truncation or some other error.
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: cannot get executable path: %s\n",
|
2016-01-27 17:52:53 +01:00
|
|
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
2015-08-27 03:46:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR args[64];
|
2016-08-26 01:00:22 +02:00
|
|
|
snwprintf(args, arraysize(args), L"adb -L %s fork-server server --reply-fd %d",
|
|
|
|
socket_spec.c_str(), ack_write_as_int);
|
2015-08-27 03:46:09 +02:00
|
|
|
|
|
|
|
PROCESS_INFORMATION pinfo;
|
|
|
|
ZeroMemory(&pinfo, sizeof(pinfo));
|
|
|
|
|
|
|
|
if (!CreateProcessW(
|
2009-03-04 04:32:55 +01:00
|
|
|
program_path, /* program path */
|
2013-11-13 09:23:37 +01:00
|
|
|
args,
|
2009-03-04 04:32:55 +01:00
|
|
|
/* the fork-server argument will set the
|
|
|
|
debug = 2 in the child */
|
|
|
|
NULL, /* process handle is not inheritable */
|
|
|
|
NULL, /* thread handle is not inheritable */
|
|
|
|
TRUE, /* yes, inherit some handles */
|
|
|
|
DETACHED_PROCESS, /* the new process doesn't have a console */
|
|
|
|
NULL, /* use parent's environment block */
|
|
|
|
NULL, /* use parent's starting directory */
|
|
|
|
&startup, /* startup info, i.e. std handles */
|
2015-08-27 03:46:09 +02:00
|
|
|
&pinfo )) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: CreateProcessW failed: %s\n",
|
2016-01-27 17:52:53 +01:00
|
|
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
2015-08-27 03:46:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
unique_handle process_handle(pinfo.hProcess);
|
|
|
|
pinfo.hProcess = NULL;
|
|
|
|
|
|
|
|
// Close handles that we no longer need to complete the rest.
|
|
|
|
CloseHandle(pinfo.hThread);
|
|
|
|
pinfo.hThread = NULL;
|
|
|
|
|
|
|
|
nul_read.reset();
|
|
|
|
ack_write.reset();
|
|
|
|
stdout_write.reset();
|
|
|
|
stderr_write.reset();
|
|
|
|
|
2015-11-22 02:23:51 +01:00
|
|
|
// Start threads to read from subprocess stdout/stderr and write to ours to make subprocess
|
|
|
|
// errors easier to diagnose. Note that the threads internally create inheritable handles, but
|
|
|
|
// that is ok because we've already spawned the subprocess.
|
2015-08-27 03:46:09 +02:00
|
|
|
|
|
|
|
// In the past, reading from a pipe before the child process's C Runtime
|
|
|
|
// started up and called GetFileType() caused a hang: http://blogs.msdn.com/b/oldnewthing/archive/2011/12/02/10243553.aspx#10244216
|
|
|
|
// This is reportedly fixed in Windows Vista: https://support.microsoft.com/en-us/kb/2009703
|
|
|
|
// I was unable to reproduce the problem on Windows XP. It sounds like a
|
|
|
|
// Windows Update may have fixed this: https://www.duckware.com/tech/peeknamedpipe.html
|
|
|
|
unique_handle stdout_thread(reinterpret_cast<HANDLE>(
|
|
|
|
_beginthreadex(NULL, 0, _redirect_stdout_thread, stdout_read.get(),
|
|
|
|
0, NULL)));
|
|
|
|
if (stdout_thread.get() == nullptr) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: cannot create thread: %s\n", strerror(errno));
|
2015-08-27 03:46:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
stdout_read.release(); // Transfer ownership to new thread
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
unique_handle stderr_thread(reinterpret_cast<HANDLE>(
|
|
|
|
_beginthreadex(NULL, 0, _redirect_stderr_thread, stderr_read.get(),
|
|
|
|
0, NULL)));
|
|
|
|
if (stderr_thread.get() == nullptr) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: cannot create thread: %s\n", strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-27 03:46:09 +02:00
|
|
|
stderr_read.release(); // Transfer ownership to new thread
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
bool got_ack = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
// Wait for the "OK\n" message, for the pipe to be closed, or other error.
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-08-27 03:46:09 +02:00
|
|
|
char temp[3];
|
|
|
|
DWORD count = 0;
|
|
|
|
|
|
|
|
if (ReadFile(ack_read.get(), temp, sizeof(temp), &count, NULL)) {
|
|
|
|
const CHAR expected[] = "OK\n";
|
|
|
|
const DWORD expected_length = arraysize(expected) - 1;
|
|
|
|
if (count == expected_length &&
|
|
|
|
memcmp(temp, expected, expected_length) == 0) {
|
|
|
|
got_ack = true;
|
|
|
|
} else {
|
2018-09-03 04:19:39 +02:00
|
|
|
ReportServerStartupFailure(pinfo.dwProcessId);
|
2017-06-15 17:35:24 +02:00
|
|
|
return -1;
|
2015-08-27 03:46:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const DWORD err = GetLastError();
|
|
|
|
// If the ACK was not written and the process exited, GetLastError()
|
|
|
|
// is probably ERROR_BROKEN_PIPE, in which case that info is not
|
|
|
|
// useful to the user.
|
|
|
|
fprintf(stderr, "could not read ok from ADB Server%s\n",
|
|
|
|
err == ERROR_BROKEN_PIPE ? "" :
|
|
|
|
android::base::StringPrintf(": %s",
|
2016-01-27 17:52:53 +01:00
|
|
|
android::base::SystemErrorCodeToString(err).c_str()).c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-08-27 03:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always try to wait a bit for threads reading stdout/stderr to finish.
|
|
|
|
// If the process started ok, it should close the pipes causing the threads
|
|
|
|
// to finish. If the process had an error, it should exit, also causing
|
|
|
|
// the pipes to be closed. In that case we want to read all of the output
|
|
|
|
// and write it out so that the user can diagnose failures.
|
|
|
|
const DWORD thread_timeout_ms = 15 * 1000;
|
|
|
|
const HANDLE threads[] = { stdout_thread.get(), stderr_thread.get() };
|
|
|
|
const DWORD wait_result = WaitForMultipleObjects(arraysize(threads),
|
|
|
|
threads, TRUE, thread_timeout_ms);
|
|
|
|
if (wait_result == WAIT_TIMEOUT) {
|
|
|
|
// Threads did not finish after waiting a little while. Perhaps the
|
|
|
|
// server didn't close pipes, or it is hung.
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: timed out waiting for threads to finish reading from ADB server\n");
|
2015-08-27 03:46:09 +02:00
|
|
|
// Process handles are signaled when the process exits, so if we wait
|
|
|
|
// on the handle for 0 seconds and it returns 'timeout', that means that
|
|
|
|
// the process is still running.
|
|
|
|
if (WaitForSingleObject(process_handle.get(), 0) == WAIT_TIMEOUT) {
|
|
|
|
// We could TerminateProcess(), but that seems somewhat presumptive.
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: server is running with process id %lu\n", pinfo.dwProcessId);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-08-27 03:46:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait_result != WAIT_OBJECT_0) {
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: unexpected result waiting for threads: %lu: %s\n", wait_result,
|
|
|
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
2015-08-27 03:46:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now ignore the thread exit codes and assume they worked properly.
|
|
|
|
|
|
|
|
if (!got_ack) {
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-11-11 18:24:11 +01:00
|
|
|
#else /* !defined(_WIN32) */
|
2009-03-04 04:32:55 +01:00
|
|
|
// set up a pipe so the child can tell us when it is ready.
|
2018-05-11 21:55:56 +02:00
|
|
|
unique_fd pipe_read, pipe_write;
|
|
|
|
if (!Pipe(&pipe_read, &pipe_write)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-09-01 00:07:18 +02:00
|
|
|
|
|
|
|
std::string path = android::base::GetExecutablePath();
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
pid_t pid = fork();
|
2016-09-01 00:07:18 +02:00
|
|
|
if (pid < 0) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
// child side of the fork
|
2018-05-11 21:55:56 +02:00
|
|
|
pipe_read.reset();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-08-07 03:38:47 +02:00
|
|
|
// android::base::Pipe unconditionally opens the pipe with O_CLOEXEC.
|
|
|
|
// Undo this manually.
|
|
|
|
fcntl(pipe_write.get(), F_SETFD, 0);
|
|
|
|
|
2015-08-07 19:10:29 +02:00
|
|
|
char reply_fd[30];
|
2018-05-11 21:55:56 +02:00
|
|
|
snprintf(reply_fd, sizeof(reply_fd), "%d", pipe_write.get());
|
2009-03-04 04:32:55 +01:00
|
|
|
// child process
|
2016-08-26 01:00:22 +02:00
|
|
|
int result = execl(path.c_str(), "adb", "-L", socket_spec.c_str(), "fork-server", "server",
|
|
|
|
"--reply-fd", reply_fd, NULL);
|
2009-03-04 04:32:55 +01:00
|
|
|
// this should not return
|
2017-04-18 23:34:16 +02:00
|
|
|
fprintf(stderr, "adb: execl returned %d: %s\n", result, strerror(errno));
|
2017-06-15 17:35:24 +02:00
|
|
|
} else {
|
2009-03-04 04:32:55 +01:00
|
|
|
// parent side of the fork
|
2017-06-15 17:35:24 +02:00
|
|
|
char temp[3] = {};
|
2009-03-04 04:32:55 +01:00
|
|
|
// wait for the "OK\n" message
|
2018-05-11 21:55:56 +02:00
|
|
|
pipe_write.reset();
|
|
|
|
int ret = adb_read(pipe_read.get(), temp, 3);
|
2011-03-16 23:57:42 +01:00
|
|
|
int saved_errno = errno;
|
2018-05-11 21:55:56 +02:00
|
|
|
pipe_read.reset();
|
2009-03-04 04:32:55 +01:00
|
|
|
if (ret < 0) {
|
2011-03-16 23:57:42 +01:00
|
|
|
fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
|
2017-06-15 17:35:24 +02:00
|
|
|
ReportServerStartupFailure(pid);
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 18:24:11 +01:00
|
|
|
#endif /* !defined(_WIN32) */
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-11 18:24:11 +01:00
|
|
|
#endif /* ADB_HOST */
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-07-31 03:49:03 +02:00
|
|
|
bool handle_forward_request(const char* service, atransport* transport, int reply_fd) {
|
|
|
|
return handle_forward_request(service, [transport](std::string*) { return transport; },
|
|
|
|
reply_fd);
|
|
|
|
}
|
|
|
|
|
2013-03-21 21:07:42 +01:00
|
|
|
// Try to handle a network forwarding request.
|
2018-07-31 03:49:03 +02:00
|
|
|
bool handle_forward_request(const char* service,
|
|
|
|
std::function<atransport*(std::string* error)> transport_acquirer,
|
|
|
|
int reply_fd) {
|
|
|
|
if (!strcmp(service, "list-forward")) {
|
|
|
|
// Create the list of forward redirections.
|
|
|
|
std::string listeners = format_listeners();
|
|
|
|
#if ADB_HOST
|
|
|
|
SendOkay(reply_fd);
|
|
|
|
#endif
|
|
|
|
SendProtocolString(reply_fd, listeners);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(service, "killforward-all")) {
|
|
|
|
remove_all_listeners();
|
|
|
|
#if ADB_HOST
|
|
|
|
/* On the host: 1st OKAY is connect, 2nd OKAY is status */
|
|
|
|
SendOkay(reply_fd);
|
|
|
|
#endif
|
|
|
|
SendOkay(reply_fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
if (!strncmp(service, "forward:", 8) || !strncmp(service, "killforward:", 12)) {
|
|
|
|
// killforward:local
|
|
|
|
// forward:(norebind:)?local;remote
|
2018-07-31 03:49:03 +02:00
|
|
|
std::string error;
|
|
|
|
atransport* transport = transport_acquirer(&error);
|
|
|
|
if (!transport) {
|
|
|
|
SendFail(reply_fd, error);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
bool kill_forward = false;
|
|
|
|
bool no_rebind = false;
|
|
|
|
if (android::base::StartsWith(service, "killforward:")) {
|
|
|
|
kill_forward = true;
|
|
|
|
service += 12;
|
2015-07-30 02:21:21 +02:00
|
|
|
} else {
|
|
|
|
service += 8; // skip past "forward:"
|
2015-05-30 02:55:19 +02:00
|
|
|
if (android::base::StartsWith(service, "norebind:")) {
|
|
|
|
no_rebind = true;
|
|
|
|
service += 9;
|
|
|
|
}
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
std::vector<std::string> pieces = android::base::Split(service, ";");
|
2013-03-21 21:07:42 +01:00
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
if (kill_forward) {
|
|
|
|
// Check killforward: parameter format: '<local>'
|
|
|
|
if (pieces.size() != 1 || pieces[0].empty()) {
|
|
|
|
SendFail(reply_fd, android::base::StringPrintf("bad killforward: %s", service));
|
2018-07-31 03:49:03 +02:00
|
|
|
return true;
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-30 02:55:19 +02:00
|
|
|
// Check forward: parameter format: '<local>;<remote>'
|
|
|
|
if (pieces.size() != 2 || pieces[0].empty() || pieces[1].empty() || pieces[1][0] == '*') {
|
|
|
|
SendFail(reply_fd, android::base::StringPrintf("bad forward: %s", service));
|
2018-07-31 03:49:03 +02:00
|
|
|
return true;
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
InstallStatus r;
|
2016-04-07 20:25:48 +02:00
|
|
|
int resolved_tcp_port = 0;
|
2015-05-30 02:55:19 +02:00
|
|
|
if (kill_forward) {
|
|
|
|
r = remove_listener(pieces[0].c_str(), transport);
|
2013-03-21 21:07:42 +01:00
|
|
|
} else {
|
2019-11-14 02:51:19 +01:00
|
|
|
int flags = 0;
|
|
|
|
if (no_rebind) {
|
|
|
|
flags |= INSTALL_LISTENER_NO_REBIND;
|
|
|
|
}
|
|
|
|
r = install_listener(pieces[0], pieces[1].c_str(), transport, flags, &resolved_tcp_port,
|
|
|
|
&error);
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
2015-04-20 17:09:20 +02:00
|
|
|
if (r == INSTALL_STATUS_OK) {
|
2013-03-21 21:07:42 +01:00
|
|
|
#if ADB_HOST
|
2016-04-07 20:25:48 +02:00
|
|
|
// On the host: 1st OKAY is connect, 2nd OKAY is status.
|
2015-05-01 02:32:03 +02:00
|
|
|
SendOkay(reply_fd);
|
2013-03-21 21:07:42 +01:00
|
|
|
#endif
|
2015-05-01 02:32:03 +02:00
|
|
|
SendOkay(reply_fd);
|
2016-04-07 20:25:48 +02:00
|
|
|
|
|
|
|
// If a TCP port was resolved, send the actual port number back.
|
|
|
|
if (resolved_tcp_port != 0) {
|
|
|
|
SendProtocolString(reply_fd, android::base::StringPrintf("%d", resolved_tcp_port));
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:49:03 +02:00
|
|
|
return true;
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
|
|
|
|
2015-04-20 17:09:20 +02:00
|
|
|
std::string message;
|
|
|
|
switch (r) {
|
2015-05-30 02:55:19 +02:00
|
|
|
case INSTALL_STATUS_OK: message = "success (!)"; break;
|
2015-04-20 17:09:20 +02:00
|
|
|
case INSTALL_STATUS_INTERNAL_ERROR: message = "internal error"; break;
|
|
|
|
case INSTALL_STATUS_CANNOT_BIND:
|
2015-08-12 01:45:32 +02:00
|
|
|
message = android::base::StringPrintf("cannot bind listener: %s",
|
2015-07-31 08:07:55 +02:00
|
|
|
error.c_str());
|
2015-04-20 17:09:20 +02:00
|
|
|
break;
|
|
|
|
case INSTALL_STATUS_CANNOT_REBIND:
|
2015-07-30 02:21:21 +02:00
|
|
|
message = android::base::StringPrintf("cannot rebind existing socket");
|
2015-04-20 17:09:20 +02:00
|
|
|
break;
|
2015-05-30 02:55:19 +02:00
|
|
|
case INSTALL_STATUS_LISTENER_NOT_FOUND:
|
|
|
|
message = android::base::StringPrintf("listener '%s' not found", service);
|
|
|
|
break;
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(reply_fd, message);
|
2018-07-31 03:49:03 +02:00
|
|
|
return true;
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
2018-07-31 03:49:03 +02:00
|
|
|
|
|
|
|
return false;
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
|
|
|
|
2015-06-23 22:00:32 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
static int SendOkay(int fd, const std::string& s) {
|
|
|
|
SendOkay(fd);
|
|
|
|
SendProtocolString(fd, s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:56 +02:00
|
|
|
static bool g_reject_kill_server = false;
|
|
|
|
void adb_set_reject_kill_server(bool value) {
|
|
|
|
g_reject_kill_server = value;
|
|
|
|
}
|
|
|
|
|
2019-02-21 05:37:26 +01:00
|
|
|
HostRequestResult handle_host_request(std::string_view service, TransportType type,
|
|
|
|
const char* serial, TransportId transport_id, int reply_fd,
|
|
|
|
asocket* s) {
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "kill") {
|
2020-04-06 21:07:56 +02:00
|
|
|
if (g_reject_kill_server) {
|
|
|
|
LOG(WARNING) << "adb server ignoring kill-server";
|
|
|
|
SendFail(reply_fd, "kill-server rejected by remote server");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "adb server killed by remote request\n");
|
|
|
|
SendOkay(reply_fd);
|
2015-08-12 02:05:02 +02:00
|
|
|
|
2020-04-06 21:07:56 +02:00
|
|
|
// Rely on process exit to close the socket for us.
|
|
|
|
exit(0);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 05:37:26 +01:00
|
|
|
LOG(DEBUG) << "handle_host_request(" << service << ")";
|
|
|
|
|
|
|
|
// Transport selection:
|
|
|
|
if (service.starts_with("transport") || service.starts_with("tport:")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
TransportType type = kTransportAny;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-02-21 04:30:59 +01:00
|
|
|
std::string serial_storage;
|
2019-02-21 05:37:26 +01:00
|
|
|
bool legacy = true;
|
|
|
|
|
|
|
|
// New transport selection protocol:
|
|
|
|
// This is essentially identical to the previous version, except it returns the selected
|
|
|
|
// transport id to the caller as well.
|
2019-05-03 18:02:45 +02:00
|
|
|
if (android::base::ConsumePrefix(&service, "tport:")) {
|
2019-02-21 05:37:26 +01:00
|
|
|
legacy = false;
|
2019-05-03 18:02:45 +02:00
|
|
|
if (android::base::ConsumePrefix(&service, "serial:")) {
|
2019-02-21 05:37:26 +01:00
|
|
|
serial_storage = service;
|
|
|
|
serial = serial_storage.c_str();
|
|
|
|
} else if (service == "usb") {
|
|
|
|
type = kTransportUsb;
|
|
|
|
} else if (service == "local") {
|
|
|
|
type = kTransportLocal;
|
|
|
|
} else if (service == "any") {
|
|
|
|
type = kTransportAny;
|
|
|
|
}
|
2019-02-21 04:30:59 +01:00
|
|
|
|
2019-02-21 05:37:26 +01:00
|
|
|
// Selection by id is unimplemented, since you obviously already know the transport id
|
|
|
|
// you're connecting to.
|
|
|
|
} else {
|
2019-05-03 18:02:45 +02:00
|
|
|
if (android::base::ConsumePrefix(&service, "transport-id:")) {
|
2019-02-21 05:37:26 +01:00
|
|
|
if (!ParseUint(&transport_id, service)) {
|
|
|
|
SendFail(reply_fd, "invalid transport id");
|
|
|
|
return HostRequestResult::Handled;
|
|
|
|
}
|
|
|
|
} else if (service == "transport-usb") {
|
|
|
|
type = kTransportUsb;
|
|
|
|
} else if (service == "transport-local") {
|
|
|
|
type = kTransportLocal;
|
|
|
|
} else if (service == "transport-any") {
|
|
|
|
type = kTransportAny;
|
2019-05-03 18:02:45 +02:00
|
|
|
} else if (android::base::ConsumePrefix(&service, "transport:")) {
|
2019-02-21 05:37:26 +01:00
|
|
|
serial_storage = service;
|
|
|
|
serial = serial_storage.c_str();
|
2017-08-17 01:57:01 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:55:10 +02:00
|
|
|
std::string error;
|
2017-08-17 01:57:01 +02:00
|
|
|
atransport* t = acquire_one_transport(type, serial, transport_id, nullptr, &error);
|
2015-06-23 22:00:32 +02:00
|
|
|
if (t != nullptr) {
|
|
|
|
s->transport = t;
|
2015-05-01 02:32:03 +02:00
|
|
|
SendOkay(reply_fd);
|
2018-08-07 23:14:21 +02:00
|
|
|
|
2019-02-21 05:37:26 +01:00
|
|
|
if (!legacy) {
|
|
|
|
// Nothing we can do if this fails.
|
|
|
|
WriteFdExactly(reply_fd, &t->id, sizeof(t->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return HostRequestResult::SwitchedTransport;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-10-07 23:55:10 +02:00
|
|
|
SendFail(reply_fd, error);
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a list of all connected devices
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "devices" || service == "devices-l") {
|
|
|
|
bool long_listing = service == "devices-l";
|
|
|
|
D("Getting device list...");
|
|
|
|
std::string device_list = list_transports(long_listing);
|
|
|
|
D("Sending device list...");
|
|
|
|
SendOkay(reply_fd, device_list);
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "reconnect-offline") {
|
2016-10-27 23:01:08 +02:00
|
|
|
std::string response;
|
|
|
|
close_usb_devices([&response](const atransport* transport) {
|
2018-05-05 01:04:49 +02:00
|
|
|
if (!ConnectionStateIsOnline(transport->GetConnectionState())) {
|
|
|
|
response += "reconnecting " + transport->serial_name() + "\n";
|
|
|
|
return true;
|
2016-10-27 23:01:08 +02:00
|
|
|
}
|
2018-05-05 01:04:49 +02:00
|
|
|
return false;
|
2019-03-28 23:47:44 +01:00
|
|
|
}, true);
|
2016-10-27 23:01:08 +02:00
|
|
|
if (!response.empty()) {
|
|
|
|
response.resize(response.size() - 1);
|
|
|
|
}
|
|
|
|
SendOkay(reply_fd, response);
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2016-10-27 23:01:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "features") {
|
2015-10-07 23:55:10 +02:00
|
|
|
std::string error;
|
2019-09-12 18:12:26 +02:00
|
|
|
atransport* t =
|
|
|
|
s->transport ? s->transport
|
|
|
|
: acquire_one_transport(type, serial, transport_id, nullptr, &error);
|
2015-09-05 01:40:30 +02:00
|
|
|
if (t != nullptr) {
|
2015-09-22 19:43:08 +02:00
|
|
|
SendOkay(reply_fd, FeatureSetToString(t->features()));
|
2015-09-05 01:40:30 +02:00
|
|
|
} else {
|
2015-10-07 23:55:10 +02:00
|
|
|
SendFail(reply_fd, error);
|
2015-09-05 01:40:30 +02:00
|
|
|
}
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2015-09-05 01:40:30 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "host-features") {
|
2017-02-23 02:07:01 +01:00
|
|
|
FeatureSet features = supported_features();
|
|
|
|
// Abuse features to report libusb status.
|
|
|
|
if (should_use_libusb()) {
|
|
|
|
features.insert(kFeatureLibusb);
|
|
|
|
}
|
2017-05-23 23:30:00 +02:00
|
|
|
features.insert(kFeaturePushSync);
|
2017-02-23 02:07:01 +01:00
|
|
|
SendOkay(reply_fd, FeatureSetToString(features));
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2017-02-23 02:07:01 +01:00
|
|
|
}
|
|
|
|
|
2009-10-12 05:04:18 +02:00
|
|
|
// remove TCP transport
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service.starts_with("disconnect:")) {
|
|
|
|
std::string address(service.substr(11));
|
2015-07-18 21:21:30 +02:00
|
|
|
if (address.empty()) {
|
2015-08-27 21:03:11 +02:00
|
|
|
kick_all_tcp_devices();
|
2018-08-07 23:14:21 +02:00
|
|
|
SendOkay(reply_fd, "disconnected everything");
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2009-10-12 05:04:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 21:21:30 +02:00
|
|
|
std::string serial;
|
|
|
|
std::string host;
|
|
|
|
int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
|
|
|
|
std::string error;
|
2019-07-19 05:44:39 +02:00
|
|
|
if (address.starts_with("vsock:") || address.starts_with("localfilesystem:")) {
|
2019-01-05 03:51:11 +01:00
|
|
|
serial = address;
|
|
|
|
} else if (!android::base::ParseNetAddress(address, &host, &port, &serial, &error)) {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendFail(reply_fd, android::base::StringPrintf("couldn't parse '%s': %s",
|
|
|
|
address.c_str(), error.c_str()));
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2015-07-18 21:21:30 +02:00
|
|
|
}
|
|
|
|
atransport* t = find_transport(serial.c_str());
|
|
|
|
if (t == nullptr) {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendFail(reply_fd, android::base::StringPrintf("no such device '%s'", serial.c_str()));
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2015-07-18 21:21:30 +02:00
|
|
|
}
|
2015-08-27 21:03:11 +02:00
|
|
|
kick_transport(t);
|
2018-08-07 23:14:21 +02:00
|
|
|
SendOkay(reply_fd, android::base::StringPrintf("disconnected %s", address.c_str()));
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2009-08-25 00:58:40 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:55:10 +02:00
|
|
|
// Returns our value for ADB_SERVER_VERSION.
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "version") {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendOkay(reply_fd, android::base::StringPrintf("%04x", ADB_SERVER_VERSION));
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-06-23 22:00:32 +02:00
|
|
|
// These always report "unknown" rather than the actual error, for scripts.
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "get-serialno") {
|
2015-10-07 23:55:10 +02:00
|
|
|
std::string error;
|
2019-09-12 18:12:26 +02:00
|
|
|
atransport* t =
|
|
|
|
s->transport ? s->transport
|
|
|
|
: acquire_one_transport(type, serial, transport_id, nullptr, &error);
|
2015-10-07 23:55:10 +02:00
|
|
|
if (t) {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendOkay(reply_fd, !t->serial.empty() ? t->serial : "unknown");
|
2015-10-07 23:55:10 +02:00
|
|
|
} else {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendFail(reply_fd, error);
|
2015-10-07 23:55:10 +02:00
|
|
|
}
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "get-devpath") {
|
2015-10-07 23:55:10 +02:00
|
|
|
std::string error;
|
2019-09-12 18:12:26 +02:00
|
|
|
atransport* t =
|
|
|
|
s->transport ? s->transport
|
|
|
|
: acquire_one_transport(type, serial, transport_id, nullptr, &error);
|
2015-10-07 23:55:10 +02:00
|
|
|
if (t) {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendOkay(reply_fd, !t->devpath.empty() ? t->devpath : "unknown");
|
2015-10-07 23:55:10 +02:00
|
|
|
} else {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendFail(reply_fd, error);
|
2015-10-07 23:55:10 +02:00
|
|
|
}
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2012-04-20 20:21:14 +02:00
|
|
|
}
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "get-state") {
|
2015-10-07 23:55:10 +02:00
|
|
|
std::string error;
|
2019-09-12 18:12:26 +02:00
|
|
|
atransport* t =
|
|
|
|
s->transport ? s->transport
|
|
|
|
: acquire_one_transport(type, serial, transport_id, nullptr, &error);
|
2015-10-07 23:55:10 +02:00
|
|
|
if (t) {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendOkay(reply_fd, t->connection_state_name());
|
2015-10-07 23:55:10 +02:00
|
|
|
} else {
|
2018-08-07 23:14:21 +02:00
|
|
|
SendFail(reply_fd, error);
|
2015-10-07 23:55:10 +02:00
|
|
|
}
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2015-06-23 22:00:32 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:55:10 +02:00
|
|
|
// Indicates a new emulator instance has started.
|
2019-05-03 18:02:45 +02:00
|
|
|
if (android::base::ConsumePrefix(&service, "emulator:")) {
|
2019-02-21 04:30:59 +01:00
|
|
|
unsigned int port;
|
|
|
|
if (!ParseUint(&port, service)) {
|
|
|
|
LOG(ERROR) << "received invalid port for emulator: " << service;
|
|
|
|
} else {
|
|
|
|
local_connect(port);
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* we don't even need to send a reply */
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-04-05 22:50:44 +02:00
|
|
|
|
2019-02-21 04:30:59 +01:00
|
|
|
if (service == "reconnect") {
|
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 01:01:01 +01:00
|
|
|
std::string response;
|
2019-09-12 18:12:26 +02:00
|
|
|
atransport* t = s->transport ? s->transport
|
|
|
|
: acquire_one_transport(type, serial, transport_id, nullptr,
|
|
|
|
&response, true);
|
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 01:01:01 +01:00
|
|
|
if (t != nullptr) {
|
2019-03-28 23:47:44 +01:00
|
|
|
kick_transport(t, true);
|
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 01:01:01 +01:00
|
|
|
response =
|
2019-02-21 04:30:59 +01:00
|
|
|
"reconnecting " + t->serial_name() + " [" + t->connection_state_name() + "]\n";
|
2016-04-05 22:50:44 +02:00
|
|
|
}
|
2018-08-07 23:14:21 +02:00
|
|
|
SendOkay(reply_fd, response);
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2016-04-05 22:50:44 +02:00
|
|
|
}
|
2014-07-15 02:23:06 +02:00
|
|
|
|
2019-02-21 04:30:59 +01:00
|
|
|
// TODO: Switch handle_forward_request to string_view.
|
|
|
|
std::string service_str(service);
|
2019-09-12 18:12:26 +02:00
|
|
|
auto transport_acquirer = [=](std::string* error) {
|
|
|
|
if (s->transport) {
|
|
|
|
return s->transport;
|
|
|
|
} else {
|
|
|
|
std::string error;
|
|
|
|
return acquire_one_transport(type, serial, transport_id, nullptr, &error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (handle_forward_request(service_str.c_str(), transport_acquirer, reply_fd)) {
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Handled;
|
2018-04-13 21:17:03 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 05:37:26 +01:00
|
|
|
return HostRequestResult::Unhandled;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-05-04 07:37:10 +02:00
|
|
|
|
|
|
|
static auto& init_mutex = *new std::mutex();
|
|
|
|
static auto& init_cv = *new std::condition_variable();
|
|
|
|
static bool device_scan_complete = false;
|
|
|
|
static bool transports_ready = false;
|
|
|
|
|
|
|
|
void update_transport_status() {
|
|
|
|
bool result = iterate_transports([](const atransport* t) {
|
|
|
|
if (t->type == kTransportUsb && t->online != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
bool ready;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(init_mutex);
|
|
|
|
transports_ready = result;
|
|
|
|
ready = transports_ready && device_scan_complete;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ready) {
|
|
|
|
init_cv.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_notify_device_scan_complete() {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(init_mutex);
|
2017-05-31 20:54:56 +02:00
|
|
|
if (device_scan_complete) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-04 07:37:10 +02:00
|
|
|
device_scan_complete = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_transport_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_wait_for_device_initialization() {
|
|
|
|
std::unique_lock<std::mutex> lock(init_mutex);
|
|
|
|
init_cv.wait_for(lock, 3s, []() { return device_scan_complete && transports_ready; });
|
|
|
|
}
|
|
|
|
|
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 01:01:01 +01:00
|
|
|
#endif // ADB_HOST
|