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> |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame^] | 24 | #include <stdint.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 29 | #include <atomic> |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 30 | #include <deque> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 31 | #include <functional> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 32 | #include <list> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 33 | #include <mutex> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 34 | #include <unordered_map> |
| 35 | #include <vector> |
| 36 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
| 38 | #include <android-base/stringprintf.h> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 39 | #include <android-base/thread_annotations.h> |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame^] | 40 | #include <android-base/threads.h> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 41 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 42 | #include "adb_io.h" |
leozwang | d3fc15f | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 43 | #include "adb_trace.h" |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 44 | #include "adb_unique_fd.h" |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 45 | #include "adb_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 47 | #if !ADB_HOST |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 48 | // This socket is used when a subproc shell service exists. |
| 49 | // It wakes up the fdevent_loop() and cause the correct handling |
| 50 | // of the shell's pseudo-tty master. I.e. force close it. |
| 51 | int SHELL_EXIT_NOTIFY_FD = -1; |
Alex Vallée | 947cb3e | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 52 | #endif // !ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | #define FDE_EVENTMASK 0x00ff |
| 55 | #define FDE_STATEMASK 0xff00 |
| 56 | |
| 57 | #define FDE_ACTIVE 0x0100 |
| 58 | #define FDE_PENDING 0x0200 |
| 59 | #define FDE_CREATED 0x0400 |
| 60 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 61 | struct PollNode { |
| 62 | fdevent* fde; |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 63 | adb_pollfd pollfd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 65 | explicit PollNode(fdevent* fde) : fde(fde) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 66 | memset(&pollfd, 0, sizeof(pollfd)); |
| 67 | pollfd.fd = fde->fd; |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 68 | |
| 69 | #if defined(__linux__) |
| 70 | // Always enable POLLRDHUP, so the host server can take action when some clients disconnect. |
| 71 | // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034. |
| 72 | pollfd.events = POLLRDHUP; |
| 73 | #endif |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 74 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 77 | // All operations to fdevent should happen only in the main thread. |
| 78 | // That's why we don't need a lock for fdevent. |
Josh Gao | b7b1edf | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 79 | static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>(); |
| 80 | static auto& g_pending_list = *new std::list<fdevent*>(); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 81 | static std::atomic<bool> terminate_loop(false); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 82 | static bool main_thread_valid; |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame^] | 83 | static uint64_t main_thread_id; |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 84 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 85 | static auto& run_queue_notify_fd = *new unique_fd(); |
| 86 | static auto& run_queue_mutex = *new std::mutex(); |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 87 | static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 88 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 89 | void check_main_thread() { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 90 | if (main_thread_valid) { |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame^] | 91 | CHECK_EQ(main_thread_id, android::base::GetThreadId()); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 95 | void set_main_thread() { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 96 | main_thread_valid = true; |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame^] | 97 | main_thread_id = android::base::GetThreadId(); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 98 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 100 | static std::string dump_fde(const fdevent* fde) { |
| 101 | std::string state; |
| 102 | if (fde->state & FDE_ACTIVE) { |
| 103 | state += "A"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 105 | if (fde->state & FDE_PENDING) { |
| 106 | state += "P"; |
| 107 | } |
| 108 | if (fde->state & FDE_CREATED) { |
| 109 | state += "C"; |
| 110 | } |
| 111 | if (fde->state & FDE_READ) { |
| 112 | state += "R"; |
| 113 | } |
| 114 | if (fde->state & FDE_WRITE) { |
| 115 | state += "W"; |
| 116 | } |
| 117 | if (fde->state & FDE_ERROR) { |
| 118 | state += "E"; |
| 119 | } |
| 120 | if (fde->state & FDE_DONT_CLOSE) { |
| 121 | state += "D"; |
| 122 | } |
| 123 | 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] | 124 | } |
| 125 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 126 | fdevent* fdevent_create(int fd, fd_func func, void* arg) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 127 | check_main_thread(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | fdevent *fde = (fdevent*) malloc(sizeof(fdevent)); |
| 129 | if(fde == 0) return 0; |
| 130 | fdevent_install(fde, fd, func, arg); |
| 131 | fde->state |= FDE_CREATED; |
| 132 | return fde; |
| 133 | } |
| 134 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 135 | void fdevent_destroy(fdevent* fde) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 136 | check_main_thread(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | if(fde == 0) return; |
| 138 | if(!(fde->state & FDE_CREATED)) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 139 | 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] | 140 | } |
| 141 | fdevent_remove(fde); |
SungHyun Kwon | abb80e0 | 2015-03-03 13:56:42 +0900 | [diff] [blame] | 142 | free(fde); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 145 | void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 146 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 147 | CHECK_GE(fd, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | memset(fde, 0, sizeof(fdevent)); |
| 149 | fde->state = FDE_ACTIVE; |
| 150 | fde->fd = fd; |
| 151 | fde->func = func; |
| 152 | fde->arg = arg; |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 153 | if (!set_file_block_mode(fd, false)) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 154 | // Here is not proper to handle the error. If it fails here, some error is |
| 155 | // likely to be detected by poll(), then we can let the callback function |
| 156 | // to handle it. |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 157 | LOG(ERROR) << "failed to set non-blocking mode for fd " << fd; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 158 | } |
| 159 | auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde)); |
| 160 | CHECK(pair.second) << "install existing fd " << fd; |
| 161 | D("fdevent_install %s", dump_fde(fde).c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 164 | void fdevent_remove(fdevent* fde) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 165 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 166 | D("fdevent_remove %s", dump_fde(fde).c_str()); |
| 167 | if (fde->state & FDE_ACTIVE) { |
| 168 | g_poll_node_map.erase(fde->fd); |
| 169 | if (fde->state & FDE_PENDING) { |
| 170 | g_pending_list.remove(fde); |
| 171 | } |
| 172 | if (!(fde->state & FDE_DONT_CLOSE)) { |
| 173 | adb_close(fde->fd); |
| 174 | fde->fd = -1; |
| 175 | } |
| 176 | fde->state = 0; |
| 177 | fde->events = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 181 | static void fdevent_update(fdevent* fde, unsigned events) { |
| 182 | auto it = g_poll_node_map.find(fde->fd); |
| 183 | CHECK(it != g_poll_node_map.end()); |
| 184 | PollNode& node = it->second; |
| 185 | if (events & FDE_READ) { |
| 186 | node.pollfd.events |= POLLIN; |
| 187 | } else { |
| 188 | node.pollfd.events &= ~POLLIN; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 191 | if (events & FDE_WRITE) { |
| 192 | node.pollfd.events |= POLLOUT; |
| 193 | } else { |
| 194 | node.pollfd.events &= ~POLLOUT; |
| 195 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | fde->state = (fde->state & FDE_STATEMASK) | events; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 197 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 199 | void fdevent_set(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 200 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 201 | events &= FDE_EVENTMASK; |
| 202 | if ((fde->state & FDE_EVENTMASK) == events) { |
| 203 | return; |
| 204 | } |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 205 | CHECK(fde->state & FDE_ACTIVE); |
| 206 | fdevent_update(fde, events); |
| 207 | D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 208 | |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 209 | if (fde->state & FDE_PENDING) { |
| 210 | // If we are pending, make sure we don't signal an event that is no longer wanted. |
| 211 | fde->events &= events; |
| 212 | if (fde->events == 0) { |
| 213 | g_pending_list.remove(fde); |
| 214 | fde->state &= ~FDE_PENDING; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 219 | void fdevent_add(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 220 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 221 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 224 | void fdevent_del(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 225 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 226 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 229 | static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 230 | std::string result; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 231 | for (const auto& pollfd : pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 232 | std::string op; |
| 233 | if (pollfd.events & POLLIN) { |
| 234 | op += "R"; |
| 235 | } |
| 236 | if (pollfd.events & POLLOUT) { |
| 237 | op += "W"; |
| 238 | } |
| 239 | android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str()); |
| 240 | } |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | static void fdevent_process() { |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 245 | std::vector<adb_pollfd> pollfds; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 246 | for (const auto& pair : g_poll_node_map) { |
| 247 | pollfds.push_back(pair.second.pollfd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 248 | } |
| 249 | CHECK_GT(pollfds.size(), 0u); |
| 250 | D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str()); |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 251 | int ret = adb_poll(&pollfds[0], pollfds.size(), -1); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 252 | if (ret == -1) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 253 | PLOG(ERROR) << "poll(), ret = " << ret; |
| 254 | return; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 255 | } |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 256 | for (const auto& pollfd : pollfds) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 257 | if (pollfd.revents != 0) { |
| 258 | D("for fd %d, revents = %x", pollfd.fd, pollfd.revents); |
| 259 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 260 | unsigned events = 0; |
| 261 | if (pollfd.revents & POLLIN) { |
| 262 | events |= FDE_READ; |
| 263 | } |
| 264 | if (pollfd.revents & POLLOUT) { |
| 265 | events |= FDE_WRITE; |
| 266 | } |
| 267 | if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
| 268 | // We fake a read, as the rest of the code assumes that errors will |
| 269 | // be detected at that point. |
| 270 | events |= FDE_READ | FDE_ERROR; |
| 271 | } |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 272 | #if defined(__linux__) |
| 273 | if (pollfd.revents & POLLRDHUP) { |
| 274 | events |= FDE_READ | FDE_ERROR; |
| 275 | } |
| 276 | #endif |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 277 | if (events != 0) { |
| 278 | auto it = g_poll_node_map.find(pollfd.fd); |
| 279 | CHECK(it != g_poll_node_map.end()); |
| 280 | fdevent* fde = it->second.fde; |
| 281 | CHECK_EQ(fde->fd, pollfd.fd); |
| 282 | fde->events |= events; |
| 283 | D("%s got events %x", dump_fde(fde).c_str(), events); |
| 284 | fde->state |= FDE_PENDING; |
| 285 | g_pending_list.push_back(fde); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 290 | static void fdevent_call_fdfunc(fdevent* fde) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 291 | unsigned events = fde->events; |
| 292 | fde->events = 0; |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 293 | CHECK(fde->state & FDE_PENDING); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 294 | fde->state &= (~FDE_PENDING); |
| 295 | D("fdevent_call_fdfunc %s", dump_fde(fde).c_str()); |
| 296 | fde->func(fde->fd, events, fde->arg); |
| 297 | } |
| 298 | |
| 299 | #if !ADB_HOST |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 300 | |
| 301 | #include <sys/ioctl.h> |
| 302 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 303 | static void fdevent_subproc_event_func(int fd, unsigned ev, void* /* userdata */) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 304 | D("subproc handling on fd = %d, ev = %x", fd, ev); |
| 305 | |
| 306 | CHECK_GE(fd, 0); |
| 307 | |
| 308 | if (ev & FDE_READ) { |
| 309 | int subproc_fd; |
| 310 | |
| 311 | if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) { |
| 312 | LOG(FATAL) << "Failed to read the subproc's fd from " << fd; |
| 313 | } |
| 314 | auto it = g_poll_node_map.find(subproc_fd); |
| 315 | if (it == g_poll_node_map.end()) { |
| 316 | D("subproc_fd %d cleared from fd_table", subproc_fd); |
Josh Gao | c65fae9 | 2016-01-19 16:21:17 -0800 | [diff] [blame] | 317 | adb_close(subproc_fd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | fdevent* subproc_fde = it->second.fde; |
| 321 | if(subproc_fde->fd != subproc_fd) { |
| 322 | // Already reallocated? |
Josh Gao | c65fae9 | 2016-01-19 16:21:17 -0800 | [diff] [blame] | 323 | LOG(FATAL) << "subproc_fd(" << subproc_fd << ") != subproc_fde->fd(" << subproc_fde->fd |
| 324 | << ")"; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | |
| 328 | subproc_fde->force_eof = 1; |
| 329 | |
| 330 | int rcount = 0; |
| 331 | ioctl(subproc_fd, FIONREAD, &rcount); |
| 332 | D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno); |
| 333 | if (rcount != 0) { |
| 334 | // If there is data left, it will show up in the select(). |
| 335 | // This works because there is no other thread reading that |
| 336 | // data when in this fd_func(). |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | D("subproc_fde %s", dump_fde(subproc_fde).c_str()); |
| 341 | subproc_fde->events |= FDE_READ; |
| 342 | if(subproc_fde->state & FDE_PENDING) { |
| 343 | return; |
| 344 | } |
| 345 | subproc_fde->state |= FDE_PENDING; |
| 346 | fdevent_call_fdfunc(subproc_fde); |
| 347 | } |
| 348 | } |
| 349 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 350 | static void fdevent_subproc_setup() { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 351 | int s[2]; |
| 352 | |
| 353 | if(adb_socketpair(s)) { |
| 354 | PLOG(FATAL) << "cannot create shell-exit socket-pair"; |
| 355 | } |
| 356 | D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]); |
| 357 | |
| 358 | SHELL_EXIT_NOTIFY_FD = s[0]; |
| 359 | fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL); |
| 360 | CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler"; |
| 361 | fdevent_add(fde, FDE_READ); |
| 362 | } |
| 363 | #endif // !ADB_HOST |
| 364 | |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 365 | static void fdevent_run_flush() EXCLUDES(run_queue_mutex) { |
| 366 | // We need to be careful around reentrancy here, since a function we call can queue up another |
| 367 | // function. |
| 368 | while (true) { |
| 369 | std::function<void()> fn; |
| 370 | { |
| 371 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 372 | if (run_queue.empty()) { |
| 373 | break; |
| 374 | } |
| 375 | fn = run_queue.front(); |
| 376 | run_queue.pop_front(); |
| 377 | } |
| 378 | fn(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 379 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) { |
| 383 | CHECK_GE(fd, 0); |
| 384 | CHECK(ev & FDE_READ); |
| 385 | |
| 386 | char buf[1024]; |
| 387 | |
| 388 | // Empty the fd. |
| 389 | if (adb_read(fd, buf, sizeof(buf)) == -1) { |
| 390 | PLOG(FATAL) << "failed to empty run queue notify fd"; |
| 391 | } |
| 392 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 393 | fdevent_run_flush(); |
| 394 | } |
| 395 | |
| 396 | static void fdevent_run_setup() { |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 397 | { |
| 398 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 399 | CHECK(run_queue_notify_fd.get() == -1); |
| 400 | int s[2]; |
| 401 | if (adb_socketpair(s) != 0) { |
| 402 | PLOG(FATAL) << "failed to create run queue notify socketpair"; |
| 403 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 404 | |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 405 | run_queue_notify_fd.reset(s[0]); |
| 406 | fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr); |
| 407 | CHECK(fde != nullptr); |
| 408 | fdevent_add(fde, FDE_READ); |
| 409 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 410 | |
| 411 | fdevent_run_flush(); |
| 412 | } |
| 413 | |
| 414 | void fdevent_run_on_main_thread(std::function<void()> fn) { |
| 415 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 416 | run_queue.push_back(std::move(fn)); |
| 417 | |
| 418 | // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up. |
| 419 | // In that case, rely on the setup code to flush the queue without a notification being needed. |
| 420 | if (run_queue_notify_fd != -1) { |
| 421 | if (adb_write(run_queue_notify_fd.get(), "", 1) != 1) { |
| 422 | PLOG(FATAL) << "failed to write to run queue notify fd"; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void fdevent_loop() { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 428 | set_main_thread(); |
Alex Vallée | 947cb3e | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 429 | #if !ADB_HOST |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 430 | fdevent_subproc_setup(); |
Alex Vallée | 947cb3e | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 431 | #endif // !ADB_HOST |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 432 | fdevent_run_setup(); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 433 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 434 | while (true) { |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 435 | if (terminate_loop) { |
| 436 | return; |
| 437 | } |
| 438 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 439 | D("--- --- waiting for events"); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 440 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | fdevent_process(); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 442 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 443 | while (!g_pending_list.empty()) { |
| 444 | fdevent* fde = g_pending_list.front(); |
| 445 | g_pending_list.pop_front(); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 446 | fdevent_call_fdfunc(fde); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 450 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 451 | void fdevent_terminate_loop() { |
| 452 | terminate_loop = true; |
| 453 | } |
| 454 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 455 | size_t fdevent_installed_count() { |
| 456 | return g_poll_node_map.size(); |
| 457 | } |
| 458 | |
| 459 | void fdevent_reset() { |
| 460 | g_poll_node_map.clear(); |
| 461 | g_pending_list.clear(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 462 | |
| 463 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 464 | run_queue_notify_fd.reset(); |
| 465 | run_queue.clear(); |
| 466 | |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 467 | main_thread_valid = false; |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 468 | terminate_loop = false; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 469 | } |