android uid gid和linux uid gid的研究和对应关系
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/os/UserHandle.java/?v=source
public static void formatUid(PrintWriter pw, int uid) { if (uid < Process.FIRST_APPLICATION_UID) { pw.print(uid); } else { pw.print('u'); pw.print(getUserId(uid)); final int appId = getAppId(uid); if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { pw.print('i'); pw.print(appId - Process.FIRST_ISOLATED_UID); } else if (appId >= Process.FIRST_APPLICATION_UID) { pw.print('a'); pw.print(appId - Process.FIRST_APPLICATION_UID); } else { pw.print('s'); pw.print(appId); } } }
public static boolean isApp(int uid) { if (uid > 0) { final int appId = getAppId(uid); return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID; } else { return false; } }
所以只要是app appId的范围一定在10000到19999
public static final int PER_USER_RANGE = 100000;
public static final int SHARED_USER_GID = 9997; //Defines the gid shared by all applications running under the same profile. 简单说是一个userId的appId共享这个gid(不同的userid拥有不同的uid组)的数据,目前没发现用处。
可以通过Userid 获得gid
/** * Returns the gid shared between all apps with this userId. * @hide */ public static final int getUserGid(int userId) { return getUid(userId, Process.SHARED_USER_GID); }
public static final int FIRST_SHARED_APPLICATION_GID = 50000;//First gid for applications to share resources. Used when forward-locking is enabled but all UserHandles need to be able to read the resources.
public static final int LAST_SHARED_APPLICATION_GID = 59999;//Last gid for applications to share resources. Used when forward-locking is enabled but all UserHandles need to be able to read the resources.
上面两行是共享的gid范围 the shared app gid 每个appID会属于某个用户组。同属于一个用户组的appid 有些权限是共享的。可以通过uid 获得gid
/** * Returns the shared app gid for a given uid or appId. * @hide */ public static final int getSharedAppGid(int id) { return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE) - Process.FIRST_APPLICATION_UID; }
final int appId = getAppId(uid);
appId=uid%PER_USER_RANGE //一般用户是0 PER_USER_RANGE为10w ,所以appId不会超过10w,如果每增加一个用户则uid就增加10w,如果当前登陆手机的用户是1 则uid 就在原来的基础上加10w
linux uid=前面参数+(android appId-10000)
前面参数 如果是4.0版本以下 则是app_
前面参数 如果是4.1版本以上 则是u0_a 其中0是使用手机的用户,4.1以上后android为多用户做的一些变化。如果使用手机的用户是1 则是u1_a
4.1版本以上 附加规则
application appid:[10 000, 19 999]
shared application gid:[50 000, 59 999]
isolated appid:[99 000, 99 999]
system appid似乎是从1000开始,那猜测应该是到9 999结束。
代码依据
如果是SHARED_USER_GID 前面参数u0_s
如果是isolated appid 前面参数u0_i
isolated appid指得是为一个沙盒进程所创建的isolated appid,此isolated appid没有任何权限,用以加载一段不确定的对系统存在风险的代码。创建方法是在service 标签加 android:isolatedProcess="true" ,此时service不仅是在别的进程而且进程的用户也是不一样,所以就没有当前app的任何权限。
SHARED_USER_GID 9997 指的是一个userid下的共享进程,可以给所有在此userid下的ap共享此用户的数据