1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "epoll.h"
18
19 #include <stdint.h>
20 #include <sys/epoll.h>
21
22 #include <chrono>
23 #include <functional>
24 #include <map>
25
26 #include <android-base/logging.h>
27
28 namespace android {
29 namespace init {
30
Epoll()31 Epoll::Epoll() {}
32
Open()33 Result<void> Epoll::Open() {
34 if (epoll_fd_ >= 0) return {};
35 epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC));
36
37 if (epoll_fd_ == -1) {
38 return ErrnoError() << "epoll_create1 failed";
39 }
40 return {};
41 }
42
RegisterHandler(int fd,Handler handler,uint32_t events)43 Result<void> Epoll::RegisterHandler(int fd, Handler handler, uint32_t events) {
44 if (!events) {
45 return Error() << "Must specify events";
46 }
47
48 Info info;
49 info.events = events;
50 info.handler = std::make_shared<decltype(handler)>(std::move(handler));
51 auto [it, inserted] = epoll_handlers_.emplace(fd, std::move(info));
52 if (!inserted) {
53 return Error() << "Cannot specify two epoll handlers for a given FD";
54 }
55 epoll_event ev;
56 ev.events = events;
57 // std::map's iterators do not get invalidated until erased, so we use the
58 // pointer to the std::function in the map directly for epoll_ctl.
59 ev.data.ptr = reinterpret_cast<void*>(&it->second);
60 if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev) == -1) {
61 Result<void> result = ErrnoError() << "epoll_ctl failed to add fd";
62 epoll_handlers_.erase(fd);
63 return result;
64 }
65 return {};
66 }
67
UnregisterHandler(int fd)68 Result<void> Epoll::UnregisterHandler(int fd) {
69 if (epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) == -1) {
70 return ErrnoError() << "epoll_ctl failed to remove fd";
71 }
72 if (epoll_handlers_.erase(fd) != 1) {
73 return Error() << "Attempting to remove epoll handler for FD without an existing handler";
74 }
75 return {};
76 }
77
Wait(std::optional<std::chrono::milliseconds> timeout)78 Result<std::vector<std::shared_ptr<Epoll::Handler>>> Epoll::Wait(
79 std::optional<std::chrono::milliseconds> timeout) {
80 int timeout_ms = -1;
81 if (timeout && timeout->count() < INT_MAX) {
82 timeout_ms = timeout->count();
83 }
84 const auto max_events = epoll_handlers_.size();
85 epoll_event ev[max_events];
86 auto num_events = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd_, ev, max_events, timeout_ms));
87 if (num_events == -1) {
88 return ErrnoError() << "epoll_wait failed";
89 }
90 std::vector<std::shared_ptr<Handler>> pending_functions;
91 for (int i = 0; i < num_events; ++i) {
92 auto& info = *reinterpret_cast<Info*>(ev[i].data.ptr);
93 if ((info.events & (EPOLLIN | EPOLLPRI)) == (EPOLLIN | EPOLLPRI) &&
94 (ev[i].events & EPOLLIN) != ev[i].events) {
95 // This handler wants to know about exception events, and just got one.
96 // Log something informational.
97 LOG(ERROR) << "Received unexpected epoll event set: " << ev[i].events;
98 }
99 pending_functions.emplace_back(info.handler);
100 }
101
102 return pending_functions;
103 }
104
105 } // namespace init
106 } // namespace android
107