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