blob: e7a28d2e54b58292ec4769745e110f34460cc1ba [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 Abgrall408fa572011-03-16 15:57:42 -070039#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 },
JP Abgrall408fa572011-03-16 15:57:42 -070096 { "services", TRACE_SERVICES },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097 { NULL, 0 }
98 };
99
100 if (p == NULL)
101 return;
102
103 /* use a comma/column/semi-colum/space separated list */
104 while (*p) {
105 int len, tagn;
106
107 q = strpbrk(p, " ,:;");
108 if (q == NULL) {
109 q = p + strlen(p);
110 }
111 len = q - p;
112
113 for (tagn = 0; tags[tagn].tag != NULL; tagn++)
114 {
115 int taglen = strlen(tags[tagn].tag);
116
117 if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
118 {
119 int flag = tags[tagn].flag;
120 if (flag == 0) {
121 adb_trace_mask = ~0;
122 return;
123 }
124 adb_trace_mask |= (1 << flag);
125 break;
126 }
127 }
128 p = q;
129 if (*p)
130 p++;
131 }
132}
133
134
135apacket *get_apacket(void)
136{
137 apacket *p = malloc(sizeof(apacket));
138 if(p == 0) fatal("failed to allocate an apacket");
139 memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
140 return p;
141}
142
143void put_apacket(apacket *p)
144{
145 free(p);
146}
147
148void handle_online(void)
149{
150 D("adb: online\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151}
152
153void handle_offline(atransport *t)
154{
155 D("adb: offline\n");
156 //Close the associated usb
157 run_transport_disconnects(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158}
159
160#if TRACE_PACKETS
161#define DUMPMAX 32
162void print_packet(const char *label, apacket *p)
163{
164 char *tag;
165 char *x;
166 unsigned count;
167
168 switch(p->msg.command){
169 case A_SYNC: tag = "SYNC"; break;
170 case A_CNXN: tag = "CNXN" ; break;
171 case A_OPEN: tag = "OPEN"; break;
172 case A_OKAY: tag = "OKAY"; break;
173 case A_CLSE: tag = "CLSE"; break;
174 case A_WRTE: tag = "WRTE"; break;
175 default: tag = "????"; break;
176 }
177
178 fprintf(stderr, "%s: %s %08x %08x %04x \"",
179 label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
180 count = p->msg.data_length;
181 x = (char*) p->data;
182 if(count > DUMPMAX) {
183 count = DUMPMAX;
184 tag = "\n";
185 } else {
186 tag = "\"\n";
187 }
188 while(count-- > 0){
189 if((*x >= ' ') && (*x < 127)) {
190 fputc(*x, stderr);
191 } else {
192 fputc('.', stderr);
193 }
194 x++;
195 }
196 fprintf(stderr, tag);
197}
198#endif
199
200static void send_ready(unsigned local, unsigned remote, atransport *t)
201{
202 D("Calling send_ready \n");
203 apacket *p = get_apacket();
204 p->msg.command = A_OKAY;
205 p->msg.arg0 = local;
206 p->msg.arg1 = remote;
207 send_packet(p, t);
208}
209
210static void send_close(unsigned local, unsigned remote, atransport *t)
211{
212 D("Calling send_close \n");
213 apacket *p = get_apacket();
214 p->msg.command = A_CLSE;
215 p->msg.arg0 = local;
216 p->msg.arg1 = remote;
217 send_packet(p, t);
218}
219
220static void send_connect(atransport *t)
221{
222 D("Calling send_connect \n");
223 apacket *cp = get_apacket();
224 cp->msg.command = A_CNXN;
225 cp->msg.arg0 = A_VERSION;
226 cp->msg.arg1 = MAX_PAYLOAD;
227 snprintf((char*) cp->data, sizeof cp->data, "%s::",
228 HOST ? "host" : adb_device_banner);
229 cp->msg.data_length = strlen((char*) cp->data) + 1;
230 send_packet(cp, t);
231#if ADB_HOST
232 /* XXX why sleep here? */
233 // allow the device some time to respond to the connect message
234 adb_sleep_ms(1000);
235#endif
236}
237
238static char *connection_state_name(atransport *t)
239{
240 if (t == NULL) {
241 return "unknown";
242 }
243
244 switch(t->connection_state) {
245 case CS_BOOTLOADER:
246 return "bootloader";
247 case CS_DEVICE:
248 return "device";
249 case CS_OFFLINE:
250 return "offline";
251 default:
252 return "unknown";
253 }
254}
255
256void parse_banner(char *banner, atransport *t)
257{
258 char *type, *product, *end;
259
260 D("parse_banner: %s\n", banner);
261 type = banner;
262 product = strchr(type, ':');
263 if(product) {
264 *product++ = 0;
265 } else {
266 product = "";
267 }
268
269 /* remove trailing ':' */
270 end = strchr(product, ':');
271 if(end) *end = 0;
272
273 /* save product name in device structure */
274 if (t->product == NULL) {
275 t->product = strdup(product);
276 } else if (strcmp(product, t->product) != 0) {
277 free(t->product);
278 t->product = strdup(product);
279 }
280
281 if(!strcmp(type, "bootloader")){
282 D("setting connection_state to CS_BOOTLOADER\n");
283 t->connection_state = CS_BOOTLOADER;
284 update_transports();
285 return;
286 }
287
288 if(!strcmp(type, "device")) {
289 D("setting connection_state to CS_DEVICE\n");
290 t->connection_state = CS_DEVICE;
291 update_transports();
292 return;
293 }
294
295 if(!strcmp(type, "recovery")) {
296 D("setting connection_state to CS_RECOVERY\n");
297 t->connection_state = CS_RECOVERY;
298 update_transports();
299 return;
300 }
301
302 t->connection_state = CS_HOST;
303}
304
305void handle_packet(apacket *p, atransport *t)
306{
307 asocket *s;
308
Viral Mehta899913f2010-06-16 18:41:28 +0530309 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
310 ((char*) (&(p->msg.command)))[1],
311 ((char*) (&(p->msg.command)))[2],
312 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 print_packet("recv", p);
314
315 switch(p->msg.command){
316 case A_SYNC:
317 if(p->msg.arg0){
318 send_packet(p, t);
319 if(HOST) send_connect(t);
320 } else {
321 t->connection_state = CS_OFFLINE;
322 handle_offline(t);
323 send_packet(p, t);
324 }
325 return;
326
327 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
328 /* XXX verify version, etc */
329 if(t->connection_state != CS_OFFLINE) {
330 t->connection_state = CS_OFFLINE;
331 handle_offline(t);
332 }
333 parse_banner((char*) p->data, t);
334 handle_online();
335 if(!HOST) send_connect(t);
336 break;
337
338 case A_OPEN: /* OPEN(local-id, 0, "destination") */
339 if(t->connection_state != CS_OFFLINE) {
340 char *name = (char*) p->data;
341 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
342 s = create_local_service_socket(name);
343 if(s == 0) {
344 send_close(0, p->msg.arg0, t);
345 } else {
346 s->peer = create_remote_socket(p->msg.arg0, t);
347 s->peer->peer = s;
348 send_ready(s->id, s->peer->id, t);
349 s->ready(s);
350 }
351 }
352 break;
353
354 case A_OKAY: /* READY(local-id, remote-id, "") */
355 if(t->connection_state != CS_OFFLINE) {
356 if((s = find_local_socket(p->msg.arg1))) {
357 if(s->peer == 0) {
358 s->peer = create_remote_socket(p->msg.arg0, t);
359 s->peer->peer = s;
360 }
361 s->ready(s);
362 }
363 }
364 break;
365
366 case A_CLSE: /* CLOSE(local-id, remote-id, "") */
367 if(t->connection_state != CS_OFFLINE) {
368 if((s = find_local_socket(p->msg.arg1))) {
369 s->close(s);
370 }
371 }
372 break;
373
374 case A_WRTE:
375 if(t->connection_state != CS_OFFLINE) {
376 if((s = find_local_socket(p->msg.arg1))) {
377 unsigned rid = p->msg.arg0;
378 p->len = p->msg.data_length;
379
380 if(s->enqueue(s, p) == 0) {
381 D("Enqueue the socket\n");
382 send_ready(s->id, rid, t);
383 }
384 return;
385 }
386 }
387 break;
388
389 default:
390 printf("handle_packet: what is %08x?!\n", p->msg.command);
391 }
392
393 put_apacket(p);
394}
395
396alistener listener_list = {
397 .next = &listener_list,
398 .prev = &listener_list,
399};
400
401static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
402{
403 asocket *s;
404
405 if(ev & FDE_READ) {
406 struct sockaddr addr;
407 socklen_t alen;
408 int fd;
409
410 alen = sizeof(addr);
411 fd = adb_socket_accept(_fd, &addr, &alen);
412 if(fd < 0) return;
413
414 adb_socket_setbufsize(fd, CHUNK_SIZE);
415
416 s = create_local_socket(fd);
417 if(s) {
418 connect_to_smartsocket(s);
419 return;
420 }
421
422 adb_close(fd);
423 }
424}
425
426static void listener_event_func(int _fd, unsigned ev, void *_l)
427{
428 alistener *l = _l;
429 asocket *s;
430
431 if(ev & FDE_READ) {
432 struct sockaddr addr;
433 socklen_t alen;
434 int fd;
435
436 alen = sizeof(addr);
437 fd = adb_socket_accept(_fd, &addr, &alen);
438 if(fd < 0) return;
439
440 s = create_local_socket(fd);
441 if(s) {
442 s->transport = l->transport;
443 connect_to_remote(s, l->connect_to);
444 return;
445 }
446
447 adb_close(fd);
448 }
449}
450
451static void free_listener(alistener* l)
452{
453 if (l->next) {
454 l->next->prev = l->prev;
455 l->prev->next = l->next;
456 l->next = l->prev = l;
457 }
458
459 // closes the corresponding fd
460 fdevent_remove(&l->fde);
461
462 if (l->local_name)
463 free((char*)l->local_name);
464
465 if (l->connect_to)
466 free((char*)l->connect_to);
467
468 if (l->transport) {
469 remove_transport_disconnect(l->transport, &l->disconnect);
470 }
471 free(l);
472}
473
474static void listener_disconnect(void* _l, atransport* t)
475{
476 alistener* l = _l;
477
478 free_listener(l);
479}
480
481int local_name_to_fd(const char *name)
482{
483 int port;
484
485 if(!strncmp("tcp:", name, 4)){
486 int ret;
487 port = atoi(name + 4);
488 ret = socket_loopback_server(port, SOCK_STREAM);
489 return ret;
490 }
491#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
492 // It's non-sensical to support the "reserved" space on the adb host side
493 if(!strncmp(name, "local:", 6)) {
494 return socket_local_server(name + 6,
495 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
496 } else if(!strncmp(name, "localabstract:", 14)) {
497 return socket_local_server(name + 14,
498 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
499 } else if(!strncmp(name, "localfilesystem:", 16)) {
500 return socket_local_server(name + 16,
501 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
502 }
503
504#endif
505 printf("unknown local portname '%s'\n", name);
506 return -1;
507}
508
509static int remove_listener(const char *local_name, const char *connect_to, atransport* transport)
510{
511 alistener *l;
512
513 for (l = listener_list.next; l != &listener_list; l = l->next) {
514 if (!strcmp(local_name, l->local_name) &&
515 !strcmp(connect_to, l->connect_to) &&
516 l->transport && l->transport == transport) {
517
518 listener_disconnect(l, transport);
519 return 0;
520 }
521 }
522
523 return -1;
524}
525
526static int install_listener(const char *local_name, const char *connect_to, atransport* transport)
527{
528 alistener *l;
529
530 //printf("install_listener('%s','%s')\n", local_name, connect_to);
531
532 for(l = listener_list.next; l != &listener_list; l = l->next){
533 if(strcmp(local_name, l->local_name) == 0) {
534 char *cto;
535
536 /* can't repurpose a smartsocket */
537 if(l->connect_to[0] == '*') {
538 return -1;
539 }
540
541 cto = strdup(connect_to);
542 if(cto == 0) {
543 return -1;
544 }
545
546 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
547 free((void*) l->connect_to);
548 l->connect_to = cto;
549 if (l->transport != transport) {
550 remove_transport_disconnect(l->transport, &l->disconnect);
551 l->transport = transport;
552 add_transport_disconnect(l->transport, &l->disconnect);
553 }
554 return 0;
555 }
556 }
557
558 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
559 if((l->local_name = strdup(local_name)) == 0) goto nomem;
560 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
561
562
563 l->fd = local_name_to_fd(local_name);
564 if(l->fd < 0) {
565 free((void*) l->local_name);
566 free((void*) l->connect_to);
567 free(l);
568 printf("cannot bind '%s'\n", local_name);
569 return -2;
570 }
571
572 close_on_exec(l->fd);
573 if(!strcmp(l->connect_to, "*smartsocket*")) {
574 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
575 } else {
576 fdevent_install(&l->fde, l->fd, listener_event_func, l);
577 }
578 fdevent_set(&l->fde, FDE_READ);
579
580 l->next = &listener_list;
581 l->prev = listener_list.prev;
582 l->next->prev = l;
583 l->prev->next = l;
584 l->transport = transport;
585
586 if (transport) {
587 l->disconnect.opaque = l;
588 l->disconnect.func = listener_disconnect;
589 add_transport_disconnect(transport, &l->disconnect);
590 }
591 return 0;
592
593nomem:
594 fatal("cannot allocate listener");
595 return 0;
596}
597
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598#ifdef HAVE_WIN32_PROC
599static BOOL WINAPI ctrlc_handler(DWORD type)
600{
601 exit(STATUS_CONTROL_C_EXIT);
602 return TRUE;
603}
604#endif
605
606static void adb_cleanup(void)
607{
608 usb_cleanup();
609}
610
611void start_logging(void)
612{
613#ifdef HAVE_WIN32_PROC
614 char temp[ MAX_PATH ];
615 FILE* fnul;
616 FILE* flog;
617
618 GetTempPath( sizeof(temp) - 8, temp );
619 strcat( temp, "adb.log" );
620
621 /* Win32 specific redirections */
622 fnul = fopen( "NUL", "rt" );
623 if (fnul != NULL)
624 stdin[0] = fnul[0];
625
626 flog = fopen( temp, "at" );
627 if (flog == NULL)
628 flog = fnul;
629
630 setvbuf( flog, NULL, _IONBF, 0 );
631
632 stdout[0] = flog[0];
633 stderr[0] = flog[0];
634 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
635#else
636 int fd;
637
638 fd = unix_open("/dev/null", O_RDONLY);
639 dup2(fd, 0);
JP Abgrall408fa572011-03-16 15:57:42 -0700640 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641
642 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
643 if(fd < 0) {
644 fd = unix_open("/dev/null", O_WRONLY);
645 }
646 dup2(fd, 1);
647 dup2(fd, 2);
JP Abgrall408fa572011-03-16 15:57:42 -0700648 adb_close(fd);
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);
793 adb_close(fd[1]);
794
795 // child process
796 int result = execl(path, "adb", "fork-server", "server", NULL);
797 // this should not return
798 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
799 } else {
800 // parent side of the fork
801
802 char temp[3];
803
804 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
805 // wait for the "OK\n" message
806 adb_close(fd[1]);
807 int ret = adb_read(fd[0], temp, 3);
JP Abgrall408fa572011-03-16 15:57:42 -0700808 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 adb_close(fd[0]);
810 if (ret < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -0700811 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800812 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 Abgrall408fa572011-03-16 15:57:42 -0700850 // No SIGCHLD. Let the service subproc 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) {
Kenny Rootff9d3482011-07-31 09:15:41 -0700874 property_get("ro.secure", value, "1");
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
JP Abgrall61b90bd2011-11-09 10:30:08 -0800910 ** AID_NET_BW_STATS to read out qtaguid statistics
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 */
The Android Open Source Project20155492009-03-11 12:12:01 -0700912 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
JP Abgrall61b90bd2011-11-09 10:30:08 -0800913 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_RW, AID_MOUNT,
914 AID_NET_BW_STATS };
Nick Kralevich44db9902010-08-27 14:35:07 -0700915 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
916 exit(1);
917 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800918
919 /* then switch user and group to "shell" */
Nick Kralevich44db9902010-08-27 14:35:07 -0700920 if (setgid(AID_SHELL) != 0) {
921 exit(1);
922 }
923 if (setuid(AID_SHELL) != 0) {
924 exit(1);
925 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800926
Mike Lockwood5f4b0512009-08-04 20:37:51 -0400927 /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */
928 header.version = _LINUX_CAPABILITY_VERSION;
929 header.pid = 0;
930 cap.effective = cap.permitted = (1 << CAP_SYS_BOOT);
931 cap.inheritable = 0;
932 capset(&header, &cap);
933
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100934 D("Local port disabled\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800935 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100936 char local_name[30];
937 build_local_name(local_name, sizeof(local_name), server_port);
938 if(install_listener(local_name, "*smartsocket*", NULL)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800939 exit(1);
940 }
941 }
942
Joe Onoratoae868a42011-12-07 17:50:28 -0800943 int usb = 0;
944 if (access("/dev/android_adb", F_OK) == 0) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700945 // listen on USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946 usb_init();
Joe Onoratoae868a42011-12-07 17:50:28 -0800947 usb = 1;
948 }
949
950 // If one of these properties is set, also listen on that port
951 // If one of the properties isn't set and we couldn't listen on usb,
952 // listen on the default port.
953 property_get("service.adb.tcp.port", value, "");
954 if (!value[0]) {
955 property_get("persist.adb.tcp.port", value, "");
956 }
957 if (sscanf(value, "%d", &port) == 1 && port > 0) {
958 printf("using port=%d\n", port);
959 // listen on TCP port specified by service.adb.tcp.port property
960 local_init(port);
961 } else if (!usb) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700962 // listen on default port
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100963 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800964 }
Joe Onoratoae868a42011-12-07 17:50:28 -0800965
JP Abgrall408fa572011-03-16 15:57:42 -0700966 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800967 init_jdwp();
JP Abgrall408fa572011-03-16 15:57:42 -0700968 D("adb_main(): post init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800969#endif
970
971 if (is_daemon)
972 {
973 // inform our parent that we are up and running.
974#ifdef HAVE_WIN32_PROC
975 DWORD count;
976 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
977#elif defined(HAVE_FORKEXEC)
978 fprintf(stderr, "OK\n");
979#endif
980 start_logging();
981 }
JP Abgrall408fa572011-03-16 15:57:42 -0700982 D("Event loop starting\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800983
984 fdevent_loop();
985
986 usb_cleanup();
987
988 return 0;
989}
990
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100991#if ADB_HOST
992void connect_device(char* host, char* buffer, int buffer_size)
993{
994 int port, fd;
995 char* portstr = strchr(host, ':');
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400996 char hostbuf[100];
997 char serial[100];
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100998
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400999 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
1000 if (portstr) {
1001 if (portstr - host >= sizeof(hostbuf)) {
1002 snprintf(buffer, buffer_size, "bad host name %s", host);
1003 return;
1004 }
1005 // zero terminate the host at the point we found the colon
1006 hostbuf[portstr - host] = 0;
1007 if (sscanf(portstr + 1, "%d", &port) == 0) {
1008 snprintf(buffer, buffer_size, "bad port number %s", portstr);
1009 return;
1010 }
1011 } else {
1012 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001013 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001014
1015 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
1016 if (find_transport(serial)) {
1017 snprintf(buffer, buffer_size, "already connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001018 return;
1019 }
1020
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001021 fd = socket_network_client(hostbuf, port, SOCK_STREAM);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001022 if (fd < 0) {
1023 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
1024 return;
1025 }
1026
1027 D("client: connected on remote on fd %d\n", fd);
1028 close_on_exec(fd);
1029 disable_tcp_nagle(fd);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001030 register_socket_transport(fd, serial, port, 0);
1031 snprintf(buffer, buffer_size, "connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001032}
1033
1034void connect_emulator(char* port_spec, char* buffer, int buffer_size)
1035{
1036 char* port_separator = strchr(port_spec, ',');
1037 if (!port_separator) {
1038 snprintf(buffer, buffer_size,
1039 "unable to parse '%s' as <console port>,<adb port>",
1040 port_spec);
1041 return;
1042 }
1043
1044 // Zero-terminate console port and make port_separator point to 2nd port.
1045 *port_separator++ = 0;
1046 int console_port = strtol(port_spec, NULL, 0);
1047 int adb_port = strtol(port_separator, NULL, 0);
1048 if (!(console_port > 0 && adb_port > 0)) {
1049 *(port_separator - 1) = ',';
1050 snprintf(buffer, buffer_size,
1051 "Invalid port numbers: Expected positive numbers, got '%s'",
1052 port_spec);
1053 return;
1054 }
1055
1056 /* Check if the emulator is already known.
1057 * Note: There's a small but harmless race condition here: An emulator not
1058 * present just yet could be registered by another invocation right
1059 * after doing this check here. However, local_connect protects
1060 * against double-registration too. From here, a better error message
1061 * can be produced. In the case of the race condition, the very specific
1062 * error message won't be shown, but the data doesn't get corrupted. */
1063 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
1064 if (known_emulator != NULL) {
1065 snprintf(buffer, buffer_size,
1066 "Emulator on port %d already registered.", adb_port);
1067 return;
1068 }
1069
1070 /* Check if more emulators can be registered. Similar unproblematic
1071 * race condition as above. */
1072 int candidate_slot = get_available_local_transport_index();
1073 if (candidate_slot < 0) {
1074 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
1075 return;
1076 }
1077
1078 /* Preconditions met, try to connect to the emulator. */
1079 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
1080 snprintf(buffer, buffer_size,
1081 "Connected to emulator on ports %d,%d", console_port, adb_port);
1082 } else {
1083 snprintf(buffer, buffer_size,
1084 "Could not connect to emulator on ports %d,%d",
1085 console_port, adb_port);
1086 }
1087}
1088#endif
1089
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001090int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1091{
1092 atransport *transport = NULL;
1093 char buf[4096];
1094
1095 if(!strcmp(service, "kill")) {
1096 fprintf(stderr,"adb server killed by remote request\n");
1097 fflush(stdout);
1098 adb_write(reply_fd, "OKAY", 4);
1099 usb_cleanup();
1100 exit(0);
1101 }
1102
1103#if ADB_HOST
1104 // "transport:" is used for switching transport with a specified serial number
1105 // "transport-usb:" is used for switching transport to the only USB transport
1106 // "transport-local:" is used for switching transport to the only local transport
1107 // "transport-any:" is used for switching transport to the only transport
1108 if (!strncmp(service, "transport", strlen("transport"))) {
1109 char* error_string = "unknown failure";
1110 transport_type type = kTransportAny;
1111
1112 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1113 type = kTransportUsb;
1114 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1115 type = kTransportLocal;
1116 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1117 type = kTransportAny;
1118 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1119 service += strlen("transport:");
Tom Marlin3175c8e2011-07-27 12:56:14 -05001120 serial = service;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001121 }
1122
1123 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1124
1125 if (transport) {
1126 s->transport = transport;
1127 adb_write(reply_fd, "OKAY", 4);
1128 } else {
1129 sendfailmsg(reply_fd, error_string);
1130 }
1131 return 1;
1132 }
1133
1134 // return a list of all connected devices
1135 if (!strcmp(service, "devices")) {
1136 char buffer[4096];
1137 memset(buf, 0, sizeof(buf));
1138 memset(buffer, 0, sizeof(buffer));
1139 D("Getting device list \n");
1140 list_transports(buffer, sizeof(buffer));
1141 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer);
1142 D("Wrote device list \n");
1143 writex(reply_fd, buf, strlen(buf));
1144 return 0;
1145 }
1146
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001147 // add a new TCP transport, device or emulator
Mike Lockwood2f38b692009-08-24 15:58:40 -07001148 if (!strncmp(service, "connect:", 8)) {
1149 char buffer[4096];
Mike Lockwood2f38b692009-08-24 15:58:40 -07001150 char* host = service + 8;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001151 if (!strncmp(host, "emu:", 4)) {
1152 connect_emulator(host + 4, buffer, sizeof(buffer));
1153 } else {
1154 connect_device(host, buffer, sizeof(buffer));
Mike Lockwood2f38b692009-08-24 15:58:40 -07001155 }
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001156 // Send response for emulator and device
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001157 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
1158 writex(reply_fd, buf, strlen(buf));
1159 return 0;
1160 }
1161
1162 // remove TCP transport
1163 if (!strncmp(service, "disconnect:", 11)) {
1164 char buffer[4096];
1165 memset(buffer, 0, sizeof(buffer));
1166 char* serial = service + 11;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001167 if (serial[0] == 0) {
1168 // disconnect from all TCP devices
1169 unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001170 } else {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001171 char hostbuf[100];
1172 // assume port 5555 if no port is specified
1173 if (!strchr(serial, ':')) {
1174 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1175 serial = hostbuf;
1176 }
1177 atransport *t = find_transport(serial);
1178
1179 if (t) {
1180 unregister_transport(t);
1181 } else {
1182 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1183 }
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001184 }
1185
1186 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
Mike Lockwood2f38b692009-08-24 15:58:40 -07001187 writex(reply_fd, buf, strlen(buf));
1188 return 0;
1189 }
1190
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191 // returns our value for ADB_SERVER_VERSION
1192 if (!strcmp(service, "version")) {
1193 char version[12];
1194 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
1195 snprintf(buf, sizeof buf, "OKAY%04x%s", (unsigned)strlen(version), version);
1196 writex(reply_fd, buf, strlen(buf));
1197 return 0;
1198 }
1199
1200 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1201 char *out = "unknown";
1202 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1203 if (transport && transport->serial) {
1204 out = transport->serial;
1205 }
1206 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out);
1207 writex(reply_fd, buf, strlen(buf));
1208 return 0;
1209 }
1210 // indicates a new emulator instance has started
1211 if (!strncmp(service,"emulator:",9)) {
1212 int port = atoi(service+9);
1213 local_connect(port);
1214 /* we don't even need to send a reply */
1215 return 0;
1216 }
1217#endif // ADB_HOST
1218
1219 if(!strncmp(service,"forward:",8) || !strncmp(service,"killforward:",12)) {
1220 char *local, *remote, *err;
1221 int r;
1222 atransport *transport;
1223
1224 int createForward = strncmp(service,"kill",4);
1225
1226 local = service + (createForward ? 8 : 12);
1227 remote = strchr(local,';');
1228 if(remote == 0) {
1229 sendfailmsg(reply_fd, "malformed forward spec");
1230 return 0;
1231 }
1232
1233 *remote++ = 0;
1234 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')){
1235 sendfailmsg(reply_fd, "malformed forward spec");
1236 return 0;
1237 }
1238
1239 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1240 if (!transport) {
1241 sendfailmsg(reply_fd, err);
1242 return 0;
1243 }
1244
1245 if (createForward) {
1246 r = install_listener(local, remote, transport);
1247 } else {
1248 r = remove_listener(local, remote, transport);
1249 }
1250 if(r == 0) {
1251 /* 1st OKAY is connect, 2nd OKAY is status */
1252 writex(reply_fd, "OKAYOKAY", 8);
1253 return 0;
1254 }
1255
1256 if (createForward) {
1257 sendfailmsg(reply_fd, (r == -1) ? "cannot rebind smartsocket" : "cannot bind socket");
1258 } else {
1259 sendfailmsg(reply_fd, "cannot remove listener");
1260 }
1261 return 0;
1262 }
1263
1264 if(!strncmp(service,"get-state",strlen("get-state"))) {
1265 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1266 char *state = connection_state_name(transport);
1267 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(state),state);
1268 writex(reply_fd, buf, strlen(buf));
1269 return 0;
1270 }
1271 return -1;
1272}
1273
1274#if !ADB_HOST
1275int recovery_mode = 0;
1276#endif
1277
1278int main(int argc, char **argv)
1279{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001280#if ADB_HOST
1281 adb_sysdeps_init();
JP Abgrall408fa572011-03-16 15:57:42 -07001282 adb_trace_init();
1283 D("Handling commandline()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284 return adb_commandline(argc - 1, argv + 1);
1285#else
1286 if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
1287 adb_device_banner = "recovery";
1288 recovery_mode = 1;
1289 }
Mike Lockwood1f546e62009-05-25 18:17:55 -04001290
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001291 start_device_log();
JP Abgrall408fa572011-03-16 15:57:42 -07001292 D("Handling main()\n");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001293 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001294#endif
1295}