blob: f5b4053bd0b29064e67429e0da8969f0e57f5707 [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() {
265 return mUserIds;
266 }
267
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700268 private void readUserList() {
Amith Yamasani13593602012-03-22 16:16:17 -0700269 synchronized (mUsers) {
270 readUserListLocked();
271 }
272 }
273
274 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700275 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700276 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700277 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700278 return;
279 }
280 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700281 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700282 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700283 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700284 XmlPullParser parser = Xml.newPullParser();
285 parser.setInput(fis, null);
286 int type;
287 while ((type = parser.next()) != XmlPullParser.START_TAG
288 && type != XmlPullParser.END_DOCUMENT) {
289 ;
290 }
291
292 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700293 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700294 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700295 return;
296 }
297
Amith Yamasani2a003292012-08-14 18:25:45 -0700298 mNextSerialNumber = -1;
299 if (parser.getName().equals(TAG_USERS)) {
300 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
301 if (lastSerialNumber != null) {
302 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
303 }
304 }
305
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700306 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
307 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
308 String id = parser.getAttributeValue(null, ATTR_ID);
309 UserInfo user = readUser(Integer.parseInt(id));
310 if (user != null) {
311 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700312 if (user.isGuest()) {
313 mGuestEnabled = true;
314 }
315 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
316 mNextSerialNumber = user.id + 1;
317 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700318 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700319 }
320 }
Amith Yamasani13593602012-03-22 16:16:17 -0700321 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700322 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700323 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700324 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700325 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800326 } finally {
327 if (fis != null) {
328 try {
329 fis.close();
330 } catch (IOException e) {
331 }
332 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700333 }
334 }
335
Amith Yamasani13593602012-03-22 16:16:17 -0700336 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700337 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700338 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700339 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
340 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700341 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700342
Amith Yamasani13593602012-03-22 16:16:17 -0700343 writeUserListLocked();
344 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700345 }
346
347 /*
348 * Writes the user file in this format:
349 *
350 * <user flags="20039023" id="0">
351 * <name>Primary</name>
352 * </user>
353 */
Amith Yamasani13593602012-03-22 16:16:17 -0700354 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700355 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700356 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700357 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700358 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700359 final BufferedOutputStream bos = new BufferedOutputStream(fos);
360
361 // XmlSerializer serializer = XmlUtils.serializerInstance();
362 final XmlSerializer serializer = new FastXmlSerializer();
363 serializer.setOutput(bos, "utf-8");
364 serializer.startDocument(null, true);
365 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
366
367 serializer.startTag(null, TAG_USER);
368 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700369 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700370 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700371 if (userInfo.iconPath != null) {
372 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
373 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700374
375 serializer.startTag(null, TAG_NAME);
376 serializer.text(userInfo.name);
377 serializer.endTag(null, TAG_NAME);
378
379 serializer.endTag(null, TAG_USER);
380
381 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700382 userFile.finishWrite(fos);
383 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700384 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700385 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700386 }
387 }
388
389 /*
390 * Writes the user list file in this format:
391 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700392 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700393 * <user id="0"></user>
394 * <user id="2"></user>
395 * </users>
396 */
Amith Yamasani13593602012-03-22 16:16:17 -0700397 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700398 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700399 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700400 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700401 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700402 final BufferedOutputStream bos = new BufferedOutputStream(fos);
403
404 // XmlSerializer serializer = XmlUtils.serializerInstance();
405 final XmlSerializer serializer = new FastXmlSerializer();
406 serializer.setOutput(bos, "utf-8");
407 serializer.startDocument(null, true);
408 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
409
410 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700411 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700412
413 for (int i = 0; i < mUsers.size(); i++) {
414 UserInfo user = mUsers.valueAt(i);
415 serializer.startTag(null, TAG_USER);
416 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
417 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700418 }
419
420 serializer.endTag(null, TAG_USERS);
421
422 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700423 userListFile.finishWrite(fos);
424 } catch (Exception e) {
425 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700426 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700427 }
428 }
429
430 private UserInfo readUser(int id) {
431 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700432 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700433 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700434 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700435
436 FileInputStream fis = null;
437 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700438 AtomicFile userFile =
439 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
440 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700441 XmlPullParser parser = Xml.newPullParser();
442 parser.setInput(fis, null);
443 int type;
444 while ((type = parser.next()) != XmlPullParser.START_TAG
445 && type != XmlPullParser.END_DOCUMENT) {
446 ;
447 }
448
449 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700450 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700451 return null;
452 }
453
454 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
455 String storedId = parser.getAttributeValue(null, ATTR_ID);
456 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700457 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700458 return null;
459 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700460 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
461 if (serialNumberValue != null) {
462 serialNumber = Integer.parseInt(serialNumberValue);
463 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700464 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
465 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700466 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700467
468 while ((type = parser.next()) != XmlPullParser.START_TAG
469 && type != XmlPullParser.END_DOCUMENT) {
470 }
471 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
472 type = parser.next();
473 if (type == XmlPullParser.TEXT) {
474 name = parser.getText();
475 }
476 }
477 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700478
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700479 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700480 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700481 return userInfo;
482
483 } catch (IOException ioe) {
484 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800485 } finally {
486 if (fis != null) {
487 try {
488 fis.close();
489 } catch (IOException e) {
490 }
491 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700492 }
493 return null;
494 }
495
Amith Yamasani258848d2012-08-10 17:06:33 -0700496 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700497 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700498 checkManageUsersPermission("Only the system can create users");
Amith Yamasani0b285492011-04-14 17:35:23 -0700499 int userId = getNextAvailableId();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700500 UserInfo userInfo = new UserInfo(userId, name, null, flags);
Amith Yamasani0b285492011-04-14 17:35:23 -0700501 File userPath = new File(mBaseUserPath, Integer.toString(userId));
Amith Yamasani13593602012-03-22 16:16:17 -0700502 if (!createPackageFolders(userId, userPath)) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700503 return null;
504 }
Amith Yamasani13593602012-03-22 16:16:17 -0700505 synchronized (mUsers) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700506 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani13593602012-03-22 16:16:17 -0700507 mUsers.put(userId, userInfo);
508 writeUserListLocked();
509 writeUserLocked(userInfo);
510 updateUserIdsLocked();
511 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700512 if (userInfo != null) {
513 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
Amith Yamasani2a003292012-08-14 18:25:45 -0700514 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
515 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Amith Yamasanifc6e0ca2012-08-17 17:07:14 -0700516 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_BOOT_COMPLETED),
517 new UserHandle(userInfo.id));
Amith Yamasani258848d2012-08-10 17:06:33 -0700518 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700519 return userInfo;
520 }
521
Amith Yamasani0b285492011-04-14 17:35:23 -0700522 /**
523 * Removes a user and all data directories created for that user. This method should be called
524 * after the user's processes have been terminated.
525 * @param id the user's id
526 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700527 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700528 checkManageUsersPermission("Only the system can remove users");
529 boolean result;
Amith Yamasani13593602012-03-22 16:16:17 -0700530 synchronized (mUsers) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700531 result = removeUserLocked(userHandle);
532 }
533 // Let other services shutdown any activity
534 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
535 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
536 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
537 return result;
538 }
539
540 @Override
541 public int getUserSerialNumber(int userHandle) {
542 synchronized (mUsers) {
543 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700544 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700545 }
546 }
547
548 @Override
549 public int getUserHandle(int userSerialNumber) {
550 synchronized (mUsers) {
551 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700552 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700553 }
554 // Not found
555 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700556 }
557 }
558
Amith Yamasani258848d2012-08-10 17:06:33 -0700559 private boolean removeUserLocked(int userHandle) {
560 final UserInfo user = mUsers.get(userHandle);
561 if (userHandle == 0 || user == null) {
562 return false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700563 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700564
565 mPm.cleanUpUser(userHandle);
566
567 // Remove this user from the list
568 mUsers.remove(userHandle);
569 // Remove user file
Amith Yamasani2a003292012-08-14 18:25:45 -0700570 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
Amith Yamasani258848d2012-08-10 17:06:33 -0700571 userFile.delete();
572 // Update the user list
573 writeUserListLocked();
574 updateUserIdsLocked();
575
Amith Yamasani258848d2012-08-10 17:06:33 -0700576 removePackageFolders(userHandle);
577 return true;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700578 }
579
Amith Yamasani0b285492011-04-14 17:35:23 -0700580 public void installPackageForAllUsers(String packageName, int uid) {
581 for (int userId : mUserIds) {
582 // Don't do it for the primary user, it will become recursive.
583 if (userId == 0)
584 continue;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700585 mInstaller.createUserData(packageName, UserHandle.getUid(userId, uid),
Amith Yamasani0b285492011-04-14 17:35:23 -0700586 userId);
587 }
588 }
589
590 public void clearUserDataForAllUsers(String packageName) {
591 for (int userId : mUserIds) {
592 // Don't do it for the primary user, it will become recursive.
593 if (userId == 0)
594 continue;
595 mInstaller.clearUserData(packageName, userId);
596 }
597 }
598
599 public void removePackageForAllUsers(String packageName) {
600 for (int userId : mUserIds) {
601 // Don't do it for the primary user, it will become recursive.
602 if (userId == 0)
603 continue;
604 mInstaller.remove(packageName, userId);
605 }
606 }
607
608 /**
609 * Caches the list of user ids in an array, adjusting the array size when necessary.
610 */
Amith Yamasani13593602012-03-22 16:16:17 -0700611 private void updateUserIdsLocked() {
Amith Yamasani0b285492011-04-14 17:35:23 -0700612 if (mUserIds == null || mUserIds.length != mUsers.size()) {
613 mUserIds = new int[mUsers.size()];
614 }
615 for (int i = 0; i < mUsers.size(); i++) {
616 mUserIds[i] = mUsers.keyAt(i);
617 }
618 }
619
620 /**
621 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700622 * TODO: May not be a good idea to recycle ids, in case it results in confusion
623 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700624 * @return
625 */
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700626 private int getNextAvailableId() {
Amith Yamasani195263742012-08-21 15:40:12 -0700627 synchronized (mUsers) {
628 int i = 0;
629 while (i < Integer.MAX_VALUE) {
630 if (mUsers.indexOfKey(i) < 0) {
631 break;
632 }
633 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700634 }
Amith Yamasani195263742012-08-21 15:40:12 -0700635 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700636 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700637 }
638
Amith Yamasani13593602012-03-22 16:16:17 -0700639 private boolean createPackageFolders(int id, File userPath) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700640 // mInstaller may not be available for unit-tests.
Amith Yamasani13593602012-03-22 16:16:17 -0700641 if (mInstaller == null) return true;
Amith Yamasani0b285492011-04-14 17:35:23 -0700642
Amith Yamasani0b285492011-04-14 17:35:23 -0700643 // Create the user path
644 userPath.mkdir();
645 FileUtils.setPermissions(userPath.toString(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
646 | FileUtils.S_IXOTH, -1, -1);
647
Amith Yamasani742a6712011-05-04 14:49:28 -0700648 mInstaller.cloneUserData(0, id, false);
649
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700650 return true;
651 }
652
Amith Yamasani13593602012-03-22 16:16:17 -0700653 boolean removePackageFolders(int id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700654 // mInstaller may not be available for unit-tests.
655 if (mInstaller == null) return true;
656
657 mInstaller.removeUserDataDirs(id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700658 return true;
659 }
660}