blob: 3391668acc2646d6b45a599c3a6a6181e12db14f [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
Amith Yamasanib8151ec2012-04-18 18:02:48 -070019import static android.os.ParcelFileDescriptor.MODE_CREATE;
20import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
21
Amith Yamasani13593602012-03-22 16:16:17 -070022import com.android.internal.util.ArrayUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070023import com.android.internal.util.FastXmlSerializer;
24
Amith Yamasani2a003292012-08-14 18:25:45 -070025import android.app.ActivityManager;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070026import android.app.ActivityManagerNative;
27import android.app.IStopUserCallback;
Amith Yamasani258848d2012-08-10 17:06:33 -070028import android.content.Context;
29import android.content.Intent;
Amith Yamasani0b285492011-04-14 17:35:23 -070030import android.content.pm.PackageManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070031import android.content.pm.UserInfo;
Amith Yamasani258848d2012-08-10 17:06:33 -070032import android.os.Binder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070033import android.os.Environment;
34import android.os.FileUtils;
Amith Yamasani258848d2012-08-10 17:06:33 -070035import android.os.IUserManager;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070036import android.os.ParcelFileDescriptor;
Amith Yamasani258848d2012-08-10 17:06:33 -070037import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070038import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070039import android.os.UserHandle;
Amith Yamasani2a003292012-08-14 18:25:45 -070040import android.util.AtomicFile;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070041import android.util.Slog;
42import android.util.SparseArray;
43import android.util.Xml;
44
45import java.io.BufferedOutputStream;
46import java.io.File;
47import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070048import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070049import java.io.FileOutputStream;
50import java.io.IOException;
51import java.util.ArrayList;
52import java.util.List;
53
54import org.xmlpull.v1.XmlPullParser;
55import org.xmlpull.v1.XmlPullParserException;
56import org.xmlpull.v1.XmlSerializer;
57
Amith Yamasani258848d2012-08-10 17:06:33 -070058public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070059
Amith Yamasani2a003292012-08-14 18:25:45 -070060 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070061
Amith Yamasani4b2e9342011-03-31 12:38:53 -070062 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070063 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070064 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070065 private static final String ATTR_ID = "id";
Amith Yamasani2a003292012-08-14 18:25:45 -070066 private static final String ATTR_SERIAL_NO = "serialNumber";
67 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070068 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070069 private static final String TAG_USER = "user";
70
Amith Yamasani0b285492011-04-14 17:35:23 -070071 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070072 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070073 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070074
Dianne Hackborn4428e172012-08-24 17:43:05 -070075 private final Context mContext;
76 private final PackageManagerService mPm;
77 private final Object mInstallLock;
78 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070079
80 private final File mUsersDir;
81 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -070082 private final File mBaseUserPath;
83
84 private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
85
Amith Yamasani0b285492011-04-14 17:35:23 -070086 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -070087 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -070088 private int mNextSerialNumber;
Amith Yamasani0b285492011-04-14 17:35:23 -070089
Amith Yamasani258848d2012-08-10 17:06:33 -070090 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070091
Dianne Hackborn4428e172012-08-24 17:43:05 -070092 public static UserManagerService getInstance() {
93 synchronized (UserManagerService.class) {
94 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070095 }
Amith Yamasani258848d2012-08-10 17:06:33 -070096 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070097
98 /**
99 * Available for testing purposes.
100 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700101 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700102 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700103 }
104
Dianne Hackborn4428e172012-08-24 17:43:05 -0700105 /**
106 * Called by package manager to create the service. This is closely
107 * associated with the package manager, and the given lock is the
108 * package manager's own lock.
109 */
110 UserManagerService(Context context, PackageManagerService pm,
111 Object installLock, Object packagesLock) {
112 this(context, pm, installLock, packagesLock,
113 Environment.getDataDirectory(),
114 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700115 }
116
Dianne Hackborn4428e172012-08-24 17:43:05 -0700117 /**
118 * Available for testing purposes.
119 */
120 private UserManagerService(Context context, PackageManagerService pm,
121 Object installLock, Object packagesLock,
122 File dataDir, File baseUserPath) {
123 synchronized (UserManagerService.class) {
124 mContext = context;
125 mPm = pm;
126 mInstallLock = installLock;
127 mPackagesLock = packagesLock;
128 mUsersDir = new File(dataDir, USER_INFO_DIR);
129 mUsersDir.mkdirs();
130 // Make zeroth user directory, for services to migrate their files to that location
131 File userZeroDir = new File(mUsersDir, "0");
132 userZeroDir.mkdirs();
133 mBaseUserPath = baseUserPath;
134 FileUtils.setPermissions(mUsersDir.toString(),
135 FileUtils.S_IRWXU|FileUtils.S_IRWXG
136 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
137 -1, -1);
138 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
139 readUserList();
140 sInstance = this;
141 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700142 }
143
144 @Override
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700145 public List<UserInfo> getUsers() {
Amith Yamasani2a003292012-08-14 18:25:45 -0700146 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700147 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700148 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
149 for (int i = 0; i < mUsers.size(); i++) {
150 users.add(mUsers.valueAt(i));
151 }
152 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700153 }
Amith Yamasani13593602012-03-22 16:16:17 -0700154 }
155
Amith Yamasani258848d2012-08-10 17:06:33 -0700156 @Override
157 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700158 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700159 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700160 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700161 }
162 }
163
Amith Yamasani195263742012-08-21 15:40:12 -0700164 /*
165 * Should be locked on mUsers before calling this.
166 */
167 private UserInfo getUserInfoLocked(int userId) {
168 return mUsers.get(userId);
169 }
170
Amith Yamasani13593602012-03-22 16:16:17 -0700171 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700172 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700173 return ArrayUtils.contains(mUserIds, userId);
174 }
175 }
176
Amith Yamasani258848d2012-08-10 17:06:33 -0700177 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700178 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700179 checkManageUsersPermission("rename users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700180 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700181 UserInfo info = mUsers.get(userId);
182 if (name != null && !name.equals(info.name)) {
183 info.name = name;
184 writeUserLocked(info);
185 }
186 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700187 }
188
Amith Yamasani258848d2012-08-10 17:06:33 -0700189 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700190 public ParcelFileDescriptor setUserIcon(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700191 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700192 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700193 UserInfo info = mUsers.get(userId);
194 if (info == null) return null;
195 ParcelFileDescriptor fd = updateIconBitmapLocked(info);
196 if (fd != null) {
197 writeUserLocked(info);
198 }
199 return fd;
200 }
201 }
202
Amith Yamasani258848d2012-08-10 17:06:33 -0700203 @Override
204 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700205 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700206 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700207 if (mGuestEnabled != enable) {
208 mGuestEnabled = enable;
209 // Erase any guest user that currently exists
210 for (int i = 0; i < mUsers.size(); i++) {
211 UserInfo user = mUsers.valueAt(i);
212 if (user.isGuest()) {
213 if (!enable) {
214 removeUser(user.id);
215 }
216 return;
217 }
218 }
219 // No guest was found
220 if (enable) {
221 createUser("Guest", UserInfo.FLAG_GUEST);
222 }
223 }
224 }
225 }
226
227 @Override
228 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700229 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700230 return mGuestEnabled;
231 }
232 }
233
234 @Override
235 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700236 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700237 // TODO:
238 }
239
240 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700241 * Enforces that only the system UID or root's UID or apps that have the
242 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
243 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700244 *
245 * @param message used as message if SecurityException is thrown
246 * @throws SecurityException if the caller is not system or root
247 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700248 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700249 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700250 if (uid != Process.SYSTEM_UID && uid != 0
251 && ActivityManager.checkComponentPermission(
252 android.Manifest.permission.MANAGE_USERS,
253 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
254 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700255 }
256 }
257
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700258 private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
259 try {
260 File dir = new File(mUsersDir, Integer.toString(info.id));
261 File file = new File(dir, USER_PHOTO_FILENAME);
262 if (!dir.exists()) {
263 dir.mkdir();
264 FileUtils.setPermissions(
265 dir.getPath(),
266 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
267 -1, -1);
268 }
269 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
270 MODE_CREATE|MODE_READ_WRITE);
271 info.iconPath = file.getAbsolutePath();
272 return fd;
273 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700274 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700275 }
276 return null;
277 }
278
Amith Yamasani0b285492011-04-14 17:35:23 -0700279 /**
280 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
281 * cache it elsewhere.
282 * @return the array of user ids.
283 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700284 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700285 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700286 return mUserIds;
287 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700288 }
289
Dianne Hackborn4428e172012-08-24 17:43:05 -0700290 int[] getUserIdsLPr() {
291 return mUserIds;
292 }
293
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700294 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700295 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700296 readUserListLocked();
297 }
298 }
299
300 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700301 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700302 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700303 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700304 return;
305 }
306 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700307 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700308 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700309 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700310 XmlPullParser parser = Xml.newPullParser();
311 parser.setInput(fis, null);
312 int type;
313 while ((type = parser.next()) != XmlPullParser.START_TAG
314 && type != XmlPullParser.END_DOCUMENT) {
315 ;
316 }
317
318 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700319 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700320 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700321 return;
322 }
323
Amith Yamasani2a003292012-08-14 18:25:45 -0700324 mNextSerialNumber = -1;
325 if (parser.getName().equals(TAG_USERS)) {
326 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
327 if (lastSerialNumber != null) {
328 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
329 }
330 }
331
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700332 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
333 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
334 String id = parser.getAttributeValue(null, ATTR_ID);
335 UserInfo user = readUser(Integer.parseInt(id));
336 if (user != null) {
337 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700338 if (user.isGuest()) {
339 mGuestEnabled = true;
340 }
341 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
342 mNextSerialNumber = user.id + 1;
343 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700344 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700345 }
346 }
Amith Yamasani13593602012-03-22 16:16:17 -0700347 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700348 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700349 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700350 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700351 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800352 } finally {
353 if (fis != null) {
354 try {
355 fis.close();
356 } catch (IOException e) {
357 }
358 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700359 }
360 }
361
Amith Yamasani13593602012-03-22 16:16:17 -0700362 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700363 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700364 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700365 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
366 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700367 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700368
Amith Yamasani13593602012-03-22 16:16:17 -0700369 writeUserListLocked();
370 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700371 }
372
373 /*
374 * Writes the user file in this format:
375 *
376 * <user flags="20039023" id="0">
377 * <name>Primary</name>
378 * </user>
379 */
Amith Yamasani13593602012-03-22 16:16:17 -0700380 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700381 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700382 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700383 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700384 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700385 final BufferedOutputStream bos = new BufferedOutputStream(fos);
386
387 // XmlSerializer serializer = XmlUtils.serializerInstance();
388 final XmlSerializer serializer = new FastXmlSerializer();
389 serializer.setOutput(bos, "utf-8");
390 serializer.startDocument(null, true);
391 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
392
393 serializer.startTag(null, TAG_USER);
394 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700395 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700396 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700397 if (userInfo.iconPath != null) {
398 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
399 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700400
401 serializer.startTag(null, TAG_NAME);
402 serializer.text(userInfo.name);
403 serializer.endTag(null, TAG_NAME);
404
405 serializer.endTag(null, TAG_USER);
406
407 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700408 userFile.finishWrite(fos);
409 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700410 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700411 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700412 }
413 }
414
415 /*
416 * Writes the user list file in this format:
417 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700418 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700419 * <user id="0"></user>
420 * <user id="2"></user>
421 * </users>
422 */
Amith Yamasani13593602012-03-22 16:16:17 -0700423 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700424 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700425 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700426 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700427 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700428 final BufferedOutputStream bos = new BufferedOutputStream(fos);
429
430 // XmlSerializer serializer = XmlUtils.serializerInstance();
431 final XmlSerializer serializer = new FastXmlSerializer();
432 serializer.setOutput(bos, "utf-8");
433 serializer.startDocument(null, true);
434 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
435
436 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700437 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700438
439 for (int i = 0; i < mUsers.size(); i++) {
440 UserInfo user = mUsers.valueAt(i);
441 serializer.startTag(null, TAG_USER);
442 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
443 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700444 }
445
446 serializer.endTag(null, TAG_USERS);
447
448 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700449 userListFile.finishWrite(fos);
450 } catch (Exception e) {
451 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700452 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700453 }
454 }
455
456 private UserInfo readUser(int id) {
457 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700458 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700459 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700460 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700461
462 FileInputStream fis = null;
463 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700464 AtomicFile userFile =
465 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
466 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700467 XmlPullParser parser = Xml.newPullParser();
468 parser.setInput(fis, null);
469 int type;
470 while ((type = parser.next()) != XmlPullParser.START_TAG
471 && type != XmlPullParser.END_DOCUMENT) {
472 ;
473 }
474
475 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700476 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700477 return null;
478 }
479
480 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
481 String storedId = parser.getAttributeValue(null, ATTR_ID);
482 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700483 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700484 return null;
485 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700486 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
487 if (serialNumberValue != null) {
488 serialNumber = Integer.parseInt(serialNumberValue);
489 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700490 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
491 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700492 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700493
494 while ((type = parser.next()) != XmlPullParser.START_TAG
495 && type != XmlPullParser.END_DOCUMENT) {
496 }
497 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
498 type = parser.next();
499 if (type == XmlPullParser.TEXT) {
500 name = parser.getText();
501 }
502 }
503 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700504
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700505 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700506 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700507 return userInfo;
508
509 } catch (IOException ioe) {
510 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800511 } finally {
512 if (fis != null) {
513 try {
514 fis.close();
515 } catch (IOException e) {
516 }
517 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700518 }
519 return null;
520 }
521
Amith Yamasani258848d2012-08-10 17:06:33 -0700522 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700523 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700524 checkManageUsersPermission("Only the system can create users");
Amith Yamasani0b285492011-04-14 17:35:23 -0700525 int userId = getNextAvailableId();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700526 UserInfo userInfo = new UserInfo(userId, name, null, flags);
Amith Yamasani0b285492011-04-14 17:35:23 -0700527 File userPath = new File(mBaseUserPath, Integer.toString(userId));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700528 synchronized (mInstallLock) {
529 synchronized (mPackagesLock) {
530 userInfo.serialNumber = mNextSerialNumber++;
531 mUsers.put(userId, userInfo);
532 writeUserListLocked();
533 writeUserLocked(userInfo);
534 updateUserIdsLocked();
535 mPm.createNewUserLILPw(userId, userPath);
536 }
Amith Yamasani13593602012-03-22 16:16:17 -0700537 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700538 if (userInfo != null) {
539 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
Amith Yamasani2a003292012-08-14 18:25:45 -0700540 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
Amith Yamasanifc6e0ca2012-08-17 17:07:14 -0700541 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_BOOT_COMPLETED),
542 new UserHandle(userInfo.id));
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700543 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
544 android.Manifest.permission.MANAGE_USERS);
Amith Yamasani258848d2012-08-10 17:06:33 -0700545 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700546 return userInfo;
547 }
548
Amith Yamasani0b285492011-04-14 17:35:23 -0700549 /**
550 * Removes a user and all data directories created for that user. This method should be called
551 * after the user's processes have been terminated.
552 * @param id the user's id
553 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700554 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700555 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700556 final UserInfo user;
557 synchronized (mPackagesLock) {
558 user = mUsers.get(userHandle);
559 if (userHandle == 0 || user == null) {
560 return false;
561 }
562 }
563
564 int res;
565 try {
566 res = ActivityManagerNative.getDefault().stopUser(userHandle,
567 new IStopUserCallback.Stub() {
568 @Override
569 public void userStopped(int userId) {
570 finishRemoveUser(userId);
571 }
572 @Override
573 public void userStopAborted(int userId) {
574 }
575 });
576 } catch (RemoteException e) {
577 return false;
578 }
579
580 return res == ActivityManager.USER_OP_SUCCESS;
581 }
582
583 void finishRemoveUser(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700584 synchronized (mInstallLock) {
585 synchronized (mPackagesLock) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700586 // Cleanup package manager settings
587 mPm.cleanUpUserLILPw(userHandle);
588
589 // Remove this user from the list
590 mUsers.remove(userHandle);
591 // Remove user file
592 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
593 userFile.delete();
594 // Update the user list
595 writeUserListLocked();
596 updateUserIdsLocked();
Amith Yamasani61f57372012-08-31 12:12:28 -0700597 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700598 }
599 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700600
Amith Yamasani2a003292012-08-14 18:25:45 -0700601 // Let other services shutdown any activity
602 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
603 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
604 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700605 }
606
Amith Yamasani61f57372012-08-31 12:12:28 -0700607 private void removeDirectoryRecursive(File parent) {
608 if (parent.isDirectory()) {
609 String[] files = parent.list();
610 for (String filename : files) {
611 File child = new File(parent, filename);
612 removeDirectoryRecursive(child);
613 }
614 }
615 parent.delete();
616 }
617
Amith Yamasani2a003292012-08-14 18:25:45 -0700618 @Override
619 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700620 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700621 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700622 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700623 }
624 }
625
626 @Override
627 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700628 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700629 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700630 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700631 }
632 // Not found
633 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700634 }
635 }
636
Amith Yamasani0b285492011-04-14 17:35:23 -0700637 /**
638 * Caches the list of user ids in an array, adjusting the array size when necessary.
639 */
Amith Yamasani13593602012-03-22 16:16:17 -0700640 private void updateUserIdsLocked() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700641 int[] newUsers = new int[mUsers.size()];
Amith Yamasani0b285492011-04-14 17:35:23 -0700642 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700643 newUsers[i] = mUsers.keyAt(i);
Amith Yamasani0b285492011-04-14 17:35:23 -0700644 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700645 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700646 }
647
648 /**
649 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700650 * TODO: May not be a good idea to recycle ids, in case it results in confusion
651 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700652 * @return
653 */
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700654 private int getNextAvailableId() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700655 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700656 int i = 0;
657 while (i < Integer.MAX_VALUE) {
658 if (mUsers.indexOfKey(i) < 0) {
659 break;
660 }
661 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700662 }
Amith Yamasani195263742012-08-21 15:40:12 -0700663 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700664 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700665 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700666}