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

Commit 4a908422 authored by Josh Gao's avatar Josh Gao Committed by Gerrit Code Review
Browse files

Merge changes I6541bb13,I486055bb,Id6ac1c54,I16cf7d44

* changes:
  adb: make fdevent_test, socket_test compile on Windows.
  adb: add fd exhaustion test, fix errno reporting in sysdeps_win32.
  adb: move win32 fd base to 2048, fix fd allocation.
  adb: don't emulate fdevent or socketpair on Windows.
parents ee127f3d 022d447e
Loading
Loading
Loading
Loading
+3 −10
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ LIBADB_SRC_FILES := \
    adb_listeners.cpp \
    adb_trace.cpp \
    adb_utils.cpp \
    fdevent.cpp \
    sockets.cpp \
    transport.cpp \
    transport_local.cpp \
@@ -58,6 +59,8 @@ LIBADB_SRC_FILES := \
LIBADB_TEST_SRCS := \
    adb_io_test.cpp \
    adb_utils_test.cpp \
    fdevent_test.cpp \
    socket_test.cpp \
    sysdeps_test.cpp \
    transport_test.cpp \

@@ -75,12 +78,10 @@ LIBADB_windows_CFLAGS := \
    $(ADB_COMMON_windows_CFLAGS) \

LIBADB_darwin_SRC_FILES := \
    fdevent.cpp \
    get_my_path_darwin.cpp \
    usb_osx.cpp \

LIBADB_linux_SRC_FILES := \
    fdevent.cpp \
    get_my_path_linux.cpp \
    usb_linux.cpp \

@@ -88,14 +89,6 @@ LIBADB_windows_SRC_FILES := \
    sysdeps_win32.cpp \
    usb_windows.cpp \

LIBADB_TEST_linux_SRCS := \
    fdevent_test.cpp \
    socket_test.cpp \

LIBADB_TEST_darwin_SRCS := \
    fdevent_test.cpp \
    socket_test.cpp \

LIBADB_TEST_windows_SRCS := \
    sysdeps_win32_test.cpp \

+1 −0
Original line number Diff line number Diff line
@@ -213,6 +213,7 @@ std::string perror_str(const char* msg) {
}

#if !defined(_WIN32)
// Windows version provided in sysdeps_win32.cpp
bool set_file_block_mode(int fd, bool block) {
    int flags = fcntl(fd, F_GETFL, 0);
    if (flags == -1) {
+21 −9
Original line number Diff line number Diff line
@@ -21,12 +21,11 @@
#include "fdevent.h"

#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include <atomic>
#include <list>
#include <unordered_map>
#include <vector>
@@ -54,7 +53,7 @@ int SHELL_EXIT_NOTIFY_FD = -1;

struct PollNode {
  fdevent* fde;
  ::pollfd pollfd;
  adb_pollfd pollfd;

  PollNode(fdevent* fde) : fde(fde) {
      memset(&pollfd, 0, sizeof(pollfd));
@@ -72,18 +71,19 @@ struct PollNode {
// That's why we don't need a lock for fdevent.
static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
static auto& g_pending_list = *new std::list<fdevent*>();
static std::atomic<bool> terminate_loop(false);
static bool main_thread_valid;
static pthread_t main_thread;
static unsigned long main_thread_id;

static void check_main_thread() {
    if (main_thread_valid) {
        CHECK_NE(0, pthread_equal(main_thread, pthread_self()));
        CHECK_EQ(main_thread_id, adb_thread_id());
    }
}

static void set_main_thread() {
    main_thread_valid = true;
    main_thread = pthread_self();
    main_thread_id = adb_thread_id();
}

static std::string dump_fde(const fdevent* fde) {
@@ -217,7 +217,7 @@ void fdevent_del(fdevent* fde, unsigned events) {
    fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
}

static std::string dump_pollfds(const std::vector<pollfd>& pollfds) {
static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
    std::string result;
    for (const auto& pollfd : pollfds) {
        std::string op;
@@ -233,13 +233,13 @@ static std::string dump_pollfds(const std::vector<pollfd>& pollfds) {
}

static void fdevent_process() {
    std::vector<pollfd> pollfds;
    std::vector<adb_pollfd> pollfds;
    for (const auto& pair : g_poll_node_map) {
        pollfds.push_back(pair.second.pollfd);
    }
    CHECK_GT(pollfds.size(), 0u);
    D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
    int ret = TEMP_FAILURE_RETRY(poll(&pollfds[0], pollfds.size(), -1));
    int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
    if (ret == -1) {
        PLOG(ERROR) << "poll(), ret = " << ret;
        return;
@@ -289,6 +289,9 @@ static void fdevent_call_fdfunc(fdevent* fde)
}

#if !ADB_HOST

#include <sys/ioctl.h>

static void fdevent_subproc_event_func(int fd, unsigned ev,
                                       void* /* userdata */)
{
@@ -363,6 +366,10 @@ void fdevent_loop()
#endif // !ADB_HOST

    while (true) {
        if (terminate_loop) {
            return;
        }

        D("--- --- waiting for events");

        fdevent_process();
@@ -375,6 +382,10 @@ void fdevent_loop()
    }
}

void fdevent_terminate_loop() {
    terminate_loop = true;
}

size_t fdevent_installed_count() {
    return g_poll_node_map.size();
}
@@ -383,4 +394,5 @@ void fdevent_reset() {
    g_poll_node_map.clear();
    g_pending_list.clear();
    main_thread_valid = false;
    terminate_loop = false;
}
+2 −2
Original line number Diff line number Diff line
@@ -76,9 +76,9 @@ void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms);
*/
void fdevent_loop();

// For debugging only.
// The following functions are used only for tests.
void fdevent_terminate_loop();
size_t fdevent_installed_count();
// For debugging only.
void fdevent_reset();

#endif
+24 −39
Original line number Diff line number Diff line
@@ -18,15 +18,13 @@

#include <gtest/gtest.h>

#include <pthread.h>
#include <signal.h>

#include <limits>
#include <queue>
#include <string>
#include <vector>

#include "adb_io.h"
#include "fdevent_test.h"

class FdHandler {
  public:
@@ -48,7 +46,7 @@ class FdHandler {
        if (events & FDE_READ) {
            ASSERT_EQ(fd, handler->read_fd_);
            char c;
            ASSERT_EQ(1, read(fd, &c, 1));
            ASSERT_EQ(1, adb_read(fd, &c, 1));
            handler->queue_.push(c);
            fdevent_add(&handler->write_fde_, FDE_WRITE);
        }
@@ -57,7 +55,7 @@ class FdHandler {
            ASSERT_FALSE(handler->queue_.empty());
            char c = handler->queue_.front();
            handler->queue_.pop();
            ASSERT_EQ(1, write(fd, &c, 1));
            ASSERT_EQ(1, adb_write(fd, &c, 1));
            if (handler->queue_.empty()) {
              fdevent_del(&handler->write_fde_, FDE_WRITE);
            }
@@ -72,29 +70,19 @@ class FdHandler {
    std::queue<char> queue_;
};

static void signal_handler(int) {
    pthread_exit(nullptr);
}

class FdeventTest : public ::testing::Test {
  protected:
    static void SetUpTestCase() {
        ASSERT_NE(SIG_ERR, signal(SIGUSR1, signal_handler));
        ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN));
    }

    virtual void SetUp() {
        fdevent_reset();
        ASSERT_EQ(0u, fdevent_installed_count());
    }
};

struct ThreadArg {
    int first_read_fd;
    int last_write_fd;
    size_t middle_pipe_count;
};

TEST_F(FdeventTest, fdevent_terminate) {
    adb_thread_t thread;
    PrepareThread();
    ASSERT_TRUE(adb_thread_create([](void*) { fdevent_loop(); }, nullptr, &thread));
    TerminateThread(thread);
}

static void FdEventThreadFunc(ThreadArg* arg) {
    std::vector<int> read_fds;
    std::vector<int> write_fds;
@@ -102,7 +90,7 @@ static void FdEventThreadFunc(ThreadArg* arg) {
    read_fds.push_back(arg->first_read_fd);
    for (size_t i = 0; i < arg->middle_pipe_count; ++i) {
        int fds[2];
        ASSERT_EQ(0, pipe(fds));
        ASSERT_EQ(0, adb_socketpair(fds));
        read_fds.push_back(fds[0]);
        write_fds.push_back(fds[1]);
    }
@@ -122,9 +110,9 @@ TEST_F(FdeventTest, smoke) {
    const std::string MESSAGE = "fdevent_test";
    int fd_pair1[2];
    int fd_pair2[2];
    ASSERT_EQ(0, pipe(fd_pair1));
    ASSERT_EQ(0, pipe(fd_pair2));
    pthread_t thread;
    ASSERT_EQ(0, adb_socketpair(fd_pair1));
    ASSERT_EQ(0, adb_socketpair(fd_pair2));
    adb_thread_t thread;
    ThreadArg thread_arg;
    thread_arg.first_read_fd = fd_pair1[0];
    thread_arg.last_write_fd = fd_pair2[1];
@@ -132,9 +120,9 @@ TEST_F(FdeventTest, smoke) {
    int writer = fd_pair1[1];
    int reader = fd_pair2[0];

    ASSERT_EQ(0, pthread_create(&thread, nullptr,
                                reinterpret_cast<void* (*)(void*)>(FdEventThreadFunc),
                                &thread_arg));
    PrepareThread();
    ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(FdEventThreadFunc), &thread_arg,
                                  &thread));

    for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
        std::string read_buffer = MESSAGE;
@@ -144,10 +132,9 @@ TEST_F(FdeventTest, smoke) {
        ASSERT_EQ(read_buffer, write_buffer);
    }

    ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
    ASSERT_EQ(0, pthread_join(thread, nullptr));
    ASSERT_EQ(0, close(writer));
    ASSERT_EQ(0, close(reader));
    TerminateThread(thread);
    ASSERT_EQ(0, adb_close(writer));
    ASSERT_EQ(0, adb_close(reader));
}

struct InvalidFdArg {
@@ -161,7 +148,7 @@ static void InvalidFdEventCallback(int fd, unsigned events, void* userdata) {
    ASSERT_EQ(arg->expected_events, events);
    fdevent_remove(&arg->fde);
    if (++*(arg->happened_event_count) == 2) {
        pthread_exit(nullptr);
        fdevent_terminate_loop();
    }
}

@@ -184,9 +171,7 @@ static void InvalidFdThreadFunc(void*) {
}

TEST_F(FdeventTest, invalid_fd) {
    pthread_t thread;
    ASSERT_EQ(0, pthread_create(&thread, nullptr,
                                reinterpret_cast<void* (*)(void*)>(InvalidFdThreadFunc),
                                nullptr));
    ASSERT_EQ(0, pthread_join(thread, nullptr));
    adb_thread_t thread;
    ASSERT_TRUE(adb_thread_create(InvalidFdThreadFunc, nullptr, &thread));
    ASSERT_TRUE(adb_thread_join(thread));
}
Loading