The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | #include <ctype.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <errno.h> |
Elliott Hughes | 2940ccf | 2015-04-17 14:07:52 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <limits.h> |
| 26 | #include <stdarg.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | #include <sys/stat.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 32 | #include <sys/types.h> |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 33 | #include <iostream> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 35 | #include <memory> |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 36 | #include <string> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 37 | #include <thread> |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 38 | #include <vector> |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 40 | #include <android-base/file.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Tao Wu | 135f4ab | 2016-09-16 13:01:24 -0700 | [diff] [blame] | 42 | #include <android-base/parseint.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
| 44 | #include <android-base/strings.h> |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 45 | |
Yabin Cui | d325e86 | 2014-11-17 14:48:25 -0800 | [diff] [blame] | 46 | #if !defined(_WIN32) |
Josh Gao | 8dcdb57 | 2015-10-23 15:03:31 -0700 | [diff] [blame] | 47 | #include <signal.h> |
Elliott Hughes | 0fbf961 | 2015-11-04 13:07:47 -0800 | [diff] [blame] | 48 | #include <sys/ioctl.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | #include <termios.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 50 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | #endif |
| 52 | |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 53 | #include <google/protobuf/text_format.h> |
| 54 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | #include "adb.h" |
Nick Kralevich | bea3f9c | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 56 | #include "adb_auth.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 57 | #include "adb_client.h" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 58 | #include "adb_install.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 59 | #include "adb_io.h" |
Josh Gao | 924d35a | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 60 | #include "adb_unique_fd.h" |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 61 | #include "adb_utils.h" |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 62 | #include "app_processes.pb.h" |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 63 | #include "bugreport.h" |
Josh Gao | edec033 | 2018-07-25 15:55:25 -0700 | [diff] [blame] | 64 | #include "client/file_sync_client.h" |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 65 | #include "commandline.h" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 66 | #include "fastdeploy.h" |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 67 | #include "incremental_server.h" |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 68 | #include "services.h" |
Josh Gao | 86acb86 | 2018-07-25 16:07:26 -0700 | [diff] [blame] | 69 | #include "shell_protocol.h" |
Josh Gao | 4602adb | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 70 | #include "sysdeps/chrono.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 72 | extern int gListenAll; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 74 | DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK(nullptr, nullptr); |
| 75 | |
Elliott Hughes | 57e97cb | 2018-04-03 10:54:39 -0700 | [diff] [blame] | 76 | static std::string product_file(const std::string& file) { |
Elliott Hughes | 0754cb9 | 2017-05-01 11:04:31 -0700 | [diff] [blame] | 77 | const char* ANDROID_PRODUCT_OUT = getenv("ANDROID_PRODUCT_OUT"); |
| 78 | if (ANDROID_PRODUCT_OUT == nullptr) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 79 | error_exit("product directory not specified; set $ANDROID_PRODUCT_OUT"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | } |
Elliott Hughes | 57e97cb | 2018-04-03 10:54:39 -0700 | [diff] [blame] | 81 | return std::string{ANDROID_PRODUCT_OUT} + OS_PATH_SEPARATOR_STR + file; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 84 | static void help() { |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 85 | fprintf(stdout, "%s\n", adb_version().c_str()); |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 86 | // clang-format off |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 87 | fprintf(stdout, |
| 88 | "global options:\n" |
| 89 | " -a listen on all network interfaces, not just localhost\n" |
| 90 | " -d use USB device (error if multiple devices connected)\n" |
Elliott Hughes | 83ab5c2 | 2017-01-13 16:58:25 -0800 | [diff] [blame] | 91 | " -e use TCP/IP device (error if multiple TCP/IP devices available)\n" |
Elliott Hughes | 0754cb9 | 2017-05-01 11:04:31 -0700 | [diff] [blame] | 92 | " -s SERIAL use device with given serial (overrides $ANDROID_SERIAL)\n" |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 93 | " -t ID use device with given transport id\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 94 | " -H name of adb server host [default=localhost]\n" |
| 95 | " -P port of adb server [default=5037]\n" |
| 96 | " -L SOCKET listen on given socket for adb server [default=tcp:localhost:5037]\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | "\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 98 | "general commands:\n" |
| 99 | " devices [-l] list connected devices (-l for long output)\n" |
| 100 | " help show this help message\n" |
| 101 | " version show version num\n" |
Elliott Hughes | 7e067cf | 2015-06-12 14:26:29 -0700 | [diff] [blame] | 102 | "\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | "networking:\n" |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 104 | " connect HOST[:PORT] connect to a device via TCP/IP [default port=5555]\n" |
| 105 | " disconnect [HOST[:PORT]]\n" |
| 106 | " disconnect from given TCP/IP device [default port=5555], or all\n" |
| 107 | " pair HOST[:PORT] pair with a device for secure TCP/IP communication\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 108 | " forward --list list all forward socket connections\n" |
| 109 | " forward [--no-rebind] LOCAL REMOTE\n" |
| 110 | " forward socket connection using:\n" |
| 111 | " tcp:<port> (<local> may be \"tcp:0\" to pick any open port)\n" |
| 112 | " localabstract:<unix domain socket name>\n" |
| 113 | " localreserved:<unix domain socket name>\n" |
| 114 | " localfilesystem:<unix domain socket name>\n" |
| 115 | " dev:<character device name>\n" |
| 116 | " jdwp:<process pid> (remote only)\n" |
Daniel Colascione | 3ec7be7 | 2019-11-13 17:49:37 -0800 | [diff] [blame] | 117 | " acceptfd:<fd> (listen only)\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 118 | " forward --remove LOCAL remove specific forward socket connection\n" |
| 119 | " forward --remove-all remove all forward socket connections\n" |
| 120 | " ppp TTY [PARAMETER...] run PPP over USB\n" |
| 121 | " reverse --list list all reverse socket connections from device\n" |
| 122 | " reverse [--no-rebind] REMOTE LOCAL\n" |
| 123 | " reverse socket connection using:\n" |
| 124 | " tcp:<port> (<remote> may be \"tcp:0\" to pick any open port)\n" |
| 125 | " localabstract:<unix domain socket name>\n" |
| 126 | " localreserved:<unix domain socket name>\n" |
| 127 | " localfilesystem:<unix domain socket name>\n" |
| 128 | " reverse --remove REMOTE remove specific reverse socket connection\n" |
| 129 | " reverse --remove-all remove all reverse socket connections from device\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | "\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 131 | "file transfer:\n" |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 132 | " push [--sync] [-z ALGORITHM] [-Z] LOCAL... REMOTE\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 133 | " copy local files/directories to device\n" |
Dan Albert | 06b0d6b | 2017-05-18 22:56:48 -0700 | [diff] [blame] | 134 | " --sync: only push files that are newer on the host than the device\n" |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 135 | " -n: dry run: push files to device without storing to the filesystem\n" |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 136 | " -z: enable compression with a specified algorithm (any, none, brotli)\n" |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 137 | " -Z: disable compression\n" |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 138 | " pull [-a] [-z ALGORITHM] [-Z] REMOTE... LOCAL\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 139 | " copy files/dirs from device\n" |
| 140 | " -a: preserve file timestamp and mode\n" |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 141 | " -z: enable compression with a specified algorithm (any, none, brotli)\n" |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 142 | " -Z: disable compression\n" |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 143 | " sync [-l] [-z ALGORITHM] [-Z] [all|data|odm|oem|product|system|system_ext|vendor]\n" |
Elliott Hughes | 0754cb9 | 2017-05-01 11:04:31 -0700 | [diff] [blame] | 144 | " sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)\n" |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 145 | " -n: dry run: push files to device without storing to the filesystem\n" |
Elliott Hughes | 4c8ea89 | 2019-07-30 10:32:53 -0700 | [diff] [blame] | 146 | " -l: list files that would be copied, but don't copy them\n" |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 147 | " -z: enable compression with a specified algorithm (any, none, brotli)\n" |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 148 | " -Z: disable compression\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | "\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 150 | "shell:\n" |
| 151 | " shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]\n" |
| 152 | " run remote shell command (interactive shell if no command given)\n" |
| 153 | " -e: choose escape character, or \"none\"; default '~'\n" |
| 154 | " -n: don't read from stdin\n" |
Elliott Hughes | dfe6c84 | 2019-12-13 16:43:10 -0800 | [diff] [blame] | 155 | " -T: disable pty allocation\n" |
| 156 | " -t: allocate a pty if on a tty (-tt: force pty allocation)\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 157 | " -x: disable remote exit codes and stdout/stderr separation\n" |
| 158 | " emu COMMAND run emulator console command\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | "\n" |
Elliott Hughes | 0d4bf80 | 2019-01-11 13:50:05 -0800 | [diff] [blame] | 160 | "app installation (see also `adb shell cmd package help`):\n" |
Felipe Leme | 807be77 | 2018-05-08 18:40:18 -0700 | [diff] [blame] | 161 | " install [-lrtsdg] [--instant] PACKAGE\n" |
Patrick Baumann | dc58b0a | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 162 | " push a single package to the device and install it\n" |
Felipe Leme | 807be77 | 2018-05-08 18:40:18 -0700 | [diff] [blame] | 163 | " install-multiple [-lrtsdpg] [--instant] PACKAGE...\n" |
Patrick Baumann | dc58b0a | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 164 | " push multiple APKs to the device for a single package and install them\n" |
| 165 | " install-multi-package [-lrtsdpg] [--instant] PACKAGE...\n" |
| 166 | " push one or more packages to the device and install them atomically\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 167 | " -r: replace existing application\n" |
| 168 | " -t: allow test packages\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 169 | " -d: allow version code downgrade (debuggable packages only)\n" |
| 170 | " -p: partial application install (install-multiple only)\n" |
| 171 | " -g: grant all runtime permissions\n" |
Elliott Hughes | c0ebd59 | 2019-08-02 17:09:30 -0700 | [diff] [blame] | 172 | " --abi ABI: override platform's default ABI\n" |
Felipe Leme | 807be77 | 2018-05-08 18:40:18 -0700 | [diff] [blame] | 173 | " --instant: cause the app to be installed as an ephemeral install app\n" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 174 | " --no-streaming: always push APK to device and invoke Package Manager as separate steps\n" |
| 175 | " --streaming: force streaming APK directly into Package Manager\n" |
Henry Daitx | ee01c80 | 2018-12-12 10:40:57 +0000 | [diff] [blame] | 176 | " --fastdeploy: use fast deploy\n" |
| 177 | " --no-fastdeploy: prevent use of fast deploy\n" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 178 | " --force-agent: force update of deployment agent when using fast deploy\n" |
| 179 | " --date-check-agent: update deployment agent when local version is newer and using fast deploy\n" |
| 180 | " --version-check-agent: update deployment agent when local version has different version code and using fast deploy\n" |
| 181 | #ifndef _WIN32 |
| 182 | " --local-agent: locate agent files from local source build (instead of SDK location)\n" |
| 183 | #endif |
Elliott Hughes | c0ebd59 | 2019-08-02 17:09:30 -0700 | [diff] [blame] | 184 | " (See also `adb shell pm help` for more options.)\n" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 185 | //TODO--installlog <filename> |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 186 | " uninstall [-k] PACKAGE\n" |
| 187 | " remove this app package from the device\n" |
| 188 | " '-k': keep the data and cache directories\n" |
| 189 | "\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 190 | "debugging:\n" |
| 191 | " bugreport [PATH]\n" |
| 192 | " write bugreport to given PATH [default=bugreport.zip];\n" |
| 193 | " if PATH is a directory, the bug report is saved in that directory.\n" |
| 194 | " devices that don't support zipped bug reports output to stdout.\n" |
| 195 | " jdwp list pids of processes hosting a JDWP transport\n" |
| 196 | " logcat show device log (logcat --help for more)\n" |
| 197 | "\n" |
| 198 | "security:\n" |
| 199 | " disable-verity disable dm-verity checking on userdebug builds\n" |
| 200 | " enable-verity re-enable dm-verity checking on userdebug builds\n" |
| 201 | " keygen FILE\n" |
| 202 | " generate adb public/private key; private key stored in FILE,\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 203 | "\n" |
| 204 | "scripting:\n" |
Elliott Hughes | a52b458 | 2020-03-10 16:57:47 -0700 | [diff] [blame] | 205 | " wait-for[-TRANSPORT]-STATE...\n" |
| 206 | " wait for device to be in a given state\n" |
Tao Bao | 55d407e | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 207 | " STATE: device, recovery, rescue, sideload, bootloader, or disconnect\n" |
Josh Gao | 1e9e471 | 2019-02-22 14:01:36 -0800 | [diff] [blame] | 208 | " TRANSPORT: usb, local, or any [default=any]\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 209 | " get-state print offline | bootloader | device\n" |
| 210 | " get-serialno print <serial-number>\n" |
| 211 | " get-devpath print <device-path>\n" |
David Anderson | a5db1d7 | 2018-11-21 13:43:22 -0800 | [diff] [blame] | 212 | " remount [-R]\n" |
| 213 | " remount partitions read-write. if a reboot is required, -R will\n" |
| 214 | " will automatically reboot the device.\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 215 | " reboot [bootloader|recovery|sideload|sideload-auto-reboot]\n" |
| 216 | " reboot the device; defaults to booting system image but\n" |
| 217 | " supports bootloader and recovery too. sideload reboots\n" |
| 218 | " into recovery and automatically starts sideload mode,\n" |
| 219 | " sideload-auto-reboot is the same but reboots after sideloading.\n" |
| 220 | " sideload OTAPACKAGE sideload the given full OTA package\n" |
| 221 | " root restart adbd with root permissions\n" |
| 222 | " unroot restart adbd without root permissions\n" |
bohu | ea0c3cb | 2018-10-24 10:38:33 -0700 | [diff] [blame] | 223 | " usb restart adbd listening on USB\n" |
| 224 | " tcpip PORT restart adbd listening on TCP on PORT\n" |
Tim | cd64315 | 2010-02-16 20:18:29 +0000 | [diff] [blame] | 225 | "\n" |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 226 | "internal debugging:\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 227 | " start-server ensure that there is a server running\n" |
| 228 | " kill-server kill the server if it is running\n" |
| 229 | " reconnect kick connection from host side to force reconnect\n" |
| 230 | " reconnect device kick connection from device side to force reconnect\n" |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 231 | " reconnect offline reset offline/unauthorized devices to force reconnect\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 232 | "\n" |
Elliott Hughes | 7e067cf | 2015-06-12 14:26:29 -0700 | [diff] [blame] | 233 | "environment variables:\n" |
Elliott Hughes | 4bf9bc1 | 2016-09-28 15:25:47 -0700 | [diff] [blame] | 234 | " $ADB_TRACE\n" |
| 235 | " comma-separated list of debug info to log:\n" |
| 236 | " all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp\n" |
| 237 | " $ADB_VENDOR_KEYS colon-separated list of keys (files or directories)\n" |
| 238 | " $ANDROID_SERIAL serial number to connect to (see -s)\n" |
Tim Baverstock | 25173c8 | 2019-02-08 16:27:16 +0000 | [diff] [blame] | 239 | " $ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)\n" |
| 240 | " $ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)\n" |
| 241 | ); |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 242 | // clang-format on |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Yabin Cui | d325e86 | 2014-11-17 14:48:25 -0800 | [diff] [blame] | 245 | #if defined(_WIN32) |
| 246 | |
Elliott Hughes | a2f2e56 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 247 | // Implemented in sysdeps_win32.cpp. |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 248 | void stdin_raw_init(); |
| 249 | void stdin_raw_restore(); |
Yabin Cui | d325e86 | 2014-11-17 14:48:25 -0800 | [diff] [blame] | 250 | |
| 251 | #else |
Alistair Buxton | dfa09fd | 2013-03-01 22:16:41 +0100 | [diff] [blame] | 252 | static termios g_saved_terminal_state; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 254 | static void stdin_raw_init() { |
| 255 | if (tcgetattr(STDIN_FILENO, &g_saved_terminal_state)) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | |
Alistair Buxton | dfa09fd | 2013-03-01 22:16:41 +0100 | [diff] [blame] | 257 | termios tio; |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 258 | if (tcgetattr(STDIN_FILENO, &tio)) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | |
Alistair Buxton | dfa09fd | 2013-03-01 22:16:41 +0100 | [diff] [blame] | 260 | cfmakeraw(&tio); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | |
Alistair Buxton | dfa09fd | 2013-03-01 22:16:41 +0100 | [diff] [blame] | 262 | // No timeout but request at least one character per read. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | tio.c_cc[VTIME] = 0; |
| 264 | tio.c_cc[VMIN] = 1; |
| 265 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 266 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 269 | static void stdin_raw_restore() { |
| 270 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &g_saved_terminal_state); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | } |
| 272 | #endif |
| 273 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 274 | int read_and_dump(borrowed_fd fd, bool use_shell_protocol, |
| 275 | StandardStreamsCallbackInterface* callback) { |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 276 | int exit_code = 0; |
Felipe Leme | d1dd125 | 2016-06-06 16:05:36 -0700 | [diff] [blame] | 277 | if (fd < 0) return exit_code; |
| 278 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 279 | std::unique_ptr<ShellProtocol> protocol; |
| 280 | int length = 0; |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 281 | |
| 282 | char raw_buffer[BUFSIZ]; |
| 283 | char* buffer_ptr = raw_buffer; |
| 284 | if (use_shell_protocol) { |
Josh Gao | 31b5be6 | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 285 | protocol = std::make_unique<ShellProtocol>(fd); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 286 | if (!protocol) { |
| 287 | LOG(ERROR) << "failed to allocate memory for ShellProtocol object"; |
| 288 | return 1; |
| 289 | } |
| 290 | buffer_ptr = protocol->data(); |
| 291 | } |
| 292 | |
Felipe Leme | d1dd125 | 2016-06-06 16:05:36 -0700 | [diff] [blame] | 293 | while (true) { |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 294 | if (use_shell_protocol) { |
| 295 | if (!protocol->Read()) { |
| 296 | break; |
| 297 | } |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 298 | length = protocol->data_length(); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 299 | switch (protocol->id()) { |
| 300 | case ShellProtocol::kIdStdout: |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 301 | callback->OnStdout(buffer_ptr, length); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 302 | break; |
| 303 | case ShellProtocol::kIdStderr: |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 304 | callback->OnStderr(buffer_ptr, length); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 305 | break; |
| 306 | case ShellProtocol::kIdExit: |
Josh Gao | 1e1ae45 | 2019-03-15 14:49:25 -0700 | [diff] [blame] | 307 | // data() returns a char* which doesn't have defined signedness. |
| 308 | // Cast to uint8_t to prevent 255 from being sign extended to INT_MIN, |
| 309 | // which doesn't get truncated on Windows. |
| 310 | exit_code = static_cast<uint8_t>(protocol->data()[0]); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 311 | continue; |
| 312 | default: |
| 313 | continue; |
| 314 | } |
| 315 | length = protocol->data_length(); |
| 316 | } else { |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 317 | D("read_and_dump(): pre adb_read(fd=%d)", fd.get()); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 318 | length = adb_read(fd, raw_buffer, sizeof(raw_buffer)); |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 319 | D("read_and_dump(): post adb_read(fd=%d): length=%d", fd.get(), length); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 320 | if (length <= 0) { |
| 321 | break; |
| 322 | } |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 323 | callback->OnStdout(buffer_ptr, length); |
Felipe Leme | 44a4267 | 2016-04-01 17:43:27 -0700 | [diff] [blame] | 324 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | } |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 326 | |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 327 | return callback->Done(exit_code); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 330 | static void stdinout_raw_prologue(int inFd, int outFd, int& old_stdin_mode, int& old_stdout_mode) { |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 331 | if (inFd == STDIN_FILENO) { |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 332 | stdin_raw_init(); |
Spencer Low | b7dfb79 | 2015-05-22 16:48:31 -0700 | [diff] [blame] | 333 | #ifdef _WIN32 |
| 334 | old_stdin_mode = _setmode(STDIN_FILENO, _O_BINARY); |
| 335 | if (old_stdin_mode == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 336 | PLOG(FATAL) << "could not set stdin to binary"; |
Spencer Low | b7dfb79 | 2015-05-22 16:48:31 -0700 | [diff] [blame] | 337 | } |
| 338 | #endif |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 339 | } |
Yabin Cui | d325e86 | 2014-11-17 14:48:25 -0800 | [diff] [blame] | 340 | |
Spencer Low | b7dfb79 | 2015-05-22 16:48:31 -0700 | [diff] [blame] | 341 | #ifdef _WIN32 |
| 342 | if (outFd == STDOUT_FILENO) { |
| 343 | old_stdout_mode = _setmode(STDOUT_FILENO, _O_BINARY); |
| 344 | if (old_stdout_mode == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 345 | PLOG(FATAL) << "could not set stdout to binary"; |
Spencer Low | b7dfb79 | 2015-05-22 16:48:31 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | #endif |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static void stdinout_raw_epilogue(int inFd, int outFd, int old_stdin_mode, int old_stdout_mode) { |
| 352 | if (inFd == STDIN_FILENO) { |
| 353 | stdin_raw_restore(); |
| 354 | #ifdef _WIN32 |
| 355 | if (_setmode(STDIN_FILENO, old_stdin_mode) == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 356 | PLOG(FATAL) << "could not restore stdin mode"; |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 357 | } |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | #ifdef _WIN32 |
| 362 | if (outFd == STDOUT_FILENO) { |
| 363 | if (_setmode(STDOUT_FILENO, old_stdout_mode) == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 364 | PLOG(FATAL) << "could not restore stdout mode"; |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | #endif |
| 368 | } |
| 369 | |
Josh Gao | 3828ebc | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 370 | bool copy_to_file(int inFd, int outFd) { |
| 371 | bool result = true; |
Yurii Zubrytskyi | 6eca0e6 | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 372 | std::vector<char> buf(64 * 1024); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 373 | int len; |
| 374 | long total = 0; |
| 375 | int old_stdin_mode = -1; |
| 376 | int old_stdout_mode = -1; |
| 377 | |
| 378 | D("copy_to_file(%d -> %d)", inFd, outFd); |
| 379 | |
| 380 | stdinout_raw_prologue(inFd, outFd, old_stdin_mode, old_stdout_mode); |
Spencer Low | b7dfb79 | 2015-05-22 16:48:31 -0700 | [diff] [blame] | 381 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 382 | while (true) { |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 383 | if (inFd == STDIN_FILENO) { |
Elliott Hughes | 86ab9ff | 2018-09-05 12:13:11 -0700 | [diff] [blame] | 384 | len = unix_read(inFd, buf.data(), buf.size()); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 385 | } else { |
Elliott Hughes | 86ab9ff | 2018-09-05 12:13:11 -0700 | [diff] [blame] | 386 | len = adb_read(inFd, buf.data(), buf.size()); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 387 | } |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 388 | if (len == 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 389 | D("copy_to_file() : read 0 bytes; exiting"); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | if (len < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 393 | D("copy_to_file(): read failed: %s", strerror(errno)); |
Josh Gao | 3828ebc | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 394 | result = false; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 395 | break; |
| 396 | } |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 397 | if (outFd == STDOUT_FILENO) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 398 | fwrite(buf.data(), 1, len, stdout); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 399 | fflush(stdout); |
| 400 | } else { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 401 | adb_write(outFd, buf.data(), len); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 402 | } |
Christopher Tate | c9cd3b9 | 2011-06-01 17:56:23 -0700 | [diff] [blame] | 403 | total += len; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 404 | } |
Yabin Cui | d325e86 | 2014-11-17 14:48:25 -0800 | [diff] [blame] | 405 | |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 406 | stdinout_raw_epilogue(inFd, outFd, old_stdin_mode, old_stdout_mode); |
Spencer Low | b7dfb79 | 2015-05-22 16:48:31 -0700 | [diff] [blame] | 407 | |
Josh Gao | 3828ebc | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 408 | D("copy_to_file() finished with %s after %lu bytes", result ? "success" : "failure", total); |
| 409 | return result; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 412 | static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) { |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 413 | // Old devices can't handle window size changes. |
| 414 | if (shell == nullptr) return; |
| 415 | |
Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 416 | #if defined(_WIN32) |
| 417 | struct winsize { |
| 418 | unsigned short ws_row; |
| 419 | unsigned short ws_col; |
| 420 | unsigned short ws_xpixel; |
| 421 | unsigned short ws_ypixel; |
| 422 | }; |
| 423 | #endif |
| 424 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 425 | winsize ws; |
Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 426 | |
| 427 | #if defined(_WIN32) |
| 428 | // If stdout is redirected to a non-console, we won't be able to get the |
| 429 | // console size, but that makes sense. |
| 430 | const intptr_t intptr_handle = _get_osfhandle(STDOUT_FILENO); |
| 431 | if (intptr_handle == -1) return; |
| 432 | |
| 433 | const HANDLE handle = reinterpret_cast<const HANDLE>(intptr_handle); |
| 434 | |
| 435 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 436 | memset(&info, 0, sizeof(info)); |
| 437 | if (!GetConsoleScreenBufferInfo(handle, &info)) return; |
| 438 | |
| 439 | memset(&ws, 0, sizeof(ws)); |
| 440 | // The number of visible rows, excluding offscreen scroll-back rows which are in info.dwSize.Y. |
| 441 | ws.ws_row = info.srWindow.Bottom - info.srWindow.Top + 1; |
| 442 | // If the user has disabled "Wrap text output on resize", they can make the screen buffer wider |
| 443 | // than the window, in which case we should use the width of the buffer. |
| 444 | ws.ws_col = info.dwSize.X; |
| 445 | #else |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 446 | if (ioctl(fd, TIOCGWINSZ, &ws) == -1) return; |
Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 447 | #endif |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 448 | |
| 449 | // Send the new window size as human-readable ASCII for debugging convenience. |
| 450 | size_t l = snprintf(shell->data(), shell->data_capacity(), "%dx%d,%dx%d", |
| 451 | ws.ws_row, ws.ws_col, ws.ws_xpixel, ws.ws_ypixel); |
| 452 | shell->Write(ShellProtocol::kIdWindowSizeChange, l + 1); |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 453 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 454 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 455 | // Used to pass multiple values to the stdin read thread. |
| 456 | struct StdinReadArgs { |
| 457 | int stdin_fd, write_fd; |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 458 | bool raw_stdin; |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 459 | std::unique_ptr<ShellProtocol> protocol; |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 460 | char escape_char; |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 463 | // Loops to read from stdin and push the data to the given FD. |
| 464 | // The argument should be a pointer to a StdinReadArgs object. This function |
| 465 | // will take ownership of the object and delete it when finished. |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 466 | static void stdin_read_thread_loop(void* x) { |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 467 | std::unique_ptr<StdinReadArgs> args(reinterpret_cast<StdinReadArgs*>(x)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 468 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 469 | #if !defined(_WIN32) |
| 470 | // Mask SIGTTIN in case we're in a backgrounded process. |
Josh Gao | 8dcdb57 | 2015-10-23 15:03:31 -0700 | [diff] [blame] | 471 | sigset_t sigset; |
| 472 | sigemptyset(&sigset); |
| 473 | sigaddset(&sigset, SIGTTIN); |
| 474 | pthread_sigmask(SIG_BLOCK, &sigset, nullptr); |
| 475 | #endif |
| 476 | |
Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 477 | #if defined(_WIN32) |
| 478 | // _get_interesting_input_record_uncached() causes unix_read_interruptible() |
| 479 | // to return -1 with errno == EINTR if the window size changes. |
| 480 | #else |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 481 | // Unblock SIGWINCH for this thread, so our read(2) below will be |
| 482 | // interrupted if the window size changes. |
| 483 | sigset_t mask; |
| 484 | sigemptyset(&mask); |
| 485 | sigaddset(&mask, SIGWINCH); |
| 486 | pthread_sigmask(SIG_UNBLOCK, &mask, nullptr); |
| 487 | #endif |
| 488 | |
| 489 | // Set up the initial window size. |
| 490 | send_window_size_change(args->stdin_fd, args->protocol); |
| 491 | |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 492 | char raw_buffer[BUFSIZ]; |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 493 | char* buffer_ptr = raw_buffer; |
| 494 | size_t buffer_size = sizeof(raw_buffer); |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 495 | if (args->protocol != nullptr) { |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 496 | buffer_ptr = args->protocol->data(); |
| 497 | buffer_size = args->protocol->data_capacity(); |
| 498 | } |
| 499 | |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 500 | // If we need to parse escape sequences, make life easy. |
| 501 | if (args->raw_stdin && args->escape_char != '\0') { |
| 502 | buffer_size = 1; |
| 503 | } |
| 504 | |
| 505 | enum EscapeState { kMidFlow, kStartOfLine, kInEscape }; |
| 506 | EscapeState state = kStartOfLine; |
| 507 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 508 | while (true) { |
Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 509 | // Use unix_read_interruptible() rather than adb_read() for stdin. |
| 510 | D("stdin_read_thread_loop(): pre unix_read_interruptible(fdi=%d,...)", args->stdin_fd); |
| 511 | int r = unix_read_interruptible(args->stdin_fd, buffer_ptr, |
| 512 | buffer_size); |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 513 | if (r == -1 && errno == EINTR) { |
| 514 | send_window_size_change(args->stdin_fd, args->protocol); |
| 515 | continue; |
| 516 | } |
Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 517 | D("stdin_read_thread_loop(): post unix_read_interruptible(fdi=%d,...)", args->stdin_fd); |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 518 | if (r <= 0) { |
| 519 | // Only devices using the shell protocol know to close subprocess |
| 520 | // stdin. For older devices we want to just leave the connection |
| 521 | // open, otherwise an unpredictable amount of return data could |
| 522 | // be lost due to the FD closing before all data has been received. |
| 523 | if (args->protocol) { |
| 524 | args->protocol->Write(ShellProtocol::kIdCloseStdin, 0); |
| 525 | } |
| 526 | break; |
| 527 | } |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 528 | // If we made stdin raw, check input for escape sequences. In |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 529 | // this situation signals like Ctrl+C are sent remotely rather than |
| 530 | // interpreted locally so this provides an emergency out if the remote |
| 531 | // process starts ignoring the signal. SSH also does this, see the |
| 532 | // "escape characters" section on the ssh man page for more info. |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 533 | if (args->raw_stdin && args->escape_char != '\0') { |
| 534 | char ch = buffer_ptr[0]; |
| 535 | if (ch == args->escape_char) { |
| 536 | if (state == kStartOfLine) { |
| 537 | state = kInEscape; |
| 538 | // Swallow the escape character. |
| 539 | continue; |
| 540 | } else { |
| 541 | state = kMidFlow; |
| 542 | } |
| 543 | } else { |
| 544 | if (state == kInEscape) { |
| 545 | if (ch == '.') { |
| 546 | fprintf(stderr,"\r\n[ disconnected ]\r\n"); |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 547 | stdin_raw_restore(); |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 548 | exit(0); |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 549 | } else { |
| 550 | // We swallowed an escape character that wasn't part of |
| 551 | // a valid escape sequence; time to cough it up. |
| 552 | buffer_ptr[0] = args->escape_char; |
| 553 | buffer_ptr[1] = ch; |
| 554 | ++r; |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 555 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 556 | } |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 557 | state = (ch == '\n' || ch == '\r') ? kStartOfLine : kMidFlow; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 558 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | } |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 560 | if (args->protocol) { |
| 561 | if (!args->protocol->Write(ShellProtocol::kIdStdin, r)) { |
| 562 | break; |
| 563 | } |
| 564 | } else { |
| 565 | if (!WriteFdExactly(args->write_fd, buffer_ptr, r)) { |
| 566 | break; |
| 567 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 568 | } |
| 569 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 570 | } |
| 571 | |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 572 | // Returns a shell service string with the indicated arguments and command. |
| 573 | static std::string ShellServiceString(bool use_shell_protocol, |
| 574 | const std::string& type_arg, |
| 575 | const std::string& command) { |
| 576 | std::vector<std::string> args; |
| 577 | if (use_shell_protocol) { |
| 578 | args.push_back(kShellServiceArgShellProtocol); |
Elliott Hughes | c2252df | 2015-11-18 12:45:48 -0800 | [diff] [blame] | 579 | |
| 580 | const char* terminal_type = getenv("TERM"); |
| 581 | if (terminal_type != nullptr) { |
| 582 | args.push_back(std::string("TERM=") + terminal_type); |
| 583 | } |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 584 | } |
| 585 | if (!type_arg.empty()) { |
| 586 | args.push_back(type_arg); |
| 587 | } |
| 588 | |
| 589 | // Shell service string can look like: shell[,arg1,arg2,...]:[command]. |
| 590 | return android::base::StringPrintf("shell%s%s:%s", |
| 591 | args.empty() ? "" : ",", |
| 592 | android::base::Join(args, ',').c_str(), |
| 593 | command.c_str()); |
| 594 | } |
| 595 | |
| 596 | // Connects to a shell on the device and read/writes data. |
| 597 | // |
| 598 | // Note: currently this function doesn't properly clean up resources; the |
| 599 | // FD connected to the adb server is never closed and the stdin read thread |
| 600 | // may never exit. |
| 601 | // |
| 602 | // On success returns the remote exit code if |use_shell_protocol| is true, |
| 603 | // 0 otherwise. On failure returns 1. |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 604 | static int RemoteShell(bool use_shell_protocol, const std::string& type_arg, char escape_char, |
| 605 | bool empty_command, const std::string& service_string) { |
Josh Gao | a019f78 | 2017-06-16 15:34:34 -0700 | [diff] [blame] | 606 | // Old devices can't handle a service string that's longer than MAX_PAYLOAD_V1. |
| 607 | // Use |use_shell_protocol| to determine whether to allow a command longer than that. |
| 608 | if (service_string.size() > MAX_PAYLOAD_V1 && !use_shell_protocol) { |
| 609 | fprintf(stderr, "error: shell command too long\n"); |
| 610 | return 1; |
| 611 | } |
| 612 | |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 613 | // Make local stdin raw if the device allocates a PTY, which happens if: |
| 614 | // 1. We are explicitly asking for a PTY shell, or |
| 615 | // 2. We don't specify shell type and are starting an interactive session. |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 616 | bool raw_stdin = (type_arg == kShellServiceArgPty || (type_arg.empty() && empty_command)); |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 617 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 618 | std::string error; |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 619 | int fd = adb_connect(service_string, &error); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 620 | if (fd < 0) { |
| 621 | fprintf(stderr,"error: %s\n", error.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 622 | return 1; |
| 623 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 624 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 625 | StdinReadArgs* args = new StdinReadArgs; |
| 626 | if (!args) { |
| 627 | LOG(ERROR) << "couldn't allocate StdinReadArgs object"; |
Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 628 | return 1; |
| 629 | } |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 630 | args->stdin_fd = STDIN_FILENO; |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 631 | args->write_fd = fd; |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 632 | args->raw_stdin = raw_stdin; |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 633 | args->escape_char = escape_char; |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 634 | if (use_shell_protocol) { |
Josh Gao | 31b5be6 | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 635 | args->protocol = std::make_unique<ShellProtocol>(args->write_fd); |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 636 | } |
Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 637 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 638 | if (raw_stdin) stdin_raw_init(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 639 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 640 | #if !defined(_WIN32) |
| 641 | // Ensure our process is notified if the local window size changes. |
| 642 | // We use sigaction(2) to ensure that the SA_RESTART flag is not set, |
| 643 | // because the whole reason we're sending signals is to unblock the read(2)! |
| 644 | // That also means we don't need to do anything in the signal handler: |
| 645 | // the side effect of delivering the signal is all we need. |
| 646 | struct sigaction sa; |
| 647 | memset(&sa, 0, sizeof(sa)); |
| 648 | sa.sa_handler = [](int) {}; |
| 649 | sa.sa_flags = 0; |
| 650 | sigaction(SIGWINCH, &sa, nullptr); |
| 651 | |
| 652 | // Now block SIGWINCH in this thread (the main thread) and all threads spawned |
| 653 | // from it. The stdin read thread will unblock this signal to ensure that it's |
| 654 | // the thread that receives the signal. |
| 655 | sigset_t mask; |
| 656 | sigemptyset(&mask); |
| 657 | sigaddset(&mask, SIGWINCH); |
| 658 | pthread_sigmask(SIG_BLOCK, &mask, nullptr); |
| 659 | #endif |
| 660 | |
| 661 | // TODO: combine read_and_dump with stdin_read_thread to make life simpler? |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 662 | std::thread(stdin_read_thread_loop, args).detach(); |
| 663 | int exit_code = read_and_dump(fd, use_shell_protocol); |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 664 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 665 | // TODO: properly exit stdin_read_thread_loop and close |fd|. |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 666 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 667 | // TODO: we should probably install signal handlers for this. |
| 668 | // TODO: can we use atexit? even on Windows? |
| 669 | if (raw_stdin) stdin_raw_restore(); |
David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 670 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 671 | return exit_code; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 674 | static int adb_shell(int argc, const char** argv) { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 675 | auto&& features = adb_get_feature_set(); |
| 676 | if (features.empty()) { |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 677 | return 1; |
| 678 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 679 | |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 680 | enum PtyAllocationMode { kPtyAuto, kPtyNo, kPtyYes, kPtyDefinitely }; |
| 681 | |
| 682 | // Defaults. |
| 683 | char escape_char = '~'; // -e |
| 684 | bool use_shell_protocol = CanUseFeature(features, kFeatureShell2); // -x |
| 685 | PtyAllocationMode tty = use_shell_protocol ? kPtyAuto : kPtyDefinitely; // -t/-T |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 686 | |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 687 | // Parse shell-specific command-line options. |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 688 | argv[0] = "adb shell"; // So getopt(3) error messages start "adb shell". |
Huihong Luo | af39885 | 2017-08-17 15:23:08 -0700 | [diff] [blame] | 689 | #ifdef _WIN32 |
| 690 | // fixes "adb shell -l" crash on Windows, b/37284906 |
| 691 | __argv = const_cast<char**>(argv); |
| 692 | #endif |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 693 | optind = 1; // argv[0] is always "shell", so set `optind` appropriately. |
| 694 | int opt; |
| 695 | while ((opt = getopt(argc, const_cast<char**>(argv), "+e:ntTx")) != -1) { |
| 696 | switch (opt) { |
| 697 | case 'e': |
| 698 | if (!(strlen(optarg) == 1 || strcmp(optarg, "none") == 0)) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 699 | error_exit("-e requires a single-character argument or 'none'"); |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 700 | } |
| 701 | escape_char = (strcmp(optarg, "none") == 0) ? 0 : optarg[0]; |
| 702 | break; |
| 703 | case 'n': |
| 704 | close_stdin(); |
| 705 | break; |
| 706 | case 'x': |
| 707 | // This option basically asks for historical behavior, so set options that |
| 708 | // correspond to the historical defaults. This is slightly weird in that -Tx |
| 709 | // is fine (because we'll undo the -T) but -xT isn't, but that does seem to |
| 710 | // be our least worst choice... |
| 711 | use_shell_protocol = false; |
| 712 | tty = kPtyDefinitely; |
| 713 | escape_char = '~'; |
| 714 | break; |
| 715 | case 't': |
| 716 | // Like ssh, -t arguments are cumulative so that multiple -t's |
| 717 | // are needed to force a PTY. |
| 718 | tty = (tty >= kPtyYes) ? kPtyDefinitely : kPtyYes; |
| 719 | break; |
| 720 | case 'T': |
| 721 | tty = kPtyNo; |
| 722 | break; |
| 723 | default: |
| 724 | // getopt(3) already printed an error message for us. |
Elliott Hughes | 28416d6 | 2015-11-04 13:36:32 -0800 | [diff] [blame] | 725 | return 1; |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 726 | } |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 727 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 728 | |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 729 | bool is_interactive = (optind == argc); |
David Pursell | 57dd5ae | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 730 | |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 731 | std::string shell_type_arg = kShellServiceArgPty; |
| 732 | if (tty == kPtyNo) { |
| 733 | shell_type_arg = kShellServiceArgRaw; |
| 734 | } else if (tty == kPtyAuto) { |
| 735 | // If stdin isn't a TTY, default to a raw shell; this lets |
| 736 | // things like `adb shell < my_script.sh` work as expected. |
| 737 | // Non-interactive shells should also not have a pty. |
| 738 | if (!unix_isatty(STDIN_FILENO) || !is_interactive) { |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 739 | shell_type_arg = kShellServiceArgRaw; |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 740 | } |
| 741 | } else if (tty == kPtyYes) { |
| 742 | // A single -t arg isn't enough to override implicit -T. |
| 743 | if (!unix_isatty(STDIN_FILENO)) { |
| 744 | fprintf(stderr, |
| 745 | "Remote PTY will not be allocated because stdin is not a terminal.\n" |
| 746 | "Use multiple -t options to force remote PTY allocation.\n"); |
| 747 | shell_type_arg = kShellServiceArgRaw; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | D("shell -e 0x%x t=%d use_shell_protocol=%s shell_type_arg=%s\n", |
| 752 | escape_char, tty, |
| 753 | use_shell_protocol ? "true" : "false", |
| 754 | (shell_type_arg == kShellServiceArgPty) ? "pty" : "raw"); |
| 755 | |
| 756 | // Raw mode is only supported when talking to a new device *and* using the shell protocol. |
| 757 | if (!use_shell_protocol) { |
| 758 | if (shell_type_arg != kShellServiceArgPty) { |
| 759 | fprintf(stderr, "error: %s only supports allocating a pty\n", |
| 760 | !CanUseFeature(features, kFeatureShell2) ? "device" : "-x"); |
| 761 | return 1; |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 762 | } else { |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 763 | // If we're not using the shell protocol, the type argument must be empty. |
| 764 | shell_type_arg = ""; |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 765 | } |
David Pursell | 71c8312 | 2015-09-14 15:33:50 -0700 | [diff] [blame] | 766 | } |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 767 | |
| 768 | std::string command; |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 769 | if (optind < argc) { |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 770 | // We don't escape here, just like ssh(1). http://b/20564385. |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 771 | command = android::base::Join(std::vector<const char*>(argv + optind, argv + argc), ' '); |
Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 774 | std::string service_string = ShellServiceString(use_shell_protocol, shell_type_arg, command); |
| 775 | return RemoteShell(use_shell_protocol, shell_type_arg, escape_char, command.empty(), |
| 776 | service_string); |
| 777 | } |
| 778 | |
| 779 | static int adb_abb(int argc, const char** argv) { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 780 | auto&& features = adb_get_feature_set(); |
| 781 | if (features.empty()) { |
Alex Buynytskyy | 01a65ee | 2019-01-17 13:13:56 -0800 | [diff] [blame] | 782 | return 1; |
| 783 | } |
Alex Buynytskyy | 01a65ee | 2019-01-17 13:13:56 -0800 | [diff] [blame] | 784 | if (!CanUseFeature(features, kFeatureAbb)) { |
| 785 | error_exit("abb is not supported by the device"); |
| 786 | } |
| 787 | |
Alex Buynytskyy | cc2f124 | 2019-06-27 10:33:54 -0700 | [diff] [blame] | 788 | optind = 1; // argv[0] is always "abb", so set `optind` appropriately. |
| 789 | |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 790 | // Defaults. |
| 791 | constexpr char escape_char = '~'; // -e |
| 792 | constexpr bool use_shell_protocol = true; |
| 793 | constexpr auto shell_type_arg = kShellServiceArgRaw; |
| 794 | constexpr bool empty_command = false; |
| 795 | |
Alex Buynytskyy | cc2f124 | 2019-06-27 10:33:54 -0700 | [diff] [blame] | 796 | std::vector<const char*> args(argv + optind, argv + argc); |
| 797 | std::string service_string = "abb:" + android::base::Join(args, ABB_ARG_DELIMETER); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 798 | |
| 799 | D("abb -e 0x%x [%*.s]\n", escape_char, static_cast<int>(service_string.size()), |
| 800 | service_string.data()); |
| 801 | |
| 802 | return RemoteShell(use_shell_protocol, shell_type_arg, escape_char, empty_command, |
| 803 | service_string); |
David Pursell | 71c8312 | 2015-09-14 15:33:50 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Josh Gao | 9e75634 | 2020-01-08 12:39:52 -0800 | [diff] [blame] | 806 | static int adb_shell_noinput(int argc, const char** argv) { |
| 807 | #if !defined(_WIN32) |
| 808 | unique_fd fd(adb_open("/dev/null", O_RDONLY)); |
| 809 | CHECK_NE(STDIN_FILENO, fd.get()); |
| 810 | dup2(fd.get(), STDIN_FILENO); |
| 811 | #endif |
| 812 | return adb_shell(argc, argv); |
| 813 | } |
| 814 | |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 815 | static int adb_sideload_legacy(const char* filename, int in_fd, int size) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 816 | std::string error; |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 817 | unique_fd out_fd(adb_connect(android::base::StringPrintf("sideload:%d", size), &error)); |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 818 | if (out_fd < 0) { |
| 819 | fprintf(stderr, "adb: pre-KitKat sideload connection failed: %s\n", error.c_str()); |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 820 | return -1; |
| 821 | } |
| 822 | |
| 823 | int opt = CHUNK_SIZE; |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 824 | opt = adb_setsockopt(out_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)); |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 825 | |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 826 | char buf[CHUNK_SIZE]; |
| 827 | int total = size; |
| 828 | while (size > 0) { |
| 829 | unsigned xfer = (size > CHUNK_SIZE) ? CHUNK_SIZE : size; |
| 830 | if (!ReadFdExactly(in_fd, buf, xfer)) { |
| 831 | fprintf(stderr, "adb: failed to read data from %s: %s\n", filename, strerror(errno)); |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 832 | return -1; |
| 833 | } |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 834 | if (!WriteFdExactly(out_fd, buf, xfer)) { |
| 835 | std::string error; |
| 836 | adb_status(out_fd, &error); |
| 837 | fprintf(stderr, "adb: failed to write data: %s\n", error.c_str()); |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 838 | return -1; |
| 839 | } |
| 840 | size -= xfer; |
| 841 | printf("sending: '%s' %4d%% \r", filename, (int)(100LL - ((100LL * size) / (total)))); |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 842 | fflush(stdout); |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 843 | } |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 844 | printf("\n"); |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 845 | |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 846 | if (!adb_status(out_fd, &error)) { |
| 847 | fprintf(stderr, "adb: error response: %s\n", error.c_str()); |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 848 | return -1; |
| 849 | } |
| 850 | |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 854 | #define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE) |
| 855 | |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 856 | // Connects to the sideload / rescue service on the device (served by minadbd) and sends over the |
| 857 | // data in an OTA package. |
| 858 | // |
| 859 | // It uses a simple protocol as follows. |
| 860 | // |
| 861 | // - The connect message includes the total number of bytes in the file and a block size chosen by |
| 862 | // us. |
| 863 | // |
| 864 | // - The other side sends the desired block number as eight decimal digits (e.g. "00000023" for |
| 865 | // block 23). Blocks are numbered from zero. |
| 866 | // |
| 867 | // - We send back the data of the requested block. The last block is likely to be partial; when the |
| 868 | // last block is requested we only send the part of the block that exists, it's not padded up to |
| 869 | // the block size. |
| 870 | // |
| 871 | // - When the other side sends "DONEDONE" or "FAILFAIL" instead of a block number, we have done all |
| 872 | // the data transfer. |
| 873 | // |
| 874 | static int adb_sideload_install(const char* filename, bool rescue_mode) { |
Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 875 | // TODO: use a LinePrinter instead... |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 876 | struct stat sb; |
| 877 | if (stat(filename, &sb) == -1) { |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 878 | fprintf(stderr, "adb: failed to stat file %s: %s\n", filename, strerror(errno)); |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 879 | return -1; |
| 880 | } |
| 881 | unique_fd package_fd(adb_open(filename, O_RDONLY)); |
| 882 | if (package_fd == -1) { |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 883 | fprintf(stderr, "adb: failed to open file %s: %s\n", filename, strerror(errno)); |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 884 | return -1; |
| 885 | } |
| 886 | |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 887 | std::string service = android::base::StringPrintf( |
| 888 | "%s:%" PRId64 ":%d", rescue_mode ? "rescue-install" : "sideload-host", |
| 889 | static_cast<int64_t>(sb.st_size), SIDELOAD_HOST_BLOCK_SIZE); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 890 | std::string error; |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 891 | unique_fd device_fd(adb_connect(service, &error)); |
| 892 | if (device_fd < 0) { |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 893 | fprintf(stderr, "adb: sideload connection failed: %s\n", error.c_str()); |
Elliott Hughes | cabfc3d | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 894 | |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 895 | if (rescue_mode) { |
| 896 | return -1; |
| 897 | } |
| 898 | |
Elliott Hughes | cabfc3d | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 899 | // If this is a small enough package, maybe this is an older device that doesn't |
| 900 | // support sideload-host. Try falling back to the older (<= K) sideload method. |
| 901 | if (sb.st_size > INT_MAX) { |
| 902 | return -1; |
| 903 | } |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 904 | fprintf(stderr, "adb: trying pre-KitKat sideload method...\n"); |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 905 | return adb_sideload_legacy(filename, package_fd.get(), static_cast<int>(sb.st_size)); |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 908 | int opt = SIDELOAD_HOST_BLOCK_SIZE; |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 909 | adb_setsockopt(device_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)); |
| 910 | |
| 911 | char buf[SIDELOAD_HOST_BLOCK_SIZE]; |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 912 | |
Elliott Hughes | cabfc3d | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 913 | int64_t xfer = 0; |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 914 | int last_percent = -1; |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 915 | while (true) { |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 916 | if (!ReadFdExactly(device_fd, buf, 8)) { |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 917 | fprintf(stderr, "adb: failed to read command: %s\n", strerror(errno)); |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 918 | return -1; |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 919 | } |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 920 | buf[8] = '\0'; |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 921 | |
xunchang | c2265c5 | 2019-04-22 23:14:28 -0700 | [diff] [blame] | 922 | if (strcmp(kMinadbdServicesExitSuccess, buf) == 0 || |
| 923 | strcmp(kMinadbdServicesExitFailure, buf) == 0) { |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 924 | printf("\rTotal xfer: %.2fx%*s\n", |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 925 | static_cast<double>(xfer) / (sb.st_size ? sb.st_size : 1), |
| 926 | static_cast<int>(strlen(filename) + 10), ""); |
xunchang | c2265c5 | 2019-04-22 23:14:28 -0700 | [diff] [blame] | 927 | if (strcmp(kMinadbdServicesExitFailure, buf) == 0) { |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 928 | return 1; |
| 929 | } |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 930 | return 0; |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Elliott Hughes | cabfc3d | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 933 | int64_t block = strtoll(buf, nullptr, 10); |
| 934 | int64_t offset = block * SIDELOAD_HOST_BLOCK_SIZE; |
| 935 | if (offset >= static_cast<int64_t>(sb.st_size)) { |
| 936 | fprintf(stderr, |
| 937 | "adb: failed to read block %" PRId64 " at offset %" PRId64 ", past end %" PRId64 |
| 938 | "\n", |
| 939 | block, offset, static_cast<int64_t>(sb.st_size)); |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 940 | return -1; |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 941 | } |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 942 | |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 943 | size_t to_write = SIDELOAD_HOST_BLOCK_SIZE; |
Elliott Hughes | cabfc3d | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 944 | if ((offset + SIDELOAD_HOST_BLOCK_SIZE) > static_cast<int64_t>(sb.st_size)) { |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 945 | to_write = sb.st_size - offset; |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Elliott Hughes | cabfc3d | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 948 | if (adb_lseek(package_fd, offset, SEEK_SET) != offset) { |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 949 | fprintf(stderr, "adb: failed to seek to package block: %s\n", strerror(errno)); |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 950 | return -1; |
| 951 | } |
| 952 | if (!ReadFdExactly(package_fd, buf, to_write)) { |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 953 | fprintf(stderr, "adb: failed to read package block: %s\n", strerror(errno)); |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | if (!WriteFdExactly(device_fd, buf, to_write)) { |
| 958 | adb_status(device_fd, &error); |
Elliott Hughes | 09e794c | 2017-05-02 14:31:59 -0700 | [diff] [blame] | 959 | fprintf(stderr, "adb: failed to write data '%s' *\n", error.c_str()); |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 960 | return -1; |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 961 | } |
| 962 | xfer += to_write; |
| 963 | |
| 964 | // For normal OTA packages, we expect to transfer every byte |
| 965 | // twice, plus a bit of overhead (one read during |
| 966 | // verification, one read of each byte for installation, plus |
| 967 | // extra access to things like the zip central directory). |
| 968 | // This estimate of the completion becomes 100% when we've |
| 969 | // transferred ~2.13 (=100/47) times the package size. |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 970 | int percent = static_cast<int>(xfer * 47LL / (sb.st_size ? sb.st_size : 1)); |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 971 | if (percent != last_percent) { |
Elliott Hughes | ee1ac710 | 2017-03-21 17:14:18 -0700 | [diff] [blame] | 972 | printf("\rserving: '%s' (~%d%%) ", filename, percent); |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 973 | fflush(stdout); |
| 974 | last_percent = percent; |
| 975 | } |
| 976 | } |
Doug Zongker | 71fe584 | 2014-06-26 15:35:36 -0700 | [diff] [blame] | 977 | } |
| 978 | |
xunchang | c2265c5 | 2019-04-22 23:14:28 -0700 | [diff] [blame] | 979 | static int adb_wipe_devices() { |
| 980 | auto wipe_devices_message_size = strlen(kMinadbdServicesExitSuccess); |
| 981 | std::string error; |
| 982 | unique_fd fd(adb_connect( |
| 983 | android::base::StringPrintf("rescue-wipe:userdata:%zu", wipe_devices_message_size), |
| 984 | &error)); |
| 985 | if (fd < 0) { |
| 986 | fprintf(stderr, "adb: wipe device connection failed: %s\n", error.c_str()); |
| 987 | return 1; |
| 988 | } |
| 989 | |
| 990 | std::string message(wipe_devices_message_size, '\0'); |
| 991 | if (!ReadFdExactly(fd, message.data(), wipe_devices_message_size)) { |
| 992 | fprintf(stderr, "adb: failed to read wipe result: %s\n", strerror(errno)); |
| 993 | return 1; |
| 994 | } |
| 995 | |
| 996 | if (message == kMinadbdServicesExitSuccess) { |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | if (message != kMinadbdServicesExitFailure) { |
| 1001 | fprintf(stderr, "adb: got unexpected message from rescue wipe %s\n", message.c_str()); |
| 1002 | } |
| 1003 | return 1; |
| 1004 | } |
| 1005 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1006 | /** |
| 1007 | * Run ppp in "notty" mode against a resource listed as the first parameter |
| 1008 | * eg: |
| 1009 | * |
| 1010 | * ppp dev:/dev/omap_csmi_tty0 <ppp options> |
| 1011 | * |
| 1012 | */ |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 1013 | static int ppp(int argc, const char** argv) { |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1014 | #if defined(_WIN32) |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1015 | error_exit("adb %s not implemented on Win32", argv[0]); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1016 | __builtin_unreachable(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1017 | #else |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1018 | if (argc < 2) error_exit("usage: adb %s <adb service name> [ppp opts]", argv[0]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1019 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 1020 | const char* adb_service_name = argv[1]; |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1021 | std::string error_message; |
| 1022 | int fd = adb_connect(adb_service_name, &error_message); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1023 | if (fd < 0) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1024 | error_exit("could not open adb service %s: %s", adb_service_name, error_message.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1027 | pid_t pid = fork(); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1028 | if (pid == -1) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1029 | perror_exit("fork failed"); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1030 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1031 | |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1032 | if (pid == 0) { |
| 1033 | // child side |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1034 | int i; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1035 | |
| 1036 | // copy args |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1037 | const char** ppp_args = (const char**)alloca(sizeof(char*) * argc + 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1038 | ppp_args[0] = "pppd"; |
| 1039 | for (i = 2 ; i < argc ; i++) { |
| 1040 | //argv[2] and beyond become ppp_args[1] and beyond |
| 1041 | ppp_args[i - 1] = argv[i]; |
| 1042 | } |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 1043 | ppp_args[i-1] = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1044 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1045 | dup2(fd, STDIN_FILENO); |
| 1046 | dup2(fd, STDOUT_FILENO); |
| 1047 | adb_close(STDERR_FILENO); |
| 1048 | adb_close(fd); |
| 1049 | |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1050 | execvp("pppd", (char* const*)ppp_args); |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1051 | perror_exit("exec pppd failed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1052 | } |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1053 | |
| 1054 | // parent side |
| 1055 | adb_close(fd); |
| 1056 | return 0; |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1057 | #endif /* !defined(_WIN32) */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Josh Gao | 3c99b0f | 2019-03-11 15:39:44 -0700 | [diff] [blame] | 1060 | static bool wait_for_device(const char* service, |
| 1061 | std::optional<std::chrono::milliseconds> timeout = std::nullopt) { |
Josh Gao | 08881e7 | 2016-04-13 12:14:16 -0700 | [diff] [blame] | 1062 | std::vector<std::string> components = android::base::Split(service, "-"); |
Elliott Hughes | a52b458 | 2020-03-10 16:57:47 -0700 | [diff] [blame] | 1063 | if (components.size() < 3) { |
Leo Sartre | 1fbc9db | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 1064 | fprintf(stderr, "adb: couldn't parse 'wait-for' command: %s\n", service); |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
Elliott Hughes | a52b458 | 2020-03-10 16:57:47 -0700 | [diff] [blame] | 1068 | // If the first thing after "wait-for-" wasn't a TRANSPORT, insert whatever |
| 1069 | // the current transport implies. |
| 1070 | if (components[2] != "usb" && components[2] != "local" && components[2] != "any") { |
| 1071 | TransportType t; |
| 1072 | adb_get_transport(&t, nullptr, nullptr); |
Josh Gao | 08881e7 | 2016-04-13 12:14:16 -0700 | [diff] [blame] | 1073 | auto it = components.begin() + 2; |
Elliott Hughes | 2b10111 | 2015-05-04 19:29:32 -0700 | [diff] [blame] | 1074 | if (t == kTransportUsb) { |
Josh Gao | 08881e7 | 2016-04-13 12:14:16 -0700 | [diff] [blame] | 1075 | components.insert(it, "usb"); |
Elliott Hughes | 2b10111 | 2015-05-04 19:29:32 -0700 | [diff] [blame] | 1076 | } else if (t == kTransportLocal) { |
Josh Gao | 08881e7 | 2016-04-13 12:14:16 -0700 | [diff] [blame] | 1077 | components.insert(it, "local"); |
Elliott Hughes | 2b10111 | 2015-05-04 19:29:32 -0700 | [diff] [blame] | 1078 | } else { |
Josh Gao | 08881e7 | 2016-04-13 12:14:16 -0700 | [diff] [blame] | 1079 | components.insert(it, "any"); |
Elliott Hughes | 2b10111 | 2015-05-04 19:29:32 -0700 | [diff] [blame] | 1080 | } |
Leo Sartre | 1fbc9db | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
Elliott Hughes | a52b458 | 2020-03-10 16:57:47 -0700 | [diff] [blame] | 1083 | // Stitch it back together and send it over... |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1084 | std::string cmd = format_host_command(android::base::Join(components, "-").c_str()); |
Josh Gao | 3c99b0f | 2019-03-11 15:39:44 -0700 | [diff] [blame] | 1085 | if (timeout) { |
| 1086 | std::thread([timeout]() { |
| 1087 | std::this_thread::sleep_for(*timeout); |
| 1088 | fprintf(stderr, "timeout expired while waiting for device\n"); |
| 1089 | _exit(1); |
| 1090 | }).detach(); |
| 1091 | } |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1092 | return adb_command(cmd); |
Elliott Hughes | 2b10111 | 2015-05-04 19:29:32 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1095 | static bool adb_root(const char* command) { |
| 1096 | std::string error; |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1097 | |
Josh Gao | 2020dd6 | 2019-02-22 15:20:27 -0800 | [diff] [blame] | 1098 | TransportId transport_id; |
| 1099 | unique_fd fd(adb_connect(&transport_id, android::base::StringPrintf("%s:", command), &error)); |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 1100 | if (fd < 0) { |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1101 | fprintf(stderr, "adb: unable to connect for %s: %s\n", command, error.c_str()); |
| 1102 | return false; |
| 1103 | } |
| 1104 | |
| 1105 | // Figure out whether we actually did anything. |
| 1106 | char buf[256]; |
| 1107 | char* cur = buf; |
| 1108 | ssize_t bytes_left = sizeof(buf); |
| 1109 | while (bytes_left > 0) { |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 1110 | ssize_t bytes_read = adb_read(fd, cur, bytes_left); |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1111 | if (bytes_read == 0) { |
| 1112 | break; |
| 1113 | } else if (bytes_read < 0) { |
| 1114 | fprintf(stderr, "adb: error while reading for %s: %s\n", command, strerror(errno)); |
| 1115 | return false; |
| 1116 | } |
| 1117 | cur += bytes_read; |
| 1118 | bytes_left -= bytes_read; |
| 1119 | } |
| 1120 | |
| 1121 | if (bytes_left == 0) { |
| 1122 | fprintf(stderr, "adb: unexpected output length for %s\n", command); |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Junyong Sun | 1ffccf5 | 2020-01-15 16:34:58 +0800 | [diff] [blame] | 1126 | fwrite(buf, 1, sizeof(buf) - bytes_left, stdout); |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1127 | fflush(stdout); |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1128 | if (cur != buf && strstr(buf, "restarting") == nullptr) { |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
Josh Gao | 2020dd6 | 2019-02-22 15:20:27 -0800 | [diff] [blame] | 1132 | // Wait for the device to go away. |
Josh Gao | 3c99b0f | 2019-03-11 15:39:44 -0700 | [diff] [blame] | 1133 | TransportType previous_type; |
| 1134 | const char* previous_serial; |
| 1135 | TransportId previous_id; |
| 1136 | adb_get_transport(&previous_type, &previous_serial, &previous_id); |
| 1137 | |
Josh Gao | 2020dd6 | 2019-02-22 15:20:27 -0800 | [diff] [blame] | 1138 | adb_set_transport(kTransportAny, nullptr, transport_id); |
| 1139 | wait_for_device("wait-for-disconnect"); |
Josh Gao | 3c99b0f | 2019-03-11 15:39:44 -0700 | [diff] [blame] | 1140 | |
| 1141 | // Wait for the device to come back. |
| 1142 | // If we were using a specific transport ID, there's nothing we can wait for. |
| 1143 | if (previous_id == 0) { |
| 1144 | adb_set_transport(previous_type, previous_serial, 0); |
Josh Gao | ef4a999 | 2019-05-01 18:51:46 -0700 | [diff] [blame] | 1145 | wait_for_device("wait-for-device", 6000ms); |
Josh Gao | 3c99b0f | 2019-03-11 15:39:44 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Josh Gao | 0582473 | 2016-06-16 14:00:09 -0700 | [diff] [blame] | 1148 | return true; |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1151 | int send_shell_command(const std::string& command, bool disable_shell_protocol, |
| 1152 | StandardStreamsCallbackInterface* callback) { |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1153 | unique_fd fd; |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 1154 | bool use_shell_protocol = false; |
| 1155 | |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 1156 | while (true) { |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 1157 | bool attempt_connection = true; |
| 1158 | |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 1159 | // Use shell protocol if it's supported and the caller doesn't explicitly |
| 1160 | // disable it. |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 1161 | if (!disable_shell_protocol) { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 1162 | auto&& features = adb_get_feature_set(); |
| 1163 | if (!features.empty()) { |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 1164 | use_shell_protocol = CanUseFeature(features, kFeatureShell2); |
| 1165 | } else { |
| 1166 | // Device was unreachable. |
| 1167 | attempt_connection = false; |
| 1168 | } |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1169 | } |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 1170 | |
| 1171 | if (attempt_connection) { |
| 1172 | std::string error; |
| 1173 | std::string service_string = ShellServiceString(use_shell_protocol, "", command); |
| 1174 | |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1175 | fd.reset(adb_connect(service_string, &error)); |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 1176 | if (fd >= 0) { |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 1181 | fprintf(stderr, "- waiting for device -\n"); |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1182 | if (!wait_for_device("wait-for-device")) { |
Josh Gao | b6117c4 | 2016-02-24 15:16:17 -0800 | [diff] [blame] | 1183 | return 1; |
| 1184 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1187 | return read_and_dump(fd.get(), use_shell_protocol, callback); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1188 | } |
| 1189 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1190 | static int logcat(int argc, const char** argv) { |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 1191 | char* log_tags = getenv("ANDROID_LOG_TAGS"); |
| 1192 | std::string quoted = escape_arg(log_tags == nullptr ? "" : log_tags); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1193 | |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 1194 | std::string cmd = "export ANDROID_LOG_TAGS=\"" + quoted + "\"; exec logcat"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1195 | |
Jeff Sharkey | fd546e8 | 2014-06-10 11:31:24 -0700 | [diff] [blame] | 1196 | if (!strcmp(argv[0], "longcat")) { |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 1197 | cmd += " -v long"; |
Christopher Tate | db0a880 | 2011-11-30 13:00:33 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Elliott Hughes | 6c34bba | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 1200 | --argc; |
| 1201 | ++argv; |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 1202 | while (argc-- > 0) { |
Elliott Hughes | 6c34bba | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 1203 | cmd += " " + escape_arg(*argv++); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Elliott Hughes | 31f0f1b | 2018-06-14 10:46:05 -0700 | [diff] [blame] | 1206 | return send_shell_command(cmd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1209 | static void write_zeros(int bytes, borrowed_fd fd) { |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1210 | int old_stdin_mode = -1; |
| 1211 | int old_stdout_mode = -1; |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1212 | std::vector<char> buf(bytes); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1213 | |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1214 | D("write_zeros(%d) -> %d", bytes, fd.get()); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1215 | |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1216 | stdinout_raw_prologue(-1, fd.get(), old_stdin_mode, old_stdout_mode); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1217 | |
| 1218 | if (fd == STDOUT_FILENO) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1219 | fwrite(buf.data(), 1, bytes, stdout); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1220 | fflush(stdout); |
| 1221 | } else { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1222 | adb_write(fd, buf.data(), bytes); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1225 | stdinout_raw_prologue(-1, fd.get(), old_stdin_mode, old_stdout_mode); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1226 | |
| 1227 | D("write_zeros() finished"); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 1230 | static int backup(int argc, const char** argv) { |
Al Sutton | 31d335f | 2019-05-09 16:14:05 +0000 | [diff] [blame] | 1231 | fprintf(stdout, "WARNING: adb backup is deprecated and may be removed in a future release\n"); |
| 1232 | |
Elliott Hughes | 24f165f | 2015-08-21 20:31:31 -0700 | [diff] [blame] | 1233 | const char* filename = "backup.ab"; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1234 | |
Christopher Tate | c9cd3b9 | 2011-06-01 17:56:23 -0700 | [diff] [blame] | 1235 | /* find, extract, and use any -f argument */ |
Elliott Hughes | 6c34bba | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 1236 | for (int i = 1; i < argc; i++) { |
Christopher Tate | c9cd3b9 | 2011-06-01 17:56:23 -0700 | [diff] [blame] | 1237 | if (!strcmp("-f", argv[i])) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1238 | if (i == argc - 1) error_exit("backup -f passed with no filename"); |
Christopher Tate | c9cd3b9 | 2011-06-01 17:56:23 -0700 | [diff] [blame] | 1239 | filename = argv[i+1]; |
Elliott Hughes | 6c34bba | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 1240 | for (int j = i+2; j <= argc; ) { |
Christopher Tate | c9cd3b9 | 2011-06-01 17:56:23 -0700 | [diff] [blame] | 1241 | argv[i++] = argv[j++]; |
| 1242 | } |
| 1243 | argc -= 2; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 1244 | argv[argc] = nullptr; |
Christopher Tate | c9cd3b9 | 2011-06-01 17:56:23 -0700 | [diff] [blame] | 1245 | } |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Elliott Hughes | 56e6813 | 2015-11-13 11:04:10 -0800 | [diff] [blame] | 1248 | // Bare "adb backup" or "adb backup -f filename" are not valid invocations --- |
| 1249 | // a list of packages is required. |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1250 | if (argc < 2) error_exit("backup either needs a list of packages or -all/-shared"); |
Christopher Tate | bb86bc5 | 2011-08-22 17:12:08 -0700 | [diff] [blame] | 1251 | |
Christopher Tate | b1dfffe | 2011-12-08 19:04:34 -0800 | [diff] [blame] | 1252 | adb_unlink(filename); |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1253 | unique_fd outFd(adb_creat(filename, 0640)); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1254 | if (outFd < 0) { |
Elliott Hughes | 56e6813 | 2015-11-13 11:04:10 -0800 | [diff] [blame] | 1255 | fprintf(stderr, "adb: backup unable to create file '%s': %s\n", filename, strerror(errno)); |
| 1256 | return EXIT_FAILURE; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Elliott Hughes | 6c34bba | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 1259 | std::string cmd = "backup:"; |
| 1260 | --argc; |
| 1261 | ++argv; |
| 1262 | while (argc-- > 0) { |
| 1263 | cmd += " " + escape_arg(*argv++); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 1266 | D("backup. filename=%s cmd=%s", filename, cmd.c_str()); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1267 | std::string error; |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1268 | unique_fd fd(adb_connect(cmd, &error)); |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1269 | if (fd < 0) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1270 | fprintf(stderr, "adb: unable to connect for backup: %s\n", error.c_str()); |
Elliott Hughes | 56e6813 | 2015-11-13 11:04:10 -0800 | [diff] [blame] | 1271 | return EXIT_FAILURE; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 1274 | fprintf(stdout, "Now unlock your device and confirm the backup operation...\n"); |
Elliott Hughes | 56e6813 | 2015-11-13 11:04:10 -0800 | [diff] [blame] | 1275 | fflush(stdout); |
| 1276 | |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1277 | copy_to_file(fd.get(), outFd.get()); |
Elliott Hughes | 56e6813 | 2015-11-13 11:04:10 -0800 | [diff] [blame] | 1278 | return EXIT_SUCCESS; |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 1281 | static int restore(int argc, const char** argv) { |
Al Sutton | 31d335f | 2019-05-09 16:14:05 +0000 | [diff] [blame] | 1282 | fprintf(stdout, "WARNING: adb restore is deprecated and may be removed in a future release\n"); |
| 1283 | |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1284 | if (argc != 2) error_exit("restore requires an argument"); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 1285 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1286 | const char* filename = argv[1]; |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1287 | unique_fd tarFd(adb_open(filename, O_RDONLY)); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 1288 | if (tarFd < 0) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1289 | fprintf(stderr, "adb: unable to open file %s: %s\n", filename, strerror(errno)); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 1290 | return -1; |
| 1291 | } |
| 1292 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1293 | std::string error; |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1294 | unique_fd fd(adb_connect("restore:", &error)); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 1295 | if (fd < 0) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1296 | fprintf(stderr, "adb: unable to connect for restore: %s\n", error.c_str()); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 1297 | return -1; |
| 1298 | } |
| 1299 | |
Elliott Hughes | 1fc8f6e | 2017-04-18 14:34:16 -0700 | [diff] [blame] | 1300 | fprintf(stdout, "Now unlock your device and confirm the restore operation.\n"); |
| 1301 | fflush(stdout); |
| 1302 | |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1303 | copy_to_file(tarFd.get(), fd.get()); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 1304 | |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1305 | // Provide an in-band EOD marker in case the archive file is malformed |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1306 | write_zeros(512 * 2, fd); |
Christopher Tate | c42f1bb | 2016-07-06 13:22:22 -0700 | [diff] [blame] | 1307 | |
Josh Gao | 5767d0d | 2016-03-04 15:51:03 -0800 | [diff] [blame] | 1308 | // Wait until the other side finishes, or it'll get sent SIGHUP. |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1309 | copy_to_file(fd.get(), STDOUT_FILENO); |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 1310 | return 0; |
| 1311 | } |
| 1312 | |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1313 | static CompressionType parse_compression_type(const std::string& str, bool allow_numbers) { |
| 1314 | if (allow_numbers) { |
| 1315 | if (str == "0") { |
| 1316 | return CompressionType::None; |
| 1317 | } else if (str == "1") { |
| 1318 | return CompressionType::Any; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | if (str == "any") { |
| 1323 | return CompressionType::Any; |
| 1324 | } else if (str == "none") { |
| 1325 | return CompressionType::None; |
| 1326 | } |
| 1327 | |
| 1328 | if (str == "brotli") { |
| 1329 | return CompressionType::Brotli; |
Josh Gao | ec44d35 | 2020-03-26 22:02:03 -0700 | [diff] [blame] | 1330 | } else if (str == "lz4") { |
| 1331 | return CompressionType::LZ4; |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | error_exit("unexpected compression type %s", str.c_str()); |
| 1335 | } |
| 1336 | |
Dan Albert | 06b0d6b | 2017-05-18 22:56:48 -0700 | [diff] [blame] | 1337 | static void parse_push_pull_args(const char** arg, int narg, std::vector<const char*>* srcs, |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1338 | const char** dst, bool* copy_attrs, bool* sync, |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1339 | CompressionType* compression, bool* dry_run) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1340 | *copy_attrs = false; |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1341 | if (const char* adb_compression = getenv("ADB_COMPRESSION")) { |
| 1342 | *compression = parse_compression_type(adb_compression, true); |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1343 | } |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 1344 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1345 | srcs->clear(); |
| 1346 | bool ignore_flags = false; |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 1347 | while (narg > 0) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1348 | if (ignore_flags || *arg[0] != '-') { |
| 1349 | srcs->push_back(*arg); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 1350 | } else { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1351 | if (!strcmp(*arg, "-p")) { |
| 1352 | // Silently ignore for backwards compatibility. |
| 1353 | } else if (!strcmp(*arg, "-a")) { |
| 1354 | *copy_attrs = true; |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1355 | } else if (!strcmp(*arg, "-z")) { |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1356 | if (narg < 2) { |
| 1357 | error_exit("-z requires an argument"); |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1358 | } |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1359 | *compression = parse_compression_type(*++arg, false); |
| 1360 | --narg; |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1361 | } else if (!strcmp(*arg, "-Z")) { |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1362 | *compression = CompressionType::None; |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1363 | } else if (dry_run && !strcmp(*arg, "-n")) { |
| 1364 | *dry_run = true; |
Dan Albert | 06b0d6b | 2017-05-18 22:56:48 -0700 | [diff] [blame] | 1365 | } else if (!strcmp(*arg, "--sync")) { |
| 1366 | if (sync != nullptr) { |
| 1367 | *sync = true; |
| 1368 | } |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1369 | } else if (!strcmp(*arg, "--")) { |
| 1370 | ignore_flags = true; |
| 1371 | } else { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1372 | error_exit("unrecognized option '%s'", *arg); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1373 | } |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 1374 | } |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 1375 | ++arg; |
| 1376 | --narg; |
| 1377 | } |
| 1378 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1379 | if (srcs->size() > 1) { |
| 1380 | *dst = srcs->back(); |
| 1381 | srcs->pop_back(); |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 1385 | static int adb_connect_command(const std::string& command, TransportId* transport, |
| 1386 | StandardStreamsCallbackInterface* callback) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1387 | std::string error; |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 1388 | unique_fd fd(adb_connect(transport, command, &error)); |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 1389 | if (fd < 0) { |
| 1390 | fprintf(stderr, "error: %s\n", error.c_str()); |
| 1391 | return 1; |
Tao Bao | 175b7bb | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 1392 | } |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 1393 | read_and_dump(fd, false, callback); |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 1394 | return 0; |
Tao Bao | 175b7bb | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 1397 | static int adb_connect_command(const std::string& command, TransportId* transport = nullptr) { |
| 1398 | return adb_connect_command(command, transport, &DEFAULT_STANDARD_STREAMS_CALLBACK); |
| 1399 | } |
| 1400 | |
| 1401 | // A class that prints out human readable form of the protobuf message for "track-app" service |
| 1402 | // (received in binary format). |
| 1403 | class TrackAppStreamsCallback : public DefaultStandardStreamsCallback { |
| 1404 | public: |
| 1405 | TrackAppStreamsCallback() : DefaultStandardStreamsCallback(nullptr, nullptr) {} |
| 1406 | |
| 1407 | // Assume the buffer contains at least 4 bytes of valid data. |
| 1408 | void OnStdout(const char* buffer, int length) override { |
| 1409 | if (length < 4) return; // Unexpected length received. Do nothing. |
| 1410 | |
| 1411 | adb::proto::AppProcesses binary_proto; |
| 1412 | // The first 4 bytes are the length of remaining content in hexadecimal format. |
| 1413 | binary_proto.ParseFromString(std::string(buffer + 4, length - 4)); |
| 1414 | char summary[24]; // The following string includes digits and 16 fixed characters. |
| 1415 | int written = snprintf(summary, sizeof(summary), "Process count: %d\n", |
| 1416 | binary_proto.process_size()); |
| 1417 | OnStream(nullptr, stdout, summary, written); |
| 1418 | |
| 1419 | std::string string_proto; |
| 1420 | google::protobuf::TextFormat::PrintToString(binary_proto, &string_proto); |
| 1421 | OnStream(nullptr, stdout, string_proto.data(), string_proto.length()); |
| 1422 | } |
| 1423 | |
| 1424 | private: |
| 1425 | DISALLOW_COPY_AND_ASSIGN(TrackAppStreamsCallback); |
| 1426 | }; |
| 1427 | |
Josh Gao | e89a55d | 2018-11-16 14:47:59 -0800 | [diff] [blame] | 1428 | static int adb_connect_command_bidirectional(const std::string& command) { |
| 1429 | std::string error; |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1430 | unique_fd fd(adb_connect(command, &error)); |
Josh Gao | e89a55d | 2018-11-16 14:47:59 -0800 | [diff] [blame] | 1431 | if (fd < 0) { |
| 1432 | fprintf(stderr, "error: %s\n", error.c_str()); |
| 1433 | return 1; |
| 1434 | } |
| 1435 | |
| 1436 | static constexpr auto forward = [](int src, int sink, bool exit_on_end) { |
| 1437 | char buf[4096]; |
| 1438 | while (true) { |
| 1439 | int rc = adb_read(src, buf, sizeof(buf)); |
| 1440 | if (rc == 0) { |
| 1441 | if (exit_on_end) { |
| 1442 | exit(0); |
| 1443 | } else { |
| 1444 | adb_shutdown(sink, SHUT_WR); |
| 1445 | } |
| 1446 | return; |
| 1447 | } else if (rc < 0) { |
| 1448 | perror_exit("read failed"); |
| 1449 | } |
| 1450 | if (!WriteFdExactly(sink, buf, rc)) { |
| 1451 | perror_exit("write failed"); |
| 1452 | } |
| 1453 | } |
| 1454 | }; |
| 1455 | |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1456 | std::thread read(forward, fd.get(), STDOUT_FILENO, true); |
| 1457 | std::thread write(forward, STDIN_FILENO, fd.get(), false); |
Josh Gao | e89a55d | 2018-11-16 14:47:59 -0800 | [diff] [blame] | 1458 | read.join(); |
| 1459 | write.join(); |
Josh Gao | e89a55d | 2018-11-16 14:47:59 -0800 | [diff] [blame] | 1460 | return 0; |
| 1461 | } |
| 1462 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1463 | static int adb_query_command(const std::string& command) { |
| 1464 | std::string result; |
| 1465 | std::string error; |
| 1466 | if (!adb_query(command, &result, &error)) { |
| 1467 | fprintf(stderr, "error: %s\n", error.c_str()); |
| 1468 | return 1; |
| 1469 | } |
| 1470 | printf("%s\n", result.c_str()); |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Spencer Low | a13df30 | 2015-09-07 16:20:13 -0700 | [diff] [blame] | 1474 | // Disallow stdin, stdout, and stderr. |
| 1475 | static bool _is_valid_ack_reply_fd(const int ack_reply_fd) { |
| 1476 | #ifdef _WIN32 |
| 1477 | const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); |
| 1478 | return (GetStdHandle(STD_INPUT_HANDLE) != ack_reply_handle) && |
| 1479 | (GetStdHandle(STD_OUTPUT_HANDLE) != ack_reply_handle) && |
| 1480 | (GetStdHandle(STD_ERROR_HANDLE) != ack_reply_handle); |
| 1481 | #else |
| 1482 | return ack_reply_fd > 2; |
| 1483 | #endif |
| 1484 | } |
| 1485 | |
Yurii Zubrytskyi | 2488c21 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 1486 | static bool _is_valid_os_fd(int fd) { |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 1487 | // Disallow invalid FDs and stdin/out/err as well. |
| 1488 | if (fd < 3) { |
| 1489 | return false; |
| 1490 | } |
| 1491 | #ifdef _WIN32 |
Yurii Zubrytskyi | 2488c21 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 1492 | auto handle = (HANDLE)fd; |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 1493 | DWORD info = 0; |
| 1494 | if (GetHandleInformation(handle, &info) == 0) { |
| 1495 | return false; |
| 1496 | } |
| 1497 | #else |
| 1498 | int flags = fcntl(fd, F_GETFD); |
| 1499 | if (flags == -1) { |
| 1500 | return false; |
| 1501 | } |
| 1502 | #endif |
| 1503 | return true; |
| 1504 | } |
| 1505 | |
Elliott Hughes | cabfa11 | 2016-10-19 14:47:11 -0700 | [diff] [blame] | 1506 | int adb_commandline(int argc, const char** argv) { |
Josh Gao | e032387 | 2018-08-10 14:23:43 -0700 | [diff] [blame] | 1507 | bool no_daemon = false; |
| 1508 | bool is_daemon = false; |
| 1509 | bool is_server = false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1510 | int r; |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 1511 | TransportType transport_type = kTransportAny; |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1512 | int ack_reply_fd = -1; |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1513 | |
Elliott Hughes | 65433da | 2015-11-18 18:07:48 -0800 | [diff] [blame] | 1514 | #if !defined(_WIN32) |
| 1515 | // We'd rather have EPIPE than SIGPIPE. |
| 1516 | signal(SIGPIPE, SIG_IGN); |
| 1517 | #endif |
| 1518 | |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1519 | const char* server_host_str = nullptr; |
| 1520 | const char* server_port_str = nullptr; |
| 1521 | const char* server_socket_str = nullptr; |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1522 | |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 1523 | // We need to check for -d and -e before we look at $ANDROID_SERIAL. |
| 1524 | const char* serial = nullptr; |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1525 | TransportId transport_id = 0; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 1526 | |
Riley Andrews | 98f58e8 | 2014-12-05 17:37:24 -0800 | [diff] [blame] | 1527 | while (argc > 0) { |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 1528 | if (!strcmp(argv[0], "server")) { |
Josh Gao | e032387 | 2018-08-10 14:23:43 -0700 | [diff] [blame] | 1529 | is_server = true; |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 1530 | } else if (!strcmp(argv[0], "nodaemon")) { |
Josh Gao | e032387 | 2018-08-10 14:23:43 -0700 | [diff] [blame] | 1531 | no_daemon = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1532 | } else if (!strcmp(argv[0], "fork-server")) { |
| 1533 | /* this is a special flag used only when the ADB client launches the ADB Server */ |
Josh Gao | e032387 | 2018-08-10 14:23:43 -0700 | [diff] [blame] | 1534 | is_daemon = true; |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1535 | } else if (!strcmp(argv[0], "--reply-fd")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1536 | if (argc < 2) error_exit("--reply-fd requires an argument"); |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1537 | const char* reply_fd_str = argv[1]; |
| 1538 | argc--; |
| 1539 | argv++; |
| 1540 | ack_reply_fd = strtol(reply_fd_str, nullptr, 10); |
Spencer Low | a13df30 | 2015-09-07 16:20:13 -0700 | [diff] [blame] | 1541 | if (!_is_valid_ack_reply_fd(ack_reply_fd)) { |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1542 | fprintf(stderr, "adb: invalid reply fd \"%s\"\n", reply_fd_str); |
Elliott Hughes | 2ec36b0 | 2017-02-06 16:20:30 -0800 | [diff] [blame] | 1543 | return 1; |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1544 | } |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1545 | } else if (!strncmp(argv[0], "-s", 2)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1546 | if (isdigit(argv[0][2])) { |
| 1547 | serial = argv[0] + 2; |
| 1548 | } else { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1549 | if (argc < 2 || argv[0][2] != '\0') error_exit("-s requires an argument"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1550 | serial = argv[1]; |
| 1551 | argc--; |
| 1552 | argv++; |
| 1553 | } |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1554 | } else if (!strncmp(argv[0], "-t", 2)) { |
| 1555 | const char* id; |
| 1556 | if (isdigit(argv[0][2])) { |
| 1557 | id = argv[0] + 2; |
| 1558 | } else { |
| 1559 | id = argv[1]; |
| 1560 | argc--; |
| 1561 | argv++; |
| 1562 | } |
| 1563 | transport_id = strtoll(id, const_cast<char**>(&id), 10); |
| 1564 | if (*id != '\0') { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1565 | error_exit("invalid transport id"); |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1566 | } |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 1567 | } else if (!strcmp(argv[0], "-d")) { |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 1568 | transport_type = kTransportUsb; |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 1569 | } else if (!strcmp(argv[0], "-e")) { |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 1570 | transport_type = kTransportLocal; |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 1571 | } else if (!strcmp(argv[0], "-a")) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1572 | gListenAll = 1; |
Riley Andrews | 98f58e8 | 2014-12-05 17:37:24 -0800 | [diff] [blame] | 1573 | } else if (!strncmp(argv[0], "-H", 2)) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1574 | if (argv[0][2] == '\0') { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1575 | if (argc < 2) error_exit("-H requires an argument"); |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1576 | server_host_str = argv[1]; |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1577 | argc--; |
| 1578 | argv++; |
| 1579 | } else { |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1580 | server_host_str = argv[0] + 2; |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1581 | } |
Riley Andrews | 98f58e8 | 2014-12-05 17:37:24 -0800 | [diff] [blame] | 1582 | } else if (!strncmp(argv[0], "-P", 2)) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1583 | if (argv[0][2] == '\0') { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1584 | if (argc < 2) error_exit("-P requires an argument"); |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1585 | server_port_str = argv[1]; |
| 1586 | argc--; |
| 1587 | argv++; |
| 1588 | } else { |
| 1589 | server_port_str = argv[0] + 2; |
| 1590 | } |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1591 | } else if (!strcmp(argv[0], "-L")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1592 | if (argc < 2) error_exit("-L requires an argument"); |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1593 | server_socket_str = argv[1]; |
| 1594 | argc--; |
| 1595 | argv++; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1596 | } else { |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1597 | /* out of recognized modifiers and flags */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1598 | break; |
| 1599 | } |
| 1600 | argc--; |
| 1601 | argv++; |
| 1602 | } |
| 1603 | |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1604 | if ((server_host_str || server_port_str) && server_socket_str) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1605 | error_exit("-L is incompatible with -H or -P"); |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | // If -L, -H, or -P are specified, ignore environment variables. |
| 1609 | // Otherwise, prefer ADB_SERVER_SOCKET over ANDROID_ADB_SERVER_ADDRESS/PORT. |
Tao Wu | 135f4ab | 2016-09-16 13:01:24 -0700 | [diff] [blame] | 1610 | if (!server_host_str && !server_port_str && !server_socket_str) { |
| 1611 | server_socket_str = getenv("ADB_SERVER_SOCKET"); |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | if (!server_socket_str) { |
| 1615 | // tcp:1234 and tcp:localhost:1234 are different with -a, so don't default to localhost |
| 1616 | server_host_str = server_host_str ? server_host_str : getenv("ANDROID_ADB_SERVER_ADDRESS"); |
| 1617 | |
Tao Wu | 135f4ab | 2016-09-16 13:01:24 -0700 | [diff] [blame] | 1618 | int server_port = DEFAULT_ADB_PORT; |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1619 | server_port_str = server_port_str ? server_port_str : getenv("ANDROID_ADB_SERVER_PORT"); |
Tao Wu | 135f4ab | 2016-09-16 13:01:24 -0700 | [diff] [blame] | 1620 | if (server_port_str && strlen(server_port_str) > 0) { |
| 1621 | if (!android::base::ParseInt(server_port_str, &server_port, 1, 65535)) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1622 | error_exit( |
| 1623 | "$ANDROID_ADB_SERVER_PORT must be a positive number less than 65535: " |
| 1624 | "got \"%s\"", |
| 1625 | server_port_str); |
Tao Wu | 135f4ab | 2016-09-16 13:01:24 -0700 | [diff] [blame] | 1626 | } |
| 1627 | } |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1628 | |
| 1629 | int rc; |
| 1630 | char* temp; |
| 1631 | if (server_host_str) { |
Tao Wu | 135f4ab | 2016-09-16 13:01:24 -0700 | [diff] [blame] | 1632 | rc = asprintf(&temp, "tcp:%s:%d", server_host_str, server_port); |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1633 | } else { |
Tao Wu | 135f4ab | 2016-09-16 13:01:24 -0700 | [diff] [blame] | 1634 | rc = asprintf(&temp, "tcp:%d", server_port); |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1635 | } |
| 1636 | if (rc < 0) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1637 | LOG(FATAL) << "failed to allocate server socket specification"; |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1638 | } |
| 1639 | server_socket_str = temp; |
| 1640 | } |
| 1641 | |
| 1642 | adb_set_socket_spec(server_socket_str); |
| 1643 | |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 1644 | // If none of -d, -e, or -s were specified, try $ANDROID_SERIAL. |
| 1645 | if (transport_type == kTransportAny && serial == nullptr) { |
| 1646 | serial = getenv("ANDROID_SERIAL"); |
| 1647 | } |
| 1648 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1649 | adb_set_transport(transport_type, serial, transport_id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1650 | |
David 'Digit' Turner | 305b4b0 | 2011-01-31 14:23:56 +0100 | [diff] [blame] | 1651 | if (is_server) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1652 | if (no_daemon || is_daemon) { |
Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 1653 | if (is_daemon && (ack_reply_fd == -1)) { |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1654 | fprintf(stderr, "reply fd for adb server to client communication not specified.\n"); |
Elliott Hughes | 2ec36b0 | 2017-02-06 16:20:30 -0800 | [diff] [blame] | 1655 | return 1; |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 1656 | } |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1657 | r = adb_server_main(is_daemon, server_socket_str, ack_reply_fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1658 | } else { |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 1659 | r = launch_server(server_socket_str); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1660 | } |
Riley Andrews | 98f58e8 | 2014-12-05 17:37:24 -0800 | [diff] [blame] | 1661 | if (r) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1662 | fprintf(stderr,"* could not start server *\n"); |
| 1663 | } |
| 1664 | return r; |
| 1665 | } |
| 1666 | |
Riley Andrews | 98f58e8 | 2014-12-05 17:37:24 -0800 | [diff] [blame] | 1667 | if (argc == 0) { |
Elliott Hughes | 2ec36b0 | 2017-02-06 16:20:30 -0800 | [diff] [blame] | 1668 | help(); |
| 1669 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1670 | } |
| 1671 | |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1672 | /* handle wait-for-* prefix */ |
| 1673 | if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 1674 | const char* service = argv[0]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1675 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1676 | if (!wait_for_device(service)) { |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1677 | return 1; |
| 1678 | } |
| 1679 | |
Elliott Hughes | 2b10111 | 2015-05-04 19:29:32 -0700 | [diff] [blame] | 1680 | // Allow a command to be run after wait-for-device, |
| 1681 | // e.g. 'adb wait-for-device shell'. |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1682 | if (argc == 1) { |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | /* Fall through */ |
| 1687 | argc--; |
| 1688 | argv++; |
| 1689 | } |
| 1690 | |
| 1691 | /* adb_connect() commands */ |
Riley Andrews | 98f58e8 | 2014-12-05 17:37:24 -0800 | [diff] [blame] | 1692 | if (!strcmp(argv[0], "devices")) { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 1693 | const char *listopt; |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1694 | if (argc < 2) { |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1695 | listopt = ""; |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1696 | } else if (argc == 2 && !strcmp(argv[1], "-l")) { |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1697 | listopt = argv[1]; |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1698 | } else { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1699 | error_exit("adb devices [-l]"); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1700 | } |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1701 | |
| 1702 | std::string query = android::base::StringPrintf("host:%s%s", argv[0], listopt); |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 1703 | std::string error; |
| 1704 | if (!adb_check_server_version(&error)) { |
| 1705 | error_exit("failed to check server version: %s", error.c_str()); |
| 1706 | } |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1707 | printf("List of devices attached\n"); |
| 1708 | return adb_query_command(query); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1709 | } |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1710 | else if (!strcmp(argv[0], "connect")) { |
Elliott Hughes | 65474fd | 2020-01-08 10:02:43 -0800 | [diff] [blame] | 1711 | if (argc != 2) error_exit("usage: adb connect HOST[:PORT]"); |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1712 | |
| 1713 | std::string query = android::base::StringPrintf("host:connect:%s", argv[1]); |
| 1714 | return adb_query_command(query); |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1715 | } |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1716 | else if (!strcmp(argv[0], "disconnect")) { |
Elliott Hughes | 573e67c | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 1717 | if (argc > 2) error_exit("usage: adb disconnect [HOST[:PORT]]"); |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 1718 | |
| 1719 | std::string query = android::base::StringPrintf("host:disconnect:%s", |
| 1720 | (argc == 2) ? argv[1] : ""); |
| 1721 | return adb_query_command(query); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1722 | } else if (!strcmp(argv[0], "abb")) { |
| 1723 | return adb_abb(argc, argv); |
Joshua Duong | d85f5c0 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 1724 | } else if (!strcmp(argv[0], "pair")) { |
| 1725 | if (argc != 2) error_exit("usage: adb pair <host>[:<port>]"); |
| 1726 | |
| 1727 | std::string password; |
| 1728 | printf("Enter pairing code: "); |
| 1729 | fflush(stdout); |
| 1730 | if (!std::getline(std::cin, password) || password.empty()) { |
| 1731 | error_exit("No pairing code provided"); |
| 1732 | } |
| 1733 | std::string query = |
| 1734 | android::base::StringPrintf("host:pair:%s:%s", password.c_str(), argv[1]); |
| 1735 | |
| 1736 | return adb_query_command(query); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1737 | } else if (!strcmp(argv[0], "emu")) { |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 1738 | return adb_send_emulator_command(argc, argv, serial); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1739 | } else if (!strcmp(argv[0], "shell")) { |
Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 1740 | return adb_shell(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1741 | } else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) { |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1742 | int exec_in = !strcmp(argv[0], "exec-in"); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1743 | |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1744 | if (argc < 2) error_exit("usage: adb %s command", argv[0]); |
Zach Riggle | ccafc79 | 2016-04-14 17:18:04 -0400 | [diff] [blame] | 1745 | |
Elliott Hughes | 2baae3a | 2015-04-17 10:59:34 -0700 | [diff] [blame] | 1746 | std::string cmd = "exec:"; |
| 1747 | cmd += argv[1]; |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1748 | argc -= 2; |
| 1749 | argv += 2; |
| 1750 | while (argc-- > 0) { |
Elliott Hughes | 6c34bba | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 1751 | cmd += " " + escape_arg(*argv++); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1754 | std::string error; |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1755 | unique_fd fd(adb_connect(cmd, &error)); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1756 | if (fd < 0) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 1757 | fprintf(stderr, "error: %s\n", error.c_str()); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1758 | return -1; |
| 1759 | } |
| 1760 | |
| 1761 | if (exec_in) { |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1762 | copy_to_file(STDIN_FILENO, fd.get()); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1763 | } else { |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1764 | copy_to_file(fd.get(), STDOUT_FILENO); |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1765 | } |
Jeff Sharkey | 5d9d434 | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 1766 | return 0; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1767 | } else if (!strcmp(argv[0], "kill-server")) { |
Josh Gao | b3c14ec | 2017-05-08 18:37:17 -0700 | [diff] [blame] | 1768 | return adb_kill_server() ? 0 : 1; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1769 | } else if (!strcmp(argv[0], "sideload")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1770 | if (argc != 2) error_exit("sideload requires an argument"); |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1771 | if (adb_sideload_install(argv[1], false /* rescue_mode */)) { |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 1772 | return 1; |
| 1773 | } else { |
| 1774 | return 0; |
| 1775 | } |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1776 | } else if (!strcmp(argv[0], "rescue")) { |
Tao Bao | 599eee1 | 2019-06-04 14:37:06 -0700 | [diff] [blame] | 1777 | // adb rescue getprop |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1778 | // adb rescue getprop <prop> |
| 1779 | // adb rescue install <filename> |
xunchang | c2265c5 | 2019-04-22 23:14:28 -0700 | [diff] [blame] | 1780 | // adb rescue wipe userdata |
Tao Bao | 599eee1 | 2019-06-04 14:37:06 -0700 | [diff] [blame] | 1781 | if (argc < 2) error_exit("rescue requires at least one argument"); |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1782 | if (!strcmp(argv[1], "getprop")) { |
Tao Bao | 599eee1 | 2019-06-04 14:37:06 -0700 | [diff] [blame] | 1783 | if (argc == 2) { |
| 1784 | return adb_connect_command("rescue-getprop:"); |
| 1785 | } |
| 1786 | if (argc == 3) { |
| 1787 | return adb_connect_command( |
| 1788 | android::base::StringPrintf("rescue-getprop:%s", argv[2])); |
| 1789 | } |
| 1790 | error_exit("invalid rescue getprop arguments"); |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1791 | } else if (!strcmp(argv[1], "install")) { |
Tao Bao | 599eee1 | 2019-06-04 14:37:06 -0700 | [diff] [blame] | 1792 | if (argc != 3) error_exit("rescue install requires two arguments"); |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1793 | if (adb_sideload_install(argv[2], true /* rescue_mode */) != 0) { |
| 1794 | return 1; |
| 1795 | } |
Tao Bao | 599eee1 | 2019-06-04 14:37:06 -0700 | [diff] [blame] | 1796 | } else if (!strcmp(argv[1], "wipe")) { |
| 1797 | if (argc != 3 || strcmp(argv[2], "userdata") != 0) { |
| 1798 | error_exit("invalid rescue wipe arguments"); |
| 1799 | } |
xunchang | c2265c5 | 2019-04-22 23:14:28 -0700 | [diff] [blame] | 1800 | return adb_wipe_devices(); |
Tao Bao | 11cf30b | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1801 | } else { |
| 1802 | error_exit("invalid rescue argument"); |
| 1803 | } |
| 1804 | return 0; |
Elliott Hughes | e163298 | 2017-08-23 15:42:28 -0700 | [diff] [blame] | 1805 | } else if (!strcmp(argv[0], "tcpip")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1806 | if (argc != 2) error_exit("tcpip requires an argument"); |
Elliott Hughes | e163298 | 2017-08-23 15:42:28 -0700 | [diff] [blame] | 1807 | int port; |
| 1808 | if (!android::base::ParseInt(argv[1], &port, 1, 65535)) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1809 | error_exit("tcpip: invalid port: %s", argv[1]); |
Elliott Hughes | e163298 | 2017-08-23 15:42:28 -0700 | [diff] [blame] | 1810 | } |
| 1811 | return adb_connect_command(android::base::StringPrintf("tcpip:%d", port)); |
Josh Gao | 8c2198c | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 1812 | } else if (!strcmp(argv[0], "remount")) { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 1813 | auto&& features = adb_get_feature_set(); |
| 1814 | if (features.empty()) { |
Josh Gao | 8c2198c | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 1815 | return 1; |
| 1816 | } |
| 1817 | |
| 1818 | if (CanUseFeature(features, kFeatureRemountShell)) { |
Mark Salyzyn | 6f908ce | 2019-09-17 08:01:15 -0700 | [diff] [blame] | 1819 | std::vector<const char*> args = {"shell"}; |
| 1820 | args.insert(args.cend(), argv, argv + argc); |
Josh Gao | 9e75634 | 2020-01-08 12:39:52 -0800 | [diff] [blame] | 1821 | return adb_shell_noinput(args.size(), args.data()); |
Mark Salyzyn | 6f908ce | 2019-09-17 08:01:15 -0700 | [diff] [blame] | 1822 | } else if (argc > 1) { |
| 1823 | auto command = android::base::StringPrintf("%s:%s", argv[0], argv[1]); |
| 1824 | return adb_connect_command(command); |
Josh Gao | 8c2198c | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 1825 | } else { |
| 1826 | return adb_connect_command("remount:"); |
| 1827 | } |
Elliott Hughes | 19d80b8 | 2015-07-21 16:13:40 -0700 | [diff] [blame] | 1828 | } |
Mark Salyzyn | d8e94bd | 2018-08-29 10:44:33 -0700 | [diff] [blame] | 1829 | // clang-format off |
Josh Gao | 8c2198c | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 1830 | else if (!strcmp(argv[0], "reboot") || |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1831 | !strcmp(argv[0], "reboot-bootloader") || |
Mark Salyzyn | d8e94bd | 2018-08-29 10:44:33 -0700 | [diff] [blame] | 1832 | !strcmp(argv[0], "reboot-fastboot") || |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1833 | !strcmp(argv[0], "usb") || |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1834 | !strcmp(argv[0], "disable-verity") || |
| 1835 | !strcmp(argv[0], "enable-verity")) { |
Mark Salyzyn | d8e94bd | 2018-08-29 10:44:33 -0700 | [diff] [blame] | 1836 | // clang-format on |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 1837 | std::string command; |
Tao Bao | 175b7bb | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 1838 | if (!strcmp(argv[0], "reboot-bootloader")) { |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 1839 | command = "reboot:bootloader"; |
Mark Salyzyn | d8e94bd | 2018-08-29 10:44:33 -0700 | [diff] [blame] | 1840 | } else if (!strcmp(argv[0], "reboot-fastboot")) { |
| 1841 | command = "reboot:fastboot"; |
Tao Bao | 175b7bb | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 1842 | } else if (argc > 1) { |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 1843 | command = android::base::StringPrintf("%s:%s", argv[0], argv[1]); |
Tao Bao | 175b7bb | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 1844 | } else { |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 1845 | command = android::base::StringPrintf("%s:", argv[0]); |
The Android Open Source Project | e037fd7 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 1846 | } |
Tao Bao | 175b7bb | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 1847 | return adb_connect_command(command); |
Josh Gao | 06c73ae | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 1848 | } else if (!strcmp(argv[0], "root") || !strcmp(argv[0], "unroot")) { |
| 1849 | return adb_root(argv[0]) ? 0 : 1; |
| 1850 | } else if (!strcmp(argv[0], "bugreport")) { |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 1851 | Bugreport bugreport; |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1852 | return bugreport.DoIt(argc, argv); |
Felipe Leme | 44a4267 | 2016-04-01 17:43:27 -0700 | [diff] [blame] | 1853 | } else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) { |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1854 | bool reverse = !strcmp(argv[0], "reverse"); |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1855 | --argc; |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1856 | if (argc < 1) error_exit("%s requires an argument", argv[0]); |
Josh Gao | 258d4a9 | 2018-07-31 15:36:03 -0700 | [diff] [blame] | 1857 | ++argv; |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 1858 | |
| 1859 | // Determine the <host-prefix> for this command. |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1860 | std::string host_prefix; |
David 'Digit' Turner | 2525869 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 1861 | if (reverse) { |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1862 | host_prefix = "reverse:"; |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 1863 | } else { |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1864 | host_prefix = "host:"; |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 1865 | } |
| 1866 | |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1867 | std::string cmd, error_message; |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1868 | if (strcmp(argv[0], "--list") == 0) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1869 | if (argc != 1) error_exit("--list doesn't take any arguments"); |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1870 | return adb_query_command(host_prefix + "list-forward"); |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1871 | } else if (strcmp(argv[0], "--remove-all") == 0) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1872 | if (argc != 1) error_exit("--remove-all doesn't take any arguments"); |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1873 | cmd = "killforward-all"; |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1874 | } else if (strcmp(argv[0], "--remove") == 0) { |
| 1875 | // forward --remove <local> |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1876 | if (argc != 2) error_exit("--remove requires an argument"); |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1877 | cmd = std::string("killforward:") + argv[1]; |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1878 | } else if (strcmp(argv[0], "--no-rebind") == 0) { |
| 1879 | // forward --no-rebind <local> <remote> |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1880 | if (argc != 3) error_exit("--no-rebind takes two arguments"); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1881 | if (forward_targets_are_valid(argv[1], argv[2], &error_message)) { |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1882 | cmd = std::string("forward:norebind:") + argv[1] + ";" + argv[2]; |
David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1883 | } |
Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 1884 | } else { |
| 1885 | // forward <local> <remote> |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1886 | if (argc != 2) error_exit("forward takes two arguments"); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1887 | if (forward_targets_are_valid(argv[0], argv[1], &error_message)) { |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1888 | cmd = std::string("forward:") + argv[0] + ";" + argv[1]; |
David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1889 | } |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1892 | if (!error_message.empty()) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1893 | error_exit("error: %s", error_message.c_str()); |
David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
Josh Gao | da9c767 | 2019-08-06 16:43:34 -0700 | [diff] [blame] | 1896 | unique_fd fd(adb_connect(nullptr, host_prefix + cmd, &error_message, true)); |
Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 1897 | if (fd < 0 || !adb_status(fd.get(), &error_message)) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1898 | error_exit("error: %s", error_message.c_str()); |
David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | // Server or device may optionally return a resolved TCP port number. |
| 1902 | std::string resolved_port; |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1903 | if (ReadProtocolString(fd, &resolved_port, &error_message) && !resolved_port.empty()) { |
David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1904 | printf("%s\n", resolved_port.c_str()); |
| 1905 | } |
| 1906 | |
| 1907 | ReadOrderlyShutdown(fd); |
| 1908 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1909 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1910 | /* do_sync_*() commands */ |
Riley Andrews | c8514c8 | 2014-12-05 17:32:46 -0800 | [diff] [blame] | 1911 | else if (!strcmp(argv[0], "ls")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1912 | if (argc != 2) error_exit("ls requires an argument"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1913 | return do_sync_ls(argv[1]) ? 0 : 1; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1914 | } else if (!strcmp(argv[0], "push")) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1915 | bool copy_attrs = false; |
Dan Albert | 06b0d6b | 2017-05-18 22:56:48 -0700 | [diff] [blame] | 1916 | bool sync = false; |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1917 | bool dry_run = false; |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1918 | CompressionType compression = CompressionType::Any; |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1919 | std::vector<const char*> srcs; |
| 1920 | const char* dst = nullptr; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 1921 | |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1922 | parse_push_pull_args(&argv[1], argc - 1, &srcs, &dst, ©_attrs, &sync, &compression, |
| 1923 | &dry_run); |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1924 | if (srcs.empty() || !dst) error_exit("push requires an argument"); |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1925 | return do_sync_push(srcs, dst, sync, compression, dry_run) ? 0 : 1; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1926 | } else if (!strcmp(argv[0], "pull")) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1927 | bool copy_attrs = false; |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1928 | CompressionType compression = CompressionType::Any; |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1929 | std::vector<const char*> srcs; |
| 1930 | const char* dst = "."; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 1931 | |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1932 | parse_push_pull_args(&argv[1], argc - 1, &srcs, &dst, ©_attrs, nullptr, &compression, |
| 1933 | nullptr); |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1934 | if (srcs.empty()) error_exit("pull requires an argument"); |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1935 | return do_sync_pull(srcs, dst, copy_attrs, compression) ? 0 : 1; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1936 | } else if (!strcmp(argv[0], "install")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1937 | if (argc < 2) error_exit("install requires an argument"); |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1938 | return install_app(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1939 | } else if (!strcmp(argv[0], "install-multiple")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1940 | if (argc < 2) error_exit("install-multiple requires an argument"); |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1941 | return install_multiple_app(argc, argv); |
Patrick Baumann | dc58b0a | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 1942 | } else if (!strcmp(argv[0], "install-multi-package")) { |
Mohammad Samiul Islam | b581ce9 | 2019-05-29 15:00:00 +0100 | [diff] [blame] | 1943 | if (argc < 2) error_exit("install-multi-package requires an argument"); |
Patrick Baumann | dc58b0a | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 1944 | return install_multi_package(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1945 | } else if (!strcmp(argv[0], "uninstall")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1946 | if (argc < 2) error_exit("uninstall requires an argument"); |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1947 | return uninstall_app(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1948 | } else if (!strcmp(argv[0], "sync")) { |
Elliott Hughes | d236d07 | 2015-04-21 10:17:07 -0700 | [diff] [blame] | 1949 | std::string src; |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 1950 | bool list_only = false; |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1951 | bool dry_run = false; |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1952 | CompressionType compression = CompressionType::Any; |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1953 | |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1954 | if (const char* adb_compression = getenv("ADB_COMPRESSION"); adb_compression) { |
| 1955 | compression = parse_compression_type(adb_compression, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1956 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1957 | |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1958 | int opt; |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1959 | while ((opt = getopt(argc, const_cast<char**>(argv), "lnz:Z")) != -1) { |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1960 | switch (opt) { |
| 1961 | case 'l': |
| 1962 | list_only = true; |
| 1963 | break; |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1964 | case 'n': |
| 1965 | dry_run = true; |
| 1966 | break; |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1967 | case 'z': |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1968 | compression = parse_compression_type(optarg, false); |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1969 | break; |
| 1970 | case 'Z': |
Josh Gao | 49ba558 | 2020-03-26 19:33:25 -0700 | [diff] [blame] | 1971 | compression = CompressionType::None; |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1972 | break; |
| 1973 | default: |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1974 | error_exit("usage: adb sync [-l] [-n] [-z ALGORITHM] [-Z] [PARTITION]"); |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | if (optind == argc) { |
| 1979 | src = "all"; |
| 1980 | } else if (optind + 1 == argc) { |
| 1981 | src = argv[optind]; |
| 1982 | } else { |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1983 | error_exit("usage: adb sync [-l] [-n] [-z ALGORITHM] [-Z] [PARTITION]"); |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
Justin Yun | 5d0ac62 | 2019-06-26 09:18:03 +0900 | [diff] [blame] | 1986 | std::vector<std::string> partitions{"data", "odm", "oem", "product", |
| 1987 | "system", "system_ext", "vendor"}; |
Elliott Hughes | 57e97cb | 2018-04-03 10:54:39 -0700 | [diff] [blame] | 1988 | bool found = false; |
| 1989 | for (const auto& partition : partitions) { |
| 1990 | if (src == "all" || src == partition) { |
| 1991 | std::string src_dir{product_file(partition)}; |
| 1992 | if (!directory_exists(src_dir)) continue; |
| 1993 | found = true; |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 1994 | if (!do_sync_sync(src_dir, "/" + partition, list_only, compression, dry_run)) { |
| 1995 | return 1; |
| 1996 | } |
Elliott Hughes | 57e97cb | 2018-04-03 10:54:39 -0700 | [diff] [blame] | 1997 | } |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 1998 | } |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 1999 | if (!found) error_exit("don't know how to sync %s partition", src.c_str()); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 2000 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2001 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2002 | /* passthrough commands */ |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 2003 | else if (!strcmp(argv[0], "get-state") || !strcmp(argv[0], "get-serialno") || |
| 2004 | !strcmp(argv[0], "get-devpath")) { |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 2005 | return adb_query_command(format_host_command(argv[0])); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2006 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2007 | /* other commands */ |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 2008 | else if (!strcmp(argv[0], "logcat") || !strcmp(argv[0], "lolcat") || |
| 2009 | !strcmp(argv[0], "longcat")) { |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 2010 | return logcat(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 2011 | } else if (!strcmp(argv[0], "ppp")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2012 | return ppp(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 2013 | } else if (!strcmp(argv[0], "start-server")) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 2014 | std::string error; |
Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 2015 | const int result = adb_connect("host:start-server", &error); |
| 2016 | if (result < 0) { |
| 2017 | fprintf(stderr, "error: %s\n", error.c_str()); |
| 2018 | } |
| 2019 | return result; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 2020 | } else if (!strcmp(argv[0], "backup")) { |
Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 2021 | return backup(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 2022 | } else if (!strcmp(argv[0], "restore")) { |
Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 2023 | return restore(argc, argv); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 2024 | } else if (!strcmp(argv[0], "keygen")) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 2025 | if (argc != 2) error_exit("keygen requires an argument"); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 2026 | // Always print key generation information for keygen command. |
| 2027 | adb_trace_enable(AUTH); |
Nick Kralevich | bea3f9c | 2014-11-13 15:17:29 -0800 | [diff] [blame] | 2028 | return adb_auth_keygen(argv[1]); |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 2029 | } else if (!strcmp(argv[0], "pubkey")) { |
| 2030 | if (argc != 2) error_exit("pubkey requires an argument"); |
| 2031 | return adb_auth_pubkey(argv[1]); |
| 2032 | } else if (!strcmp(argv[0], "jdwp")) { |
Tao Bao | 175b7bb | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 2033 | return adb_connect_command("jdwp"); |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 2034 | } else if (!strcmp(argv[0], "track-jdwp")) { |
Josh Gao | b463baf | 2016-05-13 18:18:23 -0700 | [diff] [blame] | 2035 | return adb_connect_command("track-jdwp"); |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 2036 | } else if (!strcmp(argv[0], "track-app")) { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 2037 | auto&& features = adb_get_feature_set(); |
| 2038 | if (features.empty()) { |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 2039 | return 1; |
| 2040 | } |
| 2041 | if (!CanUseFeature(features, kFeatureTrackApp)) { |
| 2042 | error_exit("track-app is not supported by the device"); |
| 2043 | } |
| 2044 | TrackAppStreamsCallback callback; |
| 2045 | return adb_connect_command("track-app", nullptr, &callback); |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 2046 | } else if (!strcmp(argv[0], "track-devices")) { |
Elliott Hughes | fc2f5f6 | 2019-07-31 14:13:57 -0700 | [diff] [blame] | 2047 | if (argc > 2 || (argc == 2 && strcmp(argv[1], "-l"))) { |
| 2048 | error_exit("usage: adb track-devices [-l]"); |
| 2049 | } |
| 2050 | return adb_connect_command(argc == 2 ? "host:track-devices-l" : "host:track-devices"); |
Josh Gao | 172d0d4 | 2018-06-18 14:51:56 -0700 | [diff] [blame] | 2051 | } else if (!strcmp(argv[0], "raw")) { |
| 2052 | if (argc != 2) { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 2053 | error_exit("usage: adb raw SERVICE"); |
Josh Gao | 172d0d4 | 2018-06-18 14:51:56 -0700 | [diff] [blame] | 2054 | } |
Josh Gao | e89a55d | 2018-11-16 14:47:59 -0800 | [diff] [blame] | 2055 | return adb_connect_command_bidirectional(argv[1]); |
Josh Gao | b463baf | 2016-05-13 18:18:23 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2058 | /* "adb /?" is a common idiom under Windows */ |
Elliott Hughes | 2ec36b0 | 2017-02-06 16:20:30 -0800 | [diff] [blame] | 2059 | else if (!strcmp(argv[0], "--help") || !strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2060 | help(); |
| 2061 | return 0; |
Josh Gao | 2dc4cab | 2018-11-15 17:45:46 -0800 | [diff] [blame] | 2062 | } else if (!strcmp(argv[0], "--version") || !strcmp(argv[0], "version")) { |
Elliott Hughes | 42ae260 | 2015-08-12 08:32:10 -0700 | [diff] [blame] | 2063 | fprintf(stdout, "%s", adb_version().c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2064 | return 0; |
Josh Gao | 5d1756c | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 2065 | } else if (!strcmp(argv[0], "features")) { |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 2066 | // Only list the features common to both the adb client and the device. |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 2067 | auto&& features = adb_get_feature_set(); |
| 2068 | if (features.empty()) { |
David Pursell | 0aacbbe | 2016-01-21 19:56:50 -0800 | [diff] [blame] | 2069 | return 1; |
| 2070 | } |
| 2071 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 2072 | for (const std::string& name : features) { |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 2073 | if (CanUseFeature(features, name)) { |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 2074 | printf("%s\n", name.c_str()); |
| 2075 | } |
| 2076 | } |
| 2077 | return 0; |
Josh Gao | 5d1756c | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 2078 | } else if (!strcmp(argv[0], "host-features")) { |
| 2079 | return adb_query_command("host:host-features"); |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 2080 | } else if (!strcmp(argv[0], "reconnect")) { |
| 2081 | if (argc == 1) { |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 2082 | return adb_query_command(format_host_command(argv[0])); |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 2083 | } else if (argc == 2) { |
| 2084 | if (!strcmp(argv[1], "device")) { |
| 2085 | std::string err; |
| 2086 | adb_connect("reconnect", &err); |
| 2087 | return 0; |
| 2088 | } else if (!strcmp(argv[1], "offline")) { |
| 2089 | std::string err; |
| 2090 | return adb_query_command("host:reconnect-offline"); |
| 2091 | } else { |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 2092 | error_exit("usage: adb reconnect [device|offline]"); |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 2093 | } |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 2094 | } |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 2095 | } else if (!strcmp(argv[0], "inc-server")) { |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 2096 | if (argc < 4) { |
| 2097 | #ifdef _WIN32 |
| 2098 | error_exit("usage: adb inc-server CONNECTION_HANDLE OUTPUT_HANDLE FILE1 FILE2 ..."); |
| 2099 | #else |
| 2100 | error_exit("usage: adb inc-server CONNECTION_FD OUTPUT_FD FILE1 FILE2 ..."); |
| 2101 | #endif |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 2102 | } |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 2103 | int connection_fd = atoi(argv[1]); |
Yurii Zubrytskyi | 2488c21 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 2104 | if (!_is_valid_os_fd(connection_fd)) { |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 2105 | error_exit("Invalid connection_fd number given: %d", connection_fd); |
Alex Buynytskyy | 96ff54b | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 2106 | } |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 2107 | |
| 2108 | connection_fd = adb_register_socket(connection_fd); |
| 2109 | close_on_exec(connection_fd); |
| 2110 | |
| 2111 | int output_fd = atoi(argv[2]); |
Yurii Zubrytskyi | 2488c21 | 2020-03-18 15:49:45 -0700 | [diff] [blame] | 2112 | if (!_is_valid_os_fd(output_fd)) { |
Songchun Fan | 23bd552 | 2020-03-09 11:33:44 -0700 | [diff] [blame] | 2113 | error_exit("Invalid output_fd number given: %d", output_fd); |
| 2114 | } |
| 2115 | output_fd = adb_register_socket(output_fd); |
| 2116 | close_on_exec(output_fd); |
| 2117 | return incremental::serve(connection_fd, output_fd, argc - 3, argv + 3); |
Dan Albert | 90d4b73 | 2015-05-20 18:58:41 -0700 | [diff] [blame] | 2118 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2119 | |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 2120 | error_exit("unknown command %s", argv[0]); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 2121 | __builtin_unreachable(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2122 | } |