Feature: support process management on rooted Android hosts
Android 7 起 /proc 是 hidepid=2 + SELinux,普通 APK 连其它 PID 都看不到, 因此进程管理做成 root-only:登录时探测 su(常见路径 + which su,都不行再 su -c id 兜底),有 root 才声明 CLIENT_CAP_PROCESS。否定结果只缓存 5 分钟, 避免用户误点一次「拒绝」后整个进程生命周期都用不了。 - 客户端:RootProcess.kt 通过 su 枚举 /proc、批量 kill;子连接协议与 Linux 一致(TOKEN_PSLIST / COMMAND_KILLPROCESS / 空 TOKEN_WSLIST)。 - 服务端:MCP list_processes/kill_process 与 GUI 进程管理对无能力位的 APK 直接回「需要 root」,不再下发 COMMAND_SYSTEM 后干等超时。 验证:Android 编译通过;模拟器上的 su 仅 root:shell 可执行,验证了非 root 拒绝路径(MCP -32005 + GUI 提示)。真 root 设备的枚举/kill 待实机验证。 Model: deepseek-flash Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
998c16edc5
commit
b7f87c958a
8 files changed
+447
-10
No files matched your search
@@ -43,6 +43,7 @@
|
||||
// 后者拨的是 g_SETTINGS 里的出厂地址,转移主机后还是连旧主控。
|
||||
#include "FileManagerAndroid.h"
|
||||
#include "VideoHandlerAndroid.h" // 摄像头(COMMAND_WEBCAM)
|
||||
#include "ProcessManagerAndroid.h" // 进程管理(COMMAND_SYSTEM,仅 root 设备)
|
||||
|
||||
#define LOG_TAG "YAMA"
|
||||
// LOGI/LOGE 走 android_log_tag(logger.h):既进 logcat,也进 Logger 的内存 ring,
|
||||
@@ -292,6 +293,11 @@ static jmethodID g_utf8ToGbkMid = nullptr; // NativeText.utf8ToGbk([B)[
|
||||
static jclass g_cameraClass = nullptr;
|
||||
static jmethodID g_cameraStartMid = nullptr; // ()[I
|
||||
static jmethodID g_cameraStopMid = nullptr; // ()V
|
||||
// 进程管理:RootProcess 通过 su 枚举 /proc 与 kill
|
||||
static jclass g_rootClass = nullptr;
|
||||
static jmethodID g_rootAvailableMid = nullptr; // ()Z
|
||||
static jmethodID g_rootSnapshotMid = nullptr; // ()Ljava/lang/String;
|
||||
static jmethodID g_rootKillMid = nullptr; // ([I)Z
|
||||
|
||||
// 心跳日志限流:每 60 秒最多打一次
|
||||
static std::atomic<uint64_t> g_lastHbLogMs{0};
|
||||
@@ -624,6 +630,69 @@ void StopJavaCamera()
|
||||
DetachJniForCall(attached);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 进程管理 JNI
|
||||
bool AndroidRootAvailable()
|
||||
{
|
||||
if (!g_jvm || !g_rootClass || !g_rootAvailableMid) return false;
|
||||
bool attached = false;
|
||||
JNIEnv* env = AttachJniForCall(&attached);
|
||||
if (!env) return false;
|
||||
jboolean ok = env->CallStaticBooleanMethod(g_rootClass, g_rootAvailableMid);
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionClear();
|
||||
ok = JNI_FALSE;
|
||||
}
|
||||
DetachJniForCall(attached);
|
||||
return ok == JNI_TRUE;
|
||||
}
|
||||
|
||||
std::string AndroidProcessSnapshot()
|
||||
{
|
||||
if (!g_jvm || !g_rootClass || !g_rootSnapshotMid) return "";
|
||||
bool attached = false;
|
||||
JNIEnv* env = AttachJniForCall(&attached);
|
||||
if (!env) return "";
|
||||
|
||||
std::string out;
|
||||
jstring s = (jstring)env->CallStaticObjectMethod(g_rootClass, g_rootSnapshotMid);
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionClear();
|
||||
} else if (s) {
|
||||
const char* c = env->GetStringUTFChars(s, nullptr);
|
||||
if (c) {
|
||||
out = c;
|
||||
env->ReleaseStringUTFChars(s, c);
|
||||
}
|
||||
env->DeleteLocalRef(s);
|
||||
}
|
||||
DetachJniForCall(attached);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool AndroidKillProcesses(const std::vector<int>& pids)
|
||||
{
|
||||
if (!g_jvm || !g_rootClass || !g_rootKillMid || pids.empty()) return false;
|
||||
bool attached = false;
|
||||
JNIEnv* env = AttachJniForCall(&attached);
|
||||
if (!env) return false;
|
||||
|
||||
bool ok = false;
|
||||
jintArray arr = env->NewIntArray((jsize)pids.size());
|
||||
if (arr) {
|
||||
env->SetIntArrayRegion(arr, 0, (jsize)pids.size(), (const jint*)pids.data());
|
||||
if (!env->ExceptionCheck()) {
|
||||
jboolean r = env->CallStaticBooleanMethod(g_rootClass, g_rootKillMid, arr);
|
||||
if (env->ExceptionCheck()) env->ExceptionClear();
|
||||
else ok = (r == JNI_TRUE);
|
||||
} else {
|
||||
env->ExceptionClear();
|
||||
}
|
||||
env->DeleteLocalRef(arr);
|
||||
}
|
||||
DetachJniForCall(attached);
|
||||
return ok;
|
||||
}
|
||||
|
||||
std::string AndroidMapUnixToWinPath(const std::string& unixPath)
|
||||
{
|
||||
for (const auto& r : AndroidStorageRootsCached()) {
|
||||
@@ -1563,6 +1632,46 @@ static void VideoThread()
|
||||
LOGI("VideoThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 进程管理
|
||||
// 服务端 COMMAND_SYSTEM(主控右键「进程管理」/ MCP list_processes)→ 子连接 →
|
||||
// 构造即发 TOKEN_PSLIST。只在有 su 的设备上走得到这里(登录时声明
|
||||
// CLIENT_CAP_PROCESS);非 root 设备由服务端提前拒绝,客户端再兜一层提示。
|
||||
static std::atomic<bool> g_procInFlight{false};
|
||||
|
||||
static void ProcessThread()
|
||||
{
|
||||
struct InFlightGuard {
|
||||
~InFlightGuard() { g_procInFlight.store(false); }
|
||||
} guard;
|
||||
|
||||
std::string ip; int port;
|
||||
GetServerTarget(ip, port);
|
||||
LOGI(">>> ProcessThread start → %s:%d", ip.c_str(), port);
|
||||
try {
|
||||
std::unique_ptr<IOCPClient> client(new IOCPClient(g_bExit, true));
|
||||
client->EnableSubConnAuth(true, g_myClientID);
|
||||
client->SetDefaultProto((int)(unsigned char)g_SETTINGS.protoType);
|
||||
GetServerTarget(ip, port); // 转移主机后目标会变
|
||||
if (!client->ConnectServer(ip.c_str(), port)) {
|
||||
LOGE("ProcessThread: connect failed");
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_ptr<AndroidProcessManager> handler(new AndroidProcessManager(client.get()));
|
||||
client->setManagerCallBack(handler.get(),
|
||||
IOCPManager::DataProcess,
|
||||
IOCPManager::ReconnectProcess);
|
||||
while (client->IsRunning() && client->IsConnected() && S_CLIENT_NORMAL == g_bExit)
|
||||
Sleep(200);
|
||||
client->setManagerCallBack(nullptr, nullptr, nullptr);
|
||||
handler.reset();
|
||||
client.reset();
|
||||
} catch (const std::exception& e) {
|
||||
LOGE("ProcessThread exception: %s", e.what());
|
||||
}
|
||||
LOGI(">>> ProcessThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 历史活动上报
|
||||
// 主连接一次性快照(与 Windows/Linux 一致,服务端 TOKEN_REPORT_ACTIVITY 分支不 CancelIO)。
|
||||
// 采样机每秒问一次 ControlService 的前台应用;这里只取 Dump(),几十微秒的活。
|
||||
@@ -2254,9 +2363,29 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
|
||||
ApplyUpdateApk(szBuffer, ulLength);
|
||||
break;
|
||||
|
||||
case COMMAND_SYSTEM:
|
||||
LOGI("COMMAND_SYSTEM (not implemented)");
|
||||
case COMMAND_SYSTEM: {
|
||||
// 进程管理:Android 7+ 的 /proc 受 hidepid/SELinux 限制,只有 root 能枚举。
|
||||
// 非 root 设备在登录时没有声明 CLIENT_CAP_PROCESS,正常情况下服务端会先拒绝;
|
||||
// 这里再兜一层,避免老服务端/直发命令时无声挂起。
|
||||
if (!AndroidRootAvailable()) {
|
||||
LOGI("cmd: COMMAND_SYSTEM ignored (no root)");
|
||||
sendClientMsg("Process manager",
|
||||
"This Android host is not rooted: process management requires root");
|
||||
break;
|
||||
}
|
||||
if (g_procInFlight.exchange(true)) {
|
||||
LOGI("cmd: COMMAND_SYSTEM ignored (process session already in flight)");
|
||||
break;
|
||||
}
|
||||
LOGI("cmd: COMMAND_SYSTEM → process sub-conn");
|
||||
try {
|
||||
std::thread(ProcessThread).detach();
|
||||
} catch (const std::exception& e) {
|
||||
g_procInFlight.store(false);
|
||||
LOGE("Process thread failed: %s", e.what());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef YAMA_TLS_CHANNEL
|
||||
case COMMAND_TLS_ENABLE: {
|
||||
@@ -2527,7 +2656,10 @@ static void ConnectionThread(uint64_t gen)
|
||||
logInfo.SetCaps(LOGIN_INFOR::DefaultCaps() |
|
||||
(ScreenPreviewAvailable() ? CLIENT_CAP_SCREEN_PREVIEW : 0) |
|
||||
(LockScreenAvailable() ? CLIENT_CAP_LOCK_SCREEN : 0) |
|
||||
(OverlayAvailable() ? CLIENT_CAP_PRIVACY_OVERLAY : 0));
|
||||
(OverlayAvailable() ? CLIENT_CAP_PRIVACY_OVERLAY : 0) |
|
||||
// Android 的进程列表/杀进程依赖 root(hidepid=2 + SELinux),
|
||||
// 没有 su 就不声明,服务端据此直接回 -32005 而不是等超时。
|
||||
(AndroidRootAvailable() ? CLIENT_CAP_PROCESS : 0));
|
||||
client->SendLoginInfo(logInfo.Speed(clock() - c));
|
||||
g_lastHeartbeatAckMs.store(GetUnixMs(), std::memory_order_relaxed);
|
||||
LOGI("Connected & login sent");
|
||||
@@ -2899,6 +3031,23 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
} else {
|
||||
LOGE("CameraCapture class not found");
|
||||
}
|
||||
// 进程管理:RootProcess 的 root 探测 / 进程快照 / 杀进程
|
||||
jclass rootCls = env->FindClass("com/yama/client/RootProcess");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); rootCls = nullptr; }
|
||||
if (rootCls) {
|
||||
g_rootClass = (jclass)env->NewGlobalRef(rootCls);
|
||||
g_rootAvailableMid = env->GetStaticMethodID(g_rootClass, "isRootAvailable", "()Z");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_rootAvailableMid = nullptr; }
|
||||
if (!g_rootAvailableMid) LOGE("RootProcess.isRootAvailable not found");
|
||||
g_rootSnapshotMid = env->GetStaticMethodID(g_rootClass, "snapshot", "()Ljava/lang/String;");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_rootSnapshotMid = nullptr; }
|
||||
if (!g_rootSnapshotMid) LOGE("RootProcess.snapshot not found");
|
||||
g_rootKillMid = env->GetStaticMethodID(g_rootClass, "kill", "([I)Z");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_rootKillMid = nullptr; }
|
||||
if (!g_rootKillMid) LOGE("RootProcess.kill not found");
|
||||
} else {
|
||||
LOGE("RootProcess class not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user