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