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