blob: a7208593fee3f918a6a74c1071e12177a8906d69 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albert76649012015-02-24 15:51:19 -080021#include <assert.h>
22#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <errno.h>
Elliott Hughes2940ccf2015-04-17 14:07:52 -070024#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <limits.h>
26#include <stdarg.h>
Dan Albert76649012015-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 Projectdd7bc332009-03-03 19:32:55 -080031#include <sys/stat.h>
Dan Albert76649012015-02-24 15:51:19 -080032#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
David Pursell606835a2015-09-08 17:17:02 -070034#include <memory>
Elliott Hughes2baae3a2015-04-17 10:59:34 -070035#include <string>
36
David Pursell606835a2015-09-08 17:17:02 -070037#include <base/logging.h>
Elliott Hughes2baae3a2015-04-17 10:59:34 -070038#include <base/stringprintf.h>
David Pursell4e2fd362015-09-22 10:43:08 -070039#include <base/strings.h>
Elliott Hughes2baae3a2015-04-17 10:59:34 -070040
Yabin Cuid325e862014-11-17 14:48:25 -080041#if !defined(_WIN32)
Josh Gao8dcdb572015-10-23 15:03:31 -070042#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include <termios.h>
Dan Albert76649012015-02-24 15:51:19 -080044#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045#endif
46
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include "adb.h"
Nick Kralevichbea3f9c2014-11-13 15:17:29 -080048#include "adb_auth.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080049#include "adb_client.h"
50#include "adb_io.h"
Elliott Hughes58305772015-04-17 13:57:15 -070051#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#include "file_sync_service.h"
David Pursell70ef7b42015-09-30 13:35:42 -070053#include "services.h"
David Pursell606835a2015-09-08 17:17:02 -070054#include "shell_service.h"
55#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Elliott Hughes3bd73c12015-05-05 13:10:43 -070057static int install_app(TransportType t, const char* serial, int argc, const char** argv);
58static int install_multiple_app(TransportType t, const char* serial, int argc, const char** argv);
59static int uninstall_app(TransportType t, const char* serial, int argc, const char** argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes58305772015-04-17 13:57:15 -070061static std::string gProductOutPath;
Matt Gumbeld7b33082012-11-14 10:16:17 -080062extern int gListenAll;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Elliott Hughes58305772015-04-17 13:57:15 -070064static std::string product_file(const char *extra) {
65 if (gProductOutPath.empty()) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 fprintf(stderr, "adb: Product directory not specified; "
67 "use -p or define ANDROID_PRODUCT_OUT\n");
68 exit(1);
69 }
70
Elliott Hughes58305772015-04-17 13:57:15 -070071 return android::base::StringPrintf("%s%s%s",
72 gProductOutPath.c_str(), OS_PATH_SEPARATOR_STR, extra);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073}
74
Elliott Hughes58305772015-04-17 13:57:15 -070075static void help() {
Elliott Hughes42ae2602015-08-12 08:32:10 -070076 fprintf(stderr, "%s\n", adb_version().c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 fprintf(stderr,
Matt Gumbeld7b33082012-11-14 10:16:17 -080078 " -a - directs adb to listen on all interfaces for a connection\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 " -d - directs command to the only connected USB device\n"
80 " returns an error if more than one USB device is present.\n"
81 " -e - directs command to the only running emulator.\n"
82 " returns an error if more than one emulator is running.\n"
Scott Andersone109d262012-04-20 11:21:14 -070083 " -s <specific device> - directs command to the device or emulator with the given\n"
Scott Anderson2ca3e6b2012-05-30 18:11:27 -070084 " serial number or qualifier. Overrides ANDROID_SERIAL\n"
Elliott Hughes31dbed72009-10-07 15:38:53 -070085 " environment variable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 " -p <product name or path> - simple product name like 'sooner', or\n"
87 " a relative/absolute path to a product\n"
88 " out directory like 'out/target/product/sooner'.\n"
89 " If -p is not specified, the ANDROID_PRODUCT_OUT\n"
90 " environment variable is used, which must\n"
91 " be an absolute path.\n"
Matt Gumbeld7b33082012-11-14 10:16:17 -080092 " -H - Name of adb server host (default: localhost)\n"
93 " -P - Port of adb server (default: 5037)\n"
Scott Andersone109d262012-04-20 11:21:14 -070094 " devices [-l] - list all connected devices\n"
Scott Anderson2ca3e6b2012-05-30 18:11:27 -070095 " ('-l' will also list device qualifiers)\n"
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -040096 " connect <host>[:<port>] - connect to a device via TCP/IP\n"
97 " Port 5555 is used by default if no port number is specified.\n"
98 " disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.\n"
99 " Port 5555 is used by default if no port number is specified.\n"
Bernhard Reutner-Fischer6715a432011-04-26 12:46:05 +0200100 " Using this command with no additional arguments\n"
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400101 " will disconnect from all connected TCP/IP devices.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 "\n"
103 "device commands:\n"
Elliott Hughesb708d162015-10-27 16:03:15 -0700104 " adb push <local> <remote>\n"
Mark Lindner76f2a932014-03-11 17:55:59 -0700105 " - copy file/dir to device\n"
Elliott Hughesb708d162015-10-27 16:03:15 -0700106 " adb pull [-a] <remote> [<local>]\n"
Mark Lindner76f2a932014-03-11 17:55:59 -0700107 " - copy file/dir from device\n"
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700108 " ('-a' means copy timestamp and mode)\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 " adb sync [ <directory> ] - copy host->device only if changed\n"
Anthony Newnam705c9442010-02-22 08:36:49 -0600110 " (-l means list but don't copy)\n"
David Pursell70ef7b42015-09-30 13:35:42 -0700111 " adb shell [-Ttx] - run remote shell interactively\n"
112 " adb shell [-Ttx] <command> - run remote shell command\n"
David Pursell4e2fd362015-09-22 10:43:08 -0700113 " (-T disables PTY allocation)\n"
114 " (-t forces PTY allocation)\n"
David Pursell70ef7b42015-09-30 13:35:42 -0700115 " (-x disables remote exit codes and stdout/stderr separation)\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 " adb emu <command> - run emulator console command\n"
117 " adb logcat [ <filter-spec> ] - View device log\n"
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100118 " adb forward --list - list all forward socket connections.\n"
119 " the format is a list of lines with the following format:\n"
120 " <serial> \" \" <local> \" \" <remote> \"\\n\"\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 " adb forward <local> <remote> - forward socket connections\n"
122 " forward specs are one of: \n"
123 " tcp:<port>\n"
124 " localabstract:<unix domain socket name>\n"
125 " localreserved:<unix domain socket name>\n"
126 " localfilesystem:<unix domain socket name>\n"
127 " dev:<character device name>\n"
128 " jdwp:<process pid> (remote only)\n"
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100129 " adb forward --no-rebind <local> <remote>\n"
130 " - same as 'adb forward <local> <remote>' but fails\n"
131 " if <local> is already forwarded\n"
132 " adb forward --remove <local> - remove a specific forward socket connection\n"
133 " adb forward --remove-all - remove all forward socket connections\n"
David 'Digit' Turner25258692013-03-21 21:07:42 +0100134 " adb reverse --list - list all reverse socket connections from device\n"
135 " adb reverse <remote> <local> - reverse socket connections\n"
136 " reverse specs are one of:\n"
137 " tcp:<port>\n"
138 " localabstract:<unix domain socket name>\n"
139 " localreserved:<unix domain socket name>\n"
140 " localfilesystem:<unix domain socket name>\n"
141 " adb reverse --norebind <remote> <local>\n"
142 " - same as 'adb reverse <remote> <local>' but fails\n"
143 " if <remote> is already reversed.\n"
144 " adb reverse --remove <remote>\n"
145 " - remove a specific reversed socket connection\n"
146 " adb reverse --remove-all - remove all reversed socket connections from device\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 " adb jdwp - list PIDs of processes hosting a JDWP transport\n"
Lorenzo Colitti0b3baac2015-05-28 12:03:44 +0900148 " adb install [-lrtsdg] <file>\n"
Svetoslav23d84072015-06-01 16:02:50 -0700149 " - push this package file to the device and install it\n"
150 " (-l: forward lock application)\n"
151 " (-r: replace existing application)\n"
152 " (-t: allow test packages)\n"
153 " (-s: install application on sdcard)\n"
154 " (-d: allow version code downgrade)\n"
155 " (-g: grant all runtime permissions)\n"
Lorenzo Colitti0b3baac2015-05-28 12:03:44 +0900156 " adb install-multiple [-lrtsdpg] <file...>\n"
Anonymous Coward4474ac42012-04-24 10:43:41 -0700157 " - push this package file to the device and install it\n"
Jeff Sharkey960df972014-06-09 17:30:57 -0700158 " (-l: forward lock application)\n"
159 " (-r: replace existing application)\n"
160 " (-t: allow test packages)\n"
161 " (-s: install application on sdcard)\n"
162 " (-d: allow version code downgrade)\n"
163 " (-p: partial application install)\n"
Lorenzo Colitti0b3baac2015-05-28 12:03:44 +0900164 " (-g: grant all runtime permissions)\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 " adb uninstall [-k] <package> - remove this app package from the device\n"
166 " ('-k' means keep the data and cache directories)\n"
167 " adb bugreport - return all information from the device\n"
168 " that should be included in a bug report.\n"
169 "\n"
Christopher Tate0c06eb52013-03-06 16:40:52 -0800170 " adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]\n"
Christopher Tate56885092011-10-03 18:27:01 -0700171 " - write an archive of the device's data to <file>.\n"
172 " If no -f option is supplied then the data is written\n"
173 " to \"backup.ab\" in the current directory.\n"
Christopher Tated2f54152011-04-21 12:53:28 -0700174 " (-apk|-noapk enable/disable backup of the .apks themselves\n"
Christopher Tatede034ec2011-08-09 17:05:29 -0700175 " in the archive; the default is noapk.)\n"
Christopher Tate0c06eb52013-03-06 16:40:52 -0800176 " (-obb|-noobb enable/disable backup of any installed apk expansion\n"
177 " (aka .obb) files associated with each application; the default\n"
178 " is noobb.)\n"
Christopher Tated2f54152011-04-21 12:53:28 -0700179 " (-shared|-noshared enable/disable backup of the device's\n"
180 " shared storage / SD card contents; the default is noshared.)\n"
181 " (-all means to back up all installed applications)\n"
Christopher Tate56885092011-10-03 18:27:01 -0700182 " (-system|-nosystem toggles whether -all automatically includes\n"
183 " system applications; the default is to include system apps)\n"
Christopher Tated2f54152011-04-21 12:53:28 -0700184 " (<packages...> is the list of applications to be backed up. If\n"
185 " the -all or -shared flags are passed, then the package\n"
Christopher Tate56885092011-10-03 18:27:01 -0700186 " list is optional. Applications explicitly given on the\n"
187 " command line will be included even if -nosystem would\n"
188 " ordinarily cause them to be omitted.)\n"
Christopher Tated2f54152011-04-21 12:53:28 -0700189 "\n"
Christopher Tatede034ec2011-08-09 17:05:29 -0700190 " adb restore <file> - restore device contents from the <file> backup archive\n"
Christopher Tate702967a2011-05-17 15:52:54 -0700191 "\n"
Paul Lawrence982089d2014-12-03 15:31:57 -0800192 " adb disable-verity - disable dm-verity checking on USERDEBUG builds\n"
193 " adb enable-verity - re-enable dm-verity checking on USERDEBUG builds\n"
Nick Kralevichbea3f9c2014-11-13 15:17:29 -0800194 " adb keygen <file> - generate adb public/private key. The private key is stored in <file>,\n"
195 " and the public key is stored in <file>.pub. Any existing files\n"
196 " are overwritten.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 " adb help - show this help message\n"
198 " adb version - show version num\n"
199 "\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 "scripting:\n"
201 " adb wait-for-device - block until device is online\n"
202 " adb start-server - ensure that there is a server running\n"
203 " adb kill-server - kill the server if it is running\n"
204 " adb get-state - prints: offline | bootloader | device\n"
205 " adb get-serialno - prints: <serial-number>\n"
Scott Andersone109d262012-04-20 11:21:14 -0700206 " adb get-devpath - prints: <device-path>\n"
Elliott Hughesec7a6672015-03-16 21:58:32 +0000207 " adb remount - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write\n"
Tao Bao175b7bb2015-03-29 11:22:34 -0700208 " adb reboot [bootloader|recovery]\n"
209 " - reboots the device, optionally into the bootloader or recovery program.\n"
210 " adb reboot sideload - reboots the device into the sideload mode in recovery program (adb root required).\n"
211 " adb reboot sideload-auto-reboot\n"
212 " - reboots into the sideload mode, then reboots automatically after the sideload regardless of the result.\n"
Elliott Hughes7e067cf2015-06-12 14:26:29 -0700213 " adb sideload <file> - sideloads the given package\n"
Mike Lockwoodff196702009-08-24 15:58:40 -0700214 " adb root - restarts the adbd daemon with root permissions\n"
Dan Pasanen98858812014-10-06 12:57:20 -0500215 " adb unroot - restarts the adbd daemon without root permissions\n"
Romain Guy311add42009-12-14 14:42:17 -0800216 " adb usb - restarts the adbd daemon listening on USB\n"
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000217 " adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port\n"
Elliott Hughes7e067cf2015-06-12 14:26:29 -0700218 "\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 "networking:\n"
220 " adb ppp <tty> [parameters] - Run PPP over USB.\n"
Kenny Rootc9891992009-06-08 14:40:30 -0500221 " Note: you should not automatically start a PPP connection.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 " <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1\n"
223 " [parameters] - Eg. defaultroute debug dump local notty usepeerdns\n"
224 "\n"
225 "adb sync notes: adb sync [ <directory> ]\n"
226 " <localdir> can be interpreted in several ways:\n"
227 "\n"
Elliott Hughesec7a6672015-03-16 21:58:32 +0000228 " - If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 "\n"
Elliott Hughesec7a6672015-03-16 21:58:32 +0000230 " - If it is \"system\", \"vendor\", \"oem\" or \"data\", only the corresponding partition\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 " is updated.\n"
Timcd643152010-02-16 20:18:29 +0000232 "\n"
Elliott Hughes7e067cf2015-06-12 14:26:29 -0700233 "environment variables:\n"
Timcd643152010-02-16 20:18:29 +0000234 " ADB_TRACE - Print debug information. A comma separated list of the following values\n"
235 " 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n"
236 " ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.\n"
237 " ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 );
239}
240
Elliott Hughes58305772015-04-17 13:57:15 -0700241static int usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 help();
243 return 1;
244}
245
Yabin Cuid325e862014-11-17 14:48:25 -0800246#if defined(_WIN32)
247
Elliott Hughesa2f2e562015-04-16 16:47:02 -0700248// Implemented in sysdeps_win32.cpp.
Spencer Low50184062015-03-01 15:06:21 -0800249void stdin_raw_init(int fd);
250void stdin_raw_restore(int fd);
Yabin Cuid325e862014-11-17 14:48:25 -0800251
252#else
Alistair Buxtondfa09fd2013-03-01 22:16:41 +0100253static termios g_saved_terminal_state;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254
Alistair Buxtondfa09fd2013-03-01 22:16:41 +0100255static void stdin_raw_init(int fd) {
256 if (tcgetattr(fd, &g_saved_terminal_state)) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
Alistair Buxtondfa09fd2013-03-01 22:16:41 +0100258 termios tio;
259 if (tcgetattr(fd, &tio)) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260
Alistair Buxtondfa09fd2013-03-01 22:16:41 +0100261 cfmakeraw(&tio);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
Alistair Buxtondfa09fd2013-03-01 22:16:41 +0100263 // No timeout but request at least one character per read.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 tio.c_cc[VTIME] = 0;
265 tio.c_cc[VMIN] = 1;
266
Alistair Buxtondfa09fd2013-03-01 22:16:41 +0100267 tcsetattr(fd, TCSAFLUSH, &tio);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268}
269
Alistair Buxtondfa09fd2013-03-01 22:16:41 +0100270static void stdin_raw_restore(int fd) {
271 tcsetattr(fd, TCSAFLUSH, &g_saved_terminal_state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272}
273#endif
274
David Pursell606835a2015-09-08 17:17:02 -0700275// Reads from |fd| and prints received data. If |use_shell_protocol| is true
276// this expects that incoming data will use the shell protocol, in which case
277// stdout/stderr are routed independently and the remote exit code will be
278// returned.
279static int read_and_dump(int fd, bool use_shell_protocol=false) {
280 int exit_code = 0;
281 std::unique_ptr<ShellProtocol> protocol;
282 int length = 0;
283 FILE* outfile = stdout;
284
285 char raw_buffer[BUFSIZ];
286 char* buffer_ptr = raw_buffer;
287 if (use_shell_protocol) {
288 protocol.reset(new ShellProtocol(fd));
289 if (!protocol) {
290 LOG(ERROR) << "failed to allocate memory for ShellProtocol object";
291 return 1;
292 }
293 buffer_ptr = protocol->data();
294 }
295
Elliott Hughes5677c232015-05-07 23:37:40 -0700296 while (fd >= 0) {
David Pursell606835a2015-09-08 17:17:02 -0700297 if (use_shell_protocol) {
298 if (!protocol->Read()) {
299 break;
300 }
301 switch (protocol->id()) {
302 case ShellProtocol::kIdStdout:
303 outfile = stdout;
304 break;
305 case ShellProtocol::kIdStderr:
306 outfile = stderr;
307 break;
308 case ShellProtocol::kIdExit:
309 exit_code = protocol->data()[0];
310 continue;
311 default:
312 continue;
313 }
314 length = protocol->data_length();
315 } else {
316 D("read_and_dump(): pre adb_read(fd=%d)", fd);
317 length = adb_read(fd, raw_buffer, sizeof(raw_buffer));
318 D("read_and_dump(): post adb_read(fd=%d): length=%d", fd, length);
319 if (length <= 0) {
320 break;
321 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
323
David Pursell606835a2015-09-08 17:17:02 -0700324 fwrite(buffer_ptr, 1, length, outfile);
325 fflush(outfile);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 }
David Pursell606835a2015-09-08 17:17:02 -0700327
328 return exit_code;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329}
330
Jeff Sharkey960df972014-06-09 17:30:57 -0700331static void read_status_line(int fd, char* buf, size_t count)
332{
333 count--;
334 while (count > 0) {
335 int len = adb_read(fd, buf, count);
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700336 if (len <= 0) {
Jeff Sharkey960df972014-06-09 17:30:57 -0700337 break;
338 }
339
340 buf += len;
341 count -= len;
342 }
343 *buf = '\0';
344}
345
Christopher Tated2f54152011-04-21 12:53:28 -0700346static void copy_to_file(int inFd, int outFd) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700347 const size_t BUFSIZE = 32 * 1024;
348 char* buf = (char*) malloc(BUFSIZE);
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700349 if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
Christopher Tated2f54152011-04-21 12:53:28 -0700350 int len;
Christopher Tatec9cd3b92011-06-01 17:56:23 -0700351 long total = 0;
Spencer Lowb7dfb792015-05-22 16:48:31 -0700352#ifdef _WIN32
353 int old_stdin_mode = -1;
354 int old_stdout_mode = -1;
355#endif
Christopher Tated2f54152011-04-21 12:53:28 -0700356
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700357 D("copy_to_file(%d -> %d)", inFd, outFd);
Yabin Cuid325e862014-11-17 14:48:25 -0800358
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700359 if (inFd == STDIN_FILENO) {
360 stdin_raw_init(STDIN_FILENO);
Spencer Lowb7dfb792015-05-22 16:48:31 -0700361#ifdef _WIN32
362 old_stdin_mode = _setmode(STDIN_FILENO, _O_BINARY);
363 if (old_stdin_mode == -1) {
364 fatal_errno("could not set stdin to binary");
365 }
366#endif
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700367 }
Yabin Cuid325e862014-11-17 14:48:25 -0800368
Spencer Lowb7dfb792015-05-22 16:48:31 -0700369#ifdef _WIN32
370 if (outFd == STDOUT_FILENO) {
371 old_stdout_mode = _setmode(STDOUT_FILENO, _O_BINARY);
372 if (old_stdout_mode == -1) {
373 fatal_errno("could not set stdout to binary");
374 }
375 }
376#endif
377
Elliott Hughesa7090b92015-04-17 17:03:59 -0700378 while (true) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700379 if (inFd == STDIN_FILENO) {
380 len = unix_read(inFd, buf, BUFSIZE);
381 } else {
382 len = adb_read(inFd, buf, BUFSIZE);
383 }
Christopher Tated2f54152011-04-21 12:53:28 -0700384 if (len == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700385 D("copy_to_file() : read 0 bytes; exiting");
Christopher Tated2f54152011-04-21 12:53:28 -0700386 break;
387 }
388 if (len < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700389 D("copy_to_file(): read failed: %s", strerror(errno));
Christopher Tated2f54152011-04-21 12:53:28 -0700390 break;
391 }
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700392 if (outFd == STDOUT_FILENO) {
393 fwrite(buf, 1, len, stdout);
394 fflush(stdout);
395 } else {
396 adb_write(outFd, buf, len);
397 }
Christopher Tatec9cd3b92011-06-01 17:56:23 -0700398 total += len;
Christopher Tated2f54152011-04-21 12:53:28 -0700399 }
Yabin Cuid325e862014-11-17 14:48:25 -0800400
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700401 if (inFd == STDIN_FILENO) {
402 stdin_raw_restore(STDIN_FILENO);
Spencer Lowb7dfb792015-05-22 16:48:31 -0700403#ifdef _WIN32
404 if (_setmode(STDIN_FILENO, old_stdin_mode) == -1) {
405 fatal_errno("could not restore stdin mode");
406 }
407#endif
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700408 }
Yabin Cuid325e862014-11-17 14:48:25 -0800409
Spencer Lowb7dfb792015-05-22 16:48:31 -0700410#ifdef _WIN32
411 if (outFd == STDOUT_FILENO) {
412 if (_setmode(STDOUT_FILENO, old_stdout_mode) == -1) {
413 fatal_errno("could not restore stdout mode");
414 }
415 }
416#endif
417
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700418 D("copy_to_file() finished after %lu bytes", total);
Christopher Tate5b811fa2011-06-10 11:38:37 -0700419 free(buf);
Christopher Tated2f54152011-04-21 12:53:28 -0700420}
421
David Pursell606835a2015-09-08 17:17:02 -0700422namespace {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423
David Pursell606835a2015-09-08 17:17:02 -0700424// Used to pass multiple values to the stdin read thread.
425struct StdinReadArgs {
426 int stdin_fd, write_fd;
David Pursell1ed57f02015-10-06 15:30:03 -0700427 bool raw_stdin;
David Pursell606835a2015-09-08 17:17:02 -0700428 std::unique_ptr<ShellProtocol> protocol;
429};
430
431} // namespace
432
433// Loops to read from stdin and push the data to the given FD.
434// The argument should be a pointer to a StdinReadArgs object. This function
435// will take ownership of the object and delete it when finished.
436static void* stdin_read_thread(void* x) {
437 std::unique_ptr<StdinReadArgs> args(reinterpret_cast<StdinReadArgs*>(x));
438 int state = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700440 adb_thread_setname("stdin reader");
441
Josh Gao8dcdb572015-10-23 15:03:31 -0700442#ifndef __WIN32
443 // Mask SIGTTIN in case we're in a backgrounded process
444 sigset_t sigset;
445 sigemptyset(&sigset);
446 sigaddset(&sigset, SIGTTIN);
447 pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
448#endif
449
David Pursell606835a2015-09-08 17:17:02 -0700450 char raw_buffer[1024];
451 char* buffer_ptr = raw_buffer;
452 size_t buffer_size = sizeof(raw_buffer);
453 if (args->protocol) {
454 buffer_ptr = args->protocol->data();
455 buffer_size = args->protocol->data_capacity();
456 }
457
Elliott Hughesaa245492015-08-03 10:38:08 -0700458 while (true) {
David Pursell606835a2015-09-08 17:17:02 -0700459 // Use unix_read() rather than adb_read() for stdin.
460 D("stdin_read_thread(): pre unix_read(fdi=%d,...)", args->stdin_fd);
461 int r = unix_read(args->stdin_fd, buffer_ptr, buffer_size);
462 D("stdin_read_thread(): post unix_read(fdi=%d,...)", args->stdin_fd);
David Pursell1ed57f02015-10-06 15:30:03 -0700463 if (r <= 0) {
464 // Only devices using the shell protocol know to close subprocess
465 // stdin. For older devices we want to just leave the connection
466 // open, otherwise an unpredictable amount of return data could
467 // be lost due to the FD closing before all data has been received.
468 if (args->protocol) {
469 args->protocol->Write(ShellProtocol::kIdCloseStdin, 0);
470 }
471 break;
472 }
473 // If we made stdin raw, check input for the "~." escape sequence. In
474 // this situation signals like Ctrl+C are sent remotely rather than
475 // interpreted locally so this provides an emergency out if the remote
476 // process starts ignoring the signal. SSH also does this, see the
477 // "escape characters" section on the ssh man page for more info.
478 if (args->raw_stdin) {
479 for (int n = 0; n < r; n++){
480 switch(buffer_ptr[n]) {
481 case '\n':
482 state = 1;
483 break;
484 case '\r':
485 state = 1;
486 break;
487 case '~':
David Pursell08a27092015-10-20 14:19:36 -0700488 if(state == 1) {
489 state++;
490 } else {
491 state = 0;
492 }
David Pursell1ed57f02015-10-06 15:30:03 -0700493 break;
494 case '.':
495 if(state == 2) {
496 stdin_raw_restore(args->stdin_fd);
497 fprintf(stderr,"\n* disconnect *\n");
498 exit(0);
499 }
500 default:
501 state = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 }
503 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 }
David Pursell606835a2015-09-08 17:17:02 -0700505 if (args->protocol) {
506 if (!args->protocol->Write(ShellProtocol::kIdStdin, r)) {
507 break;
508 }
509 } else {
510 if (!WriteFdExactly(args->write_fd, buffer_ptr, r)) {
511 break;
512 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 }
514 }
David Pursell606835a2015-09-08 17:17:02 -0700515
516 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517}
518
David Pursell1ed57f02015-10-06 15:30:03 -0700519// Returns a shell service string with the indicated arguments and command.
520static std::string ShellServiceString(bool use_shell_protocol,
521 const std::string& type_arg,
522 const std::string& command) {
523 std::vector<std::string> args;
524 if (use_shell_protocol) {
525 args.push_back(kShellServiceArgShellProtocol);
526 }
527 if (!type_arg.empty()) {
528 args.push_back(type_arg);
529 }
530
531 // Shell service string can look like: shell[,arg1,arg2,...]:[command].
532 return android::base::StringPrintf("shell%s%s:%s",
533 args.empty() ? "" : ",",
534 android::base::Join(args, ',').c_str(),
535 command.c_str());
536}
537
538// Connects to a shell on the device and read/writes data.
539//
540// Note: currently this function doesn't properly clean up resources; the
541// FD connected to the adb server is never closed and the stdin read thread
542// may never exit.
543//
544// On success returns the remote exit code if |use_shell_protocol| is true,
545// 0 otherwise. On failure returns 1.
546static int RemoteShell(bool use_shell_protocol, const std::string& type_arg,
547 const std::string& command) {
548 std::string service_string = ShellServiceString(use_shell_protocol,
549 type_arg, command);
550
551 // Make local stdin raw if the device allocates a PTY, which happens if:
552 // 1. We are explicitly asking for a PTY shell, or
553 // 2. We don't specify shell type and are starting an interactive session.
554 bool raw_stdin = (type_arg == kShellServiceArgPty ||
555 (type_arg.empty() && command.empty()));
556
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700557 std::string error;
David Pursell4e2fd362015-09-22 10:43:08 -0700558 int fd = adb_connect(service_string, &error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700559 if (fd < 0) {
560 fprintf(stderr,"error: %s\n", error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 return 1;
562 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563
David Pursell606835a2015-09-08 17:17:02 -0700564 StdinReadArgs* args = new StdinReadArgs;
565 if (!args) {
566 LOG(ERROR) << "couldn't allocate StdinReadArgs object";
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700567 return 1;
568 }
David Pursell1ed57f02015-10-06 15:30:03 -0700569 args->stdin_fd = STDIN_FILENO;
David Pursell606835a2015-09-08 17:17:02 -0700570 args->write_fd = fd;
David Pursell1ed57f02015-10-06 15:30:03 -0700571 args->raw_stdin = raw_stdin;
David Pursell606835a2015-09-08 17:17:02 -0700572 if (use_shell_protocol) {
573 args->protocol.reset(new ShellProtocol(args->write_fd));
574 }
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700575
David Pursell1ed57f02015-10-06 15:30:03 -0700576 if (raw_stdin) {
577 stdin_raw_init(STDIN_FILENO);
578 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579
David Pursell606835a2015-09-08 17:17:02 -0700580 int exit_code = 0;
581 if (!adb_thread_create(stdin_read_thread, args)) {
582 PLOG(ERROR) << "error starting stdin read thread";
583 exit_code = 1;
584 delete args;
585 } else {
586 exit_code = read_and_dump(fd, use_shell_protocol);
587 }
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700588
David Pursell1ed57f02015-10-06 15:30:03 -0700589 if (raw_stdin) {
590 stdin_raw_restore(STDIN_FILENO);
591 }
592
593 // TODO(dpursell): properly exit stdin_read_thread and close |fd|.
594
David Pursell606835a2015-09-08 17:17:02 -0700595 return exit_code;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596}
597
598
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700599static std::string format_host_command(const char* command, TransportType type, const char* serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 if (serial) {
Elliott Hughes6452a892015-04-29 12:28:13 -0700601 return android::base::StringPrintf("host-serial:%s:%s", serial, command);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602 }
Elliott Hughes6452a892015-04-29 12:28:13 -0700603
604 const char* prefix = "host";
605 if (type == kTransportUsb) {
606 prefix = "host-usb";
607 } else if (type == kTransportLocal) {
608 prefix = "host-local";
609 }
610 return android::base::StringPrintf("%s:%s", prefix, command);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611}
612
David Pursell4e2fd362015-09-22 10:43:08 -0700613// Returns the FeatureSet for the indicated transport.
614static FeatureSet GetFeatureSet(TransportType transport_type,
David Pursell71c83122015-09-14 15:33:50 -0700615 const char* serial) {
David Pursell4e2fd362015-09-22 10:43:08 -0700616 std::string result, error;
617
618 if (adb_query(format_host_command("features", transport_type, serial),
619 &result, &error)) {
620 return StringToFeatureSet(result);
David Pursell71c83122015-09-14 15:33:50 -0700621 }
David Pursell4e2fd362015-09-22 10:43:08 -0700622 return FeatureSet();
David Pursell71c83122015-09-14 15:33:50 -0700623}
624
Elliott Hughes6452a892015-04-29 12:28:13 -0700625static int adb_download_buffer(const char *service, const char *fn, const void* data, unsigned sz,
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700626 bool show_progress)
Doug Zongker447f0612012-01-09 14:54:53 -0800627{
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700628 std::string error;
Elliott Hughes6452a892015-04-29 12:28:13 -0700629 int fd = adb_connect(android::base::StringPrintf("%s:%d", service, sz), &error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700630 if (fd < 0) {
631 fprintf(stderr,"error: %s\n", error.c_str());
Doug Zongker447f0612012-01-09 14:54:53 -0800632 return -1;
633 }
634
635 int opt = CHUNK_SIZE;
Spencer Lowf055c192015-01-25 14:40:16 -0800636 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
Doug Zongker447f0612012-01-09 14:54:53 -0800637
Elliott Hughes6452a892015-04-29 12:28:13 -0700638 unsigned total = sz;
Dan Albertbac34742015-02-25 17:51:28 -0800639 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
Doug Zongker447f0612012-01-09 14:54:53 -0800640
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700641 if (show_progress) {
Elliott Hughes3e7048c2015-07-27 21:21:39 -0700642 const char* x = strrchr(service, ':');
643 if (x) service = x + 1;
Doug Zongker447f0612012-01-09 14:54:53 -0800644 }
645
Elliott Hughes6452a892015-04-29 12:28:13 -0700646 while (sz > 0) {
Doug Zongker447f0612012-01-09 14:54:53 -0800647 unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700648 if (!WriteFdExactly(fd, ptr, xfer)) {
649 std::string error;
650 adb_status(fd, &error);
651 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
Doug Zongker447f0612012-01-09 14:54:53 -0800652 return -1;
653 }
654 sz -= xfer;
655 ptr += xfer;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700656 if (show_progress) {
Magnus Eriksson86ae6d52013-03-05 07:37:32 +0100657 printf("sending: '%s' %4d%% \r", fn, (int)(100LL - ((100LL * sz) / (total))));
Doug Zongker447f0612012-01-09 14:54:53 -0800658 fflush(stdout);
659 }
660 }
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700661 if (show_progress) {
Doug Zongker447f0612012-01-09 14:54:53 -0800662 printf("\n");
663 }
664
Elliott Hughese67f1f82015-04-30 17:32:03 -0700665 if (!adb_status(fd, &error)) {
666 fprintf(stderr,"* error response '%s' *\n", error.c_str());
Doug Zongker447f0612012-01-09 14:54:53 -0800667 return -1;
668 }
669
670 adb_close(fd);
671 return 0;
672}
673
Doug Zongker71fe5842014-06-26 15:35:36 -0700674#define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE)
675
676/*
677 * The sideload-host protocol serves the data in a file (given on the
678 * command line) to the client, using a simple protocol:
679 *
680 * - The connect message includes the total number of bytes in the
681 * file and a block size chosen by us.
682 *
683 * - The other side sends the desired block number as eight decimal
684 * digits (eg "00000023" for block 23). Blocks are numbered from
685 * zero.
686 *
687 * - We send back the data of the requested block. The last block is
688 * likely to be partial; when the last block is requested we only
689 * send the part of the block that exists, it's not padded up to the
690 * block size.
691 *
692 * - When the other side sends "DONEDONE" instead of a block number,
693 * we hang up.
694 */
Elliott Hughes58305772015-04-17 13:57:15 -0700695static int adb_sideload_host(const char* fn) {
Doug Zongker71fe5842014-06-26 15:35:36 -0700696 unsigned sz;
697 size_t xfer = 0;
698 int status;
Dan Albertbac34742015-02-25 17:51:28 -0800699 int last_percent = -1;
700 int opt = SIDELOAD_HOST_BLOCK_SIZE;
Doug Zongker71fe5842014-06-26 15:35:36 -0700701
702 printf("loading: '%s'", fn);
703 fflush(stdout);
Dan Albertbac34742015-02-25 17:51:28 -0800704 uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz));
Doug Zongker71fe5842014-06-26 15:35:36 -0700705 if (data == 0) {
706 printf("\n");
707 fprintf(stderr, "* cannot read '%s' *\n", fn);
708 return -1;
709 }
710
Elliott Hughes6452a892015-04-29 12:28:13 -0700711 std::string service =
712 android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700713 std::string error;
Elliott Hughes6452a892015-04-29 12:28:13 -0700714 int fd = adb_connect(service, &error);
Doug Zongker71fe5842014-06-26 15:35:36 -0700715 if (fd < 0) {
716 // Try falling back to the older sideload method. Maybe this
717 // is an older device that doesn't support sideload-host.
718 printf("\n");
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700719 status = adb_download_buffer("sideload", fn, data, sz, true);
Doug Zongker71fe5842014-06-26 15:35:36 -0700720 goto done;
721 }
722
Spencer Lowf055c192015-01-25 14:40:16 -0800723 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
Doug Zongker71fe5842014-06-26 15:35:36 -0700724
Elliott Hughesa7090b92015-04-17 17:03:59 -0700725 while (true) {
Elliott Hughes6452a892015-04-29 12:28:13 -0700726 char buf[9];
Dan Albertcc731cc2015-02-24 21:26:58 -0800727 if (!ReadFdExactly(fd, buf, 8)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700728 fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
Doug Zongker71fe5842014-06-26 15:35:36 -0700729 status = -1;
730 goto done;
731 }
Elliott Hughes6452a892015-04-29 12:28:13 -0700732 buf[8] = '\0';
Doug Zongker71fe5842014-06-26 15:35:36 -0700733
Elliott Hughes6452a892015-04-29 12:28:13 -0700734 if (strcmp("DONEDONE", buf) == 0) {
Doug Zongker71fe5842014-06-26 15:35:36 -0700735 status = 0;
736 break;
737 }
738
Doug Zongker71fe5842014-06-26 15:35:36 -0700739 int block = strtol(buf, NULL, 10);
740
741 size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
742 if (offset >= sz) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700743 fprintf(stderr, "* attempt to read block %d past end\n", block);
Doug Zongker71fe5842014-06-26 15:35:36 -0700744 status = -1;
745 goto done;
746 }
747 uint8_t* start = data + offset;
748 size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE;
749 size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
750 if (offset_end > sz) {
751 to_write = sz - offset;
752 }
753
Dan Albertcc731cc2015-02-24 21:26:58 -0800754 if(!WriteFdExactly(fd, start, to_write)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700755 adb_status(fd, &error);
756 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
Doug Zongker71fe5842014-06-26 15:35:36 -0700757 status = -1;
758 goto done;
759 }
760 xfer += to_write;
761
762 // For normal OTA packages, we expect to transfer every byte
763 // twice, plus a bit of overhead (one read during
764 // verification, one read of each byte for installation, plus
765 // extra access to things like the zip central directory).
766 // This estimate of the completion becomes 100% when we've
767 // transferred ~2.13 (=100/47) times the package size.
768 int percent = (int)(xfer * 47LL / (sz ? sz : 1));
769 if (percent != last_percent) {
770 printf("\rserving: '%s' (~%d%%) ", fn, percent);
771 fflush(stdout);
772 last_percent = percent;
773 }
774 }
775
Colin Cross6d6a8982014-07-07 14:12:41 -0700776 printf("\rTotal xfer: %.2fx%*s\n", (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
Doug Zongker71fe5842014-06-26 15:35:36 -0700777
778 done:
779 if (fd >= 0) adb_close(fd);
780 free(data);
781 return status;
782}
783
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784/**
785 * Run ppp in "notty" mode against a resource listed as the first parameter
786 * eg:
787 *
788 * ppp dev:/dev/omap_csmi_tty0 <ppp options>
789 *
790 */
Elliott Hughes58305772015-04-17 13:57:15 -0700791static int ppp(int argc, const char** argv) {
Yabin Cuie77b6a02014-11-11 09:24:11 -0800792#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793 fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
794 return -1;
795#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796 if (argc < 2) {
797 fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
798 argv[0]);
799
800 return 1;
801 }
802
Dan Albertbac34742015-02-25 17:51:28 -0800803 const char* adb_service_name = argv[1];
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700804 std::string error;
805 int fd = adb_connect(adb_service_name, &error);
806 if (fd < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700808 adb_service_name, error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 return 1;
810 }
811
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700812 pid_t pid = fork();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800813
814 if (pid < 0) {
815 perror("from fork()");
816 return 1;
817 } else if (pid == 0) {
818 int err;
819 int i;
820 const char **ppp_args;
821
822 // copy args
823 ppp_args = (const char **) alloca(sizeof(char *) * argc + 1);
824 ppp_args[0] = "pppd";
825 for (i = 2 ; i < argc ; i++) {
826 //argv[2] and beyond become ppp_args[1] and beyond
827 ppp_args[i - 1] = argv[i];
828 }
829 ppp_args[i-1] = NULL;
830
831 // child side
832
833 dup2(fd, STDIN_FILENO);
834 dup2(fd, STDOUT_FILENO);
835 adb_close(STDERR_FILENO);
836 adb_close(fd);
837
838 err = execvp("pppd", (char * const *)ppp_args);
839
840 if (err < 0) {
841 perror("execing pppd");
842 }
843 exit(-1);
844 } else {
845 // parent side
846
847 adb_close(fd);
848 return 0;
849 }
Yabin Cuie77b6a02014-11-11 09:24:11 -0800850#endif /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800851}
852
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700853static bool wait_for_device(const char* service, TransportType t, const char* serial) {
Elliott Hughes2b101112015-05-04 19:29:32 -0700854 // Was the caller vague about what they'd like us to wait for?
855 // If so, check they weren't more specific in their choice of transport type.
856 if (strcmp(service, "wait-for-device") == 0) {
857 if (t == kTransportUsb) {
858 service = "wait-for-usb";
859 } else if (t == kTransportLocal) {
860 service = "wait-for-local";
861 } else {
862 service = "wait-for-any";
863 }
864 }
865
866 std::string cmd = format_host_command(service, t, serial);
Elliott Hughes424af022015-05-29 17:55:19 -0700867 return adb_command(cmd);
Elliott Hughes2b101112015-05-04 19:29:32 -0700868}
869
David Pursell70ef7b42015-09-30 13:35:42 -0700870// Connects to the device "shell" service with |command| and prints the
871// resulting output.
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700872static int send_shell_command(TransportType transport_type, const char* serial,
David Pursell70ef7b42015-09-30 13:35:42 -0700873 const std::string& command,
874 bool disable_shell_protocol) {
875 // Only use shell protocol if it's supported and the caller doesn't want
876 // to explicitly disable it.
877 bool use_shell_protocol = false;
878 if (!disable_shell_protocol) {
879 FeatureSet features = GetFeatureSet(transport_type, serial);
880 use_shell_protocol = CanUseFeature(features, kFeatureShell2);
881 }
882
883 std::string service_string = ShellServiceString(use_shell_protocol, "",
884 command);
885
Elliott Hughes2baae3a2015-04-17 10:59:34 -0700886 int fd;
887 while (true) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700888 std::string error;
David Pursell70ef7b42015-09-30 13:35:42 -0700889 fd = adb_connect(service_string, &error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700890 if (fd >= 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800891 break;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700892 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800893 fprintf(stderr,"- waiting for device -\n");
894 adb_sleep_ms(1000);
Elliott Hughes2b101112015-05-04 19:29:32 -0700895 wait_for_device("wait-for-device", transport_type, serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 }
897
David Pursell70ef7b42015-09-30 13:35:42 -0700898 int exit_code = read_and_dump(fd, use_shell_protocol);
David Pursell71c83122015-09-14 15:33:50 -0700899
900 if (adb_close(fd) < 0) {
901 PLOG(ERROR) << "failure closing FD " << fd;
Elliott Hughes2baae3a2015-04-17 10:59:34 -0700902 }
David Pursell71c83122015-09-14 15:33:50 -0700903
904 return exit_code;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800905}
906
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700907static int logcat(TransportType transport, const char* serial, int argc, const char** argv) {
Elliott Hughes2baae3a2015-04-17 10:59:34 -0700908 char* log_tags = getenv("ANDROID_LOG_TAGS");
909 std::string quoted = escape_arg(log_tags == nullptr ? "" : log_tags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910
David Pursell70ef7b42015-09-30 13:35:42 -0700911 std::string cmd = "export ANDROID_LOG_TAGS=\"" + quoted + "\"; exec logcat";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800912
Jeff Sharkeyfd546e82014-06-10 11:31:24 -0700913 if (!strcmp(argv[0], "longcat")) {
Elliott Hughes2baae3a2015-04-17 10:59:34 -0700914 cmd += " -v long";
Christopher Tatedb0a8802011-11-30 13:00:33 -0800915 }
916
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700917 --argc;
918 ++argv;
Elliott Hughes2baae3a2015-04-17 10:59:34 -0700919 while (argc-- > 0) {
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700920 cmd += " " + escape_arg(*argv++);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800921 }
922
David Pursell70ef7b42015-09-30 13:35:42 -0700923 // No need for shell protocol with logcat, always disable for simplicity.
924 return send_shell_command(transport, serial, cmd, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925}
926
Dan Albertbac34742015-02-25 17:51:28 -0800927static int backup(int argc, const char** argv) {
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700928 const char* filename = "./backup.ab";
Christopher Tated2f54152011-04-21 12:53:28 -0700929
Christopher Tatec9cd3b92011-06-01 17:56:23 -0700930 /* find, extract, and use any -f argument */
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700931 for (int i = 1; i < argc; i++) {
Christopher Tatec9cd3b92011-06-01 17:56:23 -0700932 if (!strcmp("-f", argv[i])) {
933 if (i == argc-1) {
934 fprintf(stderr, "adb: -f passed with no filename\n");
935 return usage();
936 }
937 filename = argv[i+1];
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700938 for (int j = i+2; j <= argc; ) {
Christopher Tatec9cd3b92011-06-01 17:56:23 -0700939 argv[i++] = argv[j++];
940 }
941 argc -= 2;
942 argv[argc] = NULL;
943 }
Christopher Tated2f54152011-04-21 12:53:28 -0700944 }
945
Christopher Tatebb86bc52011-08-22 17:12:08 -0700946 /* bare "adb backup" or "adb backup -f filename" are not valid invocations */
947 if (argc < 2) return usage();
948
Christopher Tateb1dfffe2011-12-08 19:04:34 -0800949 adb_unlink(filename);
Mark Salyzyn60299df2014-04-30 09:10:31 -0700950 mkdirs(filename);
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700951 int outFd = adb_creat(filename, 0640);
Christopher Tated2f54152011-04-21 12:53:28 -0700952 if (outFd < 0) {
953 fprintf(stderr, "adb: unable to open file %s\n", filename);
954 return -1;
955 }
956
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700957 std::string cmd = "backup:";
958 --argc;
959 ++argv;
960 while (argc-- > 0) {
961 cmd += " " + escape_arg(*argv++);
Christopher Tated2f54152011-04-21 12:53:28 -0700962 }
963
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700964 D("backup. filename=%s cmd=%s", filename, cmd.c_str());
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700965 std::string error;
Elliott Hughes6452a892015-04-29 12:28:13 -0700966 int fd = adb_connect(cmd, &error);
Christopher Tated2f54152011-04-21 12:53:28 -0700967 if (fd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700968 fprintf(stderr, "adb: unable to connect for backup: %s\n", error.c_str());
Christopher Tated2f54152011-04-21 12:53:28 -0700969 adb_close(outFd);
970 return -1;
971 }
972
Christopher Tatebffa4ca2012-01-06 15:43:03 -0800973 printf("Now unlock your device and confirm the backup operation.\n");
Christopher Tated2f54152011-04-21 12:53:28 -0700974 copy_to_file(fd, outFd);
975
976 adb_close(fd);
977 adb_close(outFd);
978 return 0;
979}
980
Dan Albertbac34742015-02-25 17:51:28 -0800981static int restore(int argc, const char** argv) {
Christopher Tate702967a2011-05-17 15:52:54 -0700982 if (argc != 2) return usage();
983
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700984 const char* filename = argv[1];
985 int tarFd = adb_open(filename, O_RDONLY);
Christopher Tate702967a2011-05-17 15:52:54 -0700986 if (tarFd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700987 fprintf(stderr, "adb: unable to open file %s: %s\n", filename, strerror(errno));
Christopher Tate702967a2011-05-17 15:52:54 -0700988 return -1;
989 }
990
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700991 std::string error;
992 int fd = adb_connect("restore:", &error);
Christopher Tate702967a2011-05-17 15:52:54 -0700993 if (fd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700994 fprintf(stderr, "adb: unable to connect for restore: %s\n", error.c_str());
Christopher Tate702967a2011-05-17 15:52:54 -0700995 adb_close(tarFd);
996 return -1;
997 }
998
Christopher Tatebffa4ca2012-01-06 15:43:03 -0800999 printf("Now unlock your device and confirm the restore operation.\n");
Christopher Tate702967a2011-05-17 15:52:54 -07001000 copy_to_file(tarFd, fd);
1001
1002 adb_close(fd);
1003 adb_close(tarFd);
1004 return 0;
1005}
1006
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001007/* <hint> may be:
1008 * - A simple product name
1009 * e.g., "sooner"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001010 * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir
1011 * e.g., "out/target/product/sooner"
1012 * - An absolute path to the PRODUCT_OUT dir
1013 * e.g., "/src/device/out/target/product/sooner"
1014 *
1015 * Given <hint>, try to construct an absolute path to the
1016 * ANDROID_PRODUCT_OUT dir.
1017 */
Elliott Hughes5c742702015-07-30 17:42:01 -07001018static std::string find_product_out_path(const std::string& hint) {
1019 if (hint.empty()) {
Elliott Hughes58305772015-04-17 13:57:15 -07001020 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021 }
1022
Elliott Hughes58305772015-04-17 13:57:15 -07001023 // If it's already absolute, don't bother doing any work.
Elliott Hughes5c742702015-07-30 17:42:01 -07001024 if (adb_is_absolute_host_path(hint.c_str())) {
Elliott Hughes58305772015-04-17 13:57:15 -07001025 return hint;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001026 }
1027
Elliott Hughes58305772015-04-17 13:57:15 -07001028 // If there are any slashes in it, assume it's a relative path;
1029 // make it absolute.
Elliott Hughes5c742702015-07-30 17:42:01 -07001030 if (hint.find_first_of(OS_PATH_SEPARATORS) != std::string::npos) {
Elliott Hughesa7090b92015-04-17 17:03:59 -07001031 std::string cwd;
1032 if (!getcwd(&cwd)) {
1033 fprintf(stderr, "adb: getcwd failed: %s\n", strerror(errno));
Elliott Hughes58305772015-04-17 13:57:15 -07001034 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 }
Elliott Hughes5c742702015-07-30 17:42:01 -07001036 return android::base::StringPrintf("%s%c%s", cwd.c_str(), OS_PATH_SEPARATOR, hint.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001037 }
1038
Elliott Hughes58305772015-04-17 13:57:15 -07001039 // It's a string without any slashes. Try to do something with it.
1040 //
1041 // Try to find the root of the build tree, and build a PRODUCT_OUT
1042 // path from there.
Elliott Hughesa7090b92015-04-17 17:03:59 -07001043 char* top = getenv("ANDROID_BUILD_TOP");
Elliott Hughes58305772015-04-17 13:57:15 -07001044 if (top == nullptr) {
Elliott Hughesa7090b92015-04-17 17:03:59 -07001045 fprintf(stderr, "adb: ANDROID_BUILD_TOP not set!\n");
Elliott Hughes58305772015-04-17 13:57:15 -07001046 return "";
1047 }
Elliott Hughesa7090b92015-04-17 17:03:59 -07001048
1049 std::string path = top;
Elliott Hughes58305772015-04-17 13:57:15 -07001050 path += OS_PATH_SEPARATOR_STR;
1051 path += "out";
1052 path += OS_PATH_SEPARATOR_STR;
1053 path += "target";
1054 path += OS_PATH_SEPARATOR_STR;
1055 path += "product";
1056 path += OS_PATH_SEPARATOR_STR;
1057 path += hint;
1058 if (!directory_exists(path)) {
1059 fprintf(stderr, "adb: Couldn't find a product dir based on -p %s; "
Elliott Hughes5c742702015-07-30 17:42:01 -07001060 "\"%s\" doesn't exist\n", hint.c_str(), path.c_str());
Elliott Hughesa7090b92015-04-17 17:03:59 -07001061 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001062 }
Elliott Hughes58305772015-04-17 13:57:15 -07001063 return path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064}
1065
Elliott Hughesb708d162015-10-27 16:03:15 -07001066static void parse_push_pull_args(const char **arg, int narg,
1067 char const **path1, char const **path2,
Dan Albertbac34742015-02-25 17:51:28 -08001068 int *copy_attrs) {
Lajos Molnarde8ff4a2013-04-19 12:41:09 -07001069 *copy_attrs = 0;
Mark Lindner76f2a932014-03-11 17:55:59 -07001070
Lajos Molnarde8ff4a2013-04-19 12:41:09 -07001071 while (narg > 0) {
1072 if (!strcmp(*arg, "-p")) {
Elliott Hughesb708d162015-10-27 16:03:15 -07001073 // Silently ignore for backwards compatibility.
Lajos Molnarde8ff4a2013-04-19 12:41:09 -07001074 } else if (!strcmp(*arg, "-a")) {
1075 *copy_attrs = 1;
1076 } else {
1077 break;
1078 }
Mark Lindner76f2a932014-03-11 17:55:59 -07001079 ++arg;
1080 --narg;
1081 }
1082
1083 if (narg > 0) {
1084 *path1 = *arg;
1085 ++arg;
1086 --narg;
1087 }
1088
1089 if (narg > 0) {
1090 *path2 = *arg;
1091 }
1092}
1093
Elliott Hughes6452a892015-04-29 12:28:13 -07001094static int adb_connect_command(const std::string& command) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001095 std::string error;
1096 int fd = adb_connect(command, &error);
Elliott Hughes5677c232015-05-07 23:37:40 -07001097 if (fd < 0) {
1098 fprintf(stderr, "error: %s\n", error.c_str());
1099 return 1;
Tao Bao175b7bb2015-03-29 11:22:34 -07001100 }
Elliott Hughes5677c232015-05-07 23:37:40 -07001101 read_and_dump(fd);
1102 adb_close(fd);
1103 return 0;
Tao Bao175b7bb2015-03-29 11:22:34 -07001104}
1105
Elliott Hughes6452a892015-04-29 12:28:13 -07001106static int adb_query_command(const std::string& command) {
1107 std::string result;
1108 std::string error;
1109 if (!adb_query(command, &result, &error)) {
1110 fprintf(stderr, "error: %s\n", error.c_str());
1111 return 1;
1112 }
1113 printf("%s\n", result.c_str());
1114 return 0;
1115}
1116
Spencer Lowa13df302015-09-07 16:20:13 -07001117// Disallow stdin, stdout, and stderr.
1118static bool _is_valid_ack_reply_fd(const int ack_reply_fd) {
1119#ifdef _WIN32
1120 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
1121 return (GetStdHandle(STD_INPUT_HANDLE) != ack_reply_handle) &&
1122 (GetStdHandle(STD_OUTPUT_HANDLE) != ack_reply_handle) &&
1123 (GetStdHandle(STD_ERROR_HANDLE) != ack_reply_handle);
1124#else
1125 return ack_reply_fd > 2;
1126#endif
1127}
1128
Elliott Hughesab52c182015-05-01 17:04:38 -07001129int adb_commandline(int argc, const char **argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001130 int no_daemon = 0;
1131 int is_daemon = 0;
David 'Digit' Turner305b4b02011-01-31 14:23:56 +01001132 int is_server = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001133 int r;
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001134 TransportType transport_type = kTransportAny;
Siva Velusamy9f2d1a92015-08-07 10:10:29 -07001135 int ack_reply_fd = -1;
Siva Velusamy9f2d1a92015-08-07 10:10:29 -07001136
Elliott Hughes58305772015-04-17 13:57:15 -07001137 // If defined, this should be an absolute path to
1138 // the directory containing all of the various system images
1139 // for a particular product. If not defined, and the adb
1140 // command requires this information, then the user must
1141 // specify the path using "-p".
1142 char* ANDROID_PRODUCT_OUT = getenv("ANDROID_PRODUCT_OUT");
1143 if (ANDROID_PRODUCT_OUT != nullptr) {
1144 gProductOutPath = ANDROID_PRODUCT_OUT;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001145 }
1146 // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
1147
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001148 /* Validate and assign the server port */
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001149 const char* server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001150 int server_port = DEFAULT_ADB_PORT;
1151 if (server_port_str && strlen(server_port_str) > 0) {
Siva Velusamy9f2d1a92015-08-07 10:10:29 -07001152 server_port = strtol(server_port_str, nullptr, 0);
Matt Gumbeld7b33082012-11-14 10:16:17 -08001153 if (server_port <= 0 || server_port > 65535) {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001154 fprintf(stderr,
Spencer Lowf18fc082015-08-11 17:05:02 -07001155 "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number less than 65536. Got \"%s\"\n",
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001156 server_port_str);
1157 return usage();
1158 }
1159 }
1160
Elliott Hughes8d28e192015-10-07 14:55:10 -07001161 // We need to check for -d and -e before we look at $ANDROID_SERIAL.
1162 const char* serial = nullptr;
1163
Riley Andrews98f58e82014-12-05 17:37:24 -08001164 while (argc > 0) {
1165 if (!strcmp(argv[0],"server")) {
David 'Digit' Turner305b4b02011-01-31 14:23:56 +01001166 is_server = 1;
Riley Andrews98f58e82014-12-05 17:37:24 -08001167 } else if (!strcmp(argv[0],"nodaemon")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001168 no_daemon = 1;
1169 } else if (!strcmp(argv[0], "fork-server")) {
1170 /* this is a special flag used only when the ADB client launches the ADB Server */
1171 is_daemon = 1;
Siva Velusamy9f2d1a92015-08-07 10:10:29 -07001172 } else if (!strcmp(argv[0], "--reply-fd")) {
1173 if (argc < 2) return usage();
1174 const char* reply_fd_str = argv[1];
1175 argc--;
1176 argv++;
1177 ack_reply_fd = strtol(reply_fd_str, nullptr, 10);
Spencer Lowa13df302015-09-07 16:20:13 -07001178 if (!_is_valid_ack_reply_fd(ack_reply_fd)) {
Siva Velusamy9f2d1a92015-08-07 10:10:29 -07001179 fprintf(stderr, "adb: invalid reply fd \"%s\"\n", reply_fd_str);
1180 return usage();
1181 }
Riley Andrews98f58e82014-12-05 17:37:24 -08001182 } else if (!strncmp(argv[0], "-p", 2)) {
Elliott Hughes048b27c2015-07-31 13:18:22 -07001183 const char* product = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 if (argv[0][2] == '\0') {
1185 if (argc < 2) return usage();
1186 product = argv[1];
1187 argc--;
1188 argv++;
1189 } else {
Vairavan Srinivasan81273232012-08-04 16:40:50 -07001190 product = argv[0] + 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191 }
Elliott Hughes048b27c2015-07-31 13:18:22 -07001192 gProductOutPath = find_product_out_path(product);
Elliott Hughes58305772015-04-17 13:57:15 -07001193 if (gProductOutPath.empty()) {
1194 fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195 return usage();
1196 }
1197 } else if (argv[0][0]=='-' && argv[0][1]=='s') {
1198 if (isdigit(argv[0][2])) {
1199 serial = argv[0] + 2;
1200 } else {
Riley Andrews98f58e82014-12-05 17:37:24 -08001201 if (argc < 2 || argv[0][2] != '\0') return usage();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 serial = argv[1];
1203 argc--;
1204 argv++;
1205 }
1206 } else if (!strcmp(argv[0],"-d")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001207 transport_type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001208 } else if (!strcmp(argv[0],"-e")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001209 transport_type = kTransportLocal;
Matt Gumbeld7b33082012-11-14 10:16:17 -08001210 } else if (!strcmp(argv[0],"-a")) {
1211 gListenAll = 1;
Riley Andrews98f58e82014-12-05 17:37:24 -08001212 } else if (!strncmp(argv[0], "-H", 2)) {
Matt Gumbeld7b33082012-11-14 10:16:17 -08001213 const char *hostname = NULL;
1214 if (argv[0][2] == '\0') {
1215 if (argc < 2) return usage();
1216 hostname = argv[1];
1217 argc--;
1218 argv++;
1219 } else {
1220 hostname = argv[0] + 2;
1221 }
1222 adb_set_tcp_name(hostname);
1223
Riley Andrews98f58e82014-12-05 17:37:24 -08001224 } else if (!strncmp(argv[0], "-P", 2)) {
Matt Gumbeld7b33082012-11-14 10:16:17 -08001225 if (argv[0][2] == '\0') {
1226 if (argc < 2) return usage();
1227 server_port_str = argv[1];
1228 argc--;
1229 argv++;
1230 } else {
1231 server_port_str = argv[0] + 2;
1232 }
1233 if (strlen(server_port_str) > 0) {
1234 server_port = (int) strtol(server_port_str, NULL, 0);
1235 if (server_port <= 0 || server_port > 65535) {
1236 fprintf(stderr,
1237 "adb: port number must be a positive number less than 65536. Got \"%s\"\n",
1238 server_port_str);
1239 return usage();
1240 }
1241 } else {
1242 fprintf(stderr,
1243 "adb: port number must be a positive number less than 65536. Got empty string.\n");
1244 return usage();
1245 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001246 } else {
1247 /* out of recognized modifiers and flags */
1248 break;
1249 }
1250 argc--;
1251 argv++;
1252 }
1253
Elliott Hughes8d28e192015-10-07 14:55:10 -07001254 // If none of -d, -e, or -s were specified, try $ANDROID_SERIAL.
1255 if (transport_type == kTransportAny && serial == nullptr) {
1256 serial = getenv("ANDROID_SERIAL");
1257 }
1258
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001259 adb_set_transport(transport_type, serial);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001260 adb_set_tcp_specifics(server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001261
David 'Digit' Turner305b4b02011-01-31 14:23:56 +01001262 if (is_server) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001263 if (no_daemon || is_daemon) {
Spencer Low5c398d22015-08-08 15:07:07 -07001264 if (is_daemon && (ack_reply_fd == -1)) {
Siva Velusamy9f2d1a92015-08-07 10:10:29 -07001265 fprintf(stderr, "reply fd for adb server to client communication not specified.\n");
1266 return usage();
1267 }
1268 r = adb_main(is_daemon, server_port, ack_reply_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001270 r = launch_server(server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001271 }
Riley Andrews98f58e82014-12-05 17:37:24 -08001272 if (r) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273 fprintf(stderr,"* could not start server *\n");
1274 }
1275 return r;
1276 }
1277
Riley Andrews98f58e82014-12-05 17:37:24 -08001278 if (argc == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001279 return usage();
1280 }
1281
Riley Andrewsc8514c82014-12-05 17:32:46 -08001282 /* handle wait-for-* prefix */
1283 if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
Dan Albertbac34742015-02-25 17:51:28 -08001284 const char* service = argv[0];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001285
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001286 if (!wait_for_device(service, transport_type, serial)) {
Riley Andrewsc8514c82014-12-05 17:32:46 -08001287 return 1;
1288 }
1289
Elliott Hughes2b101112015-05-04 19:29:32 -07001290 // Allow a command to be run after wait-for-device,
1291 // e.g. 'adb wait-for-device shell'.
Riley Andrewsc8514c82014-12-05 17:32:46 -08001292 if (argc == 1) {
1293 return 0;
1294 }
1295
1296 /* Fall through */
1297 argc--;
1298 argv++;
1299 }
1300
1301 /* adb_connect() commands */
Riley Andrews98f58e82014-12-05 17:37:24 -08001302 if (!strcmp(argv[0], "devices")) {
Dan Albertbac34742015-02-25 17:51:28 -08001303 const char *listopt;
Elliott Hughes6452a892015-04-29 12:28:13 -07001304 if (argc < 2) {
Scott Andersone109d262012-04-20 11:21:14 -07001305 listopt = "";
Elliott Hughes6452a892015-04-29 12:28:13 -07001306 } else if (argc == 2 && !strcmp(argv[1], "-l")) {
Scott Andersone109d262012-04-20 11:21:14 -07001307 listopt = argv[1];
Elliott Hughes6452a892015-04-29 12:28:13 -07001308 } else {
Scott Andersone109d262012-04-20 11:21:14 -07001309 fprintf(stderr, "Usage: adb devices [-l]\n");
1310 return 1;
1311 }
Elliott Hughes6452a892015-04-29 12:28:13 -07001312
1313 std::string query = android::base::StringPrintf("host:%s%s", argv[0], listopt);
1314 printf("List of devices attached\n");
1315 return adb_query_command(query);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001316 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001317 else if (!strcmp(argv[0], "connect")) {
Mike Lockwoodff196702009-08-24 15:58:40 -07001318 if (argc != 2) {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001319 fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
Mike Lockwoodff196702009-08-24 15:58:40 -07001320 return 1;
1321 }
Elliott Hughes6452a892015-04-29 12:28:13 -07001322
1323 std::string query = android::base::StringPrintf("host:connect:%s", argv[1]);
1324 return adb_query_command(query);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001325 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001326 else if (!strcmp(argv[0], "disconnect")) {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001327 if (argc > 2) {
1328 fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
1329 return 1;
1330 }
Elliott Hughes6452a892015-04-29 12:28:13 -07001331
1332 std::string query = android::base::StringPrintf("host:disconnect:%s",
1333 (argc == 2) ? argv[1] : "");
1334 return adb_query_command(query);
Mike Lockwoodff196702009-08-24 15:58:40 -07001335 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001336 else if (!strcmp(argv[0], "emu")) {
Spencer Low142ec752015-05-06 16:13:42 -07001337 return adb_send_emulator_command(argc, argv, serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001338 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001339 else if (!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
Daniel Sandlerff91ab82010-08-19 01:10:18 -04001340 char h = (argv[0][0] == 'h');
1341
David Pursell4e2fd362015-09-22 10:43:08 -07001342 FeatureSet features = GetFeatureSet(transport_type, serial);
1343
David Pursell70ef7b42015-09-30 13:35:42 -07001344 bool use_shell_protocol = CanUseFeature(features, kFeatureShell2);
David Pursell4e2fd362015-09-22 10:43:08 -07001345 if (!use_shell_protocol) {
1346 D("shell protocol not supported, using raw data transfer");
1347 } else {
1348 D("using shell protocol");
1349 }
1350
1351 // Parse shell-specific command-line options.
1352 // argv[0] is always "shell".
1353 --argc;
1354 ++argv;
David Pursell08a27092015-10-20 14:19:36 -07001355 int t_arg_count = 0;
David Pursell4e2fd362015-09-22 10:43:08 -07001356 while (argc) {
1357 if (!strcmp(argv[0], "-T") || !strcmp(argv[0], "-t")) {
David Pursell70ef7b42015-09-30 13:35:42 -07001358 if (!CanUseFeature(features, kFeatureShell2)) {
David Pursell4e2fd362015-09-22 10:43:08 -07001359 fprintf(stderr, "error: target doesn't support PTY args -Tt\n");
1360 return 1;
1361 }
David Pursell08a27092015-10-20 14:19:36 -07001362 // Like ssh, -t arguments are cumulative so that multiple -t's
1363 // are needed to force a PTY.
1364 if (argv[0][1] == 't') {
1365 ++t_arg_count;
1366 } else {
1367 t_arg_count = -1;
1368 }
David Pursell70ef7b42015-09-30 13:35:42 -07001369 --argc;
1370 ++argv;
1371 } else if (!strcmp(argv[0], "-x")) {
1372 use_shell_protocol = false;
David Pursell4e2fd362015-09-22 10:43:08 -07001373 --argc;
1374 ++argv;
1375 } else {
1376 break;
1377 }
1378 }
David Pursell4e2fd362015-09-22 10:43:08 -07001379
David Pursell08a27092015-10-20 14:19:36 -07001380 std::string shell_type_arg;
1381 if (CanUseFeature(features, kFeatureShell2)) {
1382 if (t_arg_count < 0) {
1383 shell_type_arg = kShellServiceArgRaw;
1384 } else if (t_arg_count == 0) {
1385 // If stdin isn't a TTY, default to a raw shell; this lets
1386 // things like `adb shell < my_script.sh` work as expected.
1387 // Otherwise leave |shell_type_arg| blank which uses PTY for
1388 // interactive shells and raw for non-interactive.
1389 if (!isatty(STDIN_FILENO)) {
1390 shell_type_arg = kShellServiceArgRaw;
1391 }
1392 } else if (t_arg_count == 1) {
1393 // A single -t arg isn't enough to override implicit -T.
1394 if (!isatty(STDIN_FILENO)) {
1395 fprintf(stderr,
1396 "Remote PTY will not be allocated because stdin is not a terminal.\n"
1397 "Use multiple -t options to force remote PTY allocation.\n");
1398 shell_type_arg = kShellServiceArgRaw;
1399 } else {
1400 shell_type_arg = kShellServiceArgPty;
1401 }
1402 } else {
1403 shell_type_arg = kShellServiceArgPty;
1404 }
1405 }
1406
David Pursell1ed57f02015-10-06 15:30:03 -07001407 std::string command;
1408 if (argc) {
1409 // We don't escape here, just like ssh(1). http://b/20564385.
1410 command = android::base::Join(
1411 std::vector<const char*>(argv, argv + argc), ' ');
1412 }
1413
Daniel Sandlerff91ab82010-08-19 01:10:18 -04001414 if (h) {
1415 printf("\x1b[41;33m");
1416 fflush(stdout);
1417 }
1418
David Pursell1ed57f02015-10-06 15:30:03 -07001419 r = RemoteShell(use_shell_protocol, shell_type_arg, command);
1420
1421 if (h) {
1422 printf("\x1b[0m");
1423 fflush(stdout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001424 }
1425
David Pursell1ed57f02015-10-06 15:30:03 -07001426 return r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001427 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001428 else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -07001429 int exec_in = !strcmp(argv[0], "exec-in");
Jeff Sharkey5d9d4342014-05-26 18:30:43 -07001430
Elliott Hughes2baae3a2015-04-17 10:59:34 -07001431 std::string cmd = "exec:";
1432 cmd += argv[1];
Jeff Sharkey5d9d4342014-05-26 18:30:43 -07001433 argc -= 2;
1434 argv += 2;
1435 while (argc-- > 0) {
Elliott Hughes6c34bba2015-04-17 20:11:08 -07001436 cmd += " " + escape_arg(*argv++);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -07001437 }
1438
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001439 std::string error;
Elliott Hughes6452a892015-04-29 12:28:13 -07001440 int fd = adb_connect(cmd, &error);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -07001441 if (fd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001442 fprintf(stderr, "error: %s\n", error.c_str());
Jeff Sharkey5d9d4342014-05-26 18:30:43 -07001443 return -1;
1444 }
1445
1446 if (exec_in) {
1447 copy_to_file(STDIN_FILENO, fd);
1448 } else {
1449 copy_to_file(fd, STDOUT_FILENO);
1450 }
1451
1452 adb_close(fd);
1453 return 0;
1454 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001455 else if (!strcmp(argv[0], "kill-server")) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001456 std::string error;
1457 int fd = _adb_connect("host:kill", &error);
Spencer Lowf18fc082015-08-11 17:05:02 -07001458 if (fd == -2) {
1459 // Failed to make network connection to server. Don't output the
1460 // network error since that is expected.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 fprintf(stderr,"* server not running *\n");
Spencer Lowf18fc082015-08-11 17:05:02 -07001462 // Successful exit code because the server is already "killed".
1463 return 0;
1464 } else if (fd == -1) {
1465 // Some other error.
1466 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001467 return 1;
Spencer Lowf18fc082015-08-11 17:05:02 -07001468 } else {
1469 // Successfully connected, kill command sent, okay status came back.
1470 // Server should exit() in a moment, if not already.
1471 adb_close(fd);
1472 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001473 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001474 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001475 else if (!strcmp(argv[0], "sideload")) {
1476 if (argc != 2) return usage();
Doug Zongker71fe5842014-06-26 15:35:36 -07001477 if (adb_sideload_host(argv[1])) {
Doug Zongker447f0612012-01-09 14:54:53 -08001478 return 1;
1479 } else {
1480 return 0;
1481 }
1482 }
Elliott Hughes19d80b82015-07-21 16:13:40 -07001483 else if (!strcmp(argv[0], "tcpip") && argc > 1) {
1484 return adb_connect_command(android::base::StringPrintf("tcpip:%s", argv[1]));
1485 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001486 else if (!strcmp(argv[0], "remount") ||
1487 !strcmp(argv[0], "reboot") ||
1488 !strcmp(argv[0], "reboot-bootloader") ||
Riley Andrewsc8514c82014-12-05 17:32:46 -08001489 !strcmp(argv[0], "usb") ||
1490 !strcmp(argv[0], "root") ||
Dan Pasanen98858812014-10-06 12:57:20 -05001491 !strcmp(argv[0], "unroot") ||
Riley Andrewsc8514c82014-12-05 17:32:46 -08001492 !strcmp(argv[0], "disable-verity") ||
1493 !strcmp(argv[0], "enable-verity")) {
Elliott Hughes5677c232015-05-07 23:37:40 -07001494 std::string command;
Tao Bao175b7bb2015-03-29 11:22:34 -07001495 if (!strcmp(argv[0], "reboot-bootloader")) {
Elliott Hughes5677c232015-05-07 23:37:40 -07001496 command = "reboot:bootloader";
Tao Bao175b7bb2015-03-29 11:22:34 -07001497 } else if (argc > 1) {
Elliott Hughes5677c232015-05-07 23:37:40 -07001498 command = android::base::StringPrintf("%s:%s", argv[0], argv[1]);
Tao Bao175b7bb2015-03-29 11:22:34 -07001499 } else {
Elliott Hughes5677c232015-05-07 23:37:40 -07001500 command = android::base::StringPrintf("%s:", argv[0]);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -07001501 }
Tao Bao175b7bb2015-03-29 11:22:34 -07001502 return adb_connect_command(command);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -07001503 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001504 else if (!strcmp(argv[0], "bugreport")) {
Dan Egnorc130ea72010-01-20 13:50:36 -08001505 if (argc != 1) return usage();
David Pursell70ef7b42015-09-30 13:35:42 -07001506 // No need for shell protocol with bugreport, always disable for
1507 // simplicity.
1508 return send_shell_command(transport_type, serial, "bugreport", true);
Mike Lockwoodf56d1b52009-09-03 14:54:58 -04001509 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001510 else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
Elliott Hughes424af022015-05-29 17:55:19 -07001511 bool reverse = !strcmp(argv[0], "reverse");
1512 ++argv;
1513 --argc;
1514 if (argc < 1) return usage();
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001515
1516 // Determine the <host-prefix> for this command.
Elliott Hughes424af022015-05-29 17:55:19 -07001517 std::string host_prefix;
David 'Digit' Turner25258692013-03-21 21:07:42 +01001518 if (reverse) {
Elliott Hughes424af022015-05-29 17:55:19 -07001519 host_prefix = "reverse";
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001520 } else {
David 'Digit' Turner25258692013-03-21 21:07:42 +01001521 if (serial) {
Elliott Hughes424af022015-05-29 17:55:19 -07001522 host_prefix = android::base::StringPrintf("host-serial:%s", serial);
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001523 } else if (transport_type == kTransportUsb) {
Elliott Hughes424af022015-05-29 17:55:19 -07001524 host_prefix = "host-usb";
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001525 } else if (transport_type == kTransportLocal) {
Elliott Hughes424af022015-05-29 17:55:19 -07001526 host_prefix = "host-local";
David 'Digit' Turner25258692013-03-21 21:07:42 +01001527 } else {
Elliott Hughes424af022015-05-29 17:55:19 -07001528 host_prefix = "host";
David 'Digit' Turner25258692013-03-21 21:07:42 +01001529 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001530 }
1531
Elliott Hughes424af022015-05-29 17:55:19 -07001532 std::string cmd;
1533 if (strcmp(argv[0], "--list") == 0) {
Elliott Hughesab52c182015-05-01 17:04:38 -07001534 if (argc != 1) return usage();
Elliott Hughes424af022015-05-29 17:55:19 -07001535 return adb_query_command(host_prefix + ":list-forward");
1536 } else if (strcmp(argv[0], "--remove-all") == 0) {
1537 if (argc != 1) return usage();
1538 cmd = host_prefix + ":killforward-all";
1539 } else if (strcmp(argv[0], "--remove") == 0) {
1540 // forward --remove <local>
Elliott Hughesab52c182015-05-01 17:04:38 -07001541 if (argc != 2) return usage();
Elliott Hughes424af022015-05-29 17:55:19 -07001542 cmd = host_prefix + ":killforward:" + argv[1];
1543 } else if (strcmp(argv[0], "--no-rebind") == 0) {
1544 // forward --no-rebind <local> <remote>
Elliott Hughesab52c182015-05-01 17:04:38 -07001545 if (argc != 3) return usage();
Elliott Hughes424af022015-05-29 17:55:19 -07001546 cmd = host_prefix + ":forward:norebind:" + argv[1] + ";" + argv[2];
1547 } else {
1548 // forward <local> <remote>
1549 if (argc != 2) return usage();
1550 cmd = host_prefix + ":forward:" + argv[0] + ";" + argv[1];
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001551 }
1552
Elliott Hughes424af022015-05-29 17:55:19 -07001553 return adb_command(cmd) ? 0 : 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001554 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001555 /* do_sync_*() commands */
Riley Andrewsc8514c82014-12-05 17:32:46 -08001556 else if (!strcmp(argv[0], "ls")) {
1557 if (argc != 2) return usage();
Elliott Hughesaa245492015-08-03 10:38:08 -07001558 return do_sync_ls(argv[1]) ? 0 : 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001559 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001560 else if (!strcmp(argv[0], "push")) {
Elliott Hughesaa245492015-08-03 10:38:08 -07001561 int copy_attrs = 0;
Mark Lindner76f2a932014-03-11 17:55:59 -07001562 const char* lpath = NULL, *rpath = NULL;
1563
Elliott Hughesb708d162015-10-27 16:03:15 -07001564 parse_push_pull_args(&argv[1], argc - 1, &lpath, &rpath, &copy_attrs);
Elliott Hughesaa245492015-08-03 10:38:08 -07001565 if (!lpath || !rpath || copy_attrs != 0) return usage();
Elliott Hughesb708d162015-10-27 16:03:15 -07001566 return do_sync_push(lpath, rpath) ? 0 : 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001567 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001568 else if (!strcmp(argv[0], "pull")) {
Lajos Molnarde8ff4a2013-04-19 12:41:09 -07001569 int copy_attrs = 0;
Mark Lindner76f2a932014-03-11 17:55:59 -07001570 const char* rpath = NULL, *lpath = ".";
1571
Elliott Hughesb708d162015-10-27 16:03:15 -07001572 parse_push_pull_args(&argv[1], argc - 1, &rpath, &lpath, &copy_attrs);
Elliott Hughesaa245492015-08-03 10:38:08 -07001573 if (!rpath) return usage();
Elliott Hughesb708d162015-10-27 16:03:15 -07001574 return do_sync_pull(rpath, lpath, copy_attrs) ? 0 : 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001575 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001576 else if (!strcmp(argv[0], "install")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001577 if (argc < 2) return usage();
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001578 return install_app(transport_type, serial, argc, argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001579 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001580 else if (!strcmp(argv[0], "install-multiple")) {
Jeff Sharkey960df972014-06-09 17:30:57 -07001581 if (argc < 2) return usage();
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001582 return install_multiple_app(transport_type, serial, argc, argv);
Jeff Sharkey960df972014-06-09 17:30:57 -07001583 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001584 else if (!strcmp(argv[0], "uninstall")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001585 if (argc < 2) return usage();
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001586 return uninstall_app(transport_type, serial, argc, argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001587 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001588 else if (!strcmp(argv[0], "sync")) {
Elliott Hughesd236d072015-04-21 10:17:07 -07001589 std::string src;
Elliott Hughes58305772015-04-17 13:57:15 -07001590 bool list_only = false;
Riley Andrews98f58e82014-12-05 17:37:24 -08001591 if (argc < 2) {
Elliott Hughes58305772015-04-17 13:57:15 -07001592 // No local path was specified.
Elliott Hughesd236d072015-04-21 10:17:07 -07001593 src = "";
Anthony Newnam705c9442010-02-22 08:36:49 -06001594 } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
Elliott Hughesd236d072015-04-21 10:17:07 -07001595 list_only = true;
Anthony Newnam705c9442010-02-22 08:36:49 -06001596 if (argc == 3) {
Elliott Hughesd236d072015-04-21 10:17:07 -07001597 src = argv[2];
Anthony Newnam705c9442010-02-22 08:36:49 -06001598 } else {
Elliott Hughesd236d072015-04-21 10:17:07 -07001599 src = "";
Anthony Newnam705c9442010-02-22 08:36:49 -06001600 }
Riley Andrews98f58e82014-12-05 17:37:24 -08001601 } else if (argc == 2) {
Elliott Hughes58305772015-04-17 13:57:15 -07001602 // A local path or "android"/"data" arg was specified.
Elliott Hughesd236d072015-04-21 10:17:07 -07001603 src = argv[1];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001604 } else {
1605 return usage();
1606 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001607
Elliott Hughesd236d072015-04-21 10:17:07 -07001608 if (src != "" &&
1609 src != "system" && src != "data" && src != "vendor" && src != "oem") {
Elliott Hughes58305772015-04-17 13:57:15 -07001610 return usage();
1611 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001612
Elliott Hughes58305772015-04-17 13:57:15 -07001613 std::string system_src_path = product_file("system");
1614 std::string data_src_path = product_file("data");
1615 std::string vendor_src_path = product_file("vendor");
1616 std::string oem_src_path = product_file("oem");
Elliott Hughes58305772015-04-17 13:57:15 -07001617
Elliott Hughesaa245492015-08-03 10:38:08 -07001618 bool okay = true;
1619 if (okay && (src.empty() || src == "system")) {
1620 okay = do_sync_sync(system_src_path, "/system", list_only);
Elliott Hughes58305772015-04-17 13:57:15 -07001621 }
Elliott Hughesaa245492015-08-03 10:38:08 -07001622 if (okay && (src.empty() || src == "vendor") && directory_exists(vendor_src_path)) {
1623 okay = do_sync_sync(vendor_src_path, "/vendor", list_only);
Elliott Hughes58305772015-04-17 13:57:15 -07001624 }
Elliott Hughesaa245492015-08-03 10:38:08 -07001625 if (okay && (src.empty() || src == "oem") && directory_exists(oem_src_path)) {
1626 okay = do_sync_sync(oem_src_path, "/oem", list_only);
Elliott Hughes58305772015-04-17 13:57:15 -07001627 }
Elliott Hughesaa245492015-08-03 10:38:08 -07001628 if (okay && (src.empty() || src == "data")) {
1629 okay = do_sync_sync(data_src_path, "/data", list_only);
Elliott Hughes58305772015-04-17 13:57:15 -07001630 }
Elliott Hughesaa245492015-08-03 10:38:08 -07001631 return okay ? 0 : 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001632 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001633 /* passthrough commands */
Riley Andrewsc8514c82014-12-05 17:32:46 -08001634 else if (!strcmp(argv[0],"get-state") ||
Scott Andersone109d262012-04-20 11:21:14 -07001635 !strcmp(argv[0],"get-serialno") ||
1636 !strcmp(argv[0],"get-devpath"))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001637 {
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001638 return adb_query_command(format_host_command(argv[0], transport_type, serial));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001639 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001640 /* other commands */
Riley Andrewsc8514c82014-12-05 17:32:46 -08001641 else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001642 return logcat(transport_type, serial, argc, argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001643 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001644 else if (!strcmp(argv[0],"ppp")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001645 return ppp(argc, argv);
1646 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001647 else if (!strcmp(argv[0], "start-server")) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001648 std::string error;
Spencer Lowf18fc082015-08-11 17:05:02 -07001649 const int result = adb_connect("host:start-server", &error);
1650 if (result < 0) {
1651 fprintf(stderr, "error: %s\n", error.c_str());
1652 }
1653 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001655 else if (!strcmp(argv[0], "backup")) {
Christopher Tated2f54152011-04-21 12:53:28 -07001656 return backup(argc, argv);
1657 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001658 else if (!strcmp(argv[0], "restore")) {
Christopher Tate702967a2011-05-17 15:52:54 -07001659 return restore(argc, argv);
1660 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001661 else if (!strcmp(argv[0], "keygen")) {
Nick Kralevichbea3f9c2014-11-13 15:17:29 -08001662 if (argc < 2) return usage();
Yabin Cuiaed3c612015-09-22 15:52:57 -07001663 // Always print key generation information for keygen command.
1664 adb_trace_enable(AUTH);
Nick Kralevichbea3f9c2014-11-13 15:17:29 -08001665 return adb_auth_keygen(argv[1]);
1666 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001667 else if (!strcmp(argv[0], "jdwp")) {
Tao Bao175b7bb2015-03-29 11:22:34 -07001668 return adb_connect_command("jdwp");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001669 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001670 /* "adb /?" is a common idiom under Windows */
Riley Andrewsc8514c82014-12-05 17:32:46 -08001671 else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001672 help();
1673 return 0;
1674 }
Riley Andrewsc8514c82014-12-05 17:32:46 -08001675 else if (!strcmp(argv[0], "version")) {
Elliott Hughes42ae2602015-08-12 08:32:10 -07001676 fprintf(stdout, "%s", adb_version().c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001677 return 0;
1678 }
Dan Albert90d4b732015-05-20 18:58:41 -07001679 else if (!strcmp(argv[0], "features")) {
David Pursell4e2fd362015-09-22 10:43:08 -07001680 // Only list the features common to both the adb client and the device.
1681 FeatureSet features = GetFeatureSet(transport_type, serial);
1682 for (const std::string& name : features) {
David Pursell70ef7b42015-09-30 13:35:42 -07001683 if (CanUseFeature(features, name)) {
David Pursell4e2fd362015-09-22 10:43:08 -07001684 printf("%s\n", name.c_str());
1685 }
1686 }
1687 return 0;
Dan Albert90d4b732015-05-20 18:58:41 -07001688 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001689
1690 usage();
1691 return 1;
1692}
1693
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001694static int pm_command(TransportType transport, const char* serial, int argc, const char** argv) {
David Pursell70ef7b42015-09-30 13:35:42 -07001695 std::string cmd = "pm";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001696
Elliott Hughes2baae3a2015-04-17 10:59:34 -07001697 while (argc-- > 0) {
Elliott Hughes6c34bba2015-04-17 20:11:08 -07001698 cmd += " " + escape_arg(*argv++);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001699 }
1700
David Pursell70ef7b42015-09-30 13:35:42 -07001701 // TODO(dpursell): add command-line arguments to install/uninstall to
1702 // manually disable shell protocol if needed.
1703 return send_shell_command(transport, serial, cmd, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001704}
1705
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001706static int uninstall_app(TransportType transport, const char* serial, int argc, const char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001707 /* if the user choose the -k option, we refuse to do it until devices are
1708 out with the option to uninstall the remaining data somehow (adb/ui) */
1709 if (argc == 3 && strcmp(argv[1], "-k") == 0)
1710 {
1711 printf(
1712 "The -k option uninstalls the application while retaining the data/cache.\n"
1713 "At the moment, there is no way to remove the remaining data.\n"
1714 "You will have to reinstall the application with the same signature, and fully uninstall it.\n"
1715 "If you truly wish to continue, execute 'adb shell pm uninstall -k %s'\n", argv[2]);
1716 return -1;
1717 }
1718
1719 /* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */
1720 return pm_command(transport, serial, argc, argv);
1721}
1722
Elliott Hughes5c742702015-07-30 17:42:01 -07001723static int delete_file(TransportType transport, const char* serial, const std::string& filename) {
David Pursell70ef7b42015-09-30 13:35:42 -07001724 std::string cmd = "rm -f " + escape_arg(filename);
1725 return send_shell_command(transport, serial, cmd, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001726}
1727
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001728static int install_app(TransportType transport, const char* serial, int argc, const char** argv) {
Dan Albert286bb6d2015-07-09 20:35:09 +00001729 static const char *const DATA_DEST = "/data/local/tmp/%s";
1730 static const char *const SD_DEST = "/sdcard/tmp/%s";
Kenny Root597ea5b2011-08-05 11:19:45 -07001731 const char* where = DATA_DEST;
Kenny Root597ea5b2011-08-05 11:19:45 -07001732 int i;
Jeff Sharkey960df972014-06-09 17:30:57 -07001733 struct stat sb;
Kenny Root597ea5b2011-08-05 11:19:45 -07001734
1735 for (i = 1; i < argc; i++) {
Jeff Sharkey960df972014-06-09 17:30:57 -07001736 if (!strcmp(argv[i], "-s")) {
Kenny Root597ea5b2011-08-05 11:19:45 -07001737 where = SD_DEST;
1738 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001739 }
1740
Jeff Sharkey960df972014-06-09 17:30:57 -07001741 // Find last APK argument.
1742 // All other arguments passed through verbatim.
1743 int last_apk = -1;
1744 for (i = argc - 1; i >= 0; i--) {
Dan Albertbac34742015-02-25 17:51:28 -08001745 const char* file = argv[i];
Elliott Hughes3e7048c2015-07-27 21:21:39 -07001746 const char* dot = strrchr(file, '.');
Jeff Sharkey960df972014-06-09 17:30:57 -07001747 if (dot && !strcasecmp(dot, ".apk")) {
1748 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
1749 fprintf(stderr, "Invalid APK file: %s\n", file);
1750 return -1;
1751 }
1752
1753 last_apk = i;
1754 break;
1755 }
Kenny Root597ea5b2011-08-05 11:19:45 -07001756 }
1757
Jeff Sharkey960df972014-06-09 17:30:57 -07001758 if (last_apk == -1) {
1759 fprintf(stderr, "Missing APK file\n");
1760 return -1;
Kenny Root597ea5b2011-08-05 11:19:45 -07001761 }
1762
Elliott Hughesaa245492015-08-03 10:38:08 -07001763 int result = -1;
Dan Albertbac34742015-02-25 17:51:28 -08001764 const char* apk_file = argv[last_apk];
Elliott Hughes5c742702015-07-30 17:42:01 -07001765 std::string apk_dest = android::base::StringPrintf(where, adb_basename(apk_file).c_str());
Elliott Hughesb708d162015-10-27 16:03:15 -07001766 if (!do_sync_push(apk_file, apk_dest.c_str())) goto cleanup_apk;
Elliott Hughesaa245492015-08-03 10:38:08 -07001767 argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
1768 result = pm_command(transport, serial, argc, argv);
Kenny Root597ea5b2011-08-05 11:19:45 -07001769
Kenny Root60733e92012-03-26 16:14:02 -07001770cleanup_apk:
Dan Albert286bb6d2015-07-09 20:35:09 +00001771 delete_file(transport, serial, apk_dest);
Elliott Hughesaa245492015-08-03 10:38:08 -07001772 return result;
Jeff Sharkey960df972014-06-09 17:30:57 -07001773}
1774
Elliott Hughes3bd73c12015-05-05 13:10:43 -07001775static int install_multiple_app(TransportType transport, const char* serial, int argc,
Elliott Hughes58305772015-04-17 13:57:15 -07001776 const char** argv)
Jeff Sharkey960df972014-06-09 17:30:57 -07001777{
Jeff Sharkey960df972014-06-09 17:30:57 -07001778 int i;
1779 struct stat sb;
Elliott Hughes2940ccf2015-04-17 14:07:52 -07001780 uint64_t total_size = 0;
Jeff Sharkey960df972014-06-09 17:30:57 -07001781
1782 // Find all APK arguments starting at end.
1783 // All other arguments passed through verbatim.
1784 int first_apk = -1;
1785 for (i = argc - 1; i >= 0; i--) {
Dan Albertbac34742015-02-25 17:51:28 -08001786 const char* file = argv[i];
Elliott Hughes3e7048c2015-07-27 21:21:39 -07001787 const char* dot = strrchr(file, '.');
Jeff Sharkey960df972014-06-09 17:30:57 -07001788 if (dot && !strcasecmp(dot, ".apk")) {
1789 if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
1790 fprintf(stderr, "Invalid APK file: %s\n", file);
1791 return -1;
1792 }
1793
1794 total_size += sb.st_size;
1795 first_apk = i;
1796 } else {
1797 break;
1798 }
Kenny Root597ea5b2011-08-05 11:19:45 -07001799 }
1800
Jeff Sharkey960df972014-06-09 17:30:57 -07001801 if (first_apk == -1) {
1802 fprintf(stderr, "Missing APK file\n");
1803 return 1;
1804 }
Kenny Root597ea5b2011-08-05 11:19:45 -07001805
Elliott Hughes2940ccf2015-04-17 14:07:52 -07001806 std::string cmd = android::base::StringPrintf("exec:pm install-create -S %" PRIu64, total_size);
Jeff Sharkey960df972014-06-09 17:30:57 -07001807 for (i = 1; i < first_apk; i++) {
Elliott Hughes6c34bba2015-04-17 20:11:08 -07001808 cmd += " " + escape_arg(argv[i]);
Jeff Sharkey960df972014-06-09 17:30:57 -07001809 }
1810
1811 // Create install session
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001812 std::string error;
Elliott Hughes6452a892015-04-29 12:28:13 -07001813 int fd = adb_connect(cmd, &error);
Jeff Sharkey960df972014-06-09 17:30:57 -07001814 if (fd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001815 fprintf(stderr, "Connect error for create: %s\n", error.c_str());
Jeff Sharkey960df972014-06-09 17:30:57 -07001816 return -1;
1817 }
Elliott Hughes2baae3a2015-04-17 10:59:34 -07001818 char buf[BUFSIZ];
Jeff Sharkey960df972014-06-09 17:30:57 -07001819 read_status_line(fd, buf, sizeof(buf));
1820 adb_close(fd);
1821
1822 int session_id = -1;
1823 if (!strncmp("Success", buf, 7)) {
1824 char* start = strrchr(buf, '[');
1825 char* end = strrchr(buf, ']');
1826 if (start && end) {
1827 *end = '\0';
1828 session_id = strtol(start + 1, NULL, 10);
1829 }
1830 }
1831 if (session_id < 0) {
1832 fprintf(stderr, "Failed to create session\n");
Christopher Tate71bbc672014-07-14 16:45:13 -07001833 fputs(buf, stderr);
Jeff Sharkey960df972014-06-09 17:30:57 -07001834 return -1;
1835 }
1836
1837 // Valid session, now stream the APKs
1838 int success = 1;
1839 for (i = first_apk; i < argc; i++) {
Dan Albertbac34742015-02-25 17:51:28 -08001840 const char* file = argv[i];
Jeff Sharkey960df972014-06-09 17:30:57 -07001841 if (stat(file, &sb) == -1) {
1842 fprintf(stderr, "Failed to stat %s\n", file);
1843 success = 0;
1844 goto finalize_session;
1845 }
1846
Elliott Hughes2940ccf2015-04-17 14:07:52 -07001847 std::string cmd = android::base::StringPrintf(
1848 "exec:pm install-write -S %" PRIu64 " %d %d_%s -",
Elliott Hughes5c742702015-07-30 17:42:01 -07001849 static_cast<uint64_t>(sb.st_size), session_id, i, adb_basename(file).c_str());
Jeff Sharkey960df972014-06-09 17:30:57 -07001850
1851 int localFd = adb_open(file, O_RDONLY);
1852 if (localFd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001853 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
Jeff Sharkey960df972014-06-09 17:30:57 -07001854 success = 0;
1855 goto finalize_session;
1856 }
1857
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001858 std::string error;
Elliott Hughes6452a892015-04-29 12:28:13 -07001859 int remoteFd = adb_connect(cmd, &error);
Jeff Sharkey960df972014-06-09 17:30:57 -07001860 if (remoteFd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001861 fprintf(stderr, "Connect error for write: %s\n", error.c_str());
Jeff Sharkey960df972014-06-09 17:30:57 -07001862 adb_close(localFd);
1863 success = 0;
1864 goto finalize_session;
1865 }
1866
1867 copy_to_file(localFd, remoteFd);
1868 read_status_line(remoteFd, buf, sizeof(buf));
1869
1870 adb_close(localFd);
1871 adb_close(remoteFd);
1872
1873 if (strncmp("Success", buf, 7)) {
1874 fprintf(stderr, "Failed to write %s\n", file);
Christopher Tate71bbc672014-07-14 16:45:13 -07001875 fputs(buf, stderr);
Jeff Sharkey960df972014-06-09 17:30:57 -07001876 success = 0;
1877 goto finalize_session;
1878 }
1879 }
1880
1881finalize_session:
Jeff Sharkeyac77e1f2014-07-25 09:58:25 -07001882 // Commit session if we streamed everything okay; otherwise abandon
Elliott Hughes6452a892015-04-29 12:28:13 -07001883 std::string service =
1884 android::base::StringPrintf("exec:pm install-%s %d",
1885 success ? "commit" : "abandon", session_id);
1886 fd = adb_connect(service, &error);
Jeff Sharkey960df972014-06-09 17:30:57 -07001887 if (fd < 0) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -07001888 fprintf(stderr, "Connect error for finalize: %s\n", error.c_str());
Jeff Sharkey960df972014-06-09 17:30:57 -07001889 return -1;
1890 }
1891 read_status_line(fd, buf, sizeof(buf));
1892 adb_close(fd);
1893
1894 if (!strncmp("Success", buf, 7)) {
Christopher Tate71bbc672014-07-14 16:45:13 -07001895 fputs(buf, stderr);
Jeff Sharkey960df972014-06-09 17:30:57 -07001896 return 0;
1897 } else {
1898 fprintf(stderr, "Failed to finalize session\n");
Christopher Tate71bbc672014-07-14 16:45:13 -07001899 fputs(buf, stderr);
Jeff Sharkey960df972014-06-09 17:30:57 -07001900 return -1;
1901 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001902}