blob: 0dc9581ddbca7f266e20efa0ce2e3017ccd5ba72 [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
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_TRANSPORT
18
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 Hughesfb596842015-05-01 17:04:38 -070028#include <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))){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054 D("remote local: read terminated (message)\n");
55 return -1;
56 }
57
Tamas Berghammera1c60c02015-07-13 19:12:28 +010058 if(check_header(p, t)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059 D("bad header: terminated (data)\n");
60 return -1;
61 }
62
Dan Albert66a91b02015-02-24 21:26:58 -080063 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064 D("remote local: terminated (data)\n");
65 return -1;
66 }
67
68 if(check_data(p)) {
69 D("bad data: terminated (data)\n");
70 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)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080081 D("remote local: write terminated\n");
82 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
97 const char *host = getenv("ADBHOST");
98 if (host) {
Elliott Hughes43df1092015-07-23 17:12:58 -070099 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800100 }
101#endif
102 if (fd < 0) {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100103 fd = socket_loopback_client(adb_port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104 }
105
106 if (fd >= 0) {
107 D("client: connected on remote on fd %d\n", fd);
108 close_on_exec(fd);
109 disable_tcp_nagle(fd);
Elliott Hughesfb596842015-05-01 17:04:38 -0700110 std::string serial = android::base::StringPrintf("emulator-%d", console_port);
111 register_socket_transport(fd, serial.c_str(), adb_port, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 return 0;
113 }
114 return -1;
115}
116
117
118static void *client_socket_thread(void *x)
119{
120#if ADB_HOST
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100121 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800122 int count = ADB_LOCAL_TRANSPORT_MAX;
123
124 D("transport: client_socket_thread() starting\n");
125
126 /* try to connect to any number of running emulator instances */
127 /* this is only done when ADB starts up. later, each new emulator */
128 /* will send a message to ADB to indicate that is is starting up */
129 for ( ; count > 0; count--, port += 2 ) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700130 local_connect(port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800131 }
132#endif
133 return 0;
134}
135
Mike Lockwood26b88e32009-08-24 15:58:40 -0700136static void *server_socket_thread(void * arg)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800137{
138 int serverfd, fd;
Kenny Root6ef335e2012-03-28 15:45:08 -0700139 struct sockaddr addr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140 socklen_t alen;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800141 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800142
143 D("transport: server_socket_thread() starting\n");
144 serverfd = -1;
145 for(;;) {
146 if(serverfd == -1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700147 serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800148 if(serverfd < 0) {
Elliott Hughesa585cbd2015-04-20 08:09:20 -0700149 D("server: cannot bind socket yet: %s\n", strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800150 adb_sleep_ms(1000);
151 continue;
152 }
153 close_on_exec(serverfd);
154 }
155
156 alen = sizeof(addr);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700157 D("server: trying to get new connection from %d\n", port);
Kenny Root6ef335e2012-03-28 15:45:08 -0700158 fd = adb_socket_accept(serverfd, &addr, &alen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800159 if(fd >= 0) {
160 D("server: new connection on fd %d\n", fd);
161 close_on_exec(fd);
162 disable_tcp_nagle(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700163 register_socket_transport(fd, "host", port, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800164 }
165 }
166 D("transport: server_socket_thread() exiting\n");
167 return 0;
168}
169
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800170/* This is relevant only for ADB daemon running inside the emulator. */
171#if !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800172/*
173 * Redefine open and write for qemu_pipe.h that contains inlined references
174 * to those routines. We will redifine them back after qemu_pipe.h inclusion.
175 */
176#undef open
177#undef write
178#define open adb_open
179#define write adb_write
180#include <hardware/qemu_pipe.h>
181#undef open
182#undef write
183#define open ___xxx_open
184#define write ___xxx_write
185
186/* A worker thread that monitors host connections, and registers a transport for
187 * every new host connection. This thread replaces server_socket_thread on
188 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
189 * pipe to communicate with adbd daemon inside the guest. This is done in order
190 * to provide more robust communication channel between ADB host and guest. The
191 * main issue with server_socket_thread approach is that it runs on top of TCP,
192 * and thus is sensitive to network disruptions. For instance, the
193 * ConnectionManager may decide to reset all network connections, in which case
194 * the connection between ADB host and guest will be lost. To make ADB traffic
195 * independent from the network, we use here 'adb' QEMUD service to transfer data
196 * between the host, and the guest. See external/qemu/android/adb-*.* that
197 * implements the emulator's side of the protocol. Another advantage of using
198 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
199 * anymore on network being set up.
200 * The guest side of the protocol contains the following phases:
201 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
202 * is opened, and it becomes clear whether or not emulator supports that
203 * protocol.
204 * - Wait for the ADB host to create connection with the guest. This is done by
205 * sending an 'accept' request to the adb QEMUD service, and waiting on
206 * response.
207 * - When new ADB host connection is accepted, the connection with adb QEMUD
208 * service is registered as the transport, and a 'start' request is sent to the
209 * adb QEMUD service, indicating that the guest is ready to receive messages.
210 * Note that the guest will ignore messages sent down from the emulator before
211 * the transport registration is completed. That's why we need to send the
212 * 'start' request after the transport is registered.
213 */
214static void *qemu_socket_thread(void * arg)
215{
216/* 'accept' request to the adb QEMUD service. */
217static const char _accept_req[] = "accept";
218/* 'start' request to the adb QEMUD service. */
219static const char _start_req[] = "start";
220/* 'ok' reply from the adb QEMUD service. */
221static const char _ok_resp[] = "ok";
222
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800223 const int port = (int) (uintptr_t) arg;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800224 int res, fd;
225 char tmp[256];
226 char con_name[32];
227
228 D("transport: qemu_socket_thread() starting\n");
229
230 /* adb QEMUD service connection request. */
231 snprintf(con_name, sizeof(con_name), "qemud:adb:%d", port);
232
233 /* Connect to the adb QEMUD service. */
234 fd = qemu_pipe_open(con_name);
235 if (fd < 0) {
236 /* This could be an older version of the emulator, that doesn't
237 * implement adb QEMUD service. Fall back to the old TCP way. */
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800238 D("adb service is not available. Falling back to TCP socket.\n");
Elliott Hughesf2517142015-05-05 13:41:21 -0700239 adb_thread_create(server_socket_thread, arg);
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800240 return 0;
241 }
242
243 for(;;) {
244 /*
245 * Wait till the host creates a new connection.
246 */
247
248 /* Send the 'accept' request. */
249 res = adb_write(fd, _accept_req, strlen(_accept_req));
Edwin Vane8722b972012-07-26 13:40:14 -0400250 if ((size_t)res == strlen(_accept_req)) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800251 /* Wait for the response. In the response we expect 'ok' on success,
252 * or 'ko' on failure. */
253 res = adb_read(fd, tmp, sizeof(tmp));
254 if (res != 2 || memcmp(tmp, _ok_resp, 2)) {
255 D("Accepting ADB host connection has failed.\n");
256 adb_close(fd);
257 } else {
258 /* Host is connected. Register the transport, and start the
259 * exchange. */
260 register_socket_transport(fd, "host", port, 1);
261 adb_write(fd, _start_req, strlen(_start_req));
262 }
263
264 /* Prepare for accepting of the next ADB host connection. */
265 fd = qemu_pipe_open(con_name);
266 if (fd < 0) {
267 D("adb service become unavailable.\n");
268 return 0;
269 }
270 } else {
271 D("Unable to send the '%s' request to ADB service.\n", _accept_req);
272 return 0;
273 }
274 }
275 D("transport: qemu_socket_thread() exiting\n");
276 return 0;
277}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800278#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800279
Mike Lockwood26b88e32009-08-24 15:58:40 -0700280void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800281{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282 void* (*func)(void *);
283
284 if(HOST) {
285 func = client_socket_thread;
286 } else {
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800287#if ADB_HOST
288 func = server_socket_thread;
289#else
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800290 /* For the adbd daemon in the system image we need to distinguish
291 * between the device, and the emulator. */
292 char is_qemu[PROPERTY_VALUE_MAX];
293 property_get("ro.kernel.qemu", is_qemu, "");
294 if (!strcmp(is_qemu, "1")) {
295 /* Running inside the emulator: use QEMUD pipe as the transport. */
296 func = qemu_socket_thread;
297 } else {
298 /* Running inside the device: use TCP socket as the transport. */
299 func = server_socket_thread;
300 }
Anatol Pomazau59923f42012-02-13 17:37:14 -0800301#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800302 }
303
304 D("transport: local %s init\n", HOST ? "client" : "server");
305
Elliott Hughesf2517142015-05-05 13:41:21 -0700306 if (!adb_thread_create(func, (void *) (uintptr_t) port)) {
307 fatal_errno("cannot create local socket %s thread", HOST ? "client" : "server");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800308 }
309}
310
311static void remote_kick(atransport *t)
312{
313 int fd = t->sfd;
314 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400315 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316 adb_close(fd);
317
318#if ADB_HOST
319 if(HOST) {
320 int nn;
321 adb_mutex_lock( &local_transports_lock );
322 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
323 if (local_transports[nn] == t) {
324 local_transports[nn] = NULL;
325 break;
326 }
327 }
328 adb_mutex_unlock( &local_transports_lock );
329 }
330#endif
331}
332
333static void remote_close(atransport *t)
334{
Spencer Low6f38e232015-06-09 12:32:17 -0700335 int fd = t->sfd;
336 if (fd != -1) {
337 t->sfd = -1;
338 adb_close(fd);
339 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800340}
341
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100342
343#if ADB_HOST
344/* Only call this function if you already hold local_transports_lock. */
345atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
346{
347 int i;
348 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
349 if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
350 return local_transports[i];
351 }
352 }
353 return NULL;
354}
355
356atransport* find_emulator_transport_by_adb_port(int adb_port)
357{
358 adb_mutex_lock( &local_transports_lock );
359 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
360 adb_mutex_unlock( &local_transports_lock );
361 return result;
362}
363
364/* Only call this function if you already hold local_transports_lock. */
365int get_available_local_transport_index_locked()
366{
367 int i;
368 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
369 if (local_transports[i] == NULL) {
370 return i;
371 }
372 }
373 return -1;
374}
375
376int get_available_local_transport_index()
377{
378 adb_mutex_lock( &local_transports_lock );
379 int result = get_available_local_transport_index_locked();
380 adb_mutex_unlock( &local_transports_lock );
381 return result;
382}
383#endif
384
385int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800386{
387 int fail = 0;
388
389 t->kick = remote_kick;
390 t->close = remote_close;
391 t->read_from_remote = remote_read;
392 t->write_to_remote = remote_write;
393 t->sfd = s;
394 t->sync_token = 1;
Dan Albert9a50f4c2015-05-18 16:43:57 -0700395 t->connection_state = kCsOffline;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396 t->type = kTransportLocal;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100397 t->adb_port = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398
399#if ADB_HOST
Mike Lockwood26b88e32009-08-24 15:58:40 -0700400 if (HOST && local) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800401 adb_mutex_lock( &local_transports_lock );
402 {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100403 t->adb_port = adb_port;
404 atransport* existing_transport =
405 find_emulator_transport_by_adb_port_locked(adb_port);
406 int index = get_available_local_transport_index_locked();
407 if (existing_transport != NULL) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800408 D("local transport for port %d already registered (%p)?\n",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100409 adb_port, existing_transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800410 fail = -1;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100411 } else if (index < 0) {
412 // Too many emulators.
413 D("cannot register more emulators. Maximum is %d\n",
414 ADB_LOCAL_TRANSPORT_MAX);
415 fail = -1;
416 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417 local_transports[index] = t;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100418 }
419 }
420 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800421 }
422#endif
423 return fail;
424}