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