blob: 666a15f8961d14e773fc2488626179e3a5be7a91 [file] [log] [blame]
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +02001/* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c
2**
3** Copyright 2006, Brian Swetland <swetland@frotz.net>
4**
Elliott Hughesb628cb12015-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
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +02008**
Elliott Hughesb628cb12015-08-03 10:38:08 -07009** http://www.apache.org/licenses/LICENSE-2.0
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020010**
Elliott Hughesb628cb12015-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
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020015** limitations under the License.
16*/
17
Dan Albertdb6fe642015-03-19 15:21:08 -070018#define TRACE_TAG TRACE_FDEVENT
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070019
Dan Albertdb6fe642015-03-19 15:21:08 -070020#include "sysdeps.h"
21#include "fdevent.h"
22
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020023#include <fcntl.h>
Yabin Cui56112542015-09-04 16:19:56 -070024#include <poll.h>
Dan Albertdb6fe642015-03-19 15:21:08 -070025#include <stdlib.h>
26#include <string.h>
27#include <sys/ioctl.h>
28#include <unistd.h>
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020029
Yabin Cui56112542015-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 Albert66a91b02015-02-24 21:26:58 -080037#include "adb_io.h"
leozwangf25a6a42014-07-29 12:50:02 -070038#include "adb_trace.h"
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020039
Yabin Cui56112542015-09-04 16:19:56 -070040#if !ADB_HOST
JP Abgrall2e5dd6e2011-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ée436fa6b2015-07-17 15:30:59 -040045#endif // !ADB_HOST
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020046
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020047#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 Cui56112542015-09-04 16:19:56 -070054struct PollNode {
55 fdevent* fde;
56 pollfd pollfd;
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020057
Yabin Cui56112542015-09-04 16:19:56 -070058 PollNode(fdevent* fde) : fde(fde) {
59 memset(&pollfd, 0, sizeof(pollfd));
60 pollfd.fd = fde->fd;
61 }
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020062};
63
Yabin Cui56112542015-09-04 16:19:56 -070064// All operations to fdevent should happen only in the main thread.
65// That's why we don't need a lock for fdevent.
66static std::unordered_map<int, PollNode> g_poll_node_map;
67static std::list<fdevent*> g_pending_list;
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020068
Yabin Cui56112542015-09-04 16:19:56 -070069static std::string dump_fde(const fdevent* fde) {
70 std::string state;
71 if (fde->state & FDE_ACTIVE) {
72 state += "A";
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020073 }
Yabin Cui56112542015-09-04 16:19:56 -070074 if (fde->state & FDE_PENDING) {
75 state += "P";
76 }
77 if (fde->state & FDE_CREATED) {
78 state += "C";
79 }
80 if (fde->state & FDE_READ) {
81 state += "R";
82 }
83 if (fde->state & FDE_WRITE) {
84 state += "W";
85 }
86 if (fde->state & FDE_ERROR) {
87 state += "E";
88 }
89 if (fde->state & FDE_DONT_CLOSE) {
90 state += "D";
91 }
92 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020093}
94
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020095fdevent *fdevent_create(int fd, fd_func func, void *arg)
96{
97 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
98 if(fde == 0) return 0;
99 fdevent_install(fde, fd, func, arg);
100 fde->state |= FDE_CREATED;
101 return fde;
102}
103
104void fdevent_destroy(fdevent *fde)
105{
106 if(fde == 0) return;
107 if(!(fde->state & FDE_CREATED)) {
Yabin Cui56112542015-09-04 16:19:56 -0700108 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200109 }
110 fdevent_remove(fde);
SungHyun Kwonb777b3b2015-03-03 13:56:42 +0900111 free(fde);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200112}
113
Yabin Cui56112542015-09-04 16:19:56 -0700114void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
115 CHECK_GE(fd, 0);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200116 memset(fde, 0, sizeof(fdevent));
117 fde->state = FDE_ACTIVE;
118 fde->fd = fd;
119 fde->func = func;
120 fde->arg = arg;
Yabin Cui56112542015-09-04 16:19:56 -0700121 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700122 // Here is not proper to handle the error. If it fails here, some error is
123 // likely to be detected by poll(), then we can let the callback function
124 // to handle it.
125 LOG(ERROR) << "failed to fcntl(" << fd << ") to be nonblock";
Yabin Cui56112542015-09-04 16:19:56 -0700126 }
127 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
128 CHECK(pair.second) << "install existing fd " << fd;
129 D("fdevent_install %s", dump_fde(fde).c_str());
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200130}
131
Yabin Cui56112542015-09-04 16:19:56 -0700132void fdevent_remove(fdevent* fde) {
133 D("fdevent_remove %s", dump_fde(fde).c_str());
134 if (fde->state & FDE_ACTIVE) {
135 g_poll_node_map.erase(fde->fd);
136 if (fde->state & FDE_PENDING) {
137 g_pending_list.remove(fde);
138 }
139 if (!(fde->state & FDE_DONT_CLOSE)) {
140 adb_close(fde->fd);
141 fde->fd = -1;
142 }
143 fde->state = 0;
144 fde->events = 0;
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200145 }
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200146}
147
Yabin Cui56112542015-09-04 16:19:56 -0700148static void fdevent_update(fdevent* fde, unsigned events) {
149 auto it = g_poll_node_map.find(fde->fd);
150 CHECK(it != g_poll_node_map.end());
151 PollNode& node = it->second;
152 if (events & FDE_READ) {
153 node.pollfd.events |= POLLIN;
154 } else {
155 node.pollfd.events &= ~POLLIN;
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200156 }
157
Yabin Cui56112542015-09-04 16:19:56 -0700158 if (events & FDE_WRITE) {
159 node.pollfd.events |= POLLOUT;
160 } else {
161 node.pollfd.events &= ~POLLOUT;
162 }
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200163 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cui56112542015-09-04 16:19:56 -0700164}
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200165
Yabin Cui56112542015-09-04 16:19:56 -0700166void fdevent_set(fdevent* fde, unsigned events) {
167 events &= FDE_EVENTMASK;
168 if ((fde->state & FDE_EVENTMASK) == events) {
169 return;
170 }
171 if (fde->state & FDE_ACTIVE) {
172 fdevent_update(fde, events);
173 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
174
175 if (fde->state & FDE_PENDING) {
176 // If we are pending, make sure we don't signal an event that is no longer wanted.
177 fde->events &= ~events;
178 if (fde->events == 0) {
179 g_pending_list.remove(fde);
180 fde->state &= ~FDE_PENDING;
181 }
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200182 }
183 }
184}
185
Yabin Cui56112542015-09-04 16:19:56 -0700186void fdevent_add(fdevent* fde, unsigned events) {
187 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200188}
189
Yabin Cui56112542015-09-04 16:19:56 -0700190void fdevent_del(fdevent* fde, unsigned events) {
191 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200192}
193
Yabin Cui56112542015-09-04 16:19:56 -0700194static std::string dump_pollfds(const std::vector<pollfd>& pollfds) {
195 std::string result;
196 for (auto& pollfd : pollfds) {
197 std::string op;
198 if (pollfd.events & POLLIN) {
199 op += "R";
200 }
201 if (pollfd.events & POLLOUT) {
202 op += "W";
203 }
204 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
205 }
206 return result;
207}
208
209static void fdevent_process() {
210 std::vector<pollfd> pollfds;
211 for (auto it = g_poll_node_map.begin(); it != g_poll_node_map.end(); ++it) {
212 pollfds.push_back(it->second.pollfd);
213 }
214 CHECK_GT(pollfds.size(), 0u);
215 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
216 int ret = TEMP_FAILURE_RETRY(poll(&pollfds[0], pollfds.size(), -1));
217 if (ret == -1) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700218 PLOG(ERROR) << "poll(), ret = " << ret;
219 return;
Yabin Cui56112542015-09-04 16:19:56 -0700220 }
221 for (auto& pollfd : pollfds) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700222 if (pollfd.revents != 0) {
223 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
224 }
Yabin Cui56112542015-09-04 16:19:56 -0700225 unsigned events = 0;
226 if (pollfd.revents & POLLIN) {
227 events |= FDE_READ;
228 }
229 if (pollfd.revents & POLLOUT) {
230 events |= FDE_WRITE;
231 }
232 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
233 // We fake a read, as the rest of the code assumes that errors will
234 // be detected at that point.
235 events |= FDE_READ | FDE_ERROR;
236 }
237 if (events != 0) {
238 auto it = g_poll_node_map.find(pollfd.fd);
239 CHECK(it != g_poll_node_map.end());
240 fdevent* fde = it->second.fde;
241 CHECK_EQ(fde->fd, pollfd.fd);
242 fde->events |= events;
243 D("%s got events %x", dump_fde(fde).c_str(), events);
244 fde->state |= FDE_PENDING;
245 g_pending_list.push_back(fde);
246 }
247 }
248}
249
250static void fdevent_call_fdfunc(fdevent* fde)
251{
252 unsigned events = fde->events;
253 fde->events = 0;
254 if(!(fde->state & FDE_PENDING)) return;
255 fde->state &= (~FDE_PENDING);
256 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
257 fde->func(fde->fd, events, fde->arg);
258}
259
260#if !ADB_HOST
261static void fdevent_subproc_event_func(int fd, unsigned ev,
262 void* /* userdata */)
263{
264
265 D("subproc handling on fd = %d, ev = %x", fd, ev);
266
267 CHECK_GE(fd, 0);
268
269 if (ev & FDE_READ) {
270 int subproc_fd;
271
272 if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
273 LOG(FATAL) << "Failed to read the subproc's fd from " << fd;
274 }
275 auto it = g_poll_node_map.find(subproc_fd);
276 if (it == g_poll_node_map.end()) {
277 D("subproc_fd %d cleared from fd_table", subproc_fd);
278 return;
279 }
280 fdevent* subproc_fde = it->second.fde;
281 if(subproc_fde->fd != subproc_fd) {
282 // Already reallocated?
283 D("subproc_fd(%d) != subproc_fde->fd(%d)", subproc_fd, subproc_fde->fd);
284 return;
285 }
286
287 subproc_fde->force_eof = 1;
288
289 int rcount = 0;
290 ioctl(subproc_fd, FIONREAD, &rcount);
291 D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno);
292 if (rcount != 0) {
293 // If there is data left, it will show up in the select().
294 // This works because there is no other thread reading that
295 // data when in this fd_func().
296 return;
297 }
298
299 D("subproc_fde %s", dump_fde(subproc_fde).c_str());
300 subproc_fde->events |= FDE_READ;
301 if(subproc_fde->state & FDE_PENDING) {
302 return;
303 }
304 subproc_fde->state |= FDE_PENDING;
305 fdevent_call_fdfunc(subproc_fde);
306 }
307}
308
309void fdevent_subproc_setup()
310{
311 int s[2];
312
313 if(adb_socketpair(s)) {
314 PLOG(FATAL) << "cannot create shell-exit socket-pair";
315 }
316 D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]);
317
318 SHELL_EXIT_NOTIFY_FD = s[0];
319 fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
320 CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler";
321 fdevent_add(fde, FDE_READ);
322}
323#endif // !ADB_HOST
324
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200325void fdevent_loop()
326{
Alex Vallée436fa6b2015-07-17 15:30:59 -0400327#if !ADB_HOST
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700328 fdevent_subproc_setup();
Alex Vallée436fa6b2015-07-17 15:30:59 -0400329#endif // !ADB_HOST
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200330
Elliott Hughesb628cb12015-08-03 10:38:08 -0700331 while (true) {
Yabin Cui56112542015-09-04 16:19:56 -0700332 D("--- --- waiting for events");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700333
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200334 fdevent_process();
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200335
Yabin Cui56112542015-09-04 16:19:56 -0700336 while (!g_pending_list.empty()) {
337 fdevent* fde = g_pending_list.front();
338 g_pending_list.pop_front();
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700339 fdevent_call_fdfunc(fde);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200340 }
341 }
342}
Yabin Cui2ce9d562015-09-15 16:27:09 -0700343
344size_t fdevent_installed_count() {
345 return g_poll_node_map.size();
346}
347
348void fdevent_reset() {
349 g_poll_node_map.clear();
350 g_pending_list.clear();
351}