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

Commit 71f775a9 authored by Josh Gao's avatar Josh Gao
Browse files

adb: remove fdevent_install, fdevent_remove.

Remove fdevent_install and fdevent_remove in favor of using
fdevent_create and fdevent_destroy, so that we can put RAII types (i.e.
unique_fd) into fdevent without worrying about -Wexit-time-destructors
or structs that are freed instead of deleted.

Bug: http://b/79786774
Test: python test_device.py
Change-Id: I8471cc00574ed492fe1b196944976cdaae8b7cff
parent 27440849
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ class alistener {
    alistener(const std::string& _local_name, const std::string& _connect_to);
    ~alistener();

    fdevent fde;
    fdevent* fde = nullptr;
    int fd = -1;

    std::string local_name;
@@ -60,7 +60,7 @@ alistener::alistener(const std::string& _local_name, const std::string& _connect

alistener::~alistener() {
    // Closes the corresponding fd.
    fdevent_remove(&fde);
    fdevent_destroy(fde);

    if (transport) {
        transport->RemoveDisconnect(&disconnect);
@@ -222,11 +222,11 @@ InstallStatus install_listener(const std::string& local_name, const char* connec

    close_on_exec(listener->fd);
    if (listener->connect_to == "*smartsocket*") {
        fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, listener.get());
        listener->fde = fdevent_create(listener->fd, ss_listener_event_func, listener.get());
    } else {
        fdevent_install(&listener->fde, listener->fd, listener_event_func, listener.get());
        listener->fde = fdevent_create(listener->fd, listener_event_func, listener.get());
    }
    fdevent_set(&listener->fde, FDE_READ);
    fdevent_set(listener->fde, FDE_READ);

    listener->transport = transport;

+12 −15
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@
#include "sysdeps.h"

static DNSServiceRef service_ref;
static fdevent service_ref_fde;
static fdevent* service_ref_fde;

// Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD()
// directly so that the socket is put through the appropriate compatibility
@@ -73,22 +73,21 @@ class AsyncServiceRef {
        }

        DNSServiceRefDeallocate(sdRef_);
        fdevent_remove(&fde_);
        fdevent_destroy(fde_);
    }

  protected:
    DNSServiceRef sdRef_;

    void Initialize() {
        fdevent_install(&fde_, adb_DNSServiceRefSockFD(sdRef_),
                        pump_service_ref, &sdRef_);
        fdevent_set(&fde_, FDE_READ);
        fde_ = fdevent_create(adb_DNSServiceRefSockFD(sdRef_), pump_service_ref, &sdRef_);
        fdevent_set(fde_, FDE_READ);
        initialized_ = true;
    }

  private:
    bool initialized_ = false;
    fdevent fde_;
    fdevent* fde_;
};

class ResolvedService : public AsyncServiceRef {
@@ -252,13 +251,11 @@ static void DNSSD_API register_mdns_transport(DNSServiceRef sdRef,
    if (errorCode != kDNSServiceErr_NoError) {
        D("Got error %d during mDNS browse.", errorCode);
        DNSServiceRefDeallocate(sdRef);
        fdevent_remove(&service_ref_fde);
        fdevent_destroy(service_ref_fde);
        return;
    }

    auto discovered = new DiscoveredService(interfaceIndex, serviceName,
                                            regtype, domain);

    auto discovered = new DiscoveredService(interfaceIndex, serviceName, regtype, domain);
    if (!discovered->Initialized()) {
        delete discovered;
    }
@@ -274,9 +271,9 @@ void init_mdns_transport_discovery_thread(void) {
    }

    fdevent_run_on_main_thread([]() {
        fdevent_install(&service_ref_fde, adb_DNSServiceRefSockFD(service_ref), pump_service_ref,
                        &service_ref);
        fdevent_set(&service_ref_fde, FDE_READ);
        service_ref_fde =
            fdevent_create(adb_DNSServiceRefSockFD(service_ref), pump_service_ref, &service_ref);
        fdevent_set(service_ref_fde, FDE_READ);
    });
}

+10 −8
Original line number Diff line number Diff line
@@ -35,8 +35,8 @@
#include <openssl/rsa.h>
#include <openssl/sha.h>

static fdevent listener_fde;
static fdevent framework_fde;
static fdevent* listener_fde = nullptr;
static fdevent* framework_fde = nullptr;
static int framework_fd = -1;

static void usb_disconnected(void* unused, atransport* t);
@@ -106,9 +106,11 @@ static void usb_disconnected(void* unused, atransport* t) {

static void framework_disconnected() {
    LOG(INFO) << "Framework disconnect";
    fdevent_remove(&framework_fde);
    if (framework_fde) {
        fdevent_destroy(framework_fde);
        framework_fd = -1;
    }
}

static void adbd_auth_event(int fd, unsigned events, void*) {
    if (events & FDE_READ) {
@@ -168,8 +170,8 @@ static void adbd_auth_listener(int fd, unsigned events, void* data) {
    }

    framework_fd = s;
    fdevent_install(&framework_fde, framework_fd, adbd_auth_event, nullptr);
    fdevent_add(&framework_fde, FDE_READ);
    framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr);
    fdevent_add(framework_fde, FDE_READ);

    if (needs_retry) {
        needs_retry = false;
@@ -198,8 +200,8 @@ void adbd_auth_init(void) {
        return;
    }

    fdevent_install(&listener_fde, fd, adbd_auth_listener, NULL);
    fdevent_add(&listener_fde, FDE_READ);
    listener_fde = fdevent_create(fd, adbd_auth_listener, NULL);
    fdevent_add(listener_fde, FDE_READ);
}

void send_auth_request(atransport* t) {
+17 −21
Original line number Diff line number Diff line
@@ -117,29 +117,17 @@ static std::string dump_fde(const fdevent* fde) {
    return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
}

fdevent* fdevent_create(int fd, fd_func func, void* arg) {
    check_main_thread();
    fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
    if(fde == 0) return 0;
    fdevent_install(fde, fd, func, arg);
    fde->state |= FDE_CREATED;
    return fde;
}

void fdevent_destroy(fdevent* fde) {
void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
    check_main_thread();
    if(fde == 0) return;
    if(!(fde->state & FDE_CREATED)) {
        LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
    }
    fdevent_remove(fde);
    free(fde);
    CHECK_GE(fd, 0);
    memset(fde, 0, sizeof(fdevent));
}

void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
fdevent* fdevent_create(int fd, fd_func func, void* arg) {
    check_main_thread();
    CHECK_GE(fd, 0);
    memset(fde, 0, sizeof(fdevent));

    fdevent* fde = new fdevent();
    fde->state = FDE_ACTIVE;
    fde->fd = fd;
    fde->func = func;
@@ -152,12 +140,18 @@ void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
    }
    auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
    CHECK(pair.second) << "install existing fd " << fd;
    D("fdevent_install %s", dump_fde(fde).c_str());

    fde->state |= FDE_CREATED;
    return fde;
}

void fdevent_remove(fdevent* fde) {
void fdevent_destroy(fdevent* fde) {
    check_main_thread();
    D("fdevent_remove %s", dump_fde(fde).c_str());
    if (fde == 0) return;
    if (!(fde->state & FDE_CREATED)) {
        LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
    }

    if (fde->state & FDE_ACTIVE) {
        g_poll_node_map.erase(fde->fd);
        if (fde->state & FDE_PENDING) {
@@ -170,6 +164,8 @@ void fdevent_remove(fdevent* fde) {
        fde->state = 0;
        fde->events = 0;
    }

    delete fde;
}

static void fdevent_update(fdevent* fde, unsigned events) {
+8 −17
Original line number Diff line number Diff line
@@ -33,17 +33,17 @@
typedef void (*fd_func)(int fd, unsigned events, void *userdata);

struct fdevent {
    fdevent *next;
    fdevent *prev;
    fdevent* next = nullptr;
    fdevent* prev = nullptr;

    int fd;
    int force_eof;
    int fd = -1;
    int force_eof = 0;

    uint16_t state;
    uint16_t events;
    uint16_t state = 0;
    uint16_t events = 0;

    fd_func func;
    void *arg;
    fd_func func = nullptr;
    void* arg = nullptr;
};

/* Allocate and initialize a new fdevent object
@@ -57,15 +57,6 @@ fdevent *fdevent_create(int fd, fd_func func, void *arg);
*/
void fdevent_destroy(fdevent *fde);

/* Initialize an fdevent object that was externally allocated
*/
void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);

/* Uninitialize an fdevent object that was initialized by
** fdevent_install()
*/
void fdevent_remove(fdevent *item);

/* Change which events should cause notifications
*/
void fdevent_set(fdevent *fde, unsigned events);
Loading