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

Commit 44f6592b authored by Josh Gao's avatar Josh Gao Committed by Gerrit Code Review
Browse files

Merge changes I394f5782,I9475f5d0,I59707186

* changes:
  adb: move all cleanup to a function with defined ordering.
  adb: make `adb kill-server` wait for the server to die.
  adb: libusb: replace sleep with timed CV wait.
parents 30b7f59e 01b7bc43
Loading
Loading
Loading
Loading
+4 −7
Original line number Original line Diff line number Diff line
@@ -1054,15 +1054,12 @@ int handle_host_request(const char* service, TransportType type,
    if (strcmp(service, "kill") == 0) {
    if (strcmp(service, "kill") == 0) {
        fprintf(stderr, "adb server killed by remote request\n");
        fprintf(stderr, "adb server killed by remote request\n");
        fflush(stdout);
        fflush(stdout);
        SendOkay(reply_fd);


        // On Windows, if the process exits with open sockets that
        // Send a reply even though we don't read it anymore, so that old versions
        // shutdown(SD_SEND) has not been called on, TCP RST segments will be
        // of adb that do read it don't spew error messages.
        // sent to the peers which will cause their next recv() to error-out
        SendOkay(reply_fd);
        // with WSAECONNRESET. In the case of this code, that means the client
        // may not read the OKAY sent above.
        adb_shutdown(reply_fd);


        // Rely on process exit to close the socket for us.
        android::base::quick_exit(0);
        android::base::quick_exit(0);
    }
    }


+21 −13
Original line number Original line Diff line number Diff line
@@ -123,7 +123,7 @@ bool adb_status(int fd, std::string* error) {
    return false;
    return false;
}
}


int _adb_connect(const std::string& service, std::string* error) {
static int _adb_connect(const std::string& service, std::string* error) {
    D("_adb_connect: %s", service.c_str());
    D("_adb_connect: %s", service.c_str());
    if (service.empty() || service.size() > MAX_PAYLOAD_V1) {
    if (service.empty() || service.size() > MAX_PAYLOAD_V1) {
        *error = android::base::StringPrintf("bad service name length (%zd)",
        *error = android::base::StringPrintf("bad service name length (%zd)",
@@ -158,6 +158,25 @@ int _adb_connect(const std::string& service, std::string* error) {
    return fd;
    return fd;
}
}


bool adb_kill_server() {
    D("adb_kill_server");
    std::string reason;
    int fd = socket_spec_connect(__adb_server_socket_spec, &reason);
    if (fd < 0) {
        fprintf(stderr, "cannot connect to daemon at %s: %s\n", __adb_server_socket_spec,
                reason.c_str());
        return true;
    }

    if (!SendProtocolString(fd, "host:kill")) {
        fprintf(stderr, "error: write failure during connection: %s\n", strerror(errno));
        return false;
    }

    ReadOrderlyShutdown(fd);
    return true;
}

int adb_connect(const std::string& service, std::string* error) {
int adb_connect(const std::string& service, std::string* error) {
    // first query the adb server's version
    // first query the adb server's version
    int fd = _adb_connect("host:version", error);
    int fd = _adb_connect("host:version", error);
@@ -214,18 +233,7 @@ int adb_connect(const std::string& service, std::string* error) {
        if (version != ADB_SERVER_VERSION) {
        if (version != ADB_SERVER_VERSION) {
            fprintf(stderr, "adb server version (%d) doesn't match this client (%d); killing...\n",
            fprintf(stderr, "adb server version (%d) doesn't match this client (%d); killing...\n",
                    version, ADB_SERVER_VERSION);
                    version, ADB_SERVER_VERSION);
            fd = _adb_connect("host:kill", error);
            adb_kill_server();
            if (fd >= 0) {
                ReadOrderlyShutdown(fd);
                adb_close(fd);
            } else {
                // If we couldn't connect to the server or had some other error,
                // report it, but still try to start the server.
                fprintf(stderr, "error: %s\n", error->c_str());
            }

            /* XXX can we better detect its death? */
            std::this_thread::sleep_for(2s);
            goto start_server;
            goto start_server;
        }
        }
    }
    }
+3 −1
Original line number Original line Diff line number Diff line
@@ -26,7 +26,9 @@
// Connect to adb, connect to the named service, and return a valid fd for
// Connect to adb, connect to the named service, and return a valid fd for
// interacting with that service upon success or a negative number on failure.
// interacting with that service upon success or a negative number on failure.
int adb_connect(const std::string& service, std::string* _Nonnull error);
int adb_connect(const std::string& service, std::string* _Nonnull error);
int _adb_connect(const std::string& service, std::string* _Nonnull error);

// Kill the currently running adb server, if it exists.
bool adb_kill_server();


// Connect to adb, connect to the named service, returns true if the connection
// Connect to adb, connect to the named service, returns true if the connection
// succeeded AND the service returned OKAY. Outputs any returned error otherwise.
// succeeded AND the service returned OKAY. Outputs any returned error otherwise.
+26 −7
Original line number Original line Diff line number Diff line
@@ -19,8 +19,12 @@
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


#include <algorithm>
#include <list>

#include <android-base/stringprintf.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/strings.h>
#include <android-base/thread_annotations.h>
#include <cutils/sockets.h>
#include <cutils/sockets.h>


#include "socket_spec.h"
#include "socket_spec.h"
@@ -64,8 +68,9 @@ alistener::~alistener() {


// listener_list retains ownership of all created alistener objects. Removing an alistener from
// listener_list retains ownership of all created alistener objects. Removing an alistener from
// this list will cause it to be deleted.
// this list will cause it to be deleted.
static auto& listener_list_mutex = *new std::mutex();
typedef std::list<std::unique_ptr<alistener>> ListenerList;
typedef std::list<std::unique_ptr<alistener>> ListenerList;
static ListenerList& listener_list = *new ListenerList();
static ListenerList& listener_list GUARDED_BY(listener_list_mutex) = *new ListenerList();


static void ss_listener_event_func(int _fd, unsigned ev, void *_l) {
static void ss_listener_event_func(int _fd, unsigned ev, void *_l) {
    if (ev & FDE_READ) {
    if (ev & FDE_READ) {
@@ -108,7 +113,8 @@ static void listener_event_func(int _fd, unsigned ev, void* _l)
}
}


// Called as a transport disconnect function. |arg| is the raw alistener*.
// Called as a transport disconnect function. |arg| is the raw alistener*.
static void listener_disconnect(void* arg, atransport*) {
static void listener_disconnect(void* arg, atransport*) EXCLUDES(listener_list_mutex) {
    std::lock_guard<std::mutex> lock(listener_list_mutex);
    for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) {
    for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) {
        if (iter->get() == arg) {
        if (iter->get() == arg) {
            (*iter)->transport = nullptr;
            (*iter)->transport = nullptr;
@@ -119,7 +125,8 @@ static void listener_disconnect(void* arg, atransport*) {
}
}


// Write the list of current listeners (network redirections) into a string.
// Write the list of current listeners (network redirections) into a string.
std::string format_listeners() {
std::string format_listeners() EXCLUDES(listener_list_mutex) {
    std::lock_guard<std::mutex> lock(listener_list_mutex);
    std::string result;
    std::string result;
    for (auto& l : listener_list) {
    for (auto& l : listener_list) {
        // Ignore special listeners like those for *smartsocket*
        // Ignore special listeners like those for *smartsocket*
@@ -135,7 +142,9 @@ std::string format_listeners() {
    return result;
    return result;
}
}


InstallStatus remove_listener(const char* local_name, atransport* transport) {
InstallStatus remove_listener(const char* local_name, atransport* transport)
    EXCLUDES(listener_list_mutex) {
    std::lock_guard<std::mutex> lock(listener_list_mutex);
    for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) {
    for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) {
        if (local_name == (*iter)->local_name) {
        if (local_name == (*iter)->local_name) {
            listener_list.erase(iter);
            listener_list.erase(iter);
@@ -145,7 +154,8 @@ InstallStatus remove_listener(const char* local_name, atransport* transport) {
    return INSTALL_STATUS_LISTENER_NOT_FOUND;
    return INSTALL_STATUS_LISTENER_NOT_FOUND;
}
}


void remove_all_listeners() {
void remove_all_listeners() EXCLUDES(listener_list_mutex) {
    std::lock_guard<std::mutex> lock(listener_list_mutex);
    auto iter = listener_list.begin();
    auto iter = listener_list.begin();
    while (iter != listener_list.end()) {
    while (iter != listener_list.end()) {
        // Never remove smart sockets.
        // Never remove smart sockets.
@@ -157,9 +167,18 @@ void remove_all_listeners() {
    }
    }
}
}


void close_smartsockets() EXCLUDES(listener_list_mutex) {
    std::lock_guard<std::mutex> lock(listener_list_mutex);
    auto pred = [](const std::unique_ptr<alistener>& listener) {
        return listener->local_name == "*smartsocket*";
    };
    listener_list.erase(std::remove_if(listener_list.begin(), listener_list.end(), pred));
}

InstallStatus install_listener(const std::string& local_name, const char* connect_to,
InstallStatus install_listener(const std::string& local_name, const char* connect_to,
                               atransport* transport, int no_rebind, int* resolved_tcp_port,
                               atransport* transport, int no_rebind, int* resolved_tcp_port,
                               std::string* error) {
                               std::string* error) EXCLUDES(listener_list_mutex) {
    std::lock_guard<std::mutex> lock(listener_list_mutex);
    for (auto& l : listener_list) {
    for (auto& l : listener_list) {
        if (local_name == l->local_name) {
        if (local_name == l->local_name) {
            // Can't repurpose a smartsocket.
            // Can't repurpose a smartsocket.
+2 −0
Original line number Original line Diff line number Diff line
@@ -41,4 +41,6 @@ std::string format_listeners();
InstallStatus remove_listener(const char* local_name, atransport* transport);
InstallStatus remove_listener(const char* local_name, atransport* transport);
void remove_all_listeners(void);
void remove_all_listeners(void);


void close_smartsockets();

#endif /* __ADB_LISTENERS_H */
#endif /* __ADB_LISTENERS_H */
Loading