views:

79

answers:

1

I'm looking for a alternative method to reading the /proc/PID/exe symbolic link in which to obtain the full path of a process in Android/Linux.

The reason being is that on Android, /proc/PID/exe for any process other than your own or 'self' seem to have restricted (permission denied) access.

I have looked into the following as well - they are readable, but don't seem to work:

  • /proc/PID/cmdline: Rarely contains the full path
  • /proc/PID/stat: Contains only the executable name (no path)

Failing code due to permission denied:

#include <unistd.h>
// ...
char buf[2048];
// "/proc/1234/exe" is of course replaced with a proper PID
ssizet_t len = readlink("/proc/1234/exe", buf, sizeof(buf) - 1);
if(-1 != len) {
  buf[len] = '\0';
  // buf should contain full path
} else {
  // this path always reached for PID's other than my own or
  // /proc/self/exe
}

There has to be a alternative method? In Windows, there is of course GetModuleFileNameEx, QueryFullProcessImageName(), etc.

A: 

There's other places where you can obtain the information - eg /proc/<pid>/maps - but if you're not allowed to access /proc/<pid>/exe then you aren't allowed to access those, either.

There's no end-run around it - it's a consequence of Android's security model, where each app is given its own UID.

caf
This applies for some things in the /proc FS, but others I can read. Also, this doesn't necessarily mean there isn't another approach that can be used.
NuSkooler