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