1

Edit 10 Apr 2020

I may have misnamed what we are looking for. It may actually be the linux user name of the installed app, rather than its UID. So how should we get that programmatically?

Original question below

When we use adb shell ps even on a non rooted android device, it returns process info where the UID comes in the form u0_xxxx where x represents some arbitrary digits in hex (except for system/root processes).

For example

u0_a464 31481 894 5015336 69200 0 0 S com.example.app_uid_checker

In this example app_uid_checker is my app in user space. When trying to obtain the UID programmatically, though, I get 10464 (the decimal representation of a464), and without the u0 prefix.

I tried

  1. package manager's getApplicationInfo()
  2. activity manager's getAllRunningProcess()
  3. android.os.Process.myUid()

(following suggestions in this post on SO. They all return 10464. How can I get the "full" UID? (for want of a better term, I'm not sure what the u0_a464 version of the UID should be called, as compared to the 10464 version)).

Even if we can programmatically use adb shell ps I think it may not be a good way, as adb needs developer mode to be enabled.

auspicious99
  • 3,902
  • 1
  • 44
  • 58
  • 1
    I think this is a misunderstanding. the `u0_xxxx` notation is not the UID but the user name. Therefore your actual question is `How do I get the Linux user name from it's UID on Android?`. – Robert Apr 10 '20 at 16:36
  • Oh! Is that what it is? linux user name .. hmm, so `adb shell ps' actually lists the linux user name not the uid? Then how do we get that programmatically? https://stackoverflow.com/questions/23454000/how-to-get-username-using-uid-on-android seems to indicate there isn't a simple way? – auspicious99 Apr 10 '20 at 16:45

1 Answers1

1

You need to use the geteuid(2) and getpwuid(3) to retrieve the data, as the JVM does not expose it.

extern "C" JNIEXPORT jstring JNICALL Java_com_example_GetUser_getUser(JNIEnv* env) {
    uid_t uid = geteuid();
    struct passwd *user;
    if (uid == -1)
        return NULL;
    user = getpwuid(uid);
    return env->NewStringUTF(user->pw_name);
}

Full working project: https://gitlab.com/hackintosh5/getuser

Hack5
  • 3,244
  • 17
  • 37