1.uid
android中uid用于标识一个应用程序。uid在应用安装时被分配,并且在应用存在于手机上期间,都不会改变。
一个应用程序只能有一个uid,多个用户可以使用sharedUserId 方式共享同一个uid,前提是这些应用的签名要相同。
Uid是在apk安装的时候确认的,具体代码可以参考:
frameworks\base\services\core\java\com\android\server\pm\Settings.java
/**
* Registers a user ID with the system. Potentially allocates a new user ID.
* @throws PackageManagerException If a user ID could not be allocated.
*/
void addUserToSettingLPw(PackageSetting p) throws PackageManagerException {
if (p.appId == 0) {
// Assign new user ID
p.appId = newUserIdLPw(p);
} else {
// Add new setting to list of user IDs
addUserIdLPw(p.appId, p, p.name);
}
if (p.appId < 0) {
PackageManagerService.reportSettingsProblem(Log.WARN,
"Package " + p.name + " could not be assigned a valid UID");
throw new PackageManagerException(INSTALL_FAILED_INSUFFICIENT_STORAGE,
"Package " + p.name + " could not be assigned a valid UID");
}
}
// Returns -1 if we could not find an available UserId to assign
private int newUserIdLPw(Object obj) {
// Let's be stupidly inefficient for now...
final int N = mUserIds.size();
for (int i = mFirstAvailableUid; i < N; i++) {
if (mUserIds.get(i) == null) {
mUserIds.set(i, obj);
return Process.FIRST_APPLICATION_UID + i;
}
}
// None left?
if (N > (Process.LAST_APPLICATION_UID-Process.FIRST_APPLICATION_UID)) {
return -1;
}
mUserIds.add(obj);
return Process.FIRST_APPLICATION_UID + N;
}
另外,补充一点比较容易产生的误解:关于uid 10000的范围,uid大于10000,只能代表当前是应用的uid,不能区分这个应用是系统应用还是非系统应用。uid小于10000,基本都是系统进程的uid。
/**
* Defines the root UID.
* @hide
*/
public static final int ROOT_UID = 0;
/**
* Defines the UID/GID under which system code runs.
*/
public static final int SYSTEM_UID = 1000;
/**
* Defines the UID/GID under which the telephony code runs.
*/
public static final int PHONE_UID = 1001;
/**
* Defines the UID/GID for the user shell.
* @hide
*/
public static final int SHELL_UID = 2000;
/**
* Defines the UID/GID for the log group.
* @hide
*/
public static final int LOG_UID = 1007;
/**
* Defines the UID/GID for the WIFI supplicant process.
* @hide
*/
public static final int WIFI_UID = 1010;
/**
* Defines the UID/GID for the mediaserver process.
* @hide
*/
public static final int MEDIA_UID = 1013;
/**
* Defines the UID/GID for the DRM process.
* @hide
*/
public static final int DRM_UID = 1019;
/**
* Defines the UID/GID for the group that controls VPN services.
* @hide
*/
public static final int VPN_UID = 1016;
/**
* Defines the UID/GID for keystore.
* @hide
*/
public static final int KEYSTORE_UID = 1017;
/**
* Defines the UID/GID for the NFC service process.
* @hide
*/
public static final int NFC_UID = 1027;
/**
* Defines the UID/GID for the Bluetooth service process.
* @hide
*/
public static final int BLUETOOTH_UID = 1002;
/**
* Defines the GID for the group that allows write access to the internal media storage.
* @hide
*/
public static final int MEDIA_RW_GID = 1023;
/**
* Access to installed package details
* @hide
*/
public static final int PACKAGE_INFO_GID = 1032;
/**
* Defines the UID/GID for the shared RELRO file updater process.
* @hide
*/
public static final int SHARED_RELRO_UID = 1037;
/**
* Defines the UID/GID for the audioserver process.
* @hide
*/
public static final int AUDIOSERVER_UID = 1041;
/**
* Defines the UID/GID for the cameraserver process
* @hide
*/
public static final int CAMERASERVER_UID = 1047;
/**
* Defines the UID/GID for the WebView zygote process.
* @hide
*/
public static final int WEBVIEW_ZYGOTE_UID = 1053;
/**
* Defines the UID used for resource tracking for OTA updates.
* @hide
*/
public static final int OTA_UPDATE_UID = 1061;
/**
* Defines the UID used for incidentd.
* @hide
*/
public static final int INCIDENTD_UID = 1067;
/**
* Defines the UID/GID for the Secure Element service process.
* @hide
*/
public static final int SE_UID = 1068;
/** {@hide} */
public static final int NOBODY_UID = 9999;
/**
* Defines the start of a range of UIDs (and GIDs), going from this
* number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
* to applications.
*/
public static final int FIRST_APPLICATION_UID = 10000;
/**
* Last of application-specific UIDs starting at
* {@link #FIRST_APPLICATION_UID}.
*/
public static final int LAST_APPLICATION_UID = 19999;
2.appid
与uid相关,uid相同,则appid相同。
public static @AppIdInt int getAppId(int uid) {
return uid % PER_USER_RANGE;//PER_USER_RANGE = 100000
}
2.pid
进程id,可变的。是各进程的身份标识,程序一运行系统就会自动分配给进程一个独一无二的PID。
进程中止后pid被系统回收,可能会被继续分配给新运行的程序,但是在android系统中一般不会把已经kill掉的进程ID重新分配给新的进程。
新产生进程的进程号,一般比产生之前所有的进程号都要大。
3.gid
对应于linux中用户组的概念,android 中 gid 等于uid。
4.UserID
在Android中,多用户情况下,userlist.xml中会定义一个user id,表示当前是哪个user,此时的user id是从0开始递增。
userlist.xml中的内容:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<users nextSerialNumber="10" version="7">
<guestRestrictions>
<restrictions no_sms="true" no_install_unknown_sources="true" no_config_wifi="true" no_outgoing_calls="true" />
</guestRestrictions>
<deviceOwnerUserId id="-10000" />
<user id="0" />
</users>