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

Commit 3777d2ec authored by Josh Gao's avatar Josh Gao
Browse files

adb: don't emulate fdevent or socketpair on Windows.

Change-Id: I16cf7d4427eb79f36db39e91f85402a268fa72f5
parent 519f9aea
Loading
Loading
Loading
Loading
+1 −2
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 \
@@ -75,12 +76,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 \

+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) {
+10 −9
Original line number Diff line number Diff line
@@ -21,10 +21,8 @@
#include "fdevent.h"

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

#include <list>
@@ -54,7 +52,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));
@@ -73,17 +71,17 @@ struct PollNode {
static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
static auto& g_pending_list = *new std::list<fdevent*>();
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 +215,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 +231,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 +287,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 */)
{
+26 −21
Original line number Diff line number Diff line
@@ -180,6 +180,14 @@ static __inline__ int adb_thread_setname(const std::string& name) {
    return 0;
}

static __inline__ adb_thread_t adb_thread_self() {
    return GetCurrentThread();
}

static __inline__ bool adb_thread_equal(adb_thread_t lhs, adb_thread_t rhs) {
    return GetThreadId(lhs) == GetThreadId(rhs);
}

static __inline__  unsigned long adb_thread_id()
{
    return GetCurrentThreadId();
@@ -263,24 +271,6 @@ int unix_isatty(int fd);
/* normally provided by <cutils/misc.h> */
extern void*  load_file(const char*  pathname, unsigned*  psize);

/* normally provided by "fdevent.h" */

#define FDE_READ              0x0001
#define FDE_WRITE             0x0002
#define FDE_ERROR             0x0004
#define FDE_DONT_CLOSE        0x0080

typedef void (*fd_func)(int fd, unsigned events, void *userdata);

fdevent *fdevent_create(int fd, fd_func func, void *arg);
void     fdevent_destroy(fdevent *fde);
void     fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
void     fdevent_remove(fdevent *item);
void     fdevent_set(fdevent *fde, unsigned events);
void     fdevent_add(fdevent *fde, unsigned events);
void     fdevent_del(fdevent *fde, unsigned events);
void     fdevent_loop();

static __inline__ void  adb_sleep_ms( int  mseconds )
{
    Sleep( mseconds );
@@ -304,6 +294,14 @@ extern int adb_setsockopt(int fd, int level, int optname, const void* optva

extern int  adb_socketpair( int  sv[2] );

struct adb_pollfd {
    int fd;
    short events;
    short revents;
};
extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
#define poll ___xxx_poll

static __inline__ int adb_is_absolute_host_path(const char* path) {
    return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
}
@@ -456,14 +454,14 @@ size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>*

#else /* !_WIN32 a.k.a. Unix */

#include "fdevent.h"
#include <cutils/misc.h>
#include <cutils/sockets.h>
#include <cutils/threads.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>

#include <pthread.h>
#include <unistd.h>
@@ -803,6 +801,13 @@ static __inline__ int adb_socketpair( int sv[2] )
#undef   socketpair
#define  socketpair   ___xxx_socketpair

typedef struct pollfd adb_pollfd;
static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
    return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
}

#define poll ___xxx_poll

static __inline__ void  adb_sleep_ms( int  mseconds )
{
    usleep( mseconds*1000 );
+103 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#include <unistd.h>
#include <atomic>

#include "adb_io.h"
#include "sysdeps.h"

static void increment_atomic_int(void* c) {
@@ -67,3 +68,105 @@ TEST(sysdeps_thread, exit) {
        nullptr, &thread));
    ASSERT_TRUE(adb_thread_join(thread));
}

TEST(sysdeps_socketpair, smoke) {
    int fds[2];
    ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
    ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4));
    ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4));

    char buf[4];
    ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4));
    ASSERT_STREQ(buf, "foo");
    ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4));
    ASSERT_STREQ(buf, "bar");
    ASSERT_EQ(0, adb_close(fds[0]));
    ASSERT_EQ(0, adb_close(fds[1]));
}

class sysdeps_poll : public ::testing::Test {
  protected:
    int fds[2];
    void SetUp() override {
        ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
    }

    void TearDown() override {
        ASSERT_EQ(0, adb_close(fds[0]));
        ASSERT_EQ(0, adb_close(fds[1]));
    }
};

TEST_F(sysdeps_poll, smoke) {
    adb_pollfd pfd[2];
    pfd[0].fd = fds[0];
    pfd[0].events = POLLRDNORM;
    pfd[1].fd = fds[1];
    pfd[1].events = POLLWRNORM;

    EXPECT_EQ(1, adb_poll(pfd, 2, 0));
    EXPECT_EQ(0, pfd[0].revents);
    EXPECT_EQ(POLLWRNORM, pfd[1].revents);

    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));

    // Wait for the socketpair to be flushed.
    EXPECT_EQ(1, adb_poll(pfd, 1, 100));
    EXPECT_EQ(POLLRDNORM, pfd[0].revents);

    EXPECT_EQ(2, adb_poll(pfd, 2, 0));
    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    EXPECT_EQ(POLLWRNORM, pfd[1].revents);
}

TEST_F(sysdeps_poll, timeout) {
    adb_pollfd pfd;
    pfd.fd = fds[0];
    pfd.events = POLLRDNORM;

    EXPECT_EQ(0, adb_poll(&pfd, 1, 100));
    EXPECT_EQ(0, pfd.revents);

    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));

    EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
    EXPECT_EQ(POLLRDNORM, pfd.revents);
}

TEST_F(sysdeps_poll, invalid_fd) {
    adb_pollfd pfd[3];
    pfd[0].fd = fds[0];
    pfd[0].events = POLLRDNORM;
    pfd[1].fd = INT_MAX;
    pfd[1].events = POLLRDNORM;
    pfd[2].fd = fds[1];
    pfd[2].events = POLLWRNORM;

    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));

    // Wait for the socketpair to be flushed.
    EXPECT_EQ(1, adb_poll(pfd, 1, 100));
    EXPECT_EQ(POLLRDNORM, pfd[0].revents);

    EXPECT_EQ(3, adb_poll(pfd, 3, 0));
    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    EXPECT_EQ(POLLNVAL, pfd[1].revents);
    EXPECT_EQ(POLLWRNORM, pfd[2].revents);
}

TEST_F(sysdeps_poll, duplicate_fd) {
    adb_pollfd pfd[2];
    pfd[0].fd = fds[0];
    pfd[0].events = POLLRDNORM;
    pfd[1] = pfd[0];

    EXPECT_EQ(0, adb_poll(pfd, 2, 0));
    EXPECT_EQ(0, pfd[0].revents);
    EXPECT_EQ(0, pfd[1].revents);

    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));

    EXPECT_EQ(2, adb_poll(pfd, 2, 100));
    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    EXPECT_EQ(POLLRDNORM, pfd[1].revents);
}
Loading