blob: dc2104313aa3827b55a9659b86489322484b1b5d [file] [log] [blame]
Mark Salyzyn6c6ec722015-10-24 16:20:18 -07001/*
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
Mark Salyzyn37bbf802019-03-13 07:25:52 -070019#include <stdint.h>
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070020#include <sys/epoll.h>
21
22#include <chrono>
23#include <functional>
24#include <map>
25
David Anderson0fa7c402022-03-07 19:10:57 -080026#include <android-base/logging.h>
27
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070028namespace android {
29namespace init {
30
31Epoll::Epoll() {}
32
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070033Result<void> Epoll::Open() {
34 if (epoll_fd_ >= 0) return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070035 epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC));
36
37 if (epoll_fd_ == -1) {
38 return ErrnoError() << "epoll_create1 failed";
39 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070040 return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070041}
42
David Anderson1de73842021-05-13 20:18:14 -070043Result<void> Epoll::RegisterHandler(int fd, Handler handler, uint32_t events) {
Mark Salyzyn37bbf802019-03-13 07:25:52 -070044 if (!events) {
45 return Error() << "Must specify events";
46 }
David Anderson0fa7c402022-03-07 19:10:57 -080047
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));
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070052 if (!inserted) {
53 return Error() << "Cannot specify two epoll handlers for a given FD";
54 }
55 epoll_event ev;
Mark Salyzyn37bbf802019-03-13 07:25:52 -070056 ev.events = events;
Bart Van Asscheb0177a02022-10-18 15:48:31 -070057 ev.data.fd = fd;
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070058 if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev) == -1) {
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070059 Result<void> result = ErrnoError() << "epoll_ctl failed to add fd";
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070060 epoll_handlers_.erase(fd);
61 return result;
62 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070063 return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070064}
65
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070066Result<void> Epoll::UnregisterHandler(int fd) {
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070067 if (epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) == -1) {
68 return ErrnoError() << "epoll_ctl failed to remove fd";
69 }
70 if (epoll_handlers_.erase(fd) != 1) {
71 return Error() << "Attempting to remove epoll handler for FD without an existing handler";
72 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070073 return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070074}
75
Bart Van Asschea2c16042022-10-14 09:35:35 -070076void Epoll::SetFirstCallback(std::function<void()> first_callback) {
77 first_callback_ = std::move(first_callback);
78}
79
David Anderson1de73842021-05-13 20:18:14 -070080Result<std::vector<std::shared_ptr<Epoll::Handler>>> Epoll::Wait(
Tom Cherry905a5df2019-08-30 14:12:56 -070081 std::optional<std::chrono::milliseconds> timeout) {
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070082 int timeout_ms = -1;
83 if (timeout && timeout->count() < INT_MAX) {
84 timeout_ms = timeout->count();
85 }
Tom Cherry905a5df2019-08-30 14:12:56 -070086 const auto max_events = epoll_handlers_.size();
87 epoll_event ev[max_events];
88 auto num_events = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd_, ev, max_events, timeout_ms));
89 if (num_events == -1) {
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070090 return ErrnoError() << "epoll_wait failed";
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070091 }
Bart Van Asschea2c16042022-10-14 09:35:35 -070092 if (num_events > 0 && first_callback_) {
93 first_callback_();
94 }
David Anderson1de73842021-05-13 20:18:14 -070095 std::vector<std::shared_ptr<Handler>> pending_functions;
Tom Cherry905a5df2019-08-30 14:12:56 -070096 for (int i = 0; i < num_events; ++i) {
Bart Van Asscheb0177a02022-10-18 15:48:31 -070097 const auto it = epoll_handlers_.find(ev[i].data.fd);
98 if (it == epoll_handlers_.end()) {
99 continue;
100 }
101 const Info& info = it->second;
David Anderson0fa7c402022-03-07 19:10:57 -0800102 if ((info.events & (EPOLLIN | EPOLLPRI)) == (EPOLLIN | EPOLLPRI) &&
103 (ev[i].events & EPOLLIN) != ev[i].events) {
104 // This handler wants to know about exception events, and just got one.
105 // Log something informational.
106 LOG(ERROR) << "Received unexpected epoll event set: " << ev[i].events;
107 }
108 pending_functions.emplace_back(info.handler);
Tom Cherry905a5df2019-08-30 14:12:56 -0700109 }
110
111 return pending_functions;
Mark Salyzyn6c6ec722015-10-24 16:20:18 -0700112}
113
114} // namespace init
115} // namespace android