Merge "OTA only adds Main if isMainUserPermanentAdmin" into udc-dev

This commit is contained in:
Adam Bookatz
2023-03-09 23:32:24 +00:00
committed by Android (Google) Code Review
2 changed files with 22 additions and 18 deletions

View File

@@ -3504,15 +3504,15 @@ public class UserManagerService extends IUserManager.Stub {
return; return;
} }
final int oldMainUserId = getMainUserIdUnchecked(); final int oldMainUserId = getMainUserIdUnchecked();
final int oldFlags = systemUserData.info.flags; final int oldSysFlags = systemUserData.info.flags;
final int newFlags; final int newSysFlags;
final String newUserType; final String newUserType;
if (newHeadlessSystemUserMode) { if (newHeadlessSystemUserMode) {
newUserType = UserManager.USER_TYPE_SYSTEM_HEADLESS; newUserType = UserManager.USER_TYPE_SYSTEM_HEADLESS;
newFlags = oldFlags & ~UserInfo.FLAG_FULL & ~UserInfo.FLAG_MAIN; newSysFlags = oldSysFlags & ~UserInfo.FLAG_FULL & ~UserInfo.FLAG_MAIN;
} else { } else {
newUserType = UserManager.USER_TYPE_FULL_SYSTEM; newUserType = UserManager.USER_TYPE_FULL_SYSTEM;
newFlags = oldFlags | UserInfo.FLAG_FULL; newSysFlags = oldSysFlags | UserInfo.FLAG_FULL | UserInfo.FLAG_MAIN;
} }
if (systemUserData.info.userType.equals(newUserType)) { if (systemUserData.info.userType.equals(newUserType)) {
@@ -3523,18 +3523,19 @@ public class UserManagerService extends IUserManager.Stub {
Slogf.i(LOG_TAG, "Persisting emulated system user data: type changed from %s to " Slogf.i(LOG_TAG, "Persisting emulated system user data: type changed from %s to "
+ "%s, flags changed from %s to %s", + "%s, flags changed from %s to %s",
systemUserData.info.userType, newUserType, systemUserData.info.userType, newUserType,
UserInfo.flagsToString(oldFlags), UserInfo.flagsToString(newFlags)); UserInfo.flagsToString(oldSysFlags), UserInfo.flagsToString(newSysFlags));
systemUserData.info.userType = newUserType; systemUserData.info.userType = newUserType;
systemUserData.info.flags = newFlags; systemUserData.info.flags = newSysFlags;
writeUserLP(systemUserData); writeUserLP(systemUserData);
// Switch the MainUser to a reasonable choice if needed. // Designate the MainUser to a reasonable choice if needed.
// (But if there was no MainUser, we deliberately continue to have no MainUser.)
final UserData oldMain = getUserDataNoChecks(oldMainUserId); final UserData oldMain = getUserDataNoChecks(oldMainUserId);
if (newHeadlessSystemUserMode) { if (newHeadlessSystemUserMode) {
if (oldMain != null && (oldMain.info.flags & UserInfo.FLAG_SYSTEM) != 0) { final boolean mainIsAlreadyNonSystem =
// System was MainUser. So we need a new choice for Main. Pick the oldest. oldMain != null && (oldMain.info.flags & UserInfo.FLAG_SYSTEM) == 0;
if (!mainIsAlreadyNonSystem && isMainUserPermanentAdmin()) {
// We need a new choice for Main. Pick the oldest.
// If no oldest, don't set any. Let the BootUserInitializer do that later. // If no oldest, don't set any. Let the BootUserInitializer do that later.
final UserInfo newMainUser = getEarliestCreatedFullUser(); final UserInfo newMainUser = getEarliestCreatedFullUser();
if (newMainUser != null) { if (newMainUser != null) {
@@ -3544,16 +3545,16 @@ public class UserManagerService extends IUserManager.Stub {
} }
} }
} else { } else {
// We already made user 0 Main above. Now strip it from the old Main user.
// TODO(b/256624031): For now, we demand the Main user (if there is one) is // TODO(b/256624031): For now, we demand the Main user (if there is one) is
// always the system in non-HSUM. In the future, when we relax this, change how // always the system in non-HSUM. In the future, when we relax this, change how
// we handle MAIN. // we handle MAIN.
if (oldMain != null && (oldMain.info.flags & UserInfo.FLAG_SYSTEM) == 0) { if (oldMain != null && (oldMain.info.flags & UserInfo.FLAG_SYSTEM) == 0) {
// Someone else was the MainUser; transfer it to System.
Slogf.i(LOG_TAG, "Transferring Main to user 0 from " + oldMain.info.id); Slogf.i(LOG_TAG, "Transferring Main to user 0 from " + oldMain.info.id);
oldMain.info.flags &= ~UserInfo.FLAG_MAIN; oldMain.info.flags &= ~UserInfo.FLAG_MAIN;
systemUserData.info.flags |= UserInfo.FLAG_MAIN;
writeUserLP(oldMain); writeUserLP(oldMain);
writeUserLP(systemUserData); } else {
Slogf.i(LOG_TAG, "Designated user 0 to be Main");
} }
} }
} }
@@ -3817,12 +3818,14 @@ public class UserManagerService extends IUserManager.Stub {
if (userVersion < 11) { if (userVersion < 11) {
// Add FLAG_MAIN // Add FLAG_MAIN
if (isHeadlessSystemUserMode()) { if (isHeadlessSystemUserMode()) {
final UserInfo earliestCreatedUser = getEarliestCreatedFullUser(); if (isMainUserPermanentAdmin()) {
if (earliestCreatedUser != null) { final UserInfo earliestCreatedUser = getEarliestCreatedFullUser();
earliestCreatedUser.flags |= UserInfo.FLAG_MAIN; if (earliestCreatedUser != null) {
userIdsToWrite.add(earliestCreatedUser.id); earliestCreatedUser.flags |= UserInfo.FLAG_MAIN;
userIdsToWrite.add(earliestCreatedUser.id);
}
} }
} else { } else { // not isHeadlessSystemUserMode
synchronized (mUsersLock) { synchronized (mUsersLock) {
final UserData userData = mUsers.get(UserHandle.USER_SYSTEM); final UserData userData = mUsers.get(UserHandle.USER_SYSTEM);
userData.info.flags |= UserInfo.FLAG_MAIN; userData.info.flags |= UserInfo.FLAG_MAIN;

View File

@@ -380,6 +380,7 @@ public class UserManagerServiceShellCommand extends ShellCommand {
final int pid = Process.myPid(); final int pid = Process.myPid();
Slogf.i(LOG_TAG, "Restarting Android runtime(PID=%d) to finalize changes", pid); Slogf.i(LOG_TAG, "Restarting Android runtime(PID=%d) to finalize changes", pid);
pw.println("Restarting Android runtime to finalize changes"); pw.println("Restarting Android runtime to finalize changes");
pw.println("The restart may trigger a 'Broken pipe' message; this is to be expected.");
pw.flush(); pw.flush();
// Ideally there should be a cleaner / safer option to restart system_server, but // Ideally there should be a cleaner / safer option to restart system_server, but