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 | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "transport.h" |
| 21 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Pirama Arumuga Nainar | 7eaef8a | 2016-09-06 13:34:42 -0700 | [diff] [blame] | 28 | #include <condition_variable> |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 29 | #include <mutex> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 30 | #include <thread> |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 31 | #include <unordered_map> |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 34 | #include <android-base/parsenetaddress.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 36 | #include <android-base/thread_annotations.h> |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 38 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 39 | #if !ADB_HOST |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 41 | #endif |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 42 | |
| 43 | #include "adb.h" |
| 44 | #include "adb_io.h" |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 45 | #include "adb_unique_fd.h" |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 46 | #include "adb_utils.h" |
Josh Gao | 4602adb | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 47 | #include "sysdeps/chrono.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | #if ADB_HOST |
Josh Gao | 9fe7426 | 2016-05-06 02:37:24 -0700 | [diff] [blame] | 50 | |
| 51 | // Android Wear has been using port 5601 in all of its documentation/tooling, |
| 52 | // but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX]. |
| 53 | // Avoid stomping on their port by limiting the number of emulators that can be |
| 54 | // connected. |
| 55 | #define ADB_LOCAL_TRANSPORT_MAX 16 |
| 56 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 57 | static std::mutex& local_transports_lock = *new std::mutex(); |
Josh Gao | 9fe7426 | 2016-05-06 02:37:24 -0700 | [diff] [blame] | 58 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 59 | // We keep a map from emulator port to transport. |
| 60 | // TODO: weak_ptr? |
| 61 | static auto& local_transports GUARDED_BY(local_transports_lock) = |
| 62 | *new std::unordered_map<int, atransport*>(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | #endif /* ADB_HOST */ |
| 64 | |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 65 | bool local_connect(int port) { |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 66 | std::string dummy; |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 67 | return local_connect_arbitrary_ports(port - 1, port, &dummy) == 0; |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 70 | void connect_device(const std::string& address, std::string* response) { |
| 71 | if (address.empty()) { |
| 72 | *response = "empty address"; |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | std::string serial; |
| 77 | std::string host; |
| 78 | int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; |
| 79 | if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | std::string error; |
| 84 | int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error); |
| 85 | if (fd == -1) { |
| 86 | *response = android::base::StringPrintf("unable to connect to %s: %s", |
| 87 | serial.c_str(), error.c_str()); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | D("client: connected %s remote on fd %d", serial.c_str(), fd); |
| 92 | close_on_exec(fd); |
| 93 | disable_tcp_nagle(fd); |
| 94 | |
| 95 | // Send a TCP keepalive ping to the device every second so we can detect disconnects. |
| 96 | if (!set_tcp_keepalive(fd, 1)) { |
| 97 | D("warning: failed to configure TCP keepalives (%s)", strerror(errno)); |
| 98 | } |
| 99 | |
| 100 | int ret = register_socket_transport(fd, serial.c_str(), port, 0); |
| 101 | if (ret < 0) { |
| 102 | adb_close(fd); |
| 103 | *response = android::base::StringPrintf("already connected to %s", serial.c_str()); |
| 104 | } else { |
| 105 | *response = android::base::StringPrintf("connected to %s", serial.c_str()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 110 | int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) { |
| 111 | int fd = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | |
| 113 | #if ADB_HOST |
Lingfeng Yang | 1197952 | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 114 | if (find_emulator_transport_by_adb_port(adb_port) != nullptr || |
| 115 | find_emulator_transport_by_console_port(console_port) != nullptr) { |
Yabin Cui | 0e2c194 | 2015-07-31 14:10:54 -0700 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | const char *host = getenv("ADBHOST"); |
| 120 | if (host) { |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 121 | fd = network_connect(host, adb_port, SOCK_STREAM, 0, error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } |
| 123 | #endif |
| 124 | if (fd < 0) { |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 125 | fd = network_loopback_client(adb_port, SOCK_STREAM, error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (fd >= 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 129 | D("client: connected on remote on fd %d", fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | close_on_exec(fd); |
| 131 | disable_tcp_nagle(fd); |
Lingfeng Yang | 1197952 | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 132 | std::string serial = getEmulatorSerialString(console_port); |
Yabin Cui | 0e2c194 | 2015-07-31 14:10:54 -0700 | [diff] [blame] | 133 | if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) { |
| 134 | return 0; |
| 135 | } |
| 136 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | } |
| 138 | return -1; |
| 139 | } |
| 140 | |
Yabin Cui | 661327e | 2015-08-11 13:40:42 -0700 | [diff] [blame] | 141 | #if ADB_HOST |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 142 | |
| 143 | static void PollAllLocalPortsForEmulator() { |
| 144 | int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; |
| 145 | int count = ADB_LOCAL_TRANSPORT_MAX; |
| 146 | |
| 147 | // Try to connect to any number of running emulator instances. |
| 148 | for ( ; count > 0; count--, port += 2 ) { |
| 149 | local_connect(port); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Retry the disconnected local port for 60 times, and sleep 1 second between two retries. |
| 154 | constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60; |
Josh Gao | 4602adb | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 155 | constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s; |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 156 | |
| 157 | struct RetryPort { |
| 158 | int port; |
| 159 | uint32_t retry_count; |
| 160 | }; |
| 161 | |
| 162 | // Retry emulators just kicked. |
| 163 | static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>; |
| 164 | std::mutex &retry_ports_lock = *new std::mutex; |
| 165 | std::condition_variable &retry_ports_cond = *new std::condition_variable; |
| 166 | |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 167 | static void client_socket_thread(int) { |
Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 168 | adb_thread_setname("client_socket_thread"); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 169 | D("transport: client_socket_thread() starting"); |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 170 | PollAllLocalPortsForEmulator(); |
Yabin Cui | 0e2c194 | 2015-07-31 14:10:54 -0700 | [diff] [blame] | 171 | while (true) { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 172 | std::vector<RetryPort> ports; |
| 173 | // Collect retry ports. |
| 174 | { |
| 175 | std::unique_lock<std::mutex> lock(retry_ports_lock); |
| 176 | while (retry_ports.empty()) { |
| 177 | retry_ports_cond.wait(lock); |
| 178 | } |
| 179 | retry_ports.swap(ports); |
Yabin Cui | 0e2c194 | 2015-07-31 14:10:54 -0700 | [diff] [blame] | 180 | } |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 181 | // Sleep here instead of the end of loop, because if we immediately try to reconnect |
| 182 | // the emulator just kicked, the adbd on the emulator may not have time to remove the |
| 183 | // just kicked transport. |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 184 | std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL); |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 185 | |
| 186 | // Try connecting retry ports. |
| 187 | std::vector<RetryPort> next_ports; |
| 188 | for (auto& port : ports) { |
| 189 | VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count " |
| 190 | << port.retry_count; |
| 191 | if (local_connect(port.port)) { |
| 192 | VLOG(TRANSPORT) << "retry port " << port.port << " successfully"; |
| 193 | continue; |
| 194 | } |
| 195 | if (--port.retry_count > 0) { |
| 196 | next_ports.push_back(port); |
| 197 | } else { |
| 198 | VLOG(TRANSPORT) << "stop retrying port " << port.port; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Copy back left retry ports. |
| 203 | { |
| 204 | std::unique_lock<std::mutex> lock(retry_ports_lock); |
| 205 | retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end()); |
| 206 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Yabin Cui | 661327e | 2015-08-11 13:40:42 -0700 | [diff] [blame] | 210 | #else // ADB_HOST |
| 211 | |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 212 | static void server_socket_thread(int port) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | int serverfd, fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | |
Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 215 | adb_thread_setname("server socket"); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 216 | D("transport: server_socket_thread() starting"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 | serverfd = -1; |
| 218 | for(;;) { |
| 219 | if(serverfd == -1) { |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 220 | std::string error; |
| 221 | serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 222 | if(serverfd < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 223 | D("server: cannot bind socket yet: %s", error.c_str()); |
Josh Gao | 4602adb | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 224 | std::this_thread::sleep_for(1s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | continue; |
| 226 | } |
| 227 | close_on_exec(serverfd); |
| 228 | } |
| 229 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 230 | D("server: trying to get new connection from %d", port); |
Josh Gao | 78e1eb1 | 2016-08-23 15:41:56 -0700 | [diff] [blame] | 231 | fd = adb_socket_accept(serverfd, nullptr, nullptr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 232 | if(fd >= 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 233 | D("server: new connection on fd %d", fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | close_on_exec(fd); |
| 235 | disable_tcp_nagle(fd); |
Tao Wu | 043912e | 2016-10-21 15:43:57 -0700 | [diff] [blame] | 236 | std::string serial = android::base::StringPrintf("host-%d", fd); |
| 237 | if (register_socket_transport(fd, serial.c_str(), port, 1) != 0) { |
Yabin Cui | 0140182 | 2016-05-17 15:14:25 -0700 | [diff] [blame] | 238 | adb_close(fd); |
| 239 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 242 | D("transport: server_socket_thread() exiting"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Vladimir Chtchetkine | c4f37ee | 2011-12-13 12:19:29 -0800 | [diff] [blame] | 245 | /* This is relevant only for ADB daemon running inside the emulator. */ |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 246 | /* |
| 247 | * Redefine open and write for qemu_pipe.h that contains inlined references |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 248 | * to those routines. We will redefine them back after qemu_pipe.h inclusion. |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 249 | */ |
| 250 | #undef open |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 251 | #undef read |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 252 | #undef write |
| 253 | #define open adb_open |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 254 | #define read adb_read |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 255 | #define write adb_write |
bohu | f66a7ee | 2017-03-29 12:19:40 -0700 | [diff] [blame] | 256 | #include <qemu_pipe.h> |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 257 | #undef open |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 258 | #undef read |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 259 | #undef write |
| 260 | #define open ___xxx_open |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 261 | #define read ___xxx_read |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 262 | #define write ___xxx_write |
| 263 | |
| 264 | /* A worker thread that monitors host connections, and registers a transport for |
| 265 | * every new host connection. This thread replaces server_socket_thread on |
| 266 | * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD |
| 267 | * pipe to communicate with adbd daemon inside the guest. This is done in order |
| 268 | * to provide more robust communication channel between ADB host and guest. The |
| 269 | * main issue with server_socket_thread approach is that it runs on top of TCP, |
| 270 | * and thus is sensitive to network disruptions. For instance, the |
| 271 | * ConnectionManager may decide to reset all network connections, in which case |
| 272 | * the connection between ADB host and guest will be lost. To make ADB traffic |
| 273 | * independent from the network, we use here 'adb' QEMUD service to transfer data |
| 274 | * between the host, and the guest. See external/qemu/android/adb-*.* that |
| 275 | * implements the emulator's side of the protocol. Another advantage of using |
| 276 | * QEMUD approach is that ADB will be up much sooner, since it doesn't depend |
| 277 | * anymore on network being set up. |
| 278 | * The guest side of the protocol contains the following phases: |
| 279 | * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service |
| 280 | * is opened, and it becomes clear whether or not emulator supports that |
| 281 | * protocol. |
| 282 | * - Wait for the ADB host to create connection with the guest. This is done by |
| 283 | * sending an 'accept' request to the adb QEMUD service, and waiting on |
| 284 | * response. |
| 285 | * - When new ADB host connection is accepted, the connection with adb QEMUD |
| 286 | * service is registered as the transport, and a 'start' request is sent to the |
| 287 | * adb QEMUD service, indicating that the guest is ready to receive messages. |
| 288 | * Note that the guest will ignore messages sent down from the emulator before |
| 289 | * the transport registration is completed. That's why we need to send the |
| 290 | * 'start' request after the transport is registered. |
| 291 | */ |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 292 | static void qemu_socket_thread(int port) { |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 293 | /* 'accept' request to the adb QEMUD service. */ |
| 294 | static const char _accept_req[] = "accept"; |
| 295 | /* 'start' request to the adb QEMUD service. */ |
| 296 | static const char _start_req[] = "start"; |
| 297 | /* 'ok' reply from the adb QEMUD service. */ |
| 298 | static const char _ok_resp[] = "ok"; |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 299 | |
bohu | 8ac1b04 | 2016-02-29 13:13:19 -0800 | [diff] [blame] | 300 | int fd; |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 301 | char tmp[256]; |
| 302 | char con_name[32]; |
| 303 | |
Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 304 | adb_thread_setname("qemu socket"); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 305 | D("transport: qemu_socket_thread() starting"); |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 306 | |
| 307 | /* adb QEMUD service connection request. */ |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 308 | snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port); |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 309 | |
| 310 | /* Connect to the adb QEMUD service. */ |
| 311 | fd = qemu_pipe_open(con_name); |
| 312 | if (fd < 0) { |
| 313 | /* This could be an older version of the emulator, that doesn't |
| 314 | * implement adb QEMUD service. Fall back to the old TCP way. */ |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 315 | D("adb service is not available. Falling back to TCP socket."); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 316 | std::thread(server_socket_thread, port).detach(); |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 317 | return; |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | for(;;) { |
| 321 | /* |
| 322 | * Wait till the host creates a new connection. |
| 323 | */ |
| 324 | |
| 325 | /* Send the 'accept' request. */ |
bohu | 8ac1b04 | 2016-02-29 13:13:19 -0800 | [diff] [blame] | 326 | if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) { |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 327 | /* Wait for the response. In the response we expect 'ok' on success, |
| 328 | * or 'ko' on failure. */ |
bohu | 8ac1b04 | 2016-02-29 13:13:19 -0800 | [diff] [blame] | 329 | if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 330 | D("Accepting ADB host connection has failed."); |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 331 | adb_close(fd); |
| 332 | } else { |
| 333 | /* Host is connected. Register the transport, and start the |
| 334 | * exchange. */ |
Prathmesh Prabhu | 251d46e | 2016-03-09 13:51:30 -0800 | [diff] [blame] | 335 | std::string serial = android::base::StringPrintf("host-%d", fd); |
Yabin Cui | 0140182 | 2016-05-17 15:14:25 -0700 | [diff] [blame] | 336 | if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 || |
| 337 | !WriteFdExactly(fd, _start_req, strlen(_start_req))) { |
bohu | 8ac1b04 | 2016-02-29 13:13:19 -0800 | [diff] [blame] | 338 | adb_close(fd); |
| 339 | } |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* Prepare for accepting of the next ADB host connection. */ |
| 343 | fd = qemu_pipe_open(con_name); |
| 344 | if (fd < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 345 | D("adb service become unavailable."); |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 346 | return; |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 347 | } |
| 348 | } else { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 349 | D("Unable to send the '%s' request to ADB service.", _accept_req); |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 350 | return; |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 353 | D("transport: qemu_socket_thread() exiting"); |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 354 | return; |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 355 | } |
Nicolas Norvez | 717f153 | 2017-06-06 13:38:20 -0700 | [diff] [blame] | 356 | |
| 357 | // If adbd is running inside the emulator, it will normally use QEMUD pipe (aka |
| 358 | // goldfish) as the transport. This can either be explicitly set by the |
| 359 | // service.adb.transport property, or be inferred from ro.kernel.qemu that is |
| 360 | // set to "1" for ranchu/goldfish. |
| 361 | static bool use_qemu_goldfish() { |
| 362 | // Legacy way to detect if adbd should use the goldfish pipe is to check for |
| 363 | // ro.kernel.qemu, keep that behaviour for backward compatibility. |
| 364 | if (android::base::GetBoolProperty("ro.kernel.qemu", false)) { |
| 365 | return true; |
| 366 | } |
| 367 | // If service.adb.transport is present and is set to "goldfish", use the |
| 368 | // QEMUD pipe. |
| 369 | if (android::base::GetProperty("service.adb.transport", "") == "goldfish") { |
| 370 | return true; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
Vladimir Chtchetkine | c4f37ee | 2011-12-13 12:19:29 -0800 | [diff] [blame] | 375 | #endif // !ADB_HOST |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 376 | |
Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 377 | void local_init(int port) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | { |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 379 | void (*func)(int); |
Yabin Cui | 661327e | 2015-08-11 13:40:42 -0700 | [diff] [blame] | 380 | const char* debug_name = ""; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 381 | |
Vladimir Chtchetkine | c4f37ee | 2011-12-13 12:19:29 -0800 | [diff] [blame] | 382 | #if ADB_HOST |
Yabin Cui | 661327e | 2015-08-11 13:40:42 -0700 | [diff] [blame] | 383 | func = client_socket_thread; |
| 384 | debug_name = "client"; |
Vladimir Chtchetkine | c4f37ee | 2011-12-13 12:19:29 -0800 | [diff] [blame] | 385 | #else |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 386 | // For the adbd daemon in the system image we need to distinguish |
| 387 | // between the device, and the emulator. |
Nicolas Norvez | 717f153 | 2017-06-06 13:38:20 -0700 | [diff] [blame] | 388 | func = use_qemu_goldfish() ? qemu_socket_thread : server_socket_thread; |
Yabin Cui | 661327e | 2015-08-11 13:40:42 -0700 | [diff] [blame] | 389 | debug_name = "server"; |
| 390 | #endif // !ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 392 | D("transport: local %s init", debug_name); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 393 | std::thread(func, port).detach(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 394 | } |
| 395 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 396 | #if ADB_HOST |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 397 | struct EmulatorConnection : public FdConnection { |
| 398 | EmulatorConnection(unique_fd fd, int local_port) |
| 399 | : FdConnection(std::move(fd)), local_port_(local_port) {} |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 401 | ~EmulatorConnection() { |
| 402 | VLOG(TRANSPORT) << "remote_close, local_port = " << local_port_; |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 403 | std::unique_lock<std::mutex> lock(retry_ports_lock); |
| 404 | RetryPort port; |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 405 | port.port = local_port_; |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 406 | port.retry_count = LOCAL_PORT_RETRY_COUNT; |
| 407 | retry_ports.push_back(port); |
| 408 | retry_ports_cond.notify_one(); |
| 409 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 410 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 411 | void Close() override { |
| 412 | std::lock_guard<std::mutex> lock(local_transports_lock); |
| 413 | local_transports.erase(local_port_); |
| 414 | FdConnection::Close(); |
| 415 | } |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 416 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 417 | int local_port_; |
| 418 | }; |
| 419 | |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 420 | /* Only call this function if you already hold local_transports_lock. */ |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 421 | static atransport* find_emulator_transport_by_adb_port_locked(int adb_port) |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 422 | REQUIRES(local_transports_lock) { |
| 423 | auto it = local_transports.find(adb_port); |
| 424 | if (it == local_transports.end()) { |
| 425 | return nullptr; |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 426 | } |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 427 | return it->second; |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 430 | std::string getEmulatorSerialString(int console_port) { |
Lingfeng Yang | 1197952 | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 431 | return android::base::StringPrintf("emulator-%d", console_port); |
| 432 | } |
| 433 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 434 | atransport* find_emulator_transport_by_adb_port(int adb_port) { |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 435 | std::lock_guard<std::mutex> lock(local_transports_lock); |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 436 | return find_emulator_transport_by_adb_port_locked(adb_port); |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 439 | atransport* find_emulator_transport_by_console_port(int console_port) { |
Lingfeng Yang | 1197952 | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 440 | return find_transport(getEmulatorSerialString(console_port).c_str()); |
| 441 | } |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 442 | #endif |
| 443 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 444 | int init_socket_transport(atransport* t, int s, int adb_port, int local) { |
| 445 | int fail = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 446 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 447 | unique_fd fd(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | t->sync_token = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 449 | t->type = kTransportLocal; |
| 450 | |
| 451 | #if ADB_HOST |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 452 | // Emulator connection. |
Yabin Cui | 661327e | 2015-08-11 13:40:42 -0700 | [diff] [blame] | 453 | if (local) { |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 454 | t->connection.reset(new EmulatorConnection(std::move(fd), adb_port)); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 455 | std::lock_guard<std::mutex> lock(local_transports_lock); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 456 | atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 457 | if (existing_transport != NULL) { |
| 458 | D("local transport for port %d already registered (%p)?", adb_port, existing_transport); |
| 459 | fail = -1; |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 460 | } else if (local_transports.size() >= ADB_LOCAL_TRANSPORT_MAX) { |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 461 | // Too many emulators. |
| 462 | D("cannot register more emulators. Maximum is %d", ADB_LOCAL_TRANSPORT_MAX); |
| 463 | fail = -1; |
| 464 | } else { |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 465 | local_transports[adb_port] = t; |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 466 | } |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 467 | |
| 468 | return fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | } |
| 470 | #endif |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 471 | |
| 472 | // Regular tcp connection. |
| 473 | t->connection.reset(new FdConnection(std::move(fd))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 474 | return fail; |
| 475 | } |