2018-01-29 17:30:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 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.
|
|
|
|
*/
|
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
#include "fastdeploy.h"
|
|
|
|
|
2018-01-29 17:30:36 +01:00
|
|
|
#include <algorithm>
|
2018-08-28 13:58:09 +02:00
|
|
|
#include <array>
|
2018-09-13 19:00:25 +02:00
|
|
|
#include <memory>
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
#include "android-base/file.h"
|
|
|
|
#include "android-base/strings.h"
|
2018-09-13 19:00:25 +02:00
|
|
|
#include "androidfw/ResourceTypes.h"
|
|
|
|
#include "androidfw/ZipFileRO.h"
|
2018-01-29 17:30:36 +01:00
|
|
|
#include "client/file_sync_client.h"
|
|
|
|
#include "commandline.h"
|
|
|
|
#include "fastdeploycallbacks.h"
|
2018-10-19 22:59:44 +02:00
|
|
|
#include "sysdeps.h"
|
2018-01-29 17:30:36 +01:00
|
|
|
#include "utils/String16.h"
|
|
|
|
|
2018-09-05 21:13:11 +02:00
|
|
|
static constexpr long kRequiredAgentVersion = 0x00000001;
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-09-05 21:13:11 +02:00
|
|
|
static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
static bool g_use_localagent = false;
|
2018-08-28 13:58:09 +02:00
|
|
|
|
2018-01-29 17:30:36 +01:00
|
|
|
long get_agent_version() {
|
|
|
|
std::vector<char> versionOutputBuffer;
|
|
|
|
std::vector<char> versionErrorBuffer;
|
|
|
|
|
2018-08-24 12:46:45 +02:00
|
|
|
int statusCode = capture_shell_command("/data/local/tmp/deployagent version",
|
2018-01-29 17:30:36 +01:00
|
|
|
&versionOutputBuffer, &versionErrorBuffer);
|
|
|
|
long version = -1;
|
|
|
|
|
|
|
|
if (statusCode == 0 && versionOutputBuffer.size() > 0) {
|
|
|
|
version = strtol((char*)versionOutputBuffer.data(), NULL, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_device_api_level() {
|
|
|
|
std::vector<char> sdkVersionOutputBuffer;
|
|
|
|
std::vector<char> sdkVersionErrorBuffer;
|
|
|
|
int api_level = -1;
|
|
|
|
|
|
|
|
int statusCode = capture_shell_command("getprop ro.build.version.sdk", &sdkVersionOutputBuffer,
|
|
|
|
&sdkVersionErrorBuffer);
|
2018-08-23 18:22:21 +02:00
|
|
|
if (statusCode == 0 && sdkVersionOutputBuffer.size() > 0) {
|
2018-01-29 17:30:36 +01:00
|
|
|
api_level = strtol((char*)sdkVersionOutputBuffer.data(), NULL, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
return api_level;
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
void fastdeploy_set_local_agent(bool use_localagent) {
|
|
|
|
g_use_localagent = use_localagent;
|
2018-08-28 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 17:30:36 +01:00
|
|
|
// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
|
2018-09-06 19:42:39 +02:00
|
|
|
static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
|
2018-08-28 13:58:09 +02:00
|
|
|
std::string adb_dir = android::base::GetExecutableDirectory();
|
|
|
|
if (adb_dir.empty()) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not determine location of adb!");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
if (g_use_localagent) {
|
2018-01-29 17:30:36 +01:00
|
|
|
const char* product_out = getenv("ANDROID_PRODUCT_OUT");
|
|
|
|
if (product_out == nullptr) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not locate %s because $ANDROID_PRODUCT_OUT is not defined",
|
|
|
|
local_path);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-06 19:42:39 +02:00
|
|
|
return android::base::StringPrintf("%s%s", product_out, local_path);
|
2018-01-29 17:30:36 +01:00
|
|
|
} else {
|
2018-09-06 19:42:39 +02:00
|
|
|
return adb_dir + sdk_path;
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
static bool deploy_agent(bool checkTimeStamps) {
|
2018-01-29 17:30:36 +01:00
|
|
|
std::vector<const char*> srcs;
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string jar_path =
|
|
|
|
get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
|
|
|
|
std::string script_path =
|
|
|
|
get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
|
|
|
|
srcs.push_back(jar_path.c_str());
|
|
|
|
srcs.push_back(script_path.c_str());
|
2018-01-29 17:30:36 +01:00
|
|
|
|
|
|
|
if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
|
|
|
|
// on windows the shell script might have lost execute permission
|
|
|
|
// so need to set this explicitly
|
2018-08-24 12:46:45 +02:00
|
|
|
const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string chmodCommand =
|
|
|
|
android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
|
2018-08-23 18:22:21 +02:00
|
|
|
int ret = send_shell_command(chmodCommand);
|
2018-09-06 19:42:39 +02:00
|
|
|
if (ret != 0) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Error executing %s returncode: %d", chmodCommand.c_str(), ret);
|
2018-09-06 19:42:39 +02:00
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
} else {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Error pushing agent files to device");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-06 19:42:39 +02:00
|
|
|
|
|
|
|
return true;
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
|
2018-01-29 17:30:36 +01:00
|
|
|
long agent_version = get_agent_version();
|
|
|
|
switch (agentUpdateStrategy) {
|
|
|
|
case FastDeploy_AgentUpdateAlways:
|
2018-09-06 19:42:39 +02:00
|
|
|
deploy_agent(false);
|
2018-01-29 17:30:36 +01:00
|
|
|
break;
|
|
|
|
case FastDeploy_AgentUpdateNewerTimeStamp:
|
2018-09-06 19:42:39 +02:00
|
|
|
deploy_agent(true);
|
2018-01-29 17:30:36 +01:00
|
|
|
break;
|
|
|
|
case FastDeploy_AgentUpdateDifferentVersion:
|
|
|
|
if (agent_version != kRequiredAgentVersion) {
|
|
|
|
if (agent_version < 0) {
|
|
|
|
printf("Could not detect agent on device, deploying\n");
|
|
|
|
} else {
|
|
|
|
printf("Device agent version is (%ld), (%ld) is required, re-deploying\n",
|
|
|
|
agent_version, kRequiredAgentVersion);
|
|
|
|
}
|
2018-09-06 19:42:39 +02:00
|
|
|
deploy_agent(false);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
agent_version = get_agent_version();
|
2018-09-06 19:42:39 +02:00
|
|
|
if (agent_version != kRequiredAgentVersion) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "After update agent version remains incorrect! Expected %ld but version is %ld",
|
2018-09-06 19:42:39 +02:00
|
|
|
kRequiredAgentVersion, agent_version);
|
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
static std::string get_string_from_utf16(const char16_t* input, int input_len) {
|
|
|
|
ssize_t utf8_length = utf16_to_utf8_length(input, input_len);
|
|
|
|
if (utf8_length <= 0) {
|
|
|
|
return {};
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-13 19:00:25 +02:00
|
|
|
std::string utf8;
|
|
|
|
utf8.resize(utf8_length);
|
|
|
|
utf16_to_utf8(input, input_len, &*utf8.begin(), utf8_length + 1);
|
|
|
|
return utf8;
|
2018-08-28 13:58:09 +02:00
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
static std::string get_packagename_from_apk(const char* apkPath) {
|
2018-10-19 22:59:44 +02:00
|
|
|
#undef open
|
2018-09-13 19:00:25 +02:00
|
|
|
std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath));
|
2018-10-19 22:59:44 +02:00
|
|
|
#define open ___xxx_unix_open
|
2018-09-13 19:00:25 +02:00
|
|
|
if (zipFile == nullptr) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, errno, "Could not open %s", apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-13 19:00:25 +02:00
|
|
|
android::ZipEntryRO entry = zipFile->findEntryByName("AndroidManifest.xml");
|
|
|
|
if (entry == nullptr) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not find AndroidManifest.xml inside %s", apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-13 19:00:25 +02:00
|
|
|
uint32_t manifest_len = 0;
|
|
|
|
if (!zipFile->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not read AndroidManifest.xml inside %s", apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-13 19:00:25 +02:00
|
|
|
std::vector<char> manifest_data(manifest_len);
|
|
|
|
if (!zipFile->uncompressEntry(entry, manifest_data.data(), manifest_len)) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not uncompress AndroidManifest.xml inside %s", apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-13 19:00:25 +02:00
|
|
|
android::ResXMLTree tree;
|
|
|
|
android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true);
|
|
|
|
if (setto_status != android::OK) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not parse AndroidManifest.xml inside %s", apkPath);
|
2018-09-13 19:00:25 +02:00
|
|
|
}
|
|
|
|
android::ResXMLParser::event_code_t code;
|
|
|
|
while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT &&
|
|
|
|
code != android::ResXMLParser::END_DOCUMENT) {
|
|
|
|
switch (code) {
|
|
|
|
case android::ResXMLParser::START_TAG: {
|
|
|
|
size_t element_name_length;
|
|
|
|
const char16_t* element_name = tree.getElementName(&element_name_length);
|
|
|
|
if (element_name == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::u16string element_name_string(element_name, element_name_length);
|
|
|
|
if (element_name_string == u"manifest") {
|
|
|
|
for (size_t i = 0; i < tree.getAttributeCount(); i++) {
|
|
|
|
size_t attribute_name_length;
|
|
|
|
const char16_t* attribute_name_text =
|
|
|
|
tree.getAttributeName(i, &attribute_name_length);
|
|
|
|
if (attribute_name_text == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::u16string attribute_name_string(attribute_name_text,
|
|
|
|
attribute_name_length);
|
|
|
|
if (attribute_name_string == u"package") {
|
|
|
|
size_t attribute_value_length;
|
|
|
|
const char16_t* attribute_value_text =
|
|
|
|
tree.getAttributeStringValue(i, &attribute_value_length);
|
|
|
|
if (attribute_value_text == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return get_string_from_utf16(attribute_value_text,
|
|
|
|
attribute_value_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not find package name tag in AndroidManifest.xml inside %s", apkPath);
|
|
|
|
__builtin_unreachable();
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
void extract_metadata(const char* apkPath, FILE* outputFp) {
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-08-24 12:46:45 +02:00
|
|
|
const char* kAgentExtractCommandPattern = "/data/local/tmp/deployagent extract %s";
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string extractCommand =
|
|
|
|
android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str());
|
|
|
|
|
|
|
|
std::vector<char> extractErrorBuffer;
|
|
|
|
int statusCode;
|
|
|
|
DeployAgentFileCallback cb(outputFp, &extractErrorBuffer, &statusCode);
|
2018-09-13 19:00:25 +02:00
|
|
|
int returnCode = send_shell_command(extractCommand, false, &cb);
|
|
|
|
if (returnCode != 0) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Executing %s returned %d", extractCommand.c_str(), returnCode);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
static std::string get_patch_generator_command() {
|
2018-09-06 19:42:39 +02:00
|
|
|
if (g_use_localagent) {
|
2018-01-29 17:30:36 +01:00
|
|
|
// This should never happen on a Windows machine
|
|
|
|
const char* host_out = getenv("ANDROID_HOST_OUT");
|
|
|
|
if (host_out == nullptr) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0,
|
|
|
|
"Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT "
|
|
|
|
"is not defined");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-08-28 13:58:09 +02:00
|
|
|
return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
|
|
|
|
host_out);
|
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
std::string adb_dir = android::base::GetExecutableDirectory();
|
|
|
|
if (adb_dir.empty()) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Could not locate deploypatchgenerator.jar");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-08-28 13:58:09 +02:00
|
|
|
return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
|
|
|
|
adb_dir.c_str());
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string generatePatchCommand = android::base::StringPrintf(
|
2018-08-28 13:58:09 +02:00
|
|
|
R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
|
|
|
|
patchPath);
|
2018-09-13 19:00:25 +02:00
|
|
|
int returnCode = system(generatePatchCommand.c_str());
|
|
|
|
if (returnCode != 0) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Executing %s returned %d", generatePatchCommand.c_str(), returnCode);
|
2018-09-13 19:00:25 +02:00
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_patch_path(const char* apkPath) {
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string patchDevicePath =
|
|
|
|
android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
|
|
|
|
return patchDevicePath;
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
void apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) {
|
2018-08-24 12:46:45 +02:00
|
|
|
const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s";
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string patchDevicePath = get_patch_path(apkPath);
|
|
|
|
|
|
|
|
std::vector<const char*> srcs = {patchPath};
|
|
|
|
bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
|
|
|
|
if (!push_ok) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string applyPatchCommand =
|
|
|
|
android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
|
|
|
|
patchDevicePath.c_str(), outputPath);
|
2018-08-28 13:58:09 +02:00
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
int returnCode = send_shell_command(applyPatchCommand);
|
|
|
|
if (returnCode != 0) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
|
2018-09-13 19:00:25 +02:00
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
void install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) {
|
2018-08-24 12:46:45 +02:00
|
|
|
const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s";
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-09-13 19:00:25 +02:00
|
|
|
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string patchDevicePath =
|
|
|
|
android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
|
|
|
|
|
2018-09-13 19:00:25 +02:00
|
|
|
std::vector<const char*> srcs{patchPath};
|
|
|
|
bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
|
2018-01-29 17:30:36 +01:00
|
|
|
if (!push_ok) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned char> applyOutputBuffer;
|
|
|
|
std::vector<unsigned char> applyErrorBuffer;
|
|
|
|
std::string argsString;
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
argsString.append(argv[i]);
|
|
|
|
argsString.append(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string applyPatchCommand =
|
|
|
|
android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
|
|
|
|
patchDevicePath.c_str(), argsString.c_str());
|
2018-09-13 19:00:25 +02:00
|
|
|
int returnCode = send_shell_command(applyPatchCommand);
|
|
|
|
if (returnCode != 0) {
|
2018-10-19 22:59:44 +02:00
|
|
|
error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
|
2018-09-13 19:00:25 +02:00
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|