commandline.cpp revision e4baae6d066c7d39af127a2ba1df5e91475db1a9
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 "sysdeps.h" 20 21#include <assert.h> 22#include <ctype.h> 23#include <errno.h> 24#include <inttypes.h> 25#include <limits.h> 26#include <stdarg.h> 27#include <stdint.h> 28#include <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31#include <sys/stat.h> 32#include <sys/types.h> 33 34#include <string> 35 36#include <base/stringprintf.h> 37 38#if !defined(_WIN32) 39#include <termios.h> 40#include <unistd.h> 41#endif 42 43#include "adb.h" 44#include "adb_auth.h" 45#include "adb_client.h" 46#include "adb_io.h" 47#include "adb_utils.h" 48#include "file_sync_service.h" 49 50static int install_app(TransportType t, const char* serial, int argc, const char** argv); 51static int install_multiple_app(TransportType t, const char* serial, int argc, const char** argv); 52static int uninstall_app(TransportType t, const char* serial, int argc, const char** argv); 53 54static std::string gProductOutPath; 55extern int gListenAll; 56 57static std::string product_file(const char *extra) { 58 if (gProductOutPath.empty()) { 59 fprintf(stderr, "adb: Product directory not specified; " 60 "use -p or define ANDROID_PRODUCT_OUT\n"); 61 exit(1); 62 } 63 64 return android::base::StringPrintf("%s%s%s", 65 gProductOutPath.c_str(), OS_PATH_SEPARATOR_STR, extra); 66} 67 68static void help() { 69 fprintf(stderr, "%s\n", adb_version().c_str()); 70 fprintf(stderr, 71 " -a - directs adb to listen on all interfaces for a connection\n" 72 " -d - directs command to the only connected USB device\n" 73 " returns an error if more than one USB device is present.\n" 74 " -e - directs command to the only running emulator.\n" 75 " returns an error if more than one emulator is running.\n" 76 " -s <specific device> - directs command to the device or emulator with the given\n" 77 " serial number or qualifier. Overrides ANDROID_SERIAL\n" 78 " environment variable.\n" 79 " -p <product name or path> - simple product name like 'sooner', or\n" 80 " a relative/absolute path to a product\n" 81 " out directory like 'out/target/product/sooner'.\n" 82 " If -p is not specified, the ANDROID_PRODUCT_OUT\n" 83 " environment variable is used, which must\n" 84 " be an absolute path.\n" 85 " -H - Name of adb server host (default: localhost)\n" 86 " -P - Port of adb server (default: 5037)\n" 87 " devices [-l] - list all connected devices\n" 88 " ('-l' will also list device qualifiers)\n" 89 " connect <host>[:<port>] - connect to a device via TCP/IP\n" 90 " Port 5555 is used by default if no port number is specified.\n" 91 " disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.\n" 92 " Port 5555 is used by default if no port number is specified.\n" 93 " Using this command with no additional arguments\n" 94 " will disconnect from all connected TCP/IP devices.\n" 95 "\n" 96 "device commands:\n" 97 " adb push [-p] <local> <remote>\n" 98 " - copy file/dir to device\n" 99 " ('-p' to display the transfer progress)\n" 100 " adb pull [-p] [-a] <remote> [<local>]\n" 101 " - copy file/dir from device\n" 102 " ('-p' to display the transfer progress)\n" 103 " ('-a' means copy timestamp and mode)\n" 104 " adb sync [ <directory> ] - copy host->device only if changed\n" 105 " (-l means list but don't copy)\n" 106 " adb shell - run remote shell interactively\n" 107 " adb shell <command> - run remote shell command\n" 108 " adb emu <command> - run emulator console command\n" 109 " adb logcat [ <filter-spec> ] - View device log\n" 110 " adb forward --list - list all forward socket connections.\n" 111 " the format is a list of lines with the following format:\n" 112 " <serial> \" \" <local> \" \" <remote> \"\\n\"\n" 113 " adb forward <local> <remote> - forward socket connections\n" 114 " forward specs are one of: \n" 115 " tcp:<port>\n" 116 " localabstract:<unix domain socket name>\n" 117 " localreserved:<unix domain socket name>\n" 118 " localfilesystem:<unix domain socket name>\n" 119 " dev:<character device name>\n" 120 " jdwp:<process pid> (remote only)\n" 121 " adb forward --no-rebind <local> <remote>\n" 122 " - same as 'adb forward <local> <remote>' but fails\n" 123 " if <local> is already forwarded\n" 124 " adb forward --remove <local> - remove a specific forward socket connection\n" 125 " adb forward --remove-all - remove all forward socket connections\n" 126 " adb reverse --list - list all reverse socket connections from device\n" 127 " adb reverse <remote> <local> - reverse socket connections\n" 128 " reverse specs are one of:\n" 129 " tcp:<port>\n" 130 " localabstract:<unix domain socket name>\n" 131 " localreserved:<unix domain socket name>\n" 132 " localfilesystem:<unix domain socket name>\n" 133 " adb reverse --norebind <remote> <local>\n" 134 " - same as 'adb reverse <remote> <local>' but fails\n" 135 " if <remote> is already reversed.\n" 136 " adb reverse --remove <remote>\n" 137 " - remove a specific reversed socket connection\n" 138 " adb reverse --remove-all - remove all reversed socket connections from device\n" 139 " adb jdwp - list PIDs of processes hosting a JDWP transport\n" 140 " adb install [-lrtsdg] <file>\n" 141 " - push this package file to the device and install it\n" 142 " (-l: forward lock application)\n" 143 " (-r: replace existing application)\n" 144 " (-t: allow test packages)\n" 145 " (-s: install application on sdcard)\n" 146 " (-d: allow version code downgrade)\n" 147 " (-g: grant all runtime permissions)\n" 148 " adb install-multiple [-lrtsdpg] <file...>\n" 149 " - push this package file to the device and install it\n" 150 " (-l: forward lock application)\n" 151 " (-r: replace existing application)\n" 152 " (-t: allow test packages)\n" 153 " (-s: install application on sdcard)\n" 154 " (-d: allow version code downgrade)\n" 155 " (-p: partial application install)\n" 156 " (-g: grant all runtime permissions)\n" 157 " adb uninstall [-k] <package> - remove this app package from the device\n" 158 " ('-k' means keep the data and cache directories)\n" 159 " adb bugreport - return all information from the device\n" 160 " that should be included in a bug report.\n" 161 "\n" 162 " adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]\n" 163 " - write an archive of the device's data to <file>.\n" 164 " If no -f option is supplied then the data is written\n" 165 " to \"backup.ab\" in the current directory.\n" 166 " (-apk|-noapk enable/disable backup of the .apks themselves\n" 167 " in the archive; the default is noapk.)\n" 168 " (-obb|-noobb enable/disable backup of any installed apk expansion\n" 169 " (aka .obb) files associated with each application; the default\n" 170 " is noobb.)\n" 171 " (-shared|-noshared enable/disable backup of the device's\n" 172 " shared storage / SD card contents; the default is noshared.)\n" 173 " (-all means to back up all installed applications)\n" 174 " (-system|-nosystem toggles whether -all automatically includes\n" 175 " system applications; the default is to include system apps)\n" 176 " (<packages...> is the list of applications to be backed up. If\n" 177 " the -all or -shared flags are passed, then the package\n" 178 " list is optional. Applications explicitly given on the\n" 179 " command line will be included even if -nosystem would\n" 180 " ordinarily cause them to be omitted.)\n" 181 "\n" 182 " adb restore <file> - restore device contents from the <file> backup archive\n" 183 "\n" 184 " adb disable-verity - disable dm-verity checking on USERDEBUG builds\n" 185 " adb enable-verity - re-enable dm-verity checking on USERDEBUG builds\n" 186 " adb keygen <file> - generate adb public/private key. The private key is stored in <file>,\n" 187 " and the public key is stored in <file>.pub. Any existing files\n" 188 " are overwritten.\n" 189 " adb help - show this help message\n" 190 " adb version - show version num\n" 191 "\n" 192 "scripting:\n" 193 " adb wait-for-device - block until device is online\n" 194 " adb start-server - ensure that there is a server running\n" 195 " adb kill-server - kill the server if it is running\n" 196 " adb get-state - prints: offline | bootloader | device\n" 197 " adb get-serialno - prints: <serial-number>\n" 198 " adb get-devpath - prints: <device-path>\n" 199 " adb remount - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write\n" 200 " adb reboot [bootloader|recovery]\n" 201 " - reboots the device, optionally into the bootloader or recovery program.\n" 202 " adb reboot sideload - reboots the device into the sideload mode in recovery program (adb root required).\n" 203 " adb reboot sideload-auto-reboot\n" 204 " - reboots into the sideload mode, then reboots automatically after the sideload regardless of the result.\n" 205 " adb sideload <file> - sideloads the given package\n" 206 " adb root - restarts the adbd daemon with root permissions\n" 207 " adb unroot - restarts the adbd daemon without root permissions\n" 208 " adb usb - restarts the adbd daemon listening on USB\n" 209 " adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port\n" 210 "\n" 211 "networking:\n" 212 " adb ppp <tty> [parameters] - Run PPP over USB.\n" 213 " Note: you should not automatically start a PPP connection.\n" 214 " <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1\n" 215 " [parameters] - Eg. defaultroute debug dump local notty usepeerdns\n" 216 "\n" 217 "adb sync notes: adb sync [ <directory> ]\n" 218 " <localdir> can be interpreted in several ways:\n" 219 "\n" 220 " - If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.\n" 221 "\n" 222 " - If it is \"system\", \"vendor\", \"oem\" or \"data\", only the corresponding partition\n" 223 " is updated.\n" 224 "\n" 225 "environment variables:\n" 226 " ADB_TRACE - Print debug information. A comma separated list of the following values\n" 227 " 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n" 228 " ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.\n" 229 " ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.\n" 230 ); 231} 232 233static int usage() { 234 help(); 235 return 1; 236} 237 238#if defined(_WIN32) 239 240// Implemented in sysdeps_win32.cpp. 241void stdin_raw_init(int fd); 242void stdin_raw_restore(int fd); 243 244#else 245static termios g_saved_terminal_state; 246 247static void stdin_raw_init(int fd) { 248 if (tcgetattr(fd, &g_saved_terminal_state)) return; 249 250 termios tio; 251 if (tcgetattr(fd, &tio)) return; 252 253 cfmakeraw(&tio); 254 255 // No timeout but request at least one character per read. 256 tio.c_cc[VTIME] = 0; 257 tio.c_cc[VMIN] = 1; 258 259 tcsetattr(fd, TCSAFLUSH, &tio); 260} 261 262static void stdin_raw_restore(int fd) { 263 tcsetattr(fd, TCSAFLUSH, &g_saved_terminal_state); 264} 265#endif 266 267static void read_and_dump(int fd) { 268 while (fd >= 0) { 269 D("read_and_dump(): pre adb_read(fd=%d)\n", fd); 270 char buf[BUFSIZ]; 271 int len = adb_read(fd, buf, sizeof(buf)); 272 D("read_and_dump(): post adb_read(fd=%d): len=%d\n", fd, len); 273 if (len <= 0) { 274 break; 275 } 276 277 fwrite(buf, 1, len, stdout); 278 fflush(stdout); 279 } 280} 281 282static void read_status_line(int fd, char* buf, size_t count) 283{ 284 count--; 285 while (count > 0) { 286 int len = adb_read(fd, buf, count); 287 if (len <= 0) { 288 break; 289 } 290 291 buf += len; 292 count -= len; 293 } 294 *buf = '\0'; 295} 296 297static void copy_to_file(int inFd, int outFd) { 298 const size_t BUFSIZE = 32 * 1024; 299 char* buf = (char*) malloc(BUFSIZE); 300 if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file"); 301 int len; 302 long total = 0; 303#ifdef _WIN32 304 int old_stdin_mode = -1; 305 int old_stdout_mode = -1; 306#endif 307 308 D("copy_to_file(%d -> %d)\n", inFd, outFd); 309 310 if (inFd == STDIN_FILENO) { 311 stdin_raw_init(STDIN_FILENO); 312#ifdef _WIN32 313 old_stdin_mode = _setmode(STDIN_FILENO, _O_BINARY); 314 if (old_stdin_mode == -1) { 315 fatal_errno("could not set stdin to binary"); 316 } 317#endif 318 } 319 320#ifdef _WIN32 321 if (outFd == STDOUT_FILENO) { 322 old_stdout_mode = _setmode(STDOUT_FILENO, _O_BINARY); 323 if (old_stdout_mode == -1) { 324 fatal_errno("could not set stdout to binary"); 325 } 326 } 327#endif 328 329 while (true) { 330 if (inFd == STDIN_FILENO) { 331 len = unix_read(inFd, buf, BUFSIZE); 332 } else { 333 len = adb_read(inFd, buf, BUFSIZE); 334 } 335 if (len == 0) { 336 D("copy_to_file() : read 0 bytes; exiting\n"); 337 break; 338 } 339 if (len < 0) { 340 D("copy_to_file(): read failed: %s\n", strerror(errno)); 341 break; 342 } 343 if (outFd == STDOUT_FILENO) { 344 fwrite(buf, 1, len, stdout); 345 fflush(stdout); 346 } else { 347 adb_write(outFd, buf, len); 348 } 349 total += len; 350 } 351 352 if (inFd == STDIN_FILENO) { 353 stdin_raw_restore(STDIN_FILENO); 354#ifdef _WIN32 355 if (_setmode(STDIN_FILENO, old_stdin_mode) == -1) { 356 fatal_errno("could not restore stdin mode"); 357 } 358#endif 359 } 360 361#ifdef _WIN32 362 if (outFd == STDOUT_FILENO) { 363 if (_setmode(STDOUT_FILENO, old_stdout_mode) == -1) { 364 fatal_errno("could not restore stdout mode"); 365 } 366 } 367#endif 368 369 D("copy_to_file() finished after %lu bytes\n", total); 370 free(buf); 371} 372 373static void *stdin_read_thread(void *x) 374{ 375 int fd, fdi; 376 unsigned char buf[1024]; 377 int r, n; 378 int state = 0; 379 380 int *fds = (int*) x; 381 fd = fds[0]; 382 fdi = fds[1]; 383 free(fds); 384 385 while (true) { 386 /* fdi is really the client's stdin, so use read, not adb_read here */ 387 D("stdin_read_thread(): pre unix_read(fdi=%d,...)\n", fdi); 388 r = unix_read(fdi, buf, 1024); 389 D("stdin_read_thread(): post unix_read(fdi=%d,...)\n", fdi); 390 if (r <= 0) break; 391 for (n = 0; n < r; n++){ 392 switch(buf[n]) { 393 case '\n': 394 state = 1; 395 break; 396 case '\r': 397 state = 1; 398 break; 399 case '~': 400 if(state == 1) state++; 401 break; 402 case '.': 403 if(state == 2) { 404 fprintf(stderr,"\n* disconnect *\n"); 405 stdin_raw_restore(fdi); 406 exit(0); 407 } 408 default: 409 state = 0; 410 } 411 } 412 r = adb_write(fd, buf, r); 413 if(r <= 0) { 414 break; 415 } 416 } 417 return 0; 418} 419 420static int interactive_shell() { 421 int fdi; 422 423 std::string error; 424 int fd = adb_connect("shell:", &error); 425 if (fd < 0) { 426 fprintf(stderr,"error: %s\n", error.c_str()); 427 return 1; 428 } 429 fdi = 0; //dup(0); 430 431 int* fds = reinterpret_cast<int*>(malloc(sizeof(int) * 2)); 432 if (fds == nullptr) { 433 fprintf(stderr, "couldn't allocate fds array: %s\n", strerror(errno)); 434 return 1; 435 } 436 437 fds[0] = fd; 438 fds[1] = fdi; 439 440 stdin_raw_init(fdi); 441 442 adb_thread_create(stdin_read_thread, fds); 443 read_and_dump(fd); 444 stdin_raw_restore(fdi); 445 return 0; 446} 447 448 449static std::string format_host_command(const char* command, TransportType type, const char* serial) { 450 if (serial) { 451 return android::base::StringPrintf("host-serial:%s:%s", serial, command); 452 } 453 454 const char* prefix = "host"; 455 if (type == kTransportUsb) { 456 prefix = "host-usb"; 457 } else if (type == kTransportLocal) { 458 prefix = "host-local"; 459 } 460 return android::base::StringPrintf("%s:%s", prefix, command); 461} 462 463static int adb_download_buffer(const char *service, const char *fn, const void* data, unsigned sz, 464 bool show_progress) 465{ 466 std::string error; 467 int fd = adb_connect(android::base::StringPrintf("%s:%d", service, sz), &error); 468 if (fd < 0) { 469 fprintf(stderr,"error: %s\n", error.c_str()); 470 return -1; 471 } 472 473 int opt = CHUNK_SIZE; 474 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); 475 476 unsigned total = sz; 477 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data); 478 479 if (show_progress) { 480 const char* x = strrchr(service, ':'); 481 if (x) service = x + 1; 482 } 483 484 while (sz > 0) { 485 unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz; 486 if (!WriteFdExactly(fd, ptr, xfer)) { 487 std::string error; 488 adb_status(fd, &error); 489 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str()); 490 return -1; 491 } 492 sz -= xfer; 493 ptr += xfer; 494 if (show_progress) { 495 printf("sending: '%s' %4d%% \r", fn, (int)(100LL - ((100LL * sz) / (total)))); 496 fflush(stdout); 497 } 498 } 499 if (show_progress) { 500 printf("\n"); 501 } 502 503 if (!adb_status(fd, &error)) { 504 fprintf(stderr,"* error response '%s' *\n", error.c_str()); 505 return -1; 506 } 507 508 adb_close(fd); 509 return 0; 510} 511 512#define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE) 513 514/* 515 * The sideload-host protocol serves the data in a file (given on the 516 * command line) to the client, using a simple protocol: 517 * 518 * - The connect message includes the total number of bytes in the 519 * file and a block size chosen by us. 520 * 521 * - The other side sends the desired block number as eight decimal 522 * digits (eg "00000023" for block 23). Blocks are numbered from 523 * zero. 524 * 525 * - We send back the data of the requested block. The last block is 526 * likely to be partial; when the last block is requested we only 527 * send the part of the block that exists, it's not padded up to the 528 * block size. 529 * 530 * - When the other side sends "DONEDONE" instead of a block number, 531 * we hang up. 532 */ 533static int adb_sideload_host(const char* fn) { 534 unsigned sz; 535 size_t xfer = 0; 536 int status; 537 int last_percent = -1; 538 int opt = SIDELOAD_HOST_BLOCK_SIZE; 539 540 printf("loading: '%s'", fn); 541 fflush(stdout); 542 uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz)); 543 if (data == 0) { 544 printf("\n"); 545 fprintf(stderr, "* cannot read '%s' *\n", fn); 546 return -1; 547 } 548 549 std::string service = 550 android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE); 551 std::string error; 552 int fd = adb_connect(service, &error); 553 if (fd < 0) { 554 // Try falling back to the older sideload method. Maybe this 555 // is an older device that doesn't support sideload-host. 556 printf("\n"); 557 status = adb_download_buffer("sideload", fn, data, sz, true); 558 goto done; 559 } 560 561 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); 562 563 while (true) { 564 char buf[9]; 565 if (!ReadFdExactly(fd, buf, 8)) { 566 fprintf(stderr, "* failed to read command: %s\n", strerror(errno)); 567 status = -1; 568 goto done; 569 } 570 buf[8] = '\0'; 571 572 if (strcmp("DONEDONE", buf) == 0) { 573 status = 0; 574 break; 575 } 576 577 int block = strtol(buf, NULL, 10); 578 579 size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE; 580 if (offset >= sz) { 581 fprintf(stderr, "* attempt to read block %d past end\n", block); 582 status = -1; 583 goto done; 584 } 585 uint8_t* start = data + offset; 586 size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE; 587 size_t to_write = SIDELOAD_HOST_BLOCK_SIZE; 588 if (offset_end > sz) { 589 to_write = sz - offset; 590 } 591 592 if(!WriteFdExactly(fd, start, to_write)) { 593 adb_status(fd, &error); 594 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str()); 595 status = -1; 596 goto done; 597 } 598 xfer += to_write; 599 600 // For normal OTA packages, we expect to transfer every byte 601 // twice, plus a bit of overhead (one read during 602 // verification, one read of each byte for installation, plus 603 // extra access to things like the zip central directory). 604 // This estimate of the completion becomes 100% when we've 605 // transferred ~2.13 (=100/47) times the package size. 606 int percent = (int)(xfer * 47LL / (sz ? sz : 1)); 607 if (percent != last_percent) { 608 printf("\rserving: '%s' (~%d%%) ", fn, percent); 609 fflush(stdout); 610 last_percent = percent; 611 } 612 } 613 614 printf("\rTotal xfer: %.2fx%*s\n", (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, ""); 615 616 done: 617 if (fd >= 0) adb_close(fd); 618 free(data); 619 return status; 620} 621 622/** 623 * Run ppp in "notty" mode against a resource listed as the first parameter 624 * eg: 625 * 626 * ppp dev:/dev/omap_csmi_tty0 <ppp options> 627 * 628 */ 629static int ppp(int argc, const char** argv) { 630#if defined(_WIN32) 631 fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]); 632 return -1; 633#else 634 if (argc < 2) { 635 fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n", 636 argv[0]); 637 638 return 1; 639 } 640 641 const char* adb_service_name = argv[1]; 642 std::string error; 643 int fd = adb_connect(adb_service_name, &error); 644 if (fd < 0) { 645 fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n", 646 adb_service_name, error.c_str()); 647 return 1; 648 } 649 650 pid_t pid = fork(); 651 652 if (pid < 0) { 653 perror("from fork()"); 654 return 1; 655 } else if (pid == 0) { 656 int err; 657 int i; 658 const char **ppp_args; 659 660 // copy args 661 ppp_args = (const char **) alloca(sizeof(char *) * argc + 1); 662 ppp_args[0] = "pppd"; 663 for (i = 2 ; i < argc ; i++) { 664 //argv[2] and beyond become ppp_args[1] and beyond 665 ppp_args[i - 1] = argv[i]; 666 } 667 ppp_args[i-1] = NULL; 668 669 // child side 670 671 dup2(fd, STDIN_FILENO); 672 dup2(fd, STDOUT_FILENO); 673 adb_close(STDERR_FILENO); 674 adb_close(fd); 675 676 err = execvp("pppd", (char * const *)ppp_args); 677 678 if (err < 0) { 679 perror("execing pppd"); 680 } 681 exit(-1); 682 } else { 683 // parent side 684 685 adb_close(fd); 686 return 0; 687 } 688#endif /* !defined(_WIN32) */ 689} 690 691static bool wait_for_device(const char* service, TransportType t, const char* serial) { 692 // Was the caller vague about what they'd like us to wait for? 693 // If so, check they weren't more specific in their choice of transport type. 694 if (strcmp(service, "wait-for-device") == 0) { 695 if (t == kTransportUsb) { 696 service = "wait-for-usb"; 697 } else if (t == kTransportLocal) { 698 service = "wait-for-local"; 699 } else { 700 service = "wait-for-any"; 701 } 702 } 703 704 std::string cmd = format_host_command(service, t, serial); 705 return adb_command(cmd); 706} 707 708static int send_shell_command(TransportType transport_type, const char* serial, 709 const std::string& command) { 710 int fd; 711 while (true) { 712 std::string error; 713 fd = adb_connect(command, &error); 714 if (fd >= 0) { 715 break; 716 } 717 fprintf(stderr,"- waiting for device -\n"); 718 adb_sleep_ms(1000); 719 wait_for_device("wait-for-device", transport_type, serial); 720 } 721 722 read_and_dump(fd); 723 int rc = adb_close(fd); 724 if (rc) { 725 perror("close"); 726 } 727 return rc; 728} 729 730static int logcat(TransportType transport, const char* serial, int argc, const char** argv) { 731 char* log_tags = getenv("ANDROID_LOG_TAGS"); 732 std::string quoted = escape_arg(log_tags == nullptr ? "" : log_tags); 733 734 std::string cmd = "shell:export ANDROID_LOG_TAGS=\"" + quoted + "\"; exec logcat"; 735 736 if (!strcmp(argv[0], "longcat")) { 737 cmd += " -v long"; 738 } 739 740 --argc; 741 ++argv; 742 while (argc-- > 0) { 743 cmd += " " + escape_arg(*argv++); 744 } 745 746 return send_shell_command(transport, serial, cmd); 747} 748 749static int backup(int argc, const char** argv) { 750 const char* filename = "./backup.ab"; 751 752 /* find, extract, and use any -f argument */ 753 for (int i = 1; i < argc; i++) { 754 if (!strcmp("-f", argv[i])) { 755 if (i == argc-1) { 756 fprintf(stderr, "adb: -f passed with no filename\n"); 757 return usage(); 758 } 759 filename = argv[i+1]; 760 for (int j = i+2; j <= argc; ) { 761 argv[i++] = argv[j++]; 762 } 763 argc -= 2; 764 argv[argc] = NULL; 765 } 766 } 767 768 /* bare "adb backup" or "adb backup -f filename" are not valid invocations */ 769 if (argc < 2) return usage(); 770 771 adb_unlink(filename); 772 mkdirs(filename); 773 int outFd = adb_creat(filename, 0640); 774 if (outFd < 0) { 775 fprintf(stderr, "adb: unable to open file %s\n", filename); 776 return -1; 777 } 778 779 std::string cmd = "backup:"; 780 --argc; 781 ++argv; 782 while (argc-- > 0) { 783 cmd += " " + escape_arg(*argv++); 784 } 785 786 D("backup. filename=%s cmd=%s\n", filename, cmd.c_str()); 787 std::string error; 788 int fd = adb_connect(cmd, &error); 789 if (fd < 0) { 790 fprintf(stderr, "adb: unable to connect for backup: %s\n", error.c_str()); 791 adb_close(outFd); 792 return -1; 793 } 794 795 printf("Now unlock your device and confirm the backup operation.\n"); 796 copy_to_file(fd, outFd); 797 798 adb_close(fd); 799 adb_close(outFd); 800 return 0; 801} 802 803static int restore(int argc, const char** argv) { 804 if (argc != 2) return usage(); 805 806 const char* filename = argv[1]; 807 int tarFd = adb_open(filename, O_RDONLY); 808 if (tarFd < 0) { 809 fprintf(stderr, "adb: unable to open file %s: %s\n", filename, strerror(errno)); 810 return -1; 811 } 812 813 std::string error; 814 int fd = adb_connect("restore:", &error); 815 if (fd < 0) { 816 fprintf(stderr, "adb: unable to connect for restore: %s\n", error.c_str()); 817 adb_close(tarFd); 818 return -1; 819 } 820 821 printf("Now unlock your device and confirm the restore operation.\n"); 822 copy_to_file(tarFd, fd); 823 824 adb_close(fd); 825 adb_close(tarFd); 826 return 0; 827} 828 829/* <hint> may be: 830 * - A simple product name 831 * e.g., "sooner" 832 * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir 833 * e.g., "out/target/product/sooner" 834 * - An absolute path to the PRODUCT_OUT dir 835 * e.g., "/src/device/out/target/product/sooner" 836 * 837 * Given <hint>, try to construct an absolute path to the 838 * ANDROID_PRODUCT_OUT dir. 839 */ 840static std::string find_product_out_path(const std::string& hint) { 841 if (hint.empty()) { 842 return ""; 843 } 844 845 // If it's already absolute, don't bother doing any work. 846 if (adb_is_absolute_host_path(hint.c_str())) { 847 return hint; 848 } 849 850 // If there are any slashes in it, assume it's a relative path; 851 // make it absolute. 852 if (hint.find_first_of(OS_PATH_SEPARATORS) != std::string::npos) { 853 std::string cwd; 854 if (!getcwd(&cwd)) { 855 fprintf(stderr, "adb: getcwd failed: %s\n", strerror(errno)); 856 return ""; 857 } 858 return android::base::StringPrintf("%s%c%s", cwd.c_str(), OS_PATH_SEPARATOR, hint.c_str()); 859 } 860 861 // It's a string without any slashes. Try to do something with it. 862 // 863 // Try to find the root of the build tree, and build a PRODUCT_OUT 864 // path from there. 865 char* top = getenv("ANDROID_BUILD_TOP"); 866 if (top == nullptr) { 867 fprintf(stderr, "adb: ANDROID_BUILD_TOP not set!\n"); 868 return ""; 869 } 870 871 std::string path = top; 872 path += OS_PATH_SEPARATOR_STR; 873 path += "out"; 874 path += OS_PATH_SEPARATOR_STR; 875 path += "target"; 876 path += OS_PATH_SEPARATOR_STR; 877 path += "product"; 878 path += OS_PATH_SEPARATOR_STR; 879 path += hint; 880 if (!directory_exists(path)) { 881 fprintf(stderr, "adb: Couldn't find a product dir based on -p %s; " 882 "\"%s\" doesn't exist\n", hint.c_str(), path.c_str()); 883 return ""; 884 } 885 return path; 886} 887 888static void parse_push_pull_args(const char **arg, int narg, char const **path1, 889 char const **path2, bool* show_progress, 890 int *copy_attrs) { 891 *show_progress = false; 892 *copy_attrs = 0; 893 894 while (narg > 0) { 895 if (!strcmp(*arg, "-p")) { 896 *show_progress = true; 897 } else if (!strcmp(*arg, "-a")) { 898 *copy_attrs = 1; 899 } else { 900 break; 901 } 902 ++arg; 903 --narg; 904 } 905 906 if (narg > 0) { 907 *path1 = *arg; 908 ++arg; 909 --narg; 910 } 911 912 if (narg > 0) { 913 *path2 = *arg; 914 } 915} 916 917static int adb_connect_command(const std::string& command) { 918 std::string error; 919 int fd = adb_connect(command, &error); 920 if (fd < 0) { 921 fprintf(stderr, "error: %s\n", error.c_str()); 922 return 1; 923 } 924 read_and_dump(fd); 925 adb_close(fd); 926 return 0; 927} 928 929static int adb_query_command(const std::string& command) { 930 std::string result; 931 std::string error; 932 if (!adb_query(command, &result, &error)) { 933 fprintf(stderr, "error: %s\n", error.c_str()); 934 return 1; 935 } 936 printf("%s\n", result.c_str()); 937 return 0; 938} 939 940int adb_commandline(int argc, const char **argv) { 941 int no_daemon = 0; 942 int is_daemon = 0; 943 int is_server = 0; 944 int r; 945 TransportType transport_type = kTransportAny; 946 int ack_reply_fd = -1; 947 948 // If defined, this should be an absolute path to 949 // the directory containing all of the various system images 950 // for a particular product. If not defined, and the adb 951 // command requires this information, then the user must 952 // specify the path using "-p". 953 char* ANDROID_PRODUCT_OUT = getenv("ANDROID_PRODUCT_OUT"); 954 if (ANDROID_PRODUCT_OUT != nullptr) { 955 gProductOutPath = ANDROID_PRODUCT_OUT; 956 } 957 // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint 958 959 const char* serial = getenv("ANDROID_SERIAL"); 960 961 /* Validate and assign the server port */ 962 const char* server_port_str = getenv("ANDROID_ADB_SERVER_PORT"); 963 int server_port = DEFAULT_ADB_PORT; 964 if (server_port_str && strlen(server_port_str) > 0) { 965 server_port = strtol(server_port_str, nullptr, 0); 966 if (server_port <= 0 || server_port > 65535) { 967 fprintf(stderr, 968 "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number less than 65536. Got \"%s\"\n", 969 server_port_str); 970 return usage(); 971 } 972 } 973 974 /* modifiers and flags */ 975 while (argc > 0) { 976 if (!strcmp(argv[0],"server")) { 977 is_server = 1; 978 } else if (!strcmp(argv[0],"nodaemon")) { 979 no_daemon = 1; 980 } else if (!strcmp(argv[0], "fork-server")) { 981 /* this is a special flag used only when the ADB client launches the ADB Server */ 982 is_daemon = 1; 983 } else if (!strcmp(argv[0], "--reply-fd")) { 984 if (argc < 2) return usage(); 985 const char* reply_fd_str = argv[1]; 986 argc--; 987 argv++; 988 ack_reply_fd = strtol(reply_fd_str, nullptr, 10); 989#ifdef _WIN32 990 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); 991 if ((GetStdHandle(STD_INPUT_HANDLE) == ack_reply_handle) || 992 (GetStdHandle(STD_OUTPUT_HANDLE) == ack_reply_handle) || 993 (GetStdHandle(STD_ERROR_HANDLE) == ack_reply_handle)) { 994#else 995 if (ack_reply_fd <= 2) { // Disallow stdin, stdout, and stderr. 996#endif 997 fprintf(stderr, "adb: invalid reply fd \"%s\"\n", reply_fd_str); 998 return usage(); 999 } 1000 } else if (!strncmp(argv[0], "-p", 2)) { 1001 const char* product = nullptr; 1002 if (argv[0][2] == '\0') { 1003 if (argc < 2) return usage(); 1004 product = argv[1]; 1005 argc--; 1006 argv++; 1007 } else { 1008 product = argv[0] + 2; 1009 } 1010 gProductOutPath = find_product_out_path(product); 1011 if (gProductOutPath.empty()) { 1012 fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product); 1013 return usage(); 1014 } 1015 } else if (argv[0][0]=='-' && argv[0][1]=='s') { 1016 if (isdigit(argv[0][2])) { 1017 serial = argv[0] + 2; 1018 } else { 1019 if (argc < 2 || argv[0][2] != '\0') return usage(); 1020 serial = argv[1]; 1021 argc--; 1022 argv++; 1023 } 1024 } else if (!strcmp(argv[0],"-d")) { 1025 transport_type = kTransportUsb; 1026 } else if (!strcmp(argv[0],"-e")) { 1027 transport_type = kTransportLocal; 1028 } else if (!strcmp(argv[0],"-a")) { 1029 gListenAll = 1; 1030 } else if (!strncmp(argv[0], "-H", 2)) { 1031 const char *hostname = NULL; 1032 if (argv[0][2] == '\0') { 1033 if (argc < 2) return usage(); 1034 hostname = argv[1]; 1035 argc--; 1036 argv++; 1037 } else { 1038 hostname = argv[0] + 2; 1039 } 1040 adb_set_tcp_name(hostname); 1041 1042 } else if (!strncmp(argv[0], "-P", 2)) { 1043 if (argv[0][2] == '\0') { 1044 if (argc < 2) return usage(); 1045 server_port_str = argv[1]; 1046 argc--; 1047 argv++; 1048 } else { 1049 server_port_str = argv[0] + 2; 1050 } 1051 if (strlen(server_port_str) > 0) { 1052 server_port = (int) strtol(server_port_str, NULL, 0); 1053 if (server_port <= 0 || server_port > 65535) { 1054 fprintf(stderr, 1055 "adb: port number must be a positive number less than 65536. Got \"%s\"\n", 1056 server_port_str); 1057 return usage(); 1058 } 1059 } else { 1060 fprintf(stderr, 1061 "adb: port number must be a positive number less than 65536. Got empty string.\n"); 1062 return usage(); 1063 } 1064 } else { 1065 /* out of recognized modifiers and flags */ 1066 break; 1067 } 1068 argc--; 1069 argv++; 1070 } 1071 1072 adb_set_transport(transport_type, serial); 1073 adb_set_tcp_specifics(server_port); 1074 1075 if (is_server) { 1076 if (no_daemon || is_daemon) { 1077 if (is_daemon && (ack_reply_fd == -1)) { 1078 fprintf(stderr, "reply fd for adb server to client communication not specified.\n"); 1079 return usage(); 1080 } 1081 r = adb_main(is_daemon, server_port, ack_reply_fd); 1082 } else { 1083 r = launch_server(server_port); 1084 } 1085 if (r) { 1086 fprintf(stderr,"* could not start server *\n"); 1087 } 1088 return r; 1089 } 1090 1091 if (argc == 0) { 1092 return usage(); 1093 } 1094 1095 /* handle wait-for-* prefix */ 1096 if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) { 1097 const char* service = argv[0]; 1098 1099 if (!wait_for_device(service, transport_type, serial)) { 1100 return 1; 1101 } 1102 1103 // Allow a command to be run after wait-for-device, 1104 // e.g. 'adb wait-for-device shell'. 1105 if (argc == 1) { 1106 return 0; 1107 } 1108 1109 /* Fall through */ 1110 argc--; 1111 argv++; 1112 } 1113 1114 /* adb_connect() commands */ 1115 if (!strcmp(argv[0], "devices")) { 1116 const char *listopt; 1117 if (argc < 2) { 1118 listopt = ""; 1119 } else if (argc == 2 && !strcmp(argv[1], "-l")) { 1120 listopt = argv[1]; 1121 } else { 1122 fprintf(stderr, "Usage: adb devices [-l]\n"); 1123 return 1; 1124 } 1125 1126 std::string query = android::base::StringPrintf("host:%s%s", argv[0], listopt); 1127 printf("List of devices attached\n"); 1128 return adb_query_command(query); 1129 } 1130 else if (!strcmp(argv[0], "connect")) { 1131 if (argc != 2) { 1132 fprintf(stderr, "Usage: adb connect <host>[:<port>]\n"); 1133 return 1; 1134 } 1135 1136 std::string query = android::base::StringPrintf("host:connect:%s", argv[1]); 1137 return adb_query_command(query); 1138 } 1139 else if (!strcmp(argv[0], "disconnect")) { 1140 if (argc > 2) { 1141 fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n"); 1142 return 1; 1143 } 1144 1145 std::string query = android::base::StringPrintf("host:disconnect:%s", 1146 (argc == 2) ? argv[1] : ""); 1147 return adb_query_command(query); 1148 } 1149 else if (!strcmp(argv[0], "emu")) { 1150 return adb_send_emulator_command(argc, argv, serial); 1151 } 1152 else if (!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) { 1153 char h = (argv[0][0] == 'h'); 1154 1155 if (h) { 1156 printf("\x1b[41;33m"); 1157 fflush(stdout); 1158 } 1159 1160 if (argc < 2) { 1161 D("starting interactive shell\n"); 1162 r = interactive_shell(); 1163 if (h) { 1164 printf("\x1b[0m"); 1165 fflush(stdout); 1166 } 1167 return r; 1168 } 1169 1170 std::string cmd = "shell:"; 1171 --argc; 1172 ++argv; 1173 while (argc-- > 0) { 1174 // We don't escape here, just like ssh(1). http://b/20564385. 1175 cmd += *argv++; 1176 if (*argv) cmd += " "; 1177 } 1178 1179 while (true) { 1180 D("interactive shell loop. cmd=%s\n", cmd.c_str()); 1181 std::string error; 1182 int fd = adb_connect(cmd, &error); 1183 int r; 1184 if (fd >= 0) { 1185 D("about to read_and_dump(fd=%d)\n", fd); 1186 read_and_dump(fd); 1187 D("read_and_dump() done.\n"); 1188 adb_close(fd); 1189 r = 0; 1190 } else { 1191 fprintf(stderr,"error: %s\n", error.c_str()); 1192 r = -1; 1193 } 1194 1195 if (h) { 1196 printf("\x1b[0m"); 1197 fflush(stdout); 1198 } 1199 D("interactive shell loop. return r=%d\n", r); 1200 return r; 1201 } 1202 } 1203 else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) { 1204 int exec_in = !strcmp(argv[0], "exec-in"); 1205 1206 std::string cmd = "exec:"; 1207 cmd += argv[1]; 1208 argc -= 2; 1209 argv += 2; 1210 while (argc-- > 0) { 1211 cmd += " " + escape_arg(*argv++); 1212 } 1213 1214 std::string error; 1215 int fd = adb_connect(cmd, &error); 1216 if (fd < 0) { 1217 fprintf(stderr, "error: %s\n", error.c_str()); 1218 return -1; 1219 } 1220 1221 if (exec_in) { 1222 copy_to_file(STDIN_FILENO, fd); 1223 } else { 1224 copy_to_file(fd, STDOUT_FILENO); 1225 } 1226 1227 adb_close(fd); 1228 return 0; 1229 } 1230 else if (!strcmp(argv[0], "kill-server")) { 1231 std::string error; 1232 int fd = _adb_connect("host:kill", &error); 1233 if (fd == -2) { 1234 // Failed to make network connection to server. Don't output the 1235 // network error since that is expected. 1236 fprintf(stderr,"* server not running *\n"); 1237 // Successful exit code because the server is already "killed". 1238 return 0; 1239 } else if (fd == -1) { 1240 // Some other error. 1241 fprintf(stderr, "error: %s\n", error.c_str()); 1242 return 1; 1243 } else { 1244 // Successfully connected, kill command sent, okay status came back. 1245 // Server should exit() in a moment, if not already. 1246 adb_close(fd); 1247 return 0; 1248 } 1249 } 1250 else if (!strcmp(argv[0], "sideload")) { 1251 if (argc != 2) return usage(); 1252 if (adb_sideload_host(argv[1])) { 1253 return 1; 1254 } else { 1255 return 0; 1256 } 1257 } 1258 else if (!strcmp(argv[0], "tcpip") && argc > 1) { 1259 return adb_connect_command(android::base::StringPrintf("tcpip:%s", argv[1])); 1260 } 1261 else if (!strcmp(argv[0], "remount") || 1262 !strcmp(argv[0], "reboot") || 1263 !strcmp(argv[0], "reboot-bootloader") || 1264 !strcmp(argv[0], "usb") || 1265 !strcmp(argv[0], "root") || 1266 !strcmp(argv[0], "unroot") || 1267 !strcmp(argv[0], "disable-verity") || 1268 !strcmp(argv[0], "enable-verity")) { 1269 std::string command; 1270 if (!strcmp(argv[0], "reboot-bootloader")) { 1271 command = "reboot:bootloader"; 1272 } else if (argc > 1) { 1273 command = android::base::StringPrintf("%s:%s", argv[0], argv[1]); 1274 } else { 1275 command = android::base::StringPrintf("%s:", argv[0]); 1276 } 1277 return adb_connect_command(command); 1278 } 1279 else if (!strcmp(argv[0], "bugreport")) { 1280 if (argc != 1) return usage(); 1281 return send_shell_command(transport_type, serial, "shell:bugreport"); 1282 } 1283 else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) { 1284 bool reverse = !strcmp(argv[0], "reverse"); 1285 ++argv; 1286 --argc; 1287 if (argc < 1) return usage(); 1288 1289 // Determine the <host-prefix> for this command. 1290 std::string host_prefix; 1291 if (reverse) { 1292 host_prefix = "reverse"; 1293 } else { 1294 if (serial) { 1295 host_prefix = android::base::StringPrintf("host-serial:%s", serial); 1296 } else if (transport_type == kTransportUsb) { 1297 host_prefix = "host-usb"; 1298 } else if (transport_type == kTransportLocal) { 1299 host_prefix = "host-local"; 1300 } else { 1301 host_prefix = "host"; 1302 } 1303 } 1304 1305 std::string cmd; 1306 if (strcmp(argv[0], "--list") == 0) { 1307 if (argc != 1) return usage(); 1308 return adb_query_command(host_prefix + ":list-forward"); 1309 } else if (strcmp(argv[0], "--remove-all") == 0) { 1310 if (argc != 1) return usage(); 1311 cmd = host_prefix + ":killforward-all"; 1312 } else if (strcmp(argv[0], "--remove") == 0) { 1313 // forward --remove <local> 1314 if (argc != 2) return usage(); 1315 cmd = host_prefix + ":killforward:" + argv[1]; 1316 } else if (strcmp(argv[0], "--no-rebind") == 0) { 1317 // forward --no-rebind <local> <remote> 1318 if (argc != 3) return usage(); 1319 cmd = host_prefix + ":forward:norebind:" + argv[1] + ";" + argv[2]; 1320 } else { 1321 // forward <local> <remote> 1322 if (argc != 2) return usage(); 1323 cmd = host_prefix + ":forward:" + argv[0] + ";" + argv[1]; 1324 } 1325 1326 return adb_command(cmd) ? 0 : 1; 1327 } 1328 /* do_sync_*() commands */ 1329 else if (!strcmp(argv[0], "ls")) { 1330 if (argc != 2) return usage(); 1331 return do_sync_ls(argv[1]) ? 0 : 1; 1332 } 1333 else if (!strcmp(argv[0], "push")) { 1334 bool show_progress = false; 1335 int copy_attrs = 0; 1336 const char* lpath = NULL, *rpath = NULL; 1337 1338 parse_push_pull_args(&argv[1], argc - 1, &lpath, &rpath, &show_progress, ©_attrs); 1339 if (!lpath || !rpath || copy_attrs != 0) return usage(); 1340 return do_sync_push(lpath, rpath, show_progress) ? 0 : 1; 1341 } 1342 else if (!strcmp(argv[0], "pull")) { 1343 bool show_progress = false; 1344 int copy_attrs = 0; 1345 const char* rpath = NULL, *lpath = "."; 1346 1347 parse_push_pull_args(&argv[1], argc - 1, &rpath, &lpath, &show_progress, ©_attrs); 1348 if (!rpath) return usage(); 1349 return do_sync_pull(rpath, lpath, show_progress, copy_attrs) ? 0 : 1; 1350 } 1351 else if (!strcmp(argv[0], "install")) { 1352 if (argc < 2) return usage(); 1353 return install_app(transport_type, serial, argc, argv); 1354 } 1355 else if (!strcmp(argv[0], "install-multiple")) { 1356 if (argc < 2) return usage(); 1357 return install_multiple_app(transport_type, serial, argc, argv); 1358 } 1359 else if (!strcmp(argv[0], "uninstall")) { 1360 if (argc < 2) return usage(); 1361 return uninstall_app(transport_type, serial, argc, argv); 1362 } 1363 else if (!strcmp(argv[0], "sync")) { 1364 std::string src; 1365 bool list_only = false; 1366 if (argc < 2) { 1367 // No local path was specified. 1368 src = ""; 1369 } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) { 1370 list_only = true; 1371 if (argc == 3) { 1372 src = argv[2]; 1373 } else { 1374 src = ""; 1375 } 1376 } else if (argc == 2) { 1377 // A local path or "android"/"data" arg was specified. 1378 src = argv[1]; 1379 } else { 1380 return usage(); 1381 } 1382 1383 if (src != "" && 1384 src != "system" && src != "data" && src != "vendor" && src != "oem") { 1385 return usage(); 1386 } 1387 1388 std::string system_src_path = product_file("system"); 1389 std::string data_src_path = product_file("data"); 1390 std::string vendor_src_path = product_file("vendor"); 1391 std::string oem_src_path = product_file("oem"); 1392 1393 bool okay = true; 1394 if (okay && (src.empty() || src == "system")) { 1395 okay = do_sync_sync(system_src_path, "/system", list_only); 1396 } 1397 if (okay && (src.empty() || src == "vendor") && directory_exists(vendor_src_path)) { 1398 okay = do_sync_sync(vendor_src_path, "/vendor", list_only); 1399 } 1400 if (okay && (src.empty() || src == "oem") && directory_exists(oem_src_path)) { 1401 okay = do_sync_sync(oem_src_path, "/oem", list_only); 1402 } 1403 if (okay && (src.empty() || src == "data")) { 1404 okay = do_sync_sync(data_src_path, "/data", list_only); 1405 } 1406 return okay ? 0 : 1; 1407 } 1408 /* passthrough commands */ 1409 else if (!strcmp(argv[0],"get-state") || 1410 !strcmp(argv[0],"get-serialno") || 1411 !strcmp(argv[0],"get-devpath")) 1412 { 1413 return adb_query_command(format_host_command(argv[0], transport_type, serial)); 1414 } 1415 /* other commands */ 1416 else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) { 1417 return logcat(transport_type, serial, argc, argv); 1418 } 1419 else if (!strcmp(argv[0],"ppp")) { 1420 return ppp(argc, argv); 1421 } 1422 else if (!strcmp(argv[0], "start-server")) { 1423 std::string error; 1424 const int result = adb_connect("host:start-server", &error); 1425 if (result < 0) { 1426 fprintf(stderr, "error: %s\n", error.c_str()); 1427 } 1428 return result; 1429 } 1430 else if (!strcmp(argv[0], "backup")) { 1431 return backup(argc, argv); 1432 } 1433 else if (!strcmp(argv[0], "restore")) { 1434 return restore(argc, argv); 1435 } 1436 else if (!strcmp(argv[0], "keygen")) { 1437 if (argc < 2) return usage(); 1438 return adb_auth_keygen(argv[1]); 1439 } 1440 else if (!strcmp(argv[0], "jdwp")) { 1441 return adb_connect_command("jdwp"); 1442 } 1443 /* "adb /?" is a common idiom under Windows */ 1444 else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) { 1445 help(); 1446 return 0; 1447 } 1448 else if (!strcmp(argv[0], "version")) { 1449 fprintf(stdout, "%s", adb_version().c_str()); 1450 return 0; 1451 } 1452 else if (!strcmp(argv[0], "features")) { 1453 return adb_query_command("host:features"); 1454 } 1455 1456 usage(); 1457 return 1; 1458} 1459 1460static int pm_command(TransportType transport, const char* serial, int argc, const char** argv) { 1461 std::string cmd = "shell:pm"; 1462 1463 while (argc-- > 0) { 1464 cmd += " " + escape_arg(*argv++); 1465 } 1466 1467 return send_shell_command(transport, serial, cmd); 1468} 1469 1470static int uninstall_app(TransportType transport, const char* serial, int argc, const char** argv) { 1471 /* if the user choose the -k option, we refuse to do it until devices are 1472 out with the option to uninstall the remaining data somehow (adb/ui) */ 1473 if (argc == 3 && strcmp(argv[1], "-k") == 0) 1474 { 1475 printf( 1476 "The -k option uninstalls the application while retaining the data/cache.\n" 1477 "At the moment, there is no way to remove the remaining data.\n" 1478 "You will have to reinstall the application with the same signature, and fully uninstall it.\n" 1479 "If you truly wish to continue, execute 'adb shell pm uninstall -k %s'\n", argv[2]); 1480 return -1; 1481 } 1482 1483 /* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */ 1484 return pm_command(transport, serial, argc, argv); 1485} 1486 1487static int delete_file(TransportType transport, const char* serial, const std::string& filename) { 1488 std::string cmd = "shell:rm -f " + escape_arg(filename); 1489 return send_shell_command(transport, serial, cmd); 1490} 1491 1492static int install_app(TransportType transport, const char* serial, int argc, const char** argv) { 1493 static const char *const DATA_DEST = "/data/local/tmp/%s"; 1494 static const char *const SD_DEST = "/sdcard/tmp/%s"; 1495 const char* where = DATA_DEST; 1496 int i; 1497 struct stat sb; 1498 1499 for (i = 1; i < argc; i++) { 1500 if (!strcmp(argv[i], "-s")) { 1501 where = SD_DEST; 1502 } 1503 } 1504 1505 // Find last APK argument. 1506 // All other arguments passed through verbatim. 1507 int last_apk = -1; 1508 for (i = argc - 1; i >= 0; i--) { 1509 const char* file = argv[i]; 1510 const char* dot = strrchr(file, '.'); 1511 if (dot && !strcasecmp(dot, ".apk")) { 1512 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) { 1513 fprintf(stderr, "Invalid APK file: %s\n", file); 1514 return -1; 1515 } 1516 1517 last_apk = i; 1518 break; 1519 } 1520 } 1521 1522 if (last_apk == -1) { 1523 fprintf(stderr, "Missing APK file\n"); 1524 return -1; 1525 } 1526 1527 int result = -1; 1528 const char* apk_file = argv[last_apk]; 1529 std::string apk_dest = android::base::StringPrintf(where, adb_basename(apk_file).c_str()); 1530 if (!do_sync_push(apk_file, apk_dest.c_str(), false)) goto cleanup_apk; 1531 argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */ 1532 result = pm_command(transport, serial, argc, argv); 1533 1534cleanup_apk: 1535 delete_file(transport, serial, apk_dest); 1536 return result; 1537} 1538 1539static int install_multiple_app(TransportType transport, const char* serial, int argc, 1540 const char** argv) 1541{ 1542 int i; 1543 struct stat sb; 1544 uint64_t total_size = 0; 1545 1546 // Find all APK arguments starting at end. 1547 // All other arguments passed through verbatim. 1548 int first_apk = -1; 1549 for (i = argc - 1; i >= 0; i--) { 1550 const char* file = argv[i]; 1551 const char* dot = strrchr(file, '.'); 1552 if (dot && !strcasecmp(dot, ".apk")) { 1553 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) { 1554 fprintf(stderr, "Invalid APK file: %s\n", file); 1555 return -1; 1556 } 1557 1558 total_size += sb.st_size; 1559 first_apk = i; 1560 } else { 1561 break; 1562 } 1563 } 1564 1565 if (first_apk == -1) { 1566 fprintf(stderr, "Missing APK file\n"); 1567 return 1; 1568 } 1569 1570 std::string cmd = android::base::StringPrintf("exec:pm install-create -S %" PRIu64, total_size); 1571 for (i = 1; i < first_apk; i++) { 1572 cmd += " " + escape_arg(argv[i]); 1573 } 1574 1575 // Create install session 1576 std::string error; 1577 int fd = adb_connect(cmd, &error); 1578 if (fd < 0) { 1579 fprintf(stderr, "Connect error for create: %s\n", error.c_str()); 1580 return -1; 1581 } 1582 char buf[BUFSIZ]; 1583 read_status_line(fd, buf, sizeof(buf)); 1584 adb_close(fd); 1585 1586 int session_id = -1; 1587 if (!strncmp("Success", buf, 7)) { 1588 char* start = strrchr(buf, '['); 1589 char* end = strrchr(buf, ']'); 1590 if (start && end) { 1591 *end = '\0'; 1592 session_id = strtol(start + 1, NULL, 10); 1593 } 1594 } 1595 if (session_id < 0) { 1596 fprintf(stderr, "Failed to create session\n"); 1597 fputs(buf, stderr); 1598 return -1; 1599 } 1600 1601 // Valid session, now stream the APKs 1602 int success = 1; 1603 for (i = first_apk; i < argc; i++) { 1604 const char* file = argv[i]; 1605 if (stat(file, &sb) == -1) { 1606 fprintf(stderr, "Failed to stat %s\n", file); 1607 success = 0; 1608 goto finalize_session; 1609 } 1610 1611 std::string cmd = android::base::StringPrintf( 1612 "exec:pm install-write -S %" PRIu64 " %d %d_%s -", 1613 static_cast<uint64_t>(sb.st_size), session_id, i, adb_basename(file).c_str()); 1614 1615 int localFd = adb_open(file, O_RDONLY); 1616 if (localFd < 0) { 1617 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno)); 1618 success = 0; 1619 goto finalize_session; 1620 } 1621 1622 std::string error; 1623 int remoteFd = adb_connect(cmd, &error); 1624 if (remoteFd < 0) { 1625 fprintf(stderr, "Connect error for write: %s\n", error.c_str()); 1626 adb_close(localFd); 1627 success = 0; 1628 goto finalize_session; 1629 } 1630 1631 copy_to_file(localFd, remoteFd); 1632 read_status_line(remoteFd, buf, sizeof(buf)); 1633 1634 adb_close(localFd); 1635 adb_close(remoteFd); 1636 1637 if (strncmp("Success", buf, 7)) { 1638 fprintf(stderr, "Failed to write %s\n", file); 1639 fputs(buf, stderr); 1640 success = 0; 1641 goto finalize_session; 1642 } 1643 } 1644 1645finalize_session: 1646 // Commit session if we streamed everything okay; otherwise abandon 1647 std::string service = 1648 android::base::StringPrintf("exec:pm install-%s %d", 1649 success ? "commit" : "abandon", session_id); 1650 fd = adb_connect(service, &error); 1651 if (fd < 0) { 1652 fprintf(stderr, "Connect error for finalize: %s\n", error.c_str()); 1653 return -1; 1654 } 1655 read_status_line(fd, buf, sizeof(buf)); 1656 adb_close(fd); 1657 1658 if (!strncmp("Success", buf, 7)) { 1659 fputs(buf, stderr); 1660 return 0; 1661 } else { 1662 fprintf(stderr, "Failed to finalize session\n"); 1663 fputs(buf, stderr); 1664 return -1; 1665 } 1666} 1667