blob: cc4a7a1516e65a2ce3b98e0ed4fe88180c402ce9 [file] [log] [blame]
Josh Gao9e4aa3a2019-07-08 15:23:17 -07001/*
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 Gaoc5e99e02019-07-08 17:37:23 -070053static 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
61fdevent_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
77fdevent_context_poll::~fdevent_context_poll() {
Josh Gaodd716422019-07-11 13:47:44 -070078 // Destroy calls virtual methods, but this class is final, so that's okay.
Josh Gaoc5e99e02019-07-08 17:37:23 -070079 this->Destroy(this->interrupt_fde_);
80}
81
Josh Gao9e4aa3a2019-07-08 15:23:17 -070082void fdevent_context_poll::Set(fdevent* fde, unsigned events) {
83 CheckMainThread();
Josh Gao4729f462019-07-11 16:35:37 -070084 fde->state = events;
Josh Gao9e4aa3a2019-07-08 15:23:17 -070085 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Josh Gao9e4aa3a2019-07-08 15:23:17 -070086}
87
Josh Gao9e4aa3a2019-07-08 15:23:17 -070088static 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 Gao9e4aa3a2019-07-08 15:23:17 -0700103void fdevent_context_poll::Loop() {
Josh Gao83e435a2019-07-08 18:05:16 -0700104 main_thread_id_ = android::base::GetThreadId();
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700105
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700106 while (true) {
107 if (terminate_loop_) {
Josh Gao83e435a2019-07-08 18:05:16 -0700108 break;
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700109 }
110
111 D("--- --- waiting for events");
Josh Gao4729f462019-07-11 16:35:37 -0700112 std::vector<adb_pollfd> pollfds;
113 for (const auto& [fd, fde] : this->installed_fdevents_) {
114 adb_pollfd pfd;
115 pfd.fd = fd;
116 pfd.events = 0;
117 if (fde->state & FDE_READ) {
118 pfd.events |= POLLIN;
119 }
120 if (fde->state & FDE_WRITE) {
121 pfd.events |= POLLOUT;
122 }
123 if (fde->state & FDE_ERROR) {
124 pfd.events |= POLLERR;
125 }
126#if defined(__linux__)
127 pfd.events |= POLLRDHUP;
128#endif
129 pfd.revents = 0;
130 pollfds.push_back(pfd);
131 }
132 CHECK_GT(pollfds.size(), 0u);
133 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700134
Josh Gaodd716422019-07-11 13:47:44 -0700135 std::optional<std::chrono::milliseconds> timeout = CalculatePollDuration();
Josh Gao4729f462019-07-11 16:35:37 -0700136 int timeout_ms;
137 if (!timeout) {
138 timeout_ms = -1;
139 } else {
140 timeout_ms = timeout->count();
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700141 }
142
Josh Gao4729f462019-07-11 16:35:37 -0700143 int ret = adb_poll(pollfds.data(), pollfds.size(), timeout_ms);
144 if (ret == -1) {
145 PLOG(ERROR) << "poll(), ret = " << ret;
146 return;
147 }
148
149 auto post_poll = std::chrono::steady_clock::now();
150 std::vector<fdevent_event> poll_events;
151
152 for (const auto& pollfd : pollfds) {
153 unsigned events = 0;
154 if (pollfd.revents & POLLIN) {
155 events |= FDE_READ;
156 }
157 if (pollfd.revents & POLLOUT) {
158 events |= FDE_WRITE;
159 }
160 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
161 // We fake a read, as the rest of the code assumes that errors will
162 // be detected at that point.
163 events |= FDE_READ | FDE_ERROR;
164 }
165#if defined(__linux__)
166 if (pollfd.revents & POLLRDHUP) {
167 events |= FDE_READ | FDE_ERROR;
168 }
169#endif
170
171 auto it = this->installed_fdevents_.find(pollfd.fd);
172 CHECK(it != this->installed_fdevents_.end());
173 fdevent* fde = it->second;
174
175 if (events == 0) {
176 if (fde->timeout) {
177 auto deadline = fde->last_active + *fde->timeout;
178 if (deadline < post_poll) {
179 events |= FDE_TIMEOUT;
180 }
181 }
182 }
183
184 if (events != 0) {
185 D("%s got events %x", dump_fde(fde).c_str(), events);
186 poll_events.push_back({fde, events});
187 fde->last_active = post_poll;
188 }
189 }
190 this->HandleEvents(std::move(poll_events));
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700191 }
Josh Gao83e435a2019-07-08 18:05:16 -0700192
193 main_thread_id_.reset();
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700194}
195
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700196size_t fdevent_context_poll::InstalledCount() {
Josh Gaoc5e99e02019-07-08 17:37:23 -0700197 // We always have an installed fde for interrupt.
Josh Gao4729f462019-07-11 16:35:37 -0700198 return this->installed_fdevents_.size() - 1;
Josh Gaoc5e99e02019-07-08 17:37:23 -0700199}
200
201void fdevent_context_poll::Interrupt() {
202 int rc = adb_write(this->interrupt_fd_, "", 1);
203
204 // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
205 if (rc == 0) {
206 PLOG(FATAL) << "fdevent interrupt fd was closed?";
207 } else if (rc == -1 && errno != EAGAIN) {
208 PLOG(FATAL) << "failed to write to fdevent interrupt fd";
209 }
Josh Gao9e4aa3a2019-07-08 15:23:17 -0700210}