blob: f3b8ef5ca0e5b2253f26cc6420c784ff010c2cd5 [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
Yabin Cuia1080162015-09-04 16:19:56 -070047#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -070048// This socket is used when a subproc shell service exists.
49// It wakes up the fdevent_loop() and cause the correct handling
50// of the shell's pseudo-tty master. I.e. force close it.
51int SHELL_EXIT_NOTIFY_FD = -1;
Alex Vallée947cb3e2015-07-17 15:30:59 -040052#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054#define FDE_EVENTMASK 0x00ff
55#define FDE_STATEMASK 0xff00
56
57#define FDE_ACTIVE 0x0100
58#define FDE_PENDING 0x0200
59#define FDE_CREATED 0x0400
60
Yabin Cuia1080162015-09-04 16:19:56 -070061struct PollNode {
62 fdevent* fde;
Josh Gao3777d2e2016-02-16 17:34:53 -080063 adb_pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070065 explicit PollNode(fdevent* fde) : fde(fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070066 memset(&pollfd, 0, sizeof(pollfd));
67 pollfd.fd = fde->fd;
Yabin Cuiaa77e222015-09-29 12:25:33 -070068
69#if defined(__linux__)
70 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
71 // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
72 pollfd.events = POLLRDHUP;
73#endif
Yabin Cuia1080162015-09-04 16:19:56 -070074 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075};
76
Yabin Cuia1080162015-09-04 16:19:56 -070077// All operations to fdevent should happen only in the main thread.
78// That's why we don't need a lock for fdevent.
Josh Gaob7b1edf2015-11-11 17:56:12 -080079static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
80static auto& g_pending_list = *new std::list<fdevent*>();
Josh Gao022d4472016-02-10 14:49:00 -080081static std::atomic<bool> terminate_loop(false);
Yabin Cuifbfa8402015-10-30 18:37:26 -070082static bool main_thread_valid;
Josh Gao5791e212018-03-16 14:25:42 -070083static uint64_t main_thread_id;
Yabin Cuifbfa8402015-10-30 18:37:26 -070084
Josh Gao4c936392017-05-03 14:10:39 -070085static auto& run_queue_notify_fd = *new unique_fd();
86static auto& run_queue_mutex = *new std::mutex();
Josh Gaoe39ccd32018-02-23 14:37:07 -080087static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
Josh Gao4c936392017-05-03 14:10:39 -070088
Yabin Cuib5e11412017-03-10 16:01:01 -080089void check_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070090 if (main_thread_valid) {
Josh Gao5791e212018-03-16 14:25:42 -070091 CHECK_EQ(main_thread_id, android::base::GetThreadId());
Yabin Cuifbfa8402015-10-30 18:37:26 -070092 }
93}
94
Yabin Cuib5e11412017-03-10 16:01:01 -080095void set_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070096 main_thread_valid = true;
Josh Gao5791e212018-03-16 14:25:42 -070097 main_thread_id = android::base::GetThreadId();
Yabin Cuifbfa8402015-10-30 18:37:26 -070098}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Yabin Cuia1080162015-09-04 16:19:56 -0700100static std::string dump_fde(const fdevent* fde) {
101 std::string state;
102 if (fde->state & FDE_ACTIVE) {
103 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 }
Yabin Cuia1080162015-09-04 16:19:56 -0700105 if (fde->state & FDE_PENDING) {
106 state += "P";
107 }
108 if (fde->state & FDE_CREATED) {
109 state += "C";
110 }
111 if (fde->state & FDE_READ) {
112 state += "R";
113 }
114 if (fde->state & FDE_WRITE) {
115 state += "W";
116 }
117 if (fde->state & FDE_ERROR) {
118 state += "E";
119 }
120 if (fde->state & FDE_DONT_CLOSE) {
121 state += "D";
122 }
123 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124}
125
Josh Gao4c936392017-05-03 14:10:39 -0700126fdevent* fdevent_create(int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700127 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
129 if(fde == 0) return 0;
130 fdevent_install(fde, fd, func, arg);
131 fde->state |= FDE_CREATED;
132 return fde;
133}
134
Josh Gao4c936392017-05-03 14:10:39 -0700135void fdevent_destroy(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700136 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 if(fde == 0) return;
138 if(!(fde->state & FDE_CREATED)) {
Yabin Cuia1080162015-09-04 16:19:56 -0700139 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 }
141 fdevent_remove(fde);
SungHyun Kwonabb80e02015-03-03 13:56:42 +0900142 free(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143}
144
Yabin Cuia1080162015-09-04 16:19:56 -0700145void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700146 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700147 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 memset(fde, 0, sizeof(fdevent));
149 fde->state = FDE_ACTIVE;
150 fde->fd = fd;
151 fde->func = func;
152 fde->arg = arg;
Yabin Cui6dfef252015-10-06 15:10:05 -0700153 if (!set_file_block_mode(fd, false)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700154 // Here is not proper to handle the error. If it fails here, some error is
155 // likely to be detected by poll(), then we can let the callback function
156 // to handle it.
Yabin Cui6dfef252015-10-06 15:10:05 -0700157 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
Yabin Cuia1080162015-09-04 16:19:56 -0700158 }
159 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
160 CHECK(pair.second) << "install existing fd " << fd;
161 D("fdevent_install %s", dump_fde(fde).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162}
163
Yabin Cuia1080162015-09-04 16:19:56 -0700164void fdevent_remove(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700165 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700166 D("fdevent_remove %s", dump_fde(fde).c_str());
167 if (fde->state & FDE_ACTIVE) {
168 g_poll_node_map.erase(fde->fd);
169 if (fde->state & FDE_PENDING) {
170 g_pending_list.remove(fde);
171 }
172 if (!(fde->state & FDE_DONT_CLOSE)) {
173 adb_close(fde->fd);
174 fde->fd = -1;
175 }
176 fde->state = 0;
177 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179}
180
Yabin Cuia1080162015-09-04 16:19:56 -0700181static void fdevent_update(fdevent* fde, unsigned events) {
182 auto it = g_poll_node_map.find(fde->fd);
183 CHECK(it != g_poll_node_map.end());
184 PollNode& node = it->second;
185 if (events & FDE_READ) {
186 node.pollfd.events |= POLLIN;
187 } else {
188 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 }
190
Yabin Cuia1080162015-09-04 16:19:56 -0700191 if (events & FDE_WRITE) {
192 node.pollfd.events |= POLLOUT;
193 } else {
194 node.pollfd.events &= ~POLLOUT;
195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700197}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198
Yabin Cuia1080162015-09-04 16:19:56 -0700199void fdevent_set(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700200 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700201 events &= FDE_EVENTMASK;
202 if ((fde->state & FDE_EVENTMASK) == events) {
203 return;
204 }
Spencer Low888a7482015-09-29 18:33:38 -0700205 CHECK(fde->state & FDE_ACTIVE);
206 fdevent_update(fde, events);
207 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700208
Spencer Low888a7482015-09-29 18:33:38 -0700209 if (fde->state & FDE_PENDING) {
210 // If we are pending, make sure we don't signal an event that is no longer wanted.
211 fde->events &= events;
212 if (fde->events == 0) {
213 g_pending_list.remove(fde);
214 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 }
216 }
217}
218
Yabin Cuia1080162015-09-04 16:19:56 -0700219void fdevent_add(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700220 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700221 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222}
223
Yabin Cuia1080162015-09-04 16:19:56 -0700224void fdevent_del(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700225 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700226 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227}
228
Josh Gao3777d2e2016-02-16 17:34:53 -0800229static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700230 std::string result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700231 for (const auto& pollfd : pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700232 std::string op;
233 if (pollfd.events & POLLIN) {
234 op += "R";
235 }
236 if (pollfd.events & POLLOUT) {
237 op += "W";
238 }
239 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
240 }
241 return result;
242}
243
244static void fdevent_process() {
Josh Gao3777d2e2016-02-16 17:34:53 -0800245 std::vector<adb_pollfd> pollfds;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700246 for (const auto& pair : g_poll_node_map) {
247 pollfds.push_back(pair.second.pollfd);
Yabin Cuia1080162015-09-04 16:19:56 -0700248 }
249 CHECK_GT(pollfds.size(), 0u);
250 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
Josh Gao3777d2e2016-02-16 17:34:53 -0800251 int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
Yabin Cuia1080162015-09-04 16:19:56 -0700252 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700253 PLOG(ERROR) << "poll(), ret = " << ret;
254 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700255 }
Elliott Hughes65fe2512015-10-07 15:59:35 -0700256 for (const auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700257 if (pollfd.revents != 0) {
258 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
259 }
Yabin Cuia1080162015-09-04 16:19:56 -0700260 unsigned events = 0;
261 if (pollfd.revents & POLLIN) {
262 events |= FDE_READ;
263 }
264 if (pollfd.revents & POLLOUT) {
265 events |= FDE_WRITE;
266 }
267 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
268 // We fake a read, as the rest of the code assumes that errors will
269 // be detected at that point.
270 events |= FDE_READ | FDE_ERROR;
271 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700272#if defined(__linux__)
273 if (pollfd.revents & POLLRDHUP) {
274 events |= FDE_READ | FDE_ERROR;
275 }
276#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700277 if (events != 0) {
278 auto it = g_poll_node_map.find(pollfd.fd);
279 CHECK(it != g_poll_node_map.end());
280 fdevent* fde = it->second.fde;
281 CHECK_EQ(fde->fd, pollfd.fd);
282 fde->events |= events;
283 D("%s got events %x", dump_fde(fde).c_str(), events);
284 fde->state |= FDE_PENDING;
285 g_pending_list.push_back(fde);
286 }
287 }
288}
289
Josh Gao4c936392017-05-03 14:10:39 -0700290static void fdevent_call_fdfunc(fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -0700291 unsigned events = fde->events;
292 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700293 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700294 fde->state &= (~FDE_PENDING);
295 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
296 fde->func(fde->fd, events, fde->arg);
297}
298
299#if !ADB_HOST
Josh Gao3777d2e2016-02-16 17:34:53 -0800300
301#include <sys/ioctl.h>
302
Josh Gao4c936392017-05-03 14:10:39 -0700303static void fdevent_subproc_event_func(int fd, unsigned ev, void* /* userdata */) {
Yabin Cuia1080162015-09-04 16:19:56 -0700304 D("subproc handling on fd = %d, ev = %x", fd, ev);
305
306 CHECK_GE(fd, 0);
307
308 if (ev & FDE_READ) {
309 int subproc_fd;
310
311 if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
312 LOG(FATAL) << "Failed to read the subproc's fd from " << fd;
313 }
314 auto it = g_poll_node_map.find(subproc_fd);
315 if (it == g_poll_node_map.end()) {
316 D("subproc_fd %d cleared from fd_table", subproc_fd);
Josh Gaoc65fae92016-01-19 16:21:17 -0800317 adb_close(subproc_fd);
Yabin Cuia1080162015-09-04 16:19:56 -0700318 return;
319 }
320 fdevent* subproc_fde = it->second.fde;
321 if(subproc_fde->fd != subproc_fd) {
322 // Already reallocated?
Josh Gaoc65fae92016-01-19 16:21:17 -0800323 LOG(FATAL) << "subproc_fd(" << subproc_fd << ") != subproc_fde->fd(" << subproc_fde->fd
324 << ")";
Yabin Cuia1080162015-09-04 16:19:56 -0700325 return;
326 }
327
328 subproc_fde->force_eof = 1;
329
330 int rcount = 0;
331 ioctl(subproc_fd, FIONREAD, &rcount);
332 D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno);
333 if (rcount != 0) {
334 // If there is data left, it will show up in the select().
335 // This works because there is no other thread reading that
336 // data when in this fd_func().
337 return;
338 }
339
340 D("subproc_fde %s", dump_fde(subproc_fde).c_str());
341 subproc_fde->events |= FDE_READ;
342 if(subproc_fde->state & FDE_PENDING) {
343 return;
344 }
345 subproc_fde->state |= FDE_PENDING;
346 fdevent_call_fdfunc(subproc_fde);
347 }
348}
349
Josh Gao4c936392017-05-03 14:10:39 -0700350static void fdevent_subproc_setup() {
Yabin Cuia1080162015-09-04 16:19:56 -0700351 int s[2];
352
353 if(adb_socketpair(s)) {
354 PLOG(FATAL) << "cannot create shell-exit socket-pair";
355 }
356 D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]);
357
358 SHELL_EXIT_NOTIFY_FD = s[0];
359 fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
360 CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler";
361 fdevent_add(fde, FDE_READ);
362}
363#endif // !ADB_HOST
364
Josh Gaoe39ccd32018-02-23 14:37:07 -0800365static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
366 // We need to be careful around reentrancy here, since a function we call can queue up another
367 // function.
368 while (true) {
369 std::function<void()> fn;
370 {
371 std::lock_guard<std::mutex> lock(run_queue_mutex);
372 if (run_queue.empty()) {
373 break;
374 }
375 fn = run_queue.front();
376 run_queue.pop_front();
377 }
378 fn();
Josh Gao4c936392017-05-03 14:10:39 -0700379 }
Josh Gao4c936392017-05-03 14:10:39 -0700380}
381
382static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
383 CHECK_GE(fd, 0);
384 CHECK(ev & FDE_READ);
385
386 char buf[1024];
387
388 // Empty the fd.
389 if (adb_read(fd, buf, sizeof(buf)) == -1) {
390 PLOG(FATAL) << "failed to empty run queue notify fd";
391 }
392
Josh Gao4c936392017-05-03 14:10:39 -0700393 fdevent_run_flush();
394}
395
396static void fdevent_run_setup() {
Josh Gaoe39ccd32018-02-23 14:37:07 -0800397 {
398 std::lock_guard<std::mutex> lock(run_queue_mutex);
399 CHECK(run_queue_notify_fd.get() == -1);
400 int s[2];
401 if (adb_socketpair(s) != 0) {
402 PLOG(FATAL) << "failed to create run queue notify socketpair";
403 }
Josh Gao4c936392017-05-03 14:10:39 -0700404
Josh Gao1222abc2018-03-19 15:19:45 -0700405 if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
406 PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
407 }
408
Josh Gaoe39ccd32018-02-23 14:37:07 -0800409 run_queue_notify_fd.reset(s[0]);
410 fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
411 CHECK(fde != nullptr);
412 fdevent_add(fde, FDE_READ);
413 }
Josh Gao4c936392017-05-03 14:10:39 -0700414
415 fdevent_run_flush();
416}
417
418void fdevent_run_on_main_thread(std::function<void()> fn) {
419 std::lock_guard<std::mutex> lock(run_queue_mutex);
420 run_queue.push_back(std::move(fn));
421
422 // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
423 // In that case, rely on the setup code to flush the queue without a notification being needed.
424 if (run_queue_notify_fd != -1) {
Josh Gao1222abc2018-03-19 15:19:45 -0700425 int rc = adb_write(run_queue_notify_fd.get(), "", 1);
426
427 // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
428 if (rc == 0) {
429 PLOG(FATAL) << "run queue notify fd was closed?";
430 } else if (rc == -1 && errno != EAGAIN) {
Josh Gao4c936392017-05-03 14:10:39 -0700431 PLOG(FATAL) << "failed to write to run queue notify fd";
432 }
433 }
434}
435
436void fdevent_loop() {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700437 set_main_thread();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400438#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700439 fdevent_subproc_setup();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400440#endif // !ADB_HOST
Josh Gao4c936392017-05-03 14:10:39 -0700441 fdevent_run_setup();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200442
Elliott Hughesaa245492015-08-03 10:38:08 -0700443 while (true) {
Josh Gao022d4472016-02-10 14:49:00 -0800444 if (terminate_loop) {
445 return;
446 }
447
Yabin Cuia1080162015-09-04 16:19:56 -0700448 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200451
Yabin Cuia1080162015-09-04 16:19:56 -0700452 while (!g_pending_list.empty()) {
453 fdevent* fde = g_pending_list.front();
454 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700455 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 }
457 }
458}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700459
Josh Gao022d4472016-02-10 14:49:00 -0800460void fdevent_terminate_loop() {
461 terminate_loop = true;
462}
463
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700464size_t fdevent_installed_count() {
465 return g_poll_node_map.size();
466}
467
468void fdevent_reset() {
469 g_poll_node_map.clear();
470 g_pending_list.clear();
Josh Gao4c936392017-05-03 14:10:39 -0700471
472 std::lock_guard<std::mutex> lock(run_queue_mutex);
473 run_queue_notify_fd.reset();
474 run_queue.clear();
475
Yabin Cuifbfa8402015-10-30 18:37:26 -0700476 main_thread_valid = false;
Josh Gao022d4472016-02-10 14:49:00 -0800477 terminate_loop = false;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700478}