blob: 750aa720e953b556edad0ec93aa884d271171421 [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
Amith Yamasanib8151ec2012-04-18 18:02:48 -070019import static android.os.ParcelFileDescriptor.MODE_CREATE;
20import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
21
Amith Yamasani13593602012-03-22 16:16:17 -070022import com.android.internal.util.ArrayUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070023import com.android.internal.util.FastXmlSerializer;
24
Amith Yamasani2a003292012-08-14 18:25:45 -070025import android.app.ActivityManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070026import android.content.Context;
27import android.content.Intent;
Amith Yamasani0b285492011-04-14 17:35:23 -070028import android.content.pm.PackageManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070029import android.content.pm.UserInfo;
Amith Yamasani258848d2012-08-10 17:06:33 -070030import android.os.Binder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070031import android.os.Environment;
32import android.os.FileUtils;
Amith Yamasani258848d2012-08-10 17:06:33 -070033import android.os.IUserManager;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070034import android.os.ParcelFileDescriptor;
Amith Yamasani258848d2012-08-10 17:06:33 -070035import android.os.Process;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070036import android.os.UserHandle;
Amith Yamasani2a003292012-08-14 18:25:45 -070037import android.util.AtomicFile;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070038import android.util.Slog;
39import android.util.SparseArray;
40import android.util.Xml;
41
42import java.io.BufferedOutputStream;
43import java.io.File;
44import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070045import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070046import java.io.FileOutputStream;
47import java.io.IOException;
48import java.util.ArrayList;
49import java.util.List;
50
51import org.xmlpull.v1.XmlPullParser;
52import org.xmlpull.v1.XmlPullParserException;
53import org.xmlpull.v1.XmlSerializer;
54
Amith Yamasani258848d2012-08-10 17:06:33 -070055public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070056
Amith Yamasani2a003292012-08-14 18:25:45 -070057 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070058
Amith Yamasani4b2e9342011-03-31 12:38:53 -070059 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070060 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070061 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070062 private static final String ATTR_ID = "id";
Amith Yamasani2a003292012-08-14 18:25:45 -070063 private static final String ATTR_SERIAL_NO = "serialNumber";
64 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070065 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070066 private static final String TAG_USER = "user";
67
Amith Yamasani0b285492011-04-14 17:35:23 -070068 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070069 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070070 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070071
Dianne Hackborn4428e172012-08-24 17:43:05 -070072 private final Context mContext;
73 private final PackageManagerService mPm;
74 private final Object mInstallLock;
75 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070076
77 private final File mUsersDir;
78 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -070079 private final File mBaseUserPath;
80
81 private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
82
Amith Yamasani0b285492011-04-14 17:35:23 -070083 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -070084 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -070085 private int mNextSerialNumber;
Amith Yamasani0b285492011-04-14 17:35:23 -070086
Amith Yamasani258848d2012-08-10 17:06:33 -070087 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070088
Dianne Hackborn4428e172012-08-24 17:43:05 -070089 public static UserManagerService getInstance() {
90 synchronized (UserManagerService.class) {
91 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070092 }
Amith Yamasani258848d2012-08-10 17:06:33 -070093 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070094
95 /**
96 * Available for testing purposes.
97 */
Amith Yamasani258848d2012-08-10 17:06:33 -070098 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -070099 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700100 }
101
Dianne Hackborn4428e172012-08-24 17:43:05 -0700102 /**
103 * Called by package manager to create the service. This is closely
104 * associated with the package manager, and the given lock is the
105 * package manager's own lock.
106 */
107 UserManagerService(Context context, PackageManagerService pm,
108 Object installLock, Object packagesLock) {
109 this(context, pm, installLock, packagesLock,
110 Environment.getDataDirectory(),
111 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700112 }
113
Dianne Hackborn4428e172012-08-24 17:43:05 -0700114 /**
115 * Available for testing purposes.
116 */
117 private UserManagerService(Context context, PackageManagerService pm,
118 Object installLock, Object packagesLock,
119 File dataDir, File baseUserPath) {
120 synchronized (UserManagerService.class) {
121 mContext = context;
122 mPm = pm;
123 mInstallLock = installLock;
124 mPackagesLock = packagesLock;
125 mUsersDir = new File(dataDir, USER_INFO_DIR);
126 mUsersDir.mkdirs();
127 // Make zeroth user directory, for services to migrate their files to that location
128 File userZeroDir = new File(mUsersDir, "0");
129 userZeroDir.mkdirs();
130 mBaseUserPath = baseUserPath;
131 FileUtils.setPermissions(mUsersDir.toString(),
132 FileUtils.S_IRWXU|FileUtils.S_IRWXG
133 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
134 -1, -1);
135 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
136 readUserList();
137 sInstance = this;
138 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700139 }
140
141 @Override
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700142 public List<UserInfo> getUsers() {
Amith Yamasani2a003292012-08-14 18:25:45 -0700143 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700144 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700145 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
146 for (int i = 0; i < mUsers.size(); i++) {
147 users.add(mUsers.valueAt(i));
148 }
149 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700150 }
Amith Yamasani13593602012-03-22 16:16:17 -0700151 }
152
Amith Yamasani258848d2012-08-10 17:06:33 -0700153 @Override
154 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700155 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700156 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700157 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700158 }
159 }
160
Amith Yamasani195263742012-08-21 15:40:12 -0700161 /*
162 * Should be locked on mUsers before calling this.
163 */
164 private UserInfo getUserInfoLocked(int userId) {
165 return mUsers.get(userId);
166 }
167
Amith Yamasani13593602012-03-22 16:16:17 -0700168 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700169 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700170 return ArrayUtils.contains(mUserIds, userId);
171 }
172 }
173
Amith Yamasani258848d2012-08-10 17:06:33 -0700174 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700175 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700176 checkManageUsersPermission("rename users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700177 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700178 UserInfo info = mUsers.get(userId);
179 if (name != null && !name.equals(info.name)) {
180 info.name = name;
181 writeUserLocked(info);
182 }
183 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700184 }
185
Amith Yamasani258848d2012-08-10 17:06:33 -0700186 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700187 public ParcelFileDescriptor setUserIcon(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700188 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700189 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700190 UserInfo info = mUsers.get(userId);
191 if (info == null) return null;
192 ParcelFileDescriptor fd = updateIconBitmapLocked(info);
193 if (fd != null) {
194 writeUserLocked(info);
195 }
196 return fd;
197 }
198 }
199
Amith Yamasani258848d2012-08-10 17:06:33 -0700200 @Override
201 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700202 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700203 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700204 if (mGuestEnabled != enable) {
205 mGuestEnabled = enable;
206 // Erase any guest user that currently exists
207 for (int i = 0; i < mUsers.size(); i++) {
208 UserInfo user = mUsers.valueAt(i);
209 if (user.isGuest()) {
210 if (!enable) {
211 removeUser(user.id);
212 }
213 return;
214 }
215 }
216 // No guest was found
217 if (enable) {
218 createUser("Guest", UserInfo.FLAG_GUEST);
219 }
220 }
221 }
222 }
223
224 @Override
225 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700226 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700227 return mGuestEnabled;
228 }
229 }
230
231 @Override
232 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700233 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700234 // TODO:
235 }
236
237 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700238 * Enforces that only the system UID or root's UID or apps that have the
239 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
240 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700241 *
242 * @param message used as message if SecurityException is thrown
243 * @throws SecurityException if the caller is not system or root
244 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700245 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700246 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700247 if (uid != Process.SYSTEM_UID && uid != 0
248 && ActivityManager.checkComponentPermission(
249 android.Manifest.permission.MANAGE_USERS,
250 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
251 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700252 }
253 }
254
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700255 private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
256 try {
257 File dir = new File(mUsersDir, Integer.toString(info.id));
258 File file = new File(dir, USER_PHOTO_FILENAME);
259 if (!dir.exists()) {
260 dir.mkdir();
261 FileUtils.setPermissions(
262 dir.getPath(),
263 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
264 -1, -1);
265 }
266 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
267 MODE_CREATE|MODE_READ_WRITE);
268 info.iconPath = file.getAbsolutePath();
269 return fd;
270 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700271 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700272 }
273 return null;
274 }
275
Amith Yamasani0b285492011-04-14 17:35:23 -0700276 /**
277 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
278 * cache it elsewhere.
279 * @return the array of user ids.
280 */
281 int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700282 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700283 return mUserIds;
284 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700285 }
286
Dianne Hackborn4428e172012-08-24 17:43:05 -0700287 int[] getUserIdsLPr() {
288 return mUserIds;
289 }
290
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700291 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700292 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700293 readUserListLocked();
294 }
295 }
296
297 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700298 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700299 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700300 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700301 return;
302 }
303 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700304 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700305 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700306 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700307 XmlPullParser parser = Xml.newPullParser();
308 parser.setInput(fis, null);
309 int type;
310 while ((type = parser.next()) != XmlPullParser.START_TAG
311 && type != XmlPullParser.END_DOCUMENT) {
312 ;
313 }
314
315 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700316 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700317 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700318 return;
319 }
320
Amith Yamasani2a003292012-08-14 18:25:45 -0700321 mNextSerialNumber = -1;
322 if (parser.getName().equals(TAG_USERS)) {
323 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
324 if (lastSerialNumber != null) {
325 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
326 }
327 }
328
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700329 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
330 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
331 String id = parser.getAttributeValue(null, ATTR_ID);
332 UserInfo user = readUser(Integer.parseInt(id));
333 if (user != null) {
334 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700335 if (user.isGuest()) {
336 mGuestEnabled = true;
337 }
338 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
339 mNextSerialNumber = user.id + 1;
340 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700341 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700342 }
343 }
Amith Yamasani13593602012-03-22 16:16:17 -0700344 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700345 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700346 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700347 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700348 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800349 } finally {
350 if (fis != null) {
351 try {
352 fis.close();
353 } catch (IOException e) {
354 }
355 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700356 }
357 }
358
Amith Yamasani13593602012-03-22 16:16:17 -0700359 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700360 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700361 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700362 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
363 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700364 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700365
Amith Yamasani13593602012-03-22 16:16:17 -0700366 writeUserListLocked();
367 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700368 }
369
370 /*
371 * Writes the user file in this format:
372 *
373 * <user flags="20039023" id="0">
374 * <name>Primary</name>
375 * </user>
376 */
Amith Yamasani13593602012-03-22 16:16:17 -0700377 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700378 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700379 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700380 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700381 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700382 final BufferedOutputStream bos = new BufferedOutputStream(fos);
383
384 // XmlSerializer serializer = XmlUtils.serializerInstance();
385 final XmlSerializer serializer = new FastXmlSerializer();
386 serializer.setOutput(bos, "utf-8");
387 serializer.startDocument(null, true);
388 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
389
390 serializer.startTag(null, TAG_USER);
391 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700392 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700393 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700394 if (userInfo.iconPath != null) {
395 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
396 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700397
398 serializer.startTag(null, TAG_NAME);
399 serializer.text(userInfo.name);
400 serializer.endTag(null, TAG_NAME);
401
402 serializer.endTag(null, TAG_USER);
403
404 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700405 userFile.finishWrite(fos);
406 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700407 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700408 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700409 }
410 }
411
412 /*
413 * Writes the user list file in this format:
414 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700415 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700416 * <user id="0"></user>
417 * <user id="2"></user>
418 * </users>
419 */
Amith Yamasani13593602012-03-22 16:16:17 -0700420 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700421 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700422 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700423 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700424 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700425 final BufferedOutputStream bos = new BufferedOutputStream(fos);
426
427 // XmlSerializer serializer = XmlUtils.serializerInstance();
428 final XmlSerializer serializer = new FastXmlSerializer();
429 serializer.setOutput(bos, "utf-8");
430 serializer.startDocument(null, true);
431 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
432
433 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700434 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700435
436 for (int i = 0; i < mUsers.size(); i++) {
437 UserInfo user = mUsers.valueAt(i);
438 serializer.startTag(null, TAG_USER);
439 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
440 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700441 }
442
443 serializer.endTag(null, TAG_USERS);
444
445 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700446 userListFile.finishWrite(fos);
447 } catch (Exception e) {
448 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700449 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700450 }
451 }
452
453 private UserInfo readUser(int id) {
454 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700455 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700456 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700457 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700458
459 FileInputStream fis = null;
460 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700461 AtomicFile userFile =
462 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
463 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700464 XmlPullParser parser = Xml.newPullParser();
465 parser.setInput(fis, null);
466 int type;
467 while ((type = parser.next()) != XmlPullParser.START_TAG
468 && type != XmlPullParser.END_DOCUMENT) {
469 ;
470 }
471
472 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700473 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700474 return null;
475 }
476
477 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
478 String storedId = parser.getAttributeValue(null, ATTR_ID);
479 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700480 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700481 return null;
482 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700483 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
484 if (serialNumberValue != null) {
485 serialNumber = Integer.parseInt(serialNumberValue);
486 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700487 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
488 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700489 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700490
491 while ((type = parser.next()) != XmlPullParser.START_TAG
492 && type != XmlPullParser.END_DOCUMENT) {
493 }
494 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
495 type = parser.next();
496 if (type == XmlPullParser.TEXT) {
497 name = parser.getText();
498 }
499 }
500 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700501
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700502 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700503 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700504 return userInfo;
505
506 } catch (IOException ioe) {
507 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800508 } finally {
509 if (fis != null) {
510 try {
511 fis.close();
512 } catch (IOException e) {
513 }
514 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700515 }
516 return null;
517 }
518
Amith Yamasani258848d2012-08-10 17:06:33 -0700519 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700520 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700521 checkManageUsersPermission("Only the system can create users");
Amith Yamasani0b285492011-04-14 17:35:23 -0700522 int userId = getNextAvailableId();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700523 UserInfo userInfo = new UserInfo(userId, name, null, flags);
Amith Yamasani0b285492011-04-14 17:35:23 -0700524 File userPath = new File(mBaseUserPath, Integer.toString(userId));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700525 synchronized (mInstallLock) {
526 synchronized (mPackagesLock) {
527 userInfo.serialNumber = mNextSerialNumber++;
528 mUsers.put(userId, userInfo);
529 writeUserListLocked();
530 writeUserLocked(userInfo);
531 updateUserIdsLocked();
532 mPm.createNewUserLILPw(userId, userPath);
533 }
Amith Yamasani13593602012-03-22 16:16:17 -0700534 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700535 if (userInfo != null) {
536 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
Amith Yamasani2a003292012-08-14 18:25:45 -0700537 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
538 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Amith Yamasanifc6e0ca2012-08-17 17:07:14 -0700539 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_BOOT_COMPLETED),
540 new UserHandle(userInfo.id));
Amith Yamasani258848d2012-08-10 17:06:33 -0700541 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700542 return userInfo;
543 }
544
Amith Yamasani0b285492011-04-14 17:35:23 -0700545 /**
546 * Removes a user and all data directories created for that user. This method should be called
547 * after the user's processes have been terminated.
548 * @param id the user's id
549 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700550 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700551 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700552 synchronized (mInstallLock) {
553 synchronized (mPackagesLock) {
554 final UserInfo user = mUsers.get(userHandle);
555 if (userHandle == 0 || user == null) {
556 return false;
557 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700558
Dianne Hackborn4428e172012-08-24 17:43:05 -0700559 // Cleanup package manager settings
560 mPm.cleanUpUserLILPw(userHandle);
561
562 // Remove this user from the list
563 mUsers.remove(userHandle);
564 // Remove user file
565 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
566 userFile.delete();
567 // Update the user list
568 writeUserListLocked();
569 updateUserIdsLocked();
570 }
571 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700572
Amith Yamasani2a003292012-08-14 18:25:45 -0700573 // Let other services shutdown any activity
574 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
575 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
576 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Dianne Hackborn4428e172012-08-24 17:43:05 -0700577 return true;
Amith Yamasani2a003292012-08-14 18:25:45 -0700578 }
579
580 @Override
581 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700582 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700583 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700584 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700585 }
586 }
587
588 @Override
589 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700590 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700591 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700592 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700593 }
594 // Not found
595 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700596 }
597 }
598
Amith Yamasani0b285492011-04-14 17:35:23 -0700599 /**
600 * Caches the list of user ids in an array, adjusting the array size when necessary.
601 */
Amith Yamasani13593602012-03-22 16:16:17 -0700602 private void updateUserIdsLocked() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700603 int[] newUsers = new int[mUsers.size()];
Amith Yamasani0b285492011-04-14 17:35:23 -0700604 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700605 newUsers[i] = mUsers.keyAt(i);
Amith Yamasani0b285492011-04-14 17:35:23 -0700606 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700607 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700608 }
609
610 /**
611 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700612 * TODO: May not be a good idea to recycle ids, in case it results in confusion
613 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700614 * @return
615 */
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700616 private int getNextAvailableId() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700617 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700618 int i = 0;
619 while (i < Integer.MAX_VALUE) {
620 if (mUsers.indexOfKey(i) < 0) {
621 break;
622 }
623 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700624 }
Amith Yamasani195263742012-08-21 15:40:12 -0700625 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700626 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700627 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700628}