在Android上以编程方式运行ps shell命令

bq8i3lrv  于 2023-04-21  发布在  Shell
关注(0)|答案(1)|浏览(273)

我尝试在我的Android应用程序上执行ps命令,如下所示:

try {
    Process process = Runtime.getRuntime().exec("ps");
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    int read;
    char[] buffer = new char[4096];
    StringBuffer output = new StringBuffer();
    while ((read = reader.read(buffer)) > 0) {
        output.append(buffer, 0, read);
    }
    reader.close();

    // Waits for the command to finish.
    process.waitFor();
    Log.d(TAG, output.toString());
} catch (IOException e) {

} catch (InterruptedException e) {

}

我在三星Galaxy S6上用Lollipop测试它,它可以运行,但我看到的都是root拥有的进程。
在一个有Marshmallow的Nexus 5上,我没有看到root拥有的进程,但我看到了许多其他进程。这仍然不是一个完整的列表。
Android中是否有某种保护措施,可以阻止我在某些设备/操作系统版本中看到完整的进程列表?

2lpgd968

2lpgd9681#

是的,多种机制。在Linux上,ps通过/proc文件系统工作。Nougat(Android 7)是迄今为止最严格的,platform/system/core.git#c39ba5a:你根本看不到任何属于其他用户的/proc/PID
在/proc上启用hidepid=2
将以下挂载选项添加到/proc文件系统:

hidepid=2,gid=3009

除非您在组3009(又名AID_READPROC)中,否则此更改将阻止/proc访问。
/proc访问也受到platform/system/sepolicy.git中各种规则的限制,其中一些规则适用于早期版本。

相关问题