David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 1 | /* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c |
| 2 | ** |
| 3 | ** Copyright 2006, Brian Swetland <swetland@frotz.net> |
| 4 | ** |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 5 | ** 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' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 8 | ** |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 10 | ** |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 11 | ** 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' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | #define TRACE_TAG TRACE_FDEVENT |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 19 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | #include "sysdeps.h" |
| 21 | #include "fdevent.h" |
| 22 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 23 | #include <fcntl.h> |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 24 | #include <poll.h> |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <unistd.h> |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 29 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 30 | #include <list> |
| 31 | #include <unordered_map> |
| 32 | #include <vector> |
| 33 | |
| 34 | #include <base/logging.h> |
| 35 | #include <base/stringprintf.h> |
| 36 | |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 37 | #include "adb_io.h" |
leozwang | f25a6a4 | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 38 | #include "adb_trace.h" |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 39 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 40 | #if !ADB_HOST |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 41 | // 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. |
| 44 | int SHELL_EXIT_NOTIFY_FD = -1; |
Alex Vallée | 436fa6b | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 45 | #endif // !ADB_HOST |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 46 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 47 | #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 Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 54 | struct PollNode { |
| 55 | fdevent* fde; |
| 56 | pollfd pollfd; |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 57 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 58 | PollNode(fdevent* fde) : fde(fde) { |
| 59 | memset(&pollfd, 0, sizeof(pollfd)); |
| 60 | pollfd.fd = fde->fd; |
| 61 | } |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 64 | // All operations to fdevent should happen only in the main thread. |
| 65 | // That's why we don't need a lock for fdevent. |
| 66 | static std::unordered_map<int, PollNode> g_poll_node_map; |
| 67 | static std::list<fdevent*> g_pending_list; |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 68 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 69 | static std::string dump_fde(const fdevent* fde) { |
| 70 | std::string state; |
| 71 | if (fde->state & FDE_ACTIVE) { |
| 72 | state += "A"; |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 73 | } |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 74 | 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' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 93 | } |
| 94 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 95 | fdevent *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 | |
| 104 | void fdevent_destroy(fdevent *fde) |
| 105 | { |
| 106 | if(fde == 0) return; |
| 107 | if(!(fde->state & FDE_CREATED)) { |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 108 | LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 109 | } |
| 110 | fdevent_remove(fde); |
SungHyun Kwon | b777b3b | 2015-03-03 13:56:42 +0900 | [diff] [blame] | 111 | free(fde); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 114 | void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) { |
| 115 | CHECK_GE(fd, 0); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 116 | memset(fde, 0, sizeof(fdevent)); |
| 117 | fde->state = FDE_ACTIVE; |
| 118 | fde->fd = fd; |
| 119 | fde->func = func; |
| 120 | fde->arg = arg; |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 121 | if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) { |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame^] | 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"; |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 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()); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 132 | void 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' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 145 | } |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 148 | static 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' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 158 | if (events & FDE_WRITE) { |
| 159 | node.pollfd.events |= POLLOUT; |
| 160 | } else { |
| 161 | node.pollfd.events &= ~POLLOUT; |
| 162 | } |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 163 | fde->state = (fde->state & FDE_STATEMASK) | events; |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 164 | } |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 165 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 166 | void 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' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 186 | void fdevent_add(fdevent* fde, unsigned events) { |
| 187 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 190 | void fdevent_del(fdevent* fde, unsigned events) { |
| 191 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 194 | static 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 | |
| 209 | static 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 Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame^] | 218 | PLOG(ERROR) << "poll(), ret = " << ret; |
| 219 | return; |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 220 | } |
| 221 | for (auto& pollfd : pollfds) { |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame^] | 222 | if (pollfd.revents != 0) { |
| 223 | D("for fd %d, revents = %x", pollfd.fd, pollfd.revents); |
| 224 | } |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 225 | 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 | |
| 250 | static 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 |
| 261 | static 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 | |
| 309 | void 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' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 325 | void fdevent_loop() |
| 326 | { |
Alex Vallée | 436fa6b | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 327 | #if !ADB_HOST |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 328 | fdevent_subproc_setup(); |
Alex Vallée | 436fa6b | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 329 | #endif // !ADB_HOST |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 330 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 331 | while (true) { |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 332 | D("--- --- waiting for events"); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 333 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 334 | fdevent_process(); |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 335 | |
Yabin Cui | 5611254 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 336 | while (!g_pending_list.empty()) { |
| 337 | fdevent* fde = g_pending_list.front(); |
| 338 | g_pending_list.pop_front(); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 339 | fdevent_call_fdfunc(fde); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | } |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame^] | 343 | |
| 344 | size_t fdevent_installed_count() { |
| 345 | return g_poll_node_map.size(); |
| 346 | } |
| 347 | |
| 348 | void fdevent_reset() { |
| 349 | g_poll_node_map.clear(); |
| 350 | g_pending_list.clear(); |
| 351 | } |