blob: 6e2c0a9221c369174a72bb1d6b0b09d6bc7823ce [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>
24#include <string.h>
25#include <time.h>
Mike Lockwood1f546e62009-05-25 18:17:55 -040026#include <sys/time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
28#include "sysdeps.h"
29#include "adb.h"
30
31#if !ADB_HOST
32#include <private/android_filesystem_config.h>
Mike Lockwood5f4b0512009-08-04 20:37:51 -040033#include <linux/capability.h>
34#include <linux/prctl.h>
Xavier Ducroheta09fbd12009-05-20 17:33:53 -070035#else
36#include "usb_vendors.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#endif
38
JP Abgrall69c5c4c2011-02-18 14:16:59 -080039#if ADB_TRACE
40ADB_MUTEX_DEFINE( D_lock );
41#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43int HOST = 0;
44
45static const char *adb_device_banner = "device";
46
47void fatal(const char *fmt, ...)
48{
49 va_list ap;
50 va_start(ap, fmt);
51 fprintf(stderr, "error: ");
52 vfprintf(stderr, fmt, ap);
53 fprintf(stderr, "\n");
54 va_end(ap);
55 exit(-1);
56}
57
58void fatal_errno(const char *fmt, ...)
59{
60 va_list ap;
61 va_start(ap, fmt);
62 fprintf(stderr, "error: %s: ", strerror(errno));
63 vfprintf(stderr, fmt, ap);
64 fprintf(stderr, "\n");
65 va_end(ap);
66 exit(-1);
67}
68
69int adb_trace_mask;
70
71/* read a comma/space/colum/semi-column separated list of tags
72 * from the ADB_TRACE environment variable and build the trace
73 * mask from it. note that '1' and 'all' are special cases to
74 * enable all tracing
75 */
76void adb_trace_init(void)
77{
78 const char* p = getenv("ADB_TRACE");
79 const char* q;
80
81 static const struct {
82 const char* tag;
83 int flag;
84 } tags[] = {
85 { "1", 0 },
86 { "all", 0 },
87 { "adb", TRACE_ADB },
88 { "sockets", TRACE_SOCKETS },
89 { "packets", TRACE_PACKETS },
90 { "rwx", TRACE_RWX },
91 { "usb", TRACE_USB },
92 { "sync", TRACE_SYNC },
93 { "sysdeps", TRACE_SYSDEPS },
94 { "transport", TRACE_TRANSPORT },
95 { "jdwp", TRACE_JDWP },
96 { NULL, 0 }
97 };
98
99 if (p == NULL)
100 return;
101
102 /* use a comma/column/semi-colum/space separated list */
103 while (*p) {
104 int len, tagn;
105
106 q = strpbrk(p, " ,:;");
107 if (q == NULL) {
108 q = p + strlen(p);
109 }
110 len = q - p;
111
112 for (tagn = 0; tags[tagn].tag != NULL; tagn++)
113 {
114 int taglen = strlen(tags[tagn].tag);
115
116 if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
117 {
118 int flag = tags[tagn].flag;
119 if (flag == 0) {
120 adb_trace_mask = ~0;
121 return;
122 }
123 adb_trace_mask |= (1 << flag);
124 break;
125 }
126 }
127 p = q;
128 if (*p)
129 p++;
130 }
131}
132
133
134apacket *get_apacket(void)
135{
136 apacket *p = malloc(sizeof(apacket));
137 if(p == 0) fatal("failed to allocate an apacket");
138 memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
139 return p;
140}
141
142void put_apacket(apacket *p)
143{
144 free(p);
145}
146
147void handle_online(void)
148{
149 D("adb: online\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150}
151
152void handle_offline(atransport *t)
153{
154 D("adb: offline\n");
155 //Close the associated usb
156 run_transport_disconnects(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157}
158
159#if TRACE_PACKETS
160#define DUMPMAX 32
161void print_packet(const char *label, apacket *p)
162{
163 char *tag;
164 char *x;
165 unsigned count;
166
167 switch(p->msg.command){
168 case A_SYNC: tag = "SYNC"; break;
169 case A_CNXN: tag = "CNXN" ; break;
170 case A_OPEN: tag = "OPEN"; break;
171 case A_OKAY: tag = "OKAY"; break;
172 case A_CLSE: tag = "CLSE"; break;
173 case A_WRTE: tag = "WRTE"; break;
174 default: tag = "????"; break;
175 }
176
177 fprintf(stderr, "%s: %s %08x %08x %04x \"",
178 label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
179 count = p->msg.data_length;
180 x = (char*) p->data;
181 if(count > DUMPMAX) {
182 count = DUMPMAX;
183 tag = "\n";
184 } else {
185 tag = "\"\n";
186 }
187 while(count-- > 0){
188 if((*x >= ' ') && (*x < 127)) {
189 fputc(*x, stderr);
190 } else {
191 fputc('.', stderr);
192 }
193 x++;
194 }
195 fprintf(stderr, tag);
196}
197#endif
198
199static void send_ready(unsigned local, unsigned remote, atransport *t)
200{
201 D("Calling send_ready \n");
202 apacket *p = get_apacket();
203 p->msg.command = A_OKAY;
204 p->msg.arg0 = local;
205 p->msg.arg1 = remote;
206 send_packet(p, t);
207}
208
209static void send_close(unsigned local, unsigned remote, atransport *t)
210{
211 D("Calling send_close \n");
212 apacket *p = get_apacket();
213 p->msg.command = A_CLSE;
214 p->msg.arg0 = local;
215 p->msg.arg1 = remote;
216 send_packet(p, t);
217}
218
219static void send_connect(atransport *t)
220{
221 D("Calling send_connect \n");
222 apacket *cp = get_apacket();
223 cp->msg.command = A_CNXN;
224 cp->msg.arg0 = A_VERSION;
225 cp->msg.arg1 = MAX_PAYLOAD;
226 snprintf((char*) cp->data, sizeof cp->data, "%s::",
227 HOST ? "host" : adb_device_banner);
228 cp->msg.data_length = strlen((char*) cp->data) + 1;
229 send_packet(cp, t);
230#if ADB_HOST
231 /* XXX why sleep here? */
232 // allow the device some time to respond to the connect message
233 adb_sleep_ms(1000);
234#endif
235}
236
237static char *connection_state_name(atransport *t)
238{
239 if (t == NULL) {
240 return "unknown";
241 }
242
243 switch(t->connection_state) {
244 case CS_BOOTLOADER:
245 return "bootloader";
246 case CS_DEVICE:
247 return "device";
248 case CS_OFFLINE:
249 return "offline";
250 default:
251 return "unknown";
252 }
253}
254
255void parse_banner(char *banner, atransport *t)
256{
257 char *type, *product, *end;
258
259 D("parse_banner: %s\n", banner);
260 type = banner;
261 product = strchr(type, ':');
262 if(product) {
263 *product++ = 0;
264 } else {
265 product = "";
266 }
267
268 /* remove trailing ':' */
269 end = strchr(product, ':');
270 if(end) *end = 0;
271
272 /* save product name in device structure */
273 if (t->product == NULL) {
274 t->product = strdup(product);
275 } else if (strcmp(product, t->product) != 0) {
276 free(t->product);
277 t->product = strdup(product);
278 }
279
280 if(!strcmp(type, "bootloader")){
281 D("setting connection_state to CS_BOOTLOADER\n");
282 t->connection_state = CS_BOOTLOADER;
283 update_transports();
284 return;
285 }
286
287 if(!strcmp(type, "device")) {
288 D("setting connection_state to CS_DEVICE\n");
289 t->connection_state = CS_DEVICE;
290 update_transports();
291 return;
292 }
293
294 if(!strcmp(type, "recovery")) {
295 D("setting connection_state to CS_RECOVERY\n");
296 t->connection_state = CS_RECOVERY;
297 update_transports();
298 return;
299 }
300
301 t->connection_state = CS_HOST;
302}
303
304void handle_packet(apacket *p, atransport *t)
305{
306 asocket *s;
307
Viral Mehta899913f2010-06-16 18:41:28 +0530308 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
309 ((char*) (&(p->msg.command)))[1],
310 ((char*) (&(p->msg.command)))[2],
311 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 print_packet("recv", p);
313
314 switch(p->msg.command){
315 case A_SYNC:
316 if(p->msg.arg0){
317 send_packet(p, t);
318 if(HOST) send_connect(t);
319 } else {
320 t->connection_state = CS_OFFLINE;
321 handle_offline(t);
322 send_packet(p, t);
323 }
324 return;
325
326 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
327 /* XXX verify version, etc */
328 if(t->connection_state != CS_OFFLINE) {
329 t->connection_state = CS_OFFLINE;
330 handle_offline(t);
331 }
332 parse_banner((char*) p->data, t);
333 handle_online();
334 if(!HOST) send_connect(t);
335 break;
336
337 case A_OPEN: /* OPEN(local-id, 0, "destination") */
338 if(t->connection_state != CS_OFFLINE) {
339 char *name = (char*) p->data;
340 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
341 s = create_local_service_socket(name);
342 if(s == 0) {
343 send_close(0, p->msg.arg0, t);
344 } else {
345 s->peer = create_remote_socket(p->msg.arg0, t);
346 s->peer->peer = s;
347 send_ready(s->id, s->peer->id, t);
348 s->ready(s);
349 }
350 }
351 break;
352
353 case A_OKAY: /* READY(local-id, remote-id, "") */
354 if(t->connection_state != CS_OFFLINE) {
355 if((s = find_local_socket(p->msg.arg1))) {
356 if(s->peer == 0) {
357 s->peer = create_remote_socket(p->msg.arg0, t);
358 s->peer->peer = s;
359 }
360 s->ready(s);
361 }
362 }
363 break;
364
365 case A_CLSE: /* CLOSE(local-id, remote-id, "") */
366 if(t->connection_state != CS_OFFLINE) {
367 if((s = find_local_socket(p->msg.arg1))) {
368 s->close(s);
369 }
370 }
371 break;
372
373 case A_WRTE:
374 if(t->connection_state != CS_OFFLINE) {
375 if((s = find_local_socket(p->msg.arg1))) {
376 unsigned rid = p->msg.arg0;
377 p->len = p->msg.data_length;
378
379 if(s->enqueue(s, p) == 0) {
380 D("Enqueue the socket\n");
381 send_ready(s->id, rid, t);
382 }
383 return;
384 }
385 }
386 break;
387
388 default:
389 printf("handle_packet: what is %08x?!\n", p->msg.command);
390 }
391
392 put_apacket(p);
393}
394
395alistener listener_list = {
396 .next = &listener_list,
397 .prev = &listener_list,
398};
399
400static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
401{
402 asocket *s;
403
404 if(ev & FDE_READ) {
405 struct sockaddr addr;
406 socklen_t alen;
407 int fd;
408
409 alen = sizeof(addr);
410 fd = adb_socket_accept(_fd, &addr, &alen);
411 if(fd < 0) return;
412
413 adb_socket_setbufsize(fd, CHUNK_SIZE);
414
415 s = create_local_socket(fd);
416 if(s) {
417 connect_to_smartsocket(s);
418 return;
419 }
420
421 adb_close(fd);
422 }
423}
424
425static void listener_event_func(int _fd, unsigned ev, void *_l)
426{
427 alistener *l = _l;
428 asocket *s;
429
430 if(ev & FDE_READ) {
431 struct sockaddr addr;
432 socklen_t alen;
433 int fd;
434
435 alen = sizeof(addr);
436 fd = adb_socket_accept(_fd, &addr, &alen);
437 if(fd < 0) return;
438
439 s = create_local_socket(fd);
440 if(s) {
441 s->transport = l->transport;
442 connect_to_remote(s, l->connect_to);
443 return;
444 }
445
446 adb_close(fd);
447 }
448}
449
450static void free_listener(alistener* l)
451{
452 if (l->next) {
453 l->next->prev = l->prev;
454 l->prev->next = l->next;
455 l->next = l->prev = l;
456 }
457
458 // closes the corresponding fd
459 fdevent_remove(&l->fde);
460
461 if (l->local_name)
462 free((char*)l->local_name);
463
464 if (l->connect_to)
465 free((char*)l->connect_to);
466
467 if (l->transport) {
468 remove_transport_disconnect(l->transport, &l->disconnect);
469 }
470 free(l);
471}
472
473static void listener_disconnect(void* _l, atransport* t)
474{
475 alistener* l = _l;
476
477 free_listener(l);
478}
479
480int local_name_to_fd(const char *name)
481{
482 int port;
483
484 if(!strncmp("tcp:", name, 4)){
485 int ret;
486 port = atoi(name + 4);
487 ret = socket_loopback_server(port, SOCK_STREAM);
488 return ret;
489 }
490#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
491 // It's non-sensical to support the "reserved" space on the adb host side
492 if(!strncmp(name, "local:", 6)) {
493 return socket_local_server(name + 6,
494 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
495 } else if(!strncmp(name, "localabstract:", 14)) {
496 return socket_local_server(name + 14,
497 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
498 } else if(!strncmp(name, "localfilesystem:", 16)) {
499 return socket_local_server(name + 16,
500 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
501 }
502
503#endif
504 printf("unknown local portname '%s'\n", name);
505 return -1;
506}
507
508static int remove_listener(const char *local_name, const char *connect_to, atransport* transport)
509{
510 alistener *l;
511
512 for (l = listener_list.next; l != &listener_list; l = l->next) {
513 if (!strcmp(local_name, l->local_name) &&
514 !strcmp(connect_to, l->connect_to) &&
515 l->transport && l->transport == transport) {
516
517 listener_disconnect(l, transport);
518 return 0;
519 }
520 }
521
522 return -1;
523}
524
525static int install_listener(const char *local_name, const char *connect_to, atransport* transport)
526{
527 alistener *l;
528
529 //printf("install_listener('%s','%s')\n", local_name, connect_to);
530
531 for(l = listener_list.next; l != &listener_list; l = l->next){
532 if(strcmp(local_name, l->local_name) == 0) {
533 char *cto;
534
535 /* can't repurpose a smartsocket */
536 if(l->connect_to[0] == '*') {
537 return -1;
538 }
539
540 cto = strdup(connect_to);
541 if(cto == 0) {
542 return -1;
543 }
544
545 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
546 free((void*) l->connect_to);
547 l->connect_to = cto;
548 if (l->transport != transport) {
549 remove_transport_disconnect(l->transport, &l->disconnect);
550 l->transport = transport;
551 add_transport_disconnect(l->transport, &l->disconnect);
552 }
553 return 0;
554 }
555 }
556
557 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
558 if((l->local_name = strdup(local_name)) == 0) goto nomem;
559 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
560
561
562 l->fd = local_name_to_fd(local_name);
563 if(l->fd < 0) {
564 free((void*) l->local_name);
565 free((void*) l->connect_to);
566 free(l);
567 printf("cannot bind '%s'\n", local_name);
568 return -2;
569 }
570
571 close_on_exec(l->fd);
572 if(!strcmp(l->connect_to, "*smartsocket*")) {
573 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
574 } else {
575 fdevent_install(&l->fde, l->fd, listener_event_func, l);
576 }
577 fdevent_set(&l->fde, FDE_READ);
578
579 l->next = &listener_list;
580 l->prev = listener_list.prev;
581 l->next->prev = l;
582 l->prev->next = l;
583 l->transport = transport;
584
585 if (transport) {
586 l->disconnect.opaque = l;
587 l->disconnect.func = listener_disconnect;
588 add_transport_disconnect(transport, &l->disconnect);
589 }
590 return 0;
591
592nomem:
593 fatal("cannot allocate listener");
594 return 0;
595}
596
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597#ifdef HAVE_WIN32_PROC
598static BOOL WINAPI ctrlc_handler(DWORD type)
599{
600 exit(STATUS_CONTROL_C_EXIT);
601 return TRUE;
602}
603#endif
604
605static void adb_cleanup(void)
606{
607 usb_cleanup();
608}
609
610void start_logging(void)
611{
612#ifdef HAVE_WIN32_PROC
613 char temp[ MAX_PATH ];
614 FILE* fnul;
615 FILE* flog;
616
617 GetTempPath( sizeof(temp) - 8, temp );
618 strcat( temp, "adb.log" );
619
620 /* Win32 specific redirections */
621 fnul = fopen( "NUL", "rt" );
622 if (fnul != NULL)
623 stdin[0] = fnul[0];
624
625 flog = fopen( temp, "at" );
626 if (flog == NULL)
627 flog = fnul;
628
629 setvbuf( flog, NULL, _IONBF, 0 );
630
631 stdout[0] = flog[0];
632 stderr[0] = flog[0];
633 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
634#else
635 int fd;
636
637 fd = unix_open("/dev/null", O_RDONLY);
638 dup2(fd, 0);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800639 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640
641 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
642 if(fd < 0) {
643 fd = unix_open("/dev/null", O_WRONLY);
644 }
645 dup2(fd, 1);
646 dup2(fd, 2);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800647 adb_close(fd);
648
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
650#endif
651}
652
653#if !ADB_HOST
654void start_device_log(void)
655{
656 int fd;
Mike Lockwood1f546e62009-05-25 18:17:55 -0400657 char path[PATH_MAX];
658 struct tm now;
659 time_t t;
660 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661
Mike Lockwood1f546e62009-05-25 18:17:55 -0400662 // read the trace mask from persistent property persist.adb.trace_mask
663 // give up if the property is not set or cannot be parsed
664 property_get("persist.adb.trace_mask", value, "");
665 if (sscanf(value, "%x", &adb_trace_mask) != 1)
666 return;
667
668 adb_mkdir("/data/adb", 0775);
669 tzset();
670 time(&t);
671 localtime_r(&t, &now);
672 strftime(path, sizeof(path),
673 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
674 &now);
675 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 if (fd < 0)
677 return;
678
679 // redirect stdout and stderr to the log file
680 dup2(fd, 1);
681 dup2(fd, 2);
682 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
Benoit Goby95ef8282011-02-01 18:57:41 -0800683 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684
685 fd = unix_open("/dev/null", O_RDONLY);
686 dup2(fd, 0);
Benoit Goby95ef8282011-02-01 18:57:41 -0800687 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688}
689#endif
690
691#if ADB_HOST
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100692int launch_server(int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693{
694#ifdef HAVE_WIN32_PROC
695 /* we need to start the server in the background */
696 /* we create a PIPE that will be used to wait for the server's "OK" */
697 /* message since the pipe handles must be inheritable, we use a */
698 /* security attribute */
699 HANDLE pipe_read, pipe_write;
700 SECURITY_ATTRIBUTES sa;
701 STARTUPINFO startup;
702 PROCESS_INFORMATION pinfo;
703 char program_path[ MAX_PATH ];
704 int ret;
705
706 sa.nLength = sizeof(sa);
707 sa.lpSecurityDescriptor = NULL;
708 sa.bInheritHandle = TRUE;
709
710 /* create pipe, and ensure its read handle isn't inheritable */
711 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
712 if (!ret) {
713 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
714 return -1;
715 }
716
717 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
718
719 ZeroMemory( &startup, sizeof(startup) );
720 startup.cb = sizeof(startup);
721 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
722 startup.hStdOutput = pipe_write;
723 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
724 startup.dwFlags = STARTF_USESTDHANDLES;
725
726 ZeroMemory( &pinfo, sizeof(pinfo) );
727
728 /* get path of current program */
729 GetModuleFileName( NULL, program_path, sizeof(program_path) );
730
731 ret = CreateProcess(
732 program_path, /* program path */
733 "adb fork-server server",
734 /* the fork-server argument will set the
735 debug = 2 in the child */
736 NULL, /* process handle is not inheritable */
737 NULL, /* thread handle is not inheritable */
738 TRUE, /* yes, inherit some handles */
739 DETACHED_PROCESS, /* the new process doesn't have a console */
740 NULL, /* use parent's environment block */
741 NULL, /* use parent's starting directory */
742 &startup, /* startup info, i.e. std handles */
743 &pinfo );
744
745 CloseHandle( pipe_write );
746
747 if (!ret) {
748 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
749 CloseHandle( pipe_read );
750 return -1;
751 }
752
753 CloseHandle( pinfo.hProcess );
754 CloseHandle( pinfo.hThread );
755
756 /* wait for the "OK\n" message */
757 {
758 char temp[3];
759 DWORD count;
760
761 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
762 CloseHandle( pipe_read );
763 if ( !ret ) {
764 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
765 return -1;
766 }
767 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
768 fprintf(stderr, "ADB server didn't ACK\n" );
769 return -1;
770 }
771 }
772#elif defined(HAVE_FORKEXEC)
773 char path[PATH_MAX];
774 int fd[2];
775
776 // set up a pipe so the child can tell us when it is ready.
777 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
778 if (pipe(fd)) {
779 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
780 return -1;
781 }
Alexey Tarasov31664102009-10-22 02:55:00 +1100782 get_my_path(path, PATH_MAX);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 pid_t pid = fork();
784 if(pid < 0) return -1;
785
786 if (pid == 0) {
787 // child side of the fork
788
789 // redirect stderr to the pipe
790 // we use stderr instead of stdout due to stdout's buffering behavior.
791 adb_close(fd[0]);
792 dup2(fd[1], STDERR_FILENO);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800793 dup2(fd[1], STDOUT_FILENO);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800794 adb_close(fd[1]);
795
796 // child process
797 int result = execl(path, "adb", "fork-server", "server", NULL);
798 // this should not return
799 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
800 } else {
801 // parent side of the fork
802
803 char temp[3];
804
805 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
806 // wait for the "OK\n" message
807 adb_close(fd[1]);
808 int ret = adb_read(fd[0], temp, 3);
809 adb_close(fd[0]);
810 if (ret < 0) {
811 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", errno);
812 return -1;
813 }
814 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
815 fprintf(stderr, "ADB server didn't ACK\n" );
816 return -1;
817 }
818
819 setsid();
820 }
821#else
822#error "cannot implement background server start on this platform"
823#endif
824 return 0;
825}
826#endif
827
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100828/* Constructs a local name of form tcp:port.
829 * target_str points to the target string, it's content will be overwritten.
830 * target_size is the capacity of the target string.
831 * server_port is the port number to use for the local name.
832 */
833void build_local_name(char* target_str, size_t target_size, int server_port)
834{
835 snprintf(target_str, target_size, "tcp:%d", server_port);
836}
837
838int adb_main(int is_daemon, int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839{
840#if !ADB_HOST
841 int secure = 0;
Mike Lockwood2f38b692009-08-24 15:58:40 -0700842 int port;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800843 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844#endif
845
846 atexit(adb_cleanup);
847#ifdef HAVE_WIN32_PROC
848 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
849#elif defined(HAVE_FORKEXEC)
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800850 // Let the service subproc creator handle its children.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800851 signal(SIGPIPE, SIG_IGN);
852#endif
853
854 init_transport_registration();
855
856
857#if ADB_HOST
858 HOST = 1;
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700859 usb_vendors_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800860 usb_init();
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100861 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800862
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100863 char local_name[30];
864 build_local_name(local_name, sizeof(local_name), server_port);
865 if(install_listener(local_name, "*smartsocket*", NULL)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800866 exit(1);
867 }
868#else
869 /* run adbd in secure mode if ro.secure is set and
870 ** we are not in the emulator
871 */
872 property_get("ro.kernel.qemu", value, "");
873 if (strcmp(value, "1") != 0) {
874 property_get("ro.secure", value, "");
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700875 if (strcmp(value, "1") == 0) {
876 // don't run as root if ro.secure is set...
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800877 secure = 1;
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700878
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100879 // ... except we allow running as root in userdebug builds if the
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700880 // service.adb.root property has been set by the "adb root" command
881 property_get("ro.debuggable", value, "");
882 if (strcmp(value, "1") == 0) {
883 property_get("service.adb.root", value, "");
884 if (strcmp(value, "1") == 0) {
885 secure = 0;
886 }
887 }
888 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800889 }
890
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100891 /* don't listen on a port (default 5037) if running in secure mode */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800892 /* don't run as root if we are running in secure mode */
893 if (secure) {
Mike Lockwood5f4b0512009-08-04 20:37:51 -0400894 struct __user_cap_header_struct header;
895 struct __user_cap_data_struct cap;
896
Nick Kralevich44db9902010-08-27 14:35:07 -0700897 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
898 exit(1);
899 }
Mike Lockwood5f4b0512009-08-04 20:37:51 -0400900
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901 /* add extra groups:
902 ** AID_ADB to access the USB driver
903 ** AID_LOG to read system logs (adb logcat)
904 ** AID_INPUT to diagnose input issues (getevent)
905 ** AID_INET to diagnose network issues (netcfg, ping)
906 ** AID_GRAPHICS to access the frame buffer
The Android Open Source Project20155492009-03-11 12:12:01 -0700907 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
Mike Lockwood6a3075c2009-05-25 13:52:00 -0400908 ** AID_SDCARD_RW to allow writing to the SD card
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500909 ** AID_MOUNT to allow unmounting the SD card before rebooting
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910 */
The Android Open Source Project20155492009-03-11 12:12:01 -0700911 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500912 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_RW, AID_MOUNT };
Nick Kralevich44db9902010-08-27 14:35:07 -0700913 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
914 exit(1);
915 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800916
917 /* then switch user and group to "shell" */
Nick Kralevich44db9902010-08-27 14:35:07 -0700918 if (setgid(AID_SHELL) != 0) {
919 exit(1);
920 }
921 if (setuid(AID_SHELL) != 0) {
922 exit(1);
923 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800924
Mike Lockwood5f4b0512009-08-04 20:37:51 -0400925 /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */
926 header.version = _LINUX_CAPABILITY_VERSION;
927 header.pid = 0;
928 cap.effective = cap.permitted = (1 << CAP_SYS_BOOT);
929 cap.inheritable = 0;
930 capset(&header, &cap);
931
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100932 D("Local port disabled\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800933 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100934 char local_name[30];
935 build_local_name(local_name, sizeof(local_name), server_port);
936 if(install_listener(local_name, "*smartsocket*", NULL)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800937 exit(1);
938 }
939 }
940
941 /* for the device, start the usb transport if the
Mike Lockwood8e2ceae2010-04-20 14:06:40 -0400942 ** android usb device exists and the "service.adb.tcp.port" and
943 ** "persist.adb.tcp.port" properties are not set.
944 ** Otherwise start the network transport.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945 */
Mike Lockwood8e2ceae2010-04-20 14:06:40 -0400946 property_get("service.adb.tcp.port", value, "");
947 if (!value[0])
948 property_get("persist.adb.tcp.port", value, "");
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700949 if (sscanf(value, "%d", &port) == 1 && port > 0) {
950 // listen on TCP port specified by service.adb.tcp.port property
951 local_init(port);
952 } else if (access("/dev/android_adb", F_OK) == 0) {
953 // listen on USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800954 usb_init();
955 } else {
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700956 // listen on default port
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100957 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958 }
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800959 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960 init_jdwp();
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800961 D("adb_main(): post init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962#endif
963
964 if (is_daemon)
965 {
966 // inform our parent that we are up and running.
967#ifdef HAVE_WIN32_PROC
968 DWORD count;
969 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
970#elif defined(HAVE_FORKEXEC)
971 fprintf(stderr, "OK\n");
972#endif
973 start_logging();
974 }
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800975 D("Event loop starting\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800976
977 fdevent_loop();
978
979 usb_cleanup();
980
981 return 0;
982}
983
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100984#if ADB_HOST
985void connect_device(char* host, char* buffer, int buffer_size)
986{
987 int port, fd;
988 char* portstr = strchr(host, ':');
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400989 char hostbuf[100];
990 char serial[100];
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100991
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400992 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
993 if (portstr) {
994 if (portstr - host >= sizeof(hostbuf)) {
995 snprintf(buffer, buffer_size, "bad host name %s", host);
996 return;
997 }
998 // zero terminate the host at the point we found the colon
999 hostbuf[portstr - host] = 0;
1000 if (sscanf(portstr + 1, "%d", &port) == 0) {
1001 snprintf(buffer, buffer_size, "bad port number %s", portstr);
1002 return;
1003 }
1004 } else {
1005 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001006 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001007
1008 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
1009 if (find_transport(serial)) {
1010 snprintf(buffer, buffer_size, "already connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001011 return;
1012 }
1013
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001014 fd = socket_network_client(hostbuf, port, SOCK_STREAM);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001015 if (fd < 0) {
1016 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
1017 return;
1018 }
1019
1020 D("client: connected on remote on fd %d\n", fd);
1021 close_on_exec(fd);
1022 disable_tcp_nagle(fd);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001023 register_socket_transport(fd, serial, port, 0);
1024 snprintf(buffer, buffer_size, "connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001025}
1026
1027void connect_emulator(char* port_spec, char* buffer, int buffer_size)
1028{
1029 char* port_separator = strchr(port_spec, ',');
1030 if (!port_separator) {
1031 snprintf(buffer, buffer_size,
1032 "unable to parse '%s' as <console port>,<adb port>",
1033 port_spec);
1034 return;
1035 }
1036
1037 // Zero-terminate console port and make port_separator point to 2nd port.
1038 *port_separator++ = 0;
1039 int console_port = strtol(port_spec, NULL, 0);
1040 int adb_port = strtol(port_separator, NULL, 0);
1041 if (!(console_port > 0 && adb_port > 0)) {
1042 *(port_separator - 1) = ',';
1043 snprintf(buffer, buffer_size,
1044 "Invalid port numbers: Expected positive numbers, got '%s'",
1045 port_spec);
1046 return;
1047 }
1048
1049 /* Check if the emulator is already known.
1050 * Note: There's a small but harmless race condition here: An emulator not
1051 * present just yet could be registered by another invocation right
1052 * after doing this check here. However, local_connect protects
1053 * against double-registration too. From here, a better error message
1054 * can be produced. In the case of the race condition, the very specific
1055 * error message won't be shown, but the data doesn't get corrupted. */
1056 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
1057 if (known_emulator != NULL) {
1058 snprintf(buffer, buffer_size,
1059 "Emulator on port %d already registered.", adb_port);
1060 return;
1061 }
1062
1063 /* Check if more emulators can be registered. Similar unproblematic
1064 * race condition as above. */
1065 int candidate_slot = get_available_local_transport_index();
1066 if (candidate_slot < 0) {
1067 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
1068 return;
1069 }
1070
1071 /* Preconditions met, try to connect to the emulator. */
1072 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
1073 snprintf(buffer, buffer_size,
1074 "Connected to emulator on ports %d,%d", console_port, adb_port);
1075 } else {
1076 snprintf(buffer, buffer_size,
1077 "Could not connect to emulator on ports %d,%d",
1078 console_port, adb_port);
1079 }
1080}
1081#endif
1082
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1084{
1085 atransport *transport = NULL;
1086 char buf[4096];
1087
1088 if(!strcmp(service, "kill")) {
1089 fprintf(stderr,"adb server killed by remote request\n");
1090 fflush(stdout);
1091 adb_write(reply_fd, "OKAY", 4);
1092 usb_cleanup();
1093 exit(0);
1094 }
1095
1096#if ADB_HOST
1097 // "transport:" is used for switching transport with a specified serial number
1098 // "transport-usb:" is used for switching transport to the only USB transport
1099 // "transport-local:" is used for switching transport to the only local transport
1100 // "transport-any:" is used for switching transport to the only transport
1101 if (!strncmp(service, "transport", strlen("transport"))) {
1102 char* error_string = "unknown failure";
1103 transport_type type = kTransportAny;
1104
1105 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1106 type = kTransportUsb;
1107 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1108 type = kTransportLocal;
1109 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1110 type = kTransportAny;
1111 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1112 service += strlen("transport:");
1113 serial = strdup(service);
1114 }
1115
1116 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1117
1118 if (transport) {
1119 s->transport = transport;
1120 adb_write(reply_fd, "OKAY", 4);
1121 } else {
1122 sendfailmsg(reply_fd, error_string);
1123 }
1124 return 1;
1125 }
1126
1127 // return a list of all connected devices
1128 if (!strcmp(service, "devices")) {
1129 char buffer[4096];
1130 memset(buf, 0, sizeof(buf));
1131 memset(buffer, 0, sizeof(buffer));
1132 D("Getting device list \n");
1133 list_transports(buffer, sizeof(buffer));
1134 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer);
1135 D("Wrote device list \n");
1136 writex(reply_fd, buf, strlen(buf));
1137 return 0;
1138 }
1139
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001140 // add a new TCP transport, device or emulator
Mike Lockwood2f38b692009-08-24 15:58:40 -07001141 if (!strncmp(service, "connect:", 8)) {
1142 char buffer[4096];
Mike Lockwood2f38b692009-08-24 15:58:40 -07001143 char* host = service + 8;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001144 if (!strncmp(host, "emu:", 4)) {
1145 connect_emulator(host + 4, buffer, sizeof(buffer));
1146 } else {
1147 connect_device(host, buffer, sizeof(buffer));
Mike Lockwood2f38b692009-08-24 15:58:40 -07001148 }
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001149 // Send response for emulator and device
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001150 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
1151 writex(reply_fd, buf, strlen(buf));
1152 return 0;
1153 }
1154
1155 // remove TCP transport
1156 if (!strncmp(service, "disconnect:", 11)) {
1157 char buffer[4096];
1158 memset(buffer, 0, sizeof(buffer));
1159 char* serial = service + 11;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001160 if (serial[0] == 0) {
1161 // disconnect from all TCP devices
1162 unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001163 } else {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001164 char hostbuf[100];
1165 // assume port 5555 if no port is specified
1166 if (!strchr(serial, ':')) {
1167 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1168 serial = hostbuf;
1169 }
1170 atransport *t = find_transport(serial);
1171
1172 if (t) {
1173 unregister_transport(t);
1174 } else {
1175 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1176 }
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001177 }
1178
1179 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
Mike Lockwood2f38b692009-08-24 15:58:40 -07001180 writex(reply_fd, buf, strlen(buf));
1181 return 0;
1182 }
1183
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 // returns our value for ADB_SERVER_VERSION
1185 if (!strcmp(service, "version")) {
1186 char version[12];
1187 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
1188 snprintf(buf, sizeof buf, "OKAY%04x%s", (unsigned)strlen(version), version);
1189 writex(reply_fd, buf, strlen(buf));
1190 return 0;
1191 }
1192
1193 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1194 char *out = "unknown";
1195 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1196 if (transport && transport->serial) {
1197 out = transport->serial;
1198 }
1199 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out);
1200 writex(reply_fd, buf, strlen(buf));
1201 return 0;
1202 }
1203 // indicates a new emulator instance has started
1204 if (!strncmp(service,"emulator:",9)) {
1205 int port = atoi(service+9);
1206 local_connect(port);
1207 /* we don't even need to send a reply */
1208 return 0;
1209 }
1210#endif // ADB_HOST
1211
1212 if(!strncmp(service,"forward:",8) || !strncmp(service,"killforward:",12)) {
1213 char *local, *remote, *err;
1214 int r;
1215 atransport *transport;
1216
1217 int createForward = strncmp(service,"kill",4);
1218
1219 local = service + (createForward ? 8 : 12);
1220 remote = strchr(local,';');
1221 if(remote == 0) {
1222 sendfailmsg(reply_fd, "malformed forward spec");
1223 return 0;
1224 }
1225
1226 *remote++ = 0;
1227 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')){
1228 sendfailmsg(reply_fd, "malformed forward spec");
1229 return 0;
1230 }
1231
1232 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1233 if (!transport) {
1234 sendfailmsg(reply_fd, err);
1235 return 0;
1236 }
1237
1238 if (createForward) {
1239 r = install_listener(local, remote, transport);
1240 } else {
1241 r = remove_listener(local, remote, transport);
1242 }
1243 if(r == 0) {
1244 /* 1st OKAY is connect, 2nd OKAY is status */
1245 writex(reply_fd, "OKAYOKAY", 8);
1246 return 0;
1247 }
1248
1249 if (createForward) {
1250 sendfailmsg(reply_fd, (r == -1) ? "cannot rebind smartsocket" : "cannot bind socket");
1251 } else {
1252 sendfailmsg(reply_fd, "cannot remove listener");
1253 }
1254 return 0;
1255 }
1256
1257 if(!strncmp(service,"get-state",strlen("get-state"))) {
1258 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1259 char *state = connection_state_name(transport);
1260 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(state),state);
1261 writex(reply_fd, buf, strlen(buf));
1262 return 0;
1263 }
1264 return -1;
1265}
1266
1267#if !ADB_HOST
1268int recovery_mode = 0;
1269#endif
1270
1271int main(int argc, char **argv)
1272{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273#if ADB_HOST
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +01001274 adb_trace_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001275 adb_sysdeps_init();
JP Abgrall69c5c4c2011-02-18 14:16:59 -08001276 D("Handling commandline()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001277 return adb_commandline(argc - 1, argv + 1);
1278#else
1279 if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
1280 adb_device_banner = "recovery";
1281 recovery_mode = 1;
1282 }
Mike Lockwood1f546e62009-05-25 18:17:55 -04001283
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284 start_device_log();
JP Abgrall69c5c4c2011-02-18 14:16:59 -08001285 D("Handling main()\n");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001286 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001287#endif
1288}