The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c |
| 2 | ** |
| 3 | ** Copyright 2006, Brian Swetland <swetland@frotz.net> |
| 4 | ** |
Elliott Hughes | aa24549 | 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 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | aa24549 | 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 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 18 | #define TRACE_TAG FDEVENT |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 19 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | #include "sysdeps.h" |
| 21 | #include "fdevent.h" |
| 22 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <fcntl.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 28 | #include <list> |
| 29 | #include <unordered_map> |
| 30 | #include <vector> |
| 31 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/stringprintf.h> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 34 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 35 | #include "adb_io.h" |
leozwang | d3fc15f | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 36 | #include "adb_trace.h" |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 37 | #include "adb_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 39 | #if !ADB_HOST |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 40 | // This socket is used when a subproc shell service exists. |
| 41 | // It wakes up the fdevent_loop() and cause the correct handling |
| 42 | // of the shell's pseudo-tty master. I.e. force close it. |
| 43 | int SHELL_EXIT_NOTIFY_FD = -1; |
Alex Vallée | 947cb3e | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 44 | #endif // !ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | #define FDE_EVENTMASK 0x00ff |
| 47 | #define FDE_STATEMASK 0xff00 |
| 48 | |
| 49 | #define FDE_ACTIVE 0x0100 |
| 50 | #define FDE_PENDING 0x0200 |
| 51 | #define FDE_CREATED 0x0400 |
| 52 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 53 | struct PollNode { |
| 54 | fdevent* fde; |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 55 | adb_pollfd pollfd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 57 | PollNode(fdevent* fde) : fde(fde) { |
| 58 | memset(&pollfd, 0, sizeof(pollfd)); |
| 59 | pollfd.fd = fde->fd; |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 60 | |
| 61 | #if defined(__linux__) |
| 62 | // Always enable POLLRDHUP, so the host server can take action when some clients disconnect. |
| 63 | // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034. |
| 64 | pollfd.events = POLLRDHUP; |
| 65 | #endif |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 66 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 69 | // All operations to fdevent should happen only in the main thread. |
| 70 | // That's why we don't need a lock for fdevent. |
Josh Gao | b7b1edf | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 71 | static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>(); |
| 72 | static auto& g_pending_list = *new std::list<fdevent*>(); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 73 | static bool main_thread_valid; |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 74 | static unsigned long main_thread_id; |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 75 | |
| 76 | static void check_main_thread() { |
| 77 | if (main_thread_valid) { |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 78 | CHECK_EQ(main_thread_id, adb_thread_id()); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | static void set_main_thread() { |
| 83 | main_thread_valid = true; |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 84 | main_thread_id = adb_thread_id(); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 85 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 87 | static std::string dump_fde(const fdevent* fde) { |
| 88 | std::string state; |
| 89 | if (fde->state & FDE_ACTIVE) { |
| 90 | state += "A"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 92 | if (fde->state & FDE_PENDING) { |
| 93 | state += "P"; |
| 94 | } |
| 95 | if (fde->state & FDE_CREATED) { |
| 96 | state += "C"; |
| 97 | } |
| 98 | if (fde->state & FDE_READ) { |
| 99 | state += "R"; |
| 100 | } |
| 101 | if (fde->state & FDE_WRITE) { |
| 102 | state += "W"; |
| 103 | } |
| 104 | if (fde->state & FDE_ERROR) { |
| 105 | state += "E"; |
| 106 | } |
| 107 | if (fde->state & FDE_DONT_CLOSE) { |
| 108 | state += "D"; |
| 109 | } |
| 110 | return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | } |
| 112 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | fdevent *fdevent_create(int fd, fd_func func, void *arg) |
| 114 | { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 115 | check_main_thread(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | fdevent *fde = (fdevent*) malloc(sizeof(fdevent)); |
| 117 | if(fde == 0) return 0; |
| 118 | fdevent_install(fde, fd, func, arg); |
| 119 | fde->state |= FDE_CREATED; |
| 120 | return fde; |
| 121 | } |
| 122 | |
| 123 | void fdevent_destroy(fdevent *fde) |
| 124 | { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 125 | check_main_thread(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | if(fde == 0) return; |
| 127 | if(!(fde->state & FDE_CREATED)) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 128 | LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | } |
| 130 | fdevent_remove(fde); |
SungHyun Kwon | abb80e0 | 2015-03-03 13:56:42 +0900 | [diff] [blame] | 131 | free(fde); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 134 | void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 135 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 136 | CHECK_GE(fd, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | memset(fde, 0, sizeof(fdevent)); |
| 138 | fde->state = FDE_ACTIVE; |
| 139 | fde->fd = fd; |
| 140 | fde->func = func; |
| 141 | fde->arg = arg; |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 142 | if (!set_file_block_mode(fd, false)) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 143 | // Here is not proper to handle the error. If it fails here, some error is |
| 144 | // likely to be detected by poll(), then we can let the callback function |
| 145 | // to handle it. |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 146 | LOG(ERROR) << "failed to set non-blocking mode for fd " << fd; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 147 | } |
| 148 | auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde)); |
| 149 | CHECK(pair.second) << "install existing fd " << fd; |
| 150 | D("fdevent_install %s", dump_fde(fde).c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 153 | void fdevent_remove(fdevent* fde) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 154 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 155 | D("fdevent_remove %s", dump_fde(fde).c_str()); |
| 156 | if (fde->state & FDE_ACTIVE) { |
| 157 | g_poll_node_map.erase(fde->fd); |
| 158 | if (fde->state & FDE_PENDING) { |
| 159 | g_pending_list.remove(fde); |
| 160 | } |
| 161 | if (!(fde->state & FDE_DONT_CLOSE)) { |
| 162 | adb_close(fde->fd); |
| 163 | fde->fd = -1; |
| 164 | } |
| 165 | fde->state = 0; |
| 166 | fde->events = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 167 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 170 | static void fdevent_update(fdevent* fde, unsigned events) { |
| 171 | auto it = g_poll_node_map.find(fde->fd); |
| 172 | CHECK(it != g_poll_node_map.end()); |
| 173 | PollNode& node = it->second; |
| 174 | if (events & FDE_READ) { |
| 175 | node.pollfd.events |= POLLIN; |
| 176 | } else { |
| 177 | node.pollfd.events &= ~POLLIN; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 180 | if (events & FDE_WRITE) { |
| 181 | node.pollfd.events |= POLLOUT; |
| 182 | } else { |
| 183 | node.pollfd.events &= ~POLLOUT; |
| 184 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | fde->state = (fde->state & FDE_STATEMASK) | events; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 186 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 188 | void fdevent_set(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 189 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 190 | events &= FDE_EVENTMASK; |
| 191 | if ((fde->state & FDE_EVENTMASK) == events) { |
| 192 | return; |
| 193 | } |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 194 | CHECK(fde->state & FDE_ACTIVE); |
| 195 | fdevent_update(fde, events); |
| 196 | D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 197 | |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 198 | if (fde->state & FDE_PENDING) { |
| 199 | // If we are pending, make sure we don't signal an event that is no longer wanted. |
| 200 | fde->events &= events; |
| 201 | if (fde->events == 0) { |
| 202 | g_pending_list.remove(fde); |
| 203 | fde->state &= ~FDE_PENDING; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 208 | void fdevent_add(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 209 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 210 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 213 | void fdevent_del(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 214 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 215 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 218 | static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 219 | std::string result; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 220 | for (const auto& pollfd : pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 221 | std::string op; |
| 222 | if (pollfd.events & POLLIN) { |
| 223 | op += "R"; |
| 224 | } |
| 225 | if (pollfd.events & POLLOUT) { |
| 226 | op += "W"; |
| 227 | } |
| 228 | android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str()); |
| 229 | } |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | static void fdevent_process() { |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 234 | std::vector<adb_pollfd> pollfds; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 235 | for (const auto& pair : g_poll_node_map) { |
| 236 | pollfds.push_back(pair.second.pollfd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 237 | } |
| 238 | CHECK_GT(pollfds.size(), 0u); |
| 239 | D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str()); |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 240 | int ret = adb_poll(&pollfds[0], pollfds.size(), -1); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 241 | if (ret == -1) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 242 | PLOG(ERROR) << "poll(), ret = " << ret; |
| 243 | return; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 244 | } |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 245 | for (const auto& pollfd : pollfds) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 246 | if (pollfd.revents != 0) { |
| 247 | D("for fd %d, revents = %x", pollfd.fd, pollfd.revents); |
| 248 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 249 | unsigned events = 0; |
| 250 | if (pollfd.revents & POLLIN) { |
| 251 | events |= FDE_READ; |
| 252 | } |
| 253 | if (pollfd.revents & POLLOUT) { |
| 254 | events |= FDE_WRITE; |
| 255 | } |
| 256 | if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
| 257 | // We fake a read, as the rest of the code assumes that errors will |
| 258 | // be detected at that point. |
| 259 | events |= FDE_READ | FDE_ERROR; |
| 260 | } |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 261 | #if defined(__linux__) |
| 262 | if (pollfd.revents & POLLRDHUP) { |
| 263 | events |= FDE_READ | FDE_ERROR; |
| 264 | } |
| 265 | #endif |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 266 | if (events != 0) { |
| 267 | auto it = g_poll_node_map.find(pollfd.fd); |
| 268 | CHECK(it != g_poll_node_map.end()); |
| 269 | fdevent* fde = it->second.fde; |
| 270 | CHECK_EQ(fde->fd, pollfd.fd); |
| 271 | fde->events |= events; |
| 272 | D("%s got events %x", dump_fde(fde).c_str(), events); |
| 273 | fde->state |= FDE_PENDING; |
| 274 | g_pending_list.push_back(fde); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static void fdevent_call_fdfunc(fdevent* fde) |
| 280 | { |
| 281 | unsigned events = fde->events; |
| 282 | fde->events = 0; |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 283 | CHECK(fde->state & FDE_PENDING); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 284 | fde->state &= (~FDE_PENDING); |
| 285 | D("fdevent_call_fdfunc %s", dump_fde(fde).c_str()); |
| 286 | fde->func(fde->fd, events, fde->arg); |
| 287 | } |
| 288 | |
| 289 | #if !ADB_HOST |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame^] | 290 | |
| 291 | #include <sys/ioctl.h> |
| 292 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 293 | static void fdevent_subproc_event_func(int fd, unsigned ev, |
| 294 | void* /* userdata */) |
| 295 | { |
| 296 | |
| 297 | D("subproc handling on fd = %d, ev = %x", fd, ev); |
| 298 | |
| 299 | CHECK_GE(fd, 0); |
| 300 | |
| 301 | if (ev & FDE_READ) { |
| 302 | int subproc_fd; |
| 303 | |
| 304 | if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) { |
| 305 | LOG(FATAL) << "Failed to read the subproc's fd from " << fd; |
| 306 | } |
| 307 | auto it = g_poll_node_map.find(subproc_fd); |
| 308 | if (it == g_poll_node_map.end()) { |
| 309 | D("subproc_fd %d cleared from fd_table", subproc_fd); |
Josh Gao | c65fae9 | 2016-01-19 16:21:17 -0800 | [diff] [blame] | 310 | adb_close(subproc_fd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | fdevent* subproc_fde = it->second.fde; |
| 314 | if(subproc_fde->fd != subproc_fd) { |
| 315 | // Already reallocated? |
Josh Gao | c65fae9 | 2016-01-19 16:21:17 -0800 | [diff] [blame] | 316 | LOG(FATAL) << "subproc_fd(" << subproc_fd << ") != subproc_fde->fd(" << subproc_fde->fd |
| 317 | << ")"; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | |
| 321 | subproc_fde->force_eof = 1; |
| 322 | |
| 323 | int rcount = 0; |
| 324 | ioctl(subproc_fd, FIONREAD, &rcount); |
| 325 | D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno); |
| 326 | if (rcount != 0) { |
| 327 | // If there is data left, it will show up in the select(). |
| 328 | // This works because there is no other thread reading that |
| 329 | // data when in this fd_func(). |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | D("subproc_fde %s", dump_fde(subproc_fde).c_str()); |
| 334 | subproc_fde->events |= FDE_READ; |
| 335 | if(subproc_fde->state & FDE_PENDING) { |
| 336 | return; |
| 337 | } |
| 338 | subproc_fde->state |= FDE_PENDING; |
| 339 | fdevent_call_fdfunc(subproc_fde); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void fdevent_subproc_setup() |
| 344 | { |
| 345 | int s[2]; |
| 346 | |
| 347 | if(adb_socketpair(s)) { |
| 348 | PLOG(FATAL) << "cannot create shell-exit socket-pair"; |
| 349 | } |
| 350 | D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]); |
| 351 | |
| 352 | SHELL_EXIT_NOTIFY_FD = s[0]; |
| 353 | fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL); |
| 354 | CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler"; |
| 355 | fdevent_add(fde, FDE_READ); |
| 356 | } |
| 357 | #endif // !ADB_HOST |
| 358 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | void fdevent_loop() |
| 360 | { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 361 | set_main_thread(); |
Alex Vallée | 947cb3e | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 362 | #if !ADB_HOST |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 363 | fdevent_subproc_setup(); |
Alex Vallée | 947cb3e | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 364 | #endif // !ADB_HOST |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 365 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 366 | while (true) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 367 | D("--- --- waiting for events"); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 368 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 369 | fdevent_process(); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 370 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 371 | while (!g_pending_list.empty()) { |
| 372 | fdevent* fde = g_pending_list.front(); |
| 373 | g_pending_list.pop_front(); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 374 | fdevent_call_fdfunc(fde); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 378 | |
| 379 | size_t fdevent_installed_count() { |
| 380 | return g_poll_node_map.size(); |
| 381 | } |
| 382 | |
| 383 | void fdevent_reset() { |
| 384 | g_poll_node_map.clear(); |
| 385 | g_pending_list.clear(); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 386 | main_thread_valid = false; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 387 | } |