Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #define TRACE_TAG FDEVENT |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "fdevent_poll.h" |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <atomic> |
| 30 | #include <deque> |
| 31 | #include <functional> |
| 32 | #include <list> |
| 33 | #include <mutex> |
| 34 | #include <optional> |
| 35 | #include <unordered_map> |
| 36 | #include <utility> |
| 37 | #include <variant> |
| 38 | #include <vector> |
| 39 | |
| 40 | #include <android-base/chrono_utils.h> |
| 41 | #include <android-base/file.h> |
| 42 | #include <android-base/logging.h> |
| 43 | #include <android-base/stringprintf.h> |
| 44 | #include <android-base/threads.h> |
| 45 | |
| 46 | #include "adb_io.h" |
| 47 | #include "adb_trace.h" |
| 48 | #include "adb_unique_fd.h" |
| 49 | #include "adb_utils.h" |
| 50 | #include "fdevent.h" |
| 51 | #include "sysdeps/chrono.h" |
| 52 | |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 53 | static void fdevent_interrupt(int fd, unsigned, void*) { |
| 54 | char buf[BUFSIZ]; |
| 55 | ssize_t rc = TEMP_FAILURE_RETRY(adb_read(fd, buf, sizeof(buf))); |
| 56 | if (rc == -1) { |
| 57 | PLOG(FATAL) << "failed to read from fdevent interrupt fd"; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | fdevent_context_poll::fdevent_context_poll() { |
| 62 | int s[2]; |
| 63 | if (adb_socketpair(s) != 0) { |
| 64 | PLOG(FATAL) << "failed to create fdevent interrupt socketpair"; |
| 65 | } |
| 66 | |
| 67 | if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) { |
| 68 | PLOG(FATAL) << "failed to make fdevent interrupt socket nonblocking"; |
| 69 | } |
| 70 | |
| 71 | this->interrupt_fd_.reset(s[0]); |
| 72 | fdevent* fde = this->Create(unique_fd(s[1]), fdevent_interrupt, nullptr); |
| 73 | CHECK(fde != nullptr); |
| 74 | this->Add(fde, FDE_READ); |
| 75 | } |
| 76 | |
| 77 | fdevent_context_poll::~fdevent_context_poll() { |
Josh Gao | dd71642 | 2019-07-11 13:47:44 -0700 | [diff] [blame] | 78 | // Destroy calls virtual methods, but this class is final, so that's okay. |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 79 | this->Destroy(this->interrupt_fde_); |
| 80 | } |
| 81 | |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 82 | void fdevent_context_poll::Set(fdevent* fde, unsigned events) { |
| 83 | CheckMainThread(); |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 84 | fde->state = events; |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 85 | D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events); |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 88 | static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) { |
| 89 | std::string result; |
| 90 | for (const auto& pollfd : pollfds) { |
| 91 | std::string op; |
| 92 | if (pollfd.events & POLLIN) { |
| 93 | op += "R"; |
| 94 | } |
| 95 | if (pollfd.events & POLLOUT) { |
| 96 | op += "W"; |
| 97 | } |
| 98 | android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str()); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 103 | void fdevent_context_poll::Loop() { |
Josh Gao | 83e435a | 2019-07-08 18:05:16 -0700 | [diff] [blame] | 104 | main_thread_id_ = android::base::GetThreadId(); |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 105 | |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 106 | std::vector<adb_pollfd> pollfds; |
| 107 | std::vector<fdevent_event> poll_events; |
| 108 | |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 109 | while (true) { |
| 110 | if (terminate_loop_) { |
Josh Gao | 83e435a | 2019-07-08 18:05:16 -0700 | [diff] [blame] | 111 | break; |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | D("--- --- waiting for events"); |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 115 | pollfds.clear(); |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 116 | for (const auto& [fd, fde] : this->installed_fdevents_) { |
| 117 | adb_pollfd pfd; |
| 118 | pfd.fd = fd; |
| 119 | pfd.events = 0; |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 120 | if (fde.state & FDE_READ) { |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 121 | pfd.events |= POLLIN; |
| 122 | } |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 123 | if (fde.state & FDE_WRITE) { |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 124 | pfd.events |= POLLOUT; |
| 125 | } |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 126 | if (fde.state & FDE_ERROR) { |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 127 | pfd.events |= POLLERR; |
| 128 | } |
| 129 | #if defined(__linux__) |
| 130 | pfd.events |= POLLRDHUP; |
| 131 | #endif |
| 132 | pfd.revents = 0; |
| 133 | pollfds.push_back(pfd); |
| 134 | } |
| 135 | CHECK_GT(pollfds.size(), 0u); |
| 136 | D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str()); |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 137 | |
Josh Gao | dd71642 | 2019-07-11 13:47:44 -0700 | [diff] [blame] | 138 | std::optional<std::chrono::milliseconds> timeout = CalculatePollDuration(); |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 139 | int timeout_ms; |
| 140 | if (!timeout) { |
| 141 | timeout_ms = -1; |
| 142 | } else { |
| 143 | timeout_ms = timeout->count(); |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 146 | int ret = adb_poll(pollfds.data(), pollfds.size(), timeout_ms); |
| 147 | if (ret == -1) { |
| 148 | PLOG(ERROR) << "poll(), ret = " << ret; |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | auto post_poll = std::chrono::steady_clock::now(); |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 153 | |
| 154 | for (const auto& pollfd : pollfds) { |
| 155 | unsigned events = 0; |
| 156 | if (pollfd.revents & POLLIN) { |
| 157 | events |= FDE_READ; |
| 158 | } |
| 159 | if (pollfd.revents & POLLOUT) { |
| 160 | events |= FDE_WRITE; |
| 161 | } |
| 162 | if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
| 163 | // We fake a read, as the rest of the code assumes that errors will |
| 164 | // be detected at that point. |
| 165 | events |= FDE_READ | FDE_ERROR; |
| 166 | } |
| 167 | #if defined(__linux__) |
| 168 | if (pollfd.revents & POLLRDHUP) { |
| 169 | events |= FDE_READ | FDE_ERROR; |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | auto it = this->installed_fdevents_.find(pollfd.fd); |
| 174 | CHECK(it != this->installed_fdevents_.end()); |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 175 | fdevent* fde = &it->second; |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 176 | |
| 177 | if (events == 0) { |
| 178 | if (fde->timeout) { |
| 179 | auto deadline = fde->last_active + *fde->timeout; |
| 180 | if (deadline < post_poll) { |
| 181 | events |= FDE_TIMEOUT; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (events != 0) { |
| 187 | D("%s got events %x", dump_fde(fde).c_str(), events); |
| 188 | poll_events.push_back({fde, events}); |
| 189 | fde->last_active = post_poll; |
| 190 | } |
| 191 | } |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 192 | this->HandleEvents(poll_events); |
| 193 | poll_events.clear(); |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 194 | } |
Josh Gao | 83e435a | 2019-07-08 18:05:16 -0700 | [diff] [blame] | 195 | |
| 196 | main_thread_id_.reset(); |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 199 | size_t fdevent_context_poll::InstalledCount() { |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 200 | // We always have an installed fde for interrupt. |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 201 | return this->installed_fdevents_.size() - 1; |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void fdevent_context_poll::Interrupt() { |
| 205 | int rc = adb_write(this->interrupt_fd_, "", 1); |
| 206 | |
| 207 | // It's possible that we get EAGAIN here, if lots of notifications came in while handling. |
| 208 | if (rc == 0) { |
| 209 | PLOG(FATAL) << "fdevent interrupt fd was closed?"; |
| 210 | } else if (rc == -1 && errno != EAGAIN) { |
| 211 | PLOG(FATAL) << "failed to write to fdevent interrupt fd"; |
| 212 | } |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 213 | } |
Elliott Hughes | f24aebf | 2020-05-12 16:17:11 -0700 | [diff] [blame] | 214 | |
| 215 | void fdevent_context_poll::Register(fdevent*) {} |
| 216 | |
| 217 | void fdevent_context_poll::Unregister(fdevent*) {} |