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