adb.c revision 1f546e6d1f6ccd1964336ddf0d8e8b3b11b1e945
1/* 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> 26#include <sys/time.h> 27 28#include "sysdeps.h" 29#include "adb.h" 30 31#if !ADB_HOST 32#include <private/android_filesystem_config.h> 33#else 34#include "usb_vendors.h" 35#endif 36 37 38int HOST = 0; 39 40static const char *adb_device_banner = "device"; 41 42void fatal(const char *fmt, ...) 43{ 44 va_list ap; 45 va_start(ap, fmt); 46 fprintf(stderr, "error: "); 47 vfprintf(stderr, fmt, ap); 48 fprintf(stderr, "\n"); 49 va_end(ap); 50 exit(-1); 51} 52 53void fatal_errno(const char *fmt, ...) 54{ 55 va_list ap; 56 va_start(ap, fmt); 57 fprintf(stderr, "error: %s: ", strerror(errno)); 58 vfprintf(stderr, fmt, ap); 59 fprintf(stderr, "\n"); 60 va_end(ap); 61 exit(-1); 62} 63 64int adb_trace_mask; 65 66/* read a comma/space/colum/semi-column separated list of tags 67 * from the ADB_TRACE environment variable and build the trace 68 * mask from it. note that '1' and 'all' are special cases to 69 * enable all tracing 70 */ 71void adb_trace_init(void) 72{ 73 const char* p = getenv("ADB_TRACE"); 74 const char* q; 75 76 static const struct { 77 const char* tag; 78 int flag; 79 } tags[] = { 80 { "1", 0 }, 81 { "all", 0 }, 82 { "adb", TRACE_ADB }, 83 { "sockets", TRACE_SOCKETS }, 84 { "packets", TRACE_PACKETS }, 85 { "rwx", TRACE_RWX }, 86 { "usb", TRACE_USB }, 87 { "sync", TRACE_SYNC }, 88 { "sysdeps", TRACE_SYSDEPS }, 89 { "transport", TRACE_TRANSPORT }, 90 { "jdwp", TRACE_JDWP }, 91 { NULL, 0 } 92 }; 93 94 if (p == NULL) 95 return; 96 97 /* use a comma/column/semi-colum/space separated list */ 98 while (*p) { 99 int len, tagn; 100 101 q = strpbrk(p, " ,:;"); 102 if (q == NULL) { 103 q = p + strlen(p); 104 } 105 len = q - p; 106 107 for (tagn = 0; tags[tagn].tag != NULL; tagn++) 108 { 109 int taglen = strlen(tags[tagn].tag); 110 111 if (len == taglen && !memcmp(tags[tagn].tag, p, len) ) 112 { 113 int flag = tags[tagn].flag; 114 if (flag == 0) { 115 adb_trace_mask = ~0; 116 return; 117 } 118 adb_trace_mask |= (1 << flag); 119 break; 120 } 121 } 122 p = q; 123 if (*p) 124 p++; 125 } 126} 127 128 129apacket *get_apacket(void) 130{ 131 apacket *p = malloc(sizeof(apacket)); 132 if(p == 0) fatal("failed to allocate an apacket"); 133 memset(p, 0, sizeof(apacket) - MAX_PAYLOAD); 134 return p; 135} 136 137void put_apacket(apacket *p) 138{ 139 free(p); 140} 141 142void handle_online(void) 143{ 144 D("adb: online\n"); 145#if !ADB_HOST 146 property_set("adb.connected","1"); 147#endif 148} 149 150void handle_offline(atransport *t) 151{ 152 D("adb: offline\n"); 153 //Close the associated usb 154 run_transport_disconnects(t); 155#if !ADB_HOST 156 property_set("adb.connected",""); 157#endif 158} 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 309 D("handle_packet() %d\n", p->msg.command); 310 311 print_packet("recv", p); 312 313 switch(p->msg.command){ 314 case A_SYNC: 315 if(p->msg.arg0){ 316 send_packet(p, t); 317 if(HOST) send_connect(t); 318 } else { 319 t->connection_state = CS_OFFLINE; 320 handle_offline(t); 321 send_packet(p, t); 322 } 323 return; 324 325 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */ 326 /* XXX verify version, etc */ 327 if(t->connection_state != CS_OFFLINE) { 328 t->connection_state = CS_OFFLINE; 329 handle_offline(t); 330 } 331 parse_banner((char*) p->data, t); 332 handle_online(); 333 if(!HOST) send_connect(t); 334 break; 335 336 case A_OPEN: /* OPEN(local-id, 0, "destination") */ 337 if(t->connection_state != CS_OFFLINE) { 338 char *name = (char*) p->data; 339 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0; 340 s = create_local_service_socket(name); 341 if(s == 0) { 342 send_close(0, p->msg.arg0, t); 343 } else { 344 s->peer = create_remote_socket(p->msg.arg0, t); 345 s->peer->peer = s; 346 send_ready(s->id, s->peer->id, t); 347 s->ready(s); 348 } 349 } 350 break; 351 352 case A_OKAY: /* READY(local-id, remote-id, "") */ 353 if(t->connection_state != CS_OFFLINE) { 354 if((s = find_local_socket(p->msg.arg1))) { 355 if(s->peer == 0) { 356 s->peer = create_remote_socket(p->msg.arg0, t); 357 s->peer->peer = s; 358 } 359 s->ready(s); 360 } 361 } 362 break; 363 364 case A_CLSE: /* CLOSE(local-id, remote-id, "") */ 365 if(t->connection_state != CS_OFFLINE) { 366 if((s = find_local_socket(p->msg.arg1))) { 367 s->close(s); 368 } 369 } 370 break; 371 372 case A_WRTE: 373 if(t->connection_state != CS_OFFLINE) { 374 if((s = find_local_socket(p->msg.arg1))) { 375 unsigned rid = p->msg.arg0; 376 p->len = p->msg.data_length; 377 378 if(s->enqueue(s, p) == 0) { 379 D("Enqueue the socket\n"); 380 send_ready(s->id, rid, t); 381 } 382 return; 383 } 384 } 385 break; 386 387 default: 388 printf("handle_packet: what is %08x?!\n", p->msg.command); 389 } 390 391 put_apacket(p); 392} 393 394alistener listener_list = { 395 .next = &listener_list, 396 .prev = &listener_list, 397}; 398 399static void ss_listener_event_func(int _fd, unsigned ev, void *_l) 400{ 401 asocket *s; 402 403 if(ev & FDE_READ) { 404 struct sockaddr addr; 405 socklen_t alen; 406 int fd; 407 408 alen = sizeof(addr); 409 fd = adb_socket_accept(_fd, &addr, &alen); 410 if(fd < 0) return; 411 412 adb_socket_setbufsize(fd, CHUNK_SIZE); 413 414 s = create_local_socket(fd); 415 if(s) { 416 connect_to_smartsocket(s); 417 return; 418 } 419 420 adb_close(fd); 421 } 422} 423 424static void listener_event_func(int _fd, unsigned ev, void *_l) 425{ 426 alistener *l = _l; 427 asocket *s; 428 429 if(ev & FDE_READ) { 430 struct sockaddr addr; 431 socklen_t alen; 432 int fd; 433 434 alen = sizeof(addr); 435 fd = adb_socket_accept(_fd, &addr, &alen); 436 if(fd < 0) return; 437 438 s = create_local_socket(fd); 439 if(s) { 440 s->transport = l->transport; 441 connect_to_remote(s, l->connect_to); 442 return; 443 } 444 445 adb_close(fd); 446 } 447} 448 449static void free_listener(alistener* l) 450{ 451 if (l->next) { 452 l->next->prev = l->prev; 453 l->prev->next = l->next; 454 l->next = l->prev = l; 455 } 456 457 // closes the corresponding fd 458 fdevent_remove(&l->fde); 459 460 if (l->local_name) 461 free((char*)l->local_name); 462 463 if (l->connect_to) 464 free((char*)l->connect_to); 465 466 if (l->transport) { 467 remove_transport_disconnect(l->transport, &l->disconnect); 468 } 469 free(l); 470} 471 472static void listener_disconnect(void* _l, atransport* t) 473{ 474 alistener* l = _l; 475 476 free_listener(l); 477} 478 479int local_name_to_fd(const char *name) 480{ 481 int port; 482 483 if(!strncmp("tcp:", name, 4)){ 484 int ret; 485 port = atoi(name + 4); 486 ret = socket_loopback_server(port, SOCK_STREAM); 487 return ret; 488 } 489#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */ 490 // It's non-sensical to support the "reserved" space on the adb host side 491 if(!strncmp(name, "local:", 6)) { 492 return socket_local_server(name + 6, 493 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); 494 } else if(!strncmp(name, "localabstract:", 14)) { 495 return socket_local_server(name + 14, 496 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); 497 } else if(!strncmp(name, "localfilesystem:", 16)) { 498 return socket_local_server(name + 16, 499 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); 500 } 501 502#endif 503 printf("unknown local portname '%s'\n", name); 504 return -1; 505} 506 507static int remove_listener(const char *local_name, const char *connect_to, atransport* transport) 508{ 509 alistener *l; 510 511 for (l = listener_list.next; l != &listener_list; l = l->next) { 512 if (!strcmp(local_name, l->local_name) && 513 !strcmp(connect_to, l->connect_to) && 514 l->transport && l->transport == transport) { 515 516 listener_disconnect(l, transport); 517 return 0; 518 } 519 } 520 521 return -1; 522} 523 524static int install_listener(const char *local_name, const char *connect_to, atransport* transport) 525{ 526 alistener *l; 527 528 //printf("install_listener('%s','%s')\n", local_name, connect_to); 529 530 for(l = listener_list.next; l != &listener_list; l = l->next){ 531 if(strcmp(local_name, l->local_name) == 0) { 532 char *cto; 533 534 /* can't repurpose a smartsocket */ 535 if(l->connect_to[0] == '*') { 536 return -1; 537 } 538 539 cto = strdup(connect_to); 540 if(cto == 0) { 541 return -1; 542 } 543 544 //printf("rebinding '%s' to '%s'\n", local_name, connect_to); 545 free((void*) l->connect_to); 546 l->connect_to = cto; 547 if (l->transport != transport) { 548 remove_transport_disconnect(l->transport, &l->disconnect); 549 l->transport = transport; 550 add_transport_disconnect(l->transport, &l->disconnect); 551 } 552 return 0; 553 } 554 } 555 556 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem; 557 if((l->local_name = strdup(local_name)) == 0) goto nomem; 558 if((l->connect_to = strdup(connect_to)) == 0) goto nomem; 559 560 561 l->fd = local_name_to_fd(local_name); 562 if(l->fd < 0) { 563 free((void*) l->local_name); 564 free((void*) l->connect_to); 565 free(l); 566 printf("cannot bind '%s'\n", local_name); 567 return -2; 568 } 569 570 close_on_exec(l->fd); 571 if(!strcmp(l->connect_to, "*smartsocket*")) { 572 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l); 573 } else { 574 fdevent_install(&l->fde, l->fd, listener_event_func, l); 575 } 576 fdevent_set(&l->fde, FDE_READ); 577 578 l->next = &listener_list; 579 l->prev = listener_list.prev; 580 l->next->prev = l; 581 l->prev->next = l; 582 l->transport = transport; 583 584 if (transport) { 585 l->disconnect.opaque = l; 586 l->disconnect.func = listener_disconnect; 587 add_transport_disconnect(transport, &l->disconnect); 588 } 589 return 0; 590 591nomem: 592 fatal("cannot allocate listener"); 593 return 0; 594} 595 596#ifdef HAVE_FORKEXEC 597static void sigchld_handler(int n) 598{ 599 int status; 600 while(waitpid(-1, &status, WNOHANG) > 0) ; 601} 602#endif 603 604#ifdef HAVE_WIN32_PROC 605static BOOL WINAPI ctrlc_handler(DWORD type) 606{ 607 exit(STATUS_CONTROL_C_EXIT); 608 return TRUE; 609} 610#endif 611 612static void adb_cleanup(void) 613{ 614 usb_cleanup(); 615} 616 617void start_logging(void) 618{ 619#ifdef HAVE_WIN32_PROC 620 char temp[ MAX_PATH ]; 621 FILE* fnul; 622 FILE* flog; 623 624 GetTempPath( sizeof(temp) - 8, temp ); 625 strcat( temp, "adb.log" ); 626 627 /* Win32 specific redirections */ 628 fnul = fopen( "NUL", "rt" ); 629 if (fnul != NULL) 630 stdin[0] = fnul[0]; 631 632 flog = fopen( temp, "at" ); 633 if (flog == NULL) 634 flog = fnul; 635 636 setvbuf( flog, NULL, _IONBF, 0 ); 637 638 stdout[0] = flog[0]; 639 stderr[0] = flog[0]; 640 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); 641#else 642 int fd; 643 644 fd = unix_open("/dev/null", O_RDONLY); 645 dup2(fd, 0); 646 647 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640); 648 if(fd < 0) { 649 fd = unix_open("/dev/null", O_WRONLY); 650 } 651 dup2(fd, 1); 652 dup2(fd, 2); 653 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); 654#endif 655} 656 657#if !ADB_HOST 658void start_device_log(void) 659{ 660 int fd; 661 char path[PATH_MAX]; 662 struct tm now; 663 time_t t; 664 char value[PROPERTY_VALUE_MAX]; 665 666 // read the trace mask from persistent property persist.adb.trace_mask 667 // give up if the property is not set or cannot be parsed 668 property_get("persist.adb.trace_mask", value, ""); 669 if (sscanf(value, "%x", &adb_trace_mask) != 1) 670 return; 671 672 adb_mkdir("/data/adb", 0775); 673 tzset(); 674 time(&t); 675 localtime_r(&t, &now); 676 strftime(path, sizeof(path), 677 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt", 678 &now); 679 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640); 680 if (fd < 0) 681 return; 682 683 // redirect stdout and stderr to the log file 684 dup2(fd, 1); 685 dup2(fd, 2); 686 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); 687 688 fd = unix_open("/dev/null", O_RDONLY); 689 dup2(fd, 0); 690} 691#endif 692 693#if ADB_HOST 694int launch_server() 695{ 696#ifdef HAVE_WIN32_PROC 697 /* we need to start the server in the background */ 698 /* we create a PIPE that will be used to wait for the server's "OK" */ 699 /* message since the pipe handles must be inheritable, we use a */ 700 /* security attribute */ 701 HANDLE pipe_read, pipe_write; 702 SECURITY_ATTRIBUTES sa; 703 STARTUPINFO startup; 704 PROCESS_INFORMATION pinfo; 705 char program_path[ MAX_PATH ]; 706 int ret; 707 708 sa.nLength = sizeof(sa); 709 sa.lpSecurityDescriptor = NULL; 710 sa.bInheritHandle = TRUE; 711 712 /* create pipe, and ensure its read handle isn't inheritable */ 713 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 ); 714 if (!ret) { 715 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() ); 716 return -1; 717 } 718 719 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 ); 720 721 ZeroMemory( &startup, sizeof(startup) ); 722 startup.cb = sizeof(startup); 723 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE ); 724 startup.hStdOutput = pipe_write; 725 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE ); 726 startup.dwFlags = STARTF_USESTDHANDLES; 727 728 ZeroMemory( &pinfo, sizeof(pinfo) ); 729 730 /* get path of current program */ 731 GetModuleFileName( NULL, program_path, sizeof(program_path) ); 732 733 ret = CreateProcess( 734 program_path, /* program path */ 735 "adb fork-server server", 736 /* the fork-server argument will set the 737 debug = 2 in the child */ 738 NULL, /* process handle is not inheritable */ 739 NULL, /* thread handle is not inheritable */ 740 TRUE, /* yes, inherit some handles */ 741 DETACHED_PROCESS, /* the new process doesn't have a console */ 742 NULL, /* use parent's environment block */ 743 NULL, /* use parent's starting directory */ 744 &startup, /* startup info, i.e. std handles */ 745 &pinfo ); 746 747 CloseHandle( pipe_write ); 748 749 if (!ret) { 750 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() ); 751 CloseHandle( pipe_read ); 752 return -1; 753 } 754 755 CloseHandle( pinfo.hProcess ); 756 CloseHandle( pinfo.hThread ); 757 758 /* wait for the "OK\n" message */ 759 { 760 char temp[3]; 761 DWORD count; 762 763 ret = ReadFile( pipe_read, temp, 3, &count, NULL ); 764 CloseHandle( pipe_read ); 765 if ( !ret ) { 766 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() ); 767 return -1; 768 } 769 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') { 770 fprintf(stderr, "ADB server didn't ACK\n" ); 771 return -1; 772 } 773 } 774#elif defined(HAVE_FORKEXEC) 775 char path[PATH_MAX]; 776 int fd[2]; 777 778 // set up a pipe so the child can tell us when it is ready. 779 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child. 780 if (pipe(fd)) { 781 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno); 782 return -1; 783 } 784 get_my_path(path); 785 pid_t pid = fork(); 786 if(pid < 0) return -1; 787 788 if (pid == 0) { 789 // child side of the fork 790 791 // redirect stderr to the pipe 792 // we use stderr instead of stdout due to stdout's buffering behavior. 793 adb_close(fd[0]); 794 dup2(fd[1], STDERR_FILENO); 795 adb_close(fd[1]); 796 797 // child process 798 int result = execl(path, "adb", "fork-server", "server", NULL); 799 // this should not return 800 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno); 801 } else { 802 // parent side of the fork 803 804 char temp[3]; 805 806 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C'; 807 // wait for the "OK\n" message 808 adb_close(fd[1]); 809 int ret = adb_read(fd[0], temp, 3); 810 adb_close(fd[0]); 811 if (ret < 0) { 812 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", errno); 813 return -1; 814 } 815 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') { 816 fprintf(stderr, "ADB server didn't ACK\n" ); 817 return -1; 818 } 819 820 setsid(); 821 } 822#else 823#error "cannot implement background server start on this platform" 824#endif 825 return 0; 826} 827#endif 828 829int adb_main(int is_daemon) 830{ 831#if !ADB_HOST 832 int secure = 0; 833 char value[PROPERTY_VALUE_MAX]; 834#endif 835 836 atexit(adb_cleanup); 837#ifdef HAVE_WIN32_PROC 838 SetConsoleCtrlHandler( ctrlc_handler, TRUE ); 839#elif defined(HAVE_FORKEXEC) 840 signal(SIGCHLD, sigchld_handler); 841 signal(SIGPIPE, SIG_IGN); 842#endif 843 844 init_transport_registration(); 845 846 847#if ADB_HOST 848 HOST = 1; 849 usb_vendors_init(); 850 usb_init(); 851 local_init(); 852 853 if(install_listener("tcp:5037", "*smartsocket*", NULL)) { 854 exit(1); 855 } 856#else 857 /* run adbd in secure mode if ro.secure is set and 858 ** we are not in the emulator 859 */ 860 property_get("ro.kernel.qemu", value, ""); 861 if (strcmp(value, "1") != 0) { 862 property_get("ro.secure", value, ""); 863 if (strcmp(value, "1") == 0) { 864 // don't run as root if ro.secure is set... 865 secure = 1; 866 867 // ... except we allow running as root in userdebug builds if the 868 // service.adb.root property has been set by the "adb root" command 869 property_get("ro.debuggable", value, ""); 870 if (strcmp(value, "1") == 0) { 871 property_get("service.adb.root", value, ""); 872 if (strcmp(value, "1") == 0) { 873 secure = 0; 874 } 875 } 876 } 877 } 878 879 /* don't listen on port 5037 if we are running in secure mode */ 880 /* don't run as root if we are running in secure mode */ 881 if (secure) { 882 /* add extra groups: 883 ** AID_ADB to access the USB driver 884 ** AID_LOG to read system logs (adb logcat) 885 ** AID_INPUT to diagnose input issues (getevent) 886 ** AID_INET to diagnose network issues (netcfg, ping) 887 ** AID_GRAPHICS to access the frame buffer 888 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump) 889 ** AID_SDCARD_RW to allow writing to the SD card 890 */ 891 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS, 892 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_RW }; 893 setgroups(sizeof(groups)/sizeof(groups[0]), groups); 894 895 /* then switch user and group to "shell" */ 896 setgid(AID_SHELL); 897 setuid(AID_SHELL); 898 899 D("Local port 5037 disabled\n"); 900 } else { 901 if(install_listener("tcp:5037", "*smartsocket*", NULL)) { 902 exit(1); 903 } 904 } 905 906 /* for the device, start the usb transport if the 907 ** android usb device exists, otherwise start the 908 ** network transport. 909 */ 910 if(access("/dev/android_adb", F_OK) == 0 || 911 access("/dev/android", F_OK) == 0) { 912 usb_init(); 913 } else { 914 local_init(); 915 } 916 init_jdwp(); 917#endif 918 919 if (is_daemon) 920 { 921 // inform our parent that we are up and running. 922#ifdef HAVE_WIN32_PROC 923 DWORD count; 924 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL ); 925#elif defined(HAVE_FORKEXEC) 926 fprintf(stderr, "OK\n"); 927#endif 928 start_logging(); 929 } 930 931 fdevent_loop(); 932 933 usb_cleanup(); 934 935 return 0; 936} 937 938int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s) 939{ 940 atransport *transport = NULL; 941 char buf[4096]; 942 943 if(!strcmp(service, "kill")) { 944 fprintf(stderr,"adb server killed by remote request\n"); 945 fflush(stdout); 946 adb_write(reply_fd, "OKAY", 4); 947 usb_cleanup(); 948 exit(0); 949 } 950 951#if ADB_HOST 952 // "transport:" is used for switching transport with a specified serial number 953 // "transport-usb:" is used for switching transport to the only USB transport 954 // "transport-local:" is used for switching transport to the only local transport 955 // "transport-any:" is used for switching transport to the only transport 956 if (!strncmp(service, "transport", strlen("transport"))) { 957 char* error_string = "unknown failure"; 958 transport_type type = kTransportAny; 959 960 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) { 961 type = kTransportUsb; 962 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) { 963 type = kTransportLocal; 964 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) { 965 type = kTransportAny; 966 } else if (!strncmp(service, "transport:", strlen("transport:"))) { 967 service += strlen("transport:"); 968 serial = strdup(service); 969 } 970 971 transport = acquire_one_transport(CS_ANY, type, serial, &error_string); 972 973 if (transport) { 974 s->transport = transport; 975 adb_write(reply_fd, "OKAY", 4); 976 } else { 977 sendfailmsg(reply_fd, error_string); 978 } 979 return 1; 980 } 981 982 // return a list of all connected devices 983 if (!strcmp(service, "devices")) { 984 char buffer[4096]; 985 memset(buf, 0, sizeof(buf)); 986 memset(buffer, 0, sizeof(buffer)); 987 D("Getting device list \n"); 988 list_transports(buffer, sizeof(buffer)); 989 snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer); 990 D("Wrote device list \n"); 991 writex(reply_fd, buf, strlen(buf)); 992 return 0; 993 } 994 995 // returns our value for ADB_SERVER_VERSION 996 if (!strcmp(service, "version")) { 997 char version[12]; 998 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION); 999 snprintf(buf, sizeof buf, "OKAY%04x%s", (unsigned)strlen(version), version); 1000 writex(reply_fd, buf, strlen(buf)); 1001 return 0; 1002 } 1003 1004 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) { 1005 char *out = "unknown"; 1006 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); 1007 if (transport && transport->serial) { 1008 out = transport->serial; 1009 } 1010 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out); 1011 writex(reply_fd, buf, strlen(buf)); 1012 return 0; 1013 } 1014 // indicates a new emulator instance has started 1015 if (!strncmp(service,"emulator:",9)) { 1016 int port = atoi(service+9); 1017 local_connect(port); 1018 /* we don't even need to send a reply */ 1019 return 0; 1020 } 1021#endif // ADB_HOST 1022 1023 if(!strncmp(service,"forward:",8) || !strncmp(service,"killforward:",12)) { 1024 char *local, *remote, *err; 1025 int r; 1026 atransport *transport; 1027 1028 int createForward = strncmp(service,"kill",4); 1029 1030 local = service + (createForward ? 8 : 12); 1031 remote = strchr(local,';'); 1032 if(remote == 0) { 1033 sendfailmsg(reply_fd, "malformed forward spec"); 1034 return 0; 1035 } 1036 1037 *remote++ = 0; 1038 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')){ 1039 sendfailmsg(reply_fd, "malformed forward spec"); 1040 return 0; 1041 } 1042 1043 transport = acquire_one_transport(CS_ANY, ttype, serial, &err); 1044 if (!transport) { 1045 sendfailmsg(reply_fd, err); 1046 return 0; 1047 } 1048 1049 if (createForward) { 1050 r = install_listener(local, remote, transport); 1051 } else { 1052 r = remove_listener(local, remote, transport); 1053 } 1054 if(r == 0) { 1055 /* 1st OKAY is connect, 2nd OKAY is status */ 1056 writex(reply_fd, "OKAYOKAY", 8); 1057 return 0; 1058 } 1059 1060 if (createForward) { 1061 sendfailmsg(reply_fd, (r == -1) ? "cannot rebind smartsocket" : "cannot bind socket"); 1062 } else { 1063 sendfailmsg(reply_fd, "cannot remove listener"); 1064 } 1065 return 0; 1066 } 1067 1068 if(!strncmp(service,"get-state",strlen("get-state"))) { 1069 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); 1070 char *state = connection_state_name(transport); 1071 snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(state),state); 1072 writex(reply_fd, buf, strlen(buf)); 1073 return 0; 1074 } 1075 return -1; 1076} 1077 1078#if !ADB_HOST 1079int recovery_mode = 0; 1080#endif 1081 1082int main(int argc, char **argv) 1083{ 1084 adb_trace_init(); 1085#if ADB_HOST 1086 adb_sysdeps_init(); 1087 return adb_commandline(argc - 1, argv + 1); 1088#else 1089 if((argc > 1) && (!strcmp(argv[1],"recovery"))) { 1090 adb_device_banner = "recovery"; 1091 recovery_mode = 1; 1092 } 1093 1094 start_device_log(); 1095 return adb_main(0); 1096#endif 1097} 1098