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

Commit 8bde1912 authored by Jason Jeremy Iman's avatar Jason Jeremy Iman
Browse files

Enable "localfilesystem" UNIX domain socket for ADB.

This patch introduce "service.adb.listen_addrs", a new
string type property, while keeping the old properties.
The new property added in this patch is used to listen
on UNIX domain socket "localfilesystem".

"service.adb.listen_addrs" can be used to listen on
multiple addresses, such as tcp:5555 and tcp:5556.
It is separated by ',' (comma) character.

In the process of introducing the new socket type, the
method tcp_connect is removed and combined into
socket_spec_connect.

Without specifying using the new property, adb will
try to listen on both tcp and vsock (following the
previous implementation).

Some examples of the new property value are:
  - "tcp:5555"
  - "vsock:5555"
  - "localfilesystem:/tmp/test"
  - "tcp:5555,vsock:5555"

Bug: 133378083
Test: On master-arc-dev:
        adb root;
        setprop service.adb.listen_addrs localfilesystem:
	    <path_to_socket>;
        adb connect localfilesystem:<path_to_socket>;
        adb -s localfilesystem:<path_to_socket> shell;
	    inside Chrome OS.
Test: On aosp_bluefin:
        setprop service.adb.listen_addr tcp:5555;
        adb connect <IP>:5555; adb shell;
Test: On aosp_bluefin:
        setprop service.adb.tcp.port 5555;
        adb connect <IP>:5555; adb shell;
Test: On aosp_bluefin:
        setprop service.adb.listen_addrs tcp:5555,tcp:6666;
        adb connect <IP>:5555; adb shell;
        adb connect <IP>:6666; adb shell;
Test: On aosp_bluefin:
        ./adb_test;
Test: On cuttlefish:
        launch_cvd;
        adb -s 127.0.0.1:6520 shell;
Test: Ran host tests:
        ./adb_test;
        ./adb_integration_test_adb;
        ./adb_integration_test_device;

Change-Id: I8289bf0ab3305cf23ce5695ffba46845d58ef6bb
parent 332360fe
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1167,7 +1167,7 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty
        std::string host;
        int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
        std::string error;
        if (address.starts_with("vsock:")) {
        if (address.starts_with("vsock:") || address.starts_with("localfilesystem:")) {
            serial = address;
        } else if (!android::base::ParseNetAddress(address, &host, &port, &serial, &error)) {
            SendFail(reply_fd, android::base::StringPrintf("couldn't parse '%s': %s",
+1 −1
Original line number Diff line number Diff line
@@ -200,7 +200,7 @@ void put_apacket(apacket* p);
#define ADB_SUBCLASS 0x42
#define ADB_PROTOCOL 0x1

void local_init(int port);
void local_init(const std::string& addr);
bool local_connect(int port);
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);

+1 −1
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply
    }

    if (!getenv("ADB_EMU") || strcmp(getenv("ADB_EMU"), "0") != 0) {
        local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
        local_init(android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
    }

    std::string error;
+48 −18
Original line number Diff line number Diff line
@@ -32,11 +32,13 @@
#include <sys/prctl.h>

#include <memory>
#include <vector>

#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>

#if defined(__ANDROID__)
#include <libminijail.h>
@@ -51,6 +53,7 @@
#include "adb_auth.h"
#include "adb_listeners.h"
#include "adb_utils.h"
#include "socket_spec.h"
#include "transport.h"

#include "mdns.h"
@@ -179,12 +182,26 @@ static void drop_privileges(int server_port) {
}
#endif

static void setup_port(int port) {
    LOG(INFO) << "adbd listening on port " << port;
    local_init(port);
static void setup_adb(const std::vector<std::string>& addrs) {
#if defined(__ANDROID__)
    // Get the first valid port from addrs and setup mDNS.
    int port = -1;
    std::string error;
    for (const auto& addr : addrs) {
        port = get_host_socket_spec_port(addr, &error);
        if (port != -1) {
            break;
        }
    }
    if (port == -1) {
        port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
    }
    setup_mdns(port);
#endif
    for (const auto& addr : addrs) {
        LOG(INFO) << "adbd listening on " << addr;
        local_init(addr);
    }
}

int adbd_main(int server_port) {
@@ -248,6 +265,9 @@ int adbd_main(int server_port) {
    // If one of these properties is set, also listen on that port.
    // If one of the properties isn't set and we couldn't listen on usb, listen
    // on the default port.
    std::vector<std::string> addrs;
    std::string prop_addr = android::base::GetProperty("service.adb.listen_addrs", "");
    if (prop_addr.empty()) {
        std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
        if (prop_port.empty()) {
            prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
@@ -261,12 +281,22 @@ int adbd_main(int server_port) {

        int port;
        if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
        D("using port=%d", port);
        // Listen on TCP port specified by service.adb.tcp.port property.
        setup_port(port);
            D("using tcp port=%d", port);
            // Listen on TCP and VSOCK port specified by service.adb.tcp.port property.
            addrs.push_back(android::base::StringPrintf("tcp:%d", port));
            addrs.push_back(android::base::StringPrintf("vsock:%d", port));
            setup_adb(addrs);
        } else if (!is_usb) {
            // Listen on default port.
        setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
            addrs.push_back(
                    android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
            addrs.push_back(
                    android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
            setup_adb(addrs);
        }
    } else {
        addrs = android::base::Split(prop_addr, ",");
        setup_adb(addrs);
    }

    D("adbd_main(): pre init_jdwp()");
+9 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#include <qemu_pipe.h>

#define TRACE_TAG TRANSPORT
#include "socket_spec.h"
#include "sysdeps.h"
#include "transport.h"

@@ -55,7 +56,7 @@
 *   the transport registration is completed. That's why we need to send the
 *   'start' request after the transport is registered.
 */
void qemu_socket_thread(int port) {
void qemu_socket_thread(std::string_view addr) {
    /* 'accept' request to the adb QEMUD service. */
    static const char _accept_req[] = "accept";
    /* 'start' request to the adb QEMUD service. */
@@ -69,6 +70,12 @@ void qemu_socket_thread(int port) {
    adb_thread_setname("qemu socket");
    D("transport: qemu_socket_thread() starting");

    std::string error;
    int port = get_host_socket_spec_port(addr, &error);
    if (port == -1) {
        port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
    }

    /* adb QEMUD service connection request. */
    snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);

@@ -78,7 +85,7 @@ void qemu_socket_thread(int port) {
        /* This could be an older version of the emulator, that doesn't
         * implement adb QEMUD service. Fall back to the old TCP way. */
        D("adb service is not available. Falling back to TCP socket.");
        std::thread(server_socket_thread, tcp_listen_inaddr_any, port).detach();
        std::thread(server_socket_thread, adb_listen, addr).detach();
        return;
    }

Loading