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

Commit 8b0a69f9 authored by Josh Gao's avatar Josh Gao Committed by Automerger Merge Worker
Browse files

Merge changes I5fd3b25d,I6c039b32,I014d7ad2 am: a675727a

Change-Id: I210f5541de0ec1805d2b30781886ab55a84f2f96
parents b2beddd3 a675727a
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -171,7 +171,6 @@ libadb_srcs = [
    "sysdeps/errno.cpp",
    "sysdeps/errno.cpp",
    "transport.cpp",
    "transport.cpp",
    "transport_fd.cpp",
    "transport_fd.cpp",
    "transport_local.cpp",
    "types.cpp",
    "types.cpp",
]
]


@@ -217,6 +216,7 @@ cc_library_host_static {
        "client/adb_wifi.cpp",
        "client/adb_wifi.cpp",
        "client/usb_libusb.cpp",
        "client/usb_libusb.cpp",
        "client/usb_dispatch.cpp",
        "client/usb_dispatch.cpp",
        "client/transport_local.cpp",
        "client/transport_mdns.cpp",
        "client/transport_mdns.cpp",
        "client/transport_usb.cpp",
        "client/transport_usb.cpp",
        "client/pairing/pairing_client.cpp",
        "client/pairing/pairing_client.cpp",
@@ -390,10 +390,11 @@ cc_library_static {
    compile_multilib: "both",
    compile_multilib: "both",


    srcs: libadb_srcs + libadb_linux_srcs + libadb_posix_srcs + [
    srcs: libadb_srcs + libadb_linux_srcs + libadb_posix_srcs + [
        "daemon/adb_wifi.cpp",
        "daemon/auth.cpp",
        "daemon/auth.cpp",
        "daemon/jdwp_service.cpp",
        "daemon/jdwp_service.cpp",
        "daemon/logging.cpp",
        "daemon/logging.cpp",
        "daemon/adb_wifi.cpp",
        "daemon/transport_local.cpp",
    ],
    ],


    generated_headers: ["platform_tools_version"],
    generated_headers: ["platform_tools_version"],
@@ -592,7 +593,6 @@ cc_library {
cc_binary {
cc_binary {
    name: "adbd",
    name: "adbd",
    defaults: ["adbd_defaults", "host_adbd_supported", "libadbd_binary_dependencies"],
    defaults: ["adbd_defaults", "host_adbd_supported", "libadbd_binary_dependencies"],
    stl: "libc++_static",
    recovery_available: true,
    recovery_available: true,
    apex_available: ["com.android.adbd"],
    apex_available: ["com.android.adbd"],


+8 −87
Original line number Original line Diff line number Diff line
@@ -38,10 +38,6 @@
#include <android-base/thread_annotations.h>
#include <android-base/thread_annotations.h>
#include <cutils/sockets.h>
#include <cutils/sockets.h>


#if !ADB_HOST
#include <android-base/properties.h>
#endif

#include "adb.h"
#include "adb.h"
#include "adb_io.h"
#include "adb_io.h"
#include "adb_unique_fd.h"
#include "adb_unique_fd.h"
@@ -49,8 +45,6 @@
#include "socket_spec.h"
#include "socket_spec.h"
#include "sysdeps/chrono.h"
#include "sysdeps/chrono.h"


#if ADB_HOST

// Android Wear has been using port 5601 in all of its documentation/tooling,
// Android Wear has been using port 5601 in all of its documentation/tooling,
// but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX].
// but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX].
// Avoid stomping on their port by restricting the active scanning range.
// Avoid stomping on their port by restricting the active scanning range.
@@ -76,9 +70,8 @@ static void adb_local_transport_max_port_env_override() {


// We keep a map from emulator port to transport.
// We keep a map from emulator port to transport.
// TODO: weak_ptr?
// TODO: weak_ptr?
static auto& local_transports GUARDED_BY(local_transports_lock) =
static std::unordered_map<int, atransport*> local_transports
    *new std::unordered_map<int, atransport*>();
        [[clang::no_destroy]] GUARDED_BY(local_transports_lock);
#endif /* ADB_HOST */


bool local_connect(int port) {
bool local_connect(int port) {
    std::string dummy;
    std::string dummy;
@@ -140,11 +133,9 @@ void connect_device(const std::string& address, std::string* response) {
    }
    }
}
}



int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
    unique_fd fd;
    unique_fd fd;


#if ADB_HOST
    if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
    if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
        find_emulator_transport_by_console_port(console_port) != nullptr) {
        find_emulator_transport_by_console_port(console_port) != nullptr) {
        return -1;
        return -1;
@@ -154,7 +145,7 @@ int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* e
    if (host) {
    if (host) {
        fd.reset(network_connect(host, adb_port, SOCK_STREAM, 0, error));
        fd.reset(network_connect(host, adb_port, SOCK_STREAM, 0, error));
    }
    }
#endif

    if (fd < 0) {
    if (fd < 0) {
        fd.reset(network_loopback_client(adb_port, SOCK_STREAM, error));
        fd.reset(network_loopback_client(adb_port, SOCK_STREAM, error));
    }
    }
@@ -173,8 +164,6 @@ int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* e
    return -1;
    return -1;
}
}


#if ADB_HOST

static void PollAllLocalPortsForEmulator() {
static void PollAllLocalPortsForEmulator() {
    // Try to connect to any number of running emulator instances.
    // Try to connect to any number of running emulator instances.
    for (int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; port <= adb_local_transport_max_port;
    for (int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; port <= adb_local_transport_max_port;
@@ -240,77 +229,12 @@ static void client_socket_thread(std::string_view) {
    }
    }
}
}


#else  // !ADB_HOST

void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
                          std::string_view addr) {
    adb_thread_setname("server socket");

    unique_fd serverfd;
    std::string error;

    while (serverfd == -1) {
        errno = 0;
        serverfd = listen_func(addr, &error);
        if (errno == EAFNOSUPPORT || errno == EINVAL || errno == EPROTONOSUPPORT) {
            D("unrecoverable error: '%s'", error.c_str());
            return;
        } else if (serverfd < 0) {
            D("server: cannot bind socket yet: %s", error.c_str());
            std::this_thread::sleep_for(1s);
            continue;
        }
        close_on_exec(serverfd.get());
    }

    while (true) {
        D("server: trying to get new connection from fd %d", serverfd.get());
        unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
        if (fd >= 0) {
            D("server: new connection on fd %d", fd.get());
            close_on_exec(fd.get());
            disable_tcp_nagle(fd.get());
            std::string serial = android::base::StringPrintf("host-%d", fd.get());
            // We don't care about port value in "register_socket_transport" as it is used
            // only from ADB_HOST. "server_socket_thread" is never called from ADB_HOST.
            register_socket_transport(
                    std::move(fd), std::move(serial), 0, 1,
                    [](atransport*) { return ReconnectResult::Abort; }, false);
        }
    }
    D("transport: server_socket_thread() exiting");
}

#endif

#if !ADB_HOST
unique_fd adb_listen(std::string_view addr, std::string* error) {
    return unique_fd{socket_spec_listen(addr, error, nullptr)};
}
#endif

void local_init(const std::string& addr) {
void local_init(const std::string& addr) {
#if ADB_HOST
    D("transport: local client init");
    D("transport: local client init");
    std::thread(client_socket_thread, addr).detach();
    std::thread(client_socket_thread, addr).detach();
    adb_local_transport_max_port_env_override();
    adb_local_transport_max_port_env_override();
#elif !defined(__ANDROID__)
    // Host adbd.
    D("transport: local server init");
    std::thread(server_socket_thread, adb_listen, addr).detach();
#else
    D("transport: local server init");
    // For the adbd daemon in the system image we need to distinguish
    // between the device, and the emulator.
    if (addr.starts_with("tcp:") && use_qemu_goldfish()) {
        std::thread(qemu_socket_thread, addr).detach();
    } else {
        std::thread(server_socket_thread, adb_listen, addr).detach();
    }
#endif // !ADB_HOST
}
}


#if ADB_HOST
struct EmulatorConnection : public FdConnection {
struct EmulatorConnection : public FdConnection {
    EmulatorConnection(unique_fd fd, int local_port)
    EmulatorConnection(unique_fd fd, int local_port)
        : FdConnection(std::move(fd)), local_port_(local_port) {}
        : FdConnection(std::move(fd)), local_port_(local_port) {}
@@ -352,7 +276,6 @@ atransport* find_emulator_transport_by_adb_port(int adb_port) {
atransport* find_emulator_transport_by_console_port(int console_port) {
atransport* find_emulator_transport_by_console_port(int console_port) {
    return find_transport(getEmulatorSerialString(console_port).c_str());
    return find_transport(getEmulatorSerialString(console_port).c_str());
}
}
#endif


std::string getEmulatorSerialString(int console_port) {
std::string getEmulatorSerialString(int console_port) {
    return android::base::StringPrintf("emulator-%d", console_port);
    return android::base::StringPrintf("emulator-%d", console_port);
@@ -363,7 +286,6 @@ int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local)


    t->type = kTransportLocal;
    t->type = kTransportLocal;


#if ADB_HOST
    // Emulator connection.
    // Emulator connection.
    if (local) {
    if (local) {
        auto emulator_connection = std::make_unique<EmulatorConnection>(std::move(fd), adb_port);
        auto emulator_connection = std::make_unique<EmulatorConnection>(std::move(fd), adb_port);
@@ -380,7 +302,6 @@ int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local)


        return fail;
        return fail;
    }
    }
#endif


    // Regular tcp connection.
    // Regular tcp connection.
    auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
    auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
+11 −1
Original line number Original line Diff line number Diff line
@@ -5,8 +5,18 @@ set -euxo pipefail
OUTPUT_DIR=$(realpath $(dirname "$0"))
OUTPUT_DIR=$(realpath $(dirname "$0"))
. "$OUTPUT_DIR"/include.sh
. "$OUTPUT_DIR"/include.sh


BASE_PATH=/proc/self/cwd/system/core/adb
PATHS=""
if [[ $# == 0 ]]; then
  PATHS=$BASE_PATH
else
  for arg in "$@"; do
    PATHS="$PATHS $BASE_PATH/$arg"
  done
fi

cd $ANDROID_BUILD_TOP
cd $ANDROID_BUILD_TOP
llvm-cov show --instr-profile="$OUTPUT_DIR"/adbd.profdata \
llvm-cov show --instr-profile="$OUTPUT_DIR"/adbd.profdata \
  $ANDROID_PRODUCT_OUT/apex/com.android.adbd/bin/adbd \
  $ANDROID_PRODUCT_OUT/apex/com.android.adbd/bin/adbd \
  /proc/self/cwd/system/core/adb \
  $PATHS \
  $ADB_TEST_BINARIES
  $ADB_TEST_BINARIES
+117 −0
Original line number Original line Diff line number Diff line
/*
 * 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.
 */

#define TRACE_TAG TRANSPORT

#include "sysdeps.h"
#include "transport.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <vector>

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

#if !ADB_HOST
#include <android-base/properties.h>
#endif

#include "adb.h"
#include "adb_io.h"
#include "adb_unique_fd.h"
#include "adb_utils.h"
#include "socket_spec.h"
#include "sysdeps/chrono.h"

void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
                          std::string_view addr) {
    adb_thread_setname("server socket");

    unique_fd serverfd;
    std::string error;

    while (serverfd == -1) {
        errno = 0;
        serverfd = listen_func(addr, &error);
        if (errno == EAFNOSUPPORT || errno == EINVAL || errno == EPROTONOSUPPORT) {
            D("unrecoverable error: '%s'", error.c_str());
            return;
        } else if (serverfd < 0) {
            D("server: cannot bind socket yet: %s", error.c_str());
            std::this_thread::sleep_for(1s);
            continue;
        }
        close_on_exec(serverfd.get());
    }

    while (true) {
        D("server: trying to get new connection from fd %d", serverfd.get());
        unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
        if (fd >= 0) {
            D("server: new connection on fd %d", fd.get());
            close_on_exec(fd.get());
            disable_tcp_nagle(fd.get());
            std::string serial = android::base::StringPrintf("host-%d", fd.get());
            // We don't care about port value in "register_socket_transport" as it is used
            // only from ADB_HOST. "server_socket_thread" is never called from ADB_HOST.
            register_socket_transport(
                    std::move(fd), std::move(serial), 0, 1,
                    [](atransport*) { return ReconnectResult::Abort; }, false);
        }
    }
    D("transport: server_socket_thread() exiting");
}

unique_fd adb_listen(std::string_view addr, std::string* error) {
    return unique_fd{socket_spec_listen(addr, error, nullptr)};
}

void local_init(const std::string& addr) {
#if !defined(__ANDROID__)
    // Host adbd.
    D("transport: local server init");
    std::thread(server_socket_thread, adb_listen, addr).detach();
#else
    D("transport: local server init");
    // For the adbd daemon in the system image we need to distinguish
    // between the device, and the emulator.
    if (addr.starts_with("tcp:") && use_qemu_goldfish()) {
        std::thread(qemu_socket_thread, addr).detach();
    } else {
        std::thread(server_socket_thread, adb_listen, addr).detach();
    }
#endif  // !ADB_HOST
}

int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local) {
    t->type = kTransportLocal;
    auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
    t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)));
    return 0;
}