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