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