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