简体   繁体   中英

Running ps shell command on Android programmatically

I'm trying to execute the ps command on my Android app, as such:

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) {

}

I'm testing it on a Samsung Galaxy S6 with Lollipop. It runs, but all I see are root-owned processes.

On a Nexus 5 with Marshmallow though, I don't see root owned processes, but I see many other processes. It's still not a complete list.

Is there some kind of protection within Android that prevents me from seeing the full process list in certain devices/OS versions?

Yes, multiple mechanisms. On Linux, ps works through the /proc filesystem. Nougat is strictest so far, with platform/system/core.git#c39ba5a : you can't see any /proc/PID belong to other users at all.

Enable hidepid=2 on /proc

Add the following mount options to the /proc filesystem:

 hidepid=2,gid=3009 

This change blocks /proc access unless you're in group 3009 (aka AID_READPROC).

/proc access is also restricted by various rules in platform/system/sepolicy.git , some of which applies to earlier releases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM