当前位置:   article > 正文

PMS构造函数流程分析_reconcileappsdata

reconcileappsdata

1、相关代码文件

frameworks\base\services\core\java\com\android\server\pm\PackageManagerService.java

frameworks\base\services\core\java\com\android\server\pm\Settings.java

frameworks\base\services\core\java\com\android\server\pm\PackageSetting.java

frameworks\base\core\java\com\android\server\SystemConfig.java

frameworks\base\core\java\android\content\pm\PackageParser.java

 

PMS构造函数主要工作流程如下如:

1.mInstaller、mMetrics等的初始化

  1. mFactoryTest = factoryTest;
  2. mOnlyCore = onlyCore;
  3. mMetrics = new DisplayMetrics();
  4. mInstaller = installer;

 2.sUserManager、mPermissionManager、mSettings的初始化

  1. sUserManager = new UserManagerService(context, this,
  2. new UserDataPreparer(mInstaller, mInstallLock, mContext, mOnlyCore), mPackages);
  3. mPermissionManager = PermissionManagerService.create(context,
  4. new DefaultPermissionGrantedCallback() {
  5. @Override
  6. public void onDefaultRuntimePermissionsGranted(int userId) {
  7. synchronized(mPackages) {
  8. mSettings.onDefaultRuntimePermissionsGrantedLPr(userId);
  9. }
  10. }
  11. }, mPackages /*externalLock*/);
  12. mDefaultPermissionPolicy = mPermissionManager.getDefaultPermissionGrantPolicy();
  13. mSettings = new Settings(mPermissionManager.getPermissionSettings(), mPackages);

此处对权限管理、用户管理和settings进行了初始化

2.1、启动用户管理服务

代码通过读取/data/system/users路径下的目录和文件,将系统设置的默认用户信息读如内存中:

  1. private UserManagerService(Context context, PackageManagerService pm,
  2. UserDataPreparer userDataPreparer, Object packagesLock, File dataDir) {
  3. mContext = context;
  4. mPm = pm;
  5. mPackagesLock = packagesLock;
  6. mHandler = new MainHandler();
  7. mUserDataPreparer = userDataPreparer;
  8. synchronized (mPackagesLock) {
  9. mUsersDir = new File(dataDir, USER_INFO_DIR);
  10. mUsersDir.mkdirs();
  11. // Make zeroth user directory, for services to migrate their files to that location
  12. File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM));
  13. userZeroDir.mkdirs();
  14. FileUtils.setPermissions(mUsersDir.toString(),
  15. FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH,
  16. -1, -1);
  17. mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
  18. initDefaultGuestRestrictions();
  19. readUserListLP();
  20. sInstance = this;
  21. }
  22. mLocalService = new LocalService();
  23. LocalServices.addService(UserManagerInternal.class, mLocalService);
  24. mLockPatternUtils = new LockPatternUtils(mContext);
  25. mUserStates.put(UserHandle.USER_SYSTEM, UserState.STATE_BOOTING);
  26. }

通过添加授权回调,将每次用户的授权情况都实时的通过setting写入到本地文件中:

  1. mSettings.onDefaultRuntimePermissionsGrantedLPr(userId);
  2. private final class MyHandler extends Handler {
  3. public MyHandler() {
  4. super(BackgroundThread.getHandler().getLooper());
  5. }
  6. @Override
  7. public void handleMessage(Message message) {
  8. final int userId = message.what;
  9. Runnable callback = (Runnable) message.obj;
  10. //写入到本地文件:/data/system/users/0/runtime-permissions.xml中
  11. writePermissionsSync(userId);
  12. if (callback != null) {
  13. callback.run();
  14. }
  15. }
  16. }

Settings初始化将所有的用于存储package信息的路径文件都初始化:

  1. Settings(File dataDir, PermissionSettings permission, Object lock) {
  2. mLock = lock;
  3. mPermissions = permission;
  4. mRuntimePermissionsPersistence = new RuntimePermissionPersistence(mLock);
  5. mSystemDir = new File(dataDir, "system");
  6. mSystemDir.mkdirs();
  7. FileUtils.setPermissions(mSystemDir.toString(),
  8. FileUtils.S_IRWXU|FileUtils.S_IRWXG
  9. |FileUtils.S_IROTH|FileUtils.S_IXOTH,
  10. -1, -1);
  11. mSettingsFilename = new File(mSystemDir, "packages.xml");
  12. mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
  13. mPackageListFilename = new File(mSystemDir, "packages.list");
  14. FileUtils.setPermissions(mPackageListFilename, 0640, SYSTEM_UID, PACKAGE_INFO_GID);
  15. final File kernelDir = new File("/config/sdcardfs");
  16. mKernelMappingFilename = kernelDir.exists() ? kernelDir : null;
  17. // Deprecated: Needed for migration
  18. mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
  19. mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
  20. }

3.mSettings添加uid

  1. mSettings.addSharedUserLPw("android.uid.system", Process.SYSTEM_UID,
  2. ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
  3. mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID,
  4. ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
  5. mSettings.addSharedUserLPw("android.uid.log", LOG_UID,
  6. ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
  7. mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID,
  8. ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
  9. mSettings.addSharedUserLPw("android.uid.bluetooth", BLUETOOTH_UID,
  10. ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
  11. mSettings.addSharedUserLPw("android.uid.shell", SHELL_UID,
  12. ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
  13. mSettings.addSharedUserLPw("android.uid.se", SE_UID,
  14. ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
  1. SharedUserSetting addSharedUserLPw(String name, int uid, int pkgFlags, int pkgPrivateFlags) {
  2. SharedUserSetting s = mSharedUsers.get(name);
  3. if (s != null) {
  4. if (s.userId == uid) {
  5. return s;
  6. }
  7. PackageManagerService.reportSettingsProblem(Log.ERROR,
  8. "Adding duplicate shared user, keeping first: " + name);
  9. return null;
  10. }
  11. s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
  12. s.userId = uid;
  13. if (addUserIdLPw(uid, s, name)) {
  14. //加入列表中
  15. mSharedUsers.put(name, s);
  16. return s;
  17. }
  18. return null;
  19. }

4.mPackageDexOptimizer、mDexManager初始化

  1. mPackageDexOptimizer = new PackageDexOptimizer(installer, mInstallLock, context,
  2. "*dexopt*");
  3. DexManager.Listener dexManagerListener = DexLogger.getListener(this,
  4. installer, mInstallLock);
  5. mDexManager = new DexManager(mContext, this, mPackageDexOptimizer, installer, mInstallLock,
  6. dexManagerListener);

 5.初始化默认系统配置,各种白名单

SystemConfig systemConfig = SystemConfig.getInstance();

SystemConfig负责从system/etc目录中读取xml文件,将其中预置的应用或者设置读取到列表中保存,如feature列表,省点白名单,共享库,允许使用流量列表等

 

  1. // Group-ids that are given to all packages as read from etc/permissions/*.xml.
  2. int[] mGlobalGids;
  3. // These are the built-in uid -> permission mappings that were read from the
  4. // system configuration files.
  5. final SparseArray<ArraySet<String>> mSystemPermissions = new SparseArray<>();
  6. // These are the built-in shared libraries that were read from the
  7. // system configuration files. Keys are the library names; strings are the
  8. // paths to the libraries.
  9. final ArrayMap<String, String> mSharedLibraries = new ArrayMap<>();
  10. // These are the features this devices supports that were read from the
  11. // system configuration files.
  12. final ArrayMap<String, FeatureInfo> mAvailableFeatures = new ArrayMap<>();
  13. // These are the features which this device doesn't support; the OEM
  14. // partition uses these to opt-out of features from the system image.
  15. final ArraySet<String> mUnavailableFeatures = new ArraySet<>();
  16. // These are the permission -> gid mappings that were read from the
  17. // system configuration files.
  18. final ArrayMap<String, PermissionEntry> mPermissions = new ArrayMap<>();
  19. // These are the packages that are white-listed to be able to run in the
  20. // background while in power save mode (but not whitelisted from device idle modes),
  21. // as read from the configuration files.
  22. final ArraySet<String> mAllowInPowerSaveExceptIdle = new ArraySet<>();
  23. // These are the packages that are white-listed to be able to run in the
  24. // background while in power save mode, as read from the configuration files.
  25. final ArraySet<String> mAllowInPowerSave = new ArraySet<>();
  26. // These are the packages that are white-listed to be able to run in the
  27. // background while in data-usage save mode, as read from the configuration files.
  28. final ArraySet<String> mAllowInDataUsageSave = new ArraySet<>();
  29. // These are the packages that are white-listed to be able to run background location
  30. // without throttling, as read from the configuration files.
  31. final ArraySet<String> mAllowUnthrottledLocation = new ArraySet<>();
  32. // These are the action strings of broadcasts which are whitelisted to
  33. // be delivered anonymously even to apps which target O+.
  34. final ArraySet<String> mAllowImplicitBroadcasts = new ArraySet<>();
  35. // These are the package names of apps which should be in the 'always'
  36. // URL-handling state upon factory reset.
  37. final ArraySet<String> mLinkedApps = new ArraySet<>();
  38. // These are the packages that are whitelisted to be able to run as system user
  39. final ArraySet<String> mSystemUserWhitelistedApps = new ArraySet<>();
  40. // These are the packages that should not run under system user
  41. final ArraySet<String> mSystemUserBlacklistedApps = new ArraySet<>();
  42. // These are the components that are enabled by default as VR mode listener services.
  43. final ArraySet<ComponentName> mDefaultVrComponents = new ArraySet<>();
  44. // These are the permitted backup transport service components
  45. final ArraySet<ComponentName> mBackupTransportWhitelist = new ArraySet<>();
  46. // Package names that are exempted from private API blacklisting
  47. final ArraySet<String> mHiddenApiPackageWhitelist = new ArraySet<>();
  48. SystemConfig() {
  49. // Read configuration from system
  50. readPermissions(Environment.buildPath(
  51. Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);
  52. // Read configuration from the old permissions dir
  53. readPermissions(Environment.buildPath(
  54. Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);
  55. // Vendors are only allowed to customze libs, features and privapp permissions
  56. int vendorPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_PRIVAPP_PERMISSIONS;
  57. if (Build.VERSION.FIRST_SDK_INT <= Build.VERSION_CODES.O_MR1) {
  58. // For backward compatibility
  59. vendorPermissionFlag |= (ALLOW_PERMISSIONS | ALLOW_APP_CONFIGS);
  60. }
  61. readPermissions(Environment.buildPath(
  62. Environment.getVendorDirectory(), "etc", "sysconfig"), vendorPermissionFlag);
  63. readPermissions(Environment.buildPath(
  64. Environment.getVendorDirectory(), "etc", "permissions"), vendorPermissionFlag);
  65. // Allow ODM to customize system configs as much as Vendor, because /odm is another
  66. // vendor partition other than /vendor.
  67. int odmPermissionFlag = vendorPermissionFlag;
  68. readPermissions(Environment.buildPath(
  69. Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);
  70. readPermissions(Environment.buildPath(
  71. Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);
  72. // Allow OEM to customize features and OEM permissions
  73. int oemPermissionFlag = ALLOW_FEATURES | ALLOW_OEM_PERMISSIONS;
  74. readPermissions(Environment.buildPath(
  75. Environment.getOemDirectory(), "etc", "sysconfig"), oemPermissionFlag);
  76. readPermissions(Environment.buildPath(
  77. Environment.getOemDirectory(), "etc", "permissions"), oemPermissionFlag);
  78. // Allow Product to customize system configs around libs, features, permissions and apps
  79. int productPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_PERMISSIONS |
  80. ALLOW_APP_CONFIGS | ALLOW_PRIVAPP_PERMISSIONS;
  81. readPermissions(Environment.buildPath(
  82. Environment.getProductDirectory(), "etc", "sysconfig"), productPermissionFlag);
  83. readPermissions(Environment.buildPath(
  84. Environment.getProductDirectory(), "etc", "permissions"), productPermissionFlag);
  85. }

6.初始化保护应用

mProtectedPackages = new ProtectedPackages(mContext);

7.初始化内部消息循环,加入watchdog

  1. mHandlerThread = new ServiceThread(TAG,
  2. Process.THREAD_PRIORITY_BACKGROUND, true /*allowIo*/);
  3. mHandlerThread.start();
  4. mHandler = new PackageHandler(mHandlerThread
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/273054
推荐阅读
相关标签
  

闽ICP备14008679号