commandline.cpp revision 81416fdb186070fe4db3ca5fed2e713a4eecaac1
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#include <assert.h> 18#include <ctype.h> 19#include <errno.h> 20#include <limits.h> 21#include <stdarg.h> 22#include <stdint.h> 23#include <stdio.h> 24#include <stdlib.h> 25#include <string.h> 26#include <sys/stat.h> 27#include <sys/types.h> 28 29#if !defined(_WIN32) 30#include <termios.h> 31#include <unistd.h> 32#endif 33 34#include "sysdeps.h" 35 36#define TRACE_TAG TRACE_ADB 37#include "adb.h" 38#include "adb_auth.h" 39#include "adb_client.h" 40#include "adb_io.h" 41#include "file_sync_service.h" 42 43static int do_cmd(transport_type ttype, const char* serial, const char *cmd, ...); 44 45int find_sync_dirs(const char *srcarg, 46 char **system_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out, 47 char **oem_srcdir_out); 48int install_app(transport_type transport, const char* serial, int argc, 49 const char** argv); 50int install_multiple_app(transport_type transport, const char* serial, int argc, 51 const char** argv); 52int uninstall_app(transport_type transport, const char* serial, int argc, 53 const char** argv); 54 55static const char *gProductOutPath = NULL; 56extern int gListenAll; 57 58static char *product_file(const char *extra) 59{ 60 if (gProductOutPath == NULL) { 61 fprintf(stderr, "adb: Product directory not specified; " 62 "use -p or define ANDROID_PRODUCT_OUT\n"); 63 exit(1); 64 } 65 66 int n = strlen(gProductOutPath) + strlen(extra) + 2; 67 char* x = reinterpret_cast<char*>(malloc(n)); 68 if (x == 0) { 69 fprintf(stderr, "adb: Out of memory (product_file())\n"); 70 exit(1); 71 } 72 73 snprintf(x, (size_t)n, "%s" OS_PATH_SEPARATOR_STR "%s", gProductOutPath, extra); 74 return x; 75} 76 77void version(FILE * out) { 78 fprintf(out, "Android Debug Bridge version %d.%d.%d\n", 79 ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION); 80} 81 82void help() 83{ 84 version(stderr); 85 86 fprintf(stderr, 87 "\n" 88 " -a - directs adb to listen on all interfaces for a connection\n" 89 " -d - directs command to the only connected USB device\n" 90 " returns an error if more than one USB device is present.\n" 91 " -e - directs command to the only running emulator.\n" 92 " returns an error if more than one emulator is running.\n" 93 " -s <specific device> - directs command to the device or emulator with the given\n" 94 " serial number or qualifier. Overrides ANDROID_SERIAL\n" 95 " environment variable.\n" 96 " -p <product name or path> - simple product name like 'sooner', or\n" 97 " a relative/absolute path to a product\n" 98 " out directory like 'out/target/product/sooner'.\n" 99 " If -p is not specified, the ANDROID_PRODUCT_OUT\n" 100 " environment variable is used, which must\n" 101 " be an absolute path.\n" 102 " -H - Name of adb server host (default: localhost)\n" 103 " -P - Port of adb server (default: 5037)\n" 104 " devices [-l] - list all connected devices\n" 105 " ('-l' will also list device qualifiers)\n" 106 " connect <host>[:<port>] - connect to a device via TCP/IP\n" 107 " Port 5555 is used by default if no port number is specified.\n" 108 " disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.\n" 109 " Port 5555 is used by default if no port number is specified.\n" 110 " Using this command with no additional arguments\n" 111 " will disconnect from all connected TCP/IP devices.\n" 112 "\n" 113 "device commands:\n" 114 " adb push [-p] <local> <remote>\n" 115 " - copy file/dir to device\n" 116 " ('-p' to display the transfer progress)\n" 117 " adb pull [-p] [-a] <remote> [<local>]\n" 118 " - copy file/dir from device\n" 119 " ('-p' to display the transfer progress)\n" 120 " ('-a' means copy timestamp and mode)\n" 121 " adb sync [ <directory> ] - copy host->device only if changed\n" 122 " (-l means list but don't copy)\n" 123 " (see 'adb help all')\n" 124 " adb shell - run remote shell interactively\n" 125 " adb shell <command> - run remote shell command\n" 126 " adb emu <command> - run emulator console command\n" 127 " adb logcat [ <filter-spec> ] - View device log\n" 128 " adb forward --list - list all forward socket connections.\n" 129 " the format is a list of lines with the following format:\n" 130 " <serial> \" \" <local> \" \" <remote> \"\\n\"\n" 131 " adb forward <local> <remote> - forward socket connections\n" 132 " forward specs are one of: \n" 133 " tcp:<port>\n" 134 " localabstract:<unix domain socket name>\n" 135 " localreserved:<unix domain socket name>\n" 136 " localfilesystem:<unix domain socket name>\n" 137 " dev:<character device name>\n" 138 " jdwp:<process pid> (remote only)\n" 139 " adb forward --no-rebind <local> <remote>\n" 140 " - same as 'adb forward <local> <remote>' but fails\n" 141 " if <local> is already forwarded\n" 142 " adb forward --remove <local> - remove a specific forward socket connection\n" 143 " adb forward --remove-all - remove all forward socket connections\n" 144 " adb reverse --list - list all reverse socket connections from device\n" 145 " adb reverse <remote> <local> - reverse socket connections\n" 146 " reverse specs are one of:\n" 147 " tcp:<port>\n" 148 " localabstract:<unix domain socket name>\n" 149 " localreserved:<unix domain socket name>\n" 150 " localfilesystem:<unix domain socket name>\n" 151 " adb reverse --norebind <remote> <local>\n" 152 " - same as 'adb reverse <remote> <local>' but fails\n" 153 " if <remote> is already reversed.\n" 154 " adb reverse --remove <remote>\n" 155 " - remove a specific reversed socket connection\n" 156 " adb reverse --remove-all - remove all reversed socket connections from device\n" 157 " adb jdwp - list PIDs of processes hosting a JDWP transport\n" 158 " adb install [-lrtsd] <file>\n" 159 " adb install-multiple [-lrtsdp] <file...>\n" 160 " - push this package file to the device and install it\n" 161 " (-l: forward lock application)\n" 162 " (-r: replace existing application)\n" 163 " (-t: allow test packages)\n" 164 " (-s: install application on sdcard)\n" 165 " (-d: allow version code downgrade)\n" 166 " (-p: partial application install)\n" 167 " adb uninstall [-k] <package> - remove this app package from the device\n" 168 " ('-k' means keep the data and cache directories)\n" 169 " adb bugreport - return all information from the device\n" 170 " that should be included in a bug report.\n" 171 "\n" 172 " adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]\n" 173 " - write an archive of the device's data to <file>.\n" 174 " If no -f option is supplied then the data is written\n" 175 " to \"backup.ab\" in the current directory.\n" 176 " (-apk|-noapk enable/disable backup of the .apks themselves\n" 177 " in the archive; the default is noapk.)\n" 178 " (-obb|-noobb enable/disable backup of any installed apk expansion\n" 179 " (aka .obb) files associated with each application; the default\n" 180 " is noobb.)\n" 181 " (-shared|-noshared enable/disable backup of the device's\n" 182 " shared storage / SD card contents; the default is noshared.)\n" 183 " (-all means to back up all installed applications)\n" 184 " (-system|-nosystem toggles whether -all automatically includes\n" 185 " system applications; the default is to include system apps)\n" 186 " (<packages...> is the list of applications to be backed up. If\n" 187 " the -all or -shared flags are passed, then the package\n" 188 " list is optional. Applications explicitly given on the\n" 189 " command line will be included even if -nosystem would\n" 190 " ordinarily cause them to be omitted.)\n" 191 "\n" 192 " adb restore <file> - restore device contents from the <file> backup archive\n" 193 "\n" 194 " adb disable-verity - disable dm-verity checking on USERDEBUG builds\n" 195 " adb enable-verity - re-enable dm-verity checking on USERDEBUG builds\n" 196 " adb keygen <file> - generate adb public/private key. The private key is stored in <file>,\n" 197 " and the public key is stored in <file>.pub. Any existing files\n" 198 " are overwritten.\n" 199 " adb help - show this help message\n" 200 " adb version - show version num\n" 201 "\n" 202 "scripting:\n" 203 " adb wait-for-device - block until device is online\n" 204 " adb start-server - ensure that there is a server running\n" 205 " adb kill-server - kill the server if it is running\n" 206 " adb get-state - prints: offline | bootloader | device\n" 207 " adb get-serialno - prints: <serial-number>\n" 208 " adb get-devpath - prints: <device-path>\n" 209 " adb status-window - continuously print device status for a specified device\n" 210 " adb remount - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write\n" 211 " adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program\n" 212 " adb reboot-bootloader - reboots the device into the bootloader\n" 213 " adb root - restarts the adbd daemon with root permissions\n" 214 " adb unroot - restarts the adbd daemon without root permissions\n" 215 " adb usb - restarts the adbd daemon listening on USB\n" 216 " adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port\n" 217 "networking:\n" 218 " adb ppp <tty> [parameters] - Run PPP over USB.\n" 219 " Note: you should not automatically start a PPP connection.\n" 220 " <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1\n" 221 " [parameters] - Eg. defaultroute debug dump local notty usepeerdns\n" 222 "\n" 223 "adb sync notes: adb sync [ <directory> ]\n" 224 " <localdir> can be interpreted in several ways:\n" 225 "\n" 226 " - If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.\n" 227 "\n" 228 " - If it is \"system\", \"vendor\", \"oem\" or \"data\", only the corresponding partition\n" 229 " is updated.\n" 230 "\n" 231 "environmental variables:\n" 232 " ADB_TRACE - Print debug information. A comma separated list of the following values\n" 233 " 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n" 234 " ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.\n" 235 " ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.\n" 236 ); 237} 238 239int usage() 240{ 241 help(); 242 return 1; 243} 244 245#if defined(_WIN32) 246 247// Windows does not have <termio.h>. 248static void stdin_raw_init(int fd) { 249 250} 251 252static void stdin_raw_restore(int fd) { 253 254} 255 256#else 257static struct termios tio_save; 258 259static void stdin_raw_init(int fd) 260{ 261 struct termios tio; 262 263 if(tcgetattr(fd, &tio)) return; 264 if(tcgetattr(fd, &tio_save)) return; 265 266 tio.c_lflag = 0; /* disable CANON, ECHO*, etc */ 267 268 /* no timeout but request at least one character per read */ 269 tio.c_cc[VTIME] = 0; 270 tio.c_cc[VMIN] = 1; 271 272 tcsetattr(fd, TCSANOW, &tio); 273 tcflush(fd, TCIFLUSH); 274} 275 276static void stdin_raw_restore(int fd) 277{ 278 tcsetattr(fd, TCSANOW, &tio_save); 279 tcflush(fd, TCIFLUSH); 280} 281#endif 282 283static void read_and_dump(int fd) 284{ 285 char buf[4096]; 286 int len; 287 288 while(fd >= 0) { 289 D("read_and_dump(): pre adb_read(fd=%d)\n", fd); 290 len = adb_read(fd, buf, 4096); 291 D("read_and_dump(): post adb_read(fd=%d): len=%d\n", fd, len); 292 if(len == 0) { 293 break; 294 } 295 296 if(len < 0) { 297 if(errno == EINTR) continue; 298 break; 299 } 300 fwrite(buf, 1, len, stdout); 301 fflush(stdout); 302 } 303} 304 305static void read_status_line(int fd, char* buf, size_t count) 306{ 307 count--; 308 while (count > 0) { 309 int len = adb_read(fd, buf, count); 310 if (len == 0) { 311 break; 312 } else if (len < 0) { 313 if (errno == EINTR) continue; 314 break; 315 } 316 317 buf += len; 318 count -= len; 319 } 320 *buf = '\0'; 321} 322 323static void copy_to_file(int inFd, int outFd) { 324 const size_t BUFSIZE = 32 * 1024; 325 char* buf = (char*) malloc(BUFSIZE); 326 int len; 327 long total = 0; 328 329 D("copy_to_file(%d -> %d)\n", inFd, outFd); 330 331 if (inFd == STDIN_FILENO) { 332 stdin_raw_init(STDIN_FILENO); 333 } 334 335 for (;;) { 336 if (inFd == STDIN_FILENO) { 337 len = unix_read(inFd, buf, BUFSIZE); 338 } else { 339 len = adb_read(inFd, buf, BUFSIZE); 340 } 341 if (len == 0) { 342 D("copy_to_file() : read 0 bytes; exiting\n"); 343 break; 344 } 345 if (len < 0) { 346 if (errno == EINTR) { 347 D("copy_to_file() : EINTR, retrying\n"); 348 continue; 349 } 350 D("copy_to_file() : error %d\n", errno); 351 break; 352 } 353 if (outFd == STDOUT_FILENO) { 354 fwrite(buf, 1, len, stdout); 355 fflush(stdout); 356 } else { 357 adb_write(outFd, buf, len); 358 } 359 total += len; 360 } 361 362 if (inFd == STDIN_FILENO) { 363 stdin_raw_restore(STDIN_FILENO); 364 } 365 366 D("copy_to_file() finished after %lu bytes\n", total); 367 free(buf); 368} 369 370static void *stdin_read_thread(void *x) 371{ 372 int fd, fdi; 373 unsigned char buf[1024]; 374 int r, n; 375 int state = 0; 376 377 int *fds = (int*) x; 378 fd = fds[0]; 379 fdi = fds[1]; 380 free(fds); 381 382 for(;;) { 383 /* fdi is really the client's stdin, so use read, not adb_read here */ 384 D("stdin_read_thread(): pre unix_read(fdi=%d,...)\n", fdi); 385 r = unix_read(fdi, buf, 1024); 386 D("stdin_read_thread(): post unix_read(fdi=%d,...)\n", fdi); 387 if(r == 0) break; 388 if(r < 0) { 389 if(errno == EINTR) continue; 390 break; 391 } 392 for(n = 0; n < r; n++){ 393 switch(buf[n]) { 394 case '\n': 395 state = 1; 396 break; 397 case '\r': 398 state = 1; 399 break; 400 case '~': 401 if(state == 1) state++; 402 break; 403 case '.': 404 if(state == 2) { 405 fprintf(stderr,"\n* disconnect *\n"); 406 stdin_raw_restore(fdi); 407 exit(0); 408 } 409 default: 410 state = 0; 411 } 412 } 413 r = adb_write(fd, buf, r); 414 if(r <= 0) { 415 break; 416 } 417 } 418 return 0; 419} 420 421int interactive_shell(void) 422{ 423 adb_thread_t thr; 424 int fdi, fd; 425 426 fd = adb_connect("shell:"); 427 if(fd < 0) { 428 fprintf(stderr,"error: %s\n", adb_error()); 429 return 1; 430 } 431 fdi = 0; //dup(0); 432 433 int* fds = reinterpret_cast<int*>(malloc(sizeof(int) * 2)); 434 fds[0] = fd; 435 fds[1] = fdi; 436 437 stdin_raw_init(fdi); 438 adb_thread_create(&thr, stdin_read_thread, fds); 439 read_and_dump(fd); 440 stdin_raw_restore(fdi); 441 return 0; 442} 443 444 445static void format_host_command(char* buffer, size_t buflen, const char* command, transport_type ttype, const char* serial) 446{ 447 if (serial) { 448 snprintf(buffer, buflen, "host-serial:%s:%s", serial, command); 449 } else { 450 const char* prefix = "host"; 451 if (ttype == kTransportUsb) 452 prefix = "host-usb"; 453 else if (ttype == kTransportLocal) 454 prefix = "host-local"; 455 456 snprintf(buffer, buflen, "%s:%s", prefix, command); 457 } 458} 459 460int adb_download_buffer(const char *service, const char *fn, const void* data, int sz, 461 unsigned progress) 462{ 463 char buf[4096]; 464 unsigned total; 465 int fd; 466 467 sprintf(buf,"%s:%d", service, sz); 468 fd = adb_connect(buf); 469 if(fd < 0) { 470 fprintf(stderr,"error: %s\n", adb_error()); 471 return -1; 472 } 473 474 int opt = CHUNK_SIZE; 475 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); 476 477 total = sz; 478 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data); 479 480 if(progress) { 481 char *x = strrchr(service, ':'); 482 if(x) service = x + 1; 483 } 484 485 while(sz > 0) { 486 unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz; 487 if(!WriteFdExactly(fd, ptr, xfer)) { 488 adb_status(fd); 489 fprintf(stderr,"* failed to write data '%s' *\n", adb_error()); 490 return -1; 491 } 492 sz -= xfer; 493 ptr += xfer; 494 if(progress) { 495 printf("sending: '%s' %4d%% \r", fn, (int)(100LL - ((100LL * sz) / (total)))); 496 fflush(stdout); 497 } 498 } 499 if(progress) { 500 printf("\n"); 501 } 502 503 if(!ReadFdExactly(fd, buf, 4)){ 504 fprintf(stderr,"* error reading response *\n"); 505 adb_close(fd); 506 return -1; 507 } 508 if(memcmp(buf, "OKAY", 4)) { 509 buf[4] = 0; 510 fprintf(stderr,"* error response '%s' *\n", buf); 511 adb_close(fd); 512 return -1; 513 } 514 515 adb_close(fd); 516 return 0; 517} 518 519 520int adb_download(const char *service, const char *fn, unsigned progress) 521{ 522 void *data; 523 unsigned sz; 524 525 data = load_file(fn, &sz); 526 if(data == 0) { 527 fprintf(stderr,"* cannot read '%s' *\n", fn); 528 return -1; 529 } 530 531 int status = adb_download_buffer(service, fn, data, sz, progress); 532 free(data); 533 return status; 534} 535 536#define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE) 537 538/* 539 * The sideload-host protocol serves the data in a file (given on the 540 * command line) to the client, using a simple protocol: 541 * 542 * - The connect message includes the total number of bytes in the 543 * file and a block size chosen by us. 544 * 545 * - The other side sends the desired block number as eight decimal 546 * digits (eg "00000023" for block 23). Blocks are numbered from 547 * zero. 548 * 549 * - We send back the data of the requested block. The last block is 550 * likely to be partial; when the last block is requested we only 551 * send the part of the block that exists, it's not padded up to the 552 * block size. 553 * 554 * - When the other side sends "DONEDONE" instead of a block number, 555 * we hang up. 556 */ 557int adb_sideload_host(const char* fn) { 558 unsigned sz; 559 size_t xfer = 0; 560 int status; 561 int last_percent = -1; 562 int opt = SIDELOAD_HOST_BLOCK_SIZE; 563 564 printf("loading: '%s'", fn); 565 fflush(stdout); 566 uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz)); 567 if (data == 0) { 568 printf("\n"); 569 fprintf(stderr, "* cannot read '%s' *\n", fn); 570 return -1; 571 } 572 573 char buf[100]; 574 sprintf(buf, "sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE); 575 int fd = adb_connect(buf); 576 if (fd < 0) { 577 // Try falling back to the older sideload method. Maybe this 578 // is an older device that doesn't support sideload-host. 579 printf("\n"); 580 status = adb_download_buffer("sideload", fn, data, sz, 1); 581 goto done; 582 } 583 584 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); 585 586 for (;;) { 587 if (!ReadFdExactly(fd, buf, 8)) { 588 fprintf(stderr, "* failed to read command: %s\n", adb_error()); 589 status = -1; 590 goto done; 591 } 592 593 if (strncmp("DONEDONE", buf, 8) == 0) { 594 status = 0; 595 break; 596 } 597 598 buf[8] = '\0'; 599 int block = strtol(buf, NULL, 10); 600 601 size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE; 602 if (offset >= sz) { 603 fprintf(stderr, "* attempt to read past end: %s\n", adb_error()); 604 status = -1; 605 goto done; 606 } 607 uint8_t* start = data + offset; 608 size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE; 609 size_t to_write = SIDELOAD_HOST_BLOCK_SIZE; 610 if (offset_end > sz) { 611 to_write = sz - offset; 612 } 613 614 if(!WriteFdExactly(fd, start, to_write)) { 615 adb_status(fd); 616 fprintf(stderr,"* failed to write data '%s' *\n", adb_error()); 617 status = -1; 618 goto done; 619 } 620 xfer += to_write; 621 622 // For normal OTA packages, we expect to transfer every byte 623 // twice, plus a bit of overhead (one read during 624 // verification, one read of each byte for installation, plus 625 // extra access to things like the zip central directory). 626 // This estimate of the completion becomes 100% when we've 627 // transferred ~2.13 (=100/47) times the package size. 628 int percent = (int)(xfer * 47LL / (sz ? sz : 1)); 629 if (percent != last_percent) { 630 printf("\rserving: '%s' (~%d%%) ", fn, percent); 631 fflush(stdout); 632 last_percent = percent; 633 } 634 } 635 636 printf("\rTotal xfer: %.2fx%*s\n", (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, ""); 637 638 done: 639 if (fd >= 0) adb_close(fd); 640 free(data); 641 return status; 642} 643 644static void status_window(transport_type ttype, const char* serial) 645{ 646 char command[4096]; 647 char *state = 0; 648 char *laststate = 0; 649 650 /* silence stderr */ 651#ifdef _WIN32 652 /* XXX: TODO */ 653#else 654 int fd; 655 fd = unix_open("/dev/null", O_WRONLY); 656 dup2(fd, 2); 657 adb_close(fd); 658#endif 659 660 format_host_command(command, sizeof command, "get-state", ttype, serial); 661 662 for(;;) { 663 adb_sleep_ms(250); 664 665 if(state) { 666 free(state); 667 state = 0; 668 } 669 670 state = adb_query(command); 671 672 if(state) { 673 if(laststate && !strcmp(state,laststate)){ 674 continue; 675 } else { 676 if(laststate) free(laststate); 677 laststate = strdup(state); 678 } 679 } 680 681 printf("%c[2J%c[2H", 27, 27); 682 printf("Android Debug Bridge\n"); 683 printf("State: %s\n", state ? state : "offline"); 684 fflush(stdout); 685 } 686} 687 688static int should_escape(const char c) 689{ 690 return (c == ' ' || c == '\'' || c == '"' || c == '\\' || c == '(' || c == ')'); 691} 692 693/* Duplicate and escape given argument. */ 694static char *escape_arg(const char *s) 695{ 696 const char *ts; 697 size_t alloc_len; 698 char *ret; 699 char *dest; 700 701 alloc_len = 0; 702 for (ts = s; *ts != '\0'; ts++) { 703 alloc_len++; 704 if (should_escape(*ts)) { 705 alloc_len++; 706 } 707 } 708 709 if (alloc_len == 0) { 710 // Preserve empty arguments 711 ret = (char *) malloc(3); 712 ret[0] = '\"'; 713 ret[1] = '\"'; 714 ret[2] = '\0'; 715 return ret; 716 } 717 718 ret = (char *) malloc(alloc_len + 1); 719 dest = ret; 720 721 for (ts = s; *ts != '\0'; ts++) { 722 if (should_escape(*ts)) { 723 *dest++ = '\\'; 724 } 725 *dest++ = *ts; 726 } 727 *dest++ = '\0'; 728 729 return ret; 730} 731 732/** 733 * Run ppp in "notty" mode against a resource listed as the first parameter 734 * eg: 735 * 736 * ppp dev:/dev/omap_csmi_tty0 <ppp options> 737 * 738 */ 739int ppp(int argc, const char **argv) 740{ 741#if defined(_WIN32) 742 fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]); 743 return -1; 744#else 745 pid_t pid; 746 int fd; 747 748 if (argc < 2) { 749 fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n", 750 argv[0]); 751 752 return 1; 753 } 754 755 const char* adb_service_name = argv[1]; 756 fd = adb_connect(adb_service_name); 757 758 if(fd < 0) { 759 fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n", 760 adb_service_name, adb_error()); 761 return 1; 762 } 763 764 pid = fork(); 765 766 if (pid < 0) { 767 perror("from fork()"); 768 return 1; 769 } else if (pid == 0) { 770 int err; 771 int i; 772 const char **ppp_args; 773 774 // copy args 775 ppp_args = (const char **) alloca(sizeof(char *) * argc + 1); 776 ppp_args[0] = "pppd"; 777 for (i = 2 ; i < argc ; i++) { 778 //argv[2] and beyond become ppp_args[1] and beyond 779 ppp_args[i - 1] = argv[i]; 780 } 781 ppp_args[i-1] = NULL; 782 783 // child side 784 785 dup2(fd, STDIN_FILENO); 786 dup2(fd, STDOUT_FILENO); 787 adb_close(STDERR_FILENO); 788 adb_close(fd); 789 790 err = execvp("pppd", (char * const *)ppp_args); 791 792 if (err < 0) { 793 perror("execing pppd"); 794 } 795 exit(-1); 796 } else { 797 // parent side 798 799 adb_close(fd); 800 return 0; 801 } 802#endif /* !defined(_WIN32) */ 803} 804 805static int send_shellcommand(transport_type transport, const char* serial, 806 char* buf) 807{ 808 int fd, ret; 809 810 for(;;) { 811 fd = adb_connect(buf); 812 if(fd >= 0) 813 break; 814 fprintf(stderr,"- waiting for device -\n"); 815 adb_sleep_ms(1000); 816 do_cmd(transport, serial, "wait-for-device", 0); 817 } 818 819 read_and_dump(fd); 820 ret = adb_close(fd); 821 if (ret) 822 perror("close"); 823 824 return ret; 825} 826 827static int logcat(transport_type transport, const char* serial, int argc, 828 const char** argv) 829{ 830 char buf[4096]; 831 832 char *log_tags; 833 char *quoted; 834 835 log_tags = getenv("ANDROID_LOG_TAGS"); 836 quoted = escape_arg(log_tags == NULL ? "" : log_tags); 837 snprintf(buf, sizeof(buf), 838 "shell:export ANDROID_LOG_TAGS=\"%s\"; exec logcat", quoted); 839 free(quoted); 840 841 if (!strcmp(argv[0], "longcat")) { 842 strncat(buf, " -v long", sizeof(buf) - 1); 843 } 844 845 argc -= 1; 846 argv += 1; 847 while(argc-- > 0) { 848 quoted = escape_arg(*argv++); 849 strncat(buf, " ", sizeof(buf) - 1); 850 strncat(buf, quoted, sizeof(buf) - 1); 851 free(quoted); 852 } 853 854 send_shellcommand(transport, serial, buf); 855 return 0; 856} 857 858static int mkdirs(const char *path) 859{ 860 int ret; 861 char *x = (char *)path + 1; 862 863 for(;;) { 864 x = adb_dirstart(x); 865 if(x == 0) return 0; 866 *x = 0; 867 ret = adb_mkdir(path, 0775); 868 *x = OS_PATH_SEPARATOR; 869 if((ret < 0) && (errno != EEXIST)) { 870 return ret; 871 } 872 x++; 873 } 874 return 0; 875} 876 877static int backup(int argc, const char** argv) { 878 char buf[4096]; 879 char default_name[32]; 880 const char* filename = strcpy(default_name, "./backup.ab"); 881 int fd, outFd; 882 int i, j; 883 884 /* find, extract, and use any -f argument */ 885 for (i = 1; i < argc; i++) { 886 if (!strcmp("-f", argv[i])) { 887 if (i == argc-1) { 888 fprintf(stderr, "adb: -f passed with no filename\n"); 889 return usage(); 890 } 891 filename = argv[i+1]; 892 for (j = i+2; j <= argc; ) { 893 argv[i++] = argv[j++]; 894 } 895 argc -= 2; 896 argv[argc] = NULL; 897 } 898 } 899 900 /* bare "adb backup" or "adb backup -f filename" are not valid invocations */ 901 if (argc < 2) return usage(); 902 903 adb_unlink(filename); 904 mkdirs(filename); 905 outFd = adb_creat(filename, 0640); 906 if (outFd < 0) { 907 fprintf(stderr, "adb: unable to open file %s\n", filename); 908 return -1; 909 } 910 911 snprintf(buf, sizeof(buf), "backup"); 912 for (argc--, argv++; argc; argc--, argv++) { 913 strncat(buf, ":", sizeof(buf) - strlen(buf) - 1); 914 strncat(buf, argv[0], sizeof(buf) - strlen(buf) - 1); 915 } 916 917 D("backup. filename=%s buf=%s\n", filename, buf); 918 fd = adb_connect(buf); 919 if (fd < 0) { 920 fprintf(stderr, "adb: unable to connect for backup\n"); 921 adb_close(outFd); 922 return -1; 923 } 924 925 printf("Now unlock your device and confirm the backup operation.\n"); 926 copy_to_file(fd, outFd); 927 928 adb_close(fd); 929 adb_close(outFd); 930 return 0; 931} 932 933static int restore(int argc, const char** argv) { 934 const char* filename; 935 int fd, tarFd; 936 937 if (argc != 2) return usage(); 938 939 filename = argv[1]; 940 tarFd = adb_open(filename, O_RDONLY); 941 if (tarFd < 0) { 942 fprintf(stderr, "adb: unable to open file %s\n", filename); 943 return -1; 944 } 945 946 fd = adb_connect("restore:"); 947 if (fd < 0) { 948 fprintf(stderr, "adb: unable to connect for restore\n"); 949 adb_close(tarFd); 950 return -1; 951 } 952 953 printf("Now unlock your device and confirm the restore operation.\n"); 954 copy_to_file(tarFd, fd); 955 956 adb_close(fd); 957 adb_close(tarFd); 958 return 0; 959} 960 961#define SENTINEL_FILE "config" OS_PATH_SEPARATOR_STR "envsetup.make" 962static int top_works(const char *top) 963{ 964 if (top != NULL && adb_is_absolute_host_path(top)) { 965 char path_buf[PATH_MAX]; 966 snprintf(path_buf, sizeof(path_buf), 967 "%s" OS_PATH_SEPARATOR_STR SENTINEL_FILE, top); 968 return access(path_buf, F_OK) == 0; 969 } 970 return 0; 971} 972 973static char *find_top_from(const char *indir, char path_buf[PATH_MAX]) 974{ 975 strcpy(path_buf, indir); 976 while (1) { 977 if (top_works(path_buf)) { 978 return path_buf; 979 } 980 char *s = adb_dirstop(path_buf); 981 if (s != NULL) { 982 *s = '\0'; 983 } else { 984 path_buf[0] = '\0'; 985 return NULL; 986 } 987 } 988} 989 990static char *find_top(char path_buf[PATH_MAX]) 991{ 992 char *top = getenv("ANDROID_BUILD_TOP"); 993 if (top != NULL && top[0] != '\0') { 994 if (!top_works(top)) { 995 fprintf(stderr, "adb: bad ANDROID_BUILD_TOP value \"%s\"\n", top); 996 return NULL; 997 } 998 } else { 999 top = getenv("TOP"); 1000 if (top != NULL && top[0] != '\0') { 1001 if (!top_works(top)) { 1002 fprintf(stderr, "adb: bad TOP value \"%s\"\n", top); 1003 return NULL; 1004 } 1005 } else { 1006 top = NULL; 1007 } 1008 } 1009 1010 if (top != NULL) { 1011 /* The environment pointed to a top directory that works. 1012 */ 1013 strcpy(path_buf, top); 1014 return path_buf; 1015 } 1016 1017 /* The environment didn't help. Walk up the tree from the CWD 1018 * to see if we can find the top. 1019 */ 1020 char dir[PATH_MAX]; 1021 top = find_top_from(getcwd(dir, sizeof(dir)), path_buf); 1022 if (top == NULL) { 1023 /* If the CWD isn't under a good-looking top, see if the 1024 * executable is. 1025 */ 1026 get_my_path(dir, PATH_MAX); 1027 top = find_top_from(dir, path_buf); 1028 } 1029 return top; 1030} 1031 1032/* <hint> may be: 1033 * - A simple product name 1034 * e.g., "sooner" 1035TODO: debug? sooner-debug, sooner:debug? 1036 * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir 1037 * e.g., "out/target/product/sooner" 1038 * - An absolute path to the PRODUCT_OUT dir 1039 * e.g., "/src/device/out/target/product/sooner" 1040 * 1041 * Given <hint>, try to construct an absolute path to the 1042 * ANDROID_PRODUCT_OUT dir. 1043 */ 1044static const char *find_product_out_path(const char *hint) 1045{ 1046 static char path_buf[PATH_MAX]; 1047 1048 if (hint == NULL || hint[0] == '\0') { 1049 return NULL; 1050 } 1051 1052 /* If it's already absolute, don't bother doing any work. 1053 */ 1054 if (adb_is_absolute_host_path(hint)) { 1055 strcpy(path_buf, hint); 1056 return path_buf; 1057 } 1058 1059 /* If there are any slashes in it, assume it's a relative path; 1060 * make it absolute. 1061 */ 1062 if (adb_dirstart(hint) != NULL) { 1063 if (getcwd(path_buf, sizeof(path_buf)) == NULL) { 1064 fprintf(stderr, "adb: Couldn't get CWD: %s\n", strerror(errno)); 1065 return NULL; 1066 } 1067 if (strlen(path_buf) + 1 + strlen(hint) >= sizeof(path_buf)) { 1068 fprintf(stderr, "adb: Couldn't assemble path\n"); 1069 return NULL; 1070 } 1071 strcat(path_buf, OS_PATH_SEPARATOR_STR); 1072 strcat(path_buf, hint); 1073 return path_buf; 1074 } 1075 1076 /* It's a string without any slashes. Try to do something with it. 1077 * 1078 * Try to find the root of the build tree, and build a PRODUCT_OUT 1079 * path from there. 1080 */ 1081 char top_buf[PATH_MAX]; 1082 const char *top = find_top(top_buf); 1083 if (top == NULL) { 1084 fprintf(stderr, "adb: Couldn't find top of build tree\n"); 1085 return NULL; 1086 } 1087//TODO: if we have a way to indicate debug, look in out/debug/target/... 1088 snprintf(path_buf, sizeof(path_buf), 1089 "%s" OS_PATH_SEPARATOR_STR 1090 "out" OS_PATH_SEPARATOR_STR 1091 "target" OS_PATH_SEPARATOR_STR 1092 "product" OS_PATH_SEPARATOR_STR 1093 "%s", top_buf, hint); 1094 if (access(path_buf, F_OK) < 0) { 1095 fprintf(stderr, "adb: Couldn't find a product dir " 1096 "based on \"-p %s\"; \"%s\" doesn't exist\n", hint, path_buf); 1097 return NULL; 1098 } 1099 return path_buf; 1100} 1101 1102static void parse_push_pull_args(const char **arg, int narg, char const **path1, 1103 char const **path2, int *show_progress, 1104 int *copy_attrs) { 1105 *show_progress = 0; 1106 *copy_attrs = 0; 1107 1108 while (narg > 0) { 1109 if (!strcmp(*arg, "-p")) { 1110 *show_progress = 1; 1111 } else if (!strcmp(*arg, "-a")) { 1112 *copy_attrs = 1; 1113 } else { 1114 break; 1115 } 1116 ++arg; 1117 --narg; 1118 } 1119 1120 if (narg > 0) { 1121 *path1 = *arg; 1122 ++arg; 1123 --narg; 1124 } 1125 1126 if (narg > 0) { 1127 *path2 = *arg; 1128 } 1129} 1130 1131int adb_commandline(int argc, const char **argv) 1132{ 1133 char buf[4096]; 1134 int no_daemon = 0; 1135 int is_daemon = 0; 1136 int is_server = 0; 1137 int persist = 0; 1138 int r; 1139 transport_type ttype = kTransportAny; 1140 const char* serial = NULL; 1141 const char* server_port_str = NULL; 1142 1143 /* If defined, this should be an absolute path to 1144 * the directory containing all of the various system images 1145 * for a particular product. If not defined, and the adb 1146 * command requires this information, then the user must 1147 * specify the path using "-p". 1148 */ 1149 gProductOutPath = getenv("ANDROID_PRODUCT_OUT"); 1150 if (gProductOutPath == NULL || gProductOutPath[0] == '\0') { 1151 gProductOutPath = NULL; 1152 } 1153 // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint 1154 1155 serial = getenv("ANDROID_SERIAL"); 1156 1157 /* Validate and assign the server port */ 1158 server_port_str = getenv("ANDROID_ADB_SERVER_PORT"); 1159 int server_port = DEFAULT_ADB_PORT; 1160 if (server_port_str && strlen(server_port_str) > 0) { 1161 server_port = (int) strtol(server_port_str, NULL, 0); 1162 if (server_port <= 0 || server_port > 65535) { 1163 fprintf(stderr, 1164 "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number less than 65535. Got \"%s\"\n", 1165 server_port_str); 1166 return usage(); 1167 } 1168 } 1169 1170 /* modifiers and flags */ 1171 while (argc > 0) { 1172 if (!strcmp(argv[0],"server")) { 1173 is_server = 1; 1174 } else if (!strcmp(argv[0],"nodaemon")) { 1175 no_daemon = 1; 1176 } else if (!strcmp(argv[0], "fork-server")) { 1177 /* this is a special flag used only when the ADB client launches the ADB Server */ 1178 is_daemon = 1; 1179 } else if (!strcmp(argv[0],"persist")) { 1180 persist = 1; 1181 } else if (!strncmp(argv[0], "-p", 2)) { 1182 const char *product = NULL; 1183 if (argv[0][2] == '\0') { 1184 if (argc < 2) return usage(); 1185 product = argv[1]; 1186 argc--; 1187 argv++; 1188 } else { 1189 product = argv[0] + 2; 1190 } 1191 gProductOutPath = find_product_out_path(product); 1192 if (gProductOutPath == NULL) { 1193 fprintf(stderr, "adb: could not resolve \"-p %s\"\n", 1194 product); 1195 return usage(); 1196 } 1197 } else if (argv[0][0]=='-' && argv[0][1]=='s') { 1198 if (isdigit(argv[0][2])) { 1199 serial = argv[0] + 2; 1200 } else { 1201 if (argc < 2 || argv[0][2] != '\0') return usage(); 1202 serial = argv[1]; 1203 argc--; 1204 argv++; 1205 } 1206 } else if (!strcmp(argv[0],"-d")) { 1207 ttype = kTransportUsb; 1208 } else if (!strcmp(argv[0],"-e")) { 1209 ttype = kTransportLocal; 1210 } else if (!strcmp(argv[0],"-a")) { 1211 gListenAll = 1; 1212 } else if (!strncmp(argv[0], "-H", 2)) { 1213 const char *hostname = NULL; 1214 if (argv[0][2] == '\0') { 1215 if (argc < 2) return usage(); 1216 hostname = argv[1]; 1217 argc--; 1218 argv++; 1219 } else { 1220 hostname = argv[0] + 2; 1221 } 1222 adb_set_tcp_name(hostname); 1223 1224 } else if (!strncmp(argv[0], "-P", 2)) { 1225 if (argv[0][2] == '\0') { 1226 if (argc < 2) return usage(); 1227 server_port_str = argv[1]; 1228 argc--; 1229 argv++; 1230 } else { 1231 server_port_str = argv[0] + 2; 1232 } 1233 if (strlen(server_port_str) > 0) { 1234 server_port = (int) strtol(server_port_str, NULL, 0); 1235 if (server_port <= 0 || server_port > 65535) { 1236 fprintf(stderr, 1237 "adb: port number must be a positive number less than 65536. Got \"%s\"\n", 1238 server_port_str); 1239 return usage(); 1240 } 1241 } else { 1242 fprintf(stderr, 1243 "adb: port number must be a positive number less than 65536. Got empty string.\n"); 1244 return usage(); 1245 } 1246 } else { 1247 /* out of recognized modifiers and flags */ 1248 break; 1249 } 1250 argc--; 1251 argv++; 1252 } 1253 1254 adb_set_transport(ttype, serial); 1255 adb_set_tcp_specifics(server_port); 1256 1257 if (is_server) { 1258 if (no_daemon || is_daemon) { 1259 r = adb_main(is_daemon, server_port); 1260 } else { 1261 r = launch_server(server_port); 1262 } 1263 if (r) { 1264 fprintf(stderr,"* could not start server *\n"); 1265 } 1266 return r; 1267 } 1268 1269 if (argc == 0) { 1270 return usage(); 1271 } 1272 1273 /* handle wait-for-* prefix */ 1274 if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) { 1275 const char* service = argv[0]; 1276 if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) { 1277 if (ttype == kTransportUsb) { 1278 service = "wait-for-usb"; 1279 } else if (ttype == kTransportLocal) { 1280 service = "wait-for-local"; 1281 } else { 1282 service = "wait-for-any"; 1283 } 1284 } 1285 1286 format_host_command(buf, sizeof buf, service, ttype, serial); 1287 1288 if (adb_command(buf)) { 1289 D("failure: %s *\n",adb_error()); 1290 fprintf(stderr,"error: %s\n", adb_error()); 1291 return 1; 1292 } 1293 1294 /* Allow a command to be run after wait-for-device, 1295 * e.g. 'adb wait-for-device shell'. 1296 */ 1297 if (argc == 1) { 1298 return 0; 1299 } 1300 1301 /* Fall through */ 1302 argc--; 1303 argv++; 1304 } 1305 1306 /* adb_connect() commands */ 1307 if (!strcmp(argv[0], "devices")) { 1308 char *tmp; 1309 const char *listopt; 1310 if (argc < 2) 1311 listopt = ""; 1312 else if (argc == 2 && !strcmp(argv[1], "-l")) 1313 listopt = argv[1]; 1314 else { 1315 fprintf(stderr, "Usage: adb devices [-l]\n"); 1316 return 1; 1317 } 1318 snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt); 1319 tmp = adb_query(buf); 1320 if (tmp) { 1321 printf("List of devices attached \n"); 1322 printf("%s\n", tmp); 1323 return 0; 1324 } else { 1325 return 1; 1326 } 1327 } 1328 else if (!strcmp(argv[0], "connect")) { 1329 char *tmp; 1330 if (argc != 2) { 1331 fprintf(stderr, "Usage: adb connect <host>[:<port>]\n"); 1332 return 1; 1333 } 1334 snprintf(buf, sizeof buf, "host:connect:%s", argv[1]); 1335 tmp = adb_query(buf); 1336 if (tmp) { 1337 printf("%s\n", tmp); 1338 return 0; 1339 } else { 1340 return 1; 1341 } 1342 } 1343 else if (!strcmp(argv[0], "disconnect")) { 1344 char *tmp; 1345 if (argc > 2) { 1346 fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n"); 1347 return 1; 1348 } 1349 if (argc == 2) { 1350 snprintf(buf, sizeof buf, "host:disconnect:%s", argv[1]); 1351 } else { 1352 snprintf(buf, sizeof buf, "host:disconnect:"); 1353 } 1354 tmp = adb_query(buf); 1355 if (tmp) { 1356 printf("%s\n", tmp); 1357 return 0; 1358 } else { 1359 return 1; 1360 } 1361 } 1362 else if (!strcmp(argv[0], "emu")) { 1363 return adb_send_emulator_command(argc, argv); 1364 } 1365 else if (!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) { 1366 int r; 1367 int fd; 1368 1369 char h = (argv[0][0] == 'h'); 1370 1371 if (h) { 1372 printf("\x1b[41;33m"); 1373 fflush(stdout); 1374 } 1375 1376 if (argc < 2) { 1377 D("starting interactive shell\n"); 1378 r = interactive_shell(); 1379 if (h) { 1380 printf("\x1b[0m"); 1381 fflush(stdout); 1382 } 1383 return r; 1384 } 1385 1386 snprintf(buf, sizeof(buf), "shell:%s", argv[1]); 1387 argc -= 2; 1388 argv += 2; 1389 while (argc-- > 0) { 1390 char *quoted = escape_arg(*argv++); 1391 strncat(buf, " ", sizeof(buf) - 1); 1392 strncat(buf, quoted, sizeof(buf) - 1); 1393 free(quoted); 1394 } 1395 1396 for(;;) { 1397 D("interactive shell loop. buff=%s\n", buf); 1398 fd = adb_connect(buf); 1399 if (fd >= 0) { 1400 D("about to read_and_dump(fd=%d)\n", fd); 1401 read_and_dump(fd); 1402 D("read_and_dump() done.\n"); 1403 adb_close(fd); 1404 r = 0; 1405 } else { 1406 fprintf(stderr,"error: %s\n", adb_error()); 1407 r = -1; 1408 } 1409 1410 if (persist) { 1411 fprintf(stderr,"\n- waiting for device -\n"); 1412 adb_sleep_ms(1000); 1413 do_cmd(ttype, serial, "wait-for-device", 0); 1414 } else { 1415 if (h) { 1416 printf("\x1b[0m"); 1417 fflush(stdout); 1418 } 1419 D("interactive shell loop. return r=%d\n", r); 1420 return r; 1421 } 1422 } 1423 } 1424 else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) { 1425 int exec_in = !strcmp(argv[0], "exec-in"); 1426 int fd; 1427 1428 snprintf(buf, sizeof buf, "exec:%s", argv[1]); 1429 argc -= 2; 1430 argv += 2; 1431 while (argc-- > 0) { 1432 char *quoted = escape_arg(*argv++); 1433 strncat(buf, " ", sizeof(buf) - 1); 1434 strncat(buf, quoted, sizeof(buf) - 1); 1435 free(quoted); 1436 } 1437 1438 fd = adb_connect(buf); 1439 if (fd < 0) { 1440 fprintf(stderr, "error: %s\n", adb_error()); 1441 return -1; 1442 } 1443 1444 if (exec_in) { 1445 copy_to_file(STDIN_FILENO, fd); 1446 } else { 1447 copy_to_file(fd, STDOUT_FILENO); 1448 } 1449 1450 adb_close(fd); 1451 return 0; 1452 } 1453 else if (!strcmp(argv[0], "kill-server")) { 1454 int fd; 1455 fd = _adb_connect("host:kill"); 1456 if (fd == -1) { 1457 fprintf(stderr,"* server not running *\n"); 1458 return 1; 1459 } 1460 return 0; 1461 } 1462 else if (!strcmp(argv[0], "sideload")) { 1463 if (argc != 2) return usage(); 1464 if (adb_sideload_host(argv[1])) { 1465 return 1; 1466 } else { 1467 return 0; 1468 } 1469 } 1470 else if (!strcmp(argv[0], "remount") || 1471 !strcmp(argv[0], "reboot") || 1472 !strcmp(argv[0], "reboot-bootloader") || 1473 !strcmp(argv[0], "tcpip") || 1474 !strcmp(argv[0], "usb") || 1475 !strcmp(argv[0], "root") || 1476 !strcmp(argv[0], "unroot") || 1477 !strcmp(argv[0], "disable-verity") || 1478 !strcmp(argv[0], "enable-verity")) { 1479 char command[100]; 1480 if (!strcmp(argv[0], "reboot-bootloader")) 1481 snprintf(command, sizeof(command), "reboot:bootloader"); 1482 else if (argc > 1) 1483 snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]); 1484 else 1485 snprintf(command, sizeof(command), "%s:", argv[0]); 1486 int fd = adb_connect(command); 1487 if (fd >= 0) { 1488 read_and_dump(fd); 1489 adb_close(fd); 1490 return 0; 1491 } 1492 fprintf(stderr,"error: %s\n", adb_error()); 1493 return 1; 1494 } 1495 else if (!strcmp(argv[0], "bugreport")) { 1496 if (argc != 1) return usage(); 1497 do_cmd(ttype, serial, "shell", "bugreport", 0); 1498 return 0; 1499 } 1500 /* adb_command() wrapper commands */ 1501 else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) { 1502 char host_prefix[64]; 1503 char reverse = (char) !strcmp(argv[0], "reverse"); 1504 char remove = 0; 1505 char remove_all = 0; 1506 char list = 0; 1507 char no_rebind = 0; 1508 1509 // Parse options here. 1510 while (argc > 1 && argv[1][0] == '-') { 1511 if (!strcmp(argv[1], "--list")) 1512 list = 1; 1513 else if (!strcmp(argv[1], "--remove")) 1514 remove = 1; 1515 else if (!strcmp(argv[1], "--remove-all")) 1516 remove_all = 1; 1517 else if (!strcmp(argv[1], "--no-rebind")) 1518 no_rebind = 1; 1519 else { 1520 return usage(); 1521 } 1522 argc--; 1523 argv++; 1524 } 1525 1526 // Ensure we can only use one option at a time. 1527 if (list + remove + remove_all + no_rebind > 1) { 1528 return usage(); 1529 } 1530 1531 // Determine the <host-prefix> for this command. 1532 if (reverse) { 1533 snprintf(host_prefix, sizeof host_prefix, "reverse"); 1534 } else { 1535 if (serial) { 1536 snprintf(host_prefix, sizeof host_prefix, "host-serial:%s", 1537 serial); 1538 } else if (ttype == kTransportUsb) { 1539 snprintf(host_prefix, sizeof host_prefix, "host-usb"); 1540 } else if (ttype == kTransportLocal) { 1541 snprintf(host_prefix, sizeof host_prefix, "host-local"); 1542 } else { 1543 snprintf(host_prefix, sizeof host_prefix, "host"); 1544 } 1545 } 1546 1547 // Implement forward --list 1548 if (list) { 1549 if (argc != 1) 1550 return usage(); 1551 snprintf(buf, sizeof buf, "%s:list-forward", host_prefix); 1552 char* forwards = adb_query(buf); 1553 if (forwards == NULL) { 1554 fprintf(stderr, "error: %s\n", adb_error()); 1555 return 1; 1556 } 1557 printf("%s", forwards); 1558 free(forwards); 1559 return 0; 1560 } 1561 1562 // Implement forward --remove-all 1563 else if (remove_all) { 1564 if (argc != 1) 1565 return usage(); 1566 snprintf(buf, sizeof buf, "%s:killforward-all", host_prefix); 1567 } 1568 1569 // Implement forward --remove <local> 1570 else if (remove) { 1571 if (argc != 2) 1572 return usage(); 1573 snprintf(buf, sizeof buf, "%s:killforward:%s", host_prefix, argv[1]); 1574 } 1575 // Or implement one of: 1576 // forward <local> <remote> 1577 // forward --no-rebind <local> <remote> 1578 else 1579 { 1580 if (argc != 3) 1581 return usage(); 1582 const char* command = no_rebind ? "forward:norebind" : "forward"; 1583 snprintf(buf, sizeof buf, "%s:%s:%s;%s", host_prefix, command, argv[1], argv[2]); 1584 } 1585 1586 if (adb_command(buf)) { 1587 fprintf(stderr,"error: %s\n", adb_error()); 1588 return 1; 1589 } 1590 return 0; 1591 } 1592 /* do_sync_*() commands */ 1593 else if (!strcmp(argv[0], "ls")) { 1594 if (argc != 2) return usage(); 1595 return do_sync_ls(argv[1]); 1596 } 1597 else if (!strcmp(argv[0], "push")) { 1598 int show_progress = 0; 1599 int copy_attrs = 0; // unused 1600 const char* lpath = NULL, *rpath = NULL; 1601 1602 parse_push_pull_args(&argv[1], argc - 1, &lpath, &rpath, &show_progress, ©_attrs); 1603 1604 if ((lpath != NULL) && (rpath != NULL)) { 1605 return do_sync_push(lpath, rpath, show_progress); 1606 } 1607 1608 return usage(); 1609 } 1610 else if (!strcmp(argv[0], "pull")) { 1611 int show_progress = 0; 1612 int copy_attrs = 0; 1613 const char* rpath = NULL, *lpath = "."; 1614 1615 parse_push_pull_args(&argv[1], argc - 1, &rpath, &lpath, &show_progress, ©_attrs); 1616 1617 if (rpath != NULL) { 1618 return do_sync_pull(rpath, lpath, show_progress, copy_attrs); 1619 } 1620 1621 return usage(); 1622 } 1623 else if (!strcmp(argv[0], "install")) { 1624 if (argc < 2) return usage(); 1625 return install_app(ttype, serial, argc, argv); 1626 } 1627 else if (!strcmp(argv[0], "install-multiple")) { 1628 if (argc < 2) return usage(); 1629 return install_multiple_app(ttype, serial, argc, argv); 1630 } 1631 else if (!strcmp(argv[0], "uninstall")) { 1632 if (argc < 2) return usage(); 1633 return uninstall_app(ttype, serial, argc, argv); 1634 } 1635 else if (!strcmp(argv[0], "sync")) { 1636 const char* srcarg; 1637 char *system_srcpath, *data_srcpath, *vendor_srcpath, *oem_srcpath; 1638 1639 int listonly = 0; 1640 1641 int ret; 1642 if (argc < 2) { 1643 /* No local path was specified. */ 1644 srcarg = NULL; 1645 } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) { 1646 listonly = 1; 1647 if (argc == 3) { 1648 srcarg = argv[2]; 1649 } else { 1650 srcarg = NULL; 1651 } 1652 } else if (argc == 2) { 1653 /* A local path or "android"/"data" arg was specified. */ 1654 srcarg = argv[1]; 1655 } else { 1656 return usage(); 1657 } 1658 ret = find_sync_dirs(srcarg, &system_srcpath, &data_srcpath, &vendor_srcpath, 1659 &oem_srcpath); 1660 if (ret != 0) return usage(); 1661 1662 if (system_srcpath != NULL) 1663 ret = do_sync_sync(system_srcpath, "/system", listonly); 1664 if (ret == 0 && vendor_srcpath != NULL) 1665 ret = do_sync_sync(vendor_srcpath, "/vendor", listonly); 1666 if(ret == 0 && oem_srcpath != NULL) 1667 ret = do_sync_sync(oem_srcpath, "/oem", listonly); 1668 if (ret == 0 && data_srcpath != NULL) 1669 ret = do_sync_sync(data_srcpath, "/data", listonly); 1670 1671 free(system_srcpath); 1672 free(vendor_srcpath); 1673 free(oem_srcpath); 1674 free(data_srcpath); 1675 return ret; 1676 } 1677 /* passthrough commands */ 1678 else if (!strcmp(argv[0],"get-state") || 1679 !strcmp(argv[0],"get-serialno") || 1680 !strcmp(argv[0],"get-devpath")) 1681 { 1682 char *tmp; 1683 1684 format_host_command(buf, sizeof buf, argv[0], ttype, serial); 1685 tmp = adb_query(buf); 1686 if (tmp) { 1687 printf("%s\n", tmp); 1688 return 0; 1689 } else { 1690 return 1; 1691 } 1692 } 1693 /* other commands */ 1694 else if (!strcmp(argv[0],"status-window")) { 1695 status_window(ttype, serial); 1696 return 0; 1697 } 1698 else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) { 1699 return logcat(ttype, serial, argc, argv); 1700 } 1701 else if (!strcmp(argv[0],"ppp")) { 1702 return ppp(argc, argv); 1703 } 1704 else if (!strcmp(argv[0], "start-server")) { 1705 return adb_connect("host:start-server"); 1706 } 1707 else if (!strcmp(argv[0], "backup")) { 1708 return backup(argc, argv); 1709 } 1710 else if (!strcmp(argv[0], "restore")) { 1711 return restore(argc, argv); 1712 } 1713 else if (!strcmp(argv[0], "keygen")) { 1714 if (argc < 2) return usage(); 1715 return adb_auth_keygen(argv[1]); 1716 } 1717 else if (!strcmp(argv[0], "jdwp")) { 1718 int fd = adb_connect("jdwp"); 1719 if (fd >= 0) { 1720 read_and_dump(fd); 1721 adb_close(fd); 1722 return 0; 1723 } else { 1724 fprintf(stderr, "error: %s\n", adb_error()); 1725 return -1; 1726 } 1727 } 1728 /* "adb /?" is a common idiom under Windows */ 1729 else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) { 1730 help(); 1731 return 0; 1732 } 1733 else if (!strcmp(argv[0], "version")) { 1734 version(stdout); 1735 return 0; 1736 } 1737 1738 usage(); 1739 return 1; 1740} 1741 1742#define MAX_ARGV_LENGTH 16 1743static int do_cmd(transport_type ttype, const char* serial, const char *cmd, ...) 1744{ 1745 const char *argv[MAX_ARGV_LENGTH]; 1746 int argc; 1747 va_list ap; 1748 1749 va_start(ap, cmd); 1750 argc = 0; 1751 1752 if (serial) { 1753 argv[argc++] = "-s"; 1754 argv[argc++] = serial; 1755 } else if (ttype == kTransportUsb) { 1756 argv[argc++] = "-d"; 1757 } else if (ttype == kTransportLocal) { 1758 argv[argc++] = "-e"; 1759 } 1760 1761 argv[argc++] = cmd; 1762 while(argc < MAX_ARGV_LENGTH && 1763 (argv[argc] = va_arg(ap, char*)) != 0) argc++; 1764 assert(argc < MAX_ARGV_LENGTH); 1765 va_end(ap); 1766 1767#if 0 1768 int n; 1769 fprintf(stderr,"argc = %d\n",argc); 1770 for(n = 0; n < argc; n++) { 1771 fprintf(stderr,"argv[%d] = \"%s\"\n", n, argv[n]); 1772 } 1773#endif 1774 1775 return adb_commandline(argc, argv); 1776} 1777 1778int find_sync_dirs(const char *srcarg, 1779 char **system_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out, 1780 char **oem_srcdir_out) 1781{ 1782 char *system_srcdir = NULL, *data_srcdir = NULL, *vendor_srcdir = NULL, *oem_srcdir = NULL; 1783 struct stat st; 1784 1785 if(srcarg == NULL) { 1786 system_srcdir = product_file("system"); 1787 data_srcdir = product_file("data"); 1788 vendor_srcdir = product_file("vendor"); 1789 oem_srcdir = product_file("oem"); 1790 // Check if vendor partition exists. 1791 if (lstat(vendor_srcdir, &st) || !S_ISDIR(st.st_mode)) 1792 vendor_srcdir = NULL; 1793 // Check if oem partition exists. 1794 if (lstat(oem_srcdir, &st) || !S_ISDIR(st.st_mode)) 1795 oem_srcdir = NULL; 1796 } else { 1797 // srcarg may be "data", "system", "vendor", "oem" or NULL. 1798 // If srcarg is NULL, then all partitions are synced. 1799 if(strcmp(srcarg, "system") == 0) { 1800 system_srcdir = product_file("system"); 1801 } else if(strcmp(srcarg, "data") == 0) { 1802 data_srcdir = product_file("data"); 1803 } else if(strcmp(srcarg, "vendor") == 0) { 1804 vendor_srcdir = product_file("vendor"); 1805 } else if(strcmp(srcarg, "oem") == 0) { 1806 oem_srcdir = product_file("oem"); 1807 } else { 1808 // It's not "system", "data", "vendor", or "oem". 1809 return 1; 1810 } 1811 } 1812 1813 if(system_srcdir_out != NULL) 1814 *system_srcdir_out = system_srcdir; 1815 else 1816 free(system_srcdir); 1817 1818 if(vendor_srcdir_out != NULL) 1819 *vendor_srcdir_out = vendor_srcdir; 1820 else 1821 free(vendor_srcdir); 1822 1823 if(oem_srcdir_out != NULL) 1824 *oem_srcdir_out = oem_srcdir; 1825 else 1826 free(oem_srcdir); 1827 1828 if(data_srcdir_out != NULL) 1829 *data_srcdir_out = data_srcdir; 1830 else 1831 free(data_srcdir); 1832 1833 return 0; 1834} 1835 1836static int pm_command(transport_type transport, const char* serial, 1837 int argc, const char** argv) 1838{ 1839 char buf[4096]; 1840 1841 snprintf(buf, sizeof(buf), "shell:pm"); 1842 1843 while(argc-- > 0) { 1844 char *quoted = escape_arg(*argv++); 1845 strncat(buf, " ", sizeof(buf) - 1); 1846 strncat(buf, quoted, sizeof(buf) - 1); 1847 free(quoted); 1848 } 1849 1850 send_shellcommand(transport, serial, buf); 1851 return 0; 1852} 1853 1854int uninstall_app(transport_type transport, const char* serial, int argc, 1855 const char** argv) 1856{ 1857 /* if the user choose the -k option, we refuse to do it until devices are 1858 out with the option to uninstall the remaining data somehow (adb/ui) */ 1859 if (argc == 3 && strcmp(argv[1], "-k") == 0) 1860 { 1861 printf( 1862 "The -k option uninstalls the application while retaining the data/cache.\n" 1863 "At the moment, there is no way to remove the remaining data.\n" 1864 "You will have to reinstall the application with the same signature, and fully uninstall it.\n" 1865 "If you truly wish to continue, execute 'adb shell pm uninstall -k %s'\n", argv[2]); 1866 return -1; 1867 } 1868 1869 /* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */ 1870 return pm_command(transport, serial, argc, argv); 1871} 1872 1873static int delete_file(transport_type transport, const char* serial, char* filename) 1874{ 1875 char buf[4096]; 1876 char* quoted; 1877 1878 snprintf(buf, sizeof(buf), "shell:rm -f "); 1879 quoted = escape_arg(filename); 1880 strncat(buf, quoted, sizeof(buf)-1); 1881 free(quoted); 1882 1883 send_shellcommand(transport, serial, buf); 1884 return 0; 1885} 1886 1887static const char* get_basename(const char* filename) 1888{ 1889 const char* basename = adb_dirstop(filename); 1890 if (basename) { 1891 basename++; 1892 return basename; 1893 } else { 1894 return filename; 1895 } 1896} 1897 1898int install_app(transport_type transport, const char* serial, int argc, 1899 const char** argv) 1900{ 1901 static const char *const DATA_DEST = "/data/local/tmp/%s"; 1902 static const char *const SD_DEST = "/sdcard/tmp/%s"; 1903 const char* where = DATA_DEST; 1904 int i; 1905 struct stat sb; 1906 1907 for (i = 1; i < argc; i++) { 1908 if (!strcmp(argv[i], "-s")) { 1909 where = SD_DEST; 1910 } 1911 } 1912 1913 // Find last APK argument. 1914 // All other arguments passed through verbatim. 1915 int last_apk = -1; 1916 for (i = argc - 1; i >= 0; i--) { 1917 const char* file = argv[i]; 1918 char* dot = strrchr(file, '.'); 1919 if (dot && !strcasecmp(dot, ".apk")) { 1920 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) { 1921 fprintf(stderr, "Invalid APK file: %s\n", file); 1922 return -1; 1923 } 1924 1925 last_apk = i; 1926 break; 1927 } 1928 } 1929 1930 if (last_apk == -1) { 1931 fprintf(stderr, "Missing APK file\n"); 1932 return -1; 1933 } 1934 1935 const char* apk_file = argv[last_apk]; 1936 char apk_dest[PATH_MAX]; 1937 snprintf(apk_dest, sizeof apk_dest, where, get_basename(apk_file)); 1938 int err = do_sync_push(apk_file, apk_dest, 0 /* no show progress */); 1939 if (err) { 1940 goto cleanup_apk; 1941 } else { 1942 argv[last_apk] = apk_dest; /* destination name, not source location */ 1943 } 1944 1945 pm_command(transport, serial, argc, argv); 1946 1947cleanup_apk: 1948 delete_file(transport, serial, apk_dest); 1949 return err; 1950} 1951 1952int install_multiple_app(transport_type transport, const char* serial, int argc, 1953 const char** argv) 1954{ 1955 char buf[1024]; 1956 int i; 1957 struct stat sb; 1958 unsigned long long total_size = 0; 1959 1960 // Find all APK arguments starting at end. 1961 // All other arguments passed through verbatim. 1962 int first_apk = -1; 1963 for (i = argc - 1; i >= 0; i--) { 1964 const char* file = argv[i]; 1965 char* dot = strrchr(file, '.'); 1966 if (dot && !strcasecmp(dot, ".apk")) { 1967 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) { 1968 fprintf(stderr, "Invalid APK file: %s\n", file); 1969 return -1; 1970 } 1971 1972 total_size += sb.st_size; 1973 first_apk = i; 1974 } else { 1975 break; 1976 } 1977 } 1978 1979 if (first_apk == -1) { 1980 fprintf(stderr, "Missing APK file\n"); 1981 return 1; 1982 } 1983 1984 snprintf(buf, sizeof(buf), "exec:pm install-create -S %lld", total_size); 1985 for (i = 1; i < first_apk; i++) { 1986 char *quoted = escape_arg(argv[i]); 1987 strncat(buf, " ", sizeof(buf) - 1); 1988 strncat(buf, quoted, sizeof(buf) - 1); 1989 free(quoted); 1990 } 1991 1992 // Create install session 1993 int fd = adb_connect(buf); 1994 if (fd < 0) { 1995 fprintf(stderr, "Connect error for create: %s\n", adb_error()); 1996 return -1; 1997 } 1998 read_status_line(fd, buf, sizeof(buf)); 1999 adb_close(fd); 2000 2001 int session_id = -1; 2002 if (!strncmp("Success", buf, 7)) { 2003 char* start = strrchr(buf, '['); 2004 char* end = strrchr(buf, ']'); 2005 if (start && end) { 2006 *end = '\0'; 2007 session_id = strtol(start + 1, NULL, 10); 2008 } 2009 } 2010 if (session_id < 0) { 2011 fprintf(stderr, "Failed to create session\n"); 2012 fputs(buf, stderr); 2013 return -1; 2014 } 2015 2016 // Valid session, now stream the APKs 2017 int success = 1; 2018 for (i = first_apk; i < argc; i++) { 2019 const char* file = argv[i]; 2020 if (stat(file, &sb) == -1) { 2021 fprintf(stderr, "Failed to stat %s\n", file); 2022 success = 0; 2023 goto finalize_session; 2024 } 2025 2026 snprintf(buf, sizeof(buf), "exec:pm install-write -S %lld %d %d_%s -", 2027 (long long int) sb.st_size, session_id, i, get_basename(file)); 2028 2029 int localFd = adb_open(file, O_RDONLY); 2030 if (localFd < 0) { 2031 fprintf(stderr, "Failed to open %s: %s\n", file, adb_error()); 2032 success = 0; 2033 goto finalize_session; 2034 } 2035 2036 int remoteFd = adb_connect(buf); 2037 if (remoteFd < 0) { 2038 fprintf(stderr, "Connect error for write: %s\n", adb_error()); 2039 adb_close(localFd); 2040 success = 0; 2041 goto finalize_session; 2042 } 2043 2044 copy_to_file(localFd, remoteFd); 2045 read_status_line(remoteFd, buf, sizeof(buf)); 2046 2047 adb_close(localFd); 2048 adb_close(remoteFd); 2049 2050 if (strncmp("Success", buf, 7)) { 2051 fprintf(stderr, "Failed to write %s\n", file); 2052 fputs(buf, stderr); 2053 success = 0; 2054 goto finalize_session; 2055 } 2056 } 2057 2058finalize_session: 2059 // Commit session if we streamed everything okay; otherwise abandon 2060 if (success) { 2061 snprintf(buf, sizeof(buf), "exec:pm install-commit %d", session_id); 2062 } else { 2063 snprintf(buf, sizeof(buf), "exec:pm install-abandon %d", session_id); 2064 } 2065 2066 fd = adb_connect(buf); 2067 if (fd < 0) { 2068 fprintf(stderr, "Connect error for finalize: %s\n", adb_error()); 2069 return -1; 2070 } 2071 read_status_line(fd, buf, sizeof(buf)); 2072 adb_close(fd); 2073 2074 if (!strncmp("Success", buf, 7)) { 2075 fputs(buf, stderr); 2076 return 0; 2077 } else { 2078 fprintf(stderr, "Failed to finalize session\n"); 2079 fputs(buf, stderr); 2080 return -1; 2081 } 2082} 2083