2009-03-04 04:32:55 +01: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-03-19 23:21:08 +01:00
|
|
|
#define TRACE_TAG TRACE_ADB
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <errno.h>
|
2015-04-17 23:07:52 +02:00
|
|
|
#include <inttypes.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/stat.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <sys/types.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-17 19:59:34 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <base/stringprintf.h>
|
|
|
|
|
2014-11-17 23:48:25 +01:00
|
|
|
#if !defined(_WIN32)
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <termios.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "adb.h"
|
2014-11-14 00:17:29 +01:00
|
|
|
#include "adb_auth.h"
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_client.h"
|
|
|
|
#include "adb_io.h"
|
2015-04-17 22:57:15 +02:00
|
|
|
#include "adb_utils.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "file_sync_service.h"
|
|
|
|
|
2015-05-05 22:10:43 +02: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);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
static std::string gProductOutPath;
|
2012-11-14 19:16:17 +01:00
|
|
|
extern int gListenAll;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
static std::string product_file(const char *extra) {
|
|
|
|
if (gProductOutPath.empty()) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr, "adb: Product directory not specified; "
|
|
|
|
"use -p or define ANDROID_PRODUCT_OUT\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
return android::base::StringPrintf("%s%s%s",
|
|
|
|
gProductOutPath.c_str(), OS_PATH_SEPARATOR_STR, extra);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
static void help() {
|
2015-08-12 17:32:10 +02:00
|
|
|
fprintf(stderr, "%s\n", adb_version().c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,
|
2012-11-14 19:16:17 +01:00
|
|
|
" -a - directs adb to listen on all interfaces for a connection\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" -d - directs command to the only connected USB device\n"
|
|
|
|
" returns an error if more than one USB device is present.\n"
|
|
|
|
" -e - directs command to the only running emulator.\n"
|
|
|
|
" returns an error if more than one emulator is running.\n"
|
2012-04-20 20:21:14 +02:00
|
|
|
" -s <specific device> - directs command to the device or emulator with the given\n"
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
" serial number or qualifier. Overrides ANDROID_SERIAL\n"
|
2009-10-08 00:38:53 +02:00
|
|
|
" environment variable.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" -p <product name or path> - simple product name like 'sooner', or\n"
|
|
|
|
" a relative/absolute path to a product\n"
|
|
|
|
" out directory like 'out/target/product/sooner'.\n"
|
|
|
|
" If -p is not specified, the ANDROID_PRODUCT_OUT\n"
|
|
|
|
" environment variable is used, which must\n"
|
|
|
|
" be an absolute path.\n"
|
2012-11-14 19:16:17 +01:00
|
|
|
" -H - Name of adb server host (default: localhost)\n"
|
|
|
|
" -P - Port of adb server (default: 5037)\n"
|
2012-04-20 20:21:14 +02:00
|
|
|
" devices [-l] - list all connected devices\n"
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
" ('-l' will also list device qualifiers)\n"
|
2010-05-24 16:44:35 +02:00
|
|
|
" connect <host>[:<port>] - connect to a device via TCP/IP\n"
|
|
|
|
" Port 5555 is used by default if no port number is specified.\n"
|
|
|
|
" disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.\n"
|
|
|
|
" Port 5555 is used by default if no port number is specified.\n"
|
2011-04-26 12:46:05 +02:00
|
|
|
" Using this command with no additional arguments\n"
|
2010-05-24 16:44:35 +02:00
|
|
|
" will disconnect from all connected TCP/IP devices.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
"\n"
|
|
|
|
"device commands:\n"
|
2014-03-12 01:55:59 +01:00
|
|
|
" adb push [-p] <local> <remote>\n"
|
|
|
|
" - copy file/dir to device\n"
|
|
|
|
" ('-p' to display the transfer progress)\n"
|
2013-04-19 21:41:09 +02:00
|
|
|
" adb pull [-p] [-a] <remote> [<local>]\n"
|
2014-03-12 01:55:59 +01:00
|
|
|
" - copy file/dir from device\n"
|
|
|
|
" ('-p' to display the transfer progress)\n"
|
2013-04-19 21:41:09 +02:00
|
|
|
" ('-a' means copy timestamp and mode)\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" adb sync [ <directory> ] - copy host->device only if changed\n"
|
2010-02-22 15:36:49 +01:00
|
|
|
" (-l means list but don't copy)\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" adb shell - run remote shell interactively\n"
|
|
|
|
" adb shell <command> - run remote shell command\n"
|
|
|
|
" adb emu <command> - run emulator console command\n"
|
|
|
|
" adb logcat [ <filter-spec> ] - View device log\n"
|
2012-11-14 15:01:55 +01:00
|
|
|
" adb forward --list - list all forward socket connections.\n"
|
|
|
|
" the format is a list of lines with the following format:\n"
|
|
|
|
" <serial> \" \" <local> \" \" <remote> \"\\n\"\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" adb forward <local> <remote> - forward socket connections\n"
|
|
|
|
" forward specs are one of: \n"
|
|
|
|
" tcp:<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"
|
2012-11-14 15:01:55 +01:00
|
|
|
" adb forward --no-rebind <local> <remote>\n"
|
|
|
|
" - same as 'adb forward <local> <remote>' but fails\n"
|
|
|
|
" if <local> is already forwarded\n"
|
|
|
|
" adb forward --remove <local> - remove a specific forward socket connection\n"
|
|
|
|
" adb forward --remove-all - remove all forward socket connections\n"
|
2013-03-21 21:07:42 +01:00
|
|
|
" adb reverse --list - list all reverse socket connections from device\n"
|
|
|
|
" adb reverse <remote> <local> - reverse socket connections\n"
|
|
|
|
" reverse specs are one of:\n"
|
|
|
|
" tcp:<port>\n"
|
|
|
|
" localabstract:<unix domain socket name>\n"
|
|
|
|
" localreserved:<unix domain socket name>\n"
|
|
|
|
" localfilesystem:<unix domain socket name>\n"
|
|
|
|
" adb reverse --norebind <remote> <local>\n"
|
|
|
|
" - same as 'adb reverse <remote> <local>' but fails\n"
|
|
|
|
" if <remote> is already reversed.\n"
|
|
|
|
" adb reverse --remove <remote>\n"
|
|
|
|
" - remove a specific reversed socket connection\n"
|
|
|
|
" adb reverse --remove-all - remove all reversed socket connections from device\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" adb jdwp - list PIDs of processes hosting a JDWP transport\n"
|
2014-06-10 02:30:57 +02:00
|
|
|
" adb install [-lrtsd] <file>\n"
|
|
|
|
" adb install-multiple [-lrtsdp] <file...>\n"
|
2012-04-24 19:43:41 +02:00
|
|
|
" - push this package file to the device and install it\n"
|
2014-06-10 02:30:57 +02:00
|
|
|
" (-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)\n"
|
|
|
|
" (-p: partial application install)\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" adb uninstall [-k] <package> - remove this app package from the device\n"
|
|
|
|
" ('-k' means keep the data and cache directories)\n"
|
|
|
|
" adb bugreport - return all information from the device\n"
|
|
|
|
" that should be included in a bug report.\n"
|
|
|
|
"\n"
|
2013-03-07 01:40:52 +01:00
|
|
|
" adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]\n"
|
2011-10-04 03:27:01 +02:00
|
|
|
" - write an archive of the device's data to <file>.\n"
|
|
|
|
" If no -f option is supplied then the data is written\n"
|
|
|
|
" to \"backup.ab\" in the current directory.\n"
|
2011-04-21 21:53:28 +02:00
|
|
|
" (-apk|-noapk enable/disable backup of the .apks themselves\n"
|
2011-08-10 02:05:29 +02:00
|
|
|
" in the archive; the default is noapk.)\n"
|
2013-03-07 01:40:52 +01:00
|
|
|
" (-obb|-noobb enable/disable backup of any installed apk expansion\n"
|
|
|
|
" (aka .obb) files associated with each application; the default\n"
|
|
|
|
" is noobb.)\n"
|
2011-04-21 21:53:28 +02:00
|
|
|
" (-shared|-noshared enable/disable backup of the device's\n"
|
|
|
|
" shared storage / SD card contents; the default is noshared.)\n"
|
|
|
|
" (-all means to back up all installed applications)\n"
|
2011-10-04 03:27:01 +02:00
|
|
|
" (-system|-nosystem toggles whether -all automatically includes\n"
|
|
|
|
" system applications; the default is to include system apps)\n"
|
2011-04-21 21:53:28 +02:00
|
|
|
" (<packages...> is the list of applications to be backed up. If\n"
|
|
|
|
" the -all or -shared flags are passed, then the package\n"
|
2011-10-04 03:27:01 +02:00
|
|
|
" list is optional. Applications explicitly given on the\n"
|
|
|
|
" command line will be included even if -nosystem would\n"
|
|
|
|
" ordinarily cause them to be omitted.)\n"
|
2011-04-21 21:53:28 +02:00
|
|
|
"\n"
|
2011-08-10 02:05:29 +02:00
|
|
|
" adb restore <file> - restore device contents from the <file> backup archive\n"
|
2011-05-18 00:52:54 +02:00
|
|
|
"\n"
|
2014-12-04 00:31:57 +01:00
|
|
|
" adb disable-verity - disable dm-verity checking on USERDEBUG builds\n"
|
|
|
|
" adb enable-verity - re-enable dm-verity checking on USERDEBUG builds\n"
|
2014-11-14 00:17:29 +01:00
|
|
|
" adb keygen <file> - generate adb public/private key. The private key is stored in <file>,\n"
|
|
|
|
" and the public key is stored in <file>.pub. Any existing files\n"
|
|
|
|
" are overwritten.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" adb help - show this help message\n"
|
|
|
|
" adb version - show version num\n"
|
|
|
|
"\n"
|
|
|
|
"scripting:\n"
|
|
|
|
" adb wait-for-device - block until device is online\n"
|
|
|
|
" adb start-server - ensure that there is a server running\n"
|
|
|
|
" adb kill-server - kill the server if it is running\n"
|
|
|
|
" adb get-state - prints: offline | bootloader | device\n"
|
|
|
|
" adb get-serialno - prints: <serial-number>\n"
|
2012-04-20 20:21:14 +02:00
|
|
|
" adb get-devpath - prints: <device-path>\n"
|
2015-03-16 22:58:32 +01:00
|
|
|
" adb remount - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write\n"
|
2015-03-29 20:22:34 +02:00
|
|
|
" adb reboot [bootloader|recovery]\n"
|
|
|
|
" - reboots the device, optionally into the bootloader or recovery program.\n"
|
|
|
|
" adb reboot sideload - reboots the device into the sideload mode in recovery program (adb root required).\n"
|
|
|
|
" adb reboot sideload-auto-reboot\n"
|
|
|
|
" - reboots into the sideload mode, then reboots automatically after the sideload regardless of the result.\n"
|
2015-06-12 23:26:29 +02:00
|
|
|
" adb sideload <file> - sideloads the given package\n"
|
2009-08-25 00:58:40 +02:00
|
|
|
" adb root - restarts the adbd daemon with root permissions\n"
|
2014-10-06 19:57:20 +02:00
|
|
|
" adb unroot - restarts the adbd daemon without root permissions\n"
|
2009-12-14 23:42:17 +01:00
|
|
|
" adb usb - restarts the adbd daemon listening on USB\n"
|
2014-10-09 16:22:49 +02:00
|
|
|
" adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port\n"
|
2015-06-12 23:26:29 +02:00
|
|
|
"\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
"networking:\n"
|
|
|
|
" adb ppp <tty> [parameters] - Run PPP over USB.\n"
|
2009-06-08 21:40:30 +02:00
|
|
|
" Note: you should not automatically start a PPP connection.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1\n"
|
|
|
|
" [parameters] - Eg. defaultroute debug dump local notty usepeerdns\n"
|
|
|
|
"\n"
|
|
|
|
"adb sync notes: adb sync [ <directory> ]\n"
|
|
|
|
" <localdir> can be interpreted in several ways:\n"
|
|
|
|
"\n"
|
2015-03-16 22:58:32 +01:00
|
|
|
" - If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
"\n"
|
2015-03-16 22:58:32 +01:00
|
|
|
" - If it is \"system\", \"vendor\", \"oem\" or \"data\", only the corresponding partition\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" is updated.\n"
|
2010-02-16 21:18:29 +01:00
|
|
|
"\n"
|
2015-06-12 23:26:29 +02:00
|
|
|
"environment variables:\n"
|
2010-02-16 21:18:29 +01:00
|
|
|
" ADB_TRACE - Print debug information. A comma separated list of the following values\n"
|
|
|
|
" 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n"
|
|
|
|
" ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.\n"
|
|
|
|
" ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
static int usage() {
|
2009-03-04 04:32:55 +01:00
|
|
|
help();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:48:25 +01:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
2015-04-17 01:47:02 +02:00
|
|
|
// Implemented in sysdeps_win32.cpp.
|
2015-03-02 00:06:21 +01:00
|
|
|
void stdin_raw_init(int fd);
|
|
|
|
void stdin_raw_restore(int fd);
|
2014-11-17 23:48:25 +01:00
|
|
|
|
|
|
|
#else
|
2013-03-01 22:16:41 +01:00
|
|
|
static termios g_saved_terminal_state;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-03-01 22:16:41 +01:00
|
|
|
static void stdin_raw_init(int fd) {
|
|
|
|
if (tcgetattr(fd, &g_saved_terminal_state)) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-03-01 22:16:41 +01:00
|
|
|
termios tio;
|
|
|
|
if (tcgetattr(fd, &tio)) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-03-01 22:16:41 +01:00
|
|
|
cfmakeraw(&tio);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-03-01 22:16:41 +01:00
|
|
|
// No timeout but request at least one character per read.
|
2009-03-04 04:32:55 +01:00
|
|
|
tio.c_cc[VTIME] = 0;
|
|
|
|
tio.c_cc[VMIN] = 1;
|
|
|
|
|
2013-03-01 22:16:41 +01:00
|
|
|
tcsetattr(fd, TCSAFLUSH, &tio);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2013-03-01 22:16:41 +01:00
|
|
|
static void stdin_raw_restore(int fd) {
|
|
|
|
tcsetattr(fd, TCSAFLUSH, &g_saved_terminal_state);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-08 08:37:40 +02:00
|
|
|
static void read_and_dump(int fd) {
|
|
|
|
while (fd >= 0) {
|
2011-03-16 23:57:42 +01:00
|
|
|
D("read_and_dump(): pre adb_read(fd=%d)\n", fd);
|
2015-05-08 08:37:40 +02:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
int len = adb_read(fd, buf, sizeof(buf));
|
2011-03-16 23:57:42 +01:00
|
|
|
D("read_and_dump(): post adb_read(fd=%d): len=%d\n", fd, len);
|
2015-05-08 08:37:40 +02:00
|
|
|
if (len <= 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-09-22 07:18:40 +02:00
|
|
|
fwrite(buf, 1, len, stdout);
|
|
|
|
fflush(stdout);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
static void read_status_line(int fd, char* buf, size_t count)
|
|
|
|
{
|
|
|
|
count--;
|
|
|
|
while (count > 0) {
|
|
|
|
int len = adb_read(fd, buf, count);
|
|
|
|
if (len == 0) {
|
|
|
|
break;
|
|
|
|
} else if (len < 0) {
|
|
|
|
if (errno == EINTR) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf += len;
|
|
|
|
count -= len;
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
|
|
|
}
|
|
|
|
|
2011-04-21 21:53:28 +02:00
|
|
|
static void copy_to_file(int inFd, int outFd) {
|
2011-06-10 20:38:37 +02:00
|
|
|
const size_t BUFSIZE = 32 * 1024;
|
|
|
|
char* buf = (char*) malloc(BUFSIZE);
|
2015-04-22 04:39:52 +02:00
|
|
|
if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
|
2011-04-21 21:53:28 +02:00
|
|
|
int len;
|
2011-06-02 02:56:23 +02:00
|
|
|
long total = 0;
|
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 01:48:31 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
int old_stdin_mode = -1;
|
|
|
|
int old_stdout_mode = -1;
|
|
|
|
#endif
|
2011-04-21 21:53:28 +02:00
|
|
|
|
|
|
|
D("copy_to_file(%d -> %d)\n", inFd, outFd);
|
2014-11-17 23:48:25 +01:00
|
|
|
|
2014-05-27 03:30:43 +02:00
|
|
|
if (inFd == STDIN_FILENO) {
|
|
|
|
stdin_raw_init(STDIN_FILENO);
|
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 01:48:31 +02: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 03:30:43 +02:00
|
|
|
}
|
2014-11-17 23:48:25 +01: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 01:48:31 +02: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
|
|
|
|
|
2015-04-18 02:03:59 +02:00
|
|
|
while (true) {
|
2014-05-27 03:30:43 +02:00
|
|
|
if (inFd == STDIN_FILENO) {
|
|
|
|
len = unix_read(inFd, buf, BUFSIZE);
|
|
|
|
} else {
|
|
|
|
len = adb_read(inFd, buf, BUFSIZE);
|
|
|
|
}
|
2011-04-21 21:53:28 +02:00
|
|
|
if (len == 0) {
|
2011-06-10 20:38:37 +02:00
|
|
|
D("copy_to_file() : read 0 bytes; exiting\n");
|
2011-04-21 21:53:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
2011-06-10 20:38:37 +02:00
|
|
|
if (errno == EINTR) {
|
|
|
|
D("copy_to_file() : EINTR, retrying\n");
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-21 21:53:28 +02:00
|
|
|
D("copy_to_file() : error %d\n", errno);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-27 03:30:43 +02:00
|
|
|
if (outFd == STDOUT_FILENO) {
|
|
|
|
fwrite(buf, 1, len, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
} else {
|
|
|
|
adb_write(outFd, buf, len);
|
|
|
|
}
|
2011-06-02 02:56:23 +02:00
|
|
|
total += len;
|
2011-04-21 21:53:28 +02:00
|
|
|
}
|
2014-11-17 23:48:25 +01:00
|
|
|
|
2014-05-27 03:30:43 +02:00
|
|
|
if (inFd == STDIN_FILENO) {
|
|
|
|
stdin_raw_restore(STDIN_FILENO);
|
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 01:48:31 +02:00
|
|
|
#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");
|
|
|
|
}
|
2014-05-27 03:30:43 +02: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 01:48:31 +02:00
|
|
|
#endif
|
2014-11-17 23:48:25 +01:00
|
|
|
|
2011-06-02 02:56:23 +02:00
|
|
|
D("copy_to_file() finished after %lu bytes\n", total);
|
2011-06-10 20:38:37 +02:00
|
|
|
free(buf);
|
2011-04-21 21:53:28 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
static void *stdin_read_thread(void *x)
|
|
|
|
{
|
|
|
|
int fd, fdi;
|
|
|
|
unsigned char buf[1024];
|
|
|
|
int r, n;
|
|
|
|
int state = 0;
|
|
|
|
|
|
|
|
int *fds = (int*) x;
|
|
|
|
fd = fds[0];
|
|
|
|
fdi = fds[1];
|
|
|
|
free(fds);
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
/* fdi is really the client's stdin, so use read, not adb_read here */
|
2011-03-16 23:57:42 +01:00
|
|
|
D("stdin_read_thread(): pre unix_read(fdi=%d,...)\n", fdi);
|
2009-03-04 04:32:55 +01:00
|
|
|
r = unix_read(fdi, buf, 1024);
|
2011-03-16 23:57:42 +01:00
|
|
|
D("stdin_read_thread(): post unix_read(fdi=%d,...)\n", fdi);
|
2009-03-04 04:32:55 +01:00
|
|
|
if(r == 0) break;
|
|
|
|
if(r < 0) {
|
|
|
|
if(errno == EINTR) continue;
|
|
|
|
break;
|
|
|
|
}
|
2010-05-25 19:40:15 +02:00
|
|
|
for(n = 0; n < r; n++){
|
|
|
|
switch(buf[n]) {
|
|
|
|
case '\n':
|
|
|
|
state = 1;
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
state = 1;
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
if(state == 1) state++;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
if(state == 2) {
|
|
|
|
fprintf(stderr,"\n* disconnect *\n");
|
|
|
|
stdin_raw_restore(fdi);
|
|
|
|
exit(0);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2010-05-25 19:40:15 +02:00
|
|
|
default:
|
|
|
|
state = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r = adb_write(fd, buf, r);
|
|
|
|
if(r <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
static int interactive_shell() {
|
2015-04-29 17:35:59 +02:00
|
|
|
int fdi;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect("shell:", &error);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr,"error: %s\n", error.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fdi = 0; //dup(0);
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
int* fds = reinterpret_cast<int*>(malloc(sizeof(int) * 2));
|
2015-04-22 04:39:52 +02:00
|
|
|
if (fds == nullptr) {
|
|
|
|
fprintf(stderr, "couldn't allocate fds array: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
fds[0] = fd;
|
|
|
|
fds[1] = fdi;
|
|
|
|
|
|
|
|
stdin_raw_init(fdi);
|
2015-05-05 22:41:21 +02:00
|
|
|
|
|
|
|
adb_thread_create(stdin_read_thread, fds);
|
2009-03-04 04:32:55 +01:00
|
|
|
read_and_dump(fd);
|
|
|
|
stdin_raw_restore(fdi);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static std::string format_host_command(const char* command, TransportType type, const char* serial) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (serial) {
|
2015-04-29 21:28:13 +02:00
|
|
|
return android::base::StringPrintf("host-serial:%s:%s", serial, command);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
const char* prefix = "host";
|
|
|
|
if (type == kTransportUsb) {
|
|
|
|
prefix = "host-usb";
|
|
|
|
} else if (type == kTransportLocal) {
|
|
|
|
prefix = "host-local";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-04-29 21:28:13 +02:00
|
|
|
return android::base::StringPrintf("%s:%s", prefix, command);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
static int adb_download_buffer(const char *service, const char *fn, const void* data, unsigned sz,
|
2015-04-29 17:35:59 +02:00
|
|
|
bool show_progress)
|
2012-01-09 23:54:53 +01:00
|
|
|
{
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
int fd = adb_connect(android::base::StringPrintf("%s:%d", service, sz), &error);
|
2015-04-29 17:35:59 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr,"error: %s\n", error.c_str());
|
2012-01-09 23:54:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt = CHUNK_SIZE;
|
2015-01-25 23:40:16 +01:00
|
|
|
opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
|
2012-01-09 23:54:53 +01:00
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
unsigned total = sz;
|
2015-02-26 02:51:28 +01:00
|
|
|
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
|
2012-01-09 23:54:53 +01:00
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
if (show_progress) {
|
2015-07-28 06:21:39 +02:00
|
|
|
const char* x = strrchr(service, ':');
|
|
|
|
if (x) service = x + 1;
|
2012-01-09 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
while (sz > 0) {
|
2012-01-09 23:54:53 +01:00
|
|
|
unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
|
2015-04-29 17:35:59 +02:00
|
|
|
if (!WriteFdExactly(fd, ptr, xfer)) {
|
|
|
|
std::string error;
|
|
|
|
adb_status(fd, &error);
|
|
|
|
fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
|
2012-01-09 23:54:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sz -= xfer;
|
|
|
|
ptr += xfer;
|
2015-04-29 17:35:59 +02:00
|
|
|
if (show_progress) {
|
2013-03-05 07:37:32 +01:00
|
|
|
printf("sending: '%s' %4d%% \r", fn, (int)(100LL - ((100LL * sz) / (total))));
|
2012-01-09 23:54:53 +01:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 17:35:59 +02:00
|
|
|
if (show_progress) {
|
2012-01-09 23:54:53 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
if (!adb_status(fd, &error)) {
|
|
|
|
fprintf(stderr,"* error response '%s' *\n", error.c_str());
|
2012-01-09 23:54:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-27 00:35:36 +02: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-17 22:57:15 +02:00
|
|
|
static int adb_sideload_host(const char* fn) {
|
2014-06-27 00:35:36 +02:00
|
|
|
unsigned sz;
|
|
|
|
size_t xfer = 0;
|
|
|
|
int status;
|
2015-02-26 02:51:28 +01:00
|
|
|
int last_percent = -1;
|
|
|
|
int opt = SIDELOAD_HOST_BLOCK_SIZE;
|
2014-06-27 00:35:36 +02:00
|
|
|
|
|
|
|
printf("loading: '%s'", fn);
|
|
|
|
fflush(stdout);
|
2015-02-26 02:51:28 +01:00
|
|
|
uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz));
|
2014-06-27 00:35:36 +02:00
|
|
|
if (data == 0) {
|
|
|
|
printf("\n");
|
|
|
|
fprintf(stderr, "* cannot read '%s' *\n", fn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
std::string service =
|
|
|
|
android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
int fd = adb_connect(service, &error);
|
2014-06-27 00:35:36 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
// Try falling back to the older sideload method. Maybe this
|
|
|
|
// is an older device that doesn't support sideload-host.
|
|
|
|
printf("\n");
|
2015-04-29 17:35:59 +02:00
|
|
|
status = adb_download_buffer("sideload", fn, data, sz, true);
|
2014-06-27 00:35:36 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:40:16 +01:00
|
|
|
opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
|
2014-06-27 00:35:36 +02:00
|
|
|
|
2015-04-18 02:03:59 +02:00
|
|
|
while (true) {
|
2015-04-29 21:28:13 +02:00
|
|
|
char buf[9];
|
2015-02-25 06:26:58 +01:00
|
|
|
if (!ReadFdExactly(fd, buf, 8)) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
|
2014-06-27 00:35:36 +02:00
|
|
|
status = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
2015-04-29 21:28:13 +02:00
|
|
|
buf[8] = '\0';
|
2014-06-27 00:35:36 +02:00
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
if (strcmp("DONEDONE", buf) == 0) {
|
2014-06-27 00:35:36 +02:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int block = strtol(buf, NULL, 10);
|
|
|
|
|
|
|
|
size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
|
|
|
|
if (offset >= sz) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "* attempt to read block %d past end\n", block);
|
2014-06-27 00:35:36 +02:00
|
|
|
status = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
uint8_t* start = data + offset;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-25 06:26:58 +01:00
|
|
|
if(!WriteFdExactly(fd, start, to_write)) {
|
2015-04-29 17:35:59 +02:00
|
|
|
adb_status(fd, &error);
|
|
|
|
fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
|
2014-06-27 00:35:36 +02:00
|
|
|
status = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-07 23:12:41 +02:00
|
|
|
printf("\rTotal xfer: %.2fx%*s\n", (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
|
2014-06-27 00:35:36 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (fd >= 0) adb_close(fd);
|
|
|
|
free(data);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01: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-17 22:57:15 +02:00
|
|
|
static int ppp(int argc, const char** argv) {
|
2014-11-11 18:24:11 +01:00
|
|
|
#if defined(_WIN32)
|
2009-03-04 04:32:55 +01: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 02:51:28 +01:00
|
|
|
const char* adb_service_name = argv[1];
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect(adb_service_name, &error);
|
|
|
|
if (fd < 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
|
2015-04-29 17:35:59 +02:00
|
|
|
adb_service_name, error.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
pid_t pid = fork();
|
2009-03-04 04:32:55 +01: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-11 18:24:11 +01:00
|
|
|
#endif /* !defined(_WIN32) */
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static bool wait_for_device(const char* service, TransportType t, const char* serial) {
|
2015-05-05 04:29:32 +02: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.
|
|
|
|
if (strcmp(service, "wait-for-device") == 0) {
|
|
|
|
if (t == kTransportUsb) {
|
|
|
|
service = "wait-for-usb";
|
|
|
|
} else if (t == kTransportLocal) {
|
|
|
|
service = "wait-for-local";
|
|
|
|
} else {
|
|
|
|
service = "wait-for-any";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmd = format_host_command(service, t, serial);
|
2015-05-30 02:55:19 +02:00
|
|
|
return adb_command(cmd);
|
2015-05-05 04:29:32 +02:00
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static int send_shell_command(TransportType transport_type, const char* serial,
|
2015-04-17 19:59:34 +02:00
|
|
|
const std::string& command) {
|
|
|
|
int fd;
|
|
|
|
while (true) {
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
fd = adb_connect(command, &error);
|
2015-04-29 17:35:59 +02:00
|
|
|
if (fd >= 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
2015-04-29 17:35:59 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"- waiting for device -\n");
|
|
|
|
adb_sleep_ms(1000);
|
2015-05-05 04:29:32 +02:00
|
|
|
wait_for_device("wait-for-device", transport_type, serial);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
read_and_dump(fd);
|
2015-04-17 19:59:34 +02:00
|
|
|
int rc = adb_close(fd);
|
|
|
|
if (rc) {
|
2009-03-04 04:32:55 +01:00
|
|
|
perror("close");
|
2015-04-17 19:59:34 +02:00
|
|
|
}
|
|
|
|
return rc;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static int logcat(TransportType transport, const char* serial, int argc, const char** argv) {
|
2015-04-17 19:59:34 +02:00
|
|
|
char* log_tags = getenv("ANDROID_LOG_TAGS");
|
|
|
|
std::string quoted = escape_arg(log_tags == nullptr ? "" : log_tags);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-17 19:59:34 +02:00
|
|
|
std::string cmd = "shell:export ANDROID_LOG_TAGS=\"" + quoted + "\"; exec logcat";
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-06-10 20:31:24 +02:00
|
|
|
if (!strcmp(argv[0], "longcat")) {
|
2015-04-17 19:59:34 +02:00
|
|
|
cmd += " -v long";
|
2011-11-30 22:00:33 +01:00
|
|
|
}
|
|
|
|
|
2015-04-18 05:11:08 +02:00
|
|
|
--argc;
|
|
|
|
++argv;
|
2015-04-17 19:59:34 +02:00
|
|
|
while (argc-- > 0) {
|
2015-04-18 05:11:08 +02:00
|
|
|
cmd += " " + escape_arg(*argv++);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-22 02:58:55 +02:00
|
|
|
return send_shell_command(transport, serial, cmd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
static int backup(int argc, const char** argv) {
|
2015-04-18 05:11:08 +02:00
|
|
|
const char* filename = "./backup.ab";
|
2011-04-21 21:53:28 +02:00
|
|
|
|
2011-06-02 02:56:23 +02:00
|
|
|
/* find, extract, and use any -f argument */
|
2015-04-18 05:11:08 +02:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2011-06-02 02:56:23 +02:00
|
|
|
if (!strcmp("-f", argv[i])) {
|
|
|
|
if (i == argc-1) {
|
|
|
|
fprintf(stderr, "adb: -f passed with no filename\n");
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
filename = argv[i+1];
|
2015-04-18 05:11:08 +02:00
|
|
|
for (int j = i+2; j <= argc; ) {
|
2011-06-02 02:56:23 +02:00
|
|
|
argv[i++] = argv[j++];
|
|
|
|
}
|
|
|
|
argc -= 2;
|
|
|
|
argv[argc] = NULL;
|
|
|
|
}
|
2011-04-21 21:53:28 +02:00
|
|
|
}
|
|
|
|
|
2011-08-23 02:12:08 +02:00
|
|
|
/* bare "adb backup" or "adb backup -f filename" are not valid invocations */
|
|
|
|
if (argc < 2) return usage();
|
|
|
|
|
2011-12-09 04:04:34 +01:00
|
|
|
adb_unlink(filename);
|
2014-04-30 18:10:31 +02:00
|
|
|
mkdirs(filename);
|
2015-04-18 05:11:08 +02:00
|
|
|
int outFd = adb_creat(filename, 0640);
|
2011-04-21 21:53:28 +02:00
|
|
|
if (outFd < 0) {
|
|
|
|
fprintf(stderr, "adb: unable to open file %s\n", filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-18 05:11:08 +02:00
|
|
|
std::string cmd = "backup:";
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
while (argc-- > 0) {
|
|
|
|
cmd += " " + escape_arg(*argv++);
|
2011-04-21 21:53:28 +02:00
|
|
|
}
|
|
|
|
|
2015-04-18 05:11:08 +02:00
|
|
|
D("backup. filename=%s cmd=%s\n", filename, cmd.c_str());
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
int fd = adb_connect(cmd, &error);
|
2011-04-21 21:53:28 +02:00
|
|
|
if (fd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "adb: unable to connect for backup: %s\n", error.c_str());
|
2011-04-21 21:53:28 +02:00
|
|
|
adb_close(outFd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-07 00:43:03 +01:00
|
|
|
printf("Now unlock your device and confirm the backup operation.\n");
|
2011-04-21 21:53:28 +02:00
|
|
|
copy_to_file(fd, outFd);
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
adb_close(outFd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
static int restore(int argc, const char** argv) {
|
2011-05-18 00:52:54 +02:00
|
|
|
if (argc != 2) return usage();
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
const char* filename = argv[1];
|
|
|
|
int tarFd = adb_open(filename, O_RDONLY);
|
2011-05-18 00:52:54 +02:00
|
|
|
if (tarFd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "adb: unable to open file %s: %s\n", filename, strerror(errno));
|
2011-05-18 00:52:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect("restore:", &error);
|
2011-05-18 00:52:54 +02:00
|
|
|
if (fd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "adb: unable to connect for restore: %s\n", error.c_str());
|
2011-05-18 00:52:54 +02:00
|
|
|
adb_close(tarFd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-07 00:43:03 +01:00
|
|
|
printf("Now unlock your device and confirm the restore operation.\n");
|
2011-05-18 00:52:54 +02:00
|
|
|
copy_to_file(tarFd, fd);
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
adb_close(tarFd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01: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 02:42:01 +02:00
|
|
|
static std::string find_product_out_path(const std::string& hint) {
|
|
|
|
if (hint.empty()) {
|
2015-04-17 22:57:15 +02:00
|
|
|
return "";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
// If it's already absolute, don't bother doing any work.
|
2015-07-31 02:42:01 +02:00
|
|
|
if (adb_is_absolute_host_path(hint.c_str())) {
|
2015-04-17 22:57:15 +02:00
|
|
|
return hint;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-17 22:57:15 +02:00
|
|
|
// If there are any slashes in it, assume it's a relative path;
|
|
|
|
// make it absolute.
|
2015-07-31 02:42:01 +02:00
|
|
|
if (hint.find_first_of(OS_PATH_SEPARATORS) != std::string::npos) {
|
2015-04-18 02:03:59 +02:00
|
|
|
std::string cwd;
|
|
|
|
if (!getcwd(&cwd)) {
|
|
|
|
fprintf(stderr, "adb: getcwd failed: %s\n", strerror(errno));
|
2015-04-17 22:57:15 +02:00
|
|
|
return "";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-31 02:42:01 +02:00
|
|
|
return android::base::StringPrintf("%s%c%s", cwd.c_str(), OS_PATH_SEPARATOR, hint.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-17 22:57:15 +02: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 02:03:59 +02:00
|
|
|
char* top = getenv("ANDROID_BUILD_TOP");
|
2015-04-17 22:57:15 +02:00
|
|
|
if (top == nullptr) {
|
2015-04-18 02:03:59 +02:00
|
|
|
fprintf(stderr, "adb: ANDROID_BUILD_TOP not set!\n");
|
2015-04-17 22:57:15 +02:00
|
|
|
return "";
|
|
|
|
}
|
2015-04-18 02:03:59 +02:00
|
|
|
|
|
|
|
std::string path = top;
|
2015-04-17 22:57:15 +02: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 02:42:01 +02:00
|
|
|
"\"%s\" doesn't exist\n", hint.c_str(), path.c_str());
|
2015-04-18 02:03:59 +02:00
|
|
|
return "";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-04-17 22:57:15 +02:00
|
|
|
return path;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
static void parse_push_pull_args(const char **arg, int narg, char const **path1,
|
|
|
|
char const **path2, int *show_progress,
|
|
|
|
int *copy_attrs) {
|
2014-03-12 01:55:59 +01:00
|
|
|
*show_progress = 0;
|
2013-04-19 21:41:09 +02:00
|
|
|
*copy_attrs = 0;
|
2014-03-12 01:55:59 +01:00
|
|
|
|
2013-04-19 21:41:09 +02:00
|
|
|
while (narg > 0) {
|
|
|
|
if (!strcmp(*arg, "-p")) {
|
|
|
|
*show_progress = 1;
|
|
|
|
} else if (!strcmp(*arg, "-a")) {
|
|
|
|
*copy_attrs = 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2014-03-12 01:55:59 +01:00
|
|
|
++arg;
|
|
|
|
--narg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (narg > 0) {
|
|
|
|
*path1 = *arg;
|
|
|
|
++arg;
|
|
|
|
--narg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (narg > 0) {
|
|
|
|
*path2 = *arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
static int adb_connect_command(const std::string& command) {
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect(command, &error);
|
2015-05-08 08:37:40 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
return 1;
|
2015-03-29 20:22:34 +02:00
|
|
|
}
|
2015-05-08 08:37:40 +02:00
|
|
|
read_and_dump(fd);
|
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
2015-03-29 20:22:34 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02: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-05-02 02:04:38 +02:00
|
|
|
int adb_commandline(int argc, const char **argv) {
|
2009-03-04 04:32:55 +01:00
|
|
|
int no_daemon = 0;
|
|
|
|
int is_daemon = 0;
|
2011-01-31 14:23:56 +01:00
|
|
|
int is_server = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
int r;
|
2015-05-05 22:10:43 +02:00
|
|
|
TransportType transport_type = kTransportAny;
|
2015-08-07 19:10:29 +02:00
|
|
|
int ack_reply_fd = -1;
|
|
|
|
|
2015-04-17 22:57:15 +02: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 04:32:55 +01:00
|
|
|
}
|
|
|
|
// TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
const char* serial = getenv("ANDROID_SERIAL");
|
2009-05-07 21:48:03 +02:00
|
|
|
|
2010-04-19 13:21:12 +02:00
|
|
|
/* Validate and assign the server port */
|
2015-05-05 22:10:43 +02:00
|
|
|
const char* server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
|
2010-04-19 13:21:12 +02:00
|
|
|
int server_port = DEFAULT_ADB_PORT;
|
|
|
|
if (server_port_str && strlen(server_port_str) > 0) {
|
2015-08-07 19:10:29 +02:00
|
|
|
server_port = strtol(server_port_str, nullptr, 0);
|
2012-11-14 19:16:17 +01:00
|
|
|
if (server_port <= 0 || server_port > 65535) {
|
2010-04-19 13:21:12 +02:00
|
|
|
fprintf(stderr,
|
2015-08-12 02:05:02 +02:00
|
|
|
"adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number less than 65536. Got \"%s\"\n",
|
2010-04-19 13:21:12 +02:00
|
|
|
server_port_str);
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* modifiers and flags */
|
2014-12-06 02:37:24 +01:00
|
|
|
while (argc > 0) {
|
|
|
|
if (!strcmp(argv[0],"server")) {
|
2011-01-31 14:23:56 +01:00
|
|
|
is_server = 1;
|
2014-12-06 02:37:24 +01:00
|
|
|
} else if (!strcmp(argv[0],"nodaemon")) {
|
2009-03-04 04:32:55 +01: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-07 19:10:29 +02: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-08-09 00:07:07 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
|
|
|
|
if ((GetStdHandle(STD_INPUT_HANDLE) == ack_reply_handle) ||
|
|
|
|
(GetStdHandle(STD_OUTPUT_HANDLE) == ack_reply_handle) ||
|
|
|
|
(GetStdHandle(STD_ERROR_HANDLE) == ack_reply_handle)) {
|
|
|
|
#else
|
2015-08-07 19:10:29 +02:00
|
|
|
if (ack_reply_fd <= 2) { // Disallow stdin, stdout, and stderr.
|
2015-08-09 00:07:07 +02:00
|
|
|
#endif
|
2015-08-07 19:10:29 +02:00
|
|
|
fprintf(stderr, "adb: invalid reply fd \"%s\"\n", reply_fd_str);
|
|
|
|
return usage();
|
|
|
|
}
|
2014-12-06 02:37:24 +01:00
|
|
|
} else if (!strncmp(argv[0], "-p", 2)) {
|
2015-07-31 22:18:22 +02:00
|
|
|
const char* product = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
if (argv[0][2] == '\0') {
|
|
|
|
if (argc < 2) return usage();
|
|
|
|
product = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
} else {
|
2012-08-05 01:40:50 +02:00
|
|
|
product = argv[0] + 2;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-31 22:18:22 +02:00
|
|
|
gProductOutPath = find_product_out_path(product);
|
2015-04-17 22:57:15 +02:00
|
|
|
if (gProductOutPath.empty()) {
|
|
|
|
fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product);
|
2009-03-04 04:32:55 +01: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 02:37:24 +01:00
|
|
|
if (argc < 2 || argv[0][2] != '\0') return usage();
|
2009-03-04 04:32:55 +01:00
|
|
|
serial = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[0],"-d")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
transport_type = kTransportUsb;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if (!strcmp(argv[0],"-e")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
transport_type = kTransportLocal;
|
2012-11-14 19:16:17 +01:00
|
|
|
} else if (!strcmp(argv[0],"-a")) {
|
|
|
|
gListenAll = 1;
|
2014-12-06 02:37:24 +01:00
|
|
|
} else if (!strncmp(argv[0], "-H", 2)) {
|
2012-11-14 19:16:17 +01:00
|
|
|
const char *hostname = NULL;
|
|
|
|
if (argv[0][2] == '\0') {
|
|
|
|
if (argc < 2) return usage();
|
|
|
|
hostname = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
} else {
|
|
|
|
hostname = argv[0] + 2;
|
|
|
|
}
|
|
|
|
adb_set_tcp_name(hostname);
|
|
|
|
|
2014-12-06 02:37:24 +01:00
|
|
|
} else if (!strncmp(argv[0], "-P", 2)) {
|
2012-11-14 19:16:17 +01: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;
|
|
|
|
}
|
|
|
|
if (strlen(server_port_str) > 0) {
|
|
|
|
server_port = (int) strtol(server_port_str, NULL, 0);
|
|
|
|
if (server_port <= 0 || server_port > 65535) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"adb: port number must be a positive number less than 65536. Got \"%s\"\n",
|
|
|
|
server_port_str);
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"adb: port number must be a positive number less than 65536. Got empty string.\n");
|
|
|
|
return usage();
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
|
|
|
/* out of recognized modifiers and flags */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
adb_set_transport(transport_type, serial);
|
2010-04-19 13:21:12 +02:00
|
|
|
adb_set_tcp_specifics(server_port);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2011-01-31 14:23:56 +01:00
|
|
|
if (is_server) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (no_daemon || is_daemon) {
|
2015-08-09 00:07:07 +02:00
|
|
|
if (is_daemon && (ack_reply_fd == -1)) {
|
2015-08-07 19:10:29 +02:00
|
|
|
fprintf(stderr, "reply fd for adb server to client communication not specified.\n");
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
r = adb_main(is_daemon, server_port, ack_reply_fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2010-04-19 13:21:12 +02:00
|
|
|
r = launch_server(server_port);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:37:24 +01:00
|
|
|
if (r) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"* could not start server *\n");
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-12-06 02:37:24 +01:00
|
|
|
if (argc == 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
|
2014-12-06 02:32:46 +01:00
|
|
|
/* handle wait-for-* prefix */
|
|
|
|
if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
|
2015-02-26 02:51:28 +01:00
|
|
|
const char* service = argv[0];
|
2014-12-06 02:32:46 +01:00
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
if (!wait_for_device(service, transport_type, serial)) {
|
2014-12-06 02:32:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:29:32 +02:00
|
|
|
// Allow a command to be run after wait-for-device,
|
|
|
|
// e.g. 'adb wait-for-device shell'.
|
2014-12-06 02:32:46 +01:00
|
|
|
if (argc == 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall through */
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-12-06 02:32:46 +01:00
|
|
|
/* adb_connect() commands */
|
2014-12-06 02:37:24 +01:00
|
|
|
if (!strcmp(argv[0], "devices")) {
|
2015-02-26 02:51:28 +01:00
|
|
|
const char *listopt;
|
2015-04-29 21:28:13 +02:00
|
|
|
if (argc < 2) {
|
2012-04-20 20:21:14 +02:00
|
|
|
listopt = "";
|
2015-04-29 21:28:13 +02:00
|
|
|
} else if (argc == 2 && !strcmp(argv[1], "-l")) {
|
2012-04-20 20:21:14 +02:00
|
|
|
listopt = argv[1];
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-04-29 21:28:13 +02:00
|
|
|
fprintf(stderr, "Usage: adb devices [-l]\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-04-29 21:28:13 +02: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 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "connect")) {
|
2009-08-25 00:58:40 +02:00
|
|
|
if (argc != 2) {
|
2010-05-24 16:44:35 +02:00
|
|
|
fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
|
2009-08-25 00:58:40 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-04-29 21:28:13 +02:00
|
|
|
|
|
|
|
std::string query = android::base::StringPrintf("host:connect:%s", argv[1]);
|
|
|
|
return adb_query_command(query);
|
2010-05-24 16:44:35 +02:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "disconnect")) {
|
2010-05-24 16:44:35 +02:00
|
|
|
if (argc > 2) {
|
|
|
|
fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-04-29 21:28:13 +02:00
|
|
|
|
|
|
|
std::string query = android::base::StringPrintf("host:disconnect:%s",
|
|
|
|
(argc == 2) ? argv[1] : "");
|
|
|
|
return adb_query_command(query);
|
2009-08-25 00:58:40 +02:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "emu")) {
|
2015-05-07 01:13:42 +02:00
|
|
|
return adb_send_emulator_command(argc, argv, serial);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
|
2010-08-19 07:10:18 +02:00
|
|
|
char h = (argv[0][0] == 'h');
|
|
|
|
|
|
|
|
if (h) {
|
|
|
|
printf("\x1b[41;33m");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2014-12-06 02:37:24 +01:00
|
|
|
if (argc < 2) {
|
2011-03-16 23:57:42 +01:00
|
|
|
D("starting interactive shell\n");
|
2010-08-19 07:10:18 +02:00
|
|
|
r = interactive_shell();
|
|
|
|
if (h) {
|
|
|
|
printf("\x1b[0m");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
return r;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-17 19:59:34 +02:00
|
|
|
std::string cmd = "shell:";
|
2015-05-05 04:29:32 +02:00
|
|
|
--argc;
|
|
|
|
++argv;
|
2014-05-27 03:30:43 +02:00
|
|
|
while (argc-- > 0) {
|
2015-05-05 04:29:32 +02:00
|
|
|
// We don't escape here, just like ssh(1). http://b/20564385.
|
|
|
|
cmd += *argv++;
|
|
|
|
if (*argv) cmd += " ";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-17 19:59:34 +02:00
|
|
|
while (true) {
|
|
|
|
D("interactive shell loop. cmd=%s\n", cmd.c_str());
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
int fd = adb_connect(cmd, &error);
|
2015-04-17 19:59:34 +02:00
|
|
|
int r;
|
2014-12-06 02:37:24 +01:00
|
|
|
if (fd >= 0) {
|
2011-03-16 23:57:42 +01:00
|
|
|
D("about to read_and_dump(fd=%d)\n", fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
read_and_dump(fd);
|
2011-03-16 23:57:42 +01:00
|
|
|
D("read_and_dump() done.\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
|
|
|
r = 0;
|
|
|
|
} else {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr,"error: %s\n", error.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
r = -1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 21:50:26 +02:00
|
|
|
if (h) {
|
|
|
|
printf("\x1b[0m");
|
|
|
|
fflush(stdout);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-05-05 21:50:26 +02:00
|
|
|
D("interactive shell loop. return r=%d\n", r);
|
|
|
|
return r;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
|
2014-05-27 03:30:43 +02:00
|
|
|
int exec_in = !strcmp(argv[0], "exec-in");
|
|
|
|
|
2015-04-17 19:59:34 +02:00
|
|
|
std::string cmd = "exec:";
|
|
|
|
cmd += argv[1];
|
2014-05-27 03:30:43 +02:00
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
|
|
|
while (argc-- > 0) {
|
2015-04-18 05:11:08 +02:00
|
|
|
cmd += " " + escape_arg(*argv++);
|
2014-05-27 03:30:43 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
int fd = adb_connect(cmd, &error);
|
2014-05-27 03:30:43 +02:00
|
|
|
if (fd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
2014-05-27 03:30:43 +02: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 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "kill-server")) {
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
|
|
|
int fd = _adb_connect("host:kill", &error);
|
2015-08-12 02:05:02 +02:00
|
|
|
if (fd == -2) {
|
|
|
|
// Failed to make network connection to server. Don't output the
|
|
|
|
// network error since that is expected.
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"* server not running *\n");
|
2015-08-12 02:05:02 +02: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 04:32:55 +01:00
|
|
|
return 1;
|
2015-08-12 02:05:02 +02:00
|
|
|
} else {
|
|
|
|
// Successfully connected, kill command sent, okay status came back.
|
|
|
|
// Server should exit() in a moment, if not already.
|
|
|
|
adb_close(fd);
|
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "sideload")) {
|
|
|
|
if (argc != 2) return usage();
|
2014-06-27 00:35:36 +02:00
|
|
|
if (adb_sideload_host(argv[1])) {
|
2012-01-09 23:54:53 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-07-22 01:13:40 +02:00
|
|
|
else if (!strcmp(argv[0], "tcpip") && argc > 1) {
|
|
|
|
return adb_connect_command(android::base::StringPrintf("tcpip:%s", argv[1]));
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "remount") ||
|
|
|
|
!strcmp(argv[0], "reboot") ||
|
|
|
|
!strcmp(argv[0], "reboot-bootloader") ||
|
|
|
|
!strcmp(argv[0], "usb") ||
|
|
|
|
!strcmp(argv[0], "root") ||
|
2014-10-06 19:57:20 +02:00
|
|
|
!strcmp(argv[0], "unroot") ||
|
2014-12-06 02:32:46 +01:00
|
|
|
!strcmp(argv[0], "disable-verity") ||
|
|
|
|
!strcmp(argv[0], "enable-verity")) {
|
2015-05-08 08:37:40 +02:00
|
|
|
std::string command;
|
2015-03-29 20:22:34 +02:00
|
|
|
if (!strcmp(argv[0], "reboot-bootloader")) {
|
2015-05-08 08:37:40 +02:00
|
|
|
command = "reboot:bootloader";
|
2015-03-29 20:22:34 +02:00
|
|
|
} else if (argc > 1) {
|
2015-05-08 08:37:40 +02:00
|
|
|
command = android::base::StringPrintf("%s:%s", argv[0], argv[1]);
|
2015-03-29 20:22:34 +02:00
|
|
|
} else {
|
2015-05-08 08:37:40 +02:00
|
|
|
command = android::base::StringPrintf("%s:", argv[0]);
|
2009-03-13 21:04:37 +01:00
|
|
|
}
|
2015-03-29 20:22:34 +02:00
|
|
|
return adb_connect_command(command);
|
2009-03-13 21:04:37 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "bugreport")) {
|
2010-01-20 22:50:36 +01:00
|
|
|
if (argc != 1) return usage();
|
2015-05-05 22:10:43 +02:00
|
|
|
return send_shell_command(transport_type, serial, "shell:bugreport");
|
2009-09-03 20:54:58 +02:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
|
2015-05-30 02:55:19 +02:00
|
|
|
bool reverse = !strcmp(argv[0], "reverse");
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) return usage();
|
2012-11-14 15:01:55 +01:00
|
|
|
|
|
|
|
// Determine the <host-prefix> for this command.
|
2015-05-30 02:55:19 +02:00
|
|
|
std::string host_prefix;
|
2013-03-21 21:07:42 +01:00
|
|
|
if (reverse) {
|
2015-05-30 02:55:19 +02:00
|
|
|
host_prefix = "reverse";
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2013-03-21 21:07:42 +01:00
|
|
|
if (serial) {
|
2015-05-30 02:55:19 +02:00
|
|
|
host_prefix = android::base::StringPrintf("host-serial:%s", serial);
|
2015-05-05 22:10:43 +02:00
|
|
|
} else if (transport_type == kTransportUsb) {
|
2015-05-30 02:55:19 +02:00
|
|
|
host_prefix = "host-usb";
|
2015-05-05 22:10:43 +02:00
|
|
|
} else if (transport_type == kTransportLocal) {
|
2015-05-30 02:55:19 +02:00
|
|
|
host_prefix = "host-local";
|
2013-03-21 21:07:42 +01:00
|
|
|
} else {
|
2015-05-30 02:55:19 +02:00
|
|
|
host_prefix = "host";
|
2012-11-14 15:01:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
std::string cmd;
|
|
|
|
if (strcmp(argv[0], "--list") == 0) {
|
2015-05-02 02:04:38 +02:00
|
|
|
if (argc != 1) return usage();
|
2015-05-30 02:55:19 +02: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 02:04:38 +02:00
|
|
|
if (argc != 2) return usage();
|
2015-05-30 02:55:19 +02:00
|
|
|
cmd = host_prefix + ":killforward:" + argv[1];
|
|
|
|
} else if (strcmp(argv[0], "--no-rebind") == 0) {
|
|
|
|
// forward --no-rebind <local> <remote>
|
2015-05-02 02:04:38 +02:00
|
|
|
if (argc != 3) return usage();
|
2015-05-30 02:55:19 +02:00
|
|
|
cmd = host_prefix + ":forward:norebind:" + argv[1] + ";" + argv[2];
|
|
|
|
} else {
|
|
|
|
// forward <local> <remote>
|
|
|
|
if (argc != 2) return usage();
|
|
|
|
cmd = host_prefix + ":forward:" + argv[0] + ";" + argv[1];
|
2012-11-14 15:01:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
return adb_command(cmd) ? 0 : 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
/* do_sync_*() commands */
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "ls")) {
|
|
|
|
if (argc != 2) return usage();
|
2009-03-04 04:32:55 +01:00
|
|
|
return do_sync_ls(argv[1]);
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "push")) {
|
2014-03-12 01:55:59 +01:00
|
|
|
int show_progress = 0;
|
2013-04-19 21:41:09 +02:00
|
|
|
int copy_attrs = 0; // unused
|
2014-03-12 01:55:59 +01:00
|
|
|
const char* lpath = NULL, *rpath = NULL;
|
|
|
|
|
2013-04-19 21:41:09 +02:00
|
|
|
parse_push_pull_args(&argv[1], argc - 1, &lpath, &rpath, &show_progress, ©_attrs);
|
2014-03-12 01:55:59 +01:00
|
|
|
|
|
|
|
if ((lpath != NULL) && (rpath != NULL)) {
|
2014-06-10 02:30:57 +02:00
|
|
|
return do_sync_push(lpath, rpath, show_progress);
|
2014-03-12 01:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return usage();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "pull")) {
|
2014-03-12 01:55:59 +01:00
|
|
|
int show_progress = 0;
|
2013-04-19 21:41:09 +02:00
|
|
|
int copy_attrs = 0;
|
2014-03-12 01:55:59 +01:00
|
|
|
const char* rpath = NULL, *lpath = ".";
|
|
|
|
|
2013-04-19 21:41:09 +02:00
|
|
|
parse_push_pull_args(&argv[1], argc - 1, &rpath, &lpath, &show_progress, ©_attrs);
|
2014-03-12 01:55:59 +01:00
|
|
|
|
|
|
|
if (rpath != NULL) {
|
2013-04-19 21:41:09 +02:00
|
|
|
return do_sync_pull(rpath, lpath, show_progress, copy_attrs);
|
2010-01-05 22:42:25 +01:00
|
|
|
}
|
2014-03-12 01:55:59 +01:00
|
|
|
|
|
|
|
return usage();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "install")) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (argc < 2) return usage();
|
2015-05-05 22:10:43 +02:00
|
|
|
return install_app(transport_type, serial, argc, argv);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "install-multiple")) {
|
2014-06-10 02:30:57 +02:00
|
|
|
if (argc < 2) return usage();
|
2015-05-05 22:10:43 +02:00
|
|
|
return install_multiple_app(transport_type, serial, argc, argv);
|
2014-06-10 02:30:57 +02:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "uninstall")) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (argc < 2) return usage();
|
2015-05-05 22:10:43 +02:00
|
|
|
return uninstall_app(transport_type, serial, argc, argv);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "sync")) {
|
2015-04-21 19:17:07 +02:00
|
|
|
std::string src;
|
2015-04-17 22:57:15 +02:00
|
|
|
bool list_only = false;
|
2014-12-06 02:37:24 +01:00
|
|
|
if (argc < 2) {
|
2015-04-17 22:57:15 +02:00
|
|
|
// No local path was specified.
|
2015-04-21 19:17:07 +02:00
|
|
|
src = "";
|
2010-02-22 15:36:49 +01:00
|
|
|
} else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
|
2015-04-21 19:17:07 +02:00
|
|
|
list_only = true;
|
2010-02-22 15:36:49 +01:00
|
|
|
if (argc == 3) {
|
2015-04-21 19:17:07 +02:00
|
|
|
src = argv[2];
|
2010-02-22 15:36:49 +01:00
|
|
|
} else {
|
2015-04-21 19:17:07 +02:00
|
|
|
src = "";
|
2010-02-22 15:36:49 +01:00
|
|
|
}
|
2014-12-06 02:37:24 +01:00
|
|
|
} else if (argc == 2) {
|
2015-04-17 22:57:15 +02:00
|
|
|
// A local path or "android"/"data" arg was specified.
|
2015-04-21 19:17:07 +02:00
|
|
|
src = argv[1];
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
|
|
|
return usage();
|
|
|
|
}
|
2015-04-17 22:57:15 +02:00
|
|
|
|
2015-04-21 19:17:07 +02:00
|
|
|
if (src != "" &&
|
|
|
|
src != "system" && src != "data" && src != "vendor" && src != "oem") {
|
2015-04-17 22:57:15 +02: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");
|
|
|
|
|
|
|
|
int rc = 0;
|
2015-04-21 19:17:07 +02:00
|
|
|
if (rc == 0 && (src.empty() || src == "system")) {
|
|
|
|
rc = do_sync_sync(system_src_path, "/system", list_only);
|
2015-04-17 22:57:15 +02:00
|
|
|
}
|
2015-04-21 19:17:07 +02:00
|
|
|
if (rc == 0 && (src.empty() || src == "vendor") && directory_exists(vendor_src_path)) {
|
|
|
|
rc = do_sync_sync(vendor_src_path, "/vendor", list_only);
|
2015-04-17 22:57:15 +02:00
|
|
|
}
|
2015-04-21 19:17:07 +02:00
|
|
|
if (rc == 0 && (src.empty() || src == "oem") && directory_exists(oem_src_path)) {
|
|
|
|
rc = do_sync_sync(oem_src_path, "/oem", list_only);
|
2015-04-17 22:57:15 +02:00
|
|
|
}
|
2015-04-21 19:17:07 +02:00
|
|
|
if (rc == 0 && (src.empty() || src == "data")) {
|
|
|
|
rc = do_sync_sync(data_src_path, "/data", list_only);
|
2015-04-17 22:57:15 +02:00
|
|
|
}
|
|
|
|
return rc;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
/* passthrough commands */
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0],"get-state") ||
|
2012-04-20 20:21:14 +02:00
|
|
|
!strcmp(argv[0],"get-serialno") ||
|
|
|
|
!strcmp(argv[0],"get-devpath"))
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-05-05 22:10:43 +02:00
|
|
|
return adb_query_command(format_host_command(argv[0], transport_type, serial));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
/* other commands */
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
return logcat(transport_type, serial, argc, argv);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0],"ppp")) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return ppp(argc, argv);
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "start-server")) {
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-08-12 02:05:02 +02: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 04:32:55 +01:00
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "backup")) {
|
2011-04-21 21:53:28 +02:00
|
|
|
return backup(argc, argv);
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "restore")) {
|
2011-05-18 00:52:54 +02:00
|
|
|
return restore(argc, argv);
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "keygen")) {
|
2014-11-14 00:17:29 +01:00
|
|
|
if (argc < 2) return usage();
|
|
|
|
return adb_auth_keygen(argv[1]);
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "jdwp")) {
|
2015-03-29 20:22:34 +02:00
|
|
|
return adb_connect_command("jdwp");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
/* "adb /?" is a common idiom under Windows */
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
|
2009-03-04 04:32:55 +01:00
|
|
|
help();
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-06 02:32:46 +01:00
|
|
|
else if (!strcmp(argv[0], "version")) {
|
2015-08-12 17:32:10 +02:00
|
|
|
fprintf(stdout, "%s", adb_version().c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static int pm_command(TransportType transport, const char* serial, int argc, const char** argv) {
|
2015-04-17 19:59:34 +02:00
|
|
|
std::string cmd = "shell:pm";
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-17 19:59:34 +02:00
|
|
|
while (argc-- > 0) {
|
2015-04-18 05:11:08 +02:00
|
|
|
cmd += " " + escape_arg(*argv++);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-22 02:58:55 +02:00
|
|
|
return send_shell_command(transport, serial, cmd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static int uninstall_app(TransportType transport, const char* serial, int argc, const char** argv) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* 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) */
|
|
|
|
if (argc == 3 && strcmp(argv[1], "-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 pm uninstall -k %s'\n", argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */
|
|
|
|
return pm_command(transport, serial, argc, argv);
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:42:01 +02:00
|
|
|
static int delete_file(TransportType transport, const char* serial, const std::string& filename) {
|
2015-04-17 19:59:34 +02:00
|
|
|
std::string cmd = "shell:rm -f " + escape_arg(filename);
|
2015-04-22 02:58:55 +02:00
|
|
|
return send_shell_command(transport, serial, cmd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static int install_app(TransportType transport, const char* serial, int argc, const char** argv) {
|
2015-07-09 22:35:09 +02:00
|
|
|
static const char *const DATA_DEST = "/data/local/tmp/%s";
|
|
|
|
static const char *const SD_DEST = "/sdcard/tmp/%s";
|
2014-06-10 02:30:57 +02:00
|
|
|
const char* where = DATA_DEST;
|
|
|
|
int i;
|
|
|
|
struct stat sb;
|
2011-08-05 20:19:45 +02:00
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-s")) {
|
|
|
|
where = SD_DEST;
|
|
|
|
}
|
2011-08-05 20:19:45 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
// Find last APK argument.
|
|
|
|
// All other arguments passed through verbatim.
|
|
|
|
int last_apk = -1;
|
|
|
|
for (i = argc - 1; i >= 0; i--) {
|
2015-02-26 02:51:28 +01:00
|
|
|
const char* file = argv[i];
|
2015-07-28 06:21:39 +02:00
|
|
|
const char* dot = strrchr(file, '.');
|
2014-06-10 02:30:57 +02:00
|
|
|
if (dot && !strcasecmp(dot, ".apk")) {
|
|
|
|
if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
|
|
|
|
fprintf(stderr, "Invalid APK file: %s\n", file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_apk = i;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-05 20:19:45 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
if (last_apk == -1) {
|
|
|
|
fprintf(stderr, "Missing APK file\n");
|
|
|
|
return -1;
|
2011-08-05 20:19:45 +02:00
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
const char* apk_file = argv[last_apk];
|
2015-07-31 02:42:01 +02:00
|
|
|
std::string apk_dest = android::base::StringPrintf(where, adb_basename(apk_file).c_str());
|
|
|
|
int err = do_sync_push(apk_file, apk_dest.c_str(), 0 /* no show progress */);
|
2014-06-10 02:30:57 +02:00
|
|
|
if (err) {
|
|
|
|
goto cleanup_apk;
|
|
|
|
} else {
|
2015-07-31 02:42:01 +02:00
|
|
|
argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
|
2014-06-10 02:30:57 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 02:58:55 +02:00
|
|
|
err = pm_command(transport, serial, argc, argv);
|
2014-06-10 02:30:57 +02:00
|
|
|
|
|
|
|
cleanup_apk:
|
2015-07-09 22:35:09 +02:00
|
|
|
delete_file(transport, serial, apk_dest);
|
2014-06-10 02:30:57 +02:00
|
|
|
return err;
|
2011-08-05 20:19:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static int install_multiple_app(TransportType transport, const char* serial, int argc,
|
2015-04-17 22:57:15 +02:00
|
|
|
const char** argv)
|
2011-08-05 20:19:45 +02:00
|
|
|
{
|
2010-02-19 23:53:27 +01:00
|
|
|
int i;
|
2014-06-10 02:30:57 +02:00
|
|
|
struct stat sb;
|
2015-04-17 23:07:52 +02:00
|
|
|
uint64_t total_size = 0;
|
2014-06-10 02:30:57 +02:00
|
|
|
|
|
|
|
// Find all APK arguments starting at end.
|
|
|
|
// All other arguments passed through verbatim.
|
|
|
|
int first_apk = -1;
|
|
|
|
for (i = argc - 1; i >= 0; i--) {
|
2015-02-26 02:51:28 +01:00
|
|
|
const char* file = argv[i];
|
2015-07-28 06:21:39 +02:00
|
|
|
const char* dot = strrchr(file, '.');
|
2014-06-10 02:30:57 +02:00
|
|
|
if (dot && !strcasecmp(dot, ".apk")) {
|
|
|
|
if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
|
|
|
|
fprintf(stderr, "Invalid APK file: %s\n", file);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-19 23:53:27 +01:00
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
total_size += sb.st_size;
|
|
|
|
first_apk = i;
|
|
|
|
} else {
|
2011-08-05 20:19:45 +02:00
|
|
|
break;
|
2014-06-10 02:30:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_apk == -1) {
|
|
|
|
fprintf(stderr, "Missing APK file\n");
|
2011-08-05 20:19:45 +02:00
|
|
|
return 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2011-08-05 20:19:45 +02:00
|
|
|
|
2015-04-17 23:07:52 +02:00
|
|
|
std::string cmd = android::base::StringPrintf("exec:pm install-create -S %" PRIu64, total_size);
|
2014-06-10 02:30:57 +02:00
|
|
|
for (i = 1; i < first_apk; i++) {
|
2015-04-18 05:11:08 +02:00
|
|
|
cmd += " " + escape_arg(argv[i]);
|
2014-06-10 02:30:57 +02:00
|
|
|
}
|
2011-08-05 20:19:45 +02:00
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
// Create install session
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
int fd = adb_connect(cmd, &error);
|
2014-06-10 02:30:57 +02:00
|
|
|
if (fd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "Connect error for create: %s\n", error.c_str());
|
2014-06-10 02:30:57 +02:00
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-04-17 19:59:34 +02:00
|
|
|
char buf[BUFSIZ];
|
2014-06-10 02:30:57 +02:00
|
|
|
read_status_line(fd, buf, sizeof(buf));
|
|
|
|
adb_close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-06-10 02:30:57 +02: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 01:45:13 +02:00
|
|
|
fputs(buf, stderr);
|
2014-06-10 02:30:57 +02:00
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2011-08-05 20:19:45 +02:00
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
// Valid session, now stream the APKs
|
|
|
|
int success = 1;
|
|
|
|
for (i = first_apk; i < argc; i++) {
|
2015-02-26 02:51:28 +01:00
|
|
|
const char* file = argv[i];
|
2014-06-10 02:30:57 +02:00
|
|
|
if (stat(file, &sb) == -1) {
|
|
|
|
fprintf(stderr, "Failed to stat %s\n", file);
|
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
|
|
|
}
|
2011-08-05 20:19:45 +02:00
|
|
|
|
2015-04-17 23:07:52 +02:00
|
|
|
std::string cmd = android::base::StringPrintf(
|
|
|
|
"exec:pm install-write -S %" PRIu64 " %d %d_%s -",
|
2015-07-31 02:42:01 +02:00
|
|
|
static_cast<uint64_t>(sb.st_size), session_id, i, adb_basename(file).c_str());
|
2014-06-10 02:30:57 +02:00
|
|
|
|
|
|
|
int localFd = adb_open(file, O_RDONLY);
|
|
|
|
if (localFd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
|
2014-06-10 02:30:57 +02:00
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
2011-08-05 20:19:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
std::string error;
|
2015-04-29 21:28:13 +02:00
|
|
|
int remoteFd = adb_connect(cmd, &error);
|
2014-06-10 02:30:57 +02:00
|
|
|
if (remoteFd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "Connect error for write: %s\n", error.c_str());
|
2014-06-10 02:30:57 +02:00
|
|
|
adb_close(localFd);
|
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-06-10 02:30:57 +02: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 01:45:13 +02:00
|
|
|
fputs(buf, stderr);
|
2014-06-10 02:30:57 +02:00
|
|
|
success = 0;
|
|
|
|
goto finalize_session;
|
2011-08-05 20:19:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
finalize_session:
|
2014-07-25 18:58:25 +02:00
|
|
|
// Commit session if we streamed everything okay; otherwise abandon
|
2015-04-29 21:28:13 +02:00
|
|
|
std::string service =
|
|
|
|
android::base::StringPrintf("exec:pm install-%s %d",
|
|
|
|
success ? "commit" : "abandon", session_id);
|
|
|
|
fd = adb_connect(service, &error);
|
2014-06-10 02:30:57 +02:00
|
|
|
if (fd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
fprintf(stderr, "Connect error for finalize: %s\n", error.c_str());
|
2014-06-10 02:30:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
read_status_line(fd, buf, sizeof(buf));
|
|
|
|
adb_close(fd);
|
2011-08-05 20:19:45 +02:00
|
|
|
|
2014-06-10 02:30:57 +02:00
|
|
|
if (!strncmp("Success", buf, 7)) {
|
2014-07-15 01:45:13 +02:00
|
|
|
fputs(buf, stderr);
|
2014-06-10 02:30:57 +02:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Failed to finalize session\n");
|
2014-07-15 01:45:13 +02:00
|
|
|
fputs(buf, stderr);
|
2014-06-10 02:30:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|