1.前言
在android 的產(chǎn)品開發(fā)只中,在進行一些定制開發(fā)中,對于一些apo需要通過屬性來控制禁上安裝,adb nstl也不分許安裝,所以就典熟悉adb install的安裝流程,然后來禁用adb install安裝功能,接下來分析下adb 下的安裝流程
2.禁用adb install 安裝app功能的核心類
system\core\adb\daemon\abb.cpp
system\core\adb\daemon\shell_service.cpp
3.禁用adb install 安裝app功能的核心功能分析和實現(xiàn)
在android 的產(chǎn)品中,在通過adb install 進入 adb install安裝模式后正??梢赃M行安裝app的相關(guān)操作,而adb 是pc端工具,adbd是服務(wù)端Q,運行在手機 adbd 讀取 socket 解析由 adb 傳過來的命令串,解析相關(guān)的命令執(zhí)行相關(guān)功能,所以在pc端輸入adb 相關(guān)命令就會在systemlcoreladb 模塊解析相關(guān)命令所以說在abb.cpp中來作為服務(wù)端來執(zhí)行相關(guān)功能
3.1abb.cpp相關(guān)源碼分析
在system中的adb install 安裝apk的時候會有下面的log,有install字樣。會調(diào)用StartCommandlnProcess和execCmd執(zhí)行命令abb.cpp里面的bin程序一直在讀命令ReadProtocolString,abb這個程序開機就在后臺運行
std::vector<std::string_view> parseCmdArgs(std::string_view args) {
std::vector<std::string_view> argv;
char delim = ABB_ARG_DELIMETER;
size_t size = args.size();
size_t base = 0;
while (base < size) {
size_t found;
for (found = base; found < size && args[found] && args[found] != delim; ++found)
;
if (found > base) {
argv.emplace_back(args.substr(base, found - base));
}
base = found + 1;
}
return argv;
}
} // namespace
static int execCmd(std::string_view args, int in, int out, int err) {
AdbFdTextOutput oin(out);
AdbFdTextOutput oerr(err);
return cmdMain(parseCmdArgs(args), oin, oerr, in, out, err, RunMode::kLibrary);
}
int main(int argc, char* const argv[]) {
signal(SIGPIPE, SIG_IGN);
int fd = STDIN_FILENO;
std::string data;
while (true) {
std::string error;
if (!ReadProtocolString(fd, &data, &error)) {
PLOG(ERROR) << "Failed to read message: " << error;
break;
}
std::string_view name = data;
auto protocol = SubprocessProtocol::kShell;
if (android::base::ConsumePrefix(&name, "abb:")) {
protocol = SubprocessProtocol::kShell;
} else if (android::base::ConsumePrefix(&name, "abb_exec:")) {
protocol = SubprocessProtocol::kNone;
} else {
LOG(FATAL) << "Unknown command prefix for abb: " << data;
}
unique_fd result = StartCommandInProcess(std::string(name), &execCmd, protocol);
int max_buf = LINUX_MAX_SOCKET_SIZE;
adb_setsockopt(result, SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) {
PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data;
break;
}
}
}
從abb.cpp的上述的相關(guān)的源碼中,可以知道abb.cpp里面的bin程席一直在讀命令ReadProtocolStringabb這個程序開機就在后臺運行,可以接收pc端的相關(guān)命令,在parseCmdArgs(std:string view args )中解析從adb的命令中解析命令
接下來分析下shell service.cpp中的
StartCommandInProcess相關(guān)命會分析
3.2 shell service.cpp的相關(guān)源碼分析
在system/core/adb的模塊中,在通過上述的分析得知,在這個shell service.cpp中負(fù)責(zé)在接收c端adb的相關(guān)命令中,負(fù)責(zé)解析,首先在StartCommand nProcess/std.sting name. Command command SubprocessProtocol prtoco 中fadb insta開t裝app的相關(guān)
功能的實現(xiàn)文章來源:http://www.zghlxwxcb.cn/news/detail-729175.html
unique_fd StartSubprocess(std::string name, const char* terminal_type, SubprocessType type,
SubprocessProtocol protocol, bool make_pty_raw,
SubprocessProtocol error_protocol, unique_fd* error_fd) {
D("starting %s subprocess (protocol=%s, TERM=%s): '%s'",
type == SubprocessType::kRaw ? "raw" : "PTY",
protocol == SubprocessProtocol::kNone ? "none" : "shell", terminal_type, name.c_str());
auto subprocess = std::make_unique<Subprocess>(std::move(name), terminal_type, type, protocol,
make_pty_raw);
if (!subprocess) {
LOG(ERROR) << "failed to allocate new subprocess";
*error_fd = ReportError(error_protocol, "failed to allocate new subprocess");
return {};
}
std::string error;
if (!subprocess->ForkAndExec(&error)) {
LOG(ERROR) << "failed to start subprocess: " << error;
*error_fd = ReportError(error_protocol, error);
return {};
}
unique_fd local_socket(subprocess->ReleaseLocalSocket());
D("subprocess creation successful: local_socket_fd=%d, pid=%d", local_socket.get(),
subprocess->pid());
if (!Subprocess::StartThread(std::move(subprocess), &error)) {
LOG(ERROR) << "failed to start subprocess management thread: " << error;
*error_fd = ReportError(error_protocol, error);
return {};
}
return local_socket;
}
//add core start
#include <stdio.h>
bool isAllowInstall() {
std::string value = android::base::GetProperty("persist.sys.isallow", "true");
if (strcmp("true", value.c_str()) == 0)
return true;
else
return false;
}
//add core end
unique_fd StartCommandInProcess(std::string name, Command command, SubprocessProtocol protocol) {
LOG(INFO) << "StartCommandInProcess(" << dump_hex(name.data(), name.size()) << ")";
//add core start
std::string namestring = dump_hex(name.data(), name.size());
std::string install_flag="package.install";
std::string::size_type idx=namestring.find(install_flag);
//add core end
constexpr auto terminal_type = "";
constexpr auto type = SubprocessType::kRaw;
constexpr auto make_pty_raw = false;
auto subprocess = std::make_unique<Subprocess>(std::move(name), terminal_type, type, protocol,
make_pty_raw);
//add core start
if(idx == std::string::npos ){
LOG(ERROR) << "the command do not include package.install string";
}else{
if(!isAllowInstall()) {
LOG(ERROR) << "can not allow to install app" ;
return ReportError(protocol, "can not allow to install app by adb install command");
}else
LOG(ERROR) << "Allow to install app";
}
//add core end
if (!subprocess) {
LOG(ERROR) << "failed to allocate new subprocess";
return ReportError(protocol, "failed to allocate new subprocess");
}
std::string error;
if (!subprocess->ExecInProcess(std::move(command), &error)) {
LOG(ERROR) << "failed to start subprocess: " << error;
return ReportError(protocol, error);
}
unique_fd local_socket(subprocess->ReleaseLocalSocket());
D("inprocess creation successful: local_socket_fd=%d, pid=%d", local_socket.get(),
subprocess->pid());
if (!Subprocess::StartThread(std::move(subprocess), &error)) {
LOG(ERROR) << "failed to start inprocess management thread: " << error;
return ReportError(protocol, error);
}
return local_socket;
}
在上述的shell service.cpp的相關(guān)源碼中分析得知,在通過判斷dump hex(name.data(),name.size()中是否有package.install字符來判斷是否執(zhí)行adb install安裝app的命令,然后在通過調(diào)用isAllowInstal()來判斷系統(tǒng)屬性persist.sys.isallow是否允許安裝app,當(dāng)為false就表示禁止安裝第=方app,這時候就返回ReportError(protocol,“can not allow to install app by adb install command”).就實現(xiàn)禁止通過adb install來安裝第三方app功能文章來源地址http://www.zghlxwxcb.cn/news/detail-729175.html
到了這里,關(guān)于禁用adb install 安裝app功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!