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