blob: d2a375a24235e0c1bbbba6b78d90076401120ecd [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "transport.h"
21
Dan Albertb302d122015-02-24 15:51:19 -080022#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080026#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027
Elliott Hughesf55ead92015-12-04 22:00:26 -080028#include <android-base/stringprintf.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070029#include <cutils/sockets.h>
Elliott Hughesfb596842015-05-01 17:04:38 -070030
Dan Albertb302d122015-02-24 15:51:19 -080031#if !ADB_HOST
32#include "cutils/properties.h"
33#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070034
35#include "adb.h"
36#include "adb_io.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070037#include "adb_utils.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080039#if ADB_HOST
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010040/* 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 Project9ca14dc2009-03-03 19:32:55 -080043 */
David 'Digit' Turnerb0f0a462014-03-13 11:21:58 +010044#define ADB_LOCAL_TRANSPORT_MAX 64
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045
46ADB_MUTEX_DEFINE( local_transports_lock );
47
48static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
49#endif /* ADB_HOST */
50
51static int remote_read(apacket *p, atransport *t)
52{
Dan Albert66a91b02015-02-24 21:26:58 -080053 if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
Yabin Cui815ad882015-09-02 17:44:28 -070054 D("remote local: read terminated (message)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080055 return -1;
56 }
57
Tamas Berghammera1c60c02015-07-13 19:12:28 +010058 if(check_header(p, t)) {
Yabin Cui815ad882015-09-02 17:44:28 -070059 D("bad header: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060 return -1;
61 }
62
Dan Albert66a91b02015-02-24 21:26:58 -080063 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
Yabin Cui815ad882015-09-02 17:44:28 -070064 D("remote local: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080065 return -1;
66 }
67
68 if(check_data(p)) {
Yabin Cui815ad882015-09-02 17:44:28 -070069 D("bad data: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070 return -1;
71 }
72
73 return 0;
74}
75
76static int remote_write(apacket *p, atransport *t)
77{
78 int length = p->msg.data_length;
79
Dan Albert66a91b02015-02-24 21:26:58 -080080 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
Yabin Cui815ad882015-09-02 17:44:28 -070081 D("remote local: write terminated");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082 return -1;
83 }
84
85 return 0;
86}
87
Elliott Hughes43df1092015-07-23 17:12:58 -070088void local_connect(int port) {
89 std::string dummy;
90 local_connect_arbitrary_ports(port-1, port, &dummy);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010091}
92
Elliott Hughes43df1092015-07-23 17:12:58 -070093int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
94 int fd = -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080095
96#if ADB_HOST
Yabin Cuid802bcf2015-07-31 14:10:54 -070097 if (find_emulator_transport_by_adb_port(adb_port) != nullptr) {
98 return -1;
99 }
100
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101 const char *host = getenv("ADBHOST");
102 if (host) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700103 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104 }
105#endif
106 if (fd < 0) {
Spencer Low753d4852015-07-30 23:07:55 -0700107 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108 }
109
110 if (fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700111 D("client: connected on remote on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 close_on_exec(fd);
113 disable_tcp_nagle(fd);
Elliott Hughesfb596842015-05-01 17:04:38 -0700114 std::string serial = android::base::StringPrintf("emulator-%d", console_port);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700115 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
116 return 0;
117 }
118 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800119 }
120 return -1;
121}
122
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700123#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800124static void *client_socket_thread(void *x)
125{
Siva Velusamy2669cf92015-08-28 16:37:29 -0700126 adb_thread_setname("client_socket_thread");
Yabin Cui815ad882015-09-02 17:44:28 -0700127 D("transport: client_socket_thread() starting");
Yabin Cuid802bcf2015-07-31 14:10:54 -0700128 while (true) {
129 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
130 int count = ADB_LOCAL_TRANSPORT_MAX;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800131
Yabin Cuid802bcf2015-07-31 14:10:54 -0700132 // 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 Project9ca14dc2009-03-03 19:32:55 -0800137 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800138 return 0;
139}
140
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700141#else // ADB_HOST
142
Mike Lockwood26b88e32009-08-24 15:58:40 -0700143static void *server_socket_thread(void * arg)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800144{
145 int serverfd, fd;
Erik Kline3c8d44d2015-12-01 17:27:59 +0900146 sockaddr_storage ss;
147 sockaddr *addrp = reinterpret_cast<sockaddr*>(&ss);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800148 socklen_t alen;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800149 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800150
Siva Velusamy2669cf92015-08-28 16:37:29 -0700151 adb_thread_setname("server socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700152 D("transport: server_socket_thread() starting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800153 serverfd = -1;
154 for(;;) {
155 if(serverfd == -1) {
Spencer Low753d4852015-07-30 23:07:55 -0700156 std::string error;
157 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800158 if(serverfd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700159 D("server: cannot bind socket yet: %s", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160 adb_sleep_ms(1000);
161 continue;
162 }
163 close_on_exec(serverfd);
164 }
165
Erik Kline3c8d44d2015-12-01 17:27:59 +0900166 alen = sizeof(ss);
Yabin Cui815ad882015-09-02 17:44:28 -0700167 D("server: trying to get new connection from %d", port);
Erik Kline3c8d44d2015-12-01 17:27:59 +0900168 fd = adb_socket_accept(serverfd, addrp, &alen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800169 if(fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700170 D("server: new connection on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800171 close_on_exec(fd);
172 disable_tcp_nagle(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700173 register_socket_transport(fd, "host", port, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800174 }
175 }
Yabin Cui815ad882015-09-02 17:44:28 -0700176 D("transport: server_socket_thread() exiting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177 return 0;
178}
179
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800180/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800181/*
182 * Redefine open and write for qemu_pipe.h that contains inlined references
183 * to those routines. We will redifine them back after qemu_pipe.h inclusion.
184 */
185#undef open
186#undef write
187#define open adb_open
188#define write adb_write
189#include <hardware/qemu_pipe.h>
190#undef open
191#undef write
192#define open ___xxx_open
193#define write ___xxx_write
194
195/* A worker thread that monitors host connections, and registers a transport for
196 * every new host connection. This thread replaces server_socket_thread on
197 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
198 * pipe to communicate with adbd daemon inside the guest. This is done in order
199 * to provide more robust communication channel between ADB host and guest. The
200 * main issue with server_socket_thread approach is that it runs on top of TCP,
201 * and thus is sensitive to network disruptions. For instance, the
202 * ConnectionManager may decide to reset all network connections, in which case
203 * the connection between ADB host and guest will be lost. To make ADB traffic
204 * independent from the network, we use here 'adb' QEMUD service to transfer data
205 * between the host, and the guest. See external/qemu/android/adb-*.* that
206 * implements the emulator's side of the protocol. Another advantage of using
207 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
208 * anymore on network being set up.
209 * The guest side of the protocol contains the following phases:
210 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
211 * is opened, and it becomes clear whether or not emulator supports that
212 * protocol.
213 * - Wait for the ADB host to create connection with the guest. This is done by
214 * sending an 'accept' request to the adb QEMUD service, and waiting on
215 * response.
216 * - When new ADB host connection is accepted, the connection with adb QEMUD
217 * service is registered as the transport, and a 'start' request is sent to the
218 * adb QEMUD service, indicating that the guest is ready to receive messages.
219 * Note that the guest will ignore messages sent down from the emulator before
220 * the transport registration is completed. That's why we need to send the
221 * 'start' request after the transport is registered.
222 */
223static void *qemu_socket_thread(void * arg)
224{
225/* 'accept' request to the adb QEMUD service. */
226static const char _accept_req[] = "accept";
227/* 'start' request to the adb QEMUD service. */
228static const char _start_req[] = "start";
229/* 'ok' reply from the adb QEMUD service. */
230static const char _ok_resp[] = "ok";
231
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800232 const int port = (int) (uintptr_t) arg;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800233 int res, fd;
234 char tmp[256];
235 char con_name[32];
236
Siva Velusamy2669cf92015-08-28 16:37:29 -0700237 adb_thread_setname("qemu socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700238 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800239
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 Cui815ad882015-09-02 17:44:28 -0700248 D("adb service is not available. Falling back to TCP socket.");
Elliott Hughesf2517142015-05-05 13:41:21 -0700249 adb_thread_create(server_socket_thread, arg);
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800250 return 0;
251 }
252
253 for(;;) {
254 /*
255 * Wait till the host creates a new connection.
256 */
257
258 /* Send the 'accept' request. */
259 res = adb_write(fd, _accept_req, strlen(_accept_req));
Edwin Vane8722b972012-07-26 13:40:14 -0400260 if ((size_t)res == strlen(_accept_req)) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800261 /* Wait for the response. In the response we expect 'ok' on success,
262 * or 'ko' on failure. */
263 res = adb_read(fd, tmp, sizeof(tmp));
264 if (res != 2 || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700265 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800266 adb_close(fd);
267 } else {
268 /* Host is connected. Register the transport, and start the
269 * exchange. */
270 register_socket_transport(fd, "host", port, 1);
271 adb_write(fd, _start_req, strlen(_start_req));
272 }
273
274 /* Prepare for accepting of the next ADB host connection. */
275 fd = qemu_pipe_open(con_name);
276 if (fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700277 D("adb service become unavailable.");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800278 return 0;
279 }
280 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700281 D("Unable to send the '%s' request to ADB service.", _accept_req);
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800282 return 0;
283 }
284 }
Yabin Cui815ad882015-09-02 17:44:28 -0700285 D("transport: qemu_socket_thread() exiting");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800286 return 0;
287}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800288#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800289
Mike Lockwood26b88e32009-08-24 15:58:40 -0700290void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800292 void* (*func)(void *);
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700293 const char* debug_name = "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800294
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800295#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700296 func = client_socket_thread;
297 debug_name = "client";
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800298#else
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700299 /* For the adbd daemon in the system image we need to distinguish
300 * between the device, and the emulator. */
301 char is_qemu[PROPERTY_VALUE_MAX];
302 property_get("ro.kernel.qemu", is_qemu, "");
303 if (!strcmp(is_qemu, "1")) {
304 /* Running inside the emulator: use QEMUD pipe as the transport. */
305 func = qemu_socket_thread;
306 } else {
307 /* Running inside the device: use TCP socket as the transport. */
308 func = server_socket_thread;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700310 debug_name = "server";
311#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800312
Yabin Cui815ad882015-09-02 17:44:28 -0700313 D("transport: local %s init", debug_name);
Elliott Hughesf2517142015-05-05 13:41:21 -0700314 if (!adb_thread_create(func, (void *) (uintptr_t) port)) {
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700315 fatal_errno("cannot create local socket %s thread", debug_name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316 }
317}
318
319static void remote_kick(atransport *t)
320{
321 int fd = t->sfd;
322 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400323 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800324 adb_close(fd);
325
326#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700327 int nn;
328 adb_mutex_lock( &local_transports_lock );
329 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
330 if (local_transports[nn] == t) {
331 local_transports[nn] = NULL;
332 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800333 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800334 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700335 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800336#endif
337}
338
339static void remote_close(atransport *t)
340{
Spencer Low6f38e232015-06-09 12:32:17 -0700341 int fd = t->sfd;
342 if (fd != -1) {
343 t->sfd = -1;
344 adb_close(fd);
345 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346}
347
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100348
349#if ADB_HOST
350/* Only call this function if you already hold local_transports_lock. */
351atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
352{
353 int i;
354 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
355 if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
356 return local_transports[i];
357 }
358 }
359 return NULL;
360}
361
362atransport* find_emulator_transport_by_adb_port(int adb_port)
363{
364 adb_mutex_lock( &local_transports_lock );
365 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
366 adb_mutex_unlock( &local_transports_lock );
367 return result;
368}
369
370/* Only call this function if you already hold local_transports_lock. */
371int get_available_local_transport_index_locked()
372{
373 int i;
374 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
375 if (local_transports[i] == NULL) {
376 return i;
377 }
378 }
379 return -1;
380}
381
382int get_available_local_transport_index()
383{
384 adb_mutex_lock( &local_transports_lock );
385 int result = get_available_local_transport_index_locked();
386 adb_mutex_unlock( &local_transports_lock );
387 return result;
388}
389#endif
390
391int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392{
393 int fail = 0;
394
395 t->kick = remote_kick;
396 t->close = remote_close;
397 t->read_from_remote = remote_read;
398 t->write_to_remote = remote_write;
399 t->sfd = s;
400 t->sync_token = 1;
Dan Albert9a50f4c2015-05-18 16:43:57 -0700401 t->connection_state = kCsOffline;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402 t->type = kTransportLocal;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100403 t->adb_port = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404
405#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700406 if (local) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800407 adb_mutex_lock( &local_transports_lock );
408 {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100409 t->adb_port = adb_port;
410 atransport* existing_transport =
411 find_emulator_transport_by_adb_port_locked(adb_port);
412 int index = get_available_local_transport_index_locked();
413 if (existing_transport != NULL) {
Yabin Cui815ad882015-09-02 17:44:28 -0700414 D("local transport for port %d already registered (%p)?",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100415 adb_port, existing_transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800416 fail = -1;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100417 } else if (index < 0) {
418 // Too many emulators.
Yabin Cui815ad882015-09-02 17:44:28 -0700419 D("cannot register more emulators. Maximum is %d",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100420 ADB_LOCAL_TRANSPORT_MAX);
421 fail = -1;
422 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423 local_transports[index] = t;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100424 }
425 }
426 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427 }
428#endif
429 return fail;
430}