用户创建是由UMS中的createUser完成的
1.权限校验checkManageUsersPermission
1226 @Override
1227 public UserInfo createUser(String name, int flags) {
1228 checkManageUsersPermission("Only the system can create users");
1229 return createUserInternal(name, flags, UserHandle.USER_NULL);
1230 }
2.具体创建过程
具体分析:
1.创建前的条件检查,是否有权限,是否低内存,是否超过用户上限...
2.条件满足后,获取用户userId,获取userId,从10开始累加
3.创建用户信息目录
Environment.getUserSystemDirectory(userInfo.id).mkdirs();//创建用户目录/data/system/users/userid/
/**
* Return the system directory for a user. This is for use by system services to store
* files relating to the user. This directory will be automatically deleted when the user
* is removed.
*
* @hide
*/
public static File getUserSystemDirectory(int userId) {
return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
}
/**
* Gets the system directory available for secure storage.
* If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
* Otherwise, it returns the unencrypted /data/system directory.
* @return File object representing the secure storage system directory.
* @hide
*/
public static File getSystemSecureDirectory() {
if (isEncryptedFilesystemEnabled()) {
return new File(SECURE_DATA_DIRECTORY, "system");
} else {
return new File(DATA_DIRECTORY, "system");
}
}
这里会判断系统中 EFS 这一功能是否打开. EFS(文件加密系统) ,如果打开了 EFS 功能,就会创建一个加密路径 /data/secure/system/, 否则创建的就是普通路径 /data/system .
4.写用户信息到/data/system/users/userlist.xml
writeUserListLocked();////把用户信息写到文件userlist.xml中
5.创建用户数据目录/data/user/id/,用户数据目录下存储的是该用户所用应用的数据目录,安卓不用用户的数据是独立的。
final File userDir = Environment.getDataUserDirectory(volumeUuid, userId);////创建用户数据目录/data/user/id/
如:主机用户的数据目录root@cancro:/data/user/0 # ls -l
drwxr-x--x u0_a119 u0_a119 2017-08-21 14:56 cn.wps.moffice_eng
drwxr-x--x u0_a131 u0_a131 2017-08-21 13:09 com.android.MultiplePdpTest
drwxr-x--x u0_a36 u0_a36 2017-08-16 19:17 com.android.apps.tag
drwxr-x--x u0_a0 u0_a0 2017-08-16 19:16 com.android.backupconfirm
新建用户的数据目录root@cancro:/data/user/11 # ls -l
drwxr-x--x u11_system u11_system 2017-08-20 17:29 android
drwxr-x--x u11_a119 u11_a119 2017-08-20 17:29 cn.wps.moffice_eng
drwxr-x--x u11_a131 u11_a131 2017-08-20 17:29 com.android.MultiplePdpTest
drwxr-x--x u11_a36 u11_a36 2017-08-20 17:29 com.android.apps.tag
drwxr-x--x u11_a0 u11_a0 2017-08-20 17:29 com.android.backupconfirm
6.调用PackageManagerService的createNewuserLILPw()方法,在新建用户的目录下为所有应用创建数据目录
mPm.createNewUserLILPw(userId);
7.把用户信息写到文件xx.xml中
scheduleWriteUserLocked(userInfo);//把用户信息写到文件10.xml中
8.发送广播,创建新用户了
Intent.ACTION_USER_ADDED
问题:
1.创建用户,也创建了该用户空间下的系统应用进程,什么时候创建的该用户空间下的这些进程?
2.不同用户是如何分别存储数据的?多个用户之间如何共享数据?
/** Called by UserManagerService */
void createNewUserLILPw(int userHandle) {
if (mInstaller != null) {
mInstaller.createUserConfig(userHandle);
mSettings.createNewUserLILPw(this, mInstaller, userHandle);
applyFactoryDefaultBrowserLPw(userHandle);
primeDomainVerificationsLPw(userHandle);
}
}
源码分析
1232 private UserInfo createUserInternal(String name, int flags, int parentId) {
1233 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1234 UserManager.DISALLOW_ADD_USER, false)) {//根据调用进程获取进程所属的UserId,检查该用户是否用有权限创建用户
1235 Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1236 return null;
1237 }
1238 if (ActivityManager.isLowRamDeviceStatic()) {
1239 return null;//用于判断设备是否是一个低内存的设备,如果返回为true则建议开发者减少一些消耗内存的操作。Android规定运行内存小于512M的设备为低内存设备
1240 }
1241 final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
1242 final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
1243 final long ident = Binder.clearCallingIdentity();
1244 UserInfo userInfo = null;
1245 final int userId;
1246 try {
1247 synchronized (mInstallLock) {
1248 synchronized (mPackagesLock) {
1249 UserInfo parent = null;
1250 if (parentId != UserHandle.USER_NULL) {
1251 parent = getUserInfoLocked(parentId);
1252 if (parent == null) return null;
1253 }
1254 if (isManagedProfile && !canAddMoreManagedProfiles()) {
1255 return null;
1256 }
1257 if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) {//添加非guest用户,如果数量超过上限,不能添加
1258 // If we're not adding a guest user or a managed profile and the limit has
1259 // been reached, cannot add a user.
1260 return null;
1261 }
1262 // If we're adding a guest and there already exists one, bail.
1263 if (isGuest && findCurrentGuestUserLocked() != null) {//只能有一个guest用户
1264 return null;
1265 }
1266 userId = getNextAvailableIdLocked();//获取userId,从10开始累加
1267 userInfo = new UserInfo(userId, name, null, flags);//创建用户信息对象
1268 userInfo.serialNumber = mNextSerialNumber++;//用户序列号,最终会序列化到users.xml文件中
1269 long now = System.currentTimeMillis();
1270 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;//用户创建时间
1271 userInfo.partial = true;
1272 Environment.getUserSystemDirectory(userInfo.id).mkdirs();//创建用户目录/data/system/users/userid/
1273 mUsers.put(userId, userInfo);//将创建的新用户信息加入全局用户列表变量中
1274 writeUserListLocked();////把用户信息写到文件userlist.xml中
1275 if (parent != null) {
1276 if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1277 parent.profileGroupId = parent.id;
1278 scheduleWriteUserLocked(parent);
1279 }
1280 userInfo.profileGroupId = parent.profileGroupId;
1281 }
1282 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1283 for (VolumeInfo vol : storage.getWritablePrivateVolumes()) {
1284 final String volumeUuid = vol.getFsUuid();
1285 try {
1286 final File userDir = Environment.getDataUserDirectory(volumeUuid,
1287 userId);////创建用户数据目录/data/user/id/
1288 prepareUserDirectory(userDir);
1289 enforceSerialNumber(userDir, userInfo.serialNumber);
1290 } catch (IOException e) {
1291 Log.wtf(LOG_TAG, "Failed to create user directory on " + volumeUuid, e);
1292 }
1293 }
1294 mPm.createNewUserLILPw(userId);
1295 userInfo.partial = false;
1296 scheduleWriteUserLocked(userInfo);//把用户信息写到文件10.xml中
1297 updateUserIdsLocked();
1298 Bundle restrictions = new Bundle();
1299 mUserRestrictions.append(userId, restrictions);
1300 }
1301 }
1302 mPm.newUserCreated(userId);
1303 if (userInfo != null) {
1304 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);//广播,创建用户喽
1305 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1306 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1307 android.Manifest.permission.MANAGE_USERS);
1308 }
1309 } finally {
1310 Binder.restoreCallingIdentity(ident);
1311 }
1312 return userInfo;
1313 }