user1118764
user1118764

Reputation: 9825

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?

Upvotes: 1

Views: 1930

Answers (1)

ephemient
ephemient

Reputation: 204718

Yes, multiple mechanisms. On Linux, ps works through the /proc filesystem. Nougat (Android 7) 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.

Upvotes: 3

Related Questions