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