Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 362e696b authored by Josh Gao's avatar Josh Gao
Browse files

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
parent a8db274a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -465,6 +465,7 @@ void send_auth_response(const char* token, size_t token_size, atransport* t) {
    if (key == nullptr) {
        // No more private keys to try, send the public key.
        t->SetConnectionState(kCsUnauthorized);
        t->SetConnectionEstablished(true);
        send_auth_publickey(t);
        return;
    }
+20 −7
Original line number Diff line number Diff line
@@ -1190,14 +1190,15 @@ void close_usb_devices() {
}
#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 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc
            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 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc
            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 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc

    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
+2 −2
Original line number Diff line number Diff line
@@ -364,8 +364,8 @@ void register_usb_transport(usb_handle* h, const char* serial,
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);
+6 −4
Original line number Diff line number Diff line
@@ -125,10 +125,12 @@ void connect_device(const std::string& address, std::string* response) {
        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 @@ int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* e
        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;
        }
    }