Loading init/init.cpp +27 −7 Original line number Original line Diff line number Diff line Loading @@ -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> Loading @@ -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" Loading Loading @@ -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"; } } } } Loading Loading @@ -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)); } } } } Loading init/init.h +3 −1 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,7 @@ #include <sys/types.h> #include <sys/types.h> #include <functional> #include <string> #include <string> #include <vector> #include <vector> Loading @@ -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); Loading Loading
init/init.cpp +27 −7 Original line number Original line Diff line number Diff line Loading @@ -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> Loading @@ -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" Loading Loading @@ -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"; } } } } Loading Loading @@ -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)); } } } } Loading
init/init.h +3 −1 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,7 @@ #include <sys/types.h> #include <sys/types.h> #include <functional> #include <string> #include <string> #include <vector> #include <vector> Loading @@ -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); Loading