| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | 37bbf80 | 2019-03-13 07:25:52 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 20 | #include <sys/epoll.h> | 
|  | 21 |  | 
|  | 22 | #include <chrono> | 
|  | 23 | #include <functional> | 
|  | 24 | #include <map> | 
|  | 25 |  | 
| David Anderson | 0fa7c40 | 2022-03-07 19:10:57 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> | 
|  | 27 |  | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 28 | namespace android { | 
|  | 29 | namespace init { | 
|  | 30 |  | 
|  | 31 | Epoll::Epoll() {} | 
|  | 32 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 33 | Result<void> Epoll::Open() { | 
|  | 34 | if (epoll_fd_ >= 0) return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 35 | epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC)); | 
|  | 36 |  | 
|  | 37 | if (epoll_fd_ == -1) { | 
|  | 38 | return ErrnoError() << "epoll_create1 failed"; | 
|  | 39 | } | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 40 | return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| David Anderson | 1de7384 | 2021-05-13 20:18:14 -0700 | [diff] [blame] | 43 | Result<void> Epoll::RegisterHandler(int fd, Handler handler, uint32_t events) { | 
| Mark Salyzyn | 37bbf80 | 2019-03-13 07:25:52 -0700 | [diff] [blame] | 44 | if (!events) { | 
|  | 45 | return Error() << "Must specify events"; | 
|  | 46 | } | 
| David Anderson | 0fa7c40 | 2022-03-07 19:10:57 -0800 | [diff] [blame] | 47 |  | 
| Bart Van Assche | a1c8a62 | 2022-10-14 09:41:17 -0700 | [diff] [blame] | 48 | auto [it, inserted] = epoll_handlers_.emplace( | 
|  | 49 | fd, Info{ | 
|  | 50 | .events = events, | 
| Bart Van Assche | dcd23df | 2022-10-14 12:55:35 -0700 | [diff] [blame] | 51 | .handler = std::move(handler), | 
| Bart Van Assche | a1c8a62 | 2022-10-14 09:41:17 -0700 | [diff] [blame] | 52 | }); | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 53 | if (!inserted) { | 
|  | 54 | return Error() << "Cannot specify two epoll handlers for a given FD"; | 
|  | 55 | } | 
| Bart Van Assche | a1c8a62 | 2022-10-14 09:41:17 -0700 | [diff] [blame] | 56 | epoll_event ev = { | 
|  | 57 | .events = events, | 
|  | 58 | .data.fd = fd, | 
|  | 59 | }; | 
| Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 60 | if (epoll_ctl(epoll_fd_.get(), EPOLL_CTL_ADD, fd, &ev) == -1) { | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 61 | Result<void> result = ErrnoError() << "epoll_ctl failed to add fd"; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 62 | epoll_handlers_.erase(fd); | 
|  | 63 | return result; | 
|  | 64 | } | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 65 | return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 68 | Result<void> Epoll::UnregisterHandler(int fd) { | 
| Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 69 | if (epoll_ctl(epoll_fd_.get(), EPOLL_CTL_DEL, fd, nullptr) == -1) { | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 70 | return ErrnoError() << "epoll_ctl failed to remove fd"; | 
|  | 71 | } | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 72 | auto it = epoll_handlers_.find(fd); | 
|  | 73 | if (it == epoll_handlers_.end()) { | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 74 | return Error() << "Attempting to remove epoll handler for FD without an existing handler"; | 
|  | 75 | } | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 76 | to_remove_.insert(it->first); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 77 | return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Bart Van Assche | a2c1604 | 2022-10-14 09:35:35 -0700 | [diff] [blame] | 80 | void Epoll::SetFirstCallback(std::function<void()> first_callback) { | 
|  | 81 | first_callback_ = std::move(first_callback); | 
|  | 82 | } | 
|  | 83 |  | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 84 | Result<int> Epoll::Wait(std::optional<std::chrono::milliseconds> timeout) { | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 85 | int timeout_ms = -1; | 
|  | 86 | if (timeout && timeout->count() < INT_MAX) { | 
|  | 87 | timeout_ms = timeout->count(); | 
|  | 88 | } | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 89 | const auto max_events = epoll_handlers_.size(); | 
|  | 90 | epoll_event ev[max_events]; | 
| Bart Van Assche | aee2ec8 | 2022-12-02 18:48:15 -0800 | [diff] [blame] | 91 | auto num_events = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd_.get(), ev, max_events, timeout_ms)); | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 92 | if (num_events == -1) { | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 93 | return ErrnoError() << "epoll_wait failed"; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 94 | } | 
| Bart Van Assche | a2c1604 | 2022-10-14 09:35:35 -0700 | [diff] [blame] | 95 | if (num_events > 0 && first_callback_) { | 
|  | 96 | first_callback_(); | 
|  | 97 | } | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 98 | for (int i = 0; i < num_events; ++i) { | 
| Bart Van Assche | b0177a0 | 2022-10-18 15:48:31 -0700 | [diff] [blame] | 99 | const auto it = epoll_handlers_.find(ev[i].data.fd); | 
|  | 100 | if (it == epoll_handlers_.end()) { | 
|  | 101 | continue; | 
|  | 102 | } | 
|  | 103 | const Info& info = it->second; | 
| David Anderson | 0fa7c40 | 2022-03-07 19:10:57 -0800 | [diff] [blame] | 104 | if ((info.events & (EPOLLIN | EPOLLPRI)) == (EPOLLIN | EPOLLPRI) && | 
|  | 105 | (ev[i].events & EPOLLIN) != ev[i].events) { | 
|  | 106 | // This handler wants to know about exception events, and just got one. | 
|  | 107 | // Log something informational. | 
|  | 108 | LOG(ERROR) << "Received unexpected epoll event set: " << ev[i].events; | 
|  | 109 | } | 
| Bart Van Assche | dcd23df | 2022-10-14 12:55:35 -0700 | [diff] [blame] | 110 | info.handler(); | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 111 | for (auto fd : to_remove_) { | 
|  | 112 | epoll_handlers_.erase(fd); | 
|  | 113 | } | 
|  | 114 | to_remove_.clear(); | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 115 | } | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 116 | return num_events; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | }  // namespace init | 
|  | 120 | }  // namespace android |