blob: dc03807546215d0da1b167a14db2b116612b1493 [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
Dan Albert33134262015-03-19 15:21:08 -070018#define TRACE_TAG TRACE_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;
61 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062};
63
Yabin Cuia1080162015-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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Yabin Cuia1080162015-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";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 }
Yabin Cuia1080162015-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());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093}
94
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095fdevent *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 Cuia1080162015-09-04 16:19:56 -0700108 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 }
110 fdevent_remove(fde);
SungHyun Kwonabb80e02015-03-03 13:56:42 +0900111 free(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112}
113
Yabin Cuia1080162015-09-04 16:19:56 -0700114void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
115 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 memset(fde, 0, sizeof(fdevent));
117 fde->state = FDE_ACTIVE;
118 fde->fd = fd;
119 fde->func = func;
120 fde->arg = arg;
Yabin Cuia1080162015-09-04 16:19:56 -0700121 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
122 // 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";
126 }
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());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130}
131
Yabin Cuia1080162015-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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146}
147
Yabin Cuia1080162015-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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 }
157
Yabin Cuia1080162015-09-04 16:19:56 -0700158 if (events & FDE_WRITE) {
159 node.pollfd.events |= POLLOUT;
160 } else {
161 node.pollfd.events &= ~POLLOUT;
162 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700164}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Yabin Cuia1080162015-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 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 }
183 }
184}
185
Yabin Cuia1080162015-09-04 16:19:56 -0700186void fdevent_add(fdevent* fde, unsigned events) {
187 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188}
189
Yabin Cuia1080162015-09-04 16:19:56 -0700190void fdevent_del(fdevent* fde, unsigned events) {
191 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192}
193
Yabin Cuia1080162015-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) {
218 PLOG(ERROR) << "poll(), ret = " << ret;
219 return;
220 }
221 for (auto& pollfd : pollfds) {
222 unsigned events = 0;
223 if (pollfd.revents & POLLIN) {
224 events |= FDE_READ;
225 }
226 if (pollfd.revents & POLLOUT) {
227 events |= FDE_WRITE;
228 }
229 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
230 // We fake a read, as the rest of the code assumes that errors will
231 // be detected at that point.
232 events |= FDE_READ | FDE_ERROR;
233 }
234 if (events != 0) {
235 auto it = g_poll_node_map.find(pollfd.fd);
236 CHECK(it != g_poll_node_map.end());
237 fdevent* fde = it->second.fde;
238 CHECK_EQ(fde->fd, pollfd.fd);
239 fde->events |= events;
240 D("%s got events %x", dump_fde(fde).c_str(), events);
241 fde->state |= FDE_PENDING;
242 g_pending_list.push_back(fde);
243 }
244 }
245}
246
247static void fdevent_call_fdfunc(fdevent* fde)
248{
249 unsigned events = fde->events;
250 fde->events = 0;
251 if(!(fde->state & FDE_PENDING)) return;
252 fde->state &= (~FDE_PENDING);
253 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
254 fde->func(fde->fd, events, fde->arg);
255}
256
257#if !ADB_HOST
258static void fdevent_subproc_event_func(int fd, unsigned ev,
259 void* /* userdata */)
260{
261
262 D("subproc handling on fd = %d, ev = %x", fd, ev);
263
264 CHECK_GE(fd, 0);
265
266 if (ev & FDE_READ) {
267 int subproc_fd;
268
269 if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
270 LOG(FATAL) << "Failed to read the subproc's fd from " << fd;
271 }
272 auto it = g_poll_node_map.find(subproc_fd);
273 if (it == g_poll_node_map.end()) {
274 D("subproc_fd %d cleared from fd_table", subproc_fd);
275 return;
276 }
277 fdevent* subproc_fde = it->second.fde;
278 if(subproc_fde->fd != subproc_fd) {
279 // Already reallocated?
280 D("subproc_fd(%d) != subproc_fde->fd(%d)", subproc_fd, subproc_fde->fd);
281 return;
282 }
283
284 subproc_fde->force_eof = 1;
285
286 int rcount = 0;
287 ioctl(subproc_fd, FIONREAD, &rcount);
288 D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno);
289 if (rcount != 0) {
290 // If there is data left, it will show up in the select().
291 // This works because there is no other thread reading that
292 // data when in this fd_func().
293 return;
294 }
295
296 D("subproc_fde %s", dump_fde(subproc_fde).c_str());
297 subproc_fde->events |= FDE_READ;
298 if(subproc_fde->state & FDE_PENDING) {
299 return;
300 }
301 subproc_fde->state |= FDE_PENDING;
302 fdevent_call_fdfunc(subproc_fde);
303 }
304}
305
306void fdevent_subproc_setup()
307{
308 int s[2];
309
310 if(adb_socketpair(s)) {
311 PLOG(FATAL) << "cannot create shell-exit socket-pair";
312 }
313 D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]);
314
315 SHELL_EXIT_NOTIFY_FD = s[0];
316 fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
317 CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler";
318 fdevent_add(fde, FDE_READ);
319}
320#endif // !ADB_HOST
321
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322void fdevent_loop()
323{
Alex Vallée947cb3e2015-07-17 15:30:59 -0400324#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700325 fdevent_subproc_setup();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400326#endif // !ADB_HOST
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200327
Elliott Hughesaa245492015-08-03 10:38:08 -0700328 while (true) {
Yabin Cuia1080162015-09-04 16:19:56 -0700329 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700330
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200332
Yabin Cuia1080162015-09-04 16:19:56 -0700333 while (!g_pending_list.empty()) {
334 fdevent* fde = g_pending_list.front();
335 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700336 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 }
338 }
339}