blob: a828864035505672bec640ada09422df50dc2868 [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 Yamasani258848d2012-08-10 17:06:33 -0700509 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700510 return userInfo;
511 }
512
Amith Yamasani0b285492011-04-14 17:35:23 -0700513 /**
514 * Removes a user and all data directories created for that user. This method should be called
515 * after the user's processes have been terminated.
516 * @param id the user's id
517 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700518 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700519 checkManageUsersPermission("Only the system can remove users");
520 boolean result;
Amith Yamasani13593602012-03-22 16:16:17 -0700521 synchronized (mUsers) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700522 result = removeUserLocked(userHandle);
523 }
524 // Let other services shutdown any activity
525 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
526 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
527 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
528 return result;
529 }
530
531 @Override
532 public int getUserSerialNumber(int userHandle) {
533 synchronized (mUsers) {
534 if (!exists(userHandle)) return -1;
535 return getUserInfo(userHandle).serialNumber;
536 }
537 }
538
539 @Override
540 public int getUserHandle(int userSerialNumber) {
541 synchronized (mUsers) {
542 for (int userId : mUserIds) {
543 if (getUserInfo(userId).serialNumber == userSerialNumber) return userId;
544 }
545 // Not found
546 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700547 }
548 }
549
Amith Yamasani258848d2012-08-10 17:06:33 -0700550 private boolean removeUserLocked(int userHandle) {
551 final UserInfo user = mUsers.get(userHandle);
552 if (userHandle == 0 || user == null) {
553 return false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700554 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700555
556 mPm.cleanUpUser(userHandle);
557
558 // Remove this user from the list
559 mUsers.remove(userHandle);
560 // Remove user file
Amith Yamasani2a003292012-08-14 18:25:45 -0700561 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
Amith Yamasani258848d2012-08-10 17:06:33 -0700562 userFile.delete();
563 // Update the user list
564 writeUserListLocked();
565 updateUserIdsLocked();
566
Amith Yamasani258848d2012-08-10 17:06:33 -0700567 removePackageFolders(userHandle);
568 return true;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700569 }
570
Amith Yamasani0b285492011-04-14 17:35:23 -0700571 public void installPackageForAllUsers(String packageName, int uid) {
572 for (int userId : mUserIds) {
573 // Don't do it for the primary user, it will become recursive.
574 if (userId == 0)
575 continue;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700576 mInstaller.createUserData(packageName, UserHandle.getUid(userId, uid),
Amith Yamasani0b285492011-04-14 17:35:23 -0700577 userId);
578 }
579 }
580
581 public void clearUserDataForAllUsers(String packageName) {
582 for (int userId : mUserIds) {
583 // Don't do it for the primary user, it will become recursive.
584 if (userId == 0)
585 continue;
586 mInstaller.clearUserData(packageName, userId);
587 }
588 }
589
590 public void removePackageForAllUsers(String packageName) {
591 for (int userId : mUserIds) {
592 // Don't do it for the primary user, it will become recursive.
593 if (userId == 0)
594 continue;
595 mInstaller.remove(packageName, userId);
596 }
597 }
598
599 /**
600 * Caches the list of user ids in an array, adjusting the array size when necessary.
601 */
Amith Yamasani13593602012-03-22 16:16:17 -0700602 private void updateUserIdsLocked() {
Amith Yamasani0b285492011-04-14 17:35:23 -0700603 if (mUserIds == null || mUserIds.length != mUsers.size()) {
604 mUserIds = new int[mUsers.size()];
605 }
606 for (int i = 0; i < mUsers.size(); i++) {
607 mUserIds[i] = mUsers.keyAt(i);
608 }
609 }
610
611 /**
612 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700613 * TODO: May not be a good idea to recycle ids, in case it results in confusion
614 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700615 * @return
616 */
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700617 private int getNextAvailableId() {
618 int i = 0;
619 while (i < Integer.MAX_VALUE) {
620 if (mUsers.indexOfKey(i) < 0) {
621 break;
622 }
623 i++;
624 }
625 return i;
626 }
627
Amith Yamasani13593602012-03-22 16:16:17 -0700628 private boolean createPackageFolders(int id, File userPath) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700629 // mInstaller may not be available for unit-tests.
Amith Yamasani13593602012-03-22 16:16:17 -0700630 if (mInstaller == null) return true;
Amith Yamasani0b285492011-04-14 17:35:23 -0700631
Amith Yamasani0b285492011-04-14 17:35:23 -0700632 // Create the user path
633 userPath.mkdir();
634 FileUtils.setPermissions(userPath.toString(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
635 | FileUtils.S_IXOTH, -1, -1);
636
Amith Yamasani742a6712011-05-04 14:49:28 -0700637 mInstaller.cloneUserData(0, id, false);
638
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700639 return true;
640 }
641
Amith Yamasani13593602012-03-22 16:16:17 -0700642 boolean removePackageFolders(int id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700643 // mInstaller may not be available for unit-tests.
644 if (mInstaller == null) return true;
645
646 mInstaller.removeUserDataDirs(id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700647 return true;
648 }
649}