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

Commit 7a1106b9 authored by android-build-team Robot's avatar android-build-team Robot
Browse files

Snap for 5364434 from 8d730482 to qt-release

Change-Id: I6a3e85b9e35193338e1d289f11471f5c8fecb23c
parents 58b77fb1 8d730482
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -222,7 +222,9 @@ static void help() {
        "     all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp\n"
        " $ADB_VENDOR_KEYS         colon-separated list of keys (files or directories)\n"
        " $ANDROID_SERIAL          serial number to connect to (see -s)\n"
        " $ANDROID_LOG_TAGS        tags to be used by logcat (see logcat --help)\n");
        " $ANDROID_LOG_TAGS        tags to be used by logcat (see logcat --help)\n"
        " $ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)\n"
    );
    // clang-format on
}

+29 −35
Original line number Diff line number Diff line
@@ -27,68 +27,62 @@
#include "adb_io.h"
#include "adb_unique_fd.h"

void remount_service(unique_fd fd, const std::string& cmd) {
    static constexpr char remount_cmd[] = "/system/bin/remount";
    static constexpr char remount_failed[] = "remount failed\n";
static constexpr char kRemountCmd[] = "/system/bin/remount";

static bool do_remount(int fd, const std::string& cmd) {
    if (getuid() != 0) {
        WriteFdExactly(fd.get(), "Not running as root. Try \"adb root\" first.\n");
        WriteFdExactly(fd.get(), remount_failed);
        return;
        WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
        return false;
    }

    auto pid = vfork();
    auto pid = fork();
    if (pid < 0) {
        WriteFdFmt(fd.get(), "Failed to fork to %s: %s\n", remount_cmd, strerror(errno));
        WriteFdExactly(fd.get(), remount_failed);
        return;
        WriteFdFmt(fd, "Failed to fork to %s: %s\n", kRemountCmd, strerror(errno));
        return false;
    }

    if (pid == 0) {
        // child side of the fork
        fcntl(fd.get(), F_SETFD, 0);
        dup2(fd.get(), STDIN_FILENO);
        dup2(fd.get(), STDOUT_FILENO);
        dup2(fd.get(), STDERR_FILENO);
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);

        execl(remount_cmd, remount_cmd, cmd.empty() ? nullptr : cmd.c_str(), nullptr);
        _exit(-errno ?: 42);
        execl(kRemountCmd, kRemountCmd, cmd.empty() ? nullptr : cmd.c_str(), nullptr);
        _exit(errno);
    }

    int wstatus = 0;
    auto ret = waitpid(pid, &wstatus, 0);

    if (ret == -1) {
        WriteFdFmt(fd.get(), "Failed to wait for %s: %s\n", remount_cmd, strerror(errno));
        goto err;
    }

    if (ret != pid) {
        WriteFdFmt(fd.get(), "pid %d and waitpid return %d do not match for %s\n",
                   static_cast<int>(pid), static_cast<int>(ret), remount_cmd);
        goto err;
        WriteFdFmt(fd, "Failed to wait for %s: %s\n", kRemountCmd, strerror(errno));
        return false;
    } else if (ret != pid) {
        WriteFdFmt(fd, "pid %d and waitpid return %d do not match for %s\n",
                   static_cast<int>(pid), static_cast<int>(ret), kRemountCmd);
        return false;
    }

    if (WIFSIGNALED(wstatus)) {
        WriteFdFmt(fd.get(), "%s terminated with signal %s\n", remount_cmd,
        WriteFdFmt(fd, "%s terminated with signal %s\n", kRemountCmd,
                   strsignal(WTERMSIG(wstatus)));
        goto err;
        return false;
    }

    if (!WIFEXITED(wstatus)) {
        WriteFdFmt(fd.get(), "%s stopped with status 0x%x\n", remount_cmd, wstatus);
        goto err;
        WriteFdFmt(fd, "%s stopped with status 0x%x\n", kRemountCmd, wstatus);
        return false;
    }

    if (WEXITSTATUS(wstatus)) {
        WriteFdFmt(fd.get(), "%s exited with status %d\n", remount_cmd,
                   static_cast<signed char>(WEXITSTATUS(wstatus)));
        goto err;
        WriteFdFmt(fd, "%s exited with status %d\n", kRemountCmd, WEXITSTATUS(wstatus));
        return false;
    }

    WriteFdExactly(fd.get(), "remount succeeded\n");
    return;
    return true;
}

err:
    WriteFdExactly(fd.get(), remount_failed);
void remount_service(unique_fd fd, const std::string& cmd) {
    const char* success = do_remount(fd.get(), cmd) ? "succeeded" : "failed";
    WriteFdFmt(fd.get(), "remount %s\n", success);
}
+1 −2
Original line number Diff line number Diff line
@@ -52,14 +52,13 @@ void suggest_run_adb_root(int fd) {
}

static bool make_block_device_writable(const std::string& dev) {
    int fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
    unique_fd fd(unix_open(dev, O_RDONLY | O_CLOEXEC));
    if (fd == -1) {
        return false;
    }

    int OFF = 0;
    bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
    unix_close(fd);
    return result;
}

+4 −3
Original line number Diff line number Diff line
@@ -654,9 +654,10 @@ static void usb_ffs_open_thread() {
}

void usb_init() {
    if (!android::base::GetBoolProperty("persist.adb.nonblocking_ffs", false)) {
        usb_init_legacy();
    } else {
    bool use_nonblocking = android::base::GetBoolProperty("persist.adb.nonblocking_ffs", true);
    if (use_nonblocking) {
        std::thread(usb_ffs_open_thread).detach();
    } else {
        usb_init_legacy();
    }
}
+22 −13
Original line number Diff line number Diff line
@@ -53,12 +53,27 @@

// 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].
// Avoid stomping on their port by limiting the number of emulators that can be
// connected.
#define ADB_LOCAL_TRANSPORT_MAX 16
// Avoid stomping on their port by restricting the active scanning range.
// Once emulators self-(re-)register, they'll have to avoid 5601 in their own way.
static int adb_local_transport_max_port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT + 16 * 2 - 1;

static std::mutex& local_transports_lock = *new std::mutex();

static void adb_local_transport_max_port_env_override() {
    const char* env_max_s = getenv("ADB_LOCAL_TRANSPORT_MAX_PORT");
    if (env_max_s != nullptr) {
        size_t env_max;
        if (ParseUint(&env_max, env_max_s, nullptr) && env_max < 65536) {
            // < DEFAULT_ADB_LOCAL_TRANSPORT_PORT harmlessly mimics ADB_EMU=0
            adb_local_transport_max_port = env_max;
            D("transport: ADB_LOCAL_TRANSPORT_MAX_PORT read as %d", adb_local_transport_max_port);
        } else {
            D("transport: ADB_LOCAL_TRANSPORT_MAX_PORT '%s' invalid or >= 65536, so ignored",
              env_max_s);
        }
    }
}

// We keep a map from emulator port to transport.
// TODO: weak_ptr?
static auto& local_transports GUARDED_BY(local_transports_lock) =
@@ -110,7 +125,6 @@ void connect_device(const std::string& address, std::string* response) {
            D("reconnect failed: %s", response.c_str());
            return ReconnectResult::Retry;
        }

        // This invokes the part of register_socket_transport() that needs to be
        // invoked if the atransport* has already been setup. This eventually
        // calls atransport->SetConnection() with a newly created Connection*
@@ -168,12 +182,10 @@ int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* e
#if ADB_HOST

static void PollAllLocalPortsForEmulator() {
    int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
    int count = ADB_LOCAL_TRANSPORT_MAX;

    // Try to connect to any number of running emulator instances.
    for ( ; count > 0; count--, port += 2 ) {
        local_connect(port);
    for (int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; port <= adb_local_transport_max_port;
         port += 2) {
        local_connect(port);  // Note, uses port and port-1, so '=max_port' is OK.
    }
}

@@ -289,6 +301,7 @@ void local_init(int port) {
#if ADB_HOST
    D("transport: local client init");
    std::thread(client_socket_thread, port).detach();
    adb_local_transport_max_port_env_override();
#elif !defined(__ANDROID__)
    // Host adbd.
    D("transport: local server init");
@@ -371,10 +384,6 @@ int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local)
        if (existing_transport != nullptr) {
            D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
            fail = -1;
        } else if (local_transports.size() >= ADB_LOCAL_TRANSPORT_MAX) {
            // Too many emulators.
            D("cannot register more emulators. Maximum is %d", ADB_LOCAL_TRANSPORT_MAX);
            fail = -1;
        } else {
            local_transports[adb_port] = t;
        }
Loading