blob: 914ff676a2b5749594833992f577b4bb59e86079 [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;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700199 ParcelFileDescriptor fd = openIconBitmapLocked(info, true /* write */);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700200 if (fd != null) {
201 writeUserLocked(info);
202 }
203 return fd;
204 }
205 }
206
Amith Yamasani258848d2012-08-10 17:06:33 -0700207 @Override
Amith Yamasani3b49f072012-09-17 10:21:43 -0700208 public ParcelFileDescriptor getUserIcon(int userId) {
209 checkManageUsersPermission("read users");
210 synchronized (mPackagesLock) {
211 UserInfo info = mUsers.get(userId);
212 if (info == null || info.iconPath == null) return null;
213 ParcelFileDescriptor fd = openIconBitmapLocked(info, false /* read */);
214 return fd;
215 }
216 }
217
218 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700219 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700220 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700221 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700222 if (mGuestEnabled != enable) {
223 mGuestEnabled = enable;
224 // Erase any guest user that currently exists
225 for (int i = 0; i < mUsers.size(); i++) {
226 UserInfo user = mUsers.valueAt(i);
227 if (user.isGuest()) {
228 if (!enable) {
229 removeUser(user.id);
230 }
231 return;
232 }
233 }
234 // No guest was found
235 if (enable) {
236 createUser("Guest", UserInfo.FLAG_GUEST);
237 }
238 }
239 }
240 }
241
242 @Override
243 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700244 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700245 return mGuestEnabled;
246 }
247 }
248
249 @Override
250 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700251 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700252 // TODO:
253 }
254
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700255 public void makeInitialized(int userId) {
256 checkManageUsersPermission("makeInitialized");
257 synchronized (mPackagesLock) {
258 UserInfo info = mUsers.get(userId);
259 if (info != null && (info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
260 info.flags |= UserInfo.FLAG_INITIALIZED;
261 writeUserLocked(info);
262 }
263 }
264 }
265
Amith Yamasani258848d2012-08-10 17:06:33 -0700266 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700267 * Check if we've hit the limit of how many users can be created.
268 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700269 private boolean isUserLimitReachedLocked() {
270 int nUsers = mUsers.size();
271 return nUsers >= mUserLimit;
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700272 }
273
274 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700275 * Enforces that only the system UID or root's UID or apps that have the
276 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
277 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700278 *
279 * @param message used as message if SecurityException is thrown
280 * @throws SecurityException if the caller is not system or root
281 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700282 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700283 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700284 if (uid != Process.SYSTEM_UID && uid != 0
285 && ActivityManager.checkComponentPermission(
286 android.Manifest.permission.MANAGE_USERS,
287 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
288 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700289 }
290 }
291
Amith Yamasani3b49f072012-09-17 10:21:43 -0700292 private ParcelFileDescriptor openIconBitmapLocked(UserInfo info, boolean toWrite) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700293 try {
294 File dir = new File(mUsersDir, Integer.toString(info.id));
295 File file = new File(dir, USER_PHOTO_FILENAME);
296 if (!dir.exists()) {
297 dir.mkdir();
298 FileUtils.setPermissions(
299 dir.getPath(),
300 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
301 -1, -1);
302 }
303 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
Amith Yamasani3b49f072012-09-17 10:21:43 -0700304 toWrite ? MODE_CREATE|MODE_READ_WRITE : MODE_READ_WRITE);
305 if (toWrite) {
306 info.iconPath = file.getAbsolutePath();
307 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700308 return fd;
309 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700310 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700311 }
312 return null;
313 }
314
Amith Yamasani0b285492011-04-14 17:35:23 -0700315 /**
316 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
317 * cache it elsewhere.
318 * @return the array of user ids.
319 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700320 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700321 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700322 return mUserIds;
323 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700324 }
325
Dianne Hackborn4428e172012-08-24 17:43:05 -0700326 int[] getUserIdsLPr() {
327 return mUserIds;
328 }
329
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700330 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700331 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700332 readUserListLocked();
333 }
334 }
335
336 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700337 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700338 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700339 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700340 return;
341 }
342 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700343 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700344 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700345 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700346 XmlPullParser parser = Xml.newPullParser();
347 parser.setInput(fis, null);
348 int type;
349 while ((type = parser.next()) != XmlPullParser.START_TAG
350 && type != XmlPullParser.END_DOCUMENT) {
351 ;
352 }
353
354 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700355 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700356 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700357 return;
358 }
359
Amith Yamasani2a003292012-08-14 18:25:45 -0700360 mNextSerialNumber = -1;
361 if (parser.getName().equals(TAG_USERS)) {
362 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
363 if (lastSerialNumber != null) {
364 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
365 }
366 }
367
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700368 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
369 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
370 String id = parser.getAttributeValue(null, ATTR_ID);
371 UserInfo user = readUser(Integer.parseInt(id));
372 if (user != null) {
373 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700374 if (user.isGuest()) {
375 mGuestEnabled = true;
376 }
377 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
378 mNextSerialNumber = user.id + 1;
379 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700380 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700381 }
382 }
Amith Yamasani13593602012-03-22 16:16:17 -0700383 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700384 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700385 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700386 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700387 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800388 } finally {
389 if (fis != null) {
390 try {
391 fis.close();
392 } catch (IOException e) {
393 }
394 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700395 }
396 }
397
Amith Yamasani13593602012-03-22 16:16:17 -0700398 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700399 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700400 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700401 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
402 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700403 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700404
Amith Yamasani13593602012-03-22 16:16:17 -0700405 writeUserListLocked();
406 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700407 }
408
409 /*
410 * Writes the user file in this format:
411 *
412 * <user flags="20039023" id="0">
413 * <name>Primary</name>
414 * </user>
415 */
Amith Yamasani13593602012-03-22 16:16:17 -0700416 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700417 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700418 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700419 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700420 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700421 final BufferedOutputStream bos = new BufferedOutputStream(fos);
422
423 // XmlSerializer serializer = XmlUtils.serializerInstance();
424 final XmlSerializer serializer = new FastXmlSerializer();
425 serializer.setOutput(bos, "utf-8");
426 serializer.startDocument(null, true);
427 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
428
429 serializer.startTag(null, TAG_USER);
430 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700431 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700432 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700433 if (userInfo.iconPath != null) {
434 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
435 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700436
437 serializer.startTag(null, TAG_NAME);
438 serializer.text(userInfo.name);
439 serializer.endTag(null, TAG_NAME);
440
441 serializer.endTag(null, TAG_USER);
442
443 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700444 userFile.finishWrite(fos);
445 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700446 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700447 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700448 }
449 }
450
451 /*
452 * Writes the user list file in this format:
453 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700454 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700455 * <user id="0"></user>
456 * <user id="2"></user>
457 * </users>
458 */
Amith Yamasani13593602012-03-22 16:16:17 -0700459 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700460 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700461 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700462 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700463 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700464 final BufferedOutputStream bos = new BufferedOutputStream(fos);
465
466 // XmlSerializer serializer = XmlUtils.serializerInstance();
467 final XmlSerializer serializer = new FastXmlSerializer();
468 serializer.setOutput(bos, "utf-8");
469 serializer.startDocument(null, true);
470 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
471
472 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700473 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700474
475 for (int i = 0; i < mUsers.size(); i++) {
476 UserInfo user = mUsers.valueAt(i);
477 serializer.startTag(null, TAG_USER);
478 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
479 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700480 }
481
482 serializer.endTag(null, TAG_USERS);
483
484 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700485 userListFile.finishWrite(fos);
486 } catch (Exception e) {
487 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700488 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700489 }
490 }
491
492 private UserInfo readUser(int id) {
493 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700494 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700495 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700496 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700497
498 FileInputStream fis = null;
499 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700500 AtomicFile userFile =
501 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
502 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700503 XmlPullParser parser = Xml.newPullParser();
504 parser.setInput(fis, null);
505 int type;
506 while ((type = parser.next()) != XmlPullParser.START_TAG
507 && type != XmlPullParser.END_DOCUMENT) {
508 ;
509 }
510
511 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700512 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700513 return null;
514 }
515
516 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
517 String storedId = parser.getAttributeValue(null, ATTR_ID);
518 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700519 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700520 return null;
521 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700522 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
523 if (serialNumberValue != null) {
524 serialNumber = Integer.parseInt(serialNumberValue);
525 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700526 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
527 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700528 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700529
530 while ((type = parser.next()) != XmlPullParser.START_TAG
531 && type != XmlPullParser.END_DOCUMENT) {
532 }
533 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
534 type = parser.next();
535 if (type == XmlPullParser.TEXT) {
536 name = parser.getText();
537 }
538 }
539 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700540
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700541 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700542 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700543 return userInfo;
544
545 } catch (IOException ioe) {
546 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800547 } finally {
548 if (fis != null) {
549 try {
550 fis.close();
551 } catch (IOException e) {
552 }
553 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700554 }
555 return null;
556 }
557
Amith Yamasani258848d2012-08-10 17:06:33 -0700558 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700559 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700560 checkManageUsersPermission("Only the system can create users");
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700561
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700562 final long ident = Binder.clearCallingIdentity();
563 final UserInfo userInfo;
564 try {
565 synchronized (mInstallLock) {
566 synchronized (mPackagesLock) {
567 if (isUserLimitReachedLocked()) return null;
568 int userId = getNextAvailableIdLocked();
569 userInfo = new UserInfo(userId, name, null, flags);
570 File userPath = new File(mBaseUserPath, Integer.toString(userId));
571 userInfo.serialNumber = mNextSerialNumber++;
572 mUsers.put(userId, userInfo);
573 writeUserListLocked();
574 writeUserLocked(userInfo);
575 updateUserIdsLocked();
576 mPm.createNewUserLILPw(userId, userPath);
577 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700578 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700579 if (userInfo != null) {
580 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
581 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
582 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
583 android.Manifest.permission.MANAGE_USERS);
584 }
585 } finally {
586 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -0700587 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700588 return userInfo;
589 }
590
Amith Yamasani0b285492011-04-14 17:35:23 -0700591 /**
592 * Removes a user and all data directories created for that user. This method should be called
593 * after the user's processes have been terminated.
594 * @param id the user's id
595 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700596 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700597 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700598 final UserInfo user;
599 synchronized (mPackagesLock) {
600 user = mUsers.get(userHandle);
601 if (userHandle == 0 || user == null) {
602 return false;
603 }
604 }
605
606 int res;
607 try {
608 res = ActivityManagerNative.getDefault().stopUser(userHandle,
609 new IStopUserCallback.Stub() {
610 @Override
611 public void userStopped(int userId) {
612 finishRemoveUser(userId);
613 }
614 @Override
615 public void userStopAborted(int userId) {
616 }
617 });
618 } catch (RemoteException e) {
619 return false;
620 }
621
622 return res == ActivityManager.USER_OP_SUCCESS;
623 }
624
625 void finishRemoveUser(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700626 synchronized (mInstallLock) {
627 synchronized (mPackagesLock) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700628 // Cleanup package manager settings
629 mPm.cleanUpUserLILPw(userHandle);
630
631 // Remove this user from the list
632 mUsers.remove(userHandle);
633 // Remove user file
634 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
635 userFile.delete();
636 // Update the user list
637 writeUserListLocked();
638 updateUserIdsLocked();
Amith Yamasani61f57372012-08-31 12:12:28 -0700639 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700640 }
641 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700642
Amith Yamasani2a003292012-08-14 18:25:45 -0700643 // Let other services shutdown any activity
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700644 long ident = Binder.clearCallingIdentity();
645 try {
646 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
647 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
648 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
649 android.Manifest.permission.MANAGE_USERS);
650 } finally {
651 Binder.restoreCallingIdentity(ident);
652 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700653 }
654
Amith Yamasani61f57372012-08-31 12:12:28 -0700655 private void removeDirectoryRecursive(File parent) {
656 if (parent.isDirectory()) {
657 String[] files = parent.list();
658 for (String filename : files) {
659 File child = new File(parent, filename);
660 removeDirectoryRecursive(child);
661 }
662 }
663 parent.delete();
664 }
665
Amith Yamasani2a003292012-08-14 18:25:45 -0700666 @Override
667 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700668 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700669 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700670 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700671 }
672 }
673
674 @Override
675 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700676 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700677 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700678 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700679 }
680 // Not found
681 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700682 }
683 }
684
Amith Yamasani0b285492011-04-14 17:35:23 -0700685 /**
686 * Caches the list of user ids in an array, adjusting the array size when necessary.
687 */
Amith Yamasani13593602012-03-22 16:16:17 -0700688 private void updateUserIdsLocked() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700689 int[] newUsers = new int[mUsers.size()];
Amith Yamasani0b285492011-04-14 17:35:23 -0700690 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700691 newUsers[i] = mUsers.keyAt(i);
Amith Yamasani0b285492011-04-14 17:35:23 -0700692 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700693 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700694 }
695
696 /**
697 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700698 * TODO: May not be a good idea to recycle ids, in case it results in confusion
699 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700700 * @return
701 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700702 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700703 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700704 int i = 0;
705 while (i < Integer.MAX_VALUE) {
706 if (mUsers.indexOfKey(i) < 0) {
707 break;
708 }
709 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700710 }
Amith Yamasani195263742012-08-21 15:40:12 -0700711 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700712 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700713 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700714}