blob: 9776c1bec8c7d8f9fe75f9dc9630892db91146b0 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c
2**
3** Copyright 2006, Brian Swetland <swetland@frotz.net>
4**
Elliott Hughesaa245492015-08-03 10:38:08 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008**
Elliott Hughesaa245492015-08-03 10:38:08 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010**
Elliott Hughesaa245492015-08-03 10:38:08 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015** limitations under the License.
16*/
17
Yabin Cuiaed3c612015-09-22 15:52:57 -070018#define TRACE_TAG FDEVENT
JP Abgrall408fa572011-03-16 15:57:42 -070019
Dan Albert33134262015-03-19 15:21:08 -070020#include "sysdeps.h"
21#include "fdevent.h"
22
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <fcntl.h>
Josh Gao5791e212018-03-16 14:25:42 -070024#include <stdint.h>
Dan Albert33134262015-03-19 15:21:08 -070025#include <stdlib.h>
26#include <string.h>
Dan Albert33134262015-03-19 15:21:08 -070027#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Josh Gao022d4472016-02-10 14:49:00 -080029#include <atomic>
Josh Gaoe39ccd32018-02-23 14:37:07 -080030#include <deque>
Josh Gao4c936392017-05-03 14:10:39 -070031#include <functional>
Yabin Cuia1080162015-09-04 16:19:56 -070032#include <list>
Josh Gao4c936392017-05-03 14:10:39 -070033#include <mutex>
Yabin Cuia1080162015-09-04 16:19:56 -070034#include <unordered_map>
35#include <vector>
36
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/logging.h>
38#include <android-base/stringprintf.h>
Josh Gao4c936392017-05-03 14:10:39 -070039#include <android-base/thread_annotations.h>
Josh Gao5791e212018-03-16 14:25:42 -070040#include <android-base/threads.h>
Yabin Cuia1080162015-09-04 16:19:56 -070041
Dan Albertcc731cc2015-02-24 21:26:58 -080042#include "adb_io.h"
leozwangd3fc15f2014-07-29 12:50:02 -070043#include "adb_trace.h"
Josh Gao4c936392017-05-03 14:10:39 -070044#include "adb_unique_fd.h"
Yabin Cui6dfef252015-10-06 15:10:05 -070045#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#define FDE_EVENTMASK 0x00ff
48#define FDE_STATEMASK 0xff00
49
50#define FDE_ACTIVE 0x0100
51#define FDE_PENDING 0x0200
52#define FDE_CREATED 0x0400
53
Yabin Cuia1080162015-09-04 16:19:56 -070054struct PollNode {
55 fdevent* fde;
Josh Gao3777d2e2016-02-16 17:34:53 -080056 adb_pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070058 explicit PollNode(fdevent* fde) : fde(fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070059 memset(&pollfd, 0, sizeof(pollfd));
60 pollfd.fd = fde->fd;
Yabin Cuiaa77e222015-09-29 12:25:33 -070061
62#if defined(__linux__)
63 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
64 // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
65 pollfd.events = POLLRDHUP;
66#endif
Yabin Cuia1080162015-09-04 16:19:56 -070067 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068};
69
Yabin Cuia1080162015-09-04 16:19:56 -070070// All operations to fdevent should happen only in the main thread.
71// That's why we don't need a lock for fdevent.
Josh Gaob7b1edf2015-11-11 17:56:12 -080072static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
73static auto& g_pending_list = *new std::list<fdevent*>();
Josh Gao022d4472016-02-10 14:49:00 -080074static std::atomic<bool> terminate_loop(false);
Yabin Cuifbfa8402015-10-30 18:37:26 -070075static bool main_thread_valid;
Josh Gao5791e212018-03-16 14:25:42 -070076static uint64_t main_thread_id;
Yabin Cuifbfa8402015-10-30 18:37:26 -070077
Josh Gao4c936392017-05-03 14:10:39 -070078static auto& run_queue_notify_fd = *new unique_fd();
79static auto& run_queue_mutex = *new std::mutex();
Josh Gaoe39ccd32018-02-23 14:37:07 -080080static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
Josh Gao4c936392017-05-03 14:10:39 -070081
Yabin Cuib5e11412017-03-10 16:01:01 -080082void check_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070083 if (main_thread_valid) {
Josh Gao5791e212018-03-16 14:25:42 -070084 CHECK_EQ(main_thread_id, android::base::GetThreadId());
Yabin Cuifbfa8402015-10-30 18:37:26 -070085 }
86}
87
Yabin Cuib5e11412017-03-10 16:01:01 -080088void set_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070089 main_thread_valid = true;
Josh Gao5791e212018-03-16 14:25:42 -070090 main_thread_id = android::base::GetThreadId();
Yabin Cuifbfa8402015-10-30 18:37:26 -070091}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
Yabin Cuia1080162015-09-04 16:19:56 -070093static std::string dump_fde(const fdevent* fde) {
94 std::string state;
95 if (fde->state & FDE_ACTIVE) {
96 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097 }
Yabin Cuia1080162015-09-04 16:19:56 -070098 if (fde->state & FDE_PENDING) {
99 state += "P";
100 }
101 if (fde->state & FDE_CREATED) {
102 state += "C";
103 }
104 if (fde->state & FDE_READ) {
105 state += "R";
106 }
107 if (fde->state & FDE_WRITE) {
108 state += "W";
109 }
110 if (fde->state & FDE_ERROR) {
111 state += "E";
112 }
113 if (fde->state & FDE_DONT_CLOSE) {
114 state += "D";
115 }
116 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117}
118
Josh Gao4c936392017-05-03 14:10:39 -0700119fdevent* fdevent_create(int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700120 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
122 if(fde == 0) return 0;
123 fdevent_install(fde, fd, func, arg);
124 fde->state |= FDE_CREATED;
125 return fde;
126}
127
Josh Gao4c936392017-05-03 14:10:39 -0700128void fdevent_destroy(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700129 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 if(fde == 0) return;
131 if(!(fde->state & FDE_CREATED)) {
Yabin Cuia1080162015-09-04 16:19:56 -0700132 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 }
134 fdevent_remove(fde);
SungHyun Kwonabb80e02015-03-03 13:56:42 +0900135 free(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136}
137
Yabin Cuia1080162015-09-04 16:19:56 -0700138void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700139 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700140 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 memset(fde, 0, sizeof(fdevent));
142 fde->state = FDE_ACTIVE;
143 fde->fd = fd;
144 fde->func = func;
145 fde->arg = arg;
Yabin Cui6dfef252015-10-06 15:10:05 -0700146 if (!set_file_block_mode(fd, false)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700147 // Here is not proper to handle the error. If it fails here, some error is
148 // likely to be detected by poll(), then we can let the callback function
149 // to handle it.
Yabin Cui6dfef252015-10-06 15:10:05 -0700150 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
Yabin Cuia1080162015-09-04 16:19:56 -0700151 }
152 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
153 CHECK(pair.second) << "install existing fd " << fd;
154 D("fdevent_install %s", dump_fde(fde).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155}
156
Yabin Cuia1080162015-09-04 16:19:56 -0700157void fdevent_remove(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700158 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700159 D("fdevent_remove %s", dump_fde(fde).c_str());
160 if (fde->state & FDE_ACTIVE) {
161 g_poll_node_map.erase(fde->fd);
162 if (fde->state & FDE_PENDING) {
163 g_pending_list.remove(fde);
164 }
165 if (!(fde->state & FDE_DONT_CLOSE)) {
166 adb_close(fde->fd);
167 fde->fd = -1;
168 }
169 fde->state = 0;
170 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172}
173
Yabin Cuia1080162015-09-04 16:19:56 -0700174static void fdevent_update(fdevent* fde, unsigned events) {
175 auto it = g_poll_node_map.find(fde->fd);
176 CHECK(it != g_poll_node_map.end());
177 PollNode& node = it->second;
178 if (events & FDE_READ) {
179 node.pollfd.events |= POLLIN;
180 } else {
181 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 }
183
Yabin Cuia1080162015-09-04 16:19:56 -0700184 if (events & FDE_WRITE) {
185 node.pollfd.events |= POLLOUT;
186 } else {
187 node.pollfd.events &= ~POLLOUT;
188 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Yabin Cuia1080162015-09-04 16:19:56 -0700192void fdevent_set(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700193 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700194 events &= FDE_EVENTMASK;
195 if ((fde->state & FDE_EVENTMASK) == events) {
196 return;
197 }
Spencer Low888a7482015-09-29 18:33:38 -0700198 CHECK(fde->state & FDE_ACTIVE);
199 fdevent_update(fde, events);
200 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700201
Spencer Low888a7482015-09-29 18:33:38 -0700202 if (fde->state & FDE_PENDING) {
203 // If we are pending, make sure we don't signal an event that is no longer wanted.
204 fde->events &= events;
205 if (fde->events == 0) {
206 g_pending_list.remove(fde);
207 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 }
209 }
210}
211
Yabin Cuia1080162015-09-04 16:19:56 -0700212void fdevent_add(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700213 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700214 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215}
216
Yabin Cuia1080162015-09-04 16:19:56 -0700217void fdevent_del(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700218 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700219 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220}
221
Josh Gao3777d2e2016-02-16 17:34:53 -0800222static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700223 std::string result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700224 for (const auto& pollfd : pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700225 std::string op;
226 if (pollfd.events & POLLIN) {
227 op += "R";
228 }
229 if (pollfd.events & POLLOUT) {
230 op += "W";
231 }
232 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
233 }
234 return result;
235}
236
237static void fdevent_process() {
Josh Gao3777d2e2016-02-16 17:34:53 -0800238 std::vector<adb_pollfd> pollfds;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700239 for (const auto& pair : g_poll_node_map) {
240 pollfds.push_back(pair.second.pollfd);
Yabin Cuia1080162015-09-04 16:19:56 -0700241 }
242 CHECK_GT(pollfds.size(), 0u);
243 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
Josh Gao3777d2e2016-02-16 17:34:53 -0800244 int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
Yabin Cuia1080162015-09-04 16:19:56 -0700245 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700246 PLOG(ERROR) << "poll(), ret = " << ret;
247 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700248 }
Elliott Hughes65fe2512015-10-07 15:59:35 -0700249 for (const auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700250 if (pollfd.revents != 0) {
251 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
252 }
Yabin Cuia1080162015-09-04 16:19:56 -0700253 unsigned events = 0;
254 if (pollfd.revents & POLLIN) {
255 events |= FDE_READ;
256 }
257 if (pollfd.revents & POLLOUT) {
258 events |= FDE_WRITE;
259 }
260 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
261 // We fake a read, as the rest of the code assumes that errors will
262 // be detected at that point.
263 events |= FDE_READ | FDE_ERROR;
264 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700265#if defined(__linux__)
266 if (pollfd.revents & POLLRDHUP) {
267 events |= FDE_READ | FDE_ERROR;
268 }
269#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700270 if (events != 0) {
271 auto it = g_poll_node_map.find(pollfd.fd);
272 CHECK(it != g_poll_node_map.end());
273 fdevent* fde = it->second.fde;
274 CHECK_EQ(fde->fd, pollfd.fd);
275 fde->events |= events;
276 D("%s got events %x", dump_fde(fde).c_str(), events);
277 fde->state |= FDE_PENDING;
278 g_pending_list.push_back(fde);
279 }
280 }
281}
282
Josh Gao4c936392017-05-03 14:10:39 -0700283static void fdevent_call_fdfunc(fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -0700284 unsigned events = fde->events;
285 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700286 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700287 fde->state &= (~FDE_PENDING);
288 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
289 fde->func(fde->fd, events, fde->arg);
290}
291
Josh Gaoe39ccd32018-02-23 14:37:07 -0800292static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
293 // We need to be careful around reentrancy here, since a function we call can queue up another
294 // function.
295 while (true) {
296 std::function<void()> fn;
297 {
298 std::lock_guard<std::mutex> lock(run_queue_mutex);
299 if (run_queue.empty()) {
300 break;
301 }
302 fn = run_queue.front();
303 run_queue.pop_front();
304 }
305 fn();
Josh Gao4c936392017-05-03 14:10:39 -0700306 }
Josh Gao4c936392017-05-03 14:10:39 -0700307}
308
309static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
310 CHECK_GE(fd, 0);
311 CHECK(ev & FDE_READ);
312
313 char buf[1024];
314
315 // Empty the fd.
316 if (adb_read(fd, buf, sizeof(buf)) == -1) {
317 PLOG(FATAL) << "failed to empty run queue notify fd";
318 }
319
Josh Gao4c936392017-05-03 14:10:39 -0700320 fdevent_run_flush();
321}
322
323static void fdevent_run_setup() {
Josh Gaoe39ccd32018-02-23 14:37:07 -0800324 {
325 std::lock_guard<std::mutex> lock(run_queue_mutex);
326 CHECK(run_queue_notify_fd.get() == -1);
327 int s[2];
328 if (adb_socketpair(s) != 0) {
329 PLOG(FATAL) << "failed to create run queue notify socketpair";
330 }
Josh Gao4c936392017-05-03 14:10:39 -0700331
Josh Gao1222abc2018-03-19 15:19:45 -0700332 if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
333 PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
334 }
335
Josh Gaoe39ccd32018-02-23 14:37:07 -0800336 run_queue_notify_fd.reset(s[0]);
337 fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
338 CHECK(fde != nullptr);
339 fdevent_add(fde, FDE_READ);
340 }
Josh Gao4c936392017-05-03 14:10:39 -0700341
342 fdevent_run_flush();
343}
344
345void fdevent_run_on_main_thread(std::function<void()> fn) {
346 std::lock_guard<std::mutex> lock(run_queue_mutex);
347 run_queue.push_back(std::move(fn));
348
349 // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
350 // In that case, rely on the setup code to flush the queue without a notification being needed.
351 if (run_queue_notify_fd != -1) {
Josh Gao1222abc2018-03-19 15:19:45 -0700352 int rc = adb_write(run_queue_notify_fd.get(), "", 1);
353
354 // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
355 if (rc == 0) {
356 PLOG(FATAL) << "run queue notify fd was closed?";
357 } else if (rc == -1 && errno != EAGAIN) {
Josh Gao4c936392017-05-03 14:10:39 -0700358 PLOG(FATAL) << "failed to write to run queue notify fd";
359 }
360 }
361}
362
363void fdevent_loop() {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700364 set_main_thread();
Josh Gao4c936392017-05-03 14:10:39 -0700365 fdevent_run_setup();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200366
Elliott Hughesaa245492015-08-03 10:38:08 -0700367 while (true) {
Josh Gao022d4472016-02-10 14:49:00 -0800368 if (terminate_loop) {
369 return;
370 }
371
Yabin Cuia1080162015-09-04 16:19:56 -0700372 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700373
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200375
Yabin Cuia1080162015-09-04 16:19:56 -0700376 while (!g_pending_list.empty()) {
377 fdevent* fde = g_pending_list.front();
378 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700379 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 }
381 }
382}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700383
Josh Gao022d4472016-02-10 14:49:00 -0800384void fdevent_terminate_loop() {
385 terminate_loop = true;
386}
387
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700388size_t fdevent_installed_count() {
389 return g_poll_node_map.size();
390}
391
392void fdevent_reset() {
393 g_poll_node_map.clear();
394 g_pending_list.clear();
Josh Gao4c936392017-05-03 14:10:39 -0700395
396 std::lock_guard<std::mutex> lock(run_queue_mutex);
397 run_queue_notify_fd.reset();
398 run_queue.clear();
399
Yabin Cuifbfa8402015-10-30 18:37:26 -0700400 main_thread_valid = false;
Josh Gao022d4472016-02-10 14:49:00 -0800401 terminate_loop = false;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700402}