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

时间:2017-04-20 02:59:21

标签: android shell ps

我正在尝试在我的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) {

}

我正在使用Lollipop在三星Galaxy S6上进行测试。它运行,但我看到的只是root拥有的进程。

在带有Marshmallow的Nexus 5上,我没有看到root拥有的进程,但是我看到了很多其他进程。它仍然不是一个完整的清单。

Android中是否存在某种保护措施,使我无法在某些设备/操作系统版本中看到完整的进程列表?

1 个答案:

答案 0 :(得分:2)

是的,多种机制。在Linux上,ps通过/proc文件系统工作。到目前为止,Nougat最严格,platform/system/core.git#c39ba5a:您根本看不到任何/proc/PID属于其他用户。

  

在/ proc

上启用hidepid = 2      

将以下挂载选项添加到/ proc文件系统:

hidepid=2,gid=3009
     

此更改会阻止/ proc访问,除非您在组3009中   (又名AID_READPROC)。

/proc访问权限也受platform/system/sepolicy.git中各种规则的限制,其中一些规则适用于早期版本。

相关问题