2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 06:52:57 +08:00
|
|
|
#define TRACE_TAG ADB
|
2015-03-20 06:21:08 +08:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-25 07:51:19 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <errno.h>
|
2015-04-18 05:07:52 +08:00
|
|
|
#include <inttypes.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
2015-02-25 07:51:19 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <sys/stat.h>
|
2015-02-25 07:51:19 +08:00
|
|
|
#include <sys/types.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-09-09 08:17:02 +08:00
|
|
|
#include <memory>
|
2015-04-18 01:59:34 +08:00
|
|
|
#include <string>
|
2016-11-16 04:37:32 +08:00
|
|
|
#include <thread>
|
2015-10-31 07:57:19 +08:00
|
|
|
#include <vector>
|
2015-04-18 01:59:34 +08:00
|
|
|
|
2016-05-27 13:43:19 +08:00
|
|
|
#include <android-base/file.h>
|
2015-12-05 14:00:26 +08:00
|
|
|
#include <android-base/logging.h>
|
2016-09-17 04:01:24 +08:00
|
|
|
#include <android-base/parseint.h>
|
2015-12-05 14:00:26 +08:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2015-04-18 01:59:34 +08:00
|
|
|
|
2014-11-18 06:48:25 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-10-24 06:03:31 +08:00
|
|
|
#include <signal.h>
|
2015-11-05 05:07:47 +08:00
|
|
|
#include <sys/ioctl.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <termios.h>
|
2015-02-25 07:51:19 +08:00
|
|
|
#include <unistd.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "adb.h"
|
2014-11-14 07:17:29 +08:00
|
|
|
#include "adb_auth.h"
|
2015-02-25 13:26:58 +08:00
|
|
|
#include "adb_client.h"
|
|
|
|
#include "adb_io.h"
|
2016-08-31 06:39:25 +08:00
|
|
|
#include "adb_unique_fd.h"
|
2015-04-18 04:57:15 +08:00
|
|
|
#include "adb_utils.h"
|
2016-07-20 08:07:22 +08:00
|
|
|
#include "bugreport.h"
|
|
|
|
#include "commandline.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
#include "file_sync_service.h"
|
2015-10-01 04:35:42 +08:00
|
|
|
#include "services.h"
|
2015-09-09 08:17:02 +08:00
|
|
|
#include "shell_service.h"
|
2016-11-16 10:55:47 +08:00
|
|
|
#include "sysdeps/chrono.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-05-06 04:10:43 +08:00
|
|
|
static int install_app(TransportType t, const char* serial, int argc, const char** argv);
|
|
|
|
static int install_multiple_app(TransportType t, const char* serial, int argc, const char** argv);
|
|
|
|
static int uninstall_app(TransportType t, const char* serial, int argc, const char** argv);
|
2015-11-04 08:53:08 +08:00
|
|
|
static int install_app_legacy(TransportType t, const char* serial, int argc, const char** argv);
|
|
|
|
static int uninstall_app_legacy(TransportType t, const char* serial, int argc, const char** argv);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-11-12 09:56:12 +08:00
|
|
|
static auto& gProductOutPath = *new std::string();
|
2012-11-15 02:16:17 +08:00
|
|
|
extern int gListenAll;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2016-07-27 03:14:39 +08:00
|
|
|
DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK(nullptr, nullptr);
|
|
|
|
|
2015-04-18 04:57:15 +08:00
|
|
|
static std::string product_file(const char *extra) {
|
|
|
|
if (gProductOutPath.empty()) {
|
2009-03-04 11:32:55 +08:00
|
|
|
fprintf(stderr, "adb: Product directory not specified; "
|
|
|
|
"use -p or define ANDROID_PRODUCT_OUT\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-04-18 04:57:15 +08:00
|
|
|
return android::base::StringPrintf("%s%s%s",
|
|
|
|
gProductOutPath.c_str(), OS_PATH_SEPARATOR_STR, extra);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-18 04:57:15 +08:00
|
|
|
static void help() {
|
2016-09-29 06:25:47 +08:00
|
|
|
fprintf(stdout, "%s\n", adb_version().c_str());
|
2016-07-30 08:57:00 +08:00
|
|
|
// clang-format off
|
2016-09-29 06:25:47 +08:00
|
|
|
fprintf(stdout,
|
|
|
|
"global options:\n"
|
|
|
|
" -a listen on all network interfaces, not just localhost\n"
|
|
|
|
" -d use USB device (error if multiple devices connected)\n"
|
|
|
|
" -e use emulator (error if multiple emulators running)\n"
|
|
|
|
" -s SERIAL\n"
|
|
|
|
" use device/emulator with given serial number (overrides $ANDROID_SERIAL)\n"
|
|
|
|
" -p PRODUCT\n"
|
|
|
|
" name or path ('angler'/'out/target/product/angler');\n"
|
|
|
|
" default $ANDROID_PRODUCT_OUT\n"
|
|
|
|
" -H name of adb server host [default=localhost]\n"
|
|
|
|
" -P port of adb server [default=5037]\n"
|
|
|
|
" -L SOCKET listen on given socket for adb server [default=tcp:localhost:5037]\n"
|
2009-03-04 11:32:55 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"general commands:\n"
|
|
|
|
" devices [-l] list connected devices (-l for long output)\n"
|
|
|
|
" help show this help message\n"
|
|
|
|
" version show version num\n"
|
2011-04-22 03:53:28 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"networking:\n"
|
|
|
|
" connect HOST[:PORT] connect to a device via TCP/IP [default port=5555]\n"
|
|
|
|
" disconnect [HOST[:PORT]]\n"
|
|
|
|
" disconnect from given TCP/IP device [default port=5555], or all\n"
|
|
|
|
" forward --list list all forward socket connections\n"
|
|
|
|
" forward [--no-rebind] LOCAL REMOTE\n"
|
|
|
|
" forward socket connection using:\n"
|
|
|
|
" tcp:<port> (<local> may be \"tcp:0\" to pick any open port)\n"
|
|
|
|
" localabstract:<unix domain socket name>\n"
|
|
|
|
" localreserved:<unix domain socket name>\n"
|
|
|
|
" localfilesystem:<unix domain socket name>\n"
|
|
|
|
" dev:<character device name>\n"
|
|
|
|
" jdwp:<process pid> (remote only)\n"
|
|
|
|
" forward --remove LOCAL remove specific forward socket connection\n"
|
|
|
|
" forward --remove-all remove all forward socket connections\n"
|
|
|
|
" ppp TTY [PARAMETER...] run PPP over USB\n"
|
|
|
|
" reverse --list list all reverse socket connections from device\n"
|
|
|
|
" reverse [--no-rebind] REMOTE LOCAL\n"
|
|
|
|
" reverse socket connection using:\n"
|
|
|
|
" tcp:<port> (<remote> may be \"tcp:0\" to pick any open port)\n"
|
|
|
|
" localabstract:<unix domain socket name>\n"
|
|
|
|
" localreserved:<unix domain socket name>\n"
|
|
|
|
" localfilesystem:<unix domain socket name>\n"
|
|
|
|
" reverse --remove REMOTE remove specific reverse socket connection\n"
|
|
|
|
" reverse --remove-all remove all reverse socket connections from device\n"
|
2011-05-18 06:52:54 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"file transfer:\n"
|
|
|
|
" push LOCAL... REMOTE\n"
|
|
|
|
" copy local files/directories to device\n"
|
|
|
|
" pull [-a] REMOTE... LOCAL\n"
|
|
|
|
" copy files/dirs from device\n"
|
|
|
|
" -a: preserve file timestamp and mode\n"
|
|
|
|
" sync [DIR]\n"
|
|
|
|
" copy all changed files to device; if DIR is \"system\", \"vendor\", \"oem\",\n"
|
|
|
|
" or \"data\", only sync that partition (default all)\n"
|
|
|
|
" -l: list but don't copy\n"
|
2009-03-04 11:32:55 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"shell:\n"
|
|
|
|
" shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]\n"
|
|
|
|
" run remote shell command (interactive shell if no command given)\n"
|
|
|
|
" -e: choose escape character, or \"none\"; default '~'\n"
|
|
|
|
" -n: don't read from stdin\n"
|
|
|
|
" -T: disable PTY allocation\n"
|
|
|
|
" -t: force PTY allocation\n"
|
|
|
|
" -x: disable remote exit codes and stdout/stderr separation\n"
|
|
|
|
" emu COMMAND run emulator console command\n"
|
2015-06-13 05:26:29 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"app installation:\n"
|
|
|
|
" install [-lrtsdg] PACKAGE\n"
|
|
|
|
" install-multiple [-lrtsdpg] PACKAGE...\n"
|
|
|
|
" push package(s) to the device and install them\n"
|
|
|
|
" -l: forward lock application\n"
|
|
|
|
" -r: replace existing application\n"
|
|
|
|
" -t: allow test packages\n"
|
|
|
|
" -s: install application on sdcard\n"
|
|
|
|
" -d: allow version code downgrade (debuggable packages only)\n"
|
|
|
|
" -p: partial application install (install-multiple only)\n"
|
|
|
|
" -g: grant all runtime permissions\n"
|
|
|
|
" uninstall [-k] PACKAGE\n"
|
|
|
|
" remove this app package from the device\n"
|
|
|
|
" '-k': keep the data and cache directories\n"
|
2009-03-04 11:32:55 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"backup/restore:\n"
|
|
|
|
" backup [-f FILE] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [PACKAGE...]\n"
|
|
|
|
" write an archive of the device's data to FILE [default=backup.adb]\n"
|
|
|
|
" package list optional if -all/-shared are supplied\n"
|
|
|
|
" -apk/-noapk: do/don't back up .apk files (default -noapk)\n"
|
|
|
|
" -obb/-noobb: do/don't back up .obb files (default -noobb)\n"
|
|
|
|
" -shared|-noshared: do/don't back up shared storage (default -noshared)\n"
|
|
|
|
" -all: back up all installed applications\n"
|
|
|
|
" -system|-nosystem: include system apps in -all (default -system)\n"
|
|
|
|
" restore FILE restore device contents from FILE\n"
|
2009-03-04 11:32:55 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"debugging:\n"
|
|
|
|
" bugreport [PATH]\n"
|
|
|
|
" write bugreport to given PATH [default=bugreport.zip];\n"
|
|
|
|
" if PATH is a directory, the bug report is saved in that directory.\n"
|
|
|
|
" devices that don't support zipped bug reports output to stdout.\n"
|
|
|
|
" jdwp list pids of processes hosting a JDWP transport\n"
|
|
|
|
" logcat show device log (logcat --help for more)\n"
|
2009-03-04 11:32:55 +08:00
|
|
|
"\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
"security:\n"
|
|
|
|
" disable-verity disable dm-verity checking on userdebug builds\n"
|
|
|
|
" enable-verity re-enable dm-verity checking on userdebug builds\n"
|
|
|
|
" keygen FILE\n"
|
|
|
|
" generate adb public/private key; private key stored in FILE,\n"
|
|
|
|
" public key stored in FILE.pub (existing files overwritten)\n"
|
|
|
|
"\n"
|
|
|
|
"scripting:\n"
|
|
|
|
" wait-for[-TRANSPORT]-STATE\n"
|
|
|
|
" wait for device to be in the given state\n"
|
|
|
|
" State: device, recovery, sideload, or bootloader\n"
|
|
|
|
" Transport: usb, local, or any [default=any]\n"
|
|
|
|
" get-state print offline | bootloader | device\n"
|
|
|
|
" get-serialno print <serial-number>\n"
|
|
|
|
" get-devpath print <device-path>\n"
|
|
|
|
" remount\n"
|
|
|
|
" remount /system, /vendor, and /oem partitions read-write\n"
|
|
|
|
" reboot [bootloader|recovery|sideload|sideload-auto-reboot]\n"
|
|
|
|
" reboot the device; defaults to booting system image but\n"
|
|
|
|
" supports bootloader and recovery too. sideload reboots\n"
|
|
|
|
" into recovery and automatically starts sideload mode,\n"
|
|
|
|
" sideload-auto-reboot is the same but reboots after sideloading.\n"
|
|
|
|
" sideload OTAPACKAGE sideload the given full OTA package\n"
|
|
|
|
" root restart adbd with root permissions\n"
|
|
|
|
" unroot restart adbd without root permissions\n"
|
|
|
|
" usb restart adb server listening on USB\n"
|
|
|
|
" tcpip PORT restart adb server listening on TCP on PORT\n"
|
2010-02-17 04:18:29 +08:00
|
|
|
"\n"
|
2016-04-06 04:50:44 +08:00
|
|
|
"internal debugging:\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
" start-server ensure that there is a server running\n"
|
|
|
|
" kill-server kill the server if it is running\n"
|
|
|
|
" reconnect kick connection from host side to force reconnect\n"
|
|
|
|
" reconnect device kick connection from device side to force reconnect\n"
|
|
|
|
"\n"
|
2015-06-13 05:26:29 +08:00
|
|
|
"environment variables:\n"
|
2016-09-29 06:25:47 +08:00
|
|
|
" $ADB_TRACE\n"
|
|
|
|
" comma-separated list of debug info to log:\n"
|
|
|
|
" all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp\n"
|
|
|
|
" $ADB_VENDOR_KEYS colon-separated list of keys (files or directories)\n"
|
|
|
|
" $ANDROID_SERIAL serial number to connect to (see -s)\n"
|
|
|
|
" $ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)\n");
|
2016-07-30 08:57:00 +08:00
|
|
|
// clang-format on
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-07-20 08:07:22 +08:00
|
|
|
int usage() {
|
2009-03-04 11:32:55 +08:00
|
|
|
help();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-18 06:48:25 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
2015-04-17 07:47:02 +08:00
|
|
|
// Implemented in sysdeps_win32.cpp.
|
2015-11-04 03:18:40 +08:00
|
|
|
void stdin_raw_init();
|
|
|
|
void stdin_raw_restore();
|
2014-11-18 06:48:25 +08:00
|
|
|
|
|
|
|
#else
|
2013-03-02 05:16:41 +08:00
|
|
|
static termios g_saved_terminal_state;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
static void stdin_raw_init() {
|
|
|
|
if (tcgetattr(STDIN_FILENO, &g_saved_terminal_state)) return;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2013-03-02 05:16:41 +08:00
|
|
|
termios tio;
|
2015-11-04 03:18:40 +08:00
|
|
|
if (tcgetattr(STDIN_FILENO, &tio)) return;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2013-03-02 05:16:41 +08:00
|
|
|
cfmakeraw(&tio);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2013-03-02 05:16:41 +08:00
|
|
|
// No timeout but request at least one character per read.
|
2009-03-04 11:32:55 +08:00
|
|
|
tio.c_cc[VTIME] = 0;
|
|
|
|
tio.c_cc[VMIN] = 1;
|
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
static void stdin_raw_restore() {
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &g_saved_terminal_state);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-09-09 08:17:02 +08:00
|
|
|
// Reads from |fd| and prints received data. If |use_shell_protocol| is true
|
|
|
|
// this expects that incoming data will use the shell protocol, in which case
|
|
|
|
// stdout/stderr are routed independently and the remote exit code will be
|
|
|
|
// returned.
|
2016-07-27 03:14:39 +08:00
|
|
|
// if |callback| is non-null, stdout/stderr output will be handled by it.
|
|
|
|
int read_and_dump(int fd, bool use_shell_protocol = false,
|
|
|
|
StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK) {
|
2015-09-09 08:17:02 +08:00
|
|
|
int exit_code = 0;
|
2016-06-07 07:05:36 +08:00
|
|
|
if (fd < 0) return exit_code;
|
|
|
|
|
2015-09-09 08:17:02 +08:00
|
|
|
std::unique_ptr<ShellProtocol> protocol;
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
char raw_buffer[BUFSIZ];
|
|
|
|
char* buffer_ptr = raw_buffer;
|
|
|
|
if (use_shell_protocol) {
|
|
|
|
protocol.reset(new ShellProtocol(fd));
|
|
|
|
if (!protocol) {
|
|
|
|
LOG(ERROR) << "failed to allocate memory for ShellProtocol object";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
buffer_ptr = protocol->data();
|
|
|
|
}
|
|
|
|
|
2016-06-07 07:05:36 +08:00
|
|
|
while (true) {
|
2015-09-09 08:17:02 +08:00
|
|
|
if (use_shell_protocol) {
|
|
|
|
if (!protocol->Read()) {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-27 03:14:39 +08:00
|
|
|
length = protocol->data_length();
|
2015-09-09 08:17:02 +08:00
|
|
|
switch (protocol->id()) {
|
|
|
|
case ShellProtocol::kIdStdout:
|
2016-07-27 03:14:39 +08:00
|
|
|
callback->OnStdout(buffer_ptr, length);
|
2015-09-09 08:17:02 +08:00
|
|
|
break;
|
|
|
|
case ShellProtocol::kIdStderr:
|
2016-07-27 03:14:39 +08:00
|
|
|
callback->OnStderr(buffer_ptr, length);
|
2015-09-09 08:17:02 +08:00
|
|
|
break;
|
|
|
|
case ShellProtocol::kIdExit:
|
|
|
|
exit_code = protocol->data()[0];
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
length = protocol->data_length();
|
|
|
|
} else {
|
|
|
|
D("read_and_dump(): pre adb_read(fd=%d)", fd);
|
|
|
|
length = adb_read(fd, raw_buffer, sizeof(raw_buffer));
|
|
|
|
D("read_and_dump(): post adb_read(fd=%d): length=%d", fd, length);
|
|
|
|
if (length <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-27 03:14:39 +08:00
|
|
|
callback->OnStdout(buffer_ptr, length);
|
2016-04-02 08:43:27 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-09-09 08:17:02 +08:00
|
|
|
|
2016-07-27 03:14:39 +08:00
|
|
|
return callback->Done(exit_code);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2014-06-10 08:30:57 +08:00
|
|
|
static void read_status_line(int fd, char* buf, size_t count)
|
|
|
|
{
|
|
|
|
count--;
|
|
|
|
while (count > 0) {
|
|
|
|
int len = adb_read(fd, buf, count);
|
2015-08-26 01:59:45 +08:00
|
|
|
if (len <= 0) {
|
2014-06-10 08:30:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf += len;
|
|
|
|
count -= len;
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
|
|
|
}
|
|
|
|
|
2016-07-07 04:22:22 +08:00
|
|
|
static void stdinout_raw_prologue(int inFd, int outFd, int& old_stdin_mode, int& old_stdout_mode) {
|
2014-05-27 09:30:43 +08:00
|
|
|
if (inFd == STDIN_FILENO) {
|
2015-11-04 03:18:40 +08:00
|
|
|
stdin_raw_init();
|
adb: win32: fix exec-in and exec-out to use binary mode
adb exec-in and exec-out are designed to read/write binary data
according to the commit description at:
https://android.googlesource.com/platform/system/core/+/5d9d434%5E!/
On Windows, when adb_read and adb_write are used, they are always in
binary mode (because sysdeps_win32.cpp calls Windows APIs direct). But
unix_read, unix_write, fread, fwrite, read, write use the text
translation mode of the C Runtime file descriptor, which is by default
textmode.
adb exec-in and exec-out use copy_to_file() which uses unix_read() and
fwrite() when reading/writing stdin and stdout, thus, copy_to_file()
should switch to binary mode for those cases (it already uses binary
mode for file descriptors other than stdin and stdout).
copy_to_file() is also called by adb backup, adb restore, and adb
install-multiple, but those do not use stdin or stdout, so those
codepaths should not be affected by this change.
Change-Id: I3446d9b363d20a2c2f6be2b96e55b653d99df2f9
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-05-23 07:48:31 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
old_stdin_mode = _setmode(STDIN_FILENO, _O_BINARY);
|
|
|
|
if (old_stdin_mode == -1) {
|
|
|
|
fatal_errno("could not set stdin to binary");
|
|
|
|
}
|
|
|
|
#endif
|
2014-05-27 09:30:43 +08:00
|
|
|
}
|
2014-11-18 06:48:25 +08:00
|
|
|
|
adb: win32: fix exec-in and exec-out to use binary mode
adb exec-in and exec-out are designed to read/write binary data
according to the commit description at:
https://android.googlesource.com/platform/system/core/+/5d9d434%5E!/
On Windows, when adb_read and adb_write are used, they are always in
binary mode (because sysdeps_win32.cpp calls Windows APIs direct). But
unix_read, unix_write, fread, fwrite, read, write use the text
translation mode of the C Runtime file descriptor, which is by default
textmode.
adb exec-in and exec-out use copy_to_file() which uses unix_read() and
fwrite() when reading/writing stdin and stdout, thus, copy_to_file()
should switch to binary mode for those cases (it already uses binary
mode for file descriptors other than stdin and stdout).
copy_to_file() is also called by adb backup, adb restore, and adb
install-multiple, but those do not use stdin or stdout, so those
codepaths should not be affected by this change.
Change-Id: I3446d9b363d20a2c2f6be2b96e55b653d99df2f9
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-05-23 07:48:31 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (outFd == STDOUT_FILENO) {
|
|
|
|
old_stdout_mode = _setmode(STDOUT_FILENO, _O_BINARY);
|
|
|
|
if (old_stdout_mode == -1) {
|
|
|
|
fatal_errno("could not set stdout to binary");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-07-07 04:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stdinout_raw_epilogue(int inFd, int outFd, int old_stdin_mode, int old_stdout_mode) {
|
|
|
|
if (inFd == STDIN_FILENO) {
|
|
|
|
stdin_raw_restore();
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (_setmode(STDIN_FILENO, old_stdin_mode) == -1) {
|
|
|
|
fatal_errno("could not restore stdin mode");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (outFd == STDOUT_FILENO) {
|
|
|
|
if (_setmode(STDOUT_FILENO, old_stdout_mode) == -1) {
|
|
|
|
fatal_errno("could not restore stdout mode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_to_file(int inFd, int outFd) {
|
|
|
|
const size_t BUFSIZE = 32 * 1024;
|
|
|
|
char* buf = (char*) malloc(BUFSIZE);
|
|
|
|
if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
|
|
|
|
int len;
|
|
|
|
long total = 0;
|
|
|
|
int old_stdin_mode = -1;
|
|
|
|
int old_stdout_mode = -1;
|
|
|
|
|
|
|
|
D("copy_to_file(%d -> %d)", inFd, outFd);
|
|
|
|
|
|
|
|
stdinout_raw_prologue(inFd, outFd, old_stdin_mode, old_stdout_mode);
|
adb: win32: fix exec-in and exec-out to use binary mode
adb exec-in and exec-out are designed to read/write binary data
according to the commit description at:
https://android.googlesource.com/platform/system/core/+/5d9d434%5E!/
On Windows, when adb_read and adb_write are used, they are always in
binary mode (because sysdeps_win32.cpp calls Windows APIs direct). But
unix_read, unix_write, fread, fwrite, read, write use the text
translation mode of the C Runtime file descriptor, which is by default
textmode.
adb exec-in and exec-out use copy_to_file() which uses unix_read() and
fwrite() when reading/writing stdin and stdout, thus, copy_to_file()
should switch to binary mode for those cases (it already uses binary
mode for file descriptors other than stdin and stdout).
copy_to_file() is also called by adb backup, adb restore, and adb
install-multiple, but those do not use stdin or stdout, so those
codepaths should not be affected by this change.
Change-Id: I3446d9b363d20a2c2f6be2b96e55b653d99df2f9
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-05-23 07:48:31 +08:00
|
|
|
|
2015-04-18 08:03:59 +08:00
|
|
|
while (true) {
|
2014-05-27 09:30:43 +08:00
|
|
|
if (inFd == STDIN_FILENO) {
|
|
|
|
len = unix_read(inFd, buf, BUFSIZE);
|
|
|
|
} else {
|
|
|
|
len = adb_read(inFd, buf, BUFSIZE);
|
|
|
|
}
|
2011-04-22 03:53:28 +08:00
|
|
|
if (len == 0) {
|
2015-09-03 08:44:28 +08:00
|
|
|
D("copy_to_file() : read 0 bytes; exiting");
|
2011-04-22 03:53:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
2015-09-03 08:44:28 +08:00
|
|
|
D("copy_to_file(): read failed: %s", strerror(errno));
|
2011-04-22 03:53:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-27 09:30:43 +08:00
|
|
|
if (outFd == STDOUT_FILENO) {
|
|
|
|
fwrite(buf, 1, len, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
} else {
|
|
|
|
adb_write(outFd, buf, len);
|
|
|
|
}
|
2011-06-02 08:56:23 +08:00
|
|
|
total += len;
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
2014-11-18 06:48:25 +08:00
|
|
|
|
2016-07-07 04:22:22 +08:00
|
|
|
stdinout_raw_epilogue(inFd, outFd, old_stdin_mode, old_stdout_mode);
|
2014-11-18 06:48:25 +08:00
|
|
|
|
2015-09-03 08:44:28 +08:00
|
|
|
D("copy_to_file() finished after %lu bytes", total);
|
2011-06-11 02:38:37 +08:00
|
|
|
free(buf);
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) {
|
|
|
|
// Old devices can't handle window size changes.
|
|
|
|
if (shell == nullptr) return;
|
|
|
|
|
2015-11-08 09:34:39 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
struct winsize {
|
|
|
|
unsigned short ws_row;
|
|
|
|
unsigned short ws_col;
|
|
|
|
unsigned short ws_xpixel;
|
|
|
|
unsigned short ws_ypixel;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
winsize ws;
|
2015-11-08 09:34:39 +08:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// If stdout is redirected to a non-console, we won't be able to get the
|
|
|
|
// console size, but that makes sense.
|
|
|
|
const intptr_t intptr_handle = _get_osfhandle(STDOUT_FILENO);
|
|
|
|
if (intptr_handle == -1) return;
|
|
|
|
|
|
|
|
const HANDLE handle = reinterpret_cast<const HANDLE>(intptr_handle);
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
if (!GetConsoleScreenBufferInfo(handle, &info)) return;
|
|
|
|
|
|
|
|
memset(&ws, 0, sizeof(ws));
|
|
|
|
// The number of visible rows, excluding offscreen scroll-back rows which are in info.dwSize.Y.
|
|
|
|
ws.ws_row = info.srWindow.Bottom - info.srWindow.Top + 1;
|
|
|
|
// If the user has disabled "Wrap text output on resize", they can make the screen buffer wider
|
|
|
|
// than the window, in which case we should use the width of the buffer.
|
|
|
|
ws.ws_col = info.dwSize.X;
|
|
|
|
#else
|
2015-11-04 03:18:40 +08:00
|
|
|
if (ioctl(fd, TIOCGWINSZ, &ws) == -1) return;
|
2015-11-08 09:34:39 +08:00
|
|
|
#endif
|
2015-11-04 03:18:40 +08:00
|
|
|
|
|
|
|
// Send the new window size as human-readable ASCII for debugging convenience.
|
|
|
|
size_t l = snprintf(shell->data(), shell->data_capacity(), "%dx%d,%dx%d",
|
|
|
|
ws.ws_row, ws.ws_col, ws.ws_xpixel, ws.ws_ypixel);
|
|
|
|
shell->Write(ShellProtocol::kIdWindowSizeChange, l + 1);
|
|
|
|
}
|
2015-09-09 08:17:02 +08:00
|
|
|
|
|
|
|
// Used to pass multiple values to the stdin read thread.
|
|
|
|
struct StdinReadArgs {
|
|
|
|
int stdin_fd, write_fd;
|
2015-10-07 06:30:03 +08:00
|
|
|
bool raw_stdin;
|
2015-09-09 08:17:02 +08:00
|
|
|
std::unique_ptr<ShellProtocol> protocol;
|
2015-11-05 05:36:32 +08:00
|
|
|
char escape_char;
|
2015-09-09 08:17:02 +08:00
|
|
|
};
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-09-09 08:17:02 +08:00
|
|
|
// Loops to read from stdin and push the data to the given FD.
|
|
|
|
// The argument should be a pointer to a StdinReadArgs object. This function
|
|
|
|
// will take ownership of the object and delete it when finished.
|
2016-02-13 06:31:15 +08:00
|
|
|
static void stdin_read_thread_loop(void* x) {
|
2015-09-09 08:17:02 +08:00
|
|
|
std::unique_ptr<StdinReadArgs> args(reinterpret_cast<StdinReadArgs*>(x));
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
// Mask SIGTTIN in case we're in a backgrounded process.
|
2015-10-24 06:03:31 +08:00
|
|
|
sigset_t sigset;
|
|
|
|
sigemptyset(&sigset);
|
|
|
|
sigaddset(&sigset, SIGTTIN);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
|
|
|
|
#endif
|
|
|
|
|
2015-11-08 09:34:39 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// _get_interesting_input_record_uncached() causes unix_read_interruptible()
|
|
|
|
// to return -1 with errno == EINTR if the window size changes.
|
|
|
|
#else
|
2015-11-04 03:18:40 +08:00
|
|
|
// Unblock SIGWINCH for this thread, so our read(2) below will be
|
|
|
|
// interrupted if the window size changes.
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGWINCH);
|
|
|
|
pthread_sigmask(SIG_UNBLOCK, &mask, nullptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Set up the initial window size.
|
|
|
|
send_window_size_change(args->stdin_fd, args->protocol);
|
|
|
|
|
2015-11-05 05:36:32 +08:00
|
|
|
char raw_buffer[BUFSIZ];
|
2015-09-09 08:17:02 +08:00
|
|
|
char* buffer_ptr = raw_buffer;
|
|
|
|
size_t buffer_size = sizeof(raw_buffer);
|
2015-11-04 03:18:40 +08:00
|
|
|
if (args->protocol != nullptr) {
|
2015-09-09 08:17:02 +08:00
|
|
|
buffer_ptr = args->protocol->data();
|
|
|
|
buffer_size = args->protocol->data_capacity();
|
|
|
|
}
|
|
|
|
|
2015-11-05 05:36:32 +08:00
|
|
|
// If we need to parse escape sequences, make life easy.
|
|
|
|
if (args->raw_stdin && args->escape_char != '\0') {
|
|
|
|
buffer_size = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum EscapeState { kMidFlow, kStartOfLine, kInEscape };
|
|
|
|
EscapeState state = kStartOfLine;
|
|
|
|
|
2015-08-04 01:38:08 +08:00
|
|
|
while (true) {
|
2015-11-08 09:34:39 +08:00
|
|
|
// Use unix_read_interruptible() rather than adb_read() for stdin.
|
|
|
|
D("stdin_read_thread_loop(): pre unix_read_interruptible(fdi=%d,...)", args->stdin_fd);
|
|
|
|
int r = unix_read_interruptible(args->stdin_fd, buffer_ptr,
|
|
|
|
buffer_size);
|
2015-11-04 03:18:40 +08:00
|
|
|
if (r == -1 && errno == EINTR) {
|
|
|
|
send_window_size_change(args->stdin_fd, args->protocol);
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-08 09:34:39 +08:00
|
|
|
D("stdin_read_thread_loop(): post unix_read_interruptible(fdi=%d,...)", args->stdin_fd);
|
2015-10-07 06:30:03 +08:00
|
|
|
if (r <= 0) {
|
|
|
|
// Only devices using the shell protocol know to close subprocess
|
|
|
|
// stdin. For older devices we want to just leave the connection
|
|
|
|
// open, otherwise an unpredictable amount of return data could
|
|
|
|
// be lost due to the FD closing before all data has been received.
|
|
|
|
if (args->protocol) {
|
|
|
|
args->protocol->Write(ShellProtocol::kIdCloseStdin, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-11-05 05:36:32 +08:00
|
|
|
// If we made stdin raw, check input for escape sequences. In
|
2015-10-07 06:30:03 +08:00
|
|
|
// this situation signals like Ctrl+C are sent remotely rather than
|
|
|
|
// interpreted locally so this provides an emergency out if the remote
|
|
|
|
// process starts ignoring the signal. SSH also does this, see the
|
|
|
|
// "escape characters" section on the ssh man page for more info.
|
2015-11-05 05:36:32 +08:00
|
|
|
if (args->raw_stdin && args->escape_char != '\0') {
|
|
|
|
char ch = buffer_ptr[0];
|
|
|
|
if (ch == args->escape_char) {
|
|
|
|
if (state == kStartOfLine) {
|
|
|
|
state = kInEscape;
|
|
|
|
// Swallow the escape character.
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
state = kMidFlow;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (state == kInEscape) {
|
|
|
|
if (ch == '.') {
|
|
|
|
fprintf(stderr,"\r\n[ disconnected ]\r\n");
|
2015-11-04 03:18:40 +08:00
|
|
|
stdin_raw_restore();
|
2015-10-07 06:30:03 +08:00
|
|
|
exit(0);
|
2015-11-05 05:36:32 +08:00
|
|
|
} else {
|
|
|
|
// We swallowed an escape character that wasn't part of
|
|
|
|
// a valid escape sequence; time to cough it up.
|
|
|
|
buffer_ptr[0] = args->escape_char;
|
|
|
|
buffer_ptr[1] = ch;
|
|
|
|
++r;
|
2015-10-07 06:30:03 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-11-05 05:36:32 +08:00
|
|
|
state = (ch == '\n' || ch == '\r') ? kStartOfLine : kMidFlow;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 08:17:02 +08:00
|
|
|
if (args->protocol) {
|
|
|
|
if (!args->protocol->Write(ShellProtocol::kIdStdin, r)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!WriteFdExactly(args->write_fd, buffer_ptr, r)) {
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 08:17:02 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-10-07 06:30:03 +08:00
|
|
|
// Returns a shell service string with the indicated arguments and command.
|
|
|
|
static std::string ShellServiceString(bool use_shell_protocol,
|
|
|
|
const std::string& type_arg,
|
|
|
|
const std::string& command) {
|
|
|
|
std::vector<std::string> args;
|
|
|
|
if (use_shell_protocol) {
|
|
|
|
args.push_back(kShellServiceArgShellProtocol);
|
2015-11-19 04:45:48 +08:00
|
|
|
|
|
|
|
const char* terminal_type = getenv("TERM");
|
|
|
|
if (terminal_type != nullptr) {
|
|
|
|
args.push_back(std::string("TERM=") + terminal_type);
|
|
|
|
}
|
2015-10-07 06:30:03 +08:00
|
|
|
}
|
|
|
|
if (!type_arg.empty()) {
|
|
|
|
args.push_back(type_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shell service string can look like: shell[,arg1,arg2,...]:[command].
|
|
|
|
return android::base::StringPrintf("shell%s%s:%s",
|
|
|
|
args.empty() ? "" : ",",
|
|
|
|
android::base::Join(args, ',').c_str(),
|
|
|
|
command.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connects to a shell on the device and read/writes data.
|
|
|
|
//
|
|
|
|
// Note: currently this function doesn't properly clean up resources; the
|
|
|
|
// FD connected to the adb server is never closed and the stdin read thread
|
|
|
|
// may never exit.
|
|
|
|
//
|
|
|
|
// On success returns the remote exit code if |use_shell_protocol| is true,
|
|
|
|
// 0 otherwise. On failure returns 1.
|
|
|
|
static int RemoteShell(bool use_shell_protocol, const std::string& type_arg,
|
2015-11-05 05:36:32 +08:00
|
|
|
char escape_char,
|
2015-10-07 06:30:03 +08:00
|
|
|
const std::string& command) {
|
|
|
|
std::string service_string = ShellServiceString(use_shell_protocol,
|
|
|
|
type_arg, command);
|
|
|
|
|
|
|
|
// Make local stdin raw if the device allocates a PTY, which happens if:
|
|
|
|
// 1. We are explicitly asking for a PTY shell, or
|
|
|
|
// 2. We don't specify shell type and are starting an interactive session.
|
|
|
|
bool raw_stdin = (type_arg == kShellServiceArgPty ||
|
|
|
|
(type_arg.empty() && command.empty()));
|
|
|
|
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2015-09-23 01:43:08 +08:00
|
|
|
int fd = adb_connect(service_string, &error);
|
2015-04-29 23:35:59 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr,"error: %s\n", error.c_str());
|
2009-03-04 11:32:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-09 08:17:02 +08:00
|
|
|
StdinReadArgs* args = new StdinReadArgs;
|
|
|
|
if (!args) {
|
|
|
|
LOG(ERROR) << "couldn't allocate StdinReadArgs object";
|
2015-04-22 10:39:52 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-07 06:30:03 +08:00
|
|
|
args->stdin_fd = STDIN_FILENO;
|
2015-09-09 08:17:02 +08:00
|
|
|
args->write_fd = fd;
|
2015-10-07 06:30:03 +08:00
|
|
|
args->raw_stdin = raw_stdin;
|
2015-11-05 05:36:32 +08:00
|
|
|
args->escape_char = escape_char;
|
2015-09-09 08:17:02 +08:00
|
|
|
if (use_shell_protocol) {
|
|
|
|
args->protocol.reset(new ShellProtocol(args->write_fd));
|
|
|
|
}
|
2015-04-22 10:39:52 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
if (raw_stdin) stdin_raw_init();
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
// Ensure our process is notified if the local window size changes.
|
|
|
|
// We use sigaction(2) to ensure that the SA_RESTART flag is not set,
|
|
|
|
// because the whole reason we're sending signals is to unblock the read(2)!
|
|
|
|
// That also means we don't need to do anything in the signal handler:
|
|
|
|
// the side effect of delivering the signal is all we need.
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = [](int) {};
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigaction(SIGWINCH, &sa, nullptr);
|
|
|
|
|
|
|
|
// Now block SIGWINCH in this thread (the main thread) and all threads spawned
|
|
|
|
// from it. The stdin read thread will unblock this signal to ensure that it's
|
|
|
|
// the thread that receives the signal.
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGWINCH);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &mask, nullptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO: combine read_and_dump with stdin_read_thread to make life simpler?
|
|
|
|
int exit_code = 1;
|
|
|
|
if (!adb_thread_create(stdin_read_thread_loop, args)) {
|
2015-09-09 08:17:02 +08:00
|
|
|
PLOG(ERROR) << "error starting stdin read thread";
|
|
|
|
delete args;
|
|
|
|
} else {
|
|
|
|
exit_code = read_and_dump(fd, use_shell_protocol);
|
|
|
|
}
|
2015-05-06 04:41:21 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
// TODO: properly exit stdin_read_thread_loop and close |fd|.
|
2015-10-07 06:30:03 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
// TODO: we should probably install signal handlers for this.
|
|
|
|
// TODO: can we use atexit? even on Windows?
|
|
|
|
if (raw_stdin) stdin_raw_restore();
|
2015-10-07 06:30:03 +08:00
|
|
|
|
2015-09-09 08:17:02 +08:00
|
|
|
return exit_code;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-02-01 11:12:26 +08:00
|
|
|
static int adb_shell(int argc, const char** argv) {
|
2016-01-22 11:56:50 +08:00
|
|
|
FeatureSet features;
|
2016-02-01 11:12:26 +08:00
|
|
|
std::string error;
|
|
|
|
if (!adb_get_feature_set(&features, &error)) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
2016-01-22 11:56:50 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2016-10-20 05:47:11 +08:00
|
|
|
enum PtyAllocationMode { kPtyAuto, kPtyNo, kPtyYes, kPtyDefinitely };
|
|
|
|
|
|
|
|
// Defaults.
|
|
|
|
char escape_char = '~'; // -e
|
|
|
|
bool use_shell_protocol = CanUseFeature(features, kFeatureShell2); // -x
|
|
|
|
PtyAllocationMode tty = use_shell_protocol ? kPtyAuto : kPtyDefinitely; // -t/-T
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
// Parse shell-specific command-line options.
|
2016-10-20 05:47:11 +08:00
|
|
|
argv[0] = "adb shell"; // So getopt(3) error messages start "adb shell".
|
|
|
|
optind = 1; // argv[0] is always "shell", so set `optind` appropriately.
|
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, const_cast<char**>(argv), "+e:ntTx")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'e':
|
|
|
|
if (!(strlen(optarg) == 1 || strcmp(optarg, "none") == 0)) {
|
|
|
|
fprintf(stderr, "error: -e requires a single-character argument or 'none'\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
escape_char = (strcmp(optarg, "none") == 0) ? 0 : optarg[0];
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
close_stdin();
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
// This option basically asks for historical behavior, so set options that
|
|
|
|
// correspond to the historical defaults. This is slightly weird in that -Tx
|
|
|
|
// is fine (because we'll undo the -T) but -xT isn't, but that does seem to
|
|
|
|
// be our least worst choice...
|
|
|
|
use_shell_protocol = false;
|
|
|
|
tty = kPtyDefinitely;
|
|
|
|
escape_char = '~';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
// Like ssh, -t arguments are cumulative so that multiple -t's
|
|
|
|
// are needed to force a PTY.
|
|
|
|
tty = (tty >= kPtyYes) ? kPtyDefinitely : kPtyYes;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
tty = kPtyNo;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// getopt(3) already printed an error message for us.
|
2015-11-05 05:36:32 +08:00
|
|
|
return 1;
|
2015-11-04 03:18:40 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-10-20 05:47:11 +08:00
|
|
|
bool is_interactive = (optind == argc);
|
|
|
|
|
|
|
|
std::string shell_type_arg = kShellServiceArgPty;
|
|
|
|
if (tty == kPtyNo) {
|
|
|
|
shell_type_arg = kShellServiceArgRaw;
|
|
|
|
} else if (tty == kPtyAuto) {
|
|
|
|
// If stdin isn't a TTY, default to a raw shell; this lets
|
|
|
|
// things like `adb shell < my_script.sh` work as expected.
|
|
|
|
// Non-interactive shells should also not have a pty.
|
|
|
|
if (!unix_isatty(STDIN_FILENO) || !is_interactive) {
|
|
|
|
shell_type_arg = kShellServiceArgRaw;
|
|
|
|
}
|
|
|
|
} else if (tty == kPtyYes) {
|
|
|
|
// A single -t arg isn't enough to override implicit -T.
|
|
|
|
if (!unix_isatty(STDIN_FILENO)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Remote PTY will not be allocated because stdin is not a terminal.\n"
|
|
|
|
"Use multiple -t options to force remote PTY allocation.\n");
|
|
|
|
shell_type_arg = kShellServiceArgRaw;
|
2016-01-28 08:07:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 05:47:11 +08:00
|
|
|
D("shell -e 0x%x t=%d use_shell_protocol=%s shell_type_arg=%s\n",
|
|
|
|
escape_char, tty,
|
|
|
|
use_shell_protocol ? "true" : "false",
|
|
|
|
(shell_type_arg == kShellServiceArgPty) ? "pty" : "raw");
|
|
|
|
|
|
|
|
// Raw mode is only supported when talking to a new device *and* using the shell protocol.
|
|
|
|
if (!use_shell_protocol) {
|
|
|
|
if (shell_type_arg != kShellServiceArgPty) {
|
|
|
|
fprintf(stderr, "error: %s only supports allocating a pty\n",
|
|
|
|
!CanUseFeature(features, kFeatureShell2) ? "device" : "-x");
|
|
|
|
return 1;
|
2015-11-04 03:18:40 +08:00
|
|
|
} else {
|
2016-10-20 05:47:11 +08:00
|
|
|
// If we're not using the shell protocol, the type argument must be empty.
|
|
|
|
shell_type_arg = "";
|
2015-11-04 03:18:40 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 01:43:08 +08:00
|
|
|
|
2015-11-04 03:18:40 +08:00
|
|
|
std::string command;
|
2016-10-20 05:47:11 +08:00
|
|
|
if (optind < argc) {
|
2015-11-04 03:18:40 +08:00
|
|
|
// We don't escape here, just like ssh(1). http://b/20564385.
|
2016-10-20 05:47:11 +08:00
|
|
|
command = android::base::Join(std::vector<const char*>(argv + optind, argv + argc), ' ');
|
2015-09-15 06:33:50 +08:00
|
|
|
}
|
2015-11-04 03:18:40 +08:00
|
|
|
|
2015-11-05 05:36:32 +08:00
|
|
|
return RemoteShell(use_shell_protocol, shell_type_arg, escape_char, command);
|
2015-09-15 06:33:50 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 03:28:13 +08:00
|
|
|
static int adb_download_buffer(const char *service, const char *fn, const void* data, unsigned sz,
|
2015-04-29 23:35:59 +08:00
|
|
|
bool show_progress)
|
2012-01-10 06:54:53 +08:00
|
|
|
{
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2015-04-30 03:28:13 +08:00
|
|
|
int fd = adb_connect(android::base::StringPrintf("%s:%d", service, sz), &error);
|
2015-04-29 23:35:59 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr,"error: %s\n", error.c_str());
|
2012-01-10 06:54:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt = CHUNK_SIZE;
|
2015-01-26 06:40:16 +08:00
|
|
|
opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
|
2012-01-10 06:54:53 +08:00
|
|
|
|
2015-04-30 03:28:13 +08:00
|
|
|
unsigned total = sz;
|
2015-02-26 09:51:28 +08:00
|
|
|
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
|
2012-01-10 06:54:53 +08:00
|
|
|
|
2015-04-29 23:35:59 +08:00
|
|
|
if (show_progress) {
|
2015-07-28 12:21:39 +08:00
|
|
|
const char* x = strrchr(service, ':');
|
|
|
|
if (x) service = x + 1;
|
2012-01-10 06:54:53 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 03:28:13 +08:00
|
|
|
while (sz > 0) {
|
2012-01-10 06:54:53 +08:00
|
|
|
unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
|
2015-04-29 23:35:59 +08:00
|
|
|
if (!WriteFdExactly(fd, ptr, xfer)) {
|
|
|
|
std::string error;
|
|
|
|
adb_status(fd, &error);
|
|
|
|
fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 08:32:44 +08:00
|
|
|
adb_close(fd);
|
2012-01-10 06:54:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sz -= xfer;
|
|
|
|
ptr += xfer;
|
2015-04-29 23:35:59 +08:00
|
|
|
if (show_progress) {
|
2013-03-05 14:37:32 +08:00
|
|
|
printf("sending: '%s' %4d%% \r", fn, (int)(100LL - ((100LL * sz) / (total))));
|
2012-01-10 06:54:53 +08:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 23:35:59 +08:00
|
|
|
if (show_progress) {
|
2012-01-10 06:54:53 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2015-05-01 08:32:03 +08:00
|
|
|
if (!adb_status(fd, &error)) {
|
|
|
|
fprintf(stderr,"* error response '%s' *\n", error.c_str());
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 08:32:44 +08:00
|
|
|
adb_close(fd);
|
2012-01-10 06:54:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-27 06:35:36 +08:00
|
|
|
#define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The sideload-host protocol serves the data in a file (given on the
|
|
|
|
* command line) to the client, using a simple protocol:
|
|
|
|
*
|
|
|
|
* - The connect message includes the total number of bytes in the
|
|
|
|
* file and a block size chosen by us.
|
|
|
|
*
|
|
|
|
* - The other side sends the desired block number as eight decimal
|
|
|
|
* digits (eg "00000023" for block 23). Blocks are numbered from
|
|
|
|
* zero.
|
|
|
|
*
|
|
|
|
* - We send back the data of the requested block. The last block is
|
|
|
|
* likely to be partial; when the last block is requested we only
|
|
|
|
* send the part of the block that exists, it's not padded up to the
|
|
|
|
* block size.
|
|
|
|
*
|
|
|
|
* - When the other side sends "DONEDONE" instead of a block number,
|
|
|
|
* we hang up.
|
|
|
|
*/
|
2015-04-18 04:57:15 +08:00
|
|
|
static int adb_sideload_host(const char* fn) {
|
2016-06-16 05:45:34 +08:00
|
|
|
fprintf(stderr, "loading: '%s'...\n", fn);
|
2016-05-27 13:43:19 +08:00
|
|
|
|
|
|
|
std::string content;
|
|
|
|
if (!android::base::ReadFileToString(fn, &content)) {
|
2016-06-16 05:45:34 +08:00
|
|
|
fprintf(stderr, "failed: %s\n", strerror(errno));
|
2014-06-27 06:35:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-27 13:43:19 +08:00
|
|
|
const uint8_t* data = reinterpret_cast<const uint8_t*>(content.data());
|
|
|
|
unsigned sz = content.size();
|
|
|
|
|
2016-06-16 05:45:34 +08:00
|
|
|
fprintf(stderr, "connecting...\n");
|
2015-04-30 03:28:13 +08:00
|
|
|
std::string service =
|
|
|
|
android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2016-05-27 13:43:19 +08:00
|
|
|
unique_fd fd(adb_connect(service, &error));
|
2016-06-14 06:01:13 +08:00
|
|
|
if (fd < 0) {
|
2014-06-27 06:35:36 +08:00
|
|
|
// Try falling back to the older sideload method. Maybe this
|
|
|
|
// is an older device that doesn't support sideload-host.
|
2016-06-16 05:45:34 +08:00
|
|
|
fprintf(stderr, "falling back to older sideload method...\n");
|
2016-05-27 13:43:19 +08:00
|
|
|
return adb_download_buffer("sideload", fn, data, sz, true);
|
2014-06-27 06:35:36 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 13:43:19 +08:00
|
|
|
int opt = SIDELOAD_HOST_BLOCK_SIZE;
|
|
|
|
adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
|
2014-06-27 06:35:36 +08:00
|
|
|
|
2016-05-27 13:43:19 +08:00
|
|
|
size_t xfer = 0;
|
|
|
|
int last_percent = -1;
|
2015-04-18 08:03:59 +08:00
|
|
|
while (true) {
|
2015-04-30 03:28:13 +08:00
|
|
|
char buf[9];
|
2015-02-25 13:26:58 +08:00
|
|
|
if (!ReadFdExactly(fd, buf, 8)) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
|
2016-05-27 13:43:19 +08:00
|
|
|
return -1;
|
2014-06-27 06:35:36 +08:00
|
|
|
}
|
2015-04-30 03:28:13 +08:00
|
|
|
buf[8] = '\0';
|
2014-06-27 06:35:36 +08:00
|
|
|
|
2015-04-30 03:28:13 +08:00
|
|
|
if (strcmp("DONEDONE", buf) == 0) {
|
2016-05-27 13:43:19 +08:00
|
|
|
printf("\rTotal xfer: %.2fx%*s\n",
|
|
|
|
(double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
|
|
|
|
return 0;
|
2014-06-27 06:35:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int block = strtol(buf, NULL, 10);
|
|
|
|
|
|
|
|
size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
|
|
|
|
if (offset >= sz) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "* attempt to read block %d past end\n", block);
|
2016-05-27 13:43:19 +08:00
|
|
|
return -1;
|
2014-06-27 06:35:36 +08:00
|
|
|
}
|
2016-05-27 13:43:19 +08:00
|
|
|
const uint8_t* start = data + offset;
|
2014-06-27 06:35:36 +08:00
|
|
|
size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE;
|
|
|
|
size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
|
|
|
|
if (offset_end > sz) {
|
|
|
|
to_write = sz - offset;
|
|
|
|
}
|
|
|
|
|
2016-05-27 13:43:19 +08:00
|
|
|
if (!WriteFdExactly(fd, start, to_write)) {
|
2015-04-29 23:35:59 +08:00
|
|
|
adb_status(fd, &error);
|
|
|
|
fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
|
2016-05-27 13:43:19 +08:00
|
|
|
return -1;
|
2014-06-27 06:35:36 +08:00
|
|
|
}
|
|
|
|
xfer += to_write;
|
|
|
|
|
|
|
|
// For normal OTA packages, we expect to transfer every byte
|
|
|
|
// twice, plus a bit of overhead (one read during
|
|
|
|
// verification, one read of each byte for installation, plus
|
|
|
|
// extra access to things like the zip central directory).
|
|
|
|
// This estimate of the completion becomes 100% when we've
|
|
|
|
// transferred ~2.13 (=100/47) times the package size.
|
|
|
|
int percent = (int)(xfer * 47LL / (sz ? sz : 1));
|
|
|
|
if (percent != last_percent) {
|
|
|
|
printf("\rserving: '%s' (~%d%%) ", fn, percent);
|
|
|
|
fflush(stdout);
|
|
|
|
last_percent = percent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
/**
|
|
|
|
* Run ppp in "notty" mode against a resource listed as the first parameter
|
|
|
|
* eg:
|
|
|
|
*
|
|
|
|
* ppp dev:/dev/omap_csmi_tty0 <ppp options>
|
|
|
|
*
|
|
|
|
*/
|
2015-04-18 04:57:15 +08:00
|
|
|
static int ppp(int argc, const char** argv) {
|
2014-11-12 01:24:11 +08:00
|
|
|
#if defined(_WIN32)
|
2009-03-04 11:32:55 +08:00
|
|
|
fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
|
|
|
|
argv[0]);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
const char* adb_service_name = argv[1];
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect(adb_service_name, &error);
|
|
|
|
if (fd < 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
|
2015-04-29 23:35:59 +08:00
|
|
|
adb_service_name, error.c_str());
|
2009-03-04 11:32:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-29 23:35:59 +08:00
|
|
|
pid_t pid = fork();
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
perror("from fork()");
|
|
|
|
return 1;
|
|
|
|
} else if (pid == 0) {
|
|
|
|
int err;
|
|
|
|
int i;
|
|
|
|
const char **ppp_args;
|
|
|
|
|
|
|
|
// copy args
|
|
|
|
ppp_args = (const char **) alloca(sizeof(char *) * argc + 1);
|
|
|
|
ppp_args[0] = "pppd";
|
|
|
|
for (i = 2 ; i < argc ; i++) {
|
|
|
|
//argv[2] and beyond become ppp_args[1] and beyond
|
|
|
|
ppp_args[i - 1] = argv[i];
|
|
|
|
}
|
|
|
|
ppp_args[i-1] = NULL;
|
|
|
|
|
|
|
|
// child side
|
|
|
|
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
adb_close(STDERR_FILENO);
|
|
|
|
adb_close(fd);
|
|
|
|
|
|
|
|
err = execvp("pppd", (char * const *)ppp_args);
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
perror("execing pppd");
|
|
|
|
}
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
|
|
|
// parent side
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-12 01:24:11 +08:00
|
|
|
#endif /* !defined(_WIN32) */
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 03:14:16 +08:00
|
|
|
static bool wait_for_device(const char* service, TransportType t, const char* serial) {
|
|
|
|
std::vector<std::string> components = android::base::Split(service, "-");
|
|
|
|
if (components.size() < 3 || components.size() > 4) {
|
2015-11-28 01:56:48 +08:00
|
|
|
fprintf(stderr, "adb: couldn't parse 'wait-for' command: %s\n", service);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:29:32 +08:00
|
|
|
// Was the caller vague about what they'd like us to wait for?
|
|
|
|
// If so, check they weren't more specific in their choice of transport type.
|
2016-04-14 03:14:16 +08:00
|
|
|
if (components.size() == 3) {
|
|
|
|
auto it = components.begin() + 2;
|
2015-05-05 10:29:32 +08:00
|
|
|
if (t == kTransportUsb) {
|
2016-04-14 03:14:16 +08:00
|
|
|
components.insert(it, "usb");
|
2015-05-05 10:29:32 +08:00
|
|
|
} else if (t == kTransportLocal) {
|
2016-04-14 03:14:16 +08:00
|
|
|
components.insert(it, "local");
|
2015-05-05 10:29:32 +08:00
|
|
|
} else {
|
2016-04-14 03:14:16 +08:00
|
|
|
components.insert(it, "any");
|
2015-05-05 10:29:32 +08:00
|
|
|
}
|
2016-04-14 03:14:16 +08:00
|
|
|
} else if (components[2] != "any" && components[2] != "local" && components[2] != "usb") {
|
|
|
|
fprintf(stderr, "adb: unknown type %s; expected 'any', 'local', or 'usb'\n",
|
|
|
|
components[2].c_str());
|
|
|
|
return false;
|
2015-05-05 10:29:32 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 03:18:58 +08:00
|
|
|
if (components[3] != "any" && components[3] != "bootloader" && components[3] != "device" &&
|
|
|
|
components[3] != "recovery" && components[3] != "sideload") {
|
2016-04-14 03:14:16 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"adb: unknown state %s; "
|
2016-04-14 03:18:58 +08:00
|
|
|
"expected 'any', 'bootloader', 'device', 'recovery', or 'sideload'\n",
|
2016-04-14 03:14:16 +08:00
|
|
|
components[3].c_str());
|
2015-11-28 01:56:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-14 03:14:16 +08:00
|
|
|
std::string cmd = format_host_command(android::base::Join(components, "-").c_str(), t, serial);
|
2015-05-30 08:55:19 +08:00
|
|
|
return adb_command(cmd);
|
2015-05-05 10:29:32 +08:00
|
|
|
}
|
|
|
|
|
2016-03-05 07:16:18 +08:00
|
|
|
static bool adb_root(const char* command) {
|
|
|
|
std::string error;
|
|
|
|
|
2016-05-27 13:43:19 +08:00
|
|
|
unique_fd fd(adb_connect(android::base::StringPrintf("%s:", command), &error));
|
|
|
|
if (fd < 0) {
|
2016-03-05 07:16:18 +08:00
|
|
|
fprintf(stderr, "adb: unable to connect for %s: %s\n", command, error.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out whether we actually did anything.
|
|
|
|
char buf[256];
|
|
|
|
char* cur = buf;
|
|
|
|
ssize_t bytes_left = sizeof(buf);
|
|
|
|
while (bytes_left > 0) {
|
2016-05-27 13:43:19 +08:00
|
|
|
ssize_t bytes_read = adb_read(fd, cur, bytes_left);
|
2016-03-05 07:16:18 +08:00
|
|
|
if (bytes_read == 0) {
|
|
|
|
break;
|
|
|
|
} else if (bytes_read < 0) {
|
|
|
|
fprintf(stderr, "adb: error while reading for %s: %s\n", command, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cur += bytes_read;
|
|
|
|
bytes_left -= bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_left == 0) {
|
|
|
|
fprintf(stderr, "adb: unexpected output length for %s\n", command);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
WriteFdExactly(STDOUT_FILENO, buf, sizeof(buf) - bytes_left);
|
|
|
|
if (cur != buf && strstr(buf, "restarting") == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-17 05:00:09 +08:00
|
|
|
// Give adbd some time to kill itself and come back up.
|
|
|
|
// We can't use wait-for-device because devices (e.g. adb over network) might not come back.
|
2016-11-16 10:55:47 +08:00
|
|
|
std::this_thread::sleep_for(3s);
|
2016-06-17 05:00:09 +08:00
|
|
|
return true;
|
2016-03-05 07:16:18 +08:00
|
|
|
}
|
|
|
|
|
2016-07-20 08:07:22 +08:00
|
|
|
int send_shell_command(TransportType transport_type, const char* serial, const std::string& command,
|
2016-07-27 03:14:39 +08:00
|
|
|
bool disable_shell_protocol, StandardStreamsCallbackInterface* callback) {
|
2016-01-22 11:56:50 +08:00
|
|
|
int fd;
|
2015-10-01 04:35:42 +08:00
|
|
|
bool use_shell_protocol = false;
|
|
|
|
|
2015-04-18 01:59:34 +08:00
|
|
|
while (true) {
|
2016-01-22 11:56:50 +08:00
|
|
|
bool attempt_connection = true;
|
|
|
|
|
2016-07-27 03:14:39 +08:00
|
|
|
// Use shell protocol if it's supported and the caller doesn't explicitly
|
|
|
|
// disable it.
|
2016-01-22 11:56:50 +08:00
|
|
|
if (!disable_shell_protocol) {
|
|
|
|
FeatureSet features;
|
2016-02-01 11:12:26 +08:00
|
|
|
std::string error;
|
|
|
|
if (adb_get_feature_set(&features, &error)) {
|
2016-01-22 11:56:50 +08:00
|
|
|
use_shell_protocol = CanUseFeature(features, kFeatureShell2);
|
|
|
|
} else {
|
|
|
|
// Device was unreachable.
|
|
|
|
attempt_connection = false;
|
|
|
|
}
|
2015-04-29 23:35:59 +08:00
|
|
|
}
|
2016-01-22 11:56:50 +08:00
|
|
|
|
|
|
|
if (attempt_connection) {
|
|
|
|
std::string error;
|
|
|
|
std::string service_string = ShellServiceString(use_shell_protocol, "", command);
|
|
|
|
|
|
|
|
fd = adb_connect(service_string, &error);
|
|
|
|
if (fd >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 03:14:39 +08:00
|
|
|
fprintf(stderr, "- waiting for device -\n");
|
2016-02-25 07:16:17 +08:00
|
|
|
if (!wait_for_device("wait-for-device", transport_type, serial)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 03:14:39 +08:00
|
|
|
int exit_code = read_and_dump(fd, use_shell_protocol, callback);
|
2015-09-15 06:33:50 +08:00
|
|
|
|
|
|
|
if (adb_close(fd) < 0) {
|
|
|
|
PLOG(ERROR) << "failure closing FD " << fd;
|
2015-04-18 01:59:34 +08:00
|
|
|
}
|
2015-09-15 06:33:50 +08:00
|
|
|
|
|
|
|
return exit_code;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-05-06 04:10:43 +08:00
|
|
|
static int logcat(TransportType transport, const char* serial, int argc, const char** argv) {
|
2015-04-18 01:59:34 +08:00
|
|
|
char* log_tags = getenv("ANDROID_LOG_TAGS");
|
|
|
|
std::string quoted = escape_arg(log_tags == nullptr ? "" : log_tags);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-10-01 04:35:42 +08:00
|
|
|
std::string cmd = "export ANDROID_LOG_TAGS=\"" + quoted + "\"; exec logcat";
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-06-11 02:31:24 +08:00
|
|
|
if (!strcmp(argv[0], "longcat")) {
|
2015-04-18 01:59:34 +08:00
|
|
|
cmd += " -v long";
|
2011-12-01 05:00:33 +08:00
|
|
|
}
|
|
|
|
|
2015-04-18 11:11:08 +08:00
|
|
|
--argc;
|
|
|
|
++argv;
|
2015-04-18 01:59:34 +08:00
|
|
|
while (argc-- > 0) {
|
2015-04-18 11:11:08 +08:00
|
|
|
cmd += " " + escape_arg(*argv++);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 04:35:42 +08:00
|
|
|
// No need for shell protocol with logcat, always disable for simplicity.
|
|
|
|
return send_shell_command(transport, serial, cmd, true);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:22:22 +08:00
|
|
|
static void write_zeros(int bytes, int fd) {
|
|
|
|
int old_stdin_mode = -1;
|
|
|
|
int old_stdout_mode = -1;
|
|
|
|
char* buf = (char*) calloc(1, bytes);
|
|
|
|
if (buf == nullptr) fatal("couldn't allocate buffer for write_zeros");
|
|
|
|
|
|
|
|
D("write_zeros(%d) -> %d", bytes, fd);
|
|
|
|
|
|
|
|
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
|
|
|
|
|
|
|
|
if (fd == STDOUT_FILENO) {
|
|
|
|
fwrite(buf, 1, bytes, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
} else {
|
|
|
|
adb_write(fd, buf, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
|
|
|
|
|
|
|
|
D("write_zeros() finished");
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
static int backup(int argc, const char** argv) {
|
2015-08-22 11:31:31 +08:00
|
|
|
const char* filename = "backup.ab";
|
2011-04-22 03:53:28 +08:00
|
|
|
|
2011-06-02 08:56:23 +08:00
|
|
|
/* find, extract, and use any -f argument */
|
2015-04-18 11:11:08 +08:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2011-06-02 08:56:23 +08:00
|
|
|
if (!strcmp("-f", argv[i])) {
|
|
|
|
if (i == argc-1) {
|
2015-11-14 03:04:10 +08:00
|
|
|
fprintf(stderr, "adb: backup -f passed with no filename.\n");
|
|
|
|
return EXIT_FAILURE;
|
2011-06-02 08:56:23 +08:00
|
|
|
}
|
|
|
|
filename = argv[i+1];
|
2015-04-18 11:11:08 +08:00
|
|
|
for (int j = i+2; j <= argc; ) {
|
2011-06-02 08:56:23 +08:00
|
|
|
argv[i++] = argv[j++];
|
|
|
|
}
|
|
|
|
argc -= 2;
|
|
|
|
argv[argc] = NULL;
|
|
|
|
}
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-14 03:04:10 +08:00
|
|
|
// Bare "adb backup" or "adb backup -f filename" are not valid invocations ---
|
|
|
|
// a list of packages is required.
|
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "adb: backup either needs a list of packages or -all/-shared.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2011-08-23 08:12:08 +08:00
|
|
|
|
2011-12-09 11:04:34 +08:00
|
|
|
adb_unlink(filename);
|
2015-04-18 11:11:08 +08:00
|
|
|
int outFd = adb_creat(filename, 0640);
|
2011-04-22 03:53:28 +08:00
|
|
|
if (outFd < 0) {
|
2015-11-14 03:04:10 +08:00
|
|
|
fprintf(stderr, "adb: backup unable to create file '%s': %s\n", filename, strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
|
|
|
|
2015-04-18 11:11:08 +08:00
|
|
|
std::string cmd = "backup:";
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
while (argc-- > 0) {
|
|
|
|
cmd += " " + escape_arg(*argv++);
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
|
|
|
|
2015-09-03 08:44:28 +08:00
|
|
|
D("backup. filename=%s cmd=%s", filename, cmd.c_str());
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2015-04-30 03:28:13 +08:00
|
|
|
int fd = adb_connect(cmd, &error);
|
2011-04-22 03:53:28 +08:00
|
|
|
if (fd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "adb: unable to connect for backup: %s\n", error.c_str());
|
2011-04-22 03:53:28 +08:00
|
|
|
adb_close(outFd);
|
2015-11-14 03:04:10 +08:00
|
|
|
return EXIT_FAILURE;
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-14 03:04:10 +08:00
|
|
|
printf("Now unlock your device and confirm the backup operation...\n");
|
|
|
|
fflush(stdout);
|
|
|
|
|
2011-04-22 03:53:28 +08:00
|
|
|
copy_to_file(fd, outFd);
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
adb_close(outFd);
|
2015-11-14 03:04:10 +08:00
|
|
|
return EXIT_SUCCESS;
|
2011-04-22 03:53:28 +08:00
|
|
|
}
|
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
static int restore(int argc, const char** argv) {
|
2011-05-18 06:52:54 +08:00
|
|
|
if (argc != 2) return usage();
|
|
|
|
|
2015-04-29 23:35:59 +08:00
|
|
|
const char* filename = argv[1];
|
|
|
|
int tarFd = adb_open(filename, O_RDONLY);
|
2011-05-18 06:52:54 +08:00
|
|
|
if (tarFd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "adb: unable to open file %s: %s\n", filename, strerror(errno));
|
2011-05-18 06:52:54 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect("restore:", &error);
|
2011-05-18 06:52:54 +08:00
|
|
|
if (fd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "adb: unable to connect for restore: %s\n", error.c_str());
|
2011-05-18 06:52:54 +08:00
|
|
|
adb_close(tarFd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-07 07:43:03 +08:00
|
|
|
printf("Now unlock your device and confirm the restore operation.\n");
|
2011-05-18 06:52:54 +08:00
|
|
|
copy_to_file(tarFd, fd);
|
|
|
|
|
2016-07-07 04:22:22 +08:00
|
|
|
// Provide an in-band EOD marker in case the archive file is malformed
|
|
|
|
write_zeros(512*2, fd);
|
|
|
|
|
2016-03-05 07:51:03 +08:00
|
|
|
// Wait until the other side finishes, or it'll get sent SIGHUP.
|
|
|
|
copy_to_file(fd, STDOUT_FILENO);
|
|
|
|
|
2011-05-18 06:52:54 +08:00
|
|
|
adb_close(fd);
|
|
|
|
adb_close(tarFd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
/* <hint> may be:
|
|
|
|
* - A simple product name
|
|
|
|
* e.g., "sooner"
|
|
|
|
* - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir
|
|
|
|
* e.g., "out/target/product/sooner"
|
|
|
|
* - An absolute path to the PRODUCT_OUT dir
|
|
|
|
* e.g., "/src/device/out/target/product/sooner"
|
|
|
|
*
|
|
|
|
* Given <hint>, try to construct an absolute path to the
|
|
|
|
* ANDROID_PRODUCT_OUT dir.
|
|
|
|
*/
|
2015-07-31 08:42:01 +08:00
|
|
|
static std::string find_product_out_path(const std::string& hint) {
|
|
|
|
if (hint.empty()) {
|
2015-04-18 04:57:15 +08:00
|
|
|
return "";
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-18 04:57:15 +08:00
|
|
|
// If it's already absolute, don't bother doing any work.
|
2015-07-31 08:42:01 +08:00
|
|
|
if (adb_is_absolute_host_path(hint.c_str())) {
|
2015-04-18 04:57:15 +08:00
|
|
|
return hint;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-07-28 07:25:51 +08:00
|
|
|
// If any of the OS_PATH_SEPARATORS is found, assume it's a relative path;
|
2015-04-18 04:57:15 +08:00
|
|
|
// make it absolute.
|
2016-07-28 07:25:51 +08:00
|
|
|
// NOLINT: Do not complain if OS_PATH_SEPARATORS has only one character.
|
|
|
|
if (hint.find_first_of(OS_PATH_SEPARATORS) != std::string::npos) { // NOLINT
|
2015-04-18 08:03:59 +08:00
|
|
|
std::string cwd;
|
|
|
|
if (!getcwd(&cwd)) {
|
2016-07-30 08:57:00 +08:00
|
|
|
perror("adb: getcwd failed");
|
2015-04-18 04:57:15 +08:00
|
|
|
return "";
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-07-31 08:42:01 +08:00
|
|
|
return android::base::StringPrintf("%s%c%s", cwd.c_str(), OS_PATH_SEPARATOR, hint.c_str());
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-18 04:57:15 +08:00
|
|
|
// It's a string without any slashes. Try to do something with it.
|
|
|
|
//
|
|
|
|
// Try to find the root of the build tree, and build a PRODUCT_OUT
|
|
|
|
// path from there.
|
2015-04-18 08:03:59 +08:00
|
|
|
char* top = getenv("ANDROID_BUILD_TOP");
|
2015-04-18 04:57:15 +08:00
|
|
|
if (top == nullptr) {
|
2015-04-18 08:03:59 +08:00
|
|
|
fprintf(stderr, "adb: ANDROID_BUILD_TOP not set!\n");
|
2015-04-18 04:57:15 +08:00
|
|
|
return "";
|
|
|
|
}
|
2015-04-18 08:03:59 +08:00
|
|
|
|
|
|
|
std::string path = top;
|
2015-04-18 04:57:15 +08:00
|
|
|
path += OS_PATH_SEPARATOR_STR;
|
|
|
|
path += "out";
|
|
|
|
path += OS_PATH_SEPARATOR_STR;
|
|
|
|
path += "target";
|
|
|
|
path += OS_PATH_SEPARATOR_STR;
|
|
|
|
path += "product";
|
|
|
|
path += OS_PATH_SEPARATOR_STR;
|
|
|
|
path += hint;
|
|
|
|
if (!directory_exists(path)) {
|
|
|
|
fprintf(stderr, "adb: Couldn't find a product dir based on -p %s; "
|
2015-07-31 08:42:01 +08:00
|
|
|
"\"%s\" doesn't exist\n", hint.c_str(), path.c_str());
|
2015-04-18 08:03:59 +08:00
|
|
|
return "";
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-04-18 04:57:15 +08:00
|
|
|
return path;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-10-31 07:57:19 +08:00
|
|
|
static void parse_push_pull_args(const char** arg, int narg,
|
|
|
|
std::vector<const char*>* srcs,
|
|
|
|
const char** dst, bool* copy_attrs) {
|
|
|
|
*copy_attrs = false;
|
2014-03-12 08:55:59 +08:00
|
|
|
|
2015-10-31 07:57:19 +08:00
|
|
|
srcs->clear();
|
|
|
|
bool ignore_flags = false;
|
2013-04-20 03:41:09 +08:00
|
|
|
while (narg > 0) {
|
2015-10-31 07:57:19 +08:00
|
|
|
if (ignore_flags || *arg[0] != '-') {
|
|
|
|
srcs->push_back(*arg);
|
2013-04-20 03:41:09 +08:00
|
|
|
} else {
|
2015-10-31 07:57:19 +08:00
|
|
|
if (!strcmp(*arg, "-p")) {
|
|
|
|
// Silently ignore for backwards compatibility.
|
|
|
|
} else if (!strcmp(*arg, "-a")) {
|
|
|
|
*copy_attrs = true;
|
|
|
|
} else if (!strcmp(*arg, "--")) {
|
|
|
|
ignore_flags = true;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "adb: unrecognized option '%s'\n", *arg);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-04-20 03:41:09 +08:00
|
|
|
}
|
2014-03-12 08:55:59 +08:00
|
|
|
++arg;
|
|
|
|
--narg;
|
|
|
|
}
|
|
|
|
|
2015-10-31 07:57:19 +08:00
|
|
|
if (srcs->size() > 1) {
|
|
|
|
*dst = srcs->back();
|
|
|
|
srcs->pop_back();
|
2014-03-12 08:55:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 03:28:13 +08:00
|
|
|
static int adb_connect_command(const std::string& command) {
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect(command, &error);
|
2015-05-08 14:37:40 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
return 1;
|
2015-03-30 02:22:34 +08:00
|
|
|
}
|
2015-05-08 14:37:40 +08:00
|
|
|
read_and_dump(fd);
|
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
2015-03-30 02:22:34 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 03:28:13 +08:00
|
|
|
static int adb_query_command(const std::string& command) {
|
|
|
|
std::string result;
|
|
|
|
std::string error;
|
|
|
|
if (!adb_query(command, &result, &error)) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("%s\n", result.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-08 07:20:13 +08:00
|
|
|
// Disallow stdin, stdout, and stderr.
|
|
|
|
static bool _is_valid_ack_reply_fd(const int ack_reply_fd) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
|
|
|
|
return (GetStdHandle(STD_INPUT_HANDLE) != ack_reply_handle) &&
|
|
|
|
(GetStdHandle(STD_OUTPUT_HANDLE) != ack_reply_handle) &&
|
|
|
|
(GetStdHandle(STD_ERROR_HANDLE) != ack_reply_handle);
|
|
|
|
#else
|
|
|
|
return ack_reply_fd > 2;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:22:37 +08:00
|
|
|
static bool _use_legacy_install() {
|
|
|
|
FeatureSet features;
|
|
|
|
std::string error;
|
|
|
|
if (!adb_get_feature_set(&features, &error)) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !CanUseFeature(features, kFeatureCmd);
|
|
|
|
}
|
|
|
|
|
2016-10-20 05:47:11 +08:00
|
|
|
int adb_commandline(int argc, const char** argv) {
|
2009-03-04 11:32:55 +08:00
|
|
|
int no_daemon = 0;
|
|
|
|
int is_daemon = 0;
|
2011-01-31 21:23:56 +08:00
|
|
|
int is_server = 0;
|
2009-03-04 11:32:55 +08:00
|
|
|
int r;
|
2015-05-06 04:10:43 +08:00
|
|
|
TransportType transport_type = kTransportAny;
|
2015-08-08 01:10:29 +08:00
|
|
|
int ack_reply_fd = -1;
|
|
|
|
|
2015-11-19 10:07:48 +08:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
// We'd rather have EPIPE than SIGPIPE.
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
|
2015-04-18 04:57:15 +08:00
|
|
|
// If defined, this should be an absolute path to
|
|
|
|
// the directory containing all of the various system images
|
|
|
|
// for a particular product. If not defined, and the adb
|
|
|
|
// command requires this information, then the user must
|
|
|
|
// specify the path using "-p".
|
|
|
|
char* ANDROID_PRODUCT_OUT = getenv("ANDROID_PRODUCT_OUT");
|
|
|
|
if (ANDROID_PRODUCT_OUT != nullptr) {
|
|
|
|
gProductOutPath = ANDROID_PRODUCT_OUT;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
// TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
|
|
|
|
|
2016-08-26 07:00:22 +08:00
|
|
|
const char* server_host_str = nullptr;
|
|
|
|
const char* server_port_str = nullptr;
|
|
|
|
const char* server_socket_str = nullptr;
|
2010-04-19 19:21:12 +08:00
|
|
|
|
2015-10-08 05:55:10 +08:00
|
|
|
// We need to check for -d and -e before we look at $ANDROID_SERIAL.
|
|
|
|
const char* serial = nullptr;
|
|
|
|
|
2014-12-06 09:37:24 +08:00
|
|
|
while (argc > 0) {
|
|
|
|
if (!strcmp(argv[0],"server")) {
|
2011-01-31 21:23:56 +08:00
|
|
|
is_server = 1;
|
2014-12-06 09:37:24 +08:00
|
|
|
} else if (!strcmp(argv[0],"nodaemon")) {
|
2009-03-04 11:32:55 +08:00
|
|
|
no_daemon = 1;
|
|
|
|
} else if (!strcmp(argv[0], "fork-server")) {
|
|
|
|
/* this is a special flag used only when the ADB client launches the ADB Server */
|
|
|
|
is_daemon = 1;
|
2015-08-08 01:10:29 +08:00
|
|
|
} else if (!strcmp(argv[0], "--reply-fd")) {
|
|
|
|
if (argc < 2) return usage();
|
|
|
|
const char* reply_fd_str = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
ack_reply_fd = strtol(reply_fd_str, nullptr, 10);
|
2015-09-08 07:20:13 +08:00
|
|
|
if (!_is_valid_ack_reply_fd(ack_reply_fd)) {
|
2015-08-08 01:10:29 +08:00
|
|
|
fprintf(stderr, "adb: invalid reply fd \"%s\"\n", reply_fd_str);
|
|
|
|
return usage();
|
|
|
|
}
|
2014-12-06 09:37:24 +08:00
|
|
|
} else if (!strncmp(argv[0], "-p", 2)) {
|
2015-08-01 04:18:22 +08:00
|
|
|
const char* product = nullptr;
|
2009-03-04 11:32:55 +08:00
|
|
|
if (argv[0][2] == '\0') {
|
|
|
|
if (argc < 2) return usage();
|
|
|
|
product = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
} else {
|
2012-08-05 07:40:50 +08:00
|
|
|
product = argv[0] + 2;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-08-01 04:18:22 +08:00
|
|
|
gProductOutPath = find_product_out_path(product);
|
2015-04-18 04:57:15 +08:00
|
|
|
if (gProductOutPath.empty()) {
|
|
|
|
fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product);
|
2009-03-04 11:32:55 +08:00
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
} else if (argv[0][0]=='-' && argv[0][1]=='s') {
|
|
|
|
if (isdigit(argv[0][2])) {
|
|
|
|
serial = argv[0] + 2;
|
|
|
|
} else {
|
2014-12-06 09:37:24 +08:00
|
|
|
if (argc < 2 || argv[0][2] != '\0') return usage();
|
2009-03-04 11:32:55 +08:00
|
|
|
serial = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[0],"-d")) {
|
2015-05-06 04:10:43 +08:00
|
|
|
transport_type = kTransportUsb;
|
2009-03-04 11:32:55 +08:00
|
|
|
} else if (!strcmp(argv[0],"-e")) {
|
2015-05-06 04:10:43 +08:00
|
|
|
transport_type = kTransportLocal;
|
2012-11-15 02:16:17 +08:00
|
|
|
} else if (!strcmp(argv[0],"-a")) {
|
|
|
|
gListenAll = 1;
|
2014-12-06 09:37:24 +08:00
|
|
|
} else if (!strncmp(argv[0], "-H", 2)) {
|
2012-11-15 02:16:17 +08:00
|
|
|
if (argv[0][2] == '\0') {
|
|
|
|
if (argc < 2) return usage();
|
2016-08-26 07:00:22 +08:00
|
|
|
server_host_str = argv[1];
|
2012-11-15 02:16:17 +08:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
} else {
|
2016-08-26 07:00:22 +08:00
|
|
|
server_host_str = argv[0] + 2;
|
2012-11-15 02:16:17 +08:00
|
|
|
}
|
2014-12-06 09:37:24 +08:00
|
|
|
} else if (!strncmp(argv[0], "-P", 2)) {
|
2012-11-15 02:16:17 +08:00
|
|
|
if (argv[0][2] == '\0') {
|
|
|
|
if (argc < 2) return usage();
|
|
|
|
server_port_str = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
} else {
|
|
|
|
server_port_str = argv[0] + 2;
|
|
|
|
}
|
2016-08-26 07:00:22 +08:00
|
|
|
} else if (!strcmp(argv[0], "-L")) {
|
|
|
|
if (argc < 2) return usage();
|
|
|
|
server_socket_str = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
2016-08-26 07:00:22 +08:00
|
|
|
/* out of recognized modifiers and flags */
|
2009-03-04 11:32:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
2016-08-26 07:00:22 +08:00
|
|
|
if ((server_host_str || server_port_str) && server_socket_str) {
|
|
|
|
fprintf(stderr, "adb: -L is incompatible with -H or -P\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If -L, -H, or -P are specified, ignore environment variables.
|
|
|
|
// Otherwise, prefer ADB_SERVER_SOCKET over ANDROID_ADB_SERVER_ADDRESS/PORT.
|
2016-09-17 04:01:24 +08:00
|
|
|
if (!server_host_str && !server_port_str && !server_socket_str) {
|
|
|
|
server_socket_str = getenv("ADB_SERVER_SOCKET");
|
2016-08-26 07:00:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!server_socket_str) {
|
|
|
|
// tcp:1234 and tcp:localhost:1234 are different with -a, so don't default to localhost
|
|
|
|
server_host_str = server_host_str ? server_host_str : getenv("ANDROID_ADB_SERVER_ADDRESS");
|
|
|
|
|
2016-09-17 04:01:24 +08:00
|
|
|
int server_port = DEFAULT_ADB_PORT;
|
2016-08-26 07:00:22 +08:00
|
|
|
server_port_str = server_port_str ? server_port_str : getenv("ANDROID_ADB_SERVER_PORT");
|
2016-09-17 04:01:24 +08:00
|
|
|
if (server_port_str && strlen(server_port_str) > 0) {
|
|
|
|
if (!android::base::ParseInt(server_port_str, &server_port, 1, 65535)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"adb: Env var ANDROID_ADB_SERVER_PORT must be a positive"
|
|
|
|
" number less than 65535. Got \"%s\"\n",
|
|
|
|
server_port_str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 07:00:22 +08:00
|
|
|
|
|
|
|
int rc;
|
|
|
|
char* temp;
|
|
|
|
if (server_host_str) {
|
2016-09-17 04:01:24 +08:00
|
|
|
rc = asprintf(&temp, "tcp:%s:%d", server_host_str, server_port);
|
2016-08-26 07:00:22 +08:00
|
|
|
} else {
|
2016-09-17 04:01:24 +08:00
|
|
|
rc = asprintf(&temp, "tcp:%d", server_port);
|
2016-08-26 07:00:22 +08:00
|
|
|
}
|
|
|
|
if (rc < 0) {
|
|
|
|
fatal("failed to allocate server socket specification");
|
|
|
|
}
|
|
|
|
server_socket_str = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
adb_set_socket_spec(server_socket_str);
|
|
|
|
|
2015-10-08 05:55:10 +08:00
|
|
|
// If none of -d, -e, or -s were specified, try $ANDROID_SERIAL.
|
|
|
|
if (transport_type == kTransportAny && serial == nullptr) {
|
|
|
|
serial = getenv("ANDROID_SERIAL");
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:10:43 +08:00
|
|
|
adb_set_transport(transport_type, serial);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2011-01-31 21:23:56 +08:00
|
|
|
if (is_server) {
|
2009-03-04 11:32:55 +08:00
|
|
|
if (no_daemon || is_daemon) {
|
2015-08-09 06:07:07 +08:00
|
|
|
if (is_daemon && (ack_reply_fd == -1)) {
|
2015-08-08 01:10:29 +08:00
|
|
|
fprintf(stderr, "reply fd for adb server to client communication not specified.\n");
|
|
|
|
return usage();
|
|
|
|
}
|
2016-08-26 07:00:22 +08:00
|
|
|
r = adb_server_main(is_daemon, server_socket_str, ack_reply_fd);
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
2016-08-26 07:00:22 +08:00
|
|
|
r = launch_server(server_socket_str);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:37:24 +08:00
|
|
|
if (r) {
|
2009-03-04 11:32:55 +08:00
|
|
|
fprintf(stderr,"* could not start server *\n");
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-12-06 09:37:24 +08:00
|
|
|
if (argc == 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
|
2014-12-06 09:32:46 +08:00
|
|
|
/* handle wait-for-* prefix */
|
|
|
|
if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
|
2015-02-26 09:51:28 +08:00
|
|
|
const char* service = argv[0];
|
2014-12-06 09:32:46 +08:00
|
|
|
|
2015-05-06 04:10:43 +08:00
|
|
|
if (!wait_for_device(service, transport_type, serial)) {
|
2014-12-06 09:32:46 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:29:32 +08:00
|
|
|
// Allow a command to be run after wait-for-device,
|
|
|
|
// e.g. 'adb wait-for-device shell'.
|
2014-12-06 09:32:46 +08:00
|
|
|
if (argc == 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall through */
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-12-06 09:32:46 +08:00
|
|
|
/* adb_connect() commands */
|
2014-12-06 09:37:24 +08:00
|
|
|
if (!strcmp(argv[0], "devices")) {
|
2015-02-26 09:51:28 +08:00
|
|
|
const char *listopt;
|
2015-04-30 03:28:13 +08:00
|
|
|
if (argc < 2) {
|
2012-04-21 02:21:14 +08:00
|
|
|
listopt = "";
|
2015-04-30 03:28:13 +08:00
|
|
|
} else if (argc == 2 && !strcmp(argv[1], "-l")) {
|
2012-04-21 02:21:14 +08:00
|
|
|
listopt = argv[1];
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
2015-04-30 03:28:13 +08:00
|
|
|
fprintf(stderr, "Usage: adb devices [-l]\n");
|
2009-03-04 11:32:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2015-04-30 03:28:13 +08:00
|
|
|
|
|
|
|
std::string query = android::base::StringPrintf("host:%s%s", argv[0], listopt);
|
|
|
|
printf("List of devices attached\n");
|
|
|
|
return adb_query_command(query);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "connect")) {
|
2009-08-25 06:58:40 +08:00
|
|
|
if (argc != 2) {
|
2010-05-24 22:44:35 +08:00
|
|
|
fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
|
2009-08-25 06:58:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2015-04-30 03:28:13 +08:00
|
|
|
|
|
|
|
std::string query = android::base::StringPrintf("host:connect:%s", argv[1]);
|
|
|
|
return adb_query_command(query);
|
2010-05-24 22:44:35 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "disconnect")) {
|
2010-05-24 22:44:35 +08:00
|
|
|
if (argc > 2) {
|
|
|
|
fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-04-30 03:28:13 +08:00
|
|
|
|
|
|
|
std::string query = android::base::StringPrintf("host:disconnect:%s",
|
|
|
|
(argc == 2) ? argv[1] : "");
|
|
|
|
return adb_query_command(query);
|
2009-08-25 06:58:40 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "emu")) {
|
2015-05-07 07:13:42 +08:00
|
|
|
return adb_send_emulator_command(argc, argv, serial);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-11-04 03:18:40 +08:00
|
|
|
else if (!strcmp(argv[0], "shell")) {
|
2016-02-01 11:12:26 +08:00
|
|
|
return adb_shell(argc, argv);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
|
2014-05-27 09:30:43 +08:00
|
|
|
int exec_in = !strcmp(argv[0], "exec-in");
|
|
|
|
|
2016-04-15 05:18:04 +08:00
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "Usage: adb %s command\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-18 01:59:34 +08:00
|
|
|
std::string cmd = "exec:";
|
|
|
|
cmd += argv[1];
|
2014-05-27 09:30:43 +08:00
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
|
|
|
while (argc-- > 0) {
|
2015-04-18 11:11:08 +08:00
|
|
|
cmd += " " + escape_arg(*argv++);
|
2014-05-27 09:30:43 +08:00
|
|
|
}
|
|
|
|
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2015-04-30 03:28:13 +08:00
|
|
|
int fd = adb_connect(cmd, &error);
|
2014-05-27 09:30:43 +08:00
|
|
|
if (fd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
2014-05-27 09:30:43 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exec_in) {
|
|
|
|
copy_to_file(STDIN_FILENO, fd);
|
|
|
|
} else {
|
|
|
|
copy_to_file(fd, STDOUT_FILENO);
|
|
|
|
}
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "kill-server")) {
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
|
|
|
int fd = _adb_connect("host:kill", &error);
|
2015-08-12 08:05:02 +08:00
|
|
|
if (fd == -2) {
|
|
|
|
// Failed to make network connection to server. Don't output the
|
|
|
|
// network error since that is expected.
|
2009-03-04 11:32:55 +08:00
|
|
|
fprintf(stderr,"* server not running *\n");
|
2015-08-12 08:05:02 +08:00
|
|
|
// Successful exit code because the server is already "killed".
|
|
|
|
return 0;
|
|
|
|
} else if (fd == -1) {
|
|
|
|
// Some other error.
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
2009-03-04 11:32:55 +08:00
|
|
|
return 1;
|
2015-08-12 08:05:02 +08:00
|
|
|
} else {
|
|
|
|
// Successfully connected, kill command sent, okay status came back.
|
|
|
|
// Server should exit() in a moment, if not already.
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 08:32:44 +08:00
|
|
|
ReadOrderlyShutdown(fd);
|
2015-08-12 08:05:02 +08:00
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "sideload")) {
|
|
|
|
if (argc != 2) return usage();
|
2014-06-27 06:35:36 +08:00
|
|
|
if (adb_sideload_host(argv[1])) {
|
2012-01-10 06:54:53 +08:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-07-22 07:13:40 +08:00
|
|
|
else if (!strcmp(argv[0], "tcpip") && argc > 1) {
|
|
|
|
return adb_connect_command(android::base::StringPrintf("tcpip:%s", argv[1]));
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "remount") ||
|
|
|
|
!strcmp(argv[0], "reboot") ||
|
|
|
|
!strcmp(argv[0], "reboot-bootloader") ||
|
|
|
|
!strcmp(argv[0], "usb") ||
|
|
|
|
!strcmp(argv[0], "disable-verity") ||
|
|
|
|
!strcmp(argv[0], "enable-verity")) {
|
2015-05-08 14:37:40 +08:00
|
|
|
std::string command;
|
2015-03-30 02:22:34 +08:00
|
|
|
if (!strcmp(argv[0], "reboot-bootloader")) {
|
2015-05-08 14:37:40 +08:00
|
|
|
command = "reboot:bootloader";
|
2015-03-30 02:22:34 +08:00
|
|
|
} else if (argc > 1) {
|
2015-05-08 14:37:40 +08:00
|
|
|
command = android::base::StringPrintf("%s:%s", argv[0], argv[1]);
|
2015-03-30 02:22:34 +08:00
|
|
|
} else {
|
2015-05-08 14:37:40 +08:00
|
|
|
command = android::base::StringPrintf("%s:", argv[0]);
|
2009-03-14 04:04:37 +08:00
|
|
|
}
|
2015-03-30 02:22:34 +08:00
|
|
|
return adb_connect_command(command);
|
2016-03-05 07:16:18 +08:00
|
|
|
} else if (!strcmp(argv[0], "root") || !strcmp(argv[0], "unroot")) {
|
|
|
|
return adb_root(argv[0]) ? 0 : 1;
|
|
|
|
} else if (!strcmp(argv[0], "bugreport")) {
|
2016-07-20 08:07:22 +08:00
|
|
|
Bugreport bugreport;
|
|
|
|
return bugreport.DoIt(transport_type, serial, argc, argv);
|
2016-04-02 08:43:27 +08:00
|
|
|
} else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
|
2015-05-30 08:55:19 +08:00
|
|
|
bool reverse = !strcmp(argv[0], "reverse");
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) return usage();
|
2012-11-14 22:01:55 +08:00
|
|
|
|
|
|
|
// Determine the <host-prefix> for this command.
|
2015-05-30 08:55:19 +08:00
|
|
|
std::string host_prefix;
|
2013-03-22 04:07:42 +08:00
|
|
|
if (reverse) {
|
2015-05-30 08:55:19 +08:00
|
|
|
host_prefix = "reverse";
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
2013-03-22 04:07:42 +08:00
|
|
|
if (serial) {
|
2015-05-30 08:55:19 +08:00
|
|
|
host_prefix = android::base::StringPrintf("host-serial:%s", serial);
|
2015-05-06 04:10:43 +08:00
|
|
|
} else if (transport_type == kTransportUsb) {
|
2015-05-30 08:55:19 +08:00
|
|
|
host_prefix = "host-usb";
|
2015-05-06 04:10:43 +08:00
|
|
|
} else if (transport_type == kTransportLocal) {
|
2015-05-30 08:55:19 +08:00
|
|
|
host_prefix = "host-local";
|
2013-03-22 04:07:42 +08:00
|
|
|
} else {
|
2015-05-30 08:55:19 +08:00
|
|
|
host_prefix = "host";
|
2012-11-14 22:01:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 02:25:48 +08:00
|
|
|
std::string cmd, error;
|
2015-05-30 08:55:19 +08:00
|
|
|
if (strcmp(argv[0], "--list") == 0) {
|
2015-05-02 08:04:38 +08:00
|
|
|
if (argc != 1) return usage();
|
2015-05-30 08:55:19 +08:00
|
|
|
return adb_query_command(host_prefix + ":list-forward");
|
|
|
|
} else if (strcmp(argv[0], "--remove-all") == 0) {
|
|
|
|
if (argc != 1) return usage();
|
|
|
|
cmd = host_prefix + ":killforward-all";
|
|
|
|
} else if (strcmp(argv[0], "--remove") == 0) {
|
|
|
|
// forward --remove <local>
|
2015-05-02 08:04:38 +08:00
|
|
|
if (argc != 2) return usage();
|
2015-05-30 08:55:19 +08:00
|
|
|
cmd = host_prefix + ":killforward:" + argv[1];
|
|
|
|
} else if (strcmp(argv[0], "--no-rebind") == 0) {
|
|
|
|
// forward --no-rebind <local> <remote>
|
2015-05-02 08:04:38 +08:00
|
|
|
if (argc != 3) return usage();
|
2016-04-08 02:25:48 +08:00
|
|
|
if (forward_targets_are_valid(argv[1], argv[2], &error)) {
|
|
|
|
cmd = host_prefix + ":forward:norebind:" + argv[1] + ";" + argv[2];
|
|
|
|
}
|
2015-05-30 08:55:19 +08:00
|
|
|
} else {
|
|
|
|
// forward <local> <remote>
|
|
|
|
if (argc != 2) return usage();
|
2016-04-08 02:25:48 +08:00
|
|
|
if (forward_targets_are_valid(argv[0], argv[1], &error)) {
|
|
|
|
cmd = host_prefix + ":forward:" + argv[0] + ";" + argv[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error.empty()) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
return 1;
|
2012-11-14 22:01:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-08 02:25:48 +08:00
|
|
|
int fd = adb_connect(cmd, &error);
|
|
|
|
if (fd < 0 || !adb_status(fd, &error)) {
|
|
|
|
adb_close(fd);
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server or device may optionally return a resolved TCP port number.
|
|
|
|
std::string resolved_port;
|
|
|
|
if (ReadProtocolString(fd, &resolved_port, &error) && !resolved_port.empty()) {
|
|
|
|
printf("%s\n", resolved_port.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadOrderlyShutdown(fd);
|
|
|
|
return 0;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
/* do_sync_*() commands */
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "ls")) {
|
|
|
|
if (argc != 2) return usage();
|
2015-08-04 01:38:08 +08:00
|
|
|
return do_sync_ls(argv[1]) ? 0 : 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "push")) {
|
2015-10-31 07:57:19 +08:00
|
|
|
bool copy_attrs = false;
|
|
|
|
std::vector<const char*> srcs;
|
|
|
|
const char* dst = nullptr;
|
2014-03-12 08:55:59 +08:00
|
|
|
|
2015-10-31 07:57:19 +08:00
|
|
|
parse_push_pull_args(&argv[1], argc - 1, &srcs, &dst, ©_attrs);
|
|
|
|
if (srcs.empty() || !dst) return usage();
|
|
|
|
return do_sync_push(srcs, dst) ? 0 : 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "pull")) {
|
2015-10-31 07:57:19 +08:00
|
|
|
bool copy_attrs = false;
|
|
|
|
std::vector<const char*> srcs;
|
|
|
|
const char* dst = ".";
|
2014-03-12 08:55:59 +08:00
|
|
|
|
2015-10-31 07:57:19 +08:00
|
|
|
parse_push_pull_args(&argv[1], argc - 1, &srcs, &dst, ©_attrs);
|
|
|
|
if (srcs.empty()) return usage();
|
|
|
|
return do_sync_pull(srcs, dst, copy_attrs) ? 0 : 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "install")) {
|
2009-03-04 11:32:55 +08:00
|
|
|
if (argc < 2) return usage();
|
2016-07-21 07:22:37 +08:00
|
|
|
if (_use_legacy_install()) {
|
|
|
|
return install_app_legacy(transport_type, serial, argc, argv);
|
2016-01-22 11:56:50 +08:00
|
|
|
}
|
2016-07-21 07:22:37 +08:00
|
|
|
return install_app(transport_type, serial, argc, argv);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "install-multiple")) {
|
2014-06-10 08:30:57 +08:00
|
|
|
if (argc < 2) return usage();
|
2015-05-06 04:10:43 +08:00
|
|
|
return install_multiple_app(transport_type, serial, argc, argv);
|
2014-06-10 08:30:57 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "uninstall")) {
|
2009-03-04 11:32:55 +08:00
|
|
|
if (argc < 2) return usage();
|
2016-07-21 07:22:37 +08:00
|
|
|
if (_use_legacy_install()) {
|
|
|
|
return uninstall_app_legacy(transport_type, serial, argc, argv);
|
2016-01-22 11:56:50 +08:00
|
|
|
}
|
2016-07-21 07:22:37 +08:00
|
|
|
return uninstall_app(transport_type, serial, argc, argv);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "sync")) {
|
2015-04-22 01:17:07 +08:00
|
|
|
std::string src;
|
2015-04-18 04:57:15 +08:00
|
|
|
bool list_only = false;
|
2014-12-06 09:37:24 +08:00
|
|
|
if (argc < 2) {
|
2015-04-18 04:57:15 +08:00
|
|
|
// No local path was specified.
|
2015-04-22 01:17:07 +08:00
|
|
|
src = "";
|
2010-02-22 22:36:49 +08:00
|
|
|
} else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
|
2015-04-22 01:17:07 +08:00
|
|
|
list_only = true;
|
2010-02-22 22:36:49 +08:00
|
|
|
if (argc == 3) {
|
2015-04-22 01:17:07 +08:00
|
|
|
src = argv[2];
|
2010-02-22 22:36:49 +08:00
|
|
|
} else {
|
2015-04-22 01:17:07 +08:00
|
|
|
src = "";
|
2010-02-22 22:36:49 +08:00
|
|
|
}
|
2014-12-06 09:37:24 +08:00
|
|
|
} else if (argc == 2) {
|
2015-04-18 04:57:15 +08:00
|
|
|
// A local path or "android"/"data" arg was specified.
|
2015-04-22 01:17:07 +08:00
|
|
|
src = argv[1];
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
|
|
|
return usage();
|
|
|
|
}
|
2015-04-18 04:57:15 +08:00
|
|
|
|
2015-04-22 01:17:07 +08:00
|
|
|
if (src != "" &&
|
|
|
|
src != "system" && src != "data" && src != "vendor" && src != "oem") {
|
2015-04-18 04:57:15 +08:00
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string system_src_path = product_file("system");
|
|
|
|
std::string data_src_path = product_file("data");
|
|
|
|
std::string vendor_src_path = product_file("vendor");
|
|
|
|
std::string oem_src_path = product_file("oem");
|
|
|
|
|
2015-08-04 01:38:08 +08:00
|
|
|
bool okay = true;
|
|
|
|
if (okay && (src.empty() || src == "system")) {
|
|
|
|
okay = do_sync_sync(system_src_path, "/system", list_only);
|
2015-04-18 04:57:15 +08:00
|
|
|
}
|
2015-08-04 01:38:08 +08:00
|
|
|
if (okay && (src.empty() || src == "vendor") && directory_exists(vendor_src_path)) {
|
|
|
|
okay = do_sync_sync(vendor_src_path, "/vendor", list_only);
|
2015-04-18 04:57:15 +08:00
|
|
|
}
|
2015-08-04 01:38:08 +08:00
|
|
|
if (okay && (src.empty() || src == "oem") && directory_exists(oem_src_path)) {
|
|
|
|
okay = do_sync_sync(oem_src_path, "/oem", list_only);
|
2015-04-18 04:57:15 +08:00
|
|
|
}
|
2015-08-04 01:38:08 +08:00
|
|
|
if (okay && (src.empty() || src == "data")) {
|
|
|
|
okay = do_sync_sync(data_src_path, "/data", list_only);
|
2015-04-18 04:57:15 +08:00
|
|
|
}
|
2015-08-04 01:38:08 +08:00
|
|
|
return okay ? 0 : 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
/* passthrough commands */
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0],"get-state") ||
|
2012-04-21 02:21:14 +08:00
|
|
|
!strcmp(argv[0],"get-serialno") ||
|
|
|
|
!strcmp(argv[0],"get-devpath"))
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
2015-05-06 04:10:43 +08:00
|
|
|
return adb_query_command(format_host_command(argv[0], transport_type, serial));
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
/* other commands */
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
|
2015-05-06 04:10:43 +08:00
|
|
|
return logcat(transport_type, serial, argc, argv);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0],"ppp")) {
|
2009-03-04 11:32:55 +08:00
|
|
|
return ppp(argc, argv);
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "start-server")) {
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2015-08-12 08:05:02 +08:00
|
|
|
const int result = adb_connect("host:start-server", &error);
|
|
|
|
if (result < 0) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
}
|
|
|
|
return result;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "backup")) {
|
2011-04-22 03:53:28 +08:00
|
|
|
return backup(argc, argv);
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "restore")) {
|
2011-05-18 06:52:54 +08:00
|
|
|
return restore(argc, argv);
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "keygen")) {
|
2014-11-14 07:17:29 +08:00
|
|
|
if (argc < 2) return usage();
|
2015-09-23 06:52:57 +08:00
|
|
|
// Always print key generation information for keygen command.
|
|
|
|
adb_trace_enable(AUTH);
|
2014-11-14 07:17:29 +08:00
|
|
|
return adb_auth_keygen(argv[1]);
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "jdwp")) {
|
2015-03-30 02:22:34 +08:00
|
|
|
return adb_connect_command("jdwp");
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2016-05-14 09:18:23 +08:00
|
|
|
else if (!strcmp(argv[0], "track-jdwp")) {
|
|
|
|
return adb_connect_command("track-jdwp");
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[0], "track-devices")) {
|
|
|
|
return adb_connect_command("host:track-devices");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
/* "adb /?" is a common idiom under Windows */
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
|
2009-03-04 11:32:55 +08:00
|
|
|
help();
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-06 09:32:46 +08:00
|
|
|
else if (!strcmp(argv[0], "version")) {
|
2015-08-12 23:32:10 +08:00
|
|
|
fprintf(stdout, "%s", adb_version().c_str());
|
2009-03-04 11:32:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-21 09:58:41 +08:00
|
|
|
else if (!strcmp(argv[0], "features")) {
|
2015-09-23 01:43:08 +08:00
|
|
|
// Only list the features common to both the adb client and the device.
|
2016-01-22 11:56:50 +08:00
|
|
|
FeatureSet features;
|
2016-02-01 11:12:26 +08:00
|
|
|
std::string error;
|
|
|
|
if (!adb_get_feature_set(&features, &error)) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
2016-01-22 11:56:50 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:43:08 +08:00
|
|
|
for (const std::string& name : features) {
|
2015-10-01 04:35:42 +08:00
|
|
|
if (CanUseFeature(features, name)) {
|
2015-09-23 01:43:08 +08:00
|
|
|
printf("%s\n", name.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2016-04-06 04:50:44 +08:00
|
|
|
} else if (!strcmp(argv[0], "reconnect")) {
|
|
|
|
if (argc == 1) {
|
|
|
|
return adb_query_command("host:reconnect");
|
2016-10-28 05:01:08 +08:00
|
|
|
} else if (argc == 2) {
|
|
|
|
if (!strcmp(argv[1], "device")) {
|
|
|
|
std::string err;
|
|
|
|
adb_connect("reconnect", &err);
|
|
|
|
return 0;
|
|
|
|
} else if (!strcmp(argv[1], "offline")) {
|
|
|
|
std::string err;
|
|
|
|
return adb_query_command("host:reconnect-offline");
|
|
|
|
} else {
|
|
|
|
return usage();
|
|
|
|
}
|
2016-04-06 04:50:44 +08:00
|
|
|
}
|
2015-05-21 09:58:41 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
static int uninstall_app(TransportType transport, const char* serial, int argc, const char** argv) {
|
|
|
|
// 'adb uninstall' takes the same arguments as 'cmd package uninstall' on device
|
|
|
|
std::string cmd = "cmd package";
|
2015-04-18 01:59:34 +08:00
|
|
|
while (argc-- > 0) {
|
2015-11-04 08:53:08 +08:00
|
|
|
// deny the '-k' option until the remaining data/cache can be removed with adb/UI
|
|
|
|
if (strcmp(*argv, "-k") == 0) {
|
|
|
|
printf(
|
|
|
|
"The -k option uninstalls the application while retaining the data/cache.\n"
|
|
|
|
"At the moment, there is no way to remove the remaining data.\n"
|
|
|
|
"You will have to reinstall the application with the same signature, and fully uninstall it.\n"
|
|
|
|
"If you truly wish to continue, execute 'adb shell cmd package uninstall -k'.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2015-04-18 11:11:08 +08:00
|
|
|
cmd += " " + escape_arg(*argv++);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 04:35:42 +08:00
|
|
|
return send_shell_command(transport, serial, cmd, false);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
static int install_app(TransportType transport, const char* serial, int argc, const char** argv) {
|
|
|
|
// The last argument must be the APK file
|
|
|
|
const char* file = argv[argc - 1];
|
2016-10-25 09:29:21 +08:00
|
|
|
if (!android::base::EndsWithIgnoreCase(file, ".apk")) {
|
|
|
|
fprintf(stderr, "Filename doesn't end .apk: %s\n", file);
|
|
|
|
return EXIT_FAILURE;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 09:29:21 +08:00
|
|
|
struct stat sb;
|
|
|
|
if (stat(file, &sb) == -1) {
|
|
|
|
fprintf(stderr, "Failed to stat %s: %s\n", file, strerror(errno));
|
|
|
|
return 1;
|
2015-11-04 08:53:08 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
int localFd = adb_open(file, O_RDONLY);
|
|
|
|
if (localFd < 0) {
|
|
|
|
fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
std::string error;
|
|
|
|
std::string cmd = "exec:cmd package";
|
2011-08-06 02:19:45 +08:00
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
// don't copy the APK name, but, copy the rest of the arguments as-is
|
|
|
|
while (argc-- > 1) {
|
|
|
|
cmd += " " + escape_arg(std::string(*argv++));
|
2011-08-06 02:19:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
// add size parameter [required for streaming installs]
|
|
|
|
// do last to override any user specified value
|
|
|
|
cmd += " " + android::base::StringPrintf("-S %" PRIu64, static_cast<uint64_t>(sb.st_size));
|
2014-06-10 08:30:57 +08:00
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
int remoteFd = adb_connect(cmd, &error);
|
|
|
|
if (remoteFd < 0) {
|
|
|
|
fprintf(stderr, "Connect error for write: %s\n", error.c_str());
|
|
|
|
adb_close(localFd);
|
|
|
|
return 1;
|
2011-08-06 02:19:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
copy_to_file(localFd, remoteFd);
|
|
|
|
read_status_line(remoteFd, buf, sizeof(buf));
|
2011-08-06 02:19:45 +08:00
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
adb_close(localFd);
|
|
|
|
adb_close(remoteFd);
|
2014-06-10 08:30:57 +08:00
|
|
|
|
2015-11-04 08:53:08 +08:00
|
|
|
if (strncmp("Success", buf, 7)) {
|
2015-12-18 06:04:38 +08:00
|
|
|
fprintf(stderr, "Failed to install %s: %s", file, buf);
|
2015-11-04 08:53:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fputs(buf, stderr);
|
|
|
|
return 0;
|
2011-08-06 02:19:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-06 04:10:43 +08:00
|
|
|
static int install_multiple_app(TransportType transport, const char* serial, int argc,
|
2015-04-18 04:57:15 +08:00
|
|
|
const char** argv)
|
2011-08-06 02:19:45 +08:00
|
|
|
{
|
2014-06-10 08:30:57 +08:00
|
|
|
// Find all APK arguments starting at end.
|
|
|
|
// All other arguments passed through verbatim.
|
|
|
|
int first_apk = -1;
|
2016-10-25 09:29:21 +08:00
|
|
|
uint64_t total_size = 0;
|
|
|
|
for (int i = argc - 1; i >= 0; i--) {
|
2015-02-26 09:51:28 +08:00
|
|
|
const char* file = argv[i];
|
2010-02-20 06:53:27 +08:00
|
|
|
|
2016-10-25 09:29:21 +08:00
|
|
|
if (android::base::EndsWithIgnoreCase(file, ".apk")) {
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(file, &sb) != -1) total_size += sb.st_size;
|
2014-06-10 08:30:57 +08:00
|
|
|
first_apk = i;
|
|
|
|
} else {
|
2011-08-06 02:19:45 +08:00
|
|
|
break;
|
2014-06-10 08:30:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_apk == -1) {
|
2016-10-25 09:29:21 +08:00
|
|
|
fprintf(stderr, "No APK file on command line\n");
|
2011-08-06 02:19:45 +08:00
|
|
|
return 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2011-08-06 02:19:45 +08:00
|
|
|
|
2016-07-21 07:22:37 +08:00
|
|
|
std::string install_cmd;
|
|
|
|
if (_use_legacy_install()) {
|
|
|
|
install_cmd = "exec:pm";
|
|
|
|
} else {
|
|
|
|
install_cmd = "exec:cmd package";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmd = android::base::StringPrintf("%s install-create -S %" PRIu64, install_cmd.c_str(), total_size);
|
2016-10-25 09:29:21 +08:00
|
|
|
for (int i = 1; i < first_apk; i++) {
|
2015-04-18 11:11:08 +08:00
|
|
|
cmd += " " + escape_arg(argv[i]);
|
2014-06-10 08:30:57 +08:00
|
|
|
}
|
2011-08-06 02:19:45 +08:00
|
|
|
|
2014-06-10 08:30:57 +08:00
|
|
|
// Create install session
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2015-04-30 03:28:13 +08:00
|
|
|
int fd = adb_connect(cmd, &error);
|
2014-06-10 08:30:57 +08:00
|
|
|
if (fd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "Connect error for create: %s\n", error.c_str());
|
2015-11-04 08:53:08 +08:00
|
|
|
return EXIT_FAILURE;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-04-18 01:59:34 +08:00
|
|
|
char buf[BUFSIZ];
|
2014-06-10 08:30:57 +08:00
|
|
|
read_status_line(fd, buf, sizeof(buf));
|
|
|
|
adb_close(fd);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-06-10 08:30:57 +08:00
|
|
|
int session_id = -1;
|
|
|
|
if (!strncmp("Success", buf, 7)) {
|
|
|
|
char* start = strrchr(buf, '[');
|
|
|
|
char* end = strrchr(buf, ']');
|
|
|
|
if (start && end) {
|
|
|
|
*end = '\0';
|
|
|
|
session_id = strtol(start + 1, NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (session_id < 0) {
|
|
|
|
fprintf(stderr, "Failed to create session\n");
|
2014-07-15 07:45:13 +08:00
|
|
|
fputs(buf, stderr);
|
2015-11-04 08:53:08 +08:00
|
|
|
return EXIT_FAILURE;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2011-08-06 02:19:45 +08:00
|
|
|
|
2014-06-10 08:30:57 +08:00
|
|
|
// Valid session, now stream the APKs
|
|
|
|
int success = 1;
|
2016-10-25 09:29:21 +08:00
|
|
|
for (int i = first_apk; i < argc; i++) {
|
2015-02-26 09:51:28 +08:00
|
|
|
const char* file = argv[i];
|
2016-10-25 09:29:21 +08:00
|
|
|
struct stat sb;
|
2014-06-10 08:30:57 +08:00
|
|
|
if (stat(file, &sb) == -1) {
|
2016-10-25 09:29:21 +08:00
|
|
|
fprintf(stderr, "Failed to stat %s: %s\n", file, strerror(errno));
|
2014-06-10 08:30:57 +08:00
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
|
|
|
}
|
2011-08-06 02:19:45 +08:00
|
|
|
|
2015-04-18 05:07:52 +08:00
|
|
|
std::string cmd = android::base::StringPrintf(
|
2016-07-21 07:22:37 +08:00
|
|
|
"%s install-write -S %" PRIu64 " %d %d_%s -",
|
|
|
|
install_cmd.c_str(), static_cast<uint64_t>(sb.st_size), session_id, i, adb_basename(file).c_str());
|
2014-06-10 08:30:57 +08:00
|
|
|
|
|
|
|
int localFd = adb_open(file, O_RDONLY);
|
|
|
|
if (localFd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
|
2014-06-10 08:30:57 +08:00
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
2011-08-06 02:19:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-29 23:35:59 +08:00
|
|
|
std::string error;
|
2015-04-30 03:28:13 +08:00
|
|
|
int remoteFd = adb_connect(cmd, &error);
|
2014-06-10 08:30:57 +08:00
|
|
|
if (remoteFd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "Connect error for write: %s\n", error.c_str());
|
2014-06-10 08:30:57 +08:00
|
|
|
adb_close(localFd);
|
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-06-10 08:30:57 +08:00
|
|
|
copy_to_file(localFd, remoteFd);
|
|
|
|
read_status_line(remoteFd, buf, sizeof(buf));
|
|
|
|
|
|
|
|
adb_close(localFd);
|
|
|
|
adb_close(remoteFd);
|
|
|
|
|
|
|
|
if (strncmp("Success", buf, 7)) {
|
|
|
|
fprintf(stderr, "Failed to write %s\n", file);
|
2014-07-15 07:45:13 +08:00
|
|
|
fputs(buf, stderr);
|
2014-06-10 08:30:57 +08:00
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
2011-08-06 02:19:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 08:30:57 +08:00
|
|
|
finalize_session:
|
2014-07-26 00:58:25 +08:00
|
|
|
// Commit session if we streamed everything okay; otherwise abandon
|
2015-04-30 03:28:13 +08:00
|
|
|
std::string service =
|
2016-07-21 07:22:37 +08:00
|
|
|
android::base::StringPrintf("%s install-%s %d",
|
|
|
|
install_cmd.c_str(), success ? "commit" : "abandon", session_id);
|
2015-04-30 03:28:13 +08:00
|
|
|
fd = adb_connect(service, &error);
|
2014-06-10 08:30:57 +08:00
|
|
|
if (fd < 0) {
|
2015-04-29 23:35:59 +08:00
|
|
|
fprintf(stderr, "Connect error for finalize: %s\n", error.c_str());
|
2015-11-04 08:53:08 +08:00
|
|
|
return EXIT_FAILURE;
|
2014-06-10 08:30:57 +08:00
|
|
|
}
|
|
|
|
read_status_line(fd, buf, sizeof(buf));
|
|
|
|
adb_close(fd);
|
2011-08-06 02:19:45 +08:00
|
|
|
|
2014-06-10 08:30:57 +08:00
|
|
|
if (!strncmp("Success", buf, 7)) {
|
2014-07-15 07:45:13 +08:00
|
|
|
fputs(buf, stderr);
|
2014-06-10 08:30:57 +08:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Failed to finalize session\n");
|
2014-07-15 07:45:13 +08:00
|
|
|
fputs(buf, stderr);
|
2015-11-04 08:53:08 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pm_command(TransportType transport, const char* serial, int argc, const char** argv) {
|
|
|
|
std::string cmd = "pm";
|
|
|
|
|
|
|
|
while (argc-- > 0) {
|
|
|
|
cmd += " " + escape_arg(*argv++);
|
|
|
|
}
|
|
|
|
|
|
|
|
return send_shell_command(transport, serial, cmd, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uninstall_app_legacy(TransportType transport, const char* serial, int argc, const char** argv) {
|
|
|
|
/* if the user choose the -k option, we refuse to do it until devices are
|
|
|
|
out with the option to uninstall the remaining data somehow (adb/ui) */
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-k")) {
|
|
|
|
printf(
|
|
|
|
"The -k option uninstalls the application while retaining the data/cache.\n"
|
|
|
|
"At the moment, there is no way to remove the remaining data.\n"
|
|
|
|
"You will have to reinstall the application with the same signature, and fully uninstall it.\n"
|
|
|
|
"If you truly wish to continue, execute 'adb shell pm uninstall -k'\n.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */
|
|
|
|
return pm_command(transport, serial, argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int delete_file(TransportType transport, const char* serial, const std::string& filename) {
|
|
|
|
std::string cmd = "rm -f " + escape_arg(filename);
|
|
|
|
return send_shell_command(transport, serial, cmd, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int install_app_legacy(TransportType transport, const char* serial, int argc, const char** argv) {
|
|
|
|
static const char *const DATA_DEST = "/data/local/tmp/%s";
|
|
|
|
static const char *const SD_DEST = "/sdcard/tmp/%s";
|
|
|
|
const char* where = DATA_DEST;
|
|
|
|
|
2016-10-25 09:29:21 +08:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2015-11-04 08:53:08 +08:00
|
|
|
if (!strcmp(argv[i], "-s")) {
|
|
|
|
where = SD_DEST;
|
|
|
|
}
|
2014-06-10 08:30:57 +08:00
|
|
|
}
|
2015-11-04 08:53:08 +08:00
|
|
|
|
|
|
|
// Find last APK argument.
|
|
|
|
// All other arguments passed through verbatim.
|
|
|
|
int last_apk = -1;
|
2016-10-25 09:29:21 +08:00
|
|
|
for (int i = argc - 1; i >= 0; i--) {
|
|
|
|
if (android::base::EndsWithIgnoreCase(argv[i], ".apk")) {
|
2015-11-04 08:53:08 +08:00
|
|
|
last_apk = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_apk == -1) {
|
2016-10-25 09:29:21 +08:00
|
|
|
fprintf(stderr, "No APK file on command line\n");
|
2015-11-04 08:53:08 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = -1;
|
|
|
|
std::vector<const char*> apk_file = {argv[last_apk]};
|
|
|
|
std::string apk_dest = android::base::StringPrintf(
|
|
|
|
where, adb_basename(argv[last_apk]).c_str());
|
|
|
|
if (!do_sync_push(apk_file, apk_dest.c_str())) goto cleanup_apk;
|
|
|
|
argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
|
|
|
|
result = pm_command(transport, serial, argc, argv);
|
|
|
|
|
|
|
|
cleanup_apk:
|
|
|
|
delete_file(transport, serial, apk_dest);
|
|
|
|
return result;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|