blob: 8899ea2fb874012471ca40fa3c67437b86c5cd70 [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
Amith Yamasanib8151ec2012-04-18 18:02:48 -070019import static android.os.ParcelFileDescriptor.MODE_CREATE;
20import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
21
Amith Yamasani13593602012-03-22 16:16:17 -070022import com.android.internal.util.ArrayUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070023import com.android.internal.util.FastXmlSerializer;
24
Amith Yamasani2a003292012-08-14 18:25:45 -070025import android.app.ActivityManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070026import android.content.Context;
27import android.content.Intent;
Amith Yamasani0b285492011-04-14 17:35:23 -070028import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070030import android.content.pm.UserInfo;
Amith Yamasani258848d2012-08-10 17:06:33 -070031import android.os.Binder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070032import android.os.Environment;
33import android.os.FileUtils;
Amith Yamasani258848d2012-08-10 17:06:33 -070034import android.os.IUserManager;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070035import android.os.ParcelFileDescriptor;
Amith Yamasani258848d2012-08-10 17:06:33 -070036import android.os.Process;
Amith Yamasani0b285492011-04-14 17:35:23 -070037import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070038import android.os.UserHandle;
Amith Yamasani2a003292012-08-14 18:25:45 -070039import android.util.AtomicFile;
Amith Yamasani0b285492011-04-14 17:35:23 -070040import android.util.Log;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070041import android.util.Slog;
42import android.util.SparseArray;
43import android.util.Xml;
44
45import java.io.BufferedOutputStream;
46import java.io.File;
47import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070048import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070049import java.io.FileOutputStream;
50import java.io.IOException;
51import java.util.ArrayList;
52import java.util.List;
53
54import org.xmlpull.v1.XmlPullParser;
55import org.xmlpull.v1.XmlPullParserException;
56import org.xmlpull.v1.XmlSerializer;
57
Amith Yamasani258848d2012-08-10 17:06:33 -070058public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070059
Amith Yamasani2a003292012-08-14 18:25:45 -070060 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070061
Amith Yamasani4b2e9342011-03-31 12:38:53 -070062 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070063 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070064 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070065 private static final String ATTR_ID = "id";
Amith Yamasani2a003292012-08-14 18:25:45 -070066 private static final String ATTR_SERIAL_NO = "serialNumber";
67 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070068 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070069 private static final String TAG_USER = "user";
70
Amith Yamasani0b285492011-04-14 17:35:23 -070071 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070072 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070073 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070074
Amith Yamasani13593602012-03-22 16:16:17 -070075 private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
Amith Yamasani4b2e9342011-03-31 12:38:53 -070076
77 private final File mUsersDir;
78 private final File mUserListFile;
Amith Yamasani0b285492011-04-14 17:35:23 -070079 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -070080 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -070081 private int mNextSerialNumber;
Amith Yamasani0b285492011-04-14 17:35:23 -070082
83 private Installer mInstaller;
84 private File mBaseUserPath;
Amith Yamasani258848d2012-08-10 17:06:33 -070085 private Context mContext;
86 private static UserManagerService sInstance;
87 private PackageManagerService mPm;
88
89 public synchronized static UserManagerService getInstance(Context context) {
90 if (sInstance == null) {
91 sInstance = new UserManagerService(context);
92 }
93 return sInstance;
94 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070095
96 /**
97 * Available for testing purposes.
98 */
Amith Yamasani258848d2012-08-10 17:06:33 -070099 UserManagerService(File dataDir, File baseUserPath) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700100 mUsersDir = new File(dataDir, USER_INFO_DIR);
101 mUsersDir.mkdirs();
Amith Yamasani483f3b02012-03-13 16:08:00 -0700102 // Make zeroth user directory, for services to migrate their files to that location
103 File userZeroDir = new File(mUsersDir, "0");
104 userZeroDir.mkdirs();
Amith Yamasani0b285492011-04-14 17:35:23 -0700105 mBaseUserPath = baseUserPath;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700106 FileUtils.setPermissions(mUsersDir.toString(),
107 FileUtils.S_IRWXU|FileUtils.S_IRWXG
108 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
109 -1, -1);
110 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
111 readUserList();
112 }
113
Amith Yamasani258848d2012-08-10 17:06:33 -0700114 public UserManagerService(Context context) {
115 this(Environment.getDataDirectory(), new File(Environment.getDataDirectory(), "user"));
116 mContext = context;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700117 }
118
Amith Yamasani258848d2012-08-10 17:06:33 -0700119 void setInstaller(PackageManagerService pm, Installer installer) {
120 mInstaller = installer;
121 mPm = pm;
122 }
123
124 @Override
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700125 public List<UserInfo> getUsers() {
Amith Yamasani2a003292012-08-14 18:25:45 -0700126 checkManageUsersPermission("query users");
Amith Yamasani13593602012-03-22 16:16:17 -0700127 synchronized (mUsers) {
128 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
129 for (int i = 0; i < mUsers.size(); i++) {
130 users.add(mUsers.valueAt(i));
131 }
132 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700133 }
Amith Yamasani13593602012-03-22 16:16:17 -0700134 }
135
Amith Yamasani258848d2012-08-10 17:06:33 -0700136 @Override
137 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700138 checkManageUsersPermission("query user");
Amith Yamasani13593602012-03-22 16:16:17 -0700139 synchronized (mUsers) {
Amith Yamasani195263742012-08-21 15:40:12 -0700140 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700141 }
142 }
143
Amith Yamasani195263742012-08-21 15:40:12 -0700144 /*
145 * Should be locked on mUsers before calling this.
146 */
147 private UserInfo getUserInfoLocked(int userId) {
148 return mUsers.get(userId);
149 }
150
Amith Yamasani13593602012-03-22 16:16:17 -0700151 public boolean exists(int userId) {
152 synchronized (mUsers) {
153 return ArrayUtils.contains(mUserIds, userId);
154 }
155 }
156
Amith Yamasani258848d2012-08-10 17:06:33 -0700157 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700158 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700159 checkManageUsersPermission("rename users");
Amith Yamasani13593602012-03-22 16:16:17 -0700160 synchronized (mUsers) {
161 UserInfo info = mUsers.get(userId);
162 if (name != null && !name.equals(info.name)) {
163 info.name = name;
164 writeUserLocked(info);
165 }
166 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700167 }
168
Amith Yamasani258848d2012-08-10 17:06:33 -0700169 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700170 public ParcelFileDescriptor setUserIcon(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700171 checkManageUsersPermission("update users");
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700172 synchronized (mUsers) {
173 UserInfo info = mUsers.get(userId);
174 if (info == null) return null;
175 ParcelFileDescriptor fd = updateIconBitmapLocked(info);
176 if (fd != null) {
177 writeUserLocked(info);
178 }
179 return fd;
180 }
181 }
182
Amith Yamasani258848d2012-08-10 17:06:33 -0700183 @Override
184 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700185 checkManageUsersPermission("enable guest users");
Amith Yamasani258848d2012-08-10 17:06:33 -0700186 synchronized (mUsers) {
187 if (mGuestEnabled != enable) {
188 mGuestEnabled = enable;
189 // Erase any guest user that currently exists
190 for (int i = 0; i < mUsers.size(); i++) {
191 UserInfo user = mUsers.valueAt(i);
192 if (user.isGuest()) {
193 if (!enable) {
194 removeUser(user.id);
195 }
196 return;
197 }
198 }
199 // No guest was found
200 if (enable) {
201 createUser("Guest", UserInfo.FLAG_GUEST);
202 }
203 }
204 }
205 }
206
207 @Override
208 public boolean isGuestEnabled() {
209 synchronized (mUsers) {
210 return mGuestEnabled;
211 }
212 }
213
214 @Override
215 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700216 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700217 // TODO:
218 }
219
220 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700221 * Enforces that only the system UID or root's UID or apps that have the
222 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
223 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700224 *
225 * @param message used as message if SecurityException is thrown
226 * @throws SecurityException if the caller is not system or root
227 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700228 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700229 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700230 if (uid != Process.SYSTEM_UID && uid != 0
231 && ActivityManager.checkComponentPermission(
232 android.Manifest.permission.MANAGE_USERS,
233 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
234 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700235 }
236 }
237
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700238 private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
239 try {
240 File dir = new File(mUsersDir, Integer.toString(info.id));
241 File file = new File(dir, USER_PHOTO_FILENAME);
242 if (!dir.exists()) {
243 dir.mkdir();
244 FileUtils.setPermissions(
245 dir.getPath(),
246 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
247 -1, -1);
248 }
249 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
250 MODE_CREATE|MODE_READ_WRITE);
251 info.iconPath = file.getAbsolutePath();
252 return fd;
253 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700254 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700255 }
256 return null;
257 }
258
Amith Yamasani0b285492011-04-14 17:35:23 -0700259 /**
260 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
261 * cache it elsewhere.
262 * @return the array of user ids.
263 */
264 int[] getUserIds() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700265 synchronized (mUsers) {
266 return mUserIds;
267 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700268 }
269
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700270 private void readUserList() {
Amith Yamasani13593602012-03-22 16:16:17 -0700271 synchronized (mUsers) {
272 readUserListLocked();
273 }
274 }
275
276 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700277 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700278 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700279 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700280 return;
281 }
282 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700283 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700284 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700285 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700286 XmlPullParser parser = Xml.newPullParser();
287 parser.setInput(fis, null);
288 int type;
289 while ((type = parser.next()) != XmlPullParser.START_TAG
290 && type != XmlPullParser.END_DOCUMENT) {
291 ;
292 }
293
294 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700295 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700296 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700297 return;
298 }
299
Amith Yamasani2a003292012-08-14 18:25:45 -0700300 mNextSerialNumber = -1;
301 if (parser.getName().equals(TAG_USERS)) {
302 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
303 if (lastSerialNumber != null) {
304 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
305 }
306 }
307
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700308 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
309 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
310 String id = parser.getAttributeValue(null, ATTR_ID);
311 UserInfo user = readUser(Integer.parseInt(id));
312 if (user != null) {
313 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700314 if (user.isGuest()) {
315 mGuestEnabled = true;
316 }
317 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
318 mNextSerialNumber = user.id + 1;
319 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700320 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700321 }
322 }
Amith Yamasani13593602012-03-22 16:16:17 -0700323 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700324 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700325 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700326 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700327 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800328 } finally {
329 if (fis != null) {
330 try {
331 fis.close();
332 } catch (IOException e) {
333 }
334 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700335 }
336 }
337
Amith Yamasani13593602012-03-22 16:16:17 -0700338 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700339 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700340 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700341 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
342 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700343 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700344
Amith Yamasani13593602012-03-22 16:16:17 -0700345 writeUserListLocked();
346 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700347 }
348
349 /*
350 * Writes the user file in this format:
351 *
352 * <user flags="20039023" id="0">
353 * <name>Primary</name>
354 * </user>
355 */
Amith Yamasani13593602012-03-22 16:16:17 -0700356 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700357 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700358 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700359 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700360 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700361 final BufferedOutputStream bos = new BufferedOutputStream(fos);
362
363 // XmlSerializer serializer = XmlUtils.serializerInstance();
364 final XmlSerializer serializer = new FastXmlSerializer();
365 serializer.setOutput(bos, "utf-8");
366 serializer.startDocument(null, true);
367 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
368
369 serializer.startTag(null, TAG_USER);
370 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700371 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700372 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700373 if (userInfo.iconPath != null) {
374 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
375 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700376
377 serializer.startTag(null, TAG_NAME);
378 serializer.text(userInfo.name);
379 serializer.endTag(null, TAG_NAME);
380
381 serializer.endTag(null, TAG_USER);
382
383 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700384 userFile.finishWrite(fos);
385 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700386 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700387 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700388 }
389 }
390
391 /*
392 * Writes the user list file in this format:
393 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700394 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700395 * <user id="0"></user>
396 * <user id="2"></user>
397 * </users>
398 */
Amith Yamasani13593602012-03-22 16:16:17 -0700399 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700400 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700401 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700402 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700403 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700404 final BufferedOutputStream bos = new BufferedOutputStream(fos);
405
406 // XmlSerializer serializer = XmlUtils.serializerInstance();
407 final XmlSerializer serializer = new FastXmlSerializer();
408 serializer.setOutput(bos, "utf-8");
409 serializer.startDocument(null, true);
410 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
411
412 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700413 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700414
415 for (int i = 0; i < mUsers.size(); i++) {
416 UserInfo user = mUsers.valueAt(i);
417 serializer.startTag(null, TAG_USER);
418 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
419 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700420 }
421
422 serializer.endTag(null, TAG_USERS);
423
424 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700425 userListFile.finishWrite(fos);
426 } catch (Exception e) {
427 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700428 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700429 }
430 }
431
432 private UserInfo readUser(int id) {
433 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700434 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700435 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700436 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700437
438 FileInputStream fis = null;
439 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700440 AtomicFile userFile =
441 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
442 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700443 XmlPullParser parser = Xml.newPullParser();
444 parser.setInput(fis, null);
445 int type;
446 while ((type = parser.next()) != XmlPullParser.START_TAG
447 && type != XmlPullParser.END_DOCUMENT) {
448 ;
449 }
450
451 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700452 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700453 return null;
454 }
455
456 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
457 String storedId = parser.getAttributeValue(null, ATTR_ID);
458 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700459 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700460 return null;
461 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700462 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
463 if (serialNumberValue != null) {
464 serialNumber = Integer.parseInt(serialNumberValue);
465 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700466 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
467 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700468 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700469
470 while ((type = parser.next()) != XmlPullParser.START_TAG
471 && type != XmlPullParser.END_DOCUMENT) {
472 }
473 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
474 type = parser.next();
475 if (type == XmlPullParser.TEXT) {
476 name = parser.getText();
477 }
478 }
479 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700480
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700481 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700482 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700483 return userInfo;
484
485 } catch (IOException ioe) {
486 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800487 } finally {
488 if (fis != null) {
489 try {
490 fis.close();
491 } catch (IOException e) {
492 }
493 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700494 }
495 return null;
496 }
497
Amith Yamasani258848d2012-08-10 17:06:33 -0700498 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700499 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700500 checkManageUsersPermission("Only the system can create users");
Amith Yamasani0b285492011-04-14 17:35:23 -0700501 int userId = getNextAvailableId();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700502 UserInfo userInfo = new UserInfo(userId, name, null, flags);
Amith Yamasani0b285492011-04-14 17:35:23 -0700503 File userPath = new File(mBaseUserPath, Integer.toString(userId));
Amith Yamasani13593602012-03-22 16:16:17 -0700504 if (!createPackageFolders(userId, userPath)) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700505 return null;
506 }
Amith Yamasani13593602012-03-22 16:16:17 -0700507 synchronized (mUsers) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700508 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani13593602012-03-22 16:16:17 -0700509 mUsers.put(userId, userInfo);
510 writeUserListLocked();
511 writeUserLocked(userInfo);
512 updateUserIdsLocked();
513 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700514 if (userInfo != null) {
515 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
Amith Yamasani2a003292012-08-14 18:25:45 -0700516 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
517 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Amith Yamasanifc6e0ca2012-08-17 17:07:14 -0700518 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_BOOT_COMPLETED),
519 new UserHandle(userInfo.id));
Amith Yamasani258848d2012-08-10 17:06:33 -0700520 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700521 return userInfo;
522 }
523
Amith Yamasani0b285492011-04-14 17:35:23 -0700524 /**
525 * Removes a user and all data directories created for that user. This method should be called
526 * after the user's processes have been terminated.
527 * @param id the user's id
528 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700529 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700530 checkManageUsersPermission("Only the system can remove users");
531 boolean result;
Amith Yamasani13593602012-03-22 16:16:17 -0700532 synchronized (mUsers) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700533 result = removeUserLocked(userHandle);
534 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700535
536 // Cleanup package manager settings
537 mPm.cleanUpUser(userHandle);
538
Amith Yamasani2a003292012-08-14 18:25:45 -0700539 // Let other services shutdown any activity
540 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
541 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
542 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
543 return result;
544 }
545
546 @Override
547 public int getUserSerialNumber(int userHandle) {
548 synchronized (mUsers) {
549 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700550 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700551 }
552 }
553
554 @Override
555 public int getUserHandle(int userSerialNumber) {
556 synchronized (mUsers) {
557 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700558 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700559 }
560 // Not found
561 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700562 }
563 }
564
Amith Yamasani258848d2012-08-10 17:06:33 -0700565 private boolean removeUserLocked(int userHandle) {
566 final UserInfo user = mUsers.get(userHandle);
567 if (userHandle == 0 || user == null) {
568 return false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700569 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700570
Amith Yamasani258848d2012-08-10 17:06:33 -0700571 // Remove this user from the list
572 mUsers.remove(userHandle);
573 // Remove user file
Amith Yamasani2a003292012-08-14 18:25:45 -0700574 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
Amith Yamasani258848d2012-08-10 17:06:33 -0700575 userFile.delete();
576 // Update the user list
577 writeUserListLocked();
578 updateUserIdsLocked();
579
Amith Yamasani258848d2012-08-10 17:06:33 -0700580 removePackageFolders(userHandle);
581 return true;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700582 }
583
Amith Yamasani0b285492011-04-14 17:35:23 -0700584 public void installPackageForAllUsers(String packageName, int uid) {
585 for (int userId : mUserIds) {
586 // Don't do it for the primary user, it will become recursive.
587 if (userId == 0)
588 continue;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700589 mInstaller.createUserData(packageName, UserHandle.getUid(userId, uid),
Amith Yamasani0b285492011-04-14 17:35:23 -0700590 userId);
591 }
592 }
593
594 public void clearUserDataForAllUsers(String packageName) {
595 for (int userId : mUserIds) {
596 // Don't do it for the primary user, it will become recursive.
597 if (userId == 0)
598 continue;
599 mInstaller.clearUserData(packageName, userId);
600 }
601 }
602
603 public void removePackageForAllUsers(String packageName) {
604 for (int userId : mUserIds) {
605 // Don't do it for the primary user, it will become recursive.
606 if (userId == 0)
607 continue;
608 mInstaller.remove(packageName, userId);
609 }
610 }
611
612 /**
613 * Caches the list of user ids in an array, adjusting the array size when necessary.
614 */
Amith Yamasani13593602012-03-22 16:16:17 -0700615 private void updateUserIdsLocked() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700616 int[] newUsers = new int[mUsers.size()];
Amith Yamasani0b285492011-04-14 17:35:23 -0700617 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700618 newUsers[i] = mUsers.keyAt(i);
Amith Yamasani0b285492011-04-14 17:35:23 -0700619 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700620 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700621 }
622
623 /**
624 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700625 * TODO: May not be a good idea to recycle ids, in case it results in confusion
626 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700627 * @return
628 */
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700629 private int getNextAvailableId() {
Amith Yamasani195263742012-08-21 15:40:12 -0700630 synchronized (mUsers) {
631 int i = 0;
632 while (i < Integer.MAX_VALUE) {
633 if (mUsers.indexOfKey(i) < 0) {
634 break;
635 }
636 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700637 }
Amith Yamasani195263742012-08-21 15:40:12 -0700638 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700639 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700640 }
641
Amith Yamasani13593602012-03-22 16:16:17 -0700642 private boolean createPackageFolders(int id, File userPath) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700643 // mInstaller may not be available for unit-tests.
Amith Yamasani13593602012-03-22 16:16:17 -0700644 if (mInstaller == null) return true;
Amith Yamasani0b285492011-04-14 17:35:23 -0700645
Amith Yamasani0b285492011-04-14 17:35:23 -0700646 // Create the user path
647 userPath.mkdir();
648 FileUtils.setPermissions(userPath.toString(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
649 | FileUtils.S_IXOTH, -1, -1);
650
Amith Yamasani742a6712011-05-04 14:49:28 -0700651 mInstaller.cloneUserData(0, id, false);
652
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700653 return true;
654 }
655
Amith Yamasani13593602012-03-22 16:16:17 -0700656 boolean removePackageFolders(int id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700657 // mInstaller may not be available for unit-tests.
658 if (mInstaller == null) return true;
659
660 mInstaller.removeUserDataDirs(id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700661 return true;
662 }
663}