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

Commit 1d863ff0 authored by android-build-team Robot's avatar android-build-team Robot
Browse files

Snap for 5381581 from aca2e799 to qt-release

Change-Id: I546b79ca0e862d9ce699f54e1dd21aa488c58a68
parents 078bb0c0 aca2e799
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -78,3 +78,5 @@ $(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/ld.config.txt)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/llndk.libraries.txt)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/vndksp.libraries.txt)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/recovery/root/)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/root/sbin/charger)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/recovery/root/sbin/charger)
+4 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ cc_defaults {
        "-Wthread-safety",
        "-Wvla",
        "-DADB_HOST=1",         // overridden by adbd_defaults
        "-DALLOW_ADBD_ROOT=0",  // overridden by adbd_defaults
    ],
    cpp_std: "experimental",

@@ -79,7 +80,8 @@ cc_defaults {
    product_variables: {
        debuggable: {
            cflags: [
                "-DALLOW_ADBD_ROOT",
                "-UALLOW_ADBD_ROOT",
                "-DALLOW_ADBD_ROOT=1",
                "-DALLOW_ADBD_DISABLE_VERITY",
                "-DALLOW_ADBD_NO_AUTH",
            ],
+67 −9
Original line number Diff line number Diff line
@@ -31,10 +31,12 @@

#include <condition_variable>
#include <mutex>
#include <optional>
#include <string>
#include <thread>
#include <vector>

#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/thread_annotations.h>
@@ -214,15 +216,26 @@ int adb_connect(std::string_view service, std::string* error) {
    return adb_connect(nullptr, service, error);
}

int adb_connect(TransportId* transport, std::string_view service, std::string* error) {
    // first query the adb server's version
#if defined(__linux__)
std::optional<std::string> adb_get_server_executable_path() {
    int port;
    std::string error;
    if (!parse_tcp_socket_spec(__adb_server_socket_spec, nullptr, &port, nullptr, &error)) {
        LOG(FATAL) << "failed to parse server socket spec: " << error;
    }

    return adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adb." + std::to_string(port);
}
#endif

static bool __adb_check_server_version(std::string* error) {
    unique_fd fd(_adb_connect("host:version", nullptr, error));

    LOG(DEBUG) << "adb_connect: service: " << service;
    if (fd == -2 && !is_local_socket_spec(__adb_server_socket_spec)) {
    bool local = is_local_socket_spec(__adb_server_socket_spec);
    if (fd == -2 && !local) {
        fprintf(stderr, "* cannot start server on remote host\n");
        // error is the original network connection error
        return fd;
        return false;
    } else if (fd == -2) {
        fprintf(stderr, "* daemon not running; starting now at %s\n", __adb_server_socket_spec);
    start_server:
@@ -232,7 +245,7 @@ int adb_connect(TransportId* transport, std::string_view service, std::string* e
            // return a generic error string about the overall adb_connect()
            // that the caller requested.
            *error = "cannot connect to daemon";
            return -1;
            return false;
        } else {
            fprintf(stderr, "* daemon started successfully\n");
        }
@@ -254,18 +267,39 @@ int adb_connect(TransportId* transport, std::string_view service, std::string* e
            if (sscanf(&version_string[0], "%04x", &version) != 1) {
                *error = android::base::StringPrintf("cannot parse version string: %s",
                                                     version_string.c_str());
                return -1;
                return false;
            }
        } else {
            // If fd is -1 check for "unknown host service" which would
            // indicate a version of adb that does not support the
            // version command, in which case we should fall-through to kill it.
            if (*error != "unknown host service") {
                return fd;
                return false;
            }
        }

        if (version != ADB_SERVER_VERSION) {
#if defined(__linux__)
            if (version > ADB_SERVER_VERSION && local) {
                // Try to re-exec the existing adb server's binary.
                constexpr const char* adb_reexeced = "adb (re-execed)";
                if (strcmp(adb_reexeced, *__adb_argv) != 0) {
                    __adb_argv[0] = adb_reexeced;
                    std::optional<std::string> server_path_path = adb_get_server_executable_path();
                    std::string server_path;
                    if (server_path_path &&
                        android::base::ReadFileToString(*server_path_path, &server_path)) {
                        if (execve(server_path.c_str(), const_cast<char**>(__adb_argv),
                                   const_cast<char**>(__adb_envp)) == -1) {
                            LOG(ERROR) << "failed to exec newer version at " << server_path;
                        }

                        // Fall-through to restarting the server.
                    }
                }
            }
#endif

            fprintf(stderr, "adb server version (%d) doesn't match this client (%d); killing...\n",
                    version, ADB_SERVER_VERSION);
            adb_kill_server();
@@ -273,12 +307,36 @@ int adb_connect(TransportId* transport, std::string_view service, std::string* e
        }
    }

    return true;
}

bool adb_check_server_version(std::string* error) {
    // Only check the version once per process, since this isn't atomic anyway.
    static std::once_flag once;
    static bool result;
    static std::string* err;
    std::call_once(once, []() {
        err = new std::string();
        result = __adb_check_server_version(err);
    });
    *error = *err;
    return result;
}

int adb_connect(TransportId* transport, std::string_view service, std::string* error) {
    LOG(DEBUG) << "adb_connect: service: " << service;

    // Query the adb server's version.
    if (!adb_check_server_version(error)) {
        return -1;
    }

    // if the command is start-server, we are done.
    if (service == "host:start-server") {
        return 0;
    }

    fd.reset(_adb_connect(service, transport, error));
    unique_fd fd(_adb_connect(service, transport, error));
    if (fd == -1) {
        D("_adb_connect error: %s", error->c_str());
    } else if(fd == -2) {
+16 −0
Original line number Diff line number Diff line
@@ -20,8 +20,14 @@
#include "sysdeps.h"
#include "transport.h"

#include <optional>
#include <string>

// Explicitly check the adb server version.
// All of the commands below do this implicitly.
// Only the first invocation of this function will check the server version.
bool adb_check_server_version(std::string* _Nonnull error);

// 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.
int adb_connect(std::string_view service, std::string* _Nonnull error);
@@ -65,3 +71,13 @@ std::string format_host_command(const char* _Nonnull command);

// Get the feature set of the current preferred transport.
bool adb_get_feature_set(FeatureSet* _Nonnull feature_set, std::string* _Nonnull error);

#if defined(__linux__)
// Get the path of a file containing the path to the server executable, if the socket spec set via
// adb_set_socket_spec is a local one.
std::optional<std::string> adb_get_server_executable_path();
#endif

// Globally acccesible argv/envp, for the purpose of re-execing adb.
extern const char* _Nullable * _Nullable __adb_argv;
extern const char* _Nullable * _Nullable __adb_envp;
+15 −8
Original line number Diff line number Diff line
@@ -295,7 +295,10 @@ int read_and_dump(int fd, bool use_shell_protocol = false,
                    callback->OnStderr(buffer_ptr, length);
                    break;
                case ShellProtocol::kIdExit:
                    exit_code = protocol->data()[0];
                    // data() returns a char* which doesn't have defined signedness.
                    // Cast to uint8_t to prevent 255 from being sign extended to INT_MIN,
                    // which doesn't get truncated on Windows.
                    exit_code = static_cast<uint8_t>(protocol->data()[0]);
                    continue;
                default:
                    continue;
@@ -1303,9 +1306,9 @@ static void parse_push_pull_args(const char** arg, int narg, std::vector<const c
    }
}

static int adb_connect_command(const std::string& command) {
static int adb_connect_command(const std::string& command, TransportId* transport = nullptr) {
    std::string error;
    unique_fd fd(adb_connect(command, &error));
    unique_fd fd(adb_connect(transport, command, &error));
    if (fd < 0) {
        fprintf(stderr, "error: %s\n", error.c_str());
        return 1;
@@ -1569,6 +1572,10 @@ int adb_commandline(int argc, const char** argv) {
        }

        std::string query = android::base::StringPrintf("host:%s%s", argv[0], listopt);
        std::string error;
        if (!adb_check_server_version(&error)) {
            error_exit("failed to check server version: %s", error.c_str());
        }
        printf("List of devices attached\n");
        return adb_query_command(query);
    }
Loading