adb: report connection status when we're unauthorized.
Previously, connecting to devices that end up as unauthorized would
wait 10 seconds before reporting failure to the user. After this
change, notification happens as soon as the adb server realizes.
Test: manual
Change-Id: If7c8d38f22da3d98b952eee6a334abc8566bb751
diff --git a/adb/client/auth.cpp b/adb/client/auth.cpp
index 5fbef09..71c19b8 100644
--- a/adb/client/auth.cpp
+++ b/adb/client/auth.cpp
@@ -465,6 +465,7 @@
if (key == nullptr) {
// No more private keys to try, send the public key.
t->SetConnectionState(kCsUnauthorized);
+ t->SetConnectionEstablished(true);
send_auth_publickey(t);
return;
}
diff --git a/adb/transport.cpp b/adb/transport.cpp
index 3c74c75..0e8db04 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -1190,14 +1190,15 @@
}
#endif // ADB_HOST
-int register_socket_transport(unique_fd s, std::string serial, int port, int local,
- atransport::ReconnectCallback reconnect) {
+bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
+ atransport::ReconnectCallback reconnect, int* error) {
atransport* t = new atransport(std::move(reconnect), kCsOffline);
D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port);
if (init_socket_transport(t, std::move(s), port, local) < 0) {
delete t;
- return -1;
+ if (error) *error = errno;
+ return false;
}
std::unique_lock<std::recursive_mutex> lock(transport_lock);
@@ -1206,7 +1207,8 @@
VLOG(TRANSPORT) << "socket transport " << transport->serial
<< " is already in pending_list and fails to register";
delete t;
- return -EALREADY;
+ if (error) *error = EALREADY;
+ return false;
}
}
@@ -1215,7 +1217,8 @@
VLOG(TRANSPORT) << "socket transport " << transport->serial
<< " is already in transport_list and fails to register";
delete t;
- return -EALREADY;
+ if (error) *error = EALREADY;
+ return false;
}
}
@@ -1229,10 +1232,20 @@
if (local == 1) {
// Do not wait for emulator transports.
- return 0;
+ return true;
}
- return waitable->WaitForConnection(std::chrono::seconds(10)) ? 0 : -1;
+ if (!waitable->WaitForConnection(std::chrono::seconds(10))) {
+ if (error) *error = ETIMEDOUT;
+ return false;
+ }
+
+ if (t->GetConnectionState() == kCsUnauthorized) {
+ if (error) *error = EPERM;
+ return false;
+ }
+
+ return true;
}
#if ADB_HOST
diff --git a/adb/transport.h b/adb/transport.h
index 4bfc2ce..f362f24 100644
--- a/adb/transport.h
+++ b/adb/transport.h
@@ -364,8 +364,8 @@
void connect_device(const std::string& address, std::string* response);
/* cause new transports to be init'd and added to the list */
-int register_socket_transport(unique_fd s, std::string serial, int port, int local,
- atransport::ReconnectCallback reconnect);
+bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
+ atransport::ReconnectCallback reconnect, int* error = nullptr);
// This should only be used for transports with connection_state == kCsNoPerm.
void unregister_usb_transport(usb_handle* usb);
diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp
index b20dee9..1431252 100644
--- a/adb/transport_local.cpp
+++ b/adb/transport_local.cpp
@@ -125,10 +125,12 @@
return init_socket_transport(t, std::move(fd), port, 0) >= 0;
};
- int ret = register_socket_transport(std::move(fd), serial, port, 0, std::move(reconnect));
- if (ret < 0) {
- if (ret == -EALREADY) {
+ int error;
+ if (!register_socket_transport(std::move(fd), serial, port, 0, std::move(reconnect), &error)) {
+ if (error == EALREADY) {
*response = android::base::StringPrintf("already connected to %s", serial.c_str());
+ } else if (error == EPERM) {
+ *response = android::base::StringPrintf("failed to authenticate to %s", serial.c_str());
} else {
*response = android::base::StringPrintf("failed to connect to %s", serial.c_str());
}
@@ -162,7 +164,7 @@
disable_tcp_nagle(fd.get());
std::string serial = getEmulatorSerialString(console_port);
if (register_socket_transport(std::move(fd), std::move(serial), adb_port, 1,
- [](atransport*) { return false; }) == 0) {
+ [](atransport*) { return false; })) {
return 0;
}
}