blob: 4458c85ec9c387258129a7169775f6a57ba03a3d [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>
Yabin Cuia1080162015-09-04 16:19:56 -070024#include <poll.h>
Dan Albert33134262015-03-19 15:21:08 -070025#include <stdlib.h>
26#include <string.h>
27#include <sys/ioctl.h>
28#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Yabin Cuia1080162015-09-04 16:19:56 -070030#include <list>
31#include <unordered_map>
32#include <vector>
33
34#include <base/logging.h>
35#include <base/stringprintf.h>
36
Dan Albertcc731cc2015-02-24 21:26:58 -080037#include "adb_io.h"
leozwangd3fc15f2014-07-29 12:50:02 -070038#include "adb_trace.h"
Yabin Cui6dfef252015-10-06 15:10:05 -070039#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Yabin Cuia1080162015-09-04 16:19:56 -070041#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -070042// This socket is used when a subproc shell service exists.
43// It wakes up the fdevent_loop() and cause the correct handling
44// of the shell's pseudo-tty master. I.e. force close it.
45int SHELL_EXIT_NOTIFY_FD = -1;
Alex Vallée947cb3e2015-07-17 15:30:59 -040046#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#define FDE_EVENTMASK 0x00ff
49#define FDE_STATEMASK 0xff00
50
51#define FDE_ACTIVE 0x0100
52#define FDE_PENDING 0x0200
53#define FDE_CREATED 0x0400
54
Yabin Cuia1080162015-09-04 16:19:56 -070055struct PollNode {
56 fdevent* fde;
57 pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Yabin Cuia1080162015-09-04 16:19:56 -070059 PollNode(fdevent* fde) : fde(fde) {
60 memset(&pollfd, 0, sizeof(pollfd));
61 pollfd.fd = fde->fd;
Yabin Cuiaa77e222015-09-29 12:25:33 -070062
63#if defined(__linux__)
64 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
65 // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
66 pollfd.events = POLLRDHUP;
67#endif
Yabin Cuia1080162015-09-04 16:19:56 -070068 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069};
70
Yabin Cuia1080162015-09-04 16:19:56 -070071// All operations to fdevent should happen only in the main thread.
72// That's why we don't need a lock for fdevent.
73static std::unordered_map<int, PollNode> g_poll_node_map;
74static std::list<fdevent*> g_pending_list;
Yabin Cuifbfa8402015-10-30 18:37:26 -070075static bool main_thread_valid;
76static pthread_t main_thread;
77
78static void check_main_thread() {
79 if (main_thread_valid) {
80 CHECK_NE(0, pthread_equal(main_thread, pthread_self()));
81 }
82}
83
84static void set_main_thread() {
85 main_thread_valid = true;
86 main_thread = pthread_self();
87}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Yabin Cuia1080162015-09-04 16:19:56 -070089static std::string dump_fde(const fdevent* fde) {
90 std::string state;
91 if (fde->state & FDE_ACTIVE) {
92 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 }
Yabin Cuia1080162015-09-04 16:19:56 -070094 if (fde->state & FDE_PENDING) {
95 state += "P";
96 }
97 if (fde->state & FDE_CREATED) {
98 state += "C";
99 }
100 if (fde->state & FDE_READ) {
101 state += "R";
102 }
103 if (fde->state & FDE_WRITE) {
104 state += "W";
105 }
106 if (fde->state & FDE_ERROR) {
107 state += "E";
108 }
109 if (fde->state & FDE_DONT_CLOSE) {
110 state += "D";
111 }
112 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113}
114
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115fdevent *fdevent_create(int fd, fd_func func, void *arg)
116{
Yabin Cuifbfa8402015-10-30 18:37:26 -0700117 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
119 if(fde == 0) return 0;
120 fdevent_install(fde, fd, func, arg);
121 fde->state |= FDE_CREATED;
122 return fde;
123}
124
125void fdevent_destroy(fdevent *fde)
126{
Yabin Cuifbfa8402015-10-30 18:37:26 -0700127 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 if(fde == 0) return;
129 if(!(fde->state & FDE_CREATED)) {
Yabin Cuia1080162015-09-04 16:19:56 -0700130 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 }
132 fdevent_remove(fde);
SungHyun Kwonabb80e02015-03-03 13:56:42 +0900133 free(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134}
135
Yabin Cuia1080162015-09-04 16:19:56 -0700136void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700137 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700138 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 memset(fde, 0, sizeof(fdevent));
140 fde->state = FDE_ACTIVE;
141 fde->fd = fd;
142 fde->func = func;
143 fde->arg = arg;
Yabin Cui6dfef252015-10-06 15:10:05 -0700144 if (!set_file_block_mode(fd, false)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700145 // Here is not proper to handle the error. If it fails here, some error is
146 // likely to be detected by poll(), then we can let the callback function
147 // to handle it.
Yabin Cui6dfef252015-10-06 15:10:05 -0700148 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
Yabin Cuia1080162015-09-04 16:19:56 -0700149 }
150 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
151 CHECK(pair.second) << "install existing fd " << fd;
152 D("fdevent_install %s", dump_fde(fde).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153}
154
Yabin Cuia1080162015-09-04 16:19:56 -0700155void fdevent_remove(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700156 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700157 D("fdevent_remove %s", dump_fde(fde).c_str());
158 if (fde->state & FDE_ACTIVE) {
159 g_poll_node_map.erase(fde->fd);
160 if (fde->state & FDE_PENDING) {
161 g_pending_list.remove(fde);
162 }
163 if (!(fde->state & FDE_DONT_CLOSE)) {
164 adb_close(fde->fd);
165 fde->fd = -1;
166 }
167 fde->state = 0;
168 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170}
171
Yabin Cuia1080162015-09-04 16:19:56 -0700172static void fdevent_update(fdevent* fde, unsigned events) {
173 auto it = g_poll_node_map.find(fde->fd);
174 CHECK(it != g_poll_node_map.end());
175 PollNode& node = it->second;
176 if (events & FDE_READ) {
177 node.pollfd.events |= POLLIN;
178 } else {
179 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 }
181
Yabin Cuia1080162015-09-04 16:19:56 -0700182 if (events & FDE_WRITE) {
183 node.pollfd.events |= POLLOUT;
184 } else {
185 node.pollfd.events &= ~POLLOUT;
186 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Yabin Cuia1080162015-09-04 16:19:56 -0700190void fdevent_set(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700191 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700192 events &= FDE_EVENTMASK;
193 if ((fde->state & FDE_EVENTMASK) == events) {
194 return;
195 }
Spencer Low888a7482015-09-29 18:33:38 -0700196 CHECK(fde->state & FDE_ACTIVE);
197 fdevent_update(fde, events);
198 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700199
Spencer Low888a7482015-09-29 18:33:38 -0700200 if (fde->state & FDE_PENDING) {
201 // If we are pending, make sure we don't signal an event that is no longer wanted.
202 fde->events &= events;
203 if (fde->events == 0) {
204 g_pending_list.remove(fde);
205 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 }
207 }
208}
209
Yabin Cuia1080162015-09-04 16:19:56 -0700210void fdevent_add(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700211 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700212 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213}
214
Yabin Cuia1080162015-09-04 16:19:56 -0700215void fdevent_del(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700216 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700217 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218}
219
Yabin Cuia1080162015-09-04 16:19:56 -0700220static std::string dump_pollfds(const std::vector<pollfd>& pollfds) {
221 std::string result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700222 for (const auto& pollfd : pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700223 std::string op;
224 if (pollfd.events & POLLIN) {
225 op += "R";
226 }
227 if (pollfd.events & POLLOUT) {
228 op += "W";
229 }
230 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
231 }
232 return result;
233}
234
235static void fdevent_process() {
236 std::vector<pollfd> pollfds;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700237 for (const auto& pair : g_poll_node_map) {
238 pollfds.push_back(pair.second.pollfd);
Yabin Cuia1080162015-09-04 16:19:56 -0700239 }
240 CHECK_GT(pollfds.size(), 0u);
241 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
242 int ret = TEMP_FAILURE_RETRY(poll(&pollfds[0], pollfds.size(), -1));
243 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700244 PLOG(ERROR) << "poll(), ret = " << ret;
245 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700246 }
Elliott Hughes65fe2512015-10-07 15:59:35 -0700247 for (const auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700248 if (pollfd.revents != 0) {
249 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
250 }
Yabin Cuia1080162015-09-04 16:19:56 -0700251 unsigned events = 0;
252 if (pollfd.revents & POLLIN) {
253 events |= FDE_READ;
254 }
255 if (pollfd.revents & POLLOUT) {
256 events |= FDE_WRITE;
257 }
258 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
259 // We fake a read, as the rest of the code assumes that errors will
260 // be detected at that point.
261 events |= FDE_READ | FDE_ERROR;
262 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700263#if defined(__linux__)
264 if (pollfd.revents & POLLRDHUP) {
265 events |= FDE_READ | FDE_ERROR;
266 }
267#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700268 if (events != 0) {
269 auto it = g_poll_node_map.find(pollfd.fd);
270 CHECK(it != g_poll_node_map.end());
271 fdevent* fde = it->second.fde;
272 CHECK_EQ(fde->fd, pollfd.fd);
273 fde->events |= events;
274 D("%s got events %x", dump_fde(fde).c_str(), events);
275 fde->state |= FDE_PENDING;
276 g_pending_list.push_back(fde);
277 }
278 }
279}
280
281static void fdevent_call_fdfunc(fdevent* fde)
282{
283 unsigned events = fde->events;
284 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700285 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700286 fde->state &= (~FDE_PENDING);
287 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
288 fde->func(fde->fd, events, fde->arg);
289}
290
291#if !ADB_HOST
292static void fdevent_subproc_event_func(int fd, unsigned ev,
293 void* /* userdata */)
294{
295
296 D("subproc handling on fd = %d, ev = %x", fd, ev);
297
298 CHECK_GE(fd, 0);
299
300 if (ev & FDE_READ) {
301 int subproc_fd;
302
303 if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
304 LOG(FATAL) << "Failed to read the subproc's fd from " << fd;
305 }
306 auto it = g_poll_node_map.find(subproc_fd);
307 if (it == g_poll_node_map.end()) {
308 D("subproc_fd %d cleared from fd_table", subproc_fd);
309 return;
310 }
311 fdevent* subproc_fde = it->second.fde;
312 if(subproc_fde->fd != subproc_fd) {
313 // Already reallocated?
314 D("subproc_fd(%d) != subproc_fde->fd(%d)", subproc_fd, subproc_fde->fd);
315 return;
316 }
317
318 subproc_fde->force_eof = 1;
319
320 int rcount = 0;
321 ioctl(subproc_fd, FIONREAD, &rcount);
322 D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno);
323 if (rcount != 0) {
324 // If there is data left, it will show up in the select().
325 // This works because there is no other thread reading that
326 // data when in this fd_func().
327 return;
328 }
329
330 D("subproc_fde %s", dump_fde(subproc_fde).c_str());
331 subproc_fde->events |= FDE_READ;
332 if(subproc_fde->state & FDE_PENDING) {
333 return;
334 }
335 subproc_fde->state |= FDE_PENDING;
336 fdevent_call_fdfunc(subproc_fde);
337 }
338}
339
340void fdevent_subproc_setup()
341{
342 int s[2];
343
344 if(adb_socketpair(s)) {
345 PLOG(FATAL) << "cannot create shell-exit socket-pair";
346 }
347 D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]);
348
349 SHELL_EXIT_NOTIFY_FD = s[0];
350 fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
351 CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler";
352 fdevent_add(fde, FDE_READ);
353}
354#endif // !ADB_HOST
355
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356void fdevent_loop()
357{
Yabin Cuifbfa8402015-10-30 18:37:26 -0700358 set_main_thread();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400359#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700360 fdevent_subproc_setup();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400361#endif // !ADB_HOST
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200362
Elliott Hughesaa245492015-08-03 10:38:08 -0700363 while (true) {
Yabin Cuia1080162015-09-04 16:19:56 -0700364 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200367
Yabin Cuia1080162015-09-04 16:19:56 -0700368 while (!g_pending_list.empty()) {
369 fdevent* fde = g_pending_list.front();
370 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700371 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 }
373 }
374}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700375
376size_t fdevent_installed_count() {
377 return g_poll_node_map.size();
378}
379
380void fdevent_reset() {
381 g_poll_node_map.clear();
382 g_pending_list.clear();
Yabin Cuifbfa8402015-10-30 18:37:26 -0700383 main_thread_valid = false;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700384}