blob: 1ccee9be94232606d3ad12be1fa9a84d0e00f742 [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"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Yabin Cuia1080162015-09-04 16:19:56 -070040#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -070041// This socket is used when a subproc shell service exists.
42// It wakes up the fdevent_loop() and cause the correct handling
43// of the shell's pseudo-tty master. I.e. force close it.
44int SHELL_EXIT_NOTIFY_FD = -1;
Alex Vallée947cb3e2015-07-17 15:30:59 -040045#endif // !ADB_HOST
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;
56 pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Yabin Cuia1080162015-09-04 16:19:56 -070058 PollNode(fdevent* fde) : fde(fde) {
59 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.
72static std::unordered_map<int, PollNode> g_poll_node_map;
73static std::list<fdevent*> g_pending_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Yabin Cuia1080162015-09-04 16:19:56 -070075static std::string dump_fde(const fdevent* fde) {
76 std::string state;
77 if (fde->state & FDE_ACTIVE) {
78 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 }
Yabin Cuia1080162015-09-04 16:19:56 -070080 if (fde->state & FDE_PENDING) {
81 state += "P";
82 }
83 if (fde->state & FDE_CREATED) {
84 state += "C";
85 }
86 if (fde->state & FDE_READ) {
87 state += "R";
88 }
89 if (fde->state & FDE_WRITE) {
90 state += "W";
91 }
92 if (fde->state & FDE_ERROR) {
93 state += "E";
94 }
95 if (fde->state & FDE_DONT_CLOSE) {
96 state += "D";
97 }
98 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099}
100
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101fdevent *fdevent_create(int fd, fd_func func, void *arg)
102{
103 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
104 if(fde == 0) return 0;
105 fdevent_install(fde, fd, func, arg);
106 fde->state |= FDE_CREATED;
107 return fde;
108}
109
110void fdevent_destroy(fdevent *fde)
111{
112 if(fde == 0) return;
113 if(!(fde->state & FDE_CREATED)) {
Yabin Cuia1080162015-09-04 16:19:56 -0700114 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 }
116 fdevent_remove(fde);
SungHyun Kwonabb80e02015-03-03 13:56:42 +0900117 free(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118}
119
Yabin Cuia1080162015-09-04 16:19:56 -0700120void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
121 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 memset(fde, 0, sizeof(fdevent));
123 fde->state = FDE_ACTIVE;
124 fde->fd = fd;
125 fde->func = func;
126 fde->arg = arg;
Yabin Cuia1080162015-09-04 16:19:56 -0700127 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700128 // Here is not proper to handle the error. If it fails here, some error is
129 // likely to be detected by poll(), then we can let the callback function
130 // to handle it.
131 LOG(ERROR) << "failed to fcntl(" << fd << ") to be nonblock";
Yabin Cuia1080162015-09-04 16:19:56 -0700132 }
133 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
134 CHECK(pair.second) << "install existing fd " << fd;
135 D("fdevent_install %s", dump_fde(fde).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136}
137
Yabin Cuia1080162015-09-04 16:19:56 -0700138void fdevent_remove(fdevent* fde) {
139 D("fdevent_remove %s", dump_fde(fde).c_str());
140 if (fde->state & FDE_ACTIVE) {
141 g_poll_node_map.erase(fde->fd);
142 if (fde->state & FDE_PENDING) {
143 g_pending_list.remove(fde);
144 }
145 if (!(fde->state & FDE_DONT_CLOSE)) {
146 adb_close(fde->fd);
147 fde->fd = -1;
148 }
149 fde->state = 0;
150 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152}
153
Yabin Cuia1080162015-09-04 16:19:56 -0700154static void fdevent_update(fdevent* fde, unsigned events) {
155 auto it = g_poll_node_map.find(fde->fd);
156 CHECK(it != g_poll_node_map.end());
157 PollNode& node = it->second;
158 if (events & FDE_READ) {
159 node.pollfd.events |= POLLIN;
160 } else {
161 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 }
163
Yabin Cuia1080162015-09-04 16:19:56 -0700164 if (events & FDE_WRITE) {
165 node.pollfd.events |= POLLOUT;
166 } else {
167 node.pollfd.events &= ~POLLOUT;
168 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700170}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Yabin Cuia1080162015-09-04 16:19:56 -0700172void fdevent_set(fdevent* fde, unsigned events) {
173 events &= FDE_EVENTMASK;
174 if ((fde->state & FDE_EVENTMASK) == events) {
175 return;
176 }
Spencer Low888a7482015-09-29 18:33:38 -0700177 CHECK(fde->state & FDE_ACTIVE);
178 fdevent_update(fde, events);
179 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700180
Spencer Low888a7482015-09-29 18:33:38 -0700181 if (fde->state & FDE_PENDING) {
182 // If we are pending, make sure we don't signal an event that is no longer wanted.
183 fde->events &= events;
184 if (fde->events == 0) {
185 g_pending_list.remove(fde);
186 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 }
188 }
189}
190
Yabin Cuia1080162015-09-04 16:19:56 -0700191void fdevent_add(fdevent* fde, unsigned events) {
192 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193}
194
Yabin Cuia1080162015-09-04 16:19:56 -0700195void fdevent_del(fdevent* fde, unsigned events) {
196 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197}
198
Yabin Cuia1080162015-09-04 16:19:56 -0700199static std::string dump_pollfds(const std::vector<pollfd>& pollfds) {
200 std::string result;
201 for (auto& pollfd : pollfds) {
202 std::string op;
203 if (pollfd.events & POLLIN) {
204 op += "R";
205 }
206 if (pollfd.events & POLLOUT) {
207 op += "W";
208 }
209 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
210 }
211 return result;
212}
213
214static void fdevent_process() {
215 std::vector<pollfd> pollfds;
216 for (auto it = g_poll_node_map.begin(); it != g_poll_node_map.end(); ++it) {
217 pollfds.push_back(it->second.pollfd);
218 }
219 CHECK_GT(pollfds.size(), 0u);
220 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
221 int ret = TEMP_FAILURE_RETRY(poll(&pollfds[0], pollfds.size(), -1));
222 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700223 PLOG(ERROR) << "poll(), ret = " << ret;
224 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700225 }
226 for (auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700227 if (pollfd.revents != 0) {
228 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
229 }
Yabin Cuia1080162015-09-04 16:19:56 -0700230 unsigned events = 0;
231 if (pollfd.revents & POLLIN) {
232 events |= FDE_READ;
233 }
234 if (pollfd.revents & POLLOUT) {
235 events |= FDE_WRITE;
236 }
237 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
238 // We fake a read, as the rest of the code assumes that errors will
239 // be detected at that point.
240 events |= FDE_READ | FDE_ERROR;
241 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700242#if defined(__linux__)
243 if (pollfd.revents & POLLRDHUP) {
244 events |= FDE_READ | FDE_ERROR;
245 }
246#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700247 if (events != 0) {
248 auto it = g_poll_node_map.find(pollfd.fd);
249 CHECK(it != g_poll_node_map.end());
250 fdevent* fde = it->second.fde;
251 CHECK_EQ(fde->fd, pollfd.fd);
252 fde->events |= events;
253 D("%s got events %x", dump_fde(fde).c_str(), events);
254 fde->state |= FDE_PENDING;
255 g_pending_list.push_back(fde);
256 }
257 }
258}
259
260static void fdevent_call_fdfunc(fdevent* fde)
261{
262 unsigned events = fde->events;
263 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700264 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700265 fde->state &= (~FDE_PENDING);
266 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
267 fde->func(fde->fd, events, fde->arg);
268}
269
270#if !ADB_HOST
271static void fdevent_subproc_event_func(int fd, unsigned ev,
272 void* /* userdata */)
273{
274
275 D("subproc handling on fd = %d, ev = %x", fd, ev);
276
277 CHECK_GE(fd, 0);
278
279 if (ev & FDE_READ) {
280 int subproc_fd;
281
282 if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
283 LOG(FATAL) << "Failed to read the subproc's fd from " << fd;
284 }
285 auto it = g_poll_node_map.find(subproc_fd);
286 if (it == g_poll_node_map.end()) {
287 D("subproc_fd %d cleared from fd_table", subproc_fd);
288 return;
289 }
290 fdevent* subproc_fde = it->second.fde;
291 if(subproc_fde->fd != subproc_fd) {
292 // Already reallocated?
293 D("subproc_fd(%d) != subproc_fde->fd(%d)", subproc_fd, subproc_fde->fd);
294 return;
295 }
296
297 subproc_fde->force_eof = 1;
298
299 int rcount = 0;
300 ioctl(subproc_fd, FIONREAD, &rcount);
301 D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno);
302 if (rcount != 0) {
303 // If there is data left, it will show up in the select().
304 // This works because there is no other thread reading that
305 // data when in this fd_func().
306 return;
307 }
308
309 D("subproc_fde %s", dump_fde(subproc_fde).c_str());
310 subproc_fde->events |= FDE_READ;
311 if(subproc_fde->state & FDE_PENDING) {
312 return;
313 }
314 subproc_fde->state |= FDE_PENDING;
315 fdevent_call_fdfunc(subproc_fde);
316 }
317}
318
319void fdevent_subproc_setup()
320{
321 int s[2];
322
323 if(adb_socketpair(s)) {
324 PLOG(FATAL) << "cannot create shell-exit socket-pair";
325 }
326 D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]);
327
328 SHELL_EXIT_NOTIFY_FD = s[0];
329 fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
330 CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler";
331 fdevent_add(fde, FDE_READ);
332}
333#endif // !ADB_HOST
334
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335void fdevent_loop()
336{
Alex Vallée947cb3e2015-07-17 15:30:59 -0400337#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700338 fdevent_subproc_setup();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400339#endif // !ADB_HOST
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200340
Elliott Hughesaa245492015-08-03 10:38:08 -0700341 while (true) {
Yabin Cuia1080162015-09-04 16:19:56 -0700342 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700343
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200345
Yabin Cuia1080162015-09-04 16:19:56 -0700346 while (!g_pending_list.empty()) {
347 fdevent* fde = g_pending_list.front();
348 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700349 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 }
351 }
352}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700353
354size_t fdevent_installed_count() {
355 return g_poll_node_map.size();
356}
357
358void fdevent_reset() {
359 g_poll_node_map.clear();
360 g_pending_list.clear();
361}