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