blob: fc01f60d45bd47143c18016a797efb539d2fa721 [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
Amith Yamasanib8151ec2012-04-18 18:02:48 -070019import static android.os.ParcelFileDescriptor.MODE_CREATE;
20import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
21
Amith Yamasani13593602012-03-22 16:16:17 -070022import com.android.internal.util.ArrayUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070023import com.android.internal.util.FastXmlSerializer;
24
Amith Yamasani2a003292012-08-14 18:25:45 -070025import android.app.ActivityManager;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070026import android.app.ActivityManagerNative;
27import android.app.IStopUserCallback;
Amith Yamasani258848d2012-08-10 17:06:33 -070028import android.content.Context;
29import android.content.Intent;
Amith Yamasani0b285492011-04-14 17:35:23 -070030import android.content.pm.PackageManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070031import android.content.pm.UserInfo;
Amith Yamasani258848d2012-08-10 17:06:33 -070032import android.os.Binder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070033import android.os.Environment;
34import android.os.FileUtils;
Amith Yamasani258848d2012-08-10 17:06:33 -070035import android.os.IUserManager;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070036import android.os.ParcelFileDescriptor;
Amith Yamasani258848d2012-08-10 17:06:33 -070037import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070038import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070039import android.os.UserHandle;
Amith Yamasani2a003292012-08-14 18:25:45 -070040import android.util.AtomicFile;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070041import android.util.Slog;
42import android.util.SparseArray;
43import android.util.Xml;
44
45import java.io.BufferedOutputStream;
46import java.io.File;
47import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070048import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070049import java.io.FileOutputStream;
50import java.io.IOException;
51import java.util.ArrayList;
52import java.util.List;
53
54import org.xmlpull.v1.XmlPullParser;
55import org.xmlpull.v1.XmlPullParserException;
56import org.xmlpull.v1.XmlSerializer;
57
Amith Yamasani258848d2012-08-10 17:06:33 -070058public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070059
Amith Yamasani2a003292012-08-14 18:25:45 -070060 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070061
Amith Yamasani4b2e9342011-03-31 12:38:53 -070062 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070063 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070064 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070065 private static final String ATTR_ID = "id";
Amith Yamasani2a003292012-08-14 18:25:45 -070066 private static final String ATTR_SERIAL_NO = "serialNumber";
67 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070068 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070069 private static final String TAG_USER = "user";
70
Amith Yamasani0b285492011-04-14 17:35:23 -070071 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070072 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070073 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070074
Dianne Hackborn4428e172012-08-24 17:43:05 -070075 private final Context mContext;
76 private final PackageManagerService mPm;
77 private final Object mInstallLock;
78 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070079
80 private final File mUsersDir;
81 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -070082 private final File mBaseUserPath;
83
84 private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
85
Amith Yamasani0b285492011-04-14 17:35:23 -070086 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -070087 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -070088 private int mNextSerialNumber;
Amith Yamasani0b285492011-04-14 17:35:23 -070089
Amith Yamasani258848d2012-08-10 17:06:33 -070090 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070091
Dianne Hackborn4428e172012-08-24 17:43:05 -070092 public static UserManagerService getInstance() {
93 synchronized (UserManagerService.class) {
94 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070095 }
Amith Yamasani258848d2012-08-10 17:06:33 -070096 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070097
98 /**
99 * Available for testing purposes.
100 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700101 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700102 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700103 }
104
Dianne Hackborn4428e172012-08-24 17:43:05 -0700105 /**
106 * Called by package manager to create the service. This is closely
107 * associated with the package manager, and the given lock is the
108 * package manager's own lock.
109 */
110 UserManagerService(Context context, PackageManagerService pm,
111 Object installLock, Object packagesLock) {
112 this(context, pm, installLock, packagesLock,
113 Environment.getDataDirectory(),
114 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700115 }
116
Dianne Hackborn4428e172012-08-24 17:43:05 -0700117 /**
118 * Available for testing purposes.
119 */
120 private UserManagerService(Context context, PackageManagerService pm,
121 Object installLock, Object packagesLock,
122 File dataDir, File baseUserPath) {
123 synchronized (UserManagerService.class) {
124 mContext = context;
125 mPm = pm;
126 mInstallLock = installLock;
127 mPackagesLock = packagesLock;
128 mUsersDir = new File(dataDir, USER_INFO_DIR);
129 mUsersDir.mkdirs();
130 // Make zeroth user directory, for services to migrate their files to that location
131 File userZeroDir = new File(mUsersDir, "0");
132 userZeroDir.mkdirs();
133 mBaseUserPath = baseUserPath;
134 FileUtils.setPermissions(mUsersDir.toString(),
135 FileUtils.S_IRWXU|FileUtils.S_IRWXG
136 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
137 -1, -1);
138 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
139 readUserList();
140 sInstance = this;
141 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700142 }
143
144 @Override
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700145 public List<UserInfo> getUsers() {
Amith Yamasani2a003292012-08-14 18:25:45 -0700146 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700147 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700148 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
149 for (int i = 0; i < mUsers.size(); i++) {
150 users.add(mUsers.valueAt(i));
151 }
152 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700153 }
Amith Yamasani13593602012-03-22 16:16:17 -0700154 }
155
Amith Yamasani258848d2012-08-10 17:06:33 -0700156 @Override
157 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700158 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700159 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700160 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700161 }
162 }
163
Amith Yamasani195263742012-08-21 15:40:12 -0700164 /*
165 * Should be locked on mUsers before calling this.
166 */
167 private UserInfo getUserInfoLocked(int userId) {
168 return mUsers.get(userId);
169 }
170
Amith Yamasani13593602012-03-22 16:16:17 -0700171 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700172 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700173 return ArrayUtils.contains(mUserIds, userId);
174 }
175 }
176
Amith Yamasani258848d2012-08-10 17:06:33 -0700177 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700178 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700179 checkManageUsersPermission("rename users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700180 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700181 UserInfo info = mUsers.get(userId);
182 if (name != null && !name.equals(info.name)) {
183 info.name = name;
184 writeUserLocked(info);
185 }
186 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700187 }
188
Amith Yamasani258848d2012-08-10 17:06:33 -0700189 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700190 public ParcelFileDescriptor setUserIcon(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700191 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700192 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700193 UserInfo info = mUsers.get(userId);
194 if (info == null) return null;
195 ParcelFileDescriptor fd = updateIconBitmapLocked(info);
196 if (fd != null) {
197 writeUserLocked(info);
198 }
199 return fd;
200 }
201 }
202
Amith Yamasani258848d2012-08-10 17:06:33 -0700203 @Override
204 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700205 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700206 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700207 if (mGuestEnabled != enable) {
208 mGuestEnabled = enable;
209 // Erase any guest user that currently exists
210 for (int i = 0; i < mUsers.size(); i++) {
211 UserInfo user = mUsers.valueAt(i);
212 if (user.isGuest()) {
213 if (!enable) {
214 removeUser(user.id);
215 }
216 return;
217 }
218 }
219 // No guest was found
220 if (enable) {
221 createUser("Guest", UserInfo.FLAG_GUEST);
222 }
223 }
224 }
225 }
226
227 @Override
228 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700229 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700230 return mGuestEnabled;
231 }
232 }
233
234 @Override
235 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700236 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700237 // TODO:
238 }
239
240 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700241 * Check if we've hit the limit of how many users can be created.
242 */
243 private boolean isUserLimitReached() {
244 synchronized (mInstallLock) {
245 int nUsers = mUsers.size();
246 int userLimit = mContext.getResources().getInteger(
247 com.android.internal.R.integer.config_multiuserMaximumUsers);
248 return nUsers >= userLimit;
249 }
250 }
251
252 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700253 * Enforces that only the system UID or root's UID or apps that have the
254 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
255 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700256 *
257 * @param message used as message if SecurityException is thrown
258 * @throws SecurityException if the caller is not system or root
259 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700260 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700261 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700262 if (uid != Process.SYSTEM_UID && uid != 0
263 && ActivityManager.checkComponentPermission(
264 android.Manifest.permission.MANAGE_USERS,
265 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
266 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700267 }
268 }
269
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700270 private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
271 try {
272 File dir = new File(mUsersDir, Integer.toString(info.id));
273 File file = new File(dir, USER_PHOTO_FILENAME);
274 if (!dir.exists()) {
275 dir.mkdir();
276 FileUtils.setPermissions(
277 dir.getPath(),
278 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
279 -1, -1);
280 }
281 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
282 MODE_CREATE|MODE_READ_WRITE);
283 info.iconPath = file.getAbsolutePath();
284 return fd;
285 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700286 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700287 }
288 return null;
289 }
290
Amith Yamasani0b285492011-04-14 17:35:23 -0700291 /**
292 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
293 * cache it elsewhere.
294 * @return the array of user ids.
295 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700296 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700297 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700298 return mUserIds;
299 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700300 }
301
Dianne Hackborn4428e172012-08-24 17:43:05 -0700302 int[] getUserIdsLPr() {
303 return mUserIds;
304 }
305
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700306 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700307 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700308 readUserListLocked();
309 }
310 }
311
312 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700313 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700314 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700315 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700316 return;
317 }
318 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700319 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700320 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700321 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700322 XmlPullParser parser = Xml.newPullParser();
323 parser.setInput(fis, null);
324 int type;
325 while ((type = parser.next()) != XmlPullParser.START_TAG
326 && type != XmlPullParser.END_DOCUMENT) {
327 ;
328 }
329
330 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700331 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700332 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700333 return;
334 }
335
Amith Yamasani2a003292012-08-14 18:25:45 -0700336 mNextSerialNumber = -1;
337 if (parser.getName().equals(TAG_USERS)) {
338 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
339 if (lastSerialNumber != null) {
340 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
341 }
342 }
343
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700344 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
345 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
346 String id = parser.getAttributeValue(null, ATTR_ID);
347 UserInfo user = readUser(Integer.parseInt(id));
348 if (user != null) {
349 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700350 if (user.isGuest()) {
351 mGuestEnabled = true;
352 }
353 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
354 mNextSerialNumber = user.id + 1;
355 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700356 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700357 }
358 }
Amith Yamasani13593602012-03-22 16:16:17 -0700359 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700360 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700361 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700362 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700363 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800364 } finally {
365 if (fis != null) {
366 try {
367 fis.close();
368 } catch (IOException e) {
369 }
370 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700371 }
372 }
373
Amith Yamasani13593602012-03-22 16:16:17 -0700374 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700375 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700376 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700377 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
378 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700379 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700380
Amith Yamasani13593602012-03-22 16:16:17 -0700381 writeUserListLocked();
382 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700383 }
384
385 /*
386 * Writes the user file in this format:
387 *
388 * <user flags="20039023" id="0">
389 * <name>Primary</name>
390 * </user>
391 */
Amith Yamasani13593602012-03-22 16:16:17 -0700392 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700393 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700394 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700395 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700396 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700397 final BufferedOutputStream bos = new BufferedOutputStream(fos);
398
399 // XmlSerializer serializer = XmlUtils.serializerInstance();
400 final XmlSerializer serializer = new FastXmlSerializer();
401 serializer.setOutput(bos, "utf-8");
402 serializer.startDocument(null, true);
403 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
404
405 serializer.startTag(null, TAG_USER);
406 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700407 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700408 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700409 if (userInfo.iconPath != null) {
410 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
411 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700412
413 serializer.startTag(null, TAG_NAME);
414 serializer.text(userInfo.name);
415 serializer.endTag(null, TAG_NAME);
416
417 serializer.endTag(null, TAG_USER);
418
419 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700420 userFile.finishWrite(fos);
421 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700422 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700423 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700424 }
425 }
426
427 /*
428 * Writes the user list file in this format:
429 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700430 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700431 * <user id="0"></user>
432 * <user id="2"></user>
433 * </users>
434 */
Amith Yamasani13593602012-03-22 16:16:17 -0700435 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700436 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700437 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700438 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700439 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700440 final BufferedOutputStream bos = new BufferedOutputStream(fos);
441
442 // XmlSerializer serializer = XmlUtils.serializerInstance();
443 final XmlSerializer serializer = new FastXmlSerializer();
444 serializer.setOutput(bos, "utf-8");
445 serializer.startDocument(null, true);
446 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
447
448 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700449 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700450
451 for (int i = 0; i < mUsers.size(); i++) {
452 UserInfo user = mUsers.valueAt(i);
453 serializer.startTag(null, TAG_USER);
454 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
455 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700456 }
457
458 serializer.endTag(null, TAG_USERS);
459
460 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700461 userListFile.finishWrite(fos);
462 } catch (Exception e) {
463 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700464 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700465 }
466 }
467
468 private UserInfo readUser(int id) {
469 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700470 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700471 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700472 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700473
474 FileInputStream fis = null;
475 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700476 AtomicFile userFile =
477 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
478 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700479 XmlPullParser parser = Xml.newPullParser();
480 parser.setInput(fis, null);
481 int type;
482 while ((type = parser.next()) != XmlPullParser.START_TAG
483 && type != XmlPullParser.END_DOCUMENT) {
484 ;
485 }
486
487 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700488 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700489 return null;
490 }
491
492 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
493 String storedId = parser.getAttributeValue(null, ATTR_ID);
494 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700495 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700496 return null;
497 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700498 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
499 if (serialNumberValue != null) {
500 serialNumber = Integer.parseInt(serialNumberValue);
501 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700502 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
503 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700504 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700505
506 while ((type = parser.next()) != XmlPullParser.START_TAG
507 && type != XmlPullParser.END_DOCUMENT) {
508 }
509 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
510 type = parser.next();
511 if (type == XmlPullParser.TEXT) {
512 name = parser.getText();
513 }
514 }
515 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700516
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700517 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700518 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700519 return userInfo;
520
521 } catch (IOException ioe) {
522 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800523 } finally {
524 if (fis != null) {
525 try {
526 fis.close();
527 } catch (IOException e) {
528 }
529 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700530 }
531 return null;
532 }
533
Amith Yamasani258848d2012-08-10 17:06:33 -0700534 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700535 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700536 checkManageUsersPermission("Only the system can create users");
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700537
538 if (isUserLimitReached()) return null;
539
Amith Yamasani0b285492011-04-14 17:35:23 -0700540 int userId = getNextAvailableId();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700541 UserInfo userInfo = new UserInfo(userId, name, null, flags);
Amith Yamasani0b285492011-04-14 17:35:23 -0700542 File userPath = new File(mBaseUserPath, Integer.toString(userId));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700543 synchronized (mInstallLock) {
544 synchronized (mPackagesLock) {
545 userInfo.serialNumber = mNextSerialNumber++;
546 mUsers.put(userId, userInfo);
547 writeUserListLocked();
548 writeUserLocked(userInfo);
549 updateUserIdsLocked();
550 mPm.createNewUserLILPw(userId, userPath);
551 }
Amith Yamasani13593602012-03-22 16:16:17 -0700552 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700553 if (userInfo != null) {
554 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
Amith Yamasani2a003292012-08-14 18:25:45 -0700555 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
Amith Yamasanifc6e0ca2012-08-17 17:07:14 -0700556 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_BOOT_COMPLETED),
557 new UserHandle(userInfo.id));
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700558 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
559 android.Manifest.permission.MANAGE_USERS);
Amith Yamasani258848d2012-08-10 17:06:33 -0700560 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700561 return userInfo;
562 }
563
Amith Yamasani0b285492011-04-14 17:35:23 -0700564 /**
565 * Removes a user and all data directories created for that user. This method should be called
566 * after the user's processes have been terminated.
567 * @param id the user's id
568 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700569 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700570 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700571 final UserInfo user;
572 synchronized (mPackagesLock) {
573 user = mUsers.get(userHandle);
574 if (userHandle == 0 || user == null) {
575 return false;
576 }
577 }
578
579 int res;
580 try {
581 res = ActivityManagerNative.getDefault().stopUser(userHandle,
582 new IStopUserCallback.Stub() {
583 @Override
584 public void userStopped(int userId) {
585 finishRemoveUser(userId);
586 }
587 @Override
588 public void userStopAborted(int userId) {
589 }
590 });
591 } catch (RemoteException e) {
592 return false;
593 }
594
595 return res == ActivityManager.USER_OP_SUCCESS;
596 }
597
598 void finishRemoveUser(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700599 synchronized (mInstallLock) {
600 synchronized (mPackagesLock) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700601 // Cleanup package manager settings
602 mPm.cleanUpUserLILPw(userHandle);
603
604 // Remove this user from the list
605 mUsers.remove(userHandle);
606 // Remove user file
607 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
608 userFile.delete();
609 // Update the user list
610 writeUserListLocked();
611 updateUserIdsLocked();
Amith Yamasani61f57372012-08-31 12:12:28 -0700612 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700613 }
614 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700615
Amith Yamasani2a003292012-08-14 18:25:45 -0700616 // Let other services shutdown any activity
617 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
618 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
619 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700620 }
621
Amith Yamasani61f57372012-08-31 12:12:28 -0700622 private void removeDirectoryRecursive(File parent) {
623 if (parent.isDirectory()) {
624 String[] files = parent.list();
625 for (String filename : files) {
626 File child = new File(parent, filename);
627 removeDirectoryRecursive(child);
628 }
629 }
630 parent.delete();
631 }
632
Amith Yamasani2a003292012-08-14 18:25:45 -0700633 @Override
634 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700635 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700636 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700637 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700638 }
639 }
640
641 @Override
642 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700643 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700644 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700645 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700646 }
647 // Not found
648 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700649 }
650 }
651
Amith Yamasani0b285492011-04-14 17:35:23 -0700652 /**
653 * Caches the list of user ids in an array, adjusting the array size when necessary.
654 */
Amith Yamasani13593602012-03-22 16:16:17 -0700655 private void updateUserIdsLocked() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700656 int[] newUsers = new int[mUsers.size()];
Amith Yamasani0b285492011-04-14 17:35:23 -0700657 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700658 newUsers[i] = mUsers.keyAt(i);
Amith Yamasani0b285492011-04-14 17:35:23 -0700659 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700660 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700661 }
662
663 /**
664 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700665 * TODO: May not be a good idea to recycle ids, in case it results in confusion
666 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700667 * @return
668 */
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700669 private int getNextAvailableId() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700670 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700671 int i = 0;
672 while (i < Integer.MAX_VALUE) {
673 if (mUsers.indexOfKey(i) < 0) {
674 break;
675 }
676 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700677 }
Amith Yamasani195263742012-08-21 15:40:12 -0700678 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700679 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700680 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700681}