blob: 1c5b5bd40a9dfa77229e3e4c07dc7cff41c706fc [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
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 Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_ADB
18
19#include "sysdeps.h"
20
Dan Albertb302d122015-02-24 15:51:19 -080021#include <assert.h>
22#include <ctype.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <errno.h>
Elliott Hughesd1a5b2b2015-04-17 14:07:52 -070024#include <inttypes.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <limits.h>
26#include <stdarg.h>
Dan Albertb302d122015-02-24 15:51:19 -080027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080031#include <sys/stat.h>
Dan Albertb302d122015-02-24 15:51:19 -080032#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080033
Elliott Hughes170b5682015-04-17 10:59:34 -070034#include <string>
35
36#include <base/stringprintf.h>
37
Yabin Cui6704a3c2014-11-17 14:48:25 -080038#if !defined(_WIN32)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080039#include <termios.h>
Dan Albertb302d122015-02-24 15:51:19 -080040#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080041#endif
42
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043#include "adb.h"
Nick Kralevich6183c962014-11-13 15:17:29 -080044#include "adb_auth.h"
Dan Albert66a91b02015-02-24 21:26:58 -080045#include "adb_client.h"
46#include "adb_io.h"
Elliott Hughes38104452015-04-17 13:57:15 -070047#include "adb_utils.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048#include "file_sync_service.h"
49
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070050static int install_app(TransportType t, const char* serial, int argc, const char** argv);
51static int install_multiple_app(TransportType t, const char* serial, int argc, const char** argv);
52static int uninstall_app(TransportType t, const char* serial, int argc, const char** argv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053
Elliott Hughes38104452015-04-17 13:57:15 -070054static std::string gProductOutPath;
Matt Gumbel411775c2012-11-14 10:16:17 -080055extern int gListenAll;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080056
Elliott Hughes38104452015-04-17 13:57:15 -070057static std::string product_file(const char *extra) {
58 if (gProductOutPath.empty()) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059 fprintf(stderr, "adb: Product directory not specified; "
60 "use -p or define ANDROID_PRODUCT_OUT\n");
61 exit(1);
62 }
63
Elliott Hughes38104452015-04-17 13:57:15 -070064 return android::base::StringPrintf("%s%s%s",
65 gProductOutPath.c_str(), OS_PATH_SEPARATOR_STR, extra);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080066}
67
Elliott Hughes38104452015-04-17 13:57:15 -070068static void help() {
Elliott Hughesb9f01642015-08-12 08:32:10 -070069 fprintf(stderr, "%s\n", adb_version().c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070 fprintf(stderr,
Matt Gumbel411775c2012-11-14 10:16:17 -080071 " -a - directs adb to listen on all interfaces for a connection\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080072 " -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 Anderson6dfaf4b2012-04-20 11:21:14 -070076 " -s <specific device> - directs command to the device or emulator with the given\n"
Scott Anderson27042382012-05-30 18:11:27 -070077 " serial number or qualifier. Overrides ANDROID_SERIAL\n"
Elliott Hughesec424ad2009-10-07 15:38:53 -070078 " environment variable.\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080079 " -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 Gumbel411775c2012-11-14 10:16:17 -080085 " -H - Name of adb server host (default: localhost)\n"
86 " -P - Port of adb server (default: 5037)\n"
Scott Anderson6dfaf4b2012-04-20 11:21:14 -070087 " devices [-l] - list all connected devices\n"
Scott Anderson27042382012-05-30 18:11:27 -070088 " ('-l' will also list device qualifiers)\n"
Mike Lockwood01c2c302010-05-24 10:44:35 -040089 " 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-Fischerc3e82b82011-04-26 12:46:05 +020093 " Using this command with no additional arguments\n"
Mike Lockwood01c2c302010-05-24 10:44:35 -040094 " will disconnect from all connected TCP/IP devices.\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080095 "\n"
96 "device commands:\n"
Mark Lindner9f9d1452014-03-11 17:55:59 -070097 " adb push [-p] <local> <remote>\n"
98 " - copy file/dir to device\n"
99 " ('-p' to display the transfer progress)\n"
Lajos Molnar4e23e3c2013-04-19 12:41:09 -0700100 " adb pull [-p] [-a] <remote> [<local>]\n"
Mark Lindner9f9d1452014-03-11 17:55:59 -0700101 " - copy file/dir from device\n"
102 " ('-p' to display the transfer progress)\n"
Lajos Molnar4e23e3c2013-04-19 12:41:09 -0700103 " ('-a' means copy timestamp and mode)\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104 " adb sync [ <directory> ] - copy host->device only if changed\n"
Anthony Newnamdd2db142010-02-22 08:36:49 -0600105 " (-l means list but don't copy)\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800106 " 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' Turner6c489802012-11-14 15:01:55 +0100110 " 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 Project9ca14dc2009-03-03 19:32:55 -0800113 " 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' Turner6c489802012-11-14 15:01:55 +0100121 " 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' Turner963a4492013-03-21 21:07:42 +0100126 " 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 Project9ca14dc2009-03-03 19:32:55 -0800139 " adb jdwp - list PIDs of processes hosting a JDWP transport\n"
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700140 " adb install [-lrtsd] <file>\n"
141 " adb install-multiple [-lrtsdp] <file...>\n"
Anonymous Coward5fe7ec22012-04-24 10:43:41 -0700142 " - push this package file to the device and install it\n"
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700143 " (-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 Project9ca14dc2009-03-03 19:32:55 -0800149 " 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 Tate6f2937c2013-03-06 16:40:52 -0800154 " adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]\n"
Christopher Tate1cbb6df2011-10-03 18:27:01 -0700155 " - 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 Tate73779122011-04-21 12:53:28 -0700158 " (-apk|-noapk enable/disable backup of the .apks themselves\n"
Christopher Tate24b56162011-08-09 17:05:29 -0700159 " in the archive; the default is noapk.)\n"
Christopher Tate6f2937c2013-03-06 16:40:52 -0800160 " (-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 Tate73779122011-04-21 12:53:28 -0700163 " (-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 Tate1cbb6df2011-10-03 18:27:01 -0700166 " (-system|-nosystem toggles whether -all automatically includes\n"
167 " system applications; the default is to include system apps)\n"
Christopher Tate73779122011-04-21 12:53:28 -0700168 " (<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 Tate1cbb6df2011-10-03 18:27:01 -0700170 " 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 Tate73779122011-04-21 12:53:28 -0700173 "\n"
Christopher Tate24b56162011-08-09 17:05:29 -0700174 " adb restore <file> - restore device contents from the <file> backup archive\n"
Christopher Tatecf5379b2011-05-17 15:52:54 -0700175 "\n"
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800176 " 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 Kralevich6183c962014-11-13 15:17:29 -0800178 " 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 Project9ca14dc2009-03-03 19:32:55 -0800181 " adb help - show this help message\n"
182 " adb version - show version num\n"
183 "\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184 "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 Anderson6dfaf4b2012-04-20 11:21:14 -0700190 " adb get-devpath - prints: <device-path>\n"
Elliott Hughesdedf7672015-03-16 21:58:32 +0000191 " adb remount - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write\n"
Tao Bao0db67642015-03-29 11:22:34 -0700192 " 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 Hughes52746b02015-06-12 14:26:29 -0700197 " adb sideload <file> - sideloads the given package\n"
Mike Lockwood26b88e32009-08-24 15:58:40 -0700198 " adb root - restarts the adbd daemon with root permissions\n"
Dan Pasanenfb787d92014-10-06 12:57:20 -0500199 " adb unroot - restarts the adbd daemon without root permissions\n"
Romain Guyf925d912009-12-14 14:42:17 -0800200 " adb usb - restarts the adbd daemon listening on USB\n"
Paul Lawrence5f356482014-10-09 14:22:49 +0000201 " adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port\n"
Elliott Hughes52746b02015-06-12 14:26:29 -0700202 "\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800203 "networking:\n"
204 " adb ppp <tty> [parameters] - Run PPP over USB.\n"
Kenny Rootf8eb5782009-06-08 14:40:30 -0500205 " Note: you should not automatically start a PPP connection.\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206 " <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 Hughesdedf7672015-03-16 21:58:32 +0000212 " - If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800213 "\n"
Elliott Hughesdedf7672015-03-16 21:58:32 +0000214 " - If it is \"system\", \"vendor\", \"oem\" or \"data\", only the corresponding partition\n"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215 " is updated.\n"
Tim1b29ed32010-02-16 20:18:29 +0000216 "\n"
Elliott Hughes52746b02015-06-12 14:26:29 -0700217 "environment variables:\n"
Tim1b29ed32010-02-16 20:18:29 +0000218 " 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 Project9ca14dc2009-03-03 19:32:55 -0800222 );
223}
224
Elliott Hughes38104452015-04-17 13:57:15 -0700225static int usage() {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800226 help();
227 return 1;
228}
229
Yabin Cui6704a3c2014-11-17 14:48:25 -0800230#if defined(_WIN32)
231
Elliott Hughes6a096932015-04-16 16:47:02 -0700232// Implemented in sysdeps_win32.cpp.
Spencer Lowbeb61982015-03-01 15:06:21 -0800233void stdin_raw_init(int fd);
234void stdin_raw_restore(int fd);
Yabin Cui6704a3c2014-11-17 14:48:25 -0800235
236#else
Alistair Buxton7c5dcbb2013-03-01 22:16:41 +0100237static termios g_saved_terminal_state;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800238
Alistair Buxton7c5dcbb2013-03-01 22:16:41 +0100239static void stdin_raw_init(int fd) {
240 if (tcgetattr(fd, &g_saved_terminal_state)) return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241
Alistair Buxton7c5dcbb2013-03-01 22:16:41 +0100242 termios tio;
243 if (tcgetattr(fd, &tio)) return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800244
Alistair Buxton7c5dcbb2013-03-01 22:16:41 +0100245 cfmakeraw(&tio);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800246
Alistair Buxton7c5dcbb2013-03-01 22:16:41 +0100247 // No timeout but request at least one character per read.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800248 tio.c_cc[VTIME] = 0;
249 tio.c_cc[VMIN] = 1;
250
Alistair Buxton7c5dcbb2013-03-01 22:16:41 +0100251 tcsetattr(fd, TCSAFLUSH, &tio);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252}
253
Alistair Buxton7c5dcbb2013-03-01 22:16:41 +0100254static void stdin_raw_restore(int fd) {
255 tcsetattr(fd, TCSAFLUSH, &g_saved_terminal_state);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800256}
257#endif
258
Elliott Hughesdabb9742015-05-07 23:37:40 -0700259static void read_and_dump(int fd) {
260 while (fd >= 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700261 D("read_and_dump(): pre adb_read(fd=%d)\n", fd);
Elliott Hughesdabb9742015-05-07 23:37:40 -0700262 char buf[BUFSIZ];
263 int len = adb_read(fd, buf, sizeof(buf));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700264 D("read_and_dump(): post adb_read(fd=%d): len=%d\n", fd, len);
Elliott Hughesdabb9742015-05-07 23:37:40 -0700265 if (len <= 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800266 break;
267 }
268
Mike Lockwood597ea9a2009-09-22 01:18:40 -0400269 fwrite(buf, 1, len, stdout);
270 fflush(stdout);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800271 }
272}
273
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700274static 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);
279 if (len == 0) {
280 break;
281 } else if (len < 0) {
282 if (errno == EINTR) continue;
283 break;
284 }
285
286 buf += len;
287 count -= len;
288 }
289 *buf = '\0';
290}
291
Christopher Tate73779122011-04-21 12:53:28 -0700292static void copy_to_file(int inFd, int outFd) {
Christopher Tatea162e242011-06-10 11:38:37 -0700293 const size_t BUFSIZE = 32 * 1024;
294 char* buf = (char*) malloc(BUFSIZE);
Elliott Hughesd0269c92015-04-21 19:39:52 -0700295 if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
Christopher Tate73779122011-04-21 12:53:28 -0700296 int len;
Christopher Tatefba22972011-06-01 17:56:23 -0700297 long total = 0;
Spencer Lowe7af2d32015-05-22 16:48:31 -0700298#ifdef _WIN32
299 int old_stdin_mode = -1;
300 int old_stdout_mode = -1;
301#endif
Christopher Tate73779122011-04-21 12:53:28 -0700302
303 D("copy_to_file(%d -> %d)\n", inFd, outFd);
Yabin Cui6704a3c2014-11-17 14:48:25 -0800304
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700305 if (inFd == STDIN_FILENO) {
306 stdin_raw_init(STDIN_FILENO);
Spencer Lowe7af2d32015-05-22 16:48:31 -0700307#ifdef _WIN32
308 old_stdin_mode = _setmode(STDIN_FILENO, _O_BINARY);
309 if (old_stdin_mode == -1) {
310 fatal_errno("could not set stdin to binary");
311 }
312#endif
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700313 }
Yabin Cui6704a3c2014-11-17 14:48:25 -0800314
Spencer Lowe7af2d32015-05-22 16:48:31 -0700315#ifdef _WIN32
316 if (outFd == STDOUT_FILENO) {
317 old_stdout_mode = _setmode(STDOUT_FILENO, _O_BINARY);
318 if (old_stdout_mode == -1) {
319 fatal_errno("could not set stdout to binary");
320 }
321 }
322#endif
323
Elliott Hughes7cf35752015-04-17 17:03:59 -0700324 while (true) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700325 if (inFd == STDIN_FILENO) {
326 len = unix_read(inFd, buf, BUFSIZE);
327 } else {
328 len = adb_read(inFd, buf, BUFSIZE);
329 }
Christopher Tate73779122011-04-21 12:53:28 -0700330 if (len == 0) {
Christopher Tatea162e242011-06-10 11:38:37 -0700331 D("copy_to_file() : read 0 bytes; exiting\n");
Christopher Tate73779122011-04-21 12:53:28 -0700332 break;
333 }
334 if (len < 0) {
Christopher Tatea162e242011-06-10 11:38:37 -0700335 if (errno == EINTR) {
336 D("copy_to_file() : EINTR, retrying\n");
337 continue;
338 }
Christopher Tate73779122011-04-21 12:53:28 -0700339 D("copy_to_file() : error %d\n", errno);
340 break;
341 }
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700342 if (outFd == STDOUT_FILENO) {
343 fwrite(buf, 1, len, stdout);
344 fflush(stdout);
345 } else {
346 adb_write(outFd, buf, len);
347 }
Christopher Tatefba22972011-06-01 17:56:23 -0700348 total += len;
Christopher Tate73779122011-04-21 12:53:28 -0700349 }
Yabin Cui6704a3c2014-11-17 14:48:25 -0800350
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700351 if (inFd == STDIN_FILENO) {
352 stdin_raw_restore(STDIN_FILENO);
Spencer Lowe7af2d32015-05-22 16:48:31 -0700353#ifdef _WIN32
354 if (_setmode(STDIN_FILENO, old_stdin_mode) == -1) {
355 fatal_errno("could not restore stdin mode");
356 }
357#endif
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700358 }
Yabin Cui6704a3c2014-11-17 14:48:25 -0800359
Spencer Lowe7af2d32015-05-22 16:48:31 -0700360#ifdef _WIN32
361 if (outFd == STDOUT_FILENO) {
362 if (_setmode(STDOUT_FILENO, old_stdout_mode) == -1) {
363 fatal_errno("could not restore stdout mode");
364 }
365 }
366#endif
367
Christopher Tatefba22972011-06-01 17:56:23 -0700368 D("copy_to_file() finished after %lu bytes\n", total);
Christopher Tatea162e242011-06-10 11:38:37 -0700369 free(buf);
Christopher Tate73779122011-04-21 12:53:28 -0700370}
371
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800372static void *stdin_read_thread(void *x)
373{
374 int fd, fdi;
375 unsigned char buf[1024];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800376 int r, n;
377 int state = 0;
378
379 int *fds = (int*) x;
380 fd = fds[0];
381 fdi = fds[1];
382 free(fds);
383
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800384 for(;;) {
385 /* fdi is really the client's stdin, so use read, not adb_read here */
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700386 D("stdin_read_thread(): pre unix_read(fdi=%d,...)\n", fdi);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800387 r = unix_read(fdi, buf, 1024);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700388 D("stdin_read_thread(): post unix_read(fdi=%d,...)\n", fdi);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800389 if(r == 0) break;
390 if(r < 0) {
391 if(errno == EINTR) continue;
392 break;
393 }
Mike Lockwood18ab0d62010-05-25 13:40:15 -0400394 for(n = 0; n < r; n++){
395 switch(buf[n]) {
396 case '\n':
397 state = 1;
398 break;
399 case '\r':
400 state = 1;
401 break;
402 case '~':
403 if(state == 1) state++;
404 break;
405 case '.':
406 if(state == 2) {
407 fprintf(stderr,"\n* disconnect *\n");
Mike Lockwood18ab0d62010-05-25 13:40:15 -0400408 stdin_raw_restore(fdi);
Mike Lockwood18ab0d62010-05-25 13:40:15 -0400409 exit(0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800410 }
Mike Lockwood18ab0d62010-05-25 13:40:15 -0400411 default:
412 state = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800413 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800414 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800415 r = adb_write(fd, buf, r);
416 if(r <= 0) {
417 break;
418 }
419 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420 return 0;
421}
422
Elliott Hughes38104452015-04-17 13:57:15 -0700423static int interactive_shell() {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700424 int fdi;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800425
Elliott Hughes04a98c22015-04-29 08:35:59 -0700426 std::string error;
427 int fd = adb_connect("shell:", &error);
428 if (fd < 0) {
429 fprintf(stderr,"error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800430 return 1;
431 }
432 fdi = 0; //dup(0);
433
Dan Albertf30d73c2015-02-25 17:51:28 -0800434 int* fds = reinterpret_cast<int*>(malloc(sizeof(int) * 2));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700435 if (fds == nullptr) {
436 fprintf(stderr, "couldn't allocate fds array: %s\n", strerror(errno));
437 return 1;
438 }
439
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800440 fds[0] = fd;
441 fds[1] = fdi;
442
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800443 stdin_raw_init(fdi);
Elliott Hughesf2517142015-05-05 13:41:21 -0700444
445 adb_thread_create(stdin_read_thread, fds);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800446 read_and_dump(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800447 stdin_raw_restore(fdi);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800448 return 0;
449}
450
451
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700452static std::string format_host_command(const char* command, TransportType type, const char* serial) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800453 if (serial) {
Elliott Hughesda945812015-04-29 12:28:13 -0700454 return android::base::StringPrintf("host-serial:%s:%s", serial, command);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800455 }
Elliott Hughesda945812015-04-29 12:28:13 -0700456
457 const char* prefix = "host";
458 if (type == kTransportUsb) {
459 prefix = "host-usb";
460 } else if (type == kTransportLocal) {
461 prefix = "host-local";
462 }
463 return android::base::StringPrintf("%s:%s", prefix, command);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800464}
465
Elliott Hughesda945812015-04-29 12:28:13 -0700466static int adb_download_buffer(const char *service, const char *fn, const void* data, unsigned sz,
Elliott Hughes04a98c22015-04-29 08:35:59 -0700467 bool show_progress)
Doug Zongker6b217ed2012-01-09 14:54:53 -0800468{
Elliott Hughes04a98c22015-04-29 08:35:59 -0700469 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -0700470 int fd = adb_connect(android::base::StringPrintf("%s:%d", service, sz), &error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700471 if (fd < 0) {
472 fprintf(stderr,"error: %s\n", error.c_str());
Doug Zongker6b217ed2012-01-09 14:54:53 -0800473 return -1;
474 }
475
476 int opt = CHUNK_SIZE;
Spencer Low31aafa62015-01-25 14:40:16 -0800477 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
Doug Zongker6b217ed2012-01-09 14:54:53 -0800478
Elliott Hughesda945812015-04-29 12:28:13 -0700479 unsigned total = sz;
Dan Albertf30d73c2015-02-25 17:51:28 -0800480 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
Doug Zongker6b217ed2012-01-09 14:54:53 -0800481
Elliott Hughes04a98c22015-04-29 08:35:59 -0700482 if (show_progress) {
Elliott Hughes24f9b082015-07-27 21:21:39 -0700483 const char* x = strrchr(service, ':');
484 if (x) service = x + 1;
Doug Zongker6b217ed2012-01-09 14:54:53 -0800485 }
486
Elliott Hughesda945812015-04-29 12:28:13 -0700487 while (sz > 0) {
Doug Zongker6b217ed2012-01-09 14:54:53 -0800488 unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700489 if (!WriteFdExactly(fd, ptr, xfer)) {
490 std::string error;
491 adb_status(fd, &error);
492 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
Doug Zongker6b217ed2012-01-09 14:54:53 -0800493 return -1;
494 }
495 sz -= xfer;
496 ptr += xfer;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700497 if (show_progress) {
Magnus Erikssoncb30cc62013-03-05 07:37:32 +0100498 printf("sending: '%s' %4d%% \r", fn, (int)(100LL - ((100LL * sz) / (total))));
Doug Zongker6b217ed2012-01-09 14:54:53 -0800499 fflush(stdout);
500 }
501 }
Elliott Hughes04a98c22015-04-29 08:35:59 -0700502 if (show_progress) {
Doug Zongker6b217ed2012-01-09 14:54:53 -0800503 printf("\n");
504 }
505
Elliott Hughes88b4c852015-04-30 17:32:03 -0700506 if (!adb_status(fd, &error)) {
507 fprintf(stderr,"* error response '%s' *\n", error.c_str());
Doug Zongker6b217ed2012-01-09 14:54:53 -0800508 return -1;
509 }
510
511 adb_close(fd);
512 return 0;
513}
514
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700515#define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE)
516
517/*
518 * The sideload-host protocol serves the data in a file (given on the
519 * command line) to the client, using a simple protocol:
520 *
521 * - The connect message includes the total number of bytes in the
522 * file and a block size chosen by us.
523 *
524 * - The other side sends the desired block number as eight decimal
525 * digits (eg "00000023" for block 23). Blocks are numbered from
526 * zero.
527 *
528 * - We send back the data of the requested block. The last block is
529 * likely to be partial; when the last block is requested we only
530 * send the part of the block that exists, it's not padded up to the
531 * block size.
532 *
533 * - When the other side sends "DONEDONE" instead of a block number,
534 * we hang up.
535 */
Elliott Hughes38104452015-04-17 13:57:15 -0700536static int adb_sideload_host(const char* fn) {
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700537 unsigned sz;
538 size_t xfer = 0;
539 int status;
Dan Albertf30d73c2015-02-25 17:51:28 -0800540 int last_percent = -1;
541 int opt = SIDELOAD_HOST_BLOCK_SIZE;
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700542
543 printf("loading: '%s'", fn);
544 fflush(stdout);
Dan Albertf30d73c2015-02-25 17:51:28 -0800545 uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz));
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700546 if (data == 0) {
547 printf("\n");
548 fprintf(stderr, "* cannot read '%s' *\n", fn);
549 return -1;
550 }
551
Elliott Hughesda945812015-04-29 12:28:13 -0700552 std::string service =
553 android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700554 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -0700555 int fd = adb_connect(service, &error);
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700556 if (fd < 0) {
557 // Try falling back to the older sideload method. Maybe this
558 // is an older device that doesn't support sideload-host.
559 printf("\n");
Elliott Hughes04a98c22015-04-29 08:35:59 -0700560 status = adb_download_buffer("sideload", fn, data, sz, true);
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700561 goto done;
562 }
563
Spencer Low31aafa62015-01-25 14:40:16 -0800564 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700565
Elliott Hughes7cf35752015-04-17 17:03:59 -0700566 while (true) {
Elliott Hughesda945812015-04-29 12:28:13 -0700567 char buf[9];
Dan Albert66a91b02015-02-24 21:26:58 -0800568 if (!ReadFdExactly(fd, buf, 8)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700569 fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700570 status = -1;
571 goto done;
572 }
Elliott Hughesda945812015-04-29 12:28:13 -0700573 buf[8] = '\0';
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700574
Elliott Hughesda945812015-04-29 12:28:13 -0700575 if (strcmp("DONEDONE", buf) == 0) {
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700576 status = 0;
577 break;
578 }
579
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700580 int block = strtol(buf, NULL, 10);
581
582 size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
583 if (offset >= sz) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700584 fprintf(stderr, "* attempt to read block %d past end\n", block);
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700585 status = -1;
586 goto done;
587 }
588 uint8_t* start = data + offset;
589 size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE;
590 size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
591 if (offset_end > sz) {
592 to_write = sz - offset;
593 }
594
Dan Albert66a91b02015-02-24 21:26:58 -0800595 if(!WriteFdExactly(fd, start, to_write)) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700596 adb_status(fd, &error);
597 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700598 status = -1;
599 goto done;
600 }
601 xfer += to_write;
602
603 // For normal OTA packages, we expect to transfer every byte
604 // twice, plus a bit of overhead (one read during
605 // verification, one read of each byte for installation, plus
606 // extra access to things like the zip central directory).
607 // This estimate of the completion becomes 100% when we've
608 // transferred ~2.13 (=100/47) times the package size.
609 int percent = (int)(xfer * 47LL / (sz ? sz : 1));
610 if (percent != last_percent) {
611 printf("\rserving: '%s' (~%d%%) ", fn, percent);
612 fflush(stdout);
613 last_percent = percent;
614 }
615 }
616
Colin Cross2328bea2014-07-07 14:12:41 -0700617 printf("\rTotal xfer: %.2fx%*s\n", (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
Doug Zongkerbcad29f2014-06-26 15:35:36 -0700618
619 done:
620 if (fd >= 0) adb_close(fd);
621 free(data);
622 return status;
623}
624
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800625/**
626 * Run ppp in "notty" mode against a resource listed as the first parameter
627 * eg:
628 *
629 * ppp dev:/dev/omap_csmi_tty0 <ppp options>
630 *
631 */
Elliott Hughes38104452015-04-17 13:57:15 -0700632static int ppp(int argc, const char** argv) {
Yabin Cui2fa43212014-11-11 09:24:11 -0800633#if defined(_WIN32)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800634 fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
635 return -1;
636#else
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800637 if (argc < 2) {
638 fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
639 argv[0]);
640
641 return 1;
642 }
643
Dan Albertf30d73c2015-02-25 17:51:28 -0800644 const char* adb_service_name = argv[1];
Elliott Hughes04a98c22015-04-29 08:35:59 -0700645 std::string error;
646 int fd = adb_connect(adb_service_name, &error);
647 if (fd < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800648 fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
Elliott Hughes04a98c22015-04-29 08:35:59 -0700649 adb_service_name, error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800650 return 1;
651 }
652
Elliott Hughes04a98c22015-04-29 08:35:59 -0700653 pid_t pid = fork();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800654
655 if (pid < 0) {
656 perror("from fork()");
657 return 1;
658 } else if (pid == 0) {
659 int err;
660 int i;
661 const char **ppp_args;
662
663 // copy args
664 ppp_args = (const char **) alloca(sizeof(char *) * argc + 1);
665 ppp_args[0] = "pppd";
666 for (i = 2 ; i < argc ; i++) {
667 //argv[2] and beyond become ppp_args[1] and beyond
668 ppp_args[i - 1] = argv[i];
669 }
670 ppp_args[i-1] = NULL;
671
672 // child side
673
674 dup2(fd, STDIN_FILENO);
675 dup2(fd, STDOUT_FILENO);
676 adb_close(STDERR_FILENO);
677 adb_close(fd);
678
679 err = execvp("pppd", (char * const *)ppp_args);
680
681 if (err < 0) {
682 perror("execing pppd");
683 }
684 exit(-1);
685 } else {
686 // parent side
687
688 adb_close(fd);
689 return 0;
690 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800691#endif /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800692}
693
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700694static bool wait_for_device(const char* service, TransportType t, const char* serial) {
Elliott Hugheseaabdd62015-05-04 19:29:32 -0700695 // Was the caller vague about what they'd like us to wait for?
696 // If so, check they weren't more specific in their choice of transport type.
697 if (strcmp(service, "wait-for-device") == 0) {
698 if (t == kTransportUsb) {
699 service = "wait-for-usb";
700 } else if (t == kTransportLocal) {
701 service = "wait-for-local";
702 } else {
703 service = "wait-for-any";
704 }
705 }
706
707 std::string cmd = format_host_command(service, t, serial);
Elliott Hughesd98ca8a2015-05-29 17:55:19 -0700708 return adb_command(cmd);
Elliott Hugheseaabdd62015-05-04 19:29:32 -0700709}
710
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700711static int send_shell_command(TransportType transport_type, const char* serial,
Elliott Hughes170b5682015-04-17 10:59:34 -0700712 const std::string& command) {
713 int fd;
714 while (true) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700715 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -0700716 fd = adb_connect(command, &error);
Elliott Hughes04a98c22015-04-29 08:35:59 -0700717 if (fd >= 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800718 break;
Elliott Hughes04a98c22015-04-29 08:35:59 -0700719 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800720 fprintf(stderr,"- waiting for device -\n");
721 adb_sleep_ms(1000);
Elliott Hugheseaabdd62015-05-04 19:29:32 -0700722 wait_for_device("wait-for-device", transport_type, serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800723 }
724
725 read_and_dump(fd);
Elliott Hughes170b5682015-04-17 10:59:34 -0700726 int rc = adb_close(fd);
727 if (rc) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800728 perror("close");
Elliott Hughes170b5682015-04-17 10:59:34 -0700729 }
730 return rc;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800731}
732
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700733static int logcat(TransportType transport, const char* serial, int argc, const char** argv) {
Elliott Hughes170b5682015-04-17 10:59:34 -0700734 char* log_tags = getenv("ANDROID_LOG_TAGS");
735 std::string quoted = escape_arg(log_tags == nullptr ? "" : log_tags);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800736
Elliott Hughes170b5682015-04-17 10:59:34 -0700737 std::string cmd = "shell:export ANDROID_LOG_TAGS=\"" + quoted + "\"; exec logcat";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800738
Jeff Sharkey824d1062014-06-10 11:31:24 -0700739 if (!strcmp(argv[0], "longcat")) {
Elliott Hughes170b5682015-04-17 10:59:34 -0700740 cmd += " -v long";
Christopher Tate7b9b5162011-11-30 13:00:33 -0800741 }
742
Elliott Hughese4b64792015-04-17 20:11:08 -0700743 --argc;
744 ++argv;
Elliott Hughes170b5682015-04-17 10:59:34 -0700745 while (argc-- > 0) {
Elliott Hughese4b64792015-04-17 20:11:08 -0700746 cmd += " " + escape_arg(*argv++);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800747 }
748
Elliott Hughes111a2222015-04-21 17:58:55 -0700749 return send_shell_command(transport, serial, cmd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800750}
751
Dan Albertf30d73c2015-02-25 17:51:28 -0800752static int backup(int argc, const char** argv) {
Elliott Hughese4b64792015-04-17 20:11:08 -0700753 const char* filename = "./backup.ab";
Christopher Tate73779122011-04-21 12:53:28 -0700754
Christopher Tatefba22972011-06-01 17:56:23 -0700755 /* find, extract, and use any -f argument */
Elliott Hughese4b64792015-04-17 20:11:08 -0700756 for (int i = 1; i < argc; i++) {
Christopher Tatefba22972011-06-01 17:56:23 -0700757 if (!strcmp("-f", argv[i])) {
758 if (i == argc-1) {
759 fprintf(stderr, "adb: -f passed with no filename\n");
760 return usage();
761 }
762 filename = argv[i+1];
Elliott Hughese4b64792015-04-17 20:11:08 -0700763 for (int j = i+2; j <= argc; ) {
Christopher Tatefba22972011-06-01 17:56:23 -0700764 argv[i++] = argv[j++];
765 }
766 argc -= 2;
767 argv[argc] = NULL;
768 }
Christopher Tate73779122011-04-21 12:53:28 -0700769 }
770
Christopher Tatecf4f16a2011-08-22 17:12:08 -0700771 /* bare "adb backup" or "adb backup -f filename" are not valid invocations */
772 if (argc < 2) return usage();
773
Christopher Tate1e9f2392011-12-08 19:04:34 -0800774 adb_unlink(filename);
Mark Salyzyn63e39f22014-04-30 09:10:31 -0700775 mkdirs(filename);
Elliott Hughese4b64792015-04-17 20:11:08 -0700776 int outFd = adb_creat(filename, 0640);
Christopher Tate73779122011-04-21 12:53:28 -0700777 if (outFd < 0) {
778 fprintf(stderr, "adb: unable to open file %s\n", filename);
779 return -1;
780 }
781
Elliott Hughese4b64792015-04-17 20:11:08 -0700782 std::string cmd = "backup:";
783 --argc;
784 ++argv;
785 while (argc-- > 0) {
786 cmd += " " + escape_arg(*argv++);
Christopher Tate73779122011-04-21 12:53:28 -0700787 }
788
Elliott Hughese4b64792015-04-17 20:11:08 -0700789 D("backup. filename=%s cmd=%s\n", filename, cmd.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -0700790 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -0700791 int fd = adb_connect(cmd, &error);
Christopher Tate73779122011-04-21 12:53:28 -0700792 if (fd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700793 fprintf(stderr, "adb: unable to connect for backup: %s\n", error.c_str());
Christopher Tate73779122011-04-21 12:53:28 -0700794 adb_close(outFd);
795 return -1;
796 }
797
Christopher Tate9c829102012-01-06 15:43:03 -0800798 printf("Now unlock your device and confirm the backup operation.\n");
Christopher Tate73779122011-04-21 12:53:28 -0700799 copy_to_file(fd, outFd);
800
801 adb_close(fd);
802 adb_close(outFd);
803 return 0;
804}
805
Dan Albertf30d73c2015-02-25 17:51:28 -0800806static int restore(int argc, const char** argv) {
Christopher Tatecf5379b2011-05-17 15:52:54 -0700807 if (argc != 2) return usage();
808
Elliott Hughes04a98c22015-04-29 08:35:59 -0700809 const char* filename = argv[1];
810 int tarFd = adb_open(filename, O_RDONLY);
Christopher Tatecf5379b2011-05-17 15:52:54 -0700811 if (tarFd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700812 fprintf(stderr, "adb: unable to open file %s: %s\n", filename, strerror(errno));
Christopher Tatecf5379b2011-05-17 15:52:54 -0700813 return -1;
814 }
815
Elliott Hughes04a98c22015-04-29 08:35:59 -0700816 std::string error;
817 int fd = adb_connect("restore:", &error);
Christopher Tatecf5379b2011-05-17 15:52:54 -0700818 if (fd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700819 fprintf(stderr, "adb: unable to connect for restore: %s\n", error.c_str());
Christopher Tatecf5379b2011-05-17 15:52:54 -0700820 adb_close(tarFd);
821 return -1;
822 }
823
Christopher Tate9c829102012-01-06 15:43:03 -0800824 printf("Now unlock your device and confirm the restore operation.\n");
Christopher Tatecf5379b2011-05-17 15:52:54 -0700825 copy_to_file(tarFd, fd);
826
827 adb_close(fd);
828 adb_close(tarFd);
829 return 0;
830}
831
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800832/* <hint> may be:
833 * - A simple product name
834 * e.g., "sooner"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800835 * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir
836 * e.g., "out/target/product/sooner"
837 * - An absolute path to the PRODUCT_OUT dir
838 * e.g., "/src/device/out/target/product/sooner"
839 *
840 * Given <hint>, try to construct an absolute path to the
841 * ANDROID_PRODUCT_OUT dir.
842 */
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700843static std::string find_product_out_path(const std::string& hint) {
844 if (hint.empty()) {
Elliott Hughes38104452015-04-17 13:57:15 -0700845 return "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800846 }
847
Elliott Hughes38104452015-04-17 13:57:15 -0700848 // If it's already absolute, don't bother doing any work.
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700849 if (adb_is_absolute_host_path(hint.c_str())) {
Elliott Hughes38104452015-04-17 13:57:15 -0700850 return hint;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800851 }
852
Elliott Hughes38104452015-04-17 13:57:15 -0700853 // If there are any slashes in it, assume it's a relative path;
854 // make it absolute.
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700855 if (hint.find_first_of(OS_PATH_SEPARATORS) != std::string::npos) {
Elliott Hughes7cf35752015-04-17 17:03:59 -0700856 std::string cwd;
857 if (!getcwd(&cwd)) {
858 fprintf(stderr, "adb: getcwd failed: %s\n", strerror(errno));
Elliott Hughes38104452015-04-17 13:57:15 -0700859 return "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800860 }
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700861 return android::base::StringPrintf("%s%c%s", cwd.c_str(), OS_PATH_SEPARATOR, hint.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800862 }
863
Elliott Hughes38104452015-04-17 13:57:15 -0700864 // It's a string without any slashes. Try to do something with it.
865 //
866 // Try to find the root of the build tree, and build a PRODUCT_OUT
867 // path from there.
Elliott Hughes7cf35752015-04-17 17:03:59 -0700868 char* top = getenv("ANDROID_BUILD_TOP");
Elliott Hughes38104452015-04-17 13:57:15 -0700869 if (top == nullptr) {
Elliott Hughes7cf35752015-04-17 17:03:59 -0700870 fprintf(stderr, "adb: ANDROID_BUILD_TOP not set!\n");
Elliott Hughes38104452015-04-17 13:57:15 -0700871 return "";
872 }
Elliott Hughes7cf35752015-04-17 17:03:59 -0700873
874 std::string path = top;
Elliott Hughes38104452015-04-17 13:57:15 -0700875 path += OS_PATH_SEPARATOR_STR;
876 path += "out";
877 path += OS_PATH_SEPARATOR_STR;
878 path += "target";
879 path += OS_PATH_SEPARATOR_STR;
880 path += "product";
881 path += OS_PATH_SEPARATOR_STR;
882 path += hint;
883 if (!directory_exists(path)) {
884 fprintf(stderr, "adb: Couldn't find a product dir based on -p %s; "
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700885 "\"%s\" doesn't exist\n", hint.c_str(), path.c_str());
Elliott Hughes7cf35752015-04-17 17:03:59 -0700886 return "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800887 }
Elliott Hughes38104452015-04-17 13:57:15 -0700888 return path;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800889}
890
Dan Albertf30d73c2015-02-25 17:51:28 -0800891static void parse_push_pull_args(const char **arg, int narg, char const **path1,
892 char const **path2, int *show_progress,
893 int *copy_attrs) {
Mark Lindner9f9d1452014-03-11 17:55:59 -0700894 *show_progress = 0;
Lajos Molnar4e23e3c2013-04-19 12:41:09 -0700895 *copy_attrs = 0;
Mark Lindner9f9d1452014-03-11 17:55:59 -0700896
Lajos Molnar4e23e3c2013-04-19 12:41:09 -0700897 while (narg > 0) {
898 if (!strcmp(*arg, "-p")) {
899 *show_progress = 1;
900 } else if (!strcmp(*arg, "-a")) {
901 *copy_attrs = 1;
902 } else {
903 break;
904 }
Mark Lindner9f9d1452014-03-11 17:55:59 -0700905 ++arg;
906 --narg;
907 }
908
909 if (narg > 0) {
910 *path1 = *arg;
911 ++arg;
912 --narg;
913 }
914
915 if (narg > 0) {
916 *path2 = *arg;
917 }
918}
919
Elliott Hughesda945812015-04-29 12:28:13 -0700920static int adb_connect_command(const std::string& command) {
Elliott Hughes04a98c22015-04-29 08:35:59 -0700921 std::string error;
922 int fd = adb_connect(command, &error);
Elliott Hughesdabb9742015-05-07 23:37:40 -0700923 if (fd < 0) {
924 fprintf(stderr, "error: %s\n", error.c_str());
925 return 1;
Tao Bao0db67642015-03-29 11:22:34 -0700926 }
Elliott Hughesdabb9742015-05-07 23:37:40 -0700927 read_and_dump(fd);
928 adb_close(fd);
929 return 0;
Tao Bao0db67642015-03-29 11:22:34 -0700930}
931
Elliott Hughesda945812015-04-29 12:28:13 -0700932static int adb_query_command(const std::string& command) {
933 std::string result;
934 std::string error;
935 if (!adb_query(command, &result, &error)) {
936 fprintf(stderr, "error: %s\n", error.c_str());
937 return 1;
938 }
939 printf("%s\n", result.c_str());
940 return 0;
941}
942
Elliott Hughesfb596842015-05-01 17:04:38 -0700943int adb_commandline(int argc, const char **argv) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800944 int no_daemon = 0;
945 int is_daemon = 0;
David 'Digit' Turner6826db62011-01-31 14:23:56 +0100946 int is_server = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800947 int r;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700948 TransportType transport_type = kTransportAny;
Siva Velusamya55dbd82015-08-07 10:10:29 -0700949 int ack_reply_fd = -1;
Siva Velusamya55dbd82015-08-07 10:10:29 -0700950
Elliott Hughes38104452015-04-17 13:57:15 -0700951 // If defined, this should be an absolute path to
952 // the directory containing all of the various system images
953 // for a particular product. If not defined, and the adb
954 // command requires this information, then the user must
955 // specify the path using "-p".
956 char* ANDROID_PRODUCT_OUT = getenv("ANDROID_PRODUCT_OUT");
957 if (ANDROID_PRODUCT_OUT != nullptr) {
958 gProductOutPath = ANDROID_PRODUCT_OUT;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800959 }
960 // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
961
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700962 const char* serial = getenv("ANDROID_SERIAL");
Nick Pellyaf2fe9b2009-05-07 12:48:03 -0700963
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100964 /* Validate and assign the server port */
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700965 const char* server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100966 int server_port = DEFAULT_ADB_PORT;
967 if (server_port_str && strlen(server_port_str) > 0) {
Siva Velusamya55dbd82015-08-07 10:10:29 -0700968 server_port = strtol(server_port_str, nullptr, 0);
Matt Gumbel411775c2012-11-14 10:16:17 -0800969 if (server_port <= 0 || server_port > 65535) {
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100970 fprintf(stderr,
Spencer Low85ee64b2015-08-11 17:05:02 -0700971 "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number less than 65536. Got \"%s\"\n",
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100972 server_port_str);
973 return usage();
974 }
975 }
976
977 /* modifiers and flags */
Riley Andrews03b4b372014-12-05 17:37:24 -0800978 while (argc > 0) {
979 if (!strcmp(argv[0],"server")) {
David 'Digit' Turner6826db62011-01-31 14:23:56 +0100980 is_server = 1;
Riley Andrews03b4b372014-12-05 17:37:24 -0800981 } else if (!strcmp(argv[0],"nodaemon")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800982 no_daemon = 1;
983 } else if (!strcmp(argv[0], "fork-server")) {
984 /* this is a special flag used only when the ADB client launches the ADB Server */
985 is_daemon = 1;
Siva Velusamya55dbd82015-08-07 10:10:29 -0700986 } else if (!strcmp(argv[0], "--reply-fd")) {
987 if (argc < 2) return usage();
988 const char* reply_fd_str = argv[1];
989 argc--;
990 argv++;
991 ack_reply_fd = strtol(reply_fd_str, nullptr, 10);
Spencer Lowb28812f2015-08-08 15:07:07 -0700992#ifdef _WIN32
993 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
994 if ((GetStdHandle(STD_INPUT_HANDLE) == ack_reply_handle) ||
995 (GetStdHandle(STD_OUTPUT_HANDLE) == ack_reply_handle) ||
996 (GetStdHandle(STD_ERROR_HANDLE) == ack_reply_handle)) {
997#else
Siva Velusamya55dbd82015-08-07 10:10:29 -0700998 if (ack_reply_fd <= 2) { // Disallow stdin, stdout, and stderr.
Spencer Lowb28812f2015-08-08 15:07:07 -0700999#endif
Siva Velusamya55dbd82015-08-07 10:10:29 -07001000 fprintf(stderr, "adb: invalid reply fd \"%s\"\n", reply_fd_str);
1001 return usage();
1002 }
Riley Andrews03b4b372014-12-05 17:37:24 -08001003 } else if (!strncmp(argv[0], "-p", 2)) {
Elliott Hughes13376832015-07-31 13:18:22 -07001004 const char* product = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001005 if (argv[0][2] == '\0') {
1006 if (argc < 2) return usage();
1007 product = argv[1];
1008 argc--;
1009 argv++;
1010 } else {
Vairavan Srinivasanadc39402012-08-04 16:40:50 -07001011 product = argv[0] + 2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001012 }
Elliott Hughes13376832015-07-31 13:18:22 -07001013 gProductOutPath = find_product_out_path(product);
Elliott Hughes38104452015-04-17 13:57:15 -07001014 if (gProductOutPath.empty()) {
1015 fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001016 return usage();
1017 }
1018 } else if (argv[0][0]=='-' && argv[0][1]=='s') {
1019 if (isdigit(argv[0][2])) {
1020 serial = argv[0] + 2;
1021 } else {
Riley Andrews03b4b372014-12-05 17:37:24 -08001022 if (argc < 2 || argv[0][2] != '\0') return usage();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001023 serial = argv[1];
1024 argc--;
1025 argv++;
1026 }
1027 } else if (!strcmp(argv[0],"-d")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001028 transport_type = kTransportUsb;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001029 } else if (!strcmp(argv[0],"-e")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001030 transport_type = kTransportLocal;
Matt Gumbel411775c2012-11-14 10:16:17 -08001031 } else if (!strcmp(argv[0],"-a")) {
1032 gListenAll = 1;
Riley Andrews03b4b372014-12-05 17:37:24 -08001033 } else if (!strncmp(argv[0], "-H", 2)) {
Matt Gumbel411775c2012-11-14 10:16:17 -08001034 const char *hostname = NULL;
1035 if (argv[0][2] == '\0') {
1036 if (argc < 2) return usage();
1037 hostname = argv[1];
1038 argc--;
1039 argv++;
1040 } else {
1041 hostname = argv[0] + 2;
1042 }
1043 adb_set_tcp_name(hostname);
1044
Riley Andrews03b4b372014-12-05 17:37:24 -08001045 } else if (!strncmp(argv[0], "-P", 2)) {
Matt Gumbel411775c2012-11-14 10:16:17 -08001046 if (argv[0][2] == '\0') {
1047 if (argc < 2) return usage();
1048 server_port_str = argv[1];
1049 argc--;
1050 argv++;
1051 } else {
1052 server_port_str = argv[0] + 2;
1053 }
1054 if (strlen(server_port_str) > 0) {
1055 server_port = (int) strtol(server_port_str, NULL, 0);
1056 if (server_port <= 0 || server_port > 65535) {
1057 fprintf(stderr,
1058 "adb: port number must be a positive number less than 65536. Got \"%s\"\n",
1059 server_port_str);
1060 return usage();
1061 }
1062 } else {
1063 fprintf(stderr,
1064 "adb: port number must be a positive number less than 65536. Got empty string.\n");
1065 return usage();
1066 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001067 } else {
1068 /* out of recognized modifiers and flags */
1069 break;
1070 }
1071 argc--;
1072 argv++;
1073 }
1074
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001075 adb_set_transport(transport_type, serial);
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001076 adb_set_tcp_specifics(server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001077
David 'Digit' Turner6826db62011-01-31 14:23:56 +01001078 if (is_server) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001079 if (no_daemon || is_daemon) {
Spencer Lowb28812f2015-08-08 15:07:07 -07001080 if (is_daemon && (ack_reply_fd == -1)) {
Siva Velusamya55dbd82015-08-07 10:10:29 -07001081 fprintf(stderr, "reply fd for adb server to client communication not specified.\n");
1082 return usage();
1083 }
1084 r = adb_main(is_daemon, server_port, ack_reply_fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001085 } else {
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001086 r = launch_server(server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001087 }
Riley Andrews03b4b372014-12-05 17:37:24 -08001088 if (r) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001089 fprintf(stderr,"* could not start server *\n");
1090 }
1091 return r;
1092 }
1093
Riley Andrews03b4b372014-12-05 17:37:24 -08001094 if (argc == 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001095 return usage();
1096 }
1097
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001098 /* handle wait-for-* prefix */
1099 if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
Dan Albertf30d73c2015-02-25 17:51:28 -08001100 const char* service = argv[0];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001101
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001102 if (!wait_for_device(service, transport_type, serial)) {
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001103 return 1;
1104 }
1105
Elliott Hugheseaabdd62015-05-04 19:29:32 -07001106 // Allow a command to be run after wait-for-device,
1107 // e.g. 'adb wait-for-device shell'.
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001108 if (argc == 1) {
1109 return 0;
1110 }
1111
1112 /* Fall through */
1113 argc--;
1114 argv++;
1115 }
1116
1117 /* adb_connect() commands */
Riley Andrews03b4b372014-12-05 17:37:24 -08001118 if (!strcmp(argv[0], "devices")) {
Dan Albertf30d73c2015-02-25 17:51:28 -08001119 const char *listopt;
Elliott Hughesda945812015-04-29 12:28:13 -07001120 if (argc < 2) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001121 listopt = "";
Elliott Hughesda945812015-04-29 12:28:13 -07001122 } else if (argc == 2 && !strcmp(argv[1], "-l")) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001123 listopt = argv[1];
Elliott Hughesda945812015-04-29 12:28:13 -07001124 } else {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001125 fprintf(stderr, "Usage: adb devices [-l]\n");
1126 return 1;
1127 }
Elliott Hughesda945812015-04-29 12:28:13 -07001128
1129 std::string query = android::base::StringPrintf("host:%s%s", argv[0], listopt);
1130 printf("List of devices attached\n");
1131 return adb_query_command(query);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001132 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001133 else if (!strcmp(argv[0], "connect")) {
Mike Lockwood26b88e32009-08-24 15:58:40 -07001134 if (argc != 2) {
Mike Lockwood01c2c302010-05-24 10:44:35 -04001135 fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
Mike Lockwood26b88e32009-08-24 15:58:40 -07001136 return 1;
1137 }
Elliott Hughesda945812015-04-29 12:28:13 -07001138
1139 std::string query = android::base::StringPrintf("host:connect:%s", argv[1]);
1140 return adb_query_command(query);
Mike Lockwood01c2c302010-05-24 10:44:35 -04001141 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001142 else if (!strcmp(argv[0], "disconnect")) {
Mike Lockwood01c2c302010-05-24 10:44:35 -04001143 if (argc > 2) {
1144 fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
1145 return 1;
1146 }
Elliott Hughesda945812015-04-29 12:28:13 -07001147
1148 std::string query = android::base::StringPrintf("host:disconnect:%s",
1149 (argc == 2) ? argv[1] : "");
1150 return adb_query_command(query);
Mike Lockwood26b88e32009-08-24 15:58:40 -07001151 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001152 else if (!strcmp(argv[0], "emu")) {
Spencer Low7dc759f2015-05-06 16:13:42 -07001153 return adb_send_emulator_command(argc, argv, serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001154 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001155 else if (!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
Daniel Sandlerd9bc2372010-08-19 01:10:18 -04001156 char h = (argv[0][0] == 'h');
1157
1158 if (h) {
1159 printf("\x1b[41;33m");
1160 fflush(stdout);
1161 }
1162
Riley Andrews03b4b372014-12-05 17:37:24 -08001163 if (argc < 2) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001164 D("starting interactive shell\n");
Daniel Sandlerd9bc2372010-08-19 01:10:18 -04001165 r = interactive_shell();
1166 if (h) {
1167 printf("\x1b[0m");
1168 fflush(stdout);
1169 }
1170 return r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001171 }
1172
Elliott Hughes170b5682015-04-17 10:59:34 -07001173 std::string cmd = "shell:";
Elliott Hugheseaabdd62015-05-04 19:29:32 -07001174 --argc;
1175 ++argv;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -07001176 while (argc-- > 0) {
Elliott Hugheseaabdd62015-05-04 19:29:32 -07001177 // We don't escape here, just like ssh(1). http://b/20564385.
1178 cmd += *argv++;
1179 if (*argv) cmd += " ";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001180 }
1181
Elliott Hughes170b5682015-04-17 10:59:34 -07001182 while (true) {
1183 D("interactive shell loop. cmd=%s\n", cmd.c_str());
Elliott Hughes04a98c22015-04-29 08:35:59 -07001184 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -07001185 int fd = adb_connect(cmd, &error);
Elliott Hughes170b5682015-04-17 10:59:34 -07001186 int r;
Riley Andrews03b4b372014-12-05 17:37:24 -08001187 if (fd >= 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001188 D("about to read_and_dump(fd=%d)\n", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001189 read_and_dump(fd);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001190 D("read_and_dump() done.\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001191 adb_close(fd);
1192 r = 0;
1193 } else {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001194 fprintf(stderr,"error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001195 r = -1;
1196 }
1197
Elliott Hughesc68e0e22015-05-05 12:50:26 -07001198 if (h) {
1199 printf("\x1b[0m");
1200 fflush(stdout);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001201 }
Elliott Hughesc68e0e22015-05-05 12:50:26 -07001202 D("interactive shell loop. return r=%d\n", r);
1203 return r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001204 }
1205 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001206 else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -07001207 int exec_in = !strcmp(argv[0], "exec-in");
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -07001208
Elliott Hughes170b5682015-04-17 10:59:34 -07001209 std::string cmd = "exec:";
1210 cmd += argv[1];
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -07001211 argc -= 2;
1212 argv += 2;
1213 while (argc-- > 0) {
Elliott Hughese4b64792015-04-17 20:11:08 -07001214 cmd += " " + escape_arg(*argv++);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -07001215 }
1216
Elliott Hughes04a98c22015-04-29 08:35:59 -07001217 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -07001218 int fd = adb_connect(cmd, &error);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -07001219 if (fd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001220 fprintf(stderr, "error: %s\n", error.c_str());
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -07001221 return -1;
1222 }
1223
1224 if (exec_in) {
1225 copy_to_file(STDIN_FILENO, fd);
1226 } else {
1227 copy_to_file(fd, STDOUT_FILENO);
1228 }
1229
1230 adb_close(fd);
1231 return 0;
1232 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001233 else if (!strcmp(argv[0], "kill-server")) {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001234 std::string error;
1235 int fd = _adb_connect("host:kill", &error);
Spencer Low85ee64b2015-08-11 17:05:02 -07001236 if (fd == -2) {
1237 // Failed to make network connection to server. Don't output the
1238 // network error since that is expected.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001239 fprintf(stderr,"* server not running *\n");
Spencer Low85ee64b2015-08-11 17:05:02 -07001240 // Successful exit code because the server is already "killed".
1241 return 0;
1242 } else if (fd == -1) {
1243 // Some other error.
1244 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001245 return 1;
Spencer Low85ee64b2015-08-11 17:05:02 -07001246 } else {
1247 // Successfully connected, kill command sent, okay status came back.
1248 // Server should exit() in a moment, if not already.
1249 adb_close(fd);
1250 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001251 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001252 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001253 else if (!strcmp(argv[0], "sideload")) {
1254 if (argc != 2) return usage();
Doug Zongkerbcad29f2014-06-26 15:35:36 -07001255 if (adb_sideload_host(argv[1])) {
Doug Zongker6b217ed2012-01-09 14:54:53 -08001256 return 1;
1257 } else {
1258 return 0;
1259 }
1260 }
Elliott Hughesd94e8ba2015-07-21 16:13:40 -07001261 else if (!strcmp(argv[0], "tcpip") && argc > 1) {
1262 return adb_connect_command(android::base::StringPrintf("tcpip:%s", argv[1]));
1263 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001264 else if (!strcmp(argv[0], "remount") ||
1265 !strcmp(argv[0], "reboot") ||
1266 !strcmp(argv[0], "reboot-bootloader") ||
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001267 !strcmp(argv[0], "usb") ||
1268 !strcmp(argv[0], "root") ||
Dan Pasanenfb787d92014-10-06 12:57:20 -05001269 !strcmp(argv[0], "unroot") ||
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001270 !strcmp(argv[0], "disable-verity") ||
1271 !strcmp(argv[0], "enable-verity")) {
Elliott Hughesdabb9742015-05-07 23:37:40 -07001272 std::string command;
Tao Bao0db67642015-03-29 11:22:34 -07001273 if (!strcmp(argv[0], "reboot-bootloader")) {
Elliott Hughesdabb9742015-05-07 23:37:40 -07001274 command = "reboot:bootloader";
Tao Bao0db67642015-03-29 11:22:34 -07001275 } else if (argc > 1) {
Elliott Hughesdabb9742015-05-07 23:37:40 -07001276 command = android::base::StringPrintf("%s:%s", argv[0], argv[1]);
Tao Bao0db67642015-03-29 11:22:34 -07001277 } else {
Elliott Hughesdabb9742015-05-07 23:37:40 -07001278 command = android::base::StringPrintf("%s:", argv[0]);
The Android Open Source Project9c753402009-03-13 13:04:37 -07001279 }
Tao Bao0db67642015-03-29 11:22:34 -07001280 return adb_connect_command(command);
The Android Open Source Project9c753402009-03-13 13:04:37 -07001281 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001282 else if (!strcmp(argv[0], "bugreport")) {
Dan Egnor2857f312010-01-20 13:50:36 -08001283 if (argc != 1) return usage();
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001284 return send_shell_command(transport_type, serial, "shell:bugreport");
Mike Lockwood78589f32009-09-03 14:54:58 -04001285 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001286 else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001287 bool reverse = !strcmp(argv[0], "reverse");
1288 ++argv;
1289 --argc;
1290 if (argc < 1) return usage();
David 'Digit' Turner6c489802012-11-14 15:01:55 +01001291
1292 // Determine the <host-prefix> for this command.
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001293 std::string host_prefix;
David 'Digit' Turner963a4492013-03-21 21:07:42 +01001294 if (reverse) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001295 host_prefix = "reverse";
David 'Digit' Turner6c489802012-11-14 15:01:55 +01001296 } else {
David 'Digit' Turner963a4492013-03-21 21:07:42 +01001297 if (serial) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001298 host_prefix = android::base::StringPrintf("host-serial:%s", serial);
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001299 } else if (transport_type == kTransportUsb) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001300 host_prefix = "host-usb";
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001301 } else if (transport_type == kTransportLocal) {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001302 host_prefix = "host-local";
David 'Digit' Turner963a4492013-03-21 21:07:42 +01001303 } else {
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001304 host_prefix = "host";
David 'Digit' Turner963a4492013-03-21 21:07:42 +01001305 }
David 'Digit' Turner6c489802012-11-14 15:01:55 +01001306 }
1307
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001308 std::string cmd;
1309 if (strcmp(argv[0], "--list") == 0) {
Elliott Hughesfb596842015-05-01 17:04:38 -07001310 if (argc != 1) return usage();
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001311 return adb_query_command(host_prefix + ":list-forward");
1312 } else if (strcmp(argv[0], "--remove-all") == 0) {
1313 if (argc != 1) return usage();
1314 cmd = host_prefix + ":killforward-all";
1315 } else if (strcmp(argv[0], "--remove") == 0) {
1316 // forward --remove <local>
Elliott Hughesfb596842015-05-01 17:04:38 -07001317 if (argc != 2) return usage();
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001318 cmd = host_prefix + ":killforward:" + argv[1];
1319 } else if (strcmp(argv[0], "--no-rebind") == 0) {
1320 // forward --no-rebind <local> <remote>
Elliott Hughesfb596842015-05-01 17:04:38 -07001321 if (argc != 3) return usage();
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001322 cmd = host_prefix + ":forward:norebind:" + argv[1] + ";" + argv[2];
1323 } else {
1324 // forward <local> <remote>
1325 if (argc != 2) return usage();
1326 cmd = host_prefix + ":forward:" + argv[0] + ";" + argv[1];
David 'Digit' Turner6c489802012-11-14 15:01:55 +01001327 }
1328
Elliott Hughesd98ca8a2015-05-29 17:55:19 -07001329 return adb_command(cmd) ? 0 : 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001330 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001331 /* do_sync_*() commands */
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001332 else if (!strcmp(argv[0], "ls")) {
1333 if (argc != 2) return usage();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001334 return do_sync_ls(argv[1]);
1335 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001336 else if (!strcmp(argv[0], "push")) {
Mark Lindner9f9d1452014-03-11 17:55:59 -07001337 int show_progress = 0;
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001338 int copy_attrs = 0; // unused
Mark Lindner9f9d1452014-03-11 17:55:59 -07001339 const char* lpath = NULL, *rpath = NULL;
1340
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001341 parse_push_pull_args(&argv[1], argc - 1, &lpath, &rpath, &show_progress, &copy_attrs);
Mark Lindner9f9d1452014-03-11 17:55:59 -07001342
1343 if ((lpath != NULL) && (rpath != NULL)) {
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001344 return do_sync_push(lpath, rpath, show_progress);
Mark Lindner9f9d1452014-03-11 17:55:59 -07001345 }
1346
1347 return usage();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001348 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001349 else if (!strcmp(argv[0], "pull")) {
Mark Lindner9f9d1452014-03-11 17:55:59 -07001350 int show_progress = 0;
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001351 int copy_attrs = 0;
Mark Lindner9f9d1452014-03-11 17:55:59 -07001352 const char* rpath = NULL, *lpath = ".";
1353
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001354 parse_push_pull_args(&argv[1], argc - 1, &rpath, &lpath, &show_progress, &copy_attrs);
Mark Lindner9f9d1452014-03-11 17:55:59 -07001355
1356 if (rpath != NULL) {
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001357 return do_sync_pull(rpath, lpath, show_progress, copy_attrs);
Joe Onorato23595b02010-01-05 13:42:25 -08001358 }
Mark Lindner9f9d1452014-03-11 17:55:59 -07001359
1360 return usage();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001361 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001362 else if (!strcmp(argv[0], "install")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001363 if (argc < 2) return usage();
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001364 return install_app(transport_type, serial, argc, argv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001365 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001366 else if (!strcmp(argv[0], "install-multiple")) {
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001367 if (argc < 2) return usage();
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001368 return install_multiple_app(transport_type, serial, argc, argv);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001369 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001370 else if (!strcmp(argv[0], "uninstall")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001371 if (argc < 2) return usage();
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001372 return uninstall_app(transport_type, serial, argc, argv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001373 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001374 else if (!strcmp(argv[0], "sync")) {
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001375 std::string src;
Elliott Hughes38104452015-04-17 13:57:15 -07001376 bool list_only = false;
Riley Andrews03b4b372014-12-05 17:37:24 -08001377 if (argc < 2) {
Elliott Hughes38104452015-04-17 13:57:15 -07001378 // No local path was specified.
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001379 src = "";
Anthony Newnamdd2db142010-02-22 08:36:49 -06001380 } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001381 list_only = true;
Anthony Newnamdd2db142010-02-22 08:36:49 -06001382 if (argc == 3) {
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001383 src = argv[2];
Anthony Newnamdd2db142010-02-22 08:36:49 -06001384 } else {
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001385 src = "";
Anthony Newnamdd2db142010-02-22 08:36:49 -06001386 }
Riley Andrews03b4b372014-12-05 17:37:24 -08001387 } else if (argc == 2) {
Elliott Hughes38104452015-04-17 13:57:15 -07001388 // A local path or "android"/"data" arg was specified.
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001389 src = argv[1];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001390 } else {
1391 return usage();
1392 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001393
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001394 if (src != "" &&
1395 src != "system" && src != "data" && src != "vendor" && src != "oem") {
Elliott Hughes38104452015-04-17 13:57:15 -07001396 return usage();
1397 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001398
Elliott Hughes38104452015-04-17 13:57:15 -07001399 std::string system_src_path = product_file("system");
1400 std::string data_src_path = product_file("data");
1401 std::string vendor_src_path = product_file("vendor");
1402 std::string oem_src_path = product_file("oem");
Elliott Hughes38104452015-04-17 13:57:15 -07001403
1404 int rc = 0;
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001405 if (rc == 0 && (src.empty() || src == "system")) {
1406 rc = do_sync_sync(system_src_path, "/system", list_only);
Elliott Hughes38104452015-04-17 13:57:15 -07001407 }
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001408 if (rc == 0 && (src.empty() || src == "vendor") && directory_exists(vendor_src_path)) {
1409 rc = do_sync_sync(vendor_src_path, "/vendor", list_only);
Elliott Hughes38104452015-04-17 13:57:15 -07001410 }
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001411 if (rc == 0 && (src.empty() || src == "oem") && directory_exists(oem_src_path)) {
1412 rc = do_sync_sync(oem_src_path, "/oem", list_only);
Elliott Hughes38104452015-04-17 13:57:15 -07001413 }
Elliott Hughesc5a12b22015-04-21 10:17:07 -07001414 if (rc == 0 && (src.empty() || src == "data")) {
1415 rc = do_sync_sync(data_src_path, "/data", list_only);
Elliott Hughes38104452015-04-17 13:57:15 -07001416 }
1417 return rc;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001418 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001419 /* passthrough commands */
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001420 else if (!strcmp(argv[0],"get-state") ||
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001421 !strcmp(argv[0],"get-serialno") ||
1422 !strcmp(argv[0],"get-devpath"))
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001423 {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001424 return adb_query_command(format_host_command(argv[0], transport_type, serial));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001425 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001426 /* other commands */
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001427 else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001428 return logcat(transport_type, serial, argc, argv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001429 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001430 else if (!strcmp(argv[0],"ppp")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001431 return ppp(argc, argv);
1432 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001433 else if (!strcmp(argv[0], "start-server")) {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001434 std::string error;
Spencer Low85ee64b2015-08-11 17:05:02 -07001435 const int result = adb_connect("host:start-server", &error);
1436 if (result < 0) {
1437 fprintf(stderr, "error: %s\n", error.c_str());
1438 }
1439 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001440 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001441 else if (!strcmp(argv[0], "backup")) {
Christopher Tate73779122011-04-21 12:53:28 -07001442 return backup(argc, argv);
1443 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001444 else if (!strcmp(argv[0], "restore")) {
Christopher Tatecf5379b2011-05-17 15:52:54 -07001445 return restore(argc, argv);
1446 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001447 else if (!strcmp(argv[0], "keygen")) {
Nick Kralevich6183c962014-11-13 15:17:29 -08001448 if (argc < 2) return usage();
1449 return adb_auth_keygen(argv[1]);
1450 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001451 else if (!strcmp(argv[0], "jdwp")) {
Tao Bao0db67642015-03-29 11:22:34 -07001452 return adb_connect_command("jdwp");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001453 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001454 /* "adb /?" is a common idiom under Windows */
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001455 else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001456 help();
1457 return 0;
1458 }
Riley Andrewsfacb92d2014-12-05 17:32:46 -08001459 else if (!strcmp(argv[0], "version")) {
Elliott Hughesb9f01642015-08-12 08:32:10 -07001460 fprintf(stdout, "%s", adb_version().c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001461 return 0;
1462 }
1463
1464 usage();
1465 return 1;
1466}
1467
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001468static int pm_command(TransportType transport, const char* serial, int argc, const char** argv) {
Elliott Hughes170b5682015-04-17 10:59:34 -07001469 std::string cmd = "shell:pm";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001470
Elliott Hughes170b5682015-04-17 10:59:34 -07001471 while (argc-- > 0) {
Elliott Hughese4b64792015-04-17 20:11:08 -07001472 cmd += " " + escape_arg(*argv++);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001473 }
1474
Elliott Hughes111a2222015-04-21 17:58:55 -07001475 return send_shell_command(transport, serial, cmd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001476}
1477
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001478static int uninstall_app(TransportType transport, const char* serial, int argc, const char** argv) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001479 /* if the user choose the -k option, we refuse to do it until devices are
1480 out with the option to uninstall the remaining data somehow (adb/ui) */
1481 if (argc == 3 && strcmp(argv[1], "-k") == 0)
1482 {
1483 printf(
1484 "The -k option uninstalls the application while retaining the data/cache.\n"
1485 "At the moment, there is no way to remove the remaining data.\n"
1486 "You will have to reinstall the application with the same signature, and fully uninstall it.\n"
1487 "If you truly wish to continue, execute 'adb shell pm uninstall -k %s'\n", argv[2]);
1488 return -1;
1489 }
1490
1491 /* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */
1492 return pm_command(transport, serial, argc, argv);
1493}
1494
Elliott Hughesd189cfb2015-07-30 17:42:01 -07001495static int delete_file(TransportType transport, const char* serial, const std::string& filename) {
Elliott Hughes170b5682015-04-17 10:59:34 -07001496 std::string cmd = "shell:rm -f " + escape_arg(filename);
Elliott Hughes111a2222015-04-21 17:58:55 -07001497 return send_shell_command(transport, serial, cmd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001498}
1499
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001500static int install_app(TransportType transport, const char* serial, int argc, const char** argv) {
Dan Albert3d978e62015-07-09 20:35:09 +00001501 static const char *const DATA_DEST = "/data/local/tmp/%s";
1502 static const char *const SD_DEST = "/sdcard/tmp/%s";
Kenny Root3802c992011-08-05 11:19:45 -07001503 const char* where = DATA_DEST;
Kenny Root3802c992011-08-05 11:19:45 -07001504 int i;
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001505 struct stat sb;
Kenny Root3802c992011-08-05 11:19:45 -07001506
1507 for (i = 1; i < argc; i++) {
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001508 if (!strcmp(argv[i], "-s")) {
Kenny Root3802c992011-08-05 11:19:45 -07001509 where = SD_DEST;
1510 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001511 }
1512
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001513 // Find last APK argument.
1514 // All other arguments passed through verbatim.
1515 int last_apk = -1;
1516 for (i = argc - 1; i >= 0; i--) {
Dan Albertf30d73c2015-02-25 17:51:28 -08001517 const char* file = argv[i];
Elliott Hughes24f9b082015-07-27 21:21:39 -07001518 const char* dot = strrchr(file, '.');
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001519 if (dot && !strcasecmp(dot, ".apk")) {
1520 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
1521 fprintf(stderr, "Invalid APK file: %s\n", file);
1522 return -1;
1523 }
1524
1525 last_apk = i;
1526 break;
1527 }
Kenny Root3802c992011-08-05 11:19:45 -07001528 }
1529
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001530 if (last_apk == -1) {
1531 fprintf(stderr, "Missing APK file\n");
1532 return -1;
Kenny Root3802c992011-08-05 11:19:45 -07001533 }
1534
Dan Albertf30d73c2015-02-25 17:51:28 -08001535 const char* apk_file = argv[last_apk];
Elliott Hughesd189cfb2015-07-30 17:42:01 -07001536 std::string apk_dest = android::base::StringPrintf(where, adb_basename(apk_file).c_str());
1537 int err = do_sync_push(apk_file, apk_dest.c_str(), 0 /* no show progress */);
Kenny Root3802c992011-08-05 11:19:45 -07001538 if (err) {
Kenny Root58d5f222012-03-26 16:14:02 -07001539 goto cleanup_apk;
Kenny Root3802c992011-08-05 11:19:45 -07001540 } else {
Elliott Hughesd189cfb2015-07-30 17:42:01 -07001541 argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
Kenny Root3802c992011-08-05 11:19:45 -07001542 }
1543
Elliott Hughes111a2222015-04-21 17:58:55 -07001544 err = pm_command(transport, serial, argc, argv);
Kenny Root3802c992011-08-05 11:19:45 -07001545
Kenny Root58d5f222012-03-26 16:14:02 -07001546cleanup_apk:
Dan Albert3d978e62015-07-09 20:35:09 +00001547 delete_file(transport, serial, apk_dest);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001548 return err;
1549}
1550
Elliott Hughes3aec2ba2015-05-05 13:10:43 -07001551static int install_multiple_app(TransportType transport, const char* serial, int argc,
Elliott Hughes38104452015-04-17 13:57:15 -07001552 const char** argv)
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001553{
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001554 int i;
1555 struct stat sb;
Elliott Hughesd1a5b2b2015-04-17 14:07:52 -07001556 uint64_t total_size = 0;
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001557
1558 // Find all APK arguments starting at end.
1559 // All other arguments passed through verbatim.
1560 int first_apk = -1;
1561 for (i = argc - 1; i >= 0; i--) {
Dan Albertf30d73c2015-02-25 17:51:28 -08001562 const char* file = argv[i];
Elliott Hughes24f9b082015-07-27 21:21:39 -07001563 const char* dot = strrchr(file, '.');
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001564 if (dot && !strcasecmp(dot, ".apk")) {
1565 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
1566 fprintf(stderr, "Invalid APK file: %s\n", file);
1567 return -1;
1568 }
1569
1570 total_size += sb.st_size;
1571 first_apk = i;
1572 } else {
1573 break;
1574 }
Kenny Root3802c992011-08-05 11:19:45 -07001575 }
1576
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001577 if (first_apk == -1) {
1578 fprintf(stderr, "Missing APK file\n");
1579 return 1;
1580 }
Kenny Root3802c992011-08-05 11:19:45 -07001581
Elliott Hughesd1a5b2b2015-04-17 14:07:52 -07001582 std::string cmd = android::base::StringPrintf("exec:pm install-create -S %" PRIu64, total_size);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001583 for (i = 1; i < first_apk; i++) {
Elliott Hughese4b64792015-04-17 20:11:08 -07001584 cmd += " " + escape_arg(argv[i]);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001585 }
1586
1587 // Create install session
Elliott Hughes04a98c22015-04-29 08:35:59 -07001588 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -07001589 int fd = adb_connect(cmd, &error);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001590 if (fd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001591 fprintf(stderr, "Connect error for create: %s\n", error.c_str());
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001592 return -1;
1593 }
Elliott Hughes170b5682015-04-17 10:59:34 -07001594 char buf[BUFSIZ];
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001595 read_status_line(fd, buf, sizeof(buf));
1596 adb_close(fd);
1597
1598 int session_id = -1;
1599 if (!strncmp("Success", buf, 7)) {
1600 char* start = strrchr(buf, '[');
1601 char* end = strrchr(buf, ']');
1602 if (start && end) {
1603 *end = '\0';
1604 session_id = strtol(start + 1, NULL, 10);
1605 }
1606 }
1607 if (session_id < 0) {
1608 fprintf(stderr, "Failed to create session\n");
Christopher Tate29721da2014-07-14 16:45:13 -07001609 fputs(buf, stderr);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001610 return -1;
1611 }
1612
1613 // Valid session, now stream the APKs
1614 int success = 1;
1615 for (i = first_apk; i < argc; i++) {
Dan Albertf30d73c2015-02-25 17:51:28 -08001616 const char* file = argv[i];
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001617 if (stat(file, &sb) == -1) {
1618 fprintf(stderr, "Failed to stat %s\n", file);
1619 success = 0;
1620 goto finalize_session;
1621 }
1622
Elliott Hughesd1a5b2b2015-04-17 14:07:52 -07001623 std::string cmd = android::base::StringPrintf(
1624 "exec:pm install-write -S %" PRIu64 " %d %d_%s -",
Elliott Hughesd189cfb2015-07-30 17:42:01 -07001625 static_cast<uint64_t>(sb.st_size), session_id, i, adb_basename(file).c_str());
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001626
1627 int localFd = adb_open(file, O_RDONLY);
1628 if (localFd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001629 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001630 success = 0;
1631 goto finalize_session;
1632 }
1633
Elliott Hughes04a98c22015-04-29 08:35:59 -07001634 std::string error;
Elliott Hughesda945812015-04-29 12:28:13 -07001635 int remoteFd = adb_connect(cmd, &error);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001636 if (remoteFd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001637 fprintf(stderr, "Connect error for write: %s\n", error.c_str());
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001638 adb_close(localFd);
1639 success = 0;
1640 goto finalize_session;
1641 }
1642
1643 copy_to_file(localFd, remoteFd);
1644 read_status_line(remoteFd, buf, sizeof(buf));
1645
1646 adb_close(localFd);
1647 adb_close(remoteFd);
1648
1649 if (strncmp("Success", buf, 7)) {
1650 fprintf(stderr, "Failed to write %s\n", file);
Christopher Tate29721da2014-07-14 16:45:13 -07001651 fputs(buf, stderr);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001652 success = 0;
1653 goto finalize_session;
1654 }
1655 }
1656
1657finalize_session:
Jeff Sharkey01f6fbf2014-07-25 09:58:25 -07001658 // Commit session if we streamed everything okay; otherwise abandon
Elliott Hughesda945812015-04-29 12:28:13 -07001659 std::string service =
1660 android::base::StringPrintf("exec:pm install-%s %d",
1661 success ? "commit" : "abandon", session_id);
1662 fd = adb_connect(service, &error);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001663 if (fd < 0) {
Elliott Hughes04a98c22015-04-29 08:35:59 -07001664 fprintf(stderr, "Connect error for finalize: %s\n", error.c_str());
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001665 return -1;
1666 }
1667 read_status_line(fd, buf, sizeof(buf));
1668 adb_close(fd);
1669
1670 if (!strncmp("Success", buf, 7)) {
Christopher Tate29721da2014-07-14 16:45:13 -07001671 fputs(buf, stderr);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001672 return 0;
1673 } else {
1674 fprintf(stderr, "Failed to finalize session\n");
Christopher Tate29721da2014-07-14 16:45:13 -07001675 fputs(buf, stderr);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -07001676 return -1;
1677 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001678}