blob: a58c4ea6fba607284af09b334fae8903102a7057 [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
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070086 private final int mUserLimit;
87
Amith Yamasani0b285492011-04-14 17:35:23 -070088 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -070089 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -070090 private int mNextSerialNumber;
Amith Yamasani0b285492011-04-14 17:35:23 -070091
Amith Yamasani258848d2012-08-10 17:06:33 -070092 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070093
Dianne Hackborn4428e172012-08-24 17:43:05 -070094 public static UserManagerService getInstance() {
95 synchronized (UserManagerService.class) {
96 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070097 }
Amith Yamasani258848d2012-08-10 17:06:33 -070098 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070099
100 /**
101 * Available for testing purposes.
102 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700103 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700104 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700105 }
106
Dianne Hackborn4428e172012-08-24 17:43:05 -0700107 /**
108 * Called by package manager to create the service. This is closely
109 * associated with the package manager, and the given lock is the
110 * package manager's own lock.
111 */
112 UserManagerService(Context context, PackageManagerService pm,
113 Object installLock, Object packagesLock) {
114 this(context, pm, installLock, packagesLock,
115 Environment.getDataDirectory(),
116 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700117 }
118
Dianne Hackborn4428e172012-08-24 17:43:05 -0700119 /**
120 * Available for testing purposes.
121 */
122 private UserManagerService(Context context, PackageManagerService pm,
123 Object installLock, Object packagesLock,
124 File dataDir, File baseUserPath) {
125 synchronized (UserManagerService.class) {
126 mContext = context;
127 mPm = pm;
128 mInstallLock = installLock;
129 mPackagesLock = packagesLock;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700130 mUserLimit = mContext.getResources().getInteger(
131 com.android.internal.R.integer.config_multiuserMaximumUsers);
Dianne Hackborn4428e172012-08-24 17:43:05 -0700132 mUsersDir = new File(dataDir, USER_INFO_DIR);
133 mUsersDir.mkdirs();
134 // Make zeroth user directory, for services to migrate their files to that location
135 File userZeroDir = new File(mUsersDir, "0");
136 userZeroDir.mkdirs();
137 mBaseUserPath = baseUserPath;
138 FileUtils.setPermissions(mUsersDir.toString(),
139 FileUtils.S_IRWXU|FileUtils.S_IRWXG
140 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
141 -1, -1);
142 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
143 readUserList();
144 sInstance = this;
145 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700146 }
147
148 @Override
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700149 public List<UserInfo> getUsers() {
Amith Yamasani2a003292012-08-14 18:25:45 -0700150 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700151 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700152 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
153 for (int i = 0; i < mUsers.size(); i++) {
154 users.add(mUsers.valueAt(i));
155 }
156 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700157 }
Amith Yamasani13593602012-03-22 16:16:17 -0700158 }
159
Amith Yamasani258848d2012-08-10 17:06:33 -0700160 @Override
161 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700162 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700163 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700164 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700165 }
166 }
167
Amith Yamasani195263742012-08-21 15:40:12 -0700168 /*
169 * Should be locked on mUsers before calling this.
170 */
171 private UserInfo getUserInfoLocked(int userId) {
172 return mUsers.get(userId);
173 }
174
Amith Yamasani13593602012-03-22 16:16:17 -0700175 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700176 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700177 return ArrayUtils.contains(mUserIds, userId);
178 }
179 }
180
Amith Yamasani258848d2012-08-10 17:06:33 -0700181 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700182 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700183 checkManageUsersPermission("rename users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700184 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700185 UserInfo info = mUsers.get(userId);
186 if (name != null && !name.equals(info.name)) {
187 info.name = name;
188 writeUserLocked(info);
189 }
190 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700191 }
192
Amith Yamasani258848d2012-08-10 17:06:33 -0700193 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700194 public ParcelFileDescriptor setUserIcon(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700195 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700196 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700197 UserInfo info = mUsers.get(userId);
198 if (info == null) return null;
199 ParcelFileDescriptor fd = updateIconBitmapLocked(info);
200 if (fd != null) {
201 writeUserLocked(info);
202 }
203 return fd;
204 }
205 }
206
Amith Yamasani258848d2012-08-10 17:06:33 -0700207 @Override
208 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700209 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700210 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700211 if (mGuestEnabled != enable) {
212 mGuestEnabled = enable;
213 // Erase any guest user that currently exists
214 for (int i = 0; i < mUsers.size(); i++) {
215 UserInfo user = mUsers.valueAt(i);
216 if (user.isGuest()) {
217 if (!enable) {
218 removeUser(user.id);
219 }
220 return;
221 }
222 }
223 // No guest was found
224 if (enable) {
225 createUser("Guest", UserInfo.FLAG_GUEST);
226 }
227 }
228 }
229 }
230
231 @Override
232 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700233 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700234 return mGuestEnabled;
235 }
236 }
237
238 @Override
239 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700240 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700241 // TODO:
242 }
243
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700244 public void makeInitialized(int userId) {
245 checkManageUsersPermission("makeInitialized");
246 synchronized (mPackagesLock) {
247 UserInfo info = mUsers.get(userId);
248 if (info != null && (info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
249 info.flags |= UserInfo.FLAG_INITIALIZED;
250 writeUserLocked(info);
251 }
252 }
253 }
254
Amith Yamasani258848d2012-08-10 17:06:33 -0700255 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700256 * Check if we've hit the limit of how many users can be created.
257 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700258 private boolean isUserLimitReachedLocked() {
259 int nUsers = mUsers.size();
260 return nUsers >= mUserLimit;
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700261 }
262
263 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700264 * Enforces that only the system UID or root's UID or apps that have the
265 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
266 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700267 *
268 * @param message used as message if SecurityException is thrown
269 * @throws SecurityException if the caller is not system or root
270 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700271 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700272 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700273 if (uid != Process.SYSTEM_UID && uid != 0
274 && ActivityManager.checkComponentPermission(
275 android.Manifest.permission.MANAGE_USERS,
276 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
277 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700278 }
279 }
280
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700281 private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
282 try {
283 File dir = new File(mUsersDir, Integer.toString(info.id));
284 File file = new File(dir, USER_PHOTO_FILENAME);
285 if (!dir.exists()) {
286 dir.mkdir();
287 FileUtils.setPermissions(
288 dir.getPath(),
289 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
290 -1, -1);
291 }
292 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
293 MODE_CREATE|MODE_READ_WRITE);
294 info.iconPath = file.getAbsolutePath();
295 return fd;
296 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700297 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700298 }
299 return null;
300 }
301
Amith Yamasani0b285492011-04-14 17:35:23 -0700302 /**
303 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
304 * cache it elsewhere.
305 * @return the array of user ids.
306 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700307 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700308 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700309 return mUserIds;
310 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700311 }
312
Dianne Hackborn4428e172012-08-24 17:43:05 -0700313 int[] getUserIdsLPr() {
314 return mUserIds;
315 }
316
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700317 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700318 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700319 readUserListLocked();
320 }
321 }
322
323 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700324 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700325 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700326 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700327 return;
328 }
329 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700330 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700331 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700332 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700333 XmlPullParser parser = Xml.newPullParser();
334 parser.setInput(fis, null);
335 int type;
336 while ((type = parser.next()) != XmlPullParser.START_TAG
337 && type != XmlPullParser.END_DOCUMENT) {
338 ;
339 }
340
341 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700342 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700343 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700344 return;
345 }
346
Amith Yamasani2a003292012-08-14 18:25:45 -0700347 mNextSerialNumber = -1;
348 if (parser.getName().equals(TAG_USERS)) {
349 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
350 if (lastSerialNumber != null) {
351 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
352 }
353 }
354
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700355 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
356 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
357 String id = parser.getAttributeValue(null, ATTR_ID);
358 UserInfo user = readUser(Integer.parseInt(id));
359 if (user != null) {
360 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700361 if (user.isGuest()) {
362 mGuestEnabled = true;
363 }
364 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
365 mNextSerialNumber = user.id + 1;
366 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700367 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700368 }
369 }
Amith Yamasani13593602012-03-22 16:16:17 -0700370 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700371 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700372 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700373 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700374 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800375 } finally {
376 if (fis != null) {
377 try {
378 fis.close();
379 } catch (IOException e) {
380 }
381 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700382 }
383 }
384
Amith Yamasani13593602012-03-22 16:16:17 -0700385 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700386 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700387 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700388 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
389 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700390 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700391
Amith Yamasani13593602012-03-22 16:16:17 -0700392 writeUserListLocked();
393 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700394 }
395
396 /*
397 * Writes the user file in this format:
398 *
399 * <user flags="20039023" id="0">
400 * <name>Primary</name>
401 * </user>
402 */
Amith Yamasani13593602012-03-22 16:16:17 -0700403 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700404 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700405 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700406 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700407 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700408 final BufferedOutputStream bos = new BufferedOutputStream(fos);
409
410 // XmlSerializer serializer = XmlUtils.serializerInstance();
411 final XmlSerializer serializer = new FastXmlSerializer();
412 serializer.setOutput(bos, "utf-8");
413 serializer.startDocument(null, true);
414 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
415
416 serializer.startTag(null, TAG_USER);
417 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700418 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700419 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700420 if (userInfo.iconPath != null) {
421 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
422 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700423
424 serializer.startTag(null, TAG_NAME);
425 serializer.text(userInfo.name);
426 serializer.endTag(null, TAG_NAME);
427
428 serializer.endTag(null, TAG_USER);
429
430 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700431 userFile.finishWrite(fos);
432 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700433 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700434 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700435 }
436 }
437
438 /*
439 * Writes the user list file in this format:
440 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700441 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700442 * <user id="0"></user>
443 * <user id="2"></user>
444 * </users>
445 */
Amith Yamasani13593602012-03-22 16:16:17 -0700446 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700447 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700448 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700449 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700450 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700451 final BufferedOutputStream bos = new BufferedOutputStream(fos);
452
453 // XmlSerializer serializer = XmlUtils.serializerInstance();
454 final XmlSerializer serializer = new FastXmlSerializer();
455 serializer.setOutput(bos, "utf-8");
456 serializer.startDocument(null, true);
457 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
458
459 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700460 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700461
462 for (int i = 0; i < mUsers.size(); i++) {
463 UserInfo user = mUsers.valueAt(i);
464 serializer.startTag(null, TAG_USER);
465 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
466 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700467 }
468
469 serializer.endTag(null, TAG_USERS);
470
471 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700472 userListFile.finishWrite(fos);
473 } catch (Exception e) {
474 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700475 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700476 }
477 }
478
479 private UserInfo readUser(int id) {
480 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700481 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700482 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700483 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700484
485 FileInputStream fis = null;
486 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700487 AtomicFile userFile =
488 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
489 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700490 XmlPullParser parser = Xml.newPullParser();
491 parser.setInput(fis, null);
492 int type;
493 while ((type = parser.next()) != XmlPullParser.START_TAG
494 && type != XmlPullParser.END_DOCUMENT) {
495 ;
496 }
497
498 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700499 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700500 return null;
501 }
502
503 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
504 String storedId = parser.getAttributeValue(null, ATTR_ID);
505 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700506 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700507 return null;
508 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700509 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
510 if (serialNumberValue != null) {
511 serialNumber = Integer.parseInt(serialNumberValue);
512 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700513 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
514 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700515 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700516
517 while ((type = parser.next()) != XmlPullParser.START_TAG
518 && type != XmlPullParser.END_DOCUMENT) {
519 }
520 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
521 type = parser.next();
522 if (type == XmlPullParser.TEXT) {
523 name = parser.getText();
524 }
525 }
526 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700527
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700528 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700529 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700530 return userInfo;
531
532 } catch (IOException ioe) {
533 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800534 } finally {
535 if (fis != null) {
536 try {
537 fis.close();
538 } catch (IOException e) {
539 }
540 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700541 }
542 return null;
543 }
544
Amith Yamasani258848d2012-08-10 17:06:33 -0700545 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700546 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700547 checkManageUsersPermission("Only the system can create users");
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700548
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700549 final long ident = Binder.clearCallingIdentity();
550 final UserInfo userInfo;
551 try {
552 synchronized (mInstallLock) {
553 synchronized (mPackagesLock) {
554 if (isUserLimitReachedLocked()) return null;
555 int userId = getNextAvailableIdLocked();
556 userInfo = new UserInfo(userId, name, null, flags);
557 File userPath = new File(mBaseUserPath, Integer.toString(userId));
558 userInfo.serialNumber = mNextSerialNumber++;
559 mUsers.put(userId, userInfo);
560 writeUserListLocked();
561 writeUserLocked(userInfo);
562 updateUserIdsLocked();
563 mPm.createNewUserLILPw(userId, userPath);
564 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700565 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700566 if (userInfo != null) {
567 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
568 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
569 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
570 android.Manifest.permission.MANAGE_USERS);
571 }
572 } finally {
573 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -0700574 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700575 return userInfo;
576 }
577
Amith Yamasani0b285492011-04-14 17:35:23 -0700578 /**
579 * Removes a user and all data directories created for that user. This method should be called
580 * after the user's processes have been terminated.
581 * @param id the user's id
582 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700583 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700584 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700585 final UserInfo user;
586 synchronized (mPackagesLock) {
587 user = mUsers.get(userHandle);
588 if (userHandle == 0 || user == null) {
589 return false;
590 }
591 }
592
593 int res;
594 try {
595 res = ActivityManagerNative.getDefault().stopUser(userHandle,
596 new IStopUserCallback.Stub() {
597 @Override
598 public void userStopped(int userId) {
599 finishRemoveUser(userId);
600 }
601 @Override
602 public void userStopAborted(int userId) {
603 }
604 });
605 } catch (RemoteException e) {
606 return false;
607 }
608
609 return res == ActivityManager.USER_OP_SUCCESS;
610 }
611
612 void finishRemoveUser(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700613 synchronized (mInstallLock) {
614 synchronized (mPackagesLock) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700615 // Cleanup package manager settings
616 mPm.cleanUpUserLILPw(userHandle);
617
618 // Remove this user from the list
619 mUsers.remove(userHandle);
620 // Remove user file
621 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
622 userFile.delete();
623 // Update the user list
624 writeUserListLocked();
625 updateUserIdsLocked();
Amith Yamasani61f57372012-08-31 12:12:28 -0700626 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700627 }
628 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700629
Amith Yamasani2a003292012-08-14 18:25:45 -0700630 // Let other services shutdown any activity
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700631 long ident = Binder.clearCallingIdentity();
632 try {
633 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
634 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
635 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
636 android.Manifest.permission.MANAGE_USERS);
637 } finally {
638 Binder.restoreCallingIdentity(ident);
639 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700640 }
641
Amith Yamasani61f57372012-08-31 12:12:28 -0700642 private void removeDirectoryRecursive(File parent) {
643 if (parent.isDirectory()) {
644 String[] files = parent.list();
645 for (String filename : files) {
646 File child = new File(parent, filename);
647 removeDirectoryRecursive(child);
648 }
649 }
650 parent.delete();
651 }
652
Amith Yamasani2a003292012-08-14 18:25:45 -0700653 @Override
654 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700655 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700656 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700657 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700658 }
659 }
660
661 @Override
662 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700663 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700664 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700665 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700666 }
667 // Not found
668 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700669 }
670 }
671
Amith Yamasani0b285492011-04-14 17:35:23 -0700672 /**
673 * Caches the list of user ids in an array, adjusting the array size when necessary.
674 */
Amith Yamasani13593602012-03-22 16:16:17 -0700675 private void updateUserIdsLocked() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700676 int[] newUsers = new int[mUsers.size()];
Amith Yamasani0b285492011-04-14 17:35:23 -0700677 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700678 newUsers[i] = mUsers.keyAt(i);
Amith Yamasani0b285492011-04-14 17:35:23 -0700679 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700680 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700681 }
682
683 /**
684 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700685 * TODO: May not be a good idea to recycle ids, in case it results in confusion
686 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700687 * @return
688 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700689 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700690 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700691 int i = 0;
692 while (i < Integer.MAX_VALUE) {
693 if (mUsers.indexOfKey(i) < 0) {
694 break;
695 }
696 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700697 }
Amith Yamasani195263742012-08-21 15:40:12 -0700698 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700699 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700700 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700701}