blob: 399d43ac8ef798c73da8f2b38c23b257531217ec [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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
17#define TRACE_TAG TRACE_ADB
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <ctype.h>
22#include <stdarg.h>
23#include <errno.h>
Scott Andersonc7993af2012-05-25 13:55:46 -070024#include <stddef.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <string.h>
26#include <time.h>
Mike Lockwood1f546e62009-05-25 18:17:55 -040027#include <sys/time.h>
Ray Donnellycbb98912012-11-29 01:36:08 +000028#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30#include "sysdeps.h"
31#include "adb.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070032#include "adb_auth.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Scott Andersone82c2db2012-05-25 14:10:02 -070034#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
35
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#if !ADB_HOST
37#include <private/android_filesystem_config.h>
Mike Lockwood5f4b0512009-08-04 20:37:51 -040038#include <linux/capability.h>
39#include <linux/prctl.h>
Jeff Sharkey885342a2012-08-14 21:00:22 -070040#include <sys/mount.h>
Xavier Ducroheta09fbd12009-05-20 17:33:53 -070041#else
42#include "usb_vendors.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#endif
44
JP Abgrall408fa572011-03-16 15:57:42 -070045#if ADB_TRACE
46ADB_MUTEX_DEFINE( D_lock );
47#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49int HOST = 0;
Matt Gumbeld7b33082012-11-14 10:16:17 -080050int gListenAll = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070052static int auth_enabled = 0;
53
Scott Andersone82c2db2012-05-25 14:10:02 -070054#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055static const char *adb_device_banner = "device";
Scott Andersone82c2db2012-05-25 14:10:02 -070056#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
58void fatal(const char *fmt, ...)
59{
60 va_list ap;
61 va_start(ap, fmt);
62 fprintf(stderr, "error: ");
63 vfprintf(stderr, fmt, ap);
64 fprintf(stderr, "\n");
65 va_end(ap);
66 exit(-1);
67}
68
69void fatal_errno(const char *fmt, ...)
70{
71 va_list ap;
72 va_start(ap, fmt);
73 fprintf(stderr, "error: %s: ", strerror(errno));
74 vfprintf(stderr, fmt, ap);
75 fprintf(stderr, "\n");
76 va_end(ap);
77 exit(-1);
78}
79
80int adb_trace_mask;
81
82/* read a comma/space/colum/semi-column separated list of tags
83 * from the ADB_TRACE environment variable and build the trace
84 * mask from it. note that '1' and 'all' are special cases to
85 * enable all tracing
86 */
87void adb_trace_init(void)
88{
89 const char* p = getenv("ADB_TRACE");
90 const char* q;
91
92 static const struct {
93 const char* tag;
94 int flag;
95 } tags[] = {
96 { "1", 0 },
97 { "all", 0 },
98 { "adb", TRACE_ADB },
99 { "sockets", TRACE_SOCKETS },
100 { "packets", TRACE_PACKETS },
101 { "rwx", TRACE_RWX },
102 { "usb", TRACE_USB },
103 { "sync", TRACE_SYNC },
104 { "sysdeps", TRACE_SYSDEPS },
105 { "transport", TRACE_TRANSPORT },
106 { "jdwp", TRACE_JDWP },
JP Abgrall408fa572011-03-16 15:57:42 -0700107 { "services", TRACE_SERVICES },
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700108 { "auth", TRACE_AUTH },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 { NULL, 0 }
110 };
111
112 if (p == NULL)
113 return;
114
115 /* use a comma/column/semi-colum/space separated list */
116 while (*p) {
117 int len, tagn;
118
119 q = strpbrk(p, " ,:;");
120 if (q == NULL) {
121 q = p + strlen(p);
122 }
123 len = q - p;
124
125 for (tagn = 0; tags[tagn].tag != NULL; tagn++)
126 {
127 int taglen = strlen(tags[tagn].tag);
128
129 if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
130 {
131 int flag = tags[tagn].flag;
132 if (flag == 0) {
133 adb_trace_mask = ~0;
134 return;
135 }
136 adb_trace_mask |= (1 << flag);
137 break;
138 }
139 }
140 p = q;
141 if (*p)
142 p++;
143 }
144}
145
Vladimir Chtchetkine28781b02012-02-27 10:41:53 -0800146#if !ADB_HOST
147/*
148 * Implements ADB tracing inside the emulator.
149 */
150
151#include <stdarg.h>
152
153/*
154 * Redefine open and write for qemu_pipe.h that contains inlined references
155 * to those routines. We will redifine them back after qemu_pipe.h inclusion.
156 */
157
158#undef open
159#undef write
160#define open adb_open
161#define write adb_write
162#include <hardware/qemu_pipe.h>
163#undef open
164#undef write
165#define open ___xxx_open
166#define write ___xxx_write
167
168/* A handle to adb-debug qemud service in the emulator. */
169int adb_debug_qemu = -1;
170
171/* Initializes connection with the adb-debug qemud service in the emulator. */
172static int adb_qemu_trace_init(void)
173{
174 char con_name[32];
175
176 if (adb_debug_qemu >= 0) {
177 return 0;
178 }
179
180 /* adb debugging QEMUD service connection request. */
181 snprintf(con_name, sizeof(con_name), "qemud:adb-debug");
182 adb_debug_qemu = qemu_pipe_open(con_name);
183 return (adb_debug_qemu >= 0) ? 0 : -1;
184}
185
186void adb_qemu_trace(const char* fmt, ...)
187{
188 va_list args;
189 va_start(args, fmt);
190 char msg[1024];
191
192 if (adb_debug_qemu >= 0) {
193 vsnprintf(msg, sizeof(msg), fmt, args);
194 adb_write(adb_debug_qemu, msg, strlen(msg));
195 }
196}
197#endif /* !ADB_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198
199apacket *get_apacket(void)
200{
201 apacket *p = malloc(sizeof(apacket));
202 if(p == 0) fatal("failed to allocate an apacket");
203 memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
204 return p;
205}
206
207void put_apacket(apacket *p)
208{
209 free(p);
210}
211
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700212void handle_online(atransport *t)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213{
214 D("adb: online\n");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700215 t->online = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216}
217
218void handle_offline(atransport *t)
219{
220 D("adb: offline\n");
221 //Close the associated usb
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700222 t->online = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 run_transport_disconnects(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224}
225
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700226#if DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227#define DUMPMAX 32
228void print_packet(const char *label, apacket *p)
229{
230 char *tag;
231 char *x;
232 unsigned count;
233
234 switch(p->msg.command){
235 case A_SYNC: tag = "SYNC"; break;
236 case A_CNXN: tag = "CNXN" ; break;
237 case A_OPEN: tag = "OPEN"; break;
238 case A_OKAY: tag = "OKAY"; break;
239 case A_CLSE: tag = "CLSE"; break;
240 case A_WRTE: tag = "WRTE"; break;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700241 case A_AUTH: tag = "AUTH"; break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 default: tag = "????"; break;
243 }
244
245 fprintf(stderr, "%s: %s %08x %08x %04x \"",
246 label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
247 count = p->msg.data_length;
248 x = (char*) p->data;
249 if(count > DUMPMAX) {
250 count = DUMPMAX;
251 tag = "\n";
252 } else {
253 tag = "\"\n";
254 }
255 while(count-- > 0){
256 if((*x >= ' ') && (*x < 127)) {
257 fputc(*x, stderr);
258 } else {
259 fputc('.', stderr);
260 }
261 x++;
262 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700263 fputs(tag, stderr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264}
265#endif
266
267static void send_ready(unsigned local, unsigned remote, atransport *t)
268{
269 D("Calling send_ready \n");
270 apacket *p = get_apacket();
271 p->msg.command = A_OKAY;
272 p->msg.arg0 = local;
273 p->msg.arg1 = remote;
274 send_packet(p, t);
275}
276
277static void send_close(unsigned local, unsigned remote, atransport *t)
278{
279 D("Calling send_close \n");
280 apacket *p = get_apacket();
281 p->msg.command = A_CLSE;
282 p->msg.arg0 = local;
283 p->msg.arg1 = remote;
284 send_packet(p, t);
285}
286
Scott Andersone82c2db2012-05-25 14:10:02 -0700287static size_t fill_connect_data(char *buf, size_t bufsize)
288{
289#if ADB_HOST
290 return snprintf(buf, bufsize, "host::") + 1;
291#else
292 static const char *cnxn_props[] = {
293 "ro.product.name",
294 "ro.product.model",
295 "ro.product.device",
296 };
297 static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
298 int i;
299 size_t remaining = bufsize;
300 size_t len;
301
302 len = snprintf(buf, remaining, "%s::", adb_device_banner);
303 remaining -= len;
304 buf += len;
305 for (i = 0; i < num_cnxn_props; i++) {
306 char value[PROPERTY_VALUE_MAX];
307 property_get(cnxn_props[i], value, "");
308 len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
309 remaining -= len;
310 buf += len;
311 }
312
313 return bufsize - remaining + 1;
314#endif
315}
316
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317static void send_connect(atransport *t)
318{
319 D("Calling send_connect \n");
320 apacket *cp = get_apacket();
321 cp->msg.command = A_CNXN;
322 cp->msg.arg0 = A_VERSION;
323 cp->msg.arg1 = MAX_PAYLOAD;
Scott Andersone82c2db2012-05-25 14:10:02 -0700324 cp->msg.data_length = fill_connect_data((char *)cp->data,
325 sizeof(cp->data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 send_packet(cp, t);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700327}
328
329static void send_auth_request(atransport *t)
330{
331 D("Calling send_auth_request\n");
332 apacket *p;
333 int ret;
334
335 ret = adb_auth_generate_token(t->token, sizeof(t->token));
336 if (ret != sizeof(t->token)) {
337 D("Error generating token ret=%d\n", ret);
338 return;
339 }
340
341 p = get_apacket();
342 memcpy(p->data, t->token, ret);
343 p->msg.command = A_AUTH;
344 p->msg.arg0 = ADB_AUTH_TOKEN;
345 p->msg.data_length = ret;
346 send_packet(p, t);
347}
348
349static void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
350{
351 D("Calling send_auth_response\n");
352 apacket *p = get_apacket();
353 int ret;
354
355 ret = adb_auth_sign(t->key, token, token_size, p->data);
356 if (!ret) {
357 D("Error signing the token\n");
358 put_apacket(p);
359 return;
360 }
361
362 p->msg.command = A_AUTH;
363 p->msg.arg0 = ADB_AUTH_SIGNATURE;
364 p->msg.data_length = ret;
365 send_packet(p, t);
366}
367
368static void send_auth_publickey(atransport *t)
369{
370 D("Calling send_auth_publickey\n");
371 apacket *p = get_apacket();
372 int ret;
373
374 ret = adb_auth_get_userkey(p->data, sizeof(p->data));
375 if (!ret) {
376 D("Failed to get user public key\n");
377 put_apacket(p);
378 return;
379 }
380
381 p->msg.command = A_AUTH;
382 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
383 p->msg.data_length = ret;
384 send_packet(p, t);
385}
386
387void adb_auth_verified(atransport *t)
388{
389 handle_online(t);
390 send_connect(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391}
392
393static char *connection_state_name(atransport *t)
394{
395 if (t == NULL) {
396 return "unknown";
397 }
398
399 switch(t->connection_state) {
400 case CS_BOOTLOADER:
401 return "bootloader";
402 case CS_DEVICE:
403 return "device";
404 case CS_OFFLINE:
405 return "offline";
406 default:
407 return "unknown";
408 }
409}
410
Scott Andersone82c2db2012-05-25 14:10:02 -0700411/* qual_overwrite is used to overwrite a qualifier string. dst is a
412 * pointer to a char pointer. It is assumed that if *dst is non-NULL, it
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700413 * was malloc'ed and needs to freed. *dst will be set to a dup of src.
Scott Andersone82c2db2012-05-25 14:10:02 -0700414 */
415static void qual_overwrite(char **dst, const char *src)
416{
417 if (!dst)
418 return;
419
420 free(*dst);
421 *dst = NULL;
422
423 if (!src || !*src)
424 return;
425
426 *dst = strdup(src);
427}
428
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429void parse_banner(char *banner, atransport *t)
430{
Scott Andersone82c2db2012-05-25 14:10:02 -0700431 static const char *prop_seps = ";";
432 static const char key_val_sep = '=';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700433 char *cp;
434 char *type;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435
436 D("parse_banner: %s\n", banner);
437 type = banner;
Scott Andersone82c2db2012-05-25 14:10:02 -0700438 cp = strchr(type, ':');
439 if (cp) {
440 *cp++ = 0;
441 /* Nothing is done with second field. */
442 cp = strchr(cp, ':');
443 if (cp) {
444 char *save;
445 char *key;
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700446 key = adb_strtok_r(cp + 1, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700447 while (key) {
448 cp = strchr(key, key_val_sep);
449 if (cp) {
450 *cp++ = '\0';
451 if (!strcmp(key, "ro.product.name"))
452 qual_overwrite(&t->product, cp);
453 else if (!strcmp(key, "ro.product.model"))
454 qual_overwrite(&t->model, cp);
455 else if (!strcmp(key, "ro.product.device"))
456 qual_overwrite(&t->device, cp);
457 }
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700458 key = adb_strtok_r(NULL, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700459 }
460 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 }
462
463 if(!strcmp(type, "bootloader")){
464 D("setting connection_state to CS_BOOTLOADER\n");
465 t->connection_state = CS_BOOTLOADER;
466 update_transports();
467 return;
468 }
469
470 if(!strcmp(type, "device")) {
471 D("setting connection_state to CS_DEVICE\n");
472 t->connection_state = CS_DEVICE;
473 update_transports();
474 return;
475 }
476
477 if(!strcmp(type, "recovery")) {
478 D("setting connection_state to CS_RECOVERY\n");
479 t->connection_state = CS_RECOVERY;
480 update_transports();
481 return;
482 }
483
Doug Zongker447f0612012-01-09 14:54:53 -0800484 if(!strcmp(type, "sideload")) {
485 D("setting connection_state to CS_SIDELOAD\n");
486 t->connection_state = CS_SIDELOAD;
487 update_transports();
488 return;
489 }
490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 t->connection_state = CS_HOST;
492}
493
494void handle_packet(apacket *p, atransport *t)
495{
496 asocket *s;
497
Viral Mehta899913f2010-06-16 18:41:28 +0530498 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
499 ((char*) (&(p->msg.command)))[1],
500 ((char*) (&(p->msg.command)))[2],
501 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 print_packet("recv", p);
503
504 switch(p->msg.command){
505 case A_SYNC:
506 if(p->msg.arg0){
507 send_packet(p, t);
508 if(HOST) send_connect(t);
509 } else {
510 t->connection_state = CS_OFFLINE;
511 handle_offline(t);
512 send_packet(p, t);
513 }
514 return;
515
516 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
517 /* XXX verify version, etc */
518 if(t->connection_state != CS_OFFLINE) {
519 t->connection_state = CS_OFFLINE;
520 handle_offline(t);
521 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700522
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 parse_banner((char*) p->data, t);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700524
525 if (HOST || !auth_enabled) {
526 handle_online(t);
527 if(!HOST) send_connect(t);
528 } else {
529 send_auth_request(t);
530 }
531 break;
532
533 case A_AUTH:
534 if (p->msg.arg0 == ADB_AUTH_TOKEN) {
535 t->key = adb_auth_nextkey(t->key);
536 if (t->key) {
537 send_auth_response(p->data, p->msg.data_length, t);
538 } else {
539 /* No more private keys to try, send the public key */
540 send_auth_publickey(t);
541 }
542 } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
543 if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
544 adb_auth_verified(t);
545 t->failed_auth_attempts = 0;
546 } else {
547 if (t->failed_auth_attempts++ > 10)
548 adb_sleep_ms(1000);
549 send_auth_request(t);
550 }
551 } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
552 adb_auth_confirm_key(p->data, p->msg.data_length, t);
553 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 break;
555
556 case A_OPEN: /* OPEN(local-id, 0, "destination") */
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700557 if (t->online) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 char *name = (char*) p->data;
559 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
560 s = create_local_service_socket(name);
561 if(s == 0) {
562 send_close(0, p->msg.arg0, t);
563 } else {
564 s->peer = create_remote_socket(p->msg.arg0, t);
565 s->peer->peer = s;
566 send_ready(s->id, s->peer->id, t);
567 s->ready(s);
568 }
569 }
570 break;
571
572 case A_OKAY: /* READY(local-id, remote-id, "") */
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700573 if (t->online) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574 if((s = find_local_socket(p->msg.arg1))) {
575 if(s->peer == 0) {
576 s->peer = create_remote_socket(p->msg.arg0, t);
577 s->peer->peer = s;
578 }
579 s->ready(s);
580 }
581 }
582 break;
583
584 case A_CLSE: /* CLOSE(local-id, remote-id, "") */
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700585 if (t->online) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586 if((s = find_local_socket(p->msg.arg1))) {
587 s->close(s);
588 }
589 }
590 break;
591
592 case A_WRTE:
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700593 if (t->online) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594 if((s = find_local_socket(p->msg.arg1))) {
595 unsigned rid = p->msg.arg0;
596 p->len = p->msg.data_length;
597
598 if(s->enqueue(s, p) == 0) {
599 D("Enqueue the socket\n");
600 send_ready(s->id, rid, t);
601 }
602 return;
603 }
604 }
605 break;
606
607 default:
608 printf("handle_packet: what is %08x?!\n", p->msg.command);
609 }
610
611 put_apacket(p);
612}
613
614alistener listener_list = {
615 .next = &listener_list,
616 .prev = &listener_list,
617};
618
619static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
620{
621 asocket *s;
622
623 if(ev & FDE_READ) {
624 struct sockaddr addr;
625 socklen_t alen;
626 int fd;
627
628 alen = sizeof(addr);
629 fd = adb_socket_accept(_fd, &addr, &alen);
630 if(fd < 0) return;
631
632 adb_socket_setbufsize(fd, CHUNK_SIZE);
633
634 s = create_local_socket(fd);
635 if(s) {
636 connect_to_smartsocket(s);
637 return;
638 }
639
640 adb_close(fd);
641 }
642}
643
644static void listener_event_func(int _fd, unsigned ev, void *_l)
645{
646 alistener *l = _l;
647 asocket *s;
648
649 if(ev & FDE_READ) {
650 struct sockaddr addr;
651 socklen_t alen;
652 int fd;
653
654 alen = sizeof(addr);
655 fd = adb_socket_accept(_fd, &addr, &alen);
656 if(fd < 0) return;
657
658 s = create_local_socket(fd);
659 if(s) {
660 s->transport = l->transport;
661 connect_to_remote(s, l->connect_to);
662 return;
663 }
664
665 adb_close(fd);
666 }
667}
668
669static void free_listener(alistener* l)
670{
671 if (l->next) {
672 l->next->prev = l->prev;
673 l->prev->next = l->next;
674 l->next = l->prev = l;
675 }
676
677 // closes the corresponding fd
678 fdevent_remove(&l->fde);
679
680 if (l->local_name)
681 free((char*)l->local_name);
682
683 if (l->connect_to)
684 free((char*)l->connect_to);
685
686 if (l->transport) {
687 remove_transport_disconnect(l->transport, &l->disconnect);
688 }
689 free(l);
690}
691
692static void listener_disconnect(void* _l, atransport* t)
693{
694 alistener* l = _l;
695
696 free_listener(l);
697}
698
699int local_name_to_fd(const char *name)
700{
701 int port;
702
703 if(!strncmp("tcp:", name, 4)){
704 int ret;
705 port = atoi(name + 4);
Matt Gumbeld7b33082012-11-14 10:16:17 -0800706
707 if (gListenAll > 0) {
708 ret = socket_inaddr_any_server(port, SOCK_STREAM);
709 } else {
710 ret = socket_loopback_server(port, SOCK_STREAM);
711 }
712
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 return ret;
714 }
715#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
716 // It's non-sensical to support the "reserved" space on the adb host side
717 if(!strncmp(name, "local:", 6)) {
718 return socket_local_server(name + 6,
719 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
720 } else if(!strncmp(name, "localabstract:", 14)) {
721 return socket_local_server(name + 14,
722 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
723 } else if(!strncmp(name, "localfilesystem:", 16)) {
724 return socket_local_server(name + 16,
725 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
726 }
727
728#endif
729 printf("unknown local portname '%s'\n", name);
730 return -1;
731}
732
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100733// Write a single line describing a listener to a user-provided buffer.
734// Appends a trailing zero, even in case of truncation, but the function
735// returns the full line length.
736// If |buffer| is NULL, does not write but returns required size.
737static int format_listener(alistener* l, char* buffer, size_t buffer_len) {
738 // Format is simply:
739 //
740 // <device-serial> " " <local-name> " " <remote-name> "\n"
741 //
742 int local_len = strlen(l->local_name);
743 int connect_len = strlen(l->connect_to);
744 int serial_len = strlen(l->transport->serial);
745
746 if (buffer != NULL) {
747 snprintf(buffer, buffer_len, "%s %s %s\n",
748 l->transport->serial, l->local_name, l->connect_to);
749 }
750 // NOTE: snprintf() on Windows returns -1 in case of truncation, so
751 // return the computed line length instead.
752 return local_len + connect_len + serial_len + 3;
753}
754
755// Write the list of current listeners (network redirections) into a
756// user-provided buffer. Appends a trailing zero, even in case of
757// trunctaion, but return the full size in bytes.
758// If |buffer| is NULL, does not write but returns required size.
759static int format_listeners(char* buf, size_t buflen)
760{
761 alistener* l;
762 int result = 0;
763 for (l = listener_list.next; l != &listener_list; l = l->next) {
764 // Ignore special listeners like those for *smartsocket*
765 if (l->connect_to[0] == '*')
766 continue;
767 int len = format_listener(l, buf, buflen);
768 // Ensure there is space for the trailing zero.
769 result += len;
770 if (buf != NULL) {
771 buf += len;
772 buflen -= len;
773 if (buflen <= 0)
774 break;
775 }
776 }
777 return result;
778}
779
780static int remove_listener(const char *local_name, atransport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781{
782 alistener *l;
783
784 for (l = listener_list.next; l != &listener_list; l = l->next) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100785 if (!strcmp(local_name, l->local_name)) {
786 listener_disconnect(l, l->transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 return 0;
788 }
789 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 return -1;
791}
792
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100793static void remove_all_listeners(void)
794{
795 alistener *l, *l_next;
796 for (l = listener_list.next; l != &listener_list; l = l_next) {
797 l_next = l->next;
798 // Never remove smart sockets.
799 if (l->connect_to[0] == '*')
800 continue;
801 listener_disconnect(l, l->transport);
802 }
803}
804
805// error/status codes for install_listener.
806typedef enum {
807 INSTALL_STATUS_OK = 0,
808 INSTALL_STATUS_INTERNAL_ERROR = -1,
809 INSTALL_STATUS_CANNOT_BIND = -2,
810 INSTALL_STATUS_CANNOT_REBIND = -3,
811} install_status_t;
812
813static install_status_t install_listener(const char *local_name,
814 const char *connect_to,
815 atransport* transport,
816 int no_rebind)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817{
818 alistener *l;
819
820 //printf("install_listener('%s','%s')\n", local_name, connect_to);
821
822 for(l = listener_list.next; l != &listener_list; l = l->next){
823 if(strcmp(local_name, l->local_name) == 0) {
824 char *cto;
825
826 /* can't repurpose a smartsocket */
827 if(l->connect_to[0] == '*') {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100828 return INSTALL_STATUS_INTERNAL_ERROR;
829 }
830
831 /* can't repurpose a listener if 'no_rebind' is true */
832 if (no_rebind) {
833 return INSTALL_STATUS_CANNOT_REBIND;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834 }
835
836 cto = strdup(connect_to);
837 if(cto == 0) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100838 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839 }
840
841 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
842 free((void*) l->connect_to);
843 l->connect_to = cto;
844 if (l->transport != transport) {
845 remove_transport_disconnect(l->transport, &l->disconnect);
846 l->transport = transport;
847 add_transport_disconnect(l->transport, &l->disconnect);
848 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100849 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800850 }
851 }
852
853 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
854 if((l->local_name = strdup(local_name)) == 0) goto nomem;
855 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
856
857
858 l->fd = local_name_to_fd(local_name);
859 if(l->fd < 0) {
860 free((void*) l->local_name);
861 free((void*) l->connect_to);
862 free(l);
863 printf("cannot bind '%s'\n", local_name);
864 return -2;
865 }
866
867 close_on_exec(l->fd);
868 if(!strcmp(l->connect_to, "*smartsocket*")) {
869 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
870 } else {
871 fdevent_install(&l->fde, l->fd, listener_event_func, l);
872 }
873 fdevent_set(&l->fde, FDE_READ);
874
875 l->next = &listener_list;
876 l->prev = listener_list.prev;
877 l->next->prev = l;
878 l->prev->next = l;
879 l->transport = transport;
880
881 if (transport) {
882 l->disconnect.opaque = l;
883 l->disconnect.func = listener_disconnect;
884 add_transport_disconnect(transport, &l->disconnect);
885 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100886 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800887
888nomem:
889 fatal("cannot allocate listener");
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100890 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800891}
892
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800893#ifdef HAVE_WIN32_PROC
894static BOOL WINAPI ctrlc_handler(DWORD type)
895{
896 exit(STATUS_CONTROL_C_EXIT);
897 return TRUE;
898}
899#endif
900
901static void adb_cleanup(void)
902{
903 usb_cleanup();
904}
905
906void start_logging(void)
907{
908#ifdef HAVE_WIN32_PROC
909 char temp[ MAX_PATH ];
910 FILE* fnul;
911 FILE* flog;
912
913 GetTempPath( sizeof(temp) - 8, temp );
914 strcat( temp, "adb.log" );
915
916 /* Win32 specific redirections */
917 fnul = fopen( "NUL", "rt" );
918 if (fnul != NULL)
919 stdin[0] = fnul[0];
920
921 flog = fopen( temp, "at" );
922 if (flog == NULL)
923 flog = fnul;
924
925 setvbuf( flog, NULL, _IONBF, 0 );
926
927 stdout[0] = flog[0];
928 stderr[0] = flog[0];
929 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
930#else
931 int fd;
932
933 fd = unix_open("/dev/null", O_RDONLY);
934 dup2(fd, 0);
JP Abgrall408fa572011-03-16 15:57:42 -0700935 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936
937 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
938 if(fd < 0) {
939 fd = unix_open("/dev/null", O_WRONLY);
940 }
941 dup2(fd, 1);
942 dup2(fd, 2);
JP Abgrall408fa572011-03-16 15:57:42 -0700943 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800944 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
945#endif
946}
947
948#if !ADB_HOST
949void start_device_log(void)
950{
951 int fd;
Mike Lockwood1f546e62009-05-25 18:17:55 -0400952 char path[PATH_MAX];
953 struct tm now;
954 time_t t;
955 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800956
Mike Lockwood1f546e62009-05-25 18:17:55 -0400957 // read the trace mask from persistent property persist.adb.trace_mask
958 // give up if the property is not set or cannot be parsed
959 property_get("persist.adb.trace_mask", value, "");
960 if (sscanf(value, "%x", &adb_trace_mask) != 1)
961 return;
962
963 adb_mkdir("/data/adb", 0775);
964 tzset();
965 time(&t);
966 localtime_r(&t, &now);
967 strftime(path, sizeof(path),
968 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
969 &now);
970 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 if (fd < 0)
972 return;
973
974 // redirect stdout and stderr to the log file
975 dup2(fd, 1);
976 dup2(fd, 2);
977 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
Benoit Goby95ef8282011-02-01 18:57:41 -0800978 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800979
980 fd = unix_open("/dev/null", O_RDONLY);
981 dup2(fd, 0);
Benoit Goby95ef8282011-02-01 18:57:41 -0800982 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800983}
984#endif
985
986#if ADB_HOST
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100987int launch_server(int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800988{
989#ifdef HAVE_WIN32_PROC
990 /* we need to start the server in the background */
991 /* we create a PIPE that will be used to wait for the server's "OK" */
992 /* message since the pipe handles must be inheritable, we use a */
993 /* security attribute */
994 HANDLE pipe_read, pipe_write;
Ray Donnelly267aa8b2012-11-29 01:18:50 +0000995 HANDLE stdout_handle, stderr_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996 SECURITY_ATTRIBUTES sa;
997 STARTUPINFO startup;
998 PROCESS_INFORMATION pinfo;
999 char program_path[ MAX_PATH ];
1000 int ret;
1001
1002 sa.nLength = sizeof(sa);
1003 sa.lpSecurityDescriptor = NULL;
1004 sa.bInheritHandle = TRUE;
1005
1006 /* create pipe, and ensure its read handle isn't inheritable */
1007 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
1008 if (!ret) {
1009 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
1010 return -1;
1011 }
1012
1013 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
1014
Ray Donnelly267aa8b2012-11-29 01:18:50 +00001015 /* Some programs want to launch an adb command and collect its output by
1016 * calling CreateProcess with inheritable stdout/stderr handles, then
1017 * using read() to get its output. When this happens, the stdout/stderr
1018 * handles passed to the adb client process will also be inheritable.
1019 * When starting the adb server here, care must be taken to reset them
1020 * to non-inheritable.
1021 * Otherwise, something bad happens: even if the adb command completes,
1022 * the calling process is stuck while read()-ing from the stdout/stderr
1023 * descriptors, because they're connected to corresponding handles in the
1024 * adb server process (even if the latter never uses/writes to them).
1025 */
1026 stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
1027 stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
1028 if (stdout_handle != INVALID_HANDLE_VALUE) {
1029 SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
1030 }
1031 if (stderr_handle != INVALID_HANDLE_VALUE) {
1032 SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
1033 }
1034
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 ZeroMemory( &startup, sizeof(startup) );
1036 startup.cb = sizeof(startup);
1037 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
1038 startup.hStdOutput = pipe_write;
1039 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
1040 startup.dwFlags = STARTF_USESTDHANDLES;
1041
1042 ZeroMemory( &pinfo, sizeof(pinfo) );
1043
1044 /* get path of current program */
1045 GetModuleFileName( NULL, program_path, sizeof(program_path) );
1046
1047 ret = CreateProcess(
1048 program_path, /* program path */
1049 "adb fork-server server",
1050 /* the fork-server argument will set the
1051 debug = 2 in the child */
1052 NULL, /* process handle is not inheritable */
1053 NULL, /* thread handle is not inheritable */
1054 TRUE, /* yes, inherit some handles */
1055 DETACHED_PROCESS, /* the new process doesn't have a console */
1056 NULL, /* use parent's environment block */
1057 NULL, /* use parent's starting directory */
1058 &startup, /* startup info, i.e. std handles */
1059 &pinfo );
1060
1061 CloseHandle( pipe_write );
1062
1063 if (!ret) {
1064 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
1065 CloseHandle( pipe_read );
1066 return -1;
1067 }
1068
1069 CloseHandle( pinfo.hProcess );
1070 CloseHandle( pinfo.hThread );
1071
1072 /* wait for the "OK\n" message */
1073 {
1074 char temp[3];
1075 DWORD count;
1076
1077 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
1078 CloseHandle( pipe_read );
1079 if ( !ret ) {
1080 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
1081 return -1;
1082 }
1083 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1084 fprintf(stderr, "ADB server didn't ACK\n" );
1085 return -1;
1086 }
1087 }
1088#elif defined(HAVE_FORKEXEC)
1089 char path[PATH_MAX];
1090 int fd[2];
1091
1092 // set up a pipe so the child can tell us when it is ready.
1093 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
1094 if (pipe(fd)) {
1095 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
1096 return -1;
1097 }
Alexey Tarasov31664102009-10-22 02:55:00 +11001098 get_my_path(path, PATH_MAX);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001099 pid_t pid = fork();
1100 if(pid < 0) return -1;
1101
1102 if (pid == 0) {
1103 // child side of the fork
1104
1105 // redirect stderr to the pipe
1106 // we use stderr instead of stdout due to stdout's buffering behavior.
1107 adb_close(fd[0]);
1108 dup2(fd[1], STDERR_FILENO);
1109 adb_close(fd[1]);
1110
Matt Gumbeld7b33082012-11-14 10:16:17 -08001111 char str_port[30];
1112 snprintf(str_port, sizeof(str_port), "%d", server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001113 // child process
Matt Gumbeld7b33082012-11-14 10:16:17 -08001114 int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001115 // this should not return
1116 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
1117 } else {
1118 // parent side of the fork
1119
1120 char temp[3];
1121
1122 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
1123 // wait for the "OK\n" message
1124 adb_close(fd[1]);
1125 int ret = adb_read(fd[0], temp, 3);
JP Abgrall408fa572011-03-16 15:57:42 -07001126 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001127 adb_close(fd[0]);
1128 if (ret < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -07001129 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001130 return -1;
1131 }
1132 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1133 fprintf(stderr, "ADB server didn't ACK\n" );
1134 return -1;
1135 }
1136
1137 setsid();
1138 }
1139#else
1140#error "cannot implement background server start on this platform"
1141#endif
1142 return 0;
1143}
1144#endif
1145
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001146/* Constructs a local name of form tcp:port.
1147 * target_str points to the target string, it's content will be overwritten.
1148 * target_size is the capacity of the target string.
1149 * server_port is the port number to use for the local name.
1150 */
1151void build_local_name(char* target_str, size_t target_size, int server_port)
1152{
1153 snprintf(target_str, target_size, "tcp:%d", server_port);
1154}
1155
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001156#if !ADB_HOST
1157static int should_drop_privileges() {
Nick Kralevich5890fe32012-01-19 13:11:35 -08001158#ifndef ALLOW_ADBD_ROOT
1159 return 1;
1160#else /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001161 int secure = 0;
1162 char value[PROPERTY_VALUE_MAX];
1163
1164 /* run adbd in secure mode if ro.secure is set and
1165 ** we are not in the emulator
1166 */
1167 property_get("ro.kernel.qemu", value, "");
1168 if (strcmp(value, "1") != 0) {
1169 property_get("ro.secure", value, "1");
1170 if (strcmp(value, "1") == 0) {
1171 // don't run as root if ro.secure is set...
1172 secure = 1;
1173
1174 // ... except we allow running as root in userdebug builds if the
1175 // service.adb.root property has been set by the "adb root" command
1176 property_get("ro.debuggable", value, "");
1177 if (strcmp(value, "1") == 0) {
1178 property_get("service.adb.root", value, "");
1179 if (strcmp(value, "1") == 0) {
1180 secure = 0;
1181 }
1182 }
1183 }
1184 }
1185 return secure;
Nick Kralevich5890fe32012-01-19 13:11:35 -08001186#endif /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001187}
1188#endif /* !ADB_HOST */
1189
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001190int adb_main(int is_daemon, int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191{
1192#if !ADB_HOST
Mike Lockwood2f38b692009-08-24 15:58:40 -07001193 int port;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001194 char value[PROPERTY_VALUE_MAX];
Nick Kralevicheb68fa82012-04-02 13:00:35 -07001195
1196 umask(000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001197#endif
1198
1199 atexit(adb_cleanup);
1200#ifdef HAVE_WIN32_PROC
1201 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
1202#elif defined(HAVE_FORKEXEC)
JP Abgrall408fa572011-03-16 15:57:42 -07001203 // No SIGCHLD. Let the service subproc handle its children.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001204 signal(SIGPIPE, SIG_IGN);
1205#endif
1206
1207 init_transport_registration();
1208
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001209#if ADB_HOST
1210 HOST = 1;
Xavier Ducroheta09fbd12009-05-20 17:33:53 -07001211 usb_vendors_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 usb_init();
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001213 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001214 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001216 char local_name[30];
1217 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001218 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 exit(1);
1220 }
1221#else
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001222 property_get("ro.adb.secure", value, "0");
1223 auth_enabled = !strcmp(value, "1");
1224 if (auth_enabled)
1225 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226
Jeff Sharkeyd6d42862012-09-06 13:05:40 -07001227 // Our external storage path may be different than apps, since
1228 // we aren't able to bind mount after dropping root.
1229 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
1230 if (NULL != adb_external_storage) {
1231 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
1232 } else {
1233 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
1234 " unchanged.\n");
1235 }
1236
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001237 /* don't listen on a port (default 5037) if running in secure mode */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001238 /* don't run as root if we are running in secure mode */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001239 if (should_drop_privileges()) {
Mike Lockwood5f4b0512009-08-04 20:37:51 -04001240 struct __user_cap_header_struct header;
Nick Kralevich109f4e12013-02-14 15:47:14 -08001241 struct __user_cap_data_struct cap[2];
Mike Lockwood5f4b0512009-08-04 20:37:51 -04001242
Nick Kralevich44db9902010-08-27 14:35:07 -07001243 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
1244 exit(1);
1245 }
Mike Lockwood5f4b0512009-08-04 20:37:51 -04001246
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247 /* add extra groups:
1248 ** AID_ADB to access the USB driver
1249 ** AID_LOG to read system logs (adb logcat)
1250 ** AID_INPUT to diagnose input issues (getevent)
1251 ** AID_INET to diagnose network issues (netcfg, ping)
1252 ** AID_GRAPHICS to access the frame buffer
The Android Open Source Project20155492009-03-11 12:12:01 -07001253 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
Dianne Hackborn50458cf2012-03-07 12:57:14 -08001254 ** AID_SDCARD_R to allow reading from the SD card
Mike Lockwood6a3075c2009-05-25 13:52:00 -04001255 ** AID_SDCARD_RW to allow writing to the SD card
Mike Lockwoodd969faa2010-02-24 16:07:23 -05001256 ** AID_MOUNT to allow unmounting the SD card before rebooting
JP Abgrall61b90bd2011-11-09 10:30:08 -08001257 ** AID_NET_BW_STATS to read out qtaguid statistics
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001258 */
The Android Open Source Project20155492009-03-11 12:12:01 -07001259 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
Dianne Hackborn50458cf2012-03-07 12:57:14 -08001260 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
1261 AID_MOUNT, AID_NET_BW_STATS };
Nick Kralevich44db9902010-08-27 14:35:07 -07001262 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
1263 exit(1);
1264 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001265
1266 /* then switch user and group to "shell" */
Nick Kralevich44db9902010-08-27 14:35:07 -07001267 if (setgid(AID_SHELL) != 0) {
1268 exit(1);
1269 }
1270 if (setuid(AID_SHELL) != 0) {
1271 exit(1);
1272 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273
Nick Kralevich109f4e12013-02-14 15:47:14 -08001274 memset(&header, 0, sizeof(header));
1275 memset(cap, 0, sizeof(cap));
1276
Mike Lockwood5f4b0512009-08-04 20:37:51 -04001277 /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */
Nick Kralevich109f4e12013-02-14 15:47:14 -08001278 header.version = _LINUX_CAPABILITY_VERSION_3;
Mike Lockwood5f4b0512009-08-04 20:37:51 -04001279 header.pid = 0;
Nick Kralevich109f4e12013-02-14 15:47:14 -08001280 cap[CAP_TO_INDEX(CAP_SYS_BOOT)].effective |= CAP_TO_MASK(CAP_SYS_BOOT);
1281 cap[CAP_TO_INDEX(CAP_SYS_BOOT)].permitted |= CAP_TO_MASK(CAP_SYS_BOOT);
1282 capset(&header, cap);
Mike Lockwood5f4b0512009-08-04 20:37:51 -04001283
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001284 D("Local port disabled\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001285 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001286 char local_name[30];
1287 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001288 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001289 exit(1);
1290 }
1291 }
1292
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001293 int usb = 0;
1294 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001295 // listen on USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001296 usb_init();
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001297 usb = 1;
1298 }
1299
1300 // If one of these properties is set, also listen on that port
1301 // If one of the properties isn't set and we couldn't listen on usb,
1302 // listen on the default port.
1303 property_get("service.adb.tcp.port", value, "");
1304 if (!value[0]) {
1305 property_get("persist.adb.tcp.port", value, "");
1306 }
1307 if (sscanf(value, "%d", &port) == 1 && port > 0) {
1308 printf("using port=%d\n", port);
1309 // listen on TCP port specified by service.adb.tcp.port property
1310 local_init(port);
1311 } else if (!usb) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001312 // listen on default port
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001313 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001314 }
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001315
JP Abgrall408fa572011-03-16 15:57:42 -07001316 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001317 init_jdwp();
JP Abgrall408fa572011-03-16 15:57:42 -07001318 D("adb_main(): post init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001319#endif
1320
1321 if (is_daemon)
1322 {
1323 // inform our parent that we are up and running.
1324#ifdef HAVE_WIN32_PROC
1325 DWORD count;
1326 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
1327#elif defined(HAVE_FORKEXEC)
1328 fprintf(stderr, "OK\n");
1329#endif
1330 start_logging();
1331 }
JP Abgrall408fa572011-03-16 15:57:42 -07001332 D("Event loop starting\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001333
1334 fdevent_loop();
1335
1336 usb_cleanup();
1337
1338 return 0;
1339}
1340
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001341#if ADB_HOST
1342void connect_device(char* host, char* buffer, int buffer_size)
1343{
1344 int port, fd;
1345 char* portstr = strchr(host, ':');
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001346 char hostbuf[100];
1347 char serial[100];
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001348
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001349 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
1350 if (portstr) {
Scott Andersonc7993af2012-05-25 13:55:46 -07001351 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001352 snprintf(buffer, buffer_size, "bad host name %s", host);
1353 return;
1354 }
1355 // zero terminate the host at the point we found the colon
1356 hostbuf[portstr - host] = 0;
1357 if (sscanf(portstr + 1, "%d", &port) == 0) {
1358 snprintf(buffer, buffer_size, "bad port number %s", portstr);
1359 return;
1360 }
1361 } else {
1362 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001363 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001364
1365 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
1366 if (find_transport(serial)) {
1367 snprintf(buffer, buffer_size, "already connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001368 return;
1369 }
1370
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001371 fd = socket_network_client(hostbuf, port, SOCK_STREAM);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001372 if (fd < 0) {
1373 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
1374 return;
1375 }
1376
1377 D("client: connected on remote on fd %d\n", fd);
1378 close_on_exec(fd);
1379 disable_tcp_nagle(fd);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001380 register_socket_transport(fd, serial, port, 0);
1381 snprintf(buffer, buffer_size, "connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001382}
1383
1384void connect_emulator(char* port_spec, char* buffer, int buffer_size)
1385{
1386 char* port_separator = strchr(port_spec, ',');
1387 if (!port_separator) {
1388 snprintf(buffer, buffer_size,
1389 "unable to parse '%s' as <console port>,<adb port>",
1390 port_spec);
1391 return;
1392 }
1393
1394 // Zero-terminate console port and make port_separator point to 2nd port.
1395 *port_separator++ = 0;
1396 int console_port = strtol(port_spec, NULL, 0);
1397 int adb_port = strtol(port_separator, NULL, 0);
1398 if (!(console_port > 0 && adb_port > 0)) {
1399 *(port_separator - 1) = ',';
1400 snprintf(buffer, buffer_size,
1401 "Invalid port numbers: Expected positive numbers, got '%s'",
1402 port_spec);
1403 return;
1404 }
1405
1406 /* Check if the emulator is already known.
1407 * Note: There's a small but harmless race condition here: An emulator not
1408 * present just yet could be registered by another invocation right
1409 * after doing this check here. However, local_connect protects
1410 * against double-registration too. From here, a better error message
1411 * can be produced. In the case of the race condition, the very specific
1412 * error message won't be shown, but the data doesn't get corrupted. */
1413 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
1414 if (known_emulator != NULL) {
1415 snprintf(buffer, buffer_size,
1416 "Emulator on port %d already registered.", adb_port);
1417 return;
1418 }
1419
1420 /* Check if more emulators can be registered. Similar unproblematic
1421 * race condition as above. */
1422 int candidate_slot = get_available_local_transport_index();
1423 if (candidate_slot < 0) {
1424 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
1425 return;
1426 }
1427
1428 /* Preconditions met, try to connect to the emulator. */
1429 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
1430 snprintf(buffer, buffer_size,
1431 "Connected to emulator on ports %d,%d", console_port, adb_port);
1432 } else {
1433 snprintf(buffer, buffer_size,
1434 "Could not connect to emulator on ports %d,%d",
1435 console_port, adb_port);
1436 }
1437}
1438#endif
1439
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001440int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1441{
1442 atransport *transport = NULL;
1443 char buf[4096];
1444
1445 if(!strcmp(service, "kill")) {
1446 fprintf(stderr,"adb server killed by remote request\n");
1447 fflush(stdout);
1448 adb_write(reply_fd, "OKAY", 4);
1449 usb_cleanup();
1450 exit(0);
1451 }
1452
1453#if ADB_HOST
1454 // "transport:" is used for switching transport with a specified serial number
1455 // "transport-usb:" is used for switching transport to the only USB transport
1456 // "transport-local:" is used for switching transport to the only local transport
1457 // "transport-any:" is used for switching transport to the only transport
1458 if (!strncmp(service, "transport", strlen("transport"))) {
1459 char* error_string = "unknown failure";
1460 transport_type type = kTransportAny;
1461
1462 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1463 type = kTransportUsb;
1464 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1465 type = kTransportLocal;
1466 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1467 type = kTransportAny;
1468 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1469 service += strlen("transport:");
Tom Marlin3175c8e2011-07-27 12:56:14 -05001470 serial = service;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001471 }
1472
1473 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1474
1475 if (transport) {
1476 s->transport = transport;
1477 adb_write(reply_fd, "OKAY", 4);
1478 } else {
1479 sendfailmsg(reply_fd, error_string);
1480 }
1481 return 1;
1482 }
1483
1484 // return a list of all connected devices
Scott Andersone109d262012-04-20 11:21:14 -07001485 if (!strncmp(service, "devices", 7)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001486 char buffer[4096];
Scott Andersone109d262012-04-20 11:21:14 -07001487 int use_long = !strcmp(service+7, "-l");
1488 if (use_long || service[7] == 0) {
1489 memset(buf, 0, sizeof(buf));
1490 memset(buffer, 0, sizeof(buffer));
1491 D("Getting device list \n");
1492 list_transports(buffer, sizeof(buffer), use_long);
1493 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer);
1494 D("Wrote device list \n");
1495 writex(reply_fd, buf, strlen(buf));
1496 return 0;
1497 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001498 }
1499
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001500 // add a new TCP transport, device or emulator
Mike Lockwood2f38b692009-08-24 15:58:40 -07001501 if (!strncmp(service, "connect:", 8)) {
1502 char buffer[4096];
Mike Lockwood2f38b692009-08-24 15:58:40 -07001503 char* host = service + 8;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001504 if (!strncmp(host, "emu:", 4)) {
1505 connect_emulator(host + 4, buffer, sizeof(buffer));
1506 } else {
1507 connect_device(host, buffer, sizeof(buffer));
Mike Lockwood2f38b692009-08-24 15:58:40 -07001508 }
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001509 // Send response for emulator and device
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001510 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
1511 writex(reply_fd, buf, strlen(buf));
1512 return 0;
1513 }
1514
1515 // remove TCP transport
1516 if (!strncmp(service, "disconnect:", 11)) {
1517 char buffer[4096];
1518 memset(buffer, 0, sizeof(buffer));
1519 char* serial = service + 11;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001520 if (serial[0] == 0) {
1521 // disconnect from all TCP devices
1522 unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001523 } else {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001524 char hostbuf[100];
1525 // assume port 5555 if no port is specified
1526 if (!strchr(serial, ':')) {
1527 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1528 serial = hostbuf;
1529 }
1530 atransport *t = find_transport(serial);
1531
1532 if (t) {
1533 unregister_transport(t);
1534 } else {
1535 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1536 }
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001537 }
1538
1539 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
Mike Lockwood2f38b692009-08-24 15:58:40 -07001540 writex(reply_fd, buf, strlen(buf));
1541 return 0;
1542 }
1543
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001544 // returns our value for ADB_SERVER_VERSION
1545 if (!strcmp(service, "version")) {
1546 char version[12];
1547 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
1548 snprintf(buf, sizeof buf, "OKAY%04x%s", (unsigned)strlen(version), version);
1549 writex(reply_fd, buf, strlen(buf));
1550 return 0;
1551 }
1552
1553 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1554 char *out = "unknown";
1555 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1556 if (transport && transport->serial) {
1557 out = transport->serial;
1558 }
1559 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out);
1560 writex(reply_fd, buf, strlen(buf));
1561 return 0;
1562 }
Scott Andersone109d262012-04-20 11:21:14 -07001563 if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
1564 char *out = "unknown";
1565 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1566 if (transport && transport->devpath) {
1567 out = transport->devpath;
1568 }
1569 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out);
1570 writex(reply_fd, buf, strlen(buf));
1571 return 0;
1572 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001573 // indicates a new emulator instance has started
1574 if (!strncmp(service,"emulator:",9)) {
1575 int port = atoi(service+9);
1576 local_connect(port);
1577 /* we don't even need to send a reply */
1578 return 0;
1579 }
1580#endif // ADB_HOST
1581
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001582 if(!strcmp(service,"list-forward")) {
1583 // Create the list of forward redirections.
1584 char header[9];
1585 int buffer_size = format_listeners(NULL, 0);
1586 // Add one byte for the trailing zero.
1587 char* buffer = malloc(buffer_size+1);
1588 (void) format_listeners(buffer, buffer_size+1);
1589 snprintf(header, sizeof header, "OKAY%04x", buffer_size);
1590 writex(reply_fd, header, 8);
1591 writex(reply_fd, buffer, buffer_size);
1592 free(buffer);
1593 return 0;
1594 }
1595
1596 if (!strcmp(service,"killforward-all")) {
1597 remove_all_listeners();
1598 adb_write(reply_fd, "OKAYOKAY", 8);
1599 return 0;
1600 }
1601
1602 if(!strncmp(service,"forward:",8) ||
1603 !strncmp(service,"killforward:",12)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001604 char *local, *remote, *err;
1605 int r;
1606 atransport *transport;
1607
1608 int createForward = strncmp(service,"kill",4);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001609 int no_rebind = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001610
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001611 local = strchr(service, ':') + 1;
1612
1613 // Handle forward:norebind:<local>... here
1614 if (createForward && !strncmp(local, "norebind:", 9)) {
1615 no_rebind = 1;
1616 local = strchr(local, ':') + 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001617 }
1618
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001619 remote = strchr(local,';');
1620
1621 if (createForward) {
1622 // Check forward: parameter format: '<local>;<remote>'
1623 if(remote == 0) {
1624 sendfailmsg(reply_fd, "malformed forward spec");
1625 return 0;
1626 }
1627
1628 *remote++ = 0;
1629 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')){
1630 sendfailmsg(reply_fd, "malformed forward spec");
1631 return 0;
1632 }
1633 } else {
1634 // Check killforward: parameter format: '<local>'
1635 if (local[0] == 0) {
1636 sendfailmsg(reply_fd, "malformed forward spec");
1637 return 0;
1638 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001639 }
1640
1641 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1642 if (!transport) {
1643 sendfailmsg(reply_fd, err);
1644 return 0;
1645 }
1646
1647 if (createForward) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001648 r = install_listener(local, remote, transport, no_rebind);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001649 } else {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001650 r = remove_listener(local, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001651 }
1652 if(r == 0) {
1653 /* 1st OKAY is connect, 2nd OKAY is status */
1654 writex(reply_fd, "OKAYOKAY", 8);
1655 return 0;
1656 }
1657
1658 if (createForward) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001659 const char* message;
1660 switch (r) {
1661 case INSTALL_STATUS_CANNOT_BIND:
1662 message = "cannot bind to socket";
1663 break;
1664 case INSTALL_STATUS_CANNOT_REBIND:
1665 message = "cannot rebind existing socket";
1666 break;
1667 default:
1668 message = "internal error";
1669 }
1670 sendfailmsg(reply_fd, message);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 } else {
1672 sendfailmsg(reply_fd, "cannot remove listener");
1673 }
1674 return 0;
1675 }
1676
1677 if(!strncmp(service,"get-state",strlen("get-state"))) {
1678 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1679 char *state = connection_state_name(transport);
1680 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(state),state);
1681 writex(reply_fd, buf, strlen(buf));
1682 return 0;
1683 }
1684 return -1;
1685}
1686
1687#if !ADB_HOST
1688int recovery_mode = 0;
1689#endif
1690
1691int main(int argc, char **argv)
1692{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001693#if ADB_HOST
1694 adb_sysdeps_init();
JP Abgrall408fa572011-03-16 15:57:42 -07001695 adb_trace_init();
1696 D("Handling commandline()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001697 return adb_commandline(argc - 1, argv + 1);
1698#else
Vladimir Chtchetkine28781b02012-02-27 10:41:53 -08001699 /* If adbd runs inside the emulator this will enable adb tracing via
1700 * adb-debug qemud service in the emulator. */
1701 adb_qemu_trace_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001702 if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
1703 adb_device_banner = "recovery";
1704 recovery_mode = 1;
1705 }
Mike Lockwood1f546e62009-05-25 18:17:55 -04001706
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001707 start_device_log();
JP Abgrall408fa572011-03-16 15:57:42 -07001708 D("Handling main()\n");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001709 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001710#endif
1711}