blob: 44345243c28a03091c778bd7af1461e863e0dd61 [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
Doug Zongker447f0612012-01-09 14:54:53 -0800302 if(!strcmp(type, "sideload")) {
303 D("setting connection_state to CS_SIDELOAD\n");
304 t->connection_state = CS_SIDELOAD;
305 update_transports();
306 return;
307 }
308
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 t->connection_state = CS_HOST;
310}
311
312void handle_packet(apacket *p, atransport *t)
313{
314 asocket *s;
315
Viral Mehta899913f2010-06-16 18:41:28 +0530316 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
317 ((char*) (&(p->msg.command)))[1],
318 ((char*) (&(p->msg.command)))[2],
319 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 print_packet("recv", p);
321
322 switch(p->msg.command){
323 case A_SYNC:
324 if(p->msg.arg0){
325 send_packet(p, t);
326 if(HOST) send_connect(t);
327 } else {
328 t->connection_state = CS_OFFLINE;
329 handle_offline(t);
330 send_packet(p, t);
331 }
332 return;
333
334 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
335 /* XXX verify version, etc */
336 if(t->connection_state != CS_OFFLINE) {
337 t->connection_state = CS_OFFLINE;
338 handle_offline(t);
339 }
340 parse_banner((char*) p->data, t);
341 handle_online();
342 if(!HOST) send_connect(t);
343 break;
344
345 case A_OPEN: /* OPEN(local-id, 0, "destination") */
346 if(t->connection_state != CS_OFFLINE) {
347 char *name = (char*) p->data;
348 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
349 s = create_local_service_socket(name);
350 if(s == 0) {
351 send_close(0, p->msg.arg0, t);
352 } else {
353 s->peer = create_remote_socket(p->msg.arg0, t);
354 s->peer->peer = s;
355 send_ready(s->id, s->peer->id, t);
356 s->ready(s);
357 }
358 }
359 break;
360
361 case A_OKAY: /* READY(local-id, remote-id, "") */
362 if(t->connection_state != CS_OFFLINE) {
363 if((s = find_local_socket(p->msg.arg1))) {
364 if(s->peer == 0) {
365 s->peer = create_remote_socket(p->msg.arg0, t);
366 s->peer->peer = s;
367 }
368 s->ready(s);
369 }
370 }
371 break;
372
373 case A_CLSE: /* CLOSE(local-id, remote-id, "") */
374 if(t->connection_state != CS_OFFLINE) {
375 if((s = find_local_socket(p->msg.arg1))) {
376 s->close(s);
377 }
378 }
379 break;
380
381 case A_WRTE:
382 if(t->connection_state != CS_OFFLINE) {
383 if((s = find_local_socket(p->msg.arg1))) {
384 unsigned rid = p->msg.arg0;
385 p->len = p->msg.data_length;
386
387 if(s->enqueue(s, p) == 0) {
388 D("Enqueue the socket\n");
389 send_ready(s->id, rid, t);
390 }
391 return;
392 }
393 }
394 break;
395
396 default:
397 printf("handle_packet: what is %08x?!\n", p->msg.command);
398 }
399
400 put_apacket(p);
401}
402
403alistener listener_list = {
404 .next = &listener_list,
405 .prev = &listener_list,
406};
407
408static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
409{
410 asocket *s;
411
412 if(ev & FDE_READ) {
413 struct sockaddr addr;
414 socklen_t alen;
415 int fd;
416
417 alen = sizeof(addr);
418 fd = adb_socket_accept(_fd, &addr, &alen);
419 if(fd < 0) return;
420
421 adb_socket_setbufsize(fd, CHUNK_SIZE);
422
423 s = create_local_socket(fd);
424 if(s) {
425 connect_to_smartsocket(s);
426 return;
427 }
428
429 adb_close(fd);
430 }
431}
432
433static void listener_event_func(int _fd, unsigned ev, void *_l)
434{
435 alistener *l = _l;
436 asocket *s;
437
438 if(ev & FDE_READ) {
439 struct sockaddr addr;
440 socklen_t alen;
441 int fd;
442
443 alen = sizeof(addr);
444 fd = adb_socket_accept(_fd, &addr, &alen);
445 if(fd < 0) return;
446
447 s = create_local_socket(fd);
448 if(s) {
449 s->transport = l->transport;
450 connect_to_remote(s, l->connect_to);
451 return;
452 }
453
454 adb_close(fd);
455 }
456}
457
458static void free_listener(alistener* l)
459{
460 if (l->next) {
461 l->next->prev = l->prev;
462 l->prev->next = l->next;
463 l->next = l->prev = l;
464 }
465
466 // closes the corresponding fd
467 fdevent_remove(&l->fde);
468
469 if (l->local_name)
470 free((char*)l->local_name);
471
472 if (l->connect_to)
473 free((char*)l->connect_to);
474
475 if (l->transport) {
476 remove_transport_disconnect(l->transport, &l->disconnect);
477 }
478 free(l);
479}
480
481static void listener_disconnect(void* _l, atransport* t)
482{
483 alistener* l = _l;
484
485 free_listener(l);
486}
487
488int local_name_to_fd(const char *name)
489{
490 int port;
491
492 if(!strncmp("tcp:", name, 4)){
493 int ret;
494 port = atoi(name + 4);
495 ret = socket_loopback_server(port, SOCK_STREAM);
496 return ret;
497 }
498#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
499 // It's non-sensical to support the "reserved" space on the adb host side
500 if(!strncmp(name, "local:", 6)) {
501 return socket_local_server(name + 6,
502 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
503 } else if(!strncmp(name, "localabstract:", 14)) {
504 return socket_local_server(name + 14,
505 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
506 } else if(!strncmp(name, "localfilesystem:", 16)) {
507 return socket_local_server(name + 16,
508 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
509 }
510
511#endif
512 printf("unknown local portname '%s'\n", name);
513 return -1;
514}
515
516static int remove_listener(const char *local_name, const char *connect_to, atransport* transport)
517{
518 alistener *l;
519
520 for (l = listener_list.next; l != &listener_list; l = l->next) {
521 if (!strcmp(local_name, l->local_name) &&
522 !strcmp(connect_to, l->connect_to) &&
523 l->transport && l->transport == transport) {
524
525 listener_disconnect(l, transport);
526 return 0;
527 }
528 }
529
530 return -1;
531}
532
533static int install_listener(const char *local_name, const char *connect_to, atransport* transport)
534{
535 alistener *l;
536
537 //printf("install_listener('%s','%s')\n", local_name, connect_to);
538
539 for(l = listener_list.next; l != &listener_list; l = l->next){
540 if(strcmp(local_name, l->local_name) == 0) {
541 char *cto;
542
543 /* can't repurpose a smartsocket */
544 if(l->connect_to[0] == '*') {
545 return -1;
546 }
547
548 cto = strdup(connect_to);
549 if(cto == 0) {
550 return -1;
551 }
552
553 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
554 free((void*) l->connect_to);
555 l->connect_to = cto;
556 if (l->transport != transport) {
557 remove_transport_disconnect(l->transport, &l->disconnect);
558 l->transport = transport;
559 add_transport_disconnect(l->transport, &l->disconnect);
560 }
561 return 0;
562 }
563 }
564
565 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
566 if((l->local_name = strdup(local_name)) == 0) goto nomem;
567 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
568
569
570 l->fd = local_name_to_fd(local_name);
571 if(l->fd < 0) {
572 free((void*) l->local_name);
573 free((void*) l->connect_to);
574 free(l);
575 printf("cannot bind '%s'\n", local_name);
576 return -2;
577 }
578
579 close_on_exec(l->fd);
580 if(!strcmp(l->connect_to, "*smartsocket*")) {
581 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
582 } else {
583 fdevent_install(&l->fde, l->fd, listener_event_func, l);
584 }
585 fdevent_set(&l->fde, FDE_READ);
586
587 l->next = &listener_list;
588 l->prev = listener_list.prev;
589 l->next->prev = l;
590 l->prev->next = l;
591 l->transport = transport;
592
593 if (transport) {
594 l->disconnect.opaque = l;
595 l->disconnect.func = listener_disconnect;
596 add_transport_disconnect(transport, &l->disconnect);
597 }
598 return 0;
599
600nomem:
601 fatal("cannot allocate listener");
602 return 0;
603}
604
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605#ifdef HAVE_WIN32_PROC
606static BOOL WINAPI ctrlc_handler(DWORD type)
607{
608 exit(STATUS_CONTROL_C_EXIT);
609 return TRUE;
610}
611#endif
612
613static void adb_cleanup(void)
614{
615 usb_cleanup();
616}
617
618void start_logging(void)
619{
620#ifdef HAVE_WIN32_PROC
621 char temp[ MAX_PATH ];
622 FILE* fnul;
623 FILE* flog;
624
625 GetTempPath( sizeof(temp) - 8, temp );
626 strcat( temp, "adb.log" );
627
628 /* Win32 specific redirections */
629 fnul = fopen( "NUL", "rt" );
630 if (fnul != NULL)
631 stdin[0] = fnul[0];
632
633 flog = fopen( temp, "at" );
634 if (flog == NULL)
635 flog = fnul;
636
637 setvbuf( flog, NULL, _IONBF, 0 );
638
639 stdout[0] = flog[0];
640 stderr[0] = flog[0];
641 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
642#else
643 int fd;
644
645 fd = unix_open("/dev/null", O_RDONLY);
646 dup2(fd, 0);
JP Abgrall408fa572011-03-16 15:57:42 -0700647 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648
649 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
650 if(fd < 0) {
651 fd = unix_open("/dev/null", O_WRONLY);
652 }
653 dup2(fd, 1);
654 dup2(fd, 2);
JP Abgrall408fa572011-03-16 15:57:42 -0700655 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
657#endif
658}
659
660#if !ADB_HOST
661void start_device_log(void)
662{
663 int fd;
Mike Lockwood1f546e62009-05-25 18:17:55 -0400664 char path[PATH_MAX];
665 struct tm now;
666 time_t t;
667 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668
Mike Lockwood1f546e62009-05-25 18:17:55 -0400669 // read the trace mask from persistent property persist.adb.trace_mask
670 // give up if the property is not set or cannot be parsed
671 property_get("persist.adb.trace_mask", value, "");
672 if (sscanf(value, "%x", &adb_trace_mask) != 1)
673 return;
674
675 adb_mkdir("/data/adb", 0775);
676 tzset();
677 time(&t);
678 localtime_r(&t, &now);
679 strftime(path, sizeof(path),
680 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
681 &now);
682 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 if (fd < 0)
684 return;
685
686 // redirect stdout and stderr to the log file
687 dup2(fd, 1);
688 dup2(fd, 2);
689 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
Benoit Goby95ef8282011-02-01 18:57:41 -0800690 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691
692 fd = unix_open("/dev/null", O_RDONLY);
693 dup2(fd, 0);
Benoit Goby95ef8282011-02-01 18:57:41 -0800694 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695}
696#endif
697
698#if ADB_HOST
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100699int launch_server(int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700{
701#ifdef HAVE_WIN32_PROC
702 /* we need to start the server in the background */
703 /* we create a PIPE that will be used to wait for the server's "OK" */
704 /* message since the pipe handles must be inheritable, we use a */
705 /* security attribute */
706 HANDLE pipe_read, pipe_write;
707 SECURITY_ATTRIBUTES sa;
708 STARTUPINFO startup;
709 PROCESS_INFORMATION pinfo;
710 char program_path[ MAX_PATH ];
711 int ret;
712
713 sa.nLength = sizeof(sa);
714 sa.lpSecurityDescriptor = NULL;
715 sa.bInheritHandle = TRUE;
716
717 /* create pipe, and ensure its read handle isn't inheritable */
718 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
719 if (!ret) {
720 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
721 return -1;
722 }
723
724 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
725
726 ZeroMemory( &startup, sizeof(startup) );
727 startup.cb = sizeof(startup);
728 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
729 startup.hStdOutput = pipe_write;
730 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
731 startup.dwFlags = STARTF_USESTDHANDLES;
732
733 ZeroMemory( &pinfo, sizeof(pinfo) );
734
735 /* get path of current program */
736 GetModuleFileName( NULL, program_path, sizeof(program_path) );
737
738 ret = CreateProcess(
739 program_path, /* program path */
740 "adb fork-server server",
741 /* the fork-server argument will set the
742 debug = 2 in the child */
743 NULL, /* process handle is not inheritable */
744 NULL, /* thread handle is not inheritable */
745 TRUE, /* yes, inherit some handles */
746 DETACHED_PROCESS, /* the new process doesn't have a console */
747 NULL, /* use parent's environment block */
748 NULL, /* use parent's starting directory */
749 &startup, /* startup info, i.e. std handles */
750 &pinfo );
751
752 CloseHandle( pipe_write );
753
754 if (!ret) {
755 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
756 CloseHandle( pipe_read );
757 return -1;
758 }
759
760 CloseHandle( pinfo.hProcess );
761 CloseHandle( pinfo.hThread );
762
763 /* wait for the "OK\n" message */
764 {
765 char temp[3];
766 DWORD count;
767
768 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
769 CloseHandle( pipe_read );
770 if ( !ret ) {
771 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
772 return -1;
773 }
774 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
775 fprintf(stderr, "ADB server didn't ACK\n" );
776 return -1;
777 }
778 }
779#elif defined(HAVE_FORKEXEC)
780 char path[PATH_MAX];
781 int fd[2];
782
783 // set up a pipe so the child can tell us when it is ready.
784 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
785 if (pipe(fd)) {
786 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
787 return -1;
788 }
Alexey Tarasov31664102009-10-22 02:55:00 +1100789 get_my_path(path, PATH_MAX);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 pid_t pid = fork();
791 if(pid < 0) return -1;
792
793 if (pid == 0) {
794 // child side of the fork
795
796 // redirect stderr to the pipe
797 // we use stderr instead of stdout due to stdout's buffering behavior.
798 adb_close(fd[0]);
799 dup2(fd[1], STDERR_FILENO);
800 adb_close(fd[1]);
801
802 // child process
803 int result = execl(path, "adb", "fork-server", "server", NULL);
804 // this should not return
805 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
806 } else {
807 // parent side of the fork
808
809 char temp[3];
810
811 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
812 // wait for the "OK\n" message
813 adb_close(fd[1]);
814 int ret = adb_read(fd[0], temp, 3);
JP Abgrall408fa572011-03-16 15:57:42 -0700815 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816 adb_close(fd[0]);
817 if (ret < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -0700818 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819 return -1;
820 }
821 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
822 fprintf(stderr, "ADB server didn't ACK\n" );
823 return -1;
824 }
825
826 setsid();
827 }
828#else
829#error "cannot implement background server start on this platform"
830#endif
831 return 0;
832}
833#endif
834
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100835/* Constructs a local name of form tcp:port.
836 * target_str points to the target string, it's content will be overwritten.
837 * target_size is the capacity of the target string.
838 * server_port is the port number to use for the local name.
839 */
840void build_local_name(char* target_str, size_t target_size, int server_port)
841{
842 snprintf(target_str, target_size, "tcp:%d", server_port);
843}
844
Nick Kralevichbd9206b2012-01-19 10:18:59 -0800845#if !ADB_HOST
846static int should_drop_privileges() {
Nick Kralevich5890fe32012-01-19 13:11:35 -0800847#ifndef ALLOW_ADBD_ROOT
848 return 1;
849#else /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -0800850 int secure = 0;
851 char value[PROPERTY_VALUE_MAX];
852
853 /* run adbd in secure mode if ro.secure is set and
854 ** we are not in the emulator
855 */
856 property_get("ro.kernel.qemu", value, "");
857 if (strcmp(value, "1") != 0) {
858 property_get("ro.secure", value, "1");
859 if (strcmp(value, "1") == 0) {
860 // don't run as root if ro.secure is set...
861 secure = 1;
862
863 // ... except we allow running as root in userdebug builds if the
864 // service.adb.root property has been set by the "adb root" command
865 property_get("ro.debuggable", value, "");
866 if (strcmp(value, "1") == 0) {
867 property_get("service.adb.root", value, "");
868 if (strcmp(value, "1") == 0) {
869 secure = 0;
870 }
871 }
872 }
873 }
874 return secure;
Nick Kralevich5890fe32012-01-19 13:11:35 -0800875#endif /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -0800876}
877#endif /* !ADB_HOST */
878
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100879int adb_main(int is_daemon, int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800880{
881#if !ADB_HOST
Mike Lockwood2f38b692009-08-24 15:58:40 -0700882 int port;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800883 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800884#endif
885
886 atexit(adb_cleanup);
887#ifdef HAVE_WIN32_PROC
888 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
889#elif defined(HAVE_FORKEXEC)
JP Abgrall408fa572011-03-16 15:57:42 -0700890 // No SIGCHLD. Let the service subproc handle its children.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800891 signal(SIGPIPE, SIG_IGN);
892#endif
893
894 init_transport_registration();
895
896
897#if ADB_HOST
898 HOST = 1;
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700899 usb_vendors_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800900 usb_init();
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100901 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800902
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100903 char local_name[30];
904 build_local_name(local_name, sizeof(local_name), server_port);
905 if(install_listener(local_name, "*smartsocket*", NULL)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906 exit(1);
907 }
908#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100910 /* don't listen on a port (default 5037) if running in secure mode */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 /* don't run as root if we are running in secure mode */
Nick Kralevichbd9206b2012-01-19 10:18:59 -0800912 if (should_drop_privileges()) {
Mike Lockwood5f4b0512009-08-04 20:37:51 -0400913 struct __user_cap_header_struct header;
914 struct __user_cap_data_struct cap;
915
Nick Kralevich44db9902010-08-27 14:35:07 -0700916 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
917 exit(1);
918 }
Mike Lockwood5f4b0512009-08-04 20:37:51 -0400919
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800920 /* add extra groups:
921 ** AID_ADB to access the USB driver
922 ** AID_LOG to read system logs (adb logcat)
923 ** AID_INPUT to diagnose input issues (getevent)
924 ** AID_INET to diagnose network issues (netcfg, ping)
925 ** AID_GRAPHICS to access the frame buffer
The Android Open Source Project20155492009-03-11 12:12:01 -0700926 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
Mike Lockwood6a3075c2009-05-25 13:52:00 -0400927 ** AID_SDCARD_RW to allow writing to the SD card
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500928 ** AID_MOUNT to allow unmounting the SD card before rebooting
JP Abgrall61b90bd2011-11-09 10:30:08 -0800929 ** AID_NET_BW_STATS to read out qtaguid statistics
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930 */
The Android Open Source Project20155492009-03-11 12:12:01 -0700931 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
JP Abgrall61b90bd2011-11-09 10:30:08 -0800932 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_RW, AID_MOUNT,
933 AID_NET_BW_STATS };
Nick Kralevich44db9902010-08-27 14:35:07 -0700934 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
935 exit(1);
936 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800937
938 /* then switch user and group to "shell" */
Nick Kralevich44db9902010-08-27 14:35:07 -0700939 if (setgid(AID_SHELL) != 0) {
940 exit(1);
941 }
942 if (setuid(AID_SHELL) != 0) {
943 exit(1);
944 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945
Mike Lockwood5f4b0512009-08-04 20:37:51 -0400946 /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */
947 header.version = _LINUX_CAPABILITY_VERSION;
948 header.pid = 0;
949 cap.effective = cap.permitted = (1 << CAP_SYS_BOOT);
950 cap.inheritable = 0;
951 capset(&header, &cap);
952
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100953 D("Local port disabled\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800954 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100955 char local_name[30];
956 build_local_name(local_name, sizeof(local_name), server_port);
957 if(install_listener(local_name, "*smartsocket*", NULL)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958 exit(1);
959 }
960 }
961
962 /* for the device, start the usb transport if the
Mike Lockwood8e2ceae2010-04-20 14:06:40 -0400963 ** android usb device exists and the "service.adb.tcp.port" and
964 ** "persist.adb.tcp.port" properties are not set.
965 ** Otherwise start the network transport.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800966 */
Mike Lockwood8e2ceae2010-04-20 14:06:40 -0400967 property_get("service.adb.tcp.port", value, "");
968 if (!value[0])
969 property_get("persist.adb.tcp.port", value, "");
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700970 if (sscanf(value, "%d", &port) == 1 && port > 0) {
971 // listen on TCP port specified by service.adb.tcp.port property
972 local_init(port);
973 } else if (access("/dev/android_adb", F_OK) == 0) {
974 // listen on USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800975 usb_init();
976 } else {
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700977 // listen on default port
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100978 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800979 }
JP Abgrall408fa572011-03-16 15:57:42 -0700980 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800981 init_jdwp();
JP Abgrall408fa572011-03-16 15:57:42 -0700982 D("adb_main(): post init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800983#endif
984
985 if (is_daemon)
986 {
987 // inform our parent that we are up and running.
988#ifdef HAVE_WIN32_PROC
989 DWORD count;
990 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
991#elif defined(HAVE_FORKEXEC)
992 fprintf(stderr, "OK\n");
993#endif
994 start_logging();
995 }
JP Abgrall408fa572011-03-16 15:57:42 -0700996 D("Event loop starting\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800997
998 fdevent_loop();
999
1000 usb_cleanup();
1001
1002 return 0;
1003}
1004
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001005#if ADB_HOST
1006void connect_device(char* host, char* buffer, int buffer_size)
1007{
1008 int port, fd;
1009 char* portstr = strchr(host, ':');
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001010 char hostbuf[100];
1011 char serial[100];
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001012
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001013 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
1014 if (portstr) {
1015 if (portstr - host >= sizeof(hostbuf)) {
1016 snprintf(buffer, buffer_size, "bad host name %s", host);
1017 return;
1018 }
1019 // zero terminate the host at the point we found the colon
1020 hostbuf[portstr - host] = 0;
1021 if (sscanf(portstr + 1, "%d", &port) == 0) {
1022 snprintf(buffer, buffer_size, "bad port number %s", portstr);
1023 return;
1024 }
1025 } else {
1026 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001027 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001028
1029 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
1030 if (find_transport(serial)) {
1031 snprintf(buffer, buffer_size, "already connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001032 return;
1033 }
1034
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001035 fd = socket_network_client(hostbuf, port, SOCK_STREAM);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001036 if (fd < 0) {
1037 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
1038 return;
1039 }
1040
1041 D("client: connected on remote on fd %d\n", fd);
1042 close_on_exec(fd);
1043 disable_tcp_nagle(fd);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001044 register_socket_transport(fd, serial, port, 0);
1045 snprintf(buffer, buffer_size, "connected to %s", serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001046}
1047
1048void connect_emulator(char* port_spec, char* buffer, int buffer_size)
1049{
1050 char* port_separator = strchr(port_spec, ',');
1051 if (!port_separator) {
1052 snprintf(buffer, buffer_size,
1053 "unable to parse '%s' as <console port>,<adb port>",
1054 port_spec);
1055 return;
1056 }
1057
1058 // Zero-terminate console port and make port_separator point to 2nd port.
1059 *port_separator++ = 0;
1060 int console_port = strtol(port_spec, NULL, 0);
1061 int adb_port = strtol(port_separator, NULL, 0);
1062 if (!(console_port > 0 && adb_port > 0)) {
1063 *(port_separator - 1) = ',';
1064 snprintf(buffer, buffer_size,
1065 "Invalid port numbers: Expected positive numbers, got '%s'",
1066 port_spec);
1067 return;
1068 }
1069
1070 /* Check if the emulator is already known.
1071 * Note: There's a small but harmless race condition here: An emulator not
1072 * present just yet could be registered by another invocation right
1073 * after doing this check here. However, local_connect protects
1074 * against double-registration too. From here, a better error message
1075 * can be produced. In the case of the race condition, the very specific
1076 * error message won't be shown, but the data doesn't get corrupted. */
1077 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
1078 if (known_emulator != NULL) {
1079 snprintf(buffer, buffer_size,
1080 "Emulator on port %d already registered.", adb_port);
1081 return;
1082 }
1083
1084 /* Check if more emulators can be registered. Similar unproblematic
1085 * race condition as above. */
1086 int candidate_slot = get_available_local_transport_index();
1087 if (candidate_slot < 0) {
1088 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
1089 return;
1090 }
1091
1092 /* Preconditions met, try to connect to the emulator. */
1093 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
1094 snprintf(buffer, buffer_size,
1095 "Connected to emulator on ports %d,%d", console_port, adb_port);
1096 } else {
1097 snprintf(buffer, buffer_size,
1098 "Could not connect to emulator on ports %d,%d",
1099 console_port, adb_port);
1100 }
1101}
1102#endif
1103
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001104int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1105{
1106 atransport *transport = NULL;
1107 char buf[4096];
1108
1109 if(!strcmp(service, "kill")) {
1110 fprintf(stderr,"adb server killed by remote request\n");
1111 fflush(stdout);
1112 adb_write(reply_fd, "OKAY", 4);
1113 usb_cleanup();
1114 exit(0);
1115 }
1116
1117#if ADB_HOST
1118 // "transport:" is used for switching transport with a specified serial number
1119 // "transport-usb:" is used for switching transport to the only USB transport
1120 // "transport-local:" is used for switching transport to the only local transport
1121 // "transport-any:" is used for switching transport to the only transport
1122 if (!strncmp(service, "transport", strlen("transport"))) {
1123 char* error_string = "unknown failure";
1124 transport_type type = kTransportAny;
1125
1126 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1127 type = kTransportUsb;
1128 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1129 type = kTransportLocal;
1130 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1131 type = kTransportAny;
1132 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1133 service += strlen("transport:");
Tom Marlin3175c8e2011-07-27 12:56:14 -05001134 serial = service;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001135 }
1136
1137 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1138
1139 if (transport) {
1140 s->transport = transport;
1141 adb_write(reply_fd, "OKAY", 4);
1142 } else {
1143 sendfailmsg(reply_fd, error_string);
1144 }
1145 return 1;
1146 }
1147
1148 // return a list of all connected devices
1149 if (!strcmp(service, "devices")) {
1150 char buffer[4096];
1151 memset(buf, 0, sizeof(buf));
1152 memset(buffer, 0, sizeof(buffer));
1153 D("Getting device list \n");
1154 list_transports(buffer, sizeof(buffer));
1155 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer);
1156 D("Wrote device list \n");
1157 writex(reply_fd, buf, strlen(buf));
1158 return 0;
1159 }
1160
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001161 // add a new TCP transport, device or emulator
Mike Lockwood2f38b692009-08-24 15:58:40 -07001162 if (!strncmp(service, "connect:", 8)) {
1163 char buffer[4096];
Mike Lockwood2f38b692009-08-24 15:58:40 -07001164 char* host = service + 8;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001165 if (!strncmp(host, "emu:", 4)) {
1166 connect_emulator(host + 4, buffer, sizeof(buffer));
1167 } else {
1168 connect_device(host, buffer, sizeof(buffer));
Mike Lockwood2f38b692009-08-24 15:58:40 -07001169 }
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +01001170 // Send response for emulator and device
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001171 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
1172 writex(reply_fd, buf, strlen(buf));
1173 return 0;
1174 }
1175
1176 // remove TCP transport
1177 if (!strncmp(service, "disconnect:", 11)) {
1178 char buffer[4096];
1179 memset(buffer, 0, sizeof(buffer));
1180 char* serial = service + 11;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001181 if (serial[0] == 0) {
1182 // disconnect from all TCP devices
1183 unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001184 } else {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001185 char hostbuf[100];
1186 // assume port 5555 if no port is specified
1187 if (!strchr(serial, ':')) {
1188 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1189 serial = hostbuf;
1190 }
1191 atransport *t = find_transport(serial);
1192
1193 if (t) {
1194 unregister_transport(t);
1195 } else {
1196 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1197 }
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001198 }
1199
1200 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
Mike Lockwood2f38b692009-08-24 15:58:40 -07001201 writex(reply_fd, buf, strlen(buf));
1202 return 0;
1203 }
1204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001205 // returns our value for ADB_SERVER_VERSION
1206 if (!strcmp(service, "version")) {
1207 char version[12];
1208 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
1209 snprintf(buf, sizeof buf, "OKAY%04x%s", (unsigned)strlen(version), version);
1210 writex(reply_fd, buf, strlen(buf));
1211 return 0;
1212 }
1213
1214 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1215 char *out = "unknown";
1216 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1217 if (transport && transport->serial) {
1218 out = transport->serial;
1219 }
1220 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out);
1221 writex(reply_fd, buf, strlen(buf));
1222 return 0;
1223 }
1224 // indicates a new emulator instance has started
1225 if (!strncmp(service,"emulator:",9)) {
1226 int port = atoi(service+9);
1227 local_connect(port);
1228 /* we don't even need to send a reply */
1229 return 0;
1230 }
1231#endif // ADB_HOST
1232
1233 if(!strncmp(service,"forward:",8) || !strncmp(service,"killforward:",12)) {
1234 char *local, *remote, *err;
1235 int r;
1236 atransport *transport;
1237
1238 int createForward = strncmp(service,"kill",4);
1239
1240 local = service + (createForward ? 8 : 12);
1241 remote = strchr(local,';');
1242 if(remote == 0) {
1243 sendfailmsg(reply_fd, "malformed forward spec");
1244 return 0;
1245 }
1246
1247 *remote++ = 0;
1248 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')){
1249 sendfailmsg(reply_fd, "malformed forward spec");
1250 return 0;
1251 }
1252
1253 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1254 if (!transport) {
1255 sendfailmsg(reply_fd, err);
1256 return 0;
1257 }
1258
1259 if (createForward) {
1260 r = install_listener(local, remote, transport);
1261 } else {
1262 r = remove_listener(local, remote, transport);
1263 }
1264 if(r == 0) {
1265 /* 1st OKAY is connect, 2nd OKAY is status */
1266 writex(reply_fd, "OKAYOKAY", 8);
1267 return 0;
1268 }
1269
1270 if (createForward) {
1271 sendfailmsg(reply_fd, (r == -1) ? "cannot rebind smartsocket" : "cannot bind socket");
1272 } else {
1273 sendfailmsg(reply_fd, "cannot remove listener");
1274 }
1275 return 0;
1276 }
1277
1278 if(!strncmp(service,"get-state",strlen("get-state"))) {
1279 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1280 char *state = connection_state_name(transport);
1281 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(state),state);
1282 writex(reply_fd, buf, strlen(buf));
1283 return 0;
1284 }
1285 return -1;
1286}
1287
1288#if !ADB_HOST
1289int recovery_mode = 0;
1290#endif
1291
1292int main(int argc, char **argv)
1293{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001294#if ADB_HOST
1295 adb_sysdeps_init();
JP Abgrall408fa572011-03-16 15:57:42 -07001296 adb_trace_init();
1297 D("Handling commandline()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001298 return adb_commandline(argc - 1, argv + 1);
1299#else
1300 if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
1301 adb_device_banner = "recovery";
1302 recovery_mode = 1;
1303 }
Mike Lockwood1f546e62009-05-25 18:17:55 -04001304
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001305 start_device_log();
JP Abgrall408fa572011-03-16 15:57:42 -07001306 D("Handling main()\n");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001307 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001308#endif
1309}