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>
123 lines
5.1 KiB
Kotlin
123 lines
5.1 KiB
Kotlin
package com.yama.client
|
||
|
||
import android.os.SystemClock
|
||
import android.util.Log
|
||
import java.io.File
|
||
import java.util.concurrent.TimeUnit
|
||
|
||
/**
|
||
* 进程管理(COMMAND_SYSTEM)的 Android 端。
|
||
*
|
||
* Android 7 起 /proc 挂载带 hidepid=2,且 SELinux 的 untrusted_app 域看不到其它
|
||
* PID——普通 APK 连进程名都枚举不出来,更杀不掉别人。因此这项能力只在取得 root
|
||
* (su)的设备上声明 CLIENT_CAP_PROCESS,非 root 设备由服务端直接拒绝。
|
||
*
|
||
* 数据格式与服务端 TOKEN_PSLIST 对齐:[pid:4][名字:架构\0][完整路径\0]...
|
||
* 这里只负责产出 "pid\tcomm\texe\n" 文本,二进制封装在 native 侧
|
||
* (android/app/src/main/cpp/ProcessManagerAndroid.h),避免在 Kotlin 里重复协议常量。
|
||
*/
|
||
object RootProcess {
|
||
private const val TAG = "YAMA_PROC"
|
||
private const val SU_TIMEOUT_SEC = 6L
|
||
private const val SU_PROBE_TIMEOUT_SEC = 3L
|
||
// 否定结果只缓存 5 分钟:用户可能只是当时点了「拒绝」,重启 App 才能恢复太蠢
|
||
private const val NEGATIVE_TTL_MS = 5 * 60 * 1000L
|
||
|
||
private val suCandidates = listOf(
|
||
"/system/bin/su", "/system/xbin/su", "/sbin/su", "/su/bin/su",
|
||
"/debug_ramdisk/su", "/data/adb/ksu/bin/su", "/data/adb/magisk/su"
|
||
)
|
||
|
||
@Volatile private var cachedTrue = false
|
||
@Volatile private var cachedFalseAt = 0L
|
||
|
||
/** su 是否存在。true 永久缓存;false 只缓存 5 分钟。 */
|
||
@JvmStatic
|
||
@Synchronized
|
||
fun isRootAvailable(): Boolean {
|
||
if (cachedTrue) return true
|
||
val now = SystemClock.elapsedRealtime()
|
||
if (cachedFalseAt != 0L && now - cachedFalseAt < NEGATIVE_TTL_MS) return false
|
||
|
||
var exists = suCandidates.any { runCatching { File(it).exists() }.getOrDefault(false) } ||
|
||
runCatching {
|
||
val p = ProcessBuilder("which", "su").redirectErrorStream(true).start()
|
||
val done = p.waitFor(2, TimeUnit.SECONDS)
|
||
if (!done) { p.destroyForcibly(); false } else p.exitValue() == 0
|
||
}.getOrDefault(false)
|
||
|
||
// 路径探测对 Magisk/DenyList 之类「隐藏 su」的配置不可靠;再做一次真实调用。
|
||
// 这会触发一次 root 授权框(只在探测时),授权成功才认为有 root。
|
||
// 探测超时用 3 秒,别让登录等太久。
|
||
if (!exists) {
|
||
val out = runSu("id", SU_PROBE_TIMEOUT_SEC)
|
||
exists = out != null && out.contains("uid=0")
|
||
}
|
||
|
||
if (exists) cachedTrue = true else cachedFalseAt = now
|
||
Log.i(TAG, "root available=$exists")
|
||
return exists
|
||
}
|
||
|
||
/**
|
||
* 通过 su 枚举 /proc。返回每行 "pid\tcomm\texe",失败返回空串。
|
||
* 脚本在 root shell 里跑,应用自身的 SELinux 限制不适用。
|
||
*/
|
||
@JvmStatic
|
||
fun snapshot(): String {
|
||
if (!isRootAvailable()) return ""
|
||
// 单引号包裹的 sh 脚本;printf 的 \t \n 由 sh 解释
|
||
// comm/exe 里可能含制表符/换行(进程名可被 prctl 改成任意内容),
|
||
// 先 tr 成空格,否则 native 侧按 \t 解析会被伪造的进程名打乱。
|
||
val script =
|
||
"for p in /proc/[0-9]*; do " +
|
||
"pid=\${p##*/}; " +
|
||
"comm=\$(cat \$p/comm 2>/dev/null | tr '\\t\\n\\r' ' '); " +
|
||
"exe=\$(readlink \$p/exe 2>/dev/null | tr '\\t\\n\\r' ' '); " +
|
||
"if [ -n \"\$comm\" ]; then printf '%s\\t%s\\t%s\\n' \"\$pid\" \"\$comm\" \"\$exe\"; fi; " +
|
||
"done"
|
||
return runSu(script) ?: ""
|
||
}
|
||
|
||
/** 批量 SIGKILL。返回是否全部下发(不保证进程已退出)。 */
|
||
@JvmStatic
|
||
fun kill(pids: IntArray): Boolean {
|
||
if (!isRootAvailable() || pids.isEmpty()) return false
|
||
val list = pids.filter { it > 1 }.joinToString(" ")
|
||
if (list.isEmpty()) return false
|
||
val out = runSu("kill -9 $list")
|
||
Log.i(TAG, "kill [$list] -> ${out != null}")
|
||
return out != null
|
||
}
|
||
|
||
private fun runSu(script: String, timeoutSec: Long = SU_TIMEOUT_SEC): String? {
|
||
return runCatching {
|
||
val p = ProcessBuilder("su", "-c", script)
|
||
.redirectErrorStream(true)
|
||
.start()
|
||
|
||
// 必须在等待的同时把 stdout 读走:几百个进程的输出可能超过管道缓冲,
|
||
// 写满后子进程会阻塞,waitFor 只能等到超时。
|
||
val out = StringBuilder()
|
||
val reader = Thread {
|
||
runCatching {
|
||
p.inputStream.bufferedReader().use { out.append(it.readText()) }
|
||
}
|
||
}
|
||
reader.isDaemon = true
|
||
reader.start()
|
||
|
||
val done = p.waitFor(timeoutSec, TimeUnit.SECONDS)
|
||
if (!done) {
|
||
p.destroyForcibly()
|
||
Log.w(TAG, "su timed out")
|
||
return@runCatching null
|
||
}
|
||
reader.join(1000)
|
||
val text = out.toString()
|
||
// 用户拒绝授权时 su 通常返回非 0,且输出为空/拒绝信息
|
||
if (p.exitValue() != 0 && text.isBlank()) null else text
|
||
}.onFailure { Log.w(TAG, "su failed: $it") }.getOrNull()
|
||
}
|
||
}
|