blob: e07f2f70934936d440effd72621f7cf7eab471d1 [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 Albertb302d122015-02-24 15:51:19 -080017#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080021#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022
23#include "sysdeps.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080024
25#define TRACE_TAG TRACE_TRANSPORT
26#include "adb.h"
Dan Albertb302d122015-02-24 15:51:19 -080027#if !ADB_HOST
28#include "cutils/properties.h"
29#endif
30#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080031
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080032#if ADB_HOST
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010033/* we keep a list of opened transports. The atransport struct knows to which
34 * local transport it is connected. The list is used to detect when we're
35 * trying to connect twice to a given local transport.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080036 */
David 'Digit' Turnerb0f0a462014-03-13 11:21:58 +010037#define ADB_LOCAL_TRANSPORT_MAX 64
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038
39ADB_MUTEX_DEFINE( local_transports_lock );
40
41static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
42#endif /* ADB_HOST */
43
44static int remote_read(apacket *p, atransport *t)
45{
46 if(readx(t->sfd, &p->msg, sizeof(amessage))){
47 D("remote local: read terminated (message)\n");
48 return -1;
49 }
50
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051 if(check_header(p)) {
52 D("bad header: terminated (data)\n");
53 return -1;
54 }
55
56 if(readx(t->sfd, p->data, p->msg.data_length)){
57 D("remote local: terminated (data)\n");
58 return -1;
59 }
60
61 if(check_data(p)) {
62 D("bad data: terminated (data)\n");
63 return -1;
64 }
65
66 return 0;
67}
68
69static int remote_write(apacket *p, atransport *t)
70{
71 int length = p->msg.data_length;
72
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073 if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
74 D("remote local: write terminated\n");
75 return -1;
76 }
77
78 return 0;
79}
80
81
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010082int local_connect(int port) {
83 return local_connect_arbitrary_ports(port-1, port);
84}
85
86int local_connect_arbitrary_ports(int console_port, int adb_port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080087{
88 char buf[64];
89 int fd = -1;
90
91#if ADB_HOST
92 const char *host = getenv("ADBHOST");
93 if (host) {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010094 fd = socket_network_client(host, adb_port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080095 }
96#endif
97 if (fd < 0) {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010098 fd = socket_loopback_client(adb_port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099 }
100
101 if (fd >= 0) {
102 D("client: connected on remote on fd %d\n", fd);
103 close_on_exec(fd);
104 disable_tcp_nagle(fd);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100105 snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port);
106 register_socket_transport(fd, buf, adb_port, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800107 return 0;
108 }
109 return -1;
110}
111
112
113static void *client_socket_thread(void *x)
114{
115#if ADB_HOST
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100116 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800117 int count = ADB_LOCAL_TRANSPORT_MAX;
118
119 D("transport: client_socket_thread() starting\n");
120
121 /* try to connect to any number of running emulator instances */
122 /* this is only done when ADB starts up. later, each new emulator */
123 /* will send a message to ADB to indicate that is is starting up */
124 for ( ; count > 0; count--, port += 2 ) {
125 (void) local_connect(port);
126 }
127#endif
128 return 0;
129}
130
Mike Lockwood26b88e32009-08-24 15:58:40 -0700131static void *server_socket_thread(void * arg)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800132{
133 int serverfd, fd;
Kenny Root6ef335e2012-03-28 15:45:08 -0700134 struct sockaddr addr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135 socklen_t alen;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800136 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800137
138 D("transport: server_socket_thread() starting\n");
139 serverfd = -1;
140 for(;;) {
141 if(serverfd == -1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700142 serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143 if(serverfd < 0) {
144 D("server: cannot bind socket yet\n");
145 adb_sleep_ms(1000);
146 continue;
147 }
148 close_on_exec(serverfd);
149 }
150
151 alen = sizeof(addr);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700152 D("server: trying to get new connection from %d\n", port);
Kenny Root6ef335e2012-03-28 15:45:08 -0700153 fd = adb_socket_accept(serverfd, &addr, &alen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800154 if(fd >= 0) {
155 D("server: new connection on fd %d\n", fd);
156 close_on_exec(fd);
157 disable_tcp_nagle(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700158 register_socket_transport(fd, "host", port, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800159 }
160 }
161 D("transport: server_socket_thread() exiting\n");
162 return 0;
163}
164
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800165/* This is relevant only for ADB daemon running inside the emulator. */
166#if !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800167/*
168 * Redefine open and write for qemu_pipe.h that contains inlined references
169 * to those routines. We will redifine them back after qemu_pipe.h inclusion.
170 */
171#undef open
172#undef write
173#define open adb_open
174#define write adb_write
175#include <hardware/qemu_pipe.h>
176#undef open
177#undef write
178#define open ___xxx_open
179#define write ___xxx_write
180
181/* A worker thread that monitors host connections, and registers a transport for
182 * every new host connection. This thread replaces server_socket_thread on
183 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
184 * pipe to communicate with adbd daemon inside the guest. This is done in order
185 * to provide more robust communication channel between ADB host and guest. The
186 * main issue with server_socket_thread approach is that it runs on top of TCP,
187 * and thus is sensitive to network disruptions. For instance, the
188 * ConnectionManager may decide to reset all network connections, in which case
189 * the connection between ADB host and guest will be lost. To make ADB traffic
190 * independent from the network, we use here 'adb' QEMUD service to transfer data
191 * between the host, and the guest. See external/qemu/android/adb-*.* that
192 * implements the emulator's side of the protocol. Another advantage of using
193 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
194 * anymore on network being set up.
195 * The guest side of the protocol contains the following phases:
196 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
197 * is opened, and it becomes clear whether or not emulator supports that
198 * protocol.
199 * - Wait for the ADB host to create connection with the guest. This is done by
200 * sending an 'accept' request to the adb QEMUD service, and waiting on
201 * response.
202 * - When new ADB host connection is accepted, the connection with adb QEMUD
203 * service is registered as the transport, and a 'start' request is sent to the
204 * adb QEMUD service, indicating that the guest is ready to receive messages.
205 * Note that the guest will ignore messages sent down from the emulator before
206 * the transport registration is completed. That's why we need to send the
207 * 'start' request after the transport is registered.
208 */
209static void *qemu_socket_thread(void * arg)
210{
211/* 'accept' request to the adb QEMUD service. */
212static const char _accept_req[] = "accept";
213/* 'start' request to the adb QEMUD service. */
214static const char _start_req[] = "start";
215/* 'ok' reply from the adb QEMUD service. */
216static const char _ok_resp[] = "ok";
217
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800218 const int port = (int) (uintptr_t) arg;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800219 int res, fd;
220 char tmp[256];
221 char con_name[32];
222
223 D("transport: qemu_socket_thread() starting\n");
224
225 /* adb QEMUD service connection request. */
226 snprintf(con_name, sizeof(con_name), "qemud:adb:%d", port);
227
228 /* Connect to the adb QEMUD service. */
229 fd = qemu_pipe_open(con_name);
230 if (fd < 0) {
231 /* This could be an older version of the emulator, that doesn't
232 * implement adb QEMUD service. Fall back to the old TCP way. */
233 adb_thread_t thr;
234 D("adb service is not available. Falling back to TCP socket.\n");
235 adb_thread_create(&thr, server_socket_thread, arg);
236 return 0;
237 }
238
239 for(;;) {
240 /*
241 * Wait till the host creates a new connection.
242 */
243
244 /* Send the 'accept' request. */
245 res = adb_write(fd, _accept_req, strlen(_accept_req));
Edwin Vane8722b972012-07-26 13:40:14 -0400246 if ((size_t)res == strlen(_accept_req)) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800247 /* Wait for the response. In the response we expect 'ok' on success,
248 * or 'ko' on failure. */
249 res = adb_read(fd, tmp, sizeof(tmp));
250 if (res != 2 || memcmp(tmp, _ok_resp, 2)) {
251 D("Accepting ADB host connection has failed.\n");
252 adb_close(fd);
253 } else {
254 /* Host is connected. Register the transport, and start the
255 * exchange. */
256 register_socket_transport(fd, "host", port, 1);
257 adb_write(fd, _start_req, strlen(_start_req));
258 }
259
260 /* Prepare for accepting of the next ADB host connection. */
261 fd = qemu_pipe_open(con_name);
262 if (fd < 0) {
263 D("adb service become unavailable.\n");
264 return 0;
265 }
266 } else {
267 D("Unable to send the '%s' request to ADB service.\n", _accept_req);
268 return 0;
269 }
270 }
271 D("transport: qemu_socket_thread() exiting\n");
272 return 0;
273}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800274#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800275
Mike Lockwood26b88e32009-08-24 15:58:40 -0700276void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277{
278 adb_thread_t thr;
279 void* (*func)(void *);
280
281 if(HOST) {
282 func = client_socket_thread;
283 } else {
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800284#if ADB_HOST
285 func = server_socket_thread;
286#else
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800287 /* For the adbd daemon in the system image we need to distinguish
288 * between the device, and the emulator. */
289 char is_qemu[PROPERTY_VALUE_MAX];
290 property_get("ro.kernel.qemu", is_qemu, "");
291 if (!strcmp(is_qemu, "1")) {
292 /* Running inside the emulator: use QEMUD pipe as the transport. */
293 func = qemu_socket_thread;
294 } else {
295 /* Running inside the device: use TCP socket as the transport. */
296 func = server_socket_thread;
297 }
Anatol Pomazau59923f42012-02-13 17:37:14 -0800298#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800299 }
300
301 D("transport: local %s init\n", HOST ? "client" : "server");
302
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800303 if(adb_thread_create(&thr, func, (void *) (uintptr_t) port)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800304 fatal_errno("cannot create local socket %s thread",
305 HOST ? "client" : "server");
306 }
307}
308
309static void remote_kick(atransport *t)
310{
311 int fd = t->sfd;
312 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400313 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800314 adb_close(fd);
315
316#if ADB_HOST
317 if(HOST) {
318 int nn;
319 adb_mutex_lock( &local_transports_lock );
320 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
321 if (local_transports[nn] == t) {
322 local_transports[nn] = NULL;
323 break;
324 }
325 }
326 adb_mutex_unlock( &local_transports_lock );
327 }
328#endif
329}
330
331static void remote_close(atransport *t)
332{
333 adb_close(t->fd);
334}
335
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100336
337#if ADB_HOST
338/* Only call this function if you already hold local_transports_lock. */
339atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
340{
341 int i;
342 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
343 if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
344 return local_transports[i];
345 }
346 }
347 return NULL;
348}
349
350atransport* find_emulator_transport_by_adb_port(int adb_port)
351{
352 adb_mutex_lock( &local_transports_lock );
353 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
354 adb_mutex_unlock( &local_transports_lock );
355 return result;
356}
357
358/* Only call this function if you already hold local_transports_lock. */
359int get_available_local_transport_index_locked()
360{
361 int i;
362 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
363 if (local_transports[i] == NULL) {
364 return i;
365 }
366 }
367 return -1;
368}
369
370int get_available_local_transport_index()
371{
372 adb_mutex_lock( &local_transports_lock );
373 int result = get_available_local_transport_index_locked();
374 adb_mutex_unlock( &local_transports_lock );
375 return result;
376}
377#endif
378
379int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800380{
381 int fail = 0;
382
383 t->kick = remote_kick;
384 t->close = remote_close;
385 t->read_from_remote = remote_read;
386 t->write_to_remote = remote_write;
387 t->sfd = s;
388 t->sync_token = 1;
389 t->connection_state = CS_OFFLINE;
390 t->type = kTransportLocal;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100391 t->adb_port = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392
393#if ADB_HOST
Mike Lockwood26b88e32009-08-24 15:58:40 -0700394 if (HOST && local) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800395 adb_mutex_lock( &local_transports_lock );
396 {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100397 t->adb_port = adb_port;
398 atransport* existing_transport =
399 find_emulator_transport_by_adb_port_locked(adb_port);
400 int index = get_available_local_transport_index_locked();
401 if (existing_transport != NULL) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402 D("local transport for port %d already registered (%p)?\n",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100403 adb_port, existing_transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404 fail = -1;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100405 } else if (index < 0) {
406 // Too many emulators.
407 D("cannot register more emulators. Maximum is %d\n",
408 ADB_LOCAL_TRANSPORT_MAX);
409 fail = -1;
410 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800411 local_transports[index] = t;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100412 }
413 }
414 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800415 }
416#endif
417 return fail;
418}