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

Commit ce817c38 authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "Fix error handling/reporting for "adb forward" and "adb reverse"."

parents 5788769d 424af02f
Loading
Loading
Loading
Loading
+30 −34
Original line number Diff line number Diff line
@@ -721,56 +721,50 @@ int handle_forward_request(const char* service, TransportType type, const char*
        return 1;
    }

    if (!strncmp(service, "forward:",8) ||
        !strncmp(service, "killforward:",12)) {
        char *local, *remote;
        atransport *transport;

        int createForward = strncmp(service, "kill", 4);
        int no_rebind = 0;

        local = strchr(service, ':') + 1;

        // Handle forward:norebind:<local>... here
        if (createForward && !strncmp(local, "norebind:", 9)) {
            no_rebind = 1;
            local = strchr(local, ':') + 1;
    if (!strncmp(service, "forward:", 8) || !strncmp(service, "killforward:", 12)) {
        // killforward:local
        // forward:(norebind:)?local;remote
        bool kill_forward = false;
        bool no_rebind = false;
        if (android::base::StartsWith(service, "killforward:")) {
            kill_forward = true;
            service += 12;
            if (android::base::StartsWith(service, "norebind:")) {
                no_rebind = true;
                service += 9;
            }

        remote = strchr(local,';');

        if (createForward) {
            // Check forward: parameter format: '<local>;<remote>'
            if(remote == 0) {
                SendFail(reply_fd, "malformed forward spec");
                return 1;
        } else {
            service += 8;
        }

            *remote++ = 0;
            if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) {
                SendFail(reply_fd, "malformed forward spec");
        std::vector<std::string> pieces = android::base::Split(service, ";");

        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));
                return 1;
            }
        } else {
            // Check killforward: parameter format: '<local>'
            if (local[0] == 0) {
                SendFail(reply_fd, "malformed forward spec");
            // 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));
                return 1;
            }
        }

        std::string error_msg;
        transport = acquire_one_transport(kCsAny, type, serial, &error_msg);
        atransport* transport = acquire_one_transport(kCsAny, type, serial, &error_msg);
        if (!transport) {
            SendFail(reply_fd, error_msg);
            return 1;
        }

        InstallStatus r;
        if (createForward) {
            r = install_listener(local, remote, transport, no_rebind);
        if (kill_forward) {
            r = remove_listener(pieces[0].c_str(), transport);
        } else {
            r = remove_listener(local, transport);
            r = install_listener(pieces[0], pieces[1].c_str(), transport, no_rebind);
        }
        if (r == INSTALL_STATUS_OK) {
#if ADB_HOST
@@ -783,7 +777,7 @@ int handle_forward_request(const char* service, TransportType type, const char*

        std::string message;
        switch (r) {
          case INSTALL_STATUS_OK: message = " "; break;
          case INSTALL_STATUS_OK: message = "success (!)"; break;
          case INSTALL_STATUS_INTERNAL_ERROR: message = "internal error"; break;
          case INSTALL_STATUS_CANNOT_BIND:
            message = android::base::StringPrintf("cannot bind to socket: %s", strerror(errno));
@@ -791,7 +785,9 @@ int handle_forward_request(const char* service, TransportType type, const char*
          case INSTALL_STATUS_CANNOT_REBIND:
            message = android::base::StringPrintf("cannot rebind existing socket: %s", strerror(errno));
            break;
          case INSTALL_STATUS_LISTENER_NOT_FOUND: message = "listener not found"; break;
          case INSTALL_STATUS_LISTENER_NOT_FOUND:
            message = android::base::StringPrintf("listener '%s' not found", service);
            break;
        }
        SendFail(reply_fd, message);
        return 1;
+9 −7
Original line number Diff line number Diff line
@@ -256,19 +256,21 @@ error:
}


int adb_command(const std::string& service, std::string* error) {
    int fd = adb_connect(service, error);
bool adb_command(const std::string& service) {
    std::string error;
    int fd = adb_connect(service, &error);
    if (fd < 0) {
        fprintf(stderr, "error: %s\n", error->c_str());
        return -1;
        fprintf(stderr, "error: %s\n", error.c_str());
        return false;
    }

    if (!adb_status(fd, error)) {
    if (!adb_status(fd, &error)) {
        fprintf(stderr, "error: %s\n", error.c_str());
        adb_close(fd);
        return -1;
        return false;
    }

    return 0;
    return true;
}

bool adb_query(const std::string& service, std::string* result, std::string* error) {
+3 −3
Original line number Diff line number Diff line
@@ -26,9 +26,9 @@
int adb_connect(const std::string& service, std::string* error);
int _adb_connect(const std::string& service, std::string* error);

// Connect to adb, connect to the named service, return 0 if the connection
// succeeded AND the service returned OKAY.
int adb_command(const std::string& service, std::string* error);
// Connect to adb, connect to the named service, returns true if the connection
// succeeded AND the service returned OKAY. Outputs any returned error otherwise.
bool adb_command(const std::string& service);

// Connects to the named adb service and fills 'result' with the response.
// Returns true on success; returns false and fills 'error' on failure.
+16 −28
Original line number Diff line number Diff line
@@ -26,13 +26,12 @@

int gListenAll = 0; /* Not static because it is used in commandline.c. */

alistener listener_list = {
static alistener listener_list = {
    .next = &listener_list,
    .prev = &listener_list,
};

void ss_listener_event_func(int _fd, unsigned ev, void *_l)
{
static void ss_listener_event_func(int _fd, unsigned ev, void *_l) {
    asocket *s;

    if(ev & FDE_READ) {
@@ -56,7 +55,7 @@ void ss_listener_event_func(int _fd, unsigned ev, void *_l)
    }
}

void listener_event_func(int _fd, unsigned ev, void* _l)
static void listener_event_func(int _fd, unsigned ev, void* _l)
{
    alistener* listener = reinterpret_cast<alistener*>(_l);
    asocket *s;
@@ -106,38 +105,27 @@ static void free_listener(alistener* l)
    free(l);
}

void listener_disconnect(void* listener, atransport*  t)
{
static void listener_disconnect(void* listener, atransport* t) {
    free_listener(reinterpret_cast<alistener*>(listener));
}

int local_name_to_fd(const char *name)
{
    int port;

static int local_name_to_fd(const char* name) {
    if (!strncmp("tcp:", name, 4)) {
        int  ret;
        port = atoi(name + 4);

        int port = atoi(name + 4);
        if (gListenAll > 0) {
            ret = socket_inaddr_any_server(port, SOCK_STREAM);
            return socket_inaddr_any_server(port, SOCK_STREAM);
        } else {
            ret = socket_loopback_server(port, SOCK_STREAM);
            return socket_loopback_server(port, SOCK_STREAM);
        }

        return ret;
    }
#ifndef HAVE_WIN32_IPC  /* no Unix-domain sockets on Win32 */
    // It's non-sensical to support the "reserved" space on the adb host side
    // It's nonsensical to support the "reserved" space on the adb host side
    if (!strncmp(name, "local:", 6)) {
        return socket_local_server(name + 6,
                ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
        return socket_local_server(name + 6, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
    } else if (!strncmp(name, "localabstract:", 14)) {
        return socket_local_server(name + 14,
                ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
        return socket_local_server(name + 14, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
    } else if (!strncmp(name, "localfilesystem:", 16)) {
        return socket_local_server(name + 16,
                ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
        return socket_local_server(name + 16, ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
    }

#endif
+0 −6
Original line number Diff line number Diff line
@@ -30,12 +30,6 @@ enum InstallStatus {
  INSTALL_STATUS_LISTENER_NOT_FOUND = -4,
};

extern alistener listener_list;

void listener_disconnect(void*  _l, atransport*  t);
void listener_event_func(int _fd, unsigned ev, void *_l);
void ss_listener_event_func(int _fd, unsigned ev, void *_l);

InstallStatus install_listener(const std::string& local_name,
                               const char* connect_to,
                               atransport* transport,
Loading