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