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

Commit 8ae7375f authored by Tom Cherry's avatar Tom Cherry Committed by Mark Salyzyn
Browse files

init: use std::function for epoll handling

Also allow unregistering of epoll handlers.

Bug: 64114943
Test: boot
Change-Id: I2abe6a56fd451839931d607dddb91669a7d02ff1
parent b004620f
Loading
Loading
Loading
Loading
+27 −7
Original line number Original line Diff line number Diff line
@@ -31,6 +31,10 @@
#include <sys/types.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>


#include <map>
#include <memory>
#include <optional>

#include <android-base/chrono_utils.h>
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/logging.h>
@@ -43,9 +47,6 @@
#include <private/android_filesystem_config.h>
#include <private/android_filesystem_config.h>
#include <selinux/android.h>
#include <selinux/android.h>


#include <memory>
#include <optional>

#include "action_parser.h"
#include "action_parser.h"
#include "import_parser.h"
#include "import_parser.h"
#include "init_first_stage.h"
#include "init_first_stage.h"
@@ -130,12 +131,31 @@ static void LoadBootScripts(ActionManager& action_manager, ServiceList& service_
    }
    }
}
}


void register_epoll_handler(int fd, void (*fn)()) {
static std::map<int, std::function<void()>> epoll_handlers;

void register_epoll_handler(int fd, std::function<void()> handler) {
    auto[it, inserted] = epoll_handlers.emplace(fd, std::move(handler));
    if (!inserted) {
        LOG(ERROR) << "Cannot specify two epoll handlers for a given FD";
        return;
    }
    epoll_event ev;
    epoll_event ev;
    ev.events = EPOLLIN;
    ev.events = EPOLLIN;
    ev.data.ptr = reinterpret_cast<void*>(fn);
    // std::map's iterators do not get invalidated until erased, so we use the pointer to the
    // std::function in the map directly for epoll_ctl.
    ev.data.ptr = reinterpret_cast<void*>(&it->second);
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
        PLOG(ERROR) << "epoll_ctl failed";
        PLOG(ERROR) << "epoll_ctl failed to add fd";
        epoll_handlers.erase(fd);
    }
}

void unregister_epoll_handler(int fd) {
    if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr) == -1) {
        PLOG(ERROR) << "epoll_ctl failed to remove fd";
    }
    if (epoll_handlers.erase(fd) != 1) {
        LOG(ERROR) << "Attempting to remove epoll handler for FD without an existing handler";
    }
    }
}
}


@@ -809,7 +829,7 @@ int main(int argc, char** argv) {
        if (nr == -1) {
        if (nr == -1) {
            PLOG(ERROR) << "epoll_wait failed";
            PLOG(ERROR) << "epoll_wait failed";
        } else if (nr == 1) {
        } else if (nr == 1) {
            ((void (*)()) ev.data.ptr)();
            std::invoke(*reinterpret_cast<std::function<void()>*>(ev.data.ptr));
        }
        }
    }
    }


+3 −1
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@


#include <sys/types.h>
#include <sys/types.h>


#include <functional>
#include <string>
#include <string>
#include <vector>
#include <vector>


@@ -42,7 +43,8 @@ void HandleControlMessage(const std::string& msg, const std::string& arg, pid_t


void property_changed(const std::string& name, const std::string& value);
void property_changed(const std::string& name, const std::string& value);


void register_epoll_handler(int fd, void (*fn)());
void register_epoll_handler(int fd, std::function<void()> handler);
void unregister_epoll_handler(int fd);


bool start_waiting_for_property(const char *name, const char *value);
bool start_waiting_for_property(const char *name, const char *value);