blob: fe3c87f3fc4e5fff956f47f726fa49ff4f509434 [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
Dan Albertb302d122015-02-24 15:51:19 -080028#if !ADB_HOST
29#include "cutils/properties.h"
30#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070031
32#include "adb.h"
33#include "adb_io.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080034
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035#if ADB_HOST
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010036/* we keep a list of opened transports. The atransport struct knows to which
37 * local transport it is connected. The list is used to detect when we're
38 * trying to connect twice to a given local transport.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080039 */
David 'Digit' Turnerb0f0a462014-03-13 11:21:58 +010040#define ADB_LOCAL_TRANSPORT_MAX 64
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080041
42ADB_MUTEX_DEFINE( local_transports_lock );
43
44static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
45#endif /* ADB_HOST */
46
47static int remote_read(apacket *p, atransport *t)
48{
Dan Albert66a91b02015-02-24 21:26:58 -080049 if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080050 D("remote local: read terminated (message)\n");
51 return -1;
52 }
53
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054 if(check_header(p)) {
55 D("bad header: terminated (data)\n");
56 return -1;
57 }
58
Dan Albert66a91b02015-02-24 21:26:58 -080059 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060 D("remote local: terminated (data)\n");
61 return -1;
62 }
63
64 if(check_data(p)) {
65 D("bad data: terminated (data)\n");
66 return -1;
67 }
68
69 return 0;
70}
71
72static int remote_write(apacket *p, atransport *t)
73{
74 int length = p->msg.data_length;
75
Dan Albert66a91b02015-02-24 21:26:58 -080076 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080077 D("remote local: write terminated\n");
78 return -1;
79 }
80
81 return 0;
82}
83
84
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010085int local_connect(int port) {
86 return local_connect_arbitrary_ports(port-1, port);
87}
88
89int local_connect_arbitrary_ports(int console_port, int adb_port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080090{
91 char buf[64];
92 int fd = -1;
93
94#if ADB_HOST
95 const char *host = getenv("ADBHOST");
96 if (host) {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010097 fd = socket_network_client(host, adb_port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080098 }
99#endif
100 if (fd < 0) {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100101 fd = socket_loopback_client(adb_port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800102 }
103
104 if (fd >= 0) {
105 D("client: connected on remote on fd %d\n", fd);
106 close_on_exec(fd);
107 disable_tcp_nagle(fd);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100108 snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port);
109 register_socket_transport(fd, buf, adb_port, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800110 return 0;
111 }
112 return -1;
113}
114
115
116static void *client_socket_thread(void *x)
117{
118#if ADB_HOST
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100119 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800120 int count = ADB_LOCAL_TRANSPORT_MAX;
121
122 D("transport: client_socket_thread() starting\n");
123
124 /* try to connect to any number of running emulator instances */
125 /* this is only done when ADB starts up. later, each new emulator */
126 /* will send a message to ADB to indicate that is is starting up */
127 for ( ; count > 0; count--, port += 2 ) {
128 (void) local_connect(port);
129 }
130#endif
131 return 0;
132}
133
Mike Lockwood26b88e32009-08-24 15:58:40 -0700134static void *server_socket_thread(void * arg)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135{
136 int serverfd, fd;
Kenny Root6ef335e2012-03-28 15:45:08 -0700137 struct sockaddr addr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800138 socklen_t alen;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800139 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140
141 D("transport: server_socket_thread() starting\n");
142 serverfd = -1;
143 for(;;) {
144 if(serverfd == -1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700145 serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800146 if(serverfd < 0) {
147 D("server: cannot bind socket yet\n");
148 adb_sleep_ms(1000);
149 continue;
150 }
151 close_on_exec(serverfd);
152 }
153
154 alen = sizeof(addr);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700155 D("server: trying to get new connection from %d\n", port);
Kenny Root6ef335e2012-03-28 15:45:08 -0700156 fd = adb_socket_accept(serverfd, &addr, &alen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800157 if(fd >= 0) {
158 D("server: new connection on fd %d\n", fd);
159 close_on_exec(fd);
160 disable_tcp_nagle(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700161 register_socket_transport(fd, "host", port, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800162 }
163 }
164 D("transport: server_socket_thread() exiting\n");
165 return 0;
166}
167
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800168/* This is relevant only for ADB daemon running inside the emulator. */
169#if !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800170/*
171 * Redefine open and write for qemu_pipe.h that contains inlined references
172 * to those routines. We will redifine them back after qemu_pipe.h inclusion.
173 */
174#undef open
175#undef write
176#define open adb_open
177#define write adb_write
178#include <hardware/qemu_pipe.h>
179#undef open
180#undef write
181#define open ___xxx_open
182#define write ___xxx_write
183
184/* A worker thread that monitors host connections, and registers a transport for
185 * every new host connection. This thread replaces server_socket_thread on
186 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
187 * pipe to communicate with adbd daemon inside the guest. This is done in order
188 * to provide more robust communication channel between ADB host and guest. The
189 * main issue with server_socket_thread approach is that it runs on top of TCP,
190 * and thus is sensitive to network disruptions. For instance, the
191 * ConnectionManager may decide to reset all network connections, in which case
192 * the connection between ADB host and guest will be lost. To make ADB traffic
193 * independent from the network, we use here 'adb' QEMUD service to transfer data
194 * between the host, and the guest. See external/qemu/android/adb-*.* that
195 * implements the emulator's side of the protocol. Another advantage of using
196 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
197 * anymore on network being set up.
198 * The guest side of the protocol contains the following phases:
199 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
200 * is opened, and it becomes clear whether or not emulator supports that
201 * protocol.
202 * - Wait for the ADB host to create connection with the guest. This is done by
203 * sending an 'accept' request to the adb QEMUD service, and waiting on
204 * response.
205 * - When new ADB host connection is accepted, the connection with adb QEMUD
206 * service is registered as the transport, and a 'start' request is sent to the
207 * adb QEMUD service, indicating that the guest is ready to receive messages.
208 * Note that the guest will ignore messages sent down from the emulator before
209 * the transport registration is completed. That's why we need to send the
210 * 'start' request after the transport is registered.
211 */
212static void *qemu_socket_thread(void * arg)
213{
214/* 'accept' request to the adb QEMUD service. */
215static const char _accept_req[] = "accept";
216/* 'start' request to the adb QEMUD service. */
217static const char _start_req[] = "start";
218/* 'ok' reply from the adb QEMUD service. */
219static const char _ok_resp[] = "ok";
220
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800221 const int port = (int) (uintptr_t) arg;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800222 int res, fd;
223 char tmp[256];
224 char con_name[32];
225
226 D("transport: qemu_socket_thread() starting\n");
227
228 /* adb QEMUD service connection request. */
229 snprintf(con_name, sizeof(con_name), "qemud:adb:%d", port);
230
231 /* Connect to the adb QEMUD service. */
232 fd = qemu_pipe_open(con_name);
233 if (fd < 0) {
234 /* This could be an older version of the emulator, that doesn't
235 * implement adb QEMUD service. Fall back to the old TCP way. */
236 adb_thread_t thr;
237 D("adb service is not available. Falling back to TCP socket.\n");
238 adb_thread_create(&thr, server_socket_thread, arg);
239 return 0;
240 }
241
242 for(;;) {
243 /*
244 * Wait till the host creates a new connection.
245 */
246
247 /* Send the 'accept' request. */
248 res = adb_write(fd, _accept_req, strlen(_accept_req));
Edwin Vane8722b972012-07-26 13:40:14 -0400249 if ((size_t)res == strlen(_accept_req)) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800250 /* Wait for the response. In the response we expect 'ok' on success,
251 * or 'ko' on failure. */
252 res = adb_read(fd, tmp, sizeof(tmp));
253 if (res != 2 || memcmp(tmp, _ok_resp, 2)) {
254 D("Accepting ADB host connection has failed.\n");
255 adb_close(fd);
256 } else {
257 /* Host is connected. Register the transport, and start the
258 * exchange. */
259 register_socket_transport(fd, "host", port, 1);
260 adb_write(fd, _start_req, strlen(_start_req));
261 }
262
263 /* Prepare for accepting of the next ADB host connection. */
264 fd = qemu_pipe_open(con_name);
265 if (fd < 0) {
266 D("adb service become unavailable.\n");
267 return 0;
268 }
269 } else {
270 D("Unable to send the '%s' request to ADB service.\n", _accept_req);
271 return 0;
272 }
273 }
274 D("transport: qemu_socket_thread() exiting\n");
275 return 0;
276}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800277#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800278
Mike Lockwood26b88e32009-08-24 15:58:40 -0700279void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800280{
281 adb_thread_t thr;
282 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 Hughes9c0d9402014-01-16 10:53:11 -0800306 if(adb_thread_create(&thr, func, (void *) (uintptr_t) port)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800307 fatal_errno("cannot create local socket %s thread",
308 HOST ? "client" : "server");
309 }
310}
311
312static void remote_kick(atransport *t)
313{
314 int fd = t->sfd;
315 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400316 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800317 adb_close(fd);
318
319#if ADB_HOST
320 if(HOST) {
321 int nn;
322 adb_mutex_lock( &local_transports_lock );
323 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
324 if (local_transports[nn] == t) {
325 local_transports[nn] = NULL;
326 break;
327 }
328 }
329 adb_mutex_unlock( &local_transports_lock );
330 }
331#endif
332}
333
334static void remote_close(atransport *t)
335{
336 adb_close(t->fd);
337}
338
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100339
340#if ADB_HOST
341/* Only call this function if you already hold local_transports_lock. */
342atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
343{
344 int i;
345 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
346 if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
347 return local_transports[i];
348 }
349 }
350 return NULL;
351}
352
353atransport* find_emulator_transport_by_adb_port(int adb_port)
354{
355 adb_mutex_lock( &local_transports_lock );
356 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
357 adb_mutex_unlock( &local_transports_lock );
358 return result;
359}
360
361/* Only call this function if you already hold local_transports_lock. */
362int get_available_local_transport_index_locked()
363{
364 int i;
365 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
366 if (local_transports[i] == NULL) {
367 return i;
368 }
369 }
370 return -1;
371}
372
373int get_available_local_transport_index()
374{
375 adb_mutex_lock( &local_transports_lock );
376 int result = get_available_local_transport_index_locked();
377 adb_mutex_unlock( &local_transports_lock );
378 return result;
379}
380#endif
381
382int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383{
384 int fail = 0;
385
386 t->kick = remote_kick;
387 t->close = remote_close;
388 t->read_from_remote = remote_read;
389 t->write_to_remote = remote_write;
390 t->sfd = s;
391 t->sync_token = 1;
392 t->connection_state = CS_OFFLINE;
393 t->type = kTransportLocal;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100394 t->adb_port = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800395
396#if ADB_HOST
Mike Lockwood26b88e32009-08-24 15:58:40 -0700397 if (HOST && local) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398 adb_mutex_lock( &local_transports_lock );
399 {
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100400 t->adb_port = adb_port;
401 atransport* existing_transport =
402 find_emulator_transport_by_adb_port_locked(adb_port);
403 int index = get_available_local_transport_index_locked();
404 if (existing_transport != NULL) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800405 D("local transport for port %d already registered (%p)?\n",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100406 adb_port, existing_transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800407 fail = -1;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100408 } else if (index < 0) {
409 // Too many emulators.
410 D("cannot register more emulators. Maximum is %d\n",
411 ADB_LOCAL_TRANSPORT_MAX);
412 fail = -1;
413 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800414 local_transports[index] = t;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100415 }
416 }
417 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418 }
419#endif
420 return fail;
421}