blob: b13bb681598fdfc044a01d6f6bd344d881ff4475 [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;
Amith Yamasani258848d2012-08-10 17:06:33 -070026import android.content.Context;
27import android.content.Intent;
Amith Yamasani0b285492011-04-14 17:35:23 -070028import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070030import android.content.pm.UserInfo;
Amith Yamasani258848d2012-08-10 17:06:33 -070031import android.os.Binder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070032import android.os.Environment;
33import android.os.FileUtils;
Amith Yamasani258848d2012-08-10 17:06:33 -070034import android.os.IUserManager;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070035import android.os.ParcelFileDescriptor;
Amith Yamasani258848d2012-08-10 17:06:33 -070036import android.os.Process;
Amith Yamasani0b285492011-04-14 17:35:23 -070037import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070038import android.os.UserHandle;
Amith Yamasani2a003292012-08-14 18:25:45 -070039import android.util.AtomicFile;
Amith Yamasani0b285492011-04-14 17:35:23 -070040import android.util.Log;
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
Amith Yamasani13593602012-03-22 16:16:17 -070075 private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
Amith Yamasani4b2e9342011-03-31 12:38:53 -070076
77 private final File mUsersDir;
78 private final File mUserListFile;
Amith Yamasani0b285492011-04-14 17:35:23 -070079 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -070080 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -070081 private int mNextSerialNumber;
Amith Yamasani0b285492011-04-14 17:35:23 -070082
83 private Installer mInstaller;
84 private File mBaseUserPath;
Amith Yamasani258848d2012-08-10 17:06:33 -070085 private Context mContext;
86 private static UserManagerService sInstance;
87 private PackageManagerService mPm;
88
89 public synchronized static UserManagerService getInstance(Context context) {
90 if (sInstance == null) {
91 sInstance = new UserManagerService(context);
92 }
93 return sInstance;
94 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070095
96 /**
97 * Available for testing purposes.
98 */
Amith Yamasani258848d2012-08-10 17:06:33 -070099 UserManagerService(File dataDir, File baseUserPath) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700100 mUsersDir = new File(dataDir, USER_INFO_DIR);
101 mUsersDir.mkdirs();
Amith Yamasani483f3b02012-03-13 16:08:00 -0700102 // Make zeroth user directory, for services to migrate their files to that location
103 File userZeroDir = new File(mUsersDir, "0");
104 userZeroDir.mkdirs();
Amith Yamasani0b285492011-04-14 17:35:23 -0700105 mBaseUserPath = baseUserPath;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700106 FileUtils.setPermissions(mUsersDir.toString(),
107 FileUtils.S_IRWXU|FileUtils.S_IRWXG
108 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
109 -1, -1);
110 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
111 readUserList();
112 }
113
Amith Yamasani258848d2012-08-10 17:06:33 -0700114 public UserManagerService(Context context) {
115 this(Environment.getDataDirectory(), new File(Environment.getDataDirectory(), "user"));
116 mContext = context;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700117 }
118
Amith Yamasani258848d2012-08-10 17:06:33 -0700119 void setInstaller(PackageManagerService pm, Installer installer) {
120 mInstaller = installer;
121 mPm = pm;
122 }
123
124 @Override
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700125 public List<UserInfo> getUsers() {
Amith Yamasani2a003292012-08-14 18:25:45 -0700126 checkManageUsersPermission("query users");
Amith Yamasani13593602012-03-22 16:16:17 -0700127 synchronized (mUsers) {
128 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
129 for (int i = 0; i < mUsers.size(); i++) {
130 users.add(mUsers.valueAt(i));
131 }
132 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700133 }
Amith Yamasani13593602012-03-22 16:16:17 -0700134 }
135
Amith Yamasani258848d2012-08-10 17:06:33 -0700136 @Override
137 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700138 checkManageUsersPermission("query user");
Amith Yamasani13593602012-03-22 16:16:17 -0700139 synchronized (mUsers) {
140 UserInfo info = mUsers.get(userId);
141 return info;
142 }
143 }
144
145 public boolean exists(int userId) {
146 synchronized (mUsers) {
147 return ArrayUtils.contains(mUserIds, userId);
148 }
149 }
150
Amith Yamasani258848d2012-08-10 17:06:33 -0700151 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700152 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700153 checkManageUsersPermission("rename users");
Amith Yamasani13593602012-03-22 16:16:17 -0700154 synchronized (mUsers) {
155 UserInfo info = mUsers.get(userId);
156 if (name != null && !name.equals(info.name)) {
157 info.name = name;
158 writeUserLocked(info);
159 }
160 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700161 }
162
Amith Yamasani258848d2012-08-10 17:06:33 -0700163 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700164 public ParcelFileDescriptor setUserIcon(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700165 checkManageUsersPermission("update users");
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700166 synchronized (mUsers) {
167 UserInfo info = mUsers.get(userId);
168 if (info == null) return null;
169 ParcelFileDescriptor fd = updateIconBitmapLocked(info);
170 if (fd != null) {
171 writeUserLocked(info);
172 }
173 return fd;
174 }
175 }
176
Amith Yamasani258848d2012-08-10 17:06:33 -0700177 @Override
178 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700179 checkManageUsersPermission("enable guest users");
Amith Yamasani258848d2012-08-10 17:06:33 -0700180 synchronized (mUsers) {
181 if (mGuestEnabled != enable) {
182 mGuestEnabled = enable;
183 // Erase any guest user that currently exists
184 for (int i = 0; i < mUsers.size(); i++) {
185 UserInfo user = mUsers.valueAt(i);
186 if (user.isGuest()) {
187 if (!enable) {
188 removeUser(user.id);
189 }
190 return;
191 }
192 }
193 // No guest was found
194 if (enable) {
195 createUser("Guest", UserInfo.FLAG_GUEST);
196 }
197 }
198 }
199 }
200
201 @Override
202 public boolean isGuestEnabled() {
203 synchronized (mUsers) {
204 return mGuestEnabled;
205 }
206 }
207
208 @Override
209 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700210 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700211 // TODO:
212 }
213
214 /**
215 * Enforces that only the system UID or root's UID can call a method exposed
216 * via Binder.
217 *
218 * @param message used as message if SecurityException is thrown
219 * @throws SecurityException if the caller is not system or root
220 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700221 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700222 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700223 if (uid != Process.SYSTEM_UID && uid != 0
224 && ActivityManager.checkComponentPermission(
225 android.Manifest.permission.MANAGE_USERS,
226 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
227 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700228 }
229 }
230
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700231 private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
232 try {
233 File dir = new File(mUsersDir, Integer.toString(info.id));
234 File file = new File(dir, USER_PHOTO_FILENAME);
235 if (!dir.exists()) {
236 dir.mkdir();
237 FileUtils.setPermissions(
238 dir.getPath(),
239 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
240 -1, -1);
241 }
242 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
243 MODE_CREATE|MODE_READ_WRITE);
244 info.iconPath = file.getAbsolutePath();
245 return fd;
246 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700247 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700248 }
249 return null;
250 }
251
Amith Yamasani0b285492011-04-14 17:35:23 -0700252 /**
253 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
254 * cache it elsewhere.
255 * @return the array of user ids.
256 */
257 int[] getUserIds() {
258 return mUserIds;
259 }
260
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700261 private void readUserList() {
Amith Yamasani13593602012-03-22 16:16:17 -0700262 synchronized (mUsers) {
263 readUserListLocked();
264 }
265 }
266
267 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700268 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700269 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700270 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700271 return;
272 }
273 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700274 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700275 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700276 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700277 XmlPullParser parser = Xml.newPullParser();
278 parser.setInput(fis, null);
279 int type;
280 while ((type = parser.next()) != XmlPullParser.START_TAG
281 && type != XmlPullParser.END_DOCUMENT) {
282 ;
283 }
284
285 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700286 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700287 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700288 return;
289 }
290
Amith Yamasani2a003292012-08-14 18:25:45 -0700291 mNextSerialNumber = -1;
292 if (parser.getName().equals(TAG_USERS)) {
293 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
294 if (lastSerialNumber != null) {
295 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
296 }
297 }
298
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700299 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
300 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
301 String id = parser.getAttributeValue(null, ATTR_ID);
302 UserInfo user = readUser(Integer.parseInt(id));
303 if (user != null) {
304 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700305 if (user.isGuest()) {
306 mGuestEnabled = true;
307 }
308 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
309 mNextSerialNumber = user.id + 1;
310 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700311 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700312 }
313 }
Amith Yamasani13593602012-03-22 16:16:17 -0700314 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700315 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700316 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700317 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700318 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800319 } finally {
320 if (fis != null) {
321 try {
322 fis.close();
323 } catch (IOException e) {
324 }
325 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700326 }
327 }
328
Amith Yamasani13593602012-03-22 16:16:17 -0700329 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700330 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700331 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700332 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
333 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700334 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700335
Amith Yamasani13593602012-03-22 16:16:17 -0700336 writeUserListLocked();
337 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700338 }
339
340 /*
341 * Writes the user file in this format:
342 *
343 * <user flags="20039023" id="0">
344 * <name>Primary</name>
345 * </user>
346 */
Amith Yamasani13593602012-03-22 16:16:17 -0700347 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700348 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700349 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700350 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700351 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700352 final BufferedOutputStream bos = new BufferedOutputStream(fos);
353
354 // XmlSerializer serializer = XmlUtils.serializerInstance();
355 final XmlSerializer serializer = new FastXmlSerializer();
356 serializer.setOutput(bos, "utf-8");
357 serializer.startDocument(null, true);
358 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
359
360 serializer.startTag(null, TAG_USER);
361 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700362 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700363 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700364 if (userInfo.iconPath != null) {
365 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
366 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700367
368 serializer.startTag(null, TAG_NAME);
369 serializer.text(userInfo.name);
370 serializer.endTag(null, TAG_NAME);
371
372 serializer.endTag(null, TAG_USER);
373
374 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700375 userFile.finishWrite(fos);
376 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700377 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700378 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700379 }
380 }
381
382 /*
383 * Writes the user list file in this format:
384 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700385 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700386 * <user id="0"></user>
387 * <user id="2"></user>
388 * </users>
389 */
Amith Yamasani13593602012-03-22 16:16:17 -0700390 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700391 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700392 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700393 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700394 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700395 final BufferedOutputStream bos = new BufferedOutputStream(fos);
396
397 // XmlSerializer serializer = XmlUtils.serializerInstance();
398 final XmlSerializer serializer = new FastXmlSerializer();
399 serializer.setOutput(bos, "utf-8");
400 serializer.startDocument(null, true);
401 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
402
403 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700404 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700405
406 for (int i = 0; i < mUsers.size(); i++) {
407 UserInfo user = mUsers.valueAt(i);
408 serializer.startTag(null, TAG_USER);
409 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
410 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700411 }
412
413 serializer.endTag(null, TAG_USERS);
414
415 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700416 userListFile.finishWrite(fos);
417 } catch (Exception e) {
418 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700419 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700420 }
421 }
422
423 private UserInfo readUser(int id) {
424 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700425 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700426 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700427 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700428
429 FileInputStream fis = null;
430 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700431 AtomicFile userFile =
432 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
433 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700434 XmlPullParser parser = Xml.newPullParser();
435 parser.setInput(fis, null);
436 int type;
437 while ((type = parser.next()) != XmlPullParser.START_TAG
438 && type != XmlPullParser.END_DOCUMENT) {
439 ;
440 }
441
442 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700443 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700444 return null;
445 }
446
447 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
448 String storedId = parser.getAttributeValue(null, ATTR_ID);
449 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700450 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700451 return null;
452 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700453 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
454 if (serialNumberValue != null) {
455 serialNumber = Integer.parseInt(serialNumberValue);
456 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700457 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
458 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700459 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700460
461 while ((type = parser.next()) != XmlPullParser.START_TAG
462 && type != XmlPullParser.END_DOCUMENT) {
463 }
464 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
465 type = parser.next();
466 if (type == XmlPullParser.TEXT) {
467 name = parser.getText();
468 }
469 }
470 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700471
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700472 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700473 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700474 return userInfo;
475
476 } catch (IOException ioe) {
477 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800478 } finally {
479 if (fis != null) {
480 try {
481 fis.close();
482 } catch (IOException e) {
483 }
484 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700485 }
486 return null;
487 }
488
Amith Yamasani258848d2012-08-10 17:06:33 -0700489 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700490 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700491 checkManageUsersPermission("Only the system can create users");
Amith Yamasani0b285492011-04-14 17:35:23 -0700492 int userId = getNextAvailableId();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700493 UserInfo userInfo = new UserInfo(userId, name, null, flags);
Amith Yamasani0b285492011-04-14 17:35:23 -0700494 File userPath = new File(mBaseUserPath, Integer.toString(userId));
Amith Yamasani13593602012-03-22 16:16:17 -0700495 if (!createPackageFolders(userId, userPath)) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700496 return null;
497 }
Amith Yamasani13593602012-03-22 16:16:17 -0700498 synchronized (mUsers) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700499 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani13593602012-03-22 16:16:17 -0700500 mUsers.put(userId, userInfo);
501 writeUserListLocked();
502 writeUserLocked(userInfo);
503 updateUserIdsLocked();
504 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700505 if (userInfo != null) {
506 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
Amith Yamasani2a003292012-08-14 18:25:45 -0700507 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
508 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Amith Yamasanifc6e0ca2012-08-17 17:07:14 -0700509 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_BOOT_COMPLETED),
510 new UserHandle(userInfo.id));
Amith Yamasani258848d2012-08-10 17:06:33 -0700511 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700512 return userInfo;
513 }
514
Amith Yamasani0b285492011-04-14 17:35:23 -0700515 /**
516 * Removes a user and all data directories created for that user. This method should be called
517 * after the user's processes have been terminated.
518 * @param id the user's id
519 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700520 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700521 checkManageUsersPermission("Only the system can remove users");
522 boolean result;
Amith Yamasani13593602012-03-22 16:16:17 -0700523 synchronized (mUsers) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700524 result = removeUserLocked(userHandle);
525 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700526
527 // Cleanup package manager settings
528 mPm.cleanUpUser(userHandle);
529
Amith Yamasani2a003292012-08-14 18:25:45 -0700530 // Let other services shutdown any activity
531 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
532 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
533 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
534 return result;
535 }
536
537 @Override
538 public int getUserSerialNumber(int userHandle) {
539 synchronized (mUsers) {
540 if (!exists(userHandle)) return -1;
541 return getUserInfo(userHandle).serialNumber;
542 }
543 }
544
545 @Override
546 public int getUserHandle(int userSerialNumber) {
547 synchronized (mUsers) {
548 for (int userId : mUserIds) {
549 if (getUserInfo(userId).serialNumber == userSerialNumber) return userId;
550 }
551 // Not found
552 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700553 }
554 }
555
Amith Yamasani258848d2012-08-10 17:06:33 -0700556 private boolean removeUserLocked(int userHandle) {
557 final UserInfo user = mUsers.get(userHandle);
558 if (userHandle == 0 || user == null) {
559 return false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700560 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700561
Amith Yamasani258848d2012-08-10 17:06:33 -0700562 // Remove this user from the list
563 mUsers.remove(userHandle);
564 // Remove user file
Amith Yamasani2a003292012-08-14 18:25:45 -0700565 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
Amith Yamasani258848d2012-08-10 17:06:33 -0700566 userFile.delete();
567 // Update the user list
568 writeUserListLocked();
569 updateUserIdsLocked();
570
Amith Yamasani258848d2012-08-10 17:06:33 -0700571 removePackageFolders(userHandle);
572 return true;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700573 }
574
Amith Yamasani0b285492011-04-14 17:35:23 -0700575 public void installPackageForAllUsers(String packageName, int uid) {
576 for (int userId : mUserIds) {
577 // Don't do it for the primary user, it will become recursive.
578 if (userId == 0)
579 continue;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700580 mInstaller.createUserData(packageName, UserHandle.getUid(userId, uid),
Amith Yamasani0b285492011-04-14 17:35:23 -0700581 userId);
582 }
583 }
584
585 public void clearUserDataForAllUsers(String packageName) {
586 for (int userId : mUserIds) {
587 // Don't do it for the primary user, it will become recursive.
588 if (userId == 0)
589 continue;
590 mInstaller.clearUserData(packageName, userId);
591 }
592 }
593
594 public void removePackageForAllUsers(String packageName) {
595 for (int userId : mUserIds) {
596 // Don't do it for the primary user, it will become recursive.
597 if (userId == 0)
598 continue;
599 mInstaller.remove(packageName, userId);
600 }
601 }
602
603 /**
604 * Caches the list of user ids in an array, adjusting the array size when necessary.
605 */
Amith Yamasani13593602012-03-22 16:16:17 -0700606 private void updateUserIdsLocked() {
Amith Yamasani0b285492011-04-14 17:35:23 -0700607 if (mUserIds == null || mUserIds.length != mUsers.size()) {
608 mUserIds = new int[mUsers.size()];
609 }
610 for (int i = 0; i < mUsers.size(); i++) {
611 mUserIds[i] = mUsers.keyAt(i);
612 }
613 }
614
615 /**
616 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700617 * TODO: May not be a good idea to recycle ids, in case it results in confusion
618 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700619 * @return
620 */
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700621 private int getNextAvailableId() {
622 int i = 0;
623 while (i < Integer.MAX_VALUE) {
624 if (mUsers.indexOfKey(i) < 0) {
625 break;
626 }
627 i++;
628 }
629 return i;
630 }
631
Amith Yamasani13593602012-03-22 16:16:17 -0700632 private boolean createPackageFolders(int id, File userPath) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700633 // mInstaller may not be available for unit-tests.
Amith Yamasani13593602012-03-22 16:16:17 -0700634 if (mInstaller == null) return true;
Amith Yamasani0b285492011-04-14 17:35:23 -0700635
Amith Yamasani0b285492011-04-14 17:35:23 -0700636 // Create the user path
637 userPath.mkdir();
638 FileUtils.setPermissions(userPath.toString(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
639 | FileUtils.S_IXOTH, -1, -1);
640
Amith Yamasani742a6712011-05-04 14:49:28 -0700641 mInstaller.cloneUserData(0, id, false);
642
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700643 return true;
644 }
645
Amith Yamasani13593602012-03-22 16:16:17 -0700646 boolean removePackageFolders(int id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700647 // mInstaller may not be available for unit-tests.
648 if (mInstaller == null) return true;
649
650 mInstaller.removeUserDataDirs(id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700651 return true;
652 }
653}