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