The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRANSPORT |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 18 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 19 | #include "sysdeps.h" |
Josh Gao | f2a988c | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 20 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include "transport.h" |
| 22 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 23 | #include <ctype.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 24 | #include <errno.h> |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <string.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 31 | #include <algorithm> |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 32 | #include <deque> |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 33 | #include <list> |
Pirama Arumuga Nainar | 5231aff | 2018-08-08 10:33:24 -0700 | [diff] [blame] | 34 | #include <memory> |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 35 | #include <mutex> |
Josh Gao | d2ea065 | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 36 | #include <set> |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 37 | #include <thread> |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 39 | #include <android-base/logging.h> |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 40 | #include <android-base/parsenetaddress.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
| 42 | #include <android-base/strings.h> |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 43 | #include <android-base/thread_annotations.h> |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 44 | |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 45 | #include <diagnose_usb.h> |
| 46 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include "adb.h" |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 48 | #include "adb_auth.h" |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 49 | #include "adb_io.h" |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 50 | #include "adb_trace.h" |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 51 | #include "adb_utils.h" |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 52 | #include "fdevent.h" |
Josh Gao | b018289 | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 53 | #include "sysdeps/chrono.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 55 | static void remove_transport(atransport* transport); |
| 56 | static void transport_unref(atransport* transport); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 58 | // TODO: unordered_map<TransportId, atransport*> |
Josh Gao | e3a87d0 | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 59 | static auto& transport_list = *new std::list<atransport*>(); |
| 60 | static auto& pending_list = *new std::list<atransport*>(); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 61 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 62 | static auto& transport_lock = *new std::recursive_mutex(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | |
Todd Kennedy | aff9c67 | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 64 | const char* const kFeatureShell2 = "shell_v2"; |
| 65 | const char* const kFeatureCmd = "cmd"; |
Josh Gao | a2cf375 | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 66 | const char* const kFeatureStat2 = "stat_v2"; |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 67 | const char* const kFeatureLibusb = "libusb"; |
Dan Albert | 27983bc | 2017-05-23 14:30:00 -0700 | [diff] [blame] | 68 | const char* const kFeaturePushSync = "push_sync"; |
Dario Freni | dcb4c36 | 2018-10-04 16:26:40 +0100 | [diff] [blame] | 69 | const char* const kFeatureApex = "apex"; |
Josh Gao | 281aab7 | 2018-10-22 13:00:05 -0700 | [diff] [blame] | 70 | const char* const kFeatureFixedPushMkdir = "fixed_push_mkdir"; |
Alex Buynytskyy | e1fa814 | 2019-01-17 13:13:56 -0800 | [diff] [blame^] | 71 | const char* const kFeatureAbb = "abb"; |
Todd Kennedy | aff9c67 | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 72 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 73 | namespace { |
| 74 | |
| 75 | // A class that helps the Clang Thread Safety Analysis deal with |
| 76 | // std::unique_lock. Given that std::unique_lock is movable, and the analysis |
| 77 | // can not currently perform alias analysis, it is not annotated. In order to |
| 78 | // assert that the mutex is held, a ScopedAssumeLocked can be created just after |
| 79 | // the std::unique_lock. |
| 80 | class SCOPED_CAPABILITY ScopedAssumeLocked { |
| 81 | public: |
| 82 | ScopedAssumeLocked(std::mutex& mutex) ACQUIRE(mutex) {} |
| 83 | ~ScopedAssumeLocked() RELEASE() {} |
| 84 | }; |
| 85 | |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 86 | #if ADB_HOST |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 87 | // Tracks and handles atransport*s that are attempting reconnection. |
| 88 | class ReconnectHandler { |
| 89 | public: |
| 90 | ReconnectHandler() = default; |
| 91 | ~ReconnectHandler() = default; |
| 92 | |
| 93 | // Starts the ReconnectHandler thread. |
| 94 | void Start(); |
| 95 | |
| 96 | // Requests the ReconnectHandler thread to stop. |
| 97 | void Stop(); |
| 98 | |
| 99 | // Adds the atransport* to the queue of reconnect attempts. |
| 100 | void TrackTransport(atransport* transport); |
| 101 | |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 102 | // Wake up the ReconnectHandler thread to have it check for kicked transports. |
| 103 | void CheckForKicked(); |
| 104 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 105 | private: |
| 106 | // The main thread loop. |
| 107 | void Run(); |
| 108 | |
| 109 | // Tracks a reconnection attempt. |
| 110 | struct ReconnectAttempt { |
| 111 | atransport* transport; |
Josh Gao | c4deb39 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 112 | std::chrono::steady_clock::time_point reconnect_time; |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 113 | size_t attempts_left; |
Josh Gao | b018289 | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 114 | |
| 115 | bool operator<(const ReconnectAttempt& rhs) const { |
Josh Gao | d2ea065 | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 116 | if (reconnect_time == rhs.reconnect_time) { |
| 117 | return reinterpret_cast<uintptr_t>(transport) < |
| 118 | reinterpret_cast<uintptr_t>(rhs.transport); |
| 119 | } |
| 120 | return reconnect_time < rhs.reconnect_time; |
Josh Gao | b018289 | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 121 | } |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | // Only retry for up to one minute. |
Josh Gao | b018289 | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 125 | static constexpr const std::chrono::seconds kDefaultTimeout = 10s; |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 126 | static constexpr const size_t kMaxAttempts = 6; |
| 127 | |
| 128 | // Protects all members. |
| 129 | std::mutex reconnect_mutex_; |
| 130 | bool running_ GUARDED_BY(reconnect_mutex_) = true; |
| 131 | std::thread handler_thread_; |
| 132 | std::condition_variable reconnect_cv_; |
Josh Gao | d2ea065 | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 133 | std::set<ReconnectAttempt> reconnect_queue_ GUARDED_BY(reconnect_mutex_); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 134 | |
| 135 | DISALLOW_COPY_AND_ASSIGN(ReconnectHandler); |
| 136 | }; |
| 137 | |
| 138 | void ReconnectHandler::Start() { |
| 139 | check_main_thread(); |
| 140 | handler_thread_ = std::thread(&ReconnectHandler::Run, this); |
| 141 | } |
| 142 | |
| 143 | void ReconnectHandler::Stop() { |
| 144 | check_main_thread(); |
| 145 | { |
| 146 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 147 | running_ = false; |
| 148 | } |
| 149 | reconnect_cv_.notify_one(); |
| 150 | handler_thread_.join(); |
| 151 | |
| 152 | // Drain the queue to free all resources. |
| 153 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 154 | while (!reconnect_queue_.empty()) { |
Josh Gao | d2ea065 | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 155 | ReconnectAttempt attempt = *reconnect_queue_.begin(); |
| 156 | reconnect_queue_.erase(reconnect_queue_.begin()); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 157 | remove_transport(attempt.transport); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void ReconnectHandler::TrackTransport(atransport* transport) { |
| 162 | check_main_thread(); |
| 163 | { |
| 164 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 165 | if (!running_) return; |
Josh Gao | b018289 | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 166 | // Arbitrary sleep to give adbd time to get ready, if we disconnected because it exited. |
| 167 | auto reconnect_time = std::chrono::steady_clock::now() + 250ms; |
| 168 | reconnect_queue_.emplace( |
| 169 | ReconnectAttempt{transport, reconnect_time, ReconnectHandler::kMaxAttempts}); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 170 | } |
| 171 | reconnect_cv_.notify_one(); |
| 172 | } |
| 173 | |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 174 | void ReconnectHandler::CheckForKicked() { |
| 175 | reconnect_cv_.notify_one(); |
| 176 | } |
| 177 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 178 | void ReconnectHandler::Run() { |
| 179 | while (true) { |
| 180 | ReconnectAttempt attempt; |
| 181 | { |
| 182 | std::unique_lock<std::mutex> lock(reconnect_mutex_); |
| 183 | ScopedAssumeLocked assume_lock(reconnect_mutex_); |
| 184 | |
Josh Gao | c4deb39 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 185 | if (!reconnect_queue_.empty()) { |
| 186 | // FIXME: libstdc++ (used on Windows) implements condition_variable with |
| 187 | // system_clock as its clock, so we're probably hosed if the clock changes, |
| 188 | // even if we use steady_clock throughout. This problem goes away once we |
| 189 | // switch to libc++. |
Josh Gao | d2ea065 | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 190 | reconnect_cv_.wait_until(lock, reconnect_queue_.begin()->reconnect_time); |
Josh Gao | c4deb39 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 191 | } else { |
| 192 | reconnect_cv_.wait(lock); |
| 193 | } |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 194 | |
| 195 | if (!running_) return; |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 196 | |
| 197 | // Scan the whole list for kicked transports, so that we immediately handle an explicit |
| 198 | // disconnect request. |
| 199 | bool kicked = false; |
| 200 | for (auto it = reconnect_queue_.begin(); it != reconnect_queue_.end();) { |
| 201 | if (it->transport->kicked()) { |
| 202 | D("transport %s was kicked. giving up on it.", it->transport->serial.c_str()); |
| 203 | remove_transport(it->transport); |
| 204 | it = reconnect_queue_.erase(it); |
| 205 | } else { |
| 206 | ++it; |
| 207 | } |
| 208 | kicked = true; |
| 209 | } |
| 210 | |
Josh Gao | c4deb39 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 211 | if (reconnect_queue_.empty()) continue; |
| 212 | |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 213 | // Go back to sleep if we either woke up spuriously, or we were woken up to remove |
| 214 | // a kicked transport, and the first transport isn't ready for reconnection yet. |
Josh Gao | c4deb39 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 215 | auto now = std::chrono::steady_clock::now(); |
Josh Gao | d2ea065 | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 216 | if (reconnect_queue_.begin()->reconnect_time > now) { |
Josh Gao | c4deb39 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 217 | continue; |
| 218 | } |
| 219 | |
Josh Gao | d2ea065 | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 220 | attempt = *reconnect_queue_.begin(); |
| 221 | reconnect_queue_.erase(reconnect_queue_.begin()); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 222 | } |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 223 | D("attempting to reconnect %s", attempt.transport->serial.c_str()); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 224 | |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 225 | switch (attempt.transport->Reconnect()) { |
| 226 | case ReconnectResult::Retry: { |
| 227 | D("attempting to reconnect %s failed.", attempt.transport->serial.c_str()); |
| 228 | if (attempt.attempts_left == 0) { |
| 229 | D("transport %s exceeded the number of retry attempts. giving up on it.", |
| 230 | attempt.transport->serial.c_str()); |
| 231 | remove_transport(attempt.transport); |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 236 | reconnect_queue_.emplace(ReconnectAttempt{ |
| 237 | attempt.transport, |
| 238 | std::chrono::steady_clock::now() + ReconnectHandler::kDefaultTimeout, |
| 239 | attempt.attempts_left - 1}); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 240 | continue; |
| 241 | } |
| 242 | |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 243 | case ReconnectResult::Success: |
| 244 | D("reconnection to %s succeeded.", attempt.transport->serial.c_str()); |
| 245 | register_transport(attempt.transport); |
| 246 | continue; |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 247 | |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 248 | case ReconnectResult::Abort: |
| 249 | D("cancelling reconnection attempt to %s.", attempt.transport->serial.c_str()); |
| 250 | remove_transport(attempt.transport); |
| 251 | continue; |
| 252 | } |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | static auto& reconnect_handler = *new ReconnectHandler(); |
| 257 | |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 258 | #endif |
| 259 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 260 | } // namespace |
| 261 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 262 | TransportId NextTransportId() { |
| 263 | static std::atomic<TransportId> next(1); |
| 264 | return next++; |
| 265 | } |
| 266 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 267 | BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection) |
| 268 | : underlying_(std::move(connection)) {} |
| 269 | |
| 270 | BlockingConnectionAdapter::~BlockingConnectionAdapter() { |
| 271 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): destructing"; |
| 272 | Stop(); |
| 273 | } |
| 274 | |
| 275 | void BlockingConnectionAdapter::Start() { |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 276 | std::lock_guard<std::mutex> lock(mutex_); |
| 277 | if (started_) { |
| 278 | LOG(FATAL) << "BlockingConnectionAdapter(" << this->transport_name_ |
| 279 | << "): started multiple times"; |
| 280 | } |
| 281 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 282 | read_thread_ = std::thread([this]() { |
| 283 | LOG(INFO) << this->transport_name_ << ": read thread spawning"; |
| 284 | while (true) { |
Josh Gao | f2a988c | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 285 | auto packet = std::make_unique<apacket>(); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 286 | if (!underlying_->Read(packet.get())) { |
| 287 | PLOG(INFO) << this->transport_name_ << ": read failed"; |
| 288 | break; |
| 289 | } |
| 290 | read_callback_(this, std::move(packet)); |
| 291 | } |
| 292 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "read failed"); }); |
| 293 | }); |
| 294 | |
| 295 | write_thread_ = std::thread([this]() { |
| 296 | LOG(INFO) << this->transport_name_ << ": write thread spawning"; |
| 297 | while (true) { |
| 298 | std::unique_lock<std::mutex> lock(mutex_); |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 299 | ScopedAssumeLocked assume_locked(mutex_); |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 300 | cv_.wait(lock, [this]() REQUIRES(mutex_) { |
| 301 | return this->stopped_ || !this->write_queue_.empty(); |
| 302 | }); |
| 303 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 304 | if (this->stopped_) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | std::unique_ptr<apacket> packet = std::move(this->write_queue_.front()); |
| 309 | this->write_queue_.pop_front(); |
| 310 | lock.unlock(); |
| 311 | |
| 312 | if (!this->underlying_->Write(packet.get())) { |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); }); |
| 317 | }); |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 318 | |
| 319 | started_ = true; |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void BlockingConnectionAdapter::Stop() { |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 323 | { |
| 324 | std::lock_guard<std::mutex> lock(mutex_); |
| 325 | if (!started_) { |
| 326 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started"; |
| 327 | return; |
| 328 | } |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 329 | |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 330 | if (stopped_) { |
| 331 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ |
| 332 | << "): already stopped"; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | stopped_ = true; |
| 337 | } |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 338 | |
| 339 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping"; |
| 340 | |
| 341 | this->underlying_->Close(); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 342 | this->cv_.notify_one(); |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 343 | |
| 344 | // Move the threads out into locals with the lock taken, and then unlock to let them exit. |
| 345 | std::thread read_thread; |
| 346 | std::thread write_thread; |
| 347 | |
| 348 | { |
| 349 | std::lock_guard<std::mutex> lock(mutex_); |
| 350 | read_thread = std::move(read_thread_); |
| 351 | write_thread = std::move(write_thread_); |
| 352 | } |
| 353 | |
| 354 | read_thread.join(); |
| 355 | write_thread.join(); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 356 | |
| 357 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped"; |
| 358 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); }); |
| 359 | } |
| 360 | |
| 361 | bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) { |
| 362 | { |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 363 | std::lock_guard<std::mutex> lock(this->mutex_); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 364 | write_queue_.emplace_back(std::move(packet)); |
| 365 | } |
| 366 | |
| 367 | cv_.notify_one(); |
| 368 | return true; |
| 369 | } |
| 370 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 371 | bool FdConnection::Read(apacket* packet) { |
| 372 | if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) { |
| 373 | D("remote local: read terminated (message)"); |
| 374 | return false; |
| 375 | } |
| 376 | |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 377 | if (packet->msg.data_length > MAX_PAYLOAD) { |
Josh Gao | b14756a | 2018-02-02 14:38:04 -0800 | [diff] [blame] | 378 | D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length); |
| 379 | return false; |
| 380 | } |
| 381 | |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 382 | packet->payload.resize(packet->msg.data_length); |
| 383 | |
| 384 | if (!ReadFdExactly(fd_.get(), &packet->payload[0], packet->payload.size())) { |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 385 | D("remote local: terminated (data)"); |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | bool FdConnection::Write(apacket* packet) { |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 393 | if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(packet->msg))) { |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 394 | D("remote local: write terminated"); |
| 395 | return false; |
| 396 | } |
| 397 | |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 398 | if (packet->msg.data_length) { |
| 399 | if (!WriteFdExactly(fd_.get(), &packet->payload[0], packet->msg.data_length)) { |
| 400 | D("remote local: write terminated"); |
| 401 | return false; |
| 402 | } |
| 403 | } |
| 404 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 405 | return true; |
| 406 | } |
| 407 | |
| 408 | void FdConnection::Close() { |
| 409 | adb_shutdown(fd_.get()); |
| 410 | fd_.reset(); |
| 411 | } |
| 412 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 413 | void send_packet(apacket* p, atransport* t) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 414 | p->msg.magic = p->msg.command ^ 0xffffffff; |
Tim Murray | ee7b44d | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 415 | // compute a checksum for connection/auth packets for compatibility reasons |
| 416 | if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) { |
| 417 | p->msg.data_check = 0; |
| 418 | } else { |
| 419 | p->msg.data_check = calculate_apacket_checksum(p); |
| 420 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 422 | VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "to remote", p); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 424 | if (t == nullptr) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 425 | LOG(FATAL) << "Transport is null"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 428 | if (t->Write(p) != 0) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 429 | D("%s: failed to enqueue packet, closing transport", t->serial.c_str()); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 430 | t->Kick(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 434 | void kick_transport(atransport* t) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 435 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 436 | // As kick_transport() can be called from threads without guarantee that t is valid, |
| 437 | // check if the transport is in transport_list first. |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 438 | // |
| 439 | // TODO(jmgao): WTF? Is this actually true? |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 440 | if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) { |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 441 | t->Kick(); |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 442 | } |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 443 | |
| 444 | #if ADB_HOST |
| 445 | reconnect_handler.CheckForKicked(); |
| 446 | #endif |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 447 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | |
| 449 | static int transport_registration_send = -1; |
| 450 | static int transport_registration_recv = -1; |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 451 | static fdevent* transport_registration_fde; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 453 | #if ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 454 | |
| 455 | /* this adds support required by the 'track-devices' service. |
| 456 | * this is used to send the content of "list_transport" to any |
| 457 | * number of client connections that want it through a single |
| 458 | * live TCP connection |
| 459 | */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 460 | struct device_tracker { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 461 | asocket socket; |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 462 | bool update_needed = false; |
| 463 | bool long_output = false; |
| 464 | device_tracker* next = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | /* linked list of all device trackers */ |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 468 | static device_tracker* device_tracker_list; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 470 | static void device_tracker_remove(device_tracker* tracker) { |
| 471 | device_tracker** pnode = &device_tracker_list; |
| 472 | device_tracker* node = *pnode; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 473 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 474 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 475 | while (node) { |
| 476 | if (node == tracker) { |
| 477 | *pnode = node->next; |
| 478 | break; |
| 479 | } |
| 480 | pnode = &node->next; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 481 | node = *pnode; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 482 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 485 | static void device_tracker_close(asocket* socket) { |
| 486 | device_tracker* tracker = (device_tracker*)socket; |
| 487 | asocket* peer = socket->peer; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 488 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 489 | D("device tracker %p removed", tracker); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | if (peer) { |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 491 | peer->peer = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | peer->close(peer); |
| 493 | } |
| 494 | device_tracker_remove(tracker); |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 495 | delete tracker; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 498 | static int device_tracker_enqueue(asocket* socket, apacket::payload_type) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | /* you can't read from a device tracker, close immediately */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | device_tracker_close(socket); |
| 501 | return -1; |
| 502 | } |
| 503 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 504 | static int device_tracker_send(device_tracker* tracker, const std::string& string) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 505 | asocket* peer = tracker->socket.peer; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 506 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 507 | apacket::payload_type data; |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 508 | data.resize(4 + string.size()); |
| 509 | char buf[5]; |
| 510 | snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size())); |
| 511 | memcpy(&data[0], buf, 4); |
| 512 | memcpy(&data[4], string.data(), string.size()); |
| 513 | return peer->enqueue(peer, std::move(data)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 516 | static void device_tracker_ready(asocket* socket) { |
| 517 | device_tracker* tracker = reinterpret_cast<device_tracker*>(socket); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 518 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 519 | // We want to send the device list when the tracker connects |
| 520 | // for the first time, even if no update occurred. |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 521 | if (tracker->update_needed) { |
| 522 | tracker->update_needed = false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 524 | std::string transports = list_transports(tracker->long_output); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 525 | device_tracker_send(tracker, transports); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 529 | asocket* create_device_tracker(bool long_output) { |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 530 | device_tracker* tracker = new device_tracker(); |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 531 | if (tracker == nullptr) LOG(FATAL) << "cannot allocate device tracker"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 532 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 533 | D("device tracker %p created", tracker); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 534 | |
| 535 | tracker->socket.enqueue = device_tracker_enqueue; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 536 | tracker->socket.ready = device_tracker_ready; |
| 537 | tracker->socket.close = device_tracker_close; |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 538 | tracker->update_needed = true; |
| 539 | tracker->long_output = long_output; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 540 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 541 | tracker->next = device_tracker_list; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 542 | device_tracker_list = tracker; |
| 543 | |
| 544 | return &tracker->socket; |
| 545 | } |
| 546 | |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 547 | // Check if all of the USB transports are connected. |
| 548 | bool iterate_transports(std::function<bool(const atransport*)> fn) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 549 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 550 | for (const auto& t : transport_list) { |
| 551 | if (!fn(t)) { |
| 552 | return false; |
| 553 | } |
| 554 | } |
| 555 | for (const auto& t : pending_list) { |
| 556 | if (!fn(t)) { |
| 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | return true; |
| 561 | } |
| 562 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 563 | // Call this function each time the transport list has changed. |
| 564 | void update_transports() { |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 565 | update_transport_status(); |
| 566 | |
| 567 | // Notify `adb track-devices` clients. |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 568 | std::string transports = list_transports(false); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 569 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 570 | device_tracker* tracker = device_tracker_list; |
| 571 | while (tracker != nullptr) { |
| 572 | device_tracker* next = tracker->next; |
| 573 | // This may destroy the tracker if the connection is closed. |
| 574 | device_tracker_send(tracker, transports); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 575 | tracker = next; |
| 576 | } |
| 577 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 578 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | #else |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 580 | |
| 581 | void update_transports() { |
| 582 | // Nothing to do on the device side. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 583 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 584 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 585 | #endif // ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 586 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 587 | struct tmsg { |
| 588 | atransport* transport; |
| 589 | int action; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | }; |
| 591 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 592 | static int transport_read_action(int fd, struct tmsg* m) { |
| 593 | char* p = (char*)m; |
| 594 | int len = sizeof(*m); |
| 595 | int r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 596 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 597 | while (len > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | r = adb_read(fd, p, len); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 599 | if (r > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | len -= r; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 601 | p += r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 602 | } else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 603 | D("transport_read_action: on fd %d: %s", fd, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 604 | return -1; |
| 605 | } |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 610 | static int transport_write_action(int fd, struct tmsg* m) { |
| 611 | char* p = (char*)m; |
| 612 | int len = sizeof(*m); |
| 613 | int r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 614 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 615 | while (len > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 616 | r = adb_write(fd, p, len); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 617 | if (r > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 618 | len -= r; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 619 | p += r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 | } else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 621 | D("transport_write_action: on fd %d: %s", fd, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 622 | return -1; |
| 623 | } |
| 624 | } |
| 625 | return 0; |
| 626 | } |
| 627 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 628 | static void transport_registration_func(int _fd, unsigned ev, void*) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 629 | tmsg m; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 630 | atransport* t; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 631 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 632 | if (!(ev & FDE_READ)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 633 | return; |
| 634 | } |
| 635 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 636 | if (transport_read_action(_fd, &m)) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 637 | PLOG(FATAL) << "cannot read transport registration socket"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | t = m.transport; |
| 641 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 642 | if (m.action == 0) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 643 | D("transport: %s deleting", t->serial.c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 644 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 645 | { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 646 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 647 | transport_list.remove(t); |
| 648 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 649 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 650 | delete t; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 651 | |
| 652 | update_transports(); |
| 653 | return; |
| 654 | } |
| 655 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 656 | /* don't create transport threads for inaccessible devices */ |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 657 | if (t->GetConnectionState() != kCsNoPerm) { |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 658 | // The connection gets a reference to the atransport. It will release it |
| 659 | // upon a read/write error. |
| 660 | t->ref_count++; |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 661 | t->connection()->SetTransportName(t->serial_name()); |
| 662 | t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) { |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 663 | if (!check_header(p.get(), t)) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 664 | D("%s: remote read: bad header", t->serial.c_str()); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 665 | return false; |
| 666 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 667 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 668 | VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "from remote", p.get()); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 669 | apacket* packet = p.release(); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 670 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 671 | // TODO: Does this need to run on the main thread? |
| 672 | fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); }); |
| 673 | return true; |
| 674 | }); |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 675 | t->connection()->SetErrorCallback([t](Connection*, const std::string& error) { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 676 | LOG(INFO) << t->serial_name() << ": connection terminated: " << error; |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 677 | fdevent_run_on_main_thread([t]() { |
| 678 | handle_offline(t); |
| 679 | transport_unref(t); |
| 680 | }); |
| 681 | }); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 682 | |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 683 | t->connection()->Start(); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 684 | #if ADB_HOST |
| 685 | send_connect(t); |
| 686 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 689 | { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 690 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 691 | auto it = std::find(pending_list.begin(), pending_list.end(), t); |
| 692 | if (it != pending_list.end()) { |
| 693 | pending_list.remove(t); |
| 694 | transport_list.push_front(t); |
| 695 | } |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 696 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 697 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 698 | update_transports(); |
| 699 | } |
| 700 | |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 701 | #if ADB_HOST |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 702 | void init_reconnect_handler(void) { |
| 703 | reconnect_handler.Start(); |
| 704 | } |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 705 | #endif |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 706 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 707 | void init_transport_registration(void) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 708 | int s[2]; |
| 709 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 710 | if (adb_socketpair(s)) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 711 | PLOG(FATAL) << "cannot open transport registration socketpair"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 712 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 713 | D("socketpair: (%d,%d)", s[0], s[1]); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 714 | |
| 715 | transport_registration_send = s[0]; |
| 716 | transport_registration_recv = s[1]; |
| 717 | |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 718 | transport_registration_fde = |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 719 | fdevent_create(transport_registration_recv, transport_registration_func, nullptr); |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 720 | fdevent_set(transport_registration_fde, FDE_READ); |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | void kick_all_transports() { |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 724 | #if ADB_HOST |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 725 | reconnect_handler.Stop(); |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 726 | #endif |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 727 | // To avoid only writing part of a packet to a transport after exit, kick all transports. |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 728 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 729 | for (auto t : transport_list) { |
| 730 | t->Kick(); |
| 731 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* the fdevent select pump is single threaded */ |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 735 | void register_transport(atransport* transport) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 736 | tmsg m; |
| 737 | m.transport = transport; |
| 738 | m.action = 1; |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 739 | D("transport: %s registered", transport->serial.c_str()); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 740 | if (transport_write_action(transport_registration_send, &m)) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 741 | PLOG(FATAL) << "cannot write transport registration socket"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 745 | static void remove_transport(atransport* transport) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 746 | tmsg m; |
| 747 | m.transport = transport; |
| 748 | m.action = 0; |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 749 | D("transport: %s removed", transport->serial.c_str()); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 750 | if (transport_write_action(transport_registration_send, &m)) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 751 | PLOG(FATAL) << "cannot write transport registration socket"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 755 | static void transport_unref(atransport* t) { |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 756 | check_main_thread(); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 757 | CHECK(t != nullptr); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 758 | |
Josh Gao | 4d29974 | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 759 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
| 760 | CHECK_GT(t->ref_count, 0u); |
| 761 | t->ref_count--; |
| 762 | if (t->ref_count == 0) { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 763 | LOG(INFO) << "destroying transport " << t->serial_name(); |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 764 | t->connection()->Stop(); |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 765 | #if ADB_HOST |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 766 | if (t->IsTcpDevice() && !t->kicked()) { |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 767 | D("transport: %s unref (attempting reconnection)", t->serial.c_str()); |
Josh Gao | eb65652 | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 768 | |
| 769 | // We need to clear the transport's keys, so that on the next connection, it tries |
| 770 | // again from the beginning. |
| 771 | t->ResetKeys(); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 772 | reconnect_handler.TrackTransport(t); |
| 773 | } else { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 774 | D("transport: %s unref (kicking and closing)", t->serial.c_str()); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 775 | remove_transport(t); |
| 776 | } |
Josh Gao | 5c47e9f | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 777 | #else |
| 778 | D("transport: %s unref (kicking and closing)", t->serial.c_str()); |
| 779 | remove_transport(t); |
| 780 | #endif |
| 781 | |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 782 | } else { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 783 | D("transport: %s unref (count=%zu)", t->serial.c_str(), t->ref_count); |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 784 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 785 | } |
| 786 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 787 | static int qual_match(const std::string& to_test, const char* prefix, const std::string& qual, |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 788 | bool sanitize_qual) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 789 | if (to_test.empty()) /* Return true if both the qual and to_test are empty strings. */ |
| 790 | return qual.empty(); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 791 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 792 | if (qual.empty()) return 0; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 793 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 794 | const char* ptr = to_test.c_str(); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 795 | if (prefix) { |
| 796 | while (*prefix) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 797 | if (*prefix++ != *ptr++) return 0; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 801 | for (char ch : qual) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 802 | if (sanitize_qual && !isalnum(ch)) ch = '_'; |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 803 | if (ch != *ptr++) return 0; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 806 | /* Everything matched so far. Return true if *ptr is a NUL. */ |
| 807 | return !*ptr; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 808 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 809 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 810 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, |
| 811 | bool* is_ambiguous, std::string* error_out, |
| 812 | bool accept_any_state) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 813 | atransport* result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 814 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 815 | if (transport_id != 0) { |
| 816 | *error_out = |
| 817 | android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id); |
| 818 | } else if (serial) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 819 | *error_out = android::base::StringPrintf("device '%s' not found", serial); |
| 820 | } else if (type == kTransportLocal) { |
| 821 | *error_out = "no emulators found"; |
| 822 | } else if (type == kTransportAny) { |
| 823 | *error_out = "no devices/emulators found"; |
| 824 | } else { |
| 825 | *error_out = "no devices found"; |
| 826 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 827 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 828 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 829 | for (const auto& t : transport_list) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 830 | if (t->GetConnectionState() == kCsNoPerm) { |
David Pursell | 6e5c7eb | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 831 | *error_out = UsbNoPermissionsLongHelpText(); |
Mike Lockwood | adc16b3 | 2009-08-08 13:53:16 -0400 | [diff] [blame] | 832 | continue; |
| 833 | } |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 834 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 835 | if (transport_id) { |
| 836 | if (t->id == transport_id) { |
| 837 | result = t; |
| 838 | break; |
| 839 | } |
| 840 | } else if (serial) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 841 | if (t->MatchesTarget(serial)) { |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 842 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 843 | *error_out = "more than one device"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 844 | if (is_ambiguous) *is_ambiguous = true; |
| 845 | result = nullptr; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 846 | break; |
| 847 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 848 | result = t; |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 849 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 850 | } else { |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 851 | if (type == kTransportUsb && t->type == kTransportUsb) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 852 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 853 | *error_out = "more than one device"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 854 | if (is_ambiguous) *is_ambiguous = true; |
| 855 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 856 | break; |
| 857 | } |
| 858 | result = t; |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 859 | } else if (type == kTransportLocal && t->type == kTransportLocal) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 860 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 861 | *error_out = "more than one emulator"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 862 | if (is_ambiguous) *is_ambiguous = true; |
| 863 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | result = t; |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 867 | } else if (type == kTransportAny) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 868 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 869 | *error_out = "more than one device/emulator"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 870 | if (is_ambiguous) *is_ambiguous = true; |
| 871 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 872 | break; |
| 873 | } |
| 874 | result = t; |
| 875 | } |
| 876 | } |
| 877 | } |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 878 | lock.unlock(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 879 | |
Josh Gao | 7a7c5cb | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 880 | if (result && !accept_any_state) { |
| 881 | // The caller requires an active transport. |
| 882 | // Make sure that we're actually connected. |
| 883 | ConnectionState state = result->GetConnectionState(); |
| 884 | switch (state) { |
| 885 | case kCsConnecting: |
| 886 | *error_out = "device still connecting"; |
| 887 | result = nullptr; |
| 888 | break; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 889 | |
Josh Gao | 7a7c5cb | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 890 | case kCsAuthorizing: |
| 891 | *error_out = "device still authorizing"; |
| 892 | result = nullptr; |
| 893 | break; |
| 894 | |
| 895 | case kCsUnauthorized: { |
| 896 | *error_out = "device unauthorized.\n"; |
| 897 | char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); |
| 898 | *error_out += "This adb server's $ADB_VENDOR_KEYS is "; |
| 899 | *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set"; |
| 900 | *error_out += "\n"; |
| 901 | *error_out += "Try 'adb kill-server' if that seems wrong.\n"; |
| 902 | *error_out += "Otherwise check for a confirmation dialog on your device."; |
| 903 | result = nullptr; |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | case kCsOffline: |
| 908 | *error_out = "device offline"; |
| 909 | result = nullptr; |
| 910 | break; |
| 911 | |
| 912 | default: |
| 913 | break; |
| 914 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 918 | *error_out = "success"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | return result; |
| 922 | } |
| 923 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 924 | bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) { |
| 925 | std::unique_lock<std::mutex> lock(mutex_); |
| 926 | ScopedAssumeLocked assume_locked(mutex_); |
| 927 | return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) { |
| 928 | return connection_established_ready_; |
| 929 | }) && connection_established_; |
| 930 | } |
| 931 | |
| 932 | void ConnectionWaitable::SetConnectionEstablished(bool success) { |
| 933 | { |
| 934 | std::lock_guard<std::mutex> lock(mutex_); |
| 935 | if (connection_established_ready_) return; |
| 936 | connection_established_ready_ = true; |
| 937 | connection_established_ = success; |
| 938 | D("connection established with %d", success); |
| 939 | } |
| 940 | cv_.notify_one(); |
| 941 | } |
| 942 | |
| 943 | atransport::~atransport() { |
| 944 | // If the connection callback had not been run before, run it now. |
| 945 | SetConnectionEstablished(false); |
| 946 | } |
| 947 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 948 | int atransport::Write(apacket* p) { |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 949 | return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 952 | void atransport::Kick() { |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 953 | if (!kicked_.exchange(true)) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 954 | D("kicking transport %p %s", this, this->serial.c_str()); |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 955 | this->connection()->Stop(); |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 959 | ConnectionState atransport::GetConnectionState() const { |
| 960 | return connection_state_; |
| 961 | } |
| 962 | |
| 963 | void atransport::SetConnectionState(ConnectionState state) { |
| 964 | check_main_thread(); |
| 965 | connection_state_ = state; |
| 966 | } |
| 967 | |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 968 | void atransport::SetConnection(std::unique_ptr<Connection> connection) { |
| 969 | std::lock_guard<std::mutex> lock(mutex_); |
| 970 | connection_ = std::shared_ptr<Connection>(std::move(connection)); |
| 971 | } |
| 972 | |
Josh Gao | 3cd03be | 2018-02-28 14:44:23 -0800 | [diff] [blame] | 973 | std::string atransport::connection_state_name() const { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 974 | ConnectionState state = GetConnectionState(); |
| 975 | switch (state) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 976 | case kCsOffline: |
| 977 | return "offline"; |
| 978 | case kCsBootloader: |
| 979 | return "bootloader"; |
| 980 | case kCsDevice: |
| 981 | return "device"; |
| 982 | case kCsHost: |
| 983 | return "host"; |
| 984 | case kCsRecovery: |
| 985 | return "recovery"; |
| 986 | case kCsNoPerm: |
| 987 | return UsbNoPermissionsShortHelpText(); |
| 988 | case kCsSideload: |
| 989 | return "sideload"; |
| 990 | case kCsUnauthorized: |
| 991 | return "unauthorized"; |
Josh Gao | 7a7c5cb | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 992 | case kCsAuthorizing: |
| 993 | return "authorizing"; |
| 994 | case kCsConnecting: |
| 995 | return "connecting"; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 996 | default: |
| 997 | return "unknown"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 1001 | void atransport::update_version(int version, size_t payload) { |
| 1002 | protocol_version = std::min(version, A_VERSION); |
| 1003 | max_payload = std::min(payload, MAX_PAYLOAD); |
| 1004 | } |
| 1005 | |
| 1006 | int atransport::get_protocol_version() const { |
| 1007 | return protocol_version; |
| 1008 | } |
| 1009 | |
| 1010 | size_t atransport::get_max_payload() const { |
| 1011 | return max_payload; |
| 1012 | } |
| 1013 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1014 | const FeatureSet& supported_features() { |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1015 | // Local static allocation to avoid global non-POD variables. |
| 1016 | static const FeatureSet* features = new FeatureSet{ |
Alex Buynytskyy | e1fa814 | 2019-01-17 13:13:56 -0800 | [diff] [blame^] | 1017 | kFeatureShell2, kFeatureCmd, kFeatureStat2, |
| 1018 | kFeatureFixedPushMkdir, kFeatureApex, kFeatureAbb, |
Josh Gao | 62add5b | 2018-11-08 22:39:43 -0800 | [diff] [blame] | 1019 | // Increment ADB_SERVER_VERSION when adding a feature that adbd needs |
| 1020 | // to know about. Otherwise, the client can be stuck running an old |
| 1021 | // version of the server even after upgrading their copy of adb. |
| 1022 | // (http://b/24370690) |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1023 | }; |
| 1024 | |
| 1025 | return *features; |
| 1026 | } |
| 1027 | |
| 1028 | std::string FeatureSetToString(const FeatureSet& features) { |
Elliott Hughes | c3462f4 | 2018-09-05 12:13:11 -0700 | [diff] [blame] | 1029 | return android::base::Join(features, ','); |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | FeatureSet StringToFeatureSet(const std::string& features_string) { |
David Pursell | 3d9072b | 2015-09-25 13:04:21 -0700 | [diff] [blame] | 1033 | if (features_string.empty()) { |
| 1034 | return FeatureSet(); |
| 1035 | } |
| 1036 | |
Elliott Hughes | c3462f4 | 2018-09-05 12:13:11 -0700 | [diff] [blame] | 1037 | auto names = android::base::Split(features_string, ","); |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1038 | return FeatureSet(names.begin(), names.end()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 1041 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1042 | return feature_set.count(feature) > 0 && supported_features().count(feature) > 0; |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1045 | bool atransport::has_feature(const std::string& feature) const { |
| 1046 | return features_.count(feature) > 0; |
| 1047 | } |
| 1048 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1049 | void atransport::SetFeatures(const std::string& features_string) { |
| 1050 | features_ = StringToFeatureSet(features_string); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 1053 | void atransport::AddDisconnect(adisconnect* disconnect) { |
| 1054 | disconnects_.push_back(disconnect); |
| 1055 | } |
| 1056 | |
| 1057 | void atransport::RemoveDisconnect(adisconnect* disconnect) { |
| 1058 | disconnects_.remove(disconnect); |
| 1059 | } |
| 1060 | |
| 1061 | void atransport::RunDisconnects() { |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1062 | for (const auto& disconnect : disconnects_) { |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 1063 | disconnect->func(disconnect->opaque, this); |
| 1064 | } |
| 1065 | disconnects_.clear(); |
| 1066 | } |
| 1067 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1068 | bool atransport::MatchesTarget(const std::string& target) const { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1069 | if (!serial.empty()) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1070 | if (target == serial) { |
| 1071 | return true; |
| 1072 | } else if (type == kTransportLocal) { |
| 1073 | // Local transports can match [tcp:|udp:]<hostname>[:port]. |
| 1074 | const char* local_target_ptr = target.c_str(); |
| 1075 | |
| 1076 | // For fastboot compatibility, ignore protocol prefixes. |
| 1077 | if (android::base::StartsWith(target, "tcp:") || |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1078 | android::base::StartsWith(target, "udp:")) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1079 | local_target_ptr += 4; |
| 1080 | } |
| 1081 | |
| 1082 | // Parse our |serial| and the given |target| to check if the hostnames and ports match. |
| 1083 | std::string serial_host, error; |
| 1084 | int serial_port = -1; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1085 | if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1086 | // |target| may omit the port to default to ours. |
| 1087 | std::string target_host; |
| 1088 | int target_port = serial_port; |
| 1089 | if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port, |
| 1090 | nullptr, &error) && |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1091 | serial_host == target_host && serial_port == target_port) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1092 | return true; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1098 | return (target == devpath) || qual_match(target, "product:", product, false) || |
| 1099 | qual_match(target, "model:", model, true) || |
| 1100 | qual_match(target, "device:", device, false); |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1103 | void atransport::SetConnectionEstablished(bool success) { |
| 1104 | connection_waitable_->SetConnectionEstablished(success); |
| 1105 | } |
| 1106 | |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 1107 | ReconnectResult atransport::Reconnect() { |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1108 | return reconnect_(this); |
| 1109 | } |
| 1110 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1111 | #if ADB_HOST |
| 1112 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1113 | // We use newline as our delimiter, make sure to never output it. |
| 1114 | static std::string sanitize(std::string str, bool alphanumeric) { |
| 1115 | auto pred = alphanumeric ? [](const char c) { return !isalnum(c); } |
| 1116 | : [](const char c) { return c == '\n'; }; |
| 1117 | std::replace_if(str.begin(), str.end(), pred, '_'); |
| 1118 | return str; |
| 1119 | } |
| 1120 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1121 | static void append_transport_info(std::string* result, const char* key, const std::string& value, |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1122 | bool alphanumeric) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1123 | if (value.empty()) { |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1124 | return; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1127 | *result += ' '; |
| 1128 | *result += key; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1129 | *result += sanitize(value, alphanumeric); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1132 | static void append_transport(const atransport* t, std::string* result, bool long_listing) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1133 | std::string serial = t->serial; |
| 1134 | if (serial.empty()) { |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 1135 | serial = "(no serial number)"; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1136 | } |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (!long_listing) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1139 | *result += serial; |
| 1140 | *result += '\t'; |
| 1141 | *result += t->connection_state_name(); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1142 | } else { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1143 | android::base::StringAppendF(result, "%-22s %s", serial.c_str(), |
| 1144 | t->connection_state_name().c_str()); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1145 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1146 | append_transport_info(result, "", t->devpath, false); |
| 1147 | append_transport_info(result, "product:", t->product, false); |
| 1148 | append_transport_info(result, "model:", t->model, true); |
| 1149 | append_transport_info(result, "device:", t->device, false); |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1150 | |
| 1151 | // Put id at the end, so that anyone parsing the output here can always find it by scanning |
| 1152 | // backwards from newlines, even with hypothetical devices named 'transport_id:1'. |
| 1153 | *result += " transport_id:"; |
| 1154 | *result += std::to_string(t->id); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1155 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1156 | *result += '\n'; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1159 | std::string list_transports(bool long_listing) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1160 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Artem Iglikov | 4dd3501 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 1161 | |
| 1162 | auto sorted_transport_list = transport_list; |
| 1163 | sorted_transport_list.sort([](atransport*& x, atransport*& y) { |
| 1164 | if (x->type != y->type) { |
| 1165 | return x->type < y->type; |
| 1166 | } |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1167 | return x->serial < y->serial; |
Artem Iglikov | 4dd3501 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 1168 | }); |
| 1169 | |
| 1170 | std::string result; |
| 1171 | for (const auto& t : sorted_transport_list) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1172 | append_transport(t, &result, long_listing); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1173 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1174 | return result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1177 | void close_usb_devices(std::function<bool(const atransport*)> predicate) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1178 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1179 | for (auto& t : transport_list) { |
| 1180 | if (predicate(t)) { |
| 1181 | t->Kick(); |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1186 | /* hack for osx */ |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1187 | void close_usb_devices() { |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1188 | close_usb_devices([](const atransport*) { return true; }); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1189 | } |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1190 | #endif // ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1191 | |
Josh Gao | 597044d | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1192 | bool register_socket_transport(unique_fd s, std::string serial, int port, int local, |
| 1193 | atransport::ReconnectCallback reconnect, int* error) { |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1194 | atransport* t = new atransport(std::move(reconnect), kCsOffline); |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 1195 | |
Josh Gao | a3a7147 | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1196 | D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port); |
Josh Gao | fa3107e | 2018-07-25 17:21:49 -0700 | [diff] [blame] | 1197 | if (init_socket_transport(t, std::move(s), port, local) < 0) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1198 | delete t; |
Josh Gao | 597044d | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1199 | if (error) *error = errno; |
| 1200 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1201 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1202 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1203 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1204 | for (const auto& transport : pending_list) { |
Josh Gao | a3a7147 | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1205 | if (serial == transport->serial) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1206 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1207 | << " is already in pending_list and fails to register"; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1208 | delete t; |
Josh Gao | 597044d | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1209 | if (error) *error = EALREADY; |
| 1210 | return false; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1211 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1212 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1213 | |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1214 | for (const auto& transport : transport_list) { |
Josh Gao | a3a7147 | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1215 | if (serial == transport->serial) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1216 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1217 | << " is already in transport_list and fails to register"; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1218 | delete t; |
Josh Gao | 597044d | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1219 | if (error) *error = EALREADY; |
| 1220 | return false; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
Josh Gao | a3a7147 | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1224 | t->serial = std::move(serial); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1225 | pending_list.push_front(t); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1226 | |
| 1227 | lock.unlock(); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1228 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1229 | auto waitable = t->connection_waitable(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1230 | register_transport(t); |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1231 | |
Luis Hector Chavez | 25bc41c | 2018-05-01 17:12:16 -0700 | [diff] [blame] | 1232 | if (local == 1) { |
| 1233 | // Do not wait for emulator transports. |
Josh Gao | 597044d | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1234 | return true; |
Luis Hector Chavez | 25bc41c | 2018-05-01 17:12:16 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Josh Gao | 597044d | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1237 | if (!waitable->WaitForConnection(std::chrono::seconds(10))) { |
| 1238 | if (error) *error = ETIMEDOUT; |
| 1239 | return false; |
| 1240 | } |
| 1241 | |
| 1242 | if (t->GetConnectionState() == kCsUnauthorized) { |
| 1243 | if (error) *error = EPERM; |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1250 | #if ADB_HOST |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1251 | atransport* find_transport(const char* serial) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1252 | atransport* result = nullptr; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1253 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1254 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1255 | for (auto& t : transport_list) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1256 | if (strcmp(serial, t->serial.c_str()) == 0) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1257 | result = t; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1258 | break; |
| 1259 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1260 | } |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1261 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1262 | return result; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1263 | } |
| 1264 | |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1265 | void kick_all_tcp_devices() { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1266 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1267 | for (auto& t : transport_list) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1268 | if (t->IsTcpDevice()) { |
Yabin Cui | 4e22229 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 1269 | // Kicking breaks the read_transport thread of this transport out of any read, then |
| 1270 | // the read_transport thread will notify the main thread to make this transport |
| 1271 | // offline. Then the main thread will notify the write_transport thread to exit. |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1272 | // Finally, this transport will be closed and freed in the main thread. |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 1273 | t->Kick(); |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1274 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1275 | } |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 1276 | #if ADB_HOST |
| 1277 | reconnect_handler.CheckForKicked(); |
| 1278 | #endif |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1281 | #endif |
| 1282 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1283 | void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath, |
| 1284 | unsigned writeable) { |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1285 | atransport* t = new atransport(writeable ? kCsOffline : kCsNoPerm); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1286 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1287 | D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : ""); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1288 | init_usb_transport(t, usb); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1289 | if (serial) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1290 | t->serial = serial; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1291 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (devpath) { |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1294 | t->devpath = devpath; |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1295 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1296 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1297 | { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1298 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1299 | pending_list.push_front(t); |
| 1300 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1301 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1302 | register_transport(t); |
| 1303 | } |
| 1304 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 1305 | #if ADB_HOST |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 1306 | // This should only be used for transports with connection_state == kCsNoPerm. |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1307 | void unregister_usb_transport(usb_handle* usb) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1308 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1309 | transport_list.remove_if([usb](atransport* t) { |
Josh Gao | 68f2c38 | 2018-12-11 13:11:52 -0800 | [diff] [blame] | 1310 | return t->GetUsbHandle() == usb && t->GetConnectionState() == kCsNoPerm; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1311 | }); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 1312 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 1313 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1314 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1315 | bool check_header(apacket* p, atransport* t) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1316 | if (p->msg.magic != (p->msg.command ^ 0xffffffff)) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1317 | VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command |
| 1318 | << ", magic = " << p->msg.magic; |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1319 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1322 | if (p->msg.data_length > t->get_max_payload()) { |
| 1323 | VLOG(RWX) << "check_header(): " << p->msg.data_length |
| 1324 | << " atransport::max_payload = " << t->get_max_payload(); |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1325 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1328 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1331 | #if ADB_HOST |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1332 | std::shared_ptr<RSA> atransport::NextKey() { |
Josh Gao | eb65652 | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 1333 | if (keys_.empty()) { |
| 1334 | LOG(INFO) << "fetching keys for transport " << this->serial_name(); |
| 1335 | keys_ = adb_auth_get_private_keys(); |
| 1336 | |
| 1337 | // We should have gotten at least one key: the one that's automatically generated. |
| 1338 | CHECK(!keys_.empty()); |
| 1339 | } |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1340 | |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1341 | std::shared_ptr<RSA> result = keys_[0]; |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1342 | keys_.pop_front(); |
| 1343 | return result; |
| 1344 | } |
Josh Gao | eb65652 | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 1345 | |
| 1346 | void atransport::ResetKeys() { |
| 1347 | keys_.clear(); |
| 1348 | } |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1349 | #endif |