接下来先看 Setting 类,Setting 的作用是管理 Android 系统运行过程中的一些设置信息
在 PackageManagerService.java的构造方法中,首先使用到:
mSettings = new Settings(mPackages);
mSettings.addSharedUserLPw("android.uid.system", Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.phone",RADIO_UID, ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.log", LOG_UID, ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID, ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.bluetooth", BLUETOOTH_UID, ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.shell", SHELL_UID, ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
//.../frameworks//base/services/core/java/com/android/server/pm/Settings.java
Setting(Object lock){
this(Environment.getDataDirectory(), lock);
}
Setting(File dataDir, object lock){
mLock = lock;
// /data/system
mSystemDir = new File(dataDir,"system");
// /data/system/packages.xml
mSettingsFileName = new File(mSystemDir, "packages.xml");
// /data/system/packages-backup.xml
mBackupSettingFilename = new File(mSystemDir, "packages-backup.xml");
// /data/system/packages.list
mPackageListFilename = new File(mSystemDir, "packages.list");
// /data/system/packages-stopped.xml
mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
// /data/system/packages-stopped-backup.xml
mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
}
1.packages.xml:记录系统所有安装应用的基本信息,如签名、权限
2.packages-backup.xml:packages.xml的备份
3.packages-stopped.xml:记录系统中所有被强制停止运行的应用信息
4.packages-stopped-back.xml:packages-stopped.xml的备份
5.packages.list:保存了应用的数据目录和 UID 等信息
备份是一种容错机制
下面以"android.uid.system"来分析:
mSetting.addSharedUserLPw("android.uid.system", Process.SYSTEM_UID,ApplicationInfo.FLAG_SYSTEM,ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
对应参数:
name="android.uid.system"
uid=Process.SYSTEM_UID,值为1000
pkgFlags=ApplicationInfo.FLAG_SYSTEM,标志系统 Package
pkgPrivateFlags=ApplicationInfo.PRIVATE_FLAG_PRIVILEGED(享有特权)
关于 UID,插入Process.java中变量
/**
* 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 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;
不同的 UID/GID授予不同的权限
final ArrayMap<String, SharedUserSetting> mSharedUser = new ArrayMap<String, SharedUserSetting>();
//这里分析的参数name="android.uid.system",uid=1000,pkgFlags=ApplicationInfo.FLAG_SYSTEM
SharedUserSetting addSharedUserLPw(String name, int uid, int pkgFlags, int pkgPriveateFlags){
SharedUserSetting s = mSharedUsers.get(name);
if(s != null){
if(s.userId == uid){
return s;
}
PackageManagerService.reportSetringsProblem(Log.ERROR,"Adding duplicate shared user, keeping first: "+name);
return null;
}
s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
s.userId = uid;
if(addUseridLPw(uid, s, name){
//将name=s添加到 mSharedUsers
mSharedUsers.put(name, s);
return s;
}
return null;
}
//从上,这uid=1000,s为 ShareUserSetting,name="android.uid.system"
private boolean addUserIdLPw(int uid,Object obj, Object name){
//Android对 uid 进行了分类,系统 APK 的 uid 小于10000
//如果 uid>10000,表示是应用 APK
//对于应用的 uid 限制在19999以内
if(uid > Proccess.LAST_APPLICATION_UID){
return false;
}
//表示是应用 APK
if(uid >= Process.FIRST_APPLICATION_UID){
int N = mUserIds.size();
//计算 uid 与FIRST_APPLICATION_UID的差作为索引
final int index = uid - Process.FIRST_APPLICATION_UID;
while(index>=N){
mUserIds.add(null);
N++;
}
...
mUserIds.set(index, obj);//mUserIds 保存应用package的 uid
}else{
...
mOtherUserIds.put(uid,obj);//mOtherUserIds 保存系统 package 的 uid
}
return true;
}
网友评论