From 0fa7c40c4cf1597dd0cca1f60b5eef8fdf6168a3 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 7 Mar 2022 19:10:57 -0800 Subject: [PATCH] init: Add diagnostics for signalfd epoll failures. Bug: 222441619 Test: inject failure with close, check for log messages Change-Id: Ia18b88841779e230d00a6c47aaed4952b1444f85 --- init/epoll.cpp | 19 +++++++++++++++---- init/epoll.h | 7 ++++++- init/init.cpp | 3 ++- init/sigchld_handler.cpp | 5 ++++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/init/epoll.cpp b/init/epoll.cpp index 74d8aac96..0580f8608 100644 --- a/init/epoll.cpp +++ b/init/epoll.cpp @@ -23,6 +23,8 @@ #include #include +#include + namespace android { namespace init { @@ -42,8 +44,11 @@ Result Epoll::RegisterHandler(int fd, Handler handler, uint32_t events) { if (!events) { return Error() << "Must specify events"; } - auto sp = std::make_shared(std::move(handler)); - auto [it, inserted] = epoll_handlers_.emplace(fd, std::move(sp)); + + Info info; + info.events = events; + info.handler = std::make_shared(std::move(handler)); + auto [it, inserted] = epoll_handlers_.emplace(fd, std::move(info)); if (!inserted) { return Error() << "Cannot specify two epoll handlers for a given FD"; } @@ -84,8 +89,14 @@ Result>> Epoll::Wait( } std::vector> pending_functions; for (int i = 0; i < num_events; ++i) { - auto sp = *reinterpret_cast*>(ev[i].data.ptr); - pending_functions.emplace_back(std::move(sp)); + auto& info = *reinterpret_cast(ev[i].data.ptr); + if ((info.events & (EPOLLIN | EPOLLPRI)) == (EPOLLIN | EPOLLPRI) && + (ev[i].events & EPOLLIN) != ev[i].events) { + // This handler wants to know about exception events, and just got one. + // Log something informational. + LOG(ERROR) << "Received unexpected epoll event set: " << ev[i].events; + } + pending_functions.emplace_back(info.handler); } return pending_functions; diff --git a/init/epoll.h b/init/epoll.h index 0df528935..f58ae8df3 100644 --- a/init/epoll.h +++ b/init/epoll.h @@ -46,8 +46,13 @@ class Epoll { std::optional timeout); private: + struct Info { + std::shared_ptr handler; + uint32_t events; + }; + android::base::unique_fd epoll_fd_; - std::map> epoll_handlers_; + std::map epoll_handlers_; }; } // namespace init diff --git a/init/init.cpp b/init/init.cpp index 1df4c4489..96168050e 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -646,7 +646,8 @@ static void InstallSignalFdHandler(Epoll* epoll) { PLOG(FATAL) << "failed to create signalfd"; } - if (auto result = epoll->RegisterHandler(signal_fd, HandleSignalFd); !result.ok()) { + constexpr int flags = EPOLLIN | EPOLLPRI; + if (auto result = epoll->RegisterHandler(signal_fd, HandleSignalFd, flags); !result.ok()) { LOG(FATAL) << result.error(); } } diff --git a/init/sigchld_handler.cpp b/init/sigchld_handler.cpp index 9b2c7d939..6fc64df33 100644 --- a/init/sigchld_handler.cpp +++ b/init/sigchld_handler.cpp @@ -95,7 +95,10 @@ static pid_t ReapOneProcess() { LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string; } - if (!service) return pid; + if (!service) { + LOG(INFO) << name << " did not have an associated service entry and will not be reaped"; + return pid; + } service->Reap(siginfo);