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